每次git push完成时更新版本? [英] update version each time git push is done?

查看:190
本文介绍了每次git push完成时更新版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例子我有一个2 php文件

  libs / lib1.php 
libs / lib2.php

通常会有一些信息,比如

  / ** 
* LIB1.PHP
*这个libs做了这样的
*,并且这是一个文档给你
* @version 145
* @todo东西todo
* @author DAMS
* /

/ **
* LIB2.PHP
*这个libs做了这样的
*并且这是一个doc为你
* @version 445
* @todo东西todo
* @author DAMS
* /

我们可以在每次推送时搜索并添加+1版本吗?

解决方案

你要求的基本上是关键字扩展。首先,看看Git常见问题 git是否有关键字扩张?。它说:


建议不要使用关键字扩展。关键字扩展会导致各种奇怪的问题,而且无论如何都不是很有用,尤其是在SCM的背景下。您可以使用自定义脚本在git之外执行关键字扩展。 Linux内核导出脚本可以在Makefile中设置EXTRA_VERSION变量。

如果您真的想这样做,请参阅 gitattributes(5)。如果你的翻译是不可逆的(例如SCCS关键字扩展),这可能是有问题的。 (提示:提供的 $ Id $ -expansion 将40个字符的十六进制Blob对象名称转换为ID;您可以通过使用像这样的脚本来确定哪些提交包含此blob

请参阅此处讨论这里有关GIT如何帮助的信息

所以git确实有类似关键字扩展的内容,尽管不推荐。 Git也没有文件修订版的概念(这就是你的 @version 编号似乎是),所以这将无法给你正是你要求的。

你也可以创建一个钩子(可能是一个预接收钩子),它会为你增加版本。这只是一个可执行文件(所以它可以是一个shell / Python / Perl / Ruby /无论你喜欢的脚本),当你进行推送时它会自动执行。请参阅 githooks手册页



你的钩子脚本会:


  • 识别即将被修改的文件。
  • >
  • 其中,找到包含模式 @version \ d +

  • 的版本增加版本。您希望增加父提交中的值,而不是当前文件中的值!



请注意,这是至少与使用Git FAQ推荐的关键字扩展一样糟糕,并且可能更糟糕。这可能会导致令人讨厌的合并冲突。你也可能很容易在代码中产生误导性的版本号,特别是如果你需要对旧版本提交错误修正,尽管它可能会在你合并来自多个回购/分支的更改时发生(这在大多数情况下很常见git工作流程),如果你不小心。


how do we do like changing version ( each +1 ) using git on each push?

example i have a 2 php file

libs/lib1.php
libs/lib2.php

on each header usually there is some information like

/**
 * 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
 */

can we search and add +1 do version every time we push?

解决方案

What you're asking for is essentially keyword expansion. To start, take a look at the Git FAQ question "Does git have keyword expansion?". It says:

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.

So git does have something like keyword expansion, though it's not recommended. Git also doesn't have the concept of a "file revision" (which is what your @version number appears to be), so this wouldn't be able to give you exactly what you're asking for.

You could alternatively create a hook (probably a pre-receive hook?), that would increment the version for you. This is just an executable (so it can be a shell/ Python/ Perl/ Ruby/ whatever-you're-comfortable-with script) that'll get executed automatically when you do a push. See the githooks man page.

Your hook script would:

  • Identify files that are about to be modified.
  • Of those, find ones that contain the pattern @version \d+
  • Increment the version. You want to increment the value in the parent commit, not the value that's currently in the file!

Note that this is at least as bad as using the keyword expansion that the Git FAQ recommends against, and possibly much worse. This will likely to lead to annoying merge conflicts. You could also easily end up with misleading version numbers in your code, especially if you ever have to commit a bugfix to an older version, though it will probably also happen whenever you merge changes from multiple repos/branches (which is pretty common with most git workflows) if you aren't careful.

这篇关于每次git push完成时更新版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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