Skip to content

Commit 460d102

Browse files
SRabbeliergitster
authored andcommitted
git-remote-testgit: fix error handling
If fast-export did not complete successfully the error handling code itself would error out. This was broken in commit 23b093e (Brandon Casey, Wed Jun 9 2010, Remove python 2.5'isms). Revert that commit an introduce our own copy of check_call in util.py instead. Tested by changing 'if retcode' to 'if not retcode' temporarily. Signed-off-by: Sverre Rabbelier <[email protected]> Acked-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0fb56ce commit 460d102

File tree

5 files changed

+62
-22
lines changed

5 files changed

+62
-22
lines changed

git_remote_helpers/git/exporter.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import subprocess
33
import sys
44

5+
from git_remote_helpers.util import check_call
6+
57

68
class GitExporter(object):
79
"""An exporter for testgit repositories.
@@ -53,6 +55,4 @@ def export_repo(self, base, refs=None):
5355

5456
args = ["sed", "s_refs/heads/_" + self.repo.prefix + "_g"]
5557

56-
child = subprocess.Popen(args, stdin=p1.stdout)
57-
if child.wait() != 0:
58-
raise CalledProcessError
58+
check_call(args, stdin=p1.stdout)

git_remote_helpers/git/importer.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import os
22
import subprocess
33

4+
from git_remote_helpers.util import check_call
5+
46

57
class GitImporter(object):
68
"""An importer for testgit repositories.
@@ -35,6 +37,4 @@ def do_import(self, base):
3537
if os.path.exists(path):
3638
args.append("--import-marks=" + path)
3739

38-
child = subprocess.Popen(args)
39-
if child.wait() != 0:
40-
raise CalledProcessError
40+
check_call(args)

git_remote_helpers/git/non_local.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import os
22
import subprocess
33

4-
from git_remote_helpers.util import die, warn
4+
from git_remote_helpers.util import check_call, die, warn
55

66

77
class NonLocalGit(object):
@@ -29,9 +29,7 @@ def clone(self, base):
2929
os.makedirs(path)
3030
args = ["git", "clone", "--bare", "--quiet", self.repo.gitpath, path]
3131

32-
child = subprocess.Popen(args)
33-
if child.wait() != 0:
34-
raise CalledProcessError
32+
check_call(args)
3533

3634
return path
3735

@@ -45,14 +43,10 @@ def update(self, base):
4543
die("could not find repo at %s", path)
4644

4745
args = ["git", "--git-dir=" + path, "fetch", "--quiet", self.repo.gitpath]
48-
child = subprocess.Popen(args)
49-
if child.wait() != 0:
50-
raise CalledProcessError
46+
check_call(args)
5147

5248
args = ["git", "--git-dir=" + path, "update-ref", "refs/heads/master", "FETCH_HEAD"]
53-
child = subprocess.Popen(args)
54-
if child.wait() != 0:
55-
raise CalledProcessError
49+
child = check_call(args)
5650

5751
def push(self, base):
5852
"""Pushes from the non-local repo to base.
@@ -64,6 +58,4 @@ def push(self, base):
6458
die("could not find repo at %s", path)
6559

6660
args = ["git", "--git-dir=" + path, "push", "--quiet", self.repo.gitpath, "--all"]
67-
child = subprocess.Popen(args)
68-
if child.wait() != 0:
69-
raise CalledProcessError
61+
child = check_call(args)

git_remote_helpers/git/repo.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import os
22
import subprocess
33

4+
from git_remote_helpers.util import check_call
5+
6+
47
def sanitize(rev, sep='\t'):
58
"""Converts a for-each-ref line to a name/value pair.
69
"""
@@ -53,9 +56,7 @@ def get_revs(self):
5356
path = ".cached_revs"
5457
ofile = open(path, "w")
5558

56-
child = subprocess.Popen(args, stdout=ofile)
57-
if child.wait() != 0:
58-
raise CalledProcessError
59+
check_call(args, stdout=ofile)
5960
output = open(path).readlines()
6061
self.revmap = dict(sanitize(i) for i in output)
6162
if "HEAD" in self.revmap:

git_remote_helpers/util.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,21 @@
1111
import os
1212
import subprocess
1313

14+
try:
15+
from subprocess import CalledProcessError
16+
except ImportError:
17+
# from python2.7:subprocess.py
18+
# Exception classes used by this module.
19+
class CalledProcessError(Exception):
20+
"""This exception is raised when a process run by check_call() returns
21+
a non-zero exit status. The exit status will be stored in the
22+
returncode attribute."""
23+
def __init__(self, returncode, cmd):
24+
self.returncode = returncode
25+
self.cmd = cmd
26+
def __str__(self):
27+
return "Command '%s' returned non-zero exit status %d" % (self.cmd, self.returncode)
28+
1429

1530
# Whether or not to show debug messages
1631
DEBUG = False
@@ -128,6 +143,38 @@ def run_command (args, cwd = None, shell = False, add_env = None,
128143
return (exit_code, output, errors)
129144

130145

146+
# from python2.7:subprocess.py
147+
def call(*popenargs, **kwargs):
148+
"""Run command with arguments. Wait for command to complete, then
149+
return the returncode attribute.
150+
151+
The arguments are the same as for the Popen constructor. Example:
152+
153+
retcode = call(["ls", "-l"])
154+
"""
155+
return subprocess.Popen(*popenargs, **kwargs).wait()
156+
157+
158+
# from python2.7:subprocess.py
159+
def check_call(*popenargs, **kwargs):
160+
"""Run command with arguments. Wait for command to complete. If
161+
the exit code was zero then return, otherwise raise
162+
CalledProcessError. The CalledProcessError object will have the
163+
return code in the returncode attribute.
164+
165+
The arguments are the same as for the Popen constructor. Example:
166+
167+
check_call(["ls", "-l"])
168+
"""
169+
retcode = call(*popenargs, **kwargs)
170+
if retcode:
171+
cmd = kwargs.get("args")
172+
if cmd is None:
173+
cmd = popenargs[0]
174+
raise CalledProcessError(retcode, cmd)
175+
return 0
176+
177+
131178
def file_reader_method (missing_ok = False):
132179
"""Decorator for simplifying reading of files.
133180

0 commit comments

Comments
 (0)