Repository navigation
Expand file tree
/
Copy pathcommit_test.go
More file actions
343 lines (297 loc) · 11.9 KB
/
Copy pathcommit_test.go
File metadata and controls
343 lines (297 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package gitgeneric
import (
"os"
"testing"
"time"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestGetCommitHash(t *testing.T) {
directory := t.TempDir()
repository, err := git.PlainInit(directory, false)
require.NoError(t, err)
worktree, err := repository.Worktree()
require.NoError(t, err)
commit := func(content, message string) plumbing.Hash {
require.NoError(t, os.WriteFile(directory+"/file.txt", []byte(content), 0o600))
_, err = worktree.Add("file.txt")
require.NoError(t, err)
hash, commitErr := worktree.Commit(message, &git.CommitOptions{
Author: &object.Signature{
Name: "Updatecli Test",
Email: "test@updatecli.io",
When: time.Now(),
},
})
require.NoError(t, commitErr)
return hash
}
mainHash := commit("main", "main commit")
featureReference := plumbing.NewHashReference(plumbing.NewBranchReferenceName("feature"), mainHash)
require.NoError(t, repository.Storer.SetReference(featureReference))
require.NoError(t, worktree.Checkout(&git.CheckoutOptions{Branch: featureReference.Name()}))
featureHash := commit("feature", "feature commit")
remoteReference := plumbing.NewHashReference(
plumbing.NewRemoteReferenceName(DefaultRemoteReferenceName, "remote-only"),
featureHash,
)
require.NoError(t, repository.Storer.SetReference(remoteReference))
handler := GoGit{}
tests := []struct {
name string
branch string
want string
wantErr string
workingDir string
}{
{name: "current HEAD", want: featureHash.String(), workingDir: directory},
{name: "local branch", branch: "master", want: mainHash.String(), workingDir: directory},
{name: "remote branch", branch: "remote-only", want: featureHash.String(), workingDir: directory},
{name: "missing branch", branch: "missing", wantErr: `branch "missing" not found`, workingDir: directory},
{name: "invalid repository", branch: "master", wantErr: "opening", workingDir: directory + "/missing"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := handler.GetCommitHash(tt.workingDir, tt.branch)
if tt.wantErr != "" {
require.Error(t, err)
assert.Contains(t, err.Error(), tt.wantErr)
return
}
require.NoError(t, err)
assert.Equal(t, tt.want, got)
})
}
}
func TestIsCommitExist(t *testing.T) {
directory := t.TempDir()
repository, err := git.PlainInit(directory, false)
require.NoError(t, err)
worktree, err := repository.Worktree()
require.NoError(t, err)
require.NoError(t, os.WriteFile(directory+"/file.txt", []byte("content"), 0o600))
_, err = worktree.Add("file.txt")
require.NoError(t, err)
hash, err := worktree.Commit("initial commit", &git.CommitOptions{
Author: &object.Signature{
Name: "Updatecli Test",
Email: "test@updatecli.io",
When: time.Now(),
},
})
require.NoError(t, err)
handler := GoGit{}
tests := []struct {
name string
commit string
want bool
wantErr string
workingDir string
}{
{name: "full hash", commit: hash.String(), want: true, workingDir: directory},
{name: "abbreviated hash", commit: hash.String()[:7], want: true, workingDir: directory},
{name: "missing commit", commit: "0123456789012345678901234567890123456789", workingDir: directory},
{name: "unknown revision", commit: "not-a-commit", workingDir: directory},
{name: "invalid repository", commit: hash.String(), wantErr: "opening", workingDir: directory + "/missing"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := handler.IsCommitExist(tt.workingDir, tt.commit)
if tt.wantErr != "" {
require.Error(t, err)
assert.Contains(t, err.Error(), tt.wantErr)
return
}
require.NoError(t, err)
assert.Equal(t, tt.want, got)
})
}
}
// commitDaysAgo builds the committer date of a commit created n days ago.
func commitDaysAgo(n int) time.Time {
return time.Now().Add(-time.Duration(n) * 24 * time.Hour)
}
// olderThan builds a matcher accepting the commits which are at least n days old.
func olderThan(n int) func(time.Time) bool {
return func(when time.Time) bool {
return when.Before(commitDaysAgo(n))
}
}
func TestSearchCommit(t *testing.T) {
directory := t.TempDir()
repository, err := git.PlainInit(directory, false)
require.NoError(t, err)
worktree, err := repository.Worktree()
require.NoError(t, err)
commit := func(content, message string, when time.Time) plumbing.Hash {
require.NoError(t, os.WriteFile(directory+"/file.txt", []byte(content), 0o600))
_, err = worktree.Add("file.txt")
require.NoError(t, err)
hash, commitErr := worktree.Commit(message, &git.CommitOptions{
AllowEmptyCommits: true,
Author: &object.Signature{
Name: "Updatecli Test",
Email: "test@updatecli.io",
When: when,
},
})
require.NoError(t, commitErr)
return hash
}
/*
A history with a merge, so that the walk order is actually exercised:
master 30d --- 10d ------------- merge (0d)
\ /
feature `--- 5d ----'
A depth-first walk reaches the 5 days old feature commit before the 10 days old
master one, so a matcher accepting anything older than 3 days only returns the
right commit when the history is walked by committer time.
*/
oldest := commit("oldest", "oldest commit", commitDaysAgo(30))
mainline := commit("mainline", "mainline commit", commitDaysAgo(10))
featureReference := plumbing.NewHashReference(plumbing.NewBranchReferenceName("feature"), mainline)
require.NoError(t, repository.Storer.SetReference(featureReference))
require.NoError(t, worktree.Checkout(&git.CheckoutOptions{Branch: featureReference.Name()}))
feature := commit("feature", "feature commit", commitDaysAgo(5))
require.NoError(t, worktree.Checkout(&git.CheckoutOptions{Branch: plumbing.NewBranchReferenceName("master")}))
merge := commit("merge", "merge commit", commitDaysAgo(0))
mergeCommit, err := repository.CommitObject(merge)
require.NoError(t, err)
mergeCommit.ParentHashes = []plumbing.Hash{mainline, feature}
mergeObject := repository.Storer.NewEncodedObject()
require.NoError(t, mergeCommit.Encode(mergeObject))
merge, err = repository.Storer.SetEncodedObject(mergeObject)
require.NoError(t, err)
require.NoError(t, repository.Storer.SetReference(
plumbing.NewHashReference(plumbing.NewBranchReferenceName("master"), merge)))
require.NoError(t, worktree.Reset(&git.ResetOptions{Commit: merge, Mode: git.HardReset}))
remoteReference := plumbing.NewHashReference(
plumbing.NewRemoteReferenceName(DefaultRemoteReferenceName, "remote-only"),
mainline,
)
require.NoError(t, repository.Storer.SetReference(remoteReference))
handler := GoGit{}
t.Run("a matching tip stops the walk immediately", func(t *testing.T) {
visited := 0
got, err := handler.SearchCommit(directory, "", func(time.Time) bool {
visited++
return true
})
require.NoError(t, err)
assert.Equal(t, merge.String(), got.Hash)
// The laziness of the walk is the point: a matching tip must cost one commit.
assert.Equal(t, 1, visited)
})
t.Run("the newest matching commit wins across a merge", func(t *testing.T) {
got, err := handler.SearchCommit(directory, "", olderThan(3))
require.NoError(t, err)
assert.Equal(t, feature.String(), got.Hash)
})
t.Run("the history is walked by committer time, not depth first", func(t *testing.T) {
got, err := handler.SearchCommit(directory, "", olderThan(7))
require.NoError(t, err)
assert.Equal(t, mainline.String(), got.Hash)
})
t.Run("the oldest commit is reachable", func(t *testing.T) {
got, err := handler.SearchCommit(directory, "", olderThan(20))
require.NoError(t, err)
assert.Equal(t, oldest.String(), got.Hash)
// The committer date is what the matcher was given.
assert.WithinDuration(t, commitDaysAgo(30), got.When, time.Minute)
})
t.Run("an exhausted history reports no commit found", func(t *testing.T) {
_, err := handler.SearchCommit(directory, "", olderThan(365))
require.Error(t, err)
assert.ErrorIs(t, err, ErrNoCommitFound)
})
t.Run("a local branch is resolved", func(t *testing.T) {
got, err := handler.SearchCommit(directory, "feature", func(time.Time) bool { return true })
require.NoError(t, err)
assert.Equal(t, feature.String(), got.Hash)
})
t.Run("a remote branch is resolved", func(t *testing.T) {
got, err := handler.SearchCommit(directory, "remote-only", func(time.Time) bool { return true })
require.NoError(t, err)
assert.Equal(t, mainline.String(), got.Hash)
})
t.Run("a missing branch errors", func(t *testing.T) {
_, err := handler.SearchCommit(directory, "missing", func(time.Time) bool { return true })
require.Error(t, err)
assert.Contains(t, err.Error(), `branch "missing" not found`)
})
t.Run("an invalid repository errors", func(t *testing.T) {
_, err := handler.SearchCommit(directory+"/missing", "", func(time.Time) bool { return true })
require.Error(t, err)
assert.Contains(t, err.Error(), "opening")
})
}
// TestSearchCommitTruncatedHistory covers the boundary of a shallow repository, whose
// oldest commit references parents which were never fetched.
func TestSearchCommitTruncatedHistory(t *testing.T) {
directory := t.TempDir()
repository, err := git.PlainInit(directory, false)
require.NoError(t, err)
worktree, err := repository.Worktree()
require.NoError(t, err)
commit := func(content, message string, when time.Time) plumbing.Hash {
require.NoError(t, os.WriteFile(directory+"/file.txt", []byte(content), 0o600))
_, err = worktree.Add("file.txt")
require.NoError(t, err)
hash, commitErr := worktree.Commit(message, &git.CommitOptions{
Author: &object.Signature{
Name: "Updatecli Test",
Email: "test@updatecli.io",
When: when,
},
})
require.NoError(t, commitErr)
return hash
}
boundaryHash := commit("boundary", "boundary commit", commitDaysAgo(10))
tip := commit("tip", "tip commit", commitDaysAgo(1))
// Point the oldest commit at a parent absent from the object store, exactly as the
// oldest commit of a shallow clone does.
boundary, err := repository.CommitObject(boundaryHash)
require.NoError(t, err)
boundary.ParentHashes = []plumbing.Hash{plumbing.NewHash("0123456789abcdef0123456789abcdef01234567")}
encoded := repository.Storer.NewEncodedObject()
require.NoError(t, boundary.Encode(encoded))
truncatedBoundary, err := repository.Storer.SetEncodedObject(encoded)
require.NoError(t, err)
// Re-parent the tip onto the rewritten boundary commit.
tipCommit, err := repository.CommitObject(tip)
require.NoError(t, err)
tipCommit.ParentHashes = []plumbing.Hash{truncatedBoundary}
encodedTip := repository.Storer.NewEncodedObject()
require.NoError(t, tipCommit.Encode(encodedTip))
truncatedTip, err := repository.Storer.SetEncodedObject(encodedTip)
require.NoError(t, err)
require.NoError(t, repository.Storer.SetReference(
plumbing.NewHashReference(plumbing.NewBranchReferenceName("master"), truncatedTip)))
handler := GoGit{}
/*
The commit on the shallow boundary must be matched before its missing parents are
looked up. The go-git iterators load the parents of a commit before handing it
over, so they drop this commit entirely -- which is the one a `depth` limited
clone is the most likely to want.
*/
t.Run("the commit on the boundary is still matched", func(t *testing.T) {
got, err := handler.SearchCommit(directory, "", olderThan(5))
require.NoError(t, err)
assert.Equal(t, truncatedBoundary.String(), got.Hash)
})
t.Run("a newer commit is preferred over the boundary", func(t *testing.T) {
got, err := handler.SearchCommit(directory, "", func(time.Time) bool { return true })
require.NoError(t, err)
assert.Equal(t, truncatedTip.String(), got.Hash)
})
t.Run("walking past the boundary reports a truncated history rather than failing", func(t *testing.T) {
_, err := handler.SearchCommit(directory, "", olderThan(365))
require.Error(t, err)
assert.ErrorIs(t, err, ErrNoCommitFound)
assert.ErrorIs(t, err, ErrTruncatedHistory)
})
}