Skip to content

Commit 2c0b92e

Browse files
committed
cmd: improved error handling and debug printing
head.reset: will now handle resets with paths much better, especially in the --mixed case, see http://github.com/Byron/GitPython/issues#issue/2
1 parent 97ab197 commit 2c0b92e

File tree

4 files changed

+45
-9
lines changed

4 files changed

+45
-9
lines changed

lib/git/cmd.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -337,16 +337,18 @@ def execute(self, command,
337337
proc.stdout.close()
338338
proc.stderr.close()
339339

340-
if with_exceptions and status != 0:
341-
raise GitCommandError(command, status, stderr_value)
342-
343340
if GIT_PYTHON_TRACE == 'full':
341+
cmdstr = " ".join(command)
344342
if stderr_value:
345-
print "%s -> %d: '%s' !! '%s'" % (command, status, stdout_value, stderr_value)
343+
print "%s -> %d; stdout: '%s'; stderr: '%s'" % (cmdstr, status, stdout_value, stderr_value)
346344
elif stdout_value:
347-
print "%s -> %d: '%s'" % (command, status, stdout_value)
345+
print "%s -> %d; stdout: '%s'" % (cmdstr, status, stdout_value)
348346
else:
349-
print "%s -> %d" % (command, status)
347+
print "%s -> %d" % (cmdstr, status)
348+
# END handle debug printing
349+
350+
if with_exceptions and status != 0:
351+
raise GitCommandError(command, status, stderr_value)
350352

351353
# Allow access to the command's status code
352354
if with_extended_output:

lib/git/index/base.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,9 @@ def reset(self, commit='HEAD', working_tree=False, paths=None, head=False, **kwa
10591059
:param head:
10601060
If True, the head will be set to the given commit. This is False by default,
10611061
but if True, this method behaves like HEAD.reset.
1062+
1063+
:param paths: if given as an iterable of absolute or repository-relative paths,
1064+
only these will be reset to their state at the given commit'ish
10621065
10631066
:param kwargs:
10641067
Additional keyword arguments passed to git-reset
@@ -1080,6 +1083,7 @@ def reset(self, commit='HEAD', working_tree=False, paths=None, head=False, **kwa
10801083
else:
10811084
# what we actually want to do is to merge the tree into our existing
10821085
# index, which is what git-read-tree does
1086+
# TODO: incorporate the given paths !
10831087
new_inst = type(self).from_tree(self.repo, commit)
10841088
self.entries = new_inst.entries
10851089
self.write()

lib/git/refs.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
hex_to_bin
3030
)
3131

32+
from exc import GitCommandError
3233

3334
__all__ = ("SymbolicReference", "Reference", "HEAD", "Head", "TagReference",
3435
"RemoteReference", "Tag" )
@@ -646,16 +647,36 @@ def reset(self, commit='HEAD', index=True, working_tree = False,
646647
647648
:return: self"""
648649
mode = "--soft"
650+
add_arg = None
649651
if index:
650652
mode = "--mixed"
651653

654+
# it appears, some git-versions declare mixed and paths deprecated
655+
# see http://github.com/Byron/GitPython/issues#issue/2
656+
if paths:
657+
mode = None
658+
# END special case
659+
# END handle index
660+
652661
if working_tree:
653662
mode = "--hard"
654663
if not index:
655-
raise ValueError( "Cannot reset the working tree if the index is not reset as well")
664+
raise ValueError( "Cannot reset the working tree if the index is not reset as well")
665+
656666
# END working tree handling
657667

658-
self.repo.git.reset(mode, commit, paths, **kwargs)
668+
if paths:
669+
add_arg = "--"
670+
# END nicely separate paths from rest
671+
672+
try:
673+
self.repo.git.reset(mode, commit, add_arg, paths, **kwargs)
674+
except GitCommandError, e:
675+
# git nowadays may use 1 as status to indicate there are still unstaged
676+
# modifications after the reset
677+
if e.status != 1:
678+
raise
679+
# END handle exception
659680

660681
return self
661682

test/git/test_refs.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ def test_is_valid(self):
8989
@with_rw_repo('0.1.6')
9090
def test_head_reset(self, rw_repo):
9191
cur_head = rw_repo.head
92+
old_head_commit = cur_head.commit
9293
new_head_commit = cur_head.ref.commit.parents[0]
9394
cur_head.reset(new_head_commit, index=True) # index only
9495
assert cur_head.reference.commit == new_head_commit
@@ -98,8 +99,16 @@ def test_head_reset(self, rw_repo):
9899
cur_head.reset(new_head_commit, index=True, working_tree=True) # index + wt
99100
assert cur_head.reference.commit == new_head_commit
100101

101-
# paths
102+
# paths - make sure we have something to do
103+
rw_repo.index.reset(old_head_commit.parents[0])
104+
cur_head.reset(cur_head, paths = "test")
102105
cur_head.reset(new_head_commit, paths = "lib")
106+
# hard resets with paths don't work, its all or nothing
107+
self.failUnlessRaises(GitCommandError, cur_head.reset, new_head_commit, working_tree=True, paths = "lib")
108+
109+
# we can do a mixed reset, and then checkout from the index though
110+
cur_head.reset(new_head_commit)
111+
rw_repo.index.checkout(["lib"], force=True)#
103112

104113

105114
# now that we have a write write repo, change the HEAD reference - its

0 commit comments

Comments
 (0)