Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

fix: normalize inherited relative GIT_* paths at startup (#1915)

worktrunk-bot6b750c926979
diff --git a/src/main.rs b/src/main.rs
index 7fa3d9d75..a4135dfb2 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1071,6 +1071,13 @@ fn print_help_to_stderr() {
}
fn main() {
+ // Capture the startup working directory before anything else. This is
+ // used by shell_exec to resolve relative `GIT_*` path variables inherited
+ // from a parent `git` (e.g. when invoked via `git wt ...` with
+ // `alias.wt = "!wt"`) against a stable reference, rather than against
+ // each child command's `current_dir`. See issue #1914.
+ worktrunk::shell_exec::init_startup_cwd();
+
init_rayon_thread_pool();
// Tell crossterm to always emit ANSI sequences
diff --git a/src/shell_exec.rs b/src/shell_exec.rs
index 36fa2773b..ce4a16376 100644
--- a/src/shell_exec.rs
+++ b/src/shell_exec.rs
@@ -24,6 +24,84 @@ use crate::sync::Semaphore;
/// Prevents resource exhaustion when spawning many parallel git commands.
static CMD_SEMAPHORE: OnceLock<Semaphore> = OnceLock::new();
+/// The working directory at `wt` startup. Captured once so relative `GIT_*`
+/// path variables inherited from a parent `git` process can be resolved to
+/// absolute paths regardless of each subsequent child command's `current_dir`.
+static STARTUP_CWD: OnceLock<Option<PathBuf>> = OnceLock::new();
+
+/// `GIT_*` environment variables that name paths used by git for repository
+/// discovery and I/O. When git invokes shell aliases (`alias.x = "!cmd"`) it
+/// may set some of these to *relative* paths (e.g. `GIT_DIR=.git`), which
+/// then resolve against whatever `current_dir` a child process happens to
+/// run in — not the directory where `wt` was invoked. Normalizing them to
+/// absolute paths keeps git's alias context without breaking discovery.
+const INHERITED_GIT_PATH_VARS: &[&str] = &[
+ "GIT_DIR",
+ "GIT_WORK_TREE",
+ "GIT_COMMON_DIR",
+ "GIT_INDEX_FILE",
+ "GIT_OBJECT_DIRECTORY",
+];
+
+/// Record the current working directory at `wt` startup so relative `GIT_*`
+/// path variables inherited from a parent process can later be resolved to
+/// absolute paths by [`Cmd`]'s env setup.
+///
+/// Call once during `wt` startup, before any code changes the process's
+/// working directory. Subsequent calls are no-ops.
+pub fn init_startup_cwd() {
+ STARTUP_CWD.get_or_init(|| std::env::current_dir().ok());
+}
+
+fn startup_cwd() -> Option<&'static PathBuf> {
+ STARTUP_CWD
+ .get_or_init(|| std::env::current_dir().ok())
+ .as_ref()
+}
+
+/// Pure helper: given a base directory and a lookup function for environment
+/// variables, compute the `(var, absolute_value)` overrides that should be
+/// applied to a child process's environment to shadow any inherited relative
+/// `GIT_*` path variables. Absolute values and unset variables are skipped.
+///
+/// Factored out from [`inherited_git_env_overrides`] so it can be unit-tested
+/// without touching process-wide state.
+fn compute_git_env_overrides<F>(base: &std::path::Path, lookup: F) -> Vec<(&'static str, OsString)>
+where
+ F: Fn(&str) -> Option<OsString>,
+{
+ let mut overrides = Vec::new();
+ for var in INHERITED_GIT_PATH_VARS {
+ let Some(value) = lookup(var) else {
+ continue;
+ };
+ let path = std::path::Path::new(&value);
+ if path.is_absolute() {
+ continue;
+ }
+ overrides.push((*var, base.join(path).into_os_string()));
+ }
+ overrides
+}
+
+/// Cached absolute forms of any inherited relative `GIT_*` path variables.
+/// Computed once from the startup cwd and process environment, since neither
+/// changes during the process lifetime.
+static GIT_ENV_OVERRIDES: OnceLock<Vec<(&'static str, OsString)>> = OnceLock::new();
+
+/// For each inherited `GIT_*` path variable that is set to a *relative* path,
+/// produce an absolute form resolved against the startup cwd. Returns the
+/// `(var, absolute_value)` pairs that should be applied to a child process's
+/// environment to shadow the inherited relative values.
+fn inherited_git_env_overrides() -> &'static [(&'static str, OsString)] {
+ GIT_ENV_OVERRIDES.get_or_init(|| {
+ let Some(cwd) = startup_cwd() else {
+ return Vec::new();
+ };
+ compute_git_env_overrides(cwd, |var| std::env::var_os(var))
+ })
+}
+
/// Monotonic epoch for trace timestamps.
///
/// Using `Instant` instead of `SystemTime` ensures monotonic timestamps even if
@@ -467,6 +545,14 @@ impl Cmd {
cmd.current_dir(dir);
}
+ // Normalize inherited relative `GIT_*` path variables (e.g. the
+ // `GIT_DIR=.git` git sets for shell aliases) to absolute paths
+ // resolved against the startup cwd, so they don't re-resolve against
+ // the child's `current_dir`. See issue #1914.
+ for (key, val) in inherited_git_env_overrides() {
+ cmd.env(key, val);
+ }
+
for (key, val) in &self.envs {
cmd.env(key, val);
}
@@ -939,6 +1025,63 @@ fn forward_signal_with_escalation(pgid: i32, sig: i32) {
mod tests {
use super::*;
+ #[test]
+ fn test_compute_git_env_overrides() {
+ // Use a platform-appropriate absolute base path so `Path::is_absolute`
+ // behaves the same on Windows and Unix (Unix-style `/abs/...` paths
+ // are not absolute on Windows).
+ let base_buf = std::env::temp_dir().join("wt-test-startup-cwd");
+ let base = base_buf.as_path();
+ let abs_work = std::env::temp_dir().join("wt-test-abs-work");
+ let env: std::collections::HashMap<&str, OsString> = [
+ // relative — should be resolved against base
+ ("GIT_DIR", OsString::from(".git")),
+ // absolute — should be skipped
+ ("GIT_WORK_TREE", abs_work.clone().into_os_string()),
+ // relative with parent traversal
+ ("GIT_INDEX_FILE", OsString::from("../index")),
+ // unrelated var — should not appear
+ ("GIT_AUTHOR_NAME", OsString::from("Test User")),
+ ]
+ .into_iter()
+ .collect();
+
+ let overrides = compute_git_env_overrides(base, |var| env.get(var).cloned());
+
+ // Unset GIT_COMMON_DIR / GIT_OBJECT_DIRECTORY are skipped, absolute
+ // GIT_WORK_TREE is skipped, unrelated vars are never consulted.
+ assert_eq!(overrides.len(), 2);
+ let as_map: std::collections::HashMap<_, _> = overrides.into_iter().collect();
+ assert_eq!(
+ as_map.get("GIT_DIR"),
+ Some(&base.join(".git").into_os_string())
+ );
+ assert_eq!(
+ as_map.get("GIT_INDEX_FILE"),
+ Some(&base.join("../index").into_os_string())
+ );
+ }
+
+ #[test]
+ fn test_compute_git_env_overrides_all_absolute() {
+ let base_buf = std::env::temp_dir().join("wt-test-startup-cwd");
+ let abs_git = std::env::temp_dir().join("wt-test-abs.git");
+ let env: std::collections::HashMap<&str, OsString> =
+ [("GIT_DIR", abs_git.into_os_string())]
+ .into_iter()
+ .collect();
+
+ let overrides = compute_git_env_overrides(base_buf.as_path(), |var| env.get(var).cloned());
+ assert!(overrides.is_empty());
+ }
+
+ #[test]
+ fn test_compute_git_env_overrides_all_unset() {
+ let base_buf = std::env::temp_dir().join("wt-test-startup-cwd");
+ let overrides = compute_git_env_overrides(base_buf.as_path(), |_| None);
+ assert!(overrides.is_empty());
+ }
+
#[test]
fn test_max_concurrent_commands_defaults() {
// When no env var is set, default should be used
diff --git a/tests/integration_tests/bare_repository.rs b/tests/integration_tests/bare_repository.rs
index b278665ef..dbc7e1844 100644
--- a/tests/integration_tests/bare_repository.rs
+++ b/tests/integration_tests/bare_repository.rs
@@ -284,6 +284,240 @@ fn test_bare_repo_with_repo_path_variable() {
);
}
+/// Regression test for #1914: when `wt` is invoked via a git shell alias
+/// (`alias.wt = "!wt"`), git exports `GIT_DIR` — sometimes as a *relative*
+/// path like `.git`. That relative path would otherwise re-resolve against
+/// every child command's `current_dir`, breaking worktrunk's repo discovery
+/// in bare-layout repositories. Worktrunk normalizes inherited relative
+/// `GIT_*` path variables at startup so `{{ repo_path }}` resolves
+/// identically whether `wt` is invoked directly or via a git alias.
+#[test]
+fn test_bare_repo_repo_path_with_inherited_relative_git_dir() {
+ let test = BareRepoTest::new();
+
+ // Configure a user-level alias that prints repo_path. We use user config
+ // (not project config) so it's discoverable even when the command is
+ // launched from the bare repo's parent directory.
+ let user_config = test.temp_path().join("user-config.toml");
+ fs::write(
+ &user_config,
+ "[aliases]\nprint-repo-path = \"echo REPO_PATH={{ repo_path }}\"\n",
+ )
+ .unwrap();
+
+ // Create a linked worktree so there's somewhere to run from.
+ let main_worktree = test.create_worktree("main", "main");
+ test.commit_in(&main_worktree, "Initial commit");
+
+ // Helper: run `wt step print-repo-path --yes` and extract the
+ // `REPO_PATH=...` value emitted by the alias. We compare these as-is
+ // so platform-specific path formatting (e.g. msys-style paths that
+ // Git Bash uses on Windows for `echo`) doesn't affect the test — we
+ // only assert that both invocations produce the *same* value.
+ let extract_repo_path = |out: &std::process::Output| -> String {
+ let combined = format!(
+ "{}{}",
+ String::from_utf8_lossy(&out.stdout),
+ String::from_utf8_lossy(&out.stderr)
+ );
+ combined
+ .lines()
+ .find_map(|line| line.trim().strip_prefix("REPO_PATH=").map(str::to_owned))
+ .unwrap_or_else(|| panic!("no REPO_PATH= line in output:\n{combined}"))
+ };
+
+ // Baseline: invoke `wt` normally from the main worktree.
+ let mut baseline = wt_command();
+ test.configure_wt_cmd(&mut baseline);
+ baseline
+ .env("WORKTRUNK_CONFIG_PATH", &user_config)
+ .args(["step", "print-repo-path", "--yes"])
+ .current_dir(&main_worktree);
+ let baseline_out = baseline.output().unwrap();
+ assert!(
+ baseline_out.status.success(),
+ "baseline wt failed:\nstdout: {}\nstderr: {}",
+ String::from_utf8_lossy(&baseline_out.stdout),
+ String::from_utf8_lossy(&baseline_out.stderr)
+ );
+ let baseline_repo_path = extract_repo_path(&baseline_out);
+
+ // Simulate a git alias invocation: git sets GIT_DIR (and GIT_PREFIX) for
+ // shell aliases. From a linked worktree, git sets GIT_DIR to the
+ // per-worktree admin dir — use a relative spelling to exercise the bug.
+ let worktree_git_dir = test.bare_repo_path().join("worktrees").join("main");
+ assert!(
+ worktree_git_dir.exists(),
+ "expected linked worktree admin dir at {worktree_git_dir:?}"
+ );
+ // Relative path from main_worktree to its per-worktree admin dir.
+ let relative_git_dir = PathBuf::from("..").join("worktrees").join("main");
+
+ let mut via_alias = wt_command();
+ test.configure_wt_cmd(&mut via_alias);
+ via_alias
+ .env("WORKTRUNK_CONFIG_PATH", &user_config)
+ .env("GIT_DIR", &relative_git_dir)
+ .env("GIT_PREFIX", "")
+ .args(["step", "print-repo-path", "--yes"])
+ .current_dir(&main_worktree);
+ let via_alias_out = via_alias.output().unwrap();
+ assert!(
+ via_alias_out.status.success(),
+ "wt via git alias failed:\nstdout: {}\nstderr: {}",
+ String::from_utf8_lossy(&via_alias_out.stdout),
+ String::from_utf8_lossy(&via_alias_out.stderr)
+ );
+ let via_alias_repo_path = extract_repo_path(&via_alias_out);
+
+ assert_eq!(
+ baseline_repo_path, via_alias_repo_path,
+ "repo_path differed when invoked via a simulated git alias \
+ (relative GIT_DIR was not normalized — see #1914)"
+ );
+}
+
+/// Companion to `test_bare_repo_repo_path_with_inherited_relative_git_dir`
+/// that exercises the full `git wt …` → `sh -c` → `wt` path using a *real*
+/// git alias instead of hand-crafted `GIT_*` env vars.
+///
+/// To actually trigger #1914, the layout must match the issue reporter's:
+/// `repo/.git` is the bare repository and `repo/main` is a linked worktree.
+/// Running `git wt …` from `repo/` causes git to export `GIT_DIR=.git`
+/// (relative), which is the bug vector — `BareRepoTest` uses a different
+/// layout (`repo/` *is* the bare) where git exports an absolute `GIT_DIR`
+/// and the bug doesn't reproduce, so we build the layout by hand here.
+///
+/// Unix-only: git aliases execute via `sh -c`, and shell-quoting the wt
+/// binary path for Windows' bundled sh is fiddly. The underlying fix in
+/// `shell_exec.rs` has no platform-specific code, so the simulated-env
+/// companion test still exercises the normalization on Windows.
+#[cfg(not(windows))]
+#[test]
+fn test_repo_path_via_real_git_alias_bare_dot_git_layout() {
+ use crate::common::{configure_git_env, wt_bin};
+
+ let temp_dir = tempfile::TempDir::new().unwrap();
+ let temp_path = canonicalize(temp_dir.path()).unwrap();
+
+ // Isolated gitconfig so we don't leak the user's real git settings.
+ let git_config_path = temp_path.join("test-gitconfig");
+ fs::write(
+ &git_config_path,
+ "[user]\n\tname = Test User\n\temail = [email protected]\n\
+ [init]\n\tdefaultBranch = main\n",
+ )
+ .unwrap();
+
+ // Layout: repo/.git (bare) + repo/main (linked worktree).
+ let repo_dir = temp_path.join("repo");
+ fs::create_dir(&repo_dir).unwrap();
+ let bare_git = repo_dir.join(".git");
+
+ let git = |dir: &Path| configure_git_env(Cmd::new("git"), &git_config_path).current_dir(dir);
+
+ git(&temp_path)
+ .args(["init", "--bare", "--initial-branch", "main"])
+ .arg(bare_git.to_str().unwrap())
+ .run()
+ .unwrap();
+ git(&bare_git)
+ .args(["worktree", "add", "../main"])
+ .run()
+ .unwrap();
+
+ let main_worktree = repo_dir.join("main");
+ fs::write(main_worktree.join("a.txt"), "hello").unwrap();
+ git(&main_worktree).args(["add", "a.txt"]).run().unwrap();
+ git(&main_worktree)
+ .args(["commit", "-m", "Initial commit"])
+ .run()
+ .unwrap();
+
+ // wt config with worktree-path template and the `print-repo-path` alias.
+ let user_config = temp_path.join("test-config.toml");
+ fs::write(
+ &user_config,
+ "worktree-path = \"{{ branch }}\"\n\
+ [aliases]\nprint-repo-path = \"echo REPO_PATH={{ repo_path }}\"\n",
+ )
+ .unwrap();
+ let approvals_path = temp_path.join("test-approvals.toml");
+
+ // Register the real git alias in the bare repo's config.
+ let wt_path = wt_bin();
+ let wt_path_lossy = wt_path.to_string_lossy();
+ let wt_path_escaped = shell_escape::unix::escape(wt_path_lossy);
+ git(&bare_git)
+ .args(["config", "alias.wt", &format!("!{wt_path_escaped}")])
+ .run()
+ .unwrap();
+
+ let extract_repo_path = |out: &std::process::Output| -> String {
+ let combined = format!(
+ "{}{}",
+ String::from_utf8_lossy(&out.stdout),
+ String::from_utf8_lossy(&out.stderr)
+ );
+ combined
+ .lines()
+ .find_map(|line| line.trim().strip_prefix("REPO_PATH=").map(str::to_owned))
+ .unwrap_or_else(|| panic!("no REPO_PATH= line in output:\n{combined}"))
+ };
+
+ // Shared wt env applied to both the direct and aliased invocations.
+ let apply_wt_env = |cmd: &mut Command| {
+ configure_git_cmd(cmd, &git_config_path);
+ cmd.env("WORKTRUNK_CONFIG_PATH", &user_config)
+ .env(
+ "WORKTRUNK_SYSTEM_CONFIG_PATH",
+ "/etc/xdg/worktrunk/config.toml",
+ )
+ .env("WORKTRUNK_APPROVALS_PATH", &approvals_path)
+ .env_remove("NO_COLOR")
+ .env_remove("CLICOLOR_FORCE");
+ };
+
+ // Baseline: invoke `wt` directly from `repo/`. Git discovers the bare
+ // `.git` automatically without exporting any `GIT_*` path vars.
+ let mut baseline = Command::new(wt_bin());
+ apply_wt_env(&mut baseline);
+ baseline
+ .args(["step", "print-repo-path", "--yes"])
+ .current_dir(&repo_dir);
+ let baseline_out = baseline.output().unwrap();
+ assert!(
+ baseline_out.status.success(),
+ "baseline wt failed:\nstdout: {}\nstderr: {}",
+ String::from_utf8_lossy(&baseline_out.stdout),
+ String::from_utf8_lossy(&baseline_out.stderr)
+ );
+ let baseline_repo_path = extract_repo_path(&baseline_out);
+
+ // Via the real git alias: `git wt step print-repo-path --yes`. From the
+ // `repo/` dir (not inside the worktree), git sets `GIT_DIR=.git`
+ // (relative) when exporting the alias environment — the exact bug vector
+ // from #1914.
+ let mut via_alias = Command::new("git");
+ apply_wt_env(&mut via_alias);
+ via_alias
+ .args(["wt", "step", "print-repo-path", "--yes"])
+ .current_dir(&repo_dir);
+ let via_alias_out = via_alias.output().unwrap();
+ assert!(
+ via_alias_out.status.success(),
+ "git wt via real alias failed:\nstdout: {}\nstderr: {}",
+ String::from_utf8_lossy(&via_alias_out.stdout),
+ String::from_utf8_lossy(&via_alias_out.stderr)
+ );
+ let via_alias_repo_path = extract_repo_path(&via_alias_out);
+
+ assert_eq!(
+ baseline_repo_path, via_alias_repo_path,
+ "repo_path differed when invoked via a real `git wt` alias — see #1914"
+ );
+}
+
#[rstest]
fn test_bare_repo_equivalent_to_normal_repo(repo: TestRepo) {
// This test verifies that bare repos behave identically to normal repos