Skip to content

The design language that makes your AI harness better at design.

JavaScript

Fix: find the design hook from a nested git worktree (#1016) (#1020)

Abdul Wahab3609687fa821
diff --git a/crates/common/src/git.rs b/crates/common/src/git.rs
index 836376d95..14e676f73 100644
--- a/crates/common/src/git.rs
+++ b/crates/common/src/git.rs
@@ -3,18 +3,43 @@
use crate::jsp;
+/// The gitdir of the checkout at `dir`: `<dir>/.git` itself when it is a
+/// directory, or the path a `.git` file names after `gitdir:` (a linked
+/// worktree or a submodule), resolved against `dir`.
+pub fn git_dir(dir: &str) -> Option<String> {
+ let dot_git = jsp::join(&[dir, ".git"]);
+ if std::path::Path::new(&dot_git).is_dir() {
+ return Some(dot_git);
+ }
+ let raw = std::fs::read_to_string(&dot_git).ok()?;
+ let (key, path) = raw.trim().split_once(':')?;
+ key.eq_ignore_ascii_case("gitdir").then(|| jsp::resolve(dir, &[path.trim()]))
+}
+
+/// The shared directory a linked worktree's gitdir names in its `commondir`
+/// file, relative to itself. `None` for any other gitdir: a main checkout's or
+/// a submodule's is its own common dir.
+pub fn common_dir(git_dir: &str) -> Option<String> {
+ let raw = std::fs::read_to_string(jsp::join(&[git_dir, "commondir"])).ok()?;
+ Some(jsp::resolve(git_dir, &[raw.trim()]))
+}
+
/// The `info/exclude` git reads for the repository whose gitdir is the
-/// absolute path `git_dir`. A linked worktree's gitdir names the shared
-/// directory in a `commondir` file, relative to itself, and git then ignores
-/// the worktree's own `info`.
+/// absolute path `git_dir`. Git ignores a linked worktree's own `info`.
pub fn info_exclude(git_dir: &str) -> String {
- let common = match std::fs::read_to_string(jsp::join(&[git_dir, "commondir"])) {
- Ok(raw) => jsp::resolve(git_dir, &[raw.trim()]),
- Err(_) => git_dir.to_string(),
- };
+ let common = common_dir(git_dir).unwrap_or_else(|| git_dir.to_string());
jsp::join(&[&common, "info", "exclude"])
}
+/// The main checkout of the linked worktree at `dir`: the parent of the
+/// common dir its gitdir names, when that parent's own `.git` is the common
+/// dir. A bare or separate-git-dir repository has no such checkout.
+pub fn linked_worktree_checkout(dir: &str) -> Option<String> {
+ let common = common_dir(&git_dir(dir)?)?;
+ let checkout = jsp::dirname(&common);
+ (git_dir(&checkout).as_deref() == Some(common.as_str())).then_some(checkout)
+}
+
/// The header line of every `.gitignore` the engine writes into a runtime
/// directory of the user's project.
pub const RUNTIME_IGNORE_HEADER: &str = "# Written by Impeccable: local runtime state, never committed.";
@@ -73,6 +98,53 @@ mod tests {
let _ = std::fs::remove_dir_all(&base);
}
+ #[test]
+ fn linked_worktree_checkout_needs_a_git_file_whose_gitdir_has_commondir() {
+ let base = std::env::temp_dir().join(format!("impeccable-common-wt-{}", std::process::id()));
+ let _ = std::fs::remove_dir_all(&base);
+ let tmp = base.to_string_lossy().into_owned();
+ let at = |parts: &[&str]| jsp::join(&[&[tmp.as_str()], parts].concat());
+
+ let linked_git = at(&["repo", ".git", "worktrees", "wt"]);
+ std::fs::create_dir_all(&linked_git).unwrap();
+ std::fs::write(jsp::join(&[&linked_git, "commondir"]), "../..\n").unwrap();
+ std::fs::create_dir_all(at(&["abs"])).unwrap();
+ std::fs::write(at(&["abs", ".git"]), format!("gitdir: {}\n", linked_git)).unwrap();
+ let checkout = Some(at(&["repo"]));
+ assert_eq!(linked_worktree_checkout(&at(&["abs"])), checkout);
+
+ std::fs::create_dir_all(at(&["rel"])).unwrap();
+ std::fs::write(at(&["rel", ".git"]), "GitDir: ../repo/.git/worktrees/wt\r\n").unwrap();
+ assert_eq!(linked_worktree_checkout(&at(&["rel"])), checkout);
+
+ std::fs::create_dir_all(at(&["plain", ".git"])).unwrap();
+ assert_eq!(git_dir(&at(&["plain"])), Some(at(&["plain", ".git"])));
+ assert_eq!(linked_worktree_checkout(&at(&["plain"])), None);
+
+ std::fs::create_dir_all(at(&["repo", ".git", "modules", "sub"])).unwrap();
+ std::fs::create_dir_all(at(&["repo", "sub"])).unwrap();
+ std::fs::write(at(&["repo", "sub", ".git"]), "gitdir: ../.git/modules/sub\n").unwrap();
+ assert_eq!(linked_worktree_checkout(&at(&["repo", "sub"])), None);
+
+ let bare_git = at(&["bare.git", "worktrees", "wt"]);
+ std::fs::create_dir_all(&bare_git).unwrap();
+ std::fs::write(jsp::join(&[&bare_git, "commondir"]), "../..\n").unwrap();
+ std::fs::create_dir_all(at(&["unrelated", "wt"])).unwrap();
+ std::fs::write(at(&["unrelated", "wt", ".git"]), format!("gitdir: {}\n", bare_git)).unwrap();
+ assert_eq!(linked_worktree_checkout(&at(&["unrelated", "wt"])), None);
+
+ // --separate-git-dir: the checkout's `.git` is a file naming the common dir.
+ let sep_git = at(&["sep.git", "worktrees", "wt"]);
+ std::fs::create_dir_all(&sep_git).unwrap();
+ std::fs::write(jsp::join(&[&sep_git, "commondir"]), "../..\n").unwrap();
+ std::fs::create_dir_all(at(&["sep"])).unwrap();
+ std::fs::write(at(&["sep", ".git"]), format!("gitdir: {}\n", at(&["sep.git"]))).unwrap();
+ std::fs::write(at(&["unrelated", "wt", ".git"]), format!("gitdir: {}\n", sep_git)).unwrap();
+ assert_eq!(linked_worktree_checkout(&at(&["unrelated", "wt"])), None);
+
+ let _ = std::fs::remove_dir_all(&base);
+ }
+
#[test]
fn ensure_dir_ignored_writes_once_and_keeps_user_lines() {
let dir = std::env::temp_dir().join(format!("impeccable-ignore-{}", std::process::id()));
diff --git a/crates/context/src/context_cli.rs b/crates/context/src/context_cli.rs
index 8ac37188c..354411ff9 100644
--- a/crates/context/src/context_cli.rs
+++ b/crates/context/src/context_cli.rs
@@ -114,7 +114,9 @@ pub fn automatic_hook_mode(ctx: &Ctx, cwd: &str, env: &Env, provider: &Provider)
/// borrowing an unrelated manifest near the caller. The walk itself is the
/// authority: do not append repoRoot afterward, because `resolve_project` can
/// retain an outer workspace root for a target inside an independent nested
-/// Git repository.
+/// Git repository. A linked worktree nested in its own main checkout (Claude
+/// Code's `.claude/worktrees/<name>`) does not stop the walk, so it still finds
+/// the checkout's manifest (#1016); any other `.git` does.
fn hook_manifest_search_roots(ctx: &Ctx, cwd: &str, env: &Env) -> Vec<String> {
let mut roots: Vec<String> = Vec::new();
let mut current = jsp::resolve(if ctx.project_root.is_empty() { cwd } else { &ctx.project_root }, &[]);
@@ -126,7 +128,10 @@ fn hook_manifest_search_roots(ctx: &Ctx, cwd: &str, env: &Env) -> Vec<String> {
if !roots.contains(&current) {
roots.push(current.clone());
}
- if crate::context::has_git_boundary(&current) {
+ if crate::context::has_git_boundary(&current)
+ && !impeccable_common::git::linked_worktree_checkout(&current)
+ .is_some_and(|checkout| crate::context::is_path_inside(&current, &checkout))
+ {
break;
}
let parent = jsp::dirname(&current);
@@ -733,3 +738,50 @@ mod skill_version_tests {
assert_eq!(v("---\nversion:\n---\n"), None);
}
}
+
+#[cfg(test)]
+mod worktree_hook_tests {
+ use super::*;
+
+ #[test]
+ fn nested_linked_worktree_finds_the_checkouts_hook_manifest() {
+ let base = std::env::temp_dir().join(format!("impeccable-wt-hook-{}", std::process::id()));
+ let _ = std::fs::remove_dir_all(&base);
+ let tmp = base.to_string_lossy().into_owned();
+ let at = |parts: &[&str]| jsp::join(&[&[tmp.as_str()], parts].concat());
+ let put = |path: String, body: &str| {
+ std::fs::create_dir_all(jsp::dirname(&path)).unwrap();
+ std::fs::write(path, body).unwrap();
+ };
+ let settings = r#"{"hooks":{"PostToolUse":[{"matcher":"Edit|Write","hooks":[{"type":"command","command":"\"${CLAUDE_PROJECT_DIR}/.claude/skills/impeccable/scripts/impeccable\" hook"}]}]}}"#;
+ put(at(&["main", ".claude", "settings.json"]), settings);
+ put(at(&["main", ".git", "worktrees", "demo", "commondir"]), "../..\n");
+ put(at(&["main", ".git", "worktrees", "ext", "commondir"]), "../..\n");
+ put(at(&["main", ".git", "worktrees", "other", "commondir"]), "../..\n");
+ put(at(&["main", ".claude", "worktrees", "demo", ".git"]), &format!("gitdir: {}\n", at(&["main", ".git", "worktrees", "demo"])));
+ put(at(&["ext", ".git"]), &format!("gitdir: {}\n", at(&["main", ".git", "worktrees", "ext"])));
+ // A worktree inside an unrelated repo must not borrow that repo's manifest.
+ put(at(&["other", ".claude", "settings.json"]), settings);
+ std::fs::create_dir_all(at(&["other", ".git"])).unwrap();
+ put(at(&["other", "wt", ".git"]), &format!("gitdir: {}\n", at(&["main", ".git", "worktrees", "other"])));
+ std::fs::create_dir_all(at(&["main", ".git", "modules", "sub"])).unwrap();
+ put(at(&["main", "sub", ".git"]), "gitdir: ../.git/modules/sub\n");
+
+ let env = Env::from([
+ ("HOME".to_string(), tmp.clone()),
+ ("USERPROFILE".to_string(), tmp.clone()),
+ ("IMPECCABLE_PROVIDER_ID".to_string(), "claude-code".to_string()),
+ ]);
+ let provider = crate::provider::detect(&env, &tmp);
+ let mode = |dir: String| {
+ let ctx = load_context(&dir, &TargetOptions::default(), &env);
+ automatic_hook_mode(&ctx, &dir, &env, &provider)
+ };
+ assert_eq!(mode(at(&["main", ".claude", "worktrees", "demo"])), "stop");
+ assert_eq!(mode(at(&["ext"])), "none");
+ assert_eq!(mode(at(&["main", "sub"])), "none");
+ assert_eq!(mode(at(&["other", "wt"])), "none");
+
+ let _ = std::fs::remove_dir_all(&base);
+ }
+}