cargo-steward 0.2.2

Keeps Cargo build output fast and bounded: stops the debug object-file leak, cleans stale and orphaned build directories, and checks your setup
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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
//! Warm starts for new worktrees: `cargo steward seed` and the git hooks
//! that run it.
//!
//! A new worktree normally starts with an empty build directory and compiles
//! every dependency from scratch. Seeding starts it instead as a
//! copy-on-write clone of another worktree's build directory (the main
//! checkout's when a build isn't using it, else the most recently used
//! other one): the clone takes a fraction of a second and no space, and
//! since it keeps every file's modification time, cargo finds the
//! dependencies fresh and only rebuilds crates whose sources are newer than
//! their outputs.

pub(crate) mod baseline;
pub(crate) mod clone;
mod hooks;

use std::env;
use std::ffi::OsString;
use std::fs;
use std::io;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::thread::sleep;
use std::time::{Duration, Instant};

use anyhow::{Context, Result};
use serde::Deserialize;

use crate::config::{self, Paths};
use crate::fsutil::{self, Lock};
use crate::{gc, ledger, scan};

pub use hooks::{HooksArgs, run_hooks};

use baseline::Candidate;
use clone::Cloned;

/// How long to wait in all for builds to release a build directory to seed
/// from before giving up.
pub(crate) const LOCK_WAIT: Duration = Duration::from_secs(5);

/// How long to wait for a build to release one candidate before trying the
/// next.
const EACH_WAIT: Duration = Duration::from_millis(500);

/// `cargo steward seed`. Its flags are a contract: the `cargo` proxy runs
/// `cargo-steward seed --target-dir <new> --worktree <dir> --quiet`.
#[derive(Debug, Default, clap::Args)]
pub struct SeedArgs {
    /// The target directory to start [default: the worktree's, as `cargo
    /// metadata` reports it]. With `build.build-dir` set, the worktree's
    /// build directory is started too.
    #[arg(long, value_name = "DIR")]
    pub target_dir: Option<PathBuf>,
    /// A build directory to clone, instead of looking for one; repeat it to
    /// name several. The first is used unless a build is using it, then the
    /// most recently used of the others [default: the main checkout's, else
    /// the most recently used of the repository's other worktrees']
    #[arg(long, value_name = "DIR")]
    pub from: Vec<PathBuf>,
    /// The worktree [default: the current directory]
    #[arg(long, value_name = "DIR")]
    pub worktree: Option<PathBuf>,
    /// Print nothing
    #[arg(short, long)]
    pub quiet: bool,
}

/// What seeding one build directory did. Only a real I/O failure is an
/// error; each of these exits 0.
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum Outcome {
    Seeded(Duration),
    /// The target already holds a build.
    AlreadyBuilt,
    /// The target exists and holds something else; it's left alone.
    Occupied,
    /// There is no build to clone.
    NoBaseline,
    /// Target and baseline are the same directory, or one holds the other.
    Same,
    /// Builds kept every baseline locked.
    Busy,
    /// The filesystem can't clone, or the two are on different volumes.
    Unsupported,
}

/// What seeding a directory from the best of several candidates did.
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct Seeding {
    pub outcome: Outcome,
    /// The candidate it was cloned from (or would have been, had the clone
    /// worked).
    pub from: Option<usize>,
    /// The candidates holding a build it could use, in the order tried.
    pub tried: Vec<usize>,
}

/// `cargo steward seed`.
pub fn run_seed(args: &SeedArgs) -> Result<()> {
    let paths = Paths::resolve().context("cannot locate the home directory")?;
    let config = config::load_warning(&paths);
    let say = |line: String| {
        if !args.quiet {
            println!("{line}");
        }
    };
    if !config.seed {
        say("Seeding is turned off (`seed = false`).".into());
        return Ok(());
    }
    let worktree = match &args.worktree {
        Some(dir) => dir.clone(),
        None => env::current_dir().context("reading the current directory")?,
    };
    let worktree = std::path::absolute(&worktree).unwrap_or(worktree);

    let registry = ledger::peek(&paths);
    let Some(job) = plan(args, &worktree, &registry) else {
        say(format!(
            "Nothing to seed: no cargo workspace found at {}",
            worktree.display()
        ));
        return Ok(());
    };
    let seeding = seed_best(&job.mine.build, &job.candidates, EACH_WAIT, LOCK_WAIT)?;
    say(describe(&seeding, &job.mine.build, &job.candidates));
    // With a separate build-dir, final artifacts live in the target dir:
    // start it from the same worktree's.
    if !same_path(&job.mine.build, &job.mine.target)
        && matches!(seeding.outcome, Outcome::Seeded(_))
        && let Some(i) = seeding.from
        && let Some(theirs) = &job.candidates[i].target
    {
        let from = Candidate {
            dir: theirs.clone(),
            target: None,
            ..job.candidates[i].clone()
        };
        let again = seed_best(
            &job.mine.target,
            std::slice::from_ref(&from),
            LOCK_WAIT,
            LOCK_WAIT,
        )?;
        say(describe(&again, &job.mine.target, &[from]));
    }
    Ok(())
}

/// The line `seed` prints for seeding `target` from one of `candidates`.
fn describe(seeding: &Seeding, target: &Path, candidates: &[Candidate]) -> String {
    let t = target.display();
    let dirs = |which: &[usize]| {
        let names: Vec<String> = which
            .iter()
            .map(|&i| candidates[i].dir.display().to_string())
            .collect();
        match names.as_slice() {
            [] => String::new(),
            [one] => one.clone(),
            [rest @ .., last] => format!("{} or {last}", rest.join(", ")),
        }
    };
    let all: Vec<usize> = (0..candidates.len()).collect();
    match &seeding.outcome {
        Outcome::Seeded(took) => {
            let Some(from) = seeding.from.map(|i| &candidates[i]) else {
                return format!("Seeded {t}");
            };
            let mut notes = Vec::new();
            let about = from.about();
            if !about.is_empty() {
                notes.push(about);
            }
            let busy: Vec<String> = seeding
                .tried
                .iter()
                .take_while(|&&i| Some(i) != seeding.from)
                .map(|&i| match candidates[i].label.as_str() {
                    "" => candidates[i].dir.display().to_string(),
                    label => label.to_owned(),
                })
                .collect();
            if !busy.is_empty() {
                notes.push(format!("busy: {}", busy.join(", ")));
            }
            notes.push(format!("{:.2} s, copy-on-write", took.as_secs_f64()));
            format!(
                "Seeded {t} from {} ({})",
                from.dir.display(),
                notes.join("; ")
            )
        }
        Outcome::AlreadyBuilt => format!("{t} already holds a build"),
        Outcome::Occupied => format!("Not seeding {t}: it exists and isn't empty"),
        Outcome::NoBaseline if candidates.is_empty() => {
            format!("No build to seed {t} from: the repository has no other worktree")
        }
        Outcome::NoBaseline => format!("No build to seed from at {}", dirs(&all)),
        Outcome::Same => {
            format!("{t} is the build directory it would be seeded from; nothing to seed")
        }
        Outcome::Busy if seeding.tried.len() > 1 => {
            format!("Not seeding {t}: builds are using {}", dirs(&seeding.tried))
        }
        Outcome::Busy => format!("Not seeding {t}: a build is using {}", dirs(&seeding.tried)),
        Outcome::Unsupported => format!(
            "Not seeding {t}: its filesystem can't clone from {}",
            dirs(&seeding.tried)
        ),
    }
}

/// A workspace's directories: where intermediate build output goes (the
/// build directory) and where final artifacts go (the target directory).
/// They're the same unless `build.build-dir` is set.
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct Dirs {
    pub build: PathBuf,
    pub target: PathBuf,
}

impl Dirs {
    pub(crate) fn one(dir: &Path) -> Dirs {
        Dirs {
            build: dir.to_path_buf(),
            target: dir.to_path_buf(),
        }
    }
}

/// What to seed: this worktree's directories, and the candidates to seed
/// its build directory from, ranked.
#[derive(Debug)]
struct Job {
    mine: Dirs,
    candidates: Vec<Candidate>,
}

/// What to seed for `args` in `worktree`. `None` outside a cargo workspace,
/// when there's no directory to seed.
fn plan(args: &SeedArgs, worktree: &Path, registry: &ledger::Registry) -> Option<Job> {
    if !args.from.is_empty() {
        let build = match &args.target_dir {
            Some(dir) => dir.clone(),
            None => cargo_dirs(worktree, None)?.build,
        };
        let candidates = args
            .from
            .iter()
            .enumerate()
            .map(|(i, dir)| Candidate::given(dir, i == 0, registry))
            .collect();
        return Some(Job {
            mine: Dirs::one(&build),
            candidates: baseline::rank(candidates),
        });
    }
    let mine = match &args.target_dir {
        // Cargo reports the build-dir that goes with this target dir.
        Some(target) => match cargo_dirs(worktree, Some(target)) {
            Some(dirs) if !same_path(&dirs.build, target) => Dirs {
                build: dirs.build,
                target: target.clone(),
            },
            _ => Dirs::one(target),
        },
        None => cargo_dirs(worktree, None)?,
    };
    let candidates = baseline::discover(worktree, &mine, registry);
    Some(Job { mine, candidates })
}

/// Whether two paths name the same directory, existing or not.
pub(crate) fn same_path(a: &Path, b: &Path) -> bool {
    a == b || crate::proxy::route::resolve(a) == crate::proxy::route::resolve(b)
}

/// The workspace directories of the cargo project at `dir`, as `cargo
/// metadata` reports them, so the project's config and the environment are
/// respected; with `target_dir`, as they are for a build into it. `None` if
/// `dir` isn't in a cargo project.
pub(crate) fn cargo_dirs(dir: &Path, target_dir: Option<&Path>) -> Option<Dirs> {
    let cargo = env::var_os("CARGO").unwrap_or_else(|| OsString::from("cargo"));
    let mut command = Command::new(cargo);
    command
        .args(["metadata", "--no-deps", "--format-version", "1"])
        .current_dir(dir)
        .stdin(Stdio::null())
        .stderr(Stdio::null());
    if let Some(target) = target_dir {
        command.env("CARGO_TARGET_DIR", target);
    }
    let output = command.output().ok()?;
    if !output.status.success() {
        return None;
    }
    parse_metadata(&output.stdout)
}

fn parse_metadata(json: &[u8]) -> Option<Dirs> {
    #[derive(Deserialize)]
    struct Metadata {
        target_directory: PathBuf,
        /// Reported by Cargo 1.91 and later.
        build_directory: Option<PathBuf>,
    }
    let meta: Metadata = serde_json::from_slice(json).ok()?;
    Some(Dirs {
        build: meta
            .build_directory
            .unwrap_or_else(|| meta.target_directory.clone()),
        target: meta.target_directory,
    })
}

/// Variables with which git picks a repository other than the one its
/// working directory is in. Git sets them for the hooks and `rebase --exec`
/// commands it runs, so anything those start would otherwise act on the
/// repository that ran them.
pub(crate) const GIT_REPO_VARS: [&str; 8] = [
    "GIT_DIR",
    "GIT_WORK_TREE",
    "GIT_COMMON_DIR",
    "GIT_INDEX_FILE",
    "GIT_PREFIX",
    "GIT_OBJECT_DIRECTORY",
    "GIT_ALTERNATE_OBJECT_DIRECTORIES",
    "GIT_NAMESPACE",
];

/// `git`, finding its repository from the directory it runs in alone
/// ([`GIT_REPO_VARS`] removed).
pub(crate) fn git_command() -> Command {
    let mut command = Command::new("git");
    for var in GIT_REPO_VARS {
        command.env_remove(var);
    }
    command
}

/// Runs git in `dir` and returns its output without the final newline, or
/// `None` if it fails or prints nothing. Variables a hook inherits from git
/// are cleared, so `dir` alone decides which repository this is.
pub(crate) fn git(dir: &Path, args: &[&str]) -> Option<String> {
    let output = git_command()
        .arg("-C")
        .arg(dir)
        .args(args)
        .stdin(Stdio::null())
        .stderr(Stdio::null())
        .output()
        .ok()?;
    if !output.status.success() {
        return None;
    }
    let text = String::from_utf8(output.stdout).ok()?;
    let text = text.trim_end_matches(['\n', '\r']);
    (!text.is_empty()).then(|| text.to_owned())
}

/// Starts `target` as a clone of the build directory `baseline`, waiting
/// up to `wait` for a build using it to finish.
#[cfg(test)]
pub(crate) fn seed_dir(target: &Path, baseline: &Path, wait: Duration) -> Result<Outcome> {
    let from = Candidate::given(baseline, true, &ledger::Registry::default());
    Ok(seed_best(target, &[from], wait, wait)?.outcome)
}

/// Starts `target` as a clone of the first of the ranked `candidates` that
/// holds a build no build is using.
///
/// Each candidate is tried in turn, waiting up to `each` for a build using
/// it to finish; if builds are using them all, they're tried again in turn
/// until `total` has passed. The clone is made under a temporary name next
/// to `target` while holding cargo's locks on every profile of the
/// candidate, then renamed into place, so no build ever sees a half made
/// directory. A pin is not inherited.
pub(crate) fn seed_best(
    target: &Path,
    candidates: &[Candidate],
    each: Duration,
    total: Duration,
) -> Result<Seeding> {
    let done = |outcome, from, tried| {
        Ok(Seeding {
            outcome,
            from,
            tried,
        })
    };
    let target = std::path::absolute(target).unwrap_or_else(|_| target.to_path_buf());
    let resolved = target.canonicalize().unwrap_or_else(|_| target.clone());
    let mut usable: Vec<(usize, PathBuf)> = Vec::new();
    let mut same = false;
    for (i, candidate) in candidates.iter().enumerate() {
        let Ok(dir) = candidate.dir.canonicalize() else {
            continue;
        };
        if !holds_build(&dir) {
            continue;
        }
        if resolved.starts_with(&dir) || dir.starts_with(&resolved) {
            same = true;
            continue;
        }
        usable.push((i, dir));
    }
    if usable.is_empty() {
        let outcome = if same {
            Outcome::Same
        } else {
            Outcome::NoBaseline
        };
        return done(outcome, None, Vec::new());
    }
    let tried = |usable: &[(usize, PathBuf)]| usable.iter().map(|(i, _)| *i).collect();
    if holds_build(&target) {
        return done(Outcome::AlreadyBuilt, None, tried(&usable));
    }
    if !is_empty_or_missing(&target) {
        return done(Outcome::Occupied, None, tried(&usable));
    }
    let (Some(parent), Some(name)) = (target.parent(), target.file_name()) else {
        return done(Outcome::Same, None, tried(&usable));
    };
    fs::create_dir_all(parent).with_context(|| format!("creating {}", parent.display()))?;
    let device = fsutil::device(parent);
    let all = tried(&usable);
    usable.retain(|(_, dir)| fsutil::device(dir) == device);
    if usable.is_empty() {
        return done(Outcome::Unsupported, None, all);
    }

    let dirs: Vec<&Path> = usable.iter().map(|(_, d)| d.as_path()).collect();
    let tried = tried(&usable);
    let Some((k, locks)) = lock_first(&dirs, each, total) else {
        return done(Outcome::Busy, None, tried);
    };
    let (from, baseline) = (usable[k].0, &usable[k].1);
    let started = Instant::now();
    let staging = parent.join(format!(
        ".{}.steward-seed-{}",
        name.to_string_lossy(),
        std::process::id()
    ));
    let _ = fsutil::remove(&staging);
    let cloned = clone::clone_tree(baseline, &staging);
    drop(locks);
    match cloned {
        Ok(Cloned::Done) => {}
        Ok(Cloned::Unsupported) => {
            let _ = fsutil::remove(&staging);
            return done(Outcome::Unsupported, Some(from), vec![from]);
        }
        Err(e) => {
            let _ = fsutil::remove(&staging);
            return Err(e).with_context(|| {
                format!("cloning {} to {}", baseline.display(), staging.display())
            });
        }
    }
    let _ = fs::remove_file(staging.join(gc::PIN_FILE));
    fsutil::sweep_trash(&staging);

    // Replaces `target` only if it's missing or an empty directory.
    if let Err(e) = fs::rename(&staging, &target) {
        let _ = fsutil::remove(&staging);
        if !is_empty_or_missing(&target) {
            let outcome = if holds_build(&target) {
                Outcome::AlreadyBuilt
            } else {
                Outcome::Occupied
            };
            return done(outcome, None, tried);
        }
        return Err(e).with_context(|| format!("moving the clone to {}", target.display()));
    }
    done(Outcome::Seeded(started.elapsed()), Some(from), tried)
}

/// Whether a build has started in `dir`: it's tagged as cargo's, or holds
/// build profiles (a directory made before cargo ran, as the proxy makes
/// target directories, has no tag).
pub(crate) fn holds_build(dir: &Path) -> bool {
    scan::is_cargo_root(dir) || !scan::profiles(dir).is_empty()
}

fn is_empty_or_missing(dir: &Path) -> bool {
    match fs::read_dir(dir) {
        Ok(mut entries) => entries.next().is_none(),
        Err(e) => e.kind() == io::ErrorKind::NotFound,
    }
}

/// Cargo's locks on every profile of the first of the build directories
/// `dirs` that no build is using, and which one that is. Each is tried in
/// turn for up to `each`, then all of them in turn until `total` has passed.
fn lock_first(dirs: &[&Path], each: Duration, total: Duration) -> Option<(usize, Vec<Lock>)> {
    let deadline = Instant::now() + total;
    for (i, dir) in dirs.iter().enumerate() {
        let wait = each.min(deadline.saturating_duration_since(Instant::now()));
        if let Some(locks) = lock_all(dir, wait) {
            return Some((i, locks));
        }
    }
    while Instant::now() < deadline {
        sleep(Duration::from_millis(100));
        for (i, dir) in dirs.iter().enumerate() {
            if let Some(locks) = lock_all(dir, Duration::ZERO) {
                return Some((i, locks));
            }
        }
    }
    None
}

/// Cargo's locks on every profile of the build directory `root`, retrying
/// until `wait` has passed. `None` if a build kept any of them.
fn lock_all(root: &Path, wait: Duration) -> Option<Vec<Lock>> {
    let deadline = Instant::now() + wait;
    loop {
        let mut held = Vec::new();
        let mut busy = false;
        for profile in scan::profiles(root) {
            match fsutil::lock_profile(&profile.path) {
                Lock::Busy => {
                    busy = true;
                    break;
                }
                lock => held.push(lock),
            }
        }
        if !busy {
            return Some(held);
        }
        drop(held);
        if Instant::now() >= deadline {
            return None;
        }
        sleep(Duration::from_millis(100));
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs::{File, FileTimes};
    use std::time::SystemTime;

    /// A pinned build directory with one dependency built a day ago.
    fn build_dir(root: &Path) -> PathBuf {
        let rlib = root.join("debug/deps/libdep-0123456789abcdef.rlib");
        fs::create_dir_all(root.join("debug/.fingerprint/dep-0123456789abcdef")).unwrap();
        fs::create_dir_all(rlib.parent().unwrap()).unwrap();
        fs::write(root.join(".rustc_info.json"), "{}").unwrap();
        fs::write(
            root.join("CACHEDIR.TAG"),
            "Signature: 8a477f597d28d172789f06886806bc55\n# created by cargo.\n",
        )
        .unwrap();
        fs::write(&rlib, vec![1u8; 80_000]).unwrap();
        let old = SystemTime::now() - Duration::from_secs(86_400);
        File::options()
            .write(true)
            .open(&rlib)
            .unwrap()
            .set_times(FileTimes::new().set_modified(old))
            .unwrap();
        fs::write(root.join(gc::PIN_FILE), "keep").unwrap();
        rlib
    }

    #[test]
    fn seeds_a_new_build_dir_from_the_baseline() {
        let tmp = tempfile::tempdir().unwrap();
        let base = tmp.path().join("main/target");
        let rlib = build_dir(&base);
        let target = tmp.path().join("wt/target");
        let outcome = seed_dir(&target, &base, Duration::ZERO).unwrap();
        if outcome == Outcome::Unsupported {
            assert!(!target.exists());
            return;
        }
        assert!(matches!(outcome, Outcome::Seeded(_)), "{outcome:?}");
        let copy = target.join("debug/deps/libdep-0123456789abcdef.rlib");
        assert_eq!(fs::read(&copy).unwrap(), fs::read(&rlib).unwrap());
        assert_eq!(
            fs::metadata(&copy).unwrap().modified().unwrap(),
            fs::metadata(&rlib).unwrap().modified().unwrap()
        );
        assert!(!target.join(gc::PIN_FILE).exists(), "pins aren't inherited");
        assert!(base.join(gc::PIN_FILE).exists());
        // Nothing is left under a temporary name.
        let names: Vec<String> = fsutil::entries(&tmp.path().join("wt"))
            .map(|(n, _)| n)
            .collect();
        assert_eq!(names, ["target"]);

        // Seeding again changes nothing.
        assert_eq!(
            seed_dir(&target, &base, Duration::ZERO).unwrap(),
            Outcome::AlreadyBuilt
        );
    }

    #[test]
    fn leaves_existing_same_and_missing_dirs_alone() {
        let tmp = tempfile::tempdir().unwrap();
        let base = tmp.path().join("main/target");
        build_dir(&base);
        let built = tmp.path().join("built");
        build_dir(&built);
        fs::write(built.join("debug/deps/mine"), "mine").unwrap();
        assert_eq!(
            seed_dir(&built, &base, Duration::ZERO).unwrap(),
            Outcome::AlreadyBuilt
        );
        assert_eq!(fs::read(built.join("debug/deps/mine")).unwrap(), b"mine");

        let occupied = tmp.path().join("occupied");
        fs::create_dir_all(&occupied).unwrap();
        fs::write(occupied.join("notes"), "x").unwrap();
        assert_eq!(
            seed_dir(&occupied, &base, Duration::ZERO).unwrap(),
            Outcome::Occupied
        );

        assert_eq!(
            seed_dir(&base, &base, Duration::ZERO).unwrap(),
            Outcome::Same
        );
        assert_eq!(
            seed_dir(&base.join("debug"), &base, Duration::ZERO).unwrap(),
            Outcome::Same
        );
        let new = tmp.path().join("new");
        assert_eq!(
            seed_dir(&new, &tmp.path().join("nothing"), Duration::ZERO).unwrap(),
            Outcome::NoBaseline
        );
        assert!(!new.exists());
    }

    #[test]
    fn an_empty_target_dir_is_seeded() {
        let tmp = tempfile::tempdir().unwrap();
        let base = tmp.path().join("main/target");
        build_dir(&base);
        let target = tmp.path().join("wt/target");
        fs::create_dir_all(&target).unwrap();
        let outcome = seed_dir(&target, &base, Duration::ZERO).unwrap();
        assert!(
            matches!(outcome, Outcome::Seeded(_) | Outcome::Unsupported),
            "{outcome:?}"
        );
        if matches!(outcome, Outcome::Seeded(_)) {
            assert!(target.join(".rustc_info.json").exists());
        }
    }

    #[test]
    fn skips_a_baseline_a_build_is_using() {
        let tmp = tempfile::tempdir().unwrap();
        let base = tmp.path().join("main/target");
        build_dir(&base);
        let held = fsutil::lock_profile(&base.join("debug"));
        assert!(matches!(held, Lock::Held(_)));
        let target = tmp.path().join("wt/target");
        assert_eq!(
            seed_dir(&target, &base, Duration::from_millis(250)).unwrap(),
            Outcome::Busy
        );
        assert!(!target.exists());
        drop(held);
        // Child processes other tests start can briefly hold a copy of the
        // lock, so wait as the command does.
        let outcome = seed_dir(&target, &base, LOCK_WAIT).unwrap();
        assert!(
            matches!(outcome, Outcome::Seeded(_) | Outcome::Unsupported),
            "{outcome:?}"
        );
    }

    fn worktree_candidate(dir: &Path, label: &str, main: bool, age: u64) -> Candidate {
        Candidate {
            dir: dir.to_path_buf(),
            target: None,
            label: label.into(),
            main,
            last_used: SystemTime::now() - Duration::from_secs(age),
        }
    }

    /// The main checkout's build is busy: the most recently used idle
    /// worktree's is used, without waiting out the whole budget.
    #[test]
    fn a_busy_main_falls_back_to_the_most_recent_idle_worktree() {
        let tmp = tempfile::tempdir().unwrap();
        let main = tmp.path().join("main/target");
        let older = tmp.path().join("older/target");
        let newer = tmp.path().join("newer/target");
        let busy = tmp.path().join("busy/target");
        for dir in [&main, &older, &newer, &busy] {
            build_dir(dir);
        }
        fs::write(newer.join("debug/deps/newer"), "x").unwrap();
        let candidates = baseline::rank(vec![
            worktree_candidate(&older, "worktree older", false, 600),
            worktree_candidate(&busy, "worktree busy", false, 1),
            worktree_candidate(&main, "the main checkout", true, 3600),
            worktree_candidate(&newer, "worktree newer", false, 60),
        ]);
        let order: Vec<&str> = candidates.iter().map(|c| c.label.as_str()).collect();
        assert_eq!(
            order,
            [
                "the main checkout",
                "worktree busy",
                "worktree newer",
                "worktree older"
            ]
        );

        let held_main = fsutil::lock_profile(&main.join("debug"));
        let held_busy = fsutil::lock_profile(&busy.join("debug"));
        let target = tmp.path().join("wt/target");
        let started = Instant::now();
        let seeding = seed_best(
            &target,
            &candidates,
            Duration::from_millis(100),
            Duration::from_secs(5),
        )
        .unwrap();
        assert!(
            started.elapsed() < Duration::from_secs(3),
            "waited too long"
        );
        assert_eq!(seeding.from, Some(2), "{seeding:?}");
        if seeding.outcome == Outcome::Unsupported {
            // This filesystem can't clone: the fallback was the one tried.
            assert_eq!(seeding.tried, [2]);
            return;
        }
        assert_eq!(seeding.tried, [0, 1, 2, 3]);
        assert!(matches!(seeding.outcome, Outcome::Seeded(_)), "{seeding:?}");
        assert_eq!(fs::read(target.join("debug/deps/newer")).unwrap(), b"x");
        let line = describe(&seeding, &target, &candidates);
        assert!(
            line.contains(
                "(worktree newer, last used 1m ago; busy: the main checkout, worktree busy;"
            ),
            "{line}"
        );

        // Once main is idle, it's preferred again.
        drop(held_main);
        let other = tmp.path().join("wt2/target");
        let seeding = seed_best(&other, &candidates, LOCK_WAIT, LOCK_WAIT).unwrap();
        assert_eq!(seeding.from, Some(0), "{seeding:?}");
        drop(held_busy);
    }

    /// Nothing built anywhere, or only the target itself: a cold build.
    #[test]
    fn nothing_usable_means_a_cold_build() {
        let tmp = tempfile::tempdir().unwrap();
        let empty = tmp.path().join("empty/target");
        fs::create_dir_all(&empty).unwrap();
        let notes = tmp.path().join("notes");
        fs::create_dir_all(&notes).unwrap();
        fs::write(notes.join("readme"), "x").unwrap();
        let target = tmp.path().join("wt/target");
        let candidates = [
            worktree_candidate(&tmp.path().join("missing"), "the main checkout", true, 0),
            worktree_candidate(&empty, "worktree empty", false, 0),
            worktree_candidate(&notes, "worktree notes", false, 0),
        ];
        let seeding = seed_best(&target, &candidates, Duration::ZERO, Duration::ZERO).unwrap();
        assert_eq!(seeding.outcome, Outcome::NoBaseline);
        assert!(!target.exists());
        let seeding = seed_best(&target, &[], Duration::ZERO, Duration::ZERO).unwrap();
        assert_eq!(seeding.outcome, Outcome::NoBaseline);

        // The target is the only build there is.
        build_dir(&target);
        let me = [worktree_candidate(&target, "worktree wt", false, 0)];
        let seeding = seed_best(&target, &me, Duration::ZERO, Duration::ZERO).unwrap();
        assert_eq!(seeding.outcome, Outcome::Same);
    }

    /// A build directory made before cargo ran (as the proxy makes target
    /// directories) has neither of cargo's markers, only its profiles.
    #[test]
    fn a_build_dir_without_cargos_markers_is_a_baseline() {
        let tmp = tempfile::tempdir().unwrap();
        let base = tmp.path().join("routed/main");
        build_dir(&base);
        fs::remove_file(base.join(".rustc_info.json")).unwrap();
        fs::remove_file(base.join("CACHEDIR.TAG")).unwrap();
        assert!(!scan::is_cargo_root(&base));
        let target = tmp.path().join("routed/wt");
        let outcome = seed_dir(&target, &base, Duration::ZERO).unwrap();
        assert!(
            matches!(outcome, Outcome::Seeded(_) | Outcome::Unsupported),
            "{outcome:?}"
        );
    }

    /// Builds are using every candidate: after the wait, a cold build.
    #[test]
    fn every_candidate_busy_means_a_cold_build() {
        let tmp = tempfile::tempdir().unwrap();
        let a = tmp.path().join("a/target");
        let b = tmp.path().join("b/target");
        build_dir(&a);
        build_dir(&b);
        let held = [
            fsutil::lock_profile(&a.join("debug")),
            fsutil::lock_profile(&b.join("debug")),
        ];
        let candidates = [
            worktree_candidate(&a, "the main checkout", true, 0),
            worktree_candidate(&b, "worktree b", false, 0),
        ];
        let target = tmp.path().join("wt/target");
        let started = Instant::now();
        let seeding = seed_best(
            &target,
            &candidates,
            Duration::from_millis(100),
            Duration::from_millis(400),
        )
        .unwrap();
        assert!(started.elapsed() >= Duration::from_millis(400));
        assert_eq!(seeding.outcome, Outcome::Busy);
        assert_eq!(seeding.tried, [0, 1]);
        assert!(!target.exists());
        drop(held);
    }

    #[test]
    fn reads_both_directories_from_metadata() {
        let both = br#"{"packages":[],"target_directory":"/w/target","build_directory":"/b/x"}"#;
        assert_eq!(
            parse_metadata(both),
            Some(Dirs {
                build: "/b/x".into(),
                target: "/w/target".into()
            })
        );
        let old = br#"{"packages":[],"target_directory":"/w/target"}"#;
        assert_eq!(parse_metadata(old), Some(Dirs::one(Path::new("/w/target"))));
        assert_eq!(parse_metadata(b"not json"), None);
    }

    /// Runs git with no user or system config, so tests see plain defaults.
    fn git_ok(dir: &Path, args: &[&str]) {
        let out = git_command()
            .arg("-C")
            .arg(dir)
            .args(args)
            .env("GIT_CONFIG_GLOBAL", "/dev/null")
            .env("GIT_CONFIG_NOSYSTEM", "1")
            .output()
            .unwrap();
        assert!(
            out.status.success(),
            "git {args:?}: {}",
            String::from_utf8_lossy(&out.stderr)
        );
    }

    #[test]
    fn finds_the_main_worktree_of_a_linked_one() {
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path().canonicalize().unwrap();
        let repo = root.join("repo");
        fs::create_dir_all(repo.join("crates/app")).unwrap();
        git_ok(&repo, &["init", "-q", "-b", "main"]);
        fs::write(repo.join("crates/app/file"), "x").unwrap();
        git_ok(&repo, &["add", "."]);
        git_ok(
            &repo,
            &[
                "-c",
                "user.name=t",
                "-c",
                "user.email=t@t",
                "commit",
                "-qm",
                "init",
            ],
        );
        let wt = root.join("wt");
        git_ok(&repo, &["worktree", "add", "-q", wt.to_str().unwrap()]);
        let main = |dir: &Path| {
            baseline::worktrees(dir)
                .into_iter()
                .find(|w| w.main)
                .map(|w| w.path)
        };
        assert_eq!(main(&wt), Some(repo.clone()));
        assert_eq!(main(&wt.join("crates/app")), Some(repo.clone()));
        assert_eq!(main(&repo), Some(repo.clone()));
        assert_eq!(baseline::worktrees(&wt).len(), 2);
    }

    #[test]
    fn describes_every_outcome() {
        let t = Path::new("/w/target");
        let given = [Candidate::given(
            Path::new("/m/target"),
            true,
            &ledger::Registry::default(),
        )];
        let cases = [
            (
                Outcome::Seeded(Duration::from_millis(1250)),
                "Seeded /w/target from /m/target (1.25 s, copy-on-write)",
            ),
            (Outcome::AlreadyBuilt, "/w/target already holds a build"),
            (
                Outcome::Occupied,
                "Not seeding /w/target: it exists and isn't empty",
            ),
            (Outcome::NoBaseline, "No build to seed from at /m/target"),
            (
                Outcome::Same,
                "/w/target is the build directory it would be seeded from; nothing to seed",
            ),
            (
                Outcome::Busy,
                "Not seeding /w/target: a build is using /m/target",
            ),
            (
                Outcome::Unsupported,
                "Not seeding /w/target: its filesystem can't clone from /m/target",
            ),
        ];
        for (outcome, line) in cases {
            let seeding = Seeding {
                outcome,
                from: Some(0),
                tried: vec![0],
            };
            assert_eq!(describe(&seeding, t, &given), line);
        }

        // Several candidates: which one, whose, and which were busy.
        let mut worktrees = vec![
            Candidate {
                label: "the main checkout".into(),
                ..given[0].clone()
            },
            Candidate {
                dir: "/a/target".into(),
                label: "worktree a".into(),
                main: false,
                ..given[0].clone()
            },
            Candidate {
                dir: "/b/target".into(),
                label: "worktree b".into(),
                main: false,
                ..given[0].clone()
            },
        ];
        let seeded = Seeding {
            outcome: Outcome::Seeded(Duration::from_millis(500)),
            from: Some(1),
            tried: vec![0, 1, 2],
        };
        assert_eq!(
            describe(&seeded, t, &worktrees),
            "Seeded /w/target from /a/target (worktree a; busy: the main checkout; 0.50 s, copy-on-write)"
        );
        let busy = Seeding {
            outcome: Outcome::Busy,
            from: None,
            tried: vec![0, 1, 2],
        };
        assert_eq!(
            describe(&busy, t, &worktrees),
            "Not seeding /w/target: builds are using /m/target, /a/target or /b/target"
        );
        let none = Seeding {
            outcome: Outcome::NoBaseline,
            from: None,
            tried: vec![],
        };
        assert_eq!(
            describe(&none, t, &worktrees[1..]),
            "No build to seed from at /a/target or /b/target"
        );
        worktrees.clear();
        assert_eq!(
            describe(&none, t, &worktrees),
            "No build to seed /w/target from: the repository has no other worktree"
        );
    }

    #[test]
    fn plans_explicit_dirs_and_nothing_without_a_baseline() {
        let tmp = tempfile::tempdir().unwrap();
        let registry = ledger::Registry::default();
        let args = SeedArgs {
            target_dir: Some("/w/target".into()),
            from: vec!["/m/target".into(), "/n/target".into()],
            ..SeedArgs::default()
        };
        let job = plan(&args, tmp.path(), &registry).unwrap();
        assert_eq!(job.mine, Dirs::one(Path::new("/w/target")));
        let from: Vec<&Path> = job.candidates.iter().map(|c| c.dir.as_path()).collect();
        assert_eq!(from, [Path::new("/m/target"), Path::new("/n/target")]);
        assert!(job.candidates[0].main && !job.candidates[1].main);
        // Outside git there's no other worktree to clone from.
        let args = SeedArgs {
            target_dir: Some("/w/target".into()),
            ..SeedArgs::default()
        };
        let job = plan(&args, tmp.path(), &registry).unwrap();
        assert!(job.candidates.is_empty());
        assert!(baseline::worktrees(tmp.path()).is_empty());
        // Nor anything to seed outside a cargo workspace.
        assert!(plan(&SeedArgs::default(), tmp.path(), &registry).is_none());
    }

    #[test]
    fn handles_repositories_whose_git_dir_is_elsewhere() {
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path().canonicalize().unwrap();
        let repo = root.join("repo");
        fs::create_dir_all(repo.join("sub")).unwrap();
        let gitdir = root.join("gitdir");
        git_ok(
            &repo,
            &[
                "init",
                "-q",
                "-b",
                "main",
                "--separate-git-dir",
                gitdir.to_str().unwrap(),
            ],
        );
        // Git doesn't record where this repository's worktree is, and lists
        // the git directory in its place: that's no worktree to seed from.
        assert!(baseline::worktrees(&repo.join("sub")).is_empty());

        // Nor from a linked worktree of it.
        git_ok(
            &repo,
            &[
                "-c",
                "user.name=t",
                "-c",
                "user.email=t@t",
                "commit",
                "-q",
                "--allow-empty",
                "-m",
                "init",
            ],
        );
        let wt = root.join("wt");
        git_ok(&repo, &["worktree", "add", "-q", wt.to_str().unwrap()]);
        let found = baseline::worktrees(&wt);
        assert!(found.iter().all(|w| !w.main), "{found:?}");
        assert_eq!(found.len(), 1);

        // A bare repository has no main worktree.
        let bare = root.join("bare.git");
        fs::create_dir_all(&bare).unwrap();
        git_ok(&bare, &["init", "-q", "--bare"]);
        assert!(baseline::worktrees(&bare).is_empty());
    }

    #[test]
    fn plans_nothing_to_seed_from_in_a_lone_checkout() {
        let tmp = tempfile::tempdir().unwrap();
        let repo = tmp.path().canonicalize().unwrap().join("repo");
        fs::create_dir_all(&repo).unwrap();
        git_ok(&repo, &["init", "-q", "-b", "main"]);
        let args = SeedArgs {
            target_dir: Some(repo.join("target")),
            ..SeedArgs::default()
        };
        let job = plan(&args, &repo, &ledger::Registry::default()).unwrap();
        assert!(job.candidates.is_empty(), "{job:?}");
    }

    #[test]
    fn a_baseline_that_isnt_a_build_dir_is_no_baseline() {
        let tmp = tempfile::tempdir().unwrap();
        let base = tmp.path().join("notes");
        fs::create_dir_all(&base).unwrap();
        fs::write(base.join("readme"), "x").unwrap();
        let target = tmp.path().join("wt/target");
        assert_eq!(
            seed_dir(&target, &base, Duration::ZERO).unwrap(),
            Outcome::NoBaseline
        );
        assert!(!target.exists());
    }

    #[test]
    fn a_target_without_a_name_is_left_alone() {
        let tmp = tempfile::tempdir().unwrap();
        let base = tmp.path().join("main/target");
        build_dir(&base);
        let other = tmp.path().join("other");
        fs::create_dir_all(&other).unwrap();
        // `other/missing/..` names no directory of its own to create.
        let target = other.join("missing/..");
        assert_eq!(
            seed_dir(&target, &base, Duration::ZERO).unwrap(),
            Outcome::Same
        );
        assert!(!other.join("missing").exists());
    }

    /// A target on another volume can't share the baseline's blocks.
    #[cfg(unix)]
    #[test]
    fn a_target_on_another_volume_is_unsupported() {
        let tmp = tempfile::tempdir().unwrap();
        let base = tmp.path().join("main/target");
        build_dir(&base);
        // Never created: the device check comes first.
        let target = Path::new("/dev/cargo-steward-seed-test");
        if fsutil::device(Path::new("/dev")) == fsutil::device(&base) {
            return;
        }
        assert_eq!(
            seed_dir(target, &base, Duration::ZERO).unwrap(),
            Outcome::Unsupported
        );
        assert!(!target.exists());
    }

    #[cfg(unix)]
    #[test]
    fn a_clone_that_fails_is_an_error_and_leaves_nothing() {
        use std::os::unix::fs::PermissionsExt;
        let tmp = tempfile::tempdir().unwrap();
        let base = tmp.path().join("main/target");
        build_dir(&base);
        let parent = tmp.path().join("readonly");
        fs::create_dir_all(&parent).unwrap();
        fs::set_permissions(&parent, fs::Permissions::from_mode(0o555)).unwrap();
        let result = seed_dir(&parent.join("target"), &base, Duration::ZERO);
        let names: Vec<String> = fsutil::entries(&parent).map(|(n, _)| n).collect();
        fs::set_permissions(&parent, fs::Permissions::from_mode(0o755)).unwrap();
        match result {
            Ok(outcome) => assert_eq!(outcome, Outcome::Unsupported),
            Err(e) => assert!(format!("{e:#}").contains("cloning"), "{e:#}"),
        }
        assert!(names.is_empty(), "{names:?}");
    }

    /// A dangling symlink looks missing, but the clone can't replace it.
    #[cfg(unix)]
    #[test]
    fn a_clone_that_cant_be_moved_into_place_is_an_error() {
        let tmp = tempfile::tempdir().unwrap();
        let base = tmp.path().join("main/target");
        build_dir(&base);
        let wt = tmp.path().join("wt");
        fs::create_dir_all(&wt).unwrap();
        let target = wt.join("target");
        std::os::unix::fs::symlink(tmp.path().join("nowhere"), &target).unwrap();
        match seed_dir(&target, &base, Duration::ZERO) {
            Ok(outcome) => assert_eq!(outcome, Outcome::Unsupported),
            Err(e) => assert!(format!("{e:#}").contains("moving the clone"), "{e:#}"),
        }
        // The staged clone is gone; the symlink is untouched.
        let names: Vec<String> = fsutil::entries(&wt).map(|(n, _)| n).collect();
        assert_eq!(names, ["target"]);
        assert!(fs::symlink_metadata(&target).unwrap().is_symlink());
    }

    /// With a separate build-dir, both it and the target dir are seeded,
    /// each from the main worktree's.
    #[test]
    fn plans_the_build_and_target_dirs_of_a_linked_worktree() {
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path().canonicalize().unwrap();
        let repo = root.join("repo");
        fs::create_dir_all(repo.join("src")).unwrap();
        fs::create_dir_all(repo.join(".cargo")).unwrap();
        fs::write(
            repo.join("Cargo.toml"),
            "[package]\nname = \"x\"\nversion = \"0.1.0\"\nedition = \"2021\"\n",
        )
        .unwrap();
        fs::write(repo.join("src/lib.rs"), "").unwrap();
        fs::write(
            repo.join(".cargo/config.toml"),
            "[build]\nbuild-dir = \"bdir\"\n",
        )
        .unwrap();
        git_ok(&repo, &["init", "-q", "-b", "main"]);
        git_ok(&repo, &["add", "."]);
        git_ok(
            &repo,
            &[
                "-c",
                "user.name=t",
                "-c",
                "user.email=t@t",
                "commit",
                "-qm",
                "init",
            ],
        );
        let wt = root.join("wt");
        git_ok(&repo, &["worktree", "add", "-q", wt.to_str().unwrap()]);
        let dirs = cargo_dirs(&wt, None).expect("cargo metadata in the worktree");
        if dirs.build == dirs.target {
            // A Cargo too old to report build_directory.
            return;
        }
        let registry = ledger::Registry::default();
        let job = plan(&SeedArgs::default(), &wt, &registry).unwrap();
        assert_eq!(job.mine.build, wt.join("bdir"));
        assert_eq!(job.candidates.len(), 1, "{job:?}");
        assert_eq!(job.candidates[0].dir, repo.join("bdir"));
        assert!(job.candidates[0].main);
        // The target dirs, unless CARGO_TARGET_DIR puts both elsewhere.
        assert_eq!(job.mine.target, dirs.target);
        if dirs.target.starts_with(&wt) {
            assert_eq!(job.candidates[0].target, Some(repo.join("target")));
        }

        // A target dir given on the command line comes with the build-dir
        // cargo uses with it.
        let args = SeedArgs {
            target_dir: Some(root.join("routed/wt")),
            ..SeedArgs::default()
        };
        let job = plan(&args, &wt, &registry).unwrap();
        assert_eq!(job.mine.build, wt.join("bdir"));
        assert_eq!(job.mine.target, root.join("routed/wt"));
    }

    #[test]
    fn git_finds_its_repository_from_its_directory_alone() {
        // As in a hook or `git rebase --exec`, where git exports where its
        // own repository is.
        let command = git_command();
        let removed: Vec<&std::ffi::OsStr> = command
            .get_envs()
            .filter(|(_, value)| value.is_none())
            .map(|(name, _)| name)
            .collect();
        for var in GIT_REPO_VARS {
            assert!(removed.contains(&std::ffi::OsStr::new(var)), "{var}");
        }
        // And it works: in a fresh repository, git sees that one.
        let tmp = tempfile::tempdir().unwrap();
        let repo = tmp.path().canonicalize().unwrap();
        git_ok(&repo, &["init", "-q"]);
        assert_eq!(
            git(&repo, &["rev-parse", "--show-toplevel"]).map(PathBuf::from),
            Some(repo)
        );
    }
}