From acca7c058c399f348344064bfc906960b93b2da4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer?= Date: Sun, 6 Dec 2026 22:44:39 +0300 Subject: [PATCH 1/5] Fix hooks uninstall when core.hooksPath is set --- src/git.rs | 9 +++++---- src/hooks.rs | 10 ++++++++++ tests/hooks.rs | 28 ++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/src/git.rs b/src/git.rs index c88f567..b57b31a 100644 --- a/src/git.rs +++ b/src/git.rs @@ -538,15 +538,16 @@ pub async fn config(dir: &Path, key: &str) -> Result, GitError> { } } -/// The directory Git runs this repository's hooks from. Linked worktrees -/// share it. +/// This repository's own hooks directory, where `hooks install` writes scripts. +/// Linked worktrees share the common Git directory. Unlike `--git-path hooks`, +/// this does not follow `core.hooksPath` into another tool's directory. pub async fn hooks_dir(dir: &Path) -> Result { line_in( dir, - &["rev-parse", "--path-format=absolute", "--git-path", "hooks"], + &["rev-parse", "--path-format=absolute", "--git-common-dir"], ) .await - .map(PathBuf::from) + .map(|common_dir| PathBuf::from(common_dir).join("hooks")) } /// The full repository path of the remote. diff --git a/src/hooks.rs b/src/hooks.rs index 7d5550a..4707d11 100644 --- a/src/hooks.rs +++ b/src/hooks.rs @@ -173,6 +173,16 @@ pub(crate) async fn manage(args: HooksArgs) -> anyhow::Result<()> { } HooksAction::Uninstall(selection) => { let dir = git::hooks_dir(here).await?; + if let Some(path) = git::config(here, "core.hooksPath").await? { + for hook in selection.hooks() { + say(&format!( + "core.hooksPath is {}, so remove this line from its {} hook by hand if you added it: {}", + one_line(&path), + hook.name(), + hook.line() + )); + } + } for hook in selection.hooks() { uninstall(&dir, hook).await?; } diff --git a/tests/hooks.rs b/tests/hooks.rs index ca3f94a..fbade33 100644 --- a/tests/hooks.rs +++ b/tests/hooks.rs @@ -370,6 +370,34 @@ async fn install_writes_only_its_own_scripts_and_uninstall_removes_only_them() { assert_eq!(read(&checkout.hook("pre-commit")), None); } +#[tokio::test] +async fn uninstall_keeps_hooks_path_owned_by_another_tool() { + let checkout = Checkout::new().await; + let unused = "http://127.0.0.1:9"; + let install = checkout.proofline(unused, &["hooks", "install"], "").await; + assert!(install.status.success(), "{}", stderr(&install)); + + let shared = checkout.root().join(".husky"); + tokio::fs::create_dir_all(&shared).await.unwrap(); + let copied_script = read(&checkout.hook("pre-commit")).unwrap(); + tokio::fs::write(shared.join("pre-commit"), &copied_script) + .await + .unwrap(); + checkout.git(&["config", "core.hooksPath", ".husky"]).await; + + let uninstall = checkout + .proofline(unused, &["hooks", "uninstall"], "") + .await; + assert!(uninstall.status.success(), "{}", stderr(&uninstall)); + assert_eq!(read(&checkout.hook("pre-commit")), None); + assert_eq!(read(&checkout.hook("pre-push")), None); + assert_eq!( + read(&shared.join("pre-commit")).as_deref(), + Some(copied_script.as_str()) + ); + assert!(stderr(&uninstall).contains("remove this line from its pre-commit hook by hand")); +} + #[tokio::test] async fn the_exit_policy_blocks_only_on_request_and_never_reads_a_gap_as_clean() { let checkout = Checkout::new().await; From a34a60c63a5ad0a6ef389f263e62883449775be9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer?= Date: Sun, 6 Dec 2026 23:11:03 +0300 Subject: [PATCH 2/5] Cover hook uninstall from linked worktree --- tests/hooks.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/hooks.rs b/tests/hooks.rs index fbade33..168897e 100644 --- a/tests/hooks.rs +++ b/tests/hooks.rs @@ -398,6 +398,35 @@ async fn uninstall_keeps_hooks_path_owned_by_another_tool() { assert!(stderr(&uninstall).contains("remove this line from its pre-commit hook by hand")); } +#[tokio::test] +async fn uninstall_from_linked_worktree_removes_shared_git_hooks() { + let checkout = Checkout::new().await; + let install = checkout + .proofline("http://127.0.0.1:9", &["hooks", "install"], "") + .await; + assert!(install.status.success(), "{}", stderr(&install)); + + let linked = checkout.temporary.path().join("linked"); + checkout + .git(&[ + "worktree", + "add", + "-q", + "-b", + "linked", + linked.to_str().unwrap(), + ]) + .await; + let mut command = Command::new(env!("CARGO_BIN_EXE_proofline")); + command.args(["hooks", "uninstall"]); + checkout.environment(&mut command); + let uninstall = command.current_dir(&linked).output().await.unwrap(); + + assert!(uninstall.status.success(), "{}", stderr(&uninstall)); + assert_eq!(read(&checkout.hook("pre-commit")), None); + assert_eq!(read(&checkout.hook("pre-push")), None); +} + #[tokio::test] async fn the_exit_policy_blocks_only_on_request_and_never_reads_a_gap_as_clean() { let checkout = Checkout::new().await; From 0db9391092bd7d5b531075d1e3c46b44c8c30a0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer?= Date: Sun, 6 Dec 2026 23:22:06 +0300 Subject: [PATCH 3/5] Harden hook cleanup after Proofline review --- src/git.rs | 21 ++++++++++++++++++--- src/hooks.rs | 14 ++++++++++++++ tests/hooks.rs | 24 ++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 3 deletions(-) diff --git a/src/git.rs b/src/git.rs index b57b31a..d956e8a 100644 --- a/src/git.rs +++ b/src/git.rs @@ -542,12 +542,21 @@ pub async fn config(dir: &Path, key: &str) -> Result, GitError> { /// Linked worktrees share the common Git directory. Unlike `--git-path hooks`, /// this does not follow `core.hooksPath` into another tool's directory. pub async fn hooks_dir(dir: &Path) -> Result { - line_in( + let output = git_in( dir, + None, &["rev-parse", "--path-format=absolute", "--git-common-dir"], ) - .await - .map(|common_dir| PathBuf::from(common_dir).join("hooks")) + .await?; + Ok(common_dir_from_output(&output).join("hooks")) +} + +fn common_dir_from_output(output: &[u8]) -> PathBuf { + // Git adds a line ending; a space at the end of the path is still part of + // the directory name and must not be removed with `trim()`. + let path = output.strip_suffix(b"\n").unwrap_or(&output); + let path = path.strip_suffix(b"\r").unwrap_or(path); + PathBuf::from(String::from_utf8_lossy(path).into_owned()) } /// The full repository path of the remote. @@ -638,6 +647,12 @@ pub fn full_name(url: &str) -> Option { mod tests { use super::*; + #[test] + fn git_common_dir_keeps_trailing_spaces() { + assert_eq!(common_dir_from_output(b"repo \n"), PathBuf::from("repo ")); + assert_eq!(common_dir_from_output(b"repo \r\n"), PathBuf::from("repo ")); + } + #[test] fn reads_owner_and_name_from_remote_urls() { for url in [ diff --git a/src/hooks.rs b/src/hooks.rs index 4707d11..f908aba 100644 --- a/src/hooks.rs +++ b/src/hooks.rs @@ -183,6 +183,20 @@ pub(crate) async fn manage(args: HooksArgs) -> anyhow::Result<()> { )); } } + let common_dir = dir.parent().context("Git hooks directory has no parent")?; + let common_dir = tokio::fs::canonicalize(common_dir).await?; + match tokio::fs::canonicalize(&dir).await { + Ok(resolved) if !resolved.starts_with(&common_dir) => { + say(&format!( + "{} resolves outside this repository's Git directory, so its hooks were left unchanged. Remove Proofline hooks there by hand if you own that directory.", + dir.display() + )); + return Ok(()); + } + Err(error) if error.kind() == std::io::ErrorKind::NotFound => {} + Err(error) => return Err(error.into()), + _ => {} + } for hook in selection.hooks() { uninstall(&dir, hook).await?; } diff --git a/tests/hooks.rs b/tests/hooks.rs index 168897e..1a2018b 100644 --- a/tests/hooks.rs +++ b/tests/hooks.rs @@ -427,6 +427,30 @@ async fn uninstall_from_linked_worktree_removes_shared_git_hooks() { assert_eq!(read(&checkout.hook("pre-push")), None); } +#[cfg(unix)] +#[tokio::test] +async fn uninstall_does_not_follow_symlinked_git_hooks_directory() { + let checkout = Checkout::new().await; + let install = checkout + .proofline("http://127.0.0.1:9", &["hooks", "install"], "") + .await; + assert!(install.status.success(), "{}", stderr(&install)); + + let shared = checkout.temporary.path().join("shared-hooks"); + let hooks = checkout.root().join(".git/hooks"); + tokio::fs::rename(&hooks, &shared).await.unwrap(); + std::os::unix::fs::symlink(&shared, &hooks).unwrap(); + checkout.git(&["config", "core.hooksPath", ".husky"]).await; + + let uninstall = checkout + .proofline("http://127.0.0.1:9", &["hooks", "uninstall"], "") + .await; + assert!(uninstall.status.success(), "{}", stderr(&uninstall)); + assert!(read(&shared.join("pre-commit")).is_some()); + assert!(read(&shared.join("pre-push")).is_some()); + assert!(stderr(&uninstall).contains("resolves outside this repository's Git directory")); +} + #[tokio::test] async fn the_exit_policy_blocks_only_on_request_and_never_reads_a_gap_as_clean() { let checkout = Checkout::new().await; From 4d81210de5451deae70f36c36bd0a372b101be09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer?= Date: Sun, 6 Dec 2026 23:27:59 +0300 Subject: [PATCH 4/5] Exercise symlink safety with default hook configuration --- tests/hooks.rs | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/tests/hooks.rs b/tests/hooks.rs index 1a2018b..19c25d1 100644 --- a/tests/hooks.rs +++ b/tests/hooks.rs @@ -430,25 +430,29 @@ async fn uninstall_from_linked_worktree_removes_shared_git_hooks() { #[cfg(unix)] #[tokio::test] async fn uninstall_does_not_follow_symlinked_git_hooks_directory() { - let checkout = Checkout::new().await; - let install = checkout - .proofline("http://127.0.0.1:9", &["hooks", "install"], "") - .await; - assert!(install.status.success(), "{}", stderr(&install)); - - let shared = checkout.temporary.path().join("shared-hooks"); - let hooks = checkout.root().join(".git/hooks"); - tokio::fs::rename(&hooks, &shared).await.unwrap(); - std::os::unix::fs::symlink(&shared, &hooks).unwrap(); - checkout.git(&["config", "core.hooksPath", ".husky"]).await; + for custom_path in [false, true] { + let checkout = Checkout::new().await; + let install = checkout + .proofline("http://127.0.0.1:9", &["hooks", "install"], "") + .await; + assert!(install.status.success(), "{}", stderr(&install)); + + let shared = checkout.temporary.path().join("shared-hooks"); + let hooks = checkout.root().join(".git/hooks"); + tokio::fs::rename(&hooks, &shared).await.unwrap(); + std::os::unix::fs::symlink(&shared, &hooks).unwrap(); + if custom_path { + checkout.git(&["config", "core.hooksPath", ".husky"]).await; + } - let uninstall = checkout - .proofline("http://127.0.0.1:9", &["hooks", "uninstall"], "") - .await; - assert!(uninstall.status.success(), "{}", stderr(&uninstall)); - assert!(read(&shared.join("pre-commit")).is_some()); - assert!(read(&shared.join("pre-push")).is_some()); - assert!(stderr(&uninstall).contains("resolves outside this repository's Git directory")); + let uninstall = checkout + .proofline("http://127.0.0.1:9", &["hooks", "uninstall"], "") + .await; + assert!(uninstall.status.success(), "{}", stderr(&uninstall)); + assert!(read(&shared.join("pre-commit")).is_some()); + assert!(read(&shared.join("pre-push")).is_some()); + assert!(stderr(&uninstall).contains("resolves outside this repository's Git directory")); + } } #[tokio::test] From 9f5ddae79d78ac928526cf2a7ec3dc40514d3f91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer?= Date: Sun, 6 Dec 2026 23:31:15 +0300 Subject: [PATCH 5/5] Keep hook install and uninstall within Git directory --- src/hooks.rs | 40 +++++++++++++++++++++++++++------------- tests/hooks.rs | 20 ++++++++++++++++++++ 2 files changed, 47 insertions(+), 13 deletions(-) diff --git a/src/hooks.rs b/src/hooks.rs index f908aba..b1c37f9 100644 --- a/src/hooks.rs +++ b/src/hooks.rs @@ -167,6 +167,17 @@ pub(crate) async fn manage(args: HooksArgs) -> anyhow::Result<()> { return Ok(()); } let dir = git::hooks_dir(here).await?; + if hooks_dir_is_external(&dir).await? { + for hook in selection.hooks() { + say(&format!( + "{} resolves outside this repository's Git directory, so nothing was written. Add this line to its {} hook by hand: {}", + dir.display(), + hook.name(), + hook.line() + )); + } + return Ok(()); + } for hook in selection.hooks() { install(&dir, hook).await?; } @@ -183,19 +194,12 @@ pub(crate) async fn manage(args: HooksArgs) -> anyhow::Result<()> { )); } } - let common_dir = dir.parent().context("Git hooks directory has no parent")?; - let common_dir = tokio::fs::canonicalize(common_dir).await?; - match tokio::fs::canonicalize(&dir).await { - Ok(resolved) if !resolved.starts_with(&common_dir) => { - say(&format!( - "{} resolves outside this repository's Git directory, so its hooks were left unchanged. Remove Proofline hooks there by hand if you own that directory.", - dir.display() - )); - return Ok(()); - } - Err(error) if error.kind() == std::io::ErrorKind::NotFound => {} - Err(error) => return Err(error.into()), - _ => {} + if hooks_dir_is_external(&dir).await? { + say(&format!( + "{} resolves outside this repository's Git directory, so its hooks were left unchanged. Remove Proofline hooks there by hand if you own that directory.", + dir.display() + )); + return Ok(()); } for hook in selection.hooks() { uninstall(&dir, hook).await?; @@ -205,6 +209,16 @@ pub(crate) async fn manage(args: HooksArgs) -> anyhow::Result<()> { Ok(()) } +async fn hooks_dir_is_external(dir: &Path) -> anyhow::Result { + let common_dir = dir.parent().context("Git hooks directory has no parent")?; + let common_dir = tokio::fs::canonicalize(common_dir).await?; + match tokio::fs::canonicalize(dir).await { + Ok(resolved) => Ok(!resolved.starts_with(common_dir)), + Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(false), + Err(error) => Err(error.into()), + } +} + async fn install(dir: &Path, hook: Hook) -> anyhow::Result<()> { let path = dir.join(hook.name()); let script = hook.script(); diff --git a/tests/hooks.rs b/tests/hooks.rs index 19c25d1..9cf7391 100644 --- a/tests/hooks.rs +++ b/tests/hooks.rs @@ -427,6 +427,26 @@ async fn uninstall_from_linked_worktree_removes_shared_git_hooks() { assert_eq!(read(&checkout.hook("pre-push")), None); } +#[cfg(unix)] +#[tokio::test] +async fn install_does_not_follow_symlinked_git_hooks_directory() { + let checkout = Checkout::new().await; + let hooks = checkout.root().join(".git/hooks"); + let original = checkout.temporary.path().join("original-hooks"); + let shared = checkout.temporary.path().join("shared-hooks"); + tokio::fs::rename(&hooks, &original).await.unwrap(); + tokio::fs::create_dir_all(&shared).await.unwrap(); + std::os::unix::fs::symlink(&shared, &hooks).unwrap(); + + let install = checkout + .proofline("http://127.0.0.1:9", &["hooks", "install"], "") + .await; + assert!(install.status.success(), "{}", stderr(&install)); + assert_eq!(read(&shared.join("pre-commit")), None); + assert_eq!(read(&shared.join("pre-push")), None); + assert!(stderr(&install).contains("nothing was written")); +} + #[cfg(unix)] #[tokio::test] async fn uninstall_does_not_follow_symlinked_git_hooks_directory() {