internal/gitutil/messages.go
156 lines · 4915 bytes
1package gitutil
2
3import (
4 "fmt"
5 "os/exec"
6 "strings"
7
8 "gitbay.org/gitbay/internal/toolpath"
9)
10
11const zeroSHA = "0000000000000000000000000000000000000000"
12
13type CommitAuthor struct {
14 SHA string
15 Email string
16 Day string // author date, YYYY-MM-DD
17}
18
19// RevListAuthors returns sha, author email, and author date for commits
20// reachable from new but not old, capped at max. Empty or zero old lists
21// from new alone.
22func RevListAuthors(dir, old, new string, max int) ([]CommitAuthor, error) {
23 args := []string{"-C", dir, "log", fmt.Sprintf("--max-count=%d", max), "--format=%H%x00%ae%x00%as", "--end-of-options", new}
24 if old != "" && old != zeroSHA {
25 args = append(args, "^"+old)
26 }
27 out, err := exec.Command(toolpath.Look("git"), args...).Output()
28 if err != nil {
29 return nil, fmt.Errorf("rev-list authors: %w", err)
30 }
31 var authors []CommitAuthor
32 for _, line := range strings.Split(strings.TrimSpace(string(out)), "\n") {
33 parts := strings.SplitN(line, "\x00", 3)
34 if len(parts) != 3 {
35 continue
36 }
37 authors = append(authors, CommitAuthor{SHA: parts[0], Email: parts[1], Day: parts[2]})
38 }
39 return authors, nil
40}
41
42// Parents returns a commit's parent shas.
43func Parents(dir, sha string) []string {
44 out, err := exec.Command(toolpath.Look("git"), "-C", dir, "log", "-1", "--format=%P", "--end-of-options", sha).Output()
45 if err != nil {
46 return nil
47 }
48 return strings.Fields(string(out))
49}
50
51// FirstParents maps each of shas to its first parent, in one git call. A
52// root commit, or a sha git cannot read, has no entry.
53func FirstParents(dir string, shas []string) map[string]string {
54 out := map[string]string{}
55 if len(shas) == 0 {
56 return out
57 }
58 args := append([]string{"-C", dir, "log", "--no-walk=unsorted", "--format=%H %P", "--end-of-options"}, shas...)
59 raw, err := exec.Command(toolpath.Look("git"), args...).Output()
60 if err != nil {
61 return out
62 }
63 for _, line := range strings.Split(string(raw), "\n") {
64 if f := strings.Fields(line); len(f) >= 2 {
65 out[f[0]] = f[1]
66 }
67 }
68 return out
69}
70
71// LastCommitDate returns the committer date (YYYY-MM-DD) of the ref tip,
72// or "" for empty repos.
73func LastCommitDate(dir, ref string) string {
74 out, err := exec.Command(toolpath.Look("git"), "-C", dir, "log", "-1", "--format=%cs", "--end-of-options", ref).Output()
75 if err != nil {
76 return ""
77 }
78 return strings.TrimSpace(string(out))
79}
80
81// HasCommit reports whether sha names a commit object present in dir.
82func HasCommit(dir, sha string) bool {
83 return exec.Command(toolpath.Look("git"), "-C", dir, "cat-file", "-e", sha+"^{commit}").Run() == nil
84}
85
86type CommitMsg struct {
87 SHA string
88 Message string
89 AuthorName string
90 AuthorEmail string
91}
92
93// RevListMessages returns sha and full message for commits reachable from
94// new but not old, newest first, capped at max. An empty or zero old (new
95// branch) lists from new alone, still capped.
96func RevListMessages(dir, old, new string, max int) ([]CommitMsg, error) {
97 args := []string{"-C", dir, "rev-list", fmt.Sprintf("-n%d", max), "--format=%an%x01%ae%x01%B%x00", "--end-of-options", new}
98 if old != "" && old != zeroSHA {
99 args = append(args, "^"+old)
100 }
101 out, err := exec.Command(toolpath.Look("git"), args...).Output()
102 if err != nil {
103 return nil, fmt.Errorf("rev-list messages: %w", err)
104 }
105 var msgs []CommitMsg
106 for _, chunk := range strings.Split(string(out), "\x00") {
107 chunk = strings.TrimLeft(chunk, "\n")
108 if chunk == "" {
109 continue
110 }
111 header, body, ok := strings.Cut(chunk, "\n")
112 if !ok || !strings.HasPrefix(header, "commit ") {
113 continue
114 }
115 name, rest, ok := strings.Cut(body, "\x01")
116 if !ok {
117 continue
118 }
119 email, message, ok := strings.Cut(rest, "\x01")
120 if !ok {
121 continue
122 }
123 msgs = append(msgs, CommitMsg{
124 SHA: strings.TrimPrefix(header, "commit "),
125 Message: strings.TrimSpace(message),
126 AuthorName: name,
127 AuthorEmail: email,
128 })
129 }
130 return msgs, nil
131}
132
133// Subjects returns the first line of each named commit's message, keyed
134// by sha. One git log for the whole set rather than one per sha: a page
135// of builds names a handful of distinct commits and a subprocess each
136// would show. --ignore-missing keeps a sha git cannot resolve from
137// failing the rest, because a build outlives the commit it ran on once a
138// branch is force-pushed; such a sha is simply absent from the map.
139func Subjects(dir string, shas []string) map[string]string {
140 if len(shas) == 0 {
141 return nil
142 }
143 args := append([]string{"-C", dir, "log", "--no-walk=unsorted", "--ignore-missing", "--format=%H%x00%s"}, shas...)
144 args = append(args, "--")
145 out, err := exec.Command(toolpath.Look("git"), args...).Output()
146 if err != nil {
147 return nil
148 }
149 subjects := map[string]string{}
150 for _, line := range strings.Split(strings.TrimSpace(string(out)), "\n") {
151 if sha, subject, ok := strings.Cut(line, "\x00"); ok {
152 subjects[sha] = subject
153 }
154 }
155 return subjects
156}