Skip to content

Commit ddb828e

Browse files
authored
Merge pull request gitpython-developers#645 from AJMansfield/master
Implemented Per-Call Environment Variables
2 parents 2eb6cf0 + 2e482a2 commit ddb828e

File tree

4 files changed

+24
-1
lines changed

4 files changed

+24
-1
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Contributors are:
1919
-Timothy B. Hartman <tbhartman _at_ gmail.com>
2020
-Konstantin Popov <konstantin.popov.89 _at_ yandex.ru>
2121
-Peter Jones <pjones _at_ redhat.com>
22+
-Anson Mansfield <anson.mansfield _at_ gmail.com>
2223
-Ken Odegard <ken.odegard _at_ gmail.com>
2324
-Alexis Horgix Chotard
2425

git/cmd.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
execute_kwargs = set(('istream', 'with_extended_output',
4848
'with_exceptions', 'as_process', 'stdout_as_string',
4949
'output_stream', 'with_stdout', 'kill_after_timeout',
50-
'universal_newlines', 'shell'))
50+
'universal_newlines', 'shell', 'env'))
5151

5252
log = logging.getLogger(__name__)
5353
log.addHandler(logging.NullHandler())
@@ -597,6 +597,7 @@ def execute(self, command,
597597
with_stdout=True,
598598
universal_newlines=False,
599599
shell=None,
600+
env=None,
600601
**subprocess_kwargs
601602
):
602603
"""Handles executing the command on the shell and consumes and returns
@@ -640,6 +641,9 @@ def execute(self, command,
640641
decoded into a string using the default encoding (usually utf-8).
641642
The latter can fail, if the output contains binary data.
642643
644+
:param env:
645+
A dictionary of environment variables to be passed to `subprocess.Popen`.
646+
643647
:param subprocess_kwargs:
644648
Keyword arguments to be passed to subprocess.Popen. Please note that
645649
some of the valid kwargs are already set by this method, the ones you
@@ -685,6 +689,7 @@ def execute(self, command,
685689
cwd = self._working_dir or os.getcwd()
686690

687691
# Start the process
692+
inline_env = env
688693
env = os.environ.copy()
689694
# Attempt to force all output to plain ascii english, which is what some parsing code
690695
# may expect.
@@ -693,6 +698,8 @@ def execute(self, command,
693698
env["LANGUAGE"] = "C"
694699
env["LC_ALL"] = "C"
695700
env.update(self._environment)
701+
if inline_env is not None:
702+
env.update(inline_env)
696703

697704
if is_win:
698705
cmd_not_found_exception = OSError

git/test/fixtures/ls_tree_empty

Whitespace-only changes.

git/test/test_git.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,21 @@ def test_it_ignores_false_kwargs(self, git):
106106
self.git.version(pass_this_kwarg=False)
107107
assert_true("pass_this_kwarg" not in git.call_args[1])
108108

109+
def test_it_accepts_environment_variables(self):
110+
filename = fixture_path("ls_tree_empty")
111+
with open(filename, 'r') as fh:
112+
tree = self.git.mktree(istream=fh)
113+
env = {
114+
'GIT_AUTHOR_NAME': 'Author Name',
115+
'GIT_AUTHOR_EMAIL': 'author@example.com',
116+
'GIT_AUTHOR_DATE': '1400000000+0000',
117+
'GIT_COMMITTER_NAME': 'Committer Name',
118+
'GIT_COMMITTER_EMAIL': 'committer@example.com',
119+
'GIT_COMMITTER_DATE': '1500000000+0000',
120+
}
121+
commit = self.git.commit_tree(tree, m='message', env=env)
122+
assert_equal(commit, '4cfd6b0314682d5a58f80be39850bad1640e9241')
123+
109124
def test_persistent_cat_file_command(self):
110125
# read header only
111126
import subprocess as sp

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy