git-tar-tree: no more void pointer arithmetic
[git.git] / git-svnimport.perl
index ee2940f..38ac732 100755 (executable)
@@ -13,6 +13,7 @@
 use strict;
 use warnings;
 use Getopt::Std;
+use File::Copy;
 use File::Spec;
 use File::Temp qw(tempfile);
 use File::Path qw(mkpath);
@@ -29,19 +30,21 @@ die "Need SVN:Core 1.2.1 or better" if $SVN::Core::VERSION lt "1.2.1";
 $SIG{'PIPE'}="IGNORE";
 $ENV{'TZ'}="UTC";
 
-our($opt_h,$opt_o,$opt_v,$opt_u,$opt_C,$opt_i,$opt_m,$opt_M,$opt_t,$opt_T,$opt_b,$opt_r,$opt_s,$opt_l,$opt_d,$opt_D);
+our($opt_h,$opt_o,$opt_v,$opt_u,$opt_C,$opt_i,$opt_m,$opt_M,$opt_t,$opt_T,
+    $opt_b,$opt_r,$opt_I,$opt_A,$opt_s,$opt_l,$opt_d,$opt_D);
 
 sub usage() {
        print STDERR <<END;
 Usage: ${\basename $0}     # fetch/update GIT from SVN
        [-o branch-for-HEAD] [-h] [-v] [-l max_rev]
        [-C GIT_repository] [-t tagname] [-T trunkname] [-b branchname]
-       [-d|-D] [-i] [-u] [-r] [-s start_chg] [-m] [-M regex] [SVN_URL]
+       [-d|-D] [-i] [-u] [-r] [-I ignorefilename] [-s start_chg]
+       [-m] [-M regex] [-A author_file] [SVN_URL]
 END
        exit(1);
 }
 
-getopts("b:C:dDhil:mM:o:rs:t:T:uv") or usage();
+getopts("A:b:C:dDhiI:l:mM:o:rs:t:T:uv") or usage();
 usage if $opt_h;
 
 my $tag_name = $opt_t || "tags";
@@ -60,10 +63,36 @@ my $svn_dir = $ARGV[1];
 
 our @mergerx = ();
 if ($opt_m) {
-       @mergerx = ( qr/\W(?:from|of|merge|merging|merged) (\w+)/i );
+       my $branch_esc = quotemeta ($branch_name);
+       my $trunk_esc  = quotemeta ($trunk_name);
+       @mergerx =
+       (
+               qr!\b(?:merg(?:ed?|ing))\b.*?\b((?:(?<=$branch_esc/)[\w\.\-]+)|(?:$trunk_esc))\b!i,
+               qr!\b(?:from|of)\W+((?:(?<=$branch_esc/)[\w\.\-]+)|(?:$trunk_esc))\b!i,
+               qr!\b(?:from|of)\W+(?:the )?([\w\.\-]+)[-\s]branch\b!i
+       );
 }
 if ($opt_M) {
-       push (@mergerx, qr/$opt_M/);
+       unshift (@mergerx, qr/$opt_M/);
+}
+
+# Absolutize filename now, since we will have chdir'ed by the time we
+# get around to opening it.
+$opt_A = File::Spec->rel2abs($opt_A) if $opt_A;
+
+our %users = ();
+our $users_file = undef;
+sub read_users($) {
+       $users_file = File::Spec->rel2abs(@_);
+       die "Cannot open $users_file\n" unless -f $users_file;
+       open(my $authors,$users_file);
+       while(<$authors>) {
+               chomp;
+               next unless /^(\S+?)\s*=\s*(.+?)\s*<(.+)>\s*$/;
+               (my $user,my $name,my $email) = ($1,$2,$3);
+               $users{$user} = [$name,$email];
+       }
+       close($authors);
 }
 
 select(STDERR); $|=1; select(STDOUT);
@@ -76,6 +105,7 @@ package SVNconn;
 use File::Spec;
 use File::Temp qw(tempfile);
 use POSIX qw(strftime dup2);
+use Fcntl qw(SEEK_SET);
 
 sub new {
        my($what,$repo) = @_;
@@ -112,16 +142,55 @@ sub file {
                    DIR => File::Spec->tmpdir(), UNLINK => 1);
 
        print "... $rev $path ...\n" if $opt_v;
+       my (undef, $properties);
        my $pool = SVN::Pool->new();
-       eval { $self->{'svn'}->get_file($path,$rev,$fh,$pool); };
+       eval { (undef, $properties)
+                  = $self->{'svn'}->get_file($path,$rev,$fh,$pool); };
        $pool->clear;
        if($@) {
                return undef if $@ =~ /Attempted to get checksum/;
                die $@;
        }
+       my $mode;
+       if (exists $properties->{'svn:executable'}) {
+               $mode = '100755';
+       } elsif (exists $properties->{'svn:special'}) {
+               my ($special_content, $filesize);
+               $filesize = tell $fh;
+               seek $fh, 0, SEEK_SET;
+               read $fh, $special_content, $filesize;
+               if ($special_content =~ s/^link //) {
+                       $mode = '120000';
+                       seek $fh, 0, SEEK_SET;
+                       truncate $fh, 0;
+                       print $fh $special_content;
+               } else {
+                       die "unexpected svn:special file encountered";
+               }
+       } else {
+               $mode = '100644';
+       }
        close ($fh);
 
-       return $name;
+       return ($name, $mode);
+}
+
+sub ignore {
+       my($self,$path,$rev) = @_;
+
+       print "... $rev $path ...\n" if $opt_v;
+       my (undef,undef,$properties)
+           = $self->{'svn'}->get_dir($path,$rev,undef);
+       if (exists $properties->{'svn:ignore'}) {
+               my ($fh, $name) = tempfile('gitsvn.XXXXXX',
+                                          DIR => File::Spec->tmpdir(),
+                                          UNLINK => 1);
+               print $fh $properties->{'svn:ignore'};
+               close($fh);
+               return $name;
+       } else {
+               return undef;
+       }
 }
 
 package main;
@@ -263,6 +332,14 @@ EOM
 -d $git_dir
        or die "Could not create git subdir ($git_dir).\n";
 
+my $default_authors = "$git_dir/svn-authors";
+if ($opt_A) {
+       read_users($opt_A);
+       copy($opt_A,$default_authors) or die "Copy failed: $!";
+} else {
+       read_users($default_authors) if -f $default_authors;
+}
+
 open BRANCHES,">>", "$git_dir/svn2git";
 
 sub node_kind($$$) {
@@ -296,7 +373,7 @@ sub get_file($$$) {
        my $svnpath = revert_split_path($branch,$path);
 
        # now get it
-       my $name;
+       my ($name,$mode);
        if($opt_d) {
                my($req,$res);
 
@@ -316,8 +393,9 @@ sub get_file($$$) {
                        return undef if $res->code == 301; # directory?
                        die $res->status_line." at $url\n";
                }
+               $mode = '0644'; # can't obtain mode via direct http request?
        } else {
-               $name = $svn->file("$svnpath",$rev);
+               ($name,$mode) = $svn->file("$svnpath",$rev);
                return undef unless defined $name;
        }
 
@@ -331,10 +409,37 @@ sub get_file($$$) {
        chomp $sha;
        close $F;
        unlink $name;
-       my $mode = "0644"; # SV does not seem to store any file modes
        return [$mode, $sha, $path];
 }
 
+sub get_ignore($$$$$) {
+       my($new,$old,$rev,$branch,$path) = @_;
+
+       return unless $opt_I;
+       my $svnpath = revert_split_path($branch,$path);
+       my $name = $svn->ignore("$svnpath",$rev);
+       if ($path eq '/') {
+               $path = $opt_I;
+       } else {
+               $path = File::Spec->catfile($path,$opt_I);
+       }
+       if (defined $name) {
+               my $pid = open(my $F, '-|');
+               die $! unless defined $pid;
+               if (!$pid) {
+                       exec("git-hash-object", "-w", $name)
+                           or die "Cannot create object: $!\n";
+               }
+               my $sha = <$F>;
+               chomp $sha;
+               close $F;
+               unlink $name;
+               push(@$new,['0644',$sha,$path]);
+       } else {
+               push(@$old,$path);
+       }
+}
+
 sub split_path($$) {
        my($rev,$path) = @_;
        my $branch;
@@ -431,6 +536,10 @@ sub commit {
 
        if (not defined $author) {
                $author_name = $author_email = "unknown";
+       } elsif (defined $users_file) {
+               die "User $author is not listed in $users_file\n"
+                   unless exists $users{$author};
+               ($author_name,$author_email) = @{$users{$author}};
        } elsif ($author =~ /^(.*?)\s+<(.*)>$/) {
                ($author_name, $author_email) = ($1, $2);
        } else {
@@ -530,9 +639,7 @@ sub commit {
                        }
                        if(($action->[0] eq "A") || ($action->[0] eq "R")) {
                                my $node_kind = node_kind($branch,$path,$revision);
-                               if($action->[1]) {
-                                       copy_path($revision,$branch,$path,$action->[1],$action->[2],$node_kind,\@new,\@parents);
-                               } elsif ($node_kind eq $SVN::Node::file) {
+                               if ($node_kind eq $SVN::Node::file) {
                                        my $f = get_file($revision,$branch,$path);
                                        if ($f) {
                                                push(@new,$f) if $f;
@@ -540,6 +647,16 @@ sub commit {
                                                my $opath = $action->[3];
                                                print STDERR "$revision: $branch: could not fetch '$opath'\n";
                                        }
+                               } elsif ($node_kind eq $SVN::Node::dir) {
+                                       if($action->[1]) {
+                                               copy_path($revision, $branch,
+                                                         $path, $action->[1],
+                                                         $action->[2], $node_kind,
+                                                         \@new, \@parents);
+                                       } else {
+                                               get_ignore(\@new, \@old, $revision,
+                                                          $branch, $path);
+                                       }
                                }
                        } elsif ($action->[0] eq "D") {
                                push(@old,$path);
@@ -548,6 +665,9 @@ sub commit {
                                if ($node_kind eq $SVN::Node::file) {
                                        my $f = get_file($revision,$branch,$path);
                                        push(@new,$f) if $f;
+                               } elsif ($node_kind eq $SVN::Node::dir) {
+                                       get_ignore(\@new, \@old, $revision,
+                                                  $branch,$path);
                                }
                        } else {
                                die "$revision: unknown action '".$action->[0]."' for $path\n";
@@ -759,7 +879,7 @@ sub commit_all {
 
 $opt_l = $svn->{'maxrev'} if not defined $opt_l or $opt_l > $svn->{'maxrev'};
 
-if ($svn->{'maxrev'} < $current_rev) {
+if ($opt_l < $current_rev) {
     print "Up to date: no new revisions to fetch!\n" if $opt_v;
     unlink("$git_dir/SVN2GIT_HEAD");
     exit;