Ciasto piekarz
Ciasto piekarz

Reputation: 8277

how do I emulate read and update git global config file using gitPython?

I want to reads git global config file using git config --list, so I can use to read and update the global config file ?

Upvotes: 1

Views: 4658

Answers (3)

Mike Slinn
Mike Slinn

Reputation: 8403

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.

Upvotes: 0

unhammer
unhammer

Reputation: 4700

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

Upvotes: 2

m.wasowski
m.wasowski

Reputation: 6387

gitconfig may be exactly what you need.

Upvotes: 1

Related Questions