git
[Ported from github.com/git/git] Sync with Git 2.56.1
Brought by drakestapleton
| 1 | #define USE_THE_REPOSITORY_VARIABLE |
| 2 | |
| 3 | #include "builtin.h" |
| 4 | #include "advice.h" |
| 5 | #include "cache-tree.h" |
| 6 | #include "commit.h" |
| 7 | #include "commit-reach.h" |
| 8 | #include "config.h" |
| 9 | #include "editor.h" |
| 10 | #include "environment.h" |
| 11 | #include "gettext.h" |
| 12 | #include "hex.h" |
| 13 | #include "lockfile.h" |
| 14 | #include "merge-ort.h" |
| 15 | #include "oidmap.h" |
| 16 | #include "parse-options.h" |
| 17 | #include "path.h" |
| 18 | #include "read-cache.h" |
| 19 | #include "refs.h" |
| 20 | #include "ref-filter.h" |
| 21 | #include "replay.h" |
| 22 | #include "reset.h" |
| 23 | #include "revision.h" |
| 24 | #include "sequencer.h" |
| 25 | #include "string-list.h" |
| 26 | #include "strvec.h" |
| 27 | #include "tree.h" |
| 28 | #include "tree-walk.h" |
| 29 | #include "unpack-trees.h" |
| 30 | #include "wt-status.h" |
| 31 | |
| 32 | #define GIT_HISTORY_DROP_USAGE \ |
| 33 | N_("git history drop <commit> [--dry-run] [--update-refs=(branches|head)] [--empty=(drop|keep|abort)] [--[no-]gpg-sign[=<key-id>]]") |
| 34 | #define GIT_HISTORY_FIXUP_USAGE \ |
| 35 | N_("git history fixup <commit> [--dry-run] [--update-refs=(branches|head)] [--reedit-message] [--empty=(drop|keep|abort)] [--[no-]gpg-sign[=<key-id>]]") |
| 36 | #define GIT_HISTORY_REWORD_USAGE \ |
| 37 | N_("git history reword <commit> [--dry-run] [--update-refs=(branches|head)] [--[no-]gpg-sign[=<key-id>]]") |
| 38 | #define GIT_HISTORY_SPLIT_USAGE \ |
| 39 | N_("git history split <commit> [--dry-run] [--update-refs=(branches|head)] [--[no-]gpg-sign[=<key-id>]] [--] [<pathspec>...]") |
| 40 | #define GIT_HISTORY_SQUASH_USAGE \ |
| 41 | N_("git history squash [--dry-run] [--update-refs=(branches|head)] [--[no-]edit] [--[no-]gpg-sign[=<key-id>]] <revision-range>") |
| 42 | |
| 43 | static void change_data_free(void *util, const char *str UNUSED) |
| 44 | { |
| 45 | struct wt_status_change_data *d = util; |
| 46 | free(d->rename_source); |
| 47 | free(d); |
| 48 | } |
| 49 | |
| 50 | static int fill_commit_message(struct repository *repo, |
| 51 | const struct object_id *old_tree, |
| 52 | const struct object_id *new_tree, |
| 53 | const char *default_message, |
| 54 | const char *action, |
| 55 | struct strbuf *out) |
| 56 | { |
| 57 | const char *path = git_path_commit_editmsg(); |
| 58 | const char *hint = |
| 59 | _("Please enter the commit message for the %s changes." |
| 60 | " Lines starting\nwith '%s' will be ignored, and an" |
| 61 | " empty message aborts the commit.\n"); |
| 62 | struct wt_status s; |
| 63 | |
| 64 | wt_status_prepare(repo, &s); |
| 65 | FREE_AND_NULL(s.branch); |
| 66 | s.ahead_behind_flags = AHEAD_BEHIND_QUICK; |
| 67 | s.commit_template = 1; |
| 68 | s.colopts = 0; |
| 69 | s.display_comment_prefix = 1; |
| 70 | s.hints = 0; |
| 71 | s.use_color = 0; |
| 72 | s.whence = FROM_COMMIT; |
| 73 | s.committable = 1; |
| 74 | |
| 75 | s.fp = fopen(path, "w"); |
| 76 | if (!s.fp) |
| 77 | return error_errno(_("could not open '%s'"), path); |
| 78 | |
| 79 | strbuf_addstr(out, default_message); |
| 80 | strbuf_addch(out, '\n'); |
| 81 | strbuf_commented_addf(out, comment_line_str, hint, action, comment_line_str); |
| 82 | if (fwrite(out->buf, 1, out->len, s.fp) != out->len) |
| 83 | die_errno(_("could not write to '%s'"), path); |
| 84 | |
| 85 | wt_status_collect_changes_trees(&s, old_tree, new_tree); |
| 86 | wt_status_print(&s); |
| 87 | wt_status_collect_free_buffers(&s); |
| 88 | string_list_clear_func(&s.change, change_data_free); |
| 89 | if (fclose(s.fp)) |
| 90 | die_errno(_("could not write to '%s'"), path); |
| 91 | |
| 92 | strbuf_reset(out); |
| 93 | if (launch_editor(path, out, NULL)) { |
| 94 | fprintf(stderr, _("Aborting commit as launching the editor failed.\n")); |
| 95 | return -1; |
| 96 | } |
| 97 | strbuf_stripspace(out, comment_line_str); |
| 98 | |
| 99 | cleanup_message(out, COMMIT_MSG_CLEANUP_ALL, 0); |
| 100 | |
| 101 | if (!out->len) { |
| 102 | fprintf(stderr, _("Aborting commit due to empty commit message.\n")); |
| 103 | return -1; |
| 104 | } |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | enum commit_tree_flags { |
| 110 | COMMIT_TREE_EDIT_MESSAGE = (1 << 0), |
| 111 | }; |
| 112 | |
| 113 | static int history_config(const char *var, const char *value, |
| 114 | const struct config_context *ctx, void *data) |
| 115 | { |
| 116 | const char **sign_commit = data; |
| 117 | |
| 118 | if (!strcmp(var, "commit.gpgsign")) { |
| 119 | *sign_commit = git_config_bool(var, value) ? "" : NULL; |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | return git_default_config(var, value, ctx, NULL); |
| 124 | } |
| 125 | |
| 126 | #define OPT_HISTORY_GPG_SIGN(v) { \ |
| 127 | .type = OPTION_STRING, \ |
| 128 | .short_name = 'S', \ |
| 129 | .long_name = "gpg-sign", \ |
| 130 | .value = (v), \ |
| 131 | .argh = N_("key-id"), \ |
| 132 | .help = N_("GPG-sign rewritten commits"), \ |
| 133 | .flags = PARSE_OPT_OPTARG, \ |
| 134 | .defval = (intptr_t)"", \ |
| 135 | } |
| 136 | |
| 137 | static int commit_tree_ext(struct repository *repo, |
| 138 | const char *action, |
| 139 | struct commit *commit_with_message, |
| 140 | const char *message_template, |
| 141 | const struct commit_list *parents, |
| 142 | const struct object_id *old_tree, |
| 143 | const struct object_id *new_tree, |
| 144 | const char *sign_commit, |
| 145 | struct commit **out, |
| 146 | enum commit_tree_flags flags) |
| 147 | { |
| 148 | const char *exclude_gpgsig[] = { |
| 149 | /* We reencode the message, so the encoding needs to be stripped. */ |
| 150 | "encoding", |
| 151 | /* We need to strip signatures as those will become invalid. */ |
| 152 | "gpgsig", |
| 153 | "gpgsig-sha256", |
| 154 | NULL, |
| 155 | }; |
| 156 | const char *original_message, *original_body, *ptr; |
| 157 | struct commit_extra_header *original_extra_headers = NULL; |
| 158 | struct strbuf commit_message = STRBUF_INIT; |
| 159 | struct object_id rewritten_commit_oid; |
| 160 | char *original_author = NULL; |
| 161 | size_t len; |
| 162 | int ret; |
| 163 | |
| 164 | /* We retain authorship of the original commit. */ |
| 165 | original_message = repo_logmsg_reencode(repo, commit_with_message, NULL, NULL); |
| 166 | ptr = find_commit_header(original_message, "author", &len); |
| 167 | if (ptr) |
| 168 | original_author = xmemdupz(ptr, len); |
| 169 | find_commit_subject(original_message, &original_body); |
| 170 | |
| 171 | if (!message_template) |
| 172 | message_template = original_body; |
| 173 | |
| 174 | if (flags & COMMIT_TREE_EDIT_MESSAGE) { |
| 175 | ret = fill_commit_message(repo, old_tree, new_tree, |
| 176 | message_template, action, &commit_message); |
| 177 | if (ret < 0) |
| 178 | goto out; |
| 179 | } else { |
| 180 | strbuf_addstr(&commit_message, message_template); |
| 181 | } |
| 182 | |
| 183 | original_extra_headers = read_commit_extra_headers(commit_with_message, |
| 184 | exclude_gpgsig); |
| 185 | |
| 186 | ret = commit_tree_extended(commit_message.buf, commit_message.len, new_tree, |
| 187 | parents, &rewritten_commit_oid, original_author, |
| 188 | NULL, sign_commit, original_extra_headers); |
| 189 | if (ret < 0) |
| 190 | goto out; |
| 191 | |
| 192 | *out = lookup_commit_or_die(&rewritten_commit_oid, "rewritten commit"); |
| 193 | |
| 194 | out: |
| 195 | free_commit_extra_headers(original_extra_headers); |
| 196 | strbuf_release(&commit_message); |
| 197 | free(original_author); |
| 198 | repo_unuse_commit_buffer(repo, commit_with_message, original_message); |
| 199 | return ret; |
| 200 | } |
| 201 | |
| 202 | static int first_parent_tree_oid(struct repository *repo, |
| 203 | struct commit *commit, |
| 204 | struct object_id *out) |
| 205 | { |
| 206 | struct commit *parent = commit->parents ? commit->parents->item : NULL; |
| 207 | |
| 208 | if (!parent) { |
| 209 | oidcpy(out, repo->hash_algo->empty_tree); |
| 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | if (repo_parse_commit(repo, parent)) |
| 214 | return error(_("unable to parse parent commit %s"), |
| 215 | oid_to_hex(&parent->object.oid)); |
| 216 | |
| 217 | oidcpy(out, &repo_get_commit_tree(repo, parent)->object.oid); |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | static int commit_tree_with_edited_message(struct repository *repo, |
| 222 | const char *action, |
| 223 | struct commit *original, |
| 224 | const char *sign_commit, |
| 225 | struct commit **out) |
| 226 | { |
| 227 | struct object_id parent_tree_oid; |
| 228 | const struct object_id *tree_oid; |
| 229 | |
| 230 | tree_oid = &repo_get_commit_tree(repo, original)->object.oid; |
| 231 | |
| 232 | if (first_parent_tree_oid(repo, original, &parent_tree_oid) < 0) |
| 233 | return -1; |
| 234 | |
| 235 | return commit_tree_ext(repo, action, original, NULL, original->parents, |
| 236 | &parent_tree_oid, tree_oid, sign_commit, out, |
| 237 | COMMIT_TREE_EDIT_MESSAGE); |
| 238 | } |
| 239 | |
| 240 | enum ref_action { |
| 241 | REF_ACTION_DEFAULT, |
| 242 | REF_ACTION_BRANCHES, |
| 243 | REF_ACTION_HEAD, |
| 244 | }; |
| 245 | |
| 246 | static int parse_ref_action(const struct option *opt, const char *value, int unset) |
| 247 | { |
| 248 | enum ref_action *action = opt->value; |
| 249 | |
| 250 | BUG_ON_OPT_NEG_NOARG(unset, value); |
| 251 | if (!strcmp(value, "branches")) { |
| 252 | *action = REF_ACTION_BRANCHES; |
| 253 | } else if (!strcmp(value, "head")) { |
| 254 | *action = REF_ACTION_HEAD; |
| 255 | } else { |
| 256 | return error(_("%s expects one of 'branches' or 'head'"), |
| 257 | opt->long_name); |
| 258 | } |
| 259 | |
| 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | static int revwalk_contains_merges(struct repository *repo, |
| 264 | const struct strvec *revwalk_args) |
| 265 | { |
| 266 | struct strvec args = STRVEC_INIT; |
| 267 | struct rev_info revs; |
| 268 | int ret; |
| 269 | |
| 270 | strvec_pushv(&args, revwalk_args->v); |
| 271 | strvec_push(&args, "--min-parents=2"); |
| 272 | |
| 273 | repo_init_revisions(repo, &revs, NULL); |
| 274 | |
| 275 | setup_revisions_from_strvec(&args, &revs, NULL); |
| 276 | if (args.nr != 1) |
| 277 | BUG("revisions were set up with invalid argument"); |
| 278 | |
| 279 | if (prepare_revision_walk(&revs) < 0) { |
| 280 | ret = error(_("error preparing revisions")); |
| 281 | goto out; |
| 282 | } |
| 283 | |
| 284 | if (get_revision(&revs)) { |
| 285 | ret = error(_("replaying merge commits is not supported yet!")); |
| 286 | goto out; |
| 287 | } |
| 288 | |
| 289 | reset_revision_walk(); |
| 290 | ret = 0; |
| 291 | |
| 292 | out: |
| 293 | release_revisions(&revs); |
| 294 | strvec_clear(&args); |
| 295 | return ret; |
| 296 | } |
| 297 | |
| 298 | static int setup_revwalk(struct repository *repo, |
| 299 | enum ref_action action, |
| 300 | struct commit *original, |
| 301 | struct rev_info *revs) |
| 302 | { |
| 303 | struct strvec args = STRVEC_INIT; |
| 304 | int ret; |
| 305 | |
| 306 | repo_init_revisions(repo, revs, NULL); |
| 307 | strvec_push(&args, "ignored"); |
| 308 | strvec_push(&args, "--reverse"); |
| 309 | strvec_push(&args, "--topo-order"); |
| 310 | strvec_push(&args, "--full-history"); |
| 311 | |
| 312 | /* We only want to see commits that are descendants of the old commit. */ |
| 313 | strvec_pushf(&args, "--ancestry-path=%s", |
| 314 | oid_to_hex(&original->object.oid)); |
| 315 | |
| 316 | /* |
| 317 | * Ancestry path may also show ancestors of the old commit, but we |
| 318 | * don't want to see those, either. |
| 319 | */ |
| 320 | strvec_pushf(&args, "^%s", oid_to_hex(&original->object.oid)); |
| 321 | |
| 322 | /* |
| 323 | * When we're asked to update HEAD we need to verify that the commit |
| 324 | * that we want to rewrite is actually an ancestor of it and, if so, |
| 325 | * update it. Otherwise we'll update (or print) all descendant |
| 326 | * branches. |
| 327 | */ |
| 328 | if (action == REF_ACTION_HEAD) { |
| 329 | struct commit_list *from_list = NULL; |
| 330 | struct commit *head; |
| 331 | |
| 332 | head = lookup_commit_reference_by_name("HEAD"); |
| 333 | if (!head) { |
| 334 | ret = error(_("cannot look up HEAD")); |
| 335 | goto out; |
| 336 | } |
| 337 | |
| 338 | commit_list_insert(original, &from_list); |
| 339 | ret = repo_is_descendant_of(repo, head, from_list); |
| 340 | commit_list_free(from_list); |
| 341 | |
| 342 | if (ret < 0) { |
| 343 | ret = error(_("cannot determine descendance")); |
| 344 | goto out; |
| 345 | } else if (!ret) { |
| 346 | ret = error(_("rewritten commit must be an ancestor " |
| 347 | "of HEAD when using --update-refs=head")); |
| 348 | goto out; |
| 349 | } |
| 350 | |
| 351 | strvec_push(&args, "HEAD"); |
| 352 | } else { |
| 353 | strvec_push(&args, "--branches"); |
| 354 | strvec_push(&args, "HEAD"); |
| 355 | } |
| 356 | |
| 357 | ret = revwalk_contains_merges(repo, &args); |
| 358 | if (ret < 0) |
| 359 | goto out; |
| 360 | |
| 361 | setup_revisions_from_strvec(&args, revs, NULL); |
| 362 | if (args.nr != 1) |
| 363 | BUG("revisions were set up with invalid argument"); |
| 364 | |
| 365 | ret = 0; |
| 366 | |
| 367 | out: |
| 368 | strvec_clear(&args); |
| 369 | return ret; |
| 370 | } |
| 371 | |
| 372 | static int handle_ref_update(struct ref_transaction *transaction, |
| 373 | const char *refname, |
| 374 | const struct object_id *new_oid, |
| 375 | const struct object_id *old_oid, |
| 376 | const char *reflog_msg, |
| 377 | struct strbuf *err) |
| 378 | { |
| 379 | if (!transaction) { |
| 380 | printf("update %s %s %s\n", |
| 381 | refname, oid_to_hex(new_oid), oid_to_hex(old_oid)); |
| 382 | return 0; |
| 383 | } |
| 384 | |
| 385 | return ref_transaction_update(transaction, refname, new_oid, old_oid, |
| 386 | NULL, NULL, 0, reflog_msg, err); |
| 387 | } |
| 388 | |
| 389 | static int compute_pending_ref_updates(struct rev_info *revs, |
| 390 | enum ref_action action, |
| 391 | struct commit *original, |
| 392 | struct commit *rewritten, |
| 393 | const char *sign_commit, |
| 394 | enum replay_empty_commit_action empty, |
| 395 | struct replay_result *result) |
| 396 | { |
| 397 | const struct name_decoration *decoration; |
| 398 | struct replay_revisions_options opts = { |
| 399 | .empty = empty, |
| 400 | .sign_commit = sign_commit, |
| 401 | }; |
| 402 | char hex[GIT_MAX_HEXSZ + 1]; |
| 403 | bool detached_head; |
| 404 | int head_flags = 0; |
| 405 | int ret; |
| 406 | |
| 407 | refs_read_ref_full(get_main_ref_store(revs->repo), "HEAD", |
| 408 | RESOLVE_REF_NO_RECURSE, NULL, &head_flags); |
| 409 | detached_head = !(head_flags & REF_ISSYMREF); |
| 410 | |
| 411 | opts.onto = oid_to_hex_r(hex, &rewritten->object.oid); |
| 412 | |
| 413 | ret = replay_revisions(revs, &opts, result); |
| 414 | if (ret) |
| 415 | return ret; |
| 416 | |
| 417 | if (action != REF_ACTION_BRANCHES && action != REF_ACTION_HEAD) |
| 418 | BUG("unsupported ref action %d", action); |
| 419 | |
| 420 | /* |
| 421 | * `replay_revisions()` only updates references that are |
| 422 | * ancestors of `rewritten`, so we need to manually |
| 423 | * handle updating references that point to `original`. |
| 424 | */ |
| 425 | for (decoration = get_name_decoration(&original->object); |
| 426 | decoration; |
| 427 | decoration = decoration->next) |
| 428 | { |
| 429 | if (decoration->type != DECORATION_REF_LOCAL && |
| 430 | decoration->type != DECORATION_REF_HEAD) |
| 431 | continue; |
| 432 | |
| 433 | if (action == REF_ACTION_HEAD && |
| 434 | decoration->type != DECORATION_REF_HEAD) |
| 435 | continue; |
| 436 | |
| 437 | /* |
| 438 | * We only need to update HEAD separately in case it's |
| 439 | * detached. If it's not we'd already update the branch |
| 440 | * it is pointing to. |
| 441 | */ |
| 442 | if (action == REF_ACTION_BRANCHES && |
| 443 | decoration->type == DECORATION_REF_HEAD && |
| 444 | !detached_head) |
| 445 | continue; |
| 446 | |
| 447 | replay_result_queue_update(result, decoration->name, |
| 448 | &original->object.oid, |
| 449 | &rewritten->object.oid); |
| 450 | } |
| 451 | |
| 452 | return 0; |
| 453 | } |
| 454 | |
| 455 | static int apply_pending_ref_updates(struct repository *repo, |
| 456 | const struct replay_result *result, |
| 457 | const char *reflog_msg, |
| 458 | int dry_run) |
| 459 | { |
| 460 | struct ref_transaction *transaction = NULL; |
| 461 | struct strbuf err = STRBUF_INIT; |
| 462 | int ret; |
| 463 | |
| 464 | if (!dry_run) { |
| 465 | transaction = ref_store_transaction_begin(get_main_ref_store(repo), |
| 466 | 0, &err); |
| 467 | if (!transaction) { |
| 468 | ret = error(_("failed to begin ref transaction: %s"), err.buf); |
| 469 | goto out; |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | for (size_t i = 0; i < result->updates_nr; i++) { |
| 474 | ret = handle_ref_update(transaction, |
| 475 | result->updates[i].refname, |
| 476 | &result->updates[i].new_oid, |
| 477 | &result->updates[i].old_oid, |
| 478 | reflog_msg, &err); |
| 479 | if (ret) { |
| 480 | ret = error(_("failed to update ref '%s': %s"), |
| 481 | result->updates[i].refname, err.buf); |
| 482 | goto out; |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | if (transaction && ref_transaction_commit(transaction, &err)) { |
| 487 | ret = error(_("failed to commit ref transaction: %s"), err.buf); |
| 488 | goto out; |
| 489 | } |
| 490 | |
| 491 | ret = 0; |
| 492 | |
| 493 | out: |
| 494 | ref_transaction_free(transaction); |
| 495 | strbuf_release(&err); |
| 496 | return ret; |
| 497 | } |
| 498 | |
| 499 | static int handle_reference_updates(struct rev_info *revs, |
| 500 | enum ref_action action, |
| 501 | struct commit *original, |
| 502 | struct commit *rewritten, |
| 503 | const char *reflog_msg, |
| 504 | int dry_run, |
| 505 | const char *sign_commit, |
| 506 | enum replay_empty_commit_action empty) |
| 507 | { |
| 508 | struct replay_result result = { 0 }; |
| 509 | int ret; |
| 510 | |
| 511 | ret = compute_pending_ref_updates(revs, action, original, rewritten, |
| 512 | sign_commit, empty, &result); |
| 513 | if (ret) |
| 514 | goto out; |
| 515 | |
| 516 | ret = apply_pending_ref_updates(revs->repo, &result, reflog_msg, dry_run); |
| 517 | |
| 518 | out: |
| 519 | replay_result_release(&result); |
| 520 | return ret; |
| 521 | } |
| 522 | |
| 523 | static int commit_became_empty(struct repository *repo, |
| 524 | struct commit *original, |
| 525 | struct tree *result) |
| 526 | { |
| 527 | struct object_id parent_tree_oid; |
| 528 | |
| 529 | if (first_parent_tree_oid(repo, original, &parent_tree_oid) < 0) |
| 530 | return -1; |
| 531 | |
| 532 | return oideq(&result->object.oid, &parent_tree_oid); |
| 533 | } |
| 534 | |
| 535 | static int parse_opt_empty(const struct option *opt, const char *arg, int unset) |
| 536 | { |
| 537 | enum replay_empty_commit_action *value = opt->value; |
| 538 | |
| 539 | BUG_ON_OPT_NEG(unset); |
| 540 | |
| 541 | if (!strcmp(arg, "drop")) |
| 542 | *value = REPLAY_EMPTY_COMMIT_DROP; |
| 543 | else if (!strcmp(arg, "keep")) |
| 544 | *value = REPLAY_EMPTY_COMMIT_KEEP; |
| 545 | else if (!strcmp(arg, "abort")) |
| 546 | *value = REPLAY_EMPTY_COMMIT_ABORT; |
| 547 | else |
| 548 | die(_("unrecognized '--empty=' action '%s'; " |
| 549 | "valid values are \"drop\", \"keep\", and \"abort\"."), arg); |
| 550 | |
| 551 | return 0; |
| 552 | } |
| 553 | |
| 554 | static int cmd_history_fixup(int argc, |
| 555 | const char **argv, |
| 556 | const char *prefix, |
| 557 | struct repository *repo) |
| 558 | { |
| 559 | const char * const usage[] = { |
| 560 | GIT_HISTORY_FIXUP_USAGE, |
| 561 | NULL, |
| 562 | }; |
| 563 | enum replay_empty_commit_action empty = REPLAY_EMPTY_COMMIT_DROP; |
| 564 | enum ref_action action = REF_ACTION_DEFAULT; |
| 565 | enum commit_tree_flags flags = 0; |
| 566 | const char *sign_commit = NULL; |
| 567 | int dry_run = 0; |
| 568 | struct option options[] = { |
| 569 | OPT_CALLBACK_F(0, "update-refs", &action, "(branches|head)", |
| 570 | N_("control which refs should be updated"), |
| 571 | PARSE_OPT_NONEG, parse_ref_action), |
| 572 | OPT_BOOL('n', "dry-run", &dry_run, |
| 573 | N_("perform a dry-run without updating any refs")), |
| 574 | OPT_BIT(0, "reedit-message", &flags, |
| 575 | N_("open an editor to modify the commit message"), |
| 576 | COMMIT_TREE_EDIT_MESSAGE), |
| 577 | OPT_CALLBACK_F(0, "empty", &empty, "(drop|keep|abort)", |
| 578 | N_("how to handle commits that become empty"), |
| 579 | PARSE_OPT_NONEG, parse_opt_empty), |
| 580 | OPT_HISTORY_GPG_SIGN(&sign_commit), |
| 581 | OPT_END(), |
| 582 | }; |
| 583 | struct merge_result merge_result = { 0 }; |
| 584 | struct merge_options merge_opts = { 0 }; |
| 585 | struct strbuf reflog_msg = STRBUF_INIT; |
| 586 | struct commit *head_commit, *original, *rewritten; |
| 587 | struct tree *head_tree, *original_tree, *index_tree; |
| 588 | struct rev_info revs = { 0 }; |
| 589 | bool skip_commit = false; |
| 590 | int ret; |
| 591 | |
| 592 | repo_config(repo, history_config, &sign_commit); |
| 593 | argc = parse_options(argc, argv, prefix, options, usage, 0); |
| 594 | if (argc != 1) { |
| 595 | ret = error(_("command expects a single revision")); |
| 596 | goto out; |
| 597 | } |
| 598 | |
| 599 | if (action == REF_ACTION_DEFAULT) |
| 600 | action = REF_ACTION_BRANCHES; |
| 601 | |
| 602 | if (is_bare_repository(repo)) { |
| 603 | ret = error(_("cannot run fixup in a bare repository")); |
| 604 | goto out; |
| 605 | } |
| 606 | |
| 607 | /* Resolve the original commit, which is the one we want to fix up. */ |
| 608 | original = lookup_commit_reference_by_name(argv[0]); |
| 609 | if (!original) { |
| 610 | ret = error(_("commit cannot be found: %s"), argv[0]); |
| 611 | goto out; |
| 612 | } |
| 613 | |
| 614 | /* |
| 615 | * Resolve HEAD so we can use its tree as the merge base: the staged |
| 616 | * changes are expressed as a diff from HEAD's tree to the index tree. |
| 617 | */ |
| 618 | head_commit = lookup_commit_reference_by_name("HEAD"); |
| 619 | if (!head_commit) { |
| 620 | ret = error(_("cannot look up HEAD")); |
| 621 | goto out; |
| 622 | } |
| 623 | |
| 624 | head_tree = repo_get_commit_tree(repo, head_commit); |
| 625 | if (!head_tree) { |
| 626 | ret = error(_("cannot get tree for HEAD")); |
| 627 | goto out; |
| 628 | } |
| 629 | |
| 630 | if (repo_read_index(repo) < 0) { |
| 631 | ret = error(_("unable to read index")); |
| 632 | goto out; |
| 633 | } |
| 634 | |
| 635 | if (!repo_index_has_changes(repo, head_tree, NULL)) { |
| 636 | ret = error(_("nothing to fixup: no staged changes")); |
| 637 | goto out; |
| 638 | } |
| 639 | |
| 640 | /* |
| 641 | * Write the index as a tree object. This is the "theirs" side of the |
| 642 | * three-way merge: it is HEAD's tree with the staged changes applied. |
| 643 | */ |
| 644 | index_tree = write_in_core_index_as_tree(repo, repo->index); |
| 645 | if (!index_tree) { |
| 646 | ret = error(_("unable to write index as a tree")); |
| 647 | goto out; |
| 648 | } |
| 649 | |
| 650 | original_tree = repo_get_commit_tree(repo, original); |
| 651 | if (!original_tree) { |
| 652 | ret = error(_("cannot get tree for commit %s"), argv[0]); |
| 653 | goto out; |
| 654 | } |
| 655 | |
| 656 | /* |
| 657 | * Perform the three-way merge to reapply changes in the index onto the |
| 658 | * target commit. This is using basically the same logic as a |
| 659 | * cherry-pick, where the base commit is our HEAD, ours is the original |
| 660 | * tree and theirs is the index tree. |
| 661 | */ |
| 662 | init_basic_merge_options(&merge_opts, repo); |
| 663 | merge_opts.ancestor = "HEAD"; |
| 664 | merge_opts.branch1 = argv[0]; |
| 665 | merge_opts.branch2 = "staged"; |
| 666 | merge_incore_nonrecursive(&merge_opts, head_tree, |
| 667 | original_tree, index_tree, &merge_result); |
| 668 | |
| 669 | if (merge_result.clean < 0) { |
| 670 | ret = error(_("merge failed while applying fixup")); |
| 671 | goto out; |
| 672 | } |
| 673 | |
| 674 | if (!merge_result.clean) { |
| 675 | ret = error(_("fixup would produce conflicts; aborting")); |
| 676 | goto out; |
| 677 | } |
| 678 | |
| 679 | ret = commit_became_empty(repo, original, merge_result.tree); |
| 680 | if (ret < 0) |
| 681 | goto out; |
| 682 | if (ret > 0) { |
| 683 | switch (empty) { |
| 684 | case REPLAY_EMPTY_COMMIT_DROP: |
| 685 | /* |
| 686 | * Drop the target commit by replaying its descendants |
| 687 | * directly onto its parent. |
| 688 | */ |
| 689 | rewritten = original->parents ? original->parents->item : NULL; |
| 690 | |
| 691 | /* |
| 692 | * TODO: we don't yet have the ability to drop root |
| 693 | * commits, but there's ultimately no good reason for |
| 694 | * this restriction to exist other than a technical |
| 695 | * limitation. |
| 696 | */ |
| 697 | if (!rewritten) { |
| 698 | ret = error(_("cannot drop root commit %s: " |
| 699 | "it has no parent to replay onto"), |
| 700 | argv[0]); |
| 701 | goto out; |
| 702 | } |
| 703 | |
| 704 | skip_commit = true; |
| 705 | break; |
| 706 | case REPLAY_EMPTY_COMMIT_KEEP: |
| 707 | /* Proceed and record the empty commit. */ |
| 708 | break; |
| 709 | case REPLAY_EMPTY_COMMIT_ABORT: |
| 710 | ret = error(_("fixup makes commit %s empty"), argv[0]); |
| 711 | goto out; |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | ret = setup_revwalk(repo, action, original, &revs); |
| 716 | if (ret) |
| 717 | goto out; |
| 718 | |
| 719 | if (!skip_commit) { |
| 720 | ret = commit_tree_ext(repo, "fixup", original, NULL, original->parents, |
| 721 | &original_tree->object.oid, &merge_result.tree->object.oid, |
| 722 | sign_commit, &rewritten, flags); |
| 723 | if (ret < 0) { |
| 724 | ret = error(_("failed writing fixed-up commit")); |
| 725 | goto out; |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | strbuf_addf(&reflog_msg, "fixup: updating %s", argv[0]); |
| 730 | |
| 731 | ret = handle_reference_updates(&revs, action, original, rewritten, |
| 732 | reflog_msg.buf, dry_run, sign_commit, empty); |
| 733 | if (ret < 0) { |
| 734 | ret = error(_("failed replaying descendants")); |
| 735 | goto out; |
| 736 | } |
| 737 | |
| 738 | ret = 0; |
| 739 | |
| 740 | out: |
| 741 | merge_finalize(&merge_opts, &merge_result); |
| 742 | strbuf_release(&reflog_msg); |
| 743 | release_revisions(&revs); |
| 744 | return ret; |
| 745 | } |
| 746 | |
| 747 | static int cmd_history_reword(int argc, |
| 748 | const char **argv, |
| 749 | const char *prefix, |
| 750 | struct repository *repo) |
| 751 | { |
| 752 | const char * const usage[] = { |
| 753 | GIT_HISTORY_REWORD_USAGE, |
| 754 | NULL, |
| 755 | }; |
| 756 | enum ref_action action = REF_ACTION_DEFAULT; |
| 757 | const char *sign_commit = NULL; |
| 758 | int dry_run = 0; |
| 759 | struct option options[] = { |
| 760 | OPT_CALLBACK_F(0, "update-refs", &action, "(branches|head)", |
| 761 | N_("control which refs should be updated"), |
| 762 | PARSE_OPT_NONEG, parse_ref_action), |
| 763 | OPT_BOOL('n', "dry-run", &dry_run, |
| 764 | N_("perform a dry-run without updating any refs")), |
| 765 | OPT_HISTORY_GPG_SIGN(&sign_commit), |
| 766 | OPT_END(), |
| 767 | }; |
| 768 | struct strbuf reflog_msg = STRBUF_INIT; |
| 769 | struct commit *original, *rewritten; |
| 770 | struct rev_info revs = { 0 }; |
| 771 | int ret; |
| 772 | |
| 773 | repo_config(repo, history_config, &sign_commit); |
| 774 | argc = parse_options(argc, argv, prefix, options, usage, 0); |
| 775 | if (argc != 1) { |
| 776 | ret = error(_("command expects a single revision")); |
| 777 | goto out; |
| 778 | } |
| 779 | |
| 780 | if (action == REF_ACTION_DEFAULT) |
| 781 | action = REF_ACTION_BRANCHES; |
| 782 | |
| 783 | original = lookup_commit_reference_by_name(argv[0]); |
| 784 | if (!original) { |
| 785 | ret = error(_("commit cannot be found: %s"), argv[0]); |
| 786 | goto out; |
| 787 | } |
| 788 | |
| 789 | ret = setup_revwalk(repo, action, original, &revs); |
| 790 | if (ret) |
| 791 | goto out; |
| 792 | |
| 793 | ret = commit_tree_with_edited_message(repo, "reworded", original, |
| 794 | sign_commit, &rewritten); |
| 795 | if (ret < 0) { |
| 796 | ret = error(_("failed writing reworded commit")); |
| 797 | goto out; |
| 798 | } |
| 799 | |
| 800 | strbuf_addf(&reflog_msg, "reword: updating %s", argv[0]); |
| 801 | |
| 802 | ret = handle_reference_updates(&revs, action, original, rewritten, |
| 803 | reflog_msg.buf, dry_run, sign_commit, |
| 804 | REPLAY_EMPTY_COMMIT_ABORT); |
| 805 | if (ret < 0) { |
| 806 | ret = error(_("failed replaying descendants")); |
| 807 | goto out; |
| 808 | } |
| 809 | |
| 810 | ret = 0; |
| 811 | |
| 812 | out: |
| 813 | strbuf_release(&reflog_msg); |
| 814 | release_revisions(&revs); |
| 815 | return ret; |
| 816 | } |
| 817 | |
| 818 | static int write_ondisk_index(struct repository *repo, |
| 819 | struct object_id *oid, |
| 820 | const char *path) |
| 821 | { |
| 822 | struct unpack_trees_options opts = { 0 }; |
| 823 | struct lock_file lock = LOCK_INIT; |
| 824 | struct tree_desc tree_desc; |
| 825 | struct index_state index; |
| 826 | struct tree *tree; |
| 827 | int ret; |
| 828 | |
| 829 | index_state_init(&index, repo); |
| 830 | |
| 831 | opts.head_idx = -1; |
| 832 | opts.src_index = &index; |
| 833 | opts.dst_index = &index; |
| 834 | |
| 835 | tree = repo_parse_tree_indirect(repo, oid); |
| 836 | if (!tree) { |
| 837 | ret = error(_("unable to parse tree %s"), oid_to_hex(oid)); |
| 838 | goto out; |
| 839 | } |
| 840 | init_tree_desc(&tree_desc, &tree->object.oid, tree->buffer, tree->size); |
| 841 | |
| 842 | if (unpack_trees(1, &tree_desc, &opts)) { |
| 843 | ret = error(_("unable to populate index with tree")); |
| 844 | goto out; |
| 845 | } |
| 846 | |
| 847 | prime_cache_tree(repo, &index, tree); |
| 848 | |
| 849 | if (repo_hold_lock_file_for_update(repo, &lock, path, 0) < 0) { |
| 850 | ret = error_errno(_("unable to acquire index lock")); |
| 851 | goto out; |
| 852 | } |
| 853 | |
| 854 | if (write_locked_index(&index, &lock, COMMIT_LOCK)) { |
| 855 | ret = error(_("unable to write new index file")); |
| 856 | goto out; |
| 857 | } |
| 858 | |
| 859 | ret = 0; |
| 860 | |
| 861 | out: |
| 862 | rollback_lock_file(&lock); |
| 863 | release_index(&index); |
| 864 | return ret; |
| 865 | } |
| 866 | |
| 867 | static int split_commit(struct repository *repo, |
| 868 | struct commit *original, |
| 869 | struct pathspec *pathspec, |
| 870 | const char *sign_commit, |
| 871 | struct commit **out) |
| 872 | { |
| 873 | struct interactive_options interactive_opts = INTERACTIVE_OPTIONS_INIT; |
| 874 | struct strbuf index_file = STRBUF_INIT; |
| 875 | struct index_state index = INDEX_STATE_INIT(repo); |
| 876 | const struct object_id *original_commit_tree_oid; |
| 877 | const struct object_id *old_tree_oid, *new_tree_oid; |
| 878 | struct object_id parent_tree_oid; |
| 879 | char original_commit_oid[GIT_MAX_HEXSZ + 1]; |
| 880 | struct commit *first_commit, *second_commit; |
| 881 | struct commit_list *parents = NULL; |
| 882 | struct tree *split_tree; |
| 883 | int ret; |
| 884 | |
| 885 | if (first_parent_tree_oid(repo, original, &parent_tree_oid) < 0) { |
| 886 | ret = -1; |
| 887 | goto out; |
| 888 | } |
| 889 | original_commit_tree_oid = get_commit_tree_oid(original); |
| 890 | |
| 891 | /* |
| 892 | * Construct the first commit. This is done by taking the original |
| 893 | * commit parent's tree and selectively patching changes from the diff |
| 894 | * between that parent and its child. |
| 895 | */ |
| 896 | repo_git_path_replace(repo, &index_file, "%s", "history-split.index"); |
| 897 | |
| 898 | ret = write_ondisk_index(repo, &parent_tree_oid, index_file.buf); |
| 899 | if (ret < 0) |
| 900 | goto out; |
| 901 | |
| 902 | ret = read_index_from(&index, index_file.buf, repo->gitdir); |
| 903 | if (ret < 0) { |
| 904 | ret = error(_("failed reading temporary index")); |
| 905 | goto out; |
| 906 | } |
| 907 | |
| 908 | oid_to_hex_r(original_commit_oid, &original->object.oid); |
| 909 | ret = run_add_p_index(repo, &index, index_file.buf, &interactive_opts, |
| 910 | original_commit_oid, pathspec, ADD_P_DISALLOW_EDIT); |
| 911 | if (ret < 0) |
| 912 | goto out; |
| 913 | |
| 914 | split_tree = write_in_core_index_as_tree(repo, &index); |
| 915 | if (!split_tree) { |
| 916 | ret = error(_("failed split tree")); |
| 917 | goto out; |
| 918 | } |
| 919 | |
| 920 | unlink(index_file.buf); |
| 921 | strbuf_release(&index_file); |
| 922 | |
| 923 | /* |
| 924 | * We disallow the cases where either the split-out commit or the |
| 925 | * original commit would become empty. Consequently, if we see that the |
| 926 | * new tree ID matches either of those trees we abort. |
| 927 | */ |
| 928 | if (oideq(&split_tree->object.oid, &parent_tree_oid)) { |
| 929 | ret = error(_("split commit is empty")); |
| 930 | goto out; |
| 931 | } else if (oideq(&split_tree->object.oid, original_commit_tree_oid)) { |
| 932 | ret = error(_("split commit tree matches original commit")); |
| 933 | goto out; |
| 934 | } |
| 935 | |
| 936 | /* |
| 937 | * The first commit is constructed from the split-out tree. The base |
| 938 | * that shall be diffed against is the parent of the original commit. |
| 939 | */ |
| 940 | ret = commit_tree_ext(repo, "split-out", original, NULL, original->parents, |
| 941 | &parent_tree_oid, &split_tree->object.oid, sign_commit, |
| 942 | &first_commit, |
| 943 | COMMIT_TREE_EDIT_MESSAGE); |
| 944 | if (ret < 0) { |
| 945 | ret = error(_("failed writing first commit")); |
| 946 | goto out; |
| 947 | } |
| 948 | |
| 949 | /* |
| 950 | * The second commit is constructed from the original tree. The base to |
| 951 | * diff against and the parent in this case is the first split-out |
| 952 | * commit. |
| 953 | */ |
| 954 | commit_list_append(first_commit, &parents); |
| 955 | |
| 956 | old_tree_oid = &repo_get_commit_tree(repo, first_commit)->object.oid; |
| 957 | new_tree_oid = &repo_get_commit_tree(repo, original)->object.oid; |
| 958 | |
| 959 | ret = commit_tree_ext(repo, "split-out", original, NULL, parents, old_tree_oid, |
| 960 | new_tree_oid, sign_commit, &second_commit, |
| 961 | COMMIT_TREE_EDIT_MESSAGE); |
| 962 | if (ret < 0) { |
| 963 | ret = error(_("failed writing second commit")); |
| 964 | goto out; |
| 965 | } |
| 966 | |
| 967 | *out = second_commit; |
| 968 | ret = 0; |
| 969 | |
| 970 | out: |
| 971 | if (index_file.len) |
| 972 | unlink(index_file.buf); |
| 973 | strbuf_release(&index_file); |
| 974 | commit_list_free(parents); |
| 975 | release_index(&index); |
| 976 | return ret; |
| 977 | } |
| 978 | |
| 979 | static int cmd_history_split(int argc, |
| 980 | const char **argv, |
| 981 | const char *prefix, |
| 982 | struct repository *repo) |
| 983 | { |
| 984 | const char * const usage[] = { |
| 985 | GIT_HISTORY_SPLIT_USAGE, |
| 986 | NULL, |
| 987 | }; |
| 988 | enum ref_action action = REF_ACTION_DEFAULT; |
| 989 | const char *sign_commit = NULL; |
| 990 | int dry_run = 0; |
| 991 | struct option options[] = { |
| 992 | OPT_CALLBACK_F(0, "update-refs", &action, "(branches|head)", |
| 993 | N_("control ref update behavior"), |
| 994 | PARSE_OPT_NONEG, parse_ref_action), |
| 995 | OPT_BOOL('n', "dry-run", &dry_run, |
| 996 | N_("perform a dry-run without updating any refs")), |
| 997 | OPT_HISTORY_GPG_SIGN(&sign_commit), |
| 998 | OPT_END(), |
| 999 | }; |
| 1000 | struct commit *original, *rewritten = NULL; |
| 1001 | struct strbuf reflog_msg = STRBUF_INIT; |
| 1002 | struct pathspec pathspec = { 0 }; |
| 1003 | struct rev_info revs = { 0 }; |
| 1004 | int ret; |
| 1005 | |
| 1006 | repo_config(repo, history_config, &sign_commit); |
| 1007 | argc = parse_options(argc, argv, prefix, options, usage, 0); |
| 1008 | if (argc < 1) { |
| 1009 | ret = error(_("command expects a committish")); |
| 1010 | goto out; |
| 1011 | } |
| 1012 | |
| 1013 | if (action == REF_ACTION_DEFAULT) |
| 1014 | action = REF_ACTION_BRANCHES; |
| 1015 | |
| 1016 | parse_pathspec(&pathspec, 0, |
| 1017 | PATHSPEC_PREFER_FULL | |
| 1018 | PATHSPEC_SYMLINK_LEADING_PATH | |
| 1019 | PATHSPEC_PREFIX_ORIGIN, |
| 1020 | prefix, argv + 1); |
| 1021 | |
| 1022 | original = lookup_commit_reference_by_name(argv[0]); |
| 1023 | if (!original) { |
| 1024 | ret = error(_("commit cannot be found: %s"), argv[0]); |
| 1025 | goto out; |
| 1026 | } |
| 1027 | |
| 1028 | ret = setup_revwalk(repo, action, original, &revs); |
| 1029 | if (ret < 0) |
| 1030 | goto out; |
| 1031 | |
| 1032 | if (original->parents && original->parents->next) { |
| 1033 | ret = error(_("cannot split up merge commit")); |
| 1034 | goto out; |
| 1035 | } |
| 1036 | |
| 1037 | ret = split_commit(repo, original, &pathspec, sign_commit, &rewritten); |
| 1038 | if (ret < 0) |
| 1039 | goto out; |
| 1040 | |
| 1041 | strbuf_addf(&reflog_msg, "split: updating %s", argv[0]); |
| 1042 | |
| 1043 | ret = handle_reference_updates(&revs, action, original, rewritten, |
| 1044 | reflog_msg.buf, dry_run, sign_commit, |
| 1045 | REPLAY_EMPTY_COMMIT_ABORT); |
| 1046 | if (ret < 0) { |
| 1047 | ret = error(_("failed replaying descendants")); |
| 1048 | goto out; |
| 1049 | } |
| 1050 | |
| 1051 | ret = 0; |
| 1052 | |
| 1053 | out: |
| 1054 | strbuf_release(&reflog_msg); |
| 1055 | clear_pathspec(&pathspec); |
| 1056 | release_revisions(&revs); |
| 1057 | return ret; |
| 1058 | } |
| 1059 | |
| 1060 | #define SQUASH_SEEN (1u << 11) |
| 1061 | #define SQUASH_TIP (1u << 12) |
| 1062 | #define SQUASH_AMEND_TARGET (1u << 13) |
| 1063 | |
| 1064 | static bool is_autosquash_subject(const char *s) |
| 1065 | { |
| 1066 | return starts_with(s, "amend!") || starts_with(s, "fixup!") || |
| 1067 | starts_with(s, "squash!"); |
| 1068 | } |
| 1069 | |
| 1070 | static bool skip_one_autosquash_prefix(const char *s, const char **out) |
| 1071 | { |
| 1072 | if (skip_prefix(s, "amend!", out) || skip_prefix(s, "fixup!", out) || |
| 1073 | skip_prefix(s, "squash!", out)) { |
| 1074 | while (**out == ' ') |
| 1075 | (*out)++; |
| 1076 | return true; |
| 1077 | } |
| 1078 | return false; |
| 1079 | } |
| 1080 | |
| 1081 | static void truncate_message_to_subject(struct strbuf *msg) |
| 1082 | { |
| 1083 | const char *eos = strstr(msg->buf, "\n\n"); |
| 1084 | |
| 1085 | if (eos) |
| 1086 | strbuf_setlen(msg, eos - msg->buf + 1); |
| 1087 | } |
| 1088 | |
| 1089 | struct subject_data { |
| 1090 | struct strintmap subjects; |
| 1091 | struct strbuf subject; |
| 1092 | struct strbuf squash_message; |
| 1093 | const char *message; |
| 1094 | bool edit_message; |
| 1095 | }; |
| 1096 | |
| 1097 | #define SUBJECT_DATA_INIT { \ |
| 1098 | .subjects = STRINTMAP_INIT, \ |
| 1099 | .subject = STRBUF_INIT, \ |
| 1100 | .squash_message = STRBUF_INIT, \ |
| 1101 | } |
| 1102 | |
| 1103 | static void subject_data_clear(struct subject_data *data) |
| 1104 | { |
| 1105 | strintmap_clear(&data->subjects); |
| 1106 | strbuf_release(&data->subject); |
| 1107 | strbuf_release(&data->squash_message); |
| 1108 | } |
| 1109 | |
| 1110 | static int squash_amend_message(struct repository *repo, |
| 1111 | struct commit *commit, |
| 1112 | struct subject_data *data, |
| 1113 | unsigned flags) |
| 1114 | { |
| 1115 | const char *body = data->message + data->subject.len; |
| 1116 | |
| 1117 | while (isspace(*body)) |
| 1118 | body++; |
| 1119 | |
| 1120 | if (!*body) { |
| 1121 | warning(_("ignoring %s (%s): message body is empty"), |
| 1122 | repo_find_unique_abbrev(repo, &commit->object.oid, |
| 1123 | DEFAULT_ABBREV), |
| 1124 | data->subject.buf); |
| 1125 | return 0; |
| 1126 | } |
| 1127 | |
| 1128 | if (data->edit_message) { |
| 1129 | return 0; |
| 1130 | } else if (flags & SQUASH_AMEND_TARGET) { |
| 1131 | if (starts_with(data->squash_message.buf, "squash!")) |
| 1132 | return error(_("squashing %s (%s) would overwrite " |
| 1133 | "'squash!' message, please combine them " |
| 1134 | "using '--edit'"), |
| 1135 | repo_find_unique_abbrev(repo, |
| 1136 | &commit->object.oid, |
| 1137 | DEFAULT_ABBREV), |
| 1138 | data->subject.buf); |
| 1139 | if (starts_with(data->squash_message.buf, "fixup!")) |
| 1140 | strbuf_splice(&data->squash_message, 0, 5, "amend!", 5); |
| 1141 | if (starts_with(data->squash_message.buf, "amend!")) { |
| 1142 | truncate_message_to_subject(&data->squash_message); |
| 1143 | strbuf_addch(&data->squash_message, '\n'); |
| 1144 | } else { |
| 1145 | strbuf_reset(&data->squash_message); |
| 1146 | } |
| 1147 | strbuf_addstr(&data->squash_message, body); |
| 1148 | strbuf_complete_line(&data->squash_message); |
| 1149 | } else { |
| 1150 | return error(_("cannot squash %s (%s) that does not target " |
| 1151 | "base commit without '--edit'"), |
| 1152 | repo_find_unique_abbrev(repo, &commit->object.oid, |
| 1153 | DEFAULT_ABBREV), |
| 1154 | data->subject.buf); |
| 1155 | } |
| 1156 | return 0; |
| 1157 | } |
| 1158 | |
| 1159 | static int squash_squash_message(struct repository *repo, |
| 1160 | struct commit *commit, |
| 1161 | struct subject_data *data, |
| 1162 | unsigned flags) |
| 1163 | { |
| 1164 | const char *body = data->message + data->subject.len; |
| 1165 | |
| 1166 | while (isspace(*body)) |
| 1167 | body++; |
| 1168 | |
| 1169 | if (data->edit_message) { |
| 1170 | return 0; |
| 1171 | } else if (flags & SQUASH_AMEND_TARGET) { |
| 1172 | if (starts_with(data->squash_message.buf, "fixup!")) { |
| 1173 | truncate_message_to_subject(&data->squash_message); |
| 1174 | strbuf_splice(&data->squash_message, 0, 5, "squash", 6); |
| 1175 | } |
| 1176 | if (starts_with(data->squash_message.buf, "squash!")) { |
| 1177 | strbuf_addch(&data->squash_message, '\n'); |
| 1178 | strbuf_addstr(&data->squash_message, body); |
| 1179 | strbuf_complete_line(&data->squash_message); |
| 1180 | } else { |
| 1181 | return error(_("squashing %s (%s) would discard its " |
| 1182 | "message, please combine them using " |
| 1183 | "'--edit'"), |
| 1184 | repo_find_unique_abbrev(repo, |
| 1185 | &commit->object.oid, |
| 1186 | DEFAULT_ABBREV), |
| 1187 | data->subject.buf); |
| 1188 | } |
| 1189 | } else { |
| 1190 | return error(_("cannot squash %s (%s) that does not target " |
| 1191 | "base commit without '--edit'"), |
| 1192 | repo_find_unique_abbrev(repo, &commit->object.oid, |
| 1193 | DEFAULT_ABBREV), |
| 1194 | data->subject.buf); |
| 1195 | } |
| 1196 | return 0; |
| 1197 | } |
| 1198 | |
| 1199 | static int squash_check_can_autosquash(struct repository *repo, |
| 1200 | struct commit *commit, |
| 1201 | struct subject_data *data, |
| 1202 | unsigned flags) |
| 1203 | { |
| 1204 | commit->object.flags |= flags & SQUASH_AMEND_TARGET; |
| 1205 | if (starts_with(data->subject.buf, "amend!")) |
| 1206 | return squash_amend_message(repo, commit, data, flags); |
| 1207 | else if (starts_with(data->subject.buf, "squash!")) |
| 1208 | return squash_squash_message(repo, commit, data, flags); |
| 1209 | |
| 1210 | return 0; |
| 1211 | } |
| 1212 | |
| 1213 | static int squash_check_autosquash_subject(struct repository *repo, |
| 1214 | struct commit *commit, |
| 1215 | struct subject_data *data) |
| 1216 | { |
| 1217 | const char* s = data->subject.buf; |
| 1218 | struct commit *target; |
| 1219 | struct hashmap_iter iter; |
| 1220 | struct strmap_entry *entry; |
| 1221 | /* Try skipping autosquash prefixes one at a time to allow |
| 1222 | * squashing |
| 1223 | * a commit |
| 1224 | * fixup! fixup! a commit |
| 1225 | * |
| 1226 | * where we may have started with |
| 1227 | * a commit |
| 1228 | * fixup! a commit |
| 1229 | * fixup! fixup! a commit |
| 1230 | * |
| 1231 | * and squashed the first fixup separately from the second |
| 1232 | */ |
| 1233 | while (skip_one_autosquash_prefix(s, &s)) { |
| 1234 | unsigned flags = strintmap_get(&data->subjects, s); |
| 1235 | if (flags) |
| 1236 | return squash_check_can_autosquash(repo, commit, data, flags); |
| 1237 | } |
| 1238 | /* |
| 1239 | * Allow "fixup! <hex object id>", but not "fixup! HEAD^" or |
| 1240 | * "fixup! main". If the target is not being squashed check the subject |
| 1241 | * to allow "fixup! abc123" and "fixup! <subject of abc123>" to be |
| 1242 | * squashed together. |
| 1243 | */ |
| 1244 | target = lookup_commit_reference_by_name(s); |
| 1245 | if (target && starts_with(oid_to_hex(&target->object.oid), s)) { |
| 1246 | unsigned flags = |
| 1247 | target->object.flags & (SQUASH_SEEN | SQUASH_AMEND_TARGET); |
| 1248 | if (!flags) { |
| 1249 | const char *subject_start; |
| 1250 | const char *buffer = repo_logmsg_reencode(repo, target, |
| 1251 | NULL, NULL); |
| 1252 | size_t subject_len = find_commit_subject(buffer, |
| 1253 | &subject_start); |
| 1254 | char *subject = xmemdupz(subject_start, subject_len); |
| 1255 | |
| 1256 | flags = strintmap_get(&data->subjects, subject); |
| 1257 | free(subject); |
| 1258 | repo_unuse_commit_buffer(repo, target, buffer); |
| 1259 | } |
| 1260 | if (flags) |
| 1261 | return squash_check_can_autosquash(repo, commit, |
| 1262 | data, flags); |
| 1263 | } |
| 1264 | /* Try subject prefix matches */ |
| 1265 | strintmap_for_each_entry(&data->subjects, &iter, entry) { |
| 1266 | s = data->subject.buf; |
| 1267 | while(skip_one_autosquash_prefix(s, &s)) { |
| 1268 | if (starts_with(entry->key, s)) { |
| 1269 | unsigned value = (intptr_t)entry->value; |
| 1270 | |
| 1271 | return squash_check_can_autosquash(repo, commit, |
| 1272 | data, value); |
| 1273 | } |
| 1274 | } |
| 1275 | } |
| 1276 | return error(_("cannot squash %s (%s): its target is not being " |
| 1277 | "squashed"), |
| 1278 | repo_find_unique_abbrev(repo, &commit->object.oid, |
| 1279 | DEFAULT_ABBREV), |
| 1280 | data->subject.buf); |
| 1281 | } |
| 1282 | |
| 1283 | static int squash_check_subject(struct repository *repo, |
| 1284 | struct commit *commit, |
| 1285 | struct subject_data *data) |
| 1286 | { |
| 1287 | int ret = 0; |
| 1288 | const char *buf = repo_logmsg_reencode(repo, commit, NULL, NULL); |
| 1289 | size_t subject_len = find_commit_subject(buf, &data->message); |
| 1290 | |
| 1291 | strbuf_reset(&data->subject); |
| 1292 | strbuf_add(&data->subject, data->message, subject_len); |
| 1293 | |
| 1294 | if (!strintmap_get_size(&data->subjects)) { |
| 1295 | const char *s; |
| 1296 | |
| 1297 | strbuf_addstr(&data->squash_message, data->message); |
| 1298 | strbuf_complete_line(&data->squash_message); |
| 1299 | /* |
| 1300 | * Strip a single autosquash prefix to allow squashing |
| 1301 | * fixup! base |
| 1302 | * amend! base |
| 1303 | */ |
| 1304 | s = data->subject.buf; |
| 1305 | skip_one_autosquash_prefix(s, &s); |
| 1306 | strintmap_set(&data->subjects, s, SQUASH_AMEND_TARGET | SQUASH_SEEN); |
| 1307 | commit->object.flags |= SQUASH_AMEND_TARGET; |
| 1308 | } else if (is_autosquash_subject(data->subject.buf)) { |
| 1309 | ret = squash_check_autosquash_subject(repo, commit, data); |
| 1310 | } else { |
| 1311 | strintmap_set(&data->subjects, data->subject.buf, SQUASH_SEEN); |
| 1312 | } |
| 1313 | repo_unuse_commit_buffer(repo, commit, buf); |
| 1314 | return ret; |
| 1315 | } |
| 1316 | |
| 1317 | static int build_squash_message(struct repository *repo, |
| 1318 | const struct strbuf *todo_buf, |
| 1319 | struct strbuf *out); |
| 1320 | |
| 1321 | static int setup_squash_revisions(struct repository *repo, |
| 1322 | int argc, const char **argv, |
| 1323 | struct rev_info *revs) |
| 1324 | { |
| 1325 | repo_init_revisions(repo, revs, NULL); |
| 1326 | revs->reverse = 1; |
| 1327 | revs->topo_order = 1; |
| 1328 | revs->sort_order = REV_SORT_IN_GRAPH_ORDER; |
| 1329 | revs->simplify_history = 0; |
| 1330 | revs->ancestry_path = 1; |
| 1331 | revs->limited = 1; |
| 1332 | revs->ancestry_path_implicit_bottoms = 1; |
| 1333 | |
| 1334 | argc = setup_revisions(argc, argv, revs, NULL); |
| 1335 | if (argc > 1) |
| 1336 | return error(_("unrecognized argument: %s"), argv[1]); |
| 1337 | |
| 1338 | if (revs->reverse != 1 || revs->topo_order != 1 || |
| 1339 | revs->sort_order != REV_SORT_IN_GRAPH_ORDER || |
| 1340 | revs->simplify_history != 0 || revs->boundary == 1 || |
| 1341 | revs->ancestry_path != 1 || revs->limited != 1 || |
| 1342 | revs->ancestry_path_implicit_bottoms != 1) { |
| 1343 | warning(_("ignoring rev-list options that would change how the " |
| 1344 | "range is walked")); |
| 1345 | revs->reverse = 1; |
| 1346 | revs->topo_order = 1; |
| 1347 | revs->sort_order = REV_SORT_IN_GRAPH_ORDER; |
| 1348 | revs->simplify_history = 0; |
| 1349 | revs->boundary = 0; |
| 1350 | revs->ancestry_path = 1; |
| 1351 | revs->limited = 1; |
| 1352 | revs->ancestry_path_implicit_bottoms = 1; |
| 1353 | } |
| 1354 | |
| 1355 | /* |
| 1356 | * A squash needs a base to reparent onto, so the range has to exclude |
| 1357 | * something, as in "<base>..<tip>". A revision range with no such |
| 1358 | * bottom commit cannot be squashed. |
| 1359 | */ |
| 1360 | for (size_t i = 0; i < revs->cmdline.nr; i++) |
| 1361 | if (revs->cmdline.rev[i].flags & BOTTOM) |
| 1362 | return 0; |
| 1363 | |
| 1364 | return error(_("not a '<base>..<tip>' revision range")); |
| 1365 | } |
| 1366 | |
| 1367 | /* |
| 1368 | * Resolve a revision range into its oldest commit and single tip. Every |
| 1369 | * parent after the oldest commit must either be selected or also be a parent |
| 1370 | * of the oldest commit. |
| 1371 | */ |
| 1372 | static int resolve_squash_range(struct repository *repo, |
| 1373 | bool update_branches, |
| 1374 | bool edit_message, |
| 1375 | int argc, const char **argv, |
| 1376 | struct commit **oldest_out, |
| 1377 | struct commit **tip_out, |
| 1378 | char **message_out) |
| 1379 | { |
| 1380 | struct rev_info revs; |
| 1381 | struct subject_data subject_data = SUBJECT_DATA_INIT; |
| 1382 | struct commit *commit, *oldest = NULL, *tip = NULL; |
| 1383 | struct strbuf todo_buf = STRBUF_INIT; |
| 1384 | int ret, tip_count = 0; |
| 1385 | bool walk_started = false; |
| 1386 | struct ref_filter filter = REF_FILTER_INIT; |
| 1387 | struct ref_array refs = { 0 }; |
| 1388 | |
| 1389 | subject_data.edit_message = edit_message; |
| 1390 | ret = setup_squash_revisions(repo, argc, argv, &revs); |
| 1391 | if (ret < 0) |
| 1392 | goto out; |
| 1393 | |
| 1394 | if (prepare_revision_walk(&revs) < 0) { |
| 1395 | ret = error(_("error preparing revisions")); |
| 1396 | goto out; |
| 1397 | } |
| 1398 | walk_started = true; |
| 1399 | while ((commit = get_revision(&revs))) { |
| 1400 | struct commit_list *p; |
| 1401 | |
| 1402 | if (edit_message) |
| 1403 | strbuf_addf(&todo_buf, "pick %s\n", |
| 1404 | oid_to_hex(&commit->object.oid)); |
| 1405 | |
| 1406 | if (!commit->parents) { |
| 1407 | ret = error(_("cannot squash down to root commit")); |
| 1408 | goto out; |
| 1409 | } |
| 1410 | for (p = commit->parents; oldest && p; p = p->next) { |
| 1411 | struct commit_list *q; |
| 1412 | struct object *o; |
| 1413 | bool seen; |
| 1414 | |
| 1415 | if (repo_parse_commit(repo, p->item)) { |
| 1416 | ret = error(_("cannot parse commit")); |
| 1417 | goto out; |
| 1418 | } |
| 1419 | o = &p->item->object; |
| 1420 | seen = o->flags & SQUASH_SEEN; |
| 1421 | /* |
| 1422 | * Allow parents that match the parents of the |
| 1423 | * squashed commit. |
| 1424 | */ |
| 1425 | for (q = oldest->parents; !seen && q; q = q->next) { |
| 1426 | if (p->item == q->item) { |
| 1427 | seen = true; |
| 1428 | commit_list_insert(commit, &filter.with_commit); |
| 1429 | } |
| 1430 | } |
| 1431 | if (!seen) { |
| 1432 | ret = error(_("parent %s of commit %s is " |
| 1433 | "outside the revision range"), |
| 1434 | repo_find_unique_abbrev(repo, &o->oid, |
| 1435 | DEFAULT_ABBREV), |
| 1436 | repo_find_unique_abbrev(repo, |
| 1437 | &commit->object.oid, |
| 1438 | DEFAULT_ABBREV)); |
| 1439 | goto out; |
| 1440 | } |
| 1441 | if (o->flags & SQUASH_TIP) { |
| 1442 | tip_count--; |
| 1443 | o->flags &= ~SQUASH_TIP; |
| 1444 | } |
| 1445 | } |
| 1446 | if (!oldest) { |
| 1447 | commit_list_insert(commit, &filter.with_commit); |
| 1448 | oldest = commit; |
| 1449 | } |
| 1450 | if (squash_check_subject(repo, commit, &subject_data)) { |
| 1451 | ret = -1; |
| 1452 | goto out; |
| 1453 | } |
| 1454 | tip = commit; |
| 1455 | tip->object.flags |= SQUASH_SEEN | SQUASH_TIP; |
| 1456 | tip_count++; |
| 1457 | } |
| 1458 | clear_object_flags(repo, SQUASH_SEEN | SQUASH_TIP); |
| 1459 | reset_revision_walk(); |
| 1460 | walk_started = false; |
| 1461 | |
| 1462 | if (!tip_count) { |
| 1463 | ret = error(_("the revision range is empty")); |
| 1464 | goto out; |
| 1465 | } else if (tip_count != 1) { |
| 1466 | ret = error(_("the revision range contains more than one tip " |
| 1467 | "commit")); |
| 1468 | goto out; |
| 1469 | } else if (oldest == tip) { |
| 1470 | ret = error(_("the revision range holds a single commit; " |
| 1471 | "nothing to squash")); |
| 1472 | goto out; |
| 1473 | } else if (!oldest->parents) { |
| 1474 | BUG("an in-range commit must have a parent"); |
| 1475 | } |
| 1476 | |
| 1477 | commit_list_insert(tip, &filter.no_commit); |
| 1478 | filter.kind = FILTER_REFS_BRANCHES; |
| 1479 | if (update_branches && |
| 1480 | filter_refs(&refs, &filter, filter.kind)) { |
| 1481 | ret = error(_("could not filter refs")); |
| 1482 | goto out; |
| 1483 | } |
| 1484 | if (refs.nr) { |
| 1485 | struct ref_format format = REF_FORMAT_INIT; |
| 1486 | struct ref_sorting *sorting; |
| 1487 | struct string_list sorting_options = STRING_LIST_INIT_DUP; |
| 1488 | struct strbuf branches = STRBUF_INIT; |
| 1489 | struct strbuf err = STRBUF_INIT; |
| 1490 | |
| 1491 | format.format = "%(refname:short)"; |
| 1492 | if (verify_ref_format(&format)) |
| 1493 | BUG("invalid branch format"); |
| 1494 | string_list_append(&sorting_options, "refname"); |
| 1495 | sorting = ref_sorting_options(&sorting_options); |
| 1496 | ref_array_sort(sorting, &refs); |
| 1497 | for (int i = 0; i < refs.nr; i++) { |
| 1498 | strbuf_reset(&err); |
| 1499 | strbuf_addstr(&branches, "\n "); |
| 1500 | if (format_ref_array_item(refs.items[i], &format, |
| 1501 | &branches, &err)) |
| 1502 | BUG("could not format branch name: %s", err.buf); |
| 1503 | } |
| 1504 | /* |
| 1505 | * TODO: also check HEADS from other worktrees. |
| 1506 | */ |
| 1507 | ret = error(_("the following branches cannot be rewritten as " |
| 1508 | "descendants of the squashed commit:%s"), branches.buf); |
| 1509 | advise_if_enabled(ADVICE_HISTORY_UPDATE_REFS, |
| 1510 | _("Use --update-refs=head to rewrite only " |
| 1511 | "the current branch and leave such branches " |
| 1512 | "untouched.")); |
| 1513 | strbuf_release(&err); |
| 1514 | strbuf_release(&branches); |
| 1515 | ref_sorting_release(sorting); |
| 1516 | string_list_clear(&sorting_options, 0); |
| 1517 | goto out; |
| 1518 | } |
| 1519 | if (edit_message) { |
| 1520 | strbuf_reset(&subject_data.squash_message); |
| 1521 | ret = build_squash_message(repo, &todo_buf, |
| 1522 | &subject_data.squash_message); |
| 1523 | if (ret < 0) |
| 1524 | goto out; |
| 1525 | } |
| 1526 | |
| 1527 | *oldest_out = oldest; |
| 1528 | *tip_out = tip; |
| 1529 | *message_out = strbuf_detach(&subject_data.squash_message, NULL); |
| 1530 | ret = 0; |
| 1531 | |
| 1532 | out: |
| 1533 | strbuf_release(&todo_buf); |
| 1534 | clear_object_flags(repo, SQUASH_SEEN | SQUASH_TIP | |
| 1535 | SQUASH_AMEND_TARGET); |
| 1536 | if (walk_started) |
| 1537 | reset_revision_walk(); |
| 1538 | subject_data_clear(&subject_data); |
| 1539 | release_revisions(&revs); |
| 1540 | ref_filter_clear(&filter); |
| 1541 | ref_array_clear(&refs); |
| 1542 | return ret; |
| 1543 | } |
| 1544 | |
| 1545 | static bool amend_replaces_target(struct todo_list *todo, int target) |
| 1546 | { |
| 1547 | for (int i = target + 1; i < todo->nr && |
| 1548 | todo->items[i].command != TODO_PICK; i++) { |
| 1549 | if (todo->items[i].command == TODO_SQUASH) |
| 1550 | return false; |
| 1551 | if (todo->items[i].flags & TODO_REPLACE_FIXUP_MSG) |
| 1552 | return true; |
| 1553 | } |
| 1554 | return false; |
| 1555 | } |
| 1556 | |
| 1557 | static int build_squash_message(struct repository *repo, |
| 1558 | const struct strbuf *todo_buf, |
| 1559 | struct strbuf *out) |
| 1560 | { |
| 1561 | struct todo_list todo = TODO_LIST_INIT; |
| 1562 | struct replay_opts opts = REPLAY_OPTS_INIT; |
| 1563 | int nr_commits, ret; |
| 1564 | |
| 1565 | if (todo_list_parse_insn_buffer(repo, &opts, todo_buf->buf, &todo) < 0 || |
| 1566 | todo_list_rearrange_squash(&todo) < 0) { |
| 1567 | ret = error(_("could not prepare the squash message")); |
| 1568 | goto out; |
| 1569 | } |
| 1570 | |
| 1571 | nr_commits = todo.nr; |
| 1572 | for (int i = 0; i < nr_commits; i++) { |
| 1573 | struct todo_item *item = &todo.items[i]; |
| 1574 | const char *message, *body; |
| 1575 | size_t commented_len; |
| 1576 | bool skip, squashing; |
| 1577 | |
| 1578 | squashing = item->command == TODO_SQUASH || |
| 1579 | (item->flags & TODO_REPLACE_FIXUP_MSG); |
| 1580 | if (item->command == TODO_PICK) |
| 1581 | skip = amend_replaces_target(&todo, i); |
| 1582 | else |
| 1583 | skip = !squashing; |
| 1584 | |
| 1585 | message = repo_logmsg_reencode(repo, item->commit, NULL, NULL); |
| 1586 | find_commit_subject(message, &body); |
| 1587 | |
| 1588 | if (skip) |
| 1589 | commented_len = strlen(body); |
| 1590 | else if (squashing) |
| 1591 | commented_len = squash_subject_comment_len(body, 1); |
| 1592 | else |
| 1593 | commented_len = 0; |
| 1594 | |
| 1595 | if (!i) |
| 1596 | add_squash_combination_header(out, nr_commits); |
| 1597 | strbuf_addch(out, '\n'); |
| 1598 | add_squash_message_header(out, i + 1, skip); |
| 1599 | strbuf_addstr(out, "\n\n"); |
| 1600 | strbuf_add_commented_lines(out, body, commented_len, comment_line_str); |
| 1601 | strbuf_addstr(out, body + commented_len); |
| 1602 | strbuf_complete_line(out); |
| 1603 | |
| 1604 | repo_unuse_commit_buffer(repo, item->commit, message); |
| 1605 | } |
| 1606 | |
| 1607 | ret = 0; |
| 1608 | |
| 1609 | out: |
| 1610 | todo_list_release(&todo); |
| 1611 | replay_opts_release(&opts); |
| 1612 | return ret; |
| 1613 | } |
| 1614 | |
| 1615 | static int cmd_history_squash(int argc, |
| 1616 | const char **argv, |
| 1617 | const char *prefix, |
| 1618 | struct repository *repo) |
| 1619 | { |
| 1620 | const char * const usage[] = { |
| 1621 | GIT_HISTORY_SQUASH_USAGE, |
| 1622 | NULL, |
| 1623 | }; |
| 1624 | enum ref_action action = REF_ACTION_DEFAULT; |
| 1625 | const char *sign_commit = NULL; |
| 1626 | int dry_run = 0; |
| 1627 | int edit = 1; |
| 1628 | struct option options[] = { |
| 1629 | OPT_CALLBACK_F(0, "update-refs", &action, "(branches|head)", |
| 1630 | N_("control which refs should be updated"), |
| 1631 | PARSE_OPT_NONEG, parse_ref_action), |
| 1632 | OPT_BOOL('n', "dry-run", &dry_run, |
| 1633 | N_("perform a dry-run without updating any refs")), |
| 1634 | OPT_BOOL('e', "edit", &edit, |
| 1635 | N_("edit the commit message")), |
| 1636 | OPT_HISTORY_GPG_SIGN(&sign_commit), |
| 1637 | OPT_END(), |
| 1638 | }; |
| 1639 | struct strbuf reflog_msg = STRBUF_INIT; |
| 1640 | struct commit *oldest, *tip, *rewritten; |
| 1641 | const struct object_id *base_tree_oid, *tip_tree_oid; |
| 1642 | char *message_template = NULL; |
| 1643 | struct rev_info revs = { 0 }; |
| 1644 | int ret; |
| 1645 | |
| 1646 | repo_config(repo, history_config, &sign_commit); |
| 1647 | argc = parse_options(argc, argv, prefix, options, usage, |
| 1648 | PARSE_OPT_KEEP_UNKNOWN_OPT | PARSE_OPT_KEEP_ARGV0); |
| 1649 | if (argc < 2) { |
| 1650 | ret = error(_("command expects a revision range")); |
| 1651 | goto out; |
| 1652 | } |
| 1653 | |
| 1654 | if (action == REF_ACTION_DEFAULT) |
| 1655 | action = REF_ACTION_BRANCHES; |
| 1656 | |
| 1657 | strbuf_addstr(&reflog_msg, "squash: updating "); |
| 1658 | strbuf_join_argv(&reflog_msg, argc - 1, argv + 1, ' '); |
| 1659 | |
| 1660 | ret = resolve_squash_range(repo, action == REF_ACTION_BRANCHES, |
| 1661 | edit, |
| 1662 | argc, argv, &oldest, &tip, |
| 1663 | &message_template); |
| 1664 | if (ret < 0) |
| 1665 | goto out; |
| 1666 | |
| 1667 | ret = setup_revwalk(repo, action, tip, &revs); |
| 1668 | if (ret < 0) |
| 1669 | goto out; |
| 1670 | |
| 1671 | base_tree_oid = &repo_get_commit_tree(repo, |
| 1672 | oldest->parents->item)->object.oid; |
| 1673 | tip_tree_oid = &repo_get_commit_tree(repo, tip)->object.oid; |
| 1674 | |
| 1675 | ret = commit_tree_ext(repo, "squash", oldest, message_template, |
| 1676 | oldest->parents, base_tree_oid, tip_tree_oid, |
| 1677 | sign_commit, &rewritten, |
| 1678 | edit ? COMMIT_TREE_EDIT_MESSAGE : 0); |
| 1679 | if (ret < 0) { |
| 1680 | ret = error(_("failed writing squashed commit")); |
| 1681 | goto out; |
| 1682 | } |
| 1683 | |
| 1684 | ret = handle_reference_updates(&revs, action, tip, rewritten, |
| 1685 | reflog_msg.buf, dry_run, |
| 1686 | sign_commit, |
| 1687 | REPLAY_EMPTY_COMMIT_ABORT); |
| 1688 | if (ret < 0) { |
| 1689 | ret = error(_("failed replaying descendants")); |
| 1690 | goto out; |
| 1691 | } |
| 1692 | |
| 1693 | ret = 0; |
| 1694 | |
| 1695 | out: |
| 1696 | strbuf_release(&reflog_msg); |
| 1697 | release_revisions(&revs); |
| 1698 | free(message_template); |
| 1699 | return ret; |
| 1700 | } |
| 1701 | |
| 1702 | static int update_worktree(struct repository *repo, |
| 1703 | const struct commit *old_head, |
| 1704 | const struct commit *new_head, |
| 1705 | bool dry_run) |
| 1706 | { |
| 1707 | struct reset_working_tree_options opts = { |
| 1708 | .oid_from = &old_head->object.oid, |
| 1709 | .oid = &new_head->object.oid, |
| 1710 | }; |
| 1711 | if (dry_run) |
| 1712 | opts.flags |= RESET_WORKING_TREE_DRY_RUN; |
| 1713 | return reset_working_tree(repo, &opts); |
| 1714 | } |
| 1715 | |
| 1716 | static int find_head_tree_change(struct repository *repo, |
| 1717 | const struct replay_result *result, |
| 1718 | struct commit **old_head, |
| 1719 | struct commit **new_head, |
| 1720 | bool *changed) |
| 1721 | { |
| 1722 | const struct replay_ref_update *head_update = NULL; |
| 1723 | struct commit *old_head_commit, *new_head_commit; |
| 1724 | struct tree *old_head_tree, *new_head_tree; |
| 1725 | const char *head_target; |
| 1726 | int head_flags; |
| 1727 | |
| 1728 | *changed = false; |
| 1729 | |
| 1730 | head_target = refs_resolve_ref_unsafe(get_main_ref_store(repo), "HEAD", |
| 1731 | RESOLVE_REF_NO_RECURSE | RESOLVE_REF_READING, |
| 1732 | NULL, &head_flags); |
| 1733 | if (!head_target) |
| 1734 | return error(_("cannot look up HEAD")); |
| 1735 | |
| 1736 | for (size_t i = 0; i < result->updates_nr; i++) { |
| 1737 | if (!strcmp(result->updates[i].refname, head_target)) { |
| 1738 | head_update = &result->updates[i]; |
| 1739 | break; |
| 1740 | } |
| 1741 | } |
| 1742 | |
| 1743 | if (!head_update) |
| 1744 | return 0; |
| 1745 | |
| 1746 | old_head_commit = lookup_commit_reference(repo, &head_update->old_oid); |
| 1747 | new_head_commit = lookup_commit_reference(repo, &head_update->new_oid); |
| 1748 | if (!old_head_commit || !new_head_commit) |
| 1749 | return error(_("cannot resolve HEAD commit")); |
| 1750 | |
| 1751 | old_head_tree = repo_get_commit_tree(repo, old_head_commit); |
| 1752 | new_head_tree = repo_get_commit_tree(repo, new_head_commit); |
| 1753 | if (!old_head_tree || !new_head_tree) |
| 1754 | return error(_("cannot resolve tree for HEAD")); |
| 1755 | |
| 1756 | if (oideq(&old_head_tree->object.oid, &new_head_tree->object.oid)) |
| 1757 | return 0; |
| 1758 | |
| 1759 | *old_head = old_head_commit; |
| 1760 | *new_head = new_head_commit; |
| 1761 | *changed = true; |
| 1762 | |
| 1763 | return 0; |
| 1764 | } |
| 1765 | |
| 1766 | static int cmd_history_drop(int argc, |
| 1767 | const char **argv, |
| 1768 | const char *prefix, |
| 1769 | struct repository *repo) |
| 1770 | { |
| 1771 | const char * const usage[] = { |
| 1772 | GIT_HISTORY_DROP_USAGE, |
| 1773 | NULL, |
| 1774 | }; |
| 1775 | enum replay_empty_commit_action empty = REPLAY_EMPTY_COMMIT_DROP; |
| 1776 | enum ref_action action = REF_ACTION_DEFAULT; |
| 1777 | const char *sign_commit = NULL; |
| 1778 | int dry_run = 0; |
| 1779 | struct option options[] = { |
| 1780 | OPT_CALLBACK_F(0, "update-refs", &action, "(branches|head)", |
| 1781 | N_("control which refs should be updated"), |
| 1782 | PARSE_OPT_NONEG, parse_ref_action), |
| 1783 | OPT_BOOL('n', "dry-run", &dry_run, |
| 1784 | N_("perform a dry-run without updating any refs")), |
| 1785 | OPT_CALLBACK_F(0, "empty", &empty, "(drop|keep|abort)", |
| 1786 | N_("how to handle descendants that become empty"), |
| 1787 | PARSE_OPT_NONEG, parse_opt_empty), |
| 1788 | OPT_HISTORY_GPG_SIGN(&sign_commit), |
| 1789 | OPT_END(), |
| 1790 | }; |
| 1791 | struct strbuf reflog_msg = STRBUF_INIT; |
| 1792 | struct commit *original, *rewritten; |
| 1793 | struct rev_info revs = { 0 }; |
| 1794 | struct replay_result result = { 0 }; |
| 1795 | struct commit *old_head, *new_head; |
| 1796 | bool head_moves = false; |
| 1797 | int ret; |
| 1798 | |
| 1799 | repo_config(repo, history_config, &sign_commit); |
| 1800 | argc = parse_options(argc, argv, prefix, options, usage, 0); |
| 1801 | if (argc != 1) { |
| 1802 | ret = error(_("command expects a single revision")); |
| 1803 | goto out; |
| 1804 | } |
| 1805 | |
| 1806 | if (action == REF_ACTION_DEFAULT) |
| 1807 | action = REF_ACTION_BRANCHES; |
| 1808 | |
| 1809 | original = lookup_commit_reference_by_name(argv[0]); |
| 1810 | if (!original) { |
| 1811 | ret = error(_("commit cannot be found: %s"), argv[0]); |
| 1812 | goto out; |
| 1813 | } |
| 1814 | |
| 1815 | if (!original->parents) { |
| 1816 | ret = error(_("cannot drop root commit %s: " |
| 1817 | "it has no parent to replay onto"), |
| 1818 | argv[0]); |
| 1819 | goto out; |
| 1820 | } else if (original->parents->next) { |
| 1821 | ret = error(_("cannot drop merge commit: %s"), argv[0]); |
| 1822 | goto out; |
| 1823 | } |
| 1824 | |
| 1825 | ret = setup_revwalk(repo, action, original, &revs); |
| 1826 | if (ret) |
| 1827 | goto out; |
| 1828 | |
| 1829 | rewritten = original->parents->item; |
| 1830 | |
| 1831 | ret = compute_pending_ref_updates(&revs, action, original, rewritten, |
| 1832 | sign_commit, empty, &result); |
| 1833 | if (ret) { |
| 1834 | ret = error(_("failed replaying descendants")); |
| 1835 | goto out; |
| 1836 | } |
| 1837 | |
| 1838 | /* |
| 1839 | * If HEAD will move as a result of the rewrite then we'll have to |
| 1840 | * merge in the changes into the worktree and index. This merge can of |
| 1841 | * course conflict, which will cause the whole operation to abort. |
| 1842 | * |
| 1843 | * If we had already updated the refs at that point then we'd have an |
| 1844 | * inconsistent repository state. So we first perform a dry-run merge |
| 1845 | * here before updating refs. |
| 1846 | */ |
| 1847 | if (!is_bare_repository(repo)) { |
| 1848 | ret = find_head_tree_change(repo, &result, &old_head, |
| 1849 | &new_head, &head_moves); |
| 1850 | if (ret < 0) |
| 1851 | goto out; |
| 1852 | |
| 1853 | if (head_moves && update_worktree(repo, old_head, new_head, true) < 0) { |
| 1854 | ret = error(_("dropping this commit would " |
| 1855 | "overwrite local changes; aborting")); |
| 1856 | goto out; |
| 1857 | } |
| 1858 | } |
| 1859 | |
| 1860 | strbuf_addf(&reflog_msg, "drop: dropping %s", argv[0]); |
| 1861 | ret = apply_pending_ref_updates(repo, &result, reflog_msg.buf, dry_run); |
| 1862 | if (ret < 0) { |
| 1863 | ret = error(_("failed to update references")); |
| 1864 | goto out; |
| 1865 | } |
| 1866 | |
| 1867 | if (!dry_run && head_moves && update_worktree(repo, old_head, new_head, false) < 0) { |
| 1868 | ret = error(_("could not update working tree to new commit %s"), |
| 1869 | oid_to_hex(&new_head->object.oid)); |
| 1870 | goto out; |
| 1871 | } |
| 1872 | |
| 1873 | ret = 0; |
| 1874 | |
| 1875 | out: |
| 1876 | replay_result_release(&result); |
| 1877 | strbuf_release(&reflog_msg); |
| 1878 | release_revisions(&revs); |
| 1879 | return ret; |
| 1880 | } |
| 1881 | |
| 1882 | int cmd_history(int argc, |
| 1883 | const char **argv, |
| 1884 | const char *prefix, |
| 1885 | struct repository *repo) |
| 1886 | { |
| 1887 | const char * const usage[] = { |
| 1888 | GIT_HISTORY_DROP_USAGE, |
| 1889 | GIT_HISTORY_FIXUP_USAGE, |
| 1890 | GIT_HISTORY_REWORD_USAGE, |
| 1891 | GIT_HISTORY_SPLIT_USAGE, |
| 1892 | GIT_HISTORY_SQUASH_USAGE, |
| 1893 | NULL, |
| 1894 | }; |
| 1895 | parse_opt_subcommand_fn *fn = NULL; |
| 1896 | struct option options[] = { |
| 1897 | OPT_SUBCOMMAND("drop", &fn, cmd_history_drop), |
| 1898 | OPT_SUBCOMMAND("fixup", &fn, cmd_history_fixup), |
| 1899 | OPT_SUBCOMMAND("reword", &fn, cmd_history_reword), |
| 1900 | OPT_SUBCOMMAND("split", &fn, cmd_history_split), |
| 1901 | OPT_SUBCOMMAND("squash", &fn, cmd_history_squash), |
| 1902 | OPT_END(), |
| 1903 | }; |
| 1904 | |
| 1905 | argc = parse_options(argc, argv, prefix, options, usage, 0); |
| 1906 | return fn(argc, argv, prefix, repo); |
| 1907 | } |