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