sr-core 3.4.0

Pure domain logic for sr
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
//! Git hook management — sync, execution, and validation.
//!
//! Keeps `.githooks/` in sync with the `hooks` section of `sr.yaml` and
//! provides the runtime for executing configured hook entries.

use std::collections::BTreeSet;
use std::hash::{Hash, Hasher};
use std::path::Path;

use crate::config::{DEFAULT_CONFIG_FILE, HookEntry, HooksConfig, ReleaseConfig};
use crate::error::ReleaseError;
use crate::hook_cache;

/// Marker comment embedded in generated hook scripts to identify sr-managed hooks.
const GENERATED_MARKER: &str = "# Generated by sr";

/// File storing the hash of the hooks config for staleness detection.
const HASH_FILE: &str = ".sr-hooks-hash";

/// Sync `.githooks/` with the hooks config. Returns `Ok(true)` if changes were made.
///
/// - Writes shim scripts for configured hooks
/// - Removes sr-managed shims no longer in config
/// - Backs up conflicting non-sr-managed hooks
/// - Updates the hash file
/// - Sets `core.hooksPath`
pub fn sync_hooks(
    repo_root: &Path,
    config: &HooksConfig,
) -> Result<bool, crate::error::ReleaseError> {
    let hooks_dir = repo_root.join(".githooks");
    let hash_path = hooks_dir.join(HASH_FILE);
    let current_hash = config_hash(config);

    // Fast path: already in sync.
    if let Ok(stored) = std::fs::read_to_string(&hash_path)
        && stored.trim() == current_hash
    {
        return Ok(false);
    }

    let configured: BTreeSet<&str> = config
        .hooks
        .iter()
        .filter(|(_, entries)| !entries.is_empty())
        .map(|(name, _)| name.as_str())
        .collect();

    if configured.is_empty() {
        let removed = remove_stale_hooks(&hooks_dir, &configured)?;
        // Clean up hash file too.
        let _ = std::fs::remove_file(&hash_path);
        return Ok(removed);
    }

    std::fs::create_dir_all(&hooks_dir).map_err(|e| {
        crate::error::ReleaseError::Config(format!("failed to create .githooks: {e}"))
    })?;

    let mut changed = false;

    for &hook_name in &configured {
        let hook_path = hooks_dir.join(hook_name);
        let expected = shim_script(hook_name);

        match std::fs::read_to_string(&hook_path) {
            Ok(existing) if existing == expected => {
                // Already correct.
            }
            Ok(existing) if existing.contains(GENERATED_MARKER) => {
                // Sr-managed but outdated — overwrite.
                write_shim(&hook_path, &expected)?;
                changed = true;
            }
            Ok(_) => {
                // Non-sr-managed hook conflicts — back up and replace.
                let backup = hooks_dir.join(format!("{hook_name}.bak"));
                std::fs::rename(&hook_path, &backup).map_err(|e| {
                    crate::error::ReleaseError::Config(format!(
                        "failed to backup .githooks/{hook_name}: {e}"
                    ))
                })?;
                eprintln!("backed up .githooks/{hook_name} → .githooks/{hook_name}.bak");
                write_shim(&hook_path, &expected)?;
                changed = true;
            }
            Err(_) => {
                // Does not exist — create.
                write_shim(&hook_path, &expected)?;
                changed = true;
            }
        }
    }

    if remove_stale_hooks(&hooks_dir, &configured)? {
        changed = true;
    }

    // Write hash file.
    std::fs::write(&hash_path, &current_hash).map_err(|e| {
        crate::error::ReleaseError::Config(format!("failed to write hooks hash: {e}"))
    })?;

    if changed {
        set_hooks_path(repo_root);
    }

    Ok(changed)
}

/// Check whether hooks need syncing (cheap hash comparison).
pub fn needs_sync(repo_root: &Path, config: &HooksConfig) -> bool {
    let hash_path = repo_root.join(".githooks").join(HASH_FILE);
    match std::fs::read_to_string(&hash_path) {
        Ok(stored) => stored.trim() != config_hash(config),
        Err(_) => {
            // No hash file — need sync if there are hooks configured.
            !config.hooks.is_empty()
        }
    }
}

/// Compute a deterministic hash of the hooks config.
fn config_hash(config: &HooksConfig) -> String {
    let json = serde_json::to_string(&config.hooks).unwrap_or_default();
    let mut hasher = std::collections::hash_map::DefaultHasher::new();
    json.hash(&mut hasher);
    format!("{:016x}", hasher.finish())
}

/// Generate the canonical shim script for a hook.
fn shim_script(hook_name: &str) -> String {
    format!(
        "#!/usr/bin/env sh\n\
         {GENERATED_MARKER} — edit the hooks section in {config} to modify.\n\
         exec sr hook run {hook_name} -- \"$@\"\n",
        config = DEFAULT_CONFIG_FILE,
    )
}

/// Write a shim script and set it executable.
fn write_shim(path: &Path, content: &str) -> Result<(), crate::error::ReleaseError> {
    std::fs::write(path, content)
        .map_err(|e| crate::error::ReleaseError::Config(format!("failed to write hook: {e}")))?;

    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o755)).map_err(|e| {
            crate::error::ReleaseError::Config(format!("failed to chmod hook: {e}"))
        })?;
    }

    if let Some(name) = path.file_name().and_then(|n| n.to_str()) {
        eprintln!("synced .githooks/{name}");
    }

    Ok(())
}

/// Remove sr-managed hooks not in the configured set. Returns `true` if any were removed.
fn remove_stale_hooks(
    hooks_dir: &Path,
    configured: &BTreeSet<&str>,
) -> Result<bool, crate::error::ReleaseError> {
    if !hooks_dir.is_dir() {
        return Ok(false);
    }

    let mut removed = false;
    let entries = std::fs::read_dir(hooks_dir).map_err(|e| {
        crate::error::ReleaseError::Config(format!("failed to read .githooks: {e}"))
    })?;

    for entry in entries {
        let entry = entry.map_err(|e| crate::error::ReleaseError::Config(e.to_string()))?;
        let path = entry.path();

        if !path.is_file() {
            continue;
        }

        let name = match path.file_name().and_then(|n| n.to_str()) {
            Some(n) => n.to_string(),
            None => continue,
        };

        // Skip the hash file and backup files.
        if name == HASH_FILE || name.ends_with(".bak") {
            continue;
        }

        // Only remove sr-managed hooks.
        if !is_sr_managed(&path) {
            continue;
        }

        if !configured.contains(name.as_str()) {
            std::fs::remove_file(&path).map_err(|e| {
                crate::error::ReleaseError::Config(format!(
                    "failed to remove .githooks/{name}: {e}"
                ))
            })?;
            eprintln!("removed stale .githooks/{name}");
            removed = true;
        }
    }

    Ok(removed)
}

/// Check if a hook file was generated by sr.
fn is_sr_managed(path: &Path) -> bool {
    std::fs::read_to_string(path)
        .map(|content| content.contains(GENERATED_MARKER))
        .unwrap_or(false)
}

/// Set `core.hooksPath` to `.githooks/`.
fn set_hooks_path(repo_root: &Path) {
    let _ = std::process::Command::new("git")
        .args(["config", "core.hooksPath", ".githooks/"])
        .current_dir(repo_root)
        .status();
}

// ---------------------------------------------------------------------------
// Shell execution
// ---------------------------------------------------------------------------

/// Run a shell command (`sh -c`), optionally piping data to stdin and/or
/// injecting environment variables. Returns an error if the command exits
/// non-zero.
pub fn run_shell(
    cmd: &str,
    stdin_data: Option<&str>,
    env: &[(&str, &str)],
) -> Result<(), ReleaseError> {
    let mut child = {
        let mut builder = std::process::Command::new("sh");
        builder.args(["-c", cmd]);
        for &(k, v) in env {
            builder.env(k, v);
        }
        if stdin_data.is_some() {
            builder.stdin(std::process::Stdio::piped());
        } else {
            builder.stdin(std::process::Stdio::inherit());
        }
        builder
            .spawn()
            .map_err(|e| ReleaseError::Hook(format!("{cmd}: {e}")))?
    };

    if let Some(data) = stdin_data
        && let Some(ref mut stdin) = child.stdin
    {
        use std::io::Write;
        let _ = stdin.write_all(data.as_bytes());
    }

    let status = child
        .wait()
        .map_err(|e| ReleaseError::Hook(format!("{cmd}: {e}")))?;

    if !status.success() {
        let code = status.code().unwrap_or(1);
        return Err(ReleaseError::Hook(format!("{cmd} exited with code {code}")));
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// Hook execution
// ---------------------------------------------------------------------------

/// Build a JSON context object for a git hook based on its name and positional args.
pub fn build_hook_json(hook_name: &str, args: &[String]) -> serde_json::Value {
    let mut obj = serde_json::Map::new();
    obj.insert("hook".into(), serde_json::Value::String(hook_name.into()));
    obj.insert(
        "args".into(),
        serde_json::Value::Array(
            args.iter()
                .map(|a| serde_json::Value::String(a.clone()))
                .collect(),
        ),
    );

    // Add named fields for well-known hooks
    match hook_name {
        "commit-msg" => {
            if let Some(f) = args.first() {
                obj.insert("message_file".into(), serde_json::Value::String(f.clone()));
            }
        }
        "prepare-commit-msg" => {
            if let Some(f) = args.first() {
                obj.insert("message_file".into(), serde_json::Value::String(f.clone()));
            }
            if let Some(s) = args.get(1) {
                obj.insert("source".into(), serde_json::Value::String(s.clone()));
            }
            if let Some(s) = args.get(2) {
                obj.insert("sha".into(), serde_json::Value::String(s.clone()));
            }
        }
        "pre-push" => {
            if let Some(r) = args.first() {
                obj.insert("remote_name".into(), serde_json::Value::String(r.clone()));
            }
            if let Some(u) = args.get(1) {
                obj.insert("remote_url".into(), serde_json::Value::String(u.clone()));
            }
        }
        "pre-rebase" => {
            if let Some(u) = args.first() {
                obj.insert("upstream".into(), serde_json::Value::String(u.clone()));
            }
            if let Some(b) = args.get(1) {
                obj.insert("branch".into(), serde_json::Value::String(b.clone()));
            }
        }
        "post-checkout" => {
            if let Some(r) = args.first() {
                obj.insert("prev_ref".into(), serde_json::Value::String(r.clone()));
            }
            if let Some(r) = args.get(1) {
                obj.insert("new_ref".into(), serde_json::Value::String(r.clone()));
            }
            if let Some(f) = args.get(2) {
                obj.insert(
                    "branch_checkout".into(),
                    serde_json::Value::String(f.clone()),
                );
            }
        }
        "post-merge" => {
            if let Some(s) = args.first() {
                obj.insert("squash".into(), serde_json::Value::String(s.clone()));
            }
        }
        _ => {}
    }

    serde_json::Value::Object(obj)
}

/// Get staged files from git (excluding deletes).
fn staged_files() -> Result<Vec<String>, ReleaseError> {
    let output = std::process::Command::new("git")
        .args(["diff", "--cached", "--name-only", "--diff-filter=ACMR"])
        .output()
        .map_err(|e| ReleaseError::Hook(format!("git diff --cached: {e}")))?;
    let stdout = String::from_utf8_lossy(&output.stdout);
    Ok(stdout
        .lines()
        .filter(|l| !l.is_empty())
        .map(|l| l.to_string())
        .collect())
}

/// Match files against glob patterns. Matches against both the full path and the basename.
fn match_files(files: &[String], patterns: &[String]) -> Vec<String> {
    let compiled: Vec<glob::Pattern> = patterns
        .iter()
        .filter_map(|p| glob::Pattern::new(p).ok())
        .collect();

    files
        .iter()
        .filter(|f| {
            let basename = Path::new(f)
                .file_name()
                .and_then(|n| n.to_str())
                .unwrap_or(f);
            compiled
                .iter()
                .any(|pat| pat.matches(f) || pat.matches(basename))
        })
        .cloned()
        .collect()
}

/// Discover the git repo root via `git rev-parse --show-toplevel`.
fn repo_root() -> Option<std::path::PathBuf> {
    let output = std::process::Command::new("git")
        .args(["rev-parse", "--show-toplevel"])
        .output()
        .ok()?;
    if !output.status.success() {
        return None;
    }
    Some(String::from_utf8_lossy(&output.stdout).trim().into())
}

/// Run all entries for a configured git hook.
///
/// Simple entries run as shell commands with JSON context piped to stdin.
/// Step entries match staged files against patterns and run rules only when matches exist.
/// Rules containing `{files}` receive the matched file list.
///
/// Step entries use a per-file content-hash cache so that:
/// - Steps are skipped when all matched files are unchanged since the last pass
/// - `{files}` rules receive only changed files
/// - On failure, earlier successful steps are preserved for the next retry
pub fn run_hook(
    config: &ReleaseConfig,
    hook_name: &str,
    args: &[String],
) -> Result<(), ReleaseError> {
    let entries = config
        .hooks
        .hooks
        .get(hook_name)
        .ok_or_else(|| ReleaseError::Hook(format!("no hook configured for '{hook_name}'")))?;

    if entries.is_empty() {
        return Ok(());
    }

    let json = build_hook_json(hook_name, args);
    let json_str = serde_json::to_string(&json)
        .map_err(|e| ReleaseError::Hook(format!("failed to serialize hook context: {e}")))?;

    // Resolve repo root and load step cache (graceful degradation).
    let no_cache = std::env::var("SR_HOOK_NO_CACHE").is_ok_and(|v| v == "1");
    let root = repo_root();
    let mut step_cache = match (&root, no_cache) {
        (Some(r), false) => Some(hook_cache::load_step_cache(r)),
        _ => None,
    };
    let mut cache_dirty = false;

    // Lazily fetch staged files only when a Step entry needs them.
    let mut cached_staged: Option<Vec<String>> = None;

    let result = run_hook_inner(
        entries,
        hook_name,
        &json_str,
        &mut cached_staged,
        root.as_deref(),
        &mut step_cache,
        &mut cache_dirty,
    );

    // Save cache on both success and failure (preserves partial progress).
    if cache_dirty
        && let (Some(r), Some(cache)) = (&root, &step_cache)
        && let Err(e) = hook_cache::save_step_cache(r, cache)
    {
        eprintln!("warning: failed to save hook cache: {e}");
    }

    result
}

/// Inner loop extracted so we can save the cache in all exit paths.
fn run_hook_inner(
    entries: &[HookEntry],
    hook_name: &str,
    json_str: &str,
    cached_staged: &mut Option<Vec<String>>,
    root: Option<&Path>,
    step_cache: &mut Option<hook_cache::StepCache>,
    cache_dirty: &mut bool,
) -> Result<(), ReleaseError> {
    for entry in entries {
        match entry {
            HookEntry::Simple(cmd) => {
                run_shell(cmd, Some(json_str), &[])?;
            }
            HookEntry::Step {
                step,
                patterns,
                rules,
            } => {
                let all_staged = match cached_staged {
                    Some(files) => files,
                    None => {
                        *cached_staged = Some(staged_files().unwrap_or_default());
                        cached_staged.as_mut().unwrap()
                    }
                };

                if all_staged.is_empty() {
                    eprintln!("{hook_name}: no staged files, skipping steps.");
                    break;
                }

                let matched = match_files(all_staged, patterns);
                if matched.is_empty() {
                    eprintln!("{hook_name} [{step}]: no files match {patterns:?}, skipping.");
                    continue;
                }

                // Compute content hashes and determine which files changed.
                let (changed_files, all_hashes) = match (root, step_cache.as_ref()) {
                    (Some(r), Some(cache)) => {
                        let hashes = hook_cache::hash_staged_files(r, &matched);
                        let diff = hook_cache::changed_files_for_step(cache, step, &hashes);
                        (Some(diff), Some(hashes))
                    }
                    _ => (None, None),
                };

                // Check if we can skip entirely.
                if let Some(ref diff) = changed_files {
                    if diff.changed.is_empty() {
                        eprintln!(
                            "{hook_name} [{step}]: all {} files cached, skipping.",
                            diff.cached.len()
                        );
                        continue;
                    }
                    if !diff.cached.is_empty() {
                        eprintln!(
                            "{hook_name} [{step}]: {} of {} files changed, re-checking.",
                            diff.changed.len(),
                            diff.changed.len() + diff.cached.len()
                        );
                    }
                }

                // Build the file list for rules.
                let effective_files = match &changed_files {
                    Some(diff) => &diff.changed,
                    None => &matched,
                };

                for rule in rules {
                    let cmd = if rule.contains("{files}") {
                        // Only pass changed files to {files} rules.
                        let files_str = effective_files.join(" ");
                        rule.replace("{files}", &files_str)
                    } else {
                        // Non-{files} rules (workspace-wide): run if any file changed.
                        rule.clone()
                    };

                    eprintln!("{hook_name} [{step}]: {cmd}");
                    run_shell(&cmd, None, &[])?;
                }

                // Step passed — record all matched files (including cached ones).
                if let (Some(cache), Some(hashes)) = (step_cache.as_mut(), all_hashes) {
                    hook_cache::record_step_pass(cache, step, &hashes);
                    *cache_dirty = true;
                }
            }
        }
    }

    Ok(())
}

/// Validate a commit message file against the configured conventional commit pattern and types.
/// Reads hook JSON from stdin to get the message_file path.
pub fn validate_commit_msg(config: &ReleaseConfig) -> Result<(), ReleaseError> {
    use std::io::Read;
    let mut input = String::new();
    std::io::stdin()
        .read_to_string(&mut input)
        .map_err(|e| ReleaseError::Hook(format!("failed to read stdin: {e}")))?;

    let json: serde_json::Value = serde_json::from_str(&input)
        .map_err(|e| ReleaseError::Hook(format!("invalid JSON on stdin: {e}")))?;

    let file = json["message_file"]
        .as_str()
        .ok_or_else(|| ReleaseError::Hook("missing 'message_file' in hook JSON".into()))?;

    let content = std::fs::read_to_string(file)
        .map_err(|e| ReleaseError::Hook(format!("cannot read commit message file: {e}")))?;

    let first_line = content.lines().next().unwrap_or("").trim();

    // Allow merge commits
    if first_line.starts_with("Merge ") {
        return Ok(());
    }

    // Allow fixup/squash/amend commits (from rebase -i)
    if first_line.starts_with("fixup! ")
        || first_line.starts_with("squash! ")
        || first_line.starts_with("amend! ")
    {
        return Ok(());
    }

    let re = regex::Regex::new(&config.commit_pattern)
        .map_err(|e| ReleaseError::Hook(format!("invalid commit_pattern: {e}")))?;

    if !re.is_match(first_line) {
        let type_names: Vec<&str> = config.types.iter().map(|t| t.name.as_str()).collect();
        return Err(ReleaseError::Hook(format!(
            "commit message does not follow Conventional Commits.\n\n\
             \x20 Expected: <type>(<scope>): <description>\n\
             \x20 Got:      {first_line}\n\n\
             \x20 Valid types: {}\n\
             \x20 Breaking:    append '!' before the colon, e.g. feat!: ...\n\n\
             \x20 Examples:\n\
             \x20   feat: add release dry-run flag\n\
             \x20   fix(core): handle empty tag list\n\
             \x20   feat!: redesign config format",
            type_names.join(", "),
        )));
    }

    // Extract and validate the type
    if let Some(caps) = re.captures(first_line) {
        let msg_type = caps.name("type").map(|m| m.as_str()).unwrap_or_default();

        if !config.types.iter().any(|t| t.name == msg_type) {
            let type_names: Vec<&str> = config.types.iter().map(|t| t.name.as_str()).collect();
            return Err(ReleaseError::Hook(format!(
                "commit type '{msg_type}' is not allowed.\n\n\
                 \x20 Valid types: {}",
                type_names.join(", "),
            )));
        }
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::HookEntry;
    use std::collections::BTreeMap;

    fn make_config(hooks: &[(&str, Vec<HookEntry>)]) -> HooksConfig {
        let mut map = BTreeMap::new();
        for (name, entries) in hooks {
            map.insert(name.to_string(), entries.clone());
        }
        HooksConfig { hooks: map }
    }

    #[test]
    fn creates_hook_scripts() {
        let dir = tempfile::tempdir().unwrap();
        let config = make_config(&[("pre-commit", vec![HookEntry::Simple("echo hi".into())])]);

        let changed = sync_hooks(dir.path(), &config).unwrap();
        assert!(changed);

        let hook = dir.path().join(".githooks/pre-commit");
        assert!(hook.exists());
        let content = std::fs::read_to_string(&hook).unwrap();
        assert!(content.contains("sr hook run pre-commit"));
        assert!(content.contains(GENERATED_MARKER));
    }

    #[test]
    fn idempotent_returns_false() {
        let dir = tempfile::tempdir().unwrap();
        let config = make_config(&[(
            "commit-msg",
            vec![HookEntry::Simple("sr hook commit-msg".into())],
        )]);

        assert!(sync_hooks(dir.path(), &config).unwrap());
        // Second call — hash matches, no changes.
        assert!(!sync_hooks(dir.path(), &config).unwrap());
    }

    #[test]
    fn removes_stale_hooks() {
        let dir = tempfile::tempdir().unwrap();
        let hooks_dir = dir.path().join(".githooks");
        std::fs::create_dir_all(&hooks_dir).unwrap();

        // Write a sr-managed hook that won't be in config.
        std::fs::write(
            hooks_dir.join("pre-push"),
            format!("{GENERATED_MARKER}\nold script"),
        )
        .unwrap();

        // Write a non-sr-managed hook that should be left alone.
        std::fs::write(hooks_dir.join("post-checkout"), "#!/bin/sh\necho custom").unwrap();

        let config = make_config(&[("pre-commit", vec![HookEntry::Simple("echo hi".into())])]);

        sync_hooks(dir.path(), &config).unwrap();

        assert!(
            !hooks_dir.join("pre-push").exists(),
            "stale sr-managed hook should be removed"
        );
        assert!(
            hooks_dir.join("post-checkout").exists(),
            "non-sr-managed hook should be preserved"
        );
        assert!(hooks_dir.join("pre-commit").exists());
    }

    #[test]
    fn backs_up_conflicting_hooks() {
        let dir = tempfile::tempdir().unwrap();
        let hooks_dir = dir.path().join(".githooks");
        std::fs::create_dir_all(&hooks_dir).unwrap();

        // Write a non-sr-managed hook with the same name as a configured hook.
        let custom_content = "#!/bin/sh\necho custom commit-msg hook";
        std::fs::write(hooks_dir.join("commit-msg"), custom_content).unwrap();

        let config = make_config(&[(
            "commit-msg",
            vec![HookEntry::Simple("sr hook commit-msg".into())],
        )]);

        sync_hooks(dir.path(), &config).unwrap();

        // Original should be backed up.
        let backup = hooks_dir.join("commit-msg.bak");
        assert!(backup.exists());
        assert_eq!(std::fs::read_to_string(&backup).unwrap(), custom_content);

        // New hook should be the shim.
        let content = std::fs::read_to_string(hooks_dir.join("commit-msg")).unwrap();
        assert!(content.contains("sr hook run commit-msg"));
    }

    #[test]
    fn empty_config_cleans_up() {
        let dir = tempfile::tempdir().unwrap();
        let hooks_dir = dir.path().join(".githooks");
        std::fs::create_dir_all(&hooks_dir).unwrap();

        std::fs::write(
            hooks_dir.join("pre-commit"),
            format!("{GENERATED_MARKER}\nscript"),
        )
        .unwrap();
        std::fs::write(hooks_dir.join(".sr-hooks-hash"), "oldhash").unwrap();

        let config = make_config(&[]);
        sync_hooks(dir.path(), &config).unwrap();

        assert!(!hooks_dir.join("pre-commit").exists());
        assert!(!hooks_dir.join(".sr-hooks-hash").exists());
    }

    #[test]
    fn needs_sync_detects_changes() {
        let dir = tempfile::tempdir().unwrap();
        let config = make_config(&[("pre-commit", vec![HookEntry::Simple("echo hi".into())])]);

        assert!(needs_sync(dir.path(), &config));

        sync_hooks(dir.path(), &config).unwrap();
        assert!(!needs_sync(dir.path(), &config));

        // Change config.
        let config2 =
            make_config(&[("pre-commit", vec![HookEntry::Simple("echo changed".into())])]);
        assert!(needs_sync(dir.path(), &config2));
    }
}