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

Re: [PATCH v3] diff: ensure consistent diff behavior with ignore options

From
Junio C Hamano <gitster@pobox.com>
Date
Aug 6, 2025, 20:56 UTC
Message-ID
<xmqqpld8tbib.fsf@gitster.g>
In-Reply-To
<20250806123306.25532-1-yldhome2d2@gmail.com>
Lidong Yan <yldhome2d2@gmail.com> writes:
Show 8 quoted lines
> +static int quick_consume(void *priv, char *line, unsigned long len)
> +{
> +	struct emit_callback *ecbdata = priv;
> +	struct diff_options *o = ecbdata->opt;
> +
> +	o->found_changes = 1;
> +	return 1;
> +}
"make DEVELOPER=YesPlease" would not be very happy, without

-static int quick_consume(void *priv, char *line, unsigned long len) +static int quick_consume(void *priv, char *line UNUSED, unsigned long len UNUSED)

Show 15 quoted lines
> +/* return 1 if any change is found; otherwise, return 0 */
> +static int diff_flush_patch_quiet(struct diff_filepair *p, struct diff_options *o)
> +{
> +	int diff_opt = o->diff_optimize;
> +	int found_changes = o->found_changes;
> +	int ret;
> +
> +	o->diff_optimize = DIFF_OPT_DRY_RUN;
> +	o->found_changes = 0;
> +	diff_flush_patch(p, o);
> +	ret = o->found_changes;
> +	o->diff_optimize = diff_opt;
> +	o->found_changes |= found_changes;
> +	return ret;
> +}
Show 23 quoted lines
>  static void diff_flush_stat(struct diff_filepair *p, struct diff_options *o,
>  			    struct diffstat_t *diffstat)
>  {
> @@ -6778,7 +6810,19 @@ void diff_flush(struct diff_options *options)
>  			     DIFF_FORMAT_CHECKDIFF)) {
>  		for (i = 0; i < q->nr; i++) {
>  			struct diff_filepair *p = q->queue[i];
> -			if (check_pair_status(p))
> +			int need_flush = 1;
> +
> +			if (!check_pair_status(p))
> +				continue;
> +
> +			if (options->flags.diff_from_contents) {
> +				if (diff_flush_patch_quiet(p, options))
> +					need_flush = 1;
> +				else
> +					need_flush = 0;
> +			}
> +
> +			if (need_flush)
>  				flush_one_pair(p, options);
>  		}

I am having a hard time understanding the logic in this loop. Is it equivalent to the following loop, and ...

		for (i = 0; i < q->nr; i++) {
			struct diff_filepair *p = q->queue[i];
			if (!check_pair_status(p))
				continue;
			if (options->flags.diff_from_contents &&
			    !diff_flush_patch_quietly(p, options))
				continue; /* no changes */
			flush_one_pair(p, options);
		}
... if so, isn't the above rewrite easier to follow?

The idea is that we cannot do anything with DIFF_STATUS_UNKNOWN pairs (hence we continue), and when we are filtering output by the "has the pair changed in a way that the user cares about?" criteria, we check with flush_patch_quietly, and if there is no change worth talking about, we do not do anything with such a pair, either.

Only when these conditions are not met, we call flush_one_pair() to show the name or name status or whatever.

Show 22 quoted lines
>  		separator++;
> @@ -6831,19 +6875,10 @@ void diff_flush(struct diff_options *options)
>  	if (output_format & DIFF_FORMAT_NO_OUTPUT &&
>  	    options->flags.exit_with_status &&
>  	    options->flags.diff_from_contents) {
> -		/*
> -		 * run diff_flush_patch for the exit status. setting
> -		 * options->file to /dev/null should be safe, because we
> -		 * aren't supposed to produce any output anyway.
> -		 */
> -		diff_free_file(options);
> -		options->file = xfopen("/dev/null", "w");
> -		options->close_file = 1;
> -		options->color_moved = 0;
>  		for (i = 0; i < q->nr; i++) {
>  			struct diff_filepair *p = q->queue[i];
>  			if (check_pair_status(p))
> -				diff_flush_patch(p, options);
> +				diff_flush_patch_quiet(p, options);
>  			if (options->found_changes)
>  				break;
>  		}

It is a natural consequence of having a helper that does the "quiet" thing that we can now lose the /dev/null hack, which is very nice.

Overall, the changes are easy to follow and look good.
Thanks.
Previous: Lidong YanNext: Lidong Yan
Message 29 of 35 in “git-diff: --ignore-matching-lines has no effect on the output when --name-only is used”
  1. hi@arnes.spaceJul 23, 2025
  2. Lidong YanJul 23, 2025
  3. Junio C HamanoJul 23, 2025
  4. Lidong YanJul 24, 2025
  5. Eric SunshineJul 24, 2025
  6. Lidong YanJul 24, 2025
  7. hi@arnes.spaceJul 25, 2025
  8. hi@arnes.spaceJul 25, 2025
  9. Lidong YanJul 25, 2025
  10. hi@arnes.spaceJul 25, 2025
  11. Jeff KingJul 25, 2025
  12. Junio C HamanoJul 25, 2025
  13. diff: ensure consistent diff behavior with -I<regex> across output formatsLidong Yan, Jul 29, 2025
  14. Junio C HamanoJul 30, 2025
  15. Jeff KingAug 2, 2025
  16. Lidong YanAug 3, 2025
  17. Junio C HamanoAug 3, 2025
  18. Junio C HamanoAug 4, 2025
  19. Jeff KingAug 4, 2025
  20. diff: ensure consistent diff behavior with -I<regex> across output formatsLidong Yan, Aug 3, 2025
  21. Junio C HamanoAug 4, 2025
  22. Lidong YanAug 4, 2025
  23. Junio C HamanoAug 4, 2025
  24. Lidong YanAug 5, 2025
  25. Junio C HamanoAug 5, 2025
  26. diff: ensure consistent diff behavior with ignore optionsLidong Yan, Aug 6, 2025
  27. Junio C HamanoAug 6, 2025
  28. Lidong YanAug 7, 2025
  29. Junio C HamanoAug 6, 2025
  30. Lidong YanAug 7, 2025
  31. diff: ensure consistent diff behavior with ignore optionsLidong Yan, Aug 7, 2025
  32. Junio C HamanoAug 7, 2025
  33. Lidong YanAug 8, 2025
  34. diff: ensure consistent diff behavior with ignore optionsLidong Yan, Aug 8, 2025
  35. Johannes SchindelinOct 16, 2025

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.