How to auto-generate a version string in git [duplicate]

怎甘沉沦 提交于 2020-01-03 20:03:13

问题


Possible Duplicate:
Enable ident string for Git repos

In my project, every source file (regardless of the language - Java, Python, shell) has a comment line that contains source control information - branch, date of last commit, committer name, etc.

This is done by using special placeholders (e.g. $Branch$) which are auto-replaced by the source control application.

Is it possible to achieve similar functionality in git?

I am using Git Extensions on Windows and yet-to-be-decided GUI on Linux, but I assume both are just GUIs that invoke the git command-line tools.


回答1:


You have to read about smudge/clean filters in Git in order to get some iteration of keyword-replacement.

Grok section "Keyword Expansion", where example of expanding $DATE$ keyword explained (and a must reverse-operation). In you case most work of expanding $SOMEKEYWORD$ to version string can be performed by git describe under the hood, clean part must be implemented by hand (your hand)




回答2:


Git does not have support for these placeholders and probably never will have.

Instead, automaticaly generate source file which contains output of git describe command and include it while compiling your application. Or you can generate some configuration file instead (JSON or whatever you want).

To generate C++ header with version info, use shell script like this (you can add these commands directly to your makefile):

(
  echo '/* Generated file, do not edit. */'
  echo  '#define APP_VERSION "'`git describe`'"'
  echo  '#define APP_VERSION_DATE "'`git log -n 1 --format=%ai`'"'
) > version.h

To do this for scripting languages you can use post-commit and other hooks.



来源:https://stackoverflow.com/questions/14673007/how-to-auto-generate-a-version-string-in-git

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!