Skip to content

Commit bce652e

Browse files
cruesslerStephan Dilly
authored andcommitted
Add filter to AsyncLog
This is a small change that makes it possible to reuse the logic in `AsyncLog` for the file history view. `AsyncLog` passes the filter to `FileLogWalker` unchanged.
1 parent 7f0a236 commit bce652e

File tree

4 files changed

+24
-14
lines changed

4 files changed

+24
-14
lines changed

asyncgit/src/revlog.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
22
error::Result,
3-
sync::{utils::repo, CommitId, LogWalker},
3+
sync::{utils::repo, CommitId, LogWalker, LogWalkerFilter},
44
AsyncGitNotification, CWD,
55
};
66
use crossbeam_channel::Sender;
@@ -32,6 +32,7 @@ pub struct AsyncLog {
3232
sender: Sender<AsyncGitNotification>,
3333
pending: Arc<AtomicBool>,
3434
background: Arc<AtomicBool>,
35+
filter: Option<LogWalkerFilter>,
3536
}
3637

3738
static LIMIT_COUNT: usize = 3000;
@@ -40,12 +41,16 @@ static SLEEP_BACKGROUND: Duration = Duration::from_millis(1000);
4041

4142
impl AsyncLog {
4243
///
43-
pub fn new(sender: &Sender<AsyncGitNotification>) -> Self {
44+
pub fn new(
45+
sender: &Sender<AsyncGitNotification>,
46+
filter: Option<LogWalkerFilter>,
47+
) -> Self {
4448
Self {
4549
current: Arc::new(Mutex::new(Vec::new())),
4650
sender: sender.clone(),
4751
pending: Arc::new(AtomicBool::new(false)),
4852
background: Arc::new(AtomicBool::new(false)),
53+
filter,
4954
}
5055
}
5156

@@ -126,13 +131,16 @@ impl AsyncLog {
126131

127132
self.pending.store(true, Ordering::Relaxed);
128133

134+
let filter = self.filter.clone();
135+
129136
rayon_core::spawn(move || {
130137
scope_time!("async::revlog");
131138

132139
Self::fetch_helper(
133140
&arc_current,
134141
&arc_background,
135142
&sender,
143+
filter,
136144
)
137145
.expect("failed to fetch");
138146

@@ -148,10 +156,12 @@ impl AsyncLog {
148156
arc_current: &Arc<Mutex<Vec<CommitId>>>,
149157
arc_background: &Arc<AtomicBool>,
150158
sender: &Sender<AsyncGitNotification>,
159+
filter: Option<LogWalkerFilter>,
151160
) -> Result<()> {
152161
let mut entries = Vec::with_capacity(LIMIT_COUNT);
153162
let r = repo(CWD)?;
154-
let mut walker = LogWalker::new(&r, LIMIT_COUNT)?;
163+
let mut walker =
164+
LogWalker::new(&r, LIMIT_COUNT)?.filter(filter);
155165
loop {
156166
entries.clear();
157167
let res_is_err = walker.read(&mut entries).is_err();

asyncgit/src/sync/logwalker.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use git2::{Commit, Oid, Repository};
44
use std::{
55
cmp::Ordering,
66
collections::{BinaryHeap, HashSet},
7+
sync::Arc,
78
};
89

910
struct TimeOrderedCommit<'a>(Commit<'a>);
@@ -28,8 +29,10 @@ impl<'a> Ord for TimeOrderedCommit<'a> {
2829
}
2930
}
3031

31-
type LogWalkerFilter =
32-
Box<dyn Fn(&Repository, &CommitId) -> Result<bool>>;
32+
///
33+
pub type LogWalkerFilter = Arc<
34+
Box<dyn Fn(&Repository, &CommitId) -> Result<bool> + Send + Sync>,
35+
>;
3336

3437
///
3538
pub struct LogWalker<'a> {
@@ -58,11 +61,8 @@ impl<'a> LogWalker<'a> {
5861
}
5962

6063
///
61-
pub fn filter(self, filter: LogWalkerFilter) -> Self {
62-
Self {
63-
filter: Some(filter),
64-
..self
65-
}
64+
pub fn filter(self, filter: Option<LogWalkerFilter>) -> Self {
65+
Self { filter, ..self }
6666
}
6767

6868
///
@@ -211,7 +211,7 @@ mod tests {
211211

212212
let mut items = Vec::new();
213213
let mut walker = LogWalker::new(&repo, 100)?
214-
.filter(Box::new(diff_contains_baz));
214+
.filter(Some(Arc::new(Box::new(diff_contains_baz))));
215215
walker.read(&mut items).unwrap();
216216

217217
assert_eq!(items.len(), 1);
@@ -238,7 +238,7 @@ mod tests {
238238

239239
let mut items = Vec::new();
240240
let mut walker = LogWalker::new(&repo, 100)?
241-
.filter(Box::new(diff_contains_bar));
241+
.filter(Some(Arc::new(Box::new(diff_contains_bar))));
242242
walker.read(&mut items).unwrap();
243243

244244
assert_eq!(items.len(), 0);

asyncgit/src/sync/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pub use hooks::{
5555
};
5656
pub use hunks::{reset_hunk, stage_hunk, unstage_hunk};
5757
pub use ignore::add_to_ignore;
58-
pub use logwalker::LogWalker;
58+
pub use logwalker::{LogWalker, LogWalkerFilter};
5959
pub use merge::{
6060
abort_merge, merge_branch, merge_commit, merge_msg, mergehead_ids,
6161
};

src/tabs/revlog.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl Revlog {
6060
theme,
6161
key_config.clone(),
6262
),
63-
git_log: AsyncLog::new(sender),
63+
git_log: AsyncLog::new(sender, None),
6464
git_tags: AsyncTags::new(sender),
6565
visible: false,
6666
branch_name: cached::BranchName::new(CWD),

0 commit comments

Comments
 (0)