From b9e67540801f2630be8aa1acbfddfec4202360ac Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Tue, 5 Apr 2022 10:27:03 +0800 Subject: [PATCH] support for the --max-candidates flag (#298) --- git-repository/src/commit.rs | 8 ++++++++ git-repository/src/object/commit.rs | 1 + git-revision/src/describe.rs | 4 ++-- gitoxide-core/src/repository/commit.rs | 3 +++ src/plumbing/main.rs | 2 ++ src/plumbing/options.rs | 4 ++++ 6 files changed, 20 insertions(+), 2 deletions(-) diff --git a/git-repository/src/commit.rs b/git-repository/src/commit.rs index edd7ba2410f..f1049159726 100644 --- a/git-repository/src/commit.rs +++ b/git-repository/src/commit.rs @@ -116,6 +116,7 @@ pub mod describe { pub(crate) select: SelectRef, pub(crate) first_parent: bool, pub(crate) id_as_fallback: bool, + pub(crate) max_candidates: usize, } impl<'repo> Platform<'repo> { @@ -131,6 +132,12 @@ pub mod describe { self } + /// Only consider the given amount of candidates, instead of the default of 10. + pub fn max_candidates(mut self, candidates: usize) -> Self { + self.max_candidates = candidates; + self + } + /// If true, even if no candidate is available a format will always be produced. pub fn id_as_fallback(mut self, use_fallback: bool) -> Self { self.id_as_fallback = use_fallback; @@ -158,6 +165,7 @@ pub mod describe { name_by_oid: self.select.names(self.repo)?, fallback_to_oid: self.id_as_fallback, first_parent: self.first_parent, + max_candidates: self.max_candidates, ..Default::default() }, )?; diff --git a/git-repository/src/object/commit.rs b/git-repository/src/object/commit.rs index 44a5a45c6b6..434cc6e1399 100644 --- a/git-repository/src/object/commit.rs +++ b/git-repository/src/object/commit.rs @@ -134,6 +134,7 @@ impl<'repo> Commit<'repo> { select: Default::default(), first_parent: false, id_as_fallback: false, + max_candidates: 10, } } } diff --git a/git-revision/src/describe.rs b/git-revision/src/describe.rs index f45cd9ad45b..c7739f02b88 100644 --- a/git-revision/src/describe.rs +++ b/git-revision/src/describe.rs @@ -104,7 +104,7 @@ pub struct Options<'name> { pub name_by_oid: hash_hasher::HashedMap>, /// The amount of names we will keep track of. Defaults to the maximum of 32. /// - /// If the number is exceeded, it will be capped at 32. + /// If the number is exceeded, it will be capped at 32 and defaults to 10. pub max_candidates: usize, /// If no candidate for naming, always show the abbreviated hash. Default: false. pub fallback_to_oid: bool, @@ -117,7 +117,7 @@ pub struct Options<'name> { impl<'name> Default for Options<'name> { fn default() -> Self { Options { - max_candidates: 28, // the same number as git uses, otherwise we perform worse by default on big repos + max_candidates: 10, // the same number as git uses, otherwise we perform worse by default on big repos name_by_oid: Default::default(), fallback_to_oid: false, first_parent: false, diff --git a/gitoxide-core/src/repository/commit.rs b/gitoxide-core/src/repository/commit.rs index 0eba3235e2b..f8dc75e181d 100644 --- a/gitoxide-core/src/repository/commit.rs +++ b/gitoxide-core/src/repository/commit.rs @@ -13,6 +13,7 @@ pub fn describe( first_parent, always, statistics, + max_candidates, long_format, }: describe::Options, ) -> Result<()> { @@ -34,6 +35,7 @@ pub fn describe( .names(select_ref) .traverse_first_parent(first_parent) .id_as_fallback(always) + .max_candidates(max_candidates) .try_resolve()? .with_context(|| format!("Did not find a single candidate ref for naming id '{}'", commit.id))?; @@ -57,5 +59,6 @@ pub mod describe { pub always: bool, pub long_format: bool, pub statistics: bool, + pub max_candidates: usize, } } diff --git a/src/plumbing/main.rs b/src/plumbing/main.rs index 8c71cae5398..2447650bfd4 100644 --- a/src/plumbing/main.rs +++ b/src/plumbing/main.rs @@ -165,6 +165,7 @@ pub fn main() -> Result<()> { always, long, statistics, + max_candidates, rev_spec, } => prepare_and_run( "repository-commit-describe", @@ -184,6 +185,7 @@ pub fn main() -> Result<()> { long_format: long, first_parent, statistics, + max_candidates, always, }, ) diff --git a/src/plumbing/options.rs b/src/plumbing/options.rs index 519c30d64ac..70027da4ff6 100644 --- a/src/plumbing/options.rs +++ b/src/plumbing/options.rs @@ -411,6 +411,10 @@ pub mod repo { #[clap(long, short = 'l')] long: bool, + /// Consider only the given `n` candidates. This can take longer, but potentially produces more accurate results. + #[clap(long, short = 'c', default_value = "10")] + max_candidates: usize, + /// Print information on stderr to inform about performance statistics #[clap(long, short = 's')] statistics: bool,