Re: [PATCH v3 2/2] diff: index-line: respect --abbrev in object's name
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Aug 14, 2020, 18:59 UTC
- Message-ID
- <xmqqy2mhvys6.fsf@gitster.c.googlers.com>
- In-Reply-To
- <xmqqwo21xiuy.fsf@gitster.c.googlers.com>
Junio C Hamano <gitster@pobox.com> writes:
Show 28 quoted lines
> Ouch. These apparently come from
>
> process_diffs () {
> ...
> }
>
> Hash-independence may be good, but it should not munge expected mode
> bits from 100644 to ffff44, which I think is a bug in the original
> introduced in 72f936b1 (t4013: make test hash independent,
> 2020-02-07).
>
> When we are adjusting the abbrev length of the index line, of course
> $_x07 would not be sufficient to match the abbreviated object name
> in full, so a79 vs 895 can be explained and is a bug in this patch
> that did not update the process_diffs helper.
>
> Another thing that I find somewhat problematic in the original
> (brian cc'ed) is that it does not special case all-zero object name
> specially. By turning any and all instances of $_x40 to $ZERO_OID,
> we lose the distinction between a random-looking object name which
> got turned into $ZERO_OID by the processing, and an object name that
> was $ZERO_OID from the beginning, so we won't catch a possible
> future bug where new file's preimage object name is not $ZERO_OID
> (this is plausible when you try to show an intent-to-add entry; the
> diff between the index and the working tree would be "new file"
> patch, but the index entry records the object name for an empty
> blob, e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 instead of $ZERO_OID
> can easily be emitted by a mistaken implementation).So here is what I came up with as a possible starting point. The idea is to grab hexadecimal strings at locations the original tried to isolate with various contexts, and
- if the input happens to be all zero, use '0', otherwise use 'f'
- if the input is 40-bytes (i.e. unabbreviated object name in the SHA-1 world), repeat the character chosen in the first step as many times as there are chars in $ZERO_OID
- otherwise, repeat the character chosen in the first step as many times as there are chars in the input.
- regardless of all of the above, special case possible in-tree blob modes (100644, 100755 and 120000) and don't munge them.
I haven't tried it with the patch that started this discussion thread, nor with SHA-256 build, though.
t/t4013-diff-various.sh | 58 +++++++++++++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 21 deletions(-)
diff --git a/t/t4013-diff-various.sh b/t/t4013-diff-various.sh index 43267d6024..b33e60ab9d 100755 --- a/t/t4013-diff-various.sh +++ b/t/t4013-diff-various.sh @@ -130,27 +130,43 @@ test_expect_success setup ' EOF process_diffs () { - _x04="[0-9a-f][0-9a-f][0-9a-f][0-9a-f]" && - _x07="$_x05[0-9a-f][0-9a-f]" && - sed -e "s/$OID_REGEX/$ZERO_OID/g" \ - -e "s/From $_x40 /From $ZERO_OID /" \ - -e "s/from $_x40)/from $ZERO_OID)/" \ - -e "s/commit $_x40\$/commit $ZERO_OID/" \ - -e "s/commit $_x40 (/commit $ZERO_OID (/" \ - -e "s/$_x40 $_x40 $_x40/$ZERO_OID $ZERO_OID $ZERO_OID/" \ - -e "s/$_x40 $_x40 /$ZERO_OID $ZERO_OID /" \ - -e "s/^$_x40 $_x40$/$ZERO_OID $ZERO_OID/" \ - -e "s/^$_x40 /$ZERO_OID /" \ - -e "s/^$_x40$/$ZERO_OID/" \ - -e "s/$_x07\.\.$_x07/fffffff..fffffff/g" \ - -e "s/$_x07,$_x07\.\.$_x07/fffffff,fffffff..fffffff/g" \ - -e "s/$_x07 $_x07 $_x07/fffffff fffffff fffffff/g" \ - -e "s/$_x07 $_x07 /fffffff fffffff /g" \ - -e "s/Merge: $_x07 $_x07/Merge: fffffff fffffff/g" \ - -e "s/$_x07\.\.\./fffffff.../g" \ - -e "s/ $_x04\.\.\./ ffff.../g" \ - -e "s/ $_x04/ ffff/g" \ - "$1" + perl -e ' + my $oid_length = length($ARGV[0]); + my $x40 = "[0-9a-f]{40}"; + my $xab = "[0-9a-f]{4,16}"; + my $orx = "[0-9a-f]" x $oid_length; + + sub munge_oid { + my ($oid) = @_; + my $x; + + if ($oid =~ /^(100644|100755|120000)$/) { + return $oid; + } + + if ($oid =~ /^0*$/) { + $x = "0"; + } else { + $x = "f"; + } + + if (length($oid) == 40) { + return $x x $oid_length; + } else { + return $x x length($oid); + } + } + + while (<STDIN>) { + s/($orx)/munge_oid($1)/ge; + s/From ($x40)( |\))/"From " . munge_oid($1) . $2/ge; + s/commit ($x40)($| \()/"commit " . munge_oid($1) . $2/ge; + s/($x40) /munge_oid($1) . " "/ge; + s/^($x40)($| )/munge_oid($1) . $2/e; + s/($xab)(\.\.|,| |\.\.\.|$)/munge_oid($1) . $2/ge; + print; + } + ' "$ZERO_OID" <"$1" } V=$(git version | sed -e 's/^git version //' -e 's/\./\\./g')