Jean-Marc Volle
Jean-Marc Volle

Reputation: 3323

list all tags by creation date on a given commit

I would like to retrieve the last (chronologicaly) tag put on a given commit. So far I did not succeed to do it, it seems all tags put on the same commit share the commit creation date.

Sample of what I did using git tag --points-at my_commit --sort=-creatordate

Here is a pseudo sample of what I try to achieve in eg bash:

commands (for testing purpose):

sha=`git rev-parse --verify HEAD`
git tag test_a
git tag test_c
git tag test_b
git tag --points-at $sha --sort=-creatordate

result (seemingly alphabetical order):

test_a
test_b
test_c

What I expect: (creation order)

test_a
test_c
test_b

I would like to find a way to retrieve that test_b is the last tag put on the current commit

Solution

solution provided below in the answers and added later to ease reading: Using annotated tags instead of tags allows to retrieve their own "creation" date. So this test case works:

git tag -a test_a -m " a test tag"
git tag -a test_c -m " a test tag"
git tag -a test_b -m " a test tag"
git tag --points-at $sha --sort=-creatordate

results:

test_a
test_c
test_b

Upvotes: 0

Views: 269

Answers (1)

torek
torek

Reputation: 487993

Ture Pålsson's comment has one of the keys here: only annotated tags contain their own date-and-time-stamps. This is almost certainly the problem (it is with your tests, at least).

Each commit has two date-and-time-stamps in it, one being the author date and the other being the committer date. Often these two are identical. They differ when the commit has been rebased or otherwise copied: now the author date is the original date, and the committer date contains the date at which this particular commit was created. (The author and committer name and email address can differ as well; for instance, this is normal with emailed patches, where the author is the person who sent the email, and the committer is the person who used that email message to create the commit.)

An annotated tag has similar information, although there is only the one tagger rather than a separate author and committer.

The git for-each-ref command—and in sufficiently modern versions of Git, git tag as well—has --sort options, as you are using:

... --sort=-creatordate

As the documentation says:

For commit and tag objects, the special creatordate and creator fields will correspond to the appropriate date or name-email-date tuple from the committer or tagger fields depending on the object type. These are intended for working on a mix of annotated and lightweight tags.

So it looks like you have some lightweight tags, that don't have their own tag-creation dates, and -creatordate is using the committer date fields from the tagged commits. The only way you can get what you'd like is to make sure that the tags are annotated.

Upvotes: 1

Related Questions