1// SPDX-License-Identifier: AGPL-3.0-or-later
2
3package gitcli
4
5import (
6 "context"
7 "errors"
8 "fmt"
9 "strings"
10
11 "bnema.dev/ermite/internal/domain"
12 "bnema.dev/ermite/internal/ports"
13)
14
15var _ ports.PullGit = (*Git)(nil)
16
17// TestMerge runs `git merge-tree --write-tree`, which merges in the object
18// database and touches no ref or index: it prints the tree of the result and
19// exits 0, or exits 1 when the merge conflicts. Commits with no common history
20// do not merge (GitHub makes no test merge for them either).
21func (g *Git) TestMerge(ctx context.Context, repo domain.RepoName, base, head domain.ObjectID) (domain.ObjectID, bool, error) {
22 dir, err := g.repoDir(repo)
23 if err != nil {
24 return domain.ObjectID{}, false, err
25 }
26 if _, err := g.MergeBase(ctx, repo, base, head); errors.Is(err, domain.ErrNotFound) {
27 return domain.ObjectID{}, false, nil
28 } else if err != nil {
29 return domain.ObjectID{}, false, err
30 }
31 out, err := g.run(ctx, readArgs(dir, "merge-tree", "--write-tree", "--no-messages", "--end-of-options", base.String(), head.String())...)
32 switch {
33 case exitCode(err) == 1:
34 return domain.ObjectID{}, false, nil
35 case err != nil:
36 return domain.ObjectID{}, false, fmt.Errorf("test merge %s into %s in %q: %w", head, base, repo, err)
37 }
38 line, _, _ := strings.Cut(out, "\n")
39 tree, err := parseID(line)
40 if err != nil {
41 return domain.ObjectID{}, false, err
42 }
43 return tree, true, nil
44}
45
46// SetPullRef runs `git update-ref` on a ref that domain.IsPullRef accepts, or
47// `update-ref -d` for a zero id (deleting a ref that is not there is fine).
48//
49//hexcheck:ignore suspicious-business-logic-in-adapter checks the ref name with the domain rule before handing it to git
50func (g *Git) SetPullRef(ctx context.Context, repo domain.RepoName, ref string, id domain.ObjectID) error {
51 if !domain.IsPullRef(ref) {
52 return fmt.Errorf("pull ref %q of %q: %w", ref, repo, domain.ErrInvalid)
53 }
54 dir, err := g.repoDir(repo)
55 if err != nil {
56 return err
57 }
58 args := []string{"--git-dir=" + dir, "update-ref"}
59 if id.IsZero() {
60 args = append(args, "-d", ref)
61 } else {
62 args = append(args, ref, id.String())
63 }
64 if _, err := g.run(ctx, args...); err != nil {
65 return fmt.Errorf("update pull ref %q of %q: %w", ref, repo, err)
66 }
67 return nil
68}
69
70// PullRef reads a ref that domain.IsPullRef accepts with `git rev-parse`.
71//
72//hexcheck:ignore suspicious-business-logic-in-adapter checks the ref name with the domain rule before handing it to git
73func (g *Git) PullRef(ctx context.Context, repo domain.RepoName, ref string) (domain.ObjectID, error) {
74 if !domain.IsPullRef(ref) {
75 return domain.ObjectID{}, fmt.Errorf("pull ref %q of %q: %w", ref, repo, domain.ErrNotFound)
76 }
77 dir, err := g.repoDir(repo)
78 if err != nil {
79 return domain.ObjectID{}, err
80 }
81 out, err := g.run(ctx, readArgs(dir, "rev-parse", "--verify", "--quiet", ref+"^{commit}")...)
82 if exitCode(err) == 1 {
83 return domain.ObjectID{}, fmt.Errorf("pull ref %q of %q: %w", ref, repo, domain.ErrNotFound)
84 }
85 if err != nil {
86 return domain.ObjectID{}, fmt.Errorf("read pull ref %q of %q: %w", ref, repo, err)
87 }
88 return parseID(out)
89}