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#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"
28
29enum rebase_type {
30 REBASE_INVALID = -1,
31 REBASE_FALSE = 0,
32 REBASE_TRUE,
33 REBASE_PRESERVE,
34 REBASE_MERGES,
35 REBASE_INTERACTIVE
36};
37
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.
44 */
45static enum rebase_type parse_config_rebase(const char *key, const char *value,
46 int fatal)
47{
48 int v = git_parse_maybe_bool(value);
49
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;
60 /*
61 * Please update _git_config() in git-completion.bash when you
62 * add new rebase modes.
63 */
64
65 if (fatal)
66 die(_("Invalid value for %s: %s"), key, value);
67 else
68 error(_("Invalid value for %s: %s"), key, value);
69
70 return REBASE_INVALID;
71}
72
73/**
74 * Callback for --rebase, which parses arg with parse_config_rebase().
75 */
76static int parse_opt_rebase(const struct option *opt, const char *arg, int unset)
77{
78 enum rebase_type *value = opt->value;
79
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;
85}
86
87static const char * const pull_usage[] = {
88 N_("git pull [<options>] [<repository> [<refspec>...]]"),
89 NULL
90};
91
92/* Shared options */
93static int opt_verbosity;
94static char *opt_progress;
95static int recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
96
97/* Options passed to git-merge or git-rebase */
98static enum rebase_type opt_rebase = -1;
99static char *opt_diffstat;
100static char *opt_log;
101static char *opt_signoff;
102static char *opt_squash;
103static char *opt_commit;
104static char *opt_edit;
105static char *cleanup_arg;
106static char *opt_ff;
107static char *opt_verify_signatures;
108static int opt_autostash = -1;
109static int config_autostash;
110static struct argv_array opt_strategies = ARGV_ARRAY_INIT;
111static struct argv_array opt_strategy_opts = ARGV_ARRAY_INIT;
112static char *opt_gpg_sign;
113static int opt_allow_unrelated_histories;
114
115/* Options passed to git-fetch */
116static char *opt_all;
117static char *opt_append;
118static char *opt_upload_pack;
119static int opt_force;
120static char *opt_tags;
121static char *opt_prune;
122static char *max_children;
123static int opt_dry_run;
124static char *opt_keep;
125static char *opt_depth;
126static char *opt_unshallow;
127static char *opt_update_shallow;
128static char *opt_refmap;
129static char *opt_ipv4;
130static char *opt_ipv6;
131static int opt_show_forced_updates = -1;
132static char *set_upstream;
133
134static 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 },
144
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"),
189 0),
190 OPT_PASSTHRU_ARGV('X', "strategy-option", &opt_strategy_opts,
191 N_("option=value"),
192 N_("option for selected merge strategy"),
193 0),
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),
200
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"),
211 0),
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"),
229 0),
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),
250
251 OPT_END()
252};
253
254/**
255 * Pushes "-q" or "-v" switches into arr to match the opt_verbosity level.
256 */
257static void argv_push_verbosity(struct argv_array *arr)
258{
259 int verbosity;
260
261 for (verbosity = opt_verbosity; verbosity > 0; verbosity--)
262 argv_array_push(arr, "-v");
263
264 for (verbosity = opt_verbosity; verbosity < 0; verbosity++)
265 argv_array_push(arr, "-q");
266}
267
268/**
269 * Pushes "-f" switches into arr to match the opt_force level.
270 */
271static void argv_push_force(struct argv_array *arr)
272{
273 int force = opt_force;
274 while (force-- > 0)
275 argv_array_push(arr, "-f");
276}
277
278/**
279 * Sets the GIT_REFLOG_ACTION environment variable to the concatenation of argv
280 */
281static void set_reflog_message(int argc, const char **argv)
282{
283 int i;
284 struct strbuf msg = STRBUF_INIT;
285
286 for (i = 0; i < argc; i++) {
287 if (i)
288 strbuf_addch(&msg, ' ');
289 strbuf_addstr(&msg, argv[i]);
290 }
291
292 setenv("GIT_REFLOG_ACTION", msg.buf, 0);
293
294 strbuf_release(&msg);
295}
296
297/**
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.
302 */
303static const char *config_get_ff(void)
304{
305 const char *value;
306
307 if (git_config_get_value("pull.ff", &value))
308 return NULL;
309
310 switch (git_parse_maybe_bool(value)) {
311 case 0:
312 return "--no-ff";
313 case 1:
314 return "--ff";
315 }
316
317 if (!strcmp(value, "only"))
318 return "--ff-only";
319
320 die(_("Invalid value for pull.ff: %s"), value);
321}
322
323/**
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.
329 */
330static enum rebase_type config_get_rebase(void)
331{
332 struct branch *curr_branch = branch_get("HEAD");
333 const char *value;
334
335 if (curr_branch) {
336 char *key = xstrfmt("branch.%s.rebase", curr_branch->name);
337
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;
342 }
343
344 free(key);
345 }
346
347 if (!git_config_get_value("pull.rebase", &value))
348 return parse_config_rebase("pull.rebase", value, 1);
349
350 return REBASE_FALSE;
351}
352
353/**
354 * Read config variables.
355 */
356static int git_pull_config(const char *var, const char *value, void *cb)
357{
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;
365 }
366 return git_default_config(var, value, cb);
367}
368
369/**
370 * Appends merge candidates from FETCH_HEAD that are not marked not-for-merge
371 * into merge_heads.
372 */
373static void get_merge_heads(struct oid_array *merge_heads)
374{
375 const char *filename = git_path_fetch_head(the_repository);
376 FILE *fp;
377 struct strbuf sb = STRBUF_INIT;
378 struct object_id oid;
379
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);
388 }
389 fclose(fp);
390 strbuf_release(&sb);
391}
392
393/**
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.
396 */
397static int get_only_remote(struct remote *remote, void *cb_data)
398{
399 const char **remote_name = cb_data;
400
401 if (*remote_name)
402 return -1;
403
404 *remote_name = remote->name;
405 return 0;
406}
407
408/**
409 * Dies with the appropriate reason for why there are no merge candidates:
410 *
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.
414 *
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.
418 *
419 * 3. We fetched from the branch's or repo's default remote, but:
420 *
421 * a. We are not on a branch, so there will never be a configured branch to
422 * merge with.
423 *
424 * b. We are on a branch, but there is no configured branch to merge with.
425 *
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.)
429 */
430static void NORETURN die_no_merge_candidates(const char *repo, const char **refspecs)
431{
432 struct branch *curr_branch = branch_get("HEAD");
433 const char *remote = curr_branch ? curr_branch->remote_name : NULL;
434
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;
459
460 if (for_each_remote(get_only_remote, &remote_name) || !remote_name)
461 remote_name = _("<remote>");
462
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);
481}
482
483/**
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.
487 */
488static void parse_repo_refspecs(int argc, const char **argv, const char **repo,
489 const char ***refspecs)
490{
491 if (argc > 0) {
492 *repo = *argv++;
493 argc--;
494 } else
495 *repo = NULL;
496 *refspecs = argv;
497}
498
499/**
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.
502 */
503static int run_fetch(const char *repo, const char **refspecs)
504{
505 struct argv_array args = ARGV_ARRAY_INIT;
506 int ret;
507
508 argv_array_pushl(&args, "fetch", "--update-head-ok", NULL);
509
510 /* Shared options */
511 argv_push_verbosity(&args);
512 if (opt_progress)
513 argv_array_push(&args, opt_progress);
514
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");
540 }
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);
565
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;
574}
575
576/**
577 * "Pulls into void" by branching off merge_head.
578 */
579static int pull_into_void(const struct object_id *merge_head,
580 const struct object_id *curr_head)
581{
582 if (opt_verify_signatures) {
583 struct commit *commit;
584
585 commit = lookup_commit(the_repository, merge_head);
586 if (!commit)
587 die(_("unable to access commit %s"),
588 oid_to_hex(merge_head));
589
590 verify_merge_signature(commit, opt_verbosity);
591 }
592
593 /*
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.
598 */
599 if (checkout_fast_forward(the_repository,
600 the_hash_algo->empty_tree,
601 merge_head, 0))
602 return 1;
603
604 if (update_ref("initial pull", "HEAD", merge_head, curr_head, 0, UPDATE_REFS_DIE_ON_ERR))
605 return 1;
606
607 return 0;
608}
609
610static int rebase_submodules(void)
611{
612 struct child_process cp = CHILD_PROCESS_INIT;
613
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);
619
620 return run_command(&cp);
621}
622
623static int update_submodules(void)
624{
625 struct child_process cp = CHILD_PROCESS_INIT;
626
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);
632
633 return run_command(&cp);
634}
635
636/**
637 * Runs git-merge, returning its exit status.
638 */
639static int run_merge(void)
640{
641 int ret;
642 struct argv_array args = ARGV_ARRAY_INIT;
643
644 argv_array_pushl(&args, "merge", NULL);
645
646 /* Shared options */
647 argv_push_verbosity(&args);
648 if (opt_progress)
649 argv_array_push(&args, opt_progress);
650
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");
676
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;
681}
682
683/**
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.
689 */
690static const char *get_upstream_branch(const char *remote)
691{
692 struct remote *rm;
693 struct branch *curr_branch;
694 const char *curr_branch_remote;
695
696 rm = remote_get(remote);
697 if (!rm)
698 return NULL;
699
700 curr_branch = branch_get("HEAD");
701 if (!curr_branch)
702 return NULL;
703
704 curr_branch_remote = remote_for_branch(curr_branch, NULL);
705 assert(curr_branch_remote);
706
707 if (strcmp(curr_branch_remote, rm->name))
708 return NULL;
709
710 return branch_get_upstream(curr_branch, NULL);
711}
712
713/**
714 * Derives the remote-tracking branch from the remote and refspec.
715 *
716 * FIXME: The current implementation assumes the default mapping of
717 * refs/heads/<branch_name> to refs/remotes/<remote_name>/<branch_name>.
718 */
719static const char *get_tracking_branch(const char *remote, const char *refspec)
720{
721 struct refspec_item spec;
722 const char *spec_src;
723 const char *merge_branch;
724
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))
730 ;
731 else if (skip_prefix(spec_src, "refs/heads/", &spec_src))
732 ;
733 else if (starts_with(spec_src, "refs/") ||
734 starts_with(spec_src, "tags/") ||
735 starts_with(spec_src, "remotes/"))
736 spec_src = "";
737
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;
745
746 refspec_item_clear(&spec);
747 return merge_branch;
748}
749
750/**
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.
754 */
755static int get_rebase_fork_point(struct object_id *fork_point, const char *repo,
756 const char *refspec)
757{
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;
763
764 curr_branch = branch_get("HEAD");
765 if (!curr_branch)
766 return -1;
767
768 if (refspec)
769 remote_branch = get_tracking_branch(repo, refspec);
770 else
771 remote_branch = get_upstream_branch(repo);
772
773 if (!remote_branch)
774 return -1;
775
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;
781
782 ret = capture_command(&cp, &sb, GIT_MAX_HEXSZ);
783 if (ret)
784 goto cleanup;
785
786 ret = get_oid_hex(sb.buf, fork_point);
787 if (ret)
788 goto cleanup;
789
790cleanup:
791 strbuf_release(&sb);
792 return ret ? -1 : 0;
793}
794
795/**
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.
798 */
799static 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)
803{
804 struct commit_list *revs = NULL, *result;
805
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);
813
814 result = get_octopus_merge_bases(revs);
815 free_commit_list(revs);
816 reduce_heads_replace(&result);
817
818 if (!result)
819 return 1;
820
821 oidcpy(merge_base, &result->item->object.oid);
822 free_commit_list(result);
823 return 0;
824}
825
826/**
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.
830 */
831static int run_rebase(const struct object_id *curr_head,
832 const struct object_id *merge_head,
833 const struct object_id *fork_point)
834{
835 int ret;
836 struct object_id oct_merge_base;
837 struct argv_array args = ARGV_ARRAY_INIT;
838
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;
842
843 argv_array_push(&args, "rebase");
844
845 /* Shared options */
846 argv_push_verbosity(&args);
847
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"));
868
869 argv_array_push(&args, "--onto");
870 argv_array_push(&args, oid_to_hex(merge_head));
871
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));
876
877 ret = run_command_v_opt(args.argv, RUN_GIT_CMD);
878 argv_array_clear(&args);
879 return ret;
880}
881
882int cmd_pull(int argc, const char **argv, const char *prefix)
883{
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;
889
890 if (!getenv("GIT_REFLOG_ACTION"))
891 set_reflog_message(argc, argv);
892
893 git_config(git_pull_config, NULL);
894
895 argc = parse_options(argc, argv, prefix, pull_options, pull_usage, 0);
896
897 if (cleanup_arg)
898 /*
899 * this only checks the validity of cleanup_arg; we don't need
900 * a valid value for use_editor
901 */
902 get_cleanup_mode(cleanup_arg, 0);
903
904 parse_repo_refspecs(argc, argv, &repo, &refspecs);
905
906 if (!opt_ff)
907 opt_ff = xstrdup_or_null(config_get_ff());
908
909 if (opt_rebase < 0)
910 opt_rebase = config_get_rebase();
911
912 if (read_cache_unmerged())
913 die_resolve_conflict("pull");
914
915 if (file_exists(git_path_merge_head(the_repository)))
916 die_conclude_merge();
917
918 if (get_oid("HEAD", &orig_head))
919 oidclr(&orig_head);
920
921 if (!opt_rebase && opt_autostash != -1)
922 die(_("--[no-]autostash option is only valid with --rebase."));
923
924 autostash = config_autostash;
925 if (opt_rebase) {
926 if (opt_autostash != -1)
927 autostash = opt_autostash;
928
929 if (is_null_oid(&orig_head) && !is_cache_unborn())
930 die(_("Updating an unborn branch with changes added to the index."));
931
932 if (!autostash)
933 require_clean_work_tree(the_repository,
934 N_("pull with rebase"),
935 _("please commit or stash them."), 1, 0);
936
937 if (get_rebase_fork_point(&rebase_fork_point, repo, *refspecs))
938 oidclr(&rebase_fork_point);
939 }
940
941 if (run_fetch(repo, refspecs))
942 return 1;
943
944 if (opt_dry_run)
945 return 0;
946
947 if (get_oid("HEAD", &curr_head))
948 oidclr(&curr_head);
949
950 if (!is_null_oid(&orig_head) && !is_null_oid(&curr_head) &&
951 !oideq(&orig_head, &curr_head)) {
952 /*
953 * The fetch involved updating the current branch.
954 *
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.
958 */
959
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));
963
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));
972 }
973
974 get_merge_heads(&merge_heads);
975
976 if (!merge_heads.nr)
977 die_no_merge_candidates(repo, refspecs);
978
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);
983 }
984 if (opt_rebase && merge_heads.nr > 1)
985 die(_("Cannot rebase onto multiple branches."));
986
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;
996
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();
1006 }
1007 }
1008 ret = run_rebase(&curr_head, merge_heads.oid, &rebase_fork_point);
1009
1010 if (!ret && (recurse_submodules == RECURSE_SUBMODULES_ON ||
1011 recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND))
1012 ret = rebase_submodules();
1013
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;
1021 }
1022}