dir: consolidate similar code in treat_directory()
[git/raj.git] / builtin / pull.c
blobd25ff13a60f2d08efdc160c359364ab94a5b19ae
1 /*
2 * Builtin "git pull"
4 * Based on git-pull.sh by Junio C Hamano
6 * Fetch one or more remote refs and merge it/them into the current HEAD.
7 */
8 #define USE_THE_INDEX_COMPATIBILITY_MACROS
9 #include "cache.h"
10 #include "config.h"
11 #include "builtin.h"
12 #include "parse-options.h"
13 #include "exec-cmd.h"
14 #include "run-command.h"
15 #include "sha1-array.h"
16 #include "remote.h"
17 #include "dir.h"
18 #include "refs.h"
19 #include "refspec.h"
20 #include "revision.h"
21 #include "submodule.h"
22 #include "submodule-config.h"
23 #include "tempfile.h"
24 #include "lockfile.h"
25 #include "wt-status.h"
26 #include "commit-reach.h"
27 #include "sequencer.h"
29 enum rebase_type {
30 REBASE_INVALID = -1,
31 REBASE_FALSE = 0,
32 REBASE_TRUE,
33 REBASE_PRESERVE,
34 REBASE_MERGES,
35 REBASE_INTERACTIVE
38 /**
39 * Parses the value of --rebase. If value is a false value, returns
40 * REBASE_FALSE. If value is a true value, returns REBASE_TRUE. If value is
41 * "merges", returns REBASE_MERGES. If value is "preserve", returns
42 * REBASE_PRESERVE. If value is a invalid value, dies with a fatal error if
43 * fatal is true, otherwise returns REBASE_INVALID.
45 static enum rebase_type parse_config_rebase(const char *key, const char *value,
46 int fatal)
48 int v = git_parse_maybe_bool(value);
50 if (!v)
51 return REBASE_FALSE;
52 else if (v > 0)
53 return REBASE_TRUE;
54 else if (!strcmp(value, "preserve") || !strcmp(value, "p"))
55 return REBASE_PRESERVE;
56 else if (!strcmp(value, "merges") || !strcmp(value, "m"))
57 return REBASE_MERGES;
58 else if (!strcmp(value, "interactive") || !strcmp(value, "i"))
59 return REBASE_INTERACTIVE;
61 * Please update _git_config() in git-completion.bash when you
62 * add new rebase modes.
65 if (fatal)
66 die(_("Invalid value for %s: %s"), key, value);
67 else
68 error(_("Invalid value for %s: %s"), key, value);
70 return REBASE_INVALID;
73 /**
74 * Callback for --rebase, which parses arg with parse_config_rebase().
76 static int parse_opt_rebase(const struct option *opt, const char *arg, int unset)
78 enum rebase_type *value = opt->value;
80 if (arg)
81 *value = parse_config_rebase("--rebase", arg, 0);
82 else
83 *value = unset ? REBASE_FALSE : REBASE_TRUE;
84 return *value == REBASE_INVALID ? -1 : 0;
87 static const char * const pull_usage[] = {
88 N_("git pull [<options>] [<repository> [<refspec>...]]"),
89 NULL
92 /* Shared options */
93 static int opt_verbosity;
94 static char *opt_progress;
95 static int recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
97 /* Options passed to git-merge or git-rebase */
98 static enum rebase_type opt_rebase = -1;
99 static char *opt_diffstat;
100 static char *opt_log;
101 static char *opt_signoff;
102 static char *opt_squash;
103 static char *opt_commit;
104 static char *opt_edit;
105 static char *cleanup_arg;
106 static char *opt_ff;
107 static char *opt_verify_signatures;
108 static int opt_autostash = -1;
109 static int config_autostash;
110 static struct argv_array opt_strategies = ARGV_ARRAY_INIT;
111 static struct argv_array opt_strategy_opts = ARGV_ARRAY_INIT;
112 static char *opt_gpg_sign;
113 static int opt_allow_unrelated_histories;
115 /* Options passed to git-fetch */
116 static char *opt_all;
117 static char *opt_append;
118 static char *opt_upload_pack;
119 static int opt_force;
120 static char *opt_tags;
121 static char *opt_prune;
122 static char *max_children;
123 static int opt_dry_run;
124 static char *opt_keep;
125 static char *opt_depth;
126 static char *opt_unshallow;
127 static char *opt_update_shallow;
128 static char *opt_refmap;
129 static char *opt_ipv4;
130 static char *opt_ipv6;
131 static int opt_show_forced_updates = -1;
132 static char *set_upstream;
134 static struct option pull_options[] = {
135 /* Shared options */
136 OPT__VERBOSITY(&opt_verbosity),
137 OPT_PASSTHRU(0, "progress", &opt_progress, NULL,
138 N_("force progress reporting"),
139 PARSE_OPT_NOARG),
140 { OPTION_CALLBACK, 0, "recurse-submodules",
141 &recurse_submodules, N_("on-demand"),
142 N_("control for recursive fetching of submodules"),
143 PARSE_OPT_OPTARG, option_fetch_parse_recurse_submodules },
145 /* Options passed to git-merge or git-rebase */
146 OPT_GROUP(N_("Options related to merging")),
147 { OPTION_CALLBACK, 'r', "rebase", &opt_rebase,
148 "(false|true|merges|preserve|interactive)",
149 N_("incorporate changes by rebasing rather than merging"),
150 PARSE_OPT_OPTARG, parse_opt_rebase },
151 OPT_PASSTHRU('n', NULL, &opt_diffstat, NULL,
152 N_("do not show a diffstat at the end of the merge"),
153 PARSE_OPT_NOARG | PARSE_OPT_NONEG),
154 OPT_PASSTHRU(0, "stat", &opt_diffstat, NULL,
155 N_("show a diffstat at the end of the merge"),
156 PARSE_OPT_NOARG),
157 OPT_PASSTHRU(0, "summary", &opt_diffstat, NULL,
158 N_("(synonym to --stat)"),
159 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN),
160 OPT_PASSTHRU(0, "log", &opt_log, N_("n"),
161 N_("add (at most <n>) entries from shortlog to merge commit message"),
162 PARSE_OPT_OPTARG),
163 OPT_PASSTHRU(0, "signoff", &opt_signoff, NULL,
164 N_("add Signed-off-by:"),
165 PARSE_OPT_OPTARG),
166 OPT_PASSTHRU(0, "squash", &opt_squash, NULL,
167 N_("create a single commit instead of doing a merge"),
168 PARSE_OPT_NOARG),
169 OPT_PASSTHRU(0, "commit", &opt_commit, NULL,
170 N_("perform a commit if the merge succeeds (default)"),
171 PARSE_OPT_NOARG),
172 OPT_PASSTHRU(0, "edit", &opt_edit, NULL,
173 N_("edit message before committing"),
174 PARSE_OPT_NOARG),
175 OPT_CLEANUP(&cleanup_arg),
176 OPT_PASSTHRU(0, "ff", &opt_ff, NULL,
177 N_("allow fast-forward"),
178 PARSE_OPT_NOARG),
179 OPT_PASSTHRU(0, "ff-only", &opt_ff, NULL,
180 N_("abort if fast-forward is not possible"),
181 PARSE_OPT_NOARG | PARSE_OPT_NONEG),
182 OPT_PASSTHRU(0, "verify-signatures", &opt_verify_signatures, NULL,
183 N_("verify that the named commit has a valid GPG signature"),
184 PARSE_OPT_NOARG),
185 OPT_BOOL(0, "autostash", &opt_autostash,
186 N_("automatically stash/stash pop before and after rebase")),
187 OPT_PASSTHRU_ARGV('s', "strategy", &opt_strategies, N_("strategy"),
188 N_("merge strategy to use"),
190 OPT_PASSTHRU_ARGV('X', "strategy-option", &opt_strategy_opts,
191 N_("option=value"),
192 N_("option for selected merge strategy"),
194 OPT_PASSTHRU('S', "gpg-sign", &opt_gpg_sign, N_("key-id"),
195 N_("GPG sign commit"),
196 PARSE_OPT_OPTARG),
197 OPT_SET_INT(0, "allow-unrelated-histories",
198 &opt_allow_unrelated_histories,
199 N_("allow merging unrelated histories"), 1),
201 /* Options passed to git-fetch */
202 OPT_GROUP(N_("Options related to fetching")),
203 OPT_PASSTHRU(0, "all", &opt_all, NULL,
204 N_("fetch from all remotes"),
205 PARSE_OPT_NOARG),
206 OPT_PASSTHRU('a', "append", &opt_append, NULL,
207 N_("append to .git/FETCH_HEAD instead of overwriting"),
208 PARSE_OPT_NOARG),
209 OPT_PASSTHRU(0, "upload-pack", &opt_upload_pack, N_("path"),
210 N_("path to upload pack on remote end"),
212 OPT__FORCE(&opt_force, N_("force overwrite of local branch"), 0),
213 OPT_PASSTHRU('t', "tags", &opt_tags, NULL,
214 N_("fetch all tags and associated objects"),
215 PARSE_OPT_NOARG),
216 OPT_PASSTHRU('p', "prune", &opt_prune, NULL,
217 N_("prune remote-tracking branches no longer on remote"),
218 PARSE_OPT_NOARG),
219 OPT_PASSTHRU('j', "jobs", &max_children, N_("n"),
220 N_("number of submodules pulled in parallel"),
221 PARSE_OPT_OPTARG),
222 OPT_BOOL(0, "dry-run", &opt_dry_run,
223 N_("dry run")),
224 OPT_PASSTHRU('k', "keep", &opt_keep, NULL,
225 N_("keep downloaded pack"),
226 PARSE_OPT_NOARG),
227 OPT_PASSTHRU(0, "depth", &opt_depth, N_("depth"),
228 N_("deepen history of shallow clone"),
230 OPT_PASSTHRU(0, "unshallow", &opt_unshallow, NULL,
231 N_("convert to a complete repository"),
232 PARSE_OPT_NONEG | PARSE_OPT_NOARG),
233 OPT_PASSTHRU(0, "update-shallow", &opt_update_shallow, NULL,
234 N_("accept refs that update .git/shallow"),
235 PARSE_OPT_NOARG),
236 OPT_PASSTHRU(0, "refmap", &opt_refmap, N_("refmap"),
237 N_("specify fetch refmap"),
238 PARSE_OPT_NONEG),
239 OPT_PASSTHRU('4', "ipv4", &opt_ipv4, NULL,
240 N_("use IPv4 addresses only"),
241 PARSE_OPT_NOARG),
242 OPT_PASSTHRU('6', "ipv6", &opt_ipv6, NULL,
243 N_("use IPv6 addresses only"),
244 PARSE_OPT_NOARG),
245 OPT_BOOL(0, "show-forced-updates", &opt_show_forced_updates,
246 N_("check for forced-updates on all updated branches")),
247 OPT_PASSTHRU(0, "set-upstream", &set_upstream, NULL,
248 N_("set upstream for git pull/fetch"),
249 PARSE_OPT_NOARG),
251 OPT_END()
255 * Pushes "-q" or "-v" switches into arr to match the opt_verbosity level.
257 static void argv_push_verbosity(struct argv_array *arr)
259 int verbosity;
261 for (verbosity = opt_verbosity; verbosity > 0; verbosity--)
262 argv_array_push(arr, "-v");
264 for (verbosity = opt_verbosity; verbosity < 0; verbosity++)
265 argv_array_push(arr, "-q");
269 * Pushes "-f" switches into arr to match the opt_force level.
271 static void argv_push_force(struct argv_array *arr)
273 int force = opt_force;
274 while (force-- > 0)
275 argv_array_push(arr, "-f");
279 * Sets the GIT_REFLOG_ACTION environment variable to the concatenation of argv
281 static void set_reflog_message(int argc, const char **argv)
283 int i;
284 struct strbuf msg = STRBUF_INIT;
286 for (i = 0; i < argc; i++) {
287 if (i)
288 strbuf_addch(&msg, ' ');
289 strbuf_addstr(&msg, argv[i]);
292 setenv("GIT_REFLOG_ACTION", msg.buf, 0);
294 strbuf_release(&msg);
298 * If pull.ff is unset, returns NULL. If pull.ff is "true", returns "--ff". If
299 * pull.ff is "false", returns "--no-ff". If pull.ff is "only", returns
300 * "--ff-only". Otherwise, if pull.ff is set to an invalid value, die with an
301 * error.
303 static const char *config_get_ff(void)
305 const char *value;
307 if (git_config_get_value("pull.ff", &value))
308 return NULL;
310 switch (git_parse_maybe_bool(value)) {
311 case 0:
312 return "--no-ff";
313 case 1:
314 return "--ff";
317 if (!strcmp(value, "only"))
318 return "--ff-only";
320 die(_("Invalid value for pull.ff: %s"), value);
324 * Returns the default configured value for --rebase. It first looks for the
325 * value of "branch.$curr_branch.rebase", where $curr_branch is the current
326 * branch, and if HEAD is detached or the configuration key does not exist,
327 * looks for the value of "pull.rebase". If both configuration keys do not
328 * exist, returns REBASE_FALSE.
330 static enum rebase_type config_get_rebase(void)
332 struct branch *curr_branch = branch_get("HEAD");
333 const char *value;
335 if (curr_branch) {
336 char *key = xstrfmt("branch.%s.rebase", curr_branch->name);
338 if (!git_config_get_value(key, &value)) {
339 enum rebase_type ret = parse_config_rebase(key, value, 1);
340 free(key);
341 return ret;
344 free(key);
347 if (!git_config_get_value("pull.rebase", &value))
348 return parse_config_rebase("pull.rebase", value, 1);
350 return REBASE_FALSE;
354 * Read config variables.
356 static int git_pull_config(const char *var, const char *value, void *cb)
358 if (!strcmp(var, "rebase.autostash")) {
359 config_autostash = git_config_bool(var, value);
360 return 0;
361 } else if (!strcmp(var, "submodule.recurse")) {
362 recurse_submodules = git_config_bool(var, value) ?
363 RECURSE_SUBMODULES_ON : RECURSE_SUBMODULES_OFF;
364 return 0;
366 return git_default_config(var, value, cb);
370 * Appends merge candidates from FETCH_HEAD that are not marked not-for-merge
371 * into merge_heads.
373 static void get_merge_heads(struct oid_array *merge_heads)
375 const char *filename = git_path_fetch_head(the_repository);
376 FILE *fp;
377 struct strbuf sb = STRBUF_INIT;
378 struct object_id oid;
380 fp = xfopen(filename, "r");
381 while (strbuf_getline_lf(&sb, fp) != EOF) {
382 const char *p;
383 if (parse_oid_hex(sb.buf, &oid, &p))
384 continue; /* invalid line: does not start with object ID */
385 if (starts_with(p, "\tnot-for-merge\t"))
386 continue; /* ref is not-for-merge */
387 oid_array_append(merge_heads, &oid);
389 fclose(fp);
390 strbuf_release(&sb);
394 * Used by die_no_merge_candidates() as a for_each_remote() callback to
395 * retrieve the name of the remote if the repository only has one remote.
397 static int get_only_remote(struct remote *remote, void *cb_data)
399 const char **remote_name = cb_data;
401 if (*remote_name)
402 return -1;
404 *remote_name = remote->name;
405 return 0;
409 * Dies with the appropriate reason for why there are no merge candidates:
411 * 1. We fetched from a specific remote, and a refspec was given, but it ended
412 * up not fetching anything. This is usually because the user provided a
413 * wildcard refspec which had no matches on the remote end.
415 * 2. We fetched from a non-default remote, but didn't specify a branch to
416 * merge. We can't use the configured one because it applies to the default
417 * remote, thus the user must specify the branches to merge.
419 * 3. We fetched from the branch's or repo's default remote, but:
421 * a. We are not on a branch, so there will never be a configured branch to
422 * merge with.
424 * b. We are on a branch, but there is no configured branch to merge with.
426 * 4. We fetched from the branch's or repo's default remote, but the configured
427 * branch to merge didn't get fetched. (Either it doesn't exist, or wasn't
428 * part of the configured fetch refspec.)
430 static void NORETURN die_no_merge_candidates(const char *repo, const char **refspecs)
432 struct branch *curr_branch = branch_get("HEAD");
433 const char *remote = curr_branch ? curr_branch->remote_name : NULL;
435 if (*refspecs) {
436 if (opt_rebase)
437 fprintf_ln(stderr, _("There is no candidate for rebasing against among the refs that you just fetched."));
438 else
439 fprintf_ln(stderr, _("There are no candidates for merging among the refs that you just fetched."));
440 fprintf_ln(stderr, _("Generally this means that you provided a wildcard refspec which had no\n"
441 "matches on the remote end."));
442 } else if (repo && curr_branch && (!remote || strcmp(repo, remote))) {
443 fprintf_ln(stderr, _("You asked to pull from the remote '%s', but did not specify\n"
444 "a branch. Because this is not the default configured remote\n"
445 "for your current branch, you must specify a branch on the command line."),
446 repo);
447 } else if (!curr_branch) {
448 fprintf_ln(stderr, _("You are not currently on a branch."));
449 if (opt_rebase)
450 fprintf_ln(stderr, _("Please specify which branch you want to rebase against."));
451 else
452 fprintf_ln(stderr, _("Please specify which branch you want to merge with."));
453 fprintf_ln(stderr, _("See git-pull(1) for details."));
454 fprintf(stderr, "\n");
455 fprintf_ln(stderr, " git pull %s %s", _("<remote>"), _("<branch>"));
456 fprintf(stderr, "\n");
457 } else if (!curr_branch->merge_nr) {
458 const char *remote_name = NULL;
460 if (for_each_remote(get_only_remote, &remote_name) || !remote_name)
461 remote_name = _("<remote>");
463 fprintf_ln(stderr, _("There is no tracking information for the current branch."));
464 if (opt_rebase)
465 fprintf_ln(stderr, _("Please specify which branch you want to rebase against."));
466 else
467 fprintf_ln(stderr, _("Please specify which branch you want to merge with."));
468 fprintf_ln(stderr, _("See git-pull(1) for details."));
469 fprintf(stderr, "\n");
470 fprintf_ln(stderr, " git pull %s %s", _("<remote>"), _("<branch>"));
471 fprintf(stderr, "\n");
472 fprintf_ln(stderr, _("If you wish to set tracking information for this branch you can do so with:"));
473 fprintf(stderr, "\n");
474 fprintf_ln(stderr, " git branch --set-upstream-to=%s/%s %s\n",
475 remote_name, _("<branch>"), curr_branch->name);
476 } else
477 fprintf_ln(stderr, _("Your configuration specifies to merge with the ref '%s'\n"
478 "from the remote, but no such ref was fetched."),
479 *curr_branch->merge_name);
480 exit(1);
484 * Parses argv into [<repo> [<refspecs>...]], returning their values in `repo`
485 * as a string and `refspecs` as a null-terminated array of strings. If `repo`
486 * is not provided in argv, it is set to NULL.
488 static void parse_repo_refspecs(int argc, const char **argv, const char **repo,
489 const char ***refspecs)
491 if (argc > 0) {
492 *repo = *argv++;
493 argc--;
494 } else
495 *repo = NULL;
496 *refspecs = argv;
500 * Runs git-fetch, returning its exit status. `repo` and `refspecs` are the
501 * repository and refspecs to fetch, or NULL if they are not provided.
503 static int run_fetch(const char *repo, const char **refspecs)
505 struct argv_array args = ARGV_ARRAY_INIT;
506 int ret;
508 argv_array_pushl(&args, "fetch", "--update-head-ok", NULL);
510 /* Shared options */
511 argv_push_verbosity(&args);
512 if (opt_progress)
513 argv_array_push(&args, opt_progress);
515 /* Options passed to git-fetch */
516 if (opt_all)
517 argv_array_push(&args, opt_all);
518 if (opt_append)
519 argv_array_push(&args, opt_append);
520 if (opt_upload_pack)
521 argv_array_push(&args, opt_upload_pack);
522 argv_push_force(&args);
523 if (opt_tags)
524 argv_array_push(&args, opt_tags);
525 if (opt_prune)
526 argv_array_push(&args, opt_prune);
527 if (recurse_submodules != RECURSE_SUBMODULES_DEFAULT)
528 switch (recurse_submodules) {
529 case RECURSE_SUBMODULES_ON:
530 argv_array_push(&args, "--recurse-submodules=on");
531 break;
532 case RECURSE_SUBMODULES_OFF:
533 argv_array_push(&args, "--recurse-submodules=no");
534 break;
535 case RECURSE_SUBMODULES_ON_DEMAND:
536 argv_array_push(&args, "--recurse-submodules=on-demand");
537 break;
538 default:
539 BUG("submodule recursion option not understood");
541 if (max_children)
542 argv_array_push(&args, max_children);
543 if (opt_dry_run)
544 argv_array_push(&args, "--dry-run");
545 if (opt_keep)
546 argv_array_push(&args, opt_keep);
547 if (opt_depth)
548 argv_array_push(&args, opt_depth);
549 if (opt_unshallow)
550 argv_array_push(&args, opt_unshallow);
551 if (opt_update_shallow)
552 argv_array_push(&args, opt_update_shallow);
553 if (opt_refmap)
554 argv_array_push(&args, opt_refmap);
555 if (opt_ipv4)
556 argv_array_push(&args, opt_ipv4);
557 if (opt_ipv6)
558 argv_array_push(&args, opt_ipv6);
559 if (opt_show_forced_updates > 0)
560 argv_array_push(&args, "--show-forced-updates");
561 else if (opt_show_forced_updates == 0)
562 argv_array_push(&args, "--no-show-forced-updates");
563 if (set_upstream)
564 argv_array_push(&args, set_upstream);
566 if (repo) {
567 argv_array_push(&args, repo);
568 argv_array_pushv(&args, refspecs);
569 } else if (*refspecs)
570 BUG("refspecs without repo?");
571 ret = run_command_v_opt(args.argv, RUN_GIT_CMD);
572 argv_array_clear(&args);
573 return ret;
577 * "Pulls into void" by branching off merge_head.
579 static int pull_into_void(const struct object_id *merge_head,
580 const struct object_id *curr_head)
582 if (opt_verify_signatures) {
583 struct commit *commit;
585 commit = lookup_commit(the_repository, merge_head);
586 if (!commit)
587 die(_("unable to access commit %s"),
588 oid_to_hex(merge_head));
590 verify_merge_signature(commit, opt_verbosity);
594 * Two-way merge: we treat the index as based on an empty tree,
595 * and try to fast-forward to HEAD. This ensures we will not lose
596 * index/worktree changes that the user already made on the unborn
597 * branch.
599 if (checkout_fast_forward(the_repository,
600 the_hash_algo->empty_tree,
601 merge_head, 0))
602 return 1;
604 if (update_ref("initial pull", "HEAD", merge_head, curr_head, 0, UPDATE_REFS_DIE_ON_ERR))
605 return 1;
607 return 0;
610 static int rebase_submodules(void)
612 struct child_process cp = CHILD_PROCESS_INIT;
614 cp.git_cmd = 1;
615 cp.no_stdin = 1;
616 argv_array_pushl(&cp.args, "submodule", "update",
617 "--recursive", "--rebase", NULL);
618 argv_push_verbosity(&cp.args);
620 return run_command(&cp);
623 static int update_submodules(void)
625 struct child_process cp = CHILD_PROCESS_INIT;
627 cp.git_cmd = 1;
628 cp.no_stdin = 1;
629 argv_array_pushl(&cp.args, "submodule", "update",
630 "--recursive", "--checkout", NULL);
631 argv_push_verbosity(&cp.args);
633 return run_command(&cp);
637 * Runs git-merge, returning its exit status.
639 static int run_merge(void)
641 int ret;
642 struct argv_array args = ARGV_ARRAY_INIT;
644 argv_array_pushl(&args, "merge", NULL);
646 /* Shared options */
647 argv_push_verbosity(&args);
648 if (opt_progress)
649 argv_array_push(&args, opt_progress);
651 /* Options passed to git-merge */
652 if (opt_diffstat)
653 argv_array_push(&args, opt_diffstat);
654 if (opt_log)
655 argv_array_push(&args, opt_log);
656 if (opt_signoff)
657 argv_array_push(&args, opt_signoff);
658 if (opt_squash)
659 argv_array_push(&args, opt_squash);
660 if (opt_commit)
661 argv_array_push(&args, opt_commit);
662 if (opt_edit)
663 argv_array_push(&args, opt_edit);
664 if (cleanup_arg)
665 argv_array_pushf(&args, "--cleanup=%s", cleanup_arg);
666 if (opt_ff)
667 argv_array_push(&args, opt_ff);
668 if (opt_verify_signatures)
669 argv_array_push(&args, opt_verify_signatures);
670 argv_array_pushv(&args, opt_strategies.argv);
671 argv_array_pushv(&args, opt_strategy_opts.argv);
672 if (opt_gpg_sign)
673 argv_array_push(&args, opt_gpg_sign);
674 if (opt_allow_unrelated_histories > 0)
675 argv_array_push(&args, "--allow-unrelated-histories");
677 argv_array_push(&args, "FETCH_HEAD");
678 ret = run_command_v_opt(args.argv, RUN_GIT_CMD);
679 argv_array_clear(&args);
680 return ret;
684 * Returns remote's upstream branch for the current branch. If remote is NULL,
685 * the current branch's configured default remote is used. Returns NULL if
686 * `remote` does not name a valid remote, HEAD does not point to a branch,
687 * remote is not the branch's configured remote or the branch does not have any
688 * configured upstream branch.
690 static const char *get_upstream_branch(const char *remote)
692 struct remote *rm;
693 struct branch *curr_branch;
694 const char *curr_branch_remote;
696 rm = remote_get(remote);
697 if (!rm)
698 return NULL;
700 curr_branch = branch_get("HEAD");
701 if (!curr_branch)
702 return NULL;
704 curr_branch_remote = remote_for_branch(curr_branch, NULL);
705 assert(curr_branch_remote);
707 if (strcmp(curr_branch_remote, rm->name))
708 return NULL;
710 return branch_get_upstream(curr_branch, NULL);
714 * Derives the remote-tracking branch from the remote and refspec.
716 * FIXME: The current implementation assumes the default mapping of
717 * refs/heads/<branch_name> to refs/remotes/<remote_name>/<branch_name>.
719 static const char *get_tracking_branch(const char *remote, const char *refspec)
721 struct refspec_item spec;
722 const char *spec_src;
723 const char *merge_branch;
725 refspec_item_init_or_die(&spec, refspec, REFSPEC_FETCH);
726 spec_src = spec.src;
727 if (!*spec_src || !strcmp(spec_src, "HEAD"))
728 spec_src = "HEAD";
729 else if (skip_prefix(spec_src, "heads/", &spec_src))
731 else if (skip_prefix(spec_src, "refs/heads/", &spec_src))
733 else if (starts_with(spec_src, "refs/") ||
734 starts_with(spec_src, "tags/") ||
735 starts_with(spec_src, "remotes/"))
736 spec_src = "";
738 if (*spec_src) {
739 if (!strcmp(remote, "."))
740 merge_branch = mkpath("refs/heads/%s", spec_src);
741 else
742 merge_branch = mkpath("refs/remotes/%s/%s", remote, spec_src);
743 } else
744 merge_branch = NULL;
746 refspec_item_clear(&spec);
747 return merge_branch;
751 * Given the repo and refspecs, sets fork_point to the point at which the
752 * current branch forked from its remote-tracking branch. Returns 0 on success,
753 * -1 on failure.
755 static int get_rebase_fork_point(struct object_id *fork_point, const char *repo,
756 const char *refspec)
758 int ret;
759 struct branch *curr_branch;
760 const char *remote_branch;
761 struct child_process cp = CHILD_PROCESS_INIT;
762 struct strbuf sb = STRBUF_INIT;
764 curr_branch = branch_get("HEAD");
765 if (!curr_branch)
766 return -1;
768 if (refspec)
769 remote_branch = get_tracking_branch(repo, refspec);
770 else
771 remote_branch = get_upstream_branch(repo);
773 if (!remote_branch)
774 return -1;
776 argv_array_pushl(&cp.args, "merge-base", "--fork-point",
777 remote_branch, curr_branch->name, NULL);
778 cp.no_stdin = 1;
779 cp.no_stderr = 1;
780 cp.git_cmd = 1;
782 ret = capture_command(&cp, &sb, GIT_MAX_HEXSZ);
783 if (ret)
784 goto cleanup;
786 ret = get_oid_hex(sb.buf, fork_point);
787 if (ret)
788 goto cleanup;
790 cleanup:
791 strbuf_release(&sb);
792 return ret ? -1 : 0;
796 * Sets merge_base to the octopus merge base of curr_head, merge_head and
797 * fork_point. Returns 0 if a merge base is found, 1 otherwise.
799 static int get_octopus_merge_base(struct object_id *merge_base,
800 const struct object_id *curr_head,
801 const struct object_id *merge_head,
802 const struct object_id *fork_point)
804 struct commit_list *revs = NULL, *result;
806 commit_list_insert(lookup_commit_reference(the_repository, curr_head),
807 &revs);
808 commit_list_insert(lookup_commit_reference(the_repository, merge_head),
809 &revs);
810 if (!is_null_oid(fork_point))
811 commit_list_insert(lookup_commit_reference(the_repository, fork_point),
812 &revs);
814 result = get_octopus_merge_bases(revs);
815 free_commit_list(revs);
816 reduce_heads_replace(&result);
818 if (!result)
819 return 1;
821 oidcpy(merge_base, &result->item->object.oid);
822 free_commit_list(result);
823 return 0;
827 * Given the current HEAD oid, the merge head returned from git-fetch and the
828 * fork point calculated by get_rebase_fork_point(), runs git-rebase with the
829 * appropriate arguments and returns its exit status.
831 static int run_rebase(const struct object_id *curr_head,
832 const struct object_id *merge_head,
833 const struct object_id *fork_point)
835 int ret;
836 struct object_id oct_merge_base;
837 struct argv_array args = ARGV_ARRAY_INIT;
839 if (!get_octopus_merge_base(&oct_merge_base, curr_head, merge_head, fork_point))
840 if (!is_null_oid(fork_point) && oideq(&oct_merge_base, fork_point))
841 fork_point = NULL;
843 argv_array_push(&args, "rebase");
845 /* Shared options */
846 argv_push_verbosity(&args);
848 /* Options passed to git-rebase */
849 if (opt_rebase == REBASE_MERGES)
850 argv_array_push(&args, "--rebase-merges");
851 else if (opt_rebase == REBASE_PRESERVE)
852 argv_array_push(&args, "--preserve-merges");
853 else if (opt_rebase == REBASE_INTERACTIVE)
854 argv_array_push(&args, "--interactive");
855 if (opt_diffstat)
856 argv_array_push(&args, opt_diffstat);
857 argv_array_pushv(&args, opt_strategies.argv);
858 argv_array_pushv(&args, opt_strategy_opts.argv);
859 if (opt_gpg_sign)
860 argv_array_push(&args, opt_gpg_sign);
861 if (opt_autostash == 0)
862 argv_array_push(&args, "--no-autostash");
863 else if (opt_autostash == 1)
864 argv_array_push(&args, "--autostash");
865 if (opt_verify_signatures &&
866 !strcmp(opt_verify_signatures, "--verify-signatures"))
867 warning(_("ignoring --verify-signatures for rebase"));
869 argv_array_push(&args, "--onto");
870 argv_array_push(&args, oid_to_hex(merge_head));
872 if (fork_point && !is_null_oid(fork_point))
873 argv_array_push(&args, oid_to_hex(fork_point));
874 else
875 argv_array_push(&args, oid_to_hex(merge_head));
877 ret = run_command_v_opt(args.argv, RUN_GIT_CMD);
878 argv_array_clear(&args);
879 return ret;
882 int cmd_pull(int argc, const char **argv, const char *prefix)
884 const char *repo, **refspecs;
885 struct oid_array merge_heads = OID_ARRAY_INIT;
886 struct object_id orig_head, curr_head;
887 struct object_id rebase_fork_point;
888 int autostash;
890 if (!getenv("GIT_REFLOG_ACTION"))
891 set_reflog_message(argc, argv);
893 git_config(git_pull_config, NULL);
895 argc = parse_options(argc, argv, prefix, pull_options, pull_usage, 0);
897 if (cleanup_arg)
899 * this only checks the validity of cleanup_arg; we don't need
900 * a valid value for use_editor
902 get_cleanup_mode(cleanup_arg, 0);
904 parse_repo_refspecs(argc, argv, &repo, &refspecs);
906 if (!opt_ff)
907 opt_ff = xstrdup_or_null(config_get_ff());
909 if (opt_rebase < 0)
910 opt_rebase = config_get_rebase();
912 if (read_cache_unmerged())
913 die_resolve_conflict("pull");
915 if (file_exists(git_path_merge_head(the_repository)))
916 die_conclude_merge();
918 if (get_oid("HEAD", &orig_head))
919 oidclr(&orig_head);
921 if (!opt_rebase && opt_autostash != -1)
922 die(_("--[no-]autostash option is only valid with --rebase."));
924 autostash = config_autostash;
925 if (opt_rebase) {
926 if (opt_autostash != -1)
927 autostash = opt_autostash;
929 if (is_null_oid(&orig_head) && !is_cache_unborn())
930 die(_("Updating an unborn branch with changes added to the index."));
932 if (!autostash)
933 require_clean_work_tree(the_repository,
934 N_("pull with rebase"),
935 _("please commit or stash them."), 1, 0);
937 if (get_rebase_fork_point(&rebase_fork_point, repo, *refspecs))
938 oidclr(&rebase_fork_point);
941 if (run_fetch(repo, refspecs))
942 return 1;
944 if (opt_dry_run)
945 return 0;
947 if (get_oid("HEAD", &curr_head))
948 oidclr(&curr_head);
950 if (!is_null_oid(&orig_head) && !is_null_oid(&curr_head) &&
951 !oideq(&orig_head, &curr_head)) {
953 * The fetch involved updating the current branch.
955 * The working tree and the index file are still based on
956 * orig_head commit, but we are merging into curr_head.
957 * Update the working tree to match curr_head.
960 warning(_("fetch updated the current branch head.\n"
961 "fast-forwarding your working tree from\n"
962 "commit %s."), oid_to_hex(&orig_head));
964 if (checkout_fast_forward(the_repository, &orig_head,
965 &curr_head, 0))
966 die(_("Cannot fast-forward your working tree.\n"
967 "After making sure that you saved anything precious from\n"
968 "$ git diff %s\n"
969 "output, run\n"
970 "$ git reset --hard\n"
971 "to recover."), oid_to_hex(&orig_head));
974 get_merge_heads(&merge_heads);
976 if (!merge_heads.nr)
977 die_no_merge_candidates(repo, refspecs);
979 if (is_null_oid(&orig_head)) {
980 if (merge_heads.nr > 1)
981 die(_("Cannot merge multiple branches into empty head."));
982 return pull_into_void(merge_heads.oid, &curr_head);
984 if (opt_rebase && merge_heads.nr > 1)
985 die(_("Cannot rebase onto multiple branches."));
987 if (opt_rebase) {
988 int ret = 0;
989 if ((recurse_submodules == RECURSE_SUBMODULES_ON ||
990 recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND) &&
991 submodule_touches_in_range(the_repository, &rebase_fork_point, &curr_head))
992 die(_("cannot rebase with locally recorded submodule modifications"));
993 if (!autostash) {
994 struct commit_list *list = NULL;
995 struct commit *merge_head, *head;
997 head = lookup_commit_reference(the_repository,
998 &orig_head);
999 commit_list_insert(head, &list);
1000 merge_head = lookup_commit_reference(the_repository,
1001 &merge_heads.oid[0]);
1002 if (is_descendant_of(merge_head, list)) {
1003 /* we can fast-forward this without invoking rebase */
1004 opt_ff = "--ff-only";
1005 ret = run_merge();
1008 ret = run_rebase(&curr_head, merge_heads.oid, &rebase_fork_point);
1010 if (!ret && (recurse_submodules == RECURSE_SUBMODULES_ON ||
1011 recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND))
1012 ret = rebase_submodules();
1014 return ret;
1015 } else {
1016 int ret = run_merge();
1017 if (!ret && (recurse_submodules == RECURSE_SUBMODULES_ON ||
1018 recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND))
1019 ret = update_submodules();
1020 return ret;