William Pursell
William Pursell

Reputation: 212248

Why does git-for-each-ref fail to sort tags correctly?

When I have a repository with both lightweight and annotated tags, git-for-each-ref only seems to sort one of the sets. I would like to modify my call to for-each-ref to get output which sorts all the tags and intermixes them in the output.

For example:


bash-3.2$ git tag | 
> xargs -I T git log -n 1 --format='%at T' T |
> sort -rn | 
> awk '{print $2}'
lwt3
at3
lwt2
at2
lwt1
at1
bash-3.2$ git for-each-ref --sort=-authordate refs/tags | awk '{print $3}'
refs/tags/lwt3
refs/tags/lwt2
refs/tags/lwt1
refs/tags/at1
refs/tags/at2
refs/tags/at3
bash-3.2$ git --version
git version 1.6.6.80.g2df32

Using -committerdate or -taggerdate generates similar output, and the tags are never correctly sorted. When using -*authordate, or -*committerdate, the group which is sorted is inverted, while -*taggerdate sorts nothing.

Is there some other option of which I am unaware? And is this correct behavior? I can see why committerdate or taggerdate would only sort the commits or the tags, respectively, but it seems like authordate ought to do what I want.

Upvotes: 6

Views: 3067

Answers (2)

Yann Droneaud
Yann Droneaud

Reputation: 5463

To have annotated tags and lightweight tags sorted altogether, based on the commit date, I'm using:

git for-each-ref --format='%(*committerdate:raw)%(committerdate:raw) %(refname) %(*objectname) %(objectname)' refs/tags | \
  sort -n | awk '{ print $4, $3; }' 

This command will list every tag and the associated commit object id, in chronological order.

Upvotes: 5

Cascabel
Cascabel

Reputation: 496932

Okay, went ahead and verified what I said in my comment:

Only annotated tags create a tab object in the repository, which contains among other things the taggerdate field you're trying to sort on here. Your tags are most likely lightweight tags, and therefore contain no such information.

And for the other half, committerdate and authordate appear to work correctly for me on tags - they're just sorting the commit the tag points to, since there's no information about when the tag itself was created. (And they reverse the sort if and only if you put a - in front)

Upvotes: 4

Related Questions