Tips & Tricks

Auto build number and revision date

How to manage the version number in Git? stackoverflow

  1. Create file in project dir (or where you want) build_number and put the value 1 in this file <!–
  2. Go to git dir and create file called “pre-commit” in .git/hooks/ (without .sample)
#!/bin/bash
#
# An example hook script to verify what is about to be committed.
# Called by "git commit" with no arguments.  The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
#
# Update Buildnumber
# - Read buildnumber from JSON or default 0
buildnumber=$(jq -r .buildnumber .postcommit.json // 0)
# - Increment
((currentbuildnumber+=1))
#echo $currentbuildnumber
#
# Log Buildnumber
currentbranch=`git branch | tr -cd "[:alpha:]"`
git log $currentbranch --pretty=format:"%h - %an, %ar : %s, Build: $currentbuildnumber"
#
# Get repository name
repo=$(basename `git rev-parse --show-toplevel`)
#
# Get version from tags (= latest)
version=$(git describe --abbrev=0 --tags)
#git tag>tags
#
# [date command --iso-8601 option](https://unix.stackexchange.com/a/629504)
# [how to get local date/time in linux terminal while server configured in UTC/different timezone?](https://stackoverflow.com/a/63063754)
# For a local time, use "date".
revision_local=$(date +'%Y-%m-%dT%H:%M:%S.%3N%:z')
#
# For UTC time, use "date -u".
revision=$(date -u +'%Y-%m-%dT%H:%M:%S.%3N%:z')
#
# Build JSON
json=$(cat <<EOL
{
    "repository":       "${repo:-REPOSITORY}",
    "version":          "${version:-VERSION}",
    "revision":         "${revision:-REVISION}",
    "revision_local":   "${revision_local:-revision_local}",
    "build_number":     ${currentbuildnumber:-0}
}
EOL
)
# Write JSON to file
echo "$json" > repo.json

Example: pre-commit

Post commit log

  1. Go to git dir and create file called “post-commit” in .git/hooks/ (without .sample) In .git/hooks/post-commit add the command:
git show --pretty=format:'{"commit":"%H","author":"%an","email":"%ae","date":"%aI","subject":"%f"}' --no-patch --date=iso-strict > .postcommit.json

which in .post-commit will give you data like:

{"commit":"cafedeaffacebadaddbeebeefdeaddadbadbedacecab","author":"Who Me","email":"whome@nowhere.com","date":"2026-03-06T10:16:38+01:00","subject":"Commit test-2"}
  1. Put this code there if you want “The Full Monty”

(Source: git-show - Show various types of objects ):

#!/bin/bash
# Runs after commit
#
# [Auto build number and revision date](https://github.com/Clicketyclick/TipsAndTricks/tree/master/docs/Github#auto-build-number-and-revision-date)
# [Post commit log](https://github.com/Clicketyclick/TipsAndTricks/tree/master/docs/Github#post-commit-log)
#
# Read build number
buildnumber=$((cat postcommit.json 2>/dev/null || echo '{}') | jq '.buildnumber // 0')
# - Increment
((buildnumber+=1))
#
currentbranch=`git branch | tr -cd "[:alpha:]"`
git log $currentbranch --pretty=format:"%h - %an, %ar : %s, Build: $buildnumber" > /dev/null
#
# Get repository name
system_name=$(basename `git rev-parse --show-toplevel`)
#
version=$(git describe --abbrev=0 --tags)


# 1. Define the Git fields we want to extract (separated by a pipe)
# Note: Use a delimiter that is unlikely to appear in names (like | or \t)
git_format="%H|%h|%T|%t|%P|%p|%an|%aN|%ae|%aE|%al|%aL|%ad|%aD|%ar|%at|%ai|%aI|%as|%ah|%cn|%cN|%ce|%cE|%cl|%cL|%cd|%cD|%cr|%ct|%ci|%cI|%cs|%ch|%d|%D|${system_name}|${version}|${currentbranch}|${buildnumber}|%cI"

# 2. Define the jq transformation structure
# This maps the array indices from the split to your JSON keys
jq_filter='split("|") | {
    "system_name": .[36],
    "version": .[37],
    "currentbranch": .[38],
    "buildnumber": (.[39] | tonumber),
    "revision": .[40],
    "commit_hash": .[0],
    "abbreviated_commit_hash": .[1],
    "tree_hash": .[2],
    "abbreviated_tree_hash": .[3],
    "parent_hashes": .[4],
    "abbreviated_parent_hashes": .[5],
    "author_name": .[6],
    "author_name_mailmap": .[7],
    "author_email": .[8],
    "author_email_mailmap": .[9],
    "author_email_local-part": .[10],
    "author_local-part": .[11],
    "author_date": .[12],
    "author_date_RFC2822_style": .[13],
    "author_date_relative": .[14],
    "author_date_UNIX_timestamp": .[15],
    "author_date_ISO_8601-like_format": .[16],
    "author_date_strict_ISO_8601_format": .[17],
    "author_date_short_format_(YYYY-MM-DD)": .[18],
    "author_date_human_style": .[19],
    "committer_name": .[20],
    "committer_name_mailmap": .[21],
    "committer_email": .[22],
    "committer_email_mailmap": .[23],
    "committer_email_local-part": .[24],
    "committer_local-part": .[25],
    "committer_date": .[26],
    "committer_date_RFC2822_style": .[27],
    "committer_date_relative": .[28],
    "committer_date_UNIX_timestamp": .[29],
    "committer_date_ISO_8601-like_format": .[30],
    "committer_date_strict_ISO_8601_format": .[31],
    "committer_date_short_format": .[32],
    "committer_date_human_style": .[33],
    "ref_names": .[34],
    "ref_names_without_wrapping": .[35]
}'

# 3. Execute
git show -s --format="$git_format" --no-patch --date=iso-strict | jq -R "$jq_filter" > .postcommit.json