diff options
author | Michael Trier <mtrier@gmail.com> | 2008-05-26 10:51:07 -0400 |
---|---|---|
committer | Michael Trier <mtrier@gmail.com> | 2008-05-26 10:51:07 -0400 |
commit | 27ceafbb3f74b4677d5bae6c7ea031de07c6cfad (patch) | |
tree | 1a5cf74ebc29d6b3ddc6c686133a56389cad88d2 | |
parent | 7c60b88138b836307bdde0487c4ffb82121b1c5e (diff) | |
parent | 70094734d601e1220c95ac586fdffbda3a23e10b (diff) | |
download | gitpython-27ceafbb3f74b4677d5bae6c7ea031de07c6cfad.tar.gz |
Merge git://gitorious.org/git-python/apollo13
-rw-r--r-- | lib/git_python/__init__.py | 2 | ||||
-rw-r--r-- | lib/git_python/git.py | 43 | ||||
-rw-r--r-- | lib/git_python/utils.py | 3 | ||||
-rw-r--r-- | test/git/test_git.py | 26 | ||||
-rw-r--r-- | test/git/test_repo.py | 18 | ||||
-rw-r--r-- | test/git/test_utils.py | 6 |
6 files changed, 39 insertions, 59 deletions
diff --git a/lib/git_python/__init__.py b/lib/git_python/__init__.py index 71142a40..11f93313 100644 --- a/lib/git_python/__init__.py +++ b/lib/git_python/__init__.py @@ -11,7 +11,7 @@ from git_python.repo import Repo from git_python.stats import Stats from git_python.tag import Tag from git_python.tree import Tree -from git_python.utils import shell_escape, dashify, touch +from git_python.utils import dashify, touch __all__ = [ name for name, obj in locals().items() if not (name.startswith('_') or inspect.ismodule(obj)) ] diff --git a/lib/git_python/git.py b/lib/git_python/git.py index ee256356..aad1f85d 100644 --- a/lib/git_python/git.py +++ b/lib/git_python/git.py @@ -11,27 +11,27 @@ class Git(MethodMissingMixin): def __init__(self, git_dir): super(Git, self).__init__() self.git_dir = git_dir - - git_binary = "/usr/bin/env git" - + @property def get_dir(self): return self.git_dir - + def execute(self, command): """ Handles executing the command on the shell and consumes and returns the returned information (stdout) - + ``command`` The command to execute """ - print command - proc = subprocess.Popen(command, - shell=True, - stdout=subprocess.PIPE + print ' '.join(command) + proc = subprocess.Popen(command, + cwd = self.git_dir, + stdout=subprocess.PIPE ) - stdout_value = proc.communicate()[0] + proc.wait() + stdout_value = proc.stdout.read() + proc.stdout.close() return stdout_value def transform_kwargs(self, **kwargs): @@ -44,38 +44,41 @@ class Git(MethodMissingMixin): if v is True: args.append("-%s" % k) else: - args.append("-%s %r" % (k, v)) + args.append("-%s" % k) + args.append(v) else: if v is True: args.append("--%s" % dashify(k)) else: - args.append("--%s=%r" % (dashify(k), v)) + args.append("--%s=%s" % (dashify(k), v)) return args - + def method_missing(self, method, *args, **kwargs): """ Run the given git command with the specified arguments and return the result as a String - + ``method`` is the command - + ``args`` is the list of arguments - + ``kwargs`` is a dict of keyword arguments Examples git.rev_list('master', max_count=10, header=True) - + Returns str """ opt_args = self.transform_kwargs(**kwargs) - ext_args = map(lambda a: (a == '--') and a or "%s" % shell_escape(a), args) + ext_args = map(lambda a: (a == '--') and a or "%s" % a, args) args = opt_args + ext_args - - call = "%s --git-dir=%s %s %s" % (self.git_binary, self.git_dir, dashify(method), ' '.join(args)) + + call = ['git-'+dashify(method)] + call.extend(args) + stdout_value = self.execute(call) return stdout_value diff --git a/lib/git_python/utils.py b/lib/git_python/utils.py index 2bd6f2cd..c2140ba0 100644 --- a/lib/git_python/utils.py +++ b/lib/git_python/utils.py @@ -1,6 +1,3 @@ -def shell_escape(string): - return str(string).replace("'", "\\\\'") - def dashify(string): return string.replace('_', '-') diff --git a/test/git/test_git.py b/test/git/test_git.py index 0634a5dd..43fd231c 100644 --- a/test/git/test_git.py +++ b/test/git/test_git.py @@ -4,9 +4,8 @@ from git_python import * class TestGit(object): def setup(self): - base = os.path.join(os.path.dirname(__file__), "../.."), + base = os.path.join(os.path.dirname(__file__), "../..") self.git = Git(base) - self.git_bin_base = "%s --git-dir=%s" % (Git.git_binary, base) @patch(Git, 'execute') def test_method_missing_calls_execute(self, git): @@ -17,7 +16,7 @@ class TestGit(object): def test_it_transforms_kwargs_into_git_command_arguments(self): assert_equal(["-s"], self.git.transform_kwargs(**{'s': True})) - assert_equal(["-s 5"], self.git.transform_kwargs(**{'s': 5})) + assert_equal(["-s", 5], self.git.transform_kwargs(**{'s': 5})) assert_equal(["--max-count"], self.git.transform_kwargs(**{'max_count': True})) assert_equal(["--max-count=5"], self.git.transform_kwargs(**{'max_count': 5})) @@ -25,23 +24,4 @@ class TestGit(object): assert_equal(["-s", "-t"], self.git.transform_kwargs(**{'s': True, 't': True})) def test_it_executes_git_to_shell_and_returns_result(self): - assert_match('^git version [\d\.]*$', self.git.execute("%s version" % Git.git_binary)) - - def test_it_transforms_kwargs_shell_escapes_arguments(self): - assert_equal(["--foo=\"bazz'er\""], self.git.transform_kwargs(**{'foo': "bazz'er"})) - assert_equal(["-x \"bazz'er\""], self.git.transform_kwargs(**{'x': "bazz'er"})) - - @patch(Git, 'execute') - def test_it_really_shell_escapes_arguments_to_the_git_shell_1(self, git): - self.git.foo(**{'bar': "bazz'er"}) - assert_true(git.called) - assert_equal(git.call_args, ((("%s foo --bar=\"bazz'er\"" % self.git_bin_base),), {})) - - @patch(Git, 'execute') - def test_it_really_shell_escapes_arguments_to_the_git_shell_2(self, git): - self.git.bar(**{'x': "quu'x"}) - assert_true(git.called) - assert_equal(git.call_args, ((("%s bar -x \"quu'x\"" % self.git_bin_base),), {})) - - def test_it_shell_escapes_the_standalone_argument(self): - self.git.foo("bar's", {}) + assert_match('^git version [\d\.]*$', self.git.execute(["git","version"])) diff --git a/test/git/test_repo.py b/test/git/test_repo.py index 52f5856d..ff267956 100644 --- a/test/git/test_repo.py +++ b/test/git/test_repo.py @@ -13,7 +13,7 @@ class TestRepo(object): @raises(NoSuchPathError) def test_new_should_raise_on_non_existant_path(self): - Repo("/foobar") + Repo("~/foobar") def test_description(self): assert_equal("Unnamed repository; edit this file to name it for gitweb.", self.repo.description) @@ -108,34 +108,34 @@ class TestRepo(object): def test_init_bare(self, repo, git): git.return_value = True - Repo.init_bare("/foo/bar.git") + Repo.init_bare("~/foo/bar.git") assert_true(git.called) assert_equal(git.call_args, (('init',), {})) assert_true(repo.called) - assert_equal(repo.call_args, (('/foo/bar.git',), {})) + assert_equal(repo.call_args, (('~/foo/bar.git',), {})) @patch(Repo, '__init__') @patch(Git, 'method_missing') def test_init_bare_with_options(self, repo, git): git.return_value = True - Repo.init_bare("/foo/bar.git", **{'template': "/baz/sweet"}) + Repo.init_bare("~/foo/bar.git", **{'template': "/baz/sweet"}) assert_true(git.called) assert_equal(git.call_args, (('init',), {'template': '/baz/sweet'})) assert_true(repo.called) - assert_equal(repo.call_args, (('/foo/bar.git',), {})) + assert_equal(repo.call_args, (('~/foo/bar.git',), {})) @patch(Repo, '__init__') @patch(Git, 'method_missing') def test_fork_bare(self, repo, git): git.return_value = None - self.repo.fork_bare("/foo/bar.git") + self.repo.fork_bare("~/foo/bar.git") assert_true(git.called) - assert_equal(git.call_args, (('clone', '%s/.git' % absolute_project_path(), '/foo/bar.git'), {'bare': True})) + assert_equal(git.call_args, (('clone', '%s/.git' % absolute_project_path(), '~/foo/bar.git'), {'bare': True})) assert_true(repo.called) @patch(Repo, '__init__') @@ -143,10 +143,10 @@ class TestRepo(object): def test_fork_bare_with_options(self, repo, git): git.return_value = None - self.repo.fork_bare("/foo/bar.git", **{'template': '/awesome'}) + self.repo.fork_bare("~/foo/bar.git", **{'template': '/awesome'}) assert_true(git.called) - assert_equal(git.call_args, (('clone', '%s/.git' % absolute_project_path(), '/foo/bar.git'), + assert_equal(git.call_args, (('clone', '%s/.git' % absolute_project_path(), '~/foo/bar.git'), {'bare': True, 'template': '/awesome'})) assert_true(repo.called) diff --git a/test/git/test_utils.py b/test/git/test_utils.py index a9af2040..b2b42a1c 100644 --- a/test/git/test_utils.py +++ b/test/git/test_utils.py @@ -6,10 +6,10 @@ class TestUtils(object): def setup(self): base = os.path.join(os.path.dirname(__file__), "../.."), self.git = Git(base) - self.git_bin_base = "%s --git-dir='%s'" % (Git.git_binary, base) +# self.git_bin_base = "%s --git-dir='%s'" % (Git.git_binary, base) - def test_it_escapes_single_quotes_with_shell_escape(self): - assert_equal("\\\\'foo", shell_escape("'foo")) +# def test_it_escapes_single_quotes_with_shell_escape(self): +# assert_equal("\\\\'foo", shell_escape("'foo")) def test_it_should_dashify(self): assert_equal('this-is-my-argument', dashify('this_is_my_argument')) |