wrapper.c: wrapper to open a file, fprintf then close
[gitweb.git] / git-svn.perl
index 6c7bd95032b890d2f79cff0d18ac6aa688a74757..b6e2186cef0fcddc7c7b82378b07941d9f671a92 100755 (executable)
@@ -113,7 +113,7 @@ sub _req_svn {
        $_template, $_shared,
        $_version, $_fetch_all, $_no_rebase, $_fetch_parent,
        $_before, $_after,
-       $_merge, $_strategy, $_preserve_merges, $_dry_run, $_local,
+       $_merge, $_strategy, $_preserve_merges, $_dry_run, $_parents, $_local,
        $_prefix, $_no_checkout, $_url, $_verbose,
        $_commit_url, $_tag, $_merge_info, $_interactive);
 
@@ -126,6 +126,7 @@ sub _req_svn {
                     'config-dir=s' => \$Git::SVN::Ra::config_dir,
                     'no-auth-cache' => \$Git::SVN::Prompt::_no_auth_cache,
                     'ignore-paths=s' => \$Git::SVN::Fetcher::_ignore_regex,
+                    'include-paths=s' => \$Git::SVN::Fetcher::_include_regex,
                     'ignore-refs=s' => \$Git::SVN::Ra::_ignore_refs_regex );
 my %fc_opts = ( 'follow-parent|follow!' => \$Git::SVN::_follow_parent,
                'authors-file|A=s' => \$_authors,
@@ -202,6 +203,7 @@ sub _req_svn {
                    { 'message|m=s' => \$_message,
                      'destination|d=s' => \$_branch_dest,
                      'dry-run|n' => \$_dry_run,
+                     'parents' => \$_parents,
                      'tag|t' => \$_tag,
                      'username=s' => \$Git::SVN::Prompt::_username,
                      'commit-url=s' => \$_commit_url } ],
@@ -210,6 +212,7 @@ sub _req_svn {
                 { 'message|m=s' => \$_message,
                   'destination|d=s' => \$_branch_dest,
                   'dry-run|n' => \$_dry_run,
+                  'parents' => \$_parents,
                   'username=s' => \$Git::SVN::Prompt::_username,
                   'commit-url=s' => \$_commit_url } ],
        'set-tree' => [ \&cmd_set_tree,
@@ -257,8 +260,8 @@ sub _req_svn {
                        } ],
        'find-rev' => [ \&cmd_find_rev,
                        "Translate between SVN revision numbers and tree-ish",
-                       { 'before' => \$_before,
-                         'after' => \$_after } ],
+                       { 'B|before' => \$_before,
+                         'A|after' => \$_after } ],
        'rebase' => [ \&cmd_rebase, "Fetch and rebase your working directory",
                        { 'merge|m|M' => \$_merge,
                          'verbose|v' => \$_verbose,
@@ -303,13 +306,16 @@ sub readline {
 }
 package main;
 
-my $term = eval {
-       $ENV{"GIT_SVN_NOTTY"}
-               ? new Term::ReadLine 'git-svn', \*STDIN, \*STDOUT
-               : new Term::ReadLine 'git-svn';
-};
-if ($@) {
-       $term = new FakeTerm "$@: going non-interactive";
+my $term;
+sub term_init {
+       $term = eval {
+               $ENV{"GIT_SVN_NOTTY"}
+                       ? new Term::ReadLine 'git-svn', \*STDIN, \*STDOUT
+                       : new Term::ReadLine 'git-svn';
+       };
+       if ($@) {
+               $term = new FakeTerm "$@: going non-interactive";
+       }
 }
 
 my $cmd;
@@ -421,6 +427,7 @@ sub ask {
        my $default = $arg{default};
        my $resp;
        my $i = 0;
+       term_init() unless $term;
 
        if ( !( defined($term->IN)
             && defined( fileno($term->IN) )
@@ -470,6 +477,9 @@ sub do_git_init_db {
        my $ignore_paths_regex = \$Git::SVN::Fetcher::_ignore_regex;
        command_noisy('config', "$pfx.ignore-paths", $$ignore_paths_regex)
                if defined $$ignore_paths_regex;
+       my $include_paths_regex = \$Git::SVN::Fetcher::_include_regex;
+       command_noisy('config', "$pfx.include-paths", $$include_paths_regex)
+               if defined $$include_paths_regex;
        my $ignore_refs_regex = \$Git::SVN::Ra::_ignore_refs_regex;
        command_noisy('config', "$pfx.ignore-refs", $$ignore_refs_regex)
                if defined $$ignore_refs_regex;
@@ -669,12 +679,14 @@ sub merge_revs_into_hash {
 }
 
 sub merge_merge_info {
-       my ($mergeinfo_one, $mergeinfo_two) = @_;
+       my ($mergeinfo_one, $mergeinfo_two, $ignore_branch) = @_;
        my %result_hash = ();
 
        merge_revs_into_hash(\%result_hash, $mergeinfo_one);
        merge_revs_into_hash(\%result_hash, $mergeinfo_two);
 
+       delete $result_hash{$ignore_branch} if $ignore_branch;
+
        my $result = '';
        # Sort below is for consistency's sake
        for my $branchname (sort keys(%result_hash)) {
@@ -695,6 +707,7 @@ sub populate_merge_info {
                my $all_parents_ok = 1;
                my $aggregate_mergeinfo = '';
                my $rooturl = $gs->repos_root;
+               my ($target_branch) = $gs->full_pushurl =~ /^\Q$rooturl\E(.*)/;
 
                if (defined($rewritten_parent)) {
                        # Replace first parent with newly-rewritten version
@@ -726,7 +739,8 @@ sub populate_merge_info {
                        # Merge previous mergeinfo values
                        $aggregate_mergeinfo =
                                merge_merge_info($aggregate_mergeinfo,
-                                                                $par_mergeinfo, 0);
+                                                               $par_mergeinfo,
+                                                               $target_branch);
 
                        next if $parent eq $parents[0]; # Skip first parent
                        # Add new changes being placed in tree by merge
@@ -769,7 +783,8 @@ sub populate_merge_info {
                        my $newmergeinfo = "$branchpath:" . join(',', @revsin);
                        $aggregate_mergeinfo =
                                merge_merge_info($aggregate_mergeinfo,
-                                                                $newmergeinfo, 1);
+                                                               $newmergeinfo,
+                                                               $target_branch);
                }
                if ($all_parents_ok and $aggregate_mergeinfo) {
                        return $aggregate_mergeinfo;
@@ -820,7 +835,7 @@ sub dcommit_rebase {
 sub cmd_dcommit {
        my $head = shift;
        command_noisy(qw/update-index --refresh/);
-       git_cmd_try { command_oneline(qw/diff-index --quiet HEAD/) }
+       git_cmd_try { command_oneline(qw/diff-index --quiet HEAD --/) }
                'Cannot dcommit with a dirty index.  Commit your changes first, '
                . "or stash them with `git stash'.\n";
        $head ||= 'HEAD';
@@ -1150,7 +1165,9 @@ sub cmd_branch {
        ::_req_svn();
 
        my $ctx = SVN::Client->new(
-               auth    => Git::SVN::Ra::_auth_providers(),
+               config => SVN::Core::config_get_config(
+                       $Git::SVN::Ra::config_dir
+               ),
                log_msg => sub {
                        ${ $_[0] } = defined $_message
                                ? $_message
@@ -1163,6 +1180,10 @@ sub cmd_branch {
                $ctx->ls($dst, 'HEAD', 0);
        } and die "branch ${branch_name} already exists\n";
 
+       if ($_parents) {
+               mk_parent_dirs($ctx, $dst);
+       }
+
        print "Copying ${src} at r${rev} to ${dst}...\n";
        $ctx->copy($src, $rev, $dst)
                unless $_dry_run;
@@ -1170,6 +1191,17 @@ sub cmd_branch {
        $gs->fetch_all;
 }
 
+sub mk_parent_dirs {
+       my ($ctx, $parent) = @_;
+       $parent =~ s{/[^/]*$}{};
+
+       if (!eval{$ctx->ls($parent, 'HEAD', 0)}) {
+               mk_parent_dirs($ctx, $parent);
+               print "Creating parent folder ${parent} ...\n";
+               $ctx->mkdir($parent) unless $_dry_run;
+       }
+}
+
 sub cmd_find_rev {
        my $revision_or_hash = shift or die "SVN or git revision required ",
                                            "as a command-line argument\n";
@@ -1220,7 +1252,7 @@ sub cmd_rebase {
                return;
        }
        if (command(qw/diff-index HEAD --/)) {
-               print STDERR "Cannot rebase with uncommited changes:\n";
+               print STDERR "Cannot rebase with uncommitted changes:\n";
                command_noisy('status');
                exit 1;
        }
@@ -1363,7 +1395,7 @@ sub cmd_multi_init {
                usage(1);
        }
 
-       $_prefix = '' unless defined $_prefix;
+       $_prefix = 'origin/' unless defined $_prefix;
        if (defined $url) {
                $url = canonicalize_url($url);
                init_subdir(@_);
@@ -1449,10 +1481,37 @@ sub cmd_commit_diff {
        }
 }
 
-
 sub cmd_info {
-       my $path = canonicalize_path(defined($_[0]) ? $_[0] : ".");
-       my $fullpath = canonicalize_path($cmd_dir_prefix . $path);
+       my $path_arg = defined($_[0]) ? $_[0] : '.';
+       my $path = $path_arg;
+       if (File::Spec->file_name_is_absolute($path)) {
+               $path = canonicalize_path($path);
+
+               my $toplevel = eval {
+                       my @cmd = qw/rev-parse --show-toplevel/;
+                       command_oneline(\@cmd, STDERR => 0);
+               };
+
+               # remove $toplevel from the absolute path:
+               my ($vol, $dirs, $file) = File::Spec->splitpath($path);
+               my (undef, $tdirs, $tfile) = File::Spec->splitpath($toplevel);
+               my @dirs = File::Spec->splitdir($dirs);
+               my @tdirs = File::Spec->splitdir($tdirs);
+               pop @dirs if $dirs[-1] eq '';
+               pop @tdirs if $tdirs[-1] eq '';
+               push @dirs, $file;
+               push @tdirs, $tfile;
+               while (@tdirs && @dirs && $tdirs[0] eq $dirs[0]) {
+                       shift @dirs;
+                       shift @tdirs;
+               }
+               $dirs = File::Spec->catdir(@dirs);
+               $path = File::Spec->catpath($vol, $dirs);
+
+               $path = canonicalize_path($path);
+       } else {
+               $path = canonicalize_path($cmd_dir_prefix . $path);
+       }
        if (exists $_[1]) {
                die "Too many arguments specified\n";
        }
@@ -1473,14 +1532,14 @@ sub cmd_info {
        # canonicalize_path() will return "" to make libsvn 1.5.x happy,
        $path = "." if $path eq "";
 
-       my $full_url = canonicalize_url( add_path_to_url( $url, $fullpath ) );
+       my $full_url = canonicalize_url( add_path_to_url( $url, $path ) );
 
        if ($_url) {
                print "$full_url\n";
                return;
        }
 
-       my $result = "Path: $path\n";
+       my $result = "Path: $path_arg\n";
        $result .= "Name: " . basename($path) . "\n" if $file_type ne "dir";
        $result .= "URL: $full_url\n";
 
@@ -1511,7 +1570,7 @@ sub cmd_info {
        }
 
        my ($lc_author, $lc_rev, $lc_date_utc);
-       my @args = Git::SVN::Log::git_svn_log_cmd($rev, $rev, "--", $fullpath);
+       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>) {
@@ -1733,7 +1792,7 @@ sub get_commit_entry {
                my $msgbuf = "";
                while (<$msg_fh>) {
                        if (!$in_msg) {
-                               $in_msg = 1 if (/^\s*$/);
+                               $in_msg = 1 if (/^$/);
                                $author = $1 if (/^author (.*>)/);
                        } elsif (/^git-svn-id: /) {
                                # skip this for now, we regenerate the
@@ -1906,7 +1965,7 @@ sub cmt_sha2rev_batch {
 sub working_head_info {
        my ($head, $refs) = @_;
        my @args = qw/rev-list --first-parent --pretty=medium/;
-       my ($fh, $ctx) = command_output_pipe(@args, $head);
+       my ($fh, $ctx) = command_output_pipe(@args, $head, "--");
        my $hash;
        my %max;
        while (<$fh>) {