From: Karl Hasselström Date: Sun, 21 Sep 2008 12:17:41 +0000 (+0200) Subject: New command: stg redo X-Git-Tag: v0.15-rc1~140 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~mdw/git/stgit/commitdiff_plain/121c14e552a2ba679e06842ca43bf00aa147755f New command: stg redo Command for undoing an undo. Signed-off-by: Karl Hasselström --- diff --git a/stgit/commands/redo.py b/stgit/commands/redo.py new file mode 100644 index 0000000..8952680 --- /dev/null +++ b/stgit/commands/redo.py @@ -0,0 +1,54 @@ +# -*- coding: utf-8 -*- + +__copyright__ = """ +Copyright (C) 2008, Karl Hasselström + +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 +""" + +from stgit.argparse import opt +from stgit.commands import common +from stgit.lib import log, transaction + +help = 'Undo the last undo operation' +kind = 'stack' +usage = [''] +description = """ +If the last command was an undo, reset the patch stack to the state it +had before the undo. Consecutive invocations of "stg redo" will undo +the effects of consecutive invocations of "stg undo". + +It is an error to run "stg redo" if the last command was not an +undo.""" + +options = [ + opt('-n', '--number', type = 'int', metavar = 'N', default = 1, + short = 'Undo the last N undos'), + opt('--hard', action = 'store_true', + short = 'Discard changes in your index/worktree')] + +directory = common.DirectoryHasRepositoryLib() + +def func(parser, options, args): + stack = directory.repository.current_stack + if options.number < 1: + raise common.CmdException('Bad number of undos to redo') + state = log.undo_state(stack, -options.number) + trans = transaction.StackTransaction(stack, 'redo %d' % options.number, + discard_changes = options.hard) + try: + log.reset_stack(trans, stack.repository.default_iw, state, []) + except transaction.TransactionHalted: + pass + return trans.run(stack.repository.default_iw) diff --git a/stgit/lib/log.py b/stgit/lib/log.py index 8b7d2e4..448532d 100644 --- a/stgit/lib/log.py +++ b/stgit/lib/log.py @@ -461,6 +461,8 @@ def undo_state(stack, undo_steps): operations along the way we have to add those undo steps to C{undo_steps}.) + If C{undo_steps} is negative, redo instead of undo. + @return: The log entry that is the destination of the undo operation @rtype: L{LogEntry}""" @@ -470,13 +472,22 @@ def undo_state(stack, undo_steps): except KeyError: raise LogException('Log is empty') log = get_log_entry(stack.repository, ref, commit) - while undo_steps > 0: + while undo_steps != 0: msg = log.message.strip() - m = re.match(r'^undo\s+(\d+)$', msg) - if m: - undo_steps += int(m.group(1)) + um = re.match(r'^undo\s+(\d+)$', msg) + if undo_steps > 0: + if um: + undo_steps += int(um.group(1)) + else: + undo_steps -= 1 else: - undo_steps -= 1 + rm = re.match(r'^redo\s+(\d+)$', msg) + if um: + undo_steps += 1 + elif rm: + undo_steps -= int(rm.group(1)) + else: + raise LogException('No more redo information available') if not log.prev: raise LogException('Not enough undo information available') log = log.prev diff --git a/t/t3104-redo.sh b/t/t3104-redo.sh new file mode 100755 index 0000000..030311d --- /dev/null +++ b/t/t3104-redo.sh @@ -0,0 +1,114 @@ +#!/bin/sh + +test_description='Simple test cases for "stg redo"' + +. ./test-lib.sh + +# Ignore our own output files. +cat > .git/info/exclude <> a && + git add a && + git commit -m a && + echo 111 >> a && + git commit -a -m p1 && + echo 222 >> a && + git commit -a -m p2 && + echo 333 >> a && + git commit -a -m p3 && + stg uncommit -n 3 +' + +cat > expected.txt < expected.txt < expected.txt < expected.txt < expected.txt < expected.txt < expected.txt < expected.txt <