Porcelain and --json contracts

Porcelain and --json contracts

Every verb prints a machine shape as well as prose: --porcelain for tab-separated rows, --json for one JSON object. Those columns and keys are the stable part of loot's output. Everything else it prints can change.

Which verbs

A form of a verb takes a machine format when its usage line shows --porcelain, --json, or both, and that line names the keys. The forms that take neither are the ones whose stdout is already the answer: content (loot cat, loot diff --content, loot format-patch, loot grep), one name (loot buoy --nearest), another program's output (loot bisect run), or a verb that runs until stopped (loot serve). loot help --json prints the whole set as data:

loot help --json
{"contract":14,"verbs":[{"verb":"status","valued":["--path","--remote"],
  "bare":["--json","--porcelain","--unpushed"],"porcelain":true,"json":true,
  "forms":[{"usage":"loot status [--porcelain|--json] ...","porcelain":true,"json":true}],
  "subcommands":[]}, ...]}

Each verb carries the flags its gate accepts, one entry in forms per usage line saying whether that form names a machine format, and its subcommands with their own flags. Bare loot --help is the same index as one line per verb; loot <verb> --help prints one verb in full, and loot help --all prints all of them.

Most verbs with a machine shape take both formats, and a few take one. If you pass both, --json wins.

What a write answers

A verb that writes answers --json with one object naming what it wrote, so the id goes straight into the next command. A version id is whole hex. A change id comes twice: change in the hex loot log --json prints, and change_id in the letters a selector takes.

$ loot describe -m "fix the parser" --json
{"contract":14,"change":"9d82…","change_id":"txsmoxmm…","version":"7132…","subject":"fix the parser"}
$ loot ticket new --title "parser drops the last token" --json
{"contract":14,"ticket":"voryupzl","wrote":["tickets/voryupzl/meta","tickets/voryupzl/body"]}

Under --json, stdout is that object and nothing else. A verb that narrates as it works (loot push, loot clone) narrates on stderr.

The contract

$ loot --version --json
{"contract":14,"version":"0.4.30"}

Every --json object carries the same "contract" as its first key. The two exceptions are loot telemetry show and loot telemetry on, which print the telemetry report itself. The number is loot's storage format version (see Format versions and compatibility), so it moves when the format does, and a breaking change to a column or a mark must move it. Porcelain carries no version line, so a parser never skips one.

The public alpha says commands and printed output change without notice. These columns and marks are the exception. A shape grows in three ways only: a new member at the end of a flags field, a new trailing key in a JSON object, or new rows behind a flag that did not exist before. A row that has shipped never gains a column. So match flag members rather than whole fields, and ignore JSON keys you don't know.

Porcelain rules

  • One row per line, fields separated by tabs, no header.
  • Field one is a single-character mark naming the row's kind. Rows with different marks can have different field counts, so switch on the mark first. The same mark can mean different things in different verbs.
  • An absent value is - (null in JSON). A count loot could not measure is also -, never 0; a 0 is a real measurement.
  • Yes/no facts share one comma-separated flags field, - when empty.
  • The field holding text someone else chose, such as a path, comes last. Split off the leading fields and keep the rest whole.

Some older shapes break a rule: loot gates separates fields with single spaces, loot telemetry rows are keyed by field name, and some rows put a path or a name before the last field. Where a field can hold a tab or a line break, read --json. A path containing a line break cannot be framed as a row; newer shapes print - in its place and add the unprintable flag, and --json carries the name escaped.

An example

loot diff --porcelain after editing two files, one of them restricted:

M	34d45fd27b5a659e48ac633698c9725af09009819452ef97007151e00293c18b	restricted=alice	lines	1	1	-	.env
M	623a5460d841b6d1c13d080e85500e0043fd4ba4a8ba9c1aa9b4f6e0d212276c	internal	lines	1	1	-	README.md
%	2	1	0	0

Each path row is the change class (+, M, -, R), the content address, the tier, how far loot could read the content, lines added, lines deleted, flags, and the path. The % row closes every run with a disclosure tally: clear, restricted, embargoed, unreadable. A path you hold no key for has its name withheld and the sealed flag.

loot diff --porcelain | while IFS=$'\t' read -r mark oid tier rung added deleted flags path; do
  [ "$mark" = "%" ] && continue            # the tally row has its own fields
  case ",$flags," in *,sealed,*) continue ;; esac
  echo "$mark $path ($tier): +$added -$deleted"
done

Errors and stderr

Under --json, a failing verb prints {"contract":14,"error":{"code":…,"message":…}} on stderr and exits 1. Codes are frozen like columns; error is the generic code for a failure that has no code of its own. Without --json, including under --porcelain, a failure is the loot: <message> line. Notes and warnings also go to stderr, so stdout holds only the shape. A conflict is not a failure: the verb exits 2 and the path is a C row. See Exit codes.

From Windows PowerShell 5.1, judge a verb by $LASTEXITCODE: under $ErrorActionPreference = 'Stop' a note on stderr throws even when the verb succeeded. Pass a body as a file, never through a pipe, which 5.1 encodes as ASCII unless $OutputEncoding is UTF-8. pwsh 7 has neither trap.

loot seek --schema prints seek's whole machine contract (flags, marks, columns and error codes) as JSON derived from the code.