]>
Commit | Line | Data |
---|---|---|
26ae337b | 1 | #include "cache.h" |
b2141fc1 | 2 | #include "config.h" |
697cc8ef | 3 | #include "lockfile.h" |
26ae337b | 4 | #include "dir.h" |
cbd53a21 | 5 | #include "object-store.h" |
043a4492 RR |
6 | #include "object.h" |
7 | #include "commit.h" | |
0505d604 | 8 | #include "sequencer.h" |
043a4492 RR |
9 | #include "tag.h" |
10 | #include "run-command.h" | |
d807c4a0 | 11 | #include "exec-cmd.h" |
043a4492 RR |
12 | #include "utf8.h" |
13 | #include "cache-tree.h" | |
14 | #include "diff.h" | |
15 | #include "revision.h" | |
16 | #include "rerere.h" | |
14c4586c EN |
17 | #include "merge-ort.h" |
18 | #include "merge-ort-wrappers.h" | |
043a4492 | 19 | #include "refs.h" |
dbbcd44f | 20 | #include "strvec.h" |
a1c75762 | 21 | #include "quote.h" |
967dfd4d | 22 | #include "trailer.h" |
56dc3ab0 | 23 | #include "log-tree.h" |
311af526 | 24 | #include "wt-status.h" |
c44a4c65 | 25 | #include "hashmap.h" |
a87a6f3c PW |
26 | #include "notes-utils.h" |
27 | #include "sigchain.h" | |
9055e401 JS |
28 | #include "unpack-trees.h" |
29 | #include "worktree.h" | |
1644c73c JS |
30 | #include "oidmap.h" |
31 | #include "oidset.h" | |
8315bd20 | 32 | #include "commit-slab.h" |
65b5f948 | 33 | #include "alias.h" |
64043556 | 34 | #include "commit-reach.h" |
b97e1873 | 35 | #include "rebase-interactive.h" |
0816f1df | 36 | #include "reset.h" |
043a4492 RR |
37 | |
38 | #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION" | |
26ae337b | 39 | |
5fe81438 | 40 | static const char sign_off_header[] = "Signed-off-by: "; |
cd650a4e | 41 | static const char cherry_picked_prefix[] = "(cherry picked from commit "; |
5ed75e2a | 42 | |
66618a50 PW |
43 | GIT_PATH_FUNC(git_path_commit_editmsg, "COMMIT_EDITMSG") |
44 | ||
901ba7b1 | 45 | static GIT_PATH_FUNC(git_path_seq_dir, "sequencer") |
8a2a0f53 JS |
46 | |
47 | static GIT_PATH_FUNC(git_path_todo_file, "sequencer/todo") | |
48 | static GIT_PATH_FUNC(git_path_opts_file, "sequencer/opts") | |
49 | static GIT_PATH_FUNC(git_path_head_file, "sequencer/head") | |
1e41229d | 50 | static GIT_PATH_FUNC(git_path_abort_safety_file, "sequencer/abort-safety") |
f932729c | 51 | |
84583957 JS |
52 | static GIT_PATH_FUNC(rebase_path, "rebase-merge") |
53 | /* | |
54 | * The file containing rebase commands, comments, and empty lines. | |
55 | * This file is created by "git rebase -i" then edited by the user. As | |
56 | * the lines are processed, they are removed from the front of this | |
57 | * file and written to the tail of 'done'. | |
58 | */ | |
44b776c3 | 59 | GIT_PATH_FUNC(rebase_path_todo, "rebase-merge/git-rebase-todo") |
a930eb03 | 60 | GIT_PATH_FUNC(rebase_path_todo_backup, "rebase-merge/git-rebase-todo.backup") |
b97e1873 | 61 | |
5a5445d8 AG |
62 | GIT_PATH_FUNC(rebase_path_dropped, "rebase-merge/dropped") |
63 | ||
1df6df0c JS |
64 | /* |
65 | * The rebase command lines that have already been processed. A line | |
66 | * is moved here when it is first handled, before any associated user | |
67 | * actions. | |
68 | */ | |
69 | static GIT_PATH_FUNC(rebase_path_done, "rebase-merge/done") | |
ef80069a JS |
70 | /* |
71 | * The file to keep track of how many commands were already processed (e.g. | |
72 | * for the prompt). | |
73 | */ | |
9ad36356 | 74 | static GIT_PATH_FUNC(rebase_path_msgnum, "rebase-merge/msgnum") |
ef80069a JS |
75 | /* |
76 | * The file to keep track of how many commands are to be processed in total | |
77 | * (e.g. for the prompt). | |
78 | */ | |
9ad36356 | 79 | static GIT_PATH_FUNC(rebase_path_msgtotal, "rebase-merge/end") |
6e98de72 JS |
80 | /* |
81 | * The commit message that is planned to be used for any changes that | |
82 | * need to be committed following a user interaction. | |
83 | */ | |
84 | static GIT_PATH_FUNC(rebase_path_message, "rebase-merge/message") | |
85 | /* | |
86 | * The file into which is accumulated the suggested commit message for | |
87 | * squash/fixup commands. When the first of a series of squash/fixups | |
88 | * is seen, the file is created and the commit message from the | |
89 | * previous commit and from the first squash/fixup commit are written | |
90 | * to it. The commit message for each subsequent squash/fixup commit | |
91 | * is appended to the file as it is processed. | |
6e98de72 JS |
92 | */ |
93 | static GIT_PATH_FUNC(rebase_path_squash_msg, "rebase-merge/message-squash") | |
94 | /* | |
95 | * If the current series of squash/fixups has not yet included a squash | |
96 | * command, then this file exists and holds the commit message of the | |
97 | * original "pick" commit. (If the series ends without a "squash" | |
98 | * command, then this can be used as the commit message of the combined | |
99 | * commit without opening the editor.) | |
100 | */ | |
101 | static GIT_PATH_FUNC(rebase_path_fixup_msg, "rebase-merge/message-fixup") | |
e12a7ef5 JS |
102 | /* |
103 | * This file contains the list fixup/squash commands that have been | |
104 | * accumulated into message-fixup or message-squash so far. | |
105 | */ | |
106 | static GIT_PATH_FUNC(rebase_path_current_fixups, "rebase-merge/current-fixups") | |
b5a67045 JS |
107 | /* |
108 | * A script to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and | |
109 | * GIT_AUTHOR_DATE that will be used for the commit that is currently | |
110 | * being rebased. | |
111 | */ | |
112 | static GIT_PATH_FUNC(rebase_path_author_script, "rebase-merge/author-script") | |
56dc3ab0 JS |
113 | /* |
114 | * When an "edit" rebase command is being processed, the SHA1 of the | |
115 | * commit to be edited is recorded in this file. When "git rebase | |
116 | * --continue" is executed, if there are any staged changes then they | |
117 | * will be amended to the HEAD commit, but only provided the HEAD | |
118 | * commit is still the commit to be edited. When any other rebase | |
119 | * command is processed, this file is deleted. | |
120 | */ | |
121 | static GIT_PATH_FUNC(rebase_path_amend, "rebase-merge/amend") | |
122 | /* | |
123 | * When we stop at a given patch via the "edit" command, this file contains | |
0512eabd | 124 | * the commit object name of the corresponding patch. |
56dc3ab0 JS |
125 | */ |
126 | static GIT_PATH_FUNC(rebase_path_stopped_sha, "rebase-merge/stopped-sha") | |
25cb8df9 JS |
127 | /* |
128 | * For the post-rewrite hook, we make a list of rewritten commits and | |
129 | * their new sha1s. The rewritten-pending list keeps the sha1s of | |
130 | * commits that have been processed, but not committed yet, | |
131 | * e.g. because they are waiting for a 'squash' command. | |
132 | */ | |
133 | static GIT_PATH_FUNC(rebase_path_rewritten_list, "rebase-merge/rewritten-list") | |
134 | static GIT_PATH_FUNC(rebase_path_rewritten_pending, | |
135 | "rebase-merge/rewritten-pending") | |
9055e401 | 136 | |
d87d48b2 | 137 | /* |
15beaaa3 | 138 | * The path of the file containing the OID of the "squash onto" commit, i.e. |
d87d48b2 JS |
139 | * the dummy commit used for `reset [new root]`. |
140 | */ | |
141 | static GIT_PATH_FUNC(rebase_path_squash_onto, "rebase-merge/squash-onto") | |
142 | ||
9055e401 JS |
143 | /* |
144 | * The path of the file listing refs that need to be deleted after the rebase | |
145 | * finishes. This is used by the `label` command to record the need for cleanup. | |
146 | */ | |
147 | static GIT_PATH_FUNC(rebase_path_refs_to_delete, "rebase-merge/refs-to-delete") | |
148 | ||
a1c75762 JS |
149 | /* |
150 | * The following files are written by git-rebase just after parsing the | |
65850686 | 151 | * command-line. |
a1c75762 JS |
152 | */ |
153 | static GIT_PATH_FUNC(rebase_path_gpg_sign_opt, "rebase-merge/gpg_sign_opt") | |
7573cec5 | 154 | static GIT_PATH_FUNC(rebase_path_cdate_is_adate, "rebase-merge/cdate_is_adate") |
a3894aad | 155 | static GIT_PATH_FUNC(rebase_path_ignore_date, "rebase-merge/ignore_date") |
556907f1 JS |
156 | static GIT_PATH_FUNC(rebase_path_orig_head, "rebase-merge/orig-head") |
157 | static GIT_PATH_FUNC(rebase_path_verbose, "rebase-merge/verbose") | |
899b49c4 | 158 | static GIT_PATH_FUNC(rebase_path_quiet, "rebase-merge/quiet") |
a852ec7f | 159 | static GIT_PATH_FUNC(rebase_path_signoff, "rebase-merge/signoff") |
4b83ce9f JS |
160 | static GIT_PATH_FUNC(rebase_path_head_name, "rebase-merge/head-name") |
161 | static GIT_PATH_FUNC(rebase_path_onto, "rebase-merge/onto") | |
796c7972 | 162 | static GIT_PATH_FUNC(rebase_path_autostash, "rebase-merge/autostash") |
ca6c6b45 JS |
163 | static GIT_PATH_FUNC(rebase_path_strategy, "rebase-merge/strategy") |
164 | static GIT_PATH_FUNC(rebase_path_strategy_opts, "rebase-merge/strategy_opts") | |
9b6d7a62 | 165 | static GIT_PATH_FUNC(rebase_path_allow_rerere_autoupdate, "rebase-merge/allow_rerere_autoupdate") |
d421afa0 | 166 | static GIT_PATH_FUNC(rebase_path_reschedule_failed_exec, "rebase-merge/reschedule-failed-exec") |
e98c4269 EN |
167 | static GIT_PATH_FUNC(rebase_path_drop_redundant_commits, "rebase-merge/drop_redundant_commits") |
168 | static GIT_PATH_FUNC(rebase_path_keep_redundant_commits, "rebase-merge/keep_redundant_commits") | |
b5a67045 | 169 | |
28d6daed PW |
170 | static int git_sequencer_config(const char *k, const char *v, void *cb) |
171 | { | |
172 | struct replay_opts *opts = cb; | |
173 | int status; | |
174 | ||
175 | if (!strcmp(k, "commit.cleanup")) { | |
176 | const char *s; | |
177 | ||
178 | status = git_config_string(&s, k, v); | |
179 | if (status) | |
180 | return status; | |
181 | ||
d74f3e58 | 182 | if (!strcmp(s, "verbatim")) { |
28d6daed | 183 | opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_NONE; |
d74f3e58 PW |
184 | opts->explicit_cleanup = 1; |
185 | } else if (!strcmp(s, "whitespace")) { | |
28d6daed | 186 | opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_SPACE; |
d74f3e58 PW |
187 | opts->explicit_cleanup = 1; |
188 | } else if (!strcmp(s, "strip")) { | |
28d6daed | 189 | opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_ALL; |
d74f3e58 PW |
190 | opts->explicit_cleanup = 1; |
191 | } else if (!strcmp(s, "scissors")) { | |
1a2b985f | 192 | opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_SCISSORS; |
d74f3e58 PW |
193 | opts->explicit_cleanup = 1; |
194 | } else { | |
28d6daed PW |
195 | warning(_("invalid commit message cleanup mode '%s'"), |
196 | s); | |
d74f3e58 | 197 | } |
28d6daed | 198 | |
f40f3c16 | 199 | free((char *)s); |
28d6daed PW |
200 | return status; |
201 | } | |
202 | ||
203 | if (!strcmp(k, "commit.gpgsign")) { | |
ed1e5282 | 204 | opts->gpg_sign = git_config_bool(k, v) ? xstrdup("") : NULL; |
28d6daed PW |
205 | return 0; |
206 | } | |
207 | ||
14c4586c EN |
208 | if (!opts->default_strategy && !strcmp(k, "pull.twohead")) { |
209 | int ret = git_config_string((const char**)&opts->default_strategy, k, v); | |
210 | if (ret == 0) { | |
211 | /* | |
212 | * pull.twohead is allowed to be multi-valued; we only | |
213 | * care about the first value. | |
214 | */ | |
215 | char *tmp = strchr(opts->default_strategy, ' '); | |
216 | if (tmp) | |
217 | *tmp = '\0'; | |
218 | } | |
219 | return ret; | |
220 | } | |
221 | ||
28d6daed PW |
222 | status = git_gpg_config(k, v, NULL); |
223 | if (status) | |
224 | return status; | |
225 | ||
226 | return git_diff_basic_config(k, v, NULL); | |
227 | } | |
228 | ||
229 | void sequencer_init_config(struct replay_opts *opts) | |
230 | { | |
231 | opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_NONE; | |
232 | git_config(git_sequencer_config, opts); | |
233 | } | |
234 | ||
b5a67045 JS |
235 | static inline int is_rebase_i(const struct replay_opts *opts) |
236 | { | |
84583957 | 237 | return opts->action == REPLAY_INTERACTIVE_REBASE; |
b5a67045 JS |
238 | } |
239 | ||
285abf56 JS |
240 | static const char *get_dir(const struct replay_opts *opts) |
241 | { | |
84583957 JS |
242 | if (is_rebase_i(opts)) |
243 | return rebase_path(); | |
285abf56 JS |
244 | return git_path_seq_dir(); |
245 | } | |
246 | ||
c0246501 JS |
247 | static const char *get_todo_path(const struct replay_opts *opts) |
248 | { | |
84583957 JS |
249 | if (is_rebase_i(opts)) |
250 | return rebase_path_todo(); | |
c0246501 JS |
251 | return git_path_todo_file(); |
252 | } | |
253 | ||
bab4d109 BC |
254 | /* |
255 | * Returns 0 for non-conforming footer | |
256 | * Returns 1 for conforming footer | |
257 | * Returns 2 when sob exists within conforming footer | |
258 | * Returns 3 when sob exists within conforming footer as last entry | |
259 | */ | |
260 | static int has_conforming_footer(struct strbuf *sb, struct strbuf *sob, | |
66e83d9b | 261 | size_t ignore_footer) |
b971e04f | 262 | { |
00a21f5c | 263 | struct process_trailer_options opts = PROCESS_TRAILER_OPTIONS_INIT; |
967dfd4d | 264 | struct trailer_info info; |
a3b636e2 | 265 | size_t i; |
967dfd4d | 266 | int found_sob = 0, found_sob_last = 0; |
9dad073d | 267 | char saved_char; |
b971e04f | 268 | |
ffce7f59 JK |
269 | opts.no_divider = 1; |
270 | ||
9dad073d JK |
271 | if (ignore_footer) { |
272 | saved_char = sb->buf[sb->len - ignore_footer]; | |
273 | sb->buf[sb->len - ignore_footer] = '\0'; | |
274 | } | |
275 | ||
00a21f5c | 276 | trailer_info_get(&info, sb->buf, &opts); |
b971e04f | 277 | |
9dad073d JK |
278 | if (ignore_footer) |
279 | sb->buf[sb->len - ignore_footer] = saved_char; | |
280 | ||
967dfd4d | 281 | if (info.trailer_start == info.trailer_end) |
b971e04f BC |
282 | return 0; |
283 | ||
967dfd4d JT |
284 | for (i = 0; i < info.trailer_nr; i++) |
285 | if (sob && !strncmp(info.trailers[i], sob->buf, sob->len)) { | |
286 | found_sob = 1; | |
287 | if (i == info.trailer_nr - 1) | |
288 | found_sob_last = 1; | |
289 | } | |
b971e04f | 290 | |
967dfd4d | 291 | trailer_info_release(&info); |
bab4d109 | 292 | |
967dfd4d | 293 | if (found_sob_last) |
bab4d109 BC |
294 | return 3; |
295 | if (found_sob) | |
296 | return 2; | |
b971e04f BC |
297 | return 1; |
298 | } | |
5ed75e2a | 299 | |
a1c75762 JS |
300 | static const char *gpg_sign_opt_quoted(struct replay_opts *opts) |
301 | { | |
302 | static struct strbuf buf = STRBUF_INIT; | |
303 | ||
304 | strbuf_reset(&buf); | |
305 | if (opts->gpg_sign) | |
306 | sq_quotef(&buf, "-S%s", opts->gpg_sign); | |
307 | return buf.buf; | |
308 | } | |
309 | ||
2863584f | 310 | int sequencer_remove_state(struct replay_opts *opts) |
26ae337b | 311 | { |
9055e401 | 312 | struct strbuf buf = STRBUF_INIT; |
37e9ee5c | 313 | int i, ret = 0; |
03a4e260 | 314 | |
9055e401 JS |
315 | if (is_rebase_i(opts) && |
316 | strbuf_read_file(&buf, rebase_path_refs_to_delete(), 0) > 0) { | |
317 | char *p = buf.buf; | |
318 | while (*p) { | |
319 | char *eol = strchr(p, '\n'); | |
320 | if (eol) | |
321 | *eol = '\0'; | |
c2417d3a | 322 | if (delete_ref("(rebase) cleanup", p, NULL, 0) < 0) { |
9055e401 | 323 | warning(_("could not delete '%s'"), p); |
37e9ee5c PW |
324 | ret = -1; |
325 | } | |
9055e401 JS |
326 | if (!eol) |
327 | break; | |
328 | p = eol + 1; | |
329 | } | |
330 | } | |
331 | ||
03a4e260 | 332 | free(opts->gpg_sign); |
14c4586c | 333 | free(opts->default_strategy); |
03a4e260 JS |
334 | free(opts->strategy); |
335 | for (i = 0; i < opts->xopts_nr; i++) | |
336 | free(opts->xopts[i]); | |
337 | free(opts->xopts); | |
e12a7ef5 | 338 | strbuf_release(&opts->current_fixups); |
26ae337b | 339 | |
9055e401 JS |
340 | strbuf_reset(&buf); |
341 | strbuf_addstr(&buf, get_dir(opts)); | |
37e9ee5c PW |
342 | if (remove_dir_recursively(&buf, 0)) |
343 | ret = error(_("could not remove '%s'"), buf.buf); | |
9055e401 | 344 | strbuf_release(&buf); |
2863584f | 345 | |
37e9ee5c | 346 | return ret; |
26ae337b | 347 | } |
043a4492 RR |
348 | |
349 | static const char *action_name(const struct replay_opts *opts) | |
350 | { | |
84583957 JS |
351 | switch (opts->action) { |
352 | case REPLAY_REVERT: | |
353 | return N_("revert"); | |
354 | case REPLAY_PICK: | |
355 | return N_("cherry-pick"); | |
356 | case REPLAY_INTERACTIVE_REBASE: | |
c2417d3a | 357 | return N_("rebase"); |
84583957 | 358 | } |
1a07e59c | 359 | die(_("unknown action: %d"), opts->action); |
043a4492 RR |
360 | } |
361 | ||
043a4492 RR |
362 | struct commit_message { |
363 | char *parent_label; | |
7b35eaf8 JK |
364 | char *label; |
365 | char *subject; | |
043a4492 RR |
366 | const char *message; |
367 | }; | |
368 | ||
39755964 JS |
369 | static const char *short_commit_name(struct commit *commit) |
370 | { | |
aab9583f | 371 | return find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV); |
39755964 JS |
372 | } |
373 | ||
043a4492 RR |
374 | static int get_message(struct commit *commit, struct commit_message *out) |
375 | { | |
043a4492 | 376 | const char *abbrev, *subject; |
7b35eaf8 | 377 | int subject_len; |
043a4492 | 378 | |
7b35eaf8 | 379 | out->message = logmsg_reencode(commit, NULL, get_commit_output_encoding()); |
39755964 | 380 | abbrev = short_commit_name(commit); |
043a4492 RR |
381 | |
382 | subject_len = find_commit_subject(out->message, &subject); | |
383 | ||
7b35eaf8 | 384 | out->subject = xmemdupz(subject, subject_len); |
7d056dea | 385 | out->label = xstrfmt("%s (%s)", abbrev, out->subject); |
7b35eaf8 JK |
386 | out->parent_label = xstrfmt("parent of %s", out->label); |
387 | ||
043a4492 RR |
388 | return 0; |
389 | } | |
390 | ||
d74a4e57 | 391 | static void free_message(struct commit *commit, struct commit_message *msg) |
043a4492 RR |
392 | { |
393 | free(msg->parent_label); | |
7b35eaf8 JK |
394 | free(msg->label); |
395 | free(msg->subject); | |
b66103c3 | 396 | unuse_commit_buffer(commit, msg->message); |
043a4492 RR |
397 | } |
398 | ||
005af339 NTND |
399 | static void print_advice(struct repository *r, int show_hint, |
400 | struct replay_opts *opts) | |
043a4492 RR |
401 | { |
402 | char *msg = getenv("GIT_CHERRY_PICK_HELP"); | |
403 | ||
404 | if (msg) { | |
405 | fprintf(stderr, "%s\n", msg); | |
406 | /* | |
41ccfdd9 | 407 | * A conflict has occurred but the porcelain |
043a4492 RR |
408 | * (typically rebase --interactive) wants to take care |
409 | * of the commit itself so remove CHERRY_PICK_HEAD | |
410 | */ | |
c8e4159e HWN |
411 | refs_delete_ref(get_main_ref_store(r), "", "CHERRY_PICK_HEAD", |
412 | NULL, 0); | |
043a4492 RR |
413 | return; |
414 | } | |
415 | ||
ed727b19 PH |
416 | if (show_hint) { |
417 | if (opts->no_commit) | |
418 | advise(_("after resolving the conflicts, mark the corrected paths\n" | |
419 | "with 'git add <paths>' or 'git rm <paths>'")); | |
420 | else | |
421 | advise(_("after resolving the conflicts, mark the corrected paths\n" | |
422 | "with 'git add <paths>' or 'git rm <paths>'\n" | |
423 | "and commit the result with 'git commit'")); | |
424 | } | |
043a4492 RR |
425 | } |
426 | ||
ddb81e50 AG |
427 | static int write_message(const void *buf, size_t len, const char *filename, |
428 | int append_eol) | |
043a4492 | 429 | { |
14bca6c6 | 430 | struct lock_file msg_file = LOCK_INIT; |
043a4492 | 431 | |
4ef3d8f0 JS |
432 | int msg_fd = hold_lock_file_for_update(&msg_file, filename, 0); |
433 | if (msg_fd < 0) | |
93b3df6f | 434 | return error_errno(_("could not lock '%s'"), filename); |
75871495 | 435 | if (write_in_full(msg_fd, buf, len) < 0) { |
bf5c0571 | 436 | error_errno(_("could not write to '%s'"), filename); |
4f66c837 | 437 | rollback_lock_file(&msg_file); |
bf5c0571 | 438 | return -1; |
4f66c837 | 439 | } |
f56fffef | 440 | if (append_eol && write(msg_fd, "\n", 1) < 0) { |
bf5c0571 | 441 | error_errno(_("could not write eol to '%s'"), filename); |
f56fffef | 442 | rollback_lock_file(&msg_file); |
bf5c0571 | 443 | return -1; |
f56fffef | 444 | } |
350292a1 MÅ |
445 | if (commit_lock_file(&msg_file) < 0) |
446 | return error(_("failed to finalize '%s'"), filename); | |
4ef3d8f0 JS |
447 | |
448 | return 0; | |
043a4492 RR |
449 | } |
450 | ||
c20de8be | 451 | int read_oneliner(struct strbuf *buf, |
3442c3d1 | 452 | const char *path, unsigned flags) |
1dfc84e9 JS |
453 | { |
454 | int orig_len = buf->len; | |
455 | ||
1dfc84e9 | 456 | if (strbuf_read_file(buf, path, 0) < 0) { |
bfa50c2c DL |
457 | if ((flags & READ_ONELINER_WARN_MISSING) || |
458 | (errno != ENOENT && errno != ENOTDIR)) | |
5b2f6d9c | 459 | warning_errno(_("could not read '%s'"), path); |
1dfc84e9 JS |
460 | return 0; |
461 | } | |
462 | ||
463 | if (buf->len > orig_len && buf->buf[buf->len - 1] == '\n') { | |
464 | if (--buf->len > orig_len && buf->buf[buf->len - 1] == '\r') | |
465 | --buf->len; | |
466 | buf->buf[buf->len] = '\0'; | |
467 | } | |
468 | ||
3442c3d1 | 469 | if ((flags & READ_ONELINER_SKIP_IF_EMPTY) && buf->len == orig_len) |
1dfc84e9 JS |
470 | return 0; |
471 | ||
472 | return 1; | |
473 | } | |
474 | ||
005af339 | 475 | static struct tree *empty_tree(struct repository *r) |
043a4492 | 476 | { |
005af339 | 477 | return lookup_tree(r, the_hash_algo->empty_tree); |
043a4492 RR |
478 | } |
479 | ||
e1ff0a32 | 480 | static int error_dirty_index(struct repository *repo, struct replay_opts *opts) |
043a4492 | 481 | { |
e1ff0a32 | 482 | if (repo_read_index_unmerged(repo)) |
c28cbc5e | 483 | return error_resolve_conflict(_(action_name(opts))); |
043a4492 | 484 | |
93b3df6f | 485 | error(_("your local changes would be overwritten by %s."), |
c28cbc5e | 486 | _(action_name(opts))); |
043a4492 RR |
487 | |
488 | if (advice_commit_before_merge) | |
93b3df6f | 489 | advise(_("commit your changes or stash them to proceed.")); |
043a4492 RR |
490 | return -1; |
491 | } | |
492 | ||
1e41229d SB |
493 | static void update_abort_safety_file(void) |
494 | { | |
495 | struct object_id head; | |
496 | ||
497 | /* Do nothing on a single-pick */ | |
498 | if (!file_exists(git_path_seq_dir())) | |
499 | return; | |
500 | ||
501 | if (!get_oid("HEAD", &head)) | |
502 | write_file(git_path_abort_safety_file(), "%s", oid_to_hex(&head)); | |
503 | else | |
504 | write_file(git_path_abort_safety_file(), "%s", ""); | |
505 | } | |
506 | ||
f11c9580 NTND |
507 | static int fast_forward_to(struct repository *r, |
508 | const struct object_id *to, | |
509 | const struct object_id *from, | |
510 | int unborn, | |
511 | struct replay_opts *opts) | |
043a4492 | 512 | { |
d668d16c | 513 | struct ref_transaction *transaction; |
eb4be1cb | 514 | struct strbuf sb = STRBUF_INIT; |
d668d16c | 515 | struct strbuf err = STRBUF_INIT; |
043a4492 | 516 | |
e1ff0a32 | 517 | repo_read_index(r); |
f11c9580 | 518 | if (checkout_fast_forward(r, from, to, 1)) |
0e408fc3 | 519 | return -1; /* the callee should have complained already */ |
651ab9f5 | 520 | |
c28cbc5e | 521 | strbuf_addf(&sb, _("%s: fast-forward"), _(action_name(opts))); |
d668d16c RS |
522 | |
523 | transaction = ref_transaction_begin(&err); | |
524 | if (!transaction || | |
525 | ref_transaction_update(transaction, "HEAD", | |
d87d48b2 JS |
526 | to, unborn && !is_rebase_i(opts) ? |
527 | &null_oid : from, | |
1d147bdf | 528 | 0, sb.buf, &err) || |
db7516ab | 529 | ref_transaction_commit(transaction, &err)) { |
d668d16c RS |
530 | ref_transaction_free(transaction); |
531 | error("%s", err.buf); | |
532 | strbuf_release(&sb); | |
533 | strbuf_release(&err); | |
534 | return -1; | |
535 | } | |
651ab9f5 | 536 | |
eb4be1cb | 537 | strbuf_release(&sb); |
d668d16c RS |
538 | strbuf_release(&err); |
539 | ref_transaction_free(transaction); | |
1e41229d | 540 | update_abort_safety_file(); |
d668d16c | 541 | return 0; |
043a4492 RR |
542 | } |
543 | ||
f29cd862 DL |
544 | enum commit_msg_cleanup_mode get_cleanup_mode(const char *cleanup_arg, |
545 | int use_editor) | |
546 | { | |
547 | if (!cleanup_arg || !strcmp(cleanup_arg, "default")) | |
548 | return use_editor ? COMMIT_MSG_CLEANUP_ALL : | |
549 | COMMIT_MSG_CLEANUP_SPACE; | |
550 | else if (!strcmp(cleanup_arg, "verbatim")) | |
551 | return COMMIT_MSG_CLEANUP_NONE; | |
552 | else if (!strcmp(cleanup_arg, "whitespace")) | |
553 | return COMMIT_MSG_CLEANUP_SPACE; | |
554 | else if (!strcmp(cleanup_arg, "strip")) | |
555 | return COMMIT_MSG_CLEANUP_ALL; | |
556 | else if (!strcmp(cleanup_arg, "scissors")) | |
557 | return use_editor ? COMMIT_MSG_CLEANUP_SCISSORS : | |
558 | COMMIT_MSG_CLEANUP_SPACE; | |
559 | else | |
560 | die(_("Invalid cleanup mode %s"), cleanup_arg); | |
561 | } | |
562 | ||
dc42e9a8 PW |
563 | /* |
564 | * NB using int rather than enum cleanup_mode to stop clang's | |
565 | * -Wtautological-constant-out-of-range-compare complaining that the comparison | |
566 | * is always true. | |
567 | */ | |
568 | static const char *describe_cleanup_mode(int cleanup_mode) | |
569 | { | |
570 | static const char *modes[] = { "whitespace", | |
571 | "verbatim", | |
572 | "scissors", | |
573 | "strip" }; | |
574 | ||
575 | if (cleanup_mode < ARRAY_SIZE(modes)) | |
576 | return modes[cleanup_mode]; | |
577 | ||
578 | BUG("invalid cleanup_mode provided (%d)", cleanup_mode); | |
579 | } | |
580 | ||
f11c9580 | 581 | void append_conflicts_hint(struct index_state *istate, |
1a2b985f | 582 | struct strbuf *msgbuf, enum commit_msg_cleanup_mode cleanup_mode) |
75c961b7 JH |
583 | { |
584 | int i; | |
585 | ||
1a2b985f DL |
586 | if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) { |
587 | strbuf_addch(msgbuf, '\n'); | |
588 | wt_status_append_cut_line(msgbuf); | |
589 | strbuf_addch(msgbuf, comment_line_char); | |
590 | } | |
591 | ||
261f315b JH |
592 | strbuf_addch(msgbuf, '\n'); |
593 | strbuf_commented_addf(msgbuf, "Conflicts:\n"); | |
f11c9580 NTND |
594 | for (i = 0; i < istate->cache_nr;) { |
595 | const struct cache_entry *ce = istate->cache[i++]; | |
75c961b7 | 596 | if (ce_stage(ce)) { |
261f315b | 597 | strbuf_commented_addf(msgbuf, "\t%s\n", ce->name); |
f11c9580 NTND |
598 | while (i < istate->cache_nr && |
599 | !strcmp(ce->name, istate->cache[i]->name)) | |
75c961b7 JH |
600 | i++; |
601 | } | |
602 | } | |
603 | } | |
604 | ||
f11c9580 NTND |
605 | static int do_recursive_merge(struct repository *r, |
606 | struct commit *base, struct commit *next, | |
043a4492 | 607 | const char *base_label, const char *next_label, |
48be4c62 | 608 | struct object_id *head, struct strbuf *msgbuf, |
043a4492 RR |
609 | struct replay_opts *opts) |
610 | { | |
611 | struct merge_options o; | |
14c4586c | 612 | struct merge_result result; |
b4db8a2b | 613 | struct tree *next_tree, *base_tree, *head_tree; |
14c4586c | 614 | int clean, show_output; |
d20bc01a | 615 | int i; |
14bca6c6 | 616 | struct lock_file index_lock = LOCK_INIT; |
043a4492 | 617 | |
3a95f31d | 618 | if (repo_hold_locked_index(r, &index_lock, LOCK_REPORT_ON_ERROR) < 0) |
bd588867 | 619 | return -1; |
043a4492 | 620 | |
e1ff0a32 | 621 | repo_read_index(r); |
043a4492 | 622 | |
0d6caa2d | 623 | init_merge_options(&o, r); |
043a4492 RR |
624 | o.ancestor = base ? base_label : "(empty tree)"; |
625 | o.branch1 = "HEAD"; | |
626 | o.branch2 = next ? next_label : "(empty tree)"; | |
62fdb652 JS |
627 | if (is_rebase_i(opts)) |
628 | o.buffer_output = 2; | |
9268cf4a | 629 | o.show_rename_progress = 1; |
043a4492 RR |
630 | |
631 | head_tree = parse_tree_indirect(head); | |
005af339 NTND |
632 | next_tree = next ? get_commit_tree(next) : empty_tree(r); |
633 | base_tree = base ? get_commit_tree(base) : empty_tree(r); | |
043a4492 | 634 | |
d20bc01a JK |
635 | for (i = 0; i < opts->xopts_nr; i++) |
636 | parse_merge_opt(&o, opts->xopts[i]); | |
043a4492 | 637 | |
14c4586c EN |
638 | if (opts->strategy && !strcmp(opts->strategy, "ort")) { |
639 | memset(&result, 0, sizeof(result)); | |
640 | merge_incore_nonrecursive(&o, base_tree, head_tree, next_tree, | |
641 | &result); | |
642 | show_output = !is_rebase_i(opts) || !result.clean; | |
643 | /* | |
644 | * TODO: merge_switch_to_result will update index/working tree; | |
645 | * we only really want to do that if !result.clean || this is | |
646 | * the final patch to be picked. But determining this is the | |
647 | * final patch would take some work, and "head_tree" would need | |
648 | * to be replace with the tree the index matched before we | |
649 | * started doing any picks. | |
650 | */ | |
651 | merge_switch_to_result(&o, head_tree, &result, 1, show_output); | |
652 | clean = result.clean; | |
653 | } else { | |
654 | clean = merge_trees(&o, head_tree, next_tree, base_tree); | |
655 | if (is_rebase_i(opts) && clean <= 0) | |
656 | fputs(o.obuf.buf, stdout); | |
657 | strbuf_release(&o.obuf); | |
658 | } | |
64816524 MÅ |
659 | if (clean < 0) { |
660 | rollback_lock_file(&index_lock); | |
f241ff0d | 661 | return clean; |
64816524 | 662 | } |
043a4492 | 663 | |
f11c9580 | 664 | if (write_locked_index(r->index, &index_lock, |
61000814 | 665 | COMMIT_LOCK | SKIP_IF_UNCHANGED)) |
66f5f6dc ÆAB |
666 | /* |
667 | * TRANSLATORS: %s will be "revert", "cherry-pick" or | |
c2417d3a | 668 | * "rebase". |
84583957 | 669 | */ |
c527b55e | 670 | return error(_("%s: Unable to write new index file"), |
c28cbc5e | 671 | _(action_name(opts))); |
043a4492 | 672 | |
75c961b7 | 673 | if (!clean) |
1a2b985f DL |
674 | append_conflicts_hint(r->index, msgbuf, |
675 | opts->default_msg_cleanup); | |
043a4492 RR |
676 | |
677 | return !clean; | |
678 | } | |
679 | ||
f11c9580 | 680 | static struct object_id *get_cache_tree_oid(struct index_state *istate) |
ba97aea1 | 681 | { |
f11c9580 NTND |
682 | if (!istate->cache_tree) |
683 | istate->cache_tree = cache_tree(); | |
ba97aea1 | 684 | |
f11c9580 NTND |
685 | if (!cache_tree_fully_valid(istate->cache_tree)) |
686 | if (cache_tree_update(istate, 0)) { | |
ba97aea1 JS |
687 | error(_("unable to update cache tree")); |
688 | return NULL; | |
689 | } | |
690 | ||
f11c9580 | 691 | return &istate->cache_tree->oid; |
ba97aea1 JS |
692 | } |
693 | ||
005af339 | 694 | static int is_index_unchanged(struct repository *r) |
b27cfb0d | 695 | { |
ba97aea1 | 696 | struct object_id head_oid, *cache_tree_oid; |
b27cfb0d | 697 | struct commit *head_commit; |
005af339 | 698 | struct index_state *istate = r->index; |
b27cfb0d | 699 | |
49e61479 | 700 | if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &head_oid, NULL)) |
aee42e1f | 701 | return error(_("could not resolve HEAD commit")); |
b27cfb0d | 702 | |
005af339 | 703 | head_commit = lookup_commit(r, &head_oid); |
4b580061 NH |
704 | |
705 | /* | |
706 | * If head_commit is NULL, check_commit, called from | |
707 | * lookup_commit, would have indicated that head_commit is not | |
708 | * a commit object already. parse_commit() will return failure | |
709 | * without further complaints in such a case. Otherwise, if | |
710 | * the commit is invalid, parse_commit() will complain. So | |
711 | * there is nothing for us to say here. Just return failure. | |
712 | */ | |
713 | if (parse_commit(head_commit)) | |
714 | return -1; | |
b27cfb0d | 715 | |
f11c9580 | 716 | if (!(cache_tree_oid = get_cache_tree_oid(istate))) |
ba97aea1 | 717 | return -1; |
b27cfb0d | 718 | |
4a7e27e9 | 719 | return oideq(cache_tree_oid, get_commit_tree_oid(head_commit)); |
b27cfb0d NH |
720 | } |
721 | ||
0473f28a JS |
722 | static int write_author_script(const char *message) |
723 | { | |
724 | struct strbuf buf = STRBUF_INIT; | |
725 | const char *eol; | |
726 | int res; | |
727 | ||
728 | for (;;) | |
729 | if (!*message || starts_with(message, "\n")) { | |
730 | missing_author: | |
731 | /* Missing 'author' line? */ | |
732 | unlink(rebase_path_author_script()); | |
733 | return 0; | |
734 | } else if (skip_prefix(message, "author ", &message)) | |
735 | break; | |
736 | else if ((eol = strchr(message, '\n'))) | |
737 | message = eol + 1; | |
738 | else | |
739 | goto missing_author; | |
740 | ||
741 | strbuf_addstr(&buf, "GIT_AUTHOR_NAME='"); | |
742 | while (*message && *message != '\n' && *message != '\r') | |
743 | if (skip_prefix(message, " <", &message)) | |
744 | break; | |
745 | else if (*message != '\'') | |
746 | strbuf_addch(&buf, *(message++)); | |
747 | else | |
4aa5ff94 | 748 | strbuf_addf(&buf, "'\\%c'", *(message++)); |
0473f28a JS |
749 | strbuf_addstr(&buf, "'\nGIT_AUTHOR_EMAIL='"); |
750 | while (*message && *message != '\n' && *message != '\r') | |
751 | if (skip_prefix(message, "> ", &message)) | |
752 | break; | |
753 | else if (*message != '\'') | |
754 | strbuf_addch(&buf, *(message++)); | |
755 | else | |
4aa5ff94 | 756 | strbuf_addf(&buf, "'\\%c'", *(message++)); |
0473f28a JS |
757 | strbuf_addstr(&buf, "'\nGIT_AUTHOR_DATE='@"); |
758 | while (*message && *message != '\n' && *message != '\r') | |
759 | if (*message != '\'') | |
760 | strbuf_addch(&buf, *(message++)); | |
761 | else | |
4aa5ff94 | 762 | strbuf_addf(&buf, "'\\%c'", *(message++)); |
0f16c09a | 763 | strbuf_addch(&buf, '\''); |
0473f28a JS |
764 | res = write_message(buf.buf, buf.len, rebase_path_author_script(), 1); |
765 | strbuf_release(&buf); | |
766 | return res; | |
767 | } | |
768 | ||
bcd33ec2 PW |
769 | /** |
770 | * Take a series of KEY='VALUE' lines where VALUE part is | |
771 | * sq-quoted, and append <KEY, VALUE> at the end of the string list | |
772 | */ | |
773 | static int parse_key_value_squoted(char *buf, struct string_list *list) | |
774 | { | |
775 | while (*buf) { | |
776 | struct string_list_item *item; | |
777 | char *np; | |
778 | char *cp = strchr(buf, '='); | |
779 | if (!cp) { | |
780 | np = strchrnul(buf, '\n'); | |
781 | return error(_("no key present in '%.*s'"), | |
782 | (int) (np - buf), buf); | |
783 | } | |
784 | np = strchrnul(cp, '\n'); | |
785 | *cp++ = '\0'; | |
786 | item = string_list_append(list, buf); | |
787 | ||
788 | buf = np + (*np == '\n'); | |
789 | *np = '\0'; | |
790 | cp = sq_dequote(cp); | |
791 | if (!cp) | |
792 | return error(_("unable to dequote value of '%s'"), | |
793 | item->string); | |
794 | item->util = xstrdup(cp); | |
795 | } | |
796 | return 0; | |
797 | } | |
4aa5ff94 | 798 | |
bcd33ec2 PW |
799 | /** |
800 | * Reads and parses the state directory's "author-script" file, and sets name, | |
801 | * email and date accordingly. | |
802 | * Returns 0 on success, -1 if the file could not be parsed. | |
803 | * | |
804 | * The author script is of the format: | |
805 | * | |
806 | * GIT_AUTHOR_NAME='$author_name' | |
807 | * GIT_AUTHOR_EMAIL='$author_email' | |
808 | * GIT_AUTHOR_DATE='$author_date' | |
809 | * | |
810 | * where $author_name, $author_email and $author_date are quoted. We are strict | |
c3c003e7 | 811 | * with our parsing, as the file was meant to be eval'd in the now-removed |
bcd33ec2 PW |
812 | * git-am.sh/git-rebase--interactive.sh scripts, and thus if the file differs |
813 | * from what this function expects, it is better to bail out than to do | |
814 | * something that the user does not expect. | |
4aa5ff94 | 815 | */ |
bcd33ec2 PW |
816 | int read_author_script(const char *path, char **name, char **email, char **date, |
817 | int allow_missing) | |
4aa5ff94 | 818 | { |
bcd33ec2 PW |
819 | struct strbuf buf = STRBUF_INIT; |
820 | struct string_list kv = STRING_LIST_INIT_DUP; | |
821 | int retval = -1; /* assume failure */ | |
822 | int i, name_i = -2, email_i = -2, date_i = -2, err = 0; | |
4aa5ff94 | 823 | |
bcd33ec2 PW |
824 | if (strbuf_read_file(&buf, path, 256) <= 0) { |
825 | strbuf_release(&buf); | |
826 | if (errno == ENOENT && allow_missing) | |
827 | return 0; | |
828 | else | |
829 | return error_errno(_("could not open '%s' for reading"), | |
830 | path); | |
831 | } | |
832 | ||
833 | if (parse_key_value_squoted(buf.buf, &kv)) | |
834 | goto finish; | |
835 | ||
836 | for (i = 0; i < kv.nr; i++) { | |
837 | if (!strcmp(kv.items[i].string, "GIT_AUTHOR_NAME")) { | |
838 | if (name_i != -2) | |
839 | name_i = error(_("'GIT_AUTHOR_NAME' already given")); | |
840 | else | |
841 | name_i = i; | |
842 | } else if (!strcmp(kv.items[i].string, "GIT_AUTHOR_EMAIL")) { | |
843 | if (email_i != -2) | |
844 | email_i = error(_("'GIT_AUTHOR_EMAIL' already given")); | |
845 | else | |
846 | email_i = i; | |
847 | } else if (!strcmp(kv.items[i].string, "GIT_AUTHOR_DATE")) { | |
848 | if (date_i != -2) | |
849 | date_i = error(_("'GIT_AUTHOR_DATE' already given")); | |
850 | else | |
851 | date_i = i; | |
852 | } else { | |
853 | err = error(_("unknown variable '%s'"), | |
854 | kv.items[i].string); | |
855 | } | |
856 | } | |
857 | if (name_i == -2) | |
858 | error(_("missing 'GIT_AUTHOR_NAME'")); | |
859 | if (email_i == -2) | |
860 | error(_("missing 'GIT_AUTHOR_EMAIL'")); | |
861 | if (date_i == -2) | |
862 | error(_("missing 'GIT_AUTHOR_DATE'")); | |
863 | if (date_i < 0 || email_i < 0 || date_i < 0 || err) | |
864 | goto finish; | |
4d924528 JH |
865 | *name = kv.items[name_i].util; |
866 | *email = kv.items[email_i].util; | |
867 | *date = kv.items[date_i].util; | |
bcd33ec2 PW |
868 | retval = 0; |
869 | finish: | |
870 | string_list_clear(&kv, !!retval); | |
871 | strbuf_release(&buf); | |
872 | return retval; | |
4aa5ff94 PW |
873 | } |
874 | ||
b5a67045 | 875 | /* |
4d010a75 | 876 | * Read a GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL AND GIT_AUTHOR_DATE from a |
c972bf4c | 877 | * file with shell quoting into struct strvec. Returns -1 on |
4d010a75 | 878 | * error, 0 otherwise. |
b5a67045 | 879 | */ |
c972bf4c | 880 | static int read_env_script(struct strvec *env) |
b5a67045 | 881 | { |
4d010a75 | 882 | char *name, *email, *date; |
b5a67045 | 883 | |
4d010a75 PW |
884 | if (read_author_script(rebase_path_author_script(), |
885 | &name, &email, &date, 0)) | |
a2a20b0d | 886 | return -1; |
b5a67045 | 887 | |
c972bf4c JK |
888 | strvec_pushf(env, "GIT_AUTHOR_NAME=%s", name); |
889 | strvec_pushf(env, "GIT_AUTHOR_EMAIL=%s", email); | |
890 | strvec_pushf(env, "GIT_AUTHOR_DATE=%s", date); | |
4d010a75 PW |
891 | free(name); |
892 | free(email); | |
893 | free(date); | |
b5a67045 | 894 | |
a2a20b0d | 895 | return 0; |
b5a67045 JS |
896 | } |
897 | ||
356ee465 PW |
898 | static char *get_author(const char *message) |
899 | { | |
900 | size_t len; | |
901 | const char *a; | |
902 | ||
903 | a = find_commit_header(message, "author", &len); | |
904 | if (a) | |
905 | return xmemdupz(a, len); | |
906 | ||
907 | return NULL; | |
908 | } | |
909 | ||
9c31b19d | 910 | static const char *author_date_from_env_array(const struct strvec *env) |
7573cec5 PW |
911 | { |
912 | int i; | |
913 | const char *date; | |
914 | ||
9c31b19d JH |
915 | for (i = 0; i < env->nr; i++) |
916 | if (skip_prefix(env->v[i], | |
7573cec5 PW |
917 | "GIT_AUTHOR_DATE=", &date)) |
918 | return date; | |
919 | /* | |
920 | * If GIT_AUTHOR_DATE is missing we should have already errored out when | |
921 | * reading the script | |
922 | */ | |
923 | BUG("GIT_AUTHOR_DATE missing from author script"); | |
924 | } | |
925 | ||
791eb870 JS |
926 | static const char staged_changes_advice[] = |
927 | N_("you have staged changes in your working tree\n" | |
928 | "If these changes are meant to be squashed into the previous commit, run:\n" | |
929 | "\n" | |
930 | " git commit --amend %s\n" | |
931 | "\n" | |
932 | "If they are meant to go into a new commit, run:\n" | |
933 | "\n" | |
934 | " git commit %s\n" | |
935 | "\n" | |
936 | "In both cases, once you're done, continue with:\n" | |
937 | "\n" | |
938 | " git rebase --continue\n"); | |
939 | ||
789b3eff JS |
940 | #define ALLOW_EMPTY (1<<0) |
941 | #define EDIT_MSG (1<<1) | |
942 | #define AMEND_MSG (1<<2) | |
943 | #define CLEANUP_MSG (1<<3) | |
b92ff6e8 | 944 | #define VERIFY_MSG (1<<4) |
d87d48b2 | 945 | #define CREATE_ROOT_COMMIT (1<<5) |
789b3eff | 946 | |
34bec2c4 AG |
947 | static int run_command_silent_on_success(struct child_process *cmd) |
948 | { | |
949 | struct strbuf buf = STRBUF_INIT; | |
950 | int rc; | |
951 | ||
952 | cmd->stdout_to_stderr = 1; | |
953 | rc = pipe_command(cmd, | |
954 | NULL, 0, | |
955 | NULL, 0, | |
956 | &buf, 0); | |
957 | ||
958 | if (rc) | |
959 | fputs(buf.buf, stderr); | |
960 | strbuf_release(&buf); | |
961 | return rc; | |
962 | } | |
963 | ||
043a4492 RR |
964 | /* |
965 | * If we are cherry-pick, and if the merge did not result in | |
966 | * hand-editing, we will hit this commit and inherit the original | |
967 | * author date and name. | |
b5a67045 | 968 | * |
043a4492 RR |
969 | * If we are revert, or if our cherry-pick results in a hand merge, |
970 | * we had better say that the current user is responsible for that. | |
b5a67045 JS |
971 | * |
972 | * An exception is when run_git_commit() is called during an | |
973 | * interactive rebase: in that case, we will want to retain the | |
974 | * author metadata. | |
043a4492 | 975 | */ |
20f4b044 | 976 | static int run_git_commit(const char *defmsg, |
f11c9580 | 977 | struct replay_opts *opts, |
789b3eff | 978 | unsigned int flags) |
043a4492 | 979 | { |
07d968ef | 980 | struct child_process cmd = CHILD_PROCESS_INIT; |
b27cfb0d | 981 | |
07d968ef JS |
982 | cmd.git_cmd = 1; |
983 | ||
34bec2c4 AG |
984 | if (is_rebase_i(opts) && read_env_script(&cmd.env_array)) { |
985 | const char *gpg_opt = gpg_sign_opt_quoted(opts); | |
a1c75762 | 986 | |
34bec2c4 AG |
987 | return error(_(staged_changes_advice), |
988 | gpg_opt, gpg_opt); | |
b5a67045 JS |
989 | } |
990 | ||
7573cec5 | 991 | if (opts->committer_date_is_author_date) |
9c31b19d JH |
992 | strvec_pushf(&cmd.env_array, "GIT_COMMITTER_DATE=%s", |
993 | opts->ignore_date ? | |
994 | "" : | |
995 | author_date_from_env_array(&cmd.env_array)); | |
a3894aad | 996 | if (opts->ignore_date) |
9c31b19d | 997 | strvec_push(&cmd.env_array, "GIT_AUTHOR_DATE="); |
7573cec5 | 998 | |
c972bf4c | 999 | strvec_push(&cmd.args, "commit"); |
043a4492 | 1000 | |
b92ff6e8 | 1001 | if (!(flags & VERIFY_MSG)) |
c972bf4c | 1002 | strvec_push(&cmd.args, "-n"); |
789b3eff | 1003 | if ((flags & AMEND_MSG)) |
c972bf4c | 1004 | strvec_push(&cmd.args, "--amend"); |
3bdd5522 | 1005 | if (opts->gpg_sign) |
c972bf4c | 1006 | strvec_pushf(&cmd.args, "-S%s", opts->gpg_sign); |
cf0ad4d1 | 1007 | else |
c972bf4c | 1008 | strvec_push(&cmd.args, "--no-gpg-sign"); |
b5a67045 | 1009 | if (defmsg) |
c972bf4c | 1010 | strvec_pushl(&cmd.args, "-F", defmsg, NULL); |
dc4b5bc3 | 1011 | else if (!(flags & EDIT_MSG)) |
c972bf4c | 1012 | strvec_pushl(&cmd.args, "-C", "HEAD", NULL); |
789b3eff | 1013 | if ((flags & CLEANUP_MSG)) |
c972bf4c | 1014 | strvec_push(&cmd.args, "--cleanup=strip"); |
789b3eff | 1015 | if ((flags & EDIT_MSG)) |
c972bf4c | 1016 | strvec_push(&cmd.args, "-e"); |
789b3eff | 1017 | else if (!(flags & CLEANUP_MSG) && |
0009426d | 1018 | !opts->signoff && !opts->record_origin && |
1a2b985f | 1019 | !opts->explicit_cleanup) |
c972bf4c | 1020 | strvec_push(&cmd.args, "--cleanup=verbatim"); |
b27cfb0d | 1021 | |
789b3eff | 1022 | if ((flags & ALLOW_EMPTY)) |
c972bf4c | 1023 | strvec_push(&cmd.args, "--allow-empty"); |
df478b74 | 1024 | |
a3ec9eaf | 1025 | if (!(flags & EDIT_MSG)) |
c972bf4c | 1026 | strvec_push(&cmd.args, "--allow-empty-message"); |
4bee9584 | 1027 | |
34bec2c4 AG |
1028 | if (is_rebase_i(opts) && !(flags & EDIT_MSG)) |
1029 | return run_command_silent_on_success(&cmd); | |
1030 | else | |
1031 | return run_command(&cmd); | |
b27cfb0d NH |
1032 | } |
1033 | ||
d0aaa46f PW |
1034 | static int rest_is_empty(const struct strbuf *sb, int start) |
1035 | { | |
1036 | int i, eol; | |
1037 | const char *nl; | |
1038 | ||
1039 | /* Check if the rest is just whitespace and Signed-off-by's. */ | |
1040 | for (i = start; i < sb->len; i++) { | |
1041 | nl = memchr(sb->buf + i, '\n', sb->len - i); | |
1042 | if (nl) | |
1043 | eol = nl - sb->buf; | |
1044 | else | |
1045 | eol = sb->len; | |
1046 | ||
1047 | if (strlen(sign_off_header) <= eol - i && | |
1048 | starts_with(sb->buf + i, sign_off_header)) { | |
1049 | i = eol; | |
1050 | continue; | |
1051 | } | |
1052 | while (i < eol) | |
1053 | if (!isspace(sb->buf[i++])) | |
1054 | return 0; | |
1055 | } | |
1056 | ||
1057 | return 1; | |
1058 | } | |
1059 | ||
f29cd862 DL |
1060 | void cleanup_message(struct strbuf *msgbuf, |
1061 | enum commit_msg_cleanup_mode cleanup_mode, int verbose) | |
1062 | { | |
1063 | if (verbose || /* Truncate the message just before the diff, if any. */ | |
1064 | cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) | |
1065 | strbuf_setlen(msgbuf, wt_status_locate_end(msgbuf->buf, msgbuf->len)); | |
1066 | if (cleanup_mode != COMMIT_MSG_CLEANUP_NONE) | |
1067 | strbuf_stripspace(msgbuf, cleanup_mode == COMMIT_MSG_CLEANUP_ALL); | |
1068 | } | |
1069 | ||
d0aaa46f PW |
1070 | /* |
1071 | * Find out if the message in the strbuf contains only whitespace and | |
1072 | * Signed-off-by lines. | |
1073 | */ | |
1074 | int message_is_empty(const struct strbuf *sb, | |
1075 | enum commit_msg_cleanup_mode cleanup_mode) | |
1076 | { | |
1077 | if (cleanup_mode == COMMIT_MSG_CLEANUP_NONE && sb->len) | |
1078 | return 0; | |
1079 | return rest_is_empty(sb, 0); | |
1080 | } | |
1081 | ||
1082 | /* | |
1083 | * See if the user edited the message in the editor or left what | |
1084 | * was in the template intact | |
1085 | */ | |
1086 | int template_untouched(const struct strbuf *sb, const char *template_file, | |
1087 | enum commit_msg_cleanup_mode cleanup_mode) | |
1088 | { | |
1089 | struct strbuf tmpl = STRBUF_INIT; | |
1090 | const char *start; | |
1091 | ||
1092 | if (cleanup_mode == COMMIT_MSG_CLEANUP_NONE && sb->len) | |
1093 | return 0; | |
1094 | ||
1095 | if (!template_file || strbuf_read_file(&tmpl, template_file, 0) <= 0) | |
1096 | return 0; | |
1097 | ||
1098 | strbuf_stripspace(&tmpl, cleanup_mode == COMMIT_MSG_CLEANUP_ALL); | |
1099 | if (!skip_prefix(sb->buf, tmpl.buf, &start)) | |
1100 | start = sb->buf; | |
1101 | strbuf_release(&tmpl); | |
1102 | return rest_is_empty(sb, start - sb->buf); | |
1103 | } | |
1104 | ||
0505d604 PW |
1105 | int update_head_with_reflog(const struct commit *old_head, |
1106 | const struct object_id *new_head, | |
1107 | const char *action, const struct strbuf *msg, | |
1108 | struct strbuf *err) | |
1109 | { | |
1110 | struct ref_transaction *transaction; | |
1111 | struct strbuf sb = STRBUF_INIT; | |
1112 | const char *nl; | |
1113 | int ret = 0; | |
1114 | ||
1115 | if (action) { | |
1116 | strbuf_addstr(&sb, action); | |
1117 | strbuf_addstr(&sb, ": "); | |
1118 | } | |
1119 | ||
1120 | nl = strchr(msg->buf, '\n'); | |
1121 | if (nl) { | |
1122 | strbuf_add(&sb, msg->buf, nl + 1 - msg->buf); | |
1123 | } else { | |
1124 | strbuf_addbuf(&sb, msg); | |
1125 | strbuf_addch(&sb, '\n'); | |
1126 | } | |
1127 | ||
1128 | transaction = ref_transaction_begin(err); | |
1129 | if (!transaction || | |
1130 | ref_transaction_update(transaction, "HEAD", new_head, | |
1131 | old_head ? &old_head->object.oid : &null_oid, | |
1132 | 0, sb.buf, err) || | |
1133 | ref_transaction_commit(transaction, err)) { | |
1134 | ret = -1; | |
1135 | } | |
1136 | ref_transaction_free(transaction); | |
1137 | strbuf_release(&sb); | |
1138 | ||
1139 | return ret; | |
1140 | } | |
1141 | ||
a87a6f3c PW |
1142 | static int run_rewrite_hook(const struct object_id *oldoid, |
1143 | const struct object_id *newoid) | |
1144 | { | |
1145 | struct child_process proc = CHILD_PROCESS_INIT; | |
1146 | const char *argv[3]; | |
1147 | int code; | |
1148 | struct strbuf sb = STRBUF_INIT; | |
1149 | ||
1150 | argv[0] = find_hook("post-rewrite"); | |
1151 | if (!argv[0]) | |
1152 | return 0; | |
1153 | ||
1154 | argv[1] = "amend"; | |
1155 | argv[2] = NULL; | |
1156 | ||
1157 | proc.argv = argv; | |
1158 | proc.in = -1; | |
1159 | proc.stdout_to_stderr = 1; | |
6206286e | 1160 | proc.trace2_hook_name = "post-rewrite"; |
a87a6f3c PW |
1161 | |
1162 | code = start_command(&proc); | |
1163 | if (code) | |
1164 | return code; | |
1165 | strbuf_addf(&sb, "%s %s\n", oid_to_hex(oldoid), oid_to_hex(newoid)); | |
1166 | sigchain_push(SIGPIPE, SIG_IGN); | |
1167 | write_in_full(proc.in, sb.buf, sb.len); | |
1168 | close(proc.in); | |
1169 | strbuf_release(&sb); | |
1170 | sigchain_pop(SIGPIPE); | |
1171 | return finish_command(&proc); | |
1172 | } | |
1173 | ||
1d18d758 NTND |
1174 | void commit_post_rewrite(struct repository *r, |
1175 | const struct commit *old_head, | |
a87a6f3c PW |
1176 | const struct object_id *new_head) |
1177 | { | |
1178 | struct notes_rewrite_cfg *cfg; | |
1179 | ||
1180 | cfg = init_copy_notes_for_rewrite("amend"); | |
1181 | if (cfg) { | |
1182 | /* we are amending, so old_head is not NULL */ | |
1183 | copy_note_for_rewrite(cfg, &old_head->object.oid, new_head); | |
1d18d758 | 1184 | finish_copy_notes_for_rewrite(r, cfg, "Notes added by 'git commit --amend'"); |
a87a6f3c PW |
1185 | } |
1186 | run_rewrite_hook(&old_head->object.oid, new_head); | |
1187 | } | |
1188 | ||
f11c9580 NTND |
1189 | static int run_prepare_commit_msg_hook(struct repository *r, |
1190 | struct strbuf *msg, | |
1191 | const char *commit) | |
66618a50 | 1192 | { |
49697cb7 PW |
1193 | int ret = 0; |
1194 | const char *name, *arg1 = NULL, *arg2 = NULL; | |
66618a50 PW |
1195 | |
1196 | name = git_path_commit_editmsg(); | |
1197 | if (write_message(msg->buf, msg->len, name, 0)) | |
1198 | return -1; | |
1199 | ||
49697cb7 PW |
1200 | if (commit) { |
1201 | arg1 = "commit"; | |
1202 | arg2 = commit; | |
1203 | } else { | |
1204 | arg1 = "message"; | |
1205 | } | |
1206 | if (run_commit_hook(0, r->index_file, "prepare-commit-msg", name, | |
1207 | arg1, arg2, NULL)) | |
66618a50 | 1208 | ret = error(_("'prepare-commit-msg' hook failed")); |
66618a50 PW |
1209 | |
1210 | return ret; | |
1211 | } | |
1212 | ||
e47c6caf PW |
1213 | static const char implicit_ident_advice_noconfig[] = |
1214 | N_("Your name and email address were configured automatically based\n" | |
1215 | "on your username and hostname. Please check that they are accurate.\n" | |
1216 | "You can suppress this message by setting them explicitly. Run the\n" | |
1217 | "following command and follow the instructions in your editor to edit\n" | |
1218 | "your configuration file:\n" | |
1219 | "\n" | |
1220 | " git config --global --edit\n" | |
1221 | "\n" | |
1222 | "After doing this, you may fix the identity used for this commit with:\n" | |
1223 | "\n" | |
1224 | " git commit --amend --reset-author\n"); | |
1225 | ||
1226 | static const char implicit_ident_advice_config[] = | |
1227 | N_("Your name and email address were configured automatically based\n" | |
1228 | "on your username and hostname. Please check that they are accurate.\n" | |
1229 | "You can suppress this message by setting them explicitly:\n" | |
1230 | "\n" | |
1231 | " git config --global user.name \"Your Name\"\n" | |
1232 | " git config --global user.email you@example.com\n" | |
1233 | "\n" | |
1234 | "After doing this, you may fix the identity used for this commit with:\n" | |
1235 | "\n" | |
1236 | " git commit --amend --reset-author\n"); | |
1237 | ||
1238 | static const char *implicit_ident_advice(void) | |
1239 | { | |
1240 | char *user_config = expand_user_path("~/.gitconfig", 0); | |
1241 | char *xdg_config = xdg_config_home("config"); | |
1242 | int config_exists = file_exists(user_config) || file_exists(xdg_config); | |
1243 | ||
1244 | free(user_config); | |
1245 | free(xdg_config); | |
1246 | ||
1247 | if (config_exists) | |
1248 | return _(implicit_ident_advice_config); | |
1249 | else | |
1250 | return _(implicit_ident_advice_noconfig); | |
1251 | ||
1252 | } | |
1253 | ||
f11c9580 NTND |
1254 | void print_commit_summary(struct repository *r, |
1255 | const char *prefix, | |
1256 | const struct object_id *oid, | |
e47c6caf PW |
1257 | unsigned int flags) |
1258 | { | |
1259 | struct rev_info rev; | |
1260 | struct commit *commit; | |
1261 | struct strbuf format = STRBUF_INIT; | |
1262 | const char *head; | |
1263 | struct pretty_print_context pctx = {0}; | |
1264 | struct strbuf author_ident = STRBUF_INIT; | |
1265 | struct strbuf committer_ident = STRBUF_INIT; | |
1266 | ||
f11c9580 | 1267 | commit = lookup_commit(r, oid); |
e47c6caf PW |
1268 | if (!commit) |
1269 | die(_("couldn't look up newly created commit")); | |
1270 | if (parse_commit(commit)) | |
1271 | die(_("could not parse newly created commit")); | |
1272 | ||
1273 | strbuf_addstr(&format, "format:%h] %s"); | |
1274 | ||
1275 | format_commit_message(commit, "%an <%ae>", &author_ident, &pctx); | |
1276 | format_commit_message(commit, "%cn <%ce>", &committer_ident, &pctx); | |
1277 | if (strbuf_cmp(&author_ident, &committer_ident)) { | |
1278 | strbuf_addstr(&format, "\n Author: "); | |
1279 | strbuf_addbuf_percentquote(&format, &author_ident); | |
1280 | } | |
1281 | if (flags & SUMMARY_SHOW_AUTHOR_DATE) { | |
1282 | struct strbuf date = STRBUF_INIT; | |
1283 | ||
1284 | format_commit_message(commit, "%ad", &date, &pctx); | |
1285 | strbuf_addstr(&format, "\n Date: "); | |
1286 | strbuf_addbuf_percentquote(&format, &date); | |
1287 | strbuf_release(&date); | |
1288 | } | |
1289 | if (!committer_ident_sufficiently_given()) { | |
1290 | strbuf_addstr(&format, "\n Committer: "); | |
1291 | strbuf_addbuf_percentquote(&format, &committer_ident); | |
1292 | if (advice_implicit_identity) { | |
1293 | strbuf_addch(&format, '\n'); | |
1294 | strbuf_addstr(&format, implicit_ident_advice()); | |
1295 | } | |
1296 | } | |
1297 | strbuf_release(&author_ident); | |
1298 | strbuf_release(&committer_ident); | |
1299 | ||
f11c9580 | 1300 | repo_init_revisions(r, &rev, prefix); |
e47c6caf PW |
1301 | setup_revisions(0, NULL, &rev, NULL); |
1302 | ||
1303 | rev.diff = 1; | |
1304 | rev.diffopt.output_format = | |
1305 | DIFF_FORMAT_SHORTSTAT | DIFF_FORMAT_SUMMARY; | |
1306 | ||
1307 | rev.verbose_header = 1; | |
1308 | rev.show_root_diff = 1; | |
1309 | get_commit_format(format.buf, &rev); | |
1310 | rev.always_show_header = 0; | |
0f57f731 | 1311 | rev.diffopt.detect_rename = DIFF_DETECT_RENAME; |
e47c6caf PW |
1312 | rev.diffopt.break_opt = 0; |
1313 | diff_setup_done(&rev.diffopt); | |
1314 | ||
1315 | head = resolve_ref_unsafe("HEAD", 0, NULL, NULL); | |
1316 | if (!head) | |
1317 | die_errno(_("unable to resolve HEAD after creating commit")); | |
1318 | if (!strcmp(head, "HEAD")) | |
1319 | head = _("detached HEAD"); | |
1320 | else | |
1321 | skip_prefix(head, "refs/heads/", &head); | |
1322 | printf("[%s%s ", head, (flags & SUMMARY_INITIAL_COMMIT) ? | |
1323 | _(" (root-commit)") : ""); | |
1324 | ||
1325 | if (!log_tree_commit(&rev, commit)) { | |
1326 | rev.always_show_header = 1; | |
1327 | rev.use_terminator = 1; | |
1328 | log_tree_commit(&rev, commit); | |
1329 | } | |
1330 | ||
1331 | strbuf_release(&format); | |
1332 | } | |
1333 | ||
005af339 | 1334 | static int parse_head(struct repository *r, struct commit **head) |
356ee465 PW |
1335 | { |
1336 | struct commit *current_head; | |
1337 | struct object_id oid; | |
1338 | ||
1339 | if (get_oid("HEAD", &oid)) { | |
1340 | current_head = NULL; | |
1341 | } else { | |
005af339 | 1342 | current_head = lookup_commit_reference(r, &oid); |
356ee465 PW |
1343 | if (!current_head) |
1344 | return error(_("could not parse HEAD")); | |
9001dc2a | 1345 | if (!oideq(&oid, ¤t_head->object.oid)) { |
356ee465 PW |
1346 | warning(_("HEAD %s is not a commit!"), |
1347 | oid_to_hex(&oid)); | |
1348 | } | |
1349 | if (parse_commit(current_head)) | |
1350 | return error(_("could not parse HEAD commit")); | |
1351 | } | |
1352 | *head = current_head; | |
1353 | ||
1354 | return 0; | |
1355 | } | |
1356 | ||
1357 | /* | |
1358 | * Try to commit without forking 'git commit'. In some cases we need | |
1359 | * to run 'git commit' to display an error message | |
1360 | * | |
1361 | * Returns: | |
1362 | * -1 - error unable to commit | |
1363 | * 0 - success | |
1364 | * 1 - run 'git commit' | |
1365 | */ | |
f11c9580 NTND |
1366 | static int try_to_commit(struct repository *r, |
1367 | struct strbuf *msg, const char *author, | |
356ee465 PW |
1368 | struct replay_opts *opts, unsigned int flags, |
1369 | struct object_id *oid) | |
1370 | { | |
1371 | struct object_id tree; | |
b0a31861 | 1372 | struct commit *current_head = NULL; |
356ee465 PW |
1373 | struct commit_list *parents = NULL; |
1374 | struct commit_extra_header *extra = NULL; | |
1375 | struct strbuf err = STRBUF_INIT; | |
66618a50 | 1376 | struct strbuf commit_msg = STRBUF_INIT; |
4d924528 | 1377 | char *amend_author = NULL; |
7573cec5 | 1378 | const char *committer = NULL; |
66618a50 | 1379 | const char *hook_commit = NULL; |
356ee465 PW |
1380 | enum commit_msg_cleanup_mode cleanup; |
1381 | int res = 0; | |
1382 | ||
005af339 | 1383 | if (parse_head(r, ¤t_head)) |
356ee465 | 1384 | return -1; |
4d924528 | 1385 | |
356ee465 | 1386 | if (flags & AMEND_MSG) { |
42d4e1d1 | 1387 | const char *exclude_gpgsig[] = { "gpgsig", "gpgsig-sha256", NULL }; |
356ee465 PW |
1388 | const char *out_enc = get_commit_output_encoding(); |
1389 | const char *message = logmsg_reencode(current_head, NULL, | |
1390 | out_enc); | |
1391 | ||
1392 | if (!msg) { | |
1393 | const char *orig_message = NULL; | |
1394 | ||
1395 | find_commit_subject(message, &orig_message); | |
66618a50 | 1396 | msg = &commit_msg; |
356ee465 | 1397 | strbuf_addstr(msg, orig_message); |
66618a50 | 1398 | hook_commit = "HEAD"; |
356ee465 | 1399 | } |
4d924528 | 1400 | author = amend_author = get_author(message); |
356ee465 PW |
1401 | unuse_commit_buffer(current_head, message); |
1402 | if (!author) { | |
1403 | res = error(_("unable to parse commit author")); | |
1404 | goto out; | |
1405 | } | |
1406 | parents = copy_commit_list(current_head->parents); | |
1407 | extra = read_commit_extra_headers(current_head, exclude_gpgsig); | |
b0a31861 PW |
1408 | } else if (current_head && |
1409 | (!(flags & CREATE_ROOT_COMMIT) || (flags & AMEND_MSG))) { | |
356ee465 PW |
1410 | commit_list_insert(current_head, &parents); |
1411 | } | |
1412 | ||
f11c9580 | 1413 | if (write_index_as_tree(&tree, r->index, r->index_file, 0, NULL)) { |
356ee465 PW |
1414 | res = error(_("git write-tree failed to write a tree")); |
1415 | goto out; | |
1416 | } | |
1417 | ||
2d05ef27 PW |
1418 | if (!(flags & ALLOW_EMPTY)) { |
1419 | struct commit *first_parent = current_head; | |
1420 | ||
1421 | if (flags & AMEND_MSG) { | |
1422 | if (current_head->parents) { | |
1423 | first_parent = current_head->parents->item; | |
1424 | if (repo_parse_commit(r, first_parent)) { | |
1425 | res = error(_("could not parse HEAD commit")); | |
1426 | goto out; | |
1427 | } | |
1428 | } else { | |
1429 | first_parent = NULL; | |
1430 | } | |
1431 | } | |
1432 | if (oideq(first_parent | |
1433 | ? get_commit_tree_oid(first_parent) | |
1434 | : the_hash_algo->empty_tree, | |
1435 | &tree)) { | |
1436 | res = 1; /* run 'git commit' to display error message */ | |
1437 | goto out; | |
1438 | } | |
356ee465 PW |
1439 | } |
1440 | ||
66618a50 | 1441 | if (find_hook("prepare-commit-msg")) { |
f11c9580 | 1442 | res = run_prepare_commit_msg_hook(r, msg, hook_commit); |
66618a50 PW |
1443 | if (res) |
1444 | goto out; | |
1445 | if (strbuf_read_file(&commit_msg, git_path_commit_editmsg(), | |
1446 | 2048) < 0) { | |
1447 | res = error_errno(_("unable to read commit message " | |
1448 | "from '%s'"), | |
1449 | git_path_commit_editmsg()); | |
1450 | goto out; | |
1451 | } | |
1452 | msg = &commit_msg; | |
1453 | } | |
1454 | ||
d74f3e58 PW |
1455 | if (flags & CLEANUP_MSG) |
1456 | cleanup = COMMIT_MSG_CLEANUP_ALL; | |
1457 | else if ((opts->signoff || opts->record_origin) && | |
1458 | !opts->explicit_cleanup) | |
1459 | cleanup = COMMIT_MSG_CLEANUP_SPACE; | |
1460 | else | |
1461 | cleanup = opts->default_msg_cleanup; | |
66618a50 PW |
1462 | |
1463 | if (cleanup != COMMIT_MSG_CLEANUP_NONE) | |
1464 | strbuf_stripspace(msg, cleanup == COMMIT_MSG_CLEANUP_ALL); | |
a3ec9eaf | 1465 | if ((flags & EDIT_MSG) && message_is_empty(msg, cleanup)) { |
66618a50 PW |
1466 | res = 1; /* run 'git commit' to display error message */ |
1467 | goto out; | |
1468 | } | |
1469 | ||
7573cec5 PW |
1470 | if (opts->committer_date_is_author_date) { |
1471 | struct ident_split id; | |
1472 | struct strbuf date = STRBUF_INIT; | |
1473 | ||
a3894aad PW |
1474 | if (!opts->ignore_date) { |
1475 | if (split_ident_line(&id, author, (int)strlen(author)) < 0) { | |
1476 | res = error(_("invalid author identity '%s'"), | |
1477 | author); | |
1478 | goto out; | |
1479 | } | |
1480 | if (!id.date_begin) { | |
1481 | res = error(_( | |
1482 | "corrupt author: missing date information")); | |
1483 | goto out; | |
1484 | } | |
1485 | strbuf_addf(&date, "@%.*s %.*s", | |
1486 | (int)(id.date_end - id.date_begin), | |
1487 | id.date_begin, | |
1488 | (int)(id.tz_end - id.tz_begin), | |
1489 | id.tz_begin); | |
1490 | } else { | |
1491 | reset_ident_date(); | |
7573cec5 | 1492 | } |
2020451c JK |
1493 | committer = fmt_ident(getenv("GIT_COMMITTER_NAME"), |
1494 | getenv("GIT_COMMITTER_EMAIL"), | |
a3894aad PW |
1495 | WANT_COMMITTER_IDENT, |
1496 | opts->ignore_date ? NULL : date.buf, | |
7573cec5 PW |
1497 | IDENT_STRICT); |
1498 | strbuf_release(&date); | |
1499 | } else { | |
1500 | reset_ident_date(); | |
1501 | } | |
12f7babd | 1502 | |
a3894aad PW |
1503 | if (opts->ignore_date) { |
1504 | struct ident_split id; | |
1505 | char *name, *email; | |
12f7babd | 1506 | |
a3894aad PW |
1507 | if (split_ident_line(&id, author, strlen(author)) < 0) { |
1508 | error(_("invalid author identity '%s'"), author); | |
1509 | goto out; | |
1510 | } | |
1511 | name = xmemdupz(id.name_begin, id.name_end - id.name_begin); | |
1512 | email = xmemdupz(id.mail_begin, id.mail_end - id.mail_begin); | |
1513 | author = fmt_ident(name, email, WANT_AUTHOR_IDENT, NULL, | |
1514 | IDENT_STRICT); | |
1515 | free(name); | |
1516 | free(email); | |
1517 | } | |
1518 | ||
e8cbe211 | 1519 | if (commit_tree_extended(msg->buf, msg->len, &tree, parents, oid, |
7573cec5 | 1520 | author, committer, opts->gpg_sign, extra)) { |
356ee465 PW |
1521 | res = error(_("failed to write commit object")); |
1522 | goto out; | |
1523 | } | |
1524 | ||
1525 | if (update_head_with_reflog(current_head, oid, | |
1526 | getenv("GIT_REFLOG_ACTION"), msg, &err)) { | |
1527 | res = error("%s", err.buf); | |
1528 | goto out; | |
1529 | } | |
1530 | ||
4627bc77 | 1531 | run_commit_hook(0, r->index_file, "post-commit", NULL); |
356ee465 | 1532 | if (flags & AMEND_MSG) |
1d18d758 | 1533 | commit_post_rewrite(r, current_head, oid); |
356ee465 PW |
1534 | |
1535 | out: | |
1536 | free_commit_extra_headers(extra); | |
1537 | strbuf_release(&err); | |
66618a50 | 1538 | strbuf_release(&commit_msg); |
4d924528 | 1539 | free(amend_author); |
356ee465 PW |
1540 | |
1541 | return res; | |
1542 | } | |
1543 | ||
430b75f7 PW |
1544 | static int write_rebase_head(struct object_id *oid) |
1545 | { | |
1546 | if (update_ref("rebase", "REBASE_HEAD", oid, | |
1547 | NULL, REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR)) | |
1548 | return error(_("could not update %s"), "REBASE_HEAD"); | |
1549 | ||
1550 | return 0; | |
1551 | } | |
1552 | ||
f11c9580 NTND |
1553 | static int do_commit(struct repository *r, |
1554 | const char *msg_file, const char *author, | |
430b75f7 PW |
1555 | struct replay_opts *opts, unsigned int flags, |
1556 | struct object_id *oid) | |
356ee465 PW |
1557 | { |
1558 | int res = 1; | |
1559 | ||
b0a31861 | 1560 | if (!(flags & EDIT_MSG) && !(flags & VERIFY_MSG)) { |
356ee465 PW |
1561 | struct object_id oid; |
1562 | struct strbuf sb = STRBUF_INIT; | |
1563 | ||
1564 | if (msg_file && strbuf_read_file(&sb, msg_file, 2048) < 0) | |
1565 | return error_errno(_("unable to read commit message " | |
1566 | "from '%s'"), | |
1567 | msg_file); | |
1568 | ||
f11c9580 NTND |
1569 | res = try_to_commit(r, msg_file ? &sb : NULL, |
1570 | author, opts, flags, &oid); | |
356ee465 PW |
1571 | strbuf_release(&sb); |
1572 | if (!res) { | |
c8e4159e HWN |
1573 | refs_delete_ref(get_main_ref_store(r), "", |
1574 | "CHERRY_PICK_HEAD", NULL, 0); | |
f11c9580 | 1575 | unlink(git_path_merge_msg(r)); |
356ee465 | 1576 | if (!is_rebase_i(opts)) |
f11c9580 | 1577 | print_commit_summary(r, NULL, &oid, |
356ee465 PW |
1578 | SUMMARY_SHOW_AUTHOR_DATE); |
1579 | return res; | |
1580 | } | |
1581 | } | |
430b75f7 PW |
1582 | if (res == 1) { |
1583 | if (is_rebase_i(opts) && oid) | |
1584 | if (write_rebase_head(oid)) | |
1585 | return -1; | |
20f4b044 | 1586 | return run_git_commit(msg_file, opts, flags); |
430b75f7 | 1587 | } |
356ee465 PW |
1588 | |
1589 | return res; | |
1590 | } | |
1591 | ||
b27cfb0d NH |
1592 | static int is_original_commit_empty(struct commit *commit) |
1593 | { | |
092bbcdf | 1594 | const struct object_id *ptree_oid; |
b27cfb0d NH |
1595 | |
1596 | if (parse_commit(commit)) | |
aee42e1f | 1597 | return error(_("could not parse commit %s"), |
f2fd0760 | 1598 | oid_to_hex(&commit->object.oid)); |
b27cfb0d NH |
1599 | if (commit->parents) { |
1600 | struct commit *parent = commit->parents->item; | |
1601 | if (parse_commit(parent)) | |
aee42e1f | 1602 | return error(_("could not parse parent commit %s"), |
f2fd0760 | 1603 | oid_to_hex(&parent->object.oid)); |
2e27bd77 | 1604 | ptree_oid = get_commit_tree_oid(parent); |
b27cfb0d | 1605 | } else { |
eb0ccfd7 | 1606 | ptree_oid = the_hash_algo->empty_tree; /* commit is root */ |
043a4492 | 1607 | } |
043a4492 | 1608 | |
4a7e27e9 | 1609 | return oideq(ptree_oid, get_commit_tree_oid(commit)); |
043a4492 RR |
1610 | } |
1611 | ||
ac2b0e8f | 1612 | /* |
e98c4269 EN |
1613 | * Should empty commits be allowed? Return status: |
1614 | * <0: Error in is_index_unchanged(r) or is_original_commit_empty(commit) | |
1615 | * 0: Halt on empty commit | |
1616 | * 1: Allow empty commit | |
1617 | * 2: Drop empty commit | |
ac2b0e8f | 1618 | */ |
f11c9580 NTND |
1619 | static int allow_empty(struct repository *r, |
1620 | struct replay_opts *opts, | |
1621 | struct commit *commit) | |
ac2b0e8f | 1622 | { |
d48e5e21 | 1623 | int index_unchanged, originally_empty; |
ac2b0e8f JH |
1624 | |
1625 | /* | |
e98c4269 | 1626 | * Four cases: |
ac2b0e8f JH |
1627 | * |
1628 | * (1) we do not allow empty at all and error out. | |
1629 | * | |
e98c4269 EN |
1630 | * (2) we allow ones that were initially empty, and |
1631 | * just drop the ones that become empty | |
ac2b0e8f | 1632 | * |
e98c4269 EN |
1633 | * (3) we allow ones that were initially empty, but |
1634 | * halt for the ones that become empty; | |
1635 | * | |
1636 | * (4) we allow both. | |
ac2b0e8f JH |
1637 | */ |
1638 | if (!opts->allow_empty) | |
1639 | return 0; /* let "git commit" barf as necessary */ | |
1640 | ||
005af339 | 1641 | index_unchanged = is_index_unchanged(r); |
ac2b0e8f JH |
1642 | if (index_unchanged < 0) |
1643 | return index_unchanged; | |
1644 | if (!index_unchanged) | |
1645 | return 0; /* we do not have to say --allow-empty */ | |
1646 | ||
1647 | if (opts->keep_redundant_commits) | |
1648 | return 1; | |
1649 | ||
d48e5e21 EN |
1650 | originally_empty = is_original_commit_empty(commit); |
1651 | if (originally_empty < 0) | |
1652 | return originally_empty; | |
e98c4269 | 1653 | if (originally_empty) |
ac2b0e8f | 1654 | return 1; |
e98c4269 EN |
1655 | else if (opts->drop_redundant_commits) |
1656 | return 2; | |
1657 | else | |
1658 | return 0; | |
ac2b0e8f JH |
1659 | } |
1660 | ||
414697a9 JS |
1661 | static struct { |
1662 | char c; | |
1663 | const char *str; | |
1664 | } todo_command_info[] = { | |
1665 | { 'p', "pick" }, | |
1666 | { 0, "revert" }, | |
1667 | { 'e', "edit" }, | |
04efc8b5 | 1668 | { 'r', "reword" }, |
414697a9 JS |
1669 | { 'f', "fixup" }, |
1670 | { 's', "squash" }, | |
1671 | { 'x', "exec" }, | |
71f82465 | 1672 | { 'b', "break" }, |
9055e401 JS |
1673 | { 'l', "label" }, |
1674 | { 't', "reset" }, | |
4c68e7dd | 1675 | { 'm', "merge" }, |
b3fdd581 | 1676 | { 0, "noop" }, |
ac191470 JS |
1677 | { 'd', "drop" }, |
1678 | { 0, NULL } | |
004fefa7 JS |
1679 | }; |
1680 | ||
1681 | static const char *command_to_string(const enum todo_command command) | |
1682 | { | |
ac191470 | 1683 | if (command < TODO_COMMENT) |
414697a9 | 1684 | return todo_command_info[command].str; |
02127c63 | 1685 | die(_("unknown command: %d"), command); |
004fefa7 JS |
1686 | } |
1687 | ||
ee5462d6 | 1688 | static char command_to_char(const enum todo_command command) |
d8ae6c84 | 1689 | { |
68e7090f | 1690 | if (command < TODO_COMMENT) |
d8ae6c84 LB |
1691 | return todo_command_info[command].c; |
1692 | return comment_line_char; | |
1693 | } | |
1694 | ||
25c43667 JS |
1695 | static int is_noop(const enum todo_command command) |
1696 | { | |
b3fdd581 | 1697 | return TODO_NOOP <= command; |
25c43667 | 1698 | } |
004fefa7 | 1699 | |
6e98de72 JS |
1700 | static int is_fixup(enum todo_command command) |
1701 | { | |
1702 | return command == TODO_FIXUP || command == TODO_SQUASH; | |
1703 | } | |
1704 | ||
d87d48b2 JS |
1705 | /* Does this command create a (non-merge) commit? */ |
1706 | static int is_pick_or_similar(enum todo_command command) | |
1707 | { | |
1708 | switch (command) { | |
1709 | case TODO_PICK: | |
1710 | case TODO_REVERT: | |
1711 | case TODO_EDIT: | |
1712 | case TODO_REWORD: | |
1713 | case TODO_FIXUP: | |
1714 | case TODO_SQUASH: | |
1715 | return 1; | |
1716 | default: | |
1717 | return 0; | |
1718 | } | |
1719 | } | |
1720 | ||
005af339 NTND |
1721 | static int update_squash_messages(struct repository *r, |
1722 | enum todo_command command, | |
1723 | struct commit *commit, | |
1724 | struct replay_opts *opts) | |
6e98de72 JS |
1725 | { |
1726 | struct strbuf buf = STRBUF_INIT; | |
e12a7ef5 | 1727 | int res; |
6e98de72 | 1728 | const char *message, *body; |
b3757442 | 1729 | const char *encoding = get_commit_output_encoding(); |
6e98de72 | 1730 | |
e12a7ef5 | 1731 | if (opts->current_fixup_count > 0) { |
6e98de72 | 1732 | struct strbuf header = STRBUF_INIT; |
e12a7ef5 | 1733 | char *eol; |
6e98de72 | 1734 | |
e12a7ef5 | 1735 | if (strbuf_read_file(&buf, rebase_path_squash_msg(), 9) <= 0) |
6e98de72 JS |
1736 | return error(_("could not read '%s'"), |
1737 | rebase_path_squash_msg()); | |
1738 | ||
e12a7ef5 JS |
1739 | eol = buf.buf[0] != comment_line_char ? |
1740 | buf.buf : strchrnul(buf.buf, '\n'); | |
6e98de72 JS |
1741 | |
1742 | strbuf_addf(&header, "%c ", comment_line_char); | |
e12a7ef5 JS |
1743 | strbuf_addf(&header, _("This is a combination of %d commits."), |
1744 | opts->current_fixup_count + 2); | |
6e98de72 JS |
1745 | strbuf_splice(&buf, 0, eol - buf.buf, header.buf, header.len); |
1746 | strbuf_release(&header); | |
1747 | } else { | |
33d66df3 | 1748 | struct object_id head; |
6e98de72 JS |
1749 | struct commit *head_commit; |
1750 | const char *head_message, *body; | |
1751 | ||
33d66df3 | 1752 | if (get_oid("HEAD", &head)) |
6e98de72 | 1753 | return error(_("need a HEAD to fixup")); |
005af339 | 1754 | if (!(head_commit = lookup_commit_reference(r, &head))) |
6e98de72 | 1755 | return error(_("could not read HEAD")); |
b3757442 | 1756 | if (!(head_message = logmsg_reencode(head_commit, NULL, encoding))) |
6e98de72 JS |
1757 | return error(_("could not read HEAD's commit message")); |
1758 | ||
1759 | find_commit_subject(head_message, &body); | |
1760 | if (write_message(body, strlen(body), | |
1761 | rebase_path_fixup_msg(), 0)) { | |
1762 | unuse_commit_buffer(head_commit, head_message); | |
1763 | return error(_("cannot write '%s'"), | |
1764 | rebase_path_fixup_msg()); | |
1765 | } | |
1766 | ||
6e98de72 | 1767 | strbuf_addf(&buf, "%c ", comment_line_char); |
e12a7ef5 | 1768 | strbuf_addf(&buf, _("This is a combination of %d commits."), 2); |
6e98de72 JS |
1769 | strbuf_addf(&buf, "\n%c ", comment_line_char); |
1770 | strbuf_addstr(&buf, _("This is the 1st commit message:")); | |
1771 | strbuf_addstr(&buf, "\n\n"); | |
1772 | strbuf_addstr(&buf, body); | |
1773 | ||
1774 | unuse_commit_buffer(head_commit, head_message); | |
1775 | } | |
1776 | ||
b3757442 | 1777 | if (!(message = logmsg_reencode(commit, NULL, encoding))) |
6e98de72 JS |
1778 | return error(_("could not read commit message of %s"), |
1779 | oid_to_hex(&commit->object.oid)); | |
1780 | find_commit_subject(message, &body); | |
1781 | ||
1782 | if (command == TODO_SQUASH) { | |
1783 | unlink(rebase_path_fixup_msg()); | |
1784 | strbuf_addf(&buf, "\n%c ", comment_line_char); | |
e12a7ef5 | 1785 | strbuf_addf(&buf, _("This is the commit message #%d:"), |
dd2e36eb | 1786 | ++opts->current_fixup_count + 1); |
6e98de72 JS |
1787 | strbuf_addstr(&buf, "\n\n"); |
1788 | strbuf_addstr(&buf, body); | |
1789 | } else if (command == TODO_FIXUP) { | |
1790 | strbuf_addf(&buf, "\n%c ", comment_line_char); | |
1791 | strbuf_addf(&buf, _("The commit message #%d will be skipped:"), | |
dd2e36eb | 1792 | ++opts->current_fixup_count + 1); |
6e98de72 JS |
1793 | strbuf_addstr(&buf, "\n\n"); |
1794 | strbuf_add_commented_lines(&buf, body, strlen(body)); | |
1795 | } else | |
1796 | return error(_("unknown command: %d"), command); | |
1797 | unuse_commit_buffer(commit, message); | |
1798 | ||
1799 | res = write_message(buf.buf, buf.len, rebase_path_squash_msg(), 0); | |
1800 | strbuf_release(&buf); | |
e12a7ef5 JS |
1801 | |
1802 | if (!res) { | |
1803 | strbuf_addf(&opts->current_fixups, "%s%s %s", | |
1804 | opts->current_fixups.len ? "\n" : "", | |
1805 | command_to_string(command), | |
1806 | oid_to_hex(&commit->object.oid)); | |
1807 | res = write_message(opts->current_fixups.buf, | |
1808 | opts->current_fixups.len, | |
1809 | rebase_path_current_fixups(), 0); | |
1810 | } | |
1811 | ||
6e98de72 JS |
1812 | return res; |
1813 | } | |
1814 | ||
3b335762 NTND |
1815 | static void flush_rewritten_pending(void) |
1816 | { | |
25cb8df9 | 1817 | struct strbuf buf = STRBUF_INIT; |
092bbcdf | 1818 | struct object_id newoid; |
25cb8df9 JS |
1819 | FILE *out; |
1820 | ||
092bbcdf | 1821 | if (strbuf_read_file(&buf, rebase_path_rewritten_pending(), (GIT_MAX_HEXSZ + 1) * 2) > 0 && |
1822 | !get_oid("HEAD", &newoid) && | |
e9d983f1 | 1823 | (out = fopen_or_warn(rebase_path_rewritten_list(), "a"))) { |
25cb8df9 JS |
1824 | char *bol = buf.buf, *eol; |
1825 | ||
1826 | while (*bol) { | |
1827 | eol = strchrnul(bol, '\n'); | |
1828 | fprintf(out, "%.*s %s\n", (int)(eol - bol), | |
092bbcdf | 1829 | bol, oid_to_hex(&newoid)); |
25cb8df9 JS |
1830 | if (!*eol) |
1831 | break; | |
1832 | bol = eol + 1; | |
1833 | } | |
1834 | fclose(out); | |
1835 | unlink(rebase_path_rewritten_pending()); | |
1836 | } | |
1837 | strbuf_release(&buf); | |
1838 | } | |
1839 | ||
1840 | static void record_in_rewritten(struct object_id *oid, | |
3b335762 NTND |
1841 | enum todo_command next_command) |
1842 | { | |
e9d983f1 | 1843 | FILE *out = fopen_or_warn(rebase_path_rewritten_pending(), "a"); |
25cb8df9 JS |
1844 | |
1845 | if (!out) | |
1846 | return; | |
1847 | ||
1848 | fprintf(out, "%s\n", oid_to_hex(oid)); | |
1849 | fclose(out); | |
1850 | ||
1851 | if (!is_fixup(next_command)) | |
1852 | flush_rewritten_pending(); | |
1853 | } | |
1854 | ||
f11c9580 NTND |
1855 | static int do_pick_commit(struct repository *r, |
1856 | enum todo_command command, | |
1857 | struct commit *commit, | |
1858 | struct replay_opts *opts, | |
a47ba3c7 | 1859 | int final_fixup, int *check_todo) |
043a4492 | 1860 | { |
789b3eff | 1861 | unsigned int flags = opts->edit ? EDIT_MSG : 0; |
f11c9580 | 1862 | const char *msg_file = opts->edit ? NULL : git_path_merge_msg(r); |
ace976b2 | 1863 | struct object_id head; |
043a4492 RR |
1864 | struct commit *base, *next, *parent; |
1865 | const char *base_label, *next_label; | |
356ee465 | 1866 | char *author = NULL; |
d74a4e57 | 1867 | struct commit_message msg = { NULL, NULL, NULL, NULL }; |
043a4492 | 1868 | struct strbuf msgbuf = STRBUF_INIT; |
e98c4269 | 1869 | int res, unborn = 0, reword = 0, allow, drop_commit; |
043a4492 RR |
1870 | |
1871 | if (opts->no_commit) { | |
1872 | /* | |
1873 | * We do not intend to commit immediately. We just want to | |
1874 | * merge the differences in, so let's compute the tree | |
1875 | * that represents the "current" state for merge-recursive | |
1876 | * to work on. | |
1877 | */ | |
f11c9580 | 1878 | if (write_index_as_tree(&head, r->index, r->index_file, 0, NULL)) |
93b3df6f | 1879 | return error(_("your index file is unmerged.")); |
043a4492 | 1880 | } else { |
ace976b2 | 1881 | unborn = get_oid("HEAD", &head); |
d87d48b2 JS |
1882 | /* Do we want to generate a root commit? */ |
1883 | if (is_pick_or_similar(command) && opts->have_squash_onto && | |
4a7e27e9 | 1884 | oideq(&head, &opts->squash_onto)) { |
d87d48b2 JS |
1885 | if (is_fixup(command)) |
1886 | return error(_("cannot fixup root commit")); | |
1887 | flags |= CREATE_ROOT_COMMIT; | |
1888 | unborn = 1; | |
1889 | } else if (unborn) | |
eb0ccfd7 | 1890 | oidcpy(&head, the_hash_algo->empty_tree); |
ffc00a48 | 1891 | if (index_differs_from(r, unborn ? empty_tree_oid_hex() : "HEAD", |
02f2f56b | 1892 | NULL, 0)) |
e1ff0a32 | 1893 | return error_dirty_index(r, opts); |
043a4492 | 1894 | } |
f11c9580 | 1895 | discard_index(r->index); |
043a4492 | 1896 | |
637666c8 | 1897 | if (!commit->parents) |
043a4492 | 1898 | parent = NULL; |
043a4492 RR |
1899 | else if (commit->parents->next) { |
1900 | /* Reverting or cherry-picking a merge commit */ | |
1901 | int cnt; | |
1902 | struct commit_list *p; | |
1903 | ||
1904 | if (!opts->mainline) | |
93b3df6f | 1905 | return error(_("commit %s is a merge but no -m option was given."), |
f2fd0760 | 1906 | oid_to_hex(&commit->object.oid)); |
043a4492 RR |
1907 | |
1908 | for (cnt = 1, p = commit->parents; | |
1909 | cnt != opts->mainline && p; | |
1910 | cnt++) | |
1911 | p = p->next; | |
1912 | if (cnt != opts->mainline || !p) | |
93b3df6f | 1913 | return error(_("commit %s does not have parent %d"), |
f2fd0760 | 1914 | oid_to_hex(&commit->object.oid), opts->mainline); |
043a4492 | 1915 | parent = p->item; |
37897bfc SO |
1916 | } else if (1 < opts->mainline) |
1917 | /* | |
1918 | * Non-first parent explicitly specified as mainline for | |
1919 | * non-merge commit | |
1920 | */ | |
1921 | return error(_("commit %s does not have parent %d"), | |
1922 | oid_to_hex(&commit->object.oid), opts->mainline); | |
043a4492 RR |
1923 | else |
1924 | parent = commit->parents->item; | |
1925 | ||
bcbb68be JS |
1926 | if (get_message(commit, &msg) != 0) |
1927 | return error(_("cannot get commit message for %s"), | |
1928 | oid_to_hex(&commit->object.oid)); | |
1929 | ||
6e98de72 | 1930 | if (opts->allow_ff && !is_fixup(command) && |
4a7e27e9 | 1931 | ((parent && oideq(&parent->object.oid, &head)) || |
bcbb68be JS |
1932 | (!parent && unborn))) { |
1933 | if (is_rebase_i(opts)) | |
1934 | write_author_script(msg.message); | |
f11c9580 | 1935 | res = fast_forward_to(r, &commit->object.oid, &head, unborn, |
bcbb68be JS |
1936 | opts); |
1937 | if (res || command != TODO_REWORD) | |
1938 | goto leave; | |
450efe2d | 1939 | reword = 1; |
bcbb68be JS |
1940 | msg_file = NULL; |
1941 | goto fast_forward_edit; | |
1942 | } | |
043a4492 | 1943 | if (parent && parse_commit(parent) < 0) |
004fefa7 JS |
1944 | /* TRANSLATORS: The first %s will be a "todo" command like |
1945 | "revert" or "pick", the second %s a SHA1. */ | |
043a4492 | 1946 | return error(_("%s: cannot parse parent commit %s"), |
004fefa7 JS |
1947 | command_to_string(command), |
1948 | oid_to_hex(&parent->object.oid)); | |
043a4492 | 1949 | |
043a4492 RR |
1950 | /* |
1951 | * "commit" is an existing commit. We would want to apply | |
1952 | * the difference it introduces since its first parent "prev" | |
1953 | * on top of the current HEAD if we are cherry-pick. Or the | |
1954 | * reverse of it if we are revert. | |
1955 | */ | |
1956 | ||
004fefa7 | 1957 | if (command == TODO_REVERT) { |
043a4492 RR |
1958 | base = commit; |
1959 | base_label = msg.label; | |
1960 | next = parent; | |
1961 | next_label = msg.parent_label; | |
1962 | strbuf_addstr(&msgbuf, "Revert \""); | |
1963 | strbuf_addstr(&msgbuf, msg.subject); | |
1964 | strbuf_addstr(&msgbuf, "\"\n\nThis reverts commit "); | |
f2fd0760 | 1965 | strbuf_addstr(&msgbuf, oid_to_hex(&commit->object.oid)); |
043a4492 RR |
1966 | |
1967 | if (commit->parents && commit->parents->next) { | |
1968 | strbuf_addstr(&msgbuf, ", reversing\nchanges made to "); | |
f2fd0760 | 1969 | strbuf_addstr(&msgbuf, oid_to_hex(&parent->object.oid)); |
043a4492 RR |
1970 | } |
1971 | strbuf_addstr(&msgbuf, ".\n"); | |
1972 | } else { | |
1973 | const char *p; | |
1974 | ||
1975 | base = parent; | |
1976 | base_label = msg.parent_label; | |
1977 | next = commit; | |
1978 | next_label = msg.label; | |
1979 | ||
23aa5142 JS |
1980 | /* Append the commit log message to msgbuf. */ |
1981 | if (find_commit_subject(msg.message, &p)) | |
1982 | strbuf_addstr(&msgbuf, p); | |
043a4492 RR |
1983 | |
1984 | if (opts->record_origin) { | |
44dc738a | 1985 | strbuf_complete_line(&msgbuf); |
bab4d109 | 1986 | if (!has_conforming_footer(&msgbuf, NULL, 0)) |
b971e04f | 1987 | strbuf_addch(&msgbuf, '\n'); |
cd650a4e | 1988 | strbuf_addstr(&msgbuf, cherry_picked_prefix); |
f2fd0760 | 1989 | strbuf_addstr(&msgbuf, oid_to_hex(&commit->object.oid)); |
043a4492 RR |
1990 | strbuf_addstr(&msgbuf, ")\n"); |
1991 | } | |
356ee465 PW |
1992 | if (!is_fixup(command)) |
1993 | author = get_author(msg.message); | |
043a4492 RR |
1994 | } |
1995 | ||
04efc8b5 | 1996 | if (command == TODO_REWORD) |
450efe2d | 1997 | reword = 1; |
04efc8b5 | 1998 | else if (is_fixup(command)) { |
005af339 | 1999 | if (update_squash_messages(r, command, commit, opts)) |
6e98de72 | 2000 | return -1; |
789b3eff | 2001 | flags |= AMEND_MSG; |
6e98de72 JS |
2002 | if (!final_fixup) |
2003 | msg_file = rebase_path_squash_msg(); | |
2004 | else if (file_exists(rebase_path_fixup_msg())) { | |
789b3eff | 2005 | flags |= CLEANUP_MSG; |
6e98de72 JS |
2006 | msg_file = rebase_path_fixup_msg(); |
2007 | } else { | |
f11c9580 | 2008 | const char *dest = git_path_squash_msg(r); |
6e98de72 JS |
2009 | unlink(dest); |
2010 | if (copy_file(dest, rebase_path_squash_msg(), 0666)) | |
2011 | return error(_("could not rename '%s' to '%s'"), | |
2012 | rebase_path_squash_msg(), dest); | |
f11c9580 | 2013 | unlink(git_path_merge_msg(r)); |
6e98de72 | 2014 | msg_file = dest; |
789b3eff | 2015 | flags |= EDIT_MSG; |
6e98de72 JS |
2016 | } |
2017 | } | |
2018 | ||
a852ec7f | 2019 | if (opts->signoff && !is_fixup(command)) |
b34eeea3 PW |
2020 | append_signoff(&msgbuf, 0, 0); |
2021 | ||
0473f28a JS |
2022 | if (is_rebase_i(opts) && write_author_script(msg.message) < 0) |
2023 | res = -1; | |
14c4586c EN |
2024 | else if (!opts->strategy || |
2025 | !strcmp(opts->strategy, "recursive") || | |
2026 | !strcmp(opts->strategy, "ort") || | |
2027 | command == TODO_REVERT) { | |
f11c9580 | 2028 | res = do_recursive_merge(r, base, next, base_label, next_label, |
48be4c62 | 2029 | &head, &msgbuf, opts); |
f241ff0d | 2030 | if (res < 0) |
19517fb9 SB |
2031 | goto leave; |
2032 | ||
75871495 | 2033 | res |= write_message(msgbuf.buf, msgbuf.len, |
f11c9580 | 2034 | git_path_merge_msg(r), 0); |
043a4492 RR |
2035 | } else { |
2036 | struct commit_list *common = NULL; | |
2037 | struct commit_list *remotes = NULL; | |
2038 | ||
75871495 | 2039 | res = write_message(msgbuf.buf, msgbuf.len, |
f11c9580 | 2040 | git_path_merge_msg(r), 0); |
043a4492 RR |
2041 | |
2042 | commit_list_insert(base, &common); | |
2043 | commit_list_insert(next, &remotes); | |
f11c9580 | 2044 | res |= try_merge_command(r, opts->strategy, |
03a4e260 | 2045 | opts->xopts_nr, (const char **)opts->xopts, |
ace976b2 | 2046 | common, oid_to_hex(&head), remotes); |
043a4492 RR |
2047 | free_commit_list(common); |
2048 | free_commit_list(remotes); | |
2049 | } | |
452202c7 | 2050 | strbuf_release(&msgbuf); |
043a4492 RR |
2051 | |
2052 | /* | |
2053 | * If the merge was clean or if it failed due to conflict, we write | |
2054 | * CHERRY_PICK_HEAD for the subsequent invocation of commit to use. | |
2055 | * However, if the merge did not even start, then we don't want to | |
2056 | * write it at all. | |
2057 | */ | |
21b11c6d PW |
2058 | if ((command == TODO_PICK || command == TODO_REWORD || |
2059 | command == TODO_EDIT) && !opts->no_commit && | |
2060 | (res == 0 || res == 1) && | |
ae077771 | 2061 | update_ref(NULL, "CHERRY_PICK_HEAD", &commit->object.oid, NULL, |
91774afc | 2062 | REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR)) |
dbfad033 | 2063 | res = -1; |
004fefa7 | 2064 | if (command == TODO_REVERT && ((opts->no_commit && res == 0) || res == 1) && |
ae077771 | 2065 | update_ref(NULL, "REVERT_HEAD", &commit->object.oid, NULL, |
91774afc | 2066 | REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR)) |
dbfad033 | 2067 | res = -1; |
043a4492 RR |
2068 | |
2069 | if (res) { | |
004fefa7 | 2070 | error(command == TODO_REVERT |
043a4492 RR |
2071 | ? _("could not revert %s... %s") |
2072 | : _("could not apply %s... %s"), | |
39755964 | 2073 | short_commit_name(commit), msg.subject); |
005af339 | 2074 | print_advice(r, res == 1, opts); |
f11c9580 | 2075 | repo_rerere(r, opts->allow_rerere_auto); |
c8d1351d | 2076 | goto leave; |
043a4492 RR |
2077 | } |
2078 | ||
e98c4269 | 2079 | drop_commit = 0; |
f11c9580 | 2080 | allow = allow_empty(r, opts, commit); |
706728a3 FC |
2081 | if (allow < 0) { |
2082 | res = allow; | |
2083 | goto leave; | |
e98c4269 | 2084 | } else if (allow == 1) { |
789b3eff | 2085 | flags |= ALLOW_EMPTY; |
e98c4269 EN |
2086 | } else if (allow == 2) { |
2087 | drop_commit = 1; | |
c8e4159e HWN |
2088 | refs_delete_ref(get_main_ref_store(r), "", "CHERRY_PICK_HEAD", |
2089 | NULL, 0); | |
9a1b7474 | 2090 | unlink(git_path_merge_msg(r)); |
e98c4269 EN |
2091 | fprintf(stderr, |
2092 | _("dropping %s %s -- patch contents already upstream\n"), | |
2093 | oid_to_hex(&commit->object.oid), msg.subject); | |
2094 | } /* else allow == 0 and there's nothing special to do */ | |
2095 | if (!opts->no_commit && !drop_commit) { | |
356ee465 | 2096 | if (author || command == TODO_REVERT || (flags & AMEND_MSG)) |
430b75f7 PW |
2097 | res = do_commit(r, msg_file, author, opts, flags, |
2098 | commit? &commit->object.oid : NULL); | |
356ee465 PW |
2099 | else |
2100 | res = error(_("unable to parse commit author")); | |
a47ba3c7 PW |
2101 | *check_todo = !!(flags & EDIT_MSG); |
2102 | if (!res && reword) { | |
450efe2d | 2103 | fast_forward_edit: |
20f4b044 | 2104 | res = run_git_commit(NULL, opts, EDIT_MSG | |
450efe2d PW |
2105 | VERIFY_MSG | AMEND_MSG | |
2106 | (flags & ALLOW_EMPTY)); | |
a47ba3c7 PW |
2107 | *check_todo = 1; |
2108 | } | |
356ee465 | 2109 | } |
6e98de72 | 2110 | |
450efe2d | 2111 | |
6e98de72 JS |
2112 | if (!res && final_fixup) { |
2113 | unlink(rebase_path_fixup_msg()); | |
2114 | unlink(rebase_path_squash_msg()); | |
e12a7ef5 JS |
2115 | unlink(rebase_path_current_fixups()); |
2116 | strbuf_reset(&opts->current_fixups); | |
2117 | opts->current_fixup_count = 0; | |
6e98de72 | 2118 | } |
c8d1351d FC |
2119 | |
2120 | leave: | |
d74a4e57 | 2121 | free_message(commit, &msg); |
356ee465 | 2122 | free(author); |
1e41229d | 2123 | update_abort_safety_file(); |
043a4492 RR |
2124 | |
2125 | return res; | |
2126 | } | |
2127 | ||
c3e8618c | 2128 | static int prepare_revs(struct replay_opts *opts) |
043a4492 | 2129 | { |
a73e22e9 MZ |
2130 | /* |
2131 | * picking (but not reverting) ranges (but not individual revisions) | |
2132 | * should be done in reverse | |
2133 | */ | |
2134 | if (opts->action == REPLAY_PICK && !opts->revs->no_walk) | |
043a4492 RR |
2135 | opts->revs->reverse ^= 1; |
2136 | ||
2137 | if (prepare_revision_walk(opts->revs)) | |
c3e8618c | 2138 | return error(_("revision walk setup failed")); |
043a4492 | 2139 | |
c3e8618c | 2140 | return 0; |
043a4492 RR |
2141 | } |
2142 | ||
f11c9580 NTND |
2143 | static int read_and_refresh_cache(struct repository *r, |
2144 | struct replay_opts *opts) | |
043a4492 | 2145 | { |
14bca6c6 | 2146 | struct lock_file index_lock = LOCK_INIT; |
3a95f31d NTND |
2147 | int index_fd = repo_hold_locked_index(r, &index_lock, 0); |
2148 | if (repo_read_index(r) < 0) { | |
49fb937e | 2149 | rollback_lock_file(&index_lock); |
0d9c6dc9 | 2150 | return error(_("git %s: failed to read the index"), |
c28cbc5e | 2151 | _(action_name(opts))); |
49fb937e | 2152 | } |
f11c9580 | 2153 | refresh_index(r->index, REFRESH_QUIET|REFRESH_UNMERGED, NULL, NULL, NULL); |
61000814 | 2154 | if (index_fd >= 0) { |
f11c9580 | 2155 | if (write_locked_index(r->index, &index_lock, |
61000814 | 2156 | COMMIT_LOCK | SKIP_IF_UNCHANGED)) { |
0d9c6dc9 | 2157 | return error(_("git %s: failed to refresh the index"), |
c28cbc5e | 2158 | _(action_name(opts))); |
49fb937e | 2159 | } |
043a4492 | 2160 | } |
0d9c6dc9 | 2161 | return 0; |
043a4492 RR |
2162 | } |
2163 | ||
4c68e7dd JS |
2164 | enum todo_item_flags { |
2165 | TODO_EDIT_MERGE_MSG = 1 | |
2166 | }; | |
2167 | ||
5d94d545 | 2168 | void todo_list_release(struct todo_list *todo_list) |
043a4492 | 2169 | { |
004fefa7 | 2170 | strbuf_release(&todo_list->buf); |
6a83d902 | 2171 | FREE_AND_NULL(todo_list->items); |
004fefa7 JS |
2172 | todo_list->nr = todo_list->alloc = 0; |
2173 | } | |
043a4492 | 2174 | |
004fefa7 JS |
2175 | static struct todo_item *append_new_todo(struct todo_list *todo_list) |
2176 | { | |
2177 | ALLOC_GROW(todo_list->items, todo_list->nr + 1, todo_list->alloc); | |
8638114e | 2178 | todo_list->total_nr++; |
004fefa7 | 2179 | return todo_list->items + todo_list->nr++; |
043a4492 RR |
2180 | } |
2181 | ||
6ad656db AG |
2182 | const char *todo_item_get_arg(struct todo_list *todo_list, |
2183 | struct todo_item *item) | |
2184 | { | |
2185 | return todo_list->buf.buf + item->arg_offset; | |
2186 | } | |
2187 | ||
3e81bccd PW |
2188 | static int is_command(enum todo_command command, const char **bol) |
2189 | { | |
2190 | const char *str = todo_command_info[command].str; | |
2191 | const char nick = todo_command_info[command].c; | |
2192 | const char *p = *bol + 1; | |
2193 | ||
2194 | return skip_prefix(*bol, str, bol) || | |
2195 | ((nick && **bol == nick) && | |
2196 | (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r' || !*p) && | |
2197 | (*bol = p)); | |
2198 | } | |
2199 | ||
005af339 | 2200 | static int parse_insn_line(struct repository *r, struct todo_item *item, |
6ad656db | 2201 | const char *buf, const char *bol, char *eol) |
043a4492 | 2202 | { |
1e43ed98 | 2203 | struct object_id commit_oid; |
043a4492 | 2204 | char *end_of_object_name; |
004fefa7 JS |
2205 | int i, saved, status, padding; |
2206 | ||
4c68e7dd JS |
2207 | item->flags = 0; |
2208 | ||
8f8550b3 JS |
2209 | /* left-trim */ |
2210 | bol += strspn(bol, " \t"); | |
2211 | ||
25c43667 | 2212 | if (bol == eol || *bol == '\r' || *bol == comment_line_char) { |
ac191470 | 2213 | item->command = TODO_COMMENT; |
25c43667 | 2214 | item->commit = NULL; |
6ad656db | 2215 | item->arg_offset = bol - buf; |
25c43667 JS |
2216 | item->arg_len = eol - bol; |
2217 | return 0; | |
2218 | } | |
2219 | ||
ac191470 | 2220 | for (i = 0; i < TODO_COMMENT; i++) |
3e81bccd | 2221 | if (is_command(i, &bol)) { |
004fefa7 JS |
2222 | item->command = i; |
2223 | break; | |
2224 | } | |
ac191470 | 2225 | if (i >= TODO_COMMENT) |
004fefa7 | 2226 | return -1; |
043a4492 | 2227 | |
66afa24f JS |
2228 | /* Eat up extra spaces/ tabs before object name */ |
2229 | padding = strspn(bol, " \t"); | |
2230 | bol += padding; | |
2231 | ||
71f82465 | 2232 | if (item->command == TODO_NOOP || item->command == TODO_BREAK) { |
66afa24f JS |
2233 | if (bol != eol) |
2234 | return error(_("%s does not accept arguments: '%s'"), | |
2235 | command_to_string(item->command), bol); | |
25c43667 | 2236 | item->commit = NULL; |
6ad656db | 2237 | item->arg_offset = bol - buf; |
25c43667 JS |
2238 | item->arg_len = eol - bol; |
2239 | return 0; | |
2240 | } | |
2241 | ||
043a4492 | 2242 | if (!padding) |
66afa24f JS |
2243 | return error(_("missing arguments for %s"), |
2244 | command_to_string(item->command)); | |
043a4492 | 2245 | |
9055e401 JS |
2246 | if (item->command == TODO_EXEC || item->command == TODO_LABEL || |
2247 | item->command == TODO_RESET) { | |
7dcbb3cb | 2248 | item->commit = NULL; |
6ad656db | 2249 | item->arg_offset = bol - buf; |
311af526 JS |
2250 | item->arg_len = (int)(eol - bol); |
2251 | return 0; | |
2252 | } | |
2253 | ||
4c68e7dd JS |
2254 | if (item->command == TODO_MERGE) { |
2255 | if (skip_prefix(bol, "-C", &bol)) | |
2256 | bol += strspn(bol, " \t"); | |
2257 | else if (skip_prefix(bol, "-c", &bol)) { | |
2258 | bol += strspn(bol, " \t"); | |
2259 | item->flags |= TODO_EDIT_MERGE_MSG; | |
2260 | } else { | |
2261 | item->flags |= TODO_EDIT_MERGE_MSG; | |
2262 | item->commit = NULL; | |
6ad656db | 2263 | item->arg_offset = bol - buf; |
4c68e7dd JS |
2264 | item->arg_len = (int)(eol - bol); |
2265 | return 0; | |
2266 | } | |
2267 | } | |
2268 | ||
004fefa7 | 2269 | end_of_object_name = (char *) bol + strcspn(bol, " \t\n"); |
043a4492 RR |
2270 | saved = *end_of_object_name; |
2271 | *end_of_object_name = '\0'; | |
1e43ed98 | 2272 | status = get_oid(bol, &commit_oid); |
d859dcad JS |
2273 | if (status < 0) |
2274 | error(_("could not parse '%s'"), bol); /* return later */ | |
043a4492 RR |
2275 | *end_of_object_name = saved; |
2276 | ||
6ad656db AG |
2277 | bol = end_of_object_name + strspn(end_of_object_name, " \t"); |
2278 | item->arg_offset = bol - buf; | |
2279 | item->arg_len = (int)(eol - bol); | |
c22f7dfb | 2280 | |
043a4492 | 2281 | if (status < 0) |
d859dcad | 2282 | return status; |
043a4492 | 2283 | |
005af339 | 2284 | item->commit = lookup_commit_reference(r, &commit_oid); |
d859dcad | 2285 | return item->commit ? 0 : -1; |
043a4492 RR |
2286 | } |
2287 | ||
4a72486d PW |
2288 | int sequencer_get_last_command(struct repository *r, enum replay_action *action) |
2289 | { | |
ed5b1ca1 | 2290 | const char *todo_file, *bol; |
4a72486d | 2291 | struct strbuf buf = STRBUF_INIT; |
ed5b1ca1 | 2292 | int ret = 0; |
4a72486d PW |
2293 | |
2294 | todo_file = git_path_todo_file(); | |
2295 | if (strbuf_read_file(&buf, todo_file, 0) < 0) { | |
ed5b1ca1 | 2296 | if (errno == ENOENT || errno == ENOTDIR) |
4a72486d PW |
2297 | return -1; |
2298 | else | |
2299 | return error_errno("unable to open '%s'", todo_file); | |
2300 | } | |
ed5b1ca1 PW |
2301 | bol = buf.buf + strspn(buf.buf, " \t\r\n"); |
2302 | if (is_command(TODO_PICK, &bol) && (*bol == ' ' || *bol == '\t')) | |
4a72486d | 2303 | *action = REPLAY_PICK; |
ed5b1ca1 PW |
2304 | else if (is_command(TODO_REVERT, &bol) && |
2305 | (*bol == ' ' || *bol == '\t')) | |
4a72486d PW |
2306 | *action = REPLAY_REVERT; |
2307 | else | |
ed5b1ca1 | 2308 | ret = -1; |
4a72486d | 2309 | |
4a72486d PW |
2310 | strbuf_release(&buf); |
2311 | ||
2312 | return ret; | |
2313 | } | |
2314 | ||
5d94d545 AG |
2315 | int todo_list_parse_insn_buffer(struct repository *r, char *buf, |
2316 | struct todo_list *todo_list) | |
043a4492 | 2317 | { |
004fefa7 JS |
2318 | struct todo_item *item; |
2319 | char *p = buf, *next_p; | |
6e98de72 | 2320 | int i, res = 0, fixup_okay = file_exists(rebase_path_done()); |
043a4492 | 2321 | |
2b71595d AG |
2322 | todo_list->current = todo_list->nr = 0; |
2323 | ||
004fefa7 | 2324 | for (i = 1; *p; i++, p = next_p) { |
043a4492 | 2325 | char *eol = strchrnul(p, '\n'); |
004fefa7 JS |
2326 | |
2327 | next_p = *eol ? eol + 1 /* skip LF */ : eol; | |
2328 | ||
6307041d JS |
2329 | if (p != eol && eol[-1] == '\r') |
2330 | eol--; /* strip Carriage Return */ | |
2331 | ||
004fefa7 JS |
2332 | item = append_new_todo(todo_list); |
2333 | item->offset_in_buf = p - todo_list->buf.buf; | |
6ad656db | 2334 | if (parse_insn_line(r, item, buf, p, eol)) { |
93b3df6f | 2335 | res = error(_("invalid line %d: %.*s"), |
004fefa7 | 2336 | i, (int)(eol - p), p); |
2b71595d | 2337 | item->command = TODO_COMMENT + 1; |
6ad656db | 2338 | item->arg_offset = p - buf; |
2b71595d AG |
2339 | item->arg_len = (int)(eol - p); |
2340 | item->commit = NULL; | |
004fefa7 | 2341 | } |
6e98de72 JS |
2342 | |
2343 | if (fixup_okay) | |
2344 | ; /* do nothing */ | |
2345 | else if (is_fixup(item->command)) | |
2346 | return error(_("cannot '%s' without a previous commit"), | |
2347 | command_to_string(item->command)); | |
2348 | else if (!is_noop(item->command)) | |
2349 | fixup_okay = 1; | |
043a4492 | 2350 | } |
52865279 | 2351 | |
004fefa7 | 2352 | return res; |
043a4492 RR |
2353 | } |
2354 | ||
968492e4 JS |
2355 | static int count_commands(struct todo_list *todo_list) |
2356 | { | |
2357 | int count = 0, i; | |
2358 | ||
2359 | for (i = 0; i < todo_list->nr; i++) | |
2360 | if (todo_list->items[i].command != TODO_COMMENT) | |
2361 | count++; | |
2362 | ||
2363 | return count; | |
2364 | } | |
2365 | ||
a01c2a5f JS |
2366 | static int get_item_line_offset(struct todo_list *todo_list, int index) |
2367 | { | |
2368 | return index < todo_list->nr ? | |
2369 | todo_list->items[index].offset_in_buf : todo_list->buf.len; | |
2370 | } | |
2371 | ||
2372 | static const char *get_item_line(struct todo_list *todo_list, int index) | |
2373 | { | |
2374 | return todo_list->buf.buf + get_item_line_offset(todo_list, index); | |
2375 | } | |
2376 | ||
2377 | static int get_item_line_length(struct todo_list *todo_list, int index) | |
2378 | { | |
2379 | return get_item_line_offset(todo_list, index + 1) | |
2380 | - get_item_line_offset(todo_list, index); | |
2381 | } | |
2382 | ||
87805600 RS |
2383 | static ssize_t strbuf_read_file_or_whine(struct strbuf *sb, const char *path) |
2384 | { | |
2385 | int fd; | |
2386 | ssize_t len; | |
2387 | ||
2388 | fd = open(path, O_RDONLY); | |
2389 | if (fd < 0) | |
2390 | return error_errno(_("could not open '%s'"), path); | |
2391 | len = strbuf_read(sb, fd, 0); | |
2392 | close(fd); | |
2393 | if (len < 0) | |
2394 | return error(_("could not read '%s'."), path); | |
2395 | return len; | |
2396 | } | |
2397 | ||
b07d9bfd PW |
2398 | static int have_finished_the_last_pick(void) |
2399 | { | |
2400 | struct strbuf buf = STRBUF_INIT; | |
2401 | const char *eol; | |
2402 | const char *todo_path = git_path_todo_file(); | |
2403 | int ret = 0; | |
2404 | ||
2405 | if (strbuf_read_file(&buf, todo_path, 0) < 0) { | |
2406 | if (errno == ENOENT) { | |
2407 | return 0; | |
2408 | } else { | |
2409 | error_errno("unable to open '%s'", todo_path); | |
2410 | return 0; | |
2411 | } | |
2412 | } | |
2413 | /* If there is only one line then we are done */ | |
2414 | eol = strchr(buf.buf, '\n'); | |
2415 | if (!eol || !eol[1]) | |
2416 | ret = 1; | |
2417 | ||
2418 | strbuf_release(&buf); | |
2419 | ||
2420 | return ret; | |
2421 | } | |
2422 | ||
f496b064 | 2423 | void sequencer_post_commit_cleanup(struct repository *r, int verbose) |
b07d9bfd PW |
2424 | { |
2425 | struct replay_opts opts = REPLAY_OPTS_INIT; | |
2426 | int need_cleanup = 0; | |
2427 | ||
c8e4159e HWN |
2428 | if (refs_ref_exists(get_main_ref_store(r), "CHERRY_PICK_HEAD")) { |
2429 | if (!refs_delete_ref(get_main_ref_store(r), "", | |
2430 | "CHERRY_PICK_HEAD", NULL, 0) && | |
2431 | verbose) | |
f496b064 | 2432 | warning(_("cancelling a cherry picking in progress")); |
b07d9bfd PW |
2433 | opts.action = REPLAY_PICK; |
2434 | need_cleanup = 1; | |
2435 | } | |
2436 | ||
b8825ef2 HWN |
2437 | if (refs_ref_exists(get_main_ref_store(r), "REVERT_HEAD")) { |
2438 | if (!refs_delete_ref(get_main_ref_store(r), "", "REVERT_HEAD", | |
2439 | NULL, 0) && | |
2440 | verbose) | |
f496b064 | 2441 | warning(_("cancelling a revert in progress")); |
b07d9bfd PW |
2442 | opts.action = REPLAY_REVERT; |
2443 | need_cleanup = 1; | |
2444 | } | |
2445 | ||
2446 | if (!need_cleanup) | |
2447 | return; | |
2448 | ||
2449 | if (!have_finished_the_last_pick()) | |
2450 | return; | |
2451 | ||
2452 | sequencer_remove_state(&opts); | |
2453 | } | |
2454 | ||
3f34f2d8 AG |
2455 | static void todo_list_write_total_nr(struct todo_list *todo_list) |
2456 | { | |
2457 | FILE *f = fopen_or_warn(rebase_path_msgtotal(), "w"); | |
2458 | ||
2459 | if (f) { | |
2460 | fprintf(f, "%d\n", todo_list->total_nr); | |
2461 | fclose(f); | |
2462 | } | |
2463 | } | |
2464 | ||
005af339 NTND |
2465 | static int read_populate_todo(struct repository *r, |
2466 | struct todo_list *todo_list, | |
2467 | struct replay_opts *opts) | |
043a4492 | 2468 | { |
54fd3243 | 2469 | struct stat st; |
c0246501 | 2470 | const char *todo_file = get_todo_path(opts); |
87805600 | 2471 | int res; |
043a4492 | 2472 | |
004fefa7 | 2473 | strbuf_reset(&todo_list->buf); |
87805600 RS |
2474 | if (strbuf_read_file_or_whine(&todo_list->buf, todo_file) < 0) |
2475 | return -1; | |
043a4492 | 2476 | |
54fd3243 SH |
2477 | res = stat(todo_file, &st); |
2478 | if (res) | |
2479 | return error(_("could not stat '%s'"), todo_file); | |
2480 | fill_stat_data(&todo_list->stat, &st); | |
2481 | ||
5d94d545 | 2482 | res = todo_list_parse_insn_buffer(r, todo_list->buf.buf, todo_list); |
27fdbb96 JS |
2483 | if (res) { |
2484 | if (is_rebase_i(opts)) | |
2485 | return error(_("please fix this using " | |
2486 | "'git rebase --edit-todo'.")); | |
93b3df6f | 2487 | return error(_("unusable instruction sheet: '%s'"), todo_file); |
27fdbb96 | 2488 | } |
2eeaf1b3 | 2489 | |
52865279 JS |
2490 | if (!todo_list->nr && |
2491 | (!is_rebase_i(opts) || !file_exists(rebase_path_done()))) | |
2492 | return error(_("no commits parsed.")); | |
2493 | ||
2eeaf1b3 | 2494 | if (!is_rebase_i(opts)) { |
004fefa7 JS |
2495 | enum todo_command valid = |
2496 | opts->action == REPLAY_PICK ? TODO_PICK : TODO_REVERT; | |
2497 | int i; | |
2498 | ||
2499 | for (i = 0; i < todo_list->nr; i++) | |
2500 | if (valid == todo_list->items[i].command) | |
2501 | continue; | |
2502 | else if (valid == TODO_PICK) | |
93b3df6f | 2503 | return error(_("cannot cherry-pick during a revert.")); |
004fefa7 | 2504 | else |
93b3df6f | 2505 | return error(_("cannot revert during a cherry-pick.")); |
004fefa7 JS |
2506 | } |
2507 | ||
968492e4 JS |
2508 | if (is_rebase_i(opts)) { |
2509 | struct todo_list done = TODO_LIST_INIT; | |
2510 | ||
2511 | if (strbuf_read_file(&done.buf, rebase_path_done(), 0) > 0 && | |
5d94d545 | 2512 | !todo_list_parse_insn_buffer(r, done.buf.buf, &done)) |
968492e4 JS |
2513 | todo_list->done_nr = count_commands(&done); |
2514 | else | |
2515 | todo_list->done_nr = 0; | |
2516 | ||
2517 | todo_list->total_nr = todo_list->done_nr | |
2518 | + count_commands(todo_list); | |
968492e4 | 2519 | todo_list_release(&done); |
ef80069a | 2520 | |
3f34f2d8 | 2521 | todo_list_write_total_nr(todo_list); |
968492e4 JS |
2522 | } |
2523 | ||
0ae42a03 | 2524 | return 0; |
043a4492 RR |
2525 | } |
2526 | ||
03a4e260 JS |
2527 | static int git_config_string_dup(char **dest, |
2528 | const char *var, const char *value) | |
2529 | { | |
2530 | if (!value) | |
2531 | return config_error_nonbool(var); | |
2532 | free(*dest); | |
2533 | *dest = xstrdup(value); | |
2534 | return 0; | |
2535 | } | |
2536 | ||
043a4492 RR |
2537 | static int populate_opts_cb(const char *key, const char *value, void *data) |
2538 | { | |
2539 | struct replay_opts *opts = data; | |
2540 | int error_flag = 1; | |
2541 | ||
2542 | if (!value) | |
2543 | error_flag = 0; | |
2544 | else if (!strcmp(key, "options.no-commit")) | |
2545 | opts->no_commit = git_config_bool_or_int(key, value, &error_flag); | |
2546 | else if (!strcmp(key, "options.edit")) | |
2547 | opts->edit = git_config_bool_or_int(key, value, &error_flag); | |
6860ce5d PW |
2548 | else if (!strcmp(key, "options.allow-empty")) |
2549 | opts->allow_empty = | |
2550 | git_config_bool_or_int(key, value, &error_flag); | |
2551 | else if (!strcmp(key, "options.allow-empty-message")) | |
2552 | opts->allow_empty_message = | |
2553 | git_config_bool_or_int(key, value, &error_flag); | |
2554 | else if (!strcmp(key, "options.keep-redundant-commits")) | |
2555 | opts->keep_redundant_commits = | |
2556 | git_config_bool_or_int(key, value, &error_flag); | |
043a4492 RR |
2557 | else if (!strcmp(key, "options.signoff")) |
2558 | opts->signoff = git_config_bool_or_int(key, value, &error_flag); | |
2559 | else if (!strcmp(key, "options.record-origin")) | |
2560 | opts->record_origin = git_config_bool_or_int(key, value, &error_flag); | |
2561 | else if (!strcmp(key, "options.allow-ff")) | |
2562 | opts->allow_ff = git_config_bool_or_int(key, value, &error_flag); | |
2563 | else if (!strcmp(key, "options.mainline")) | |
2564 | opts->mainline = git_config_int(key, value); | |
2565 | else if (!strcmp(key, "options.strategy")) | |
03a4e260 | 2566 | git_config_string_dup(&opts->strategy, key, value); |
3253553e | 2567 | else if (!strcmp(key, "options.gpg-sign")) |
03a4e260 | 2568 | git_config_string_dup(&opts->gpg_sign, key, value); |
043a4492 RR |
2569 | else if (!strcmp(key, "options.strategy-option")) { |
2570 | ALLOC_GROW(opts->xopts, opts->xopts_nr + 1, opts->xopts_alloc); | |
2571 | opts->xopts[opts->xopts_nr++] = xstrdup(value); | |
8d8cb4b0 PW |
2572 | } else if (!strcmp(key, "options.allow-rerere-auto")) |
2573 | opts->allow_rerere_auto = | |
2574 | git_config_bool_or_int(key, value, &error_flag) ? | |
2575 | RERERE_AUTOUPDATE : RERERE_NOAUTOUPDATE; | |
dc42e9a8 PW |
2576 | else if (!strcmp(key, "options.default-msg-cleanup")) { |
2577 | opts->explicit_cleanup = 1; | |
2578 | opts->default_msg_cleanup = get_cleanup_mode(value, 1); | |
2579 | } else | |
93b3df6f | 2580 | return error(_("invalid key: %s"), key); |
043a4492 RR |
2581 | |
2582 | if (!error_flag) | |
93b3df6f | 2583 | return error(_("invalid value for %s: %s"), key, value); |
043a4492 RR |
2584 | |
2585 | return 0; | |
2586 | } | |
2587 | ||
65850686 | 2588 | void parse_strategy_opts(struct replay_opts *opts, char *raw_opts) |
ca6c6b45 JS |
2589 | { |
2590 | int i; | |
65850686 | 2591 | char *strategy_opts_string = raw_opts; |
ca6c6b45 | 2592 | |
0060041d EN |
2593 | if (*strategy_opts_string == ' ') |
2594 | strategy_opts_string++; | |
65850686 | 2595 | |
0060041d EN |
2596 | opts->xopts_nr = split_cmdline(strategy_opts_string, |
2597 | (const char ***)&opts->xopts); | |
ca6c6b45 JS |
2598 | for (i = 0; i < opts->xopts_nr; i++) { |
2599 | const char *arg = opts->xopts[i]; | |
2600 | ||
2601 | skip_prefix(arg, "--", &arg); | |
2602 | opts->xopts[i] = xstrdup(arg); | |
2603 | } | |
2604 | } | |
2605 | ||
65850686 AG |
2606 | static void read_strategy_opts(struct replay_opts *opts, struct strbuf *buf) |
2607 | { | |
2608 | strbuf_reset(buf); | |
2609 | if (!read_oneliner(buf, rebase_path_strategy(), 0)) | |
2610 | return; | |
2611 | opts->strategy = strbuf_detach(buf, NULL); | |
2612 | if (!read_oneliner(buf, rebase_path_strategy_opts(), 0)) | |
2613 | return; | |
2614 | ||
2615 | parse_strategy_opts(opts, buf->buf); | |
2616 | } | |
2617 | ||
5adf9bdc | 2618 | static int read_populate_opts(struct replay_opts *opts) |
043a4492 | 2619 | { |
a1c75762 JS |
2620 | if (is_rebase_i(opts)) { |
2621 | struct strbuf buf = STRBUF_INIT; | |
65c425a2 | 2622 | int ret = 0; |
a1c75762 | 2623 | |
3442c3d1 DL |
2624 | if (read_oneliner(&buf, rebase_path_gpg_sign_opt(), |
2625 | READ_ONELINER_SKIP_IF_EMPTY)) { | |
a1c75762 JS |
2626 | if (!starts_with(buf.buf, "-S")) |
2627 | strbuf_reset(&buf); | |
2628 | else { | |
2629 | free(opts->gpg_sign); | |
2630 | opts->gpg_sign = xstrdup(buf.buf + 2); | |
2631 | } | |
9b6d7a62 PW |
2632 | strbuf_reset(&buf); |
2633 | } | |
2634 | ||
3442c3d1 DL |
2635 | if (read_oneliner(&buf, rebase_path_allow_rerere_autoupdate(), |
2636 | READ_ONELINER_SKIP_IF_EMPTY)) { | |
9b6d7a62 PW |
2637 | if (!strcmp(buf.buf, "--rerere-autoupdate")) |
2638 | opts->allow_rerere_auto = RERERE_AUTOUPDATE; | |
2639 | else if (!strcmp(buf.buf, "--no-rerere-autoupdate")) | |
2640 | opts->allow_rerere_auto = RERERE_NOAUTOUPDATE; | |
2641 | strbuf_reset(&buf); | |
a1c75762 | 2642 | } |
a1c75762 | 2643 | |
556907f1 JS |
2644 | if (file_exists(rebase_path_verbose())) |
2645 | opts->verbose = 1; | |
2646 | ||
899b49c4 EN |
2647 | if (file_exists(rebase_path_quiet())) |
2648 | opts->quiet = 1; | |
2649 | ||
a852ec7f PW |
2650 | if (file_exists(rebase_path_signoff())) { |
2651 | opts->allow_ff = 0; | |
2652 | opts->signoff = 1; | |
2653 | } | |
2654 | ||
7573cec5 PW |
2655 | if (file_exists(rebase_path_cdate_is_adate())) { |
2656 | opts->allow_ff = 0; | |
2657 | opts->committer_date_is_author_date = 1; | |
2658 | } | |
2659 | ||
a3894aad PW |
2660 | if (file_exists(rebase_path_ignore_date())) { |
2661 | opts->allow_ff = 0; | |
2662 | opts->ignore_date = 1; | |
2663 | } | |
2664 | ||
d421afa0 JS |
2665 | if (file_exists(rebase_path_reschedule_failed_exec())) |
2666 | opts->reschedule_failed_exec = 1; | |
2667 | ||
e98c4269 EN |
2668 | if (file_exists(rebase_path_drop_redundant_commits())) |
2669 | opts->drop_redundant_commits = 1; | |
2670 | ||
2671 | if (file_exists(rebase_path_keep_redundant_commits())) | |
2672 | opts->keep_redundant_commits = 1; | |
2673 | ||
ca6c6b45 | 2674 | read_strategy_opts(opts, &buf); |
65c425a2 | 2675 | strbuf_reset(&buf); |
ca6c6b45 | 2676 | |
e12a7ef5 | 2677 | if (read_oneliner(&opts->current_fixups, |
3442c3d1 DL |
2678 | rebase_path_current_fixups(), |
2679 | READ_ONELINER_SKIP_IF_EMPTY)) { | |
e12a7ef5 JS |
2680 | const char *p = opts->current_fixups.buf; |
2681 | opts->current_fixup_count = 1; | |
2682 | while ((p = strchr(p, '\n'))) { | |
2683 | opts->current_fixup_count++; | |
2684 | p++; | |
2685 | } | |
2686 | } | |
2687 | ||
d87d48b2 | 2688 | if (read_oneliner(&buf, rebase_path_squash_onto(), 0)) { |
c7793861 | 2689 | if (get_oid_committish(buf.buf, &opts->squash_onto) < 0) { |
65c425a2 DL |
2690 | ret = error(_("unusable squash-onto")); |
2691 | goto done_rebase_i; | |
2692 | } | |
d87d48b2 JS |
2693 | opts->have_squash_onto = 1; |
2694 | } | |
2695 | ||
65c425a2 DL |
2696 | done_rebase_i: |
2697 | strbuf_release(&buf); | |
2698 | return ret; | |
a1c75762 | 2699 | } |
b5a67045 | 2700 | |
f932729c | 2701 | if (!file_exists(git_path_opts_file())) |
0d00da7b JS |
2702 | return 0; |
2703 | /* | |
2704 | * The function git_parse_source(), called from git_config_from_file(), | |
2705 | * may die() in case of a syntactically incorrect file. We do not care | |
2706 | * about this case, though, because we wrote that file ourselves, so we | |
2707 | * are pretty certain that it is syntactically correct. | |
2708 | */ | |
5adf9bdc | 2709 | if (git_config_from_file(populate_opts_cb, git_path_opts_file(), opts) < 0) |
93b3df6f | 2710 | return error(_("malformed options sheet: '%s'"), |
0d00da7b JS |
2711 | git_path_opts_file()); |
2712 | return 0; | |
043a4492 RR |
2713 | } |
2714 | ||
65850686 AG |
2715 | static void write_strategy_opts(struct replay_opts *opts) |
2716 | { | |
2717 | int i; | |
2718 | struct strbuf buf = STRBUF_INIT; | |
2719 | ||
2720 | for (i = 0; i < opts->xopts_nr; ++i) | |
2721 | strbuf_addf(&buf, " --%s", opts->xopts[i]); | |
2722 | ||
2723 | write_file(rebase_path_strategy_opts(), "%s\n", buf.buf); | |
2724 | strbuf_release(&buf); | |
2725 | } | |
2726 | ||
2727 | int write_basic_state(struct replay_opts *opts, const char *head_name, | |
a2bb10d0 | 2728 | struct commit *onto, const struct object_id *orig_head) |
65850686 | 2729 | { |
65850686 AG |
2730 | if (head_name) |
2731 | write_file(rebase_path_head_name(), "%s\n", head_name); | |
2732 | if (onto) | |
7d3488eb PW |
2733 | write_file(rebase_path_onto(), "%s\n", |
2734 | oid_to_hex(&onto->object.oid)); | |
65850686 | 2735 | if (orig_head) |
a2bb10d0 PW |
2736 | write_file(rebase_path_orig_head(), "%s\n", |
2737 | oid_to_hex(orig_head)); | |
65850686 | 2738 | |
8a997ed1 EN |
2739 | if (opts->quiet) |
2740 | write_file(rebase_path_quiet(), "%s", ""); | |
65850686 | 2741 | if (opts->verbose) |
4af51741 | 2742 | write_file(rebase_path_verbose(), "%s", ""); |
65850686 AG |
2743 | if (opts->strategy) |
2744 | write_file(rebase_path_strategy(), "%s\n", opts->strategy); | |
2745 | if (opts->xopts_nr > 0) | |
2746 | write_strategy_opts(opts); | |
2747 | ||
2748 | if (opts->allow_rerere_auto == RERERE_AUTOUPDATE) | |
2749 | write_file(rebase_path_allow_rerere_autoupdate(), "--rerere-autoupdate\n"); | |
2750 | else if (opts->allow_rerere_auto == RERERE_NOAUTOUPDATE) | |
2751 | write_file(rebase_path_allow_rerere_autoupdate(), "--no-rerere-autoupdate\n"); | |
2752 | ||
2753 | if (opts->gpg_sign) | |
2754 | write_file(rebase_path_gpg_sign_opt(), "-S%s\n", opts->gpg_sign); | |
2755 | if (opts->signoff) | |
2756 | write_file(rebase_path_signoff(), "--signoff\n"); | |
e98c4269 EN |
2757 | if (opts->drop_redundant_commits) |
2758 | write_file(rebase_path_drop_redundant_commits(), "%s", ""); | |
2759 | if (opts->keep_redundant_commits) | |
2760 | write_file(rebase_path_keep_redundant_commits(), "%s", ""); | |
7573cec5 PW |
2761 | if (opts->committer_date_is_author_date) |
2762 | write_file(rebase_path_cdate_is_adate(), "%s", ""); | |
a3894aad PW |
2763 | if (opts->ignore_date) |
2764 | write_file(rebase_path_ignore_date(), "%s", ""); | |
d421afa0 JS |
2765 | if (opts->reschedule_failed_exec) |
2766 | write_file(rebase_path_reschedule_failed_exec(), "%s", ""); | |
65850686 AG |
2767 | |
2768 | return 0; | |
2769 | } | |
2770 | ||
004fefa7 | 2771 | static int walk_revs_populate_todo(struct todo_list *todo_list, |
043a4492 RR |
2772 | struct replay_opts *opts) |
2773 | { | |
004fefa7 JS |
2774 | enum todo_command command = opts->action == REPLAY_PICK ? |
2775 | TODO_PICK : TODO_REVERT; | |
414697a9 | 2776 | const char *command_string = todo_command_info[command].str; |
019a9d83 | 2777 | const char *encoding; |
043a4492 | 2778 | struct commit *commit; |
043a4492 | 2779 | |
34b0528b JS |
2780 | if (prepare_revs(opts)) |
2781 | return -1; | |
043a4492 | 2782 | |
019a9d83 ĐTCD |
2783 | encoding = get_log_output_encoding(); |
2784 | ||
004fefa7 JS |
2785 | while ((commit = get_revision(opts->revs))) { |
2786 | struct todo_item *item = append_new_todo(todo_list); | |
019a9d83 | 2787 | const char *commit_buffer = logmsg_reencode(commit, NULL, encoding); |
004fefa7 JS |
2788 | const char *subject; |
2789 | int subject_len; | |
2790 | ||
2791 | item->command = command; | |
2792 | item->commit = commit; | |
6ad656db | 2793 | item->arg_offset = 0; |
c22f7dfb | 2794 | item->arg_len = 0; |
004fefa7 JS |
2795 | item->offset_in_buf = todo_list->buf.len; |
2796 | subject_len = find_commit_subject(commit_buffer, &subject); | |
2797 | strbuf_addf(&todo_list->buf, "%s %s %.*s\n", command_string, | |
2798 | short_commit_name(commit), subject_len, subject); | |
2799 | unuse_commit_buffer(commit, commit_buffer); | |
2800 | } | |
8530c739 JK |
2801 | |
2802 | if (!todo_list->nr) | |
2803 | return error(_("empty commit set passed")); | |
2804 | ||
34b0528b | 2805 | return 0; |
043a4492 RR |
2806 | } |
2807 | ||
6a1f9046 | 2808 | static int create_seq_dir(struct repository *r) |
043a4492 | 2809 | { |
6a1f9046 RA |
2810 | enum replay_action action; |
2811 | const char *in_progress_error = NULL; | |
2812 | const char *in_progress_advice = NULL; | |
c8e4159e | 2813 | unsigned int advise_skip = |
b8825ef2 | 2814 | refs_ref_exists(get_main_ref_store(r), "REVERT_HEAD") || |
c8e4159e | 2815 | refs_ref_exists(get_main_ref_store(r), "CHERRY_PICK_HEAD"); |
6a1f9046 RA |
2816 | |
2817 | if (!sequencer_get_last_command(r, &action)) { | |
2818 | switch (action) { | |
2819 | case REPLAY_REVERT: | |
2820 | in_progress_error = _("revert is already in progress"); | |
2821 | in_progress_advice = | |
dcb500dc | 2822 | _("try \"git revert (--continue | %s--abort | --quit)\""); |
6a1f9046 RA |
2823 | break; |
2824 | case REPLAY_PICK: | |
2825 | in_progress_error = _("cherry-pick is already in progress"); | |
2826 | in_progress_advice = | |
dcb500dc | 2827 | _("try \"git cherry-pick (--continue | %s--abort | --quit)\""); |
6a1f9046 RA |
2828 | break; |
2829 | default: | |
2830 | BUG("unexpected action in create_seq_dir"); | |
2831 | } | |
2832 | } | |
2833 | if (in_progress_error) { | |
2834 | error("%s", in_progress_error); | |
2835 | if (advice_sequencer_in_use) | |
dcb500dc RA |
2836 | advise(in_progress_advice, |
2837 | advise_skip ? "--skip | " : ""); | |
043a4492 | 2838 | return -1; |
6a1f9046 RA |
2839 | } |
2840 | if (mkdir(git_path_seq_dir(), 0777) < 0) | |
93b3df6f | 2841 | return error_errno(_("could not create sequencer directory '%s'"), |
f6e82b0d | 2842 | git_path_seq_dir()); |
6a1f9046 | 2843 | |
043a4492 RR |
2844 | return 0; |
2845 | } | |
2846 | ||
311fd397 | 2847 | static int save_head(const char *head) |
043a4492 | 2848 | { |
14bca6c6 | 2849 | struct lock_file head_lock = LOCK_INIT; |
043a4492 RR |
2850 | struct strbuf buf = STRBUF_INIT; |
2851 | int fd; | |
ed3f9a12 | 2852 | ssize_t written; |
043a4492 | 2853 | |
311fd397 | 2854 | fd = hold_lock_file_for_update(&head_lock, git_path_head_file(), 0); |
350292a1 | 2855 | if (fd < 0) |
93b3df6f | 2856 | return error_errno(_("could not lock HEAD")); |
043a4492 | 2857 | strbuf_addf(&buf, "%s\n", head); |
ed3f9a12 RS |
2858 | written = write_in_full(fd, buf.buf, buf.len); |
2859 | strbuf_release(&buf); | |
2860 | if (written < 0) { | |
bf5c0571 | 2861 | error_errno(_("could not write to '%s'"), git_path_head_file()); |
311fd397 | 2862 | rollback_lock_file(&head_lock); |
bf5c0571 | 2863 | return -1; |
311fd397 | 2864 | } |
350292a1 MÅ |
2865 | if (commit_lock_file(&head_lock) < 0) |
2866 | return error(_("failed to finalize '%s'"), git_path_head_file()); | |
311fd397 | 2867 | return 0; |
043a4492 RR |
2868 | } |
2869 | ||
1e41229d SB |
2870 | static int rollback_is_safe(void) |
2871 | { | |
2872 | struct strbuf sb = STRBUF_INIT; | |
2873 | struct object_id expected_head, actual_head; | |
2874 | ||
2875 | if (strbuf_read_file(&sb, git_path_abort_safety_file(), 0) >= 0) { | |
2876 | strbuf_trim(&sb); | |
2877 | if (get_oid_hex(sb.buf, &expected_head)) { | |
2878 | strbuf_release(&sb); | |
2879 | die(_("could not parse %s"), git_path_abort_safety_file()); | |
2880 | } | |
2881 | strbuf_release(&sb); | |
2882 | } | |
2883 | else if (errno == ENOENT) | |
2884 | oidclr(&expected_head); | |
2885 | else | |
2886 | die_errno(_("could not read '%s'"), git_path_abort_safety_file()); | |
2887 | ||
2888 | if (get_oid("HEAD", &actual_head)) | |
2889 | oidclr(&actual_head); | |
2890 | ||
4a7e27e9 | 2891 | return oideq(&actual_head, &expected_head); |
1e41229d SB |
2892 | } |
2893 | ||
918d1e6e | 2894 | static int reset_merge(const struct object_id *oid) |
043a4492 | 2895 | { |
265ab48f | 2896 | int ret; |
c972bf4c | 2897 | struct strvec argv = STRVEC_INIT; |
1e41229d | 2898 | |
c972bf4c | 2899 | strvec_pushl(&argv, "reset", "--merge", NULL); |
265ab48f RA |
2900 | |
2901 | if (!is_null_oid(oid)) | |
c972bf4c | 2902 | strvec_push(&argv, oid_to_hex(oid)); |
1e41229d | 2903 | |
d70a9eb6 | 2904 | ret = run_command_v_opt(argv.v, RUN_GIT_CMD); |
c972bf4c | 2905 | strvec_clear(&argv); |
265ab48f RA |
2906 | |
2907 | return ret; | |
043a4492 RR |
2908 | } |
2909 | ||
005af339 | 2910 | static int rollback_single_pick(struct repository *r) |
043a4492 | 2911 | { |
092bbcdf | 2912 | struct object_id head_oid; |
043a4492 | 2913 | |
c8e4159e | 2914 | if (!refs_ref_exists(get_main_ref_store(r), "CHERRY_PICK_HEAD") && |
b8825ef2 | 2915 | !refs_ref_exists(get_main_ref_store(r), "REVERT_HEAD")) |
043a4492 | 2916 | return error(_("no cherry-pick or revert in progress")); |
34c290a6 | 2917 | if (read_ref_full("HEAD", 0, &head_oid, NULL)) |
043a4492 | 2918 | return error(_("cannot resolve HEAD")); |
092bbcdf | 2919 | if (is_null_oid(&head_oid)) |
043a4492 | 2920 | return error(_("cannot abort from a branch yet to be born")); |
918d1e6e | 2921 | return reset_merge(&head_oid); |
043a4492 RR |
2922 | } |
2923 | ||
de81ca3f RA |
2924 | static int skip_single_pick(void) |
2925 | { | |
2926 | struct object_id head; | |
2927 | ||
2928 | if (read_ref_full("HEAD", 0, &head, NULL)) | |
2929 | return error(_("cannot resolve HEAD")); | |
2930 | return reset_merge(&head); | |
043a4492 RR |
2931 | } |
2932 | ||
005af339 | 2933 | int sequencer_rollback(struct repository *r, struct replay_opts *opts) |
043a4492 | 2934 | { |
043a4492 | 2935 | FILE *f; |
092bbcdf | 2936 | struct object_id oid; |
043a4492 | 2937 | struct strbuf buf = STRBUF_INIT; |
092bbcdf | 2938 | const char *p; |
043a4492 | 2939 | |
f932729c | 2940 | f = fopen(git_path_head_file(), "r"); |
043a4492 RR |
2941 | if (!f && errno == ENOENT) { |
2942 | /* | |
2943 | * There is no multiple-cherry-pick in progress. | |
2944 | * If CHERRY_PICK_HEAD or REVERT_HEAD indicates | |
2945 | * a single-cherry-pick in progress, abort that. | |
2946 | */ | |
005af339 | 2947 | return rollback_single_pick(r); |
043a4492 RR |
2948 | } |
2949 | if (!f) | |
f7ed1953 | 2950 | return error_errno(_("cannot open '%s'"), git_path_head_file()); |
8f309aeb | 2951 | if (strbuf_getline_lf(&buf, f)) { |
f7ed1953 | 2952 | error(_("cannot read '%s': %s"), git_path_head_file(), |
f932729c | 2953 | ferror(f) ? strerror(errno) : _("unexpected end of file")); |
043a4492 RR |
2954 | fclose(f); |
2955 | goto fail; | |
2956 | } | |
2957 | fclose(f); | |
092bbcdf | 2958 | if (parse_oid_hex(buf.buf, &oid, &p) || *p != '\0') { |
043a4492 | 2959 | error(_("stored pre-cherry-pick HEAD file '%s' is corrupt"), |
f932729c | 2960 | git_path_head_file()); |
043a4492 RR |
2961 | goto fail; |
2962 | } | |
092bbcdf | 2963 | if (is_null_oid(&oid)) { |
0f974e21 MG |
2964 | error(_("cannot abort from a branch yet to be born")); |
2965 | goto fail; | |
2966 | } | |
1e41229d SB |
2967 | |
2968 | if (!rollback_is_safe()) { | |
2969 | /* Do not error, just do not rollback */ | |
2970 | warning(_("You seem to have moved HEAD. " | |
2971 | "Not rewinding, check your HEAD!")); | |
2972 | } else | |
918d1e6e | 2973 | if (reset_merge(&oid)) |
043a4492 | 2974 | goto fail; |
043a4492 | 2975 | strbuf_release(&buf); |
2863584f | 2976 | return sequencer_remove_state(opts); |
043a4492 RR |
2977 | fail: |
2978 | strbuf_release(&buf); | |
2979 | return -1; | |
2980 | } | |
2981 | ||
de81ca3f RA |
2982 | int sequencer_skip(struct repository *r, struct replay_opts *opts) |
2983 | { | |
2984 | enum replay_action action = -1; | |
2985 | sequencer_get_last_command(r, &action); | |
2986 | ||
2987 | /* | |
2988 | * Check whether the subcommand requested to skip the commit is actually | |
2989 | * in progress and that it's safe to skip the commit. | |
2990 | * | |
2991 | * opts->action tells us which subcommand requested to skip the commit. | |
2992 | * If the corresponding .git/<ACTION>_HEAD exists, we know that the | |
2993 | * action is in progress and we can skip the commit. | |
2994 | * | |
2995 | * Otherwise we check that the last instruction was related to the | |
2996 | * particular subcommand we're trying to execute and barf if that's not | |
2997 | * the case. | |
2998 | * | |
2999 | * Finally we check that the rollback is "safe", i.e., has the HEAD | |
3000 | * moved? In this case, it doesn't make sense to "reset the merge" and | |
3001 | * "skip the commit" as the user already handled this by committing. But | |
3002 | * we'd not want to barf here, instead give advice on how to proceed. We | |
3003 | * only need to check that when .git/<ACTION>_HEAD doesn't exist because | |
3004 | * it gets removed when the user commits, so if it still exists we're | |
3005 | * sure the user can't have committed before. | |
3006 | */ | |
3007 | switch (opts->action) { | |
3008 | case REPLAY_REVERT: | |
b8825ef2 | 3009 | if (!refs_ref_exists(get_main_ref_store(r), "REVERT_HEAD")) { |
de81ca3f RA |
3010 | if (action != REPLAY_REVERT) |
3011 | return error(_("no revert in progress")); | |
3012 | if (!rollback_is_safe()) | |
3013 | goto give_advice; | |
3014 | } | |
3015 | break; | |
3016 | case REPLAY_PICK: | |
c8e4159e HWN |
3017 | if (!refs_ref_exists(get_main_ref_store(r), |
3018 | "CHERRY_PICK_HEAD")) { | |
de81ca3f RA |
3019 | if (action != REPLAY_PICK) |
3020 | return error(_("no cherry-pick in progress")); | |
3021 | if (!rollback_is_safe()) | |
3022 | goto give_advice; | |
3023 | } | |
3024 | break; | |
3025 | default: | |
3026 | BUG("unexpected action in sequencer_skip"); | |
3027 | } | |
3028 | ||
3029 | if (skip_single_pick()) | |
3030 | return error(_("failed to skip the commit")); | |
3031 | if (!is_directory(git_path_seq_dir())) | |
3032 | return 0; | |
3033 | ||
3034 | return sequencer_continue(r, opts); | |
3035 | ||
3036 | give_advice: | |
3037 | error(_("there is nothing to skip")); | |
3038 | ||
3039 | if (advice_resolve_conflict) { | |
3040 | advise(_("have you committed already?\n" | |
3041 | "try \"git %s --continue\""), | |
3042 | action == REPLAY_REVERT ? "revert" : "cherry-pick"); | |
3043 | } | |
3044 | return -1; | |
3045 | } | |
3046 | ||
004fefa7 | 3047 | static int save_todo(struct todo_list *todo_list, struct replay_opts *opts) |
043a4492 | 3048 | { |
14bca6c6 | 3049 | struct lock_file todo_lock = LOCK_INIT; |
004fefa7 JS |
3050 | const char *todo_path = get_todo_path(opts); |
3051 | int next = todo_list->current, offset, fd; | |
043a4492 | 3052 | |
84583957 JS |
3053 | /* |
3054 | * rebase -i writes "git-rebase-todo" without the currently executing | |
3055 | * command, appending it to "done" instead. | |
3056 | */ | |
3057 | if (is_rebase_i(opts)) | |
3058 | next++; | |
3059 | ||
004fefa7 | 3060 | fd = hold_lock_file_for_update(&todo_lock, todo_path, 0); |
221675de | 3061 | if (fd < 0) |
93b3df6f | 3062 | return error_errno(_("could not lock '%s'"), todo_path); |
a01c2a5f | 3063 | offset = get_item_line_offset(todo_list, next); |
004fefa7 JS |
3064 | if (write_in_full(fd, todo_list->buf.buf + offset, |
3065 | todo_list->buf.len - offset) < 0) | |
93b3df6f | 3066 | return error_errno(_("could not write to '%s'"), todo_path); |
004fefa7 | 3067 | if (commit_lock_file(&todo_lock) < 0) |
350292a1 | 3068 | return error(_("failed to finalize '%s'"), todo_path); |
1df6df0c | 3069 | |
a01c2a5f JS |
3070 | if (is_rebase_i(opts) && next > 0) { |
3071 | const char *done = rebase_path_done(); | |
3072 | int fd = open(done, O_CREAT | O_WRONLY | O_APPEND, 0666); | |
3073 | int ret = 0; | |
1df6df0c | 3074 | |
a01c2a5f JS |
3075 | if (fd < 0) |
3076 | return 0; | |
3077 | if (write_in_full(fd, get_item_line(todo_list, next - 1), | |
3078 | get_item_line_length(todo_list, next - 1)) | |
3079 | < 0) | |
3080 | ret = error_errno(_("could not write to '%s'"), done); | |
3081 | if (close(fd) < 0) | |
3082 | ret = error_errno(_("failed to finalize '%s'"), done); | |
3083 | return ret; | |
1df6df0c | 3084 | } |
221675de | 3085 | return 0; |
043a4492 RR |
3086 | } |
3087 | ||
88d5a271 | 3088 | static int save_opts(struct replay_opts *opts) |
043a4492 | 3089 | { |
f932729c | 3090 | const char *opts_file = git_path_opts_file(); |
88d5a271 | 3091 | int res = 0; |
043a4492 RR |
3092 | |
3093 | if (opts->no_commit) | |
f59199d5 PW |
3094 | res |= git_config_set_in_file_gently(opts_file, |
3095 | "options.no-commit", "true"); | |
043a4492 | 3096 | if (opts->edit) |
f59199d5 PW |
3097 | res |= git_config_set_in_file_gently(opts_file, |
3098 | "options.edit", "true"); | |
6860ce5d PW |
3099 | if (opts->allow_empty) |
3100 | res |= git_config_set_in_file_gently(opts_file, | |
3101 | "options.allow-empty", "true"); | |
3102 | if (opts->allow_empty_message) | |
3103 | res |= git_config_set_in_file_gently(opts_file, | |
3104 | "options.allow-empty-message", "true"); | |
3105 | if (opts->keep_redundant_commits) | |
3106 | res |= git_config_set_in_file_gently(opts_file, | |
3107 | "options.keep-redundant-commits", "true"); | |
043a4492 | 3108 | if (opts->signoff) |
f59199d5 PW |
3109 | res |= git_config_set_in_file_gently(opts_file, |
3110 | "options.signoff", "true"); | |
043a4492 | 3111 | if (opts->record_origin) |
f59199d5 PW |
3112 | res |= git_config_set_in_file_gently(opts_file, |
3113 | "options.record-origin", "true"); | |
043a4492 | 3114 | if (opts->allow_ff) |
f59199d5 PW |
3115 | res |= git_config_set_in_file_gently(opts_file, |
3116 | "options.allow-ff", "true"); | |
043a4492 RR |
3117 | if (opts->mainline) { |
3118 | struct strbuf buf = STRBUF_INIT; | |
3119 | strbuf_addf(&buf, "%d", opts->mainline); | |
f59199d5 PW |
3120 | res |= git_config_set_in_file_gently(opts_file, |
3121 | "options.mainline", buf.buf); | |
043a4492 RR |
3122 | strbuf_release(&buf); |
3123 | } | |
3124 | if (opts->strategy) | |
f59199d5 PW |
3125 | res |= git_config_set_in_file_gently(opts_file, |
3126 | "options.strategy", opts->strategy); | |
3253553e | 3127 | if (opts->gpg_sign) |
f59199d5 PW |
3128 | res |= git_config_set_in_file_gently(opts_file, |
3129 | "options.gpg-sign", opts->gpg_sign); | |
043a4492 RR |
3130 | if (opts->xopts) { |
3131 | int i; | |
3132 | for (i = 0; i < opts->xopts_nr; i++) | |
88d5a271 | 3133 | res |= git_config_set_multivar_in_file_gently(opts_file, |
f59199d5 PW |
3134 | "options.strategy-option", |
3135 | opts->xopts[i], "^$", 0); | |
043a4492 | 3136 | } |
8d8cb4b0 | 3137 | if (opts->allow_rerere_auto) |
f59199d5 PW |
3138 | res |= git_config_set_in_file_gently(opts_file, |
3139 | "options.allow-rerere-auto", | |
3140 | opts->allow_rerere_auto == RERERE_AUTOUPDATE ? | |
3141 | "true" : "false"); | |
dc42e9a8 PW |
3142 | |
3143 | if (opts->explicit_cleanup) | |
3144 | res |= git_config_set_in_file_gently(opts_file, | |
3145 | "options.default-msg-cleanup", | |
3146 | describe_cleanup_mode(opts->default_msg_cleanup)); | |
88d5a271 | 3147 | return res; |
043a4492 RR |
3148 | } |
3149 | ||
f11c9580 NTND |
3150 | static int make_patch(struct repository *r, |
3151 | struct commit *commit, | |
3152 | struct replay_opts *opts) | |
56dc3ab0 JS |
3153 | { |
3154 | struct strbuf buf = STRBUF_INIT; | |
3155 | struct rev_info log_tree_opt; | |
0512eabd JH |
3156 | const char *subject; |
3157 | char hex[GIT_MAX_HEXSZ + 1]; | |
56dc3ab0 JS |
3158 | int res = 0; |
3159 | ||
0512eabd JH |
3160 | oid_to_hex_r(hex, &commit->object.oid); |
3161 | if (write_message(hex, strlen(hex), rebase_path_stopped_sha(), 1) < 0) | |
56dc3ab0 | 3162 | return -1; |
430b75f7 | 3163 | res |= write_rebase_head(&commit->object.oid); |
56dc3ab0 JS |
3164 | |
3165 | strbuf_addf(&buf, "%s/patch", get_dir(opts)); | |
3166 | memset(&log_tree_opt, 0, sizeof(log_tree_opt)); | |
f11c9580 | 3167 | repo_init_revisions(r, &log_tree_opt, NULL); |
56dc3ab0 JS |
3168 | log_tree_opt.abbrev = 0; |
3169 | log_tree_opt.diff = 1; | |
3170 | log_tree_opt.diffopt.output_format = DIFF_FORMAT_PATCH; | |
3171 | log_tree_opt.disable_stdin = 1; | |
3172 | log_tree_opt.no_commit_id = 1; | |
3173 | log_tree_opt.diffopt.file = fopen(buf.buf, "w"); | |
3174 | log_tree_opt.diffopt.use_color = GIT_COLOR_NEVER; | |
3175 | if (!log_tree_opt.diffopt.file) | |
3176 | res |= error_errno(_("could not open '%s'"), buf.buf); | |
3177 | else { | |
3178 | res |= log_tree_commit(&log_tree_opt, commit); | |
3179 | fclose(log_tree_opt.diffopt.file); | |
3180 | } | |
3181 | strbuf_reset(&buf); | |
3182 | ||
3183 | strbuf_addf(&buf, "%s/message", get_dir(opts)); | |
3184 | if (!file_exists(buf.buf)) { | |
52f52e5a ĐTCD |
3185 | const char *encoding = get_commit_output_encoding(); |
3186 | const char *commit_buffer = logmsg_reencode(commit, NULL, encoding); | |
56dc3ab0 JS |
3187 | find_commit_subject(commit_buffer, &subject); |
3188 | res |= write_message(subject, strlen(subject), buf.buf, 1); | |
3189 | unuse_commit_buffer(commit, commit_buffer); | |
3190 | } | |
3191 | strbuf_release(&buf); | |
3192 | ||
3193 | return res; | |
3194 | } | |
3195 | ||
3196 | static int intend_to_amend(void) | |
3197 | { | |
092bbcdf | 3198 | struct object_id head; |
56dc3ab0 JS |
3199 | char *p; |
3200 | ||
092bbcdf | 3201 | if (get_oid("HEAD", &head)) |
56dc3ab0 JS |
3202 | return error(_("cannot read HEAD")); |
3203 | ||
092bbcdf | 3204 | p = oid_to_hex(&head); |
56dc3ab0 JS |
3205 | return write_message(p, strlen(p), rebase_path_amend(), 1); |
3206 | } | |
3207 | ||
f11c9580 NTND |
3208 | static int error_with_patch(struct repository *r, |
3209 | struct commit *commit, | |
3210 | const char *subject, int subject_len, | |
3211 | struct replay_opts *opts, | |
3212 | int exit_code, int to_amend) | |
56dc3ab0 | 3213 | { |
bc9238bb | 3214 | if (commit) { |
f11c9580 | 3215 | if (make_patch(r, commit, opts)) |
bc9238bb | 3216 | return -1; |
5a5c5e95 | 3217 | } else if (copy_file(rebase_path_message(), |
f11c9580 | 3218 | git_path_merge_msg(r), 0666)) |
bc9238bb | 3219 | return error(_("unable to copy '%s' to '%s'"), |
f11c9580 | 3220 | git_path_merge_msg(r), rebase_path_message()); |
56dc3ab0 JS |
3221 | |
3222 | if (to_amend) { | |
3223 | if (intend_to_amend()) | |
3224 | return -1; | |
3225 | ||
02127c63 NTND |
3226 | fprintf(stderr, |
3227 | _("You can amend the commit now, with\n" | |
3228 | "\n" | |
3229 | " git commit --amend %s\n" | |
3230 | "\n" | |
3231 | "Once you are satisfied with your changes, run\n" | |
3232 | "\n" | |
3233 | " git rebase --continue\n"), | |
3234 | gpg_sign_opt_quoted(opts)); | |
bc9238bb PW |
3235 | } else if (exit_code) { |
3236 | if (commit) | |
5a5c5e95 JH |
3237 | fprintf_ln(stderr, _("Could not apply %s... %.*s"), |
3238 | short_commit_name(commit), subject_len, subject); | |
bc9238bb PW |
3239 | else |
3240 | /* | |
3241 | * We don't have the hash of the parent so | |
3242 | * just print the line from the todo file. | |
3243 | */ | |
5a5c5e95 JH |
3244 | fprintf_ln(stderr, _("Could not merge %.*s"), |
3245 | subject_len, subject); | |
bc9238bb | 3246 | } |
56dc3ab0 JS |
3247 | |
3248 | return exit_code; | |
3249 | } | |
3250 | ||
f11c9580 NTND |
3251 | static int error_failed_squash(struct repository *r, |
3252 | struct commit *commit, | |
3253 | struct replay_opts *opts, | |
3254 | int subject_len, | |
3255 | const char *subject) | |
6e98de72 | 3256 | { |
e12a7ef5 JS |
3257 | if (copy_file(rebase_path_message(), rebase_path_squash_msg(), 0666)) |
3258 | return error(_("could not copy '%s' to '%s'"), | |
6e98de72 | 3259 | rebase_path_squash_msg(), rebase_path_message()); |
f11c9580 NTND |
3260 | unlink(git_path_merge_msg(r)); |
3261 | if (copy_file(git_path_merge_msg(r), rebase_path_message(), 0666)) | |
6e98de72 | 3262 | return error(_("could not copy '%s' to '%s'"), |
102de880 | 3263 | rebase_path_message(), |
f11c9580 NTND |
3264 | git_path_merge_msg(r)); |
3265 | return error_with_patch(r, commit, subject, subject_len, opts, 1, 0); | |
6e98de72 JS |
3266 | } |
3267 | ||
f11c9580 | 3268 | static int do_exec(struct repository *r, const char *command_line) |
311af526 | 3269 | { |
c972bf4c | 3270 | struct strvec child_env = STRVEC_INIT; |
311af526 JS |
3271 | const char *child_argv[] = { NULL, NULL }; |
3272 | int dirty, status; | |
3273 | ||
4d55d63b | 3274 | fprintf(stderr, _("Executing: %s\n"), command_line); |
311af526 | 3275 | child_argv[0] = command_line; |
c972bf4c JK |
3276 | strvec_pushf(&child_env, "GIT_DIR=%s", absolute_path(get_git_dir())); |
3277 | strvec_pushf(&child_env, "GIT_WORK_TREE=%s", | |
f6d8942b | 3278 | absolute_path(get_git_work_tree())); |
09d7b6c6 | 3279 | status = run_command_v_opt_cd_env(child_argv, RUN_USING_SHELL, NULL, |
d70a9eb6 | 3280 | child_env.v); |
311af526 JS |
3281 | |
3282 | /* force re-reading of the cache */ | |
e1ff0a32 | 3283 | if (discard_index(r->index) < 0 || repo_read_index(r) < 0) |
311af526 JS |
3284 | return error(_("could not read index")); |
3285 | ||
f11c9580 | 3286 | dirty = require_clean_work_tree(r, "rebase", NULL, 1, 1); |
311af526 JS |
3287 | |
3288 | if (status) { | |
3289 | warning(_("execution failed: %s\n%s" | |
3290 | "You can fix the problem, and then run\n" | |
3291 | "\n" | |
3292 | " git rebase --continue\n" | |
3293 | "\n"), | |
3294 | command_line, | |
3295 | dirty ? N_("and made changes to the index and/or the " | |
3296 | "working tree\n") : ""); | |
3297 | if (status == 127) | |
3298 | /* command not found */ | |
3299 | status = 1; | |
3300 | } else if (dirty) { | |
3301 | warning(_("execution succeeded: %s\nbut " | |
3302 | "left changes to the index and/or the working tree\n" | |
3303 | "Commit or stash your changes, and then run\n" | |
3304 | "\n" | |
3305 | " git rebase --continue\n" | |
3306 | "\n"), command_line); | |
3307 | status = 1; | |
3308 | } | |
3309 | ||
c972bf4c | 3310 | strvec_clear(&child_env); |
09d7b6c6 | 3311 | |
311af526 JS |
3312 | return status; |
3313 | } | |
3314 | ||
9055e401 JS |
3315 | static int safe_append(const char *filename, const char *fmt, ...) |
3316 | { | |
3317 | va_list ap; | |
3318 | struct lock_file lock = LOCK_INIT; | |
3319 | int fd = hold_lock_file_for_update(&lock, filename, | |
3320 | LOCK_REPORT_ON_ERROR); | |
3321 | struct strbuf buf = STRBUF_INIT; | |
3322 | ||
3323 | if (fd < 0) | |
3324 | return -1; | |
3325 | ||
3326 | if (strbuf_read_file(&buf, filename, 0) < 0 && errno != ENOENT) { | |
3327 | error_errno(_("could not read '%s'"), filename); | |
3328 | rollback_lock_file(&lock); | |
3329 | return -1; | |
3330 | } | |
3331 | strbuf_complete(&buf, '\n'); | |
3332 | va_start(ap, fmt); | |
3333 | strbuf_vaddf(&buf, fmt, ap); | |
3334 | va_end(ap); | |
3335 | ||
3336 | if (write_in_full(fd, buf.buf, buf.len) < 0) { | |
3337 | error_errno(_("could not write to '%s'"), filename); | |
3338 | strbuf_release(&buf); | |
3339 | rollback_lock_file(&lock); | |
3340 | return -1; | |
3341 | } | |
3342 | if (commit_lock_file(&lock) < 0) { | |
3343 | strbuf_release(&buf); | |
3344 | rollback_lock_file(&lock); | |
3345 | return error(_("failed to finalize '%s'"), filename); | |
3346 | } | |
3347 | ||
3348 | strbuf_release(&buf); | |
3349 | return 0; | |
3350 | } | |
3351 | ||
f11c9580 | 3352 | static int do_label(struct repository *r, const char *name, int len) |
9055e401 | 3353 | { |
f11c9580 | 3354 | struct ref_store *refs = get_main_ref_store(r); |
9055e401 JS |
3355 | struct ref_transaction *transaction; |
3356 | struct strbuf ref_name = STRBUF_INIT, err = STRBUF_INIT; | |
3357 | struct strbuf msg = STRBUF_INIT; | |
3358 | int ret = 0; | |
3359 | struct object_id head_oid; | |
3360 | ||
3361 | if (len == 1 && *name == '#') | |
02127c63 | 3362 | return error(_("illegal label name: '%.*s'"), len, name); |
9055e401 JS |
3363 | |
3364 | strbuf_addf(&ref_name, "refs/rewritten/%.*s", len, name); | |
c2417d3a | 3365 | strbuf_addf(&msg, "rebase (label) '%.*s'", len, name); |
9055e401 JS |
3366 | |
3367 | transaction = ref_store_transaction_begin(refs, &err); | |
3368 | if (!transaction) { | |
3369 | error("%s", err.buf); | |
3370 | ret = -1; | |
3371 | } else if (get_oid("HEAD", &head_oid)) { | |
3372 | error(_("could not read HEAD")); | |
3373 | ret = -1; | |
3374 | } else if (ref_transaction_update(transaction, ref_name.buf, &head_oid, | |
3375 | NULL, 0, msg.buf, &err) < 0 || | |
3376 | ref_transaction_commit(transaction, &err)) { | |
3377 | error("%s", err.buf); | |
3378 | ret = -1; | |
3379 | } | |
3380 | ref_transaction_free(transaction); | |
3381 | strbuf_release(&err); | |
3382 | strbuf_release(&msg); | |
3383 | ||
3384 | if (!ret) | |
3385 | ret = safe_append(rebase_path_refs_to_delete(), | |
3386 | "%s\n", ref_name.buf); | |
3387 | strbuf_release(&ref_name); | |
3388 | ||
3389 | return ret; | |
3390 | } | |
3391 | ||
3392 | static const char *reflog_message(struct replay_opts *opts, | |
3393 | const char *sub_action, const char *fmt, ...); | |
3394 | ||
f11c9580 NTND |
3395 | static int do_reset(struct repository *r, |
3396 | const char *name, int len, | |
3397 | struct replay_opts *opts) | |
9055e401 JS |
3398 | { |
3399 | struct strbuf ref_name = STRBUF_INIT; | |
3400 | struct object_id oid; | |
3401 | struct lock_file lock = LOCK_INIT; | |
3402 | struct tree_desc desc; | |
3403 | struct tree *tree; | |
3404 | struct unpack_trees_options unpack_tree_opts; | |
71571cd7 | 3405 | int ret = 0; |
9055e401 | 3406 | |
3a95f31d | 3407 | if (repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) |
9055e401 JS |
3408 | return -1; |
3409 | ||
ebddf393 JS |
3410 | if (len == 10 && !strncmp("[new root]", name, len)) { |
3411 | if (!opts->have_squash_onto) { | |
3412 | const char *hex; | |
3413 | if (commit_tree("", 0, the_hash_algo->empty_tree, | |
3414 | NULL, &opts->squash_onto, | |
3415 | NULL, NULL)) | |
3416 | return error(_("writing fake root commit")); | |
3417 | opts->have_squash_onto = 1; | |
3418 | hex = oid_to_hex(&opts->squash_onto); | |
3419 | if (write_message(hex, strlen(hex), | |
3420 | rebase_path_squash_onto(), 0)) | |
3421 | return error(_("writing squash-onto")); | |
3422 | } | |
3423 | oidcpy(&oid, &opts->squash_onto); | |
3424 | } else { | |
71571cd7 MÅ |
3425 | int i; |
3426 | ||
ebddf393 JS |
3427 | /* Determine the length of the label */ |
3428 | for (i = 0; i < len; i++) | |
3429 | if (isspace(name[i])) | |
71571cd7 MÅ |
3430 | break; |
3431 | len = i; | |
ebddf393 JS |
3432 | |
3433 | strbuf_addf(&ref_name, "refs/rewritten/%.*s", len, name); | |
3434 | if (get_oid(ref_name.buf, &oid) && | |
3435 | get_oid(ref_name.buf + strlen("refs/rewritten/"), &oid)) { | |
3436 | error(_("could not read '%s'"), ref_name.buf); | |
3437 | rollback_lock_file(&lock); | |
3438 | strbuf_release(&ref_name); | |
3439 | return -1; | |
3440 | } | |
9055e401 JS |
3441 | } |
3442 | ||
3443 | memset(&unpack_tree_opts, 0, sizeof(unpack_tree_opts)); | |
3444 | setup_unpack_trees_porcelain(&unpack_tree_opts, "reset"); | |
3445 | unpack_tree_opts.head_idx = 1; | |
f11c9580 NTND |
3446 | unpack_tree_opts.src_index = r->index; |
3447 | unpack_tree_opts.dst_index = r->index; | |
9055e401 JS |
3448 | unpack_tree_opts.fn = oneway_merge; |
3449 | unpack_tree_opts.merge = 1; | |
3450 | unpack_tree_opts.update = 1; | |
3f267856 | 3451 | init_checkout_metadata(&unpack_tree_opts.meta, name, &oid, NULL); |
9055e401 | 3452 | |
e1ff0a32 | 3453 | if (repo_read_index_unmerged(r)) { |
9055e401 JS |
3454 | rollback_lock_file(&lock); |
3455 | strbuf_release(&ref_name); | |
3456 | return error_resolve_conflict(_(action_name(opts))); | |
3457 | } | |
3458 | ||
5e575807 | 3459 | if (!fill_tree_descriptor(r, &desc, &oid)) { |
9055e401 JS |
3460 | error(_("failed to find tree of %s"), oid_to_hex(&oid)); |
3461 | rollback_lock_file(&lock); | |
3462 | free((void *)desc.buffer); | |
3463 | strbuf_release(&ref_name); | |
3464 | return -1; | |
3465 | } | |
3466 | ||
3467 | if (unpack_trees(1, &desc, &unpack_tree_opts)) { | |
3468 | rollback_lock_file(&lock); | |
3469 | free((void *)desc.buffer); | |
3470 | strbuf_release(&ref_name); | |
3471 | return -1; | |
3472 | } | |
3473 | ||
3474 | tree = parse_tree_indirect(&oid); | |
c207e9e1 | 3475 | prime_cache_tree(r, r->index, tree); |
9055e401 | 3476 | |
f11c9580 | 3477 | if (write_locked_index(r->index, &lock, COMMIT_LOCK) < 0) |
9055e401 JS |
3478 | ret = error(_("could not write index")); |
3479 | free((void *)desc.buffer); | |
3480 | ||
3481 | if (!ret) | |
3482 | ret = update_ref(reflog_message(opts, "reset", "'%.*s'", | |
3483 | len, name), "HEAD", &oid, | |
3484 | NULL, 0, UPDATE_REFS_MSG_ON_ERR); | |
3485 | ||
3486 | strbuf_release(&ref_name); | |
3487 | return ret; | |
3488 | } | |
3489 | ||
2b6ad0f4 JS |
3490 | static struct commit *lookup_label(const char *label, int len, |
3491 | struct strbuf *buf) | |
3492 | { | |
3493 | struct commit *commit; | |
3494 | ||
3495 | strbuf_reset(buf); | |
3496 | strbuf_addf(buf, "refs/rewritten/%.*s", len, label); | |
3497 | commit = lookup_commit_reference_by_name(buf->buf); | |
3498 | if (!commit) { | |
3499 | /* fall back to non-rewritten ref or commit */ | |
3500 | strbuf_splice(buf, 0, strlen("refs/rewritten/"), "", 0); | |
3501 | commit = lookup_commit_reference_by_name(buf->buf); | |
3502 | } | |
3503 | ||
3504 | if (!commit) | |
3505 | error(_("could not resolve '%s'"), buf->buf); | |
3506 | ||
3507 | return commit; | |
3508 | } | |
3509 | ||
f11c9580 NTND |
3510 | static int do_merge(struct repository *r, |
3511 | struct commit *commit, | |
3512 | const char *arg, int arg_len, | |
4c68e7dd JS |
3513 | int flags, struct replay_opts *opts) |
3514 | { | |
3515 | int run_commit_flags = (flags & TODO_EDIT_MERGE_MSG) ? | |
3516 | EDIT_MSG | VERIFY_MSG : 0; | |
3517 | struct strbuf ref_name = STRBUF_INIT; | |
3518 | struct commit *head_commit, *merge_commit, *i; | |
3519 | struct commit_list *bases, *j, *reversed = NULL; | |
2b6ad0f4 | 3520 | struct commit_list *to_merge = NULL, **tail = &to_merge; |
e145d993 | 3521 | const char *strategy = !opts->xopts_nr && |
14c4586c EN |
3522 | (!opts->strategy || |
3523 | !strcmp(opts->strategy, "recursive") || | |
3524 | !strcmp(opts->strategy, "ort")) ? | |
e145d993 | 3525 | NULL : opts->strategy; |
4c68e7dd | 3526 | struct merge_options o; |
2b6ad0f4 | 3527 | int merge_arg_len, oneline_offset, can_fast_forward, ret, k; |
4c68e7dd JS |
3528 | static struct lock_file lock; |
3529 | const char *p; | |
3530 | ||
3a95f31d | 3531 | if (repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) { |
4c68e7dd JS |
3532 | ret = -1; |
3533 | goto leave_merge; | |
3534 | } | |
3535 | ||
3536 | head_commit = lookup_commit_reference_by_name("HEAD"); | |
3537 | if (!head_commit) { | |
3538 | ret = error(_("cannot merge without a current revision")); | |
3539 | goto leave_merge; | |
3540 | } | |
3541 | ||
2b6ad0f4 JS |
3542 | /* |
3543 | * For octopus merges, the arg starts with the list of revisions to be | |
3544 | * merged. The list is optionally followed by '#' and the oneline. | |
3545 | */ | |
3546 | merge_arg_len = oneline_offset = arg_len; | |
3547 | for (p = arg; p - arg < arg_len; p += strspn(p, " \t\n")) { | |
3548 | if (!*p) | |
3549 | break; | |
3550 | if (*p == '#' && (!p[1] || isspace(p[1]))) { | |
3551 | p += 1 + strspn(p + 1, " \t\n"); | |
3552 | oneline_offset = p - arg; | |
3553 | break; | |
3554 | } | |
3555 | k = strcspn(p, " \t\n"); | |
3556 | if (!k) | |
3557 | continue; | |
3558 | merge_commit = lookup_label(p, k, &ref_name); | |
3559 | if (!merge_commit) { | |
3560 | ret = error(_("unable to parse '%.*s'"), k, p); | |
3561 | goto leave_merge; | |
3562 | } | |
3563 | tail = &commit_list_insert(merge_commit, tail)->next; | |
3564 | p += k; | |
3565 | merge_arg_len = p - arg; | |
4c68e7dd JS |
3566 | } |
3567 | ||
2b6ad0f4 JS |
3568 | if (!to_merge) { |
3569 | ret = error(_("nothing to merge: '%.*s'"), arg_len, arg); | |
4c68e7dd JS |
3570 | goto leave_merge; |
3571 | } | |
3572 | ||
9c85a1c2 | 3573 | if (opts->have_squash_onto && |
4a7e27e9 | 3574 | oideq(&head_commit->object.oid, &opts->squash_onto)) { |
9c85a1c2 JS |
3575 | /* |
3576 | * When the user tells us to "merge" something into a | |
3577 | * "[new root]", let's simply fast-forward to the merge head. | |
3578 | */ | |
3579 | rollback_lock_file(&lock); | |
2b6ad0f4 JS |
3580 | if (to_merge->next) |
3581 | ret = error(_("octopus merge cannot be executed on " | |
3582 | "top of a [new root]")); | |
3583 | else | |
f11c9580 | 3584 | ret = fast_forward_to(r, &to_merge->item->object.oid, |
2b6ad0f4 JS |
3585 | &head_commit->object.oid, 0, |
3586 | opts); | |
9c85a1c2 JS |
3587 | goto leave_merge; |
3588 | } | |
3589 | ||
4c68e7dd | 3590 | if (commit) { |
5772b0c7 ĐTCD |
3591 | const char *encoding = get_commit_output_encoding(); |
3592 | const char *message = logmsg_reencode(commit, NULL, encoding); | |
4c68e7dd JS |
3593 | const char *body; |
3594 | int len; | |
3595 | ||
3596 | if (!message) { | |
3597 | ret = error(_("could not get commit message of '%s'"), | |
3598 | oid_to_hex(&commit->object.oid)); | |
3599 | goto leave_merge; | |
3600 | } | |
3601 | write_author_script(message); | |
3602 | find_commit_subject(message, &body); | |
3603 | len = strlen(body); | |
f11c9580 | 3604 | ret = write_message(body, len, git_path_merge_msg(r), 0); |
4c68e7dd JS |
3605 | unuse_commit_buffer(commit, message); |
3606 | if (ret) { | |
3607 | error_errno(_("could not write '%s'"), | |
f11c9580 | 3608 | git_path_merge_msg(r)); |
4c68e7dd JS |
3609 | goto leave_merge; |
3610 | } | |
3611 | } else { | |
3612 | struct strbuf buf = STRBUF_INIT; | |
3613 | int len; | |
3614 | ||
3615 | strbuf_addf(&buf, "author %s", git_author_info(0)); | |
3616 | write_author_script(buf.buf); | |
3617 | strbuf_reset(&buf); | |
3618 | ||
3619 | if (oneline_offset < arg_len) { | |
3620 | p = arg + oneline_offset; | |
3621 | len = arg_len - oneline_offset; | |
3622 | } else { | |
2b6ad0f4 JS |
3623 | strbuf_addf(&buf, "Merge %s '%.*s'", |
3624 | to_merge->next ? "branches" : "branch", | |
4c68e7dd JS |
3625 | merge_arg_len, arg); |
3626 | p = buf.buf; | |
3627 | len = buf.len; | |
3628 | } | |
3629 | ||
f11c9580 | 3630 | ret = write_message(p, len, git_path_merge_msg(r), 0); |
4c68e7dd JS |
3631 | strbuf_release(&buf); |
3632 | if (ret) { | |
3633 | error_errno(_("could not write '%s'"), | |
f11c9580 | 3634 | git_path_merge_msg(r)); |
4c68e7dd JS |
3635 | goto leave_merge; |
3636 | } | |
3637 | } | |
3638 | ||
d1e8b011 JS |
3639 | /* |
3640 | * If HEAD is not identical to the first parent of the original merge | |
3641 | * commit, we cannot fast-forward. | |
3642 | */ | |
3643 | can_fast_forward = opts->allow_ff && commit && commit->parents && | |
4a7e27e9 JK |
3644 | oideq(&commit->parents->item->object.oid, |
3645 | &head_commit->object.oid); | |
d1e8b011 JS |
3646 | |
3647 | /* | |
2b6ad0f4 | 3648 | * If any merge head is different from the original one, we cannot |
d1e8b011 JS |
3649 | * fast-forward. |
3650 | */ | |
3651 | if (can_fast_forward) { | |
2b6ad0f4 | 3652 | struct commit_list *p = commit->parents->next; |
d1e8b011 | 3653 | |
2b6ad0f4 | 3654 | for (j = to_merge; j && p; j = j->next, p = p->next) |
9001dc2a | 3655 | if (!oideq(&j->item->object.oid, |
2b6ad0f4 JS |
3656 | &p->item->object.oid)) { |
3657 | can_fast_forward = 0; | |
3658 | break; | |
3659 | } | |
3660 | /* | |
3661 | * If the number of merge heads differs from the original merge | |
3662 | * commit, we cannot fast-forward. | |
3663 | */ | |
3664 | if (j || p) | |
d1e8b011 JS |
3665 | can_fast_forward = 0; |
3666 | } | |
3667 | ||
2b6ad0f4 | 3668 | if (can_fast_forward) { |
d1e8b011 | 3669 | rollback_lock_file(&lock); |
f11c9580 | 3670 | ret = fast_forward_to(r, &commit->object.oid, |
d1e8b011 | 3671 | &head_commit->object.oid, 0, opts); |
6df8df08 PW |
3672 | if (flags & TODO_EDIT_MERGE_MSG) { |
3673 | run_commit_flags |= AMEND_MSG; | |
3674 | goto fast_forward_edit; | |
3675 | } | |
d1e8b011 JS |
3676 | goto leave_merge; |
3677 | } | |
3678 | ||
e145d993 | 3679 | if (strategy || to_merge->next) { |
2b6ad0f4 JS |
3680 | /* Octopus merge */ |
3681 | struct child_process cmd = CHILD_PROCESS_INIT; | |
3682 | ||
3683 | if (read_env_script(&cmd.env_array)) { | |
3684 | const char *gpg_opt = gpg_sign_opt_quoted(opts); | |
3685 | ||
3686 | ret = error(_(staged_changes_advice), gpg_opt, gpg_opt); | |
3687 | goto leave_merge; | |
3688 | } | |
3689 | ||
7573cec5 | 3690 | if (opts->committer_date_is_author_date) |
9c31b19d JH |
3691 | strvec_pushf(&cmd.env_array, "GIT_COMMITTER_DATE=%s", |
3692 | opts->ignore_date ? | |
3693 | "" : | |
3694 | author_date_from_env_array(&cmd.env_array)); | |
a3894aad | 3695 | if (opts->ignore_date) |
9c31b19d | 3696 | strvec_push(&cmd.env_array, "GIT_AUTHOR_DATE="); |
7573cec5 | 3697 | |
2b6ad0f4 | 3698 | cmd.git_cmd = 1; |
c972bf4c JK |
3699 | strvec_push(&cmd.args, "merge"); |
3700 | strvec_push(&cmd.args, "-s"); | |
e145d993 | 3701 | if (!strategy) |
c972bf4c | 3702 | strvec_push(&cmd.args, "octopus"); |
e145d993 | 3703 | else { |
c972bf4c | 3704 | strvec_push(&cmd.args, strategy); |
e145d993 | 3705 | for (k = 0; k < opts->xopts_nr; k++) |
c972bf4c | 3706 | strvec_pushf(&cmd.args, |
f6d8942b | 3707 | "-X%s", opts->xopts[k]); |
e145d993 | 3708 | } |
c972bf4c JK |
3709 | strvec_push(&cmd.args, "--no-edit"); |
3710 | strvec_push(&cmd.args, "--no-ff"); | |
3711 | strvec_push(&cmd.args, "--no-log"); | |
3712 | strvec_push(&cmd.args, "--no-stat"); | |
3713 | strvec_push(&cmd.args, "-F"); | |
3714 | strvec_push(&cmd.args, git_path_merge_msg(r)); | |
2b6ad0f4 | 3715 | if (opts->gpg_sign) |
ae03c97a | 3716 | strvec_pushf(&cmd.args, "-S%s", opts->gpg_sign); |
19dad040 SČ |
3717 | else |
3718 | strvec_push(&cmd.args, "--no-gpg-sign"); | |
2b6ad0f4 JS |
3719 | |
3720 | /* Add the tips to be merged */ | |
3721 | for (j = to_merge; j; j = j->next) | |
c972bf4c | 3722 | strvec_push(&cmd.args, |
f6d8942b | 3723 | oid_to_hex(&j->item->object.oid)); |
2b6ad0f4 JS |
3724 | |
3725 | strbuf_release(&ref_name); | |
c8e4159e HWN |
3726 | refs_delete_ref(get_main_ref_store(r), "", "CHERRY_PICK_HEAD", |
3727 | NULL, 0); | |
2b6ad0f4 JS |
3728 | rollback_lock_file(&lock); |
3729 | ||
2b6ad0f4 JS |
3730 | ret = run_command(&cmd); |
3731 | ||
3732 | /* force re-reading of the cache */ | |
f11c9580 | 3733 | if (!ret && (discard_index(r->index) < 0 || |
e1ff0a32 | 3734 | repo_read_index(r) < 0)) |
2b6ad0f4 JS |
3735 | ret = error(_("could not read index")); |
3736 | goto leave_merge; | |
3737 | } | |
3738 | ||
3739 | merge_commit = to_merge->item; | |
4c68e7dd | 3740 | bases = get_merge_bases(head_commit, merge_commit); |
4a7e27e9 JK |
3741 | if (bases && oideq(&merge_commit->object.oid, |
3742 | &bases->item->object.oid)) { | |
7ccdf65b JS |
3743 | ret = 0; |
3744 | /* skip merging an ancestor of HEAD */ | |
3745 | goto leave_merge; | |
3746 | } | |
3747 | ||
4439c7a3 | 3748 | write_message(oid_to_hex(&merge_commit->object.oid), the_hash_algo->hexsz, |
cde55548 JH |
3749 | git_path_merge_head(r), 0); |
3750 | write_message("no-ff", 5, git_path_merge_mode(r), 0); | |
85f8d9da | 3751 | |
4c68e7dd JS |
3752 | for (j = bases; j; j = j->next) |
3753 | commit_list_insert(j->item, &reversed); | |
3754 | free_commit_list(bases); | |
3755 | ||
e1ff0a32 | 3756 | repo_read_index(r); |
0d6caa2d | 3757 | init_merge_options(&o, r); |
4c68e7dd JS |
3758 | o.branch1 = "HEAD"; |
3759 | o.branch2 = ref_name.buf; | |
3760 | o.buffer_output = 2; | |
3761 | ||
14c4586c EN |
3762 | if (opts->strategy && !strcmp(opts->strategy, "ort")) { |
3763 | /* | |
3764 | * TODO: Should use merge_incore_recursive() and | |
3765 | * merge_switch_to_result(), skipping the call to | |
3766 | * merge_switch_to_result() when we don't actually need to | |
3767 | * update the index and working copy immediately. | |
3768 | */ | |
3769 | ret = merge_ort_recursive(&o, | |
3770 | head_commit, merge_commit, reversed, | |
3771 | &i); | |
3772 | } else { | |
3773 | ret = merge_recursive(&o, head_commit, merge_commit, reversed, | |
3774 | &i); | |
3775 | } | |
4c68e7dd JS |
3776 | if (ret <= 0) |
3777 | fputs(o.obuf.buf, stdout); | |
3778 | strbuf_release(&o.obuf); | |
3779 | if (ret < 0) { | |
3780 | error(_("could not even attempt to merge '%.*s'"), | |
3781 | merge_arg_len, arg); | |
3782 | goto leave_merge; | |
3783 | } | |
3784 | /* | |
3785 | * The return value of merge_recursive() is 1 on clean, and 0 on | |
3786 | * unclean merge. | |
3787 | * | |
3788 | * Let's reverse that, so that do_merge() returns 0 upon success and | |
3789 | * 1 upon failed merge (keeping the return value -1 for the cases where | |
3790 | * we will want to reschedule the `merge` command). | |
3791 | */ | |
3792 | ret = !ret; | |
3793 | ||
f11c9580 NTND |
3794 | if (r->index->cache_changed && |
3795 | write_locked_index(r->index, &lock, COMMIT_LOCK)) { | |
4c68e7dd JS |
3796 | ret = error(_("merge: Unable to write new index file")); |
3797 | goto leave_merge; | |
3798 | } | |
3799 | ||
3800 | rollback_lock_file(&lock); | |
3801 | if (ret) | |
f11c9580 | 3802 | repo_rerere(r, opts->allow_rerere_auto); |
4c68e7dd JS |
3803 | else |
3804 | /* | |
3805 | * In case of problems, we now want to return a positive | |
3806 | * value (a negative one would indicate that the `merge` | |
3807 | * command needs to be rescheduled). | |
3808 | */ | |
6df8df08 | 3809 | fast_forward_edit: |
20f4b044 | 3810 | ret = !!run_git_commit(git_path_merge_msg(r), opts, |
f11c9580 | 3811 | run_commit_flags); |
4c68e7dd JS |
3812 | |
3813 | leave_merge: | |
3814 | strbuf_release(&ref_name); | |
3815 | rollback_lock_file(&lock); | |
2b6ad0f4 | 3816 | free_commit_list(to_merge); |
4c68e7dd JS |
3817 | return ret; |
3818 | } | |
3819 | ||
6e98de72 JS |
3820 | static int is_final_fixup(struct todo_list *todo_list) |
3821 | { | |
3822 | int i = todo_list->current; | |
3823 | ||
3824 | if (!is_fixup(todo_list->items[i].command)) | |
3825 | return 0; | |
3826 | ||
3827 | while (++i < todo_list->nr) | |
3828 | if (is_fixup(todo_list->items[i].command)) | |
3829 | return 0; | |
3830 | else if (!is_noop(todo_list->items[i].command)) | |
3831 | break; | |
3832 | return 1; | |
3833 | } | |
3834 | ||
25cb8df9 JS |
3835 | static enum todo_command peek_command(struct todo_list *todo_list, int offset) |
3836 | { | |
3837 | int i; | |
3838 | ||
3839 | for (i = todo_list->current + offset; i < todo_list->nr; i++) | |
3840 | if (!is_noop(todo_list->items[i].command)) | |
3841 | return todo_list->items[i].command; | |
3842 | ||
3843 | return -1; | |
3844 | } | |
3845 | ||
0816f1df DL |
3846 | void create_autostash(struct repository *r, const char *path, |
3847 | const char *default_reflog_action) | |
3848 | { | |
3849 | struct strbuf buf = STRBUF_INIT; | |
3850 | struct lock_file lock_file = LOCK_INIT; | |
3851 | int fd; | |
3852 | ||
3853 | fd = repo_hold_locked_index(r, &lock_file, 0); | |
3854 | refresh_index(r->index, REFRESH_QUIET, NULL, NULL, NULL); | |
3855 | if (0 <= fd) | |
3856 | repo_update_index_if_able(r, &lock_file); | |
3857 | rollback_lock_file(&lock_file); | |
3858 | ||
3859 | if (has_unstaged_changes(r, 1) || | |
3860 | has_uncommitted_changes(r, 1)) { | |
3861 | struct child_process stash = CHILD_PROCESS_INIT; | |
3862 | struct object_id oid; | |
3863 | ||
c972bf4c | 3864 | strvec_pushl(&stash.args, |
f6d8942b | 3865 | "stash", "create", "autostash", NULL); |
0816f1df DL |
3866 | stash.git_cmd = 1; |
3867 | stash.no_stdin = 1; | |
3868 | strbuf_reset(&buf); | |
3869 | if (capture_command(&stash, &buf, GIT_MAX_HEXSZ)) | |
3870 | die(_("Cannot autostash")); | |
3871 | strbuf_trim_trailing_newline(&buf); | |
3872 | if (get_oid(buf.buf, &oid)) | |
3873 | die(_("Unexpected stash response: '%s'"), | |
3874 | buf.buf); | |
3875 | strbuf_reset(&buf); | |
3876 | strbuf_add_unique_abbrev(&buf, &oid, DEFAULT_ABBREV); | |
3877 | ||
3878 | if (safe_create_leading_directories_const(path)) | |
3879 | die(_("Could not create directory for '%s'"), | |
3880 | path); | |
3881 | write_file(path, "%s", oid_to_hex(&oid)); | |
3882 | printf(_("Created autostash: %s\n"), buf.buf); | |
3883 | if (reset_head(r, NULL, "reset --hard", | |
3884 | NULL, RESET_HEAD_HARD, NULL, NULL, | |
3885 | default_reflog_action) < 0) | |
3886 | die(_("could not reset --hard")); | |
3887 | ||
3888 | if (discard_index(r->index) < 0 || | |
3889 | repo_read_index(r) < 0) | |
3890 | die(_("could not read index")); | |
3891 | } | |
3892 | strbuf_release(&buf); | |
3893 | } | |
3894 | ||
804fe315 | 3895 | static int apply_save_autostash_oid(const char *stash_oid, int attempt_apply) |
796c7972 | 3896 | { |
796c7972 JS |
3897 | struct child_process child = CHILD_PROCESS_INIT; |
3898 | int ret = 0; | |
3899 | ||
12b6e136 DL |
3900 | if (attempt_apply) { |
3901 | child.git_cmd = 1; | |
3902 | child.no_stdout = 1; | |
3903 | child.no_stderr = 1; | |
c972bf4c JK |
3904 | strvec_push(&child.args, "stash"); |
3905 | strvec_push(&child.args, "apply"); | |
3906 | strvec_push(&child.args, stash_oid); | |
12b6e136 | 3907 | ret = run_command(&child); |
796c7972 | 3908 | } |
796c7972 | 3909 | |
12b6e136 | 3910 | if (attempt_apply && !ret) |
cdb866b3 | 3911 | fprintf(stderr, _("Applied autostash.\n")); |
796c7972 JS |
3912 | else { |
3913 | struct child_process store = CHILD_PROCESS_INIT; | |
3914 | ||
3915 | store.git_cmd = 1; | |
c972bf4c JK |
3916 | strvec_push(&store.args, "stash"); |
3917 | strvec_push(&store.args, "store"); | |
3918 | strvec_push(&store.args, "-m"); | |
3919 | strvec_push(&store.args, "autostash"); | |
3920 | strvec_push(&store.args, "-q"); | |
3921 | strvec_push(&store.args, stash_oid); | |
796c7972 | 3922 | if (run_command(&store)) |
804fe315 | 3923 | ret = error(_("cannot store %s"), stash_oid); |
796c7972 | 3924 | else |
cdb866b3 | 3925 | fprintf(stderr, |
12b6e136 | 3926 | _("%s\n" |
cdb866b3 JS |
3927 | "Your changes are safe in the stash.\n" |
3928 | "You can run \"git stash pop\" or" | |
12b6e136 DL |
3929 | " \"git stash drop\" at any time.\n"), |
3930 | attempt_apply ? | |
3931 | _("Applying autostash resulted in conflicts.") : | |
3932 | _("Autostash exists; creating a new stash entry.")); | |
796c7972 JS |
3933 | } |
3934 | ||
796c7972 JS |
3935 | return ret; |
3936 | } | |
3937 | ||
804fe315 DL |
3938 | static int apply_save_autostash(const char *path, int attempt_apply) |
3939 | { | |
3940 | struct strbuf stash_oid = STRBUF_INIT; | |
3941 | int ret = 0; | |
3942 | ||
3943 | if (!read_oneliner(&stash_oid, path, | |
3944 | READ_ONELINER_SKIP_IF_EMPTY)) { | |
3945 | strbuf_release(&stash_oid); | |
3946 | return 0; | |
3947 | } | |
3948 | strbuf_trim(&stash_oid); | |
3949 | ||
3950 | ret = apply_save_autostash_oid(stash_oid.buf, attempt_apply); | |
3951 | ||
0dd562e0 | 3952 | unlink(path); |
facca7f0 | 3953 | strbuf_release(&stash_oid); |
796c7972 JS |
3954 | return ret; |
3955 | } | |
3956 | ||
12b6e136 DL |
3957 | int save_autostash(const char *path) |
3958 | { | |
3959 | return apply_save_autostash(path, 0); | |
3960 | } | |
3961 | ||
3962 | int apply_autostash(const char *path) | |
3963 | { | |
3964 | return apply_save_autostash(path, 1); | |
3965 | } | |
3966 | ||
804fe315 DL |
3967 | int apply_autostash_oid(const char *stash_oid) |
3968 | { | |
3969 | return apply_save_autostash_oid(stash_oid, 1); | |
3970 | } | |
3971 | ||
96e832a5 JS |
3972 | static const char *reflog_message(struct replay_opts *opts, |
3973 | const char *sub_action, const char *fmt, ...) | |
3974 | { | |
3975 | va_list ap; | |
3976 | static struct strbuf buf = STRBUF_INIT; | |
1f6965f9 | 3977 | char *reflog_action = getenv(GIT_REFLOG_ACTION); |
96e832a5 JS |
3978 | |
3979 | va_start(ap, fmt); | |
3980 | strbuf_reset(&buf); | |
1f6965f9 | 3981 | strbuf_addstr(&buf, reflog_action ? reflog_action : action_name(opts)); |
96e832a5 JS |
3982 | if (sub_action) |
3983 | strbuf_addf(&buf, " (%s)", sub_action); | |
3984 | if (fmt) { | |
3985 | strbuf_addstr(&buf, ": "); | |
3986 | strbuf_vaddf(&buf, fmt, ap); | |
3987 | } | |
3988 | va_end(ap); | |
3989 | ||
3990 | return buf.buf; | |
3991 | } | |
3992 | ||
fc4a6735 PW |
3993 | static int run_git_checkout(struct repository *r, struct replay_opts *opts, |
3994 | const char *commit, const char *action) | |
2c58483a AG |
3995 | { |
3996 | struct child_process cmd = CHILD_PROCESS_INIT; | |
fc4a6735 | 3997 | int ret; |
2c58483a AG |
3998 | |
3999 | cmd.git_cmd = 1; | |
4000 | ||
c972bf4c JK |
4001 | strvec_push(&cmd.args, "checkout"); |
4002 | strvec_push(&cmd.args, commit); | |
4003 | strvec_pushf(&cmd.env_array, GIT_REFLOG_ACTION "=%s", action); | |
2c58483a AG |
4004 | |
4005 | if (opts->verbose) | |
fc4a6735 | 4006 | ret = run_command(&cmd); |
2c58483a | 4007 | else |
fc4a6735 PW |
4008 | ret = run_command_silent_on_success(&cmd); |
4009 | ||
4010 | if (!ret) | |
4011 | discard_index(r->index); | |
4012 | ||
4013 | return ret; | |
2c58483a AG |
4014 | } |
4015 | ||
fc4a6735 | 4016 | static int checkout_onto(struct repository *r, struct replay_opts *opts, |
7d3488eb | 4017 | const char *onto_name, const struct object_id *onto, |
f3e27a02 | 4018 | const struct object_id *orig_head) |
4df66c40 | 4019 | { |
4df66c40 AG |
4020 | const char *action = reflog_message(opts, "start", "checkout %s", onto_name); |
4021 | ||
7d3488eb | 4022 | if (run_git_checkout(r, opts, oid_to_hex(onto), action)) { |
be1bb600 | 4023 | apply_autostash(rebase_path_autostash()); |
4df66c40 AG |
4024 | sequencer_remove_state(opts); |
4025 | return error(_("could not detach HEAD")); | |
4026 | } | |
4027 | ||
f3e27a02 | 4028 | return update_ref(NULL, "ORIG_HEAD", orig_head, NULL, 0, UPDATE_REFS_MSG_ON_ERR); |
4df66c40 AG |
4029 | } |
4030 | ||
005af339 | 4031 | static int stopped_at_head(struct repository *r) |
71f82465 JS |
4032 | { |
4033 | struct object_id head; | |
4034 | struct commit *commit; | |
4035 | struct commit_message message; | |
4036 | ||
789b1f70 | 4037 | if (get_oid("HEAD", &head) || |
005af339 | 4038 | !(commit = lookup_commit(r, &head)) || |
71f82465 JS |
4039 | parse_commit(commit) || get_message(commit, &message)) |
4040 | fprintf(stderr, _("Stopped at HEAD\n")); | |
4041 | else { | |
4042 | fprintf(stderr, _("Stopped at %s\n"), message.label); | |
4043 | free_message(commit, &message); | |
4044 | } | |
4045 | return 0; | |
4046 | ||
4047 | } | |
4048 | ||
cb5206ea JS |
4049 | static const char rescheduled_advice[] = |
4050 | N_("Could not execute the todo command\n" | |
4051 | "\n" | |
4052 | " %.*s" | |
4053 | "\n" | |
4054 | "It has been rescheduled; To edit the command before continuing, please\n" | |
4055 | "edit the todo list first:\n" | |
4056 | "\n" | |
4057 | " git rebase --edit-todo\n" | |
4058 | " git rebase --continue\n"); | |
4059 | ||
f11c9580 NTND |
4060 | static int pick_commits(struct repository *r, |
4061 | struct todo_list *todo_list, | |
4062 | struct replay_opts *opts) | |
043a4492 | 4063 | { |
9055e401 | 4064 | int res = 0, reschedule = 0; |
1f6965f9 | 4065 | char *prev_reflog_action; |
043a4492 | 4066 | |
1f6965f9 | 4067 | /* Note that 0 for 3rd parameter of setenv means set only if not set */ |
043a4492 | 4068 | setenv(GIT_REFLOG_ACTION, action_name(opts), 0); |
1f6965f9 | 4069 | prev_reflog_action = xstrdup(getenv(GIT_REFLOG_ACTION)); |
043a4492 RR |
4070 | if (opts->allow_ff) |
4071 | assert(!(opts->signoff || opts->no_commit || | |
7573cec5 | 4072 | opts->record_origin || opts->edit || |
a3894aad PW |
4073 | opts->committer_date_is_author_date || |
4074 | opts->ignore_date)); | |
f11c9580 | 4075 | if (read_and_refresh_cache(r, opts)) |
0d9c6dc9 | 4076 | return -1; |
043a4492 | 4077 | |
004fefa7 JS |
4078 | while (todo_list->current < todo_list->nr) { |
4079 | struct todo_item *item = todo_list->items + todo_list->current; | |
6ad656db | 4080 | const char *arg = todo_item_get_arg(todo_list, item); |
a47ba3c7 | 4081 | int check_todo = 0; |
6ad656db | 4082 | |
004fefa7 | 4083 | if (save_todo(todo_list, opts)) |
221675de | 4084 | return -1; |
6e98de72 | 4085 | if (is_rebase_i(opts)) { |
ef80069a JS |
4086 | if (item->command != TODO_COMMENT) { |
4087 | FILE *f = fopen(rebase_path_msgnum(), "w"); | |
4088 | ||
4089 | todo_list->done_nr++; | |
4090 | ||
4091 | if (f) { | |
4092 | fprintf(f, "%d\n", todo_list->done_nr); | |
4093 | fclose(f); | |
4094 | } | |
899b49c4 | 4095 | if (!opts->quiet) |
4d55d63b | 4096 | fprintf(stderr, _("Rebasing (%d/%d)%s"), |
899b49c4 EN |
4097 | todo_list->done_nr, |
4098 | todo_list->total_nr, | |
4099 | opts->verbose ? "\n" : "\r"); | |
ef80069a | 4100 | } |
6e98de72 JS |
4101 | unlink(rebase_path_message()); |
4102 | unlink(rebase_path_author_script()); | |
4103 | unlink(rebase_path_stopped_sha()); | |
4104 | unlink(rebase_path_amend()); | |
34e7771b | 4105 | unlink(git_path_merge_head(r)); |
fbd7a232 | 4106 | delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF); |
71f82465 | 4107 | |
d7d90885 SG |
4108 | if (item->command == TODO_BREAK) { |
4109 | if (!opts->verbose) | |
4110 | term_clear_line(); | |
005af339 | 4111 | return stopped_at_head(r); |
d7d90885 | 4112 | } |
6e98de72 JS |
4113 | } |
4114 | if (item->command <= TODO_SQUASH) { | |
8ab37ef2 | 4115 | if (is_rebase_i(opts)) |
1f6965f9 | 4116 | setenv(GIT_REFLOG_ACTION, reflog_message(opts, |
8ab37ef2 JS |
4117 | command_to_string(item->command), NULL), |
4118 | 1); | |
f11c9580 | 4119 | res = do_pick_commit(r, item->command, item->commit, |
a47ba3c7 PW |
4120 | opts, is_final_fixup(todo_list), |
4121 | &check_todo); | |
1f6965f9 EN |
4122 | if (is_rebase_i(opts)) |
4123 | setenv(GIT_REFLOG_ACTION, prev_reflog_action, 1); | |
9d7bf3cf JS |
4124 | if (is_rebase_i(opts) && res < 0) { |
4125 | /* Reschedule */ | |
cb5206ea JS |
4126 | advise(_(rescheduled_advice), |
4127 | get_item_line_length(todo_list, | |
4128 | todo_list->current), | |
4129 | get_item_line(todo_list, | |
4130 | todo_list->current)); | |
9d7bf3cf JS |
4131 | todo_list->current--; |
4132 | if (save_todo(todo_list, opts)) | |
4133 | return -1; | |
4134 | } | |
56dc3ab0 JS |
4135 | if (item->command == TODO_EDIT) { |
4136 | struct commit *commit = item->commit; | |
d7d90885 SG |
4137 | if (!res) { |
4138 | if (!opts->verbose) | |
4139 | term_clear_line(); | |
99429213 | 4140 | fprintf(stderr, |
a42e1b41 | 4141 | _("Stopped at %s... %.*s\n"), |
56dc3ab0 | 4142 | short_commit_name(commit), |
6ad656db | 4143 | item->arg_len, arg); |
d7d90885 | 4144 | } |
f11c9580 | 4145 | return error_with_patch(r, commit, |
6ad656db | 4146 | arg, item->arg_len, opts, res, !res); |
56dc3ab0 | 4147 | } |
25cb8df9 JS |
4148 | if (is_rebase_i(opts) && !res) |
4149 | record_in_rewritten(&item->commit->object.oid, | |
4150 | peek_command(todo_list, 1)); | |
6e98de72 JS |
4151 | if (res && is_fixup(item->command)) { |
4152 | if (res == 1) | |
4153 | intend_to_amend(); | |
f11c9580 | 4154 | return error_failed_squash(r, item->commit, opts, |
6ad656db | 4155 | item->arg_len, arg); |
a9279c67 PW |
4156 | } else if (res && is_rebase_i(opts) && item->commit) { |
4157 | int to_amend = 0; | |
4158 | struct object_id oid; | |
4159 | ||
4160 | /* | |
4161 | * If we are rewording and have either | |
4162 | * fast-forwarded already, or are about to | |
4163 | * create a new root commit, we want to amend, | |
4164 | * otherwise we do not. | |
4165 | */ | |
4166 | if (item->command == TODO_REWORD && | |
4167 | !get_oid("HEAD", &oid) && | |
4a7e27e9 | 4168 | (oideq(&item->commit->object.oid, &oid) || |
a9279c67 | 4169 | (opts->have_squash_onto && |
4a7e27e9 | 4170 | oideq(&opts->squash_onto, &oid)))) |
a9279c67 PW |
4171 | to_amend = 1; |
4172 | ||
f11c9580 | 4173 | return res | error_with_patch(r, item->commit, |
6ad656db | 4174 | arg, item->arg_len, opts, |
a9279c67 PW |
4175 | res, to_amend); |
4176 | } | |
311af526 | 4177 | } else if (item->command == TODO_EXEC) { |
6ad656db | 4178 | char *end_of_arg = (char *)(arg + item->arg_len); |
311af526 JS |
4179 | int saved = *end_of_arg; |
4180 | ||
d7d90885 SG |
4181 | if (!opts->verbose) |
4182 | term_clear_line(); | |
311af526 | 4183 | *end_of_arg = '\0'; |
6ad656db | 4184 | res = do_exec(r, arg); |
311af526 | 4185 | *end_of_arg = saved; |
54fd3243 | 4186 | |
d421afa0 JS |
4187 | if (res) { |
4188 | if (opts->reschedule_failed_exec) | |
4189 | reschedule = 1; | |
54fd3243 | 4190 | } |
a47ba3c7 | 4191 | check_todo = 1; |
9055e401 | 4192 | } else if (item->command == TODO_LABEL) { |
6ad656db | 4193 | if ((res = do_label(r, arg, item->arg_len))) |
9055e401 JS |
4194 | reschedule = 1; |
4195 | } else if (item->command == TODO_RESET) { | |
6ad656db | 4196 | if ((res = do_reset(r, arg, item->arg_len, opts))) |
9055e401 | 4197 | reschedule = 1; |
4c68e7dd | 4198 | } else if (item->command == TODO_MERGE) { |
f11c9580 | 4199 | if ((res = do_merge(r, item->commit, |
6ad656db | 4200 | arg, item->arg_len, |
4c68e7dd JS |
4201 | item->flags, opts)) < 0) |
4202 | reschedule = 1; | |
537e7d61 JS |
4203 | else if (item->commit) |
4204 | record_in_rewritten(&item->commit->object.oid, | |
4205 | peek_command(todo_list, 1)); | |
4206 | if (res > 0) | |
4c68e7dd | 4207 | /* failed with merge conflicts */ |
f11c9580 | 4208 | return error_with_patch(r, item->commit, |
6ad656db AG |
4209 | arg, item->arg_len, |
4210 | opts, res, 0); | |
56dc3ab0 | 4211 | } else if (!is_noop(item->command)) |
25c43667 JS |
4212 | return error(_("unknown command %d"), item->command); |
4213 | ||
9055e401 JS |
4214 | if (reschedule) { |
4215 | advise(_(rescheduled_advice), | |
4216 | get_item_line_length(todo_list, | |
4217 | todo_list->current), | |
4218 | get_item_line(todo_list, todo_list->current)); | |
4219 | todo_list->current--; | |
4220 | if (save_todo(todo_list, opts)) | |
4221 | return -1; | |
4c68e7dd | 4222 | if (item->commit) |
f11c9580 NTND |
4223 | return error_with_patch(r, |
4224 | item->commit, | |
6ad656db AG |
4225 | arg, item->arg_len, |
4226 | opts, res, 0); | |
befd4f6a | 4227 | } else if (is_rebase_i(opts) && check_todo && !res) { |
a47ba3c7 PW |
4228 | struct stat st; |
4229 | ||
4230 | if (stat(get_todo_path(opts), &st)) { | |
4231 | res = error_errno(_("could not stat '%s'"), | |
4232 | get_todo_path(opts)); | |
4233 | } else if (match_stat_data(&todo_list->stat, &st)) { | |
4234 | /* Reread the todo file if it has changed. */ | |
4235 | todo_list_release(todo_list); | |
4236 | if (read_populate_todo(r, todo_list, opts)) | |
4237 | res = -1; /* message was printed */ | |
4238 | /* `current` will be incremented below */ | |
4239 | todo_list->current = -1; | |
4240 | } | |
9055e401 JS |
4241 | } |
4242 | ||
004fefa7 | 4243 | todo_list->current++; |
043a4492 RR |
4244 | if (res) |
4245 | return res; | |
4246 | } | |
4247 | ||
56dc3ab0 | 4248 | if (is_rebase_i(opts)) { |
4b83ce9f | 4249 | struct strbuf head_ref = STRBUF_INIT, buf = STRBUF_INIT; |
25cb8df9 | 4250 | struct stat st; |
556907f1 | 4251 | |
56dc3ab0 JS |
4252 | /* Stopped in the middle, as planned? */ |
4253 | if (todo_list->current < todo_list->nr) | |
4254 | return 0; | |
556907f1 | 4255 | |
4b83ce9f JS |
4256 | if (read_oneliner(&head_ref, rebase_path_head_name(), 0) && |
4257 | starts_with(head_ref.buf, "refs/")) { | |
96e832a5 | 4258 | const char *msg; |
092bbcdf | 4259 | struct object_id head, orig; |
4b83ce9f JS |
4260 | int res; |
4261 | ||
092bbcdf | 4262 | if (get_oid("HEAD", &head)) { |
4b83ce9f JS |
4263 | res = error(_("cannot read HEAD")); |
4264 | cleanup_head_ref: | |
4265 | strbuf_release(&head_ref); | |
4266 | strbuf_release(&buf); | |
4267 | return res; | |
4268 | } | |
4269 | if (!read_oneliner(&buf, rebase_path_orig_head(), 0) || | |
092bbcdf | 4270 | get_oid_hex(buf.buf, &orig)) { |
4b83ce9f JS |
4271 | res = error(_("could not read orig-head")); |
4272 | goto cleanup_head_ref; | |
4273 | } | |
4ab867b8 | 4274 | strbuf_reset(&buf); |
4b83ce9f JS |
4275 | if (!read_oneliner(&buf, rebase_path_onto(), 0)) { |
4276 | res = error(_("could not read 'onto'")); | |
4277 | goto cleanup_head_ref; | |
4278 | } | |
96e832a5 JS |
4279 | msg = reflog_message(opts, "finish", "%s onto %s", |
4280 | head_ref.buf, buf.buf); | |
ae077771 | 4281 | if (update_ref(msg, head_ref.buf, &head, &orig, |
91774afc | 4282 | REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR)) { |
4b83ce9f JS |
4283 | res = error(_("could not update %s"), |
4284 | head_ref.buf); | |
4285 | goto cleanup_head_ref; | |
4286 | } | |
96e832a5 | 4287 | msg = reflog_message(opts, "finish", "returning to %s", |
4b83ce9f | 4288 | head_ref.buf); |
96e832a5 | 4289 | if (create_symref("HEAD", head_ref.buf, msg)) { |
4b83ce9f JS |
4290 | res = error(_("could not update HEAD to %s"), |
4291 | head_ref.buf); | |
4292 | goto cleanup_head_ref; | |
4293 | } | |
4294 | strbuf_reset(&buf); | |
4295 | } | |
4296 | ||
556907f1 JS |
4297 | if (opts->verbose) { |
4298 | struct rev_info log_tree_opt; | |
4299 | struct object_id orig, head; | |
4300 | ||
4301 | memset(&log_tree_opt, 0, sizeof(log_tree_opt)); | |
f11c9580 | 4302 | repo_init_revisions(r, &log_tree_opt, NULL); |
556907f1 JS |
4303 | log_tree_opt.diff = 1; |
4304 | log_tree_opt.diffopt.output_format = | |
4305 | DIFF_FORMAT_DIFFSTAT; | |
4306 | log_tree_opt.disable_stdin = 1; | |
4307 | ||
4308 | if (read_oneliner(&buf, rebase_path_orig_head(), 0) && | |
e82caf38 | 4309 | !get_oid(buf.buf, &orig) && |
4310 | !get_oid("HEAD", &head)) { | |
66f414f8 BW |
4311 | diff_tree_oid(&orig, &head, "", |
4312 | &log_tree_opt.diffopt); | |
556907f1 JS |
4313 | log_tree_diff_flush(&log_tree_opt); |
4314 | } | |
4315 | } | |
25cb8df9 JS |
4316 | flush_rewritten_pending(); |
4317 | if (!stat(rebase_path_rewritten_list(), &st) && | |
4318 | st.st_size > 0) { | |
4319 | struct child_process child = CHILD_PROCESS_INIT; | |
79516045 JS |
4320 | const char *post_rewrite_hook = |
4321 | find_hook("post-rewrite"); | |
25cb8df9 JS |
4322 | |
4323 | child.in = open(rebase_path_rewritten_list(), O_RDONLY); | |
4324 | child.git_cmd = 1; | |
c972bf4c JK |
4325 | strvec_push(&child.args, "notes"); |
4326 | strvec_push(&child.args, "copy"); | |
4327 | strvec_push(&child.args, "--for-rewrite=rebase"); | |
25cb8df9 JS |
4328 | /* we don't care if this copying failed */ |
4329 | run_command(&child); | |
79516045 JS |
4330 | |
4331 | if (post_rewrite_hook) { | |
4332 | struct child_process hook = CHILD_PROCESS_INIT; | |
4333 | ||
4334 | hook.in = open(rebase_path_rewritten_list(), | |
4335 | O_RDONLY); | |
4336 | hook.stdout_to_stderr = 1; | |
6206286e | 4337 | hook.trace2_hook_name = "post-rewrite"; |
c972bf4c JK |
4338 | strvec_push(&hook.args, post_rewrite_hook); |
4339 | strvec_push(&hook.args, "rebase"); | |
79516045 JS |
4340 | /* we don't care if this hook failed */ |
4341 | run_command(&hook); | |
4342 | } | |
25cb8df9 | 4343 | } |
be1bb600 | 4344 | apply_autostash(rebase_path_autostash()); |
25cb8df9 | 4345 | |
d7d90885 SG |
4346 | if (!opts->quiet) { |
4347 | if (!opts->verbose) | |
4348 | term_clear_line(); | |
899b49c4 | 4349 | fprintf(stderr, |
4d55d63b | 4350 | _("Successfully rebased and updated %s.\n"), |
899b49c4 | 4351 | head_ref.buf); |
d7d90885 | 4352 | } |
5da4966f | 4353 | |
556907f1 | 4354 | strbuf_release(&buf); |
4b83ce9f | 4355 | strbuf_release(&head_ref); |
56dc3ab0 JS |
4356 | } |
4357 | ||
043a4492 RR |
4358 | /* |
4359 | * Sequence of picks finished successfully; cleanup by | |
4360 | * removing the .git/sequencer directory | |
4361 | */ | |
2863584f | 4362 | return sequencer_remove_state(opts); |
043a4492 RR |
4363 | } |
4364 | ||
005af339 | 4365 | static int continue_single_pick(struct repository *r) |
043a4492 RR |
4366 | { |
4367 | const char *argv[] = { "commit", NULL }; | |
4368 | ||
c8e4159e | 4369 | if (!refs_ref_exists(get_main_ref_store(r), "CHERRY_PICK_HEAD") && |
b8825ef2 | 4370 | !refs_ref_exists(get_main_ref_store(r), "REVERT_HEAD")) |
043a4492 RR |
4371 | return error(_("no cherry-pick or revert in progress")); |
4372 | return run_command_v_opt(argv, RUN_GIT_CMD); | |
4373 | } | |
4374 | ||
f11c9580 NTND |
4375 | static int commit_staged_changes(struct repository *r, |
4376 | struct replay_opts *opts, | |
15ef6931 | 4377 | struct todo_list *todo_list) |
9d93ccd1 | 4378 | { |
789b3eff | 4379 | unsigned int flags = ALLOW_EMPTY | EDIT_MSG; |
15ef6931 | 4380 | unsigned int final_fixup = 0, is_clean; |
9d93ccd1 | 4381 | |
f11c9580 | 4382 | if (has_unstaged_changes(r, 1)) |
9d93ccd1 | 4383 | return error(_("cannot rebase: You have unstaged changes.")); |
52632209 | 4384 | |
f11c9580 | 4385 | is_clean = !has_uncommitted_changes(r, 0); |
9d93ccd1 JS |
4386 | |
4387 | if (file_exists(rebase_path_amend())) { | |
4388 | struct strbuf rev = STRBUF_INIT; | |
092bbcdf | 4389 | struct object_id head, to_amend; |
9d93ccd1 | 4390 | |
092bbcdf | 4391 | if (get_oid("HEAD", &head)) |
9d93ccd1 JS |
4392 | return error(_("cannot amend non-existing commit")); |
4393 | if (!read_oneliner(&rev, rebase_path_amend(), 0)) | |
4394 | return error(_("invalid file: '%s'"), rebase_path_amend()); | |
092bbcdf | 4395 | if (get_oid_hex(rev.buf, &to_amend)) |
9d93ccd1 JS |
4396 | return error(_("invalid contents: '%s'"), |
4397 | rebase_path_amend()); | |
9001dc2a | 4398 | if (!is_clean && !oideq(&head, &to_amend)) |
9d93ccd1 JS |
4399 | return error(_("\nYou have uncommitted changes in your " |
4400 | "working tree. Please, commit them\n" | |
4401 | "first and then run 'git rebase " | |
4402 | "--continue' again.")); | |
15ef6931 JS |
4403 | /* |
4404 | * When skipping a failed fixup/squash, we need to edit the | |
4405 | * commit message, the current fixup list and count, and if it | |
4406 | * was the last fixup/squash in the chain, we need to clean up | |
4407 | * the commit message and if there was a squash, let the user | |
4408 | * edit it. | |
4409 | */ | |
10d2f354 JS |
4410 | if (!is_clean || !opts->current_fixup_count) |
4411 | ; /* this is not the final fixup */ | |
87ae8a1a | 4412 | else if (!oideq(&head, &to_amend) || |
10d2f354 JS |
4413 | !file_exists(rebase_path_stopped_sha())) { |
4414 | /* was a final fixup or squash done manually? */ | |
4415 | if (!is_fixup(peek_command(todo_list, 0))) { | |
4416 | unlink(rebase_path_fixup_msg()); | |
4417 | unlink(rebase_path_squash_msg()); | |
4418 | unlink(rebase_path_current_fixups()); | |
4419 | strbuf_reset(&opts->current_fixups); | |
4420 | opts->current_fixup_count = 0; | |
4421 | } | |
4422 | } else { | |
4423 | /* we are in a fixup/squash chain */ | |
15ef6931 JS |
4424 | const char *p = opts->current_fixups.buf; |
4425 | int len = opts->current_fixups.len; | |
4426 | ||
4427 | opts->current_fixup_count--; | |
4428 | if (!len) | |
4429 | BUG("Incorrect current_fixups:\n%s", p); | |
4430 | while (len && p[len - 1] != '\n') | |
4431 | len--; | |
4432 | strbuf_setlen(&opts->current_fixups, len); | |
4433 | if (write_message(p, len, rebase_path_current_fixups(), | |
4434 | 0) < 0) | |
4435 | return error(_("could not write file: '%s'"), | |
4436 | rebase_path_current_fixups()); | |
4437 | ||
4438 | /* | |
4439 | * If a fixup/squash in a fixup/squash chain failed, the | |
4440 | * commit message is already correct, no need to commit | |
4441 | * it again. | |
4442 | * | |
4443 | * Only if it is the final command in the fixup/squash | |
4444 | * chain, and only if the chain is longer than a single | |
4445 | * fixup/squash command (which was just skipped), do we | |
4446 | * actually need to re-commit with a cleaned up commit | |
4447 | * message. | |
4448 | */ | |
4449 | if (opts->current_fixup_count > 0 && | |
4450 | !is_fixup(peek_command(todo_list, 0))) { | |
4451 | final_fixup = 1; | |
4452 | /* | |
4453 | * If there was not a single "squash" in the | |
4454 | * chain, we only need to clean up the commit | |
4455 | * message, no need to bother the user with | |
4456 | * opening the commit message in the editor. | |
4457 | */ | |
4458 | if (!starts_with(p, "squash ") && | |
4459 | !strstr(p, "\nsquash ")) | |
4460 | flags = (flags & ~EDIT_MSG) | CLEANUP_MSG; | |
4461 | } else if (is_fixup(peek_command(todo_list, 0))) { | |
4462 | /* | |
4463 | * We need to update the squash message to skip | |
4464 | * the latest commit message. | |
4465 | */ | |
4466 | struct commit *commit; | |
4467 | const char *path = rebase_path_squash_msg(); | |
b3757442 | 4468 | const char *encoding = get_commit_output_encoding(); |
15ef6931 | 4469 | |
005af339 | 4470 | if (parse_head(r, &commit) || |
b3757442 | 4471 | !(p = logmsg_reencode(commit, NULL, encoding)) || |
15ef6931 JS |
4472 | write_message(p, strlen(p), path, 0)) { |
4473 | unuse_commit_buffer(commit, p); | |
4474 | return error(_("could not write file: " | |
4475 | "'%s'"), path); | |
4476 | } | |
4477 | unuse_commit_buffer(commit, p); | |
4478 | } | |
4479 | } | |
9d93ccd1 JS |
4480 | |
4481 | strbuf_release(&rev); | |
789b3eff | 4482 | flags |= AMEND_MSG; |
9d93ccd1 JS |
4483 | } |
4484 | ||
15ef6931 | 4485 | if (is_clean) { |
c8e4159e HWN |
4486 | if (refs_ref_exists(get_main_ref_store(r), |
4487 | "CHERRY_PICK_HEAD") && | |
4488 | refs_delete_ref(get_main_ref_store(r), "", | |
4489 | "CHERRY_PICK_HEAD", NULL, 0)) | |
15ef6931 JS |
4490 | return error(_("could not remove CHERRY_PICK_HEAD")); |
4491 | if (!final_fixup) | |
4492 | return 0; | |
4493 | } | |
4494 | ||
20f4b044 | 4495 | if (run_git_commit(final_fixup ? NULL : rebase_path_message(), |
15ef6931 | 4496 | opts, flags)) |
9d93ccd1 JS |
4497 | return error(_("could not commit staged changes.")); |
4498 | unlink(rebase_path_amend()); | |
34e7771b | 4499 | unlink(git_path_merge_head(r)); |
15ef6931 JS |
4500 | if (final_fixup) { |
4501 | unlink(rebase_path_fixup_msg()); | |
4502 | unlink(rebase_path_squash_msg()); | |
4503 | } | |
4504 | if (opts->current_fixup_count > 0) { | |
4505 | /* | |
4506 | * Whether final fixup or not, we just cleaned up the commit | |
4507 | * message... | |
4508 | */ | |
4509 | unlink(rebase_path_current_fixups()); | |
4510 | strbuf_reset(&opts->current_fixups); | |
4511 | opts->current_fixup_count = 0; | |
4512 | } | |
9d93ccd1 JS |
4513 | return 0; |
4514 | } | |
4515 | ||
f11c9580 | 4516 | int sequencer_continue(struct repository *r, struct replay_opts *opts) |
043a4492 | 4517 | { |
004fefa7 JS |
4518 | struct todo_list todo_list = TODO_LIST_INIT; |
4519 | int res; | |
043a4492 | 4520 | |
f11c9580 | 4521 | if (read_and_refresh_cache(r, opts)) |
2863584f JS |
4522 | return -1; |
4523 | ||
15ef6931 JS |
4524 | if (read_populate_opts(opts)) |
4525 | return -1; | |
9d93ccd1 | 4526 | if (is_rebase_i(opts)) { |
005af339 | 4527 | if ((res = read_populate_todo(r, &todo_list, opts))) |
15ef6931 | 4528 | goto release_todo_list; |
5a5445d8 AG |
4529 | |
4530 | if (file_exists(rebase_path_dropped())) { | |
4531 | if ((res = todo_list_check_against_backup(r, &todo_list))) | |
4532 | goto release_todo_list; | |
4533 | ||
4534 | unlink(rebase_path_dropped()); | |
4535 | } | |
4536 | ||
f6b9413b AG |
4537 | if (commit_staged_changes(r, opts, &todo_list)) { |
4538 | res = -1; | |
4539 | goto release_todo_list; | |
4540 | } | |
4258a6da | 4541 | } else if (!file_exists(get_todo_path(opts))) |
005af339 NTND |
4542 | return continue_single_pick(r); |
4543 | else if ((res = read_populate_todo(r, &todo_list, opts))) | |
004fefa7 | 4544 | goto release_todo_list; |
043a4492 | 4545 | |
4258a6da JS |
4546 | if (!is_rebase_i(opts)) { |
4547 | /* Verify that the conflict has been resolved */ | |
c8e4159e HWN |
4548 | if (refs_ref_exists(get_main_ref_store(r), |
4549 | "CHERRY_PICK_HEAD") || | |
b8825ef2 | 4550 | refs_ref_exists(get_main_ref_store(r), "REVERT_HEAD")) { |
005af339 | 4551 | res = continue_single_pick(r); |
4258a6da JS |
4552 | if (res) |
4553 | goto release_todo_list; | |
4554 | } | |
ffc00a48 | 4555 | if (index_differs_from(r, "HEAD", NULL, 0)) { |
e1ff0a32 | 4556 | res = error_dirty_index(r, opts); |
004fefa7 | 4557 | goto release_todo_list; |
4258a6da JS |
4558 | } |
4559 | todo_list.current++; | |
ca98c6d4 JS |
4560 | } else if (file_exists(rebase_path_stopped_sha())) { |
4561 | struct strbuf buf = STRBUF_INIT; | |
4562 | struct object_id oid; | |
4563 | ||
3442c3d1 DL |
4564 | if (read_oneliner(&buf, rebase_path_stopped_sha(), |
4565 | READ_ONELINER_SKIP_IF_EMPTY) && | |
0512eabd | 4566 | !get_oid_hex(buf.buf, &oid)) |
ca98c6d4 JS |
4567 | record_in_rewritten(&oid, peek_command(&todo_list, 0)); |
4568 | strbuf_release(&buf); | |
043a4492 | 4569 | } |
4258a6da | 4570 | |
f11c9580 | 4571 | res = pick_commits(r, &todo_list, opts); |
004fefa7 JS |
4572 | release_todo_list: |
4573 | todo_list_release(&todo_list); | |
4574 | return res; | |
043a4492 RR |
4575 | } |
4576 | ||
f11c9580 NTND |
4577 | static int single_pick(struct repository *r, |
4578 | struct commit *cmit, | |
4579 | struct replay_opts *opts) | |
043a4492 | 4580 | { |
a47ba3c7 PW |
4581 | int check_todo; |
4582 | ||
043a4492 | 4583 | setenv(GIT_REFLOG_ACTION, action_name(opts), 0); |
f11c9580 | 4584 | return do_pick_commit(r, opts->action == REPLAY_PICK ? |
a47ba3c7 PW |
4585 | TODO_PICK : TODO_REVERT, cmit, opts, 0, |
4586 | &check_todo); | |
043a4492 RR |
4587 | } |
4588 | ||
f11c9580 NTND |
4589 | int sequencer_pick_revisions(struct repository *r, |
4590 | struct replay_opts *opts) | |
043a4492 | 4591 | { |
004fefa7 | 4592 | struct todo_list todo_list = TODO_LIST_INIT; |
1e43ed98 | 4593 | struct object_id oid; |
004fefa7 | 4594 | int i, res; |
043a4492 | 4595 | |
2863584f | 4596 | assert(opts->revs); |
f11c9580 | 4597 | if (read_and_refresh_cache(r, opts)) |
0d9c6dc9 | 4598 | return -1; |
043a4492 | 4599 | |
21246dbb | 4600 | for (i = 0; i < opts->revs->pending.nr; i++) { |
1e43ed98 | 4601 | struct object_id oid; |
21246dbb MV |
4602 | const char *name = opts->revs->pending.objects[i].name; |
4603 | ||
4604 | /* This happens when using --stdin. */ | |
4605 | if (!strlen(name)) | |
4606 | continue; | |
4607 | ||
1e43ed98 | 4608 | if (!get_oid(name, &oid)) { |
f11c9580 NTND |
4609 | if (!lookup_commit_reference_gently(r, &oid, 1)) { |
4610 | enum object_type type = oid_object_info(r, | |
0df8e965 | 4611 | &oid, |
abef9020 | 4612 | NULL); |
b9b946d4 | 4613 | return error(_("%s: can't cherry-pick a %s"), |
debca9d2 | 4614 | name, type_name(type)); |
7c0b0d8d | 4615 | } |
21246dbb | 4616 | } else |
b9b946d4 | 4617 | return error(_("%s: bad revision"), name); |
21246dbb MV |
4618 | } |
4619 | ||
043a4492 RR |
4620 | /* |
4621 | * If we were called as "git cherry-pick <commit>", just | |
4622 | * cherry-pick/revert it, set CHERRY_PICK_HEAD / | |
4623 | * REVERT_HEAD, and don't touch the sequencer state. | |
4624 | * This means it is possible to cherry-pick in the middle | |
4625 | * of a cherry-pick sequence. | |
4626 | */ | |
4627 | if (opts->revs->cmdline.nr == 1 && | |
4628 | opts->revs->cmdline.rev->whence == REV_CMD_REV && | |
4629 | opts->revs->no_walk && | |
4630 | !opts->revs->cmdline.rev->flags) { | |
4631 | struct commit *cmit; | |
4632 | if (prepare_revision_walk(opts->revs)) | |
b9b946d4 | 4633 | return error(_("revision walk setup failed")); |
043a4492 | 4634 | cmit = get_revision(opts->revs); |
c5e358d0 JK |
4635 | if (!cmit) |
4636 | return error(_("empty commit set passed")); | |
4637 | if (get_revision(opts->revs)) | |
4638 | BUG("unexpected extra commit from walk"); | |
f11c9580 | 4639 | return single_pick(r, cmit, opts); |
043a4492 RR |
4640 | } |
4641 | ||
4642 | /* | |
4643 | * Start a new cherry-pick/ revert sequence; but | |
4644 | * first, make sure that an existing one isn't in | |
4645 | * progress | |
4646 | */ | |
4647 | ||
34b0528b | 4648 | if (walk_revs_populate_todo(&todo_list, opts) || |
6a1f9046 | 4649 | create_seq_dir(r) < 0) |
043a4492 | 4650 | return -1; |
1e43ed98 | 4651 | if (get_oid("HEAD", &oid) && (opts->action == REPLAY_REVERT)) |
93b3df6f | 4652 | return error(_("can't revert as initial commit")); |
1e43ed98 | 4653 | if (save_head(oid_to_hex(&oid))) |
311fd397 | 4654 | return -1; |
88d5a271 JS |
4655 | if (save_opts(opts)) |
4656 | return -1; | |
1e41229d | 4657 | update_abort_safety_file(); |
f11c9580 | 4658 | res = pick_commits(r, &todo_list, opts); |
004fefa7 JS |
4659 | todo_list_release(&todo_list); |
4660 | return res; | |
043a4492 | 4661 | } |
5ed75e2a | 4662 | |
66e83d9b | 4663 | void append_signoff(struct strbuf *msgbuf, size_t ignore_footer, unsigned flag) |
5ed75e2a | 4664 | { |
bab4d109 | 4665 | unsigned no_dup_sob = flag & APPEND_SIGNOFF_DEDUP; |
5ed75e2a | 4666 | struct strbuf sob = STRBUF_INIT; |
bab4d109 | 4667 | int has_footer; |
5ed75e2a MV |
4668 | |
4669 | strbuf_addstr(&sob, sign_off_header); | |
39ab4d09 | 4670 | strbuf_addstr(&sob, fmt_name(WANT_COMMITTER_IDENT)); |
5ed75e2a | 4671 | strbuf_addch(&sob, '\n'); |
bab4d109 | 4672 | |
44dc738a JT |
4673 | if (!ignore_footer) |
4674 | strbuf_complete_line(msgbuf); | |
4675 | ||
bab4d109 BC |
4676 | /* |
4677 | * If the whole message buffer is equal to the sob, pretend that we | |
4678 | * found a conforming footer with a matching sob | |
4679 | */ | |
4680 | if (msgbuf->len - ignore_footer == sob.len && | |
4681 | !strncmp(msgbuf->buf, sob.buf, sob.len)) | |
4682 | has_footer = 3; | |
4683 | else | |
4684 | has_footer = has_conforming_footer(msgbuf, &sob, ignore_footer); | |
4685 | ||
33f2f9ab BC |
4686 | if (!has_footer) { |
4687 | const char *append_newlines = NULL; | |
4688 | size_t len = msgbuf->len - ignore_footer; | |
4689 | ||
8c613fd5 BC |
4690 | if (!len) { |
4691 | /* | |
4692 | * The buffer is completely empty. Leave foom for | |
4693 | * the title and body to be filled in by the user. | |
4694 | */ | |
33f2f9ab | 4695 | append_newlines = "\n\n"; |
8c613fd5 BC |
4696 | } else if (len == 1) { |
4697 | /* | |
4698 | * Buffer contains a single newline. Add another | |
4699 | * so that we leave room for the title and body. | |
4700 | */ | |
4701 | append_newlines = "\n"; | |
4702 | } else if (msgbuf->buf[len - 2] != '\n') { | |
4703 | /* | |
4704 | * Buffer ends with a single newline. Add another | |
4705 | * so that there is an empty line between the message | |
4706 | * body and the sob. | |
4707 | */ | |
33f2f9ab | 4708 | append_newlines = "\n"; |
8c613fd5 | 4709 | } /* else, the buffer already ends with two newlines. */ |
33f2f9ab BC |
4710 | |
4711 | if (append_newlines) | |
4712 | strbuf_splice(msgbuf, msgbuf->len - ignore_footer, 0, | |
4713 | append_newlines, strlen(append_newlines)); | |
5ed75e2a | 4714 | } |
bab4d109 BC |
4715 | |
4716 | if (has_footer != 3 && (!no_dup_sob || has_footer != 2)) | |
4717 | strbuf_splice(msgbuf, msgbuf->len - ignore_footer, 0, | |
4718 | sob.buf, sob.len); | |
4719 | ||
5ed75e2a MV |
4720 | strbuf_release(&sob); |
4721 | } | |
62db5247 | 4722 | |
1644c73c JS |
4723 | struct labels_entry { |
4724 | struct hashmap_entry entry; | |
4725 | char label[FLEX_ARRAY]; | |
4726 | }; | |
4727 | ||
939af16e EW |
4728 | static int labels_cmp(const void *fndata, const struct hashmap_entry *eptr, |
4729 | const struct hashmap_entry *entry_or_key, const void *key) | |
1644c73c | 4730 | { |
939af16e EW |
4731 | const struct labels_entry *a, *b; |
4732 | ||
4733 | a = container_of(eptr, const struct labels_entry, entry); | |
4734 | b = container_of(entry_or_key, const struct labels_entry, entry); | |
4735 | ||
1644c73c JS |
4736 | return key ? strcmp(a->label, key) : strcmp(a->label, b->label); |
4737 | } | |
4738 | ||
4739 | struct string_entry { | |
4740 | struct oidmap_entry entry; | |
4741 | char string[FLEX_ARRAY]; | |
4742 | }; | |
4743 | ||
4744 | struct label_state { | |
4745 | struct oidmap commit2label; | |
4746 | struct hashmap labels; | |
4747 | struct strbuf buf; | |
4748 | }; | |
4749 | ||
4750 | static const char *label_oid(struct object_id *oid, const char *label, | |
4751 | struct label_state *state) | |
4752 | { | |
4753 | struct labels_entry *labels_entry; | |
4754 | struct string_entry *string_entry; | |
4755 | struct object_id dummy; | |
1644c73c JS |
4756 | int i; |
4757 | ||
4758 | string_entry = oidmap_get(&state->commit2label, oid); | |
4759 | if (string_entry) | |
4760 | return string_entry->string; | |
4761 | ||
4762 | /* | |
4763 | * For "uninteresting" commits, i.e. commits that are not to be | |
4764 | * rebased, and which can therefore not be labeled, we use a unique | |
4765 | * abbreviation of the commit name. This is slightly more complicated | |
4766 | * than calling find_unique_abbrev() because we also need to make | |
4767 | * sure that the abbreviation does not conflict with any other | |
4768 | * label. | |
4769 | * | |
4770 | * We disallow "interesting" commits to be labeled by a string that | |
4771 | * is a valid full-length hash, to ensure that we always can find an | |
4772 | * abbreviation for any uninteresting commit's names that does not | |
4773 | * clash with any other label. | |
4774 | */ | |
867bc1d2 | 4775 | strbuf_reset(&state->buf); |
1644c73c JS |
4776 | if (!label) { |
4777 | char *p; | |
4778 | ||
4439c7a3 | 4779 | strbuf_grow(&state->buf, GIT_MAX_HEXSZ); |
1644c73c JS |
4780 | label = p = state->buf.buf; |
4781 | ||
4782 | find_unique_abbrev_r(p, oid, default_abbrev); | |
4783 | ||
4784 | /* | |
4785 | * We may need to extend the abbreviated hash so that there is | |
4786 | * no conflicting label. | |
4787 | */ | |
4788 | if (hashmap_get_from_hash(&state->labels, strihash(p), p)) { | |
4789 | size_t i = strlen(p) + 1; | |
4790 | ||
4791 | oid_to_hex_r(p, oid); | |
4439c7a3 | 4792 | for (; i < the_hash_algo->hexsz; i++) { |
1644c73c JS |
4793 | char save = p[i]; |
4794 | p[i] = '\0'; | |
4795 | if (!hashmap_get_from_hash(&state->labels, | |
4796 | strihash(p), p)) | |
4797 | break; | |
4798 | p[i] = save; | |
4799 | } | |
4800 | } | |
867bc1d2 | 4801 | } else { |
1644c73c JS |
4802 | struct strbuf *buf = &state->buf; |
4803 | ||
1644c73c | 4804 | /* |
cd552227 MR |
4805 | * Sanitize labels by replacing non-alpha-numeric characters |
4806 | * (including white-space ones) by dashes, as they might be | |
4807 | * illegal in file names (and hence in ref names). | |
4808 | * | |
4809 | * Note that we retain non-ASCII UTF-8 characters (identified | |
4810 | * via the most significant bit). They should be all acceptable | |
4811 | * in file names. We do not validate the UTF-8 here, that's not | |
4812 | * the job of this function. | |
1644c73c | 4813 | */ |
867bc1d2 | 4814 | for (; *label; label++) |
cd552227 MR |
4815 | if ((*label & 0x80) || isalnum(*label)) |
4816 | strbuf_addch(buf, *label); | |
4817 | /* avoid leading dash and double-dashes */ | |
4818 | else if (buf->len && buf->buf[buf->len - 1] != '-') | |
4819 | strbuf_addch(buf, '-'); | |
4820 | if (!buf->len) { | |
4821 | strbuf_addstr(buf, "rev-"); | |
4822 | strbuf_add_unique_abbrev(buf, oid, default_abbrev); | |
4823 | } | |
867bc1d2 | 4824 | label = buf->buf; |
1644c73c | 4825 | |
867bc1d2 JS |
4826 | if ((buf->len == the_hash_algo->hexsz && |
4827 | !get_oid_hex(label, &dummy)) || | |
4828 | (buf->len == 1 && *label == '#') || | |
4829 | hashmap_get_from_hash(&state->labels, | |
4830 | strihash(label), label)) { | |
4831 | /* | |
4832 | * If the label already exists, or if the label is a | |
4833 | * valid full OID, or the label is a '#' (which we use | |
4834 | * as a separator between merge heads and oneline), we | |
4835 | * append a dash and a number to make it unique. | |
4836 | */ | |
4837 | size_t len = buf->len; | |
1644c73c | 4838 | |
867bc1d2 JS |
4839 | for (i = 2; ; i++) { |
4840 | strbuf_setlen(buf, len); | |
4841 | strbuf_addf(buf, "-%d", i); | |
4842 | if (!hashmap_get_from_hash(&state->labels, | |
4843 | strihash(buf->buf), | |
4844 | buf->buf)) | |
4845 | break; | |
4846 | } | |
1644c73c | 4847 | |
867bc1d2 JS |
4848 | label = buf->buf; |
4849 | } | |
1644c73c JS |
4850 | } |
4851 | ||
4852 | FLEX_ALLOC_STR(labels_entry, label, label); | |
d22245a2 | 4853 | hashmap_entry_init(&labels_entry->entry, strihash(label)); |
b94e5c1d | 4854 | hashmap_add(&state->labels, &labels_entry->entry); |
1644c73c JS |
4855 | |
4856 | FLEX_ALLOC_STR(string_entry, string, label); | |
4857 | oidcpy(&string_entry->entry.oid, oid); | |
4858 | oidmap_put(&state->commit2label, string_entry); | |
4859 | ||
4860 | return string_entry->string; | |
4861 | } | |
4862 | ||
4863 | static int make_script_with_merges(struct pretty_print_context *pp, | |
d358fc28 | 4864 | struct rev_info *revs, struct strbuf *out, |
1644c73c JS |
4865 | unsigned flags) |
4866 | { | |
b9cbd295 | 4867 | int keep_empty = flags & TODO_LIST_KEEP_EMPTY; |
7543f6f4 | 4868 | int rebase_cousins = flags & TODO_LIST_REBASE_COUSINS; |
e1fac531 | 4869 | int root_with_onto = flags & TODO_LIST_ROOT_WITH_ONTO; |
1644c73c JS |
4870 | struct strbuf buf = STRBUF_INIT, oneline = STRBUF_INIT; |
4871 | struct strbuf label = STRBUF_INIT; | |
4872 | struct commit_list *commits = NULL, **tail = &commits, *iter; | |
4873 | struct commit_list *tips = NULL, **tips_tail = &tips; | |
4874 | struct commit *commit; | |
4875 | struct oidmap commit2todo = OIDMAP_INIT; | |
4876 | struct string_entry *entry; | |
4877 | struct oidset interesting = OIDSET_INIT, child_seen = OIDSET_INIT, | |
4878 | shown = OIDSET_INIT; | |
4879 | struct label_state state = { OIDMAP_INIT, { NULL }, STRBUF_INIT }; | |
4880 | ||
4881 | int abbr = flags & TODO_LIST_ABBREVIATE_CMDS; | |
4882 | const char *cmd_pick = abbr ? "p" : "pick", | |
4883 | *cmd_label = abbr ? "l" : "label", | |
4884 | *cmd_reset = abbr ? "t" : "reset", | |
4885 | *cmd_merge = abbr ? "m" : "merge"; | |
4886 | ||
4887 | oidmap_init(&commit2todo, 0); | |
4888 | oidmap_init(&state.commit2label, 0); | |
939af16e | 4889 | hashmap_init(&state.labels, labels_cmp, NULL, 0); |
1644c73c JS |
4890 | strbuf_init(&state.buf, 32); |
4891 | ||
4892 | if (revs->cmdline.nr && (revs->cmdline.rev[0].flags & BOTTOM)) { | |
e02058a7 | 4893 | struct labels_entry *onto_label_entry; |
1644c73c JS |
4894 | struct object_id *oid = &revs->cmdline.rev[0].item->oid; |
4895 | FLEX_ALLOC_STR(entry, string, "onto"); | |
4896 | oidcpy(&entry->entry.oid, oid); | |
4897 | oidmap_put(&state.commit2label, entry); | |
e02058a7 ĐTCD |
4898 | |
4899 | FLEX_ALLOC_STR(onto_label_entry, label, "onto"); | |
4900 | hashmap_entry_init(&onto_label_entry->entry, strihash("onto")); | |
4901 | hashmap_add(&state.labels, &onto_label_entry->entry); | |
1644c73c JS |
4902 | } |
4903 | ||
4904 | /* | |
4905 | * First phase: | |
4906 | * - get onelines for all commits | |
4907 | * - gather all branch tips (i.e. 2nd or later parents of merges) | |
4908 | * - label all branch tips | |
4909 | */ | |
4910 | while ((commit = get_revision(revs))) { | |
4911 | struct commit_list *to_merge; | |
1644c73c JS |
4912 | const char *p1, *p2; |
4913 | struct object_id *oid; | |
4914 | int is_empty; | |
4915 | ||
4916 | tail = &commit_list_insert(commit, tail)->next; | |
4917 | oidset_insert(&interesting, &commit->object.oid); | |
4918 | ||
4919 | is_empty = is_original_commit_empty(commit); | |
4920 | if (!is_empty && (commit->object.flags & PATCHSAME)) | |
4921 | continue; | |
b9cbd295 EN |
4922 | if (is_empty && !keep_empty) |
4923 | continue; | |
1644c73c JS |
4924 | |
4925 | strbuf_reset(&oneline); | |
4926 | pretty_print_commit(pp, commit, &oneline); | |
4927 | ||
4928 | to_merge = commit->parents ? commit->parents->next : NULL; | |
4929 | if (!to_merge) { | |
4930 | /* non-merge commit: easy case */ | |
4931 | strbuf_reset(&buf); | |
1644c73c JS |
4932 | strbuf_addf(&buf, "%s %s %s", cmd_pick, |
4933 | oid_to_hex(&commit->object.oid), | |
4934 | oneline.buf); | |
1b5735f7 EN |
4935 | if (is_empty) |
4936 | strbuf_addf(&buf, " %c empty", | |
4937 | comment_line_char); | |
1644c73c JS |
4938 | |
4939 | FLEX_ALLOC_STR(entry, string, buf.buf); | |
4940 | oidcpy(&entry->entry.oid, &commit->object.oid); | |
4941 | oidmap_put(&commit2todo, entry); | |
4942 | ||
4943 | continue; | |
4944 | } | |
4945 | ||
1644c73c JS |
4946 | /* Create a label */ |
4947 | strbuf_reset(&label); | |
4948 | if (skip_prefix(oneline.buf, "Merge ", &p1) && | |
4949 | (p1 = strchr(p1, '\'')) && | |
4950 | (p2 = strchr(++p1, '\''))) | |
4951 | strbuf_add(&label, p1, p2 - p1); | |
4952 | else if (skip_prefix(oneline.buf, "Merge pull request ", | |
4953 | &p1) && | |
4954 | (p1 = strstr(p1, " from "))) | |
4955 | strbuf_addstr(&label, p1 + strlen(" from ")); | |
4956 | else | |
4957 | strbuf_addbuf(&label, &oneline); | |
4958 | ||
1644c73c JS |
4959 | strbuf_reset(&buf); |
4960 | strbuf_addf(&buf, "%s -C %s", | |
4961 | cmd_merge, oid_to_hex(&commit->object.oid)); | |
4962 | ||
2b6ad0f4 JS |
4963 | /* label the tips of merged branches */ |
4964 | for (; to_merge; to_merge = to_merge->next) { | |
4965 | oid = &to_merge->item->object.oid; | |
4966 | strbuf_addch(&buf, ' '); | |
4967 | ||
4968 | if (!oidset_contains(&interesting, oid)) { | |
4969 | strbuf_addstr(&buf, label_oid(oid, NULL, | |
4970 | &state)); | |
4971 | continue; | |
4972 | } | |
1644c73c | 4973 | |
1644c73c JS |
4974 | tips_tail = &commit_list_insert(to_merge->item, |
4975 | tips_tail)->next; | |
4976 | ||
4977 | strbuf_addstr(&buf, label_oid(oid, label.buf, &state)); | |
4978 | } | |
4979 | strbuf_addf(&buf, " # %s", oneline.buf); | |
4980 | ||
4981 | FLEX_ALLOC_STR(entry, string, buf.buf); | |
4982 | oidcpy(&entry->entry.oid, &commit->object.oid); | |
4983 | oidmap_put(&commit2todo, entry); | |
4984 | } | |
4985 | ||
4986 | /* | |
4987 | * Second phase: | |
4988 | * - label branch points | |
4989 | * - add HEAD to the branch tips | |
4990 | */ | |
4991 | for (iter = commits; iter; iter = iter->next) { | |
4992 | struct commit_list *parent = iter->item->parents; | |
4993 | for (; parent; parent = parent->next) { | |
4994 | struct object_id *oid = &parent->item->object.oid; | |
4995 | if (!oidset_contains(&interesting, oid)) | |
4996 | continue; | |
6e8fc70f | 4997 | if (oidset_insert(&child_seen, oid)) |
1644c73c JS |
4998 | label_oid(oid, "branch-point", &state); |
4999 | } | |
5000 | ||
15beaaa3 | 5001 | /* Add HEAD as implicit "tip of branch" */ |
1644c73c JS |
5002 | if (!iter->next) |
5003 | tips_tail = &commit_list_insert(iter->item, | |
5004 | tips_tail)->next; | |
5005 | } | |
5006 | ||
5007 | /* | |
5008 | * Third phase: output the todo list. This is a bit tricky, as we | |
5009 | * want to avoid jumping back and forth between revisions. To | |
5010 | * accomplish that goal, we walk backwards from the branch tips, | |
5011 | * gathering commits not yet shown, reversing the list on the fly, | |
5012 | * then outputting that list (labeling revisions as needed). | |
5013 | */ | |
d358fc28 | 5014 | strbuf_addf(out, "%s onto\n", cmd_label); |
1644c73c JS |
5015 | for (iter = tips; iter; iter = iter->next) { |
5016 | struct commit_list *list = NULL, *iter2; | |
5017 | ||
5018 | commit = iter->item; | |
5019 | if (oidset_contains(&shown, &commit->object.oid)) | |
5020 | continue; | |
5021 | entry = oidmap_get(&state.commit2label, &commit->object.oid); | |
5022 | ||
5023 | if (entry) | |
d358fc28 | 5024 | strbuf_addf(out, "\n%c Branch %s\n", comment_line_char, entry->string); |
1644c73c | 5025 | else |
d358fc28 | 5026 | strbuf_addch(out, '\n'); |
1644c73c JS |
5027 | |
5028 | while (oidset_contains(&interesting, &commit->object.oid) && | |
5029 | !oidset_contains(&shown, &commit->object.oid)) { | |
5030 | commit_list_insert(commit, &list); | |
5031 | if (!commit->parents) { | |
5032 | commit = NULL; | |
5033 | break; | |
5034 | } | |
5035 | commit = commit->parents->item; | |
5036 | } | |
5037 | ||
5038 | if (!commit) | |
d358fc28 | 5039 | strbuf_addf(out, "%s %s\n", cmd_reset, |
e1fac531 JS |
5040 | rebase_cousins || root_with_onto ? |
5041 | "onto" : "[new root]"); | |
1644c73c JS |
5042 | else { |
5043 | const char *to = NULL; | |
5044 | ||
5045 | entry = oidmap_get(&state.commit2label, | |
5046 | &commit->object.oid); | |
5047 | if (entry) | |
5048 | to = entry->string; | |
7543f6f4 JS |
5049 | else if (!rebase_cousins) |
5050 | to = label_oid(&commit->object.oid, NULL, | |
5051 | &state); | |
1644c73c JS |
5052 | |
5053 | if (!to || !strcmp(to, "onto")) | |
d358fc28 | 5054 | strbuf_addf(out, "%s onto\n", cmd_reset); |
1644c73c JS |
5055 | else { |
5056 | strbuf_reset(&oneline); | |
5057 | pretty_print_commit(pp, commit, &oneline); | |
d358fc28 AG |
5058 | strbuf_addf(out, "%s %s # %s\n", |
5059 | cmd_reset, to, oneline.buf); | |
1644c73c JS |
5060 | } |
5061 | } | |
5062 | ||
5063 | for (iter2 = list; iter2; iter2 = iter2->next) { | |
5064 | struct object_id *oid = &iter2->item->object.oid; | |
5065 | entry = oidmap_get(&commit2todo, oid); | |
5066 | /* only show if not already upstream */ | |
5067 | if (entry) | |
d358fc28 | 5068 | strbuf_addf(out, "%s\n", entry->string); |
1644c73c JS |
5069 | entry = oidmap_get(&state.commit2label, oid); |
5070 | if (entry) | |
d358fc28 AG |
5071 | strbuf_addf(out, "%s %s\n", |
5072 | cmd_label, entry->string); | |
1644c73c JS |
5073 | oidset_insert(&shown, oid); |
5074 | } | |
5075 | ||
5076 | free_commit_list(list); | |
5077 | } | |
5078 | ||
5079 | free_commit_list(commits); | |
5080 | free_commit_list(tips); | |
5081 | ||
5082 | strbuf_release(&label); | |
5083 | strbuf_release(&oneline); | |
5084 | strbuf_release(&buf); | |
5085 | ||
5086 | oidmap_free(&commit2todo, 1); | |
5087 | oidmap_free(&state.commit2label, 1); | |
6da1a258 | 5088 | hashmap_clear_and_free(&state.labels, struct labels_entry, entry); |
1644c73c JS |
5089 | strbuf_release(&state.buf); |
5090 | ||
5091 | return 0; | |
5092 | } | |
5093 | ||
d358fc28 AG |
5094 | int sequencer_make_script(struct repository *r, struct strbuf *out, int argc, |
5095 | const char **argv, unsigned flags) | |
62db5247 JS |
5096 | { |
5097 | char *format = NULL; | |
5098 | struct pretty_print_context pp = {0}; | |
62db5247 JS |
5099 | struct rev_info revs; |
5100 | struct commit *commit; | |
b9cbd295 | 5101 | int keep_empty = flags & TODO_LIST_KEEP_EMPTY; |
d8ae6c84 | 5102 | const char *insn = flags & TODO_LIST_ABBREVIATE_CMDS ? "p" : "pick"; |
1644c73c | 5103 | int rebase_merges = flags & TODO_LIST_REBASE_MERGES; |
0fcb4f6b | 5104 | int reapply_cherry_picks = flags & TODO_LIST_REAPPLY_CHERRY_PICKS; |
62db5247 | 5105 | |
f11c9580 | 5106 | repo_init_revisions(r, &revs, NULL); |
62db5247 | 5107 | revs.verbose_header = 1; |
1644c73c JS |
5108 | if (!rebase_merges) |
5109 | revs.max_parents = 1; | |
0fcb4f6b | 5110 | revs.cherry_mark = !reapply_cherry_picks; |
62db5247 JS |
5111 | revs.limited = 1; |
5112 | revs.reverse = 1; | |
5113 | revs.right_only = 1; | |
5114 | revs.sort_order = REV_SORT_IN_GRAPH_ORDER; | |
5115 | revs.topo_order = 1; | |
5116 | ||
5117 | revs.pretty_given = 1; | |
5118 | git_config_get_string("rebase.instructionFormat", &format); | |
5119 | if (!format || !*format) { | |
5120 | free(format); | |
5121 | format = xstrdup("%s"); | |
5122 | } | |
5123 | get_commit_format(format, &revs); | |
5124 | free(format); | |
5125 | pp.fmt = revs.commit_format; | |
5126 | pp.output_encoding = get_log_output_encoding(); | |
5127 | ||
5128 | if (setup_revisions(argc, argv, &revs, NULL) > 1) | |
5129 | return error(_("make_script: unhandled options")); | |
5130 | ||
5131 | if (prepare_revision_walk(&revs) < 0) | |
5132 | return error(_("make_script: error preparing revisions")); | |
5133 | ||
1644c73c JS |
5134 | if (rebase_merges) |
5135 | return make_script_with_merges(&pp, &revs, out, flags); | |
5136 | ||
62db5247 | 5137 | while ((commit = get_revision(&revs))) { |
d48e5e21 | 5138 | int is_empty = is_original_commit_empty(commit); |
76ea2358 PW |
5139 | |
5140 | if (!is_empty && (commit->object.flags & PATCHSAME)) | |
5141 | continue; | |
b9cbd295 EN |
5142 | if (is_empty && !keep_empty) |
5143 | continue; | |
d358fc28 | 5144 | strbuf_addf(out, "%s %s ", insn, |
d8ae6c84 | 5145 | oid_to_hex(&commit->object.oid)); |
d358fc28 | 5146 | pretty_print_commit(&pp, commit, out); |
1b5735f7 EN |
5147 | if (is_empty) |
5148 | strbuf_addf(out, " %c empty", comment_line_char); | |
d358fc28 | 5149 | strbuf_addch(out, '\n'); |
62db5247 | 5150 | } |
62db5247 JS |
5151 | return 0; |
5152 | } | |
3546c8d9 | 5153 | |
0cce4a27 LB |
5154 | /* |
5155 | * Add commands after pick and (series of) squash/fixup commands | |
5156 | * in the todo list. | |
5157 | */ | |
1ba204de AG |
5158 | void todo_list_add_exec_commands(struct todo_list *todo_list, |
5159 | struct string_list *commands) | |
3546c8d9 | 5160 | { |
683153a4 AG |
5161 | struct strbuf *buf = &todo_list->buf; |
5162 | size_t base_offset = buf->len; | |
5163 | int i, insert, nr = 0, alloc = 0; | |
5164 | struct todo_item *items = NULL, *base_items = NULL; | |
3546c8d9 | 5165 | |
683153a4 AG |
5166 | base_items = xcalloc(commands->nr, sizeof(struct todo_item)); |
5167 | for (i = 0; i < commands->nr; i++) { | |
5168 | size_t command_len = strlen(commands->items[i].string); | |
3546c8d9 | 5169 | |
683153a4 AG |
5170 | strbuf_addstr(buf, commands->items[i].string); |
5171 | strbuf_addch(buf, '\n'); | |
3546c8d9 | 5172 | |
683153a4 AG |
5173 | base_items[i].command = TODO_EXEC; |
5174 | base_items[i].offset_in_buf = base_offset; | |
5175 | base_items[i].arg_offset = base_offset + strlen("exec "); | |
5176 | base_items[i].arg_len = command_len - strlen("exec "); | |
3546c8d9 | 5177 | |
683153a4 | 5178 | base_offset += command_len + 1; |
3546c8d9 JS |
5179 | } |
5180 | ||
1ace63bc JS |
5181 | /* |
5182 | * Insert <commands> after every pick. Here, fixup/squash chains | |
5183 | * are considered part of the pick, so we insert the commands *after* | |
5184 | * those chains if there are any. | |
683153a4 | 5185 | * |
15beaaa3 | 5186 | * As we insert the exec commands immediately after rearranging |
683153a4 AG |
5187 | * any fixups and before the user edits the list, a fixup chain |
5188 | * can never contain comments (any comments are empty picks that | |
5189 | * have been commented out because the user did not specify | |
5190 | * --keep-empty). So, it is safe to insert an exec command | |
5191 | * without looking at the command following a comment. | |
1ace63bc | 5192 | */ |
683153a4 AG |
5193 | insert = 0; |
5194 | for (i = 0; i < todo_list->nr; i++) { | |
5195 | enum todo_command command = todo_list->items[i].command; | |
5196 | if (insert && !is_fixup(command)) { | |
5197 | ALLOC_GROW(items, nr + commands->nr, alloc); | |
5198 | COPY_ARRAY(items + nr, base_items, commands->nr); | |
5199 | nr += commands->nr; | |
5200 | ||
5201 | insert = 0; | |
0cce4a27 | 5202 | } |
1ace63bc | 5203 | |
683153a4 AG |
5204 | ALLOC_GROW(items, nr + 1, alloc); |
5205 | items[nr++] = todo_list->items[i]; | |
5206 | ||
1ace63bc | 5207 | if (command == TODO_PICK || command == TODO_MERGE) |
683153a4 | 5208 | insert = 1; |
0cce4a27 LB |
5209 | } |
5210 | ||
1ace63bc | 5211 | /* insert or append final <commands> */ |
683153a4 AG |
5212 | if (insert || nr == todo_list->nr) { |
5213 | ALLOC_GROW(items, nr + commands->nr, alloc); | |
5214 | COPY_ARRAY(items + nr, base_items, commands->nr); | |
5215 | nr += commands->nr; | |
5216 | } | |
0cce4a27 | 5217 | |
683153a4 AG |
5218 | free(base_items); |
5219 | FREE_AND_NULL(todo_list->items); | |
5220 | todo_list->items = items; | |
5221 | todo_list->nr = nr; | |
5222 | todo_list->alloc = alloc; | |
0cce4a27 | 5223 | } |
3546c8d9 | 5224 | |
616d7740 AG |
5225 | static void todo_list_to_strbuf(struct repository *r, struct todo_list *todo_list, |
5226 | struct strbuf *buf, int num, unsigned flags) | |
3546c8d9 | 5227 | { |
8dccc7a6 | 5228 | struct todo_item *item; |
616d7740 | 5229 | int i, max = todo_list->nr; |
3546c8d9 | 5230 | |
616d7740 AG |
5231 | if (num > 0 && num < max) |
5232 | max = num; | |
3546c8d9 | 5233 | |
616d7740 | 5234 | for (item = todo_list->items, i = 0; i < max; i++, item++) { |
68e7090f AG |
5235 | char cmd; |
5236 | ||
8dccc7a6 LB |
5237 | /* if the item is not a command write it and continue */ |
5238 | if (item->command >= TODO_COMMENT) { | |
616d7740 | 5239 | strbuf_addf(buf, "%.*s\n", item->arg_len, |
cbef27d6 | 5240 | todo_item_get_arg(todo_list, item)); |
8dccc7a6 | 5241 | continue; |
3546c8d9 | 5242 | } |
8dccc7a6 LB |
5243 | |
5244 | /* add command to the buffer */ | |
68e7090f AG |
5245 | cmd = command_to_char(item->command); |
5246 | if ((flags & TODO_LIST_ABBREVIATE_CMDS) && cmd) | |
5247 | strbuf_addch(buf, cmd); | |
d8ae6c84 | 5248 | else |
616d7740 | 5249 | strbuf_addstr(buf, command_to_string(item->command)); |
8dccc7a6 LB |
5250 | |
5251 | /* add commit id */ | |
5252 | if (item->commit) { | |
313a48ea | 5253 | const char *oid = flags & TODO_LIST_SHORTEN_IDS ? |
8dccc7a6 LB |
5254 | short_commit_name(item->commit) : |
5255 | oid_to_hex(&item->commit->object.oid); | |
5256 | ||
4c68e7dd JS |
5257 | if (item->command == TODO_MERGE) { |
5258 | if (item->flags & TODO_EDIT_MERGE_MSG) | |
616d7740 | 5259 | strbuf_addstr(buf, " -c"); |
4c68e7dd | 5260 | else |
616d7740 | 5261 | strbuf_addstr(buf, " -C"); |
4c68e7dd JS |
5262 | } |
5263 | ||
616d7740 | 5264 | strbuf_addf(buf, " %s", oid); |
3546c8d9 | 5265 | } |
4c68e7dd | 5266 | |
8dccc7a6 | 5267 | /* add all the rest */ |
c7b4d79c | 5268 | if (!item->arg_len) |
616d7740 | 5269 | strbuf_addch(buf, '\n'); |
c7b4d79c | 5270 | else |
616d7740 | 5271 | strbuf_addf(buf, " %.*s\n", item->arg_len, |
cbef27d6 | 5272 | todo_item_get_arg(todo_list, item)); |
3546c8d9 | 5273 | } |
3546c8d9 | 5274 | } |
94399949 | 5275 | |
616d7740 AG |
5276 | int todo_list_write_to_file(struct repository *r, struct todo_list *todo_list, |
5277 | const char *file, const char *shortrevisions, | |
5278 | const char *shortonto, int num, unsigned flags) | |
94399949 | 5279 | { |
af1fc3ad | 5280 | int res; |
616d7740 | 5281 | struct strbuf buf = STRBUF_INIT; |
94399949 | 5282 | |
616d7740 | 5283 | todo_list_to_strbuf(r, todo_list, &buf, num, flags); |
af1fc3ad | 5284 | if (flags & TODO_LIST_APPEND_TODO_HELP) |
d48e5e21 | 5285 | append_todo_help(count_commands(todo_list), |
af1fc3ad | 5286 | shortrevisions, shortonto, &buf); |
94399949 | 5287 | |
616d7740 | 5288 | res = write_message(buf.buf, buf.len, file, 0); |
cbef27d6 | 5289 | strbuf_release(&buf); |
8315bd20 | 5290 | |
616d7740 | 5291 | return res; |
cbef27d6 | 5292 | } |
94399949 | 5293 | |
cdac2b01 | 5294 | /* skip picking commits whose parents are unchanged */ |
6bfeb7f1 AG |
5295 | static int skip_unnecessary_picks(struct repository *r, |
5296 | struct todo_list *todo_list, | |
5297 | struct object_id *base_oid) | |
cdac2b01 | 5298 | { |
d4ed5d77 | 5299 | struct object_id *parent_oid; |
6bfeb7f1 | 5300 | int i; |
cdac2b01 | 5301 | |
6bfeb7f1 AG |
5302 | for (i = 0; i < todo_list->nr; i++) { |
5303 | struct todo_item *item = todo_list->items + i; | |
cdac2b01 JS |
5304 | |
5305 | if (item->command >= TODO_NOOP) | |
5306 | continue; | |
5307 | if (item->command != TODO_PICK) | |
5308 | break; | |
5309 | if (parse_commit(item->commit)) { | |
cdac2b01 JS |
5310 | return error(_("could not parse commit '%s'"), |
5311 | oid_to_hex(&item->commit->object.oid)); | |
5312 | } | |
5313 | if (!item->commit->parents) | |
5314 | break; /* root commit */ | |
5315 | if (item->commit->parents->next) | |
5316 | break; /* merge commit */ | |
5317 | parent_oid = &item->commit->parents->item->object.oid; | |
6bfeb7f1 | 5318 | if (!oideq(parent_oid, base_oid)) |
cdac2b01 | 5319 | break; |
6bfeb7f1 | 5320 | oidcpy(base_oid, &item->commit->object.oid); |
cdac2b01 JS |
5321 | } |
5322 | if (i > 0) { | |
cdac2b01 JS |
5323 | const char *done_path = rebase_path_done(); |
5324 | ||
6bfeb7f1 | 5325 | if (todo_list_write_to_file(r, todo_list, done_path, NULL, NULL, i, 0)) { |
cdac2b01 | 5326 | error_errno(_("could not write to '%s'"), done_path); |
cdac2b01 JS |
5327 | return -1; |
5328 | } | |
cdac2b01 | 5329 | |
6bfeb7f1 AG |
5330 | MOVE_ARRAY(todo_list->items, todo_list->items + i, todo_list->nr - i); |
5331 | todo_list->nr -= i; | |
5332 | todo_list->current = 0; | |
34065541 | 5333 | todo_list->done_nr += i; |
cdac2b01 | 5334 | |
6bfeb7f1 AG |
5335 | if (is_fixup(peek_command(todo_list, 0))) |
5336 | record_in_rewritten(base_oid, peek_command(todo_list, 0)); | |
cdac2b01 JS |
5337 | } |
5338 | ||
cdac2b01 JS |
5339 | return 0; |
5340 | } | |
c44a4c65 | 5341 | |
005af339 | 5342 | int complete_action(struct repository *r, struct replay_opts *opts, unsigned flags, |
b97e1873 | 5343 | const char *shortrevisions, const char *onto_name, |
f3e27a02 | 5344 | struct commit *onto, const struct object_id *orig_head, |
7d3488eb PW |
5345 | struct string_list *commands, unsigned autosquash, |
5346 | struct todo_list *todo_list) | |
b97e1873 | 5347 | { |
5da69c0d AK |
5348 | char shortonto[GIT_MAX_HEXSZ + 1]; |
5349 | const char *todo_file = rebase_path_todo(); | |
94bcad79 | 5350 | struct todo_list new_todo = TODO_LIST_INIT; |
b6992261 | 5351 | struct strbuf *buf = &todo_list->buf, buf2 = STRBUF_INIT; |
7d3488eb | 5352 | struct object_id oid = onto->object.oid; |
1451d0f6 | 5353 | int res; |
b97e1873 | 5354 | |
5da69c0d | 5355 | find_unique_abbrev_r(shortonto, &oid, DEFAULT_ABBREV); |
b97e1873 | 5356 | |
94bcad79 AG |
5357 | if (buf->len == 0) { |
5358 | struct todo_item *item = append_new_todo(todo_list); | |
5359 | item->command = TODO_NOOP; | |
5360 | item->commit = NULL; | |
5361 | item->arg_len = item->arg_offset = item->flags = item->offset_in_buf = 0; | |
5362 | } | |
b97e1873 | 5363 | |
94bcad79 | 5364 | if (autosquash && todo_list_rearrange_squash(todo_list)) |
b97e1873 AG |
5365 | return -1; |
5366 | ||
683153a4 | 5367 | if (commands->nr) |
94bcad79 | 5368 | todo_list_add_exec_commands(todo_list, commands); |
b97e1873 | 5369 | |
94bcad79 | 5370 | if (count_commands(todo_list) == 0) { |
be1bb600 | 5371 | apply_autostash(rebase_path_autostash()); |
b97e1873 | 5372 | sequencer_remove_state(opts); |
b97e1873 AG |
5373 | |
5374 | return error(_("nothing to do")); | |
5375 | } | |
5376 | ||
1451d0f6 AG |
5377 | res = edit_todo_list(r, todo_list, &new_todo, shortrevisions, |
5378 | shortonto, flags); | |
5379 | if (res == -1) | |
b97e1873 | 5380 | return -1; |
1451d0f6 | 5381 | else if (res == -2) { |
be1bb600 | 5382 | apply_autostash(rebase_path_autostash()); |
b97e1873 | 5383 | sequencer_remove_state(opts); |
b97e1873 AG |
5384 | |
5385 | return -1; | |
1451d0f6 | 5386 | } else if (res == -3) { |
be1bb600 | 5387 | apply_autostash(rebase_path_autostash()); |
b97e1873 | 5388 | sequencer_remove_state(opts); |
94bcad79 | 5389 | todo_list_release(&new_todo); |
b97e1873 AG |
5390 | |
5391 | return error(_("nothing to do")); | |
5a5445d8 | 5392 | } else if (res == -4) { |
7d3488eb | 5393 | checkout_onto(r, opts, onto_name, &onto->object.oid, orig_head); |
94bcad79 AG |
5394 | todo_list_release(&new_todo); |
5395 | ||
b97e1873 AG |
5396 | return -1; |
5397 | } | |
5398 | ||
b6992261 JS |
5399 | /* Expand the commit IDs */ |
5400 | todo_list_to_strbuf(r, &new_todo, &buf2, -1, 0); | |
5401 | strbuf_swap(&new_todo.buf, &buf2); | |
5402 | strbuf_release(&buf2); | |
5403 | new_todo.total_nr -= new_todo.nr; | |
5404 | if (todo_list_parse_insn_buffer(r, new_todo.buf.buf, &new_todo) < 0) | |
5405 | BUG("invalid todo list after expanding IDs:\n%s", | |
5406 | new_todo.buf.buf); | |
5407 | ||
6bfeb7f1 AG |
5408 | if (opts->allow_ff && skip_unnecessary_picks(r, &new_todo, &oid)) { |
5409 | todo_list_release(&new_todo); | |
b97e1873 | 5410 | return error(_("could not skip unnecessary pick commands")); |
6bfeb7f1 AG |
5411 | } |
5412 | ||
94bcad79 AG |
5413 | if (todo_list_write_to_file(r, &new_todo, todo_file, NULL, NULL, -1, |
5414 | flags & ~(TODO_LIST_SHORTEN_IDS))) { | |
5415 | todo_list_release(&new_todo); | |
5416 | return error_errno(_("could not write '%s'"), todo_file); | |
5417 | } | |
5418 | ||
393adf7a | 5419 | res = -1; |
b97e1873 | 5420 | |
7d3488eb | 5421 | if (checkout_onto(r, opts, onto_name, &oid, orig_head)) |
393adf7a | 5422 | goto cleanup; |
29d51e21 | 5423 | |
005af339 | 5424 | if (require_clean_work_tree(r, "rebase", "", 1, 1)) |
393adf7a | 5425 | goto cleanup; |
b97e1873 | 5426 | |
393adf7a AG |
5427 | todo_list_write_total_nr(&new_todo); |
5428 | res = pick_commits(r, &new_todo, opts); | |
5429 | ||
5430 | cleanup: | |
5431 | todo_list_release(&new_todo); | |
5432 | ||
5433 | return res; | |
b97e1873 AG |
5434 | } |
5435 | ||
c44a4c65 JS |
5436 | struct subject2item_entry { |
5437 | struct hashmap_entry entry; | |
5438 | int i; | |
5439 | char subject[FLEX_ARRAY]; | |
5440 | }; | |
5441 | ||
5442 | static int subject2item_cmp(const void *fndata, | |
939af16e EW |
5443 | const struct hashmap_entry *eptr, |
5444 | const struct hashmap_entry *entry_or_key, | |
5445 | const void *key) | |
c44a4c65 | 5446 | { |
939af16e EW |
5447 | const struct subject2item_entry *a, *b; |
5448 | ||
5449 | a = container_of(eptr, const struct subject2item_entry, entry); | |
5450 | b = container_of(entry_or_key, const struct subject2item_entry, entry); | |
5451 | ||
c44a4c65 JS |
5452 | return key ? strcmp(a->subject, key) : strcmp(a->subject, b->subject); |
5453 | } | |
5454 | ||
3cc0287b NTND |
5455 | define_commit_slab(commit_todo_item, struct todo_item *); |
5456 | ||
c44a4c65 JS |
5457 | /* |
5458 | * Rearrange the todo list that has both "pick commit-id msg" and "pick | |
5459 | * commit-id fixup!/squash! msg" in it so that the latter is put immediately | |
5460 | * after the former, and change "pick" to "fixup"/"squash". | |
5461 | * | |
5462 | * Note that if the config has specified a custom instruction format, each log | |
5463 | * message will have to be retrieved from the commit (as the oneline in the | |
5464 | * script cannot be trusted) in order to normalize the autosquash arrangement. | |
5465 | */ | |
79d7e883 | 5466 | int todo_list_rearrange_squash(struct todo_list *todo_list) |
c44a4c65 | 5467 | { |
c44a4c65 | 5468 | struct hashmap subject2item; |
f2a04904 | 5469 | int rearranged = 0, *next, *tail, i, nr = 0, alloc = 0; |
c44a4c65 | 5470 | char **subjects; |
3cc0287b | 5471 | struct commit_todo_item commit_todo; |
f2a04904 | 5472 | struct todo_item *items = NULL; |
c44a4c65 | 5473 | |
3cc0287b | 5474 | init_commit_todo_item(&commit_todo); |
c44a4c65 JS |
5475 | /* |
5476 | * The hashmap maps onelines to the respective todo list index. | |
5477 | * | |
5478 | * If any items need to be rearranged, the next[i] value will indicate | |
5479 | * which item was moved directly after the i'th. | |
5480 | * | |
5481 | * In that case, last[i] will indicate the index of the latest item to | |
5482 | * be moved to appear after the i'th. | |
5483 | */ | |
939af16e | 5484 | hashmap_init(&subject2item, subject2item_cmp, NULL, todo_list->nr); |
f2a04904 AG |
5485 | ALLOC_ARRAY(next, todo_list->nr); |
5486 | ALLOC_ARRAY(tail, todo_list->nr); | |
5487 | ALLOC_ARRAY(subjects, todo_list->nr); | |
5488 | for (i = 0; i < todo_list->nr; i++) { | |
c44a4c65 | 5489 | struct strbuf buf = STRBUF_INIT; |
f2a04904 | 5490 | struct todo_item *item = todo_list->items + i; |
c44a4c65 JS |
5491 | const char *commit_buffer, *subject, *p; |
5492 | size_t subject_len; | |
5493 | int i2 = -1; | |
5494 | struct subject2item_entry *entry; | |
5495 | ||
5496 | next[i] = tail[i] = -1; | |
2f6b1d13 | 5497 | if (!item->commit || item->command == TODO_DROP) { |
c44a4c65 JS |
5498 | subjects[i] = NULL; |
5499 | continue; | |
5500 | } | |
5501 | ||
5502 | if (is_fixup(item->command)) { | |
3cc0287b | 5503 | clear_commit_todo_item(&commit_todo); |
c44a4c65 JS |
5504 | return error(_("the script was already rearranged.")); |
5505 | } | |
5506 | ||
3cc0287b | 5507 | *commit_todo_item_at(&commit_todo, item->commit) = item; |
c44a4c65 JS |
5508 | |
5509 | parse_commit(item->commit); | |
0798d16f | 5510 | commit_buffer = logmsg_reencode(item->commit, NULL, "UTF-8"); |
c44a4c65 JS |
5511 | find_commit_subject(commit_buffer, &subject); |
5512 | format_subject(&buf, subject, " "); | |
5513 | subject = subjects[i] = strbuf_detach(&buf, &subject_len); | |
5514 | unuse_commit_buffer(item->commit, commit_buffer); | |
5515 | if ((skip_prefix(subject, "fixup! ", &p) || | |
5516 | skip_prefix(subject, "squash! ", &p))) { | |
5517 | struct commit *commit2; | |
5518 | ||
5519 | for (;;) { | |
5520 | while (isspace(*p)) | |
5521 | p++; | |
5522 | if (!skip_prefix(p, "fixup! ", &p) && | |
5523 | !skip_prefix(p, "squash! ", &p)) | |
5524 | break; | |
5525 | } | |
5526 | ||
f23a4651 EW |
5527 | entry = hashmap_get_entry_from_hash(&subject2item, |
5528 | strhash(p), p, | |
5529 | struct subject2item_entry, | |
5530 | entry); | |
5531 | if (entry) | |
c44a4c65 JS |
5532 | /* found by title */ |
5533 | i2 = entry->i; | |
5534 | else if (!strchr(p, ' ') && | |
5535 | (commit2 = | |
5536 | lookup_commit_reference_by_name(p)) && | |
3cc0287b | 5537 | *commit_todo_item_at(&commit_todo, commit2)) |
c44a4c65 | 5538 | /* found by commit name */ |
3cc0287b | 5539 | i2 = *commit_todo_item_at(&commit_todo, commit2) |
f2a04904 | 5540 | - todo_list->items; |
c44a4c65 JS |
5541 | else { |
5542 | /* copy can be a prefix of the commit subject */ | |
5543 | for (i2 = 0; i2 < i; i2++) | |
5544 | if (subjects[i2] && | |
5545 | starts_with(subjects[i2], p)) | |
5546 | break; | |
5547 | if (i2 == i) | |
5548 | i2 = -1; | |
5549 | } | |
5550 | } | |
5551 | if (i2 >= 0) { | |
5552 | rearranged = 1; | |
f2a04904 | 5553 | todo_list->items[i].command = |
c44a4c65 JS |
5554 | starts_with(subject, "fixup!") ? |
5555 | TODO_FIXUP : TODO_SQUASH; | |
02471e7e JS |
5556 | if (tail[i2] < 0) { |
5557 | next[i] = next[i2]; | |
c44a4c65 | 5558 | next[i2] = i; |
02471e7e JS |
5559 | } else { |
5560 | next[i] = next[tail[i2]]; | |
c44a4c65 | 5561 | next[tail[i2]] = i; |
02471e7e | 5562 | } |
c44a4c65 JS |
5563 | tail[i2] = i; |
5564 | } else if (!hashmap_get_from_hash(&subject2item, | |
5565 | strhash(subject), subject)) { | |
5566 | FLEX_ALLOC_MEM(entry, subject, subject, subject_len); | |
5567 | entry->i = i; | |
d22245a2 EW |
5568 | hashmap_entry_init(&entry->entry, |
5569 | strhash(entry->subject)); | |
26b455f2 | 5570 | hashmap_put(&subject2item, &entry->entry); |
c44a4c65 JS |
5571 | } |
5572 | } | |
5573 | ||
5574 | if (rearranged) { | |
f2a04904 AG |
5575 | for (i = 0; i < todo_list->nr; i++) { |
5576 | enum todo_command command = todo_list->items[i].command; | |
c44a4c65 JS |
5577 | int cur = i; |
5578 | ||
5579 | /* | |
5580 | * Initially, all commands are 'pick's. If it is a | |
5581 | * fixup or a squash now, we have rearranged it. | |
5582 | */ | |
5583 | if (is_fixup(command)) | |
5584 | continue; | |
5585 | ||
5586 | while (cur >= 0) { | |
f2a04904 AG |
5587 | ALLOC_GROW(items, nr + 1, alloc); |
5588 | items[nr++] = todo_list->items[cur]; | |
c44a4c65 JS |
5589 | cur = next[cur]; |
5590 | } | |
5591 | } | |
5592 | ||
f2a04904 AG |
5593 | FREE_AND_NULL(todo_list->items); |
5594 | todo_list->items = items; | |
5595 | todo_list->nr = nr; | |
5596 | todo_list->alloc = alloc; | |
c44a4c65 JS |
5597 | } |
5598 | ||
5599 | free(next); | |
5600 | free(tail); | |
f2a04904 | 5601 | for (i = 0; i < todo_list->nr; i++) |
c44a4c65 JS |
5602 | free(subjects[i]); |
5603 | free(subjects); | |
6da1a258 | 5604 | hashmap_clear_and_free(&subject2item, struct subject2item_entry, entry); |
c44a4c65 | 5605 | |
3cc0287b | 5606 | clear_commit_todo_item(&commit_todo); |
f2a04904 AG |
5607 | |
5608 | return 0; | |
c44a4c65 | 5609 | } |
901ba7b1 PW |
5610 | |
5611 | int sequencer_determine_whence(struct repository *r, enum commit_whence *whence) | |
5612 | { | |
c8e4159e | 5613 | if (refs_ref_exists(get_main_ref_store(r), "CHERRY_PICK_HEAD")) { |
430b75f7 PW |
5614 | struct object_id cherry_pick_head, rebase_head; |
5615 | ||
5616 | if (file_exists(git_path_seq_dir())) | |
5617 | *whence = FROM_CHERRY_PICK_MULTI; | |
5618 | if (file_exists(rebase_path()) && | |
5619 | !get_oid("REBASE_HEAD", &rebase_head) && | |
5620 | !get_oid("CHERRY_PICK_HEAD", &cherry_pick_head) && | |
5621 | oideq(&rebase_head, &cherry_pick_head)) | |
5622 | *whence = FROM_REBASE_PICK; | |
5623 | else | |
5624 | *whence = FROM_CHERRY_PICK_SINGLE; | |
5625 | ||
901ba7b1 PW |
5626 | return 1; |
5627 | } | |
5628 | ||
5629 | return 0; | |
5630 | } |