@@ -37,6 +37,13 @@ impl Kind {
37
37
}
38
38
39
39
impl < ' repo > Head < ' repo > {
40
+ /// Returns the name of this references, always `HEAD`.
41
+ pub fn name ( & self ) -> FullNameRef < ' static > {
42
+ // TODO: use a statically checked version of this when available.
43
+ use std:: convert:: TryFrom ;
44
+ FullNameRef :: try_from ( "HEAD" ) . expect ( "HEAD is valid" )
45
+ }
46
+
40
47
/// Returns the full reference name of this head if it is not detached, or `None` otherwise.
41
48
pub fn referent_name ( & self ) -> Option < FullNameRef < ' _ > > {
42
49
Some ( match & self . kind {
@@ -49,14 +56,15 @@ impl<'repo> Head<'repo> {
49
56
pub fn is_detached ( & self ) -> bool {
50
57
matches ! ( self . kind, Kind :: Detached { .. } )
51
58
}
52
- }
53
59
54
- impl < ' repo > Head < ' repo > {
55
60
// TODO: tests
56
61
/// Returns the id the head points to, which isn't possible on unborn heads.
57
62
pub fn id ( & self ) -> Option < easy:: Oid < ' repo > > {
58
63
match & self . kind {
59
- Kind :: Symbolic ( r) => r. target . as_id ( ) . map ( |oid| oid. to_owned ( ) . attach ( self . handle ) ) ,
64
+ Kind :: Symbolic ( r) => r
65
+ . target
66
+ . as_id ( )
67
+ . map ( |oid| oid. to_owned ( ) . attach ( self . handle ) ) ,
60
68
Kind :: Detached { peeled, target } => ( * peeled)
61
69
. unwrap_or_else ( || target. to_owned ( ) )
62
70
. attach ( self . handle )
@@ -66,14 +74,17 @@ impl<'repo> Head<'repo> {
66
74
}
67
75
68
76
/// Force transforming this instance into the symbolic reference that it points to, or panic if it is unborn or detached.
77
+ ///
78
+ /// # Panics
79
+ ///
80
+ /// If this isn't actually a head pointing to a symbolic reference.
69
81
pub fn into_referent ( self ) -> easy:: Reference < ' repo > {
70
82
match self . kind {
71
83
Kind :: Symbolic ( r) => r. attach ( self . handle ) ,
72
84
_ => panic ! ( "BUG: Expected head to be a born symbolic reference" ) ,
73
85
}
74
86
}
75
87
}
76
-
77
88
///
78
89
pub mod log {
79
90
use std:: convert:: TryFrom ;
@@ -150,9 +161,13 @@ pub mod peel {
150
161
Some ( match & mut self . kind {
151
162
Kind :: Unborn ( _name) => return None ,
152
163
Kind :: Detached {
153
- peeled : Some ( peeled) , ..
164
+ peeled : Some ( peeled) ,
165
+ ..
154
166
} => Ok ( ( * peeled) . attach ( self . handle ) ) ,
155
- Kind :: Detached { peeled : None , target } => {
167
+ Kind :: Detached {
168
+ peeled : None ,
169
+ target,
170
+ } => {
156
171
match target
157
172
. attach ( self . handle )
158
173
. object ( )
@@ -185,12 +200,14 @@ pub mod peel {
185
200
/// more object to follow, transform the id into a commit if possible and return that.
186
201
///
187
202
/// Returns an error if the head is unborn or if it doesn't point to a commit.
188
- pub fn peel_to_commit_in_place ( & mut self ) -> Result < easy:: Commit < ' repo > , peel_to_commit:: Error > {
189
- let id = self
190
- . peel_to_id_in_place ( )
191
- . ok_or_else ( || peel_to_commit:: Error :: Unborn {
192
- name : self . referent_name ( ) . expect ( "unborn" ) . to_owned ( ) ,
193
- } ) ??;
203
+ pub fn peel_to_commit_in_place (
204
+ & mut self ,
205
+ ) -> Result < easy:: Commit < ' repo > , peel_to_commit:: Error > {
206
+ let id =
207
+ self . peel_to_id_in_place ( )
208
+ . ok_or_else ( || peel_to_commit:: Error :: Unborn {
209
+ name : self . referent_name ( ) . expect ( "unborn" ) . to_owned ( ) ,
210
+ } ) ??;
194
211
id. object ( )
195
212
. map_err ( |err| peel_to_commit:: Error :: Peel ( Error :: FindExistingObject ( err) ) )
196
213
. and_then ( |object| object. try_into_commit ( ) . map_err ( Into :: into) )
@@ -202,15 +219,22 @@ pub mod peel {
202
219
Some ( match self . kind {
203
220
Kind :: Unborn ( _name) => return None ,
204
221
Kind :: Detached {
205
- peeled : Some ( peeled) , ..
222
+ peeled : Some ( peeled) ,
223
+ ..
206
224
} => Ok ( peeled. attach ( self . handle ) ) ,
207
- Kind :: Detached { peeled : None , target } => target
225
+ Kind :: Detached {
226
+ peeled : None ,
227
+ target,
228
+ } => target
208
229
. attach ( self . handle )
209
230
. object ( )
210
231
. map_err ( Into :: into)
211
232
. and_then ( |obj| obj. peel_tags_to_end ( ) . map_err ( Into :: into) )
212
233
. map ( |obj| obj. id . attach ( self . handle ) ) ,
213
- Kind :: Symbolic ( r) => r. attach ( self . handle ) . peel_to_id_in_place ( ) . map_err ( Into :: into) ,
234
+ Kind :: Symbolic ( r) => r
235
+ . attach ( self . handle )
236
+ . peel_to_id_in_place ( )
237
+ . map_err ( Into :: into) ,
214
238
} )
215
239
}
216
240
}
0 commit comments