]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/pull.c
pull: give the advice for choosing rebase/merge much later
[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"
fe299ec5 15#include "oid-array.h"
44c175c7 16#include "remote.h"
4a4cf9e8 17#include "dir.h"
88f8576e 18#include "rebase.h"
49ec402d 19#include "refs.h"
ec0cb496 20#include "refspec.h"
8944969c 21#include "revision.h"
a6d7eb2c
SB
22#include "submodule.h"
23#include "submodule-config.h"
db86e61c 24#include "tempfile.h"
8944969c 25#include "lockfile.h"
fd84986f 26#include "wt-status.h"
64043556 27#include "commit-reach.h"
d540b70c 28#include "sequencer.h"
1e1ea69f 29
278f4be8
FC
30static int default_mode;
31
1678b81e
PT
32/**
33 * Parses the value of --rebase. If value is a false value, returns
34 * REBASE_FALSE. If value is a true value, returns REBASE_TRUE. If value is
1131ec98
JS
35 * "merges", returns REBASE_MERGES. If value is "preserve", returns
36 * REBASE_PRESERVE. If value is a invalid value, dies with a fatal error if
37 * fatal is true, otherwise returns REBASE_INVALID.
1678b81e
PT
38 */
39static enum rebase_type parse_config_rebase(const char *key, const char *value,
40 int fatal)
41{
88f8576e
BW
42 enum rebase_type v = rebase_parse_value(value);
43 if (v != REBASE_INVALID)
44 return v;
1678b81e
PT
45
46 if (fatal)
47 die(_("Invalid value for %s: %s"), key, value);
48 else
49 error(_("Invalid value for %s: %s"), key, value);
50
51 return REBASE_INVALID;
52}
53
54/**
55 * Callback for --rebase, which parses arg with parse_config_rebase().
56 */
57static int parse_opt_rebase(const struct option *opt, const char *arg, int unset)
58{
59 enum rebase_type *value = opt->value;
60
61 if (arg)
62 *value = parse_config_rebase("--rebase", arg, 0);
63 else
64 *value = unset ? REBASE_FALSE : REBASE_TRUE;
65 return *value == REBASE_INVALID ? -1 : 0;
66}
67
1e1ea69f 68static const char * const pull_usage[] = {
e7a7401f 69 N_("git pull [<options>] [<repository> [<refspec>...]]"),
1e1ea69f
PT
70 NULL
71};
72
2a747902
PT
73/* Shared options */
74static int opt_verbosity;
75static char *opt_progress;
a6d7eb2c 76static int recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
2a747902 77
1678b81e 78/* Options passed to git-merge or git-rebase */
81dbd768 79static enum rebase_type opt_rebase = -1;
11b6d178
PT
80static char *opt_diffstat;
81static char *opt_log;
3a4d2c74 82static char *opt_signoff;
11b6d178
PT
83static char *opt_squash;
84static char *opt_commit;
85static char *opt_edit;
d540b70c 86static char *cleanup_arg;
11b6d178
PT
87static char *opt_ff;
88static char *opt_verify_signatures;
f66398eb 89static int opt_autostash = -1;
c48d73bd 90static int config_autostash;
54887b46 91static int check_trust_level = 1;
22f9b7f3
JK
92static struct strvec opt_strategies = STRVEC_INIT;
93static struct strvec opt_strategy_opts = STRVEC_INIT;
11b6d178 94static char *opt_gpg_sign;
09c2cb87 95static int opt_allow_unrelated_histories;
11b6d178 96
a32975f5
PT
97/* Options passed to git-fetch */
98static char *opt_all;
99static char *opt_append;
100static char *opt_upload_pack;
101static int opt_force;
102static char *opt_tags;
103static char *opt_prune;
62104ba1 104static char *max_children;
a32975f5
PT
105static int opt_dry_run;
106static char *opt_keep;
107static char *opt_depth;
108static char *opt_unshallow;
109static char *opt_update_shallow;
110static char *opt_refmap;
ffb4568a
WS
111static char *opt_ipv4;
112static char *opt_ipv6;
3883c551 113static int opt_show_forced_updates = -1;
24bc1a12 114static char *set_upstream;
22f9b7f3 115static struct strvec opt_fetch = STRVEC_INIT;
a32975f5 116
1e1ea69f 117static struct option pull_options[] = {
2a747902
PT
118 /* Shared options */
119 OPT__VERBOSITY(&opt_verbosity),
120 OPT_PASSTHRU(0, "progress", &opt_progress, NULL,
121 N_("force progress reporting"),
122 PARSE_OPT_NOARG),
203c8533 123 OPT_CALLBACK_F(0, "recurse-submodules",
a6d7eb2c
SB
124 &recurse_submodules, N_("on-demand"),
125 N_("control for recursive fetching of submodules"),
203c8533 126 PARSE_OPT_OPTARG, option_fetch_parse_recurse_submodules),
2a747902 127
1678b81e 128 /* Options passed to git-merge or git-rebase */
11b6d178 129 OPT_GROUP(N_("Options related to merging")),
203c8533 130 OPT_CALLBACK_F('r', "rebase", &opt_rebase,
bbc072f5 131 "(false|true|merges|preserve|interactive)",
1678b81e 132 N_("incorporate changes by rebasing rather than merging"),
203c8533 133 PARSE_OPT_OPTARG, parse_opt_rebase),
11b6d178
PT
134 OPT_PASSTHRU('n', NULL, &opt_diffstat, NULL,
135 N_("do not show a diffstat at the end of the merge"),
136 PARSE_OPT_NOARG | PARSE_OPT_NONEG),
137 OPT_PASSTHRU(0, "stat", &opt_diffstat, NULL,
138 N_("show a diffstat at the end of the merge"),
139 PARSE_OPT_NOARG),
140 OPT_PASSTHRU(0, "summary", &opt_diffstat, NULL,
141 N_("(synonym to --stat)"),
142 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN),
143 OPT_PASSTHRU(0, "log", &opt_log, N_("n"),
144 N_("add (at most <n>) entries from shortlog to merge commit message"),
145 PARSE_OPT_OPTARG),
3a4d2c74 146 OPT_PASSTHRU(0, "signoff", &opt_signoff, NULL,
3abd4a67 147 N_("add a Signed-off-by trailer"),
3a4d2c74 148 PARSE_OPT_OPTARG),
11b6d178
PT
149 OPT_PASSTHRU(0, "squash", &opt_squash, NULL,
150 N_("create a single commit instead of doing a merge"),
151 PARSE_OPT_NOARG),
152 OPT_PASSTHRU(0, "commit", &opt_commit, NULL,
153 N_("perform a commit if the merge succeeds (default)"),
154 PARSE_OPT_NOARG),
155 OPT_PASSTHRU(0, "edit", &opt_edit, NULL,
156 N_("edit message before committing"),
157 PARSE_OPT_NOARG),
d540b70c 158 OPT_CLEANUP(&cleanup_arg),
11b6d178
PT
159 OPT_PASSTHRU(0, "ff", &opt_ff, NULL,
160 N_("allow fast-forward"),
161 PARSE_OPT_NOARG),
162 OPT_PASSTHRU(0, "ff-only", &opt_ff, NULL,
163 N_("abort if fast-forward is not possible"),
164 PARSE_OPT_NOARG | PARSE_OPT_NONEG),
165 OPT_PASSTHRU(0, "verify-signatures", &opt_verify_signatures, NULL,
166 N_("verify that the named commit has a valid GPG signature"),
167 PARSE_OPT_NOARG),
f66398eb 168 OPT_BOOL(0, "autostash", &opt_autostash,
d9f15d37 169 N_("automatically stash/stash pop before and after")),
11b6d178
PT
170 OPT_PASSTHRU_ARGV('s', "strategy", &opt_strategies, N_("strategy"),
171 N_("merge strategy to use"),
172 0),
173 OPT_PASSTHRU_ARGV('X', "strategy-option", &opt_strategy_opts,
174 N_("option=value"),
175 N_("option for selected merge strategy"),
176 0),
177 OPT_PASSTHRU('S', "gpg-sign", &opt_gpg_sign, N_("key-id"),
178 N_("GPG sign commit"),
179 PARSE_OPT_OPTARG),
09c2cb87
JH
180 OPT_SET_INT(0, "allow-unrelated-histories",
181 &opt_allow_unrelated_histories,
182 N_("allow merging unrelated histories"), 1),
11b6d178 183
a32975f5
PT
184 /* Options passed to git-fetch */
185 OPT_GROUP(N_("Options related to fetching")),
186 OPT_PASSTHRU(0, "all", &opt_all, NULL,
187 N_("fetch from all remotes"),
188 PARSE_OPT_NOARG),
189 OPT_PASSTHRU('a', "append", &opt_append, NULL,
190 N_("append to .git/FETCH_HEAD instead of overwriting"),
191 PARSE_OPT_NOARG),
192 OPT_PASSTHRU(0, "upload-pack", &opt_upload_pack, N_("path"),
193 N_("path to upload pack on remote end"),
194 0),
1224781d 195 OPT__FORCE(&opt_force, N_("force overwrite of local branch"), 0),
a32975f5
PT
196 OPT_PASSTHRU('t', "tags", &opt_tags, NULL,
197 N_("fetch all tags and associated objects"),
198 PARSE_OPT_NOARG),
199 OPT_PASSTHRU('p', "prune", &opt_prune, NULL,
200 N_("prune remote-tracking branches no longer on remote"),
201 PARSE_OPT_NOARG),
62104ba1
SB
202 OPT_PASSTHRU('j', "jobs", &max_children, N_("n"),
203 N_("number of submodules pulled in parallel"),
204 PARSE_OPT_OPTARG),
a32975f5
PT
205 OPT_BOOL(0, "dry-run", &opt_dry_run,
206 N_("dry run")),
207 OPT_PASSTHRU('k', "keep", &opt_keep, NULL,
208 N_("keep downloaded pack"),
209 PARSE_OPT_NOARG),
210 OPT_PASSTHRU(0, "depth", &opt_depth, N_("depth"),
211 N_("deepen history of shallow clone"),
212 0),
13ac5edb
RS
213 OPT_PASSTHRU_ARGV(0, "shallow-since", &opt_fetch, N_("time"),
214 N_("deepen history of shallow repository based on time"),
215 0),
216 OPT_PASSTHRU_ARGV(0, "shallow-exclude", &opt_fetch, N_("revision"),
217 N_("deepen history of shallow clone, excluding rev"),
218 0),
219 OPT_PASSTHRU_ARGV(0, "deepen", &opt_fetch, N_("n"),
220 N_("deepen history of shallow clone"),
221 0),
a32975f5
PT
222 OPT_PASSTHRU(0, "unshallow", &opt_unshallow, NULL,
223 N_("convert to a complete repository"),
224 PARSE_OPT_NONEG | PARSE_OPT_NOARG),
225 OPT_PASSTHRU(0, "update-shallow", &opt_update_shallow, NULL,
226 N_("accept refs that update .git/shallow"),
227 PARSE_OPT_NOARG),
228 OPT_PASSTHRU(0, "refmap", &opt_refmap, N_("refmap"),
229 N_("specify fetch refmap"),
230 PARSE_OPT_NONEG),
13ac5edb
RS
231 OPT_PASSTHRU_ARGV('o', "server-option", &opt_fetch,
232 N_("server-specific"),
233 N_("option to transmit"),
234 0),
ffb4568a
WS
235 OPT_PASSTHRU('4', "ipv4", &opt_ipv4, NULL,
236 N_("use IPv4 addresses only"),
237 PARSE_OPT_NOARG),
238 OPT_PASSTHRU('6', "ipv6", &opt_ipv6, NULL,
239 N_("use IPv6 addresses only"),
240 PARSE_OPT_NOARG),
13ac5edb
RS
241 OPT_PASSTHRU_ARGV(0, "negotiation-tip", &opt_fetch, N_("revision"),
242 N_("report that we have only objects reachable from this object"),
243 0),
3883c551
DS
244 OPT_BOOL(0, "show-forced-updates", &opt_show_forced_updates,
245 N_("check for forced-updates on all updated branches")),
24bc1a12
CB
246 OPT_PASSTHRU(0, "set-upstream", &set_upstream, NULL,
247 N_("set upstream for git pull/fetch"),
248 PARSE_OPT_NOARG),
a32975f5 249
1e1ea69f
PT
250 OPT_END()
251};
252
2a747902
PT
253/**
254 * Pushes "-q" or "-v" switches into arr to match the opt_verbosity level.
255 */
22f9b7f3 256static void argv_push_verbosity(struct strvec *arr)
2a747902
PT
257{
258 int verbosity;
259
260 for (verbosity = opt_verbosity; verbosity > 0; verbosity--)
22f9b7f3 261 strvec_push(arr, "-v");
2a747902
PT
262
263 for (verbosity = opt_verbosity; verbosity < 0; verbosity++)
22f9b7f3 264 strvec_push(arr, "-q");
2a747902
PT
265}
266
a32975f5
PT
267/**
268 * Pushes "-f" switches into arr to match the opt_force level.
269 */
22f9b7f3 270static void argv_push_force(struct strvec *arr)
a32975f5
PT
271{
272 int force = opt_force;
273 while (force-- > 0)
22f9b7f3 274 strvec_push(arr, "-f");
a32975f5
PT
275}
276
41fca098
PT
277/**
278 * Sets the GIT_REFLOG_ACTION environment variable to the concatenation of argv
279 */
280static void set_reflog_message(int argc, const char **argv)
281{
282 int i;
283 struct strbuf msg = STRBUF_INIT;
284
285 for (i = 0; i < argc; i++) {
286 if (i)
287 strbuf_addch(&msg, ' ');
288 strbuf_addstr(&msg, argv[i]);
289 }
290
291 setenv("GIT_REFLOG_ACTION", msg.buf, 0);
292
293 strbuf_release(&msg);
294}
295
a9de9897
PT
296/**
297 * If pull.ff is unset, returns NULL. If pull.ff is "true", returns "--ff". If
298 * pull.ff is "false", returns "--no-ff". If pull.ff is "only", returns
299 * "--ff-only". Otherwise, if pull.ff is set to an invalid value, die with an
300 * error.
301 */
302static const char *config_get_ff(void)
303{
304 const char *value;
305
306 if (git_config_get_value("pull.ff", &value))
307 return NULL;
308
89576613 309 switch (git_parse_maybe_bool(value)) {
a9de9897
PT
310 case 0:
311 return "--no-ff";
312 case 1:
313 return "--ff";
314 }
315
316 if (!strcmp(value, "only"))
317 return "--ff-only";
318
319 die(_("Invalid value for pull.ff: %s"), value);
320}
321
81dbd768
PT
322/**
323 * Returns the default configured value for --rebase. It first looks for the
324 * value of "branch.$curr_branch.rebase", where $curr_branch is the current
325 * branch, and if HEAD is detached or the configuration key does not exist,
326 * looks for the value of "pull.rebase". If both configuration keys do not
327 * exist, returns REBASE_FALSE.
328 */
329static enum rebase_type config_get_rebase(void)
330{
331 struct branch *curr_branch = branch_get("HEAD");
332 const char *value;
333
334 if (curr_branch) {
335 char *key = xstrfmt("branch.%s.rebase", curr_branch->name);
336
337 if (!git_config_get_value(key, &value)) {
338 enum rebase_type ret = parse_config_rebase(key, value, 1);
339 free(key);
340 return ret;
341 }
342
343 free(key);
344 }
345
346 if (!git_config_get_value("pull.rebase", &value))
347 return parse_config_rebase("pull.rebase", value, 1);
348
278f4be8 349 default_mode = 1;
d18c950a 350
81dbd768
PT
351 return REBASE_FALSE;
352}
353
c48d73bd
MJ
354/**
355 * Read config variables.
356 */
357static int git_pull_config(const char *var, const char *value, void *cb)
358{
54887b46
HJI
359 int status;
360
c48d73bd
MJ
361 if (!strcmp(var, "rebase.autostash")) {
362 config_autostash = git_config_bool(var, value);
363 return 0;
121e43fa
NMC
364 } else if (!strcmp(var, "submodule.recurse")) {
365 recurse_submodules = git_config_bool(var, value) ?
366 RECURSE_SUBMODULES_ON : RECURSE_SUBMODULES_OFF;
367 return 0;
54887b46
HJI
368 } else if (!strcmp(var, "gpg.mintrustlevel")) {
369 check_trust_level = 0;
c48d73bd 370 }
54887b46
HJI
371
372 status = git_gpg_config(var, value, cb);
373 if (status)
374 return status;
375
c48d73bd
MJ
376 return git_default_config(var, value, cb);
377}
378
44c175c7
PT
379/**
380 * Appends merge candidates from FETCH_HEAD that are not marked not-for-merge
381 * into merge_heads.
382 */
910650d2 383static void get_merge_heads(struct oid_array *merge_heads)
44c175c7 384{
102de880 385 const char *filename = git_path_fetch_head(the_repository);
44c175c7
PT
386 FILE *fp;
387 struct strbuf sb = STRBUF_INIT;
14bb40b3 388 struct object_id oid;
44c175c7 389
23a9e071 390 fp = xfopen(filename, "r");
8f309aeb 391 while (strbuf_getline_lf(&sb, fp) != EOF) {
fbfc089d 392 const char *p;
393 if (parse_oid_hex(sb.buf, &oid, &p))
394 continue; /* invalid line: does not start with object ID */
395 if (starts_with(p, "\tnot-for-merge\t"))
44c175c7 396 continue; /* ref is not-for-merge */
910650d2 397 oid_array_append(merge_heads, &oid);
44c175c7
PT
398 }
399 fclose(fp);
400 strbuf_release(&sb);
401}
402
403/**
404 * Used by die_no_merge_candidates() as a for_each_remote() callback to
405 * retrieve the name of the remote if the repository only has one remote.
406 */
407static int get_only_remote(struct remote *remote, void *cb_data)
408{
409 const char **remote_name = cb_data;
410
411 if (*remote_name)
412 return -1;
413
414 *remote_name = remote->name;
415 return 0;
416}
417
418/**
419 * Dies with the appropriate reason for why there are no merge candidates:
420 *
421 * 1. We fetched from a specific remote, and a refspec was given, but it ended
422 * up not fetching anything. This is usually because the user provided a
423 * wildcard refspec which had no matches on the remote end.
424 *
425 * 2. We fetched from a non-default remote, but didn't specify a branch to
426 * merge. We can't use the configured one because it applies to the default
427 * remote, thus the user must specify the branches to merge.
428 *
429 * 3. We fetched from the branch's or repo's default remote, but:
430 *
431 * a. We are not on a branch, so there will never be a configured branch to
432 * merge with.
433 *
434 * b. We are on a branch, but there is no configured branch to merge with.
435 *
436 * 4. We fetched from the branch's or repo's default remote, but the configured
437 * branch to merge didn't get fetched. (Either it doesn't exist, or wasn't
438 * part of the configured fetch refspec.)
439 */
440static void NORETURN die_no_merge_candidates(const char *repo, const char **refspecs)
441{
442 struct branch *curr_branch = branch_get("HEAD");
443 const char *remote = curr_branch ? curr_branch->remote_name : NULL;
444
445 if (*refspecs) {
b7b31471
PT
446 if (opt_rebase)
447 fprintf_ln(stderr, _("There is no candidate for rebasing against among the refs that you just fetched."));
448 else
449 fprintf_ln(stderr, _("There are no candidates for merging among the refs that you just fetched."));
44c175c7
PT
450 fprintf_ln(stderr, _("Generally this means that you provided a wildcard refspec which had no\n"
451 "matches on the remote end."));
452 } else if (repo && curr_branch && (!remote || strcmp(repo, remote))) {
453 fprintf_ln(stderr, _("You asked to pull from the remote '%s', but did not specify\n"
454 "a branch. Because this is not the default configured remote\n"
455 "for your current branch, you must specify a branch on the command line."),
456 repo);
457 } else if (!curr_branch) {
458 fprintf_ln(stderr, _("You are not currently on a branch."));
b7b31471
PT
459 if (opt_rebase)
460 fprintf_ln(stderr, _("Please specify which branch you want to rebase against."));
461 else
462 fprintf_ln(stderr, _("Please specify which branch you want to merge with."));
44c175c7
PT
463 fprintf_ln(stderr, _("See git-pull(1) for details."));
464 fprintf(stderr, "\n");
8a0de58a 465 fprintf_ln(stderr, " git pull %s %s", _("<remote>"), _("<branch>"));
44c175c7
PT
466 fprintf(stderr, "\n");
467 } else if (!curr_branch->merge_nr) {
468 const char *remote_name = NULL;
469
470 if (for_each_remote(get_only_remote, &remote_name) || !remote_name)
8a0de58a 471 remote_name = _("<remote>");
44c175c7
PT
472
473 fprintf_ln(stderr, _("There is no tracking information for the current branch."));
b7b31471
PT
474 if (opt_rebase)
475 fprintf_ln(stderr, _("Please specify which branch you want to rebase against."));
476 else
477 fprintf_ln(stderr, _("Please specify which branch you want to merge with."));
44c175c7
PT
478 fprintf_ln(stderr, _("See git-pull(1) for details."));
479 fprintf(stderr, "\n");
8a0de58a 480 fprintf_ln(stderr, " git pull %s %s", _("<remote>"), _("<branch>"));
44c175c7 481 fprintf(stderr, "\n");
daf9f649
VA
482 fprintf_ln(stderr, _("If you wish to set tracking information for this branch you can do so with:"));
483 fprintf(stderr, "\n");
484 fprintf_ln(stderr, " git branch --set-upstream-to=%s/%s %s\n",
485 remote_name, _("<branch>"), curr_branch->name);
44c175c7
PT
486 } else
487 fprintf_ln(stderr, _("Your configuration specifies to merge with the ref '%s'\n"
488 "from the remote, but no such ref was fetched."),
489 *curr_branch->merge_name);
490 exit(1);
491}
492
f2c5baa1
PT
493/**
494 * Parses argv into [<repo> [<refspecs>...]], returning their values in `repo`
495 * as a string and `refspecs` as a null-terminated array of strings. If `repo`
496 * is not provided in argv, it is set to NULL.
497 */
498static void parse_repo_refspecs(int argc, const char **argv, const char **repo,
499 const char ***refspecs)
500{
501 if (argc > 0) {
502 *repo = *argv++;
503 argc--;
504 } else
505 *repo = NULL;
506 *refspecs = argv;
507}
508
509/**
510 * Runs git-fetch, returning its exit status. `repo` and `refspecs` are the
511 * repository and refspecs to fetch, or NULL if they are not provided.
512 */
513static int run_fetch(const char *repo, const char **refspecs)
514{
22f9b7f3 515 struct strvec args = STRVEC_INIT;
f2c5baa1
PT
516 int ret;
517
22f9b7f3 518 strvec_pushl(&args, "fetch", "--update-head-ok", NULL);
2a747902
PT
519
520 /* Shared options */
521 argv_push_verbosity(&args);
522 if (opt_progress)
22f9b7f3 523 strvec_push(&args, opt_progress);
2a747902 524
a32975f5
PT
525 /* Options passed to git-fetch */
526 if (opt_all)
22f9b7f3 527 strvec_push(&args, opt_all);
a32975f5 528 if (opt_append)
22f9b7f3 529 strvec_push(&args, opt_append);
a32975f5 530 if (opt_upload_pack)
22f9b7f3 531 strvec_push(&args, opt_upload_pack);
a32975f5
PT
532 argv_push_force(&args);
533 if (opt_tags)
22f9b7f3 534 strvec_push(&args, opt_tags);
a32975f5 535 if (opt_prune)
22f9b7f3 536 strvec_push(&args, opt_prune);
a6d7eb2c
SB
537 if (recurse_submodules != RECURSE_SUBMODULES_DEFAULT)
538 switch (recurse_submodules) {
539 case RECURSE_SUBMODULES_ON:
22f9b7f3 540 strvec_push(&args, "--recurse-submodules=on");
a6d7eb2c
SB
541 break;
542 case RECURSE_SUBMODULES_OFF:
22f9b7f3 543 strvec_push(&args, "--recurse-submodules=no");
a6d7eb2c
SB
544 break;
545 case RECURSE_SUBMODULES_ON_DEMAND:
22f9b7f3 546 strvec_push(&args, "--recurse-submodules=on-demand");
a6d7eb2c
SB
547 break;
548 default:
549 BUG("submodule recursion option not understood");
550 }
62104ba1 551 if (max_children)
22f9b7f3 552 strvec_push(&args, max_children);
a32975f5 553 if (opt_dry_run)
22f9b7f3 554 strvec_push(&args, "--dry-run");
a32975f5 555 if (opt_keep)
22f9b7f3 556 strvec_push(&args, opt_keep);
a32975f5 557 if (opt_depth)
22f9b7f3 558 strvec_push(&args, opt_depth);
a32975f5 559 if (opt_unshallow)
22f9b7f3 560 strvec_push(&args, opt_unshallow);
a32975f5 561 if (opt_update_shallow)
22f9b7f3 562 strvec_push(&args, opt_update_shallow);
a32975f5 563 if (opt_refmap)
22f9b7f3 564 strvec_push(&args, opt_refmap);
ffb4568a 565 if (opt_ipv4)
22f9b7f3 566 strvec_push(&args, opt_ipv4);
ffb4568a 567 if (opt_ipv6)
22f9b7f3 568 strvec_push(&args, opt_ipv6);
3883c551 569 if (opt_show_forced_updates > 0)
22f9b7f3 570 strvec_push(&args, "--show-forced-updates");
3883c551 571 else if (opt_show_forced_updates == 0)
22f9b7f3 572 strvec_push(&args, "--no-show-forced-updates");
24bc1a12 573 if (set_upstream)
22f9b7f3 574 strvec_push(&args, set_upstream);
d70a9eb6 575 strvec_pushv(&args, opt_fetch.v);
a32975f5 576
f2c5baa1 577 if (repo) {
22f9b7f3
JK
578 strvec_push(&args, repo);
579 strvec_pushv(&args, refspecs);
f2c5baa1 580 } else if (*refspecs)
033abf97 581 BUG("refspecs without repo?");
d70a9eb6 582 ret = run_command_v_opt(args.v, RUN_GIT_CMD);
22f9b7f3 583 strvec_clear(&args);
f2c5baa1
PT
584 return ret;
585}
586
49ec402d
PT
587/**
588 * "Pulls into void" by branching off merge_head.
589 */
ee3051bd 590static int pull_into_void(const struct object_id *merge_head,
f9b11147 591 const struct object_id *curr_head)
49ec402d 592{
01a31f3b
JK
593 if (opt_verify_signatures) {
594 struct commit *commit;
595
596 commit = lookup_commit(the_repository, merge_head);
597 if (!commit)
598 die(_("unable to access commit %s"),
599 oid_to_hex(merge_head));
600
54887b46
HJI
601 verify_merge_signature(commit, opt_verbosity,
602 check_trust_level);
01a31f3b
JK
603 }
604
49ec402d
PT
605 /*
606 * Two-way merge: we treat the index as based on an empty tree,
607 * and try to fast-forward to HEAD. This ensures we will not lose
608 * index/worktree changes that the user already made on the unborn
609 * branch.
610 */
7e196c3a
NTND
611 if (checkout_fast_forward(the_repository,
612 the_hash_algo->empty_tree,
613 merge_head, 0))
49ec402d
PT
614 return 1;
615
ae077771 616 if (update_ref("initial pull", "HEAD", merge_head, curr_head, 0, UPDATE_REFS_DIE_ON_ERR))
49ec402d
PT
617 return 1;
618
619 return 0;
620}
621
a6d7eb2c
SB
622static int rebase_submodules(void)
623{
624 struct child_process cp = CHILD_PROCESS_INIT;
625
626 cp.git_cmd = 1;
627 cp.no_stdin = 1;
22f9b7f3 628 strvec_pushl(&cp.args, "submodule", "update",
f6d8942b 629 "--recursive", "--rebase", NULL);
a56771a6 630 argv_push_verbosity(&cp.args);
a6d7eb2c
SB
631
632 return run_command(&cp);
633}
634
635static int update_submodules(void)
636{
637 struct child_process cp = CHILD_PROCESS_INIT;
638
639 cp.git_cmd = 1;
640 cp.no_stdin = 1;
22f9b7f3 641 strvec_pushl(&cp.args, "submodule", "update",
f6d8942b 642 "--recursive", "--checkout", NULL);
a56771a6 643 argv_push_verbosity(&cp.args);
a6d7eb2c
SB
644
645 return run_command(&cp);
646}
647
f2c5baa1
PT
648/**
649 * Runs git-merge, returning its exit status.
650 */
651static int run_merge(void)
652{
653 int ret;
22f9b7f3 654 struct strvec args = STRVEC_INIT;
f2c5baa1 655
22f9b7f3 656 strvec_pushl(&args, "merge", NULL);
2a747902
PT
657
658 /* Shared options */
659 argv_push_verbosity(&args);
660 if (opt_progress)
22f9b7f3 661 strvec_push(&args, opt_progress);
2a747902 662
11b6d178
PT
663 /* Options passed to git-merge */
664 if (opt_diffstat)
22f9b7f3 665 strvec_push(&args, opt_diffstat);
11b6d178 666 if (opt_log)
22f9b7f3 667 strvec_push(&args, opt_log);
3a4d2c74 668 if (opt_signoff)
22f9b7f3 669 strvec_push(&args, opt_signoff);
11b6d178 670 if (opt_squash)
22f9b7f3 671 strvec_push(&args, opt_squash);
11b6d178 672 if (opt_commit)
22f9b7f3 673 strvec_push(&args, opt_commit);
11b6d178 674 if (opt_edit)
22f9b7f3 675 strvec_push(&args, opt_edit);
d540b70c 676 if (cleanup_arg)
22f9b7f3 677 strvec_pushf(&args, "--cleanup=%s", cleanup_arg);
11b6d178 678 if (opt_ff)
22f9b7f3 679 strvec_push(&args, opt_ff);
11b6d178 680 if (opt_verify_signatures)
22f9b7f3 681 strvec_push(&args, opt_verify_signatures);
d70a9eb6
JK
682 strvec_pushv(&args, opt_strategies.v);
683 strvec_pushv(&args, opt_strategy_opts.v);
11b6d178 684 if (opt_gpg_sign)
22f9b7f3 685 strvec_push(&args, opt_gpg_sign);
d9f15d37 686 if (opt_autostash == 0)
22f9b7f3 687 strvec_push(&args, "--no-autostash");
d9f15d37 688 else if (opt_autostash == 1)
22f9b7f3 689 strvec_push(&args, "--autostash");
09c2cb87 690 if (opt_allow_unrelated_histories > 0)
22f9b7f3 691 strvec_push(&args, "--allow-unrelated-histories");
11b6d178 692
22f9b7f3 693 strvec_push(&args, "FETCH_HEAD");
d70a9eb6 694 ret = run_command_v_opt(args.v, RUN_GIT_CMD);
22f9b7f3 695 strvec_clear(&args);
f2c5baa1
PT
696 return ret;
697}
698
1678b81e
PT
699/**
700 * Returns remote's upstream branch for the current branch. If remote is NULL,
701 * the current branch's configured default remote is used. Returns NULL if
702 * `remote` does not name a valid remote, HEAD does not point to a branch,
703 * remote is not the branch's configured remote or the branch does not have any
704 * configured upstream branch.
705 */
706static const char *get_upstream_branch(const char *remote)
707{
708 struct remote *rm;
709 struct branch *curr_branch;
710 const char *curr_branch_remote;
711
712 rm = remote_get(remote);
713 if (!rm)
714 return NULL;
715
716 curr_branch = branch_get("HEAD");
717 if (!curr_branch)
718 return NULL;
719
720 curr_branch_remote = remote_for_branch(curr_branch, NULL);
721 assert(curr_branch_remote);
722
723 if (strcmp(curr_branch_remote, rm->name))
724 return NULL;
725
726 return branch_get_upstream(curr_branch, NULL);
727}
728
729/**
30aa96cd 730 * Derives the remote-tracking branch from the remote and refspec.
1678b81e
PT
731 *
732 * FIXME: The current implementation assumes the default mapping of
733 * refs/heads/<branch_name> to refs/remotes/<remote_name>/<branch_name>.
734 */
735static const char *get_tracking_branch(const char *remote, const char *refspec)
736{
895d3912 737 struct refspec_item spec;
1678b81e
PT
738 const char *spec_src;
739 const char *merge_branch;
740
dc064221 741 refspec_item_init_or_die(&spec, refspec, REFSPEC_FETCH);
895d3912 742 spec_src = spec.src;
1678b81e
PT
743 if (!*spec_src || !strcmp(spec_src, "HEAD"))
744 spec_src = "HEAD";
745 else if (skip_prefix(spec_src, "heads/", &spec_src))
746 ;
747 else if (skip_prefix(spec_src, "refs/heads/", &spec_src))
748 ;
749 else if (starts_with(spec_src, "refs/") ||
750 starts_with(spec_src, "tags/") ||
751 starts_with(spec_src, "remotes/"))
752 spec_src = "";
753
754 if (*spec_src) {
755 if (!strcmp(remote, "."))
756 merge_branch = mkpath("refs/heads/%s", spec_src);
757 else
758 merge_branch = mkpath("refs/remotes/%s/%s", remote, spec_src);
759 } else
760 merge_branch = NULL;
761
895d3912 762 refspec_item_clear(&spec);
1678b81e
PT
763 return merge_branch;
764}
765
766/**
767 * Given the repo and refspecs, sets fork_point to the point at which the
30aa96cd 768 * current branch forked from its remote-tracking branch. Returns 0 on success,
1678b81e
PT
769 * -1 on failure.
770 */
f9b11147 771static int get_rebase_fork_point(struct object_id *fork_point, const char *repo,
1678b81e
PT
772 const char *refspec)
773{
774 int ret;
775 struct branch *curr_branch;
776 const char *remote_branch;
777 struct child_process cp = CHILD_PROCESS_INIT;
778 struct strbuf sb = STRBUF_INIT;
779
780 curr_branch = branch_get("HEAD");
781 if (!curr_branch)
782 return -1;
783
784 if (refspec)
785 remote_branch = get_tracking_branch(repo, refspec);
786 else
787 remote_branch = get_upstream_branch(repo);
788
789 if (!remote_branch)
790 return -1;
791
22f9b7f3 792 strvec_pushl(&cp.args, "merge-base", "--fork-point",
f6d8942b 793 remote_branch, curr_branch->name, NULL);
1678b81e
PT
794 cp.no_stdin = 1;
795 cp.no_stderr = 1;
796 cp.git_cmd = 1;
797
fbfc089d 798 ret = capture_command(&cp, &sb, GIT_MAX_HEXSZ);
1678b81e
PT
799 if (ret)
800 goto cleanup;
801
f9b11147 802 ret = get_oid_hex(sb.buf, fork_point);
1678b81e
PT
803 if (ret)
804 goto cleanup;
805
806cleanup:
807 strbuf_release(&sb);
808 return ret ? -1 : 0;
809}
810
811/**
812 * Sets merge_base to the octopus merge base of curr_head, merge_head and
813 * fork_point. Returns 0 if a merge base is found, 1 otherwise.
814 */
f9b11147 815static int get_octopus_merge_base(struct object_id *merge_base,
816 const struct object_id *curr_head,
ee3051bd 817 const struct object_id *merge_head,
f9b11147 818 const struct object_id *fork_point)
1678b81e
PT
819{
820 struct commit_list *revs = NULL, *result;
821
2122f675
SB
822 commit_list_insert(lookup_commit_reference(the_repository, curr_head),
823 &revs);
824 commit_list_insert(lookup_commit_reference(the_repository, merge_head),
825 &revs);
f9b11147 826 if (!is_null_oid(fork_point))
2122f675
SB
827 commit_list_insert(lookup_commit_reference(the_repository, fork_point),
828 &revs);
1678b81e 829
4da72644 830 result = get_octopus_merge_bases(revs);
1678b81e 831 free_commit_list(revs);
4da72644
832 reduce_heads_replace(&result);
833
1678b81e
PT
834 if (!result)
835 return 1;
836
f9b11147 837 oidcpy(merge_base, &result->item->object.oid);
4da72644 838 free_commit_list(result);
1678b81e
PT
839 return 0;
840}
841
842/**
fbfc089d 843 * Given the current HEAD oid, the merge head returned from git-fetch and the
4f66d79a
PB
844 * fork point calculated by get_rebase_fork_point(), compute the <newbase> and
845 * <upstream> arguments to use for the upcoming git-rebase invocation.
1678b81e 846 */
4f66d79a
PB
847static int get_rebase_newbase_and_upstream(struct object_id *newbase,
848 struct object_id *upstream,
849 const struct object_id *curr_head,
ee3051bd 850 const struct object_id *merge_head,
f9b11147 851 const struct object_id *fork_point)
1678b81e 852{
f9b11147 853 struct object_id oct_merge_base;
1678b81e 854
f9b11147 855 if (!get_octopus_merge_base(&oct_merge_base, curr_head, merge_head, fork_point))
4a7e27e9 856 if (!is_null_oid(fork_point) && oideq(&oct_merge_base, fork_point))
1678b81e
PT
857 fork_point = NULL;
858
4f66d79a
PB
859 if (fork_point && !is_null_oid(fork_point))
860 oidcpy(upstream, fork_point);
861 else
862 oidcpy(upstream, merge_head);
863
864 oidcpy(newbase, merge_head);
865
866 return 0;
867}
868
869/**
870 * Given the <newbase> and <upstream> calculated by
871 * get_rebase_newbase_and_upstream(), runs git-rebase with the
872 * appropriate arguments and returns its exit status.
873 */
874static int run_rebase(const struct object_id *newbase,
875 const struct object_id *upstream)
876{
877 int ret;
878 struct strvec args = STRVEC_INIT;
879
22f9b7f3 880 strvec_push(&args, "rebase");
1678b81e
PT
881
882 /* Shared options */
883 argv_push_verbosity(&args);
884
885 /* Options passed to git-rebase */
1131ec98 886 if (opt_rebase == REBASE_MERGES)
22f9b7f3 887 strvec_push(&args, "--rebase-merges");
1131ec98 888 else if (opt_rebase == REBASE_PRESERVE)
22f9b7f3 889 strvec_push(&args, "--preserve-merges");
f5eb87b9 890 else if (opt_rebase == REBASE_INTERACTIVE)
22f9b7f3 891 strvec_push(&args, "--interactive");
1678b81e 892 if (opt_diffstat)
22f9b7f3 893 strvec_push(&args, opt_diffstat);
d70a9eb6
JK
894 strvec_pushv(&args, opt_strategies.v);
895 strvec_pushv(&args, opt_strategy_opts.v);
1678b81e 896 if (opt_gpg_sign)
22f9b7f3 897 strvec_push(&args, opt_gpg_sign);
f66398eb 898 if (opt_autostash == 0)
22f9b7f3 899 strvec_push(&args, "--no-autostash");
f66398eb 900 else if (opt_autostash == 1)
22f9b7f3 901 strvec_push(&args, "--autostash");
c57e501c
AH
902 if (opt_verify_signatures &&
903 !strcmp(opt_verify_signatures, "--verify-signatures"))
904 warning(_("ignoring --verify-signatures for rebase"));
1678b81e 905
22f9b7f3 906 strvec_push(&args, "--onto");
4f66d79a 907 strvec_push(&args, oid_to_hex(newbase));
1678b81e 908
4f66d79a 909 strvec_push(&args, oid_to_hex(upstream));
1678b81e 910
d70a9eb6 911 ret = run_command_v_opt(args.v, RUN_GIT_CMD);
22f9b7f3 912 strvec_clear(&args);
1678b81e
PT
913 return ret;
914}
915
77a7ec63
FC
916static int get_can_ff(struct object_id *orig_head, struct object_id *orig_merge_head)
917{
918 int ret;
919 struct commit_list *list = NULL;
920 struct commit *merge_head, *head;
921
922 head = lookup_commit_reference(the_repository, orig_head);
923 commit_list_insert(head, &list);
924 merge_head = lookup_commit_reference(the_repository, orig_merge_head);
925 ret = repo_is_descendant_of(the_repository, merge_head, list);
926 free_commit_list(list);
927 return ret;
928}
929
1e1ea69f
PT
930int cmd_pull(int argc, const char **argv, const char *prefix)
931{
f2c5baa1 932 const char *repo, **refspecs;
910650d2 933 struct oid_array merge_heads = OID_ARRAY_INIT;
f9b11147 934 struct object_id orig_head, curr_head;
935 struct object_id rebase_fork_point;
f15e7cf5 936 int autostash;
f2c5baa1 937
41fca098
PT
938 if (!getenv("GIT_REFLOG_ACTION"))
939 set_reflog_message(argc, argv);
940
cad0c692
NMC
941 git_config(git_pull_config, NULL);
942
1e1ea69f
PT
943 argc = parse_options(argc, argv, prefix, pull_options, pull_usage, 0);
944
d540b70c
DL
945 if (cleanup_arg)
946 /*
947 * this only checks the validity of cleanup_arg; we don't need
948 * a valid value for use_editor
949 */
950 get_cleanup_mode(cleanup_arg, 0);
951
f2c5baa1
PT
952 parse_repo_refspecs(argc, argv, &repo, &refspecs);
953
a9de9897
PT
954 if (!opt_ff)
955 opt_ff = xstrdup_or_null(config_get_ff());
956
81dbd768
PT
957 if (opt_rebase < 0)
958 opt_rebase = config_get_rebase();
959
4a4cf9e8 960 if (read_cache_unmerged())
8785c425 961 die_resolve_conflict("pull");
4a4cf9e8 962
102de880 963 if (file_exists(git_path_merge_head(the_repository)))
4a4cf9e8
PT
964 die_conclude_merge();
965
f9b11147 966 if (get_oid("HEAD", &orig_head))
967 oidclr(&orig_head);
fe911b8c 968
f15e7cf5 969 autostash = config_autostash;
8944969c 970 if (opt_rebase) {
f66398eb
MJ
971 if (opt_autostash != -1)
972 autostash = opt_autostash;
53c76dc0 973
f9b11147 974 if (is_null_oid(&orig_head) && !is_cache_unborn())
8944969c
PT
975 die(_("Updating an unborn branch with changes added to the index."));
976
53c76dc0 977 if (!autostash)
5b02ca38
NTND
978 require_clean_work_tree(the_repository,
979 N_("pull with rebase"),
d8cc92ab 980 _("please commit or stash them."), 1, 0);
8944969c 981
f9b11147 982 if (get_rebase_fork_point(&rebase_fork_point, repo, *refspecs))
983 oidclr(&rebase_fork_point);
8944969c 984 }
1678b81e 985
f2c5baa1
PT
986 if (run_fetch(repo, refspecs))
987 return 1;
988
a32975f5
PT
989 if (opt_dry_run)
990 return 0;
991
f9b11147 992 if (get_oid("HEAD", &curr_head))
993 oidclr(&curr_head);
fe911b8c 994
f9b11147 995 if (!is_null_oid(&orig_head) && !is_null_oid(&curr_head) &&
9001dc2a 996 !oideq(&orig_head, &curr_head)) {
fe911b8c
PT
997 /*
998 * The fetch involved updating the current branch.
999 *
1000 * The working tree and the index file are still based on
1001 * orig_head commit, but we are merging into curr_head.
1002 * Update the working tree to match curr_head.
1003 */
1004
1005 warning(_("fetch updated the current branch head.\n"
1006 "fast-forwarding your working tree from\n"
f9b11147 1007 "commit %s."), oid_to_hex(&orig_head));
fe911b8c 1008
7e196c3a
NTND
1009 if (checkout_fast_forward(the_repository, &orig_head,
1010 &curr_head, 0))
fe911b8c
PT
1011 die(_("Cannot fast-forward your working tree.\n"
1012 "After making sure that you saved anything precious from\n"
1013 "$ git diff %s\n"
1014 "output, run\n"
1015 "$ git reset --hard\n"
f9b11147 1016 "to recover."), oid_to_hex(&orig_head));
fe911b8c
PT
1017 }
1018
44c175c7
PT
1019 get_merge_heads(&merge_heads);
1020
1021 if (!merge_heads.nr)
1022 die_no_merge_candidates(repo, refspecs);
1023
f9b11147 1024 if (is_null_oid(&orig_head)) {
49ec402d
PT
1025 if (merge_heads.nr > 1)
1026 die(_("Cannot merge multiple branches into empty head."));
ee3051bd 1027 return pull_into_void(merge_heads.oid, &curr_head);
33b842a1
JH
1028 }
1029 if (opt_rebase && merge_heads.nr > 1)
1030 die(_("Cannot rebase onto multiple branches."));
1031
278f4be8
FC
1032 if (default_mode && opt_verbosity >= 0 && !opt_ff) {
1033 advise(_("Pulling without specifying how to reconcile divergent branches is\n"
1034 "discouraged. You can squelch this message by running one of the following\n"
1035 "commands sometime before your next pull:\n"
1036 "\n"
1037 " git config pull.rebase false # merge (the default strategy)\n"
1038 " git config pull.rebase true # rebase\n"
1039 " git config pull.ff only # fast-forward only\n"
1040 "\n"
1041 "You can replace \"git config\" with \"git config --global\" to set a default\n"
1042 "preference for all repositories. You can also pass --rebase, --no-rebase,\n"
1043 "or --ff-only on the command line to override the configured default per\n"
1044 "invocation.\n"));
1045 }
1046
33b842a1 1047 if (opt_rebase) {
a6d7eb2c 1048 int ret = 0;
fbae70dd 1049 int ran_ff = 0;
4f66d79a
PB
1050
1051 struct object_id newbase;
1052 struct object_id upstream;
1053 get_rebase_newbase_and_upstream(&newbase, &upstream, &curr_head,
1054 merge_heads.oid, &rebase_fork_point);
1055
a6d7eb2c
SB
1056 if ((recurse_submodules == RECURSE_SUBMODULES_ON ||
1057 recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND) &&
5176f20f 1058 submodule_touches_in_range(the_repository, &upstream, &curr_head))
a6d7eb2c 1059 die(_("cannot rebase with locally recorded submodule modifications"));
f15e7cf5 1060 if (!autostash) {
77a7ec63 1061 if (get_can_ff(&orig_head, &merge_heads.oid[0])) {
f15e7cf5
TB
1062 /* we can fast-forward this without invoking rebase */
1063 opt_ff = "--ff-only";
fbae70dd 1064 ran_ff = 1;
a6d7eb2c 1065 ret = run_merge();
f15e7cf5 1066 }
33b842a1 1067 }
fbae70dd 1068 if (!ran_ff)
4f66d79a 1069 ret = run_rebase(&newbase, &upstream);
a6d7eb2c
SB
1070
1071 if (!ret && (recurse_submodules == RECURSE_SUBMODULES_ON ||
1072 recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND))
1073 ret = rebase_submodules();
1074
1075 return ret;
33b842a1 1076 } else {
a6d7eb2c
SB
1077 int ret = run_merge();
1078 if (!ret && (recurse_submodules == RECURSE_SUBMODULES_ON ||
1079 recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND))
1080 ret = update_submodules();
1081 return ret;
33b842a1 1082 }
1e1ea69f 1083}