我想将自定义元数据与git commit
相关联。专门用于记录代码审查中的审阅ID,但它可以是任何内容。标签似乎是一种自然的方式,但我希望每次提交都有一个评论,我不想让gitk
与大量的标签混在一起。是否有其他机制来添加自定义元数据?我可以让某些标签隐身吗?如果我能告诉gitk
不显示匹配某些模式或RE的标签,那可能会有效,但我没有办法做到这一点。
这正是git notes的用途。
使用git notes
,您可以在提交中添加“注释”。您也可以将它们添加到其他Git对象中,但是我们只关注提交,因为这就是问题所在。
注释是Git对象,原则上可以是“任意”(任意数据)。但是,为了我们的目的,我们将专注于简单和文本的东西。
这个问题提到了审核ID,所以让我们用某种方式来表示这样的事情。我不知道哪些评论ID确实如此,但希望以下是明智的:
Review-id: 42
所以这实际上是一个键值对。让我们将上面的字符串添加到当前提交中:
git notes add -m "Review-id: 42"
如果你运行git log
,笔记将以内联方式显示:(†1)
Author: Victor Version Control <[email protected]>
Date: Tue Nov 8 21:10:25 2016 +0100
Implement feature x
Notes:
Review-id: 42
当然,您可以在本说明中添加更多“子注释”(我们将坚持使用简单的key: value
语法,每行一个值)。例如,如果您在三个月后发现提交消息出错了,只需将更正附加到注释:
git notes append -m "Errata: It was actually feature y."
git log
:
Author: Victor Version Control <[email protected]>
Date: Tue Nov 8 21:10:25 2016 +0100
Implement feature x
Notes:
Review-id: 42
Errata: It was actually feature y.
我们使用git notes append
来轻松地将这些额外数据添加到注释中。您也可以使用git notes edit
直接编辑文件。
当然,由于Git注释只是一个可变文件,因此可能会遇到合并冲突。为了减少这种可能性,您可以:
man git-notes
,“注释合并策略”部分。OP问道:
>我可以让某些标签隐身吗?
默认情况下,git log
只显示一个音符,即.git/refs/notes/commits
。 commits
只是命名空间中的一个注释。也许您希望问题出现在他们自己的命名空间中:
git notes --ref=issues add -m "Fixes: #32"
由于这存储在.git/refs/notes/issues
而不是.git/refs/notes/commits
中,因此当您运行git log
时,“Fixes:#32”将不会显示。所以你默认有效地使这些笔记不可见。
如果你想要它显示,请将--notes=issues
传递给git log
:
$ git log --notes=issues
Author: Victor Version Control <[email protected]>
Date: Tue Nov 8 21:10:25 2016 +0100
Implement feature x
Notes (issues):
Fixes: #32
但现在.git/refs/notes/commits
被隐藏了。那个也很容易被包括在内:
$ git log --notes=issues --notes=commits
Author: Victor Version Control <[email protected]>
Date: Tue Nov 8 21:10:25 2016 +0100
Implement feature x
Notes (issues):
Fixes: #32
Notes:
Review-id: 42
Errata: It was actually feature y.
有一些变量可以配置默认显示的注释;见man git-config
。
元数据当然可以直接记录在提交消息中。(†2)但是提交消息是不可变的,因此更改它们实际上意味着进行全新的提交,并带来所需的所有涟漪效果。另一方面,Git-notes是可变的,所以你总是可以修改它们。注释的每个修改当然都是版本控制的。在我们的例子中,对于.git/refs/notes/commits
:
$ git log refs/notes/commits
Author: Victor Version Control <[email protected]>
commit 9f0697c6bbbc6a97ecce9834d4c9afa0d668bcad
Date: Tue Nov 8 21:13:52 2016 +0100
Notes added by 'git notes append'
commit b60997e49444732ed2defc8a6ca88e9e21001a1d
Author: Victor Version Control <[email protected]>
Date: Tue Nov 8 21:10:38 2016 +0100
Notes added by 'git notes add'
默认情况下不会分享您的笔记;你必须明确地这样做。与其他参考文献相比,共享笔记不是非常用户友好。我们必须使用refspec语法:
git push refs/notes/*
以上将把你的所有笔记都推到遥控器上。
似乎提取笔记有点复杂;如果指定refspec的两面,你可以这样做:
git fetch origin refs/notes/*:refs/notes/*
所以这绝对不方便。如果您打算定期使用Git-notes,您可能需要设置gitconfig以始终获取注释:
[remote "origin"]
…
fetch = +refs/notes/*:refs/notes/*
(来源:https://git-scm.com/blog/2010/08/25/notes.html)
Git有一个不方便的默认值,即在重写提交时不会继续注释。因此,如果您举例说明一系列提交,那么这些注释将不会转移到新的提交中。
变量notes.rewrite.<command>
默认设置为true
,因此可以假设音符被结转。但问题是,变量notes.rewriteRef
决定了哪些音符将会被带走,它没有任何震撼力。要将此值设置为与所有注释匹配,请执行以下操作:
git config --global notes.rewriteRef "refs/notes/*"
现在,在执行像git
rebase
这样的重写操作时,所有注释都会被转移。
如果您使用git format-patch
格式化您的更改以作为电子邮件发送,并且您将一些元数据存储为Git备注,则可以将--notes
选项传递给git format-patch
,以便将备注附加到电子邮件草稿中。
†1:“当命令行中没有给出git log
,--pretty
或--format
选项时,这是--oneline
[...]的默认值。” - man git-log
,git version 2.10.2
†2:在诸如以下项目中使用的元数据提交消息的一种惯例/约定。 Git和Linux内核是在提交消息的“预告片”中添加键值对,即在底部。例如,参见Linus Torvalds的this commit:
mm: remove gup_flags FOLL_WRITE games from __get_user_pages()
This is an ancient bug that was actually attempted to be fixed once
(badly) by me eleven years ago in commit 4ceb5db9757a ("Fix
get_user_pages() race for write access") but that was then undone due to
problems on s390 by commit f33ea7f404e5 ("fix get_user_pages bug").
In the meantime, the s390 situation has long been fixed, and we can now
fix it by checking the pte_dirty() bit properly (and do it better). The
s390 dirty bit was implemented in abf09bed3cce ("s390/mm: implement
software dirty bits") which made it into v3.9. Earlier kernels will
have to look at the page state itself.
Also, the VM has become more scalable, and what used a purely
theoretical race back then has become easier to trigger.
To fix it, we introduce a new internal FOLL_COW flag to mark the "yes,
we already did a COW" rather than play racy games with FOLL_WRITE that
is very fundamental, and then use the pte dirty flag to validate that
the FOLL_COW flag is still valid.
Reported-and-tested-by: Phil "not Paul" Oester <[email protected]>
Acked-by: Hugh Dickins <[email protected]>
Reviewed-by: Michal Hocko <[email protected]>
Cc: Andy Lutomirski <[email protected]>
Cc: Kees Cook <[email protected]>
Cc: Oleg Nesterov <[email protected]>
Cc: Willy Tarreau <[email protected]>
Cc: Nick Piggin <[email protected]>
Cc: Greg Thelen <[email protected]>
Cc: [email protected]
Signed-off-by: Linus Torvalds <[email protected]>
看到:
man git-interpret-trailers