1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
6 #include "environment.h"
9 #include "rebase-interactive.h"
10 #include "repository.h"
12 #include "commit-slab.h"
15 #include "object-name.h"
17 static const char edit_todo_list_advice
[] =
18 N_("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"
23 enum missing_commit_check_level
{
24 MISSING_COMMIT_CHECK_IGNORE
= 0,
25 MISSING_COMMIT_CHECK_WARN
,
26 MISSING_COMMIT_CHECK_ERROR
29 static enum missing_commit_check_level
get_missing_commit_check_level(void)
33 if (git_config_get_value("rebase.missingcommitscheck", &value
) ||
34 !strcasecmp("ignore", value
))
35 return MISSING_COMMIT_CHECK_IGNORE
;
36 if (!strcasecmp("warn", value
))
37 return MISSING_COMMIT_CHECK_WARN
;
38 if (!strcasecmp("error", value
))
39 return MISSING_COMMIT_CHECK_ERROR
;
40 warning(_("unrecognized setting %s for option "
41 "rebase.missingCommitsCheck. Ignoring."), value
);
42 return MISSING_COMMIT_CHECK_IGNORE
;
45 void append_todo_help(int command_count
,
46 const char *shortrevisions
, const char *shortonto
,
49 const 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"
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"
71 "These lines can be re-ordered; they are executed from top to bottom.\n");
72 unsigned edit_todo
= !(shortrevisions
&& shortonto
);
75 strbuf_addch(buf
, '\n');
76 strbuf_commented_addf(buf
, comment_line_str
,
77 Q_("Rebase %s onto %s (%d command)",
78 "Rebase %s onto %s (%d commands)",
80 shortrevisions
, shortonto
, command_count
);
83 strbuf_add_commented_lines(buf
, msg
, strlen(msg
), comment_line_str
);
85 if (get_missing_commit_check_level() == MISSING_COMMIT_CHECK_ERROR
)
86 msg
= _("\nDo not remove any line. Use 'drop' "
87 "explicitly to remove a commit.\n");
89 msg
= _("\nIf you remove a line here "
90 "THAT COMMIT WILL BE LOST.\n");
92 strbuf_add_commented_lines(buf
, msg
, strlen(msg
), comment_line_str
);
95 msg
= _("\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");
100 msg
= _("\nHowever, if you remove everything, "
101 "the rebase will be aborted.\n\n");
103 strbuf_add_commented_lines(buf
, msg
, strlen(msg
), comment_line_str
);
106 int edit_todo_list(struct repository
*r
, struct replay_opts
*opts
,
107 struct todo_list
*todo_list
, struct todo_list
*new_todo
,
108 const char *shortrevisions
, const char *shortonto
,
111 const char *todo_file
= rebase_path_todo(),
112 *todo_backup
= rebase_path_todo_backup();
113 unsigned initial
= shortrevisions
&& shortonto
;
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. */
120 incorrect
= todo_list_parse_insn_buffer(r
, opts
,
123 file_exists(rebase_path_dropped());
125 if (todo_list_write_to_file(r
, todo_list
, todo_file
, shortrevisions
, shortonto
,
126 -1, flags
| TODO_LIST_SHORTEN_IDS
| TODO_LIST_APPEND_TODO_HELP
))
127 return error_errno(_("could not write '%s'"), todo_file
);
130 todo_list_write_to_file(r
, todo_list
, todo_backup
,
131 shortrevisions
, shortonto
, -1,
132 (flags
| TODO_LIST_APPEND_TODO_HELP
) & ~TODO_LIST_SHORTEN_IDS
) < 0)
133 return error(_("could not write '%s'."), rebase_path_todo_backup());
135 if (launch_sequence_editor(todo_file
, &new_todo
->buf
, NULL
))
138 strbuf_stripspace(&new_todo
->buf
, comment_line_str
);
139 if (initial
&& new_todo
->buf
.len
== 0)
142 if (todo_list_parse_insn_buffer(r
, opts
, new_todo
->buf
.buf
, new_todo
)) {
143 fprintf(stderr
, _(edit_todo_list_advice
));
148 if (todo_list_check_against_backup(r
, opts
, new_todo
)) {
149 write_file(rebase_path_dropped(), "%s", "");
154 unlink(rebase_path_dropped());
155 } else if (todo_list_check(todo_list
, new_todo
)) {
156 write_file(rebase_path_dropped(), "%s", "");
161 * See if branches need to be added or removed from the update-refs
162 * file based on the new todo list.
164 todo_list_filter_update_refs(r
, new_todo
);
169 define_commit_slab(commit_seen
, unsigned char);
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.
176 int todo_list_check(struct todo_list
*old_todo
, struct todo_list
*new_todo
)
178 enum missing_commit_check_level check_level
= get_missing_commit_check_level();
179 struct strbuf missing
= STRBUF_INIT
;
181 struct commit_seen commit_seen
;
183 init_commit_seen(&commit_seen
);
185 if (check_level
== MISSING_COMMIT_CHECK_IGNORE
)
188 /* Mark the commits in git-rebase-todo as seen */
189 for (i
= 0; i
< new_todo
->nr
; i
++) {
190 struct commit
*commit
= new_todo
->items
[i
].commit
;
192 *commit_seen_at(&commit_seen
, commit
) = 1;
195 /* Find commits in git-rebase-todo.backup yet unseen */
196 for (i
= old_todo
->nr
- 1; i
>= 0; i
--) {
197 struct todo_item
*item
= old_todo
->items
+ i
;
198 struct commit
*commit
= item
->commit
;
199 if (commit
&& !*commit_seen_at(&commit_seen
, commit
)) {
200 strbuf_addf(&missing
, " - %s %.*s\n",
201 repo_find_unique_abbrev(the_repository
, &commit
->object
.oid
, DEFAULT_ABBREV
),
203 todo_item_get_arg(old_todo
, item
));
204 *commit_seen_at(&commit_seen
, commit
) = 1;
208 /* Warn about missing commits */
212 if (check_level
== MISSING_COMMIT_CHECK_ERROR
)
216 _("Warning: some commits may have been dropped accidentally.\n"
217 "Dropped commits (newer to older):\n"));
219 /* Make the list user-friendly and display */
220 fputs(missing
.buf
, stderr
);
221 strbuf_release(&missing
);
223 fprintf(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"));
229 fprintf(stderr
, _(edit_todo_list_advice
));
232 clear_commit_seen(&commit_seen
);
236 int todo_list_check_against_backup(struct repository
*r
,
237 struct replay_opts
*opts
,
238 struct todo_list
*todo_list
)
240 struct todo_list backup
= TODO_LIST_INIT
;
243 if (strbuf_read_file(&backup
.buf
, rebase_path_todo_backup(), 0) > 0) {
244 todo_list_parse_insn_buffer(r
, opts
, backup
.buf
.buf
, &backup
);
245 res
= todo_list_check(&backup
, todo_list
);
248 todo_list_release(&backup
);