Skip to content

Commit 676a2e5

Browse files
committed
fix: Don't use Result for early return in try_fold
Move is_topo_consecutive into revwalk::is_continuous. Drop single selection match case, it's handled by catch-all.
1 parent 4a204aa commit 676a2e5

File tree

3 files changed

+52
-36
lines changed

3 files changed

+52
-36
lines changed

asyncgit/src/sync/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ mod rebase;
2424
pub mod remotes;
2525
mod repository;
2626
mod reset;
27-
mod revwalk;
27+
pub mod revwalk;
2828
mod reword;
2929
pub mod sign;
3030
mod staging;
@@ -90,7 +90,6 @@ pub use remotes::{
9090
pub(crate) use repository::repo;
9191
pub use repository::{RepoPath, RepoPathRef};
9292
pub use reset::{reset_repo, reset_stage, reset_workdir};
93-
pub use revwalk::revwalk;
9493
pub use reword::reword;
9594
pub use staging::{discard_lines, stage_lines};
9695
pub use stash::{

asyncgit/src/sync/revwalk.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use std::ops::Bound;
1+
//! git revwalk utils
2+
use std::ops::{Bound, ControlFlow};
23

34
use crate::Result;
45
use git2::{Commit, Oid};
@@ -58,3 +59,32 @@ fn resolve<'r>(
5859
Bound::Unbounded => Ok(None),
5960
}
6061
}
62+
63+
/// Checks if `commits` range is continuous under `sort` flags.
64+
pub fn is_continuous(
65+
repo_path: &RepoPath,
66+
sort: git2::Sort,
67+
commits: &[CommitId],
68+
) -> Result<bool> {
69+
match commits {
70+
[] | [_] => Ok(true),
71+
[start, .., end] => revwalk(
72+
repo_path,
73+
Bound::Excluded(start),
74+
Bound::Included(end),
75+
sort,
76+
|revwalk| match revwalk.zip(commits).try_fold(
77+
Ok(true),
78+
|acc, (r, c)| match r
79+
.map(CommitId::new)
80+
.and_then(|r| acc.map(|acc| acc && (&r == c)))
81+
{
82+
Ok(true) => ControlFlow::Continue(Ok(true)),
83+
otherwise => ControlFlow::Break(otherwise),
84+
},
85+
) {
86+
ControlFlow::Continue(v) | ControlFlow::Break(v) => v,
87+
},
88+
),
89+
}
90+
}

src/components/commitlist.rs

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ use ratatui::{
2929
Frame,
3030
};
3131
use std::{
32-
borrow::Cow, cell::Cell, cmp, collections::BTreeMap, ops::Bound,
33-
rc::Rc, time::Instant,
32+
borrow::Cow, cell::Cell, cmp, collections::BTreeMap, rc::Rc,
33+
time::Instant,
3434
};
3535

3636
const ELEMENTS_PER_LINE: usize = 9;
@@ -138,45 +138,32 @@ impl CommitList {
138138

139139
/// Build string of marked or selected (if none are marked) commit ids
140140
fn concat_selected_commit_ids(&self) -> Result<Option<String>> {
141-
match self.marked.as_slice() {
142-
[] => Ok(self
141+
let ret = match self.marked.as_slice() {
142+
[] => self
143143
.items
144144
.iter()
145145
.nth(
146146
self.selection
147147
.saturating_sub(self.items.index_offset()),
148148
)
149-
.map(|e| e.id.to_string())),
150-
[(_idx, commit)] => Ok(Some(commit.to_string())),
151-
[latest, .., earliest] => {
152-
let marked_rev = self.marked.iter().rev();
153-
let marked_topo_consecutive = revwalk(
149+
.map(|e| e.id.to_string()),
150+
[latest, .., earliest]
151+
if revwalk::is_continuous(
154152
&self.repo.borrow(),
155-
Bound::Excluded(&earliest.1),
156-
Bound::Included(&latest.1),
157-
Sort::TOPOLOGICAL | Sort::REVERSE,
158-
|revwalk| {
159-
revwalk.zip(marked_rev).try_fold(
160-
true,
161-
|acc, (r, m)| {
162-
let revwalked = CommitId::new(r?);
163-
let marked = m.1;
164-
Ok(acc && (revwalked == marked))
165-
},
166-
)
167-
},
168-
)?;
169-
let yank = if marked_topo_consecutive {
170-
format!("{}^..{}", earliest.1, latest.1)
171-
} else {
172-
self.marked
173-
.iter()
174-
.map(|(_idx, commit)| commit.to_string())
175-
.join(" ")
176-
};
177-
Ok(Some(yank))
153+
Sort::TOPOLOGICAL,
154+
&self.marked.iter().map(|x| x.1).collect_vec(),
155+
)? =>
156+
{
157+
Some(format!("{}^..{}", earliest.1, latest.1))
178158
}
179-
}
159+
marked => Some(
160+
marked
161+
.iter()
162+
.map(|(_idx, commit)| commit.to_string())
163+
.join(" "),
164+
),
165+
};
166+
Ok(ret)
180167
}
181168

182169
/// Copy currently marked or selected (if none are marked) commit ids

0 commit comments

Comments
 (0)