Pratik Karki | 55071ea | 2018-08-07 01:16:09 +0545 | [diff] [blame] | 1 | /* |
| 2 | * "git rebase" builtin command |
| 3 | * |
| 4 | * Copyright (c) 2018 Pratik Karki |
| 5 | */ |
| 6 | |
| 7 | #include "builtin.h" |
| 8 | #include "run-command.h" |
| 9 | #include "exec-cmd.h" |
| 10 | #include "argv-array.h" |
| 11 | #include "dir.h" |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 12 | #include "packfile.h" |
| 13 | #include "refs.h" |
| 14 | #include "quote.h" |
| 15 | #include "config.h" |
| 16 | #include "cache-tree.h" |
| 17 | #include "unpack-trees.h" |
| 18 | #include "lockfile.h" |
Pratik Karki | f28d40d | 2018-09-04 14:27:07 -0700 | [diff] [blame^] | 19 | #include "parse-options.h" |
| 20 | |
| 21 | static char const * const builtin_rebase_usage[] = { |
| 22 | N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] " |
| 23 | "[<upstream>] [<branch>]"), |
| 24 | N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] " |
| 25 | "--root [<branch>]"), |
| 26 | N_("git rebase --continue | --abort | --skip | --edit-todo"), |
| 27 | NULL |
| 28 | }; |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 29 | |
| 30 | static GIT_PATH_FUNC(apply_dir, "rebase-apply") |
| 31 | static GIT_PATH_FUNC(merge_dir, "rebase-merge") |
| 32 | |
| 33 | enum rebase_type { |
| 34 | REBASE_UNSPECIFIED = -1, |
| 35 | REBASE_AM, |
| 36 | REBASE_MERGE, |
| 37 | REBASE_INTERACTIVE, |
| 38 | REBASE_PRESERVE_MERGES |
| 39 | }; |
Pratik Karki | 55071ea | 2018-08-07 01:16:09 +0545 | [diff] [blame] | 40 | |
| 41 | static int use_builtin_rebase(void) |
| 42 | { |
| 43 | struct child_process cp = CHILD_PROCESS_INIT; |
| 44 | struct strbuf out = STRBUF_INIT; |
| 45 | int ret; |
| 46 | |
| 47 | argv_array_pushl(&cp.args, |
| 48 | "config", "--bool", "rebase.usebuiltin", NULL); |
| 49 | cp.git_cmd = 1; |
| 50 | if (capture_command(&cp, &out, 6)) { |
| 51 | strbuf_release(&out); |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | strbuf_trim(&out); |
| 56 | ret = !strcmp("true", out.buf); |
| 57 | strbuf_release(&out); |
| 58 | return ret; |
| 59 | } |
| 60 | |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 61 | static int apply_autostash(void) |
| 62 | { |
| 63 | warning("TODO"); |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | struct rebase_options { |
| 68 | enum rebase_type type; |
| 69 | const char *state_dir; |
| 70 | struct commit *upstream; |
| 71 | const char *upstream_name; |
| 72 | char *head_name; |
| 73 | struct object_id orig_head; |
| 74 | struct commit *onto; |
| 75 | const char *onto_name; |
| 76 | const char *revisions; |
| 77 | int root; |
| 78 | struct commit *restrict_revision; |
| 79 | int dont_finish_rebase; |
| 80 | }; |
| 81 | |
| 82 | /* Returns the filename prefixed by the state_dir */ |
| 83 | static const char *state_dir_path(const char *filename, struct rebase_options *opts) |
| 84 | { |
| 85 | static struct strbuf path = STRBUF_INIT; |
| 86 | static size_t prefix_len; |
| 87 | |
| 88 | if (!prefix_len) { |
| 89 | strbuf_addf(&path, "%s/", opts->state_dir); |
| 90 | prefix_len = path.len; |
| 91 | } |
| 92 | |
| 93 | strbuf_setlen(&path, prefix_len); |
| 94 | strbuf_addstr(&path, filename); |
| 95 | return path.buf; |
| 96 | } |
| 97 | |
| 98 | static int finish_rebase(struct rebase_options *opts) |
| 99 | { |
| 100 | struct strbuf dir = STRBUF_INIT; |
| 101 | const char *argv_gc_auto[] = { "gc", "--auto", NULL }; |
| 102 | |
| 103 | delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF); |
| 104 | apply_autostash(); |
| 105 | close_all_packs(the_repository->objects); |
| 106 | /* |
| 107 | * We ignore errors in 'gc --auto', since the |
| 108 | * user should see them. |
| 109 | */ |
| 110 | run_command_v_opt(argv_gc_auto, RUN_GIT_CMD); |
| 111 | strbuf_addstr(&dir, opts->state_dir); |
| 112 | remove_dir_recursively(&dir, 0); |
| 113 | strbuf_release(&dir); |
| 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | static struct commit *peel_committish(const char *name) |
| 119 | { |
| 120 | struct object *obj; |
| 121 | struct object_id oid; |
| 122 | |
| 123 | if (get_oid(name, &oid)) |
| 124 | return NULL; |
| 125 | obj = parse_object(the_repository, &oid); |
| 126 | return (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT); |
| 127 | } |
| 128 | |
| 129 | static void add_var(struct strbuf *buf, const char *name, const char *value) |
| 130 | { |
| 131 | if (!value) |
| 132 | strbuf_addf(buf, "unset %s; ", name); |
| 133 | else { |
| 134 | strbuf_addf(buf, "%s=", name); |
| 135 | sq_quote_buf(buf, value); |
| 136 | strbuf_addstr(buf, "; "); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | static int run_specific_rebase(struct rebase_options *opts) |
| 141 | { |
| 142 | const char *argv[] = { NULL, NULL }; |
| 143 | struct strbuf script_snippet = STRBUF_INIT; |
| 144 | int status; |
| 145 | const char *backend, *backend_func; |
| 146 | |
| 147 | add_var(&script_snippet, "GIT_DIR", absolute_path(get_git_dir())); |
| 148 | add_var(&script_snippet, "state_dir", opts->state_dir); |
| 149 | |
| 150 | add_var(&script_snippet, "upstream_name", opts->upstream_name); |
| 151 | add_var(&script_snippet, "upstream", |
| 152 | oid_to_hex(&opts->upstream->object.oid)); |
| 153 | add_var(&script_snippet, "head_name", opts->head_name); |
| 154 | add_var(&script_snippet, "orig_head", oid_to_hex(&opts->orig_head)); |
| 155 | add_var(&script_snippet, "onto", oid_to_hex(&opts->onto->object.oid)); |
| 156 | add_var(&script_snippet, "onto_name", opts->onto_name); |
| 157 | add_var(&script_snippet, "revisions", opts->revisions); |
| 158 | add_var(&script_snippet, "restrict_revision", opts->restrict_revision ? |
| 159 | oid_to_hex(&opts->restrict_revision->object.oid) : NULL); |
| 160 | |
| 161 | switch (opts->type) { |
| 162 | case REBASE_AM: |
| 163 | backend = "git-rebase--am"; |
| 164 | backend_func = "git_rebase__am"; |
| 165 | break; |
| 166 | case REBASE_INTERACTIVE: |
| 167 | backend = "git-rebase--interactive"; |
| 168 | backend_func = "git_rebase__interactive"; |
| 169 | break; |
| 170 | case REBASE_MERGE: |
| 171 | backend = "git-rebase--merge"; |
| 172 | backend_func = "git_rebase__merge"; |
| 173 | break; |
| 174 | case REBASE_PRESERVE_MERGES: |
| 175 | backend = "git-rebase--preserve-merges"; |
| 176 | backend_func = "git_rebase__preserve_merges"; |
| 177 | break; |
| 178 | default: |
| 179 | BUG("Unhandled rebase type %d", opts->type); |
| 180 | break; |
| 181 | } |
| 182 | |
| 183 | strbuf_addf(&script_snippet, |
| 184 | ". git-sh-setup && . git-rebase--common &&" |
| 185 | " . %s && %s", backend, backend_func); |
| 186 | argv[0] = script_snippet.buf; |
| 187 | |
| 188 | status = run_command_v_opt(argv, RUN_USING_SHELL); |
| 189 | if (opts->dont_finish_rebase) |
| 190 | ; /* do nothing */ |
| 191 | else if (status == 0) { |
| 192 | if (!file_exists(state_dir_path("stopped-sha", opts))) |
| 193 | finish_rebase(opts); |
| 194 | } else if (status == 2) { |
| 195 | struct strbuf dir = STRBUF_INIT; |
| 196 | |
| 197 | apply_autostash(); |
| 198 | strbuf_addstr(&dir, opts->state_dir); |
| 199 | remove_dir_recursively(&dir, 0); |
| 200 | strbuf_release(&dir); |
| 201 | die("Nothing to do"); |
| 202 | } |
| 203 | |
| 204 | strbuf_release(&script_snippet); |
| 205 | |
| 206 | return status ? -1 : 0; |
| 207 | } |
| 208 | |
| 209 | #define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION" |
| 210 | |
| 211 | static int reset_head(struct object_id *oid, const char *action, |
| 212 | const char *switch_to_branch, int detach_head) |
| 213 | { |
| 214 | struct object_id head_oid; |
| 215 | struct tree_desc desc; |
| 216 | struct lock_file lock = LOCK_INIT; |
| 217 | struct unpack_trees_options unpack_tree_opts; |
| 218 | struct tree *tree; |
| 219 | const char *reflog_action; |
| 220 | struct strbuf msg = STRBUF_INIT; |
| 221 | size_t prefix_len; |
| 222 | struct object_id *orig = NULL, oid_orig, |
| 223 | *old_orig = NULL, oid_old_orig; |
| 224 | int ret = 0; |
| 225 | |
| 226 | if (hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0) |
| 227 | return -1; |
| 228 | |
| 229 | if (!oid) { |
| 230 | if (get_oid("HEAD", &head_oid)) { |
| 231 | rollback_lock_file(&lock); |
| 232 | return error(_("could not determine HEAD revision")); |
| 233 | } |
| 234 | oid = &head_oid; |
| 235 | } |
| 236 | |
| 237 | memset(&unpack_tree_opts, 0, sizeof(unpack_tree_opts)); |
| 238 | setup_unpack_trees_porcelain(&unpack_tree_opts, action); |
| 239 | unpack_tree_opts.head_idx = 1; |
| 240 | unpack_tree_opts.src_index = the_repository->index; |
| 241 | unpack_tree_opts.dst_index = the_repository->index; |
| 242 | unpack_tree_opts.fn = oneway_merge; |
| 243 | unpack_tree_opts.update = 1; |
| 244 | unpack_tree_opts.merge = 1; |
| 245 | if (!detach_head) |
| 246 | unpack_tree_opts.reset = 1; |
| 247 | |
| 248 | if (read_index_unmerged(the_repository->index) < 0) { |
| 249 | rollback_lock_file(&lock); |
| 250 | return error(_("could not read index")); |
| 251 | } |
| 252 | |
| 253 | if (!fill_tree_descriptor(&desc, oid)) { |
| 254 | error(_("failed to find tree of %s"), oid_to_hex(oid)); |
| 255 | rollback_lock_file(&lock); |
| 256 | free((void *)desc.buffer); |
| 257 | return -1; |
| 258 | } |
| 259 | |
| 260 | if (unpack_trees(1, &desc, &unpack_tree_opts)) { |
| 261 | rollback_lock_file(&lock); |
| 262 | free((void *)desc.buffer); |
| 263 | return -1; |
| 264 | } |
| 265 | |
| 266 | tree = parse_tree_indirect(oid); |
| 267 | prime_cache_tree(the_repository->index, tree); |
| 268 | |
| 269 | if (write_locked_index(the_repository->index, &lock, COMMIT_LOCK) < 0) |
| 270 | ret = error(_("could not write index")); |
| 271 | free((void *)desc.buffer); |
| 272 | |
| 273 | if (ret) |
| 274 | return ret; |
| 275 | |
| 276 | reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT); |
| 277 | strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : "rebase"); |
| 278 | prefix_len = msg.len; |
| 279 | |
| 280 | if (!get_oid("ORIG_HEAD", &oid_old_orig)) |
| 281 | old_orig = &oid_old_orig; |
| 282 | if (!get_oid("HEAD", &oid_orig)) { |
| 283 | orig = &oid_orig; |
| 284 | strbuf_addstr(&msg, "updating ORIG_HEAD"); |
| 285 | update_ref(msg.buf, "ORIG_HEAD", orig, old_orig, 0, |
| 286 | UPDATE_REFS_MSG_ON_ERR); |
| 287 | } else if (old_orig) |
| 288 | delete_ref(NULL, "ORIG_HEAD", old_orig, 0); |
| 289 | strbuf_setlen(&msg, prefix_len); |
| 290 | strbuf_addstr(&msg, "updating HEAD"); |
| 291 | if (!switch_to_branch) |
| 292 | ret = update_ref(msg.buf, "HEAD", oid, orig, REF_NO_DEREF, |
| 293 | UPDATE_REFS_MSG_ON_ERR); |
| 294 | else { |
| 295 | ret = create_symref("HEAD", switch_to_branch, msg.buf); |
| 296 | if (!ret) |
| 297 | ret = update_ref(msg.buf, "HEAD", oid, NULL, 0, |
| 298 | UPDATE_REFS_MSG_ON_ERR); |
| 299 | } |
| 300 | |
| 301 | strbuf_release(&msg); |
| 302 | return ret; |
| 303 | } |
| 304 | |
Pratik Karki | 55071ea | 2018-08-07 01:16:09 +0545 | [diff] [blame] | 305 | int cmd_rebase(int argc, const char **argv, const char *prefix) |
| 306 | { |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 307 | struct rebase_options options = { |
| 308 | .type = REBASE_UNSPECIFIED, |
| 309 | }; |
| 310 | const char *branch_name; |
| 311 | int ret, flags; |
| 312 | struct strbuf msg = STRBUF_INIT; |
| 313 | struct strbuf revisions = STRBUF_INIT; |
Pratik Karki | f28d40d | 2018-09-04 14:27:07 -0700 | [diff] [blame^] | 314 | struct option builtin_rebase_options[] = { |
| 315 | OPT_STRING(0, "onto", &options.onto_name, |
| 316 | N_("revision"), |
| 317 | N_("rebase onto given branch instead of upstream")), |
| 318 | OPT_END(), |
| 319 | }; |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 320 | |
Pratik Karki | 55071ea | 2018-08-07 01:16:09 +0545 | [diff] [blame] | 321 | /* |
| 322 | * NEEDSWORK: Once the builtin rebase has been tested enough |
| 323 | * and git-legacy-rebase.sh is retired to contrib/, this preamble |
| 324 | * can be removed. |
| 325 | */ |
| 326 | |
| 327 | if (!use_builtin_rebase()) { |
| 328 | const char *path = mkpath("%s/git-legacy-rebase", |
| 329 | git_exec_path()); |
| 330 | |
| 331 | if (sane_execvp(path, (char **)argv) < 0) |
| 332 | die_errno(_("could not exec %s"), path); |
| 333 | else |
| 334 | BUG("sane_execvp() returned???"); |
| 335 | } |
| 336 | |
Pratik Karki | f28d40d | 2018-09-04 14:27:07 -0700 | [diff] [blame^] | 337 | if (argc == 2 && !strcmp(argv[1], "-h")) |
| 338 | usage_with_options(builtin_rebase_usage, |
| 339 | builtin_rebase_options); |
| 340 | |
Pratik Karki | 55071ea | 2018-08-07 01:16:09 +0545 | [diff] [blame] | 341 | prefix = setup_git_directory(); |
| 342 | trace_repo_setup(prefix); |
| 343 | setup_work_tree(); |
| 344 | |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 345 | git_config(git_default_config, NULL); |
Pratik Karki | f28d40d | 2018-09-04 14:27:07 -0700 | [diff] [blame^] | 346 | argc = parse_options(argc, argv, prefix, |
| 347 | builtin_rebase_options, |
| 348 | builtin_rebase_usage, 0); |
| 349 | |
| 350 | if (argc > 2) |
| 351 | usage_with_options(builtin_rebase_usage, |
| 352 | builtin_rebase_options); |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 353 | |
| 354 | switch (options.type) { |
| 355 | case REBASE_MERGE: |
| 356 | case REBASE_INTERACTIVE: |
| 357 | case REBASE_PRESERVE_MERGES: |
| 358 | options.state_dir = merge_dir(); |
| 359 | break; |
| 360 | case REBASE_AM: |
| 361 | options.state_dir = apply_dir(); |
| 362 | break; |
| 363 | default: |
| 364 | /* the default rebase backend is `--am` */ |
| 365 | options.type = REBASE_AM; |
| 366 | options.state_dir = apply_dir(); |
| 367 | break; |
| 368 | } |
| 369 | |
| 370 | if (!options.root) { |
Pratik Karki | f28d40d | 2018-09-04 14:27:07 -0700 | [diff] [blame^] | 371 | if (argc < 1) |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 372 | die("TODO: handle @{upstream}"); |
| 373 | else { |
Pratik Karki | f28d40d | 2018-09-04 14:27:07 -0700 | [diff] [blame^] | 374 | options.upstream_name = argv[0]; |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 375 | argc--; |
| 376 | argv++; |
| 377 | if (!strcmp(options.upstream_name, "-")) |
| 378 | options.upstream_name = "@{-1}"; |
| 379 | } |
| 380 | options.upstream = peel_committish(options.upstream_name); |
| 381 | if (!options.upstream) |
| 382 | die(_("invalid upstream '%s'"), options.upstream_name); |
| 383 | } else |
| 384 | die("TODO: upstream for --root"); |
| 385 | |
| 386 | /* Make sure the branch to rebase onto is valid. */ |
| 387 | if (!options.onto_name) |
| 388 | options.onto_name = options.upstream_name; |
| 389 | if (strstr(options.onto_name, "...")) { |
| 390 | die("TODO"); |
| 391 | } else { |
| 392 | options.onto = peel_committish(options.onto_name); |
| 393 | if (!options.onto) |
| 394 | die(_("Does not point to a valid commit '%s'"), |
| 395 | options.onto_name); |
| 396 | } |
| 397 | |
| 398 | /* |
| 399 | * If the branch to rebase is given, that is the branch we will rebase |
| 400 | * branch_name -- branch/commit being rebased, or |
| 401 | * HEAD (already detached) |
| 402 | * orig_head -- commit object name of tip of the branch before rebasing |
| 403 | * head_name -- refs/heads/<that-branch> or "detached HEAD" |
| 404 | */ |
Pratik Karki | f28d40d | 2018-09-04 14:27:07 -0700 | [diff] [blame^] | 405 | if (argc > 0) |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 406 | die("TODO: handle switch_to"); |
| 407 | else { |
| 408 | /* Do not need to switch branches, we are already on it. */ |
| 409 | options.head_name = |
| 410 | xstrdup_or_null(resolve_ref_unsafe("HEAD", 0, NULL, |
| 411 | &flags)); |
| 412 | if (!options.head_name) |
| 413 | die(_("No such ref: %s"), "HEAD"); |
| 414 | if (flags & REF_ISSYMREF) { |
| 415 | if (!skip_prefix(options.head_name, |
| 416 | "refs/heads/", &branch_name)) |
| 417 | branch_name = options.head_name; |
| 418 | |
| 419 | } else { |
| 420 | options.head_name = xstrdup("detached HEAD"); |
| 421 | branch_name = "HEAD"; |
| 422 | } |
| 423 | if (get_oid("HEAD", &options.orig_head)) |
| 424 | die(_("Could not resolve HEAD to a revision")); |
| 425 | } |
| 426 | |
| 427 | strbuf_addf(&msg, "rebase: checkout %s", options.onto_name); |
| 428 | if (reset_head(&options.onto->object.oid, "checkout", NULL, 1)) |
| 429 | die(_("Could not detach HEAD")); |
| 430 | strbuf_release(&msg); |
| 431 | |
| 432 | strbuf_addf(&revisions, "%s..%s", |
| 433 | options.root ? oid_to_hex(&options.onto->object.oid) : |
| 434 | (options.restrict_revision ? |
| 435 | oid_to_hex(&options.restrict_revision->object.oid) : |
| 436 | oid_to_hex(&options.upstream->object.oid)), |
| 437 | oid_to_hex(&options.orig_head)); |
| 438 | |
| 439 | options.revisions = revisions.buf; |
| 440 | |
| 441 | ret = !!run_specific_rebase(&options); |
| 442 | |
| 443 | strbuf_release(&revisions); |
| 444 | free(options.head_name); |
| 445 | return ret; |
Pratik Karki | 55071ea | 2018-08-07 01:16:09 +0545 | [diff] [blame] | 446 | } |