Skip to content

Commit e74095e

Browse files
committedAug 11, 2024··
fix: prevent panic in Repository::rev_parse_single() when HEAD was invalid.
When using a refspec like `HEAD:file`.
1 parent 17bd32a commit e74095e

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed
 

‎gix/src/revision/spec/parse/delegate/mod.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,16 @@ impl<'repo> Delegate<'repo> {
203203
for (r, obj) in self.refs.iter().zip(self.objs.iter_mut()) {
204204
if let (Some(ref_), obj_opt @ None) = (r, obj) {
205205
if let Some(id) = ref_.target.try_id().map(ToOwned::to_owned).or_else(|| {
206-
ref_.clone()
207-
.attach(repo)
208-
.peel_to_id_in_place()
209-
.ok()
210-
.map(crate::Id::detach)
206+
match ref_.clone().attach(repo).peel_to_id_in_place() {
207+
Err(err) => {
208+
self.err.push(Error::PeelToId {
209+
name: ref_.name.clone(),
210+
source: err,
211+
});
212+
None
213+
}
214+
Ok(id) => Some(id.detach()),
215+
}
211216
}) {
212217
obj_opt.get_or_insert_with(HashSet::default).insert(id);
213218
};

‎gix/src/revision/spec/parse/types.rs

+5
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ pub struct Options {
5555
#[derive(Debug, thiserror::Error)]
5656
#[allow(missing_docs)]
5757
pub enum Error {
58+
#[error("Could not peel '{}' to obtain its target", name)]
59+
PeelToId {
60+
name: gix_ref::FullName,
61+
source: reference::peel::Error,
62+
},
5863
#[error("The rev-spec is malformed and misses a ref name")]
5964
Malformed,
6065
#[error("Unborn heads do not have a reflog yet")]

‎gix/tests/revision/spec/from_bytes/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,11 @@ fn access_blob_through_tree() {
147147
#[test]
148148
fn invalid_head() {
149149
let repo = repo("invalid-head").unwrap();
150+
let err = parse_spec("HEAD:file", &repo).unwrap_err();
151+
assert_eq!(err.to_string(), "Could not peel 'HEAD' to obtain its target");
152+
150153
let err = parse_spec("HEAD", &repo).unwrap_err();
151154
assert_eq!(err.to_string(), "The rev-spec is malformed and misses a ref name");
152-
153-
let err = parse_spec("HEAD:file", &repo).unwrap_err();
154-
assert_eq!(err.to_string(), "TBD");
155155
}
156156

157157
#[test]

0 commit comments

Comments
 (0)
Please sign in to comment.