Code

git-svn: proper detection of bare repositories
[git.git] / git-svn.perl
index 80a5728371bdcf3ac90716df5705c9bec3edcf29..86075ec616418f77e42dd72593a7efd00e1200c3 100755 (executable)
@@ -66,7 +66,7 @@ my ($_stdin, $_help, $_edit,
        $_version, $_fetch_all, $_no_rebase,
        $_merge, $_strategy, $_dry_run, $_local,
        $_prefix, $_no_checkout, $_url, $_verbose,
-       $_git_format, $_commit_url);
+       $_git_format, $_commit_url, $_tag);
 $Git::SVN::_follow_parent = 1;
 my %remote_opts = ( 'username=s' => \$Git::SVN::Prompt::_username,
                     'config-dir=s' => \$Git::SVN::Ra::config_dir,
@@ -131,6 +131,15 @@ my %cmd = (
                          'revision|r=i' => \$_revision,
                          'no-rebase' => \$_no_rebase,
                        %cmt_opts, %fc_opts } ],
+       branch => [ \&cmd_branch,
+                   'Create a branch in the SVN repository',
+                   { 'message|m=s' => \$_message,
+                     'dry-run|n' => \$_dry_run,
+                     'tag|t' => \$_tag } ],
+       tag => [ sub { $_tag = 1; cmd_branch(@_) },
+                'Create a tag in the SVN repository',
+                { 'message|m=s' => \$_message,
+                  'dry-run|n' => \$_dry_run } ],
        'set-tree' => [ \&cmd_set_tree,
                        "Set an SVN repository to a git tree-ish",
                        { 'stdin|' => \$_stdin, %cmt_opts, %fc_opts, } ],
@@ -214,11 +223,13 @@ unless ($cmd && $cmd =~ /(?:clone|init|multi-init)$/) {
                            "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";
-               }
+               my $cdup = undef;
+               git_cmd_try {
+                       $cdup = command_oneline(qw/rev-parse --show-cdup/);
+                       $git_dir = '.' unless ($cdup);
+                       chomp $cdup if ($cdup);
+                       $cdup = "." unless ($cdup && length $cdup);
+               } "Already at toplevel, but $git_dir not found\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 ",
@@ -537,6 +548,42 @@ sub cmd_dcommit {
        unlink $gs->{index};
 }
 
+sub cmd_branch {
+       my ($branch_name, $head) = @_;
+
+       unless (defined $branch_name && length $branch_name) {
+               die(($_tag ? "tag" : "branch") . " name required\n");
+       }
+       $head ||= 'HEAD';
+
+       my ($src, $rev, undef, $gs) = working_head_info($head);
+
+       my $remote = Git::SVN::read_all_remotes()->{svn};
+       my $glob = $remote->{ $_tag ? 'tags' : 'branches' };
+       my ($lft, $rgt) = @{ $glob->{path} }{qw/left right/};
+       my $dst = join '/', $remote->{url}, $lft, $branch_name, ($rgt || ());
+
+       my $ctx = SVN::Client->new(
+               auth    => Git::SVN::Ra::_auth_providers(),
+               log_msg => sub {
+                       ${ $_[0] } = defined $_message
+                               ? $_message
+                               : 'Create ' . ($_tag ? 'tag ' : 'branch ' )
+                               . $branch_name;
+               },
+       );
+
+       eval {
+               $ctx->ls($dst, 'HEAD', 0);
+       } and die "branch ${branch_name} already exists\n";
+
+       print "Copying ${src} at r${rev} to ${dst}...\n";
+       $ctx->copy($src, $rev, $dst)
+               unless $_dry_run;
+
+       $gs->fetch_all;
+}
+
 sub cmd_find_rev {
        my $revision_or_hash = shift or die "SVN or git revision required ",
                                            "as a command-line argument\n";
@@ -807,7 +854,7 @@ sub escape_uri_only {
        my ($uri) = @_;
        my @tmp;
        foreach (split m{/}, $uri) {
-               s/([^\w.%+-]|%(?![a-fA-F0-9]{2}))/sprintf("%%%02X",ord($1))/eg;
+               s/([^~\w.%+-]|%(?![a-fA-F0-9]{2}))/sprintf("%%%02X",ord($1))/eg;
                push @tmp, $_;
        }
        join('/', @tmp);
@@ -1091,9 +1138,19 @@ sub get_commit_entry {
                system($editor, $commit_editmsg);
        }
        rename $commit_editmsg, $commit_msg or croak $!;
-       open $log_fh, '<', $commit_msg or croak $!;
-       { local $/; chomp($log_entry{log} = <$log_fh>); }
-       close $log_fh or croak $!;
+       {
+               # SVN requires messages to be UTF-8 when entering the repo
+               local $/;
+               open $log_fh, '<', $commit_msg or croak $!;
+               binmode $log_fh;
+               chomp($log_entry{log} = <$log_fh>);
+
+               if (my $enc = Git::config('i18n.commitencoding')) {
+                       require Encode;
+                       Encode::from_to($log_entry{log}, $enc, 'UTF-8');
+               }
+               close $log_fh or croak $!;
+       }
        unlink $commit_msg;
        \%log_entry;
 }
@@ -1146,7 +1203,7 @@ sub read_repo_config {
                my $v = $opts->{$o};
                my ($key) = ($o =~ /^([a-zA-Z\-]+)/);
                $key =~ s/-//g;
-               my $arg = 'git-config';
+               my $arg = 'git config';
                $arg .= ' --int' if ($o =~ /[:=]i$/);
                $arg .= ' --bool' if ($o !~ /[:=][sfi]$/);
                if (ref $v eq 'ARRAY') {
@@ -2222,12 +2279,20 @@ sub do_git_commit {
        }
        die "Tree is not a valid sha1: $tree\n" if $tree !~ /^$::sha1$/o;
 
-       my @exec = ('git-commit-tree', $tree);
+       my @exec = ('git', 'commit-tree', $tree);
        foreach ($self->get_commit_parents($log_entry)) {
                push @exec, '-p', $_;
        }
        defined(my $pid = open3(my $msg_fh, my $out_fh, '>&STDERR', @exec))
                                                                   or croak $!;
+       binmode $msg_fh;
+
+       # we always get UTF-8 from SVN, but we may want our commits in
+       # a different encoding.
+       if (my $enc = Git::config('i18n.commitencoding')) {
+               require Encode;
+               Encode::from_to($log_entry->{log}, 'UTF-8', $enc);
+       }
        print $msg_fh $log_entry->{log} or croak $!;
        restore_commit_header_env($old_env);
        unless ($self->no_metadata) {
@@ -2591,7 +2656,7 @@ sub set_tree {
        my ($self, $tree) = (shift, shift);
        my $log_entry = ::get_commit_entry($tree);
        unless ($self->{last_rev}) {
-               fatal("Must have an existing revision to commit");
+               ::fatal("Must have an existing revision to commit");
        }
        my %ed_opts = ( r => $self->{last_rev},
                        log => $log_entry->{log},
@@ -3492,7 +3557,7 @@ sub repo_path {
 sub url_path {
        my ($self, $path) = @_;
        if ($self->{url} =~ m#^https?://#) {
-               $path =~ s/([^a-zA-Z0-9_.-])/uc sprintf("%%%02x",ord($1))/eg;
+               $path =~ s/([^~a-zA-Z0-9_.-])/uc sprintf("%%%02x",ord($1))/eg;
        }
        $self->{url} . '/' . $self->repo_path($path);
 }
@@ -3845,7 +3910,7 @@ sub escape_uri_only {
        my ($uri) = @_;
        my @tmp;
        foreach (split m{/}, $uri) {
-               s/([^\w.%+-]|%(?![a-fA-F0-9]{2}))/sprintf("%%%02X",ord($1))/eg;
+               s/([^~\w.%+-]|%(?![a-fA-F0-9]{2}))/sprintf("%%%02X",ord($1))/eg;
                push @tmp, $_;
        }
        join('/', @tmp);