]> git.ipfire.org Git - thirdparty/git.git/blame - add-patch.c
read-cache*.h: move declarations for read-cache.c functions from cache.h
[thirdparty/git.git] / add-patch.c
CommitLineData
f6aa7ecc
JS
1#include "cache.h"
2#include "add-interactive.h"
6c6ddf92 3#include "advice.h"
36bf1958 4#include "alloc.h"
4e120823 5#include "editor.h"
7ee24e18 6#include "environment.h"
f394e093 7#include "gettext.h"
dabab1d6 8#include "object-name.h"
08c46a49 9#include "read-cache-ll.h"
f6aa7ecc
JS
10#include "strbuf.h"
11#include "run-command.h"
dbbcd44f 12#include "strvec.h"
f6aa7ecc 13#include "pathspec.h"
e3bd11b4 14#include "color.h"
25ea47af 15#include "diff.h"
04f816b1 16#include "compat/terminal.h"
08d383f2 17#include "prompt.h"
25ea47af 18
0ecd9d27 19enum prompt_mode_type {
2c8bd847 20 PROMPT_MODE_CHANGE = 0, PROMPT_DELETION, PROMPT_ADDITION, PROMPT_HUNK,
d2a233cb 21 PROMPT_MODE_MAX, /* must be last */
0ecd9d27
JS
22};
23
d2a233cb
JS
24struct patch_mode {
25 /*
26 * The magic constant 4 is chosen such that all patch modes
27 * provide enough space for three command-line arguments followed by a
28 * trailing `NULL`.
29 */
30 const char *diff_cmd[4], *apply_args[4], *apply_check_args[4];
36bae1dc 31 unsigned is_reverse:1, index_only:1, apply_for_checkout:1;
d2a233cb
JS
32 const char *prompt_mode[PROMPT_MODE_MAX];
33 const char *edit_hunk_hint, *help_patch_text;
34};
35
36static struct patch_mode patch_mode_add = {
37 .diff_cmd = { "diff-files", NULL },
38 .apply_args = { "--cached", NULL },
39 .apply_check_args = { "--cached", NULL },
40 .prompt_mode = {
41 N_("Stage mode change [y,n,q,a,d%s,?]? "),
42 N_("Stage deletion [y,n,q,a,d%s,?]? "),
2c8bd847 43 N_("Stage addition [y,n,q,a,d%s,?]? "),
d2a233cb
JS
44 N_("Stage this hunk [y,n,q,a,d%s,?]? ")
45 },
46 .edit_hunk_hint = N_("If the patch applies cleanly, the edited hunk "
47 "will immediately be marked for staging."),
48 .help_patch_text =
49 N_("y - stage this hunk\n"
50 "n - do not stage this hunk\n"
51 "q - quit; do not stage this hunk or any of the remaining "
52 "ones\n"
53 "a - stage this hunk and all later hunks in the file\n"
54 "d - do not stage this hunk or any of the later hunks in "
55 "the file\n")
0ecd9d27
JS
56};
57
36bae1dc
JS
58static struct patch_mode patch_mode_stash = {
59 .diff_cmd = { "diff-index", "HEAD", NULL },
60 .apply_args = { "--cached", NULL },
61 .apply_check_args = { "--cached", NULL },
62 .prompt_mode = {
63 N_("Stash mode change [y,n,q,a,d%s,?]? "),
64 N_("Stash deletion [y,n,q,a,d%s,?]? "),
2c8bd847 65 N_("Stash addition [y,n,q,a,d%s,?]? "),
36bae1dc
JS
66 N_("Stash this hunk [y,n,q,a,d%s,?]? "),
67 },
68 .edit_hunk_hint = N_("If the patch applies cleanly, the edited hunk "
69 "will immediately be marked for stashing."),
70 .help_patch_text =
71 N_("y - stash this hunk\n"
72 "n - do not stash this hunk\n"
73 "q - quit; do not stash this hunk or any of the remaining "
74 "ones\n"
75 "a - stash this hunk and all later hunks in the file\n"
76 "d - do not stash this hunk or any of the later hunks in "
77 "the file\n"),
78};
79
80static struct patch_mode patch_mode_reset_head = {
81 .diff_cmd = { "diff-index", "--cached", NULL },
82 .apply_args = { "-R", "--cached", NULL },
83 .apply_check_args = { "-R", "--cached", NULL },
84 .is_reverse = 1,
85 .index_only = 1,
86 .prompt_mode = {
87 N_("Unstage mode change [y,n,q,a,d%s,?]? "),
88 N_("Unstage deletion [y,n,q,a,d%s,?]? "),
2c8bd847 89 N_("Unstage addition [y,n,q,a,d%s,?]? "),
36bae1dc
JS
90 N_("Unstage this hunk [y,n,q,a,d%s,?]? "),
91 },
92 .edit_hunk_hint = N_("If the patch applies cleanly, the edited hunk "
93 "will immediately be marked for unstaging."),
94 .help_patch_text =
95 N_("y - unstage this hunk\n"
96 "n - do not unstage this hunk\n"
97 "q - quit; do not unstage this hunk or any of the remaining "
98 "ones\n"
99 "a - unstage this hunk and all later hunks in the file\n"
100 "d - do not unstage this hunk or any of the later hunks in "
101 "the file\n"),
102};
103
104static struct patch_mode patch_mode_reset_nothead = {
105 .diff_cmd = { "diff-index", "-R", "--cached", NULL },
106 .apply_args = { "--cached", NULL },
107 .apply_check_args = { "--cached", NULL },
108 .index_only = 1,
109 .prompt_mode = {
110 N_("Apply mode change to index [y,n,q,a,d%s,?]? "),
111 N_("Apply deletion to index [y,n,q,a,d%s,?]? "),
2c8bd847 112 N_("Apply addition to index [y,n,q,a,d%s,?]? "),
36bae1dc
JS
113 N_("Apply this hunk to index [y,n,q,a,d%s,?]? "),
114 },
115 .edit_hunk_hint = N_("If the patch applies cleanly, the edited hunk "
116 "will immediately be marked for applying."),
117 .help_patch_text =
118 N_("y - apply this hunk to index\n"
119 "n - do not apply this hunk to index\n"
120 "q - quit; do not apply this hunk or any of the remaining "
121 "ones\n"
122 "a - apply this hunk and all later hunks in the file\n"
123 "d - do not apply this hunk or any of the later hunks in "
124 "the file\n"),
125};
126
52628f94
JS
127static struct patch_mode patch_mode_checkout_index = {
128 .diff_cmd = { "diff-files", NULL },
129 .apply_args = { "-R", NULL },
130 .apply_check_args = { "-R", NULL },
131 .is_reverse = 1,
132 .prompt_mode = {
133 N_("Discard mode change from worktree [y,n,q,a,d%s,?]? "),
134 N_("Discard deletion from worktree [y,n,q,a,d%s,?]? "),
2c8bd847 135 N_("Discard addition from worktree [y,n,q,a,d%s,?]? "),
52628f94
JS
136 N_("Discard this hunk from worktree [y,n,q,a,d%s,?]? "),
137 },
138 .edit_hunk_hint = N_("If the patch applies cleanly, the edited hunk "
139 "will immediately be marked for discarding."),
140 .help_patch_text =
141 N_("y - discard this hunk from worktree\n"
142 "n - do not discard this hunk from worktree\n"
143 "q - quit; do not discard this hunk or any of the remaining "
144 "ones\n"
145 "a - discard this hunk and all later hunks in the file\n"
146 "d - do not discard this hunk or any of the later hunks in "
147 "the file\n"),
148};
149
150static struct patch_mode patch_mode_checkout_head = {
151 .diff_cmd = { "diff-index", NULL },
152 .apply_for_checkout = 1,
153 .apply_check_args = { "-R", NULL },
154 .is_reverse = 1,
155 .prompt_mode = {
156 N_("Discard mode change from index and worktree [y,n,q,a,d%s,?]? "),
157 N_("Discard deletion from index and worktree [y,n,q,a,d%s,?]? "),
2c8bd847 158 N_("Discard addition from index and worktree [y,n,q,a,d%s,?]? "),
52628f94
JS
159 N_("Discard this hunk from index and worktree [y,n,q,a,d%s,?]? "),
160 },
161 .edit_hunk_hint = N_("If the patch applies cleanly, the edited hunk "
162 "will immediately be marked for discarding."),
163 .help_patch_text =
164 N_("y - discard this hunk from index and worktree\n"
165 "n - do not discard this hunk from index and worktree\n"
166 "q - quit; do not discard this hunk or any of the remaining "
167 "ones\n"
168 "a - discard this hunk and all later hunks in the file\n"
169 "d - do not discard this hunk or any of the later hunks in "
170 "the file\n"),
171};
172
173static struct patch_mode patch_mode_checkout_nothead = {
174 .diff_cmd = { "diff-index", "-R", NULL },
175 .apply_for_checkout = 1,
176 .apply_check_args = { NULL },
177 .prompt_mode = {
178 N_("Apply mode change to index and worktree [y,n,q,a,d%s,?]? "),
179 N_("Apply deletion to index and worktree [y,n,q,a,d%s,?]? "),
2c8bd847 180 N_("Apply addition to index and worktree [y,n,q,a,d%s,?]? "),
52628f94
JS
181 N_("Apply this hunk to index and worktree [y,n,q,a,d%s,?]? "),
182 },
183 .edit_hunk_hint = N_("If the patch applies cleanly, the edited hunk "
184 "will immediately be marked for applying."),
185 .help_patch_text =
186 N_("y - apply this hunk to index and worktree\n"
187 "n - do not apply this hunk to index and worktree\n"
188 "q - quit; do not apply this hunk or any of the remaining "
189 "ones\n"
190 "a - apply this hunk and all later hunks in the file\n"
191 "d - do not apply this hunk or any of the later hunks in "
192 "the file\n"),
193};
194
cee6cb73
JS
195static struct patch_mode patch_mode_worktree_head = {
196 .diff_cmd = { "diff-index", NULL },
197 .apply_args = { "-R", NULL },
198 .apply_check_args = { "-R", NULL },
199 .is_reverse = 1,
200 .prompt_mode = {
f6f0ee24
RS
201 N_("Discard mode change from worktree [y,n,q,a,d%s,?]? "),
202 N_("Discard deletion from worktree [y,n,q,a,d%s,?]? "),
203 N_("Discard addition from worktree [y,n,q,a,d%s,?]? "),
204 N_("Discard this hunk from worktree [y,n,q,a,d%s,?]? "),
cee6cb73
JS
205 },
206 .edit_hunk_hint = N_("If the patch applies cleanly, the edited hunk "
207 "will immediately be marked for discarding."),
208 .help_patch_text =
209 N_("y - discard this hunk from worktree\n"
210 "n - do not discard this hunk from worktree\n"
211 "q - quit; do not discard this hunk or any of the remaining "
212 "ones\n"
213 "a - discard this hunk and all later hunks in the file\n"
214 "d - do not discard this hunk or any of the later hunks in "
215 "the file\n"),
216};
217
218static struct patch_mode patch_mode_worktree_nothead = {
219 .diff_cmd = { "diff-index", "-R", NULL },
220 .apply_args = { NULL },
221 .apply_check_args = { NULL },
222 .prompt_mode = {
f6f0ee24
RS
223 N_("Apply mode change to worktree [y,n,q,a,d%s,?]? "),
224 N_("Apply deletion to worktree [y,n,q,a,d%s,?]? "),
225 N_("Apply addition to worktree [y,n,q,a,d%s,?]? "),
226 N_("Apply this hunk to worktree [y,n,q,a,d%s,?]? "),
cee6cb73
JS
227 },
228 .edit_hunk_hint = N_("If the patch applies cleanly, the edited hunk "
229 "will immediately be marked for applying."),
230 .help_patch_text =
231 N_("y - apply this hunk to worktree\n"
232 "n - do not apply this hunk to worktree\n"
233 "q - quit; do not apply this hunk or any of the remaining "
234 "ones\n"
235 "a - apply this hunk and all later hunks in the file\n"
236 "d - do not apply this hunk or any of the later hunks in "
237 "the file\n"),
238};
239
25ea47af
JS
240struct hunk_header {
241 unsigned long old_offset, old_count, new_offset, new_count;
242 /*
243 * Start/end offsets to the extra text after the second `@@` in the
244 * hunk header, e.g. the function signature. This is expected to
245 * include the newline.
246 */
247 size_t extra_start, extra_end, colored_extra_start, colored_extra_end;
fd3f7f61 248 unsigned suppress_colored_line_range:1;
25ea47af 249};
f6aa7ecc
JS
250
251struct hunk {
510aeca1 252 size_t start, end, colored_start, colored_end, splittable_into;
bcdd297b 253 ssize_t delta;
f6aa7ecc 254 enum { UNDECIDED_HUNK = 0, SKIP_HUNK, USE_HUNK } use;
25ea47af 255 struct hunk_header header;
f6aa7ecc
JS
256};
257
258struct add_p_state {
25ea47af 259 struct add_i_state s;
f6aa7ecc
JS
260 struct strbuf answer, buf;
261
262 /* parsed diff */
e3bd11b4 263 struct strbuf plain, colored;
80399aec
JS
264 struct file_diff {
265 struct hunk head;
266 struct hunk *hunk;
267 size_t hunk_nr, hunk_alloc;
2c8bd847 268 unsigned deleted:1, added:1, mode_change:1,binary:1;
80399aec
JS
269 } *file_diff;
270 size_t file_diff_nr;
d2a233cb
JS
271
272 /* patch mode */
273 struct patch_mode *mode;
274 const char *revision;
f6aa7ecc
JS
275};
276
324efcf6
PW
277static void add_p_state_clear(struct add_p_state *s)
278{
279 size_t i;
280
281 strbuf_release(&s->answer);
282 strbuf_release(&s->buf);
283 strbuf_release(&s->plain);
284 strbuf_release(&s->colored);
285 for (i = 0; i < s->file_diff_nr; i++)
286 free(s->file_diff[i].hunk);
287 free(s->file_diff);
288 clear_add_i_state(&s->s);
289}
290
48ca53ca 291__attribute__((format (printf, 2, 3)))
7584dd3c
JS
292static void err(struct add_p_state *s, const char *fmt, ...)
293{
294 va_list args;
295
296 va_start(args, fmt);
297 fputs(s->s.error_color, stderr);
298 vfprintf(stderr, fmt, args);
299 fputs(s->s.reset_color, stderr);
300 fputc('\n', stderr);
301 va_end(args);
302}
303
f6aa7ecc
JS
304static void setup_child_process(struct add_p_state *s,
305 struct child_process *cp, ...)
306{
307 va_list ap;
308 const char *arg;
309
310 va_start(ap, cp);
311 while ((arg = va_arg(ap, const char *)))
ef8d7ac4 312 strvec_push(&cp->args, arg);
f6aa7ecc
JS
313 va_end(ap);
314
315 cp->git_cmd = 1;
29fda24d 316 strvec_pushf(&cp->env,
f6d8942b 317 INDEX_ENVIRONMENT "=%s", s->s.r->index_file);
25ea47af
JS
318}
319
320static int parse_range(const char **p,
321 unsigned long *offset, unsigned long *count)
322{
323 char *pend;
324
325 *offset = strtoul(*p, &pend, 10);
326 if (pend == *p)
327 return -1;
328 if (*pend != ',') {
329 *count = 1;
330 *p = pend;
331 return 0;
332 }
333 *count = strtoul(pend + 1, (char **)p, 10);
334 return *p == pend + 1 ? -1 : 0;
335}
336
337static int parse_hunk_header(struct add_p_state *s, struct hunk *hunk)
338{
339 struct hunk_header *header = &hunk->header;
340 const char *line = s->plain.buf + hunk->start, *p = line;
341 char *eol = memchr(p, '\n', s->plain.len - hunk->start);
342
343 if (!eol)
344 eol = s->plain.buf + s->plain.len;
345
346 if (!skip_prefix(p, "@@ -", &p) ||
347 parse_range(&p, &header->old_offset, &header->old_count) < 0 ||
348 !skip_prefix(p, " +", &p) ||
349 parse_range(&p, &header->new_offset, &header->new_count) < 0 ||
350 !skip_prefix(p, " @@", &p))
351 return error(_("could not parse hunk header '%.*s'"),
352 (int)(eol - line), line);
353
354 hunk->start = eol - s->plain.buf + (*eol == '\n');
355 header->extra_start = p - s->plain.buf;
356 header->extra_end = hunk->start;
357
358 if (!s->colored.len) {
359 header->colored_extra_start = header->colored_extra_end = 0;
360 return 0;
361 }
362
363 /* Now find the extra text in the colored diff */
364 line = s->colored.buf + hunk->colored_start;
365 eol = memchr(line, '\n', s->colored.len - hunk->colored_start);
366 if (!eol)
367 eol = s->colored.buf + s->colored.len;
368 p = memmem(line, eol - line, "@@ -", 4);
fd3f7f61
JS
369 if (p && (p = memmem(p + 4, eol - p - 4, " @@", 3))) {
370 header->colored_extra_start = p + 3 - s->colored.buf;
371 } else {
372 /* could not parse colored hunk header, leave as-is */
373 header->colored_extra_start = hunk->colored_start;
374 header->suppress_colored_line_range = 1;
375 }
25ea47af 376 hunk->colored_start = eol - s->colored.buf + (*eol == '\n');
25ea47af
JS
377 header->colored_extra_end = hunk->colored_start;
378
379 return 0;
f6aa7ecc
JS
380}
381
5906d5de
JS
382static int is_octal(const char *p, size_t len)
383{
384 if (!len)
385 return 0;
386
387 while (len--)
388 if (*p < '0' || *(p++) > '7')
389 return 0;
390 return 1;
391}
392
7008ddc6
PW
393static void complete_file(char marker, struct hunk *hunk)
394{
395 if (marker == '-' || marker == '+')
396 /*
397 * Last hunk ended in non-context line (i.e. it
398 * appended lines to the file, so there are no
399 * trailing context lines).
400 */
401 hunk->splittable_into++;
402}
403
f6aa7ecc
JS
404static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
405{
ef8d7ac4 406 struct strvec args = STRVEC_INIT;
08b1ea4c 407 const char *diff_algorithm = s->s.interactive_diff_algorithm;
e3bd11b4 408 struct strbuf *plain = &s->plain, *colored = NULL;
f6aa7ecc 409 struct child_process cp = CHILD_PROCESS_INIT;
510aeca1 410 char *p, *pend, *colored_p = NULL, *colored_pend = NULL, marker = '\0';
80399aec
JS
411 size_t file_diff_alloc = 0, i, color_arg_index;
412 struct file_diff *file_diff = NULL;
f6aa7ecc
JS
413 struct hunk *hunk = NULL;
414 int res;
415
ef8d7ac4 416 strvec_pushv(&args, s->mode->diff_cmd);
08b1ea4c 417 if (diff_algorithm)
ef8d7ac4 418 strvec_pushf(&args, "--diff-algorithm=%s", diff_algorithm);
d2a233cb
JS
419 if (s->revision) {
420 struct object_id oid;
ef8d7ac4 421 strvec_push(&args,
f6d8942b
JK
422 /* could be on an unborn branch */
423 !strcmp("HEAD", s->revision) &&
d850b7a5 424 repo_get_oid(the_repository, "HEAD", &oid) ?
f6d8942b 425 empty_tree_oid_hex() : s->revision);
d2a233cb 426 }
d70a9eb6 427 color_arg_index = args.nr;
f6aa7ecc 428 /* Use `--no-color` explicitly, just in case `diff.color = always`. */
0a101676
JS
429 strvec_pushl(&args, "--no-color", "--ignore-submodules=dirty", "-p",
430 "--", NULL);
f6aa7ecc 431 for (i = 0; i < ps->nr; i++)
ef8d7ac4 432 strvec_push(&args, ps->items[i].original);
f6aa7ecc 433
e3bd11b4 434 setup_child_process(s, &cp, NULL);
6def0ff8 435 strvec_pushv(&cp.args, args.v);
f6aa7ecc 436 res = capture_command(&cp, plain, 0);
e3bd11b4 437 if (res) {
ef8d7ac4 438 strvec_clear(&args);
f6aa7ecc 439 return error(_("could not parse diff"));
e3bd11b4
JS
440 }
441 if (!plain->len) {
ef8d7ac4 442 strvec_clear(&args);
f6aa7ecc 443 return 0;
e3bd11b4 444 }
f6aa7ecc
JS
445 strbuf_complete_line(plain);
446
e3bd11b4
JS
447 if (want_color_fd(1, -1)) {
448 struct child_process colored_cp = CHILD_PROCESS_INIT;
180f48df 449 const char *diff_filter = s->s.interactive_diff_filter;
e3bd11b4
JS
450
451 setup_child_process(s, &colored_cp, NULL);
d70a9eb6 452 xsnprintf((char *)args.v[color_arg_index], 8, "--color");
6def0ff8 453 strvec_pushv(&colored_cp.args, args.v);
e3bd11b4
JS
454 colored = &s->colored;
455 res = capture_command(&colored_cp, colored, 0);
ef8d7ac4 456 strvec_clear(&args);
e3bd11b4
JS
457 if (res)
458 return error(_("could not parse colored diff"));
180f48df
JS
459
460 if (diff_filter) {
461 struct child_process filter_cp = CHILD_PROCESS_INIT;
462
463 setup_child_process(s, &filter_cp,
464 diff_filter, NULL);
465 filter_cp.git_cmd = 0;
466 filter_cp.use_shell = 1;
467 strbuf_reset(&s->buf);
468 if (pipe_command(&filter_cp,
469 colored->buf, colored->len,
470 &s->buf, colored->len,
471 NULL, 0) < 0)
472 return error(_("failed to run '%s'"),
473 diff_filter);
474 strbuf_swap(colored, &s->buf);
475 }
476
e3bd11b4
JS
477 strbuf_complete_line(colored);
478 colored_p = colored->buf;
479 colored_pend = colored_p + colored->len;
480 }
ef8d7ac4 481 strvec_clear(&args);
e3bd11b4 482
80399aec 483 /* parse files and hunks */
f6aa7ecc
JS
484 p = plain->buf;
485 pend = p + plain->len;
486 while (p != pend) {
487 char *eol = memchr(p, '\n', pend - p);
75a009dc 488 const char *deleted = NULL, *mode_change = NULL;
47dc4fd5 489
f6aa7ecc
JS
490 if (!eol)
491 eol = pend;
492
28d1122f
JK
493 if (starts_with(p, "diff ") ||
494 starts_with(p, "* Unmerged path ")) {
7008ddc6 495 complete_file(marker, hunk);
2ebe436c 496 ALLOC_GROW_BY(s->file_diff, s->file_diff_nr, 1,
80399aec
JS
497 file_diff_alloc);
498 file_diff = s->file_diff + s->file_diff_nr - 1;
80399aec
JS
499 hunk = &file_diff->head;
500 hunk->start = p - plain->buf;
501 if (colored_p)
502 hunk->colored_start = colored_p - colored->buf;
510aeca1 503 marker = '\0';
f6aa7ecc
JS
504 } else if (p == plain->buf)
505 BUG("diff starts with unexpected line:\n"
506 "%.*s\n", (int)(eol - p), p);
75a009dc 507 else if (file_diff->deleted)
47dc4fd5
JS
508 ; /* keep the rest of the file in a single "hunk" */
509 else if (starts_with(p, "@@ ") ||
510 (hunk == &file_diff->head &&
75a009dc 511 (skip_prefix(p, "deleted file", &deleted)))) {
510aeca1
JS
512 if (marker == '-' || marker == '+')
513 /*
514 * Should not happen; previous hunk did not end
515 * in a context line? Handle it anyway.
516 */
517 hunk->splittable_into++;
518
2ebe436c 519 ALLOC_GROW_BY(file_diff->hunk, file_diff->hunk_nr, 1,
80399aec
JS
520 file_diff->hunk_alloc);
521 hunk = file_diff->hunk + file_diff->hunk_nr - 1;
f6aa7ecc
JS
522
523 hunk->start = p - plain->buf;
e3bd11b4
JS
524 if (colored)
525 hunk->colored_start = colored_p - colored->buf;
25ea47af 526
47dc4fd5
JS
527 if (deleted)
528 file_diff->deleted = 1;
529 else if (parse_hunk_header(s, hunk) < 0)
25ea47af 530 return -1;
510aeca1
JS
531
532 /*
533 * Start counting into how many hunks this one can be
534 * split
535 */
536 marker = *p;
75a009dc
PW
537 } else if (hunk == &file_diff->head &&
538 starts_with(p, "new file")) {
539 file_diff->added = 1;
5906d5de
JS
540 } else if (hunk == &file_diff->head &&
541 skip_prefix(p, "old mode ", &mode_change) &&
542 is_octal(mode_change, eol - mode_change)) {
543 if (file_diff->mode_change)
544 BUG("double mode change?\n\n%.*s",
545 (int)(eol - plain->buf), plain->buf);
2ebe436c 546 if (file_diff->hunk_nr)
5906d5de
JS
547 BUG("mode change in the middle?\n\n%.*s",
548 (int)(eol - plain->buf), plain->buf);
549
550 /*
551 * Do *not* change `hunk`: the mode change pseudo-hunk
552 * is _part of_ the header "hunk".
553 */
554 file_diff->mode_change = 1;
2ebe436c 555 ALLOC_GROW_BY(file_diff->hunk, file_diff->hunk_nr, 1,
5906d5de 556 file_diff->hunk_alloc);
5906d5de
JS
557 file_diff->hunk->start = p - plain->buf;
558 if (colored_p)
559 file_diff->hunk->colored_start =
560 colored_p - colored->buf;
561 } else if (hunk == &file_diff->head &&
562 skip_prefix(p, "new mode ", &mode_change) &&
563 is_octal(mode_change, eol - mode_change)) {
564
565 /*
566 * Extend the "mode change" pseudo-hunk to include also
567 * the "new mode" line.
568 */
569 if (!file_diff->mode_change)
570 BUG("'new mode' without 'old mode'?\n\n%.*s",
571 (int)(eol - plain->buf), plain->buf);
572 if (file_diff->hunk_nr != 1)
573 BUG("mode change in the middle?\n\n%.*s",
574 (int)(eol - plain->buf), plain->buf);
575 if (p - plain->buf != file_diff->hunk->end)
576 BUG("'new mode' does not immediately follow "
577 "'old mode'?\n\n%.*s",
578 (int)(eol - plain->buf), plain->buf);
2e408319
JS
579 } else if (hunk == &file_diff->head &&
580 starts_with(p, "Binary files "))
581 file_diff->binary = 1;
f6aa7ecc 582
2c8bd847
JS
583 if (!!file_diff->deleted + !!file_diff->added +
584 !!file_diff->mode_change > 1)
585 BUG("diff can only contain delete *or* add *or* a "
586 "mode change?!?\n%.*s",
5906d5de
JS
587 (int)(eol - (plain->buf + file_diff->head.start)),
588 plain->buf + file_diff->head.start);
589
510aeca1
JS
590 if ((marker == '-' || marker == '+') && *p == ' ')
591 hunk->splittable_into++;
592 if (marker && *p != '\\')
593 marker = *p;
594
f6aa7ecc
JS
595 p = eol == pend ? pend : eol + 1;
596 hunk->end = p - plain->buf;
e3bd11b4
JS
597
598 if (colored) {
599 char *colored_eol = memchr(colored_p, '\n',
600 colored_pend - colored_p);
601 if (colored_eol)
602 colored_p = colored_eol + 1;
180f48df 603 else if (p != pend)
b6633a00
JS
604 /* non-colored has more lines? */
605 goto mismatched_output;
606 else if (colored_p == colored_pend)
607 /* last line has no matching colored one? */
180f48df 608 goto mismatched_output;
e3bd11b4
JS
609 else
610 colored_p = colored_pend;
611
612 hunk->colored_end = colored_p - colored->buf;
613 }
5906d5de
JS
614
615 if (mode_change) {
616 if (file_diff->hunk_nr != 1)
617 BUG("mode change in hunk #%d???",
618 (int)file_diff->hunk_nr);
619 /* Adjust the end of the "mode change" pseudo-hunk */
620 file_diff->hunk->end = hunk->end;
621 if (colored)
622 file_diff->hunk->colored_end = hunk->colored_end;
623 }
f6aa7ecc 624 }
7008ddc6 625 complete_file(marker, hunk);
510aeca1 626
180f48df
JS
627 /* non-colored shorter than colored? */
628 if (colored_p != colored_pend) {
629mismatched_output:
630 error(_("mismatched output from interactive.diffFilter"));
631 advise(_("Your filter must maintain a one-to-one correspondence\n"
632 "between its input and output lines."));
633 return -1;
634 }
635
f6aa7ecc
JS
636 return 0;
637}
638
510aeca1
JS
639static size_t find_next_line(struct strbuf *sb, size_t offset)
640{
641 char *eol;
642
643 if (offset >= sb->len)
644 BUG("looking for next line beyond buffer (%d >= %d)\n%s",
645 (int)offset, (int)sb->len, sb->buf);
646
647 eol = memchr(sb->buf + offset, '\n', sb->len - offset);
648 if (!eol)
649 return sb->len;
650 return eol - sb->buf + 1;
651}
652
f6aa7ecc 653static void render_hunk(struct add_p_state *s, struct hunk *hunk,
25ea47af 654 ssize_t delta, int colored, struct strbuf *out)
f6aa7ecc 655{
25ea47af
JS
656 struct hunk_header *header = &hunk->header;
657
658 if (hunk->header.old_offset != 0 || hunk->header.new_offset != 0) {
659 /*
660 * Generate the hunk header dynamically, except for special
661 * hunks (such as the diff header).
662 */
663 const char *p;
664 size_t len;
665 unsigned long old_offset = header->old_offset;
666 unsigned long new_offset = header->new_offset;
667
668 if (!colored) {
669 p = s->plain.buf + header->extra_start;
670 len = header->extra_end - header->extra_start;
fd3f7f61
JS
671 } else if (header->suppress_colored_line_range) {
672 strbuf_add(out,
673 s->colored.buf + header->colored_extra_start,
674 header->colored_extra_end -
675 header->colored_extra_start);
676
677 strbuf_add(out, s->colored.buf + hunk->colored_start,
678 hunk->colored_end - hunk->colored_start);
679 return;
25ea47af
JS
680 } else {
681 strbuf_addstr(out, s->s.fraginfo_color);
682 p = s->colored.buf + header->colored_extra_start;
683 len = header->colored_extra_end
684 - header->colored_extra_start;
685 }
686
d2a233cb
JS
687 if (s->mode->is_reverse)
688 old_offset -= delta;
689 else
690 new_offset += delta;
25ea47af 691
decc9ee4
JS
692 strbuf_addf(out, "@@ -%lu", old_offset);
693 if (header->old_count != 1)
694 strbuf_addf(out, ",%lu", header->old_count);
695 strbuf_addf(out, " +%lu", new_offset);
696 if (header->new_count != 1)
697 strbuf_addf(out, ",%lu", header->new_count);
698 strbuf_addstr(out, " @@");
699
25ea47af
JS
700 if (len)
701 strbuf_add(out, p, len);
702 else if (colored)
6f1a5caa 703 strbuf_addf(out, "%s\n", s->s.reset_color);
25ea47af
JS
704 else
705 strbuf_addch(out, '\n');
706 }
707
e3bd11b4
JS
708 if (colored)
709 strbuf_add(out, s->colored.buf + hunk->colored_start,
710 hunk->colored_end - hunk->colored_start);
711 else
712 strbuf_add(out, s->plain.buf + hunk->start,
713 hunk->end - hunk->start);
f6aa7ecc
JS
714}
715
5906d5de
JS
716static void render_diff_header(struct add_p_state *s,
717 struct file_diff *file_diff, int colored,
718 struct strbuf *out)
719{
720 /*
721 * If there was a mode change, the first hunk is a pseudo hunk that
722 * corresponds to the mode line in the header. If the user did not want
723 * to stage that "hunk", we actually have to cut it out from the header.
724 */
725 int skip_mode_change =
726 file_diff->mode_change && file_diff->hunk->use != USE_HUNK;
727 struct hunk *head = &file_diff->head, *first = file_diff->hunk;
728
729 if (!skip_mode_change) {
730 render_hunk(s, head, 0, colored, out);
731 return;
732 }
733
734 if (colored) {
735 const char *p = s->colored.buf;
736
737 strbuf_add(out, p + head->colored_start,
738 first->colored_start - head->colored_start);
739 strbuf_add(out, p + first->colored_end,
740 head->colored_end - first->colored_end);
741 } else {
742 const char *p = s->plain.buf;
743
744 strbuf_add(out, p + head->start, first->start - head->start);
745 strbuf_add(out, p + first->end, head->end - first->end);
746 }
747}
748
11f2c0da
JS
749/* Coalesce hunks again that were split */
750static int merge_hunks(struct add_p_state *s, struct file_diff *file_diff,
bcdd297b 751 size_t *hunk_index, int use_all, struct hunk *merged)
11f2c0da 752{
bcdd297b 753 size_t i = *hunk_index, delta;
11f2c0da
JS
754 struct hunk *hunk = file_diff->hunk + i;
755 /* `header` corresponds to the merged hunk */
756 struct hunk_header *header = &merged->header, *next;
757
bcdd297b 758 if (!use_all && hunk->use != USE_HUNK)
11f2c0da
JS
759 return 0;
760
761 *merged = *hunk;
762 /* We simply skip the colored part (if any) when merging hunks */
763 merged->colored_start = merged->colored_end = 0;
764
765 for (; i + 1 < file_diff->hunk_nr; i++) {
766 hunk++;
767 next = &hunk->header;
768
769 /*
770 * Stop merging hunks when:
771 *
772 * - the hunk is not selected for use, or
773 * - the hunk does not overlap with the already-merged hunk(s)
774 */
bcdd297b
JS
775 if ((!use_all && hunk->use != USE_HUNK) ||
776 header->new_offset >= next->new_offset + merged->delta ||
777 header->new_offset + header->new_count
778 < next->new_offset + merged->delta)
11f2c0da
JS
779 break;
780
bcdd297b
JS
781 /*
782 * If the hunks were not edited, and overlap, we can simply
783 * extend the line range.
784 */
785 if (merged->start < hunk->start && merged->end > hunk->start) {
786 merged->end = hunk->end;
787 merged->colored_end = hunk->colored_end;
788 delta = 0;
789 } else {
790 const char *plain = s->plain.buf;
791 size_t overlapping_line_count = header->new_offset
792 + header->new_count - merged->delta
793 - next->new_offset;
794 size_t overlap_end = hunk->start;
795 size_t overlap_start = overlap_end;
796 size_t overlap_next, len, j;
797
798 /*
799 * One of the hunks was edited: the modified hunk was
800 * appended to the strbuf `s->plain`.
801 *
802 * Let's ensure that at least the last context line of
803 * the first hunk overlaps with the corresponding line
804 * of the second hunk, and then merge.
805 */
806 for (j = 0; j < overlapping_line_count; j++) {
807 overlap_next = find_next_line(&s->plain,
808 overlap_end);
809
810 if (overlap_next > hunk->end)
811 BUG("failed to find %d context lines "
812 "in:\n%.*s",
813 (int)overlapping_line_count,
814 (int)(hunk->end - hunk->start),
815 plain + hunk->start);
816
817 if (plain[overlap_end] != ' ')
818 return error(_("expected context line "
819 "#%d in\n%.*s"),
820 (int)(j + 1),
821 (int)(hunk->end
822 - hunk->start),
823 plain + hunk->start);
824
825 overlap_start = overlap_end;
826 overlap_end = overlap_next;
827 }
828 len = overlap_end - overlap_start;
829
830 if (len > merged->end - merged->start ||
831 memcmp(plain + merged->end - len,
832 plain + overlap_start, len))
833 return error(_("hunks do not overlap:\n%.*s\n"
834 "\tdoes not end with:\n%.*s"),
835 (int)(merged->end - merged->start),
836 plain + merged->start,
837 (int)len, plain + overlap_start);
838
839 /*
840 * Since the start-end ranges are not adjacent, we
841 * cannot simply take the union of the ranges. To
842 * address that, we temporarily append the union of the
843 * lines to the `plain` strbuf.
844 */
845 if (merged->end != s->plain.len) {
846 size_t start = s->plain.len;
847
848 strbuf_add(&s->plain, plain + merged->start,
849 merged->end - merged->start);
850 plain = s->plain.buf;
851 merged->start = start;
852 merged->end = s->plain.len;
853 }
854
855 strbuf_add(&s->plain,
856 plain + overlap_end,
857 hunk->end - overlap_end);
858 merged->end = s->plain.len;
859 merged->splittable_into += hunk->splittable_into;
860 delta = merged->delta;
861 merged->delta += hunk->delta;
862 }
11f2c0da
JS
863
864 header->old_count = next->old_offset + next->old_count
865 - header->old_offset;
bcdd297b
JS
866 header->new_count = next->new_offset + delta
867 + next->new_count - header->new_offset;
11f2c0da
JS
868 }
869
870 if (i == *hunk_index)
871 return 0;
872
873 *hunk_index = i;
874 return 1;
875}
876
80399aec 877static void reassemble_patch(struct add_p_state *s,
bcdd297b
JS
878 struct file_diff *file_diff, int use_all,
879 struct strbuf *out)
f6aa7ecc
JS
880{
881 struct hunk *hunk;
bcdd297b 882 size_t save_len = s->plain.len, i;
25ea47af 883 ssize_t delta = 0;
f6aa7ecc 884
5906d5de 885 render_diff_header(s, file_diff, 0, out);
f6aa7ecc 886
5906d5de 887 for (i = file_diff->mode_change; i < file_diff->hunk_nr; i++) {
11f2c0da
JS
888 struct hunk merged = { 0 };
889
80399aec 890 hunk = file_diff->hunk + i;
bcdd297b 891 if (!use_all && hunk->use != USE_HUNK)
25ea47af
JS
892 delta += hunk->header.old_count
893 - hunk->header.new_count;
11f2c0da
JS
894 else {
895 /* merge overlapping hunks into a temporary hunk */
bcdd297b 896 if (merge_hunks(s, file_diff, &i, use_all, &merged))
11f2c0da
JS
897 hunk = &merged;
898
25ea47af 899 render_hunk(s, hunk, delta, 0, out);
bcdd297b
JS
900
901 /*
902 * In case `merge_hunks()` used `plain` as a scratch
903 * pad (this happens when an edited hunk had to be
904 * coalesced with another hunk).
905 */
906 strbuf_setlen(&s->plain, save_len);
907
908 delta += hunk->delta;
11f2c0da 909 }
f6aa7ecc
JS
910 }
911}
912
510aeca1
JS
913static int split_hunk(struct add_p_state *s, struct file_diff *file_diff,
914 size_t hunk_index)
915{
916 int colored = !!s->colored.len, first = 1;
917 struct hunk *hunk = file_diff->hunk + hunk_index;
918 size_t splittable_into;
919 size_t end, colored_end, current, colored_current = 0, context_line_count;
920 struct hunk_header remaining, *header;
921 char marker, ch;
922
923 if (hunk_index >= file_diff->hunk_nr)
924 BUG("invalid hunk index: %d (must be >= 0 and < %d)",
925 (int)hunk_index, (int)file_diff->hunk_nr);
926
927 if (hunk->splittable_into < 2)
928 return 0;
929 splittable_into = hunk->splittable_into;
930
931 end = hunk->end;
932 colored_end = hunk->colored_end;
933
934 remaining = hunk->header;
935
936 file_diff->hunk_nr += splittable_into - 1;
937 ALLOC_GROW(file_diff->hunk, file_diff->hunk_nr, file_diff->hunk_alloc);
938 if (hunk_index + splittable_into < file_diff->hunk_nr)
939 memmove(file_diff->hunk + hunk_index + splittable_into,
940 file_diff->hunk + hunk_index + 1,
941 (file_diff->hunk_nr - hunk_index - splittable_into)
942 * sizeof(*hunk));
943 hunk = file_diff->hunk + hunk_index;
944 hunk->splittable_into = 1;
945 memset(hunk + 1, 0, (splittable_into - 1) * sizeof(*hunk));
946
947 header = &hunk->header;
948 header->old_count = header->new_count = 0;
949
950 current = hunk->start;
951 if (colored)
952 colored_current = hunk->colored_start;
953 marker = '\0';
954 context_line_count = 0;
955
956 while (splittable_into > 1) {
957 ch = s->plain.buf[current];
958
959 if (!ch)
960 BUG("buffer overrun while splitting hunks");
961
962 /*
963 * Is this the first context line after a chain of +/- lines?
964 * Then record the start of the next split hunk.
965 */
966 if ((marker == '-' || marker == '+') && ch == ' ') {
967 first = 0;
968 hunk[1].start = current;
969 if (colored)
970 hunk[1].colored_start = colored_current;
971 context_line_count = 0;
972 }
973
974 /*
975 * Was the previous line a +/- one? Alternatively, is this the
976 * first line (and not a +/- one)?
977 *
978 * Then just increment the appropriate counter and continue
979 * with the next line.
980 */
981 if (marker != ' ' || (ch != '-' && ch != '+')) {
982next_hunk_line:
983 /* Comment lines are attached to the previous line */
984 if (ch == '\\')
985 ch = marker ? marker : ' ';
986
987 /* current hunk not done yet */
988 if (ch == ' ')
989 context_line_count++;
990 else if (ch == '-')
991 header->old_count++;
992 else if (ch == '+')
993 header->new_count++;
994 else
995 BUG("unhandled diff marker: '%c'", ch);
996 marker = ch;
997 current = find_next_line(&s->plain, current);
998 if (colored)
999 colored_current =
1000 find_next_line(&s->colored,
1001 colored_current);
1002 continue;
1003 }
1004
1005 /*
1006 * We got us the start of a new hunk!
1007 *
1008 * This is a context line, so it is shared with the previous
1009 * hunk, if any.
1010 */
1011
1012 if (first) {
1013 if (header->old_count || header->new_count)
1014 BUG("counts are off: %d/%d",
1015 (int)header->old_count,
1016 (int)header->new_count);
1017
1018 header->old_count = context_line_count;
1019 header->new_count = context_line_count;
1020 context_line_count = 0;
1021 first = 0;
1022 goto next_hunk_line;
1023 }
1024
1025 remaining.old_offset += header->old_count;
1026 remaining.old_count -= header->old_count;
1027 remaining.new_offset += header->new_count;
1028 remaining.new_count -= header->new_count;
1029
1030 /* initialize next hunk header's offsets */
1031 hunk[1].header.old_offset =
1032 header->old_offset + header->old_count;
1033 hunk[1].header.new_offset =
1034 header->new_offset + header->new_count;
1035
1036 /* add one split hunk */
1037 header->old_count += context_line_count;
1038 header->new_count += context_line_count;
1039
1040 hunk->end = current;
1041 if (colored)
1042 hunk->colored_end = colored_current;
1043
1044 hunk++;
1045 hunk->splittable_into = 1;
1046 hunk->use = hunk[-1].use;
1047 header = &hunk->header;
1048
1049 header->old_count = header->new_count = context_line_count;
1050 context_line_count = 0;
1051
1052 splittable_into--;
1053 marker = ch;
1054 }
1055
1056 /* last hunk simply gets the rest */
1057 if (header->old_offset != remaining.old_offset)
1058 BUG("miscounted old_offset: %lu != %lu",
1059 header->old_offset, remaining.old_offset);
1060 if (header->new_offset != remaining.new_offset)
1061 BUG("miscounted new_offset: %lu != %lu",
1062 header->new_offset, remaining.new_offset);
1063 header->old_count = remaining.old_count;
1064 header->new_count = remaining.new_count;
1065 hunk->end = end;
1066 if (colored)
1067 hunk->colored_end = colored_end;
1068
1069 return 0;
1070}
1071
bcdd297b
JS
1072static void recolor_hunk(struct add_p_state *s, struct hunk *hunk)
1073{
1074 const char *plain = s->plain.buf;
1075 size_t current, eol, next;
1076
1077 if (!s->colored.len)
1078 return;
1079
1080 hunk->colored_start = s->colored.len;
1081 for (current = hunk->start; current < hunk->end; ) {
1082 for (eol = current; eol < hunk->end; eol++)
1083 if (plain[eol] == '\n')
1084 break;
1085 next = eol + (eol < hunk->end);
1086 if (eol > current && plain[eol - 1] == '\r')
1087 eol--;
1088
1089 strbuf_addstr(&s->colored,
1090 plain[current] == '-' ?
1091 s->s.file_old_color :
1092 plain[current] == '+' ?
1093 s->s.file_new_color :
1094 s->s.context_color);
1095 strbuf_add(&s->colored, plain + current, eol - current);
6f1a5caa 1096 strbuf_addstr(&s->colored, s->s.reset_color);
bcdd297b
JS
1097 if (next > eol)
1098 strbuf_add(&s->colored, plain + eol, next - eol);
1099 current = next;
1100 }
1101 hunk->colored_end = s->colored.len;
1102}
1103
1104static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
1105{
1106 size_t i;
1107
1108 strbuf_reset(&s->buf);
1109 strbuf_commented_addf(&s->buf, _("Manual hunk edit mode -- see bottom for "
1110 "a quick guide.\n"));
1111 render_hunk(s, hunk, 0, 0, &s->buf);
1112 strbuf_commented_addf(&s->buf,
1113 _("---\n"
1114 "To remove '%c' lines, make them ' ' lines "
1115 "(context).\n"
1116 "To remove '%c' lines, delete them.\n"
1117 "Lines starting with %c will be removed.\n"),
d2a233cb
JS
1118 s->mode->is_reverse ? '+' : '-',
1119 s->mode->is_reverse ? '-' : '+',
1120 comment_line_char);
1121 strbuf_commented_addf(&s->buf, "%s", _(s->mode->edit_hunk_hint));
bcdd297b
JS
1122 /*
1123 * TRANSLATORS: 'it' refers to the patch mentioned in the previous
1124 * messages.
1125 */
1126 strbuf_commented_addf(&s->buf,
1127 _("If it does not apply cleanly, you will be "
1128 "given an opportunity to\n"
1129 "edit again. If all lines of the hunk are "
1130 "removed, then the edit is\n"
1131 "aborted and the hunk is left unchanged.\n"));
1132
1133 if (strbuf_edit_interactively(&s->buf, "addp-hunk-edit.diff", NULL) < 0)
1134 return -1;
1135
1136 /* strip out commented lines */
1137 hunk->start = s->plain.len;
1138 for (i = 0; i < s->buf.len; ) {
1139 size_t next = find_next_line(&s->buf, i);
1140
1141 if (s->buf.buf[i] != comment_line_char)
1142 strbuf_add(&s->plain, s->buf.buf + i, next - i);
1143 i = next;
1144 }
1145
1146 hunk->end = s->plain.len;
1147 if (hunk->end == hunk->start)
1148 /* The user aborted editing by deleting everything */
1149 return 0;
1150
1151 recolor_hunk(s, hunk);
1152
1153 /*
1154 * If the hunk header is intact, parse it, otherwise simply use the
1155 * hunk header prior to editing (which will adjust `hunk->start` to
1156 * skip the hunk header).
1157 */
1158 if (s->plain.buf[hunk->start] == '@' &&
1159 parse_hunk_header(s, hunk) < 0)
1160 return error(_("could not parse hunk header"));
1161
1162 return 1;
1163}
1164
1165static ssize_t recount_edited_hunk(struct add_p_state *s, struct hunk *hunk,
1166 size_t orig_old_count, size_t orig_new_count)
1167{
1168 struct hunk_header *header = &hunk->header;
1169 size_t i;
1170
1171 header->old_count = header->new_count = 0;
1172 for (i = hunk->start; i < hunk->end; ) {
1173 switch (s->plain.buf[i]) {
1174 case '-':
1175 header->old_count++;
1176 break;
1177 case '+':
1178 header->new_count++;
1179 break;
1180 case ' ': case '\r': case '\n':
1181 header->old_count++;
1182 header->new_count++;
1183 break;
1184 }
1185
1186 i = find_next_line(&s->plain, i);
1187 }
1188
1189 return orig_old_count - orig_new_count
1190 - header->old_count + header->new_count;
1191}
1192
1193static int run_apply_check(struct add_p_state *s,
1194 struct file_diff *file_diff)
1195{
1196 struct child_process cp = CHILD_PROCESS_INIT;
1197
1198 strbuf_reset(&s->buf);
1199 reassemble_patch(s, file_diff, 1, &s->buf);
1200
1201 setup_child_process(s, &cp,
d2a233cb 1202 "apply", "--check", NULL);
ef8d7ac4 1203 strvec_pushv(&cp.args, s->mode->apply_check_args);
bcdd297b
JS
1204 if (pipe_command(&cp, s->buf.buf, s->buf.len, NULL, 0, NULL, 0))
1205 return error(_("'git apply --cached' failed"));
1206
1207 return 0;
1208}
1209
04f816b1
JS
1210static int read_single_character(struct add_p_state *s)
1211{
1212 if (s->s.use_single_key) {
1213 int res = read_key_without_echo(&s->answer);
1214 printf("%s\n", res == EOF ? "" : s->answer.buf);
1215 return res;
1216 }
1217
08d383f2 1218 if (git_read_line_interactively(&s->answer) == EOF)
04f816b1 1219 return EOF;
04f816b1
JS
1220 return 0;
1221}
1222
bcdd297b
JS
1223static int prompt_yesno(struct add_p_state *s, const char *prompt)
1224{
1225 for (;;) {
1226 color_fprintf(stdout, s->s.prompt_color, "%s", _(prompt));
1227 fflush(stdout);
04f816b1 1228 if (read_single_character(s) == EOF)
bcdd297b 1229 return -1;
bcdd297b
JS
1230 switch (tolower(s->answer.buf[0])) {
1231 case 'n': return 0;
1232 case 'y': return 1;
1233 }
1234 }
1235}
1236
1237static int edit_hunk_loop(struct add_p_state *s,
1238 struct file_diff *file_diff, struct hunk *hunk)
1239{
1240 size_t plain_len = s->plain.len, colored_len = s->colored.len;
1241 struct hunk backup;
1242
1243 backup = *hunk;
1244
1245 for (;;) {
1246 int res = edit_hunk_manually(s, hunk);
1247 if (res == 0) {
84544f2e 1248 /* abandoned */
bcdd297b
JS
1249 *hunk = backup;
1250 return -1;
1251 }
1252
1253 if (res > 0) {
1254 hunk->delta +=
1255 recount_edited_hunk(s, hunk,
1256 backup.header.old_count,
1257 backup.header.new_count);
1258 if (!run_apply_check(s, file_diff))
1259 return 0;
1260 }
1261
1262 /* Drop edits (they were appended to s->plain) */
1263 strbuf_setlen(&s->plain, plain_len);
1264 strbuf_setlen(&s->colored, colored_len);
1265 *hunk = backup;
1266
1267 /*
1268 * TRANSLATORS: do not translate [y/n]
1269 * The program will only accept that input at this point.
1270 * Consider translating (saying "no" discards!) as
1271 * (saying "n" for "no" discards!) if the translation
1272 * of the word "no" does not start with n.
1273 */
1274 res = prompt_yesno(s, _("Your edited hunk does not apply. "
1275 "Edit again (saying \"no\" discards!) "
1276 "[y/n]? "));
1277 if (res < 1)
1278 return -1;
1279 }
1280}
1281
52628f94
JS
1282static int apply_for_checkout(struct add_p_state *s, struct strbuf *diff,
1283 int is_reverse)
1284{
1285 const char *reverse = is_reverse ? "-R" : NULL;
1286 struct child_process check_index = CHILD_PROCESS_INIT;
1287 struct child_process check_worktree = CHILD_PROCESS_INIT;
1288 struct child_process apply_index = CHILD_PROCESS_INIT;
1289 struct child_process apply_worktree = CHILD_PROCESS_INIT;
1290 int applies_index, applies_worktree;
1291
1292 setup_child_process(s, &check_index,
1293 "apply", "--cached", "--check", reverse, NULL);
1294 applies_index = !pipe_command(&check_index, diff->buf, diff->len,
1295 NULL, 0, NULL, 0);
1296
1297 setup_child_process(s, &check_worktree,
1298 "apply", "--check", reverse, NULL);
1299 applies_worktree = !pipe_command(&check_worktree, diff->buf, diff->len,
1300 NULL, 0, NULL, 0);
1301
1302 if (applies_worktree && applies_index) {
1303 setup_child_process(s, &apply_index,
1304 "apply", "--cached", reverse, NULL);
1305 pipe_command(&apply_index, diff->buf, diff->len,
1306 NULL, 0, NULL, 0);
1307
1308 setup_child_process(s, &apply_worktree,
1309 "apply", reverse, NULL);
1310 pipe_command(&apply_worktree, diff->buf, diff->len,
1311 NULL, 0, NULL, 0);
1312
1313 return 1;
1314 }
1315
1316 if (!applies_index) {
1317 err(s, _("The selected hunks do not apply to the index!"));
1318 if (prompt_yesno(s, _("Apply them to the worktree "
1319 "anyway? ")) > 0) {
1320 setup_child_process(s, &apply_worktree,
1321 "apply", reverse, NULL);
1322 return pipe_command(&apply_worktree, diff->buf,
1323 diff->len, NULL, 0, NULL, 0);
1324 }
1325 err(s, _("Nothing was applied.\n"));
1326 } else
1327 /* As a last resort, show the diff to the user */
1328 fwrite(diff->buf, diff->len, 1, stderr);
1329
1330 return 0;
1331}
1332
9254bdfb
JS
1333#define SUMMARY_HEADER_WIDTH 20
1334#define SUMMARY_LINE_WIDTH 80
1335static void summarize_hunk(struct add_p_state *s, struct hunk *hunk,
1336 struct strbuf *out)
1337{
1338 struct hunk_header *header = &hunk->header;
1339 struct strbuf *plain = &s->plain;
1340 size_t len = out->len, i;
1341
1342 strbuf_addf(out, " -%lu,%lu +%lu,%lu ",
1343 header->old_offset, header->old_count,
1344 header->new_offset, header->new_count);
1345 if (out->len - len < SUMMARY_HEADER_WIDTH)
1346 strbuf_addchars(out, ' ',
1347 SUMMARY_HEADER_WIDTH + len - out->len);
1348 for (i = hunk->start; i < hunk->end; i = find_next_line(plain, i))
1349 if (plain->buf[i] != ' ')
1350 break;
1351 if (i < hunk->end)
1352 strbuf_add(out, plain->buf + i, find_next_line(plain, i) - i);
1353 if (out->len - len > SUMMARY_LINE_WIDTH)
1354 strbuf_setlen(out, len + SUMMARY_LINE_WIDTH);
1355 strbuf_complete_line(out);
1356}
1357
1358#define DISPLAY_HUNKS_LINES 20
1359static size_t display_hunks(struct add_p_state *s,
1360 struct file_diff *file_diff, size_t start_index)
1361{
1362 size_t end_index = start_index + DISPLAY_HUNKS_LINES;
1363
1364 if (end_index > file_diff->hunk_nr)
1365 end_index = file_diff->hunk_nr;
1366
1367 while (start_index < end_index) {
1368 struct hunk *hunk = file_diff->hunk + start_index++;
1369
1370 strbuf_reset(&s->buf);
1371 strbuf_addf(&s->buf, "%c%2d: ", hunk->use == USE_HUNK ? '+'
1372 : hunk->use == SKIP_HUNK ? '-' : ' ',
1373 (int)start_index);
1374 summarize_hunk(s, hunk, &s->buf);
1375 fputs(s->buf.buf, stdout);
1376 }
1377
1378 return end_index;
1379}
1380
54d9d9b2
JS
1381static const char help_patch_remainder[] =
1382N_("j - leave this hunk undecided, see next undecided hunk\n"
f6aa7ecc
JS
1383 "J - leave this hunk undecided, see next hunk\n"
1384 "k - leave this hunk undecided, see previous undecided hunk\n"
1385 "K - leave this hunk undecided, see previous hunk\n"
9254bdfb 1386 "g - select a hunk to go to\n"
d6cf8733 1387 "/ - search for a hunk matching the given regex\n"
510aeca1 1388 "s - split the current hunk into smaller hunks\n"
bcdd297b 1389 "e - manually edit the current hunk\n"
f6aa7ecc
JS
1390 "? - print help\n");
1391
80399aec
JS
1392static int patch_update_file(struct add_p_state *s,
1393 struct file_diff *file_diff)
f6aa7ecc
JS
1394{
1395 size_t hunk_index = 0;
1396 ssize_t i, undecided_previous, undecided_next;
1397 struct hunk *hunk;
1398 char ch;
1399 struct child_process cp = CHILD_PROCESS_INIT;
ade246ef 1400 int colored = !!s->colored.len, quit = 0;
0ecd9d27 1401 enum prompt_mode_type prompt_mode_type;
ce910287
PW
1402 enum {
1403 ALLOW_GOTO_PREVIOUS_HUNK = 1 << 0,
1404 ALLOW_GOTO_PREVIOUS_UNDECIDED_HUNK = 1 << 1,
1405 ALLOW_GOTO_NEXT_HUNK = 1 << 2,
1406 ALLOW_GOTO_NEXT_UNDECIDED_HUNK = 1 << 3,
1407 ALLOW_SEARCH_AND_GOTO = 1 << 4,
1408 ALLOW_SPLIT = 1 << 5,
1409 ALLOW_EDIT = 1 << 6
1410 } permitted = 0;
f6aa7ecc 1411
75a009dc
PW
1412 /* Empty added files have no hunks */
1413 if (!file_diff->hunk_nr && !file_diff->added)
f6aa7ecc
JS
1414 return 0;
1415
1416 strbuf_reset(&s->buf);
5906d5de 1417 render_diff_header(s, file_diff, colored, &s->buf);
f6aa7ecc
JS
1418 fputs(s->buf.buf, stdout);
1419 for (;;) {
80399aec 1420 if (hunk_index >= file_diff->hunk_nr)
f6aa7ecc 1421 hunk_index = 0;
75a009dc
PW
1422 hunk = file_diff->hunk_nr
1423 ? file_diff->hunk + hunk_index
1424 : &file_diff->head;
f6aa7ecc 1425 undecided_previous = -1;
f6aa7ecc 1426 undecided_next = -1;
75a009dc
PW
1427
1428 if (file_diff->hunk_nr) {
1429 for (i = hunk_index - 1; i >= 0; i--)
1430 if (file_diff->hunk[i].use == UNDECIDED_HUNK) {
1431 undecided_previous = i;
1432 break;
1433 }
1434
1435 for (i = hunk_index + 1; i < file_diff->hunk_nr; i++)
1436 if (file_diff->hunk[i].use == UNDECIDED_HUNK) {
1437 undecided_next = i;
1438 break;
1439 }
1440 }
f6aa7ecc
JS
1441
1442 /* Everything decided? */
1443 if (undecided_previous < 0 && undecided_next < 0 &&
1444 hunk->use != UNDECIDED_HUNK)
1445 break;
1446
1447 strbuf_reset(&s->buf);
75a009dc
PW
1448 if (file_diff->hunk_nr) {
1449 render_hunk(s, hunk, 0, colored, &s->buf);
1450 fputs(s->buf.buf, stdout);
f6aa7ecc 1451
75a009dc
PW
1452 strbuf_reset(&s->buf);
1453 if (undecided_previous >= 0) {
1454 permitted |= ALLOW_GOTO_PREVIOUS_UNDECIDED_HUNK;
1455 strbuf_addstr(&s->buf, ",k");
1456 }
1457 if (hunk_index) {
1458 permitted |= ALLOW_GOTO_PREVIOUS_HUNK;
1459 strbuf_addstr(&s->buf, ",K");
1460 }
1461 if (undecided_next >= 0) {
1462 permitted |= ALLOW_GOTO_NEXT_UNDECIDED_HUNK;
1463 strbuf_addstr(&s->buf, ",j");
1464 }
1465 if (hunk_index + 1 < file_diff->hunk_nr) {
1466 permitted |= ALLOW_GOTO_NEXT_HUNK;
1467 strbuf_addstr(&s->buf, ",J");
1468 }
1469 if (file_diff->hunk_nr > 1) {
1470 permitted |= ALLOW_SEARCH_AND_GOTO;
1471 strbuf_addstr(&s->buf, ",g,/");
1472 }
1473 if (hunk->splittable_into > 1) {
1474 permitted |= ALLOW_SPLIT;
1475 strbuf_addstr(&s->buf, ",s");
1476 }
1477 if (hunk_index + 1 > file_diff->mode_change &&
1478 !file_diff->deleted) {
1479 permitted |= ALLOW_EDIT;
1480 strbuf_addstr(&s->buf, ",e");
1481 }
ce910287 1482 }
0ecd9d27
JS
1483 if (file_diff->deleted)
1484 prompt_mode_type = PROMPT_DELETION;
2c8bd847
JS
1485 else if (file_diff->added)
1486 prompt_mode_type = PROMPT_ADDITION;
0ecd9d27
JS
1487 else if (file_diff->mode_change && !hunk_index)
1488 prompt_mode_type = PROMPT_MODE_CHANGE;
1489 else
1490 prompt_mode_type = PROMPT_HUNK;
1491
6681e360 1492 printf("%s(%"PRIuMAX"/%"PRIuMAX") ", s->s.prompt_color,
80399aec 1493 (uintmax_t)hunk_index + 1,
75a009dc
PW
1494 (uintmax_t)(file_diff->hunk_nr
1495 ? file_diff->hunk_nr
1496 : 1));
6681e360
JS
1497 printf(_(s->mode->prompt_mode[prompt_mode_type]),
1498 s->buf.buf);
1499 if (*s->s.reset_color)
1500 fputs(s->s.reset_color, stdout);
f6aa7ecc 1501 fflush(stdout);
04f816b1 1502 if (read_single_character(s) == EOF)
f6aa7ecc 1503 break;
f6aa7ecc
JS
1504
1505 if (!s->answer.len)
1506 continue;
1507 ch = tolower(s->answer.buf[0]);
1508 if (ch == 'y') {
1509 hunk->use = USE_HUNK;
1510soft_increment:
1511 hunk_index = undecided_next < 0 ?
80399aec 1512 file_diff->hunk_nr : undecided_next;
f6aa7ecc
JS
1513 } else if (ch == 'n') {
1514 hunk->use = SKIP_HUNK;
1515 goto soft_increment;
1516 } else if (ch == 'a') {
75a009dc
PW
1517 if (file_diff->hunk_nr) {
1518 for (; hunk_index < file_diff->hunk_nr; hunk_index++) {
1519 hunk = file_diff->hunk + hunk_index;
1520 if (hunk->use == UNDECIDED_HUNK)
1521 hunk->use = USE_HUNK;
1522 }
1523 } else if (hunk->use == UNDECIDED_HUNK) {
1524 hunk->use = USE_HUNK;
f6aa7ecc 1525 }
ade246ef 1526 } else if (ch == 'd' || ch == 'q') {
75a009dc
PW
1527 if (file_diff->hunk_nr) {
1528 for (; hunk_index < file_diff->hunk_nr; hunk_index++) {
1529 hunk = file_diff->hunk + hunk_index;
1530 if (hunk->use == UNDECIDED_HUNK)
1531 hunk->use = SKIP_HUNK;
1532 }
1533 } else if (hunk->use == UNDECIDED_HUNK) {
1534 hunk->use = SKIP_HUNK;
f6aa7ecc 1535 }
ade246ef
JS
1536 if (ch == 'q') {
1537 quit = 1;
1538 break;
1539 }
7584dd3c 1540 } else if (s->answer.buf[0] == 'K') {
ce910287 1541 if (permitted & ALLOW_GOTO_PREVIOUS_HUNK)
7584dd3c
JS
1542 hunk_index--;
1543 else
1544 err(s, _("No previous hunk"));
1545 } else if (s->answer.buf[0] == 'J') {
ce910287 1546 if (permitted & ALLOW_GOTO_NEXT_HUNK)
7584dd3c
JS
1547 hunk_index++;
1548 else
1549 err(s, _("No next hunk"));
1550 } else if (s->answer.buf[0] == 'k') {
ce910287 1551 if (permitted & ALLOW_GOTO_PREVIOUS_UNDECIDED_HUNK)
7584dd3c
JS
1552 hunk_index = undecided_previous;
1553 else
1554 err(s, _("No previous hunk"));
1555 } else if (s->answer.buf[0] == 'j') {
ce910287 1556 if (permitted & ALLOW_GOTO_NEXT_UNDECIDED_HUNK)
7584dd3c
JS
1557 hunk_index = undecided_next;
1558 else
1559 err(s, _("No next hunk"));
9254bdfb
JS
1560 } else if (s->answer.buf[0] == 'g') {
1561 char *pend;
1562 unsigned long response;
1563
ce910287 1564 if (!(permitted & ALLOW_SEARCH_AND_GOTO)) {
9254bdfb
JS
1565 err(s, _("No other hunks to goto"));
1566 continue;
1567 }
1568 strbuf_remove(&s->answer, 0, 1);
1569 strbuf_trim(&s->answer);
1570 i = hunk_index - DISPLAY_HUNKS_LINES / 2;
cfd0163d 1571 if (i < (int)file_diff->mode_change)
9254bdfb
JS
1572 i = file_diff->mode_change;
1573 while (s->answer.len == 0) {
1574 i = display_hunks(s, file_diff, i);
1575 printf("%s", i < file_diff->hunk_nr ?
1576 _("go to which hunk (<ret> to see "
1577 "more)? ") : _("go to which hunk? "));
1578 fflush(stdout);
1579 if (strbuf_getline(&s->answer,
1580 stdin) == EOF)
1581 break;
1582 strbuf_trim_trailing_newline(&s->answer);
1583 }
1584
1585 strbuf_trim(&s->answer);
1586 response = strtoul(s->answer.buf, &pend, 10);
1587 if (*pend || pend == s->answer.buf)
1588 err(s, _("Invalid number: '%s'"),
1589 s->answer.buf);
1590 else if (0 < response && response <= file_diff->hunk_nr)
1591 hunk_index = response - 1;
1592 else
1593 err(s, Q_("Sorry, only %d hunk available.",
1594 "Sorry, only %d hunks available.",
1595 file_diff->hunk_nr),
1596 (int)file_diff->hunk_nr);
d6cf8733
JS
1597 } else if (s->answer.buf[0] == '/') {
1598 regex_t regex;
1599 int ret;
1600
ce910287 1601 if (!(permitted & ALLOW_SEARCH_AND_GOTO)) {
d6cf8733
JS
1602 err(s, _("No other hunks to search"));
1603 continue;
1604 }
1605 strbuf_remove(&s->answer, 0, 1);
1606 strbuf_trim_trailing_newline(&s->answer);
1607 if (s->answer.len == 0) {
1608 printf("%s", _("search for regex? "));
1609 fflush(stdout);
1610 if (strbuf_getline(&s->answer,
1611 stdin) == EOF)
1612 break;
1613 strbuf_trim_trailing_newline(&s->answer);
1614 if (s->answer.len == 0)
1615 continue;
1616 }
1617 ret = regcomp(&regex, s->answer.buf,
1618 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
1619 if (ret) {
1620 char errbuf[1024];
1621
1622 regerror(ret, &regex, errbuf, sizeof(errbuf));
1623 err(s, _("Malformed search regexp %s: %s"),
1624 s->answer.buf, errbuf);
1625 continue;
1626 }
1627 i = hunk_index;
1628 for (;;) {
1629 /* render the hunk into a scratch buffer */
1630 render_hunk(s, file_diff->hunk + i, 0, 0,
1631 &s->buf);
1632 if (regexec(&regex, s->buf.buf, 0, NULL, 0)
1633 != REG_NOMATCH)
1634 break;
1635 i++;
1636 if (i == file_diff->hunk_nr)
1637 i = 0;
1638 if (i != hunk_index)
1639 continue;
1640 err(s, _("No hunk matches the given pattern"));
1641 break;
1642 }
1643 hunk_index = i;
510aeca1
JS
1644 } else if (s->answer.buf[0] == 's') {
1645 size_t splittable_into = hunk->splittable_into;
ce910287 1646 if (!(permitted & ALLOW_SPLIT))
510aeca1
JS
1647 err(s, _("Sorry, cannot split this hunk"));
1648 else if (!split_hunk(s, file_diff,
1649 hunk - file_diff->hunk))
1650 color_fprintf_ln(stdout, s->s.header_color,
1651 _("Split into %d hunks."),
1652 (int)splittable_into);
bcdd297b 1653 } else if (s->answer.buf[0] == 'e') {
ce910287 1654 if (!(permitted & ALLOW_EDIT))
bcdd297b
JS
1655 err(s, _("Sorry, cannot edit this hunk"));
1656 else if (edit_hunk_loop(s, file_diff, hunk) >= 0) {
1657 hunk->use = USE_HUNK;
1658 goto soft_increment;
1659 }
54d9d9b2
JS
1660 } else {
1661 const char *p = _(help_patch_remainder), *eol = p;
1662
1663 color_fprintf(stdout, s->s.help_color, "%s",
d2a233cb 1664 _(s->mode->help_patch_text));
54d9d9b2
JS
1665
1666 /*
1667 * Show only those lines of the remainder that are
1668 * actually applicable with the current hunk.
1669 */
1670 for (; *p; p = eol + (*eol == '\n')) {
1671 eol = strchrnul(p, '\n');
1672
1673 /*
1674 * `s->buf` still contains the part of the
1675 * commands shown in the prompt that are not
1676 * always available.
1677 */
1678 if (*p != '?' && !strchr(s->buf.buf, *p))
1679 continue;
1680
1681 color_fprintf_ln(stdout, s->s.help_color,
1682 "%.*s", (int)(eol - p), p);
1683 }
1684 }
f6aa7ecc
JS
1685 }
1686
1687 /* Any hunk to be used? */
80399aec
JS
1688 for (i = 0; i < file_diff->hunk_nr; i++)
1689 if (file_diff->hunk[i].use == USE_HUNK)
f6aa7ecc
JS
1690 break;
1691
75a009dc
PW
1692 if (i < file_diff->hunk_nr ||
1693 (!file_diff->hunk_nr && file_diff->head.use == USE_HUNK)) {
f6aa7ecc
JS
1694 /* At least one hunk selected: apply */
1695 strbuf_reset(&s->buf);
bcdd297b 1696 reassemble_patch(s, file_diff, 0, &s->buf);
f6aa7ecc 1697
25ea47af 1698 discard_index(s->s.r->index);
52628f94
JS
1699 if (s->mode->apply_for_checkout)
1700 apply_for_checkout(s, &s->buf,
1701 s->mode->is_reverse);
1702 else {
1703 setup_child_process(s, &cp, "apply", NULL);
ef8d7ac4 1704 strvec_pushv(&cp.args, s->mode->apply_args);
52628f94
JS
1705 if (pipe_command(&cp, s->buf.buf, s->buf.len,
1706 NULL, 0, NULL, 0))
1707 error(_("'git apply' failed"));
1708 }
dc626415 1709 if (repo_read_index(s->s.r) >= 0)
25ea47af 1710 repo_refresh_and_write_index(s->s.r, REFRESH_QUIET, 0,
f6aa7ecc
JS
1711 1, NULL, NULL, NULL);
1712 }
1713
1714 putchar('\n');
ade246ef 1715 return quit;
f6aa7ecc
JS
1716}
1717
d2a233cb
JS
1718int run_add_p(struct repository *r, enum add_p_mode mode,
1719 const char *revision, const struct pathspec *ps)
f6aa7ecc 1720{
25ea47af
JS
1721 struct add_p_state s = {
1722 { r }, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT
1723 };
2e408319 1724 size_t i, binary_count = 0;
25ea47af
JS
1725
1726 init_add_i_state(&s.s, r);
f6aa7ecc 1727
36bae1dc
JS
1728 if (mode == ADD_P_STASH)
1729 s.mode = &patch_mode_stash;
1730 else if (mode == ADD_P_RESET) {
f82a9e51
DL
1731 /*
1732 * NEEDSWORK: Instead of comparing to the literal "HEAD",
1733 * compare the commit objects instead so that other ways of
1734 * saying the same thing (such as "@") are also handled
1735 * appropriately.
1736 *
1737 * This applies to the cases below too.
1738 */
36bae1dc
JS
1739 if (!revision || !strcmp(revision, "HEAD"))
1740 s.mode = &patch_mode_reset_head;
1741 else
1742 s.mode = &patch_mode_reset_nothead;
52628f94
JS
1743 } else if (mode == ADD_P_CHECKOUT) {
1744 if (!revision)
1745 s.mode = &patch_mode_checkout_index;
1746 else if (!strcmp(revision, "HEAD"))
1747 s.mode = &patch_mode_checkout_head;
1748 else
1749 s.mode = &patch_mode_checkout_nothead;
cee6cb73
JS
1750 } else if (mode == ADD_P_WORKTREE) {
1751 if (!revision)
1752 s.mode = &patch_mode_checkout_index;
1753 else if (!strcmp(revision, "HEAD"))
1754 s.mode = &patch_mode_worktree_head;
1755 else
1756 s.mode = &patch_mode_worktree_nothead;
36bae1dc
JS
1757 } else
1758 s.mode = &patch_mode_add;
d2a233cb
JS
1759 s.revision = revision;
1760
9c5f3ee3
ÆAB
1761 discard_index(r->index);
1762 if (repo_read_index(r) < 0 ||
36bae1dc
JS
1763 (!s.mode->index_only &&
1764 repo_refresh_and_write_index(r, REFRESH_QUIET, 0, 1,
1765 NULL, NULL, NULL) < 0) ||
f6aa7ecc 1766 parse_diff(&s, ps) < 0) {
324efcf6 1767 add_p_state_clear(&s);
f6aa7ecc
JS
1768 return -1;
1769 }
1770
80399aec 1771 for (i = 0; i < s.file_diff_nr; i++)
2e408319
JS
1772 if (s.file_diff[i].binary && !s.file_diff[i].hunk_nr)
1773 binary_count++;
1774 else if (patch_update_file(&s, s.file_diff + i))
80399aec 1775 break;
f6aa7ecc 1776
2e408319
JS
1777 if (s.file_diff_nr == 0)
1778 fprintf(stderr, _("No changes.\n"));
1779 else if (binary_count == s.file_diff_nr)
1780 fprintf(stderr, _("Only binary files changed.\n"));
1781
324efcf6 1782 add_p_state_clear(&s);
f6aa7ecc
JS
1783 return 0;
1784}