3 #include "environment.h"
6 #include "rebase-interactive.h"
8 #include "commit-slab.h"
13 static const char edit_todo_list_advice
[] =
14 N_("You can fix this with 'git rebase --edit-todo' "
15 "and then run 'git rebase --continue'.\n"
16 "Or you can abort the rebase with 'git rebase"
19 enum missing_commit_check_level
{
20 MISSING_COMMIT_CHECK_IGNORE
= 0,
21 MISSING_COMMIT_CHECK_WARN
,
22 MISSING_COMMIT_CHECK_ERROR
25 static enum missing_commit_check_level
get_missing_commit_check_level(void)
29 if (git_config_get_value("rebase.missingcommitscheck", &value
) ||
30 !strcasecmp("ignore", value
))
31 return MISSING_COMMIT_CHECK_IGNORE
;
32 if (!strcasecmp("warn", value
))
33 return MISSING_COMMIT_CHECK_WARN
;
34 if (!strcasecmp("error", value
))
35 return MISSING_COMMIT_CHECK_ERROR
;
36 warning(_("unrecognized setting %s for option "
37 "rebase.missingCommitsCheck. Ignoring."), value
);
38 return MISSING_COMMIT_CHECK_IGNORE
;
41 void append_todo_help(int command_count
,
42 const char *shortrevisions
, const char *shortonto
,
45 const char *msg
= _("\nCommands:\n"
46 "p, pick <commit> = use commit\n"
47 "r, reword <commit> = use commit, but edit the commit message\n"
48 "e, edit <commit> = use commit, but stop for amending\n"
49 "s, squash <commit> = use commit, but meld into previous commit\n"
50 "f, fixup [-C | -c] <commit> = like \"squash\" but keep only the previous\n"
51 " commit's log message, unless -C is used, in which case\n"
52 " keep only this commit's message; -c is same as -C but\n"
54 "x, exec <command> = run command (the rest of the line) using shell\n"
55 "b, break = stop here (continue rebase later with 'git rebase --continue')\n"
56 "d, drop <commit> = remove commit\n"
57 "l, label <label> = label current HEAD with a name\n"
58 "t, reset <label> = reset HEAD to a label\n"
59 "m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]\n"
60 " create a merge commit using the original merge commit's\n"
61 " message (or the oneline, if no original merge commit was\n"
62 " specified); use -c <commit> to reword the commit message\n"
63 "u, update-ref <ref> = track a placeholder for the <ref> to be updated\n"
64 " to this position in the new commits. The <ref> is\n"
65 " updated at the end of the rebase\n"
67 "These lines can be re-ordered; they are executed from top to bottom.\n");
68 unsigned edit_todo
= !(shortrevisions
&& shortonto
);
71 strbuf_addch(buf
, '\n');
72 strbuf_commented_addf(buf
, Q_("Rebase %s onto %s (%d command)",
73 "Rebase %s onto %s (%d commands)",
75 shortrevisions
, shortonto
, command_count
);
78 strbuf_add_commented_lines(buf
, msg
, strlen(msg
));
80 if (get_missing_commit_check_level() == MISSING_COMMIT_CHECK_ERROR
)
81 msg
= _("\nDo not remove any line. Use 'drop' "
82 "explicitly to remove a commit.\n");
84 msg
= _("\nIf you remove a line here "
85 "THAT COMMIT WILL BE LOST.\n");
87 strbuf_add_commented_lines(buf
, msg
, strlen(msg
));
90 msg
= _("\nYou are editing the todo file "
91 "of an ongoing interactive rebase.\n"
92 "To continue rebase after editing, run:\n"
93 " git rebase --continue\n\n");
95 msg
= _("\nHowever, if you remove everything, "
96 "the rebase will be aborted.\n\n");
98 strbuf_add_commented_lines(buf
, msg
, strlen(msg
));
101 int edit_todo_list(struct repository
*r
, struct todo_list
*todo_list
,
102 struct todo_list
*new_todo
, const char *shortrevisions
,
103 const char *shortonto
, unsigned flags
)
105 const char *todo_file
= rebase_path_todo(),
106 *todo_backup
= rebase_path_todo_backup();
107 unsigned initial
= shortrevisions
&& shortonto
;
110 /* If the user is editing the todo list, we first try to parse
111 * it. If there is an error, we do not return, because the user
112 * might want to fix it in the first place. */
114 incorrect
= todo_list_parse_insn_buffer(r
, todo_list
->buf
.buf
, todo_list
) |
115 file_exists(rebase_path_dropped());
117 if (todo_list_write_to_file(r
, todo_list
, todo_file
, shortrevisions
, shortonto
,
118 -1, flags
| TODO_LIST_SHORTEN_IDS
| TODO_LIST_APPEND_TODO_HELP
))
119 return error_errno(_("could not write '%s'"), todo_file
);
122 todo_list_write_to_file(r
, todo_list
, todo_backup
,
123 shortrevisions
, shortonto
, -1,
124 (flags
| TODO_LIST_APPEND_TODO_HELP
) & ~TODO_LIST_SHORTEN_IDS
) < 0)
125 return error(_("could not write '%s'."), rebase_path_todo_backup());
127 if (launch_sequence_editor(todo_file
, &new_todo
->buf
, NULL
))
130 strbuf_stripspace(&new_todo
->buf
, 1);
131 if (initial
&& new_todo
->buf
.len
== 0)
134 if (todo_list_parse_insn_buffer(r
, new_todo
->buf
.buf
, new_todo
)) {
135 fprintf(stderr
, _(edit_todo_list_advice
));
140 if (todo_list_check_against_backup(r
, new_todo
)) {
141 write_file(rebase_path_dropped(), "%s", "");
146 unlink(rebase_path_dropped());
147 } else if (todo_list_check(todo_list
, new_todo
)) {
148 write_file(rebase_path_dropped(), "%s", "");
153 * See if branches need to be added or removed from the update-refs
154 * file based on the new todo list.
156 todo_list_filter_update_refs(r
, new_todo
);
161 define_commit_slab(commit_seen
, unsigned char);
163 * Check if the user dropped some commits by mistake
164 * Behaviour determined by rebase.missingCommitsCheck.
165 * Check if there is an unrecognized command or a
166 * bad SHA-1 in a command.
168 int todo_list_check(struct todo_list
*old_todo
, struct todo_list
*new_todo
)
170 enum missing_commit_check_level check_level
= get_missing_commit_check_level();
171 struct strbuf missing
= STRBUF_INIT
;
173 struct commit_seen commit_seen
;
175 init_commit_seen(&commit_seen
);
177 if (check_level
== MISSING_COMMIT_CHECK_IGNORE
)
180 /* Mark the commits in git-rebase-todo as seen */
181 for (i
= 0; i
< new_todo
->nr
; i
++) {
182 struct commit
*commit
= new_todo
->items
[i
].commit
;
184 *commit_seen_at(&commit_seen
, commit
) = 1;
187 /* Find commits in git-rebase-todo.backup yet unseen */
188 for (i
= old_todo
->nr
- 1; i
>= 0; i
--) {
189 struct todo_item
*item
= old_todo
->items
+ i
;
190 struct commit
*commit
= item
->commit
;
191 if (commit
&& !*commit_seen_at(&commit_seen
, commit
)) {
192 strbuf_addf(&missing
, " - %s %.*s\n",
193 find_unique_abbrev(&commit
->object
.oid
, DEFAULT_ABBREV
),
195 todo_item_get_arg(old_todo
, item
));
196 *commit_seen_at(&commit_seen
, commit
) = 1;
200 /* Warn about missing commits */
204 if (check_level
== MISSING_COMMIT_CHECK_ERROR
)
208 _("Warning: some commits may have been dropped accidentally.\n"
209 "Dropped commits (newer to older):\n"));
211 /* Make the list user-friendly and display */
212 fputs(missing
.buf
, stderr
);
213 strbuf_release(&missing
);
215 fprintf(stderr
, _("To avoid this message, use \"drop\" to "
216 "explicitly remove a commit.\n\n"
217 "Use 'git config rebase.missingCommitsCheck' to change "
218 "the level of warnings.\n"
219 "The possible behaviours are: ignore, warn, error.\n\n"));
221 fprintf(stderr
, _(edit_todo_list_advice
));
224 clear_commit_seen(&commit_seen
);
228 int todo_list_check_against_backup(struct repository
*r
, struct todo_list
*todo_list
)
230 struct todo_list backup
= TODO_LIST_INIT
;
233 if (strbuf_read_file(&backup
.buf
, rebase_path_todo_backup(), 0) > 0) {
234 todo_list_parse_insn_buffer(r
, backup
.buf
.buf
, &backup
);
235 res
= todo_list_check(&backup
, todo_list
);
238 todo_list_release(&backup
);