From 5984f0829ef5369e83c28108378fe0065a617b3c Mon Sep 17 00:00:00 2001 From: David Knaack Date: Fri, 19 Aug 2022 08:27:41 +0200 Subject: [PATCH] perf(git_commit): only use exact match for tag by default (#4281) --- .github/config-schema.json | 7 +++++++ docs/config/README.md | 19 ++++++++++--------- src/configs/git_commit.rs | 2 ++ src/modules/git_commit.rs | 10 +++++++--- 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/.github/config-schema.json b/.github/config-schema.json index a2e019a7..5d0a7c96 100644 --- a/.github/config-schema.json +++ b/.github/config-schema.json @@ -525,6 +525,7 @@ "only_detached": true, "style": "green bold", "tag_disabled": true, + "tag_max_candidates": 0, "tag_symbol": " 🏷 " }, "allOf": [ @@ -2799,6 +2800,12 @@ "tag_disabled": { "default": true, "type": "boolean" + }, + "tag_max_candidates": { + "default": 0, + "type": "integer", + "format": "uint", + "minimum": 0.0 } } }, diff --git a/docs/config/README.md b/docs/config/README.md index 0267ee1c..ba6ca574 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -1558,15 +1558,16 @@ The `git_commit` module shows the current commit hash and also the tag (if any) ### Options -| Option | Default | Description | -| -------------------- | ------------------------------ | ------------------------------------------------------- | -| `commit_hash_length` | `7` | The length of the displayed git commit hash. | -| `format` | `"[\\($hash$tag\\)]($style) "` | The format for the module. | -| `style` | `"bold green"` | The style for the module. | -| `only_detached` | `true` | Only show git commit hash when in detached `HEAD` state | -| `tag_disabled` | `true` | Disables showing tag info in `git_commit` module. | -| `tag_symbol` | `" 🏷 "` | Tag symbol prefixing the info shown | -| `disabled` | `false` | Disables the `git_commit` module. | +| Option | Default | Description | +| -------------------- | ------------------------------ | ------------------------------------------------------------------------------------ | +| `commit_hash_length` | `7` | The length of the displayed git commit hash. | +| `format` | `"[\\($hash$tag\\)]($style) "` | The format for the module. | +| `style` | `"bold green"` | The style for the module. | +| `only_detached` | `true` | Only show git commit hash when in detached `HEAD` state | +| `tag_disabled` | `true` | Disables showing tag info in `git_commit` module. | +| `tag_max_candidates` | `0` | How many commits to consider for tag display. The default only allows exact matches. | +| `tag_symbol` | `" 🏷 "` | Tag symbol prefixing the info shown | +| `disabled` | `false` | Disables the `git_commit` module. | ### Variables diff --git a/src/configs/git_commit.rs b/src/configs/git_commit.rs index 12715cb5..6b0d3140 100644 --- a/src/configs/git_commit.rs +++ b/src/configs/git_commit.rs @@ -11,6 +11,7 @@ pub struct GitCommitConfig<'a> { pub disabled: bool, pub tag_symbol: &'a str, pub tag_disabled: bool, + pub tag_max_candidates: usize, } impl<'a> Default for GitCommitConfig<'a> { @@ -24,6 +25,7 @@ impl<'a> Default for GitCommitConfig<'a> { disabled: false, tag_symbol: " 🏷 ", tag_disabled: true, + tag_max_candidates: 0, } } } diff --git a/src/modules/git_commit.rs b/src/modules/git_commit.rs index 88ab069a..60f8793e 100644 --- a/src/modules/git_commit.rs +++ b/src/modules/git_commit.rs @@ -32,7 +32,7 @@ pub fn module<'a>(context: &'a Context) -> Option> { "tag" => Some(Ok(format!( "{}{}", config.tag_symbol, - git_tag(context.get_repo().ok()?)? + git_tag(context.get_repo().ok()?, &config)? ))), _ => None, }) @@ -50,13 +50,17 @@ pub fn module<'a>(context: &'a Context) -> Option> { Some(module) } -fn git_tag(repo: &Repo) -> Option { +fn git_tag(repo: &Repo, config: &GitCommitConfig) -> Option { // allow environment variables like GITOXIDE_OBJECT_CACHE_MEMORY and GITOXIDE_DISABLE_PACK_CACHE to speed up operation for some repos let mut git_repo = repo.open().apply_environment(); git_repo.object_cache_size_if_unset(4 * 1024 * 1024); let head_commit = git_repo.head_commit().ok()?; - let describe_platform = head_commit.describe().names(AnnotatedTags); + let describe_platform = head_commit + .describe() + .names(AnnotatedTags) + .max_candidates(config.tag_max_candidates) + .traverse_first_parent(true); let formatter = describe_platform.try_format().ok()??; Some(formatter.name?.to_string())