Skip to content

Commit 6e9c4d4

Browse files
bk2204gitster
authored andcommitted
git-cvsexportcommit: port to SHA-256
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 <[email protected]> Acked-by: Eric Wong <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f3eaa09 commit 6e9c4d4

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

git-cvsexportcommit.perl

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
my $repo = Git->repository();
2323
$opt_w = $repo->config('cvsexportcommit.cvsdir') unless defined $opt_w;
2424

25+
my $tmpdir = File::Temp->newdir;
26+
my $hash_algo = $repo->config('extensions.objectformat') || 'sha1';
27+
my $hexsz = $hash_algo eq 'sha256' ? 64 : 40;
28+
2529
if ($opt_w || $opt_W) {
2630
# Remember where GIT_DIR is before changing to CVS checkout
2731
unless ($ENV{GIT_DIR}) {
@@ -96,7 +100,7 @@
96100
}
97101

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

114-
my $noparent = "0000000000000000000000000000000000000000";
118+
my $noparent = "0" x $hexsz;
115119
if ($parent) {
116120
my $found;
117121
# double check that it's a valid parent
@@ -174,7 +178,7 @@
174178
print "Checking if patch will apply\n";
175179

176180
my @stat;
177-
open APPLY, "GIT_DIR= git-apply $context --summary --numstat<.cvsexportcommit.diff|" || die "cannot patch";
181+
open APPLY, "GIT_INDEX_FILE=$tmpdir/index git-apply $context --summary --numstat<.cvsexportcommit.diff|" || die "cannot patch";
178182
@stat=<APPLY>;
179183
close APPLY || die "Cannot patch";
180184
my (@bfiles,@files,@afiles,@dfiles);
@@ -329,7 +333,7 @@
329333
if ($opt_W) {
330334
system("git checkout -q $commit^0") && die "cannot patch";
331335
} else {
332-
`GIT_DIR= git-apply $context --summary --numstat --apply <.cvsexportcommit.diff` || die "cannot patch";
336+
`GIT_INDEX_FILE=$tmpdir/index git-apply $context --summary --numstat --apply <.cvsexportcommit.diff` || die "cannot patch";
333337
}
334338

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

408412
if ($opt_W) {
409413
system("git checkout $go_back_to") && die "cannot move back to $go_back_to";
410-
if (!($go_back_to =~ /^[0-9a-fA-F]{40}$/)) {
414+
if (!($go_back_to =~ /^[0-9a-fA-F]{$hexsz}$/)) {
411415
system("git symbolic-ref HEAD $go_back_to") &&
412416
die "cannot move back to $go_back_to";
413417
}

0 commit comments

Comments
 (0)