Skip to content

Commit

Permalink
fix: commits when multiple files share the same name
Browse files Browse the repository at this point in the history
There was an issue setting up the git commit when multiple `Cargo.toml`
files were present in a repository. This is because the logic that
created the commit would replace all of them. This fix works by building
a path recursively when navigating the tree.

This absolutely needs to be removed when gitoxide supports committing
directly.
  • Loading branch information
justinrubek committed May 17, 2024
1 parent 2ec393c commit 30b55aa
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ fn prepare_commit(
) -> Result<gix::worktree::object::Tree> {
let head = repo.head_commit()?;
let tree: gix::worktree::object::Tree = head.tree()?.decode()?.into();
let new_tree = rewrite_tree(repo, &tree.clone(), &changes)?;
let new_tree = rewrite_tree(repo, &tree.clone(), &changes, &mut PathBuf::new())?;
Ok(new_tree)
}

Expand All @@ -261,6 +261,7 @@ fn rewrite_tree(
repo: &gix::Repository,
tree: &gix::worktree::object::Tree,
changes: &Vec<PathBuf>,
tree_path: &mut PathBuf,
) -> Result<gix::worktree::object::Tree> {
let mut new_entries = vec![];

Expand All @@ -269,7 +270,9 @@ fn rewrite_tree(
match &object.kind {
gix::object::Kind::Tree => {
let old_tree = object.clone().into_tree().decode()?.into();
let new_tree = rewrite_tree(repo, &old_tree, changes)?;
tree_path.push(entry.filename.to_string());
let new_tree = rewrite_tree(repo, &old_tree, changes, tree_path)?;
tree_path.pop();
let new_id = repo.write_object(&new_tree)?;

new_entries.push(gix::worktree::object::tree::Entry {
Expand All @@ -279,7 +282,8 @@ fn rewrite_tree(
});
}
gix::object::Kind::Blob => {
let file_path: PathBuf = entry.filename.clone().to_string().into();
let file_name = entry.filename.clone().to_string();
let file_path = tree_path.join(file_name);
if let Some(new_path) = changes.iter().find(|p| **p == file_path) {
println!("replacing {:?}", new_path);
let new_id = repo.write_blob_stream(std::fs::File::open(new_path)?)?;
Expand Down

0 comments on commit 30b55aa

Please sign in to comment.