سؤال

I wrote a pre-commit hook to increment the version number in the source. Why are the changes applied after the commit? What can I do to do that prior to the commit?

#!/bin/sh
# Hook to increment version number before checkin

REPO_DIR=$(cd `dirname $0`/../.. && pwd)
VERS_FILE="${REPO_DIR}/version.php"
CUR_DATE=`date -u '+%Y-%m-%d %H:%M:%S'`
HOOK=`basename $0`

if [ -w ${VERS_FILE} ]; then
    # increment last digit of version string
    perl -i -pe 's/(\$version\s?=\s?['\''|"][\d\.]+)(\d+)/"$1".($2+1)/e' "${VERS_FILE}"
    RV=$?
    if [ 0 -ne ${RV} ]; then
        echo "ERROR: Updating version in ${VERS_FILE} failed"
        exit $RV
    fi
    echo "INFO: Increment version in file ${VERS_FILE}"
fi

echo "Finish $HOOK at $CUR_DATE"
exit 0

I'm aware that such automatic changes aren't recommended. But technically this should be possible anyway. I'm using git 1.6.3.3 on Ubuntu 9.10.

هل كانت مفيدة؟

المحلول

Your pre-commit script modifies the working tree copy of the file, but not the version in the index. The latter one is what actually gets committed.

You need to git add the file after modifying it. But, doing this will mean that all changes to that file will be included in every commit even if you're trying to commit just some changes from the working tree.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top