从 git 命令获取 JSON,例如 git status
Get JSON from git commands, such as git status
如果我运行这个命令:
$ git status
我得到:
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
等输出的最佳方法是什么?
这不是JSON,而是git status
has a --porcelain
option:
Give the output in an easy-to-parse format for scripts. This is similar to the short output, but will remain stable across Git versions and regardless of user configuration.
Version 2 format adds more detailed information about the state of the worktree and changed items. Version 2 also defines an extensible set of easy to parse optional headers.
Header lines start with "#" and are added in response to specific command line arguments. Parsers should ignore headers they don’t recognize.
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
,它解析 git status --porcelain=v2 --branch
并为您的 shell.
输出格式良好的字符串
如果我运行这个命令:
$ git status
我得到:
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
等输出的最佳方法是什么?
这不是JSON,而是git status
has a --porcelain
option:
Give the output in an easy-to-parse format for scripts. This is similar to the short output, but will remain stable across Git versions and regardless of user configuration.
Version 2 format adds more detailed information about the state of the worktree and changed items. Version 2 also defines an extensible set of easy to parse optional headers.
Header lines start with "#" and are added in response to specific command line arguments. Parsers should ignore headers they don’t recognize.
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
,它解析 git status --porcelain=v2 --branch
并为您的 shell.