Repository navigation
Expand file tree
/
Copy pathworktree_test.adm
More file actions
428 lines (414 loc) · 18.2 KB
/
Copy pathworktree_test.adm
File metadata and controls
428 lines (414 loc) · 18.2 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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
// The working tree: status, add, rm, mv, commit and line endings. With
// ADM_TEST_GIT_KEEP set, each repository keeps `.git/adm-status` (our
// porcelain) to compare with `git status --porcelain` by hand.
partial module adm.dev.git {
use (
std.net.ssh
std.os
std.testing
)
internal def newWorktree(name string) !Repository {
let dir = try scratch(name)
let repo = try init(dir, InitOptions{initialBranch: "main"})
repo.config.override("user.name", "Ana Lima")
repo.config.override("user.email", "ana@example.com")
repo.config.override("core.excludesfile", "")
repo.config.override("core.attributesfile", "")
return repo
}
internal def writeFile(repo Repository, path string, text string) !none {
let full = os.join(repo.workDir, path)
try os.mkdir(os.dir(full), true)
try os.writeFile(full, text.bytes())
return none
}
// The porcelain status, also left in the repository for comparison.
internal def statusText(repo Repository, options StatusOptions = StatusOptions{}) !string {
let text = porcelain(try repo.status(options))
try os.writeFile(os.join(repo.gitDir, "adm-status"), text.bytes())
return text
}
check "Working tree" {
// Tags are made lightweight or annotated, signed by hand or from the
// configuration (`tag.gpgsign`, `commit.gpgsign` with an SSH key file),
// verified through the configured allowed signers, and deleted.
@test()
def MakesAndSignsTags() !none {
let repo = try newWorktree("tags")
try writeFile(repo, "a.txt", "one\n")
try repo.add(".")
let first = try repo.commit("First")
let light = try repo.createTag("v1")
fail "lightweight" unless light.name == "refs/tags/v1" && !(light.id is none) && (try repo.resolve("v1")).equals(first)
let twice = false
repo.createTag("v1") onerror (err error) {
twice = true
recover Reference{name: ""}
}
fail "existing tag taken" unless twice
let noted = try repo.createTag("v2", "HEAD", "Second release\n\nNotes.\n")
let tagId = noted.id
fail "annotated id" when tagId is none
let peeled = noted.peeled
fail "annotated peeled" unless !(peeled is none) && peeled.equals(first)
let t = try repo.tag(tagId)
let tagger = t.tagger
fail "tagger" unless !(tagger is none) && tagger.name == "Ana Lima" && t.message == "Second release\n\nNotes.\n" && t.name == "v2"
fail "bad name taken" unless repo.createTag("bad name") is error
let forced = try repo.createTag("v2", "HEAD", force = true)
fail "force" unless forced.peeled is none
// Signed by hand, checked with an allowed-signers verifier.
let key = try ssh.generateKey()
let allowed = try parseAllowedSigners("ana@example.com " + key.publicKey().str())
let signed = try repo.createTag("v3", "HEAD", "Signed", signer = new SSHSigner(key))
let verdict = try repo.verifyTag("v3", new SSHVerifier(allowed))
fail "signature: {verdict.problem}" unless verdict.valid
let _ = signed
// From the configuration: an SSH key file, tag.gpgsign and
// commit.gpgsign, and the verifier from the allowed signers file.
let keyFile = os.join(repo.workDir, "..", "signing_key")
try os.writeFile(keyFile, (try key.encode()).bytes(), 0o600)
let signersFile = os.join(repo.workDir, "..", "allowed_signers")
try os.writeFile(signersFile, ("ana@example.com " + key.publicKey().str() + "\n").bytes())
repo.config.override("gpg.format", "ssh")
repo.config.override("user.signingkey", keyFile)
repo.config.override("gpg.ssh.allowedsignersfile", signersFile)
repo.config.override("tag.gpgsign", "true")
repo.config.override("commit.gpgsign", "true")
fail "no signer from the configuration" when (try repo.signer()) is none
let verifier = try repo.verifier()
fail "no verifier from the configuration" when verifier is none
return none when verifier is none
let auto = try repo.createTag("v4", "HEAD", "Auto signed")
let _ = auto
fail "tag not signed from the configuration" unless (try repo.verifyTag("v4", verifier)).valid
try writeFile(repo, "a.txt", "two\n")
try repo.add(".")
let second = try repo.commit("Second")
fail "commit not signed from the configuration" unless (try repo.verifyCommit(second.str(), verifier)).valid
try repo.deleteTag("v4")
fail "tag not deleted" unless repo.resolve("v4") is error
repo.close()
return none
}
// `checkIgnore` names the pattern that decides a path as git's
// check-ignore -v does: the file and line, a negation, an ignored
// folder's rule for a path under it, info/exclude and the excludes file.
@test()
def ReportsTheDecidingIgnorePattern() !none {
let repo = try newWorktree("check-ignore")
let excludes = os.join(repo.workDir, "..", "global-ignore")
try os.writeFile(excludes, "*.log\n".bytes())
repo.config.override("core.excludesfile", excludes)
try writeFile(repo, ".gitignore", "# build output\n/out/\n*.tmp\n!keep.tmp\n")
try writeFile(repo, "src/.gitignore", "gen/\n")
try os.mkdir(os.join(repo.gitDir, "info"), true)
try os.writeFile(repo.excludePath(), "secret.txt\n".bytes())
fail "excludes file" unless repo.excludesFile() == excludes
let top = repo.checkIgnore("note.tmp")
fail "top: {top}" unless !(top is none) && top.file == os.join(repo.workDir, ".gitignore") && top.line == 3 && top.pattern == "*.tmp" && !top.negate
let kept = repo.checkIgnore("keep.tmp")
fail "negated" unless !(kept is none) && kept.line == 4 && kept.pattern == "!keep.tmp" && kept.negate
let under = repo.checkIgnore("out/a/b.c")
fail "folder rule: {under}" unless !(under is none) && under.line == 2 && under.pattern == "/out/" && !under.negate
let nested = repo.checkIgnore("src/gen/x.adm")
fail "nested: {nested}" unless !(nested is none) && nested.file == os.join(repo.workDir, "src/.gitignore") && nested.line == 1 && nested.pattern == "gen/"
let secret = repo.checkIgnore("secret.txt")
fail "info/exclude" unless !(secret is none) && secret.file == repo.excludePath() && secret.line == 1
let log = repo.checkIgnore("run.log")
fail "excludes" unless !(log is none) && log.file == excludes && log.pattern == "*.log"
fail "plain file matched" unless repo.checkIgnore("main.adm") is none
let status = try repo.status()
let _ = status
repo.close()
return none
}
@test()
def reportsChanges() !none {
let repo = try newWorktree("status")
try writeFile(repo, "a.txt", "one\n")
try writeFile(repo, "b.txt", "two\n")
try writeFile(repo, "dir/c.txt", "three\n")
try repo.add(".")
fail "staged: {try statusText(repo)}" unless (try statusText(repo)) == "A a.txt\nA b.txt\nA dir/c.txt\n"
try repo.commit("First")
fail "clean: {try statusText(repo)}" unless (try statusText(repo)) == ""
try writeFile(repo, "a.txt", "one changed\n")
try os.remove(os.join(repo.workDir, "b.txt"))
try writeFile(repo, "d.txt", "new\n")
try writeFile(repo, "fresh/e.txt", "e\n")
let text = try statusText(repo)
fail "status: {text}" unless text == " M a.txt\n D b.txt\n?? d.txt\n?? fresh/\n"
let all = try statusText(repo, StatusOptions{untracked: UntrackedMode.All})
fail "-uall: {all}" unless all == " M a.txt\n D b.txt\n?? d.txt\n?? fresh/e.txt\n"
try repo.add("a.txt", "b.txt")
let staged = try statusText(repo)
fail "after add: {staged}" unless staged == "M a.txt\nD b.txt\n?? d.txt\n?? fresh/\n"
repo.close()
return none
}
@test()
def detectsRenamesAndIgnores() !none {
let repo = try newWorktree("renames")
try writeFile(repo, ".gitignore", "*.log\nbuild/\n")
try writeFile(repo, "src/main.c", "int main() {\n\treturn 0;\n}\n")
try repo.add(".")
try repo.commit("Initial")
try repo.move("src/main.c", "src/app.c")
try writeFile(repo, "debug.log", "x\n")
try writeFile(repo, "build/out.o", "x\n")
let text = try statusText(repo)
fail "rename: {text}" unless text == "R src/main.c -> src/app.c\n"
let ignored = try statusText(repo, StatusOptions{ignored: true})
fail "ignored: {ignored}" unless ignored == "!! build/\n!! debug.log\nR src/main.c -> src/app.c\n"
// An ignored file is refused unless forced.
let refused = false
repo.add("debug.log") onerror (err error) {
refused = true
recover none
}
fail "ignored file added" unless refused
try repo.add(["debug.log"], AddOptions{force: true})
fail "forced add: {try statusText(repo)}" unless (try statusText(repo)) == "A debug.log\nR src/main.c -> src/app.c\n"
repo.close()
return none
}
@test()
def tracksModesLinksAndIntent() !none {
let repo = try newWorktree("modes")
try writeFile(repo, "run.sh", "#!/bin/sh\n")
try writeFile(repo, "target.txt", "t\n")
try os.symlink("target.txt", os.join(repo.workDir, "link"))
try repo.add(".")
try repo.commit("Initial")
let tree = try repo.tree("HEAD")
let link = tree.entry("link")
fail "link mode" when link is none || link.mode != EntryMode.Symlink
try os.chmod(os.join(repo.workDir, "run.sh"), 0o755)
try writeFile(repo, "later.txt", "l\n")
try repo.add(["later.txt"], AddOptions{intentToAdd: true})
let text = try statusText(repo)
fail "modes: {text}" unless text == " A later.txt\n M run.sh\n"
try repo.add("run.sh")
let mode = (try repo.index()).entry("run.sh")
fail "exec bit not staged" when mode is none || mode.mode != 0o100755
try statusText(repo)
repo.close()
return none
}
@test()
def convertsLineEndings() !none {
let repo = try newWorktree("eol")
try writeFile(repo, ".gitattributes", "*.txt text eol=crlf\n*.bin -text\n")
try writeFile(repo, "a.txt", "one\r\ntwo\r\n")
try writeFile(repo, "b.bin", "raw\r\n")
try repo.add(".")
try repo.commit("Initial")
let a = try repo.blob("HEAD:a.txt")
fail "a.txt stored with CR" unless string(a.data) == "one\ntwo\n"
let b = try repo.blob("HEAD:b.bin")
fail "b.bin changed" unless string(b.data) == "raw\r\n"
fail "clean after add: {try statusText(repo)}" unless (try statusText(repo)) == ""
fail "smudge" unless string(try toWorktree(repo, "a.txt", a.data)) == "one\r\ntwo\r\n"
// core.autocrlf=true: CRLF files are stored with LF, LF ones too.
let auto = try newWorktree("autocrlf")
auto.config.override("core.autocrlf", "true")
try writeFile(auto, "c.txt", "x\r\ny\r\n")
try auto.add(".")
let idx = try auto.index()
let e = idx.entry("c.txt")
fail "c.txt not staged" when e is none
let (_, data) = try auto.raw(e.id)
fail "autocrlf stored {data.len()} bytes" unless string(data) == "x\ny\n"
try statusText(auto)
auto.close()
try statusText(repo)
repo.close()
return none
}
@test()
def catchesRacilyCleanFiles() !none {
let repo = try newWorktree("racy")
try writeFile(repo, "f.txt", "aaaa\n")
try repo.add(".")
try repo.commit("Initial")
// Same size, same second: only the racy check sees it.
try writeFile(repo, "f.txt", "bbbb\n")
let text = try statusText(repo)
fail "racy: {text}" unless text == " M f.txt\n"
repo.close()
return none
}
@test()
def removesAndAmends() !none {
let repo = try newWorktree("rm")
try writeFile(repo, "keep.txt", "k\n")
try writeFile(repo, "gone.txt", "g\n")
try repo.add(".")
let first = try repo.commit("First")
try repo.remove(["gone.txt"])
fail "gone.txt still on disk" when os.exists(os.join(repo.workDir, "gone.txt")) onerror recover true
let text = try statusText(repo)
fail "rm: {text}" unless text == "D gone.txt\n"
let second = try repo.commit("Second\n\n# comment\nbody \n\n\n")
let c = try repo.commitAt(second)
fail "message cleanup: {c.message}" unless c.message == "Second\n\nbody\n"
fail "parent" unless c.parents.len() == 1 && c.parents[0].equals(first)
try writeFile(repo, "keep.txt", "k2\n")
try repo.add("keep.txt")
let amended = try repo.commit("Second, amended", CommitOptions{amend: true})
let a = try repo.commitAt(amended)
fail "amend parent" unless a.parents.len() == 1 && a.parents[0].equals(first)
fail "amend author kept" unless a.author.str() == c.author.str()
let log = try repo.reflog("HEAD")
fail "reflog {log.len()}" unless log.len() == 3 && log[2].message == "commit (amend): Second, amended"
// Nothing to commit.
let refused = false
repo.commit("Again") onerror (err error) {
refused = true
recover zeroId()
}
fail "empty commit accepted" unless refused
try statusText(repo)
repo.close()
return none
}
@test()
def guardsRemovals() !none {
// Each case: what is done to `f.txt` after the commit, and whether
// `rm` and `rm --cached` go through without force (as git decides).
let cases string[][] = [["modify", "no", "yes"], ["stage", "no", "yes"], ["stage-modify", "no", "no"], ["new", "no", "yes"]]
for let c in cases {
for let cached in [false, true] {
let repo = try newWorktree("rm-{c[0]}-{cached}")
try writeFile(repo, "f.txt", "one\n")
try writeFile(repo, "keep.txt", "keep\n")
try repo.add("f.txt", "keep.txt")
let _ = try repo.commit("First")
switch c[0] {
case "modify":
try writeFile(repo, "f.txt", "two\n")
case "stage":
try writeFile(repo, "f.txt", "two\n")
try repo.add("f.txt")
case "stage-modify":
try writeFile(repo, "f.txt", "two\n")
try repo.add("f.txt")
try writeFile(repo, "f.txt", "three\n")
case "new":
try writeFile(repo, "g.txt", "new\n")
try repo.add("g.txt")
}
let target = c[0] == "new" ?? ("g.txt", "f.txt")
let want = cached ?? (c[2] == "yes", c[1] == "yes")
if gitBinary != "" {
let home = try scratch("rm-home")
let args = cached ?? (string["rm", "-n", "-q", "--cached", target], string["rm", "-n", "-q", target])
let gitOk = (runGit(home, repo.workDir, args) onerror recover "refused") != "refused"
fail "{c[0]} cached={cached}: git says {gitOk}, the case table {want}" unless gitOk == want
}
let ok = true
repo.remove([target], cached) onerror (err error) {
ok = false
recover none
}
fail "{c[0]} cached={cached}: removal {ok ?? ("went through", "was refused")}" unless ok == want
// Forced, it always goes.
if !ok {
try repo.remove([target], cached, true)
}
fail "{c[0]} cached={cached}: still staged" unless (try repo.index()).entry(target) is none
}
}
return none
}
@test()
def switchesBranches() !none {
let repo = try newWorktree("checkout")
try writeFile(repo, "shared.txt", "shared\n")
try writeFile(repo, "changing.txt", "main\n")
try writeFile(repo, "dir/only-main.txt", "m\n")
try repo.add(".")
let base = try repo.commit("Base")
try repo.createBranch("feature")
try repo.checkout("feature")
fail "branch is {try repo.branch()}" unless (try repo.branch()) == "feature"
try writeFile(repo, "changing.txt", "feature\n")
try repo.remove(["dir/only-main.txt"])
try writeFile(repo, "new.txt", "new\n")
try repo.add(".")
try repo.commit("Feature work")
// A local edit to a file both branches share is carried over.
try writeFile(repo, "shared.txt", "edited\n")
try repo.checkout("main")
fail "edit lost" unless string(try os.readFile(os.join(repo.workDir, "shared.txt"))) == "edited\n"
fail "changing.txt not switched" unless string(try os.readFile(os.join(repo.workDir, "changing.txt"))) == "main\n"
fail "new.txt left behind" when os.exists(os.join(repo.workDir, "new.txt")) onerror recover true
fail "only-main.txt not restored" unless os.exists(os.join(repo.workDir, "dir", "only-main.txt")) onerror recover false
fail "status after switch: {try statusText(repo)}" unless (try statusText(repo)) == " M shared.txt\n"
// An edit to a file that differs between branches stops the switch.
try writeFile(repo, "changing.txt", "local\n")
let refused = false
repo.checkout("feature") onerror (err error) {
refused = err.message.contains("would be overwritten")
recover none
}
fail "dirty switch allowed" unless refused
fail "branch moved anyway" unless (try repo.branch()) == "main"
// Force throws local changes away.
try repo.checkout("feature", CheckoutOptions{force: true})
fail "forced status: {try statusText(repo)}" unless (try statusText(repo)) == ""
// Detached at a commit.
try repo.checkout(base.str())
fail "not detached" unless (try repo.branch()) == ""
let log = try repo.reflog("HEAD")
let last = log[log.len() - 1]
fail "reflog says {last.message}" unless last.message == "checkout: moving from feature to {base.str()}" && last.newId.equals(base)
try repo.checkout("main")
try repo.renameBranch("feature", "topic")
fail "rename" unless (try repo.branches()).join(",") == "main,topic"
let unmerged = false
repo.deleteBranch("topic") onerror (err error) {
unmerged = true
recover none
}
fail "unmerged branch deleted" unless unmerged
try repo.deleteBranch("topic", true)
try statusText(repo)
repo.close()
return none
}
@test()
def resetsAndRestores() !none {
let repo = try newWorktree("reset")
try writeFile(repo, "a.txt", "1\n")
try repo.add(".")
let first = try repo.commit("First")
try writeFile(repo, "a.txt", "2\n")
try repo.add(".")
let second = try repo.commit("Second")
try repo.reset("HEAD~1", ResetMode.Soft)
fail "soft: {try statusText(repo)}" unless (try statusText(repo)) == "M a.txt\n"
fail "ORIG_HEAD" unless (try repo.resolve("ORIG_HEAD")).equals(second)
try repo.reset("HEAD", ResetMode.Mixed)
fail "mixed: {try statusText(repo)}" unless (try statusText(repo)) == " M a.txt\n"
try repo.reset("HEAD", ResetMode.Hard)
fail "hard: {try statusText(repo)}" unless (try statusText(repo)) == ""
fail "hard content" unless string(try os.readFile(os.join(repo.workDir, "a.txt"))) == "1\n"
try writeFile(repo, "a.txt", "3\n")
try repo.add("a.txt")
try repo.restore(["a.txt"], "", true, false)
fail "restore --staged: {try statusText(repo)}" unless (try statusText(repo)) == " M a.txt\n"
try repo.restore(["a.txt"])
fail "restore: {try statusText(repo)}" unless (try statusText(repo)) == ""
try repo.restore(["a.txt"], second.str())
fail "restore --source" unless string(try os.readFile(os.join(repo.workDir, "a.txt"))) == "2\n"
let log = try repo.reflog("HEAD")
fail "reset reflog" unless log[log.len() - 1].message == "reset: moving to HEAD" && log[log.len() - 3].message == "reset: moving to HEAD~1"
fail "head moved" unless (try repo.resolve("HEAD")).equals(first)
try statusText(repo)
repo.close()
return none
}
}
}