Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Git prepare commit hook to alter a file. Append commit id to the version.js file with a prepare-commit hook
# Disclaimer: Commits should have the following format
# <PROJNAME>: <DESCRIPTION>
# Example:
# JIRA-450: My description
# Following code will substring the JIRA-450 and append it as last value of the `window.version` variable, replacing the `SAMPLE`
# This is just a sample, it is not recommended to do such mission, versions should be handled by CI and should follow the SEMVER specification: http://semver.org/
# Should add validation to ensure one is committing following the conventions
# Git hooks documentation https://git-scm.com/docs/githooks
# Get commit message
MESSAGE=`cat $1`
# Substring, get characters, starting from 1 (first) until ':'
MESSAGE=$( echo $MESSAGE | cut -d ':' -f 1 )
# Your js contents
CONTENTS="window.version = '1.2.3-"
# Concatenating message with contents
CONTENTS+=$MESSAGE
CONTENTS+="';"
# Writing to file before commiting
cat > version.js << EOF
$CONTENTS
window.version = '1.2.3-SAMPLE';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment