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