How to automatically generate version string in git

Possible duplicate:
Enable ID string for git repos

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

This is done using special placeholders (for example, $ Branch $), which are automatically replaced by the version control application.

Is it possible to achieve similar functionality in git?

I use Git extensions for Windows and an undefined GUI for Linux, but I assume that both are just GUIs that invoke Git command-line tools.

+4
source share
2 answers

You have to read smudge / clean filters in Git to get some iteration of keyword substitution.

The Grok Keyword Extension section, which explains an example of expanding the keyword $ DATE $ (and a ). In this case, most work on expanding the $ SOMEKEYWORD $ line to the version can be done using git describe under the hood, the clean part must be done manually (your hand)

+3
source

Git does not support these placeholders and probably never will.

Instead, automatically create a source file containing the output of the git describe command and including it at compile time of your application. Or you can create some kind of configuration file (JSON or whatever).

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

 ( 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 interceptors.

+5
source

All Articles