2 #include "add-interactive.h"
4 #include "run-command.h"
5 #include "argv-array.h"
10 enum prompt_mode_type
{
11 PROMPT_MODE_CHANGE
= 0, PROMPT_DELETION
, PROMPT_HUNK
,
12 PROMPT_MODE_MAX
, /* must be last */
17 * The magic constant 4 is chosen such that all patch modes
18 * provide enough space for three command-line arguments followed by a
21 const char *diff_cmd
[4], *apply_args
[4], *apply_check_args
[4];
22 unsigned is_reverse
:1, index_only
:1, apply_for_checkout
:1;
23 const char *prompt_mode
[PROMPT_MODE_MAX
];
24 const char *edit_hunk_hint
, *help_patch_text
;
27 static struct patch_mode patch_mode_add
= {
28 .diff_cmd
= { "diff-files", NULL
},
29 .apply_args
= { "--cached", NULL
},
30 .apply_check_args
= { "--cached", NULL
},
32 N_("Stage mode change [y,n,q,a,d%s,?]? "),
33 N_("Stage deletion [y,n,q,a,d%s,?]? "),
34 N_("Stage this hunk [y,n,q,a,d%s,?]? ")
36 .edit_hunk_hint
= N_("If the patch applies cleanly, the edited hunk "
37 "will immediately be marked for staging."),
39 N_("y - stage this hunk\n"
40 "n - do not stage this hunk\n"
41 "q - quit; do not stage this hunk or any of the remaining "
43 "a - stage this hunk and all later hunks in the file\n"
44 "d - do not stage this hunk or any of the later hunks in "
48 static struct patch_mode patch_mode_stash
= {
49 .diff_cmd
= { "diff-index", "HEAD", NULL
},
50 .apply_args
= { "--cached", NULL
},
51 .apply_check_args
= { "--cached", NULL
},
53 N_("Stash mode change [y,n,q,a,d%s,?]? "),
54 N_("Stash deletion [y,n,q,a,d%s,?]? "),
55 N_("Stash this hunk [y,n,q,a,d%s,?]? "),
57 .edit_hunk_hint
= N_("If the patch applies cleanly, the edited hunk "
58 "will immediately be marked for stashing."),
60 N_("y - stash this hunk\n"
61 "n - do not stash this hunk\n"
62 "q - quit; do not stash this hunk or any of the remaining "
64 "a - stash this hunk and all later hunks in the file\n"
65 "d - do not stash this hunk or any of the later hunks in "
69 static struct patch_mode patch_mode_reset_head
= {
70 .diff_cmd
= { "diff-index", "--cached", NULL
},
71 .apply_args
= { "-R", "--cached", NULL
},
72 .apply_check_args
= { "-R", "--cached", NULL
},
76 N_("Unstage mode change [y,n,q,a,d%s,?]? "),
77 N_("Unstage deletion [y,n,q,a,d%s,?]? "),
78 N_("Unstage this hunk [y,n,q,a,d%s,?]? "),
80 .edit_hunk_hint
= N_("If the patch applies cleanly, the edited hunk "
81 "will immediately be marked for unstaging."),
83 N_("y - unstage this hunk\n"
84 "n - do not unstage this hunk\n"
85 "q - quit; do not unstage this hunk or any of the remaining "
87 "a - unstage this hunk and all later hunks in the file\n"
88 "d - do not unstage this hunk or any of the later hunks in "
92 static struct patch_mode patch_mode_reset_nothead
= {
93 .diff_cmd
= { "diff-index", "-R", "--cached", NULL
},
94 .apply_args
= { "--cached", NULL
},
95 .apply_check_args
= { "--cached", NULL
},
98 N_("Apply mode change to index [y,n,q,a,d%s,?]? "),
99 N_("Apply deletion to index [y,n,q,a,d%s,?]? "),
100 N_("Apply this hunk to index [y,n,q,a,d%s,?]? "),
102 .edit_hunk_hint
= N_("If the patch applies cleanly, the edited hunk "
103 "will immediately be marked for applying."),
105 N_("y - apply this hunk to index\n"
106 "n - do not apply this hunk to index\n"
107 "q - quit; do not apply this hunk or any of the remaining "
109 "a - apply this hunk and all later hunks in the file\n"
110 "d - do not apply this hunk or any of the later hunks in "
114 static struct patch_mode patch_mode_checkout_index
= {
115 .diff_cmd
= { "diff-files", NULL
},
116 .apply_args
= { "-R", NULL
},
117 .apply_check_args
= { "-R", NULL
},
120 N_("Discard mode change from worktree [y,n,q,a,d%s,?]? "),
121 N_("Discard deletion from worktree [y,n,q,a,d%s,?]? "),
122 N_("Discard this hunk from worktree [y,n,q,a,d%s,?]? "),
124 .edit_hunk_hint
= N_("If the patch applies cleanly, the edited hunk "
125 "will immediately be marked for discarding."),
127 N_("y - discard this hunk from worktree\n"
128 "n - do not discard this hunk from worktree\n"
129 "q - quit; do not discard this hunk or any of the remaining "
131 "a - discard this hunk and all later hunks in the file\n"
132 "d - do not discard this hunk or any of the later hunks in "
136 static struct patch_mode patch_mode_checkout_head
= {
137 .diff_cmd
= { "diff-index", NULL
},
138 .apply_for_checkout
= 1,
139 .apply_check_args
= { "-R", NULL
},
142 N_("Discard mode change from index and worktree [y,n,q,a,d%s,?]? "),
143 N_("Discard deletion from index and worktree [y,n,q,a,d%s,?]? "),
144 N_("Discard this hunk from index and worktree [y,n,q,a,d%s,?]? "),
146 .edit_hunk_hint
= N_("If the patch applies cleanly, the edited hunk "
147 "will immediately be marked for discarding."),
149 N_("y - discard this hunk from index and worktree\n"
150 "n - do not discard this hunk from index and worktree\n"
151 "q - quit; do not discard this hunk or any of the remaining "
153 "a - discard this hunk and all later hunks in the file\n"
154 "d - do not discard this hunk or any of the later hunks in "
158 static struct patch_mode patch_mode_checkout_nothead
= {
159 .diff_cmd
= { "diff-index", "-R", NULL
},
160 .apply_for_checkout
= 1,
161 .apply_check_args
= { NULL
},
163 N_("Apply mode change to index and worktree [y,n,q,a,d%s,?]? "),
164 N_("Apply deletion to index and worktree [y,n,q,a,d%s,?]? "),
165 N_("Apply this hunk to index and worktree [y,n,q,a,d%s,?]? "),
167 .edit_hunk_hint
= N_("If the patch applies cleanly, the edited hunk "
168 "will immediately be marked for applying."),
170 N_("y - apply this hunk to index and worktree\n"
171 "n - do not apply this hunk to index and worktree\n"
172 "q - quit; do not apply this hunk or any of the remaining "
174 "a - apply this hunk and all later hunks in the file\n"
175 "d - do not apply this hunk or any of the later hunks in "
180 unsigned long old_offset
, old_count
, new_offset
, new_count
;
182 * Start/end offsets to the extra text after the second `@@` in the
183 * hunk header, e.g. the function signature. This is expected to
184 * include the newline.
186 size_t extra_start
, extra_end
, colored_extra_start
, colored_extra_end
;
190 size_t start
, end
, colored_start
, colored_end
, splittable_into
;
192 enum { UNDECIDED_HUNK
= 0, SKIP_HUNK
, USE_HUNK
} use
;
193 struct hunk_header header
;
197 struct add_i_state s
;
198 struct strbuf answer
, buf
;
201 struct strbuf plain
, colored
;
205 size_t hunk_nr
, hunk_alloc
;
206 unsigned deleted
:1, mode_change
:1,binary
:1;
211 struct patch_mode
*mode
;
212 const char *revision
;
215 static void err(struct add_p_state
*s
, const char *fmt
, ...)
220 fputs(s
->s
.error_color
, stderr
);
221 vfprintf(stderr
, fmt
, args
);
222 fputs(s
->s
.reset_color
, stderr
);
227 static void setup_child_process(struct add_p_state
*s
,
228 struct child_process
*cp
, ...)
234 while ((arg
= va_arg(ap
, const char *)))
235 argv_array_push(&cp
->args
, arg
);
239 argv_array_pushf(&cp
->env_array
,
240 INDEX_ENVIRONMENT
"=%s", s
->s
.r
->index_file
);
243 static int parse_range(const char **p
,
244 unsigned long *offset
, unsigned long *count
)
248 *offset
= strtoul(*p
, &pend
, 10);
256 *count
= strtoul(pend
+ 1, (char **)p
, 10);
257 return *p
== pend
+ 1 ? -1 : 0;
260 static int parse_hunk_header(struct add_p_state
*s
, struct hunk
*hunk
)
262 struct hunk_header
*header
= &hunk
->header
;
263 const char *line
= s
->plain
.buf
+ hunk
->start
, *p
= line
;
264 char *eol
= memchr(p
, '\n', s
->plain
.len
- hunk
->start
);
267 eol
= s
->plain
.buf
+ s
->plain
.len
;
269 if (!skip_prefix(p
, "@@ -", &p
) ||
270 parse_range(&p
, &header
->old_offset
, &header
->old_count
) < 0 ||
271 !skip_prefix(p
, " +", &p
) ||
272 parse_range(&p
, &header
->new_offset
, &header
->new_count
) < 0 ||
273 !skip_prefix(p
, " @@", &p
))
274 return error(_("could not parse hunk header '%.*s'"),
275 (int)(eol
- line
), line
);
277 hunk
->start
= eol
- s
->plain
.buf
+ (*eol
== '\n');
278 header
->extra_start
= p
- s
->plain
.buf
;
279 header
->extra_end
= hunk
->start
;
281 if (!s
->colored
.len
) {
282 header
->colored_extra_start
= header
->colored_extra_end
= 0;
286 /* Now find the extra text in the colored diff */
287 line
= s
->colored
.buf
+ hunk
->colored_start
;
288 eol
= memchr(line
, '\n', s
->colored
.len
- hunk
->colored_start
);
290 eol
= s
->colored
.buf
+ s
->colored
.len
;
291 p
= memmem(line
, eol
- line
, "@@ -", 4);
293 return error(_("could not parse colored hunk header '%.*s'"),
294 (int)(eol
- line
), line
);
295 p
= memmem(p
+ 4, eol
- p
- 4, " @@", 3);
297 return error(_("could not parse colored hunk header '%.*s'"),
298 (int)(eol
- line
), line
);
299 hunk
->colored_start
= eol
- s
->colored
.buf
+ (*eol
== '\n');
300 header
->colored_extra_start
= p
+ 3 - s
->colored
.buf
;
301 header
->colored_extra_end
= hunk
->colored_start
;
306 static int is_octal(const char *p
, size_t len
)
312 if (*p
< '0' || *(p
++) > '7')
317 static int parse_diff(struct add_p_state
*s
, const struct pathspec
*ps
)
319 struct argv_array args
= ARGV_ARRAY_INIT
;
320 struct strbuf
*plain
= &s
->plain
, *colored
= NULL
;
321 struct child_process cp
= CHILD_PROCESS_INIT
;
322 char *p
, *pend
, *colored_p
= NULL
, *colored_pend
= NULL
, marker
= '\0';
323 size_t file_diff_alloc
= 0, i
, color_arg_index
;
324 struct file_diff
*file_diff
= NULL
;
325 struct hunk
*hunk
= NULL
;
328 argv_array_pushv(&args
, s
->mode
->diff_cmd
);
330 struct object_id oid
;
331 argv_array_push(&args
,
332 /* could be on an unborn branch */
333 !strcmp("HEAD", s
->revision
) &&
334 get_oid("HEAD", &oid
) ?
335 empty_tree_oid_hex() : s
->revision
);
337 color_arg_index
= args
.argc
;
338 /* Use `--no-color` explicitly, just in case `diff.color = always`. */
339 argv_array_pushl(&args
, "--no-color", "-p", "--", NULL
);
340 for (i
= 0; i
< ps
->nr
; i
++)
341 argv_array_push(&args
, ps
->items
[i
].original
);
343 setup_child_process(s
, &cp
, NULL
);
345 res
= capture_command(&cp
, plain
, 0);
347 argv_array_clear(&args
);
348 return error(_("could not parse diff"));
351 argv_array_clear(&args
);
354 strbuf_complete_line(plain
);
356 if (want_color_fd(1, -1)) {
357 struct child_process colored_cp
= CHILD_PROCESS_INIT
;
359 setup_child_process(s
, &colored_cp
, NULL
);
360 xsnprintf((char *)args
.argv
[color_arg_index
], 8, "--color");
361 colored_cp
.argv
= args
.argv
;
362 colored
= &s
->colored
;
363 res
= capture_command(&colored_cp
, colored
, 0);
364 argv_array_clear(&args
);
366 return error(_("could not parse colored diff"));
367 strbuf_complete_line(colored
);
368 colored_p
= colored
->buf
;
369 colored_pend
= colored_p
+ colored
->len
;
371 argv_array_clear(&args
);
373 /* parse files and hunks */
375 pend
= p
+ plain
->len
;
377 char *eol
= memchr(p
, '\n', pend
- p
);
378 const char *deleted
= NULL
, *mode_change
= NULL
;
383 if (starts_with(p
, "diff ")) {
385 ALLOC_GROW(s
->file_diff
, s
->file_diff_nr
,
387 file_diff
= s
->file_diff
+ s
->file_diff_nr
- 1;
388 memset(file_diff
, 0, sizeof(*file_diff
));
389 hunk
= &file_diff
->head
;
390 hunk
->start
= p
- plain
->buf
;
392 hunk
->colored_start
= colored_p
- colored
->buf
;
394 } else if (p
== plain
->buf
)
395 BUG("diff starts with unexpected line:\n"
396 "%.*s\n", (int)(eol
- p
), p
);
397 else if (file_diff
->deleted
)
398 ; /* keep the rest of the file in a single "hunk" */
399 else if (starts_with(p
, "@@ ") ||
400 (hunk
== &file_diff
->head
&&
401 skip_prefix(p
, "deleted file", &deleted
))) {
402 if (marker
== '-' || marker
== '+')
404 * Should not happen; previous hunk did not end
405 * in a context line? Handle it anyway.
407 hunk
->splittable_into
++;
409 file_diff
->hunk_nr
++;
410 ALLOC_GROW(file_diff
->hunk
, file_diff
->hunk_nr
,
411 file_diff
->hunk_alloc
);
412 hunk
= file_diff
->hunk
+ file_diff
->hunk_nr
- 1;
413 memset(hunk
, 0, sizeof(*hunk
));
415 hunk
->start
= p
- plain
->buf
;
417 hunk
->colored_start
= colored_p
- colored
->buf
;
420 file_diff
->deleted
= 1;
421 else if (parse_hunk_header(s
, hunk
) < 0)
425 * Start counting into how many hunks this one can be
429 } else if (hunk
== &file_diff
->head
&&
430 skip_prefix(p
, "old mode ", &mode_change
) &&
431 is_octal(mode_change
, eol
- mode_change
)) {
432 if (file_diff
->mode_change
)
433 BUG("double mode change?\n\n%.*s",
434 (int)(eol
- plain
->buf
), plain
->buf
);
435 if (file_diff
->hunk_nr
++)
436 BUG("mode change in the middle?\n\n%.*s",
437 (int)(eol
- plain
->buf
), plain
->buf
);
440 * Do *not* change `hunk`: the mode change pseudo-hunk
441 * is _part of_ the header "hunk".
443 file_diff
->mode_change
= 1;
444 ALLOC_GROW(file_diff
->hunk
, file_diff
->hunk_nr
,
445 file_diff
->hunk_alloc
);
446 memset(file_diff
->hunk
, 0, sizeof(struct hunk
));
447 file_diff
->hunk
->start
= p
- plain
->buf
;
449 file_diff
->hunk
->colored_start
=
450 colored_p
- colored
->buf
;
451 } else if (hunk
== &file_diff
->head
&&
452 skip_prefix(p
, "new mode ", &mode_change
) &&
453 is_octal(mode_change
, eol
- mode_change
)) {
456 * Extend the "mode change" pseudo-hunk to include also
457 * the "new mode" line.
459 if (!file_diff
->mode_change
)
460 BUG("'new mode' without 'old mode'?\n\n%.*s",
461 (int)(eol
- plain
->buf
), plain
->buf
);
462 if (file_diff
->hunk_nr
!= 1)
463 BUG("mode change in the middle?\n\n%.*s",
464 (int)(eol
- plain
->buf
), plain
->buf
);
465 if (p
- plain
->buf
!= file_diff
->hunk
->end
)
466 BUG("'new mode' does not immediately follow "
467 "'old mode'?\n\n%.*s",
468 (int)(eol
- plain
->buf
), plain
->buf
);
469 } else if (hunk
== &file_diff
->head
&&
470 starts_with(p
, "Binary files "))
471 file_diff
->binary
= 1;
473 if (file_diff
->deleted
&& file_diff
->mode_change
)
474 BUG("diff contains delete *and* a mode change?!?\n%.*s",
475 (int)(eol
- (plain
->buf
+ file_diff
->head
.start
)),
476 plain
->buf
+ file_diff
->head
.start
);
478 if ((marker
== '-' || marker
== '+') && *p
== ' ')
479 hunk
->splittable_into
++;
480 if (marker
&& *p
!= '\\')
483 p
= eol
== pend
? pend
: eol
+ 1;
484 hunk
->end
= p
- plain
->buf
;
487 char *colored_eol
= memchr(colored_p
, '\n',
488 colored_pend
- colored_p
);
490 colored_p
= colored_eol
+ 1;
492 colored_p
= colored_pend
;
494 hunk
->colored_end
= colored_p
- colored
->buf
;
498 if (file_diff
->hunk_nr
!= 1)
499 BUG("mode change in hunk #%d???",
500 (int)file_diff
->hunk_nr
);
501 /* Adjust the end of the "mode change" pseudo-hunk */
502 file_diff
->hunk
->end
= hunk
->end
;
504 file_diff
->hunk
->colored_end
= hunk
->colored_end
;
508 if (marker
== '-' || marker
== '+')
510 * Last hunk ended in non-context line (i.e. it appended lines
511 * to the file, so there are no trailing context lines).
513 hunk
->splittable_into
++;
518 static size_t find_next_line(struct strbuf
*sb
, size_t offset
)
522 if (offset
>= sb
->len
)
523 BUG("looking for next line beyond buffer (%d >= %d)\n%s",
524 (int)offset
, (int)sb
->len
, sb
->buf
);
526 eol
= memchr(sb
->buf
+ offset
, '\n', sb
->len
- offset
);
529 return eol
- sb
->buf
+ 1;
532 static void render_hunk(struct add_p_state
*s
, struct hunk
*hunk
,
533 ssize_t delta
, int colored
, struct strbuf
*out
)
535 struct hunk_header
*header
= &hunk
->header
;
537 if (hunk
->header
.old_offset
!= 0 || hunk
->header
.new_offset
!= 0) {
539 * Generate the hunk header dynamically, except for special
540 * hunks (such as the diff header).
544 unsigned long old_offset
= header
->old_offset
;
545 unsigned long new_offset
= header
->new_offset
;
548 p
= s
->plain
.buf
+ header
->extra_start
;
549 len
= header
->extra_end
- header
->extra_start
;
551 strbuf_addstr(out
, s
->s
.fraginfo_color
);
552 p
= s
->colored
.buf
+ header
->colored_extra_start
;
553 len
= header
->colored_extra_end
554 - header
->colored_extra_start
;
557 if (s
->mode
->is_reverse
)
562 strbuf_addf(out
, "@@ -%lu,%lu +%lu,%lu @@",
563 old_offset
, header
->old_count
,
564 new_offset
, header
->new_count
);
566 strbuf_add(out
, p
, len
);
568 strbuf_addf(out
, "%s\n", GIT_COLOR_RESET
);
570 strbuf_addch(out
, '\n');
574 strbuf_add(out
, s
->colored
.buf
+ hunk
->colored_start
,
575 hunk
->colored_end
- hunk
->colored_start
);
577 strbuf_add(out
, s
->plain
.buf
+ hunk
->start
,
578 hunk
->end
- hunk
->start
);
581 static void render_diff_header(struct add_p_state
*s
,
582 struct file_diff
*file_diff
, int colored
,
586 * If there was a mode change, the first hunk is a pseudo hunk that
587 * corresponds to the mode line in the header. If the user did not want
588 * to stage that "hunk", we actually have to cut it out from the header.
590 int skip_mode_change
=
591 file_diff
->mode_change
&& file_diff
->hunk
->use
!= USE_HUNK
;
592 struct hunk
*head
= &file_diff
->head
, *first
= file_diff
->hunk
;
594 if (!skip_mode_change
) {
595 render_hunk(s
, head
, 0, colored
, out
);
600 const char *p
= s
->colored
.buf
;
602 strbuf_add(out
, p
+ head
->colored_start
,
603 first
->colored_start
- head
->colored_start
);
604 strbuf_add(out
, p
+ first
->colored_end
,
605 head
->colored_end
- first
->colored_end
);
607 const char *p
= s
->plain
.buf
;
609 strbuf_add(out
, p
+ head
->start
, first
->start
- head
->start
);
610 strbuf_add(out
, p
+ first
->end
, head
->end
- first
->end
);
614 /* Coalesce hunks again that were split */
615 static int merge_hunks(struct add_p_state
*s
, struct file_diff
*file_diff
,
616 size_t *hunk_index
, int use_all
, struct hunk
*merged
)
618 size_t i
= *hunk_index
, delta
;
619 struct hunk
*hunk
= file_diff
->hunk
+ i
;
620 /* `header` corresponds to the merged hunk */
621 struct hunk_header
*header
= &merged
->header
, *next
;
623 if (!use_all
&& hunk
->use
!= USE_HUNK
)
627 /* We simply skip the colored part (if any) when merging hunks */
628 merged
->colored_start
= merged
->colored_end
= 0;
630 for (; i
+ 1 < file_diff
->hunk_nr
; i
++) {
632 next
= &hunk
->header
;
635 * Stop merging hunks when:
637 * - the hunk is not selected for use, or
638 * - the hunk does not overlap with the already-merged hunk(s)
640 if ((!use_all
&& hunk
->use
!= USE_HUNK
) ||
641 header
->new_offset
>= next
->new_offset
+ merged
->delta
||
642 header
->new_offset
+ header
->new_count
643 < next
->new_offset
+ merged
->delta
)
647 * If the hunks were not edited, and overlap, we can simply
648 * extend the line range.
650 if (merged
->start
< hunk
->start
&& merged
->end
> hunk
->start
) {
651 merged
->end
= hunk
->end
;
652 merged
->colored_end
= hunk
->colored_end
;
655 const char *plain
= s
->plain
.buf
;
656 size_t overlapping_line_count
= header
->new_offset
657 + header
->new_count
- merged
->delta
659 size_t overlap_end
= hunk
->start
;
660 size_t overlap_start
= overlap_end
;
661 size_t overlap_next
, len
, j
;
664 * One of the hunks was edited: the modified hunk was
665 * appended to the strbuf `s->plain`.
667 * Let's ensure that at least the last context line of
668 * the first hunk overlaps with the corresponding line
669 * of the second hunk, and then merge.
671 for (j
= 0; j
< overlapping_line_count
; j
++) {
672 overlap_next
= find_next_line(&s
->plain
,
675 if (overlap_next
> hunk
->end
)
676 BUG("failed to find %d context lines "
678 (int)overlapping_line_count
,
679 (int)(hunk
->end
- hunk
->start
),
680 plain
+ hunk
->start
);
682 if (plain
[overlap_end
] != ' ')
683 return error(_("expected context line "
688 plain
+ hunk
->start
);
690 overlap_start
= overlap_end
;
691 overlap_end
= overlap_next
;
693 len
= overlap_end
- overlap_start
;
695 if (len
> merged
->end
- merged
->start
||
696 memcmp(plain
+ merged
->end
- len
,
697 plain
+ overlap_start
, len
))
698 return error(_("hunks do not overlap:\n%.*s\n"
699 "\tdoes not end with:\n%.*s"),
700 (int)(merged
->end
- merged
->start
),
701 plain
+ merged
->start
,
702 (int)len
, plain
+ overlap_start
);
705 * Since the start-end ranges are not adjacent, we
706 * cannot simply take the union of the ranges. To
707 * address that, we temporarily append the union of the
708 * lines to the `plain` strbuf.
710 if (merged
->end
!= s
->plain
.len
) {
711 size_t start
= s
->plain
.len
;
713 strbuf_add(&s
->plain
, plain
+ merged
->start
,
714 merged
->end
- merged
->start
);
715 plain
= s
->plain
.buf
;
716 merged
->start
= start
;
717 merged
->end
= s
->plain
.len
;
720 strbuf_add(&s
->plain
,
722 hunk
->end
- overlap_end
);
723 merged
->end
= s
->plain
.len
;
724 merged
->splittable_into
+= hunk
->splittable_into
;
725 delta
= merged
->delta
;
726 merged
->delta
+= hunk
->delta
;
729 header
->old_count
= next
->old_offset
+ next
->old_count
730 - header
->old_offset
;
731 header
->new_count
= next
->new_offset
+ delta
732 + next
->new_count
- header
->new_offset
;
735 if (i
== *hunk_index
)
742 static void reassemble_patch(struct add_p_state
*s
,
743 struct file_diff
*file_diff
, int use_all
,
747 size_t save_len
= s
->plain
.len
, i
;
750 render_diff_header(s
, file_diff
, 0, out
);
752 for (i
= file_diff
->mode_change
; i
< file_diff
->hunk_nr
; i
++) {
753 struct hunk merged
= { 0 };
755 hunk
= file_diff
->hunk
+ i
;
756 if (!use_all
&& hunk
->use
!= USE_HUNK
)
757 delta
+= hunk
->header
.old_count
758 - hunk
->header
.new_count
;
760 /* merge overlapping hunks into a temporary hunk */
761 if (merge_hunks(s
, file_diff
, &i
, use_all
, &merged
))
764 render_hunk(s
, hunk
, delta
, 0, out
);
767 * In case `merge_hunks()` used `plain` as a scratch
768 * pad (this happens when an edited hunk had to be
769 * coalesced with another hunk).
771 strbuf_setlen(&s
->plain
, save_len
);
773 delta
+= hunk
->delta
;
778 static int split_hunk(struct add_p_state
*s
, struct file_diff
*file_diff
,
781 int colored
= !!s
->colored
.len
, first
= 1;
782 struct hunk
*hunk
= file_diff
->hunk
+ hunk_index
;
783 size_t splittable_into
;
784 size_t end
, colored_end
, current
, colored_current
= 0, context_line_count
;
785 struct hunk_header remaining
, *header
;
788 if (hunk_index
>= file_diff
->hunk_nr
)
789 BUG("invalid hunk index: %d (must be >= 0 and < %d)",
790 (int)hunk_index
, (int)file_diff
->hunk_nr
);
792 if (hunk
->splittable_into
< 2)
794 splittable_into
= hunk
->splittable_into
;
797 colored_end
= hunk
->colored_end
;
799 remaining
= hunk
->header
;
801 file_diff
->hunk_nr
+= splittable_into
- 1;
802 ALLOC_GROW(file_diff
->hunk
, file_diff
->hunk_nr
, file_diff
->hunk_alloc
);
803 if (hunk_index
+ splittable_into
< file_diff
->hunk_nr
)
804 memmove(file_diff
->hunk
+ hunk_index
+ splittable_into
,
805 file_diff
->hunk
+ hunk_index
+ 1,
806 (file_diff
->hunk_nr
- hunk_index
- splittable_into
)
808 hunk
= file_diff
->hunk
+ hunk_index
;
809 hunk
->splittable_into
= 1;
810 memset(hunk
+ 1, 0, (splittable_into
- 1) * sizeof(*hunk
));
812 header
= &hunk
->header
;
813 header
->old_count
= header
->new_count
= 0;
815 current
= hunk
->start
;
817 colored_current
= hunk
->colored_start
;
819 context_line_count
= 0;
821 while (splittable_into
> 1) {
822 ch
= s
->plain
.buf
[current
];
825 BUG("buffer overrun while splitting hunks");
828 * Is this the first context line after a chain of +/- lines?
829 * Then record the start of the next split hunk.
831 if ((marker
== '-' || marker
== '+') && ch
== ' ') {
833 hunk
[1].start
= current
;
835 hunk
[1].colored_start
= colored_current
;
836 context_line_count
= 0;
840 * Was the previous line a +/- one? Alternatively, is this the
841 * first line (and not a +/- one)?
843 * Then just increment the appropriate counter and continue
844 * with the next line.
846 if (marker
!= ' ' || (ch
!= '-' && ch
!= '+')) {
848 /* Comment lines are attached to the previous line */
850 ch
= marker
? marker
: ' ';
852 /* current hunk not done yet */
854 context_line_count
++;
860 BUG("unhandled diff marker: '%c'", ch
);
862 current
= find_next_line(&s
->plain
, current
);
865 find_next_line(&s
->colored
,
871 * We got us the start of a new hunk!
873 * This is a context line, so it is shared with the previous
878 if (header
->old_count
|| header
->new_count
)
879 BUG("counts are off: %d/%d",
880 (int)header
->old_count
,
881 (int)header
->new_count
);
883 header
->old_count
= context_line_count
;
884 header
->new_count
= context_line_count
;
885 context_line_count
= 0;
890 remaining
.old_offset
+= header
->old_count
;
891 remaining
.old_count
-= header
->old_count
;
892 remaining
.new_offset
+= header
->new_count
;
893 remaining
.new_count
-= header
->new_count
;
895 /* initialize next hunk header's offsets */
896 hunk
[1].header
.old_offset
=
897 header
->old_offset
+ header
->old_count
;
898 hunk
[1].header
.new_offset
=
899 header
->new_offset
+ header
->new_count
;
901 /* add one split hunk */
902 header
->old_count
+= context_line_count
;
903 header
->new_count
+= context_line_count
;
907 hunk
->colored_end
= colored_current
;
910 hunk
->splittable_into
= 1;
911 hunk
->use
= hunk
[-1].use
;
912 header
= &hunk
->header
;
914 header
->old_count
= header
->new_count
= context_line_count
;
915 context_line_count
= 0;
921 /* last hunk simply gets the rest */
922 if (header
->old_offset
!= remaining
.old_offset
)
923 BUG("miscounted old_offset: %lu != %lu",
924 header
->old_offset
, remaining
.old_offset
);
925 if (header
->new_offset
!= remaining
.new_offset
)
926 BUG("miscounted new_offset: %lu != %lu",
927 header
->new_offset
, remaining
.new_offset
);
928 header
->old_count
= remaining
.old_count
;
929 header
->new_count
= remaining
.new_count
;
932 hunk
->colored_end
= colored_end
;
937 static void recolor_hunk(struct add_p_state
*s
, struct hunk
*hunk
)
939 const char *plain
= s
->plain
.buf
;
940 size_t current
, eol
, next
;
945 hunk
->colored_start
= s
->colored
.len
;
946 for (current
= hunk
->start
; current
< hunk
->end
; ) {
947 for (eol
= current
; eol
< hunk
->end
; eol
++)
948 if (plain
[eol
] == '\n')
950 next
= eol
+ (eol
< hunk
->end
);
951 if (eol
> current
&& plain
[eol
- 1] == '\r')
954 strbuf_addstr(&s
->colored
,
955 plain
[current
] == '-' ?
956 s
->s
.file_old_color
:
957 plain
[current
] == '+' ?
958 s
->s
.file_new_color
:
960 strbuf_add(&s
->colored
, plain
+ current
, eol
- current
);
961 strbuf_addstr(&s
->colored
, GIT_COLOR_RESET
);
963 strbuf_add(&s
->colored
, plain
+ eol
, next
- eol
);
966 hunk
->colored_end
= s
->colored
.len
;
969 static int edit_hunk_manually(struct add_p_state
*s
, struct hunk
*hunk
)
973 strbuf_reset(&s
->buf
);
974 strbuf_commented_addf(&s
->buf
, _("Manual hunk edit mode -- see bottom for "
975 "a quick guide.\n"));
976 render_hunk(s
, hunk
, 0, 0, &s
->buf
);
977 strbuf_commented_addf(&s
->buf
,
979 "To remove '%c' lines, make them ' ' lines "
981 "To remove '%c' lines, delete them.\n"
982 "Lines starting with %c will be removed.\n"),
983 s
->mode
->is_reverse
? '+' : '-',
984 s
->mode
->is_reverse
? '-' : '+',
986 strbuf_commented_addf(&s
->buf
, "%s", _(s
->mode
->edit_hunk_hint
));
988 * TRANSLATORS: 'it' refers to the patch mentioned in the previous
991 strbuf_commented_addf(&s
->buf
,
992 _("If it does not apply cleanly, you will be "
993 "given an opportunity to\n"
994 "edit again. If all lines of the hunk are "
995 "removed, then the edit is\n"
996 "aborted and the hunk is left unchanged.\n"));
998 if (strbuf_edit_interactively(&s
->buf
, "addp-hunk-edit.diff", NULL
) < 0)
1001 /* strip out commented lines */
1002 hunk
->start
= s
->plain
.len
;
1003 for (i
= 0; i
< s
->buf
.len
; ) {
1004 size_t next
= find_next_line(&s
->buf
, i
);
1006 if (s
->buf
.buf
[i
] != comment_line_char
)
1007 strbuf_add(&s
->plain
, s
->buf
.buf
+ i
, next
- i
);
1011 hunk
->end
= s
->plain
.len
;
1012 if (hunk
->end
== hunk
->start
)
1013 /* The user aborted editing by deleting everything */
1016 recolor_hunk(s
, hunk
);
1019 * If the hunk header is intact, parse it, otherwise simply use the
1020 * hunk header prior to editing (which will adjust `hunk->start` to
1021 * skip the hunk header).
1023 if (s
->plain
.buf
[hunk
->start
] == '@' &&
1024 parse_hunk_header(s
, hunk
) < 0)
1025 return error(_("could not parse hunk header"));
1030 static ssize_t
recount_edited_hunk(struct add_p_state
*s
, struct hunk
*hunk
,
1031 size_t orig_old_count
, size_t orig_new_count
)
1033 struct hunk_header
*header
= &hunk
->header
;
1036 header
->old_count
= header
->new_count
= 0;
1037 for (i
= hunk
->start
; i
< hunk
->end
; ) {
1038 switch (s
->plain
.buf
[i
]) {
1040 header
->old_count
++;
1043 header
->new_count
++;
1045 case ' ': case '\r': case '\n':
1046 header
->old_count
++;
1047 header
->new_count
++;
1051 i
= find_next_line(&s
->plain
, i
);
1054 return orig_old_count
- orig_new_count
1055 - header
->old_count
+ header
->new_count
;
1058 static int run_apply_check(struct add_p_state
*s
,
1059 struct file_diff
*file_diff
)
1061 struct child_process cp
= CHILD_PROCESS_INIT
;
1063 strbuf_reset(&s
->buf
);
1064 reassemble_patch(s
, file_diff
, 1, &s
->buf
);
1066 setup_child_process(s
, &cp
,
1067 "apply", "--check", NULL
);
1068 argv_array_pushv(&cp
.args
, s
->mode
->apply_check_args
);
1069 if (pipe_command(&cp
, s
->buf
.buf
, s
->buf
.len
, NULL
, 0, NULL
, 0))
1070 return error(_("'git apply --cached' failed"));
1075 static int prompt_yesno(struct add_p_state
*s
, const char *prompt
)
1078 color_fprintf(stdout
, s
->s
.prompt_color
, "%s", _(prompt
));
1080 if (strbuf_getline(&s
->answer
, stdin
) == EOF
)
1082 strbuf_trim_trailing_newline(&s
->answer
);
1083 switch (tolower(s
->answer
.buf
[0])) {
1090 static int edit_hunk_loop(struct add_p_state
*s
,
1091 struct file_diff
*file_diff
, struct hunk
*hunk
)
1093 size_t plain_len
= s
->plain
.len
, colored_len
= s
->colored
.len
;
1099 int res
= edit_hunk_manually(s
, hunk
);
1108 recount_edited_hunk(s
, hunk
,
1109 backup
.header
.old_count
,
1110 backup
.header
.new_count
);
1111 if (!run_apply_check(s
, file_diff
))
1115 /* Drop edits (they were appended to s->plain) */
1116 strbuf_setlen(&s
->plain
, plain_len
);
1117 strbuf_setlen(&s
->colored
, colored_len
);
1121 * TRANSLATORS: do not translate [y/n]
1122 * The program will only accept that input at this point.
1123 * Consider translating (saying "no" discards!) as
1124 * (saying "n" for "no" discards!) if the translation
1125 * of the word "no" does not start with n.
1127 res
= prompt_yesno(s
, _("Your edited hunk does not apply. "
1128 "Edit again (saying \"no\" discards!) "
1135 static int apply_for_checkout(struct add_p_state
*s
, struct strbuf
*diff
,
1138 const char *reverse
= is_reverse
? "-R" : NULL
;
1139 struct child_process check_index
= CHILD_PROCESS_INIT
;
1140 struct child_process check_worktree
= CHILD_PROCESS_INIT
;
1141 struct child_process apply_index
= CHILD_PROCESS_INIT
;
1142 struct child_process apply_worktree
= CHILD_PROCESS_INIT
;
1143 int applies_index
, applies_worktree
;
1145 setup_child_process(s
, &check_index
,
1146 "apply", "--cached", "--check", reverse
, NULL
);
1147 applies_index
= !pipe_command(&check_index
, diff
->buf
, diff
->len
,
1150 setup_child_process(s
, &check_worktree
,
1151 "apply", "--check", reverse
, NULL
);
1152 applies_worktree
= !pipe_command(&check_worktree
, diff
->buf
, diff
->len
,
1155 if (applies_worktree
&& applies_index
) {
1156 setup_child_process(s
, &apply_index
,
1157 "apply", "--cached", reverse
, NULL
);
1158 pipe_command(&apply_index
, diff
->buf
, diff
->len
,
1161 setup_child_process(s
, &apply_worktree
,
1162 "apply", reverse
, NULL
);
1163 pipe_command(&apply_worktree
, diff
->buf
, diff
->len
,
1169 if (!applies_index
) {
1170 err(s
, _("The selected hunks do not apply to the index!"));
1171 if (prompt_yesno(s
, _("Apply them to the worktree "
1173 setup_child_process(s
, &apply_worktree
,
1174 "apply", reverse
, NULL
);
1175 return pipe_command(&apply_worktree
, diff
->buf
,
1176 diff
->len
, NULL
, 0, NULL
, 0);
1178 err(s
, _("Nothing was applied.\n"));
1180 /* As a last resort, show the diff to the user */
1181 fwrite(diff
->buf
, diff
->len
, 1, stderr
);
1186 #define SUMMARY_HEADER_WIDTH 20
1187 #define SUMMARY_LINE_WIDTH 80
1188 static void summarize_hunk(struct add_p_state
*s
, struct hunk
*hunk
,
1191 struct hunk_header
*header
= &hunk
->header
;
1192 struct strbuf
*plain
= &s
->plain
;
1193 size_t len
= out
->len
, i
;
1195 strbuf_addf(out
, " -%lu,%lu +%lu,%lu ",
1196 header
->old_offset
, header
->old_count
,
1197 header
->new_offset
, header
->new_count
);
1198 if (out
->len
- len
< SUMMARY_HEADER_WIDTH
)
1199 strbuf_addchars(out
, ' ',
1200 SUMMARY_HEADER_WIDTH
+ len
- out
->len
);
1201 for (i
= hunk
->start
; i
< hunk
->end
; i
= find_next_line(plain
, i
))
1202 if (plain
->buf
[i
] != ' ')
1205 strbuf_add(out
, plain
->buf
+ i
, find_next_line(plain
, i
) - i
);
1206 if (out
->len
- len
> SUMMARY_LINE_WIDTH
)
1207 strbuf_setlen(out
, len
+ SUMMARY_LINE_WIDTH
);
1208 strbuf_complete_line(out
);
1211 #define DISPLAY_HUNKS_LINES 20
1212 static size_t display_hunks(struct add_p_state
*s
,
1213 struct file_diff
*file_diff
, size_t start_index
)
1215 size_t end_index
= start_index
+ DISPLAY_HUNKS_LINES
;
1217 if (end_index
> file_diff
->hunk_nr
)
1218 end_index
= file_diff
->hunk_nr
;
1220 while (start_index
< end_index
) {
1221 struct hunk
*hunk
= file_diff
->hunk
+ start_index
++;
1223 strbuf_reset(&s
->buf
);
1224 strbuf_addf(&s
->buf
, "%c%2d: ", hunk
->use
== USE_HUNK
? '+'
1225 : hunk
->use
== SKIP_HUNK
? '-' : ' ',
1227 summarize_hunk(s
, hunk
, &s
->buf
);
1228 fputs(s
->buf
.buf
, stdout
);
1234 static const char help_patch_remainder
[] =
1235 N_("j - leave this hunk undecided, see next undecided hunk\n"
1236 "J - leave this hunk undecided, see next hunk\n"
1237 "k - leave this hunk undecided, see previous undecided hunk\n"
1238 "K - leave this hunk undecided, see previous hunk\n"
1239 "g - select a hunk to go to\n"
1240 "/ - search for a hunk matching the given regex\n"
1241 "s - split the current hunk into smaller hunks\n"
1242 "e - manually edit the current hunk\n"
1243 "? - print help\n");
1245 static int patch_update_file(struct add_p_state
*s
,
1246 struct file_diff
*file_diff
)
1248 size_t hunk_index
= 0;
1249 ssize_t i
, undecided_previous
, undecided_next
;
1252 struct child_process cp
= CHILD_PROCESS_INIT
;
1253 int colored
= !!s
->colored
.len
, quit
= 0;
1254 enum prompt_mode_type prompt_mode_type
;
1256 if (!file_diff
->hunk_nr
)
1259 strbuf_reset(&s
->buf
);
1260 render_diff_header(s
, file_diff
, colored
, &s
->buf
);
1261 fputs(s
->buf
.buf
, stdout
);
1263 if (hunk_index
>= file_diff
->hunk_nr
)
1265 hunk
= file_diff
->hunk
+ hunk_index
;
1267 undecided_previous
= -1;
1268 for (i
= hunk_index
- 1; i
>= 0; i
--)
1269 if (file_diff
->hunk
[i
].use
== UNDECIDED_HUNK
) {
1270 undecided_previous
= i
;
1274 undecided_next
= -1;
1275 for (i
= hunk_index
+ 1; i
< file_diff
->hunk_nr
; i
++)
1276 if (file_diff
->hunk
[i
].use
== UNDECIDED_HUNK
) {
1281 /* Everything decided? */
1282 if (undecided_previous
< 0 && undecided_next
< 0 &&
1283 hunk
->use
!= UNDECIDED_HUNK
)
1286 strbuf_reset(&s
->buf
);
1287 render_hunk(s
, hunk
, 0, colored
, &s
->buf
);
1288 fputs(s
->buf
.buf
, stdout
);
1290 strbuf_reset(&s
->buf
);
1291 if (undecided_previous
>= 0)
1292 strbuf_addstr(&s
->buf
, ",k");
1294 strbuf_addstr(&s
->buf
, ",K");
1295 if (undecided_next
>= 0)
1296 strbuf_addstr(&s
->buf
, ",j");
1297 if (hunk_index
+ 1 < file_diff
->hunk_nr
)
1298 strbuf_addstr(&s
->buf
, ",J");
1299 if (file_diff
->hunk_nr
> 1)
1300 strbuf_addstr(&s
->buf
, ",g,/");
1301 if (hunk
->splittable_into
> 1)
1302 strbuf_addstr(&s
->buf
, ",s");
1303 if (hunk_index
+ 1 > file_diff
->mode_change
&&
1304 !file_diff
->deleted
)
1305 strbuf_addstr(&s
->buf
, ",e");
1307 if (file_diff
->deleted
)
1308 prompt_mode_type
= PROMPT_DELETION
;
1309 else if (file_diff
->mode_change
&& !hunk_index
)
1310 prompt_mode_type
= PROMPT_MODE_CHANGE
;
1312 prompt_mode_type
= PROMPT_HUNK
;
1314 color_fprintf(stdout
, s
->s
.prompt_color
,
1315 "(%"PRIuMAX
"/%"PRIuMAX
") ",
1316 (uintmax_t)hunk_index
+ 1,
1317 (uintmax_t)file_diff
->hunk_nr
);
1318 color_fprintf(stdout
, s
->s
.prompt_color
,
1319 _(s
->mode
->prompt_mode
[prompt_mode_type
]),
1322 if (strbuf_getline(&s
->answer
, stdin
) == EOF
)
1324 strbuf_trim_trailing_newline(&s
->answer
);
1328 ch
= tolower(s
->answer
.buf
[0]);
1330 hunk
->use
= USE_HUNK
;
1332 hunk_index
= undecided_next
< 0 ?
1333 file_diff
->hunk_nr
: undecided_next
;
1334 } else if (ch
== 'n') {
1335 hunk
->use
= SKIP_HUNK
;
1336 goto soft_increment
;
1337 } else if (ch
== 'a') {
1338 for (; hunk_index
< file_diff
->hunk_nr
; hunk_index
++) {
1339 hunk
= file_diff
->hunk
+ hunk_index
;
1340 if (hunk
->use
== UNDECIDED_HUNK
)
1341 hunk
->use
= USE_HUNK
;
1343 } else if (ch
== 'd' || ch
== 'q') {
1344 for (; hunk_index
< file_diff
->hunk_nr
; hunk_index
++) {
1345 hunk
= file_diff
->hunk
+ hunk_index
;
1346 if (hunk
->use
== UNDECIDED_HUNK
)
1347 hunk
->use
= SKIP_HUNK
;
1353 } else if (s
->answer
.buf
[0] == 'K') {
1357 err(s
, _("No previous hunk"));
1358 } else if (s
->answer
.buf
[0] == 'J') {
1359 if (hunk_index
+ 1 < file_diff
->hunk_nr
)
1362 err(s
, _("No next hunk"));
1363 } else if (s
->answer
.buf
[0] == 'k') {
1364 if (undecided_previous
>= 0)
1365 hunk_index
= undecided_previous
;
1367 err(s
, _("No previous hunk"));
1368 } else if (s
->answer
.buf
[0] == 'j') {
1369 if (undecided_next
>= 0)
1370 hunk_index
= undecided_next
;
1372 err(s
, _("No next hunk"));
1373 } else if (s
->answer
.buf
[0] == 'g') {
1375 unsigned long response
;
1377 if (file_diff
->hunk_nr
< 2) {
1378 err(s
, _("No other hunks to goto"));
1381 strbuf_remove(&s
->answer
, 0, 1);
1382 strbuf_trim(&s
->answer
);
1383 i
= hunk_index
- DISPLAY_HUNKS_LINES
/ 2;
1384 if (i
< file_diff
->mode_change
)
1385 i
= file_diff
->mode_change
;
1386 while (s
->answer
.len
== 0) {
1387 i
= display_hunks(s
, file_diff
, i
);
1388 printf("%s", i
< file_diff
->hunk_nr
?
1389 _("go to which hunk (<ret> to see "
1390 "more)? ") : _("go to which hunk? "));
1392 if (strbuf_getline(&s
->answer
,
1395 strbuf_trim_trailing_newline(&s
->answer
);
1398 strbuf_trim(&s
->answer
);
1399 response
= strtoul(s
->answer
.buf
, &pend
, 10);
1400 if (*pend
|| pend
== s
->answer
.buf
)
1401 err(s
, _("Invalid number: '%s'"),
1403 else if (0 < response
&& response
<= file_diff
->hunk_nr
)
1404 hunk_index
= response
- 1;
1406 err(s
, Q_("Sorry, only %d hunk available.",
1407 "Sorry, only %d hunks available.",
1408 file_diff
->hunk_nr
),
1409 (int)file_diff
->hunk_nr
);
1410 } else if (s
->answer
.buf
[0] == '/') {
1414 if (file_diff
->hunk_nr
< 2) {
1415 err(s
, _("No other hunks to search"));
1418 strbuf_remove(&s
->answer
, 0, 1);
1419 strbuf_trim_trailing_newline(&s
->answer
);
1420 if (s
->answer
.len
== 0) {
1421 printf("%s", _("search for regex? "));
1423 if (strbuf_getline(&s
->answer
,
1426 strbuf_trim_trailing_newline(&s
->answer
);
1427 if (s
->answer
.len
== 0)
1430 ret
= regcomp(®ex
, s
->answer
.buf
,
1431 REG_EXTENDED
| REG_NOSUB
| REG_NEWLINE
);
1435 regerror(ret
, ®ex
, errbuf
, sizeof(errbuf
));
1436 err(s
, _("Malformed search regexp %s: %s"),
1437 s
->answer
.buf
, errbuf
);
1442 /* render the hunk into a scratch buffer */
1443 render_hunk(s
, file_diff
->hunk
+ i
, 0, 0,
1445 if (regexec(®ex
, s
->buf
.buf
, 0, NULL
, 0)
1449 if (i
== file_diff
->hunk_nr
)
1451 if (i
!= hunk_index
)
1453 err(s
, _("No hunk matches the given pattern"));
1457 } else if (s
->answer
.buf
[0] == 's') {
1458 size_t splittable_into
= hunk
->splittable_into
;
1459 if (splittable_into
< 2)
1460 err(s
, _("Sorry, cannot split this hunk"));
1461 else if (!split_hunk(s
, file_diff
,
1462 hunk
- file_diff
->hunk
))
1463 color_fprintf_ln(stdout
, s
->s
.header_color
,
1464 _("Split into %d hunks."),
1465 (int)splittable_into
);
1466 } else if (s
->answer
.buf
[0] == 'e') {
1467 if (hunk_index
+ 1 == file_diff
->mode_change
)
1468 err(s
, _("Sorry, cannot edit this hunk"));
1469 else if (edit_hunk_loop(s
, file_diff
, hunk
) >= 0) {
1470 hunk
->use
= USE_HUNK
;
1471 goto soft_increment
;
1474 const char *p
= _(help_patch_remainder
), *eol
= p
;
1476 color_fprintf(stdout
, s
->s
.help_color
, "%s",
1477 _(s
->mode
->help_patch_text
));
1480 * Show only those lines of the remainder that are
1481 * actually applicable with the current hunk.
1483 for (; *p
; p
= eol
+ (*eol
== '\n')) {
1484 eol
= strchrnul(p
, '\n');
1487 * `s->buf` still contains the part of the
1488 * commands shown in the prompt that are not
1491 if (*p
!= '?' && !strchr(s
->buf
.buf
, *p
))
1494 color_fprintf_ln(stdout
, s
->s
.help_color
,
1495 "%.*s", (int)(eol
- p
), p
);
1500 /* Any hunk to be used? */
1501 for (i
= 0; i
< file_diff
->hunk_nr
; i
++)
1502 if (file_diff
->hunk
[i
].use
== USE_HUNK
)
1505 if (i
< file_diff
->hunk_nr
) {
1506 /* At least one hunk selected: apply */
1507 strbuf_reset(&s
->buf
);
1508 reassemble_patch(s
, file_diff
, 0, &s
->buf
);
1510 discard_index(s
->s
.r
->index
);
1511 if (s
->mode
->apply_for_checkout
)
1512 apply_for_checkout(s
, &s
->buf
,
1513 s
->mode
->is_reverse
);
1515 setup_child_process(s
, &cp
, "apply", NULL
);
1516 argv_array_pushv(&cp
.args
, s
->mode
->apply_args
);
1517 if (pipe_command(&cp
, s
->buf
.buf
, s
->buf
.len
,
1519 error(_("'git apply' failed"));
1521 if (!repo_read_index(s
->s
.r
))
1522 repo_refresh_and_write_index(s
->s
.r
, REFRESH_QUIET
, 0,
1523 1, NULL
, NULL
, NULL
);
1530 int run_add_p(struct repository
*r
, enum add_p_mode mode
,
1531 const char *revision
, const struct pathspec
*ps
)
1533 struct add_p_state s
= {
1534 { r
}, STRBUF_INIT
, STRBUF_INIT
, STRBUF_INIT
, STRBUF_INIT
1536 size_t i
, binary_count
= 0;
1538 init_add_i_state(&s
.s
, r
);
1540 if (mode
== ADD_P_STASH
)
1541 s
.mode
= &patch_mode_stash
;
1542 else if (mode
== ADD_P_RESET
) {
1543 if (!revision
|| !strcmp(revision
, "HEAD"))
1544 s
.mode
= &patch_mode_reset_head
;
1546 s
.mode
= &patch_mode_reset_nothead
;
1547 } else if (mode
== ADD_P_CHECKOUT
) {
1549 s
.mode
= &patch_mode_checkout_index
;
1550 else if (!strcmp(revision
, "HEAD"))
1551 s
.mode
= &patch_mode_checkout_head
;
1553 s
.mode
= &patch_mode_checkout_nothead
;
1555 s
.mode
= &patch_mode_add
;
1556 s
.revision
= revision
;
1558 if (discard_index(r
->index
) < 0 || repo_read_index(r
) < 0 ||
1559 (!s
.mode
->index_only
&&
1560 repo_refresh_and_write_index(r
, REFRESH_QUIET
, 0, 1,
1561 NULL
, NULL
, NULL
) < 0) ||
1562 parse_diff(&s
, ps
) < 0) {
1563 strbuf_release(&s
.plain
);
1564 strbuf_release(&s
.colored
);
1568 for (i
= 0; i
< s
.file_diff_nr
; i
++)
1569 if (s
.file_diff
[i
].binary
&& !s
.file_diff
[i
].hunk_nr
)
1571 else if (patch_update_file(&s
, s
.file_diff
+ i
))
1574 if (s
.file_diff_nr
== 0)
1575 fprintf(stderr
, _("No changes.\n"));
1576 else if (binary_count
== s
.file_diff_nr
)
1577 fprintf(stderr
, _("Only binary files changed.\n"));
1579 strbuf_release(&s
.answer
);
1580 strbuf_release(&s
.buf
);
1581 strbuf_release(&s
.plain
);
1582 strbuf_release(&s
.colored
);