1
1
use gix_hash:: ObjectId ;
2
2
use gix_odb:: FindExt ;
3
3
4
+ use crate :: ext:: ObjectIdExt ;
4
5
use crate :: { revision, Repository } ;
5
6
6
7
/// The error returned by [`Platform::all()`].
@@ -13,6 +14,73 @@ pub enum Error {
13
14
ShallowCommits ( #[ from] crate :: shallow:: open:: Error ) ,
14
15
}
15
16
17
+ /// Information about a commit that we obtained naturally as part of the iteration.
18
+ #[ derive( Debug , Clone ) ]
19
+ pub struct Info < ' repo > {
20
+ /// The detached id of the commit.
21
+ pub id : gix_hash:: ObjectId ,
22
+ /// All parent ids we have encountered. Note that these will be at most one if [`Parents::First`] is enabled.
23
+ pub parent_ids : gix_traverse:: commit:: ParentIds ,
24
+ /// The time at which the commit was created. It's only `Some(_)` if sorting is not [`Sorting::BreadthFirst`], as the walk
25
+ /// needs to require the commit-date.
26
+ pub commit_time : Option < u64 > ,
27
+
28
+ repo : & ' repo Repository ,
29
+ }
30
+
31
+ /// Access
32
+ impl < ' repo > Info < ' repo > {
33
+ /// Provide an attached version of our [`id`][Info::id] field.
34
+ pub fn id ( & self ) -> crate :: Id < ' repo > {
35
+ self . id . attach ( self . repo )
36
+ }
37
+
38
+ /// Read the whole object from the object database.
39
+ ///
40
+ /// Note that this is an expensive operation which shouldn't be performed unless one needs more than parent ids
41
+ /// and commit time.
42
+ pub fn object ( & self ) -> Result < crate :: Commit < ' repo > , crate :: object:: find:: existing:: Error > {
43
+ Ok ( self . id ( ) . object ( ) ?. into_commit ( ) )
44
+ }
45
+
46
+ /// Provide an iterator yielding attached versions of our [`parent_ids`][Info::parent_ids] field.
47
+ pub fn parent_ids ( & self ) -> impl Iterator < Item = crate :: Id < ' repo > > + ' _ {
48
+ self . parent_ids . iter ( ) . map ( |id| id. attach ( self . repo ) )
49
+ }
50
+
51
+ /// Returns the commit-time of this commit.
52
+ ///
53
+ /// ### Panics
54
+ ///
55
+ /// If the iteration wasn't ordered by date.
56
+ pub fn commit_time ( & self ) -> u64 {
57
+ self . commit_time . expect ( "traversal involving date caused it to be set" )
58
+ }
59
+ }
60
+
61
+ /// Initialization and detachment
62
+ impl < ' repo > Info < ' repo > {
63
+ /// Create a new instance that represents `info`, but is attached to `repo` as well.
64
+ pub fn new ( info : gix_traverse:: commit:: Info , repo : & ' repo Repository ) -> Self {
65
+ Info {
66
+ id : info. id ,
67
+ parent_ids : info. parent_ids ,
68
+ commit_time : info. commit_time ,
69
+ repo,
70
+ }
71
+ }
72
+ /// Consume this instance and remove the reference to the underlying repository.
73
+ ///
74
+ /// This is useful for sending instances across threads, for example.
75
+ pub fn detach ( self ) -> gix_traverse:: commit:: Info {
76
+ gix_traverse:: commit:: Info {
77
+ id : self . id ,
78
+ parent_ids : self . parent_ids ,
79
+ commit_time : self . commit_time ,
80
+ }
81
+ }
82
+ }
83
+
16
84
/// A platform to traverse the revision graph by adding starting points as well as points which shouldn't be crossed,
17
85
/// returned by [`Repository::rev_walk()`].
18
86
///
@@ -41,7 +109,7 @@ impl<'repo> Platform<'repo> {
41
109
42
110
/// Create-time builder methods
43
111
impl < ' repo > Platform < ' repo > {
44
- /// Set the sort mode for commits to the given value. The default is to order by topology .
112
+ /// Set the sort mode for commits to the given value. The default is to order topologically breadth-first .
45
113
pub fn sorting ( mut self , sorting : gix_traverse:: commit:: Sorting ) -> Self {
46
114
self . sorting = sorting;
47
115
self
@@ -117,9 +185,7 @@ impl<'repo> Platform<'repo> {
117
185
)
118
186
. sorting ( sorting) ?
119
187
. parents ( parents)
120
- . commit_graph ( use_commit_graph. then ( || self . repo . commit_graph ( ) . ok ( ) ) . flatten ( ) )
121
- // TODO: use wrapped items instead
122
- . map ( |res| res. map ( |item| item. id ) ) ,
188
+ . commit_graph ( use_commit_graph. then ( || self . repo . commit_graph ( ) . ok ( ) ) . flatten ( ) ) ,
123
189
) ,
124
190
} )
125
191
}
@@ -135,20 +201,21 @@ impl<'repo> Platform<'repo> {
135
201
}
136
202
137
203
pub ( crate ) mod iter {
138
- use crate :: { ext:: ObjectIdExt , Id } ;
139
-
140
204
/// The iterator returned by [`crate::revision::walk::Platform::all()`].
141
205
pub struct Walk < ' repo > {
142
206
pub ( crate ) repo : & ' repo crate :: Repository ,
143
- pub ( crate ) inner :
144
- Box < dyn Iterator < Item = Result < gix_hash:: ObjectId , gix_traverse:: commit:: ancestors:: Error > > + ' repo > ,
207
+ pub ( crate ) inner : Box <
208
+ dyn Iterator < Item = Result < gix_traverse:: commit:: Info , gix_traverse:: commit:: ancestors:: Error > > + ' repo ,
209
+ > ,
145
210
}
146
211
147
212
impl < ' repo > Iterator for Walk < ' repo > {
148
- type Item = Result < Id < ' repo > , gix_traverse:: commit:: ancestors:: Error > ;
213
+ type Item = Result < super :: Info < ' repo > , gix_traverse:: commit:: ancestors:: Error > ;
149
214
150
215
fn next ( & mut self ) -> Option < Self :: Item > {
151
- self . inner . next ( ) . map ( |res| res. map ( |id| id. attach ( self . repo ) ) )
216
+ self . inner
217
+ . next ( )
218
+ . map ( |res| res. map ( |info| super :: Info :: new ( info, self . repo ) ) )
152
219
}
153
220
}
154
221
}
0 commit comments