Skip to content

Instantly share code, notes, and snippets.

@r2DoesInc
Created June 2, 2014 14:32
Show Gist options
  • Save r2DoesInc/0139fc3e6e6b19adcbb2 to your computer and use it in GitHub Desktop.
Save r2DoesInc/0139fc3e6e6b19adcbb2 to your computer and use it in GitHub Desktop.
Git hook to bump versionCode and versionName on every commit
#!/bin/sh
#Define Manifest file and current versionCode.
manf=AndroidManifest.xml
versCode=`sed -n '/versionCode=/s/.*"\([0-9][0-9]*\)".*/\1/p' $manf`
#versionCode is a number, so simply add 1.
newVersCode=$((versCode + 1))
#Swap old versionCode with new versionCode and write it to the file
sed /versionCode=/s/'"'$versCode'"'/'"'$newVersCode'"'/ $manf >new$manf && cp new$manf $manf && rm -f new$manf
echo versionCode=$newVersCode
#Get the versoinName string
versName=`sed -n '/versionName=/s/ android:versionName="//p' $manf`
#Since the versionName is actually a string, we only want to bump the last number.
versName=`echo $versName| cut -f1 -d"\""`
lastPlace=${versName#${versName%?}}
newLast=$((lastPlace + 1))
newVersName="${versName%?}$newLast"
sed /versionName=/s/'"'$versName'"'/'"'$newVersName'"'/ $manf >new$manf && cp new$manf $manf && rm -f new$manf
echo versionName=$newVersName
#Add new changes so they are included.
git add $PWD/.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment