12

If I run this command:

$ git status

I get:

On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean

This is pretty difficult to parse.

But what would be really nice is --json output, in another world, I'd love to see:

 $ git status --json

and get this:

   {
    "currentBranch": "master",
    "remoteTrackingBranch": "origin/master",
    "isUpToDateWithRemote": true,
    "workingDirectoryClean": true
   }

is there some tool in the NPM ecosystem that can parse Git output into JSON? What is the best way to parse the output from git status, etc?

1
  • For example, git log has an option git log --oneline but AFAICT, git status has no analogue to that Commented Jul 8, 2018 at 4:45

1 Answer 1

9

This is no JSON, but 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.

See porcelain format v1 and v2:

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

See as an example robertgzr/porcelain, which parses git status --porcelain=v2 --branch and outputs nicely formatted strings for your shell.

Sign up to request clarification or add additional context in comments.

7 Comments

Future JSON output could be implemented later: public-inbox.org/git/[email protected]/T
All unix tools would all benefit from --json. W/o --json, it's for humans, with --json it's for machines.
@AlexanderMills my point was: git status --porcelain is already for machine, not for human consumption. But yes, it is not JSON yet.
@AlexanderMills I agree, which is why I mention in the comments public-inbox.org/git/[email protected]/T: a --porcelain=v3 option with JSON output could be coming. But for now, you would have to parse the existing output, and generate a JSON yourself.
Yeah once again the Unix world should be looking to windows where we have something like this for a decade now. But unix guys are too arrogance to admit their failures (looking at how they still fight against systemd)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.