Skip to content

Instantly share code, notes, and snippets.

@labmonkey
Created January 25, 2016 12:39
Show Gist options
  • Select an option

  • Save labmonkey/f2f9bb6ecf22254a175b to your computer and use it in GitHub Desktop.

Select an option

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!
#!/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