Files
git/diff-provider-internal.h
Michael Montalbo 91c8989d4e diff: consult oid-only hunk providers via diff.<driver>.process
The provider chain so far holds the diff-hunks store in front of the
terminal builtin computation.  Open it to external processes: a pair on
a path whose driver configures diff.<driver>.process is answered by a
long-running process speaking a pkt-line protocol (following the filter
process protocol), registered at the head of the chain and consulted
before the store and before any blob is loaded.

The protocol starts with the smallest request that can carry an answer:
object names alone.  A request is the pathname and the pair's
old-oid/new-oid, with no content.  The process answers with hunk lines,
with a zero-hunk success that asserts the blobs equivalent (trailing
newlines included), or with status=need-content, on which the pair
falls through to the builtin answer.  This serves the two shapes that
need no content pushed to them: a cache keyed on the blob pair, and a
process that fetches the blobs itself (for example over "git cat-file
--batch").  A pair whose side is not a stored blob carries a NULL id;
the provider sends no request and passes it.  Because Git holds no
content for the exchange, the answer is used as sent: hunks are
validated for order, overlap, lockstep alignment, and magnitude, then
replayed without the normalization xdiff applies to diffs it computes
itself.  The magnitude bound is the blobs' sizes, read from the object
database without loading content: a blob of N bytes holds at most N
lines.

Because the process's answer is authoritative, it outranks the store,
and its head-of-chain position says so.  A pair the process answers
never reaches the store and is never recorded, so nothing it produces
enters the store, which holds the builtin answer only.  A request it
does not answer, whether need-content, a missing capability, or a
missing id, passes down the chain to the builtin answer, which is what
the store serves, so the store may serve such a pair and a warming run
may record it.  Entries recorded before a process was configured are
not purged; a pair the process answers ignores them, and "git diff-hunks
clear" discards them.

The provider gates itself per request.  The driver is looked up by the
old-side path, so a renamed file resolves to the same driver, and by
the repository-relative path, so a diff.relative run from a
subdirectory names the pair the same way.  Options the process is never
told about select no process: the whitespace-ignoring options, -I,
--anchored, and an algorithm forced by option or configuration (blame
routes its algorithm through xdl_opts, so --histogram is covered).  The
request gains its last field, the path; the consumers change only by
filling it, and neither names the process.

The provider's state is its repository's pool of running processes,
keyed by the configured command, so drivers sharing a command share a
process, a submodule speaks to its own, and releasing the provider
(from repo_clear()) stops them.  The pool owns a copy of each command
string, so an entry outlives a config re-read.  A command that fails
stays as an entry that is not retried: its request and every later one
pass, so the store may serve the path for the rest of the command.

A protocol error in a response never kills the command.  The response
is read through a packet reader gentle about framing, so an error takes
one path: a single warning, the process stopped and marked failed, and
the builtin diff for the rest of the command.  That covers garbage
bytes, a truncated response, an empty packet, a bare status, and an
unrecognized status.  Semantically invalid coordinates cost only their
pair: the response is drained, the pair is computed, and the process
stays alive.  A path the protocol cannot carry (an embedded newline, or
one too long for a packet) falls back per path rather than costing the
command its process.  The handshake keeps one fatal check: a process
that announces a capability Git did not request aborts the command, as
the long-running filter protocol does.

Consulting is allowed per command, following the allow_textconv
precedent.  "git diff", "git log" and "git show", and "git blame" set
allow_diff_process; the plumbing diff commands and the interactive-patch
machinery never set it, so scripted and staging output stays builtin.
The options adjust the flag:

- --no-ext-diff clears it and --ext-diff sets it;
- --diff-process and --no-diff-process set and clear it alone, leaving
  external diff drivers as they were;
- format-patch clears it unconditionally, so a generated patch applies
  for recipients without the process;
- range-diff passes --no-ext-diff to the "git log" it compares.

git blame and the summary formats consult the process.  For blame, a
pair reported equivalent emits no hunks, so the whole commit passes to
its parent.  In the stat formats such a pair sums to a zero-count entry,
which the "nothing changed" rule omits, as under -w.  The subprocess is
long-running: one startup cost across a traversal, one round-trip per
consulted pair.  Answers travel in struct xdl_hunk, new in
xdiff-interface.h, holding xdiff's 1-based coordinates; nothing feeds
them back to xdiff, since only coordinate consumers consult.

A content-carrying request is the natural extension: it would serve
sides that are not stored blobs and processes that want content pushed
to them, and bring patch output and log -L's range tracking to the same
answer.  As it stands, a process's answers show in blame and the summary
formats while patch output stays builtin.

t4080 exercises the protocol, the per-command gate, and the error paths:

- each adversarial response shape warns and falls back to builtin, the
  request log proving which failures disable the process and which keep
  it alive (a malformed hunk line, coordinates past the blob size, a
  count overflowing strtol(), overlapping or misaligned hunks, an
  unrecognized status, a bare status, an empty packet, a mid-response
  crash, and raw garbage);
- a capability-less process and status=abort degrade without noise, and
  a failed start warns once and returns the path to the store;
- a trailing token on a hunk line is ignored, pinning field
  appendability;
- positive consults for git diff, git show, and diff-tree under
  --ext-diff and --diff-process; textconv output and gitlink sides are
  never identified; a diff.relative run consults by the repo-relative
  path;
- the equivalence answer is pinned from both consumers, and a warming
  run past a deferring process records the pair for a later read.

Helped-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Michael Montalbo <mmontalbo@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2026-08-01 15:09:52 -07:00

131 lines
4.4 KiB
C

#ifndef DIFF_PROVIDER_INTERNAL_H
#define DIFF_PROVIDER_INTERNAL_H
#include "diff-provider.h"
/*
* The implementor-facing half of the hunk provider interface: the
* provider chain a repository owns, and the rules a provider applies
* to its own answer before any consumer sees it. Provider
* implementations include this header; consumers of the interface
* use only diff-provider.h.
*/
/*
* A provider's verdict on one request. Only the chain walk
* (diff-provider.c) sees these; it maps the dispositions of a whole
* walk onto the public outcome set.
*/
enum diff_provider_disposition {
/*
* The provider failed to produce the answer it owns. Only
* the computing provider returns this: its compute leg is
* the one part of a consultation that can fail, and the walk
* ends with the public error outcome.
*/
DIFF_PROVIDER_DISP_ERROR = -1,
/*
* Answered: every hunk of the pair has been emitted through
* the consumer's callback.
*/
DIFF_PROVIDER_DISP_ANSWERED = 0,
/* Not this provider's request: the walk consults the next one. */
DIFF_PROVIDER_DISP_PASS,
/*
* The pair must not be answered from identity nor recorded:
* the request is shaped by parameters the provider's
* recording key cannot express, so a recorded answer would
* not match this request, and this request's result must not
* be recorded under that key. The walk goes on, but consults
* only the computing provider, and its fall-through outcome
* tells the consumer not to record.
*/
DIFF_PROVIDER_DISP_STOP_NO_RECORD,
};
/*
* One provider in a repository's chain (repository.h). The chain is
* assembled in diff-provider.c with a fixed composition; whether a
* provider applies to a request is decided by nobody but the
* provider, whose consult gates itself and passes. Chain position
* carries the authority resolution: an earlier provider's answer or
* refusal outranks every provider after it.
*/
struct diff_provider {
/*
* Consult this provider for one request. fill is NULL on a
* consult-only walk; only the computing provider reads it,
* and it must pass when fill is NULL.
*/
enum diff_provider_disposition
(*consult)(struct diff_provider *provider,
const struct diff_provider_request *req,
diff_provider_fill_fn fill, void *fill_data,
xdl_emit_hunk_consume_func_t hunk_cb,
void *cb_data);
/*
* Tear down the provider's state, or NULL when it owns none.
* Runs when the owning repository is cleared; the chain frees
* the provider itself afterwards.
*/
void (*release)(struct diff_provider *provider);
void *state;
/*
* Set on the provider that loads content and computes rather
* than answering from the request's identity. It alone is
* still consulted after a stop-no-record: an identity answer
* may no longer be served, but the computation must still
* run.
*/
unsigned computes:1;
struct diff_provider *next;
};
/*
* The providers Git ships, besides the builtin computation that
* diff-provider.c holds itself. Each call returns a fresh provider
* for one repository's chain.
*/
struct diff_provider *diff_process_provider_new(void);
struct diff_provider *diff_hunks_store_provider_new(void);
/*
* Incremental well-formedness check for a provider-supplied hunk
* sequence, shared by every provider. Each coordinate, and each
* hunk's end (its start plus count), must fit int32 (a consumer may
* truncate to int, and a provider may serialize as such); hunks must
* be in order and must not overlap; and the unchanged run between
* hunks must be the same length on both sides, or a consumer that
* walks the two files in lockstep desynchronizes. Every rule
* constrains differences between coordinates, so the check applies
* to 0-based and 1-based sequences alike.
*
* Feed the hunks in order to a zero-initialized struct; the first
* nonzero return names the violated rule, and the whole sequence must
* then be discarded unemitted.
*/
struct diff_provider_hunks_check {
int64_t prev_old_end, prev_new_end;
};
enum diff_provider_hunks_error {
DIFF_PROVIDER_HUNKS_OK = 0,
DIFF_PROVIDER_HUNKS_RANGE, /* negative or beyond int32 */
DIFF_PROVIDER_HUNKS_OVERLAP, /* out of order or overlapping */
DIFF_PROVIDER_HUNKS_MISALIGNED, /* unchanged runs differ in length */
};
enum diff_provider_hunks_error
diff_provider_check_hunk(struct diff_provider_hunks_check *c,
long old_start, long old_count,
long new_start, long new_count);
#endif /* DIFF_PROVIDER_INTERNAL_H */