pull: implement pulling into an unborn branch
[git/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 */
8#include "cache.h"
9#include "builtin.h"
10#include "parse-options.h"
11#include "exec_cmd.h"
f2c5baa1 12#include "run-command.h"
44c175c7
PT
13#include "sha1-array.h"
14#include "remote.h"
4a4cf9e8 15#include "dir.h"
49ec402d 16#include "refs.h"
1e1ea69f
PT
17
18static const char * const pull_usage[] = {
f2c5baa1 19 N_("git pull [options] [<repository> [<refspec>...]]"),
1e1ea69f
PT
20 NULL
21};
22
2a747902
PT
23/* Shared options */
24static int opt_verbosity;
25static char *opt_progress;
26
11b6d178
PT
27/* Options passed to git-merge */
28static char *opt_diffstat;
29static char *opt_log;
30static char *opt_squash;
31static char *opt_commit;
32static char *opt_edit;
33static char *opt_ff;
34static char *opt_verify_signatures;
35static struct argv_array opt_strategies = ARGV_ARRAY_INIT;
36static struct argv_array opt_strategy_opts = ARGV_ARRAY_INIT;
37static char *opt_gpg_sign;
38
a32975f5
PT
39/* Options passed to git-fetch */
40static char *opt_all;
41static char *opt_append;
42static char *opt_upload_pack;
43static int opt_force;
44static char *opt_tags;
45static char *opt_prune;
46static char *opt_recurse_submodules;
47static int opt_dry_run;
48static char *opt_keep;
49static char *opt_depth;
50static char *opt_unshallow;
51static char *opt_update_shallow;
52static char *opt_refmap;
53
1e1ea69f 54static struct option pull_options[] = {
2a747902
PT
55 /* Shared options */
56 OPT__VERBOSITY(&opt_verbosity),
57 OPT_PASSTHRU(0, "progress", &opt_progress, NULL,
58 N_("force progress reporting"),
59 PARSE_OPT_NOARG),
60
11b6d178
PT
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"),
68 PARSE_OPT_NOARG),
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"),
74 PARSE_OPT_OPTARG),
75 OPT_PASSTHRU(0, "squash", &opt_squash, NULL,
76 N_("create a single commit instead of doing a merge"),
77 PARSE_OPT_NOARG),
78 OPT_PASSTHRU(0, "commit", &opt_commit, NULL,
79 N_("perform a commit if the merge succeeds (default)"),
80 PARSE_OPT_NOARG),
81 OPT_PASSTHRU(0, "edit", &opt_edit, NULL,
82 N_("edit message before committing"),
83 PARSE_OPT_NOARG),
84 OPT_PASSTHRU(0, "ff", &opt_ff, NULL,
85 N_("allow fast-forward"),
86 PARSE_OPT_NOARG),
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"),
92 PARSE_OPT_NOARG),
93 OPT_PASSTHRU_ARGV('s', "strategy", &opt_strategies, N_("strategy"),
94 N_("merge strategy to use"),
95 0),
96 OPT_PASSTHRU_ARGV('X', "strategy-option", &opt_strategy_opts,
97 N_("option=value"),
98 N_("option for selected merge strategy"),
99 0),
100 OPT_PASSTHRU('S', "gpg-sign", &opt_gpg_sign, N_("key-id"),
101 N_("GPG sign commit"),
102 PARSE_OPT_OPTARG),
103
a32975f5
PT
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"),
108 PARSE_OPT_NOARG),
109 OPT_PASSTHRU('a', "append", &opt_append, NULL,
110 N_("append to .git/FETCH_HEAD instead of overwriting"),
111 PARSE_OPT_NOARG),
112 OPT_PASSTHRU(0, "upload-pack", &opt_upload_pack, N_("path"),
113 N_("path to upload pack on remote end"),
114 0),
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"),
118 PARSE_OPT_NOARG),
119 OPT_PASSTHRU('p', "prune", &opt_prune, NULL,
120 N_("prune remote-tracking branches no longer on remote"),
121 PARSE_OPT_NOARG),
122 OPT_PASSTHRU(0, "recurse-submodules", &opt_recurse_submodules,
123 N_("on-demand"),
124 N_("control recursive fetching of submodules"),
125 PARSE_OPT_OPTARG),
126 OPT_BOOL(0, "dry-run", &opt_dry_run,
127 N_("dry run")),
128 OPT_PASSTHRU('k', "keep", &opt_keep, NULL,
129 N_("keep downloaded pack"),
130 PARSE_OPT_NOARG),
131 OPT_PASSTHRU(0, "depth", &opt_depth, N_("depth"),
132 N_("deepen history of shallow clone"),
133 0),
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"),
139 PARSE_OPT_NOARG),
140 OPT_PASSTHRU(0, "refmap", &opt_refmap, N_("refmap"),
141 N_("specify fetch refmap"),
142 PARSE_OPT_NONEG),
143
1e1ea69f
PT
144 OPT_END()
145};
146
2a747902
PT
147/**
148 * Pushes "-q" or "-v" switches into arr to match the opt_verbosity level.
149 */
150static void argv_push_verbosity(struct argv_array *arr)
151{
152 int verbosity;
153
154 for (verbosity = opt_verbosity; verbosity > 0; verbosity--)
155 argv_array_push(arr, "-v");
156
157 for (verbosity = opt_verbosity; verbosity < 0; verbosity++)
158 argv_array_push(arr, "-q");
159}
160
a32975f5
PT
161/**
162 * Pushes "-f" switches into arr to match the opt_force level.
163 */
164static void argv_push_force(struct argv_array *arr)
165{
166 int force = opt_force;
167 while (force-- > 0)
168 argv_array_push(arr, "-f");
169}
170
a9de9897
PT
171/**
172 * If pull.ff is unset, returns NULL. If pull.ff is "true", returns "--ff". If
173 * pull.ff is "false", returns "--no-ff". If pull.ff is "only", returns
174 * "--ff-only". Otherwise, if pull.ff is set to an invalid value, die with an
175 * error.
176 */
177static const char *config_get_ff(void)
178{
179 const char *value;
180
181 if (git_config_get_value("pull.ff", &value))
182 return NULL;
183
184 switch (git_config_maybe_bool("pull.ff", value)) {
185 case 0:
186 return "--no-ff";
187 case 1:
188 return "--ff";
189 }
190
191 if (!strcmp(value, "only"))
192 return "--ff-only";
193
194 die(_("Invalid value for pull.ff: %s"), value);
195}
196
44c175c7
PT
197/**
198 * Appends merge candidates from FETCH_HEAD that are not marked not-for-merge
199 * into merge_heads.
200 */
201static void get_merge_heads(struct sha1_array *merge_heads)
202{
203 const char *filename = git_path("FETCH_HEAD");
204 FILE *fp;
205 struct strbuf sb = STRBUF_INIT;
206 unsigned char sha1[GIT_SHA1_RAWSZ];
207
208 if (!(fp = fopen(filename, "r")))
209 die_errno(_("could not open '%s' for reading"), filename);
210 while (strbuf_getline(&sb, fp, '\n') != EOF) {
211 if (get_sha1_hex(sb.buf, sha1))
212 continue; /* invalid line: does not start with SHA1 */
213 if (starts_with(sb.buf + GIT_SHA1_HEXSZ, "\tnot-for-merge\t"))
214 continue; /* ref is not-for-merge */
215 sha1_array_append(merge_heads, sha1);
216 }
217 fclose(fp);
218 strbuf_release(&sb);
219}
220
221/**
222 * Used by die_no_merge_candidates() as a for_each_remote() callback to
223 * retrieve the name of the remote if the repository only has one remote.
224 */
225static int get_only_remote(struct remote *remote, void *cb_data)
226{
227 const char **remote_name = cb_data;
228
229 if (*remote_name)
230 return -1;
231
232 *remote_name = remote->name;
233 return 0;
234}
235
236/**
237 * Dies with the appropriate reason for why there are no merge candidates:
238 *
239 * 1. We fetched from a specific remote, and a refspec was given, but it ended
240 * up not fetching anything. This is usually because the user provided a
241 * wildcard refspec which had no matches on the remote end.
242 *
243 * 2. We fetched from a non-default remote, but didn't specify a branch to
244 * merge. We can't use the configured one because it applies to the default
245 * remote, thus the user must specify the branches to merge.
246 *
247 * 3. We fetched from the branch's or repo's default remote, but:
248 *
249 * a. We are not on a branch, so there will never be a configured branch to
250 * merge with.
251 *
252 * b. We are on a branch, but there is no configured branch to merge with.
253 *
254 * 4. We fetched from the branch's or repo's default remote, but the configured
255 * branch to merge didn't get fetched. (Either it doesn't exist, or wasn't
256 * part of the configured fetch refspec.)
257 */
258static void NORETURN die_no_merge_candidates(const char *repo, const char **refspecs)
259{
260 struct branch *curr_branch = branch_get("HEAD");
261 const char *remote = curr_branch ? curr_branch->remote_name : NULL;
262
263 if (*refspecs) {
264 fprintf_ln(stderr, _("There are no candidates for merging among the refs that you just fetched."));
265 fprintf_ln(stderr, _("Generally this means that you provided a wildcard refspec which had no\n"
266 "matches on the remote end."));
267 } else if (repo && curr_branch && (!remote || strcmp(repo, remote))) {
268 fprintf_ln(stderr, _("You asked to pull from the remote '%s', but did not specify\n"
269 "a branch. Because this is not the default configured remote\n"
270 "for your current branch, you must specify a branch on the command line."),
271 repo);
272 } else if (!curr_branch) {
273 fprintf_ln(stderr, _("You are not currently on a branch."));
274 fprintf_ln(stderr, _("Please specify which branch you want to merge with."));
275 fprintf_ln(stderr, _("See git-pull(1) for details."));
276 fprintf(stderr, "\n");
277 fprintf_ln(stderr, " git pull <remote> <branch>");
278 fprintf(stderr, "\n");
279 } else if (!curr_branch->merge_nr) {
280 const char *remote_name = NULL;
281
282 if (for_each_remote(get_only_remote, &remote_name) || !remote_name)
283 remote_name = "<remote>";
284
285 fprintf_ln(stderr, _("There is no tracking information for the current branch."));
286 fprintf_ln(stderr, _("Please specify which branch you want to merge with."));
287 fprintf_ln(stderr, _("See git-pull(1) for details."));
288 fprintf(stderr, "\n");
289 fprintf_ln(stderr, " git pull <remote> <branch>");
290 fprintf(stderr, "\n");
291 fprintf_ln(stderr, _("If you wish to set tracking information for this branch you can do so with:\n"
292 "\n"
293 " git branch --set-upstream-to=%s/<branch> %s\n"),
294 remote_name, curr_branch->name);
295 } else
296 fprintf_ln(stderr, _("Your configuration specifies to merge with the ref '%s'\n"
297 "from the remote, but no such ref was fetched."),
298 *curr_branch->merge_name);
299 exit(1);
300}
301
f2c5baa1
PT
302/**
303 * Parses argv into [<repo> [<refspecs>...]], returning their values in `repo`
304 * as a string and `refspecs` as a null-terminated array of strings. If `repo`
305 * is not provided in argv, it is set to NULL.
306 */
307static void parse_repo_refspecs(int argc, const char **argv, const char **repo,
308 const char ***refspecs)
309{
310 if (argc > 0) {
311 *repo = *argv++;
312 argc--;
313 } else
314 *repo = NULL;
315 *refspecs = argv;
316}
317
318/**
319 * Runs git-fetch, returning its exit status. `repo` and `refspecs` are the
320 * repository and refspecs to fetch, or NULL if they are not provided.
321 */
322static int run_fetch(const char *repo, const char **refspecs)
323{
324 struct argv_array args = ARGV_ARRAY_INIT;
325 int ret;
326
327 argv_array_pushl(&args, "fetch", "--update-head-ok", NULL);
2a747902
PT
328
329 /* Shared options */
330 argv_push_verbosity(&args);
331 if (opt_progress)
332 argv_array_push(&args, opt_progress);
333
a32975f5
PT
334 /* Options passed to git-fetch */
335 if (opt_all)
336 argv_array_push(&args, opt_all);
337 if (opt_append)
338 argv_array_push(&args, opt_append);
339 if (opt_upload_pack)
340 argv_array_push(&args, opt_upload_pack);
341 argv_push_force(&args);
342 if (opt_tags)
343 argv_array_push(&args, opt_tags);
344 if (opt_prune)
345 argv_array_push(&args, opt_prune);
346 if (opt_recurse_submodules)
347 argv_array_push(&args, opt_recurse_submodules);
348 if (opt_dry_run)
349 argv_array_push(&args, "--dry-run");
350 if (opt_keep)
351 argv_array_push(&args, opt_keep);
352 if (opt_depth)
353 argv_array_push(&args, opt_depth);
354 if (opt_unshallow)
355 argv_array_push(&args, opt_unshallow);
356 if (opt_update_shallow)
357 argv_array_push(&args, opt_update_shallow);
358 if (opt_refmap)
359 argv_array_push(&args, opt_refmap);
360
f2c5baa1
PT
361 if (repo) {
362 argv_array_push(&args, repo);
363 argv_array_pushv(&args, refspecs);
364 } else if (*refspecs)
365 die("BUG: refspecs without repo?");
366 ret = run_command_v_opt(args.argv, RUN_GIT_CMD);
367 argv_array_clear(&args);
368 return ret;
369}
370
49ec402d
PT
371/**
372 * "Pulls into void" by branching off merge_head.
373 */
374static int pull_into_void(const unsigned char *merge_head,
375 const unsigned char *curr_head)
376{
377 /*
378 * Two-way merge: we treat the index as based on an empty tree,
379 * and try to fast-forward to HEAD. This ensures we will not lose
380 * index/worktree changes that the user already made on the unborn
381 * branch.
382 */
383 if (checkout_fast_forward(EMPTY_TREE_SHA1_BIN, merge_head, 0))
384 return 1;
385
386 if (update_ref("initial pull", "HEAD", merge_head, curr_head, 0, UPDATE_REFS_DIE_ON_ERR))
387 return 1;
388
389 return 0;
390}
391
f2c5baa1
PT
392/**
393 * Runs git-merge, returning its exit status.
394 */
395static int run_merge(void)
396{
397 int ret;
398 struct argv_array args = ARGV_ARRAY_INIT;
399
400 argv_array_pushl(&args, "merge", NULL);
2a747902
PT
401
402 /* Shared options */
403 argv_push_verbosity(&args);
404 if (opt_progress)
405 argv_array_push(&args, opt_progress);
406
11b6d178
PT
407 /* Options passed to git-merge */
408 if (opt_diffstat)
409 argv_array_push(&args, opt_diffstat);
410 if (opt_log)
411 argv_array_push(&args, opt_log);
412 if (opt_squash)
413 argv_array_push(&args, opt_squash);
414 if (opt_commit)
415 argv_array_push(&args, opt_commit);
416 if (opt_edit)
417 argv_array_push(&args, opt_edit);
418 if (opt_ff)
419 argv_array_push(&args, opt_ff);
420 if (opt_verify_signatures)
421 argv_array_push(&args, opt_verify_signatures);
422 argv_array_pushv(&args, opt_strategies.argv);
423 argv_array_pushv(&args, opt_strategy_opts.argv);
424 if (opt_gpg_sign)
425 argv_array_push(&args, opt_gpg_sign);
426
f2c5baa1
PT
427 argv_array_push(&args, "FETCH_HEAD");
428 ret = run_command_v_opt(args.argv, RUN_GIT_CMD);
429 argv_array_clear(&args);
430 return ret;
431}
432
1e1ea69f
PT
433int cmd_pull(int argc, const char **argv, const char *prefix)
434{
f2c5baa1 435 const char *repo, **refspecs;
44c175c7 436 struct sha1_array merge_heads = SHA1_ARRAY_INIT;
fe911b8c 437 unsigned char orig_head[GIT_SHA1_RAWSZ], curr_head[GIT_SHA1_RAWSZ];
f2c5baa1 438
1e1ea69f
PT
439 if (!getenv("_GIT_USE_BUILTIN_PULL")) {
440 const char *path = mkpath("%s/git-pull", git_exec_path());
441
442 if (sane_execvp(path, (char **)argv) < 0)
443 die_errno("could not exec %s", path);
444 }
445
446 argc = parse_options(argc, argv, prefix, pull_options, pull_usage, 0);
447
f2c5baa1
PT
448 parse_repo_refspecs(argc, argv, &repo, &refspecs);
449
a9de9897
PT
450 if (!opt_ff)
451 opt_ff = xstrdup_or_null(config_get_ff());
452
4a4cf9e8
PT
453 git_config(git_default_config, NULL);
454
455 if (read_cache_unmerged())
456 die_resolve_conflict("Pull");
457
458 if (file_exists(git_path("MERGE_HEAD")))
459 die_conclude_merge();
460
fe911b8c
PT
461 if (get_sha1("HEAD", orig_head))
462 hashclr(orig_head);
463
f2c5baa1
PT
464 if (run_fetch(repo, refspecs))
465 return 1;
466
a32975f5
PT
467 if (opt_dry_run)
468 return 0;
469
fe911b8c
PT
470 if (get_sha1("HEAD", curr_head))
471 hashclr(curr_head);
472
473 if (!is_null_sha1(orig_head) && !is_null_sha1(curr_head) &&
474 hashcmp(orig_head, curr_head)) {
475 /*
476 * The fetch involved updating the current branch.
477 *
478 * The working tree and the index file are still based on
479 * orig_head commit, but we are merging into curr_head.
480 * Update the working tree to match curr_head.
481 */
482
483 warning(_("fetch updated the current branch head.\n"
484 "fast-forwarding your working tree from\n"
485 "commit %s."), sha1_to_hex(orig_head));
486
487 if (checkout_fast_forward(orig_head, curr_head, 0))
488 die(_("Cannot fast-forward your working tree.\n"
489 "After making sure that you saved anything precious from\n"
490 "$ git diff %s\n"
491 "output, run\n"
492 "$ git reset --hard\n"
493 "to recover."), sha1_to_hex(orig_head));
494 }
495
44c175c7
PT
496 get_merge_heads(&merge_heads);
497
498 if (!merge_heads.nr)
499 die_no_merge_candidates(repo, refspecs);
500
49ec402d
PT
501 if (is_null_sha1(orig_head)) {
502 if (merge_heads.nr > 1)
503 die(_("Cannot merge multiple branches into empty head."));
504 return pull_into_void(*merge_heads.sha1, curr_head);
505 } else
506 return run_merge();
1e1ea69f 507}