git-lfs 0.4.0

Large file storage for git, implemented in Rust
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
//! `git lfs install` / `uninstall`: register or remove the LFS filter
//! configuration and git hooks.

use std::fs;
use std::io;
use std::path::{Path, PathBuf};
use std::process::Command;

use git_lfs_git::{ConfigScope, config, git_dir};

/// Which config file the install/uninstall write goes to. Maps to the
/// scope flag git config takes; `File(path)` becomes `--file=<path>`,
/// the rest are the obvious mappings.
///
/// `Global` and `System` and `File(_)` are "scoped outside the repo" —
/// the success message says "Global Git LFS configuration has been
/// removed." (per t-uninstall test 10's `--file` assertion). `Local`
/// and `Worktree` are per-repo and stay quieter.
#[derive(Debug, Clone)]
pub enum InstallScope {
    Global,
    System,
    Local,
    Worktree,
    File(PathBuf),
}

impl InstallScope {
    fn config_arg(&self) -> String {
        match self {
            Self::Global => "--global".into(),
            Self::System => "--system".into(),
            Self::Local => "--local".into(),
            Self::Worktree => "--worktree".into(),
            Self::File(p) => format!("--file={}", p.display()),
        }
    }

    /// `Local` / `Worktree` are per-repo; everything else operates on
    /// a config file outside the repo and shouldn't try to touch hooks.
    pub fn is_repo_scope(&self) -> bool {
        matches!(self, Self::Local | Self::Worktree)
    }

    /// Whether the success message should say "Global ..." (vs the
    /// quieter "Local ..." used for per-repo scopes). Mirrors
    /// upstream's distinction — `--file` is treated as global-like
    /// since it usually points at `$XDG_CONFIG_HOME/git/config`.
    pub fn announces_global(&self) -> bool {
        matches!(self, Self::Global | Self::System | Self::File(_))
    }
}

/// `filter.lfs.<key>` settings written by install. Order is intentional —
/// `process` is what git actually uses when filter-process is available;
/// `clean`/`smudge` are the per-invocation fallback; `required = true`
/// means git refuses to commit if the filter fails (the safe default).
const FILTER_KEYS: &[(&str, &str)] = &[
    ("filter.lfs.clean", "git-lfs clean -- %f"),
    ("filter.lfs.smudge", "git-lfs smudge -- %f"),
    ("filter.lfs.process", "git-lfs filter-process"),
    ("filter.lfs.required", "true"),
];

/// `--skip-smudge` variant: tell git to invoke smudge/process with `--skip`
/// so pointer text passes through untouched. Use with a follow-up
/// `git lfs pull` to download content on demand.
const FILTER_KEYS_SKIP_SMUDGE: &[(&str, &str)] = &[
    ("filter.lfs.clean", "git-lfs clean -- %f"),
    ("filter.lfs.smudge", "git-lfs smudge --skip -- %f"),
    ("filter.lfs.process", "git-lfs filter-process --skip"),
    ("filter.lfs.required", "true"),
];

/// Previously-shipped filter values we silently overwrite without
/// requiring `--force`. Mirrors upstream's `Upgradeables` map (see
/// `lfs/attribute.go::filterAttribute`). The key is the full
/// `filter.lfs.<x>` config name. The list also includes the *other
/// mode's* current value so toggling between regular and `--skip-smudge`
/// is silent (test 6 of t-install relies on this).
fn upgradeables_for(key: &str, skip_smudge: bool) -> &'static [&'static str] {
    match (key, skip_smudge) {
        ("filter.lfs.clean", false) => &["git-lfs clean %f"],
        ("filter.lfs.clean", true) => &["git-lfs clean %f"],
        ("filter.lfs.smudge", false) => &[
            "git-lfs smudge %f",
            "git-lfs smudge --skip %f",
            "git-lfs smudge --skip -- %f",
        ],
        ("filter.lfs.smudge", true) => &[
            "git-lfs smudge %f",
            "git-lfs smudge --skip %f",
            "git-lfs smudge -- %f",
        ],
        ("filter.lfs.process", false) => &[
            "git-lfs filter",
            "git-lfs filter --skip",
            "git-lfs filter-process --skip",
        ],
        ("filter.lfs.process", true) => &[
            "git-lfs filter",
            "git-lfs filter --skip",
            "git-lfs filter-process",
        ],
        _ => &[],
    }
}

const HOOKS: &[&str] = &["pre-push", "post-checkout", "post-commit", "post-merge"];

/// Hook script template. `{{Command}}` is replaced with the hook type at
/// install time. The PATH check matches the upstream wording verbatim so
/// users see the same error if `git-lfs` later disappears from PATH.
const HOOK_TEMPLATE: &str = "\
#!/bin/sh
command -v git-lfs >/dev/null 2>&1 || { printf >&2 \"\\n%s\\n\\n\" \
\"This repository is configured for Git LFS but 'git-lfs' was not found on your path. \
If you no longer wish to use Git LFS, remove this hook by deleting the '{{Command}}' file \
in the hooks directory (set by 'core.hookspath'; usually '.git/hooks').\"; exit 2; }
git lfs {{Command}} \"$@\"
";

#[derive(Debug, Clone)]
pub struct InstallOptions {
    pub scope: InstallScope,
    pub force: bool,
    /// Skip writing hooks; only set the config.
    pub skip_repo: bool,
    /// Configure smudge/process with `--skip` so pointer text passes
    /// through. `git lfs pull` is the recovery path.
    pub skip_smudge: bool,
}

#[derive(Debug, thiserror::Error)]
pub enum InstallError {
    #[error(transparent)]
    Git(#[from] git_lfs_git::Error),
    #[error(transparent)]
    Io(#[from] io::Error),
    /// `filter.lfs.<x>` is set to a value we don't recognize as our
    /// own (current or any historically-shipped variant). Display
    /// matches upstream's `lfs/attribute.go::Attribute.set` wording so
    /// the t-install grep `(clean|smudge)" attribute should be` lands
    /// on the same substring.
    #[error("the {key:?} attribute should be {wanted:?} but is {existing:?}")]
    FilterAttribute {
        key: String,
        existing: String,
        wanted: String,
    },
    /// One of our hooks exists with user-edited content. Carries the
    /// existing text so the dispatcher can render the upstream-format
    /// "Hook already exists" block (matched verbatim by t-install
    /// test 4 and t-update tests 6/7).
    #[error("hook {hook:?} already exists with different contents")]
    HookConflict { hook: String, existing: String },
    /// `git config <scope> ...` failed (e.g. `--worktree` against a
    /// repo that hasn't enabled `extensions.worktreeConfig`, or
    /// `--local` against a `.git` the user can't write to). The CLI
    /// dispatcher renders this verbatim to stdout (the upstream tests
    /// `grep -E "error running.*git.*config"` against `>out.log`)
    /// and exits 2.
    #[error("error running 'git {}': {stderr}", args.join(" "))]
    ConfigCommandFailed { args: Vec<String>, stderr: String },
}

/// Run the install. Writes the four LFS git hooks when in a repo and
/// `skip_repo` is false; the scope governs which config file gets the
/// `filter.lfs.*` writes.
pub fn install(cwd: &Path, opts: &InstallOptions) -> Result<(), InstallError> {
    set_filter_config(cwd, opts)?;

    let in_repo = git_dir(cwd).is_ok();
    let install_hooks_too = !opts.skip_repo && (opts.scope.is_repo_scope() || in_repo);
    if install_hooks_too {
        install_all_hooks(cwd, opts)?;
    }
    Ok(())
}

fn set_filter_config(cwd: &Path, opts: &InstallOptions) -> Result<(), InstallError> {
    let keys = if opts.skip_smudge {
        FILTER_KEYS_SKIP_SMUDGE
    } else {
        FILTER_KEYS
    };
    for (key, wanted) in keys {
        let current = scoped_get(cwd, &opts.scope, key)?;
        let needs_set = match current.as_deref() {
            None | Some("") => true,
            Some(v) if v == *wanted => false,
            Some(v) if opts.force => {
                let _ = v;
                true
            }
            Some(v) => {
                let upgradeables = upgradeables_for(key, opts.skip_smudge);
                if upgradeables.contains(&v) {
                    true
                } else {
                    return Err(InstallError::FilterAttribute {
                        key: (*key).into(),
                        existing: v.to_owned(),
                        wanted: (*wanted).into(),
                    });
                }
            }
        };
        if needs_set {
            scoped_set(cwd, &opts.scope, key, wanted)?;
        }
    }
    Ok(())
}

/// `git config <scope> --get <key>` for one of the install scopes,
/// including `--file=<path>`. Returns `Ok(None)` when the key isn't
/// set or when the scope is unreachable (matches `git_lfs_git::config`'s
/// permissive interpretation of `git config` exit codes).
fn scoped_get(
    cwd: &Path,
    scope: &InstallScope,
    key: &str,
) -> Result<Option<String>, git_lfs_git::Error> {
    let out = Command::new("git")
        .arg("-C")
        .arg(cwd)
        .args(["config", "--includes"])
        .arg(scope.config_arg())
        .args(["--get", key])
        .output()?;
    match out.status.code() {
        Some(0) => Ok(Some(String::from_utf8_lossy(&out.stdout).trim().to_owned())),
        // 1 = key absent; 2 = multivar (we'll collapse with
        // `--replace-all` on write); 128/129 = scope unreachable.
        Some(1) | Some(2) | Some(128) | Some(129) => Ok(None),
        _ => Err(git_lfs_git::Error::Failed(
            String::from_utf8_lossy(&out.stderr).trim().to_owned(),
        )),
    }
}

/// `git config <scope> --replace-all <key> <value>`. The
/// `--replace-all` flag matches upstream's `git/config.go::Set*`
/// helpers and is what lets `git lfs install --force` collapse a
/// multivar (test 14 of t-install).
fn scoped_set(
    cwd: &Path,
    scope: &InstallScope,
    key: &str,
    value: &str,
) -> Result<(), InstallError> {
    let scope_arg = scope.config_arg();
    let args = vec![
        "config".into(),
        scope_arg.clone(),
        "--replace-all".into(),
        key.into(),
        value.into(),
    ];
    let out = Command::new("git")
        .arg("-C")
        .arg(cwd)
        .arg("config")
        .arg(&scope_arg)
        .args(["--replace-all", key, value])
        .output()?;
    if out.status.success() {
        Ok(())
    } else {
        Err(InstallError::ConfigCommandFailed {
            args,
            stderr: String::from_utf8_lossy(&out.stderr).trim().to_owned(),
        })
    }
}

fn scoped_unset(cwd: &Path, scope: &InstallScope, key: &str) -> Result<(), InstallError> {
    let scope_arg = scope.config_arg();
    let args = vec![
        "config".into(),
        scope_arg.clone(),
        "--unset".into(),
        key.into(),
    ];
    let out = Command::new("git")
        .arg("-C")
        .arg(cwd)
        .arg("config")
        .arg(&scope_arg)
        .args(["--unset", key])
        .output()?;
    match out.status.code() {
        // 0 = unset; 5 = key wasn't there.
        Some(0) | Some(5) => Ok(()),
        _ => Err(InstallError::ConfigCommandFailed {
            args,
            stderr: String::from_utf8_lossy(&out.stderr).trim().to_owned(),
        }),
    }
}

pub(crate) fn install_all_hooks(cwd: &Path, opts: &InstallOptions) -> Result<(), InstallError> {
    let hooks_dir = effective_hooks_dir(cwd)?;
    fs::create_dir_all(&hooks_dir)?;
    // Pre-flight: classify every hook before writing any of them so a
    // conflict on hook N doesn't leave hooks 1..N-1 already replaced.
    // `--force` skips the check and overwrites whatever's there.
    if !opts.force {
        for hook in HOOKS {
            let path = hooks_dir.join(hook);
            if let HookStatus::Conflict { existing } = classify_hook(&path, hook)? {
                return Err(InstallError::HookConflict {
                    hook: (*hook).into(),
                    existing,
                });
            }
        }
    }
    for hook in HOOKS {
        install_one_hook(&hooks_dir, hook, opts)?;
    }
    Ok(())
}

/// Render the `Hook already exists: <hook>` block matched by t-install
/// test 4 and t-update tests 6/7. Goes to stderr, matching upstream's
/// `Exit("%s", err)` path. The trailing line break after the third
/// section is omitted; the calling test captures with `2>&1` and
/// `$(...)` (which strips trailing newlines anyway).
pub fn print_hook_conflict(hook: &str, existing: &str) {
    eprintln!("Hook already exists: {hook}");
    eprintln!();
    for line in existing.lines() {
        eprintln!("\t{line}");
    }
    eprintln!();
    eprintln!("To resolve this, either:");
    eprintln!("  1: run `git lfs update --manual` for instructions on how to merge hooks.");
    eprintln!("  2: run `git lfs update --force` to overwrite your hook.");
}

/// Resolve the hooks directory git would actually invoke. Honors
/// `core.hookspath` (relative paths resolve against the working tree
/// root, or the git dir for bare repos), falling back to
/// `<git-dir>/hooks` when unset.
pub fn effective_hooks_dir(cwd: &Path) -> Result<std::path::PathBuf, InstallError> {
    let git_dir = git_dir(cwd)?;
    if let Ok(Some(hookspath)) = config::get(cwd, ConfigScope::Local, "core.hookspath")
        && !hookspath.is_empty()
    {
        let expanded = expand_home(&hookspath);
        let hp = Path::new(&expanded);
        if hp.is_absolute() {
            return Ok(hp.to_path_buf());
        }
        // Relative paths anchor on the working-tree root for non-bare
        // repos (where git_dir is `<work>/.git`), or on the git dir
        // itself for bare repos.
        let base = git_dir.parent().unwrap_or(&git_dir);
        return Ok(base.join(hp));
    }
    Ok(git_dir.join("hooks"))
}

/// Tilde-expand a leading `~/` or bare `~` against `$HOME`. Mirrors
/// upstream's `tools.ExpandPath` for the same-user case (the `~user`
/// lookup form is not used by any test today; if a user actually sets
/// `core.hooksPath = ~someone/hooks` we'll fall through to the
/// literal value and `git config` itself will surface the failure).
fn expand_home(raw: &str) -> String {
    if raw == "~"
        && let Some(home) = std::env::var_os("HOME")
    {
        return home.to_string_lossy().into_owned();
    }
    if let Some(rest) = raw.strip_prefix("~/")
        && let Some(home) = std::env::var_os("HOME")
    {
        return Path::new(&home).join(rest).to_string_lossy().into_owned();
    }
    raw.to_owned()
}

/// Best-effort hook installer used by `git lfs track`'s auto-install
/// pathway: writes any of our four hooks that don't already exist (or
/// already match our template), silently *skips* hooks that exist with
/// user-edited contents. Never errors on conflict — track shouldn't
/// fail because someone has a custom pre-push hook.
pub fn try_install_hooks(cwd: &Path) -> Result<(), InstallError> {
    let hooks_dir = git_dir(cwd)?.join("hooks");
    fs::create_dir_all(&hooks_dir)?;
    for hook in HOOKS {
        let path = hooks_dir.join(hook);
        let wanted = HOOK_TEMPLATE.replace("{{Command}}", hook);
        match classify_hook(&path, hook)? {
            HookStatus::Missing | HookStatus::Current | HookStatus::Legacy => {
                write_hook(&path, &wanted)?;
            }
            HookStatus::Conflict { .. } => {
                // User-edited hook — leave it alone.
            }
        }
    }
    Ok(())
}

/// Outcome of inspecting an existing hook file relative to our current
/// template. Drives both the install/update writer and the
/// `git lfs update`-side conflict UI.
#[derive(Debug)]
pub enum HookStatus {
    /// File doesn't exist or is empty — safe to write.
    Missing,
    /// File matches our current template — already installed.
    Current,
    /// File matches a previously-shipped template (or a leading-
    /// whitespace variant of one) — replace with the current version.
    Legacy,
    /// File has user-edited content. Carries the existing text so the
    /// caller can render it in a conflict message.
    Conflict { existing: String },
}

/// Inspect `<hooks_dir>/<hook>` and classify it.
pub fn classify_hook(hook_path: &Path, hook: &str) -> io::Result<HookStatus> {
    let existing = match fs::read_to_string(hook_path) {
        Ok(s) => s,
        Err(e) if e.kind() == io::ErrorKind::NotFound => return Ok(HookStatus::Missing),
        Err(e) => return Err(e),
    };
    if existing.trim().is_empty() {
        return Ok(HookStatus::Missing);
    }
    let wanted = HOOK_TEMPLATE.replace("{{Command}}", hook);
    if existing.trim() == wanted.trim() {
        return Ok(HookStatus::Current);
    }
    let normalized = strip_leading_indent(&existing);
    if legacy_templates(hook)
        .iter()
        .any(|t| normalized.trim() == t.trim())
    {
        return Ok(HookStatus::Legacy);
    }
    Ok(HookStatus::Conflict { existing })
}

/// Strip leading tabs/spaces from each line. Lets us recognize the
/// pre-2.6 hook format that indents the body with one TAB (test
/// "update with leading spaces" exercises this).
fn strip_leading_indent(s: &str) -> String {
    s.lines()
        .map(|l| l.trim_start_matches(['\t', ' ']))
        .collect::<Vec<_>>()
        .join("\n")
}

/// Previously-shipped hook templates we recognize as ours and silently
/// upgrade to the current [`HOOK_TEMPLATE`]. Each returned string is
/// compared verbatim against the existing hook (after leading-whitespace
/// normalization). The first three are pre-push-only — older versions
/// shipped a `git lfs push --stdin` invocation specific to that hook.
fn legacy_templates(hook: &str) -> Vec<String> {
    let mut out: Vec<String> = Vec::new();
    if hook == "pre-push" {
        out.push("#!/bin/sh\ngit lfs push --stdin $*".into());
        out.push("#!/bin/sh\ngit lfs push --stdin \"$@\"".into());
    }
    out.push(format!("#!/bin/sh\ngit lfs {hook} \"$@\""));
    out.push(format!(
        "#!/bin/sh\n\
         command -v git-lfs >/dev/null 2>&1 || \
         {{ echo >&2 \"\\nThis repository has been set up with Git LFS but Git LFS is not installed.\\n\"; exit 0; }}\n\
         git lfs {hook} \"$@\""
    ));
    out.push(format!(
        "#!/bin/sh\n\
         command -v git-lfs >/dev/null 2>&1 || \
         {{ echo >&2 \"\\nThis repository has been set up with Git LFS but Git LFS is not installed.\\n\"; exit 2; }}\n\
         git lfs {hook} \"$@\""
    ));
    out.push(format!(
        "#!/bin/sh\n\
         command -v git-lfs >/dev/null 2>&1 || \
         {{ echo >&2 \"\\nThis repository is configured for Git LFS but 'git-lfs' was not found on your path. \
         If you no longer wish to use Git LFS, remove this hook by deleting '.git/hooks/{hook}'.\\n\"; exit 2; }}\n\
         git lfs {hook} \"$@\""
    ));
    out.push(format!(
        "#!/bin/sh\n\
         command -v git-lfs >/dev/null 2>&1 || \
         {{ echo >&2 \"\\nThis repository is configured for Git LFS but 'git-lfs' was not found on your path. \
         If you no longer wish to use Git LFS, remove this hook by deleting the '{hook}' file in the hooks directory \
         (set by 'core.hookspath'; usually '.git/hooks').\\n\"; exit 2; }}\n\
         git lfs {hook} \"$@\""
    ));
    out
}

/// The current template for `hook`, used both as the on-disk content
/// and as the body printed by `git lfs update --manual`.
pub fn current_template(hook: &str) -> String {
    HOOK_TEMPLATE.replace("{{Command}}", hook)
}

fn install_one_hook(
    hooks_dir: &Path,
    hook: &str,
    opts: &InstallOptions,
) -> Result<(), InstallError> {
    let path = hooks_dir.join(hook);
    let wanted = HOOK_TEMPLATE.replace("{{Command}}", hook);
    match classify_hook(&path, hook)? {
        HookStatus::Current => Ok(()),
        HookStatus::Missing | HookStatus::Legacy => {
            write_hook(&path, &wanted)?;
            Ok(())
        }
        // `Conflict` only reaches here when `opts.force` is set —
        // `install_all_hooks`'s pre-flight rejects unforced conflicts.
        HookStatus::Conflict { existing } if opts.force => {
            let _ = existing;
            write_hook(&path, &wanted)?;
            Ok(())
        }
        HookStatus::Conflict { existing } => Err(InstallError::HookConflict {
            hook: hook.into(),
            existing,
        }),
    }
}

fn write_hook(path: &Path, content: &str) -> io::Result<()> {
    fs::write(path, content)?;
    set_executable(path)
}

#[cfg(unix)]
fn set_executable(path: &Path) -> io::Result<()> {
    use std::os::unix::fs::PermissionsExt;
    let mut perms = fs::metadata(path)?.permissions();
    perms.set_mode(0o755);
    fs::set_permissions(path, perms)
}

#[cfg(not(unix))]
fn set_executable(_path: &Path) -> io::Result<()> {
    Ok(())
}

#[derive(Debug, Clone)]
pub struct UninstallOptions {
    pub scope: InstallScope,
    /// Skip removing hooks; only unset the config.
    pub skip_repo: bool,
    /// Inverse of `skip_repo`: remove only the hooks and leave the
    /// `filter.lfs.*` config alone. Set by `git lfs uninstall hooks`.
    pub hooks_only: bool,
}

/// Run the uninstall. Mirrors [`install`]: clears the four `filter.lfs.*`
/// config keys and (unless `skip_repo`) removes the LFS hooks. Hooks are
/// only deleted if their contents match what we'd write — a user-edited
/// hook is left in place so we don't blow away local customizations.
pub fn uninstall(cwd: &Path, opts: &UninstallOptions) -> Result<(), InstallError> {
    if !opts.hooks_only {
        unset_filter_config(cwd, opts)?;
    }

    let in_repo = git_dir(cwd).is_ok();
    let touch_hooks =
        opts.hooks_only || (!opts.skip_repo && (opts.scope.is_repo_scope() || in_repo));
    if touch_hooks {
        uninstall_all_hooks(cwd)?;
    }
    Ok(())
}

fn unset_filter_config(cwd: &Path, opts: &UninstallOptions) -> Result<(), InstallError> {
    for (key, _) in FILTER_KEYS {
        scoped_unset(cwd, &opts.scope, key)?;
    }
    Ok(())
}

fn uninstall_all_hooks(cwd: &Path) -> Result<(), InstallError> {
    let hooks_dir = effective_hooks_dir(cwd)?;
    for hook in HOOKS {
        uninstall_one_hook(&hooks_dir, hook)?;
    }
    Ok(())
}

fn uninstall_one_hook(hooks_dir: &Path, hook: &str) -> Result<(), InstallError> {
    let path = hooks_dir.join(hook);
    let wanted = HOOK_TEMPLATE.replace("{{Command}}", hook);
    match fs::read_to_string(&path) {
        Ok(existing) if existing.trim() == wanted.trim() => {
            fs::remove_file(&path)?;
            Ok(())
        }
        Ok(_) => {
            // Hook exists but isn't ours — leave it alone.
            Ok(())
        }
        Err(e) if e.kind() == io::ErrorKind::NotFound => Ok(()),
        Err(e) => Err(InstallError::Io(e)),
    }
}