I want to reads git global config file using git config --list, so I can use to read and update the global config file ?
3 Answers
This will give you the ~/.gitconfig type config:
globalconfig = git.GitConfigParser([os.path.normpath(os.path.expanduser("~/.gitconfig"))], read_only=True)
That's more or less what gitpython itself does, except it also uses "system" and "repo" level configs (where system is "/etc/gitconfig"), see
def _get_config_path(self, config_level):
and
def config_reader(self, config_level=None):
in git/base.py in the source of gitpython
Comments
gitconfig may be exactly what you need.
3 Comments
Ciasto piekarz
So does
.gitconfig always lies in user directory location ? I mean what if its location is different for a different user ?torek
The "user global" (as opposed to "system global") git config file may be in
~/.gitconfig, but XDG now suggests that it should be in $XDG_CONFIG_HOME/git/config with XDG_CONFIG_HOME defaulting to ~/.config. Insert standard grumble about XDG here. :-)Berk
This library package is an empty package and can not be installed.
Since @unhammer posted their answer in 2014 GitPython has continued to evolve. Here is an example of how to read the global user name using Python 3 and GitPython v3.1.11:
def git_user_name() -> str:
from git import Repo
reader = Repo(path=None).config_reader()
field = reader.get_value("user", "name")
return field
This works from any directory, a local repo is not required.