From 30b55aa1e7e1dc00b70e5afd410e65b8fb6ac392 Mon Sep 17 00:00:00 2001 From: Justin Rubek <25621857+justinrubek@users.noreply.github.com> Date: Thu, 16 May 2024 20:23:42 -0500 Subject: [PATCH] fix: commits when multiple files share the same name 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. --- src/app.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/app.rs b/src/app.rs index d6291a0..6b8e7a2 100644 --- a/src/app.rs +++ b/src/app.rs @@ -249,7 +249,7 @@ fn prepare_commit( ) -> Result { 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) } @@ -261,6 +261,7 @@ fn rewrite_tree( repo: &gix::Repository, tree: &gix::worktree::object::Tree, changes: &Vec, + tree_path: &mut PathBuf, ) -> Result { let mut new_entries = vec![]; @@ -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 { @@ -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)?)?;