10

that's more of a know-how questions probably:

I'm versioning with git and send files for a PHP CMS to the test or production site using rsync. Now I'd like to keep track on what commit is currently deployed using a fool-proof and automated system, I was thinking about this:

Set up a git hook to add/update a text file with the latest tag and commit hash. Then I can easily look up the commit.

My problem is that at the time of pre-commit the script won't know the commit hash. Is there any straight-forward method to get that done (or another approach that comes to the same ends)?

Thanks for your input in advance!

Klas Mellbourn
  • 42,571
  • 24
  • 140
  • 158
Martin Rasser
  • 300
  • 2
  • 10
  • I'd think you want to handle that on the deploy side. Write a script to do your deploys, then make that script copy the current hash somewhere in each file before it copies them. – Mason May 03 '13 at 17:56
  • Funny, after posting the question I got exactly that idea. Still, having git taking care of the version file would be even better - this way it won't matter how the deployment is handled. – Martin Rasser May 03 '13 at 17:59
  • If you try to store the version information in a file under git's version control, update and commit, you're going to get a new commit SHA1. :) You have to do it outside of git. – Tuxdude May 03 '13 at 18:14
  • No, this is logically impossible, and your solution can't work. Placing the "current" commit ID into a file will necessarily change the current commit it. You can (at best) put the *previous* commit ID into the file, but this will be much less useful. You really, *really* shouldn't be trying to manage this with Git. – user229044 May 03 '13 at 19:23

3 Answers3

10

Alright, I think I got an ok-solution:

There is a git hook called post-commit and here is what I do:

  • I put the file holding the tag/hash on .gitignore (to avoid unnecessary changes on the next commit)
  • Let the post-commit hook update the version file.

Content of the hook file:

#!/bin/sh 
git describe --tags > version.txt 

Now I'm sure that file is up-to-date after each commit, so I'm all set as long as I do a commit before deploying.

Notes: Nasty beginner's caveat: make the hook file executable, git ignores the file without warning if it isn't.

All about git hooks: http://git-scm.com/book/en/Customizing-Git-Git-Hooks

All about .gitignore: http://git-scm.com/book/en/Git-Basics-Recording-Changes-to-the-Repository#Ignoring-Files

Cheers,

Martin

Martin Rasser
  • 300
  • 2
  • 10
3

This is an FAQ.

https://git.wiki.kernel.org/index.php/Git_FAQ#Does_Git_have_keyword_expansion.3F

Search for export-subst in gitattributes(5), you need to use git-archive(1) to get the substitution done.

(%H gives you the hash. In order the get the tag you would still need a script that calls git-describe(1), I don't see a format for that)

Uwe Geuder
  • 2,236
  • 1
  • 15
  • 21
  • Beautiful! After some trial and error, here's my .gitattribtues: `version.txt export-subst`. And my version.txt: `$Format:%H$` – Peter Ehrlich Oct 09 '15 at 23:27
3

Since you're using rsync to deploy your code, then, do something like this:

$ git describe --long > VERSION.txt

Then, include the VERSION.txt in the rsync package.

The git describe string looks like this:

$ git describe --long
r1.0-2-gca93d0a

In the above:

  1. Latest tag is r1.0
  2. The 2 indicates that we are two commits past that tag
  3. The g stands for 'git' (ok, that's a little weird, but, oh well)
  4. The current hash is ca93d0a
John Jesus
  • 2,284
  • 16
  • 18
  • Thanks, looking good, saw your answer after posting mine - check out the answer where git takes care of it. Come to think of it, letting the rsync script take care of it has its merits, since it makes sure the version file is up-to-date. I could even take the rsync script so far to actually disallow a deployment if there are uncommited changes in the working copy. – Martin Rasser May 04 '13 at 11:28
  • Good idea about checking the working tree for strays. Can you vote up my answer if it's useful? I need a couple of points to get to the next level. – John Jesus May 05 '13 at 13:47