| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -384,7 +384,7 @@ Change configuration for a specific remote only:: | |||
| 384 | 384 | ||
| 385 | 385 | o.config_writer.set("pushurl", "other_url") | |
| 386 | 386 | ||
| 387 | - You can also specify an SSH key to use for any operations on the remotes: | ||
| 387 | + You can also specify an SSH key to use for any operations on the remotes:: | ||
| 388 | 388 | ||
| 389 | 389 | private_key_file = project_dir+'id_rsa_deployment_key' | |
| 390 | 390 | with repo.git.sshkey(private_key_file): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -439,6 +439,10 @@ def _set_cache_(self, attr): | |||
| 439 | 439 | super(Git, self)._set_cache_(attr) | |
| 440 | 440 | # END handle version info | |
| 441 | 441 | ||
| 442 | + def _sshkey_script_path(self): | ||
| 443 | + this_dir = os.path.dirname(__file__) | ||
| 444 | + return os.path.join(this_dir, 'scripts', 'ssh_wrapper.sh') | ||
| 445 | + | ||
| 442 | 446 | @property | |
| 443 | 447 | def working_dir(self): | |
| 444 | 448 | """:return: Git directory we are working on""" | |
@@ -541,6 +545,7 @@ def execute(self, command, | |||
| 541 | 545 | # Start the process | |
| 542 | 546 | env = os.environ.copy() | |
| 543 | 547 | env["LC_MESSAGES"] = "C" | |
| 548 | + print(self._environment) | ||
| 544 | 549 | env.update(self._environment) | |
| 545 | 550 | ||
| 546 | 551 | proc = Popen(command, | |
@@ -633,7 +638,7 @@ def update_environment(self, **kwargs): | |||
| 633 | 638 | :return: dict that maps environment variables to their old values | |
| 634 | 639 | """ | |
| 635 | 640 | old_env = {} | |
| 636 | - for key, value in kwargs.iteritems(): | ||
| 641 | + for key, value in kwargs.items(): | ||
| 637 | 642 | # set value if it is None | |
| 638 | 643 | if value is not None: | |
| 639 | 644 | if key in self._environment: | |
@@ -648,14 +653,14 @@ def update_environment(self, **kwargs): | |||
| 648 | 653 | return old_env | |
| 649 | 654 | ||
| 650 | 655 | @contextmanager | |
| 651 | - def with_environment(self, **kwargs): | ||
| 656 | + def custom_environment(self, **kwargs): | ||
| 652 | 657 | """ | |
| 653 | - A context manager around the above update_environment to restore the | ||
| 658 | + A context manager around the above ``update_environment`` method to restore the | ||
| 654 | 659 | environment back to its previous state after operation. | |
| 655 | 660 | ||
| 656 | 661 | ``Examples``:: | |
| 657 | 662 | ||
| 658 | - with self.with_environment(GIT_SSH='/bin/ssh_wrapper'): | ||
| 663 | + with self.custom_environment(GIT_SSH='/bin/ssh_wrapper'): | ||
| 659 | 664 | repo.remotes.origin.fetch() | |
| 660 | 665 | ||
| 661 | 666 | :param kwargs: see update_environment | |
@@ -667,22 +672,20 @@ def with_environment(self, **kwargs): | |||
| 667 | 672 | self.update_environment(**old_env) | |
| 668 | 673 | ||
| 669 | 674 | @contextmanager | |
| 670 | - def sshkey(self, sshkey_file): | ||
| 675 | + def sshkey(self, sshkey_file_path): | ||
| 671 | 676 | """ | |
| 672 | 677 | A context manager to temporarily set an SSH key for all operations that | |
| 673 | 678 | run inside it. | |
| 674 | 679 | ||
| 675 | 680 | ``Examples``:: | |
| 676 | 681 | ||
| 677 | - with self.environment(GIT_SSH=project_dir+'deployment_key'): | ||
| 682 | + with self.sshkey('deployment_key'): | ||
| 678 | 683 | repo.remotes.origin.fetch() | |
| 679 | 684 | ||
| 680 | - :param sshkey_file: Path to a private SSH key file | ||
| 685 | + :param sshkey_file_path: Path to a private SSH key file | ||
| 681 | 686 | """ | |
| 682 | - this_dir = os.path.dirname(__file__) | ||
| 683 | - ssh_wrapper = os.path.join(this_dir, '..', 'scripts', 'ssh_wrapper.py') | ||
| 684 | - | ||
| 685 | - with self.with_environment(GIT_SSH_KEY_FILE=sshkey_file, GIT_SSH=ssh_wrapper): | ||
| 687 | + ssh_wrapper = self._sshkey_script_path() | ||
| 688 | + with self.custom_environment(GIT_SSH_KEY_FILE=sshkey_file_path, GIT_SSH=ssh_wrapper): | ||
| 686 | 689 | yield | |
| 687 | 690 | ||
| 688 | 691 | def transform_kwargs(self, split_single_char_options=False, **kwargs): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,2 @@ | |||
| 1 | + #!/usr/bin/env sh | ||
| 2 | + ssh -i "$GIT_SSH_KEY_FILE" $@ | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,18 +4,24 @@ | |||
| 4 | 4 | # | |
| 5 | 5 | # This module is part of GitPython and is released under | |
| 6 | 6 | # the BSD License: http://www.opensource.org/licenses/bsd-license.php | |
| 7 | - | ||
| 8 | 7 | import os | |
| 9 | 8 | import mock | |
| 10 | - from git.test.lib import (TestBase, | ||
| 11 | - patch, | ||
| 12 | - raises, | ||
| 13 | - assert_equal, | ||
| 14 | - assert_true, | ||
| 15 | - assert_match, | ||
| 16 | - fixture_path) | ||
| 17 | - from git import (Git, | ||
| 18 | - GitCommandError) | ||
| 9 | + | ||
| 10 | + from git.test.lib import ( | ||
| 11 | + TestBase, | ||
| 12 | + patch, | ||
| 13 | + raises, | ||
| 14 | + assert_equal, | ||
| 15 | + assert_true, | ||
| 16 | + assert_match, | ||
| 17 | + fixture_path | ||
| 18 | + ) | ||
| 19 | + from git import ( | ||
| 20 | + Git, | ||
| 21 | + GitCommandError, | ||
| 22 | + Repo | ||
| 23 | + ) | ||
| 24 | + from gitdb.test.lib import with_rw_directory | ||
| 19 | 25 | ||
| 20 | 26 | from git.compat import PY3 | |
| 21 | 27 | ||
@@ -154,12 +160,13 @@ def test_env_vars_passed_to_git(self): | |||
| 154 | 160 | with mock.patch.dict('os.environ', {'GIT_EDITOR': editor}): | |
| 155 | 161 | assert self.git.var("GIT_EDITOR") == editor | |
| 156 | 162 | ||
| 157 | - def test_environment(self): | ||
| 163 | + @with_rw_directory | ||
| 164 | + def test_environment(self, rw_dir): | ||
| 158 | 165 | # sanity check | |
| 159 | 166 | assert self.git.environment() == {} | |
| 160 | 167 | ||
| 161 | 168 | # make sure the context manager works and cleans up after itself | |
| 162 | - with self.git.with_environment(PWD='/tmp'): | ||
| 169 | + with self.git.custom_environment(PWD='/tmp'): | ||
| 163 | 170 | assert self.git.environment() == {'PWD': '/tmp'} | |
| 164 | 171 | ||
| 165 | 172 | assert self.git.environment() == {} | |
@@ -173,3 +180,12 @@ def test_environment(self): | |||
| 173 | 180 | new_env = self.git.update_environment(**old_env) | |
| 174 | 181 | assert new_env == {'VARKEY': 'VARVALUE'} | |
| 175 | 182 | assert self.git.environment() == {} | |
| 183 | + | ||
| 184 | + rw_repo = Repo.init(os.path.join(rw_dir, 'repo')) | ||
| 185 | + remote = rw_repo.create_remote('ssh-origin', "ssh://git@server/foo") | ||
| 186 | + | ||
| 187 | + with rw_repo.git.sshkey('doesntexist.key'): | ||
| 188 | + remote.fetch() | ||
| 189 | + # end | ||
| 190 | + | ||
| 191 | + | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments