问题
I have to get the latest git tag from the remote git repository. I have used following command for finding the latest tag
git ls-remote --tags xxxxx@xxxx.xxxx.net:xxxx.git |grep "\."|grep -v -|grep -v {| sort -n -t. -k3 -k4
This gives me following output
c8be4313ae8261214acb6d3d41f9ece8d47a4ad5 refs/tags/v0.2.1
9e776cff51a8bb15f0539b852a819723d1e37c69 refs/tags/v0.2.2
ee1f173f4e7da0996af9f7c91e0952bec8c2358b refs/tags/v0.1.3
5d6777bf2b2e5bae41ae9ab966320c691c1f2ee2 refs/tags/v0.1.4
6d3040673330ed763bc0c1a6e6cd5dbc82392d4f refs/tags/v0.1.5
4afd29dc48805053be911a85c6da6b195e96e786 refs/tags/v0.1.6
8d5cc76d50b153f836561bb933b1f5ad488748d1 refs/tags/v0.1.7
1c0cdebaed828aaef2897c9240b4440898f70766 refs/tags/v0.1.8
683de590ba8d633c801d2628f4d9de58f9de371a refs/tags/v0.1.9
925797f07cfc94a3af1f56cdabd856e11b222b78 refs/tags/v0.1.10
But I have to find the v0.2.2 which is latest created. how can I find the latest created tag ( tag with latest created date) . Is there any other way to do it?
回答1:
You probably need latest tag reachable, not latest tag created:
git describe --tags --abbrev=0
Anyway, just in case you really need last tag created:
git does have two kinds of tags: lightweight and annotated. If your tags are lightweight then tough luck, you can't do this, creation date is not tracked. If you have access to filesystem where your remote repo is stored, then you can try checking timestamps on files in /refs/tags - but this info is not necessarily accurate (it's only timestamp of creation of tag file in this particular repo).
For annotated tags, however, you CAN obtain creation date; once you get sha (by ls-tree or other means), run:
git cat-file -p <sha>
to show tagger, message and creation date or simply:
git show <sha>
to additionally show referenced commit, too.
Using this information in script is doable (maybe not trivial due to date format).
I was going to refer you to git internals description about tags, but turns out, that all this info is also described on ProGit, Git Basics - Tagging.
回答2:
Assuming it's acceptable to determine this locally, you can make sure that your tags are up-to-date with:
git fetch --tags
And then run this from the command line:
git rev-list --tags --timestamp --no-walk |
sort -nr |
head -n1 |
cut -f 2 -d ' ' |
xargs git describe --contains
The git rev-list
command above with --tags
will put down the commits for all tags, while the --timestamp
will make sure each line is prefixed with the timestamp of the commit. The --no-walk
options tells rev-list
not to list any parent commits, so the only commits that show up in the output are the ones tied to tags. sort -nr
says sort the lines numerically and in reverse order so that the commit with the most recent timestamp is at the top. The cut
command allows us to peal off the commit id of the tagged commit,
Running this in my Python Nose repository, I get this:
:: git rev-list --tags --timestamp --no-walk | sort -nr | head -n1 | cut -f 2 -d ' ' | xargs git describe --contains
release_1.3.0
If you must do this all remotely, then you don't really have the option of picking the one with the latest creation date. That information is simply not advertised. You need to download the objects to your local repository first since all this metadata is captured in the objects.
来源:https://stackoverflow.com/questions/21439488/find-latest-git-tag-from-the-remote-git-repository