Unexpected behavior when git-adding files in a pre-commit hook then using "git commit -o"

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

I’ve encountered unexpected behavior performing a git add from a pre-commit hook, followed by performing a commit using the “-o” flag, providing a pathspec. Here is a small bash script that explains the issue and serves as a repro:

###############
echo "----> Setup a new test repo"
dir_name="PreCommitHookAddTest"
rm -rf $dir_name; mkdir $dir_name; cd $dir_name
git init; git commit --allow-empty -m "Initial commit"

echo "----> Add a pre-commit hook that stages a file that doesn't currently exist in the repo"
echo "touch auto-added; git add auto-added" > .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit

echo "----> Try committing a new file using the '-o' flag"
touch manually-added; git add manually-added
git commit -o -m "Commit that ran the pre-commit hook and should contain file 'auto-added'" -- manually-added

echo "----> Results (expected: working copy clean; actual: auto-added is reported as both DELETED and UNTRACKED. HEAD and working copy are the same, staging area contains ‘incorrect' state)"
git status

echo "----> Stage the file after the fact"
git add auto-added

echo "----> Notice that the working copy is now clean"
git status
###############

I would like to avoid the above-described “invalid” state by performing operations correctly in my pre-commit hook, rather than having to add a post-commit hook only to perform a “git add” after the fact (which the repro script emulates); it seems like an ugly workaround to me.

I have had similar issues before, which Jeff King kindly resolved here:
http://thread.gmane.org/gmane.comp.version-control.git/263319/focus=263323

The provided solution does not seem to help in this new context, or at least I cannot make sense of it in relation to this issue, with my limited knowledge of Git internals.

Any insight would be appreciated!

Thanks in advance,

Michaël Fortin
www.irradiated.net

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]