use std::{path::Path, process::Command};
use pretty_assertions::assert_eq;
use tempfile::TempDir;
use super::{add, inspect, remove, restore, Checkout, Holds, WorktreeError};
fn git(dir: &Path, args: &[&str]) -> String {
let mut command = Command::new("git");
for name in crate::git::LOCATION_ENV {
command.env_remove(name);
}
let output = command
.arg("-C")
.arg(dir)
.args([
"-c",
"user.name=rho",
"-c",
"user.email=rho@example.com",
"-c",
"commit.gpgsign=false",
])
.args(args)
.output()
.unwrap();
assert!(
output.status.success(),
"git {args:?}: {}",
String::from_utf8_lossy(&output.stderr)
);
String::from_utf8(output.stdout).unwrap().trim().to_owned()
}
#[tokio::test]
async fn a_worktree_branches_from_head_and_keeps_the_workspace_place() {
let home = TempDir::new().unwrap();
let repository = home.path().join("my repo");
std::fs::create_dir_all(repository.join("src")).unwrap();
git(&repository, &["init", "--quiet", "--initial-branch=main"]);
std::fs::write(repository.join("src/lib.txt"), "base").unwrap();
git(&repository, &["add", "."]);
git(&repository, &["commit", "--quiet", "-m", "base"]);
let workspace = crate::paths::canonicalize(&repository.join("src")).unwrap();
let root = home.path().join("rho-home/worktrees");
let added = add(&root, &workspace).await.unwrap();
let path = &added.worktree.path;
assert_eq!(
(
added.worktree.branch.starts_with("rho/my-repo-"),
path.parent() == Some(crate::paths::canonicalize(&root).unwrap().as_path()),
path.file_name()
.map(|name| name.to_string_lossy().into_owned()),
added.cwd.clone(),
added.worktree.origin.clone(),
git(path, &["branch", "--show-current"]),
std::fs::read_to_string(added.cwd.join("lib.txt")).unwrap(),
git(&repository, &["branch", "--show-current"]),
git(&repository, &["status", "--porcelain"]),
),
(
true,
true,
added
.worktree
.branch
.strip_prefix("rho/")
.map(str::to_owned),
path.join("src"),
workspace,
added.worktree.branch.clone(),
"base".to_owned(),
"main".to_owned(),
String::new(),
)
);
}
#[tokio::test]
async fn a_workspace_outside_git_is_refused() {
let home = TempDir::new().unwrap();
let workspace = crate::paths::canonicalize(home.path()).unwrap();
let refused = add(&home.path().join("worktrees"), &workspace).await;
assert!(
matches!(&refused, Err(WorktreeError::NotARepository(path)) if path == &workspace),
"{refused:?}"
);
}
fn repository(home: &Path) -> std::path::PathBuf {
let repository = home.join("repo");
std::fs::create_dir_all(&repository).unwrap();
git(&repository, &["init", "--quiet", "--initial-branch=main"]);
std::fs::write(repository.join("shared.txt"), "base").unwrap();
git(&repository, &["add", "."]);
git(&repository, &["commit", "--quiet", "-m", "base"]);
crate::paths::canonicalize(&repository).unwrap()
}
#[tokio::test]
async fn inspect_reads_what_removing_a_worktree_would_lose() {
let home = TempDir::new().unwrap();
let origin = repository(home.path());
let root = home.path().join("worktrees");
let commit = |path: &Path| {
std::fs::write(path.join("new.txt"), "work").unwrap();
git(path, &["add", "."]);
git(path, &["commit", "--quiet", "-m", "work"]);
};
let mut found = Vec::new();
let mut expected = Vec::new();
type Finds = fn(String) -> Holds;
let main: Finds = |commit| Holds::Merged {
base: "main".to_owned(),
commit,
};
let changes: Finds = |_| Holds::Changes;
type Arrange<'a> = &'a dyn Fn(&Path);
let cases: [(&str, Arrange, Finds); 7] = [
("nothing new", &|_| {}, main),
(
"untracked",
&|path| std::fs::write(path.join("new.txt"), "work").unwrap(),
changes,
),
(
"modified",
&|path| std::fs::write(path.join("shared.txt"), "edit").unwrap(),
changes,
),
(
"staged",
&|path| {
std::fs::write(path.join("shared.txt"), "edit").unwrap();
git(path, &["add", "."]);
},
changes,
),
("unmerged commit", &commit, |_| Holds::Unmerged {
base: "main".to_owned(),
}),
(
"merged commit",
&|path| {
commit(path);
let branch = git(path, &["branch", "--show-current"]);
git(&origin, &["merge", "--quiet", "--ff-only", branch.as_str()]);
},
main,
),
(
"untracked, hidden by config",
&|path| {
git(path, &["config", "status.showUntrackedFiles", "no"]);
std::fs::write(path.join("hidden.txt"), "work").unwrap();
},
changes,
),
];
for (case, arrange, holds) in cases {
let added = add(&root, &origin).await.unwrap();
arrange(&added.worktree.path);
found.push((case, inspect(&added.worktree).await.unwrap()));
expected.push((
case,
holds(git(&added.worktree.path, &["rev-parse", "HEAD"])),
));
}
git(&origin, &["checkout", "--quiet", "--detach"]);
let detached = add(&root, &origin).await.unwrap();
found.push((
"detached origin",
inspect(&detached.worktree).await.unwrap(),
));
expected.push((
"detached origin",
Holds::Merged {
base: git(&origin, &["rev-parse", "--short", "HEAD"]),
commit: git(&origin, &["rev-parse", "HEAD"]),
},
));
assert_eq!(found, expected);
}
#[tokio::test]
async fn inspect_reads_squash_and_rebase_merges_as_merged() {
const NEXT_TO: usize = 8;
type Merge = fn(&Path, &Path, &[String; 2]);
let cases: [(&str, usize, Merge, bool); 8] = [
(
"merge commit",
NEXT_TO,
|origin, _, [_, last]| {
git(
origin,
&["merge", "--quiet", "--no-ff", "--no-edit", last.as_str()],
);
},
true,
),
(
"squash merge",
NEXT_TO,
|origin, _, [_, last]| {
git(origin, &["merge", "--quiet", "--squash", last.as_str()]);
git(origin, &["commit", "--quiet", "-m", "squash"]);
},
true,
),
(
"rebase merge",
NEXT_TO,
|origin, _, [first, last]| {
git(origin, &["cherry-pick", first.as_str(), last.as_str()]);
},
true,
),
(
"rebase merge, its lines edited since",
3,
|origin, _, [first, last]| {
git(origin, &["cherry-pick", first.as_str(), last.as_str()]);
edit_line(origin, 10, "ten, edited again");
},
true,
),
(
"conflicts with HEAD",
NEXT_TO,
|origin, _, _| edit_line(origin, 10, "ten, another way"),
false,
),
(
"half rebased",
NEXT_TO,
|origin, _, [first, _]| {
git(origin, &["cherry-pick", first.as_str()]);
},
false,
),
(
"a merge commit adding changes, then rebased",
3,
|origin, worktree, [first, last]| {
git(worktree, &["merge", "--quiet", "--no-commit", "main"]);
std::fs::write(worktree.join("resolved.txt"), "new").unwrap();
git(worktree, &["add", "resolved.txt"]);
git(worktree, &["commit", "--quiet", "--no-edit"]);
git(origin, &["cherry-pick", first.as_str(), last.as_str()]);
},
false,
),
("not merged", NEXT_TO, |_, _, _| {}, false),
];
let mut found = Vec::new();
let mut expected = Vec::new();
for (case, origin_edits, merge, merged) in cases {
let home = TempDir::new().unwrap();
let origin = repository(home.path());
edit_line(&origin, 1, "1");
let added = add(&home.path().join("worktrees"), &origin).await.unwrap();
let path = &added.worktree.path;
let commits = [(10, "ten"), (12, "twelve")].map(|(line, text)| {
edit_line(path, line, text);
git(path, &["rev-parse", "HEAD"])
});
edit_line(&origin, origin_edits, "moved on");
merge(&origin, path, &commits);
found.push((case, inspect(&added.worktree).await.unwrap()));
let head = git(path, &["rev-parse", "HEAD"]);
expected.push((
case,
match merged {
true => Holds::Merged {
base: "main".to_owned(),
commit: head,
},
false => Holds::Unmerged {
base: "main".to_owned(),
},
},
));
}
assert_eq!(found, expected);
}
fn edit_line(dir: &Path, number: usize, text: &str) {
let file = dir.join("lines.txt");
let mut lines: Vec<String> = match std::fs::read_to_string(&file) {
Ok(content) => content.lines().map(str::to_owned).collect(),
Err(_) => (1..=20).map(|line| line.to_string()).collect(),
};
lines[number - 1] = text.to_owned();
std::fs::write(&file, lines.join("\n") + "\n").unwrap();
git(dir, &["add", "lines.txt"]);
git(dir, &["commit", "--quiet", "-m", text]);
}
#[tokio::test]
async fn a_removed_worktree_comes_back_at_the_commit_it_was_removed_at() {
let home = TempDir::new().unwrap();
let origin = repository(home.path());
let root = home.path().join("worktrees");
let removed_at = git(&origin, &["rev-parse", "HEAD"]);
let mut found = Vec::new();
let mut expected = Vec::new();
for (case, delete_branch, checkout) in [
("branch kept", false, Checkout::Branch),
("branch deleted", true, Checkout::Branch),
("detached", false, Checkout::Detached),
] {
let added = add(&root, &origin).await.unwrap();
let worktree = &added.worktree;
remove(worktree).await.unwrap();
let branch_kept = !git(&origin, &["branch", "--list", worktree.branch.as_str()]).is_empty();
if delete_branch {
git(
&origin,
&["branch", "--quiet", "-D", worktree.branch.as_str()],
);
}
std::fs::write(origin.join(format!("{case}.txt")), case).unwrap();
git(&origin, &["add", "."]);
git(&origin, &["commit", "--quiet", "-m", case]);
let cwd = worktree.path.join("untracked/dir");
restore(worktree, &removed_at, &cwd, checkout)
.await
.unwrap();
found.push((
case,
branch_kept,
git(&worktree.path, &["branch", "--show-current"]),
git(&worktree.path, &["rev-parse", "HEAD"]),
cwd.is_dir(),
));
let branch = match checkout {
Checkout::Branch => worktree.branch.clone(),
Checkout::Detached => String::new(),
};
expected.push((case, true, branch, removed_at.clone(), true));
}
assert_eq!(found, expected);
}
#[tokio::test]
async fn a_removed_worktree_whose_commit_git_collected_comes_back_at_head() {
let mut found = Vec::new();
let mut expected = Vec::new();
for checkout in [Checkout::Branch, Checkout::Detached] {
let home = TempDir::new().unwrap();
let origin = repository(home.path());
let added = add(&home.path().join("worktrees"), &origin).await.unwrap();
let worktree = &added.worktree;
edit_line(&worktree.path, 10, "ten");
let removed_at = git(&worktree.path, &["rev-parse", "HEAD"]);
git(
&origin,
&["merge", "--quiet", "--squash", worktree.branch.as_str()],
);
git(&origin, &["commit", "--quiet", "-m", "squash"]);
remove(worktree).await.unwrap();
git(
&origin,
&["branch", "--quiet", "-D", worktree.branch.as_str()],
);
git(&origin, &["reflog", "expire", "--expire=now", "--all"]);
git(&origin, &["gc", "--quiet", "--prune=now"]);
let objects = ["cat-file", "--batch-check", "--batch-all-objects"];
let collected = !git(&origin, &objects).contains(removed_at.as_str());
restore(worktree, &removed_at, &worktree.path, checkout)
.await
.unwrap();
found.push((
checkout,
collected,
git(&worktree.path, &["branch", "--show-current"]),
git(&worktree.path, &["rev-parse", "HEAD"]),
));
let branch = match checkout {
Checkout::Branch => worktree.branch.clone(),
Checkout::Detached => String::new(),
};
expected.push((checkout, true, branch, git(&origin, &["rev-parse", "HEAD"])));
}
assert_eq!(found, expected);
}