Skip to content

Commit edcc858

Browse files
larsxschneidergitster
authored andcommitted
convert: add filter.<driver>.process option
Git's clean/smudge mechanism invokes an external filter process for every single blob that is affected by a filter. If Git filters a lot of blobs then the startup time of the external filter processes can become a significant part of the overall Git execution time. In a preliminary performance test this developer used a clean/smudge filter written in golang to filter 12,000 files. This process took 364s with the existing filter mechanism and 5s with the new mechanism. See details here: git-lfs/git-lfs#1382 This patch adds the `filter.<driver>.process` string option which, if used, keeps the external filter process running and processes all blobs with the packet format (pkt-line) based protocol over standard input and standard output. The full protocol is explained in detail in `Documentation/gitattributes.txt`. A few key decisions: * The long running filter process is referred to as filter protocol version 2 because the existing single shot filter invocation is considered version 1. * Git sends a welcome message and expects a response right after the external filter process has started. This ensures that Git will not hang if a version 1 filter is incorrectly used with the filter.<driver>.process option for version 2 filters. In addition, Git can detect this kind of error and warn the user. * The status of a filter operation (e.g. "success" or "error) is set before the actual response and (if necessary!) re-set after the response. The advantage of this two step status response is that if the filter detects an error early, then the filter can communicate this and Git does not even need to create structures to read the response. * All status responses are pkt-line lists terminated with a flush packet. This allows us to send other status fields with the same protocol in the future. Helped-by: Martin-Louis Bright <[email protected]> Reviewed-by: Jakub Narebski <[email protected]> Signed-off-by: Lars Schneider <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 234fa07 commit edcc858

File tree

4 files changed

+1082
-10
lines changed

4 files changed

+1082
-10
lines changed

Documentation/gitattributes.txt

Lines changed: 154 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,15 @@ checkout, when the `smudge` command is specified, the command is
293293
fed the blob object from its standard input, and its standard
294294
output is used to update the worktree file. Similarly, the
295295
`clean` command is used to convert the contents of worktree file
296-
upon checkin.
296+
upon checkin. By default these commands process only a single
297+
blob and terminate. If a long running `process` filter is used
298+
in place of `clean` and/or `smudge` filters, then Git can process
299+
all blobs with a single filter command invocation for the entire
300+
life of a single Git command, for example `git add --all`. If a
301+
long running `process` filter is configured then it always takes
302+
precedence over a configured single blob filter. See section
303+
below for the description of the protocol used to communicate with
304+
a `process` filter.
297305

298306
One use of the content filtering is to massage the content into a shape
299307
that is more convenient for the platform, filesystem, and the user to use.
@@ -373,6 +381,151 @@ not exist, or may have different contents. So, smudge and clean commands
373381
should not try to access the file on disk, but only act as filters on the
374382
content provided to them on standard input.
375383

384+
Long Running Filter Process
385+
^^^^^^^^^^^^^^^^^^^^^^^^^^^
386+
387+
If the filter command (a string value) is defined via
388+
`filter.<driver>.process` then Git can process all blobs with a
389+
single filter invocation for the entire life of a single Git
390+
command. This is achieved by using a packet format (pkt-line,
391+
see technical/protocol-common.txt) based protocol over standard
392+
input and standard output as follows. All packets, except for the
393+
"*CONTENT" packets and the "0000" flush packet, are considered
394+
text and therefore are terminated by a LF.
395+
396+
Git starts the filter when it encounters the first file
397+
that needs to be cleaned or smudged. After the filter started
398+
Git sends a welcome message ("git-filter-client"), a list of supported
399+
protocol version numbers, and a flush packet. Git expects to read a welcome
400+
response message ("git-filter-server"), exactly one protocol version number
401+
from the previously sent list, and a flush packet. All further
402+
communication will be based on the selected version. The remaining
403+
protocol description below documents "version=2". Please note that
404+
"version=42" in the example below does not exist and is only there
405+
to illustrate how the protocol would look like with more than one
406+
version.
407+
408+
After the version negotiation Git sends a list of all capabilities that
409+
it supports and a flush packet. Git expects to read a list of desired
410+
capabilities, which must be a subset of the supported capabilities list,
411+
and a flush packet as response:
412+
------------------------
413+
packet: git> git-filter-client
414+
packet: git> version=2
415+
packet: git> version=42
416+
packet: git> 0000
417+
packet: git< git-filter-server
418+
packet: git< version=2
419+
packet: git< 0000
420+
packet: git> capability=clean
421+
packet: git> capability=smudge
422+
packet: git> capability=not-yet-invented
423+
packet: git> 0000
424+
packet: git< capability=clean
425+
packet: git< capability=smudge
426+
packet: git< 0000
427+
------------------------
428+
Supported filter capabilities in version 2 are "clean" and
429+
"smudge".
430+
431+
Afterwards Git sends a list of "key=value" pairs terminated with
432+
a flush packet. The list will contain at least the filter command
433+
(based on the supported capabilities) and the pathname of the file
434+
to filter relative to the repository root. Right after the flush packet
435+
Git sends the content split in zero or more pkt-line packets and a
436+
flush packet to terminate content. Please note, that the filter
437+
must not send any response before it received the content and the
438+
final flush packet.
439+
------------------------
440+
packet: git> command=smudge
441+
packet: git> pathname=path/testfile.dat
442+
packet: git> 0000
443+
packet: git> CONTENT
444+
packet: git> 0000
445+
------------------------
446+
447+
The filter is expected to respond with a list of "key=value" pairs
448+
terminated with a flush packet. If the filter does not experience
449+
problems then the list must contain a "success" status. Right after
450+
these packets the filter is expected to send the content in zero
451+
or more pkt-line packets and a flush packet at the end. Finally, a
452+
second list of "key=value" pairs terminated with a flush packet
453+
is expected. The filter can change the status in the second list
454+
or keep the status as is with an empty list. Please note that the
455+
empty list must be terminated with a flush packet regardless.
456+
457+
------------------------
458+
packet: git< status=success
459+
packet: git< 0000
460+
packet: git< SMUDGED_CONTENT
461+
packet: git< 0000
462+
packet: git< 0000 # empty list, keep "status=success" unchanged!
463+
------------------------
464+
465+
If the result content is empty then the filter is expected to respond
466+
with a "success" status and a flush packet to signal the empty content.
467+
------------------------
468+
packet: git< status=success
469+
packet: git< 0000
470+
packet: git< 0000 # empty content!
471+
packet: git< 0000 # empty list, keep "status=success" unchanged!
472+
------------------------
473+
474+
In case the filter cannot or does not want to process the content,
475+
it is expected to respond with an "error" status.
476+
------------------------
477+
packet: git< status=error
478+
packet: git< 0000
479+
------------------------
480+
481+
If the filter experiences an error during processing, then it can
482+
send the status "error" after the content was (partially or
483+
completely) sent.
484+
------------------------
485+
packet: git< status=success
486+
packet: git< 0000
487+
packet: git< HALF_WRITTEN_ERRONEOUS_CONTENT
488+
packet: git< 0000
489+
packet: git< status=error
490+
packet: git< 0000
491+
------------------------
492+
493+
In case the filter cannot or does not want to process the content
494+
as well as any future content for the lifetime of the Git process,
495+
then it is expected to respond with an "abort" status at any point
496+
in the protocol.
497+
------------------------
498+
packet: git< status=abort
499+
packet: git< 0000
500+
------------------------
501+
502+
Git neither stops nor restarts the filter process in case the
503+
"error"/"abort" status is set. However, Git sets its exit code
504+
according to the `filter.<driver>.required` flag, mimicking the
505+
behavior of the `filter.<driver>.clean` / `filter.<driver>.smudge`
506+
mechanism.
507+
508+
If the filter dies during the communication or does not adhere to
509+
the protocol then Git will stop the filter process and restart it
510+
with the next file that needs to be processed. Depending on the
511+
`filter.<driver>.required` flag Git will interpret that as error.
512+
513+
After the filter has processed a blob it is expected to wait for
514+
the next "key=value" list containing a command. Git will close
515+
the command pipe on exit. The filter is expected to detect EOF
516+
and exit gracefully on its own. Git will wait until the filter
517+
process has stopped.
518+
519+
If you develop your own long running filter
520+
process then the `GIT_TRACE_PACKET` environment variables can be
521+
very helpful for debugging (see linkgit:git[1]).
522+
523+
Please note that you cannot use an existing `filter.<driver>.clean`
524+
or `filter.<driver>.smudge` command with `filter.<driver>.process`
525+
because the former two use a different inter process communication
526+
protocol than the latter one.
527+
528+
376529
Interaction between checkin/checkout attributes
377530
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
378531

0 commit comments

Comments
 (0)