Use a *real* built-in diff generator
[git.git] / git-svnimport.perl
index c536d70..639aa41 100755 (executable)
 # The head revision is on branch "origin" by default.
 # You can change that with the '-o' option.
 
-require 5.008; # for shell-safe open("-|",LIST)
 use strict;
 use warnings;
 use Getopt::Std;
+use File::Copy;
 use File::Spec;
 use File::Temp qw(tempfile);
 use File::Path qw(mkpath);
@@ -30,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";
@@ -67,6 +69,25 @@ if ($opt_M) {
        push (@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);
 
 
@@ -113,16 +134,40 @@ sub file {
                    DIR => File::Spec->tmpdir(), UNLINK => 1);
 
        print "... $rev $path ...\n" if $opt_v;
-       my $pool = SVN::Pool->new();
-       eval { $self->{'svn'}->get_file($path,$rev,$fh,$pool); };
-       $pool->clear;
+       my (undef, $properties);
+       eval { (undef, $properties)
+                  = $self->{'svn'}->get_file($path,$rev,$fh); };
        if($@) {
                return undef if $@ =~ /Attempted to get checksum/;
                die $@;
        }
+       my $mode;
+       if (exists $properties->{'svn:executable'}) {
+               $mode = '0755';
+       } else {
+               $mode = '0644';
+       }
        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;
@@ -264,6 +309,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($$$) {
@@ -297,7 +350,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);
 
@@ -317,21 +370,53 @@ 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;
        }
 
-       open my $F, '-|', "git-hash-object", "-w", $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;
-       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;
@@ -398,7 +483,12 @@ sub copy_path($$$$$$$$) {
                        $srcpath =~ s#/*$#/#;
        }
        
-       open my $f,"-|","git-ls-tree","-r","-z",$gitrev,$srcpath;
+       my $pid = open my $f,'-|';
+       die $! unless defined $pid;
+       if (!$pid) {
+               exec("git-ls-tree","-r","-z",$gitrev,$srcpath)
+                       or die $!;
+       }
        local $/ = "\0";
        while(<$f>) {
                chomp;
@@ -423,6 +513,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 {
@@ -532,6 +626,9 @@ sub commit {
                                                my $opath = $action->[3];
                                                print STDERR "$revision: $branch: could not fetch '$opath'\n";
                                        }
+                               } elsif ($node_kind eq $SVN::Node::dir) {
+                                       get_ignore(\@new, \@old, $revision,
+                                                  $branch,$path);
                                }
                        } elsif ($action->[0] eq "D") {
                                push(@old,$path);
@@ -540,6 +637,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";
@@ -554,7 +654,11 @@ sub commit {
                                @o1 = @old;
                                @old = ();
                        }
-                       open my $F, "-|", "git-ls-files", "-z", @o1 or die $!;
+                       my $pid = open my $F, "-|";
+                       die "$!" unless defined $pid;
+                       if (!$pid) {
+                               exec("git-ls-files", "-z", @o1) or die $!;
+                       }
                        @o1 = ();
                        local $/ = "\0";
                        while(<$F>) {