2 #include "add-interactive.h"
4 #include "run-command.h"
9 #include "compat/terminal.h"
12 enum prompt_mode_type
{
13 PROMPT_MODE_CHANGE
= 0, PROMPT_DELETION
, PROMPT_ADDITION
, PROMPT_HUNK
,
14 PROMPT_MODE_MAX
, /* must be last */
19 * The magic constant 4 is chosen such that all patch modes
20 * provide enough space for three command-line arguments followed by a
23 const char *diff_cmd
[4], *apply_args
[4], *apply_check_args
[4];
24 unsigned is_reverse
:1, index_only
:1, apply_for_checkout
:1;
25 const char *prompt_mode
[PROMPT_MODE_MAX
];
26 const char *edit_hunk_hint
, *help_patch_text
;
29 static struct patch_mode patch_mode_add
= {
30 .diff_cmd
= { "diff-files", NULL
},
31 .apply_args
= { "--cached", NULL
},
32 .apply_check_args
= { "--cached", NULL
},
34 N_("Stage mode change [y,n,q,a,d%s,?]? "),
35 N_("Stage deletion [y,n,q,a,d%s,?]? "),
36 N_("Stage addition [y,n,q,a,d%s,?]? "),
37 N_("Stage this hunk [y,n,q,a,d%s,?]? ")
39 .edit_hunk_hint
= N_("If the patch applies cleanly, the edited hunk "
40 "will immediately be marked for staging."),
42 N_("y - stage this hunk\n"
43 "n - do not stage this hunk\n"
44 "q - quit; do not stage this hunk or any of the remaining "
46 "a - stage this hunk and all later hunks in the file\n"
47 "d - do not stage this hunk or any of the later hunks in "
51 static struct patch_mode patch_mode_stash
= {
52 .diff_cmd
= { "diff-index", "HEAD", NULL
},
53 .apply_args
= { "--cached", NULL
},
54 .apply_check_args
= { "--cached", NULL
},
56 N_("Stash mode change [y,n,q,a,d%s,?]? "),
57 N_("Stash deletion [y,n,q,a,d%s,?]? "),
58 N_("Stash addition [y,n,q,a,d%s,?]? "),
59 N_("Stash this hunk [y,n,q,a,d%s,?]? "),
61 .edit_hunk_hint
= N_("If the patch applies cleanly, the edited hunk "
62 "will immediately be marked for stashing."),
64 N_("y - stash this hunk\n"
65 "n - do not stash this hunk\n"
66 "q - quit; do not stash this hunk or any of the remaining "
68 "a - stash this hunk and all later hunks in the file\n"
69 "d - do not stash this hunk or any of the later hunks in "
73 static struct patch_mode patch_mode_reset_head
= {
74 .diff_cmd
= { "diff-index", "--cached", NULL
},
75 .apply_args
= { "-R", "--cached", NULL
},
76 .apply_check_args
= { "-R", "--cached", NULL
},
80 N_("Unstage mode change [y,n,q,a,d%s,?]? "),
81 N_("Unstage deletion [y,n,q,a,d%s,?]? "),
82 N_("Unstage addition [y,n,q,a,d%s,?]? "),
83 N_("Unstage this hunk [y,n,q,a,d%s,?]? "),
85 .edit_hunk_hint
= N_("If the patch applies cleanly, the edited hunk "
86 "will immediately be marked for unstaging."),
88 N_("y - unstage this hunk\n"
89 "n - do not unstage this hunk\n"
90 "q - quit; do not unstage this hunk or any of the remaining "
92 "a - unstage this hunk and all later hunks in the file\n"
93 "d - do not unstage this hunk or any of the later hunks in "
97 static struct patch_mode patch_mode_reset_nothead
= {
98 .diff_cmd
= { "diff-index", "-R", "--cached", NULL
},
99 .apply_args
= { "--cached", NULL
},
100 .apply_check_args
= { "--cached", NULL
},
103 N_("Apply mode change to index [y,n,q,a,d%s,?]? "),
104 N_("Apply deletion to index [y,n,q,a,d%s,?]? "),
105 N_("Apply addition to index [y,n,q,a,d%s,?]? "),
106 N_("Apply this hunk to index [y,n,q,a,d%s,?]? "),
108 .edit_hunk_hint
= N_("If the patch applies cleanly, the edited hunk "
109 "will immediately be marked for applying."),
111 N_("y - apply this hunk to index\n"
112 "n - do not apply this hunk to index\n"
113 "q - quit; do not apply this hunk or any of the remaining "
115 "a - apply this hunk and all later hunks in the file\n"
116 "d - do not apply this hunk or any of the later hunks in "
120 static struct patch_mode patch_mode_checkout_index
= {
121 .diff_cmd
= { "diff-files", NULL
},
122 .apply_args
= { "-R", NULL
},
123 .apply_check_args
= { "-R", NULL
},
126 N_("Discard mode change from worktree [y,n,q,a,d%s,?]? "),
127 N_("Discard deletion from worktree [y,n,q,a,d%s,?]? "),
128 N_("Discard addition from worktree [y,n,q,a,d%s,?]? "),
129 N_("Discard this hunk from worktree [y,n,q,a,d%s,?]? "),
131 .edit_hunk_hint
= N_("If the patch applies cleanly, the edited hunk "
132 "will immediately be marked for discarding."),
134 N_("y - discard this hunk from worktree\n"
135 "n - do not discard this hunk from worktree\n"
136 "q - quit; do not discard this hunk or any of the remaining "
138 "a - discard this hunk and all later hunks in the file\n"
139 "d - do not discard this hunk or any of the later hunks in "
143 static struct patch_mode patch_mode_checkout_head
= {
144 .diff_cmd
= { "diff-index", NULL
},
145 .apply_for_checkout
= 1,
146 .apply_check_args
= { "-R", NULL
},
149 N_("Discard mode change from index and worktree [y,n,q,a,d%s,?]? "),
150 N_("Discard deletion from index and worktree [y,n,q,a,d%s,?]? "),
151 N_("Discard addition from index and worktree [y,n,q,a,d%s,?]? "),
152 N_("Discard this hunk from index and worktree [y,n,q,a,d%s,?]? "),
154 .edit_hunk_hint
= N_("If the patch applies cleanly, the edited hunk "
155 "will immediately be marked for discarding."),
157 N_("y - discard this hunk from index and worktree\n"
158 "n - do not discard this hunk from index and worktree\n"
159 "q - quit; do not discard this hunk or any of the remaining "
161 "a - discard this hunk and all later hunks in the file\n"
162 "d - do not discard this hunk or any of the later hunks in "
166 static struct patch_mode patch_mode_checkout_nothead
= {
167 .diff_cmd
= { "diff-index", "-R", NULL
},
168 .apply_for_checkout
= 1,
169 .apply_check_args
= { NULL
},
171 N_("Apply mode change to index and worktree [y,n,q,a,d%s,?]? "),
172 N_("Apply deletion to index and worktree [y,n,q,a,d%s,?]? "),
173 N_("Apply addition to index and worktree [y,n,q,a,d%s,?]? "),
174 N_("Apply this hunk to index and worktree [y,n,q,a,d%s,?]? "),
176 .edit_hunk_hint
= N_("If the patch applies cleanly, the edited hunk "
177 "will immediately be marked for applying."),
179 N_("y - apply this hunk to index and worktree\n"
180 "n - do not apply this hunk to index and worktree\n"
181 "q - quit; do not apply this hunk or any of the remaining "
183 "a - apply this hunk and all later hunks in the file\n"
184 "d - do not apply this hunk or any of the later hunks in "
188 static struct patch_mode patch_mode_worktree_head
= {
189 .diff_cmd
= { "diff-index", NULL
},
190 .apply_args
= { "-R", NULL
},
191 .apply_check_args
= { "-R", NULL
},
194 N_("Discard mode change from index and worktree [y,n,q,a,d%s,?]? "),
195 N_("Discard deletion from index and worktree [y,n,q,a,d%s,?]? "),
196 N_("Discard addition from index and worktree [y,n,q,a,d%s,?]? "),
197 N_("Discard this hunk from index and worktree [y,n,q,a,d%s,?]? "),
199 .edit_hunk_hint
= N_("If the patch applies cleanly, the edited hunk "
200 "will immediately be marked for discarding."),
202 N_("y - discard this hunk from worktree\n"
203 "n - do not discard this hunk from worktree\n"
204 "q - quit; do not discard this hunk or any of the remaining "
206 "a - discard this hunk and all later hunks in the file\n"
207 "d - do not discard this hunk or any of the later hunks in "
211 static struct patch_mode patch_mode_worktree_nothead
= {
212 .diff_cmd
= { "diff-index", "-R", NULL
},
213 .apply_args
= { NULL
},
214 .apply_check_args
= { NULL
},
216 N_("Apply mode change to index and worktree [y,n,q,a,d%s,?]? "),
217 N_("Apply deletion to index and worktree [y,n,q,a,d%s,?]? "),
218 N_("Apply addition to index and worktree [y,n,q,a,d%s,?]? "),
219 N_("Apply this hunk to index and worktree [y,n,q,a,d%s,?]? "),
221 .edit_hunk_hint
= N_("If the patch applies cleanly, the edited hunk "
222 "will immediately be marked for applying."),
224 N_("y - apply this hunk to worktree\n"
225 "n - do not apply this hunk to worktree\n"
226 "q - quit; do not apply this hunk or any of the remaining "
228 "a - apply this hunk and all later hunks in the file\n"
229 "d - do not apply this hunk or any of the later hunks in "
234 unsigned long old_offset
, old_count
, new_offset
, new_count
;
236 * Start/end offsets to the extra text after the second `@@` in the
237 * hunk header, e.g. the function signature. This is expected to
238 * include the newline.
240 size_t extra_start
, extra_end
, colored_extra_start
, colored_extra_end
;
244 size_t start
, end
, colored_start
, colored_end
, splittable_into
;
246 enum { UNDECIDED_HUNK
= 0, SKIP_HUNK
, USE_HUNK
} use
;
247 struct hunk_header header
;
251 struct add_i_state s
;
252 struct strbuf answer
, buf
;
255 struct strbuf plain
, colored
;
259 size_t hunk_nr
, hunk_alloc
;
260 unsigned deleted
:1, added
:1, mode_change
:1,binary
:1;
265 struct patch_mode
*mode
;
266 const char *revision
;
269 static void add_p_state_clear(struct add_p_state
*s
)
273 strbuf_release(&s
->answer
);
274 strbuf_release(&s
->buf
);
275 strbuf_release(&s
->plain
);
276 strbuf_release(&s
->colored
);
277 for (i
= 0; i
< s
->file_diff_nr
; i
++)
278 free(s
->file_diff
[i
].hunk
);
280 clear_add_i_state(&s
->s
);
283 __attribute__((format (printf
, 2, 3)))
284 static void err(struct add_p_state
*s
, const char *fmt
, ...)
289 fputs(s
->s
.error_color
, stderr
);
290 vfprintf(stderr
, fmt
, args
);
291 fputs(s
->s
.reset_color
, stderr
);
296 static void setup_child_process(struct add_p_state
*s
,
297 struct child_process
*cp
, ...)
303 while ((arg
= va_arg(ap
, const char *)))
304 strvec_push(&cp
->args
, arg
);
308 strvec_pushf(&cp
->env_array
,
309 INDEX_ENVIRONMENT
"=%s", s
->s
.r
->index_file
);
312 static int parse_range(const char **p
,
313 unsigned long *offset
, unsigned long *count
)
317 *offset
= strtoul(*p
, &pend
, 10);
325 *count
= strtoul(pend
+ 1, (char **)p
, 10);
326 return *p
== pend
+ 1 ? -1 : 0;
329 static int parse_hunk_header(struct add_p_state
*s
, struct hunk
*hunk
)
331 struct hunk_header
*header
= &hunk
->header
;
332 const char *line
= s
->plain
.buf
+ hunk
->start
, *p
= line
;
333 char *eol
= memchr(p
, '\n', s
->plain
.len
- hunk
->start
);
336 eol
= s
->plain
.buf
+ s
->plain
.len
;
338 if (!skip_prefix(p
, "@@ -", &p
) ||
339 parse_range(&p
, &header
->old_offset
, &header
->old_count
) < 0 ||
340 !skip_prefix(p
, " +", &p
) ||
341 parse_range(&p
, &header
->new_offset
, &header
->new_count
) < 0 ||
342 !skip_prefix(p
, " @@", &p
))
343 return error(_("could not parse hunk header '%.*s'"),
344 (int)(eol
- line
), line
);
346 hunk
->start
= eol
- s
->plain
.buf
+ (*eol
== '\n');
347 header
->extra_start
= p
- s
->plain
.buf
;
348 header
->extra_end
= hunk
->start
;
350 if (!s
->colored
.len
) {
351 header
->colored_extra_start
= header
->colored_extra_end
= 0;
355 /* Now find the extra text in the colored diff */
356 line
= s
->colored
.buf
+ hunk
->colored_start
;
357 eol
= memchr(line
, '\n', s
->colored
.len
- hunk
->colored_start
);
359 eol
= s
->colored
.buf
+ s
->colored
.len
;
360 p
= memmem(line
, eol
- line
, "@@ -", 4);
362 return error(_("could not parse colored hunk header '%.*s'"),
363 (int)(eol
- line
), line
);
364 p
= memmem(p
+ 4, eol
- p
- 4, " @@", 3);
366 return error(_("could not parse colored hunk header '%.*s'"),
367 (int)(eol
- line
), line
);
368 hunk
->colored_start
= eol
- s
->colored
.buf
+ (*eol
== '\n');
369 header
->colored_extra_start
= p
+ 3 - s
->colored
.buf
;
370 header
->colored_extra_end
= hunk
->colored_start
;
375 static int is_octal(const char *p
, size_t len
)
381 if (*p
< '0' || *(p
++) > '7')
386 static int parse_diff(struct add_p_state
*s
, const struct pathspec
*ps
)
388 struct strvec args
= STRVEC_INIT
;
389 const char *diff_algorithm
= s
->s
.interactive_diff_algorithm
;
390 struct strbuf
*plain
= &s
->plain
, *colored
= NULL
;
391 struct child_process cp
= CHILD_PROCESS_INIT
;
392 char *p
, *pend
, *colored_p
= NULL
, *colored_pend
= NULL
, marker
= '\0';
393 size_t file_diff_alloc
= 0, i
, color_arg_index
;
394 struct file_diff
*file_diff
= NULL
;
395 struct hunk
*hunk
= NULL
;
398 strvec_pushv(&args
, s
->mode
->diff_cmd
);
400 strvec_pushf(&args
, "--diff-algorithm=%s", diff_algorithm
);
402 struct object_id oid
;
404 /* could be on an unborn branch */
405 !strcmp("HEAD", s
->revision
) &&
406 get_oid("HEAD", &oid
) ?
407 empty_tree_oid_hex() : s
->revision
);
409 color_arg_index
= args
.nr
;
410 /* Use `--no-color` explicitly, just in case `diff.color = always`. */
411 strvec_pushl(&args
, "--no-color", "-p", "--", NULL
);
412 for (i
= 0; i
< ps
->nr
; i
++)
413 strvec_push(&args
, ps
->items
[i
].original
);
415 setup_child_process(s
, &cp
, NULL
);
416 strvec_pushv(&cp
.args
, args
.v
);
417 res
= capture_command(&cp
, plain
, 0);
420 return error(_("could not parse diff"));
426 strbuf_complete_line(plain
);
428 if (want_color_fd(1, -1)) {
429 struct child_process colored_cp
= CHILD_PROCESS_INIT
;
430 const char *diff_filter
= s
->s
.interactive_diff_filter
;
432 setup_child_process(s
, &colored_cp
, NULL
);
433 xsnprintf((char *)args
.v
[color_arg_index
], 8, "--color");
434 strvec_pushv(&colored_cp
.args
, args
.v
);
435 colored
= &s
->colored
;
436 res
= capture_command(&colored_cp
, colored
, 0);
439 return error(_("could not parse colored diff"));
442 struct child_process filter_cp
= CHILD_PROCESS_INIT
;
444 setup_child_process(s
, &filter_cp
,
446 filter_cp
.git_cmd
= 0;
447 filter_cp
.use_shell
= 1;
448 strbuf_reset(&s
->buf
);
449 if (pipe_command(&filter_cp
,
450 colored
->buf
, colored
->len
,
451 &s
->buf
, colored
->len
,
453 return error(_("failed to run '%s'"),
455 strbuf_swap(colored
, &s
->buf
);
458 strbuf_complete_line(colored
);
459 colored_p
= colored
->buf
;
460 colored_pend
= colored_p
+ colored
->len
;
464 /* parse files and hunks */
466 pend
= p
+ plain
->len
;
468 char *eol
= memchr(p
, '\n', pend
- p
);
469 const char *deleted
= NULL
, *mode_change
= NULL
;
474 if (starts_with(p
, "diff ")) {
475 ALLOC_GROW_BY(s
->file_diff
, s
->file_diff_nr
, 1,
477 file_diff
= s
->file_diff
+ s
->file_diff_nr
- 1;
478 hunk
= &file_diff
->head
;
479 hunk
->start
= p
- plain
->buf
;
481 hunk
->colored_start
= colored_p
- colored
->buf
;
483 } else if (p
== plain
->buf
)
484 BUG("diff starts with unexpected line:\n"
485 "%.*s\n", (int)(eol
- p
), p
);
486 else if (file_diff
->deleted
)
487 ; /* keep the rest of the file in a single "hunk" */
488 else if (starts_with(p
, "@@ ") ||
489 (hunk
== &file_diff
->head
&&
490 (skip_prefix(p
, "deleted file", &deleted
)))) {
491 if (marker
== '-' || marker
== '+')
493 * Should not happen; previous hunk did not end
494 * in a context line? Handle it anyway.
496 hunk
->splittable_into
++;
498 ALLOC_GROW_BY(file_diff
->hunk
, file_diff
->hunk_nr
, 1,
499 file_diff
->hunk_alloc
);
500 hunk
= file_diff
->hunk
+ file_diff
->hunk_nr
- 1;
502 hunk
->start
= p
- plain
->buf
;
504 hunk
->colored_start
= colored_p
- colored
->buf
;
507 file_diff
->deleted
= 1;
508 else if (parse_hunk_header(s
, hunk
) < 0)
512 * Start counting into how many hunks this one can be
516 } else if (hunk
== &file_diff
->head
&&
517 starts_with(p
, "new file")) {
518 file_diff
->added
= 1;
519 } else if (hunk
== &file_diff
->head
&&
520 skip_prefix(p
, "old mode ", &mode_change
) &&
521 is_octal(mode_change
, eol
- mode_change
)) {
522 if (file_diff
->mode_change
)
523 BUG("double mode change?\n\n%.*s",
524 (int)(eol
- plain
->buf
), plain
->buf
);
525 if (file_diff
->hunk_nr
)
526 BUG("mode change in the middle?\n\n%.*s",
527 (int)(eol
- plain
->buf
), plain
->buf
);
530 * Do *not* change `hunk`: the mode change pseudo-hunk
531 * is _part of_ the header "hunk".
533 file_diff
->mode_change
= 1;
534 ALLOC_GROW_BY(file_diff
->hunk
, file_diff
->hunk_nr
, 1,
535 file_diff
->hunk_alloc
);
536 file_diff
->hunk
->start
= p
- plain
->buf
;
538 file_diff
->hunk
->colored_start
=
539 colored_p
- colored
->buf
;
540 } else if (hunk
== &file_diff
->head
&&
541 skip_prefix(p
, "new mode ", &mode_change
) &&
542 is_octal(mode_change
, eol
- mode_change
)) {
545 * Extend the "mode change" pseudo-hunk to include also
546 * the "new mode" line.
548 if (!file_diff
->mode_change
)
549 BUG("'new mode' without 'old mode'?\n\n%.*s",
550 (int)(eol
- plain
->buf
), plain
->buf
);
551 if (file_diff
->hunk_nr
!= 1)
552 BUG("mode change in the middle?\n\n%.*s",
553 (int)(eol
- plain
->buf
), plain
->buf
);
554 if (p
- plain
->buf
!= file_diff
->hunk
->end
)
555 BUG("'new mode' does not immediately follow "
556 "'old mode'?\n\n%.*s",
557 (int)(eol
- plain
->buf
), plain
->buf
);
558 } else if (hunk
== &file_diff
->head
&&
559 starts_with(p
, "Binary files "))
560 file_diff
->binary
= 1;
562 if (!!file_diff
->deleted
+ !!file_diff
->added
+
563 !!file_diff
->mode_change
> 1)
564 BUG("diff can only contain delete *or* add *or* a "
565 "mode change?!?\n%.*s",
566 (int)(eol
- (plain
->buf
+ file_diff
->head
.start
)),
567 plain
->buf
+ file_diff
->head
.start
);
569 if ((marker
== '-' || marker
== '+') && *p
== ' ')
570 hunk
->splittable_into
++;
571 if (marker
&& *p
!= '\\')
574 p
= eol
== pend
? pend
: eol
+ 1;
575 hunk
->end
= p
- plain
->buf
;
578 char *colored_eol
= memchr(colored_p
, '\n',
579 colored_pend
- colored_p
);
581 colored_p
= colored_eol
+ 1;
583 /* colored shorter than non-colored? */
584 goto mismatched_output
;
586 colored_p
= colored_pend
;
588 hunk
->colored_end
= colored_p
- colored
->buf
;
592 if (file_diff
->hunk_nr
!= 1)
593 BUG("mode change in hunk #%d???",
594 (int)file_diff
->hunk_nr
);
595 /* Adjust the end of the "mode change" pseudo-hunk */
596 file_diff
->hunk
->end
= hunk
->end
;
598 file_diff
->hunk
->colored_end
= hunk
->colored_end
;
602 if (marker
== '-' || marker
== '+')
604 * Last hunk ended in non-context line (i.e. it appended lines
605 * to the file, so there are no trailing context lines).
607 hunk
->splittable_into
++;
609 /* non-colored shorter than colored? */
610 if (colored_p
!= colored_pend
) {
612 error(_("mismatched output from interactive.diffFilter"));
613 advise(_("Your filter must maintain a one-to-one correspondence\n"
614 "between its input and output lines."));
621 static size_t find_next_line(struct strbuf
*sb
, size_t offset
)
625 if (offset
>= sb
->len
)
626 BUG("looking for next line beyond buffer (%d >= %d)\n%s",
627 (int)offset
, (int)sb
->len
, sb
->buf
);
629 eol
= memchr(sb
->buf
+ offset
, '\n', sb
->len
- offset
);
632 return eol
- sb
->buf
+ 1;
635 static void render_hunk(struct add_p_state
*s
, struct hunk
*hunk
,
636 ssize_t delta
, int colored
, struct strbuf
*out
)
638 struct hunk_header
*header
= &hunk
->header
;
640 if (hunk
->header
.old_offset
!= 0 || hunk
->header
.new_offset
!= 0) {
642 * Generate the hunk header dynamically, except for special
643 * hunks (such as the diff header).
647 unsigned long old_offset
= header
->old_offset
;
648 unsigned long new_offset
= header
->new_offset
;
651 p
= s
->plain
.buf
+ header
->extra_start
;
652 len
= header
->extra_end
- header
->extra_start
;
654 strbuf_addstr(out
, s
->s
.fraginfo_color
);
655 p
= s
->colored
.buf
+ header
->colored_extra_start
;
656 len
= header
->colored_extra_end
657 - header
->colored_extra_start
;
660 if (s
->mode
->is_reverse
)
665 strbuf_addf(out
, "@@ -%lu", old_offset
);
666 if (header
->old_count
!= 1)
667 strbuf_addf(out
, ",%lu", header
->old_count
);
668 strbuf_addf(out
, " +%lu", new_offset
);
669 if (header
->new_count
!= 1)
670 strbuf_addf(out
, ",%lu", header
->new_count
);
671 strbuf_addstr(out
, " @@");
674 strbuf_add(out
, p
, len
);
676 strbuf_addf(out
, "%s\n", s
->s
.reset_color
);
678 strbuf_addch(out
, '\n');
682 strbuf_add(out
, s
->colored
.buf
+ hunk
->colored_start
,
683 hunk
->colored_end
- hunk
->colored_start
);
685 strbuf_add(out
, s
->plain
.buf
+ hunk
->start
,
686 hunk
->end
- hunk
->start
);
689 static void render_diff_header(struct add_p_state
*s
,
690 struct file_diff
*file_diff
, int colored
,
694 * If there was a mode change, the first hunk is a pseudo hunk that
695 * corresponds to the mode line in the header. If the user did not want
696 * to stage that "hunk", we actually have to cut it out from the header.
698 int skip_mode_change
=
699 file_diff
->mode_change
&& file_diff
->hunk
->use
!= USE_HUNK
;
700 struct hunk
*head
= &file_diff
->head
, *first
= file_diff
->hunk
;
702 if (!skip_mode_change
) {
703 render_hunk(s
, head
, 0, colored
, out
);
708 const char *p
= s
->colored
.buf
;
710 strbuf_add(out
, p
+ head
->colored_start
,
711 first
->colored_start
- head
->colored_start
);
712 strbuf_add(out
, p
+ first
->colored_end
,
713 head
->colored_end
- first
->colored_end
);
715 const char *p
= s
->plain
.buf
;
717 strbuf_add(out
, p
+ head
->start
, first
->start
- head
->start
);
718 strbuf_add(out
, p
+ first
->end
, head
->end
- first
->end
);
722 /* Coalesce hunks again that were split */
723 static int merge_hunks(struct add_p_state
*s
, struct file_diff
*file_diff
,
724 size_t *hunk_index
, int use_all
, struct hunk
*merged
)
726 size_t i
= *hunk_index
, delta
;
727 struct hunk
*hunk
= file_diff
->hunk
+ i
;
728 /* `header` corresponds to the merged hunk */
729 struct hunk_header
*header
= &merged
->header
, *next
;
731 if (!use_all
&& hunk
->use
!= USE_HUNK
)
735 /* We simply skip the colored part (if any) when merging hunks */
736 merged
->colored_start
= merged
->colored_end
= 0;
738 for (; i
+ 1 < file_diff
->hunk_nr
; i
++) {
740 next
= &hunk
->header
;
743 * Stop merging hunks when:
745 * - the hunk is not selected for use, or
746 * - the hunk does not overlap with the already-merged hunk(s)
748 if ((!use_all
&& hunk
->use
!= USE_HUNK
) ||
749 header
->new_offset
>= next
->new_offset
+ merged
->delta
||
750 header
->new_offset
+ header
->new_count
751 < next
->new_offset
+ merged
->delta
)
755 * If the hunks were not edited, and overlap, we can simply
756 * extend the line range.
758 if (merged
->start
< hunk
->start
&& merged
->end
> hunk
->start
) {
759 merged
->end
= hunk
->end
;
760 merged
->colored_end
= hunk
->colored_end
;
763 const char *plain
= s
->plain
.buf
;
764 size_t overlapping_line_count
= header
->new_offset
765 + header
->new_count
- merged
->delta
767 size_t overlap_end
= hunk
->start
;
768 size_t overlap_start
= overlap_end
;
769 size_t overlap_next
, len
, j
;
772 * One of the hunks was edited: the modified hunk was
773 * appended to the strbuf `s->plain`.
775 * Let's ensure that at least the last context line of
776 * the first hunk overlaps with the corresponding line
777 * of the second hunk, and then merge.
779 for (j
= 0; j
< overlapping_line_count
; j
++) {
780 overlap_next
= find_next_line(&s
->plain
,
783 if (overlap_next
> hunk
->end
)
784 BUG("failed to find %d context lines "
786 (int)overlapping_line_count
,
787 (int)(hunk
->end
- hunk
->start
),
788 plain
+ hunk
->start
);
790 if (plain
[overlap_end
] != ' ')
791 return error(_("expected context line "
796 plain
+ hunk
->start
);
798 overlap_start
= overlap_end
;
799 overlap_end
= overlap_next
;
801 len
= overlap_end
- overlap_start
;
803 if (len
> merged
->end
- merged
->start
||
804 memcmp(plain
+ merged
->end
- len
,
805 plain
+ overlap_start
, len
))
806 return error(_("hunks do not overlap:\n%.*s\n"
807 "\tdoes not end with:\n%.*s"),
808 (int)(merged
->end
- merged
->start
),
809 plain
+ merged
->start
,
810 (int)len
, plain
+ overlap_start
);
813 * Since the start-end ranges are not adjacent, we
814 * cannot simply take the union of the ranges. To
815 * address that, we temporarily append the union of the
816 * lines to the `plain` strbuf.
818 if (merged
->end
!= s
->plain
.len
) {
819 size_t start
= s
->plain
.len
;
821 strbuf_add(&s
->plain
, plain
+ merged
->start
,
822 merged
->end
- merged
->start
);
823 plain
= s
->plain
.buf
;
824 merged
->start
= start
;
825 merged
->end
= s
->plain
.len
;
828 strbuf_add(&s
->plain
,
830 hunk
->end
- overlap_end
);
831 merged
->end
= s
->plain
.len
;
832 merged
->splittable_into
+= hunk
->splittable_into
;
833 delta
= merged
->delta
;
834 merged
->delta
+= hunk
->delta
;
837 header
->old_count
= next
->old_offset
+ next
->old_count
838 - header
->old_offset
;
839 header
->new_count
= next
->new_offset
+ delta
840 + next
->new_count
- header
->new_offset
;
843 if (i
== *hunk_index
)
850 static void reassemble_patch(struct add_p_state
*s
,
851 struct file_diff
*file_diff
, int use_all
,
855 size_t save_len
= s
->plain
.len
, i
;
858 render_diff_header(s
, file_diff
, 0, out
);
860 for (i
= file_diff
->mode_change
; i
< file_diff
->hunk_nr
; i
++) {
861 struct hunk merged
= { 0 };
863 hunk
= file_diff
->hunk
+ i
;
864 if (!use_all
&& hunk
->use
!= USE_HUNK
)
865 delta
+= hunk
->header
.old_count
866 - hunk
->header
.new_count
;
868 /* merge overlapping hunks into a temporary hunk */
869 if (merge_hunks(s
, file_diff
, &i
, use_all
, &merged
))
872 render_hunk(s
, hunk
, delta
, 0, out
);
875 * In case `merge_hunks()` used `plain` as a scratch
876 * pad (this happens when an edited hunk had to be
877 * coalesced with another hunk).
879 strbuf_setlen(&s
->plain
, save_len
);
881 delta
+= hunk
->delta
;
886 static int split_hunk(struct add_p_state
*s
, struct file_diff
*file_diff
,
889 int colored
= !!s
->colored
.len
, first
= 1;
890 struct hunk
*hunk
= file_diff
->hunk
+ hunk_index
;
891 size_t splittable_into
;
892 size_t end
, colored_end
, current
, colored_current
= 0, context_line_count
;
893 struct hunk_header remaining
, *header
;
896 if (hunk_index
>= file_diff
->hunk_nr
)
897 BUG("invalid hunk index: %d (must be >= 0 and < %d)",
898 (int)hunk_index
, (int)file_diff
->hunk_nr
);
900 if (hunk
->splittable_into
< 2)
902 splittable_into
= hunk
->splittable_into
;
905 colored_end
= hunk
->colored_end
;
907 remaining
= hunk
->header
;
909 file_diff
->hunk_nr
+= splittable_into
- 1;
910 ALLOC_GROW(file_diff
->hunk
, file_diff
->hunk_nr
, file_diff
->hunk_alloc
);
911 if (hunk_index
+ splittable_into
< file_diff
->hunk_nr
)
912 memmove(file_diff
->hunk
+ hunk_index
+ splittable_into
,
913 file_diff
->hunk
+ hunk_index
+ 1,
914 (file_diff
->hunk_nr
- hunk_index
- splittable_into
)
916 hunk
= file_diff
->hunk
+ hunk_index
;
917 hunk
->splittable_into
= 1;
918 memset(hunk
+ 1, 0, (splittable_into
- 1) * sizeof(*hunk
));
920 header
= &hunk
->header
;
921 header
->old_count
= header
->new_count
= 0;
923 current
= hunk
->start
;
925 colored_current
= hunk
->colored_start
;
927 context_line_count
= 0;
929 while (splittable_into
> 1) {
930 ch
= s
->plain
.buf
[current
];
933 BUG("buffer overrun while splitting hunks");
936 * Is this the first context line after a chain of +/- lines?
937 * Then record the start of the next split hunk.
939 if ((marker
== '-' || marker
== '+') && ch
== ' ') {
941 hunk
[1].start
= current
;
943 hunk
[1].colored_start
= colored_current
;
944 context_line_count
= 0;
948 * Was the previous line a +/- one? Alternatively, is this the
949 * first line (and not a +/- one)?
951 * Then just increment the appropriate counter and continue
952 * with the next line.
954 if (marker
!= ' ' || (ch
!= '-' && ch
!= '+')) {
956 /* Comment lines are attached to the previous line */
958 ch
= marker
? marker
: ' ';
960 /* current hunk not done yet */
962 context_line_count
++;
968 BUG("unhandled diff marker: '%c'", ch
);
970 current
= find_next_line(&s
->plain
, current
);
973 find_next_line(&s
->colored
,
979 * We got us the start of a new hunk!
981 * This is a context line, so it is shared with the previous
986 if (header
->old_count
|| header
->new_count
)
987 BUG("counts are off: %d/%d",
988 (int)header
->old_count
,
989 (int)header
->new_count
);
991 header
->old_count
= context_line_count
;
992 header
->new_count
= context_line_count
;
993 context_line_count
= 0;
998 remaining
.old_offset
+= header
->old_count
;
999 remaining
.old_count
-= header
->old_count
;
1000 remaining
.new_offset
+= header
->new_count
;
1001 remaining
.new_count
-= header
->new_count
;
1003 /* initialize next hunk header's offsets */
1004 hunk
[1].header
.old_offset
=
1005 header
->old_offset
+ header
->old_count
;
1006 hunk
[1].header
.new_offset
=
1007 header
->new_offset
+ header
->new_count
;
1009 /* add one split hunk */
1010 header
->old_count
+= context_line_count
;
1011 header
->new_count
+= context_line_count
;
1013 hunk
->end
= current
;
1015 hunk
->colored_end
= colored_current
;
1018 hunk
->splittable_into
= 1;
1019 hunk
->use
= hunk
[-1].use
;
1020 header
= &hunk
->header
;
1022 header
->old_count
= header
->new_count
= context_line_count
;
1023 context_line_count
= 0;
1029 /* last hunk simply gets the rest */
1030 if (header
->old_offset
!= remaining
.old_offset
)
1031 BUG("miscounted old_offset: %lu != %lu",
1032 header
->old_offset
, remaining
.old_offset
);
1033 if (header
->new_offset
!= remaining
.new_offset
)
1034 BUG("miscounted new_offset: %lu != %lu",
1035 header
->new_offset
, remaining
.new_offset
);
1036 header
->old_count
= remaining
.old_count
;
1037 header
->new_count
= remaining
.new_count
;
1040 hunk
->colored_end
= colored_end
;
1045 static void recolor_hunk(struct add_p_state
*s
, struct hunk
*hunk
)
1047 const char *plain
= s
->plain
.buf
;
1048 size_t current
, eol
, next
;
1050 if (!s
->colored
.len
)
1053 hunk
->colored_start
= s
->colored
.len
;
1054 for (current
= hunk
->start
; current
< hunk
->end
; ) {
1055 for (eol
= current
; eol
< hunk
->end
; eol
++)
1056 if (plain
[eol
] == '\n')
1058 next
= eol
+ (eol
< hunk
->end
);
1059 if (eol
> current
&& plain
[eol
- 1] == '\r')
1062 strbuf_addstr(&s
->colored
,
1063 plain
[current
] == '-' ?
1064 s
->s
.file_old_color
:
1065 plain
[current
] == '+' ?
1066 s
->s
.file_new_color
:
1067 s
->s
.context_color
);
1068 strbuf_add(&s
->colored
, plain
+ current
, eol
- current
);
1069 strbuf_addstr(&s
->colored
, s
->s
.reset_color
);
1071 strbuf_add(&s
->colored
, plain
+ eol
, next
- eol
);
1074 hunk
->colored_end
= s
->colored
.len
;
1077 static int edit_hunk_manually(struct add_p_state
*s
, struct hunk
*hunk
)
1081 strbuf_reset(&s
->buf
);
1082 strbuf_commented_addf(&s
->buf
, _("Manual hunk edit mode -- see bottom for "
1083 "a quick guide.\n"));
1084 render_hunk(s
, hunk
, 0, 0, &s
->buf
);
1085 strbuf_commented_addf(&s
->buf
,
1087 "To remove '%c' lines, make them ' ' lines "
1089 "To remove '%c' lines, delete them.\n"
1090 "Lines starting with %c will be removed.\n"),
1091 s
->mode
->is_reverse
? '+' : '-',
1092 s
->mode
->is_reverse
? '-' : '+',
1094 strbuf_commented_addf(&s
->buf
, "%s", _(s
->mode
->edit_hunk_hint
));
1096 * TRANSLATORS: 'it' refers to the patch mentioned in the previous
1099 strbuf_commented_addf(&s
->buf
,
1100 _("If it does not apply cleanly, you will be "
1101 "given an opportunity to\n"
1102 "edit again. If all lines of the hunk are "
1103 "removed, then the edit is\n"
1104 "aborted and the hunk is left unchanged.\n"));
1106 if (strbuf_edit_interactively(&s
->buf
, "addp-hunk-edit.diff", NULL
) < 0)
1109 /* strip out commented lines */
1110 hunk
->start
= s
->plain
.len
;
1111 for (i
= 0; i
< s
->buf
.len
; ) {
1112 size_t next
= find_next_line(&s
->buf
, i
);
1114 if (s
->buf
.buf
[i
] != comment_line_char
)
1115 strbuf_add(&s
->plain
, s
->buf
.buf
+ i
, next
- i
);
1119 hunk
->end
= s
->plain
.len
;
1120 if (hunk
->end
== hunk
->start
)
1121 /* The user aborted editing by deleting everything */
1124 recolor_hunk(s
, hunk
);
1127 * If the hunk header is intact, parse it, otherwise simply use the
1128 * hunk header prior to editing (which will adjust `hunk->start` to
1129 * skip the hunk header).
1131 if (s
->plain
.buf
[hunk
->start
] == '@' &&
1132 parse_hunk_header(s
, hunk
) < 0)
1133 return error(_("could not parse hunk header"));
1138 static ssize_t
recount_edited_hunk(struct add_p_state
*s
, struct hunk
*hunk
,
1139 size_t orig_old_count
, size_t orig_new_count
)
1141 struct hunk_header
*header
= &hunk
->header
;
1144 header
->old_count
= header
->new_count
= 0;
1145 for (i
= hunk
->start
; i
< hunk
->end
; ) {
1146 switch (s
->plain
.buf
[i
]) {
1148 header
->old_count
++;
1151 header
->new_count
++;
1153 case ' ': case '\r': case '\n':
1154 header
->old_count
++;
1155 header
->new_count
++;
1159 i
= find_next_line(&s
->plain
, i
);
1162 return orig_old_count
- orig_new_count
1163 - header
->old_count
+ header
->new_count
;
1166 static int run_apply_check(struct add_p_state
*s
,
1167 struct file_diff
*file_diff
)
1169 struct child_process cp
= CHILD_PROCESS_INIT
;
1171 strbuf_reset(&s
->buf
);
1172 reassemble_patch(s
, file_diff
, 1, &s
->buf
);
1174 setup_child_process(s
, &cp
,
1175 "apply", "--check", NULL
);
1176 strvec_pushv(&cp
.args
, s
->mode
->apply_check_args
);
1177 if (pipe_command(&cp
, s
->buf
.buf
, s
->buf
.len
, NULL
, 0, NULL
, 0))
1178 return error(_("'git apply --cached' failed"));
1183 static int read_single_character(struct add_p_state
*s
)
1185 if (s
->s
.use_single_key
) {
1186 int res
= read_key_without_echo(&s
->answer
);
1187 printf("%s\n", res
== EOF
? "" : s
->answer
.buf
);
1191 if (git_read_line_interactively(&s
->answer
) == EOF
)
1196 static int prompt_yesno(struct add_p_state
*s
, const char *prompt
)
1199 color_fprintf(stdout
, s
->s
.prompt_color
, "%s", _(prompt
));
1201 if (read_single_character(s
) == EOF
)
1203 switch (tolower(s
->answer
.buf
[0])) {
1210 static int edit_hunk_loop(struct add_p_state
*s
,
1211 struct file_diff
*file_diff
, struct hunk
*hunk
)
1213 size_t plain_len
= s
->plain
.len
, colored_len
= s
->colored
.len
;
1219 int res
= edit_hunk_manually(s
, hunk
);
1228 recount_edited_hunk(s
, hunk
,
1229 backup
.header
.old_count
,
1230 backup
.header
.new_count
);
1231 if (!run_apply_check(s
, file_diff
))
1235 /* Drop edits (they were appended to s->plain) */
1236 strbuf_setlen(&s
->plain
, plain_len
);
1237 strbuf_setlen(&s
->colored
, colored_len
);
1241 * TRANSLATORS: do not translate [y/n]
1242 * The program will only accept that input at this point.
1243 * Consider translating (saying "no" discards!) as
1244 * (saying "n" for "no" discards!) if the translation
1245 * of the word "no" does not start with n.
1247 res
= prompt_yesno(s
, _("Your edited hunk does not apply. "
1248 "Edit again (saying \"no\" discards!) "
1255 static int apply_for_checkout(struct add_p_state
*s
, struct strbuf
*diff
,
1258 const char *reverse
= is_reverse
? "-R" : NULL
;
1259 struct child_process check_index
= CHILD_PROCESS_INIT
;
1260 struct child_process check_worktree
= CHILD_PROCESS_INIT
;
1261 struct child_process apply_index
= CHILD_PROCESS_INIT
;
1262 struct child_process apply_worktree
= CHILD_PROCESS_INIT
;
1263 int applies_index
, applies_worktree
;
1265 setup_child_process(s
, &check_index
,
1266 "apply", "--cached", "--check", reverse
, NULL
);
1267 applies_index
= !pipe_command(&check_index
, diff
->buf
, diff
->len
,
1270 setup_child_process(s
, &check_worktree
,
1271 "apply", "--check", reverse
, NULL
);
1272 applies_worktree
= !pipe_command(&check_worktree
, diff
->buf
, diff
->len
,
1275 if (applies_worktree
&& applies_index
) {
1276 setup_child_process(s
, &apply_index
,
1277 "apply", "--cached", reverse
, NULL
);
1278 pipe_command(&apply_index
, diff
->buf
, diff
->len
,
1281 setup_child_process(s
, &apply_worktree
,
1282 "apply", reverse
, NULL
);
1283 pipe_command(&apply_worktree
, diff
->buf
, diff
->len
,
1289 if (!applies_index
) {
1290 err(s
, _("The selected hunks do not apply to the index!"));
1291 if (prompt_yesno(s
, _("Apply them to the worktree "
1293 setup_child_process(s
, &apply_worktree
,
1294 "apply", reverse
, NULL
);
1295 return pipe_command(&apply_worktree
, diff
->buf
,
1296 diff
->len
, NULL
, 0, NULL
, 0);
1298 err(s
, _("Nothing was applied.\n"));
1300 /* As a last resort, show the diff to the user */
1301 fwrite(diff
->buf
, diff
->len
, 1, stderr
);
1306 #define SUMMARY_HEADER_WIDTH 20
1307 #define SUMMARY_LINE_WIDTH 80
1308 static void summarize_hunk(struct add_p_state
*s
, struct hunk
*hunk
,
1311 struct hunk_header
*header
= &hunk
->header
;
1312 struct strbuf
*plain
= &s
->plain
;
1313 size_t len
= out
->len
, i
;
1315 strbuf_addf(out
, " -%lu,%lu +%lu,%lu ",
1316 header
->old_offset
, header
->old_count
,
1317 header
->new_offset
, header
->new_count
);
1318 if (out
->len
- len
< SUMMARY_HEADER_WIDTH
)
1319 strbuf_addchars(out
, ' ',
1320 SUMMARY_HEADER_WIDTH
+ len
- out
->len
);
1321 for (i
= hunk
->start
; i
< hunk
->end
; i
= find_next_line(plain
, i
))
1322 if (plain
->buf
[i
] != ' ')
1325 strbuf_add(out
, plain
->buf
+ i
, find_next_line(plain
, i
) - i
);
1326 if (out
->len
- len
> SUMMARY_LINE_WIDTH
)
1327 strbuf_setlen(out
, len
+ SUMMARY_LINE_WIDTH
);
1328 strbuf_complete_line(out
);
1331 #define DISPLAY_HUNKS_LINES 20
1332 static size_t display_hunks(struct add_p_state
*s
,
1333 struct file_diff
*file_diff
, size_t start_index
)
1335 size_t end_index
= start_index
+ DISPLAY_HUNKS_LINES
;
1337 if (end_index
> file_diff
->hunk_nr
)
1338 end_index
= file_diff
->hunk_nr
;
1340 while (start_index
< end_index
) {
1341 struct hunk
*hunk
= file_diff
->hunk
+ start_index
++;
1343 strbuf_reset(&s
->buf
);
1344 strbuf_addf(&s
->buf
, "%c%2d: ", hunk
->use
== USE_HUNK
? '+'
1345 : hunk
->use
== SKIP_HUNK
? '-' : ' ',
1347 summarize_hunk(s
, hunk
, &s
->buf
);
1348 fputs(s
->buf
.buf
, stdout
);
1354 static const char help_patch_remainder
[] =
1355 N_("j - leave this hunk undecided, see next undecided hunk\n"
1356 "J - leave this hunk undecided, see next hunk\n"
1357 "k - leave this hunk undecided, see previous undecided hunk\n"
1358 "K - leave this hunk undecided, see previous hunk\n"
1359 "g - select a hunk to go to\n"
1360 "/ - search for a hunk matching the given regex\n"
1361 "s - split the current hunk into smaller hunks\n"
1362 "e - manually edit the current hunk\n"
1363 "? - print help\n");
1365 static int patch_update_file(struct add_p_state
*s
,
1366 struct file_diff
*file_diff
)
1368 size_t hunk_index
= 0;
1369 ssize_t i
, undecided_previous
, undecided_next
;
1372 struct child_process cp
= CHILD_PROCESS_INIT
;
1373 int colored
= !!s
->colored
.len
, quit
= 0;
1374 enum prompt_mode_type prompt_mode_type
;
1376 ALLOW_GOTO_PREVIOUS_HUNK
= 1 << 0,
1377 ALLOW_GOTO_PREVIOUS_UNDECIDED_HUNK
= 1 << 1,
1378 ALLOW_GOTO_NEXT_HUNK
= 1 << 2,
1379 ALLOW_GOTO_NEXT_UNDECIDED_HUNK
= 1 << 3,
1380 ALLOW_SEARCH_AND_GOTO
= 1 << 4,
1381 ALLOW_SPLIT
= 1 << 5,
1385 /* Empty added files have no hunks */
1386 if (!file_diff
->hunk_nr
&& !file_diff
->added
)
1389 strbuf_reset(&s
->buf
);
1390 render_diff_header(s
, file_diff
, colored
, &s
->buf
);
1391 fputs(s
->buf
.buf
, stdout
);
1393 if (hunk_index
>= file_diff
->hunk_nr
)
1395 hunk
= file_diff
->hunk_nr
1396 ? file_diff
->hunk
+ hunk_index
1398 undecided_previous
= -1;
1399 undecided_next
= -1;
1401 if (file_diff
->hunk_nr
) {
1402 for (i
= hunk_index
- 1; i
>= 0; i
--)
1403 if (file_diff
->hunk
[i
].use
== UNDECIDED_HUNK
) {
1404 undecided_previous
= i
;
1408 for (i
= hunk_index
+ 1; i
< file_diff
->hunk_nr
; i
++)
1409 if (file_diff
->hunk
[i
].use
== UNDECIDED_HUNK
) {
1415 /* Everything decided? */
1416 if (undecided_previous
< 0 && undecided_next
< 0 &&
1417 hunk
->use
!= UNDECIDED_HUNK
)
1420 strbuf_reset(&s
->buf
);
1421 if (file_diff
->hunk_nr
) {
1422 render_hunk(s
, hunk
, 0, colored
, &s
->buf
);
1423 fputs(s
->buf
.buf
, stdout
);
1425 strbuf_reset(&s
->buf
);
1426 if (undecided_previous
>= 0) {
1427 permitted
|= ALLOW_GOTO_PREVIOUS_UNDECIDED_HUNK
;
1428 strbuf_addstr(&s
->buf
, ",k");
1431 permitted
|= ALLOW_GOTO_PREVIOUS_HUNK
;
1432 strbuf_addstr(&s
->buf
, ",K");
1434 if (undecided_next
>= 0) {
1435 permitted
|= ALLOW_GOTO_NEXT_UNDECIDED_HUNK
;
1436 strbuf_addstr(&s
->buf
, ",j");
1438 if (hunk_index
+ 1 < file_diff
->hunk_nr
) {
1439 permitted
|= ALLOW_GOTO_NEXT_HUNK
;
1440 strbuf_addstr(&s
->buf
, ",J");
1442 if (file_diff
->hunk_nr
> 1) {
1443 permitted
|= ALLOW_SEARCH_AND_GOTO
;
1444 strbuf_addstr(&s
->buf
, ",g,/");
1446 if (hunk
->splittable_into
> 1) {
1447 permitted
|= ALLOW_SPLIT
;
1448 strbuf_addstr(&s
->buf
, ",s");
1450 if (hunk_index
+ 1 > file_diff
->mode_change
&&
1451 !file_diff
->deleted
) {
1452 permitted
|= ALLOW_EDIT
;
1453 strbuf_addstr(&s
->buf
, ",e");
1456 if (file_diff
->deleted
)
1457 prompt_mode_type
= PROMPT_DELETION
;
1458 else if (file_diff
->added
)
1459 prompt_mode_type
= PROMPT_ADDITION
;
1460 else if (file_diff
->mode_change
&& !hunk_index
)
1461 prompt_mode_type
= PROMPT_MODE_CHANGE
;
1463 prompt_mode_type
= PROMPT_HUNK
;
1465 printf("%s(%"PRIuMAX
"/%"PRIuMAX
") ", s
->s
.prompt_color
,
1466 (uintmax_t)hunk_index
+ 1,
1467 (uintmax_t)(file_diff
->hunk_nr
1468 ? file_diff
->hunk_nr
1470 printf(_(s
->mode
->prompt_mode
[prompt_mode_type
]),
1472 if (*s
->s
.reset_color
)
1473 fputs(s
->s
.reset_color
, stdout
);
1475 if (read_single_character(s
) == EOF
)
1480 ch
= tolower(s
->answer
.buf
[0]);
1482 hunk
->use
= USE_HUNK
;
1484 hunk_index
= undecided_next
< 0 ?
1485 file_diff
->hunk_nr
: undecided_next
;
1486 } else if (ch
== 'n') {
1487 hunk
->use
= SKIP_HUNK
;
1488 goto soft_increment
;
1489 } else if (ch
== 'a') {
1490 if (file_diff
->hunk_nr
) {
1491 for (; hunk_index
< file_diff
->hunk_nr
; hunk_index
++) {
1492 hunk
= file_diff
->hunk
+ hunk_index
;
1493 if (hunk
->use
== UNDECIDED_HUNK
)
1494 hunk
->use
= USE_HUNK
;
1496 } else if (hunk
->use
== UNDECIDED_HUNK
) {
1497 hunk
->use
= USE_HUNK
;
1499 } else if (ch
== 'd' || ch
== 'q') {
1500 if (file_diff
->hunk_nr
) {
1501 for (; hunk_index
< file_diff
->hunk_nr
; hunk_index
++) {
1502 hunk
= file_diff
->hunk
+ hunk_index
;
1503 if (hunk
->use
== UNDECIDED_HUNK
)
1504 hunk
->use
= SKIP_HUNK
;
1506 } else if (hunk
->use
== UNDECIDED_HUNK
) {
1507 hunk
->use
= SKIP_HUNK
;
1513 } else if (s
->answer
.buf
[0] == 'K') {
1514 if (permitted
& ALLOW_GOTO_PREVIOUS_HUNK
)
1517 err(s
, _("No previous hunk"));
1518 } else if (s
->answer
.buf
[0] == 'J') {
1519 if (permitted
& ALLOW_GOTO_NEXT_HUNK
)
1522 err(s
, _("No next hunk"));
1523 } else if (s
->answer
.buf
[0] == 'k') {
1524 if (permitted
& ALLOW_GOTO_PREVIOUS_UNDECIDED_HUNK
)
1525 hunk_index
= undecided_previous
;
1527 err(s
, _("No previous hunk"));
1528 } else if (s
->answer
.buf
[0] == 'j') {
1529 if (permitted
& ALLOW_GOTO_NEXT_UNDECIDED_HUNK
)
1530 hunk_index
= undecided_next
;
1532 err(s
, _("No next hunk"));
1533 } else if (s
->answer
.buf
[0] == 'g') {
1535 unsigned long response
;
1537 if (!(permitted
& ALLOW_SEARCH_AND_GOTO
)) {
1538 err(s
, _("No other hunks to goto"));
1541 strbuf_remove(&s
->answer
, 0, 1);
1542 strbuf_trim(&s
->answer
);
1543 i
= hunk_index
- DISPLAY_HUNKS_LINES
/ 2;
1544 if (i
< file_diff
->mode_change
)
1545 i
= file_diff
->mode_change
;
1546 while (s
->answer
.len
== 0) {
1547 i
= display_hunks(s
, file_diff
, i
);
1548 printf("%s", i
< file_diff
->hunk_nr
?
1549 _("go to which hunk (<ret> to see "
1550 "more)? ") : _("go to which hunk? "));
1552 if (strbuf_getline(&s
->answer
,
1555 strbuf_trim_trailing_newline(&s
->answer
);
1558 strbuf_trim(&s
->answer
);
1559 response
= strtoul(s
->answer
.buf
, &pend
, 10);
1560 if (*pend
|| pend
== s
->answer
.buf
)
1561 err(s
, _("Invalid number: '%s'"),
1563 else if (0 < response
&& response
<= file_diff
->hunk_nr
)
1564 hunk_index
= response
- 1;
1566 err(s
, Q_("Sorry, only %d hunk available.",
1567 "Sorry, only %d hunks available.",
1568 file_diff
->hunk_nr
),
1569 (int)file_diff
->hunk_nr
);
1570 } else if (s
->answer
.buf
[0] == '/') {
1574 if (!(permitted
& ALLOW_SEARCH_AND_GOTO
)) {
1575 err(s
, _("No other hunks to search"));
1578 strbuf_remove(&s
->answer
, 0, 1);
1579 strbuf_trim_trailing_newline(&s
->answer
);
1580 if (s
->answer
.len
== 0) {
1581 printf("%s", _("search for regex? "));
1583 if (strbuf_getline(&s
->answer
,
1586 strbuf_trim_trailing_newline(&s
->answer
);
1587 if (s
->answer
.len
== 0)
1590 ret
= regcomp(®ex
, s
->answer
.buf
,
1591 REG_EXTENDED
| REG_NOSUB
| REG_NEWLINE
);
1595 regerror(ret
, ®ex
, errbuf
, sizeof(errbuf
));
1596 err(s
, _("Malformed search regexp %s: %s"),
1597 s
->answer
.buf
, errbuf
);
1602 /* render the hunk into a scratch buffer */
1603 render_hunk(s
, file_diff
->hunk
+ i
, 0, 0,
1605 if (regexec(®ex
, s
->buf
.buf
, 0, NULL
, 0)
1609 if (i
== file_diff
->hunk_nr
)
1611 if (i
!= hunk_index
)
1613 err(s
, _("No hunk matches the given pattern"));
1617 } else if (s
->answer
.buf
[0] == 's') {
1618 size_t splittable_into
= hunk
->splittable_into
;
1619 if (!(permitted
& ALLOW_SPLIT
))
1620 err(s
, _("Sorry, cannot split this hunk"));
1621 else if (!split_hunk(s
, file_diff
,
1622 hunk
- file_diff
->hunk
))
1623 color_fprintf_ln(stdout
, s
->s
.header_color
,
1624 _("Split into %d hunks."),
1625 (int)splittable_into
);
1626 } else if (s
->answer
.buf
[0] == 'e') {
1627 if (!(permitted
& ALLOW_EDIT
))
1628 err(s
, _("Sorry, cannot edit this hunk"));
1629 else if (edit_hunk_loop(s
, file_diff
, hunk
) >= 0) {
1630 hunk
->use
= USE_HUNK
;
1631 goto soft_increment
;
1634 const char *p
= _(help_patch_remainder
), *eol
= p
;
1636 color_fprintf(stdout
, s
->s
.help_color
, "%s",
1637 _(s
->mode
->help_patch_text
));
1640 * Show only those lines of the remainder that are
1641 * actually applicable with the current hunk.
1643 for (; *p
; p
= eol
+ (*eol
== '\n')) {
1644 eol
= strchrnul(p
, '\n');
1647 * `s->buf` still contains the part of the
1648 * commands shown in the prompt that are not
1651 if (*p
!= '?' && !strchr(s
->buf
.buf
, *p
))
1654 color_fprintf_ln(stdout
, s
->s
.help_color
,
1655 "%.*s", (int)(eol
- p
), p
);
1660 /* Any hunk to be used? */
1661 for (i
= 0; i
< file_diff
->hunk_nr
; i
++)
1662 if (file_diff
->hunk
[i
].use
== USE_HUNK
)
1665 if (i
< file_diff
->hunk_nr
||
1666 (!file_diff
->hunk_nr
&& file_diff
->head
.use
== USE_HUNK
)) {
1667 /* At least one hunk selected: apply */
1668 strbuf_reset(&s
->buf
);
1669 reassemble_patch(s
, file_diff
, 0, &s
->buf
);
1671 discard_index(s
->s
.r
->index
);
1672 if (s
->mode
->apply_for_checkout
)
1673 apply_for_checkout(s
, &s
->buf
,
1674 s
->mode
->is_reverse
);
1676 setup_child_process(s
, &cp
, "apply", NULL
);
1677 strvec_pushv(&cp
.args
, s
->mode
->apply_args
);
1678 if (pipe_command(&cp
, s
->buf
.buf
, s
->buf
.len
,
1680 error(_("'git apply' failed"));
1682 if (repo_read_index(s
->s
.r
) >= 0)
1683 repo_refresh_and_write_index(s
->s
.r
, REFRESH_QUIET
, 0,
1684 1, NULL
, NULL
, NULL
);
1691 int run_add_p(struct repository
*r
, enum add_p_mode mode
,
1692 const char *revision
, const struct pathspec
*ps
)
1694 struct add_p_state s
= {
1695 { r
}, STRBUF_INIT
, STRBUF_INIT
, STRBUF_INIT
, STRBUF_INIT
1697 size_t i
, binary_count
= 0;
1699 init_add_i_state(&s
.s
, r
);
1701 if (mode
== ADD_P_STASH
)
1702 s
.mode
= &patch_mode_stash
;
1703 else if (mode
== ADD_P_RESET
) {
1705 * NEEDSWORK: Instead of comparing to the literal "HEAD",
1706 * compare the commit objects instead so that other ways of
1707 * saying the same thing (such as "@") are also handled
1710 * This applies to the cases below too.
1712 if (!revision
|| !strcmp(revision
, "HEAD"))
1713 s
.mode
= &patch_mode_reset_head
;
1715 s
.mode
= &patch_mode_reset_nothead
;
1716 } else if (mode
== ADD_P_CHECKOUT
) {
1718 s
.mode
= &patch_mode_checkout_index
;
1719 else if (!strcmp(revision
, "HEAD"))
1720 s
.mode
= &patch_mode_checkout_head
;
1722 s
.mode
= &patch_mode_checkout_nothead
;
1723 } else if (mode
== ADD_P_WORKTREE
) {
1725 s
.mode
= &patch_mode_checkout_index
;
1726 else if (!strcmp(revision
, "HEAD"))
1727 s
.mode
= &patch_mode_worktree_head
;
1729 s
.mode
= &patch_mode_worktree_nothead
;
1731 s
.mode
= &patch_mode_add
;
1732 s
.revision
= revision
;
1734 if (discard_index(r
->index
) < 0 || repo_read_index(r
) < 0 ||
1735 (!s
.mode
->index_only
&&
1736 repo_refresh_and_write_index(r
, REFRESH_QUIET
, 0, 1,
1737 NULL
, NULL
, NULL
) < 0) ||
1738 parse_diff(&s
, ps
) < 0) {
1739 add_p_state_clear(&s
);
1743 for (i
= 0; i
< s
.file_diff_nr
; i
++)
1744 if (s
.file_diff
[i
].binary
&& !s
.file_diff
[i
].hunk_nr
)
1746 else if (patch_update_file(&s
, s
.file_diff
+ i
))
1749 if (s
.file_diff_nr
== 0)
1750 fprintf(stderr
, _("No changes.\n"));
1751 else if (binary_count
== s
.file_diff_nr
)
1752 fprintf(stderr
, _("Only binary files changed.\n"));
1754 add_p_state_clear(&s
);