| 1433 | } |
| 1434 | |
| 1435 | static int apply_for_checkout(struct add_p_state *s, struct strbuf *diff, |
| 1436 | int is_reverse) |
| 1437 | { |
| 1438 | const char *reverse = is_reverse ? "-R" : NULL; |
| 1439 | struct child_process check_index = CHILD_PROCESS_INIT; |
| 1440 | struct child_process check_worktree = CHILD_PROCESS_INIT; |
| 1441 | struct child_process apply_index = CHILD_PROCESS_INIT; |
| 1442 | struct child_process apply_worktree = CHILD_PROCESS_INIT; |
| 1443 | int applies_index, applies_worktree; |
| 1444 | |
| 1445 | setup_child_process(s, &check_index, |
| 1446 | "apply", "--cached", "--check", reverse, NULL); |
| 1447 | applies_index = !pipe_command(&check_index, diff->buf, diff->len, |
| 1448 | NULL, 0, NULL, 0); |
| 1449 | |
| 1450 | setup_child_process(s, &check_worktree, |
| 1451 | "apply", "--check", reverse, NULL); |
| 1452 | applies_worktree = !pipe_command(&check_worktree, diff->buf, diff->len, |
| 1453 | NULL, 0, NULL, 0); |
| 1454 | |
| 1455 | if (applies_worktree && applies_index) { |
| 1456 | setup_child_process(s, &apply_index, |
| 1457 | "apply", "--cached", reverse, NULL); |
| 1458 | pipe_command(&apply_index, diff->buf, diff->len, |
| 1459 | NULL, 0, NULL, 0); |
| 1460 | |
| 1461 | setup_child_process(s, &apply_worktree, |
| 1462 | "apply", reverse, NULL); |
| 1463 | pipe_command(&apply_worktree, diff->buf, diff->len, |
| 1464 | NULL, 0, NULL, 0); |
| 1465 | |
| 1466 | return 1; |
| 1467 | } |
| 1468 | |
| 1469 | if (!applies_index) { |
| 1470 | err(s, _("The selected hunks do not apply to the index!")); |
| 1471 | if (prompt_yesno(s, _("Apply them to the worktree " |
| 1472 | "anyway? ")) > 0) { |
| 1473 | setup_child_process(s, &apply_worktree, |
| 1474 | "apply", reverse, NULL); |
| 1475 | return pipe_command(&apply_worktree, diff->buf, |
| 1476 | diff->len, NULL, 0, NULL, 0); |
| 1477 | } |
| 1478 | err(s, _("Nothing was applied.\n")); |
| 1479 | } else |
| 1480 | /* As a last resort, show the diff to the user */ |
| 1481 | fwrite(diff->buf, diff->len, 1, stdout); |
| 1482 | |
| 1483 | return 0; |
| 1484 | } |
| 1485 | |
| 1486 | #define SUMMARY_HEADER_WIDTH 20 |
| 1487 | #define SUMMARY_LINE_WIDTH 80 |
no test coverage detected