summaryrefslogtreecommitdiff
path: root/git
diff options
context:
space:
mode:
Diffstat (limited to 'git')
-rw-r--r--git/cmd.py25
-rwxr-xr-xgit/scripts/ssh_wrapper.sh2
-rw-r--r--git/test/test_git.py40
3 files changed, 44 insertions, 23 deletions
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
+
+