From: Karl Hasselström Date: Sun, 21 Sep 2008 12:17:40 +0000 (+0200) Subject: New command: stg reset X-Git-Tag: v0.15-rc1~147 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~mdw/git/stgit/commitdiff_plain/3a3a4f2f9876f08f09d6bc6771cb951bba285375 New command: stg reset Given a commit object from the log, resets the stack (or just the named patches) to the state given by that log entry. Signed-off-by: Karl Hasselström --- diff --git a/stgit/commands/reset.py b/stgit/commands/reset.py new file mode 100644 index 0000000..b637841 --- /dev/null +++ b/stgit/commands/reset.py @@ -0,0 +1,122 @@ +# -*- 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.commands import common +from stgit.lib import git, log, transaction +from stgit.out import out + +help = 'Reset the patch stack to an earlier state' +kind = 'stack' +usage = [' []'] +description = """ +Reset the patch stack to an earlier state. The state is specified with +a commit from a stack log; for a branch foo, StGit stores the stack +log in foo.stgit^. So to undo the last N StGit commands (or rather, +the last N log entries; there is not an exact one-to-one +relationship), you would say + + stg reset foo.stgit^~N + +or, if you are not sure how many steps to undo, you can view the log +with "git log" or gitk + + gitk foo.stgit^ + +and then reset to any sha1 you see in the log. + +If one or more patch names are given, reset only those patches, and +leave the rest alone.""" + +options = [] + +directory = common.DirectoryHasRepositoryLib() + +def reset_stack(stack, iw, state, only_patches): + only_patches = set(only_patches) + def mask(s): + if only_patches: + return s & only_patches + else: + return s + patches_to_reset = mask(set(state.applied + state.unapplied + state.hidden)) + existing_patches = set(stack.patchorder.all) + to_delete = mask(existing_patches - patches_to_reset) + trans = transaction.StackTransaction(stack, 'reset') + + # If we have to change the stack base, we need to pop all patches + # first. + if not only_patches and trans.base != state.base: + trans.pop_patches(lambda pn: True) + out.info('Setting stack base to %s' % state.base.sha1) + trans.base = state.base + + # In one go, do all the popping we have to in order to pop the + # patches we're going to delete or modify. + def mod(pn): + if only_patches and not pn in only_patches: + return False + if pn in to_delete: + return True + if stack.patches.get(pn).commit != state.patches.get(pn, None): + return True + return False + trans.pop_patches(mod) + + # Delete and modify/create patches. We've previously popped all + # patches that we touch in this step. + trans.delete_patches(lambda pn: pn in to_delete) + for pn in patches_to_reset: + if pn in existing_patches: + if trans.patches[pn] == state.patches[pn]: + continue + else: + out.info('Resetting %s' % pn) + else: + if pn in state.hidden: + trans.hidden.append(pn) + else: + trans.unapplied.append(pn) + out.info('Resurrecting %s' % pn) + trans.patches[pn] = state.patches[pn] + + # Push/pop patches as necessary. + try: + if only_patches: + # Push all the patches that we've popped, if they still + # exist. + pushable = set(trans.unapplied) + for pn in stack.patchorder.applied: + if pn in pushable: + trans.push_patch(pn, iw) + else: + # Recreate the exact order specified by the goal state. + trans.reorder_patches(state.applied, state.unapplied, + state.hidden, iw) + except transaction.TransactionHalted: + pass + return trans.run(iw) + +def func(parser, options, args): + stack = directory.repository.current_stack + if len(args) >= 1: + ref, patches = args[0], args[1:] + state = log.get_log_entry(stack.repository, ref) + else: + raise common.CmdException('Wrong number of arguments') + return reset_stack(stack, stack.repository.default_iw, state, patches) diff --git a/stgit/lib/log.py b/stgit/lib/log.py index cb32d3e..124fbcf 100644 --- a/stgit/lib/log.py +++ b/stgit/lib/log.py @@ -151,6 +151,12 @@ class LogEntry(object): if self.__prev != None and not isinstance(self.__prev, LogEntry): self.__prev = self.from_commit(self.__repo, self.__prev) return self.__prev + @property + def base(self): + if self.applied: + return self.patches[self.applied[0]].data.parent + else: + return self.head @classmethod def from_stack(cls, prev, stack, message): return cls( @@ -280,6 +286,12 @@ class LogEntry(object): tree = tree, message = self.message, parents = [self.simplified] + parents)) +def get_log_entry(repo, ref): + try: + return LogEntry.from_commit(repo, repo.rev_parse(ref)) + except LogException, e: + raise LogException('While reading log from %s: %s' % (ref, e)) + def log_entry(stack, msg): """Write a new log entry for the stack.""" ref = log_ref(stack.name) diff --git a/t/t3100-reset.sh b/t/t3100-reset.sh new file mode 100755 index 0000000..0b5cd2c --- /dev/null +++ b/t/t3100-reset.sh @@ -0,0 +1,160 @@ +#!/bin/sh + +test_description='Simple test cases for "stg reset"' + +. ./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 && + stg pop +' + +cat > expected.txt < expected.txt < expected.txt < expected.txt < expected.txt < expected.txt < expected.txt <> a && + stg refresh && + test "$(echo $(stg series --all))" = "+ p1 > p2" && + test_cmp expected.txt a +' + +cat > expected.txt <