Git fork
at 3da4413dbcdb8df021739ca6f20fe4f0bcd1fd3c 1824 lines 53 kB view raw
1#define USE_THE_REPOSITORY_VARIABLE 2#define DISABLE_SIGN_COMPARE_WARNINGS 3 4#include "git-compat-util.h" 5#include "add-interactive.h" 6#include "advice.h" 7#include "editor.h" 8#include "environment.h" 9#include "gettext.h" 10#include "object-name.h" 11#include "pager.h" 12#include "read-cache-ll.h" 13#include "repository.h" 14#include "strbuf.h" 15#include "sigchain.h" 16#include "run-command.h" 17#include "strvec.h" 18#include "pathspec.h" 19#include "color.h" 20#include "compat/terminal.h" 21#include "prompt.h" 22 23enum prompt_mode_type { 24 PROMPT_MODE_CHANGE = 0, PROMPT_DELETION, PROMPT_ADDITION, PROMPT_HUNK, 25 PROMPT_MODE_MAX, /* must be last */ 26}; 27 28struct patch_mode { 29 /* 30 * The magic constant 4 is chosen such that all patch modes 31 * provide enough space for three command-line arguments followed by a 32 * trailing `NULL`. 33 */ 34 const char *diff_cmd[4], *apply_args[4], *apply_check_args[4]; 35 unsigned is_reverse:1, index_only:1, apply_for_checkout:1; 36 const char *prompt_mode[PROMPT_MODE_MAX]; 37 const char *edit_hunk_hint, *help_patch_text; 38}; 39 40static struct patch_mode patch_mode_add = { 41 .diff_cmd = { "diff-files", NULL }, 42 .apply_args = { "--cached", NULL }, 43 .apply_check_args = { "--cached", NULL }, 44 .prompt_mode = { 45 N_("Stage mode change [y,n,q,a,d%s,?]? "), 46 N_("Stage deletion [y,n,q,a,d%s,?]? "), 47 N_("Stage addition [y,n,q,a,d%s,?]? "), 48 N_("Stage this hunk [y,n,q,a,d%s,?]? ") 49 }, 50 .edit_hunk_hint = N_("If the patch applies cleanly, the edited hunk " 51 "will immediately be marked for staging."), 52 .help_patch_text = 53 N_("y - stage this hunk\n" 54 "n - do not stage this hunk\n" 55 "q - quit; do not stage this hunk or any of the remaining " 56 "ones\n" 57 "a - stage this hunk and all later hunks in the file\n" 58 "d - do not stage this hunk or any of the later hunks in " 59 "the file\n") 60}; 61 62static struct patch_mode patch_mode_stash = { 63 .diff_cmd = { "diff-index", "HEAD", NULL }, 64 .apply_args = { "--cached", NULL }, 65 .apply_check_args = { "--cached", NULL }, 66 .prompt_mode = { 67 N_("Stash mode change [y,n,q,a,d%s,?]? "), 68 N_("Stash deletion [y,n,q,a,d%s,?]? "), 69 N_("Stash addition [y,n,q,a,d%s,?]? "), 70 N_("Stash this hunk [y,n,q,a,d%s,?]? "), 71 }, 72 .edit_hunk_hint = N_("If the patch applies cleanly, the edited hunk " 73 "will immediately be marked for stashing."), 74 .help_patch_text = 75 N_("y - stash this hunk\n" 76 "n - do not stash this hunk\n" 77 "q - quit; do not stash this hunk or any of the remaining " 78 "ones\n" 79 "a - stash this hunk and all later hunks in the file\n" 80 "d - do not stash this hunk or any of the later hunks in " 81 "the file\n"), 82}; 83 84static struct patch_mode patch_mode_reset_head = { 85 .diff_cmd = { "diff-index", "--cached", NULL }, 86 .apply_args = { "-R", "--cached", NULL }, 87 .apply_check_args = { "-R", "--cached", NULL }, 88 .is_reverse = 1, 89 .index_only = 1, 90 .prompt_mode = { 91 N_("Unstage mode change [y,n,q,a,d%s,?]? "), 92 N_("Unstage deletion [y,n,q,a,d%s,?]? "), 93 N_("Unstage addition [y,n,q,a,d%s,?]? "), 94 N_("Unstage this hunk [y,n,q,a,d%s,?]? "), 95 }, 96 .edit_hunk_hint = N_("If the patch applies cleanly, the edited hunk " 97 "will immediately be marked for unstaging."), 98 .help_patch_text = 99 N_("y - unstage this hunk\n" 100 "n - do not unstage this hunk\n" 101 "q - quit; do not unstage this hunk or any of the remaining " 102 "ones\n" 103 "a - unstage this hunk and all later hunks in the file\n" 104 "d - do not unstage this hunk or any of the later hunks in " 105 "the file\n"), 106}; 107 108static struct patch_mode patch_mode_reset_nothead = { 109 .diff_cmd = { "diff-index", "-R", "--cached", NULL }, 110 .apply_args = { "--cached", NULL }, 111 .apply_check_args = { "--cached", NULL }, 112 .index_only = 1, 113 .prompt_mode = { 114 N_("Apply mode change to index [y,n,q,a,d%s,?]? "), 115 N_("Apply deletion to index [y,n,q,a,d%s,?]? "), 116 N_("Apply addition to index [y,n,q,a,d%s,?]? "), 117 N_("Apply this hunk to index [y,n,q,a,d%s,?]? "), 118 }, 119 .edit_hunk_hint = N_("If the patch applies cleanly, the edited hunk " 120 "will immediately be marked for applying."), 121 .help_patch_text = 122 N_("y - apply this hunk to index\n" 123 "n - do not apply this hunk to index\n" 124 "q - quit; do not apply this hunk or any of the remaining " 125 "ones\n" 126 "a - apply this hunk and all later hunks in the file\n" 127 "d - do not apply this hunk or any of the later hunks in " 128 "the file\n"), 129}; 130 131static struct patch_mode patch_mode_checkout_index = { 132 .diff_cmd = { "diff-files", NULL }, 133 .apply_args = { "-R", NULL }, 134 .apply_check_args = { "-R", NULL }, 135 .is_reverse = 1, 136 .prompt_mode = { 137 N_("Discard mode change from worktree [y,n,q,a,d%s,?]? "), 138 N_("Discard deletion from worktree [y,n,q,a,d%s,?]? "), 139 N_("Discard addition from worktree [y,n,q,a,d%s,?]? "), 140 N_("Discard this hunk from worktree [y,n,q,a,d%s,?]? "), 141 }, 142 .edit_hunk_hint = N_("If the patch applies cleanly, the edited hunk " 143 "will immediately be marked for discarding."), 144 .help_patch_text = 145 N_("y - discard this hunk from worktree\n" 146 "n - do not discard this hunk from worktree\n" 147 "q - quit; do not discard this hunk or any of the remaining " 148 "ones\n" 149 "a - discard this hunk and all later hunks in the file\n" 150 "d - do not discard this hunk or any of the later hunks in " 151 "the file\n"), 152}; 153 154static struct patch_mode patch_mode_checkout_head = { 155 .diff_cmd = { "diff-index", NULL }, 156 .apply_for_checkout = 1, 157 .apply_check_args = { "-R", NULL }, 158 .is_reverse = 1, 159 .prompt_mode = { 160 N_("Discard mode change from index and worktree [y,n,q,a,d%s,?]? "), 161 N_("Discard deletion from index and worktree [y,n,q,a,d%s,?]? "), 162 N_("Discard addition from index and worktree [y,n,q,a,d%s,?]? "), 163 N_("Discard this hunk from index and worktree [y,n,q,a,d%s,?]? "), 164 }, 165 .edit_hunk_hint = N_("If the patch applies cleanly, the edited hunk " 166 "will immediately be marked for discarding."), 167 .help_patch_text = 168 N_("y - discard this hunk from index and worktree\n" 169 "n - do not discard this hunk from index and worktree\n" 170 "q - quit; do not discard this hunk or any of the remaining " 171 "ones\n" 172 "a - discard this hunk and all later hunks in the file\n" 173 "d - do not discard this hunk or any of the later hunks in " 174 "the file\n"), 175}; 176 177static struct patch_mode patch_mode_checkout_nothead = { 178 .diff_cmd = { "diff-index", "-R", NULL }, 179 .apply_for_checkout = 1, 180 .apply_check_args = { NULL }, 181 .prompt_mode = { 182 N_("Apply mode change to index and worktree [y,n,q,a,d%s,?]? "), 183 N_("Apply deletion to index and worktree [y,n,q,a,d%s,?]? "), 184 N_("Apply addition to index and worktree [y,n,q,a,d%s,?]? "), 185 N_("Apply this hunk to index and worktree [y,n,q,a,d%s,?]? "), 186 }, 187 .edit_hunk_hint = N_("If the patch applies cleanly, the edited hunk " 188 "will immediately be marked for applying."), 189 .help_patch_text = 190 N_("y - apply this hunk to index and worktree\n" 191 "n - do not apply this hunk to index and worktree\n" 192 "q - quit; do not apply this hunk or any of the remaining " 193 "ones\n" 194 "a - apply this hunk and all later hunks in the file\n" 195 "d - do not apply this hunk or any of the later hunks in " 196 "the file\n"), 197}; 198 199static struct patch_mode patch_mode_worktree_head = { 200 .diff_cmd = { "diff-index", NULL }, 201 .apply_args = { "-R", NULL }, 202 .apply_check_args = { "-R", NULL }, 203 .is_reverse = 1, 204 .prompt_mode = { 205 N_("Discard mode change from worktree [y,n,q,a,d%s,?]? "), 206 N_("Discard deletion from worktree [y,n,q,a,d%s,?]? "), 207 N_("Discard addition from worktree [y,n,q,a,d%s,?]? "), 208 N_("Discard this hunk from worktree [y,n,q,a,d%s,?]? "), 209 }, 210 .edit_hunk_hint = N_("If the patch applies cleanly, the edited hunk " 211 "will immediately be marked for discarding."), 212 .help_patch_text = 213 N_("y - discard this hunk from worktree\n" 214 "n - do not discard this hunk from worktree\n" 215 "q - quit; do not discard this hunk or any of the remaining " 216 "ones\n" 217 "a - discard this hunk and all later hunks in the file\n" 218 "d - do not discard this hunk or any of the later hunks in " 219 "the file\n"), 220}; 221 222static struct patch_mode patch_mode_worktree_nothead = { 223 .diff_cmd = { "diff-index", "-R", NULL }, 224 .apply_args = { NULL }, 225 .apply_check_args = { NULL }, 226 .prompt_mode = { 227 N_("Apply mode change to worktree [y,n,q,a,d%s,?]? "), 228 N_("Apply deletion to worktree [y,n,q,a,d%s,?]? "), 229 N_("Apply addition to worktree [y,n,q,a,d%s,?]? "), 230 N_("Apply this hunk to worktree [y,n,q,a,d%s,?]? "), 231 }, 232 .edit_hunk_hint = N_("If the patch applies cleanly, the edited hunk " 233 "will immediately be marked for applying."), 234 .help_patch_text = 235 N_("y - apply this hunk to worktree\n" 236 "n - do not apply this hunk to worktree\n" 237 "q - quit; do not apply this hunk or any of the remaining " 238 "ones\n" 239 "a - apply this hunk and all later hunks in the file\n" 240 "d - do not apply this hunk or any of the later hunks in " 241 "the file\n"), 242}; 243 244struct hunk_header { 245 unsigned long old_offset, old_count, new_offset, new_count; 246 /* 247 * Start/end offsets to the extra text after the second `@@` in the 248 * hunk header, e.g. the function signature. This is expected to 249 * include the newline. 250 */ 251 size_t extra_start, extra_end, colored_extra_start, colored_extra_end; 252 unsigned suppress_colored_line_range:1; 253}; 254 255struct hunk { 256 size_t start, end, colored_start, colored_end, splittable_into; 257 ssize_t delta; 258 enum { UNDECIDED_HUNK = 0, SKIP_HUNK, USE_HUNK } use; 259 struct hunk_header header; 260}; 261 262struct add_p_state { 263 struct add_i_state s; 264 struct strbuf answer, buf; 265 266 /* parsed diff */ 267 struct strbuf plain, colored; 268 struct file_diff { 269 struct hunk head; 270 struct hunk *hunk; 271 size_t hunk_nr, hunk_alloc; 272 unsigned deleted:1, added:1, mode_change:1,binary:1; 273 } *file_diff; 274 size_t file_diff_nr; 275 276 /* patch mode */ 277 struct patch_mode *mode; 278 const char *revision; 279}; 280 281static void add_p_state_clear(struct add_p_state *s) 282{ 283 size_t i; 284 285 strbuf_release(&s->answer); 286 strbuf_release(&s->buf); 287 strbuf_release(&s->plain); 288 strbuf_release(&s->colored); 289 for (i = 0; i < s->file_diff_nr; i++) 290 free(s->file_diff[i].hunk); 291 free(s->file_diff); 292 clear_add_i_state(&s->s); 293} 294 295__attribute__((format (printf, 2, 3))) 296static void err(struct add_p_state *s, const char *fmt, ...) 297{ 298 va_list args; 299 300 va_start(args, fmt); 301 fputs(s->s.error_color, stdout); 302 vprintf(fmt, args); 303 puts(s->s.reset_color); 304 va_end(args); 305} 306 307LAST_ARG_MUST_BE_NULL 308static void setup_child_process(struct add_p_state *s, 309 struct child_process *cp, ...) 310{ 311 va_list ap; 312 const char *arg; 313 314 va_start(ap, cp); 315 while ((arg = va_arg(ap, const char *))) 316 strvec_push(&cp->args, arg); 317 va_end(ap); 318 319 cp->git_cmd = 1; 320 strvec_pushf(&cp->env, 321 INDEX_ENVIRONMENT "=%s", s->s.r->index_file); 322} 323 324static int parse_range(const char **p, 325 unsigned long *offset, unsigned long *count) 326{ 327 char *pend; 328 329 *offset = strtoul(*p, &pend, 10); 330 if (pend == *p) 331 return -1; 332 if (*pend != ',') { 333 *count = 1; 334 *p = pend; 335 return 0; 336 } 337 *count = strtoul(pend + 1, (char **)p, 10); 338 return *p == pend + 1 ? -1 : 0; 339} 340 341static int parse_hunk_header(struct add_p_state *s, struct hunk *hunk) 342{ 343 struct hunk_header *header = &hunk->header; 344 const char *line = s->plain.buf + hunk->start, *p = line; 345 char *eol = memchr(p, '\n', s->plain.len - hunk->start); 346 347 if (!eol) 348 eol = s->plain.buf + s->plain.len; 349 350 if (!skip_prefix(p, "@@ -", &p) || 351 parse_range(&p, &header->old_offset, &header->old_count) < 0 || 352 !skip_prefix(p, " +", &p) || 353 parse_range(&p, &header->new_offset, &header->new_count) < 0 || 354 !skip_prefix(p, " @@", &p)) 355 return error(_("could not parse hunk header '%.*s'"), 356 (int)(eol - line), line); 357 358 hunk->start = eol - s->plain.buf + (*eol == '\n'); 359 header->extra_start = p - s->plain.buf; 360 header->extra_end = hunk->start; 361 362 if (!s->colored.len) { 363 header->colored_extra_start = header->colored_extra_end = 0; 364 return 0; 365 } 366 367 /* Now find the extra text in the colored diff */ 368 line = s->colored.buf + hunk->colored_start; 369 eol = memchr(line, '\n', s->colored.len - hunk->colored_start); 370 if (!eol) 371 eol = s->colored.buf + s->colored.len; 372 p = memmem(line, eol - line, "@@ -", 4); 373 if (p && (p = memmem(p + 4, eol - p - 4, " @@", 3))) { 374 header->colored_extra_start = p + 3 - s->colored.buf; 375 } else { 376 /* could not parse colored hunk header, leave as-is */ 377 header->colored_extra_start = hunk->colored_start; 378 header->suppress_colored_line_range = 1; 379 } 380 hunk->colored_start = eol - s->colored.buf + (*eol == '\n'); 381 header->colored_extra_end = hunk->colored_start; 382 383 return 0; 384} 385 386static int is_octal(const char *p, size_t len) 387{ 388 if (!len) 389 return 0; 390 391 while (len--) 392 if (*p < '0' || *(p++) > '7') 393 return 0; 394 return 1; 395} 396 397static void complete_file(char marker, struct hunk *hunk) 398{ 399 if (marker == '-' || marker == '+') 400 /* 401 * Last hunk ended in non-context line (i.e. it 402 * appended lines to the file, so there are no 403 * trailing context lines). 404 */ 405 hunk->splittable_into++; 406} 407 408/* Empty context lines may omit the leading ' ' */ 409static int normalize_marker(const char *p) 410{ 411 return p[0] == '\n' || (p[0] == '\r' && p[1] == '\n') ? ' ' : p[0]; 412} 413 414static int parse_diff(struct add_p_state *s, const struct pathspec *ps) 415{ 416 struct strvec args = STRVEC_INIT; 417 struct strbuf *plain = &s->plain, *colored = NULL; 418 struct child_process cp = CHILD_PROCESS_INIT; 419 char *p, *pend, *colored_p = NULL, *colored_pend = NULL, marker = '\0'; 420 size_t file_diff_alloc = 0, i, color_arg_index; 421 struct file_diff *file_diff = NULL; 422 struct hunk *hunk = NULL; 423 int res; 424 425 strvec_pushv(&args, s->mode->diff_cmd); 426 if (s->s.context != -1) 427 strvec_pushf(&args, "--unified=%i", s->s.context); 428 if (s->s.interhunkcontext != -1) 429 strvec_pushf(&args, "--inter-hunk-context=%i", s->s.interhunkcontext); 430 if (s->s.interactive_diff_algorithm) 431 strvec_pushf(&args, "--diff-algorithm=%s", s->s.interactive_diff_algorithm); 432 if (s->revision) { 433 struct object_id oid; 434 strvec_push(&args, 435 /* could be on an unborn branch */ 436 !strcmp("HEAD", s->revision) && 437 repo_get_oid(the_repository, "HEAD", &oid) ? 438 empty_tree_oid_hex(the_repository->hash_algo) : s->revision); 439 } 440 color_arg_index = args.nr; 441 /* Use `--no-color` explicitly, just in case `diff.color = always`. */ 442 strvec_pushl(&args, "--no-color", "--ignore-submodules=dirty", "-p", 443 "--", NULL); 444 for (i = 0; i < ps->nr; i++) 445 strvec_push(&args, ps->items[i].original); 446 447 setup_child_process(s, &cp, NULL); 448 strvec_pushv(&cp.args, args.v); 449 res = capture_command(&cp, plain, 0); 450 if (res) { 451 strvec_clear(&args); 452 return error(_("could not parse diff")); 453 } 454 if (!plain->len) { 455 strvec_clear(&args); 456 return 0; 457 } 458 strbuf_complete_line(plain); 459 460 if (want_color_fd(1, -1)) { 461 struct child_process colored_cp = CHILD_PROCESS_INIT; 462 const char *diff_filter = s->s.interactive_diff_filter; 463 464 setup_child_process(s, &colored_cp, NULL); 465 xsnprintf((char *)args.v[color_arg_index], 8, "--color"); 466 strvec_pushv(&colored_cp.args, args.v); 467 colored = &s->colored; 468 res = capture_command(&colored_cp, colored, 0); 469 strvec_clear(&args); 470 if (res) 471 return error(_("could not parse colored diff")); 472 473 if (diff_filter) { 474 struct child_process filter_cp = CHILD_PROCESS_INIT; 475 476 setup_child_process(s, &filter_cp, 477 diff_filter, NULL); 478 filter_cp.git_cmd = 0; 479 filter_cp.use_shell = 1; 480 strbuf_reset(&s->buf); 481 if (pipe_command(&filter_cp, 482 colored->buf, colored->len, 483 &s->buf, colored->len, 484 NULL, 0) < 0) 485 return error(_("failed to run '%s'"), 486 diff_filter); 487 strbuf_swap(colored, &s->buf); 488 } 489 490 strbuf_complete_line(colored); 491 colored_p = colored->buf; 492 colored_pend = colored_p + colored->len; 493 } 494 strvec_clear(&args); 495 496 /* parse files and hunks */ 497 p = plain->buf; 498 pend = p + plain->len; 499 while (p != pend) { 500 char *eol = memchr(p, '\n', pend - p); 501 const char *deleted = NULL, *mode_change = NULL; 502 char ch = normalize_marker(p); 503 504 if (!eol) 505 eol = pend; 506 507 if (starts_with(p, "diff ") || 508 starts_with(p, "* Unmerged path ")) { 509 complete_file(marker, hunk); 510 ALLOC_GROW_BY(s->file_diff, s->file_diff_nr, 1, 511 file_diff_alloc); 512 file_diff = s->file_diff + s->file_diff_nr - 1; 513 hunk = &file_diff->head; 514 hunk->start = p - plain->buf; 515 if (colored_p) 516 hunk->colored_start = colored_p - colored->buf; 517 marker = '\0'; 518 } else if (p == plain->buf) 519 BUG("diff starts with unexpected line:\n" 520 "%.*s\n", (int)(eol - p), p); 521 else if (file_diff->deleted) 522 ; /* keep the rest of the file in a single "hunk" */ 523 else if (starts_with(p, "@@ ") || 524 (hunk == &file_diff->head && 525 (skip_prefix(p, "deleted file", &deleted)))) { 526 if (marker == '-' || marker == '+') 527 /* 528 * Should not happen; previous hunk did not end 529 * in a context line? Handle it anyway. 530 */ 531 hunk->splittable_into++; 532 533 ALLOC_GROW_BY(file_diff->hunk, file_diff->hunk_nr, 1, 534 file_diff->hunk_alloc); 535 hunk = file_diff->hunk + file_diff->hunk_nr - 1; 536 537 hunk->start = p - plain->buf; 538 if (colored) 539 hunk->colored_start = colored_p - colored->buf; 540 541 if (deleted) 542 file_diff->deleted = 1; 543 else if (parse_hunk_header(s, hunk) < 0) 544 return -1; 545 546 /* 547 * Start counting into how many hunks this one can be 548 * split 549 */ 550 marker = ch; 551 } else if (hunk == &file_diff->head && 552 starts_with(p, "new file")) { 553 file_diff->added = 1; 554 } else if (hunk == &file_diff->head && 555 skip_prefix(p, "old mode ", &mode_change) && 556 is_octal(mode_change, eol - mode_change)) { 557 if (file_diff->mode_change) 558 BUG("double mode change?\n\n%.*s", 559 (int)(eol - plain->buf), plain->buf); 560 if (file_diff->hunk_nr) 561 BUG("mode change in the middle?\n\n%.*s", 562 (int)(eol - plain->buf), plain->buf); 563 564 /* 565 * Do *not* change `hunk`: the mode change pseudo-hunk 566 * is _part of_ the header "hunk". 567 */ 568 file_diff->mode_change = 1; 569 ALLOC_GROW_BY(file_diff->hunk, file_diff->hunk_nr, 1, 570 file_diff->hunk_alloc); 571 file_diff->hunk->start = p - plain->buf; 572 if (colored_p) 573 file_diff->hunk->colored_start = 574 colored_p - colored->buf; 575 } else if (hunk == &file_diff->head && 576 skip_prefix(p, "new mode ", &mode_change) && 577 is_octal(mode_change, eol - mode_change)) { 578 579 /* 580 * Extend the "mode change" pseudo-hunk to include also 581 * the "new mode" line. 582 */ 583 if (!file_diff->mode_change) 584 BUG("'new mode' without 'old mode'?\n\n%.*s", 585 (int)(eol - plain->buf), plain->buf); 586 if (file_diff->hunk_nr != 1) 587 BUG("mode change in the middle?\n\n%.*s", 588 (int)(eol - plain->buf), plain->buf); 589 if (p - plain->buf != file_diff->hunk->end) 590 BUG("'new mode' does not immediately follow " 591 "'old mode'?\n\n%.*s", 592 (int)(eol - plain->buf), plain->buf); 593 } else if (hunk == &file_diff->head && 594 starts_with(p, "Binary files ")) 595 file_diff->binary = 1; 596 597 if (!!file_diff->deleted + !!file_diff->added + 598 !!file_diff->mode_change > 1) 599 BUG("diff can only contain delete *or* add *or* a " 600 "mode change?!?\n%.*s", 601 (int)(eol - (plain->buf + file_diff->head.start)), 602 plain->buf + file_diff->head.start); 603 604 if ((marker == '-' || marker == '+') && ch == ' ') 605 hunk->splittable_into++; 606 if (marker && ch != '\\') 607 marker = ch; 608 609 p = eol == pend ? pend : eol + 1; 610 hunk->end = p - plain->buf; 611 612 if (colored) { 613 char *colored_eol = memchr(colored_p, '\n', 614 colored_pend - colored_p); 615 if (colored_eol) 616 colored_p = colored_eol + 1; 617 else if (p != pend) 618 /* non-colored has more lines? */ 619 goto mismatched_output; 620 else if (colored_p == colored_pend) 621 /* last line has no matching colored one? */ 622 goto mismatched_output; 623 else 624 colored_p = colored_pend; 625 626 hunk->colored_end = colored_p - colored->buf; 627 } 628 629 if (mode_change) { 630 if (file_diff->hunk_nr != 1) 631 BUG("mode change in hunk #%d???", 632 (int)file_diff->hunk_nr); 633 /* Adjust the end of the "mode change" pseudo-hunk */ 634 file_diff->hunk->end = hunk->end; 635 if (colored) 636 file_diff->hunk->colored_end = hunk->colored_end; 637 } 638 } 639 complete_file(marker, hunk); 640 641 /* non-colored shorter than colored? */ 642 if (colored_p != colored_pend) { 643mismatched_output: 644 error(_("mismatched output from interactive.diffFilter")); 645 advise(_("Your filter must maintain a one-to-one correspondence\n" 646 "between its input and output lines.")); 647 return -1; 648 } 649 650 return 0; 651} 652 653static size_t find_next_line(struct strbuf *sb, size_t offset) 654{ 655 char *eol; 656 657 if (offset >= sb->len) 658 BUG("looking for next line beyond buffer (%d >= %d)\n%s", 659 (int)offset, (int)sb->len, sb->buf); 660 661 eol = memchr(sb->buf + offset, '\n', sb->len - offset); 662 if (!eol) 663 return sb->len; 664 return eol - sb->buf + 1; 665} 666 667static void render_hunk(struct add_p_state *s, struct hunk *hunk, 668 ssize_t delta, int colored, struct strbuf *out) 669{ 670 struct hunk_header *header = &hunk->header; 671 672 if (hunk->header.old_offset != 0 || hunk->header.new_offset != 0) { 673 /* 674 * Generate the hunk header dynamically, except for special 675 * hunks (such as the diff header). 676 */ 677 const char *p; 678 size_t len; 679 unsigned long old_offset = header->old_offset; 680 unsigned long new_offset = header->new_offset; 681 682 if (!colored) { 683 p = s->plain.buf + header->extra_start; 684 len = header->extra_end - header->extra_start; 685 } else if (header->suppress_colored_line_range) { 686 strbuf_add(out, 687 s->colored.buf + header->colored_extra_start, 688 header->colored_extra_end - 689 header->colored_extra_start); 690 691 strbuf_add(out, s->colored.buf + hunk->colored_start, 692 hunk->colored_end - hunk->colored_start); 693 return; 694 } else { 695 strbuf_addstr(out, s->s.fraginfo_color); 696 p = s->colored.buf + header->colored_extra_start; 697 len = header->colored_extra_end 698 - header->colored_extra_start; 699 } 700 701 if (s->mode->is_reverse) 702 old_offset -= delta; 703 else 704 new_offset += delta; 705 706 strbuf_addf(out, "@@ -%lu", old_offset); 707 if (header->old_count != 1) 708 strbuf_addf(out, ",%lu", header->old_count); 709 strbuf_addf(out, " +%lu", new_offset); 710 if (header->new_count != 1) 711 strbuf_addf(out, ",%lu", header->new_count); 712 strbuf_addstr(out, " @@"); 713 714 if (len) 715 strbuf_add(out, p, len); 716 else if (colored) 717 strbuf_addf(out, "%s\n", s->s.reset_color); 718 else 719 strbuf_addch(out, '\n'); 720 } 721 722 if (colored) 723 strbuf_add(out, s->colored.buf + hunk->colored_start, 724 hunk->colored_end - hunk->colored_start); 725 else 726 strbuf_add(out, s->plain.buf + hunk->start, 727 hunk->end - hunk->start); 728} 729 730static void render_diff_header(struct add_p_state *s, 731 struct file_diff *file_diff, int colored, 732 struct strbuf *out) 733{ 734 /* 735 * If there was a mode change, the first hunk is a pseudo hunk that 736 * corresponds to the mode line in the header. If the user did not want 737 * to stage that "hunk", we actually have to cut it out from the header. 738 */ 739 int skip_mode_change = 740 file_diff->mode_change && file_diff->hunk->use != USE_HUNK; 741 struct hunk *head = &file_diff->head, *first = file_diff->hunk; 742 743 if (!skip_mode_change) { 744 render_hunk(s, head, 0, colored, out); 745 return; 746 } 747 748 if (colored) { 749 const char *p = s->colored.buf; 750 751 strbuf_add(out, p + head->colored_start, 752 first->colored_start - head->colored_start); 753 strbuf_add(out, p + first->colored_end, 754 head->colored_end - first->colored_end); 755 } else { 756 const char *p = s->plain.buf; 757 758 strbuf_add(out, p + head->start, first->start - head->start); 759 strbuf_add(out, p + first->end, head->end - first->end); 760 } 761} 762 763/* Coalesce hunks again that were split */ 764static int merge_hunks(struct add_p_state *s, struct file_diff *file_diff, 765 size_t *hunk_index, int use_all, struct hunk *merged) 766{ 767 size_t i = *hunk_index, delta; 768 struct hunk *hunk = file_diff->hunk + i; 769 /* `header` corresponds to the merged hunk */ 770 struct hunk_header *header = &merged->header, *next; 771 772 if (!use_all && hunk->use != USE_HUNK) 773 return 0; 774 775 *merged = *hunk; 776 /* We simply skip the colored part (if any) when merging hunks */ 777 merged->colored_start = merged->colored_end = 0; 778 779 for (; i + 1 < file_diff->hunk_nr; i++) { 780 hunk++; 781 next = &hunk->header; 782 783 /* 784 * Stop merging hunks when: 785 * 786 * - the hunk is not selected for use, or 787 * - the hunk does not overlap with the already-merged hunk(s) 788 */ 789 if ((!use_all && hunk->use != USE_HUNK) || 790 header->new_offset >= next->new_offset + merged->delta || 791 header->new_offset + header->new_count 792 < next->new_offset + merged->delta) 793 break; 794 795 /* 796 * If the hunks were not edited, and overlap, we can simply 797 * extend the line range. 798 */ 799 if (merged->start < hunk->start && merged->end > hunk->start) { 800 merged->end = hunk->end; 801 merged->colored_end = hunk->colored_end; 802 delta = 0; 803 } else { 804 const char *plain = s->plain.buf; 805 size_t overlapping_line_count = header->new_offset 806 + header->new_count - merged->delta 807 - next->new_offset; 808 size_t overlap_end = hunk->start; 809 size_t overlap_start = overlap_end; 810 size_t overlap_next, len, j; 811 812 /* 813 * One of the hunks was edited: the modified hunk was 814 * appended to the strbuf `s->plain`. 815 * 816 * Let's ensure that at least the last context line of 817 * the first hunk overlaps with the corresponding line 818 * of the second hunk, and then merge. 819 */ 820 for (j = 0; j < overlapping_line_count; j++) { 821 overlap_next = find_next_line(&s->plain, 822 overlap_end); 823 824 if (overlap_next > hunk->end) 825 BUG("failed to find %d context lines " 826 "in:\n%.*s", 827 (int)overlapping_line_count, 828 (int)(hunk->end - hunk->start), 829 plain + hunk->start); 830 831 if (normalize_marker(&plain[overlap_end]) != ' ') 832 return error(_("expected context line " 833 "#%d in\n%.*s"), 834 (int)(j + 1), 835 (int)(hunk->end 836 - hunk->start), 837 plain + hunk->start); 838 839 overlap_start = overlap_end; 840 overlap_end = overlap_next; 841 } 842 len = overlap_end - overlap_start; 843 844 if (len > merged->end - merged->start || 845 memcmp(plain + merged->end - len, 846 plain + overlap_start, len)) 847 return error(_("hunks do not overlap:\n%.*s\n" 848 "\tdoes not end with:\n%.*s"), 849 (int)(merged->end - merged->start), 850 plain + merged->start, 851 (int)len, plain + overlap_start); 852 853 /* 854 * Since the start-end ranges are not adjacent, we 855 * cannot simply take the union of the ranges. To 856 * address that, we temporarily append the union of the 857 * lines to the `plain` strbuf. 858 */ 859 if (merged->end != s->plain.len) { 860 size_t start = s->plain.len; 861 862 strbuf_add(&s->plain, plain + merged->start, 863 merged->end - merged->start); 864 plain = s->plain.buf; 865 merged->start = start; 866 merged->end = s->plain.len; 867 } 868 869 strbuf_add(&s->plain, 870 plain + overlap_end, 871 hunk->end - overlap_end); 872 merged->end = s->plain.len; 873 merged->splittable_into += hunk->splittable_into; 874 delta = merged->delta; 875 merged->delta += hunk->delta; 876 } 877 878 header->old_count = next->old_offset + next->old_count 879 - header->old_offset; 880 header->new_count = next->new_offset + delta 881 + next->new_count - header->new_offset; 882 } 883 884 if (i == *hunk_index) 885 return 0; 886 887 *hunk_index = i; 888 return 1; 889} 890 891static void reassemble_patch(struct add_p_state *s, 892 struct file_diff *file_diff, int use_all, 893 struct strbuf *out) 894{ 895 struct hunk *hunk; 896 size_t save_len = s->plain.len, i; 897 ssize_t delta = 0; 898 899 render_diff_header(s, file_diff, 0, out); 900 901 for (i = file_diff->mode_change; i < file_diff->hunk_nr; i++) { 902 struct hunk merged = { 0 }; 903 904 hunk = file_diff->hunk + i; 905 if (!use_all && hunk->use != USE_HUNK) 906 delta += hunk->header.old_count 907 - hunk->header.new_count; 908 else { 909 /* merge overlapping hunks into a temporary hunk */ 910 if (merge_hunks(s, file_diff, &i, use_all, &merged)) 911 hunk = &merged; 912 913 render_hunk(s, hunk, delta, 0, out); 914 915 /* 916 * In case `merge_hunks()` used `plain` as a scratch 917 * pad (this happens when an edited hunk had to be 918 * coalesced with another hunk). 919 */ 920 strbuf_setlen(&s->plain, save_len); 921 922 delta += hunk->delta; 923 } 924 } 925} 926 927static int split_hunk(struct add_p_state *s, struct file_diff *file_diff, 928 size_t hunk_index) 929{ 930 int colored = !!s->colored.len, first = 1; 931 struct hunk *hunk = file_diff->hunk + hunk_index; 932 size_t splittable_into; 933 size_t end, colored_end, current, colored_current = 0, context_line_count; 934 struct hunk_header remaining, *header; 935 char marker, ch; 936 937 if (hunk_index >= file_diff->hunk_nr) 938 BUG("invalid hunk index: %d (must be >= 0 and < %d)", 939 (int)hunk_index, (int)file_diff->hunk_nr); 940 941 if (hunk->splittable_into < 2) 942 return 0; 943 splittable_into = hunk->splittable_into; 944 945 end = hunk->end; 946 colored_end = hunk->colored_end; 947 948 remaining = hunk->header; 949 950 file_diff->hunk_nr += splittable_into - 1; 951 ALLOC_GROW(file_diff->hunk, file_diff->hunk_nr, file_diff->hunk_alloc); 952 if (hunk_index + splittable_into < file_diff->hunk_nr) 953 memmove(file_diff->hunk + hunk_index + splittable_into, 954 file_diff->hunk + hunk_index + 1, 955 (file_diff->hunk_nr - hunk_index - splittable_into) 956 * sizeof(*hunk)); 957 hunk = file_diff->hunk + hunk_index; 958 hunk->splittable_into = 1; 959 memset(hunk + 1, 0, (splittable_into - 1) * sizeof(*hunk)); 960 961 header = &hunk->header; 962 header->old_count = header->new_count = 0; 963 964 current = hunk->start; 965 if (colored) 966 colored_current = hunk->colored_start; 967 marker = '\0'; 968 context_line_count = 0; 969 970 while (splittable_into > 1) { 971 ch = normalize_marker(&s->plain.buf[current]); 972 973 if (!ch) 974 BUG("buffer overrun while splitting hunks"); 975 976 /* 977 * Is this the first context line after a chain of +/- lines? 978 * Then record the start of the next split hunk. 979 */ 980 if ((marker == '-' || marker == '+') && ch == ' ') { 981 first = 0; 982 hunk[1].start = current; 983 if (colored) 984 hunk[1].colored_start = colored_current; 985 context_line_count = 0; 986 } 987 988 /* 989 * Was the previous line a +/- one? Alternatively, is this the 990 * first line (and not a +/- one)? 991 * 992 * Then just increment the appropriate counter and continue 993 * with the next line. 994 */ 995 if (marker != ' ' || (ch != '-' && ch != '+')) { 996next_hunk_line: 997 /* Comment lines are attached to the previous line */ 998 if (ch == '\\') 999 ch = marker ? marker : ' '; 1000 1001 /* current hunk not done yet */ 1002 if (ch == ' ') 1003 context_line_count++; 1004 else if (ch == '-') 1005 header->old_count++; 1006 else if (ch == '+') 1007 header->new_count++; 1008 else 1009 BUG("unhandled diff marker: '%c'", ch); 1010 marker = ch; 1011 current = find_next_line(&s->plain, current); 1012 if (colored) 1013 colored_current = 1014 find_next_line(&s->colored, 1015 colored_current); 1016 continue; 1017 } 1018 1019 /* 1020 * We got us the start of a new hunk! 1021 * 1022 * This is a context line, so it is shared with the previous 1023 * hunk, if any. 1024 */ 1025 1026 if (first) { 1027 if (header->old_count || header->new_count) 1028 BUG("counts are off: %d/%d", 1029 (int)header->old_count, 1030 (int)header->new_count); 1031 1032 header->old_count = context_line_count; 1033 header->new_count = context_line_count; 1034 context_line_count = 0; 1035 first = 0; 1036 goto next_hunk_line; 1037 } 1038 1039 remaining.old_offset += header->old_count; 1040 remaining.old_count -= header->old_count; 1041 remaining.new_offset += header->new_count; 1042 remaining.new_count -= header->new_count; 1043 1044 /* initialize next hunk header's offsets */ 1045 hunk[1].header.old_offset = 1046 header->old_offset + header->old_count; 1047 hunk[1].header.new_offset = 1048 header->new_offset + header->new_count; 1049 1050 /* add one split hunk */ 1051 header->old_count += context_line_count; 1052 header->new_count += context_line_count; 1053 1054 hunk->end = current; 1055 if (colored) 1056 hunk->colored_end = colored_current; 1057 1058 hunk++; 1059 hunk->splittable_into = 1; 1060 hunk->use = hunk[-1].use; 1061 header = &hunk->header; 1062 1063 header->old_count = header->new_count = context_line_count; 1064 context_line_count = 0; 1065 1066 splittable_into--; 1067 marker = ch; 1068 } 1069 1070 /* last hunk simply gets the rest */ 1071 if (header->old_offset != remaining.old_offset) 1072 BUG("miscounted old_offset: %lu != %lu", 1073 header->old_offset, remaining.old_offset); 1074 if (header->new_offset != remaining.new_offset) 1075 BUG("miscounted new_offset: %lu != %lu", 1076 header->new_offset, remaining.new_offset); 1077 header->old_count = remaining.old_count; 1078 header->new_count = remaining.new_count; 1079 hunk->end = end; 1080 if (colored) 1081 hunk->colored_end = colored_end; 1082 1083 return 0; 1084} 1085 1086static void recolor_hunk(struct add_p_state *s, struct hunk *hunk) 1087{ 1088 const char *plain = s->plain.buf; 1089 size_t current, eol, next; 1090 1091 if (!s->colored.len) 1092 return; 1093 1094 hunk->colored_start = s->colored.len; 1095 for (current = hunk->start; current < hunk->end; ) { 1096 for (eol = current; eol < hunk->end; eol++) 1097 if (plain[eol] == '\n') 1098 break; 1099 next = eol + (eol < hunk->end); 1100 if (eol > current && plain[eol - 1] == '\r') 1101 eol--; 1102 1103 strbuf_addstr(&s->colored, 1104 plain[current] == '-' ? 1105 s->s.file_old_color : 1106 plain[current] == '+' ? 1107 s->s.file_new_color : 1108 s->s.context_color); 1109 strbuf_add(&s->colored, plain + current, eol - current); 1110 strbuf_addstr(&s->colored, s->s.reset_color); 1111 if (next > eol) 1112 strbuf_add(&s->colored, plain + eol, next - eol); 1113 current = next; 1114 } 1115 hunk->colored_end = s->colored.len; 1116} 1117 1118static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk) 1119{ 1120 size_t i; 1121 1122 strbuf_reset(&s->buf); 1123 strbuf_commented_addf(&s->buf, comment_line_str, 1124 _("Manual hunk edit mode -- see bottom for " 1125 "a quick guide.\n")); 1126 render_hunk(s, hunk, 0, 0, &s->buf); 1127 strbuf_commented_addf(&s->buf, comment_line_str, 1128 _("---\n" 1129 "To remove '%c' lines, make them ' ' lines " 1130 "(context).\n" 1131 "To remove '%c' lines, delete them.\n" 1132 "Lines starting with %s will be removed.\n"), 1133 s->mode->is_reverse ? '+' : '-', 1134 s->mode->is_reverse ? '-' : '+', 1135 comment_line_str); 1136 strbuf_commented_addf(&s->buf, comment_line_str, "%s", 1137 _(s->mode->edit_hunk_hint)); 1138 /* 1139 * TRANSLATORS: 'it' refers to the patch mentioned in the previous 1140 * messages. 1141 */ 1142 strbuf_commented_addf(&s->buf, comment_line_str, 1143 _("If it does not apply cleanly, you will be " 1144 "given an opportunity to\n" 1145 "edit again. If all lines of the hunk are " 1146 "removed, then the edit is\n" 1147 "aborted and the hunk is left unchanged.\n")); 1148 1149 if (strbuf_edit_interactively(the_repository, &s->buf, 1150 "addp-hunk-edit.diff", NULL) < 0) 1151 return -1; 1152 1153 /* strip out commented lines */ 1154 hunk->start = s->plain.len; 1155 for (i = 0; i < s->buf.len; ) { 1156 size_t next = find_next_line(&s->buf, i); 1157 1158 if (!starts_with(s->buf.buf + i, comment_line_str)) 1159 strbuf_add(&s->plain, s->buf.buf + i, next - i); 1160 i = next; 1161 } 1162 1163 hunk->end = s->plain.len; 1164 if (hunk->end == hunk->start) 1165 /* The user aborted editing by deleting everything */ 1166 return 0; 1167 1168 recolor_hunk(s, hunk); 1169 1170 /* 1171 * If the hunk header is intact, parse it, otherwise simply use the 1172 * hunk header prior to editing (which will adjust `hunk->start` to 1173 * skip the hunk header). 1174 */ 1175 if (s->plain.buf[hunk->start] == '@' && 1176 parse_hunk_header(s, hunk) < 0) 1177 return error(_("could not parse hunk header")); 1178 1179 return 1; 1180} 1181 1182static ssize_t recount_edited_hunk(struct add_p_state *s, struct hunk *hunk, 1183 size_t orig_old_count, size_t orig_new_count) 1184{ 1185 struct hunk_header *header = &hunk->header; 1186 size_t i; 1187 1188 header->old_count = header->new_count = 0; 1189 for (i = hunk->start; i < hunk->end; ) { 1190 switch(normalize_marker(&s->plain.buf[i])) { 1191 case '-': 1192 header->old_count++; 1193 break; 1194 case '+': 1195 header->new_count++; 1196 break; 1197 case ' ': 1198 header->old_count++; 1199 header->new_count++; 1200 break; 1201 } 1202 1203 i = find_next_line(&s->plain, i); 1204 } 1205 1206 return orig_old_count - orig_new_count 1207 - header->old_count + header->new_count; 1208} 1209 1210static int run_apply_check(struct add_p_state *s, 1211 struct file_diff *file_diff) 1212{ 1213 struct child_process cp = CHILD_PROCESS_INIT; 1214 1215 strbuf_reset(&s->buf); 1216 reassemble_patch(s, file_diff, 1, &s->buf); 1217 1218 setup_child_process(s, &cp, 1219 "apply", "--check", NULL); 1220 strvec_pushv(&cp.args, s->mode->apply_check_args); 1221 if (pipe_command(&cp, s->buf.buf, s->buf.len, NULL, 0, NULL, 0)) 1222 return error(_("'git apply --cached' failed")); 1223 1224 return 0; 1225} 1226 1227static int read_single_character(struct add_p_state *s) 1228{ 1229 if (s->s.use_single_key) { 1230 int res = read_key_without_echo(&s->answer); 1231 printf("%s\n", res == EOF ? "" : s->answer.buf); 1232 return res; 1233 } 1234 1235 if (git_read_line_interactively(&s->answer) == EOF) 1236 return EOF; 1237 return 0; 1238} 1239 1240static int prompt_yesno(struct add_p_state *s, const char *prompt) 1241{ 1242 for (;;) { 1243 color_fprintf(stdout, s->s.prompt_color, "%s", _(prompt)); 1244 fflush(stdout); 1245 if (read_single_character(s) == EOF) 1246 return -1; 1247 /* do not limit to 1-byte input to allow 'no' etc. */ 1248 switch (tolower(s->answer.buf[0])) { 1249 case 'n': return 0; 1250 case 'y': return 1; 1251 } 1252 } 1253} 1254 1255static int edit_hunk_loop(struct add_p_state *s, 1256 struct file_diff *file_diff, struct hunk *hunk) 1257{ 1258 size_t plain_len = s->plain.len, colored_len = s->colored.len; 1259 struct hunk backup; 1260 1261 backup = *hunk; 1262 1263 for (;;) { 1264 int res = edit_hunk_manually(s, hunk); 1265 if (res == 0) { 1266 /* abandoned */ 1267 *hunk = backup; 1268 return -1; 1269 } 1270 1271 if (res > 0) { 1272 hunk->delta += 1273 recount_edited_hunk(s, hunk, 1274 backup.header.old_count, 1275 backup.header.new_count); 1276 if (!run_apply_check(s, file_diff)) 1277 return 0; 1278 } 1279 1280 /* Drop edits (they were appended to s->plain) */ 1281 strbuf_setlen(&s->plain, plain_len); 1282 strbuf_setlen(&s->colored, colored_len); 1283 *hunk = backup; 1284 1285 /* 1286 * TRANSLATORS: do not translate [y/n] 1287 * The program will only accept that input at this point. 1288 * Consider translating (saying "no" discards!) as 1289 * (saying "n" for "no" discards!) if the translation 1290 * of the word "no" does not start with n. 1291 */ 1292 res = prompt_yesno(s, _("Your edited hunk does not apply. " 1293 "Edit again (saying \"no\" discards!) " 1294 "[y/n]? ")); 1295 if (res < 1) 1296 return -1; 1297 } 1298} 1299 1300static int apply_for_checkout(struct add_p_state *s, struct strbuf *diff, 1301 int is_reverse) 1302{ 1303 const char *reverse = is_reverse ? "-R" : NULL; 1304 struct child_process check_index = CHILD_PROCESS_INIT; 1305 struct child_process check_worktree = CHILD_PROCESS_INIT; 1306 struct child_process apply_index = CHILD_PROCESS_INIT; 1307 struct child_process apply_worktree = CHILD_PROCESS_INIT; 1308 int applies_index, applies_worktree; 1309 1310 setup_child_process(s, &check_index, 1311 "apply", "--cached", "--check", reverse, NULL); 1312 applies_index = !pipe_command(&check_index, diff->buf, diff->len, 1313 NULL, 0, NULL, 0); 1314 1315 setup_child_process(s, &check_worktree, 1316 "apply", "--check", reverse, NULL); 1317 applies_worktree = !pipe_command(&check_worktree, diff->buf, diff->len, 1318 NULL, 0, NULL, 0); 1319 1320 if (applies_worktree && applies_index) { 1321 setup_child_process(s, &apply_index, 1322 "apply", "--cached", reverse, NULL); 1323 pipe_command(&apply_index, diff->buf, diff->len, 1324 NULL, 0, NULL, 0); 1325 1326 setup_child_process(s, &apply_worktree, 1327 "apply", reverse, NULL); 1328 pipe_command(&apply_worktree, diff->buf, diff->len, 1329 NULL, 0, NULL, 0); 1330 1331 return 1; 1332 } 1333 1334 if (!applies_index) { 1335 err(s, _("The selected hunks do not apply to the index!")); 1336 if (prompt_yesno(s, _("Apply them to the worktree " 1337 "anyway? ")) > 0) { 1338 setup_child_process(s, &apply_worktree, 1339 "apply", reverse, NULL); 1340 return pipe_command(&apply_worktree, diff->buf, 1341 diff->len, NULL, 0, NULL, 0); 1342 } 1343 err(s, _("Nothing was applied.\n")); 1344 } else 1345 /* As a last resort, show the diff to the user */ 1346 fwrite(diff->buf, diff->len, 1, stdout); 1347 1348 return 0; 1349} 1350 1351#define SUMMARY_HEADER_WIDTH 20 1352#define SUMMARY_LINE_WIDTH 80 1353static void summarize_hunk(struct add_p_state *s, struct hunk *hunk, 1354 struct strbuf *out) 1355{ 1356 struct hunk_header *header = &hunk->header; 1357 struct strbuf *plain = &s->plain; 1358 size_t len = out->len, i; 1359 1360 strbuf_addf(out, " -%lu,%lu +%lu,%lu ", 1361 header->old_offset, header->old_count, 1362 header->new_offset, header->new_count); 1363 if (out->len - len < SUMMARY_HEADER_WIDTH) 1364 strbuf_addchars(out, ' ', 1365 SUMMARY_HEADER_WIDTH + len - out->len); 1366 for (i = hunk->start; i < hunk->end; i = find_next_line(plain, i)) 1367 if (plain->buf[i] != ' ') 1368 break; 1369 if (i < hunk->end) 1370 strbuf_add(out, plain->buf + i, find_next_line(plain, i) - i); 1371 if (out->len - len > SUMMARY_LINE_WIDTH) 1372 strbuf_setlen(out, len + SUMMARY_LINE_WIDTH); 1373 strbuf_complete_line(out); 1374} 1375 1376#define DISPLAY_HUNKS_LINES 20 1377static size_t display_hunks(struct add_p_state *s, 1378 struct file_diff *file_diff, size_t start_index) 1379{ 1380 size_t end_index = start_index + DISPLAY_HUNKS_LINES; 1381 1382 if (end_index > file_diff->hunk_nr) 1383 end_index = file_diff->hunk_nr; 1384 1385 while (start_index < end_index) { 1386 struct hunk *hunk = file_diff->hunk + start_index++; 1387 1388 strbuf_reset(&s->buf); 1389 strbuf_addf(&s->buf, "%c%2d: ", hunk->use == USE_HUNK ? '+' 1390 : hunk->use == SKIP_HUNK ? '-' : ' ', 1391 (int)start_index); 1392 summarize_hunk(s, hunk, &s->buf); 1393 fputs(s->buf.buf, stdout); 1394 } 1395 1396 return end_index; 1397} 1398 1399static const char help_patch_remainder[] = 1400N_("j - leave this hunk undecided, see next undecided hunk\n" 1401 "J - leave this hunk undecided, see next hunk\n" 1402 "k - leave this hunk undecided, see previous undecided hunk\n" 1403 "K - leave this hunk undecided, see previous hunk\n" 1404 "g - select a hunk to go to\n" 1405 "/ - search for a hunk matching the given regex\n" 1406 "s - split the current hunk into smaller hunks\n" 1407 "e - manually edit the current hunk\n" 1408 "p - print the current hunk, 'P' to use the pager\n" 1409 "? - print help\n"); 1410 1411static int patch_update_file(struct add_p_state *s, 1412 struct file_diff *file_diff) 1413{ 1414 size_t hunk_index = 0; 1415 ssize_t i, undecided_previous, undecided_next, rendered_hunk_index = -1; 1416 struct hunk *hunk; 1417 char ch; 1418 struct child_process cp = CHILD_PROCESS_INIT; 1419 int colored = !!s->colored.len, quit = 0, use_pager = 0; 1420 enum prompt_mode_type prompt_mode_type; 1421 enum { 1422 ALLOW_GOTO_PREVIOUS_HUNK = 1 << 0, 1423 ALLOW_GOTO_PREVIOUS_UNDECIDED_HUNK = 1 << 1, 1424 ALLOW_GOTO_NEXT_HUNK = 1 << 2, 1425 ALLOW_GOTO_NEXT_UNDECIDED_HUNK = 1 << 3, 1426 ALLOW_SEARCH_AND_GOTO = 1 << 4, 1427 ALLOW_SPLIT = 1 << 5, 1428 ALLOW_EDIT = 1 << 6 1429 } permitted = 0; 1430 1431 /* Empty added files have no hunks */ 1432 if (!file_diff->hunk_nr && !file_diff->added) 1433 return 0; 1434 1435 strbuf_reset(&s->buf); 1436 render_diff_header(s, file_diff, colored, &s->buf); 1437 fputs(s->buf.buf, stdout); 1438 for (;;) { 1439 if (hunk_index >= file_diff->hunk_nr) 1440 hunk_index = 0; 1441 hunk = file_diff->hunk_nr 1442 ? file_diff->hunk + hunk_index 1443 : &file_diff->head; 1444 undecided_previous = -1; 1445 undecided_next = -1; 1446 1447 if (file_diff->hunk_nr) { 1448 for (i = hunk_index - 1; i >= 0; i--) 1449 if (file_diff->hunk[i].use == UNDECIDED_HUNK) { 1450 undecided_previous = i; 1451 break; 1452 } 1453 1454 for (i = hunk_index + 1; i < file_diff->hunk_nr; i++) 1455 if (file_diff->hunk[i].use == UNDECIDED_HUNK) { 1456 undecided_next = i; 1457 break; 1458 } 1459 } 1460 1461 /* Everything decided? */ 1462 if (undecided_previous < 0 && undecided_next < 0 && 1463 hunk->use != UNDECIDED_HUNK) 1464 break; 1465 1466 strbuf_reset(&s->buf); 1467 if (file_diff->hunk_nr) { 1468 if (rendered_hunk_index != hunk_index) { 1469 if (use_pager) { 1470 setup_pager(the_repository); 1471 sigchain_push(SIGPIPE, SIG_IGN); 1472 } 1473 render_hunk(s, hunk, 0, colored, &s->buf); 1474 fputs(s->buf.buf, stdout); 1475 rendered_hunk_index = hunk_index; 1476 if (use_pager) { 1477 sigchain_pop(SIGPIPE); 1478 wait_for_pager(); 1479 use_pager = 0; 1480 } 1481 } 1482 1483 strbuf_reset(&s->buf); 1484 if (undecided_previous >= 0) { 1485 permitted |= ALLOW_GOTO_PREVIOUS_UNDECIDED_HUNK; 1486 strbuf_addstr(&s->buf, ",k"); 1487 } 1488 if (hunk_index) { 1489 permitted |= ALLOW_GOTO_PREVIOUS_HUNK; 1490 strbuf_addstr(&s->buf, ",K"); 1491 } 1492 if (undecided_next >= 0) { 1493 permitted |= ALLOW_GOTO_NEXT_UNDECIDED_HUNK; 1494 strbuf_addstr(&s->buf, ",j"); 1495 } 1496 if (hunk_index + 1 < file_diff->hunk_nr) { 1497 permitted |= ALLOW_GOTO_NEXT_HUNK; 1498 strbuf_addstr(&s->buf, ",J"); 1499 } 1500 if (file_diff->hunk_nr > 1) { 1501 permitted |= ALLOW_SEARCH_AND_GOTO; 1502 strbuf_addstr(&s->buf, ",g,/"); 1503 } 1504 if (hunk->splittable_into > 1) { 1505 permitted |= ALLOW_SPLIT; 1506 strbuf_addstr(&s->buf, ",s"); 1507 } 1508 if (hunk_index + 1 > file_diff->mode_change && 1509 !file_diff->deleted) { 1510 permitted |= ALLOW_EDIT; 1511 strbuf_addstr(&s->buf, ",e"); 1512 } 1513 strbuf_addstr(&s->buf, ",p"); 1514 } 1515 if (file_diff->deleted) 1516 prompt_mode_type = PROMPT_DELETION; 1517 else if (file_diff->added) 1518 prompt_mode_type = PROMPT_ADDITION; 1519 else if (file_diff->mode_change && !hunk_index) 1520 prompt_mode_type = PROMPT_MODE_CHANGE; 1521 else 1522 prompt_mode_type = PROMPT_HUNK; 1523 1524 printf("%s(%"PRIuMAX"/%"PRIuMAX") ", s->s.prompt_color, 1525 (uintmax_t)hunk_index + 1, 1526 (uintmax_t)(file_diff->hunk_nr 1527 ? file_diff->hunk_nr 1528 : 1)); 1529 printf(_(s->mode->prompt_mode[prompt_mode_type]), 1530 s->buf.buf); 1531 if (*s->s.reset_color) 1532 fputs(s->s.reset_color, stdout); 1533 fflush(stdout); 1534 if (read_single_character(s) == EOF) 1535 break; 1536 1537 if (!s->answer.len) 1538 continue; 1539 ch = tolower(s->answer.buf[0]); 1540 1541 /* 'g' takes a hunk number and '/' takes a regexp */ 1542 if (s->answer.len != 1 && (ch != 'g' && ch != '/')) { 1543 err(s, _("Only one letter is expected, got '%s'"), s->answer.buf); 1544 continue; 1545 } 1546 if (ch == 'y') { 1547 hunk->use = USE_HUNK; 1548soft_increment: 1549 hunk_index = undecided_next < 0 ? 1550 file_diff->hunk_nr : undecided_next; 1551 } else if (ch == 'n') { 1552 hunk->use = SKIP_HUNK; 1553 goto soft_increment; 1554 } else if (ch == 'a') { 1555 if (file_diff->hunk_nr) { 1556 for (; hunk_index < file_diff->hunk_nr; hunk_index++) { 1557 hunk = file_diff->hunk + hunk_index; 1558 if (hunk->use == UNDECIDED_HUNK) 1559 hunk->use = USE_HUNK; 1560 } 1561 } else if (hunk->use == UNDECIDED_HUNK) { 1562 hunk->use = USE_HUNK; 1563 } 1564 } else if (ch == 'd' || ch == 'q') { 1565 if (file_diff->hunk_nr) { 1566 for (; hunk_index < file_diff->hunk_nr; hunk_index++) { 1567 hunk = file_diff->hunk + hunk_index; 1568 if (hunk->use == UNDECIDED_HUNK) 1569 hunk->use = SKIP_HUNK; 1570 } 1571 } else if (hunk->use == UNDECIDED_HUNK) { 1572 hunk->use = SKIP_HUNK; 1573 } 1574 if (ch == 'q') { 1575 quit = 1; 1576 break; 1577 } 1578 } else if (s->answer.buf[0] == 'K') { 1579 if (permitted & ALLOW_GOTO_PREVIOUS_HUNK) 1580 hunk_index--; 1581 else 1582 err(s, _("No previous hunk")); 1583 } else if (s->answer.buf[0] == 'J') { 1584 if (permitted & ALLOW_GOTO_NEXT_HUNK) 1585 hunk_index++; 1586 else 1587 err(s, _("No next hunk")); 1588 } else if (s->answer.buf[0] == 'k') { 1589 if (permitted & ALLOW_GOTO_PREVIOUS_UNDECIDED_HUNK) 1590 hunk_index = undecided_previous; 1591 else 1592 err(s, _("No previous hunk")); 1593 } else if (s->answer.buf[0] == 'j') { 1594 if (permitted & ALLOW_GOTO_NEXT_UNDECIDED_HUNK) 1595 hunk_index = undecided_next; 1596 else 1597 err(s, _("No next hunk")); 1598 } else if (s->answer.buf[0] == 'g') { 1599 char *pend; 1600 unsigned long response; 1601 1602 if (!(permitted & ALLOW_SEARCH_AND_GOTO)) { 1603 err(s, _("No other hunks to goto")); 1604 continue; 1605 } 1606 strbuf_remove(&s->answer, 0, 1); 1607 strbuf_trim(&s->answer); 1608 i = hunk_index - DISPLAY_HUNKS_LINES / 2; 1609 if (i < (int)file_diff->mode_change) 1610 i = file_diff->mode_change; 1611 while (s->answer.len == 0) { 1612 i = display_hunks(s, file_diff, i); 1613 printf("%s", i < file_diff->hunk_nr ? 1614 _("go to which hunk (<ret> to see " 1615 "more)? ") : _("go to which hunk? ")); 1616 fflush(stdout); 1617 if (strbuf_getline(&s->answer, 1618 stdin) == EOF) 1619 break; 1620 strbuf_trim_trailing_newline(&s->answer); 1621 } 1622 1623 strbuf_trim(&s->answer); 1624 response = strtoul(s->answer.buf, &pend, 10); 1625 if (*pend || pend == s->answer.buf) 1626 err(s, _("Invalid number: '%s'"), 1627 s->answer.buf); 1628 else if (0 < response && response <= file_diff->hunk_nr) 1629 hunk_index = response - 1; 1630 else 1631 err(s, Q_("Sorry, only %d hunk available.", 1632 "Sorry, only %d hunks available.", 1633 file_diff->hunk_nr), 1634 (int)file_diff->hunk_nr); 1635 } else if (s->answer.buf[0] == '/') { 1636 regex_t regex; 1637 int ret; 1638 1639 if (!(permitted & ALLOW_SEARCH_AND_GOTO)) { 1640 err(s, _("No other hunks to search")); 1641 continue; 1642 } 1643 strbuf_remove(&s->answer, 0, 1); 1644 strbuf_trim_trailing_newline(&s->answer); 1645 if (s->answer.len == 0) { 1646 printf("%s", _("search for regex? ")); 1647 fflush(stdout); 1648 if (strbuf_getline(&s->answer, 1649 stdin) == EOF) 1650 break; 1651 strbuf_trim_trailing_newline(&s->answer); 1652 if (s->answer.len == 0) 1653 continue; 1654 } 1655 ret = regcomp(&regex, s->answer.buf, 1656 REG_EXTENDED | REG_NOSUB | REG_NEWLINE); 1657 if (ret) { 1658 char errbuf[1024]; 1659 1660 regerror(ret, &regex, errbuf, sizeof(errbuf)); 1661 err(s, _("Malformed search regexp %s: %s"), 1662 s->answer.buf, errbuf); 1663 continue; 1664 } 1665 i = hunk_index; 1666 for (;;) { 1667 /* render the hunk into a scratch buffer */ 1668 render_hunk(s, file_diff->hunk + i, 0, 0, 1669 &s->buf); 1670 if (regexec(&regex, s->buf.buf, 0, NULL, 0) 1671 != REG_NOMATCH) 1672 break; 1673 i++; 1674 if (i == file_diff->hunk_nr) 1675 i = 0; 1676 if (i != hunk_index) 1677 continue; 1678 err(s, _("No hunk matches the given pattern")); 1679 break; 1680 } 1681 regfree(&regex); 1682 hunk_index = i; 1683 } else if (s->answer.buf[0] == 's') { 1684 size_t splittable_into = hunk->splittable_into; 1685 if (!(permitted & ALLOW_SPLIT)) { 1686 err(s, _("Sorry, cannot split this hunk")); 1687 } else if (!split_hunk(s, file_diff, 1688 hunk - file_diff->hunk)) { 1689 color_fprintf_ln(stdout, s->s.header_color, 1690 _("Split into %d hunks."), 1691 (int)splittable_into); 1692 rendered_hunk_index = -1; 1693 } 1694 } else if (s->answer.buf[0] == 'e') { 1695 if (!(permitted & ALLOW_EDIT)) 1696 err(s, _("Sorry, cannot edit this hunk")); 1697 else if (edit_hunk_loop(s, file_diff, hunk) >= 0) { 1698 hunk->use = USE_HUNK; 1699 goto soft_increment; 1700 } 1701 } else if (ch == 'p') { 1702 rendered_hunk_index = -1; 1703 use_pager = (s->answer.buf[0] == 'P') ? 1 : 0; 1704 } else if (s->answer.buf[0] == '?') { 1705 const char *p = _(help_patch_remainder), *eol = p; 1706 1707 color_fprintf(stdout, s->s.help_color, "%s", 1708 _(s->mode->help_patch_text)); 1709 1710 /* 1711 * Show only those lines of the remainder that are 1712 * actually applicable with the current hunk. 1713 */ 1714 for (; *p; p = eol + (*eol == '\n')) { 1715 eol = strchrnul(p, '\n'); 1716 1717 /* 1718 * `s->buf` still contains the part of the 1719 * commands shown in the prompt that are not 1720 * always available. 1721 */ 1722 if (*p != '?' && !strchr(s->buf.buf, *p)) 1723 continue; 1724 1725 color_fprintf_ln(stdout, s->s.help_color, 1726 "%.*s", (int)(eol - p), p); 1727 } 1728 } else { 1729 err(s, _("Unknown command '%s' (use '?' for help)"), 1730 s->answer.buf); 1731 } 1732 } 1733 1734 /* Any hunk to be used? */ 1735 for (i = 0; i < file_diff->hunk_nr; i++) 1736 if (file_diff->hunk[i].use == USE_HUNK) 1737 break; 1738 1739 if (i < file_diff->hunk_nr || 1740 (!file_diff->hunk_nr && file_diff->head.use == USE_HUNK)) { 1741 /* At least one hunk selected: apply */ 1742 strbuf_reset(&s->buf); 1743 reassemble_patch(s, file_diff, 0, &s->buf); 1744 1745 discard_index(s->s.r->index); 1746 if (s->mode->apply_for_checkout) 1747 apply_for_checkout(s, &s->buf, 1748 s->mode->is_reverse); 1749 else { 1750 setup_child_process(s, &cp, "apply", NULL); 1751 strvec_pushv(&cp.args, s->mode->apply_args); 1752 if (pipe_command(&cp, s->buf.buf, s->buf.len, 1753 NULL, 0, NULL, 0)) 1754 error(_("'git apply' failed")); 1755 } 1756 if (repo_read_index(s->s.r) >= 0) 1757 repo_refresh_and_write_index(s->s.r, REFRESH_QUIET, 0, 1758 1, NULL, NULL, NULL); 1759 } 1760 1761 putchar('\n'); 1762 return quit; 1763} 1764 1765int run_add_p(struct repository *r, enum add_p_mode mode, 1766 struct add_p_opt *o, const char *revision, 1767 const struct pathspec *ps) 1768{ 1769 struct add_p_state s = { 1770 { r }, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT 1771 }; 1772 size_t i, binary_count = 0; 1773 1774 init_add_i_state(&s.s, r, o); 1775 1776 if (mode == ADD_P_STASH) 1777 s.mode = &patch_mode_stash; 1778 else if (mode == ADD_P_RESET) { 1779 if (!revision || !strcmp(revision, "HEAD")) 1780 s.mode = &patch_mode_reset_head; 1781 else 1782 s.mode = &patch_mode_reset_nothead; 1783 } else if (mode == ADD_P_CHECKOUT) { 1784 if (!revision) 1785 s.mode = &patch_mode_checkout_index; 1786 else if (!strcmp(revision, "HEAD")) 1787 s.mode = &patch_mode_checkout_head; 1788 else 1789 s.mode = &patch_mode_checkout_nothead; 1790 } else if (mode == ADD_P_WORKTREE) { 1791 if (!revision) 1792 s.mode = &patch_mode_checkout_index; 1793 else if (!strcmp(revision, "HEAD")) 1794 s.mode = &patch_mode_worktree_head; 1795 else 1796 s.mode = &patch_mode_worktree_nothead; 1797 } else 1798 s.mode = &patch_mode_add; 1799 s.revision = revision; 1800 1801 discard_index(r->index); 1802 if (repo_read_index(r) < 0 || 1803 (!s.mode->index_only && 1804 repo_refresh_and_write_index(r, REFRESH_QUIET, 0, 1, 1805 NULL, NULL, NULL) < 0) || 1806 parse_diff(&s, ps) < 0) { 1807 add_p_state_clear(&s); 1808 return -1; 1809 } 1810 1811 for (i = 0; i < s.file_diff_nr; i++) 1812 if (s.file_diff[i].binary && !s.file_diff[i].hunk_nr) 1813 binary_count++; 1814 else if (patch_update_file(&s, s.file_diff + i)) 1815 break; 1816 1817 if (s.file_diff_nr == 0) 1818 err(&s, _("No changes.")); 1819 else if (binary_count == s.file_diff_nr) 1820 err(&s, _("Only binary files changed.")); 1821 1822 add_p_state_clear(&s); 1823 return 0; 1824}