Chris Vilches
Chris Vilches

Reputation: 1187

How to create a Git GUI and parse git's output?

I want to create a little Git GUI and my idea is to make a program that calls the normal git software (using exec or something similar), but my problem is that I don't know what to do with the stdout (output) of git. It prints everything as a string.

Is there a way to make it print some kind of data that can be easily parsed, such as a JSON or something?

How are GUIs like git-gui for Windows or Gitkraken getting the output?

If you know another program that works like that, then please tell me the approach I can use.

Upvotes: 1

Views: 1780

Answers (2)

Philippe
Philippe

Reputation: 31227

You've got 2 possibilities with their own advantages and trade offs.

  1. Use the git exe

Pro:

  • Up to date
  • do not behave differently than the official client, contrary to a library (major advantage)

Cons:

  • painful to use and parse output
  • need to support different versions

But some commands could output something easier to parse. See for example parameters --porcelain or --outputin the command help: https://git-scm.com/docs/git-status

  1. Use a library in your language of choice.

You could try to use a pure implementation in your language of choice or use a wrapper around the well maintained library 'libgit2' https://libgit2.github.com/

Pro:

  • Really easier to use
  • performance could be better (no need to read git files at every command)

Cons:

  • use could be a little different or features limited (especially for language pure implementations)

Ps: which language and which system do you target? Why not improve an existing GUI? Because that's a lot of hard work...

Upvotes: 2

Bryan Oakley
Bryan Oakley

Reputation: 386342

There are libraries for working with git in many languages. For example, you can use python's gitpython or ruby's ruby-git, etc.

For example:

from git import Repo
repo = Repo("/path/to/a/repo")
for file in repo.untracked_files:
    print("untracked file:", file)

Upvotes: 3

Related Questions