4

I am using a pre-push hook that copies some files around and uses grunt bump to up the version number on my package prior to zipping it up. This all happens on the master branch and the assumption is that because it's a push on master, it's worthy of a patch version increase and zips up my Chrome extension to include all the necessary files.

The issue I am running into is that the version is modified too late for it to be committed for the hook to include it in the push, thus my next commit is always 1 patch version behind what is actually tagged.

What solution do I have here? This is a single-user project so I am not worried too much about going against how the hook is intended to be used. Would an option be to commit and a sub push, and then return non-zero from this hook to prevent the push from happening?

#!/bin/sh
PATH=$PATH:/usr/local/bin:/usr/local/sbin

BRANCH="$1"
EXTENSION_PATH=./extension
SCRIPT_PATH=$EXTENSION_PATH/script/


if [ "$branch" == "master" ]
then

    # remove the existing script folder; we'll just re-populate it
    echo "Removing existing scripts..."
    rm -rf $EXTENSION_PATH/script

    echo "Making target directory: $SCRIPT_PATH"
    mkdir $SCRIPT_PATH

    echo "Copying script files..."
    cp config.js $SCRIPT_PATH
    cp supportportal.js $SCRIPT_PATH
    cp -r ./scripts/ $SCRIPT_PATH/scripts/

    # bump the version
    grunt bump

    # commit changes and push here
    # >>>

    # if on a tag, append that to the filename
    VERSION=$(git describe --tags --always)

    # get name of the most top folder of git repo directory,
    # combine with revision tail
    OUTPUT=extension-$VERSION.zip

    # building archive
    zip -r -X $OUTPUT $EXTENSION_PATH 

    echo "Done!";
fi

exit 0 #exit 1 instead of 0 to cancel original push?
Tristan Lee
  • 1,210
  • 10
  • 17

1 Answers1

0

Instead of using a client-side hook like a pre-push or even a pre-commit one, another alternative is to use a content filter driver, specifically the 'clean' one, which is run automatically on git commit.

I proposed that in "Why is the index not updated after doing a git add in a pre-commit hook?"... but that would not capture the last commit (in git describe), ... since said last commit is being built.

Instead, as in this answer, a post-commit hook could be used to (if it is run in the right branch) detect the version change and add/update the version file.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250