{"thread":{"id":"39234","subject":"Unexpected behavior when git-adding files in a pre-commit hook then using \"git commit -o\"","startedAt":"2015-05-04T12:33:39Z","lastAt":"2015-05-04T18:03:32Z","messageCount":2,"participants":["Michaël Fortin","Junio C Hamano"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"260513","messageId":"463FC822-916F-4160-A1F2-B4AAEFF3A5B2@gmail.com","threadId":"39234","inReplyTo":null,"subject":"Unexpected behavior when git-adding files in a pre-commit hook then using \"git commit -o\"","fromName":"Michaël Fortin","fromEmail":"fortinmike@gmail.com","sentAt":"2015-05-04T12:33:39Z","receivedAt":"2015-05-04T12:33:39Z","isPatch":false,"sender":{"key":"fortinmike@gmail.com","avatar":"https://gravatar.com/avatar/c564734f25f0c8a52dfd1cae6aaef18131f10f8a03c3d5173ac951b0d51f1afe?d=mp&s=160"},"body":"Hi,\n\nI’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:\n\n###############\necho \"----> Setup a new test repo\"\ndir_name=\"PreCommitHookAddTest\"\nrm -rf $dir_name; mkdir $dir_name; cd $dir_name\ngit init; git commit --allow-empty -m \"Initial commit\"\n\necho \"----> Add a pre-commit hook that stages a file that doesn't currently exist in the repo\"\necho \"touch auto-added; git add auto-added\" > .git/hooks/pre-commit\nchmod +x .git/hooks/pre-commit\n\necho \"----> Try committing a new file using the '-o' flag\"\ntouch manually-added; git add manually-added\ngit commit -o -m \"Commit that ran the pre-commit hook and should contain file 'auto-added'\" -- manually-added\n\necho \"----> 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)\"\ngit status\n\necho \"----> Stage the file after the fact\"\ngit add auto-added\n\necho \"----> Notice that the working copy is now clean\"\ngit status\n###############\n\nI 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.\n\nI have had similar issues before, which Jeff King kindly resolved here:\nhttp://thread.gmane.org/gmane.comp.version-control.git/263319/focus=263323\n\nThe 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.\n\nAny insight would be appreciated!\n\nThanks in advance,\n\nMichaël Fortin\nwww.irradiated.net\n"},{"id":"260537","messageId":"xmqqvbg8rzij.fsf@gitster.dls.corp.google.com","threadId":"39234","inReplyTo":"463FC822-916F-4160-A1F2-B4AAEFF3A5B2@gmail.com","subject":"Re: Unexpected behavior when git-adding files in a pre-commit hook then using \"git commit -o\"","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2015-05-04T18:03:32Z","receivedAt":"2015-05-04T18:03:32Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Michaël Fortin <fortinmike@gmail.com> writes:\n\n> I’ve encountered unexpected behavior performing a git add from a\n> pre-commit hook, followed by performing a commit using the “-o”\n> flag,...\n\nAs pre-commit is a verification (and rejection) mechanism, not a\nmechanism to tweak what is to be commited, it is totally expected\nthat its behaviour is unspecified when you modify anything in it.\n\nI think I saw a similar confusion posted to the list once in the\npast few years.  Perhaps we would need a documentation patch to\nclarify this.\n\nThanks.\n"}]}