]>
git.ipfire.org Git - thirdparty/git.git/blob - add-patch.c
2007f55e0431cfc43c69827105dd87ebf7d915d4
2 #include "add-interactive.h"
4 #include "run-command.h"
5 #include "argv-array.h"
11 unsigned long old_offset
, old_count
, new_offset
, new_count
;
13 * Start/end offsets to the extra text after the second `@@` in the
14 * hunk header, e.g. the function signature. This is expected to
15 * include the newline.
17 size_t extra_start
, extra_end
, colored_extra_start
, colored_extra_end
;
21 size_t start
, end
, colored_start
, colored_end
;
22 enum { UNDECIDED_HUNK
= 0, SKIP_HUNK
, USE_HUNK
} use
;
23 struct hunk_header header
;
28 struct strbuf answer
, buf
;
31 struct strbuf plain
, colored
;
35 size_t hunk_nr
, hunk_alloc
;
36 unsigned deleted
:1, mode_change
:1;
41 static void err(struct add_p_state
*s
, const char *fmt
, ...)
46 fputs(s
->s
.error_color
, stderr
);
47 vfprintf(stderr
, fmt
, args
);
48 fputs(s
->s
.reset_color
, stderr
);
53 static void setup_child_process(struct add_p_state
*s
,
54 struct child_process
*cp
, ...)
60 while ((arg
= va_arg(ap
, const char *)))
61 argv_array_push(&cp
->args
, arg
);
65 argv_array_pushf(&cp
->env_array
,
66 INDEX_ENVIRONMENT
"=%s", s
->s
.r
->index_file
);
69 static int parse_range(const char **p
,
70 unsigned long *offset
, unsigned long *count
)
74 *offset
= strtoul(*p
, &pend
, 10);
82 *count
= strtoul(pend
+ 1, (char **)p
, 10);
83 return *p
== pend
+ 1 ? -1 : 0;
86 static int parse_hunk_header(struct add_p_state
*s
, struct hunk
*hunk
)
88 struct hunk_header
*header
= &hunk
->header
;
89 const char *line
= s
->plain
.buf
+ hunk
->start
, *p
= line
;
90 char *eol
= memchr(p
, '\n', s
->plain
.len
- hunk
->start
);
93 eol
= s
->plain
.buf
+ s
->plain
.len
;
95 if (!skip_prefix(p
, "@@ -", &p
) ||
96 parse_range(&p
, &header
->old_offset
, &header
->old_count
) < 0 ||
97 !skip_prefix(p
, " +", &p
) ||
98 parse_range(&p
, &header
->new_offset
, &header
->new_count
) < 0 ||
99 !skip_prefix(p
, " @@", &p
))
100 return error(_("could not parse hunk header '%.*s'"),
101 (int)(eol
- line
), line
);
103 hunk
->start
= eol
- s
->plain
.buf
+ (*eol
== '\n');
104 header
->extra_start
= p
- s
->plain
.buf
;
105 header
->extra_end
= hunk
->start
;
107 if (!s
->colored
.len
) {
108 header
->colored_extra_start
= header
->colored_extra_end
= 0;
112 /* Now find the extra text in the colored diff */
113 line
= s
->colored
.buf
+ hunk
->colored_start
;
114 eol
= memchr(line
, '\n', s
->colored
.len
- hunk
->colored_start
);
116 eol
= s
->colored
.buf
+ s
->colored
.len
;
117 p
= memmem(line
, eol
- line
, "@@ -", 4);
119 return error(_("could not parse colored hunk header '%.*s'"),
120 (int)(eol
- line
), line
);
121 p
= memmem(p
+ 4, eol
- p
- 4, " @@", 3);
123 return error(_("could not parse colored hunk header '%.*s'"),
124 (int)(eol
- line
), line
);
125 hunk
->colored_start
= eol
- s
->colored
.buf
+ (*eol
== '\n');
126 header
->colored_extra_start
= p
+ 3 - s
->colored
.buf
;
127 header
->colored_extra_end
= hunk
->colored_start
;
132 static int is_octal(const char *p
, size_t len
)
138 if (*p
< '0' || *(p
++) > '7')
143 static int parse_diff(struct add_p_state
*s
, const struct pathspec
*ps
)
145 struct argv_array args
= ARGV_ARRAY_INIT
;
146 struct strbuf
*plain
= &s
->plain
, *colored
= NULL
;
147 struct child_process cp
= CHILD_PROCESS_INIT
;
148 char *p
, *pend
, *colored_p
= NULL
, *colored_pend
= NULL
;
149 size_t file_diff_alloc
= 0, i
, color_arg_index
;
150 struct file_diff
*file_diff
= NULL
;
151 struct hunk
*hunk
= NULL
;
154 /* Use `--no-color` explicitly, just in case `diff.color = always`. */
155 argv_array_pushl(&args
, "diff-files", "-p", "--no-color", "--", NULL
);
156 color_arg_index
= args
.argc
- 2;
157 for (i
= 0; i
< ps
->nr
; i
++)
158 argv_array_push(&args
, ps
->items
[i
].original
);
160 setup_child_process(s
, &cp
, NULL
);
162 res
= capture_command(&cp
, plain
, 0);
164 argv_array_clear(&args
);
165 return error(_("could not parse diff"));
168 argv_array_clear(&args
);
171 strbuf_complete_line(plain
);
173 if (want_color_fd(1, -1)) {
174 struct child_process colored_cp
= CHILD_PROCESS_INIT
;
176 setup_child_process(s
, &colored_cp
, NULL
);
177 xsnprintf((char *)args
.argv
[color_arg_index
], 8, "--color");
178 colored_cp
.argv
= args
.argv
;
179 colored
= &s
->colored
;
180 res
= capture_command(&colored_cp
, colored
, 0);
181 argv_array_clear(&args
);
183 return error(_("could not parse colored diff"));
184 strbuf_complete_line(colored
);
185 colored_p
= colored
->buf
;
186 colored_pend
= colored_p
+ colored
->len
;
188 argv_array_clear(&args
);
190 /* parse files and hunks */
192 pend
= p
+ plain
->len
;
194 char *eol
= memchr(p
, '\n', pend
- p
);
195 const char *deleted
= NULL
, *mode_change
= NULL
;
200 if (starts_with(p
, "diff ")) {
202 ALLOC_GROW(s
->file_diff
, s
->file_diff_nr
,
204 file_diff
= s
->file_diff
+ s
->file_diff_nr
- 1;
205 memset(file_diff
, 0, sizeof(*file_diff
));
206 hunk
= &file_diff
->head
;
207 hunk
->start
= p
- plain
->buf
;
209 hunk
->colored_start
= colored_p
- colored
->buf
;
210 } else if (p
== plain
->buf
)
211 BUG("diff starts with unexpected line:\n"
212 "%.*s\n", (int)(eol
- p
), p
);
213 else if (file_diff
->deleted
)
214 ; /* keep the rest of the file in a single "hunk" */
215 else if (starts_with(p
, "@@ ") ||
216 (hunk
== &file_diff
->head
&&
217 skip_prefix(p
, "deleted file", &deleted
))) {
218 file_diff
->hunk_nr
++;
219 ALLOC_GROW(file_diff
->hunk
, file_diff
->hunk_nr
,
220 file_diff
->hunk_alloc
);
221 hunk
= file_diff
->hunk
+ file_diff
->hunk_nr
- 1;
222 memset(hunk
, 0, sizeof(*hunk
));
224 hunk
->start
= p
- plain
->buf
;
226 hunk
->colored_start
= colored_p
- colored
->buf
;
229 file_diff
->deleted
= 1;
230 else if (parse_hunk_header(s
, hunk
) < 0)
232 } else if (hunk
== &file_diff
->head
&&
233 skip_prefix(p
, "old mode ", &mode_change
) &&
234 is_octal(mode_change
, eol
- mode_change
)) {
235 if (file_diff
->mode_change
)
236 BUG("double mode change?\n\n%.*s",
237 (int)(eol
- plain
->buf
), plain
->buf
);
238 if (file_diff
->hunk_nr
++)
239 BUG("mode change in the middle?\n\n%.*s",
240 (int)(eol
- plain
->buf
), plain
->buf
);
243 * Do *not* change `hunk`: the mode change pseudo-hunk
244 * is _part of_ the header "hunk".
246 file_diff
->mode_change
= 1;
247 ALLOC_GROW(file_diff
->hunk
, file_diff
->hunk_nr
,
248 file_diff
->hunk_alloc
);
249 memset(file_diff
->hunk
, 0, sizeof(struct hunk
));
250 file_diff
->hunk
->start
= p
- plain
->buf
;
252 file_diff
->hunk
->colored_start
=
253 colored_p
- colored
->buf
;
254 } else if (hunk
== &file_diff
->head
&&
255 skip_prefix(p
, "new mode ", &mode_change
) &&
256 is_octal(mode_change
, eol
- mode_change
)) {
259 * Extend the "mode change" pseudo-hunk to include also
260 * the "new mode" line.
262 if (!file_diff
->mode_change
)
263 BUG("'new mode' without 'old mode'?\n\n%.*s",
264 (int)(eol
- plain
->buf
), plain
->buf
);
265 if (file_diff
->hunk_nr
!= 1)
266 BUG("mode change in the middle?\n\n%.*s",
267 (int)(eol
- plain
->buf
), plain
->buf
);
268 if (p
- plain
->buf
!= file_diff
->hunk
->end
)
269 BUG("'new mode' does not immediately follow "
270 "'old mode'?\n\n%.*s",
271 (int)(eol
- plain
->buf
), plain
->buf
);
274 if (file_diff
->deleted
&& file_diff
->mode_change
)
275 BUG("diff contains delete *and* a mode change?!?\n%.*s",
276 (int)(eol
- (plain
->buf
+ file_diff
->head
.start
)),
277 plain
->buf
+ file_diff
->head
.start
);
279 p
= eol
== pend
? pend
: eol
+ 1;
280 hunk
->end
= p
- plain
->buf
;
283 char *colored_eol
= memchr(colored_p
, '\n',
284 colored_pend
- colored_p
);
286 colored_p
= colored_eol
+ 1;
288 colored_p
= colored_pend
;
290 hunk
->colored_end
= colored_p
- colored
->buf
;
294 if (file_diff
->hunk_nr
!= 1)
295 BUG("mode change in hunk #%d???",
296 (int)file_diff
->hunk_nr
);
297 /* Adjust the end of the "mode change" pseudo-hunk */
298 file_diff
->hunk
->end
= hunk
->end
;
300 file_diff
->hunk
->colored_end
= hunk
->colored_end
;
307 static void render_hunk(struct add_p_state
*s
, struct hunk
*hunk
,
308 ssize_t delta
, int colored
, struct strbuf
*out
)
310 struct hunk_header
*header
= &hunk
->header
;
312 if (hunk
->header
.old_offset
!= 0 || hunk
->header
.new_offset
!= 0) {
314 * Generate the hunk header dynamically, except for special
315 * hunks (such as the diff header).
319 unsigned long old_offset
= header
->old_offset
;
320 unsigned long new_offset
= header
->new_offset
;
323 p
= s
->plain
.buf
+ header
->extra_start
;
324 len
= header
->extra_end
- header
->extra_start
;
326 strbuf_addstr(out
, s
->s
.fraginfo_color
);
327 p
= s
->colored
.buf
+ header
->colored_extra_start
;
328 len
= header
->colored_extra_end
329 - header
->colored_extra_start
;
334 strbuf_addf(out
, "@@ -%lu,%lu +%lu,%lu @@",
335 old_offset
, header
->old_count
,
336 new_offset
, header
->new_count
);
338 strbuf_add(out
, p
, len
);
340 strbuf_addf(out
, "%s\n", GIT_COLOR_RESET
);
342 strbuf_addch(out
, '\n');
346 strbuf_add(out
, s
->colored
.buf
+ hunk
->colored_start
,
347 hunk
->colored_end
- hunk
->colored_start
);
349 strbuf_add(out
, s
->plain
.buf
+ hunk
->start
,
350 hunk
->end
- hunk
->start
);
353 static void render_diff_header(struct add_p_state
*s
,
354 struct file_diff
*file_diff
, int colored
,
358 * If there was a mode change, the first hunk is a pseudo hunk that
359 * corresponds to the mode line in the header. If the user did not want
360 * to stage that "hunk", we actually have to cut it out from the header.
362 int skip_mode_change
=
363 file_diff
->mode_change
&& file_diff
->hunk
->use
!= USE_HUNK
;
364 struct hunk
*head
= &file_diff
->head
, *first
= file_diff
->hunk
;
366 if (!skip_mode_change
) {
367 render_hunk(s
, head
, 0, colored
, out
);
372 const char *p
= s
->colored
.buf
;
374 strbuf_add(out
, p
+ head
->colored_start
,
375 first
->colored_start
- head
->colored_start
);
376 strbuf_add(out
, p
+ first
->colored_end
,
377 head
->colored_end
- first
->colored_end
);
379 const char *p
= s
->plain
.buf
;
381 strbuf_add(out
, p
+ head
->start
, first
->start
- head
->start
);
382 strbuf_add(out
, p
+ first
->end
, head
->end
- first
->end
);
386 static void reassemble_patch(struct add_p_state
*s
,
387 struct file_diff
*file_diff
, struct strbuf
*out
)
393 render_diff_header(s
, file_diff
, 0, out
);
395 for (i
= file_diff
->mode_change
; i
< file_diff
->hunk_nr
; i
++) {
396 hunk
= file_diff
->hunk
+ i
;
397 if (hunk
->use
!= USE_HUNK
)
398 delta
+= hunk
->header
.old_count
399 - hunk
->header
.new_count
;
401 render_hunk(s
, hunk
, delta
, 0, out
);
405 static const char help_patch_text
[] =
406 N_("y - stage this hunk\n"
407 "n - do not stage this hunk\n"
408 "a - stage this and all the remaining hunks\n"
409 "d - do not stage this hunk nor any of the remaining hunks\n"
410 "j - leave this hunk undecided, see next undecided hunk\n"
411 "J - leave this hunk undecided, see next hunk\n"
412 "k - leave this hunk undecided, see previous undecided hunk\n"
413 "K - leave this hunk undecided, see previous hunk\n"
416 static int patch_update_file(struct add_p_state
*s
,
417 struct file_diff
*file_diff
)
419 size_t hunk_index
= 0;
420 ssize_t i
, undecided_previous
, undecided_next
;
423 struct child_process cp
= CHILD_PROCESS_INIT
;
424 int colored
= !!s
->colored
.len
;
426 if (!file_diff
->hunk_nr
)
429 strbuf_reset(&s
->buf
);
430 render_diff_header(s
, file_diff
, colored
, &s
->buf
);
431 fputs(s
->buf
.buf
, stdout
);
433 if (hunk_index
>= file_diff
->hunk_nr
)
435 hunk
= file_diff
->hunk
+ hunk_index
;
437 undecided_previous
= -1;
438 for (i
= hunk_index
- 1; i
>= 0; i
--)
439 if (file_diff
->hunk
[i
].use
== UNDECIDED_HUNK
) {
440 undecided_previous
= i
;
445 for (i
= hunk_index
+ 1; i
< file_diff
->hunk_nr
; i
++)
446 if (file_diff
->hunk
[i
].use
== UNDECIDED_HUNK
) {
451 /* Everything decided? */
452 if (undecided_previous
< 0 && undecided_next
< 0 &&
453 hunk
->use
!= UNDECIDED_HUNK
)
456 strbuf_reset(&s
->buf
);
457 render_hunk(s
, hunk
, 0, colored
, &s
->buf
);
458 fputs(s
->buf
.buf
, stdout
);
460 strbuf_reset(&s
->buf
);
461 if (undecided_previous
>= 0)
462 strbuf_addstr(&s
->buf
, ",k");
464 strbuf_addstr(&s
->buf
, ",K");
465 if (undecided_next
>= 0)
466 strbuf_addstr(&s
->buf
, ",j");
467 if (hunk_index
+ 1 < file_diff
->hunk_nr
)
468 strbuf_addstr(&s
->buf
, ",J");
469 color_fprintf(stdout
, s
->s
.prompt_color
,
470 "(%"PRIuMAX
"/%"PRIuMAX
") ",
471 (uintmax_t)hunk_index
+ 1,
472 (uintmax_t)file_diff
->hunk_nr
);
473 color_fprintf(stdout
, s
->s
.prompt_color
,
474 _("Stage this hunk [y,n,a,d%s,?]? "),
477 if (strbuf_getline(&s
->answer
, stdin
) == EOF
)
479 strbuf_trim_trailing_newline(&s
->answer
);
483 ch
= tolower(s
->answer
.buf
[0]);
485 hunk
->use
= USE_HUNK
;
487 hunk_index
= undecided_next
< 0 ?
488 file_diff
->hunk_nr
: undecided_next
;
489 } else if (ch
== 'n') {
490 hunk
->use
= SKIP_HUNK
;
492 } else if (ch
== 'a') {
493 for (; hunk_index
< file_diff
->hunk_nr
; hunk_index
++) {
494 hunk
= file_diff
->hunk
+ hunk_index
;
495 if (hunk
->use
== UNDECIDED_HUNK
)
496 hunk
->use
= USE_HUNK
;
498 } else if (ch
== 'd') {
499 for (; hunk_index
< file_diff
->hunk_nr
; hunk_index
++) {
500 hunk
= file_diff
->hunk
+ hunk_index
;
501 if (hunk
->use
== UNDECIDED_HUNK
)
502 hunk
->use
= SKIP_HUNK
;
504 } else if (s
->answer
.buf
[0] == 'K') {
508 err(s
, _("No previous hunk"));
509 } else if (s
->answer
.buf
[0] == 'J') {
510 if (hunk_index
+ 1 < file_diff
->hunk_nr
)
513 err(s
, _("No next hunk"));
514 } else if (s
->answer
.buf
[0] == 'k') {
515 if (undecided_previous
>= 0)
516 hunk_index
= undecided_previous
;
518 err(s
, _("No previous hunk"));
519 } else if (s
->answer
.buf
[0] == 'j') {
520 if (undecided_next
>= 0)
521 hunk_index
= undecided_next
;
523 err(s
, _("No next hunk"));
525 color_fprintf(stdout
, s
->s
.help_color
,
529 /* Any hunk to be used? */
530 for (i
= 0; i
< file_diff
->hunk_nr
; i
++)
531 if (file_diff
->hunk
[i
].use
== USE_HUNK
)
534 if (i
< file_diff
->hunk_nr
) {
535 /* At least one hunk selected: apply */
536 strbuf_reset(&s
->buf
);
537 reassemble_patch(s
, file_diff
, &s
->buf
);
539 discard_index(s
->s
.r
->index
);
540 setup_child_process(s
, &cp
, "apply", "--cached", NULL
);
541 if (pipe_command(&cp
, s
->buf
.buf
, s
->buf
.len
,
543 error(_("'git apply --cached' failed"));
544 if (!repo_read_index(s
->s
.r
))
545 repo_refresh_and_write_index(s
->s
.r
, REFRESH_QUIET
, 0,
546 1, NULL
, NULL
, NULL
);
553 int run_add_p(struct repository
*r
, const struct pathspec
*ps
)
555 struct add_p_state s
= {
556 { r
}, STRBUF_INIT
, STRBUF_INIT
, STRBUF_INIT
, STRBUF_INIT
560 init_add_i_state(&s
.s
, r
);
562 if (discard_index(r
->index
) < 0 || repo_read_index(r
) < 0 ||
563 repo_refresh_and_write_index(r
, REFRESH_QUIET
, 0, 1,
564 NULL
, NULL
, NULL
) < 0 ||
565 parse_diff(&s
, ps
) < 0) {
566 strbuf_release(&s
.plain
);
567 strbuf_release(&s
.colored
);
571 for (i
= 0; i
< s
.file_diff_nr
; i
++)
572 if (patch_update_file(&s
, s
.file_diff
+ i
))
575 strbuf_release(&s
.answer
);
576 strbuf_release(&s
.buf
);
577 strbuf_release(&s
.plain
);
578 strbuf_release(&s
.colored
);