From 46f0c8fc2f0a1572e95dff621fb09d0d435ccfbe Mon Sep 17 00:00:00 2001 From: Mark Silverstein Date: Fri, 25 Sep 2026 13:57:48 +0800 Subject: [PATCH] fix: keep a checkout's config from running programs during git status A repository's .git/config can set core.fsmonitor to a program, which git runs on every status; safe.directory does not cover repos the user owns. Selecting a directory now overrides fsmonitor, hooksPath and pager on the command line, which outranks repository config. Co-Authored-By: Claude Opus 5.5 (1M context) --- crates/disktree-app/src/git.rs | 44 ++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/crates/disktree-app/src/git.rs b/crates/disktree-app/src/git.rs index 5c52eb6..d76a7d4 100644 --- a/crates/disktree-app/src/git.rs +++ b/crates/disktree-app/src/git.rs @@ -76,6 +76,18 @@ fn run(path: &Path, args: &[&str]) -> Option { let output = Command::new("git") .arg("-C") .arg(path) + // A checkout's own config can name programs for git to run: an + // fsmonitor on every `status`, hooks, a pager. Selecting a directory + // in a disk viewer must not execute anything it contains, and a + // command-line `-c` outranks the repository's config. + .args([ + "-c", + "core.fsmonitor=false", + "-c", + "core.hooksPath=/dev/null", + "-c", + "core.pager=cat", + ]) .args(args) // Never prompt, never page, never take a lock for the index refresh. .env("GIT_TERMINAL_PROMPT", "0") @@ -134,4 +146,36 @@ mod tests { assert_eq!(state.changed, 1, "one untracked file"); assert_eq!(state.stashes, 0); } + + #[cfg(unix)] + #[test] + fn a_checkout_cannot_make_git_run_its_programs() { + use std::os::unix::fs::PermissionsExt as _; + + let dir = tempfile::tempdir().expect("tempdir"); + let marker = dir.path().join("ran"); + let hook = dir.path().join("hook.sh"); + std::fs::write( + &hook, + format!("#!/bin/sh\ntouch '{}'\n", marker.display()), + ) + .expect("write hook"); + std::fs::set_permissions(&hook, std::fs::Permissions::from_mode(0o755)) + .expect("chmod"); + let git = |args: &[&str]| { + Command::new("git") + .arg("-C") + .arg(dir.path()) + .args(args) + .output() + .is_ok_and(|output| output.status.success()) + }; + if !git(&["init", "-q"]) { + return; // no git on this machine + } + let hook = hook.to_string_lossy(); + assert!(git(&["config", "core.fsmonitor", &hook])); + assert!(state(dir.path()).is_some()); + assert!(!marker.exists(), "the checkout's fsmonitor ran"); + } }