Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5b01b58

Browse files
committed
made with_exceptions=True default (don't look before you leak ;)) and removed the combined output of stderr and stdout.
Also renamed with_status to extended_output. The method_missing function needs to be modified, as it does a kwargs.pop(xxx, None); which resulted in with_excpetions=None -> False all the time... Test should follow tomorrow.
1 parent 66beac6 commit 5b01b58

File tree

3 files changed

+38
-53
lines changed

3 files changed

+38
-53
lines changed

lib/git/cmd.py

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ def get_dir(self):
7878
def execute(self, command,
7979
istream=None,
8080
keep_cwd=False,
81-
with_status=False,
81+
extended_output=False,
8282
with_stderr=False,
83-
with_exceptions=False,
83+
with_exceptions=True,
8484
with_raw_output=False,
8585
):
8686
"""
@@ -98,11 +98,8 @@ def execute(self, command,
9898
GitPython uses get_work_tree() as its working directory by
9999
default and get_git_dir() for bare repositories.
100100
101-
``with_status``
102-
Whether to return a (status, str) tuple.
103-
104-
``with_stderr``
105-
Whether to combine stderr into the output.
101+
``extended_output``
102+
Whether to return a (status, stdout, stderr) tuple.
106103
107104
``with_exceptions``
108105
Whether to raise an exception when git returns a non-zero status.
@@ -111,20 +108,13 @@ def execute(self, command,
111108
Whether to avoid stripping off trailing whitespace.
112109
113110
Returns
114-
str(output) # with_status = False (Default)
115-
tuple(int(status), str(output)) # with_status = True
111+
str(output) # extended_output = False (Default)
112+
tuple(int(status), str(output)) # extended_output = True
116113
"""
117114

118115
if GIT_PYTHON_TRACE and not GIT_PYTHON_TRACE == 'full':
119116
print ' '.join(command)
120117

121-
# Allow stderr to be merged into stdout when with_stderr is True.
122-
# Otherwise, throw stderr away.
123-
if with_stderr:
124-
stderr = subprocess.STDOUT
125-
else:
126-
stderr = subprocess.PIPE
127-
128118
# Allow the user to have the command executed in their working dir.
129119
if keep_cwd:
130120
cwd = os.getcwd()
@@ -135,28 +125,29 @@ def execute(self, command,
135125
proc = subprocess.Popen(command,
136126
cwd=cwd,
137127
stdin=istream,
138-
stderr=stderr,
128+
stderr=subprocess.PIPE,
139129
stdout=subprocess.PIPE
140130
)
141131

142132
# Wait for the process to return
143-
stdout_value = proc.stdout.read()
144-
status = proc.wait()
145-
proc.stdout.close()
146-
147-
if proc.stderr:
148-
stderr_value = proc.stderr.read()
149-
proc.stderr.close()
133+
try:
134+
stdout_value = proc.stdout.read()
135+
stderr_value = proc.stderr.read()
136+
status = proc.wait()
137+
finally:
138+
proc.stdout.close()
139+
proc.stderr.close()
150140

151141
# Strip off trailing whitespace by default
152142
if not with_raw_output:
153143
stdout_value = stdout_value.rstrip()
144+
stderr_value = stderr_value.rstrip()
145+
146+
print command, status
154147

155-
# Grab the exit status
156-
status = proc.poll()
157148
if with_exceptions and status != 0:
158-
raise GitCommandError("%s returned exit status %d"
159-
% (str(command), status))
149+
print 19
150+
raise GitCommandError(command, status, stderr_value)
160151

161152
if GIT_PYTHON_TRACE == 'full':
162153
if stderr_value:
@@ -167,8 +158,8 @@ def execute(self, command,
167158
print "%s -> %d" % (command, status)
168159

169160
# Allow access to the command's status code
170-
if with_status:
171-
return (status, stdout_value)
161+
if extended_output:
162+
return (status, stdout_value, stderr_value)
172163
else:
173164
return stdout_value
174165

@@ -217,9 +208,9 @@ def method_missing(self, method, *args, **kwargs):
217208
# otherwise these'll end up in args, which is bad.
218209
istream = kwargs.pop("istream", None)
219210
keep_cwd = kwargs.pop("keep_cwd", None)
220-
with_status = kwargs.pop("with_status", None)
211+
extended_output = kwargs.pop("extended_output", None)
221212
with_stderr = kwargs.pop("with_stderr", None)
222-
with_exceptions = kwargs.pop("with_exceptions", None)
213+
with_exceptions = kwargs.pop("with_exceptions", True)
223214
with_raw_output = kwargs.pop("with_raw_output", None)
224215

225216
# Prepare the argument list
@@ -233,7 +224,7 @@ def method_missing(self, method, *args, **kwargs):
233224
return self.execute(call,
234225
istream = istream,
235226
keep_cwd = keep_cwd,
236-
with_status = with_status,
227+
extended_output = extended_output,
237228
with_stderr = with_stderr,
238229
with_exceptions = with_exceptions,
239230
with_raw_output = with_raw_output,

lib/git/errors.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,12 @@ class NoSuchPathError(Exception):
55
pass
66

77
class GitCommandError(Exception):
8-
pass
8+
def __init__(self, command, status, stderr=None):
9+
self.stderr = stderr
10+
self.status = status
11+
self.command = command
12+
13+
def __str__(self):
14+
return repr("%s returned exit status %d" %
15+
(str(self.command), self.status))
16+

test/git/test_git.py

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ def test_method_missing_calls_execute(self, git):
1414
assert_true(git.called)
1515
# assert_equal(git.call_args, ((("%s version " % self.git_bin_base),), {}))
1616

17+
@raises(GitCommandError)
18+
def test_it_raises_errors(self):
19+
self.git.this_does_not_exist()
20+
21+
1722
def test_it_transforms_kwargs_into_git_command_arguments(self):
1823
assert_equal(["-s"], self.git.transform_kwargs(**{'s': True}))
1924
assert_equal(["-s5"], self.git.transform_kwargs(**{'s': 5}))
@@ -33,25 +38,6 @@ def test_it_accepts_stdin(self):
3338
self.git.hash_object(istream=fh, stdin=True))
3439
fh.close()
3540

36-
def test_it_returns_status_and_ignores_stderr(self):
37-
assert_equal((1, ""), self.git.this_does_not_exist(with_status=True))
38-
39-
@raises(GitCommandError)
40-
def test_it_raises_errors(self):
41-
self.git.this_does_not_exist(with_exceptions=True)
42-
43-
def test_it_returns_stderr_in_output(self):
44-
# Note: no trailiing newline
45-
assert_match(r"^git: 'this-does-not-exist' is not a git-command",
46-
self.git.this_does_not_exist(with_stderr=True))
47-
48-
def test_it_does_not_strip_output_when_using_with_raw_output(self):
49-
# Note: trailing newline
50-
assert_match(r"^git: 'this-does-not-exist' is not a git-command" \
51-
r"(\. See 'git --help'\.)?" + os.linesep,
52-
self.git.this_does_not_exist(with_stderr=True,
53-
with_raw_output=True))
54-
5541
def test_it_handles_large_input(self):
5642
output = self.git.execute(["cat", "/bin/bash"])
5743
assert_true(len(output) > 4096) # at least 4k

0 commit comments

Comments
 (0)