| 1 | /* |
| 2 | * Builtin "git pull" |
| 3 | * |
| 4 | * Based on git-pull.sh by Junio C Hamano |
| 5 | * |
| 6 | * Fetch one or more remote refs and merge it/them into the current HEAD. |
| 7 | */ |
| 8 | |
| 9 | #define USE_THE_REPOSITORY_VARIABLE |
| 10 | |
| 11 | #include "builtin.h" |
| 12 | #include "advice.h" |
| 13 | #include "config.h" |
| 14 | #include "environment.h" |
| 15 | #include "gettext.h" |
| 16 | #include "hex.h" |
| 17 | #include "merge.h" |
| 18 | #include "object-name.h" |
| 19 | #include "parse-options.h" |
| 20 | #include "run-command.h" |
| 21 | #include "oid-array.h" |
| 22 | #include "remote.h" |
| 23 | #include "dir.h" |
| 24 | #include "path.h" |
| 25 | #include "read-cache-ll.h" |
| 26 | #include "rebase.h" |
| 27 | #include "refs.h" |
| 28 | #include "refspec.h" |
| 29 | #include "submodule.h" |
| 30 | #include "submodule-config.h" |
| 31 | #include "wt-status.h" |
| 32 | #include "commit-reach.h" |
| 33 | #include "sequencer.h" |
| 34 | |
| 35 | /** |
| 36 | * Parses the value of --rebase. If value is a false value, returns |
| 37 | * REBASE_FALSE. If value is a true value, returns REBASE_TRUE. If value is |
| 38 | * "merges", returns REBASE_MERGES. If value is a invalid value, dies with |
| 39 | * a fatal error if fatal is true, otherwise returns REBASE_INVALID. |
| 40 | */ |
| 41 | static enum rebase_type parse_config_rebase(const char *key, const char *value, |
| 42 | int fatal) |
| 43 | { |
| 44 | enum rebase_type v = rebase_parse_value(value); |
| 45 | if (v != REBASE_INVALID) |
| 46 | return v; |
| 47 | |
| 48 | if (fatal) |
| 49 | die(_("invalid value for '%s': '%s'"), key, value); |
| 50 | else |
| 51 | error(_("invalid value for '%s': '%s'"), key, value); |
| 52 | |
| 53 | return REBASE_INVALID; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Callback for --rebase, which parses arg with parse_config_rebase(). |
| 58 | */ |
| 59 | static int parse_opt_rebase(const struct option *opt, const char *arg, int unset) |
| 60 | { |
| 61 | enum rebase_type *value = opt->value; |
| 62 | |
| 63 | if (arg) |
| 64 | *value = parse_config_rebase("--rebase", arg, 0); |
| 65 | else |
| 66 | *value = unset ? REBASE_FALSE : REBASE_TRUE; |
| 67 | return *value == REBASE_INVALID ? -1 : 0; |
| 68 | } |
| 69 | |
| 70 | static const char * const pull_usage[] = { |
| 71 | N_("git pull [<options>] [<repository> [<refspec>...]]"), |
| 72 | NULL |
| 73 | }; |
| 74 | |
| 75 | /* Shared options */ |
| 76 | static int opt_verbosity; |
| 77 | static const char *opt_progress; |
| 78 | static int recurse_submodules = RECURSE_SUBMODULES_DEFAULT; |
| 79 | static int recurse_submodules_cli = RECURSE_SUBMODULES_DEFAULT; |
| 80 | |
| 81 | /* Options passed to git-merge or git-rebase */ |
| 82 | static enum rebase_type opt_rebase = -1; |
| 83 | static const char *opt_diffstat; |
| 84 | static const char *opt_log; |
| 85 | static const char *opt_signoff; |
| 86 | static const char *opt_squash; |
| 87 | static const char *opt_commit; |
| 88 | static const char *opt_edit; |
| 89 | static const char *cleanup_arg; |
| 90 | static char *opt_ff; |
| 91 | static const char *opt_verify_signatures; |
| 92 | static const char *opt_verify; |
| 93 | static int opt_autostash = -1; |
| 94 | static int config_rebase_autostash; |
| 95 | static int config_pull_autostash = -1; |
| 96 | static int check_trust_level = 1; |
| 97 | static struct strvec opt_strategies = STRVEC_INIT; |
| 98 | static struct strvec opt_strategy_opts = STRVEC_INIT; |
| 99 | static const char *opt_gpg_sign; |
| 100 | static int opt_allow_unrelated_histories; |
| 101 | |
| 102 | /* Options passed to git-fetch */ |
| 103 | static const char *opt_all; |
| 104 | static const char *opt_append; |
| 105 | static const char *opt_upload_pack; |
| 106 | static int opt_force; |
| 107 | static const char *opt_tags; |
| 108 | static const char *opt_prune; |
| 109 | static const char *max_children; |
| 110 | static int opt_dry_run; |
| 111 | static const char *opt_keep; |
| 112 | static const char *opt_depth; |
| 113 | static const char *opt_unshallow; |
| 114 | static const char *opt_update_shallow; |
| 115 | static const char *opt_refmap; |
| 116 | static const char *opt_ipv4; |
| 117 | static const char *opt_ipv6; |
| 118 | static int opt_show_forced_updates = -1; |
| 119 | static const char *set_upstream; |
| 120 | static struct strvec opt_fetch = STRVEC_INIT; |
| 121 | |
| 122 | /** |
| 123 | * Pushes "-q" or "-v" switches into arr to match the opt_verbosity level. |
| 124 | */ |
| 125 | static void argv_push_verbosity(struct strvec *arr) |
| 126 | { |
| 127 | int verbosity; |
| 128 | |
| 129 | for (verbosity = opt_verbosity; verbosity > 0; verbosity--) |
| 130 | strvec_push(arr, "-v"); |
| 131 | |
| 132 | for (verbosity = opt_verbosity; verbosity < 0; verbosity++) |
| 133 | strvec_push(arr, "-q"); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Pushes "-f" switches into arr to match the opt_force level. |
| 138 | */ |
| 139 | static void argv_push_force(struct strvec *arr) |
| 140 | { |
| 141 | int force = opt_force; |
| 142 | while (force-- > 0) |
| 143 | strvec_push(arr, "-f"); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Sets the GIT_REFLOG_ACTION environment variable to the concatenation of argv |
| 148 | */ |
| 149 | static void set_reflog_message(int argc, const char **argv) |
| 150 | { |
| 151 | int i; |
| 152 | struct strbuf msg = STRBUF_INIT; |
| 153 | |
| 154 | for (i = 0; i < argc; i++) { |
| 155 | if (i) |
| 156 | strbuf_addch(&msg, ' '); |
| 157 | strbuf_addstr(&msg, argv[i]); |
| 158 | } |
| 159 | |
| 160 | setenv("GIT_REFLOG_ACTION", msg.buf, 0); |
| 161 | |
| 162 | strbuf_release(&msg); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * If pull.ff is unset, returns NULL. If pull.ff is "true", returns "--ff". If |
| 167 | * pull.ff is "false", returns "--no-ff". If pull.ff is "only", returns |
| 168 | * "--ff-only". Otherwise, if pull.ff is set to an invalid value, die with an |
| 169 | * error. |
| 170 | */ |
| 171 | static const char *config_get_ff(void) |
| 172 | { |
| 173 | const char *value; |
| 174 | |
| 175 | if (repo_config_get_value(the_repository, "pull.ff", &value)) |
| 176 | return NULL; |
| 177 | |
| 178 | switch (git_parse_maybe_bool(value)) { |
| 179 | case 0: |
| 180 | return "--no-ff"; |
| 181 | case 1: |
| 182 | return "--ff"; |
| 183 | } |
| 184 | |
| 185 | if (!strcmp(value, "only")) |
| 186 | return "--ff-only"; |
| 187 | |
| 188 | die(_("invalid value for '%s': '%s'"), "pull.ff", value); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Returns the default configured value for --rebase. It first looks for the |
| 193 | * value of "branch.$curr_branch.rebase", where $curr_branch is the current |
| 194 | * branch, and if HEAD is detached or the configuration key does not exist, |
| 195 | * looks for the value of "pull.rebase". If both configuration keys do not |
| 196 | * exist, returns REBASE_FALSE. |
| 197 | */ |
| 198 | static enum rebase_type config_get_rebase(int *rebase_unspecified) |
| 199 | { |
| 200 | struct branch *curr_branch = branch_get("HEAD"); |
| 201 | const char *value; |
| 202 | |
| 203 | if (curr_branch) { |
| 204 | char *key = xstrfmt("branch.%s.rebase", curr_branch->name); |
| 205 | |
| 206 | if (!repo_config_get_value(the_repository, key, &value)) { |
| 207 | enum rebase_type ret = parse_config_rebase(key, value, 1); |
| 208 | free(key); |
| 209 | return ret; |
| 210 | } |
| 211 | |
| 212 | free(key); |
| 213 | } |
| 214 | |
| 215 | if (!repo_config_get_value(the_repository, "pull.rebase", &value)) |
| 216 | return parse_config_rebase("pull.rebase", value, 1); |
| 217 | |
| 218 | *rebase_unspecified = 1; |
| 219 | |
| 220 | return REBASE_FALSE; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Read config variables. |
| 225 | */ |
| 226 | static int git_pull_config(const char *var, const char *value, |
| 227 | const struct config_context *ctx, void *cb) |
| 228 | { |
| 229 | if (!strcmp(var, "rebase.autostash")) { |
| 230 | /* |
| 231 | * run_rebase() also reads this option. The reason we handle it here is |
| 232 | * that when pull.rebase is true, a fast-forward may occur without |
| 233 | * invoking run_rebase(). We need to ensure that autostash is set even |
| 234 | * in the fast-forward case. |
| 235 | * |
| 236 | * run_merge() handles merge.autostash, so we don't handle it here. |
| 237 | */ |
| 238 | config_rebase_autostash = git_config_bool(var, value); |
| 239 | return 0; |
| 240 | } else if (!strcmp(var, "pull.autostash")) { |
| 241 | config_pull_autostash = git_config_bool(var, value); |
| 242 | return 0; |
| 243 | } else if (!strcmp(var, "submodule.recurse")) { |
| 244 | recurse_submodules = git_config_bool(var, value) ? |
| 245 | RECURSE_SUBMODULES_ON : RECURSE_SUBMODULES_OFF; |
| 246 | return 0; |
| 247 | } else if (!strcmp(var, "gpg.mintrustlevel")) { |
| 248 | check_trust_level = 0; |
| 249 | } |
| 250 | |
| 251 | return git_default_config(var, value, ctx, cb); |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Appends merge candidates from FETCH_HEAD that are not marked not-for-merge |
| 256 | * into merge_heads. |
| 257 | */ |
| 258 | static void get_merge_heads(struct oid_array *merge_heads) |
| 259 | { |
| 260 | const char *filename = git_path_fetch_head(the_repository); |
| 261 | FILE *fp; |
| 262 | struct strbuf sb = STRBUF_INIT; |
| 263 | struct object_id oid; |
| 264 | |
| 265 | fp = xfopen(filename, "r"); |
| 266 | while (strbuf_getline_lf(&sb, fp) != EOF) { |
| 267 | const char *p; |
| 268 | if (parse_oid_hex(sb.buf, &oid, &p)) |
| 269 | continue; /* invalid line: does not start with object ID */ |
| 270 | if (starts_with(p, "\tnot-for-merge\t")) |
| 271 | continue; /* ref is not-for-merge */ |
| 272 | oid_array_append(merge_heads, &oid); |
| 273 | } |
| 274 | fclose(fp); |
| 275 | strbuf_release(&sb); |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Used by die_no_merge_candidates() as a for_each_remote() callback to |
| 280 | * retrieve the name of the remote if the repository only has one remote. |
| 281 | */ |
| 282 | static int get_only_remote(struct remote *remote, void *cb_data) |
| 283 | { |
| 284 | const char **remote_name = cb_data; |
| 285 | |
| 286 | if (*remote_name) |
| 287 | return -1; |
| 288 | |
| 289 | *remote_name = remote->name; |
| 290 | return 0; |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Dies with the appropriate reason for why there are no merge candidates: |
| 295 | * |
| 296 | * 1. We fetched from a specific remote, and a refspec was given, but it ended |
| 297 | * up not fetching anything. This is usually because the user provided a |
| 298 | * wildcard refspec which had no matches on the remote end. |
| 299 | * |
| 300 | * 2. We fetched from a non-default remote, but didn't specify a branch to |
| 301 | * merge. We can't use the configured one because it applies to the default |
| 302 | * remote, thus the user must specify the branches to merge. |
| 303 | * |
| 304 | * 3. We fetched from the branch's or repo's default remote, but: |
| 305 | * |
| 306 | * a. We are not on a branch, so there will never be a configured branch to |
| 307 | * merge with. |
| 308 | * |
| 309 | * b. We are on a branch, but there is no configured branch to merge with. |
| 310 | * |
| 311 | * 4. We fetched from the branch's or repo's default remote, but the configured |
| 312 | * branch to merge didn't get fetched. (Either it doesn't exist, or wasn't |
| 313 | * part of the configured fetch refspec.) |
| 314 | */ |
| 315 | static void NORETURN die_no_merge_candidates(const char *repo, const char **refspecs) |
| 316 | { |
| 317 | struct branch *curr_branch = branch_get("HEAD"); |
| 318 | const char *remote = curr_branch ? curr_branch->remote_name : NULL; |
| 319 | |
| 320 | if (*refspecs) { |
| 321 | if (opt_rebase) |
| 322 | fprintf_ln(stderr, _("There is no candidate for rebasing against among the refs that you just fetched.")); |
| 323 | else |
| 324 | fprintf_ln(stderr, _("There are no candidates for merging among the refs that you just fetched.")); |
| 325 | fprintf_ln(stderr, _("Generally this means that you provided a wildcard refspec which had no\n" |
| 326 | "matches on the remote end.")); |
| 327 | } else if (repo && curr_branch && (!remote || strcmp(repo, remote))) { |
| 328 | fprintf_ln(stderr, _("You asked to pull from the remote '%s', but did not specify\n" |
| 329 | "a branch. Because this is not the default configured remote\n" |
| 330 | "for your current branch, you must specify a branch on the command line."), |
| 331 | repo); |
| 332 | } else if (!curr_branch) { |
| 333 | fprintf_ln(stderr, _("You are not currently on a branch.")); |
| 334 | if (opt_rebase) |
| 335 | fprintf_ln(stderr, _("Please specify which branch you want to rebase against.")); |
| 336 | else |
| 337 | fprintf_ln(stderr, _("Please specify which branch you want to merge with.")); |
| 338 | fprintf_ln(stderr, _("See git-pull(1) for details.")); |
| 339 | fprintf(stderr, "\n"); |
| 340 | fprintf_ln(stderr, " git pull %s %s", _("<remote>"), _("<branch>")); |
| 341 | fprintf(stderr, "\n"); |
| 342 | } else if (!curr_branch->merge_nr) { |
| 343 | const char *remote_name = NULL; |
| 344 | |
| 345 | if (for_each_remote(get_only_remote, &remote_name) || !remote_name) |
| 346 | remote_name = _("<remote>"); |
| 347 | |
| 348 | fprintf_ln(stderr, _("There is no tracking information for the current branch.")); |
| 349 | if (opt_rebase) |
| 350 | fprintf_ln(stderr, _("Please specify which branch you want to rebase against.")); |
| 351 | else |
| 352 | fprintf_ln(stderr, _("Please specify which branch you want to merge with.")); |
| 353 | fprintf_ln(stderr, _("See git-pull(1) for details.")); |
| 354 | fprintf(stderr, "\n"); |
| 355 | fprintf_ln(stderr, " git pull %s %s", _("<remote>"), _("<branch>")); |
| 356 | fprintf(stderr, "\n"); |
| 357 | fprintf_ln(stderr, _("If you wish to set tracking information for this branch you can do so with:")); |
| 358 | fprintf(stderr, "\n"); |
| 359 | fprintf_ln(stderr, " git branch --set-upstream-to=%s/%s %s\n", |
| 360 | remote_name, _("<branch>"), curr_branch->name); |
| 361 | } else |
| 362 | fprintf_ln(stderr, _("Your configuration specifies to merge with the ref '%s'\n" |
| 363 | "from the remote, but no such ref was fetched."), |
| 364 | curr_branch->merge[0]->src); |
| 365 | exit(1); |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * Parses argv into [<repo> [<refspecs>...]], returning their values in `repo` |
| 370 | * as a string and `refspecs` as a null-terminated array of strings. If `repo` |
| 371 | * is not provided in argv, it is set to NULL. |
| 372 | */ |
| 373 | static void parse_repo_refspecs(int argc, const char **argv, const char **repo, |
| 374 | const char ***refspecs) |
| 375 | { |
| 376 | if (argc > 0) { |
| 377 | *repo = *argv++; |
| 378 | argc--; |
| 379 | } else |
| 380 | *repo = NULL; |
| 381 | *refspecs = argv; |
| 382 | } |
| 383 | |
| 384 | /** |
| 385 | * Runs git-fetch, returning its exit status. `repo` and `refspecs` are the |
| 386 | * repository and refspecs to fetch, or NULL if they are not provided. |
| 387 | */ |
| 388 | static int run_fetch(const char *repo, const char **refspecs) |
| 389 | { |
| 390 | struct child_process cmd = CHILD_PROCESS_INIT; |
| 391 | |
| 392 | strvec_pushl(&cmd.args, "fetch", "--update-head-ok", NULL); |
| 393 | |
| 394 | /* Shared options */ |
| 395 | argv_push_verbosity(&cmd.args); |
| 396 | if (opt_progress) |
| 397 | strvec_push(&cmd.args, opt_progress); |
| 398 | |
| 399 | /* Options passed to git-fetch */ |
| 400 | if (opt_all) |
| 401 | strvec_push(&cmd.args, opt_all); |
| 402 | if (opt_append) |
| 403 | strvec_push(&cmd.args, opt_append); |
| 404 | if (opt_upload_pack) |
| 405 | strvec_push(&cmd.args, opt_upload_pack); |
| 406 | argv_push_force(&cmd.args); |
| 407 | if (opt_tags) |
| 408 | strvec_push(&cmd.args, opt_tags); |
| 409 | if (opt_prune) |
| 410 | strvec_push(&cmd.args, opt_prune); |
| 411 | if (recurse_submodules_cli != RECURSE_SUBMODULES_DEFAULT) |
| 412 | switch (recurse_submodules_cli) { |
| 413 | case RECURSE_SUBMODULES_ON: |
| 414 | strvec_push(&cmd.args, "--recurse-submodules=on"); |
| 415 | break; |
| 416 | case RECURSE_SUBMODULES_OFF: |
| 417 | strvec_push(&cmd.args, "--recurse-submodules=no"); |
| 418 | break; |
| 419 | case RECURSE_SUBMODULES_ON_DEMAND: |
| 420 | strvec_push(&cmd.args, "--recurse-submodules=on-demand"); |
| 421 | break; |
| 422 | default: |
| 423 | BUG("submodule recursion option not understood"); |
| 424 | } |
| 425 | if (max_children) |
| 426 | strvec_push(&cmd.args, max_children); |
| 427 | if (opt_dry_run) |
| 428 | strvec_push(&cmd.args, "--dry-run"); |
| 429 | if (opt_keep) |
| 430 | strvec_push(&cmd.args, opt_keep); |
| 431 | if (opt_depth) |
| 432 | strvec_push(&cmd.args, opt_depth); |
| 433 | if (opt_unshallow) |
| 434 | strvec_push(&cmd.args, opt_unshallow); |
| 435 | if (opt_update_shallow) |
| 436 | strvec_push(&cmd.args, opt_update_shallow); |
| 437 | if (opt_refmap) |
| 438 | strvec_push(&cmd.args, opt_refmap); |
| 439 | if (opt_ipv4) |
| 440 | strvec_push(&cmd.args, opt_ipv4); |
| 441 | if (opt_ipv6) |
| 442 | strvec_push(&cmd.args, opt_ipv6); |
| 443 | if (opt_show_forced_updates > 0) |
| 444 | strvec_push(&cmd.args, "--show-forced-updates"); |
| 445 | else if (opt_show_forced_updates == 0) |
| 446 | strvec_push(&cmd.args, "--no-show-forced-updates"); |
| 447 | if (set_upstream) |
| 448 | strvec_push(&cmd.args, set_upstream); |
| 449 | strvec_pushv(&cmd.args, opt_fetch.v); |
| 450 | |
| 451 | if (repo) { |
| 452 | strvec_push(&cmd.args, repo); |
| 453 | strvec_pushv(&cmd.args, refspecs); |
| 454 | } else if (*refspecs) |
| 455 | BUG("refspecs without repo?"); |
| 456 | cmd.git_cmd = 1; |
| 457 | cmd.odb_to_close = the_repository->objects; |
| 458 | return run_command(&cmd); |
| 459 | } |
| 460 | |
| 461 | /** |
| 462 | * "Pulls into void" by branching off merge_head. |
| 463 | */ |
| 464 | static int pull_into_void(const struct object_id *merge_head, |
| 465 | const struct object_id *curr_head) |
| 466 | { |
| 467 | if (opt_verify_signatures) { |
| 468 | struct commit *commit; |
| 469 | |
| 470 | commit = lookup_commit(the_repository, merge_head); |
| 471 | if (!commit) |
| 472 | die(_("unable to access commit %s"), |
| 473 | oid_to_hex(merge_head)); |
| 474 | |
| 475 | verify_merge_signature(commit, opt_verbosity, |
| 476 | check_trust_level); |
| 477 | } |
| 478 | |
| 479 | /* |
| 480 | * Two-way merge: we treat the index as based on an empty tree, |
| 481 | * and try to fast-forward to HEAD. This ensures we will not lose |
| 482 | * index/worktree changes that the user already made on the unborn |
| 483 | * branch. |
| 484 | */ |
| 485 | if (checkout_fast_forward(the_repository, |
| 486 | the_hash_algo->empty_tree, |
| 487 | merge_head, 0)) |
| 488 | return 1; |
| 489 | |
| 490 | if (refs_update_ref(get_main_ref_store(the_repository), "initial pull", "HEAD", merge_head, curr_head, 0, UPDATE_REFS_DIE_ON_ERR)) |
| 491 | return 1; |
| 492 | |
| 493 | return 0; |
| 494 | } |
| 495 | |
| 496 | static int rebase_submodules(void) |
| 497 | { |
| 498 | struct child_process cp = CHILD_PROCESS_INIT; |
| 499 | |
| 500 | cp.git_cmd = 1; |
| 501 | cp.no_stdin = 1; |
| 502 | strvec_pushl(&cp.args, "submodule", "update", |
| 503 | "--recursive", "--rebase", NULL); |
| 504 | argv_push_verbosity(&cp.args); |
| 505 | |
| 506 | return run_command(&cp); |
| 507 | } |
| 508 | |
| 509 | static int update_submodules(void) |
| 510 | { |
| 511 | struct child_process cp = CHILD_PROCESS_INIT; |
| 512 | |
| 513 | cp.git_cmd = 1; |
| 514 | cp.no_stdin = 1; |
| 515 | strvec_pushl(&cp.args, "submodule", "update", |
| 516 | "--recursive", "--checkout", NULL); |
| 517 | argv_push_verbosity(&cp.args); |
| 518 | |
| 519 | return run_command(&cp); |
| 520 | } |
| 521 | |
| 522 | /** |
| 523 | * Runs git-merge, returning its exit status. |
| 524 | */ |
| 525 | static int run_merge(void) |
| 526 | { |
| 527 | struct child_process cmd = CHILD_PROCESS_INIT; |
| 528 | |
| 529 | strvec_pushl(&cmd.args, "merge", NULL); |
| 530 | |
| 531 | /* Shared options */ |
| 532 | argv_push_verbosity(&cmd.args); |
| 533 | if (opt_progress) |
| 534 | strvec_push(&cmd.args, opt_progress); |
| 535 | |
| 536 | /* Options passed to git-merge */ |
| 537 | if (opt_diffstat) |
| 538 | strvec_push(&cmd.args, opt_diffstat); |
| 539 | if (opt_log) |
| 540 | strvec_push(&cmd.args, opt_log); |
| 541 | if (opt_signoff) |
| 542 | strvec_push(&cmd.args, opt_signoff); |
| 543 | if (opt_squash) |
| 544 | strvec_push(&cmd.args, opt_squash); |
| 545 | if (opt_commit) |
| 546 | strvec_push(&cmd.args, opt_commit); |
| 547 | if (opt_edit) |
| 548 | strvec_push(&cmd.args, opt_edit); |
| 549 | if (cleanup_arg) |
| 550 | strvec_pushf(&cmd.args, "--cleanup=%s", cleanup_arg); |
| 551 | if (opt_ff) |
| 552 | strvec_push(&cmd.args, opt_ff); |
| 553 | if (opt_verify) |
| 554 | strvec_push(&cmd.args, opt_verify); |
| 555 | if (opt_verify_signatures) |
| 556 | strvec_push(&cmd.args, opt_verify_signatures); |
| 557 | strvec_pushv(&cmd.args, opt_strategies.v); |
| 558 | strvec_pushv(&cmd.args, opt_strategy_opts.v); |
| 559 | if (opt_gpg_sign) |
| 560 | strvec_push(&cmd.args, opt_gpg_sign); |
| 561 | if (opt_autostash == 0) |
| 562 | strvec_push(&cmd.args, "--no-autostash"); |
| 563 | else if (opt_autostash == 1) |
| 564 | strvec_push(&cmd.args, "--autostash"); |
| 565 | if (opt_allow_unrelated_histories > 0) |
| 566 | strvec_push(&cmd.args, "--allow-unrelated-histories"); |
| 567 | |
| 568 | strvec_push(&cmd.args, "FETCH_HEAD"); |
| 569 | cmd.git_cmd = 1; |
| 570 | return run_command(&cmd); |
| 571 | } |
| 572 | |
| 573 | /** |
| 574 | * Returns remote's upstream branch for the current branch. If remote is NULL, |
| 575 | * the current branch's configured default remote is used. Returns NULL if |
| 576 | * `remote` does not name a valid remote, HEAD does not point to a branch, |
| 577 | * remote is not the branch's configured remote or the branch does not have any |
| 578 | * configured upstream branch. |
| 579 | */ |
| 580 | static const char *get_upstream_branch(const char *remote) |
| 581 | { |
| 582 | struct remote *rm; |
| 583 | struct branch *curr_branch; |
| 584 | const char *curr_branch_remote; |
| 585 | |
| 586 | rm = remote_get(remote); |
| 587 | if (!rm) |
| 588 | return NULL; |
| 589 | |
| 590 | curr_branch = branch_get("HEAD"); |
| 591 | if (!curr_branch) |
| 592 | return NULL; |
| 593 | |
| 594 | curr_branch_remote = remote_for_branch(curr_branch, NULL); |
| 595 | assert(curr_branch_remote); |
| 596 | |
| 597 | if (strcmp(curr_branch_remote, rm->name)) |
| 598 | return NULL; |
| 599 | |
| 600 | return branch_get_upstream(curr_branch, NULL); |
| 601 | } |
| 602 | |
| 603 | /** |
| 604 | * Derives the remote-tracking branch from the remote and refspec. |
| 605 | * |
| 606 | * FIXME: The current implementation assumes the default mapping of |
| 607 | * refs/heads/<branch_name> to refs/remotes/<remote_name>/<branch_name>. |
| 608 | */ |
| 609 | static const char *get_tracking_branch(const char *remote, const char *refspec) |
| 610 | { |
| 611 | struct refspec_item spec; |
| 612 | const char *spec_src; |
| 613 | const char *merge_branch; |
| 614 | |
| 615 | if (!refspec_item_init_fetch(&spec, refspec)) |
| 616 | die(_("invalid refspec '%s'"), refspec); |
| 617 | spec_src = spec.src; |
| 618 | if (!*spec_src || !strcmp(spec_src, "HEAD")) |
| 619 | spec_src = "HEAD"; |
| 620 | else if (skip_prefix(spec_src, "heads/", &spec_src)) |
| 621 | ; |
| 622 | else if (skip_prefix(spec_src, "refs/heads/", &spec_src)) |
| 623 | ; |
| 624 | else if (starts_with(spec_src, "refs/") || |
| 625 | starts_with(spec_src, "tags/") || |
| 626 | starts_with(spec_src, "remotes/")) |
| 627 | spec_src = ""; |
| 628 | |
| 629 | if (*spec_src) { |
| 630 | if (!strcmp(remote, ".")) |
| 631 | merge_branch = mkpath("refs/heads/%s", spec_src); |
| 632 | else |
| 633 | merge_branch = mkpath("refs/remotes/%s/%s", remote, spec_src); |
| 634 | } else |
| 635 | merge_branch = NULL; |
| 636 | |
| 637 | refspec_item_clear(&spec); |
| 638 | return merge_branch; |
| 639 | } |
| 640 | |
| 641 | /** |
| 642 | * Given the repo and refspecs, sets fork_point to the point at which the |
| 643 | * current branch forked from its remote-tracking branch. Returns 0 on success, |
| 644 | * -1 on failure. |
| 645 | */ |
| 646 | static int get_rebase_fork_point(struct object_id *fork_point, const char *repo, |
| 647 | const char *refspec) |
| 648 | { |
| 649 | int ret; |
| 650 | struct branch *curr_branch; |
| 651 | const char *remote_branch; |
| 652 | struct child_process cp = CHILD_PROCESS_INIT; |
| 653 | struct strbuf sb = STRBUF_INIT; |
| 654 | |
| 655 | curr_branch = branch_get("HEAD"); |
| 656 | if (!curr_branch) |
| 657 | return -1; |
| 658 | |
| 659 | if (refspec) |
| 660 | remote_branch = get_tracking_branch(repo, refspec); |
| 661 | else |
| 662 | remote_branch = get_upstream_branch(repo); |
| 663 | |
| 664 | if (!remote_branch) |
| 665 | return -1; |
| 666 | |
| 667 | strvec_pushl(&cp.args, "merge-base", "--fork-point", |
| 668 | remote_branch, curr_branch->name, NULL); |
| 669 | cp.no_stdin = 1; |
| 670 | cp.no_stderr = 1; |
| 671 | cp.git_cmd = 1; |
| 672 | |
| 673 | ret = capture_command(&cp, &sb, GIT_MAX_HEXSZ); |
| 674 | if (ret) |
| 675 | goto cleanup; |
| 676 | |
| 677 | ret = get_oid_hex(sb.buf, fork_point); |
| 678 | if (ret) |
| 679 | goto cleanup; |
| 680 | |
| 681 | cleanup: |
| 682 | strbuf_release(&sb); |
| 683 | return ret ? -1 : 0; |
| 684 | } |
| 685 | |
| 686 | /** |
| 687 | * Sets merge_base to the octopus merge base of curr_head, merge_head and |
| 688 | * fork_point. Returns 0 if a merge base is found, 1 otherwise. |
| 689 | */ |
| 690 | static int get_octopus_merge_base(struct object_id *merge_base, |
| 691 | const struct object_id *curr_head, |
| 692 | const struct object_id *merge_head, |
| 693 | const struct object_id *fork_point) |
| 694 | { |
| 695 | struct commit_list *revs = NULL, *result = NULL; |
| 696 | |
| 697 | commit_list_insert(lookup_commit_reference(the_repository, curr_head), |
| 698 | &revs); |
| 699 | commit_list_insert(lookup_commit_reference(the_repository, merge_head), |
| 700 | &revs); |
| 701 | if (!is_null_oid(fork_point)) |
| 702 | commit_list_insert(lookup_commit_reference(the_repository, fork_point), |
| 703 | &revs); |
| 704 | |
| 705 | if (get_octopus_merge_bases(revs, &result) < 0) |
| 706 | exit(128); |
| 707 | commit_list_free(revs); |
| 708 | reduce_heads_replace(&result); |
| 709 | |
| 710 | if (!result) |
| 711 | return 1; |
| 712 | |
| 713 | oidcpy(merge_base, &result->item->object.oid); |
| 714 | commit_list_free(result); |
| 715 | return 0; |
| 716 | } |
| 717 | |
| 718 | /** |
| 719 | * Given the current HEAD oid, the merge head returned from git-fetch and the |
| 720 | * fork point calculated by get_rebase_fork_point(), compute the <newbase> and |
| 721 | * <upstream> arguments to use for the upcoming git-rebase invocation. |
| 722 | */ |
| 723 | static int get_rebase_newbase_and_upstream(struct object_id *newbase, |
| 724 | struct object_id *upstream, |
| 725 | const struct object_id *curr_head, |
| 726 | const struct object_id *merge_head, |
| 727 | const struct object_id *fork_point) |
| 728 | { |
| 729 | struct object_id oct_merge_base; |
| 730 | |
| 731 | if (!get_octopus_merge_base(&oct_merge_base, curr_head, merge_head, fork_point)) |
| 732 | if (!is_null_oid(fork_point) && oideq(&oct_merge_base, fork_point)) |
| 733 | fork_point = NULL; |
| 734 | |
| 735 | if (fork_point && !is_null_oid(fork_point)) |
| 736 | oidcpy(upstream, fork_point); |
| 737 | else |
| 738 | oidcpy(upstream, merge_head); |
| 739 | |
| 740 | oidcpy(newbase, merge_head); |
| 741 | |
| 742 | return 0; |
| 743 | } |
| 744 | |
| 745 | /** |
| 746 | * Given the <newbase> and <upstream> calculated by |
| 747 | * get_rebase_newbase_and_upstream(), runs git-rebase with the |
| 748 | * appropriate arguments and returns its exit status. |
| 749 | */ |
| 750 | static int run_rebase(const struct object_id *newbase, |
| 751 | const struct object_id *upstream) |
| 752 | { |
| 753 | struct child_process cmd = CHILD_PROCESS_INIT; |
| 754 | |
| 755 | strvec_push(&cmd.args, "rebase"); |
| 756 | |
| 757 | /* Shared options */ |
| 758 | argv_push_verbosity(&cmd.args); |
| 759 | |
| 760 | /* Options passed to git-rebase */ |
| 761 | if (opt_rebase == REBASE_MERGES) |
| 762 | strvec_push(&cmd.args, "--rebase-merges"); |
| 763 | else if (opt_rebase == REBASE_INTERACTIVE) |
| 764 | strvec_push(&cmd.args, "--interactive"); |
| 765 | if (opt_diffstat) |
| 766 | strvec_push(&cmd.args, opt_diffstat); |
| 767 | strvec_pushv(&cmd.args, opt_strategies.v); |
| 768 | strvec_pushv(&cmd.args, opt_strategy_opts.v); |
| 769 | if (opt_gpg_sign) |
| 770 | strvec_push(&cmd.args, opt_gpg_sign); |
| 771 | if (opt_signoff) |
| 772 | strvec_push(&cmd.args, opt_signoff); |
| 773 | if (opt_autostash == 0) |
| 774 | strvec_push(&cmd.args, "--no-autostash"); |
| 775 | else if (opt_autostash == 1) |
| 776 | strvec_push(&cmd.args, "--autostash"); |
| 777 | if (opt_verify_signatures && |
| 778 | !strcmp(opt_verify_signatures, "--verify-signatures")) |
| 779 | warning(_("ignoring --verify-signatures for rebase")); |
| 780 | |
| 781 | strvec_push(&cmd.args, "--onto"); |
| 782 | strvec_push(&cmd.args, oid_to_hex(newbase)); |
| 783 | |
| 784 | strvec_push(&cmd.args, oid_to_hex(upstream)); |
| 785 | |
| 786 | cmd.git_cmd = 1; |
| 787 | return run_command(&cmd); |
| 788 | } |
| 789 | |
| 790 | static int get_can_ff(struct object_id *orig_head, |
| 791 | struct oid_array *merge_heads) |
| 792 | { |
| 793 | int ret; |
| 794 | struct commit_list *list = NULL; |
| 795 | struct commit *merge_head, *head; |
| 796 | struct object_id *orig_merge_head; |
| 797 | |
| 798 | if (merge_heads->nr > 1) |
| 799 | return 0; |
| 800 | |
| 801 | orig_merge_head = &merge_heads->oid[0]; |
| 802 | head = lookup_commit_reference(the_repository, orig_head); |
| 803 | commit_list_insert(head, &list); |
| 804 | merge_head = lookup_commit_reference(the_repository, orig_merge_head); |
| 805 | ret = repo_is_descendant_of(the_repository, merge_head, list); |
| 806 | commit_list_free(list); |
| 807 | if (ret < 0) |
| 808 | exit(128); |
| 809 | return ret; |
| 810 | } |
| 811 | |
| 812 | /* |
| 813 | * Is orig_head a descendant of _all_ merge_heads? |
| 814 | * Unfortunately is_descendant_of() cannot be used as it asks |
| 815 | * if orig_head is a descendant of at least one of them. |
| 816 | */ |
| 817 | static int already_up_to_date(struct object_id *orig_head, |
| 818 | struct oid_array *merge_heads) |
| 819 | { |
| 820 | struct commit *ours; |
| 821 | |
| 822 | ours = lookup_commit_reference(the_repository, orig_head); |
| 823 | for (size_t i = 0; i < merge_heads->nr; i++) { |
| 824 | struct commit_list *list = NULL; |
| 825 | struct commit *theirs; |
| 826 | int ok; |
| 827 | |
| 828 | theirs = lookup_commit_reference(the_repository, &merge_heads->oid[i]); |
| 829 | commit_list_insert(theirs, &list); |
| 830 | ok = repo_is_descendant_of(the_repository, ours, list); |
| 831 | commit_list_free(list); |
| 832 | if (ok < 0) |
| 833 | exit(128); |
| 834 | if (!ok) |
| 835 | return 0; |
| 836 | } |
| 837 | return 1; |
| 838 | } |
| 839 | |
| 840 | static void show_advice_pull_non_ff(void) |
| 841 | { |
| 842 | advise(_("You have divergent branches and need to specify how to reconcile them.\n" |
| 843 | "You can do so by running one of the following commands sometime before\n" |
| 844 | "your next pull:\n" |
| 845 | "\n" |
| 846 | " git config pull.rebase false # merge\n" |
| 847 | " git config pull.rebase true # rebase\n" |
| 848 | " git config pull.ff only # fast-forward only\n" |
| 849 | "\n" |
| 850 | "You can replace \"git config\" with \"git config --global\" to set a default\n" |
| 851 | "preference for all repositories. You can also pass --rebase, --no-rebase,\n" |
| 852 | "or --ff-only on the command line to override the configured default per\n" |
| 853 | "invocation.\n")); |
| 854 | } |
| 855 | |
| 856 | int cmd_pull(int argc, |
| 857 | const char **argv, |
| 858 | const char *prefix, |
| 859 | struct repository *repository UNUSED) |
| 860 | { |
| 861 | const char *repo, **refspecs; |
| 862 | struct oid_array merge_heads = OID_ARRAY_INIT; |
| 863 | struct object_id orig_head, curr_head; |
| 864 | struct object_id rebase_fork_point; |
| 865 | int rebase_unspecified = 0; |
| 866 | int can_ff; |
| 867 | int divergent; |
| 868 | int ret; |
| 869 | static struct option pull_options[] = { |
| 870 | /* Shared options */ |
| 871 | OPT__VERBOSITY(&opt_verbosity), |
| 872 | OPT_PASSTHRU(0, "progress", &opt_progress, NULL, |
| 873 | N_("force progress reporting"), |
| 874 | PARSE_OPT_NOARG), |
| 875 | OPT_CALLBACK_F(0, "recurse-submodules", |
| 876 | &recurse_submodules_cli, N_("on-demand"), |
| 877 | N_("control for recursive fetching of submodules"), |
| 878 | PARSE_OPT_OPTARG, option_fetch_parse_recurse_submodules), |
| 879 | |
| 880 | /* Options passed to git-merge or git-rebase */ |
| 881 | OPT_GROUP(N_("Options related to merging")), |
| 882 | OPT_CALLBACK_F('r', "rebase", &opt_rebase, |
| 883 | "(false|true|merges|interactive)", |
| 884 | N_("incorporate changes by rebasing rather than merging"), |
| 885 | PARSE_OPT_OPTARG, parse_opt_rebase), |
| 886 | OPT_PASSTHRU('n', NULL, &opt_diffstat, NULL, |
| 887 | N_("do not show a diffstat at the end of the merge"), |
| 888 | PARSE_OPT_NOARG | PARSE_OPT_NONEG), |
| 889 | OPT_PASSTHRU(0, "stat", &opt_diffstat, NULL, |
| 890 | N_("show a diffstat at the end of the merge"), |
| 891 | PARSE_OPT_NOARG), |
| 892 | OPT_PASSTHRU(0, "summary", &opt_diffstat, NULL, |
| 893 | N_("(synonym to --stat)"), |
| 894 | PARSE_OPT_NOARG | PARSE_OPT_HIDDEN), |
| 895 | OPT_PASSTHRU(0, "compact-summary", &opt_diffstat, NULL, |
| 896 | N_("show a compact-summary at the end of the merge"), |
| 897 | PARSE_OPT_NOARG), |
| 898 | OPT_PASSTHRU(0, "log", &opt_log, N_("n"), |
| 899 | N_("add (at most <n>) entries from shortlog to merge commit message"), |
| 900 | PARSE_OPT_OPTARG), |
| 901 | OPT_PASSTHRU(0, "signoff", &opt_signoff, NULL, |
| 902 | N_("add a Signed-off-by trailer"), |
| 903 | PARSE_OPT_OPTARG), |
| 904 | OPT_PASSTHRU(0, "squash", &opt_squash, NULL, |
| 905 | N_("create a single commit instead of doing a merge"), |
| 906 | PARSE_OPT_NOARG), |
| 907 | OPT_PASSTHRU(0, "commit", &opt_commit, NULL, |
| 908 | N_("perform a commit if the merge succeeds (default)"), |
| 909 | PARSE_OPT_NOARG), |
| 910 | OPT_PASSTHRU(0, "edit", &opt_edit, NULL, |
| 911 | N_("edit message before committing"), |
| 912 | PARSE_OPT_NOARG), |
| 913 | OPT_CLEANUP(&cleanup_arg), |
| 914 | OPT_PASSTHRU(0, "ff", &opt_ff, NULL, |
| 915 | N_("allow fast-forward"), |
| 916 | PARSE_OPT_NOARG), |
| 917 | OPT_PASSTHRU(0, "ff-only", &opt_ff, NULL, |
| 918 | N_("abort if fast-forward is not possible"), |
| 919 | PARSE_OPT_NOARG | PARSE_OPT_NONEG), |
| 920 | OPT_PASSTHRU(0, "verify", &opt_verify, NULL, |
| 921 | N_("control use of pre-merge-commit and commit-msg hooks"), |
| 922 | PARSE_OPT_NOARG), |
| 923 | OPT_PASSTHRU(0, "verify-signatures", &opt_verify_signatures, NULL, |
| 924 | N_("verify that the named commit has a valid GPG signature"), |
| 925 | PARSE_OPT_NOARG), |
| 926 | OPT_BOOL(0, "autostash", &opt_autostash, |
| 927 | N_("automatically stash/stash pop before and after")), |
| 928 | OPT_PASSTHRU_ARGV('s', "strategy", &opt_strategies, N_("strategy"), |
| 929 | N_("merge strategy to use"), |
| 930 | 0), |
| 931 | OPT_PASSTHRU_ARGV('X', "strategy-option", &opt_strategy_opts, |
| 932 | N_("option=value"), |
| 933 | N_("option for selected merge strategy"), |
| 934 | 0), |
| 935 | OPT_PASSTHRU('S', "gpg-sign", &opt_gpg_sign, N_("key-id"), |
| 936 | N_("GPG sign commit"), |
| 937 | PARSE_OPT_OPTARG), |
| 938 | OPT_SET_INT(0, "allow-unrelated-histories", |
| 939 | &opt_allow_unrelated_histories, |
| 940 | N_("allow merging unrelated histories"), 1), |
| 941 | |
| 942 | /* Options passed to git-fetch */ |
| 943 | OPT_GROUP(N_("Options related to fetching")), |
| 944 | OPT_PASSTHRU(0, "all", &opt_all, NULL, |
| 945 | N_("fetch from all remotes"), |
| 946 | PARSE_OPT_NOARG), |
| 947 | OPT_PASSTHRU('a', "append", &opt_append, NULL, |
| 948 | N_("append to .git/FETCH_HEAD instead of overwriting"), |
| 949 | PARSE_OPT_NOARG), |
| 950 | OPT_PASSTHRU(0, "upload-pack", &opt_upload_pack, N_("path"), |
| 951 | N_("path to upload pack on remote end"), |
| 952 | 0), |
| 953 | OPT__FORCE(&opt_force, N_("force overwrite of local branch"), 0), |
| 954 | OPT_PASSTHRU('t', "tags", &opt_tags, NULL, |
| 955 | N_("fetch all tags and associated objects"), |
| 956 | PARSE_OPT_NOARG), |
| 957 | OPT_PASSTHRU('p', "prune", &opt_prune, NULL, |
| 958 | N_("prune remote-tracking branches no longer on remote"), |
| 959 | PARSE_OPT_NOARG), |
| 960 | OPT_PASSTHRU('j', "jobs", &max_children, N_("n"), |
| 961 | N_("number of submodules pulled in parallel"), |
| 962 | PARSE_OPT_OPTARG), |
| 963 | OPT_BOOL(0, "dry-run", &opt_dry_run, |
| 964 | N_("dry run")), |
| 965 | OPT_PASSTHRU('k', "keep", &opt_keep, NULL, |
| 966 | N_("keep downloaded pack"), |
| 967 | PARSE_OPT_NOARG), |
| 968 | OPT_PASSTHRU(0, "depth", &opt_depth, N_("depth"), |
| 969 | N_("deepen history of shallow clone"), |
| 970 | 0), |
| 971 | OPT_PASSTHRU_ARGV(0, "shallow-since", &opt_fetch, N_("time"), |
| 972 | N_("deepen history of shallow repository based on time"), |
| 973 | 0), |
| 974 | OPT_PASSTHRU_ARGV(0, "shallow-exclude", &opt_fetch, N_("ref"), |
| 975 | N_("deepen history of shallow clone, excluding ref"), |
| 976 | 0), |
| 977 | OPT_PASSTHRU_ARGV(0, "deepen", &opt_fetch, N_("n"), |
| 978 | N_("deepen history of shallow clone"), |
| 979 | 0), |
| 980 | OPT_PASSTHRU(0, "unshallow", &opt_unshallow, NULL, |
| 981 | N_("convert to a complete repository"), |
| 982 | PARSE_OPT_NONEG | PARSE_OPT_NOARG), |
| 983 | OPT_PASSTHRU(0, "update-shallow", &opt_update_shallow, NULL, |
| 984 | N_("accept refs that update .git/shallow"), |
| 985 | PARSE_OPT_NOARG), |
| 986 | OPT_PASSTHRU(0, "refmap", &opt_refmap, N_("refmap"), |
| 987 | N_("specify fetch refmap"), |
| 988 | PARSE_OPT_NONEG), |
| 989 | OPT_PASSTHRU_ARGV('o', "server-option", &opt_fetch, |
| 990 | N_("server-specific"), |
| 991 | N_("option to transmit"), |
| 992 | 0), |
| 993 | OPT_PASSTHRU('4', "ipv4", &opt_ipv4, NULL, |
| 994 | N_("use IPv4 addresses only"), |
| 995 | PARSE_OPT_NOARG), |
| 996 | OPT_PASSTHRU('6', "ipv6", &opt_ipv6, NULL, |
| 997 | N_("use IPv6 addresses only"), |
| 998 | PARSE_OPT_NOARG), |
| 999 | OPT_PASSTHRU_ARGV(0, "negotiation-restrict", &opt_fetch, N_("revision"), |
| 1000 | N_("report that we have only objects reachable from this object"), |
| 1001 | 0), |
| 1002 | OPT_ALIAS(0, "negotiation-tip", "negotiation-restrict"), |
| 1003 | OPT_PASSTHRU_ARGV(0, "negotiation-include", &opt_fetch, N_("revision"), |
| 1004 | N_("ensure this ref is always sent as a negotiation have"), |
| 1005 | 0), |
| 1006 | OPT_BOOL(0, "show-forced-updates", &opt_show_forced_updates, |
| 1007 | N_("check for forced-updates on all updated branches")), |
| 1008 | OPT_PASSTHRU(0, "set-upstream", &set_upstream, NULL, |
| 1009 | N_("set upstream for git pull/fetch"), |
| 1010 | PARSE_OPT_NOARG), |
| 1011 | |
| 1012 | OPT_END() |
| 1013 | }; |
| 1014 | |
| 1015 | if (!getenv("GIT_REFLOG_ACTION")) |
| 1016 | set_reflog_message(argc, argv); |
| 1017 | |
| 1018 | repo_config(the_repository, git_pull_config, NULL); |
| 1019 | if (the_repository->gitdir) { |
| 1020 | prepare_repo_settings(the_repository); |
| 1021 | the_repository->settings.command_requires_full_index = 0; |
| 1022 | } |
| 1023 | |
| 1024 | argc = parse_options(argc, argv, prefix, pull_options, pull_usage, 0); |
| 1025 | if (opt_autostash == -1) |
| 1026 | opt_autostash = config_pull_autostash; |
| 1027 | |
| 1028 | if (recurse_submodules_cli != RECURSE_SUBMODULES_DEFAULT) |
| 1029 | recurse_submodules = recurse_submodules_cli; |
| 1030 | |
| 1031 | if (cleanup_arg) |
| 1032 | /* |
| 1033 | * this only checks the validity of cleanup_arg; we don't need |
| 1034 | * a valid value for use_editor |
| 1035 | */ |
| 1036 | get_cleanup_mode(cleanup_arg, 0); |
| 1037 | |
| 1038 | parse_repo_refspecs(argc, argv, &repo, &refspecs); |
| 1039 | |
| 1040 | if (!opt_ff) { |
| 1041 | opt_ff = xstrdup_or_null(config_get_ff()); |
| 1042 | /* |
| 1043 | * A subtle point: opt_ff was set on the line above via |
| 1044 | * reading from config. opt_rebase, in contrast, is set |
| 1045 | * before this point via command line options. The setting |
| 1046 | * of opt_rebase via reading from config (using |
| 1047 | * config_get_rebase()) does not happen until later. We |
| 1048 | * are relying on the next if-condition happening before |
| 1049 | * the config_get_rebase() call so that an explicit |
| 1050 | * "--rebase" can override a config setting of |
| 1051 | * pull.ff=only. |
| 1052 | */ |
| 1053 | if (opt_rebase >= 0 && opt_ff && !strcmp(opt_ff, "--ff-only")) { |
| 1054 | free(opt_ff); |
| 1055 | opt_ff = xstrdup("--ff"); |
| 1056 | } |
| 1057 | } |
| 1058 | |
| 1059 | if (opt_rebase < 0) |
| 1060 | opt_rebase = config_get_rebase(&rebase_unspecified); |
| 1061 | |
| 1062 | if (repo_read_index_unmerged(the_repository)) |
| 1063 | die_resolve_conflict("pull"); |
| 1064 | |
| 1065 | if (file_exists(git_path_merge_head(the_repository))) |
| 1066 | die_conclude_merge(); |
| 1067 | |
| 1068 | if (repo_get_oid(the_repository, "HEAD", &orig_head)) |
| 1069 | oidclr(&orig_head, the_repository->hash_algo); |
| 1070 | |
| 1071 | if (opt_rebase) { |
| 1072 | if (opt_autostash == -1) |
| 1073 | opt_autostash = config_rebase_autostash; |
| 1074 | |
| 1075 | if (is_null_oid(&orig_head) && !is_index_unborn(the_repository->index)) |
| 1076 | die(_("Updating an unborn branch with changes added to the index.")); |
| 1077 | |
| 1078 | if (!opt_autostash) |
| 1079 | require_clean_work_tree(the_repository, |
| 1080 | N_("pull with rebase"), |
| 1081 | _("Please commit or stash them."), 1, 0); |
| 1082 | |
| 1083 | if (get_rebase_fork_point(&rebase_fork_point, repo, *refspecs)) |
| 1084 | oidclr(&rebase_fork_point, the_repository->hash_algo); |
| 1085 | } |
| 1086 | |
| 1087 | if (run_fetch(repo, refspecs)) |
| 1088 | return 1; |
| 1089 | |
| 1090 | if (opt_dry_run) |
| 1091 | return 0; |
| 1092 | |
| 1093 | if (repo_get_oid(the_repository, "HEAD", &curr_head)) |
| 1094 | oidclr(&curr_head, the_repository->hash_algo); |
| 1095 | |
| 1096 | if (!is_null_oid(&orig_head) && !is_null_oid(&curr_head) && |
| 1097 | !oideq(&orig_head, &curr_head)) { |
| 1098 | /* |
| 1099 | * The fetch involved updating the current branch. |
| 1100 | * |
| 1101 | * The working tree and the index file are still based on |
| 1102 | * orig_head commit, but we are merging into curr_head. |
| 1103 | * Update the working tree to match curr_head. |
| 1104 | */ |
| 1105 | |
| 1106 | warning(_("fetch updated the current branch head.\n" |
| 1107 | "fast-forwarding your working tree from\n" |
| 1108 | "commit %s."), oid_to_hex(&orig_head)); |
| 1109 | |
| 1110 | if (checkout_fast_forward(the_repository, &orig_head, |
| 1111 | &curr_head, 0)) |
| 1112 | die(_("Cannot fast-forward your working tree.\n" |
| 1113 | "After making sure that you saved anything precious from\n" |
| 1114 | "$ git diff %s\n" |
| 1115 | "output, run\n" |
| 1116 | "$ git reset --hard\n" |
| 1117 | "to recover."), oid_to_hex(&orig_head)); |
| 1118 | } |
| 1119 | |
| 1120 | get_merge_heads(&merge_heads); |
| 1121 | |
| 1122 | if (!merge_heads.nr) |
| 1123 | die_no_merge_candidates(repo, refspecs); |
| 1124 | |
| 1125 | if (is_null_oid(&orig_head)) { |
| 1126 | if (merge_heads.nr > 1) |
| 1127 | die(_("Cannot merge multiple branches into empty head.")); |
| 1128 | ret = pull_into_void(merge_heads.oid, &curr_head); |
| 1129 | goto cleanup; |
| 1130 | } |
| 1131 | if (merge_heads.nr > 1) { |
| 1132 | if (opt_rebase) |
| 1133 | die(_("Cannot rebase onto multiple branches.")); |
| 1134 | if (opt_ff && !strcmp(opt_ff, "--ff-only")) |
| 1135 | die(_("Cannot fast-forward to multiple branches.")); |
| 1136 | } |
| 1137 | |
| 1138 | can_ff = get_can_ff(&orig_head, &merge_heads); |
| 1139 | divergent = !can_ff && !already_up_to_date(&orig_head, &merge_heads); |
| 1140 | |
| 1141 | /* ff-only takes precedence over rebase */ |
| 1142 | if (opt_ff && !strcmp(opt_ff, "--ff-only")) { |
| 1143 | if (divergent) |
| 1144 | die_ff_impossible(); |
| 1145 | opt_rebase = REBASE_FALSE; |
| 1146 | } |
| 1147 | /* If no action specified and we can't fast forward, then warn. */ |
| 1148 | if (!opt_ff && rebase_unspecified && divergent) { |
| 1149 | show_advice_pull_non_ff(); |
| 1150 | die(_("Need to specify how to reconcile divergent branches.")); |
| 1151 | } |
| 1152 | |
| 1153 | if (opt_rebase) { |
| 1154 | struct object_id newbase; |
| 1155 | struct object_id upstream; |
| 1156 | get_rebase_newbase_and_upstream(&newbase, &upstream, &curr_head, |
| 1157 | merge_heads.oid, &rebase_fork_point); |
| 1158 | |
| 1159 | if ((recurse_submodules == RECURSE_SUBMODULES_ON || |
| 1160 | recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND) && |
| 1161 | submodule_touches_in_range(the_repository, &upstream, &curr_head)) |
| 1162 | die(_("cannot rebase with locally recorded submodule modifications")); |
| 1163 | |
| 1164 | if (can_ff) { |
| 1165 | /* we can fast-forward this without invoking rebase */ |
| 1166 | free(opt_ff); |
| 1167 | opt_ff = xstrdup("--ff-only"); |
| 1168 | ret = run_merge(); |
| 1169 | } else { |
| 1170 | ret = run_rebase(&newbase, &upstream); |
| 1171 | } |
| 1172 | |
| 1173 | if (!ret && (recurse_submodules == RECURSE_SUBMODULES_ON || |
| 1174 | recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND)) |
| 1175 | ret = rebase_submodules(); |
| 1176 | |
| 1177 | goto cleanup; |
| 1178 | } else { |
| 1179 | ret = run_merge(); |
| 1180 | if (!ret && (recurse_submodules == RECURSE_SUBMODULES_ON || |
| 1181 | recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND)) |
| 1182 | ret = update_submodules(); |
| 1183 | goto cleanup; |
| 1184 | } |
| 1185 | |
| 1186 | cleanup: |
| 1187 | oid_array_clear(&merge_heads); |
| 1188 | return ret; |
| 1189 | } |