1+ #![ allow( clippy:: missing_panics_doc) ]
2+
13use super :: CommitId ;
24use crate :: error:: Result ;
35use git2:: { Repository , Revwalk } ;
46
5- ///
6- pub enum Mode {
7- HeadOnly ,
8- AllRefs ,
9- }
10-
117///
128pub struct LogWalker < ' a > {
139 repo : & ' a Repository ,
1410 revwalk : Option < Revwalk < ' a > > ,
15- mode : Mode ,
11+ limit : usize ,
1612}
1713
1814impl < ' a > LogWalker < ' a > {
1915 ///
20- pub const fn new ( repo : & ' a Repository ) -> Self {
16+ pub const fn new ( repo : & ' a Repository , limit : usize ) -> Self {
2117 Self {
2218 repo,
2319 revwalk : None ,
24- mode : Mode :: HeadOnly ,
20+ limit ,
2521 }
2622 }
2723
2824 ///
29- pub const fn mode ( self , mode : Mode ) -> Self {
30- let mut res = self ;
31- res. mode = mode;
32- res
33- }
34-
35- ///
36- pub fn read (
37- & mut self ,
38- out : & mut Vec < CommitId > ,
39- limit : usize ,
40- ) -> Result < usize > {
25+ pub fn read ( & mut self , out : & mut Vec < CommitId > ) -> Result < usize > {
4126 let mut count = 0_usize ;
4227
4328 if self . revwalk . is_none ( ) {
4429 let mut walk = self . repo . revwalk ( ) ?;
4530
46- // note: setting a sorting sifnificantly slows down big revwalks
47-
48- if matches ! ( self . mode, Mode :: HeadOnly ) {
49- walk. push_head ( ) ?;
50- } else {
51- walk. push_glob ( "*" ) ?;
52- }
31+ walk. push_head ( ) ?;
5332
5433 self . revwalk = Some ( walk) ;
5534 }
@@ -59,7 +38,7 @@ impl<'a> LogWalker<'a> {
5938 out. push ( id. into ( ) ) ;
6039 count += 1 ;
6140
62- if count == limit {
41+ if count == self . limit {
6342 break ;
6443 }
6544 }
@@ -73,11 +52,9 @@ impl<'a> LogWalker<'a> {
7352mod tests {
7453 use super :: * ;
7554 use crate :: sync:: {
76- checkout_branch, commit, create_branch, get_commits_info,
77- stage_add_file,
78- tests:: { repo_init_empty, write_commit_file_at} ,
55+ commit, get_commits_info, stage_add_file,
56+ tests:: repo_init_empty,
7957 } ;
80- use git2:: Time ;
8158 use pretty_assertions:: assert_eq;
8259 use std:: { fs:: File , io:: Write , path:: Path } ;
8360
@@ -96,8 +73,8 @@ mod tests {
9673 let oid2 = commit ( repo_path, "commit2" ) . unwrap ( ) ;
9774
9875 let mut items = Vec :: new ( ) ;
99- let mut walk = LogWalker :: new ( & repo) ;
100- walk. read ( & mut items, 1 ) . unwrap ( ) ;
76+ let mut walk = LogWalker :: new ( & repo, 1 ) ;
77+ walk. read ( & mut items) . unwrap ( ) ;
10178
10279 assert_eq ! ( items. len( ) , 1 ) ;
10380 assert_eq ! ( items[ 0 ] , oid2. into( ) ) ;
@@ -120,8 +97,8 @@ mod tests {
12097 let oid2 = commit ( repo_path, "commit2" ) . unwrap ( ) ;
12198
12299 let mut items = Vec :: new ( ) ;
123- let mut walk = LogWalker :: new ( & repo) ;
124- walk. read ( & mut items, 100 ) . unwrap ( ) ;
100+ let mut walk = LogWalker :: new ( & repo, 100 ) ;
101+ walk. read ( & mut items) . unwrap ( ) ;
125102
126103 let info = get_commits_info ( repo_path, & items, 50 ) . unwrap ( ) ;
127104 dbg ! ( & info) ;
@@ -130,66 +107,66 @@ mod tests {
130107 assert_eq ! ( items[ 0 ] , oid2. into( ) ) ;
131108
132109 let mut items = Vec :: new ( ) ;
133- walk. read ( & mut items, 100 ) . unwrap ( ) ;
110+ walk. read ( & mut items) . unwrap ( ) ;
134111
135112 assert_eq ! ( items. len( ) , 0 ) ;
136113
137114 Ok ( ( ) )
138115 }
139116
140- fn walk_all_commits ( repo : & Repository ) -> Vec < CommitId > {
141- let mut items = Vec :: new ( ) ;
142- let mut walk = LogWalker :: new ( & repo) . mode ( Mode :: AllRefs ) ;
143- walk. read ( & mut items, 10 ) . unwrap ( ) ;
144- items
145- }
146-
147- #[ test]
148- fn test_multiple_branches ( ) {
149- let ( td, repo) = repo_init_empty ( ) . unwrap ( ) ;
150- let repo_path = td. path ( ) . to_string_lossy ( ) ;
151-
152- let c1 = write_commit_file_at (
153- & repo,
154- "test.txt" ,
155- "" ,
156- "c1" ,
157- Time :: new ( 1 , 0 ) ,
158- ) ;
159-
160- let items = walk_all_commits ( & repo) ;
161-
162- assert_eq ! ( items, vec![ c1] ) ;
163-
164- let b1 = create_branch ( & repo_path, "b1" ) . unwrap ( ) ;
165-
166- let c2 = write_commit_file_at (
167- & repo,
168- "test2.txt" ,
169- "" ,
170- "c2" ,
171- Time :: new ( 2 , 0 ) ,
172- ) ;
173-
174- let items = walk_all_commits ( & repo) ;
175- assert_eq ! ( items, vec![ c2, c1] ) ;
176-
177- let _b2 = create_branch ( & repo_path, "b2" ) . unwrap ( ) ;
178-
179- let c3 = write_commit_file_at (
180- & repo,
181- "test3.txt" ,
182- "" ,
183- "c3" ,
184- Time :: new ( 3 , 0 ) ,
185- ) ;
186-
187- let items = walk_all_commits ( & repo) ;
188- assert_eq ! ( items, vec![ c2, c3, c1] ) ;
189-
190- checkout_branch ( & repo_path, & b1) . unwrap ( ) ;
191-
192- let items = walk_all_commits ( & repo) ;
193- assert_eq ! ( items, vec![ c2, c3, c1] ) ;
194- }
117+ // fn walk_all_commits(repo: &Repository) -> Vec<CommitId> {
118+ // let mut items = Vec::new();
119+ // let mut walk = LogWalker::new(&repo).mode(Mode::AllRefs);
120+ // walk.read(&mut items, 10).unwrap();
121+ // items
122+ // }
123+
124+ // #[test]
125+ // fn test_multiple_branches() {
126+ // let (td, repo) = repo_init_empty().unwrap();
127+ // let repo_path = td.path().to_string_lossy();
128+
129+ // let c1 = write_commit_file_at(
130+ // &repo,
131+ // "test.txt",
132+ // "",
133+ // "c1",
134+ // Time::new(1, 0),
135+ // );
136+
137+ // let items = walk_all_commits(&repo);
138+
139+ // assert_eq!(items, vec![c1]);
140+
141+ // let b1 = create_branch(&repo_path, "b1").unwrap();
142+
143+ // let c2 = write_commit_file_at(
144+ // &repo,
145+ // "test2.txt",
146+ // "",
147+ // "c2",
148+ // Time::new(2, 0),
149+ // );
150+
151+ // let items = walk_all_commits(&repo);
152+ // assert_eq!(items, vec![c2, c1]);
153+
154+ // let _b2 = create_branch(&repo_path, "b2").unwrap();
155+
156+ // let c3 = write_commit_file_at(
157+ // &repo,
158+ // "test3.txt",
159+ // "",
160+ // "c3",
161+ // Time::new(3, 0),
162+ // );
163+
164+ // let items = walk_all_commits(&repo);
165+ // assert_eq!(items, vec![c2, c3, c1]);
166+
167+ // checkout_branch(&repo_path, &b1).unwrap();
168+
169+ // let items = walk_all_commits(&repo);
170+ // assert_eq!(items, vec![c2, c3, c1]);
171+ // }
195172}
0 commit comments