I'm trying to write a Git pre-commit hook script. It should write the date of commit at the beginning of modified files.
My problem is that I can't add modified files to the previous commit. When I am trying invoke to a Git commit again, it runs recursive. How can I write the script, which appends the time of modification at the end of modified files?
My code:
#!/bin/bash
files_modified=`git diff-index --name-only HEAD`
for f in $files_modified; do
    if [[ $f == *.groovy ]]; then
        $line = $(head -1 f)
        if [[ $line == "/%%*" ]];
           then
               sed -i 1d
           fi
           echo "/%% " + $(date +"%m_%d_%Y") + " %%\\" >> f
           git add f
    fi
done
git commit --amend #recursive
exit
                You cannot amend a commit in a pre commit hook.
And what you are doing is similar to the keyword expansion mechanism, which is not a best practice with Git (or any DVCS), as explained in "To put the prefix ?<revision-number> to codes by Git/Svn".
Other approaches include:
May be git attribute with smudge/clean operation is what you are looking for:


Related: use git smudge/clean to replace file contents
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With