我们如何在每次推送时使用 git 更改版本(每个 +1)?
例如我有一个 2 php 文件
libs/lib1.php
libs/lib2.php
在每个标题上通常有一些信息,例如
/**
* LIB1.PHP
* this libs does something like this
* and that this is a doc for you
* @version 145
* @todo something todo
* @author DAMS
*/
/**
* LIB2.PHP
* this libs does something like this
* and that this is a doc for you
* @version 445
* @todo something todo
* @author DAMS
*/
我们可以在每次推送时搜索并添加 +1 do 版本吗?
最佳答案
您所要求的本质上是关键字扩展。首先,查看 Git FAQ 问题“Does git have keyword expansion?”。它说:
Keyword expansion is not recommended. Keyword expansion causes all sorts of strange problems and isn't really useful anyway, especially within the context of an SCM. You can perform keyword expansion outside of git using a custom 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. (Hint: the supplied $Id$-expansion puts the 40-character hexadecimal blob object name into the id; you can figure out which commits include this blob by using a script like this.)
See here for a discussion, and here on how GIT may help anyway.
所以 git 确实有类似关键字扩展的东西,虽然不推荐。 Git 也没有“文件修订”的概念(这就是你的 @version
数字似乎是什么),所以这将无法准确地给出你所要求的为。
您也可以创建一个 Hook (可能是预接收 Hook ?),这会为您增加版本。这只是一个可执行文件(因此它可以是一个 shell/Python/Perl/Ruby/任何你喜欢的脚本),当你执行推送时它会自动执行。查看githooks man page .
你的钩子(Hook)脚本将:
- 识别将要修改的文件。
- 其中,找到包含模式
@version\d+
的那些
- 增加版本。您想要增加父提交中的值,而不是当前文件中的值!
请注意,这至少与使用 Git 常见问题解答建议反对的关键字扩展一样糟糕,甚至可能更糟。这可能会导致烦人的 merge 冲突。你也可能很容易在你的代码中得到误导性的版本号,特别是如果你不得不提交一个错误修复到一个旧版本,尽管它也可能在你 merge 来自多个 repos/branches 的更改时发生(这在大多数情况下很常见git 工作流程)如果你不小心的话。
关于git - 每次 git push 完成后更新版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4532835/