Merge branch 'master' of git://repo.or.cz/git-gui
[gitweb.git] / git-svn.perl
index b4e8966919e67bc8aff3235aa2cd3da706dc928a..326e89fe037561c1ad72d91c7783bf2a0f603826 100755 (executable)
@@ -9,6 +9,7 @@
 $AUTHOR = 'Eric Wong <normalperson@yhbt.net>';
 $VERSION = '@@GIT_VERSION@@';
 
+my $git_dir_user_set = 1 if defined $ENV{GIT_DIR};
 $ENV{GIT_DIR} ||= '.git';
 $Git::SVN::default_repo_id = 'svn';
 $Git::SVN::default_ref_id = $ENV{GIT_SVN_ID} || 'git-svn';
@@ -178,6 +179,28 @@ BEGIN
 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}) {
+               if ($git_dir_user_set) {
+                       die "GIT_DIR=$ENV{GIT_DIR} explicitly set, ",
+                           "but it is not a directory\n";
+               }
+               my $git_dir = delete $ENV{GIT_DIR};
+               chomp(my $cdup = command_oneline(qw/rev-parse --show-cdup/));
+               unless (length $cdup) {
+                       die "Already at toplevel, but $git_dir ",
+                           "not found '$cdup'\n";
+               }
+               chdir $cdup or die "Unable to chdir up to '$cdup'\n";
+               unless (-d $git_dir) {
+                       die "$git_dir still not found after going to ",
+                           "'$cdup'\n";
+               }
+               $ENV{GIT_DIR} = $git_dir;
+       }
+}
 unless ($cmd =~ /^(?:clone|init|multi-init|commit-diff)$/) {
        Git::SVN::Migration::migration_check();
 }
@@ -253,7 +276,7 @@ sub init_subdir {
        my $repo_path = shift or return;
        mkpath([$repo_path]) unless -d $repo_path;
        chdir $repo_path or die "Couldn't chdir to $repo_path: $!\n";
-       $ENV{GIT_DIR} = $repo_path . "/.git";
+       $ENV{GIT_DIR} = '.git';
 }
 
 sub cmd_clone {
@@ -263,12 +286,8 @@ sub cmd_clone {
            $url !~ m#^[a-z\+]+://#) {
                $path = $url;
        }
-       warn "--path: $path\n" if defined $path;
        $path = basename($url) if !defined $path || !length $path;
-       warn "++path: $path\n" if defined $path;
-       mkpath([$path]);
-       chdir $path or die "Couldn't chdir to $path\n";
-       cmd_init(@_);
+       cmd_init($url, $path);
        Git::SVN::fetch_all($Git::SVN::default_repo_id);
 }
 
@@ -428,7 +447,7 @@ sub cmd_show_ignore {
        my $url = (::working_head_info('HEAD'))[0];
        my $gs = Git::SVN->find_by_url($url) || Git::SVN->new;
        my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
-       $gs->traverse_ignore(\*STDOUT, '', $r);
+       $gs->traverse_ignore(\*STDOUT, $gs->{path}, $r);
 }
 
 sub cmd_multi_init {
@@ -436,12 +455,12 @@ sub cmd_multi_init {
        unless (defined $_trunk || defined $_branches || defined $_tags) {
                usage(1);
        }
-       do_git_init_db();
        $_prefix = '' unless defined $_prefix;
        if (defined $url) {
                $url =~ s#/+$##;
                init_subdir(@_);
        }
+       do_git_init_db();
        if (defined $_trunk) {
                my $trunk_ref = $_prefix . 'trunk';
                # try both old-style and new-style lookups:
@@ -1315,7 +1334,7 @@ sub traverse_ignore {
        my $ra = $self->ra;
        my ($dirent, undef, $props) = $ra->get_dir($path, $r);
        my $p = $path;
-       $p =~ s#^\Q$ra->{svn_path}\E/##;
+       $p =~ s#^\Q$self->{path}\E(/|$)##;
        print $fh length $p ? "\n# $p\n" : "\n# /\n";
        if (my $s = $props->{'svn:ignore'}) {
                $s =~ s/[\r\n]+/\n/g;
@@ -1656,9 +1675,9 @@ sub find_parent_branch {
        }
        if (defined $r0 && defined $parent) {
                print STDERR "Found branch parent: ($self->{ref_id}) $parent\n";
-               $self->assert_index_clean($parent);
                my $ed;
                if ($self->ra->can_do_switch) {
+                       $self->assert_index_clean($parent);
                        print STDERR "Following parent with do_switch\n";
                        # do_switch works with svn/trunk >= r22312, but that
                        # is not included with SVN 1.4.3 (the latest version
@@ -1824,6 +1843,8 @@ sub make_log_entry {
                $full_url =~ s#^\Q$svm->{replace}\E(/|$)#$svm->{source}$1# or
                             die "Failed to replace '$svm->{replace}' with ",
                                 "'$svm->{source}' in $full_url\n";
+               # throw away username for storing in records
+               remove_username($full_url);
                $log_entry{metadata} = "$full_url\@$r $uuid";
                $log_entry{svm_revision} = $r;
                $email ||= "$author\@$uuid"
@@ -1892,12 +1913,14 @@ sub rebuild {
        my ($rev_list, $ctx) = command_output_pipe("rev-list", $self->refname);
        my $latest;
        my $full_url = $self->full_url;
+       remove_username($full_url);
        my $svn_uuid;
        while (<$rev_list>) {
                chomp;
                my $c = $_;
                die "Non-SHA1: $c\n" unless $c =~ /^$::sha1$/o;
                my ($url, $rev, $uuid) = ::cmt_metadata($c);
+               remove_username($url);
 
                # ignore merges (from set-tree)
                next if (!defined $rev || !$uuid);
@@ -2071,6 +2094,10 @@ sub uri_encode {
        $f
 }
 
+sub remove_username {
+       $_[0] =~ s{^([^:]*://)[^@]+@}{$1};
+}
+
 package Git::SVN::Prompt;
 use strict;
 use warnings;
@@ -2852,6 +2879,7 @@ sub new {
        my ($class, $url) = @_;
        $url =~ s!/+$!!;
        return $RA if ($RA && $RA->{url} eq $url);
+       $RA->{pool}->clear if $RA;
 
        SVN::_Core::svn_config_ensure($config_dir, undef);
        my ($baton, $callbacks) = SVN::Core::auth_open_helper([
@@ -2904,6 +2932,10 @@ sub gs_do_update {
        my $new = ($rev_a == $rev_b);
        my $path = $gs->{path};
 
+       if ($new && -e $gs->{index}) {
+               unlink $gs->{index} or die
+                 "Couldn't unlink index: $gs->{index}: $!\n";
+       }
        my $pool = SVN::Pool->new;
        $editor->set_path_strip($path);
        my (@pc) = split m#/#, $path;