Skip to content

Commit

Permalink
git-cvsexportcommit: port to SHA-256
Browse files Browse the repository at this point in the history
When we apply a binary patch, we must have the full object ID in the
header in order to apply it; without that, any attempt to apply it will
fail.  If we set GIT_DIR to empty, git apply does not know about the
hash algorithm we're using, and consequently any attempt to apply a
patch using SHA-256 will fail, since the object ID is the wrong length.

The reason we set the GIT_DIR environment variable is because we don't
want to modify the index; we just want to know whether the patch
applies.  Instead, let's just use a temporary file for the index, which
will be cleaned up automatically when the object goes out of scope.

Additionally, read the configuration for the repository and compute the
length of an object ID based on it.  Use that when matching object IDs
with a regex or computing the all-zeros object ID.

Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
Acked-by: Eric Wong <e@80x24.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
bk2204 authored and gitster committed Jun 22, 2020
1 parent f3eaa09 commit 6e9c4d4
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions git-cvsexportcommit.perl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
my $repo = Git->repository();
$opt_w = $repo->config('cvsexportcommit.cvsdir') unless defined $opt_w;

my $tmpdir = File::Temp->newdir;
my $hash_algo = $repo->config('extensions.objectformat') || 'sha1';
my $hexsz = $hash_algo eq 'sha256' ? 64 : 40;

if ($opt_w || $opt_W) {
# Remember where GIT_DIR is before changing to CVS checkout
unless ($ENV{GIT_DIR}) {
Expand Down Expand Up @@ -96,7 +100,7 @@
}

if ($stage eq 'headers') {
if ($line =~ m/^parent (\w{40})$/) { # found a parent
if ($line =~ m/^parent ([0-9a-f]{$hexsz})$/) { # found a parent
push @parents, $1;
} elsif ($line =~ m/^author (.+) \d+ [-+]\d+$/) {
$author = $1;
Expand All @@ -111,7 +115,7 @@
}
}

my $noparent = "0000000000000000000000000000000000000000";
my $noparent = "0" x $hexsz;
if ($parent) {
my $found;
# double check that it's a valid parent
Expand Down Expand Up @@ -174,7 +178,7 @@
print "Checking if patch will apply\n";

my @stat;
open APPLY, "GIT_DIR= git-apply $context --summary --numstat<.cvsexportcommit.diff|" || die "cannot patch";
open APPLY, "GIT_INDEX_FILE=$tmpdir/index git-apply $context --summary --numstat<.cvsexportcommit.diff|" || die "cannot patch";
@stat=<APPLY>;
close APPLY || die "Cannot patch";
my (@bfiles,@files,@afiles,@dfiles);
Expand Down Expand Up @@ -329,7 +333,7 @@
if ($opt_W) {
system("git checkout -q $commit^0") && die "cannot patch";
} else {
`GIT_DIR= git-apply $context --summary --numstat --apply <.cvsexportcommit.diff` || die "cannot patch";
`GIT_INDEX_FILE=$tmpdir/index git-apply $context --summary --numstat --apply <.cvsexportcommit.diff` || die "cannot patch";
}

print "Patch applied successfully. Adding new files and directories to CVS\n";
Expand Down Expand Up @@ -407,7 +411,7 @@

if ($opt_W) {
system("git checkout $go_back_to") && die "cannot move back to $go_back_to";
if (!($go_back_to =~ /^[0-9a-fA-F]{40}$/)) {
if (!($go_back_to =~ /^[0-9a-fA-F]{$hexsz}$/)) {
system("git symbolic-ref HEAD $go_back_to") &&
die "cannot move back to $go_back_to";
}
Expand Down

0 comments on commit 6e9c4d4

Please sign in to comment.