]> git.ipfire.org Git - thirdparty/git.git/blame - rebase-interactive.c
object-name.h: move declarations for object-name.c functions from cache.h
[thirdparty/git.git] / rebase-interactive.c
CommitLineData
145e05ac
AG
1#include "cache.h"
2#include "commit.h"
32a8f510 3#include "environment.h"
f394e093 4#include "gettext.h"
145e05ac 5#include "sequencer.h"
6ca89c6f 6#include "rebase-interactive.h"
145e05ac 7#include "strbuf.h"
6ca89c6f
AG
8#include "commit-slab.h"
9#include "config.h"
5a5445d8 10#include "dir.h"
dabab1d6 11#include "object-name.h"
d5ebb50d 12#include "wrapper.h"
6ca89c6f 13
1da5874c
AG
14static const char edit_todo_list_advice[] =
15N_("You can fix this with 'git rebase --edit-todo' "
16"and then run 'git rebase --continue'.\n"
17"Or you can abort the rebase with 'git rebase"
18" --abort'.\n");
19
6ca89c6f
AG
20enum missing_commit_check_level {
21 MISSING_COMMIT_CHECK_IGNORE = 0,
22 MISSING_COMMIT_CHECK_WARN,
23 MISSING_COMMIT_CHECK_ERROR
24};
25
26static enum missing_commit_check_level get_missing_commit_check_level(void)
27{
28 const char *value;
29
30 if (git_config_get_value("rebase.missingcommitscheck", &value) ||
31 !strcasecmp("ignore", value))
32 return MISSING_COMMIT_CHECK_IGNORE;
33 if (!strcasecmp("warn", value))
34 return MISSING_COMMIT_CHECK_WARN;
35 if (!strcasecmp("error", value))
36 return MISSING_COMMIT_CHECK_ERROR;
37 warning(_("unrecognized setting %s for option "
38 "rebase.missingCommitsCheck. Ignoring."), value);
39 return MISSING_COMMIT_CHECK_IGNORE;
40}
145e05ac 41
d48e5e21 42void append_todo_help(int command_count,
af1fc3ad 43 const char *shortrevisions, const char *shortonto,
a9f5476f 44 struct strbuf *buf)
145e05ac 45{
145e05ac
AG
46 const char *msg = _("\nCommands:\n"
47"p, pick <commit> = use commit\n"
48"r, reword <commit> = use commit, but edit the commit message\n"
49"e, edit <commit> = use commit, but stop for amending\n"
50"s, squash <commit> = use commit, but meld into previous commit\n"
f07871d3
CM
51"f, fixup [-C | -c] <commit> = like \"squash\" but keep only the previous\n"
52" commit's log message, unless -C is used, in which case\n"
53" keep only this commit's message; -c is same as -C but\n"
54" opens the editor\n"
145e05ac 55"x, exec <command> = run command (the rest of the line) using shell\n"
71f82465 56"b, break = stop here (continue rebase later with 'git rebase --continue')\n"
145e05ac
AG
57"d, drop <commit> = remove commit\n"
58"l, label <label> = label current HEAD with a name\n"
59"t, reset <label> = reset HEAD to a label\n"
60"m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]\n"
f57fd48d
DS
61" create a merge commit using the original merge commit's\n"
62" message (or the oneline, if no original merge commit was\n"
63" specified); use -c <commit> to reword the commit message\n"
a97d7916
DS
64"u, update-ref <ref> = track a placeholder for the <ref> to be updated\n"
65" to this position in the new commits. The <ref> is\n"
66" updated at the end of the rebase\n"
145e05ac
AG
67"\n"
68"These lines can be re-ordered; they are executed from top to bottom.\n");
af1fc3ad
AG
69 unsigned edit_todo = !(shortrevisions && shortonto);
70
71 if (!edit_todo) {
72 strbuf_addch(buf, '\n');
73 strbuf_commented_addf(buf, Q_("Rebase %s onto %s (%d command)",
74 "Rebase %s onto %s (%d commands)",
75 command_count),
76 shortrevisions, shortonto, command_count);
77 }
145e05ac 78
a9f5476f 79 strbuf_add_commented_lines(buf, msg, strlen(msg));
145e05ac
AG
80
81 if (get_missing_commit_check_level() == MISSING_COMMIT_CHECK_ERROR)
82 msg = _("\nDo not remove any line. Use 'drop' "
83 "explicitly to remove a commit.\n");
84 else
85 msg = _("\nIf you remove a line here "
86 "THAT COMMIT WILL BE LOST.\n");
87
a9f5476f 88 strbuf_add_commented_lines(buf, msg, strlen(msg));
145e05ac
AG
89
90 if (edit_todo)
91 msg = _("\nYou are editing the todo file "
92 "of an ongoing interactive rebase.\n"
93 "To continue rebase after editing, run:\n"
94 " git rebase --continue\n\n");
95 else
96 msg = _("\nHowever, if you remove everything, "
97 "the rebase will be aborted.\n\n");
98
a9f5476f 99 strbuf_add_commented_lines(buf, msg, strlen(msg));
a9f5476f
AG
100}
101
a930eb03
AG
102int edit_todo_list(struct repository *r, struct todo_list *todo_list,
103 struct todo_list *new_todo, const char *shortrevisions,
104 const char *shortonto, unsigned flags)
64a43cbd 105{
5a5445d8
AG
106 const char *todo_file = rebase_path_todo(),
107 *todo_backup = rebase_path_todo_backup();
a930eb03 108 unsigned initial = shortrevisions && shortonto;
5a5445d8 109 int incorrect = 0;
64a43cbd 110
a930eb03
AG
111 /* If the user is editing the todo list, we first try to parse
112 * it. If there is an error, we do not return, because the user
113 * might want to fix it in the first place. */
114 if (!initial)
5a5445d8
AG
115 incorrect = todo_list_parse_insn_buffer(r, todo_list->buf.buf, todo_list) |
116 file_exists(rebase_path_dropped());
a930eb03
AG
117
118 if (todo_list_write_to_file(r, todo_list, todo_file, shortrevisions, shortonto,
119 -1, flags | TODO_LIST_SHORTEN_IDS | TODO_LIST_APPEND_TODO_HELP))
120 return error_errno(_("could not write '%s'"), todo_file);
121
5a5445d8
AG
122 if (!incorrect &&
123 todo_list_write_to_file(r, todo_list, todo_backup,
26027625
JS
124 shortrevisions, shortonto, -1,
125 (flags | TODO_LIST_APPEND_TODO_HELP) & ~TODO_LIST_SHORTEN_IDS) < 0)
126 return error(_("could not write '%s'."), rebase_path_todo_backup());
a930eb03
AG
127
128 if (launch_sequence_editor(todo_file, &new_todo->buf, NULL))
129 return -2;
130
131 strbuf_stripspace(&new_todo->buf, 1);
132 if (initial && new_todo->buf.len == 0)
133 return -3;
a9f5476f 134
5a5445d8
AG
135 if (todo_list_parse_insn_buffer(r, new_todo->buf.buf, new_todo)) {
136 fprintf(stderr, _(edit_todo_list_advice));
137 return -4;
138 }
139
140 if (incorrect) {
141 if (todo_list_check_against_backup(r, new_todo)) {
7daf4f2a 142 write_file(rebase_path_dropped(), "%s", "");
5a5445d8
AG
143 return -4;
144 }
145
146 if (incorrect > 0)
147 unlink(rebase_path_dropped());
148 } else if (todo_list_check(todo_list, new_todo)) {
7daf4f2a 149 write_file(rebase_path_dropped(), "%s", "");
5a5445d8
AG
150 return -4;
151 }
64a43cbd 152
b3b1a21d
DS
153 /*
154 * See if branches need to be added or removed from the update-refs
155 * file based on the new todo list.
156 */
157 todo_list_filter_update_refs(r, new_todo);
158
a930eb03 159 return 0;
64a43cbd 160}
6ca89c6f
AG
161
162define_commit_slab(commit_seen, unsigned char);
163/*
164 * Check if the user dropped some commits by mistake
165 * Behaviour determined by rebase.missingCommitsCheck.
166 * Check if there is an unrecognized command or a
167 * bad SHA-1 in a command.
168 */
169int todo_list_check(struct todo_list *old_todo, struct todo_list *new_todo)
170{
171 enum missing_commit_check_level check_level = get_missing_commit_check_level();
172 struct strbuf missing = STRBUF_INIT;
173 int res = 0, i;
174 struct commit_seen commit_seen;
175
176 init_commit_seen(&commit_seen);
177
178 if (check_level == MISSING_COMMIT_CHECK_IGNORE)
179 goto leave_check;
180
181 /* Mark the commits in git-rebase-todo as seen */
182 for (i = 0; i < new_todo->nr; i++) {
183 struct commit *commit = new_todo->items[i].commit;
184 if (commit)
185 *commit_seen_at(&commit_seen, commit) = 1;
186 }
187
188 /* Find commits in git-rebase-todo.backup yet unseen */
189 for (i = old_todo->nr - 1; i >= 0; i--) {
190 struct todo_item *item = old_todo->items + i;
191 struct commit *commit = item->commit;
192 if (commit && !*commit_seen_at(&commit_seen, commit)) {
193 strbuf_addf(&missing, " - %s %.*s\n",
d850b7a5 194 repo_find_unique_abbrev(the_repository, &commit->object.oid, DEFAULT_ABBREV),
6ca89c6f
AG
195 item->arg_len,
196 todo_item_get_arg(old_todo, item));
197 *commit_seen_at(&commit_seen, commit) = 1;
198 }
199 }
200
201 /* Warn about missing commits */
202 if (!missing.len)
203 goto leave_check;
204
205 if (check_level == MISSING_COMMIT_CHECK_ERROR)
206 res = 1;
207
208 fprintf(stderr,
209 _("Warning: some commits may have been dropped accidentally.\n"
210 "Dropped commits (newer to older):\n"));
211
212 /* Make the list user-friendly and display */
213 fputs(missing.buf, stderr);
214 strbuf_release(&missing);
215
216 fprintf(stderr, _("To avoid this message, use \"drop\" to "
217 "explicitly remove a commit.\n\n"
218 "Use 'git config rebase.missingCommitsCheck' to change "
219 "the level of warnings.\n"
220 "The possible behaviours are: ignore, warn, error.\n\n"));
221
5a5445d8
AG
222 fprintf(stderr, _(edit_todo_list_advice));
223
6ca89c6f
AG
224leave_check:
225 clear_commit_seen(&commit_seen);
226 return res;
227}
1da5874c 228
5a5445d8
AG
229int todo_list_check_against_backup(struct repository *r, struct todo_list *todo_list)
230{
231 struct todo_list backup = TODO_LIST_INIT;
232 int res = 0;
233
234 if (strbuf_read_file(&backup.buf, rebase_path_todo_backup(), 0) > 0) {
235 todo_list_parse_insn_buffer(r, backup.buf.buf, &backup);
236 res = todo_list_check(&backup, todo_list);
237 }
238
239 todo_list_release(&backup);
240 return res;
241}