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