Re: [PATCH 07/12] diff: update the way rewrite diff handles incomplete lines
On 04/11/2025 02:09, Junio C Hamano wrote:
Show 8 quoted lines
>
> Revamp the way the complete-rewrite code path feeds the lines to the
> output layer by treating the last line of the pre/post image when it
> is an incomplete line specially.
>
> This lets us remove the DIFF_SYMBOL_NO_LF_EOF hack and use the usual
> DIFF_SYMBOL_CONTEXT_INCOMPLETE code path, which will later learn how
> to handle whitespace errors.
Show 19 quoted lines
> @@ -1786,22 +1777,36 @@ static void emit_rewrite_lines(struct emit_callback *ecbdata,
> const char *endp = NULL;
>
> while (0 < size) {
> - int len;
> + int len, plen;
> + char *pdata = NULL;
>
> endp = memchr(data, '\n', size);
> len = endp ? (endp - data + 1) : size;
> + plen = len;
> +
> + if (!endp) {
> + plen = len + 1;
> + pdata = xmalloc(plen + 2);
> + memcpy(pdata, data, len);
> + pdata[len] = '\n';
> + pdata[len + 1] = '\0';
> + }I think it would be clearer to refactor this as
endp = memchr(data, '\n', size);
- len = endp ? (endp - data + 1) : size;
if (endp) {
len = endp - data + 1;
plen = len;
} else {
len = size;
+ plen = len + 1;
+ pdata = xmalloc(plen + 2);
+ memcpy(pdata, data, len);
+ pdata[len] = '\n';
+ pdata[len + 1] = '\0';
+ }Thanks
Phillip
Show 23 quoted lines
> if (prefix != '+') {
> ecbdata->lno_in_preimage++;
> - emit_del_line(ecbdata, data, len);
> + emit_del_line(ecbdata, pdata ? pdata : data, plen);
> } else {
> ecbdata->lno_in_postimage++;
> - emit_add_line(ecbdata, data, len);
> + emit_add_line(ecbdata, pdata ? pdata : data, plen);
> }
> + free(pdata);
> size -= len;
> data += len;
> }
> - if (!endp)
> - emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_NO_LF_EOF, NULL, 0, 0);
> + if (!endp) {
> + static const char nneof[] = "\\ No newline at end of file\n";
> + ecbdata->last_line_kind = prefix;
> + emit_incomplete_line(ecbdata, nneof, sizeof(nneof) - 1);
> + }
> }
>
> static void emit_rewrite_diff(const char *name_a,