MCPcopy Index your code
hub / github.com/cli/cli / Commits

Method Commits

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

Source from the content-addressed store, hash-verified

302}
303
304func (c *Client) Commits(ctx context.Context, baseRef, headRef string) ([]*Commit, error) {
305 // The formatting directive %x00 indicates that git should include the null byte as a separator.
306 // We use this because it is not a valid character to include in a commit message. Previously,
307 // commas were used here but when we Split on them, we would get incorrect results if commit titles
308 // happened to contain them.
309 // https://git-scm.com/docs/pretty-formats#Documentation/pretty-formats.txt-emx00em
310 args := []string{"-c", "log.ShowSignature=false", "log", "--pretty=format:%H%x00%s%x00%b%x00", "--cherry", fmt.Sprintf("%s...%s", baseRef, headRef)}
311 cmd, err := c.Command(ctx, args...)
312 if err != nil {
313 return nil, err
314 }
315 out, err := cmd.Output()
316 if err != nil {
317 return nil, err
318 }
319
320 commits := []*Commit{}
321 commitLogs := commitLogRE.FindAllString(string(out), -1)
322 for _, commitLog := range commitLogs {
323 // Each line looks like this:
324 // 6a6872b918c601a0e730710ad8473938a7516d30\u0000title 1\u0000Body 1\u0000\n
325
326 // Or with an optional body:
327 // 6a6872b918c601a0e730710ad8473938a7516d30\u0000title 1\u0000\u0000\n
328
329 // Therefore after splitting we will have:
330 // ["6a6872b918c601a0e730710ad8473938a7516d30", "title 1", "Body 1", ""]
331
332 // Or with an optional body:
333 // ["6a6872b918c601a0e730710ad8473938a7516d30", "title 1", "", ""]
334 commitLogParts := strings.Split(commitLog, "\u0000")
335 commits = append(commits, &Commit{
336 Sha: commitLogParts[0],
337 Title: commitLogParts[1],
338 Body: commitLogParts[2],
339 })
340 }
341
342 if len(commits) == 0 {
343 return nil, fmt.Errorf("could not find any commits between %s and %s", baseRef, headRef)
344 }
345
346 return commits, nil
347}
348
349func (c *Client) LastCommit(ctx context.Context) (*Commit, error) {
350 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