Skip to content

Commit

Permalink
Merge branch 'bc/sha-256-cvs-svn-updates'
Browse files Browse the repository at this point in the history
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
  • Loading branch information
gitster committed Jul 7, 2020
2 parents d80bea4 + 6e9c4d4 commit e7e113a
Show file tree
Hide file tree
Showing 17 changed files with 151 additions and 111 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
8 changes: 4 additions & 4 deletions git-cvsimport.perl
Original file line number Diff line number Diff line change
Expand Up @@ -637,9 +637,9 @@ ()
return $pwd;
}

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

sub get_headref ($) {
Expand Down Expand Up @@ -810,7 +810,7 @@ ()
open(my $fh, '-|', qw(git write-tree))
or die "unable to open git write-tree: $!";
chomp(my $tree = <$fh>);
is_sha1($tree)
is_oid($tree)
or die "Cannot get tree id ($tree): $!";
close($fh)
or die "Error running git write-tree: $?\n";
Expand Down Expand Up @@ -896,7 +896,7 @@ sub commit {

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

Expand Down
37 changes: 20 additions & 17 deletions git-cvsserver.perl
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ sub req_Root
}
foreach my $line ( @gitvars )
{
next unless ( $line =~ /^(gitcvs)\.(?:(ext|pserver)\.)?([\w-]+)=(.*)$/ );
next unless ( $line =~ /^(gitcvs|extensions)\.(?:(ext|pserver)\.)?([\w-]+)=(.*)$/ );
unless ($2) {
$cfg->{$1}{$3} = $4;
} else {
Expand All @@ -392,6 +392,9 @@ sub req_Root
$log->nofile();
}

$state->{rawsz} = ($cfg->{'extensions'}{'objectformat'} || 'sha1') eq 'sha256' ? 32 : 20;
$state->{hexsz} = $state->{rawsz} * 2;

return 1;
}

Expand Down Expand Up @@ -1581,7 +1584,7 @@ sub req_ci

$parenthash = safe_pipe_capture('git', 'show-ref', '-s', $branchRef);
chomp $parenthash;
if ($parenthash !~ /^[0-9a-f]{40}$/)
if ($parenthash !~ /^[0-9a-f]{$state->{hexsz}}$/)
{
if ( defined($stickyInfo) && defined($stickyInfo->{tag}) )
{
Expand Down Expand Up @@ -1708,7 +1711,7 @@ sub req_ci
chomp($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)");
print "error 1 Commit failed (unknown reason)\n";
Expand Down Expand Up @@ -2375,7 +2378,7 @@ sub req_annotate
print "E ***************\n";
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 $data = $2;
Expand Down Expand Up @@ -2852,7 +2855,7 @@ sub transmitfile
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);
chomp $type;
Expand Down Expand Up @@ -3042,7 +3045,7 @@ sub ensureWorkTree

my $ver = safe_pipe_capture('git', 'show-ref', '-s', "refs/heads/$state->{module}");
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}");
print "error 1 cannot find the current HEAD of module";
Expand Down Expand Up @@ -3281,7 +3284,7 @@ sub open_blob_or_die
}
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");
die "Need filehash\n";
Expand Down Expand Up @@ -3817,7 +3820,7 @@ sub update
chomp $commitsha1;

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}'");
}
Expand Down Expand Up @@ -3957,7 +3960,7 @@ sub update
while ( <FILELIST> )
{
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 : $_");
}
Expand Down Expand Up @@ -4625,11 +4628,11 @@ sub getmeta
$db_query->execute($filename, $intRev);
$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(.*)$/);
$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);
}
Expand All @@ -4639,7 +4642,7 @@ sub getmeta
$log->warning("failed get $revision with commithash=$commitHash");
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(),
# which only calls this for stuff that should already be in
Expand All @@ -4658,7 +4661,7 @@ sub getmeta
if(! $meta)
{
my($revCommit)=$self->lookupCommitRef($revision);
if($revCommit=~/^[0-9a-f]{40}$/)
if($revCommit=~/^[0-9a-f]{$state->{hexsz}}$/)
{
return $self->getMetaFromCommithash($filename,$revCommit);
}
Expand All @@ -4672,7 +4675,7 @@ sub getmeta
else
{
my($revCommit)=$self->lookupCommitRef($revision);
if($revCommit=~/^[0-9a-f]{40}$/)
if($revCommit=~/^[0-9a-f]{$state->{hexsz}}$/)
{
return $self->getMetaFromCommithash($filename,$revCommit);
}
Expand Down Expand Up @@ -4767,7 +4770,7 @@ sub getMetaFromCommithash

my($fileHash) = ::safe_pipe_capture("git","rev-parse","$revCommit:$filename");
chomp $fileHash;
if(!($fileHash=~/^[0-9a-f]{40}$/))
if(!($fileHash=~/^[0-9a-f]{$state->{hexsz}}$/))
{
die "Invalid fileHash '$fileHash' looking up"
." '$revCommit:$filename'\n";
Expand Down Expand Up @@ -4863,7 +4866,7 @@ sub lookupCommitRef
$commitHash = ::safe_pipe_capture("git","rev-parse","--verify","--quiet",
$self->unescapeRefName($ref));
$commitHash=~s/\s*$//;
if(!($commitHash=~/^[0-9a-f]{40}$/))
if(!($commitHash=~/^[0-9a-f]{$state->{hexsz}}$/))
{
$commitHash=undef;
}
Expand Down Expand Up @@ -4909,7 +4912,7 @@ sub commitmessage
my $commithash = shift;
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;
$db_query = $self->{dbh}->prepare_cached("SELECT value FROM $tablename WHERE key=?",{},1);
Expand Down
25 changes: 17 additions & 8 deletions git-svn.perl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
use warnings;
use strict;
use vars qw/ $AUTHOR $VERSION
$sha1 $sha1_short $_revision $_repository
$oid $oid_short $oid_length
$_revision $_repository
$_q $_authors $_authors_prog %users/;
$AUTHOR = 'Eric Wong <normalperson@yhbt.net>';
$VERSION = '@@GIT_VERSION@@';
Expand Down Expand Up @@ -103,8 +104,9 @@ sub _req_svn {
}
}

$sha1 = qr/[a-f\d]{40}/;
$sha1_short = qr/[a-f\d]{4,40}/;
$oid = qr/(?:[a-f\d]{40}(?:[a-f\d]{24})?)/;
$oid_short = qr/[a-f\d]{4,64}/;
$oid_length = 40;
my ($_stdin, $_help, $_edit,
$_message, $_file, $_branch_dest,
$_template, $_shared,
Expand Down Expand Up @@ -498,6 +500,7 @@ sub do_git_init_db {
command_noisy('config', "$pfx.preserve-empty-dirs", 'true');
command_noisy('config', "$pfx.placeholder-filename", $$fname);
}
load_object_format();
}

sub init_subdir {
Expand Down Expand Up @@ -582,7 +585,7 @@ sub cmd_set_tree {
print "Reading from stdin...\n";
@commits = ();
while (<STDIN>) {
if (/\b($sha1_short)\b/o) {
if (/\b($oid_short)\b/o) {
unshift @commits, $1;
}
}
Expand Down Expand Up @@ -1831,7 +1834,7 @@ sub get_tree_from_treeish {
if ($type eq 'commit') {
$expected = (grep /^tree /, command(qw/cat-file commit/,
$treeish))[0];
($expected) = ($expected =~ /^tree ($sha1)$/o);
($expected) = ($expected =~ /^tree ($oid)$/o);
die "Unable to get tree from $treeish\n" unless $expected;
} elsif ($type eq 'tree') {
$expected = $treeish;
Expand Down Expand Up @@ -1975,9 +1978,15 @@ sub read_git_config {
}
}
}
load_object_format();
delete @$opts{@config_only} if @config_only;
}

sub load_object_format {
chomp(my $hash = `git config --get extensions.objectformat`);
$::oid_length = 64 if $hash eq 'sha256';
}

sub extract_metadata {
my $id = shift or return (undef, undef, undef);
my ($url, $rev, $uuid) = ($id =~ /^\s*git-svn-id:\s+(.*)\@(\d+)
Expand Down Expand Up @@ -2006,10 +2015,10 @@ sub cmt_sha2rev_batch {
print $out $sha, "\n";

while (my $line = <$in>) {
if ($first && $line =~ /^[[:xdigit:]]{40}\smissing$/) {
if ($first && $line =~ /^$::oid\smissing$/) {
last;
} elsif ($first &&
$line =~ /^[[:xdigit:]]{40}\scommit\s(\d+)$/) {
$line =~ /^$::oid\scommit\s(\d+)$/) {
$first = 0;
$size = $1;
next;
Expand All @@ -2036,7 +2045,7 @@ sub working_head_info {
my $hash;
my %max;
while (<$fh>) {
if ( m{^commit ($::sha1)$} ) {
if ( m{^commit ($::oid)$} ) {
unshift @$refs, $hash if $hash and $refs;
$hash = $1;
next;
Expand Down
6 changes: 4 additions & 2 deletions perl/Git/IndexInfo.pm
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ use Git qw/command_input_pipe command_close_pipe/;

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

sub remove {
my ($self, $path) = @_;
if (print { $self->{gui} } '0 ', 0 x 40, "\t", $path, "\0") {
my $length = $self->{hash_algo} eq 'sha256' ? 64 : 40;
if (print { $self->{gui} } '0 ', 0 x $length, "\t", $path, "\0") {
return ++$self->{nr};
}
undef;
Expand Down

0 comments on commit e7e113a

Please sign in to comment.