pull: check if in unresolved merge state
[git.git] / builtin / pull.c
blob1e688be4504d914774fa2629b3198f82a259fc0b
1 /*
2 * Builtin "git pull"
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.
7 */
8 #include "cache.h"
9 #include "builtin.h"
10 #include "parse-options.h"
11 #include "exec_cmd.h"
12 #include "run-command.h"
13 #include "sha1-array.h"
14 #include "remote.h"
15 #include "dir.h"
17 static const char * const pull_usage[] = {
18 N_("git pull [options] [<repository> [<refspec>...]]"),
19 NULL
22 /* Shared options */
23 static int opt_verbosity;
24 static char *opt_progress;
26 /* Options passed to git-merge */
27 static char *opt_diffstat;
28 static char *opt_log;
29 static char *opt_squash;
30 static char *opt_commit;
31 static char *opt_edit;
32 static char *opt_ff;
33 static char *opt_verify_signatures;
34 static struct argv_array opt_strategies = ARGV_ARRAY_INIT;
35 static struct argv_array opt_strategy_opts = ARGV_ARRAY_INIT;
36 static char *opt_gpg_sign;
38 /* Options passed to git-fetch */
39 static char *opt_all;
40 static char *opt_append;
41 static char *opt_upload_pack;
42 static int opt_force;
43 static char *opt_tags;
44 static char *opt_prune;
45 static char *opt_recurse_submodules;
46 static int opt_dry_run;
47 static char *opt_keep;
48 static char *opt_depth;
49 static char *opt_unshallow;
50 static char *opt_update_shallow;
51 static char *opt_refmap;
53 static struct option pull_options[] = {
54 /* Shared options */
55 OPT__VERBOSITY(&opt_verbosity),
56 OPT_PASSTHRU(0, "progress", &opt_progress, NULL,
57 N_("force progress reporting"),
58 PARSE_OPT_NOARG),
60 /* Options passed to git-merge */
61 OPT_GROUP(N_("Options related to merging")),
62 OPT_PASSTHRU('n', NULL, &opt_diffstat, NULL,
63 N_("do not show a diffstat at the end of the merge"),
64 PARSE_OPT_NOARG | PARSE_OPT_NONEG),
65 OPT_PASSTHRU(0, "stat", &opt_diffstat, NULL,
66 N_("show a diffstat at the end of the merge"),
67 PARSE_OPT_NOARG),
68 OPT_PASSTHRU(0, "summary", &opt_diffstat, NULL,
69 N_("(synonym to --stat)"),
70 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN),
71 OPT_PASSTHRU(0, "log", &opt_log, N_("n"),
72 N_("add (at most <n>) entries from shortlog to merge commit message"),
73 PARSE_OPT_OPTARG),
74 OPT_PASSTHRU(0, "squash", &opt_squash, NULL,
75 N_("create a single commit instead of doing a merge"),
76 PARSE_OPT_NOARG),
77 OPT_PASSTHRU(0, "commit", &opt_commit, NULL,
78 N_("perform a commit if the merge succeeds (default)"),
79 PARSE_OPT_NOARG),
80 OPT_PASSTHRU(0, "edit", &opt_edit, NULL,
81 N_("edit message before committing"),
82 PARSE_OPT_NOARG),
83 OPT_PASSTHRU(0, "ff", &opt_ff, NULL,
84 N_("allow fast-forward"),
85 PARSE_OPT_NOARG),
86 OPT_PASSTHRU(0, "ff-only", &opt_ff, NULL,
87 N_("abort if fast-forward is not possible"),
88 PARSE_OPT_NOARG | PARSE_OPT_NONEG),
89 OPT_PASSTHRU(0, "verify-signatures", &opt_verify_signatures, NULL,
90 N_("verify that the named commit has a valid GPG signature"),
91 PARSE_OPT_NOARG),
92 OPT_PASSTHRU_ARGV('s', "strategy", &opt_strategies, N_("strategy"),
93 N_("merge strategy to use"),
94 0),
95 OPT_PASSTHRU_ARGV('X', "strategy-option", &opt_strategy_opts,
96 N_("option=value"),
97 N_("option for selected merge strategy"),
98 0),
99 OPT_PASSTHRU('S', "gpg-sign", &opt_gpg_sign, N_("key-id"),
100 N_("GPG sign commit"),
101 PARSE_OPT_OPTARG),
103 /* Options passed to git-fetch */
104 OPT_GROUP(N_("Options related to fetching")),
105 OPT_PASSTHRU(0, "all", &opt_all, NULL,
106 N_("fetch from all remotes"),
107 PARSE_OPT_NOARG),
108 OPT_PASSTHRU('a', "append", &opt_append, NULL,
109 N_("append to .git/FETCH_HEAD instead of overwriting"),
110 PARSE_OPT_NOARG),
111 OPT_PASSTHRU(0, "upload-pack", &opt_upload_pack, N_("path"),
112 N_("path to upload pack on remote end"),
114 OPT__FORCE(&opt_force, N_("force overwrite of local branch")),
115 OPT_PASSTHRU('t', "tags", &opt_tags, NULL,
116 N_("fetch all tags and associated objects"),
117 PARSE_OPT_NOARG),
118 OPT_PASSTHRU('p', "prune", &opt_prune, NULL,
119 N_("prune remote-tracking branches no longer on remote"),
120 PARSE_OPT_NOARG),
121 OPT_PASSTHRU(0, "recurse-submodules", &opt_recurse_submodules,
122 N_("on-demand"),
123 N_("control recursive fetching of submodules"),
124 PARSE_OPT_OPTARG),
125 OPT_BOOL(0, "dry-run", &opt_dry_run,
126 N_("dry run")),
127 OPT_PASSTHRU('k', "keep", &opt_keep, NULL,
128 N_("keep downloaded pack"),
129 PARSE_OPT_NOARG),
130 OPT_PASSTHRU(0, "depth", &opt_depth, N_("depth"),
131 N_("deepen history of shallow clone"),
133 OPT_PASSTHRU(0, "unshallow", &opt_unshallow, NULL,
134 N_("convert to a complete repository"),
135 PARSE_OPT_NONEG | PARSE_OPT_NOARG),
136 OPT_PASSTHRU(0, "update-shallow", &opt_update_shallow, NULL,
137 N_("accept refs that update .git/shallow"),
138 PARSE_OPT_NOARG),
139 OPT_PASSTHRU(0, "refmap", &opt_refmap, N_("refmap"),
140 N_("specify fetch refmap"),
141 PARSE_OPT_NONEG),
143 OPT_END()
147 * Pushes "-q" or "-v" switches into arr to match the opt_verbosity level.
149 static void argv_push_verbosity(struct argv_array *arr)
151 int verbosity;
153 for (verbosity = opt_verbosity; verbosity > 0; verbosity--)
154 argv_array_push(arr, "-v");
156 for (verbosity = opt_verbosity; verbosity < 0; verbosity++)
157 argv_array_push(arr, "-q");
161 * Pushes "-f" switches into arr to match the opt_force level.
163 static void argv_push_force(struct argv_array *arr)
165 int force = opt_force;
166 while (force-- > 0)
167 argv_array_push(arr, "-f");
171 * If pull.ff is unset, returns NULL. If pull.ff is "true", returns "--ff". If
172 * pull.ff is "false", returns "--no-ff". If pull.ff is "only", returns
173 * "--ff-only". Otherwise, if pull.ff is set to an invalid value, die with an
174 * error.
176 static const char *config_get_ff(void)
178 const char *value;
180 if (git_config_get_value("pull.ff", &value))
181 return NULL;
183 switch (git_config_maybe_bool("pull.ff", value)) {
184 case 0:
185 return "--no-ff";
186 case 1:
187 return "--ff";
190 if (!strcmp(value, "only"))
191 return "--ff-only";
193 die(_("Invalid value for pull.ff: %s"), value);
197 * Appends merge candidates from FETCH_HEAD that are not marked not-for-merge
198 * into merge_heads.
200 static void get_merge_heads(struct sha1_array *merge_heads)
202 const char *filename = git_path("FETCH_HEAD");
203 FILE *fp;
204 struct strbuf sb = STRBUF_INIT;
205 unsigned char sha1[GIT_SHA1_RAWSZ];
207 if (!(fp = fopen(filename, "r")))
208 die_errno(_("could not open '%s' for reading"), filename);
209 while (strbuf_getline(&sb, fp, '\n') != EOF) {
210 if (get_sha1_hex(sb.buf, sha1))
211 continue; /* invalid line: does not start with SHA1 */
212 if (starts_with(sb.buf + GIT_SHA1_HEXSZ, "\tnot-for-merge\t"))
213 continue; /* ref is not-for-merge */
214 sha1_array_append(merge_heads, sha1);
216 fclose(fp);
217 strbuf_release(&sb);
221 * Used by die_no_merge_candidates() as a for_each_remote() callback to
222 * retrieve the name of the remote if the repository only has one remote.
224 static int get_only_remote(struct remote *remote, void *cb_data)
226 const char **remote_name = cb_data;
228 if (*remote_name)
229 return -1;
231 *remote_name = remote->name;
232 return 0;
236 * Dies with the appropriate reason for why there are no merge candidates:
238 * 1. We fetched from a specific remote, and a refspec was given, but it ended
239 * up not fetching anything. This is usually because the user provided a
240 * wildcard refspec which had no matches on the remote end.
242 * 2. We fetched from a non-default remote, but didn't specify a branch to
243 * merge. We can't use the configured one because it applies to the default
244 * remote, thus the user must specify the branches to merge.
246 * 3. We fetched from the branch's or repo's default remote, but:
248 * a. We are not on a branch, so there will never be a configured branch to
249 * merge with.
251 * b. We are on a branch, but there is no configured branch to merge with.
253 * 4. We fetched from the branch's or repo's default remote, but the configured
254 * branch to merge didn't get fetched. (Either it doesn't exist, or wasn't
255 * part of the configured fetch refspec.)
257 static void NORETURN die_no_merge_candidates(const char *repo, const char **refspecs)
259 struct branch *curr_branch = branch_get("HEAD");
260 const char *remote = curr_branch ? curr_branch->remote_name : NULL;
262 if (*refspecs) {
263 fprintf_ln(stderr, _("There are no candidates for merging among the refs that you just fetched."));
264 fprintf_ln(stderr, _("Generally this means that you provided a wildcard refspec which had no\n"
265 "matches on the remote end."));
266 } else if (repo && curr_branch && (!remote || strcmp(repo, remote))) {
267 fprintf_ln(stderr, _("You asked to pull from the remote '%s', but did not specify\n"
268 "a branch. Because this is not the default configured remote\n"
269 "for your current branch, you must specify a branch on the command line."),
270 repo);
271 } else if (!curr_branch) {
272 fprintf_ln(stderr, _("You are not currently on a branch."));
273 fprintf_ln(stderr, _("Please specify which branch you want to merge with."));
274 fprintf_ln(stderr, _("See git-pull(1) for details."));
275 fprintf(stderr, "\n");
276 fprintf_ln(stderr, " git pull <remote> <branch>");
277 fprintf(stderr, "\n");
278 } else if (!curr_branch->merge_nr) {
279 const char *remote_name = NULL;
281 if (for_each_remote(get_only_remote, &remote_name) || !remote_name)
282 remote_name = "<remote>";
284 fprintf_ln(stderr, _("There is no tracking information for the current branch."));
285 fprintf_ln(stderr, _("Please specify which branch you want to merge with."));
286 fprintf_ln(stderr, _("See git-pull(1) for details."));
287 fprintf(stderr, "\n");
288 fprintf_ln(stderr, " git pull <remote> <branch>");
289 fprintf(stderr, "\n");
290 fprintf_ln(stderr, _("If you wish to set tracking information for this branch you can do so with:\n"
291 "\n"
292 " git branch --set-upstream-to=%s/<branch> %s\n"),
293 remote_name, curr_branch->name);
294 } else
295 fprintf_ln(stderr, _("Your configuration specifies to merge with the ref '%s'\n"
296 "from the remote, but no such ref was fetched."),
297 *curr_branch->merge_name);
298 exit(1);
302 * Parses argv into [<repo> [<refspecs>...]], returning their values in `repo`
303 * as a string and `refspecs` as a null-terminated array of strings. If `repo`
304 * is not provided in argv, it is set to NULL.
306 static void parse_repo_refspecs(int argc, const char **argv, const char **repo,
307 const char ***refspecs)
309 if (argc > 0) {
310 *repo = *argv++;
311 argc--;
312 } else
313 *repo = NULL;
314 *refspecs = argv;
318 * Runs git-fetch, returning its exit status. `repo` and `refspecs` are the
319 * repository and refspecs to fetch, or NULL if they are not provided.
321 static int run_fetch(const char *repo, const char **refspecs)
323 struct argv_array args = ARGV_ARRAY_INIT;
324 int ret;
326 argv_array_pushl(&args, "fetch", "--update-head-ok", NULL);
328 /* Shared options */
329 argv_push_verbosity(&args);
330 if (opt_progress)
331 argv_array_push(&args, opt_progress);
333 /* Options passed to git-fetch */
334 if (opt_all)
335 argv_array_push(&args, opt_all);
336 if (opt_append)
337 argv_array_push(&args, opt_append);
338 if (opt_upload_pack)
339 argv_array_push(&args, opt_upload_pack);
340 argv_push_force(&args);
341 if (opt_tags)
342 argv_array_push(&args, opt_tags);
343 if (opt_prune)
344 argv_array_push(&args, opt_prune);
345 if (opt_recurse_submodules)
346 argv_array_push(&args, opt_recurse_submodules);
347 if (opt_dry_run)
348 argv_array_push(&args, "--dry-run");
349 if (opt_keep)
350 argv_array_push(&args, opt_keep);
351 if (opt_depth)
352 argv_array_push(&args, opt_depth);
353 if (opt_unshallow)
354 argv_array_push(&args, opt_unshallow);
355 if (opt_update_shallow)
356 argv_array_push(&args, opt_update_shallow);
357 if (opt_refmap)
358 argv_array_push(&args, opt_refmap);
360 if (repo) {
361 argv_array_push(&args, repo);
362 argv_array_pushv(&args, refspecs);
363 } else if (*refspecs)
364 die("BUG: refspecs without repo?");
365 ret = run_command_v_opt(args.argv, RUN_GIT_CMD);
366 argv_array_clear(&args);
367 return ret;
371 * Runs git-merge, returning its exit status.
373 static int run_merge(void)
375 int ret;
376 struct argv_array args = ARGV_ARRAY_INIT;
378 argv_array_pushl(&args, "merge", NULL);
380 /* Shared options */
381 argv_push_verbosity(&args);
382 if (opt_progress)
383 argv_array_push(&args, opt_progress);
385 /* Options passed to git-merge */
386 if (opt_diffstat)
387 argv_array_push(&args, opt_diffstat);
388 if (opt_log)
389 argv_array_push(&args, opt_log);
390 if (opt_squash)
391 argv_array_push(&args, opt_squash);
392 if (opt_commit)
393 argv_array_push(&args, opt_commit);
394 if (opt_edit)
395 argv_array_push(&args, opt_edit);
396 if (opt_ff)
397 argv_array_push(&args, opt_ff);
398 if (opt_verify_signatures)
399 argv_array_push(&args, opt_verify_signatures);
400 argv_array_pushv(&args, opt_strategies.argv);
401 argv_array_pushv(&args, opt_strategy_opts.argv);
402 if (opt_gpg_sign)
403 argv_array_push(&args, opt_gpg_sign);
405 argv_array_push(&args, "FETCH_HEAD");
406 ret = run_command_v_opt(args.argv, RUN_GIT_CMD);
407 argv_array_clear(&args);
408 return ret;
411 int cmd_pull(int argc, const char **argv, const char *prefix)
413 const char *repo, **refspecs;
414 struct sha1_array merge_heads = SHA1_ARRAY_INIT;
416 if (!getenv("_GIT_USE_BUILTIN_PULL")) {
417 const char *path = mkpath("%s/git-pull", git_exec_path());
419 if (sane_execvp(path, (char **)argv) < 0)
420 die_errno("could not exec %s", path);
423 argc = parse_options(argc, argv, prefix, pull_options, pull_usage, 0);
425 parse_repo_refspecs(argc, argv, &repo, &refspecs);
427 if (!opt_ff)
428 opt_ff = xstrdup_or_null(config_get_ff());
430 git_config(git_default_config, NULL);
432 if (read_cache_unmerged())
433 die_resolve_conflict("Pull");
435 if (file_exists(git_path("MERGE_HEAD")))
436 die_conclude_merge();
438 if (run_fetch(repo, refspecs))
439 return 1;
441 if (opt_dry_run)
442 return 0;
444 get_merge_heads(&merge_heads);
446 if (!merge_heads.nr)
447 die_no_merge_candidates(repo, refspecs);
449 return run_merge();