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