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