Skip to content

Commit e9f8f15

Browse files
committed
fix(remote): allow to raise during push/fetch
Do not swallow non-zero exit status during push and fetch unless we managed to parse head information. This behaviour will effetively handle cases were no work was done due to invalid refspecs or insufficient permissions. Fixes #271
1 parent 723f100 commit e9f8f15

File tree

4 files changed

+18
-34
lines changed

4 files changed

+18
-34
lines changed

Diff for: doc/source/changes.rst

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ Changelog
77
* `IndexFile.add()` will now write the index without any extension data by default. However, you may override this behaviour with the new `write_extension_data` keyword argument.
88

99
- Renamed `ignore_tree_extension_data` keyword argument in `IndexFile.write(...)` to `ignore_extension_data`
10+
* If the git command executed during `Remote.push(...)|fetch(...)` returns with an non-zero exit code and GitPython didn't
11+
obtain any head-information, the corresponding `GitCommandError` will be raised. This may break previous code which expected
12+
these operations to never raise. However, that behavious is undesirable as it would effectively hide the fact that there
13+
was an error. See `this issue <https://github.com/gitpython-developers/GitPython/issues/271>`_ for more information.
1014

1115
0.3.6 - Features
1216
================

Diff for: git/remote.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,12 @@ def _get_fetch_info_from_stderr(self, proc, progress):
568568
# end
569569

570570
# We are only interested in stderr here ...
571-
finalize_process(proc)
571+
try:
572+
finalize_process(proc)
573+
except Exception:
574+
if len(fetch_info_lines) == 0:
575+
raise
576+
# end exception handler
572577

573578
# read head information
574579
fp = open(join(self.repo.git_dir, 'FETCH_HEAD'), 'rb')
@@ -601,7 +606,11 @@ def stdout_handler(line):
601606
# END exception handling
602607
# END for each line
603608

604-
handle_process_output(proc, stdout_handler, progress_handler, finalize_process)
609+
try:
610+
handle_process_output(proc, stdout_handler, progress_handler, finalize_process)
611+
except Exception:
612+
if len(output) == 0:
613+
raise
605614
return output
606615

607616
def fetch(self, refspec=None, progress=None, **kwargs):

Diff for: git/test/test_remote.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,7 @@ def _assert_push_and_pull(self, remote, rw_repo, remote_repo):
313313
self._do_test_push_result(res, remote)
314314

315315
# invalid refspec
316-
res = remote.push("hellothere")
317-
assert len(res) == 0
316+
self.failUnlessRaises(GitCommandError, remote.push, "hellothere")
318317

319318
# push new tags
320319
progress = TestRemoteProgress()

Diff for: git/util.py

+2-30
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@
1616

1717
# NOTE: Some of the unused imports might be used/imported by others.
1818
# Handle once test-cases are back up and running.
19-
from .exc import (
20-
GitCommandError,
21-
InvalidGitRepositoryError
22-
)
19+
from .exc import InvalidGitRepositoryError
2320

2421
from .compat import (
2522
MAXSIZE,
@@ -154,32 +151,7 @@ def get_user_id():
154151

155152
def finalize_process(proc):
156153
"""Wait for the process (clone, fetch, pull or push) and handle its errors accordingly"""
157-
try:
158-
proc.wait()
159-
except GitCommandError:
160-
# if a push has rejected items, the command has non-zero return status
161-
# a return status of 128 indicates a connection error - reraise the previous one
162-
# Everything else will still be parsed and made available through PushInfo flags
163-
# Estimated error results look like this:
164-
# ```bash
165-
# To /var/folders/xp/m48gs2tx2vg95tmtzw7tprs40000gn/T/tmpk5jeBeremote_repo_test_base
166-
# ! refs/heads/master:refs/heads/master [rejected] (non-fast-forward)
167-
# Done
168-
# error: failed to push some refs to
169-
# '/var/folders/xp/m48gs2tx2vg95tmtzw7tprs40000gn/T/tmpk5jeBeremote_repo_test_base'
170-
# hint: Updates were rejected because the tip of your current branch is behind
171-
# hint: its remote counterpart. Integrate the remote changes (e.g.
172-
# hint: 'git pull ...') before pushing again.
173-
# hint: See the 'Note about fast-forwards' in 'git push --help' for details.
174-
# ```
175-
# See https://github.com/gitpython-developers/GitPython/blob/master/git/test/test_remote.py#L305
176-
# on how to check for these kinds of errors.
177-
# Also see this issue for a reason for this verbosity:
178-
# https://github.com/gitpython-developers/GitPython/issues/271
179-
if proc.poll() == 128:
180-
raise
181-
pass
182-
# END exception handling
154+
proc.wait()
183155

184156
#} END utilities
185157

0 commit comments

Comments
 (0)