v0.21.0 ef38ad0

pull.go RawHistoryFolder

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).
21//
22//hexcheck:ignore suspicious-business-logic-in-adapter maps git exit codes to the clean/conflict answer of the port
23func (g *Git) TestMerge(ctx context.Context, repo domain.RepoName, base, head domain.ObjectID) (domain.ObjectID, bool, error) {
24	dir, err := g.repoDir(repo)
25	if err != nil {
26		return domain.ObjectID{}, false, err
27	}
28	if _, err := g.MergeBase(ctx, repo, base, head); errors.Is(err, domain.ErrNotFound) {
29		return domain.ObjectID{}, false, nil
30	} else if err != nil {
31		return domain.ObjectID{}, false, err
32	}
33	out, err := g.run(ctx, readArgs(dir, "merge-tree", "--write-tree", "--no-messages", "--end-of-options", base.String(), head.String())...)
34	switch {
35	case exitCode(err) == 1:
36		return domain.ObjectID{}, false, nil
37	case err != nil:
38		return domain.ObjectID{}, false, fmt.Errorf("test merge %s into %s in %q: %w", head, base, repo, err)
39	}
40	line, _, _ := strings.Cut(out, "\n")
41	tree, err := parseID(line)
42	if err != nil {
43		return domain.ObjectID{}, false, err
44	}
45	return tree, true, nil
46}
47
48// SetPullRef runs `git update-ref` on a ref that domain.IsPullRef accepts, or
49// `update-ref -d` for a zero id (deleting a ref that is not there is fine).
50//
51//hexcheck:ignore suspicious-business-logic-in-adapter checks the ref name with the domain rule before handing it to git
52func (g *Git) SetPullRef(ctx context.Context, repo domain.RepoName, ref string, id domain.ObjectID) error {
53	if !domain.IsPullRef(ref) {
54		return fmt.Errorf("pull ref %q of %q: %w", ref, repo, domain.ErrInvalid)
55	}
56	dir, err := g.repoDir(repo)
57	if err != nil {
58		return err
59	}
60	args := []string{"--git-dir=" + dir, "update-ref"}
61	if id.IsZero() {
62		args = append(args, "-d", ref)
63	} else {
64		args = append(args, ref, id.String())
65	}
66	if _, err := g.run(ctx, args...); err != nil {
67		return fmt.Errorf("update pull ref %q of %q: %w", ref, repo, err)
68	}
69	return nil
70}
71
72// PullRef reads a ref that domain.IsPullRef accepts with `git rev-parse`.
73//
74//hexcheck:ignore suspicious-business-logic-in-adapter checks the ref name with the domain rule before handing it to git
75func (g *Git) PullRef(ctx context.Context, repo domain.RepoName, ref string) (domain.ObjectID, error) {
76	if !domain.IsPullRef(ref) {
77		return domain.ObjectID{}, fmt.Errorf("pull ref %q of %q: %w", ref, repo, domain.ErrNotFound)
78	}
79	dir, err := g.repoDir(repo)
80	if err != nil {
81		return domain.ObjectID{}, err
82	}
83	out, err := g.run(ctx, readArgs(dir, "rev-parse", "--verify", "--quiet", ref+"^{commit}")...)
84	if exitCode(err) == 1 {
85		return domain.ObjectID{}, fmt.Errorf("pull ref %q of %q: %w", ref, repo, domain.ErrNotFound)
86	}
87	if err != nil {
88		return domain.ObjectID{}, fmt.Errorf("read pull ref %q of %q: %w", ref, repo, err)
89	}
90	return parseID(out)
91}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 2

Tags 45

↑↓ moveenter openesc close