-
-
Notifications
You must be signed in to change notification settings - Fork 753
Expand file tree
/
Copy pathlogwalker.rs
More file actions
362 lines (290 loc) · 9.43 KB
/
Copy pathlogwalker.rs
File metadata and controls
362 lines (290 loc) · 9.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
use super::{CommitId, SharedCommitFilterFn};
use crate::error::Result;
use git2::{Repository, Revwalk, Sort};
use gix::revision::Walk;
/// Visit commits in topological order, and date order where possible.
/// If a filter is provided, only commits that pass the filter are returned.
pub struct LogWalker<'a> {
/// Revision walk engine
walk: Revwalk<'a>,
/// Total number of commits that have been visited
visited_count: usize,
/// Upper limit on buffer size
limit: usize,
/// The source of commits
repo: &'a Repository,
/// Filter on which commits will be returned when reading
filter: Option<SharedCommitFilterFn>,
}
impl<'a> LogWalker<'a> {
/// Create a new log walker
/// with an upper limit on number of commits visited in one batch.
pub fn new(repo: &'a Repository, limit: usize) -> Result<Self> {
let head = repo.head()?.peel_to_commit()?;
let mut walk = repo.revwalk()?;
// TOPOLOGICAL + TIME guarantees parents come after children,
// and ties/independent branches are ordered by timestamp (--date-order).
walk.set_sorting(Sort::TOPOLOGICAL | Sort::TIME)?;
walk.push(head.id())?;
Ok(Self {
walk,
visited_count: 0,
limit,
repo,
filter: None,
})
}
/// Number of visited commits
pub const fn visited(&self) -> usize {
self.visited_count
}
/// Add a filter to use when reading commits
#[must_use]
pub fn filter(
self,
filter: Option<SharedCommitFilterFn>,
) -> Self {
Self { filter, ..self }
}
/// Get a batch of commits
pub fn read(&mut self, out: &mut Vec<CommitId>) -> Result<usize> {
let mut count = 0_usize;
for oid_result in self.walk.by_ref() {
let oid = oid_result?;
let id: CommitId = oid.into();
let commit_should_be_included =
if let Some(ref filter) = self.filter {
filter(self.repo, &id)?
} else {
true
};
if commit_should_be_included {
out.push(id);
}
self.visited_count += 1;
count += 1;
if count == self.limit {
break;
}
}
Ok(count)
}
}
/// This is separate from `LogWalker` because filtering currently (June 2024) works through
/// `SharedCommitFilterFn`.
///
/// `SharedCommitFilterFn` requires access to a `git2::repo::Repository` because, under the hood,
/// it calls into functions that work with a `git2::repo::Repository`. It seems unwise to open a
/// repo both through `gix::discover` and `Repository::open_ext` at the same time, so there is a
/// separate struct that works with `gix::Repository` only.
///
/// A more long-term option is to refactor filtering to work with a `gix::Repository` and to remove
/// `LogWalker` once this is done, but this is a larger effort.
pub struct LogWalkerWithoutFilter<'a> {
walk: Walk<'a>,
limit: usize,
visited: usize,
}
impl<'a> LogWalkerWithoutFilter<'a> {
///
pub fn new(
repo: &'a mut gix::Repository,
limit: usize,
) -> Result<Self> {
// This seems to be an object cache size that yields optimal performance. There’s no specific
// reason this is 2^14, so benchmarking might reveal that there’s better values.
repo.object_cache_size_if_unset(2_usize.pow(14));
let commit = repo.head()?.peel_to_commit()?;
let tips = [commit.id];
let platform = repo
.rev_walk(tips)
.sorting(gix::revision::walk::Sorting::ByCommitTime(gix::traverse::commit::simple::CommitTimeOrder::NewestFirst))
.use_commit_graph(false);
let walk = platform.all()?;
Ok(Self {
walk,
limit,
visited: 0,
})
}
///
pub const fn visited(&self) -> usize {
self.visited
}
///
pub fn read(&mut self, out: &mut Vec<CommitId>) -> Result<usize> {
let mut count = 0_usize;
while let Some(Ok(info)) = self.walk.next() {
out.push(info.id.into());
count += 1;
if count == self.limit {
break;
}
}
self.visited += count;
Ok(count)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::error::Result;
use crate::sync::commit_filter::{SearchFields, SearchOptions};
use crate::sync::repository::gix_repo;
use crate::sync::tests::write_commit_file;
use crate::sync::{
commit, get_commits_info, stage_add_file,
tests::repo_init_empty,
};
use crate::sync::{
diff_contains_file, filter_commit_by_search, LogFilterSearch,
LogFilterSearchOptions, RepoPath,
};
use pretty_assertions::assert_eq;
use std::{fs::File, io::Write, path::Path};
#[test]
fn test_limit() -> Result<()> {
let file_path = Path::new("foo");
let (_td, repo) = repo_init_empty().unwrap();
let root = repo.path().parent().unwrap();
let repo_path: &RepoPath =
&root.as_os_str().to_str().unwrap().into();
File::create(root.join(file_path))?.write_all(b"a")?;
stage_add_file(repo_path, file_path).unwrap();
commit(repo_path, "commit1").unwrap();
File::create(root.join(file_path))?.write_all(b"a")?;
stage_add_file(repo_path, file_path).unwrap();
let oid2 = commit(repo_path, "commit2").unwrap();
let mut items = Vec::new();
let mut walk = LogWalker::new(&repo, 1)?;
walk.read(&mut items).unwrap();
assert_eq!(items.len(), 1);
assert_eq!(items[0], oid2);
Ok(())
}
#[test]
fn test_logwalker() -> Result<()> {
let file_path = Path::new("foo");
let (_td, repo) = repo_init_empty().unwrap();
let root = repo.path().parent().unwrap();
let repo_path: &RepoPath =
&root.as_os_str().to_str().unwrap().into();
File::create(root.join(file_path))?.write_all(b"a")?;
stage_add_file(repo_path, file_path).unwrap();
commit(repo_path, "commit1").unwrap();
File::create(root.join(file_path))?.write_all(b"a")?;
stage_add_file(repo_path, file_path).unwrap();
let oid2 = commit(repo_path, "commit2").unwrap();
let mut items = Vec::new();
let mut walk = LogWalker::new(&repo, 100)?;
walk.read(&mut items).unwrap();
let info = get_commits_info(repo_path, &items, 50).unwrap();
dbg!(&info);
assert_eq!(items.len(), 2);
assert_eq!(items[0], oid2);
let mut items = Vec::new();
walk.read(&mut items).unwrap();
assert_eq!(items.len(), 0);
Ok(())
}
#[test]
fn test_logwalker_without_filter() -> Result<()> {
let file_path = Path::new("foo");
let (_td, repo) = repo_init_empty().unwrap();
let root = repo.path().parent().unwrap();
let repo_path: &RepoPath =
&root.as_os_str().to_str().unwrap().into();
File::create(root.join(file_path))?.write_all(b"a")?;
stage_add_file(repo_path, file_path).unwrap();
commit(repo_path, "commit1").unwrap();
File::create(root.join(file_path))?.write_all(b"a")?;
stage_add_file(repo_path, file_path).unwrap();
let oid2 = commit(repo_path, "commit2").unwrap();
let mut repo: gix::Repository = gix_repo(repo_path)?;
let mut walk = LogWalkerWithoutFilter::new(&mut repo, 100)?;
let mut items = Vec::new();
assert!(matches!(walk.read(&mut items), Ok(2)));
let info = get_commits_info(repo_path, &items, 50).unwrap();
dbg!(&info);
assert_eq!(items.len(), 2);
assert_eq!(items[0], oid2);
let mut items = Vec::new();
assert!(matches!(walk.read(&mut items), Ok(0)));
assert_eq!(items.len(), 0);
Ok(())
}
#[test]
fn test_logwalker_with_filter() -> Result<()> {
let file_path = Path::new("foo");
let second_file_path = Path::new("baz");
let (_td, repo) = repo_init_empty().unwrap();
let root = repo.path().parent().unwrap();
let repo_path: RepoPath =
root.as_os_str().to_str().unwrap().into();
File::create(root.join(file_path))?.write_all(b"a")?;
stage_add_file(&repo_path, file_path).unwrap();
let _first_commit_id = commit(&repo_path, "commit1").unwrap();
File::create(root.join(second_file_path))?.write_all(b"a")?;
stage_add_file(&repo_path, second_file_path).unwrap();
let second_commit_id = commit(&repo_path, "commit2").unwrap();
File::create(root.join(file_path))?.write_all(b"b")?;
stage_add_file(&repo_path, file_path).unwrap();
let _third_commit_id = commit(&repo_path, "commit3").unwrap();
let diff_contains_baz = diff_contains_file("baz".into());
let mut items = Vec::new();
let mut walker = LogWalker::new(&repo, 100)?
.filter(Some(diff_contains_baz));
walker.read(&mut items).unwrap();
assert_eq!(items.len(), 1);
assert_eq!(items[0], second_commit_id);
let mut items = Vec::new();
walker.read(&mut items).unwrap();
assert_eq!(items.len(), 0);
let diff_contains_bar = diff_contains_file("bar".into());
let mut items = Vec::new();
let mut walker = LogWalker::new(&repo, 100)?
.filter(Some(diff_contains_bar));
walker.read(&mut items).unwrap();
assert_eq!(items.len(), 0);
Ok(())
}
#[test]
fn test_logwalker_with_filter_search() {
let (_td, repo) = repo_init_empty().unwrap();
write_commit_file(&repo, "foo", "a", "commit1");
let second_commit_id = write_commit_file(
&repo,
"baz",
"a",
"my commit msg (#2)",
);
write_commit_file(&repo, "foo", "b", "commit3");
let log_filter = filter_commit_by_search(
LogFilterSearch::new(LogFilterSearchOptions {
fields: SearchFields::MESSAGE_SUMMARY,
options: SearchOptions::FUZZY_SEARCH,
search_pattern: String::from("my msg"),
}),
);
let mut items = Vec::new();
let mut walker = LogWalker::new(&repo, 100)
.unwrap()
.filter(Some(log_filter));
walker.read(&mut items).unwrap();
assert_eq!(items.len(), 1);
assert_eq!(items[0], second_commit_id);
let log_filter = filter_commit_by_search(
LogFilterSearch::new(LogFilterSearchOptions {
fields: SearchFields::FILENAMES,
options: SearchOptions::FUZZY_SEARCH,
search_pattern: String::from("fo"),
}),
);
let mut items = Vec::new();
let mut walker = LogWalker::new(&repo, 100)
.unwrap()
.filter(Some(log_filter));
walker.read(&mut items).unwrap();
assert_eq!(items.len(), 2);
}
}