reset: fix usage
[git/git-svn.git] / builtin / pull.c
blob6214af9b10c213046e13b81fb30cea64abc0e619
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 #include "cache.h"
9 #include "builtin.h"
10 #include "parse-options.h"
11 #include "exec_cmd.h"
12 #include "run-command.h"
13 #include "sha1-array.h"
14 #include "remote.h"
15 #include "dir.h"
16 #include "refs.h"
17 #include "revision.h"
18 #include "tempfile.h"
19 #include "lockfile.h"
21 enum rebase_type {
22 REBASE_INVALID = -1,
23 REBASE_FALSE = 0,
24 REBASE_TRUE,
25 REBASE_PRESERVE,
26 REBASE_INTERACTIVE
29 /**
30 * Parses the value of --rebase. If value is a false value, returns
31 * REBASE_FALSE. If value is a true value, returns REBASE_TRUE. If value is
32 * "preserve", returns REBASE_PRESERVE. If value is a invalid value, dies with
33 * a fatal error if fatal is true, otherwise returns REBASE_INVALID.
35 static enum rebase_type parse_config_rebase(const char *key, const char *value,
36 int fatal)
38 int v = git_config_maybe_bool("pull.rebase", value);
40 if (!v)
41 return REBASE_FALSE;
42 else if (v > 0)
43 return REBASE_TRUE;
44 else if (!strcmp(value, "preserve"))
45 return REBASE_PRESERVE;
46 else if (!strcmp(value, "interactive"))
47 return REBASE_INTERACTIVE;
49 if (fatal)
50 die(_("Invalid value for %s: %s"), key, value);
51 else
52 error(_("Invalid value for %s: %s"), key, value);
54 return REBASE_INVALID;
57 /**
58 * Callback for --rebase, which parses arg with parse_config_rebase().
60 static int parse_opt_rebase(const struct option *opt, const char *arg, int unset)
62 enum rebase_type *value = opt->value;
64 if (arg)
65 *value = parse_config_rebase("--rebase", arg, 0);
66 else
67 *value = unset ? REBASE_FALSE : REBASE_TRUE;
68 return *value == REBASE_INVALID ? -1 : 0;
71 static const char * const pull_usage[] = {
72 N_("git pull [<options>] [<repository> [<refspec>...]]"),
73 NULL
76 /* Shared options */
77 static int opt_verbosity;
78 static char *opt_progress;
80 /* Options passed to git-merge or git-rebase */
81 static enum rebase_type opt_rebase = -1;
82 static char *opt_diffstat;
83 static char *opt_log;
84 static char *opt_squash;
85 static char *opt_commit;
86 static char *opt_edit;
87 static char *opt_ff;
88 static char *opt_verify_signatures;
89 static struct argv_array opt_strategies = ARGV_ARRAY_INIT;
90 static struct argv_array opt_strategy_opts = ARGV_ARRAY_INIT;
91 static char *opt_gpg_sign;
93 /* Options passed to git-fetch */
94 static char *opt_all;
95 static char *opt_append;
96 static char *opt_upload_pack;
97 static int opt_force;
98 static char *opt_tags;
99 static char *opt_prune;
100 static char *opt_recurse_submodules;
101 static char *max_children;
102 static int opt_dry_run;
103 static char *opt_keep;
104 static char *opt_depth;
105 static char *opt_unshallow;
106 static char *opt_update_shallow;
107 static char *opt_refmap;
109 static struct option pull_options[] = {
110 /* Shared options */
111 OPT__VERBOSITY(&opt_verbosity),
112 OPT_PASSTHRU(0, "progress", &opt_progress, NULL,
113 N_("force progress reporting"),
114 PARSE_OPT_NOARG),
116 /* Options passed to git-merge or git-rebase */
117 OPT_GROUP(N_("Options related to merging")),
118 { OPTION_CALLBACK, 'r', "rebase", &opt_rebase,
119 "false|true|preserve|interactive",
120 N_("incorporate changes by rebasing rather than merging"),
121 PARSE_OPT_OPTARG, parse_opt_rebase },
122 OPT_PASSTHRU('n', NULL, &opt_diffstat, NULL,
123 N_("do not show a diffstat at the end of the merge"),
124 PARSE_OPT_NOARG | PARSE_OPT_NONEG),
125 OPT_PASSTHRU(0, "stat", &opt_diffstat, NULL,
126 N_("show a diffstat at the end of the merge"),
127 PARSE_OPT_NOARG),
128 OPT_PASSTHRU(0, "summary", &opt_diffstat, NULL,
129 N_("(synonym to --stat)"),
130 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN),
131 OPT_PASSTHRU(0, "log", &opt_log, N_("n"),
132 N_("add (at most <n>) entries from shortlog to merge commit message"),
133 PARSE_OPT_OPTARG),
134 OPT_PASSTHRU(0, "squash", &opt_squash, NULL,
135 N_("create a single commit instead of doing a merge"),
136 PARSE_OPT_NOARG),
137 OPT_PASSTHRU(0, "commit", &opt_commit, NULL,
138 N_("perform a commit if the merge succeeds (default)"),
139 PARSE_OPT_NOARG),
140 OPT_PASSTHRU(0, "edit", &opt_edit, NULL,
141 N_("edit message before committing"),
142 PARSE_OPT_NOARG),
143 OPT_PASSTHRU(0, "ff", &opt_ff, NULL,
144 N_("allow fast-forward"),
145 PARSE_OPT_NOARG),
146 OPT_PASSTHRU(0, "ff-only", &opt_ff, NULL,
147 N_("abort if fast-forward is not possible"),
148 PARSE_OPT_NOARG | PARSE_OPT_NONEG),
149 OPT_PASSTHRU(0, "verify-signatures", &opt_verify_signatures, NULL,
150 N_("verify that the named commit has a valid GPG signature"),
151 PARSE_OPT_NOARG),
152 OPT_PASSTHRU_ARGV('s', "strategy", &opt_strategies, N_("strategy"),
153 N_("merge strategy to use"),
155 OPT_PASSTHRU_ARGV('X', "strategy-option", &opt_strategy_opts,
156 N_("option=value"),
157 N_("option for selected merge strategy"),
159 OPT_PASSTHRU('S', "gpg-sign", &opt_gpg_sign, N_("key-id"),
160 N_("GPG sign commit"),
161 PARSE_OPT_OPTARG),
163 /* Options passed to git-fetch */
164 OPT_GROUP(N_("Options related to fetching")),
165 OPT_PASSTHRU(0, "all", &opt_all, NULL,
166 N_("fetch from all remotes"),
167 PARSE_OPT_NOARG),
168 OPT_PASSTHRU('a', "append", &opt_append, NULL,
169 N_("append to .git/FETCH_HEAD instead of overwriting"),
170 PARSE_OPT_NOARG),
171 OPT_PASSTHRU(0, "upload-pack", &opt_upload_pack, N_("path"),
172 N_("path to upload pack on remote end"),
174 OPT__FORCE(&opt_force, N_("force overwrite of local branch")),
175 OPT_PASSTHRU('t', "tags", &opt_tags, NULL,
176 N_("fetch all tags and associated objects"),
177 PARSE_OPT_NOARG),
178 OPT_PASSTHRU('p', "prune", &opt_prune, NULL,
179 N_("prune remote-tracking branches no longer on remote"),
180 PARSE_OPT_NOARG),
181 OPT_PASSTHRU(0, "recurse-submodules", &opt_recurse_submodules,
182 N_("on-demand"),
183 N_("control recursive fetching of submodules"),
184 PARSE_OPT_OPTARG),
185 OPT_PASSTHRU('j', "jobs", &max_children, N_("n"),
186 N_("number of submodules pulled in parallel"),
187 PARSE_OPT_OPTARG),
188 OPT_BOOL(0, "dry-run", &opt_dry_run,
189 N_("dry run")),
190 OPT_PASSTHRU('k', "keep", &opt_keep, NULL,
191 N_("keep downloaded pack"),
192 PARSE_OPT_NOARG),
193 OPT_PASSTHRU(0, "depth", &opt_depth, N_("depth"),
194 N_("deepen history of shallow clone"),
196 OPT_PASSTHRU(0, "unshallow", &opt_unshallow, NULL,
197 N_("convert to a complete repository"),
198 PARSE_OPT_NONEG | PARSE_OPT_NOARG),
199 OPT_PASSTHRU(0, "update-shallow", &opt_update_shallow, NULL,
200 N_("accept refs that update .git/shallow"),
201 PARSE_OPT_NOARG),
202 OPT_PASSTHRU(0, "refmap", &opt_refmap, N_("refmap"),
203 N_("specify fetch refmap"),
204 PARSE_OPT_NONEG),
206 OPT_END()
210 * Pushes "-q" or "-v" switches into arr to match the opt_verbosity level.
212 static void argv_push_verbosity(struct argv_array *arr)
214 int verbosity;
216 for (verbosity = opt_verbosity; verbosity > 0; verbosity--)
217 argv_array_push(arr, "-v");
219 for (verbosity = opt_verbosity; verbosity < 0; verbosity++)
220 argv_array_push(arr, "-q");
224 * Pushes "-f" switches into arr to match the opt_force level.
226 static void argv_push_force(struct argv_array *arr)
228 int force = opt_force;
229 while (force-- > 0)
230 argv_array_push(arr, "-f");
234 * Sets the GIT_REFLOG_ACTION environment variable to the concatenation of argv
236 static void set_reflog_message(int argc, const char **argv)
238 int i;
239 struct strbuf msg = STRBUF_INIT;
241 for (i = 0; i < argc; i++) {
242 if (i)
243 strbuf_addch(&msg, ' ');
244 strbuf_addstr(&msg, argv[i]);
247 setenv("GIT_REFLOG_ACTION", msg.buf, 0);
249 strbuf_release(&msg);
253 * If pull.ff is unset, returns NULL. If pull.ff is "true", returns "--ff". If
254 * pull.ff is "false", returns "--no-ff". If pull.ff is "only", returns
255 * "--ff-only". Otherwise, if pull.ff is set to an invalid value, die with an
256 * error.
258 static const char *config_get_ff(void)
260 const char *value;
262 if (git_config_get_value("pull.ff", &value))
263 return NULL;
265 switch (git_config_maybe_bool("pull.ff", value)) {
266 case 0:
267 return "--no-ff";
268 case 1:
269 return "--ff";
272 if (!strcmp(value, "only"))
273 return "--ff-only";
275 die(_("Invalid value for pull.ff: %s"), value);
279 * Returns the default configured value for --rebase. It first looks for the
280 * value of "branch.$curr_branch.rebase", where $curr_branch is the current
281 * branch, and if HEAD is detached or the configuration key does not exist,
282 * looks for the value of "pull.rebase". If both configuration keys do not
283 * exist, returns REBASE_FALSE.
285 static enum rebase_type config_get_rebase(void)
287 struct branch *curr_branch = branch_get("HEAD");
288 const char *value;
290 if (curr_branch) {
291 char *key = xstrfmt("branch.%s.rebase", curr_branch->name);
293 if (!git_config_get_value(key, &value)) {
294 enum rebase_type ret = parse_config_rebase(key, value, 1);
295 free(key);
296 return ret;
299 free(key);
302 if (!git_config_get_value("pull.rebase", &value))
303 return parse_config_rebase("pull.rebase", value, 1);
305 return REBASE_FALSE;
309 * Returns 1 if there are unstaged changes, 0 otherwise.
311 static int has_unstaged_changes(const char *prefix)
313 struct rev_info rev_info;
314 int result;
316 init_revisions(&rev_info, prefix);
317 DIFF_OPT_SET(&rev_info.diffopt, IGNORE_SUBMODULES);
318 DIFF_OPT_SET(&rev_info.diffopt, QUICK);
319 diff_setup_done(&rev_info.diffopt);
320 result = run_diff_files(&rev_info, 0);
321 return diff_result_code(&rev_info.diffopt, result);
325 * Returns 1 if there are uncommitted changes, 0 otherwise.
327 static int has_uncommitted_changes(const char *prefix)
329 struct rev_info rev_info;
330 int result;
332 if (is_cache_unborn())
333 return 0;
335 init_revisions(&rev_info, prefix);
336 DIFF_OPT_SET(&rev_info.diffopt, IGNORE_SUBMODULES);
337 DIFF_OPT_SET(&rev_info.diffopt, QUICK);
338 add_head_to_pending(&rev_info);
339 diff_setup_done(&rev_info.diffopt);
340 result = run_diff_index(&rev_info, 1);
341 return diff_result_code(&rev_info.diffopt, result);
345 * If the work tree has unstaged or uncommitted changes, dies with the
346 * appropriate message.
348 static void die_on_unclean_work_tree(const char *prefix)
350 struct lock_file *lock_file = xcalloc(1, sizeof(*lock_file));
351 int do_die = 0;
353 hold_locked_index(lock_file, 0);
354 refresh_cache(REFRESH_QUIET);
355 update_index_if_able(&the_index, lock_file);
356 rollback_lock_file(lock_file);
358 if (has_unstaged_changes(prefix)) {
359 error(_("Cannot pull with rebase: You have unstaged changes."));
360 do_die = 1;
363 if (has_uncommitted_changes(prefix)) {
364 if (do_die)
365 error(_("Additionally, your index contains uncommitted changes."));
366 else
367 error(_("Cannot pull with rebase: Your index contains uncommitted changes."));
368 do_die = 1;
371 if (do_die)
372 exit(1);
376 * Appends merge candidates from FETCH_HEAD that are not marked not-for-merge
377 * into merge_heads.
379 static void get_merge_heads(struct sha1_array *merge_heads)
381 const char *filename = git_path("FETCH_HEAD");
382 FILE *fp;
383 struct strbuf sb = STRBUF_INIT;
384 unsigned char sha1[GIT_SHA1_RAWSZ];
386 if (!(fp = fopen(filename, "r")))
387 die_errno(_("could not open '%s' for reading"), filename);
388 while (strbuf_getline_lf(&sb, fp) != EOF) {
389 if (get_sha1_hex(sb.buf, sha1))
390 continue; /* invalid line: does not start with SHA1 */
391 if (starts_with(sb.buf + GIT_SHA1_HEXSZ, "\tnot-for-merge\t"))
392 continue; /* ref is not-for-merge */
393 sha1_array_append(merge_heads, sha1);
395 fclose(fp);
396 strbuf_release(&sb);
400 * Used by die_no_merge_candidates() as a for_each_remote() callback to
401 * retrieve the name of the remote if the repository only has one remote.
403 static int get_only_remote(struct remote *remote, void *cb_data)
405 const char **remote_name = cb_data;
407 if (*remote_name)
408 return -1;
410 *remote_name = remote->name;
411 return 0;
415 * Dies with the appropriate reason for why there are no merge candidates:
417 * 1. We fetched from a specific remote, and a refspec was given, but it ended
418 * up not fetching anything. This is usually because the user provided a
419 * wildcard refspec which had no matches on the remote end.
421 * 2. We fetched from a non-default remote, but didn't specify a branch to
422 * merge. We can't use the configured one because it applies to the default
423 * remote, thus the user must specify the branches to merge.
425 * 3. We fetched from the branch's or repo's default remote, but:
427 * a. We are not on a branch, so there will never be a configured branch to
428 * merge with.
430 * b. We are on a branch, but there is no configured branch to merge with.
432 * 4. We fetched from the branch's or repo's default remote, but the configured
433 * branch to merge didn't get fetched. (Either it doesn't exist, or wasn't
434 * part of the configured fetch refspec.)
436 static void NORETURN die_no_merge_candidates(const char *repo, const char **refspecs)
438 struct branch *curr_branch = branch_get("HEAD");
439 const char *remote = curr_branch ? curr_branch->remote_name : NULL;
441 if (*refspecs) {
442 if (opt_rebase)
443 fprintf_ln(stderr, _("There is no candidate for rebasing against among the refs that you just fetched."));
444 else
445 fprintf_ln(stderr, _("There are no candidates for merging among the refs that you just fetched."));
446 fprintf_ln(stderr, _("Generally this means that you provided a wildcard refspec which had no\n"
447 "matches on the remote end."));
448 } else if (repo && curr_branch && (!remote || strcmp(repo, remote))) {
449 fprintf_ln(stderr, _("You asked to pull from the remote '%s', but did not specify\n"
450 "a branch. Because this is not the default configured remote\n"
451 "for your current branch, you must specify a branch on the command line."),
452 repo);
453 } else if (!curr_branch) {
454 fprintf_ln(stderr, _("You are not currently on a branch."));
455 if (opt_rebase)
456 fprintf_ln(stderr, _("Please specify which branch you want to rebase against."));
457 else
458 fprintf_ln(stderr, _("Please specify which branch you want to merge with."));
459 fprintf_ln(stderr, _("See git-pull(1) for details."));
460 fprintf(stderr, "\n");
461 fprintf_ln(stderr, " git pull %s %s", _("<remote>"), _("<branch>"));
462 fprintf(stderr, "\n");
463 } else if (!curr_branch->merge_nr) {
464 const char *remote_name = NULL;
466 if (for_each_remote(get_only_remote, &remote_name) || !remote_name)
467 remote_name = _("<remote>");
469 fprintf_ln(stderr, _("There is no tracking information for the current branch."));
470 if (opt_rebase)
471 fprintf_ln(stderr, _("Please specify which branch you want to rebase against."));
472 else
473 fprintf_ln(stderr, _("Please specify which branch you want to merge with."));
474 fprintf_ln(stderr, _("See git-pull(1) for details."));
475 fprintf(stderr, "\n");
476 fprintf_ln(stderr, " git pull %s %s", _("<remote>"), _("<branch>"));
477 fprintf(stderr, "\n");
478 fprintf_ln(stderr, _("If you wish to set tracking information for this branch you can do so with:"));
479 fprintf(stderr, "\n");
480 fprintf_ln(stderr, " git branch --set-upstream-to=%s/%s %s\n",
481 remote_name, _("<branch>"), curr_branch->name);
482 } else
483 fprintf_ln(stderr, _("Your configuration specifies to merge with the ref '%s'\n"
484 "from the remote, but no such ref was fetched."),
485 *curr_branch->merge_name);
486 exit(1);
490 * Parses argv into [<repo> [<refspecs>...]], returning their values in `repo`
491 * as a string and `refspecs` as a null-terminated array of strings. If `repo`
492 * is not provided in argv, it is set to NULL.
494 static void parse_repo_refspecs(int argc, const char **argv, const char **repo,
495 const char ***refspecs)
497 if (argc > 0) {
498 *repo = *argv++;
499 argc--;
500 } else
501 *repo = NULL;
502 *refspecs = argv;
506 * Runs git-fetch, returning its exit status. `repo` and `refspecs` are the
507 * repository and refspecs to fetch, or NULL if they are not provided.
509 static int run_fetch(const char *repo, const char **refspecs)
511 struct argv_array args = ARGV_ARRAY_INIT;
512 int ret;
514 argv_array_pushl(&args, "fetch", "--update-head-ok", NULL);
516 /* Shared options */
517 argv_push_verbosity(&args);
518 if (opt_progress)
519 argv_array_push(&args, opt_progress);
521 /* Options passed to git-fetch */
522 if (opt_all)
523 argv_array_push(&args, opt_all);
524 if (opt_append)
525 argv_array_push(&args, opt_append);
526 if (opt_upload_pack)
527 argv_array_push(&args, opt_upload_pack);
528 argv_push_force(&args);
529 if (opt_tags)
530 argv_array_push(&args, opt_tags);
531 if (opt_prune)
532 argv_array_push(&args, opt_prune);
533 if (opt_recurse_submodules)
534 argv_array_push(&args, opt_recurse_submodules);
535 if (max_children)
536 argv_array_push(&args, max_children);
537 if (opt_dry_run)
538 argv_array_push(&args, "--dry-run");
539 if (opt_keep)
540 argv_array_push(&args, opt_keep);
541 if (opt_depth)
542 argv_array_push(&args, opt_depth);
543 if (opt_unshallow)
544 argv_array_push(&args, opt_unshallow);
545 if (opt_update_shallow)
546 argv_array_push(&args, opt_update_shallow);
547 if (opt_refmap)
548 argv_array_push(&args, opt_refmap);
550 if (repo) {
551 argv_array_push(&args, repo);
552 argv_array_pushv(&args, refspecs);
553 } else if (*refspecs)
554 die("BUG: refspecs without repo?");
555 ret = run_command_v_opt(args.argv, RUN_GIT_CMD);
556 argv_array_clear(&args);
557 return ret;
561 * "Pulls into void" by branching off merge_head.
563 static int pull_into_void(const unsigned char *merge_head,
564 const unsigned char *curr_head)
567 * Two-way merge: we treat the index as based on an empty tree,
568 * and try to fast-forward to HEAD. This ensures we will not lose
569 * index/worktree changes that the user already made on the unborn
570 * branch.
572 if (checkout_fast_forward(EMPTY_TREE_SHA1_BIN, merge_head, 0))
573 return 1;
575 if (update_ref("initial pull", "HEAD", merge_head, curr_head, 0, UPDATE_REFS_DIE_ON_ERR))
576 return 1;
578 return 0;
582 * Runs git-merge, returning its exit status.
584 static int run_merge(void)
586 int ret;
587 struct argv_array args = ARGV_ARRAY_INIT;
589 argv_array_pushl(&args, "merge", NULL);
591 /* Shared options */
592 argv_push_verbosity(&args);
593 if (opt_progress)
594 argv_array_push(&args, opt_progress);
596 /* Options passed to git-merge */
597 if (opt_diffstat)
598 argv_array_push(&args, opt_diffstat);
599 if (opt_log)
600 argv_array_push(&args, opt_log);
601 if (opt_squash)
602 argv_array_push(&args, opt_squash);
603 if (opt_commit)
604 argv_array_push(&args, opt_commit);
605 if (opt_edit)
606 argv_array_push(&args, opt_edit);
607 if (opt_ff)
608 argv_array_push(&args, opt_ff);
609 if (opt_verify_signatures)
610 argv_array_push(&args, opt_verify_signatures);
611 argv_array_pushv(&args, opt_strategies.argv);
612 argv_array_pushv(&args, opt_strategy_opts.argv);
613 if (opt_gpg_sign)
614 argv_array_push(&args, opt_gpg_sign);
616 argv_array_push(&args, "FETCH_HEAD");
617 ret = run_command_v_opt(args.argv, RUN_GIT_CMD);
618 argv_array_clear(&args);
619 return ret;
623 * Returns remote's upstream branch for the current branch. If remote is NULL,
624 * the current branch's configured default remote is used. Returns NULL if
625 * `remote` does not name a valid remote, HEAD does not point to a branch,
626 * remote is not the branch's configured remote or the branch does not have any
627 * configured upstream branch.
629 static const char *get_upstream_branch(const char *remote)
631 struct remote *rm;
632 struct branch *curr_branch;
633 const char *curr_branch_remote;
635 rm = remote_get(remote);
636 if (!rm)
637 return NULL;
639 curr_branch = branch_get("HEAD");
640 if (!curr_branch)
641 return NULL;
643 curr_branch_remote = remote_for_branch(curr_branch, NULL);
644 assert(curr_branch_remote);
646 if (strcmp(curr_branch_remote, rm->name))
647 return NULL;
649 return branch_get_upstream(curr_branch, NULL);
653 * Derives the remote tracking branch from the remote and refspec.
655 * FIXME: The current implementation assumes the default mapping of
656 * refs/heads/<branch_name> to refs/remotes/<remote_name>/<branch_name>.
658 static const char *get_tracking_branch(const char *remote, const char *refspec)
660 struct refspec *spec;
661 const char *spec_src;
662 const char *merge_branch;
664 spec = parse_fetch_refspec(1, &refspec);
665 spec_src = spec->src;
666 if (!*spec_src || !strcmp(spec_src, "HEAD"))
667 spec_src = "HEAD";
668 else if (skip_prefix(spec_src, "heads/", &spec_src))
670 else if (skip_prefix(spec_src, "refs/heads/", &spec_src))
672 else if (starts_with(spec_src, "refs/") ||
673 starts_with(spec_src, "tags/") ||
674 starts_with(spec_src, "remotes/"))
675 spec_src = "";
677 if (*spec_src) {
678 if (!strcmp(remote, "."))
679 merge_branch = mkpath("refs/heads/%s", spec_src);
680 else
681 merge_branch = mkpath("refs/remotes/%s/%s", remote, spec_src);
682 } else
683 merge_branch = NULL;
685 free_refspec(1, spec);
686 return merge_branch;
690 * Given the repo and refspecs, sets fork_point to the point at which the
691 * current branch forked from its remote tracking branch. Returns 0 on success,
692 * -1 on failure.
694 static int get_rebase_fork_point(unsigned char *fork_point, const char *repo,
695 const char *refspec)
697 int ret;
698 struct branch *curr_branch;
699 const char *remote_branch;
700 struct child_process cp = CHILD_PROCESS_INIT;
701 struct strbuf sb = STRBUF_INIT;
703 curr_branch = branch_get("HEAD");
704 if (!curr_branch)
705 return -1;
707 if (refspec)
708 remote_branch = get_tracking_branch(repo, refspec);
709 else
710 remote_branch = get_upstream_branch(repo);
712 if (!remote_branch)
713 return -1;
715 argv_array_pushl(&cp.args, "merge-base", "--fork-point",
716 remote_branch, curr_branch->name, NULL);
717 cp.no_stdin = 1;
718 cp.no_stderr = 1;
719 cp.git_cmd = 1;
721 ret = capture_command(&cp, &sb, GIT_SHA1_HEXSZ);
722 if (ret)
723 goto cleanup;
725 ret = get_sha1_hex(sb.buf, fork_point);
726 if (ret)
727 goto cleanup;
729 cleanup:
730 strbuf_release(&sb);
731 return ret ? -1 : 0;
735 * Sets merge_base to the octopus merge base of curr_head, merge_head and
736 * fork_point. Returns 0 if a merge base is found, 1 otherwise.
738 static int get_octopus_merge_base(unsigned char *merge_base,
739 const unsigned char *curr_head,
740 const unsigned char *merge_head,
741 const unsigned char *fork_point)
743 struct commit_list *revs = NULL, *result;
745 commit_list_insert(lookup_commit_reference(curr_head), &revs);
746 commit_list_insert(lookup_commit_reference(merge_head), &revs);
747 if (!is_null_sha1(fork_point))
748 commit_list_insert(lookup_commit_reference(fork_point), &revs);
750 result = reduce_heads(get_octopus_merge_bases(revs));
751 free_commit_list(revs);
752 if (!result)
753 return 1;
755 hashcpy(merge_base, result->item->object.oid.hash);
756 return 0;
760 * Given the current HEAD SHA1, the merge head returned from git-fetch and the
761 * fork point calculated by get_rebase_fork_point(), runs git-rebase with the
762 * appropriate arguments and returns its exit status.
764 static int run_rebase(const unsigned char *curr_head,
765 const unsigned char *merge_head,
766 const unsigned char *fork_point)
768 int ret;
769 unsigned char oct_merge_base[GIT_SHA1_RAWSZ];
770 struct argv_array args = ARGV_ARRAY_INIT;
772 if (!get_octopus_merge_base(oct_merge_base, curr_head, merge_head, fork_point))
773 if (!is_null_sha1(fork_point) && !hashcmp(oct_merge_base, fork_point))
774 fork_point = NULL;
776 argv_array_push(&args, "rebase");
778 /* Shared options */
779 argv_push_verbosity(&args);
781 /* Options passed to git-rebase */
782 if (opt_rebase == REBASE_PRESERVE)
783 argv_array_push(&args, "--preserve-merges");
784 else if (opt_rebase == REBASE_INTERACTIVE)
785 argv_array_push(&args, "--interactive");
786 if (opt_diffstat)
787 argv_array_push(&args, opt_diffstat);
788 argv_array_pushv(&args, opt_strategies.argv);
789 argv_array_pushv(&args, opt_strategy_opts.argv);
790 if (opt_gpg_sign)
791 argv_array_push(&args, opt_gpg_sign);
793 argv_array_push(&args, "--onto");
794 argv_array_push(&args, sha1_to_hex(merge_head));
796 if (fork_point && !is_null_sha1(fork_point))
797 argv_array_push(&args, sha1_to_hex(fork_point));
798 else
799 argv_array_push(&args, sha1_to_hex(merge_head));
801 ret = run_command_v_opt(args.argv, RUN_GIT_CMD);
802 argv_array_clear(&args);
803 return ret;
806 int cmd_pull(int argc, const char **argv, const char *prefix)
808 const char *repo, **refspecs;
809 struct sha1_array merge_heads = SHA1_ARRAY_INIT;
810 unsigned char orig_head[GIT_SHA1_RAWSZ], curr_head[GIT_SHA1_RAWSZ];
811 unsigned char rebase_fork_point[GIT_SHA1_RAWSZ];
813 if (!getenv("GIT_REFLOG_ACTION"))
814 set_reflog_message(argc, argv);
816 argc = parse_options(argc, argv, prefix, pull_options, pull_usage, 0);
818 parse_repo_refspecs(argc, argv, &repo, &refspecs);
820 if (!opt_ff)
821 opt_ff = xstrdup_or_null(config_get_ff());
823 if (opt_rebase < 0)
824 opt_rebase = config_get_rebase();
826 git_config(git_default_config, NULL);
828 if (read_cache_unmerged())
829 die_resolve_conflict("Pull");
831 if (file_exists(git_path("MERGE_HEAD")))
832 die_conclude_merge();
834 if (get_sha1("HEAD", orig_head))
835 hashclr(orig_head);
837 if (opt_rebase) {
838 int autostash = 0;
840 if (is_null_sha1(orig_head) && !is_cache_unborn())
841 die(_("Updating an unborn branch with changes added to the index."));
843 git_config_get_bool("rebase.autostash", &autostash);
844 if (!autostash)
845 die_on_unclean_work_tree(prefix);
847 if (get_rebase_fork_point(rebase_fork_point, repo, *refspecs))
848 hashclr(rebase_fork_point);
851 if (run_fetch(repo, refspecs))
852 return 1;
854 if (opt_dry_run)
855 return 0;
857 if (get_sha1("HEAD", curr_head))
858 hashclr(curr_head);
860 if (!is_null_sha1(orig_head) && !is_null_sha1(curr_head) &&
861 hashcmp(orig_head, curr_head)) {
863 * The fetch involved updating the current branch.
865 * The working tree and the index file are still based on
866 * orig_head commit, but we are merging into curr_head.
867 * Update the working tree to match curr_head.
870 warning(_("fetch updated the current branch head.\n"
871 "fast-forwarding your working tree from\n"
872 "commit %s."), sha1_to_hex(orig_head));
874 if (checkout_fast_forward(orig_head, curr_head, 0))
875 die(_("Cannot fast-forward your working tree.\n"
876 "After making sure that you saved anything precious from\n"
877 "$ git diff %s\n"
878 "output, run\n"
879 "$ git reset --hard\n"
880 "to recover."), sha1_to_hex(orig_head));
883 get_merge_heads(&merge_heads);
885 if (!merge_heads.nr)
886 die_no_merge_candidates(repo, refspecs);
888 if (is_null_sha1(orig_head)) {
889 if (merge_heads.nr > 1)
890 die(_("Cannot merge multiple branches into empty head."));
891 return pull_into_void(*merge_heads.sha1, curr_head);
892 } else if (opt_rebase) {
893 if (merge_heads.nr > 1)
894 die(_("Cannot rebase onto multiple branches."));
895 return run_rebase(curr_head, *merge_heads.sha1, rebase_fork_point);
896 } else
897 return run_merge();