解析 git 配置 --list

Parse git config --list

我要解析git config --list

首先我想我可以将其解析为以点分隔的对象。例如

color.ui=auto
branch.master.remote=upstream
branch.master.merge=refs/heads/master
# => { 
    core: { ui: auto },
    branch: { master: { remote: upstream, merge: refs/heads/master }}
}

但有时名字有点:(

我想做的如下:

url.git@gist.github.com:.pushinsteadof=https://gist.github.com/
branch.chore/foo.bar.baz.remote=upstream
branch.chore/foo.bar.baz.merge=refs/heads/master
# => {
    url: { git@gist.github.com: { pushinsteadof: https://gist.github.com/ } },
    branch: { chore/foo.bar.baz: { remote: upstream, merge: refs/heads/master } }
}

如何实现? 我应该重新实现 git-config https://git-scm.com/docs/git-config#FILES 并包含部分吗? 我想更改节分隔符。

据我所见git config man page,关键的第一个和最后一个元素中没有点。

因此在 branch.chore/foo.bar.baz.remote 中,您可以搜索第一个“.”。最后一个“。” (给你 branchremote)。中间的都是一个键名。