Skip to content

Commit c0de53e

Browse files
authored
Merge pull request #9066 from gitbutlerapp/kv-branch-23
Make hunk assignments handle no diff scenario better
2 parents 5dead13 + b6b2399 commit c0de53e

File tree

1 file changed

+69
-56
lines changed
  • crates/but-hunk-assignment/src

1 file changed

+69
-56
lines changed

crates/but-hunk-assignment/src/lib.rs

Lines changed: 69 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,8 @@ pub fn assign(
220220
but_core::diff::worktree_changes(repo)?.changes;
221221
let mut worktree_assignments = vec![];
222222
for change in &worktree_changes {
223-
let diff = change.unified_diff(repo, ctx.app_settings().context_lines)?;
224-
worktree_assignments.extend(diff_to_assignments(diff, change.path.clone()));
223+
let diff = change.unified_diff(repo, ctx.app_settings().context_lines);
224+
worktree_assignments.extend(diff_to_assignments(diff.ok(), change.path.clone()));
225225
}
226226

227227
// Reconcile worktree with the persisted assignments
@@ -295,8 +295,8 @@ pub fn assignments_with_fallback(
295295
}
296296
let mut worktree_assignments = vec![];
297297
for change in &worktree_changes {
298-
let diff = change.unified_diff(repo, ctx.app_settings().context_lines)?;
299-
worktree_assignments.extend(diff_to_assignments(diff, change.path.clone()));
298+
let diff = change.unified_diff(repo, ctx.app_settings().context_lines);
299+
worktree_assignments.extend(diff_to_assignments(diff.ok(), change.path.clone()));
300300
}
301301
let reconciled = reconcile_with_worktree_and_locks(
302302
ctx,
@@ -408,66 +408,79 @@ fn hunk_dependency_assignments(
408408
}
409409

410410
/// This also generates a UUID for the assignment
411-
fn diff_to_assignments(diff: UnifiedDiff, path: BString) -> Vec<HunkAssignment> {
411+
fn diff_to_assignments(diff: Option<UnifiedDiff>, path: BString) -> Vec<HunkAssignment> {
412412
let path_str = path.to_str_lossy();
413-
match diff {
414-
but_core::UnifiedDiff::Binary => vec![HunkAssignment {
415-
id: Some(Uuid::new_v4()),
416-
hunk_header: None,
417-
path: path_str.into(),
418-
path_bytes: path,
419-
stack_id: None,
420-
hunk_locks: None,
421-
line_nums_added: None,
422-
line_nums_removed: None,
423-
}],
424-
but_core::UnifiedDiff::TooLarge { .. } => vec![HunkAssignment {
413+
if let Some(diff) = diff {
414+
match diff {
415+
but_core::UnifiedDiff::Binary => vec![HunkAssignment {
416+
id: Some(Uuid::new_v4()),
417+
hunk_header: None,
418+
path: path_str.into(),
419+
path_bytes: path,
420+
stack_id: None,
421+
hunk_locks: None,
422+
line_nums_added: None,
423+
line_nums_removed: None,
424+
}],
425+
but_core::UnifiedDiff::TooLarge { .. } => vec![HunkAssignment {
426+
id: Some(Uuid::new_v4()),
427+
hunk_header: None,
428+
path: path_str.into(),
429+
path_bytes: path,
430+
stack_id: None,
431+
hunk_locks: None,
432+
line_nums_added: None,
433+
line_nums_removed: None,
434+
}],
435+
but_core::UnifiedDiff::Patch {
436+
hunks,
437+
is_result_of_binary_to_text_conversion,
438+
..
439+
} => {
440+
// If there are no hunks, then the assignment is for the whole file
441+
if is_result_of_binary_to_text_conversion || hunks.is_empty() {
442+
vec![HunkAssignment {
443+
id: Some(Uuid::new_v4()),
444+
hunk_header: None,
445+
path: path_str.into(),
446+
path_bytes: path,
447+
stack_id: None,
448+
hunk_locks: None,
449+
line_nums_added: None,
450+
line_nums_removed: None,
451+
}]
452+
} else {
453+
hunks
454+
.iter()
455+
.map(|hunk| {
456+
let (line_nums_added_new, line_nums_removed_old) =
457+
line_nums_from_hunk(&hunk.diff, hunk.old_start, hunk.new_start);
458+
HunkAssignment {
459+
id: Some(Uuid::new_v4()),
460+
hunk_header: Some(hunk.into()),
461+
path: path_str.clone().into(),
462+
path_bytes: path.clone(),
463+
stack_id: None,
464+
hunk_locks: None,
465+
line_nums_added: Some(line_nums_added_new),
466+
line_nums_removed: Some(line_nums_removed_old),
467+
}
468+
})
469+
.collect()
470+
}
471+
}
472+
}
473+
} else {
474+
vec![HunkAssignment {
425475
id: Some(Uuid::new_v4()),
426476
hunk_header: None,
427477
path: path_str.into(),
428-
path_bytes: path,
478+
path_bytes: path.clone(),
429479
stack_id: None,
430480
hunk_locks: None,
431481
line_nums_added: None,
432482
line_nums_removed: None,
433-
}],
434-
but_core::UnifiedDiff::Patch {
435-
hunks,
436-
is_result_of_binary_to_text_conversion,
437-
..
438-
} => {
439-
// If there are no hunks, then the assignment is for the whole file
440-
if is_result_of_binary_to_text_conversion || hunks.is_empty() {
441-
vec![HunkAssignment {
442-
id: Some(Uuid::new_v4()),
443-
hunk_header: None,
444-
path: path_str.into(),
445-
path_bytes: path,
446-
stack_id: None,
447-
hunk_locks: None,
448-
line_nums_added: None,
449-
line_nums_removed: None,
450-
}]
451-
} else {
452-
hunks
453-
.iter()
454-
.map(|hunk| {
455-
let (line_nums_added_new, line_nums_removed_old) =
456-
line_nums_from_hunk(&hunk.diff, hunk.old_start, hunk.new_start);
457-
HunkAssignment {
458-
id: Some(Uuid::new_v4()),
459-
hunk_header: Some(hunk.into()),
460-
path: path_str.clone().into(),
461-
path_bytes: path.clone(),
462-
stack_id: None,
463-
hunk_locks: None,
464-
line_nums_added: Some(line_nums_added_new),
465-
line_nums_removed: Some(line_nums_removed_old),
466-
}
467-
})
468-
.collect()
469-
}
470-
}
483+
}]
471484
}
472485
}
473486

0 commit comments

Comments
 (0)