Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get JSON from git commands, such as git status

Tags:

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?

like image 428
Alexander Mills Avatar asked Jul 08 '18 04:07

Alexander Mills


People also ask

How do I check my git status?

To check the status, open the git bash, and run the status command on your desired directory.

What is git status command?

The git status command displays the state of the working directory and the staging area. It lets you see which changes have been staged, which haven't, and which files aren't being tracked by Git.

What is git and git commands?

Git commands are a distributed version control system for tracking changes in any set of files. They were originally designed for coordinating work among programmers who were operating source codes during software development.

How many commands are there in git?

There are three commands with similar names: git reset , git restore and git revert .


1 Answers

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.

like image 119
VonC Avatar answered Sep 28 '22 02:09

VonC