repolens 2.0.2

A CLI tool to audit and prepare repositories for open source or enterprise standards
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
//! Git hooks management module
//!
//! This module provides functionality for installing and managing Git hooks
//! that integrate RepoLens into the development workflow. Supported hooks:
//! - **pre-commit**: Checks for exposed secrets before each commit
//! - **pre-push**: Runs a full audit before pushing to a remote

use std::fs;
use std::path::{Path, PathBuf};

use serde::{Deserialize, Serialize};

use crate::error::{ActionError, RepoLensError};

/// Name of the pre-commit hook file
const PRE_COMMIT_HOOK: &str = "pre-commit";

/// Name of the pre-push hook file
const PRE_PUSH_HOOK: &str = "pre-push";

/// Suffix used for backing up existing hooks
const BACKUP_SUFFIX: &str = ".repolens-backup";

/// Configuration for Git hooks
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HooksConfig {
    /// Whether to install the pre-commit hook
    #[serde(default = "default_true")]
    pub pre_commit: bool,
    /// Whether to install the pre-push hook
    #[serde(default = "default_true")]
    pub pre_push: bool,
    /// Whether warnings should cause hook failure
    #[serde(default)]
    pub fail_on_warnings: bool,
}

fn default_true() -> bool {
    true
}

impl Default for HooksConfig {
    fn default() -> Self {
        Self {
            pre_commit: true,
            pre_push: true,
            fail_on_warnings: false,
        }
    }
}

/// Manages the installation and removal of Git hooks
#[derive(Debug)]
pub struct HooksManager {
    /// Path to the .git/hooks directory
    hooks_dir: PathBuf,
    /// Hook configuration
    config: HooksConfig,
}

impl HooksManager {
    /// Create a new HooksManager for the given repository root
    ///
    /// # Arguments
    ///
    /// * `repo_root` - The root directory of the git repository
    /// * `config` - Hook configuration
    ///
    /// # Errors
    ///
    /// Returns an error if the `.git/hooks` directory cannot be found or created
    pub fn new(repo_root: &Path, config: HooksConfig) -> Result<Self, RepoLensError> {
        let hooks_dir = find_hooks_dir(repo_root)?;
        Ok(Self { hooks_dir, config })
    }

    /// Install the configured Git hooks
    ///
    /// # Arguments
    ///
    /// * `force` - Whether to overwrite existing hooks (after backing them up)
    ///
    /// # Returns
    ///
    /// A vector of messages describing what was installed
    pub fn install(&self, force: bool) -> Result<Vec<String>, RepoLensError> {
        let mut messages = Vec::new();

        // Ensure hooks directory exists
        fs::create_dir_all(&self.hooks_dir).map_err(|e| {
            RepoLensError::Action(ActionError::DirectoryCreate {
                path: self.hooks_dir.display().to_string(),
                source: e,
            })
        })?;

        if self.config.pre_commit {
            let msg = self.install_hook(
                PRE_COMMIT_HOOK,
                &generate_pre_commit_hook(&self.config),
                force,
            )?;
            messages.push(msg);
        }

        if self.config.pre_push {
            let msg =
                self.install_hook(PRE_PUSH_HOOK, &generate_pre_push_hook(&self.config), force)?;
            messages.push(msg);
        }

        Ok(messages)
    }

    /// Remove installed RepoLens hooks, restoring backups if they exist
    ///
    /// # Returns
    ///
    /// A vector of messages describing what was removed
    pub fn remove(&self) -> Result<Vec<String>, RepoLensError> {
        let mut messages = Vec::new();

        let msg = self.remove_hook(PRE_COMMIT_HOOK)?;
        messages.push(msg);

        let msg = self.remove_hook(PRE_PUSH_HOOK)?;
        messages.push(msg);

        Ok(messages)
    }

    /// Install a single hook file
    fn install_hook(
        &self,
        hook_name: &str,
        content: &str,
        force: bool,
    ) -> Result<String, RepoLensError> {
        let hook_path = self.hooks_dir.join(hook_name);

        // Check if hook already exists
        if hook_path.exists() {
            if is_repolens_hook(&hook_path)? {
                // Already a RepoLens hook, overwrite it
                write_hook_file(&hook_path, content)?;
                return Ok(format!("Updated existing RepoLens {} hook", hook_name));
            }

            if !force {
                return Err(RepoLensError::Action(ActionError::ExecutionFailed {
                    message: format!(
                        "Hook '{}' already exists. Use --force to overwrite (existing hook will be backed up)",
                        hook_name
                    ),
                }));
            }

            // Backup existing hook
            let backup_path = self
                .hooks_dir
                .join(format!("{}{}", hook_name, BACKUP_SUFFIX));
            fs::copy(&hook_path, &backup_path).map_err(|e| {
                RepoLensError::Action(ActionError::FileWrite {
                    path: backup_path.display().to_string(),
                    source: e,
                })
            })?;

            write_hook_file(&hook_path, content)?;
            return Ok(format!(
                "Installed {} hook (existing hook backed up to {}{})",
                hook_name, hook_name, BACKUP_SUFFIX
            ));
        }

        write_hook_file(&hook_path, content)?;
        Ok(format!("Installed {} hook", hook_name))
    }

    /// Remove a single hook file, restoring backup if available
    fn remove_hook(&self, hook_name: &str) -> Result<String, RepoLensError> {
        let hook_path = self.hooks_dir.join(hook_name);
        let backup_path = self
            .hooks_dir
            .join(format!("{}{}", hook_name, BACKUP_SUFFIX));

        if !hook_path.exists() {
            return Ok(format!("No {} hook to remove", hook_name));
        }

        if !is_repolens_hook(&hook_path)? {
            return Ok(format!(
                "Skipped {} hook (not installed by RepoLens)",
                hook_name
            ));
        }

        fs::remove_file(&hook_path).map_err(|e| {
            RepoLensError::Action(ActionError::FileWrite {
                path: hook_path.display().to_string(),
                source: e,
            })
        })?;

        // Restore backup if it exists
        if backup_path.exists() {
            fs::rename(&backup_path, &hook_path).map_err(|e| {
                RepoLensError::Action(ActionError::FileWrite {
                    path: hook_path.display().to_string(),
                    source: e,
                })
            })?;
            return Ok(format!(
                "Removed {} hook and restored previous hook from backup",
                hook_name
            ));
        }

        Ok(format!("Removed {} hook", hook_name))
    }

    /// Get the path to the hooks directory
    #[allow(dead_code)]
    pub fn hooks_dir(&self) -> &Path {
        &self.hooks_dir
    }
}

/// Find the `.git/hooks` directory for a repository
///
/// This function handles both standard repositories and worktrees.
fn find_hooks_dir(repo_root: &Path) -> Result<PathBuf, RepoLensError> {
    let git_dir = repo_root.join(".git");

    if git_dir.is_dir() {
        return Ok(git_dir.join("hooks"));
    }

    // Handle git worktrees: .git is a file containing "gitdir: <path>"
    if git_dir.is_file() {
        let content = fs::read_to_string(&git_dir).map_err(|e| {
            RepoLensError::Action(ActionError::ExecutionFailed {
                message: format!("Failed to read .git file: {}", e),
            })
        })?;

        if let Some(gitdir_path) = content.strip_prefix("gitdir: ") {
            let gitdir_path = gitdir_path.trim();
            let gitdir = if Path::new(gitdir_path).is_absolute() {
                PathBuf::from(gitdir_path)
            } else {
                repo_root.join(gitdir_path)
            };
            return Ok(gitdir.join("hooks"));
        }
    }

    Err(RepoLensError::Action(ActionError::ExecutionFailed {
        message: format!(
            "Could not find .git directory in {}. Is this a git repository?",
            repo_root.display()
        ),
    }))
}

/// Check if a hook file was installed by RepoLens
fn is_repolens_hook(hook_path: &Path) -> Result<bool, RepoLensError> {
    let content = fs::read_to_string(hook_path).map_err(|e| {
        RepoLensError::Action(ActionError::ExecutionFailed {
            message: format!("Failed to read hook file '{}': {}", hook_path.display(), e),
        })
    })?;
    Ok(content.contains("# RepoLens Git Hook"))
}

/// Write a hook file and set executable permissions
fn write_hook_file(path: &Path, content: &str) -> Result<(), RepoLensError> {
    fs::write(path, content).map_err(|e| {
        RepoLensError::Action(ActionError::FileWrite {
            path: path.display().to_string(),
            source: e,
        })
    })?;

    // Set executable permissions on Unix
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let permissions = fs::Permissions::from_mode(0o755);
        fs::set_permissions(path, permissions).map_err(|e| {
            RepoLensError::Action(ActionError::FileWrite {
                path: path.display().to_string(),
                source: e,
            })
        })?;
    }

    Ok(())
}

/// Generate the content of the pre-commit hook script
///
/// The pre-commit hook runs a secrets scan to prevent committing exposed credentials.
pub fn generate_pre_commit_hook(config: &HooksConfig) -> String {
    let fail_on_warnings = if config.fail_on_warnings {
        " --fail-on-warnings"
    } else {
        ""
    };

    format!(
        r#"#!/bin/sh
# RepoLens Git Hook - pre-commit
# This hook was automatically installed by RepoLens.
# It checks for exposed secrets before allowing a commit.
#
# To skip this hook, use: git commit --no-verify

set -e

echo "RepoLens: Checking for exposed secrets..."

if ! command -v repolens >/dev/null 2>&1; then
    echo "Warning: repolens is not installed or not in PATH. Skipping pre-commit check."
    echo "Install it with: cargo install repolens"
    exit 0
fi

if ! repolens plan --only secrets --format terminal{fail_on_warnings} 2>/dev/null; then
    echo ""
    echo "RepoLens: Secrets detected! Commit aborted."
    echo "Please remove or ignore the detected secrets before committing."
    echo "To skip this check, use: git commit --no-verify"
    exit 1
fi

echo "RepoLens: No secrets detected. Proceeding with commit."
"#
    )
}

/// Generate the content of the pre-push hook script
///
/// The pre-push hook runs a full audit to prevent pushing code with issues.
pub fn generate_pre_push_hook(config: &HooksConfig) -> String {
    let fail_on_warnings = if config.fail_on_warnings {
        " --fail-on-warnings"
    } else {
        ""
    };

    format!(
        r#"#!/bin/sh
# RepoLens Git Hook - pre-push
# This hook was automatically installed by RepoLens.
# It runs a full audit before allowing a push.
#
# To skip this hook, use: git push --no-verify

set -e

echo "RepoLens: Running full audit before push..."

if ! command -v repolens >/dev/null 2>&1; then
    echo "Warning: repolens is not installed or not in PATH. Skipping pre-push check."
    echo "Install it with: cargo install repolens"
    exit 0
fi

if ! repolens plan --format terminal{fail_on_warnings} 2>/dev/null; then
    echo ""
    echo "RepoLens: Audit issues found! Push aborted."
    echo "Please fix the issues before pushing."
    echo "To skip this check, use: git push --no-verify"
    exit 1
fi

echo "RepoLens: Audit passed. Proceeding with push."
"#
    )
}

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

    /// Helper to create a temporary git repository structure
    fn create_temp_repo() -> TempDir {
        let temp_dir = TempDir::new().unwrap();
        let git_dir = temp_dir.path().join(".git");
        fs::create_dir_all(git_dir.join("hooks")).unwrap();
        temp_dir
    }

    /// Helper to create a temporary git worktree structure
    fn create_temp_worktree() -> (TempDir, TempDir) {
        // Main repo
        let main_repo = TempDir::new().unwrap();
        let main_git_dir = main_repo.path().join(".git");
        fs::create_dir_all(main_git_dir.join("worktrees").join("wt1")).unwrap();
        fs::create_dir_all(main_git_dir.join("hooks")).unwrap();

        // Worktree
        let worktree = TempDir::new().unwrap();
        let wt_git_dir = main_git_dir.join("worktrees").join("wt1");
        fs::create_dir_all(&wt_git_dir).unwrap();

        // .git file in worktree pointing to the worktree git dir
        let git_file_content = format!("gitdir: {}", wt_git_dir.display());
        fs::write(worktree.path().join(".git"), git_file_content).unwrap();
        fs::create_dir_all(wt_git_dir.join("hooks")).unwrap();

        (main_repo, worktree)
    }

    #[test]
    fn test_hooks_config_default() {
        let config = HooksConfig::default();
        assert!(config.pre_commit);
        assert!(config.pre_push);
        assert!(!config.fail_on_warnings);
    }

    #[test]
    fn test_find_hooks_dir_standard_repo() {
        let temp_dir = create_temp_repo();
        let hooks_dir = find_hooks_dir(temp_dir.path()).unwrap();
        assert_eq!(hooks_dir, temp_dir.path().join(".git/hooks"));
    }

    #[test]
    fn test_find_hooks_dir_worktree() {
        let (_main_repo, worktree) = create_temp_worktree();
        let hooks_dir = find_hooks_dir(worktree.path()).unwrap();
        assert!(hooks_dir.to_string_lossy().contains("hooks"));
    }

    #[test]
    fn test_find_hooks_dir_not_a_repo() {
        let temp_dir = TempDir::new().unwrap();
        let result = find_hooks_dir(temp_dir.path());
        assert!(result.is_err());
    }

    #[test]
    fn test_hooks_manager_new() {
        let temp_dir = create_temp_repo();
        let config = HooksConfig::default();
        let manager = HooksManager::new(temp_dir.path(), config);
        assert!(manager.is_ok());
    }

    #[test]
    fn test_hooks_manager_new_not_a_repo() {
        let temp_dir = TempDir::new().unwrap();
        let config = HooksConfig::default();
        let result = HooksManager::new(temp_dir.path(), config);
        assert!(result.is_err());
    }

    #[test]
    fn test_install_all_hooks() {
        let temp_dir = create_temp_repo();
        let config = HooksConfig::default();
        let manager = HooksManager::new(temp_dir.path(), config).unwrap();

        let messages = manager.install(false).unwrap();
        assert_eq!(messages.len(), 2);

        // Verify files exist
        assert!(temp_dir.path().join(".git/hooks/pre-commit").exists());
        assert!(temp_dir.path().join(".git/hooks/pre-push").exists());
    }

    #[test]
    fn test_install_pre_commit_only() {
        let temp_dir = create_temp_repo();
        let config = HooksConfig {
            pre_commit: true,
            pre_push: false,
            fail_on_warnings: false,
        };
        let manager = HooksManager::new(temp_dir.path(), config).unwrap();

        let messages = manager.install(false).unwrap();
        assert_eq!(messages.len(), 1);
        assert!(messages[0].contains("pre-commit"));

        assert!(temp_dir.path().join(".git/hooks/pre-commit").exists());
        assert!(!temp_dir.path().join(".git/hooks/pre-push").exists());
    }

    #[test]
    fn test_install_pre_push_only() {
        let temp_dir = create_temp_repo();
        let config = HooksConfig {
            pre_commit: false,
            pre_push: true,
            fail_on_warnings: false,
        };
        let manager = HooksManager::new(temp_dir.path(), config).unwrap();

        let messages = manager.install(false).unwrap();
        assert_eq!(messages.len(), 1);
        assert!(messages[0].contains("pre-push"));

        assert!(!temp_dir.path().join(".git/hooks/pre-commit").exists());
        assert!(temp_dir.path().join(".git/hooks/pre-push").exists());
    }

    #[test]
    fn test_install_existing_hook_without_force() {
        let temp_dir = create_temp_repo();
        let hook_path = temp_dir.path().join(".git/hooks/pre-commit");
        fs::write(&hook_path, "#!/bin/sh\necho 'existing hook'").unwrap();

        let config = HooksConfig {
            pre_commit: true,
            pre_push: false,
            fail_on_warnings: false,
        };
        let manager = HooksManager::new(temp_dir.path(), config).unwrap();

        let result = manager.install(false);
        assert!(result.is_err());
    }

    #[test]
    fn test_install_existing_hook_with_force() {
        let temp_dir = create_temp_repo();
        let hook_path = temp_dir.path().join(".git/hooks/pre-commit");
        fs::write(&hook_path, "#!/bin/sh\necho 'existing hook'").unwrap();

        let config = HooksConfig {
            pre_commit: true,
            pre_push: false,
            fail_on_warnings: false,
        };
        let manager = HooksManager::new(temp_dir.path(), config).unwrap();

        let messages = manager.install(true).unwrap();
        assert_eq!(messages.len(), 1);
        assert!(messages[0].contains("backed up"));

        // Verify backup exists
        let backup_path = temp_dir
            .path()
            .join(".git/hooks/pre-commit.repolens-backup");
        assert!(backup_path.exists());
        let backup_content = fs::read_to_string(&backup_path).unwrap();
        assert!(backup_content.contains("existing hook"));

        // Verify new hook is installed
        let new_content = fs::read_to_string(&hook_path).unwrap();
        assert!(new_content.contains("RepoLens Git Hook"));
    }

    #[test]
    fn test_install_overwrites_existing_repolens_hook() {
        let temp_dir = create_temp_repo();
        let hook_path = temp_dir.path().join(".git/hooks/pre-commit");
        fs::write(
            &hook_path,
            "#!/bin/sh\n# RepoLens Git Hook\necho 'old version'",
        )
        .unwrap();

        let config = HooksConfig {
            pre_commit: true,
            pre_push: false,
            fail_on_warnings: false,
        };
        let manager = HooksManager::new(temp_dir.path(), config).unwrap();

        let messages = manager.install(false).unwrap();
        assert_eq!(messages.len(), 1);
        assert!(messages[0].contains("Updated"));

        // No backup should be created for RepoLens hooks
        let backup_path = temp_dir
            .path()
            .join(".git/hooks/pre-commit.repolens-backup");
        assert!(!backup_path.exists());
    }

    #[test]
    fn test_remove_hooks() {
        let temp_dir = create_temp_repo();
        let config = HooksConfig::default();
        let manager = HooksManager::new(temp_dir.path(), config).unwrap();

        // First install
        manager.install(false).unwrap();

        // Then remove
        let messages = manager.remove().unwrap();
        assert_eq!(messages.len(), 2);

        assert!(!temp_dir.path().join(".git/hooks/pre-commit").exists());
        assert!(!temp_dir.path().join(".git/hooks/pre-push").exists());
    }

    #[test]
    fn test_remove_nonexistent_hooks() {
        let temp_dir = create_temp_repo();
        let config = HooksConfig::default();
        let manager = HooksManager::new(temp_dir.path(), config).unwrap();

        let messages = manager.remove().unwrap();
        assert_eq!(messages.len(), 2);
        assert!(messages[0].contains("No"));
        assert!(messages[1].contains("No"));
    }

    #[test]
    fn test_remove_non_repolens_hook() {
        let temp_dir = create_temp_repo();
        let hook_path = temp_dir.path().join(".git/hooks/pre-commit");
        fs::write(&hook_path, "#!/bin/sh\necho 'custom hook'").unwrap();

        let config = HooksConfig::default();
        let manager = HooksManager::new(temp_dir.path(), config).unwrap();

        let messages = manager.remove().unwrap();
        assert!(messages[0].contains("Skipped"));

        // Hook should still exist
        assert!(hook_path.exists());
    }

    #[test]
    fn test_remove_with_backup_restoration() {
        let temp_dir = create_temp_repo();
        let hook_path = temp_dir.path().join(".git/hooks/pre-commit");
        let backup_path = temp_dir
            .path()
            .join(".git/hooks/pre-commit.repolens-backup");

        // Create existing hook then force-install
        fs::write(&hook_path, "#!/bin/sh\necho 'original hook'").unwrap();
        let config = HooksConfig {
            pre_commit: true,
            pre_push: false,
            fail_on_warnings: false,
        };
        let manager = HooksManager::new(temp_dir.path(), config).unwrap();
        manager.install(true).unwrap();

        // Verify backup exists
        assert!(backup_path.exists());

        // Remove hook - should restore backup
        let messages = manager.remove().unwrap();
        assert!(messages[0].contains("restored"));

        // Original hook should be restored
        let content = fs::read_to_string(&hook_path).unwrap();
        assert!(content.contains("original hook"));

        // Backup should be gone
        assert!(!backup_path.exists());
    }

    #[test]
    fn test_generate_pre_commit_hook_content() {
        let config = HooksConfig::default();
        let content = generate_pre_commit_hook(&config);

        assert!(content.starts_with("#!/bin/sh"));
        assert!(content.contains("# RepoLens Git Hook"));
        assert!(content.contains("pre-commit"));
        assert!(content.contains("repolens plan --only secrets"));
        assert!(content.contains("--no-verify"));
        assert!(!content.contains("--fail-on-warnings"));
    }

    #[test]
    fn test_generate_pre_commit_hook_with_fail_on_warnings() {
        let config = HooksConfig {
            pre_commit: true,
            pre_push: true,
            fail_on_warnings: true,
        };
        let content = generate_pre_commit_hook(&config);
        assert!(content.contains("--fail-on-warnings"));
    }

    #[test]
    fn test_generate_pre_push_hook_content() {
        let config = HooksConfig::default();
        let content = generate_pre_push_hook(&config);

        assert!(content.starts_with("#!/bin/sh"));
        assert!(content.contains("# RepoLens Git Hook"));
        assert!(content.contains("pre-push"));
        assert!(content.contains("repolens plan"));
        assert!(content.contains("--no-verify"));
        assert!(!content.contains("--fail-on-warnings"));
    }

    #[test]
    fn test_generate_pre_push_hook_with_fail_on_warnings() {
        let config = HooksConfig {
            pre_commit: true,
            pre_push: true,
            fail_on_warnings: true,
        };
        let content = generate_pre_push_hook(&config);
        assert!(content.contains("--fail-on-warnings"));
    }

    #[test]
    fn test_is_repolens_hook() {
        let temp_dir = TempDir::new().unwrap();

        // RepoLens hook
        let repolens_hook = temp_dir.path().join("repolens-hook");
        fs::write(&repolens_hook, "#!/bin/sh\n# RepoLens Git Hook\necho test").unwrap();
        assert!(is_repolens_hook(&repolens_hook).unwrap());

        // Non-RepoLens hook
        let other_hook = temp_dir.path().join("other-hook");
        fs::write(&other_hook, "#!/bin/sh\necho test").unwrap();
        assert!(!is_repolens_hook(&other_hook).unwrap());
    }

    #[test]
    fn test_write_hook_file() {
        let temp_dir = TempDir::new().unwrap();
        let hook_path = temp_dir.path().join("test-hook");
        let content = "#!/bin/sh\necho 'test'";

        write_hook_file(&hook_path, content).unwrap();

        let written = fs::read_to_string(&hook_path).unwrap();
        assert_eq!(written, content);

        // Check permissions on Unix
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            let metadata = fs::metadata(&hook_path).unwrap();
            let mode = metadata.permissions().mode();
            assert_eq!(mode & 0o755, 0o755);
        }
    }

    #[test]
    fn test_hooks_manager_hooks_dir() {
        let temp_dir = create_temp_repo();
        let config = HooksConfig::default();
        let manager = HooksManager::new(temp_dir.path(), config).unwrap();
        assert_eq!(manager.hooks_dir(), temp_dir.path().join(".git/hooks"));
    }

    #[test]
    fn test_install_creates_hooks_dir_if_missing() {
        let temp_dir = TempDir::new().unwrap();
        // Create .git dir but not hooks subdir
        let git_dir = temp_dir.path().join(".git");
        fs::create_dir_all(&git_dir).unwrap();

        let config = HooksConfig {
            pre_commit: true,
            pre_push: false,
            fail_on_warnings: false,
        };
        let manager = HooksManager::new(temp_dir.path(), config).unwrap();

        let messages = manager.install(false).unwrap();
        assert_eq!(messages.len(), 1);
        assert!(temp_dir.path().join(".git/hooks/pre-commit").exists());
    }

    #[test]
    fn test_install_no_hooks_selected() {
        let temp_dir = create_temp_repo();
        let config = HooksConfig {
            pre_commit: false,
            pre_push: false,
            fail_on_warnings: false,
        };
        let manager = HooksManager::new(temp_dir.path(), config).unwrap();

        let messages = manager.install(false).unwrap();
        assert!(messages.is_empty());
    }

    #[test]
    fn test_find_hooks_dir_invalid_git_file() {
        let temp_dir = TempDir::new().unwrap();
        // Create .git as a file with invalid content
        fs::write(temp_dir.path().join(".git"), "not a gitdir reference").unwrap();

        let result = find_hooks_dir(temp_dir.path());
        assert!(result.is_err());
    }

    #[test]
    fn test_hooks_config_serialize_deserialize() {
        let config = HooksConfig {
            pre_commit: true,
            pre_push: false,
            fail_on_warnings: true,
        };
        let toml_str = toml::to_string(&config).unwrap();
        let deserialized: HooksConfig = toml::from_str(&toml_str).unwrap();
        assert!(deserialized.pre_commit);
        assert!(!deserialized.pre_push);
        assert!(deserialized.fail_on_warnings);
    }

    #[test]
    fn test_hooks_config_deserialize_from_toml() {
        let toml_str = r#"
            pre_commit = false
            pre_push = true
            fail_on_warnings = true
        "#;
        let config: HooksConfig = toml::from_str(toml_str).unwrap();
        assert!(!config.pre_commit);
        assert!(config.pre_push);
        assert!(config.fail_on_warnings);
    }

    #[test]
    fn test_hooks_config_default_serde() {
        // Verify that default values work when fields are missing from TOML
        let toml_str = "";
        let config: HooksConfig = toml::from_str(toml_str).unwrap();
        assert!(config.pre_commit);
        assert!(config.pre_push);
        assert!(!config.fail_on_warnings);
    }

    #[test]
    fn test_find_hooks_dir_worktree_with_absolute_path() {
        // Test worktree with absolute gitdir path
        let main_repo = TempDir::new().unwrap();
        let main_git_dir = main_repo.path().join(".git");
        fs::create_dir_all(main_git_dir.join("worktrees").join("wt-abs")).unwrap();
        fs::create_dir_all(main_git_dir.join("hooks")).unwrap();

        let worktree = TempDir::new().unwrap();
        let wt_git_dir = main_git_dir.join("worktrees").join("wt-abs");
        fs::create_dir_all(wt_git_dir.join("hooks")).unwrap();

        // Write .git file with absolute path
        let git_file_content = format!("gitdir: {}", wt_git_dir.display());
        fs::write(worktree.path().join(".git"), git_file_content).unwrap();

        let hooks_dir = find_hooks_dir(worktree.path()).unwrap();
        assert!(hooks_dir.to_string_lossy().contains("hooks"));
        assert_eq!(hooks_dir, wt_git_dir.join("hooks"));
    }

    #[test]
    fn test_is_repolens_hook_nonexistent_file() {
        let temp_dir = TempDir::new().unwrap();
        let nonexistent = temp_dir.path().join("nonexistent");
        let result = is_repolens_hook(&nonexistent);
        assert!(result.is_err());
    }

    #[test]
    fn test_install_hook_content_contains_expected_strings() {
        let temp_dir = create_temp_repo();
        let config = HooksConfig {
            pre_commit: true,
            pre_push: true,
            fail_on_warnings: false,
        };
        let manager = HooksManager::new(temp_dir.path(), config).unwrap();
        manager.install(false).unwrap();

        // Verify pre-commit content
        let pre_commit_content =
            fs::read_to_string(temp_dir.path().join(".git/hooks/pre-commit")).unwrap();
        assert!(pre_commit_content.contains("#!/bin/sh"));
        assert!(pre_commit_content.contains("# RepoLens Git Hook"));
        assert!(pre_commit_content.contains("repolens plan --only secrets"));

        // Verify pre-push content
        let pre_push_content =
            fs::read_to_string(temp_dir.path().join(".git/hooks/pre-push")).unwrap();
        assert!(pre_push_content.contains("#!/bin/sh"));
        assert!(pre_push_content.contains("# RepoLens Git Hook"));
        assert!(pre_push_content.contains("repolens plan --format terminal"));
    }

    #[test]
    fn test_install_then_reinstall_repolens_hooks() {
        let temp_dir = create_temp_repo();
        let config = HooksConfig::default();
        let manager = HooksManager::new(temp_dir.path(), config).unwrap();

        // Install once
        let messages = manager.install(false).unwrap();
        assert_eq!(messages.len(), 2);
        assert!(messages[0].contains("Installed"));
        assert!(messages[1].contains("Installed"));

        // Install again - should update existing RepoLens hooks
        let messages = manager.install(false).unwrap();
        assert_eq!(messages.len(), 2);
        assert!(messages[0].contains("Updated"));
        assert!(messages[1].contains("Updated"));
    }

    #[test]
    fn test_remove_pre_push_hook() {
        let temp_dir = create_temp_repo();
        let config = HooksConfig::default();
        let manager = HooksManager::new(temp_dir.path(), config).unwrap();

        // Install both hooks
        manager.install(false).unwrap();
        assert!(temp_dir.path().join(".git/hooks/pre-push").exists());

        // Remove all hooks
        let messages = manager.remove().unwrap();
        assert_eq!(messages.len(), 2);
        assert!(messages[0].contains("Removed pre-commit"));
        assert!(messages[1].contains("Removed pre-push"));

        assert!(!temp_dir.path().join(".git/hooks/pre-commit").exists());
        assert!(!temp_dir.path().join(".git/hooks/pre-push").exists());
    }

    #[test]
    fn test_force_install_pre_push_with_existing() {
        let temp_dir = create_temp_repo();
        let hook_path = temp_dir.path().join(".git/hooks/pre-push");
        fs::write(&hook_path, "#!/bin/sh\necho 'existing pre-push'").unwrap();

        let config = HooksConfig {
            pre_commit: false,
            pre_push: true,
            fail_on_warnings: false,
        };
        let manager = HooksManager::new(temp_dir.path(), config).unwrap();

        // Force install should back up and replace
        let messages = manager.install(true).unwrap();
        assert_eq!(messages.len(), 1);
        assert!(messages[0].contains("backed up"));

        let backup_path = temp_dir.path().join(".git/hooks/pre-push.repolens-backup");
        assert!(backup_path.exists());
    }

    #[test]
    fn test_write_hook_file_error_on_invalid_path() {
        let result = write_hook_file(Path::new("/nonexistent/dir/hook"), "content");
        assert!(result.is_err());
    }

    #[test]
    fn test_hooks_config_clone() {
        let config = HooksConfig {
            pre_commit: false,
            pre_push: true,
            fail_on_warnings: true,
        };
        let cloned = config.clone();
        assert_eq!(cloned.pre_commit, config.pre_commit);
        assert_eq!(cloned.pre_push, config.pre_push);
        assert_eq!(cloned.fail_on_warnings, config.fail_on_warnings);
    }

    #[test]
    fn test_hooks_config_debug() {
        let config = HooksConfig::default();
        let debug_str = format!("{:?}", config);
        assert!(debug_str.contains("HooksConfig"));
        assert!(debug_str.contains("pre_commit"));
    }

    #[test]
    fn test_hooks_manager_debug() {
        let temp_dir = create_temp_repo();
        let config = HooksConfig::default();
        let manager = HooksManager::new(temp_dir.path(), config).unwrap();
        let debug_str = format!("{:?}", manager);
        assert!(debug_str.contains("HooksManager"));
    }

    #[test]
    fn test_find_hooks_dir_worktree_with_relative_path() {
        // Test worktree with relative gitdir path
        let temp_dir = TempDir::new().unwrap();

        // Create structure: temp_dir/.git/worktrees/wt-rel/hooks
        let main_git_dir = temp_dir.path().join(".git");
        let wt_git_dir = main_git_dir.join("worktrees").join("wt-rel");
        fs::create_dir_all(wt_git_dir.join("hooks")).unwrap();

        // Create worktree subdirectory
        let worktree_dir = temp_dir.path().join("worktree");
        fs::create_dir_all(&worktree_dir).unwrap();

        // Write .git file with relative path (relative from worktree_dir)
        // The relative path from worktree_dir to wt_git_dir is "../.git/worktrees/wt-rel"
        let git_file_content = "gitdir: ../.git/worktrees/wt-rel";
        fs::write(worktree_dir.join(".git"), git_file_content).unwrap();

        let hooks_dir = find_hooks_dir(&worktree_dir).unwrap();
        assert!(hooks_dir.to_string_lossy().contains("hooks"));
    }

    #[test]
    fn test_remove_hook_restores_backup_for_pre_push() {
        let temp_dir = create_temp_repo();
        let hook_path = temp_dir.path().join(".git/hooks/pre-push");
        let backup_path = temp_dir.path().join(".git/hooks/pre-push.repolens-backup");

        // Create existing hook then force-install
        fs::write(&hook_path, "#!/bin/sh\necho 'original pre-push'").unwrap();
        let config = HooksConfig {
            pre_commit: false,
            pre_push: true,
            fail_on_warnings: false,
        };
        let manager = HooksManager::new(temp_dir.path(), config).unwrap();
        manager.install(true).unwrap();

        // Verify backup exists
        assert!(backup_path.exists());

        // Remove hook - should restore backup
        let messages = manager.remove().unwrap();
        // First message is about pre-commit (no hook to remove)
        // Second message is about pre-push (restored)
        assert!(messages[1].contains("restored"));

        // Original hook should be restored
        let content = fs::read_to_string(&hook_path).unwrap();
        assert!(content.contains("original pre-push"));

        // Backup should be gone
        assert!(!backup_path.exists());
    }

    #[test]
    fn test_install_both_hooks_with_force_over_existing() {
        let temp_dir = create_temp_repo();
        let pre_commit_path = temp_dir.path().join(".git/hooks/pre-commit");
        let pre_push_path = temp_dir.path().join(".git/hooks/pre-push");

        // Create existing hooks
        fs::write(&pre_commit_path, "#!/bin/sh\necho 'old commit hook'").unwrap();
        fs::write(&pre_push_path, "#!/bin/sh\necho 'old push hook'").unwrap();

        let config = HooksConfig::default();
        let manager = HooksManager::new(temp_dir.path(), config).unwrap();

        let messages = manager.install(true).unwrap();
        assert_eq!(messages.len(), 2);
        assert!(messages[0].contains("backed up"));
        assert!(messages[1].contains("backed up"));

        // Verify both backups exist
        assert!(
            temp_dir
                .path()
                .join(".git/hooks/pre-commit.repolens-backup")
                .exists()
        );
        assert!(
            temp_dir
                .path()
                .join(".git/hooks/pre-push.repolens-backup")
                .exists()
        );

        // Verify new hooks are installed
        let commit_content = fs::read_to_string(&pre_commit_path).unwrap();
        assert!(commit_content.contains("RepoLens Git Hook"));
        let push_content = fs::read_to_string(&pre_push_path).unwrap();
        assert!(push_content.contains("RepoLens Git Hook"));
    }

    #[test]
    fn test_generate_hooks_scripts_structure() {
        let config = HooksConfig {
            pre_commit: true,
            pre_push: true,
            fail_on_warnings: false,
        };

        let pre_commit = generate_pre_commit_hook(&config);
        // Check script structure
        assert!(pre_commit.contains("set -e"));
        assert!(pre_commit.contains("command -v repolens"));
        assert!(pre_commit.contains("cargo install repolens"));
        assert!(pre_commit.contains("exit 0"));
        assert!(pre_commit.contains("exit 1"));

        let pre_push = generate_pre_push_hook(&config);
        assert!(pre_push.contains("set -e"));
        assert!(pre_push.contains("command -v repolens"));
        assert!(pre_push.contains("cargo install repolens"));
        assert!(pre_push.contains("exit 0"));
        assert!(pre_push.contains("exit 1"));
    }

    #[cfg(unix)]
    #[test]
    fn test_install_fails_when_hooks_dir_not_creatable() {
        use std::os::unix::fs::PermissionsExt;

        let temp_dir = TempDir::new().unwrap();
        let git_dir = temp_dir.path().join(".git");
        fs::create_dir_all(&git_dir).unwrap();
        // Make .git read-only so hooks dir can't be created
        fs::set_permissions(&git_dir, fs::Permissions::from_mode(0o444)).unwrap();

        let config = HooksConfig::default();
        let manager = HooksManager::new(temp_dir.path(), config).unwrap();

        let result = manager.install(false);
        assert!(result.is_err());

        // Restore permissions for cleanup
        fs::set_permissions(&git_dir, fs::Permissions::from_mode(0o755)).unwrap();
    }

    #[cfg(unix)]
    #[test]
    fn test_remove_hook_fails_when_file_not_removable() {
        use std::os::unix::fs::PermissionsExt;

        let temp_dir = create_temp_repo();
        let hooks_dir = temp_dir.path().join(".git/hooks");
        let config = HooksConfig::default();
        let manager = HooksManager::new(temp_dir.path(), config).unwrap();

        // Install hooks first
        manager.install(false).unwrap();

        // Make hooks directory read+execute only (can read files but can't delete)
        fs::set_permissions(&hooks_dir, fs::Permissions::from_mode(0o555)).unwrap();

        let result = manager.remove();
        assert!(result.is_err());

        // Restore permissions for cleanup
        fs::set_permissions(&hooks_dir, fs::Permissions::from_mode(0o755)).unwrap();
    }

    #[cfg(unix)]
    #[test]
    fn test_install_force_fails_when_backup_not_possible() {
        use std::os::unix::fs::PermissionsExt;

        let temp_dir = create_temp_repo();
        let hooks_dir = temp_dir.path().join(".git/hooks");
        let hook_path = hooks_dir.join("pre-commit");

        // Create an existing non-RepoLens hook
        fs::write(&hook_path, "#!/bin/sh\necho 'existing'").unwrap();

        // Make the hook file writable but the directory read-only + execute only
        // This prevents creating new files (backup) but allows reading existing ones
        fs::set_permissions(&hooks_dir, fs::Permissions::from_mode(0o555)).unwrap();

        let config = HooksConfig {
            pre_commit: true,
            pre_push: false,
            fail_on_warnings: false,
        };
        let manager = HooksManager::new(temp_dir.path(), config).unwrap();

        let result = manager.install(true);
        assert!(result.is_err());

        // Restore permissions for cleanup
        fs::set_permissions(&hooks_dir, fs::Permissions::from_mode(0o755)).unwrap();
    }

    #[test]
    fn test_remove_all_hooks_with_mixed_state() {
        let temp_dir = create_temp_repo();

        // Install only pre-commit as a RepoLens hook
        let config = HooksConfig {
            pre_commit: true,
            pre_push: false,
            fail_on_warnings: false,
        };
        let manager = HooksManager::new(temp_dir.path(), config).unwrap();
        manager.install(false).unwrap();

        // Add a non-RepoLens pre-push hook
        let pre_push_path = temp_dir.path().join(".git/hooks/pre-push");
        fs::write(&pre_push_path, "#!/bin/sh\necho 'custom push hook'").unwrap();

        // Remove all - should remove pre-commit but skip pre-push
        let full_config = HooksConfig::default();
        let full_manager = HooksManager::new(temp_dir.path(), full_config).unwrap();
        let messages = full_manager.remove().unwrap();

        assert!(messages[0].contains("Removed pre-commit"));
        assert!(messages[1].contains("Skipped"));

        // pre-commit should be gone, pre-push should remain
        assert!(!temp_dir.path().join(".git/hooks/pre-commit").exists());
        assert!(pre_push_path.exists());
    }
}