v0.28.1 cc7ba64

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).
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}

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