@matheusbsbs / matheus-bagliano / commits / 905f8b7dfc
Fork 0

git-svn: add a 'rebase' command

This works similarly to 'svn update' or 'git pull' except that it preserves linear history with 'git rebase' instead of 'git merge' for ease of dcommit-ing with git-svn. While we're at it, put the working_head_info() logic into its own function and allow --fetch-all/--all for dcommit and rebase (which will fetch all refs in the current [svn-remote] instead of just the working one). Note that the '-a' switch (short for --fetch-all/--all) has been removed as it conflicts with the non-svn 'git fetch' Signed-off-by: Eric Wong <normalperson@yhbt.net>

Eric Wong committed Feb 16, 2007 at 03:22 UTC 905f8b7dfc2c520b91f418ab0f2aecb1c371fbe4
1 file changed +63 -28
git-svn.perl
+63 -28
@@ -56,7 +56,7 @@ my ($_stdin, $_help, $_edit,
56 $_template, $_shared,
57 $_version, $_fetch_all,
58 $_merge, $_strategy, $_dry_run,
59 - $_prefix, $_no_checkout);
59 + $_prefix, $_no_checkout, $_verbose);
60 $Git::SVN::_follow_parent = 1;
61 my %remote_opts = ( 'username=s' => \$Git::SVN::Prompt::_username,
62 'config-dir=s' => \$Git::SVN::Ra::config_dir,
@@ -88,7 +88,7 @@ my %cmt_opts = ( 'edit|e' => \$_edit,
88 my %cmd = (
89 fetch => [ \&cmd_fetch, "Download new revisions from SVN",
90 { 'revision|r=s' => \$_revision,
91 - 'all|a' => \$_fetch_all,
91 + 'fetch-all|all' => \$_fetch_all,
92 %fc_opts } ],
93 init => [ \&cmd_init, "Initialize a repo for tracking" .
94 " (requires URL argument)",
@@ -101,7 +101,9 @@ my %cmd = (
101 'Commit several diffs to merge with upstream',
102 { 'merge|m|M' => \$_merge,
103 'strategy|s=s' => \$_strategy,
104 + 'verbose|v' => \$_verbose,
105 'dry-run|n' => \$_dry_run,
106 + 'fetch-all|all' => \$_fetch_all,
107 %cmt_opts, %fc_opts } ],
108 'set-tree' => [ \&cmd_set_tree,
109 "Set an SVN repository to a git tree-ish",
@@ -129,6 +131,12 @@ my %cmd = (
131 'color' => \$Git::SVN::Log::color,
132 'pager=s' => \$Git::SVN::Log::pager,
133 } ],
134 + 'rebase' => [ \&cmd_rebase, "Fetch and rebase your working directory",
135 + { 'merge|m|M' => \$_merge,
136 + 'verbose|v' => \$_verbose,
137 + 'strategy|s=s' => \$_strategy,
138 + 'fetch-all|all' => \$_fetch_all,
139 + %fc_opts } ],
140 'commit-diff' => [ \&cmd_commit_diff,
141 'Commit a diff between two trees',
142 { 'message|m=s' => \$_message,
@@ -248,7 +256,7 @@ sub cmd_fetch {
256 }
257 my ($remote) = @_;
258 if (@_ > 1) {
251 - die "Usage: $0 fetch [--all|-a] [svn-remote]\n";
259 + die "Usage: $0 fetch [--all] [svn-remote]\n";
260 }
261 $remote ||= $Git::SVN::default_repo_id;
262 if ($_fetch_all) {
@@ -296,21 +304,12 @@ sub cmd_set_tree {
304 sub cmd_dcommit {
305 my $head = shift;
306 $head ||= 'HEAD';
299 - my ($url, $rev, $uuid);
300 - my ($fh, $ctx) = command_output_pipe('rev-list', $head);
307 my @refs;
302 - my $c;
303 - while (<$fh>) {
304 - $c = $_;
305 - chomp $c;
306 - ($url, $rev, $uuid) = cmt_metadata($c);
307 - last if (defined $url && defined $rev && defined $uuid);
308 - unshift @refs, $c;
309 - }
310 - close $fh; # most likely breaking the pipe
308 + my ($url, $rev, $uuid) = working_head_info($head, \@refs);
309 + my $c = $refs[-1];
310 unless (defined $url && defined $rev && defined $uuid) {
311 die "Unable to determine upstream SVN information from ",
313 - "$head history:\n $ctx\n";
312 + "$head history\n";
313 }
314 my $gs = Git::SVN->find_by_url($url);
315 my $last_rev;
@@ -354,15 +353,13 @@ sub cmd_dcommit {
353 "now resync your SVN::Mirror repository.\n";
354 return;
355 }
357 - $gs->fetch;
356 + $_fetch_all ? $gs->fetch_all : $gs->fetch;
357 # we always want to rebase against the current HEAD, not any
358 # head that was passed to us
359 my @diff = command('diff-tree', 'HEAD', $gs->refname, '--');
360 my @finish;
361 if (@diff) {
363 - @finish = qw/rebase/;
364 - push @finish, qw/--merge/ if $_merge;
365 - push @finish, "--strategy=$_strategy" if $_strategy;
362 + @finish = rebase_cmd();
363 print STDERR "W: HEAD and ", $gs->refname, " differ, ",
364 "using @finish:\n", "@diff";
365 } else {
@@ -374,6 +371,24 @@ sub cmd_dcommit {
371 command_noisy(@finish, $gs->refname);
372 }
373
374 +sub cmd_rebase {
375 + command_noisy(qw/update-index --refresh/);
376 + my $url = (working_head_info('HEAD'))[0];
377 + if (!defined $url) {
378 + die "Unable to determine upstream SVN information from ",
379 + "working tree history\n";
380 + }
381 +
382 + my $gs = Git::SVN->find_by_url($url);
383 + if (command(qw/diff-index HEAD --/)) {
384 + print STDERR "Cannot rebase with uncommited changes:\n";
385 + command_noisy('status');
386 + exit 1;
387 + }
388 + $_fetch_all ? $gs->fetch_all : $gs->fetch;
389 + command_noisy(rebase_cmd(), $gs->refname);
390 +}
391 +
392 sub cmd_show_ignore {
393 my $gs = Git::SVN->new;
394 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
@@ -468,6 +483,14 @@ sub cmd_commit_diff {
483
484 ########################### utility functions #########################
485
486 +sub rebase_cmd {
487 + my @cmd = qw/rebase/;
488 + push @cmd, '-v' if $_verbose;
489 + push @cmd, qw/--merge/ if $_merge;
490 + push @cmd, "--strategy=$_strategy" if $_strategy;
491 + @cmd;
492 +}
493 +
494 sub post_fetch_checkout {
495 return if $_no_checkout;
496 my $gs = $Git::SVN::_head or return;
@@ -687,6 +710,20 @@ sub cmt_metadata {
710 command(qw/cat-file commit/, shift)))[-1]);
711 }
712
713 +sub working_head_info {
714 + my ($head, $refs) = @_;
715 + my ($url, $rev, $uuid);
716 + my ($fh, $ctx) = command_output_pipe('rev-list', $head);
717 + while (<$fh>) {
718 + chomp;
719 + ($url, $rev, $uuid) = cmt_metadata($_);
720 + last if (defined $url && defined $rev && defined $uuid);
721 + unshift @$refs, $_ if $refs;
722 + }
723 + close $fh; # break the pipe
724 + ($url, $rev, $uuid);
725 +}
726 +
727 package Git::SVN;
728 use strict;
729 use warnings;
@@ -783,6 +820,12 @@ sub parse_revision_argument {
820
821 sub fetch_all {
822 my ($repo_id, $remotes) = @_;
823 + if (ref $repo_id) {
824 + my $gs = $repo_id;
825 + $repo_id = undef;
826 + $repo_id = $gs->{repo_id};
827 + }
828 + $remotes ||= read_all_remotes();
829 my $remote = $remotes->{$repo_id} or
830 die "[svn-remote \"$repo_id\"] unknown\n";
831 my $fetch = $remote->{fetch};
@@ -3085,15 +3128,7 @@ sub git_svn_log_cmd {
3128 last;
3129 }
3130
3088 - my $url;
3089 - my ($fh, $ctx) = command_output_pipe('rev-list', $head);
3090 - while (<$fh>) {
3091 - chomp;
3092 - $url = (::cmt_metadata($_))[0];
3093 - last if defined $url;
3094 - }
3095 - close $fh; # break the pipe
3096 -
3131 + my $url = (::working_head_info($head))[0];
3132 my $gs = Git::SVN->find_by_url($url) || Git::SVN->_new;
3133 my @cmd = (qw/log --abbrev-commit --pretty=raw --default/,
3134 $gs->refname);