I used to work under Subversion/SVN and was instantly using nice feature called keyword substitution. Just putting in source files smth like:
/*
* $Author: ivanovpv $
* $Rev: 42 $
* $LastChangedDate: 2012-05-25 21:47:42 +0200 (Fri, 25 May 2012) $
*/
And each time Subversion was substituting keywords (Author, Rev, LastChangedDate) with actual ones.
Some time ago I was forced to move to Git and just wondering is there's something similar to Subversion's keyword substitution in Git?
Sadly not.
Read their documentation, link attached: Keyword Expansion
Git doesn't ship with this functionality out of the box. However, there is a chapter in the Git Book on Customizing Git and one of the examples is how to use git attributes to implement a similar result.
There is even an example for
$LastChangedDate: $
:Solution
Well, you could easily implement such a feature yourself.
Basically I embedded the commit command into a shell script. This script will first substitute the desired macros and then commit the changes. The project consists of two files:
Content?
keysub
, a bash shell script andkeysub.awk
an awk script to replace keywords in a specific file. A third file is a config file which contains the values that should be substituted (besides variable stuff like commit count and timestamp).How do you use it?
You call
keysub
instead of commit with the same options. The-m
or-a
option should come before any other commit option. A new option (that should always come first) is-f
which takes a config file as a value. Example:or
keysub
keysub.awk
Config file
Remarks
I've tried to document well enough so you can easily implement it and modify it to your own, personal needs. Note that you can give the macros any name you want to, as long as you modify it in the source code. I also aimed to keep it relatively easy to extend the script, you should be able to add new macros fairly easily. If you're interested in extending or modifying the script, you might want to take a look at the .git directory too, there should be plenty of info there that can help to enhance the script, due to lack of time I didn't investigate the folder though.