开发者

List tags with commits in the same format like git branch -v

Hi I would like to list tags like it's listed by:

# git branch -v
* devel  e7f5e36 firxed bugs
  master 63e9c56 remove unused code

without th开发者_JAVA百科e * (you can't checkout tag).

It would be good to have an option to list full or short SHA1. A bash script is also fine, but it would be nice to use git commands more and shell scripting less.

I've read this question How to tell which commit a tag points to in Git? and it helped me, but it's not all I want.

Edit:

I didn't know that annotated tags had SHA1. I wanted SHA1 of commits that tag points to, not the tags themselfs.


Update Removed old stuff, update with formatting

Simplest git porcelain way to get the most info:

git show-ref --tags --abbrev

(loose the --abbrev for full sha1)

Full bells and whistles:

git for-each-ref --format="%(refname) %(objectname) %(subject)" |
      grep /tags/ |
      sed 's#^refs/tags/##'

Typical output:

tag1 b93d2f5e5ad52b8abbee9f5ca8435a1d271ddbb6 test
tag2 b93d2f5e5ad52b8abbee9f5ca8435a1d271ddbb6 test
tag3 58b05fd27be9484bf981752e89843bee7aff8860 test2
tag4 b1be7e4de8949b7ba515e4cad5d214dc71d9b6d2 annotated
tag5 779528366646eeef1d87d30fa3d76c70d0493dff and signed

To clone my test repo:

base64 -d <<HERE > /tmp/so5529735.bundle
IyB2MiBnaXQgYnVuZGxlCjU4YjA1ZmQyN2JlOTQ4NGJmOTgxNzUyZTg5ODQzYmVlN2FmZjg4NjAg
cmVmcy9yZW1vdGVzL3EvbWFzdGVyCmI5M2QyZjVlNWFkNTJiOGFiYmVlOWY1Y2E4NDM1YTFkMjcx
ZGRiYjYgcmVmcy90YWdzL3RhZzEKYjkzZDJmNWU1YWQ1MmI4YWJiZWU5ZjVjYTg0MzVhMWQyNzFk
ZGJiNiByZWZzL3RhZ3MvdGFnMgo1OGIwNWZkMjdiZTk0ODRiZjk4MTc1MmU4OTg0M2JlZTdhZmY4
ODYwIHJlZnMvdGFncy90YWczCmIxYmU3ZTRkZTg5NDliN2JhNTE1ZTRjYWQ1ZDIxNGRjNzFkOWI2
ZDIgcmVmcy90YWdzL3RhZzQKNzc5NTI4MzY2NjQ2ZWVlZ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/SwjjBmdFkoouWmLGlkamCtoGRgQEXV2JeXn5JYklq
ChcAos4JUiB4nAMAAAAAATDs5z1PX9htU9L79152rIjAVSvV
HERE

git clone --mirror /tmp/so5529735.bundle /tmp/so5529735.git

Cheers


Result of sehe's version:

# git for-each-ref --format="%(refname) %(objectname) %(subject)" \
  | grep /tags/ |       sed 's#^refs/tags/##'
initial a9dcc1f24cacde535523bddc22f4c69467428550 initial commit
v0.1.1-alpha b784145a9a71478337f5ceae30aaac4e1b955ee5 initial version. big chance to work. not quite tested
v0.1.2-alpha 193993aabb0c3634ebea0405ecd420e99c609422 it is still alpha
v0.1.3-alpha 415fb8f25982233b1b1211d8500e923e40ce64fd works better. not tested in details.

result of sehe's version with short output (like default output git branch -v):

# git for-each-ref --format="%(refname) %(objectname:short) %(subject)" \
   | grep /tags/ |       sed 's#^refs/tags/##'
initial a9dcc1f initial commit
v0.1.1-alpha b784145 initial version. big chance to work. not quite tested
v0.1.2-alpha 193993a it is still alpha
v0.1.3-alpha 415fb8f works better. not tested in details.

Result of my version:

initial      a9dcc1f initial commit
v0.1.1-alpha a9dcc1f initial version. big chance to work. not quite tested
v0.1.2-alpha 8dd326d it is still alpha
v0.1.3-alpha 54d7ccd works better. not tested in details.

Here initial is lightweight tag

Difference:

  1. (most imortant) First column is automatically padded to biggest tag length - it's more readable
  2. SHA1 is of commit that the tag points to even if it's not lightweight tag. This way you can easily see that initial points to is the same commit as v0.1.1-alpha. This behavior can easily be made optional.

My script is too long. I'll post only git calling function (bash script). Most of it is taken from git-for-each-ref man page:

function gitTags(){
  arg="$1"
  REF=${arg:-"*"}
  fmt='
    r=%(refname)
    t=%(*objecttype)
    T=${r#refs/tags/}
    o=%(*objectname:short)
    s=%(subject)
    kind="T"
    if [ "$t" = "" ] ; then
      # could be a lightweight tag
      t=%(objecttype)
      kind="L"
      o=%(objectname:short)
    fi
    len=${#T}
    if [ "$len" -gt ${len1:-0} ]; then
      len1=$len
    fi

    echo -e "$T\t$o\t$s"
  '
  eval=`git for-each-ref --shell --format="$fmt" \
          --sort='*objecttype' --sort=taggerdate "refs/tags/$REF"`

  out=`eval "$eval"'echo $len1;'`
  local len1=`echo "$out"|tail -n 1`

  echo "$out"|head -n -1 \
     |awk 'BEGIN{FS="\t"}{printf("%-'$len1's %s %s\n", $1, $2, $3)}'

}

usage:

#show all tags:
gitTags
#show all tags starting with v (v1.0)
gitTags v*


The OP said he wanted the SHA1 of the commit that the tags points to. The above solutions given by sehe and NickSoft don't give the SHA1 of the commit when the tag is an annotated tag. Instead, they give the SHA1 of the annotation object being pointed to. (The annotation object points to the commit.)

If you want to list every tag and the SHA1 of the commit, then you can simply execute:

    git log --tags --oneline --decorate=short

Unfortunately, that won't give you a format that is similar to

    git branch -v

But you can come pretty close by executing:

    git for-each-ref --format="%(refname:short)" refs/tags | 
    awk '{printf "%s ",$1; system("git rev-list --max-count=1 "$1)}'

Note that git rev-list will give you the SHA1 of the commit, but git show-ref and git for-each-ref only give the SHA1 of the object being pointed to (which in the case of an annotated tag is the annotation object not the commit).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜