Andrius
Andrius

Reputation: 21128

Read and update git config with python?

I tried configparser, but it does not respect indentation for git config files (git config files have indentation inside sections as opposed to linux usual format without indentation). After I update config file, all indentation is gone.

There is similar question here: how do I emulate read and update git global config file using gitPython?

Though, I wonder if its possible to use native python to do same thing? Or gitpython is best alternative?

For example, before updating, config looks like this:

[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true

After I update it with configparser, it looks like this:

[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
test = 10

P.S. Suggested gitconfig library seems to not even exist. It does not install, saying its some sort of dummy project.

Upvotes: 4

Views: 6543

Answers (2)

Gustavo
Gustavo

Reputation: 126

You can get any property from config file using:

repo = Repo(dir_of_repository)
user_name = repo.config_reader().get_value("user", "name")

Upvotes: 3

Andreas Haferburg
Andreas Haferburg

Reputation: 5510

Here's what worked for me:

with repo.config_writer() as config:
    config.set_value("git-p4", "client", p4client)

Upvotes: 3

Related Questions