Skip to content

Commit

Permalink
feat: describe() aborts search early if there is no input name in t…
Browse files Browse the repository at this point in the history
…he name map.
  • Loading branch information
Byron committed Aug 19, 2022
1 parent 2f9dc84 commit df62f50
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
22 changes: 11 additions & 11 deletions git-revision/src/describe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,18 +177,8 @@ pub(crate) mod function {
Find: for<'b> FnMut(&oid, &'b mut Vec<u8>) -> Result<Option<CommitRefIter<'b>>, E>,
E: std::error::Error + Send + Sync + 'static,
{
if let Some(name) = name_by_oid.get(commit) {
return Ok(Some(Outcome {
name: name.clone().into(),
id: commit.to_owned(),
depth: 0,
name_by_oid,
commits_seen: 0,
}));
}

max_candidates = max_candidates.min(MAX_CANDIDATES);
if max_candidates == 0 {
if max_candidates == 0 || name_by_oid.is_empty() {
return if fallback_to_oid {
Ok(Some(Outcome {
id: commit.to_owned(),
Expand All @@ -202,6 +192,16 @@ pub(crate) mod function {
};
}

if let Some(name) = name_by_oid.get(commit) {
return Ok(Some(Outcome {
name: name.clone().into(),
id: commit.to_owned(),
depth: 0,
name_by_oid,
commits_seen: 0,
}));
}

let mut buf = Vec::new();
let mut parent_buf = Vec::new();

Expand Down
9 changes: 7 additions & 2 deletions git-revision/tests/describe/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn option_none_if_no_tag_found() -> crate::Result {
}

#[test]
fn fallback_if_configured_in_options_but_no_candidate() -> crate::Result {
fn fallback_if_configured_in_options_but_no_candidate_or_names() -> crate::Result {
let repo = repo();
let commit = repo.head_commit()?;
let res = git_revision::describe(
Expand All @@ -38,7 +38,10 @@ fn fallback_if_configured_in_options_but_no_candidate() -> crate::Result {
.expect("fallback activated");
assert!(res.name.is_none(), "no name can be found");
assert_eq!(res.depth, 0, "just a default, not relevant as there is no name");
assert_eq!(res.commits_seen, 8, "a traversal is performed");
assert_eq!(
res.commits_seen, 0,
"a traversal is isn't performed as name map is empty, and that's the whole point"
);
assert_eq!(res.into_format(7).to_string(), "01ec18a");
Ok(())
}
Expand Down Expand Up @@ -91,6 +94,7 @@ fn not_enough_candidates() -> crate::Result {

assert_eq!(res.name, Some(name), "it finds the youngest/most-recent name");
assert_eq!(res.id, commit.id);
assert_eq!(res.commits_seen, 6, "it has to traverse commits");
assert_eq!(
res.depth, 3,
"it calculates the final number of commits even though it aborted early"
Expand Down Expand Up @@ -146,6 +150,7 @@ fn typical_usecases() {
);
assert_eq!(res.id, commit.id);
assert_eq!(res.depth, 3);
assert_eq!(res.commits_seen, 6);

let res = git_revision::describe(
&commit.id,
Expand Down

0 comments on commit df62f50

Please sign in to comment.