@@ -2,10 +2,17 @@ use super::CommitId;
2
2
use crate :: error:: Result ;
3
3
use git2:: { Repository , Revwalk , Sort } ;
4
4
5
+ ///
6
+ pub enum Mode {
7
+ HeadOnly ,
8
+ AllRefs ,
9
+ }
10
+
5
11
///
6
12
pub struct LogWalker < ' a > {
7
13
repo : & ' a Repository ,
8
14
revwalk : Option < Revwalk < ' a > > ,
15
+ mode : Mode ,
9
16
}
10
17
11
18
impl < ' a > LogWalker < ' a > {
@@ -14,9 +21,17 @@ impl<'a> LogWalker<'a> {
14
21
Self {
15
22
repo,
16
23
revwalk : None ,
24
+ mode : Mode :: HeadOnly ,
17
25
}
18
26
}
19
27
28
+ ///
29
+ pub const fn mode ( self , mode : Mode ) -> Self {
30
+ let mut res = self ;
31
+ res. mode = mode;
32
+ res
33
+ }
34
+
20
35
///
21
36
pub fn read (
22
37
& mut self ,
@@ -28,7 +43,12 @@ impl<'a> LogWalker<'a> {
28
43
if self . revwalk . is_none ( ) {
29
44
let mut walk = self . repo . revwalk ( ) ?;
30
45
31
- walk. push_head ( ) ?;
46
+ if matches ! ( self . mode, Mode :: HeadOnly ) {
47
+ walk. push_head ( ) ?;
48
+ } else {
49
+ walk. push_glob ( "*" ) ?;
50
+ }
51
+
32
52
walk. set_sorting ( Sort :: TIME ) ?;
33
53
34
54
self . revwalk = Some ( walk) ;
@@ -53,9 +73,12 @@ impl<'a> LogWalker<'a> {
53
73
mod tests {
54
74
use super :: * ;
55
75
use crate :: sync:: {
56
- commit, get_commits_info, stage_add_file,
57
- tests:: repo_init_empty,
76
+ checkout_branch, commit, create_branch, get_commits_info,
77
+ stage_add_file,
78
+ tests:: { repo_init_empty, write_commit_file_at} ,
58
79
} ;
80
+ use git2:: Time ;
81
+ use pretty_assertions:: assert_eq;
59
82
use std:: { fs:: File , io:: Write , path:: Path } ;
60
83
61
84
#[ test]
@@ -113,4 +136,60 @@ mod tests {
113
136
114
137
Ok ( ( ) )
115
138
}
139
+
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![ c3, c2, c1] ) ;
189
+
190
+ checkout_branch ( & repo_path, & b1) . unwrap ( ) ;
191
+
192
+ let items = walk_all_commits ( & repo) ;
193
+ assert_eq ! ( items, vec![ c3, c2, c1] ) ;
194
+ }
116
195
}
0 commit comments