]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/pull.c
msvc: accommodate for vcpkg's upgrade to OpenSSL v1.1.x
[thirdparty/git.git] / builtin / pull.c
CommitLineData
1e1ea69f
PT
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 */
f8adbec9 8#define USE_THE_INDEX_COMPATIBILITY_MACROS
1e1ea69f 9#include "cache.h"
b2141fc1 10#include "config.h"
1e1ea69f
PT
11#include "builtin.h"
12#include "parse-options.h"
d807c4a0 13#include "exec-cmd.h"
f2c5baa1 14#include "run-command.h"
44c175c7
PT
15#include "sha1-array.h"
16#include "remote.h"
4a4cf9e8 17#include "dir.h"
49ec402d 18#include "refs.h"
ec0cb496 19#include "refspec.h"
8944969c 20#include "revision.h"
a6d7eb2c
SB
21#include "submodule.h"
22#include "submodule-config.h"
db86e61c 23#include "tempfile.h"
8944969c 24#include "lockfile.h"
fd84986f 25#include "wt-status.h"
64043556 26#include "commit-reach.h"
d540b70c 27#include "sequencer.h"
1e1ea69f 28
1678b81e
PT
29enum rebase_type {
30 REBASE_INVALID = -1,
31 REBASE_FALSE = 0,
32 REBASE_TRUE,
f5eb87b9 33 REBASE_PRESERVE,
1131ec98 34 REBASE_MERGES,
f5eb87b9 35 REBASE_INTERACTIVE
1678b81e
PT
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
1131ec98
JS
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.
1678b81e
PT
44 */
45static enum rebase_type parse_config_rebase(const char *key, const char *value,
46 int fatal)
47{
89576613 48 int v = git_parse_maybe_bool(value);
1678b81e
PT
49
50 if (!v)
51 return REBASE_FALSE;
52 else if (v > 0)
53 return REBASE_TRUE;
46af44b0 54 else if (!strcmp(value, "preserve") || !strcmp(value, "p"))
1678b81e 55 return REBASE_PRESERVE;
46af44b0 56 else if (!strcmp(value, "merges") || !strcmp(value, "m"))
1131ec98 57 return REBASE_MERGES;
46af44b0 58 else if (!strcmp(value, "interactive") || !strcmp(value, "i"))
f5eb87b9 59 return REBASE_INTERACTIVE;
5a59a230
NTND
60 /*
61 * Please update _git_config() in git-completion.bash when you
62 * add new rebase modes.
63 */
1678b81e
PT
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
1e1ea69f 87static const char * const pull_usage[] = {
e7a7401f 88 N_("git pull [<options>] [<repository> [<refspec>...]]"),
1e1ea69f
PT
89 NULL
90};
91
2a747902
PT
92/* Shared options */
93static int opt_verbosity;
94static char *opt_progress;
a6d7eb2c 95static int recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
2a747902 96
1678b81e 97/* Options passed to git-merge or git-rebase */
81dbd768 98static enum rebase_type opt_rebase = -1;
11b6d178
PT
99static char *opt_diffstat;
100static char *opt_log;
3a4d2c74 101static char *opt_signoff;
11b6d178
PT
102static char *opt_squash;
103static char *opt_commit;
104static char *opt_edit;
d540b70c 105static char *cleanup_arg;
11b6d178
PT
106static char *opt_ff;
107static char *opt_verify_signatures;
f66398eb 108static int opt_autostash = -1;
c48d73bd 109static int config_autostash;
11b6d178
PT
110static struct argv_array opt_strategies = ARGV_ARRAY_INIT;
111static struct argv_array opt_strategy_opts = ARGV_ARRAY_INIT;
112static char *opt_gpg_sign;
09c2cb87 113static int opt_allow_unrelated_histories;
11b6d178 114
a32975f5
PT
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;
62104ba1 122static char *max_children;
a32975f5
PT
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;
ffb4568a
WS
129static char *opt_ipv4;
130static char *opt_ipv6;
3883c551 131static int opt_show_forced_updates = -1;
24bc1a12 132static char *set_upstream;
a32975f5 133
1e1ea69f 134static struct option pull_options[] = {
2a747902
PT
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),
a6d7eb2c
SB
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 },
2a747902 144
1678b81e 145 /* Options passed to git-merge or git-rebase */
11b6d178 146 OPT_GROUP(N_("Options related to merging")),
1678b81e 147 { OPTION_CALLBACK, 'r', "rebase", &opt_rebase,
bbc072f5 148 "(false|true|merges|preserve|interactive)",
1678b81e
PT
149 N_("incorporate changes by rebasing rather than merging"),
150 PARSE_OPT_OPTARG, parse_opt_rebase },
11b6d178
PT
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),
3a4d2c74
TK
163 OPT_PASSTHRU(0, "signoff", &opt_signoff, NULL,
164 N_("add Signed-off-by:"),
165 PARSE_OPT_OPTARG),
11b6d178
PT
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),
d540b70c 175 OPT_CLEANUP(&cleanup_arg),
11b6d178
PT
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),
f66398eb
MJ
185 OPT_BOOL(0, "autostash", &opt_autostash,
186 N_("automatically stash/stash pop before and after rebase")),
11b6d178
PT
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),
09c2cb87
JH
197 OPT_SET_INT(0, "allow-unrelated-histories",
198 &opt_allow_unrelated_histories,
199 N_("allow merging unrelated histories"), 1),
11b6d178 200
a32975f5
PT
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),
1224781d 212 OPT__FORCE(&opt_force, N_("force overwrite of local branch"), 0),
a32975f5
PT
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),
62104ba1
SB
219 OPT_PASSTHRU('j', "jobs", &max_children, N_("n"),
220 N_("number of submodules pulled in parallel"),
221 PARSE_OPT_OPTARG),
a32975f5
PT
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),
ffb4568a
WS
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),
3883c551
DS
245 OPT_BOOL(0, "show-forced-updates", &opt_show_forced_updates,
246 N_("check for forced-updates on all updated branches")),
24bc1a12
CB
247 OPT_PASSTHRU(0, "set-upstream", &set_upstream, NULL,
248 N_("set upstream for git pull/fetch"),
249 PARSE_OPT_NOARG),
a32975f5 250
1e1ea69f
PT
251 OPT_END()
252};
253
2a747902
PT
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
a32975f5
PT
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
41fca098
PT
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
a9de9897
PT
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
89576613 310 switch (git_parse_maybe_bool(value)) {
a9de9897
PT
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
81dbd768
PT
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
c48d73bd
MJ
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;
121e43fa
NMC
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;
c48d73bd
MJ
365 }
366 return git_default_config(var, value, cb);
367}
368
44c175c7
PT
369/**
370 * Appends merge candidates from FETCH_HEAD that are not marked not-for-merge
371 * into merge_heads.
372 */
910650d2 373static void get_merge_heads(struct oid_array *merge_heads)
44c175c7 374{
102de880 375 const char *filename = git_path_fetch_head(the_repository);
44c175c7
PT
376 FILE *fp;
377 struct strbuf sb = STRBUF_INIT;
14bb40b3 378 struct object_id oid;
44c175c7 379
23a9e071 380 fp = xfopen(filename, "r");
8f309aeb 381 while (strbuf_getline_lf(&sb, fp) != EOF) {
fbfc089d 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"))
44c175c7 386 continue; /* ref is not-for-merge */
910650d2 387 oid_array_append(merge_heads, &oid);
44c175c7
PT
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) {
b7b31471
PT
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."));
44c175c7
PT
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."));
b7b31471
PT
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."));
44c175c7
PT
453 fprintf_ln(stderr, _("See git-pull(1) for details."));
454 fprintf(stderr, "\n");
8a0de58a 455 fprintf_ln(stderr, " git pull %s %s", _("<remote>"), _("<branch>"));
44c175c7
PT
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)
8a0de58a 461 remote_name = _("<remote>");
44c175c7
PT
462
463 fprintf_ln(stderr, _("There is no tracking information for the current branch."));
b7b31471
PT
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."));
44c175c7
PT
468 fprintf_ln(stderr, _("See git-pull(1) for details."));
469 fprintf(stderr, "\n");
8a0de58a 470 fprintf_ln(stderr, " git pull %s %s", _("<remote>"), _("<branch>"));
44c175c7 471 fprintf(stderr, "\n");
daf9f649
VA
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);
44c175c7
PT
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
f2c5baa1
PT
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);
2a747902
PT
509
510 /* Shared options */
511 argv_push_verbosity(&args);
512 if (opt_progress)
513 argv_array_push(&args, opt_progress);
514
a32975f5
PT
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);
a6d7eb2c
SB
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 }
62104ba1
SB
541 if (max_children)
542 argv_array_push(&args, max_children);
a32975f5
PT
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);
ffb4568a
WS
555 if (opt_ipv4)
556 argv_array_push(&args, opt_ipv4);
557 if (opt_ipv6)
558 argv_array_push(&args, opt_ipv6);
3883c551
DS
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");
24bc1a12
CB
563 if (set_upstream)
564 argv_array_push(&args, set_upstream);
a32975f5 565
f2c5baa1
PT
566 if (repo) {
567 argv_array_push(&args, repo);
568 argv_array_pushv(&args, refspecs);
569 } else if (*refspecs)
033abf97 570 BUG("refspecs without repo?");
f2c5baa1
PT
571 ret = run_command_v_opt(args.argv, RUN_GIT_CMD);
572 argv_array_clear(&args);
573 return ret;
574}
575
49ec402d
PT
576/**
577 * "Pulls into void" by branching off merge_head.
578 */
ee3051bd 579static int pull_into_void(const struct object_id *merge_head,
f9b11147 580 const struct object_id *curr_head)
49ec402d 581{
01a31f3b
JK
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
49ec402d
PT
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 */
7e196c3a
NTND
599 if (checkout_fast_forward(the_repository,
600 the_hash_algo->empty_tree,
601 merge_head, 0))
49ec402d
PT
602 return 1;
603
ae077771 604 if (update_ref("initial pull", "HEAD", merge_head, curr_head, 0, UPDATE_REFS_DIE_ON_ERR))
49ec402d
PT
605 return 1;
606
607 return 0;
608}
609
a6d7eb2c
SB
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);
a56771a6 618 argv_push_verbosity(&cp.args);
a6d7eb2c
SB
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);
a56771a6 631 argv_push_verbosity(&cp.args);
a6d7eb2c
SB
632
633 return run_command(&cp);
634}
635
f2c5baa1
PT
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);
2a747902
PT
645
646 /* Shared options */
647 argv_push_verbosity(&args);
648 if (opt_progress)
649 argv_array_push(&args, opt_progress);
650
11b6d178
PT
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);
3a4d2c74
TK
656 if (opt_signoff)
657 argv_array_push(&args, opt_signoff);
11b6d178
PT
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);
d540b70c
DL
664 if (cleanup_arg)
665 argv_array_pushf(&args, "--cleanup=%s", cleanup_arg);
11b6d178
PT
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);
09c2cb87
JH
674 if (opt_allow_unrelated_histories > 0)
675 argv_array_push(&args, "--allow-unrelated-histories");
11b6d178 676
f2c5baa1
PT
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
1678b81e
PT
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/**
30aa96cd 714 * Derives the remote-tracking branch from the remote and refspec.
1678b81e
PT
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{
895d3912 721 struct refspec_item spec;
1678b81e
PT
722 const char *spec_src;
723 const char *merge_branch;
724
dc064221 725 refspec_item_init_or_die(&spec, refspec, REFSPEC_FETCH);
895d3912 726 spec_src = spec.src;
1678b81e
PT
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
895d3912 746 refspec_item_clear(&spec);
1678b81e
PT
747 return merge_branch;
748}
749
750/**
751 * Given the repo and refspecs, sets fork_point to the point at which the
30aa96cd 752 * current branch forked from its remote-tracking branch. Returns 0 on success,
1678b81e
PT
753 * -1 on failure.
754 */
f9b11147 755static int get_rebase_fork_point(struct object_id *fork_point, const char *repo,
1678b81e
PT
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
fbfc089d 782 ret = capture_command(&cp, &sb, GIT_MAX_HEXSZ);
1678b81e
PT
783 if (ret)
784 goto cleanup;
785
f9b11147 786 ret = get_oid_hex(sb.buf, fork_point);
1678b81e
PT
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 */
f9b11147 799static int get_octopus_merge_base(struct object_id *merge_base,
800 const struct object_id *curr_head,
ee3051bd 801 const struct object_id *merge_head,
f9b11147 802 const struct object_id *fork_point)
1678b81e
PT
803{
804 struct commit_list *revs = NULL, *result;
805
2122f675
SB
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);
f9b11147 810 if (!is_null_oid(fork_point))
2122f675
SB
811 commit_list_insert(lookup_commit_reference(the_repository, fork_point),
812 &revs);
1678b81e 813
4da72644 814 result = get_octopus_merge_bases(revs);
1678b81e 815 free_commit_list(revs);
4da72644
816 reduce_heads_replace(&result);
817
1678b81e
PT
818 if (!result)
819 return 1;
820
f9b11147 821 oidcpy(merge_base, &result->item->object.oid);
4da72644 822 free_commit_list(result);
1678b81e
PT
823 return 0;
824}
825
826/**
fbfc089d 827 * Given the current HEAD oid, the merge head returned from git-fetch and the
1678b81e
PT
828 * fork point calculated by get_rebase_fork_point(), runs git-rebase with the
829 * appropriate arguments and returns its exit status.
830 */
f9b11147 831static int run_rebase(const struct object_id *curr_head,
ee3051bd 832 const struct object_id *merge_head,
f9b11147 833 const struct object_id *fork_point)
1678b81e
PT
834{
835 int ret;
f9b11147 836 struct object_id oct_merge_base;
1678b81e
PT
837 struct argv_array args = ARGV_ARRAY_INIT;
838
f9b11147 839 if (!get_octopus_merge_base(&oct_merge_base, curr_head, merge_head, fork_point))
4a7e27e9 840 if (!is_null_oid(fork_point) && oideq(&oct_merge_base, fork_point))
1678b81e
PT
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 */
1131ec98
JS
849 if (opt_rebase == REBASE_MERGES)
850 argv_array_push(&args, "--rebase-merges");
851 else if (opt_rebase == REBASE_PRESERVE)
1678b81e 852 argv_array_push(&args, "--preserve-merges");
f5eb87b9
JS
853 else if (opt_rebase == REBASE_INTERACTIVE)
854 argv_array_push(&args, "--interactive");
1678b81e
PT
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);
f66398eb
MJ
861 if (opt_autostash == 0)
862 argv_array_push(&args, "--no-autostash");
863 else if (opt_autostash == 1)
864 argv_array_push(&args, "--autostash");
c57e501c
AH
865 if (opt_verify_signatures &&
866 !strcmp(opt_verify_signatures, "--verify-signatures"))
867 warning(_("ignoring --verify-signatures for rebase"));
1678b81e
PT
868
869 argv_array_push(&args, "--onto");
ee3051bd 870 argv_array_push(&args, oid_to_hex(merge_head));
1678b81e 871
f9b11147 872 if (fork_point && !is_null_oid(fork_point))
873 argv_array_push(&args, oid_to_hex(fork_point));
1678b81e 874 else
ee3051bd 875 argv_array_push(&args, oid_to_hex(merge_head));
1678b81e
PT
876
877 ret = run_command_v_opt(args.argv, RUN_GIT_CMD);
878 argv_array_clear(&args);
879 return ret;
880}
881
1e1ea69f
PT
882int cmd_pull(int argc, const char **argv, const char *prefix)
883{
f2c5baa1 884 const char *repo, **refspecs;
910650d2 885 struct oid_array merge_heads = OID_ARRAY_INIT;
f9b11147 886 struct object_id orig_head, curr_head;
887 struct object_id rebase_fork_point;
f15e7cf5 888 int autostash;
f2c5baa1 889
41fca098
PT
890 if (!getenv("GIT_REFLOG_ACTION"))
891 set_reflog_message(argc, argv);
892
cad0c692
NMC
893 git_config(git_pull_config, NULL);
894
1e1ea69f
PT
895 argc = parse_options(argc, argv, prefix, pull_options, pull_usage, 0);
896
d540b70c
DL
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
f2c5baa1
PT
904 parse_repo_refspecs(argc, argv, &repo, &refspecs);
905
a9de9897
PT
906 if (!opt_ff)
907 opt_ff = xstrdup_or_null(config_get_ff());
908
81dbd768
PT
909 if (opt_rebase < 0)
910 opt_rebase = config_get_rebase();
911
4a4cf9e8 912 if (read_cache_unmerged())
8785c425 913 die_resolve_conflict("pull");
4a4cf9e8 914
102de880 915 if (file_exists(git_path_merge_head(the_repository)))
4a4cf9e8
PT
916 die_conclude_merge();
917
f9b11147 918 if (get_oid("HEAD", &orig_head))
919 oidclr(&orig_head);
fe911b8c 920
f66398eb
MJ
921 if (!opt_rebase && opt_autostash != -1)
922 die(_("--[no-]autostash option is only valid with --rebase."));
923
f15e7cf5 924 autostash = config_autostash;
8944969c 925 if (opt_rebase) {
f66398eb
MJ
926 if (opt_autostash != -1)
927 autostash = opt_autostash;
53c76dc0 928
f9b11147 929 if (is_null_oid(&orig_head) && !is_cache_unborn())
8944969c
PT
930 die(_("Updating an unborn branch with changes added to the index."));
931
53c76dc0 932 if (!autostash)
5b02ca38
NTND
933 require_clean_work_tree(the_repository,
934 N_("pull with rebase"),
d8cc92ab 935 _("please commit or stash them."), 1, 0);
8944969c 936
f9b11147 937 if (get_rebase_fork_point(&rebase_fork_point, repo, *refspecs))
938 oidclr(&rebase_fork_point);
8944969c 939 }
1678b81e 940
f2c5baa1
PT
941 if (run_fetch(repo, refspecs))
942 return 1;
943
a32975f5
PT
944 if (opt_dry_run)
945 return 0;
946
f9b11147 947 if (get_oid("HEAD", &curr_head))
948 oidclr(&curr_head);
fe911b8c 949
f9b11147 950 if (!is_null_oid(&orig_head) && !is_null_oid(&curr_head) &&
9001dc2a 951 !oideq(&orig_head, &curr_head)) {
fe911b8c
PT
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"
f9b11147 962 "commit %s."), oid_to_hex(&orig_head));
fe911b8c 963
7e196c3a
NTND
964 if (checkout_fast_forward(the_repository, &orig_head,
965 &curr_head, 0))
fe911b8c
PT
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"
f9b11147 971 "to recover."), oid_to_hex(&orig_head));
fe911b8c
PT
972 }
973
44c175c7
PT
974 get_merge_heads(&merge_heads);
975
976 if (!merge_heads.nr)
977 die_no_merge_candidates(repo, refspecs);
978
f9b11147 979 if (is_null_oid(&orig_head)) {
49ec402d
PT
980 if (merge_heads.nr > 1)
981 die(_("Cannot merge multiple branches into empty head."));
ee3051bd 982 return pull_into_void(merge_heads.oid, &curr_head);
33b842a1
JH
983 }
984 if (opt_rebase && merge_heads.nr > 1)
985 die(_("Cannot rebase onto multiple branches."));
986
987 if (opt_rebase) {
a6d7eb2c
SB
988 int ret = 0;
989 if ((recurse_submodules == RECURSE_SUBMODULES_ON ||
990 recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND) &&
6245b98b 991 submodule_touches_in_range(the_repository, &rebase_fork_point, &curr_head))
a6d7eb2c 992 die(_("cannot rebase with locally recorded submodule modifications"));
f15e7cf5
TB
993 if (!autostash) {
994 struct commit_list *list = NULL;
995 struct commit *merge_head, *head;
996
2122f675
SB
997 head = lookup_commit_reference(the_repository,
998 &orig_head);
f15e7cf5 999 commit_list_insert(head, &list);
2122f675
SB
1000 merge_head = lookup_commit_reference(the_repository,
1001 &merge_heads.oid[0]);
f15e7cf5
TB
1002 if (is_descendant_of(merge_head, list)) {
1003 /* we can fast-forward this without invoking rebase */
1004 opt_ff = "--ff-only";
a6d7eb2c 1005 ret = run_merge();
f15e7cf5 1006 }
33b842a1 1007 }
a6d7eb2c
SB
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;
33b842a1 1015 } else {
a6d7eb2c
SB
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;
33b842a1 1021 }
1e1ea69f 1022}