git-cvsserver: port to SHA-256

The code of git-cvsserver currently has several hard-coded 20 and 40
constants that are the length of SHA-1.  When parsing the configuration
file, read the extensions.objectformat configuration setting as well as
CVS-related ones and adjust the hash sizes accordingly.  Use these
computed values in all the places we match object IDs.

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>
This commit is contained in:
brian m. carlson 2020-06-22 18:04:16 +00:00 committed by Junio C Hamano
parent 66eadd119a
commit 05ea93d6b2

View File

@ -365,7 +365,7 @@ sub req_Root
} }
foreach my $line ( @gitvars ) foreach my $line ( @gitvars )
{ {
next unless ( $line =~ /^(gitcvs)\.(?:(ext|pserver)\.)?([\w-]+)=(.*)$/ ); next unless ( $line =~ /^(gitcvs|extensions)\.(?:(ext|pserver)\.)?([\w-]+)=(.*)$/ );
unless ($2) { unless ($2) {
$cfg->{$1}{$3} = $4; $cfg->{$1}{$3} = $4;
} else { } else {
@ -392,6 +392,9 @@ sub req_Root
$log->nofile(); $log->nofile();
} }
$state->{rawsz} = ($cfg->{'extensions'}{'objectformat'} || 'sha1') eq 'sha256' ? 32 : 20;
$state->{hexsz} = $state->{rawsz} * 2;
return 1; return 1;
} }
@ -1581,7 +1584,7 @@ sub req_ci
$parenthash = safe_pipe_capture('git', 'show-ref', '-s', $branchRef); $parenthash = safe_pipe_capture('git', 'show-ref', '-s', $branchRef);
chomp $parenthash; chomp $parenthash;
if ($parenthash !~ /^[0-9a-f]{40}$/) if ($parenthash !~ /^[0-9a-f]{$state->{hexsz}}$/)
{ {
if ( defined($stickyInfo) && defined($stickyInfo->{tag}) ) if ( defined($stickyInfo) && defined($stickyInfo->{tag}) )
{ {
@ -1708,7 +1711,7 @@ sub req_ci
chomp($commithash); chomp($commithash);
$log->info("Commit hash : $commithash"); $log->info("Commit hash : $commithash");
unless ( $commithash =~ /[a-zA-Z0-9]{40}/ ) unless ( $commithash =~ /[a-zA-Z0-9]{$state->{hexsz}}/ )
{ {
$log->warn("Commit failed (Invalid commit hash)"); $log->warn("Commit failed (Invalid commit hash)");
print "error 1 Commit failed (unknown reason)\n"; print "error 1 Commit failed (unknown reason)\n";
@ -2375,7 +2378,7 @@ sub req_annotate
print "E ***************\n"; print "E ***************\n";
while ( <ANNOTATE> ) while ( <ANNOTATE> )
{ {
if (m/^([a-zA-Z0-9]{40})\t\([^\)]*\)(.*)$/i) if (m/^([a-zA-Z0-9]{$state->{hexsz}})\t\([^\)]*\)(.*)$/i)
{ {
my $commithash = $1; my $commithash = $1;
my $data = $2; my $data = $2;
@ -2852,7 +2855,7 @@ sub transmitfile
return; return;
} }
die "Need filehash" unless ( defined ( $filehash ) and $filehash =~ /^[a-zA-Z0-9]{40}$/ ); die "Need filehash" unless ( defined ( $filehash ) and $filehash =~ /^[a-zA-Z0-9]{$state->{hexsz}}$/ );
my $type = safe_pipe_capture('git', 'cat-file', '-t', $filehash); my $type = safe_pipe_capture('git', 'cat-file', '-t', $filehash);
chomp $type; chomp $type;
@ -3042,7 +3045,7 @@ sub ensureWorkTree
my $ver = safe_pipe_capture('git', 'show-ref', '-s', "refs/heads/$state->{module}"); my $ver = safe_pipe_capture('git', 'show-ref', '-s', "refs/heads/$state->{module}");
chomp $ver; chomp $ver;
if ($ver !~ /^[0-9a-f]{40}$/) if ($ver !~ /^[0-9a-f]{$state->{hexsz}}$/)
{ {
$log->warn("Error from git show-ref -s refs/head$state->{module}"); $log->warn("Error from git show-ref -s refs/head$state->{module}");
print "error 1 cannot find the current HEAD of module"; print "error 1 cannot find the current HEAD of module";
@ -3281,7 +3284,7 @@ sub open_blob_or_die
} }
elsif( $srcType eq "sha1" ) elsif( $srcType eq "sha1" )
{ {
unless ( defined ( $name ) and $name =~ /^[a-zA-Z0-9]{40}$/ ) unless ( defined ( $name ) and $name =~ /^[a-zA-Z0-9]{$state->{hexsz}}$/ )
{ {
$log->warn("Need filehash"); $log->warn("Need filehash");
die "Need filehash\n"; die "Need filehash\n";
@ -3817,7 +3820,7 @@ sub update
chomp $commitsha1; chomp $commitsha1;
my $commitinfo = ::safe_pipe_capture('git', 'cat-file', 'commit', $self->{module}); my $commitinfo = ::safe_pipe_capture('git', 'cat-file', 'commit', $self->{module});
unless ( $commitinfo =~ /tree\s+[a-zA-Z0-9]{40}/ ) unless ( $commitinfo =~ /tree\s+[a-zA-Z0-9]{$state->{hexsz}}/ )
{ {
die("Invalid module '$self->{module}'"); die("Invalid module '$self->{module}'");
} }
@ -3957,7 +3960,7 @@ sub update
while ( <FILELIST> ) while ( <FILELIST> )
{ {
chomp; chomp;
unless ( /^:\d{6}\s+([0-7]{6})\s+[a-f0-9]{40}\s+([a-f0-9]{40})\s+(\w)$/o ) unless ( /^:\d{6}\s+([0-7]{6})\s+[a-f0-9]{$state->{hexsz}}\s+([a-f0-9]{$state->{hexsz}})\s+(\w)$/o )
{ {
die("Couldn't process git-diff-tree line : $_"); die("Couldn't process git-diff-tree line : $_");
} }
@ -4625,11 +4628,11 @@ sub getmeta
$db_query->execute($filename, $intRev); $db_query->execute($filename, $intRev);
$meta = $db_query->fetchrow_hashref; $meta = $db_query->fetchrow_hashref;
} }
elsif ( $revision =~ /^2\.1\.1\.2000(\.[1-3][0-9][0-9]){20}$/ ) elsif ( $revision =~ /^2\.1\.1\.2000(\.[1-3][0-9][0-9]){$state->{rawsz}}$/ )
{ {
my ($commitHash)=($revision=~/^2\.1\.1\.2000(.*)$/); my ($commitHash)=($revision=~/^2\.1\.1\.2000(.*)$/);
$commitHash=~s/\.([0-9]+)/sprintf("%02x",$1-100)/eg; $commitHash=~s/\.([0-9]+)/sprintf("%02x",$1-100)/eg;
if($commitHash=~/^[0-9a-f]{40}$/) if($commitHash=~/^[0-9a-f]{$state->{hexsz}}$/)
{ {
return $self->getMetaFromCommithash($filename,$commitHash); return $self->getMetaFromCommithash($filename,$commitHash);
} }
@ -4639,7 +4642,7 @@ sub getmeta
$log->warning("failed get $revision with commithash=$commitHash"); $log->warning("failed get $revision with commithash=$commitHash");
undef $revision; undef $revision;
} }
elsif ( $revision =~ /^[0-9a-f]{40}$/ ) elsif ( $revision =~ /^[0-9a-f]{$state->{hexsz}}$/ )
{ {
# Try DB first. This is mostly only useful for req_annotate(), # Try DB first. This is mostly only useful for req_annotate(),
# which only calls this for stuff that should already be in # which only calls this for stuff that should already be in
@ -4658,7 +4661,7 @@ sub getmeta
if(! $meta) if(! $meta)
{ {
my($revCommit)=$self->lookupCommitRef($revision); my($revCommit)=$self->lookupCommitRef($revision);
if($revCommit=~/^[0-9a-f]{40}$/) if($revCommit=~/^[0-9a-f]{$state->{hexsz}}$/)
{ {
return $self->getMetaFromCommithash($filename,$revCommit); return $self->getMetaFromCommithash($filename,$revCommit);
} }
@ -4672,7 +4675,7 @@ sub getmeta
else else
{ {
my($revCommit)=$self->lookupCommitRef($revision); my($revCommit)=$self->lookupCommitRef($revision);
if($revCommit=~/^[0-9a-f]{40}$/) if($revCommit=~/^[0-9a-f]{$state->{hexsz}}$/)
{ {
return $self->getMetaFromCommithash($filename,$revCommit); return $self->getMetaFromCommithash($filename,$revCommit);
} }
@ -4767,7 +4770,7 @@ sub getMetaFromCommithash
my($fileHash) = ::safe_pipe_capture("git","rev-parse","$revCommit:$filename"); my($fileHash) = ::safe_pipe_capture("git","rev-parse","$revCommit:$filename");
chomp $fileHash; chomp $fileHash;
if(!($fileHash=~/^[0-9a-f]{40}$/)) if(!($fileHash=~/^[0-9a-f]{$state->{hexsz}}$/))
{ {
die "Invalid fileHash '$fileHash' looking up" die "Invalid fileHash '$fileHash' looking up"
." '$revCommit:$filename'\n"; ." '$revCommit:$filename'\n";
@ -4863,7 +4866,7 @@ sub lookupCommitRef
$commitHash = ::safe_pipe_capture("git","rev-parse","--verify","--quiet", $commitHash = ::safe_pipe_capture("git","rev-parse","--verify","--quiet",
$self->unescapeRefName($ref)); $self->unescapeRefName($ref));
$commitHash=~s/\s*$//; $commitHash=~s/\s*$//;
if(!($commitHash=~/^[0-9a-f]{40}$/)) if(!($commitHash=~/^[0-9a-f]{$state->{hexsz}}$/))
{ {
$commitHash=undef; $commitHash=undef;
} }
@ -4909,7 +4912,7 @@ sub commitmessage
my $commithash = shift; my $commithash = shift;
my $tablename = $self->tablename("commitmsgs"); my $tablename = $self->tablename("commitmsgs");
die("Need commithash") unless ( defined($commithash) and $commithash =~ /^[a-zA-Z0-9]{40}$/ ); die("Need commithash") unless ( defined($commithash) and $commithash =~ /^[a-zA-Z0-9]{$state->{hexsz}}$/ );
my $db_query; my $db_query;
$db_query = $self->{dbh}->prepare_cached("SELECT value FROM $tablename WHERE key=?",{},1); $db_query = $self->{dbh}->prepare_cached("SELECT value FROM $tablename WHERE key=?",{},1);