]> asedeno.scripts.mit.edu Git - git.git/blobdiff - git-svn.perl
Merge branch 'jc/spht'
[git.git] / git-svn.perl
index 98c980fa8fc97b3811f8edba2c67ea3a8e174631..9f884eb2132c76b86475b97256e0ed565f84bb2e 100755 (executable)
@@ -35,6 +35,7 @@ push @Git::SVN::Ra::ISA, 'SVN::Ra';
 push @SVN::Git::Editor::ISA, 'SVN::Delta::Editor';
 push @SVN::Git::Fetcher::ISA, 'SVN::Delta::Editor';
 use Carp qw/croak/;
+use Digest::MD5;
 use IO::File qw//;
 use File::Basename qw/dirname basename/;
 use File::Path qw/mkpath/;
@@ -48,8 +49,7 @@ BEGIN {
        foreach (qw/command command_oneline command_noisy command_output_pipe
                    command_input_pipe command_close_pipe/) {
                for my $package ( qw(SVN::Git::Editor SVN::Git::Fetcher
-                       Git::SVN::Migration Git::SVN::Log Git::SVN
-                       Git::SVN::Util),
+                       Git::SVN::Migration Git::SVN::Log Git::SVN),
                        __PACKAGE__) {
                        *{"${package}::$_"} = \&{"Git::$_"};
                }
@@ -65,7 +65,7 @@ my ($_stdin, $_help, $_edit,
        $_template, $_shared,
        $_version, $_fetch_all, $_no_rebase,
        $_merge, $_strategy, $_dry_run, $_local,
-       $_prefix, $_no_checkout, $_verbose);
+       $_prefix, $_no_checkout, $_url, $_verbose);
 $Git::SVN::_follow_parent = 1;
 my %remote_opts = ( 'username=s' => \$Git::SVN::Prompt::_username,
                     'config-dir=s' => \$Git::SVN::Ra::config_dir,
@@ -81,6 +81,7 @@ my %fc_opts = ( 'follow-parent|follow!' => \$Git::SVN::_follow_parent,
                'quiet|q' => \$_q,
                'repack-flags|repack-args|repack-opts=s' =>
                   \$Git::SVN::_repack_flags,
+               'use-log-author' => \$Git::SVN::_use_log_author,
                %remote_opts );
 
 my ($_trunk, $_tags, $_branches, $_stdlayout);
@@ -142,6 +143,9 @@ my %cmd = (
        'show-ignore' => [ \&cmd_show_ignore, "Show svn:ignore listings",
                        { 'revision|r=i' => \$_revision
                        } ],
+       'show-externals' => [ \&cmd_show_externals, "Show svn:externals listings",
+                       { 'revision|r=i' => \$_revision
+                       } ],
        'multi-fetch' => [ \&cmd_multi_fetch,
                           "Deprecated alias for $0 fetch --all",
                           { 'revision|r=s' => \$_revision, %fc_opts } ],
@@ -178,6 +182,10 @@ my %cmd = (
                          'file|F=s' => \$_file,
                          'revision|r=s' => \$_revision,
                        %cmt_opts } ],
+       'info' => [ \&cmd_info,
+                   "Show info about the latest SVN revision
+                    on the current branch",
+                   { 'url' => \$_url, } ],
 );
 
 my $cmd;
@@ -189,23 +197,6 @@ for (my $i = 0; $i < @ARGV; $i++) {
        }
 };
 
-my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
-
-read_repo_config(\%opts);
-Getopt::Long::Configure('pass_through') if ($cmd && $cmd eq 'log');
-my $rv = GetOptions(%opts, 'help|H|h' => \$_help, 'version|V' => \$_version,
-                    'minimize-connections' => \$Git::SVN::Migration::_minimize,
-                    'id|i=s' => \$Git::SVN::default_ref_id,
-                    'svn-remote|remote|R=s' => sub {
-                       $Git::SVN::no_reuse_existing = 1;
-                       $Git::SVN::default_repo_id = $_[1] });
-exit 1 if (!$rv && $cmd && $cmd ne 'log');
-
-usage(0) if $_help;
-version() if $_version;
-usage(1) unless defined $cmd;
-load_authors() if $_authors;
-
 # make sure we're always running
 unless ($cmd =~ /(?:clone|init|multi-init)$/) {
        unless (-d $ENV{GIT_DIR}) {
@@ -227,6 +218,24 @@ unless ($cmd =~ /(?:clone|init|multi-init)$/) {
                $ENV{GIT_DIR} = $git_dir;
        }
 }
+
+my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
+
+read_repo_config(\%opts);
+Getopt::Long::Configure('pass_through') if ($cmd && $cmd eq 'log');
+my $rv = GetOptions(%opts, 'help|H|h' => \$_help, 'version|V' => \$_version,
+                    'minimize-connections' => \$Git::SVN::Migration::_minimize,
+                    'id|i=s' => \$Git::SVN::default_ref_id,
+                    'svn-remote|remote|R=s' => sub {
+                       $Git::SVN::no_reuse_existing = 1;
+                       $Git::SVN::default_repo_id = $_[1] });
+exit 1 if (!$rv && $cmd && $cmd ne 'log');
+
+usage(0) if $_help;
+version() if $_version;
+usage(1) unless defined $cmd;
+load_authors() if $_authors;
+
 unless ($cmd =~ /^(?:clone|init|multi-init|commit-diff)$/) {
        Git::SVN::Migration::migration_check();
 }
@@ -541,6 +550,8 @@ sub cmd_rebase {
                exit 1;
        }
        unless ($_local) {
+               # rebase will checkout for us, so no need to do it explicitly
+               $_no_checkout = 'true';
                $_fetch_all ? $gs->fetch_all : $gs->fetch;
        }
        command_noisy(rebase_cmd(), $gs->refname);
@@ -561,6 +572,21 @@ sub cmd_show_ignore {
        });
 }
 
+sub cmd_show_externals {
+       my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
+       $gs ||= Git::SVN->new;
+       my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
+       $gs->prop_walk($gs->{path}, $r, sub {
+               my ($gs, $path, $props) = @_;
+               print STDOUT "\n# $path\n";
+               my $s = $props->{'svn:externals'} or return;
+               $s =~ s/[\r\n]+/\n/g;
+               chomp $s;
+               $s =~ s#^#$path#gm;
+               print STDOUT "$s\n";
+       });
+}
+
 sub cmd_create_ignore {
        my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
        $gs ||= Git::SVN->new;
@@ -586,12 +612,18 @@ sub cmd_create_ignore {
 
 sub canonicalize_path {
        my ($path) = @_;
+       my $dot_slash_added = 0;
+       if (substr($path, 0, 1) ne "/") {
+               $path = "./" . $path;
+               $dot_slash_added = 1;
+       }
        # File::Spec->canonpath doesn't collapse x/../y into y (for a
        # good reason), so let's do this manually.
        $path =~ s#/+#/#g;
        $path =~ s#/\.(?:/|$)#/#g;
        $path =~ s#/[^/]+/\.\.##g;
        $path =~ s#/$##g;
+       $path =~ s#^\./## if $dot_slash_added;
        return $path;
 }
 
@@ -743,6 +775,114 @@ sub cmd_commit_diff {
        }
 }
 
+sub cmd_info {
+       my $path = canonicalize_path(shift or ".");
+       unless (scalar(@_) == 0) {
+               die "Too many arguments specified\n";
+       }
+
+       my ($file_type, $diff_status) = find_file_type_and_diff_status($path);
+
+       if (!$file_type && !$diff_status) {
+               print STDERR "$path:  (Not a versioned resource)\n\n";
+               return;
+       }
+
+       my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
+       unless ($gs) {
+               die "Unable to determine upstream SVN information from ",
+                   "working tree history\n";
+       }
+       my $full_url = $url . ($path eq "." ? "" : "/$path");
+
+       if ($_url) {
+               print $full_url, "\n";
+               return;
+       }
+
+       my $result = "Path: $path\n";
+       $result .= "Name: " . basename($path) . "\n" if $file_type ne "dir";
+       $result .= "URL: " . $full_url . "\n";
+
+       eval {
+               my $repos_root = $gs->repos_root;
+               Git::SVN::remove_username($repos_root);
+               $result .= "Repository Root: $repos_root\n";
+       };
+       if ($@) {
+               $result .= "Repository Root: (offline)\n";
+       }
+       $result .= "Repository UUID: $uuid\n" unless $diff_status eq "A";
+       $result .= "Revision: " . ($diff_status eq "A" ? 0 : $rev) . "\n";
+
+       $result .= "Node Kind: " .
+                  ($file_type eq "dir" ? "directory" : "file") . "\n";
+
+       my $schedule = $diff_status eq "A"
+                      ? "add"
+                      : ($diff_status eq "D" ? "delete" : "normal");
+       $result .= "Schedule: $schedule\n";
+
+       if ($diff_status eq "A") {
+               print $result, "\n";
+               return;
+       }
+
+       my ($lc_author, $lc_rev, $lc_date_utc);
+       my @args = Git::SVN::Log::git_svn_log_cmd($rev, $rev, "--", $path);
+       my $log = command_output_pipe(@args);
+       my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
+       while (<$log>) {
+               if (/^${esc_color}author (.+) <[^>]+> (\d+) ([\-\+]?\d+)$/o) {
+                       $lc_author = $1;
+                       $lc_date_utc = Git::SVN::Log::parse_git_date($2, $3);
+               } elsif (/^${esc_color}    (git-svn-id:.+)$/o) {
+                       (undef, $lc_rev, undef) = ::extract_metadata($1);
+               }
+       }
+       close $log;
+
+       Git::SVN::Log::set_local_timezone();
+
+       $result .= "Last Changed Author: $lc_author\n";
+       $result .= "Last Changed Rev: $lc_rev\n";
+       $result .= "Last Changed Date: " .
+                  Git::SVN::Log::format_svn_date($lc_date_utc) . "\n";
+
+       if ($file_type ne "dir") {
+               my $text_last_updated_date =
+                   ($diff_status eq "D" ? $lc_date_utc : (stat $path)[9]);
+               $result .=
+                   "Text Last Updated: " .
+                   Git::SVN::Log::format_svn_date($text_last_updated_date) .
+                   "\n";
+               my $checksum;
+               if ($diff_status eq "D") {
+                       my ($fh, $ctx) =
+                           command_output_pipe(qw(cat-file blob), "HEAD:$path");
+                       if ($file_type eq "link") {
+                               my $file_name = <$fh>;
+                               $checksum = md5sum("link $file_name");
+                       } else {
+                               $checksum = md5sum($fh);
+                       }
+                       command_close_pipe($fh, $ctx);
+               } elsif ($file_type eq "link") {
+                       my $file_name =
+                           command(qw(cat-file blob), "HEAD:$path");
+                       $checksum =
+                           md5sum("link " . $file_name);
+               } else {
+                       open FILE, "<", $path or die $!;
+                       $checksum = md5sum(\*FILE);
+                       close FILE or die $!;
+               }
+               $result .= "Checksum: " . $checksum . "\n";
+       }
+
+       print $result, "\n";
+}
+
 ########################### utility functions #########################
 
 sub rebase_cmd {
@@ -1050,10 +1190,30 @@ sub linearize_history {
        (\@linear_refs, \%parents);
 }
 
-package Git::SVN::Util;
-use strict;
-use warnings;
-use Digest::MD5;
+sub find_file_type_and_diff_status {
+       my ($path) = @_;
+       return ('dir', '') if $path eq '.';
+
+       my $diff_output =
+           command_oneline(qw(diff --cached --name-status --), $path) || "";
+       my $diff_status = (split(' ', $diff_output))[0] || "";
+
+       my $ls_tree = command_oneline(qw(ls-tree HEAD), $path) || "";
+
+       return (undef, undef) if !$diff_status && !$ls_tree;
+
+       if ($diff_status eq "A") {
+               return ("link", $diff_status) if -l $path;
+               return ("dir", $diff_status) if -d $path;
+               return ("file", $diff_status);
+       }
+
+       my $mode = (split(' ', $ls_tree))[0] || "";
+
+       return ("link", $diff_status) if $mode eq "120000";
+       return ("dir", $diff_status) if $mode eq "040000";
+       return ("file", $diff_status);
+}
 
 sub md5sum {
        my $arg = shift;
@@ -1076,7 +1236,8 @@ use strict;
 use warnings;
 use vars qw/$default_repo_id $default_ref_id $_no_metadata $_follow_parent
             $_repack $_repack_flags $_use_svm_props $_head
-            $_use_svnsync_props $no_reuse_existing $_minimize_url/;
+            $_use_svnsync_props $no_reuse_existing $_minimize_url
+           $_use_log_author/;
 use Carp qw/croak/;
 use File::Path qw/mkpath/;
 use File::Copy qw/copy/;
@@ -1635,9 +1796,24 @@ sub ra_uuid {
        $self->{ra_uuid};
 }
 
+sub _set_repos_root {
+       my ($self, $repos_root) = @_;
+       my $k = "svn-remote.$self->{repo_id}.reposRoot";
+       $repos_root ||= $self->ra->{repos_root};
+       tmp_config($k, $repos_root);
+       $repos_root;
+}
+
+sub repos_root {
+       my ($self) = @_;
+       my $k = "svn-remote.$self->{repo_id}.reposRoot";
+       eval { tmp_config('--get', $k) } || $self->_set_repos_root;
+}
+
 sub ra {
        my ($self) = shift;
        my $ra = Git::SVN::Ra->new($self->{url});
+       $self->_set_repos_root($ra->{repos_root});
        if ($self->use_svm_props && !$self->{svm}) {
                if ($self->no_metadata) {
                        die "Can't have both 'noMetadata' and ",
@@ -1901,11 +2077,17 @@ sub do_git_commit {
                croak "$log_entry->{revision} = $c already exists! ",
                      "Why are we refetching it?\n";
        }
-       $ENV{GIT_AUTHOR_NAME} = $ENV{GIT_COMMITTER_NAME} = $log_entry->{name};
-       $ENV{GIT_AUTHOR_EMAIL} = $ENV{GIT_COMMITTER_EMAIL} =
-                                                         $log_entry->{email};
+       $ENV{GIT_AUTHOR_NAME} = $log_entry->{name};
+       $ENV{GIT_AUTHOR_EMAIL} = $log_entry->{email};
        $ENV{GIT_AUTHOR_DATE} = $ENV{GIT_COMMITTER_DATE} = $log_entry->{date};
 
+       $ENV{GIT_COMMITTER_NAME} = (defined $log_entry->{commit_name})
+                                               ? $log_entry->{commit_name}
+                                               : $log_entry->{name};
+       $ENV{GIT_COMMITTER_EMAIL} = (defined $log_entry->{commit_email})
+                                               ? $log_entry->{commit_email}
+                                               : $log_entry->{email};
+
        my $tree = $log_entry->{tree};
        if (!defined $tree) {
                $tree = $self->tmp_index_do(sub {
@@ -2193,7 +2375,17 @@ sub make_log_entry {
        $log_entry{log} .= "\n";
        my $author = $log_entry{author} = check_author($log_entry{author});
        my ($name, $email) = defined $::users{$author} ? @{$::users{$author}}
-                                                      : ($author, undef);
+                                                      : ($author, undef);
+
+       my ($commit_name, $commit_email) = ($name, $email);
+       if ($_use_log_author) {
+               if ($log_entry{log} =~ /From:\s+(.*?)\s+<(.*)>\s*\n/) {
+                       ($name, $email) = ($1, $2);
+               } elsif ($log_entry{log} =~
+                                     /Signed-off-by:\s+(.*?)\s+<(.*)>\s*\n/) {
+                       ($name, $email) = ($1, $2);
+               }
+       }
        if (defined $headrev && $self->use_svm_props) {
                if ($self->rewrite_root) {
                        die "Can't have both 'useSvmProps' and 'rewriteRoot' ",
@@ -2216,23 +2408,28 @@ sub make_log_entry {
                remove_username($full_url);
                $log_entry{metadata} = "$full_url\@$r $uuid";
                $log_entry{svm_revision} = $r;
-               $email ||= "$author\@$uuid"
+               $email ||= "$author\@$uuid";
+               $commit_email ||= "$author\@$uuid";
        } elsif ($self->use_svnsync_props) {
                my $full_url = $self->svnsync->{url};
                $full_url .= "/$self->{path}" if length $self->{path};
                remove_username($full_url);
                my $uuid = $self->svnsync->{uuid};
                $log_entry{metadata} = "$full_url\@$rev $uuid";
-               $email ||= "$author\@$uuid"
+               $email ||= "$author\@$uuid";
+               $commit_email ||= "$author\@$uuid";
        } else {
                my $url = $self->metadata_url;
                remove_username($url);
                $log_entry{metadata} = "$url\@$rev " .
                                       $self->ra->get_uuid;
                $email ||= "$author\@" . $self->ra->get_uuid;
+               $commit_email ||= "$author\@" . $self->ra->get_uuid;
        }
        $log_entry{name} = $name;
        $log_entry{email} = $email;
+       $log_entry{commit_name} = $commit_name;
+       $log_entry{commit_email} = $commit_email;
        \%log_entry;
 }
 
@@ -2789,7 +2986,7 @@ sub apply_textdelta {
 
                if (defined $exp) {
                        seek $base, 0, 0 or croak $!;
-                       my $got = Git::SVN::Util::md5sum($base);
+                       my $got = ::md5sum($base);
                        die "Checksum mismatch: $fb->{path} $fb->{blob}\n",
                            "expected: $exp\n",
                            "     got: $got\n" if ($got ne $exp);
@@ -2808,7 +3005,7 @@ sub close_file {
        if (my $fh = $fb->{fh}) {
                if (defined $exp) {
                        seek($fh, 0, 0) or croak $!;
-                       my $got = Git::SVN::Util::md5sum($fh);
+                       my $got = ::md5sum($fh);
                        if ($got ne $exp) {
                                die "Checksum mismatch: $path\n",
                                    "expected: $exp\n    got: $got\n";
@@ -3163,7 +3360,7 @@ sub chg_file {
        $fh->flush == 0 or croak $!;
        seek $fh, 0, 0 or croak $!;
 
-       my $exp = Git::SVN::Util::md5sum($fh);
+       my $exp = ::md5sum($fh);
        seek $fh, 0, 0 or croak $!;
 
        my $pool = SVN::Pool->new;