← 返回首页
Allow for setting git options, that are persistent across subcommand calls by bpoldrack · Pull Request #536 · gitpython-developers/GitPython · GitHub
Skip to content

Navigation Menu

Toggle navigation
Sign in
Appearance settings
Search or jump to...

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Include my email address so I can be contacted

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Resetting focus
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected Viewed files
Conversations
Failed to load comments. Retry
Loading
Jump to
Jump to file
Failed to load files. Retry
Loading
Diff view
Unified
Split
Hide whitespace
Apply and reload
Show whitespace
Diff view
Unified
Split
Hide whitespace
Apply and reload
22 changes: 20 additions & 2 deletions git/cmd.py
Show comments View file Edit file Delete file Open in desktop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ class Git(LazyMixin):
Set its value to 'full' to see details about the returned values.
"""
__slots__ = ("_working_dir", "cat_file_all", "cat_file_header", "_version_info",
"_git_options", "_environment")
"_git_options", "_persistent_git_options", "_environment")

_excluded_ = ('cat_file_all', 'cat_file_header', '_version_info')

Expand Down Expand Up @@ -386,6 +386,7 @@ def __init__(self, working_dir=None):
super(Git, self).__init__()
self._working_dir = working_dir
self._git_options = ()
self._persistent_git_options = []

# Extra environment variables to pass to git commands
self._environment = {}
Expand All @@ -402,6 +403,20 @@ def __getattr__(self, name):
return LazyMixin.__getattr__(self, name)
return lambda *args, **kwargs: self._call_process(name, *args, **kwargs)

def set_persistent_git_options(self, **kwargs):
"""Specify command line options to the git executable
for subsequent subcommand calls

:param kwargs:
is a dict of keyword arguments.
these arguments are passed as in _call_process
but will be passed to the git command rather than
the subcommand.
"""

self._persistent_git_options = self.transform_kwargs(
split_single_char_options=True, **kwargs)

def _set_cache_(self, attr):
if attr == '_version_info':
# We only use the first 4 numbers, as everthing else could be strings in fact (on windows)
Expand Down Expand Up @@ -820,7 +835,10 @@ def _call_process(self, method, *args, **kwargs):

call = [self.GIT_PYTHON_GIT_EXECUTABLE]

# add the git options, the reset to empty
# add persistent git options
call.extend(self._persistent_git_options)

# add the git options, then reset to empty
# to avoid side_effects
call.extend(self._git_options)
self._git_options = ()
Expand Down
14 changes: 14 additions & 0 deletions git/test/test_git.py
Show comments View file Edit file Delete file Open in desktop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,20 @@ def test_options_are_passed_to_git(self):
git_command_version = self.git.version()
self.assertEquals(git_version, git_command_version)

def test_persistent_options(self):
git_command_version = self.git.version()
# analog to test_options_are_passed_to_git
self.git.set_persistent_git_options(version=True)
git_version = self.git.NoOp()
self.assertEquals(git_version, git_command_version)
# subsequent calls keep this option:
git_version_2 = self.git.NoOp()
self.assertEquals(git_version_2, git_command_version)

# reset to empty:
self.git.set_persistent_git_options()
self.assertRaises(GitCommandError, self.git.NoOp)

def test_single_char_git_options_are_passed_to_git(self):
input_value = 'TestValue'
output_value = self.git(c='user.name=%s' % input_value).config('--get', 'user.name')
Expand Down
Toggle all file notes Toggle all file annotations

Footer

© 2026 GitHub, Inc.