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.
10 #include "parse-options.h"
12 #include "run-command.h"
13 #include "sha1-array.h"
26 * Parses the value of --rebase. If value is a false value, returns
27 * REBASE_FALSE. If value is a true value, returns REBASE_TRUE. If value is
28 * "preserve", returns REBASE_PRESERVE. If value is a invalid value, dies with
29 * a fatal error if fatal is true, otherwise returns REBASE_INVALID.
31 static enum rebase_type
parse_config_rebase(const char *key
, const char *value
,
34 int v
= git_config_maybe_bool("pull.rebase", value
);
40 else if (!strcmp(value
, "preserve"))
41 return REBASE_PRESERVE
;
44 die(_("Invalid value for %s: %s"), key
, value
);
46 error(_("Invalid value for %s: %s"), key
, value
);
48 return REBASE_INVALID
;
52 * Callback for --rebase, which parses arg with parse_config_rebase().
54 static int parse_opt_rebase(const struct option
*opt
, const char *arg
, int unset
)
56 enum rebase_type
*value
= opt
->value
;
59 *value
= parse_config_rebase("--rebase", arg
, 0);
61 *value
= unset
? REBASE_FALSE
: REBASE_TRUE
;
62 return *value
== REBASE_INVALID
? -1 : 0;
65 static const char * const pull_usage
[] = {
66 N_("git pull [options] [<repository> [<refspec>...]]"),
71 static int opt_verbosity
;
72 static char *opt_progress
;
74 /* Options passed to git-merge or git-rebase */
75 static enum rebase_type opt_rebase
;
76 static char *opt_diffstat
;
78 static char *opt_squash
;
79 static char *opt_commit
;
80 static char *opt_edit
;
82 static char *opt_verify_signatures
;
83 static struct argv_array opt_strategies
= ARGV_ARRAY_INIT
;
84 static struct argv_array opt_strategy_opts
= ARGV_ARRAY_INIT
;
85 static char *opt_gpg_sign
;
87 /* Options passed to git-fetch */
89 static char *opt_append
;
90 static char *opt_upload_pack
;
92 static char *opt_tags
;
93 static char *opt_prune
;
94 static char *opt_recurse_submodules
;
95 static int opt_dry_run
;
96 static char *opt_keep
;
97 static char *opt_depth
;
98 static char *opt_unshallow
;
99 static char *opt_update_shallow
;
100 static char *opt_refmap
;
102 static struct option pull_options
[] = {
104 OPT__VERBOSITY(&opt_verbosity
),
105 OPT_PASSTHRU(0, "progress", &opt_progress
, NULL
,
106 N_("force progress reporting"),
109 /* Options passed to git-merge or git-rebase */
110 OPT_GROUP(N_("Options related to merging")),
111 { OPTION_CALLBACK
, 'r', "rebase", &opt_rebase
,
112 N_("false|true|preserve"),
113 N_("incorporate changes by rebasing rather than merging"),
114 PARSE_OPT_OPTARG
, parse_opt_rebase
},
115 OPT_PASSTHRU('n', NULL
, &opt_diffstat
, NULL
,
116 N_("do not show a diffstat at the end of the merge"),
117 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
),
118 OPT_PASSTHRU(0, "stat", &opt_diffstat
, NULL
,
119 N_("show a diffstat at the end of the merge"),
121 OPT_PASSTHRU(0, "summary", &opt_diffstat
, NULL
,
122 N_("(synonym to --stat)"),
123 PARSE_OPT_NOARG
| PARSE_OPT_HIDDEN
),
124 OPT_PASSTHRU(0, "log", &opt_log
, N_("n"),
125 N_("add (at most <n>) entries from shortlog to merge commit message"),
127 OPT_PASSTHRU(0, "squash", &opt_squash
, NULL
,
128 N_("create a single commit instead of doing a merge"),
130 OPT_PASSTHRU(0, "commit", &opt_commit
, NULL
,
131 N_("perform a commit if the merge succeeds (default)"),
133 OPT_PASSTHRU(0, "edit", &opt_edit
, NULL
,
134 N_("edit message before committing"),
136 OPT_PASSTHRU(0, "ff", &opt_ff
, NULL
,
137 N_("allow fast-forward"),
139 OPT_PASSTHRU(0, "ff-only", &opt_ff
, NULL
,
140 N_("abort if fast-forward is not possible"),
141 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
),
142 OPT_PASSTHRU(0, "verify-signatures", &opt_verify_signatures
, NULL
,
143 N_("verify that the named commit has a valid GPG signature"),
145 OPT_PASSTHRU_ARGV('s', "strategy", &opt_strategies
, N_("strategy"),
146 N_("merge strategy to use"),
148 OPT_PASSTHRU_ARGV('X', "strategy-option", &opt_strategy_opts
,
150 N_("option for selected merge strategy"),
152 OPT_PASSTHRU('S', "gpg-sign", &opt_gpg_sign
, N_("key-id"),
153 N_("GPG sign commit"),
156 /* Options passed to git-fetch */
157 OPT_GROUP(N_("Options related to fetching")),
158 OPT_PASSTHRU(0, "all", &opt_all
, NULL
,
159 N_("fetch from all remotes"),
161 OPT_PASSTHRU('a', "append", &opt_append
, NULL
,
162 N_("append to .git/FETCH_HEAD instead of overwriting"),
164 OPT_PASSTHRU(0, "upload-pack", &opt_upload_pack
, N_("path"),
165 N_("path to upload pack on remote end"),
167 OPT__FORCE(&opt_force
, N_("force overwrite of local branch")),
168 OPT_PASSTHRU('t', "tags", &opt_tags
, NULL
,
169 N_("fetch all tags and associated objects"),
171 OPT_PASSTHRU('p', "prune", &opt_prune
, NULL
,
172 N_("prune remote-tracking branches no longer on remote"),
174 OPT_PASSTHRU(0, "recurse-submodules", &opt_recurse_submodules
,
176 N_("control recursive fetching of submodules"),
178 OPT_BOOL(0, "dry-run", &opt_dry_run
,
180 OPT_PASSTHRU('k', "keep", &opt_keep
, NULL
,
181 N_("keep downloaded pack"),
183 OPT_PASSTHRU(0, "depth", &opt_depth
, N_("depth"),
184 N_("deepen history of shallow clone"),
186 OPT_PASSTHRU(0, "unshallow", &opt_unshallow
, NULL
,
187 N_("convert to a complete repository"),
188 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
),
189 OPT_PASSTHRU(0, "update-shallow", &opt_update_shallow
, NULL
,
190 N_("accept refs that update .git/shallow"),
192 OPT_PASSTHRU(0, "refmap", &opt_refmap
, N_("refmap"),
193 N_("specify fetch refmap"),
200 * Pushes "-q" or "-v" switches into arr to match the opt_verbosity level.
202 static void argv_push_verbosity(struct argv_array
*arr
)
206 for (verbosity
= opt_verbosity
; verbosity
> 0; verbosity
--)
207 argv_array_push(arr
, "-v");
209 for (verbosity
= opt_verbosity
; verbosity
< 0; verbosity
++)
210 argv_array_push(arr
, "-q");
214 * Pushes "-f" switches into arr to match the opt_force level.
216 static void argv_push_force(struct argv_array
*arr
)
218 int force
= opt_force
;
220 argv_array_push(arr
, "-f");
224 * Sets the GIT_REFLOG_ACTION environment variable to the concatenation of argv
226 static void set_reflog_message(int argc
, const char **argv
)
229 struct strbuf msg
= STRBUF_INIT
;
231 for (i
= 0; i
< argc
; i
++) {
233 strbuf_addch(&msg
, ' ');
234 strbuf_addstr(&msg
, argv
[i
]);
237 setenv("GIT_REFLOG_ACTION", msg
.buf
, 0);
239 strbuf_release(&msg
);
243 * If pull.ff is unset, returns NULL. If pull.ff is "true", returns "--ff". If
244 * pull.ff is "false", returns "--no-ff". If pull.ff is "only", returns
245 * "--ff-only". Otherwise, if pull.ff is set to an invalid value, die with an
248 static const char *config_get_ff(void)
252 if (git_config_get_value("pull.ff", &value
))
255 switch (git_config_maybe_bool("pull.ff", value
)) {
262 if (!strcmp(value
, "only"))
265 die(_("Invalid value for pull.ff: %s"), value
);
269 * Appends merge candidates from FETCH_HEAD that are not marked not-for-merge
272 static void get_merge_heads(struct sha1_array
*merge_heads
)
274 const char *filename
= git_path("FETCH_HEAD");
276 struct strbuf sb
= STRBUF_INIT
;
277 unsigned char sha1
[GIT_SHA1_RAWSZ
];
279 if (!(fp
= fopen(filename
, "r")))
280 die_errno(_("could not open '%s' for reading"), filename
);
281 while (strbuf_getline(&sb
, fp
, '\n') != EOF
) {
282 if (get_sha1_hex(sb
.buf
, sha1
))
283 continue; /* invalid line: does not start with SHA1 */
284 if (starts_with(sb
.buf
+ GIT_SHA1_HEXSZ
, "\tnot-for-merge\t"))
285 continue; /* ref is not-for-merge */
286 sha1_array_append(merge_heads
, sha1
);
293 * Used by die_no_merge_candidates() as a for_each_remote() callback to
294 * retrieve the name of the remote if the repository only has one remote.
296 static int get_only_remote(struct remote
*remote
, void *cb_data
)
298 const char **remote_name
= cb_data
;
303 *remote_name
= remote
->name
;
308 * Dies with the appropriate reason for why there are no merge candidates:
310 * 1. We fetched from a specific remote, and a refspec was given, but it ended
311 * up not fetching anything. This is usually because the user provided a
312 * wildcard refspec which had no matches on the remote end.
314 * 2. We fetched from a non-default remote, but didn't specify a branch to
315 * merge. We can't use the configured one because it applies to the default
316 * remote, thus the user must specify the branches to merge.
318 * 3. We fetched from the branch's or repo's default remote, but:
320 * a. We are not on a branch, so there will never be a configured branch to
323 * b. We are on a branch, but there is no configured branch to merge with.
325 * 4. We fetched from the branch's or repo's default remote, but the configured
326 * branch to merge didn't get fetched. (Either it doesn't exist, or wasn't
327 * part of the configured fetch refspec.)
329 static void NORETURN
die_no_merge_candidates(const char *repo
, const char **refspecs
)
331 struct branch
*curr_branch
= branch_get("HEAD");
332 const char *remote
= curr_branch
? curr_branch
->remote_name
: NULL
;
335 fprintf_ln(stderr
, _("There are no candidates for merging among the refs that you just fetched."));
336 fprintf_ln(stderr
, _("Generally this means that you provided a wildcard refspec which had no\n"
337 "matches on the remote end."));
338 } else if (repo
&& curr_branch
&& (!remote
|| strcmp(repo
, remote
))) {
339 fprintf_ln(stderr
, _("You asked to pull from the remote '%s', but did not specify\n"
340 "a branch. Because this is not the default configured remote\n"
341 "for your current branch, you must specify a branch on the command line."),
343 } else if (!curr_branch
) {
344 fprintf_ln(stderr
, _("You are not currently on a branch."));
345 fprintf_ln(stderr
, _("Please specify which branch you want to merge with."));
346 fprintf_ln(stderr
, _("See git-pull(1) for details."));
347 fprintf(stderr
, "\n");
348 fprintf_ln(stderr
, " git pull <remote> <branch>");
349 fprintf(stderr
, "\n");
350 } else if (!curr_branch
->merge_nr
) {
351 const char *remote_name
= NULL
;
353 if (for_each_remote(get_only_remote
, &remote_name
) || !remote_name
)
354 remote_name
= "<remote>";
356 fprintf_ln(stderr
, _("There is no tracking information for the current branch."));
357 fprintf_ln(stderr
, _("Please specify which branch you want to merge with."));
358 fprintf_ln(stderr
, _("See git-pull(1) for details."));
359 fprintf(stderr
, "\n");
360 fprintf_ln(stderr
, " git pull <remote> <branch>");
361 fprintf(stderr
, "\n");
362 fprintf_ln(stderr
, _("If you wish to set tracking information for this branch you can do so with:\n"
364 " git branch --set-upstream-to=%s/<branch> %s\n"),
365 remote_name
, curr_branch
->name
);
367 fprintf_ln(stderr
, _("Your configuration specifies to merge with the ref '%s'\n"
368 "from the remote, but no such ref was fetched."),
369 *curr_branch
->merge_name
);
374 * Parses argv into [<repo> [<refspecs>...]], returning their values in `repo`
375 * as a string and `refspecs` as a null-terminated array of strings. If `repo`
376 * is not provided in argv, it is set to NULL.
378 static void parse_repo_refspecs(int argc
, const char **argv
, const char **repo
,
379 const char ***refspecs
)
390 * Runs git-fetch, returning its exit status. `repo` and `refspecs` are the
391 * repository and refspecs to fetch, or NULL if they are not provided.
393 static int run_fetch(const char *repo
, const char **refspecs
)
395 struct argv_array args
= ARGV_ARRAY_INIT
;
398 argv_array_pushl(&args
, "fetch", "--update-head-ok", NULL
);
401 argv_push_verbosity(&args
);
403 argv_array_push(&args
, opt_progress
);
405 /* Options passed to git-fetch */
407 argv_array_push(&args
, opt_all
);
409 argv_array_push(&args
, opt_append
);
411 argv_array_push(&args
, opt_upload_pack
);
412 argv_push_force(&args
);
414 argv_array_push(&args
, opt_tags
);
416 argv_array_push(&args
, opt_prune
);
417 if (opt_recurse_submodules
)
418 argv_array_push(&args
, opt_recurse_submodules
);
420 argv_array_push(&args
, "--dry-run");
422 argv_array_push(&args
, opt_keep
);
424 argv_array_push(&args
, opt_depth
);
426 argv_array_push(&args
, opt_unshallow
);
427 if (opt_update_shallow
)
428 argv_array_push(&args
, opt_update_shallow
);
430 argv_array_push(&args
, opt_refmap
);
433 argv_array_push(&args
, repo
);
434 argv_array_pushv(&args
, refspecs
);
435 } else if (*refspecs
)
436 die("BUG: refspecs without repo?");
437 ret
= run_command_v_opt(args
.argv
, RUN_GIT_CMD
);
438 argv_array_clear(&args
);
443 * "Pulls into void" by branching off merge_head.
445 static int pull_into_void(const unsigned char *merge_head
,
446 const unsigned char *curr_head
)
449 * Two-way merge: we treat the index as based on an empty tree,
450 * and try to fast-forward to HEAD. This ensures we will not lose
451 * index/worktree changes that the user already made on the unborn
454 if (checkout_fast_forward(EMPTY_TREE_SHA1_BIN
, merge_head
, 0))
457 if (update_ref("initial pull", "HEAD", merge_head
, curr_head
, 0, UPDATE_REFS_DIE_ON_ERR
))
464 * Runs git-merge, returning its exit status.
466 static int run_merge(void)
469 struct argv_array args
= ARGV_ARRAY_INIT
;
471 argv_array_pushl(&args
, "merge", NULL
);
474 argv_push_verbosity(&args
);
476 argv_array_push(&args
, opt_progress
);
478 /* Options passed to git-merge */
480 argv_array_push(&args
, opt_diffstat
);
482 argv_array_push(&args
, opt_log
);
484 argv_array_push(&args
, opt_squash
);
486 argv_array_push(&args
, opt_commit
);
488 argv_array_push(&args
, opt_edit
);
490 argv_array_push(&args
, opt_ff
);
491 if (opt_verify_signatures
)
492 argv_array_push(&args
, opt_verify_signatures
);
493 argv_array_pushv(&args
, opt_strategies
.argv
);
494 argv_array_pushv(&args
, opt_strategy_opts
.argv
);
496 argv_array_push(&args
, opt_gpg_sign
);
498 argv_array_push(&args
, "FETCH_HEAD");
499 ret
= run_command_v_opt(args
.argv
, RUN_GIT_CMD
);
500 argv_array_clear(&args
);
505 * Returns remote's upstream branch for the current branch. If remote is NULL,
506 * the current branch's configured default remote is used. Returns NULL if
507 * `remote` does not name a valid remote, HEAD does not point to a branch,
508 * remote is not the branch's configured remote or the branch does not have any
509 * configured upstream branch.
511 static const char *get_upstream_branch(const char *remote
)
514 struct branch
*curr_branch
;
515 const char *curr_branch_remote
;
517 rm
= remote_get(remote
);
521 curr_branch
= branch_get("HEAD");
525 curr_branch_remote
= remote_for_branch(curr_branch
, NULL
);
526 assert(curr_branch_remote
);
528 if (strcmp(curr_branch_remote
, rm
->name
))
531 return branch_get_upstream(curr_branch
, NULL
);
535 * Derives the remote tracking branch from the remote and refspec.
537 * FIXME: The current implementation assumes the default mapping of
538 * refs/heads/<branch_name> to refs/remotes/<remote_name>/<branch_name>.
540 static const char *get_tracking_branch(const char *remote
, const char *refspec
)
542 struct refspec
*spec
;
543 const char *spec_src
;
544 const char *merge_branch
;
546 spec
= parse_fetch_refspec(1, &refspec
);
547 spec_src
= spec
->src
;
548 if (!*spec_src
|| !strcmp(spec_src
, "HEAD"))
550 else if (skip_prefix(spec_src
, "heads/", &spec_src
))
552 else if (skip_prefix(spec_src
, "refs/heads/", &spec_src
))
554 else if (starts_with(spec_src
, "refs/") ||
555 starts_with(spec_src
, "tags/") ||
556 starts_with(spec_src
, "remotes/"))
560 if (!strcmp(remote
, "."))
561 merge_branch
= mkpath("refs/heads/%s", spec_src
);
563 merge_branch
= mkpath("refs/remotes/%s/%s", remote
, spec_src
);
567 free_refspec(1, spec
);
572 * Given the repo and refspecs, sets fork_point to the point at which the
573 * current branch forked from its remote tracking branch. Returns 0 on success,
576 static int get_rebase_fork_point(unsigned char *fork_point
, const char *repo
,
580 struct branch
*curr_branch
;
581 const char *remote_branch
;
582 struct child_process cp
= CHILD_PROCESS_INIT
;
583 struct strbuf sb
= STRBUF_INIT
;
585 curr_branch
= branch_get("HEAD");
590 remote_branch
= get_tracking_branch(repo
, refspec
);
592 remote_branch
= get_upstream_branch(repo
);
597 argv_array_pushl(&cp
.args
, "merge-base", "--fork-point",
598 remote_branch
, curr_branch
->name
, NULL
);
603 ret
= capture_command(&cp
, &sb
, GIT_SHA1_HEXSZ
);
607 ret
= get_sha1_hex(sb
.buf
, fork_point
);
617 * Sets merge_base to the octopus merge base of curr_head, merge_head and
618 * fork_point. Returns 0 if a merge base is found, 1 otherwise.
620 static int get_octopus_merge_base(unsigned char *merge_base
,
621 const unsigned char *curr_head
,
622 const unsigned char *merge_head
,
623 const unsigned char *fork_point
)
625 struct commit_list
*revs
= NULL
, *result
;
627 commit_list_insert(lookup_commit_reference(curr_head
), &revs
);
628 commit_list_insert(lookup_commit_reference(merge_head
), &revs
);
629 if (!is_null_sha1(fork_point
))
630 commit_list_insert(lookup_commit_reference(fork_point
), &revs
);
632 result
= reduce_heads(get_octopus_merge_bases(revs
));
633 free_commit_list(revs
);
637 hashcpy(merge_base
, result
->item
->object
.sha1
);
642 * Given the current HEAD SHA1, the merge head returned from git-fetch and the
643 * fork point calculated by get_rebase_fork_point(), runs git-rebase with the
644 * appropriate arguments and returns its exit status.
646 static int run_rebase(const unsigned char *curr_head
,
647 const unsigned char *merge_head
,
648 const unsigned char *fork_point
)
651 unsigned char oct_merge_base
[GIT_SHA1_RAWSZ
];
652 struct argv_array args
= ARGV_ARRAY_INIT
;
654 if (!get_octopus_merge_base(oct_merge_base
, curr_head
, merge_head
, fork_point
))
655 if (!is_null_sha1(fork_point
) && !hashcmp(oct_merge_base
, fork_point
))
658 argv_array_push(&args
, "rebase");
661 argv_push_verbosity(&args
);
663 /* Options passed to git-rebase */
664 if (opt_rebase
== REBASE_PRESERVE
)
665 argv_array_push(&args
, "--preserve-merges");
667 argv_array_push(&args
, opt_diffstat
);
668 argv_array_pushv(&args
, opt_strategies
.argv
);
669 argv_array_pushv(&args
, opt_strategy_opts
.argv
);
671 argv_array_push(&args
, opt_gpg_sign
);
673 argv_array_push(&args
, "--onto");
674 argv_array_push(&args
, sha1_to_hex(merge_head
));
676 if (fork_point
&& !is_null_sha1(fork_point
))
677 argv_array_push(&args
, sha1_to_hex(fork_point
));
679 argv_array_push(&args
, sha1_to_hex(merge_head
));
681 ret
= run_command_v_opt(args
.argv
, RUN_GIT_CMD
);
682 argv_array_clear(&args
);
686 int cmd_pull(int argc
, const char **argv
, const char *prefix
)
688 const char *repo
, **refspecs
;
689 struct sha1_array merge_heads
= SHA1_ARRAY_INIT
;
690 unsigned char orig_head
[GIT_SHA1_RAWSZ
], curr_head
[GIT_SHA1_RAWSZ
];
691 unsigned char rebase_fork_point
[GIT_SHA1_RAWSZ
];
693 if (!getenv("_GIT_USE_BUILTIN_PULL")) {
694 const char *path
= mkpath("%s/git-pull", git_exec_path());
696 if (sane_execvp(path
, (char **)argv
) < 0)
697 die_errno("could not exec %s", path
);
700 if (!getenv("GIT_REFLOG_ACTION"))
701 set_reflog_message(argc
, argv
);
703 argc
= parse_options(argc
, argv
, prefix
, pull_options
, pull_usage
, 0);
705 parse_repo_refspecs(argc
, argv
, &repo
, &refspecs
);
708 opt_ff
= xstrdup_or_null(config_get_ff());
710 git_config(git_default_config
, NULL
);
712 if (read_cache_unmerged())
713 die_resolve_conflict("Pull");
715 if (file_exists(git_path("MERGE_HEAD")))
716 die_conclude_merge();
718 if (get_sha1("HEAD", orig_head
))
722 if (get_rebase_fork_point(rebase_fork_point
, repo
, *refspecs
))
723 hashclr(rebase_fork_point
);
725 if (run_fetch(repo
, refspecs
))
731 if (get_sha1("HEAD", curr_head
))
734 if (!is_null_sha1(orig_head
) && !is_null_sha1(curr_head
) &&
735 hashcmp(orig_head
, curr_head
)) {
737 * The fetch involved updating the current branch.
739 * The working tree and the index file are still based on
740 * orig_head commit, but we are merging into curr_head.
741 * Update the working tree to match curr_head.
744 warning(_("fetch updated the current branch head.\n"
745 "fast-forwarding your working tree from\n"
746 "commit %s."), sha1_to_hex(orig_head
));
748 if (checkout_fast_forward(orig_head
, curr_head
, 0))
749 die(_("Cannot fast-forward your working tree.\n"
750 "After making sure that you saved anything precious from\n"
753 "$ git reset --hard\n"
754 "to recover."), sha1_to_hex(orig_head
));
757 get_merge_heads(&merge_heads
);
760 die_no_merge_candidates(repo
, refspecs
);
762 if (is_null_sha1(orig_head
)) {
763 if (merge_heads
.nr
> 1)
764 die(_("Cannot merge multiple branches into empty head."));
765 return pull_into_void(*merge_heads
.sha1
, curr_head
);
766 } else if (opt_rebase
) {
767 if (merge_heads
.nr
> 1)
768 die(_("Cannot rebase onto multiple branches."));
769 return run_rebase(curr_head
, *merge_heads
.sha1
, rebase_fork_point
);