Skip to content
/ git Public
forked from git/git

Commit cf15095

Browse files
jltoblergitster
authored andcommitted
builtin/diff-pairs: allow explicit diff queue flush
The diffs queued from git-diff-pairs(1) are flushed when stdin is closed. To enable greater flexibility, allow control over when the diff queue is flushed by writing a single NUL byte on stdin between input file pairs. Diff output between flushes is separated by a single NUL byte. Signed-off-by: Justin Tobler <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5bd10b2 commit cf15095

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

Documentation/git-diff-pairs.adoc

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ in the NUL-terminated raw output format as generated by commands such as `git
1717
diff-tree -z -r --raw`. By default, the outputted diffs are computed and shown
1818
in the patch format when stdin closes.
1919

20+
A single NUL byte may be written to stdin between raw input lines to compute
21+
file pair diffs up to that point instead of waiting for stdin to close. A NUL
22+
byte is also written to the output to delimit between these batches of diffs.
23+
2024
Usage of this command enables the traditional diff pipeline to be broken up
2125
into separate stages where `diff-pairs` acts as the output phase. Other
2226
commands, such as `diff-tree`, may serve as a frontend to compute the raw

builtin/diff-pairs.c

+14
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ int cmd_diff_pairs(int argc, const char **argv, const char *prefix,
5757
show_usage_with_options_if_asked(argc, argv, builtin_diff_pairs_usage, parseopts);
5858

5959
repo_config(repo, git_diff_basic_config, NULL);
60+
revs.diffopt.no_free = 1;
6061
revs.disable_stdin = 1;
6162
revs.abbrev = 0;
6263
revs.diff = 1;
@@ -106,6 +107,18 @@ int cmd_diff_pairs(int argc, const char **argv, const char *prefix,
106107
break;
107108

108109
p = meta.buf;
110+
if (!*p) {
111+
diffcore_std(&revs.diffopt);
112+
diff_flush(&revs.diffopt);
113+
/*
114+
* When the diff queue is explicitly flushed, append a
115+
* NUL byte to separate batches of diffs.
116+
*/
117+
fputc('\0', revs.diffopt.file);
118+
fflush(revs.diffopt.file);
119+
continue;
120+
}
121+
109122
if (*p != ':')
110123
die(_("invalid raw diff input"));
111124
p++;
@@ -179,6 +192,7 @@ int cmd_diff_pairs(int argc, const char **argv, const char *prefix,
179192
}
180193
}
181194

195+
revs.diffopt.no_free = 0;
182196
diffcore_std(&revs.diffopt);
183197
diff_flush(&revs.diffopt);
184198
ret = diff_result_code(&revs);

t/t4070-diff-pairs.sh

+9
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,13 @@ test_expect_success 'diff-pairs does not support pathspec arguments' '
7878
test_cmp expect err
7979
'
8080

81+
test_expect_success 'diff-pairs explicit queue flush' '
82+
git diff-tree -r -M -C -C -z base new >expect &&
83+
printf "\0" >>expect &&
84+
git diff-tree -r -M -C -C -z base new >>expect &&
85+
86+
git diff-pairs --raw -z <expect >actual &&
87+
test_cmp expect actual
88+
'
89+
8190
test_done

0 commit comments

Comments
 (0)