Check for local changes with "goto"
PATCH 1/1 Check for local changes with "goto"
This is done by default, unless the --keep option is passed, for consistency with the "pop" command. The index is checked in the Transaction.run() function so that other commands could benefit from this feature (off by default).
--- stgit/commands/goto.py | 8 ++++++-- stgit/lib/transaction.py | 7 ++++++- t/t2300-refresh-subdir.sh | 2 +- t/t2800-goto-subdir.sh | 4 ++-- t/t3000-dirty-merge.sh | 2 +- 5 files changed, 16 insertions(+), 7 deletions(-)
stgit/commands/goto.py +6 -2
@@ -18,6 +18,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA from stgit.commands import common from stgit.lib import transaction from stgit import argparse +from stgit.argparse import opt help = 'Push or pop patches to the given one' kind = 'stack'
@@ -27,7 +28,10 @@ Push/pop patches to/from the stack until the one given on the command line becomes current.""" args = [argparse.other_applied_patches, argparse.unapplied_patches] -options = [] +options = [ + opt('-k', '--keep', action = 'store_true', + short = 'Keep the local changes') +] directory = common.DirectoryHasRepositoryLib()
@@ -52,4 +56,4 @@ def func(parser, options, args): raise common.CmdException('Cannot goto a hidden patch') else: raise common.CmdException('Patch "%s" does not exist' % patch) - return trans.run(iw) + return trans.run(iw, check_clean = not options.keep)
stgit/lib/transaction.py +6 -1
@@ -183,13 +183,18 @@ class StackTransaction(object): self.__checkout(self.__stack.head.data.tree, iw, allow_bad_head = True) def run(self, iw = None, set_head = True, allow_bad_head = False, - print_current_patch = True): + print_current_patch = True, check_clean = False): """Execute the transaction. Will either succeed, or fail (with an exception) and do nothing.""" self.__check_consistency() log.log_external_mods(self.__stack) new_head = self.head + # Check for not clean index + if check_clean and iw and not iw.index.is_clean(): + self.__halt('Repository not clean. Use "refresh" or ' + '"status --reset"') + # Set branch head. if set_head: if iw:
t/t2300-refresh-subdir.sh +1 -1
@@ -65,7 +65,7 @@ test_expect_success 'refresh -u -p <subdir>' ' test_expect_success 'refresh an unapplied patch' ' stg refresh -u && - stg goto p0 && + stg goto --keep p0 && test "$(stg status)" = "M foo.txt" && stg refresh -p p1 && test "$(stg status)" = "" &&
t/t2800-goto-subdir.sh +2 -2
@@ -25,7 +25,7 @@ cat > expected2.txt <<EOF bar EOF test_expect_success 'Goto in subdirectory (just pop)' ' - (cd foo && stg goto p1) && + (cd foo && stg goto --keep p1) && cat foo/bar > actual.txt && test_cmp expected1.txt actual.txt && ls foo > actual.txt &&
@@ -48,7 +48,7 @@ cat > expected2.txt <<EOF bar EOF test_expect_success 'Goto in subdirectory (conflicting push)' ' - (cd foo && stg goto p3) ; + (cd foo && stg goto --keep p3) ; [ $? -eq 3 ] && cat foo/bar > actual.txt && test_cmp expected1.txt actual.txt &&
t/t3000-dirty-merge.sh +1 -1
@@ -26,7 +26,7 @@ test_expect_success 'Push with dirty worktree' ' echo 4 > a && [ "$(echo $(stg series --applied --noprefix))" = "p1" ] && [ "$(echo $(stg series --unapplied --noprefix))" = "p2" ] && - conflict stg goto p2 && + conflict stg goto --keep p2 && [ "$(echo $(stg series --applied --noprefix))" = "p1" ] && [ "$(echo $(stg series --unapplied --noprefix))" = "p2" ] && [ "$(echo $(cat a))" = "4" ]
Quoted 4 lines · Catalin Marinas
This is done by default, unless the --keep option is passed, for consistency with the "pop" command. The index is checked in the
Transaction.run()function so that other commands could benefit from this feature (off by default).
This looks good, except for ...
stgit/lib/transaction.py class StackTransaction(object): · line 196
Quoted 4 lines
stgit/lib/transaction.py +4
-0+ # Check for not clean index + if check_clean and iw and not iw.index.is_clean(): + self.__halt('Repository not clean. Use "refresh" or ' + '"status --reset"')
... this, which doesn't do what I think you think it does.
Index.is_clean() calls "git update-index --refresh", which checks for changes in the worktree relative to the index. It's bad design to have it in Index rather than IndexAndWorktree, but that's my fault, not yours. ;-) But the point that breaks your patch is that it doesn't check for changes between index and HEAD -- try it and see.
The fix I'd suggest is to move the existing is_clean() method to IndexAndWorktree, and call it maybe worktree_clean(). And create a method in Index() called is_clean(tree) that checks whether the index is clean with respect to the given Tree (I think this method should just call "git diff-index --quiet --cached <tree>".). Then call both of these methods.
Sorry if I just keep creating more work for you. :-/
Signature · 2 lines
Karl Hasselström, [email protected] www.treskal.com/kalle
2009/1/29 Karl Hasselström <[email protected]>:
Quoted 14 lines · Catalin Marinas
Review
Quoted 4 lines
(quoted diff) +4
-0+ # Check for not clean index + if check_clean and iw and not iw.index.is_clean(): + self.__halt('Repository not clean. Use "refresh" or ' + '"status --reset"')... this, which doesn't do what I think you think it does.
Index.is_clean()calls "git update-index --refresh", which checks for changes in the worktree relative to the index. It's bad design to have it in Index rather than IndexAndWorktree, but that's my fault, not yours. ;-) But the point that breaks your patch is that it doesn't check for changes between index and HEAD -- try it and see.
Quoted 6 lines
The fix I'd suggest is to move the existing
is_clean()method to IndexAndWorktree, and call it maybeworktree_clean(). And create a method inIndex()calledis_clean(tree)that checks whether the index is clean with respect to the given Tree (I think this method should just call "git diff-index --quiet --cached <tree>".). Then call both of these methods.
What about this (only pasting the relevant hunks, though they may be wrapped by the web interface):
stgit/lib/git.py +17 -2
@@ -706,9 +706,15 @@ class Index(RunWithEnv): ).output_one_line()) except run.RunException: raise MergeException('Conflicting merge') - def is_clean(self): + def is_clean(self, tree = None): + """Check whether the index is clean relative to the given tree.""" + if tree: + sha1 = tree.sha1 + else: + sha1 = 'HEAD' try: - self.run(['git', 'update-index', '--refresh']).discard_output() + self.run(['git', 'diff-index', '--quiet', '--cached', sha1] + ).discard_output() except run.RunException: return False else:
@@ -858,6 +864,15 @@ class IndexAndWorktree(RunWithEnvCwd): cmd = ['git', 'update-index', '--remove'] self.run(cmd + ['-z', '--stdin'] ).input_nulterm(paths).discard_output() + def worktree_clean(self): + """Check whether the worktree is clean relative and no updates or + merges are needed.""" + try: + self.run(['git', 'update-index', '--refresh']).discard_output() + except run.RunException: + return False + else: + return True class Branch(object): """Represents a Git branch."""
stgit/lib/transaction.py +10 -1
@@ -183,13 +183,22 @@ class StackTransaction(object): self.__checkout(self.__stack.head.data.tree, iw, allow_bad_head = True) def run(self, iw = None, set_head = True, allow_bad_head = False, - print_current_patch = True): + print_current_patch = True, check_clean = False): """Execute the transaction. Will either succeed, or fail (with an exception) and do nothing.""" self.__check_consistency() log.log_external_mods(self.__stack) new_head = self.head + # Check for not clean index and worktree + if check_clean and iw: + if not iw.worktree_clean(): + self.__halt('Repository not clean. Use "refresh" or ' + '"status --reset"') + elif not iw.index.is_clean()): + self.__halt('Index and HEAD different. Use "repair" to ' + 'recover additional commits') + # Set branch head. if set_head: if iw:
Thanks.
Signature · 1 line
Catalin
Quoted 8 lines · Catalin Marinas
stgit/lib/git.py +3
-1@@ -706,9 +706,15 @@ class Index(RunWithEnv): ).output_one_line()) except run.RunException: raise MergeException('Conflicting merge') - def is_clean(self): + def is_clean(self, tree = None): + """Check whether the index is clean relative to the given tree.""" + if tree:
stgit/lib/git.py class Index(RunWithEnv): · line 720
Quoted 10 lines
stgit/lib/git.py +5
-1+ sha1 = tree.sha1 + else: + sha1 = 'HEAD' try: - self.run(['git', 'update-index', '--refresh']).discard_output() + self.run(['git', 'diff-index', '--quiet', '--cached', sha1] + ).discard_output() except run.RunException: return False else:
OK (though I personally would have allowed only Tree objects, with no defaulting to the current HEAD).
The docstring should say s/tree/treeish/.
stgit/lib/git.py class IndexAndWorktree(RunWithEnvCwd): · line 875
Quoted 13 lines
stgit/lib/git.py +9
-0@@ -858,6 +864,15 @@ class IndexAndWorktree(RunWithEnvCwd): cmd = ['git', 'update-index', '--remove'] self.run(cmd + ['-z', '--stdin'] ).input_nulterm(paths).discard_output() + def worktree_clean(self): + """Check whether the worktree is clean relative and no updates or + merges are needed.""" + try: + self.run(['git', 'update-index', '--refresh']).discard_output() + except run.RunException: + return False + else: + return True
Clean relative to the index.
And what do merges have to do with it?
stgit/lib/transaction.py class StackTransaction(object): · line 200
Quoted 8 lines
stgit/lib/transaction.py +8
-0+ # Check for not clean index and worktree + if check_clean and iw: + if not iw.worktree_clean(): + self.__halt('Repository not clean. Use "refresh" or ' + '"status --reset"') + elif not iw.index.is_clean()): + self.__halt('Index and HEAD different. Use "repair" to ' + 'recover additional commits')
The first message is good, but the second one is misleading -- you'd be much better off just reusing the same message as for the first case. (You could recommend refresh --index if you want to get fancy.)
Signature · 2 lines
Karl Hasselström, [email protected] www.treskal.com/kalle
2009/1/30 Karl Hasselström <[email protected]>:
Quoted 15 lines · Catalin Marinas
Quoted 14 lines
(quoted diff) +6
-2@@ -706,9 +706,15 @@ class Index(RunWithEnv): ).output_one_line()) except run.RunException: raise MergeException('Conflicting merge') - def is_clean(self): + def is_clean(self, tree = None): + """Check whether the index is clean relative to the given tree.""" + if tree: + sha1 = tree.sha1 + else: + sha1 = 'HEAD' try: - self.run(['git', 'update-index', '--refresh']).discard_output()
Quoted 8 lines
Review class Index(RunWithEnv): · line 720
Quoted 5 lines
(quoted diff) +2
-0+ self.run(['git', 'diff-index', '--quiet', '--cached', sha1] + ).discard_output() except run.RunException: return False else:OK (though I personally would have allowed only Tree objects, with no defaulting to the current HEAD).
Done.
See below for an update. I added an __assert_index_worktree_clean() function in Transaction.
Now, should we add the check_clean argument to Transaction.__init__() rather than run() as we do for the allow_bad_head case? The check would need to be done in run() where we have an iw. Just a thought, I'm not convinced it is better.
stgit/lib/git.py +12 -2
@@ -706,9 +706,11 @@ class Index(RunWithEnv): ).output_one_line()) except run.RunException: raise MergeException('Conflicting merge') - def is_clean(self): + def is_clean(self, tree): + """Check whether the index is clean relative to the given treeish.""" try: - self.run(['git', 'update-index', '--refresh']).discard_output() + self.run(['git', 'diff-index', '--quiet', '--cached', tree.sha1] + ).discard_output() except run.RunException: return False else:
@@ -858,6 +860,14 @@ class IndexAndWorktree(RunWithEnvCwd): cmd = ['git', 'update-index', '--remove'] self.run(cmd + ['-z', '--stdin'] ).input_nulterm(paths).discard_output() + def worktree_clean(self): + """Check whether the worktree is clean relative to index.""" + try: + self.run(['git', 'update-index', '--refresh']).discard_output() + except run.RunException: + return False + else: + return True class Branch(object): """Represents a Git branch."""
stgit/lib/transaction.py +10 -1
@@ -147,6 +147,11 @@ class StackTransaction(object): 'This can happen if you modify a branch with git.', '"stg repair --help" explains more about what to do next.') self.__abort() + def __assert_index_worktree_clean(self, iw): + if not iw.worktree_clean() or \ + not iw.index.is_clean(self.stack.head.data.tree): + self.__halt('Repository not clean. Use "refresh" or ' + '"status --reset"') def __checkout(self, tree, iw, allow_bad_head): if not allow_bad_head: self.__assert_head_top_equal()
@@ -183,13 +188,17 @@ class StackTransaction(object): self.__checkout(self.__stack.head.data.tree, iw, allow_bad_head = True) def run(self, iw = None, set_head = True, allow_bad_head = False, - print_current_patch = True): + print_current_patch = True, check_clean = False): """Execute the transaction. Will either succeed, or fail (with an exception) and do nothing.""" self.__check_consistency() log.log_external_mods(self.__stack) new_head = self.head + # Check for clean index and worktree + if check_clean and iw: + self.__assert_index_worktree_clean(iw) + # Set branch head. if set_head: if iw:
Signature · 1 line
Catalin
2009/1/30 Catalin Marinas <[email protected]>:
Quoted 2 lines
Now, should we add the
check_cleanargument toTransaction.__init__()rather thanrun()as we do for theallow_bad_headcase?
It looks like this may be a better option. The previous patch fails if "goto" pushes a patch with standard git-apply followed by another patch with a three-way merge. When Transaction.run() is called, even if the patch pushing succeeded, the function complains about local changes because of the "iw.index.is_clean(self.stack.head)" check.
It is also a bit weird to push/pop patches and only complain at the end of local changes. Below is an updated patch which does the checking in Transaction.__init__ (only the relevant parts of the patch):
stgit/lib/git.py +12 -2
@@ -706,9 +706,11 @@ class Index(RunWithEnv): ).output_one_line()) except run.RunException: raise MergeException('Conflicting merge') - def is_clean(self): + def is_clean(self, tree): + """Check whether the index is clean relative to the given treeish.""" try: - self.run(['git', 'update-index', '--refresh']).discard_output() + self.run(['git', 'diff-index', '--quiet', '--cached', tree.sha1] + ).discard_output() except run.RunException: return False else:
@@ -858,6 +860,14 @@ class IndexAndWorktree(RunWithEnvCwd): cmd = ['git', 'update-index', '--remove'] self.run(cmd + ['-z', '--stdin'] ).input_nulterm(paths).discard_output() + def worktree_clean(self): + """Check whether the worktree is clean relative to index.""" + try: + self.run(['git', 'update-index', '--refresh']).discard_output() + except run.RunException: + return False + else: + return True class Branch(object): """Represents a Git branch."""
stgit/lib/transaction.py +10 -1
@@ -75,7 +75,8 @@ class StackTransaction(object): your refs and index+worktree, or fail without having done anything.""" def __init__(self, stack, msg, discard_changes = False, - allow_conflicts = False, allow_bad_head = False): + allow_conflicts = False, allow_bad_head = False, + check_clean = False): """Create a new L{StackTransaction}. @param discard_changes: Discard any changes in index+worktree
@@ -102,6 +103,8 @@ class StackTransaction(object): self.__temp_index = self.temp_index_tree = None if not allow_bad_head: self.__assert_head_top_equal() + if check_clean: + self.__assert_index_worktree_clean() stack = property(lambda self: self.__stack) patches = property(lambda self: self.__patches) def __set_applied(self, val):
@@ -147,6 +150,12 @@ class StackTransaction(object): 'This can happen if you modify a branch with git.', '"stg repair --help" explains more about what to do next.') self.__abort() + def __assert_index_worktree_clean(self): + iw = self.__stack.repository.default_iw + if not iw.worktree_clean() or \ + not iw.index.is_clean(self.stack.head): + self.__halt('Repository not clean. Use "refresh" or ' + '"status --reset"') def __checkout(self, tree, iw, allow_bad_head): if not allow_bad_head: self.__assert_head_top_equal()
Signature · 1 line
Catalin
Quoted 7 lines · Catalin Marinas
2009/1/30 Catalin Marinas <[email protected]>:
Quoted 3 lines
Now, should we add the
check_cleanargument toTransaction.__init__()rather thanrun()as we do for theallow_bad_headcase?It looks like this may be a better option.
Sorry for taking so long to respond, but ... I strongly advise against using default_iw in transaction.py. It's library code, and it should take stuff like index and worktree as input parameters from layers that are higher up in the abstraction stack. Compare the kernel policy of having policy in userspace and not in the kernel.
And if you accept that reasoning, the check has to go in run() rather than __init__(), because we don't have an iw in __init__(). (Though we could add such a parameter, I guess.)
Quoted 5 lines
The previous patch fails if "goto" pushes a patch with standard git-apply followed by another patch with a three-way merge. When
Transaction.run()is called, even if the patch pushing succeeded, the function complains about local changes because of the "iw.index.is_clean(self.stack.head)" check.
Hmm, so that would have to be worked around somehow ... I guess doing the check in __init__() might make sense after all, since that's before we start changing things. How about adding a check_clean_relative_to paramter to __init__() that's not a boolean, but an iw to check against? It would default to None, meaning no check.
Quoted 2 lines
It is also a bit weird to push/pop patches and only complain at the end of local changes.
You mean the behavior the new infrastructure currently gives you? It's actually convenient in a number of cases. Assume for example that you have patch A that changes file foo, patch B that changes file bar, and then local changes to file bar. At this point you can pop A without problem, even though a middle stage is to pop and push B which touches the same file as your local changes -- the existing checks will only compare the diff between the original and final tree with your local changes, and that diff doesn't contain bar.
Quoted 2 lines
Below is an updated patch which does the checking in Transaction.__init__ (only the relevant parts of the patch):
Looks good, except for the hard-coded default_iw as I mentioned above.
Signature · 2 lines
Karl Hasselström, [email protected] www.treskal.com/kalle
2009/2/6 Karl Hasselström <[email protected]>:
Quoted 8 lines · Catalin Marinas
Quoted 7 lines
2009/1/30 Catalin Marinas <[email protected]>:
Quoted 3 lines
Now, should we add the
check_cleanargument toTransaction.__init__()rather thanrun()as we do for theallow_bad_headcase?
Quoted 6 lines
Quoted 1 line
It looks like this may be a better option.
Sorry for taking so long to respond, but ... I strongly advise against using
default_iwintransaction.py. It's library code, and it should take stuff like index and worktree as input parameters from layers that are higher up in the abstraction stack.
OK, no problem with that.
Quoted 6 lines
Quoted 5 lines
The previous patch fails if "goto" pushes a patch with standard git-apply followed by another patch with a three-way merge. When
Transaction.run()is called, even if the patch pushing succeeded, the function complains about local changes because of the "iw.index.is_clean(self.stack.head)" check.
Quoted 6 lines
Hmm, so that would have to be worked around somehow ... I guess doing the check in
__init__()might make sense after all, since that's before we start changing things. How about adding acheck_clean_relative_toparamter to__init__()that's not a boolean, but an iw to check against? It would default to None, meaning no check.
OK, that's better.
Quoted 11 lines
Quoted 2 lines
It is also a bit weird to push/pop patches and only complain at the end of local changes.
You mean the behavior the new infrastructure currently gives you? It's actually convenient in a number of cases. Assume for example that you have patch A that changes file foo, patch B that changes file bar, and then local changes to file bar. At this point you can pop A without problem, even though a middle stage is to pop and push B which touches the same file as your local changes -- the existing checks will only compare the diff between the original and final tree with your local changes, and that diff doesn't contain bar.
Yes, I agree that's a nice feature and it would still be available with the --keep option (I wrote in the past why I wouldn't leave the current behaviour to be the default).
Above I was referring to the new behaviour which checks for local changes by default (--keep not passed). If we do the test in run() you may push or pop patches and only fail at the end when actually the operation shouldn't have started. I plan to add the auto interactive merging to the new infrastructure (by invoking mergetool if IndexAndWorktree.merge() fails, based on the stgit.autoimerge option) and pushing may become a more complex operation if enabled.
I'll repost the patch. Thanks.
Signature · 1 line
Catalin