From df62f5081291f65f994b2aa66f0599f47eea8d4d Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Fri, 19 Aug 2022 15:14:37 +0800 Subject: [PATCH] feat: `describe()` aborts search early if there is no input name in the name map. --- git-revision/src/describe.rs | 22 +++++++++++----------- git-revision/tests/describe/mod.rs | 9 +++++++-- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/git-revision/src/describe.rs b/git-revision/src/describe.rs index 7f117e43504..2c34c70eb91 100644 --- a/git-revision/src/describe.rs +++ b/git-revision/src/describe.rs @@ -177,18 +177,8 @@ pub(crate) mod function { Find: for<'b> FnMut(&oid, &'b mut Vec) -> Result>, 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(), @@ -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(); diff --git a/git-revision/tests/describe/mod.rs b/git-revision/tests/describe/mod.rs index 9b4b2eb41c4..66f8aa4a6e6 100644 --- a/git-revision/tests/describe/mod.rs +++ b/git-revision/tests/describe/mod.rs @@ -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( @@ -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(()) } @@ -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" @@ -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,