1// SPDX-License-Identifier: AGPL-3.0-or-later
2
3package gitcli
4
5import (
6 "bufio"
7 "context"
8 "errors"
9 "fmt"
10 "io"
11 "io/fs"
12 "os"
13 "path/filepath"
14 "strconv"
15 "strings"
16 "sync"
17 "time"
18
19 "bnema.dev/ermite/internal/domain"
20 "bnema.dev/ermite/internal/ports"
21)
22
23var _ ports.RepoReader = (*Git)(nil)
24
25const (
26 headsRefPrefix = "refs/heads/"
27 tagsRefPrefix = "refs/tags/"
28 // maxRefLine bounds a line of for-each-ref.
29 maxRefLine = 16 * 1024
30)
31
32// notFound reports whether err means a missing object.
33func notFound(err error) bool { return errors.Is(err, errObjectMissing) }
34
35// ListRefs lists HEAD, the branches and the tags with `git for-each-ref`.
36// An annotated tag is followed to its commit; a tag of a tag is followed with
37// one `cat-file` request each.
38//
39//hexcheck:ignore suspicious-business-logic-in-adapter parses git output into domain refs
40func (g *Git) ListRefs(ctx context.Context, repo domain.RepoName) ([]domain.Ref, error) {
41 dir, err := g.repoDir(repo)
42 if err != nil {
43 return nil, err
44 }
45 type raw struct {
46 name, id, typ, derefID, derefType string
47 }
48 var raws []raw
49 args := readArgs(dir, "for-each-ref",
50 "--format=%(refname)%00%(objectname)%00%(objecttype)%00%(*objectname)%00%(*objecttype)",
51 headsRefPrefix, tagsRefPrefix)
52 err = g.pipe(ctx, args, func(r *bufio.Reader) error {
53 lr := newLineReader(r, maxRefLine)
54 for {
55 line, _, err := lr.next()
56 if errors.Is(err, io.EOF) {
57 return nil
58 }
59 if err != nil {
60 return err
61 }
62 f := strings.Split(string(line), "\x00")
63 if len(f) != 5 {
64 return fmt.Errorf("git for-each-ref: unexpected line %q", truncateText(string(line)))
65 }
66 raws = append(raws, raw{f[0], f[1], f[2], f[3], f[4]})
67 }
68 })
69 if err != nil {
70 return nil, fmt.Errorf("list refs of %q: %w", repo, err)
71 }
72
73 refs := make([]domain.Ref, 0, len(raws)+1)
74 byName := make(map[string]domain.Ref, len(raws))
75 var nested []int // indices in refs of tags that point to a tag
76 for _, rw := range raws {
77 ref := domain.Ref{Name: rw.name}
78 switch {
79 case strings.HasPrefix(rw.name, headsRefPrefix):
80 ref.Kind, ref.ShortName = domain.RefKindBranch, strings.TrimPrefix(rw.name, headsRefPrefix)
81 case strings.HasPrefix(rw.name, tagsRefPrefix):
82 ref.Kind, ref.ShortName = domain.RefKindTag, strings.TrimPrefix(rw.name, tagsRefPrefix)
83 default:
84 continue
85 }
86 if ref.ID, err = domain.ParseObjectID(rw.id); err != nil {
87 return nil, fmt.Errorf("list refs of %q: %w", repo, err)
88 }
89 switch {
90 case rw.typ == "commit":
91 ref.CommitID = ref.ID
92 case rw.typ == "tag" && rw.derefType == "commit":
93 if ref.CommitID, err = domain.ParseObjectID(rw.derefID); err != nil {
94 return nil, fmt.Errorf("list refs of %q: %w", repo, err)
95 }
96 case rw.typ == "tag" && rw.derefType == "tag":
97 nested = append(nested, len(refs))
98 }
99 refs = append(refs, ref)
100 byName[ref.Name] = ref
101 }
102 if len(nested) > 0 {
103 err := g.cat.do(ctx, dir, func(c *catConn) error {
104 for _, i := range nested {
105 h, err := c.info(refs[i].Name + "^{commit}")
106 if notFound(err) {
107 continue
108 }
109 if err != nil {
110 return err
111 }
112 if refs[i].CommitID, err = domain.ParseObjectID(h.ID); err != nil {
113 return err
114 }
115 }
116 return nil
117 })
118 if err != nil {
119 return nil, fmt.Errorf("follow tags of %q: %w", repo, err)
120 }
121 for _, i := range nested {
122 byName[refs[i].Name] = refs[i]
123 }
124 }
125
126 head := domain.Ref{Name: "HEAD", Kind: domain.RefKindHead}
127 target, err := readHead(dir)
128 if err != nil {
129 return nil, fmt.Errorf("read HEAD of %q: %w", repo, err)
130 }
131 if b, ok := byName[target]; ok && target != "" {
132 head.ShortName, head.ID, head.CommitID = b.ShortName, b.ID, b.CommitID
133 } else if short, ok := strings.CutPrefix(target, headsRefPrefix); ok {
134 head.ShortName = short // unborn branch
135 }
136 return append([]domain.Ref{head}, refs...), nil
137}
138
139// readHead returns the ref HEAD of the bare repository in dir points to, or
140// "" if HEAD is detached. It reads the HEAD file (the repositories use the
141// files ref backend), which is what `git symbolic-ref HEAD` would answer
142// without starting a process. A missing file is domain.ErrNotFound.
143func readHead(dir string) (string, error) {
144 raw, err := os.ReadFile(filepath.Join(dir, "HEAD"))
145 if err != nil {
146 if errors.Is(err, fs.ErrNotExist) {
147 return "", domain.ErrNotFound
148 }
149 return "", fmt.Errorf("read HEAD: %w", err)
150 }
151 ref, ok := strings.CutPrefix(strings.TrimSpace(string(raw)), "ref: ")
152 if !ok {
153 return "", nil // a commit id: detached
154 }
155 return ref, nil
156}
157
158// revCandidate is one way to read a revision as a commit.
159type revCandidate struct {
160 spec string // object expression for cat-file
161 ref string // full ref name it stands for, "" for ids and HEAD
162}
163
164// candidates lists, in order, how a revision is tried. A full object id is an
165// object id first: a branch or a tag that happens to be named like a commit
166// must not redirect a link to that commit; refs are tried only when no such
167// object exists. Otherwise it is the branch, then the tag, then (for "HEAD",
168// full ref names and abbreviations) as written. Other forms Git understands,
169// such as "main~2" or "FETCH_HEAD", are not revisions of ermite.
170func candidates(rev domain.Revision) []revCandidate {
171 s := rev.String()
172 switch {
173 case rev.IsHead() || s == "HEAD":
174 return []revCandidate{{spec: "HEAD^{commit}"}}
175 }
176 refs := []revCandidate{
177 {spec: headsRefPrefix + s + "^{commit}", ref: headsRefPrefix + s},
178 {spec: tagsRefPrefix + s + "^{commit}", ref: tagsRefPrefix + s},
179 }
180 switch {
181 case rev.IsObjectID():
182 return append([]revCandidate{{spec: s + "^{commit}"}}, refs...)
183 case strings.HasPrefix(s, "refs/"):
184 return append(refs, revCandidate{spec: s + "^{commit}", ref: s})
185 case rev.IsAbbrevID():
186 return append(refs, revCandidate{spec: s + "^{commit}"})
187 }
188 return refs
189}
190
191// resolve resolves rev on an open cat-file connection.
192//
193//hexcheck:ignore suspicious-business-logic-in-adapter maps a revision to git object names, a git detail of the reader
194func resolve(c *catConn, rev domain.Revision) (domain.ResolvedRevision, error) {
195 for _, cand := range candidates(rev) {
196 h, err := c.info(cand.spec)
197 if notFound(err) {
198 continue
199 }
200 if err != nil {
201 return domain.ResolvedRevision{}, err
202 }
203 if h.Type != "commit" {
204 continue
205 }
206 id, err := domain.ParseObjectID(h.ID)
207 if err != nil {
208 return domain.ResolvedRevision{}, c.fail(err)
209 }
210 return domain.ResolvedRevision{CommitID: id, Ref: cand.ref}, nil
211 }
212 return domain.ResolvedRevision{}, fmt.Errorf("revision %q: %w", rev, domain.ErrNotFound)
213}
214
215// ResolveRevision resolves a revision with `cat-file`.
216//
217//hexcheck:ignore suspicious-business-logic-in-adapter parses git output into domain values; limits and rules come from the port
218func (g *Git) ResolveRevision(ctx context.Context, repo domain.RepoName, rev domain.Revision) (domain.ResolvedRevision, error) {
219 dir, err := g.repoDir(repo)
220 if err != nil {
221 return domain.ResolvedRevision{}, err
222 }
223 var res domain.ResolvedRevision
224 err = g.cat.do(ctx, dir, func(c *catConn) (err error) {
225 res, err = resolve(c, rev)
226 return err
227 })
228 if err != nil {
229 return domain.ResolvedRevision{}, fmt.Errorf("resolve %q in %q: %w", rev, repo, err)
230 }
231 return res, nil
232}
233
234// ResolveRefPath tries the leading segments of path as a revision (branch,
235// tag, full ref name, HEAD; a single segment may also be a commit id), longest
236// first, with up to one `cat-file` request per candidate on the repository's
237// process. At most domain.MaxRefPathTries segments are tried.
238//
239//hexcheck:ignore suspicious-business-logic-in-adapter the split is domain.SplitRefPath; this only runs it on a cat-file connection
240func (g *Git) ResolveRefPath(ctx context.Context, repo domain.RepoName, path string) (domain.RefPath, error) {
241 dir, err := g.repoDir(repo)
242 if err != nil {
243 return domain.RefPath{}, err
244 }
245 var out domain.RefPath
246 err = g.cat.do(ctx, dir, func(c *catConn) (err error) {
247 out, err = domain.SplitRefPath(path, func(rev domain.Revision) (domain.ResolvedRevision, error) {
248 return resolve(c, rev)
249 })
250 return err
251 })
252 if err != nil {
253 return domain.RefPath{}, fmt.Errorf("resolve path %q in %q: %w", path, repo, err)
254 }
255 return out, nil
256}
257
258// object is what lookup finds at a path of a revision.
259type object struct {
260 commit domain.ObjectID
261 info catInfo
262}
263
264// lookup resolves rev and reads the header of the object at path, in one
265// request series on the repository's process. A missing path is
266// domain.ErrNotFound.
267func (g *Git) lookup(ctx context.Context, dir string, rev domain.Revision, path domain.TreePath) (object, error) {
268 var obj object
269 err := g.cat.do(ctx, dir, func(c *catConn) error {
270 res, err := resolve(c, rev)
271 if err != nil {
272 return err
273 }
274 spec := res.CommitID.String() + "^{tree}"
275 if !path.IsRoot() {
276 spec = res.CommitID.String() + ":" + path.String()
277 }
278 h, err := c.info(spec)
279 if notFound(err) {
280 return fmt.Errorf("path %q: %w", path, domain.ErrNotFound)
281 }
282 if err != nil {
283 return err
284 }
285 obj = object{commit: res.CommitID, info: h}
286 return nil
287 })
288 return obj, err
289}
290
291// GetTree lists a directory with `git ls-tree -z -l`.
292//
293//hexcheck:ignore suspicious-business-logic-in-adapter parses git output into domain values; limits and rules come from the port
294func (g *Git) GetTree(ctx context.Context, repo domain.RepoName, rev domain.Revision, path domain.TreePath, includeLast bool) (domain.Tree, error) {
295 dir, err := g.repoDir(repo)
296 if err != nil {
297 return domain.Tree{}, err
298 }
299 obj, err := g.lookup(ctx, dir, rev, path)
300 if err != nil {
301 return domain.Tree{}, fmt.Errorf("tree %q of %q: %w", path, repo, err)
302 }
303 if obj.info.Type != "tree" {
304 return domain.Tree{}, fmt.Errorf("tree %q of %q: %w", path, repo, domain.ErrNotDirectory)
305 }
306 treeID, err := domain.ParseObjectID(obj.info.ID)
307 if err != nil {
308 return domain.Tree{}, err
309 }
310 tree := domain.Tree{CommitID: obj.commit, ID: treeID}
311
312 err = g.pipe(ctx, readArgs(dir, "ls-tree", "-z", "-l", treeID.String()), func(r *bufio.Reader) error {
313 for {
314 tok, err := readToken(r)
315 if errors.Is(err, io.EOF) {
316 return nil
317 }
318 if err != nil {
319 return err
320 }
321 e, err := parseTreeEntry(tok, path)
322 if err != nil {
323 return err
324 }
325 tree.Entries = append(tree.Entries, e)
326 }
327 })
328 if err != nil {
329 return domain.Tree{}, fmt.Errorf("list tree %q of %q: %w", path, repo, err)
330 }
331 domain.SortTreeEntries(tree.Entries)
332 tree.BoundTree()
333 if includeLast {
334 if err := g.fillLastCommits(ctx, dir, &tree); err != nil {
335 return domain.Tree{}, fmt.Errorf("last commits of %q in %q: %w", path, repo, err)
336 }
337 }
338 return tree, nil
339}
340
341// parseTreeEntry parses "<mode> <type> <id> <size>\t<name>" of ls-tree -l.
342//
343//hexcheck:ignore suspicious-business-logic-in-adapter parses git output into domain values; limits and rules come from the port
344func parseTreeEntry(rec string, parent domain.TreePath) (domain.TreeEntry, error) {
345 meta, name, ok := strings.Cut(rec, "\t")
346 f := strings.Fields(meta)
347 if !ok || len(f) != 4 || name == "" {
348 return domain.TreeEntry{}, fmt.Errorf("git ls-tree: unexpected entry %q", truncateText(rec))
349 }
350 mode, err := strconv.ParseUint(f[0], 8, 32)
351 if err != nil {
352 return domain.TreeEntry{}, fmt.Errorf("git ls-tree: mode of %q: %w", truncateText(rec), err)
353 }
354 id, err := domain.ParseObjectID(f[2])
355 if err != nil {
356 return domain.TreeEntry{}, fmt.Errorf("git ls-tree: %w", err)
357 }
358 var size int64
359 if f[3] != "-" {
360 if size, err = strconv.ParseInt(f[3], 10, 64); err != nil {
361 return domain.TreeEntry{}, fmt.Errorf("git ls-tree: size of %q: %w", truncateText(rec), err)
362 }
363 }
364 kind := domain.EntryKindOf(uint32(mode))
365 if kind == "" {
366 return domain.TreeEntry{}, fmt.Errorf("git ls-tree: unknown mode %s of %q", f[0], name)
367 }
368 return domain.TreeEntry{
369 Name: name, Path: parent.Join(name), Kind: kind, Mode: uint32(mode), ID: id, Size: size,
370 }, nil
371}
372
373// fillLastCommits sets the last commit of the first MaxLastCommitEntries
374// entries with `git log -1 <commit> -- <path>`, run by a small pool of
375// workers: min(4, max(1, MaxExec/2)) at most. Git answers each from the
376// commit-graph and its changed-path filters when they exist. Every command
377// takes a slot of the exec semaphore on its own (the workers hold none), so a
378// large directory cannot use more than its share of the processes the server
379// may run. Each worker writes its own entry, so the result does not depend on
380// the scheduling; the first error stops the others and is returned. Entries
381// beyond the bound are left without one and the tree says so.
382//
383//hexcheck:ignore suspicious-business-logic-in-adapter fills the tree with git output; the bound comes from the port
384func (g *Git) fillLastCommits(ctx context.Context, dir string, tree *domain.Tree) error {
385 n := len(tree.Entries)
386 if n > domain.MaxLastCommitEntries {
387 n = domain.MaxLastCommitEntries
388 tree.LastCommitsOmitted = true
389 }
390 workers := min(4, max(1, cap(g.exec)/2), n)
391 if workers == 0 {
392 return nil
393 }
394 ctx, cancel := context.WithCancel(ctx)
395 defer cancel()
396 var (
397 wg sync.WaitGroup
398 mu sync.Mutex
399 firstErr error
400 )
401 next := make(chan int)
402 for range workers {
403 wg.Go(func() {
404 for i := range next {
405 c, err := g.lastCommit(ctx, dir, tree.CommitID, tree.Entries[i].Path)
406 if err != nil {
407 mu.Lock()
408 if firstErr == nil {
409 firstErr = err
410 cancel()
411 }
412 mu.Unlock()
413 continue
414 }
415 tree.Entries[i].LastCommit = &c
416 }
417 })
418 }
419feed:
420 for i := range n {
421 select {
422 case next <- i:
423 case <-ctx.Done():
424 break feed
425 }
426 }
427 close(next)
428 wg.Wait()
429 if firstErr != nil {
430 return firstErr
431 }
432 return ctx.Err()
433}
434
435// lastCommit returns the newest commit reachable from head that changed path;
436// an empty path means head itself.
437//
438//hexcheck:ignore suspicious-business-logic-in-adapter parses git output into domain values; limits and rules come from the port
439func (g *Git) lastCommit(ctx context.Context, dir string, head domain.ObjectID, path string) (domain.Commit, error) {
440 var c domain.Commit
441 found := false
442 args := readArgs(dir, "log", "-z", "-n", "1", "--format="+commitFormat, "--end-of-options", head.String())
443 if path != "" {
444 args = append(args, "--", path)
445 }
446 err := g.pipe(ctx, args, func(r *bufio.Reader) (err error) {
447 c, err = readCommit(r)
448 if errors.Is(err, io.EOF) {
449 return nil
450 }
451 found = err == nil
452 return err
453 })
454 if err != nil {
455 return domain.Commit{}, err
456 }
457 if !found {
458 return domain.Commit{}, fmt.Errorf("no commit touches %q: %w", path, domain.ErrNotFound)
459 }
460 return c, nil
461}
462
463// commitFormat is the `git log` format of a commit: NUL separated fields,
464// the message last. With -z the commits are NUL terminated, so a commit is
465// nine tokens.
466const commitFormat = "%H%x00%P%x00%an%x00%ae%x00%at%x00%cn%x00%ce%x00%ct%x00%B"
467
468// readCommit reads one commit in commitFormat. It returns io.EOF at the end
469// of the output.
470//
471//hexcheck:ignore suspicious-business-logic-in-adapter parses git output into domain values; limits and rules come from the port
472func readCommit(r *bufio.Reader) (domain.Commit, error) {
473 var f [9]string
474 for i := range f {
475 tok, err := readToken(r)
476 if err != nil {
477 if i > 0 && errors.Is(err, io.EOF) {
478 err = io.ErrUnexpectedEOF
479 }
480 return domain.Commit{}, err
481 }
482 f[i] = tok
483 }
484 id, err := domain.ParseObjectID(f[0])
485 if err != nil {
486 return domain.Commit{}, fmt.Errorf("git log: %w", err)
487 }
488 c := domain.Commit{ID: id}
489 for p := range strings.FieldsSeq(f[1]) {
490 pid, err := domain.ParseObjectID(p)
491 if err != nil {
492 return domain.Commit{}, fmt.Errorf("git log: parent of %s: %w", id, err)
493 }
494 c.Parents = append(c.Parents, pid)
495 }
496 if c.Author, err = signature(f[2], f[3], f[4]); err != nil {
497 return domain.Commit{}, fmt.Errorf("git log: author of %s: %w", id, err)
498 }
499 if c.Committer, err = signature(f[5], f[6], f[7]); err != nil {
500 return domain.Commit{}, fmt.Errorf("git log: committer of %s: %w", id, err)
501 }
502 c.Message, c.Summary = domain.SplitCommitMessage(f[8])
503 return c, nil
504}
505
506func signature(name, email, unix string) (domain.Signature, error) {
507 sec, err := strconv.ParseInt(unix, 10, 64)
508 if err != nil {
509 return domain.Signature{}, err
510 }
511 return domain.Signature{
512 Name: domain.ValidGitText(name),
513 Email: domain.ValidGitText(email),
514 Time: time.Unix(sec, 0).UTC(),
515 }, nil
516}
517
518// ListCommits pages through `git log`, which stops after Skip+Limit+1 commits.
519//
520//hexcheck:ignore suspicious-business-logic-in-adapter parses git output into domain values; limits and rules come from the port
521func (g *Git) ListCommits(ctx context.Context, repo domain.RepoName, q ports.CommitQuery) (ports.CommitPage, error) {
522 if err := q.Validate(); err != nil {
523 return ports.CommitPage{}, err
524 }
525 dir, err := g.repoDir(repo)
526 if err != nil {
527 return ports.CommitPage{}, err
528 }
529 var head domain.ResolvedRevision
530 err = g.cat.do(ctx, dir, func(c *catConn) (err error) {
531 head, err = resolve(c, q.Revision)
532 return err
533 })
534 if err != nil {
535 return ports.CommitPage{}, fmt.Errorf("history of %q: %w", repo, err)
536 }
537 page := ports.CommitPage{Head: head.CommitID}
538 args := readArgs(dir, "log", "-z", "--skip="+strconv.Itoa(q.Skip), "-n", strconv.Itoa(q.Limit+1),
539 "--format="+commitFormat, "--end-of-options", head.CommitID.String())
540 if !q.Path.IsRoot() {
541 args = append(args, "--", q.Path.String())
542 }
543 err = g.pipe(ctx, args, func(r *bufio.Reader) error {
544 for {
545 c, err := readCommit(r)
546 if errors.Is(err, io.EOF) {
547 return nil
548 }
549 if err != nil {
550 return err
551 }
552 if len(page.Commits) == q.Limit {
553 page.HasMore = true
554 return errStop
555 }
556 page.Commits = append(page.Commits, c)
557 }
558 })
559 if err != nil {
560 return ports.CommitPage{}, fmt.Errorf("history of %q: %w", repo, err)
561 }
562 return page, nil
563}