d396ecc599b2b5760a7ea9b0bfd38083024233a2
1#include "cache.h"
2#include "commit.h"
3#include "sequencer.h"
4#include "rebase-interactive.h"
5#include "strbuf.h"
6#include "commit-slab.h"
7#include "config.h"
8
9enum missing_commit_check_level {
10 MISSING_COMMIT_CHECK_IGNORE = 0,
11 MISSING_COMMIT_CHECK_WARN,
12 MISSING_COMMIT_CHECK_ERROR
13};
14
15static enum missing_commit_check_level get_missing_commit_check_level(void)
16{
17 const char *value;
18
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;
29}
30
31void append_todo_help(unsigned edit_todo, unsigned keep_empty,
32 struct strbuf *buf)
33{
34 const char *msg = _("\nCommands:\n"
35"p, pick <commit> = use commit\n"
36"r, reword <commit> = use commit, but edit the commit message\n"
37"e, edit <commit> = use commit, but stop for amending\n"
38"s, squash <commit> = use commit, but meld into previous commit\n"
39"f, fixup <commit> = like \"squash\", but discard this commit's log message\n"
40"x, exec <command> = run command (the rest of the line) using shell\n"
41"b, break = stop here (continue rebase later with 'git rebase --continue')\n"
42"d, drop <commit> = remove commit\n"
43"l, label <label> = label current HEAD with a name\n"
44"t, reset <label> = reset HEAD to a label\n"
45"m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]\n"
46". create a merge commit using the original merge commit's\n"
47". message (or the oneline, if no original merge commit was\n"
48". specified). Use -c <commit> to reword the commit message.\n"
49"\n"
50"These lines can be re-ordered; they are executed from top to bottom.\n");
51
52 strbuf_add_commented_lines(buf, msg, strlen(msg));
53
54 if (get_missing_commit_check_level() == MISSING_COMMIT_CHECK_ERROR)
55 msg = _("\nDo not remove any line. Use 'drop' "
56 "explicitly to remove a commit.\n");
57 else
58 msg = _("\nIf you remove a line here "
59 "THAT COMMIT WILL BE LOST.\n");
60
61 strbuf_add_commented_lines(buf, msg, strlen(msg));
62
63 if (edit_todo)
64 msg = _("\nYou are editing the todo file "
65 "of an ongoing interactive rebase.\n"
66 "To continue rebase after editing, run:\n"
67 " git rebase --continue\n\n");
68 else
69 msg = _("\nHowever, if you remove everything, "
70 "the rebase will be aborted.\n\n");
71
72 strbuf_add_commented_lines(buf, msg, strlen(msg));
73
74 if (!keep_empty) {
75 msg = _("Note that empty commits are commented out");
76 strbuf_add_commented_lines(buf, msg, strlen(msg));
77 }
78}
79
80int edit_todo_list(struct repository *r, unsigned flags)
81{
82 const char *todo_file = rebase_path_todo();
83 struct todo_list todo_list = TODO_LIST_INIT;
84 int res = 0;
85
86 if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
87 return error_errno(_("could not read '%s'."), todo_file);
88
89 strbuf_stripspace(&todo_list.buf, 1);
90 todo_list_parse_insn_buffer(r, todo_list.buf.buf, &todo_list);
91 if (todo_list_write_to_file(r, &todo_list, todo_file, NULL, NULL, -1,
92 flags | TODO_LIST_SHORTEN_IDS | TODO_LIST_APPEND_TODO_HELP)) {
93 todo_list_release(&todo_list);
94 return -1;
95 }
96
97 strbuf_reset(&todo_list.buf);
98 if (launch_sequence_editor(todo_file, &todo_list.buf, NULL)) {
99 todo_list_release(&todo_list);
100 return -1;
101 }
102
103 if (!todo_list_parse_insn_buffer(r, todo_list.buf.buf, &todo_list))
104 res = todo_list_write_to_file(r, &todo_list, todo_file, NULL, NULL, -1,
105 flags & ~(TODO_LIST_SHORTEN_IDS));
106
107 todo_list_release(&todo_list);
108 return res;
109}
110
111define_commit_slab(commit_seen, unsigned char);
112/*
113 * Check if the user dropped some commits by mistake
114 * Behaviour determined by rebase.missingCommitsCheck.
115 * Check if there is an unrecognized command or a
116 * bad SHA-1 in a command.
117 */
118int todo_list_check(struct todo_list *old_todo, struct todo_list *new_todo)
119{
120 enum missing_commit_check_level check_level = get_missing_commit_check_level();
121 struct strbuf missing = STRBUF_INIT;
122 int res = 0, i;
123 struct commit_seen commit_seen;
124
125 init_commit_seen(&commit_seen);
126
127 if (check_level == MISSING_COMMIT_CHECK_IGNORE)
128 goto leave_check;
129
130 /* Mark the commits in git-rebase-todo as seen */
131 for (i = 0; i < new_todo->nr; i++) {
132 struct commit *commit = new_todo->items[i].commit;
133 if (commit)
134 *commit_seen_at(&commit_seen, commit) = 1;
135 }
136
137 /* Find commits in git-rebase-todo.backup yet unseen */
138 for (i = old_todo->nr - 1; i >= 0; i--) {
139 struct todo_item *item = old_todo->items + i;
140 struct commit *commit = item->commit;
141 if (commit && !*commit_seen_at(&commit_seen, commit)) {
142 strbuf_addf(&missing, " - %s %.*s\n",
143 find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV),
144 item->arg_len,
145 todo_item_get_arg(old_todo, item));
146 *commit_seen_at(&commit_seen, commit) = 1;
147 }
148 }
149
150 /* Warn about missing commits */
151 if (!missing.len)
152 goto leave_check;
153
154 if (check_level == MISSING_COMMIT_CHECK_ERROR)
155 res = 1;
156
157 fprintf(stderr,
158 _("Warning: some commits may have been dropped accidentally.\n"
159 "Dropped commits (newer to older):\n"));
160
161 /* Make the list user-friendly and display */
162 fputs(missing.buf, stderr);
163 strbuf_release(&missing);
164
165 fprintf(stderr, _("To avoid this message, use \"drop\" to "
166 "explicitly remove a commit.\n\n"
167 "Use 'git config rebase.missingCommitsCheck' to change "
168 "the level of warnings.\n"
169 "The possible behaviours are: ignore, warn, error.\n\n"));
170
171leave_check:
172 clear_commit_seen(&commit_seen);
173 return res;
174}