ofsht 0.6.0

Git worktree management tool
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
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
#![allow(clippy::missing_errors_doc)]
#![allow(clippy::literal_string_with_formatting_args)]
use anyhow::{Context, Result};
use globset::{GlobBuilder, GlobSet, GlobSetBuilder};
use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
use std::collections::VecDeque;
use std::io::{BufRead, BufReader};
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::time::{Duration, Instant};
use walkdir::WalkDir;

use crate::color;
use crate::config::HookActions;

/// Hook executor interface for running hook actions
#[allow(dead_code)]
pub trait HookExecutor {
    /// Execute hook actions in the specified worktree directory
    ///
    /// # Arguments
    /// * `actions` - Hook actions to execute (run, copy, link)
    /// * `worktree_path` - Path to the worktree where hooks will be executed
    /// * `source_path` - Path to the source repository for file operations
    fn execute_hooks(
        &self,
        actions: &HookActions,
        worktree_path: &Path,
        source_path: &Path,
    ) -> Result<()>;
}

/// Real hook executor implementation
#[derive(Debug, Default)]
#[allow(dead_code)]
pub struct RealHookExecutor;

impl HookExecutor for RealHookExecutor {
    fn execute_hooks(
        &self,
        actions: &HookActions,
        worktree_path: &Path,
        source_path: &Path,
    ) -> Result<()> {
        let mp = MultiProgress::new();
        let errors = execute_hooks_impl(
            actions,
            worktree_path,
            source_path,
            color::ColorMode::Auto,
            "  ",
            &mp,
        );
        if errors.is_empty() {
            Ok(())
        } else {
            anyhow::bail!("{}", errors.join("; "))
        }
    }
}

/// Emit a static line into a `MultiProgress`, preserving bar ordering in TTY mode.
///
/// In TTY mode, creates a bar that immediately finishes with the message,
/// keeping it positioned correctly relative to active spinners.
/// In non-TTY mode, simply prints to stderr.
#[allow(clippy::missing_panics_doc)]
pub fn emit_line(mp: &MultiProgress, is_tty: bool, msg: String) {
    if is_tty {
        let bar = mp.add(ProgressBar::new(0));
        // set_style MUST be called before finish_with_message —
        // the default bar style has no {msg} placeholder.
        bar.set_style(ProgressStyle::with_template("{msg}").unwrap());
        bar.finish_with_message(msg);
    } else {
        eprintln!("{msg}");
    }
}

/// Pattern type for file matching
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum PatternKind {
    /// Literal file path (no glob metacharacters)
    Literal,
    /// Glob pattern (contains *, ?, [, ], {, })
    Glob,
}

/// Detect whether a pattern is a glob or a literal path
fn detect_pattern_kind(pattern: &str) -> PatternKind {
    const GLOB_CHARS: &[char] = &['*', '?', '[', ']', '{', '}'];
    if pattern.chars().any(|c| GLOB_CHARS.contains(&c)) {
        PatternKind::Glob
    } else {
        PatternKind::Literal
    }
}

/// Expand a pattern to a list of matching paths
///
/// Returns a tuple of (`PatternKind`, `Vec<PathBuf>`)
/// - For literal patterns: returns the path if it exists, empty vec otherwise
/// - For glob patterns: returns all matching paths, empty vec if no matches
fn expand_pattern(pattern: &str, base: &Path) -> Result<(PatternKind, Vec<PathBuf>)> {
    let kind = detect_pattern_kind(pattern);
    let paths = match kind {
        PatternKind::Literal => {
            let path = base.join(pattern);
            if path.exists() {
                vec![path]
            } else {
                vec![]
            }
        }
        PatternKind::Glob => {
            let glob = GlobBuilder::new(pattern)
                .literal_separator(true)
                .build()
                .with_context(|| format!("Invalid glob pattern: {pattern}"))?;
            let mut builder = GlobSetBuilder::new();
            builder.add(glob);
            let globset = builder.build()?;

            expand_glob(&globset, base)
        }
    };
    Ok((kind, paths))
}

/// Expand glob pattern to matching paths using walkdir
fn expand_glob(globset: &GlobSet, base: &Path) -> Vec<PathBuf> {
    let mut matches = Vec::new();

    for entry in WalkDir::new(base)
        .follow_links(false)
        .into_iter()
        .filter_map(std::result::Result::ok)
    {
        let path = entry.path();
        // Get relative path from base for glob matching
        if let Ok(rel_path) = path.strip_prefix(base) {
            if globset.is_match(rel_path) {
                matches.push(path.to_path_buf());
            }
        }
    }

    matches
}

/// Execute hook actions in the specified directory
///
/// Returns `Err` if any hook action fails, after executing all actions.
/// All errors are collected and joined with "; ".
#[allow(dead_code)]
pub fn execute_hooks(
    actions: &HookActions,
    worktree_path: &Path,
    source_path: &Path,
    color_mode: color::ColorMode,
    indent: &str,
) -> Result<()> {
    let mp = MultiProgress::new();
    execute_hooks_with_mp(actions, worktree_path, source_path, color_mode, indent, &mp)
}

/// Execute hook actions with a shared `MultiProgress`.
///
/// Use this variant when the caller manages its own header spinner
/// in the same `MultiProgress`, ensuring correct bar ordering.
pub fn execute_hooks_with_mp(
    actions: &HookActions,
    worktree_path: &Path,
    source_path: &Path,
    color_mode: color::ColorMode,
    indent: &str,
    mp: &MultiProgress,
) -> Result<()> {
    let errors = execute_hooks_impl(actions, worktree_path, source_path, color_mode, indent, mp);
    if errors.is_empty() {
        Ok(())
    } else {
        anyhow::bail!("{}", errors.join("; "))
    }
}

/// Execute hook actions, printing warnings on failure but never returning Err.
///
/// Hooks are supplementary automation — failures should not block the primary
/// operation (worktree creation, removal, etc.).
#[cfg(test)]
pub fn execute_hooks_lenient(
    actions: &HookActions,
    worktree_path: &Path,
    source_path: &Path,
    color_mode: color::ColorMode,
    indent: &str,
) {
    let mp = MultiProgress::new();
    execute_hooks_lenient_with_mp(actions, worktree_path, source_path, color_mode, indent, &mp);
}

/// Execute hook actions leniently with a shared `MultiProgress`.
///
/// Use this variant when the caller manages its own header spinner
/// in the same `MultiProgress`, ensuring correct bar ordering.
pub fn execute_hooks_lenient_with_mp(
    actions: &HookActions,
    worktree_path: &Path,
    source_path: &Path,
    color_mode: color::ColorMode,
    indent: &str,
    mp: &MultiProgress,
) {
    let is_tty = color_mode.should_colorize();
    let errors = execute_hooks_impl(actions, worktree_path, source_path, color_mode, indent, mp);
    for err in &errors {
        emit_line(
            mp,
            is_tty,
            format!(
                "{indent}{}",
                color::warn(color_mode, format!("Hook error: {err}"))
            ),
        );
    }
}

/// Execute hook actions in the specified directory (internal implementation)
///
/// Executes all hook actions regardless of individual failures, collecting
/// error messages into a `Vec<String>`.
fn execute_hooks_impl(
    actions: &HookActions,
    worktree_path: &Path,
    source_path: &Path,
    color_mode: color::ColorMode,
    indent: &str,
    mp: &MultiProgress,
) -> Vec<String> {
    let total_actions = actions.run.len() + actions.copy.len() + actions.link.len();
    let mut action_index = 0;
    let mut errors = Vec::new();

    // Execute commands
    for cmd in &actions.run {
        action_index += 1;
        let is_last = action_index == total_actions;
        if let Err(e) = execute_command(cmd, worktree_path, color_mode, is_last, indent, mp) {
            errors.push(e.to_string());
        }
    }

    // Copy files from source to worktree
    for pattern in &actions.copy {
        action_index += 1;
        let is_last = action_index == total_actions;
        if let Err(e) = copy_files(
            pattern,
            source_path,
            worktree_path,
            color_mode,
            is_last,
            indent,
            mp,
        ) {
            errors.push(e.to_string());
        }
    }

    // Create symbolic links
    for pattern in &actions.link {
        action_index += 1;
        let is_last = action_index == total_actions;
        if let Err(e) = create_symlinks(
            pattern,
            source_path,
            worktree_path,
            color_mode,
            is_last,
            indent,
            mp,
        ) {
            errors.push(e.to_string());
        }
    }

    errors
}

/// Number of trailing output lines to keep for failure diagnostics
const FAILURE_TAIL_LINES: usize = 10;

fn execute_command(
    cmd: &str,
    working_dir: &Path,
    color_mode: color::ColorMode,
    _is_last: bool,
    indent: &str,
    mp: &MultiProgress,
) -> Result<()> {
    let start = Instant::now();

    // Merge stderr into stdout at shell level, pipe the single stream.
    // This avoids deadlock (only one pipe to drain) and keeps output ordering natural.
    let merged_cmd = format!("{cmd} 2>&1");
    let mut child = Command::new("sh")
        .arg("-c")
        .arg(&merged_cmd)
        .current_dir(working_dir)
        .stdout(Stdio::piped())
        .stderr(Stdio::null())
        .spawn()
        .with_context(|| format!("Failed to execute command: {cmd}"))?;

    let child_stdout = child.stdout.take().expect("stdout was piped");

    // Setup spinner + preview bar in the shared MultiProgress (TTY only)
    let is_tty = color_mode.should_colorize();
    let (spinner, preview_bar) = if is_tty {
        let spinner = mp.add(ProgressBar::new_spinner());
        spinner.set_style(
            ProgressStyle::default_spinner()
                .template("{prefix}{spinner:.cyan} {msg}")
                .unwrap(),
        );
        spinner.set_prefix(indent.to_string());
        spinner.set_message(cmd.to_string());
        spinner.enable_steady_tick(Duration::from_millis(100));

        let preview = mp.add(ProgressBar::new(0));
        preview.set_style(ProgressStyle::with_template("{prefix}  {msg:.dim}").unwrap());
        preview.set_prefix(indent.to_string());

        (Some(spinner), Some(preview))
    } else {
        (None, None)
    };

    // Consume output in a background thread.
    // Updates preview bar in real-time and keeps last N lines for failure diagnostics.
    let preview_clone = preview_bar.clone();
    let reader_handle = std::thread::spawn(move || {
        let reader = BufReader::new(child_stdout);
        let mut tail = VecDeque::<String>::with_capacity(FAILURE_TAIL_LINES);
        for line in reader.lines().map_while(Result::ok) {
            // Update preview bar with truncated last line
            if let Some(ref pb) = preview_clone {
                let display = if line.len() > 60 {
                    format!("{}", &line[..59])
                } else {
                    line.clone()
                };
                pb.set_message(display);
            }
            // Ring buffer for failure diagnostics
            if tail.len() >= FAILURE_TAIL_LINES {
                tail.pop_front();
            }
            tail.push_back(line);
        }
        tail
    });

    let status = child
        .wait()
        .with_context(|| format!("Failed to wait for command: {cmd}"))?;
    let elapsed = start.elapsed();

    // Join reader thread to get tail buffer
    let tail = reader_handle.join().unwrap_or_default();

    // Clear preview bar
    if let Some(pb) = preview_bar {
        pb.finish_and_clear();
    }

    if status.success() {
        let timing_info = format_duration(elapsed);
        let msg = format!(
            "{indent}{} {}",
            color::success(color_mode, cmd),
            color::dim(color_mode, timing_info)
        );
        if let Some(pb) = spinner {
            // TTY: transform spinner into completion message (stays in place)
            pb.set_style(ProgressStyle::with_template("{msg}").unwrap());
            pb.finish_with_message(msg);
        } else {
            // non-TTY: print directly
            eprintln!("{msg}");
        }
    } else {
        // Clear spinner on failure
        if let Some(pb) = spinner {
            pb.finish_and_clear();
        }
        // Show last N lines of output for diagnostics
        for line in &tail {
            emit_line(
                mp,
                is_tty,
                format!("{indent}  {}", color::dim(color_mode, line)),
            );
        }
        anyhow::bail!("Hook command failed: {cmd}");
    }

    Ok(())
}

/// Format duration for display (only if >= 100ms)
fn format_duration(duration: Duration) -> String {
    let millis = duration.as_millis();
    if millis < 100 {
        String::new()
    } else if millis < 1000 {
        format!("({millis}ms)")
    } else {
        let secs = duration.as_secs_f64();
        format!("({secs:.1}s)")
    }
}

/// Copy files for a pattern (supports glob)
fn copy_files(
    pattern: &str,
    source_path: &Path,
    dest_path: &Path,
    color_mode: color::ColorMode,
    _is_last: bool,
    indent: &str,
    mp: &MultiProgress,
) -> Result<()> {
    let is_tty = color_mode.should_colorize();
    let (kind, paths) = expand_pattern(pattern, source_path)?;

    // If literal and not found, warn user
    if kind == PatternKind::Literal && paths.is_empty() {
        emit_line(
            mp,
            is_tty,
            format!(
                "{indent}{}",
                color::warn(
                    color_mode,
                    format!(
                        "Source file not found, skipping: {}",
                        source_path.join(pattern).display()
                    )
                )
            ),
        );
        return Ok(());
    }

    // Copy each matched path
    for src_path in paths {
        // Get relative path from source
        let rel_path = src_path
            .strip_prefix(source_path)
            .with_context(|| format!("Failed to get relative path for {}", src_path.display()))?;

        // Create same relative path in destination
        let dst_path = dest_path.join(rel_path);

        // Create parent directory if needed
        if let Some(parent) = dst_path.parent() {
            std::fs::create_dir_all(parent).with_context(|| {
                format!("Failed to create parent directory: {}", parent.display())
            })?;
        }

        emit_line(
            mp,
            is_tty,
            format!(
                "{indent}{}",
                color::success(color_mode, format!("Copied: {}", rel_path.display()))
            ),
        );

        if src_path.is_dir() {
            copy_dir_all(&src_path, &dst_path)?;
        } else {
            std::fs::copy(&src_path, &dst_path).with_context(|| {
                format!(
                    "Failed to copy {} to {}",
                    src_path.display(),
                    dst_path.display()
                )
            })?;
        }
    }

    Ok(())
}

/// Recursively copy a directory
fn copy_dir_all(src: &Path, dst: &Path) -> Result<()> {
    std::fs::create_dir_all(dst)
        .with_context(|| format!("Failed to create directory: {}", dst.display()))?;

    for entry in std::fs::read_dir(src)
        .with_context(|| format!("Failed to read directory: {}", src.display()))?
    {
        let entry = entry?;
        let src_path = entry.path();
        let dst_path = dst.join(entry.file_name());

        if src_path.is_dir() {
            copy_dir_all(&src_path, &dst_path)?;
        } else {
            std::fs::copy(&src_path, &dst_path).with_context(|| {
                format!(
                    "Failed to copy {} to {}",
                    src_path.display(),
                    dst_path.display()
                )
            })?;
        }
    }

    Ok(())
}

/// Result of ensuring a symlink exists at the destination path
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum SymlinkResult {
    /// A new symlink was created
    Created,
    /// The symlink already existed and points to the correct target
    AlreadyCorrect,
    /// The symlink existed but pointed to a different target; it was replaced
    Replaced,
}

/// Ensure a symlink at `dst` points to `src`, creating or replacing as needed
///
/// Returns an error if `dst` exists and is not a symlink (to protect user data).
fn ensure_symlink(src: &Path, dst: &Path) -> Result<SymlinkResult> {
    let mut was_replaced = false;

    if let Ok(metadata) = dst.symlink_metadata() {
        if metadata.file_type().is_symlink() {
            let current_target = std::fs::read_link(dst)
                .with_context(|| format!("Failed to read symlink target: {}", dst.display()))?;
            if current_target == src {
                return Ok(SymlinkResult::AlreadyCorrect);
            }
            // Wrong target: remove and recreate
            // remove_file works for file symlinks; on Windows, directory symlinks need remove_dir
            std::fs::remove_file(dst)
                .or_else(|_| std::fs::remove_dir(dst))
                .with_context(|| format!("Failed to remove existing symlink: {}", dst.display()))?;
            was_replaced = true;
        } else {
            anyhow::bail!(
                "Cannot create symlink: {} already exists and is not a symlink",
                dst.display()
            );
        }
    }

    #[cfg(unix)]
    std::os::unix::fs::symlink(src, dst).with_context(|| {
        format!(
            "Failed to create symlink from {} to {}",
            src.display(),
            dst.display()
        )
    })?;

    #[cfg(windows)]
    {
        if src.is_dir() {
            std::os::windows::fs::symlink_dir(src, dst)
        } else {
            std::os::windows::fs::symlink_file(src, dst)
        }
        .with_context(|| {
            format!(
                "Failed to create symlink from {} to {}",
                src.display(),
                dst.display()
            )
        })?;
    }

    Ok(if was_replaced {
        SymlinkResult::Replaced
    } else {
        SymlinkResult::Created
    })
}

/// Create symlinks for a pattern (supports glob)
fn create_symlinks(
    pattern: &str,
    source_path: &Path,
    worktree_path: &Path,
    color_mode: color::ColorMode,
    _is_last: bool,
    indent: &str,
    mp: &MultiProgress,
) -> Result<()> {
    let is_tty = color_mode.should_colorize();
    let (kind, paths) = expand_pattern(pattern, source_path)?;

    // If literal and not found, warn user
    if kind == PatternKind::Literal && paths.is_empty() {
        emit_line(
            mp,
            is_tty,
            format!(
                "{indent}{}",
                color::warn(
                    color_mode,
                    format!(
                        "Source file not found for symlink, skipping: {}",
                        source_path.join(pattern).display()
                    )
                )
            ),
        );
        return Ok(());
    }

    // Create symlink for each matched path
    for src_path in paths {
        // Get relative path from source
        let rel_path = src_path
            .strip_prefix(source_path)
            .with_context(|| format!("Failed to get relative path for {}", src_path.display()))?;

        // Create same relative path in worktree
        let dst_path = worktree_path.join(rel_path);

        // Create parent directory if needed
        if let Some(parent) = dst_path.parent() {
            std::fs::create_dir_all(parent).with_context(|| {
                format!("Failed to create parent directory: {}", parent.display())
            })?;
        }

        let result = ensure_symlink(&src_path, &dst_path)?;
        let msg = match result {
            SymlinkResult::Created | SymlinkResult::Replaced => {
                format!("Linked: {}", rel_path.display())
            }
            SymlinkResult::AlreadyCorrect => {
                format!("Linked (unchanged): {}", rel_path.display())
            }
        };
        emit_line(
            mp,
            is_tty,
            format!("{indent}{}", color::success(color_mode, msg)),
        );
    }

    Ok(())
}

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

    /// Mock hook executor for testing
    struct MockHookExecutor {
        should_fail: bool,
    }

    impl MockHookExecutor {
        fn new() -> Self {
            Self { should_fail: false }
        }

        fn with_failure() -> Self {
            Self { should_fail: true }
        }
    }

    impl HookExecutor for MockHookExecutor {
        fn execute_hooks(
            &self,
            _actions: &HookActions,
            _worktree_path: &Path,
            _source_path: &Path,
        ) -> Result<()> {
            if self.should_fail {
                anyhow::bail!("Mock hook executor failure");
            }
            Ok(())
        }
    }

    #[test]
    fn test_mock_hook_executor_success() {
        let executor = MockHookExecutor::new();
        let actions = HookActions::default();
        let worktree_path = PathBuf::from("/test/worktree");
        let source_path = PathBuf::from("/test/source");
        let result = executor.execute_hooks(&actions, &worktree_path, &source_path);
        assert!(result.is_ok());
    }

    #[test]
    fn test_mock_hook_executor_failure() {
        let executor = MockHookExecutor::with_failure();
        let actions = HookActions::default();
        let worktree_path = PathBuf::from("/test/worktree");
        let source_path = PathBuf::from("/test/source");
        let result = executor.execute_hooks(&actions, &worktree_path, &source_path);
        assert!(result.is_err());
    }

    #[test]
    fn test_detect_pattern_kind_literal() {
        assert_eq!(detect_pattern_kind("file.txt"), PatternKind::Literal);
        assert_eq!(detect_pattern_kind("dir/file.txt"), PatternKind::Literal);
        assert_eq!(detect_pattern_kind(".env.local"), PatternKind::Literal);
        assert_eq!(
            detect_pattern_kind("config/test.toml"),
            PatternKind::Literal
        );
    }

    #[test]
    fn test_detect_pattern_kind_glob() {
        assert_eq!(detect_pattern_kind("*.txt"), PatternKind::Glob);
        assert_eq!(detect_pattern_kind("dir/**/*.rs"), PatternKind::Glob);
        assert_eq!(detect_pattern_kind(".env.*"), PatternKind::Glob);
        assert_eq!(detect_pattern_kind("file?.txt"), PatternKind::Glob);
        assert_eq!(detect_pattern_kind("file[0-9].txt"), PatternKind::Glob);
        assert_eq!(detect_pattern_kind("file{1,2}.txt"), PatternKind::Glob);
    }

    #[test]
    fn test_expand_pattern_literal_exists() {
        let temp_dir = std::env::temp_dir();
        let test_file = temp_dir.join("test_expand_literal.txt");
        std::fs::write(&test_file, "test").unwrap();

        let (kind, paths) = expand_pattern("test_expand_literal.txt", &temp_dir).unwrap();
        assert_eq!(kind, PatternKind::Literal);
        assert_eq!(paths.len(), 1);
        assert_eq!(paths[0], test_file);

        std::fs::remove_file(&test_file).ok();
    }

    #[test]
    fn test_expand_pattern_literal_not_exists() {
        let temp_dir = std::env::temp_dir();
        let (kind, paths) = expand_pattern("nonexistent_file.txt", &temp_dir).unwrap();
        assert_eq!(kind, PatternKind::Literal);
        assert_eq!(paths.len(), 0);
    }

    #[test]
    fn test_expand_pattern_glob_single_match() {
        let temp_dir = std::env::temp_dir().join("test_glob_single");
        std::fs::create_dir_all(&temp_dir).unwrap();

        let test_file = temp_dir.join("test.txt");
        std::fs::write(&test_file, "test").unwrap();

        let (kind, paths) = expand_pattern("*.txt", &temp_dir).unwrap();
        assert_eq!(kind, PatternKind::Glob);
        assert_eq!(paths.len(), 1);
        assert_eq!(paths[0], test_file);

        std::fs::remove_dir_all(&temp_dir).ok();
    }

    #[test]
    fn test_expand_pattern_glob_multiple_matches() {
        let temp_dir = std::env::temp_dir().join("test_glob_multiple");
        std::fs::create_dir_all(&temp_dir).unwrap();

        let file1 = temp_dir.join("test1.json");
        let file2 = temp_dir.join("test2.json");
        std::fs::write(&file1, "{}").unwrap();
        std::fs::write(&file2, "{}").unwrap();

        let (kind, mut paths) = expand_pattern("*.json", &temp_dir).unwrap();
        assert_eq!(kind, PatternKind::Glob);
        assert_eq!(paths.len(), 2);
        paths.sort();
        assert!(paths.contains(&file1));
        assert!(paths.contains(&file2));

        std::fs::remove_dir_all(&temp_dir).ok();
    }

    #[test]
    fn test_expand_pattern_glob_no_match() {
        let temp_dir = std::env::temp_dir().join("test_glob_no_match");
        std::fs::create_dir_all(&temp_dir).unwrap();

        let (kind, paths) = expand_pattern("*.xyz", &temp_dir).unwrap();
        assert_eq!(kind, PatternKind::Glob);
        assert_eq!(paths.len(), 0);

        std::fs::remove_dir_all(&temp_dir).ok();
    }

    #[test]
    fn test_expand_pattern_directory_match() {
        let temp_dir = std::env::temp_dir().join("test_dir_match");
        std::fs::create_dir_all(&temp_dir).unwrap();

        let dir1 = temp_dir.join("node_modules");
        std::fs::create_dir_all(&dir1).unwrap();

        // Test literal directory match
        let (kind, paths) = expand_pattern("node_modules", &temp_dir).unwrap();
        assert_eq!(kind, PatternKind::Literal);
        assert_eq!(paths.len(), 1);
        assert_eq!(paths[0], dir1);
        assert!(paths[0].is_dir());

        std::fs::remove_dir_all(&temp_dir).ok();
    }

    #[test]
    fn test_execute_hooks_empty() {
        let actions = HookActions::default();
        let temp_dir = std::env::temp_dir();
        let result = execute_hooks(
            &actions,
            &temp_dir,
            &temp_dir,
            color::ColorMode::Never,
            "  ",
        );
        assert!(result.is_ok());
    }

    #[test]
    fn test_execute_command_success() {
        let temp_dir = std::env::temp_dir();
        let result = execute_command(
            "echo test",
            &temp_dir,
            color::ColorMode::Never,
            false,
            "  ",
            &MultiProgress::new(),
        );
        assert!(result.is_ok());
    }

    #[test]
    fn test_execute_command_failure() {
        let temp_dir = std::env::temp_dir();
        let result = execute_command(
            "exit 1",
            &temp_dir,
            color::ColorMode::Never,
            false,
            "  ",
            &MultiProgress::new(),
        );
        assert!(result.is_err());
    }

    #[test]
    fn test_execute_command_with_stdout() {
        // Commands with stdout should not pollute stdout stream
        // (Hook output should go to stderr to avoid breaking shell integration)
        let temp_dir = std::env::temp_dir();

        // This test verifies the command executes successfully
        // The actual stream verification is done via integration testing
        let result = execute_command(
            "echo 'hook output'",
            &temp_dir,
            color::ColorMode::Never,
            false,
            "  ",
            &MultiProgress::new(),
        );
        assert!(result.is_ok());
    }

    #[test]
    fn test_copy_files_literal_not_exists() {
        let temp_dir = std::env::temp_dir();
        let result = copy_files(
            "nonexistent.txt",
            &temp_dir,
            &temp_dir,
            color::ColorMode::Never,
            false,
            "  ",
            &MultiProgress::new(),
        );
        assert!(result.is_ok()); // Should warn but not fail
    }

    #[test]
    fn test_copy_files_glob() {
        let src_dir = std::env::temp_dir().join("test_copy_glob_src");
        let dst_dir = std::env::temp_dir().join("test_copy_glob_dst");
        std::fs::create_dir_all(&src_dir).unwrap();
        std::fs::create_dir_all(&dst_dir).unwrap();

        // Create test files
        std::fs::write(src_dir.join("test1.json"), "{}").unwrap();
        std::fs::write(src_dir.join("test2.json"), "{}").unwrap();

        let result = copy_files(
            "*.json",
            &src_dir,
            &dst_dir,
            color::ColorMode::Never,
            false,
            "  ",
            &MultiProgress::new(),
        );
        assert!(result.is_ok());

        // Verify files were copied
        assert!(dst_dir.join("test1.json").exists());
        assert!(dst_dir.join("test2.json").exists());

        std::fs::remove_dir_all(&src_dir).ok();
        std::fs::remove_dir_all(&dst_dir).ok();
    }

    #[test]
    fn test_expand_pattern_glob_star_does_not_cross_path_separator() {
        let temp_dir = std::env::temp_dir().join("test_glob_no_cross_sep");
        std::fs::create_dir_all(&temp_dir).unwrap();

        // Create directory structure: .claude/wadackel-a/SKILL.md, .claude/wadackel-b/SKILL.md
        let dir_a = temp_dir.join(".claude").join("wadackel-a");
        let dir_b = temp_dir.join(".claude").join("wadackel-b");
        std::fs::create_dir_all(&dir_a).unwrap();
        std::fs::create_dir_all(&dir_b).unwrap();
        std::fs::write(dir_a.join("SKILL.md"), "skill a").unwrap();
        std::fs::write(dir_b.join("SKILL.md"), "skill b").unwrap();

        let (kind, paths) = expand_pattern(".claude/wadackel-*", &temp_dir).unwrap();
        assert_eq!(kind, PatternKind::Glob);
        // Should match only the two directories, not the nested SKILL.md files
        assert_eq!(paths.len(), 2);
        let path_strs: Vec<_> = paths
            .iter()
            .map(|p| p.to_string_lossy().into_owned())
            .collect();
        assert!(path_strs.iter().any(|s| s.ends_with("wadackel-a")));
        assert!(path_strs.iter().any(|s| s.ends_with("wadackel-b")));
        // Must NOT include nested files
        assert!(!path_strs.iter().any(|s| s.ends_with("SKILL.md")));

        std::fs::remove_dir_all(&temp_dir).ok();
    }

    #[test]
    fn test_expand_pattern_glob_double_star_matches_recursively() {
        let temp_dir = std::env::temp_dir().join("test_glob_double_star");
        std::fs::create_dir_all(&temp_dir).unwrap();

        // Create: config/a.json, config/sub/b.json
        let config_dir = temp_dir.join("config");
        let sub_dir = config_dir.join("sub");
        std::fs::create_dir_all(&sub_dir).unwrap();
        std::fs::write(config_dir.join("a.json"), "{}").unwrap();
        std::fs::write(sub_dir.join("b.json"), "{}").unwrap();

        let (kind, paths) = expand_pattern("config/**/*.json", &temp_dir).unwrap();
        assert_eq!(kind, PatternKind::Glob);
        // Both files should match via ** recursive glob
        assert_eq!(paths.len(), 2);
        let path_strs: Vec<_> = paths
            .iter()
            .map(|p| p.to_string_lossy().into_owned())
            .collect();
        assert!(path_strs.iter().any(|s| s.ends_with("a.json")));
        assert!(path_strs.iter().any(|s| s.ends_with("b.json")));

        std::fs::remove_dir_all(&temp_dir).ok();
    }

    #[test]
    #[cfg(unix)]
    fn test_create_symlinks_glob_directory_with_nested_files() {
        let src_dir = std::env::temp_dir().join("test_symlink_glob_src");
        let dst_dir = std::env::temp_dir().join("test_symlink_glob_dst");
        std::fs::create_dir_all(&src_dir).unwrap();
        std::fs::create_dir_all(&dst_dir).unwrap();

        // Create: skills/skill-a/SKILL.md, skills/skill-b/SKILL.md
        let skill_a = src_dir.join("skills").join("skill-a");
        let skill_b = src_dir.join("skills").join("skill-b");
        std::fs::create_dir_all(&skill_a).unwrap();
        std::fs::create_dir_all(&skill_b).unwrap();
        std::fs::write(skill_a.join("SKILL.md"), "skill a").unwrap();
        std::fs::write(skill_b.join("SKILL.md"), "skill b").unwrap();

        // Should succeed without EEXIST errors - only directories are symlinked
        let result = create_symlinks(
            "skills/skill-*",
            &src_dir,
            &dst_dir,
            color::ColorMode::Never,
            false,
            "  ",
            &MultiProgress::new(),
        );
        assert!(result.is_ok(), "symlink creation failed: {result:?}");

        // Verify symlinks created for directories
        let link_a = dst_dir.join("skills").join("skill-a");
        let link_b = dst_dir.join("skills").join("skill-b");
        assert!(link_a.exists(), "skill-a symlink not found");
        assert!(link_b.exists(), "skill-b symlink not found");

        // Verify nested files are accessible through symlinks
        assert!(link_a.join("SKILL.md").exists());
        assert!(link_b.join("SKILL.md").exists());

        std::fs::remove_dir_all(&src_dir).ok();
        std::fs::remove_dir_all(&dst_dir).ok();
    }

    // ensure_symlink tests (unix only)
    #[test]
    #[cfg(unix)]
    fn test_ensure_symlink_creates_new() {
        let tmp = std::env::temp_dir().join("test_ensure_symlink_new");
        std::fs::create_dir_all(&tmp).unwrap();
        let src = tmp.join("src_file");
        let dst = tmp.join("dst_link");
        std::fs::write(&src, "hello").unwrap();

        let result = ensure_symlink(&src, &dst).unwrap();
        assert_eq!(result, SymlinkResult::Created);
        assert!(dst.symlink_metadata().unwrap().file_type().is_symlink());
        assert_eq!(std::fs::read_link(&dst).unwrap(), src);

        std::fs::remove_dir_all(&tmp).ok();
    }

    #[test]
    #[cfg(unix)]
    fn test_ensure_symlink_already_correct() {
        let tmp = std::env::temp_dir().join("test_ensure_symlink_correct");
        std::fs::create_dir_all(&tmp).unwrap();
        let src = tmp.join("src_file");
        let dst = tmp.join("dst_link");
        std::fs::write(&src, "hello").unwrap();
        std::os::unix::fs::symlink(&src, &dst).unwrap();

        let result = ensure_symlink(&src, &dst).unwrap();
        assert_eq!(result, SymlinkResult::AlreadyCorrect);
        // Symlink should still point to the same target
        assert_eq!(std::fs::read_link(&dst).unwrap(), src);

        std::fs::remove_dir_all(&tmp).ok();
    }

    #[test]
    #[cfg(unix)]
    fn test_ensure_symlink_replaced_wrong_target() {
        let tmp = std::env::temp_dir().join("test_ensure_symlink_replace");
        std::fs::create_dir_all(&tmp).unwrap();
        let src = tmp.join("src_file");
        let other = tmp.join("other_file");
        let dst = tmp.join("dst_link");
        std::fs::write(&src, "hello").unwrap();
        std::fs::write(&other, "other").unwrap();
        // Point dst at 'other' first
        std::os::unix::fs::symlink(&other, &dst).unwrap();

        let result = ensure_symlink(&src, &dst).unwrap();
        assert_eq!(result, SymlinkResult::Replaced);
        assert_eq!(std::fs::read_link(&dst).unwrap(), src);

        std::fs::remove_dir_all(&tmp).ok();
    }

    #[test]
    #[cfg(unix)]
    fn test_ensure_symlink_dangling_replaced() {
        let tmp = std::env::temp_dir().join("test_ensure_symlink_dangling");
        std::fs::create_dir_all(&tmp).unwrap();
        let src = tmp.join("src_file");
        let nonexistent = tmp.join("gone");
        let dst = tmp.join("dst_link");
        std::fs::write(&src, "hello").unwrap();
        // Create dangling symlink (points to a path that doesn't exist)
        std::os::unix::fs::symlink(&nonexistent, &dst).unwrap();

        let result = ensure_symlink(&src, &dst).unwrap();
        assert_eq!(result, SymlinkResult::Replaced);
        assert_eq!(std::fs::read_link(&dst).unwrap(), src);

        std::fs::remove_dir_all(&tmp).ok();
    }

    #[test]
    #[cfg(unix)]
    fn test_ensure_symlink_regular_file_conflict() {
        let tmp = std::env::temp_dir().join("test_ensure_symlink_conflict_file");
        std::fs::create_dir_all(&tmp).unwrap();
        let src = tmp.join("src_file");
        let dst = tmp.join("dst_regular");
        std::fs::write(&src, "hello").unwrap();
        // dst is a regular file (not a symlink)
        std::fs::write(&dst, "regular").unwrap();

        let result = ensure_symlink(&src, &dst);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("already exists and is not a symlink"),
            "unexpected error: {err}"
        );

        std::fs::remove_dir_all(&tmp).ok();
    }

    #[test]
    #[cfg(unix)]
    fn test_ensure_symlink_directory_conflict() {
        let tmp = std::env::temp_dir().join("test_ensure_symlink_conflict_dir");
        std::fs::create_dir_all(&tmp).unwrap();
        let src = tmp.join("src_file");
        let dst = tmp.join("dst_directory");
        std::fs::write(&src, "hello").unwrap();
        // dst is a directory (not a symlink)
        std::fs::create_dir_all(&dst).unwrap();

        let result = ensure_symlink(&src, &dst);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("already exists and is not a symlink"),
            "unexpected error: {err}"
        );

        std::fs::remove_dir_all(&tmp).ok();
    }

    #[test]
    fn test_execute_hooks_continues_after_run_failure() {
        let tmp = std::env::temp_dir().join("test_hooks_continue");
        std::fs::create_dir_all(&tmp).unwrap();

        // Create a marker file to prove the second command ran
        let marker = tmp.join("second_ran");
        let actions = HookActions {
            run: vec!["exit 1".to_string(), format!("touch {}", marker.display())],
            copy: vec![],
            link: vec![],
        };

        let errors = execute_hooks_impl(
            &actions,
            &tmp,
            &tmp,
            color::ColorMode::Never,
            "  ",
            &MultiProgress::new(),
        );

        // First command should have failed
        assert_eq!(errors.len(), 1);
        assert!(errors[0].contains("Hook command failed"));

        // Second command should still have executed
        assert!(marker.exists(), "Second hook command was not executed");

        std::fs::remove_dir_all(&tmp).ok();
    }

    #[test]
    fn test_execute_hooks_returns_all_errors() {
        let tmp = std::env::temp_dir().join("test_hooks_all_errors");
        std::fs::create_dir_all(&tmp).unwrap();

        let actions = HookActions {
            run: vec!["exit 1".to_string(), "exit 2".to_string()],
            copy: vec![],
            link: vec![],
        };

        let errors = execute_hooks_impl(
            &actions,
            &tmp,
            &tmp,
            color::ColorMode::Never,
            "  ",
            &MultiProgress::new(),
        );

        // Both commands should have failed
        assert_eq!(errors.len(), 2);

        std::fs::remove_dir_all(&tmp).ok();
    }

    #[test]
    fn test_execute_hooks_strict_returns_err_on_failure() {
        let tmp = std::env::temp_dir().join("test_hooks_strict_err");
        std::fs::create_dir_all(&tmp).unwrap();

        let actions = HookActions {
            run: vec!["exit 1".to_string()],
            copy: vec![],
            link: vec![],
        };

        let result = execute_hooks(&actions, &tmp, &tmp, color::ColorMode::Never, "  ");
        assert!(result.is_err());

        std::fs::remove_dir_all(&tmp).ok();
    }

    #[test]
    fn test_execute_hooks_lenient_does_not_panic() {
        let tmp = std::env::temp_dir().join("test_hooks_lenient");
        std::fs::create_dir_all(&tmp).unwrap();

        let actions = HookActions {
            run: vec!["exit 1".to_string()],
            copy: vec![],
            link: vec![],
        };

        // execute_hooks_lenient returns () — it should not panic
        execute_hooks_lenient(&actions, &tmp, &tmp, color::ColorMode::Never, "  ");

        std::fs::remove_dir_all(&tmp).ok();
    }
}