Файли
openidea/crates/ide-vcs/tests/partial_clone.rs
T
audichuang a66c9e6d05 fix(vcs): retry git status without rename detection when a partial clone cannot fetch a blob (#6987)
In a blobless partial clone rename detection needs a staged blob it cannot fetch (Restricted Mode sets GIT_NO_LAZY_FETCH, or the promisor is unreachable) and git status fails. Status retries once with --no-renames when git reports the promisor remote.

Co-authored-by: audichuang <[email protected]>
2026-11-30 17:50:44 +08:00

145 рядки
6.7 KiB
Rust

//! Partial clones (`--filter=blob:none`) with a sparse checkout: blobs outside the
//! checkout are never fetched, and gitoxide's status cannot read them.
use ide_vcs::{ChangeKind, LogQuery, Repos};
use std::path::Path;
use std::process::Command;
fn git(dir: &Path, args: &[&str]) {
let out = Command::new("git")
.current_dir(dir)
.args(["-c", "init.defaultBranch=main", "-c", "core.autocrlf=false", "-c", "protocol.file.allow=always"])
.args(args)
.env("GIT_TERMINAL_PROMPT", "0")
.output()
.expect("git runs");
assert!(out.status.success(), "git {args:?}: {}", String::from_utf8_lossy(&out.stderr));
}
fn git_available() -> bool {
Command::new("git").arg("--version").output().is_ok_and(|o| o.status.success())
}
fn file_url(path: &Path) -> String {
let path = path.to_string_lossy().replace('\\', "/");
format!("file:///{}", path.trim_start_matches('/'))
}
/// A blobless, cone-mode sparse clone of a small source repository; `docs/` and `app/` blobs stay remote.
fn blobless_clone(base: &Path) -> std::path::PathBuf {
let source = base.join("source");
std::fs::create_dir_all(source.join("docs")).unwrap();
std::fs::create_dir_all(source.join("app")).unwrap();
git(&source, &["init", "-q"]);
git(&source, &["config", "user.name", "Test User"]);
git(&source, &["config", "user.email", "[email protected]"]);
git(&source, &["config", "commit.gpgsign", "false"]);
std::fs::write(source.join("README.md"), "hello\n").unwrap();
let guide: String = (1..=200).map(|i| format!("line {i}\n")).collect();
std::fs::write(source.join("docs/guide.txt"), &guide).unwrap();
std::fs::write(source.join("app/main.rs"), "fn main() {}\n").unwrap();
git(&source, &["add", "-A"]);
git(&source, &["commit", "-q", "-m", "init"]);
let bare = base.join("bare.git");
git(base, &["clone", "-q", "--bare", "source", "bare.git"]);
git(&bare, &["config", "uploadpack.allowFilter", "true"]);
git(&bare, &["config", "uploadpack.allowAnySHA1InWant", "true"]);
let work = base.join("work");
git(base, &["clone", "-q", "--filter=blob:none", "--sparse", &file_url(&bare), "work"]);
git(&work, &["config", "user.name", "Test User"]);
git(&work, &["config", "user.email", "[email protected]"]);
// A global `status.renames=false` would make the baseline pass without the fix.
git(&work, &["config", "status.renames", "true"]);
assert!(!work.join("app/main.rs").exists(), "app/ is outside the sparse checkout");
work
}
#[test]
fn blobless_sparse_clone_keeps_its_repository_and_status() {
if !git_available() {
eprintln!("git is not available; skipping");
return;
}
let tmp = tempfile::tempdir().unwrap();
let base = dunce::canonicalize(tmp.path()).unwrap();
let work = blobless_clone(&base);
let bare = base.join("bare.git");
std::fs::write(work.join("README.md"), "hello\nchanged\n").unwrap();
std::fs::write(work.join("new.txt"), "new\n").unwrap();
// A staged deletion of a never-fetched blob plus a near-identical addition: rename
// detection would have to fetch the missing blob (`git rm --sparse` needs git 2.35+).
git(&work, &["rm", "-q", "--sparse", "docs/guide.txt"]);
let renamed: String = (1..=199).map(|i| format!("line {i}\n")).collect();
std::fs::write(work.join("guide2.txt"), renamed).unwrap();
git(&work, &["add", "guide2.txt"]);
// The promisor remote is gone: status must not need it.
std::fs::rename(&bare, base.join("gone.git")).unwrap();
let repos = Repos::discover(&work, &|_| false).unwrap();
let repo = repos.primary().expect("the partial clone is a repository");
assert_eq!(repo.root(), work);
assert_eq!(repo.branch_info().unwrap().head.as_deref(), Some("main"));
assert_eq!(repo.log(&LogQuery::default()).unwrap().len(), 1);
let status = repos.status().expect("status of a partial clone");
let find = |rel: &str| status.iter().find(|f| f.rel_path == rel);
let readme = find("README.md").expect("README.md listed");
assert_eq!((readme.staged, readme.unstaged), (None, Some(ChangeKind::Modified)));
let new = find("new.txt").expect("new.txt listed");
assert_eq!(new.unstaged, Some(ChangeKind::Untracked));
// Rename detection needs the never-fetched blob, so the retry without it lists an addition.
let moved = find("guide2.txt").expect("guide2.txt listed");
assert_eq!((moved.staged, moved.renamed_from.as_deref()), (Some(ChangeKind::Added), None));
let removed = find("docs/guide.txt").expect("docs/guide.txt listed");
assert_eq!(removed.staged, Some(ChangeKind::Deleted));
// Skip-worktree entries outside the checkout are not missing files.
assert!(find("app/main.rs").is_none(), "{status:?}");
assert_eq!(status.len(), 4, "{status:?}");
// The watcher's scoped refresh (a trusted project's `git status` with a pathspec) retries the
// same way.
repo.set_execution_allowed(true);
let scoped = repo.status_paths(&[work.join("guide2.txt"), work.join("docs/guide.txt")]).expect("scoped status");
let find = |rel: &str| scoped.iter().find(|f| f.rel_path == rel);
let moved = find("guide2.txt").expect("guide2.txt listed");
assert_eq!((moved.staged, moved.renamed_from.as_deref()), (Some(ChangeKind::Added), None));
assert_eq!(find("docs/guide.txt").expect("docs/guide.txt listed").staged, Some(ChangeKind::Deleted));
assert_eq!(scoped.len(), 2, "{scoped:?}");
}
#[test]
fn blobless_clone_still_detects_a_rename_between_local_blobs() {
if !git_available() {
eprintln!("git is not available; skipping");
return;
}
let tmp = tempfile::tempdir().unwrap();
let base = dunce::canonicalize(tmp.path()).unwrap();
let work = blobless_clone(&base);
// README.md is inside the cone, so both sides of the rename are local blobs.
git(&work, &["mv", "README.md", "README2.md"]);
let repos = Repos::discover(&work, &|_| false).unwrap();
let repo = repos.primary().expect("the partial clone is a repository");
let status = repos.status().expect("status of a partial clone");
let moved = status.iter().find(|f| f.rel_path == "README2.md").expect("README2.md listed");
assert_eq!(
(moved.staged, moved.renamed_from.as_deref()),
(Some(ChangeKind::Renamed), Some("README.md")),
"{status:?}"
);
repo.set_execution_allowed(true);
let scoped = repo.status_paths(&[work.join("README2.md"), work.join("README.md")]).expect("scoped status");
let moved = scoped.iter().find(|f| f.rel_path == "README2.md").expect("README2.md listed");
assert_eq!(
(moved.staged, moved.renamed_from.as_deref()),
(Some(ChangeKind::Renamed), Some("README.md")),
"{scoped:?}"
);
}