summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian Thiel <byronimo@gmail.com>2015-01-22 16:22:15 +0100
committerSebastian Thiel <byronimo@gmail.com>2015-01-22 16:22:21 +0100
commit2ce3fe7cef8910aadc2a2b39a3dab4242a751380 (patch)
treef7d25f8897aac64b434ad4282a917ed32ae34952
parent6f038611ff120f8283f0f1a56962f95b66730c72 (diff)
downloadgitpython-2ce3fe7cef8910aadc2a2b39a3dab4242a751380.tar.gz
Intermediate commit on my way to get this finalized.
Renamed context manager 'with_environment' to 'custom_environment'. On my way to implement sshkey test.
-rw-r--r--doc/source/tutorial.rst2
-rw-r--r--git/cmd.py25
-rwxr-xr-xgit/scripts/ssh_wrapper.sh2
-rw-r--r--git/test/test_git.py40
-rwxr-xr-xscripts/ssh_wrapper.py9
5 files changed, 45 insertions, 33 deletions
diff --git a/doc/source/tutorial.rst b/doc/source/tutorial.rst
index 6ca0fd87..33a0a884 100644
--- a/doc/source/tutorial.rst
+++ b/doc/source/tutorial.rst
@@ -384,7 +384,7 @@ Change configuration for a specific remote only::
o.config_writer.set("pushurl", "other_url")
-You can also specify an SSH key to use for any operations on the remotes:
+You can also specify an SSH key to use for any operations on the remotes::
private_key_file = project_dir+'id_rsa_deployment_key'
with repo.git.sshkey(private_key_file):
diff --git a/git/cmd.py b/git/cmd.py
index 91442470..a0319073 100644
--- a/git/cmd.py
+++ b/git/cmd.py
@@ -439,6 +439,10 @@ class Git(LazyMixin):
super(Git, self)._set_cache_(attr)
# END handle version info
+ def _sshkey_script_path(self):
+ this_dir = os.path.dirname(__file__)
+ return os.path.join(this_dir, 'scripts', 'ssh_wrapper.sh')
+
@property
def working_dir(self):
""":return: Git directory we are working on"""
@@ -541,6 +545,7 @@ class Git(LazyMixin):
# Start the process
env = os.environ.copy()
env["LC_MESSAGES"] = "C"
+ print(self._environment)
env.update(self._environment)
proc = Popen(command,
@@ -633,7 +638,7 @@ class Git(LazyMixin):
:return: dict that maps environment variables to their old values
"""
old_env = {}
- for key, value in kwargs.iteritems():
+ for key, value in kwargs.items():
# set value if it is None
if value is not None:
if key in self._environment:
@@ -648,14 +653,14 @@ class Git(LazyMixin):
return old_env
@contextmanager
- def with_environment(self, **kwargs):
+ def custom_environment(self, **kwargs):
"""
- A context manager around the above update_environment to restore the
+ A context manager around the above ``update_environment`` method to restore the
environment back to its previous state after operation.
``Examples``::
- with self.with_environment(GIT_SSH='/bin/ssh_wrapper'):
+ with self.custom_environment(GIT_SSH='/bin/ssh_wrapper'):
repo.remotes.origin.fetch()
:param kwargs: see update_environment
@@ -667,22 +672,20 @@ class Git(LazyMixin):
self.update_environment(**old_env)
@contextmanager
- def sshkey(self, sshkey_file):
+ def sshkey(self, sshkey_file_path):
"""
A context manager to temporarily set an SSH key for all operations that
run inside it.
``Examples``::
- with self.environment(GIT_SSH=project_dir+'deployment_key'):
+ with self.sshkey('deployment_key'):
repo.remotes.origin.fetch()
- :param sshkey_file: Path to a private SSH key file
+ :param sshkey_file_path: Path to a private SSH key file
"""
- this_dir = os.path.dirname(__file__)
- ssh_wrapper = os.path.join(this_dir, '..', 'scripts', 'ssh_wrapper.py')
-
- with self.with_environment(GIT_SSH_KEY_FILE=sshkey_file, GIT_SSH=ssh_wrapper):
+ ssh_wrapper = self._sshkey_script_path()
+ with self.custom_environment(GIT_SSH_KEY_FILE=sshkey_file_path, GIT_SSH=ssh_wrapper):
yield
def transform_kwargs(self, split_single_char_options=False, **kwargs):
diff --git a/git/scripts/ssh_wrapper.sh b/git/scripts/ssh_wrapper.sh
new file mode 100755
index 00000000..bc0ab024
--- /dev/null
+++ b/git/scripts/ssh_wrapper.sh
@@ -0,0 +1,2 @@
+#!/usr/bin/env sh
+ssh -i "$GIT_SSH_KEY_FILE" $@
diff --git a/git/test/test_git.py b/git/test/test_git.py
index 85ee56c6..990f4cd0 100644
--- a/git/test/test_git.py
+++ b/git/test/test_git.py
@@ -4,18 +4,24 @@
#
# This module is part of GitPython and is released under
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
-
import os
import mock
-from git.test.lib import (TestBase,
- patch,
- raises,
- assert_equal,
- assert_true,
- assert_match,
- fixture_path)
-from git import (Git,
- GitCommandError)
+
+from git.test.lib import (
+ TestBase,
+ patch,
+ raises,
+ assert_equal,
+ assert_true,
+ assert_match,
+ fixture_path
+)
+from git import (
+ Git,
+ GitCommandError,
+ Repo
+)
+from gitdb.test.lib import with_rw_directory
from git.compat import PY3
@@ -154,12 +160,13 @@ class TestGit(TestBase):
with mock.patch.dict('os.environ', {'GIT_EDITOR': editor}):
assert self.git.var("GIT_EDITOR") == editor
- def test_environment(self):
+ @with_rw_directory
+ def test_environment(self, rw_dir):
# sanity check
assert self.git.environment() == {}
# make sure the context manager works and cleans up after itself
- with self.git.with_environment(PWD='/tmp'):
+ with self.git.custom_environment(PWD='/tmp'):
assert self.git.environment() == {'PWD': '/tmp'}
assert self.git.environment() == {}
@@ -173,3 +180,12 @@ class TestGit(TestBase):
new_env = self.git.update_environment(**old_env)
assert new_env == {'VARKEY': 'VARVALUE'}
assert self.git.environment() == {}
+
+ rw_repo = Repo.init(os.path.join(rw_dir, 'repo'))
+ remote = rw_repo.create_remote('ssh-origin', "ssh://git@server/foo")
+
+ with rw_repo.git.sshkey('doesntexist.key'):
+ remote.fetch()
+ # end
+
+
diff --git a/scripts/ssh_wrapper.py b/scripts/ssh_wrapper.py
deleted file mode 100755
index af657f60..00000000
--- a/scripts/ssh_wrapper.py
+++ /dev/null
@@ -1,9 +0,0 @@
-#!/usr/bin/env python
-
-import os
-import subprocess
-import sys
-
-ssh_options = ['-i', os.environ['GIT_SSH_KEY_FILE']]
-ret_code = subprocess.call(['ssh'] + ssh_options + sys.argv[1:])
-sys.exit(ret_code)