How to simply tag a file deployed into hostile environments with a token reversible into the mod date and git commit SHA
Sometimes you ship code to someone “in the wild” and later you need to understand which revision they’re running, e.g. during trouble-shooting or when they file a bug report. Doing this with Git is pretty easy:
1. Tell git that you’d like cvs-style ident keywords expanded on checkout for whatever file, e.g. /path/to/foo:
echo "path/to/foo ident" >> .gitattributes;
git ci .gitattributes -m "enable keyword expansion in path/to/foo"
Only do this for text files, by the way, not binaries.
2. Add the string $Id$
somewhere in /path/to/foo
, like this:
# current git blob
v = '$Id$'
...
Now, for all revisions checked-out after your telling git that you want $Id$
keywords to be expanded in foo
, whenever foo
is checked out, git will replace the actual $Id$
in the file with the most-recent blob when foo
was changed, e.g.:
# current git blob
v = '$Id: e184834e6757aac77fd0f71344934b1cd774e6d4 $'
...
Now, you can ask the remote customer to send you the value of v
to get the blob version they’re running. But we’re not done yet!
3. To turn that blob’s SHA into a commit SHA (which will tell you when that file was committed, what was changed in the commit, and what other files were changed on that commit), use this script:
#!/bin/sh
blob=$1
git rev-list --all |
while read commit; do
if git ls-tree -r $commit | grep -q $blob; then
echo $commit
fi
done