🌐 AI搜索 & 代理 主页
blob: 3cc88d8a8004d91eb2a404214f4ac136a845d61f [file] [log] [blame]
Elijah Newrenbc5c5ec2023-05-16 06:33:571#include "git-compat-util.h"
Elijah Newren0b027f62023-03-21 06:25:582#include "abspath.h"
Elijah Newren6c6ddf92023-04-11 03:00:393#include "advice.h"
Brandon Williamsb2141fc2017-06-14 18:07:364#include "config.h"
Elijah Newrend5fff462023-04-22 20:17:125#include "copy.h"
Elijah Newren7ee24e12023-03-21 06:25:576#include "environment.h"
Elijah Newrenf394e092023-03-21 06:25:547#include "gettext.h"
Elijah Newren41771fa2023-02-24 00:09:278#include "hex.h"
Michael Haggerty697cc8e2014-10-01 10:28:429#include "lockfile.h"
Ramkumar Ramachandra26ae3372011-08-04 10:39:1110#include "dir.h"
Elijah Newren87bed172023-04-11 07:41:5311#include "object-file.h"
Elijah Newrendabab1d2023-04-11 07:41:4912#include "object-name.h"
Elijah Newrena034e912023-05-16 06:34:0613#include "object-store-ll.h"
Ramkumar Ramachandra043a4492012-01-11 18:15:5714#include "object.h"
Elijah Newrenca4eed72023-04-11 07:41:5915#include "pager.h"
Ramkumar Ramachandra043a4492012-01-11 18:15:5716#include "commit.h"
Phillip Wood0505d602017-11-17 11:34:4717#include "sequencer.h"
Ramkumar Ramachandra043a4492012-01-11 18:15:5718#include "run-command.h"
Ævar Arnfjörð Bjarmason5e3aba32021-09-26 19:03:2619#include "hook.h"
Ramkumar Ramachandra043a4492012-01-11 18:15:5720#include "utf8.h"
21#include "cache-tree.h"
22#include "diff.h"
Elijah Newrenc3399322023-05-16 06:33:5923#include "path.h"
Ramkumar Ramachandra043a4492012-01-11 18:15:5724#include "revision.h"
25#include "rerere.h"
Elijah Newren750324d2023-05-16 06:33:5426#include "merge.h"
Elijah Newren14c45862020-11-02 23:45:3427#include "merge-ort.h"
28#include "merge-ort-wrappers.h"
Ramkumar Ramachandra043a4492012-01-11 18:15:5729#include "refs.h"
Elijah Newrenbaf889c2023-05-16 06:33:5130#include "sparse-index.h"
Jeff Kingdbbcd442020-07-28 20:23:3931#include "strvec.h"
Johannes Schindelina1c75762016-10-21 12:25:1232#include "quote.h"
Jonathan Tan967dfd42016-11-02 17:29:2033#include "trailer.h"
Johannes Schindelin56dc3ab2017-01-02 15:26:4334#include "log-tree.h"
Johannes Schindelin311af522017-01-02 15:26:4735#include "wt-status.h"
Johannes Schindelinc44a4c62017-07-14 14:45:3136#include "hashmap.h"
Phillip Wooda87a6f32017-11-17 11:34:4837#include "notes-utils.h"
38#include "sigchain.h"
Johannes Schindelin9055e402018-04-25 12:28:4739#include "unpack-trees.h"
Johannes Schindelin1644c732018-04-25 12:29:0340#include "oidmap.h"
41#include "oidset.h"
Nguyễn Thái Ngọc Duy8315bd22018-05-19 05:28:2242#include "commit-slab.h"
Nguyễn Thái Ngọc Duy65b5f942018-05-20 18:40:0643#include "alias.h"
Derrick Stolee64043552018-07-20 16:33:0444#include "commit-reach.h"
Alban Gruinb97e1872018-08-28 12:10:3645#include "rebase-interactive.h"
Denton Liu0816f1d2020-04-07 14:28:0346#include "reset.h"
Derrick Stolee900b50c2022-07-19 18:33:3947#include "branch.h"
Ramkumar Ramachandra043a4492012-01-11 18:15:5748
49#define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
Ramkumar Ramachandra26ae3372011-08-04 10:39:1150
Mark Ruvald Pedersen7481d2b2023-08-10 16:34:5951/*
52 * To accommodate common filesystem limitations, where the loose refs' file
53 * names must not exceed `NAME_MAX`, the labels generated by `git rebase
54 * --rebase-merges` need to be truncated if the corresponding commit subjects
55 * are too long.
56 * Add some margin to stay clear from reaching `NAME_MAX`.
57 */
58#define GIT_MAX_LABEL_LENGTH ((NAME_MAX) - (LOCK_SUFFIX_LEN) - 16)
59
Ramsay Jones5fe81432019-02-11 17:16:5860static const char sign_off_header[] = "Signed-off-by: ";
Brandon Caseycd650a42013-02-12 10:17:3261static const char cherry_picked_prefix[] = "(cherry picked from commit ";
Miklos Vajna5ed75e22012-09-14 06:52:0362
Phillip Wood66618a52018-01-24 12:34:2263GIT_PATH_FUNC(git_path_commit_editmsg, "COMMIT_EDITMSG")
64
Phillip Wood901ba7b2019-12-06 16:06:1165static GIT_PATH_FUNC(git_path_seq_dir, "sequencer")
Johannes Schindelin8a2a0f52016-10-14 13:17:1266
67static GIT_PATH_FUNC(git_path_todo_file, "sequencer/todo")
68static GIT_PATH_FUNC(git_path_opts_file, "sequencer/opts")
69static GIT_PATH_FUNC(git_path_head_file, "sequencer/head")
Stephan Beyer1e412292016-12-07 21:51:3270static GIT_PATH_FUNC(git_path_abort_safety_file, "sequencer/abort-safety")
Jeff Kingf9327292015-08-10 09:38:5771
Johannes Schindelin84583952017-01-02 15:26:2872static GIT_PATH_FUNC(rebase_path, "rebase-merge")
73/*
74 * The file containing rebase commands, comments, and empty lines.
75 * This file is created by "git rebase -i" then edited by the user. As
76 * the lines are processed, they are removed from the front of this
77 * file and written to the tail of 'done'.
78 */
Alban Gruin44b776c2018-08-10 16:51:2879GIT_PATH_FUNC(rebase_path_todo, "rebase-merge/git-rebase-todo")
Alban Gruina930eb02019-03-05 19:18:0380GIT_PATH_FUNC(rebase_path_todo_backup, "rebase-merge/git-rebase-todo.backup")
Alban Gruinb97e1872018-08-28 12:10:3681
Alban Gruin5a5445d2020-01-28 21:12:4682GIT_PATH_FUNC(rebase_path_dropped, "rebase-merge/dropped")
83
Johannes Schindelinb5a67042016-10-21 12:25:0484/*
Johannes Schindelin1df6df02017-01-02 15:27:0085 * The rebase command lines that have already been processed. A line
86 * is moved here when it is first handled, before any associated user
87 * actions.
88 */
89static GIT_PATH_FUNC(rebase_path_done, "rebase-merge/done")
90/*
Johannes Schindelinef800692017-01-02 15:36:2091 * The file to keep track of how many commands were already processed (e.g.
92 * for the prompt).
93 */
Beat Bolli9ad36352018-07-09 19:25:3594static GIT_PATH_FUNC(rebase_path_msgnum, "rebase-merge/msgnum")
Johannes Schindelinef800692017-01-02 15:36:2095/*
96 * The file to keep track of how many commands are to be processed in total
97 * (e.g. for the prompt).
98 */
Beat Bolli9ad36352018-07-09 19:25:3599static GIT_PATH_FUNC(rebase_path_msgtotal, "rebase-merge/end")
Johannes Schindelinef800692017-01-02 15:36:20100/*
Johannes Schindelin6e98de72017-01-02 15:27:07101 * The commit message that is planned to be used for any changes that
102 * need to be committed following a user interaction.
103 */
104static GIT_PATH_FUNC(rebase_path_message, "rebase-merge/message")
105/*
106 * The file into which is accumulated the suggested commit message for
107 * squash/fixup commands. When the first of a series of squash/fixups
108 * is seen, the file is created and the commit message from the
109 * previous commit and from the first squash/fixup commit are written
110 * to it. The commit message for each subsequent squash/fixup commit
111 * is appended to the file as it is processed.
Johannes Schindelin6e98de72017-01-02 15:27:07112 */
113static GIT_PATH_FUNC(rebase_path_squash_msg, "rebase-merge/message-squash")
114/*
115 * If the current series of squash/fixups has not yet included a squash
116 * command, then this file exists and holds the commit message of the
117 * original "pick" commit. (If the series ends without a "squash"
118 * command, then this can be used as the commit message of the combined
119 * commit without opening the editor.)
120 */
121static GIT_PATH_FUNC(rebase_path_fixup_msg, "rebase-merge/message-fixup")
122/*
Johannes Schindeline12a7ef2018-04-27 20:48:21123 * This file contains the list fixup/squash commands that have been
124 * accumulated into message-fixup or message-squash so far.
125 */
126static GIT_PATH_FUNC(rebase_path_current_fixups, "rebase-merge/current-fixups")
127/*
Johannes Schindelinb5a67042016-10-21 12:25:04128 * A script to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
129 * GIT_AUTHOR_DATE that will be used for the commit that is currently
130 * being rebased.
131 */
132static GIT_PATH_FUNC(rebase_path_author_script, "rebase-merge/author-script")
Johannes Schindelina1c75762016-10-21 12:25:12133/*
Johannes Schindelin56dc3ab2017-01-02 15:26:43134 * When an "edit" rebase command is being processed, the SHA1 of the
135 * commit to be edited is recorded in this file. When "git rebase
136 * --continue" is executed, if there are any staged changes then they
137 * will be amended to the HEAD commit, but only provided the HEAD
138 * commit is still the commit to be edited. When any other rebase
139 * command is processed, this file is deleted.
140 */
141static GIT_PATH_FUNC(rebase_path_amend, "rebase-merge/amend")
142/*
143 * When we stop at a given patch via the "edit" command, this file contains
Junio C Hamano0512eab2020-09-25 05:49:12144 * the commit object name of the corresponding patch.
Johannes Schindelin56dc3ab2017-01-02 15:26:43145 */
146static GIT_PATH_FUNC(rebase_path_stopped_sha, "rebase-merge/stopped-sha")
147/*
Phillip Wood206a78d2023-09-06 15:22:46148 * When we stop for the user to resolve conflicts this file contains
149 * the patch of the commit that is being picked.
150 */
151static GIT_PATH_FUNC(rebase_path_patch, "rebase-merge/patch")
152/*
Johannes Schindelin25cb8df2017-01-02 15:28:16153 * For the post-rewrite hook, we make a list of rewritten commits and
154 * their new sha1s. The rewritten-pending list keeps the sha1s of
155 * commits that have been processed, but not committed yet,
156 * e.g. because they are waiting for a 'squash' command.
157 */
158static GIT_PATH_FUNC(rebase_path_rewritten_list, "rebase-merge/rewritten-list")
159static GIT_PATH_FUNC(rebase_path_rewritten_pending,
160 "rebase-merge/rewritten-pending")
Johannes Schindelin9055e402018-04-25 12:28:47161
162/*
Elijah Newren15beaaa2019-11-05 17:07:23163 * The path of the file containing the OID of the "squash onto" commit, i.e.
Johannes Schindelind87d48b2018-05-03 23:01:17164 * the dummy commit used for `reset [new root]`.
165 */
166static GIT_PATH_FUNC(rebase_path_squash_onto, "rebase-merge/squash-onto")
167
168/*
Johannes Schindelin9055e402018-04-25 12:28:47169 * The path of the file listing refs that need to be deleted after the rebase
170 * finishes. This is used by the `label` command to record the need for cleanup.
171 */
172static GIT_PATH_FUNC(rebase_path_refs_to_delete, "rebase-merge/refs-to-delete")
173
Johannes Schindelin25cb8df2017-01-02 15:28:16174/*
Derrick Stoleeaa7f2fd2022-07-19 18:33:35175 * The update-refs file stores a list of refs that will be updated at the end
176 * of the rebase sequence. The 'update-ref <ref>' commands in the todo file
177 * update the OIDs for the refs in this file, but the refs are not updated
178 * until the end of the rebase sequence.
179 *
180 * rebase_path_update_refs() returns the path to this file for a given
181 * worktree directory. For the current worktree, pass the_repository->gitdir.
182 */
183static char *rebase_path_update_refs(const char *wt_git_dir)
184{
185 return xstrfmt("%s/rebase-merge/update-refs", wt_git_dir);
186}
187
188/*
Johannes Schindelina1c75762016-10-21 12:25:12189 * The following files are written by git-rebase just after parsing the
Alban Gruin65850682018-08-28 12:10:40190 * command-line.
Johannes Schindelina1c75762016-10-21 12:25:12191 */
192static GIT_PATH_FUNC(rebase_path_gpg_sign_opt, "rebase-merge/gpg_sign_opt")
Phillip Wood7573cec2020-08-17 17:40:02193static GIT_PATH_FUNC(rebase_path_cdate_is_adate, "rebase-merge/cdate_is_adate")
Phillip Wooda3894aa2020-08-17 17:40:03194static GIT_PATH_FUNC(rebase_path_ignore_date, "rebase-merge/ignore_date")
Johannes Schindelin556907f2017-01-02 15:26:53195static GIT_PATH_FUNC(rebase_path_orig_head, "rebase-merge/orig-head")
196static GIT_PATH_FUNC(rebase_path_verbose, "rebase-merge/verbose")
Elijah Newren899b49c2018-12-11 16:11:36197static GIT_PATH_FUNC(rebase_path_quiet, "rebase-merge/quiet")
Phillip Wooda852ec72018-03-20 11:10:55198static GIT_PATH_FUNC(rebase_path_signoff, "rebase-merge/signoff")
Johannes Schindelin4b83ce92017-01-02 15:27:53199static GIT_PATH_FUNC(rebase_path_head_name, "rebase-merge/head-name")
200static GIT_PATH_FUNC(rebase_path_onto, "rebase-merge/onto")
Johannes Schindelin796c7972017-01-02 15:28:27201static GIT_PATH_FUNC(rebase_path_autostash, "rebase-merge/autostash")
Johannes Schindelinca6c6b42017-01-02 15:28:30202static GIT_PATH_FUNC(rebase_path_strategy, "rebase-merge/strategy")
203static GIT_PATH_FUNC(rebase_path_strategy_opts, "rebase-merge/strategy_opts")
Phillip Wood9b6d7a62017-08-02 10:44:17204static GIT_PATH_FUNC(rebase_path_allow_rerere_autoupdate, "rebase-merge/allow_rerere_autoupdate")
Johannes Schindelind421afa2018-12-10 19:04:58205static GIT_PATH_FUNC(rebase_path_reschedule_failed_exec, "rebase-merge/reschedule-failed-exec")
Ævar Arnfjörð Bjarmasone5b32bf2021-04-09 08:01:38206static GIT_PATH_FUNC(rebase_path_no_reschedule_failed_exec, "rebase-merge/no-reschedule-failed-exec")
Elijah Newrene98c4262020-02-15 21:36:25207static GIT_PATH_FUNC(rebase_path_drop_redundant_commits, "rebase-merge/drop_redundant_commits")
208static GIT_PATH_FUNC(rebase_path_keep_redundant_commits, "rebase-merge/keep_redundant_commits")
Johannes Schindelinb5a67042016-10-21 12:25:04209
Derrick Stoleeaa7f2fd2022-07-19 18:33:35210/**
211 * A 'struct update_refs_record' represents a value in the update-refs
212 * list. We use a string_list to map refs to these (before, after) pairs.
213 */
214struct update_ref_record {
215 struct object_id before;
216 struct object_id after;
217};
218
Derrick Stolee89fc0b52022-07-19 18:33:40219static struct update_ref_record *init_update_ref_record(const char *ref)
220{
221 struct update_ref_record *rec;
222
223 CALLOC_ARRAY(rec, 1);
224
225 oidcpy(&rec->before, null_oid());
226 oidcpy(&rec->after, null_oid());
227
228 /* This may fail, but that's fine, we will keep the null OID. */
229 read_ref(ref, &rec->before);
230
231 return rec;
232}
233
Glen Chooa4e7e312023-06-28 19:26:22234static int git_sequencer_config(const char *k, const char *v,
235 const struct config_context *ctx, void *cb)
Phillip Wood28d6dae2017-12-13 11:46:21236{
237 struct replay_opts *opts = cb;
Phillip Wood28d6dae2017-12-13 11:46:21238
239 if (!strcmp(k, "commit.cleanup")) {
Jeff Kingea8f9492023-12-07 07:26:42240 if (!v)
241 return config_error_nonbool(k);
Phillip Wood28d6dae2017-12-13 11:46:21242
Jeff Kingea8f9492023-12-07 07:26:42243 if (!strcmp(v, "verbatim")) {
Phillip Wood28d6dae2017-12-13 11:46:21244 opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_NONE;
Phillip Woodd74f3e52019-03-29 11:08:42245 opts->explicit_cleanup = 1;
Jeff Kingea8f9492023-12-07 07:26:42246 } else if (!strcmp(v, "whitespace")) {
Phillip Wood28d6dae2017-12-13 11:46:21247 opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_SPACE;
Phillip Woodd74f3e52019-03-29 11:08:42248 opts->explicit_cleanup = 1;
Jeff Kingea8f9492023-12-07 07:26:42249 } else if (!strcmp(v, "strip")) {
Phillip Wood28d6dae2017-12-13 11:46:21250 opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_ALL;
Phillip Woodd74f3e52019-03-29 11:08:42251 opts->explicit_cleanup = 1;
Jeff Kingea8f9492023-12-07 07:26:42252 } else if (!strcmp(v, "scissors")) {
Denton Liu1a2b9852019-04-17 10:23:30253 opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_SCISSORS;
Phillip Woodd74f3e52019-03-29 11:08:42254 opts->explicit_cleanup = 1;
255 } else {
Phillip Wood28d6dae2017-12-13 11:46:21256 warning(_("invalid commit message cleanup mode '%s'"),
Jeff Kingea8f9492023-12-07 07:26:42257 v);
Phillip Woodd74f3e52019-03-29 11:08:42258 }
Phillip Wood28d6dae2017-12-13 11:46:21259
Jeff Kingea8f9492023-12-07 07:26:42260 return 0;
Phillip Wood28d6dae2017-12-13 11:46:21261 }
262
263 if (!strcmp(k, "commit.gpgsign")) {
Johannes Schindelined1e5282017-12-22 11:50:50264 opts->gpg_sign = git_config_bool(k, v) ? xstrdup("") : NULL;
Phillip Wood28d6dae2017-12-13 11:46:21265 return 0;
266 }
267
Elijah Newren14c45862020-11-02 23:45:34268 if (!opts->default_strategy && !strcmp(k, "pull.twohead")) {
269 int ret = git_config_string((const char**)&opts->default_strategy, k, v);
270 if (ret == 0) {
271 /*
272 * pull.twohead is allowed to be multi-valued; we only
273 * care about the first value.
274 */
275 char *tmp = strchr(opts->default_strategy, ' ');
276 if (tmp)
277 *tmp = '\0';
278 }
279 return ret;
280 }
281
Junio C Hamano191faaf2022-05-31 16:22:20282 if (opts->action == REPLAY_REVERT && !strcmp(k, "revert.reference"))
Junio C Hamano43966ab2022-05-27 06:01:39283 opts->commit_use_reference = git_config_bool(k, v);
284
Glen Chooa4e7e312023-06-28 19:26:22285 return git_diff_basic_config(k, v, ctx, NULL);
Phillip Wood28d6dae2017-12-13 11:46:21286}
287
288void sequencer_init_config(struct replay_opts *opts)
289{
290 opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_NONE;
291 git_config(git_sequencer_config, opts);
292}
293
Johannes Schindelinb5a67042016-10-21 12:25:04294static inline int is_rebase_i(const struct replay_opts *opts)
295{
Johannes Schindelin84583952017-01-02 15:26:28296 return opts->action == REPLAY_INTERACTIVE_REBASE;
Johannes Schindelinb5a67042016-10-21 12:25:04297}
298
Johannes Schindelin285abf52016-10-14 13:17:20299static const char *get_dir(const struct replay_opts *opts)
300{
Johannes Schindelin84583952017-01-02 15:26:28301 if (is_rebase_i(opts))
302 return rebase_path();
Johannes Schindelin285abf52016-10-14 13:17:20303 return git_path_seq_dir();
304}
305
Johannes Schindelinc0246502016-10-21 12:24:32306static const char *get_todo_path(const struct replay_opts *opts)
307{
Johannes Schindelin84583952017-01-02 15:26:28308 if (is_rebase_i(opts))
309 return rebase_path_todo();
Johannes Schindelinc0246502016-10-21 12:24:32310 return git_path_todo_file();
311}
312
Brandon Caseybab4d102013-02-12 10:17:35313/*
314 * Returns 0 for non-conforming footer
315 * Returns 1 for conforming footer
316 * Returns 2 when sob exists within conforming footer
317 * Returns 3 when sob exists within conforming footer as last entry
318 */
319static int has_conforming_footer(struct strbuf *sb, struct strbuf *sob,
Jeff King66e83d92018-08-23 00:50:51320 size_t ignore_footer)
Brandon Caseyb971e042013-02-12 10:17:34321{
Jeff King00a21f52018-08-23 00:46:23322 struct process_trailer_options opts = PROCESS_TRAILER_OPTIONS_INIT;
Jonathan Tan967dfd42016-11-02 17:29:20323 struct trailer_info info;
Jeff Kinga3b636e2018-08-23 00:45:44324 size_t i;
Jonathan Tan967dfd42016-11-02 17:29:20325 int found_sob = 0, found_sob_last = 0;
Jeff King9dad0732020-09-30 12:34:11326 char saved_char;
Brandon Caseyb971e042013-02-12 10:17:34327
Jeff Kingffce7f52018-08-23 00:50:37328 opts.no_divider = 1;
329
Jeff King9dad0732020-09-30 12:34:11330 if (ignore_footer) {
331 saved_char = sb->buf[sb->len - ignore_footer];
332 sb->buf[sb->len - ignore_footer] = '\0';
333 }
334
Jeff King00a21f52018-08-23 00:46:23335 trailer_info_get(&info, sb->buf, &opts);
Jonathan Tan967dfd42016-11-02 17:29:20336
Jeff King9dad0732020-09-30 12:34:11337 if (ignore_footer)
338 sb->buf[sb->len - ignore_footer] = saved_char;
339
Linus Arverde7c27a2023-10-20 19:01:35340 if (info.trailer_block_start == info.trailer_block_end)
Brandon Caseyb971e042013-02-12 10:17:34341 return 0;
342
Jonathan Tan967dfd42016-11-02 17:29:20343 for (i = 0; i < info.trailer_nr; i++)
344 if (sob && !strncmp(info.trailers[i], sob->buf, sob->len)) {
345 found_sob = 1;
346 if (i == info.trailer_nr - 1)
347 found_sob_last = 1;
348 }
Brandon Caseyb971e042013-02-12 10:17:34349
Jonathan Tan967dfd42016-11-02 17:29:20350 trailer_info_release(&info);
Brandon Caseyb971e042013-02-12 10:17:34351
Jonathan Tan967dfd42016-11-02 17:29:20352 if (found_sob_last)
Brandon Caseybab4d102013-02-12 10:17:35353 return 3;
354 if (found_sob)
355 return 2;
Brandon Caseyb971e042013-02-12 10:17:34356 return 1;
357}
Ramkumar Ramachandra26ae3372011-08-04 10:39:11358
Johannes Schindelina1c75762016-10-21 12:25:12359static const char *gpg_sign_opt_quoted(struct replay_opts *opts)
360{
361 static struct strbuf buf = STRBUF_INIT;
362
363 strbuf_reset(&buf);
364 if (opts->gpg_sign)
365 sq_quotef(&buf, "-S%s", opts->gpg_sign);
366 return buf.buf;
367}
368
Ævar Arnfjörð Bjarmason9ff2f062023-02-06 19:08:08369void replay_opts_release(struct replay_opts *opts)
Ævar Arnfjörð Bjarmason6a09c3a2023-02-06 19:08:07370{
371 free(opts->gpg_sign);
372 free(opts->reflog_action);
373 free(opts->default_strategy);
374 free(opts->strategy);
Phillip Woodfb60b9f2023-04-10 09:08:28375 strvec_clear (&opts->xopts);
Ævar Arnfjörð Bjarmason6a09c3a2023-02-06 19:08:07376 strbuf_release(&opts->current_fixups);
Ævar Arnfjörð Bjarmasona6a700a2023-02-06 19:08:09377 if (opts->revs)
378 release_revisions(opts->revs);
379 free(opts->revs);
Ævar Arnfjörð Bjarmason6a09c3a2023-02-06 19:08:07380}
381
Johannes Schindelin28635842016-10-21 12:24:55382int sequencer_remove_state(struct replay_opts *opts)
Ramkumar Ramachandra26ae3372011-08-04 10:39:11383{
Johannes Schindelin9055e402018-04-25 12:28:47384 struct strbuf buf = STRBUF_INIT;
Ævar Arnfjörð Bjarmason6a09c3a2023-02-06 19:08:07385 int ret = 0;
Johannes Schindelin03a4e262016-10-21 12:24:13386
Johannes Schindelin9055e402018-04-25 12:28:47387 if (is_rebase_i(opts) &&
388 strbuf_read_file(&buf, rebase_path_refs_to_delete(), 0) > 0) {
389 char *p = buf.buf;
390 while (*p) {
391 char *eol = strchr(p, '\n');
392 if (eol)
393 *eol = '\0';
Elijah Newrenc2417d32020-02-15 21:36:36394 if (delete_ref("(rebase) cleanup", p, NULL, 0) < 0) {
Johannes Schindelin9055e402018-04-25 12:28:47395 warning(_("could not delete '%s'"), p);
Phillip Wood37e9ee52019-05-14 18:03:48396 ret = -1;
397 }
Johannes Schindelin9055e402018-04-25 12:28:47398 if (!eol)
399 break;
400 p = eol + 1;
401 }
402 }
403
Johannes Schindelin9055e402018-04-25 12:28:47404 strbuf_reset(&buf);
405 strbuf_addstr(&buf, get_dir(opts));
Phillip Wood37e9ee52019-05-14 18:03:48406 if (remove_dir_recursively(&buf, 0))
407 ret = error(_("could not remove '%s'"), buf.buf);
Johannes Schindelin9055e402018-04-25 12:28:47408 strbuf_release(&buf);
Johannes Schindelin28635842016-10-21 12:24:55409
Phillip Wood37e9ee52019-05-14 18:03:48410 return ret;
Ramkumar Ramachandra26ae3372011-08-04 10:39:11411}
Ramkumar Ramachandra043a4492012-01-11 18:15:57412
413static const char *action_name(const struct replay_opts *opts)
414{
Johannes Schindelin84583952017-01-02 15:26:28415 switch (opts->action) {
416 case REPLAY_REVERT:
417 return N_("revert");
418 case REPLAY_PICK:
419 return N_("cherry-pick");
420 case REPLAY_INTERACTIVE_REBASE:
Elijah Newrenc2417d32020-02-15 21:36:36421 return N_("rebase");
Johannes Schindelin84583952017-01-02 15:26:28422 }
Nguyễn Thái Ngọc Duy1a07e592018-07-21 07:49:19423 die(_("unknown action: %d"), opts->action);
Ramkumar Ramachandra043a4492012-01-11 18:15:57424}
425
Ramkumar Ramachandra043a4492012-01-11 18:15:57426struct commit_message {
427 char *parent_label;
Jeff King7b35eaf2016-02-22 22:44:57428 char *label;
429 char *subject;
Ramkumar Ramachandra043a4492012-01-11 18:15:57430 const char *message;
431};
432
Jeff Kingcb646ff2023-08-29 23:43:39433static const char *short_commit_name(struct repository *r, struct commit *commit)
Johannes Schindelin39755962016-10-21 12:24:37434{
Jeff Kingcb646ff2023-08-29 23:43:39435 return repo_find_unique_abbrev(r, &commit->object.oid, DEFAULT_ABBREV);
Johannes Schindelin39755962016-10-21 12:24:37436}
437
Ramkumar Ramachandra043a4492012-01-11 18:15:57438static int get_message(struct commit *commit, struct commit_message *out)
439{
Ramkumar Ramachandra043a4492012-01-11 18:15:57440 const char *abbrev, *subject;
Jeff King7b35eaf2016-02-22 22:44:57441 int subject_len;
Ramkumar Ramachandra043a4492012-01-11 18:15:57442
Ævar Arnfjörð Bjarmasonecb50912023-03-28 13:58:48443 out->message = repo_logmsg_reencode(the_repository, commit, NULL,
444 get_commit_output_encoding());
Jeff Kingcb646ff2023-08-29 23:43:39445 abbrev = short_commit_name(the_repository, commit);
Ramkumar Ramachandra043a4492012-01-11 18:15:57446
447 subject_len = find_commit_subject(out->message, &subject);
448
Jeff King7b35eaf2016-02-22 22:44:57449 out->subject = xmemdupz(subject, subject_len);
Elijah Newren7d056de2020-08-12 14:40:04450 out->label = xstrfmt("%s (%s)", abbrev, out->subject);
Jeff King7b35eaf2016-02-22 22:44:57451 out->parent_label = xstrfmt("parent of %s", out->label);
452
Ramkumar Ramachandra043a4492012-01-11 18:15:57453 return 0;
454}
455
Jeff Kingd74a4e52014-06-10 21:39:35456static void free_message(struct commit *commit, struct commit_message *msg)
Ramkumar Ramachandra043a4492012-01-11 18:15:57457{
458 free(msg->parent_label);
Jeff King7b35eaf2016-02-22 22:44:57459 free(msg->label);
460 free(msg->subject);
Ævar Arnfjörð Bjarmasonecb50912023-03-28 13:58:48461 repo_unuse_commit_buffer(the_repository, commit, msg->message);
Ramkumar Ramachandra043a4492012-01-11 18:15:57462}
463
Nguyễn Thái Ngọc Duy005af332018-11-10 05:48:57464static void print_advice(struct repository *r, int show_hint,
465 struct replay_opts *opts)
Ramkumar Ramachandra043a4492012-01-11 18:15:57466{
467 char *msg = getenv("GIT_CHERRY_PICK_HELP");
468
469 if (msg) {
ZheNing Huf1725562021-08-22 13:08:41470 advise("%s\n", msg);
Ramkumar Ramachandra043a4492012-01-11 18:15:57471 /*
Stefano Lattarini41ccfdd2013-04-11 22:36:10472 * A conflict has occurred but the porcelain
Ramkumar Ramachandra043a4492012-01-11 18:15:57473 * (typically rebase --interactive) wants to take care
474 * of the commit itself so remove CHERRY_PICK_HEAD
475 */
Han-Wen Nienhuysc8e41592020-08-21 16:59:35476 refs_delete_ref(get_main_ref_store(r), "", "CHERRY_PICK_HEAD",
477 NULL, 0);
Ramkumar Ramachandra043a4492012-01-11 18:15:57478 return;
479 }
480
Phil Horded727b12012-02-22 00:44:17481 if (show_hint) {
482 if (opts->no_commit)
483 advise(_("after resolving the conflicts, mark the corrected paths\n"
484 "with 'git add <paths>' or 'git rm <paths>'"));
ZheNing Huf1725562021-08-22 13:08:41485 else if (opts->action == REPLAY_PICK)
486 advise(_("After resolving the conflicts, mark them with\n"
487 "\"git add/rm <pathspec>\", then run\n"
488 "\"git cherry-pick --continue\".\n"
489 "You can instead skip this commit with \"git cherry-pick --skip\".\n"
490 "To abort and get back to the state before \"git cherry-pick\",\n"
491 "run \"git cherry-pick --abort\"."));
492 else if (opts->action == REPLAY_REVERT)
493 advise(_("After resolving the conflicts, mark them with\n"
494 "\"git add/rm <pathspec>\", then run\n"
495 "\"git revert --continue\".\n"
496 "You can instead skip this commit with \"git revert --skip\".\n"
497 "To abort and get back to the state before \"git revert\",\n"
498 "run \"git revert --abort\"."));
Phil Horded727b12012-02-22 00:44:17499 else
ZheNing Huf1725562021-08-22 13:08:41500 BUG("unexpected pick action in print_advice()");
Phil Horded727b12012-02-22 00:44:17501 }
Ramkumar Ramachandra043a4492012-01-11 18:15:57502}
503
Alban Gruinddb81e52019-03-05 19:18:01504static int write_message(const void *buf, size_t len, const char *filename,
505 int append_eol)
Ramkumar Ramachandra043a4492012-01-11 18:15:57506{
Martin Ågren14bca6c2018-02-27 21:30:09507 struct lock_file msg_file = LOCK_INIT;
Ramkumar Ramachandra043a4492012-01-11 18:15:57508
Johannes Schindelin4ef3d8f2016-09-09 14:37:05509 int msg_fd = hold_lock_file_for_update(&msg_file, filename, 0);
510 if (msg_fd < 0)
Johannes Schindelin93b3df62016-10-21 12:26:25511 return error_errno(_("could not lock '%s'"), filename);
Johannes Schindelin75871492016-10-21 12:26:05512 if (write_in_full(msg_fd, buf, len) < 0) {
Johannes Schindelinbf5c0572018-04-25 12:28:17513 error_errno(_("could not write to '%s'"), filename);
Johannes Schindelin4f66c832016-10-21 12:26:00514 rollback_lock_file(&msg_file);
Johannes Schindelinbf5c0572018-04-25 12:28:17515 return -1;
Johannes Schindelin4f66c832016-10-21 12:26:00516 }
Johannes Schindelinf56fffe2016-10-21 12:26:09517 if (append_eol && write(msg_fd, "\n", 1) < 0) {
Johannes Schindelinbf5c0572018-04-25 12:28:17518 error_errno(_("could not write eol to '%s'"), filename);
Johannes Schindelinf56fffe2016-10-21 12:26:09519 rollback_lock_file(&msg_file);
Johannes Schindelinbf5c0572018-04-25 12:28:17520 return -1;
Johannes Schindelinf56fffe2016-10-21 12:26:09521 }
Martin Ågren350292a2018-02-28 19:07:58522 if (commit_lock_file(&msg_file) < 0)
523 return error(_("failed to finalize '%s'"), filename);
Johannes Schindelin4ef3d8f2016-09-09 14:37:05524
525 return 0;
Ramkumar Ramachandra043a4492012-01-11 18:15:57526}
527
Denton Liuc20de8b2020-04-07 14:27:54528int read_oneliner(struct strbuf *buf,
Denton Liu3442c3d2020-04-07 14:27:52529 const char *path, unsigned flags)
Johannes Schindelin1dfc84e2016-10-21 12:25:08530{
531 int orig_len = buf->len;
532
Johannes Schindelin1dfc84e2016-10-21 12:25:08533 if (strbuf_read_file(buf, path, 0) < 0) {
Denton Liubfa50c22020-04-07 14:27:53534 if ((flags & READ_ONELINER_WARN_MISSING) ||
535 (errno != ENOENT && errno != ENOTDIR))
Denton Liu5b2f6d92020-04-07 14:27:51536 warning_errno(_("could not read '%s'"), path);
Johannes Schindelin1dfc84e2016-10-21 12:25:08537 return 0;
538 }
539
540 if (buf->len > orig_len && buf->buf[buf->len - 1] == '\n') {
541 if (--buf->len > orig_len && buf->buf[buf->len - 1] == '\r')
542 --buf->len;
543 buf->buf[buf->len] = '\0';
544 }
545
Denton Liu3442c3d2020-04-07 14:27:52546 if ((flags & READ_ONELINER_SKIP_IF_EMPTY) && buf->len == orig_len)
Johannes Schindelin1dfc84e2016-10-21 12:25:08547 return 0;
548
549 return 1;
550}
551
Nguyễn Thái Ngọc Duy005af332018-11-10 05:48:57552static struct tree *empty_tree(struct repository *r)
Ramkumar Ramachandra043a4492012-01-11 18:15:57553{
Nguyễn Thái Ngọc Duy005af332018-11-10 05:48:57554 return lookup_tree(r, the_hash_algo->empty_tree);
Ramkumar Ramachandra043a4492012-01-11 18:15:57555}
556
Nguyễn Thái Ngọc Duye1ff0a32019-01-12 02:13:26557static int error_dirty_index(struct repository *repo, struct replay_opts *opts)
Ramkumar Ramachandra043a4492012-01-11 18:15:57558{
Nguyễn Thái Ngọc Duye1ff0a32019-01-12 02:13:26559 if (repo_read_index_unmerged(repo))
Michael J Gruber1c8dfc32022-08-18 13:13:27560 return error_resolve_conflict(action_name(opts));
Ramkumar Ramachandra043a4492012-01-11 18:15:57561
Johannes Schindelin93b3df62016-10-21 12:26:25562 error(_("your local changes would be overwritten by %s."),
Johannes Schindelinc28cbc52016-10-21 12:26:17563 _(action_name(opts)));
Ramkumar Ramachandra043a4492012-01-11 18:15:57564
Ben Boeckeled9bff02021-08-23 10:44:00565 if (advice_enabled(ADVICE_COMMIT_BEFORE_MERGE))
Johannes Schindelin93b3df62016-10-21 12:26:25566 advise(_("commit your changes or stash them to proceed."));
Ramkumar Ramachandra043a4492012-01-11 18:15:57567 return -1;
568}
569
Stephan Beyer1e412292016-12-07 21:51:32570static void update_abort_safety_file(void)
571{
572 struct object_id head;
573
574 /* Do nothing on a single-pick */
575 if (!file_exists(git_path_seq_dir()))
576 return;
577
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 13:58:46578 if (!repo_get_oid(the_repository, "HEAD", &head))
Stephan Beyer1e412292016-12-07 21:51:32579 write_file(git_path_abort_safety_file(), "%s", oid_to_hex(&head));
580 else
581 write_file(git_path_abort_safety_file(), "%s", "");
582}
583
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:56584static int fast_forward_to(struct repository *r,
585 const struct object_id *to,
586 const struct object_id *from,
587 int unborn,
588 struct replay_opts *opts)
Ramkumar Ramachandra043a4492012-01-11 18:15:57589{
Ronnie Sahlbergd668d162014-04-16 22:37:45590 struct ref_transaction *transaction;
Ramkumar Ramachandraeb4be1c2013-06-19 07:37:09591 struct strbuf sb = STRBUF_INIT;
Ronnie Sahlbergd668d162014-04-16 22:37:45592 struct strbuf err = STRBUF_INIT;
Ramkumar Ramachandra043a4492012-01-11 18:15:57593
Nguyễn Thái Ngọc Duye1ff0a32019-01-12 02:13:26594 repo_read_index(r);
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:56595 if (checkout_fast_forward(r, from, to, 1))
Johannes Schindelin0e408fc2016-09-09 14:37:55596 return -1; /* the callee should have complained already */
Ronnie Sahlberg651ab9f2014-04-16 18:56:52597
Michael J Gruber5670e0e2022-08-18 13:13:26598 strbuf_addf(&sb, "%s: fast-forward", action_name(opts));
Ronnie Sahlbergd668d162014-04-16 22:37:45599
600 transaction = ref_transaction_begin(&err);
601 if (!transaction ||
602 ref_transaction_update(transaction, "HEAD",
Johannes Schindelind87d48b2018-05-03 23:01:17603 to, unborn && !is_rebase_i(opts) ?
brian m. carlson14228442021-04-26 01:02:56604 null_oid() : from,
Michael Haggerty1d147bd2015-02-17 17:00:15605 0, sb.buf, &err) ||
Ronnie Sahlbergdb7516a2014-04-30 19:22:42606 ref_transaction_commit(transaction, &err)) {
Ronnie Sahlbergd668d162014-04-16 22:37:45607 ref_transaction_free(transaction);
608 error("%s", err.buf);
609 strbuf_release(&sb);
610 strbuf_release(&err);
611 return -1;
612 }
Ronnie Sahlberg651ab9f2014-04-16 18:56:52613
Ramkumar Ramachandraeb4be1c2013-06-19 07:37:09614 strbuf_release(&sb);
Ronnie Sahlbergd668d162014-04-16 22:37:45615 strbuf_release(&err);
616 ref_transaction_free(transaction);
Stephan Beyer1e412292016-12-07 21:51:32617 update_abort_safety_file();
Ronnie Sahlbergd668d162014-04-16 22:37:45618 return 0;
Ramkumar Ramachandra043a4492012-01-11 18:15:57619}
620
Denton Liuf29cd862019-04-17 10:23:25621enum commit_msg_cleanup_mode get_cleanup_mode(const char *cleanup_arg,
622 int use_editor)
623{
624 if (!cleanup_arg || !strcmp(cleanup_arg, "default"))
625 return use_editor ? COMMIT_MSG_CLEANUP_ALL :
626 COMMIT_MSG_CLEANUP_SPACE;
627 else if (!strcmp(cleanup_arg, "verbatim"))
628 return COMMIT_MSG_CLEANUP_NONE;
629 else if (!strcmp(cleanup_arg, "whitespace"))
630 return COMMIT_MSG_CLEANUP_SPACE;
631 else if (!strcmp(cleanup_arg, "strip"))
632 return COMMIT_MSG_CLEANUP_ALL;
633 else if (!strcmp(cleanup_arg, "scissors"))
634 return use_editor ? COMMIT_MSG_CLEANUP_SCISSORS :
635 COMMIT_MSG_CLEANUP_SPACE;
636 else
637 die(_("Invalid cleanup mode %s"), cleanup_arg);
638}
639
Phillip Wooddc42e9a2019-04-17 10:23:29640/*
641 * NB using int rather than enum cleanup_mode to stop clang's
642 * -Wtautological-constant-out-of-range-compare complaining that the comparison
643 * is always true.
644 */
645static const char *describe_cleanup_mode(int cleanup_mode)
646{
647 static const char *modes[] = { "whitespace",
648 "verbatim",
649 "scissors",
650 "strip" };
651
652 if (cleanup_mode < ARRAY_SIZE(modes))
653 return modes[cleanup_mode];
654
655 BUG("invalid cleanup_mode provided (%d)", cleanup_mode);
656}
657
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:56658void append_conflicts_hint(struct index_state *istate,
Denton Liu1a2b9852019-04-17 10:23:30659 struct strbuf *msgbuf, enum commit_msg_cleanup_mode cleanup_mode)
Junio C Hamano75c961b2014-10-24 18:34:59660{
661 int i;
662
Denton Liu1a2b9852019-04-17 10:23:30663 if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) {
664 strbuf_addch(msgbuf, '\n');
665 wt_status_append_cut_line(msgbuf);
666 strbuf_addch(msgbuf, comment_line_char);
667 }
668
Junio C Hamano261f3152014-10-28 20:04:38669 strbuf_addch(msgbuf, '\n');
Calvin Wan787cb8a2023-06-06 19:48:43670 strbuf_commented_addf(msgbuf, comment_line_char, "Conflicts:\n");
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:56671 for (i = 0; i < istate->cache_nr;) {
672 const struct cache_entry *ce = istate->cache[i++];
Junio C Hamano75c961b2014-10-24 18:34:59673 if (ce_stage(ce)) {
Calvin Wan787cb8a2023-06-06 19:48:43674 strbuf_commented_addf(msgbuf, comment_line_char,
675 "\t%s\n", ce->name);
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:56676 while (i < istate->cache_nr &&
677 !strcmp(ce->name, istate->cache[i]->name))
Junio C Hamano75c961b2014-10-24 18:34:59678 i++;
679 }
680 }
681}
682
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:56683static int do_recursive_merge(struct repository *r,
684 struct commit *base, struct commit *next,
Ramkumar Ramachandra043a4492012-01-11 18:15:57685 const char *base_label, const char *next_label,
brian m. carlson48be4c62017-05-06 22:10:36686 struct object_id *head, struct strbuf *msgbuf,
Ramkumar Ramachandra043a4492012-01-11 18:15:57687 struct replay_opts *opts)
688{
689 struct merge_options o;
Elijah Newren14c45862020-11-02 23:45:34690 struct merge_result result;
Elijah Newrenb4db8a22019-08-17 18:41:30691 struct tree *next_tree, *base_tree, *head_tree;
Elijah Newren14c45862020-11-02 23:45:34692 int clean, show_output;
Jeff Kingd20bc012020-01-29 05:46:47693 int i;
Martin Ågren14bca6c2018-02-27 21:30:09694 struct lock_file index_lock = LOCK_INIT;
Ramkumar Ramachandra043a4492012-01-11 18:15:57695
Nguyễn Thái Ngọc Duy3a95f312019-01-12 02:13:24696 if (repo_hold_locked_index(r, &index_lock, LOCK_REPORT_ON_ERROR) < 0)
Phillip Woodbd588862017-11-15 10:41:25697 return -1;
Ramkumar Ramachandra043a4492012-01-11 18:15:57698
Nguyễn Thái Ngọc Duye1ff0a32019-01-12 02:13:26699 repo_read_index(r);
Ramkumar Ramachandra043a4492012-01-11 18:15:57700
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 02:13:29701 init_merge_options(&o, r);
Ramkumar Ramachandra043a4492012-01-11 18:15:57702 o.ancestor = base ? base_label : "(empty tree)";
703 o.branch1 = "HEAD";
704 o.branch2 = next ? next_label : "(empty tree)";
Johannes Schindelin62fdb652017-01-02 15:35:39705 if (is_rebase_i(opts))
706 o.buffer_output = 2;
Elijah Newren9268cf42017-11-13 20:16:00707 o.show_rename_progress = 1;
Ramkumar Ramachandra043a4492012-01-11 18:15:57708
709 head_tree = parse_tree_indirect(head);
Ævar Arnfjörð Bjarmason4a93b892023-03-28 13:58:58710 next_tree = next ? repo_get_commit_tree(r, next) : empty_tree(r);
711 base_tree = base ? repo_get_commit_tree(r, base) : empty_tree(r);
Ramkumar Ramachandra043a4492012-01-11 18:15:57712
Phillip Woodfb60b9f2023-04-10 09:08:28713 for (i = 0; i < opts->xopts.nr; i++)
714 parse_merge_opt(&o, opts->xopts.v[i]);
Ramkumar Ramachandra043a4492012-01-11 18:15:57715
Elijah Newren6a5fb962021-08-04 05:38:01716 if (!opts->strategy || !strcmp(opts->strategy, "ort")) {
Elijah Newren14c45862020-11-02 23:45:34717 memset(&result, 0, sizeof(result));
718 merge_incore_nonrecursive(&o, base_tree, head_tree, next_tree,
719 &result);
720 show_output = !is_rebase_i(opts) || !result.clean;
721 /*
722 * TODO: merge_switch_to_result will update index/working tree;
723 * we only really want to do that if !result.clean || this is
724 * the final patch to be picked. But determining this is the
725 * final patch would take some work, and "head_tree" would need
726 * to be replace with the tree the index matched before we
727 * started doing any picks.
728 */
729 merge_switch_to_result(&o, head_tree, &result, 1, show_output);
730 clean = result.clean;
731 } else {
Derrick Stolee5d9c9342021-09-08 11:24:00732 ensure_full_index(r->index);
Elijah Newren14c45862020-11-02 23:45:34733 clean = merge_trees(&o, head_tree, next_tree, base_tree);
734 if (is_rebase_i(opts) && clean <= 0)
735 fputs(o.obuf.buf, stdout);
736 strbuf_release(&o.obuf);
737 }
Martin Ågren64816522018-02-27 21:30:10738 if (clean < 0) {
739 rollback_lock_file(&index_lock);
Johannes Schindelinf241ff02016-07-26 16:06:02740 return clean;
Martin Ågren64816522018-02-27 21:30:10741 }
Ramkumar Ramachandra043a4492012-01-11 18:15:57742
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:56743 if (write_locked_index(r->index, &index_lock,
Martin Ågren61000812018-03-01 20:40:20744 COMMIT_LOCK | SKIP_IF_UNCHANGED))
Ævar Arnfjörð Bjarmason66f5f6d2017-05-11 21:20:12745 /*
746 * TRANSLATORS: %s will be "revert", "cherry-pick" or
Elijah Newrenc2417d32020-02-15 21:36:36747 * "rebase".
Johannes Schindelin84583952017-01-02 15:26:28748 */
Johannes Schindelinc527b552016-09-09 14:37:10749 return error(_("%s: Unable to write new index file"),
Johannes Schindelinc28cbc52016-10-21 12:26:17750 _(action_name(opts)));
Ramkumar Ramachandra043a4492012-01-11 18:15:57751
Junio C Hamano75c961b2014-10-24 18:34:59752 if (!clean)
Denton Liu1a2b9852019-04-17 10:23:30753 append_conflicts_hint(r->index, msgbuf,
754 opts->default_msg_cleanup);
Ramkumar Ramachandra043a4492012-01-11 18:15:57755
756 return !clean;
757}
758
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:56759static struct object_id *get_cache_tree_oid(struct index_state *istate)
Johannes Schindelinba97aea2018-05-03 23:01:15760{
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:56761 if (!cache_tree_fully_valid(istate->cache_tree))
762 if (cache_tree_update(istate, 0)) {
Johannes Schindelinba97aea2018-05-03 23:01:15763 error(_("unable to update cache tree"));
764 return NULL;
765 }
766
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:56767 return &istate->cache_tree->oid;
Johannes Schindelinba97aea2018-05-03 23:01:15768}
769
Nguyễn Thái Ngọc Duy005af332018-11-10 05:48:57770static int is_index_unchanged(struct repository *r)
Neil Hormanb27cfb02012-04-20 14:36:15771{
Johannes Schindelinba97aea2018-05-03 23:01:15772 struct object_id head_oid, *cache_tree_oid;
Neil Hormanb27cfb02012-04-20 14:36:15773 struct commit *head_commit;
Nguyễn Thái Ngọc Duy005af332018-11-10 05:48:57774 struct index_state *istate = r->index;
Neil Hormanb27cfb02012-04-20 14:36:15775
brian m. carlson49e61472017-10-15 22:07:09776 if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &head_oid, NULL))
Johannes Schindelinaee42e12017-12-22 23:55:43777 return error(_("could not resolve HEAD commit"));
Neil Hormanb27cfb02012-04-20 14:36:15778
Nguyễn Thái Ngọc Duy005af332018-11-10 05:48:57779 head_commit = lookup_commit(r, &head_oid);
Neil Horman4b580062012-05-03 12:10:22780
781 /*
782 * If head_commit is NULL, check_commit, called from
783 * lookup_commit, would have indicated that head_commit is not
Ævar Arnfjörð Bjarmasonc7c33f52023-03-28 13:58:57784 * a commit object already. repo_parse_commit() will return failure
Neil Horman4b580062012-05-03 12:10:22785 * without further complaints in such a case. Otherwise, if
Ævar Arnfjörð Bjarmasonc7c33f52023-03-28 13:58:57786 * the commit is invalid, repo_parse_commit() will complain. So
Neil Horman4b580062012-05-03 12:10:22787 * there is nothing for us to say here. Just return failure.
788 */
Ævar Arnfjörð Bjarmason4a93b892023-03-28 13:58:58789 if (repo_parse_commit(r, head_commit))
Neil Horman4b580062012-05-03 12:10:22790 return -1;
Neil Hormanb27cfb02012-04-20 14:36:15791
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:56792 if (!(cache_tree_oid = get_cache_tree_oid(istate)))
Johannes Schindelinba97aea2018-05-03 23:01:15793 return -1;
Neil Hormanb27cfb02012-04-20 14:36:15794
Jeff King4a7e27e2018-08-28 21:22:40795 return oideq(cache_tree_oid, get_commit_tree_oid(head_commit));
Neil Hormanb27cfb02012-04-20 14:36:15796}
797
Johannes Schindelin0473f282017-01-02 15:27:18798static int write_author_script(const char *message)
799{
800 struct strbuf buf = STRBUF_INIT;
801 const char *eol;
802 int res;
803
804 for (;;)
805 if (!*message || starts_with(message, "\n")) {
806missing_author:
807 /* Missing 'author' line? */
808 unlink(rebase_path_author_script());
809 return 0;
810 } else if (skip_prefix(message, "author ", &message))
811 break;
812 else if ((eol = strchr(message, '\n')))
813 message = eol + 1;
814 else
815 goto missing_author;
816
817 strbuf_addstr(&buf, "GIT_AUTHOR_NAME='");
818 while (*message && *message != '\n' && *message != '\r')
819 if (skip_prefix(message, " <", &message))
820 break;
821 else if (*message != '\'')
822 strbuf_addch(&buf, *(message++));
823 else
Phillip Wood4aa5ff92018-08-07 09:34:52824 strbuf_addf(&buf, "'\\%c'", *(message++));
Johannes Schindelin0473f282017-01-02 15:27:18825 strbuf_addstr(&buf, "'\nGIT_AUTHOR_EMAIL='");
826 while (*message && *message != '\n' && *message != '\r')
827 if (skip_prefix(message, "> ", &message))
828 break;
829 else if (*message != '\'')
830 strbuf_addch(&buf, *(message++));
831 else
Phillip Wood4aa5ff92018-08-07 09:34:52832 strbuf_addf(&buf, "'\\%c'", *(message++));
Johannes Schindelin0473f282017-01-02 15:27:18833 strbuf_addstr(&buf, "'\nGIT_AUTHOR_DATE='@");
834 while (*message && *message != '\n' && *message != '\r')
835 if (*message != '\'')
836 strbuf_addch(&buf, *(message++));
837 else
Phillip Wood4aa5ff92018-08-07 09:34:52838 strbuf_addf(&buf, "'\\%c'", *(message++));
Eric Sunshine0f16c092018-07-31 07:33:29839 strbuf_addch(&buf, '\'');
Johannes Schindelin0473f282017-01-02 15:27:18840 res = write_message(buf.buf, buf.len, rebase_path_author_script(), 1);
841 strbuf_release(&buf);
842 return res;
843}
844
Phillip Woodbcd33ec2018-10-31 10:15:55845/**
846 * Take a series of KEY='VALUE' lines where VALUE part is
847 * sq-quoted, and append <KEY, VALUE> at the end of the string list
Phillip Wood4aa5ff92018-08-07 09:34:52848 */
Phillip Woodbcd33ec2018-10-31 10:15:55849static int parse_key_value_squoted(char *buf, struct string_list *list)
Phillip Wood4aa5ff92018-08-07 09:34:52850{
Phillip Woodbcd33ec2018-10-31 10:15:55851 while (*buf) {
852 struct string_list_item *item;
853 char *np;
854 char *cp = strchr(buf, '=');
855 if (!cp) {
856 np = strchrnul(buf, '\n');
857 return error(_("no key present in '%.*s'"),
858 (int) (np - buf), buf);
859 }
860 np = strchrnul(cp, '\n');
861 *cp++ = '\0';
862 item = string_list_append(list, buf);
Phillip Wood4aa5ff92018-08-07 09:34:52863
Phillip Woodbcd33ec2018-10-31 10:15:55864 buf = np + (*np == '\n');
865 *np = '\0';
866 cp = sq_dequote(cp);
867 if (!cp)
868 return error(_("unable to dequote value of '%s'"),
869 item->string);
870 item->util = xstrdup(cp);
871 }
Phillip Wood4aa5ff92018-08-07 09:34:52872 return 0;
873}
874
Phillip Woodbcd33ec2018-10-31 10:15:55875/**
876 * Reads and parses the state directory's "author-script" file, and sets name,
877 * email and date accordingly.
878 * Returns 0 on success, -1 if the file could not be parsed.
879 *
880 * The author script is of the format:
881 *
882 * GIT_AUTHOR_NAME='$author_name'
883 * GIT_AUTHOR_EMAIL='$author_email'
884 * GIT_AUTHOR_DATE='$author_date'
885 *
886 * where $author_name, $author_email and $author_date are quoted. We are strict
Johannes Schindelinc3c003e2019-05-14 11:22:33887 * with our parsing, as the file was meant to be eval'd in the now-removed
Phillip Woodbcd33ec2018-10-31 10:15:55888 * git-am.sh/git-rebase--interactive.sh scripts, and thus if the file differs
889 * from what this function expects, it is better to bail out than to do
890 * something that the user does not expect.
891 */
892int read_author_script(const char *path, char **name, char **email, char **date,
893 int allow_missing)
894{
895 struct strbuf buf = STRBUF_INIT;
896 struct string_list kv = STRING_LIST_INIT_DUP;
897 int retval = -1; /* assume failure */
898 int i, name_i = -2, email_i = -2, date_i = -2, err = 0;
899
900 if (strbuf_read_file(&buf, path, 256) <= 0) {
901 strbuf_release(&buf);
902 if (errno == ENOENT && allow_missing)
903 return 0;
904 else
905 return error_errno(_("could not open '%s' for reading"),
906 path);
907 }
908
909 if (parse_key_value_squoted(buf.buf, &kv))
910 goto finish;
911
912 for (i = 0; i < kv.nr; i++) {
913 if (!strcmp(kv.items[i].string, "GIT_AUTHOR_NAME")) {
914 if (name_i != -2)
915 name_i = error(_("'GIT_AUTHOR_NAME' already given"));
916 else
917 name_i = i;
918 } else if (!strcmp(kv.items[i].string, "GIT_AUTHOR_EMAIL")) {
919 if (email_i != -2)
920 email_i = error(_("'GIT_AUTHOR_EMAIL' already given"));
921 else
922 email_i = i;
923 } else if (!strcmp(kv.items[i].string, "GIT_AUTHOR_DATE")) {
924 if (date_i != -2)
925 date_i = error(_("'GIT_AUTHOR_DATE' already given"));
926 else
927 date_i = i;
928 } else {
929 err = error(_("unknown variable '%s'"),
930 kv.items[i].string);
931 }
932 }
933 if (name_i == -2)
934 error(_("missing 'GIT_AUTHOR_NAME'"));
935 if (email_i == -2)
936 error(_("missing 'GIT_AUTHOR_EMAIL'"));
937 if (date_i == -2)
938 error(_("missing 'GIT_AUTHOR_DATE'"));
Jeff King45350aeb2022-10-03 17:35:02939 if (name_i < 0 || email_i < 0 || date_i < 0 || err)
Phillip Woodbcd33ec2018-10-31 10:15:55940 goto finish;
Junio C Hamano4d924522020-01-12 20:27:41941 *name = kv.items[name_i].util;
942 *email = kv.items[email_i].util;
943 *date = kv.items[date_i].util;
Phillip Woodbcd33ec2018-10-31 10:15:55944 retval = 0;
945finish:
946 string_list_clear(&kv, !!retval);
947 strbuf_release(&buf);
948 return retval;
949}
Neil Horman4b580062012-05-03 12:10:22950
Ramkumar Ramachandra043a4492012-01-11 18:15:57951/*
Phillip Wood4d010a72018-10-31 10:15:56952 * Read a GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL AND GIT_AUTHOR_DATE from a
Jeff Kingc972bf42020-07-28 20:25:12953 * file with shell quoting into struct strvec. Returns -1 on
Phillip Wood4d010a72018-10-31 10:15:56954 * error, 0 otherwise.
Johannes Schindelinb5a67042016-10-21 12:25:04955 */
Jeff Kingc972bf42020-07-28 20:25:12956static int read_env_script(struct strvec *env)
Johannes Schindelinb5a67042016-10-21 12:25:04957{
Phillip Wood4d010a72018-10-31 10:15:56958 char *name, *email, *date;
Johannes Schindelinb5a67042016-10-21 12:25:04959
Phillip Wood4d010a72018-10-31 10:15:56960 if (read_author_script(rebase_path_author_script(),
961 &name, &email, &date, 0))
Johannes Schindelina2a20b02017-01-02 15:35:25962 return -1;
Johannes Schindelinb5a67042016-10-21 12:25:04963
Jeff Kingc972bf42020-07-28 20:25:12964 strvec_pushf(env, "GIT_AUTHOR_NAME=%s", name);
965 strvec_pushf(env, "GIT_AUTHOR_EMAIL=%s", email);
966 strvec_pushf(env, "GIT_AUTHOR_DATE=%s", date);
Phillip Wood4d010a72018-10-31 10:15:56967 free(name);
968 free(email);
969 free(date);
Johannes Schindelinb5a67042016-10-21 12:25:04970
Johannes Schindelina2a20b02017-01-02 15:35:25971 return 0;
Johannes Schindelinb5a67042016-10-21 12:25:04972}
973
Phillip Wood356ee462017-11-24 11:07:57974static char *get_author(const char *message)
975{
976 size_t len;
977 const char *a;
978
979 a = find_commit_header(message, "author", &len);
980 if (a)
981 return xmemdupz(a, len);
982
983 return NULL;
984}
985
Ævar Arnfjörð Bjarmasonb3193252022-06-02 09:09:51986static const char *author_date_from_env(const struct strvec *env)
Phillip Wood7573cec2020-08-17 17:40:02987{
988 int i;
989 const char *date;
990
Junio C Hamano9c31b192020-09-03 19:37:01991 for (i = 0; i < env->nr; i++)
992 if (skip_prefix(env->v[i],
Phillip Wood7573cec2020-08-17 17:40:02993 "GIT_AUTHOR_DATE=", &date))
994 return date;
995 /*
996 * If GIT_AUTHOR_DATE is missing we should have already errored out when
997 * reading the script
998 */
999 BUG("GIT_AUTHOR_DATE missing from author script");
1000}
1001
Johannes Schindelin791eb872016-10-21 12:26:321002static const char staged_changes_advice[] =
1003N_("you have staged changes in your working tree\n"
1004"If these changes are meant to be squashed into the previous commit, run:\n"
1005"\n"
1006" git commit --amend %s\n"
1007"\n"
1008"If they are meant to go into a new commit, run:\n"
1009"\n"
1010" git commit %s\n"
1011"\n"
1012"In both cases, once you're done, continue with:\n"
1013"\n"
1014" git rebase --continue\n");
1015
Johannes Schindelin789b3ef2017-03-23 16:07:111016#define ALLOW_EMPTY (1<<0)
1017#define EDIT_MSG (1<<1)
1018#define AMEND_MSG (1<<2)
1019#define CLEANUP_MSG (1<<3)
Johannes Schindelinb92ff6e2017-03-23 16:07:171020#define VERIFY_MSG (1<<4)
Johannes Schindelind87d48b2018-05-03 23:01:171021#define CREATE_ROOT_COMMIT (1<<5)
Johannes Schindelinf7d42ce2021-01-28 16:16:421022#define VERBATIM_MSG (1<<6)
Johannes Schindelin789b3ef2017-03-23 16:07:111023
Alban Gruin34bec2c2018-08-10 16:51:321024static int run_command_silent_on_success(struct child_process *cmd)
1025{
1026 struct strbuf buf = STRBUF_INIT;
1027 int rc;
1028
1029 cmd->stdout_to_stderr = 1;
1030 rc = pipe_command(cmd,
1031 NULL, 0,
1032 NULL, 0,
1033 &buf, 0);
1034
1035 if (rc)
1036 fputs(buf.buf, stderr);
1037 strbuf_release(&buf);
1038 return rc;
1039}
1040
Johannes Schindelinb5a67042016-10-21 12:25:041041/*
Ramkumar Ramachandra043a4492012-01-11 18:15:571042 * If we are cherry-pick, and if the merge did not result in
1043 * hand-editing, we will hit this commit and inherit the original
1044 * author date and name.
Johannes Schindelinb5a67042016-10-21 12:25:041045 *
Ramkumar Ramachandra043a4492012-01-11 18:15:571046 * If we are revert, or if our cherry-pick results in a hand merge,
1047 * we had better say that the current user is responsible for that.
Johannes Schindelinb5a67042016-10-21 12:25:041048 *
1049 * An exception is when run_git_commit() is called during an
1050 * interactive rebase: in that case, we will want to retain the
1051 * author metadata.
Ramkumar Ramachandra043a4492012-01-11 18:15:571052 */
Jeff King20f4b042020-09-30 12:29:311053static int run_git_commit(const char *defmsg,
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:561054 struct replay_opts *opts,
Johannes Schindelin789b3ef2017-03-23 16:07:111055 unsigned int flags)
Ramkumar Ramachandra043a4492012-01-11 18:15:571056{
Johannes Schindelin07d968e2017-01-02 15:35:291057 struct child_process cmd = CHILD_PROCESS_INIT;
Ramkumar Ramachandra043a4492012-01-11 18:15:571058
Johannes Schindelinf7d42ce2021-01-28 16:16:421059 if ((flags & CLEANUP_MSG) && (flags & VERBATIM_MSG))
1060 BUG("CLEANUP_MSG and VERBATIM_MSG are mutually exclusive");
1061
Johannes Schindelin07d968e2017-01-02 15:35:291062 cmd.git_cmd = 1;
1063
Phillip Wood9d6b9df2021-11-02 21:30:511064 if (is_rebase_i(opts) &&
1065 ((opts->committer_date_is_author_date && !opts->ignore_date) ||
1066 !(!defmsg && (flags & AMEND_MSG))) &&
Ævar Arnfjörð Bjarmason29fda242022-06-02 09:09:501067 read_env_script(&cmd.env)) {
Alban Gruin34bec2c2018-08-10 16:51:321068 const char *gpg_opt = gpg_sign_opt_quoted(opts);
Johannes Schindelin9a757c42017-01-02 15:35:341069
Alban Gruin34bec2c2018-08-10 16:51:321070 return error(_(staged_changes_advice),
1071 gpg_opt, gpg_opt);
Johannes Schindelinb5a67042016-10-21 12:25:041072 }
1073
Phillip Woodd188a602022-11-09 14:21:571074 strvec_pushf(&cmd.env, GIT_REFLOG_ACTION "=%s", opts->reflog_message);
1075
Phillip Wood7573cec2020-08-17 17:40:021076 if (opts->committer_date_is_author_date)
Ævar Arnfjörð Bjarmason29fda242022-06-02 09:09:501077 strvec_pushf(&cmd.env, "GIT_COMMITTER_DATE=%s",
Junio C Hamano9c31b192020-09-03 19:37:011078 opts->ignore_date ?
1079 "" :
Ævar Arnfjörð Bjarmasonb3193252022-06-02 09:09:511080 author_date_from_env(&cmd.env));
Phillip Wooda3894aa2020-08-17 17:40:031081 if (opts->ignore_date)
Ævar Arnfjörð Bjarmason29fda242022-06-02 09:09:501082 strvec_push(&cmd.env, "GIT_AUTHOR_DATE=");
Phillip Wood7573cec2020-08-17 17:40:021083
Jeff Kingc972bf42020-07-28 20:25:121084 strvec_push(&cmd.args, "commit");
Neil Hormanb27cfb02012-04-20 14:36:151085
Johannes Schindelinb92ff6e2017-03-23 16:07:171086 if (!(flags & VERIFY_MSG))
Jeff Kingc972bf42020-07-28 20:25:121087 strvec_push(&cmd.args, "-n");
Johannes Schindelin789b3ef2017-03-23 16:07:111088 if ((flags & AMEND_MSG))
Jeff Kingc972bf42020-07-28 20:25:121089 strvec_push(&cmd.args, "--amend");
Jeff King3bdd5522014-06-19 21:28:201090 if (opts->gpg_sign)
Jeff Kingc972bf42020-07-28 20:25:121091 strvec_pushf(&cmd.args, "-S%s", opts->gpg_sign);
Đoàn Trần Công Danhcf0ad4d2020-04-03 10:28:031092 else
Jeff Kingc972bf42020-07-28 20:25:121093 strvec_push(&cmd.args, "--no-gpg-sign");
Johannes Schindelinb5a67042016-10-21 12:25:041094 if (defmsg)
Jeff Kingc972bf42020-07-28 20:25:121095 strvec_pushl(&cmd.args, "-F", defmsg, NULL);
Johannes Schindelindc4b5bc2018-04-27 20:48:281096 else if (!(flags & EDIT_MSG))
Jeff Kingc972bf42020-07-28 20:25:121097 strvec_pushl(&cmd.args, "-C", "HEAD", NULL);
Johannes Schindelin789b3ef2017-03-23 16:07:111098 if ((flags & CLEANUP_MSG))
Jeff Kingc972bf42020-07-28 20:25:121099 strvec_push(&cmd.args, "--cleanup=strip");
Johannes Schindelinf7d42ce2021-01-28 16:16:421100 if ((flags & VERBATIM_MSG))
1101 strvec_push(&cmd.args, "--cleanup=verbatim");
Johannes Schindelin789b3ef2017-03-23 16:07:111102 if ((flags & EDIT_MSG))
Jeff Kingc972bf42020-07-28 20:25:121103 strvec_push(&cmd.args, "-e");
Johannes Schindelin789b3ef2017-03-23 16:07:111104 else if (!(flags & CLEANUP_MSG) &&
Johannes Schindelin00094262016-10-21 12:25:281105 !opts->signoff && !opts->record_origin &&
Denton Liu1a2b9852019-04-17 10:23:301106 !opts->explicit_cleanup)
Jeff Kingc972bf42020-07-28 20:25:121107 strvec_push(&cmd.args, "--cleanup=verbatim");
Ramkumar Ramachandra043a4492012-01-11 18:15:571108
Johannes Schindelin789b3ef2017-03-23 16:07:111109 if ((flags & ALLOW_EMPTY))
Jeff Kingc972bf42020-07-28 20:25:121110 strvec_push(&cmd.args, "--allow-empty");
Neil Hormandf478b72012-04-11 20:21:531111
Elijah Newrena3ec9ea2018-09-12 21:18:481112 if (!(flags & EDIT_MSG))
Jeff Kingc972bf42020-07-28 20:25:121113 strvec_push(&cmd.args, "--allow-empty-message");
Chris Webb4bee9582012-08-02 10:38:511114
Alban Gruin34bec2c2018-08-10 16:51:321115 if (is_rebase_i(opts) && !(flags & EDIT_MSG))
1116 return run_command_silent_on_success(&cmd);
1117 else
1118 return run_command(&cmd);
Neil Hormanb27cfb02012-04-20 14:36:151119}
Ramkumar Ramachandra043a4492012-01-11 18:15:571120
Phillip Woodd0aaa462017-11-10 11:09:421121static int rest_is_empty(const struct strbuf *sb, int start)
1122{
1123 int i, eol;
1124 const char *nl;
1125
1126 /* Check if the rest is just whitespace and Signed-off-by's. */
1127 for (i = start; i < sb->len; i++) {
1128 nl = memchr(sb->buf + i, '\n', sb->len - i);
1129 if (nl)
1130 eol = nl - sb->buf;
1131 else
1132 eol = sb->len;
1133
1134 if (strlen(sign_off_header) <= eol - i &&
1135 starts_with(sb->buf + i, sign_off_header)) {
1136 i = eol;
1137 continue;
1138 }
1139 while (i < eol)
1140 if (!isspace(sb->buf[i++]))
1141 return 0;
1142 }
1143
1144 return 1;
1145}
1146
Denton Liuf29cd862019-04-17 10:23:251147void cleanup_message(struct strbuf *msgbuf,
1148 enum commit_msg_cleanup_mode cleanup_mode, int verbose)
1149{
1150 if (verbose || /* Truncate the message just before the diff, if any. */
1151 cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS)
1152 strbuf_setlen(msgbuf, wt_status_locate_end(msgbuf->buf, msgbuf->len));
1153 if (cleanup_mode != COMMIT_MSG_CLEANUP_NONE)
Calvin Wan787cb8a2023-06-06 19:48:431154 strbuf_stripspace(msgbuf,
1155 cleanup_mode == COMMIT_MSG_CLEANUP_ALL ? comment_line_char : '\0');
Denton Liuf29cd862019-04-17 10:23:251156}
1157
Phillip Woodd0aaa462017-11-10 11:09:421158/*
1159 * Find out if the message in the strbuf contains only whitespace and
1160 * Signed-off-by lines.
1161 */
1162int message_is_empty(const struct strbuf *sb,
1163 enum commit_msg_cleanup_mode cleanup_mode)
1164{
1165 if (cleanup_mode == COMMIT_MSG_CLEANUP_NONE && sb->len)
1166 return 0;
1167 return rest_is_empty(sb, 0);
1168}
1169
1170/*
1171 * See if the user edited the message in the editor or left what
1172 * was in the template intact
1173 */
1174int template_untouched(const struct strbuf *sb, const char *template_file,
1175 enum commit_msg_cleanup_mode cleanup_mode)
1176{
1177 struct strbuf tmpl = STRBUF_INIT;
1178 const char *start;
1179
1180 if (cleanup_mode == COMMIT_MSG_CLEANUP_NONE && sb->len)
1181 return 0;
1182
1183 if (!template_file || strbuf_read_file(&tmpl, template_file, 0) <= 0)
1184 return 0;
1185
Calvin Wan787cb8a2023-06-06 19:48:431186 strbuf_stripspace(&tmpl,
1187 cleanup_mode == COMMIT_MSG_CLEANUP_ALL ? comment_line_char : '\0');
Phillip Woodd0aaa462017-11-10 11:09:421188 if (!skip_prefix(sb->buf, tmpl.buf, &start))
1189 start = sb->buf;
1190 strbuf_release(&tmpl);
1191 return rest_is_empty(sb, start - sb->buf);
1192}
1193
Phillip Wood0505d602017-11-17 11:34:471194int update_head_with_reflog(const struct commit *old_head,
1195 const struct object_id *new_head,
1196 const char *action, const struct strbuf *msg,
1197 struct strbuf *err)
1198{
1199 struct ref_transaction *transaction;
1200 struct strbuf sb = STRBUF_INIT;
1201 const char *nl;
1202 int ret = 0;
1203
1204 if (action) {
1205 strbuf_addstr(&sb, action);
1206 strbuf_addstr(&sb, ": ");
1207 }
1208
1209 nl = strchr(msg->buf, '\n');
1210 if (nl) {
1211 strbuf_add(&sb, msg->buf, nl + 1 - msg->buf);
1212 } else {
1213 strbuf_addbuf(&sb, msg);
1214 strbuf_addch(&sb, '\n');
1215 }
1216
1217 transaction = ref_transaction_begin(err);
1218 if (!transaction ||
1219 ref_transaction_update(transaction, "HEAD", new_head,
brian m. carlson14228442021-04-26 01:02:561220 old_head ? &old_head->object.oid : null_oid(),
Phillip Wood0505d602017-11-17 11:34:471221 0, sb.buf, err) ||
1222 ref_transaction_commit(transaction, err)) {
1223 ret = -1;
1224 }
1225 ref_transaction_free(transaction);
1226 strbuf_release(&sb);
1227
1228 return ret;
1229}
1230
Phillip Wooda87a6f32017-11-17 11:34:481231static int run_rewrite_hook(const struct object_id *oldoid,
1232 const struct object_id *newoid)
1233{
1234 struct child_process proc = CHILD_PROCESS_INIT;
Phillip Wooda87a6f32017-11-17 11:34:481235 int code;
1236 struct strbuf sb = STRBUF_INIT;
Ævar Arnfjörð Bjarmason2b709892021-11-25 22:52:201237 const char *hook_path = find_hook("post-rewrite");
Phillip Wooda87a6f32017-11-17 11:34:481238
Ævar Arnfjörð Bjarmason2b709892021-11-25 22:52:201239 if (!hook_path)
Phillip Wooda87a6f32017-11-17 11:34:481240 return 0;
1241
Ævar Arnfjörð Bjarmason2b709892021-11-25 22:52:201242 strvec_pushl(&proc.args, hook_path, "amend", NULL);
Phillip Wooda87a6f32017-11-17 11:34:481243 proc.in = -1;
1244 proc.stdout_to_stderr = 1;
Jeff Hostetler62062862019-02-22 22:25:061245 proc.trace2_hook_name = "post-rewrite";
Phillip Wooda87a6f32017-11-17 11:34:481246
1247 code = start_command(&proc);
1248 if (code)
1249 return code;
1250 strbuf_addf(&sb, "%s %s\n", oid_to_hex(oldoid), oid_to_hex(newoid));
1251 sigchain_push(SIGPIPE, SIG_IGN);
1252 write_in_full(proc.in, sb.buf, sb.len);
1253 close(proc.in);
1254 strbuf_release(&sb);
1255 sigchain_pop(SIGPIPE);
1256 return finish_command(&proc);
1257}
1258
Nguyễn Thái Ngọc Duy1d18d752019-01-12 02:13:231259void commit_post_rewrite(struct repository *r,
1260 const struct commit *old_head,
Phillip Wooda87a6f32017-11-17 11:34:481261 const struct object_id *new_head)
1262{
1263 struct notes_rewrite_cfg *cfg;
1264
1265 cfg = init_copy_notes_for_rewrite("amend");
1266 if (cfg) {
1267 /* we are amending, so old_head is not NULL */
1268 copy_note_for_rewrite(cfg, &old_head->object.oid, new_head);
Nguyễn Thái Ngọc Duy1d18d752019-01-12 02:13:231269 finish_copy_notes_for_rewrite(r, cfg, "Notes added by 'git commit --amend'");
Phillip Wooda87a6f32017-11-17 11:34:481270 }
1271 run_rewrite_hook(&old_head->object.oid, new_head);
1272}
1273
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:561274static int run_prepare_commit_msg_hook(struct repository *r,
1275 struct strbuf *msg,
1276 const char *commit)
Phillip Wood66618a52018-01-24 12:34:221277{
Phillip Wood49697cb2019-10-15 10:25:311278 int ret = 0;
1279 const char *name, *arg1 = NULL, *arg2 = NULL;
Phillip Wood66618a52018-01-24 12:34:221280
1281 name = git_path_commit_editmsg();
1282 if (write_message(msg->buf, msg->len, name, 0))
1283 return -1;
1284
Phillip Wood49697cb2019-10-15 10:25:311285 if (commit) {
1286 arg1 = "commit";
1287 arg2 = commit;
1288 } else {
1289 arg1 = "message";
1290 }
Ævar Arnfjörð Bjarmasona8cc5942022-03-07 12:33:461291 if (run_commit_hook(0, r->index_file, NULL, "prepare-commit-msg", name,
Phillip Wood49697cb2019-10-15 10:25:311292 arg1, arg2, NULL))
Phillip Wood66618a52018-01-24 12:34:221293 ret = error(_("'prepare-commit-msg' hook failed"));
Phillip Wood66618a52018-01-24 12:34:221294
1295 return ret;
1296}
1297
Phillip Woode47c6ca2017-11-24 11:07:541298static const char implicit_ident_advice_noconfig[] =
1299N_("Your name and email address were configured automatically based\n"
1300"on your username and hostname. Please check that they are accurate.\n"
1301"You can suppress this message by setting them explicitly. Run the\n"
1302"following command and follow the instructions in your editor to edit\n"
1303"your configuration file:\n"
1304"\n"
1305" git config --global --edit\n"
1306"\n"
1307"After doing this, you may fix the identity used for this commit with:\n"
1308"\n"
1309" git commit --amend --reset-author\n");
1310
1311static const char implicit_ident_advice_config[] =
1312N_("Your name and email address were configured automatically based\n"
1313"on your username and hostname. Please check that they are accurate.\n"
1314"You can suppress this message by setting them explicitly:\n"
1315"\n"
1316" git config --global user.name \"Your Name\"\n"
1317" git config --global user.email you@example.com\n"
1318"\n"
1319"After doing this, you may fix the identity used for this commit with:\n"
1320"\n"
1321" git commit --amend --reset-author\n");
1322
1323static const char *implicit_ident_advice(void)
1324{
Johannes Schindelina03b0972021-07-24 22:06:521325 char *user_config = interpolate_path("~/.gitconfig", 0);
Phillip Woode47c6ca2017-11-24 11:07:541326 char *xdg_config = xdg_config_home("config");
1327 int config_exists = file_exists(user_config) || file_exists(xdg_config);
1328
1329 free(user_config);
1330 free(xdg_config);
1331
1332 if (config_exists)
1333 return _(implicit_ident_advice_config);
1334 else
1335 return _(implicit_ident_advice_noconfig);
1336
1337}
1338
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:561339void print_commit_summary(struct repository *r,
1340 const char *prefix,
1341 const struct object_id *oid,
Phillip Woode47c6ca2017-11-24 11:07:541342 unsigned int flags)
1343{
1344 struct rev_info rev;
1345 struct commit *commit;
1346 struct strbuf format = STRBUF_INIT;
1347 const char *head;
1348 struct pretty_print_context pctx = {0};
1349 struct strbuf author_ident = STRBUF_INIT;
1350 struct strbuf committer_ident = STRBUF_INIT;
Ævar Arnfjörð Bjarmasoned90f042021-10-16 09:39:231351 struct ref_store *refs;
Phillip Woode47c6ca2017-11-24 11:07:541352
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:561353 commit = lookup_commit(r, oid);
Phillip Woode47c6ca2017-11-24 11:07:541354 if (!commit)
1355 die(_("couldn't look up newly created commit"));
Ævar Arnfjörð Bjarmason4a93b892023-03-28 13:58:581356 if (repo_parse_commit(r, commit))
Phillip Woode47c6ca2017-11-24 11:07:541357 die(_("could not parse newly created commit"));
1358
1359 strbuf_addstr(&format, "format:%h] %s");
1360
Ævar Arnfjörð Bjarmason4a93b892023-03-28 13:58:581361 repo_format_commit_message(r, commit, "%an <%ae>", &author_ident,
1362 &pctx);
1363 repo_format_commit_message(r, commit, "%cn <%ce>", &committer_ident,
1364 &pctx);
Phillip Woode47c6ca2017-11-24 11:07:541365 if (strbuf_cmp(&author_ident, &committer_ident)) {
1366 strbuf_addstr(&format, "\n Author: ");
1367 strbuf_addbuf_percentquote(&format, &author_ident);
1368 }
1369 if (flags & SUMMARY_SHOW_AUTHOR_DATE) {
1370 struct strbuf date = STRBUF_INIT;
1371
Ævar Arnfjörð Bjarmason4a93b892023-03-28 13:58:581372 repo_format_commit_message(r, commit, "%ad", &date, &pctx);
Phillip Woode47c6ca2017-11-24 11:07:541373 strbuf_addstr(&format, "\n Date: ");
1374 strbuf_addbuf_percentquote(&format, &date);
1375 strbuf_release(&date);
1376 }
1377 if (!committer_ident_sufficiently_given()) {
1378 strbuf_addstr(&format, "\n Committer: ");
1379 strbuf_addbuf_percentquote(&format, &committer_ident);
Ben Boeckeled9bff02021-08-23 10:44:001380 if (advice_enabled(ADVICE_IMPLICIT_IDENTITY)) {
Phillip Woode47c6ca2017-11-24 11:07:541381 strbuf_addch(&format, '\n');
1382 strbuf_addstr(&format, implicit_ident_advice());
1383 }
1384 }
1385 strbuf_release(&author_ident);
1386 strbuf_release(&committer_ident);
1387
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:561388 repo_init_revisions(r, &rev, prefix);
Phillip Woode47c6ca2017-11-24 11:07:541389 setup_revisions(0, NULL, &rev, NULL);
1390
1391 rev.diff = 1;
1392 rev.diffopt.output_format =
1393 DIFF_FORMAT_SHORTSTAT | DIFF_FORMAT_SUMMARY;
1394
1395 rev.verbose_header = 1;
1396 rev.show_root_diff = 1;
1397 get_commit_format(format.buf, &rev);
1398 rev.always_show_header = 0;
Junio C Hamano0f57f732018-02-13 21:39:151399 rev.diffopt.detect_rename = DIFF_DETECT_RENAME;
Phillip Woode47c6ca2017-11-24 11:07:541400 diff_setup_done(&rev.diffopt);
1401
Ævar Arnfjörð Bjarmason4a93b892023-03-28 13:58:581402 refs = get_main_ref_store(r);
Ævar Arnfjörð Bjarmasonce14de02022-01-26 14:37:011403 head = refs_resolve_ref_unsafe(refs, "HEAD", 0, NULL, NULL);
Ævar Arnfjörð Bjarmason09444e72022-01-26 14:37:001404 if (!head)
1405 die(_("unable to resolve HEAD after creating commit"));
Phillip Woode47c6ca2017-11-24 11:07:541406 if (!strcmp(head, "HEAD"))
1407 head = _("detached HEAD");
1408 else
1409 skip_prefix(head, "refs/heads/", &head);
1410 printf("[%s%s ", head, (flags & SUMMARY_INITIAL_COMMIT) ?
1411 _(" (root-commit)") : "");
1412
1413 if (!log_tree_commit(&rev, commit)) {
1414 rev.always_show_header = 1;
1415 rev.use_terminator = 1;
1416 log_tree_commit(&rev, commit);
1417 }
1418
Ævar Arnfjörð Bjarmason2108fe42022-04-13 20:01:361419 release_revisions(&rev);
Phillip Woode47c6ca2017-11-24 11:07:541420 strbuf_release(&format);
1421}
1422
Nguyễn Thái Ngọc Duy005af332018-11-10 05:48:571423static int parse_head(struct repository *r, struct commit **head)
Phillip Wood356ee462017-11-24 11:07:571424{
1425 struct commit *current_head;
1426 struct object_id oid;
1427
Ævar Arnfjörð Bjarmason4a93b892023-03-28 13:58:581428 if (repo_get_oid(r, "HEAD", &oid)) {
Phillip Wood356ee462017-11-24 11:07:571429 current_head = NULL;
1430 } else {
Nguyễn Thái Ngọc Duy005af332018-11-10 05:48:571431 current_head = lookup_commit_reference(r, &oid);
Phillip Wood356ee462017-11-24 11:07:571432 if (!current_head)
1433 return error(_("could not parse HEAD"));
Jeff King9001dc22018-08-28 21:22:481434 if (!oideq(&oid, &current_head->object.oid)) {
Phillip Wood356ee462017-11-24 11:07:571435 warning(_("HEAD %s is not a commit!"),
1436 oid_to_hex(&oid));
1437 }
Ævar Arnfjörð Bjarmason4a93b892023-03-28 13:58:581438 if (repo_parse_commit(r, current_head))
Phillip Wood356ee462017-11-24 11:07:571439 return error(_("could not parse HEAD commit"));
1440 }
1441 *head = current_head;
1442
1443 return 0;
1444}
1445
1446/*
1447 * Try to commit without forking 'git commit'. In some cases we need
1448 * to run 'git commit' to display an error message
1449 *
1450 * Returns:
1451 * -1 - error unable to commit
1452 * 0 - success
1453 * 1 - run 'git commit'
1454 */
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:561455static int try_to_commit(struct repository *r,
1456 struct strbuf *msg, const char *author,
Phillip Wood356ee462017-11-24 11:07:571457 struct replay_opts *opts, unsigned int flags,
1458 struct object_id *oid)
1459{
1460 struct object_id tree;
Phillip Woodb0a31862019-08-19 09:18:231461 struct commit *current_head = NULL;
Phillip Wood356ee462017-11-24 11:07:571462 struct commit_list *parents = NULL;
1463 struct commit_extra_header *extra = NULL;
1464 struct strbuf err = STRBUF_INIT;
Phillip Wood66618a52018-01-24 12:34:221465 struct strbuf commit_msg = STRBUF_INIT;
Junio C Hamano4d924522020-01-12 20:27:411466 char *amend_author = NULL;
Phillip Wood7573cec2020-08-17 17:40:021467 const char *committer = NULL;
Phillip Wood66618a52018-01-24 12:34:221468 const char *hook_commit = NULL;
Phillip Wood356ee462017-11-24 11:07:571469 enum commit_msg_cleanup_mode cleanup;
1470 int res = 0;
1471
Johannes Schindelinf7d42ce2021-01-28 16:16:421472 if ((flags & CLEANUP_MSG) && (flags & VERBATIM_MSG))
1473 BUG("CLEANUP_MSG and VERBATIM_MSG are mutually exclusive");
1474
Nguyễn Thái Ngọc Duy005af332018-11-10 05:48:571475 if (parse_head(r, &current_head))
Phillip Wood356ee462017-11-24 11:07:571476 return -1;
Junio C Hamano4d924522020-01-12 20:27:411477
Phillip Wood356ee462017-11-24 11:07:571478 if (flags & AMEND_MSG) {
brian m. carlson42d4e1d2020-02-22 20:17:421479 const char *exclude_gpgsig[] = { "gpgsig", "gpgsig-sha256", NULL };
Phillip Wood356ee462017-11-24 11:07:571480 const char *out_enc = get_commit_output_encoding();
Ævar Arnfjörð Bjarmason4a93b892023-03-28 13:58:581481 const char *message = repo_logmsg_reencode(r, current_head,
1482 NULL, out_enc);
Phillip Wood356ee462017-11-24 11:07:571483
1484 if (!msg) {
1485 const char *orig_message = NULL;
1486
1487 find_commit_subject(message, &orig_message);
Phillip Wood66618a52018-01-24 12:34:221488 msg = &commit_msg;
Phillip Wood356ee462017-11-24 11:07:571489 strbuf_addstr(msg, orig_message);
Phillip Wood66618a52018-01-24 12:34:221490 hook_commit = "HEAD";
Phillip Wood356ee462017-11-24 11:07:571491 }
Junio C Hamano4d924522020-01-12 20:27:411492 author = amend_author = get_author(message);
Ævar Arnfjörð Bjarmason4a93b892023-03-28 13:58:581493 repo_unuse_commit_buffer(r, current_head,
Ævar Arnfjörð Bjarmasonecb50912023-03-28 13:58:481494 message);
Phillip Wood356ee462017-11-24 11:07:571495 if (!author) {
1496 res = error(_("unable to parse commit author"));
1497 goto out;
1498 }
1499 parents = copy_commit_list(current_head->parents);
1500 extra = read_commit_extra_headers(current_head, exclude_gpgsig);
Phillip Woodb0a31862019-08-19 09:18:231501 } else if (current_head &&
1502 (!(flags & CREATE_ROOT_COMMIT) || (flags & AMEND_MSG))) {
Phillip Wood356ee462017-11-24 11:07:571503 commit_list_insert(current_head, &parents);
1504 }
1505
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:561506 if (write_index_as_tree(&tree, r->index, r->index_file, 0, NULL)) {
Phillip Wood356ee462017-11-24 11:07:571507 res = error(_("git write-tree failed to write a tree"));
1508 goto out;
1509 }
1510
Phillip Wood2d05ef22019-11-22 19:43:031511 if (!(flags & ALLOW_EMPTY)) {
1512 struct commit *first_parent = current_head;
1513
1514 if (flags & AMEND_MSG) {
1515 if (current_head->parents) {
1516 first_parent = current_head->parents->item;
1517 if (repo_parse_commit(r, first_parent)) {
1518 res = error(_("could not parse HEAD commit"));
1519 goto out;
1520 }
1521 } else {
1522 first_parent = NULL;
1523 }
1524 }
1525 if (oideq(first_parent
1526 ? get_commit_tree_oid(first_parent)
1527 : the_hash_algo->empty_tree,
1528 &tree)) {
1529 res = 1; /* run 'git commit' to display error message */
1530 goto out;
1531 }
Phillip Wood356ee462017-11-24 11:07:571532 }
1533
Ævar Arnfjörð Bjarmason07a348e2021-09-26 19:03:281534 if (hook_exists("prepare-commit-msg")) {
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:561535 res = run_prepare_commit_msg_hook(r, msg, hook_commit);
Phillip Wood66618a52018-01-24 12:34:221536 if (res)
1537 goto out;
1538 if (strbuf_read_file(&commit_msg, git_path_commit_editmsg(),
1539 2048) < 0) {
1540 res = error_errno(_("unable to read commit message "
1541 "from '%s'"),
1542 git_path_commit_editmsg());
1543 goto out;
1544 }
1545 msg = &commit_msg;
1546 }
1547
Phillip Woodd74f3e52019-03-29 11:08:421548 if (flags & CLEANUP_MSG)
1549 cleanup = COMMIT_MSG_CLEANUP_ALL;
Johannes Schindelinf7d42ce2021-01-28 16:16:421550 else if (flags & VERBATIM_MSG)
1551 cleanup = COMMIT_MSG_CLEANUP_NONE;
Phillip Woodd74f3e52019-03-29 11:08:421552 else if ((opts->signoff || opts->record_origin) &&
1553 !opts->explicit_cleanup)
1554 cleanup = COMMIT_MSG_CLEANUP_SPACE;
1555 else
1556 cleanup = opts->default_msg_cleanup;
Phillip Wood66618a52018-01-24 12:34:221557
1558 if (cleanup != COMMIT_MSG_CLEANUP_NONE)
Calvin Wan787cb8a2023-06-06 19:48:431559 strbuf_stripspace(msg,
1560 cleanup == COMMIT_MSG_CLEANUP_ALL ? comment_line_char : '\0');
Elijah Newrena3ec9ea2018-09-12 21:18:481561 if ((flags & EDIT_MSG) && message_is_empty(msg, cleanup)) {
Phillip Wood66618a52018-01-24 12:34:221562 res = 1; /* run 'git commit' to display error message */
1563 goto out;
1564 }
1565
Phillip Wood7573cec2020-08-17 17:40:021566 if (opts->committer_date_is_author_date) {
1567 struct ident_split id;
1568 struct strbuf date = STRBUF_INIT;
Johannes Sixt12f7bab2018-04-18 18:15:041569
Phillip Wooda3894aa2020-08-17 17:40:031570 if (!opts->ignore_date) {
1571 if (split_ident_line(&id, author, (int)strlen(author)) < 0) {
1572 res = error(_("invalid author identity '%s'"),
1573 author);
1574 goto out;
1575 }
1576 if (!id.date_begin) {
1577 res = error(_(
1578 "corrupt author: missing date information"));
1579 goto out;
1580 }
1581 strbuf_addf(&date, "@%.*s %.*s",
1582 (int)(id.date_end - id.date_begin),
1583 id.date_begin,
1584 (int)(id.tz_end - id.tz_begin),
1585 id.tz_begin);
1586 } else {
1587 reset_ident_date();
Phillip Wood7573cec2020-08-17 17:40:021588 }
Jeff King20204512020-10-23 07:26:301589 committer = fmt_ident(getenv("GIT_COMMITTER_NAME"),
1590 getenv("GIT_COMMITTER_EMAIL"),
Phillip Wooda3894aa2020-08-17 17:40:031591 WANT_COMMITTER_IDENT,
1592 opts->ignore_date ? NULL : date.buf,
Phillip Wood7573cec2020-08-17 17:40:021593 IDENT_STRICT);
1594 strbuf_release(&date);
1595 } else {
1596 reset_ident_date();
1597 }
Junio C Hamano8be83422018-02-15 22:55:431598
Phillip Wooda3894aa2020-08-17 17:40:031599 if (opts->ignore_date) {
1600 struct ident_split id;
1601 char *name, *email;
1602
1603 if (split_ident_line(&id, author, strlen(author)) < 0) {
1604 error(_("invalid author identity '%s'"), author);
1605 goto out;
1606 }
1607 name = xmemdupz(id.name_begin, id.name_end - id.name_begin);
1608 email = xmemdupz(id.mail_begin, id.mail_end - id.mail_begin);
1609 author = fmt_ident(name, email, WANT_AUTHOR_IDENT, NULL,
1610 IDENT_STRICT);
1611 free(name);
1612 free(email);
1613 }
1614
Phillip Woode8cbe212020-08-17 17:40:011615 if (commit_tree_extended(msg->buf, msg->len, &tree, parents, oid,
Phillip Wood7573cec2020-08-17 17:40:021616 author, committer, opts->gpg_sign, extra)) {
Phillip Wood356ee462017-11-24 11:07:571617 res = error(_("failed to write commit object"));
1618 goto out;
1619 }
1620
Phillip Woodd188a602022-11-09 14:21:571621 if (update_head_with_reflog(current_head, oid, opts->reflog_message,
1622 msg, &err)) {
Phillip Wood356ee462017-11-24 11:07:571623 res = error("%s", err.buf);
1624 goto out;
1625 }
1626
Ævar Arnfjörð Bjarmasona8cc5942022-03-07 12:33:461627 run_commit_hook(0, r->index_file, NULL, "post-commit", NULL);
Phillip Wood356ee462017-11-24 11:07:571628 if (flags & AMEND_MSG)
Nguyễn Thái Ngọc Duy1d18d752019-01-12 02:13:231629 commit_post_rewrite(r, current_head, oid);
Phillip Wood356ee462017-11-24 11:07:571630
1631out:
1632 free_commit_extra_headers(extra);
1633 strbuf_release(&err);
Phillip Wood66618a52018-01-24 12:34:221634 strbuf_release(&commit_msg);
Junio C Hamano4d924522020-01-12 20:27:411635 free(amend_author);
Phillip Wood356ee462017-11-24 11:07:571636
1637 return res;
1638}
1639
Phillip Wood430b75f2019-12-06 16:06:121640static int write_rebase_head(struct object_id *oid)
1641{
1642 if (update_ref("rebase", "REBASE_HEAD", oid,
1643 NULL, REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR))
1644 return error(_("could not update %s"), "REBASE_HEAD");
1645
1646 return 0;
1647}
1648
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:561649static int do_commit(struct repository *r,
1650 const char *msg_file, const char *author,
Phillip Wood430b75f2019-12-06 16:06:121651 struct replay_opts *opts, unsigned int flags,
1652 struct object_id *oid)
Phillip Wood356ee462017-11-24 11:07:571653{
1654 int res = 1;
1655
Phillip Woodb0a31862019-08-19 09:18:231656 if (!(flags & EDIT_MSG) && !(flags & VERIFY_MSG)) {
Phillip Wood356ee462017-11-24 11:07:571657 struct object_id oid;
1658 struct strbuf sb = STRBUF_INIT;
1659
1660 if (msg_file && strbuf_read_file(&sb, msg_file, 2048) < 0)
1661 return error_errno(_("unable to read commit message "
1662 "from '%s'"),
1663 msg_file);
1664
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:561665 res = try_to_commit(r, msg_file ? &sb : NULL,
1666 author, opts, flags, &oid);
Phillip Wood356ee462017-11-24 11:07:571667 strbuf_release(&sb);
1668 if (!res) {
Han-Wen Nienhuysc8e41592020-08-21 16:59:351669 refs_delete_ref(get_main_ref_store(r), "",
1670 "CHERRY_PICK_HEAD", NULL, 0);
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:561671 unlink(git_path_merge_msg(r));
Phillip Wood356ee462017-11-24 11:07:571672 if (!is_rebase_i(opts))
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:561673 print_commit_summary(r, NULL, &oid,
Phillip Wood356ee462017-11-24 11:07:571674 SUMMARY_SHOW_AUTHOR_DATE);
1675 return res;
1676 }
1677 }
Phillip Wood430b75f2019-12-06 16:06:121678 if (res == 1) {
1679 if (is_rebase_i(opts) && oid)
1680 if (write_rebase_head(oid))
1681 return -1;
Jeff King20f4b042020-09-30 12:29:311682 return run_git_commit(msg_file, opts, flags);
Phillip Wood430b75f2019-12-06 16:06:121683 }
Phillip Wood356ee462017-11-24 11:07:571684
1685 return res;
1686}
1687
Neil Hormanb27cfb02012-04-20 14:36:151688static int is_original_commit_empty(struct commit *commit)
1689{
brian m. carlson092bbcd2017-07-13 23:49:221690 const struct object_id *ptree_oid;
Neil Hormanb27cfb02012-04-20 14:36:151691
Ævar Arnfjörð Bjarmasonecb50912023-03-28 13:58:481692 if (repo_parse_commit(the_repository, commit))
Johannes Schindelinaee42e12017-12-22 23:55:431693 return error(_("could not parse commit %s"),
brian m. carlsonf2fd0762015-11-10 02:22:281694 oid_to_hex(&commit->object.oid));
Neil Hormanb27cfb02012-04-20 14:36:151695 if (commit->parents) {
1696 struct commit *parent = commit->parents->item;
Ævar Arnfjörð Bjarmasonecb50912023-03-28 13:58:481697 if (repo_parse_commit(the_repository, parent))
Johannes Schindelinaee42e12017-12-22 23:55:431698 return error(_("could not parse parent commit %s"),
brian m. carlsonf2fd0762015-11-10 02:22:281699 oid_to_hex(&parent->object.oid));
Derrick Stolee2e27bd72018-04-06 19:09:381700 ptree_oid = get_commit_tree_oid(parent);
Neil Hormanb27cfb02012-04-20 14:36:151701 } else {
brian m. carlsoneb0ccfd2017-11-12 21:28:541702 ptree_oid = the_hash_algo->empty_tree; /* commit is root */
Neil Hormanb27cfb02012-04-20 14:36:151703 }
1704
Jeff King4a7e27e2018-08-28 21:22:401705 return oideq(ptree_oid, get_commit_tree_oid(commit));
Ramkumar Ramachandra043a4492012-01-11 18:15:571706}
1707
Junio C Hamanoac2b0e82012-05-30 00:14:411708/*
Elijah Newrene98c4262020-02-15 21:36:251709 * Should empty commits be allowed? Return status:
1710 * <0: Error in is_index_unchanged(r) or is_original_commit_empty(commit)
1711 * 0: Halt on empty commit
1712 * 1: Allow empty commit
1713 * 2: Drop empty commit
Junio C Hamanoac2b0e82012-05-30 00:14:411714 */
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:561715static int allow_empty(struct repository *r,
1716 struct replay_opts *opts,
1717 struct commit *commit)
Junio C Hamanoac2b0e82012-05-30 00:14:411718{
Elijah Newrend48e5e22020-02-15 21:36:241719 int index_unchanged, originally_empty;
Junio C Hamanoac2b0e82012-05-30 00:14:411720
1721 /*
Elijah Newrene98c4262020-02-15 21:36:251722 * Four cases:
Junio C Hamanoac2b0e82012-05-30 00:14:411723 *
1724 * (1) we do not allow empty at all and error out.
1725 *
Elijah Newrene98c4262020-02-15 21:36:251726 * (2) we allow ones that were initially empty, and
1727 * just drop the ones that become empty
Junio C Hamanoac2b0e82012-05-30 00:14:411728 *
Elijah Newrene98c4262020-02-15 21:36:251729 * (3) we allow ones that were initially empty, but
1730 * halt for the ones that become empty;
1731 *
1732 * (4) we allow both.
Junio C Hamanoac2b0e82012-05-30 00:14:411733 */
1734 if (!opts->allow_empty)
1735 return 0; /* let "git commit" barf as necessary */
1736
Nguyễn Thái Ngọc Duy005af332018-11-10 05:48:571737 index_unchanged = is_index_unchanged(r);
Junio C Hamanoac2b0e82012-05-30 00:14:411738 if (index_unchanged < 0)
1739 return index_unchanged;
1740 if (!index_unchanged)
1741 return 0; /* we do not have to say --allow-empty */
1742
1743 if (opts->keep_redundant_commits)
1744 return 1;
1745
Elijah Newrend48e5e22020-02-15 21:36:241746 originally_empty = is_original_commit_empty(commit);
1747 if (originally_empty < 0)
1748 return originally_empty;
Elijah Newrene98c4262020-02-15 21:36:251749 if (originally_empty)
Junio C Hamanoac2b0e82012-05-30 00:14:411750 return 1;
Elijah Newrene98c4262020-02-15 21:36:251751 else if (opts->drop_redundant_commits)
1752 return 2;
1753 else
1754 return 0;
Junio C Hamanoac2b0e82012-05-30 00:14:411755}
1756
Johannes Schindelin414697a2017-01-02 15:27:151757static struct {
1758 char c;
1759 const char *str;
1760} todo_command_info[] = {
Derrick Stoleed7ce9a22022-07-19 18:33:371761 [TODO_PICK] = { 'p', "pick" },
1762 [TODO_REVERT] = { 0, "revert" },
1763 [TODO_EDIT] = { 'e', "edit" },
1764 [TODO_REWORD] = { 'r', "reword" },
1765 [TODO_FIXUP] = { 'f', "fixup" },
1766 [TODO_SQUASH] = { 's', "squash" },
1767 [TODO_EXEC] = { 'x', "exec" },
1768 [TODO_BREAK] = { 'b', "break" },
1769 [TODO_LABEL] = { 'l', "label" },
1770 [TODO_RESET] = { 't', "reset" },
1771 [TODO_MERGE] = { 'm', "merge" },
Derrick Stoleea97d7912022-07-19 18:33:381772 [TODO_UPDATE_REF] = { 'u', "update-ref" },
Derrick Stoleed7ce9a22022-07-19 18:33:371773 [TODO_NOOP] = { 0, "noop" },
1774 [TODO_DROP] = { 'd', "drop" },
1775 [TODO_COMMENT] = { 0, NULL },
Johannes Schindelin004fefa2016-10-21 12:24:411776};
1777
1778static const char *command_to_string(const enum todo_command command)
1779{
Johannes Schindelinac191472017-01-02 15:34:391780 if (command < TODO_COMMENT)
Johannes Schindelin414697a2017-01-02 15:27:151781 return todo_command_info[command].str;
Nguyễn Thái Ngọc Duy02127c62018-07-21 07:49:381782 die(_("unknown command: %d"), command);
Johannes Schindelin004fefa2016-10-21 12:24:411783}
1784
Junio C Hamanoee5462d2017-12-27 19:12:451785static char command_to_char(const enum todo_command command)
Liam Beguind8ae6c82017-12-05 17:52:341786{
Alban Gruin68e70902020-03-30 12:42:351787 if (command < TODO_COMMENT)
Liam Beguind8ae6c82017-12-05 17:52:341788 return todo_command_info[command].c;
1789 return comment_line_char;
1790}
1791
Johannes Schindelin25c43662017-01-02 15:26:381792static int is_noop(const enum todo_command command)
1793{
Johannes Schindelinb3fdd582017-01-02 15:34:341794 return TODO_NOOP <= command;
Johannes Schindelin25c43662017-01-02 15:26:381795}
Johannes Schindelin004fefa2016-10-21 12:24:411796
Johannes Schindelin6e98de72017-01-02 15:27:071797static int is_fixup(enum todo_command command)
Ramkumar Ramachandra043a4492012-01-11 18:15:571798{
Johannes Schindelin6e98de72017-01-02 15:27:071799 return command == TODO_FIXUP || command == TODO_SQUASH;
1800}
1801
Johannes Schindelind87d48b2018-05-03 23:01:171802/* Does this command create a (non-merge) commit? */
1803static int is_pick_or_similar(enum todo_command command)
1804{
1805 switch (command) {
1806 case TODO_PICK:
1807 case TODO_REVERT:
1808 case TODO_EDIT:
1809 case TODO_REWORD:
1810 case TODO_FIXUP:
1811 case TODO_SQUASH:
1812 return 1;
1813 default:
1814 return 0;
1815 }
1816}
1817
Charvi Mendiratta9e3cebd2021-01-29 18:20:471818enum todo_item_flags {
1819 TODO_EDIT_MERGE_MSG = (1 << 0),
1820 TODO_REPLACE_FIXUP_MSG = (1 << 1),
1821 TODO_EDIT_FIXUP_MSG = (1 << 2),
1822};
1823
Charvi Mendiratta71ee81c2021-01-29 18:20:461824static const char first_commit_msg_str[] = N_("This is the 1st commit message:");
1825static const char nth_commit_msg_fmt[] = N_("This is the commit message #%d:");
Charvi Mendiratta9e3cebd2021-01-29 18:20:471826static const char skip_first_commit_msg_str[] = N_("The 1st commit message will be skipped:");
Charvi Mendiratta71ee81c2021-01-29 18:20:461827static const char skip_nth_commit_msg_fmt[] = N_("The commit message #%d will be skipped:");
1828static const char combined_commit_msg_fmt[] = N_("This is a combination of %d commits.");
1829
Charvi Mendiratta1f969602021-02-08 19:25:201830static int is_fixup_flag(enum todo_command command, unsigned flag)
Phillip Wood498bb5b2021-01-19 07:40:571831{
Charvi Mendiratta9e3cebd2021-01-29 18:20:471832 return command == TODO_FIXUP && ((flag & TODO_REPLACE_FIXUP_MSG) ||
1833 (flag & TODO_EDIT_FIXUP_MSG));
1834}
Phillip Wood7cdb9682021-01-29 18:20:441835
Charvi Mendiratta9e3cebd2021-01-29 18:20:471836/*
1837 * Wrapper around strbuf_add_commented_lines() which avoids double
1838 * commenting commit subjects.
1839 */
1840static void add_commented_lines(struct strbuf *buf, const void *str, size_t len)
1841{
1842 const char *s = str;
1843 while (len > 0 && s[0] == comment_line_char) {
1844 size_t count;
1845 const char *n = memchr(s, '\n', len);
1846 if (!n)
1847 count = len;
1848 else
1849 count = n - s + 1;
1850 strbuf_add(buf, s, count);
1851 s += count;
1852 len -= count;
1853 }
Calvin Wan787cb8a2023-06-06 19:48:431854 strbuf_add_commented_lines(buf, s, len, comment_line_char);
Charvi Mendiratta9e3cebd2021-01-29 18:20:471855}
1856
1857/* Does the current fixup chain contain a squash command? */
1858static int seen_squash(struct replay_opts *opts)
1859{
1860 return starts_with(opts->current_fixups.buf, "squash") ||
1861 strstr(opts->current_fixups.buf, "\nsquash");
1862}
1863
1864static void update_comment_bufs(struct strbuf *buf1, struct strbuf *buf2, int n)
1865{
1866 strbuf_setlen(buf1, 2);
1867 strbuf_addf(buf1, _(nth_commit_msg_fmt), n);
1868 strbuf_addch(buf1, '\n');
1869 strbuf_setlen(buf2, 2);
1870 strbuf_addf(buf2, _(skip_nth_commit_msg_fmt), n);
1871 strbuf_addch(buf2, '\n');
1872}
1873
1874/*
1875 * Comment out any un-commented commit messages, updating the message comments
1876 * to say they will be skipped but do not comment out the empty lines that
1877 * surround commit messages and their comments.
1878 */
1879static void update_squash_message_for_fixup(struct strbuf *msg)
1880{
1881 void (*copy_lines)(struct strbuf *, const void *, size_t) = strbuf_add;
1882 struct strbuf buf1 = STRBUF_INIT, buf2 = STRBUF_INIT;
1883 const char *s, *start;
1884 char *orig_msg;
1885 size_t orig_msg_len;
1886 int i = 1;
1887
1888 strbuf_addf(&buf1, "# %s\n", _(first_commit_msg_str));
1889 strbuf_addf(&buf2, "# %s\n", _(skip_first_commit_msg_str));
1890 s = start = orig_msg = strbuf_detach(msg, &orig_msg_len);
1891 while (s) {
1892 const char *next;
1893 size_t off;
1894 if (skip_prefix(s, buf1.buf, &next)) {
1895 /*
1896 * Copy the last message, preserving the blank line
1897 * preceding the current line
1898 */
1899 off = (s > start + 1 && s[-2] == '\n') ? 1 : 0;
1900 copy_lines(msg, start, s - start - off);
1901 if (off)
1902 strbuf_addch(msg, '\n');
1903 /*
1904 * The next message needs to be commented out but the
1905 * message header is already commented out so just copy
1906 * it and the blank line that follows it.
1907 */
1908 strbuf_addbuf(msg, &buf2);
1909 if (*next == '\n')
1910 strbuf_addch(msg, *next++);
1911 start = s = next;
1912 copy_lines = add_commented_lines;
1913 update_comment_bufs(&buf1, &buf2, ++i);
1914 } else if (skip_prefix(s, buf2.buf, &next)) {
1915 off = (s > start + 1 && s[-2] == '\n') ? 1 : 0;
1916 copy_lines(msg, start, s - start - off);
1917 start = s - off;
1918 s = next;
1919 copy_lines = strbuf_add;
1920 update_comment_bufs(&buf1, &buf2, ++i);
1921 } else {
1922 s = strchr(s, '\n');
1923 if (s)
1924 s++;
1925 }
1926 }
1927 copy_lines(msg, start, orig_msg_len - (start - orig_msg));
1928 free(orig_msg);
1929 strbuf_release(&buf1);
1930 strbuf_release(&buf2);
1931}
1932
1933static int append_squash_message(struct strbuf *buf, const char *body,
1934 enum todo_command command, struct replay_opts *opts,
Charvi Mendirattaa25314c2021-02-08 19:25:191935 unsigned flag)
Charvi Mendiratta9e3cebd2021-01-29 18:20:471936{
1937 const char *fixup_msg;
1938 size_t commented_len = 0, fixup_off;
1939 /*
1940 * amend is non-interactive and not normally used with fixup!
1941 * or squash! commits, so only comment out those subjects when
1942 * squashing commit messages.
1943 */
1944 if (starts_with(body, "amend!") ||
1945 ((command == TODO_SQUASH || seen_squash(opts)) &&
1946 (starts_with(body, "squash!") || starts_with(body, "fixup!"))))
Charvi Mendiratta6e0e2882021-03-15 07:54:311947 commented_len = commit_subject_length(body);
Charvi Mendiratta9e3cebd2021-01-29 18:20:471948
Phillip Wood498bb5b2021-01-19 07:40:571949 strbuf_addf(buf, "\n%c ", comment_line_char);
Charvi Mendiratta71ee81c2021-01-29 18:20:461950 strbuf_addf(buf, _(nth_commit_msg_fmt),
Phillip Wood498bb5b2021-01-19 07:40:571951 ++opts->current_fixup_count + 1);
1952 strbuf_addstr(buf, "\n\n");
Calvin Wan787cb8a2023-06-06 19:48:431953 strbuf_add_commented_lines(buf, body, commented_len, comment_line_char);
Charvi Mendiratta9e3cebd2021-01-29 18:20:471954 /* buf->buf may be reallocated so store an offset into the buffer */
1955 fixup_off = buf->len;
Phillip Wood7cdb9682021-01-29 18:20:441956 strbuf_addstr(buf, body + commented_len);
Charvi Mendiratta9e3cebd2021-01-29 18:20:471957
1958 /* fixup -C after squash behaves like squash */
Charvi Mendiratta1f969602021-02-08 19:25:201959 if (is_fixup_flag(command, flag) && !seen_squash(opts)) {
Charvi Mendiratta9e3cebd2021-01-29 18:20:471960 /*
1961 * We're replacing the commit message so we need to
1962 * append the Signed-off-by: trailer if the user
1963 * requested '--signoff'.
1964 */
1965 if (opts->signoff)
1966 append_signoff(buf, 0, 0);
1967
1968 if ((command == TODO_FIXUP) &&
1969 (flag & TODO_REPLACE_FIXUP_MSG) &&
1970 (file_exists(rebase_path_fixup_msg()) ||
1971 !file_exists(rebase_path_squash_msg()))) {
1972 fixup_msg = skip_blank_lines(buf->buf + fixup_off);
1973 if (write_message(fixup_msg, strlen(fixup_msg),
1974 rebase_path_fixup_msg(), 0) < 0)
1975 return error(_("cannot write '%s'"),
1976 rebase_path_fixup_msg());
1977 } else {
1978 unlink(rebase_path_fixup_msg());
1979 }
1980 } else {
1981 unlink(rebase_path_fixup_msg());
1982 }
1983
1984 return 0;
Phillip Wood498bb5b2021-01-19 07:40:571985}
1986
Nguyễn Thái Ngọc Duy005af332018-11-10 05:48:571987static int update_squash_messages(struct repository *r,
1988 enum todo_command command,
1989 struct commit *commit,
Charvi Mendiratta9e3cebd2021-01-29 18:20:471990 struct replay_opts *opts,
Charvi Mendirattaa25314c2021-02-08 19:25:191991 unsigned flag)
Johannes Schindelin6e98de72017-01-02 15:27:071992{
1993 struct strbuf buf = STRBUF_INIT;
Charvi Mendiratta9e3cebd2021-01-29 18:20:471994 int res = 0;
Johannes Schindelin6e98de72017-01-02 15:27:071995 const char *message, *body;
Doan Tran Cong Danhb3757442019-11-08 09:43:481996 const char *encoding = get_commit_output_encoding();
Johannes Schindelin6e98de72017-01-02 15:27:071997
Johannes Schindeline12a7ef2018-04-27 20:48:211998 if (opts->current_fixup_count > 0) {
Johannes Schindelin6e98de72017-01-02 15:27:071999 struct strbuf header = STRBUF_INIT;
Johannes Schindeline12a7ef2018-04-27 20:48:212000 char *eol;
Johannes Schindelin6e98de72017-01-02 15:27:072001
Johannes Schindeline12a7ef2018-04-27 20:48:212002 if (strbuf_read_file(&buf, rebase_path_squash_msg(), 9) <= 0)
Johannes Schindelin6e98de72017-01-02 15:27:072003 return error(_("could not read '%s'"),
2004 rebase_path_squash_msg());
2005
Johannes Schindeline12a7ef2018-04-27 20:48:212006 eol = buf.buf[0] != comment_line_char ?
2007 buf.buf : strchrnul(buf.buf, '\n');
Johannes Schindelin6e98de72017-01-02 15:27:072008
2009 strbuf_addf(&header, "%c ", comment_line_char);
Charvi Mendiratta71ee81c2021-01-29 18:20:462010 strbuf_addf(&header, _(combined_commit_msg_fmt),
Johannes Schindeline12a7ef2018-04-27 20:48:212011 opts->current_fixup_count + 2);
Johannes Schindelin6e98de72017-01-02 15:27:072012 strbuf_splice(&buf, 0, eol - buf.buf, header.buf, header.len);
2013 strbuf_release(&header);
Charvi Mendiratta1f969602021-02-08 19:25:202014 if (is_fixup_flag(command, flag) && !seen_squash(opts))
Charvi Mendiratta9e3cebd2021-01-29 18:20:472015 update_squash_message_for_fixup(&buf);
Johannes Schindelin6e98de72017-01-02 15:27:072016 } else {
brian m. carlson33d66df2017-05-06 22:10:072017 struct object_id head;
Johannes Schindelin6e98de72017-01-02 15:27:072018 struct commit *head_commit;
2019 const char *head_message, *body;
2020
Ævar Arnfjörð Bjarmason4a93b892023-03-28 13:58:582021 if (repo_get_oid(r, "HEAD", &head))
Johannes Schindelin6e98de72017-01-02 15:27:072022 return error(_("need a HEAD to fixup"));
Nguyễn Thái Ngọc Duy005af332018-11-10 05:48:572023 if (!(head_commit = lookup_commit_reference(r, &head)))
Johannes Schindelin6e98de72017-01-02 15:27:072024 return error(_("could not read HEAD"));
Ævar Arnfjörð Bjarmason4a93b892023-03-28 13:58:582025 if (!(head_message = repo_logmsg_reencode(r, head_commit, NULL,
2026 encoding)))
Johannes Schindelin6e98de72017-01-02 15:27:072027 return error(_("could not read HEAD's commit message"));
2028
2029 find_commit_subject(head_message, &body);
Charvi Mendiratta9e3cebd2021-01-29 18:20:472030 if (command == TODO_FIXUP && !flag && write_message(body, strlen(body),
Phillip Woodeab0df02021-01-19 07:40:562031 rebase_path_fixup_msg(), 0) < 0) {
Ævar Arnfjörð Bjarmason4a93b892023-03-28 13:58:582032 repo_unuse_commit_buffer(r, head_commit, head_message);
Phillip Woodeab0df02021-01-19 07:40:562033 return error(_("cannot write '%s'"), rebase_path_fixup_msg());
Johannes Schindelin6e98de72017-01-02 15:27:072034 }
Johannes Schindelin6e98de72017-01-02 15:27:072035 strbuf_addf(&buf, "%c ", comment_line_char);
Charvi Mendiratta71ee81c2021-01-29 18:20:462036 strbuf_addf(&buf, _(combined_commit_msg_fmt), 2);
Johannes Schindelin6e98de72017-01-02 15:27:072037 strbuf_addf(&buf, "\n%c ", comment_line_char);
Charvi Mendiratta1f969602021-02-08 19:25:202038 strbuf_addstr(&buf, is_fixup_flag(command, flag) ?
Charvi Mendiratta9e3cebd2021-01-29 18:20:472039 _(skip_first_commit_msg_str) :
2040 _(first_commit_msg_str));
Johannes Schindelin6e98de72017-01-02 15:27:072041 strbuf_addstr(&buf, "\n\n");
Charvi Mendiratta1f969602021-02-08 19:25:202042 if (is_fixup_flag(command, flag))
Calvin Wan787cb8a2023-06-06 19:48:432043 strbuf_add_commented_lines(&buf, body, strlen(body),
2044 comment_line_char);
Charvi Mendiratta9e3cebd2021-01-29 18:20:472045 else
2046 strbuf_addstr(&buf, body);
Johannes Schindelin6e98de72017-01-02 15:27:072047
Ævar Arnfjörð Bjarmason4a93b892023-03-28 13:58:582048 repo_unuse_commit_buffer(r, head_commit, head_message);
Johannes Schindelin6e98de72017-01-02 15:27:072049 }
2050
Ævar Arnfjörð Bjarmason4a93b892023-03-28 13:58:582051 if (!(message = repo_logmsg_reencode(r, commit, NULL, encoding)))
Johannes Schindelin6e98de72017-01-02 15:27:072052 return error(_("could not read commit message of %s"),
2053 oid_to_hex(&commit->object.oid));
2054 find_commit_subject(message, &body);
2055
Charvi Mendiratta1f969602021-02-08 19:25:202056 if (command == TODO_SQUASH || is_fixup_flag(command, flag)) {
Charvi Mendiratta9e3cebd2021-01-29 18:20:472057 res = append_squash_message(&buf, body, command, opts, flag);
Johannes Schindelin6e98de72017-01-02 15:27:072058 } else if (command == TODO_FIXUP) {
2059 strbuf_addf(&buf, "\n%c ", comment_line_char);
Charvi Mendiratta71ee81c2021-01-29 18:20:462060 strbuf_addf(&buf, _(skip_nth_commit_msg_fmt),
Phillip Wooddd2e36e2018-08-15 09:41:252061 ++opts->current_fixup_count + 1);
Johannes Schindelin6e98de72017-01-02 15:27:072062 strbuf_addstr(&buf, "\n\n");
Calvin Wan787cb8a2023-06-06 19:48:432063 strbuf_add_commented_lines(&buf, body, strlen(body),
2064 comment_line_char);
Johannes Schindelin6e98de72017-01-02 15:27:072065 } else
2066 return error(_("unknown command: %d"), command);
Ævar Arnfjörð Bjarmason4a93b892023-03-28 13:58:582067 repo_unuse_commit_buffer(r, commit, message);
Johannes Schindelin6e98de72017-01-02 15:27:072068
Charvi Mendiratta9e3cebd2021-01-29 18:20:472069 if (!res)
2070 res = write_message(buf.buf, buf.len, rebase_path_squash_msg(),
2071 0);
Johannes Schindelin6e98de72017-01-02 15:27:072072 strbuf_release(&buf);
Johannes Schindeline12a7ef2018-04-27 20:48:212073
2074 if (!res) {
2075 strbuf_addf(&opts->current_fixups, "%s%s %s",
2076 opts->current_fixups.len ? "\n" : "",
2077 command_to_string(command),
2078 oid_to_hex(&commit->object.oid));
2079 res = write_message(opts->current_fixups.buf,
2080 opts->current_fixups.len,
2081 rebase_path_current_fixups(), 0);
2082 }
2083
Johannes Schindelin6e98de72017-01-02 15:27:072084 return res;
2085}
2086
Nguyễn Thái Ngọc Duy3b335762018-12-09 10:25:212087static void flush_rewritten_pending(void)
2088{
Johannes Schindelin25cb8df2017-01-02 15:28:162089 struct strbuf buf = STRBUF_INIT;
brian m. carlson092bbcd2017-07-13 23:49:222090 struct object_id newoid;
Johannes Schindelin25cb8df2017-01-02 15:28:162091 FILE *out;
2092
brian m. carlson092bbcd2017-07-13 23:49:222093 if (strbuf_read_file(&buf, rebase_path_rewritten_pending(), (GIT_MAX_HEXSZ + 1) * 2) > 0 &&
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 13:58:462094 !repo_get_oid(the_repository, "HEAD", &newoid) &&
Nguyễn Thái Ngọc Duye9d983f2017-05-03 10:16:502095 (out = fopen_or_warn(rebase_path_rewritten_list(), "a"))) {
Johannes Schindelin25cb8df2017-01-02 15:28:162096 char *bol = buf.buf, *eol;
2097
2098 while (*bol) {
2099 eol = strchrnul(bol, '\n');
2100 fprintf(out, "%.*s %s\n", (int)(eol - bol),
brian m. carlson092bbcd2017-07-13 23:49:222101 bol, oid_to_hex(&newoid));
Johannes Schindelin25cb8df2017-01-02 15:28:162102 if (!*eol)
2103 break;
2104 bol = eol + 1;
2105 }
2106 fclose(out);
2107 unlink(rebase_path_rewritten_pending());
2108 }
2109 strbuf_release(&buf);
2110}
2111
2112static void record_in_rewritten(struct object_id *oid,
Nguyễn Thái Ngọc Duy3b335762018-12-09 10:25:212113 enum todo_command next_command)
2114{
Nguyễn Thái Ngọc Duye9d983f2017-05-03 10:16:502115 FILE *out = fopen_or_warn(rebase_path_rewritten_pending(), "a");
Johannes Schindelin25cb8df2017-01-02 15:28:162116
2117 if (!out)
2118 return;
2119
2120 fprintf(out, "%s\n", oid_to_hex(oid));
2121 fclose(out);
2122
2123 if (!is_fixup(next_command))
2124 flush_rewritten_pending();
2125}
2126
Elijah Newren39edfd52021-03-31 06:52:202127static int should_edit(struct replay_opts *opts) {
2128 if (opts->edit < 0)
2129 /*
2130 * Note that we only handle the case of non-conflicted
2131 * commits; continue_single_pick() handles the conflicted
2132 * commits itself instead of calling this function.
2133 */
2134 return (opts->action == REPLAY_REVERT && isatty(0)) ? 1 : 0;
2135 return opts->edit;
2136}
2137
Junio C Hamano43966ab2022-05-27 06:01:392138static void refer_to_commit(struct replay_opts *opts,
2139 struct strbuf *msgbuf, struct commit *commit)
2140{
2141 if (opts->commit_use_reference) {
2142 struct pretty_print_context ctx = {
2143 .abbrev = DEFAULT_ABBREV,
2144 .date_mode.type = DATE_SHORT,
2145 };
Ævar Arnfjörð Bjarmasonbab82162023-03-28 13:58:512146 repo_format_commit_message(the_repository, commit,
2147 "%h (%s, %ad)", msgbuf, &ctx);
Junio C Hamano43966ab2022-05-27 06:01:392148 } else {
2149 strbuf_addstr(msgbuf, oid_to_hex(&commit->object.oid));
2150 }
2151}
2152
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:562153static int do_pick_commit(struct repository *r,
Charvi Mendirattaae70e342021-01-29 18:20:452154 struct todo_item *item,
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:562155 struct replay_opts *opts,
Phillip Wooda47ba3c2019-08-19 09:18:222156 int final_fixup, int *check_todo)
Johannes Schindelin6e98de72017-01-02 15:27:072157{
Elijah Newren39edfd52021-03-31 06:52:202158 unsigned int flags = should_edit(opts) ? EDIT_MSG : 0;
2159 const char *msg_file = should_edit(opts) ? NULL : git_path_merge_msg(r);
brian m. carlsonace976b2017-05-06 22:10:322160 struct object_id head;
Ramkumar Ramachandra043a4492012-01-11 18:15:572161 struct commit *base, *next, *parent;
2162 const char *base_label, *next_label;
Phillip Wood356ee462017-11-24 11:07:572163 char *author = NULL;
Jeff Kingd74a4e52014-06-10 21:39:352164 struct commit_message msg = { NULL, NULL, NULL, NULL };
Ramkumar Ramachandra043a4492012-01-11 18:15:572165 struct strbuf msgbuf = STRBUF_INIT;
Elijah Newrene98c4262020-02-15 21:36:252166 int res, unborn = 0, reword = 0, allow, drop_commit;
Charvi Mendirattaae70e342021-01-29 18:20:452167 enum todo_command command = item->command;
2168 struct commit *commit = item->commit;
Ramkumar Ramachandra043a4492012-01-11 18:15:572169
2170 if (opts->no_commit) {
2171 /*
2172 * We do not intend to commit immediately. We just want to
2173 * merge the differences in, so let's compute the tree
Elijah Newren81483fe2021-08-04 23:50:542174 * that represents the "current" state for the merge machinery
Ramkumar Ramachandra043a4492012-01-11 18:15:572175 * to work on.
2176 */
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:562177 if (write_index_as_tree(&head, r->index, r->index_file, 0, NULL))
Johannes Schindelin93b3df62016-10-21 12:26:252178 return error(_("your index file is unmerged."));
Ramkumar Ramachandra043a4492012-01-11 18:15:572179 } else {
Ævar Arnfjörð Bjarmason4a93b892023-03-28 13:58:582180 unborn = repo_get_oid(r, "HEAD", &head);
Johannes Schindelind87d48b2018-05-03 23:01:172181 /* Do we want to generate a root commit? */
2182 if (is_pick_or_similar(command) && opts->have_squash_onto &&
Jeff King4a7e27e2018-08-28 21:22:402183 oideq(&head, &opts->squash_onto)) {
Johannes Schindelind87d48b2018-05-03 23:01:172184 if (is_fixup(command))
2185 return error(_("cannot fixup root commit"));
2186 flags |= CREATE_ROOT_COMMIT;
2187 unborn = 1;
2188 } else if (unborn)
brian m. carlsoneb0ccfd2017-11-12 21:28:542189 oidcpy(&head, the_hash_algo->empty_tree);
Nguyễn Thái Ngọc Duyffc00a42018-11-10 05:49:042190 if (index_differs_from(r, unborn ? empty_tree_oid_hex() : "HEAD",
Brandon Williams02f2f562017-10-31 18:19:052191 NULL, 0))
Nguyễn Thái Ngọc Duye1ff0a32019-01-12 02:13:262192 return error_dirty_index(r, opts);
Ramkumar Ramachandra043a4492012-01-11 18:15:572193 }
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:562194 discard_index(r->index);
Ramkumar Ramachandra043a4492012-01-11 18:15:572195
Johannes Schindelin637666c2017-01-02 15:26:082196 if (!commit->parents)
Ramkumar Ramachandra043a4492012-01-11 18:15:572197 parent = NULL;
Ramkumar Ramachandra043a4492012-01-11 18:15:572198 else if (commit->parents->next) {
2199 /* Reverting or cherry-picking a merge commit */
2200 int cnt;
2201 struct commit_list *p;
2202
2203 if (!opts->mainline)
Johannes Schindelin93b3df62016-10-21 12:26:252204 return error(_("commit %s is a merge but no -m option was given."),
brian m. carlsonf2fd0762015-11-10 02:22:282205 oid_to_hex(&commit->object.oid));
Ramkumar Ramachandra043a4492012-01-11 18:15:572206
2207 for (cnt = 1, p = commit->parents;
2208 cnt != opts->mainline && p;
2209 cnt++)
2210 p = p->next;
2211 if (cnt != opts->mainline || !p)
Johannes Schindelin93b3df62016-10-21 12:26:252212 return error(_("commit %s does not have parent %d"),
brian m. carlsonf2fd0762015-11-10 02:22:282213 oid_to_hex(&commit->object.oid), opts->mainline);
Ramkumar Ramachandra043a4492012-01-11 18:15:572214 parent = p->item;
Sergey Organov37897bf2018-12-14 04:53:512215 } else if (1 < opts->mainline)
2216 /*
2217 * Non-first parent explicitly specified as mainline for
2218 * non-merge commit
2219 */
2220 return error(_("commit %s does not have parent %d"),
2221 oid_to_hex(&commit->object.oid), opts->mainline);
Ramkumar Ramachandra043a4492012-01-11 18:15:572222 else
2223 parent = commit->parents->item;
2224
Johannes Schindelinbcbb68b2017-01-02 15:28:052225 if (get_message(commit, &msg) != 0)
2226 return error(_("cannot get commit message for %s"),
2227 oid_to_hex(&commit->object.oid));
2228
Johannes Schindelin6e98de72017-01-02 15:27:072229 if (opts->allow_ff && !is_fixup(command) &&
Jeff King4a7e27e2018-08-28 21:22:402230 ((parent && oideq(&parent->object.oid, &head)) ||
Johannes Schindelinbcbb68b2017-01-02 15:28:052231 (!parent && unborn))) {
2232 if (is_rebase_i(opts))
2233 write_author_script(msg.message);
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:562234 res = fast_forward_to(r, &commit->object.oid, &head, unborn,
Johannes Schindelinbcbb68b2017-01-02 15:28:052235 opts);
2236 if (res || command != TODO_REWORD)
2237 goto leave;
Phillip Wood450efe22019-08-19 09:18:212238 reword = 1;
Johannes Schindelinbcbb68b2017-01-02 15:28:052239 msg_file = NULL;
2240 goto fast_forward_edit;
2241 }
Ævar Arnfjörð Bjarmason4a93b892023-03-28 13:58:582242 if (parent && repo_parse_commit(r, parent) < 0)
Johannes Schindelin004fefa2016-10-21 12:24:412243 /* TRANSLATORS: The first %s will be a "todo" command like
2244 "revert" or "pick", the second %s a SHA1. */
Ramkumar Ramachandra043a4492012-01-11 18:15:572245 return error(_("%s: cannot parse parent commit %s"),
Johannes Schindelin004fefa2016-10-21 12:24:412246 command_to_string(command),
2247 oid_to_hex(&parent->object.oid));
Ramkumar Ramachandra043a4492012-01-11 18:15:572248
Ramkumar Ramachandra043a4492012-01-11 18:15:572249 /*
2250 * "commit" is an existing commit. We would want to apply
2251 * the difference it introduces since its first parent "prev"
2252 * on top of the current HEAD if we are cherry-pick. Or the
2253 * reverse of it if we are revert.
2254 */
2255
Johannes Schindelin004fefa2016-10-21 12:24:412256 if (command == TODO_REVERT) {
Oswald Buddenhagen883cb1b2023-09-02 07:20:352257 const char *orig_subject;
2258
Ramkumar Ramachandra043a4492012-01-11 18:15:572259 base = commit;
2260 base_label = msg.label;
2261 next = parent;
2262 next_label = msg.parent_label;
Junio C Hamano43966ab2022-05-27 06:01:392263 if (opts->commit_use_reference) {
2264 strbuf_addstr(&msgbuf,
2265 "# *** SAY WHY WE ARE REVERTING ON THE TITLE LINE ***");
Oswald Buddenhagen883cb1b2023-09-02 07:20:352266 } else if (skip_prefix(msg.subject, "Revert \"", &orig_subject) &&
2267 /*
2268 * We don't touch pre-existing repeated reverts, because
2269 * theoretically these can be nested arbitrarily deeply,
2270 * thus requiring excessive complexity to deal with.
2271 */
2272 !starts_with(orig_subject, "Revert \"")) {
2273 strbuf_addstr(&msgbuf, "Reapply \"");
2274 strbuf_addstr(&msgbuf, orig_subject);
Junio C Hamano43966ab2022-05-27 06:01:392275 } else {
2276 strbuf_addstr(&msgbuf, "Revert \"");
2277 strbuf_addstr(&msgbuf, msg.subject);
2278 strbuf_addstr(&msgbuf, "\"");
2279 }
2280 strbuf_addstr(&msgbuf, "\n\nThis reverts commit ");
2281 refer_to_commit(opts, &msgbuf, commit);
Ramkumar Ramachandra043a4492012-01-11 18:15:572282
2283 if (commit->parents && commit->parents->next) {
2284 strbuf_addstr(&msgbuf, ", reversing\nchanges made to ");
Junio C Hamano43966ab2022-05-27 06:01:392285 refer_to_commit(opts, &msgbuf, parent);
Ramkumar Ramachandra043a4492012-01-11 18:15:572286 }
2287 strbuf_addstr(&msgbuf, ".\n");
2288 } else {
2289 const char *p;
2290
2291 base = parent;
2292 base_label = msg.parent_label;
2293 next = commit;
2294 next_label = msg.label;
2295
Johannes Schindelin23aa5142017-01-02 15:26:202296 /* Append the commit log message to msgbuf. */
2297 if (find_commit_subject(msg.message, &p))
2298 strbuf_addstr(&msgbuf, p);
Ramkumar Ramachandra043a4492012-01-11 18:15:572299
2300 if (opts->record_origin) {
Jonathan Tan44dc7382017-04-26 20:50:032301 strbuf_complete_line(&msgbuf);
Brandon Caseybab4d102013-02-12 10:17:352302 if (!has_conforming_footer(&msgbuf, NULL, 0))
Brandon Caseyb971e042013-02-12 10:17:342303 strbuf_addch(&msgbuf, '\n');
Brandon Caseycd650a42013-02-12 10:17:322304 strbuf_addstr(&msgbuf, cherry_picked_prefix);
brian m. carlsonf2fd0762015-11-10 02:22:282305 strbuf_addstr(&msgbuf, oid_to_hex(&commit->object.oid));
Ramkumar Ramachandra043a4492012-01-11 18:15:572306 strbuf_addstr(&msgbuf, ")\n");
2307 }
Phillip Wood356ee462017-11-24 11:07:572308 if (!is_fixup(command))
2309 author = get_author(msg.message);
Ramkumar Ramachandra043a4492012-01-11 18:15:572310 }
2311
Johannes Schindelin04efc8b2017-01-02 15:28:002312 if (command == TODO_REWORD)
Phillip Wood450efe22019-08-19 09:18:212313 reword = 1;
Johannes Schindelin04efc8b2017-01-02 15:28:002314 else if (is_fixup(command)) {
Charvi Mendiratta9e3cebd2021-01-29 18:20:472315 if (update_squash_messages(r, command, commit,
Ævar Arnfjörð Bjarmasona5792e92023-02-06 19:08:112316 opts, item->flags)) {
2317 res = -1;
2318 goto leave;
2319 }
Johannes Schindelin789b3ef2017-03-23 16:07:112320 flags |= AMEND_MSG;
Johannes Schindelin6e98de72017-01-02 15:27:072321 if (!final_fixup)
2322 msg_file = rebase_path_squash_msg();
2323 else if (file_exists(rebase_path_fixup_msg())) {
Johannes Schindelinf7d42ce2021-01-28 16:16:422324 flags |= VERBATIM_MSG;
Johannes Schindelin6e98de72017-01-02 15:27:072325 msg_file = rebase_path_fixup_msg();
2326 } else {
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:562327 const char *dest = git_path_squash_msg(r);
Johannes Schindelin6e98de72017-01-02 15:27:072328 unlink(dest);
Ævar Arnfjörð Bjarmasona5792e92023-02-06 19:08:112329 if (copy_file(dest, rebase_path_squash_msg(), 0666)) {
Oswald Buddenhagen82af2c62023-09-03 15:11:322330 res = error(_("could not copy '%s' to '%s'"),
Ævar Arnfjörð Bjarmasona5792e92023-02-06 19:08:112331 rebase_path_squash_msg(), dest);
2332 goto leave;
2333 }
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:562334 unlink(git_path_merge_msg(r));
Johannes Schindelin6e98de72017-01-02 15:27:072335 msg_file = dest;
Johannes Schindelin789b3ef2017-03-23 16:07:112336 flags |= EDIT_MSG;
Johannes Schindelin6e98de72017-01-02 15:27:072337 }
2338 }
2339
Phillip Wooda852ec72018-03-20 11:10:552340 if (opts->signoff && !is_fixup(command))
Phillip Woodb34eeea2017-11-24 11:07:552341 append_signoff(&msgbuf, 0, 0);
2342
Johannes Schindelin0473f282017-01-02 15:27:182343 if (is_rebase_i(opts) && write_author_script(msg.message) < 0)
2344 res = -1;
Elijah Newren14c45862020-11-02 23:45:342345 else if (!opts->strategy ||
2346 !strcmp(opts->strategy, "recursive") ||
2347 !strcmp(opts->strategy, "ort") ||
2348 command == TODO_REVERT) {
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:562349 res = do_recursive_merge(r, base, next, base_label, next_label,
brian m. carlson48be4c62017-05-06 22:10:362350 &head, &msgbuf, opts);
Johannes Schindelinf241ff02016-07-26 16:06:022351 if (res < 0)
Stefan Beller19517fb2018-06-01 20:01:452352 goto leave;
2353
Johannes Schindelin75871492016-10-21 12:26:052354 res |= write_message(msgbuf.buf, msgbuf.len,
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:562355 git_path_merge_msg(r), 0);
Ramkumar Ramachandra043a4492012-01-11 18:15:572356 } else {
2357 struct commit_list *common = NULL;
2358 struct commit_list *remotes = NULL;
2359
Johannes Schindelin75871492016-10-21 12:26:052360 res = write_message(msgbuf.buf, msgbuf.len,
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:562361 git_path_merge_msg(r), 0);
Ramkumar Ramachandra043a4492012-01-11 18:15:572362
2363 commit_list_insert(base, &common);
2364 commit_list_insert(next, &remotes);
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:562365 res |= try_merge_command(r, opts->strategy,
Phillip Woodfb60b9f2023-04-10 09:08:282366 opts->xopts.nr, opts->xopts.v,
brian m. carlsonace976b2017-05-06 22:10:322367 common, oid_to_hex(&head), remotes);
Ramkumar Ramachandra043a4492012-01-11 18:15:572368 free_commit_list(common);
2369 free_commit_list(remotes);
2370 }
Ramkumar Ramachandra043a4492012-01-11 18:15:572371
2372 /*
2373 * If the merge was clean or if it failed due to conflict, we write
2374 * CHERRY_PICK_HEAD for the subsequent invocation of commit to use.
2375 * However, if the merge did not even start, then we don't want to
2376 * write it at all.
2377 */
Phillip Wood21b11c62019-12-06 16:06:092378 if ((command == TODO_PICK || command == TODO_REWORD ||
2379 command == TODO_EDIT) && !opts->no_commit &&
2380 (res == 0 || res == 1) &&
brian m. carlsonae077772017-10-15 22:06:512381 update_ref(NULL, "CHERRY_PICK_HEAD", &commit->object.oid, NULL,
Michael Haggerty91774af2017-11-05 08:42:062382 REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR))
Johannes Schindelindbfad032016-08-26 13:47:142383 res = -1;
Johannes Schindelin004fefa2016-10-21 12:24:412384 if (command == TODO_REVERT && ((opts->no_commit && res == 0) || res == 1) &&
brian m. carlsonae077772017-10-15 22:06:512385 update_ref(NULL, "REVERT_HEAD", &commit->object.oid, NULL,
Michael Haggerty91774af2017-11-05 08:42:062386 REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR))
Johannes Schindelindbfad032016-08-26 13:47:142387 res = -1;
Ramkumar Ramachandra043a4492012-01-11 18:15:572388
2389 if (res) {
Johannes Schindelin004fefa2016-10-21 12:24:412390 error(command == TODO_REVERT
Ramkumar Ramachandra043a4492012-01-11 18:15:572391 ? _("could not revert %s... %s")
2392 : _("could not apply %s... %s"),
Jeff Kingcb646ff2023-08-29 23:43:392393 short_commit_name(r, commit), msg.subject);
Nguyễn Thái Ngọc Duy005af332018-11-10 05:48:572394 print_advice(r, res == 1, opts);
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:562395 repo_rerere(r, opts->allow_rerere_auto);
Felipe Contrerasc8d13512013-05-29 03:56:212396 goto leave;
Ramkumar Ramachandra043a4492012-01-11 18:15:572397 }
2398
Elijah Newrene98c4262020-02-15 21:36:252399 drop_commit = 0;
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:562400 allow = allow_empty(r, opts, commit);
Felipe Contreras706728a2013-06-06 08:58:572401 if (allow < 0) {
2402 res = allow;
2403 goto leave;
Elijah Newrene98c4262020-02-15 21:36:252404 } else if (allow == 1) {
Johannes Schindelin789b3ef2017-03-23 16:07:112405 flags |= ALLOW_EMPTY;
Elijah Newrene98c4262020-02-15 21:36:252406 } else if (allow == 2) {
2407 drop_commit = 1;
Han-Wen Nienhuysc8e41592020-08-21 16:59:352408 refs_delete_ref(get_main_ref_store(r), "", "CHERRY_PICK_HEAD",
2409 NULL, 0);
Elijah Newren9a1b7472020-03-11 15:30:222410 unlink(git_path_merge_msg(r));
Elijah Newren52918282021-03-20 00:03:522411 unlink(git_path_auto_merge(r));
Elijah Newrene98c4262020-02-15 21:36:252412 fprintf(stderr,
2413 _("dropping %s %s -- patch contents already upstream\n"),
2414 oid_to_hex(&commit->object.oid), msg.subject);
2415 } /* else allow == 0 and there's nothing special to do */
2416 if (!opts->no_commit && !drop_commit) {
Phillip Wood356ee462017-11-24 11:07:572417 if (author || command == TODO_REVERT || (flags & AMEND_MSG))
Phillip Wood430b75f2019-12-06 16:06:122418 res = do_commit(r, msg_file, author, opts, flags,
2419 commit? &commit->object.oid : NULL);
Phillip Wood356ee462017-11-24 11:07:572420 else
2421 res = error(_("unable to parse commit author"));
Phillip Wooda47ba3c2019-08-19 09:18:222422 *check_todo = !!(flags & EDIT_MSG);
2423 if (!res && reword) {
Phillip Wood450efe22019-08-19 09:18:212424fast_forward_edit:
Jeff King20f4b042020-09-30 12:29:312425 res = run_git_commit(NULL, opts, EDIT_MSG |
Phillip Wood450efe22019-08-19 09:18:212426 VERIFY_MSG | AMEND_MSG |
2427 (flags & ALLOW_EMPTY));
Phillip Wooda47ba3c2019-08-19 09:18:222428 *check_todo = 1;
2429 }
Phillip Wood356ee462017-11-24 11:07:572430 }
Johannes Schindelin6e98de72017-01-02 15:27:072431
Phillip Wood450efe22019-08-19 09:18:212432
Johannes Schindelin6e98de72017-01-02 15:27:072433 if (!res && final_fixup) {
2434 unlink(rebase_path_fixup_msg());
2435 unlink(rebase_path_squash_msg());
Johannes Schindeline12a7ef2018-04-27 20:48:212436 unlink(rebase_path_current_fixups());
2437 strbuf_reset(&opts->current_fixups);
2438 opts->current_fixup_count = 0;
Johannes Schindelin6e98de72017-01-02 15:27:072439 }
Felipe Contrerasc8d13512013-05-29 03:56:212440
2441leave:
Jeff Kingd74a4e52014-06-10 21:39:352442 free_message(commit, &msg);
Phillip Wood356ee462017-11-24 11:07:572443 free(author);
Ævar Arnfjörð Bjarmasona5792e92023-02-06 19:08:112444 strbuf_release(&msgbuf);
Stephan Beyer1e412292016-12-07 21:51:322445 update_abort_safety_file();
Ramkumar Ramachandra043a4492012-01-11 18:15:572446
2447 return res;
2448}
2449
Johannes Schindelinc3e86182016-09-09 14:37:182450static int prepare_revs(struct replay_opts *opts)
Ramkumar Ramachandra043a4492012-01-11 18:15:572451{
Martin von Zweigbergka73e22e2012-08-29 06:15:562452 /*
2453 * picking (but not reverting) ranges (but not individual revisions)
2454 * should be done in reverse
2455 */
2456 if (opts->action == REPLAY_PICK && !opts->revs->no_walk)
Ramkumar Ramachandra043a4492012-01-11 18:15:572457 opts->revs->reverse ^= 1;
2458
2459 if (prepare_revision_walk(opts->revs))
Johannes Schindelinc3e86182016-09-09 14:37:182460 return error(_("revision walk setup failed"));
Ramkumar Ramachandra043a4492012-01-11 18:15:572461
Johannes Schindelinc3e86182016-09-09 14:37:182462 return 0;
Ramkumar Ramachandra043a4492012-01-11 18:15:572463}
2464
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:562465static int read_and_refresh_cache(struct repository *r,
2466 struct replay_opts *opts)
Ramkumar Ramachandra043a4492012-01-11 18:15:572467{
Martin Ågren14bca6c2018-02-27 21:30:092468 struct lock_file index_lock = LOCK_INIT;
Nguyễn Thái Ngọc Duy3a95f312019-01-12 02:13:242469 int index_fd = repo_hold_locked_index(r, &index_lock, 0);
2470 if (repo_read_index(r) < 0) {
Johannes Schindelin49fb9372016-09-09 14:38:202471 rollback_lock_file(&index_lock);
Johannes Schindelin0d9c6dc2016-09-09 14:37:212472 return error(_("git %s: failed to read the index"),
Michael J Gruber629444a2022-08-18 13:13:282473 action_name(opts));
Johannes Schindelin49fb9372016-09-09 14:38:202474 }
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:562475 refresh_index(r->index, REFRESH_QUIET|REFRESH_UNMERGED, NULL, NULL, NULL);
Derrick Stolee5d9c9342021-09-08 11:24:002476
Martin Ågren61000812018-03-01 20:40:202477 if (index_fd >= 0) {
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:562478 if (write_locked_index(r->index, &index_lock,
Martin Ågren61000812018-03-01 20:40:202479 COMMIT_LOCK | SKIP_IF_UNCHANGED)) {
Johannes Schindelin0d9c6dc2016-09-09 14:37:212480 return error(_("git %s: failed to refresh the index"),
Michael J Gruber629444a2022-08-18 13:13:282481 action_name(opts));
Johannes Schindelin49fb9372016-09-09 14:38:202482 }
Ramkumar Ramachandra043a4492012-01-11 18:15:572483 }
Derrick Stolee5d9c9342021-09-08 11:24:002484
2485 /*
2486 * If we are resolving merges in any way other than "ort", then
2487 * expand the sparse index.
2488 */
2489 if (opts->strategy && strcmp(opts->strategy, "ort"))
2490 ensure_full_index(r->index);
Johannes Schindelin0d9c6dc2016-09-09 14:37:212491 return 0;
Ramkumar Ramachandra043a4492012-01-11 18:15:572492}
2493
Alban Gruin5d94d542018-12-29 16:03:592494void todo_list_release(struct todo_list *todo_list)
Johannes Schindelin004fefa2016-10-21 12:24:412495{
2496 strbuf_release(&todo_list->buf);
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:462497 FREE_AND_NULL(todo_list->items);
Johannes Schindelin004fefa2016-10-21 12:24:412498 todo_list->nr = todo_list->alloc = 0;
Ramkumar Ramachandra043a4492012-01-11 18:15:572499}
2500
Johannes Schindelin004fefa2016-10-21 12:24:412501static struct todo_item *append_new_todo(struct todo_list *todo_list)
2502{
2503 ALLOC_GROW(todo_list->items, todo_list->nr + 1, todo_list->alloc);
Johannes Schindelin004fefa2016-10-21 12:24:412504 return todo_list->items + todo_list->nr++;
2505}
2506
Alban Gruin6ad656d2019-01-29 15:01:462507const char *todo_item_get_arg(struct todo_list *todo_list,
2508 struct todo_item *item)
2509{
2510 return todo_list->buf.buf + item->arg_offset;
2511}
2512
Phillip Wood3e81bcc2019-06-27 14:12:452513static int is_command(enum todo_command command, const char **bol)
2514{
2515 const char *str = todo_command_info[command].str;
2516 const char nick = todo_command_info[command].c;
Phillip Wood7aed2c02023-02-23 20:55:002517 const char *p = *bol;
Phillip Wood3e81bcc2019-06-27 14:12:452518
Phillip Wood7aed2c02023-02-23 20:55:002519 return (skip_prefix(p, str, &p) || (nick && *p++ == nick)) &&
2520 (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r' || !*p) &&
2521 (*bol = p);
Phillip Wood3e81bcc2019-06-27 14:12:452522}
2523
Phillip Wood16b38802023-02-20 14:19:342524static int check_label_or_ref_arg(enum todo_command command, const char *arg)
2525{
2526 switch (command) {
2527 case TODO_LABEL:
2528 /*
2529 * '#' is not a valid label as the merge command uses it to
2530 * separate merge parents from the commit subject.
2531 */
2532 if (!strcmp(arg, "#") ||
2533 check_refname_format(arg, REFNAME_ALLOW_ONELEVEL))
2534 return error(_("'%s' is not a valid label"), arg);
2535 break;
2536
2537 case TODO_UPDATE_REF:
2538 if (check_refname_format(arg, REFNAME_ALLOW_ONELEVEL))
2539 return error(_("'%s' is not a valid refname"), arg);
2540 if (check_refname_format(arg, 0))
2541 return error(_("update-ref requires a fully qualified "
2542 "refname e.g. refs/heads/%s"), arg);
2543 break;
2544
2545 default:
2546 BUG("unexpected todo_command");
2547 }
2548
2549 return 0;
2550}
2551
Nguyễn Thái Ngọc Duy005af332018-11-10 05:48:572552static int parse_insn_line(struct repository *r, struct todo_item *item,
Alban Gruin6ad656d2019-01-29 15:01:462553 const char *buf, const char *bol, char *eol)
Ramkumar Ramachandra043a4492012-01-11 18:15:572554{
brian m. carlson1e43ed92017-05-06 22:10:092555 struct object_id commit_oid;
Ramkumar Ramachandra043a4492012-01-11 18:15:572556 char *end_of_object_name;
Johannes Schindelin004fefa2016-10-21 12:24:412557 int i, saved, status, padding;
Ramkumar Ramachandra043a4492012-01-11 18:15:572558
Johannes Schindelin4c68e7d2018-04-25 12:28:542559 item->flags = 0;
2560
Johannes Schindelin8f8550b2016-10-21 12:25:362561 /* left-trim */
2562 bol += strspn(bol, " \t");
2563
Johannes Schindelin25c43662017-01-02 15:26:382564 if (bol == eol || *bol == '\r' || *bol == comment_line_char) {
Johannes Schindelinac191472017-01-02 15:34:392565 item->command = TODO_COMMENT;
Johannes Schindelin25c43662017-01-02 15:26:382566 item->commit = NULL;
Alban Gruin6ad656d2019-01-29 15:01:462567 item->arg_offset = bol - buf;
Johannes Schindelin25c43662017-01-02 15:26:382568 item->arg_len = eol - bol;
2569 return 0;
2570 }
2571
Johannes Schindelinac191472017-01-02 15:34:392572 for (i = 0; i < TODO_COMMENT; i++)
Phillip Wood3e81bcc2019-06-27 14:12:452573 if (is_command(i, &bol)) {
Johannes Schindelin004fefa2016-10-21 12:24:412574 item->command = i;
2575 break;
2576 }
Johannes Schindelinac191472017-01-02 15:34:392577 if (i >= TODO_COMMENT)
Phillip Wood7aed2c02023-02-23 20:55:002578 return error(_("invalid command '%.*s'"),
2579 (int)strcspn(bol, " \t\r\n"), bol);
Ramkumar Ramachandra043a4492012-01-11 18:15:572580
Johannes Schindelin66afa242017-12-22 23:55:572581 /* Eat up extra spaces/ tabs before object name */
2582 padding = strspn(bol, " \t");
2583 bol += padding;
2584
Johannes Schindelin71f82462018-10-12 13:14:262585 if (item->command == TODO_NOOP || item->command == TODO_BREAK) {
Johannes Schindelin66afa242017-12-22 23:55:572586 if (bol != eol)
2587 return error(_("%s does not accept arguments: '%s'"),
2588 command_to_string(item->command), bol);
Johannes Schindelin25c43662017-01-02 15:26:382589 item->commit = NULL;
Alban Gruin6ad656d2019-01-29 15:01:462590 item->arg_offset = bol - buf;
Johannes Schindelin25c43662017-01-02 15:26:382591 item->arg_len = eol - bol;
2592 return 0;
2593 }
2594
Ramkumar Ramachandra043a4492012-01-11 18:15:572595 if (!padding)
Johannes Schindelin66afa242017-12-22 23:55:572596 return error(_("missing arguments for %s"),
2597 command_to_string(item->command));
Ramkumar Ramachandra043a4492012-01-11 18:15:572598
Johannes Schindelin9055e402018-04-25 12:28:472599 if (item->command == TODO_EXEC || item->command == TODO_LABEL ||
Derrick Stoleea97d7912022-07-19 18:33:382600 item->command == TODO_RESET || item->command == TODO_UPDATE_REF) {
Phillip Wood16b38802023-02-20 14:19:342601 int ret = 0;
2602
Liam Beguin7dcbb3c2017-12-03 22:17:152603 item->commit = NULL;
Alban Gruin6ad656d2019-01-29 15:01:462604 item->arg_offset = bol - buf;
Johannes Schindelin311af522017-01-02 15:26:472605 item->arg_len = (int)(eol - bol);
Phillip Wood16b38802023-02-20 14:19:342606 if (item->command == TODO_LABEL ||
2607 item->command == TODO_UPDATE_REF) {
2608 saved = *eol;
2609 *eol = '\0';
2610 ret = check_label_or_ref_arg(item->command, bol);
2611 *eol = saved;
2612 }
2613 return ret;
Johannes Schindelin311af522017-01-02 15:26:472614 }
2615
Charvi Mendiratta9e3cebd2021-01-29 18:20:472616 if (item->command == TODO_FIXUP) {
Phillip Wood666b6e12023-02-23 20:55:012617 if (skip_prefix(bol, "-C", &bol)) {
Charvi Mendiratta9e3cebd2021-01-29 18:20:472618 bol += strspn(bol, " \t");
2619 item->flags |= TODO_REPLACE_FIXUP_MSG;
Phillip Wood666b6e12023-02-23 20:55:012620 } else if (skip_prefix(bol, "-c", &bol)) {
Charvi Mendiratta9e3cebd2021-01-29 18:20:472621 bol += strspn(bol, " \t");
2622 item->flags |= TODO_EDIT_FIXUP_MSG;
2623 }
2624 }
2625
Johannes Schindelin4c68e7d2018-04-25 12:28:542626 if (item->command == TODO_MERGE) {
2627 if (skip_prefix(bol, "-C", &bol))
2628 bol += strspn(bol, " \t");
2629 else if (skip_prefix(bol, "-c", &bol)) {
2630 bol += strspn(bol, " \t");
2631 item->flags |= TODO_EDIT_MERGE_MSG;
2632 } else {
2633 item->flags |= TODO_EDIT_MERGE_MSG;
2634 item->commit = NULL;
Alban Gruin6ad656d2019-01-29 15:01:462635 item->arg_offset = bol - buf;
Johannes Schindelin4c68e7d2018-04-25 12:28:542636 item->arg_len = (int)(eol - bol);
2637 return 0;
2638 }
2639 }
2640
Johannes Schindelin004fefa2016-10-21 12:24:412641 end_of_object_name = (char *) bol + strcspn(bol, " \t\n");
Ramkumar Ramachandra043a4492012-01-11 18:15:572642 saved = *end_of_object_name;
2643 *end_of_object_name = '\0';
Ævar Arnfjörð Bjarmason4a93b892023-03-28 13:58:582644 status = repo_get_oid(r, bol, &commit_oid);
Johannes Schindelind859dca2020-01-23 12:28:172645 if (status < 0)
2646 error(_("could not parse '%s'"), bol); /* return later */
Ramkumar Ramachandra043a4492012-01-11 18:15:572647 *end_of_object_name = saved;
2648
Alban Gruin6ad656d2019-01-29 15:01:462649 bol = end_of_object_name + strspn(end_of_object_name, " \t");
2650 item->arg_offset = bol - buf;
2651 item->arg_len = (int)(eol - bol);
Johannes Schindelinc22f7df2016-10-21 12:25:002652
Ramkumar Ramachandra043a4492012-01-11 18:15:572653 if (status < 0)
Johannes Schindelind859dca2020-01-23 12:28:172654 return status;
Ramkumar Ramachandra043a4492012-01-11 18:15:572655
Nguyễn Thái Ngọc Duy005af332018-11-10 05:48:572656 item->commit = lookup_commit_reference(r, &commit_oid);
Johannes Schindelind859dca2020-01-23 12:28:172657 return item->commit ? 0 : -1;
Ramkumar Ramachandra043a4492012-01-11 18:15:572658}
2659
Jeff Kingc9f7b1e2023-08-29 23:44:232660int sequencer_get_last_command(struct repository *r UNUSED, enum replay_action *action)
Phillip Wood4a724862019-04-16 10:18:422661{
Phillip Wooded5b1ca2019-06-27 14:12:462662 const char *todo_file, *bol;
Phillip Wood4a724862019-04-16 10:18:422663 struct strbuf buf = STRBUF_INIT;
Phillip Wooded5b1ca2019-06-27 14:12:462664 int ret = 0;
Phillip Wood4a724862019-04-16 10:18:422665
2666 todo_file = git_path_todo_file();
2667 if (strbuf_read_file(&buf, todo_file, 0) < 0) {
Phillip Wooded5b1ca2019-06-27 14:12:462668 if (errno == ENOENT || errno == ENOTDIR)
Phillip Wood4a724862019-04-16 10:18:422669 return -1;
2670 else
2671 return error_errno("unable to open '%s'", todo_file);
2672 }
Phillip Wooded5b1ca2019-06-27 14:12:462673 bol = buf.buf + strspn(buf.buf, " \t\r\n");
2674 if (is_command(TODO_PICK, &bol) && (*bol == ' ' || *bol == '\t'))
Phillip Wood4a724862019-04-16 10:18:422675 *action = REPLAY_PICK;
Phillip Wooded5b1ca2019-06-27 14:12:462676 else if (is_command(TODO_REVERT, &bol) &&
2677 (*bol == ' ' || *bol == '\t'))
Phillip Wood4a724862019-04-16 10:18:422678 *action = REPLAY_REVERT;
2679 else
Phillip Wooded5b1ca2019-06-27 14:12:462680 ret = -1;
Phillip Wood4a724862019-04-16 10:18:422681
Phillip Wood4a724862019-04-16 10:18:422682 strbuf_release(&buf);
2683
2684 return ret;
2685}
2686
Alban Gruin5d94d542018-12-29 16:03:592687int todo_list_parse_insn_buffer(struct repository *r, char *buf,
2688 struct todo_list *todo_list)
Ramkumar Ramachandra043a4492012-01-11 18:15:572689{
Johannes Schindelin004fefa2016-10-21 12:24:412690 struct todo_item *item;
2691 char *p = buf, *next_p;
Johannes Schindelin6e98de72017-01-02 15:27:072692 int i, res = 0, fixup_okay = file_exists(rebase_path_done());
Ramkumar Ramachandra043a4492012-01-11 18:15:572693
Johannes Schindelin170eea92023-05-13 08:11:262694 todo_list->current = todo_list->nr = todo_list->total_nr = 0;
Alban Gruin2b715952018-12-29 16:03:582695
Johannes Schindelin004fefa2016-10-21 12:24:412696 for (i = 1; *p; i++, p = next_p) {
Ramkumar Ramachandra043a4492012-01-11 18:15:572697 char *eol = strchrnul(p, '\n');
Johannes Schindelin004fefa2016-10-21 12:24:412698
2699 next_p = *eol ? eol + 1 /* skip LF */ : eol;
2700
Johannes Schindelin63070412016-10-21 12:24:462701 if (p != eol && eol[-1] == '\r')
2702 eol--; /* strip Carriage Return */
2703
Johannes Schindelin004fefa2016-10-21 12:24:412704 item = append_new_todo(todo_list);
2705 item->offset_in_buf = p - todo_list->buf.buf;
Alban Gruin6ad656d2019-01-29 15:01:462706 if (parse_insn_line(r, item, buf, p, eol)) {
Johannes Schindelin93b3df62016-10-21 12:26:252707 res = error(_("invalid line %d: %.*s"),
Johannes Schindelin004fefa2016-10-21 12:24:412708 i, (int)(eol - p), p);
Alban Gruin2b715952018-12-29 16:03:582709 item->command = TODO_COMMENT + 1;
Alban Gruin6ad656d2019-01-29 15:01:462710 item->arg_offset = p - buf;
Alban Gruin2b715952018-12-29 16:03:582711 item->arg_len = (int)(eol - p);
2712 item->commit = NULL;
Johannes Schindelin004fefa2016-10-21 12:24:412713 }
Johannes Schindelin6e98de72017-01-02 15:27:072714
Johannes Schindelin170eea92023-05-13 08:11:262715 if (item->command != TODO_COMMENT)
2716 todo_list->total_nr++;
2717
Johannes Schindelin6e98de72017-01-02 15:27:072718 if (fixup_okay)
2719 ; /* do nothing */
2720 else if (is_fixup(item->command))
Alex Henrie9645a082023-07-22 21:28:252721 res = error(_("cannot '%s' without a previous commit"),
Johannes Schindelin6e98de72017-01-02 15:27:072722 command_to_string(item->command));
2723 else if (!is_noop(item->command))
2724 fixup_okay = 1;
Ramkumar Ramachandra043a4492012-01-11 18:15:572725 }
Johannes Schindelin52865272017-01-02 15:27:342726
Johannes Schindelin004fefa2016-10-21 12:24:412727 return res;
Ramkumar Ramachandra043a4492012-01-11 18:15:572728}
2729
Johannes Schindelin968492e2017-01-02 15:35:462730static int count_commands(struct todo_list *todo_list)
2731{
2732 int count = 0, i;
2733
2734 for (i = 0; i < todo_list->nr; i++)
2735 if (todo_list->items[i].command != TODO_COMMENT)
2736 count++;
2737
2738 return count;
2739}
2740
Johannes Schindelina01c2a52018-04-25 12:28:292741static int get_item_line_offset(struct todo_list *todo_list, int index)
2742{
2743 return index < todo_list->nr ?
2744 todo_list->items[index].offset_in_buf : todo_list->buf.len;
2745}
2746
2747static const char *get_item_line(struct todo_list *todo_list, int index)
2748{
2749 return todo_list->buf.buf + get_item_line_offset(todo_list, index);
2750}
2751
2752static int get_item_line_length(struct todo_list *todo_list, int index)
2753{
2754 return get_item_line_offset(todo_list, index + 1)
2755 - get_item_line_offset(todo_list, index);
2756}
2757
René Scharfe87805602018-02-22 19:29:252758static ssize_t strbuf_read_file_or_whine(struct strbuf *sb, const char *path)
2759{
2760 int fd;
2761 ssize_t len;
2762
2763 fd = open(path, O_RDONLY);
2764 if (fd < 0)
2765 return error_errno(_("could not open '%s'"), path);
2766 len = strbuf_read(sb, fd, 0);
2767 close(fd);
2768 if (len < 0)
2769 return error(_("could not read '%s'."), path);
2770 return len;
2771}
2772
Phillip Woodb07d9bf2019-04-16 10:18:412773static int have_finished_the_last_pick(void)
2774{
2775 struct strbuf buf = STRBUF_INIT;
2776 const char *eol;
2777 const char *todo_path = git_path_todo_file();
2778 int ret = 0;
2779
2780 if (strbuf_read_file(&buf, todo_path, 0) < 0) {
2781 if (errno == ENOENT) {
2782 return 0;
2783 } else {
2784 error_errno("unable to open '%s'", todo_path);
2785 return 0;
2786 }
2787 }
2788 /* If there is only one line then we are done */
2789 eol = strchr(buf.buf, '\n');
2790 if (!eol || !eol[1])
2791 ret = 1;
2792
2793 strbuf_release(&buf);
2794
2795 return ret;
2796}
2797
Junio C Hamanof496b062019-07-09 22:25:442798void sequencer_post_commit_cleanup(struct repository *r, int verbose)
Phillip Woodb07d9bf2019-04-16 10:18:412799{
2800 struct replay_opts opts = REPLAY_OPTS_INIT;
2801 int need_cleanup = 0;
2802
Han-Wen Nienhuysc8e41592020-08-21 16:59:352803 if (refs_ref_exists(get_main_ref_store(r), "CHERRY_PICK_HEAD")) {
2804 if (!refs_delete_ref(get_main_ref_store(r), "",
2805 "CHERRY_PICK_HEAD", NULL, 0) &&
2806 verbose)
Junio C Hamanof496b062019-07-09 22:25:442807 warning(_("cancelling a cherry picking in progress"));
Phillip Woodb07d9bf2019-04-16 10:18:412808 opts.action = REPLAY_PICK;
2809 need_cleanup = 1;
2810 }
2811
Han-Wen Nienhuysb8825ef2020-08-21 16:59:372812 if (refs_ref_exists(get_main_ref_store(r), "REVERT_HEAD")) {
2813 if (!refs_delete_ref(get_main_ref_store(r), "", "REVERT_HEAD",
2814 NULL, 0) &&
2815 verbose)
Junio C Hamanof496b062019-07-09 22:25:442816 warning(_("cancelling a revert in progress"));
Phillip Woodb07d9bf2019-04-16 10:18:412817 opts.action = REPLAY_REVERT;
2818 need_cleanup = 1;
2819 }
2820
Elijah Newren52918282021-03-20 00:03:522821 unlink(git_path_auto_merge(r));
2822
Phillip Woodb07d9bf2019-04-16 10:18:412823 if (!need_cleanup)
2824 return;
2825
2826 if (!have_finished_the_last_pick())
2827 return;
2828
2829 sequencer_remove_state(&opts);
2830}
2831
Alban Gruin3f34f2d2019-11-24 17:43:302832static void todo_list_write_total_nr(struct todo_list *todo_list)
2833{
2834 FILE *f = fopen_or_warn(rebase_path_msgtotal(), "w");
2835
2836 if (f) {
2837 fprintf(f, "%d\n", todo_list->total_nr);
2838 fclose(f);
2839 }
2840}
2841
Nguyễn Thái Ngọc Duy005af332018-11-10 05:48:572842static int read_populate_todo(struct repository *r,
2843 struct todo_list *todo_list,
2844 struct replay_opts *opts)
Ramkumar Ramachandra043a4492012-01-11 18:15:572845{
Johannes Schindelinc0246502016-10-21 12:24:322846 const char *todo_file = get_todo_path(opts);
René Scharfe87805602018-02-22 19:29:252847 int res;
Ramkumar Ramachandra043a4492012-01-11 18:15:572848
Johannes Schindelin004fefa2016-10-21 12:24:412849 strbuf_reset(&todo_list->buf);
René Scharfe87805602018-02-22 19:29:252850 if (strbuf_read_file_or_whine(&todo_list->buf, todo_file) < 0)
2851 return -1;
Ramkumar Ramachandra043a4492012-01-11 18:15:572852
Alban Gruin5d94d542018-12-29 16:03:592853 res = todo_list_parse_insn_buffer(r, todo_list->buf.buf, todo_list);
Johannes Schindelin27fdbb92017-01-02 15:35:422854 if (res) {
2855 if (is_rebase_i(opts))
2856 return error(_("please fix this using "
2857 "'git rebase --edit-todo'."));
Johannes Schindelin93b3df62016-10-21 12:26:252858 return error(_("unusable instruction sheet: '%s'"), todo_file);
Johannes Schindelin27fdbb92017-01-02 15:35:422859 }
Johannes Schindelin2eeaf1b2016-10-21 12:26:132860
Johannes Schindelin52865272017-01-02 15:27:342861 if (!todo_list->nr &&
2862 (!is_rebase_i(opts) || !file_exists(rebase_path_done())))
2863 return error(_("no commits parsed."));
2864
Johannes Schindelin2eeaf1b2016-10-21 12:26:132865 if (!is_rebase_i(opts)) {
Johannes Schindelin004fefa2016-10-21 12:24:412866 enum todo_command valid =
2867 opts->action == REPLAY_PICK ? TODO_PICK : TODO_REVERT;
2868 int i;
2869
2870 for (i = 0; i < todo_list->nr; i++)
2871 if (valid == todo_list->items[i].command)
2872 continue;
2873 else if (valid == TODO_PICK)
Johannes Schindelin93b3df62016-10-21 12:26:252874 return error(_("cannot cherry-pick during a revert."));
Johannes Schindelin004fefa2016-10-21 12:24:412875 else
Johannes Schindelin93b3df62016-10-21 12:26:252876 return error(_("cannot revert during a cherry-pick."));
Johannes Schindelin004fefa2016-10-21 12:24:412877 }
2878
Johannes Schindelin968492e2017-01-02 15:35:462879 if (is_rebase_i(opts)) {
2880 struct todo_list done = TODO_LIST_INIT;
2881
2882 if (strbuf_read_file(&done.buf, rebase_path_done(), 0) > 0 &&
Alban Gruin5d94d542018-12-29 16:03:592883 !todo_list_parse_insn_buffer(r, done.buf.buf, &done))
Johannes Schindelin968492e2017-01-02 15:35:462884 todo_list->done_nr = count_commands(&done);
2885 else
2886 todo_list->done_nr = 0;
2887
2888 todo_list->total_nr = todo_list->done_nr
2889 + count_commands(todo_list);
Johannes Schindelin968492e2017-01-02 15:35:462890 todo_list_release(&done);
Johannes Schindelinef800692017-01-02 15:36:202891
Alban Gruin3f34f2d2019-11-24 17:43:302892 todo_list_write_total_nr(todo_list);
Johannes Schindelin968492e2017-01-02 15:35:462893 }
2894
Johannes Schindelin0ae42a02016-09-09 14:37:242895 return 0;
Ramkumar Ramachandra043a4492012-01-11 18:15:572896}
2897
Johannes Schindelin03a4e262016-10-21 12:24:132898static int git_config_string_dup(char **dest,
2899 const char *var, const char *value)
2900{
2901 if (!value)
2902 return config_error_nonbool(var);
2903 free(*dest);
2904 *dest = xstrdup(value);
2905 return 0;
2906}
2907
Glen Chooa4e7e312023-06-28 19:26:222908static int populate_opts_cb(const char *key, const char *value,
Glen Choo8868b1e2023-06-28 19:26:272909 const struct config_context *ctx,
Glen Chooa4e7e312023-06-28 19:26:222910 void *data)
Ramkumar Ramachandra043a4492012-01-11 18:15:572911{
2912 struct replay_opts *opts = data;
2913 int error_flag = 1;
2914
2915 if (!value)
2916 error_flag = 0;
2917 else if (!strcmp(key, "options.no-commit"))
Glen Choo8868b1e2023-06-28 19:26:272918 opts->no_commit = git_config_bool_or_int(key, value, ctx->kvi, &error_flag);
Ramkumar Ramachandra043a4492012-01-11 18:15:572919 else if (!strcmp(key, "options.edit"))
Glen Choo8868b1e2023-06-28 19:26:272920 opts->edit = git_config_bool_or_int(key, value, ctx->kvi, &error_flag);
Phillip Wood6860ce52019-03-13 18:26:152921 else if (!strcmp(key, "options.allow-empty"))
2922 opts->allow_empty =
Glen Choo8868b1e2023-06-28 19:26:272923 git_config_bool_or_int(key, value, ctx->kvi, &error_flag);
Phillip Wood6860ce52019-03-13 18:26:152924 else if (!strcmp(key, "options.allow-empty-message"))
2925 opts->allow_empty_message =
Glen Choo8868b1e2023-06-28 19:26:272926 git_config_bool_or_int(key, value, ctx->kvi, &error_flag);
Phillip Wood6860ce52019-03-13 18:26:152927 else if (!strcmp(key, "options.keep-redundant-commits"))
2928 opts->keep_redundant_commits =
Glen Choo8868b1e2023-06-28 19:26:272929 git_config_bool_or_int(key, value, ctx->kvi, &error_flag);
Ramkumar Ramachandra043a4492012-01-11 18:15:572930 else if (!strcmp(key, "options.signoff"))
Glen Choo8868b1e2023-06-28 19:26:272931 opts->signoff = git_config_bool_or_int(key, value, ctx->kvi, &error_flag);
Ramkumar Ramachandra043a4492012-01-11 18:15:572932 else if (!strcmp(key, "options.record-origin"))
Glen Choo8868b1e2023-06-28 19:26:272933 opts->record_origin = git_config_bool_or_int(key, value, ctx->kvi, &error_flag);
Ramkumar Ramachandra043a4492012-01-11 18:15:572934 else if (!strcmp(key, "options.allow-ff"))
Glen Choo8868b1e2023-06-28 19:26:272935 opts->allow_ff = git_config_bool_or_int(key, value, ctx->kvi, &error_flag);
Ramkumar Ramachandra043a4492012-01-11 18:15:572936 else if (!strcmp(key, "options.mainline"))
Glen Choo8868b1e2023-06-28 19:26:272937 opts->mainline = git_config_int(key, value, ctx->kvi);
Ramkumar Ramachandra043a4492012-01-11 18:15:572938 else if (!strcmp(key, "options.strategy"))
Johannes Schindelin03a4e262016-10-21 12:24:132939 git_config_string_dup(&opts->strategy, key, value);
Nicolas Vigier32535532014-01-24 00:50:582940 else if (!strcmp(key, "options.gpg-sign"))
Johannes Schindelin03a4e262016-10-21 12:24:132941 git_config_string_dup(&opts->gpg_sign, key, value);
Ramkumar Ramachandra043a4492012-01-11 18:15:572942 else if (!strcmp(key, "options.strategy-option")) {
Phillip Woodfb60b9f2023-04-10 09:08:282943 strvec_push(&opts->xopts, value);
Phillip Wood8d8cb4b2017-08-02 10:44:192944 } else if (!strcmp(key, "options.allow-rerere-auto"))
2945 opts->allow_rerere_auto =
Glen Choo8868b1e2023-06-28 19:26:272946 git_config_bool_or_int(key, value, ctx->kvi, &error_flag) ?
Phillip Wood8d8cb4b2017-08-02 10:44:192947 RERERE_AUTOUPDATE : RERERE_NOAUTOUPDATE;
Phillip Wooddc42e9a2019-04-17 10:23:292948 else if (!strcmp(key, "options.default-msg-cleanup")) {
2949 opts->explicit_cleanup = 1;
2950 opts->default_msg_cleanup = get_cleanup_mode(value, 1);
2951 } else
Johannes Schindelin93b3df62016-10-21 12:26:252952 return error(_("invalid key: %s"), key);
Ramkumar Ramachandra043a4492012-01-11 18:15:572953
2954 if (!error_flag)
Jean-Noël Avila1a8aea82022-01-31 22:07:472955 return error(_("invalid value for '%s': '%s'"), key, value);
Ramkumar Ramachandra043a4492012-01-11 18:15:572956
2957 return 0;
2958}
2959
Phillip Wood4a8bc982023-04-10 09:08:292960static void parse_strategy_opts(struct replay_opts *opts, char *raw_opts)
Johannes Schindelinca6c6b42017-01-02 15:28:302961{
2962 int i;
Ævar Arnfjörð Bjarmason15a4cc92023-03-07 18:21:592963 int count;
Phillip Woodfb60b9f2023-04-10 09:08:282964 const char **argv;
Alban Gruin65850682018-08-28 12:10:402965 char *strategy_opts_string = raw_opts;
Johannes Schindelinca6c6b42017-01-02 15:28:302966
Elijah Newren00600412018-06-27 15:48:042967 if (*strategy_opts_string == ' ')
2968 strategy_opts_string++;
Alban Gruin65850682018-08-28 12:10:402969
Phillip Woodfb60b9f2023-04-10 09:08:282970 count = split_cmdline(strategy_opts_string, &argv);
Ævar Arnfjörð Bjarmason15a4cc92023-03-07 18:21:592971 if (count < 0)
Phillip Wood4960e5c2023-04-10 09:08:302972 BUG("could not split '%s': %s", strategy_opts_string,
Ævar Arnfjörð Bjarmason15a4cc92023-03-07 18:21:592973 split_cmdline_strerror(count));
Phillip Woodfb60b9f2023-04-10 09:08:282974 for (i = 0; i < count; i++) {
2975 const char *arg = argv[i];
Johannes Schindelinca6c6b42017-01-02 15:28:302976
2977 skip_prefix(arg, "--", &arg);
Phillip Woodfb60b9f2023-04-10 09:08:282978 strvec_push(&opts->xopts, arg);
Johannes Schindelinca6c6b42017-01-02 15:28:302979 }
Phillip Woodfb60b9f2023-04-10 09:08:282980 free(argv);
Johannes Schindelinca6c6b42017-01-02 15:28:302981}
2982
Alban Gruin65850682018-08-28 12:10:402983static void read_strategy_opts(struct replay_opts *opts, struct strbuf *buf)
2984{
2985 strbuf_reset(buf);
2986 if (!read_oneliner(buf, rebase_path_strategy(), 0))
2987 return;
2988 opts->strategy = strbuf_detach(buf, NULL);
2989 if (!read_oneliner(buf, rebase_path_strategy_opts(), 0))
2990 return;
2991
2992 parse_strategy_opts(opts, buf->buf);
2993}
2994
Johannes Schindelin5adf9bd2016-10-14 13:17:162995static int read_populate_opts(struct replay_opts *opts)
Ramkumar Ramachandra043a4492012-01-11 18:15:572996{
Johannes Schindelina1c75762016-10-21 12:25:122997 if (is_rebase_i(opts)) {
2998 struct strbuf buf = STRBUF_INIT;
Denton Liu65c425a2020-04-04 01:11:162999 int ret = 0;
Johannes Schindelina1c75762016-10-21 12:25:123000
Denton Liu3442c3d2020-04-07 14:27:523001 if (read_oneliner(&buf, rebase_path_gpg_sign_opt(),
3002 READ_ONELINER_SKIP_IF_EMPTY)) {
Johannes Schindelina1c75762016-10-21 12:25:123003 if (!starts_with(buf.buf, "-S"))
3004 strbuf_reset(&buf);
3005 else {
3006 free(opts->gpg_sign);
3007 opts->gpg_sign = xstrdup(buf.buf + 2);
3008 }
Phillip Wood9b6d7a62017-08-02 10:44:173009 strbuf_reset(&buf);
3010 }
3011
Denton Liu3442c3d2020-04-07 14:27:523012 if (read_oneliner(&buf, rebase_path_allow_rerere_autoupdate(),
3013 READ_ONELINER_SKIP_IF_EMPTY)) {
Phillip Wood9b6d7a62017-08-02 10:44:173014 if (!strcmp(buf.buf, "--rerere-autoupdate"))
3015 opts->allow_rerere_auto = RERERE_AUTOUPDATE;
3016 else if (!strcmp(buf.buf, "--no-rerere-autoupdate"))
3017 opts->allow_rerere_auto = RERERE_NOAUTOUPDATE;
3018 strbuf_reset(&buf);
Johannes Schindelina1c75762016-10-21 12:25:123019 }
Johannes Schindelina1c75762016-10-21 12:25:123020
Johannes Schindelin556907f2017-01-02 15:26:533021 if (file_exists(rebase_path_verbose()))
3022 opts->verbose = 1;
3023
Elijah Newren899b49c2018-12-11 16:11:363024 if (file_exists(rebase_path_quiet()))
3025 opts->quiet = 1;
3026
Phillip Wooda852ec72018-03-20 11:10:553027 if (file_exists(rebase_path_signoff())) {
3028 opts->allow_ff = 0;
3029 opts->signoff = 1;
3030 }
3031
Phillip Wood7573cec2020-08-17 17:40:023032 if (file_exists(rebase_path_cdate_is_adate())) {
3033 opts->allow_ff = 0;
3034 opts->committer_date_is_author_date = 1;
3035 }
3036
Phillip Wooda3894aa2020-08-17 17:40:033037 if (file_exists(rebase_path_ignore_date())) {
3038 opts->allow_ff = 0;
3039 opts->ignore_date = 1;
3040 }
3041
Johannes Schindelind421afa2018-12-10 19:04:583042 if (file_exists(rebase_path_reschedule_failed_exec()))
3043 opts->reschedule_failed_exec = 1;
Ævar Arnfjörð Bjarmasone5b32bf2021-04-09 08:01:383044 else if (file_exists(rebase_path_no_reschedule_failed_exec()))
3045 opts->reschedule_failed_exec = 0;
Johannes Schindelind421afa2018-12-10 19:04:583046
Elijah Newrene98c4262020-02-15 21:36:253047 if (file_exists(rebase_path_drop_redundant_commits()))
3048 opts->drop_redundant_commits = 1;
3049
3050 if (file_exists(rebase_path_keep_redundant_commits()))
3051 opts->keep_redundant_commits = 1;
3052
Johannes Schindelinca6c6b42017-01-02 15:28:303053 read_strategy_opts(opts, &buf);
Denton Liu65c425a2020-04-04 01:11:163054 strbuf_reset(&buf);
Johannes Schindelinca6c6b42017-01-02 15:28:303055
Johannes Schindeline12a7ef2018-04-27 20:48:213056 if (read_oneliner(&opts->current_fixups,
Denton Liu3442c3d2020-04-07 14:27:523057 rebase_path_current_fixups(),
3058 READ_ONELINER_SKIP_IF_EMPTY)) {
Johannes Schindeline12a7ef2018-04-27 20:48:213059 const char *p = opts->current_fixups.buf;
3060 opts->current_fixup_count = 1;
3061 while ((p = strchr(p, '\n'))) {
3062 opts->current_fixup_count++;
3063 p++;
3064 }
3065 }
3066
Johannes Schindelind87d48b2018-05-03 23:01:173067 if (read_oneliner(&buf, rebase_path_squash_onto(), 0)) {
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 13:58:463068 if (repo_get_oid_committish(the_repository, buf.buf, &opts->squash_onto) < 0) {
Denton Liu65c425a2020-04-04 01:11:163069 ret = error(_("unusable squash-onto"));
3070 goto done_rebase_i;
3071 }
Johannes Schindelind87d48b2018-05-03 23:01:173072 opts->have_squash_onto = 1;
3073 }
3074
Denton Liu65c425a2020-04-04 01:11:163075done_rebase_i:
3076 strbuf_release(&buf);
3077 return ret;
Johannes Schindelina1c75762016-10-21 12:25:123078 }
Johannes Schindelinb5a67042016-10-21 12:25:043079
Jeff Kingf9327292015-08-10 09:38:573080 if (!file_exists(git_path_opts_file()))
Johannes Schindelin0d00da72016-09-09 14:37:273081 return 0;
3082 /*
3083 * The function git_parse_source(), called from git_config_from_file(),
3084 * may die() in case of a syntactically incorrect file. We do not care
3085 * about this case, though, because we wrote that file ourselves, so we
3086 * are pretty certain that it is syntactically correct.
3087 */
Johannes Schindelin5adf9bd2016-10-14 13:17:163088 if (git_config_from_file(populate_opts_cb, git_path_opts_file(), opts) < 0)
Johannes Schindelin93b3df62016-10-21 12:26:253089 return error(_("malformed options sheet: '%s'"),
Johannes Schindelin0d00da72016-09-09 14:37:273090 git_path_opts_file());
3091 return 0;
Ramkumar Ramachandra043a4492012-01-11 18:15:573092}
3093
Alban Gruin65850682018-08-28 12:10:403094static void write_strategy_opts(struct replay_opts *opts)
3095{
Alban Gruin65850682018-08-28 12:10:403096 struct strbuf buf = STRBUF_INIT;
3097
Phillip Wood4960e5c2023-04-10 09:08:303098 /*
3099 * Quote strategy options so that they can be read correctly
3100 * by split_cmdline().
3101 */
3102 quote_cmdline(&buf, opts->xopts.v);
Alban Gruin65850682018-08-28 12:10:403103 write_file(rebase_path_strategy_opts(), "%s\n", buf.buf);
3104 strbuf_release(&buf);
3105}
3106
3107int write_basic_state(struct replay_opts *opts, const char *head_name,
Phillip Wooda2bb10d2020-11-04 15:29:393108 struct commit *onto, const struct object_id *orig_head)
Alban Gruin65850682018-08-28 12:10:403109{
Alban Gruin65850682018-08-28 12:10:403110 if (head_name)
3111 write_file(rebase_path_head_name(), "%s\n", head_name);
3112 if (onto)
Phillip Wood7d3488e2019-04-17 14:30:393113 write_file(rebase_path_onto(), "%s\n",
3114 oid_to_hex(&onto->object.oid));
Alban Gruin65850682018-08-28 12:10:403115 if (orig_head)
Phillip Wooda2bb10d2020-11-04 15:29:393116 write_file(rebase_path_orig_head(), "%s\n",
3117 oid_to_hex(orig_head));
Alban Gruin65850682018-08-28 12:10:403118
Elijah Newren8a997ed2020-02-15 21:36:273119 if (opts->quiet)
3120 write_file(rebase_path_quiet(), "%s", "");
Alban Gruin65850682018-08-28 12:10:403121 if (opts->verbose)
Carlo Marcelo Arenas Belón4af51742018-10-25 09:38:543122 write_file(rebase_path_verbose(), "%s", "");
Alban Gruin65850682018-08-28 12:10:403123 if (opts->strategy)
3124 write_file(rebase_path_strategy(), "%s\n", opts->strategy);
Phillip Woodfb60b9f2023-04-10 09:08:283125 if (opts->xopts.nr > 0)
Alban Gruin65850682018-08-28 12:10:403126 write_strategy_opts(opts);
3127
3128 if (opts->allow_rerere_auto == RERERE_AUTOUPDATE)
3129 write_file(rebase_path_allow_rerere_autoupdate(), "--rerere-autoupdate\n");
3130 else if (opts->allow_rerere_auto == RERERE_NOAUTOUPDATE)
3131 write_file(rebase_path_allow_rerere_autoupdate(), "--no-rerere-autoupdate\n");
3132
3133 if (opts->gpg_sign)
3134 write_file(rebase_path_gpg_sign_opt(), "-S%s\n", opts->gpg_sign);
3135 if (opts->signoff)
3136 write_file(rebase_path_signoff(), "--signoff\n");
Elijah Newrene98c4262020-02-15 21:36:253137 if (opts->drop_redundant_commits)
3138 write_file(rebase_path_drop_redundant_commits(), "%s", "");
3139 if (opts->keep_redundant_commits)
3140 write_file(rebase_path_keep_redundant_commits(), "%s", "");
Phillip Wood7573cec2020-08-17 17:40:023141 if (opts->committer_date_is_author_date)
3142 write_file(rebase_path_cdate_is_adate(), "%s", "");
Phillip Wooda3894aa2020-08-17 17:40:033143 if (opts->ignore_date)
3144 write_file(rebase_path_ignore_date(), "%s", "");
Johannes Schindelind421afa2018-12-10 19:04:583145 if (opts->reschedule_failed_exec)
3146 write_file(rebase_path_reschedule_failed_exec(), "%s", "");
Ævar Arnfjörð Bjarmasone5b32bf2021-04-09 08:01:383147 else
3148 write_file(rebase_path_no_reschedule_failed_exec(), "%s", "");
Alban Gruin65850682018-08-28 12:10:403149
3150 return 0;
3151}
3152
Johannes Schindelin004fefa2016-10-21 12:24:413153static int walk_revs_populate_todo(struct todo_list *todo_list,
Ramkumar Ramachandra043a4492012-01-11 18:15:573154 struct replay_opts *opts)
3155{
Johannes Schindelin004fefa2016-10-21 12:24:413156 enum todo_command command = opts->action == REPLAY_PICK ?
3157 TODO_PICK : TODO_REVERT;
Johannes Schindelin414697a2017-01-02 15:27:153158 const char *command_string = todo_command_info[command].str;
Doan Tran Cong Danh019a9d82019-11-08 09:43:473159 const char *encoding;
Ramkumar Ramachandra043a4492012-01-11 18:15:573160 struct commit *commit;
Ramkumar Ramachandra043a4492012-01-11 18:15:573161
Johannes Schindelin34b05282016-09-09 14:37:153162 if (prepare_revs(opts))
3163 return -1;
Ramkumar Ramachandra043a4492012-01-11 18:15:573164
Doan Tran Cong Danh019a9d82019-11-08 09:43:473165 encoding = get_log_output_encoding();
3166
Johannes Schindelin004fefa2016-10-21 12:24:413167 while ((commit = get_revision(opts->revs))) {
3168 struct todo_item *item = append_new_todo(todo_list);
Ævar Arnfjörð Bjarmasonecb50912023-03-28 13:58:483169 const char *commit_buffer = repo_logmsg_reencode(the_repository,
3170 commit, NULL,
3171 encoding);
Johannes Schindelin004fefa2016-10-21 12:24:413172 const char *subject;
3173 int subject_len;
3174
3175 item->command = command;
3176 item->commit = commit;
Alban Gruin6ad656d2019-01-29 15:01:463177 item->arg_offset = 0;
Johannes Schindelinc22f7df2016-10-21 12:25:003178 item->arg_len = 0;
Johannes Schindelin004fefa2016-10-21 12:24:413179 item->offset_in_buf = todo_list->buf.len;
3180 subject_len = find_commit_subject(commit_buffer, &subject);
3181 strbuf_addf(&todo_list->buf, "%s %s %.*s\n", command_string,
Jeff Kingcb646ff2023-08-29 23:43:393182 short_commit_name(the_repository, commit),
3183 subject_len, subject);
Ævar Arnfjörð Bjarmasonecb50912023-03-28 13:58:483184 repo_unuse_commit_buffer(the_repository, commit,
3185 commit_buffer);
Johannes Schindelin004fefa2016-10-21 12:24:413186 }
Jeff King8530c732018-07-09 19:48:193187
3188 if (!todo_list->nr)
3189 return error(_("empty commit set passed"));
3190
Johannes Schindelin34b05282016-09-09 14:37:153191 return 0;
Ramkumar Ramachandra043a4492012-01-11 18:15:573192}
3193
Rohit Ashiwal6a1f9042019-07-02 09:11:253194static int create_seq_dir(struct repository *r)
Ramkumar Ramachandra043a4492012-01-11 18:15:573195{
Rohit Ashiwal6a1f9042019-07-02 09:11:253196 enum replay_action action;
3197 const char *in_progress_error = NULL;
3198 const char *in_progress_advice = NULL;
Han-Wen Nienhuysc8e41592020-08-21 16:59:353199 unsigned int advise_skip =
Han-Wen Nienhuysb8825ef2020-08-21 16:59:373200 refs_ref_exists(get_main_ref_store(r), "REVERT_HEAD") ||
Han-Wen Nienhuysc8e41592020-08-21 16:59:353201 refs_ref_exists(get_main_ref_store(r), "CHERRY_PICK_HEAD");
Rohit Ashiwal6a1f9042019-07-02 09:11:253202
3203 if (!sequencer_get_last_command(r, &action)) {
3204 switch (action) {
3205 case REPLAY_REVERT:
3206 in_progress_error = _("revert is already in progress");
3207 in_progress_advice =
Rohit Ashiwaldcb500d2019-07-02 09:11:293208 _("try \"git revert (--continue | %s--abort | --quit)\"");
Rohit Ashiwal6a1f9042019-07-02 09:11:253209 break;
3210 case REPLAY_PICK:
3211 in_progress_error = _("cherry-pick is already in progress");
3212 in_progress_advice =
Rohit Ashiwaldcb500d2019-07-02 09:11:293213 _("try \"git cherry-pick (--continue | %s--abort | --quit)\"");
Rohit Ashiwal6a1f9042019-07-02 09:11:253214 break;
3215 default:
3216 BUG("unexpected action in create_seq_dir");
3217 }
3218 }
3219 if (in_progress_error) {
3220 error("%s", in_progress_error);
Ben Boeckeled9bff02021-08-23 10:44:003221 if (advice_enabled(ADVICE_SEQUENCER_IN_USE))
Rohit Ashiwaldcb500d2019-07-02 09:11:293222 advise(in_progress_advice,
3223 advise_skip ? "--skip | " : "");
Ramkumar Ramachandra043a4492012-01-11 18:15:573224 return -1;
Rohit Ashiwal6a1f9042019-07-02 09:11:253225 }
3226 if (mkdir(git_path_seq_dir(), 0777) < 0)
Johannes Schindelin93b3df62016-10-21 12:26:253227 return error_errno(_("could not create sequencer directory '%s'"),
Johannes Schindelinf6e82b02016-09-09 14:37:443228 git_path_seq_dir());
Rohit Ashiwal6a1f9042019-07-02 09:11:253229
Ramkumar Ramachandra043a4492012-01-11 18:15:573230 return 0;
3231}
3232
Johannes Schindelin311fd392016-09-09 14:37:473233static int save_head(const char *head)
Ramkumar Ramachandra043a4492012-01-11 18:15:573234{
Oswald Buddenhagen54dbd092023-03-23 16:22:353235 return write_message(head, strlen(head), git_path_head_file(), 1);
Ramkumar Ramachandra043a4492012-01-11 18:15:573236}
3237
Stephan Beyer1e412292016-12-07 21:51:323238static int rollback_is_safe(void)
3239{
3240 struct strbuf sb = STRBUF_INIT;
3241 struct object_id expected_head, actual_head;
3242
3243 if (strbuf_read_file(&sb, git_path_abort_safety_file(), 0) >= 0) {
3244 strbuf_trim(&sb);
3245 if (get_oid_hex(sb.buf, &expected_head)) {
3246 strbuf_release(&sb);
3247 die(_("could not parse %s"), git_path_abort_safety_file());
3248 }
3249 strbuf_release(&sb);
3250 }
3251 else if (errno == ENOENT)
3252 oidclr(&expected_head);
3253 else
3254 die_errno(_("could not read '%s'"), git_path_abort_safety_file());
3255
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 13:58:463256 if (repo_get_oid(the_repository, "HEAD", &actual_head))
Stephan Beyer1e412292016-12-07 21:51:323257 oidclr(&actual_head);
3258
Jeff King4a7e27e2018-08-28 21:22:403259 return oideq(&actual_head, &expected_head);
Stephan Beyer1e412292016-12-07 21:51:323260}
3261
Rohit Ashiwal918d1e62019-07-02 09:11:263262static int reset_merge(const struct object_id *oid)
Ramkumar Ramachandra043a4492012-01-11 18:15:573263{
René Scharfe0e906732022-10-30 11:51:143264 struct child_process cmd = CHILD_PROCESS_INIT;
Stephan Beyer1e412292016-12-07 21:51:323265
René Scharfe0e906732022-10-30 11:51:143266 cmd.git_cmd = 1;
3267 strvec_pushl(&cmd.args, "reset", "--merge", NULL);
Rohit Ashiwal265ab482019-07-02 09:11:273268
3269 if (!is_null_oid(oid))
René Scharfe0e906732022-10-30 11:51:143270 strvec_push(&cmd.args, oid_to_hex(oid));
Rohit Ashiwal265ab482019-07-02 09:11:273271
René Scharfe0e906732022-10-30 11:51:143272 return run_command(&cmd);
Ramkumar Ramachandra043a4492012-01-11 18:15:573273}
3274
Nguyễn Thái Ngọc Duy005af332018-11-10 05:48:573275static int rollback_single_pick(struct repository *r)
Ramkumar Ramachandra043a4492012-01-11 18:15:573276{
brian m. carlson092bbcd2017-07-13 23:49:223277 struct object_id head_oid;
Ramkumar Ramachandra043a4492012-01-11 18:15:573278
Han-Wen Nienhuysc8e41592020-08-21 16:59:353279 if (!refs_ref_exists(get_main_ref_store(r), "CHERRY_PICK_HEAD") &&
Han-Wen Nienhuysb8825ef2020-08-21 16:59:373280 !refs_ref_exists(get_main_ref_store(r), "REVERT_HEAD"))
Ramkumar Ramachandra043a4492012-01-11 18:15:573281 return error(_("no cherry-pick or revert in progress"));
brian m. carlson34c290a2017-10-15 22:06:563282 if (read_ref_full("HEAD", 0, &head_oid, NULL))
Ramkumar Ramachandra043a4492012-01-11 18:15:573283 return error(_("cannot resolve HEAD"));
brian m. carlson092bbcd2017-07-13 23:49:223284 if (is_null_oid(&head_oid))
Ramkumar Ramachandra043a4492012-01-11 18:15:573285 return error(_("cannot abort from a branch yet to be born"));
Rohit Ashiwal918d1e62019-07-02 09:11:263286 return reset_merge(&head_oid);
Ramkumar Ramachandra043a4492012-01-11 18:15:573287}
3288
Rohit Ashiwalde81ca32019-07-02 09:11:283289static int skip_single_pick(void)
3290{
3291 struct object_id head;
3292
3293 if (read_ref_full("HEAD", 0, &head, NULL))
3294 return error(_("cannot resolve HEAD"));
3295 return reset_merge(&head);
Ramkumar Ramachandra043a4492012-01-11 18:15:573296}
3297
Nguyễn Thái Ngọc Duy005af332018-11-10 05:48:573298int sequencer_rollback(struct repository *r, struct replay_opts *opts)
Ramkumar Ramachandra043a4492012-01-11 18:15:573299{
Ramkumar Ramachandra043a4492012-01-11 18:15:573300 FILE *f;
brian m. carlson092bbcd2017-07-13 23:49:223301 struct object_id oid;
Ramkumar Ramachandra043a4492012-01-11 18:15:573302 struct strbuf buf = STRBUF_INIT;
brian m. carlson092bbcd2017-07-13 23:49:223303 const char *p;
Ramkumar Ramachandra043a4492012-01-11 18:15:573304
Jeff Kingf9327292015-08-10 09:38:573305 f = fopen(git_path_head_file(), "r");
Ramkumar Ramachandra043a4492012-01-11 18:15:573306 if (!f && errno == ENOENT) {
3307 /*
3308 * There is no multiple-cherry-pick in progress.
3309 * If CHERRY_PICK_HEAD or REVERT_HEAD indicates
3310 * a single-cherry-pick in progress, abort that.
3311 */
Nguyễn Thái Ngọc Duy005af332018-11-10 05:48:573312 return rollback_single_pick(r);
Ramkumar Ramachandra043a4492012-01-11 18:15:573313 }
3314 if (!f)
Johannes Schindelinf7ed1952016-10-21 12:26:213315 return error_errno(_("cannot open '%s'"), git_path_head_file());
Junio C Hamano8f309ae2016-01-13 23:31:173316 if (strbuf_getline_lf(&buf, f)) {
Johannes Schindelinf7ed1952016-10-21 12:26:213317 error(_("cannot read '%s': %s"), git_path_head_file(),
Jeff Kingf9327292015-08-10 09:38:573318 ferror(f) ? strerror(errno) : _("unexpected end of file"));
Ramkumar Ramachandra043a4492012-01-11 18:15:573319 fclose(f);
3320 goto fail;
3321 }
3322 fclose(f);
brian m. carlson092bbcd2017-07-13 23:49:223323 if (parse_oid_hex(buf.buf, &oid, &p) || *p != '\0') {
Ramkumar Ramachandra043a4492012-01-11 18:15:573324 error(_("stored pre-cherry-pick HEAD file '%s' is corrupt"),
Jeff Kingf9327292015-08-10 09:38:573325 git_path_head_file());
Ramkumar Ramachandra043a4492012-01-11 18:15:573326 goto fail;
3327 }
brian m. carlson092bbcd2017-07-13 23:49:223328 if (is_null_oid(&oid)) {
Michael J Gruber0f974e22016-06-06 13:23:543329 error(_("cannot abort from a branch yet to be born"));
3330 goto fail;
3331 }
Stephan Beyer1e412292016-12-07 21:51:323332
3333 if (!rollback_is_safe()) {
3334 /* Do not error, just do not rollback */
3335 warning(_("You seem to have moved HEAD. "
3336 "Not rewinding, check your HEAD!"));
3337 } else
Rohit Ashiwal918d1e62019-07-02 09:11:263338 if (reset_merge(&oid))
Ramkumar Ramachandra043a4492012-01-11 18:15:573339 goto fail;
Ramkumar Ramachandra043a4492012-01-11 18:15:573340 strbuf_release(&buf);
Johannes Schindelin28635842016-10-21 12:24:553341 return sequencer_remove_state(opts);
Ramkumar Ramachandra043a4492012-01-11 18:15:573342fail:
3343 strbuf_release(&buf);
3344 return -1;
3345}
3346
Rohit Ashiwalde81ca32019-07-02 09:11:283347int sequencer_skip(struct repository *r, struct replay_opts *opts)
3348{
3349 enum replay_action action = -1;
3350 sequencer_get_last_command(r, &action);
3351
3352 /*
3353 * Check whether the subcommand requested to skip the commit is actually
3354 * in progress and that it's safe to skip the commit.
3355 *
3356 * opts->action tells us which subcommand requested to skip the commit.
3357 * If the corresponding .git/<ACTION>_HEAD exists, we know that the
3358 * action is in progress and we can skip the commit.
3359 *
3360 * Otherwise we check that the last instruction was related to the
3361 * particular subcommand we're trying to execute and barf if that's not
3362 * the case.
3363 *
3364 * Finally we check that the rollback is "safe", i.e., has the HEAD
3365 * moved? In this case, it doesn't make sense to "reset the merge" and
3366 * "skip the commit" as the user already handled this by committing. But
3367 * we'd not want to barf here, instead give advice on how to proceed. We
3368 * only need to check that when .git/<ACTION>_HEAD doesn't exist because
3369 * it gets removed when the user commits, so if it still exists we're
3370 * sure the user can't have committed before.
3371 */
3372 switch (opts->action) {
3373 case REPLAY_REVERT:
Han-Wen Nienhuysb8825ef2020-08-21 16:59:373374 if (!refs_ref_exists(get_main_ref_store(r), "REVERT_HEAD")) {
Rohit Ashiwalde81ca32019-07-02 09:11:283375 if (action != REPLAY_REVERT)
3376 return error(_("no revert in progress"));
3377 if (!rollback_is_safe())
3378 goto give_advice;
3379 }
3380 break;
3381 case REPLAY_PICK:
Han-Wen Nienhuysc8e41592020-08-21 16:59:353382 if (!refs_ref_exists(get_main_ref_store(r),
3383 "CHERRY_PICK_HEAD")) {
Rohit Ashiwalde81ca32019-07-02 09:11:283384 if (action != REPLAY_PICK)
3385 return error(_("no cherry-pick in progress"));
3386 if (!rollback_is_safe())
3387 goto give_advice;
3388 }
3389 break;
3390 default:
3391 BUG("unexpected action in sequencer_skip");
3392 }
3393
3394 if (skip_single_pick())
3395 return error(_("failed to skip the commit"));
3396 if (!is_directory(git_path_seq_dir()))
3397 return 0;
3398
3399 return sequencer_continue(r, opts);
3400
3401give_advice:
3402 error(_("there is nothing to skip"));
3403
Ben Boeckeled9bff02021-08-23 10:44:003404 if (advice_enabled(ADVICE_RESOLVE_CONFLICT)) {
Rohit Ashiwalde81ca32019-07-02 09:11:283405 advise(_("have you committed already?\n"
3406 "try \"git %s --continue\""),
3407 action == REPLAY_REVERT ? "revert" : "cherry-pick");
3408 }
3409 return -1;
3410}
3411
Phillip Wood203573b2023-09-06 15:22:513412static int save_todo(struct todo_list *todo_list, struct replay_opts *opts,
3413 int reschedule)
Ramkumar Ramachandra043a4492012-01-11 18:15:573414{
Martin Ågren14bca6c2018-02-27 21:30:093415 struct lock_file todo_lock = LOCK_INIT;
Johannes Schindelin004fefa2016-10-21 12:24:413416 const char *todo_path = get_todo_path(opts);
3417 int next = todo_list->current, offset, fd;
Ramkumar Ramachandra043a4492012-01-11 18:15:573418
Johannes Schindelin84583952017-01-02 15:26:283419 /*
3420 * rebase -i writes "git-rebase-todo" without the currently executing
3421 * command, appending it to "done" instead.
3422 */
Phillip Wood203573b2023-09-06 15:22:513423 if (is_rebase_i(opts) && !reschedule)
Johannes Schindelin84583952017-01-02 15:26:283424 next++;
3425
Johannes Schindelin004fefa2016-10-21 12:24:413426 fd = hold_lock_file_for_update(&todo_lock, todo_path, 0);
Johannes Schindelin221675d2016-09-09 14:37:503427 if (fd < 0)
Johannes Schindelin93b3df62016-10-21 12:26:253428 return error_errno(_("could not lock '%s'"), todo_path);
Johannes Schindelina01c2a52018-04-25 12:28:293429 offset = get_item_line_offset(todo_list, next);
Johannes Schindelin004fefa2016-10-21 12:24:413430 if (write_in_full(fd, todo_list->buf.buf + offset,
3431 todo_list->buf.len - offset) < 0)
Johannes Schindelin93b3df62016-10-21 12:26:253432 return error_errno(_("could not write to '%s'"), todo_path);
Johannes Schindelin004fefa2016-10-21 12:24:413433 if (commit_lock_file(&todo_lock) < 0)
Martin Ågren350292a2018-02-28 19:07:583434 return error(_("failed to finalize '%s'"), todo_path);
Johannes Schindelin1df6df02017-01-02 15:27:003435
Phillip Wood203573b2023-09-06 15:22:513436 if (is_rebase_i(opts) && !reschedule && next > 0) {
Johannes Schindelina01c2a52018-04-25 12:28:293437 const char *done = rebase_path_done();
3438 int fd = open(done, O_CREAT | O_WRONLY | O_APPEND, 0666);
3439 int ret = 0;
Johannes Schindelin1df6df02017-01-02 15:27:003440
Johannes Schindelina01c2a52018-04-25 12:28:293441 if (fd < 0)
3442 return 0;
3443 if (write_in_full(fd, get_item_line(todo_list, next - 1),
3444 get_item_line_length(todo_list, next - 1))
3445 < 0)
3446 ret = error_errno(_("could not write to '%s'"), done);
3447 if (close(fd) < 0)
3448 ret = error_errno(_("failed to finalize '%s'"), done);
3449 return ret;
Johannes Schindelin1df6df02017-01-02 15:27:003450 }
Johannes Schindelin221675d2016-09-09 14:37:503451 return 0;
Ramkumar Ramachandra043a4492012-01-11 18:15:573452}
3453
Johannes Schindelin88d5a272016-09-09 14:37:533454static int save_opts(struct replay_opts *opts)
Ramkumar Ramachandra043a4492012-01-11 18:15:573455{
Jeff Kingf9327292015-08-10 09:38:573456 const char *opts_file = git_path_opts_file();
Johannes Schindelin88d5a272016-09-09 14:37:533457 int res = 0;
Ramkumar Ramachandra043a4492012-01-11 18:15:573458
3459 if (opts->no_commit)
Phillip Woodf59199d2019-03-13 18:26:133460 res |= git_config_set_in_file_gently(opts_file,
3461 "options.no-commit", "true");
Elijah Newren39edfd52021-03-31 06:52:203462 if (opts->edit >= 0)
3463 res |= git_config_set_in_file_gently(opts_file, "options.edit",
3464 opts->edit ? "true" : "false");
Phillip Wood6860ce52019-03-13 18:26:153465 if (opts->allow_empty)
3466 res |= git_config_set_in_file_gently(opts_file,
3467 "options.allow-empty", "true");
3468 if (opts->allow_empty_message)
3469 res |= git_config_set_in_file_gently(opts_file,
3470 "options.allow-empty-message", "true");
3471 if (opts->keep_redundant_commits)
3472 res |= git_config_set_in_file_gently(opts_file,
3473 "options.keep-redundant-commits", "true");
Ramkumar Ramachandra043a4492012-01-11 18:15:573474 if (opts->signoff)
Phillip Woodf59199d2019-03-13 18:26:133475 res |= git_config_set_in_file_gently(opts_file,
3476 "options.signoff", "true");
Ramkumar Ramachandra043a4492012-01-11 18:15:573477 if (opts->record_origin)
Phillip Woodf59199d2019-03-13 18:26:133478 res |= git_config_set_in_file_gently(opts_file,
3479 "options.record-origin", "true");
Ramkumar Ramachandra043a4492012-01-11 18:15:573480 if (opts->allow_ff)
Phillip Woodf59199d2019-03-13 18:26:133481 res |= git_config_set_in_file_gently(opts_file,
3482 "options.allow-ff", "true");
Ramkumar Ramachandra043a4492012-01-11 18:15:573483 if (opts->mainline) {
3484 struct strbuf buf = STRBUF_INIT;
3485 strbuf_addf(&buf, "%d", opts->mainline);
Phillip Woodf59199d2019-03-13 18:26:133486 res |= git_config_set_in_file_gently(opts_file,
3487 "options.mainline", buf.buf);
Ramkumar Ramachandra043a4492012-01-11 18:15:573488 strbuf_release(&buf);
3489 }
3490 if (opts->strategy)
Phillip Woodf59199d2019-03-13 18:26:133491 res |= git_config_set_in_file_gently(opts_file,
3492 "options.strategy", opts->strategy);
Nicolas Vigier32535532014-01-24 00:50:583493 if (opts->gpg_sign)
Phillip Woodf59199d2019-03-13 18:26:133494 res |= git_config_set_in_file_gently(opts_file,
3495 "options.gpg-sign", opts->gpg_sign);
Phillip Woodfb60b9f2023-04-10 09:08:283496 for (size_t i = 0; i < opts->xopts.nr; i++)
3497 res |= git_config_set_multivar_in_file_gently(opts_file,
3498 "options.strategy-option",
3499 opts->xopts.v[i], "^$", 0);
Phillip Wood8d8cb4b2017-08-02 10:44:193500 if (opts->allow_rerere_auto)
Phillip Woodf59199d2019-03-13 18:26:133501 res |= git_config_set_in_file_gently(opts_file,
3502 "options.allow-rerere-auto",
3503 opts->allow_rerere_auto == RERERE_AUTOUPDATE ?
3504 "true" : "false");
Phillip Wooddc42e9a2019-04-17 10:23:293505
3506 if (opts->explicit_cleanup)
3507 res |= git_config_set_in_file_gently(opts_file,
3508 "options.default-msg-cleanup",
3509 describe_cleanup_mode(opts->default_msg_cleanup));
Johannes Schindelin88d5a272016-09-09 14:37:533510 return res;
Ramkumar Ramachandra043a4492012-01-11 18:15:573511}
3512
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:563513static int make_patch(struct repository *r,
3514 struct commit *commit,
3515 struct replay_opts *opts)
Johannes Schindelin56dc3ab2017-01-02 15:26:433516{
Johannes Schindelin56dc3ab2017-01-02 15:26:433517 struct rev_info log_tree_opt;
Junio C Hamano0512eab2020-09-25 05:49:123518 const char *subject;
3519 char hex[GIT_MAX_HEXSZ + 1];
Johannes Schindelin56dc3ab2017-01-02 15:26:433520 int res = 0;
3521
Phillip Wood206a78d2023-09-06 15:22:463522 if (!is_rebase_i(opts))
3523 BUG("make_patch should only be called when rebasing");
3524
Junio C Hamano0512eab2020-09-25 05:49:123525 oid_to_hex_r(hex, &commit->object.oid);
3526 if (write_message(hex, strlen(hex), rebase_path_stopped_sha(), 1) < 0)
Johannes Schindelin56dc3ab2017-01-02 15:26:433527 return -1;
Phillip Wood430b75f2019-12-06 16:06:123528 res |= write_rebase_head(&commit->object.oid);
Johannes Schindelin56dc3ab2017-01-02 15:26:433529
Johannes Schindelin56dc3ab2017-01-02 15:26:433530 memset(&log_tree_opt, 0, sizeof(log_tree_opt));
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:563531 repo_init_revisions(r, &log_tree_opt, NULL);
Johannes Schindelin56dc3ab2017-01-02 15:26:433532 log_tree_opt.abbrev = 0;
3533 log_tree_opt.diff = 1;
3534 log_tree_opt.diffopt.output_format = DIFF_FORMAT_PATCH;
3535 log_tree_opt.disable_stdin = 1;
3536 log_tree_opt.no_commit_id = 1;
Phillip Wood206a78d2023-09-06 15:22:463537 log_tree_opt.diffopt.file = fopen(rebase_path_patch(), "w");
Johannes Schindelin56dc3ab2017-01-02 15:26:433538 log_tree_opt.diffopt.use_color = GIT_COLOR_NEVER;
3539 if (!log_tree_opt.diffopt.file)
Phillip Wood206a78d2023-09-06 15:22:463540 res |= error_errno(_("could not open '%s'"),
3541 rebase_path_patch());
Johannes Schindelin56dc3ab2017-01-02 15:26:433542 else {
3543 res |= log_tree_commit(&log_tree_opt, commit);
3544 fclose(log_tree_opt.diffopt.file);
3545 }
Johannes Schindelin56dc3ab2017-01-02 15:26:433546
Phillip Wood9f678992023-09-06 15:22:473547 if (!file_exists(rebase_path_message())) {
Doan Tran Cong Danh52f52e52019-11-11 06:03:413548 const char *encoding = get_commit_output_encoding();
Ævar Arnfjörð Bjarmason4a93b892023-03-28 13:58:583549 const char *commit_buffer = repo_logmsg_reencode(r,
Ævar Arnfjörð Bjarmasonecb50912023-03-28 13:58:483550 commit, NULL,
3551 encoding);
Johannes Schindelin56dc3ab2017-01-02 15:26:433552 find_commit_subject(commit_buffer, &subject);
Phillip Wood9f678992023-09-06 15:22:473553 res |= write_message(subject, strlen(subject), rebase_path_message(), 1);
Ævar Arnfjörð Bjarmason4a93b892023-03-28 13:58:583554 repo_unuse_commit_buffer(r, commit,
Ævar Arnfjörð Bjarmasonecb50912023-03-28 13:58:483555 commit_buffer);
Johannes Schindelin56dc3ab2017-01-02 15:26:433556 }
Ævar Arnfjörð Bjarmason2108fe42022-04-13 20:01:363557 release_revisions(&log_tree_opt);
Johannes Schindelin56dc3ab2017-01-02 15:26:433558
3559 return res;
3560}
3561
3562static int intend_to_amend(void)
3563{
brian m. carlson092bbcd2017-07-13 23:49:223564 struct object_id head;
Johannes Schindelin56dc3ab2017-01-02 15:26:433565 char *p;
3566
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 13:58:463567 if (repo_get_oid(the_repository, "HEAD", &head))
Johannes Schindelin56dc3ab2017-01-02 15:26:433568 return error(_("cannot read HEAD"));
3569
brian m. carlson092bbcd2017-07-13 23:49:223570 p = oid_to_hex(&head);
Johannes Schindelin56dc3ab2017-01-02 15:26:433571 return write_message(p, strlen(p), rebase_path_amend(), 1);
3572}
3573
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:563574static int error_with_patch(struct repository *r,
3575 struct commit *commit,
3576 const char *subject, int subject_len,
3577 struct replay_opts *opts,
3578 int exit_code, int to_amend)
Johannes Schindelin56dc3ab2017-01-02 15:26:433579{
Phillip Woodbc9238b2018-08-15 09:39:353580 if (commit) {
Nguyễn Th��i Ngọc Duyf11c9582018-11-10 05:48:563581 if (make_patch(r, commit, opts))
Phillip Woodbc9238b2018-08-15 09:39:353582 return -1;
Junio C Hamano5a5c5e92018-08-20 19:41:333583 } else if (copy_file(rebase_path_message(),
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:563584 git_path_merge_msg(r), 0666))
Phillip Woodbc9238b2018-08-15 09:39:353585 return error(_("unable to copy '%s' to '%s'"),
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:563586 git_path_merge_msg(r), rebase_path_message());
Johannes Schindelin56dc3ab2017-01-02 15:26:433587
3588 if (to_amend) {
3589 if (intend_to_amend())
3590 return -1;
3591
Nguyễn Thái Ngọc Duy02127c62018-07-21 07:49:383592 fprintf(stderr,
3593 _("You can amend the commit now, with\n"
3594 "\n"
3595 " git commit --amend %s\n"
3596 "\n"
3597 "Once you are satisfied with your changes, run\n"
3598 "\n"
3599 " git rebase --continue\n"),
3600 gpg_sign_opt_quoted(opts));
Phillip Woodbc9238b2018-08-15 09:39:353601 } else if (exit_code) {
3602 if (commit)
Junio C Hamano5a5c5e92018-08-20 19:41:333603 fprintf_ln(stderr, _("Could not apply %s... %.*s"),
Jeff Kingcb646ff2023-08-29 23:43:393604 short_commit_name(r, commit), subject_len, subject);
Phillip Woodbc9238b2018-08-15 09:39:353605 else
3606 /*
3607 * We don't have the hash of the parent so
3608 * just print the line from the todo file.
3609 */
Junio C Hamano5a5c5e92018-08-20 19:41:333610 fprintf_ln(stderr, _("Could not merge %.*s"),
3611 subject_len, subject);
Phillip Woodbc9238b2018-08-15 09:39:353612 }
Johannes Schindelin56dc3ab2017-01-02 15:26:433613
3614 return exit_code;
3615}
3616
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:563617static int error_failed_squash(struct repository *r,
3618 struct commit *commit,
3619 struct replay_opts *opts,
3620 int subject_len,
3621 const char *subject)
Johannes Schindelin6e98de72017-01-02 15:27:073622{
Johannes Schindeline12a7ef2018-04-27 20:48:213623 if (copy_file(rebase_path_message(), rebase_path_squash_msg(), 0666))
3624 return error(_("could not copy '%s' to '%s'"),
Johannes Schindelin6e98de72017-01-02 15:27:073625 rebase_path_squash_msg(), rebase_path_message());
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:563626 unlink(git_path_merge_msg(r));
3627 if (copy_file(git_path_merge_msg(r), rebase_path_message(), 0666))
Johannes Schindelin6e98de72017-01-02 15:27:073628 return error(_("could not copy '%s' to '%s'"),
Stefan Beller102de882018-05-17 22:51:513629 rebase_path_message(),
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:563630 git_path_merge_msg(r));
3631 return error_with_patch(r, commit, subject, subject_len, opts, 1, 0);
Johannes Schindelin6e98de72017-01-02 15:27:073632}
3633
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:563634static int do_exec(struct repository *r, const char *command_line)
Johannes Schindelin311af522017-01-02 15:26:473635{
René Scharfeddbb47f2022-10-30 11:55:063636 struct child_process cmd = CHILD_PROCESS_INIT;
Johannes Schindelin311af522017-01-02 15:26:473637 int dirty, status;
3638
Alban Gruin4d55d632020-03-28 13:05:153639 fprintf(stderr, _("Executing: %s\n"), command_line);
René Scharfeddbb47f2022-10-30 11:55:063640 cmd.use_shell = 1;
3641 strvec_push(&cmd.args, command_line);
3642 status = run_command(&cmd);
Johannes Schindelin311af522017-01-02 15:26:473643
3644 /* force re-reading of the cache */
Ævar Arnfjörð Bjarmason9c5f3ee2022-11-19 13:07:313645 discard_index(r->index);
3646 if (repo_read_index(r) < 0)
Johannes Schindelin311af522017-01-02 15:26:473647 return error(_("could not read index"));
3648
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:563649 dirty = require_clean_work_tree(r, "rebase", NULL, 1, 1);
Johannes Schindelin311af522017-01-02 15:26:473650
3651 if (status) {
3652 warning(_("execution failed: %s\n%s"
3653 "You can fix the problem, and then run\n"
3654 "\n"
3655 " git rebase --continue\n"
3656 "\n"),
3657 command_line,
Oswald Buddenhagend45cbe32023-04-28 12:56:483658 dirty ? _("and made changes to the index and/or the "
Oswald Buddenhagenb734fe42023-04-28 12:56:493659 "working tree.\n") : "");
Johannes Schindelin311af522017-01-02 15:26:473660 if (status == 127)
3661 /* command not found */
3662 status = 1;
3663 } else if (dirty) {
3664 warning(_("execution succeeded: %s\nbut "
Oswald Buddenhagenb734fe42023-04-28 12:56:493665 "left changes to the index and/or the working tree.\n"
Johannes Schindelin311af522017-01-02 15:26:473666 "Commit or stash your changes, and then run\n"
3667 "\n"
3668 " git rebase --continue\n"
3669 "\n"), command_line);
3670 status = 1;
3671 }
3672
3673 return status;
3674}
3675
Ævar Arnfjörð Bjarmason48ca53c2021-07-13 08:05:183676__attribute__((format (printf, 2, 3)))
Johannes Schindelin9055e402018-04-25 12:28:473677static int safe_append(const char *filename, const char *fmt, ...)
3678{
3679 va_list ap;
3680 struct lock_file lock = LOCK_INIT;
3681 int fd = hold_lock_file_for_update(&lock, filename,
3682 LOCK_REPORT_ON_ERROR);
3683 struct strbuf buf = STRBUF_INIT;
3684
3685 if (fd < 0)
3686 return -1;
3687
3688 if (strbuf_read_file(&buf, filename, 0) < 0 && errno != ENOENT) {
3689 error_errno(_("could not read '%s'"), filename);
3690 rollback_lock_file(&lock);
3691 return -1;
3692 }
3693 strbuf_complete(&buf, '\n');
3694 va_start(ap, fmt);
3695 strbuf_vaddf(&buf, fmt, ap);
3696 va_end(ap);
3697
3698 if (write_in_full(fd, buf.buf, buf.len) < 0) {
3699 error_errno(_("could not write to '%s'"), filename);
3700 strbuf_release(&buf);
3701 rollback_lock_file(&lock);
3702 return -1;
3703 }
3704 if (commit_lock_file(&lock) < 0) {
3705 strbuf_release(&buf);
Johannes Schindelin9055e402018-04-25 12:28:473706 return error(_("failed to finalize '%s'"), filename);
3707 }
3708
3709 strbuf_release(&buf);
3710 return 0;
3711}
3712
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:563713static int do_label(struct repository *r, const char *name, int len)
Johannes Schindelin9055e402018-04-25 12:28:473714{
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:563715 struct ref_store *refs = get_main_ref_store(r);
Johannes Schindelin9055e402018-04-25 12:28:473716 struct ref_transaction *transaction;
3717 struct strbuf ref_name = STRBUF_INIT, err = STRBUF_INIT;
3718 struct strbuf msg = STRBUF_INIT;
3719 int ret = 0;
3720 struct object_id head_oid;
3721
3722 if (len == 1 && *name == '#')
Nguyễn Thái Ngọc Duy02127c62018-07-21 07:49:383723 return error(_("illegal label name: '%.*s'"), len, name);
Johannes Schindelin9055e402018-04-25 12:28:473724
3725 strbuf_addf(&ref_name, "refs/rewritten/%.*s", len, name);
Elijah Newrenc2417d32020-02-15 21:36:363726 strbuf_addf(&msg, "rebase (label) '%.*s'", len, name);
Johannes Schindelin9055e402018-04-25 12:28:473727
Junio C Hamanoc6da34a2022-04-13 22:51:333728 transaction = ref_store_transaction_begin(refs, &err);
Johannes Schindelin9055e402018-04-25 12:28:473729 if (!transaction) {
3730 error("%s", err.buf);
3731 ret = -1;
Ævar Arnfjörð Bjarmason4a93b892023-03-28 13:58:583732 } else if (repo_get_oid(r, "HEAD", &head_oid)) {
Johannes Schindelin9055e402018-04-25 12:28:473733 error(_("could not read HEAD"));
3734 ret = -1;
3735 } else if (ref_transaction_update(transaction, ref_name.buf, &head_oid,
3736 NULL, 0, msg.buf, &err) < 0 ||
3737 ref_transaction_commit(transaction, &err)) {
3738 error("%s", err.buf);
3739 ret = -1;
3740 }
3741 ref_transaction_free(transaction);
3742 strbuf_release(&err);
3743 strbuf_release(&msg);
3744
3745 if (!ret)
3746 ret = safe_append(rebase_path_refs_to_delete(),
3747 "%s\n", ref_name.buf);
3748 strbuf_release(&ref_name);
3749
3750 return ret;
3751}
3752
Phillip Woodd188a602022-11-09 14:21:573753static const char *sequencer_reflog_action(struct replay_opts *opts)
3754{
3755 if (!opts->reflog_action) {
3756 opts->reflog_action = getenv(GIT_REFLOG_ACTION);
3757 opts->reflog_action =
3758 xstrdup(opts->reflog_action ? opts->reflog_action
3759 : action_name(opts));
3760 }
3761
3762 return opts->reflog_action;
3763}
3764
Ævar Arnfjörð Bjarmason48ca53c2021-07-13 08:05:183765__attribute__((format (printf, 3, 4)))
Johannes Schindelin9055e402018-04-25 12:28:473766static const char *reflog_message(struct replay_opts *opts,
Ævar Arnfjörð Bjarmasond4ac3052021-07-10 08:47:283767 const char *sub_action, const char *fmt, ...)
3768{
3769 va_list ap;
3770 static struct strbuf buf = STRBUF_INIT;
Ævar Arnfjörð Bjarmasond4ac3052021-07-10 08:47:283771
3772 va_start(ap, fmt);
3773 strbuf_reset(&buf);
Phillip Woodd188a602022-11-09 14:21:573774 strbuf_addstr(&buf, sequencer_reflog_action(opts));
Ævar Arnfjörð Bjarmasond4ac3052021-07-10 08:47:283775 if (sub_action)
3776 strbuf_addf(&buf, " (%s)", sub_action);
3777 if (fmt) {
3778 strbuf_addstr(&buf, ": ");
3779 strbuf_vaddf(&buf, fmt, ap);
3780 }
3781 va_end(ap);
3782
3783 return buf.buf;
3784}
Johannes Schindelin9055e402018-04-25 12:28:473785
Phillip Wood688d82f2022-11-10 16:43:413786static struct commit *lookup_label(struct repository *r, const char *label,
3787 int len, struct strbuf *buf)
Phillip Wood82766b22022-11-10 16:43:403788{
3789 struct commit *commit;
Phillip Wood688d82f2022-11-10 16:43:413790 struct object_id oid;
Phillip Wood82766b22022-11-10 16:43:403791
3792 strbuf_reset(buf);
3793 strbuf_addf(buf, "refs/rewritten/%.*s", len, label);
Phillip Wood688d82f2022-11-10 16:43:413794 if (!read_ref(buf->buf, &oid)) {
3795 commit = lookup_commit_object(r, &oid);
3796 } else {
Phillip Wood82766b22022-11-10 16:43:403797 /* fall back to non-rewritten ref or commit */
3798 strbuf_splice(buf, 0, strlen("refs/rewritten/"), "", 0);
3799 commit = lookup_commit_reference_by_name(buf->buf);
3800 }
3801
3802 if (!commit)
3803 error(_("could not resolve '%s'"), buf->buf);
3804
3805 return commit;
3806}
3807
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:563808static int do_reset(struct repository *r,
3809 const char *name, int len,
3810 struct replay_opts *opts)
Johannes Schindelin9055e402018-04-25 12:28:473811{
3812 struct strbuf ref_name = STRBUF_INIT;
3813 struct object_id oid;
3814 struct lock_file lock = LOCK_INIT;
Ævar Arnfjörð Bjarmason0c52cf82021-10-13 13:23:543815 struct tree_desc desc = { 0 };
Johannes Schindelin9055e402018-04-25 12:28:473816 struct tree *tree;
Ævar Arnfjörð Bjarmason6e658542021-10-13 13:23:553817 struct unpack_trees_options unpack_tree_opts = { 0 };
Martin Ågren71571cd2018-10-30 08:09:373818 int ret = 0;
Johannes Schindelin9055e402018-04-25 12:28:473819
Nguyễn Thái Ngọc Duy3a95f312019-01-12 02:13:243820 if (repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0)
Johannes Schindelin9055e402018-04-25 12:28:473821 return -1;
3822
Johannes Schindelinebddf392018-05-03 23:01:233823 if (len == 10 && !strncmp("[new root]", name, len)) {
3824 if (!opts->have_squash_onto) {
3825 const char *hex;
3826 if (commit_tree("", 0, the_hash_algo->empty_tree,
3827 NULL, &opts->squash_onto,
3828 NULL, NULL))
3829 return error(_("writing fake root commit"));
3830 opts->have_squash_onto = 1;
3831 hex = oid_to_hex(&opts->squash_onto);
3832 if (write_message(hex, strlen(hex),
3833 rebase_path_squash_onto(), 0))
3834 return error(_("writing squash-onto"));
3835 }
3836 oidcpy(&oid, &opts->squash_onto);
3837 } else {
Martin Ågren71571cd2018-10-30 08:09:373838 int i;
Phillip Wood82766b22022-11-10 16:43:403839 struct commit *commit;
Martin Ågren71571cd2018-10-30 08:09:373840
Johannes Schindelinebddf392018-05-03 23:01:233841 /* Determine the length of the label */
3842 for (i = 0; i < len; i++)
3843 if (isspace(name[i]))
Martin Ågren71571cd2018-10-30 08:09:373844 break;
3845 len = i;
Johannes Schindelin9055e402018-04-25 12:28:473846
Phillip Wood688d82f2022-11-10 16:43:413847 commit = lookup_label(r, name, len, &ref_name);
Phillip Wood82766b22022-11-10 16:43:403848 if (!commit) {
3849 ret = -1;
Ævar Arnfjörð Bjarmason0c52cf82021-10-13 13:23:543850 goto cleanup;
Johannes Schindelinebddf392018-05-03 23:01:233851 }
Phillip Wood82766b22022-11-10 16:43:403852 oid = commit->object.oid;
Johannes Schindelin9055e402018-04-25 12:28:473853 }
3854
Johannes Schindelin9055e402018-04-25 12:28:473855 setup_unpack_trees_porcelain(&unpack_tree_opts, "reset");
3856 unpack_tree_opts.head_idx = 1;
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:563857 unpack_tree_opts.src_index = r->index;
3858 unpack_tree_opts.dst_index = r->index;
Johannes Schindelin9055e402018-04-25 12:28:473859 unpack_tree_opts.fn = oneway_merge;
3860 unpack_tree_opts.merge = 1;
3861 unpack_tree_opts.update = 1;
Elijah Newren1b5f3732021-09-27 16:33:433862 unpack_tree_opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */
Victoria Dye652bd022022-11-10 19:06:053863 unpack_tree_opts.skip_cache_tree_update = 1;
brian m. carlson3f267852020-03-16 18:05:063864 init_checkout_metadata(&unpack_tree_opts.meta, name, &oid, NULL);
Johannes Schindelin9055e402018-04-25 12:28:473865
Nguyễn Thái Ngọc Duye1ff0a32019-01-12 02:13:263866 if (repo_read_index_unmerged(r)) {
Michael J Gruber1c8dfc32022-08-18 13:13:273867 ret = error_resolve_conflict(action_name(opts));
Ævar Arnfjörð Bjarmason0c52cf82021-10-13 13:23:543868 goto cleanup;
Johannes Schindelin9055e402018-04-25 12:28:473869 }
3870
Nguyễn Thái Ngọc Duy5e575802019-06-27 09:28:483871 if (!fill_tree_descriptor(r, &desc, &oid)) {
Ævar Arnfjörð Bjarmason0c52cf82021-10-13 13:23:543872 ret = error(_("failed to find tree of %s"), oid_to_hex(&oid));
3873 goto cleanup;
Johannes Schindelin9055e402018-04-25 12:28:473874 }
3875
3876 if (unpack_trees(1, &desc, &unpack_tree_opts)) {
Ævar Arnfjörð Bjarmason0c52cf82021-10-13 13:23:543877 ret = -1;
3878 goto cleanup;
Johannes Schindelin9055e402018-04-25 12:28:473879 }
3880
3881 tree = parse_tree_indirect(&oid);
Nguyễn Thái Ngọc Duyc207e9e2018-11-10 05:49:023882 prime_cache_tree(r, r->index, tree);
Johannes Schindelin9055e402018-04-25 12:28:473883
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:563884 if (write_locked_index(r->index, &lock, COMMIT_LOCK) < 0)
Johannes Schindelin9055e402018-04-25 12:28:473885 ret = error(_("could not write index"));
Johannes Schindelin9055e402018-04-25 12:28:473886
3887 if (!ret)
3888 ret = update_ref(reflog_message(opts, "reset", "'%.*s'",
3889 len, name), "HEAD", &oid,
3890 NULL, 0, UPDATE_REFS_MSG_ON_ERR);
Ævar Arnfjörð Bjarmason0c52cf82021-10-13 13:23:543891cleanup:
3892 free((void *)desc.buffer);
3893 if (ret < 0)
3894 rollback_lock_file(&lock);
Johannes Schindelin9055e402018-04-25 12:28:473895 strbuf_release(&ref_name);
Ævar Arnfjörð Bjarmason6e658542021-10-13 13:23:553896 clear_unpack_trees_porcelain(&unpack_tree_opts);
Johannes Schindelin9055e402018-04-25 12:28:473897 return ret;
3898}
3899
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:563900static int do_merge(struct repository *r,
3901 struct commit *commit,
3902 const char *arg, int arg_len,
Phillip Wood2be6b6f2021-08-20 15:40:353903 int flags, int *check_todo, struct replay_opts *opts)
Johannes Schindelin4c68e7d2018-04-25 12:28:543904{
Phillip Wood2be6b6f2021-08-20 15:40:353905 int run_commit_flags = 0;
Johannes Schindelin4c68e7d2018-04-25 12:28:543906 struct strbuf ref_name = STRBUF_INIT;
3907 struct commit *head_commit, *merge_commit, *i;
Jayati Shrivastava5327d892022-03-16 11:20:233908 struct commit_list *bases, *j;
Johannes Schindelin2b6ad0f2017-12-21 14:52:453909 struct commit_list *to_merge = NULL, **tail = &to_merge;
Phillip Woodfb60b9f2023-04-10 09:08:283910 const char *strategy = !opts->xopts.nr &&
Elijah Newren14c45862020-11-02 23:45:343911 (!opts->strategy ||
3912 !strcmp(opts->strategy, "recursive") ||
3913 !strcmp(opts->strategy, "ort")) ?
Johannes Schindeline145d992019-07-31 15:18:473914 NULL : opts->strategy;
Johannes Schindelin4c68e7d2018-04-25 12:28:543915 struct merge_options o;
Johannes Schindelin2b6ad0f2017-12-21 14:52:453916 int merge_arg_len, oneline_offset, can_fast_forward, ret, k;
Johannes Schindelin4c68e7d2018-04-25 12:28:543917 static struct lock_file lock;
3918 const char *p;
3919
Nguyễn Thái Ngọc Duy3a95f312019-01-12 02:13:243920 if (repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) {
Johannes Schindelin4c68e7d2018-04-25 12:28:543921 ret = -1;
3922 goto leave_merge;
3923 }
3924
3925 head_commit = lookup_commit_reference_by_name("HEAD");
3926 if (!head_commit) {
3927 ret = error(_("cannot merge without a current revision"));
3928 goto leave_merge;
3929 }
3930
Johannes Schindelin2b6ad0f2017-12-21 14:52:453931 /*
3932 * For octopus merges, the arg starts with the list of revisions to be
3933 * merged. The list is optionally followed by '#' and the oneline.
3934 */
3935 merge_arg_len = oneline_offset = arg_len;
3936 for (p = arg; p - arg < arg_len; p += strspn(p, " \t\n")) {
3937 if (!*p)
3938 break;
3939 if (*p == '#' && (!p[1] || isspace(p[1]))) {
3940 p += 1 + strspn(p + 1, " \t\n");
3941 oneline_offset = p - arg;
3942 break;
3943 }
3944 k = strcspn(p, " \t\n");
3945 if (!k)
3946 continue;
Phillip Wood688d82f2022-11-10 16:43:413947 merge_commit = lookup_label(r, p, k, &ref_name);
Johannes Schindelin2b6ad0f2017-12-21 14:52:453948 if (!merge_commit) {
3949 ret = error(_("unable to parse '%.*s'"), k, p);
3950 goto leave_merge;
3951 }
3952 tail = &commit_list_insert(merge_commit, tail)->next;
3953 p += k;
3954 merge_arg_len = p - arg;
Johannes Schindelin4c68e7d2018-04-25 12:28:543955 }
3956
Johannes Schindelin2b6ad0f2017-12-21 14:52:453957 if (!to_merge) {
3958 ret = error(_("nothing to merge: '%.*s'"), arg_len, arg);
Johannes Schindelin4c68e7d2018-04-25 12:28:543959 goto leave_merge;
3960 }
3961
Johannes Schindelin9c85a1c2018-05-03 23:01:283962 if (opts->have_squash_onto &&
Jeff King4a7e27e2018-08-28 21:22:403963 oideq(&head_commit->object.oid, &opts->squash_onto)) {
Johannes Schindelin9c85a1c2018-05-03 23:01:283964 /*
3965 * When the user tells us to "merge" something into a
3966 * "[new root]", let's simply fast-forward to the merge head.
3967 */
3968 rollback_lock_file(&lock);
Johannes Schindelin2b6ad0f2017-12-21 14:52:453969 if (to_merge->next)
3970 ret = error(_("octopus merge cannot be executed on "
3971 "top of a [new root]"));
3972 else
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:563973 ret = fast_forward_to(r, &to_merge->item->object.oid,
Johannes Schindelin2b6ad0f2017-12-21 14:52:453974 &head_commit->object.oid, 0,
3975 opts);
Johannes Schindelin9c85a1c2018-05-03 23:01:283976 goto leave_merge;
3977 }
3978
Phillip Woodbaf8ec82021-08-20 15:40:373979 /*
3980 * If HEAD is not identical to the first parent of the original merge
3981 * commit, we cannot fast-forward.
3982 */
3983 can_fast_forward = opts->allow_ff && commit && commit->parents &&
3984 oideq(&commit->parents->item->object.oid,
3985 &head_commit->object.oid);
3986
3987 /*
3988 * If any merge head is different from the original one, we cannot
3989 * fast-forward.
3990 */
3991 if (can_fast_forward) {
3992 struct commit_list *p = commit->parents->next;
3993
3994 for (j = to_merge; j && p; j = j->next, p = p->next)
3995 if (!oideq(&j->item->object.oid,
3996 &p->item->object.oid)) {
3997 can_fast_forward = 0;
3998 break;
3999 }
4000 /*
4001 * If the number of merge heads differs from the original merge
4002 * commit, we cannot fast-forward.
4003 */
4004 if (j || p)
4005 can_fast_forward = 0;
4006 }
4007
4008 if (can_fast_forward) {
4009 rollback_lock_file(&lock);
4010 ret = fast_forward_to(r, &commit->object.oid,
4011 &head_commit->object.oid, 0, opts);
4012 if (flags & TODO_EDIT_MERGE_MSG)
4013 goto fast_forward_edit;
4014
4015 goto leave_merge;
4016 }
4017
Johannes Schindelin4c68e7d2018-04-25 12:28:544018 if (commit) {
Doan Tran Cong Danh5772b0c2019-11-11 06:03:404019 const char *encoding = get_commit_output_encoding();
Ævar Arnfjörð Bjarmason4a93b892023-03-28 13:58:584020 const char *message = repo_logmsg_reencode(r, commit, NULL,
Ævar Arnfjörð Bjarmasonecb50912023-03-28 13:58:484021 encoding);
Johannes Schindelin4c68e7d2018-04-25 12:28:544022 const char *body;
4023 int len;
4024
4025 if (!message) {
4026 ret = error(_("could not get commit message of '%s'"),
4027 oid_to_hex(&commit->object.oid));
4028 goto leave_merge;
4029 }
4030 write_author_script(message);
4031 find_commit_subject(message, &body);
4032 len = strlen(body);
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:564033 ret = write_message(body, len, git_path_merge_msg(r), 0);
Ævar Arnfjörð Bjarmason4a93b892023-03-28 13:58:584034 repo_unuse_commit_buffer(r, commit, message);
Johannes Schindelin4c68e7d2018-04-25 12:28:544035 if (ret) {
4036 error_errno(_("could not write '%s'"),
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:564037 git_path_merge_msg(r));
Johannes Schindelin4c68e7d2018-04-25 12:28:544038 goto leave_merge;
4039 }
4040 } else {
4041 struct strbuf buf = STRBUF_INIT;
4042 int len;
4043
4044 strbuf_addf(&buf, "author %s", git_author_info(0));
4045 write_author_script(buf.buf);
4046 strbuf_reset(&buf);
4047
4048 if (oneline_offset < arg_len) {
4049 p = arg + oneline_offset;
4050 len = arg_len - oneline_offset;
4051 } else {
Johannes Schindelin2b6ad0f2017-12-21 14:52:454052 strbuf_addf(&buf, "Merge %s '%.*s'",
4053 to_merge->next ? "branches" : "branch",
Johannes Schindelin4c68e7d2018-04-25 12:28:544054 merge_arg_len, arg);
4055 p = buf.buf;
4056 len = buf.len;
4057 }
4058
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:564059 ret = write_message(p, len, git_path_merge_msg(r), 0);
Johannes Schindelin4c68e7d2018-04-25 12:28:544060 strbuf_release(&buf);
4061 if (ret) {
4062 error_errno(_("could not write '%s'"),
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:564063 git_path_merge_msg(r));
Johannes Schindelin4c68e7d2018-04-25 12:28:544064 goto leave_merge;
4065 }
4066 }
4067
Johannes Schindeline145d992019-07-31 15:18:474068 if (strategy || to_merge->next) {
Johannes Schindelin2b6ad0f2017-12-21 14:52:454069 /* Octopus merge */
4070 struct child_process cmd = CHILD_PROCESS_INIT;
4071
Ævar Arnfjörð Bjarmason29fda242022-06-02 09:09:504072 if (read_env_script(&cmd.env)) {
Johannes Schindelin2b6ad0f2017-12-21 14:52:454073 const char *gpg_opt = gpg_sign_opt_quoted(opts);
4074
4075 ret = error(_(staged_changes_advice), gpg_opt, gpg_opt);
4076 goto leave_merge;
4077 }
4078
Phillip Wood7573cec2020-08-17 17:40:024079 if (opts->committer_date_is_author_date)
Ævar Arnfjörð Bjarmason29fda242022-06-02 09:09:504080 strvec_pushf(&cmd.env, "GIT_COMMITTER_DATE=%s",
Junio C Hamano9c31b192020-09-03 19:37:014081 opts->ignore_date ?
4082 "" :
Ævar Arnfjörð Bjarmasonb3193252022-06-02 09:09:514083 author_date_from_env(&cmd.env));
Phillip Wooda3894aa2020-08-17 17:40:034084 if (opts->ignore_date)
Ævar Arnfjörð Bjarmason29fda242022-06-02 09:09:504085 strvec_push(&cmd.env, "GIT_AUTHOR_DATE=");
Phillip Wood7573cec2020-08-17 17:40:024086
Johannes Schindelin2b6ad0f2017-12-21 14:52:454087 cmd.git_cmd = 1;
Jeff Kingc972bf42020-07-28 20:25:124088 strvec_push(&cmd.args, "merge");
4089 strvec_push(&cmd.args, "-s");
Johannes Schindeline145d992019-07-31 15:18:474090 if (!strategy)
Jeff Kingc972bf42020-07-28 20:25:124091 strvec_push(&cmd.args, "octopus");
Johannes Schindeline145d992019-07-31 15:18:474092 else {
Jeff Kingc972bf42020-07-28 20:25:124093 strvec_push(&cmd.args, strategy);
Phillip Woodfb60b9f2023-04-10 09:08:284094 for (k = 0; k < opts->xopts.nr; k++)
Jeff Kingc972bf42020-07-28 20:25:124095 strvec_pushf(&cmd.args,
Phillip Woodfb60b9f2023-04-10 09:08:284096 "-X%s", opts->xopts.v[k]);
Johannes Schindeline145d992019-07-31 15:18:474097 }
Phillip Woodf2563c92021-08-20 15:40:384098 if (!(flags & TODO_EDIT_MERGE_MSG))
4099 strvec_push(&cmd.args, "--no-edit");
4100 else
4101 strvec_push(&cmd.args, "--edit");
Jeff Kingc972bf42020-07-28 20:25:124102 strvec_push(&cmd.args, "--no-ff");
4103 strvec_push(&cmd.args, "--no-log");
4104 strvec_push(&cmd.args, "--no-stat");
4105 strvec_push(&cmd.args, "-F");
4106 strvec_push(&cmd.args, git_path_merge_msg(r));
Johannes Schindelin2b6ad0f2017-12-21 14:52:454107 if (opts->gpg_sign)
Samuel Čavojae03c972020-10-17 23:15:554108 strvec_pushf(&cmd.args, "-S%s", opts->gpg_sign);
Samuel Čavoj19dad042020-10-17 23:15:564109 else
4110 strvec_push(&cmd.args, "--no-gpg-sign");
Johannes Schindelin2b6ad0f2017-12-21 14:52:454111
4112 /* Add the tips to be merged */
4113 for (j = to_merge; j; j = j->next)
Jeff Kingc972bf42020-07-28 20:25:124114 strvec_push(&cmd.args,
Jeff Kingf6d89422020-07-28 20:26:314115 oid_to_hex(&j->item->object.oid));
Johannes Schindelin2b6ad0f2017-12-21 14:52:454116
4117 strbuf_release(&ref_name);
Han-Wen Nienhuysc8e41592020-08-21 16:59:354118 refs_delete_ref(get_main_ref_store(r), "", "CHERRY_PICK_HEAD",
4119 NULL, 0);
Johannes Schindelin2b6ad0f2017-12-21 14:52:454120 rollback_lock_file(&lock);
4121
Johannes Schindelin2b6ad0f2017-12-21 14:52:454122 ret = run_command(&cmd);
4123
4124 /* force re-reading of the cache */
Ævar Arnfjörð Bjarmason9c5f3ee2022-11-19 13:07:314125 if (!ret) {
4126 discard_index(r->index);
4127 if (repo_read_index(r) < 0)
4128 ret = error(_("could not read index"));
4129 }
Johannes Schindelin2b6ad0f2017-12-21 14:52:454130 goto leave_merge;
4131 }
4132
4133 merge_commit = to_merge->item;
Ævar Arnfjörð Bjarmason4a93b892023-03-28 13:58:584134 bases = repo_get_merge_bases(r, head_commit, merge_commit);
Jeff King4a7e27e2018-08-28 21:22:404135 if (bases && oideq(&merge_commit->object.oid,
4136 &bases->item->object.oid)) {
Johannes Schindelin7ccdf652018-04-25 12:29:314137 ret = 0;
4138 /* skip merging an ancestor of HEAD */
4139 goto leave_merge;
4140 }
4141
brian m. carlson4439c7a2019-08-18 20:04:164142 write_message(oid_to_hex(&merge_commit->object.oid), the_hash_algo->hexsz,
Junio C Hamanocde55542019-01-04 21:33:334143 git_path_merge_head(r), 0);
4144 write_message("no-ff", 5, git_path_merge_mode(r), 0);
Johannes Schindelin85f8d9d2018-11-12 23:25:584145
Jayati Shrivastava5327d892022-03-16 11:20:234146 bases = reverse_commit_list(bases);
Johannes Schindelin4c68e7d2018-04-25 12:28:544147
Nguyễn Thái Ngọc Duye1ff0a32019-01-12 02:13:264148 repo_read_index(r);
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 02:13:294149 init_merge_options(&o, r);
Johannes Schindelin4c68e7d2018-04-25 12:28:544150 o.branch1 = "HEAD";
4151 o.branch2 = ref_name.buf;
4152 o.buffer_output = 2;
4153
Elijah Newren6a5fb962021-08-04 05:38:014154 if (!opts->strategy || !strcmp(opts->strategy, "ort")) {
Elijah Newren14c45862020-11-02 23:45:344155 /*
4156 * TODO: Should use merge_incore_recursive() and
4157 * merge_switch_to_result(), skipping the call to
4158 * merge_switch_to_result() when we don't actually need to
4159 * update the index and working copy immediately.
4160 */
4161 ret = merge_ort_recursive(&o,
Jayati Shrivastava5327d892022-03-16 11:20:234162 head_commit, merge_commit, bases,
Elijah Newren14c45862020-11-02 23:45:344163 &i);
4164 } else {
Jayati Shrivastava5327d892022-03-16 11:20:234165 ret = merge_recursive(&o, head_commit, merge_commit, bases,
Elijah Newren14c45862020-11-02 23:45:344166 &i);
4167 }
Johannes Schindelin4c68e7d2018-04-25 12:28:544168 if (ret <= 0)
4169 fputs(o.obuf.buf, stdout);
4170 strbuf_release(&o.obuf);
4171 if (ret < 0) {
4172 error(_("could not even attempt to merge '%.*s'"),
4173 merge_arg_len, arg);
Phillip Woode032abd2023-09-06 15:22:494174 unlink(git_path_merge_msg(r));
Johannes Schindelin4c68e7d2018-04-25 12:28:544175 goto leave_merge;
4176 }
4177 /*
4178 * The return value of merge_recursive() is 1 on clean, and 0 on
4179 * unclean merge.
4180 *
4181 * Let's reverse that, so that do_merge() returns 0 upon success and
4182 * 1 upon failed merge (keeping the return value -1 for the cases where
4183 * we will want to reschedule the `merge` command).
4184 */
4185 ret = !ret;
4186
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:564187 if (r->index->cache_changed &&
4188 write_locked_index(r->index, &lock, COMMIT_LOCK)) {
Johannes Schindelin4c68e7d2018-04-25 12:28:544189 ret = error(_("merge: Unable to write new index file"));
4190 goto leave_merge;
4191 }
4192
4193 rollback_lock_file(&lock);
4194 if (ret)
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:564195 repo_rerere(r, opts->allow_rerere_auto);
Johannes Schindelin4c68e7d2018-04-25 12:28:544196 else
4197 /*
4198 * In case of problems, we now want to return a positive
4199 * value (a negative one would indicate that the `merge`
4200 * command needs to be rescheduled).
4201 */
Jeff King20f4b042020-09-30 12:29:314202 ret = !!run_git_commit(git_path_merge_msg(r), opts,
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:564203 run_commit_flags);
Johannes Schindelin4c68e7d2018-04-25 12:28:544204
Phillip Wood2be6b6f2021-08-20 15:40:354205 if (!ret && flags & TODO_EDIT_MERGE_MSG) {
4206 fast_forward_edit:
4207 *check_todo = 1;
4208 run_commit_flags |= AMEND_MSG | EDIT_MSG | VERIFY_MSG;
4209 ret = !!run_git_commit(NULL, opts, run_commit_flags);
4210 }
4211
4212
Johannes Schindelin4c68e7d2018-04-25 12:28:544213leave_merge:
4214 strbuf_release(&ref_name);
4215 rollback_lock_file(&lock);
Johannes Schindelin2b6ad0f2017-12-21 14:52:454216 free_commit_list(to_merge);
Johannes Schindelin4c68e7d2018-04-25 12:28:544217 return ret;
4218}
4219
Derrick Stolee89fc0b52022-07-19 18:33:404220static int write_update_refs_state(struct string_list *refs_to_oids)
Derrick Stoleea97d7912022-07-19 18:33:384221{
Derrick Stolee89fc0b52022-07-19 18:33:404222 int result = 0;
4223 struct lock_file lock = LOCK_INIT;
4224 FILE *fp = NULL;
4225 struct string_list_item *item;
4226 char *path;
4227
Derrick Stolee89fc0b52022-07-19 18:33:404228 path = rebase_path_update_refs(the_repository->gitdir);
4229
Victoria Dye44da9e02022-11-07 17:47:524230 if (!refs_to_oids->nr) {
4231 if (unlink(path) && errno != ENOENT)
4232 result = error_errno(_("could not unlink: %s"), path);
4233 goto cleanup;
4234 }
4235
Derrick Stolee89fc0b52022-07-19 18:33:404236 if (safe_create_leading_directories(path)) {
4237 result = error(_("unable to create leading directories of %s"),
4238 path);
4239 goto cleanup;
4240 }
4241
4242 if (hold_lock_file_for_update(&lock, path, 0) < 0) {
4243 result = error(_("another 'rebase' process appears to be running; "
4244 "'%s.lock' already exists"),
4245 path);
4246 goto cleanup;
4247 }
4248
4249 fp = fdopen_lock_file(&lock, "w");
4250 if (!fp) {
4251 result = error_errno(_("could not open '%s' for writing"), path);
4252 rollback_lock_file(&lock);
4253 goto cleanup;
4254 }
4255
4256 for_each_string_list_item(item, refs_to_oids) {
4257 struct update_ref_record *rec = item->util;
4258 fprintf(fp, "%s\n%s\n%s\n", item->string,
4259 oid_to_hex(&rec->before), oid_to_hex(&rec->after));
4260 }
4261
4262 result = commit_lock_file(&lock);
4263
4264cleanup:
4265 free(path);
4266 return result;
4267}
4268
Derrick Stoleeb3b1a212022-07-19 18:33:414269/*
4270 * Parse the update-refs file for the current rebase, then remove the
4271 * refs that do not appear in the todo_list (and have not had updated
4272 * values stored) and add refs that are in the todo_list but not
4273 * represented in the update-refs file.
4274 *
4275 * If there are changes to the update-refs list, then write the new state
4276 * to disk.
4277 */
4278void todo_list_filter_update_refs(struct repository *r,
4279 struct todo_list *todo_list)
4280{
4281 int i;
4282 int updated = 0;
4283 struct string_list update_refs = STRING_LIST_INIT_DUP;
4284
4285 sequencer_get_update_refs_state(r->gitdir, &update_refs);
4286
4287 /*
4288 * For each item in the update_refs list, if it has no updated
4289 * value and does not appear in the todo_list, then remove it
4290 * from the update_refs list.
4291 */
4292 for (i = 0; i < update_refs.nr; i++) {
4293 int j;
4294 int found = 0;
4295 const char *ref = update_refs.items[i].string;
4296 size_t reflen = strlen(ref);
4297 struct update_ref_record *rec = update_refs.items[i].util;
4298
4299 /* OID already stored as updated. */
4300 if (!is_null_oid(&rec->after))
4301 continue;
4302
Johannes Schindelinfa5103d2023-05-13 08:11:254303 for (j = 0; !found && j < todo_list->nr; j++) {
Derrick Stoleeb3b1a212022-07-19 18:33:414304 struct todo_item *item = &todo_list->items[j];
4305 const char *arg = todo_list->buf.buf + item->arg_offset;
4306
4307 if (item->command != TODO_UPDATE_REF)
4308 continue;
4309
4310 if (item->arg_len != reflen ||
4311 strncmp(arg, ref, reflen))
4312 continue;
4313
4314 found = 1;
4315 }
4316
4317 if (!found) {
4318 free(update_refs.items[i].string);
4319 free(update_refs.items[i].util);
4320
4321 update_refs.nr--;
4322 MOVE_ARRAY(update_refs.items + i, update_refs.items + i + 1, update_refs.nr - i);
4323
4324 updated = 1;
4325 i--;
4326 }
4327 }
4328
4329 /*
4330 * For each todo_item, check if its ref is in the update_refs list.
4331 * If not, then add it as an un-updated ref.
4332 */
Johannes Schindelinfa5103d2023-05-13 08:11:254333 for (i = 0; i < todo_list->nr; i++) {
Derrick Stoleeb3b1a212022-07-19 18:33:414334 struct todo_item *item = &todo_list->items[i];
4335 const char *arg = todo_list->buf.buf + item->arg_offset;
4336 int j, found = 0;
4337
4338 if (item->command != TODO_UPDATE_REF)
4339 continue;
4340
4341 for (j = 0; !found && j < update_refs.nr; j++) {
4342 const char *ref = update_refs.items[j].string;
4343
4344 found = strlen(ref) == item->arg_len &&
4345 !strncmp(ref, arg, item->arg_len);
4346 }
4347
4348 if (!found) {
4349 struct string_list_item *inserted;
4350 struct strbuf argref = STRBUF_INIT;
4351
4352 strbuf_add(&argref, arg, item->arg_len);
4353 inserted = string_list_insert(&update_refs, argref.buf);
4354 inserted->util = init_update_ref_record(argref.buf);
4355 strbuf_release(&argref);
4356 updated = 1;
4357 }
4358 }
4359
4360 if (updated)
4361 write_update_refs_state(&update_refs);
4362 string_list_clear(&update_refs, 1);
4363}
4364
Derrick Stolee89fc0b52022-07-19 18:33:404365static int do_update_ref(struct repository *r, const char *refname)
4366{
4367 struct string_list_item *item;
4368 struct string_list list = STRING_LIST_INIT_DUP;
4369
4370 if (sequencer_get_update_refs_state(r->gitdir, &list))
4371 return -1;
4372
4373 for_each_string_list_item(item, &list) {
4374 if (!strcmp(item->string, refname)) {
4375 struct update_ref_record *rec = item->util;
4376 if (read_ref("HEAD", &rec->after))
4377 return -1;
4378 break;
4379 }
4380 }
4381
4382 write_update_refs_state(&list);
4383 string_list_clear(&list, 1);
Derrick Stoleea97d7912022-07-19 18:33:384384 return 0;
4385}
4386
Derrick Stolee46118842022-07-19 18:33:444387static int do_update_refs(struct repository *r, int quiet)
Derrick Stolee89fc0b52022-07-19 18:33:404388{
4389 int res = 0;
4390 struct string_list_item *item;
4391 struct string_list refs_to_oids = STRING_LIST_INIT_DUP;
4392 struct ref_store *refs = get_main_ref_store(r);
Derrick Stolee46118842022-07-19 18:33:444393 struct strbuf update_msg = STRBUF_INIT;
4394 struct strbuf error_msg = STRBUF_INIT;
Derrick Stolee89fc0b52022-07-19 18:33:404395
4396 if ((res = sequencer_get_update_refs_state(r->gitdir, &refs_to_oids)))
4397 return res;
4398
4399 for_each_string_list_item(item, &refs_to_oids) {
4400 struct update_ref_record *rec = item->util;
Derrick Stolee46118842022-07-19 18:33:444401 int loop_res;
Derrick Stolee89fc0b52022-07-19 18:33:404402
Derrick Stolee46118842022-07-19 18:33:444403 loop_res = refs_update_ref(refs, "rewritten during rebase",
4404 item->string,
4405 &rec->after, &rec->before,
4406 0, UPDATE_REFS_MSG_ON_ERR);
4407 res |= loop_res;
4408
4409 if (quiet)
4410 continue;
4411
4412 if (loop_res)
4413 strbuf_addf(&error_msg, "\t%s\n", item->string);
4414 else
4415 strbuf_addf(&update_msg, "\t%s\n", item->string);
4416 }
4417
4418 if (!quiet &&
4419 (update_msg.len || error_msg.len)) {
4420 fprintf(stderr,
4421 _("Updated the following refs with %s:\n%s"),
4422 "--update-refs",
4423 update_msg.buf);
4424
4425 if (res)
4426 fprintf(stderr,
4427 _("Failed to update the following refs with %s:\n%s"),
4428 "--update-refs",
4429 error_msg.buf);
Derrick Stolee89fc0b52022-07-19 18:33:404430 }
4431
4432 string_list_clear(&refs_to_oids, 1);
Derrick Stolee46118842022-07-19 18:33:444433 strbuf_release(&update_msg);
4434 strbuf_release(&error_msg);
Derrick Stolee89fc0b52022-07-19 18:33:404435 return res;
4436}
4437
Johannes Schindelin6e98de72017-01-02 15:27:074438static int is_final_fixup(struct todo_list *todo_list)
4439{
4440 int i = todo_list->current;
4441
4442 if (!is_fixup(todo_list->items[i].command))
4443 return 0;
4444
4445 while (++i < todo_list->nr)
4446 if (is_fixup(todo_list->items[i].command))
4447 return 0;
4448 else if (!is_noop(todo_list->items[i].command))
4449 break;
4450 return 1;
4451}
4452
Johannes Schindelin25cb8df2017-01-02 15:28:164453static enum todo_command peek_command(struct todo_list *todo_list, int offset)
4454{
4455 int i;
4456
4457 for (i = todo_list->current + offset; i < todo_list->nr; i++)
4458 if (!is_noop(todo_list->items[i].command))
4459 return todo_list->items[i].command;
4460
4461 return -1;
4462}
4463
Phillip Woodb7de1532022-01-26 13:05:444464void create_autostash(struct repository *r, const char *path)
Johannes Schindelin796c7972017-01-02 15:28:274465{
Denton Liu0816f1d2020-04-07 14:28:034466 struct strbuf buf = STRBUF_INIT;
4467 struct lock_file lock_file = LOCK_INIT;
4468 int fd;
4469
4470 fd = repo_hold_locked_index(r, &lock_file, 0);
4471 refresh_index(r->index, REFRESH_QUIET, NULL, NULL, NULL);
4472 if (0 <= fd)
4473 repo_update_index_if_able(r, &lock_file);
4474 rollback_lock_file(&lock_file);
4475
4476 if (has_unstaged_changes(r, 1) ||
4477 has_uncommitted_changes(r, 1)) {
4478 struct child_process stash = CHILD_PROCESS_INIT;
Phillip Wood6ae80862022-01-26 13:05:464479 struct reset_head_opts ropts = { .flags = RESET_HEAD_HARD };
Denton Liu0816f1d2020-04-07 14:28:034480 struct object_id oid;
4481
Jeff Kingc972bf42020-07-28 20:25:124482 strvec_pushl(&stash.args,
Jeff Kingf6d89422020-07-28 20:26:314483 "stash", "create", "autostash", NULL);
Denton Liu0816f1d2020-04-07 14:28:034484 stash.git_cmd = 1;
4485 stash.no_stdin = 1;
4486 strbuf_reset(&buf);
4487 if (capture_command(&stash, &buf, GIT_MAX_HEXSZ))
4488 die(_("Cannot autostash"));
4489 strbuf_trim_trailing_newline(&buf);
Ævar Arnfjörð Bjarmason4a93b892023-03-28 13:58:584490 if (repo_get_oid(r, buf.buf, &oid))
Denton Liu0816f1d2020-04-07 14:28:034491 die(_("Unexpected stash response: '%s'"),
4492 buf.buf);
4493 strbuf_reset(&buf);
4494 strbuf_add_unique_abbrev(&buf, &oid, DEFAULT_ABBREV);
4495
4496 if (safe_create_leading_directories_const(path))
4497 die(_("Could not create directory for '%s'"),
4498 path);
4499 write_file(path, "%s", oid_to_hex(&oid));
4500 printf(_("Created autostash: %s\n"), buf.buf);
Phillip Wood6ae80862022-01-26 13:05:464501 if (reset_head(r, &ropts) < 0)
Denton Liu0816f1d2020-04-07 14:28:034502 die(_("could not reset --hard"));
Ævar Arnfjörð Bjarmason9c5f3ee2022-11-19 13:07:314503 discard_index(r->index);
4504 if (repo_read_index(r) < 0)
Denton Liu0816f1d2020-04-07 14:28:034505 die(_("could not read index"));
4506 }
4507 strbuf_release(&buf);
4508}
4509
Denton Liu804fe312020-04-07 14:28:064510static int apply_save_autostash_oid(const char *stash_oid, int attempt_apply)
Johannes Schindelin796c7972017-01-02 15:28:274511{
Johannes Schindelin796c7972017-01-02 15:28:274512 struct child_process child = CHILD_PROCESS_INIT;
4513 int ret = 0;
4514
Denton Liu12b6e132020-04-07 14:28:054515 if (attempt_apply) {
4516 child.git_cmd = 1;
4517 child.no_stdout = 1;
4518 child.no_stderr = 1;
Jeff Kingc972bf42020-07-28 20:25:124519 strvec_push(&child.args, "stash");
4520 strvec_push(&child.args, "apply");
4521 strvec_push(&child.args, stash_oid);
Denton Liu12b6e132020-04-07 14:28:054522 ret = run_command(&child);
Johannes Schindelin796c7972017-01-02 15:28:274523 }
Johannes Schindelin796c7972017-01-02 15:28:274524
Denton Liu12b6e132020-04-07 14:28:054525 if (attempt_apply && !ret)
Johannes Schindelincdb866b2017-06-19 17:56:024526 fprintf(stderr, _("Applied autostash.\n"));
Johannes Schindelin796c7972017-01-02 15:28:274527 else {
4528 struct child_process store = CHILD_PROCESS_INIT;
4529
4530 store.git_cmd = 1;
Jeff Kingc972bf42020-07-28 20:25:124531 strvec_push(&store.args, "stash");
4532 strvec_push(&store.args, "store");
4533 strvec_push(&store.args, "-m");
4534 strvec_push(&store.args, "autostash");
4535 strvec_push(&store.args, "-q");
4536 strvec_push(&store.args, stash_oid);
Johannes Schindelin796c7972017-01-02 15:28:274537 if (run_command(&store))
Denton Liu804fe312020-04-07 14:28:064538 ret = error(_("cannot store %s"), stash_oid);
Johannes Schindelin796c7972017-01-02 15:28:274539 else
Johannes Schindelincdb866b2017-06-19 17:56:024540 fprintf(stderr,
Denton Liu12b6e132020-04-07 14:28:054541 _("%s\n"
Johannes Schindelincdb866b2017-06-19 17:56:024542 "Your changes are safe in the stash.\n"
4543 "You can run \"git stash pop\" or"
Denton Liu12b6e132020-04-07 14:28:054544 " \"git stash drop\" at any time.\n"),
4545 attempt_apply ?
4546 _("Applying autostash resulted in conflicts.") :
4547 _("Autostash exists; creating a new stash entry."));
Johannes Schindelin796c7972017-01-02 15:28:274548 }
4549
Johannes Schindelin796c7972017-01-02 15:28:274550 return ret;
4551}
4552
Denton Liu804fe312020-04-07 14:28:064553static int apply_save_autostash(const char *path, int attempt_apply)
4554{
4555 struct strbuf stash_oid = STRBUF_INIT;
4556 int ret = 0;
4557
4558 if (!read_oneliner(&stash_oid, path,
4559 READ_ONELINER_SKIP_IF_EMPTY)) {
4560 strbuf_release(&stash_oid);
4561 return 0;
4562 }
4563 strbuf_trim(&stash_oid);
4564
4565 ret = apply_save_autostash_oid(stash_oid.buf, attempt_apply);
4566
Denton Liu0dd562e2020-04-07 14:28:044567 unlink(path);
Denton Liufacca7f2020-04-07 14:27:574568 strbuf_release(&stash_oid);
Ramkumar Ramachandra043a4492012-01-11 18:15:574569 return ret;
4570}
4571
Denton Liu12b6e132020-04-07 14:28:054572int save_autostash(const char *path)
4573{
4574 return apply_save_autostash(path, 0);
4575}
4576
4577int apply_autostash(const char *path)
4578{
4579 return apply_save_autostash(path, 1);
4580}
4581
Denton Liu804fe312020-04-07 14:28:064582int apply_autostash_oid(const char *stash_oid)
4583{
4584 return apply_save_autostash_oid(stash_oid, 1);
4585}
4586
Phillip Woodfc4a6732019-03-19 19:03:074587static int checkout_onto(struct repository *r, struct replay_opts *opts,
Phillip Wood7d3488e2019-04-17 14:30:394588 const char *onto_name, const struct object_id *onto,
Phillip Woodf3e27a02020-11-04 15:29:384589 const struct object_id *orig_head)
Alban Gruin4df66c42018-08-10 16:51:344590{
Phillip Wood38c541c2022-01-26 13:05:494591 struct reset_head_opts ropts = {
4592 .oid = onto,
4593 .orig_head = orig_head,
4594 .flags = RESET_HEAD_DETACH | RESET_ORIG_HEAD |
4595 RESET_HEAD_RUN_POST_CHECKOUT_HOOK,
4596 .head_msg = reflog_message(opts, "start", "checkout %s",
4597 onto_name),
Phillip Woodd188a602022-11-09 14:21:574598 .default_reflog_action = sequencer_reflog_action(opts)
Phillip Wood38c541c2022-01-26 13:05:494599 };
4600 if (reset_head(r, &ropts)) {
Denton Liube1bb602020-04-07 14:27:564601 apply_autostash(rebase_path_autostash());
Alban Gruin4df66c42018-08-10 16:51:344602 sequencer_remove_state(opts);
4603 return error(_("could not detach HEAD"));
4604 }
4605
Phillip Wood38c541c2022-01-26 13:05:494606 return 0;
Alban Gruin4df66c42018-08-10 16:51:344607}
4608
Nguyễn Thái Ngọc Duy005af332018-11-10 05:48:574609static int stopped_at_head(struct repository *r)
Johannes Schindelin71f82462018-10-12 13:14:264610{
4611 struct object_id head;
4612 struct commit *commit;
4613 struct commit_message message;
4614
Ævar Arnfjörð Bjarmason4a93b892023-03-28 13:58:584615 if (repo_get_oid(r, "HEAD", &head) ||
Nguyễn Thái Ngọc Duy005af332018-11-10 05:48:574616 !(commit = lookup_commit(r, &head)) ||
Ævar Arnfjörð Bjarmason4a93b892023-03-28 13:58:584617 repo_parse_commit(r, commit) || get_message(commit, &message))
Johannes Schindelin71f82462018-10-12 13:14:264618 fprintf(stderr, _("Stopped at HEAD\n"));
4619 else {
4620 fprintf(stderr, _("Stopped at %s\n"), message.label);
4621 free_message(commit, &message);
4622 }
4623 return 0;
4624
4625}
4626
Phillip Wooddfa8bae2021-09-23 15:26:204627static int reread_todo_if_changed(struct repository *r,
4628 struct todo_list *todo_list,
4629 struct replay_opts *opts)
4630{
Phillip Wood2b88fe02021-09-23 15:26:214631 int offset;
4632 struct strbuf buf = STRBUF_INIT;
Phillip Wooddfa8bae2021-09-23 15:26:204633
Phillip Wood2b88fe02021-09-23 15:26:214634 if (strbuf_read_file_or_whine(&buf, get_todo_path(opts)) < 0)
4635 return -1;
4636 offset = get_item_line_offset(todo_list, todo_list->current + 1);
4637 if (buf.len != todo_list->buf.len - offset ||
4638 memcmp(buf.buf, todo_list->buf.buf + offset, buf.len)) {
Phillip Wooddfa8bae2021-09-23 15:26:204639 /* Reread the todo file if it has changed. */
4640 todo_list_release(todo_list);
4641 if (read_populate_todo(r, todo_list, opts))
4642 return -1; /* message was printed */
4643 /* `current` will be incremented on return */
4644 todo_list->current = -1;
4645 }
Phillip Wood2b88fe02021-09-23 15:26:214646 strbuf_release(&buf);
Phillip Wooddfa8bae2021-09-23 15:26:204647
4648 return 0;
4649}
4650
Johannes Schindelincb5206e2018-04-25 12:28:334651static const char rescheduled_advice[] =
4652N_("Could not execute the todo command\n"
4653"\n"
4654" %.*s"
4655"\n"
4656"It has been rescheduled; To edit the command before continuing, please\n"
4657"edit the todo list first:\n"
4658"\n"
4659" git rebase --edit-todo\n"
4660" git rebase --continue\n");
4661
Phillip Woodf2b5f412023-09-06 15:22:484662static int pick_one_commit(struct repository *r,
4663 struct todo_list *todo_list,
4664 struct replay_opts *opts,
Phillip Woode032abd2023-09-06 15:22:494665 int *check_todo, int* reschedule)
Phillip Woodf2b5f412023-09-06 15:22:484666{
4667 int res;
4668 struct todo_item *item = todo_list->items + todo_list->current;
4669 const char *arg = todo_item_get_arg(todo_list, item);
4670 if (is_rebase_i(opts))
4671 opts->reflog_message = reflog_message(
4672 opts, command_to_string(item->command), NULL);
4673
4674 res = do_pick_commit(r, item, opts, is_final_fixup(todo_list),
4675 check_todo);
4676 if (is_rebase_i(opts) && res < 0) {
4677 /* Reschedule */
Phillip Woode032abd2023-09-06 15:22:494678 *reschedule = 1;
4679 return -1;
Phillip Woodf2b5f412023-09-06 15:22:484680 }
4681 if (item->command == TODO_EDIT) {
4682 struct commit *commit = item->commit;
4683 if (!res) {
4684 if (!opts->verbose)
4685 term_clear_line();
4686 fprintf(stderr, _("Stopped at %s... %.*s\n"),
Junio C Hamanob995e782023-09-14 18:17:004687 short_commit_name(r, commit), item->arg_len, arg);
Phillip Woodf2b5f412023-09-06 15:22:484688 }
4689 return error_with_patch(r, commit,
4690 arg, item->arg_len, opts, res, !res);
4691 }
4692 if (is_rebase_i(opts) && !res)
4693 record_in_rewritten(&item->commit->object.oid,
4694 peek_command(todo_list, 1));
4695 if (res && is_fixup(item->command)) {
4696 if (res == 1)
4697 intend_to_amend();
4698 return error_failed_squash(r, item->commit, opts,
4699 item->arg_len, arg);
4700 } else if (res && is_rebase_i(opts) && item->commit) {
4701 int to_amend = 0;
4702 struct object_id oid;
4703
4704 /*
4705 * If we are rewording and have either
4706 * fast-forwarded already, or are about to
4707 * create a new root commit, we want to amend,
4708 * otherwise we do not.
4709 */
4710 if (item->command == TODO_REWORD &&
4711 !repo_get_oid(r, "HEAD", &oid) &&
4712 (oideq(&item->commit->object.oid, &oid) ||
4713 (opts->have_squash_onto &&
4714 oideq(&opts->squash_onto, &oid))))
4715 to_amend = 1;
4716
4717 return res | error_with_patch(r, item->commit,
4718 arg, item->arg_len, opts,
4719 res, to_amend);
4720 }
4721 return res;
4722}
4723
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:564724static int pick_commits(struct repository *r,
4725 struct todo_list *todo_list,
4726 struct replay_opts *opts)
Ramkumar Ramachandra043a4492012-01-11 18:15:574727{
Johannes Schindelin9055e402018-04-25 12:28:474728 int res = 0, reschedule = 0;
Ramkumar Ramachandra043a4492012-01-11 18:15:574729
Phillip Woodd188a602022-11-09 14:21:574730 opts->reflog_message = sequencer_reflog_action(opts);
Ramkumar Ramachandra043a4492012-01-11 18:15:574731 if (opts->allow_ff)
4732 assert(!(opts->signoff || opts->no_commit ||
Elijah Newren39edfd52021-03-31 06:52:204733 opts->record_origin || should_edit(opts) ||
Phillip Wooda3894aa2020-08-17 17:40:034734 opts->committer_date_is_author_date ||
4735 opts->ignore_date));
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:564736 if (read_and_refresh_cache(r, opts))
Johannes Schindelin0d9c6dc2016-09-09 14:37:214737 return -1;
Ramkumar Ramachandra043a4492012-01-11 18:15:574738
Phillip Wood36ac8612023-09-06 15:22:454739 unlink(rebase_path_message());
4740 unlink(rebase_path_stopped_sha());
4741 unlink(rebase_path_amend());
Phillip Wood206a78d2023-09-06 15:22:464742 unlink(rebase_path_patch());
Phillip Wood36ac8612023-09-06 15:22:454743
Johannes Schindelin004fefa2016-10-21 12:24:414744 while (todo_list->current < todo_list->nr) {
4745 struct todo_item *item = todo_list->items + todo_list->current;
Alban Gruin6ad656d2019-01-29 15:01:464746 const char *arg = todo_item_get_arg(todo_list, item);
Phillip Wooda47ba3c2019-08-19 09:18:224747 int check_todo = 0;
Alban Gruin6ad656d2019-01-29 15:01:464748
Phillip Wood203573b2023-09-06 15:22:514749 if (save_todo(todo_list, opts, reschedule))
Johannes Schindelin221675d2016-09-09 14:37:504750 return -1;
Johannes Schindelin6e98de72017-01-02 15:27:074751 if (is_rebase_i(opts)) {
Johannes Schindelinef800692017-01-02 15:36:204752 if (item->command != TODO_COMMENT) {
4753 FILE *f = fopen(rebase_path_msgnum(), "w");
4754
4755 todo_list->done_nr++;
4756
4757 if (f) {
4758 fprintf(f, "%d\n", todo_list->done_nr);
4759 fclose(f);
4760 }
Elijah Newren899b49c2018-12-11 16:11:364761 if (!opts->quiet)
Alban Gruin4d55d632020-03-28 13:05:154762 fprintf(stderr, _("Rebasing (%d/%d)%s"),
Elijah Newren899b49c2018-12-11 16:11:364763 todo_list->done_nr,
4764 todo_list->total_nr,
4765 opts->verbose ? "\n" : "\r");
Johannes Schindelinef800692017-01-02 15:36:204766 }
Johannes Schindelin6e98de72017-01-02 15:27:074767 unlink(rebase_path_author_script());
Nguyễn Thái Ngọc Duy34e77712019-06-27 09:28:524768 unlink(git_path_merge_head(r));
Elijah Newren52918282021-03-20 00:03:524769 unlink(git_path_auto_merge(r));
Nguyễn Thái Ngọc Duyfbd7a232018-02-11 09:43:284770 delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF);
Johannes Schindelin71f82462018-10-12 13:14:264771
SZEDER Gábord7d90882019-06-27 13:42:484772 if (item->command == TODO_BREAK) {
4773 if (!opts->verbose)
4774 term_clear_line();
Nguyễn Thái Ngọc Duy005af332018-11-10 05:48:574775 return stopped_at_head(r);
SZEDER Gábord7d90882019-06-27 13:42:484776 }
Johannes Schindelin6e98de72017-01-02 15:27:074777 }
4778 if (item->command <= TODO_SQUASH) {
Phillip Woode032abd2023-09-06 15:22:494779 res = pick_one_commit(r, todo_list, opts, &check_todo,
4780 &reschedule);
Phillip Woodf2b5f412023-09-06 15:22:484781 if (!res && item->command == TODO_EDIT)
4782 return 0;
Johannes Schindelin311af522017-01-02 15:26:474783 } else if (item->command == TODO_EXEC) {
Alban Gruin6ad656d2019-01-29 15:01:464784 char *end_of_arg = (char *)(arg + item->arg_len);
Johannes Schindelin311af522017-01-02 15:26:474785 int saved = *end_of_arg;
4786
SZEDER Gábord7d90882019-06-27 13:42:484787 if (!opts->verbose)
4788 term_clear_line();
Johannes Schindelin311af522017-01-02 15:26:474789 *end_of_arg = '\0';
Alban Gruin6ad656d2019-01-29 15:01:464790 res = do_exec(r, arg);
Johannes Schindelin311af522017-01-02 15:26:474791 *end_of_arg = saved;
Stephen Hicks54fd3242017-04-26 19:17:404792
Johannes Schindelind421afa2018-12-10 19:04:584793 if (res) {
4794 if (opts->reschedule_failed_exec)
4795 reschedule = 1;
Stephen Hicks54fd3242017-04-26 19:17:404796 }
Phillip Wooda47ba3c2019-08-19 09:18:224797 check_todo = 1;
Johannes Schindelin9055e402018-04-25 12:28:474798 } else if (item->command == TODO_LABEL) {
Alban Gruin6ad656d2019-01-29 15:01:464799 if ((res = do_label(r, arg, item->arg_len)))
Johannes Schindelin9055e402018-04-25 12:28:474800 reschedule = 1;
4801 } else if (item->command == TODO_RESET) {
Alban Gruin6ad656d2019-01-29 15:01:464802 if ((res = do_reset(r, arg, item->arg_len, opts)))
Johannes Schindelin9055e402018-04-25 12:28:474803 reschedule = 1;
Johannes Schindelin4c68e7d2018-04-25 12:28:544804 } else if (item->command == TODO_MERGE) {
Phillip Wood2be6b6f2021-08-20 15:40:354805 if ((res = do_merge(r, item->commit, arg, item->arg_len,
4806 item->flags, &check_todo, opts)) < 0)
Johannes Schindelin4c68e7d2018-04-25 12:28:544807 reschedule = 1;
Johannes Schindelin537e7d62018-04-25 12:29:294808 else if (item->commit)
4809 record_in_rewritten(&item->commit->object.oid,
4810 peek_command(todo_list, 1));
4811 if (res > 0)
Johannes Schindelin4c68e7d2018-04-25 12:28:544812 /* failed with merge conflicts */
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:564813 return error_with_patch(r, item->commit,
Alban Gruin6ad656d2019-01-29 15:01:464814 arg, item->arg_len,
4815 opts, res, 0);
Derrick Stoleea97d7912022-07-19 18:33:384816 } else if (item->command == TODO_UPDATE_REF) {
4817 struct strbuf ref = STRBUF_INIT;
4818 strbuf_add(&ref, arg, item->arg_len);
4819 if ((res = do_update_ref(r, ref.buf)))
4820 reschedule = 1;
4821 strbuf_release(&ref);
Johannes Schindelin56dc3ab2017-01-02 15:26:434822 } else if (!is_noop(item->command))
Johannes Schindelin25c43662017-01-02 15:26:384823 return error(_("unknown command %d"), item->command);
4824
Johannes Schindelin9055e402018-04-25 12:28:474825 if (reschedule) {
4826 advise(_(rescheduled_advice),
4827 get_item_line_length(todo_list,
4828 todo_list->current),
4829 get_item_line(todo_list, todo_list->current));
Phillip Wood203573b2023-09-06 15:22:514830 if (save_todo(todo_list, opts, reschedule))
Johannes Schindelin9055e402018-04-25 12:28:474831 return -1;
Johannes Schindelin4c68e7d2018-04-25 12:28:544832 if (item->commit)
Phillip Woode032abd2023-09-06 15:22:494833 write_rebase_head(&item->commit->object.oid);
Phillip Wooddfa8bae2021-09-23 15:26:204834 } else if (is_rebase_i(opts) && check_todo && !res &&
4835 reread_todo_if_changed(r, todo_list, opts)) {
4836 return -1;
Johannes Schindelin9055e402018-04-25 12:28:474837 }
4838
Ramkumar Ramachandra043a4492012-01-11 18:15:574839 if (res)
4840 return res;
Phillip Woodf2b5f412023-09-06 15:22:484841
4842 todo_list->current++;
Ramkumar Ramachandra043a4492012-01-11 18:15:574843 }
4844
Johannes Schindelin56dc3ab2017-01-02 15:26:434845 if (is_rebase_i(opts)) {
Johannes Schindelin4b83ce92017-01-02 15:27:534846 struct strbuf head_ref = STRBUF_INIT, buf = STRBUF_INIT;
Johannes Schindelin25cb8df2017-01-02 15:28:164847 struct stat st;
Johannes Schindelin556907f2017-01-02 15:26:534848
Johannes Schindelin4b83ce92017-01-02 15:27:534849 if (read_oneliner(&head_ref, rebase_path_head_name(), 0) &&
4850 starts_with(head_ref.buf, "refs/")) {
Johannes Schindelin96e832a2017-01-02 15:28:094851 const char *msg;
brian m. carlson092bbcd2017-07-13 23:49:224852 struct object_id head, orig;
Johannes Schindelin4b83ce92017-01-02 15:27:534853 int res;
4854
Ævar Arnfjörð Bjarmason4a93b892023-03-28 13:58:584855 if (repo_get_oid(r, "HEAD", &head)) {
Johannes Schindelin4b83ce92017-01-02 15:27:534856 res = error(_("cannot read HEAD"));
4857cleanup_head_ref:
4858 strbuf_release(&head_ref);
4859 strbuf_release(&buf);
4860 return res;
4861 }
4862 if (!read_oneliner(&buf, rebase_path_orig_head(), 0) ||
brian m. carlson092bbcd2017-07-13 23:49:224863 get_oid_hex(buf.buf, &orig)) {
Johannes Schindelin4b83ce92017-01-02 15:27:534864 res = error(_("could not read orig-head"));
4865 goto cleanup_head_ref;
4866 }
Phillip Wood4ab867b2017-05-18 10:02:324867 strbuf_reset(&buf);
Johannes Schindelin4b83ce92017-01-02 15:27:534868 if (!read_oneliner(&buf, rebase_path_onto(), 0)) {
4869 res = error(_("could not read 'onto'"));
4870 goto cleanup_head_ref;
4871 }
Johannes Schindelin96e832a2017-01-02 15:28:094872 msg = reflog_message(opts, "finish", "%s onto %s",
4873 head_ref.buf, buf.buf);
brian m. carlsonae077772017-10-15 22:06:514874 if (update_ref(msg, head_ref.buf, &head, &orig,
Michael Haggerty91774af2017-11-05 08:42:064875 REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR)) {
Johannes Schindelin4b83ce92017-01-02 15:27:534876 res = error(_("could not update %s"),
4877 head_ref.buf);
4878 goto cleanup_head_ref;
4879 }
Johannes Schindelin96e832a2017-01-02 15:28:094880 msg = reflog_message(opts, "finish", "returning to %s",
Johannes Schindelin4b83ce92017-01-02 15:27:534881 head_ref.buf);
Johannes Schindelin96e832a2017-01-02 15:28:094882 if (create_symref("HEAD", head_ref.buf, msg)) {
Johannes Schindelin4b83ce92017-01-02 15:27:534883 res = error(_("could not update HEAD to %s"),
4884 head_ref.buf);
4885 goto cleanup_head_ref;
4886 }
4887 strbuf_reset(&buf);
4888 }
4889
Johannes Schindelin556907f2017-01-02 15:26:534890 if (opts->verbose) {
4891 struct rev_info log_tree_opt;
4892 struct object_id orig, head;
4893
4894 memset(&log_tree_opt, 0, sizeof(log_tree_opt));
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:564895 repo_init_revisions(r, &log_tree_opt, NULL);
Johannes Schindelin556907f2017-01-02 15:26:534896 log_tree_opt.diff = 1;
4897 log_tree_opt.diffopt.output_format =
4898 DIFF_FORMAT_DIFFSTAT;
4899 log_tree_opt.disable_stdin = 1;
4900
4901 if (read_oneliner(&buf, rebase_path_orig_head(), 0) &&
Ævar Arnfjörð Bjarmason4a93b892023-03-28 13:58:584902 !repo_get_oid(r, buf.buf, &orig) &&
4903 !repo_get_oid(r, "HEAD", &head)) {
Brandon Williams66f414f2017-05-30 17:31:034904 diff_tree_oid(&orig, &head, "",
4905 &log_tree_opt.diffopt);
Johannes Schindelin556907f2017-01-02 15:26:534906 log_tree_diff_flush(&log_tree_opt);
4907 }
Ævar Arnfjörð Bjarmason2108fe42022-04-13 20:01:364908 release_revisions(&log_tree_opt);
Johannes Schindelin556907f2017-01-02 15:26:534909 }
Johannes Schindelin25cb8df2017-01-02 15:28:164910 flush_rewritten_pending();
4911 if (!stat(rebase_path_rewritten_list(), &st) &&
4912 st.st_size > 0) {
4913 struct child_process child = CHILD_PROCESS_INIT;
Emily Shaffer96af5642023-02-08 19:21:144914 struct run_hooks_opt hook_opt = RUN_HOOKS_OPT_INIT;
Johannes Schindelin25cb8df2017-01-02 15:28:164915
4916 child.in = open(rebase_path_rewritten_list(), O_RDONLY);
4917 child.git_cmd = 1;
Jeff Kingc972bf42020-07-28 20:25:124918 strvec_push(&child.args, "notes");
4919 strvec_push(&child.args, "copy");
4920 strvec_push(&child.args, "--for-rewrite=rebase");
Johannes Schindelin25cb8df2017-01-02 15:28:164921 /* we don't care if this copying failed */
4922 run_command(&child);
Johannes Schindelin79516042017-01-02 15:28:234923
Emily Shaffer96af5642023-02-08 19:21:144924 hook_opt.path_to_stdin = rebase_path_rewritten_list();
4925 strvec_push(&hook_opt.args, "rebase");
4926 run_hooks_opt("post-rewrite", &hook_opt);
Johannes Schindelin25cb8df2017-01-02 15:28:164927 }
Denton Liube1bb602020-04-07 14:27:564928 apply_autostash(rebase_path_autostash());
Johannes Schindelin25cb8df2017-01-02 15:28:164929
SZEDER Gábord7d90882019-06-27 13:42:484930 if (!opts->quiet) {
4931 if (!opts->verbose)
4932 term_clear_line();
Elijah Newren899b49c2018-12-11 16:11:364933 fprintf(stderr,
Alban Gruin4d55d632020-03-28 13:05:154934 _("Successfully rebased and updated %s.\n"),
Elijah Newren899b49c2018-12-11 16:11:364935 head_ref.buf);
SZEDER Gábord7d90882019-06-27 13:42:484936 }
Johannes Schindelin5da49662017-01-02 15:36:254937
Johannes Schindelin556907f2017-01-02 15:26:534938 strbuf_release(&buf);
Johannes Schindelin4b83ce92017-01-02 15:27:534939 strbuf_release(&head_ref);
Derrick Stolee89fc0b52022-07-19 18:33:404940
Derrick Stolee46118842022-07-19 18:33:444941 if (do_update_refs(r, opts->quiet))
Derrick Stolee89fc0b52022-07-19 18:33:404942 return -1;
Johannes Schindelin56dc3ab2017-01-02 15:26:434943 }
4944
Ramkumar Ramachandra043a4492012-01-11 18:15:574945 /*
4946 * Sequence of picks finished successfully; cleanup by
4947 * removing the .git/sequencer directory
4948 */
Johannes Schindelin28635842016-10-21 12:24:554949 return sequencer_remove_state(opts);
Ramkumar Ramachandra043a4492012-01-11 18:15:574950}
4951
Elijah Newren39edfd52021-03-31 06:52:204952static int continue_single_pick(struct repository *r, struct replay_opts *opts)
Ramkumar Ramachandra043a4492012-01-11 18:15:574953{
René Scharfe0e906732022-10-30 11:51:144954 struct child_process cmd = CHILD_PROCESS_INIT;
Ramkumar Ramachandra043a4492012-01-11 18:15:574955
Han-Wen Nienhuysc8e41592020-08-21 16:59:354956 if (!refs_ref_exists(get_main_ref_store(r), "CHERRY_PICK_HEAD") &&
Han-Wen Nienhuysb8825ef2020-08-21 16:59:374957 !refs_ref_exists(get_main_ref_store(r), "REVERT_HEAD"))
Ramkumar Ramachandra043a4492012-01-11 18:15:574958 return error(_("no cherry-pick or revert in progress"));
Elijah Newren39edfd52021-03-31 06:52:204959
René Scharfe0e906732022-10-30 11:51:144960 cmd.git_cmd = 1;
4961 strvec_push(&cmd.args, "commit");
Elijah Newren39edfd52021-03-31 06:52:204962
4963 /*
4964 * continue_single_pick() handles the case of recovering from a
4965 * conflict. should_edit() doesn't handle that case; for a conflict,
4966 * we want to edit if the user asked for it, or if they didn't specify
4967 * and stdin is a tty.
4968 */
4969 if (!opts->edit || (opts->edit < 0 && !isatty(0)))
4970 /*
4971 * Include --cleanup=strip as well because we don't want the
4972 * "# Conflicts:" messages.
4973 */
René Scharfe0e906732022-10-30 11:51:144974 strvec_pushl(&cmd.args, "--no-edit", "--cleanup=strip", NULL);
Elijah Newren39edfd52021-03-31 06:52:204975
René Scharfe0e906732022-10-30 11:51:144976 return run_command(&cmd);
Ramkumar Ramachandra043a4492012-01-11 18:15:574977}
4978
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:564979static int commit_staged_changes(struct repository *r,
4980 struct replay_opts *opts,
Johannes Schindelin15ef6932018-04-27 20:48:304981 struct todo_list *todo_list)
Johannes Schindelin9d93ccd2017-01-02 15:27:214982{
Johannes Schindelin789b3ef2017-03-23 16:07:114983 unsigned int flags = ALLOW_EMPTY | EDIT_MSG;
Johannes Schindelin15ef6932018-04-27 20:48:304984 unsigned int final_fixup = 0, is_clean;
Johannes Schindelin9d93ccd2017-01-02 15:27:214985
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:564986 if (has_unstaged_changes(r, 1))
Johannes Schindelin9d93ccd2017-01-02 15:27:214987 return error(_("cannot rebase: You have unstaged changes."));
Johannes Schindelin52632202017-01-02 15:27:254988
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:564989 is_clean = !has_uncommitted_changes(r, 0);
Johannes Schindelin9d93ccd2017-01-02 15:27:214990
Phillip Wood405509c2023-09-06 15:22:504991 if (!is_clean && !file_exists(rebase_path_message())) {
4992 const char *gpg_opt = gpg_sign_opt_quoted(opts);
4993
4994 return error(_(staged_changes_advice), gpg_opt, gpg_opt);
4995 }
Johannes Schindelin9d93ccd2017-01-02 15:27:214996 if (file_exists(rebase_path_amend())) {
4997 struct strbuf rev = STRBUF_INIT;
brian m. carlson092bbcd2017-07-13 23:49:224998 struct object_id head, to_amend;
Johannes Schindelin9d93ccd2017-01-02 15:27:214999
Ævar Arnfjörð Bjarmason4a93b892023-03-28 13:58:585000 if (repo_get_oid(r, "HEAD", &head))
Johannes Schindelin9d93ccd2017-01-02 15:27:215001 return error(_("cannot amend non-existing commit"));
5002 if (!read_oneliner(&rev, rebase_path_amend(), 0))
5003 return error(_("invalid file: '%s'"), rebase_path_amend());
brian m. carlson092bbcd2017-07-13 23:49:225004 if (get_oid_hex(rev.buf, &to_amend))
Johannes Schindelin9d93ccd2017-01-02 15:27:215005 return error(_("invalid contents: '%s'"),
5006 rebase_path_amend());
Jeff King9001dc22018-08-28 21:22:485007 if (!is_clean && !oideq(&head, &to_amend))
Johannes Schindelin9d93ccd2017-01-02 15:27:215008 return error(_("\nYou have uncommitted changes in your "
5009 "working tree. Please, commit them\n"
5010 "first and then run 'git rebase "
5011 "--continue' again."));
Johannes Schindelin15ef6932018-04-27 20:48:305012 /*
5013 * When skipping a failed fixup/squash, we need to edit the
5014 * commit message, the current fixup list and count, and if it
5015 * was the last fixup/squash in the chain, we need to clean up
5016 * the commit message and if there was a squash, let the user
5017 * edit it.
5018 */
Johannes Schindelin10d2f352018-08-31 23:45:045019 if (!is_clean || !opts->current_fixup_count)
5020 ; /* this is not the final fixup */
Junio C Hamano87ae8a12018-09-24 17:30:455021 else if (!oideq(&head, &to_amend) ||
Johannes Schindelin10d2f352018-08-31 23:45:045022 !file_exists(rebase_path_stopped_sha())) {
5023 /* was a final fixup or squash done manually? */
5024 if (!is_fixup(peek_command(todo_list, 0))) {
5025 unlink(rebase_path_fixup_msg());
5026 unlink(rebase_path_squash_msg());
5027 unlink(rebase_path_current_fixups());
5028 strbuf_reset(&opts->current_fixups);
5029 opts->current_fixup_count = 0;
5030 }
5031 } else {
5032 /* we are in a fixup/squash chain */
Johannes Schindelin15ef6932018-04-27 20:48:305033 const char *p = opts->current_fixups.buf;
5034 int len = opts->current_fixups.len;
5035
5036 opts->current_fixup_count--;
5037 if (!len)
5038 BUG("Incorrect current_fixups:\n%s", p);
5039 while (len && p[len - 1] != '\n')
5040 len--;
5041 strbuf_setlen(&opts->current_fixups, len);
5042 if (write_message(p, len, rebase_path_current_fixups(),
5043 0) < 0)
5044 return error(_("could not write file: '%s'"),
5045 rebase_path_current_fixups());
5046
5047 /*
5048 * If a fixup/squash in a fixup/squash chain failed, the
5049 * commit message is already correct, no need to commit
5050 * it again.
5051 *
5052 * Only if it is the final command in the fixup/squash
5053 * chain, and only if the chain is longer than a single
5054 * fixup/squash command (which was just skipped), do we
5055 * actually need to re-commit with a cleaned up commit
5056 * message.
5057 */
5058 if (opts->current_fixup_count > 0 &&
5059 !is_fixup(peek_command(todo_list, 0))) {
5060 final_fixup = 1;
5061 /*
5062 * If there was not a single "squash" in the
5063 * chain, we only need to clean up the commit
5064 * message, no need to bother the user with
5065 * opening the commit message in the editor.
5066 */
5067 if (!starts_with(p, "squash ") &&
5068 !strstr(p, "\nsquash "))
5069 flags = (flags & ~EDIT_MSG) | CLEANUP_MSG;
5070 } else if (is_fixup(peek_command(todo_list, 0))) {
5071 /*
5072 * We need to update the squash message to skip
5073 * the latest commit message.
5074 */
Phillip Wood6ce7afe2023-08-03 13:09:355075 int res = 0;
Johannes Schindelin15ef6932018-04-27 20:48:305076 struct commit *commit;
Phillip Wood6ce7afe2023-08-03 13:09:355077 const char *msg;
Johannes Schindelin15ef6932018-04-27 20:48:305078 const char *path = rebase_path_squash_msg();
Doan Tran Cong Danhb3757442019-11-08 09:43:485079 const char *encoding = get_commit_output_encoding();
Johannes Schindelin15ef6932018-04-27 20:48:305080
Phillip Wood6ce7afe2023-08-03 13:09:355081 if (parse_head(r, &commit))
5082 return error(_("could not parse HEAD"));
5083
5084 p = repo_logmsg_reencode(r, commit, NULL, encoding);
5085 if (!p) {
5086 res = error(_("could not parse commit %s"),
5087 oid_to_hex(&commit->object.oid));
5088 goto unuse_commit_buffer;
Johannes Schindelin15ef6932018-04-27 20:48:305089 }
Phillip Wood6ce7afe2023-08-03 13:09:355090 find_commit_subject(p, &msg);
5091 if (write_message(msg, strlen(msg), path, 0)) {
5092 res = error(_("could not write file: "
5093 "'%s'"), path);
5094 goto unuse_commit_buffer;
5095 }
5096 unuse_commit_buffer:
5097 repo_unuse_commit_buffer(r, commit, p);
5098 if (res)
5099 return res;
Johannes Schindelin15ef6932018-04-27 20:48:305100 }
5101 }
Johannes Schindelin9d93ccd2017-01-02 15:27:215102
5103 strbuf_release(&rev);
Johannes Schindelin789b3ef2017-03-23 16:07:115104 flags |= AMEND_MSG;
Johannes Schindelin9d93ccd2017-01-02 15:27:215105 }
5106
Johannes Schindelin15ef6932018-04-27 20:48:305107 if (is_clean) {
Han-Wen Nienhuysc8e41592020-08-21 16:59:355108 if (refs_ref_exists(get_main_ref_store(r),
5109 "CHERRY_PICK_HEAD") &&
5110 refs_delete_ref(get_main_ref_store(r), "",
5111 "CHERRY_PICK_HEAD", NULL, 0))
Johannes Schindelin15ef6932018-04-27 20:48:305112 return error(_("could not remove CHERRY_PICK_HEAD"));
Phillip Woode5ee33e2021-08-12 13:42:095113 if (unlink(git_path_merge_msg(r)) && errno != ENOENT)
5114 return error_errno(_("could not remove '%s'"),
5115 git_path_merge_msg(r));
Johannes Schindelin15ef6932018-04-27 20:48:305116 if (!final_fixup)
5117 return 0;
5118 }
5119
Jeff King20f4b042020-09-30 12:29:315120 if (run_git_commit(final_fixup ? NULL : rebase_path_message(),
Johannes Schindelin15ef6932018-04-27 20:48:305121 opts, flags))
Johannes Schindelin9d93ccd2017-01-02 15:27:215122 return error(_("could not commit staged changes."));
5123 unlink(rebase_path_amend());
Nguyễn Thái Ngọc Duy34e77712019-06-27 09:28:525124 unlink(git_path_merge_head(r));
Elijah Newren52918282021-03-20 00:03:525125 unlink(git_path_auto_merge(r));
Johannes Schindelin15ef6932018-04-27 20:48:305126 if (final_fixup) {
5127 unlink(rebase_path_fixup_msg());
5128 unlink(rebase_path_squash_msg());
5129 }
5130 if (opts->current_fixup_count > 0) {
5131 /*
5132 * Whether final fixup or not, we just cleaned up the commit
5133 * message...
5134 */
5135 unlink(rebase_path_current_fixups());
5136 strbuf_reset(&opts->current_fixups);
5137 opts->current_fixup_count = 0;
5138 }
Johannes Schindelin9d93ccd2017-01-02 15:27:215139 return 0;
5140}
5141
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:565142int sequencer_continue(struct repository *r, struct replay_opts *opts)
Ramkumar Ramachandra043a4492012-01-11 18:15:575143{
Johannes Schindelin004fefa2016-10-21 12:24:415144 struct todo_list todo_list = TODO_LIST_INIT;
5145 int res;
Ramkumar Ramachandra043a4492012-01-11 18:15:575146
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:565147 if (read_and_refresh_cache(r, opts))
Johannes Schindelin28635842016-10-21 12:24:555148 return -1;
5149
Johannes Schindelin15ef6932018-04-27 20:48:305150 if (read_populate_opts(opts))
5151 return -1;
Johannes Schindelin9d93ccd2017-01-02 15:27:215152 if (is_rebase_i(opts)) {
Nguyễn Thái Ngọc Duy005af332018-11-10 05:48:575153 if ((res = read_populate_todo(r, &todo_list, opts)))
Johannes Schindelin15ef6932018-04-27 20:48:305154 goto release_todo_list;
Alban Gruin5a5445d2020-01-28 21:12:465155
5156 if (file_exists(rebase_path_dropped())) {
5157 if ((res = todo_list_check_against_backup(r, &todo_list)))
5158 goto release_todo_list;
5159
5160 unlink(rebase_path_dropped());
5161 }
5162
Phillip Woodd188a602022-11-09 14:21:575163 opts->reflog_message = reflog_message(opts, "continue", NULL);
Alban Gruinf6b94132019-11-28 23:02:035164 if (commit_staged_changes(r, opts, &todo_list)) {
5165 res = -1;
5166 goto release_todo_list;
5167 }
Johannes Schindelin4258a6d2017-01-02 15:27:305168 } else if (!file_exists(get_todo_path(opts)))
Elijah Newren39edfd52021-03-31 06:52:205169 return continue_single_pick(r, opts);
Nguyễn Thái Ngọc Duy005af332018-11-10 05:48:575170 else if ((res = read_populate_todo(r, &todo_list, opts)))
Johannes Schindelin004fefa2016-10-21 12:24:415171 goto release_todo_list;
Ramkumar Ramachandra043a4492012-01-11 18:15:575172
Johannes Schindelin4258a6d2017-01-02 15:27:305173 if (!is_rebase_i(opts)) {
5174 /* Verify that the conflict has been resolved */
Han-Wen Nienhuysc8e41592020-08-21 16:59:355175 if (refs_ref_exists(get_main_ref_store(r),
5176 "CHERRY_PICK_HEAD") ||
Han-Wen Nienhuysb8825ef2020-08-21 16:59:375177 refs_ref_exists(get_main_ref_store(r), "REVERT_HEAD")) {
Elijah Newren39edfd52021-03-31 06:52:205178 res = continue_single_pick(r, opts);
Johannes Schindelin4258a6d2017-01-02 15:27:305179 if (res)
5180 goto release_todo_list;
5181 }
Nguyễn Thái Ngọc Duyffc00a42018-11-10 05:49:045182 if (index_differs_from(r, "HEAD", NULL, 0)) {
Nguyễn Thái Ngọc Duye1ff0a32019-01-12 02:13:265183 res = error_dirty_index(r, opts);
Johannes Schindelin004fefa2016-10-21 12:24:415184 goto release_todo_list;
Johannes Schindelin4258a6d2017-01-02 15:27:305185 }
5186 todo_list.current++;
Johannes Schindelinca98c6d2017-01-02 15:28:205187 } else if (file_exists(rebase_path_stopped_sha())) {
5188 struct strbuf buf = STRBUF_INIT;
5189 struct object_id oid;
5190
Denton Liu3442c3d2020-04-07 14:27:525191 if (read_oneliner(&buf, rebase_path_stopped_sha(),
5192 READ_ONELINER_SKIP_IF_EMPTY) &&
Junio C Hamano0512eab2020-09-25 05:49:125193 !get_oid_hex(buf.buf, &oid))
Johannes Schindelinca98c6d2017-01-02 15:28:205194 record_in_rewritten(&oid, peek_command(&todo_list, 0));
5195 strbuf_release(&buf);
Ramkumar Ramachandra043a4492012-01-11 18:15:575196 }
Johannes Schindelin4258a6d2017-01-02 15:27:305197
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:565198 res = pick_commits(r, &todo_list, opts);
Johannes Schindelin004fefa2016-10-21 12:24:415199release_todo_list:
5200 todo_list_release(&todo_list);
5201 return res;
Ramkumar Ramachandra043a4492012-01-11 18:15:575202}
5203
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:565204static int single_pick(struct repository *r,
5205 struct commit *cmit,
5206 struct replay_opts *opts)
Ramkumar Ramachandra043a4492012-01-11 18:15:575207{
Phillip Wooda47ba3c2019-08-19 09:18:225208 int check_todo;
Charvi Mendirattaae70e342021-01-29 18:20:455209 struct todo_item item;
5210
5211 item.command = opts->action == REPLAY_PICK ?
5212 TODO_PICK : TODO_REVERT;
5213 item.commit = cmit;
Phillip Wooda47ba3c2019-08-19 09:18:225214
Phillip Woodd188a602022-11-09 14:21:575215 opts->reflog_message = sequencer_reflog_action(opts);
Charvi Mendirattaae70e342021-01-29 18:20:455216 return do_pick_commit(r, &item, opts, 0, &check_todo);
Ramkumar Ramachandra043a4492012-01-11 18:15:575217}
5218
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:565219int sequencer_pick_revisions(struct repository *r,
5220 struct replay_opts *opts)
Ramkumar Ramachandra043a4492012-01-11 18:15:575221{
Johannes Schindelin004fefa2016-10-21 12:24:415222 struct todo_list todo_list = TODO_LIST_INIT;
brian m. carlson1e43ed92017-05-06 22:10:095223 struct object_id oid;
Johannes Schindelin004fefa2016-10-21 12:24:415224 int i, res;
Ramkumar Ramachandra043a4492012-01-11 18:15:575225
Johannes Schindelin28635842016-10-21 12:24:555226 assert(opts->revs);
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:565227 if (read_and_refresh_cache(r, opts))
Johannes Schindelin0d9c6dc2016-09-09 14:37:215228 return -1;
Ramkumar Ramachandra043a4492012-01-11 18:15:575229
Miklos Vajna21246db2013-04-11 13:06:525230 for (i = 0; i < opts->revs->pending.nr; i++) {
brian m. carlson1e43ed92017-05-06 22:10:095231 struct object_id oid;
Miklos Vajna21246db2013-04-11 13:06:525232 const char *name = opts->revs->pending.objects[i].name;
5233
5234 /* This happens when using --stdin. */
5235 if (!strlen(name))
5236 continue;
5237
Ævar Arnfjörð Bjarmason4a93b892023-03-28 13:58:585238 if (!repo_get_oid(r, name, &oid)) {
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:565239 if (!lookup_commit_reference_gently(r, &oid, 1)) {
5240 enum object_type type = oid_object_info(r,
Stefan Beller0df8e962018-04-25 18:20:595241 &oid,
brian m. carlsonabef9022018-03-12 02:27:465242 NULL);
Johannes Schindelinb9b946d2016-08-26 13:47:105243 return error(_("%s: can't cherry-pick a %s"),
Brandon Williamsdebca9d2018-02-14 18:59:245244 name, type_name(type));
Junio C Hamano7c0b0d82013-05-09 20:27:495245 }
Miklos Vajna21246db2013-04-11 13:06:525246 } else
Johannes Schindelinb9b946d2016-08-26 13:47:105247 return error(_("%s: bad revision"), name);
Miklos Vajna21246db2013-04-11 13:06:525248 }
5249
Ramkumar Ramachandra043a4492012-01-11 18:15:575250 /*
5251 * If we were called as "git cherry-pick <commit>", just
5252 * cherry-pick/revert it, set CHERRY_PICK_HEAD /
5253 * REVERT_HEAD, and don't touch the sequencer state.
5254 * This means it is possible to cherry-pick in the middle
5255 * of a cherry-pick sequence.
5256 */
5257 if (opts->revs->cmdline.nr == 1 &&
5258 opts->revs->cmdline.rev->whence == REV_CMD_REV &&
5259 opts->revs->no_walk &&
5260 !opts->revs->cmdline.rev->flags) {
5261 struct commit *cmit;
5262 if (prepare_revision_walk(opts->revs))
Johannes Schindelinb9b946d2016-08-26 13:47:105263 return error(_("revision walk setup failed"));
Ramkumar Ramachandra043a4492012-01-11 18:15:575264 cmit = get_revision(opts->revs);
Jeff Kingc5e358d2018-07-10 04:32:085265 if (!cmit)
5266 return error(_("empty commit set passed"));
5267 if (get_revision(opts->revs))
5268 BUG("unexpected extra commit from walk");
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:565269 return single_pick(r, cmit, opts);
Ramkumar Ramachandra043a4492012-01-11 18:15:575270 }
5271
5272 /*
5273 * Start a new cherry-pick/ revert sequence; but
5274 * first, make sure that an existing one isn't in
5275 * progress
5276 */
5277
Johannes Schindelin34b05282016-09-09 14:37:155278 if (walk_revs_populate_todo(&todo_list, opts) ||
Rohit Ashiwal6a1f9042019-07-02 09:11:255279 create_seq_dir(r) < 0)
Ramkumar Ramachandra043a4492012-01-11 18:15:575280 return -1;
Ævar Arnfjörð Bjarmason4a93b892023-03-28 13:58:585281 if (repo_get_oid(r, "HEAD", &oid) && (opts->action == REPLAY_REVERT))
Johannes Schindelin93b3df62016-10-21 12:26:255282 return error(_("can't revert as initial commit"));
brian m. carlson1e43ed92017-05-06 22:10:095283 if (save_head(oid_to_hex(&oid)))
Johannes Schindelin311fd392016-09-09 14:37:475284 return -1;
Johannes Schindelin88d5a272016-09-09 14:37:535285 if (save_opts(opts))
5286 return -1;
Stephan Beyer1e412292016-12-07 21:51:325287 update_abort_safety_file();
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:565288 res = pick_commits(r, &todo_list, opts);
Johannes Schindelin004fefa2016-10-21 12:24:415289 todo_list_release(&todo_list);
5290 return res;
Ramkumar Ramachandra043a4492012-01-11 18:15:575291}
Miklos Vajna5ed75e22012-09-14 06:52:035292
Jeff King66e83d92018-08-23 00:50:515293void append_signoff(struct strbuf *msgbuf, size_t ignore_footer, unsigned flag)
Miklos Vajna5ed75e22012-09-14 06:52:035294{
Brandon Caseybab4d102013-02-12 10:17:355295 unsigned no_dup_sob = flag & APPEND_SIGNOFF_DEDUP;
Miklos Vajna5ed75e22012-09-14 06:52:035296 struct strbuf sob = STRBUF_INIT;
Brandon Caseybab4d102013-02-12 10:17:355297 int has_footer;
Miklos Vajna5ed75e22012-09-14 06:52:035298
5299 strbuf_addstr(&sob, sign_off_header);
William Hubbs39ab4d02019-02-04 18:48:505300 strbuf_addstr(&sob, fmt_name(WANT_COMMITTER_IDENT));
Miklos Vajna5ed75e22012-09-14 06:52:035301 strbuf_addch(&sob, '\n');
Brandon Caseybab4d102013-02-12 10:17:355302
Jonathan Tan44dc7382017-04-26 20:50:035303 if (!ignore_footer)
5304 strbuf_complete_line(msgbuf);
5305
Brandon Caseybab4d102013-02-12 10:17:355306 /*
5307 * If the whole message buffer is equal to the sob, pretend that we
5308 * found a conforming footer with a matching sob
5309 */
5310 if (msgbuf->len - ignore_footer == sob.len &&
5311 !strncmp(msgbuf->buf, sob.buf, sob.len))
5312 has_footer = 3;
5313 else
5314 has_footer = has_conforming_footer(msgbuf, &sob, ignore_footer);
5315
Brandon Casey33f2f9a2013-02-12 10:33:425316 if (!has_footer) {
5317 const char *append_newlines = NULL;
5318 size_t len = msgbuf->len - ignore_footer;
5319
Brandon Casey8c613fd2013-02-22 22:05:275320 if (!len) {
5321 /*
5322 * The buffer is completely empty. Leave foom for
5323 * the title and body to be filled in by the user.
5324 */
Brandon Casey33f2f9a2013-02-12 10:33:425325 append_newlines = "\n\n";
Brandon Casey8c613fd2013-02-22 22:05:275326 } else if (len == 1) {
5327 /*
5328 * Buffer contains a single newline. Add another
5329 * so that we leave room for the title and body.
5330 */
Brandon Casey33f2f9a2013-02-12 10:33:425331 append_newlines = "\n";
Brandon Casey8c613fd2013-02-22 22:05:275332 } else if (msgbuf->buf[len - 2] != '\n') {
5333 /*
5334 * Buffer ends with a single newline. Add another
5335 * so that there is an empty line between the message
5336 * body and the sob.
5337 */
5338 append_newlines = "\n";
5339 } /* else, the buffer already ends with two newlines. */
Brandon Casey33f2f9a2013-02-12 10:33:425340
5341 if (append_newlines)
5342 strbuf_splice(msgbuf, msgbuf->len - ignore_footer, 0,
5343 append_newlines, strlen(append_newlines));
Miklos Vajna5ed75e22012-09-14 06:52:035344 }
Brandon Caseybab4d102013-02-12 10:17:355345
5346 if (has_footer != 3 && (!no_dup_sob || has_footer != 2))
5347 strbuf_splice(msgbuf, msgbuf->len - ignore_footer, 0,
5348 sob.buf, sob.len);
5349
Miklos Vajna5ed75e22012-09-14 06:52:035350 strbuf_release(&sob);
5351}
Johannes Schindelin62db5242017-07-14 14:44:585352
Johannes Schindelin1644c732018-04-25 12:29:035353struct labels_entry {
5354 struct hashmap_entry entry;
5355 char label[FLEX_ARRAY];
5356};
5357
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 17:09:485358static int labels_cmp(const void *fndata UNUSED,
Jeff King02c3c592022-08-19 10:08:465359 const struct hashmap_entry *eptr,
Eric Wong939af162019-10-06 23:30:375360 const struct hashmap_entry *entry_or_key, const void *key)
Johannes Schindelin1644c732018-04-25 12:29:035361{
Eric Wong939af162019-10-06 23:30:375362 const struct labels_entry *a, *b;
5363
5364 a = container_of(eptr, const struct labels_entry, entry);
5365 b = container_of(entry_or_key, const struct labels_entry, entry);
5366
Johannes Schindelin1644c732018-04-25 12:29:035367 return key ? strcmp(a->label, key) : strcmp(a->label, b->label);
5368}
5369
5370struct string_entry {
5371 struct oidmap_entry entry;
5372 char string[FLEX_ARRAY];
5373};
5374
5375struct label_state {
5376 struct oidmap commit2label;
5377 struct hashmap labels;
5378 struct strbuf buf;
Johannes Schindelinac300bd2023-08-10 16:35:005379 int max_label_length;
Johannes Schindelin1644c732018-04-25 12:29:035380};
5381
5382static const char *label_oid(struct object_id *oid, const char *label,
5383 struct label_state *state)
5384{
5385 struct labels_entry *labels_entry;
5386 struct string_entry *string_entry;
5387 struct object_id dummy;
Johannes Schindelin1644c732018-04-25 12:29:035388 int i;
5389
5390 string_entry = oidmap_get(&state->commit2label, oid);
5391 if (string_entry)
5392 return string_entry->string;
5393
5394 /*
5395 * For "uninteresting" commits, i.e. commits that are not to be
5396 * rebased, and which can therefore not be labeled, we use a unique
5397 * abbreviation of the commit name. This is slightly more complicated
Ævar Arnfjörð Bjarmasonc7c33f52023-03-28 13:58:575398 * than calling repo_find_unique_abbrev() because we also need to make
Johannes Schindelin1644c732018-04-25 12:29:035399 * sure that the abbreviation does not conflict with any other
5400 * label.
5401 *
5402 * We disallow "interesting" commits to be labeled by a string that
5403 * is a valid full-length hash, to ensure that we always can find an
5404 * abbreviation for any uninteresting commit's names that does not
5405 * clash with any other label.
5406 */
Johannes Schindelin867bc1d2019-11-17 23:16:095407 strbuf_reset(&state->buf);
Johannes Schindelin1644c732018-04-25 12:29:035408 if (!label) {
5409 char *p;
5410
brian m. carlson4439c7a2019-08-18 20:04:165411 strbuf_grow(&state->buf, GIT_MAX_HEXSZ);
Johannes Schindelin1644c732018-04-25 12:29:035412 label = p = state->buf.buf;
5413
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 13:58:465414 repo_find_unique_abbrev_r(the_repository, p, oid,
5415 default_abbrev);
Johannes Schindelin1644c732018-04-25 12:29:035416
5417 /*
5418 * We may need to extend the abbreviated hash so that there is
5419 * no conflicting label.
5420 */
5421 if (hashmap_get_from_hash(&state->labels, strihash(p), p)) {
5422 size_t i = strlen(p) + 1;
5423
5424 oid_to_hex_r(p, oid);
brian m. carlson4439c7a2019-08-18 20:04:165425 for (; i < the_hash_algo->hexsz; i++) {
Johannes Schindelin1644c732018-04-25 12:29:035426 char save = p[i];
5427 p[i] = '\0';
5428 if (!hashmap_get_from_hash(&state->labels,
5429 strihash(p), p))
5430 break;
5431 p[i] = save;
5432 }
5433 }
Johannes Schindelin867bc1d2019-11-17 23:16:095434 } else {
Johannes Schindelin1644c732018-04-25 12:29:035435 struct strbuf *buf = &state->buf;
Mark Ruvald Pedersen7481d2b2023-08-10 16:34:595436 int label_is_utf8 = 1; /* start with this assumption */
Johannes Schindelinac300bd2023-08-10 16:35:005437 size_t max_len = buf->len + state->max_label_length;
Johannes Schindelin1644c732018-04-25 12:29:035438
Matthew Rogerscd552222019-11-17 23:16:105439 /*
5440 * Sanitize labels by replacing non-alpha-numeric characters
5441 * (including white-space ones) by dashes, as they might be
5442 * illegal in file names (and hence in ref names).
5443 *
5444 * Note that we retain non-ASCII UTF-8 characters (identified
5445 * via the most significant bit). They should be all acceptable
Mark Ruvald Pedersen7481d2b2023-08-10 16:34:595446 * in file names.
5447 *
5448 * As we will use the labels as names of (loose) refs, it is
5449 * vital that the name not be longer than the maximum component
5450 * size of the file system (`NAME_MAX`). We are careful to
5451 * truncate the label accordingly, allowing for the `.lock`
5452 * suffix and for the label to be UTF-8 encoded (i.e. we avoid
5453 * truncating in the middle of a character).
Matthew Rogerscd552222019-11-17 23:16:105454 */
Mark Ruvald Pedersen7481d2b2023-08-10 16:34:595455 for (; *label && buf->len + 1 < max_len; label++)
5456 if (isalnum(*label) ||
5457 (!label_is_utf8 && (*label & 0x80)))
Matthew Rogerscd552222019-11-17 23:16:105458 strbuf_addch(buf, *label);
Mark Ruvald Pedersen7481d2b2023-08-10 16:34:595459 else if (*label & 0x80) {
5460 const char *p = label;
5461
5462 utf8_width(&p, NULL);
5463 if (p) {
5464 if (buf->len + (p - label) > max_len)
5465 break;
5466 strbuf_add(buf, label, p - label);
5467 label = p - 1;
5468 } else {
5469 label_is_utf8 = 0;
5470 strbuf_addch(buf, *label);
5471 }
Matthew Rogerscd552222019-11-17 23:16:105472 /* avoid leading dash and double-dashes */
Mark Ruvald Pedersen7481d2b2023-08-10 16:34:595473 } else if (buf->len && buf->buf[buf->len - 1] != '-')
Matthew Rogerscd552222019-11-17 23:16:105474 strbuf_addch(buf, '-');
5475 if (!buf->len) {
5476 strbuf_addstr(buf, "rev-");
5477 strbuf_add_unique_abbrev(buf, oid, default_abbrev);
Johannes Schindelin1644c732018-04-25 12:29:035478 }
Johannes Schindelin1644c732018-04-25 12:29:035479 label = buf->buf;
Johannes Schindelin867bc1d2019-11-17 23:16:095480
5481 if ((buf->len == the_hash_algo->hexsz &&
5482 !get_oid_hex(label, &dummy)) ||
5483 (buf->len == 1 && *label == '#') ||
5484 hashmap_get_from_hash(&state->labels,
5485 strihash(label), label)) {
5486 /*
5487 * If the label already exists, or if the label is a
5488 * valid full OID, or the label is a '#' (which we use
5489 * as a separator between merge heads and oneline), we
5490 * append a dash and a number to make it unique.
5491 */
5492 size_t len = buf->len;
5493
5494 for (i = 2; ; i++) {
5495 strbuf_setlen(buf, len);
5496 strbuf_addf(buf, "-%d", i);
5497 if (!hashmap_get_from_hash(&state->labels,
5498 strihash(buf->buf),
5499 buf->buf))
5500 break;
5501 }
5502
5503 label = buf->buf;
5504 }
Johannes Schindelin1644c732018-04-25 12:29:035505 }
5506
5507 FLEX_ALLOC_STR(labels_entry, label, label);
Eric Wongd22245a2019-10-06 23:30:275508 hashmap_entry_init(&labels_entry->entry, strihash(label));
Eric Wongb94e5c12019-10-06 23:30:295509 hashmap_add(&state->labels, &labels_entry->entry);
Johannes Schindelin1644c732018-04-25 12:29:035510
5511 FLEX_ALLOC_STR(string_entry, string, label);
5512 oidcpy(&string_entry->entry.oid, oid);
5513 oidmap_put(&state->commit2label, string_entry);
5514
5515 return string_entry->string;
5516}
5517
5518static int make_script_with_merges(struct pretty_print_context *pp,
Alban Gruind358fc22019-03-05 19:17:565519 struct rev_info *revs, struct strbuf *out,
Johannes Schindelin1644c732018-04-25 12:29:035520 unsigned flags)
5521{
Elijah Newrenb9cbd292020-04-11 02:44:255522 int keep_empty = flags & TODO_LIST_KEEP_EMPTY;
Johannes Schindelin7543f6f2018-04-25 12:29:405523 int rebase_cousins = flags & TODO_LIST_REBASE_COUSINS;
Johannes Schindeline1fac532019-07-31 15:18:495524 int root_with_onto = flags & TODO_LIST_ROOT_WITH_ONTO;
Josh Steadmon767a4ca2021-08-30 21:46:025525 int skipped_commit = 0;
Johannes Schindelin1644c732018-04-25 12:29:035526 struct strbuf buf = STRBUF_INIT, oneline = STRBUF_INIT;
5527 struct strbuf label = STRBUF_INIT;
5528 struct commit_list *commits = NULL, **tail = &commits, *iter;
5529 struct commit_list *tips = NULL, **tips_tail = &tips;
5530 struct commit *commit;
5531 struct oidmap commit2todo = OIDMAP_INIT;
5532 struct string_entry *entry;
5533 struct oidset interesting = OIDSET_INIT, child_seen = OIDSET_INIT,
5534 shown = OIDSET_INIT;
Johannes Schindelinac300bd2023-08-10 16:35:005535 struct label_state state =
5536 { OIDMAP_INIT, { NULL }, STRBUF_INIT, GIT_MAX_LABEL_LENGTH };
Johannes Schindelin1644c732018-04-25 12:29:035537
5538 int abbr = flags & TODO_LIST_ABBREVIATE_CMDS;
5539 const char *cmd_pick = abbr ? "p" : "pick",
5540 *cmd_label = abbr ? "l" : "label",
5541 *cmd_reset = abbr ? "t" : "reset",
5542 *cmd_merge = abbr ? "m" : "merge";
5543
Johannes Schindelinac300bd2023-08-10 16:35:005544 git_config_get_int("rebase.maxlabellength", &state.max_label_length);
5545
Johannes Schindelin1644c732018-04-25 12:29:035546 oidmap_init(&commit2todo, 0);
5547 oidmap_init(&state.commit2label, 0);
Eric Wong939af162019-10-06 23:30:375548 hashmap_init(&state.labels, labels_cmp, NULL, 0);
Johannes Schindelin1644c732018-04-25 12:29:035549 strbuf_init(&state.buf, 32);
5550
5551 if (revs->cmdline.nr && (revs->cmdline.rev[0].flags & BOTTOM)) {
Doan Tran Cong Danhe02058a2019-11-18 11:57:475552 struct labels_entry *onto_label_entry;
Johannes Schindelin1644c732018-04-25 12:29:035553 struct object_id *oid = &revs->cmdline.rev[0].item->oid;
5554 FLEX_ALLOC_STR(entry, string, "onto");
5555 oidcpy(&entry->entry.oid, oid);
5556 oidmap_put(&state.commit2label, entry);
Doan Tran Cong Danhe02058a2019-11-18 11:57:475557
5558 FLEX_ALLOC_STR(onto_label_entry, label, "onto");
5559 hashmap_entry_init(&onto_label_entry->entry, strihash("onto"));
5560 hashmap_add(&state.labels, &onto_label_entry->entry);
Johannes Schindelin1644c732018-04-25 12:29:035561 }
5562
5563 /*
5564 * First phase:
5565 * - get onelines for all commits
5566 * - gather all branch tips (i.e. 2nd or later parents of merges)
5567 * - label all branch tips
5568 */
5569 while ((commit = get_revision(revs))) {
5570 struct commit_list *to_merge;
Johannes Schindelin1644c732018-04-25 12:29:035571 const char *p1, *p2;
5572 struct object_id *oid;
5573 int is_empty;
5574
5575 tail = &commit_list_insert(commit, tail)->next;
5576 oidset_insert(&interesting, &commit->object.oid);
5577
5578 is_empty = is_original_commit_empty(commit);
Josh Steadmon767a4ca2021-08-30 21:46:025579 if (!is_empty && (commit->object.flags & PATCHSAME)) {
5580 if (flags & TODO_LIST_WARN_SKIPPED_CHERRY_PICKS)
5581 warning(_("skipped previously applied commit %s"),
Jeff Kingcb646ff2023-08-29 23:43:395582 short_commit_name(the_repository, commit));
Josh Steadmon767a4ca2021-08-30 21:46:025583 skipped_commit = 1;
Johannes Schindelin1644c732018-04-25 12:29:035584 continue;
Josh Steadmon767a4ca2021-08-30 21:46:025585 }
Elijah Newrenb9cbd292020-04-11 02:44:255586 if (is_empty && !keep_empty)
5587 continue;
Johannes Schindelin1644c732018-04-25 12:29:035588
5589 strbuf_reset(&oneline);
5590 pretty_print_commit(pp, commit, &oneline);
5591
5592 to_merge = commit->parents ? commit->parents->next : NULL;
5593 if (!to_merge) {
5594 /* non-merge commit: easy case */
5595 strbuf_reset(&buf);
Johannes Schindelin1644c732018-04-25 12:29:035596 strbuf_addf(&buf, "%s %s %s", cmd_pick,
5597 oid_to_hex(&commit->object.oid),
5598 oneline.buf);
Elijah Newren1b5735f2020-04-11 02:44:245599 if (is_empty)
5600 strbuf_addf(&buf, " %c empty",
5601 comment_line_char);
Johannes Schindelin1644c732018-04-25 12:29:035602
5603 FLEX_ALLOC_STR(entry, string, buf.buf);
5604 oidcpy(&entry->entry.oid, &commit->object.oid);
5605 oidmap_put(&commit2todo, entry);
5606
5607 continue;
5608 }
5609
Johannes Schindelin1644c732018-04-25 12:29:035610 /* Create a label */
5611 strbuf_reset(&label);
5612 if (skip_prefix(oneline.buf, "Merge ", &p1) &&
5613 (p1 = strchr(p1, '\'')) &&
5614 (p2 = strchr(++p1, '\'')))
5615 strbuf_add(&label, p1, p2 - p1);
5616 else if (skip_prefix(oneline.buf, "Merge pull request ",
5617 &p1) &&
5618 (p1 = strstr(p1, " from ")))
5619 strbuf_addstr(&label, p1 + strlen(" from "));
5620 else
5621 strbuf_addbuf(&label, &oneline);
5622
Johannes Schindelin1644c732018-04-25 12:29:035623 strbuf_reset(&buf);
5624 strbuf_addf(&buf, "%s -C %s",
5625 cmd_merge, oid_to_hex(&commit->object.oid));
5626
Johannes Schindelin2b6ad0f2017-12-21 14:52:455627 /* label the tips of merged branches */
5628 for (; to_merge; to_merge = to_merge->next) {
5629 oid = &to_merge->item->object.oid;
5630 strbuf_addch(&buf, ' ');
Johannes Schindelin1644c732018-04-25 12:29:035631
Johannes Schindelin2b6ad0f2017-12-21 14:52:455632 if (!oidset_contains(&interesting, oid)) {
5633 strbuf_addstr(&buf, label_oid(oid, NULL,
5634 &state));
5635 continue;
5636 }
5637
Johannes Schindelin1644c732018-04-25 12:29:035638 tips_tail = &commit_list_insert(to_merge->item,
5639 tips_tail)->next;
5640
5641 strbuf_addstr(&buf, label_oid(oid, label.buf, &state));
5642 }
5643 strbuf_addf(&buf, " # %s", oneline.buf);
5644
5645 FLEX_ALLOC_STR(entry, string, buf.buf);
5646 oidcpy(&entry->entry.oid, &commit->object.oid);
5647 oidmap_put(&commit2todo, entry);
5648 }
Josh Steadmon767a4ca2021-08-30 21:46:025649 if (skipped_commit)
5650 advise_if_enabled(ADVICE_SKIPPED_CHERRY_PICKS,
5651 _("use --reapply-cherry-picks to include skipped commits"));
Johannes Schindelin1644c732018-04-25 12:29:035652
5653 /*
5654 * Second phase:
5655 * - label branch points
5656 * - add HEAD to the branch tips
5657 */
5658 for (iter = commits; iter; iter = iter->next) {
5659 struct commit_list *parent = iter->item->parents;
5660 for (; parent; parent = parent->next) {
5661 struct object_id *oid = &parent->item->object.oid;
5662 if (!oidset_contains(&interesting, oid))
5663 continue;
René Scharfe6e8fc702018-10-03 13:06:495664 if (oidset_insert(&child_seen, oid))
Johannes Schindelin1644c732018-04-25 12:29:035665 label_oid(oid, "branch-point", &state);
5666 }
5667
Elijah Newren15beaaa2019-11-05 17:07:235668 /* Add HEAD as implicit "tip of branch" */
Johannes Schindelin1644c732018-04-25 12:29:035669 if (!iter->next)
5670 tips_tail = &commit_list_insert(iter->item,
5671 tips_tail)->next;
5672 }
5673
5674 /*
5675 * Third phase: output the todo list. This is a bit tricky, as we
5676 * want to avoid jumping back and forth between revisions. To
5677 * accomplish that goal, we walk backwards from the branch tips,
5678 * gathering commits not yet shown, reversing the list on the fly,
5679 * then outputting that list (labeling revisions as needed).
5680 */
Alban Gruind358fc22019-03-05 19:17:565681 strbuf_addf(out, "%s onto\n", cmd_label);
Johannes Schindelin1644c732018-04-25 12:29:035682 for (iter = tips; iter; iter = iter->next) {
5683 struct commit_list *list = NULL, *iter2;
5684
5685 commit = iter->item;
5686 if (oidset_contains(&shown, &commit->object.oid))
5687 continue;
5688 entry = oidmap_get(&state.commit2label, &commit->object.oid);
5689
5690 if (entry)
Alban Gruind358fc22019-03-05 19:17:565691 strbuf_addf(out, "\n%c Branch %s\n", comment_line_char, entry->string);
Johannes Schindelin1644c732018-04-25 12:29:035692 else
Alban Gruind358fc22019-03-05 19:17:565693 strbuf_addch(out, '\n');
Johannes Schindelin1644c732018-04-25 12:29:035694
5695 while (oidset_contains(&interesting, &commit->object.oid) &&
5696 !oidset_contains(&shown, &commit->object.oid)) {
5697 commit_list_insert(commit, &list);
5698 if (!commit->parents) {
5699 commit = NULL;
5700 break;
5701 }
5702 commit = commit->parents->item;
5703 }
5704
5705 if (!commit)
Alban Gruind358fc22019-03-05 19:17:565706 strbuf_addf(out, "%s %s\n", cmd_reset,
Johannes Schindeline1fac532019-07-31 15:18:495707 rebase_cousins || root_with_onto ?
5708 "onto" : "[new root]");
Johannes Schindelin1644c732018-04-25 12:29:035709 else {
5710 const char *to = NULL;
5711
5712 entry = oidmap_get(&state.commit2label,
5713 &commit->object.oid);
5714 if (entry)
5715 to = entry->string;
Johannes Schindelin7543f6f2018-04-25 12:29:405716 else if (!rebase_cousins)
5717 to = label_oid(&commit->object.oid, NULL,
5718 &state);
Johannes Schindelin1644c732018-04-25 12:29:035719
5720 if (!to || !strcmp(to, "onto"))
Alban Gruind358fc22019-03-05 19:17:565721 strbuf_addf(out, "%s onto\n", cmd_reset);
Johannes Schindelin1644c732018-04-25 12:29:035722 else {
5723 strbuf_reset(&oneline);
5724 pretty_print_commit(pp, commit, &oneline);
Alban Gruind358fc22019-03-05 19:17:565725 strbuf_addf(out, "%s %s # %s\n",
5726 cmd_reset, to, oneline.buf);
Johannes Schindelin1644c732018-04-25 12:29:035727 }
5728 }
5729
5730 for (iter2 = list; iter2; iter2 = iter2->next) {
5731 struct object_id *oid = &iter2->item->object.oid;
5732 entry = oidmap_get(&commit2todo, oid);
5733 /* only show if not already upstream */
5734 if (entry)
Alban Gruind358fc22019-03-05 19:17:565735 strbuf_addf(out, "%s\n", entry->string);
Johannes Schindelin1644c732018-04-25 12:29:035736 entry = oidmap_get(&state.commit2label, oid);
5737 if (entry)
Alban Gruind358fc22019-03-05 19:17:565738 strbuf_addf(out, "%s %s\n",
5739 cmd_label, entry->string);
Johannes Schindelin1644c732018-04-25 12:29:035740 oidset_insert(&shown, oid);
5741 }
5742
5743 free_commit_list(list);
5744 }
5745
5746 free_commit_list(commits);
5747 free_commit_list(tips);
5748
5749 strbuf_release(&label);
5750 strbuf_release(&oneline);
5751 strbuf_release(&buf);
5752
5753 oidmap_free(&commit2todo, 1);
5754 oidmap_free(&state.commit2label, 1);
Elijah Newren6da1a252020-11-02 18:55:055755 hashmap_clear_and_free(&state.labels, struct labels_entry, entry);
Johannes Schindelin1644c732018-04-25 12:29:035756 strbuf_release(&state.buf);
5757
5758 return 0;
5759}
5760
Alban Gruind358fc22019-03-05 19:17:565761int sequencer_make_script(struct repository *r, struct strbuf *out, int argc,
5762 const char **argv, unsigned flags)
Johannes Schindelin62db5242017-07-14 14:44:585763{
5764 char *format = NULL;
5765 struct pretty_print_context pp = {0};
Johannes Schindelin62db5242017-07-14 14:44:585766 struct rev_info revs;
5767 struct commit *commit;
Elijah Newrenb9cbd292020-04-11 02:44:255768 int keep_empty = flags & TODO_LIST_KEEP_EMPTY;
Liam Beguind8ae6c82017-12-05 17:52:345769 const char *insn = flags & TODO_LIST_ABBREVIATE_CMDS ? "p" : "pick";
Johannes Schindelin1644c732018-04-25 12:29:035770 int rebase_merges = flags & TODO_LIST_REBASE_MERGES;
Jonathan Tan0fcb4f62020-04-11 02:44:275771 int reapply_cherry_picks = flags & TODO_LIST_REAPPLY_CHERRY_PICKS;
Josh Steadmon767a4ca2021-08-30 21:46:025772 int skipped_commit = 0;
Ævar Arnfjörð Bjarmason0139c582022-04-13 20:01:405773 int ret = 0;
Johannes Schindelin62db5242017-07-14 14:44:585774
Nguyễn Thái Ngọc Duyf11c9582018-11-10 05:48:565775 repo_init_revisions(r, &revs, NULL);
Johannes Schindelin62db5242017-07-14 14:44:585776 revs.verbose_header = 1;
Johannes Schindelin1644c732018-04-25 12:29:035777 if (!rebase_merges)
5778 revs.max_parents = 1;
Jonathan Tan0fcb4f62020-04-11 02:44:275779 revs.cherry_mark = !reapply_cherry_picks;
Johannes Schindelin62db5242017-07-14 14:44:585780 revs.limited = 1;
5781 revs.reverse = 1;
5782 revs.right_only = 1;
5783 revs.sort_order = REV_SORT_IN_GRAPH_ORDER;
5784 revs.topo_order = 1;
5785
5786 revs.pretty_given = 1;
5787 git_config_get_string("rebase.instructionFormat", &format);
5788 if (!format || !*format) {
5789 free(format);
5790 format = xstrdup("%s");
5791 }
5792 get_commit_format(format, &revs);
5793 free(format);
5794 pp.fmt = revs.commit_format;
5795 pp.output_encoding = get_log_output_encoding();
5796
Ævar Arnfjörð Bjarmason0139c582022-04-13 20:01:405797 if (setup_revisions(argc, argv, &revs, NULL) > 1) {
5798 ret = error(_("make_script: unhandled options"));
5799 goto cleanup;
5800 }
Johannes Schindelin62db5242017-07-14 14:44:585801
Ævar Arnfjörð Bjarmason0139c582022-04-13 20:01:405802 if (prepare_revision_walk(&revs) < 0) {
5803 ret = error(_("make_script: error preparing revisions"));
5804 goto cleanup;
5805 }
Johannes Schindelin62db5242017-07-14 14:44:585806
Ævar Arnfjörð Bjarmason0139c582022-04-13 20:01:405807 if (rebase_merges) {
5808 ret = make_script_with_merges(&pp, &revs, out, flags);
5809 goto cleanup;
5810 }
Johannes Schindelin1644c732018-04-25 12:29:035811
Johannes Schindelin62db5242017-07-14 14:44:585812 while ((commit = get_revision(&revs))) {
Elijah Newrend48e5e22020-02-15 21:36:245813 int is_empty = is_original_commit_empty(commit);
Phillip Wood76ea2352018-03-20 10:03:145814
Josh Steadmon767a4ca2021-08-30 21:46:025815 if (!is_empty && (commit->object.flags & PATCHSAME)) {
5816 if (flags & TODO_LIST_WARN_SKIPPED_CHERRY_PICKS)
5817 warning(_("skipped previously applied commit %s"),
Jeff Kingcb646ff2023-08-29 23:43:395818 short_commit_name(r, commit));
Josh Steadmon767a4ca2021-08-30 21:46:025819 skipped_commit = 1;
Phillip Wood76ea2352018-03-20 10:03:145820 continue;
Josh Steadmon767a4ca2021-08-30 21:46:025821 }
Elijah Newrenb9cbd292020-04-11 02:44:255822 if (is_empty && !keep_empty)
5823 continue;
Alban Gruind358fc22019-03-05 19:17:565824 strbuf_addf(out, "%s %s ", insn,
Liam Beguind8ae6c82017-12-05 17:52:345825 oid_to_hex(&commit->object.oid));
Alban Gruind358fc22019-03-05 19:17:565826 pretty_print_commit(&pp, commit, out);
Elijah Newren1b5735f2020-04-11 02:44:245827 if (is_empty)
5828 strbuf_addf(out, " %c empty", comment_line_char);
Alban Gruind358fc22019-03-05 19:17:565829 strbuf_addch(out, '\n');
Johannes Schindelin62db5242017-07-14 14:44:585830 }
Josh Steadmon767a4ca2021-08-30 21:46:025831 if (skipped_commit)
5832 advise_if_enabled(ADVICE_SKIPPED_CHERRY_PICKS,
5833 _("use --reapply-cherry-picks to include skipped commits"));
Ævar Arnfjörð Bjarmason0139c582022-04-13 20:01:405834cleanup:
5835 release_revisions(&revs);
5836 return ret;
Johannes Schindelin62db5242017-07-14 14:44:585837}
Johannes Schindelin3546c8d2017-07-14 14:45:115838
Liam Beguin0cce4a22017-12-05 17:52:335839/*
5840 * Add commands after pick and (series of) squash/fixup commands
5841 * in the todo list.
5842 */
Johannes Schindelin17919c32021-09-07 21:05:125843static void todo_list_add_exec_commands(struct todo_list *todo_list,
5844 struct string_list *commands)
Johannes Schindelin3546c8d2017-07-14 14:45:115845{
Alban Gruin683153a2019-03-05 19:17:545846 struct strbuf *buf = &todo_list->buf;
5847 size_t base_offset = buf->len;
5848 int i, insert, nr = 0, alloc = 0;
5849 struct todo_item *items = NULL, *base_items = NULL;
Johannes Schindelin3546c8d2017-07-14 14:45:115850
René Scharfeca56dad2021-03-13 16:17:225851 CALLOC_ARRAY(base_items, commands->nr);
Alban Gruin683153a2019-03-05 19:17:545852 for (i = 0; i < commands->nr; i++) {
5853 size_t command_len = strlen(commands->items[i].string);
Johannes Schindelin3546c8d2017-07-14 14:45:115854
Alban Gruin683153a2019-03-05 19:17:545855 strbuf_addstr(buf, commands->items[i].string);
5856 strbuf_addch(buf, '\n');
5857
5858 base_items[i].command = TODO_EXEC;
5859 base_items[i].offset_in_buf = base_offset;
Phillip Woode57d2c52023-01-12 16:50:015860 base_items[i].arg_offset = base_offset;
5861 base_items[i].arg_len = command_len;
Alban Gruin683153a2019-03-05 19:17:545862
5863 base_offset += command_len + 1;
Johannes Schindelin3546c8d2017-07-14 14:45:115864 }
5865
Johannes Schindelin1ace63b2018-08-09 09:41:115866 /*
5867 * Insert <commands> after every pick. Here, fixup/squash chains
5868 * are considered part of the pick, so we insert the commands *after*
5869 * those chains if there are any.
Alban Gruin683153a2019-03-05 19:17:545870 *
Elijah Newren15beaaa2019-11-05 17:07:235871 * As we insert the exec commands immediately after rearranging
Alban Gruin683153a2019-03-05 19:17:545872 * any fixups and before the user edits the list, a fixup chain
5873 * can never contain comments (any comments are empty picks that
5874 * have been commented out because the user did not specify
5875 * --keep-empty). So, it is safe to insert an exec command
5876 * without looking at the command following a comment.
Johannes Schindelin1ace63b2018-08-09 09:41:115877 */
Alban Gruin683153a2019-03-05 19:17:545878 insert = 0;
5879 for (i = 0; i < todo_list->nr; i++) {
5880 enum todo_command command = todo_list->items[i].command;
5881 if (insert && !is_fixup(command)) {
5882 ALLOC_GROW(items, nr + commands->nr, alloc);
5883 COPY_ARRAY(items + nr, base_items, commands->nr);
5884 nr += commands->nr;
Johannes Schindelin1ace63b2018-08-09 09:41:115885
Alban Gruin683153a2019-03-05 19:17:545886 insert = 0;
Johannes Schindelin3546c8d2017-07-14 14:45:115887 }
Johannes Schindelin1ace63b2018-08-09 09:41:115888
Alban Gruin683153a2019-03-05 19:17:545889 ALLOC_GROW(items, nr + 1, alloc);
5890 items[nr++] = todo_list->items[i];
5891
Johannes Schindelin1ace63b2018-08-09 09:41:115892 if (command == TODO_PICK || command == TODO_MERGE)
Alban Gruin683153a2019-03-05 19:17:545893 insert = 1;
Johannes Schindelin3546c8d2017-07-14 14:45:115894 }
Liam Beguin0cce4a22017-12-05 17:52:335895
Johannes Schindelin1ace63b2018-08-09 09:41:115896 /* insert or append final <commands> */
Elijah Newrencc9dcde2021-11-30 03:58:395897 if (insert) {
Alban Gruin683153a2019-03-05 19:17:545898 ALLOC_GROW(items, nr + commands->nr, alloc);
5899 COPY_ARRAY(items + nr, base_items, commands->nr);
5900 nr += commands->nr;
Johannes Schindelin3546c8d2017-07-14 14:45:115901 }
5902
Alban Gruin683153a2019-03-05 19:17:545903 free(base_items);
5904 FREE_AND_NULL(todo_list->items);
5905 todo_list->items = items;
5906 todo_list->nr = nr;
5907 todo_list->alloc = alloc;
5908}
5909
Jeff Kingcb646ff2023-08-29 23:43:395910static void todo_list_to_strbuf(struct repository *r,
5911 struct todo_list *todo_list,
Alban Gruin616d7742019-01-29 15:01:485912 struct strbuf *buf, int num, unsigned flags)
Johannes Schindelin3546c8d2017-07-14 14:45:115913{
Johannes Schindelin3546c8d2017-07-14 14:45:115914 struct todo_item *item;
Alban Gruin616d7742019-01-29 15:01:485915 int i, max = todo_list->nr;
Johannes Schindelin3546c8d2017-07-14 14:45:115916
Alban Gruin616d7742019-01-29 15:01:485917 if (num > 0 && num < max)
5918 max = num;
5919
5920 for (item = todo_list->items, i = 0; i < max; i++, item++) {
Alban Gruin68e70902020-03-30 12:42:355921 char cmd;
5922
Liam Beguin8dccc7a2017-12-05 17:52:305923 /* if the item is not a command write it and continue */
5924 if (item->command >= TODO_COMMENT) {
Alban Gruin616d7742019-01-29 15:01:485925 strbuf_addf(buf, "%.*s\n", item->arg_len,
Alban Gruincbef27d2019-01-29 15:01:475926 todo_item_get_arg(todo_list, item));
Liam Beguin8dccc7a2017-12-05 17:52:305927 continue;
Johannes Schindelin3546c8d2017-07-14 14:45:115928 }
Liam Beguin8dccc7a2017-12-05 17:52:305929
5930 /* add command to the buffer */
Alban Gruin68e70902020-03-30 12:42:355931 cmd = command_to_char(item->command);
5932 if ((flags & TODO_LIST_ABBREVIATE_CMDS) && cmd)
5933 strbuf_addch(buf, cmd);
Liam Beguind8ae6c82017-12-05 17:52:345934 else
Alban Gruin616d7742019-01-29 15:01:485935 strbuf_addstr(buf, command_to_string(item->command));
Liam Beguin8dccc7a2017-12-05 17:52:305936
5937 /* add commit id */
5938 if (item->commit) {
Liam Beguin313a48e2017-12-05 17:52:325939 const char *oid = flags & TODO_LIST_SHORTEN_IDS ?
Jeff Kingcb646ff2023-08-29 23:43:395940 short_commit_name(r, item->commit) :
Liam Beguin8dccc7a2017-12-05 17:52:305941 oid_to_hex(&item->commit->object.oid);
5942
Charvi Mendiratta9e3cebd2021-01-29 18:20:475943 if (item->command == TODO_FIXUP) {
5944 if (item->flags & TODO_EDIT_FIXUP_MSG)
5945 strbuf_addstr(buf, " -c");
5946 else if (item->flags & TODO_REPLACE_FIXUP_MSG) {
5947 strbuf_addstr(buf, " -C");
5948 }
5949 }
5950
Johannes Schindelin4c68e7d2018-04-25 12:28:545951 if (item->command == TODO_MERGE) {
5952 if (item->flags & TODO_EDIT_MERGE_MSG)
Alban Gruin616d7742019-01-29 15:01:485953 strbuf_addstr(buf, " -c");
Johannes Schindelin4c68e7d2018-04-25 12:28:545954 else
Alban Gruin616d7742019-01-29 15:01:485955 strbuf_addstr(buf, " -C");
Johannes Schindelin4c68e7d2018-04-25 12:28:545956 }
5957
Alban Gruin616d7742019-01-29 15:01:485958 strbuf_addf(buf, " %s", oid);
Liam Beguin8dccc7a2017-12-05 17:52:305959 }
Johannes Schindelin4c68e7d2018-04-25 12:28:545960
Liam Beguin8dccc7a2017-12-05 17:52:305961 /* add all the rest */
Johannes Schindelinc7b4d792017-12-22 23:56:005962 if (!item->arg_len)
Alban Gruin616d7742019-01-29 15:01:485963 strbuf_addch(buf, '\n');
Johannes Schindelinc7b4d792017-12-22 23:56:005964 else
Alban Gruin616d7742019-01-29 15:01:485965 strbuf_addf(buf, " %.*s\n", item->arg_len,
Alban Gruincbef27d2019-01-29 15:01:475966 todo_item_get_arg(todo_list, item));
Johannes Schindelin3546c8d2017-07-14 14:45:115967 }
Johannes Schindelin3546c8d2017-07-14 14:45:115968}
Johannes Schindelin94399942017-07-14 14:45:215969
Alban Gruin616d7742019-01-29 15:01:485970int todo_list_write_to_file(struct repository *r, struct todo_list *todo_list,
5971 const char *file, const char *shortrevisions,
5972 const char *shortonto, int num, unsigned flags)
Johannes Schindelin94399942017-07-14 14:45:215973{
Alban Gruinaf1fc3a2019-03-05 19:18:025974 int res;
Alban Gruin616d7742019-01-29 15:01:485975 struct strbuf buf = STRBUF_INIT;
Johannes Schindelin94399942017-07-14 14:45:215976
Alban Gruin616d7742019-01-29 15:01:485977 todo_list_to_strbuf(r, todo_list, &buf, num, flags);
Alban Gruinaf1fc3a2019-03-05 19:18:025978 if (flags & TODO_LIST_APPEND_TODO_HELP)
Elijah Newrend48e5e22020-02-15 21:36:245979 append_todo_help(count_commands(todo_list),
Alban Gruinaf1fc3a2019-03-05 19:18:025980 shortrevisions, shortonto, &buf);
Johannes Schindelin94399942017-07-14 14:45:215981
Alban Gruin616d7742019-01-29 15:01:485982 res = write_message(buf.buf, buf.len, file, 0);
Alban Gruincbef27d2019-01-29 15:01:475983 strbuf_release(&buf);
Johannes Schindelin94399942017-07-14 14:45:215984
5985 return res;
5986}
Johannes Schindelincdac2b02017-07-14 14:45:255987
5988/* skip picking commits whose parents are unchanged */
Alban Gruin6bfeb7f2019-03-05 19:18:005989static int skip_unnecessary_picks(struct repository *r,
5990 struct todo_list *todo_list,
5991 struct object_id *base_oid)
Johannes Schindelincdac2b02017-07-14 14:45:255992{
Alban Gruind4ed5d72018-08-10 16:51:365993 struct object_id *parent_oid;
Alban Gruin6bfeb7f2019-03-05 19:18:005994 int i;
Johannes Schindelincdac2b02017-07-14 14:45:255995
Alban Gruin6bfeb7f2019-03-05 19:18:005996 for (i = 0; i < todo_list->nr; i++) {
5997 struct todo_item *item = todo_list->items + i;
Johannes Schindelincdac2b02017-07-14 14:45:255998
5999 if (item->command >= TODO_NOOP)
6000 continue;
6001 if (item->command != TODO_PICK)
6002 break;
Ævar Arnfjörð Bjarmason4a93b892023-03-28 13:58:586003 if (repo_parse_commit(r, item->commit)) {
Johannes Schindelincdac2b02017-07-14 14:45:256004 return error(_("could not parse commit '%s'"),
6005 oid_to_hex(&item->commit->object.oid));
6006 }
6007 if (!item->commit->parents)
6008 break; /* root commit */
6009 if (item->commit->parents->next)
6010 break; /* merge commit */
6011 parent_oid = &item->commit->parents->item->object.oid;
Alban Gruin6bfeb7f2019-03-05 19:18:006012 if (!oideq(parent_oid, base_oid))
Johannes Schindelincdac2b02017-07-14 14:45:256013 break;
Alban Gruin6bfeb7f2019-03-05 19:18:006014 oidcpy(base_oid, &item->commit->object.oid);
Johannes Schindelincdac2b02017-07-14 14:45:256015 }
6016 if (i > 0) {
Johannes Schindelincdac2b02017-07-14 14:45:256017 const char *done_path = rebase_path_done();
6018
Alban Gruin6bfeb7f2019-03-05 19:18:006019 if (todo_list_write_to_file(r, todo_list, done_path, NULL, NULL, i, 0)) {
Johannes Schindelincdac2b02017-07-14 14:45:256020 error_errno(_("could not write to '%s'"), done_path);
Johannes Schindelincdac2b02017-07-14 14:45:256021 return -1;
6022 }
Johannes Schindelincdac2b02017-07-14 14:45:256023
Alban Gruin6bfeb7f2019-03-05 19:18:006024 MOVE_ARRAY(todo_list->items, todo_list->items + i, todo_list->nr - i);
6025 todo_list->nr -= i;
6026 todo_list->current = 0;
Alban Gruin34065542019-11-24 17:43:296027 todo_list->done_nr += i;
Alban Gruin6bfeb7f2019-03-05 19:18:006028
6029 if (is_fixup(peek_command(todo_list, 0)))
6030 record_in_rewritten(base_oid, peek_command(todo_list, 0));
Johannes Schindelincdac2b02017-07-14 14:45:256031 }
6032
Johannes Schindelincdac2b02017-07-14 14:45:256033 return 0;
6034}
Johannes Schindelinc44a4c62017-07-14 14:45:316035
Derrick Stolee900b50c2022-07-19 18:33:396036struct todo_add_branch_context {
6037 struct todo_item *items;
6038 size_t items_nr;
6039 size_t items_alloc;
6040 struct strbuf *buf;
6041 struct commit *commit;
6042 struct string_list refs_to_oids;
6043};
6044
6045static int add_decorations_to_list(const struct commit *commit,
6046 struct todo_add_branch_context *ctx)
6047{
6048 const struct name_decoration *decoration = get_name_decoration(&commit->object);
Derrick Stoleeaa37f3e2022-07-19 18:33:436049 const char *head_ref = resolve_ref_unsafe("HEAD",
6050 RESOLVE_REF_READING,
6051 NULL,
6052 NULL);
Derrick Stolee900b50c2022-07-19 18:33:396053
6054 while (decoration) {
6055 struct todo_item *item;
6056 const char *path;
6057 size_t base_offset = ctx->buf->len;
6058
Derrick Stoleeaa37f3e2022-07-19 18:33:436059 /*
6060 * If the branch is the current HEAD, then it will be
6061 * updated by the default rebase behavior.
6062 */
6063 if (head_ref && !strcmp(head_ref, decoration->name)) {
6064 decoration = decoration->next;
6065 continue;
6066 }
6067
Derrick Stolee900b50c2022-07-19 18:33:396068 ALLOC_GROW(ctx->items,
6069 ctx->items_nr + 1,
6070 ctx->items_alloc);
6071 item = &ctx->items[ctx->items_nr];
6072 memset(item, 0, sizeof(*item));
6073
6074 /* If the branch is checked out, then leave a comment instead. */
6075 if ((path = branch_checked_out(decoration->name))) {
6076 item->command = TODO_COMMENT;
6077 strbuf_addf(ctx->buf, "# Ref %s checked out at '%s'\n",
6078 decoration->name, path);
6079 } else {
6080 struct string_list_item *sti;
6081 item->command = TODO_UPDATE_REF;
6082 strbuf_addf(ctx->buf, "%s\n", decoration->name);
6083
6084 sti = string_list_insert(&ctx->refs_to_oids,
6085 decoration->name);
Derrick Stolee89fc0b52022-07-19 18:33:406086 sti->util = init_update_ref_record(decoration->name);
Derrick Stolee900b50c2022-07-19 18:33:396087 }
6088
6089 item->offset_in_buf = base_offset;
6090 item->arg_offset = base_offset;
6091 item->arg_len = ctx->buf->len - base_offset;
6092 ctx->items_nr++;
6093
6094 decoration = decoration->next;
6095 }
6096
6097 return 0;
6098}
6099
6100/*
6101 * For each 'pick' command, find out if the commit has a decoration in
6102 * refs/heads/. If so, then add a 'label for-update-refs/' command.
6103 */
6104static int todo_list_add_update_ref_commands(struct todo_list *todo_list)
6105{
Derrick Stolee89fc0b52022-07-19 18:33:406106 int i, res;
Derrick Stolee900b50c2022-07-19 18:33:396107 static struct string_list decorate_refs_exclude = STRING_LIST_INIT_NODUP;
6108 static struct string_list decorate_refs_exclude_config = STRING_LIST_INIT_NODUP;
6109 static struct string_list decorate_refs_include = STRING_LIST_INIT_NODUP;
6110 struct decoration_filter decoration_filter = {
6111 .include_ref_pattern = &decorate_refs_include,
6112 .exclude_ref_pattern = &decorate_refs_exclude,
6113 .exclude_ref_config_pattern = &decorate_refs_exclude_config,
6114 };
6115 struct todo_add_branch_context ctx = {
6116 .buf = &todo_list->buf,
6117 .refs_to_oids = STRING_LIST_INIT_DUP,
6118 };
6119
6120 ctx.items_alloc = 2 * todo_list->nr + 1;
6121 ALLOC_ARRAY(ctx.items, ctx.items_alloc);
6122
6123 string_list_append(&decorate_refs_include, "refs/heads/");
6124 load_ref_decorations(&decoration_filter, 0);
6125
6126 for (i = 0; i < todo_list->nr; ) {
6127 struct todo_item *item = &todo_list->items[i];
6128
6129 /* insert ith item into new list */
6130 ALLOC_GROW(ctx.items,
6131 ctx.items_nr + 1,
6132 ctx.items_alloc);
6133
6134 ctx.items[ctx.items_nr++] = todo_list->items[i++];
6135
6136 if (item->commit) {
6137 ctx.commit = item->commit;
6138 add_decorations_to_list(item->commit, &ctx);
6139 }
6140 }
6141
Derrick Stolee89fc0b52022-07-19 18:33:406142 res = write_update_refs_state(&ctx.refs_to_oids);
6143
Derrick Stolee900b50c2022-07-19 18:33:396144 string_list_clear(&ctx.refs_to_oids, 1);
Derrick Stolee89fc0b52022-07-19 18:33:406145
6146 if (res) {
6147 /* we failed, so clean up the new list. */
6148 free(ctx.items);
6149 return res;
6150 }
6151
Derrick Stolee900b50c2022-07-19 18:33:396152 free(todo_list->items);
6153 todo_list->items = ctx.items;
6154 todo_list->nr = ctx.items_nr;
6155 todo_list->alloc = ctx.items_alloc;
6156
6157 return 0;
6158}
6159
Nguyễn Thái Ngọc Duy005af332018-11-10 05:48:576160int complete_action(struct repository *r, struct replay_opts *opts, unsigned flags,
Alban Gruinb97e1872018-08-28 12:10:366161 const char *shortrevisions, const char *onto_name,
Phillip Woodf3e27a02020-11-04 15:29:386162 struct commit *onto, const struct object_id *orig_head,
Phillip Wood7d3488e2019-04-17 14:30:396163 struct string_list *commands, unsigned autosquash,
Derrick Stolee900b50c2022-07-19 18:33:396164 unsigned update_refs,
Phillip Wood7d3488e2019-04-17 14:30:396165 struct todo_list *todo_list)
Alban Gruinb97e1872018-08-28 12:10:366166{
Antti Keränen5da69c02020-08-13 17:42:576167 char shortonto[GIT_MAX_HEXSZ + 1];
6168 const char *todo_file = rebase_path_todo();
Alban Gruin94bcad72019-03-05 19:17:576169 struct todo_list new_todo = TODO_LIST_INIT;
Johannes Schindelinb6992262020-01-23 12:28:186170 struct strbuf *buf = &todo_list->buf, buf2 = STRBUF_INIT;
Phillip Wood7d3488e2019-04-17 14:30:396171 struct object_id oid = onto->object.oid;
Alban Gruin1451d0f2019-03-05 19:18:046172 int res;
Alban Gruinb97e1872018-08-28 12:10:366173
Ævar Arnfjörð Bjarmason4a93b892023-03-28 13:58:586174 repo_find_unique_abbrev_r(r, shortonto, &oid,
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 13:58:466175 DEFAULT_ABBREV);
Alban Gruinb97e1872018-08-28 12:10:366176
Alban Gruinb97e1872018-08-28 12:10:366177 if (buf->len == 0) {
Alban Gruin94bcad72019-03-05 19:17:576178 struct todo_item *item = append_new_todo(todo_list);
6179 item->command = TODO_NOOP;
6180 item->commit = NULL;
6181 item->arg_len = item->arg_offset = item->flags = item->offset_in_buf = 0;
6182 }
Alban Gruinb97e1872018-08-28 12:10:366183
Derrick Stolee900b50c2022-07-19 18:33:396184 if (update_refs && todo_list_add_update_ref_commands(todo_list))
6185 return -1;
6186
Alban Gruin94bcad72019-03-05 19:17:576187 if (autosquash && todo_list_rearrange_squash(todo_list))
Alban Gruinb97e1872018-08-28 12:10:366188 return -1;
6189
Alban Gruin683153a2019-03-05 19:17:546190 if (commands->nr)
Alban Gruin94bcad72019-03-05 19:17:576191 todo_list_add_exec_commands(todo_list, commands);
Alban Gruinb97e1872018-08-28 12:10:366192
Alban Gruin94bcad72019-03-05 19:17:576193 if (count_commands(todo_list) == 0) {
Denton Liube1bb602020-04-07 14:27:566194 apply_autostash(rebase_path_autostash());
Alban Gruinb97e1872018-08-28 12:10:366195 sequencer_remove_state(opts);
Alban Gruinb97e1872018-08-28 12:10:366196
6197 return error(_("nothing to do"));
6198 }
6199
Alban Gruin1451d0f2019-03-05 19:18:046200 res = edit_todo_list(r, todo_list, &new_todo, shortrevisions,
6201 shortonto, flags);
6202 if (res == -1)
6203 return -1;
6204 else if (res == -2) {
Denton Liube1bb602020-04-07 14:27:566205 apply_autostash(rebase_path_autostash());
Alban Gruinb97e1872018-08-28 12:10:366206 sequencer_remove_state(opts);
Alban Gruinb97e1872018-08-28 12:10:366207
Alban Gruinb97e1872018-08-28 12:10:366208 return -1;
Alban Gruin1451d0f2019-03-05 19:18:046209 } else if (res == -3) {
Denton Liube1bb602020-04-07 14:27:566210 apply_autostash(rebase_path_autostash());
Alban Gruinb97e1872018-08-28 12:10:366211 sequencer_remove_state(opts);
Alban Gruin94bcad72019-03-05 19:17:576212 todo_list_release(&new_todo);
Alban Gruinb97e1872018-08-28 12:10:366213
6214 return error(_("nothing to do"));
Alban Gruin5a5445d2020-01-28 21:12:466215 } else if (res == -4) {
Phillip Wood7d3488e2019-04-17 14:30:396216 checkout_onto(r, opts, onto_name, &onto->object.oid, orig_head);
Alban Gruin94bcad72019-03-05 19:17:576217 todo_list_release(&new_todo);
6218
Alban Gruinb97e1872018-08-28 12:10:366219 return -1;
6220 }
6221
Johannes Schindelinb6992262020-01-23 12:28:186222 /* Expand the commit IDs */
6223 todo_list_to_strbuf(r, &new_todo, &buf2, -1, 0);
6224 strbuf_swap(&new_todo.buf, &buf2);
6225 strbuf_release(&buf2);
Johannes Schindelin170eea92023-05-13 08:11:266226 /* Nothing is done yet, and we're reparsing, so let's reset the count */
6227 new_todo.total_nr = 0;
Johannes Schindelinb6992262020-01-23 12:28:186228 if (todo_list_parse_insn_buffer(r, new_todo.buf.buf, &new_todo) < 0)
6229 BUG("invalid todo list after expanding IDs:\n%s",
6230 new_todo.buf.buf);
6231
Alban Gruin6bfeb7f2019-03-05 19:18:006232 if (opts->allow_ff && skip_unnecessary_picks(r, &new_todo, &oid)) {
6233 todo_list_release(&new_todo);
Alban Gruinb97e1872018-08-28 12:10:366234 return error(_("could not skip unnecessary pick commands"));
Alban Gruin6bfeb7f2019-03-05 19:18:006235 }
6236
Alban Gruin94bcad72019-03-05 19:17:576237 if (todo_list_write_to_file(r, &new_todo, todo_file, NULL, NULL, -1,
6238 flags & ~(TODO_LIST_SHORTEN_IDS))) {
6239 todo_list_release(&new_todo);
6240 return error_errno(_("could not write '%s'"), todo_file);
6241 }
6242
Alban Gruin393adf72019-11-24 17:43:326243 res = -1;
Alban Gruinb97e1872018-08-28 12:10:366244
Phillip Wood7d3488e2019-04-17 14:30:396245 if (checkout_onto(r, opts, onto_name, &oid, orig_head))
Alban Gruin393adf72019-11-24 17:43:326246 goto cleanup;
Nguyễn Thái Ngọc Duy29d51e22018-11-03 14:32:296247
Oswald Buddenhagena9b59552023-08-24 15:00:466248 if (require_clean_work_tree(r, "rebase", NULL, 1, 1))
Alban Gruin393adf72019-11-24 17:43:326249 goto cleanup;
Alban Gruinb97e1872018-08-28 12:10:366250
Alban Gruin393adf72019-11-24 17:43:326251 todo_list_write_total_nr(&new_todo);
6252 res = pick_commits(r, &new_todo, opts);
6253
6254cleanup:
6255 todo_list_release(&new_todo);
6256
6257 return res;
Alban Gruinb97e1872018-08-28 12:10:366258}
6259
Johannes Schindelinc44a4c62017-07-14 14:45:316260struct subject2item_entry {
6261 struct hashmap_entry entry;
6262 int i;
6263 char subject[FLEX_ARRAY];
6264};
6265
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 17:09:486266static int subject2item_cmp(const void *fndata UNUSED,
Eric Wong939af162019-10-06 23:30:376267 const struct hashmap_entry *eptr,
6268 const struct hashmap_entry *entry_or_key,
6269 const void *key)
Johannes Schindelinc44a4c62017-07-14 14:45:316270{
Eric Wong939af162019-10-06 23:30:376271 const struct subject2item_entry *a, *b;
6272
6273 a = container_of(eptr, const struct subject2item_entry, entry);
6274 b = container_of(entry_or_key, const struct subject2item_entry, entry);
6275
Johannes Schindelinc44a4c62017-07-14 14:45:316276 return key ? strcmp(a->subject, key) : strcmp(a->subject, b->subject);
6277}
6278
Nguyễn Thái Ngọc Duy3cc02872018-05-19 05:28:236279define_commit_slab(commit_todo_item, struct todo_item *);
6280
Charvi Mendiratta1f969602021-02-08 19:25:206281static int skip_fixupish(const char *subject, const char **p) {
Charvi Mendirattabae5b4a2021-01-29 18:20:496282 return skip_prefix(subject, "fixup! ", p) ||
6283 skip_prefix(subject, "amend! ", p) ||
6284 skip_prefix(subject, "squash! ", p);
6285}
6286
Johannes Schindelinc44a4c62017-07-14 14:45:316287/*
6288 * Rearrange the todo list that has both "pick commit-id msg" and "pick
6289 * commit-id fixup!/squash! msg" in it so that the latter is put immediately
6290 * after the former, and change "pick" to "fixup"/"squash".
6291 *
6292 * Note that if the config has specified a custom instruction format, each log
6293 * message will have to be retrieved from the commit (as the oneline in the
6294 * script cannot be trusted) in order to normalize the autosquash arrangement.
6295 */
Alban Gruin79d7e882019-03-05 19:17:596296int todo_list_rearrange_squash(struct todo_list *todo_list)
Johannes Schindelinc44a4c62017-07-14 14:45:316297{
Johannes Schindelinc44a4c62017-07-14 14:45:316298 struct hashmap subject2item;
Oswald Buddenhagen82dc42c2023-08-09 17:15:326299 int rearranged = 0, *next, *tail, i, nr = 0;
Johannes Schindelinc44a4c62017-07-14 14:45:316300 char **subjects;
Nguyễn Thái Ngọc Duy3cc02872018-05-19 05:28:236301 struct commit_todo_item commit_todo;
Alban Gruinf2a04902019-03-05 19:17:556302 struct todo_item *items = NULL;
Johannes Schindelinc44a4c62017-07-14 14:45:316303
Nguyễn Thái Ngọc Duy3cc02872018-05-19 05:28:236304 init_commit_todo_item(&commit_todo);
Johannes Schindelinc44a4c62017-07-14 14:45:316305 /*
6306 * The hashmap maps onelines to the respective todo list index.
6307 *
6308 * If any items need to be rearranged, the next[i] value will indicate
6309 * which item was moved directly after the i'th.
6310 *
6311 * In that case, last[i] will indicate the index of the latest item to
6312 * be moved to appear after the i'th.
6313 */
Eric Wong939af162019-10-06 23:30:376314 hashmap_init(&subject2item, subject2item_cmp, NULL, todo_list->nr);
Alban Gruinf2a04902019-03-05 19:17:556315 ALLOC_ARRAY(next, todo_list->nr);
6316 ALLOC_ARRAY(tail, todo_list->nr);
6317 ALLOC_ARRAY(subjects, todo_list->nr);
6318 for (i = 0; i < todo_list->nr; i++) {
Johannes Schindelinc44a4c62017-07-14 14:45:316319 struct strbuf buf = STRBUF_INIT;
Alban Gruinf2a04902019-03-05 19:17:556320 struct todo_item *item = todo_list->items + i;
Johannes Schindelinc44a4c62017-07-14 14:45:316321 const char *commit_buffer, *subject, *p;
6322 size_t subject_len;
6323 int i2 = -1;
6324 struct subject2item_entry *entry;
6325
6326 next[i] = tail[i] = -1;
Johannes Schindelin2f6b1d12018-04-25 12:28:256327 if (!item->commit || item->command == TODO_DROP) {
Johannes Schindelinc44a4c62017-07-14 14:45:316328 subjects[i] = NULL;
6329 continue;
6330 }
6331
6332 if (is_fixup(item->command)) {
Nguyễn Thái Ngọc Duy3cc02872018-05-19 05:28:236333 clear_commit_todo_item(&commit_todo);
Johannes Schindelinc44a4c62017-07-14 14:45:316334 return error(_("the script was already rearranged."));
6335 }
6336
Ævar Arnfjörð Bjarmasonecb50912023-03-28 13:58:486337 repo_parse_commit(the_repository, item->commit);
6338 commit_buffer = repo_logmsg_reencode(the_repository,
6339 item->commit, NULL,
6340 "UTF-8");
Johannes Schindelinc44a4c62017-07-14 14:45:316341 find_commit_subject(commit_buffer, &subject);
6342 format_subject(&buf, subject, " ");
6343 subject = subjects[i] = strbuf_detach(&buf, &subject_len);
Ævar Arnfjörð Bjarmasonecb50912023-03-28 13:58:486344 repo_unuse_commit_buffer(the_repository, item->commit,
6345 commit_buffer);
Charvi Mendiratta1f969602021-02-08 19:25:206346 if (skip_fixupish(subject, &p)) {
Johannes Schindelinc44a4c62017-07-14 14:45:316347 struct commit *commit2;
6348
6349 for (;;) {
6350 while (isspace(*p))
6351 p++;
Charvi Mendiratta1f969602021-02-08 19:25:206352 if (!skip_fixupish(p, &p))
Johannes Schindelinc44a4c62017-07-14 14:45:316353 break;
6354 }
6355
Eric Wongf23a4652019-10-06 23:30:366356 entry = hashmap_get_entry_from_hash(&subject2item,
6357 strhash(p), p,
6358 struct subject2item_entry,
6359 entry);
6360 if (entry)
Johannes Schindelinc44a4c62017-07-14 14:45:316361 /* found by title */
6362 i2 = entry->i;
6363 else if (!strchr(p, ' ') &&
6364 (commit2 =
6365 lookup_commit_reference_by_name(p)) &&
Nguyễn Thái Ngọc Duy3cc02872018-05-19 05:28:236366 *commit_todo_item_at(&commit_todo, commit2))
Johannes Schindelinc44a4c62017-07-14 14:45:316367 /* found by commit name */
Nguyễn Thái Ngọc Duy3cc02872018-05-19 05:28:236368 i2 = *commit_todo_item_at(&commit_todo, commit2)
Alban Gruinf2a04902019-03-05 19:17:556369 - todo_list->items;
Johannes Schindelinc44a4c62017-07-14 14:45:316370 else {
6371 /* copy can be a prefix of the commit subject */
6372 for (i2 = 0; i2 < i; i2++)
6373 if (subjects[i2] &&
6374 starts_with(subjects[i2], p))
6375 break;
6376 if (i2 == i)
6377 i2 = -1;
6378 }
6379 }
6380 if (i2 >= 0) {
6381 rearranged = 1;
Charvi Mendirattabae5b4a2021-01-29 18:20:496382 if (starts_with(subject, "fixup!")) {
6383 todo_list->items[i].command = TODO_FIXUP;
6384 } else if (starts_with(subject, "amend!")) {
6385 todo_list->items[i].command = TODO_FIXUP;
6386 todo_list->items[i].flags = TODO_REPLACE_FIXUP_MSG;
6387 } else {
6388 todo_list->items[i].command = TODO_SQUASH;
6389 }
Johannes Schindelin02471e72020-05-09 19:23:396390 if (tail[i2] < 0) {
6391 next[i] = next[i2];
Johannes Schindelinc44a4c62017-07-14 14:45:316392 next[i2] = i;
Johannes Schindelin02471e72020-05-09 19:23:396393 } else {
6394 next[i] = next[tail[i2]];
Johannes Schindelinc44a4c62017-07-14 14:45:316395 next[tail[i2]] = i;
Johannes Schindelin02471e72020-05-09 19:23:396396 }
Johannes Schindelinc44a4c62017-07-14 14:45:316397 tail[i2] = i;
6398 } else if (!hashmap_get_from_hash(&subject2item,
6399 strhash(subject), subject)) {
6400 FLEX_ALLOC_MEM(entry, subject, subject, subject_len);
6401 entry->i = i;
Eric Wongd22245a2019-10-06 23:30:276402 hashmap_entry_init(&entry->entry,
6403 strhash(entry->subject));
Eric Wong26b455f2019-10-06 23:30:326404 hashmap_put(&subject2item, &entry->entry);
Johannes Schindelinc44a4c62017-07-14 14:45:316405 }
Johannes Altmanninger3e367a52022-09-24 22:29:046406
6407 *commit_todo_item_at(&commit_todo, item->commit) = item;
Johannes Schindelinc44a4c62017-07-14 14:45:316408 }
6409
6410 if (rearranged) {
Oswald Buddenhagen82dc42c2023-08-09 17:15:326411 ALLOC_ARRAY(items, todo_list->nr);
6412
Alban Gruinf2a04902019-03-05 19:17:556413 for (i = 0; i < todo_list->nr; i++) {
6414 enum todo_command command = todo_list->items[i].command;
Johannes Schindelinc44a4c62017-07-14 14:45:316415 int cur = i;
6416
6417 /*
6418 * Initially, all commands are 'pick's. If it is a
6419 * fixup or a squash now, we have rearranged it.
6420 */
6421 if (is_fixup(command))
6422 continue;
6423
6424 while (cur >= 0) {
Alban Gruinf2a04902019-03-05 19:17:556425 items[nr++] = todo_list->items[cur];
Johannes Schindelinc44a4c62017-07-14 14:45:316426 cur = next[cur];
6427 }
6428 }
6429
Oswald Buddenhagen82dc42c2023-08-09 17:15:326430 assert(nr == todo_list->nr);
6431 todo_list->alloc = nr;
Alban Gruinf2a04902019-03-05 19:17:556432 FREE_AND_NULL(todo_list->items);
6433 todo_list->items = items;
Johannes Schindelinc44a4c62017-07-14 14:45:316434 }
6435
6436 free(next);
6437 free(tail);
Alban Gruinf2a04902019-03-05 19:17:556438 for (i = 0; i < todo_list->nr; i++)
Johannes Schindelinc44a4c62017-07-14 14:45:316439 free(subjects[i]);
6440 free(subjects);
Elijah Newren6da1a252020-11-02 18:55:056441 hashmap_clear_and_free(&subject2item, struct subject2item_entry, entry);
Johannes Schindelinc44a4c62017-07-14 14:45:316442
Nguyễn Thái Ngọc Duy3cc02872018-05-19 05:28:236443 clear_commit_todo_item(&commit_todo);
Alban Gruinf2a04902019-03-05 19:17:556444
6445 return 0;
Johannes Schindelinc44a4c62017-07-14 14:45:316446}
Phillip Wood901ba7b2019-12-06 16:06:116447
6448int sequencer_determine_whence(struct repository *r, enum commit_whence *whence)
6449{
Han-Wen Nienhuysc8e41592020-08-21 16:59:356450 if (refs_ref_exists(get_main_ref_store(r), "CHERRY_PICK_HEAD")) {
Phillip Wood430b75f2019-12-06 16:06:126451 struct object_id cherry_pick_head, rebase_head;
6452
6453 if (file_exists(git_path_seq_dir()))
6454 *whence = FROM_CHERRY_PICK_MULTI;
6455 if (file_exists(rebase_path()) &&
Ævar Arnfjörð Bjarmason4a93b892023-03-28 13:58:586456 !repo_get_oid(r, "REBASE_HEAD", &rebase_head) &&
6457 !repo_get_oid(r, "CHERRY_PICK_HEAD", &cherry_pick_head) &&
Phillip Wood430b75f2019-12-06 16:06:126458 oideq(&rebase_head, &cherry_pick_head))
6459 *whence = FROM_REBASE_PICK;
6460 else
6461 *whence = FROM_CHERRY_PICK_SINGLE;
6462
Phillip Wood901ba7b2019-12-06 16:06:116463 return 1;
6464 }
6465
6466 return 0;
6467}
Derrick Stoleeaa7f2fd2022-07-19 18:33:356468
6469int sequencer_get_update_refs_state(const char *wt_dir,
6470 struct string_list *refs)
6471{
6472 int result = 0;
6473 FILE *fp = NULL;
6474 struct strbuf ref = STRBUF_INIT;
6475 struct strbuf hash = STRBUF_INIT;
6476 struct update_ref_record *rec = NULL;
6477
6478 char *path = rebase_path_update_refs(wt_dir);
6479
6480 fp = fopen(path, "r");
6481 if (!fp)
6482 goto cleanup;
6483
6484 while (strbuf_getline(&ref, fp) != EOF) {
6485 struct string_list_item *item;
6486
6487 CALLOC_ARRAY(rec, 1);
6488
6489 if (strbuf_getline(&hash, fp) == EOF ||
6490 get_oid_hex(hash.buf, &rec->before)) {
6491 warning(_("update-refs file at '%s' is invalid"),
6492 path);
6493 result = -1;
6494 goto cleanup;
6495 }
6496
6497 if (strbuf_getline(&hash, fp) == EOF ||
6498 get_oid_hex(hash.buf, &rec->after)) {
6499 warning(_("update-refs file at '%s' is invalid"),
6500 path);
6501 result = -1;
6502 goto cleanup;
6503 }
6504
6505 item = string_list_insert(refs, ref.buf);
6506 item->util = rec;
6507 rec = NULL;
6508 }
6509
6510cleanup:
6511 if (fp)
6512 fclose(fp);
6513 free(path);
6514 free(rec);
6515 strbuf_release(&ref);
6516 strbuf_release(&hash);
6517 return result;
6518}