pull: set reflog message
[git/mingw/j6t.git] / builtin / pull.c
blob98caffee3f2dc5d1fe64c16527d0dd81866af709
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"
16 #include "refs.h"
18 static const char * const pull_usage[] = {
19 N_("git pull [options] [<repository> [<refspec>...]]"),
20 NULL
23 /* Shared options */
24 static int opt_verbosity;
25 static char *opt_progress;
27 /* Options passed to git-merge */
28 static char *opt_diffstat;
29 static char *opt_log;
30 static char *opt_squash;
31 static char *opt_commit;
32 static char *opt_edit;
33 static char *opt_ff;
34 static char *opt_verify_signatures;
35 static struct argv_array opt_strategies = ARGV_ARRAY_INIT;
36 static struct argv_array opt_strategy_opts = ARGV_ARRAY_INIT;
37 static char *opt_gpg_sign;
39 /* Options passed to git-fetch */
40 static char *opt_all;
41 static char *opt_append;
42 static char *opt_upload_pack;
43 static int opt_force;
44 static char *opt_tags;
45 static char *opt_prune;
46 static char *opt_recurse_submodules;
47 static int opt_dry_run;
48 static char *opt_keep;
49 static char *opt_depth;
50 static char *opt_unshallow;
51 static char *opt_update_shallow;
52 static char *opt_refmap;
54 static struct option pull_options[] = {
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),
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),
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"),
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"),
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),
144 OPT_END()
148 * Pushes "-q" or "-v" switches into arr to match the opt_verbosity level.
150 static void argv_push_verbosity(struct argv_array *arr)
152 int verbosity;
154 for (verbosity = opt_verbosity; verbosity > 0; verbosity--)
155 argv_array_push(arr, "-v");
157 for (verbosity = opt_verbosity; verbosity < 0; verbosity++)
158 argv_array_push(arr, "-q");
162 * Pushes "-f" switches into arr to match the opt_force level.
164 static void argv_push_force(struct argv_array *arr)
166 int force = opt_force;
167 while (force-- > 0)
168 argv_array_push(arr, "-f");
172 * Sets the GIT_REFLOG_ACTION environment variable to the concatenation of argv
174 static void set_reflog_message(int argc, const char **argv)
176 int i;
177 struct strbuf msg = STRBUF_INIT;
179 for (i = 0; i < argc; i++) {
180 if (i)
181 strbuf_addch(&msg, ' ');
182 strbuf_addstr(&msg, argv[i]);
185 setenv("GIT_REFLOG_ACTION", msg.buf, 0);
187 strbuf_release(&msg);
191 * If pull.ff is unset, returns NULL. If pull.ff is "true", returns "--ff". If
192 * pull.ff is "false", returns "--no-ff". If pull.ff is "only", returns
193 * "--ff-only". Otherwise, if pull.ff is set to an invalid value, die with an
194 * error.
196 static const char *config_get_ff(void)
198 const char *value;
200 if (git_config_get_value("pull.ff", &value))
201 return NULL;
203 switch (git_config_maybe_bool("pull.ff", value)) {
204 case 0:
205 return "--no-ff";
206 case 1:
207 return "--ff";
210 if (!strcmp(value, "only"))
211 return "--ff-only";
213 die(_("Invalid value for pull.ff: %s"), value);
217 * Appends merge candidates from FETCH_HEAD that are not marked not-for-merge
218 * into merge_heads.
220 static void get_merge_heads(struct sha1_array *merge_heads)
222 const char *filename = git_path("FETCH_HEAD");
223 FILE *fp;
224 struct strbuf sb = STRBUF_INIT;
225 unsigned char sha1[GIT_SHA1_RAWSZ];
227 if (!(fp = fopen(filename, "r")))
228 die_errno(_("could not open '%s' for reading"), filename);
229 while (strbuf_getline(&sb, fp, '\n') != EOF) {
230 if (get_sha1_hex(sb.buf, sha1))
231 continue; /* invalid line: does not start with SHA1 */
232 if (starts_with(sb.buf + GIT_SHA1_HEXSZ, "\tnot-for-merge\t"))
233 continue; /* ref is not-for-merge */
234 sha1_array_append(merge_heads, sha1);
236 fclose(fp);
237 strbuf_release(&sb);
241 * Used by die_no_merge_candidates() as a for_each_remote() callback to
242 * retrieve the name of the remote if the repository only has one remote.
244 static int get_only_remote(struct remote *remote, void *cb_data)
246 const char **remote_name = cb_data;
248 if (*remote_name)
249 return -1;
251 *remote_name = remote->name;
252 return 0;
256 * Dies with the appropriate reason for why there are no merge candidates:
258 * 1. We fetched from a specific remote, and a refspec was given, but it ended
259 * up not fetching anything. This is usually because the user provided a
260 * wildcard refspec which had no matches on the remote end.
262 * 2. We fetched from a non-default remote, but didn't specify a branch to
263 * merge. We can't use the configured one because it applies to the default
264 * remote, thus the user must specify the branches to merge.
266 * 3. We fetched from the branch's or repo's default remote, but:
268 * a. We are not on a branch, so there will never be a configured branch to
269 * merge with.
271 * b. We are on a branch, but there is no configured branch to merge with.
273 * 4. We fetched from the branch's or repo's default remote, but the configured
274 * branch to merge didn't get fetched. (Either it doesn't exist, or wasn't
275 * part of the configured fetch refspec.)
277 static void NORETURN die_no_merge_candidates(const char *repo, const char **refspecs)
279 struct branch *curr_branch = branch_get("HEAD");
280 const char *remote = curr_branch ? curr_branch->remote_name : NULL;
282 if (*refspecs) {
283 fprintf_ln(stderr, _("There are no candidates for merging among the refs that you just fetched."));
284 fprintf_ln(stderr, _("Generally this means that you provided a wildcard refspec which had no\n"
285 "matches on the remote end."));
286 } else if (repo && curr_branch && (!remote || strcmp(repo, remote))) {
287 fprintf_ln(stderr, _("You asked to pull from the remote '%s', but did not specify\n"
288 "a branch. Because this is not the default configured remote\n"
289 "for your current branch, you must specify a branch on the command line."),
290 repo);
291 } else if (!curr_branch) {
292 fprintf_ln(stderr, _("You are not currently on a branch."));
293 fprintf_ln(stderr, _("Please specify which branch you want to merge with."));
294 fprintf_ln(stderr, _("See git-pull(1) for details."));
295 fprintf(stderr, "\n");
296 fprintf_ln(stderr, " git pull <remote> <branch>");
297 fprintf(stderr, "\n");
298 } else if (!curr_branch->merge_nr) {
299 const char *remote_name = NULL;
301 if (for_each_remote(get_only_remote, &remote_name) || !remote_name)
302 remote_name = "<remote>";
304 fprintf_ln(stderr, _("There is no tracking information for the current branch."));
305 fprintf_ln(stderr, _("Please specify which branch you want to merge with."));
306 fprintf_ln(stderr, _("See git-pull(1) for details."));
307 fprintf(stderr, "\n");
308 fprintf_ln(stderr, " git pull <remote> <branch>");
309 fprintf(stderr, "\n");
310 fprintf_ln(stderr, _("If you wish to set tracking information for this branch you can do so with:\n"
311 "\n"
312 " git branch --set-upstream-to=%s/<branch> %s\n"),
313 remote_name, curr_branch->name);
314 } else
315 fprintf_ln(stderr, _("Your configuration specifies to merge with the ref '%s'\n"
316 "from the remote, but no such ref was fetched."),
317 *curr_branch->merge_name);
318 exit(1);
322 * Parses argv into [<repo> [<refspecs>...]], returning their values in `repo`
323 * as a string and `refspecs` as a null-terminated array of strings. If `repo`
324 * is not provided in argv, it is set to NULL.
326 static void parse_repo_refspecs(int argc, const char **argv, const char **repo,
327 const char ***refspecs)
329 if (argc > 0) {
330 *repo = *argv++;
331 argc--;
332 } else
333 *repo = NULL;
334 *refspecs = argv;
338 * Runs git-fetch, returning its exit status. `repo` and `refspecs` are the
339 * repository and refspecs to fetch, or NULL if they are not provided.
341 static int run_fetch(const char *repo, const char **refspecs)
343 struct argv_array args = ARGV_ARRAY_INIT;
344 int ret;
346 argv_array_pushl(&args, "fetch", "--update-head-ok", NULL);
348 /* Shared options */
349 argv_push_verbosity(&args);
350 if (opt_progress)
351 argv_array_push(&args, opt_progress);
353 /* Options passed to git-fetch */
354 if (opt_all)
355 argv_array_push(&args, opt_all);
356 if (opt_append)
357 argv_array_push(&args, opt_append);
358 if (opt_upload_pack)
359 argv_array_push(&args, opt_upload_pack);
360 argv_push_force(&args);
361 if (opt_tags)
362 argv_array_push(&args, opt_tags);
363 if (opt_prune)
364 argv_array_push(&args, opt_prune);
365 if (opt_recurse_submodules)
366 argv_array_push(&args, opt_recurse_submodules);
367 if (opt_dry_run)
368 argv_array_push(&args, "--dry-run");
369 if (opt_keep)
370 argv_array_push(&args, opt_keep);
371 if (opt_depth)
372 argv_array_push(&args, opt_depth);
373 if (opt_unshallow)
374 argv_array_push(&args, opt_unshallow);
375 if (opt_update_shallow)
376 argv_array_push(&args, opt_update_shallow);
377 if (opt_refmap)
378 argv_array_push(&args, opt_refmap);
380 if (repo) {
381 argv_array_push(&args, repo);
382 argv_array_pushv(&args, refspecs);
383 } else if (*refspecs)
384 die("BUG: refspecs without repo?");
385 ret = run_command_v_opt(args.argv, RUN_GIT_CMD);
386 argv_array_clear(&args);
387 return ret;
391 * "Pulls into void" by branching off merge_head.
393 static int pull_into_void(const unsigned char *merge_head,
394 const unsigned char *curr_head)
397 * Two-way merge: we treat the index as based on an empty tree,
398 * and try to fast-forward to HEAD. This ensures we will not lose
399 * index/worktree changes that the user already made on the unborn
400 * branch.
402 if (checkout_fast_forward(EMPTY_TREE_SHA1_BIN, merge_head, 0))
403 return 1;
405 if (update_ref("initial pull", "HEAD", merge_head, curr_head, 0, UPDATE_REFS_DIE_ON_ERR))
406 return 1;
408 return 0;
412 * Runs git-merge, returning its exit status.
414 static int run_merge(void)
416 int ret;
417 struct argv_array args = ARGV_ARRAY_INIT;
419 argv_array_pushl(&args, "merge", NULL);
421 /* Shared options */
422 argv_push_verbosity(&args);
423 if (opt_progress)
424 argv_array_push(&args, opt_progress);
426 /* Options passed to git-merge */
427 if (opt_diffstat)
428 argv_array_push(&args, opt_diffstat);
429 if (opt_log)
430 argv_array_push(&args, opt_log);
431 if (opt_squash)
432 argv_array_push(&args, opt_squash);
433 if (opt_commit)
434 argv_array_push(&args, opt_commit);
435 if (opt_edit)
436 argv_array_push(&args, opt_edit);
437 if (opt_ff)
438 argv_array_push(&args, opt_ff);
439 if (opt_verify_signatures)
440 argv_array_push(&args, opt_verify_signatures);
441 argv_array_pushv(&args, opt_strategies.argv);
442 argv_array_pushv(&args, opt_strategy_opts.argv);
443 if (opt_gpg_sign)
444 argv_array_push(&args, opt_gpg_sign);
446 argv_array_push(&args, "FETCH_HEAD");
447 ret = run_command_v_opt(args.argv, RUN_GIT_CMD);
448 argv_array_clear(&args);
449 return ret;
452 int cmd_pull(int argc, const char **argv, const char *prefix)
454 const char *repo, **refspecs;
455 struct sha1_array merge_heads = SHA1_ARRAY_INIT;
456 unsigned char orig_head[GIT_SHA1_RAWSZ], curr_head[GIT_SHA1_RAWSZ];
458 if (!getenv("_GIT_USE_BUILTIN_PULL")) {
459 const char *path = mkpath("%s/git-pull", git_exec_path());
461 if (sane_execvp(path, (char **)argv) < 0)
462 die_errno("could not exec %s", path);
465 if (!getenv("GIT_REFLOG_ACTION"))
466 set_reflog_message(argc, argv);
468 argc = parse_options(argc, argv, prefix, pull_options, pull_usage, 0);
470 parse_repo_refspecs(argc, argv, &repo, &refspecs);
472 if (!opt_ff)
473 opt_ff = xstrdup_or_null(config_get_ff());
475 git_config(git_default_config, NULL);
477 if (read_cache_unmerged())
478 die_resolve_conflict("Pull");
480 if (file_exists(git_path("MERGE_HEAD")))
481 die_conclude_merge();
483 if (get_sha1("HEAD", orig_head))
484 hashclr(orig_head);
486 if (run_fetch(repo, refspecs))
487 return 1;
489 if (opt_dry_run)
490 return 0;
492 if (get_sha1("HEAD", curr_head))
493 hashclr(curr_head);
495 if (!is_null_sha1(orig_head) && !is_null_sha1(curr_head) &&
496 hashcmp(orig_head, curr_head)) {
498 * The fetch involved updating the current branch.
500 * The working tree and the index file are still based on
501 * orig_head commit, but we are merging into curr_head.
502 * Update the working tree to match curr_head.
505 warning(_("fetch updated the current branch head.\n"
506 "fast-forwarding your working tree from\n"
507 "commit %s."), sha1_to_hex(orig_head));
509 if (checkout_fast_forward(orig_head, curr_head, 0))
510 die(_("Cannot fast-forward your working tree.\n"
511 "After making sure that you saved anything precious from\n"
512 "$ git diff %s\n"
513 "output, run\n"
514 "$ git reset --hard\n"
515 "to recover."), sha1_to_hex(orig_head));
518 get_merge_heads(&merge_heads);
520 if (!merge_heads.nr)
521 die_no_merge_candidates(repo, refspecs);
523 if (is_null_sha1(orig_head)) {
524 if (merge_heads.nr > 1)
525 die(_("Cannot merge multiple branches into empty head."));
526 return pull_into_void(*merge_heads.sha1, curr_head);
527 } else
528 return run_merge();