From 67260a382f3d4fb841fe4cb9c19cc6ca1ada26be Mon Sep 17 00:00:00 2001 From: Anson Mansfield Date: Wed, 19 Jul 2017 18:16:51 -0400 Subject: [PATCH 1/3] test if it accepts environment variables in commands --- git/test/fixtures/ls_tree_empty | 0 git/test/test_git.py | 15 +++++++++++++++ 2 files changed, 15 insertions(+) create mode 100644 git/test/fixtures/ls_tree_empty diff --git a/git/test/fixtures/ls_tree_empty b/git/test/fixtures/ls_tree_empty new file mode 100644 index 000000000..e69de29bb diff --git a/git/test/test_git.py b/git/test/test_git.py index 3c8b6f828..4c3afce91 100644 --- a/git/test/test_git.py +++ b/git/test/test_git.py @@ -105,6 +105,21 @@ def test_it_ignores_false_kwargs(self, git): self.git.version(pass_this_kwarg=False) assert_true("pass_this_kwarg" not in git.call_args[1]) + def test_it_accepts_environment_variables(self): + filename = fixture_path("ls_tree_empty") + with open(filename, 'r') as fh: + tree = self.git.mktree(istream=fh) + env = { + 'GIT_AUTHOR_NAME': 'Author Name', + 'GIT_AUTHOR_EMAIL': 'author@example.com', + 'GIT_AUTHOR_DATE': '1400000000+0000', + 'GIT_COMMITTER_NAME': 'Committer Name', + 'GIT_COMMITTER_EMAIL': 'committer@example.com', + 'GIT_COMMITTER_DATE': '1500000000+0000', + } + commit = self.git.commit_tree(tree, m='message', env=env) + assert_equal(commit, '4cfd6b0314682d5a58f80be39850bad1640e9241') + def test_persistent_cat_file_command(self): # read header only import subprocess as sp From 24d4926fd8479b8a298de84a2bcfdb94709ac619 Mon Sep 17 00:00:00 2001 From: Anson Mansfield Date: Wed, 19 Jul 2017 18:36:17 -0400 Subject: [PATCH 2/3] implemented per-call environment variable support --- git/cmd.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/git/cmd.py b/git/cmd.py index 3637ac9e4..6022180af 100644 --- a/git/cmd.py +++ b/git/cmd.py @@ -46,7 +46,7 @@ execute_kwargs = set(('istream', 'with_extended_output', 'with_exceptions', 'as_process', 'stdout_as_string', 'output_stream', 'with_stdout', 'kill_after_timeout', - 'universal_newlines', 'shell')) + 'universal_newlines', 'shell', 'env')) log = logging.getLogger(__name__) log.addHandler(logging.NullHandler()) @@ -471,6 +471,7 @@ def execute(self, command, with_stdout=True, universal_newlines=False, shell=None, + env=None, **subprocess_kwargs ): """Handles executing the command on the shell and consumes and returns @@ -514,6 +515,9 @@ def execute(self, command, decoded into a string using the default encoding (usually utf-8). The latter can fail, if the output contains binary data. + :param env: + A dictionary of environment variables to be passed to `subprocess.Popen`. + :param subprocess_kwargs: Keyword arguments to be passed to subprocess.Popen. Please note that some of the valid kwargs are already set by this method, the ones you @@ -559,6 +563,7 @@ def execute(self, command, cwd = self._working_dir or os.getcwd() # Start the process + inline_env = env env = os.environ.copy() # Attempt to force all output to plain ascii english, which is what some parsing code # may expect. @@ -567,6 +572,8 @@ def execute(self, command, env["LANGUAGE"] = "C" env["LC_ALL"] = "C" env.update(self._environment) + if inline_env is not None: + env.update(inline_env) if is_win: cmd_not_found_exception = OSError From 251128d41cdf39a49468ed5d997cc1640339ccbc Mon Sep 17 00:00:00 2001 From: Anson Mansfield Date: Wed, 19 Jul 2017 19:25:47 -0400 Subject: [PATCH 3/3] added myself --- AUTHORS | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS b/AUTHORS index ad7c452c0..a54f45d22 100644 --- a/AUTHORS +++ b/AUTHORS @@ -19,5 +19,6 @@ Contributors are: -Timothy B. Hartman -Konstantin Popov -Peter Jones +-Anson Mansfield Portions derived from other open source works and are clearly marked.