Question

One of the scripts in my git repository is distributed to many users. When they run this script I would it to look into my repository and see if any change is made to this script and if so suggest the users to update the script from the repository. The users know nothing about git and wish to understand as little as possible in the system implementation.

I would like to automatically update a this script's md5sum every time I commit it, without the need to bother and do it manually. Here is the pre-commit hook I wrote

#!/bin/sh

echo -n "# "  > test.txt.tmp
linesNumber=`wc -l test.txt | awk '{print $1}'`
tail -n $(($linesNumber - 1)) test.txt | md5sum | awk '{print $1}' >> test.txt.tmp
tail -n $(($linesNumber - 1)) test.txt >> test.txt.tmp
mv test.txt.tmp test.txt
#git push
#git commit --no-verify --message "update file md5sum"

The md5sum works fine, the problem is that after the commit my script is again shows as modified. I tried to commit my changes without running the pre-commit hook (--no-verify) but git says I can't commit because my branch is ahead of 'origin/master' by 1 commit, I tried to push my changes before committing the md5sum but it never finished the operation.

What is the right way to achieve this type of functionality?

Was it helpful?

Solution

Your pre-commit hook does not git add the updated test.txt file.

Note that the version of the file in the working directory is not necessarily the version that would be committed:

echo foo > test.txt
git add test.txt
echo bar > test.txt
git commit -m 'this commit has "foo" in test.txt'

To see, in a pre-commit hook, the version of the file in the index—i.e., what will be committed if the pre-commit hook allows the commit to proceed, without doing any git add step—you can use git show :0:test.txt. The magic :0: prefix is gitrevisions syntax for "version staged for commit". (You can abbreviate this further as just :test.txt, although I find the :0: a bit more obvious as a revspec; I've seen real files really named :README for instance. :-) ) Note that this bypasses any smudge filter(s) though.

(Hooks that want to test the entire current index contents, but preserve working directory state as well, can use git stash save --keep-index. See How do I properly git stash/pop in pre-commit hooks to get a clean working tree for tests for advice, and a caveat, on doing this.)

In my current version of git (1.8.5.4), if I modify a file and git add the result in a pre-commit hook, the contents I added are what goes into the commit. (I do have some vague recollection of an earlier version of git saving the index before the pre-commit hook runs, so that changes made to the index did not make it into the commit, but I might just be mis-remembering.)

Aside: It's generally not a great idea to modify the commit-contents in a pre-commit hook. They're really just meant to verify that the commit is ready to go. But if you're determined to modify the contents, git add (or git update-index but add is much simpler) will do it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top