MCPcopy
hub / github.com/cli/cli / getCommits

Function getCommits

pkg/cmd/repo/garden/http.go:16–68  ·  view source on GitHub ↗
(client *http.Client, repo ghrepo.Interface, maxCommits int)

Source from the content-addressed store, hash-verified

14)
15
16func getCommits(client *http.Client, repo ghrepo.Interface, maxCommits int) ([]*Commit, error) {
17 type Item struct {
18 Author struct {
19 Login string
20 }
21 Sha string
22 }
23
24 type Result []Item
25
26 commits := []*Commit{}
27
28 pathF := func(page int) string {
29 return fmt.Sprintf("repos/%s/%s/commits?per_page=100&page=%d", repo.RepoOwner(), repo.RepoName(), page)
30 }
31
32 page := 1
33 paginating := true
34 for paginating {
35 if len(commits) >= maxCommits {
36 break
37 }
38 result := Result{}
39 links, err := getResponse(client, repo.RepoHost(), pathF(page), &result)
40 if err != nil {
41 return nil, err
42 }
43 for _, r := range result {
44 colorFunc := shaToColorFunc(r.Sha)
45 handle := r.Author.Login
46 if handle == "" {
47 handle = "a mysterious stranger"
48 }
49 commits = append(commits, &Commit{
50 Handle: handle,
51 Sha: r.Sha,
52 Char: colorFunc(string(handle[0])),
53 })
54 }
55 if len(links) == 0 || !strings.Contains(links[0], "last") {
56 paginating = false
57 }
58 page++
59 time.Sleep(500)
60 }
61
62 // reverse to get older commits first
63 for i, j := 0, len(commits)-1; i < j; i, j = i+1, j-1 {
64 commits[i], commits[j] = commits[j], commits[i]
65 }
66
67 return commits, nil
68}
69
70// getResponse performs the API call and returns the response's link header values.
71// If the "Link" header is missing, the returned slice will be nil.

Callers 1

gardenRunFunction · 0.85

Calls 7

getResponseFunction · 0.85
shaToColorFuncFunction · 0.85
colorFuncFuncType · 0.85
ContainsMethod · 0.80
RepoOwnerMethod · 0.65
RepoNameMethod · 0.65
RepoHostMethod · 0.65

Tested by

no test coverage detected