{"thread":{"id":"22031","subject":"[PATCH] t4019 \"grep\" portability fix","startedAt":"2009-12-26T21:53:17Z","lastAt":"2009-12-26T21:53:17Z","messageCount":1,"participants":["Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"130352","messageId":"7vvdftxv02.fsf@alter.siamese.dyndns.org","threadId":"22031","inReplyTo":null,"subject":"[PATCH] t4019 \"grep\" portability fix","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2009-12-26T21:53:17Z","receivedAt":"2009-12-26T21:53:17Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Input to \"grep\" is supposed to be \"text\", but we deliberately feed output\nfrom \"git diff --color\" to sift it into two sets of lines (ones with\nerrors, the other without).  Some implementations of \"grep\" only report\nmatches with the exit status, without showing the matched lines in their\noutput (e.g. OpenBSD 4.6, which says \"Binary file .. matches\").\n\nFortunately, \"grep -a\" is often a way to force the command to treat its\ninput as text.\n\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n t/t4019-diff-wserror.sh |   56 +++++++++++++++++++++++-----------------------\n 1 files changed, 28 insertions(+), 28 deletions(-)\n\ndiff --git a/t/t4019-diff-wserror.sh b/t/t4019-diff-wserror.sh\nindex 3a3663f..f6d1f1e 100755\n--- a/t/t4019-diff-wserror.sh\n+++ b/t/t4019-diff-wserror.sh\n@@ -20,11 +20,27 @@ test_expect_success setup '\n \n blue_grep='7;34m' ;# ESC [ 7 ; 3 4 m\n \n+printf \"\\033[%s\" \"$blue_grep\" >check-grep\n+if (grep \"$blue_grep\" <check-grep | grep \"$blue_grep\") >/dev/null 2>&1\n+then\n+\tgrep_a=grep\n+elif (grep -a \"$blue_grep\" <check-grep | grep -a \"$blue_grep\") >/dev/null 2>&1\n+then\n+\tgrep_a='grep -a'\n+else\n+\tgrep_a=grep ;# expected to fail...\n+fi\n+rm -f check-grep\n+\n+prepare_output () {\n+\tgit diff --color >output\n+\t$grep_a \"$blue_grep\" output >error\n+\t$grep_a -v \"$blue_grep\" output >normal\n+}\n+\n test_expect_success default '\n \n-\tgit diff --color >output\n-\tgrep \"$blue_grep\" output >error\n-\tgrep -v \"$blue_grep\" output >normal\n+\tprepare_output\n \n \tgrep Eight normal >/dev/null &&\n \tgrep HT error >/dev/null &&\n@@ -37,9 +53,7 @@ test_expect_success default '\n test_expect_success 'without -trail' '\n \n \tgit config core.whitespace -trail\n-\tgit diff --color >output\n-\tgrep \"$blue_grep\" output >error\n-\tgrep -v \"$blue_grep\" output >normal\n+\tprepare_output\n \n \tgrep Eight normal >/dev/null &&\n \tgrep HT error >/dev/null &&\n@@ -53,9 +67,7 @@ test_expect_success 'without -trail (attribute)' '\n \n \tgit config --unset core.whitespace\n \techo \"F whitespace=-trail\" >.gitattributes\n-\tgit diff --color >output\n-\tgrep \"$blue_grep\" output >error\n-\tgrep -v \"$blue_grep\" output >normal\n+\tprepare_output\n \n \tgrep Eight normal >/dev/null &&\n \tgrep HT error >/dev/null &&\n@@ -69,9 +81,7 @@ test_expect_success 'without -space' '\n \n \trm -f .gitattributes\n \tgit config core.whitespace -space\n-\tgit diff --color >output\n-\tgrep \"$blue_grep\" output >error\n-\tgrep -v \"$blue_grep\" output >normal\n+\tprepare_output\n \n \tgrep Eight normal >/dev/null &&\n \tgrep HT normal >/dev/null &&\n@@ -85,9 +95,7 @@ test_expect_success 'without -space (attribute)' '\n \n \tgit config --unset core.whitespace\n \techo \"F whitespace=-space\" >.gitattributes\n-\tgit diff --color >output\n-\tgrep \"$blue_grep\" output >error\n-\tgrep -v \"$blue_grep\" output >normal\n+\tprepare_output\n \n \tgrep Eight normal >/dev/null &&\n \tgrep HT normal >/dev/null &&\n@@ -101,9 +109,7 @@ test_expect_success 'with indent-non-tab only' '\n \n \trm -f .gitattributes\n \tgit config core.whitespace indent,-trailing,-space\n-\tgit diff --color >output\n-\tgrep \"$blue_grep\" output >error\n-\tgrep -v \"$blue_grep\" output >normal\n+\tprepare_output\n \n \tgrep Eight error >/dev/null &&\n \tgrep HT normal >/dev/null &&\n@@ -117,9 +123,7 @@ test_expect_success 'with indent-non-tab only (attribute)' '\n \n \tgit config --unset core.whitespace\n \techo \"F whitespace=indent,-trailing,-space\" >.gitattributes\n-\tgit diff --color >output\n-\tgrep \"$blue_grep\" output >error\n-\tgrep -v \"$blue_grep\" output >normal\n+\tprepare_output\n \n \tgrep Eight error >/dev/null &&\n \tgrep HT normal >/dev/null &&\n@@ -133,9 +137,7 @@ test_expect_success 'with cr-at-eol' '\n \n \trm -f .gitattributes\n \tgit config core.whitespace cr-at-eol\n-\tgit diff --color >output\n-\tgrep \"$blue_grep\" output >error\n-\tgrep -v \"$blue_grep\" output >normal\n+\tprepare_output\n \n \tgrep Eight normal >/dev/null &&\n \tgrep HT error >/dev/null &&\n@@ -149,9 +151,7 @@ test_expect_success 'with cr-at-eol (attribute)' '\n \n \tgit config --unset core.whitespace\n \techo \"F whitespace=trailing,cr-at-eol\" >.gitattributes\n-\tgit diff --color >output\n-\tgrep \"$blue_grep\" output >error\n-\tgrep -v \"$blue_grep\" output >normal\n+\tprepare_output\n \n \tgrep Eight normal >/dev/null &&\n \tgrep HT error >/dev/null &&\n@@ -195,7 +195,7 @@ test_expect_success 'color new trailing blank lines' '\n \tgit add x &&\n \t{ echo a; echo; echo; echo; echo c; echo; echo; echo; echo; } >x &&\n \tgit diff --color x >output &&\n-\tcnt=$(grep \"${blue_grep}\" output | wc -l) &&\n+\tcnt=$($grep_a \"${blue_grep}\" output | wc -l) &&\n \ttest $cnt = 2\n '\n \n"}]}