Created
January 25, 2016 12:39
-
-
Save labmonkey/f2f9bb6ecf22254a175b to your computer and use it in GitHub Desktop.
Git hook that increments Wordpress plugin and theme version automatically based on number of commits!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # | |
| ## DESCRIPTION | |
| # Git hook that increments Wordpress plugin and theme version | |
| # automatically based on number of commits! | |
| # | |
| ## INSTALLATION | |
| # In order to install put this file in git root and run this command: | |
| # | |
| # ln -s -f ../../pre-commit.sh .git/hooks/pre-commit | |
| # | |
| # The -f flag will replace existing hook file if any! | |
| # | |
| ## SETUP | |
| # Requires TAGS in repository. | |
| # Modifies files with @git-repository set to current repository remote file base name. | |
| # This enables subrepositories to not be affected. | |
| # | |
| # Example Plugin header: | |
| # /* | |
| # * Plugin Name: My Plugin | |
| # * Version: 1.0.0 | |
| # * @git-repository github-repository-name.git | |
| # */ | |
| # | |
| ## WHAT IT DOES | |
| # After Each commit the major version is taken form tag (like 1.0) and | |
| # the minor version will increase based on commit count | |
| # so 1.0.0 -> 1.0.1 -> 1.0.2 -> ... etc | |
| git stash -q --keep-index | |
| echo "Increasing Wordpress plugin and theme version number..."; | |
| REMOTE="$(basename $(git ls-remote --get-url))" | |
| PATTERN="@git-repository\(.*\)$REMOTE" | |
| TAG=$(git describe | cut -d "-" -f 1-2 | tr "-" ".") | |
| TAG="${TAG%.*}.$((${TAG##*.}+1))" | |
| FILES="$(grep --include=\*.{php,css} -r -w -n -l . -e "$PATTERN")" | |
| count=0 | |
| for line in $FILES | |
| do | |
| count=$((count+1)) | |
| sed -i '' -e "s/Version:\(.*\)/Version: $TAG/g" $line | |
| echo "Found file: " $line | |
| done | |
| if [ $count -eq 1 ] | |
| then | |
| files="$(echo file)" | |
| else | |
| files="$(echo files)" | |
| fi | |
| echo "Adding changed files to current commit..."; | |
| # Stage updated files | |
| git add -u | |
| # Re-apply original unstaged changes | |
| git stash pop -q | |
| echo "...Done! Affected $count $files! New version is $TAG"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment