如果我运行这个命令:
$ git status
I get:
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
这很难解析。
但真正好的是 --json 输出,在另一个世界中,我很想看到:
$ git status --json
并得到这个:
{
"currentBranch": "master",
"remoteTrackingBranch": "origin/master",
"isUpToDateWithRemote": true,
"workingDirectoryClean": true
}
NPM 生态系统中是否有一些工具可以将 Git 输出解析为 JSON?解析输出的最佳方法是什么git status
, etc?
这不是 JSON,但是git status has a --porcelain option https://git-scm.com/docs/git-status#git-status---porcelainltversiongt:
以易于解析的脚本格式提供输出。这与短输出类似,但无论用户配置如何,跨 Git 版本都将保持稳定。
See 瓷器格式 v1 和 v2 https://git-scm.com/docs/git-status#_porcelain_format_version_1:
版本 2 格式添加了有关工作树状态和更改项目的更多详细信息。版本 2 还定义了一组可扩展的易于解析的可选标头。
标题行以“#”开头,是为了响应特定的命令行参数而添加的。解析器应该忽略它们无法识别的标头。
vonc@voncvb C:\test
> git status --porcelain=v2 --branch
# branch.oid a4a9ae9616e5f1da136a3ff717e722d055ca9aa7
# branch.head master
# branch.upstream origin/master
1 .M N... 100644 100644 100644 67f7a2a439ffb9dd18dd65bb6fd296f8c16c55b3 67f7a2a439ffb9dd18dd65bb6fd296f8c16c55b3 test/file1.txt
1 .M N... 100644 100644 100644 d59cac0c8acf674ba3316944451dcbec3e6ec3d7 d59cac0c8acf674ba3316944451dcbec3e6ec3d7 test/file2.txt
参见示例robertgzr/porcelain https://github.com/robertgzr/porcelain,它解析git status --porcelain=v2 --branch
并为您的 shell 输出格式良好的字符串。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)