From 80aea880267ae30cdf461692bbe4ced32095450c Mon Sep 17 00:00:00 2001 From: Kamil Pabin Date: Wed, 12 Oct 2022 20:41:00 +0200 Subject: [PATCH] Add support for git describe --abbrev --- docs/modules/ROOT/pages/grgit-describe.adoc | 10 +++++++++- .../org/ajoberstar/grgit/operation/DescribeOp.groovy | 8 ++++++++ .../ajoberstar/grgit/operation/DescribeOpSpec.groovy | 10 ++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/docs/modules/ROOT/pages/grgit-describe.adoc b/docs/modules/ROOT/pages/grgit-describe.adoc index a6debf7c..e59fc256 100644 --- a/docs/modules/ROOT/pages/grgit-describe.adoc +++ b/docs/modules/ROOT/pages/grgit-describe.adoc @@ -13,7 +13,7 @@ grgit.describe() [source, groovy] ---- -grgit.describe(commit: , longDescr: , tags: , match: [], always: ) +grgit.describe(commit: , longDescr: , tags: , match: [], always: , abbrev ) ---- [source, groovy] @@ -24,6 +24,7 @@ grgit.describe { tags = match = [] always = + abbrev = } ---- @@ -40,6 +41,7 @@ always:: (`boolean`, default `false`) When `true`, always describe a commit in s longDescr:: (`boolean`, default `false`) Always output the long format (the tag, the number of commits and the abbreviated commit name) even when it matches a tag. This is useful when you want to see parts of the commit object name in "describe" output, even when the commit in question happens to be a tagged version. Instead of just emitting the tag name, it will describe such a commit as v1.2-0-gdeadbee (0th commit since tag v1.2 that points at object deadbee…​.). tags:: (`boolean`, default `false`) Instead of using only the annotated tags, use any tag found in `refs/tags` namespace. This option enables matching a lightweight (non-annotated) tag. match:: (`List`, default `[]`) Only consider tags matching the given glob(7) pattern, excluding the "refs/tags/" prefix. This can be used to avoid leaking private tags from the repository. If multiple patterns are given they will be accumulated, and tags matching any of the patterns will be considered. +abbrev:: (`Integer`, default `null`) Instead of using the default number of hexadecimal digits (which will vary according to the number of objects in the repository with a default of 7) of the abbreviated object name, use digits, or as many digits as needed to form a unique object name. An of 0 will suppress long format, only showing the closest tag. == Examples @@ -64,6 +66,12 @@ Always output the long format (the tag, the number of commits and the abbreviate grgit.describe(longDescr: true) == '2.0.0-rc.1-7-g91fda36' ---- +Abbreviate object name to at least 2 digits, or as many digits as needed to form a unique object name. + +[source, groovy] +---- +grgit.describe(abbrev: 2) == '2.0.0-rc.1-7-g91fd' +---- == See Also diff --git a/grgit-core/src/main/groovy/org/ajoberstar/grgit/operation/DescribeOp.groovy b/grgit-core/src/main/groovy/org/ajoberstar/grgit/operation/DescribeOp.groovy index d7a811b3..7a83a4a9 100644 --- a/grgit-core/src/main/groovy/org/ajoberstar/grgit/operation/DescribeOp.groovy +++ b/grgit-core/src/main/groovy/org/ajoberstar/grgit/operation/DescribeOp.groovy @@ -46,6 +46,11 @@ class DescribeOp implements Callable { */ List match = [] + /** + * Abbreviate resulting object name to use at least n hexadecimal digits + */ + Integer abbrev + String call(){ DescribeCommand cmd = repo.jgit.describe() if (commit) { @@ -57,6 +62,9 @@ class DescribeOp implements Callable { if (match) { cmd.setMatch(match as String[]) } + if(abbrev != null) { + cmd.setAbbrev(abbrev) + } return cmd.call() } } diff --git a/grgit-core/src/test/groovy/org/ajoberstar/grgit/operation/DescribeOpSpec.groovy b/grgit-core/src/test/groovy/org/ajoberstar/grgit/operation/DescribeOpSpec.groovy index e152a2e5..aad0e227 100644 --- a/grgit-core/src/test/groovy/org/ajoberstar/grgit/operation/DescribeOpSpec.groovy +++ b/grgit-core/src/test/groovy/org/ajoberstar/grgit/operation/DescribeOpSpec.groovy @@ -63,4 +63,14 @@ class DescribeOpSpec extends SimpleGitOpSpec { expect: grgit.describe(match: ['second*']).startsWith('second-2-') } + + def 'with abbrev'() { + expect: + grgit.describe(abbrev: abbrev) ==~ expectedPattern + + where: + abbrev | expectedPattern + 0 | /^another$/ + 30 | /^another-1-g.{30}$/ + } }