chiark / gitweb /
git-debrebase: stitch better
[dgit.git] / git-debrebase
index 6f89fc26a988edc24e101aa7c583c4b14de300ca..a853c0639c1cf1f0daad4f891e0c3838c1912b87 100755 (executable)
@@ -29,6 +29,7 @@
 #    git-debrebase [<options> --] [<git-rebase options...>]
 #    git-debrebase [<options>] analyse
 #    git-debrebase [<options>] launder         # prints breakwater tip etc.
+#    git-debrebase [<options>] stitch [--prose=<for commit message>]
 #    git-debrebase [<options>] downstream-rebase-launder-v0  # experimental
 #
 #    git-debrebase [<options>] gbp2debrebase-v0 \
@@ -98,13 +99,17 @@ sub badusage ($) {
     die "bad usage: $m\n";
 }
 
-sub cfg ($) {
-    my ($k) = @_;
-    $/ = "\0";
+sub cfg ($;$) {
+    my ($k, $optional) = @_;
+    local $/ = "\0";
     my @cmd = qw(git config -z);
     push @cmd, qw(--get-all) if wantarray;
     push @cmd, $k;
-    my $out = cmdoutput @cmd;
+    my $out = cmdoutput_errok @cmd;
+    if (!defined $out) {
+       fail "missing required git config $k" unless $optional;
+       return ();
+    }
     return split /\0/, $out;
 }
 
@@ -791,7 +796,10 @@ sub walk ($;$$) {
     return @r
 }
 
-sub get_head () { return git_rev_parse qw(HEAD); }
+sub get_head () {
+    git_check_unmodified();
+    return git_rev_parse qw(HEAD);
+}
 
 sub update_head ($$$) {
     my ($old, $new, $mrest) = @_;
@@ -825,9 +833,14 @@ sub cmd_launder () {
 
 sub defaultcmd_rebase () {
     my $old = get_head();
+    my ($status, $message) = record_ffq_prev();
+    if ($status eq 'written' || $status eq 'exists') {
+    } else {
+       fproblem $status, "could not record ffq-prev: $message";
+       fproblems_maybe_bail();
+    }
     my ($tip,$breakwater) = walk $old;
     update_head_postlaunder $old, $tip, 'launder for rebase';
-    @ARGV = qw(-i) unless @ARGV; # make configurable
     runcmd @git, qw(rebase), @ARGV, $breakwater;
 }
 
@@ -838,12 +851,26 @@ sub cmd_analyse () {
     if (defined $old) {
        $old = git_rev_parse $old;
     } else {
-       $old = get_head();
+       $old = git_rev_parse 'HEAD';
     }
     my ($dummy,$breakwater) = walk $old, 1,*STDOUT;
     STDOUT->error and die $!;
 }
 
+sub ffq_prev_branchinfo () {
+    # => ('status', "message", [$current, $ffq_prev])
+    # 'status' may be
+    #    branch         message is undef
+    #    weird-symref   } no $current,
+    #    notbranch      }  no $ffq_prev
+    my $current = git_get_symref();
+    return ('detached', 'detached HEAD') unless defined $current;
+    return ('weird-symref', 'HEAD symref is not to refs/')
+       unless $current =~ m{^refs/};
+    my $ffq_prev = "refs/$ffq_refprefix/$'";
+    return ('branch', undef, $current, $ffq_prev);
+}
+
 sub record_ffq_prev () {
     # => ('status', "message")
     # 'status' may be
@@ -855,22 +882,19 @@ sub record_ffq_prev () {
     # if not ff from some branch we should be ff from, is an fproblem
     # if "written", will have printed something about that to stdout,
     #   and also some messages about ff checks
-    my $current = git_get_symref();
-    return ('detached', 'detached HEAD') unless defined $current;
-    return ('weird-symref', 'HEAD symref is not to refs/')
-       unless $current =~ m{^refs/};
-    my $ffq_prev = "refs/$ffq_refprefix/$'";
+    my ($status, $message, $current, $ffq_prev) = ffq_prev_branchinfo();
+    return ($status, $message) unless $status eq 'branch';
 
     my $currentval = get_head();
 
     my $exists = git_get_ref $ffq_prev;
-    return ('exists',"$ffq_prev already exists") if defined $exists;
+    return ('exists',"$ffq_prev already exists") if $exists;
 
     return ('not-branch', 'HEAD symref is not to refs/heads/')
        unless $current =~ m{^refs/heads/};
     my $branch = $';
 
-    my @check_specs = split /\;/, (cfg "branch.$branch.ffq-ffrefs") // '*';
+    my @check_specs = split /\;/, (cfg "branch.$branch.ffq-ffrefs",1) // '*';
     my %checked;
 
     my $check = sub {
@@ -897,7 +921,7 @@ sub record_ffq_prev () {
        }
     };
 
-    my $merge = cfg "branch.$branch.merge";
+    my $merge = cfg "branch.$branch.merge",1;
     if (defined $merge && $merge =~ m{^refs/heads/}) {
        my $rhs = $';
        my $check_remote = sub {
@@ -905,17 +929,19 @@ sub record_ffq_prev () {
            return unless defined $remote;
            $check->("refs/remotes/$remote/$rhs", $desc);
        };
-       $check_remote->((cfg "branch.$branch.remote"),
+       $check_remote->((cfg "branch.$branch.remote",1),
                        'remote fetch/merge branch');
-       $check_remote->((cfg "branch.$branch.pushRemote") //
-                       (cfg "branch.$branch.pushDefault"),
+       $check_remote->((cfg "branch.$branch.pushRemote",1) //
+                       (cfg "branch.$branch.pushDefault",1),
                        'remote push branch');
     }
     if ($branch =~ m{^dgit/}) {
        $check->("remotes/dgit/$branch", 'remote dgit branch');
+    } elsif ($branch =~ m{^master$}) {
+       $check->("remotes/dgit/dgit/sid", 'remote dgit branch for sid');
     }
 
-    fproblems_maybe_fail();
+    fproblems_maybe_bail();
     runcmd @git, qw(update-ref -m), "record current head for preservation",
        $ffq_prev, $currentval, $git_null_obj;
     print "Recorded current head for preservation\n" or die $!;
@@ -923,7 +949,6 @@ sub record_ffq_prev () {
 }
 
 sub cmd_new_upstream_v0 () {
-    # tree should be clean and this is not checked
     # automatically and unconditionally launders before rebasing
     # if rebase --abort is used, laundering has still been done
 
@@ -1112,6 +1137,38 @@ sub cmd_record_ffq_prev () {
     }
 }
 
+sub cmd_stitch () {
+    my $prose = '';
+    GetOptions('prose=s', \$prose) or die badusage("bad options to stitch");
+    badusage "no arguments allowed" if @ARGV;
+    my ($status, $message, $current, $ffq_prev) = ffq_prev_branchinfo();
+    if ($status ne 'branch') {
+       fproblem $status, "could not check ffq-prev: $message";
+       fproblems_maybe_bail();
+    }
+    my $prev = $ffq_prev && git_get_ref $ffq_prev;
+    if (!$prev) {
+       fail "No ffq-prev to stitch." unless $opt_noop_ok;
+    }
+    fresh_workarea();
+    my $old_head = get_head();
+    my $new_head = make_commit [ $old_head, $ffq_prev ], [
+       'Declare fast forward / record previous work',
+        "[git-debrebase pseudomerge: stitch$prose]",
+    ];
+    my @upd_cmd = (@git, qw(update-ref --stdin));
+    debugcmd '>|', @upd_cmd;
+    open U, "|-", @upd_cmd or die $!;
+    my $u = <<END;
+update HEAD $new_head $old_head
+delete $ffq_prev $prev
+END
+    printdebug ">= ", $_, "\n" foreach split /\n/, $u;
+    print U $u;
+    printdebug ">\$\n";
+    close U or failedcmd @upd_cmd;
+}
+
 sub cmd_gbp2debrebase () {
     badusage "needs 1 optional argument, the upstream" unless @ARGV<=1;
     my ($upstream_spec) = @ARGV;
@@ -1174,7 +1231,7 @@ sub cmd_gbp2debrebase () {
        # rebase the patch queue onto the new breakwater
        runcmd @git, qw(reset --quiet --hard patch-queue/gdr-internal);
        runcmd @git, qw(rebase --quiet --onto), $work, qw(gdr-internal);
-       $work = get_head();
+       $work = git_rev_parse 'HEAD';
     };
 
     update_head_checkout $old_head, $work, 'gbp2debrebase';