Search code examples
gitpost-update

update version each time git push is done?


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?


Solution

  • 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.