@@ -3,6 +3,36 @@ use gix_hash::oid;
3
3
use smallvec:: SmallVec ;
4
4
use std:: ops:: Index ;
5
5
6
+ /// The time in seconds since unix epoch at which a commit was created.
7
+ pub type CommitterTimestamp = u64 ;
8
+
9
+ impl < ' find , T : Default > Graph < ' find , T > {
10
+ /// Lookup `id` without failing if the commit doesn't exist, and assure that `id` is inserted into our set.
11
+ /// If it wasn't, associate it with the default value. Assure `update_data(data)` gets run.
12
+ /// Return the commit when done.
13
+ /// Note that none of the data updates happen if there was no commit named `id`.
14
+ pub fn try_lookup_and_insert (
15
+ & mut self ,
16
+ id : gix_hash:: ObjectId ,
17
+ update_data : impl FnOnce ( & mut T ) ,
18
+ ) -> Result < Option < Commit < ' _ > > , lookup:: Error > {
19
+ let res = try_lookup ( & id, & mut self . find , self . cache . as_ref ( ) , & mut self . buf ) ?;
20
+ Ok ( res. map ( |commit| {
21
+ match self . set . entry ( id) {
22
+ gix_hashtable:: hash_map:: Entry :: Vacant ( entry) => {
23
+ let mut data = T :: default ( ) ;
24
+ update_data ( & mut data) ;
25
+ entry. insert ( data) ;
26
+ }
27
+ gix_hashtable:: hash_map:: Entry :: Occupied ( mut entry) => {
28
+ update_data ( entry. get_mut ( ) ) ;
29
+ }
30
+ } ;
31
+ commit
32
+ } ) )
33
+ }
34
+ }
35
+
6
36
impl < ' find , T > Graph < ' find , T > {
7
37
/// Create a new instance with `find` to retrieve commits and optionally `cache` to accelerate commit access.
8
38
pub fn new < Find , E > ( mut find : Find , cache : impl Into < Option < gix_commitgraph:: Graph > > ) -> Self
@@ -66,7 +96,7 @@ impl<'find, T> Graph<'find, T> {
66
96
pub fn insert_parents (
67
97
& mut self ,
68
98
id : & gix_hash:: oid ,
69
- mut new_parent_data : impl FnMut ( gix_hash:: ObjectId , u64 ) -> T ,
99
+ mut new_parent_data : impl FnMut ( gix_hash:: ObjectId , CommitterTimestamp ) -> T ,
70
100
mut update_existing : impl FnMut ( gix_hash:: ObjectId , & mut T ) ,
71
101
first_parent : bool ,
72
102
) -> Result < ( ) , insert_parents:: Error > {
@@ -135,10 +165,10 @@ impl<'a, 'find, T> Index<&'a gix_hash::oid> for Graph<'find, T> {
135
165
pub mod lookup {
136
166
/// The error returned by [`try_lookup()`][crate::Graph::try_lookup()].
137
167
#[ derive( Debug , thiserror:: Error ) ]
138
- #[ allow ( missing_docs ) ]
139
- pub enum Error {
140
- #[ error ( "There was an error looking up a commit" ) ]
141
- Find ( # [ from ] Box < dyn std:: error:: Error + Send + Sync + ' static > ) ,
168
+ #[ error ( "There was an error looking up a commit" ) ]
169
+ pub struct Error {
170
+ #[ from ]
171
+ source : Box < dyn std:: error:: Error + Send + Sync + ' static > ,
142
172
}
143
173
144
174
///
@@ -186,7 +216,7 @@ pub struct Commit<'graph> {
186
216
///
187
217
pub mod commit {
188
218
use super :: Commit ;
189
- use crate :: graph:: Either ;
219
+ use crate :: graph:: { CommitterTimestamp , Either } ;
190
220
191
221
impl < ' graph > Commit < ' graph > {
192
222
/// Return an iterator over the parents of this commit.
@@ -202,13 +232,13 @@ pub mod commit {
202
232
///
203
233
/// This is the single-most important date for determining recency of commits.
204
234
/// Note that this can only fail if the commit is backed by the object database *and* parsing fails.
205
- pub fn committer_timestamp ( & self ) -> Result < u64 , gix_object:: decode:: Error > {
235
+ pub fn committer_timestamp ( & self ) -> Result < CommitterTimestamp , gix_object:: decode:: Error > {
206
236
Ok ( match & self . backing {
207
237
Either :: Left ( buf) => {
208
238
gix_object:: CommitRefIter :: from_bytes ( buf)
209
239
. committer ( ) ?
210
240
. time
211
- . seconds_since_unix_epoch as u64
241
+ . seconds_since_unix_epoch as CommitterTimestamp
212
242
}
213
243
Either :: Right ( ( cache, pos) ) => cache. commit_at ( * pos) . committer_timestamp ( ) ,
214
244
} )
0 commit comments