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

Method Commits

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

Source from the content-addressed store, hash-verified

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

Callers 1

TestClientCommitsFunction · 0.95

Calls 3

CommandMethod · 0.95
OutputMethod · 0.65
ErrorfMethod · 0.65

Tested by 1

TestClientCommitsFunction · 0.76