Skip to content

Commit c85be5d

Browse files
Merge pull request #11366 from gitbutlerapp/jt/awf
Clarifying `assignments_with_fallback`
2 parents a96f7cd + 1d50656 commit c85be5d

File tree

2 files changed

+39
-37
lines changed

2 files changed

+39
-37
lines changed

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

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ pub fn assign(
275275
&applied_stacks,
276276
MultipleOverlapping::SetMostLines,
277277
true,
278-
)?;
278+
);
279279

280280
// Reconcile with the requested changes
281281
let with_requests = reconcile::assignments(
@@ -284,17 +284,17 @@ pub fn assign(
284284
&applied_stacks,
285285
MultipleOverlapping::SetMostLines,
286286
true,
287-
)?;
287+
);
288288

289289
// Reconcile with hunk locks
290-
let lock_assignments = hunk_dependency_assignments(deps)?;
290+
let lock_assignments = hunk_dependency_assignments(deps);
291291
let with_locks = reconcile::assignments(
292292
&with_requests,
293293
&lock_assignments,
294294
&applied_stacks,
295295
MultipleOverlapping::SetNone,
296296
false,
297-
)?;
297+
);
298298

299299
state::set_assignments(db, with_locks.clone())?;
300300

@@ -319,16 +319,29 @@ pub fn assign(
319319
Ok(rejections)
320320
}
321321

322-
/// Same as the `reconcile_with_worktree_and_locks` function, but if the operation produces an error, it will create a fallback set of assignments from the worktree changes alone.
323-
/// An optional error is returned alongside the assignments, which will be `None` if the operation was successful and it will be set if the operation failed and a fallback was used.
324-
///
325-
/// The fallback can of course also fail, in which case the tauri operation will error out.
322+
/// Similar to the `reconcile_with_worktree_and_locks` function.
323+
/// TODO: figure out a better name for this function
326324
pub fn assignments_with_fallback(
327325
ctx: &mut Context,
328326
set_assignment_from_locks: bool,
329327
worktree_changes: Option<impl IntoIterator<Item = impl Into<but_core::TreeChange>>>,
330328
deps: Option<&HunkDependencies>,
331329
) -> Result<(Vec<HunkAssignment>, Option<anyhow::Error>)> {
330+
let hunk_assignments = reconcile_worktree_changes_with_worktree_and_locks(
331+
ctx,
332+
set_assignment_from_locks,
333+
worktree_changes,
334+
deps,
335+
)?;
336+
Ok((hunk_assignments, None))
337+
}
338+
339+
fn reconcile_worktree_changes_with_worktree_and_locks(
340+
ctx: &mut Context,
341+
set_assignment_from_locks: bool,
342+
worktree_changes: Option<impl IntoIterator<Item = impl Into<but_core::TreeChange>>>,
343+
deps: Option<&HunkDependencies>,
344+
) -> Result<Vec<HunkAssignment>> {
332345
let repo = ctx.repo.get()?;
333346
let worktree_changes: Vec<but_core::TreeChange> = match worktree_changes {
334347
Some(wtc) => wtc.into_iter().map(Into::into).collect(),
@@ -344,7 +357,7 @@ pub fn assignments_with_fallback(
344357
};
345358

346359
if worktree_changes.is_empty() {
347-
return Ok((vec![], None));
360+
return Ok(vec![]);
348361
}
349362
let mut worktree_assignments = vec![];
350363
for change in &worktree_changes {
@@ -360,12 +373,10 @@ pub fn assignments_with_fallback(
360373
set_assignment_from_locks,
361374
&worktree_assignments,
362375
deps,
363-
)
364-
.map(|a| (a, None))
365-
.unwrap_or_else(|e| (worktree_assignments, Some(e)));
376+
)?;
366377

367378
let db = &mut *ctx.db.get_mut()?;
368-
state::set_assignments(db, reconciled.0.clone())?;
379+
state::set_assignments(db, reconciled.clone())?;
369380
Ok(reconciled)
370381
}
371382

@@ -410,21 +421,21 @@ fn reconcile_with_worktree_and_locks(
410421
&applied_stacks,
411422
MultipleOverlapping::SetMostLines,
412423
true,
413-
)?;
424+
);
414425

415-
let lock_assignments = hunk_dependency_assignments(deps)?;
426+
let lock_assignments = hunk_dependency_assignments(deps);
416427
let with_locks = reconcile::assignments(
417428
&with_worktree,
418429
&lock_assignments,
419430
&applied_stacks,
420431
MultipleOverlapping::SetNone,
421432
set_assignment_from_locks,
422-
)?;
433+
);
423434

424435
Ok(with_locks)
425436
}
426437

427-
fn hunk_dependency_assignments(deps: &HunkDependencies) -> Result<Vec<HunkAssignment>> {
438+
fn hunk_dependency_assignments(deps: &HunkDependencies) -> Vec<HunkAssignment> {
428439
let mut assignments = vec![];
429440
for (path, hunk, locks) in &deps.diffs {
430441
// If there are locks towards more than one stack, this means double locking and the assignment None - the user can resolve this by partial committing.
@@ -451,7 +462,7 @@ fn hunk_dependency_assignments(deps: &HunkDependencies) -> Result<Vec<HunkAssign
451462
};
452463
assignments.push(assignment);
453464
}
454-
Ok(assignments)
465+
assignments
455466
}
456467

457468
/// This also generates a UUID for the assignment
@@ -718,8 +729,7 @@ mod tests {
718729
&applied_stacks,
719730
MultipleOverlapping::SetMostLines,
720731
true,
721-
)
722-
.unwrap();
732+
);
723733
assert_eq(
724734
result,
725735
vec![
@@ -740,8 +750,7 @@ mod tests {
740750
&applied_stacks,
741751
MultipleOverlapping::SetMostLines,
742752
true,
743-
)
744-
.unwrap();
753+
);
745754
assert_eq(
746755
result,
747756
vec![HunkAssignment::new("foo.rs", 10, 5, None, Some(1))],
@@ -759,8 +768,7 @@ mod tests {
759768
&applied_stacks,
760769
MultipleOverlapping::SetMostLines,
761770
true,
762-
)
763-
.unwrap();
771+
);
764772
assert_eq(
765773
result,
766774
vec![HunkAssignment::new("foo.rs", 12, 7, Some(1), Some(1))],
@@ -781,8 +789,7 @@ mod tests {
781789
&applied_stacks,
782790
MultipleOverlapping::SetMostLines,
783791
true,
784-
)
785-
.unwrap();
792+
);
786793
assert_eq(
787794
result,
788795
vec![HunkAssignment::new("foo.rs", 5, 18, Some(2), Some(2))],
@@ -803,8 +810,7 @@ mod tests {
803810
&applied_stacks,
804811
MultipleOverlapping::SetNone,
805812
true,
806-
)
807-
.unwrap();
813+
);
808814
assert_eq(
809815
result,
810816
vec![HunkAssignment::new("foo.rs", 5, 18, None, Some(2))],
@@ -822,8 +828,7 @@ mod tests {
822828
&applied_stacks,
823829
MultipleOverlapping::SetMostLines,
824830
false,
825-
)
826-
.unwrap();
831+
);
827832
// TODO: This is actually broken
828833
assert_eq!(
829834
result,
@@ -987,8 +992,7 @@ mod tests {
987992
&applied_stacks,
988993
MultipleOverlapping::SetMostLines,
989994
true,
990-
)
991-
.unwrap();
995+
);
992996

993997
// Binary file should maintain its stack assignment
994998
assert_eq!(
@@ -1032,8 +1036,7 @@ mod tests {
10321036
&applied_stacks,
10331037
MultipleOverlapping::SetMostLines,
10341038
true,
1035-
)
1036-
.unwrap();
1039+
);
10371040

10381041
assert_eq!(
10391042
result[0].stack_id,

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::cmp::Ordering;
22

3-
use anyhow::Result;
43
use but_core::ref_metadata::StackId;
54
use itertools::Itertools;
65

@@ -57,7 +56,7 @@ pub(crate) fn assignments(
5756
applied_stack_ids: &[StackId],
5857
multiple_overlapping_resolution: MultipleOverlapping,
5958
update_unassigned: bool,
60-
) -> Result<Vec<HunkAssignment>> {
59+
) -> Vec<HunkAssignment> {
6160
let mut reconciled = vec![];
6261
for new_assignment in new {
6362
let mut new_assignment = new_assignment.clone();
@@ -93,5 +92,5 @@ pub(crate) fn assignments(
9392
}
9493
reconciled.push(new_assignment);
9594
}
96-
Ok(reconciled)
95+
reconciled
9796
}

0 commit comments

Comments
 (0)