Migration This is my new public git source viewer. Source available. Please contact with concerns/bugs. Contact me

internal/gitx/git_test.go

321286b403e696f79ab1feab51660a63bb3ee72c · 7.0 KB · 201 lines raw

1 package gitx
2
3 import (
4 "context"
5 "os"
6 "os/exec"
7 "testing"
8 )
9
10 // runGit drives git directly (not through Repo.run, which doesn't exist
11 // yet at setup time) with the same kind of isolation Repo.run uses --
12 // GIT_CONFIG_NOSYSTEM and a throwaway HOME -- so the test behaves the same
13 // on a machine with commit.gpgsign=true globally as it does in CI.
14 func runGit(t *testing.T, dir string, args ...string) {
15 t.Helper()
16 cmd := exec.Command("git", append([]string{"-C", dir, "-c", "commit.gpgsign=false"}, args...)...)
17 cmd.Env = []string{
18 "PATH=" + os.Getenv("PATH"),
19 "HOME=" + dir,
20 "GIT_CONFIG_NOSYSTEM=1",
21 "GIT_AUTHOR_NAME=test", "GIT_AUTHOR_EMAIL=test@example.com",
22 "GIT_COMMITTER_NAME=test", "GIT_COMMITTER_EMAIL=test@example.com",
23 }
24 if out, err := cmd.CombinedOutput(); err != nil {
25 t.Fatalf("git %v: %v\n%s", args, err, out)
26 }
27 }
28
29 // newTestRepo builds a real repository with one empty commit, a branch
30 // "feature/x" (a branch name containing a slash), a second branch
31 // "release", and a tag "release/1.0". The last two coexist by construction
32 // deliberately: refs/heads and refs/tags are separate namespaces, so
33 // "release" (a branch) and "release/1.0" (a tag) can both exist even
34 // though a branch "release" and a branch "release/1.0" could not -- git
35 // refuses that, since a ref name is a path and "release" can't be both a
36 // file and a directory. Opened through gitx.Open, because SplitRefPath
37 // resolves against Refs(), which shells out to `git for-each-ref` -- there
38 // is no faking that, it has to be a real repo with real refs.
39 func newTestRepo(t *testing.T) *Repo {
40 t.Helper()
41 dir := t.TempDir()
42 runGit(t, dir, "init", "-q", "-b", "main")
43 runGit(t, dir, "commit", "-q", "--allow-empty", "-m", "init")
44 runGit(t, dir, "branch", "feature/x")
45 runGit(t, dir, "branch", "release")
46 runGit(t, dir, "tag", "release/1.0")
47
48 r, err := Open(dir)
49 if err != nil {
50 t.Fatalf("Open: %v", err)
51 }
52 return r
53 }
54
55 // TestSplitRefPath guards the longest-prefix-wins rule that lets ref names
56 // contain slashes. A naive split at the first "/" gets both of the two
57 // interesting cases here wrong: it would send "feature/x/src/main.go" to
58 // ref "feature" (not a ref at all) instead of branch "feature/x", and it
59 // would send "release/1.0/docs/readme.md" to branch "release" instead of
60 // the more specific tag "release/1.0" -- the real ambiguity SplitRefPath
61 // exists to resolve, since both "release" and "release/1.0" are valid refs
62 // here and only the longest match is the intended one.
63 func TestSplitRefPath(t *testing.T) {
64 r := newTestRepo(t)
65
66 cases := []struct {
67 name string
68 rest string
69 wantRef string
70 wantPath string
71 }{
72 {"single-segment branch with a path", "main/src/main.go", "main", "src/main.go"},
73 {"slashed branch name resolves in full, not just its first segment",
74 "feature/x/src/main.go", "feature/x", "src/main.go"},
75 {"slashed tag wins over a shorter branch that's also a valid prefix",
76 "release/1.0/docs/readme.md", "release/1.0", "docs/readme.md"},
77 {"shorter branch still resolves when no longer candidate matches",
78 "release/README.md", "release", "README.md"},
79 {"slashed branch with no path", "feature/x", "feature/x", ""},
80 {"empty rest falls back to HEAD", "", "main", ""},
81 {"no ref matches: whole thing is a path at HEAD",
82 "no/such/branch/file.go", "main", "no/such/branch/file.go"},
83 }
84
85 for _, tc := range cases {
86 t.Run(tc.name, func(t *testing.T) {
87 ref, path := r.SplitRefPath(tc.rest)
88 if ref != tc.wantRef || path != tc.wantPath {
89 t.Errorf("SplitRefPath(%q) = (%q, %q), want (%q, %q)",
90 tc.rest, ref, path, tc.wantRef, tc.wantPath)
91 }
92 })
93 }
94 }
95
96 // TestIsAncestor builds its own repo (rather than reusing newTestRepo,
97 // whose branches all point at the same single commit) because it needs a
98 // real linear ancestor/descendant pair plus a genuinely diverged pair:
99 // "main" advances past the point where "other" branched off, then "other"
100 // advances on its own, so main's tip and other's tip share c1 as a common
101 // ancestor but are not ancestors of each other.
102 func TestIsAncestor(t *testing.T) {
103 dir := t.TempDir()
104 runGit(t, dir, "init", "-q", "-b", "main")
105 runGit(t, dir, "commit", "-q", "--allow-empty", "-m", "c1")
106
107 r, err := Open(dir)
108 if err != nil {
109 t.Fatalf("Open: %v", err)
110 }
111 c1, err := r.Resolve("main")
112 if err != nil {
113 t.Fatalf("Resolve(main): %v", err)
114 }
115
116 runGit(t, dir, "branch", "other")
117 runGit(t, dir, "commit", "-q", "--allow-empty", "-m", "c2")
118 c2, err := r.Resolve("main")
119 if err != nil {
120 t.Fatalf("Resolve(main): %v", err)
121 }
122
123 runGit(t, dir, "checkout", "-q", "other")
124 runGit(t, dir, "commit", "-q", "--allow-empty", "-m", "c3-diverged")
125 c3, err := r.Resolve("other")
126 if err != nil {
127 t.Fatalf("Resolve(other): %v", err)
128 }
129
130 cases := []struct {
131 name string
132 a, b string
133 want bool
134 }{
135 {"strict ancestor", c1, c2, true},
136 {"reversed order is not an ancestor relationship", c2, c1, false},
137 {"a commit is its own ancestor", c1, c1, true},
138 {"diverged commits are not ancestors either direction", c2, c3, false},
139 {"diverged commits, reverse direction", c3, c2, false},
140 }
141 for _, tc := range cases {
142 t.Run(tc.name, func(t *testing.T) {
143 got, err := r.IsAncestor(tc.a, tc.b)
144 if err != nil {
145 t.Fatalf("IsAncestor(%q, %q): %v", tc.a, tc.b, err)
146 }
147 if got != tc.want {
148 t.Errorf("IsAncestor(%q, %q) = %v, want %v", tc.a, tc.b, got, tc.want)
149 }
150 })
151 }
152
153 t.Run("bogus revision is an error, not false", func(t *testing.T) {
154 if _, err := r.IsAncestor("deadbeef", c1); err == nil {
155 t.Error("IsAncestor with a bogus revision: got nil error, want non-nil")
156 }
157 })
158 }
159
160 // TestEmptyTracksFirstPush guards against latching emptiness at Open. The
161 // watcher (core/watch.go) opens a repo the moment its directory appears --
162 // gitolite and `git init --bare` both create it before anything is pushed
163 // -- and reuses that *Repo for the life of the process, so a value captured
164 // at Open kept a repo reading as empty after its first push until restart.
165 func TestEmptyTracksFirstPush(t *testing.T) {
166 bare := t.TempDir()
167 runGit(t, bare, "init", "-q", "--bare", "-b", "main")
168
169 r, err := Open(bare)
170 if err != nil {
171 t.Fatalf("Open: %v", err)
172 }
173 if !r.Empty() {
174 t.Fatal("fresh bare repo: Empty() = false, want true")
175 }
176 if log, err := r.Log(r.HeadRef(), 1, 0, false); err != nil || len(log) != 0 {
177 t.Fatalf("Log on empty repo = %v, %v; want nil, nil", log, err)
178 }
179
180 work := t.TempDir()
181 runGit(t, work, "init", "-q", "-b", "main")
182 runGit(t, work, "commit", "-q", "--allow-empty", "-m", "first")
183 runGit(t, work, "push", "-q", bare, "main")
184
185 // Same *Repo the watcher would still be holding.
186 if r.Empty() {
187 t.Fatal("after push: Empty() = true on the long-lived Repo, want false")
188 }
189 rc := r.WithContext(context.Background(), 0)
190 if rc.Empty() {
191 t.Fatal("after push: Empty() = true on a request-bound Repo, want false")
192 }
193 sha, err := rc.Resolve(rc.HeadRef())
194 if err != nil {
195 t.Fatalf("Resolve after push: %v", err)
196 }
197 log, err := rc.Log(sha, 1, 0, false)
198 if err != nil || len(log) != 1 || log[0].Subject != "first" {
199 t.Fatalf("Log after push = %v, %v; want the pushed commit", log, err)
200 }
201 }