forked from git/git
-
Notifications
You must be signed in to change notification settings - Fork 188
[RFC] diff: add diff.<driver>.process for external hunk providers
#2120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mmontalbo
wants to merge
4
commits into
gitgitgadget:master
Choose a base branch
from
mmontalbo:mm/structural-diff-backend-clean
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f887a7e
xdiff: support external hunks via xpparam_t
mmontalbo de6d85f
userdiff: add diff.<driver>.process config
mmontalbo c25647c
diff: add long-running diff process via diff.<driver>.process
mmontalbo 39ff53a
blame: consult diff process for zero-hunk detection
mmontalbo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -218,6 +218,14 @@ endif::git-diff[] | |
| Set this option to `true` to make the diff driver cache the text | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Junio C Hamano wrote on the Git mailing list (how to reply to this email): "Michael Montalbo via GitGitGadget" <gitgitgadget@gmail.com> writes:
> Zero hunks with status=success means the tool considers the
> files equivalent. Git skips diff output for that file.
Is "zero hunk" a common word or some random string you invented? If
the latter, which is I am assuming it to be, you should define what
it means at/before the first use. Here in the proposed log message,
and ...
>
> Signed-off-by: Michael Montalbo <mmontalbo@gmail.com>
> ---
> Documentation/config/diff.adoc | 8 +
> Documentation/gitattributes.adoc | 40 ++++
> Makefile | 1 +
> diff-process.c | 206 +++++++++++++++++++
> diff-process.h | 28 +++
> diff.c | 23 +++
> t/.gitattributes | 1 +
> t/t4080-diff-process.sh | 338 +++++++++++++++++++++++++++++++
> 8 files changed, 645 insertions(+)
> create mode 100644 diff-process.c
> create mode 100644 diff-process.h
> create mode 100755 t/t4080-diff-process.sh
>
> diff --git a/Documentation/config/diff.adoc b/Documentation/config/diff.adoc
> index 1135a62a0a..4ab5f60df6 100644
> --- a/Documentation/config/diff.adoc
> +++ b/Documentation/config/diff.adoc
> @@ -218,6 +218,14 @@ endif::git-diff[]
> Set this option to `true` to make the diff driver cache the text
> conversion outputs. See linkgit:gitattributes[5] for details.
>
> +`diff.<driver>.process`::
> + The command to run as a long-running diff process.
> + The tool communicates via the pkt-line protocol and returns
> + hunks that are fed into Git's diff and blame pipelines.
> + If the tool returns zero hunks, the file is treated as
> + unchanged for both diff output and blame attribution.
> + See linkgit:gitattributes[5] for details.
... also here.
I do not know if you mean "the tool returns no hunks" (there is no
"hunk <old_start> <old_count> <new_start> <new_count>" line passed
from the tool over the protocol) or "the tool returns zero-hunk"
(there is a special "zero-hunk" message to signal this particular
condition sent over the protocol), and this description does not
quite help disambiguating between the two.
If the former, then avoid "zero hunks" as it sounds like a noun with
special meaning. Yes, we can say "tool returns one hunk", "tool
returns 31 hunks", etc., so "tool returns zero hunks" may logically
be correct, but "when the tool returns no hunks with status=success"
is much less confusing, I think. |
||
| conversion outputs. See linkgit:gitattributes[5] for details. | ||
|
|
||
| `diff.<driver>.process`:: | ||
| The command to run as a long-running diff process. | ||
| The tool communicates via the pkt-line protocol and returns | ||
| hunks that are fed into Git's diff and blame pipelines. | ||
| If the tool returns zero hunks, the file is treated as | ||
| unchanged for both diff output and blame attribution. | ||
| See linkgit:gitattributes[5] for details. | ||
|
|
||
| `diff.indentHeuristic`:: | ||
| Set this option to `false` to disable the default heuristics | ||
| that shift diff hunk boundaries to make patches easier to read. | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,206 @@ | ||
| /* | ||
| * Diff process backend: communicates with a long-running external | ||
| * tool via the pkt-line protocol to obtain custom line-matching | ||
| * results. Unlike textconv, which transforms the displayed content, | ||
| * hunks from a diff process reference original line numbers and | ||
| * the display shows the actual file content. | ||
| * | ||
| * Protocol: pkt-line over stdin/stdout, following the pattern of | ||
| * the long-running filter process protocol (see convert.c). | ||
| * | ||
| * Handshake: | ||
| * git> git-diff-client / version=1 / flush | ||
| * tool< git-diff-server / version=1 / flush | ||
| * git> capability=hunks / flush | ||
| * tool< capability=hunks / flush | ||
| * | ||
| * Per-file: | ||
| * git> command=hunks / pathname=<path> / flush | ||
| * git> <old content packetized> / flush | ||
| * git> <new content packetized> / flush | ||
| * tool< hunk <old_start> <old_count> <new_start> <new_count> | ||
| * tool< ... / flush | ||
| * tool< status=success / flush | ||
| * | ||
| * Zero hunks with status=success means the tool considers the | ||
| * files equivalent. Git will skip the diff for that file. | ||
| */ | ||
|
|
||
| #include "git-compat-util.h" | ||
| #include "diff-process.h" | ||
| #include "userdiff.h" | ||
| #include "sub-process.h" | ||
| #include "pkt-line.h" | ||
| #include "strbuf.h" | ||
| #include "xdiff/xdiff.h" | ||
|
|
||
| #define CAP_HUNKS (1u << 0) | ||
|
|
||
| struct diff_subprocess { | ||
| struct subprocess_entry subprocess; | ||
| unsigned int supported_capabilities; | ||
| }; | ||
|
|
||
| static int subprocess_map_initialized; | ||
| static struct hashmap subprocess_map; | ||
|
|
||
| static int start_diff_process_fn(struct subprocess_entry *subprocess) | ||
| { | ||
| static int versions[] = { 1, 0 }; | ||
| static struct subprocess_capability capabilities[] = { | ||
| { "hunks", CAP_HUNKS }, | ||
| { NULL, 0 } | ||
| }; | ||
| struct diff_subprocess *entry = | ||
| (struct diff_subprocess *)subprocess; | ||
|
|
||
| /* Uses dying pkt-line variant, same as convert.c filters. */ | ||
| return subprocess_handshake(subprocess, "git-diff", | ||
| versions, NULL, | ||
| capabilities, | ||
| &entry->supported_capabilities); | ||
| } | ||
|
|
||
| static struct diff_subprocess *find_or_start_process(const char *cmd) | ||
| { | ||
| struct diff_subprocess *entry; | ||
|
|
||
| if (!subprocess_map_initialized) { | ||
| subprocess_map_initialized = 1; | ||
| hashmap_init(&subprocess_map, cmd2process_cmp, NULL, 0); | ||
| } | ||
|
|
||
| entry = (struct diff_subprocess *) | ||
| subprocess_find_entry(&subprocess_map, cmd); | ||
| if (entry) | ||
| return entry; | ||
|
|
||
| entry = xcalloc(1, sizeof(*entry)); | ||
| if (subprocess_start(&subprocess_map, &entry->subprocess, | ||
| cmd, start_diff_process_fn)) { | ||
| free(entry); | ||
| return NULL; | ||
| } | ||
|
|
||
| return entry; | ||
| } | ||
|
|
||
| static int send_file_content(int fd, const char *buf, long size) | ||
| { | ||
| int ret; | ||
|
|
||
| if (size > 0) | ||
| ret = write_packetized_from_buf_no_flush(buf, size, fd); | ||
| else | ||
| ret = 0; | ||
| if (ret) | ||
| return ret; | ||
| return packet_flush_gently(fd); | ||
| } | ||
|
|
||
| static int parse_hunk_line(const char *line, struct xdl_hunk *hunk) | ||
| { | ||
| char *end; | ||
|
|
||
| /* Format: "hunk <old_start> <old_count> <new_start> <new_count>" */ | ||
| if (!skip_prefix(line, "hunk ", &line)) | ||
| return -1; | ||
|
|
||
| hunk->old_start = strtol(line, &end, 10); | ||
| if (end == line || *end != ' ') | ||
| return -1; | ||
| line = end; | ||
|
|
||
| hunk->old_count = strtol(line, &end, 10); | ||
| if (end == line || *end != ' ') | ||
| return -1; | ||
| line = end; | ||
|
|
||
| hunk->new_start = strtol(line, &end, 10); | ||
| if (end == line || *end != ' ') | ||
| return -1; | ||
| line = end; | ||
|
|
||
| hunk->new_count = strtol(line, &end, 10); | ||
| if (end == line || *end != '\0') | ||
| return -1; | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| int diff_process_get_hunks(struct userdiff_driver *drv, | ||
| const char *path, | ||
| const char *old_buf, long old_size, | ||
| const char *new_buf, long new_size, | ||
| struct xdl_hunk **hunks_out, | ||
| size_t *nr_hunks_out) | ||
| { | ||
| struct diff_subprocess *backend; | ||
| struct child_process *process; | ||
| int fd_in, fd_out; | ||
| struct strbuf status = STRBUF_INIT; | ||
| struct xdl_hunk *hunks = NULL; | ||
| struct xdl_hunk hunk; | ||
| size_t nr_hunks = 0, alloc_hunks = 0; | ||
| int len; | ||
| char *line; | ||
|
|
||
| if (!drv || !drv->process) | ||
| return -1; | ||
|
|
||
| backend = find_or_start_process(drv->process); | ||
| if (!backend) | ||
| return -1; | ||
|
|
||
| if (!(backend->supported_capabilities & CAP_HUNKS)) | ||
| return -1; | ||
|
|
||
| process = subprocess_get_child_process(&backend->subprocess); | ||
| fd_in = process->in; | ||
| fd_out = process->out; | ||
|
|
||
| /* Send request */ | ||
| if (packet_write_fmt_gently(fd_in, "command=hunks\n") || | ||
| packet_write_fmt_gently(fd_in, "pathname=%s\n", path) || | ||
| packet_flush_gently(fd_in)) | ||
| goto error; | ||
|
|
||
| /* Send old file content */ | ||
| if (send_file_content(fd_in, old_buf, old_size)) | ||
| goto error; | ||
|
|
||
| /* Send new file content */ | ||
| if (send_file_content(fd_in, new_buf, new_size)) | ||
| goto error; | ||
|
|
||
| /* Read hunks until flush packet */ | ||
| while ((len = packet_read_line_gently(fd_out, NULL, &line)) >= 0 && | ||
| line) { | ||
| if (parse_hunk_line(line, &hunk) < 0) | ||
| goto error; | ||
| ALLOC_GROW(hunks, nr_hunks + 1, alloc_hunks); | ||
| hunks[nr_hunks++] = hunk; | ||
| } | ||
| if (len < 0) | ||
| goto error; | ||
|
|
||
| /* Read status */ | ||
| if (subprocess_read_status(fd_out, &status)) | ||
| goto error; | ||
|
|
||
| if (strcmp(status.buf, "success")) { | ||
| if (!strcmp(status.buf, "abort")) | ||
| backend->supported_capabilities &= ~CAP_HUNKS; | ||
| goto error; | ||
| } | ||
|
|
||
| *hunks_out = hunks; | ||
| *nr_hunks_out = nr_hunks; | ||
| strbuf_release(&status); | ||
| return 0; | ||
|
|
||
| error: | ||
| free(hunks); | ||
| strbuf_release(&status); | ||
| return -1; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| #ifndef DIFF_PROCESS_H | ||
| #define DIFF_PROCESS_H | ||
|
|
||
| struct userdiff_driver; | ||
| struct xdl_hunk; | ||
|
|
||
| /* | ||
| * Query a diff process for hunks describing the changes | ||
| * between old_buf and new_buf. | ||
| * | ||
| * The backend is a long-running subprocess configured via | ||
| * diff.<driver>.process. It receives file content via | ||
| * pkt-line and returns hunks with 1-based line numbers. | ||
| * | ||
| * On success, sets *hunks_out and *nr_hunks_out to a newly allocated | ||
| * array (caller must free) and returns 0. | ||
| * | ||
| * On failure, returns -1. The caller should fall back to the | ||
| * builtin diff algorithm. | ||
| */ | ||
| int diff_process_get_hunks(struct userdiff_driver *drv, | ||
| const char *path, | ||
| const char *old_buf, long old_size, | ||
| const char *new_buf, long new_size, | ||
| struct xdl_hunk **hunks_out, | ||
| size_t *nr_hunks_out); | ||
|
|
||
| #endif /* DIFF_PROCESS_H */ |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Junio C Hamano wrote on the Git mailing list (how to reply to this email):