Skip to content

Commit 7c88b62

Browse files
committed
feat: easy::Head::name() to learn about the name of the HEAD ref (#293)
It's mainly for completeness to provide people with with a `FullNameRef` of HEAD.
1 parent d4a8f9f commit 7c88b62

File tree

1 file changed

+39
-15
lines changed

1 file changed

+39
-15
lines changed

git-repository/src/easy/head.rs

+39-15
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ impl Kind {
3737
}
3838

3939
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+
4047
/// Returns the full reference name of this head if it is not detached, or `None` otherwise.
4148
pub fn referent_name(&self) -> Option<FullNameRef<'_>> {
4249
Some(match &self.kind {
@@ -49,14 +56,15 @@ impl<'repo> Head<'repo> {
4956
pub fn is_detached(&self) -> bool {
5057
matches!(self.kind, Kind::Detached { .. })
5158
}
52-
}
5359

54-
impl<'repo> Head<'repo> {
5560
// TODO: tests
5661
/// Returns the id the head points to, which isn't possible on unborn heads.
5762
pub fn id(&self) -> Option<easy::Oid<'repo>> {
5863
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)),
6068
Kind::Detached { peeled, target } => (*peeled)
6169
.unwrap_or_else(|| target.to_owned())
6270
.attach(self.handle)
@@ -66,14 +74,17 @@ impl<'repo> Head<'repo> {
6674
}
6775

6876
/// 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.
6981
pub fn into_referent(self) -> easy::Reference<'repo> {
7082
match self.kind {
7183
Kind::Symbolic(r) => r.attach(self.handle),
7284
_ => panic!("BUG: Expected head to be a born symbolic reference"),
7385
}
7486
}
7587
}
76-
7788
///
7889
pub mod log {
7990
use std::convert::TryFrom;
@@ -150,9 +161,13 @@ pub mod peel {
150161
Some(match &mut self.kind {
151162
Kind::Unborn(_name) => return None,
152163
Kind::Detached {
153-
peeled: Some(peeled), ..
164+
peeled: Some(peeled),
165+
..
154166
} => Ok((*peeled).attach(self.handle)),
155-
Kind::Detached { peeled: None, target } => {
167+
Kind::Detached {
168+
peeled: None,
169+
target,
170+
} => {
156171
match target
157172
.attach(self.handle)
158173
.object()
@@ -185,12 +200,14 @@ pub mod peel {
185200
/// more object to follow, transform the id into a commit if possible and return that.
186201
///
187202
/// 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+
})??;
194211
id.object()
195212
.map_err(|err| peel_to_commit::Error::Peel(Error::FindExistingObject(err)))
196213
.and_then(|object| object.try_into_commit().map_err(Into::into))
@@ -202,15 +219,22 @@ pub mod peel {
202219
Some(match self.kind {
203220
Kind::Unborn(_name) => return None,
204221
Kind::Detached {
205-
peeled: Some(peeled), ..
222+
peeled: Some(peeled),
223+
..
206224
} => Ok(peeled.attach(self.handle)),
207-
Kind::Detached { peeled: None, target } => target
225+
Kind::Detached {
226+
peeled: None,
227+
target,
228+
} => target
208229
.attach(self.handle)
209230
.object()
210231
.map_err(Into::into)
211232
.and_then(|obj| obj.peel_tags_to_end().map_err(Into::into))
212233
.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),
214238
})
215239
}
216240
}

0 commit comments

Comments
 (0)