我如何列出所有轻量级标签? [英] How can I list all lightweight tags?

查看:85
本文介绍了我如何列出所有轻量级标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想列出所有的轻量级标签在我的仓库中;我能想到的最好的方法是将> git for-each-ref grep 削减,但它似乎会很费劲...



(当我们在这时,我们不妨谈论注释标签的同一件事:某人肯定最终会在这里想到这一点。)



编辑:



通过轻量级标签,我的意思是 的标签引用指的是标签对象。 (换句话说,unannotated tags。)

解决方案

所有轻量级标签位于 refs / tags / 命名空间,可以枚举,例如:

  git for-each-ref --format '%(refname:short)'refs / tags / 

或者:

  git show-ref --tags 

至于注释标签,那么这里的技巧 - 它也影响轻量级标签 - 注释标签实际上是git存储库中的一个对象,,有一个轻量级标签指向该对象,它可以让您通过标签名称获取带注释的标签。 1 因此,它确实是一对 的东西:一个轻量级的标签以及in-repo带注释的标签对象,这使得它不是一个轻量级的标签,除了它 是一个轻量级标签的顽固事实!



因此,它归结为:find all lightw八个标签,然后根据您想要的行为选择仅选择指向提交或标签指向标签对象的标签,然后继续发出标签名称。



有以格式编写整个脚本的 git-for-each-ref 文档中的一个长示例字符串并使用 eval 来执行它(或者你可以通过一个额外的进程来管道到 sh 来执行) 。我通常会发现将 git for-each-ref 的输出传递给时更简单 - 读取... 循环:

  git for-each-ref refs / tags / --format'%(objecttype)%(refname:short)' | 
,同时读取ty名称;做[$ ty = commit]&& echo $ name;完成

,其中打印所有轻量级标记。



比较:

  git for-each-ref refs / tags / --format '%(objecttype)%(refname:short)'| 
,同时读取ty名称;做[$ ty = tag]&& echo $ name;完成了

,其中打印了所有带注释的标签(或者更确切地说,注意,一个标签可以(可以想象 - 目前没有实际的用例,据我所知)指向除了提交或标签之外的东西;直接指向树或 blob






1 没有轻量级标签,您将无法引用带注释的标签 annotag 使用名称 annotag - 不经过所有的搜索努力 git fsck 至少用来寻找晃来晃去的物体。此外,如果删除轻量级标签,则注释标签对象可能会被垃圾收集。只要第一个标签对象具有外部名称,您就可以将一个标签对象指向另一个标签对象,以将其保存在回购站中(即禁止gc),而无需第二个标签对象的外部名称。有趣的是,注释标签的内部格式包含外部名称,所以可以使用这种技术来保护旧注释标签,通过删除其轻量级标签来隐藏它们,然后再恢复原始的轻量级标签。是否任何人都可以为此使用 ,尽管...: - )


I want to list all of the lightweight tags in my repository; the best I can think of involves combining git for-each-ref, grep, and cut, but it seems like it'll be kind of fiddly...

(While we're at it, we might as well talk about the same thing for annotated tags: someone is sure to end up here wondering about that at some point.)

Edit:

By lightweight tags, I meant those tag refs that do not refer to tag objects. (In other words, unannotated tags.)

解决方案

All the lightweight tags are in the refs/tags/ namespace and can be enumerated with, e.g.:

git for-each-ref --format '%(refname:short)' refs/tags/

or:

git show-ref --tags

As for annotated tags, well, the trick here—it affects the "lightweight" tags part too—is that an annotated tag is actually an object in the git repository, but, there's a lightweight tag that points to that object, that lets you get at the annotated tag by its tag name.1 So it's really a pair of things: a lightweight tag, plus the in-repo annotated tag object, that makes it "not a lightweight tag", except for that stubborn fact that it is a lightweight tag at the same time!

Thus, it boils down to: find all lightweight tags, then optionally select only tags pointing to commits or tags pointing to tag-objects depending on the behavior you want, then go on to emit the tag name.

There's a long example in the git-for-each-ref documentation of writing an entire script in the --format string and using eval to execute it (or you could pipe to sh to execute, at the cost of one extra process). I usually find it simpler to pipe the output of git for-each-ref into a while read ... loop:

git for-each-ref refs/tags/ --format '%(objecttype) %(refname:short)' |
    while read ty name; do [ $ty = commit ] && echo $name; done

which prints all lightweight-only tags.

Compare with:

git for-each-ref refs/tags/ --format '%(objecttype) %(refname:short)' |
    while read ty name; do [ $ty = tag ] && echo $name; done

which prints all annotated tags (or more precisely, "lightweight-that-are-annotated" tags).

Note that a tag can (conceivably—there's no actual use case for this right now, as far as I know) point to something other than a commit or a tag; it's up to you whether to do something with a tag pointing directly to a tree or blob.


1Without the lightweight tag, you would not be able to refer to annotated tag annotag using the name annotag—not without going through all the search effort that git fsck uses to find dangling objects, at least. Moreover, if you delete the lightweight tag, the annotated tag object may get garbage-collected. You can make one tag object point to another tag object to keep it in the repo (i.e., inhibit the gc) without an external name for the second tag object, as long as the first one has an external name. That's definitely an odd thing to do though.

Interestingly, the internal format for the annotated tag contains the external name, so one can use this technique to protect "old" annotated tags, hide them by removing their lightweight tags, and then later restore the original lightweight tag. Whether anyone can come up with a use for this, though... :-)

这篇关于我如何列出所有轻量级标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆