1
+ #![ allow( clippy:: missing_panics_doc) ]
2
+
1
3
use super :: CommitId ;
2
4
use crate :: error:: Result ;
3
5
use git2:: { Repository , Revwalk } ;
4
6
5
- ///
6
- pub enum Mode {
7
- HeadOnly ,
8
- AllRefs ,
9
- }
10
-
11
7
///
12
8
pub struct LogWalker < ' a > {
13
9
repo : & ' a Repository ,
14
10
revwalk : Option < Revwalk < ' a > > ,
15
- mode : Mode ,
11
+ limit : usize ,
16
12
}
17
13
18
14
impl < ' a > LogWalker < ' a > {
19
15
///
20
- pub const fn new ( repo : & ' a Repository ) -> Self {
16
+ pub const fn new ( repo : & ' a Repository , limit : usize ) -> Self {
21
17
Self {
22
18
repo,
23
19
revwalk : None ,
24
- mode : Mode :: HeadOnly ,
20
+ limit ,
25
21
}
26
22
}
27
23
28
24
///
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 > {
41
26
let mut count = 0_usize ;
42
27
43
28
if self . revwalk . is_none ( ) {
44
29
let mut walk = self . repo . revwalk ( ) ?;
45
30
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 ( ) ?;
53
32
54
33
self . revwalk = Some ( walk) ;
55
34
}
@@ -59,7 +38,7 @@ impl<'a> LogWalker<'a> {
59
38
out. push ( id. into ( ) ) ;
60
39
count += 1 ;
61
40
62
- if count == limit {
41
+ if count == self . limit {
63
42
break ;
64
43
}
65
44
}
@@ -73,11 +52,9 @@ impl<'a> LogWalker<'a> {
73
52
mod tests {
74
53
use super :: * ;
75
54
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,
79
57
} ;
80
- use git2:: Time ;
81
58
use pretty_assertions:: assert_eq;
82
59
use std:: { fs:: File , io:: Write , path:: Path } ;
83
60
@@ -96,8 +73,8 @@ mod tests {
96
73
let oid2 = commit ( repo_path, "commit2" ) . unwrap ( ) ;
97
74
98
75
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 ( ) ;
101
78
102
79
assert_eq ! ( items. len( ) , 1 ) ;
103
80
assert_eq ! ( items[ 0 ] , oid2. into( ) ) ;
@@ -120,8 +97,8 @@ mod tests {
120
97
let oid2 = commit ( repo_path, "commit2" ) . unwrap ( ) ;
121
98
122
99
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 ( ) ;
125
102
126
103
let info = get_commits_info ( repo_path, & items, 50 ) . unwrap ( ) ;
127
104
dbg ! ( & info) ;
@@ -130,66 +107,66 @@ mod tests {
130
107
assert_eq ! ( items[ 0 ] , oid2. into( ) ) ;
131
108
132
109
let mut items = Vec :: new ( ) ;
133
- walk. read ( & mut items, 100 ) . unwrap ( ) ;
110
+ walk. read ( & mut items) . unwrap ( ) ;
134
111
135
112
assert_eq ! ( items. len( ) , 0 ) ;
136
113
137
114
Ok ( ( ) )
138
115
}
139
116
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
+ // }
195
172
}
0 commit comments