1 #include "git-compat-util.h"
2 #include "add-interactive.h"
5 #include "environment.h"
7 #include "object-name.h"
8 #include "read-cache-ll.h"
9 #include "repository.h"
11 #include "run-command.h"
15 #include "compat/terminal.h"
18 enum prompt_mode_type
{
19 PROMPT_MODE_CHANGE
= 0, PROMPT_DELETION
, PROMPT_ADDITION
, PROMPT_HUNK
,
20 PROMPT_MODE_MAX
, /* must be last */
25 * The magic constant 4 is chosen such that all patch modes
26 * provide enough space for three command-line arguments followed by a
29 const char *diff_cmd
[4], *apply_args
[4], *apply_check_args
[4];
30 unsigned is_reverse
:1, index_only
:1, apply_for_checkout
:1;
31 const char *prompt_mode
[PROMPT_MODE_MAX
];
32 const char *edit_hunk_hint
, *help_patch_text
;
35 static struct patch_mode patch_mode_add
= {
36 .diff_cmd
= { "diff-files", NULL
},
37 .apply_args
= { "--cached", NULL
},
38 .apply_check_args
= { "--cached", NULL
},
40 N_("Stage mode change [y,n,q,a,d%s,?]? "),
41 N_("Stage deletion [y,n,q,a,d%s,?]? "),
42 N_("Stage addition [y,n,q,a,d%s,?]? "),
43 N_("Stage this hunk [y,n,q,a,d%s,?]? ")
45 .edit_hunk_hint
= N_("If the patch applies cleanly, the edited hunk "
46 "will immediately be marked for staging."),
48 N_("y - stage this hunk\n"
49 "n - do not stage this hunk\n"
50 "q - quit; do not stage this hunk or any of the remaining "
52 "a - stage this hunk and all later hunks in the file\n"
53 "d - do not stage this hunk or any of the later hunks in "
57 static struct patch_mode patch_mode_stash
= {
58 .diff_cmd
= { "diff-index", "HEAD", NULL
},
59 .apply_args
= { "--cached", NULL
},
60 .apply_check_args
= { "--cached", NULL
},
62 N_("Stash mode change [y,n,q,a,d%s,?]? "),
63 N_("Stash deletion [y,n,q,a,d%s,?]? "),
64 N_("Stash addition [y,n,q,a,d%s,?]? "),
65 N_("Stash this hunk [y,n,q,a,d%s,?]? "),
67 .edit_hunk_hint
= N_("If the patch applies cleanly, the edited hunk "
68 "will immediately be marked for stashing."),
70 N_("y - stash this hunk\n"
71 "n - do not stash this hunk\n"
72 "q - quit; do not stash this hunk or any of the remaining "
74 "a - stash this hunk and all later hunks in the file\n"
75 "d - do not stash this hunk or any of the later hunks in "
79 static struct patch_mode patch_mode_reset_head
= {
80 .diff_cmd
= { "diff-index", "--cached", NULL
},
81 .apply_args
= { "-R", "--cached", NULL
},
82 .apply_check_args
= { "-R", "--cached", NULL
},
86 N_("Unstage mode change [y,n,q,a,d%s,?]? "),
87 N_("Unstage deletion [y,n,q,a,d%s,?]? "),
88 N_("Unstage addition [y,n,q,a,d%s,?]? "),
89 N_("Unstage this hunk [y,n,q,a,d%s,?]? "),
91 .edit_hunk_hint
= N_("If the patch applies cleanly, the edited hunk "
92 "will immediately be marked for unstaging."),
94 N_("y - unstage this hunk\n"
95 "n - do not unstage this hunk\n"
96 "q - quit; do not unstage this hunk or any of the remaining "
98 "a - unstage this hunk and all later hunks in the file\n"
99 "d - do not unstage this hunk or any of the later hunks in "
103 static struct patch_mode patch_mode_reset_nothead
= {
104 .diff_cmd
= { "diff-index", "-R", "--cached", NULL
},
105 .apply_args
= { "--cached", NULL
},
106 .apply_check_args
= { "--cached", NULL
},
109 N_("Apply mode change to index [y,n,q,a,d%s,?]? "),
110 N_("Apply deletion to index [y,n,q,a,d%s,?]? "),
111 N_("Apply addition to index [y,n,q,a,d%s,?]? "),
112 N_("Apply this hunk to index [y,n,q,a,d%s,?]? "),
114 .edit_hunk_hint
= N_("If the patch applies cleanly, the edited hunk "
115 "will immediately be marked for applying."),
117 N_("y - apply this hunk to index\n"
118 "n - do not apply this hunk to index\n"
119 "q - quit; do not apply this hunk or any of the remaining "
121 "a - apply this hunk and all later hunks in the file\n"
122 "d - do not apply this hunk or any of the later hunks in "
126 static struct patch_mode patch_mode_checkout_index
= {
127 .diff_cmd
= { "diff-files", NULL
},
128 .apply_args
= { "-R", NULL
},
129 .apply_check_args
= { "-R", NULL
},
132 N_("Discard mode change from worktree [y,n,q,a,d%s,?]? "),
133 N_("Discard deletion from worktree [y,n,q,a,d%s,?]? "),
134 N_("Discard addition from worktree [y,n,q,a,d%s,?]? "),
135 N_("Discard this hunk from worktree [y,n,q,a,d%s,?]? "),
137 .edit_hunk_hint
= N_("If the patch applies cleanly, the edited hunk "
138 "will immediately be marked for discarding."),
140 N_("y - discard this hunk from worktree\n"
141 "n - do not discard this hunk from worktree\n"
142 "q - quit; do not discard this hunk or any of the remaining "
144 "a - discard this hunk and all later hunks in the file\n"
145 "d - do not discard this hunk or any of the later hunks in "
149 static struct patch_mode patch_mode_checkout_head
= {
150 .diff_cmd
= { "diff-index", NULL
},
151 .apply_for_checkout
= 1,
152 .apply_check_args
= { "-R", NULL
},
155 N_("Discard mode change from index and worktree [y,n,q,a,d%s,?]? "),
156 N_("Discard deletion from index and worktree [y,n,q,a,d%s,?]? "),
157 N_("Discard addition from index and worktree [y,n,q,a,d%s,?]? "),
158 N_("Discard this hunk from index and worktree [y,n,q,a,d%s,?]? "),
160 .edit_hunk_hint
= N_("If the patch applies cleanly, the edited hunk "
161 "will immediately be marked for discarding."),
163 N_("y - discard this hunk from index and worktree\n"
164 "n - do not discard this hunk from index and worktree\n"
165 "q - quit; do not discard this hunk or any of the remaining "
167 "a - discard this hunk and all later hunks in the file\n"
168 "d - do not discard this hunk or any of the later hunks in "
172 static struct patch_mode patch_mode_checkout_nothead
= {
173 .diff_cmd
= { "diff-index", "-R", NULL
},
174 .apply_for_checkout
= 1,
175 .apply_check_args
= { NULL
},
177 N_("Apply mode change to index and worktree [y,n,q,a,d%s,?]? "),
178 N_("Apply deletion to index and worktree [y,n,q,a,d%s,?]? "),
179 N_("Apply addition to index and worktree [y,n,q,a,d%s,?]? "),
180 N_("Apply this hunk to index and worktree [y,n,q,a,d%s,?]? "),
182 .edit_hunk_hint
= N_("If the patch applies cleanly, the edited hunk "
183 "will immediately be marked for applying."),
185 N_("y - apply this hunk to index and worktree\n"
186 "n - do not apply this hunk to index and worktree\n"
187 "q - quit; do not apply this hunk or any of the remaining "
189 "a - apply this hunk and all later hunks in the file\n"
190 "d - do not apply this hunk or any of the later hunks in "
194 static struct patch_mode patch_mode_worktree_head
= {
195 .diff_cmd
= { "diff-index", NULL
},
196 .apply_args
= { "-R", NULL
},
197 .apply_check_args
= { "-R", NULL
},
200 N_("Discard mode change from worktree [y,n,q,a,d%s,?]? "),
201 N_("Discard deletion from worktree [y,n,q,a,d%s,?]? "),
202 N_("Discard addition from worktree [y,n,q,a,d%s,?]? "),
203 N_("Discard this hunk from worktree [y,n,q,a,d%s,?]? "),
205 .edit_hunk_hint
= N_("If the patch applies cleanly, the edited hunk "
206 "will immediately be marked for discarding."),
208 N_("y - discard this hunk from worktree\n"
209 "n - do not discard this hunk from worktree\n"
210 "q - quit; do not discard this hunk or any of the remaining "
212 "a - discard this hunk and all later hunks in the file\n"
213 "d - do not discard this hunk or any of the later hunks in "
217 static struct patch_mode patch_mode_worktree_nothead
= {
218 .diff_cmd
= { "diff-index", "-R", NULL
},
219 .apply_args
= { NULL
},
220 .apply_check_args
= { NULL
},
222 N_("Apply mode change to worktree [y,n,q,a,d%s,?]? "),
223 N_("Apply deletion to worktree [y,n,q,a,d%s,?]? "),
224 N_("Apply addition to worktree [y,n,q,a,d%s,?]? "),
225 N_("Apply this hunk to worktree [y,n,q,a,d%s,?]? "),
227 .edit_hunk_hint
= N_("If the patch applies cleanly, the edited hunk "
228 "will immediately be marked for applying."),
230 N_("y - apply this hunk to worktree\n"
231 "n - do not apply this hunk to worktree\n"
232 "q - quit; do not apply this hunk or any of the remaining "
234 "a - apply this hunk and all later hunks in the file\n"
235 "d - do not apply this hunk or any of the later hunks in "
240 unsigned long old_offset
, old_count
, new_offset
, new_count
;
242 * Start/end offsets to the extra text after the second `@@` in the
243 * hunk header, e.g. the function signature. This is expected to
244 * include the newline.
246 size_t extra_start
, extra_end
, colored_extra_start
, colored_extra_end
;
247 unsigned suppress_colored_line_range
:1;
251 size_t start
, end
, colored_start
, colored_end
, splittable_into
;
253 enum { UNDECIDED_HUNK
= 0, SKIP_HUNK
, USE_HUNK
} use
;
254 struct hunk_header header
;
258 struct add_i_state s
;
259 struct strbuf answer
, buf
;
262 struct strbuf plain
, colored
;
266 size_t hunk_nr
, hunk_alloc
;
267 unsigned deleted
:1, added
:1, mode_change
:1,binary
:1;
272 struct patch_mode
*mode
;
273 const char *revision
;
276 static void add_p_state_clear(struct add_p_state
*s
)
280 strbuf_release(&s
->answer
);
281 strbuf_release(&s
->buf
);
282 strbuf_release(&s
->plain
);
283 strbuf_release(&s
->colored
);
284 for (i
= 0; i
< s
->file_diff_nr
; i
++)
285 free(s
->file_diff
[i
].hunk
);
287 clear_add_i_state(&s
->s
);
290 __attribute__((format (printf
, 2, 3)))
291 static void err(struct add_p_state
*s
, const char *fmt
, ...)
296 fputs(s
->s
.error_color
, stdout
);
298 puts(s
->s
.reset_color
);
302 static void setup_child_process(struct add_p_state
*s
,
303 struct child_process
*cp
, ...)
309 while ((arg
= va_arg(ap
, const char *)))
310 strvec_push(&cp
->args
, arg
);
314 strvec_pushf(&cp
->env
,
315 INDEX_ENVIRONMENT
"=%s", s
->s
.r
->index_file
);
318 static int parse_range(const char **p
,
319 unsigned long *offset
, unsigned long *count
)
323 *offset
= strtoul(*p
, &pend
, 10);
331 *count
= strtoul(pend
+ 1, (char **)p
, 10);
332 return *p
== pend
+ 1 ? -1 : 0;
335 static int parse_hunk_header(struct add_p_state
*s
, struct hunk
*hunk
)
337 struct hunk_header
*header
= &hunk
->header
;
338 const char *line
= s
->plain
.buf
+ hunk
->start
, *p
= line
;
339 char *eol
= memchr(p
, '\n', s
->plain
.len
- hunk
->start
);
342 eol
= s
->plain
.buf
+ s
->plain
.len
;
344 if (!skip_prefix(p
, "@@ -", &p
) ||
345 parse_range(&p
, &header
->old_offset
, &header
->old_count
) < 0 ||
346 !skip_prefix(p
, " +", &p
) ||
347 parse_range(&p
, &header
->new_offset
, &header
->new_count
) < 0 ||
348 !skip_prefix(p
, " @@", &p
))
349 return error(_("could not parse hunk header '%.*s'"),
350 (int)(eol
- line
), line
);
352 hunk
->start
= eol
- s
->plain
.buf
+ (*eol
== '\n');
353 header
->extra_start
= p
- s
->plain
.buf
;
354 header
->extra_end
= hunk
->start
;
356 if (!s
->colored
.len
) {
357 header
->colored_extra_start
= header
->colored_extra_end
= 0;
361 /* Now find the extra text in the colored diff */
362 line
= s
->colored
.buf
+ hunk
->colored_start
;
363 eol
= memchr(line
, '\n', s
->colored
.len
- hunk
->colored_start
);
365 eol
= s
->colored
.buf
+ s
->colored
.len
;
366 p
= memmem(line
, eol
- line
, "@@ -", 4);
367 if (p
&& (p
= memmem(p
+ 4, eol
- p
- 4, " @@", 3))) {
368 header
->colored_extra_start
= p
+ 3 - s
->colored
.buf
;
370 /* could not parse colored hunk header, leave as-is */
371 header
->colored_extra_start
= hunk
->colored_start
;
372 header
->suppress_colored_line_range
= 1;
374 hunk
->colored_start
= eol
- s
->colored
.buf
+ (*eol
== '\n');
375 header
->colored_extra_end
= hunk
->colored_start
;
380 static int is_octal(const char *p
, size_t len
)
386 if (*p
< '0' || *(p
++) > '7')
391 static void complete_file(char marker
, struct hunk
*hunk
)
393 if (marker
== '-' || marker
== '+')
395 * Last hunk ended in non-context line (i.e. it
396 * appended lines to the file, so there are no
397 * trailing context lines).
399 hunk
->splittable_into
++;
402 static int parse_diff(struct add_p_state
*s
, const struct pathspec
*ps
)
404 struct strvec args
= STRVEC_INIT
;
405 const char *diff_algorithm
= s
->s
.interactive_diff_algorithm
;
406 struct strbuf
*plain
= &s
->plain
, *colored
= NULL
;
407 struct child_process cp
= CHILD_PROCESS_INIT
;
408 char *p
, *pend
, *colored_p
= NULL
, *colored_pend
= NULL
, marker
= '\0';
409 size_t file_diff_alloc
= 0, i
, color_arg_index
;
410 struct file_diff
*file_diff
= NULL
;
411 struct hunk
*hunk
= NULL
;
414 strvec_pushv(&args
, s
->mode
->diff_cmd
);
416 strvec_pushf(&args
, "--diff-algorithm=%s", diff_algorithm
);
418 struct object_id oid
;
420 /* could be on an unborn branch */
421 !strcmp("HEAD", s
->revision
) &&
422 repo_get_oid(the_repository
, "HEAD", &oid
) ?
423 empty_tree_oid_hex() : s
->revision
);
425 color_arg_index
= args
.nr
;
426 /* Use `--no-color` explicitly, just in case `diff.color = always`. */
427 strvec_pushl(&args
, "--no-color", "--ignore-submodules=dirty", "-p",
429 for (i
= 0; i
< ps
->nr
; i
++)
430 strvec_push(&args
, ps
->items
[i
].original
);
432 setup_child_process(s
, &cp
, NULL
);
433 strvec_pushv(&cp
.args
, args
.v
);
434 res
= capture_command(&cp
, plain
, 0);
437 return error(_("could not parse diff"));
443 strbuf_complete_line(plain
);
445 if (want_color_fd(1, -1)) {
446 struct child_process colored_cp
= CHILD_PROCESS_INIT
;
447 const char *diff_filter
= s
->s
.interactive_diff_filter
;
449 setup_child_process(s
, &colored_cp
, NULL
);
450 xsnprintf((char *)args
.v
[color_arg_index
], 8, "--color");
451 strvec_pushv(&colored_cp
.args
, args
.v
);
452 colored
= &s
->colored
;
453 res
= capture_command(&colored_cp
, colored
, 0);
456 return error(_("could not parse colored diff"));
459 struct child_process filter_cp
= CHILD_PROCESS_INIT
;
461 setup_child_process(s
, &filter_cp
,
463 filter_cp
.git_cmd
= 0;
464 filter_cp
.use_shell
= 1;
465 strbuf_reset(&s
->buf
);
466 if (pipe_command(&filter_cp
,
467 colored
->buf
, colored
->len
,
468 &s
->buf
, colored
->len
,
470 return error(_("failed to run '%s'"),
472 strbuf_swap(colored
, &s
->buf
);
475 strbuf_complete_line(colored
);
476 colored_p
= colored
->buf
;
477 colored_pend
= colored_p
+ colored
->len
;
481 /* parse files and hunks */
483 pend
= p
+ plain
->len
;
485 char *eol
= memchr(p
, '\n', pend
- p
);
486 const char *deleted
= NULL
, *mode_change
= NULL
;
491 if (starts_with(p
, "diff ") ||
492 starts_with(p
, "* Unmerged path ")) {
493 complete_file(marker
, hunk
);
494 ALLOC_GROW_BY(s
->file_diff
, s
->file_diff_nr
, 1,
496 file_diff
= s
->file_diff
+ s
->file_diff_nr
- 1;
497 hunk
= &file_diff
->head
;
498 hunk
->start
= p
- plain
->buf
;
500 hunk
->colored_start
= colored_p
- colored
->buf
;
502 } else if (p
== plain
->buf
)
503 BUG("diff starts with unexpected line:\n"
504 "%.*s\n", (int)(eol
- p
), p
);
505 else if (file_diff
->deleted
)
506 ; /* keep the rest of the file in a single "hunk" */
507 else if (starts_with(p
, "@@ ") ||
508 (hunk
== &file_diff
->head
&&
509 (skip_prefix(p
, "deleted file", &deleted
)))) {
510 if (marker
== '-' || marker
== '+')
512 * Should not happen; previous hunk did not end
513 * in a context line? Handle it anyway.
515 hunk
->splittable_into
++;
517 ALLOC_GROW_BY(file_diff
->hunk
, file_diff
->hunk_nr
, 1,
518 file_diff
->hunk_alloc
);
519 hunk
= file_diff
->hunk
+ file_diff
->hunk_nr
- 1;
521 hunk
->start
= p
- plain
->buf
;
523 hunk
->colored_start
= colored_p
- colored
->buf
;
526 file_diff
->deleted
= 1;
527 else if (parse_hunk_header(s
, hunk
) < 0)
531 * Start counting into how many hunks this one can be
535 } else if (hunk
== &file_diff
->head
&&
536 starts_with(p
, "new file")) {
537 file_diff
->added
= 1;
538 } else if (hunk
== &file_diff
->head
&&
539 skip_prefix(p
, "old mode ", &mode_change
) &&
540 is_octal(mode_change
, eol
- mode_change
)) {
541 if (file_diff
->mode_change
)
542 BUG("double mode change?\n\n%.*s",
543 (int)(eol
- plain
->buf
), plain
->buf
);
544 if (file_diff
->hunk_nr
)
545 BUG("mode change in the middle?\n\n%.*s",
546 (int)(eol
- plain
->buf
), plain
->buf
);
549 * Do *not* change `hunk`: the mode change pseudo-hunk
550 * is _part of_ the header "hunk".
552 file_diff
->mode_change
= 1;
553 ALLOC_GROW_BY(file_diff
->hunk
, file_diff
->hunk_nr
, 1,
554 file_diff
->hunk_alloc
);
555 file_diff
->hunk
->start
= p
- plain
->buf
;
557 file_diff
->hunk
->colored_start
=
558 colored_p
- colored
->buf
;
559 } else if (hunk
== &file_diff
->head
&&
560 skip_prefix(p
, "new mode ", &mode_change
) &&
561 is_octal(mode_change
, eol
- mode_change
)) {
564 * Extend the "mode change" pseudo-hunk to include also
565 * the "new mode" line.
567 if (!file_diff
->mode_change
)
568 BUG("'new mode' without 'old mode'?\n\n%.*s",
569 (int)(eol
- plain
->buf
), plain
->buf
);
570 if (file_diff
->hunk_nr
!= 1)
571 BUG("mode change in the middle?\n\n%.*s",
572 (int)(eol
- plain
->buf
), plain
->buf
);
573 if (p
- plain
->buf
!= file_diff
->hunk
->end
)
574 BUG("'new mode' does not immediately follow "
575 "'old mode'?\n\n%.*s",
576 (int)(eol
- plain
->buf
), plain
->buf
);
577 } else if (hunk
== &file_diff
->head
&&
578 starts_with(p
, "Binary files "))
579 file_diff
->binary
= 1;
581 if (!!file_diff
->deleted
+ !!file_diff
->added
+
582 !!file_diff
->mode_change
> 1)
583 BUG("diff can only contain delete *or* add *or* a "
584 "mode change?!?\n%.*s",
585 (int)(eol
- (plain
->buf
+ file_diff
->head
.start
)),
586 plain
->buf
+ file_diff
->head
.start
);
588 if ((marker
== '-' || marker
== '+') && *p
== ' ')
589 hunk
->splittable_into
++;
590 if (marker
&& *p
!= '\\')
593 p
= eol
== pend
? pend
: eol
+ 1;
594 hunk
->end
= p
- plain
->buf
;
597 char *colored_eol
= memchr(colored_p
, '\n',
598 colored_pend
- colored_p
);
600 colored_p
= colored_eol
+ 1;
602 /* non-colored has more lines? */
603 goto mismatched_output
;
604 else if (colored_p
== colored_pend
)
605 /* last line has no matching colored one? */
606 goto mismatched_output
;
608 colored_p
= colored_pend
;
610 hunk
->colored_end
= colored_p
- colored
->buf
;
614 if (file_diff
->hunk_nr
!= 1)
615 BUG("mode change in hunk #%d???",
616 (int)file_diff
->hunk_nr
);
617 /* Adjust the end of the "mode change" pseudo-hunk */
618 file_diff
->hunk
->end
= hunk
->end
;
620 file_diff
->hunk
->colored_end
= hunk
->colored_end
;
623 complete_file(marker
, hunk
);
625 /* non-colored shorter than colored? */
626 if (colored_p
!= colored_pend
) {
628 error(_("mismatched output from interactive.diffFilter"));
629 advise(_("Your filter must maintain a one-to-one correspondence\n"
630 "between its input and output lines."));
637 static size_t find_next_line(struct strbuf
*sb
, size_t offset
)
641 if (offset
>= sb
->len
)
642 BUG("looking for next line beyond buffer (%d >= %d)\n%s",
643 (int)offset
, (int)sb
->len
, sb
->buf
);
645 eol
= memchr(sb
->buf
+ offset
, '\n', sb
->len
- offset
);
648 return eol
- sb
->buf
+ 1;
651 static void render_hunk(struct add_p_state
*s
, struct hunk
*hunk
,
652 ssize_t delta
, int colored
, struct strbuf
*out
)
654 struct hunk_header
*header
= &hunk
->header
;
656 if (hunk
->header
.old_offset
!= 0 || hunk
->header
.new_offset
!= 0) {
658 * Generate the hunk header dynamically, except for special
659 * hunks (such as the diff header).
663 unsigned long old_offset
= header
->old_offset
;
664 unsigned long new_offset
= header
->new_offset
;
667 p
= s
->plain
.buf
+ header
->extra_start
;
668 len
= header
->extra_end
- header
->extra_start
;
669 } else if (header
->suppress_colored_line_range
) {
671 s
->colored
.buf
+ header
->colored_extra_start
,
672 header
->colored_extra_end
-
673 header
->colored_extra_start
);
675 strbuf_add(out
, s
->colored
.buf
+ hunk
->colored_start
,
676 hunk
->colored_end
- hunk
->colored_start
);
679 strbuf_addstr(out
, s
->s
.fraginfo_color
);
680 p
= s
->colored
.buf
+ header
->colored_extra_start
;
681 len
= header
->colored_extra_end
682 - header
->colored_extra_start
;
685 if (s
->mode
->is_reverse
)
690 strbuf_addf(out
, "@@ -%lu", old_offset
);
691 if (header
->old_count
!= 1)
692 strbuf_addf(out
, ",%lu", header
->old_count
);
693 strbuf_addf(out
, " +%lu", new_offset
);
694 if (header
->new_count
!= 1)
695 strbuf_addf(out
, ",%lu", header
->new_count
);
696 strbuf_addstr(out
, " @@");
699 strbuf_add(out
, p
, len
);
701 strbuf_addf(out
, "%s\n", s
->s
.reset_color
);
703 strbuf_addch(out
, '\n');
707 strbuf_add(out
, s
->colored
.buf
+ hunk
->colored_start
,
708 hunk
->colored_end
- hunk
->colored_start
);
710 strbuf_add(out
, s
->plain
.buf
+ hunk
->start
,
711 hunk
->end
- hunk
->start
);
714 static void render_diff_header(struct add_p_state
*s
,
715 struct file_diff
*file_diff
, int colored
,
719 * If there was a mode change, the first hunk is a pseudo hunk that
720 * corresponds to the mode line in the header. If the user did not want
721 * to stage that "hunk", we actually have to cut it out from the header.
723 int skip_mode_change
=
724 file_diff
->mode_change
&& file_diff
->hunk
->use
!= USE_HUNK
;
725 struct hunk
*head
= &file_diff
->head
, *first
= file_diff
->hunk
;
727 if (!skip_mode_change
) {
728 render_hunk(s
, head
, 0, colored
, out
);
733 const char *p
= s
->colored
.buf
;
735 strbuf_add(out
, p
+ head
->colored_start
,
736 first
->colored_start
- head
->colored_start
);
737 strbuf_add(out
, p
+ first
->colored_end
,
738 head
->colored_end
- first
->colored_end
);
740 const char *p
= s
->plain
.buf
;
742 strbuf_add(out
, p
+ head
->start
, first
->start
- head
->start
);
743 strbuf_add(out
, p
+ first
->end
, head
->end
- first
->end
);
747 /* Coalesce hunks again that were split */
748 static int merge_hunks(struct add_p_state
*s
, struct file_diff
*file_diff
,
749 size_t *hunk_index
, int use_all
, struct hunk
*merged
)
751 size_t i
= *hunk_index
, delta
;
752 struct hunk
*hunk
= file_diff
->hunk
+ i
;
753 /* `header` corresponds to the merged hunk */
754 struct hunk_header
*header
= &merged
->header
, *next
;
756 if (!use_all
&& hunk
->use
!= USE_HUNK
)
760 /* We simply skip the colored part (if any) when merging hunks */
761 merged
->colored_start
= merged
->colored_end
= 0;
763 for (; i
+ 1 < file_diff
->hunk_nr
; i
++) {
765 next
= &hunk
->header
;
768 * Stop merging hunks when:
770 * - the hunk is not selected for use, or
771 * - the hunk does not overlap with the already-merged hunk(s)
773 if ((!use_all
&& hunk
->use
!= USE_HUNK
) ||
774 header
->new_offset
>= next
->new_offset
+ merged
->delta
||
775 header
->new_offset
+ header
->new_count
776 < next
->new_offset
+ merged
->delta
)
780 * If the hunks were not edited, and overlap, we can simply
781 * extend the line range.
783 if (merged
->start
< hunk
->start
&& merged
->end
> hunk
->start
) {
784 merged
->end
= hunk
->end
;
785 merged
->colored_end
= hunk
->colored_end
;
788 const char *plain
= s
->plain
.buf
;
789 size_t overlapping_line_count
= header
->new_offset
790 + header
->new_count
- merged
->delta
792 size_t overlap_end
= hunk
->start
;
793 size_t overlap_start
= overlap_end
;
794 size_t overlap_next
, len
, j
;
797 * One of the hunks was edited: the modified hunk was
798 * appended to the strbuf `s->plain`.
800 * Let's ensure that at least the last context line of
801 * the first hunk overlaps with the corresponding line
802 * of the second hunk, and then merge.
804 for (j
= 0; j
< overlapping_line_count
; j
++) {
805 overlap_next
= find_next_line(&s
->plain
,
808 if (overlap_next
> hunk
->end
)
809 BUG("failed to find %d context lines "
811 (int)overlapping_line_count
,
812 (int)(hunk
->end
- hunk
->start
),
813 plain
+ hunk
->start
);
815 if (plain
[overlap_end
] != ' ')
816 return error(_("expected context line "
821 plain
+ hunk
->start
);
823 overlap_start
= overlap_end
;
824 overlap_end
= overlap_next
;
826 len
= overlap_end
- overlap_start
;
828 if (len
> merged
->end
- merged
->start
||
829 memcmp(plain
+ merged
->end
- len
,
830 plain
+ overlap_start
, len
))
831 return error(_("hunks do not overlap:\n%.*s\n"
832 "\tdoes not end with:\n%.*s"),
833 (int)(merged
->end
- merged
->start
),
834 plain
+ merged
->start
,
835 (int)len
, plain
+ overlap_start
);
838 * Since the start-end ranges are not adjacent, we
839 * cannot simply take the union of the ranges. To
840 * address that, we temporarily append the union of the
841 * lines to the `plain` strbuf.
843 if (merged
->end
!= s
->plain
.len
) {
844 size_t start
= s
->plain
.len
;
846 strbuf_add(&s
->plain
, plain
+ merged
->start
,
847 merged
->end
- merged
->start
);
848 plain
= s
->plain
.buf
;
849 merged
->start
= start
;
850 merged
->end
= s
->plain
.len
;
853 strbuf_add(&s
->plain
,
855 hunk
->end
- overlap_end
);
856 merged
->end
= s
->plain
.len
;
857 merged
->splittable_into
+= hunk
->splittable_into
;
858 delta
= merged
->delta
;
859 merged
->delta
+= hunk
->delta
;
862 header
->old_count
= next
->old_offset
+ next
->old_count
863 - header
->old_offset
;
864 header
->new_count
= next
->new_offset
+ delta
865 + next
->new_count
- header
->new_offset
;
868 if (i
== *hunk_index
)
875 static void reassemble_patch(struct add_p_state
*s
,
876 struct file_diff
*file_diff
, int use_all
,
880 size_t save_len
= s
->plain
.len
, i
;
883 render_diff_header(s
, file_diff
, 0, out
);
885 for (i
= file_diff
->mode_change
; i
< file_diff
->hunk_nr
; i
++) {
886 struct hunk merged
= { 0 };
888 hunk
= file_diff
->hunk
+ i
;
889 if (!use_all
&& hunk
->use
!= USE_HUNK
)
890 delta
+= hunk
->header
.old_count
891 - hunk
->header
.new_count
;
893 /* merge overlapping hunks into a temporary hunk */
894 if (merge_hunks(s
, file_diff
, &i
, use_all
, &merged
))
897 render_hunk(s
, hunk
, delta
, 0, out
);
900 * In case `merge_hunks()` used `plain` as a scratch
901 * pad (this happens when an edited hunk had to be
902 * coalesced with another hunk).
904 strbuf_setlen(&s
->plain
, save_len
);
906 delta
+= hunk
->delta
;
911 static int split_hunk(struct add_p_state
*s
, struct file_diff
*file_diff
,
914 int colored
= !!s
->colored
.len
, first
= 1;
915 struct hunk
*hunk
= file_diff
->hunk
+ hunk_index
;
916 size_t splittable_into
;
917 size_t end
, colored_end
, current
, colored_current
= 0, context_line_count
;
918 struct hunk_header remaining
, *header
;
921 if (hunk_index
>= file_diff
->hunk_nr
)
922 BUG("invalid hunk index: %d (must be >= 0 and < %d)",
923 (int)hunk_index
, (int)file_diff
->hunk_nr
);
925 if (hunk
->splittable_into
< 2)
927 splittable_into
= hunk
->splittable_into
;
930 colored_end
= hunk
->colored_end
;
932 remaining
= hunk
->header
;
934 file_diff
->hunk_nr
+= splittable_into
- 1;
935 ALLOC_GROW(file_diff
->hunk
, file_diff
->hunk_nr
, file_diff
->hunk_alloc
);
936 if (hunk_index
+ splittable_into
< file_diff
->hunk_nr
)
937 memmove(file_diff
->hunk
+ hunk_index
+ splittable_into
,
938 file_diff
->hunk
+ hunk_index
+ 1,
939 (file_diff
->hunk_nr
- hunk_index
- splittable_into
)
941 hunk
= file_diff
->hunk
+ hunk_index
;
942 hunk
->splittable_into
= 1;
943 memset(hunk
+ 1, 0, (splittable_into
- 1) * sizeof(*hunk
));
945 header
= &hunk
->header
;
946 header
->old_count
= header
->new_count
= 0;
948 current
= hunk
->start
;
950 colored_current
= hunk
->colored_start
;
952 context_line_count
= 0;
954 while (splittable_into
> 1) {
955 ch
= s
->plain
.buf
[current
];
958 BUG("buffer overrun while splitting hunks");
961 * Is this the first context line after a chain of +/- lines?
962 * Then record the start of the next split hunk.
964 if ((marker
== '-' || marker
== '+') && ch
== ' ') {
966 hunk
[1].start
= current
;
968 hunk
[1].colored_start
= colored_current
;
969 context_line_count
= 0;
973 * Was the previous line a +/- one? Alternatively, is this the
974 * first line (and not a +/- one)?
976 * Then just increment the appropriate counter and continue
977 * with the next line.
979 if (marker
!= ' ' || (ch
!= '-' && ch
!= '+')) {
981 /* Comment lines are attached to the previous line */
983 ch
= marker
? marker
: ' ';
985 /* current hunk not done yet */
987 context_line_count
++;
993 BUG("unhandled diff marker: '%c'", ch
);
995 current
= find_next_line(&s
->plain
, current
);
998 find_next_line(&s
->colored
,
1004 * We got us the start of a new hunk!
1006 * This is a context line, so it is shared with the previous
1011 if (header
->old_count
|| header
->new_count
)
1012 BUG("counts are off: %d/%d",
1013 (int)header
->old_count
,
1014 (int)header
->new_count
);
1016 header
->old_count
= context_line_count
;
1017 header
->new_count
= context_line_count
;
1018 context_line_count
= 0;
1020 goto next_hunk_line
;
1023 remaining
.old_offset
+= header
->old_count
;
1024 remaining
.old_count
-= header
->old_count
;
1025 remaining
.new_offset
+= header
->new_count
;
1026 remaining
.new_count
-= header
->new_count
;
1028 /* initialize next hunk header's offsets */
1029 hunk
[1].header
.old_offset
=
1030 header
->old_offset
+ header
->old_count
;
1031 hunk
[1].header
.new_offset
=
1032 header
->new_offset
+ header
->new_count
;
1034 /* add one split hunk */
1035 header
->old_count
+= context_line_count
;
1036 header
->new_count
+= context_line_count
;
1038 hunk
->end
= current
;
1040 hunk
->colored_end
= colored_current
;
1043 hunk
->splittable_into
= 1;
1044 hunk
->use
= hunk
[-1].use
;
1045 header
= &hunk
->header
;
1047 header
->old_count
= header
->new_count
= context_line_count
;
1048 context_line_count
= 0;
1054 /* last hunk simply gets the rest */
1055 if (header
->old_offset
!= remaining
.old_offset
)
1056 BUG("miscounted old_offset: %lu != %lu",
1057 header
->old_offset
, remaining
.old_offset
);
1058 if (header
->new_offset
!= remaining
.new_offset
)
1059 BUG("miscounted new_offset: %lu != %lu",
1060 header
->new_offset
, remaining
.new_offset
);
1061 header
->old_count
= remaining
.old_count
;
1062 header
->new_count
= remaining
.new_count
;
1065 hunk
->colored_end
= colored_end
;
1070 static void recolor_hunk(struct add_p_state
*s
, struct hunk
*hunk
)
1072 const char *plain
= s
->plain
.buf
;
1073 size_t current
, eol
, next
;
1075 if (!s
->colored
.len
)
1078 hunk
->colored_start
= s
->colored
.len
;
1079 for (current
= hunk
->start
; current
< hunk
->end
; ) {
1080 for (eol
= current
; eol
< hunk
->end
; eol
++)
1081 if (plain
[eol
] == '\n')
1083 next
= eol
+ (eol
< hunk
->end
);
1084 if (eol
> current
&& plain
[eol
- 1] == '\r')
1087 strbuf_addstr(&s
->colored
,
1088 plain
[current
] == '-' ?
1089 s
->s
.file_old_color
:
1090 plain
[current
] == '+' ?
1091 s
->s
.file_new_color
:
1092 s
->s
.context_color
);
1093 strbuf_add(&s
->colored
, plain
+ current
, eol
- current
);
1094 strbuf_addstr(&s
->colored
, s
->s
.reset_color
);
1096 strbuf_add(&s
->colored
, plain
+ eol
, next
- eol
);
1099 hunk
->colored_end
= s
->colored
.len
;
1102 static int edit_hunk_manually(struct add_p_state
*s
, struct hunk
*hunk
)
1106 strbuf_reset(&s
->buf
);
1107 strbuf_commented_addf(&s
->buf
, comment_line_str
,
1108 _("Manual hunk edit mode -- see bottom for "
1109 "a quick guide.\n"));
1110 render_hunk(s
, hunk
, 0, 0, &s
->buf
);
1111 strbuf_commented_addf(&s
->buf
, comment_line_str
,
1113 "To remove '%c' lines, make them ' ' lines "
1115 "To remove '%c' lines, delete them.\n"
1116 "Lines starting with %s will be removed.\n"),
1117 s
->mode
->is_reverse
? '+' : '-',
1118 s
->mode
->is_reverse
? '-' : '+',
1120 strbuf_commented_addf(&s
->buf
, comment_line_str
, "%s",
1121 _(s
->mode
->edit_hunk_hint
));
1123 * TRANSLATORS: 'it' refers to the patch mentioned in the previous
1126 strbuf_commented_addf(&s
->buf
, comment_line_str
,
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"));
1133 if (strbuf_edit_interactively(&s
->buf
, "addp-hunk-edit.diff", NULL
) < 0)
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
);
1141 if (!starts_with(s
->buf
.buf
+ i
, comment_line_str
))
1142 strbuf_add(&s
->plain
, s
->buf
.buf
+ i
, next
- i
);
1146 hunk
->end
= s
->plain
.len
;
1147 if (hunk
->end
== hunk
->start
)
1148 /* The user aborted editing by deleting everything */
1151 recolor_hunk(s
, hunk
);
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).
1158 if (s
->plain
.buf
[hunk
->start
] == '@' &&
1159 parse_hunk_header(s
, hunk
) < 0)
1160 return error(_("could not parse hunk header"));
1165 static ssize_t
recount_edited_hunk(struct add_p_state
*s
, struct hunk
*hunk
,
1166 size_t orig_old_count
, size_t orig_new_count
)
1168 struct hunk_header
*header
= &hunk
->header
;
1171 header
->old_count
= header
->new_count
= 0;
1172 for (i
= hunk
->start
; i
< hunk
->end
; ) {
1173 switch (s
->plain
.buf
[i
]) {
1175 header
->old_count
++;
1178 header
->new_count
++;
1180 case ' ': case '\r': case '\n':
1181 header
->old_count
++;
1182 header
->new_count
++;
1186 i
= find_next_line(&s
->plain
, i
);
1189 return orig_old_count
- orig_new_count
1190 - header
->old_count
+ header
->new_count
;
1193 static int run_apply_check(struct add_p_state
*s
,
1194 struct file_diff
*file_diff
)
1196 struct child_process cp
= CHILD_PROCESS_INIT
;
1198 strbuf_reset(&s
->buf
);
1199 reassemble_patch(s
, file_diff
, 1, &s
->buf
);
1201 setup_child_process(s
, &cp
,
1202 "apply", "--check", NULL
);
1203 strvec_pushv(&cp
.args
, s
->mode
->apply_check_args
);
1204 if (pipe_command(&cp
, s
->buf
.buf
, s
->buf
.len
, NULL
, 0, NULL
, 0))
1205 return error(_("'git apply --cached' failed"));
1210 static int read_single_character(struct add_p_state
*s
)
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
);
1218 if (git_read_line_interactively(&s
->answer
) == EOF
)
1223 static int prompt_yesno(struct add_p_state
*s
, const char *prompt
)
1226 color_fprintf(stdout
, s
->s
.prompt_color
, "%s", _(prompt
));
1228 if (read_single_character(s
) == EOF
)
1230 switch (tolower(s
->answer
.buf
[0])) {
1237 static int edit_hunk_loop(struct add_p_state
*s
,
1238 struct file_diff
*file_diff
, struct hunk
*hunk
)
1240 size_t plain_len
= s
->plain
.len
, colored_len
= s
->colored
.len
;
1246 int res
= edit_hunk_manually(s
, hunk
);
1255 recount_edited_hunk(s
, hunk
,
1256 backup
.header
.old_count
,
1257 backup
.header
.new_count
);
1258 if (!run_apply_check(s
, file_diff
))
1262 /* Drop edits (they were appended to s->plain) */
1263 strbuf_setlen(&s
->plain
, plain_len
);
1264 strbuf_setlen(&s
->colored
, colored_len
);
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.
1274 res
= prompt_yesno(s
, _("Your edited hunk does not apply. "
1275 "Edit again (saying \"no\" discards!) "
1282 static int apply_for_checkout(struct add_p_state
*s
, struct strbuf
*diff
,
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
;
1292 setup_child_process(s
, &check_index
,
1293 "apply", "--cached", "--check", reverse
, NULL
);
1294 applies_index
= !pipe_command(&check_index
, diff
->buf
, diff
->len
,
1297 setup_child_process(s
, &check_worktree
,
1298 "apply", "--check", reverse
, NULL
);
1299 applies_worktree
= !pipe_command(&check_worktree
, diff
->buf
, diff
->len
,
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
,
1308 setup_child_process(s
, &apply_worktree
,
1309 "apply", reverse
, NULL
);
1310 pipe_command(&apply_worktree
, diff
->buf
, diff
->len
,
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 "
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);
1325 err(s
, _("Nothing was applied.\n"));
1327 /* As a last resort, show the diff to the user */
1328 fwrite(diff
->buf
, diff
->len
, 1, stdout
);
1333 #define SUMMARY_HEADER_WIDTH 20
1334 #define SUMMARY_LINE_WIDTH 80
1335 static void summarize_hunk(struct add_p_state
*s
, struct hunk
*hunk
,
1338 struct hunk_header
*header
= &hunk
->header
;
1339 struct strbuf
*plain
= &s
->plain
;
1340 size_t len
= out
->len
, i
;
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
] != ' ')
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
);
1358 #define DISPLAY_HUNKS_LINES 20
1359 static size_t display_hunks(struct add_p_state
*s
,
1360 struct file_diff
*file_diff
, size_t start_index
)
1362 size_t end_index
= start_index
+ DISPLAY_HUNKS_LINES
;
1364 if (end_index
> file_diff
->hunk_nr
)
1365 end_index
= file_diff
->hunk_nr
;
1367 while (start_index
< end_index
) {
1368 struct hunk
*hunk
= file_diff
->hunk
+ start_index
++;
1370 strbuf_reset(&s
->buf
);
1371 strbuf_addf(&s
->buf
, "%c%2d: ", hunk
->use
== USE_HUNK
? '+'
1372 : hunk
->use
== SKIP_HUNK
? '-' : ' ',
1374 summarize_hunk(s
, hunk
, &s
->buf
);
1375 fputs(s
->buf
.buf
, stdout
);
1381 static const char help_patch_remainder
[] =
1382 N_("j - leave this hunk undecided, see next undecided hunk\n"
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"
1386 "g - select a hunk to go to\n"
1387 "/ - search for a hunk matching the given regex\n"
1388 "s - split the current hunk into smaller hunks\n"
1389 "e - manually edit the current hunk\n"
1390 "p - print the current hunk\n"
1391 "? - print help\n");
1393 static int patch_update_file(struct add_p_state
*s
,
1394 struct file_diff
*file_diff
)
1396 size_t hunk_index
= 0;
1397 ssize_t i
, undecided_previous
, undecided_next
, rendered_hunk_index
= -1;
1400 struct child_process cp
= CHILD_PROCESS_INIT
;
1401 int colored
= !!s
->colored
.len
, quit
= 0;
1402 enum prompt_mode_type prompt_mode_type
;
1404 ALLOW_GOTO_PREVIOUS_HUNK
= 1 << 0,
1405 ALLOW_GOTO_PREVIOUS_UNDECIDED_HUNK
= 1 << 1,
1406 ALLOW_GOTO_NEXT_HUNK
= 1 << 2,
1407 ALLOW_GOTO_NEXT_UNDECIDED_HUNK
= 1 << 3,
1408 ALLOW_SEARCH_AND_GOTO
= 1 << 4,
1409 ALLOW_SPLIT
= 1 << 5,
1413 /* Empty added files have no hunks */
1414 if (!file_diff
->hunk_nr
&& !file_diff
->added
)
1417 strbuf_reset(&s
->buf
);
1418 render_diff_header(s
, file_diff
, colored
, &s
->buf
);
1419 fputs(s
->buf
.buf
, stdout
);
1421 if (hunk_index
>= file_diff
->hunk_nr
)
1423 hunk
= file_diff
->hunk_nr
1424 ? file_diff
->hunk
+ hunk_index
1426 undecided_previous
= -1;
1427 undecided_next
= -1;
1429 if (file_diff
->hunk_nr
) {
1430 for (i
= hunk_index
- 1; i
>= 0; i
--)
1431 if (file_diff
->hunk
[i
].use
== UNDECIDED_HUNK
) {
1432 undecided_previous
= i
;
1436 for (i
= hunk_index
+ 1; i
< file_diff
->hunk_nr
; i
++)
1437 if (file_diff
->hunk
[i
].use
== UNDECIDED_HUNK
) {
1443 /* Everything decided? */
1444 if (undecided_previous
< 0 && undecided_next
< 0 &&
1445 hunk
->use
!= UNDECIDED_HUNK
)
1448 strbuf_reset(&s
->buf
);
1449 if (file_diff
->hunk_nr
) {
1450 if (rendered_hunk_index
!= hunk_index
) {
1451 render_hunk(s
, hunk
, 0, colored
, &s
->buf
);
1452 fputs(s
->buf
.buf
, stdout
);
1453 rendered_hunk_index
= hunk_index
;
1456 strbuf_reset(&s
->buf
);
1457 if (undecided_previous
>= 0) {
1458 permitted
|= ALLOW_GOTO_PREVIOUS_UNDECIDED_HUNK
;
1459 strbuf_addstr(&s
->buf
, ",k");
1462 permitted
|= ALLOW_GOTO_PREVIOUS_HUNK
;
1463 strbuf_addstr(&s
->buf
, ",K");
1465 if (undecided_next
>= 0) {
1466 permitted
|= ALLOW_GOTO_NEXT_UNDECIDED_HUNK
;
1467 strbuf_addstr(&s
->buf
, ",j");
1469 if (hunk_index
+ 1 < file_diff
->hunk_nr
) {
1470 permitted
|= ALLOW_GOTO_NEXT_HUNK
;
1471 strbuf_addstr(&s
->buf
, ",J");
1473 if (file_diff
->hunk_nr
> 1) {
1474 permitted
|= ALLOW_SEARCH_AND_GOTO
;
1475 strbuf_addstr(&s
->buf
, ",g,/");
1477 if (hunk
->splittable_into
> 1) {
1478 permitted
|= ALLOW_SPLIT
;
1479 strbuf_addstr(&s
->buf
, ",s");
1481 if (hunk_index
+ 1 > file_diff
->mode_change
&&
1482 !file_diff
->deleted
) {
1483 permitted
|= ALLOW_EDIT
;
1484 strbuf_addstr(&s
->buf
, ",e");
1486 strbuf_addstr(&s
->buf
, ",p");
1488 if (file_diff
->deleted
)
1489 prompt_mode_type
= PROMPT_DELETION
;
1490 else if (file_diff
->added
)
1491 prompt_mode_type
= PROMPT_ADDITION
;
1492 else if (file_diff
->mode_change
&& !hunk_index
)
1493 prompt_mode_type
= PROMPT_MODE_CHANGE
;
1495 prompt_mode_type
= PROMPT_HUNK
;
1497 printf("%s(%"PRIuMAX
"/%"PRIuMAX
") ", s
->s
.prompt_color
,
1498 (uintmax_t)hunk_index
+ 1,
1499 (uintmax_t)(file_diff
->hunk_nr
1500 ? file_diff
->hunk_nr
1502 printf(_(s
->mode
->prompt_mode
[prompt_mode_type
]),
1504 if (*s
->s
.reset_color
)
1505 fputs(s
->s
.reset_color
, stdout
);
1507 if (read_single_character(s
) == EOF
)
1512 ch
= tolower(s
->answer
.buf
[0]);
1514 hunk
->use
= USE_HUNK
;
1516 hunk_index
= undecided_next
< 0 ?
1517 file_diff
->hunk_nr
: undecided_next
;
1518 } else if (ch
== 'n') {
1519 hunk
->use
= SKIP_HUNK
;
1520 goto soft_increment
;
1521 } else if (ch
== 'a') {
1522 if (file_diff
->hunk_nr
) {
1523 for (; hunk_index
< file_diff
->hunk_nr
; hunk_index
++) {
1524 hunk
= file_diff
->hunk
+ hunk_index
;
1525 if (hunk
->use
== UNDECIDED_HUNK
)
1526 hunk
->use
= USE_HUNK
;
1528 } else if (hunk
->use
== UNDECIDED_HUNK
) {
1529 hunk
->use
= USE_HUNK
;
1531 } else if (ch
== 'd' || ch
== 'q') {
1532 if (file_diff
->hunk_nr
) {
1533 for (; hunk_index
< file_diff
->hunk_nr
; hunk_index
++) {
1534 hunk
= file_diff
->hunk
+ hunk_index
;
1535 if (hunk
->use
== UNDECIDED_HUNK
)
1536 hunk
->use
= SKIP_HUNK
;
1538 } else if (hunk
->use
== UNDECIDED_HUNK
) {
1539 hunk
->use
= SKIP_HUNK
;
1545 } else if (s
->answer
.buf
[0] == 'K') {
1546 if (permitted
& ALLOW_GOTO_PREVIOUS_HUNK
)
1549 err(s
, _("No previous hunk"));
1550 } else if (s
->answer
.buf
[0] == 'J') {
1551 if (permitted
& ALLOW_GOTO_NEXT_HUNK
)
1554 err(s
, _("No next hunk"));
1555 } else if (s
->answer
.buf
[0] == 'k') {
1556 if (permitted
& ALLOW_GOTO_PREVIOUS_UNDECIDED_HUNK
)
1557 hunk_index
= undecided_previous
;
1559 err(s
, _("No previous hunk"));
1560 } else if (s
->answer
.buf
[0] == 'j') {
1561 if (permitted
& ALLOW_GOTO_NEXT_UNDECIDED_HUNK
)
1562 hunk_index
= undecided_next
;
1564 err(s
, _("No next hunk"));
1565 } else if (s
->answer
.buf
[0] == 'g') {
1567 unsigned long response
;
1569 if (!(permitted
& ALLOW_SEARCH_AND_GOTO
)) {
1570 err(s
, _("No other hunks to goto"));
1573 strbuf_remove(&s
->answer
, 0, 1);
1574 strbuf_trim(&s
->answer
);
1575 i
= hunk_index
- DISPLAY_HUNKS_LINES
/ 2;
1576 if (i
< (int)file_diff
->mode_change
)
1577 i
= file_diff
->mode_change
;
1578 while (s
->answer
.len
== 0) {
1579 i
= display_hunks(s
, file_diff
, i
);
1580 printf("%s", i
< file_diff
->hunk_nr
?
1581 _("go to which hunk (<ret> to see "
1582 "more)? ") : _("go to which hunk? "));
1584 if (strbuf_getline(&s
->answer
,
1587 strbuf_trim_trailing_newline(&s
->answer
);
1590 strbuf_trim(&s
->answer
);
1591 response
= strtoul(s
->answer
.buf
, &pend
, 10);
1592 if (*pend
|| pend
== s
->answer
.buf
)
1593 err(s
, _("Invalid number: '%s'"),
1595 else if (0 < response
&& response
<= file_diff
->hunk_nr
)
1596 hunk_index
= response
- 1;
1598 err(s
, Q_("Sorry, only %d hunk available.",
1599 "Sorry, only %d hunks available.",
1600 file_diff
->hunk_nr
),
1601 (int)file_diff
->hunk_nr
);
1602 } else if (s
->answer
.buf
[0] == '/') {
1606 if (!(permitted
& ALLOW_SEARCH_AND_GOTO
)) {
1607 err(s
, _("No other hunks to search"));
1610 strbuf_remove(&s
->answer
, 0, 1);
1611 strbuf_trim_trailing_newline(&s
->answer
);
1612 if (s
->answer
.len
== 0) {
1613 printf("%s", _("search for regex? "));
1615 if (strbuf_getline(&s
->answer
,
1618 strbuf_trim_trailing_newline(&s
->answer
);
1619 if (s
->answer
.len
== 0)
1622 ret
= regcomp(®ex
, s
->answer
.buf
,
1623 REG_EXTENDED
| REG_NOSUB
| REG_NEWLINE
);
1627 regerror(ret
, ®ex
, errbuf
, sizeof(errbuf
));
1628 err(s
, _("Malformed search regexp %s: %s"),
1629 s
->answer
.buf
, errbuf
);
1634 /* render the hunk into a scratch buffer */
1635 render_hunk(s
, file_diff
->hunk
+ i
, 0, 0,
1637 if (regexec(®ex
, s
->buf
.buf
, 0, NULL
, 0)
1641 if (i
== file_diff
->hunk_nr
)
1643 if (i
!= hunk_index
)
1645 err(s
, _("No hunk matches the given pattern"));
1650 } else if (s
->answer
.buf
[0] == 's') {
1651 size_t splittable_into
= hunk
->splittable_into
;
1652 if (!(permitted
& ALLOW_SPLIT
)) {
1653 err(s
, _("Sorry, cannot split this hunk"));
1654 } else if (!split_hunk(s
, file_diff
,
1655 hunk
- file_diff
->hunk
)) {
1656 color_fprintf_ln(stdout
, s
->s
.header_color
,
1657 _("Split into %d hunks."),
1658 (int)splittable_into
);
1659 rendered_hunk_index
= -1;
1661 } else if (s
->answer
.buf
[0] == 'e') {
1662 if (!(permitted
& ALLOW_EDIT
))
1663 err(s
, _("Sorry, cannot edit this hunk"));
1664 else if (edit_hunk_loop(s
, file_diff
, hunk
) >= 0) {
1665 hunk
->use
= USE_HUNK
;
1666 goto soft_increment
;
1668 } else if (s
->answer
.buf
[0] == 'p') {
1669 rendered_hunk_index
= -1;
1670 } else if (s
->answer
.buf
[0] == '?') {
1671 const char *p
= _(help_patch_remainder
), *eol
= p
;
1673 color_fprintf(stdout
, s
->s
.help_color
, "%s",
1674 _(s
->mode
->help_patch_text
));
1677 * Show only those lines of the remainder that are
1678 * actually applicable with the current hunk.
1680 for (; *p
; p
= eol
+ (*eol
== '\n')) {
1681 eol
= strchrnul(p
, '\n');
1684 * `s->buf` still contains the part of the
1685 * commands shown in the prompt that are not
1688 if (*p
!= '?' && !strchr(s
->buf
.buf
, *p
))
1691 color_fprintf_ln(stdout
, s
->s
.help_color
,
1692 "%.*s", (int)(eol
- p
), p
);
1695 err(s
, _("Unknown command '%s' (use '?' for help)"),
1700 /* Any hunk to be used? */
1701 for (i
= 0; i
< file_diff
->hunk_nr
; i
++)
1702 if (file_diff
->hunk
[i
].use
== USE_HUNK
)
1705 if (i
< file_diff
->hunk_nr
||
1706 (!file_diff
->hunk_nr
&& file_diff
->head
.use
== USE_HUNK
)) {
1707 /* At least one hunk selected: apply */
1708 strbuf_reset(&s
->buf
);
1709 reassemble_patch(s
, file_diff
, 0, &s
->buf
);
1711 discard_index(s
->s
.r
->index
);
1712 if (s
->mode
->apply_for_checkout
)
1713 apply_for_checkout(s
, &s
->buf
,
1714 s
->mode
->is_reverse
);
1716 setup_child_process(s
, &cp
, "apply", NULL
);
1717 strvec_pushv(&cp
.args
, s
->mode
->apply_args
);
1718 if (pipe_command(&cp
, s
->buf
.buf
, s
->buf
.len
,
1720 error(_("'git apply' failed"));
1722 if (repo_read_index(s
->s
.r
) >= 0)
1723 repo_refresh_and_write_index(s
->s
.r
, REFRESH_QUIET
, 0,
1724 1, NULL
, NULL
, NULL
);
1731 int run_add_p(struct repository
*r
, enum add_p_mode mode
,
1732 const char *revision
, const struct pathspec
*ps
)
1734 struct add_p_state s
= {
1735 { r
}, STRBUF_INIT
, STRBUF_INIT
, STRBUF_INIT
, STRBUF_INIT
1737 size_t i
, binary_count
= 0;
1739 init_add_i_state(&s
.s
, r
);
1741 if (mode
== ADD_P_STASH
)
1742 s
.mode
= &patch_mode_stash
;
1743 else if (mode
== ADD_P_RESET
) {
1744 if (!revision
|| !strcmp(revision
, "HEAD"))
1745 s
.mode
= &patch_mode_reset_head
;
1747 s
.mode
= &patch_mode_reset_nothead
;
1748 } else if (mode
== ADD_P_CHECKOUT
) {
1750 s
.mode
= &patch_mode_checkout_index
;
1751 else if (!strcmp(revision
, "HEAD"))
1752 s
.mode
= &patch_mode_checkout_head
;
1754 s
.mode
= &patch_mode_checkout_nothead
;
1755 } else if (mode
== ADD_P_WORKTREE
) {
1757 s
.mode
= &patch_mode_checkout_index
;
1758 else if (!strcmp(revision
, "HEAD"))
1759 s
.mode
= &patch_mode_worktree_head
;
1761 s
.mode
= &patch_mode_worktree_nothead
;
1763 s
.mode
= &patch_mode_add
;
1764 s
.revision
= revision
;
1766 discard_index(r
->index
);
1767 if (repo_read_index(r
) < 0 ||
1768 (!s
.mode
->index_only
&&
1769 repo_refresh_and_write_index(r
, REFRESH_QUIET
, 0, 1,
1770 NULL
, NULL
, NULL
) < 0) ||
1771 parse_diff(&s
, ps
) < 0) {
1772 add_p_state_clear(&s
);
1776 for (i
= 0; i
< s
.file_diff_nr
; i
++)
1777 if (s
.file_diff
[i
].binary
&& !s
.file_diff
[i
].hunk_nr
)
1779 else if (patch_update_file(&s
, s
.file_diff
+ i
))
1782 if (s
.file_diff_nr
== 0)
1783 err(&s
, _("No changes."));
1784 else if (binary_count
== s
.file_diff_nr
)
1785 err(&s
, _("Only binary files changed."));
1787 add_p_state_clear(&s
);