7

Situation

We have a git workflow, where all release versions are stacked on the master branch, and when a commit is ready for deployment, we add a tag to it, then use git archive to build a bundle to be deployed.

We use a version.txt file, marked in .gitattributes with the option export-subst, to keep track of which commit was used to build a given archive.

Question

If I write $Format:%d$ in version.txt, and export a tagged commit, I will have something like (HEAD, tag, master) written in the resulting file.

How can I have the tag alone ?

[edit] There is a git command which already produces that :

git describe --tags HEAD

This will output :

latest_tag            #if HEAD is tagged

latest_tag-5-g03cc91b #if HEAD is not tagged,
                      #and the latest tag is 5 commits ago on commit g03cc91b

Is there some way to have a slug replaced with this output in version.txt ?

2
  • I don't believe you can. The list of supported placeholders are listed in git help log under the --pretty-format section. There is not one for a tag name (and indeed, what would it do if the commit in question was not referenced by a tag?).
    – twalberg
    Commented Oct 29, 2013 at 14:30
  • 1
    Really too bad this isn't possible. It would be very welcome in a world with version tags v1.0.1 and automatically inserting the right version in source files for projects that require that metadata.
    – aross
    Commented Nov 25, 2016 at 14:36

1 Answer 1

3
+50

You could use a content filter driver which would on git checkout automatically do the exact change you want.

content filter river: smudge

(From Pro Git book 7.2 Customizing Git - Git Attributes)

You declare in a .gitattributes file a smudge script which will do the substitution for a special marker (that you want to be replaced with git describe --tags HEAD), without touching the other markers to be modified by export-subst on git archive.

Then you call git archive, which should (not tested) replace the rest.

2
  • Ok, it could be a workaround. This leads to another question : do you know a way to share the .git/config (or at least the relevant part for smudging/cleaning) along with the repository ?
    – LeGEC
    Commented Jun 4, 2014 at 8:40
  • @LeGEC the smudge script and the .gitattributes are in the repo (so sharable with the rest of the repo). But the declaration in the .git/config remains to be done by the user (you could version a script the user has to execute to add that config). A config is never shared (stackoverflow.com/a/6548056/6309)
    – VonC
    Commented Jun 9, 2014 at 5:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.