M4.1: Git backend through the installed git
New crate lutra-vcs (L3): a safe git runner (argument arrays, no prompts, GIT_OPTIONAL_LOCKS=0 for reads, timeouts 30 s / 120 s for hooks, output caps, one spawn function for H1's kill-on-close helper), porcelain v2 / -z parsers (property-tested), Repo operations (status, stage, unstage before the first commit, commit and amend, show at HEAD/index, branches, checkout with remote tracking, branch creation checked by check-ref-format) and a .git poll (RepoWatch). Engine: git.status, git.stage, git.unstage, git.commit, git.show, git.branches, git.checkout, git.createBranch on pending outcomes, off the engine lock; gitChanged after changes and debounced 300 ms on .git or project changes; setting git.executable (section Git). Tests against real temp repositories, engine tests through run_waiting, docs (architecture, gaps G8, Git parity row, roadmap M4 row). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
cd8c34b094
commit
13e7b0aa12
28 changed files with 3485 additions and 9 deletions
479
crates/vcs/tests/repo.rs
Normal file
479
crates/vcs/tests/repo.rs
Normal file
|
|
@ -0,0 +1,479 @@
|
|||
//! The Git backend against real repositories created in temp folders:
|
||||
//! an initial repository without HEAD, commits, renames, CRLF, untracked
|
||||
//! files, a detached HEAD, Unicode paths, a file over the cap, binary
|
||||
//! files, a failing hook, branches and a project inside a repository.
|
||||
//! Every test skips with a message when git is not installed.
|
||||
|
||||
#![allow(
|
||||
clippy::unwrap_used,
|
||||
clippy::expect_used,
|
||||
clippy::panic,
|
||||
clippy::indexing_slicing
|
||||
)]
|
||||
|
||||
use std::path::Path;
|
||||
use std::process::Command;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use lutra_vcs::{FILE_CAP, FileState, Git, Repo, RepoWatch, Rev, VcsError};
|
||||
|
||||
/// The installed git, or `None` (with a message) when there is none.
|
||||
fn installed() -> Option<Git> {
|
||||
match Git::detect("") {
|
||||
Ok(git) => Some(git),
|
||||
Err(e) => {
|
||||
eprintln!("skipped: no usable git installed ({e})");
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Runs git in `dir` for test setup and asserts it succeeded.
|
||||
fn git(dir: &Path, args: &[&str]) -> String {
|
||||
let out = Command::new("git")
|
||||
.args(args)
|
||||
.current_dir(dir)
|
||||
.env_remove("GIT_DIR")
|
||||
.env_remove("GIT_WORK_TREE")
|
||||
.env_remove("GIT_INDEX_FILE")
|
||||
.output()
|
||||
.unwrap();
|
||||
assert!(
|
||||
out.status.success(),
|
||||
"git {args:?}: {}",
|
||||
String::from_utf8_lossy(&out.stderr)
|
||||
);
|
||||
String::from_utf8_lossy(&out.stdout).trim().to_string()
|
||||
}
|
||||
|
||||
/// A new repository on branch `main` with a local identity, no signing,
|
||||
/// no line-ending conversion and its own hooks folder.
|
||||
fn init(dir: &Path) {
|
||||
git(dir, &["init", "-q", "-b", "main"]);
|
||||
for (key, value) in [
|
||||
("user.name", "Lutra Test"),
|
||||
("user.email", "test@lutra.invalid"),
|
||||
("commit.gpgsign", "false"),
|
||||
("core.autocrlf", "false"),
|
||||
("core.hooksPath", ".git/hooks"),
|
||||
] {
|
||||
git(dir, &["config", key, value]);
|
||||
}
|
||||
}
|
||||
|
||||
/// Writes a file, creating its folders.
|
||||
fn write(dir: &Path, path: &str, text: impl AsRef<[u8]>) {
|
||||
let full = dir.join(path);
|
||||
std::fs::create_dir_all(full.parent().unwrap()).unwrap();
|
||||
std::fs::write(full, text).unwrap();
|
||||
}
|
||||
|
||||
/// Opens the repository at `dir`.
|
||||
fn open(git: &Git, dir: &Path) -> Repo {
|
||||
git.open(dir).unwrap().expect("a repository")
|
||||
}
|
||||
|
||||
/// The states of `path` in a status, or `None` if it is not listed.
|
||||
fn states(repo: &Repo, path: &str) -> Option<(FileState, FileState)> {
|
||||
repo.status()
|
||||
.unwrap()
|
||||
.files
|
||||
.into_iter()
|
||||
.find(|f| f.path == path)
|
||||
.map(|f| (f.index, f.worktree))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn folders_outside_a_repository_have_none() {
|
||||
let Some(cli) = installed() else { return };
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
assert!(cli.open(dir.path()).unwrap().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_missing_executable_is_not_found() {
|
||||
let err = Git::detect("C:/no/such/git-binary-for-lutra").unwrap_err();
|
||||
assert!(matches!(err, VcsError::NotFound(_)), "{err:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn initial_repository_stages_unstages_and_commits() {
|
||||
let Some(cli) = installed() else { return };
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
init(dir.path());
|
||||
write(dir.path(), "src/a.php", "<?php\necho 1;\n");
|
||||
let repo = open(&cli, dir.path());
|
||||
|
||||
let status = repo.status().unwrap();
|
||||
assert_eq!(status.head.branch.as_deref(), Some("main"));
|
||||
assert_eq!(status.head.head, None);
|
||||
assert!(!status.head.detached);
|
||||
assert_eq!(
|
||||
states(&repo, "src/a.php"),
|
||||
Some((FileState::Untracked, FileState::Untracked))
|
||||
);
|
||||
|
||||
repo.stage(&["src/a.php".into()]).unwrap();
|
||||
assert_eq!(
|
||||
states(&repo, "src/a.php"),
|
||||
Some((FileState::Added, FileState::Unmodified))
|
||||
);
|
||||
assert_eq!(
|
||||
repo.show("src/a.php", Rev::Index).unwrap().text.as_deref(),
|
||||
Some("<?php\necho 1;\n")
|
||||
);
|
||||
assert_eq!(repo.show("src/a.php", Rev::Head).unwrap().text, None);
|
||||
|
||||
repo.unstage(&["src/a.php".into()]).unwrap();
|
||||
assert_eq!(
|
||||
states(&repo, "src/a.php"),
|
||||
Some((FileState::Untracked, FileState::Untracked))
|
||||
);
|
||||
|
||||
repo.stage(&["src/a.php".into()]).unwrap();
|
||||
assert_eq!(repo.commit(" \n", false), Err(VcsError::EmptyMessage));
|
||||
let commit = repo.commit("#1 first commit\n\nbody", false).unwrap();
|
||||
assert_eq!(commit.summary, "#1 first commit");
|
||||
assert_eq!(commit.commit, git(dir.path(), &["rev-parse", "HEAD"]));
|
||||
let status = repo.status().unwrap();
|
||||
assert!(status.files.is_empty(), "{:?}", status.files);
|
||||
assert_eq!(status.head.head.as_deref(), Some(commit.commit.as_str()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn modified_deleted_renamed_and_amended() {
|
||||
let Some(cli) = installed() else { return };
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
init(dir.path());
|
||||
write(dir.path(), "a.php", "<?php // a\n");
|
||||
write(
|
||||
dir.path(),
|
||||
"old.php",
|
||||
"<?php // a file long enough to be found as a rename\n",
|
||||
);
|
||||
write(dir.path(), "gone.php", "<?php // gone\n");
|
||||
git(dir.path(), &["add", "-A"]);
|
||||
git(dir.path(), &["commit", "-q", "-m", "base"]);
|
||||
let repo = open(&cli, dir.path());
|
||||
|
||||
write(dir.path(), "a.php", "<?php // changed\n");
|
||||
std::fs::remove_file(dir.path().join("gone.php")).unwrap();
|
||||
std::fs::rename(dir.path().join("old.php"), dir.path().join("new.php")).unwrap();
|
||||
assert_eq!(
|
||||
states(&repo, "a.php"),
|
||||
Some((FileState::Unmodified, FileState::Modified))
|
||||
);
|
||||
assert_eq!(
|
||||
states(&repo, "gone.php"),
|
||||
Some((FileState::Unmodified, FileState::Deleted))
|
||||
);
|
||||
|
||||
repo.stage(&[
|
||||
"a.php".into(),
|
||||
"gone.php".into(),
|
||||
"old.php".into(),
|
||||
"new.php".into(),
|
||||
])
|
||||
.unwrap();
|
||||
let status = repo.status().unwrap();
|
||||
let renamed = status.files.iter().find(|f| f.path == "new.php").unwrap();
|
||||
assert_eq!(renamed.index, FileState::Renamed);
|
||||
assert_eq!(renamed.orig_path.as_deref(), Some("old.php"));
|
||||
assert_eq!(
|
||||
states(&repo, "gone.php"),
|
||||
Some((FileState::Deleted, FileState::Unmodified))
|
||||
);
|
||||
assert_eq!(
|
||||
repo.show("a.php", Rev::Head).unwrap().text.as_deref(),
|
||||
Some("<?php // a\n")
|
||||
);
|
||||
assert_eq!(
|
||||
repo.show("a.php", Rev::Index).unwrap().text.as_deref(),
|
||||
Some("<?php // changed\n")
|
||||
);
|
||||
|
||||
repo.unstage(&["new.php".into(), "old.php".into()]).unwrap();
|
||||
assert_eq!(
|
||||
states(&repo, "new.php"),
|
||||
Some((FileState::Untracked, FileState::Untracked))
|
||||
);
|
||||
|
||||
let first = repo.commit("second", false).unwrap();
|
||||
let amended = repo.commit("second, amended", true).unwrap();
|
||||
assert_ne!(first.commit, amended.commit);
|
||||
assert_eq!(amended.summary, "second, amended");
|
||||
assert_eq!(git(dir.path(), &["rev-list", "--count", "HEAD"]), "2");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn crlf_work_trees_with_autocrlf_are_clean() {
|
||||
let Some(cli) = installed() else { return };
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
init(dir.path());
|
||||
git(dir.path(), &["config", "core.autocrlf", "true"]);
|
||||
write(dir.path(), "crlf.php", "<?php\r\necho 1;\r\n");
|
||||
git(dir.path(), &["add", "-A"]);
|
||||
git(dir.path(), &["commit", "-q", "-m", "crlf"]);
|
||||
let repo = open(&cli, dir.path());
|
||||
assert!(repo.status().unwrap().files.is_empty());
|
||||
assert_eq!(
|
||||
repo.show("crlf.php", Rev::Head).unwrap().text.as_deref(),
|
||||
Some("<?php\necho 1;\n"),
|
||||
"the blob as stored, without line-ending conversion"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unicode_and_space_paths_round_trip() {
|
||||
let Some(cli) = installed() else { return };
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
init(dir.path());
|
||||
let name = "Ünïcode dir/€ file \"quoted\" ü.php";
|
||||
let name = if cfg!(windows) {
|
||||
"Ünïcode dir/€ file 'quoted' ü.php"
|
||||
} else {
|
||||
name
|
||||
};
|
||||
write(dir.path(), name, "<?php\n");
|
||||
let repo = open(&cli, dir.path());
|
||||
assert_eq!(
|
||||
states(&repo, name),
|
||||
Some((FileState::Untracked, FileState::Untracked))
|
||||
);
|
||||
repo.stage(&[name.to_string()]).unwrap();
|
||||
assert_eq!(
|
||||
states(&repo, name),
|
||||
Some((FileState::Added, FileState::Unmodified))
|
||||
);
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::ffi::OsStrExt;
|
||||
write(dir.path(), "star*.php", "<?php\n");
|
||||
write(dir.path(), "starX.php", "<?php\n");
|
||||
repo.stage(&["star*.php".into()]).unwrap();
|
||||
assert_eq!(
|
||||
states(&repo, "starX.php"),
|
||||
Some((FileState::Untracked, FileState::Untracked)),
|
||||
"paths are literal, not patterns"
|
||||
);
|
||||
let raw = std::ffi::OsStr::from_bytes(b"caf\xe9.php");
|
||||
std::fs::write(dir.path().join(raw), "<?php\n").unwrap();
|
||||
assert_eq!(
|
||||
states(&repo, "caf\u{fffd}.php"),
|
||||
Some((FileState::Untracked, FileState::Untracked)),
|
||||
"a non-UTF-8 name is listed lossily instead of failing the status"
|
||||
);
|
||||
}
|
||||
assert_eq!(
|
||||
repo.show(name, Rev::Index).unwrap().text.as_deref(),
|
||||
Some("<?php\n")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn detached_head_and_branches() {
|
||||
let Some(cli) = installed() else { return };
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
init(dir.path());
|
||||
write(dir.path(), "a.php", "<?php // main\n");
|
||||
git(dir.path(), &["add", "-A"]);
|
||||
git(dir.path(), &["commit", "-q", "-m", "base"]);
|
||||
let repo = open(&cli, dir.path());
|
||||
|
||||
repo.create_branch("feature/x", None, false).unwrap();
|
||||
for bad in ["bad..name", "-x", "@{-1}", "with space", ""] {
|
||||
let err = repo.create_branch(bad, None, false).unwrap_err();
|
||||
assert!(matches!(err, VcsError::InvalidName(_)), "{bad}: {err:?}");
|
||||
}
|
||||
assert!(repo.create_branch("feature/x", None, false).is_err());
|
||||
let branches = repo.branches().unwrap();
|
||||
assert_eq!(branches.current.as_deref(), Some("main"));
|
||||
let names: Vec<_> = branches.local.iter().map(|b| b.name.as_str()).collect();
|
||||
assert_eq!(names, vec!["feature/x", "main"]);
|
||||
|
||||
repo.checkout("feature/x").unwrap();
|
||||
assert_eq!(
|
||||
repo.status().unwrap().head.branch.as_deref(),
|
||||
Some("feature/x")
|
||||
);
|
||||
write(dir.path(), "a.php", "<?php // feature\n");
|
||||
repo.stage(&["a.php".into()]).unwrap();
|
||||
repo.commit("feature", false).unwrap();
|
||||
|
||||
repo.checkout("main").unwrap();
|
||||
write(dir.path(), "a.php", "<?php // dirty on main\n");
|
||||
let err = repo.checkout("feature/x").unwrap_err();
|
||||
assert!(
|
||||
matches!(&err, VcsError::Git(m) if m.contains("overwritten")),
|
||||
"{err:?}"
|
||||
);
|
||||
assert!(matches!(
|
||||
repo.checkout("nope").unwrap_err(),
|
||||
VcsError::Git(_)
|
||||
));
|
||||
git(dir.path(), &["checkout", "-q", "--", "a.php"]);
|
||||
|
||||
repo.create_branch("topic", Some("feature/x"), true)
|
||||
.unwrap();
|
||||
assert_eq!(repo.branches().unwrap().current.as_deref(), Some("topic"));
|
||||
|
||||
git(dir.path(), &["checkout", "-q", "--detach", "HEAD"]);
|
||||
let head = repo.head().unwrap();
|
||||
assert!(head.detached);
|
||||
assert_eq!(head.branch, None);
|
||||
assert!(head.head.is_some());
|
||||
assert_eq!(repo.branches().unwrap().current, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn remote_branches_are_listed_and_checked_out_as_tracking_branches() {
|
||||
let Some(cli) = installed() else { return };
|
||||
let upstream = tempfile::tempdir().unwrap();
|
||||
init(upstream.path());
|
||||
write(upstream.path(), "a.php", "<?php\n");
|
||||
git(upstream.path(), &["add", "-A"]);
|
||||
git(upstream.path(), &["commit", "-q", "-m", "base"]);
|
||||
git(upstream.path(), &["branch", "remote-only"]);
|
||||
let clone = tempfile::tempdir().unwrap();
|
||||
git(
|
||||
clone.path(),
|
||||
&["clone", "-q", upstream.path().to_str().unwrap(), "."],
|
||||
);
|
||||
let repo = open(&cli, clone.path());
|
||||
let branches = repo.branches().unwrap();
|
||||
let remote: Vec<_> = branches.remote.iter().map(|b| b.name.as_str()).collect();
|
||||
assert_eq!(remote, vec!["origin/main", "origin/remote-only"]);
|
||||
assert_eq!(branches.local[0].upstream.as_deref(), Some("origin/main"));
|
||||
let status = repo.status().unwrap();
|
||||
assert_eq!(status.head.upstream.as_deref(), Some("origin/main"));
|
||||
assert_eq!((status.head.ahead, status.head.behind), (Some(0), Some(0)));
|
||||
|
||||
repo.checkout("origin/remote-only").unwrap();
|
||||
let branches = repo.branches().unwrap();
|
||||
assert_eq!(branches.current.as_deref(), Some("remote-only"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn large_and_binary_files() {
|
||||
let Some(cli) = installed() else { return };
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
init(dir.path());
|
||||
write(dir.path(), "big.txt", vec![b'a'; FILE_CAP + 1]);
|
||||
write(dir.path(), "image.png", b"\x89PNG\r\n\x1a\n\0\0\0");
|
||||
write(dir.path(), "latin1.php", b"<?php // caf\xe9\n");
|
||||
write(dir.path(), "bom.php", b"\xEF\xBB\xBF<?php\n");
|
||||
git(dir.path(), &["add", "-A"]);
|
||||
let repo = open(&cli, dir.path());
|
||||
let big = repo.show("big.txt", Rev::Index).unwrap();
|
||||
assert!(big.too_large && big.text.is_none());
|
||||
let image = repo.show("image.png", Rev::Index).unwrap();
|
||||
assert!(image.binary && image.text.is_none());
|
||||
assert!(repo.show("latin1.php", Rev::Index).unwrap().binary);
|
||||
assert_eq!(
|
||||
repo.show("bom.php", Rev::Index).unwrap().text.as_deref(),
|
||||
Some("<?php\n")
|
||||
);
|
||||
assert_eq!(
|
||||
repo.show("src", Rev::Index).unwrap().text,
|
||||
None,
|
||||
"a missing path"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_failing_hook_reports_its_output() {
|
||||
let Some(cli) = installed() else { return };
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
init(dir.path());
|
||||
write(
|
||||
dir.path(),
|
||||
".git/hooks/pre-commit",
|
||||
"#!/bin/sh\necho 'lint failed: bad.php line 3' >&2\nexit 1\n",
|
||||
);
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
let hook = dir.path().join(".git/hooks/pre-commit");
|
||||
std::fs::set_permissions(hook, std::fs::Permissions::from_mode(0o755)).unwrap();
|
||||
}
|
||||
write(dir.path(), "a.php", "<?php\n");
|
||||
let repo = open(&cli, dir.path());
|
||||
repo.stage(&["a.php".into()]).unwrap();
|
||||
let err = repo.commit("blocked", false).unwrap_err();
|
||||
assert!(
|
||||
matches!(&err, VcsError::Git(m) if m.contains("lint failed: bad.php line 3")),
|
||||
"{err:?}"
|
||||
);
|
||||
assert_eq!(repo.status().unwrap().head.head, None, "nothing committed");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nothing_staged_is_refused_with_gits_message() {
|
||||
let Some(cli) = installed() else { return };
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
init(dir.path());
|
||||
let repo = open(&cli, dir.path());
|
||||
assert!(matches!(
|
||||
repo.commit("empty", false).unwrap_err(),
|
||||
VcsError::Git(_)
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_project_inside_a_repository_sees_its_own_files() {
|
||||
let Some(cli) = installed() else { return };
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
init(dir.path());
|
||||
write(dir.path(), "outside.php", "<?php\n");
|
||||
write(dir.path(), "app/inside.php", "<?php\n");
|
||||
let project = dir.path().join("app");
|
||||
let repo = open(&cli, &project);
|
||||
assert_eq!(repo.prefix(), "app/");
|
||||
let paths: Vec<_> = repo
|
||||
.status()
|
||||
.unwrap()
|
||||
.files
|
||||
.into_iter()
|
||||
.map(|f| f.path)
|
||||
.collect();
|
||||
assert_eq!(paths, vec!["inside.php"]);
|
||||
repo.stage(&["inside.php".into()]).unwrap();
|
||||
assert_eq!(
|
||||
states(&repo, "inside.php"),
|
||||
Some((FileState::Added, FileState::Unmodified))
|
||||
);
|
||||
for bad in ["../outside.php", "/etc/passwd", "", "a\nb"] {
|
||||
let err = repo.stage(&[bad.into()]).unwrap_err();
|
||||
assert!(matches!(err, VcsError::InvalidPath(_)), "{bad}: {err:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_watch_sees_outside_changes_and_skips_rebased_ones() {
|
||||
let Some(cli) = installed() else { return };
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
init(dir.path());
|
||||
write(dir.path(), "a.php", "<?php\n");
|
||||
let repo = open(&cli, dir.path());
|
||||
let watch = RepoWatch::start(repo.git_dir(), repo.common_dir(), Duration::from_millis(20));
|
||||
std::thread::sleep(Duration::from_millis(60));
|
||||
assert!(!watch.changed());
|
||||
|
||||
git(dir.path(), &["add", "-A"]);
|
||||
let deadline = Instant::now() + Duration::from_secs(5);
|
||||
while !watch.changed() {
|
||||
assert!(Instant::now() < deadline, "the change was not seen");
|
||||
std::thread::sleep(Duration::from_millis(10));
|
||||
}
|
||||
|
||||
git(dir.path(), &["commit", "-q", "-m", "x"]);
|
||||
watch.rebase();
|
||||
std::thread::sleep(Duration::from_millis(100));
|
||||
let _ = watch.changed();
|
||||
git(dir.path(), &["branch", "other"]);
|
||||
let deadline = Instant::now() + Duration::from_secs(5);
|
||||
while !watch.changed() {
|
||||
assert!(Instant::now() < deadline, "the new branch was not seen");
|
||||
std::thread::sleep(Duration::from_millis(10));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue