{"thread":{"id":"6896","subject":"[BUG] diff -B does not (always?) use colors","startedAt":"2007-02-20T10:08:46Z","lastAt":"2007-02-20T18:35:03Z","messageCount":4,"participants":["Matthias Lederhofer","Johannes Schindelin","Junio C Hamano"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"35105","messageId":"20070220100846.GA7928@moooo.ath.cx","threadId":"6896","inReplyTo":null,"subject":"[BUG] diff -B does not (always?) use colors","fromName":"Matthias Lederhofer","fromEmail":"matled@gmx.net","sentAt":"2007-02-20T10:08:46Z","receivedAt":"2007-02-20T10:08:46Z","isPatch":false,"sender":{"key":"matled@gmx.net","avatar":null},"body":"I found that git diff -B does not always use colors and I don't have\ntime to figure out what it is atm.\n\nThe bug can be triggered by this two files and running git diff with\n-B.  You can also just clone the repository from\ngit://igit.ath.cx/~matled/tmp/break/\n\n% git cat-file -p HEAD~1:test\ndef test(p)\n    if p\n        Array.new\n        Array.new(2)\n        Array.new(5, \"A\")\n\n        # only one copy of the object is created\n        a = Array.new(2, Hash.new)\n        a[0]['cat'] = 'feline'\n        a\n        a[1]['cat'] = 'Felix'\n        a\n\n        # here multiple copies are created\n        a = Array.new(2) { Hash.new }\n        a[0]['cat'] = 'feline'\n        a\n\n        squares = Array.new(5) {|i| i*i}\n        squares\n\n        copy = Array.new(squares)\n    end\nend\n% git cat-file -p HEAD:test  \ndef test(p)\n    test_bla if p\nend\n\ndef test_bla\n    Array.new\n    Array.new(2)\n    Array.new(5, \"A\")\n\n    # only one copy of the object is created\n    a = Array.new(2, Hash.new)\n    a[0]['cat'] = 'feline'\n    a\n    a[1]['cat'] = 'Felix'\n    a\n\n    # here multiple copies are created\n    a = Array.new(2) { Hash.new }\n    a[0]['cat'] = 'feline'\n    a\n\n    squares = Array.new(5) {|i| i*i}\n    squares\n\n    copy = Array.new(squares)\nend\n"},{"id":"35119","messageId":"Pine.LNX.4.63.0702201506551.22628@wbgn013.biozentrum.uni-wuerzburg.de","threadId":"6896","inReplyTo":"20070220100846.GA7928@moooo.ath.cx","subject":"[PATCH] Teach diff -B about colours","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2007-02-20T14:08:46Z","receivedAt":"2007-02-20T14:08:46Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"\nMatthias Lederhofer noticed that `diff -B` did not pick up on diff \ncolournig.\n\nSigned-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>\n---\n\n\tOn Tue, 20 Feb 2007, Matthias Lederhofer wrote:\n\n\t> I found that git diff -B does not always use colors and I don't \n\t> have time to figure out what it is atm.\n\t> \n\t> The bug can be triggered by this two files and running git diff \n\t> with -B.\n\n\tThanks to this excellent bug report, it was very easy to find and \n\tfix that bug.\n\n diff.c |   39 +++++++++++++++++++++++++--------------\n 1 files changed, 25 insertions(+), 14 deletions(-)\n\ndiff --git a/diff.c b/diff.c\nindex 6f8b742..5094dcd 100644\n--- a/diff.c\n+++ b/diff.c\n@@ -184,30 +184,40 @@ static void print_line_count(int count)\n \t}\n }\n \n-static void copy_file(int prefix, const char *data, int size)\n+static void copy_file(int prefix, const char *data, int size,\n+\t\tconst char *set, const char *reset)\n {\n \tint ch, nl_just_seen = 1;\n \twhile (0 < size--) {\n \t\tch = *data++;\n-\t\tif (nl_just_seen)\n+\t\tif (nl_just_seen) {\n+\t\t\tfputs(set, stdout);\n \t\t\tputchar(prefix);\n-\t\tputchar(ch);\n-\t\tif (ch == '\\n')\n+\t\t}\n+\t\tif (ch == '\\n') {\n \t\t\tnl_just_seen = 1;\n-\t\telse\n+\t\t\tfputs(reset, stdout);\n+\t\t} else\n \t\t\tnl_just_seen = 0;\n+\t\tputchar(ch);\n \t}\n \tif (!nl_just_seen)\n-\t\tprintf(\"\\n\\\\ No newline at end of file\\n\");\n+\t\tprintf(\"%s\\n\\\\ No newline at end of file\\n\", reset);\n }\n \n static void emit_rewrite_diff(const char *name_a,\n \t\t\t      const char *name_b,\n \t\t\t      struct diff_filespec *one,\n-\t\t\t      struct diff_filespec *two)\n+\t\t\t      struct diff_filespec *two,\n+\t\t\t      int color_diff)\n {\n \tint lc_a, lc_b;\n \tconst char *name_a_tab, *name_b_tab;\n+\tconst char *metainfo = diff_get_color(color_diff, DIFF_METAINFO);\n+\tconst char *fraginfo = diff_get_color(color_diff, DIFF_FRAGINFO);\n+\tconst char *old = diff_get_color(color_diff, DIFF_FILE_OLD);\n+\tconst char *new = diff_get_color(color_diff, DIFF_FILE_NEW);\n+\tconst char *reset = diff_get_color(color_diff, DIFF_RESET);\n \n \tname_a_tab = strchr(name_a, ' ') ? \"\\t\" : \"\";\n \tname_b_tab = strchr(name_b, ' ') ? \"\\t\" : \"\";\n@@ -216,17 +226,17 @@ static void emit_rewrite_diff(const char *name_a,\n \tdiff_populate_filespec(two, 0);\n \tlc_a = count_lines(one->data, one->size);\n \tlc_b = count_lines(two->data, two->size);\n-\tprintf(\"--- a/%s%s\\n+++ b/%s%s\\n@@ -\",\n-\t       name_a, name_a_tab,\n-\t       name_b, name_b_tab);\n+\tprintf(\"%s--- a/%s%s%s\\n%s+++ b/%s%s%s\\n%s@@ -\",\n+\t       metainfo, name_a, name_a_tab, reset,\n+\t       metainfo, name_b, name_b_tab, reset, fraginfo);\n \tprint_line_count(lc_a);\n \tprintf(\" +\");\n \tprint_line_count(lc_b);\n-\tprintf(\" @@\\n\");\n+\tprintf(\" @@%s\\n\", reset);\n \tif (lc_a)\n-\t\tcopy_file('-', one->data, one->size);\n+\t\tcopy_file('-', one->data, one->size, old, reset);\n \tif (lc_b)\n-\t\tcopy_file('+', two->data, two->size);\n+\t\tcopy_file('+', two->data, two->size, new, reset);\n }\n \n static int fill_mmfile(mmfile_t *mf, struct diff_filespec *one)\n@@ -1084,7 +1094,8 @@ static void builtin_diff(const char *name_a,\n \t\tif ((one->mode ^ two->mode) & S_IFMT)\n \t\t\tgoto free_ab_and_return;\n \t\tif (complete_rewrite) {\n-\t\t\temit_rewrite_diff(name_a, name_b, one, two);\n+\t\t\temit_rewrite_diff(name_a, name_b, one, two,\n+\t\t\t\t\to->color_diff);\n \t\t\tgoto free_ab_and_return;\n \t\t}\n \t}\n-- \n1.5.0.1.2160.g5bd9-dirty\n"},{"id":"35131","messageId":"7vodnovgcs.fsf@assigned-by-dhcp.cox.net","threadId":"6896","inReplyTo":"Pine.LNX.4.63.0702201506551.22628@wbgn013.biozentrum.uni-wuerzburg.de","subject":"Re: [PATCH] Teach diff -B about colours","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2007-02-20T18:22:43Z","receivedAt":"2007-02-20T18:22:43Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:\n\n> Matthias Lederhofer noticed that `diff -B` did not pick up on diff \n> colournig.\n\nsp.?\n\n> -static void copy_file(int prefix, const char *data, int size)\n> +static void copy_file(int prefix, const char *data, int size,\n> +\t\tconst char *set, const char *reset)\n> ...\n>  \tif (!nl_just_seen)\n> -\t\tprintf(\"\\n\\\\ No newline at end of file\\n\");\n> +\t\tprintf(\"%s\\n\\\\ No newline at end of file\\n\", reset);\n>  }\n\nAre you sure about this one?  If preimage lacked terminating LF and\nthe postimage has it, then don't you want to see \"\\ No...\" as an\naddition?\n"},{"id":"35132","messageId":"Pine.LNX.4.63.0702201932100.22628@wbgn013.biozentrum.uni-wuerzburg.de","threadId":"6896","inReplyTo":"7vodnovgcs.fsf@assigned-by-dhcp.cox.net","subject":"Re: [PATCH] Teach diff -B about colours","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2007-02-20T18:35:03Z","receivedAt":"2007-02-20T18:35:03Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi,\n\nOn Tue, 20 Feb 2007, Junio C Hamano wrote:\n\n> Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:\n> \n> > Matthias Lederhofer noticed that `diff -B` did not pick up on diff \n> > colournig.\n> \n> sp.?\n\ns/colournig/colouring/\n\n> > -static void copy_file(int prefix, const char *data, int size)\n> > +static void copy_file(int prefix, const char *data, int size,\n> > +\t\tconst char *set, const char *reset)\n> > ...\n> >  \tif (!nl_just_seen)\n> > -\t\tprintf(\"\\n\\\\ No newline at end of file\\n\");\n> > +\t\tprintf(\"%s\\n\\\\ No newline at end of file\\n\", reset);\n> >  }\n> \n> Are you sure about this one?  If preimage lacked terminating LF and the \n> postimage has it, then don't you want to see \"\\ No...\" as an addition?\n\nYes, you're right. But I tried to imitate what git-diff does without -B.\n\nCiao,\nDscho\n"}]}