forked from gitui-org/gitui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogwalker.rs
More file actions
113 lines (92 loc) · 2.96 KB
/
Copy pathlogwalker.rs
File metadata and controls
113 lines (92 loc) · 2.96 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
use super::CommitId;
use crate::error::Result;
use git2::{Repository, Revwalk};
///
pub struct LogWalker<'a> {
repo: &'a Repository,
revwalk: Option<Revwalk<'a>>,
}
impl<'a> LogWalker<'a> {
///
pub fn new(repo: &'a Repository) -> Self {
Self {
repo,
revwalk: None,
}
}
///
pub fn read(
&mut self,
out: &mut Vec<CommitId>,
limit: usize,
) -> Result<usize> {
let mut count = 0_usize;
if self.revwalk.is_none() {
let mut walk = self.repo.revwalk()?;
walk.push_head()?;
self.revwalk = Some(walk);
}
if let Some(ref mut walk) = self.revwalk {
for id in walk.into_iter().flatten() {
out.push(id.into());
count += 1;
if count == limit {
break;
}
}
}
Ok(count)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::sync::{
commit, get_commits_info, stage_add_file,
tests::repo_init_empty,
};
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 = root.as_os_str().to_str().unwrap();
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);
walk.read(&mut items, 1).unwrap();
assert_eq!(items.len(), 1);
assert_eq!(items[0], oid2.into());
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 = root.as_os_str().to_str().unwrap();
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);
walk.read(&mut items, 100).unwrap();
let info = get_commits_info(repo_path, &items, 50).unwrap();
dbg!(&info);
assert_eq!(items.len(), 2);
assert_eq!(items[0], oid2.into());
let mut items = Vec::new();
walk.read(&mut items, 100).unwrap();
assert_eq!(items.len(), 0);
Ok(())
}
}