File tree Expand file tree Collapse file tree 4 files changed +19
-11
lines changed Expand file tree Collapse file tree 4 files changed +19
-11
lines changed Original file line number Diff line number Diff line change @@ -227,6 +227,22 @@ impl crate::Repository {
227227 Ok ( self . head_commit ( ) ?. tree_id ( ) ?)
228228 }
229229
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+
230246 /// Return the tree object the `HEAD^{tree}` reference currently points to after peeling it fully,
231247 /// following symbolic references and tags until a tree is found.
232248 ///
Original file line number Diff line number Diff line change 4545
4646 let obtain_tree_id = || -> Result < Option < gix_hash:: ObjectId > , crate :: status:: into_iter:: Error > {
4747 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 ( ) ) ,
5949 Some ( Some ( tree_id) ) => Some ( tree_id) ,
6050 None => None ,
6151 } )
Original file line number Diff line number Diff line change @@ -667,6 +667,7 @@ mod blocking_io {
667667 assert ! ( !repo. index_path( ) . is_file( ) , "newly initialized repos have no index" ) ;
668668 let head = repo. head ( ) ?;
669669 assert ! ( head. is_unborn( ) ) ;
670+ assert_eq ! ( repo. head_tree_id_or_empty( ) ?, repo. empty_tree( ) . id( ) ) ;
670671
671672 assert ! (
672673 head. log_iter( ) . all( ) ?. is_none( ) ,
Original file line number Diff line number Diff line change @@ -12,6 +12,7 @@ mod peel {
1212 let commit = repo. head_commit ( ) ?;
1313 assert_eq ! ( commit. id, expected_commit) ;
1414 assert_eq ! ( repo. head_tree_id( ) ?, commit. tree_id( ) ?) ;
15+ assert_eq ! ( repo. head_tree_id_or_empty( ) ?, commit. tree_id( ) ?) ;
1516 assert_eq ! ( repo. head( ) ?. try_into_peeled_id( ) ?. expect( "born" ) , expected_commit) ;
1617 assert_eq ! ( repo. head( ) ?. peel_to_object_in_place( ) ?. id, expected_commit) ;
1718 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