1

So if I have a git config file something like this.

[color "branch"]
    current = yellow bold
    local = green bold
    remote = cyan bold

I want to read the text between in quotes. I tried using

repo_config = Repo(projects_dir+"/"+repo+".git")
color=repo_config.config_reader().get_value("color")

I can read the fields inside it like current,local,remote but I want to read the quoted text corresponding to color, how do I go about it

2 Answers 2

1
configs = """
[color "branch"]
    current = yellow bold
    local = green bold
    remote = cyan bold
"""
file_name = 'test-config.txt'

with open(file_name,'w') as file:
    file.write(configs)

method 01

with open(file_name,'r') as file:
    file_txt = file.read()
    
print(file_txt.split('"')[1])

method 02

import configparser
config = configparser.ConfigParser()
config.read(file_name)
print(config.sections()[0].split(' ')[-1].replace('"',''))

output

## method 01
branch
## method 02
branch
Sign up to request clarification or add additional context in comments.

Comments

0

Using gitpython v3.1.31, you can write to the global system config file like so:

def configure_git_details():
    config = git.GitConfigParser(
        file_or_files=git.config.get_config_path("global"),
        read_only=False
    )

    with config:
        config.set_value("user", "name", "Foo Bar")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.