Add new 'rebase' command.
authorYann Dirson <ydirson@altern.org>
Sat, 20 Jan 2007 18:04:21 +0000 (19:04 +0100)
committerCatalin Marinas <catalin.marinas@gmail.com>
Sun, 21 Jan 2007 22:54:43 +0000 (22:54 +0000)
Signed-off-by: Yann Dirson <ydirson@altern.org>
contrib/stgit-completion.bash
stgit/commands/rebase.py [new file with mode: 0644]
stgit/main.py
t/t2200-rebase.sh [new file with mode: 0755]

index 92d41b1..d0d716c 100644 (file)
@@ -36,6 +36,7 @@ _stg_commands="
     pop
     pull
     push
+    rebase
     refresh
     rename
     resolved
diff --git a/stgit/commands/rebase.py b/stgit/commands/rebase.py
new file mode 100644 (file)
index 0000000..8b7bca0
--- /dev/null
@@ -0,0 +1,64 @@
+__copyright__ = """
+Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License version 2 as
+published by the Free Software Foundation.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+"""
+
+import sys, os
+from optparse import OptionParser, make_option
+
+from stgit.commands.common import *
+from stgit.utils import *
+from stgit import stack, git
+
+
+help = 'move the stack base to another point in history'
+usage = """%prog [options] <new-base-id>
+
+Pop all patches from current stack, move the stack base to the given
+<new-base-id> and push the patches back."""
+
+options = [make_option('-n', '--nopush',
+                       help = 'do not push the patches back after rebasing',
+                       action = 'store_true')]
+
+def func(parser, options, args):
+    """Rebase the current stack
+    """
+    if len(args) != 1:
+        parser.error('incorrect number of arguments')
+
+    if crt_series.get_protected():
+        raise CmdException, 'This branch is protected. Rebase is not permitted'
+
+    check_local_changes()
+    check_conflicts()
+    check_head_top_equal()
+
+    # pop all patches
+    applied = crt_series.get_applied()
+    if len(applied) > 0:
+        print 'Popping all applied patches...',
+        sys.stdout.flush()
+        crt_series.pop_patch(applied[0])
+        print 'done'
+
+    print 'Rebasing to "%s"...' % args[0]
+    git.reset(tree_id = git_id(args[0]))
+
+    # push the patches back
+    if not options.nopush:
+        push_patches(applied)
+
+    print_crt_patch()
index 99e0832..f9b07d7 100644 (file)
@@ -80,6 +80,7 @@ commands = Commands({
     'pop':              'pop',
     'pull':             'pull',
     'push':             'push',
+    'rebase':           'rebase',
     'refresh':          'refresh',
     'rename':           'rename',
     'resolved':         'resolved',
@@ -110,6 +111,7 @@ stackcommands = (
     'init',
     'pop',
     'push',
+    'rebase',
     'series',
     'top',
     'unapplied',
diff --git a/t/t2200-rebase.sh b/t/t2200-rebase.sh
new file mode 100755 (executable)
index 0000000..e2d9d9a
--- /dev/null
@@ -0,0 +1,33 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Yann Dirson
+#
+
+test_description='Test the "rebase" command.'
+
+. ./test-lib.sh
+
+test_expect_success \
+       'Setup a multi-commit branch and fork an stgit stack' \
+       '
+       echo foo > file1 &&
+       git add file1 &&
+       git commit -m a &&
+       echo foo > file2 &&
+       git add file2 &&
+       git commit -m b &&
+
+       stg branch --create stack &&
+       stg new p -m . &&
+       echo bar >> file1 &&
+       stg refresh
+       '
+
+test_expect_success \
+       'Rebase to previous commit' \
+       '
+       stg rebase master~1 &&
+       test `git rev-parse bases/stack` = `git rev-parse master~1`
+       '
+
+test_done