Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get latest Git tag from a repository using Phing

Tags:

git

build

phing

I'm new to phing and building a script to automate some build tasks.

Is there a way to retrieve just the most recently added tag to a git repo? I can pull up a list of all my tags but cannot seem to filter it down to the latest one.

Here's the relevant code that fetches my git tags:

    <gittag 
        repository="${repo.dir.resolved}" 
        list="true" 
        outputProperty="versionTag" 
        pattern="v*" />

The output of the above results in a list of tags (prefixed by "v"):

[gittag] git-tag output: v1.0.0
v1.0.1
v1.0.2

Any ideas on how I can get this down to just the v1.0.2?

like image 821
mozami Avatar asked Nov 13 '11 17:11

mozami


People also ask

How do I get latest GitHub tags?

Returns the latest tag in the current branch. To get the latest annotated tag which targets only the current commit in the current branch, use git describe --exact-match --abbrev=0 .

How do I get most recent tags?

In order to find the latest Git tag available on your repository, you have to use the “git describe” command with the “–tags” option. This way, you will be presented with the tag that is associated with the latest commit of your current checked out branch.

What is git describe?

The `git describe` command finds the most recent tag that is reachable from a commit. If the tag is on the current commit, then only the tag is shown. Otherwise, it shows the tag name appended with the number of additional commits and the abbreviated SHA of the current commit.

How do you list a tag?

Listing the available tags in Git is straightforward. Just type git tag (with optional -l or --list ). You can also search for tags that match a particular pattern. The command finds the most recent tag that is reachable from a commit.


1 Answers

Managed to get this done as follows:

    <exec 
        outputProperty="latestVersion" 
        command="git describe --tags `git rev-list --tags --max-count=1`" 
        dir="${repo.dir.resolved}"/>

It does work, though I'm open to suggestions if this can be improved!

like image 68
mozami Avatar answered Oct 11 '22 04:10

mozami