Skip to content

Commit

Permalink
support for the --max-candidates flag (#298)
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Apr 5, 2022
1 parent 60cb858 commit b9e6754
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 2 deletions.
8 changes: 8 additions & 0 deletions git-repository/src/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand All @@ -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;
Expand Down Expand Up @@ -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()
},
)?;
Expand Down
1 change: 1 addition & 0 deletions git-repository/src/object/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ impl<'repo> Commit<'repo> {
select: Default::default(),
first_parent: false,
id_as_fallback: false,
max_candidates: 10,
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions git-revision/src/describe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ pub struct Options<'name> {
pub name_by_oid: hash_hasher::HashedMap<git_hash::ObjectId, Cow<'name, BStr>>,
/// 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,
Expand All @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions gitoxide-core/src/repository/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub fn describe(
first_parent,
always,
statistics,
max_candidates,
long_format,
}: describe::Options,
) -> Result<()> {
Expand All @@ -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))?;

Expand All @@ -57,5 +59,6 @@ pub mod describe {
pub always: bool,
pub long_format: bool,
pub statistics: bool,
pub max_candidates: usize,
}
}
2 changes: 2 additions & 0 deletions src/plumbing/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ pub fn main() -> Result<()> {
always,
long,
statistics,
max_candidates,
rev_spec,
} => prepare_and_run(
"repository-commit-describe",
Expand All @@ -184,6 +185,7 @@ pub fn main() -> Result<()> {
long_format: long,
first_parent,
statistics,
max_candidates,
always,
},
)
Expand Down
4 changes: 4 additions & 0 deletions src/plumbing/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit b9e6754

Please sign in to comment.