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