File tree Expand file tree Collapse file tree 2 files changed +33
-1
lines changed Expand file tree Collapse file tree 2 files changed +33
-1
lines changed Original file line number Diff line number Diff line change @@ -62,6 +62,7 @@ impl<'repo> Reference<'repo> {
62
62
}
63
63
}
64
64
65
+ /// Peeling
65
66
impl < ' repo > Reference < ' repo > {
66
67
/// Follow all symbolic targets this reference might point to and peel the underlying object
67
68
/// to the end of the chain, and return it.
@@ -81,6 +82,18 @@ impl<'repo> Reference<'repo> {
81
82
pub fn into_fully_peeled_id ( mut self ) -> Result < Id < ' repo > , peel:: Error > {
82
83
self . peel_to_id_in_place ( )
83
84
}
85
+
86
+ /// Follow this symbolic reference one level and return the ref it refers to.
87
+ ///
88
+ /// Returns `None` if this is not a symbolic reference, hence the leaf of the chain.
89
+ pub fn follow ( & self ) -> Option < Result < Reference < ' repo > , gix_ref:: file:: find:: existing:: Error > > {
90
+ self . inner . follow ( & self . repo . refs ) . map ( |res| {
91
+ res. map ( |r| Reference {
92
+ inner : r,
93
+ repo : self . repo ,
94
+ } )
95
+ } )
96
+ }
84
97
}
85
98
86
99
mod edits;
Original file line number Diff line number Diff line change @@ -23,7 +23,7 @@ mod find {
23
23
use std:: convert:: TryInto ;
24
24
25
25
use gix_ref as refs;
26
- use gix_ref:: FullNameRef ;
26
+ use gix_ref:: { FullName , FullNameRef , Target } ;
27
27
28
28
use crate :: util:: hex_to_id;
29
29
@@ -64,6 +64,25 @@ mod find {
64
64
assert_eq ! ( symbolic_ref. into_fully_peeled_id( ) ?, the_commit, "idempotency" ) ;
65
65
Ok ( ( ) )
66
66
}
67
+
68
+ #[ test]
69
+ fn and_follow ( ) -> crate :: Result {
70
+ let repo = repo ( ) ?;
71
+ let mut symbolic_ref = repo. find_reference ( "multi-link-target1" ) ?;
72
+ let first_hop = Target :: Symbolic ( FullName :: try_from ( "refs/tags/multi-link-target2" ) . expect ( "valid" ) ) ;
73
+ assert_eq ! ( symbolic_ref. target( ) , first_hop. to_ref( ) ) ;
74
+
75
+ let second_hop = Target :: Symbolic ( FullName :: try_from ( "refs/remotes/origin/multi-link-target3" ) . expect ( "valid" ) ) ;
76
+ symbolic_ref = symbolic_ref. follow ( ) . expect ( "another hop" ) ?;
77
+ assert_eq ! ( symbolic_ref. target( ) , second_hop. to_ref( ) ) ;
78
+
79
+ let last_hop = Target :: Peeled ( hex_to_id ( "134385f6d781b7e97062102c6a483440bfda2a03" ) ) ;
80
+ symbolic_ref = symbolic_ref. follow ( ) . expect ( "another hop" ) ?;
81
+ assert_eq ! ( symbolic_ref. target( ) , last_hop. to_ref( ) ) ;
82
+
83
+ assert ! ( symbolic_ref. follow( ) . is_none( ) , "direct references can't be followed" ) ;
84
+ Ok ( ( ) )
85
+ }
67
86
}
68
87
69
88
#[ test]
You can’t perform that action at this time.
0 commit comments