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