Minor refactor for slightly improved readability

Maybe the intent of some parts is clearer now as well.
This commit is contained in:
Sebastian Thiel 2024-09-11 21:28:52 +02:00
parent 530a982379
commit ffdb0f6e2f
No known key found for this signature in database
GPG Key ID: 9CB5EE7895E8268B

View File

@ -63,8 +63,10 @@ where
&& hunks[0].diff_lines.contains_str(b"Subproject commit"); && hunks[0].diff_lines.contains_str(b"Subproject commit");
// if file exists // if file exists
if full_path.exists() || allow_new_file { let full_path_exists = full_path.exists();
if hunks.len() == 1 && hunks[0].change_type == crate::ChangeType::Deleted { let discard_hunk = (hunks.len() == 1).then(|| &hunks[0]);
if full_path_exists || allow_new_file {
if discard_hunk.map_or(false, |hunk| hunk.change_type == crate::ChangeType::Deleted) {
// File was created but now that hunk is being discarded with an inversed hunk // File was created but now that hunk is being discarded with an inversed hunk
builder.remove(rel_path); builder.remove(rel_path);
fs::remove_file(full_path.clone())?; fs::remove_file(full_path.clone())?;
@ -122,7 +124,7 @@ where
)?; )?;
builder.upsert(rel_path, blob_oid, filemode); builder.upsert(rel_path, blob_oid, filemode);
} else if let Ok(tree_entry) = base_tree.get_path(rel_path) { } else if let Ok(tree_entry) = base_tree.get_path(rel_path) {
if hunks.len() == 1 && hunks[0].binary { if discard_hunk.map_or(false, |hunk| hunk.binary) {
let new_blob_oid = &hunks[0].diff_lines; let new_blob_oid = &hunks[0].diff_lines;
// convert string to Oid // convert string to Oid
let new_blob_oid = new_blob_oid let new_blob_oid = new_blob_oid
@ -185,19 +187,17 @@ where
let new_blob_oid = git_repository.blob(blob_contents.as_bytes())?; let new_blob_oid = git_repository.blob(blob_contents.as_bytes())?;
// upsert into the builder // upsert into the builder
builder.upsert(rel_path, new_blob_oid, filemode); builder.upsert(rel_path, new_blob_oid, filemode);
} else if !full_path.exists() } else if !full_path_exists
&& hunks.len() == 1 && discard_hunk.map_or(false, |hunk| hunk.change_type == crate::ChangeType::Added)
&& hunks[0].change_type == crate::ChangeType::Added
{ {
// File was deleted but now that hunk is being discarded with an inversed hunk // File was deleted but now that hunk is being discarded with an inversed hunk
let mut blob_contents = BString::default();
let mut all_diffs = BString::default(); let mut all_diffs = BString::default();
for hunk in hunks { for hunk in hunks {
all_diffs.push_str(&hunk.diff_lines); all_diffs.push_str(&hunk.diff_lines);
} }
let patch = Patch::from_bytes(&all_diffs)?; let patch = Patch::from_bytes(&all_diffs)?;
blob_contents = apply(&blob_contents, &patch) let blob_contents =
.context(format!("failed to apply {}", all_diffs))?; apply([], &patch).context(format!("failed to apply {}", all_diffs))?;
let new_blob_oid = git_repository.blob(&blob_contents)?; let new_blob_oid = git_repository.blob(&blob_contents)?;
builder.upsert(rel_path, new_blob_oid, filemode); builder.upsert(rel_path, new_blob_oid, filemode);