]>
git.ipfire.org Git - thirdparty/git.git/blob - add-patch.c
c32541f46d7ad19860709571d50ca01a63a65e3c
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
;
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 parse_diff(struct add_p_state
*s
, const struct pathspec
*ps
)
134 struct argv_array args
= ARGV_ARRAY_INIT
;
135 struct strbuf
*plain
= &s
->plain
, *colored
= NULL
;
136 struct child_process cp
= CHILD_PROCESS_INIT
;
137 char *p
, *pend
, *colored_p
= NULL
, *colored_pend
= NULL
;
138 size_t file_diff_alloc
= 0, i
, color_arg_index
;
139 struct file_diff
*file_diff
= NULL
;
140 struct hunk
*hunk
= NULL
;
143 /* Use `--no-color` explicitly, just in case `diff.color = always`. */
144 argv_array_pushl(&args
, "diff-files", "-p", "--no-color", "--", NULL
);
145 color_arg_index
= args
.argc
- 2;
146 for (i
= 0; i
< ps
->nr
; i
++)
147 argv_array_push(&args
, ps
->items
[i
].original
);
149 setup_child_process(s
, &cp
, NULL
);
151 res
= capture_command(&cp
, plain
, 0);
153 argv_array_clear(&args
);
154 return error(_("could not parse diff"));
157 argv_array_clear(&args
);
160 strbuf_complete_line(plain
);
162 if (want_color_fd(1, -1)) {
163 struct child_process colored_cp
= CHILD_PROCESS_INIT
;
165 setup_child_process(s
, &colored_cp
, NULL
);
166 xsnprintf((char *)args
.argv
[color_arg_index
], 8, "--color");
167 colored_cp
.argv
= args
.argv
;
168 colored
= &s
->colored
;
169 res
= capture_command(&colored_cp
, colored
, 0);
170 argv_array_clear(&args
);
172 return error(_("could not parse colored diff"));
173 strbuf_complete_line(colored
);
174 colored_p
= colored
->buf
;
175 colored_pend
= colored_p
+ colored
->len
;
177 argv_array_clear(&args
);
179 /* parse files and hunks */
181 pend
= p
+ plain
->len
;
183 char *eol
= memchr(p
, '\n', pend
- p
);
184 const char *deleted
= NULL
;
189 if (starts_with(p
, "diff ")) {
191 ALLOC_GROW(s
->file_diff
, s
->file_diff_nr
,
193 file_diff
= s
->file_diff
+ s
->file_diff_nr
- 1;
194 memset(file_diff
, 0, sizeof(*file_diff
));
195 hunk
= &file_diff
->head
;
196 hunk
->start
= p
- plain
->buf
;
198 hunk
->colored_start
= colored_p
- colored
->buf
;
199 } else if (p
== plain
->buf
)
200 BUG("diff starts with unexpected line:\n"
201 "%.*s\n", (int)(eol
- p
), p
);
202 else if (file_diff
->deleted
)
203 ; /* keep the rest of the file in a single "hunk" */
204 else if (starts_with(p
, "@@ ") ||
205 (hunk
== &file_diff
->head
&&
206 skip_prefix(p
, "deleted file", &deleted
))) {
207 file_diff
->hunk_nr
++;
208 ALLOC_GROW(file_diff
->hunk
, file_diff
->hunk_nr
,
209 file_diff
->hunk_alloc
);
210 hunk
= file_diff
->hunk
+ file_diff
->hunk_nr
- 1;
211 memset(hunk
, 0, sizeof(*hunk
));
213 hunk
->start
= p
- plain
->buf
;
215 hunk
->colored_start
= colored_p
- colored
->buf
;
218 file_diff
->deleted
= 1;
219 else if (parse_hunk_header(s
, hunk
) < 0)
223 p
= eol
== pend
? pend
: eol
+ 1;
224 hunk
->end
= p
- plain
->buf
;
227 char *colored_eol
= memchr(colored_p
, '\n',
228 colored_pend
- colored_p
);
230 colored_p
= colored_eol
+ 1;
232 colored_p
= colored_pend
;
234 hunk
->colored_end
= colored_p
- colored
->buf
;
241 static void render_hunk(struct add_p_state
*s
, struct hunk
*hunk
,
242 ssize_t delta
, int colored
, struct strbuf
*out
)
244 struct hunk_header
*header
= &hunk
->header
;
246 if (hunk
->header
.old_offset
!= 0 || hunk
->header
.new_offset
!= 0) {
248 * Generate the hunk header dynamically, except for special
249 * hunks (such as the diff header).
253 unsigned long old_offset
= header
->old_offset
;
254 unsigned long new_offset
= header
->new_offset
;
257 p
= s
->plain
.buf
+ header
->extra_start
;
258 len
= header
->extra_end
- header
->extra_start
;
260 strbuf_addstr(out
, s
->s
.fraginfo_color
);
261 p
= s
->colored
.buf
+ header
->colored_extra_start
;
262 len
= header
->colored_extra_end
263 - header
->colored_extra_start
;
268 strbuf_addf(out
, "@@ -%lu,%lu +%lu,%lu @@",
269 old_offset
, header
->old_count
,
270 new_offset
, header
->new_count
);
272 strbuf_add(out
, p
, len
);
274 strbuf_addf(out
, "%s\n", GIT_COLOR_RESET
);
276 strbuf_addch(out
, '\n');
280 strbuf_add(out
, s
->colored
.buf
+ hunk
->colored_start
,
281 hunk
->colored_end
- hunk
->colored_start
);
283 strbuf_add(out
, s
->plain
.buf
+ hunk
->start
,
284 hunk
->end
- hunk
->start
);
287 static void reassemble_patch(struct add_p_state
*s
,
288 struct file_diff
*file_diff
, struct strbuf
*out
)
294 render_hunk(s
, &file_diff
->head
, 0, 0, out
);
296 for (i
= 0; i
< file_diff
->hunk_nr
; i
++) {
297 hunk
= file_diff
->hunk
+ i
;
298 if (hunk
->use
!= USE_HUNK
)
299 delta
+= hunk
->header
.old_count
300 - hunk
->header
.new_count
;
302 render_hunk(s
, hunk
, delta
, 0, out
);
306 static const char help_patch_text
[] =
307 N_("y - stage this hunk\n"
308 "n - do not stage this hunk\n"
309 "a - stage this and all the remaining hunks\n"
310 "d - do not stage this hunk nor any of the remaining hunks\n"
311 "j - leave this hunk undecided, see next undecided hunk\n"
312 "J - leave this hunk undecided, see next hunk\n"
313 "k - leave this hunk undecided, see previous undecided hunk\n"
314 "K - leave this hunk undecided, see previous hunk\n"
317 static int patch_update_file(struct add_p_state
*s
,
318 struct file_diff
*file_diff
)
320 size_t hunk_index
= 0;
321 ssize_t i
, undecided_previous
, undecided_next
;
324 struct child_process cp
= CHILD_PROCESS_INIT
;
325 int colored
= !!s
->colored
.len
;
327 if (!file_diff
->hunk_nr
)
330 strbuf_reset(&s
->buf
);
331 render_hunk(s
, &file_diff
->head
, 0, colored
, &s
->buf
);
332 fputs(s
->buf
.buf
, stdout
);
334 if (hunk_index
>= file_diff
->hunk_nr
)
336 hunk
= file_diff
->hunk
+ hunk_index
;
338 undecided_previous
= -1;
339 for (i
= hunk_index
- 1; i
>= 0; i
--)
340 if (file_diff
->hunk
[i
].use
== UNDECIDED_HUNK
) {
341 undecided_previous
= i
;
346 for (i
= hunk_index
+ 1; i
< file_diff
->hunk_nr
; i
++)
347 if (file_diff
->hunk
[i
].use
== UNDECIDED_HUNK
) {
352 /* Everything decided? */
353 if (undecided_previous
< 0 && undecided_next
< 0 &&
354 hunk
->use
!= UNDECIDED_HUNK
)
357 strbuf_reset(&s
->buf
);
358 render_hunk(s
, hunk
, 0, colored
, &s
->buf
);
359 fputs(s
->buf
.buf
, stdout
);
361 strbuf_reset(&s
->buf
);
362 if (undecided_previous
>= 0)
363 strbuf_addstr(&s
->buf
, ",k");
365 strbuf_addstr(&s
->buf
, ",K");
366 if (undecided_next
>= 0)
367 strbuf_addstr(&s
->buf
, ",j");
368 if (hunk_index
+ 1 < file_diff
->hunk_nr
)
369 strbuf_addstr(&s
->buf
, ",J");
370 color_fprintf(stdout
, s
->s
.prompt_color
,
371 "(%"PRIuMAX
"/%"PRIuMAX
") ",
372 (uintmax_t)hunk_index
+ 1,
373 (uintmax_t)file_diff
->hunk_nr
);
374 color_fprintf(stdout
, s
->s
.prompt_color
,
375 _("Stage this hunk [y,n,a,d%s,?]? "),
378 if (strbuf_getline(&s
->answer
, stdin
) == EOF
)
380 strbuf_trim_trailing_newline(&s
->answer
);
384 ch
= tolower(s
->answer
.buf
[0]);
386 hunk
->use
= USE_HUNK
;
388 hunk_index
= undecided_next
< 0 ?
389 file_diff
->hunk_nr
: undecided_next
;
390 } else if (ch
== 'n') {
391 hunk
->use
= SKIP_HUNK
;
393 } else if (ch
== 'a') {
394 for (; hunk_index
< file_diff
->hunk_nr
; hunk_index
++) {
395 hunk
= file_diff
->hunk
+ hunk_index
;
396 if (hunk
->use
== UNDECIDED_HUNK
)
397 hunk
->use
= USE_HUNK
;
399 } else if (ch
== 'd') {
400 for (; hunk_index
< file_diff
->hunk_nr
; hunk_index
++) {
401 hunk
= file_diff
->hunk
+ hunk_index
;
402 if (hunk
->use
== UNDECIDED_HUNK
)
403 hunk
->use
= SKIP_HUNK
;
405 } else if (s
->answer
.buf
[0] == 'K') {
409 err(s
, _("No previous hunk"));
410 } else if (s
->answer
.buf
[0] == 'J') {
411 if (hunk_index
+ 1 < file_diff
->hunk_nr
)
414 err(s
, _("No next hunk"));
415 } else if (s
->answer
.buf
[0] == 'k') {
416 if (undecided_previous
>= 0)
417 hunk_index
= undecided_previous
;
419 err(s
, _("No previous hunk"));
420 } else if (s
->answer
.buf
[0] == 'j') {
421 if (undecided_next
>= 0)
422 hunk_index
= undecided_next
;
424 err(s
, _("No next hunk"));
426 color_fprintf(stdout
, s
->s
.help_color
,
430 /* Any hunk to be used? */
431 for (i
= 0; i
< file_diff
->hunk_nr
; i
++)
432 if (file_diff
->hunk
[i
].use
== USE_HUNK
)
435 if (i
< file_diff
->hunk_nr
) {
436 /* At least one hunk selected: apply */
437 strbuf_reset(&s
->buf
);
438 reassemble_patch(s
, file_diff
, &s
->buf
);
440 discard_index(s
->s
.r
->index
);
441 setup_child_process(s
, &cp
, "apply", "--cached", NULL
);
442 if (pipe_command(&cp
, s
->buf
.buf
, s
->buf
.len
,
444 error(_("'git apply --cached' failed"));
445 if (!repo_read_index(s
->s
.r
))
446 repo_refresh_and_write_index(s
->s
.r
, REFRESH_QUIET
, 0,
447 1, NULL
, NULL
, NULL
);
454 int run_add_p(struct repository
*r
, const struct pathspec
*ps
)
456 struct add_p_state s
= {
457 { r
}, STRBUF_INIT
, STRBUF_INIT
, STRBUF_INIT
, STRBUF_INIT
461 init_add_i_state(&s
.s
, r
);
463 if (discard_index(r
->index
) < 0 || repo_read_index(r
) < 0 ||
464 repo_refresh_and_write_index(r
, REFRESH_QUIET
, 0, 1,
465 NULL
, NULL
, NULL
) < 0 ||
466 parse_diff(&s
, ps
) < 0) {
467 strbuf_release(&s
.plain
);
468 strbuf_release(&s
.colored
);
472 for (i
= 0; i
< s
.file_diff_nr
; i
++)
473 if (patch_update_file(&s
, s
.file_diff
+ i
))
476 strbuf_release(&s
.answer
);
477 strbuf_release(&s
.buf
);
478 strbuf_release(&s
.plain
);
479 strbuf_release(&s
.colored
);