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

Re: [PATCH 22/25] diff.c: color moved lines differently

From
Junio C Hamano <gitster@pobox.com>
Date
Jun 30, 2017, 17:54 UTC
Message-ID
<xmqqd19ls6no.fsf@gitster.mtv.corp.google.com>
In-Reply-To
<20170630000710.10601-23-sbeller@google.com>
Stefan Beller <sbeller@google.com> writes:
Show 9 quoted lines
> +static int next_byte(const char **cp, const char **endp,
> +		     const struct diff_options *diffopt)
> +{
> +	int retval;
> +
> +	if (DIFF_XDL_TST(diffopt, IGNORE_WHITESPACE_AT_EOL)) {
> +		while (*endp > *cp && isspace(**endp))
> +			(*endp)--;
> +	}

This should be done by the callers (both moved_entry_cmp() and get_string_hash()) before starting to iterate over the bytes from the beginning, no?

Show 20 quoted lines
> +	if (*cp > *endp)
> +		return -1;
> +
> +	if (DIFF_XDL_TST(diffopt, IGNORE_WHITESPACE_CHANGE)) {
> +		while (*cp < *endp && isspace(**cp))
> +			(*cp)++;
> +		/*
> +		 * After skipping a couple of whitespaces, we still have to
> +		 * account for one space.
> +		 */
> +		return (int)' ';
> +	}
> +
> +	if (DIFF_XDL_TST(diffopt, IGNORE_WHITESPACE)) {
> +		while (*cp < *endp && isspace(**cp))
> +			(*cp)++;
> +		/* return the first non-ws character via the usual below */
> +	}
> +
> +	retval = **cp;

The char could be signed, and byte 0xff may become indistinguishable from the EOF (i.e. -1) you returned earlier.

Show 23 quoted lines
> +	(*cp)++; /* advance */
> +	return retval;
> +}
> +
> +static int moved_entry_cmp(const struct moved_entry *a,
> +			   const struct moved_entry *b,
> +			   const void *keydata,
> +			   const void *data)
> +{
> +	const struct diff_options *diffopt = data;
> +	const char *ap = a->es->line, *ae = a->es->line + a->es->len;
> +	const char *bp = b->es->line, *be = b->es->line + b->es->len;
> +
> +	if (!(diffopt->xdl_opts & XDF_WHITESPACE_FLAGS))
> +		return a->es->len != b->es->len  || memcmp(ap, bp, a->es->len);
> +
> +	while (1) {
> +		int ca, cb;
> +		ca = next_byte(&ap, &ae, diffopt);
> +		cb = next_byte(&bp, &be, diffopt);
> +		if (ca != cb)
> +			return 1; /* differs */
> +		if (!ca)

Shouldn't this check for "ca == -1", as we are not dealing with NUL terminated string but a <ptr, len> thing?

Show 20 quoted lines
> +			return 0;
> +	};
> +}
> +
> +static unsigned get_string_hash(struct emitted_diff_symbol *es, struct diff_options *o)
> +{
> +	if (o->xdl_opts & XDF_WHITESPACE_FLAGS) {
> +		static struct strbuf sb = STRBUF_INIT;
> +		const char *ap = es->line, *ae = es->line + es->len;
> +		int c;
> +
> +		strbuf_reset(&sb);
> +		while ((c = next_byte(&ap, &ae, o)) > 0)
> +			strbuf_addch(&sb, c);
> +
> +		return memhash(sb.buf, sb.len);
> +	} else {
> +		return memhash(es->line, es->len);
> +	}
> +}
Previous: Stefan BellerNext: Stefan Beller
Message 15 of 63 in “Reroll of sb/diff-color-moved.”
  1. 00/25 Reroll of sb/diff-color-moved.Stefan Beller, Jun 30, 2017
  2. 02/25 diff.c: move line ending check into emit_hunk_headerStefan Beller, Jun 30, 2017
  3. 12/25 diff.c: emit_diff_symbol learns DIFF_SYMBOL_HEADERStefan Beller, Jun 30, 2017
  4. 04/25 diff.c: introduce emit_diff_symbolStefan Beller, Jun 30, 2017
  5. 06/25 diff.c: emit_diff_symbol learns DIFF_SYMBOL_CONTEXT_FRAGINFOStefan Beller, Jun 30, 2017
  6. 01/25 diff.c: readability fixStefan Beller, Jun 30, 2017
  7. 05/25 diff.c: emit_diff_symbol learns DIFF_SYMBOL_CONTEXT_MARKERStefan Beller, Jun 30, 2017
  8. 03/25 diff.c: factor out diff_flush_patch_all_file_pairsStefan Beller, Jun 30, 2017
  9. 07/25 diff.c: emit_diff_symbol learns DIFF_SYMBOL_NO_LF_EOFStefan Beller, Jun 30, 2017
  10. 18/25 diff.c: convert word diffing to use emit_diff_symbolStefan Beller, Jun 30, 2017
  11. 13/25 diff.c: emit_diff_symbol learns about DIFF_SYMBOL_BINARY_FILESStefan Beller, Jun 30, 2017
  12. 21/25 diff.c: buffer all output if asked toStefan Beller, Jun 30, 2017
  13. 20/25 diff.c: emit_diff_symbol learns about DIFF_SYMBOL_SUMMARYStefan Beller, Jun 30, 2017
  14. 22/25 diff.c: color moved lines differentlyStefan Beller, Jun 30, 2017
  15. Junio C HamanoJun 30, 2017
  16. Stefan BellerJun 30, 2017
  17. Junio C HamanoJun 30, 2017
  18. 24/25 diff.c: add dimming to moved line detectionStefan Beller, Jun 30, 2017
  19. 25/25 diff: document the new --color-moved settingStefan Beller, Jun 30, 2017
  20. Simon RuderichJun 30, 2017
  21. Stefan BellerJun 30, 2017
  22. Simon RuderichJun 30, 2017
  23. Stefan BellerJun 30, 2017
  24. 19/25 diff.c: emit_diff_symbol learns about DIFF_SYMBOL_STAT_SEPStefan Beller, Jun 30, 2017
  25. 11/25 diff.c: emit_diff_symbol learns DIFF_SYMBOL_FILEPAIR_{PLUS, MINUS}Stefan Beller, Jun 30, 2017
  26. 23/25 diff.c: color moved lines differently, plain modeStefan Beller, Jun 30, 2017
  27. 15/25 submodule.c: migrate diff output to use emit_diff_symbolStefan Beller, Jun 30, 2017
  28. 14/25 diff.c: emit_diff_symbol learns DIFF_SYMBOL_REWRITE_DIFFStefan Beller, Jun 30, 2017
  29. 16/25 diff.c: convert emit_binary_diff_body to use emit_diff_symbolStefan Beller, Jun 30, 2017
  30. 10/25 diff.c: emit_diff_symbol learns DIFF_SYMBOL_CONTEXT_INCOMPLETEStefan Beller, Jun 30, 2017
  31. 17/25 diff.c: convert show_stats to use emit_diff_symbolStefan Beller, Jun 30, 2017
  32. 09/25 diff.c: emit_diff_symbol learns DIFF_SYMBOL_WORDS[_PORCELAIN]Stefan Beller, Jun 30, 2017
  33. 08/25 diff.c: migrate emit_line_checked to use emit_diff_symbolStefan Beller, Jun 30, 2017
  34. 00/25 Reroll of sb/diff-color-moved.Stefan Beller, Jun 30, 2017
  35. 01/25 diff.c: readability fixStefan Beller, Jun 30, 2017
  36. 02/25 diff.c: move line ending check into emit_hunk_headerStefan Beller, Jun 30, 2017
  37. 06/25 diff.c: emit_diff_symbol learns DIFF_SYMBOL_CONTEXT_FRAGINFOStefan Beller, Jun 30, 2017
  38. 07/25 diff.c: emit_diff_symbol learns DIFF_SYMBOL_NO_LF_EOFStefan Beller, Jun 30, 2017
  39. 05/25 diff.c: emit_diff_symbol learns DIFF_SYMBOL_CONTEXT_MARKERStefan Beller, Jun 30, 2017
  40. 12/25 diff.c: emit_diff_symbol learns DIFF_SYMBOL_HEADERStefan Beller, Jun 30, 2017
  41. 13/25 diff.c: emit_diff_symbol learns about DIFF_SYMBOL_BINARY_FILESStefan Beller, Jun 30, 2017
  42. 14/25 diff.c: emit_diff_symbol learns DIFF_SYMBOL_REWRITE_DIFFStefan Beller, Jun 30, 2017
  43. 09/25 diff.c: emit_diff_symbol learns DIFF_SYMBOL_WORDS[_PORCELAIN]Stefan Beller, Jun 30, 2017
  44. 20/25 diff.c: emit_diff_symbol learns about DIFF_SYMBOL_SUMMARYStefan Beller, Jun 30, 2017
  45. 18/25 diff.c: convert word diffing to use emit_diff_symbolStefan Beller, Jun 30, 2017
  46. 17/25 diff.c: convert show_stats to use emit_diff_symbolStefan Beller, Jun 30, 2017
  47. 15/25 submodule.c: migrate diff output to use emit_diff_symbolStefan Beller, Jun 30, 2017
  48. 21/25 diff.c: buffer all output if asked toStefan Beller, Jun 30, 2017
  49. 25/25 diff: document the new --color-moved settingStefan Beller, Jun 30, 2017
  50. 23/25 diff.c: color moved lines differently, plain modeStefan Beller, Jun 30, 2017
  51. 22/25 diff.c: color moved lines differentlyStefan Beller, Jun 30, 2017
  52. Junio C HamanoJun 30, 2017
  53. Stefan BellerJun 30, 2017
  54. Junio C HamanoJun 30, 2017
  55. 24/25 diff.c: add dimming to moved line detectionStefan Beller, Jun 30, 2017
  56. 19/25 diff.c: emit_diff_symbol learns about DIFF_SYMBOL_STAT_SEPStefan Beller, Jun 30, 2017
  57. 16/25 diff.c: convert emit_binary_diff_body to use emit_diff_symbolStefan Beller, Jun 30, 2017
  58. 03/25 diff.c: factor out diff_flush_patch_all_file_pairsStefan Beller, Jun 30, 2017
  59. 11/25 diff.c: emit_diff_symbol learns DIFF_SYMBOL_FILEPAIR_{PLUS, MINUS}Stefan Beller, Jun 30, 2017
  60. 10/25 diff.c: emit_diff_symbol learns DIFF_SYMBOL_CONTEXT_INCOMPLETEStefan Beller, Jun 30, 2017
  61. 08/25 diff.c: migrate emit_line_checked to use emit_diff_symbolStefan Beller, Jun 30, 2017
  62. 04/25 diff.c: introduce emit_diff_symbolStefan Beller, Jun 30, 2017
  63. Junio C HamanoJun 30, 2017

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.