git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Bert Wesarg <bert.wesarg@googlemail.com>
To: Junio C Hamano <gitster@pobox.com>
Cc: Jeff King <peff@peff.net>, git@vger.kernel.org
Subject: Re: [PATCH v3] Give the hunk comment its own color
Date: Sat, 28 Nov 2009 13:08:20 +0100	[thread overview]
Message-ID: <36ca99e90911280408v186777f1h22254744fb61bf1f@mail.gmail.com> (raw)
In-Reply-To: <7vhbsfi4bz.fsf@alter.siamese.dyndns.org>

On Sat, Nov 28, 2009 at 06:52, Junio C Hamano <gitster@pobox.com> wrote:
> 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.

>
>> +     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.

>
> 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
>
>

  reply	other threads:[~2009-11-28 12:08 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-11-18 11:30 [PATCH] Give the hunk comment its own color Bert Wesarg
2009-11-18 11:44 ` Tay Ray Chuan
2009-11-18 11:57   ` Bert Wesarg
2009-11-18 14:23 ` Jeff King
2009-11-18 15:16   ` Bert Wesarg
2009-11-18 21:56   ` Junio C Hamano
2009-11-18 22:44     ` Jeff King
2009-11-26 12:05     ` Bert Wesarg
2009-11-27  2:38       ` Junio C Hamano
2009-11-27  6:29         ` Bert Wesarg
2009-11-27  6:52         ` Jeff King
2009-11-27  7:27           ` Junio C Hamano
2009-11-27  6:55         ` [PATCH v3] " Bert Wesarg
2009-11-27  8:38           ` Junio C Hamano
2009-11-27  8:44             ` Bert Wesarg
2009-11-27  8:59               ` Junio C Hamano
2009-11-28  5:52           ` Junio C Hamano
2009-11-28 12:08             ` Bert Wesarg [this message]
2009-11-30  7:07               ` Bert Wesarg
2009-11-30  7:15                 ` Junio C Hamano
2009-11-30  7:41                   ` Bert Wesarg
2009-11-30  7:47                     ` Junio C Hamano
2009-11-30  8:09                       ` Sverre Rabbelier
2009-11-30  9:00                         ` Junio C Hamano
2009-11-30  9:26                           ` Sverre Rabbelier
2009-11-18 15:11 ` [PATCH v2] " Bert Wesarg
2009-11-18 15:17   ` Jason Sewall
2009-11-18 15:20     ` Bert Wesarg

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=36ca99e90911280408v186777f1h22254744fb61bf1f@mail.gmail.com \
    --to=bert.wesarg@googlemail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=peff@peff.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).