git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
Michaël Fortin <fortinmike@gmail.com>
Date
May 4, 2015, 12:33 UTC
Message-ID
<463FC822-916F-4160-A1F2-B4AAEFF3A5B2@gmail.com>
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

Next: Junio C Hamano
Message 1 of 2 in “Unexpected behavior when git-adding files in a pre-commit hook then using "git commit -o"”
  1. Michaël FortinMay 4, 2015
  2. Junio C HamanoMay 4, 2015

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.