gitpane 0.13.0

Multi-repo Git workspace dashboard TUI
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
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
use crate::config::Config;
use std::collections::HashSet;
use std::path::{Path, PathBuf};
use walkdir::WalkDir;

/// Cheap check that a `.git` entry looks real. A bare `mkdir .git` (which
/// can happen by accident, e.g. an aborted clone) is enough to fool a
/// `.git.exists()` test but `Repository::open` then fails on every status
/// query, so we treat such paths as not-a-repo at discovery time. Anything
/// produced by `git init` will have a `HEAD` file; that is what we look for.
///
/// Three layouts are accepted:
/// - A standard checkout, where `.git` is a directory containing `HEAD`.
/// - A *symlink* to such a git directory. This is exactly what Google's
///   `repo` tool writes for every project working copy (`.git ->`
///   `.repo/projects/<name>.git`), and `Path::join("HEAD").is_file()`
///   resolves the symlink transparently.
/// - A submodule or linked worktree, where `.git` is a *file* of the form
///   `gitdir: <path>` pointing at the real git directory (e.g.
///   `<superproject>/.git/modules/<name>`). Without this branch, pinning a
///   submodule would pass `AddRepo`'s `.git.exists()` check but then vanish on
///   the next FS-driven rescan, because discovery couldn't find its `HEAD`.
///
/// Only the first layout is reachable from the root-dir tree walk. The other
/// two describe paths the user (or `repo sync`) named explicitly — a pinned
/// submodule, a pinned worktree, a `.repo/project.list` entry — and the walk
/// deliberately ignores them; see the `is_dir` check in [`discover_repos`].
/// Whether `path` is a repo gitpane can track: a working copy (`path/.git` is
/// a real git dir, symlink, or `gitdir:` pointer) or a bare repository
/// (`path` itself is the git dir, `HEAD` at top level). `AddRepo` validation
/// and pinned-repo discovery share this predicate — if they diverge, a repo
/// the user can add silently vanishes on the next rescan or restart.
pub(crate) fn is_repo_root(path: &Path) -> bool {
    is_real_git_dir(&path.join(".git")) || is_real_git_dir(path)
}

fn is_real_git_dir(dot_git: &Path) -> bool {
    if dot_git.join("HEAD").is_file() {
        return true;
    }
    if dot_git.is_file()
        && let Some(gitdir) = read_gitdir_pointer(dot_git)
    {
        return gitdir.join("HEAD").is_file();
    }
    false
}

/// Whether a candidate repo path matches an `excluded_repos` pattern.
/// Patterns match the repo's directory name or any path component.
fn is_excluded(repo_path: &Path, config: &Config) -> bool {
    let repo_name = repo_path
        .file_name()
        .map(|n| n.to_string_lossy().to_string())
        .unwrap_or_default();
    let path_str = repo_path.to_string_lossy();
    config
        .excluded_repos
        .iter()
        .any(|pattern| repo_name == *pattern || path_str.contains(pattern))
}

/// Google `repo` (git-repo) managed workspace discovery.
///
/// In such a workspace every project's working copy carries a `.git` that is
/// a *symlink* (or `gitdir:` file) pointing into `.repo/projects/`, so the
/// tree walk never sees them as directories. The authoritative list of
/// managed projects is `.repo/project.list` — one relative path per line —
/// which we enumerate instead. This is exact (matches what `repo sync`
/// manages), scales to hundreds of projects, and automatically picks up
/// projects added or removed by later `repo sync` runs because the file is
/// re-read on every discovery pass. Workspaces not managed by `repo` (no
/// `.repo/project.list`) are left untouched.
fn discover_repo_workspace_projects(
    root: &Path,
    config: &Config,
    seen: &mut HashSet<PathBuf>,
    out: &mut Vec<PathBuf>,
) {
    let contents = match std::fs::read_to_string(root.join(".repo").join("project.list")) {
        Ok(c) => c,
        Err(_) => return, // not a repo-managed workspace (or not synced yet)
    };
    // Containment is decided in canonical form. `project.list` is generated
    // from a manifest the workspace fetches from a remote, so its entries are
    // untrusted: `root.join("../../etc/x")` keeps the `..` components
    // verbatim, so a lexical `starts_with(root)` says yes while the path
    // actually resolves outside the workspace.
    let Ok(canonical_root) = root.canonicalize() else {
        return;
    };
    for line in contents.lines() {
        let rel = line.trim();
        if rel.is_empty() {
            continue;
        }
        // Canonicalizing also makes a symlinked root (e.g. `~/work` ->
        // `/real/path`) produce the same string as the tree walk's paths.
        // Without that, the same top-level repo would be added twice — once
        // by the walk, once here — and `seen` couldn't tell them apart.
        // A project whose working tree hasn't been synced yet doesn't
        // resolve at all, and is skipped along with the escapees.
        let Ok(canonical) = root.join(rel).canonicalize() else {
            continue;
        };
        if !canonical.starts_with(&canonical_root) {
            continue;
        }
        // The working copy's `.git` may be a symlink or a `gitdir:` file;
        // is_real_git_dir handles both and rejects empty/phantom dirs.
        if is_real_git_dir(&canonical.join(".git"))
            && !is_excluded(&canonical, config)
            && seen.insert(canonical.clone())
        {
            out.push(canonical);
        }
    }
}

/// Resolve a `gitdir: <path>` pointer file (used by submodules and linked
/// worktrees) to the git directory it references. Relative targets resolve
/// against the directory holding the pointer file.
fn read_gitdir_pointer(dot_git_file: &Path) -> Option<PathBuf> {
    let contents = std::fs::read_to_string(dot_git_file).ok()?;
    let target = Path::new(contents.trim().strip_prefix("gitdir:")?.trim());
    if target.is_absolute() {
        Some(target.to_path_buf())
    } else {
        Some(dot_git_file.parent()?.join(target))
    }
}

pub(crate) fn discover_repos(config: &Config) -> Vec<PathBuf> {
    let mut seen = HashSet::new();
    let mut repos = Vec::new();

    // Pinned repos first
    for pinned in &config.pinned_repos {
        let canonical = pinned.canonicalize().unwrap_or_else(|_| pinned.clone());
        if is_repo_root(&canonical) && seen.insert(canonical.clone()) {
            repos.push(canonical);
        }
    }

    // Discover from root dirs
    for root in config.effective_root_dirs().iter() {
        if !root.exists() {
            continue;
        }
        for entry in WalkDir::new(root)
            .max_depth(config.scan_depth)
            .follow_links(false)
            .into_iter()
            // Don't descend into the repo tool's own metadata (`<root>/.repo`).
            // Its gitdirs under `.repo/projects/` are not `.git` entries, but
            // `.repo/manifests/.git` is a symlink that would otherwise match.
            .filter_entry(|e| e.file_name() != ".repo")
            .filter_map(|e| e.ok())
        {
            // The walk only ever promotes a real `.git` *directory*. A `.git`
            // symlink or `gitdir:` pointer file belongs to something that
            // already has an owner: a linked worktree (which gitpane creates
            // as a sibling of its repo, so it would appear both here and
            // nested under its parent), a submodule (which the parent repo
            // renders inline), or a repo-workspace project. Those reach the
            // list through `pinned_repos` or `.repo/project.list` instead.
            if entry.file_name() == ".git"
                && entry.file_type().is_dir()
                && is_real_git_dir(entry.path())
            {
                let repo_path = entry
                    .path()
                    .parent()
                    .unwrap()
                    .canonicalize()
                    .unwrap_or_else(|_| entry.path().parent().unwrap().to_path_buf());

                if !is_excluded(&repo_path, config) && seen.insert(repo_path.clone()) {
                    repos.push(repo_path);
                }
            }
        }

        // Google `repo` (git-repo) managed workspace: enumerate
        // `.repo/project.list` for the projects the tree walk cannot see.
        discover_repo_workspace_projects(root, config, &mut seen, &mut repos);
    }

    // Pinning controls persistence, not position: `App::sort_repos` owns the
    // final order, so pinned repos sort like every other row here.
    repos.sort_by(|a, b| {
        a.file_name()
            .unwrap_or_default()
            .to_ascii_lowercase()
            .cmp(&b.file_name().unwrap_or_default().to_ascii_lowercase())
    });

    repos
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::TempDir;

    fn make_repo(parent: &std::path::Path, name: &str) -> PathBuf {
        let repo_dir = parent.join(name);
        let dot_git = repo_dir.join(".git");
        fs::create_dir_all(&dot_git).unwrap();
        // Mimic `git init`: a HEAD file is the minimum is_real_git_dir checks.
        fs::write(dot_git.join("HEAD"), "ref: refs/heads/main\n").unwrap();
        repo_dir
    }

    /// A git submodule (or linked worktree): `.git` is a *file* containing a
    /// `gitdir:` pointer to the real git directory, not a directory of its own.
    /// `make_repo` can't model this because it always creates a `.git` dir.
    fn make_submodule(
        parent: &std::path::Path,
        name: &str,
        super_git: &std::path::Path,
    ) -> PathBuf {
        let repo_dir = parent.join(name);
        fs::create_dir_all(&repo_dir).unwrap();
        // The real git dir lives under the superproject, e.g.
        // `<super>/.git/modules/<name>`, and carries the HEAD file.
        let module_git = super_git.join("modules").join(name);
        fs::create_dir_all(&module_git).unwrap();
        fs::write(module_git.join("HEAD"), "ref: refs/heads/main\n").unwrap();
        // The working tree's `.git` is a pointer file with an absolute target.
        fs::write(
            repo_dir.join(".git"),
            format!("gitdir: {}\n", module_git.display()),
        )
        .unwrap();
        repo_dir
    }

    /// Empty `.git` directory with no HEAD file — what an aborted clone or a
    /// stray `mkdir .git` looks like. Discovery must NOT pick this up because
    /// `Repository::open` will fail on every status query, which the watcher
    /// then loops on, producing infinite red error toasts in the status bar.
    fn make_phantom_git_dir(parent: &std::path::Path, name: &str) -> PathBuf {
        let repo_dir = parent.join(name);
        fs::create_dir_all(repo_dir.join(".git")).unwrap();
        repo_dir
    }

    #[test]
    fn test_discover_finds_git_repos() {
        let tmp = TempDir::new().unwrap();
        make_repo(tmp.path(), "alpha");
        make_repo(tmp.path(), "beta");

        let config = Config {
            root_dirs: vec![tmp.path().to_path_buf()],
            scan_depth: 2,
            ..Config::default()
        };

        let repos = discover_repos(&config);
        assert_eq!(repos.len(), 2);
    }

    #[test]
    fn cli_root_override_replaces_scan_root_in_discovery() {
        // The user-visible contract of --cwd/--root: discovery scans the
        // override root and only the override root, not the configured ones.
        let tmp = TempDir::new().unwrap();
        let configured = tmp.path().join("configured");
        let elsewhere = tmp.path().join("elsewhere");
        make_repo(&configured, "in-configured");
        make_repo(&elsewhere, "in-override");

        let mut config = Config {
            root_dirs: vec![configured.clone()],
            scan_depth: 2,
            ..Config::default()
        };
        config.override_root(elsewhere.clone());

        let repos = discover_repos(&config);
        let names: Vec<String> = repos
            .iter()
            .filter_map(|p| p.file_name().map(|n| n.to_string_lossy().to_string()))
            .collect();
        assert_eq!(
            names,
            ["in-override"],
            "override must replace the scan root"
        );
    }

    #[test]
    fn test_excluded_repos_are_filtered() {
        let tmp = TempDir::new().unwrap();
        make_repo(tmp.path(), "good-repo");
        make_repo(tmp.path(), "node_modules");

        let config = Config {
            root_dirs: vec![tmp.path().to_path_buf()],
            excluded_repos: vec!["node_modules".into()],
            scan_depth: 2,
            ..Config::default()
        };

        let repos = discover_repos(&config);
        assert_eq!(repos.len(), 1);
        assert!(repos[0].ends_with("good-repo"));
    }

    #[test]
    fn test_discover_skips_phantom_dot_git_at_root() {
        // Reproduces the gcloud-h100 case: `~/Code/.git/` exists as an empty
        // dir (no HEAD), and `~/Code` is the configured root_dir. Without
        // the HEAD check, discover_repos would emit `~/Code` itself as a
        // repo, then every file change anywhere under `~/Code` would route
        // to it via the watcher's classifier, `Repository::open` would fail,
        // and we'd surface a Failed-to-query toast per event.
        let tmp = TempDir::new().unwrap();
        make_phantom_git_dir(tmp.path(), ""); // creates tmp/.git (empty)
        make_repo(tmp.path(), "real-repo");

        let config = Config {
            root_dirs: vec![tmp.path().to_path_buf()],
            scan_depth: 2,
            ..Config::default()
        };

        let repos = discover_repos(&config);
        assert_eq!(repos.len(), 1, "got {repos:?}");
        assert!(repos[0].ends_with("real-repo"));
    }

    #[test]
    fn test_discover_skips_phantom_dot_git_in_child() {
        let tmp = TempDir::new().unwrap();
        make_phantom_git_dir(tmp.path(), "broken");
        make_repo(tmp.path(), "ok");

        let config = Config {
            root_dirs: vec![tmp.path().to_path_buf()],
            scan_depth: 2,
            ..Config::default()
        };

        let repos = discover_repos(&config);
        assert_eq!(repos.len(), 1, "got {repos:?}");
        assert!(repos[0].ends_with("ok"));
    }

    /// Regression: `AddRepo` accepts a bare repository (`HEAD` at top level,
    /// no `.git`), so pinned discovery must too — previously it only checked
    /// `<path>/.git`, and a pinned bare repo vanished on the next rescan or
    /// restart.
    #[test]
    fn test_pinned_bare_repo_is_discovered() {
        let tmp = TempDir::new().unwrap();
        let bare = tmp.path().join("mirror.git");
        fs::create_dir_all(&bare).unwrap();
        fs::write(bare.join("HEAD"), "ref: refs/heads/main\n").unwrap();

        let config = Config {
            root_dirs: vec![],
            pinned_repos: vec![bare.clone()],
            scan_depth: 2,
            ..Config::default()
        };

        let repos = discover_repos(&config);
        assert_eq!(repos.len(), 1, "got {repos:?}");
        assert!(repos[0].ends_with("mirror.git"));
    }

    #[test]
    fn test_is_repo_root_matches_addable_shapes() {
        let tmp = TempDir::new().unwrap();
        let working = make_repo(tmp.path(), "working");
        assert!(is_repo_root(&working));

        let bare = tmp.path().join("bare.git");
        fs::create_dir_all(&bare).unwrap();
        fs::write(bare.join("HEAD"), "ref: refs/heads/main\n").unwrap();
        assert!(is_repo_root(&bare));

        // An empty `.git` dir is not a repo — reject at add time instead of
        // accepting it and dropping it on the next rescan.
        let phantom = make_phantom_git_dir(tmp.path(), "phantom");
        assert!(!is_repo_root(&phantom));
        assert!(!is_repo_root(&tmp.path().join("missing")));
    }

    #[test]
    fn test_pinned_phantom_repo_is_skipped() {
        let tmp = TempDir::new().unwrap();
        let phantom = make_phantom_git_dir(tmp.path(), "phantom");
        let real = make_repo(tmp.path(), "real");

        let config = Config {
            root_dirs: vec![],
            pinned_repos: vec![phantom, real.clone()],
            scan_depth: 2,
            ..Config::default()
        };

        let repos = discover_repos(&config);
        assert_eq!(repos.len(), 1, "got {repos:?}");
        assert!(repos[0].ends_with("real"));
    }

    /// Regression: pinning a submodule (whose `.git` is a pointer file, not a
    /// directory) must survive discovery. Previously `is_real_git_dir` only
    /// accepted a `.git` directory with HEAD, so a pinned submodule passed
    /// `AddRepo` but was pruned on the next FS-driven rescan, vanishing from
    /// the list almost immediately after being added.
    #[test]
    fn test_pinned_submodule_is_discovered() {
        let tmp = TempDir::new().unwrap();
        let super_git = tmp.path().join("superproject").join(".git");
        fs::create_dir_all(&super_git).unwrap();
        let submodule = make_submodule(tmp.path(), "vendored-lib", &super_git);

        let config = Config {
            root_dirs: vec![],
            pinned_repos: vec![submodule.clone()],
            scan_depth: 2,
            ..Config::default()
        };

        let repos = discover_repos(&config);
        assert_eq!(repos.len(), 1, "got {repos:?}");
        assert!(repos[0].ends_with("vendored-lib"));
    }

    /// A relative `gitdir:` pointer (the form real `git submodule` writes,
    /// e.g. `gitdir: ../../.git/modules/<name>`) must resolve against the
    /// working tree, not the process CWD.
    #[test]
    fn test_pinned_submodule_relative_gitdir_is_discovered() {
        let tmp = TempDir::new().unwrap();
        // Layout: tmp/super/.git/modules/sub  and  tmp/super/deps/sub
        let super_dir = tmp.path().join("super");
        let module_git = super_dir.join(".git").join("modules").join("sub");
        fs::create_dir_all(&module_git).unwrap();
        fs::write(module_git.join("HEAD"), "ref: refs/heads/main\n").unwrap();
        let work = super_dir.join("deps").join("sub");
        fs::create_dir_all(&work).unwrap();
        // Relative pointer from tmp/super/deps/sub back to the module git dir.
        fs::write(work.join(".git"), "gitdir: ../../.git/modules/sub\n").unwrap();

        let config = Config {
            root_dirs: vec![],
            pinned_repos: vec![work.clone()],
            scan_depth: 2,
            ..Config::default()
        };

        let repos = discover_repos(&config);
        assert_eq!(repos.len(), 1, "got {repos:?}");
        assert!(repos[0].ends_with("sub"));
    }

    /// Regression: a linked worktree that sits *beside* its repo must not
    /// become a top-level row of its own. `worktree_path` puts new worktrees
    /// in the repo's parent directory by default, i.e. directly under the
    /// configured root, and their `.git` is a `gitdir:` pointer file. If the
    /// walk promoted pointer files, every worktree gitpane creates would show
    /// up twice: once as its own repo and once nested under its parent.
    #[test]
    fn test_sibling_worktree_is_not_a_top_level_repo() {
        let tmp = TempDir::new().unwrap();
        let repo = make_repo(tmp.path(), "proj");
        // What `git worktree add ../proj-feature` leaves on disk: an admin
        // dir under the repo, and a pointer file in the new working tree.
        let admin = repo.join(".git").join("worktrees").join("feature");
        fs::create_dir_all(&admin).unwrap();
        fs::write(admin.join("HEAD"), "ref: refs/heads/feature\n").unwrap();
        let worktree = tmp.path().join("proj-feature");
        fs::create_dir_all(&worktree).unwrap();
        fs::write(
            worktree.join(".git"),
            format!("gitdir: {}\n", admin.display()),
        )
        .unwrap();

        let config = Config {
            root_dirs: vec![tmp.path().to_path_buf()],
            scan_depth: 2,
            ..Config::default()
        };

        let repos = discover_repos(&config);
        assert_eq!(repos.len(), 1, "got {repos:?}");
        assert!(repos[0].ends_with("proj"));
    }

    /// Pinning controls persistence, not position: a pinned repo takes its
    /// alphabetical place. Prepending pinned repos here made discovery order
    /// diverge from `App::sort_repos`, and a watcher-driven rescan then
    /// silently snapped the sorted list back to pinned-first.
    #[test]
    fn test_pinned_repos_sort_alphabetically() {
        let tmp = TempDir::new().unwrap();
        let z_repo = make_repo(tmp.path(), "z-repo");
        make_repo(tmp.path(), "a-repo");

        let config = Config {
            root_dirs: vec![tmp.path().to_path_buf()],
            pinned_repos: vec![z_repo.clone()],
            scan_depth: 2,
            ..Config::default()
        };

        let repos = discover_repos(&config);
        assert_eq!(repos.len(), 2);
        assert!(repos[0].ends_with("a-repo"));
        assert!(repos[1].ends_with("z-repo"));
    }

    /// Build a Google `repo`-style workspace fixture under `root`:
    /// - `.repo/project.list` lists `projects` (one relative path per line),
    /// - each project's gitdir lives at `.repo/projects/<name>.git` with a HEAD,
    /// - the working copy at `<root>/<name>` gets its `.git` linked by `link`
    ///   (a symlink on unix — the layout real `repo sync` writes — or a
    ///   `gitdir:` pointer file, the portable equivalent).
    fn make_repo_workspace(root: &Path, projects: &[&str], link: impl Fn(&Path, &Path)) {
        fs::create_dir_all(root.join(".repo/projects")).unwrap();
        for name in projects {
            let work = root.join(name);
            fs::create_dir_all(&work).unwrap();
            let gitdir = root.join(".repo/projects").join(format!("{name}.git"));
            fs::create_dir_all(&gitdir).unwrap();
            fs::write(gitdir.join("HEAD"), "ref: refs/heads/main\n").unwrap();
            link(&work, &gitdir);
        }
        fs::write(
            root.join(".repo").join("project.list"),
            format!("{}\n", projects.join("\n")),
        )
        .unwrap();
    }

    /// Whether discovery returned a repo whose path ends with `suffix`.
    ///
    /// Discovery canonicalizes every path it emits, and on macOS the temp dir
    /// canonicalizes from `/var/...` to `/private/var/...`, so comparing
    /// against a `TempDir`-derived `PathBuf` never matches. Matching on the
    /// trailing components is what the rest of this module does.
    fn found(repos: &[PathBuf], suffix: &str) -> bool {
        repos.iter().any(|r| r.ends_with(suffix))
    }

    /// A Google `repo` workspace where every project's `.git` is a symlink to
    /// `.repo/projects/<name>.git` — the layout `repo sync` actually writes on
    /// unix. Before repo-aware discovery, all of these were invisible because
    /// the walk's `is_dir()` check rejected symlinks.
    #[cfg(unix)]
    #[test]
    fn test_repo_workspace_symlink_projects_are_discovered() {
        let tmp = TempDir::new().unwrap();
        let root = tmp.path();
        let projects = ["kernel-6.12", "hbre/libmm", "app/qualitytest"];
        make_repo_workspace(root, &projects, |work, gitdir| {
            std::os::unix::fs::symlink(gitdir, work.join(".git")).unwrap()
        });

        let config = Config {
            root_dirs: vec![root.to_path_buf()],
            scan_depth: 1, // the walk sees nothing; project.list does the work
            ..Config::default()
        };
        let repos = discover_repos(&config);
        assert_eq!(repos.len(), 3, "got {repos:?}");
        for name in projects {
            assert!(found(&repos, name), "missing {name}, got {repos:?}");
        }
    }

    /// Same workspace layout but with `gitdir:` pointer files instead of
    /// symlinks (portable; some repo versions/OSes write these).
    #[test]
    fn test_repo_workspace_gitdir_file_projects_are_discovered() {
        let tmp = TempDir::new().unwrap();
        let root = tmp.path();
        let projects = ["kernel-6.12", "hbre/libmm"];
        make_repo_workspace(root, &projects, |work, gitdir| {
            fs::write(work.join(".git"), format!("gitdir: {}\n", gitdir.display())).unwrap()
        });

        let config = Config {
            root_dirs: vec![root.to_path_buf()],
            scan_depth: 1,
            ..Config::default()
        };
        let repos = discover_repos(&config);
        assert_eq!(repos.len(), 2, "got {repos:?}");
        for name in projects {
            assert!(found(&repos, name), "missing {name}, got {repos:?}");
        }
    }

    /// A project listed in `.repo/project.list` whose working tree hasn't been
    /// synced yet must be skipped, not crash discovery.
    #[test]
    fn test_repo_workspace_skips_missing_worktrees() {
        let tmp = TempDir::new().unwrap();
        let root = tmp.path();
        make_repo_workspace(root, &["synced"], |work, gitdir| {
            fs::write(work.join(".git"), format!("gitdir: {}\n", gitdir.display())).unwrap()
        });
        // A second project that exists only in project.list, not on disk.
        fs::write(
            root.join(".repo").join("project.list"),
            "synced\nnot-synced-yet\n",
        )
        .unwrap();

        let config = Config {
            root_dirs: vec![root.to_path_buf()],
            scan_depth: 1,
            ..Config::default()
        };
        let repos = discover_repos(&config);
        assert_eq!(repos.len(), 1, "got {repos:?}");
        assert!(found(&repos, "synced"), "got {repos:?}");
    }

    /// `.repo/project.list` is generated from a manifest the workspace fetches
    /// from a remote, so an entry can name a path outside the root. Rejecting
    /// it has to happen after canonicalization: `<root>/../outside` still has
    /// `<root>` as a lexical prefix, so the pre-canonical check waves it
    /// through and discovery adopts a repo the user never configured.
    #[test]
    fn test_repo_workspace_rejects_paths_escaping_the_root() {
        let tmp = TempDir::new().unwrap();
        let root = tmp.path().join("ws");
        fs::create_dir_all(&root).unwrap();
        // A real repo sitting just outside the workspace root.
        make_repo(tmp.path(), "outside");
        make_repo_workspace(&root, &["inside"], |work, gitdir| {
            fs::write(work.join(".git"), format!("gitdir: {}\n", gitdir.display())).unwrap()
        });
        fs::write(
            root.join(".repo").join("project.list"),
            "inside\n../outside\n",
        )
        .unwrap();

        let config = Config {
            root_dirs: vec![root.clone()],
            scan_depth: 1,
            ..Config::default()
        };
        let repos = discover_repos(&config);
        assert_eq!(repos.len(), 1, "got {repos:?}");
        assert!(found(&repos, "inside"), "got {repos:?}");
        assert!(!found(&repos, "outside"), "got {repos:?}");
    }

    /// Native `.git` directories outside `.repo` must still be found alongside
    /// repo-managed projects, and the repo tool's own `.repo/manifests/.git`
    /// symlink must NOT be picked up as a project.
    #[cfg(unix)]
    #[test]
    fn test_repo_workspace_mixes_native_and_managed() {
        let tmp = TempDir::new().unwrap();
        let root = tmp.path();
        make_repo(root, "standalone");
        make_repo_workspace(root, &["managed"], |work, gitdir| {
            std::os::unix::fs::symlink(gitdir, work.join(".git")).unwrap()
        });
        // Simulate repo's own tooling symlink inside `.repo/`.
        fs::create_dir_all(root.join(".repo/manifests")).unwrap();
        fs::create_dir_all(root.join(".repo/manifests-git")).unwrap();
        fs::write(
            root.join(".repo/manifests-git/HEAD"),
            "ref: refs/heads/main\n",
        )
        .unwrap();
        std::os::unix::fs::symlink(
            root.join(".repo/manifests-git"),
            root.join(".repo/manifests").join(".git"),
        )
        .unwrap();

        let config = Config {
            root_dirs: vec![root.to_path_buf()],
            scan_depth: 2,
            ..Config::default()
        };
        let repos = discover_repos(&config);
        assert_eq!(repos.len(), 2, "got {repos:?}");
        assert!(found(&repos, "standalone"), "got {repos:?}");
        assert!(found(&repos, "managed"), "got {repos:?}");
        assert!(!found(&repos, "manifests"), "got {repos:?}");
    }

    /// A repo workspace reached through a symlinked root (e.g. `~/work` ->
    /// `/real/path`) must not double-add the top-level projects: the tree
    /// walk canonicalizes its paths while project.list used to keep the
    /// symlinked form, so `seen` saw two different strings for the same repo.
    #[cfg(unix)]
    #[test]
    fn test_repo_workspace_symlink_root_has_no_duplicates() {
        let tmp = TempDir::new().unwrap();
        let real_root = tmp.path().join("real");
        fs::create_dir_all(real_root.join(".repo/projects")).unwrap();
        let mut worktrees = Vec::new();
        for name in ["build", "kernel-6.12", "hbre/libmm"] {
            let work = real_root.join(name);
            fs::create_dir_all(&work).unwrap();
            let gitdir = real_root.join(".repo/projects").join(format!("{name}.git"));
            fs::create_dir_all(&gitdir).unwrap();
            fs::write(gitdir.join("HEAD"), "ref: refs/heads/main\n").unwrap();
            std::os::unix::fs::symlink(&gitdir, work.join(".git")).unwrap();
            worktrees.push(work);
        }
        fs::write(
            real_root.join(".repo").join("project.list"),
            "build\nkernel-6.12\nhbre/libmm\n",
        )
        .unwrap();

        // Access the workspace through a symlink alias, as `~/work` would be.
        let alias = tmp.path().join("alias");
        std::os::unix::fs::symlink(&real_root, &alias).unwrap();

        let config = Config {
            root_dirs: vec![alias],
            scan_depth: 2,
            ..Config::default()
        };
        let repos = discover_repos(&config);

        let unique: HashSet<&PathBuf> = repos.iter().collect();
        assert_eq!(repos.len(), unique.len(), "duplicates: {repos:?}");
        assert_eq!(repos.len(), 3, "got {repos:?}");
        // Every path is canonical (real) form, so list display is consistent.
        // The fixture root itself needs canonicalizing to compare: on macOS
        // the temp dir lives under `/var`, which resolves to `/private/var`.
        let canonical_root = real_root.canonicalize().unwrap();
        assert!(
            repos.iter().all(|p| p.starts_with(&canonical_root)),
            "non-canonical paths: {repos:?}"
        );
    }
}