Skip to content

Commit e7e113a

Browse files
committed
Merge branch 'bc/sha-256-cvs-svn-updates'
CVS/SVN interface have been prepared for SHA-256 transition * bc/sha-256-cvs-svn-updates: git-cvsexportcommit: port to SHA-256 git-cvsimport: port to SHA-256 git-cvsserver: port to SHA-256 git-svn: set the OID length based on hash algorithm perl: make SVN code hash independent perl: make Git::IndexInfo work with SHA-256 perl: create and switch variables for hash constants t/lib-git-svn: make hash size independent t9101: make hash independent t9104: make hash size independent t9100: make test work with SHA-256 t9108: make test hash independent t9168: make test hash independent t9109: make test hash independent
2 parents d80bea4 + 6e9c4d4 commit e7e113a

17 files changed

+151
-111
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
}

git-cvsimport.perl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -637,9 +637,9 @@ ()
637637
return $pwd;
638638
}
639639

640-
sub is_sha1 {
640+
sub is_oid {
641641
my $s = shift;
642-
return $s =~ /^[a-f0-9]{40}$/;
642+
return $s =~ /^[a-f0-9]{40}(?:[a-f0-9]{24})?$/;
643643
}
644644

645645
sub get_headref ($) {
@@ -810,7 +810,7 @@ ()
810810
open(my $fh, '-|', qw(git write-tree))
811811
or die "unable to open git write-tree: $!";
812812
chomp(my $tree = <$fh>);
813-
is_sha1($tree)
813+
is_oid($tree)
814814
or die "Cannot get tree id ($tree): $!";
815815
close($fh)
816816
or die "Error running git write-tree: $?\n";
@@ -896,7 +896,7 @@ sub commit {
896896

897897
print "Committed patch $patchset ($branch $commit_date)\n" if $opt_v;
898898
chomp(my $cid = <$commit_read>);
899-
is_sha1($cid) or die "Cannot get commit id ($cid): $!\n";
899+
is_oid($cid) or die "Cannot get commit id ($cid): $!\n";
900900
print "Commit ID $cid\n" if $opt_v;
901901
close($commit_read);
902902

git-cvsserver.perl

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ sub req_Root
365365
}
366366
foreach my $line ( @gitvars )
367367
{
368-
next unless ( $line =~ /^(gitcvs)\.(?:(ext|pserver)\.)?([\w-]+)=(.*)$/ );
368+
next unless ( $line =~ /^(gitcvs|extensions)\.(?:(ext|pserver)\.)?([\w-]+)=(.*)$/ );
369369
unless ($2) {
370370
$cfg->{$1}{$3} = $4;
371371
} else {
@@ -392,6 +392,9 @@ sub req_Root
392392
$log->nofile();
393393
}
394394

395+
$state->{rawsz} = ($cfg->{'extensions'}{'objectformat'} || 'sha1') eq 'sha256' ? 32 : 20;
396+
$state->{hexsz} = $state->{rawsz} * 2;
397+
395398
return 1;
396399
}
397400

@@ -1581,7 +1584,7 @@ sub req_ci
15811584

15821585
$parenthash = safe_pipe_capture('git', 'show-ref', '-s', $branchRef);
15831586
chomp $parenthash;
1584-
if ($parenthash !~ /^[0-9a-f]{40}$/)
1587+
if ($parenthash !~ /^[0-9a-f]{$state->{hexsz}}$/)
15851588
{
15861589
if ( defined($stickyInfo) && defined($stickyInfo->{tag}) )
15871590
{
@@ -1708,7 +1711,7 @@ sub req_ci
17081711
chomp($commithash);
17091712
$log->info("Commit hash : $commithash");
17101713

1711-
unless ( $commithash =~ /[a-zA-Z0-9]{40}/ )
1714+
unless ( $commithash =~ /[a-zA-Z0-9]{$state->{hexsz}}/ )
17121715
{
17131716
$log->warn("Commit failed (Invalid commit hash)");
17141717
print "error 1 Commit failed (unknown reason)\n";
@@ -2375,7 +2378,7 @@ sub req_annotate
23752378
print "E ***************\n";
23762379
while ( <ANNOTATE> )
23772380
{
2378-
if (m/^([a-zA-Z0-9]{40})\t\([^\)]*\)(.*)$/i)
2381+
if (m/^([a-zA-Z0-9]{$state->{hexsz}})\t\([^\)]*\)(.*)$/i)
23792382
{
23802383
my $commithash = $1;
23812384
my $data = $2;
@@ -2852,7 +2855,7 @@ sub transmitfile
28522855
return;
28532856
}
28542857

2855-
die "Need filehash" unless ( defined ( $filehash ) and $filehash =~ /^[a-zA-Z0-9]{40}$/ );
2858+
die "Need filehash" unless ( defined ( $filehash ) and $filehash =~ /^[a-zA-Z0-9]{$state->{hexsz}}$/ );
28562859

28572860
my $type = safe_pipe_capture('git', 'cat-file', '-t', $filehash);
28582861
chomp $type;
@@ -3042,7 +3045,7 @@ sub ensureWorkTree
30423045

30433046
my $ver = safe_pipe_capture('git', 'show-ref', '-s', "refs/heads/$state->{module}");
30443047
chomp $ver;
3045-
if ($ver !~ /^[0-9a-f]{40}$/)
3048+
if ($ver !~ /^[0-9a-f]{$state->{hexsz}}$/)
30463049
{
30473050
$log->warn("Error from git show-ref -s refs/head$state->{module}");
30483051
print "error 1 cannot find the current HEAD of module";
@@ -3281,7 +3284,7 @@ sub open_blob_or_die
32813284
}
32823285
elsif( $srcType eq "sha1" )
32833286
{
3284-
unless ( defined ( $name ) and $name =~ /^[a-zA-Z0-9]{40}$/ )
3287+
unless ( defined ( $name ) and $name =~ /^[a-zA-Z0-9]{$state->{hexsz}}$/ )
32853288
{
32863289
$log->warn("Need filehash");
32873290
die "Need filehash\n";
@@ -3817,7 +3820,7 @@ sub update
38173820
chomp $commitsha1;
38183821

38193822
my $commitinfo = ::safe_pipe_capture('git', 'cat-file', 'commit', $self->{module});
3820-
unless ( $commitinfo =~ /tree\s+[a-zA-Z0-9]{40}/ )
3823+
unless ( $commitinfo =~ /tree\s+[a-zA-Z0-9]{$state->{hexsz}}/ )
38213824
{
38223825
die("Invalid module '$self->{module}'");
38233826
}
@@ -3957,7 +3960,7 @@ sub update
39573960
while ( <FILELIST> )
39583961
{
39593962
chomp;
3960-
unless ( /^:\d{6}\s+([0-7]{6})\s+[a-f0-9]{40}\s+([a-f0-9]{40})\s+(\w)$/o )
3963+
unless ( /^:\d{6}\s+([0-7]{6})\s+[a-f0-9]{$state->{hexsz}}\s+([a-f0-9]{$state->{hexsz}})\s+(\w)$/o )
39613964
{
39623965
die("Couldn't process git-diff-tree line : $_");
39633966
}
@@ -4625,11 +4628,11 @@ sub getmeta
46254628
$db_query->execute($filename, $intRev);
46264629
$meta = $db_query->fetchrow_hashref;
46274630
}
4628-
elsif ( $revision =~ /^2\.1\.1\.2000(\.[1-3][0-9][0-9]){20}$/ )
4631+
elsif ( $revision =~ /^2\.1\.1\.2000(\.[1-3][0-9][0-9]){$state->{rawsz}}$/ )
46294632
{
46304633
my ($commitHash)=($revision=~/^2\.1\.1\.2000(.*)$/);
46314634
$commitHash=~s/\.([0-9]+)/sprintf("%02x",$1-100)/eg;
4632-
if($commitHash=~/^[0-9a-f]{40}$/)
4635+
if($commitHash=~/^[0-9a-f]{$state->{hexsz}}$/)
46334636
{
46344637
return $self->getMetaFromCommithash($filename,$commitHash);
46354638
}
@@ -4639,7 +4642,7 @@ sub getmeta
46394642
$log->warning("failed get $revision with commithash=$commitHash");
46404643
undef $revision;
46414644
}
4642-
elsif ( $revision =~ /^[0-9a-f]{40}$/ )
4645+
elsif ( $revision =~ /^[0-9a-f]{$state->{hexsz}}$/ )
46434646
{
46444647
# Try DB first. This is mostly only useful for req_annotate(),
46454648
# which only calls this for stuff that should already be in
@@ -4658,7 +4661,7 @@ sub getmeta
46584661
if(! $meta)
46594662
{
46604663
my($revCommit)=$self->lookupCommitRef($revision);
4661-
if($revCommit=~/^[0-9a-f]{40}$/)
4664+
if($revCommit=~/^[0-9a-f]{$state->{hexsz}}$/)
46624665
{
46634666
return $self->getMetaFromCommithash($filename,$revCommit);
46644667
}
@@ -4672,7 +4675,7 @@ sub getmeta
46724675
else
46734676
{
46744677
my($revCommit)=$self->lookupCommitRef($revision);
4675-
if($revCommit=~/^[0-9a-f]{40}$/)
4678+
if($revCommit=~/^[0-9a-f]{$state->{hexsz}}$/)
46764679
{
46774680
return $self->getMetaFromCommithash($filename,$revCommit);
46784681
}
@@ -4767,7 +4770,7 @@ sub getMetaFromCommithash
47674770

47684771
my($fileHash) = ::safe_pipe_capture("git","rev-parse","$revCommit:$filename");
47694772
chomp $fileHash;
4770-
if(!($fileHash=~/^[0-9a-f]{40}$/))
4773+
if(!($fileHash=~/^[0-9a-f]{$state->{hexsz}}$/))
47714774
{
47724775
die "Invalid fileHash '$fileHash' looking up"
47734776
." '$revCommit:$filename'\n";
@@ -4863,7 +4866,7 @@ sub lookupCommitRef
48634866
$commitHash = ::safe_pipe_capture("git","rev-parse","--verify","--quiet",
48644867
$self->unescapeRefName($ref));
48654868
$commitHash=~s/\s*$//;
4866-
if(!($commitHash=~/^[0-9a-f]{40}$/))
4869+
if(!($commitHash=~/^[0-9a-f]{$state->{hexsz}}$/))
48674870
{
48684871
$commitHash=undef;
48694872
}
@@ -4909,7 +4912,7 @@ sub commitmessage
49094912
my $commithash = shift;
49104913
my $tablename = $self->tablename("commitmsgs");
49114914

4912-
die("Need commithash") unless ( defined($commithash) and $commithash =~ /^[a-zA-Z0-9]{40}$/ );
4915+
die("Need commithash") unless ( defined($commithash) and $commithash =~ /^[a-zA-Z0-9]{$state->{hexsz}}$/ );
49134916

49144917
my $db_query;
49154918
$db_query = $self->{dbh}->prepare_cached("SELECT value FROM $tablename WHERE key=?",{},1);

git-svn.perl

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
use warnings;
66
use strict;
77
use vars qw/ $AUTHOR $VERSION
8-
$sha1 $sha1_short $_revision $_repository
8+
$oid $oid_short $oid_length
9+
$_revision $_repository
910
$_q $_authors $_authors_prog %users/;
1011
$AUTHOR = 'Eric Wong <[email protected]>';
1112
$VERSION = '@@GIT_VERSION@@';
@@ -103,8 +104,9 @@ sub _req_svn {
103104
}
104105
}
105106

106-
$sha1 = qr/[a-f\d]{40}/;
107-
$sha1_short = qr/[a-f\d]{4,40}/;
107+
$oid = qr/(?:[a-f\d]{40}(?:[a-f\d]{24})?)/;
108+
$oid_short = qr/[a-f\d]{4,64}/;
109+
$oid_length = 40;
108110
my ($_stdin, $_help, $_edit,
109111
$_message, $_file, $_branch_dest,
110112
$_template, $_shared,
@@ -498,6 +500,7 @@ sub do_git_init_db {
498500
command_noisy('config', "$pfx.preserve-empty-dirs", 'true');
499501
command_noisy('config', "$pfx.placeholder-filename", $$fname);
500502
}
503+
load_object_format();
501504
}
502505

503506
sub init_subdir {
@@ -582,7 +585,7 @@ sub cmd_set_tree {
582585
print "Reading from stdin...\n";
583586
@commits = ();
584587
while (<STDIN>) {
585-
if (/\b($sha1_short)\b/o) {
588+
if (/\b($oid_short)\b/o) {
586589
unshift @commits, $1;
587590
}
588591
}
@@ -1831,7 +1834,7 @@ sub get_tree_from_treeish {
18311834
if ($type eq 'commit') {
18321835
$expected = (grep /^tree /, command(qw/cat-file commit/,
18331836
$treeish))[0];
1834-
($expected) = ($expected =~ /^tree ($sha1)$/o);
1837+
($expected) = ($expected =~ /^tree ($oid)$/o);
18351838
die "Unable to get tree from $treeish\n" unless $expected;
18361839
} elsif ($type eq 'tree') {
18371840
$expected = $treeish;
@@ -1975,9 +1978,15 @@ sub read_git_config {
19751978
}
19761979
}
19771980
}
1981+
load_object_format();
19781982
delete @$opts{@config_only} if @config_only;
19791983
}
19801984

1985+
sub load_object_format {
1986+
chomp(my $hash = `git config --get extensions.objectformat`);
1987+
$::oid_length = 64 if $hash eq 'sha256';
1988+
}
1989+
19811990
sub extract_metadata {
19821991
my $id = shift or return (undef, undef, undef);
19831992
my ($url, $rev, $uuid) = ($id =~ /^\s*git-svn-id:\s+(.*)\@(\d+)
@@ -2006,10 +2015,10 @@ sub cmt_sha2rev_batch {
20062015
print $out $sha, "\n";
20072016

20082017
while (my $line = <$in>) {
2009-
if ($first && $line =~ /^[[:xdigit:]]{40}\smissing$/) {
2018+
if ($first && $line =~ /^$::oid\smissing$/) {
20102019
last;
20112020
} elsif ($first &&
2012-
$line =~ /^[[:xdigit:]]{40}\scommit\s(\d+)$/) {
2021+
$line =~ /^$::oid\scommit\s(\d+)$/) {
20132022
$first = 0;
20142023
$size = $1;
20152024
next;
@@ -2036,7 +2045,7 @@ sub working_head_info {
20362045
my $hash;
20372046
my %max;
20382047
while (<$fh>) {
2039-
if ( m{^commit ($::sha1)$} ) {
2048+
if ( m{^commit ($::oid)$} ) {
20402049
unshift @$refs, $hash if $hash and $refs;
20412050
$hash = $1;
20422051
next;

perl/Git/IndexInfo.pm

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ use Git qw/command_input_pipe command_close_pipe/;
55

66
sub new {
77
my ($class) = @_;
8+
my $hash_algo = Git::config('extensions.objectformat') || 'sha1';
89
my ($gui, $ctx) = command_input_pipe(qw/update-index -z --index-info/);
9-
bless { gui => $gui, ctx => $ctx, nr => 0}, $class;
10+
bless { gui => $gui, ctx => $ctx, nr => 0, hash_algo => $hash_algo}, $class;
1011
}
1112

1213
sub remove {
1314
my ($self, $path) = @_;
14-
if (print { $self->{gui} } '0 ', 0 x 40, "\t", $path, "\0") {
15+
my $length = $self->{hash_algo} eq 'sha256' ? 64 : 40;
16+
if (print { $self->{gui} } '0 ', 0 x $length, "\t", $path, "\0") {
1517
return ++$self->{nr};
1618
}
1719
undef;

0 commit comments

Comments
 (0)