Skip to content

Instantly share code, notes, and snippets.

@danielsanfr
Last active June 6, 2022 03:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielsanfr/7423af1b7267d0af92d4bf504624cf2c to your computer and use it in GitHub Desktop.
Save danielsanfr/7423af1b7267d0af92d4bf504624cf2c to your computer and use it in GitHub Desktop.
Use Grgit to generate a "git describe" that considers the tags of all branches.
/**
* Copyright (c) 2020-present Daniel San
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This script use the library https://github.com/ajoberstar/grgit.
*/
static final String buildDescribeString(
final String tag,
final int commitsCount,
final /* org.ajoberstar.grgit.Commit */ headCommit
) {
return "$tag-$commitsCount-g${headCommit.id.substring(0, 7)}"
}
final String unreleasedDescribe() {
final def headCommit = grgit.head()
final def commitsCount = grgit.log().size()
return buildDescribeString("unreleased", commitsCount, headCommit)
}
final String releasedDescribe(final /*Array<org.ajoberstar.grgit.Tag>*/ tags) {
tags.sort { it.dateTime }
final def lastTag = tags.last()
final def headCommit = grgit.head()
if (headCommit == lastTag.commit)
return lastTag.getName()
final def tagName = lastTag.getName()
final def commitCount = grgit.log { range(tagName, 'HEAD') }.size()
return buildDescribeString(tagName, commitCount, headCommit)
}
ext.customGitDescribe = {
if (grgit == null)
return "0.0.1-0-without_git_repo"
final def tags = grgit.tag.list()
if (tags.isEmpty())
return unreleasedDescribe()
return releasedDescribe(tags)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment