Support symlinks in create_wd_tree

This commit is contained in:
Caleb Owens 2024-09-30 18:02:46 +02:00
parent 511ca5212e
commit c7d2bfa025
3 changed files with 38 additions and 0 deletions

View File

@ -199,6 +199,13 @@ impl RepositoryExt for git2::Repository {
let file_path = worktree_path.join(path).to_owned();
if file_path.is_symlink() {
let resolved_path = file_path.read_link()?;
let path_str = resolved_path
.to_str()
.context("Failed to convert path to str")?;
let blob = self.blob(path_str.as_bytes())?;
tree_update_builder.upsert(path, blob, git2::FileMode::Link);
} else {
let file = std::fs::read(&file_path)?;
let blob = self.blob(&file)?;

View File

@ -488,3 +488,28 @@ fn executable_blobs() {
)],
);
}
#[cfg(unix)]
#[test]
fn links() {
let test_repository = TestingRepository::open();
let commit = test_repository.commit_tree(None, &[("target", "helloworld")]);
test_repository
.repository
.branch("master", &commit, true)
.unwrap();
std::os::unix::fs::symlink("target", test_repository.tempdir.path().join("link1.txt")).unwrap();
let tree: git2::Tree = test_repository.repository.create_wd_tree().unwrap();
assert_tree_matches_with_mode(
&test_repository.repository,
tree.id(),
&[
("link1.txt", b"target", &[EntryAttribute::Link]),
("target", b"helloworld", &[EntryAttribute::Blob]),
],
);
}

View File

@ -87,6 +87,12 @@ impl TestingRepository {
self.repository.find_commit(commit).unwrap()
}
pub fn persist(self) {
let path = self.tempdir.into_path();
println!("Persisting test repository at {:?}", path);
}
}
pub fn assert_commit_tree_matches<'a>(