File tree 4 files changed +19
-11
lines changed
4 files changed +19
-11
lines changed Original file line number Diff line number Diff line change @@ -227,6 +227,22 @@ impl crate::Repository {
227
227
Ok ( self . head_commit ( ) ?. tree_id ( ) ?)
228
228
}
229
229
230
+ /// Like [`Self::head_tree_id()`], but will return an empty tree hash if the repository HEAD is unborn.
231
+ pub fn head_tree_id_or_empty ( & self ) -> Result < crate :: Id < ' _ > , reference:: head_tree_id:: Error > {
232
+ self . head_tree_id ( ) . or_else ( |err| {
233
+ if let reference:: head_tree_id:: Error :: HeadCommit ( reference:: head_commit:: Error :: PeelToCommit (
234
+ crate :: head:: peel:: to_commit:: Error :: PeelToObject ( crate :: head:: peel:: to_object:: Error :: Unborn {
235
+ ..
236
+ } ) ,
237
+ ) ) = err
238
+ {
239
+ Ok ( self . empty_tree ( ) . id ( ) )
240
+ } else {
241
+ Err ( err)
242
+ }
243
+ } )
244
+ }
245
+
230
246
/// Return the tree object the `HEAD^{tree}` reference currently points to after peeling it fully,
231
247
/// following symbolic references and tags until a tree is found.
232
248
///
Original file line number Diff line number Diff line change 45
45
46
46
let obtain_tree_id = || -> Result < Option < gix_hash:: ObjectId > , crate :: status:: into_iter:: Error > {
47
47
Ok ( match self . head_tree {
48
- Some ( None ) => match self . repo . head_tree_id ( ) {
49
- Ok ( id) => Some ( id. into ( ) ) ,
50
- Err ( crate :: reference:: head_tree_id:: Error :: HeadCommit (
51
- crate :: reference:: head_commit:: Error :: PeelToCommit (
52
- crate :: head:: peel:: to_commit:: Error :: PeelToObject (
53
- crate :: head:: peel:: to_object:: Error :: Unborn { .. } ,
54
- ) ,
55
- ) ,
56
- ) ) => Some ( gix_hash:: ObjectId :: empty_tree ( self . repo . object_hash ( ) ) ) ,
57
- Err ( err) => return Err ( err. into ( ) ) ,
58
- } ,
48
+ Some ( None ) => Some ( self . repo . head_tree_id_or_empty ( ) ?. into ( ) ) ,
59
49
Some ( Some ( tree_id) ) => Some ( tree_id) ,
60
50
None => None ,
61
51
} )
Original file line number Diff line number Diff line change @@ -667,6 +667,7 @@ mod blocking_io {
667
667
assert ! ( !repo. index_path( ) . is_file( ) , "newly initialized repos have no index" ) ;
668
668
let head = repo. head ( ) ?;
669
669
assert ! ( head. is_unborn( ) ) ;
670
+ assert_eq ! ( repo. head_tree_id_or_empty( ) ?, repo. empty_tree( ) . id( ) ) ;
670
671
671
672
assert ! (
672
673
head. log_iter( ) . all( ) ?. is_none( ) ,
Original file line number Diff line number Diff line change @@ -12,6 +12,7 @@ mod peel {
12
12
let commit = repo. head_commit ( ) ?;
13
13
assert_eq ! ( commit. id, expected_commit) ;
14
14
assert_eq ! ( repo. head_tree_id( ) ?, commit. tree_id( ) ?) ;
15
+ assert_eq ! ( repo. head_tree_id_or_empty( ) ?, commit. tree_id( ) ?) ;
15
16
assert_eq ! ( repo. head( ) ?. try_into_peeled_id( ) ?. expect( "born" ) , expected_commit) ;
16
17
assert_eq ! ( repo. head( ) ?. peel_to_object_in_place( ) ?. id, expected_commit) ;
17
18
assert_eq ! ( repo. head( ) ?. try_peel_to_id_in_place( ) ?. expect( "born" ) , expected_commit) ;
You can’t perform that action at this time.
0 commit comments