Файли
openidea/crates/ide-vcs/tests/lfs_pull.rs
T
LU XIANGRUIтаwildcatDownstairs 5b486f1588 feat(vcs): detect Git LFS pointer files and pull LFS objects (#6749)
A repository tracking files with Git LFS gave no sign of it, and when the LFS objects were not downloaded (git-lfs missing, or a clone with GIT_LFS_SKIP_SMUDGE) the files stayed pointer files without explanation.

Once per repository and session in a trusted project, the LFS-tracked files whose checkout is still a pointer are listed (git ls-files ':(attr:filter=lfs)'). A notification names them and offers Pull LFS Objects (git lfs pull, after git lfs install --local when no config sets up the filter; an existing hook is kept), or says Git LFS is not installed and links to git-lfs.com. Git > Pull LFS Objects runs the same pull as a remote operation with progress, console output and cancel; it shows for repositories whose root .gitattributes uses filter=lfs.

Merged with the submodule actions from #6738: both repository flags, operations and notices are kept side by side.

Closes #6743

Co-authored-by: wildcatDownstairs <[email protected]>
2026-11-29 13:00:49 +08:00

53 рядки
2.7 KiB
Rust

use ide_core::CancellationToken;
use ide_vcs::Repo;
use std::{fs, path::Path, process::Command, sync::Mutex};
fn git(root: &Path, args: &[&str]) -> String {
let result = Command::new("git").current_dir(root).args(args).output().unwrap();
assert!(result.status.success(), "git {args:?}: {}", String::from_utf8_lossy(&result.stderr));
String::from_utf8(result.stdout).unwrap()
}
#[test]
fn pull_sets_up_lfs_despite_an_existing_hook_and_downloads_the_objects() {
if Command::new("git").args(["lfs", "version"]).output().is_ok_and(|out| !out.status.success()) {
eprintln!("git-lfs is not installed; skipping");
return;
}
// No global or system LFS filters, so the repository's own setup is what runs.
// SAFETY: this test binary has a single test, so no other thread reads the environment.
let temp = tempfile::tempdir().unwrap();
let root = dunce::canonicalize(temp.path()).unwrap();
let global = root.join("gitconfig");
fs::write(&global, "[user]\n\tname = LFS fixture\n\temail = [email protected]\n").unwrap();
unsafe {
std::env::set_var("GIT_CONFIG_GLOBAL", &global);
std::env::set_var("GIT_CONFIG_NOSYSTEM", "1");
}
let (source, clone) = (root.join("source"), root.join("clone"));
git(&root, &["init", "-q", "--bare", "-b", "main", "origin.git"]);
git(&root, &["init", "-q", "-b", "main", "source"]);
git(&source, &["lfs", "install", "--local"]);
git(&source, &["lfs", "track", "*.bin"]);
let content: Vec<u8> = (0..20_000u32).map(|i| (i % 251) as u8).collect();
fs::write(source.join("model.bin"), &content).unwrap();
git(&source, &["add", "-A"]);
git(&source, &["commit", "--no-gpg-sign", "-qm", "model"]);
git(&source, &["remote", "add", "origin", "../origin.git"]);
git(&source, &["push", "-q", "origin", "main"]);
git(&root, &["clone", "-q", "origin.git", "clone"]);
assert!(fs::read_to_string(clone.join("model.bin")).unwrap().starts_with("version https://git-lfs"));
let hook = clone.join(".git/hooks/pre-push");
fs::write(&hook, "#!/bin/sh\nexit 0\n").unwrap();
let repo = Repo::discover(&clone).unwrap().unwrap();
repo.set_execution_allowed(true);
let output = Mutex::new(String::new());
let out = |line: String| output.lock().unwrap().push_str(&(line + "\n"));
repo.pull_lfs(&CancellationToken::default(), &out).unwrap();
assert_eq!(fs::read(clone.join("model.bin")).unwrap(), content);
assert!(output.lock().unwrap().contains("Hook already exists"), "{}", output.lock().unwrap());
assert_eq!(fs::read_to_string(hook).unwrap(), "#!/bin/sh\nexit 0\n", "the existing hook is kept");
assert!(repo.lfs_status(&CancellationToken::default()).unwrap().pointers.is_empty());
}