use super::*;
#[test]
fn in_linked_worktree_requires_real_worktree_metadata() {
let tmp = tempfile::tempdir().unwrap();
let base = tmp.path().canonicalize().unwrap();
let main = base.join("main");
let wt = base.join("wt");
std::fs::create_dir_all(main.join(".git/worktrees/wt")).unwrap();
std::fs::create_dir_all(wt.join("packages/api")).unwrap();
std::fs::write(main.join(".git/HEAD"), "ref: refs/heads/main\n").unwrap();
std::fs::write(main.join(".git/worktrees/wt/commondir"), "../..\n").unwrap();
std::fs::write(
main.join(".git/worktrees/wt/HEAD"),
"ref: refs/heads/main\n",
)
.unwrap();
std::fs::write(
wt.join(".git"),
format!("gitdir: {}\n", main.join(".git/worktrees/wt").display()),
)
.unwrap();
assert!(super::in_linked_worktree(&wt));
assert!(super::in_linked_worktree(&wt.join("packages/api")));
assert!(!super::in_linked_worktree(&main));
assert!(!super::in_linked_worktree(&base));
let bare = base.join("bare.git");
let bare_wt = base.join("bare-wt");
std::fs::create_dir_all(bare.join("worktrees/bare-wt")).unwrap();
std::fs::create_dir_all(&bare_wt).unwrap();
std::fs::write(bare.join("HEAD"), "ref: refs/heads/main\n").unwrap();
std::fs::write(bare.join("worktrees/bare-wt/commondir"), "../..\n").unwrap();
std::fs::write(
bare_wt.join(".git"),
format!("gitdir: {}\n", bare.join("worktrees/bare-wt").display()),
)
.unwrap();
assert!(super::in_linked_worktree(&bare_wt));
let sep = base.join("sep-checkout");
let sep_git = base.join("worktrees/sep.git");
std::fs::create_dir_all(sep_git.join("refs")).unwrap();
std::fs::create_dir_all(&sep).unwrap();
std::fs::write(sep_git.join("HEAD"), "ref: refs/heads/main\n").unwrap();
std::fs::write(sep_git.join("config"), "[core]\n").unwrap();
std::fs::write(sep.join(".git"), format!("gitdir: {}\n", sep_git.display())).unwrap();
assert!(!super::in_linked_worktree(&sep));
let forged = base.join("forged");
let forged_git = base.join("fake/worktrees/forged");
std::fs::create_dir_all(&forged_git).unwrap();
std::fs::create_dir_all(&forged).unwrap();
std::fs::write(forged_git.join("commondir"), "../../nonexistent\n").unwrap();
std::fs::write(
forged.join(".git"),
format!("gitdir: {}\n", forged_git.display()),
)
.unwrap();
assert!(!super::in_linked_worktree(&forged));
std::fs::create_dir_all(base.join("fake/other-repo")).unwrap();
std::fs::write(base.join("fake/other-repo/HEAD"), "ref: refs/heads/main\n").unwrap();
std::fs::write(forged_git.join("commondir"), "../../other-repo\n").unwrap();
assert!(!super::in_linked_worktree(&forged));
std::fs::write(forged_git.join("commondir"), "../..\n").unwrap();
assert!(!super::in_linked_worktree(&forged));
std::fs::write(base.join("fake/HEAD"), "ref: refs/heads/main\n").unwrap();
assert!(super::in_linked_worktree(&forged));
let sub = base.join("sub");
std::fs::create_dir_all(&sub).unwrap();
std::fs::write(
sub.join(".git"),
format!("gitdir: {}\n", main.join(".git/modules/sub").display()),
)
.unwrap();
assert!(!super::in_linked_worktree(&sub));
let pruned = base.join("pruned");
std::fs::create_dir_all(&pruned).unwrap();
std::fs::write(
pruned.join(".git"),
format!("gitdir: {}\n", main.join(".git/worktrees/gone").display()),
)
.unwrap();
assert!(!super::in_linked_worktree(&pruned));
let wt_sub = wt.join("sub");
std::fs::create_dir_all(&wt_sub).unwrap();
std::fs::write(
wt_sub.join(".git"),
format!(
"gitdir: {}\n",
main.join(".git/worktrees/wt/modules/sub").display()
),
)
.unwrap();
assert!(!super::in_linked_worktree(&wt_sub));
std::fs::create_dir_all(main.join(".git/worktrees/wt/modules/sub")).unwrap();
assert!(super::in_linked_worktree(&wt_sub));
let nested_name = wt.join("modules/foo");
std::fs::create_dir_all(&nested_name).unwrap();
std::fs::create_dir_all(main.join(".git/worktrees/wt/modules/modules/foo")).unwrap();
std::fs::write(
nested_name.join(".git"),
format!(
"gitdir: {}\n",
main.join(".git/worktrees/wt/modules/modules/foo").display()
),
)
.unwrap();
assert!(super::in_linked_worktree(&nested_name));
let lookalike = wt.join("lookalike");
let lookalike_git = base.join("plain/modules/repo");
std::fs::create_dir_all(&lookalike_git).unwrap();
std::fs::create_dir_all(&lookalike).unwrap();
std::fs::write(lookalike_git.join("HEAD"), "ref: refs/heads/main\n").unwrap();
std::fs::write(
lookalike.join(".git"),
format!("gitdir: {}\n", lookalike_git.display()),
)
.unwrap();
assert!(!super::in_linked_worktree(&lookalike));
std::fs::write(base.join("plain/HEAD"), "ref: refs/heads/main\n").unwrap();
assert!(super::in_linked_worktree(&lookalike));
let nested = wt.join("vendored");
let nested_git = base.join("vendored-git");
std::fs::create_dir_all(&nested).unwrap();
std::fs::create_dir_all(&nested_git).unwrap();
std::fs::write(nested_git.join("HEAD"), "ref: refs/heads/main\n").unwrap();
std::fs::write(
nested.join(".git"),
format!("gitdir: {}\n", nested_git.display()),
)
.unwrap();
assert!(!super::in_linked_worktree(&nested));
}
#[test]
fn worktree_main_checkout_equivalent() {
let tmp = tempfile::tempdir().unwrap();
let base = tmp.path().canonicalize().unwrap();
let main = base.join("main");
let wt = base.join("wt");
std::fs::create_dir_all(main.join(".git/worktrees/wt")).unwrap();
std::fs::create_dir_all(wt.join("sub")).unwrap();
std::fs::write(main.join(".git/HEAD"), "ref: refs/heads/main\n").unwrap();
std::fs::write(main.join(".git/worktrees/wt/commondir"), "../..\n").unwrap();
std::fs::write(
main.join(".git/worktrees/wt/gitdir"),
format!("{}\n", wt.join(".git").display()),
)
.unwrap();
std::fs::write(
wt.join(".git"),
format!("gitdir: {}\n", main.join(".git/worktrees/wt").display()),
)
.unwrap();
assert_eq!(super::main_checkout_equivalent(&wt), Some(main.clone()));
assert_eq!(
super::main_checkout_equivalent(&wt.join("sub/mise.toml")),
Some(main.join("sub/mise.toml"))
);
for claimed in [base.join("claimed"), main.join("vendor/claimed")] {
std::fs::create_dir_all(&claimed).unwrap();
std::fs::write(
claimed.join(".git"),
format!("gitdir: {}\n", main.join(".git/worktrees/wt").display()),
)
.unwrap();
assert_eq!(super::main_checkout_equivalent(&claimed), None);
}
#[cfg(unix)]
{
let linked = base.join("linked");
std::fs::create_dir_all(&linked).unwrap();
std::os::unix::fs::symlink(wt.join(".git"), linked.join(".git")).unwrap();
assert_eq!(super::main_checkout_equivalent(&linked), None);
}
let rel = base.join("rel");
std::fs::create_dir_all(main.join(".git/worktrees/rel")).unwrap();
std::fs::create_dir_all(&rel).unwrap();
std::fs::write(main.join(".git/worktrees/rel/commondir"), "../..\n").unwrap();
std::fs::write(
main.join(".git/worktrees/rel/gitdir"),
"../../../../rel/.git\n",
)
.unwrap();
std::fs::write(rel.join(".git"), "gitdir: ../main/.git/worktrees/rel\n").unwrap();
assert_eq!(super::main_checkout_equivalent(&rel), Some(main.clone()));
assert_eq!(super::main_checkout_equivalent(&main), None);
assert_eq!(super::main_checkout_equivalent(&base), None);
let bare = base.join("bare.git");
let bare_wt = base.join("bare-wt");
std::fs::create_dir_all(bare.join("worktrees/bare-wt")).unwrap();
std::fs::create_dir_all(&bare_wt).unwrap();
std::fs::write(bare.join("HEAD"), "ref: refs/heads/main\n").unwrap();
std::fs::write(bare.join("worktrees/bare-wt/commondir"), "../..\n").unwrap();
std::fs::write(
bare_wt.join(".git"),
format!("gitdir: {}\n", bare.join("worktrees/bare-wt").display()),
)
.unwrap();
assert_eq!(super::main_checkout_equivalent(&bare_wt), None);
let subm = main.join("subm");
std::fs::create_dir_all(main.join(".git/modules/subm")).unwrap();
std::fs::create_dir_all(&subm).unwrap();
std::fs::write(
subm.join(".git"),
format!("gitdir: {}\n", main.join(".git/modules/subm").display()),
)
.unwrap();
assert_eq!(super::main_checkout_equivalent(&subm), None);
assert_eq!(
super::main_checkout_equivalent(&subm.join("mise.toml")),
None
);
let wt_subm = wt.join("subm");
std::fs::create_dir_all(&wt_subm).unwrap();
std::fs::write(
wt_subm.join(".git"),
format!("gitdir: {}\n", main.join(".git/modules/subm").display()),
)
.unwrap();
assert_eq!(
super::main_checkout_equivalent(&wt_subm.join("mise.toml")),
Some(subm.join("mise.toml"))
);
}
#[cfg(unix)]
#[test]
fn worktree_main_checkout_equivalent_rejects_symlinks() {
let tmp = tempfile::tempdir().unwrap();
let base = tmp.path().canonicalize().unwrap();
let main = base.join("main");
let wt = base.join("wt");
let elsewhere = base.join("elsewhere");
std::fs::create_dir_all(main.join(".git/worktrees/wt")).unwrap();
std::fs::create_dir_all(wt.join("sub")).unwrap();
std::fs::create_dir_all(&elsewhere).unwrap();
std::fs::write(main.join(".git/HEAD"), "ref: refs/heads/main\n").unwrap();
std::fs::write(main.join(".git/worktrees/wt/commondir"), "../..\n").unwrap();
std::fs::write(
main.join(".git/worktrees/wt/gitdir"),
format!("{}\n", wt.join(".git").display()),
)
.unwrap();
std::fs::write(
wt.join(".git"),
format!("gitdir: {}\n", main.join(".git/worktrees/wt").display()),
)
.unwrap();
std::fs::write(elsewhere.join("mise.toml"), "").unwrap();
std::fs::write(wt.join("mise.toml"), "").unwrap();
std::os::unix::fs::symlink(elsewhere.join("mise.toml"), main.join("mise.toml")).unwrap();
assert_eq!(super::main_checkout_equivalent(&wt.join("mise.toml")), None);
std::os::unix::fs::symlink(&elsewhere, main.join("sub")).unwrap();
assert_eq!(
super::main_checkout_equivalent(&wt.join("sub/mise.toml")),
None
);
std::fs::remove_file(main.join("mise.toml")).unwrap();
std::os::unix::fs::symlink(wt.join("mise.toml"), main.join("mise.toml")).unwrap();
assert_eq!(super::main_checkout_equivalent(&wt.join("mise.toml")), None);
std::fs::remove_file(main.join("mise.toml")).unwrap();
std::fs::write(main.join("mise.toml"), "").unwrap();
assert_eq!(
super::main_checkout_equivalent(&wt.join("mise.toml")),
Some(main.join("mise.toml"))
);
}
#[test]
fn git_commands_ignore_inherited_work_tree() {
let tmp = tempfile::tempdir().unwrap();
let src = tmp.path().join("src");
let cache = tmp.path().join("cache");
let work_tree = tmp.path().join("work-tree");
std::fs::create_dir_all(&src).unwrap();
std::fs::create_dir_all(&work_tree).unwrap();
let git_in = |dir: &std::path::Path, args: &[&str]| {
let out = Command::new("git")
.args(args)
.current_dir(dir)
.output()
.expect("spawn git");
assert!(
out.status.success(),
"git {args:?} failed: {}",
String::from_utf8_lossy(&out.stderr)
);
out
};
git_in(&src, &["-c", "init.defaultBranch=main", "init", "-q"]);
std::fs::write(src.join("file.txt"), "hello\n").unwrap();
git_in(&src, &["add", "file.txt"]);
git_in(
&src,
&[
"-c",
"user.email=t@t",
"-c",
"user.name=t",
"commit",
"-q",
"-m",
"main",
],
);
let url = format!("file://{}", src.display());
let clone = Command::new("git")
.args(["clone", "-q", &url])
.arg(&cache)
.output()
.expect("spawn git clone");
assert!(
clone.status.success(),
"git clone failed: {}",
String::from_utf8_lossy(&clone.stderr)
);
std::fs::remove_file(cache.join("file.txt")).unwrap();
let output = sanitize_git_env(
git_cmd!(&cache, "checkout", "--force", "HEAD")
.env("GIT_WORK_TREE", &work_tree)
.env("GIT_INDEX_FILE", work_tree.join("index")),
)
.stderr_to_stdout()
.stdout_capture()
.unchecked()
.run()
.expect("run git checkout");
assert!(
output.status.success(),
"git checkout failed: {}",
String::from_utf8_lossy(&output.stdout)
);
assert!(cache.join("file.txt").exists());
assert!(!work_tree.join("file.txt").exists());
assert!(!work_tree.join("index").exists());
let clone_cache = tmp.path().join("clone-cache");
sanitize_git_cmd_runner(
CmdLineRunner::new("git")
.arg("clone")
.arg("-q")
.arg(&url)
.arg(&clone_cache)
.env("GIT_WORK_TREE", &work_tree),
)
.execute()
.expect("git clone should ignore inherited GIT_WORK_TREE");
assert!(clone_cache.join("file.txt").exists());
assert!(!work_tree.join("file.txt").exists());
}
#[cfg(unix)]
#[test]
fn worktree_detection_skips_a_commondir_that_is_a_pipe() {
let tmp = tempfile::tempdir().unwrap();
let base = tmp.path().canonicalize().unwrap();
let checkout = base.join("proj");
let private = checkout.join("g/worktrees/x");
std::fs::create_dir_all(&private).unwrap();
std::fs::write(checkout.join(".git"), "gitdir: g/worktrees/x\n").unwrap();
nix::unistd::mkfifo(
&private.join("commondir"),
nix::sys::stat::Mode::S_IRUSR | nix::sys::stat::Mode::S_IWUSR,
)
.unwrap();
let (tx, rx) = std::sync::mpsc::channel();
let probe = checkout.clone();
std::thread::spawn(move || {
let _ = tx.send(super::in_linked_worktree(&probe));
});
let linked = rx
.recv_timeout(std::time::Duration::from_secs(10))
.expect("worktree detection blocked on a commondir pipe");
assert!(!linked);
}
#[cfg(unix)]
#[test]
fn main_checkout_equivalent_skips_a_gitdir_pointer_that_is_a_pipe() {
let tmp = tempfile::tempdir().unwrap();
let base = tmp.path().canonicalize().unwrap();
let main = base.join("main");
let wt = base.join("wt");
std::fs::create_dir_all(main.join(".git/worktrees/wt")).unwrap();
std::fs::create_dir_all(&wt).unwrap();
std::fs::write(main.join(".git/HEAD"), "ref: refs/heads/main\n").unwrap();
std::fs::write(main.join(".git/worktrees/wt/commondir"), "../..\n").unwrap();
nix::unistd::mkfifo(
&main.join(".git/worktrees/wt/gitdir"),
nix::sys::stat::Mode::S_IRUSR | nix::sys::stat::Mode::S_IWUSR,
)
.unwrap();
std::fs::write(
wt.join(".git"),
format!("gitdir: {}\n", main.join(".git/worktrees/wt").display()),
)
.unwrap();
let (tx, rx) = std::sync::mpsc::channel();
let probe = wt.clone();
std::thread::spawn(move || {
let _ = tx.send(super::main_checkout_equivalent(&probe));
});
let equivalent = rx
.recv_timeout(std::time::Duration::from_millis(500))
.expect("worktree trust blocked on a gitdir pipe");
assert_eq!(equivalent, None);
}