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
14 static const char *prompt_mode
[] = {
15 N_("Stage mode change [y,n,a,d%s,?]? "),
16 N_("Stage deletion [y,n,a,d%s,?]? "),
17 N_("Stage this hunk [y,n,a,d%s,?]? ")
21 unsigned long old_offset
, old_count
, new_offset
, new_count
;
23 * Start/end offsets to the extra text after the second `@@` in the
24 * hunk header, e.g. the function signature. This is expected to
25 * include the newline.
27 size_t extra_start
, extra_end
, colored_extra_start
, colored_extra_end
;
31 size_t start
, end
, colored_start
, colored_end
;
32 enum { UNDECIDED_HUNK
= 0, SKIP_HUNK
, USE_HUNK
} use
;
33 struct hunk_header header
;
38 struct strbuf answer
, buf
;
41 struct strbuf plain
, colored
;
45 size_t hunk_nr
, hunk_alloc
;
46 unsigned deleted
:1, mode_change
:1;
51 static void err(struct add_p_state
*s
, const char *fmt
, ...)
56 fputs(s
->s
.error_color
, stderr
);
57 vfprintf(stderr
, fmt
, args
);
58 fputs(s
->s
.reset_color
, stderr
);
63 static void setup_child_process(struct add_p_state
*s
,
64 struct child_process
*cp
, ...)
70 while ((arg
= va_arg(ap
, const char *)))
71 argv_array_push(&cp
->args
, arg
);
75 argv_array_pushf(&cp
->env_array
,
76 INDEX_ENVIRONMENT
"=%s", s
->s
.r
->index_file
);
79 static int parse_range(const char **p
,
80 unsigned long *offset
, unsigned long *count
)
84 *offset
= strtoul(*p
, &pend
, 10);
92 *count
= strtoul(pend
+ 1, (char **)p
, 10);
93 return *p
== pend
+ 1 ? -1 : 0;
96 static int parse_hunk_header(struct add_p_state
*s
, struct hunk
*hunk
)
98 struct hunk_header
*header
= &hunk
->header
;
99 const char *line
= s
->plain
.buf
+ hunk
->start
, *p
= line
;
100 char *eol
= memchr(p
, '\n', s
->plain
.len
- hunk
->start
);
103 eol
= s
->plain
.buf
+ s
->plain
.len
;
105 if (!skip_prefix(p
, "@@ -", &p
) ||
106 parse_range(&p
, &header
->old_offset
, &header
->old_count
) < 0 ||
107 !skip_prefix(p
, " +", &p
) ||
108 parse_range(&p
, &header
->new_offset
, &header
->new_count
) < 0 ||
109 !skip_prefix(p
, " @@", &p
))
110 return error(_("could not parse hunk header '%.*s'"),
111 (int)(eol
- line
), line
);
113 hunk
->start
= eol
- s
->plain
.buf
+ (*eol
== '\n');
114 header
->extra_start
= p
- s
->plain
.buf
;
115 header
->extra_end
= hunk
->start
;
117 if (!s
->colored
.len
) {
118 header
->colored_extra_start
= header
->colored_extra_end
= 0;
122 /* Now find the extra text in the colored diff */
123 line
= s
->colored
.buf
+ hunk
->colored_start
;
124 eol
= memchr(line
, '\n', s
->colored
.len
- hunk
->colored_start
);
126 eol
= s
->colored
.buf
+ s
->colored
.len
;
127 p
= memmem(line
, eol
- line
, "@@ -", 4);
129 return error(_("could not parse colored hunk header '%.*s'"),
130 (int)(eol
- line
), line
);
131 p
= memmem(p
+ 4, eol
- p
- 4, " @@", 3);
133 return error(_("could not parse colored hunk header '%.*s'"),
134 (int)(eol
- line
), line
);
135 hunk
->colored_start
= eol
- s
->colored
.buf
+ (*eol
== '\n');
136 header
->colored_extra_start
= p
+ 3 - s
->colored
.buf
;
137 header
->colored_extra_end
= hunk
->colored_start
;
142 static int is_octal(const char *p
, size_t len
)
148 if (*p
< '0' || *(p
++) > '7')
153 static int parse_diff(struct add_p_state
*s
, const struct pathspec
*ps
)
155 struct argv_array args
= ARGV_ARRAY_INIT
;
156 struct strbuf
*plain
= &s
->plain
, *colored
= NULL
;
157 struct child_process cp
= CHILD_PROCESS_INIT
;
158 char *p
, *pend
, *colored_p
= NULL
, *colored_pend
= NULL
;
159 size_t file_diff_alloc
= 0, i
, color_arg_index
;
160 struct file_diff
*file_diff
= NULL
;
161 struct hunk
*hunk
= NULL
;
164 /* Use `--no-color` explicitly, just in case `diff.color = always`. */
165 argv_array_pushl(&args
, "diff-files", "-p", "--no-color", "--", NULL
);
166 color_arg_index
= args
.argc
- 2;
167 for (i
= 0; i
< ps
->nr
; i
++)
168 argv_array_push(&args
, ps
->items
[i
].original
);
170 setup_child_process(s
, &cp
, NULL
);
172 res
= capture_command(&cp
, plain
, 0);
174 argv_array_clear(&args
);
175 return error(_("could not parse diff"));
178 argv_array_clear(&args
);
181 strbuf_complete_line(plain
);
183 if (want_color_fd(1, -1)) {
184 struct child_process colored_cp
= CHILD_PROCESS_INIT
;
186 setup_child_process(s
, &colored_cp
, NULL
);
187 xsnprintf((char *)args
.argv
[color_arg_index
], 8, "--color");
188 colored_cp
.argv
= args
.argv
;
189 colored
= &s
->colored
;
190 res
= capture_command(&colored_cp
, colored
, 0);
191 argv_array_clear(&args
);
193 return error(_("could not parse colored diff"));
194 strbuf_complete_line(colored
);
195 colored_p
= colored
->buf
;
196 colored_pend
= colored_p
+ colored
->len
;
198 argv_array_clear(&args
);
200 /* parse files and hunks */
202 pend
= p
+ plain
->len
;
204 char *eol
= memchr(p
, '\n', pend
- p
);
205 const char *deleted
= NULL
, *mode_change
= NULL
;
210 if (starts_with(p
, "diff ")) {
212 ALLOC_GROW(s
->file_diff
, s
->file_diff_nr
,
214 file_diff
= s
->file_diff
+ s
->file_diff_nr
- 1;
215 memset(file_diff
, 0, sizeof(*file_diff
));
216 hunk
= &file_diff
->head
;
217 hunk
->start
= p
- plain
->buf
;
219 hunk
->colored_start
= colored_p
- colored
->buf
;
220 } else if (p
== plain
->buf
)
221 BUG("diff starts with unexpected line:\n"
222 "%.*s\n", (int)(eol
- p
), p
);
223 else if (file_diff
->deleted
)
224 ; /* keep the rest of the file in a single "hunk" */
225 else if (starts_with(p
, "@@ ") ||
226 (hunk
== &file_diff
->head
&&
227 skip_prefix(p
, "deleted file", &deleted
))) {
228 file_diff
->hunk_nr
++;
229 ALLOC_GROW(file_diff
->hunk
, file_diff
->hunk_nr
,
230 file_diff
->hunk_alloc
);
231 hunk
= file_diff
->hunk
+ file_diff
->hunk_nr
- 1;
232 memset(hunk
, 0, sizeof(*hunk
));
234 hunk
->start
= p
- plain
->buf
;
236 hunk
->colored_start
= colored_p
- colored
->buf
;
239 file_diff
->deleted
= 1;
240 else if (parse_hunk_header(s
, hunk
) < 0)
242 } else if (hunk
== &file_diff
->head
&&
243 skip_prefix(p
, "old mode ", &mode_change
) &&
244 is_octal(mode_change
, eol
- mode_change
)) {
245 if (file_diff
->mode_change
)
246 BUG("double mode change?\n\n%.*s",
247 (int)(eol
- plain
->buf
), plain
->buf
);
248 if (file_diff
->hunk_nr
++)
249 BUG("mode change in the middle?\n\n%.*s",
250 (int)(eol
- plain
->buf
), plain
->buf
);
253 * Do *not* change `hunk`: the mode change pseudo-hunk
254 * is _part of_ the header "hunk".
256 file_diff
->mode_change
= 1;
257 ALLOC_GROW(file_diff
->hunk
, file_diff
->hunk_nr
,
258 file_diff
->hunk_alloc
);
259 memset(file_diff
->hunk
, 0, sizeof(struct hunk
));
260 file_diff
->hunk
->start
= p
- plain
->buf
;
262 file_diff
->hunk
->colored_start
=
263 colored_p
- colored
->buf
;
264 } else if (hunk
== &file_diff
->head
&&
265 skip_prefix(p
, "new mode ", &mode_change
) &&
266 is_octal(mode_change
, eol
- mode_change
)) {
269 * Extend the "mode change" pseudo-hunk to include also
270 * the "new mode" line.
272 if (!file_diff
->mode_change
)
273 BUG("'new mode' without 'old mode'?\n\n%.*s",
274 (int)(eol
- plain
->buf
), plain
->buf
);
275 if (file_diff
->hunk_nr
!= 1)
276 BUG("mode change in the middle?\n\n%.*s",
277 (int)(eol
- plain
->buf
), plain
->buf
);
278 if (p
- plain
->buf
!= file_diff
->hunk
->end
)
279 BUG("'new mode' does not immediately follow "
280 "'old mode'?\n\n%.*s",
281 (int)(eol
- plain
->buf
), plain
->buf
);
284 if (file_diff
->deleted
&& file_diff
->mode_change
)
285 BUG("diff contains delete *and* a mode change?!?\n%.*s",
286 (int)(eol
- (plain
->buf
+ file_diff
->head
.start
)),
287 plain
->buf
+ file_diff
->head
.start
);
289 p
= eol
== pend
? pend
: eol
+ 1;
290 hunk
->end
= p
- plain
->buf
;
293 char *colored_eol
= memchr(colored_p
, '\n',
294 colored_pend
- colored_p
);
296 colored_p
= colored_eol
+ 1;
298 colored_p
= colored_pend
;
300 hunk
->colored_end
= colored_p
- colored
->buf
;
304 if (file_diff
->hunk_nr
!= 1)
305 BUG("mode change in hunk #%d???",
306 (int)file_diff
->hunk_nr
);
307 /* Adjust the end of the "mode change" pseudo-hunk */
308 file_diff
->hunk
->end
= hunk
->end
;
310 file_diff
->hunk
->colored_end
= hunk
->colored_end
;
317 static void render_hunk(struct add_p_state
*s
, struct hunk
*hunk
,
318 ssize_t delta
, int colored
, struct strbuf
*out
)
320 struct hunk_header
*header
= &hunk
->header
;
322 if (hunk
->header
.old_offset
!= 0 || hunk
->header
.new_offset
!= 0) {
324 * Generate the hunk header dynamically, except for special
325 * hunks (such as the diff header).
329 unsigned long old_offset
= header
->old_offset
;
330 unsigned long new_offset
= header
->new_offset
;
333 p
= s
->plain
.buf
+ header
->extra_start
;
334 len
= header
->extra_end
- header
->extra_start
;
336 strbuf_addstr(out
, s
->s
.fraginfo_color
);
337 p
= s
->colored
.buf
+ header
->colored_extra_start
;
338 len
= header
->colored_extra_end
339 - header
->colored_extra_start
;
344 strbuf_addf(out
, "@@ -%lu,%lu +%lu,%lu @@",
345 old_offset
, header
->old_count
,
346 new_offset
, header
->new_count
);
348 strbuf_add(out
, p
, len
);
350 strbuf_addf(out
, "%s\n", GIT_COLOR_RESET
);
352 strbuf_addch(out
, '\n');
356 strbuf_add(out
, s
->colored
.buf
+ hunk
->colored_start
,
357 hunk
->colored_end
- hunk
->colored_start
);
359 strbuf_add(out
, s
->plain
.buf
+ hunk
->start
,
360 hunk
->end
- hunk
->start
);
363 static void render_diff_header(struct add_p_state
*s
,
364 struct file_diff
*file_diff
, int colored
,
368 * If there was a mode change, the first hunk is a pseudo hunk that
369 * corresponds to the mode line in the header. If the user did not want
370 * to stage that "hunk", we actually have to cut it out from the header.
372 int skip_mode_change
=
373 file_diff
->mode_change
&& file_diff
->hunk
->use
!= USE_HUNK
;
374 struct hunk
*head
= &file_diff
->head
, *first
= file_diff
->hunk
;
376 if (!skip_mode_change
) {
377 render_hunk(s
, head
, 0, colored
, out
);
382 const char *p
= s
->colored
.buf
;
384 strbuf_add(out
, p
+ head
->colored_start
,
385 first
->colored_start
- head
->colored_start
);
386 strbuf_add(out
, p
+ first
->colored_end
,
387 head
->colored_end
- first
->colored_end
);
389 const char *p
= s
->plain
.buf
;
391 strbuf_add(out
, p
+ head
->start
, first
->start
- head
->start
);
392 strbuf_add(out
, p
+ first
->end
, head
->end
- first
->end
);
396 static void reassemble_patch(struct add_p_state
*s
,
397 struct file_diff
*file_diff
, struct strbuf
*out
)
403 render_diff_header(s
, file_diff
, 0, out
);
405 for (i
= file_diff
->mode_change
; i
< file_diff
->hunk_nr
; i
++) {
406 hunk
= file_diff
->hunk
+ i
;
407 if (hunk
->use
!= USE_HUNK
)
408 delta
+= hunk
->header
.old_count
409 - hunk
->header
.new_count
;
411 render_hunk(s
, hunk
, delta
, 0, out
);
415 static const char help_patch_text
[] =
416 N_("y - stage this hunk\n"
417 "n - do not stage this hunk\n"
418 "a - stage this and all the remaining hunks\n"
419 "d - do not stage this hunk nor any of the remaining hunks\n"
420 "j - leave this hunk undecided, see next undecided hunk\n"
421 "J - leave this hunk undecided, see next hunk\n"
422 "k - leave this hunk undecided, see previous undecided hunk\n"
423 "K - leave this hunk undecided, see previous hunk\n"
426 static int patch_update_file(struct add_p_state
*s
,
427 struct file_diff
*file_diff
)
429 size_t hunk_index
= 0;
430 ssize_t i
, undecided_previous
, undecided_next
;
433 struct child_process cp
= CHILD_PROCESS_INIT
;
434 int colored
= !!s
->colored
.len
;
435 enum prompt_mode_type prompt_mode_type
;
437 if (!file_diff
->hunk_nr
)
440 strbuf_reset(&s
->buf
);
441 render_diff_header(s
, file_diff
, colored
, &s
->buf
);
442 fputs(s
->buf
.buf
, stdout
);
444 if (hunk_index
>= file_diff
->hunk_nr
)
446 hunk
= file_diff
->hunk
+ hunk_index
;
448 undecided_previous
= -1;
449 for (i
= hunk_index
- 1; i
>= 0; i
--)
450 if (file_diff
->hunk
[i
].use
== UNDECIDED_HUNK
) {
451 undecided_previous
= i
;
456 for (i
= hunk_index
+ 1; i
< file_diff
->hunk_nr
; i
++)
457 if (file_diff
->hunk
[i
].use
== UNDECIDED_HUNK
) {
462 /* Everything decided? */
463 if (undecided_previous
< 0 && undecided_next
< 0 &&
464 hunk
->use
!= UNDECIDED_HUNK
)
467 strbuf_reset(&s
->buf
);
468 render_hunk(s
, hunk
, 0, colored
, &s
->buf
);
469 fputs(s
->buf
.buf
, stdout
);
471 strbuf_reset(&s
->buf
);
472 if (undecided_previous
>= 0)
473 strbuf_addstr(&s
->buf
, ",k");
475 strbuf_addstr(&s
->buf
, ",K");
476 if (undecided_next
>= 0)
477 strbuf_addstr(&s
->buf
, ",j");
478 if (hunk_index
+ 1 < file_diff
->hunk_nr
)
479 strbuf_addstr(&s
->buf
, ",J");
481 if (file_diff
->deleted
)
482 prompt_mode_type
= PROMPT_DELETION
;
483 else if (file_diff
->mode_change
&& !hunk_index
)
484 prompt_mode_type
= PROMPT_MODE_CHANGE
;
486 prompt_mode_type
= PROMPT_HUNK
;
488 color_fprintf(stdout
, s
->s
.prompt_color
,
489 "(%"PRIuMAX
"/%"PRIuMAX
") ",
490 (uintmax_t)hunk_index
+ 1,
491 (uintmax_t)file_diff
->hunk_nr
);
492 color_fprintf(stdout
, s
->s
.prompt_color
,
493 _(prompt_mode
[prompt_mode_type
]), s
->buf
.buf
);
495 if (strbuf_getline(&s
->answer
, stdin
) == EOF
)
497 strbuf_trim_trailing_newline(&s
->answer
);
501 ch
= tolower(s
->answer
.buf
[0]);
503 hunk
->use
= USE_HUNK
;
505 hunk_index
= undecided_next
< 0 ?
506 file_diff
->hunk_nr
: undecided_next
;
507 } else if (ch
== 'n') {
508 hunk
->use
= SKIP_HUNK
;
510 } else if (ch
== 'a') {
511 for (; hunk_index
< file_diff
->hunk_nr
; hunk_index
++) {
512 hunk
= file_diff
->hunk
+ hunk_index
;
513 if (hunk
->use
== UNDECIDED_HUNK
)
514 hunk
->use
= USE_HUNK
;
516 } else if (ch
== 'd') {
517 for (; hunk_index
< file_diff
->hunk_nr
; hunk_index
++) {
518 hunk
= file_diff
->hunk
+ hunk_index
;
519 if (hunk
->use
== UNDECIDED_HUNK
)
520 hunk
->use
= SKIP_HUNK
;
522 } else if (s
->answer
.buf
[0] == 'K') {
526 err(s
, _("No previous hunk"));
527 } else if (s
->answer
.buf
[0] == 'J') {
528 if (hunk_index
+ 1 < file_diff
->hunk_nr
)
531 err(s
, _("No next hunk"));
532 } else if (s
->answer
.buf
[0] == 'k') {
533 if (undecided_previous
>= 0)
534 hunk_index
= undecided_previous
;
536 err(s
, _("No previous hunk"));
537 } else if (s
->answer
.buf
[0] == 'j') {
538 if (undecided_next
>= 0)
539 hunk_index
= undecided_next
;
541 err(s
, _("No next hunk"));
543 color_fprintf(stdout
, s
->s
.help_color
,
547 /* Any hunk to be used? */
548 for (i
= 0; i
< file_diff
->hunk_nr
; i
++)
549 if (file_diff
->hunk
[i
].use
== USE_HUNK
)
552 if (i
< file_diff
->hunk_nr
) {
553 /* At least one hunk selected: apply */
554 strbuf_reset(&s
->buf
);
555 reassemble_patch(s
, file_diff
, &s
->buf
);
557 discard_index(s
->s
.r
->index
);
558 setup_child_process(s
, &cp
, "apply", "--cached", NULL
);
559 if (pipe_command(&cp
, s
->buf
.buf
, s
->buf
.len
,
561 error(_("'git apply --cached' failed"));
562 if (!repo_read_index(s
->s
.r
))
563 repo_refresh_and_write_index(s
->s
.r
, REFRESH_QUIET
, 0,
564 1, NULL
, NULL
, NULL
);
571 int run_add_p(struct repository
*r
, const struct pathspec
*ps
)
573 struct add_p_state s
= {
574 { r
}, STRBUF_INIT
, STRBUF_INIT
, STRBUF_INIT
, STRBUF_INIT
578 init_add_i_state(&s
.s
, r
);
580 if (discard_index(r
->index
) < 0 || repo_read_index(r
) < 0 ||
581 repo_refresh_and_write_index(r
, REFRESH_QUIET
, 0, 1,
582 NULL
, NULL
, NULL
) < 0 ||
583 parse_diff(&s
, ps
) < 0) {
584 strbuf_release(&s
.plain
);
585 strbuf_release(&s
.colored
);
589 for (i
= 0; i
< s
.file_diff_nr
; i
++)
590 if (patch_update_file(&s
, s
.file_diff
+ i
))
593 strbuf_release(&s
.answer
);
594 strbuf_release(&s
.buf
);
595 strbuf_release(&s
.plain
);
596 strbuf_release(&s
.colored
);