From: Sverre Rabbelier <srabbelier@gmail.com>
To: Junio C Hamano <gitster@pobox.com>,
Jonathan Nieder <jrnieder@gmail.com>, Jeff King <peff@peff.net>,
Git List <git@vger.kernel.org>,
Daniel Barkalow <barkalow@iabervon.org>,
Ramkumar
Cc: Sverre Rabbelier <srabbelier@gmail.com>
Subject: [PATCH v3 11/23] git-remote-testgit: fix error handling
Date: Sat, 16 Jul 2011 15:03:31 +0200 [thread overview]
Message-ID: <1310821424-4750-12-git-send-email-srabbelier@gmail.com> (raw)
In-Reply-To: <1310821424-4750-1-git-send-email-srabbelier@gmail.com>
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>
---
Included the definition of CalledProcessError if subprocess does
not already provide it to make sure everything still works on
python 2.4.
git_remote_helpers/git/exporter.py | 6 ++--
git_remote_helpers/git/importer.py | 6 ++--
git_remote_helpers/git/non_local.py | 18 +++---------
git_remote_helpers/git/repo.py | 7 +++--
git_remote_helpers/util.py | 47 +++++++++++++++++++++++++++++++++++
5 files changed, 62 insertions(+), 22 deletions(-)
diff --git a/git_remote_helpers/git/exporter.py b/git_remote_helpers/git/exporter.py
index bc39163..9ee5f96 100644
--- a/git_remote_helpers/git/exporter.py
+++ b/git_remote_helpers/git/exporter.py
@@ -2,6 +2,8 @@ import os
import subprocess
import sys
+from git_remote_helpers.util import check_call
+
class GitExporter(object):
"""An exporter for testgit repositories.
@@ -53,6 +55,4 @@ class GitExporter(object):
args = ["sed", "s_refs/heads/_" + self.repo.prefix + "_g"]
- child = subprocess.Popen(args, stdin=p1.stdout)
- if child.wait() != 0:
- raise CalledProcessError
+ check_call(args, stdin=p1.stdout)
diff --git a/git_remote_helpers/git/importer.py b/git_remote_helpers/git/importer.py
index 70a7127..02a719a 100644
--- a/git_remote_helpers/git/importer.py
+++ b/git_remote_helpers/git/importer.py
@@ -1,6 +1,8 @@
import os
import subprocess
+from git_remote_helpers.util import check_call
+
class GitImporter(object):
"""An importer for testgit repositories.
@@ -35,6 +37,4 @@ class GitImporter(object):
if os.path.exists(path):
args.append("--import-marks=" + path)
- child = subprocess.Popen(args)
- if child.wait() != 0:
- raise CalledProcessError
+ check_call(args)
diff --git a/git_remote_helpers/git/non_local.py b/git_remote_helpers/git/non_local.py
index c53e074..e700250 100644
--- a/git_remote_helpers/git/non_local.py
+++ b/git_remote_helpers/git/non_local.py
@@ -1,7 +1,7 @@
import os
import subprocess
-from git_remote_helpers.util import die, warn
+from git_remote_helpers.util import check_call, die, warn
class NonLocalGit(object):
@@ -29,9 +29,7 @@ class NonLocalGit(object):
os.makedirs(path)
args = ["git", "clone", "--bare", "--quiet", self.repo.gitpath, path]
- child = subprocess.Popen(args)
- if child.wait() != 0:
- raise CalledProcessError
+ check_call(args)
return path
@@ -45,14 +43,10 @@ class NonLocalGit(object):
die("could not find repo at %s", path)
args = ["git", "--git-dir=" + path, "fetch", "--quiet", self.repo.gitpath]
- child = subprocess.Popen(args)
- if child.wait() != 0:
- raise CalledProcessError
+ check_call(args)
args = ["git", "--git-dir=" + path, "update-ref", "refs/heads/master", "FETCH_HEAD"]
- child = subprocess.Popen(args)
- if child.wait() != 0:
- raise CalledProcessError
+ child = check_call(args)
def push(self, base):
"""Pushes from the non-local repo to base.
@@ -64,6 +58,4 @@ class NonLocalGit(object):
die("could not find repo at %s", path)
args = ["git", "--git-dir=" + path, "push", "--quiet", self.repo.gitpath, "--all"]
- child = subprocess.Popen(args)
- if child.wait() != 0:
- raise CalledProcessError
+ child = check_call(args)
diff --git a/git_remote_helpers/git/repo.py b/git_remote_helpers/git/repo.py
index 58e1cdb..acbf8d7 100644
--- a/git_remote_helpers/git/repo.py
+++ b/git_remote_helpers/git/repo.py
@@ -1,6 +1,9 @@
import os
import subprocess
+from git_remote_helpers.util import check_call
+
+
def sanitize(rev, sep='\t'):
"""Converts a for-each-ref line to a name/value pair.
"""
@@ -53,9 +56,7 @@ class GitRepo(object):
path = ".cached_revs"
ofile = open(path, "w")
- child = subprocess.Popen(args, stdout=ofile)
- if child.wait() != 0:
- raise CalledProcessError
+ check_call(args, stdout=ofile)
output = open(path).readlines()
self.revmap = dict(sanitize(i) for i in output)
if "HEAD" in self.revmap:
diff --git a/git_remote_helpers/util.py b/git_remote_helpers/util.py
index dce83e6..1652c65 100644
--- a/git_remote_helpers/util.py
+++ b/git_remote_helpers/util.py
@@ -11,6 +11,21 @@ import sys
import os
import subprocess
+try:
+ from subprocess import CalledProcessError
+except ImportError:
+ # from python2.7:subprocess.py
+ # Exception classes used by this module.
+ class CalledProcessError(Exception):
+ """This exception is raised when a process run by check_call() returns
+ a non-zero exit status. The exit status will be stored in the
+ returncode attribute."""
+ def __init__(self, returncode, cmd):
+ self.returncode = returncode
+ self.cmd = cmd
+ def __str__(self):
+ return "Command '%s' returned non-zero exit status %d" % (self.cmd, self.returncode)
+
# Whether or not to show debug messages
DEBUG = False
@@ -128,6 +143,38 @@ def run_command (args, cwd = None, shell = False, add_env = None,
return (exit_code, output, errors)
+# from python2.7:subprocess.py
+def call(*popenargs, **kwargs):
+ """Run command with arguments. Wait for command to complete, then
+ return the returncode attribute.
+
+ The arguments are the same as for the Popen constructor. Example:
+
+ retcode = call(["ls", "-l"])
+ """
+ return subprocess.Popen(*popenargs, **kwargs).wait()
+
+
+# from python2.7:subprocess.py
+def check_call(*popenargs, **kwargs):
+ """Run command with arguments. Wait for command to complete. If
+ the exit code was zero then return, otherwise raise
+ CalledProcessError. The CalledProcessError object will have the
+ return code in the returncode attribute.
+
+ The arguments are the same as for the Popen constructor. Example:
+
+ check_call(["ls", "-l"])
+ """
+ retcode = call(*popenargs, **kwargs)
+ if retcode:
+ cmd = kwargs.get("args")
+ if cmd is None:
+ cmd = popenargs[0]
+ raise CalledProcessError(retcode, cmd)
+ return 0
+
+
def file_reader_method (missing_ok = False):
"""Decorator for simplifying reading of files.
--
1.7.5.1.292.g728120
next prev parent reply other threads:[~2011-07-16 13:04 UTC|newest]
Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-07-16 13:03 [PATCH v3 00/23] remote-helper improvements Sverre Rabbelier
2011-07-16 13:03 ` [PATCH v3 01/23] transport-helper: fix minor leak in push_refs_with_export Sverre Rabbelier
2011-07-16 13:03 ` [PATCH v3 02/23] t5800: factor out some ref tests Sverre Rabbelier
2011-07-16 13:03 ` [PATCH v3 03/23] t5800: use skip_all instead of prereq Sverre Rabbelier
2011-07-16 13:03 ` [PATCH v3 04/23] t5800: document some non-functional parts of remote helpers Sverre Rabbelier
2011-07-16 13:03 ` [PATCH v3 05/23] git-remote-testgit: import non-HEAD refs Sverre Rabbelier
2011-07-16 13:03 ` [PATCH v3 06/23] transport-helper: don't feed bogus refs to export push Sverre Rabbelier
2011-07-16 13:03 ` [PATCH v3 07/23] git_remote_helpers: push all refs during a non-local export Sverre Rabbelier
2011-07-17 23:36 ` Junio C Hamano
2011-07-23 11:27 ` Sverre Rabbelier
2011-07-16 13:03 ` [PATCH v3 08/23] remote-helpers: export GIT_DIR variable to helpers Sverre Rabbelier
2011-07-16 13:03 ` [PATCH v3 09/23] remote-curl: accept empty line as terminator Sverre Rabbelier
2011-07-16 13:03 ` [PATCH v3 10/23] git-remote-testgit: only push for non-local repositories Sverre Rabbelier
2011-07-16 13:03 ` Sverre Rabbelier [this message]
2011-07-16 13:03 ` [PATCH v3 12/23] fast-import: introduce 'done' command Sverre Rabbelier
2011-07-16 13:03 ` [PATCH v3 13/23] fast-export: support done feature Sverre Rabbelier
2011-07-16 13:03 ` [PATCH v3 14/23] transport-helper: factor out push_update_refs_status Sverre Rabbelier
2011-07-16 13:03 ` [PATCH v3 15/23] transport-helper: check status code of finish_command Sverre Rabbelier
2011-07-16 13:03 ` [PATCH v3 16/23] transport-helper: use the new done feature where possible Sverre Rabbelier
2011-07-16 13:03 ` [PATCH v3 17/23] transport-helper: update ref status after push with export Sverre Rabbelier
2011-07-16 13:03 ` [PATCH v3 18/23] transport-helper: change import semantics Sverre Rabbelier
2011-07-18 11:13 ` Dmitry Ivankov
2011-07-16 13:03 ` [PATCH v3 19/23] transport-helper: Use capname for refspec capability too Sverre Rabbelier
2011-07-16 13:03 ` [PATCH v3 20/23] transport-helper: implement marks location as capability Sverre Rabbelier
2011-07-16 13:03 ` [PATCH v3 21/23] transport-helper: die early on encountering deleted refs Sverre Rabbelier
2011-07-16 13:03 ` [RFD/PATCH v3 22/23] t5800: document inability to push new branch with old content Sverre Rabbelier
2011-07-16 13:03 ` [RDD/PATCH v3 23/23] t5800: point out that deleting branches does not work Sverre Rabbelier
2011-07-18 3:28 ` [PATCH v3 00/23] remote-helper improvements Jeff King
2011-07-23 11:28 ` Sverre Rabbelier
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1310821424-4750-12-git-send-email-srabbelier@gmail.com \
--to=srabbelier@gmail.com \
--cc=barkalow@iabervon.org \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=jrnieder@gmail.com \
--cc=peff@peff.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).