Skip to content

Commit 0057804

Browse files
committed
feat: Repository::head_commit() (#364)
A shortcut to get to the commit much faster.
1 parent f0d8a49 commit 0057804

File tree

4 files changed

+27
-10
lines changed

4 files changed

+27
-10
lines changed

git-repository/src/reference/errors.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,19 @@ pub mod head_id {
4343
}
4444
}
4545

46+
///
47+
pub mod head_commit {
48+
/// The error returned by [Repository::head_commit(…)][crate::Repository::head_commit()].
49+
#[derive(Debug, thiserror::Error)]
50+
#[allow(missing_docs)]
51+
pub enum Error {
52+
#[error(transparent)]
53+
Head(#[from] crate::reference::find::existing::Error),
54+
#[error(transparent)]
55+
PeelToCommit(#[from] crate::head::peel::to_commit::Error),
56+
}
57+
}
58+
4659
///
4760
pub mod find {
4861
///

git-repository/src/reference/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{Id, Reference};
88
pub mod iter;
99

1010
mod errors;
11-
pub use errors::{edit, find, head_id, peel};
11+
pub use errors::{edit, find, head_commit, head_id, peel};
1212

1313
use crate::ext::ObjectIdExt;
1414

git-repository/src/repository/reference.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ impl crate::Repository {
184184
.attach(self))
185185
}
186186

187-
/// Resolve the head, follow and peel its target and obtain its object id.
187+
/// Resolve the `HEAD` reference, follow and peel its target and obtain its object id.
188188
///
189189
/// Note that this may fail for various reasons, most notably because the repository
190190
/// is freshly initialized and doesn't have any commits yet.
@@ -200,6 +200,15 @@ impl crate::Repository {
200200
.map_err(Into::into)
201201
}
202202

203+
/// Return the commit object the `HEAD` reference currently points to after peeling it fully.
204+
///
205+
/// Note that this may fail for various reasons, most notably because the repository
206+
/// is freshly initialized and doesn't have any commits yet. It could also fail if the
207+
/// head does not point to a commit.
208+
pub fn head_commit(&self) -> Result<crate::Commit<'_>, crate::reference::head_commit::Error> {
209+
Ok(self.head()?.peel_to_commit_in_place()?)
210+
}
211+
203212
/// Find the reference with the given partial or full `name`, like `main`, `HEAD`, `heads/branch` or `origin/other`,
204213
/// or return an error if it wasn't found.
205214
///

git-repository/tests/easy/object.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
mod commit {
22
use std::cmp::Ordering;
33

4-
use git_repository::{Commit, Repository};
54
use git_testtools::hex_to_id;
65

76
use crate::basic_repo;
87

98
#[test]
109
fn short_id() -> crate::Result {
1110
let handle = basic_repo()?;
12-
let commit = head_commit(&handle);
11+
let commit = handle.head_commit()?;
1312
assert_eq!(commit.short_id()?.cmp_oid(&commit.id), Ordering::Equal);
1413
Ok(())
1514
}
1615

1716
#[test]
1817
fn tree() -> crate::Result {
1918
let handle = basic_repo()?;
20-
let commit = head_commit(&handle);
19+
let commit = handle.head_commit()?;
2120

2221
assert_eq!(commit.tree()?.id, commit.tree_id().expect("id present"));
2322
assert_eq!(
@@ -30,16 +29,12 @@ mod commit {
3029
#[test]
3130
fn decode() -> crate::Result {
3231
let handle = basic_repo()?;
33-
let commit = head_commit(&handle);
32+
let commit = handle.head_commit()?;
3433
assert_eq!(commit.decode()?.message, commit.message_raw()?);
3534
assert_eq!(commit.decode()?.message(), commit.message()?);
3635
assert_eq!(commit.decode()?.message, "c2\n");
3736
Ok(())
3837
}
39-
40-
fn head_commit(repo: &Repository) -> Commit<'_> {
41-
repo.head().unwrap().peel_to_commit_in_place().unwrap()
42-
}
4338
}
4439

4540
#[test]

0 commit comments

Comments
 (0)