Subject: Re: [PATCH v3 6/6] diff: shortcut for diff'ing two binary SHA-1 objects

From: Junio C Hamano <[email-masked]>
Date: Thu, 14 Aug 2014 10:17:45 -0700

Nguyễn Thái Ngọc Duy  <[email-masked]> writes:

> If we are given two SHA-1 and asked to determine if they are different
> (but not _what_ differences), we know right away by comparing SHA-1.
>
> A side effect of this patch is, because large files are marked binary,
> diff-tree will not need to unpack them. 'diff-index --cached' will not
> either. But 'diff-files' still does.
>
> Signed-off-by: Nguyễn Thái Ngọc Duy <[email-masked]>
> ---
>  diff.c           | 13 +++++++++++++
>  t/t1050-large.sh |  8 ++++++++
>  2 files changed, 21 insertions(+)
>
> diff --git a/diff.c b/diff.c
> index d381a6f..b85bcfb 100644
> --- a/diff.c
> +++ b/diff.c
> @@ -2324,6 +2324,19 @@ static void builtin_diff(const char *name_a,
>  	} else if (!DIFF_OPT_TST(o, TEXT) &&
>  	    ( (!textconv_one && diff_filespec_is_binary(one)) ||
>  	      (!textconv_two && diff_filespec_is_binary(two)) )) {
> +		if (!one->data && !two->data &&
> +		    S_ISREG(one->mode) && S_ISREG(two->mode) &&
> +		    !DIFF_OPT_TST(o, BINARY)) {
> +			if (!hashcmp(one->sha1, two->sha1)) {
> +				if (must_show_header)
> +					fprintf(o->file, "%s", header.buf);
> +				goto free_ab_and_return;
> +			}
> +			fprintf(o->file, "%s", header.buf);
> +			fprintf(o->file, "%sBinary files %s and %s differ\n",
> +				line_prefix, lbl[0], lbl[1]);
> +			goto free_ab_and_return;
> +		}

A tangent.

I think one and two can point at the same object only when this
filepair is involved in rename/copy.  In other words, one and two
with the same <mode,sha1,name> would not be given to this code.  And
must-show-header would be set to true long before we get here in
fill-metainfo in such a case.

I think this new code and the original below which you copied this
one from can probably be simplified.  It already felt wrong to see
two copies of "fprintf(o->file "%s", header.buf)" and now we have
four of them.  Because this is a copy-and-paste of the identical
logic from below, I do not want you to attempt fixing this tangent
in this patch, though.

Thanks.

>  		if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
>  			die("unable to read files to diff");
>  		/* Quite common confusing case */
> diff --git a/t/t1050-large.sh b/t/t1050-large.sh
> index 711f22c..b294963 100755
> --- a/t/t1050-large.sh
> +++ b/t/t1050-large.sh
> @@ -116,6 +116,14 @@ test_expect_success 'diff --stat' '
>  	git diff --stat HEAD^ HEAD
>  '
>  
> +test_expect_success 'diff' '
> +	git diff HEAD^ HEAD
> +'
> +
> +test_expect_success 'diff --cached' '
> +	git diff --cached HEAD^
> +'
> +
>  test_expect_success 'hash-object' '
>  	git hash-object large1
>  '