{"thread":{"id":"12759","subject":"No whitespace check during merge conflict resolution","startedAt":"2008-03-19T21:07:18Z","lastAt":"2008-03-19T21:07:18Z","messageCount":1,"participants":["Junio C Hamano"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"72402","messageId":"7vk5jy2zjt.fsf@gitster.siamese.dyndns.org","threadId":"12759","inReplyTo":null,"subject":"No whitespace check during merge conflict resolution","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2008-03-19T21:07:18Z","receivedAt":"2008-03-19T21:07:18Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"If you are merging from somebody else, and the merge needs manual conflict\nresolution, eventually you will complete it by calling \"git commit\".\n\nNow, if the tree you are merging in introduces whitespace breakages, what\nshould we do?\n\nCurrently the situation is:\n\n - If the merge does not conflict, you do not have to complete it by\n   running \"git commit\", and whitespace breakages will be accepted as part\n   of the merge.\n\n - If you need to run \"git commit\", however, pre-commit hook can kick in.\n   The sample pre-commit hook will complain.\n\nI think the semantics of \"merge\" is \"I trust him and take his changes\";\naccepting whitespace broken tree, just like we do in non-conflicting case,\nis fine.  Even though the sample hook is, eh, just \"sample\", we should\nmake it consistent.\n\nSo perhaps something like this patch would take us in the right\ndirection.\n\nNote that this I think still has issues when amending a merge.  From\ninside hooks, I do not think you can detect that you are called to handle\namending.  Worse yet, I suspect that \"HEAD\" may not be the right thing to\ncompare with when amending, in which case further fixups (even if we do\nnot want to deal with this whitespace check issues) is required.\n\n---\ndiff --git a/templates/hooks--pre-commit b/templates/hooks--pre-commit\nindex b25dce6..10b3743 100644\n--- a/templates/hooks--pre-commit\n+++ b/templates/hooks--pre-commit\n@@ -11,6 +11,12 @@\n # Lines you introduce should not have trailing whitespace.\n # Also check for an indentation that has SP before a TAB.\n \n+in_merge=0\n+if test -f $GIT_DIR/MERGE_HEAD\n+then\n+\tin_merge=1\n+fi\n+\n if git-rev-parse --verify HEAD 2>/dev/null\n then\n \tgit-diff-index -p -M --cached HEAD --\n@@ -24,6 +30,7 @@ perl -e '\n     my $filename;\n     my $reported_filename = \"\";\n     my $lineno;\n+    my $in_merge = $ARGV[0];\n     sub bad_line {\n \tmy ($why, $line) = @_;\n \tif (!$found_bad) {\n@@ -39,7 +46,7 @@ perl -e '\n \tprint STDERR \"* $why (line $lineno)\\n\";\n \tprint STDERR \"$filename:$lineno:$line\\n\";\n     }\n-    while (<>) {\n+    while (<STDIN>) {\n \tif (m|^diff --git a/(.*) b/\\1$|) {\n \t    $filename = $1;\n \t    next;\n@@ -55,16 +62,18 @@ perl -e '\n \tif (s/^\\+//) {\n \t    $lineno++;\n \t    chomp;\n-\t    if (/\\s$/) {\n-\t\tbad_line(\"trailing whitespace\", $_);\n-\t    }\n-\t    if (/^\\s* \\t/) {\n-\t\tbad_line(\"indent SP followed by a TAB\", $_);\n+\t    if (!$in_merge) {\n+\t\tif (/\\s$/) {\n+\t\t    bad_line(\"trailing whitespace\", $_);\n+\t\t}\n+\t\tif (/^\\s* \\t/) {\n+\t\t    bad_line(\"indent SP followed by a TAB\", $_);\n+\t\t}\n \t    }\n-\t    if (/^([<>])\\1{6} |^={7}$/) {\n+\t    if ($in_merge && /^([<>])\\1{6} |^={7}$/) {\n \t\tbad_line(\"unresolved merge conflict\", $_);\n \t    }\n \t}\n     }\n     exit($found_bad);\n-'\n+' \"$in_merge\"\n"}]}