git是否有类似svn propset svn:keywords或者提交前/提交后的钩子的东西?
时间:2020-03-05 18:46:30 来源:igfitidea点击:
浏览git文档时,我看不到任何类似于SVN的提交钩子或者"属性"功能的东西,例如,只要文件被提交到存储库,它们就可以更新文件中的版本号或者版权声明。
是git用户被期望为这种功能编写外部脚本(这似乎不是不可能的)还是我只是想念一些明显的东西?
编辑:只是要清楚一点,例如,我对
svn propset svn:keywords "Author Date Id Revision" expl3.dtx
像这样的字符串:
$Id: expl3.dtx 780 2008-08-30 12:32:34Z morten $
每当发生提交时,都会保持有关相关信息的最新信息。
解决方案
回答
也许最常见的SVN属性" svn:ignore"是通过.gitignore文件而不是元数据完成的。恐怕我对其他类型的元数据没有更多帮助。
回答
引用Git常见问题解答:
Does git have keyword expansion? Not recommended. Keyword expansion causes all sorts of strange problems and isn't really useful anyway, especially within the context of an SCM. Outside git you may perform keyword expansion using a script. The Linux kernel export script does this to set the EXTRA_VERSION variable in the Makefile. See gitattributes(5) if you really want to do this. If your translation is not reversible (eg SCCS keyword expansion) this may be problematic.
回答
Git确实有预提交和后提交钩子,它们位于每个.git / hooks目录中。只需修改文件并对其进行chmod使其可执行即可。
回答
我在其他地方写了一个相当完整的答案,其中的代码显示了如何做到这一点。总结:
- 我们可能不想这样做。使用
git describe
是一个合理的选择。 - 如果确实需要这样做,
$ Id $
和$ Format $
相当简单。 - 任何更高级的操作都需要使用
gitattributes
和自定义过滤器。我提供了$ Date $
的示例实现。
基于挂钩函数的解决方案通常无济于事,因为它们会使工作副本变脏。