Elijah Newren | d812c3b | 2023-04-11 00:41:56 -0700 | [diff] [blame] | 1 | #include "git-compat-util.h" |
Alban Gruin | 145e05a | 2018-08-10 18:51:29 +0200 | [diff] [blame] | 2 | #include "commit.h" |
Elijah Newren | 4e12082 | 2023-04-11 00:41:57 -0700 | [diff] [blame] | 3 | #include "editor.h" |
Elijah Newren | 32a8f51 | 2023-03-21 06:26:03 +0000 | [diff] [blame] | 4 | #include "environment.h" |
Elijah Newren | f394e09 | 2023-03-21 06:25:54 +0000 | [diff] [blame] | 5 | #include "gettext.h" |
Alban Gruin | 145e05a | 2018-08-10 18:51:29 +0200 | [diff] [blame] | 6 | #include "sequencer.h" |
Alban Gruin | 6ca89c6 | 2019-01-29 16:01:49 +0100 | [diff] [blame] | 7 | #include "rebase-interactive.h" |
Elijah Newren | d1cbe1e | 2023-04-22 20:17:20 +0000 | [diff] [blame] | 8 | #include "repository.h" |
Alban Gruin | 145e05a | 2018-08-10 18:51:29 +0200 | [diff] [blame] | 9 | #include "strbuf.h" |
Alban Gruin | 6ca89c6 | 2019-01-29 16:01:49 +0100 | [diff] [blame] | 10 | #include "commit-slab.h" |
| 11 | #include "config.h" |
Alban Gruin | 5a5445d | 2020-01-28 22:12:46 +0100 | [diff] [blame] | 12 | #include "dir.h" |
Elijah Newren | dabab1d | 2023-04-11 00:41:49 -0700 | [diff] [blame] | 13 | #include "object-name.h" |
Alban Gruin | 6ca89c6 | 2019-01-29 16:01:49 +0100 | [diff] [blame] | 14 | |
Alban Gruin | 1da5874 | 2020-01-28 22:12:45 +0100 | [diff] [blame] | 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"); |
| 20 | |
Alban Gruin | 6ca89c6 | 2019-01-29 16:01:49 +0100 | [diff] [blame] | 21 | enum missing_commit_check_level { |
| 22 | MISSING_COMMIT_CHECK_IGNORE = 0, |
| 23 | MISSING_COMMIT_CHECK_WARN, |
| 24 | MISSING_COMMIT_CHECK_ERROR |
| 25 | }; |
| 26 | |
| 27 | static enum missing_commit_check_level get_missing_commit_check_level(void) |
| 28 | { |
| 29 | const char *value; |
| 30 | |
| 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; |
| 41 | } |
Alban Gruin | 145e05a | 2018-08-10 18:51:29 +0200 | [diff] [blame] | 42 | |
Elijah Newren | d48e5e2 | 2020-02-15 21:36:24 +0000 | [diff] [blame] | 43 | void append_todo_help(int command_count, |
Alban Gruin | af1fc3a | 2019-03-05 20:18:02 +0100 | [diff] [blame] | 44 | const char *shortrevisions, const char *shortonto, |
Alban Gruin | a9f5476 | 2018-08-10 18:51:35 +0200 | [diff] [blame] | 45 | struct strbuf *buf) |
Alban Gruin | 145e05a | 2018-08-10 18:51:29 +0200 | [diff] [blame] | 46 | { |
Alban Gruin | 145e05a | 2018-08-10 18:51:29 +0200 | [diff] [blame] | 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" |
Charvi Mendiratta | f07871d | 2021-02-10 17:06:43 +0530 | [diff] [blame] | 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" |
Alban Gruin | 145e05a | 2018-08-10 18:51:29 +0200 | [diff] [blame] | 56 | "x, exec <command> = run command (the rest of the line) using shell\n" |
Johannes Schindelin | 71f8246 | 2018-10-12 06:14:26 -0700 | [diff] [blame] | 57 | "b, break = stop here (continue rebase later with 'git rebase --continue')\n" |
Alban Gruin | 145e05a | 2018-08-10 18:51:29 +0200 | [diff] [blame] | 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" |
Derrick Stolee | f57fd48 | 2022-07-19 18:33:36 +0000 | [diff] [blame] | 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" |
Derrick Stolee | a97d791 | 2022-07-19 18:33:38 +0000 | [diff] [blame] | 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" |
Alban Gruin | 145e05a | 2018-08-10 18:51:29 +0200 | [diff] [blame] | 68 | "\n" |
| 69 | "These lines can be re-ordered; they are executed from top to bottom.\n"); |
Alban Gruin | af1fc3a | 2019-03-05 20:18:02 +0100 | [diff] [blame] | 70 | unsigned edit_todo = !(shortrevisions && shortonto); |
| 71 | |
| 72 | if (!edit_todo) { |
| 73 | strbuf_addch(buf, '\n'); |
Jeff King | 3a35d96 | 2024-03-12 05:17:29 -0400 | [diff] [blame] | 74 | strbuf_commented_addf(buf, comment_line_str, |
Calvin Wan | 787cb8a | 2023-06-06 19:48:43 +0000 | [diff] [blame] | 75 | Q_("Rebase %s onto %s (%d command)", |
| 76 | "Rebase %s onto %s (%d commands)", |
| 77 | command_count), |
Alban Gruin | af1fc3a | 2019-03-05 20:18:02 +0100 | [diff] [blame] | 78 | shortrevisions, shortonto, command_count); |
| 79 | } |
Alban Gruin | 145e05a | 2018-08-10 18:51:29 +0200 | [diff] [blame] | 80 | |
Jeff King | a1bb146 | 2024-03-12 05:17:32 -0400 | [diff] [blame] | 81 | strbuf_add_commented_lines(buf, msg, strlen(msg), comment_line_str); |
Alban Gruin | 145e05a | 2018-08-10 18:51:29 +0200 | [diff] [blame] | 82 | |
| 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"); |
| 89 | |
Jeff King | a1bb146 | 2024-03-12 05:17:32 -0400 | [diff] [blame] | 90 | strbuf_add_commented_lines(buf, msg, strlen(msg), comment_line_str); |
Alban Gruin | 145e05a | 2018-08-10 18:51:29 +0200 | [diff] [blame] | 91 | |
| 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"); |
| 100 | |
Jeff King | a1bb146 | 2024-03-12 05:17:32 -0400 | [diff] [blame] | 101 | strbuf_add_commented_lines(buf, msg, strlen(msg), comment_line_str); |
Alban Gruin | a9f5476 | 2018-08-10 18:51:35 +0200 | [diff] [blame] | 102 | } |
| 103 | |
Alban Gruin | a930eb0 | 2019-03-05 20:18:03 +0100 | [diff] [blame] | 104 | int edit_todo_list(struct repository *r, struct todo_list *todo_list, |
| 105 | struct todo_list *new_todo, const char *shortrevisions, |
| 106 | const char *shortonto, unsigned flags) |
Alban Gruin | 64a43cb | 2018-08-10 18:51:31 +0200 | [diff] [blame] | 107 | { |
Alban Gruin | 5a5445d | 2020-01-28 22:12:46 +0100 | [diff] [blame] | 108 | const char *todo_file = rebase_path_todo(), |
| 109 | *todo_backup = rebase_path_todo_backup(); |
Alban Gruin | a930eb0 | 2019-03-05 20:18:03 +0100 | [diff] [blame] | 110 | unsigned initial = shortrevisions && shortonto; |
Alban Gruin | 5a5445d | 2020-01-28 22:12:46 +0100 | [diff] [blame] | 111 | int incorrect = 0; |
Alban Gruin | 64a43cb | 2018-08-10 18:51:31 +0200 | [diff] [blame] | 112 | |
Alban Gruin | a930eb0 | 2019-03-05 20:18:03 +0100 | [diff] [blame] | 113 | /* If the user is editing the todo list, we first try to parse |
| 114 | * it. If there is an error, we do not return, because the user |
| 115 | * might want to fix it in the first place. */ |
| 116 | if (!initial) |
Alban Gruin | 5a5445d | 2020-01-28 22:12:46 +0100 | [diff] [blame] | 117 | incorrect = todo_list_parse_insn_buffer(r, todo_list->buf.buf, todo_list) | |
| 118 | file_exists(rebase_path_dropped()); |
Alban Gruin | 64a43cb | 2018-08-10 18:51:31 +0200 | [diff] [blame] | 119 | |
Alban Gruin | a930eb0 | 2019-03-05 20:18:03 +0100 | [diff] [blame] | 120 | if (todo_list_write_to_file(r, todo_list, todo_file, shortrevisions, shortonto, |
| 121 | -1, flags | TODO_LIST_SHORTEN_IDS | TODO_LIST_APPEND_TODO_HELP)) |
| 122 | return error_errno(_("could not write '%s'"), todo_file); |
Alban Gruin | 64a43cb | 2018-08-10 18:51:31 +0200 | [diff] [blame] | 123 | |
Alban Gruin | 5a5445d | 2020-01-28 22:12:46 +0100 | [diff] [blame] | 124 | if (!incorrect && |
| 125 | todo_list_write_to_file(r, todo_list, todo_backup, |
Johannes Schindelin | 2602762 | 2020-01-23 12:28:19 +0000 | [diff] [blame] | 126 | shortrevisions, shortonto, -1, |
| 127 | (flags | TODO_LIST_APPEND_TODO_HELP) & ~TODO_LIST_SHORTEN_IDS) < 0) |
| 128 | return error(_("could not write '%s'."), rebase_path_todo_backup()); |
Alban Gruin | a9f5476 | 2018-08-10 18:51:35 +0200 | [diff] [blame] | 129 | |
Alban Gruin | a930eb0 | 2019-03-05 20:18:03 +0100 | [diff] [blame] | 130 | if (launch_sequence_editor(todo_file, &new_todo->buf, NULL)) |
| 131 | return -2; |
Alban Gruin | 64a43cb | 2018-08-10 18:51:31 +0200 | [diff] [blame] | 132 | |
Jeff King | 2982b65 | 2024-03-12 05:17:27 -0400 | [diff] [blame] | 133 | strbuf_stripspace(&new_todo->buf, comment_line_str); |
Alban Gruin | a930eb0 | 2019-03-05 20:18:03 +0100 | [diff] [blame] | 134 | if (initial && new_todo->buf.len == 0) |
| 135 | return -3; |
| 136 | |
Alban Gruin | 5a5445d | 2020-01-28 22:12:46 +0100 | [diff] [blame] | 137 | if (todo_list_parse_insn_buffer(r, new_todo->buf.buf, new_todo)) { |
| 138 | fprintf(stderr, _(edit_todo_list_advice)); |
| 139 | return -4; |
| 140 | } |
| 141 | |
| 142 | if (incorrect) { |
| 143 | if (todo_list_check_against_backup(r, new_todo)) { |
Ralf Thielow | 7daf4f2 | 2020-02-27 20:25:30 +0000 | [diff] [blame] | 144 | write_file(rebase_path_dropped(), "%s", ""); |
Alban Gruin | 5a5445d | 2020-01-28 22:12:46 +0100 | [diff] [blame] | 145 | return -4; |
| 146 | } |
| 147 | |
| 148 | if (incorrect > 0) |
| 149 | unlink(rebase_path_dropped()); |
| 150 | } else if (todo_list_check(todo_list, new_todo)) { |
Ralf Thielow | 7daf4f2 | 2020-02-27 20:25:30 +0000 | [diff] [blame] | 151 | write_file(rebase_path_dropped(), "%s", ""); |
Alban Gruin | 5a5445d | 2020-01-28 22:12:46 +0100 | [diff] [blame] | 152 | return -4; |
| 153 | } |
Alban Gruin | a930eb0 | 2019-03-05 20:18:03 +0100 | [diff] [blame] | 154 | |
Derrick Stolee | b3b1a21 | 2022-07-19 18:33:41 +0000 | [diff] [blame] | 155 | /* |
| 156 | * See if branches need to be added or removed from the update-refs |
| 157 | * file based on the new todo list. |
| 158 | */ |
| 159 | todo_list_filter_update_refs(r, new_todo); |
| 160 | |
Alban Gruin | a930eb0 | 2019-03-05 20:18:03 +0100 | [diff] [blame] | 161 | return 0; |
Alban Gruin | 64a43cb | 2018-08-10 18:51:31 +0200 | [diff] [blame] | 162 | } |
Alban Gruin | 6ca89c6 | 2019-01-29 16:01:49 +0100 | [diff] [blame] | 163 | |
| 164 | define_commit_slab(commit_seen, unsigned char); |
| 165 | /* |
| 166 | * Check if the user dropped some commits by mistake |
| 167 | * Behaviour determined by rebase.missingCommitsCheck. |
| 168 | * Check if there is an unrecognized command or a |
| 169 | * bad SHA-1 in a command. |
| 170 | */ |
| 171 | int todo_list_check(struct todo_list *old_todo, struct todo_list *new_todo) |
| 172 | { |
| 173 | enum missing_commit_check_level check_level = get_missing_commit_check_level(); |
| 174 | struct strbuf missing = STRBUF_INIT; |
| 175 | int res = 0, i; |
| 176 | struct commit_seen commit_seen; |
| 177 | |
| 178 | init_commit_seen(&commit_seen); |
| 179 | |
| 180 | if (check_level == MISSING_COMMIT_CHECK_IGNORE) |
| 181 | goto leave_check; |
| 182 | |
| 183 | /* Mark the commits in git-rebase-todo as seen */ |
| 184 | for (i = 0; i < new_todo->nr; i++) { |
| 185 | struct commit *commit = new_todo->items[i].commit; |
| 186 | if (commit) |
| 187 | *commit_seen_at(&commit_seen, commit) = 1; |
| 188 | } |
| 189 | |
| 190 | /* Find commits in git-rebase-todo.backup yet unseen */ |
| 191 | for (i = old_todo->nr - 1; i >= 0; i--) { |
| 192 | struct todo_item *item = old_todo->items + i; |
| 193 | struct commit *commit = item->commit; |
| 194 | if (commit && !*commit_seen_at(&commit_seen, commit)) { |
| 195 | strbuf_addf(&missing, " - %s %.*s\n", |
Ævar Arnfjörð Bjarmason | d850b7a | 2023-03-28 15:58:46 +0200 | [diff] [blame] | 196 | repo_find_unique_abbrev(the_repository, &commit->object.oid, DEFAULT_ABBREV), |
Alban Gruin | 6ca89c6 | 2019-01-29 16:01:49 +0100 | [diff] [blame] | 197 | item->arg_len, |
| 198 | todo_item_get_arg(old_todo, item)); |
| 199 | *commit_seen_at(&commit_seen, commit) = 1; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | /* Warn about missing commits */ |
| 204 | if (!missing.len) |
| 205 | goto leave_check; |
| 206 | |
| 207 | if (check_level == MISSING_COMMIT_CHECK_ERROR) |
| 208 | res = 1; |
| 209 | |
| 210 | fprintf(stderr, |
| 211 | _("Warning: some commits may have been dropped accidentally.\n" |
| 212 | "Dropped commits (newer to older):\n")); |
| 213 | |
| 214 | /* Make the list user-friendly and display */ |
| 215 | fputs(missing.buf, stderr); |
| 216 | strbuf_release(&missing); |
| 217 | |
| 218 | fprintf(stderr, _("To avoid this message, use \"drop\" to " |
| 219 | "explicitly remove a commit.\n\n" |
| 220 | "Use 'git config rebase.missingCommitsCheck' to change " |
| 221 | "the level of warnings.\n" |
| 222 | "The possible behaviours are: ignore, warn, error.\n\n")); |
| 223 | |
Alban Gruin | 5a5445d | 2020-01-28 22:12:46 +0100 | [diff] [blame] | 224 | fprintf(stderr, _(edit_todo_list_advice)); |
| 225 | |
Alban Gruin | 6ca89c6 | 2019-01-29 16:01:49 +0100 | [diff] [blame] | 226 | leave_check: |
| 227 | clear_commit_seen(&commit_seen); |
| 228 | return res; |
| 229 | } |
Alban Gruin | 1da5874 | 2020-01-28 22:12:45 +0100 | [diff] [blame] | 230 | |
Alban Gruin | 5a5445d | 2020-01-28 22:12:46 +0100 | [diff] [blame] | 231 | int todo_list_check_against_backup(struct repository *r, struct todo_list *todo_list) |
| 232 | { |
| 233 | struct todo_list backup = TODO_LIST_INIT; |
| 234 | int res = 0; |
| 235 | |
| 236 | if (strbuf_read_file(&backup.buf, rebase_path_todo_backup(), 0) > 0) { |
| 237 | todo_list_parse_insn_buffer(r, backup.buf.buf, &backup); |
| 238 | res = todo_list_check(&backup, todo_list); |
| 239 | } |
| 240 | |
| 241 | todo_list_release(&backup); |
| 242 | return res; |
| 243 | } |