Reputation: 4736
I want to use git hash's for cache busting purposes.
My deployment script would look for specific files in git, and would use the short version of the hash from the last commit where the file was changed. This means that the cache busting string for each file only changes when necessary.
So far I have the following command, which is close but not quite right:
git log -n 1 --abbrev-commit --pretty=oneline htdocs/js/sample.js
which returns:
21b1991 Commit message here
I could parse that string for the commit hash, but I'd rather not do that if possible.
I would like to make git return:
21b1991
I'd certainly like the solution to continue working should git start returning a different length hash upon issuing the command. I'm also not too against using the full hash, but I don't feel that will be necessary.
The deployment will be git based, and taking place on an Ubuntu 12.04 TLS system.
Suggestions?
Upvotes: 1
Views: 814
Reputation: 5703
You should remember that the short commit hash can vary in size, depending on how many commits you have in your history -- the price you pay for using the full hash in your code is small, while with the full hash you are assured of always referring to the correct file.
I look at the short hash as a convenience for us poor small-brained developers, not something that program code should use to save a few bytes.
Upvotes: 0
Reputation: 1984
Try --pretty=format:%h
instead of --abbrev-commit --pretty=oneline
E.g.
git log -n 1 --pretty=format:%h htdocs/js/sample.js
Upvotes: 6
Reputation: 7995
You can use awk for this:
git log -n 1 --abbrev-commit --pretty=oneline htdocs/js/sample.js | awk '{print $1}'
Upvotes: 0