4 #include "rebase-interactive.h"
6 #include "commit-slab.h"
9 enum missing_commit_check_level {
10 MISSING_COMMIT_CHECK_IGNORE = 0,
11 MISSING_COMMIT_CHECK_WARN,
12 MISSING_COMMIT_CHECK_ERROR
15 static enum missing_commit_check_level get_missing_commit_check_level(void)
19 if (git_config_get_value("rebase.missingcommitscheck", &value) ||
20 !strcasecmp("ignore", value))
21 return MISSING_COMMIT_CHECK_IGNORE;
22 if (!strcasecmp("warn", value))
23 return MISSING_COMMIT_CHECK_WARN;
24 if (!strcasecmp("error", value))
25 return MISSING_COMMIT_CHECK_ERROR;
26 warning(_("unrecognized setting %s for option "
27 "rebase.missingCommitsCheck. Ignoring."), value);
28 return MISSING_COMMIT_CHECK_IGNORE;
31 void append_todo_help(unsigned keep_empty, int command_count,
32 const char *shortrevisions, const char *shortonto,
35 const char *msg = _("\nCommands:\n"
36 "p, pick <commit> = use commit\n"
37 "r, reword <commit> = use commit, but edit the commit message\n"
38 "e, edit <commit> = use commit, but stop for amending\n"
39 "s, squash <commit> = use commit, but meld into previous commit\n"
40 "f, fixup <commit> = like \"squash\", but discard this commit's log message\n"
41 "x, exec <command> = run command (the rest of the line) using shell\n"
42 "b, break = stop here (continue rebase later with 'git rebase --continue')\n"
43 "d, drop <commit> = remove commit\n"
44 "l, label <label> = label current HEAD with a name\n"
45 "t, reset <label> = reset HEAD to a label\n"
46 "m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]\n"
47 ". create a merge commit using the original merge commit's\n"
48 ". message (or the oneline, if no original merge commit was\n"
49 ". specified). Use -c <commit> to reword the commit message.\n"
51 "These lines can be re-ordered; they are executed from top to bottom.\n");
52 unsigned edit_todo = !(shortrevisions && shortonto);
55 strbuf_addch(buf, '\n');
56 strbuf_commented_addf(buf, Q_("Rebase %s onto %s (%d command)",
57 "Rebase %s onto %s (%d commands)",
59 shortrevisions, shortonto, command_count);
62 strbuf_add_commented_lines(buf, msg, strlen(msg));
64 if (get_missing_commit_check_level() == MISSING_COMMIT_CHECK_ERROR)
65 msg = _("\nDo not remove any line. Use 'drop' "
66 "explicitly to remove a commit.\n");
68 msg = _("\nIf you remove a line here "
69 "THAT COMMIT WILL BE LOST.\n");
71 strbuf_add_commented_lines(buf, msg, strlen(msg));
74 msg = _("\nYou are editing the todo file "
75 "of an ongoing interactive rebase.\n"
76 "To continue rebase after editing, run:\n"
77 " git rebase --continue\n\n");
79 msg = _("\nHowever, if you remove everything, "
80 "the rebase will be aborted.\n\n");
82 strbuf_add_commented_lines(buf, msg, strlen(msg));
85 msg = _("Note that empty commits are commented out");
86 strbuf_add_commented_lines(buf, msg, strlen(msg));
90 int edit_todo_list(struct repository *r, unsigned flags)
92 const char *todo_file = rebase_path_todo();
93 struct todo_list todo_list = TODO_LIST_INIT;
96 if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
97 return error_errno(_("could not read '%s'."), todo_file);
99 strbuf_stripspace(&todo_list.buf, 1);
100 todo_list_parse_insn_buffer(r, todo_list.buf.buf, &todo_list);
101 if (todo_list_write_to_file(r, &todo_list, todo_file, NULL, NULL, -1,
102 flags | TODO_LIST_SHORTEN_IDS | TODO_LIST_APPEND_TODO_HELP)) {
103 todo_list_release(&todo_list);
107 strbuf_reset(&todo_list.buf);
108 if (launch_sequence_editor(todo_file, &todo_list.buf, NULL)) {
109 todo_list_release(&todo_list);
113 if (!todo_list_parse_insn_buffer(r, todo_list.buf.buf, &todo_list))
114 res = todo_list_write_to_file(r, &todo_list, todo_file, NULL, NULL, -1,
115 flags & ~(TODO_LIST_SHORTEN_IDS));
117 todo_list_release(&todo_list);
121 define_commit_slab(commit_seen, unsigned char);
123 * Check if the user dropped some commits by mistake
124 * Behaviour determined by rebase.missingCommitsCheck.
125 * Check if there is an unrecognized command or a
126 * bad SHA-1 in a command.
128 int todo_list_check(struct todo_list *old_todo, struct todo_list *new_todo)
130 enum missing_commit_check_level check_level = get_missing_commit_check_level();
131 struct strbuf missing = STRBUF_INIT;
133 struct commit_seen commit_seen;
135 init_commit_seen(&commit_seen);
137 if (check_level == MISSING_COMMIT_CHECK_IGNORE)
140 /* Mark the commits in git-rebase-todo as seen */
141 for (i = 0; i < new_todo->nr; i++) {
142 struct commit *commit = new_todo->items[i].commit;
144 *commit_seen_at(&commit_seen, commit) = 1;
147 /* Find commits in git-rebase-todo.backup yet unseen */
148 for (i = old_todo->nr - 1; i >= 0; i--) {
149 struct todo_item *item = old_todo->items + i;
150 struct commit *commit = item->commit;
151 if (commit && !*commit_seen_at(&commit_seen, commit)) {
152 strbuf_addf(&missing, " - %s %.*s\n",
153 find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV),
155 todo_item_get_arg(old_todo, item));
156 *commit_seen_at(&commit_seen, commit) = 1;
160 /* Warn about missing commits */
164 if (check_level == MISSING_COMMIT_CHECK_ERROR)
168 _("Warning: some commits may have been dropped accidentally.\n"
169 "Dropped commits (newer to older):\n"));
171 /* Make the list user-friendly and display */
172 fputs(missing.buf, stderr);
173 strbuf_release(&missing);
175 fprintf(stderr, _("To avoid this message, use \"drop\" to "
176 "explicitly remove a commit.\n\n"
177 "Use 'git config rebase.missingCommitsCheck' to change "
178 "the level of warnings.\n"
179 "The possible behaviours are: ignore, warn, error.\n\n"));
182 clear_commit_seen(&commit_seen);