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 23b093ee0 (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 <srabbelier@gmail.com> Acked-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Sverre Rabbelier committed
Jul 16, 2011 at 15:03 UTC
460d10262dae14b54123ff45e7548d872ff63983
5 files changed
+62
-22
git_remote_helpers/git/exporter.py
+3
-3
@@ -2,6 +2,8 @@ import os
2
import subprocess
3
import sys
4
5
+from git_remote_helpers.util import check_call
6
+
7
8
class GitExporter(object):
9
"""An exporter for testgit repositories.
@@ -53,6 +55,4 @@ class GitExporter(object):
55
56
args = ["sed", "s_refs/heads/_" + self.repo.prefix + "_g"]
57
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
+3
-3
@@ -1,6 +1,8 @@
1
import os
2
import subprocess
3
4
+from git_remote_helpers.util import check_call
5
+
6
7
class GitImporter(object):
8
"""An importer for testgit repositories.
@@ -35,6 +37,4 @@ class GitImporter(object):
37
if os.path.exists(path):
38
args.append("--import-marks=" + path)
39
38
- child = subprocess.Popen(args)
39
- if child.wait() != 0:
40
- raise CalledProcessError
40
+ check_call(args)
git_remote_helpers/git/non_local.py
+5
-13
@@ -1,7 +1,7 @@
1
import os
2
import subprocess
3
4
-from git_remote_helpers.util import die, warn
4
+from git_remote_helpers.util import check_call, die, warn
5
6
7
class NonLocalGit(object):
@@ -29,9 +29,7 @@ class NonLocalGit(object):
29
os.makedirs(path)
30
args = ["git", "clone", "--bare", "--quiet", self.repo.gitpath, path]
31
32
- child = subprocess.Popen(args)
33
- if child.wait() != 0:
34
- raise CalledProcessError
32
+ check_call(args)
33
34
return path
35
@@ -45,14 +43,10 @@ class NonLocalGit(object):
43
die("could not find repo at %s", path)
44
45
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)
47
48
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)
50
51
def push(self, base):
52
"""Pushes from the non-local repo to base.
@@ -64,6 +58,4 @@ class NonLocalGit(object):
58
die("could not find repo at %s", path)
59
60
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
+4
-3
@@ -1,6 +1,9 @@
1
import os
2
import subprocess
3
4
+from git_remote_helpers.util import check_call
5
+
6
+
7
def sanitize(rev, sep='\t'):
8
"""Converts a for-each-ref line to a name/value pair.
9
"""
@@ -53,9 +56,7 @@ class GitRepo(object):
56
path = ".cached_revs"
57
ofile = open(path, "w")
58
56
- child = subprocess.Popen(args, stdout=ofile)
57
- if child.wait() != 0:
58
- raise CalledProcessError
59
+ check_call(args, stdout=ofile)
60
output = open(path).readlines()
61
self.revmap = dict(sanitize(i) for i in output)
62
if "HEAD" in self.revmap:
git_remote_helpers/util.py
+47
@@ -11,6 +11,21 @@ import sys
11
import os
12
import subprocess
13
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
+
29
30
# Whether or not to show debug messages
31
DEBUG = False
@@ -128,6 +143,38 @@ def run_command (args, cwd = None, shell = False, add_env = None,
143
return (exit_code, output, errors)
144
145
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
+
178
def file_reader_method (missing_ok = False):
179
"""Decorator for simplifying reading of files.
180