git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "brian m. carlson" <sandals@crustytoothpaste.net>
To: Ron Ziroby Romero <ziroby@gmail.com>
Cc: git@vger.kernel.org
Subject: Re: [RFE] Add JSON output to git log commands
Date: Sun, 17 Aug 2025 22:11:48 +0000	[thread overview]
Message-ID: <aKJTpJL3kEyqDA6P@fruit.crustytoothpaste.net> (raw)
In-Reply-To: <CAGW8g7mV6az3ybYf3uzHYQTGLDwc34eebZnha1EQ3Sb6B8E-fQ@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 2706 bytes --]

On 2025-08-17 at 20:17:46, Ron Ziroby Romero wrote:
> I would like to add JSON output to the git log command.
> 
> ## Motivation
> 
> Machine parsing of git log output is prevalent, but git only provides
> human-readable output. Having git output JSON directly solves problems
> with the format option or third-party tools. Git has the information
> in a machine-readable format. It should output in a machine-readable
> format. JSON is ubiquitous and easy to generate, and therefore, it
> makes sense to output JSON.

Git provides plenty of machine-readable formats, to be clear.  They're
not typically structured in a standard way like JSON or CBOR, but many
forges and other tools do successfully parse Git output with a variety
of tools.

> The author of one of the third-party tools says that JSON output is
> the natural evolution of the Unix philosophy and should be done
> natively for all tools[4].
> 
> ## Current behaviour
> 
> Git log can output human-readable output in several ways. However,
> outputting in JSON requires third-party tools or hacking pretty
> output.
> 
> ## Proposed enhancement
> 
> Add a –pretty=json flag to output logs in JSON format.

I'd like to hear how you plan to deal with non-UTF-8 byte strings since
JSON must always be valid Unicode.  Most data in Git is only by
convention UTF-8 and can actually be in other encodings or no encoding
at all: refs, commit messages[0], and author and committer idents.

What would be a good idea is to add a byte string entry to the JSON
writer and use it for these formats.  If the data is not valid UTF-8, or
if it contains a % sign, then you URL-encode it.  Other encodings are
possible as well, but not JSON escapes[1].

Other good options would be to use CBOR instead, since it provides
native byte strings.

Bad options would be to use U+FFFD, since that makes the output useless
when you hit one of these cases (and I can tell you from $DAYJOB that
they're not that uncommon) and to just shovel bytes into the output and
let the reader be sad (which will definitely make the output useless as
well as result in angry bug reports to the list).

As a note, I think you want `--pretty`, not `-pretty` (we use two dashes
for long options).

[0] Yes, they declare an encoding, but it isn't always correct and the
encoding someone used is not always available on every system.  I saw
someone in the Linux kernel history write "latin1", which is not a valid
encoding according to Ruby, which I was using to parse it.
[1] `\u00ff` represents U+00FF, which is equivalent to the byte sequence
0xc3 0xbf, not 0xff.
-- 
brian m. carlson (they/them)
Toronto, Ontario, CA

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 262 bytes --]

  parent reply	other threads:[~2025-08-17 22:11 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-17 20:17 [RFE] Add JSON output to git log commands Ron Ziroby Romero
2025-08-17 21:09 ` Kristoffer Haugsbakk
2025-08-17 21:28   ` D. Ben Knoble
2025-08-17 22:11 ` brian m. carlson [this message]
2025-08-17 22:54 ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=aKJTpJL3kEyqDA6P@fruit.crustytoothpaste.net \
    --to=sandals@crustytoothpaste.net \
    --cc=git@vger.kernel.org \
    --cc=ziroby@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).