kabu 0.7.1

CLI tool to enhance git worktree and jj workspace with automated 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
//! Add worktree/workspace command implementation.
//!
//! Creates a new git worktree or jj workspace with automated setup from `.kabu/config.yaml`.
//! Supports both interactive and non-interactive modes, with rollback on failure.

use crate::cli::AddArgs;
use crate::color::{self, ColorConfig};
use crate::command::trust_check::{TrustHint, load_config_with_trust_check};
use crate::config::{self, Config, Link, OnConflict};
use crate::error::{Error, Result};
use crate::hook::{self, HookEnv};
use crate::interactive;
use crate::interactive::ConflictChoice;
use crate::operation::{self, ConflictAction, check_conflict, create_directory, resolve_conflict};
use crate::output::Output;
use crate::vcs::{self, VcsProvider};

use std::collections::HashSet;
use std::path::{Path, PathBuf};

/// Execute the `add` subcommand.
pub(crate) fn run(mut args: AddArgs, color: ColorConfig) -> Result<()> {
    let output = Output::new(args.quiet, color);

    let provider = vcs::get_provider()?;

    // Check if we're in a repository
    if !provider.is_inside_repo() {
        return Err(Error::NotInAnyRepo);
    }

    // Get repository root
    let repo_root = provider.main_workspace_root()?;

    let config = load_config_with_trust_check(
        &repo_root,
        &repo_root,
        !args.no_setup,
        TrustHint::SkipHooks {
            command: "kabu add --no-setup <path>",
        },
    )?;
    color::set_cli_theme(&config.ui.colors);

    // Handle interactive mode
    let worktree_path = if args.interactive {
        run_interactive(&mut args, &config, provider.as_ref())?
    } else {
        // Non-interactive: path is optional if config provides it
        let path = if let Some(path) = args.path.clone() {
            path
        } else {
            // Try to generate path from config
            let branch = args
                .commitish
                .as_ref()
                .or(args.new_branch.as_ref())
                .or(args.new_branch_force.as_ref())
                .ok_or(Error::PathRequired)?;

            let generated = config
                .worktree
                .generate_path(branch, &provider.repository_name()?)
                .ok_or(Error::PathRequired)?;

            PathBuf::from(generated)
        };

        if path.is_absolute() {
            path
        } else {
            std::env::current_dir()?.join(&path)
        }
    };

    // Skip setup if requested - just run workspace add
    if args.no_setup {
        if !args.dry_run {
            provider.workspace_add(&args, &worktree_path)?;
        } else {
            output.dry_run(&format!(
                "Would run: {} {} add {}",
                provider.name(),
                provider.workspace_type(),
                worktree_path.display()
            ));
        }
        return Ok(());
    }

    // Pre-validate: Check all source files exist BEFORE creating worktree
    for link in &config.link {
        // Skip validation for glob patterns - they will be expanded later
        if contains_glob_pattern(&link.source) {
            continue;
        }
        let source = repo_root.join(&link.source);
        if !source.exists() {
            return Err(Error::SourceNotFound {
                path: link.source.to_string_lossy().to_string(),
            });
        }
    }
    for copy in &config.copy {
        // Skip validation for glob patterns - they will be expanded later
        if contains_glob_pattern(&copy.source) {
            continue;
        }
        let source = repo_root.join(&copy.source);
        if !source.exists() {
            return Err(Error::SourceNotFound {
                path: copy.source.to_string_lossy().to_string(),
            });
        }
    }

    // Create hook environment
    // Use empty string as fallback for non-UTF8 file names (rare edge case)
    let worktree_name = worktree_path
        .file_name()
        .and_then(|n| n.to_str())
        .unwrap_or("")
        .to_string();

    // Get branch name and strip refs/heads/ prefix if present
    let branch = args
        .new_branch
        .clone()
        .or(args.commitish.clone())
        .or(args.new_branch_force.clone())
        .map(|b| b.strip_prefix("refs/heads/").unwrap_or(&b).to_string());

    let hook_shell = {
        #[cfg(windows)]
        {
            args.hook_shell
                .clone()
                .or_else(|| config.hooks.hook_shell.clone())
        }
        #[cfg(not(windows))]
        {
            None
        }
    };

    let hook_env = HookEnv {
        worktree_path: worktree_path.to_string_lossy().to_string(),
        worktree_name,
        branch,
        repo_root: repo_root.to_string_lossy().to_string(),
        vcs_type: provider.name().to_string(),
        change_id: None,
        commit_id: None,
        hook_shell,
    };

    // Run pre_add hooks
    if !config.hooks.pre_add.is_empty() {
        if args.dry_run {
            if !args.quiet {
                hook::dry_run_hooks("pre_add", &config.hooks.pre_add, &output);
            }
        } else {
            hook::run_pre_add(&config.hooks, &hook_env, &repo_root, &output)?;
        }
    }

    // Run workspace add
    if !args.dry_run {
        provider.workspace_add(&args, &worktree_path)?;
    } else {
        output.dry_run(&format!(
            "Would run: {} {} add {}",
            provider.name(),
            provider.workspace_type(),
            worktree_path.display()
        ));
    }

    // Process links and copies with rollback on failure
    if let Err(e) = run_setup(
        &args,
        &config,
        &repo_root,
        &worktree_path,
        &output,
        provider.as_ref(),
    ) {
        if !args.dry_run {
            let remove_worktree = config.on_setup_failure.remove_worktree.unwrap_or(false);
            let delete_branch = config.on_setup_failure.delete_branch.unwrap_or(false);

            if remove_worktree {
                eprintln!("Setup failed, rolling back workspace creation...");
                let _ = provider.workspace_remove(&worktree_path, true);
            }
            if delete_branch
                && let Some(branch) = args.new_branch.as_ref().or(args.new_branch_force.as_ref())
            {
                let _ = provider.delete_branch(branch);
            }
        }
        return Err(e);
    }

    // Run post_add hooks and track failure
    let mut post_add_failed = false;
    let mut post_add_error: Option<(String, Option<String>, Option<i32>)> = None;

    if !config.hooks.post_add.is_empty() {
        if args.dry_run {
            if !args.quiet {
                hook::dry_run_hooks("post_add", &config.hooks.post_add, &output);
            }
        } else if let Err(e) = hook::run_post_add(&config.hooks, &hook_env, &worktree_path, &output)
        {
            post_add_failed = true;

            // Extract error details
            let (command, exit_code) = match &e {
                Error::HookFailed {
                    command, exit_code, ..
                } => (command.clone(), *exit_code),
                _ => (String::new(), None),
            };

            // Find the description for the failed command
            let description = config
                .hooks
                .post_add
                .iter()
                .find(|entry| entry.command == command)
                .and_then(|entry| entry.description.clone());

            post_add_error = Some((command, description, exit_code));

            output.hook_warning("post_add", &e.to_string(), exit_code);
            output.hook_note("Worktree was created but post-setup may be incomplete.");
        }
    }

    // Display results summary
    if !args.dry_run && !args.quiet {
        if post_add_failed {
            // Show detailed results when there's a failure
            output.results_header();

            if !config.hooks.pre_add.is_empty() {
                output.results_item_success(&format!(
                    "pre_add hooks ({} succeeded)",
                    config.hooks.pre_add.len()
                ));
            }

            output.results_item_success("Worktree created");
            output.results_item_success("Setup operations completed");

            output.results_item_failed(&format!(
                "post_add hooks ({} failed)",
                if post_add_error.is_some() { 1 } else { 0 }
            ));

            if let Some((command, description, exit_code)) = post_add_error {
                output.results_failed_detail(description.as_deref(), &command, exit_code);
            }
        } else {
            // All succeeded - show message with path
            output.results_success("Worktree created successfully");
            output.list(&worktree_path.display().to_string());
        }
    }

    Ok(())
}

/// Run interactive mode to select branch and path.
fn run_interactive(
    args: &mut AddArgs,
    config: &Config,
    provider: &dyn VcsProvider,
) -> Result<PathBuf> {
    let current_dir = std::env::current_dir()?;
    let local_branches = provider.list_branches()?;
    let remote_branches = provider.list_remote_branches()?;

    let suggest_branch_name = config.worktree.branch_template.as_ref().map(|_| {
        let repository = provider.repository_name().unwrap_or_default();
        let worktree = config.worktree.clone();
        std::sync::Arc::new(move |commitish: &str| {
            let env = config::BranchTemplateEnv {
                commitish: commitish.to_string(),
                repository: repository.clone(),
            };
            worktree.generate_branch_name(&env).unwrap_or_default()
        }) as std::sync::Arc<dyn Fn(&str) -> String + Send + Sync>
    });

    let suggest_path = {
        let repository = provider.repository_name().unwrap_or_default();
        let worktree = config.worktree.clone();
        std::sync::Arc::new(move |branch: &str| worktree.generate_path(branch, &repository))
            as std::sync::Arc<dyn Fn(&str) -> Option<String> + Send + Sync>
    };

    let worktrees = provider.list_workspaces()?;
    let mut used_branches = std::collections::HashMap::new();
    let mut existing_worktrees = Vec::new();
    for worktree in worktrees {
        if let Some(branch) = worktree.branch.as_ref()
            && let Some(name) = branch.strip_prefix("refs/heads/")
        {
            used_branches.insert(name.to_string(), worktree.path.clone());
        }
        let branch = worktree
            .branch
            .as_ref()
            .map(|name| name.strip_prefix("refs/heads/").unwrap_or(name).to_string());
        existing_worktrees.push(interactive::WorktreeSummary {
            path: worktree.path,
            branch,
        });
    }

    // Capture VCS kind for use in closures (providers are unit structs, cheap to recreate)
    let vcs_kind = provider.kind();
    let fetch_log = std::sync::Arc::new(move |commitish: &str, limit: usize| {
        let provider: Box<dyn VcsProvider> = match vcs_kind {
            vcs::VcsKind::Git => Box::new(vcs::GitProvider),
            vcs::VcsKind::Jj | vcs::VcsKind::JjColocated => Box::new(vcs::JjProvider),
        };
        provider.log_oneline(commitish, limit)
    });

    let validate_branch_name = std::sync::Arc::new(move |name: &str| {
        let provider: Box<dyn VcsProvider> = match vcs_kind {
            vcs::VcsKind::Git => Box::new(vcs::GitProvider),
            vcs::VcsKind::Jj | vcs::VcsKind::JjColocated => Box::new(vcs::JjProvider),
        };
        provider.validate_branch_name(name)
    });

    let result = interactive::run_add_interactive(interactive::AddInteractiveInput {
        local_branches,
        remote_branches,
        used_branches,
        current_dir: current_dir.clone(),
        existing_worktrees,
        log_limit: 10,
        fetch_log,
        initial_path: args.path.clone(),
        suggest_path: Some(suggest_path),
        suggest_branch_name,
        validate_branch_name,
        theme: interactive::UiTheme::from_ui(&config.ui),
    })?;

    let branch_choice = result.branch_choice;
    if branch_choice.create_new {
        args.new_branch = Some(branch_choice.branch.clone());
        if let Some(base) = &branch_choice.base_commitish {
            args.commitish = Some(base.clone());
        }
    } else {
        args.commitish = Some(branch_choice.branch.clone());
    }

    args.path = Some(result.path.clone());

    let worktree_path = if result.path.is_absolute() {
        result.path
    } else {
        current_dir.join(&result.path)
    };

    Ok(worktree_path)
}

/// Run the setup operations (mkdir, symlinks and copies)
fn run_setup(
    args: &AddArgs,
    config: &Config,
    repo_root: &Path,
    worktree_path: &Path,
    output: &Output,
    provider: &dyn VcsProvider,
) -> Result<()> {
    let mut conflict_mode_override: Option<OnConflict> = args.on_conflict.map(|m| match m {
        crate::cli::OnConflictArg::Abort => OnConflict::Abort,
        crate::cli::OnConflictArg::Skip => OnConflict::Skip,
        crate::cli::OnConflictArg::Overwrite => OnConflict::Overwrite,
        crate::cli::OnConflictArg::Backup => OnConflict::Backup,
    });

    // Process mkdir
    for mkdir in &config.mkdir {
        let target = worktree_path.join(&mkdir.path);

        if args.dry_run {
            output.dry_run(&format!("Would create directory: {}", target.display()));
        } else {
            create_directory(&target)?;
            output.mkdir(&target, mkdir.description.as_deref());
        }
    }

    // Process symlinks (expand glob patterns first)
    // Cache the VCS-tracked file list across all glob link entries so we only
    // invoke `git ls-files` (or jj equivalent) at most once per `add` call.
    let mut tracked_cache: Option<TrackedCache> = None;
    for link in &config.link {
        let expanded_links = expand_link(link, repo_root, provider, &mut tracked_cache)?;
        for expanded_link in expanded_links {
            let params = OperationParams {
                source: &repo_root.join(&expanded_link.source),
                target: &worktree_path.join(&expanded_link.target),
                op_type: FileOp::Link,
                config_mode: expanded_link.on_conflict.or(config.on_conflict),
                description: expanded_link.description.as_deref(),
            };
            process_operation(&params, &mut conflict_mode_override, args.dry_run, output)?;
        }
    }

    // Process copies (expand glob patterns first)
    for copy in &config.copy {
        let expanded_copies = expand_copy(copy, repo_root)?;
        for expanded_copy in expanded_copies {
            let source = repo_root.join(&expanded_copy.source);
            let target = worktree_path.join(&expanded_copy.target);
            let params = OperationParams {
                source: &source,
                target: &target,
                op_type: FileOp::Copy,
                config_mode: expanded_copy.on_conflict.or(config.on_conflict),
                description: expanded_copy.description.as_deref(),
            };
            process_operation(&params, &mut conflict_mode_override, args.dry_run, output)?;
        }
    }

    Ok(())
}

/// File operation type.
enum FileOp {
    Link,
    Copy,
}

/// Parameters for a file operation.
struct OperationParams<'a> {
    source: &'a Path,
    target: &'a Path,
    op_type: FileOp,
    config_mode: Option<OnConflict>,
    description: Option<&'a str>,
}

/// Process a single operation (symlink or copy) with conflict handling.
fn process_operation(
    params: &OperationParams,
    override_mode: &mut Option<OnConflict>,
    dry_run: bool,
    output: &Output,
) -> Result<()> {
    let OperationParams {
        source,
        target,
        op_type,
        config_mode,
        description,
    } = params;
    // Check for conflict
    if check_conflict(target) {
        // Determine conflict mode
        let mode = if let Some(mode) = *override_mode {
            mode
        } else if let Some(mode) = *config_mode {
            mode
        } else {
            // Prompt user
            let choice: ConflictChoice = interactive::prompt_conflict(target)?;
            if choice.apply_to_all {
                *override_mode = Some(choice.mode);
            }
            choice.mode
        };

        // Resolve conflict
        let action = resolve_conflict(target, mode)?;
        match action {
            ConflictAction::Abort => return Err(Error::Aborted),
            ConflictAction::Skip => {
                output.skip(target);
                return Ok(());
            }
            ConflictAction::Proceed => {
                // Continue with operation
            }
        }
    }

    // Perform operation
    if dry_run {
        let op_name = match op_type {
            FileOp::Link => "link",
            FileOp::Copy => "copy",
        };
        output.dry_run(&format!(
            "Would {}: {} -> {}",
            op_name,
            source.display(),
            target.display()
        ));
    } else {
        match op_type {
            FileOp::Link => {
                operation::create_symlink(source, target)?;
                output.link(source, target, *description);
            }
            FileOp::Copy => {
                operation::copy_file(source, target)?;
                output.copy(source, target, *description);
            }
        }
    }

    Ok(())
}

/// Check if a path contains glob patterns.
fn contains_glob_pattern(path: &Path) -> bool {
    path.to_str()
        .map(|s| s.contains('*') || s.contains('?') || s.contains('['))
        .unwrap_or(false)
}

/// Return the longest leading sequence of path components that contain no
/// glob meta-characters. Used to start a `WalkDir` from the deepest known
/// directory instead of `repo_root`, which avoids stat()-ing unrelated
/// subtrees such as build caches or `node_modules`.
fn glob_literal_prefix(pattern: &Path) -> PathBuf {
    let mut prefix = PathBuf::new();
    for component in pattern.components() {
        let s = component.as_os_str().to_string_lossy();
        if s.contains('*') || s.contains('?') || s.contains('[') {
            break;
        }
        prefix.push(component);
    }
    prefix
}

/// Cached VCS-tracked file/directory set, reused across all glob link entries
/// that opt into `skip_tracked: true`.
struct TrackedCache {
    files: HashSet<PathBuf>,
    dirs: HashSet<PathBuf>,
}

impl TrackedCache {
    fn build(provider: &dyn VcsProvider, repo_root: &Path) -> Result<Self> {
        let files: HashSet<PathBuf> = provider
            .list_tracked_files(repo_root)?
            .into_iter()
            .collect();
        // `git ls-files` (and the jj equivalent) only emit file paths, so
        // synthesize the set of directories that contain tracked files. This
        // lets us skip a whole directory when a glob matches it.
        let mut dirs = HashSet::new();
        for f in &files {
            let mut parent = f.parent();
            while let Some(p) = parent {
                if p.as_os_str().is_empty() {
                    break;
                }
                dirs.insert(p.to_path_buf());
                parent = p.parent();
            }
        }
        Ok(Self { files, dirs })
    }
}

/// Expand a link entry with glob patterns into multiple concrete link entries.
/// If skip_tracked is true, filter out VCS-tracked files.
fn expand_link(
    link: &Link,
    repo_root: &Path,
    provider: &dyn VcsProvider,
    tracked_cache: &mut Option<TrackedCache>,
) -> Result<Vec<Link>> {
    let source_str = link.source.to_string_lossy();

    if !contains_glob_pattern(&link.source) {
        // No glob pattern, return as-is
        return Ok(vec![link.clone()]);
    }

    // Build glob matcher
    let glob = globset::GlobBuilder::new(&source_str)
        .literal_separator(true)
        .build()
        .map_err(|e| Error::ConfigValidation {
            message: format!("Invalid glob pattern '{}': {}", source_str, e),
        })?;
    let matcher = glob.compile_matcher();

    // Walk only the literal prefix of the pattern so we don't descend into
    // unrelated trees (e.g. `rust/target`, `node_modules`) just to discard
    // them after a glob-match check.
    let prefix = glob_literal_prefix(&link.source);
    let walk_root = repo_root.join(&prefix);
    if !walk_root.exists() {
        return Ok(Vec::new());
    }

    let tracked = if link.skip_tracked {
        if tracked_cache.is_none() {
            *tracked_cache = Some(TrackedCache::build(provider, repo_root)?);
        }
        tracked_cache.as_ref()
    } else {
        None
    };

    // Walk the repository and find matching files and directories
    // Collect matched directories to avoid processing their contents
    let mut matched_dirs: HashSet<PathBuf> = HashSet::new();
    let mut results = Vec::new();

    for entry in walkdir::WalkDir::new(&walk_root)
        .follow_links(false)
        .into_iter()
        .filter_entry(|e| e.file_name() != ".git")
        .filter_map(|e| e.ok())
    {
        let path = entry.path();

        // Get relative path from repo root
        let rel_path = match path.strip_prefix(repo_root) {
            Ok(p) => p,
            Err(_) => continue,
        };

        // Skip if parent directory was already matched
        let mut should_skip = false;
        for matched_dir in &matched_dirs {
            if rel_path.starts_with(matched_dir) && rel_path != matched_dir {
                should_skip = true;
                break;
            }
        }
        if should_skip {
            continue;
        }

        // Check if it matches the glob pattern
        if !matcher.is_match(rel_path) {
            continue;
        }

        // `entry.file_type()` reads from the dirent cached by walkdir and
        // avoids the extra stat() that `path.is_dir()` would issue.
        let is_dir = entry.file_type().is_dir();

        // Skip if it's tracked and skip_tracked is true
        if let Some(cache) = tracked {
            if cache.files.contains(rel_path) {
                continue;
            }
            // Directories containing tracked files should also be skipped,
            // as git ls-files only returns file paths, not directory paths
            if is_dir && cache.dirs.contains(rel_path) {
                continue;
            }
        }

        // If it's a directory, add to matched_dirs to skip its contents
        if is_dir {
            matched_dirs.insert(rel_path.to_path_buf());
        }

        // Create a link entry for this file or directory
        let mut file_link = link.clone();
        file_link.source = rel_path.to_path_buf();
        file_link.target = rel_path.to_path_buf();
        file_link.skip_tracked = false; // Already filtered, no need to check again
        results.push(file_link);
    }

    Ok(results)
}

/// Expand a copy entry with glob patterns into multiple concrete copy entries.
fn expand_copy(copy: &config::Copy, repo_root: &Path) -> Result<Vec<config::Copy>> {
    let source_str = copy.source.to_string_lossy();

    if !contains_glob_pattern(&copy.source) {
        return Ok(vec![copy.clone()]);
    }

    let glob = globset::GlobBuilder::new(&source_str)
        .literal_separator(true)
        .build()
        .map_err(|e| Error::ConfigValidation {
            message: format!("Invalid glob pattern '{}': {}", source_str, e),
        })?;
    let matcher = glob.compile_matcher();

    let prefix = glob_literal_prefix(&copy.source);
    let walk_root = repo_root.join(&prefix);
    if !walk_root.exists() {
        return Ok(Vec::new());
    }

    let mut matched_dirs: HashSet<PathBuf> = HashSet::new();
    let mut results = Vec::new();

    for entry in walkdir::WalkDir::new(&walk_root)
        .follow_links(false)
        .into_iter()
        .filter_entry(|e| e.file_name() != ".git")
        .filter_map(|e| e.ok())
    {
        let path = entry.path();
        let rel_path = match path.strip_prefix(repo_root) {
            Ok(p) => p,
            Err(_) => continue,
        };

        // Skip if parent directory was already matched
        let mut should_skip = false;
        for matched_dir in &matched_dirs {
            if rel_path.starts_with(matched_dir) && rel_path != matched_dir {
                should_skip = true;
                break;
            }
        }
        if should_skip {
            continue;
        }

        if !matcher.is_match(rel_path) {
            continue;
        }

        if entry.file_type().is_dir() {
            matched_dirs.insert(rel_path.to_path_buf());
        }

        let mut file_copy = copy.clone();
        file_copy.source = rel_path.to_path_buf();
        file_copy.target = rel_path.to_path_buf();
        results.push(file_copy);
    }

    Ok(results)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_contains_glob_pattern_with_asterisk() {
        assert!(contains_glob_pattern(Path::new("secrets/*")));
    }

    #[test]
    fn test_contains_glob_pattern_with_question() {
        assert!(contains_glob_pattern(Path::new("file?.txt")));
    }

    #[test]
    fn test_contains_glob_pattern_with_bracket() {
        assert!(contains_glob_pattern(Path::new("file[0-9].txt")));
    }

    #[test]
    fn test_contains_glob_pattern_none() {
        assert!(!contains_glob_pattern(Path::new("secrets/config.json")));
    }

    #[test]
    fn test_glob_literal_prefix_trailing_wildcard() {
        assert_eq!(
            glob_literal_prefix(Path::new(".claude/*")),
            PathBuf::from(".claude")
        );
        assert_eq!(
            glob_literal_prefix(Path::new("script/stub-server/certs/*")),
            PathBuf::from("script/stub-server/certs")
        );
    }

    #[test]
    fn test_glob_literal_prefix_wildcard_in_first_component() {
        assert_eq!(glob_literal_prefix(Path::new("dir*")), PathBuf::new());
        assert_eq!(glob_literal_prefix(Path::new("**/*.rs")), PathBuf::new());
    }

    #[test]
    fn test_glob_literal_prefix_wildcard_in_middle() {
        assert_eq!(
            glob_literal_prefix(Path::new("foo/*/bar")),
            PathBuf::from("foo")
        );
    }

    #[test]
    fn test_glob_literal_prefix_no_wildcard() {
        assert_eq!(
            glob_literal_prefix(Path::new("a/b/c.txt")),
            PathBuf::from("a/b/c.txt")
        );
    }

    #[test]
    fn test_glob_literal_prefix_question_and_bracket() {
        assert_eq!(
            glob_literal_prefix(Path::new("a/b/file?.txt")),
            PathBuf::from("a/b")
        );
        assert_eq!(
            glob_literal_prefix(Path::new("a/b/file[0-9].txt")),
            PathBuf::from("a/b")
        );
    }
}

#[cfg(all(test, feature = "impure-test"))]
mod impure_tests {
    use super::*;

    #[test]
    fn test_expand_link_no_glob() {
        use tempfile::TempDir;
        let temp_dir = TempDir::new().unwrap();
        let repo_root = temp_dir.path();

        // Create test file
        std::fs::write(repo_root.join("test.txt"), "content").unwrap();

        let link = Link {
            source: PathBuf::from("test.txt"),
            target: PathBuf::from("test.txt"),
            on_conflict: None,
            description: None,
            skip_tracked: false,
        };

        let provider = vcs::GitProvider;
        let result = expand_link(&link, repo_root, &provider, &mut None).unwrap();
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].source, PathBuf::from("test.txt"));
    }

    #[test]
    fn test_expand_link_with_glob() {
        use tempfile::TempDir;
        let temp_dir = TempDir::new().unwrap();
        let repo_root = temp_dir.path();

        // Create test files
        std::fs::create_dir_all(repo_root.join("fixtures")).unwrap();
        std::fs::write(repo_root.join("fixtures/file1.txt"), "content1").unwrap();
        std::fs::write(repo_root.join("fixtures/file2.txt"), "content2").unwrap();
        std::fs::write(repo_root.join("fixtures/data.json"), "{}").unwrap();

        let link = Link {
            source: PathBuf::from("fixtures/*.txt"),
            target: PathBuf::from("fixtures/*.txt"),
            on_conflict: None,
            description: None,
            skip_tracked: false,
        };

        let provider = vcs::GitProvider;
        let result = expand_link(&link, repo_root, &provider, &mut None).unwrap();
        assert_eq!(result.len(), 2);

        let mut sources: Vec<_> = result.iter().map(|l| l.source.clone()).collect();
        sources.sort();
        assert_eq!(sources[0], PathBuf::from("fixtures/file1.txt"));
        assert_eq!(sources[1], PathBuf::from("fixtures/file2.txt"));
    }

    #[test]
    fn test_expand_link_skip_tracked() {
        use tempfile::TempDir;
        let temp_dir = TempDir::new().unwrap();
        let repo_root = temp_dir.path();

        // Initialize git repo
        let init_result = std::process::Command::new("git")
            .args(["init"])
            .current_dir(repo_root)
            .output();

        if init_result.is_err() {
            eprintln!("Skipping test: git not available");
            return;
        }

        // Configure git user for the test repo
        std::process::Command::new("git")
            .args(["config", "user.email", "test@example.com"])
            .current_dir(repo_root)
            .output()
            .ok();
        std::process::Command::new("git")
            .args(["config", "user.name", "Test User"])
            .current_dir(repo_root)
            .output()
            .ok();

        // Create and track a file
        std::fs::create_dir_all(repo_root.join("fixtures")).unwrap();
        std::fs::write(repo_root.join("fixtures/tracked.txt"), "tracked").unwrap();
        std::fs::write(repo_root.join("fixtures/untracked.txt"), "untracked").unwrap();

        // Add and commit the tracked file
        let add_result = std::process::Command::new("git")
            .args(["add", "fixtures/tracked.txt"])
            .current_dir(repo_root)
            .output();

        if add_result.is_err() || !add_result.unwrap().status.success() {
            eprintln!("Skipping test: git add failed");
            return;
        }

        let commit_result = std::process::Command::new("git")
            .args(["commit", "-m", "Add tracked file"])
            .current_dir(repo_root)
            .output();

        if commit_result.is_err() || !commit_result.unwrap().status.success() {
            eprintln!("Skipping test: git commit failed");
            return;
        }

        let link = Link {
            source: PathBuf::from("fixtures/*.txt"),
            target: PathBuf::from("fixtures/*.txt"),
            on_conflict: None,
            description: None,
            skip_tracked: true,
        };

        let provider = vcs::GitProvider;
        let result = expand_link(&link, repo_root, &provider, &mut None).unwrap();

        // Should only include untracked file
        // Note: This test may be flaky in some environments
        if result.len() == 1 {
            assert_eq!(result[0].source, PathBuf::from("fixtures/untracked.txt"));
        } else {
            eprintln!(
                "Warning: Expected 1 result but got {}. This may be environment-dependent.",
                result.len()
            );
        }
    }

    #[test]
    fn test_expand_link_skip_tracked_directory() {
        use tempfile::TempDir;
        let temp_dir = TempDir::new().unwrap();
        let repo_root = temp_dir.path();

        // Initialize git repo
        let init_result = std::process::Command::new("git")
            .args(["init"])
            .current_dir(repo_root)
            .output();

        if init_result.is_err() {
            eprintln!("Skipping test: git not available");
            return;
        }

        // Configure git user for the test repo
        std::process::Command::new("git")
            .args(["config", "user.email", "test@example.com"])
            .current_dir(repo_root)
            .output()
            .ok();
        std::process::Command::new("git")
            .args(["config", "user.name", "Test User"])
            .current_dir(repo_root)
            .output()
            .ok();

        // Create directory structure:
        // .config/tracked-dir/file.txt  (tracked)
        // .config/untracked-dir/file.txt  (untracked)
        // .config/untracked-file.txt  (untracked)
        std::fs::create_dir_all(repo_root.join(".config/tracked-dir")).unwrap();
        std::fs::write(repo_root.join(".config/tracked-dir/file.txt"), "tracked").unwrap();
        std::fs::create_dir_all(repo_root.join(".config/untracked-dir")).unwrap();
        std::fs::write(
            repo_root.join(".config/untracked-dir/file.txt"),
            "untracked",
        )
        .unwrap();
        std::fs::write(repo_root.join(".config/untracked-file.txt"), "untracked").unwrap();

        // Track only the file inside tracked-dir
        let add_result = std::process::Command::new("git")
            .args(["add", ".config/tracked-dir/file.txt"])
            .current_dir(repo_root)
            .output();

        if add_result.is_err() || !add_result.unwrap().status.success() {
            eprintln!("Skipping test: git add failed");
            return;
        }

        let commit_result = std::process::Command::new("git")
            .args(["commit", "-m", "Add tracked dir"])
            .current_dir(repo_root)
            .output();

        if commit_result.is_err() || !commit_result.unwrap().status.success() {
            eprintln!("Skipping test: git commit failed");
            return;
        }

        let link = Link {
            source: PathBuf::from(".config/*"),
            target: PathBuf::from(".config/*"),
            on_conflict: None,
            description: None,
            skip_tracked: true,
        };

        let provider = vcs::GitProvider;
        let result = expand_link(&link, repo_root, &provider, &mut None).unwrap();

        // Should skip tracked-dir (directory containing tracked files)
        // and include untracked-dir and untracked-file.txt
        if result.len() == 2 {
            let mut sources: Vec<_> = result.iter().map(|l| l.source.clone()).collect();
            sources.sort();
            assert_eq!(sources[0], PathBuf::from(".config/untracked-dir"));
            assert_eq!(sources[1], PathBuf::from(".config/untracked-file.txt"));
        } else {
            eprintln!(
                "Warning: Expected 2 results but got {}. This may be environment-dependent.",
                result.len()
            );
        }
    }

    #[test]
    fn test_expand_link_with_directory() {
        use tempfile::TempDir;
        let temp_dir = TempDir::new().unwrap();
        let repo_root = temp_dir.path();

        // Create test directory with files inside
        std::fs::create_dir_all(repo_root.join("testdir")).unwrap();
        std::fs::write(repo_root.join("testdir/file1.txt"), "content1").unwrap();
        std::fs::write(repo_root.join("testdir/file2.txt"), "content2").unwrap();

        // Pattern matching the directory
        let link = Link {
            source: PathBuf::from("testdir"),
            target: PathBuf::from("testdir"),
            on_conflict: None,
            description: None,
            skip_tracked: false,
        };

        let provider = vcs::GitProvider;
        let result = expand_link(&link, repo_root, &provider, &mut None).unwrap();

        // Should return only the directory, not its contents
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].source, PathBuf::from("testdir"));
    }

    #[test]
    fn test_expand_link_with_glob_matching_directory() {
        use tempfile::TempDir;
        let temp_dir = TempDir::new().unwrap();
        let repo_root = temp_dir.path();

        // Create test directories with files inside
        std::fs::create_dir_all(repo_root.join("dir1")).unwrap();
        std::fs::write(repo_root.join("dir1/file.txt"), "content1").unwrap();
        std::fs::create_dir_all(repo_root.join("dir2")).unwrap();
        std::fs::write(repo_root.join("dir2/file.txt"), "content2").unwrap();
        std::fs::create_dir_all(repo_root.join("other")).unwrap();
        std::fs::write(repo_root.join("other/file.txt"), "content3").unwrap();

        // Pattern matching directories starting with "dir"
        let link = Link {
            source: PathBuf::from("dir*"),
            target: PathBuf::from("dir*"),
            on_conflict: None,
            description: None,
            skip_tracked: false,
        };

        let provider = vcs::GitProvider;
        let result = expand_link(&link, repo_root, &provider, &mut None).unwrap();

        // Should return only the directories, not their contents
        assert_eq!(result.len(), 2);

        let mut sources: Vec<_> = result.iter().map(|l| l.source.clone()).collect();
        sources.sort();
        assert_eq!(sources[0], PathBuf::from("dir1"));
        assert_eq!(sources[1], PathBuf::from("dir2"));
    }

    #[test]
    fn test_expand_copy_no_glob() {
        use tempfile::TempDir;
        let temp_dir = TempDir::new().unwrap();
        let repo_root = temp_dir.path();

        std::fs::write(repo_root.join("test.txt"), "content").unwrap();

        let copy = config::Copy {
            source: PathBuf::from("test.txt"),
            target: PathBuf::from("test.txt"),
            on_conflict: None,
            description: None,
        };

        let result = expand_copy(&copy, repo_root).unwrap();
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].source, PathBuf::from("test.txt"));
    }

    #[test]
    fn test_expand_copy_with_glob() {
        use tempfile::TempDir;
        let temp_dir = TempDir::new().unwrap();
        let repo_root = temp_dir.path();

        std::fs::create_dir_all(repo_root.join("fixtures")).unwrap();
        std::fs::write(repo_root.join("fixtures/file1.txt"), "content1").unwrap();
        std::fs::write(repo_root.join("fixtures/file2.txt"), "content2").unwrap();
        std::fs::write(repo_root.join("fixtures/data.json"), "{}").unwrap();

        let copy = config::Copy {
            source: PathBuf::from("fixtures/*.txt"),
            target: PathBuf::from("fixtures/*.txt"),
            on_conflict: None,
            description: None,
        };

        let result = expand_copy(&copy, repo_root).unwrap();
        assert_eq!(result.len(), 2);

        let mut sources: Vec<_> = result.iter().map(|c| c.source.clone()).collect();
        sources.sort();
        assert_eq!(sources[0], PathBuf::from("fixtures/file1.txt"));
        assert_eq!(sources[1], PathBuf::from("fixtures/file2.txt"));
    }

    #[test]
    fn test_expand_copy_with_directory() {
        use tempfile::TempDir;
        let temp_dir = TempDir::new().unwrap();
        let repo_root = temp_dir.path();

        std::fs::create_dir_all(repo_root.join("testdir")).unwrap();
        std::fs::write(repo_root.join("testdir/file1.txt"), "content1").unwrap();
        std::fs::write(repo_root.join("testdir/file2.txt"), "content2").unwrap();

        let copy = config::Copy {
            source: PathBuf::from("testdir"),
            target: PathBuf::from("testdir"),
            on_conflict: None,
            description: None,
        };

        let result = expand_copy(&copy, repo_root).unwrap();
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].source, PathBuf::from("testdir"));
    }

    #[test]
    fn test_expand_copy_with_glob_matching_directory() {
        use tempfile::TempDir;
        let temp_dir = TempDir::new().unwrap();
        let repo_root = temp_dir.path();

        std::fs::create_dir_all(repo_root.join("dir1")).unwrap();
        std::fs::write(repo_root.join("dir1/file.txt"), "content1").unwrap();
        std::fs::create_dir_all(repo_root.join("dir2")).unwrap();
        std::fs::write(repo_root.join("dir2/file.txt"), "content2").unwrap();
        std::fs::create_dir_all(repo_root.join("other")).unwrap();
        std::fs::write(repo_root.join("other/file.txt"), "content3").unwrap();

        let copy = config::Copy {
            source: PathBuf::from("dir*"),
            target: PathBuf::from("dir*"),
            on_conflict: None,
            description: None,
        };

        let result = expand_copy(&copy, repo_root).unwrap();
        assert_eq!(result.len(), 2);

        let mut sources: Vec<_> = result.iter().map(|c| c.source.clone()).collect();
        sources.sort();
        assert_eq!(sources[0], PathBuf::from("dir1"));
        assert_eq!(sources[1], PathBuf::from("dir2"));
    }
}