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

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

From
Junio C Hamano <gitster@pobox.com>
Date
Jun 30, 2017, 21:11 UTC
Message-ID
<xmqq1sq1qiyg.fsf@gitster.mtv.corp.google.com>
In-Reply-To
<20170630205310.7380-23-sbeller@google.com>
Stefan Beller <sbeller@google.com> writes:
Show 16 quoted lines
> +static int next_byte(const char **cp, const char **endp,
> +		     const struct diff_options *diffopt)
> +{
> +	int retval;
> +
> +	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)' ';
Do we need a cast here?
Show 10 quoted lines
> +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 (ae > ap && isspace(*ae))
> +			ae--;
Not testing for the AT_EOL option here?  

It does not make a difference in correctness; two lines that differ only by their trailing whitespaces will be hashed into the same bin even when we are not using ignore-whitespace-at-eol, making the hashmap a bit less efficient than necessary.

By the way, this is an unrelated tangent because I think you inherited this pattern by copying and pasting from elsewhere, but I think it would be better if we avoid casting the function pointer type like this:

Show 7 quoted lines
> +		if (o->color_moved) {
> +			struct hashmap add_lines, del_lines;
> +
> +			hashmap_init(&del_lines,
> +				     (hashmap_cmp_fn)moved_entry_cmp, o, 0);
> +			hashmap_init(&add_lines,
> +				     (hashmap_cmp_fn)moved_entry_cmp, o, 0);

When hashmap_cmp_fn's definition changes, these two calling sites won't be caught as passing a incorrectly typed callback function by the compiler.

Instead, we can match the actual implementation of the callback function, e.g.

Show 5 quoted lines
> +static int moved_entry_cmp(const struct diff_options *diffopt,
> +			   const struct moved_entry *a,
> +			   const struct moved_entry *b,
> +			   const void *keydata)
> +{
to the expected function type, i.e.
static int moved_entry_cmp(const void *fndata,
			   const void *entry, const void *entry_or_key,
			   const void *keydata)
{
	const struct diff_options *diffopt = fndata;
	const struct moved_entry *a = entry;
	const struct moved_entry *b = entry_or_key;
	...
by casting the parameters.
Previous: Stefan BellerNext: Stefan Beller
Message 52 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.