9

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?

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817

1 Answers1

6

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.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Future JSON output could be implemented later: https://public-inbox.org/git/57969607.9080206@gmail.com/T/ – VonC Jul 08 '18 at 04:58
  • 3
    All unix tools would all benefit from `--json`. W/o `--json`, it's for humans, with `--json` it's for machines. – Alexander Mills Jul 08 '18 at 04:59
  • 1
    @AlexanderMills my point was: `git status --porcelain` is *already* for machine, not for human consumption. But yes, it is not JSON yet. – VonC Jul 08 '18 at 05:00
  • Well, there should be a universal interface for command line tools, text streaming etc. I shouldn't have to guess how to parse the output. I should just be able to parse newline delimited JSON, if I use a --json switch. – Alexander Mills Jul 08 '18 at 05:18
  • 1
    @AlexanderMills I agree, which is why I mention in the comments https://public-inbox.org/git/57969607.9080206@gmail.com/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. – VonC Jul 08 '18 at 05:19
  • Yeah that is good, I am definitely +1 for making JSON some sort of command line standard. If JSON were used, every command line tool could have *type* information for stdout/stderr, that would be super cool. – Alexander Mills Jul 08 '18 at 05:21
  • 1
    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) – Lothar Apr 10 '21 at 22:30