Skip to content

Commit 011fdec

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 9c68fe9 commit 011fdec

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;
@@ -132,45 +132,32 @@ impl CommitList {
132132

133133
/// Build string of marked or selected (if none are marked) commit ids
134134
fn concat_selected_commit_ids(&self) -> Result<Option<String>> {
135-
match self.marked.as_slice() {
136-
[] => Ok(self
135+
let ret = match self.marked.as_slice() {
136+
[] => self
137137
.items
138138
.iter()
139139
.nth(
140140
self.selection
141141
.saturating_sub(self.items.index_offset()),
142142
)
143-
.map(|e| e.id.to_string())),
144-
[(_idx, commit)] => Ok(Some(commit.to_string())),
145-
[latest, .., earliest] => {
146-
let marked_rev = self.marked.iter().rev();
147-
let marked_topo_consecutive = revwalk(
143+
.map(|e| e.id.to_string()),
144+
[latest, .., earliest]
145+
if revwalk::is_continuous(
148146
&self.repo.borrow(),
149-
Bound::Excluded(&earliest.1),
150-
Bound::Included(&latest.1),
151-
Sort::TOPOLOGICAL | Sort::REVERSE,
152-
|revwalk| {
153-
revwalk.zip(marked_rev).try_fold(
154-
true,
155-
|acc, (r, m)| {
156-
let revwalked = CommitId::new(r?);
157-
let marked = m.1;
158-
Ok(acc && (revwalked == marked))
159-
},
160-
)
161-
},
162-
)?;
163-
let yank = if marked_topo_consecutive {
164-
format!("{}^..{}", earliest.1, latest.1)
165-
} else {
166-
self.marked
167-
.iter()
168-
.map(|(_idx, commit)| commit.to_string())
169-
.join(" ")
170-
};
171-
Ok(Some(yank))
147+
Sort::TOPOLOGICAL,
148+
&self.marked.iter().map(|x| x.1).collect_vec(),
149+
)? =>
150+
{
151+
Some(format!("{}^..{}", earliest.1, latest.1))
172152
}
173-
}
153+
marked => Some(
154+
marked
155+
.iter()
156+
.map(|(_idx, commit)| commit.to_string())
157+
.join(" "),
158+
),
159+
};
160+
Ok(ret)
174161
}
175162

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

0 commit comments

Comments
 (0)