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

Re: [PATCH v3 2/2] diff: teach diff to read algorithm from diff driver

From
Junio C Hamano <gitster@pobox.com>
Date
Feb 17, 2023, 21:50 UTC
Message-ID
<xmqqfsb4klks.fsf@gitster.g>
In-Reply-To
<b330222ce83bdf03c20085ff10fcff8a090474d5.1676665285.git.gitgitgadget@gmail.com>
"John Cai via GitGitGadget" <gitgitgadget@gmail.com> writes:
Looking good.  Some comments below.  Many of them minor.
Show 5 quoted lines
> +Setting the internal diff algorithm
> +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> +
> +The diff algorithm can be set through the `diff.algorithm` config key, but
> +sometimes it may be helpful to set the diff algorithm by path. For example, one
I would have expected "per path" instead of "by path".
> +might wish to set a diff algorithm automatically for all `.json` files such that
> +the user would not need to pass in a separate command line `--diff-algorithm`
> +flag each time.

While this is not incorrect per-se, I think the first paragraph of the proposed commit log message was a lot more convincing. Your changes may not be limited to a single kind of files, and a command line option is simply not enough. You may want one algorithm for ".json" while using another for ".c", which was really an excellent example you gave.

> +This diff algorithm applies to user facing diff output like git-diff(1),
> +git-show(1) and is used for the `--stat` output as well. The merge machinery
> +will not use the diff algorithm set through this method.
Is "format-patch" considered "user-facing"?
Show 10 quoted lines
> +NOTE: If the `command` key also exists, then Git will treat this as an external
> +diff and attempt to use the value set for `command` as an external program. For
> +instance, the following config, combined with the above `.gitattributes` file,
> +will result in `command` favored over `algorithm`.
> +
> +----------------------------------------------------------------
> +[diff "<name>"]
> +  command = j-c-diff
> +  algorithm = histogram
> +----------------------------------------------------------------

Isn't this a bit too verbose, given that the reader has just seen the external diff driver section. I wonder something like this is sufficient, without any sample configuration?

    NOTE: If `diff.<name>.command` is defined for path with the
    `diff=<name>` attribute, it is executed as an external diff driver
    (see above), and adding `diff.<name>.algorithm` has no effect (the
    algorithm is not passed to the external diff driver).
Show 22 quoted lines
> diff --git a/diff.c b/diff.c
> index 5efc22ca06b..04469da6d34 100644
> --- a/diff.c
> +++ b/diff.c
> @@ -4456,15 +4456,13 @@ static void run_diff_cmd(const char *pgm,
>  	const char *xfrm_msg = NULL;
>  	int complete_rewrite = (p->status == DIFF_STATUS_MODIFIED) && p->score;
>  	int must_show_header = 0;
> +	struct userdiff_driver *drv = NULL;
>  
> -
> -	if (o->flags.allow_external) {
> -		struct userdiff_driver *drv;
> -
> +	if (o->flags.allow_external || !o->ignore_driver_algorithm)
>  		drv = userdiff_find_by_path(o->repo->index, attr_path);
> -		if (drv && drv->external)
> -			pgm = drv->external;
> -	}
> +
> +	if (o->flags.allow_external && drv && drv->external)
> +		pgm = drv->external;

OK. There is no explicit "pgm = NULL" initialization in this function, but that is done by the caller passing NULL to the function as a parameter, so it all makes sense.

Show 8 quoted lines
> @@ -4481,12 +4479,16 @@ static void run_diff_cmd(const char *pgm,
>  		run_external_diff(pgm, name, other, one, two, xfrm_msg, o);
>  		return;
>  	}
> -	if (one && two)
> +	if (one && two) {
> +		if (drv && !o->ignore_driver_algorithm && drv->algorithm)
> +			set_diff_algorithm(o, drv->algorithm);

For symmetry with the above choice of pgm we just saw, the order of the condition might be easier to follow if written like so:

	if (!o->ignore_driver_algorithm && drv && drv->algorithm)
It would not make any measurable difference performance-wise either way.
Show 6 quoted lines
> @@ -4583,6 +4585,14 @@ static void run_diffstat(struct diff_filepair *p, struct diff_options *o,
>  	const char *name;
>  	const char *other;
>  
> +	if (!o->ignore_driver_algorithm) {
> +		struct userdiff_driver *drv = userdiff_find_by_path(o->repo->index, p->one->path);
That's an overlong line.
> +
> +		if (drv && drv->algorithm) {
> +			set_diff_algorithm(o, drv->algorithm);
> +		}
No need to have {} around a single statement block.
Show 30 quoted lines
> +	}
> +
>  	if (DIFF_PAIR_UNMERGED(p)) {
>  		/* unmerged */
>  		builtin_diffstat(p->one->path, NULL, NULL, NULL,
> @@ -5130,6 +5140,8 @@ static int diff_opt_diff_algorithm(const struct option *opt,
>  		return error(_("option diff-algorithm accepts \"myers\", "
>  			       "\"minimal\", \"patience\" and \"histogram\""));
>  
> +	options->ignore_driver_algorithm = 1;
> +
>  	return 0;
>  }
>  
> @@ -5145,6 +5157,8 @@ static int diff_opt_diff_algorithm_no_arg(const struct option *opt,
>  		BUG("available diff algorithms include \"myers\", "
>  			       "\"minimal\", \"patience\" and \"histogram\"");
>  
> +	options->ignore_driver_algorithm = 1;
> +
>  	return 0;
>  }
> @@ -5285,6 +5299,7 @@ static int diff_opt_patience(const struct option *opt,
>  	for (i = 0; i < options->anchors_nr; i++)
>  		free(options->anchors[i]);
>  	options->anchors_nr = 0;
> +	options->ignore_driver_algorithm = 1;
>  
>  	return set_diff_algorithm(options, "patience");
>  }

I was hoping that set_diff_algorithm() can be the shared common one that signals we were told to use a specific algorithm, but it also is called from internal codepaths so it cannot be it.

It is probably not worth introducing an extra helper that only calls set_diff_algorithm() and sets ignore_driver_algorithm bit only for that to reduce three-times repetition.

OK.
Show 11 quoted lines
> diff --git a/userdiff.c b/userdiff.c
> index d71b82feb74..ff25cfc4b4c 100644
> --- a/userdiff.c
> +++ b/userdiff.c
> @@ -293,7 +293,7 @@ PATTERNS("scheme",
>  	 "|([^][)(}{[ \t])+"),
>  PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
>  	 "\\\\[a-zA-Z@]+|\\\\.|[a-zA-Z0-9\x80-\xff]+"),
> -{ "default", NULL, -1, { NULL, 0 } },
> +{ "default", NULL, NULL, -1, { NULL, 0 } },
>  };

I was surprised that there is so little damage to the built-in userdiff driver definitions, but this is thanks to the PATTERNS() and IPATTERN() macro that use designated initializers. Very nice.

Nicely done.
Previous: John Cai via GitGitGadgetNext: Elijah Newren
Message 60 of 78 in “Teach diff to honor diff algorithms set through git attributes”
  1. 0/2 Teach diff to honor diff algorithms set through git attributesJohn Cai via GitGitGadget, Feb 5, 2023
  2. 1/2 diff: consolidate diff algorithm option parsingJohn Cai via GitGitGadget, Feb 5, 2023
  3. Phillip WoodFeb 6, 2023
  4. 2/2 diff: teach diff to read gitattribute diff-algorithmJohn Cai via GitGitGadget, Feb 5, 2023
  5. Eric SunshineFeb 5, 2023
  6. John CaiFeb 6, 2023
  7. Phillip WoodFeb 6, 2023
  8. Eric SunshineFeb 6, 2023
  9. John CaiFeb 6, 2023
  10. Elijah NewrenFeb 9, 2023
  11. "bad" diffs (was: [PATCH 2/2] diff: teach diff to read gitattribute diff-algorithm)Ævar Arnfjörð Bjarmason, Feb 9, 2023
  12. John CaiFeb 9, 2023
  13. Ævar Arnfjörð BjarmasonFeb 6, 2023
  14. John CaiFeb 6, 2023
  15. Phillip WoodFeb 7, 2023
  16. John CaiFeb 7, 2023
  17. Elijah NewrenFeb 9, 2023
  18. Phillip WoodFeb 9, 2023
  19. Elijah NewrenFeb 10, 2023
  20. Phillip WoodFeb 11, 2023
  21. Jeff KingFeb 11, 2023
  22. Elijah NewrenFeb 15, 2023
  23. Jeff KingFeb 15, 2023
  24. Junio C HamanoFeb 15, 2023
  25. Phillip WoodFeb 15, 2023
  26. Jeff KingFeb 15, 2023
  27. Ævar Arnfjörð BjarmasonFeb 7, 2023
  28. Phillip WoodFeb 15, 2023
  29. Elijah NewrenFeb 9, 2023
  30. John CaiFeb 14, 2023
  31. Elijah NewrenFeb 15, 2023
  32. Elijah NewrenFeb 9, 2023
  33. Ævar Arnfjörð BjarmasonFeb 9, 2023
  34. Jeff KingFeb 11, 2023
  35. Jeff KingFeb 7, 2023
  36. Ævar Arnfjörð BjarmasonFeb 7, 2023
  37. Junio C HamanoFeb 7, 2023
  38. Ævar Arnfjörð BjarmasonFeb 7, 2023
  39. Junio C HamanoFeb 7, 2023
  40. Ævar Arnfjörð BjarmasonFeb 7, 2023
  41. John CaiFeb 9, 2023
  42. Jeff KingFeb 11, 2023
  43. 0/2 Teach diff to honor diff algorithms set through git attributesJohn Cai via GitGitGadget, Feb 14, 2023
  44. 1/2 diff: consolidate diff algorithm option parsingJohn Cai via GitGitGadget, Feb 14, 2023
  45. Junio C HamanoFeb 15, 2023
  46. John CaiFeb 15, 2023
  47. Junio C HamanoFeb 15, 2023
  48. Jeff KingFeb 16, 2023
  49. Junio C HamanoFeb 16, 2023
  50. John CaiFeb 16, 2023
  51. 2/2 diff: teach diff to read gitattribute diff-algorithmJohn Cai via GitGitGadget, Feb 14, 2023
  52. Junio C HamanoFeb 15, 2023
  53. Junio C HamanoFeb 15, 2023
  54. John CaiFeb 16, 2023
  55. 0/2 Teach diff to honor diff algorithms set through git attributesJohn Cai via GitGitGadget, Feb 17, 2023
  56. 1/2 diff: consolidate diff algorithm option parsingJohn Cai via GitGitGadget, Feb 17, 2023
  57. Junio C HamanoFeb 17, 2023
  58. Elijah NewrenFeb 18, 2023
  59. 2/2 diff: teach diff to read algorithm from diff driverJohn Cai via GitGitGadget, Feb 17, 2023
  60. Junio C HamanoFeb 17, 2023
  61. Elijah NewrenFeb 18, 2023
  62. John CaiFeb 20, 2023
  63. Elijah NewrenFeb 20, 2023
  64. John CaiFeb 20, 2023
  65. Elijah NewrenFeb 20, 2023
  66. John CaiFeb 20, 2023
  67. Jeff KingFeb 22, 2023
  68. John CaiFeb 24, 2023
  69. Elijah NewrenFeb 18, 2023
  70. John CaiFeb 20, 2023
  71. 0/2 Teach diff to honor diff algorithms set through git attributesJohn Cai via GitGitGadget, Feb 20, 2023
  72. 1/2 diff: consolidate diff algorithm option parsingJohn Cai via GitGitGadget, Feb 20, 2023
  73. 2/2 diff: teach diff to read algorithm from diff driverJohn Cai via GitGitGadget, Feb 20, 2023
  74. Junio C HamanoFeb 21, 2023
  75. Elijah NewrenFeb 21, 2023
  76. Junio C HamanoFeb 21, 2023
  77. John CaiFeb 21, 2023
  78. Elijah NewrenFeb 21, 2023

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.