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

Re: [PATCH v3] Give the hunk comment its own color

From
Bert Wesarg <bert.wesarg@googlemail.com>
Date
Nov 28, 2009, 12:08 UTC
Message-ID
<36ca99e90911280408v186777f1h22254744fb61bf1f@mail.gmail.com>
In-Reply-To
<7vhbsfi4bz.fsf@alter.siamese.dyndns.org>
On Sat, Nov 28, 2009 at 06:52, Junio C Hamano <gitster@pobox.com> wrote:
Show 37 quoted lines
> Bert Wesarg <bert.wesarg@googlemail.com> writes:
>
>>  diff.c                   |   64 +++++++++++++++++++++++++++++++++++++++++++--
>> ...
>> @@ -344,6 +347,63 @@ static void emit_add_line(const char *reset,
>>       }
>>  }
>>
>> +static void emit_hunk_line(struct emit_callback *ecbdata,
>> +                        const char *line, int len)
>> +{
>> +     const char *plain = diff_get_color(ecbdata->color_diff, DIFF_PLAIN);
>> +     const char *frag = diff_get_color(ecbdata->color_diff, DIFF_FRAGINFO);
>> +     const char *func = diff_get_color(ecbdata->color_diff, DIFF_FUNCINFO);
>> +     const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET);
>> +     const char *orig_line = line;
>> +     int orig_len = len;
>> +     const char *frag_start;
>> +     int frag_len;
>> +     const char *part_end = NULL;
>> +     int part_len = 0;
>> +
>> +     /* determine length of @ */
>> +     while (part_len < len && line[part_len] == '@')
>> +             part_len++;
>> +
>> +     /* find end of frag, (Ie. find second @@) */
>> +     part_end = memmem(line + part_len, len - part_len,
>> +                       line, part_len);
>
> This is not incorrect per-se, but probably is overkill; this codepath only
> deals with two-way diff and we know we are looking at "@@ -..., +... @@"
> at this point.
>
>        part_end = memmem(line + 2, len - 2, "@@", 2);
>
> would be sufficient.
Thats right, I made it generic by purpose.
Show 22 quoted lines
>
>> +     if (!part_end)
>> +             return emit_line(ecbdata->file, frag, reset, line, len);
>> +     /* calculate total length of frag */
>> +     part_len = (part_end + part_len) - line;
>> +
>> +     /* remember frag part, we emit only if we find a space separator */
>> +     frag_start = line;
>> +     frag_len = part_len;
>> +
>> +     /* consume hunk header */
>> +     len -= part_len;
>> +     line += part_len;
>> +
>> +     /*
>> +      * for empty reminder or empty space sequence (exclusive any newlines
>> +      * or carriage returns) emit complete original line as FRAGINFO
>> +      */
>> +     if (!len || !(part_len = strspn(line, " \t")))
>
> Slightly worrisome is what guarantees this strspn() won't step outside
> len.
Thats a valid concern and should be addressed.
Show 40 quoted lines
>
> I would probably write the function like this instead.
>
> -- >8 --
>
> static void emit_hunk_header(struct emit_callback *ecbdata,
>                             const char *line, int len)
> {
>        const char *plain = diff_get_color(ecbdata->color_diff, DIFF_PLAIN);
>        const char *frag = diff_get_color(ecbdata->color_diff, DIFF_FRAGINFO);
>        const char *func = diff_get_color(ecbdata->color_diff, DIFF_FUNCINFO);
>        const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET);
>        static const char atat[2] = { '@', '@' };
>        const char *cp, *ep;
>
>        /*
>         * As a hunk header must begin with "@@ -<old>, +<new> @@",
>         * it always is at least 10 bytes long.
>         */
>        if (len < 10 ||
>            memcmp(line, atat, 2) ||
>            !(ep = memmem(line + 2, len - 2, atat, 2))) {
>                emit_line(ecbdata->file, plain, reset, line, len);
>                return;
>        }
>        ep += 2; /* skip over the second @@ */
>
>        /* The hunk header in fraginfo color */
>        emit_line(ecbdata->file, frag, reset, line, ep - line);
>
>        /* blank before the func header */
>        for (cp = ep; ep - line < len; ep++)
>                if (*ep != ' ' && *ep != 't')
>                        break;
>        if (ep != cp)
>                emit_line(ecbdata->file, plain, reset, cp, ep - cp);
>
>        if (ep < line + len)
>                emit_line(ecbdata->file, func, reset, ep, line + len - ep);
> }

Please check that its really an *ep != '\t'. Its wrong in this mail, I see only an *ep != 't'. Otherwise:

Acked-by: Bert.Wesarg@googlemail.com
>
>
Previous: Junio C HamanoNext: Bert Wesarg
Message 18 of 28 in “Give the hunk comment its own color”
  1. Give the hunk comment its own colorBert Wesarg, Nov 18, 2009
  2. Tay Ray ChuanNov 18, 2009
  3. Bert WesargNov 18, 2009
  4. Jeff KingNov 18, 2009
  5. Bert WesargNov 18, 2009
  6. Junio C HamanoNov 18, 2009
  7. Jeff KingNov 18, 2009
  8. Bert WesargNov 26, 2009
  9. Junio C HamanoNov 27, 2009
  10. Bert WesargNov 27, 2009
  11. Jeff KingNov 27, 2009
  12. Junio C HamanoNov 27, 2009
  13. Give the hunk comment its own colorBert Wesarg, Nov 27, 2009
  14. Junio C HamanoNov 27, 2009
  15. Bert WesargNov 27, 2009
  16. Junio C HamanoNov 27, 2009
  17. Junio C HamanoNov 28, 2009
  18. Bert WesargNov 28, 2009
  19. Bert WesargNov 30, 2009
  20. Junio C HamanoNov 30, 2009
  21. Bert WesargNov 30, 2009
  22. Junio C HamanoNov 30, 2009
  23. Sverre RabbelierNov 30, 2009
  24. Junio C HamanoNov 30, 2009
  25. Sverre RabbelierNov 30, 2009
  26. Give the hunk comment its own colorBert Wesarg, Nov 18, 2009
  27. Jason SewallNov 18, 2009
  28. Bert WesargNov 18, 2009

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.