wsb 0.275.7622

Workspace tools for every project under $PRJ: git across submodules, template stamping, GitHub, and the workbench
Documentation
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
//! Worktrees of a project with nested submodules, made, removed and
//! migrated against real repositories: what is being tested is which git
//! repository each checkout is a worktree of, and a stand-in would agree with
//! any answer.

use super::*;

fn run(dir: &Path, args: &[&str]) -> String {
    let output = crate::git_backend::git_command()
        .arg("-C")
        .arg(dir)
        .args(["-c", "user.name=Test", "-c", "user.email=test@example.com", "-c", "protocol.file.allow=always"])
        .args(args)
        .output()
        .expect("git runs");
    assert!(
        output.status.success(),
        "git {args:?} in {}: {}",
        dir.display(),
        String::from_utf8_lossy(&output.stderr)
    );
    String::from_utf8_lossy(&output.stdout).trim().to_string()
}

fn commit_file(dir: &Path, name: &str, text: &str) -> String {
    if let Some(parent) = dir.join(name).parent() {
        std::fs::create_dir_all(parent).expect("its folder");
    }
    std::fs::write(dir.join(name), text).expect("a file");
    run(dir, &["add", name]);
    run(dir, &["commit", "--quiet", "-m", name]);
    run(dir, &["rev-parse", "HEAD"])
}

fn origin(dir: &Path) {
    std::fs::create_dir_all(dir).expect("mkdir");
    run(dir, &["init", "--quiet", "-b", "main"]);
    commit_file(dir, "README.md", "readme\n");
}

/// `$PRJ` with project `alpha`: `top`, holding submodule `mid`, which holds
/// submodule `.deps/leaf` — cloned, recursively, as `alpha/main-worktree`.
///
/// The leaf's path starts with a dot, as `.github` does: a path component
/// that a git ref name may not have.
struct Layout {
    _scratch: tempfile::TempDir,
    origins: PathBuf,
    prj: wsb_prj::Workshop,
}

impl Layout {
    fn new() -> Self {
        let scratch = tempfile::tempdir().expect("a scratch directory");
        let base = crate::platform::canonicalize(scratch.path()).expect("resolves");
        let origins = base.join("origins");
        for name in ["leaf", "mid", "top"] {
            origin(&origins.join(name));
        }
        let leaf = origins.join("leaf");
        let mid = origins.join("mid");
        run(&mid, &["submodule", "--quiet", "add", leaf.to_str().unwrap(), ".deps/leaf"]);
        run(&mid, &["commit", "--quiet", "-m", "leaf"]);
        let top = origins.join("top");
        run(&top, &["submodule", "--quiet", "add", mid.to_str().unwrap(), "mid"]);
        run(&top, &["commit", "--quiet", "-m", "mid"]);

        let root = base.join("prj");
        let project = root.join("alpha");
        std::fs::create_dir_all(&project).expect("mkdir");
        run(
            &project,
            &["clone", "--quiet", "--recurse-submodules", top.to_str().unwrap(), "main-worktree"],
        );
        let prj = wsb_prj::Workshop::at(&root).expect("a PRJ");
        Self {
            _scratch: scratch,
            origins,
            prj,
        }
    }

    fn target(&self, name: &str) -> Target {
        Target::parse(&self.prj, &format!("alpha/{name}")).expect("a target")
    }

    fn main(&self) -> PathBuf {
        self.prj.workspace("alpha").expect("alpha").main_worktree()
    }
}

/// TEST20037: a worktree's submodules are worktrees of main's repositories,
/// on the worktree's branch, locked.
///
/// Cloned instead, each worktree carried its own copy of every submodule —
/// a gigabyte apiece here — and a commit made in one worktree's submodule
/// could not be seen from main until it had been pushed and fetched.
#[test]
fn test20037_a_worktrees_submodules_share_mains_repositories() {
    let layout = Layout::new();
    let main = layout.main();
    let target = layout.target("x");
    let checkouts = add(&target, None, None).expect("made");
    let wt = target.path();

    let paths: Vec<&str> = checkouts.iter().map(|c| c.path.as_str()).collect();
    assert_eq!(paths, ["", "mid", "mid/.deps/leaf"]);
    for checkout in &checkouts {
        assert_eq!(checkout.branch.as_deref(), Some("card/x"), "{checkout:?}");
    }

    for (here, there) in [
        (wt.clone(), main.clone()),
        (wt.join("mid"), main.join("mid")),
        (wt.join("mid/.deps/leaf"), main.join("mid/.deps/leaf")),
    ] {
        assert_eq!(
            common_dir(&here).unwrap(),
            common_dir(&there).unwrap(),
            "{} is not a worktree of {}",
            here.display(),
            there.display()
        );
        let registered = registrations(&there).unwrap();
        let mine = registered
            .iter()
            .find(|registration| same_path(&registration.path, &here))
            .expect("registered");
        assert!(mine.locked, "{} is not locked", here.display());
    }
    assert!(
        !wt.join(".git").is_dir() && !common_dir(&wt).unwrap().join("worktrees/x-worktree/modules").exists(),
        "no submodule was cloned into the worktree's git directory"
    );
    assert_eq!(run(&wt, &["status", "--porcelain"]), "", "the superproject is clean");

    // A commit in the worktree's submodule is in main's repository at once.
    let made = commit_file(&wt.join("mid/.deps/leaf"), "new.txt", "new\n");
    assert!(has_commit(&main.join("mid/.deps/leaf"), &made).unwrap());

    // An existing branch is used, and a second worktree gets its own.
    run(&main.join("mid"), &["branch", "card/y"]);
    let second = add(&layout.target("y"), None, None).expect("made");
    assert_eq!(second[1].branch.as_deref(), Some("card/y"));
}

/// TEST20038: removing a worktree refuses to lose work, and takes nothing
/// else with it.
///
/// Uncommitted changes, untracked files and commits on no remote branch, in
/// any repository however deep, refuse the removal before anything is
/// touched. Removal takes this worktree's registrations and no other's: a
/// global prune once dropped the registrations of a worktree whose folder was
/// briefly elsewhere, and its submodules stopped being repositories.
#[test]
fn test20038_removal_refuses_unsaved_work_and_prunes_nothing_else() {
    let layout = Layout::new();
    let main = layout.main();
    let target = layout.target("x");
    add(&target, None, None).expect("made");
    let wt = target.path();
    let leaf = wt.join("mid/.deps/leaf");

    // Another worktree whose folder is missing right now, and an unlocked
    // registration someone made by hand whose folder is missing too: a
    // prune would drop the second (it leaves locked ones), so the second is
    // what shows whether removal pruned.
    let away = layout.target("away");
    add(&away, None, None).expect("made");
    let moved = layout.origins.join("away-moved-aside");
    std::fs::rename(away.path(), &moved).expect("moved aside");
    let by_hand = layout.origins.join("by-hand");
    run(&main.join("mid"), &["worktree", "add", "--quiet", "--detach", by_hand.to_str().unwrap()]);
    let by_hand_moved = layout.origins.join("by-hand-moved-aside");
    std::fs::rename(&by_hand, &by_hand_moved).expect("moved aside");

    std::fs::write(leaf.join("scratch.txt"), "untracked\n").expect("a file");
    let refusal = remove(&target, false).expect_err("an untracked file refuses");
    assert!(refusal.contains("mid/.deps/leaf") && refusal.contains("untracked"), "{refusal}");
    assert!(wt.is_dir(), "nothing was removed");
    std::fs::remove_file(leaf.join("scratch.txt")).expect("rm");

    commit_file(&leaf, "local.txt", "only here\n");
    let refusal = remove(&target, false).expect_err("an unpushed commit refuses");
    assert!(refusal.contains("on no remote branch"), "{refusal}");

    run(&leaf, &["push", "--quiet", "--set-upstream", "origin", "card/x"]);
    remove(&target, true).expect("removed");
    assert!(!wt.exists());
    for repo in [main.clone(), main.join("mid"), main.join("mid/.deps/leaf")] {
        assert!(
            !registrations(&repo).unwrap().iter().any(|r| r.path.ends_with("x-worktree") || r.path.to_string_lossy().contains("x-worktree/")),
            "{} still registers the removed worktree",
            repo.display()
        );
        assert!(
            registrations(&repo).unwrap().iter().any(|r| r.path.to_string_lossy().contains("away-worktree")),
            "{} lost the other worktree's registration",
            repo.display()
        );
    }
    assert!(
        registrations(&main.join("mid")).unwrap().iter().any(|r| r.path.ends_with("by-hand")),
        "an unlocked registration somebody else made was pruned"
    );
    assert!(!has_local_branch(&main, "card/x").unwrap(), "--delete-branch deleted the branch");
    std::fs::rename(&moved, away.path()).expect("moved back");
    assert_eq!(run(&away.path().join("mid"), &["rev-parse", "--is-inside-work-tree"]), "true");
}

/// TEST20039: an older worktree's cloned submodules become shared ones, and
/// no commit only a clone had is lost.
///
/// The worktrees made before this module had a clone per submodule. Each
/// clone's branches and HEAD are fetched into main's repository under
/// `refs/salvage/<worktree>/` first; the checkout is then put back as a
/// worktree of main's repository on the same commit and branch, and the clone
/// is gone. A clone at a path no ref name could spell (`.deps/leaf`) is
/// salvaged like any other.
#[test]
fn test20039_cloned_submodules_are_migrated_without_losing_a_commit() {
    let layout = Layout::new();
    let main = layout.main();
    let target = layout.target("old");
    let wt = target.path();
    run(&main, &["worktree", "add", "--quiet", "-b", "card/old", wt.to_str().unwrap()]);
    run(&wt, &["submodule", "--quiet", "update", "--init", "--recursive"]);
    let leaf = wt.join("mid/.deps/leaf");
    run(&leaf, &["checkout", "--quiet", "-b", "feature"]);
    let only_in_clone = commit_file(&leaf, "clone-only.txt", "x\n");
    assert_ne!(common_dir(&leaf).unwrap(), common_dir(&main.join("mid/.deps/leaf")).unwrap());
    let before = list(&layout.prj.workspace("alpha").unwrap()).unwrap();
    let old = before.iter().find(|w| w.name == "old").unwrap();
    assert_eq!(old.cloned, ["mid", "mid/.deps/leaf"]);

    // Dirty clones stop it before anything moves.
    std::fs::write(wt.join("mid/stray.txt"), "x\n").expect("a file");
    assert!(migrate(&target).expect_err("dirty").contains("mid"));
    std::fs::remove_file(wt.join("mid/stray.txt")).expect("rm");

    let placed = migrate(&target).expect("migrated");
    assert_eq!(placed.iter().map(|c| c.path.as_str()).collect::<Vec<_>>(), ["mid", "mid/.deps/leaf"]);
    let main_leaf = main.join("mid/.deps/leaf");
    assert_eq!(common_dir(&leaf).unwrap(), common_dir(&main_leaf).unwrap(), "now a worktree of main's");
    assert_eq!(run(&leaf, &["rev-parse", "HEAD"]), only_in_clone, "on the same commit");
    assert_eq!(branch_of(&leaf).unwrap().as_deref(), Some("feature"), "on the same branch");
    assert_eq!(
        run(&main_leaf, &["rev-parse", "refs/salvage/old/heads/feature"]),
        only_in_clone,
        "the clone's branch is kept in main's repository"
    );
    assert!(
        !common_dir(&wt).unwrap().join("worktrees/old-worktree/modules").exists(),
        "the clones' git directories are gone"
    );
    let after = list(&layout.prj.workspace("alpha").unwrap()).unwrap();
    let old = after.iter().find(|w| w.name == "old").unwrap();
    assert!(old.cloned.is_empty(), "{old:?}");
    assert_eq!(old.shared, ["mid", "mid/.deps/leaf"]);
    assert!(migrate(&target).expect("again").is_empty(), "a migrated worktree has nothing left to migrate");
}

/// TEST20040: a worktree that cannot be made whole is not made at all.
///
/// A submodule main has not checked out cannot share main's repository. The
/// superproject worktree made before that is found is removed again, with its
/// branch, rather than left as a worktree with a hole in it.
#[test]
fn test20040_a_failed_add_leaves_nothing_behind() {
    let layout = Layout::new();
    let main = layout.main();
    run(&main, &["submodule", "--quiet", "deinit", "--force", "--all"]);
    let target = layout.target("x");
    let error = add(&target, None, None).expect_err("mid is not checked out in main");
    assert!(error.contains("mid") && error.contains("nothing of alpha/x was left behind"), "{error}");
    assert!(!target.path().exists());
    assert!(!has_local_branch(&main, "card/x").unwrap(), "the branch made for it is gone");
    assert_eq!(registrations(&main).unwrap().len(), 1, "only main is registered");

    assert!(Target::parse(&layout.prj, "alpha/main").is_err(), "main is not made by add");
    assert!(Target::parse(&layout.prj, "alpha").is_err());
    assert!(Target::parse(&layout.prj, "nope/x").is_err());
}

/// TEST20046: a checkout whose registration was pruned is reported, kept from
/// removal, and repaired without losing what it held.
///
/// A global `git worktree prune` once dropped the registrations of a
/// worktree's submodules: their `.git` named repositories that were gone, and
/// every git command in the worktree failed. Listing it said nothing useful,
/// and nothing could put it right. The repair moves the orphaned checkout to
/// the project's state folder, puts a worktree of main's repository back on
/// the commit the superproject pins, and drops the orphan only when nothing
/// in it differs.
#[test]
fn test20046_a_pruned_checkout_is_repaired_without_losing_work() {
    let layout = Layout::new();
    let main = layout.main();
    let project = layout.prj.workspace("alpha").unwrap();

    for (name, edit) in [("clean", false), ("edited", true)] {
        let target = layout.target(name);
        add(&target, None, None).expect("made");
        let wt = target.path();
        let mid = wt.join("mid");
        if edit {
            std::fs::write(mid.join("README.md"), "an edit nobody committed\n").expect("write");
        }
        // What a prune does: the registration goes, the checkout stays.
        let registration = git_dir(&mid).expect("mid is a worktree");
        std::fs::remove_dir_all(&registration).expect("pruned");

        let listed = list(&project).unwrap();
        let entry = listed.iter().find(|w| w.name == name).unwrap();
        assert_eq!(entry.broken, ["mid"], "{entry:?}");
        assert!(remove(&target, false).expect_err("refused").contains("migrate"), "rm points at the repair");

        let placed = migrate(&target).expect("repaired");
        assert!(placed.iter().any(|c| c.path == "mid"), "{placed:?}");
        assert_eq!(common_dir(&mid).unwrap(), common_dir(&main.join("mid")).unwrap());
        assert_eq!(
            run(&wt, &["ls-tree", "HEAD", "mid"]).split_whitespace().nth(2).unwrap(),
            run(&mid, &["rev-parse", "HEAD"]),
            "put back on the commit the superproject pins"
        );
        assert_eq!(run(&mid.join(".deps/leaf"), &["rev-parse", "--is-inside-work-tree"]), "true");

        let aside = project.state_dir().join("orphaned").join(name);
        if edit {
            let kept: Vec<PathBuf> = std::fs::read_dir(&aside)
                .expect("the edited checkout is kept")
                .map(|e| e.unwrap().path().join("mid/README.md"))
                .collect();
            assert_eq!(kept.len(), 1);
            assert_eq!(std::fs::read_to_string(&kept[0]).unwrap(), "an edit nobody committed\n");
        } else {
            assert!(!aside.exists(), "an orphan holding nothing new is not kept");
        }
    }
}

/// Ignore `patterns` in every worktree of the repository at `repo`, as a
/// project ignores its build output.
fn exclude(repo: &Path, patterns: &str) {
    let info = common_dir(repo).expect("a repository").join("info");
    std::fs::create_dir_all(&info).expect("mkdir");
    std::fs::write(info.join("exclude"), patterns).expect("written");
}

fn layout_ignoring() -> Layout {
    let layout = Layout::new();
    let main = layout.main();
    for repo in [main.clone(), main.join("mid"), main.join("mid/.deps/leaf")] {
        exclude(&repo, "target/\n");
    }
    layout
}

/// TEST20056: a snapshot is every repository its source has checked out,
/// each detached at that checkout's commit, and is taken again in place.
///
/// What it holds is the source as checked out — a submodule ahead of what its
/// parent pins is taken at its own commit — each a worktree of main's
/// repository, locked, with wsb's markers copied so wsb renders there. Taken
/// again, the same paths move to the new commits: what the last run added is
/// cleared and its ignored outputs are kept, so a build keyed by the paths
/// starts warm. Only a temporary worktree says it is one — not main, and not a
/// plain copy of a tree, which is no repository at all.
#[test]
fn test20056_a_snapshot_is_every_checkout_at_its_commit_taken_again_in_place() {
    let layout = layout_ignoring();
    let main = layout.main();
    // What wsb needs in a repository is committed in it, and comes with the
    // commit: nothing is copied beside the checkouts.
    commit_file(&main.join("mid"), ".wsb/repo.json", "{\"name\":\"mid\"}\n");
    let leaf_head = commit_file(&main.join("mid/.deps/leaf"), "ahead.txt", "ahead\n");
    let pinned = run(&main.join("mid"), &["rev-parse", "HEAD:.deps/leaf"]);
    assert_ne!(leaf_head, pinned, "the case this holds: a checkout ahead of its pin");

    let target = layout.target("snap");
    let checkouts = snapshot(&target, "main", false).expect("taken");
    let wt = target.path();
    assert_eq!(
        checkouts.iter().map(|c| c.path.as_str()).collect::<Vec<_>>(),
        ["", "mid", "mid/.deps/leaf"]
    );
    for relative in ["", "mid", "mid/.deps/leaf"] {
        let (here, there) = (wt.join(relative), main.join(relative));
        assert_eq!(run(&here, &["rev-parse", "HEAD"]), run(&there, &["rev-parse", "HEAD"]), "{relative:?} is not at its source's commit");
        assert_eq!(run(&here, &["branch", "--show-current"]), "", "{relative:?} is not detached");
        assert_eq!(common_dir(&here).unwrap(), common_dir(&there).unwrap(), "{relative:?} is not a worktree of main's repository");
        let registered = registrations(&there).unwrap();
        assert!(
            registered.iter().any(|r| same_path(&r.path, &here) && r.locked),
            "{relative:?} is not registered and locked"
        );
    }
    assert_eq!(std::fs::read_to_string(wt.join("mid/.wsb/repo.json")).unwrap(), "{\"name\":\"mid\"}\n");

    let record = temporary(&wt).expect("read").expect("a temporary worktree says it is one");
    assert_eq!(record.of, "main");
    assert!(!record.working);
    assert_eq!(record.commits["mid/.deps/leaf"], leaf_head);
    assert_eq!(record.commits["."], run(&main, &["rev-parse", "HEAD"]));
    assert_eq!(temporary(&main).expect("read"), None, "main took itself for a temporary worktree");
    let plain = tempfile::tempdir().expect("a scratch directory");
    std::fs::write(plain.path().join("workspace.json"), "{}\n").expect("written");
    assert_eq!(temporary(plain.path()).expect("a plain copy is answered, not an error"), None);

    std::fs::write(wt.join("mid/left.txt"), "from the last run\n").expect("written");
    std::fs::create_dir_all(wt.join("mid/target")).expect("mkdir");
    std::fs::write(wt.join("mid/target/built"), "kept\n").expect("written");
    let mid_head = commit_file(&main.join("mid"), "two.txt", "two\n");
    snapshot(&target, "main", false).expect("taken again");
    assert_eq!(run(&wt.join("mid"), &["rev-parse", "HEAD"]), mid_head);
    assert!(wt.join("mid/two.txt").is_file());
    assert!(!wt.join("mid/left.txt").exists(), "what the last run added is still there");
    assert_eq!(std::fs::read_to_string(wt.join("mid/target/built")).unwrap(), "kept\n", "an ignored output was cleared");
    assert_eq!(temporary(&wt).unwrap().unwrap().commits["mid"], mid_head);
}

/// TEST20057: a snapshot with its source's working changes holds exactly what
/// the source's working trees hold.
///
/// Modified, staged, untracked: copied. Deleted: gone. Ignored: not copied —
/// a run builds its own outputs. Each repository's changes over its own
/// checkout.
#[test]
fn test20057_a_working_snapshot_holds_the_working_changes() {
    let layout = layout_ignoring();
    let main = layout.main();
    let mid = main.join("mid");
    commit_file(&mid, "doomed.txt", "doomed\n");
    std::fs::write(mid.join("README.md"), "modified\n").expect("written");
    std::fs::remove_file(mid.join("doomed.txt")).expect("deleted");
    std::fs::write(mid.join("staged.txt"), "staged\n").expect("written");
    run(&mid, &["add", "staged.txt"]);
    std::fs::create_dir_all(mid.join("new")).expect("mkdir");
    std::fs::write(mid.join("new/untracked.txt"), "untracked\n").expect("written");
    std::fs::create_dir_all(mid.join("target")).expect("mkdir");
    std::fs::write(mid.join("target/built"), "ignored\n").expect("written");
    std::fs::write(main.join("mid/.deps/leaf/README.md"), "leaf modified\n").expect("written");

    let target = layout.target("snap");
    snapshot(&target, "main", true).expect("taken");
    let wt = target.path();
    let text = |path: &Path| std::fs::read_to_string(path).ok();
    assert_eq!(text(&wt.join("mid/README.md")).as_deref(), Some("modified\n"));
    assert_eq!(text(&wt.join("mid/staged.txt")).as_deref(), Some("staged\n"));
    assert_eq!(text(&wt.join("mid/new/untracked.txt")).as_deref(), Some("untracked\n"));
    assert!(!wt.join("mid/doomed.txt").exists(), "a deleted file is in the snapshot");
    assert!(!wt.join("mid/target").exists(), "an ignored output was copied");
    assert_eq!(text(&wt.join("mid/.deps/leaf/README.md")).as_deref(), Some("leaf modified\n"));
    assert!(temporary(&wt).unwrap().unwrap().working);
}

/// TEST20058: a snapshot is not taken over somebody's worktree, nor of a
/// source it could not be a worktree of main's repositories of — and a
/// refused first taking leaves nothing behind.
#[test]
fn test20058_a_snapshot_refuses_what_it_cannot_be() {
    let layout = layout_ignoring();
    let main = layout.main();

    let own = layout.target("x");
    add(&own, None, None).expect("made");
    let refused = snapshot(&own, "main", false).expect_err("a worktree of its own");
    assert!(refused.contains("not a temporary one"), "{refused}");
    assert_eq!(run(&own.path(), &["branch", "--show-current"]), "card/x", "the worktree was taken over");

    let snap = layout.target("snap");
    snapshot(&snap, "main", false).expect("taken");
    let refused = snapshot(&snap, "x", false).expect_err("of another source");
    assert!(refused.contains("a snapshot of alpha/main"), "{refused}");

    // A worktree made the old way: its submodules are clones of their own.
    let old = layout.target("old");
    run(&main, &["worktree", "add", "--quiet", "-b", "card/old", old.path().to_str().unwrap()]);
    run(&old.path(), &["submodule", "--quiet", "update", "--init", "--recursive"]);
    let of_old = layout.target("of-old");
    let refused = snapshot(&of_old, "old", false).expect_err("cloned submodules");
    assert!(refused.contains("mid: a separate clone") && refused.contains("migrate alpha/old"), "{refused}");
    assert!(!of_old.path().exists(), "a refused snapshot left something behind");

    // A worktree whose submodules were never checked out.
    let bare = layout.target("bare");
    run(&main, &["worktree", "add", "--quiet", "-b", "card/bare", bare.path().to_str().unwrap()]);
    let of_bare = layout.target("of-bare");
    let refused = snapshot(&of_bare, "bare", false).expect_err("not checked out");
    assert!(refused.contains("mid: not checked out in bare"), "{refused}");
    assert!(!of_bare.path().exists(), "a refused snapshot left something behind");
}

/// TEST20059: a temporary worktree is removed when everything it holds is its
/// source's, and not when it holds a commit made in it.
///
/// Its commits are its source's — a submodule's detached commit nobody has
/// pushed, here — and removing the snapshot loses none of them: the source
/// has them checked out. A commit made in the snapshot itself, above what it
/// was taken at and on no branch, would be lost.
#[test]
fn test20059_a_temporary_worktree_is_removed_when_nothing_in_it_would_be_lost() {
    let layout = layout_ignoring();
    let main = layout.main();
    commit_file(&main.join("mid/.deps/leaf"), "unpushed.txt", "only on main's local branch\n");
    let target = layout.target("snap");
    snapshot(&target, "main", false).expect("taken");
    remove(&target, false).expect("its commits are on a branch");
    assert!(!target.path().exists());

    snapshot(&target, "main", false).expect("taken again");
    commit_file(&target.path().join("mid"), "made-here.txt", "a run's own commit\n");
    let refused = remove(&target, false).expect_err("a commit made in it");
    assert!(refused.contains("mid: 1 commit(s) made here, on no branch"), "{refused}");
    assert!(target.path().is_dir());
}