Skip to content

Commit

Permalink
fix: prevent panic in Repository::rev_parse_single() when HEAD wa…
Browse files Browse the repository at this point in the history
…s invalid.

When using a refspec like `HEAD:file`.
  • Loading branch information
Byron committed Aug 11, 2024
1 parent 17bd32a commit e74095e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
15 changes: 10 additions & 5 deletions gix/src/revision/spec/parse/delegate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,16 @@ impl<'repo> Delegate<'repo> {
for (r, obj) in self.refs.iter().zip(self.objs.iter_mut()) {
if let (Some(ref_), obj_opt @ None) = (r, obj) {
if let Some(id) = ref_.target.try_id().map(ToOwned::to_owned).or_else(|| {
ref_.clone()
.attach(repo)
.peel_to_id_in_place()
.ok()
.map(crate::Id::detach)
match ref_.clone().attach(repo).peel_to_id_in_place() {
Err(err) => {
self.err.push(Error::PeelToId {
name: ref_.name.clone(),
source: err,
});
None
}
Ok(id) => Some(id.detach()),
}
}) {
obj_opt.get_or_insert_with(HashSet::default).insert(id);
};
Expand Down
5 changes: 5 additions & 0 deletions gix/src/revision/spec/parse/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ pub struct Options {
#[derive(Debug, thiserror::Error)]
#[allow(missing_docs)]
pub enum Error {
#[error("Could not peel '{}' to obtain its target", name)]
PeelToId {
name: gix_ref::FullName,
source: reference::peel::Error,
},
#[error("The rev-spec is malformed and misses a ref name")]
Malformed,
#[error("Unborn heads do not have a reflog yet")]
Expand Down
6 changes: 3 additions & 3 deletions gix/tests/revision/spec/from_bytes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,11 @@ fn access_blob_through_tree() {
#[test]
fn invalid_head() {
let repo = repo("invalid-head").unwrap();
let err = parse_spec("HEAD:file", &repo).unwrap_err();
assert_eq!(err.to_string(), "Could not peel 'HEAD' to obtain its target");

let err = parse_spec("HEAD", &repo).unwrap_err();
assert_eq!(err.to_string(), "The rev-spec is malformed and misses a ref name");

let err = parse_spec("HEAD:file", &repo).unwrap_err();
assert_eq!(err.to_string(), "TBD");
}

#[test]
Expand Down

0 comments on commit e74095e

Please sign in to comment.