Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GIT: Have current commit hash and latest tag in file on commit

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!

like image 282
Martin Rasser Avatar asked May 03 '13 17:05

Martin Rasser


People also ask

How do I see the last commit of a file?

Viewing a list of the latest commits. If you want to see what's happened recently in your project, you can use git log . This command will output a list of the latest commits in chronological order, with the latest commit first.

How do I view commit hash?

# open the git config editor $ git config --global --edit # in the alias section, add ... [alias] lastcommit = rev-parse HEAD ... From here on, use git lastcommit to show the last commit's hash. Save this answer.

How do you get a commit SHA tag?

The commit Tag/SHA is accessible inside of Looker's commit history, as well as inside of the remote repo.

Does changing the commit message change the hash?

If you amend the commit message, or the files in a commit, this will change the git hash.


2 Answers

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

like image 165
Martin Rasser Avatar answered Sep 22 '22 16:09

Martin Rasser


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)

like image 44
Uwe Geuder Avatar answered Sep 25 '22 16:09

Uwe Geuder