MCPcopy Create free account
hub / github.com/cli/cli / Commits

Method Commits

git/client.go:360–403  ·  view source on GitHub ↗
(ctx context.Context, baseRef, headRef string)

Source from the content-addressed store, hash-verified

358}
359
360func (c *Client) Commits(ctx context.Context, baseRef, headRef string) ([]*Commit, error) {
361 // The formatting directive %x00 indicates that git should include the null byte as a separator.
362 // We use this because it is not a valid character to include in a commit message. Previously,
363 // commas were used here but when we Split on them, we would get incorrect results if commit titles
364 // happened to contain them.
365 // https://git-scm.com/docs/pretty-formats#Documentation/pretty-formats.txt-emx00em
366 args := []string{"-c", "log.ShowSignature=false", "log", "--pretty=format:%H%x00%s%x00%b%x00", "--cherry", fmt.Sprintf("%s...%s", baseRef, headRef)}
367 cmd, err := c.Command(ctx, args...)
368 if err != nil {
369 return nil, err
370 }
371 out, err := cmd.Output()
372 if err != nil {
373 return nil, err
374 }
375
376 commits := []*Commit{}
377 commitLogs := commitLogRE.FindAllString(string(out), -1)
378 for _, commitLog := range commitLogs {
379 // Each line looks like this:
380 // 6a6872b918c601a0e730710ad8473938a7516d30\u0000title 1\u0000Body 1\u0000\n
381
382 // Or with an optional body:
383 // 6a6872b918c601a0e730710ad8473938a7516d30\u0000title 1\u0000\u0000\n
384
385 // Therefore after splitting we will have:
386 // ["6a6872b918c601a0e730710ad8473938a7516d30", "title 1", "Body 1", ""]
387
388 // Or with an optional body:
389 // ["6a6872b918c601a0e730710ad8473938a7516d30", "title 1", "", ""]
390 commitLogParts := strings.Split(commitLog, "\u0000")
391 commits = append(commits, &Commit{
392 Sha: commitLogParts[0],
393 Title: commitLogParts[1],
394 Body: commitLogParts[2],
395 })
396 }
397
398 if len(commits) == 0 {
399 return nil, fmt.Errorf("could not find any commits between %s and %s", baseRef, headRef)
400 }
401
402 return commits, nil
403}
404
405func (c *Client) LastCommit(ctx context.Context) (*Commit, error) {
406 output, err := c.lookupCommit(ctx, "HEAD", "%H,%s")

Callers

nothing calls this directly

Calls 3

CommandMethod · 0.95
OutputMethod · 0.65
ErrorfMethod · 0.65

Tested by

no test coverage detected