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