pull: check if in unresolved merge state
[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"
1e1ea69f
PT
16
17static const char * const pull_usage[] = {
f2c5baa1 18 N_("git pull [options] [<repository> [<refspec>...]]"),
1e1ea69f
PT
19 NULL
20};
21
2a747902
PT
22/* Shared options */
23static int opt_verbosity;
24static char *opt_progress;
25
11b6d178
PT
26/* Options passed to git-merge */
27static char *opt_diffstat;
28static char *opt_log;
29static char *opt_squash;
30static char *opt_commit;
31static char *opt_edit;
32static char *opt_ff;
33static char *opt_verify_signatures;
34static struct argv_array opt_strategies = ARGV_ARRAY_INIT;
35static struct argv_array opt_strategy_opts = ARGV_ARRAY_INIT;
36static char *opt_gpg_sign;
37
a32975f5
PT
38/* Options passed to git-fetch */
39static char *opt_all;
40static char *opt_append;
41static char *opt_upload_pack;
42static int opt_force;
43static char *opt_tags;
44static char *opt_prune;
45static char *opt_recurse_submodules;
46static int opt_dry_run;
47static char *opt_keep;
48static char *opt_depth;
49static char *opt_unshallow;
50static char *opt_update_shallow;
51static char *opt_refmap;
52
1e1ea69f 53static struct option pull_options[] = {
2a747902
PT
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),
59
11b6d178
PT
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),
102
a32975f5
PT
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"),
113 0),
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"),
132 0),
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),
142
1e1ea69f
PT
143 OPT_END()
144};
145
2a747902
PT
146/**
147 * Pushes "-q" or "-v" switches into arr to match the opt_verbosity level.
148 */
149static void argv_push_verbosity(struct argv_array *arr)
150{
151 int verbosity;
152
153 for (verbosity = opt_verbosity; verbosity > 0; verbosity--)
154 argv_array_push(arr, "-v");
155
156 for (verbosity = opt_verbosity; verbosity < 0; verbosity++)
157 argv_array_push(arr, "-q");
158}
159
a32975f5
PT
160/**
161 * Pushes "-f" switches into arr to match the opt_force level.
162 */
163static void argv_push_force(struct argv_array *arr)
164{
165 int force = opt_force;
166 while (force-- > 0)
167 argv_array_push(arr, "-f");
168}
169
a9de9897
PT
170/**
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.
175 */
176static const char *config_get_ff(void)
177{
178 const char *value;
179
180 if (git_config_get_value("pull.ff", &value))
181 return NULL;
182
183 switch (git_config_maybe_bool("pull.ff", value)) {
184 case 0:
185 return "--no-ff";
186 case 1:
187 return "--ff";
188 }
189
190 if (!strcmp(value, "only"))
191 return "--ff-only";
192
193 die(_("Invalid value for pull.ff: %s"), value);
194}
195
44c175c7
PT
196/**
197 * Appends merge candidates from FETCH_HEAD that are not marked not-for-merge
198 * into merge_heads.
199 */
200static void get_merge_heads(struct sha1_array *merge_heads)
201{
202 const char *filename = git_path("FETCH_HEAD");
203 FILE *fp;
204 struct strbuf sb = STRBUF_INIT;
205 unsigned char sha1[GIT_SHA1_RAWSZ];
206
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);
215 }
216 fclose(fp);
217 strbuf_release(&sb);
218}
219
220/**
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.
223 */
224static int get_only_remote(struct remote *remote, void *cb_data)
225{
226 const char **remote_name = cb_data;
227
228 if (*remote_name)
229 return -1;
230
231 *remote_name = remote->name;
232 return 0;
233}
234
235/**
236 * Dies with the appropriate reason for why there are no merge candidates:
237 *
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.
241 *
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.
245 *
246 * 3. We fetched from the branch's or repo's default remote, but:
247 *
248 * a. We are not on a branch, so there will never be a configured branch to
249 * merge with.
250 *
251 * b. We are on a branch, but there is no configured branch to merge with.
252 *
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.)
256 */
257static void NORETURN die_no_merge_candidates(const char *repo, const char **refspecs)
258{
259 struct branch *curr_branch = branch_get("HEAD");
260 const char *remote = curr_branch ? curr_branch->remote_name : NULL;
261
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;
280
281 if (for_each_remote(get_only_remote, &remote_name) || !remote_name)
282 remote_name = "<remote>";
283
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);
299}
300
f2c5baa1
PT
301/**
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.
305 */
306static void parse_repo_refspecs(int argc, const char **argv, const char **repo,
307 const char ***refspecs)
308{
309 if (argc > 0) {
310 *repo = *argv++;
311 argc--;
312 } else
313 *repo = NULL;
314 *refspecs = argv;
315}
316
317/**
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.
320 */
321static int run_fetch(const char *repo, const char **refspecs)
322{
323 struct argv_array args = ARGV_ARRAY_INIT;
324 int ret;
325
326 argv_array_pushl(&args, "fetch", "--update-head-ok", NULL);
2a747902
PT
327
328 /* Shared options */
329 argv_push_verbosity(&args);
330 if (opt_progress)
331 argv_array_push(&args, opt_progress);
332
a32975f5
PT
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);
359
f2c5baa1
PT
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;
368}
369
370/**
371 * Runs git-merge, returning its exit status.
372 */
373static int run_merge(void)
374{
375 int ret;
376 struct argv_array args = ARGV_ARRAY_INIT;
377
378 argv_array_pushl(&args, "merge", NULL);
2a747902
PT
379
380 /* Shared options */
381 argv_push_verbosity(&args);
382 if (opt_progress)
383 argv_array_push(&args, opt_progress);
384
11b6d178
PT
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);
404
f2c5baa1
PT
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;
409}
410
1e1ea69f
PT
411int cmd_pull(int argc, const char **argv, const char *prefix)
412{
f2c5baa1 413 const char *repo, **refspecs;
44c175c7 414 struct sha1_array merge_heads = SHA1_ARRAY_INIT;
f2c5baa1 415
1e1ea69f
PT
416 if (!getenv("_GIT_USE_BUILTIN_PULL")) {
417 const char *path = mkpath("%s/git-pull", git_exec_path());
418
419 if (sane_execvp(path, (char **)argv) < 0)
420 die_errno("could not exec %s", path);
421 }
422
423 argc = parse_options(argc, argv, prefix, pull_options, pull_usage, 0);
424
f2c5baa1
PT
425 parse_repo_refspecs(argc, argv, &repo, &refspecs);
426
a9de9897
PT
427 if (!opt_ff)
428 opt_ff = xstrdup_or_null(config_get_ff());
429
4a4cf9e8
PT
430 git_config(git_default_config, NULL);
431
432 if (read_cache_unmerged())
433 die_resolve_conflict("Pull");
434
435 if (file_exists(git_path("MERGE_HEAD")))
436 die_conclude_merge();
437
f2c5baa1
PT
438 if (run_fetch(repo, refspecs))
439 return 1;
440
a32975f5
PT
441 if (opt_dry_run)
442 return 0;
443
44c175c7
PT
444 get_merge_heads(&merge_heads);
445
446 if (!merge_heads.nr)
447 die_no_merge_candidates(repo, refspecs);
448
f2c5baa1 449 return run_merge();
1e1ea69f 450}