How can I populate the Git commit ID into a file when I commit?

前端 未结 9 1504
生来不讨喜
生来不讨喜 2020-12-02 12:45

I would like to create Git hook(s) that will populate the commit id of the commit I am about to make into a file (basically variable substitution) in my source code. Is this

9条回答
  •  醉酒成梦
    2020-12-02 12:58

    You can create a filter which does substitution on files on commit and checkout. These are called "smudge" and "clean" filters and their operation is controlled through .gitattributes. For example:

    *.c     filter=yourfilter
    

    This tells git to run the yourfilter filter for all .c files. You then have to tell git what yourfilter means:

    git config --global filter.yourfilter.clean script1
    git config --global filter.yourfilter.smudge script2
    

    You'd then write a script (sed, Perl, Python, or anything) to replace an expression like $LastSha$ with $LastSha: $ on checkout ("smudge"). The other script reverses the expansion before commit ("clean".)

    Search the Pro Git book for "Keyword Expansion" for a detailed example.

提交回复
热议问题