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.
10 #include "parse-options.h"
12 #include "run-command.h"
13 #include "sha1-array.h"
16 static const char * const pull_usage[] = {
17 N_("git pull [options] [<repository> [<refspec>...]]"),
22 static int opt_verbosity;
23 static char *opt_progress;
25 /* Options passed to git-merge */
26 static char *opt_diffstat;
28 static char *opt_squash;
29 static char *opt_commit;
30 static char *opt_edit;
32 static char *opt_verify_signatures;
33 static struct argv_array opt_strategies = ARGV_ARRAY_INIT;
34 static struct argv_array opt_strategy_opts = ARGV_ARRAY_INIT;
35 static char *opt_gpg_sign;
37 /* Options passed to git-fetch */
39 static char *opt_append;
40 static char *opt_upload_pack;
42 static char *opt_tags;
43 static char *opt_prune;
44 static char *opt_recurse_submodules;
45 static int opt_dry_run;
46 static char *opt_keep;
47 static char *opt_depth;
48 static char *opt_unshallow;
49 static char *opt_update_shallow;
50 static char *opt_refmap;
52 static struct option pull_options[] = {
54 OPT__VERBOSITY(&opt_verbosity),
55 OPT_PASSTHRU(0, "progress", &opt_progress, NULL,
56 N_("force progress reporting"),
59 /* Options passed to git-merge */
60 OPT_GROUP(N_("Options related to merging")),
61 OPT_PASSTHRU('n', NULL, &opt_diffstat, NULL,
62 N_("do not show a diffstat at the end of the merge"),
63 PARSE_OPT_NOARG | PARSE_OPT_NONEG),
64 OPT_PASSTHRU(0, "stat", &opt_diffstat, NULL,
65 N_("show a diffstat at the end of the merge"),
67 OPT_PASSTHRU(0, "summary", &opt_diffstat, NULL,
68 N_("(synonym to --stat)"),
69 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN),
70 OPT_PASSTHRU(0, "log", &opt_log, N_("n"),
71 N_("add (at most <n>) entries from shortlog to merge commit message"),
73 OPT_PASSTHRU(0, "squash", &opt_squash, NULL,
74 N_("create a single commit instead of doing a merge"),
76 OPT_PASSTHRU(0, "commit", &opt_commit, NULL,
77 N_("perform a commit if the merge succeeds (default)"),
79 OPT_PASSTHRU(0, "edit", &opt_edit, NULL,
80 N_("edit message before committing"),
82 OPT_PASSTHRU(0, "ff", &opt_ff, NULL,
83 N_("allow fast-forward"),
85 OPT_PASSTHRU(0, "ff-only", &opt_ff, NULL,
86 N_("abort if fast-forward is not possible"),
87 PARSE_OPT_NOARG | PARSE_OPT_NONEG),
88 OPT_PASSTHRU(0, "verify-signatures", &opt_verify_signatures, NULL,
89 N_("verify that the named commit has a valid GPG signature"),
91 OPT_PASSTHRU_ARGV('s', "strategy", &opt_strategies, N_("strategy"),
92 N_("merge strategy to use"),
94 OPT_PASSTHRU_ARGV('X', "strategy-option", &opt_strategy_opts,
96 N_("option for selected merge strategy"),
98 OPT_PASSTHRU('S', "gpg-sign", &opt_gpg_sign, N_("key-id"),
99 N_("GPG sign commit"),
102 /* Options passed to git-fetch */
103 OPT_GROUP(N_("Options related to fetching")),
104 OPT_PASSTHRU(0, "all", &opt_all, NULL,
105 N_("fetch from all remotes"),
107 OPT_PASSTHRU('a', "append", &opt_append, NULL,
108 N_("append to .git/FETCH_HEAD instead of overwriting"),
110 OPT_PASSTHRU(0, "upload-pack", &opt_upload_pack, N_("path"),
111 N_("path to upload pack on remote end"),
113 OPT__FORCE(&opt_force, N_("force overwrite of local branch")),
114 OPT_PASSTHRU('t', "tags", &opt_tags, NULL,
115 N_("fetch all tags and associated objects"),
117 OPT_PASSTHRU('p', "prune", &opt_prune, NULL,
118 N_("prune remote-tracking branches no longer on remote"),
120 OPT_PASSTHRU(0, "recurse-submodules", &opt_recurse_submodules,
122 N_("control recursive fetching of submodules"),
124 OPT_BOOL(0, "dry-run", &opt_dry_run,
126 OPT_PASSTHRU('k', "keep", &opt_keep, NULL,
127 N_("keep downloaded pack"),
129 OPT_PASSTHRU(0, "depth", &opt_depth, N_("depth"),
130 N_("deepen history of shallow clone"),
132 OPT_PASSTHRU(0, "unshallow", &opt_unshallow, NULL,
133 N_("convert to a complete repository"),
134 PARSE_OPT_NONEG | PARSE_OPT_NOARG),
135 OPT_PASSTHRU(0, "update-shallow", &opt_update_shallow, NULL,
136 N_("accept refs that update .git/shallow"),
138 OPT_PASSTHRU(0, "refmap", &opt_refmap, N_("refmap"),
139 N_("specify fetch refmap"),
146 * Pushes "-q" or "-v" switches into arr to match the opt_verbosity level.
148 static void argv_push_verbosity(struct argv_array *arr)
152 for (verbosity = opt_verbosity; verbosity > 0; verbosity--)
153 argv_array_push(arr, "-v");
155 for (verbosity = opt_verbosity; verbosity < 0; verbosity++)
156 argv_array_push(arr, "-q");
160 * Pushes "-f" switches into arr to match the opt_force level.
162 static void argv_push_force(struct argv_array *arr)
164 int force = opt_force;
166 argv_array_push(arr, "-f");
170 * Appends merge candidates from FETCH_HEAD that are not marked not-for-merge
173 static void get_merge_heads(struct sha1_array *merge_heads)
175 const char *filename = git_path("FETCH_HEAD");
177 struct strbuf sb = STRBUF_INIT;
178 unsigned char sha1[GIT_SHA1_RAWSZ];
180 if (!(fp = fopen(filename, "r")))
181 die_errno(_("could not open '%s' for reading"), filename);
182 while (strbuf_getline(&sb, fp, '\n') != EOF) {
183 if (get_sha1_hex(sb.buf, sha1))
184 continue; /* invalid line: does not start with SHA1 */
185 if (starts_with(sb.buf + GIT_SHA1_HEXSZ, "\tnot-for-merge\t"))
186 continue; /* ref is not-for-merge */
187 sha1_array_append(merge_heads, sha1);
194 * Used by die_no_merge_candidates() as a for_each_remote() callback to
195 * retrieve the name of the remote if the repository only has one remote.
197 static int get_only_remote(struct remote *remote, void *cb_data)
199 const char **remote_name = cb_data;
204 *remote_name = remote->name;
209 * Dies with the appropriate reason for why there are no merge candidates:
211 * 1. We fetched from a specific remote, and a refspec was given, but it ended
212 * up not fetching anything. This is usually because the user provided a
213 * wildcard refspec which had no matches on the remote end.
215 * 2. We fetched from a non-default remote, but didn't specify a branch to
216 * merge. We can't use the configured one because it applies to the default
217 * remote, thus the user must specify the branches to merge.
219 * 3. We fetched from the branch's or repo's default remote, but:
221 * a. We are not on a branch, so there will never be a configured branch to
224 * b. We are on a branch, but there is no configured branch to merge with.
226 * 4. We fetched from the branch's or repo's default remote, but the configured
227 * branch to merge didn't get fetched. (Either it doesn't exist, or wasn't
228 * part of the configured fetch refspec.)
230 static void NORETURN die_no_merge_candidates(const char *repo, const char **refspecs)
232 struct branch *curr_branch = branch_get("HEAD");
233 const char *remote = curr_branch ? curr_branch->remote_name : NULL;
236 fprintf_ln(stderr, _("There are no candidates for merging among the refs that you just fetched."));
237 fprintf_ln(stderr, _("Generally this means that you provided a wildcard refspec which had no\n"
238 "matches on the remote end."));
239 } else if (repo && curr_branch && (!remote || strcmp(repo, remote))) {
240 fprintf_ln(stderr, _("You asked to pull from the remote '%s', but did not specify\n"
241 "a branch. Because this is not the default configured remote\n"
242 "for your current branch, you must specify a branch on the command line."),
244 } else if (!curr_branch) {
245 fprintf_ln(stderr, _("You are not currently on a branch."));
246 fprintf_ln(stderr, _("Please specify which branch you want to merge with."));
247 fprintf_ln(stderr, _("See git-pull(1) for details."));
248 fprintf(stderr, "\n");
249 fprintf_ln(stderr, " git pull <remote> <branch>");
250 fprintf(stderr, "\n");
251 } else if (!curr_branch->merge_nr) {
252 const char *remote_name = NULL;
254 if (for_each_remote(get_only_remote, &remote_name) || !remote_name)
255 remote_name = "<remote>";
257 fprintf_ln(stderr, _("There is no tracking information for the current branch."));
258 fprintf_ln(stderr, _("Please specify which branch you want to merge with."));
259 fprintf_ln(stderr, _("See git-pull(1) for details."));
260 fprintf(stderr, "\n");
261 fprintf_ln(stderr, " git pull <remote> <branch>");
262 fprintf(stderr, "\n");
263 fprintf_ln(stderr, _("If you wish to set tracking information for this branch you can do so with:\n"
265 " git branch --set-upstream-to=%s/<branch> %s\n"),
266 remote_name, curr_branch->name);
268 fprintf_ln(stderr, _("Your configuration specifies to merge with the ref '%s'\n"
269 "from the remote, but no such ref was fetched."),
270 *curr_branch->merge_name);
275 * Parses argv into [<repo> [<refspecs>...]], returning their values in `repo`
276 * as a string and `refspecs` as a null-terminated array of strings. If `repo`
277 * is not provided in argv, it is set to NULL.
279 static void parse_repo_refspecs(int argc, const char **argv, const char **repo,
280 const char ***refspecs)
291 * Runs git-fetch, returning its exit status. `repo` and `refspecs` are the
292 * repository and refspecs to fetch, or NULL if they are not provided.
294 static int run_fetch(const char *repo, const char **refspecs)
296 struct argv_array args = ARGV_ARRAY_INIT;
299 argv_array_pushl(&args, "fetch", "--update-head-ok", NULL);
302 argv_push_verbosity(&args);
304 argv_array_push(&args, opt_progress);
306 /* Options passed to git-fetch */
308 argv_array_push(&args, opt_all);
310 argv_array_push(&args, opt_append);
312 argv_array_push(&args, opt_upload_pack);
313 argv_push_force(&args);
315 argv_array_push(&args, opt_tags);
317 argv_array_push(&args, opt_prune);
318 if (opt_recurse_submodules)
319 argv_array_push(&args, opt_recurse_submodules);
321 argv_array_push(&args, "--dry-run");
323 argv_array_push(&args, opt_keep);
325 argv_array_push(&args, opt_depth);
327 argv_array_push(&args, opt_unshallow);
328 if (opt_update_shallow)
329 argv_array_push(&args, opt_update_shallow);
331 argv_array_push(&args, opt_refmap);
334 argv_array_push(&args, repo);
335 argv_array_pushv(&args, refspecs);
336 } else if (*refspecs)
337 die("BUG: refspecs without repo?");
338 ret = run_command_v_opt(args.argv, RUN_GIT_CMD);
339 argv_array_clear(&args);
344 * Runs git-merge, returning its exit status.
346 static int run_merge(void)
349 struct argv_array args = ARGV_ARRAY_INIT;
351 argv_array_pushl(&args, "merge", NULL);
354 argv_push_verbosity(&args);
356 argv_array_push(&args, opt_progress);
358 /* Options passed to git-merge */
360 argv_array_push(&args, opt_diffstat);
362 argv_array_push(&args, opt_log);
364 argv_array_push(&args, opt_squash);
366 argv_array_push(&args, opt_commit);
368 argv_array_push(&args, opt_edit);
370 argv_array_push(&args, opt_ff);
371 if (opt_verify_signatures)
372 argv_array_push(&args, opt_verify_signatures);
373 argv_array_pushv(&args, opt_strategies.argv);
374 argv_array_pushv(&args, opt_strategy_opts.argv);
376 argv_array_push(&args, opt_gpg_sign);
378 argv_array_push(&args, "FETCH_HEAD");
379 ret = run_command_v_opt(args.argv, RUN_GIT_CMD);
380 argv_array_clear(&args);
384 int cmd_pull(int argc, const char **argv, const char *prefix)
386 const char *repo, **refspecs;
387 struct sha1_array merge_heads = SHA1_ARRAY_INIT;
389 if (!getenv("_GIT_USE_BUILTIN_PULL")) {
390 const char *path = mkpath("%s/git-pull", git_exec_path());
392 if (sane_execvp(path, (char **)argv) < 0)
393 die_errno("could not exec %s", path);
396 argc = parse_options(argc, argv, prefix, pull_options, pull_usage, 0);
398 parse_repo_refspecs(argc, argv, &repo, &refspecs);
400 if (run_fetch(repo, refspecs))
406 get_merge_heads(&merge_heads);
409 die_no_merge_candidates(repo, refspecs);