首页 > 解决方案 > 使用 python 读取和更新 git 配置?

问题描述

我试过configparser了,但它不尊重 git 配置文件的缩进(git 配置文件在部分内有缩进,而不是没有缩进的 linux 通常格式)。更新配置文件后,所有缩进都消失了。

这里有类似的问题:如何使用 gitPython 模拟读取和更新 git 全局配置文件?

不过,我想知道是否可以使用本机 python 来做同样的事情?还是gitpython最好的选择?

例如,在更新之前,配置如下所示:

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

在我用 更新它后configparser,它看起来像这样:

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

PS 建议gitconfig的库似乎甚至不存在。它没有安装,说它是某种虚拟项目。

标签: pythongitconfigparser

解决方案


这对我有用:

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

推荐阅读