Skip to content

Instantly share code, notes, and snippets.

@ThomDietrich
Last active December 19, 2022 15:23
Show Gist options
  • Save ThomDietrich/7127ca6e45dd4747e86ad9a609e1aeeb to your computer and use it in GitHub Desktop.
Save ThomDietrich/7127ca6e45dd4747e86ad9a609e1aeeb to your computer and use it in GitHub Desktop.
git hook for revision and branch version file
#!/bin/bash
## Automatically generate a file with git branch and revision info
##
## Example:
## [master]v2.0.0-beta-191(a830382)
## Install:
## cp git-create-revisioninfo-hook.sh .git/hooks/post-commit
## cp git-create-revisioninfo-hook.sh .git/hooks/post-checkout
## cp git-create-revisioninfo-hook.sh .git/hooks/post-merge
## chmod +x .git/hooks/post-*
FILENAME='public/gitrevision.txt'
exec 1>&2
branch=`git rev-parse --abbrev-ref HEAD`
longhash=`git log --no-show-signature --pretty=format:'%H' -n 1`
shorthash=`git log --no-show-signature --pretty=format:'%h' -n 1`
revcount=`git log --no-show-signature --oneline | wc -l | tr -d ' '`
latesttag=`git describe --tags --abbrev=0 --always`
#VERSION="[$branch]$latesttag-$revcount($shorthash)"
#VERSION="[$branch]rev$revcount($shorthash)"
JSON="{\"branch\": \"$branch\", \"shorthash\": \"$shorthash\", \"longhash\": \"$longhash\", \"revcount\": \"$revcount\", \"latesttag\": \"$latesttag\"}"
echo $JSON > $FILENAME
@jpstotz
Copy link

jpstotz commented Jan 11, 2019

I would change the line

latesttag=`git describe --tags --abbrev=0`

to

latesttag=`git describe --tags --abbrev=0 --always`

Otherwise you are getting the error fatal: No names found, cannot describe anything. if no tag has been set. With --always you are getting the commit hash if the tag is missing.

@adrfantini
Copy link

adrfantini commented May 14, 2020

Thanks for this. Do you know how would it be possible to add the generated file to the git repo, without of course having to re-commit it?

@ThomDietrich
Copy link
Author

@jpstotz sounds reasonable. Thanks!

@adrfantini you are asking one of these questions that can not be answered straight.
Basically you don't want this. It's comparable to switching on the option to burn a red timestamp on all digital photos of your smartphone. The commit info already exists within the commit metadata, no need to add the textfile to the repo. That said, if you need to do this for some perverted reason 😇 look into the various "pre-" hooks.
Let me quickly clarify the intended use case for the script as it is given above and included as a post-* hook. The file eases the effort to access git metadata from within a script or web application without git tools or elevated permissions. Way back I used the method to present the version of a web app deployed in a container on its front page, mainly to ensure that the CI/CD toolchain is working properly.

@adrfantini
Copy link

@thom my usecase is actually similar, but you are right and I can actually do this at package creation time in the makefile: it's way cleaner and makes more sense. Thanks for the answer!

@marslo
Copy link

marslo commented Jul 16, 2021

the post-commit hook will do: update ${FILENAME} every time after git commit -am <message>, so the "new" ${FILENAME} actually won't be published into remote repository due to the status is modified (instead of staged).

So, is there any way that using git-hook to modify files in post-commit steps, and put changed file into staged status (for push)?

@ThomDietrich
Copy link
Author

Fyi: Slight bugfix update and added json output

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment