Merge branch 'jk/partial-clone-sparse-blob'
[git/git.git] / rebase-interactive.c
CommitLineData
145e05ac
AG
1#include "cache.h"
2#include "commit.h"
145e05ac 3#include "sequencer.h"
6ca89c6f 4#include "rebase-interactive.h"
145e05ac 5#include "strbuf.h"
6ca89c6f
AG
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}
145e05ac 30
af1fc3ad
AG
31void append_todo_help(unsigned keep_empty, int command_count,
32 const char *shortrevisions, const char *shortonto,
a9f5476f 33 struct strbuf *buf)
145e05ac 34{
145e05ac
AG
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"
71f82465 42"b, break = stop here (continue rebase later with 'git rebase --continue')\n"
145e05ac
AG
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"
50"\n"
51"These lines can be re-ordered; they are executed from top to bottom.\n");
af1fc3ad
AG
52 unsigned edit_todo = !(shortrevisions && shortonto);
53
54 if (!edit_todo) {
55 strbuf_addch(buf, '\n');
56 strbuf_commented_addf(buf, Q_("Rebase %s onto %s (%d command)",
57 "Rebase %s onto %s (%d commands)",
58 command_count),
59 shortrevisions, shortonto, command_count);
60 }
145e05ac 61
a9f5476f 62 strbuf_add_commented_lines(buf, msg, strlen(msg));
145e05ac
AG
63
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");
67 else
68 msg = _("\nIf you remove a line here "
69 "THAT COMMIT WILL BE LOST.\n");
70
a9f5476f 71 strbuf_add_commented_lines(buf, msg, strlen(msg));
145e05ac
AG
72
73 if (edit_todo)
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");
78 else
79 msg = _("\nHowever, if you remove everything, "
80 "the rebase will be aborted.\n\n");
81
a9f5476f 82 strbuf_add_commented_lines(buf, msg, strlen(msg));
145e05ac
AG
83
84 if (!keep_empty) {
85 msg = _("Note that empty commits are commented out");
a9f5476f 86 strbuf_add_commented_lines(buf, msg, strlen(msg));
145e05ac 87 }
a9f5476f
AG
88}
89
a930eb03
AG
90int edit_todo_list(struct repository *r, struct todo_list *todo_list,
91 struct todo_list *new_todo, const char *shortrevisions,
92 const char *shortonto, unsigned flags)
64a43cbd 93{
64a43cbd 94 const char *todo_file = rebase_path_todo();
a930eb03 95 unsigned initial = shortrevisions && shortonto;
64a43cbd 96
a930eb03
AG
97 /* If the user is editing the todo list, we first try to parse
98 * it. If there is an error, we do not return, because the user
99 * might want to fix it in the first place. */
100 if (!initial)
101 todo_list_parse_insn_buffer(r, todo_list->buf.buf, todo_list);
102
103 if (todo_list_write_to_file(r, todo_list, todo_file, shortrevisions, shortonto,
104 -1, flags | TODO_LIST_SHORTEN_IDS | TODO_LIST_APPEND_TODO_HELP))
105 return error_errno(_("could not write '%s'"), todo_file);
106
107 if (initial && copy_file(rebase_path_todo_backup(), todo_file, 0666))
108 return error(_("could not copy '%s' to '%s'."), todo_file,
109 rebase_path_todo_backup());
110
111 if (launch_sequence_editor(todo_file, &new_todo->buf, NULL))
112 return -2;
113
114 strbuf_stripspace(&new_todo->buf, 1);
115 if (initial && new_todo->buf.len == 0)
116 return -3;
a9f5476f 117
a930eb03
AG
118 /* For the initial edit, the todo list gets parsed in
119 * complete_action(). */
120 if (!initial)
121 return todo_list_parse_insn_buffer(r, new_todo->buf.buf, new_todo);
64a43cbd 122
a930eb03 123 return 0;
64a43cbd 124}
6ca89c6f
AG
125
126define_commit_slab(commit_seen, unsigned char);
127/*
128 * Check if the user dropped some commits by mistake
129 * Behaviour determined by rebase.missingCommitsCheck.
130 * Check if there is an unrecognized command or a
131 * bad SHA-1 in a command.
132 */
133int todo_list_check(struct todo_list *old_todo, struct todo_list *new_todo)
134{
135 enum missing_commit_check_level check_level = get_missing_commit_check_level();
136 struct strbuf missing = STRBUF_INIT;
137 int res = 0, i;
138 struct commit_seen commit_seen;
139
140 init_commit_seen(&commit_seen);
141
142 if (check_level == MISSING_COMMIT_CHECK_IGNORE)
143 goto leave_check;
144
145 /* Mark the commits in git-rebase-todo as seen */
146 for (i = 0; i < new_todo->nr; i++) {
147 struct commit *commit = new_todo->items[i].commit;
148 if (commit)
149 *commit_seen_at(&commit_seen, commit) = 1;
150 }
151
152 /* Find commits in git-rebase-todo.backup yet unseen */
153 for (i = old_todo->nr - 1; i >= 0; i--) {
154 struct todo_item *item = old_todo->items + i;
155 struct commit *commit = item->commit;
156 if (commit && !*commit_seen_at(&commit_seen, commit)) {
157 strbuf_addf(&missing, " - %s %.*s\n",
158 find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV),
159 item->arg_len,
160 todo_item_get_arg(old_todo, item));
161 *commit_seen_at(&commit_seen, commit) = 1;
162 }
163 }
164
165 /* Warn about missing commits */
166 if (!missing.len)
167 goto leave_check;
168
169 if (check_level == MISSING_COMMIT_CHECK_ERROR)
170 res = 1;
171
172 fprintf(stderr,
173 _("Warning: some commits may have been dropped accidentally.\n"
174 "Dropped commits (newer to older):\n"));
175
176 /* Make the list user-friendly and display */
177 fputs(missing.buf, stderr);
178 strbuf_release(&missing);
179
180 fprintf(stderr, _("To avoid this message, use \"drop\" to "
181 "explicitly remove a commit.\n\n"
182 "Use 'git config rebase.missingCommitsCheck' to change "
183 "the level of warnings.\n"
184 "The possible behaviours are: ignore, warn, error.\n\n"));
185
186leave_check:
187 clear_commit_seen(&commit_seen);
188 return res;
189}