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"
18 static const char * const pull_usage
[] = {
19 N_("git pull [options] [<repository> [<refspec>...]]"),
24 static int opt_verbosity
;
25 static char *opt_progress
;
27 /* Options passed to git-merge */
28 static char *opt_diffstat
;
30 static char *opt_squash
;
31 static char *opt_commit
;
32 static char *opt_edit
;
34 static char *opt_verify_signatures
;
35 static struct argv_array opt_strategies
= ARGV_ARRAY_INIT
;
36 static struct argv_array opt_strategy_opts
= ARGV_ARRAY_INIT
;
37 static char *opt_gpg_sign
;
39 /* Options passed to git-fetch */
41 static char *opt_append
;
42 static char *opt_upload_pack
;
44 static char *opt_tags
;
45 static char *opt_prune
;
46 static char *opt_recurse_submodules
;
47 static int opt_dry_run
;
48 static char *opt_keep
;
49 static char *opt_depth
;
50 static char *opt_unshallow
;
51 static char *opt_update_shallow
;
52 static char *opt_refmap
;
54 static struct option pull_options
[] = {
56 OPT__VERBOSITY(&opt_verbosity
),
57 OPT_PASSTHRU(0, "progress", &opt_progress
, NULL
,
58 N_("force progress reporting"),
61 /* Options passed to git-merge */
62 OPT_GROUP(N_("Options related to merging")),
63 OPT_PASSTHRU('n', NULL
, &opt_diffstat
, NULL
,
64 N_("do not show a diffstat at the end of the merge"),
65 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
),
66 OPT_PASSTHRU(0, "stat", &opt_diffstat
, NULL
,
67 N_("show a diffstat at the end of the merge"),
69 OPT_PASSTHRU(0, "summary", &opt_diffstat
, NULL
,
70 N_("(synonym to --stat)"),
71 PARSE_OPT_NOARG
| PARSE_OPT_HIDDEN
),
72 OPT_PASSTHRU(0, "log", &opt_log
, N_("n"),
73 N_("add (at most <n>) entries from shortlog to merge commit message"),
75 OPT_PASSTHRU(0, "squash", &opt_squash
, NULL
,
76 N_("create a single commit instead of doing a merge"),
78 OPT_PASSTHRU(0, "commit", &opt_commit
, NULL
,
79 N_("perform a commit if the merge succeeds (default)"),
81 OPT_PASSTHRU(0, "edit", &opt_edit
, NULL
,
82 N_("edit message before committing"),
84 OPT_PASSTHRU(0, "ff", &opt_ff
, NULL
,
85 N_("allow fast-forward"),
87 OPT_PASSTHRU(0, "ff-only", &opt_ff
, NULL
,
88 N_("abort if fast-forward is not possible"),
89 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
),
90 OPT_PASSTHRU(0, "verify-signatures", &opt_verify_signatures
, NULL
,
91 N_("verify that the named commit has a valid GPG signature"),
93 OPT_PASSTHRU_ARGV('s', "strategy", &opt_strategies
, N_("strategy"),
94 N_("merge strategy to use"),
96 OPT_PASSTHRU_ARGV('X', "strategy-option", &opt_strategy_opts
,
98 N_("option for selected merge strategy"),
100 OPT_PASSTHRU('S', "gpg-sign", &opt_gpg_sign
, N_("key-id"),
101 N_("GPG sign commit"),
104 /* Options passed to git-fetch */
105 OPT_GROUP(N_("Options related to fetching")),
106 OPT_PASSTHRU(0, "all", &opt_all
, NULL
,
107 N_("fetch from all remotes"),
109 OPT_PASSTHRU('a', "append", &opt_append
, NULL
,
110 N_("append to .git/FETCH_HEAD instead of overwriting"),
112 OPT_PASSTHRU(0, "upload-pack", &opt_upload_pack
, N_("path"),
113 N_("path to upload pack on remote end"),
115 OPT__FORCE(&opt_force
, N_("force overwrite of local branch")),
116 OPT_PASSTHRU('t', "tags", &opt_tags
, NULL
,
117 N_("fetch all tags and associated objects"),
119 OPT_PASSTHRU('p', "prune", &opt_prune
, NULL
,
120 N_("prune remote-tracking branches no longer on remote"),
122 OPT_PASSTHRU(0, "recurse-submodules", &opt_recurse_submodules
,
124 N_("control recursive fetching of submodules"),
126 OPT_BOOL(0, "dry-run", &opt_dry_run
,
128 OPT_PASSTHRU('k', "keep", &opt_keep
, NULL
,
129 N_("keep downloaded pack"),
131 OPT_PASSTHRU(0, "depth", &opt_depth
, N_("depth"),
132 N_("deepen history of shallow clone"),
134 OPT_PASSTHRU(0, "unshallow", &opt_unshallow
, NULL
,
135 N_("convert to a complete repository"),
136 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
),
137 OPT_PASSTHRU(0, "update-shallow", &opt_update_shallow
, NULL
,
138 N_("accept refs that update .git/shallow"),
140 OPT_PASSTHRU(0, "refmap", &opt_refmap
, N_("refmap"),
141 N_("specify fetch refmap"),
148 * Pushes "-q" or "-v" switches into arr to match the opt_verbosity level.
150 static void argv_push_verbosity(struct argv_array
*arr
)
154 for (verbosity
= opt_verbosity
; verbosity
> 0; verbosity
--)
155 argv_array_push(arr
, "-v");
157 for (verbosity
= opt_verbosity
; verbosity
< 0; verbosity
++)
158 argv_array_push(arr
, "-q");
162 * Pushes "-f" switches into arr to match the opt_force level.
164 static void argv_push_force(struct argv_array
*arr
)
166 int force
= opt_force
;
168 argv_array_push(arr
, "-f");
172 * Sets the GIT_REFLOG_ACTION environment variable to the concatenation of argv
174 static void set_reflog_message(int argc
, const char **argv
)
177 struct strbuf msg
= STRBUF_INIT
;
179 for (i
= 0; i
< argc
; i
++) {
181 strbuf_addch(&msg
, ' ');
182 strbuf_addstr(&msg
, argv
[i
]);
185 setenv("GIT_REFLOG_ACTION", msg
.buf
, 0);
187 strbuf_release(&msg
);
191 * If pull.ff is unset, returns NULL. If pull.ff is "true", returns "--ff". If
192 * pull.ff is "false", returns "--no-ff". If pull.ff is "only", returns
193 * "--ff-only". Otherwise, if pull.ff is set to an invalid value, die with an
196 static const char *config_get_ff(void)
200 if (git_config_get_value("pull.ff", &value
))
203 switch (git_config_maybe_bool("pull.ff", value
)) {
210 if (!strcmp(value
, "only"))
213 die(_("Invalid value for pull.ff: %s"), value
);
217 * Appends merge candidates from FETCH_HEAD that are not marked not-for-merge
220 static void get_merge_heads(struct sha1_array
*merge_heads
)
222 const char *filename
= git_path("FETCH_HEAD");
224 struct strbuf sb
= STRBUF_INIT
;
225 unsigned char sha1
[GIT_SHA1_RAWSZ
];
227 if (!(fp
= fopen(filename
, "r")))
228 die_errno(_("could not open '%s' for reading"), filename
);
229 while (strbuf_getline(&sb
, fp
, '\n') != EOF
) {
230 if (get_sha1_hex(sb
.buf
, sha1
))
231 continue; /* invalid line: does not start with SHA1 */
232 if (starts_with(sb
.buf
+ GIT_SHA1_HEXSZ
, "\tnot-for-merge\t"))
233 continue; /* ref is not-for-merge */
234 sha1_array_append(merge_heads
, sha1
);
241 * Used by die_no_merge_candidates() as a for_each_remote() callback to
242 * retrieve the name of the remote if the repository only has one remote.
244 static int get_only_remote(struct remote
*remote
, void *cb_data
)
246 const char **remote_name
= cb_data
;
251 *remote_name
= remote
->name
;
256 * Dies with the appropriate reason for why there are no merge candidates:
258 * 1. We fetched from a specific remote, and a refspec was given, but it ended
259 * up not fetching anything. This is usually because the user provided a
260 * wildcard refspec which had no matches on the remote end.
262 * 2. We fetched from a non-default remote, but didn't specify a branch to
263 * merge. We can't use the configured one because it applies to the default
264 * remote, thus the user must specify the branches to merge.
266 * 3. We fetched from the branch's or repo's default remote, but:
268 * a. We are not on a branch, so there will never be a configured branch to
271 * b. We are on a branch, but there is no configured branch to merge with.
273 * 4. We fetched from the branch's or repo's default remote, but the configured
274 * branch to merge didn't get fetched. (Either it doesn't exist, or wasn't
275 * part of the configured fetch refspec.)
277 static void NORETURN
die_no_merge_candidates(const char *repo
, const char **refspecs
)
279 struct branch
*curr_branch
= branch_get("HEAD");
280 const char *remote
= curr_branch
? curr_branch
->remote_name
: NULL
;
283 fprintf_ln(stderr
, _("There are no candidates for merging among the refs that you just fetched."));
284 fprintf_ln(stderr
, _("Generally this means that you provided a wildcard refspec which had no\n"
285 "matches on the remote end."));
286 } else if (repo
&& curr_branch
&& (!remote
|| strcmp(repo
, remote
))) {
287 fprintf_ln(stderr
, _("You asked to pull from the remote '%s', but did not specify\n"
288 "a branch. Because this is not the default configured remote\n"
289 "for your current branch, you must specify a branch on the command line."),
291 } else if (!curr_branch
) {
292 fprintf_ln(stderr
, _("You are not currently on a branch."));
293 fprintf_ln(stderr
, _("Please specify which branch you want to merge with."));
294 fprintf_ln(stderr
, _("See git-pull(1) for details."));
295 fprintf(stderr
, "\n");
296 fprintf_ln(stderr
, " git pull <remote> <branch>");
297 fprintf(stderr
, "\n");
298 } else if (!curr_branch
->merge_nr
) {
299 const char *remote_name
= NULL
;
301 if (for_each_remote(get_only_remote
, &remote_name
) || !remote_name
)
302 remote_name
= "<remote>";
304 fprintf_ln(stderr
, _("There is no tracking information for the current branch."));
305 fprintf_ln(stderr
, _("Please specify which branch you want to merge with."));
306 fprintf_ln(stderr
, _("See git-pull(1) for details."));
307 fprintf(stderr
, "\n");
308 fprintf_ln(stderr
, " git pull <remote> <branch>");
309 fprintf(stderr
, "\n");
310 fprintf_ln(stderr
, _("If you wish to set tracking information for this branch you can do so with:\n"
312 " git branch --set-upstream-to=%s/<branch> %s\n"),
313 remote_name
, curr_branch
->name
);
315 fprintf_ln(stderr
, _("Your configuration specifies to merge with the ref '%s'\n"
316 "from the remote, but no such ref was fetched."),
317 *curr_branch
->merge_name
);
322 * Parses argv into [<repo> [<refspecs>...]], returning their values in `repo`
323 * as a string and `refspecs` as a null-terminated array of strings. If `repo`
324 * is not provided in argv, it is set to NULL.
326 static void parse_repo_refspecs(int argc
, const char **argv
, const char **repo
,
327 const char ***refspecs
)
338 * Runs git-fetch, returning its exit status. `repo` and `refspecs` are the
339 * repository and refspecs to fetch, or NULL if they are not provided.
341 static int run_fetch(const char *repo
, const char **refspecs
)
343 struct argv_array args
= ARGV_ARRAY_INIT
;
346 argv_array_pushl(&args
, "fetch", "--update-head-ok", NULL
);
349 argv_push_verbosity(&args
);
351 argv_array_push(&args
, opt_progress
);
353 /* Options passed to git-fetch */
355 argv_array_push(&args
, opt_all
);
357 argv_array_push(&args
, opt_append
);
359 argv_array_push(&args
, opt_upload_pack
);
360 argv_push_force(&args
);
362 argv_array_push(&args
, opt_tags
);
364 argv_array_push(&args
, opt_prune
);
365 if (opt_recurse_submodules
)
366 argv_array_push(&args
, opt_recurse_submodules
);
368 argv_array_push(&args
, "--dry-run");
370 argv_array_push(&args
, opt_keep
);
372 argv_array_push(&args
, opt_depth
);
374 argv_array_push(&args
, opt_unshallow
);
375 if (opt_update_shallow
)
376 argv_array_push(&args
, opt_update_shallow
);
378 argv_array_push(&args
, opt_refmap
);
381 argv_array_push(&args
, repo
);
382 argv_array_pushv(&args
, refspecs
);
383 } else if (*refspecs
)
384 die("BUG: refspecs without repo?");
385 ret
= run_command_v_opt(args
.argv
, RUN_GIT_CMD
);
386 argv_array_clear(&args
);
391 * "Pulls into void" by branching off merge_head.
393 static int pull_into_void(const unsigned char *merge_head
,
394 const unsigned char *curr_head
)
397 * Two-way merge: we treat the index as based on an empty tree,
398 * and try to fast-forward to HEAD. This ensures we will not lose
399 * index/worktree changes that the user already made on the unborn
402 if (checkout_fast_forward(EMPTY_TREE_SHA1_BIN
, merge_head
, 0))
405 if (update_ref("initial pull", "HEAD", merge_head
, curr_head
, 0, UPDATE_REFS_DIE_ON_ERR
))
412 * Runs git-merge, returning its exit status.
414 static int run_merge(void)
417 struct argv_array args
= ARGV_ARRAY_INIT
;
419 argv_array_pushl(&args
, "merge", NULL
);
422 argv_push_verbosity(&args
);
424 argv_array_push(&args
, opt_progress
);
426 /* Options passed to git-merge */
428 argv_array_push(&args
, opt_diffstat
);
430 argv_array_push(&args
, opt_log
);
432 argv_array_push(&args
, opt_squash
);
434 argv_array_push(&args
, opt_commit
);
436 argv_array_push(&args
, opt_edit
);
438 argv_array_push(&args
, opt_ff
);
439 if (opt_verify_signatures
)
440 argv_array_push(&args
, opt_verify_signatures
);
441 argv_array_pushv(&args
, opt_strategies
.argv
);
442 argv_array_pushv(&args
, opt_strategy_opts
.argv
);
444 argv_array_push(&args
, opt_gpg_sign
);
446 argv_array_push(&args
, "FETCH_HEAD");
447 ret
= run_command_v_opt(args
.argv
, RUN_GIT_CMD
);
448 argv_array_clear(&args
);
452 int cmd_pull(int argc
, const char **argv
, const char *prefix
)
454 const char *repo
, **refspecs
;
455 struct sha1_array merge_heads
= SHA1_ARRAY_INIT
;
456 unsigned char orig_head
[GIT_SHA1_RAWSZ
], curr_head
[GIT_SHA1_RAWSZ
];
458 if (!getenv("_GIT_USE_BUILTIN_PULL")) {
459 const char *path
= mkpath("%s/git-pull", git_exec_path());
461 if (sane_execvp(path
, (char **)argv
) < 0)
462 die_errno("could not exec %s", path
);
465 if (!getenv("GIT_REFLOG_ACTION"))
466 set_reflog_message(argc
, argv
);
468 argc
= parse_options(argc
, argv
, prefix
, pull_options
, pull_usage
, 0);
470 parse_repo_refspecs(argc
, argv
, &repo
, &refspecs
);
473 opt_ff
= xstrdup_or_null(config_get_ff());
475 git_config(git_default_config
, NULL
);
477 if (read_cache_unmerged())
478 die_resolve_conflict("Pull");
480 if (file_exists(git_path("MERGE_HEAD")))
481 die_conclude_merge();
483 if (get_sha1("HEAD", orig_head
))
486 if (run_fetch(repo
, refspecs
))
492 if (get_sha1("HEAD", curr_head
))
495 if (!is_null_sha1(orig_head
) && !is_null_sha1(curr_head
) &&
496 hashcmp(orig_head
, curr_head
)) {
498 * The fetch involved updating the current branch.
500 * The working tree and the index file are still based on
501 * orig_head commit, but we are merging into curr_head.
502 * Update the working tree to match curr_head.
505 warning(_("fetch updated the current branch head.\n"
506 "fast-forwarding your working tree from\n"
507 "commit %s."), sha1_to_hex(orig_head
));
509 if (checkout_fast_forward(orig_head
, curr_head
, 0))
510 die(_("Cannot fast-forward your working tree.\n"
511 "After making sure that you saved anything precious from\n"
514 "$ git reset --hard\n"
515 "to recover."), sha1_to_hex(orig_head
));
518 get_merge_heads(&merge_heads
);
521 die_no_merge_candidates(repo
, refspecs
);
523 if (is_null_sha1(orig_head
)) {
524 if (merge_heads
.nr
> 1)
525 die(_("Cannot merge multiple branches into empty head."));
526 return pull_into_void(*merge_heads
.sha1
, curr_head
);