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" | |
4a4cf9e8 | 15 | #include "dir.h" |
49ec402d | 16 | #include "refs.h" |
8944969c | 17 | #include "revision.h" |
db86e61c | 18 | #include "tempfile.h" |
8944969c | 19 | #include "lockfile.h" |
fd84986f | 20 | #include "wt-status.h" |
1e1ea69f | 21 | |
1678b81e PT |
22 | enum rebase_type { |
23 | REBASE_INVALID = -1, | |
24 | REBASE_FALSE = 0, | |
25 | REBASE_TRUE, | |
f5eb87b9 JS |
26 | REBASE_PRESERVE, |
27 | REBASE_INTERACTIVE | |
1678b81e PT |
28 | }; |
29 | ||
30 | /** | |
31 | * Parses the value of --rebase. If value is a false value, returns | |
32 | * REBASE_FALSE. If value is a true value, returns REBASE_TRUE. If value is | |
33 | * "preserve", returns REBASE_PRESERVE. If value is a invalid value, dies with | |
34 | * a fatal error if fatal is true, otherwise returns REBASE_INVALID. | |
35 | */ | |
36 | static enum rebase_type parse_config_rebase(const char *key, const char *value, | |
37 | int fatal) | |
38 | { | |
39 | int v = git_config_maybe_bool("pull.rebase", value); | |
40 | ||
41 | if (!v) | |
42 | return REBASE_FALSE; | |
43 | else if (v > 0) | |
44 | return REBASE_TRUE; | |
45 | else if (!strcmp(value, "preserve")) | |
46 | return REBASE_PRESERVE; | |
f5eb87b9 JS |
47 | else if (!strcmp(value, "interactive")) |
48 | return REBASE_INTERACTIVE; | |
1678b81e PT |
49 | |
50 | if (fatal) | |
51 | die(_("Invalid value for %s: %s"), key, value); | |
52 | else | |
53 | error(_("Invalid value for %s: %s"), key, value); | |
54 | ||
55 | return REBASE_INVALID; | |
56 | } | |
57 | ||
58 | /** | |
59 | * Callback for --rebase, which parses arg with parse_config_rebase(). | |
60 | */ | |
61 | static int parse_opt_rebase(const struct option *opt, const char *arg, int unset) | |
62 | { | |
63 | enum rebase_type *value = opt->value; | |
64 | ||
65 | if (arg) | |
66 | *value = parse_config_rebase("--rebase", arg, 0); | |
67 | else | |
68 | *value = unset ? REBASE_FALSE : REBASE_TRUE; | |
69 | return *value == REBASE_INVALID ? -1 : 0; | |
70 | } | |
71 | ||
1e1ea69f | 72 | static const char * const pull_usage[] = { |
e7a7401f | 73 | N_("git pull [<options>] [<repository> [<refspec>...]]"), |
1e1ea69f PT |
74 | NULL |
75 | }; | |
76 | ||
2a747902 PT |
77 | /* Shared options */ |
78 | static int opt_verbosity; | |
79 | static char *opt_progress; | |
80 | ||
1678b81e | 81 | /* Options passed to git-merge or git-rebase */ |
81dbd768 | 82 | static enum rebase_type opt_rebase = -1; |
11b6d178 PT |
83 | static char *opt_diffstat; |
84 | static char *opt_log; | |
85 | static char *opt_squash; | |
86 | static char *opt_commit; | |
87 | static char *opt_edit; | |
88 | static char *opt_ff; | |
89 | static char *opt_verify_signatures; | |
f66398eb | 90 | static int opt_autostash = -1; |
c48d73bd | 91 | static int config_autostash; |
11b6d178 PT |
92 | static struct argv_array opt_strategies = ARGV_ARRAY_INIT; |
93 | static struct argv_array opt_strategy_opts = ARGV_ARRAY_INIT; | |
94 | static char *opt_gpg_sign; | |
09c2cb87 | 95 | static int opt_allow_unrelated_histories; |
11b6d178 | 96 | |
a32975f5 PT |
97 | /* Options passed to git-fetch */ |
98 | static char *opt_all; | |
99 | static char *opt_append; | |
100 | static char *opt_upload_pack; | |
101 | static int opt_force; | |
102 | static char *opt_tags; | |
103 | static char *opt_prune; | |
104 | static char *opt_recurse_submodules; | |
62104ba1 | 105 | static char *max_children; |
a32975f5 PT |
106 | static int opt_dry_run; |
107 | static char *opt_keep; | |
108 | static char *opt_depth; | |
109 | static char *opt_unshallow; | |
110 | static char *opt_update_shallow; | |
111 | static char *opt_refmap; | |
ffb4568a WS |
112 | static char *opt_ipv4; |
113 | static char *opt_ipv6; | |
a32975f5 | 114 | |
1e1ea69f | 115 | static struct option pull_options[] = { |
2a747902 PT |
116 | /* Shared options */ |
117 | OPT__VERBOSITY(&opt_verbosity), | |
118 | OPT_PASSTHRU(0, "progress", &opt_progress, NULL, | |
119 | N_("force progress reporting"), | |
120 | PARSE_OPT_NOARG), | |
121 | ||
1678b81e | 122 | /* Options passed to git-merge or git-rebase */ |
11b6d178 | 123 | OPT_GROUP(N_("Options related to merging")), |
1678b81e | 124 | { OPTION_CALLBACK, 'r', "rebase", &opt_rebase, |
f5eb87b9 | 125 | "false|true|preserve|interactive", |
1678b81e PT |
126 | N_("incorporate changes by rebasing rather than merging"), |
127 | PARSE_OPT_OPTARG, parse_opt_rebase }, | |
11b6d178 PT |
128 | OPT_PASSTHRU('n', NULL, &opt_diffstat, NULL, |
129 | N_("do not show a diffstat at the end of the merge"), | |
130 | PARSE_OPT_NOARG | PARSE_OPT_NONEG), | |
131 | OPT_PASSTHRU(0, "stat", &opt_diffstat, NULL, | |
132 | N_("show a diffstat at the end of the merge"), | |
133 | PARSE_OPT_NOARG), | |
134 | OPT_PASSTHRU(0, "summary", &opt_diffstat, NULL, | |
135 | N_("(synonym to --stat)"), | |
136 | PARSE_OPT_NOARG | PARSE_OPT_HIDDEN), | |
137 | OPT_PASSTHRU(0, "log", &opt_log, N_("n"), | |
138 | N_("add (at most <n>) entries from shortlog to merge commit message"), | |
139 | PARSE_OPT_OPTARG), | |
140 | OPT_PASSTHRU(0, "squash", &opt_squash, NULL, | |
141 | N_("create a single commit instead of doing a merge"), | |
142 | PARSE_OPT_NOARG), | |
143 | OPT_PASSTHRU(0, "commit", &opt_commit, NULL, | |
144 | N_("perform a commit if the merge succeeds (default)"), | |
145 | PARSE_OPT_NOARG), | |
146 | OPT_PASSTHRU(0, "edit", &opt_edit, NULL, | |
147 | N_("edit message before committing"), | |
148 | PARSE_OPT_NOARG), | |
149 | OPT_PASSTHRU(0, "ff", &opt_ff, NULL, | |
150 | N_("allow fast-forward"), | |
151 | PARSE_OPT_NOARG), | |
152 | OPT_PASSTHRU(0, "ff-only", &opt_ff, NULL, | |
153 | N_("abort if fast-forward is not possible"), | |
154 | PARSE_OPT_NOARG | PARSE_OPT_NONEG), | |
155 | OPT_PASSTHRU(0, "verify-signatures", &opt_verify_signatures, NULL, | |
156 | N_("verify that the named commit has a valid GPG signature"), | |
157 | PARSE_OPT_NOARG), | |
f66398eb MJ |
158 | OPT_BOOL(0, "autostash", &opt_autostash, |
159 | N_("automatically stash/stash pop before and after rebase")), | |
11b6d178 PT |
160 | OPT_PASSTHRU_ARGV('s', "strategy", &opt_strategies, N_("strategy"), |
161 | N_("merge strategy to use"), | |
162 | 0), | |
163 | OPT_PASSTHRU_ARGV('X', "strategy-option", &opt_strategy_opts, | |
164 | N_("option=value"), | |
165 | N_("option for selected merge strategy"), | |
166 | 0), | |
167 | OPT_PASSTHRU('S', "gpg-sign", &opt_gpg_sign, N_("key-id"), | |
168 | N_("GPG sign commit"), | |
169 | PARSE_OPT_OPTARG), | |
09c2cb87 JH |
170 | OPT_SET_INT(0, "allow-unrelated-histories", |
171 | &opt_allow_unrelated_histories, | |
172 | N_("allow merging unrelated histories"), 1), | |
11b6d178 | 173 | |
a32975f5 PT |
174 | /* Options passed to git-fetch */ |
175 | OPT_GROUP(N_("Options related to fetching")), | |
176 | OPT_PASSTHRU(0, "all", &opt_all, NULL, | |
177 | N_("fetch from all remotes"), | |
178 | PARSE_OPT_NOARG), | |
179 | OPT_PASSTHRU('a', "append", &opt_append, NULL, | |
180 | N_("append to .git/FETCH_HEAD instead of overwriting"), | |
181 | PARSE_OPT_NOARG), | |
182 | OPT_PASSTHRU(0, "upload-pack", &opt_upload_pack, N_("path"), | |
183 | N_("path to upload pack on remote end"), | |
184 | 0), | |
185 | OPT__FORCE(&opt_force, N_("force overwrite of local branch")), | |
186 | OPT_PASSTHRU('t', "tags", &opt_tags, NULL, | |
187 | N_("fetch all tags and associated objects"), | |
188 | PARSE_OPT_NOARG), | |
189 | OPT_PASSTHRU('p', "prune", &opt_prune, NULL, | |
190 | N_("prune remote-tracking branches no longer on remote"), | |
191 | PARSE_OPT_NOARG), | |
192 | OPT_PASSTHRU(0, "recurse-submodules", &opt_recurse_submodules, | |
193 | N_("on-demand"), | |
194 | N_("control recursive fetching of submodules"), | |
195 | PARSE_OPT_OPTARG), | |
62104ba1 SB |
196 | OPT_PASSTHRU('j', "jobs", &max_children, N_("n"), |
197 | N_("number of submodules pulled in parallel"), | |
198 | PARSE_OPT_OPTARG), | |
a32975f5 PT |
199 | OPT_BOOL(0, "dry-run", &opt_dry_run, |
200 | N_("dry run")), | |
201 | OPT_PASSTHRU('k', "keep", &opt_keep, NULL, | |
202 | N_("keep downloaded pack"), | |
203 | PARSE_OPT_NOARG), | |
204 | OPT_PASSTHRU(0, "depth", &opt_depth, N_("depth"), | |
205 | N_("deepen history of shallow clone"), | |
206 | 0), | |
207 | OPT_PASSTHRU(0, "unshallow", &opt_unshallow, NULL, | |
208 | N_("convert to a complete repository"), | |
209 | PARSE_OPT_NONEG | PARSE_OPT_NOARG), | |
210 | OPT_PASSTHRU(0, "update-shallow", &opt_update_shallow, NULL, | |
211 | N_("accept refs that update .git/shallow"), | |
212 | PARSE_OPT_NOARG), | |
213 | OPT_PASSTHRU(0, "refmap", &opt_refmap, N_("refmap"), | |
214 | N_("specify fetch refmap"), | |
215 | PARSE_OPT_NONEG), | |
ffb4568a WS |
216 | OPT_PASSTHRU('4', "ipv4", &opt_ipv4, NULL, |
217 | N_("use IPv4 addresses only"), | |
218 | PARSE_OPT_NOARG), | |
219 | OPT_PASSTHRU('6', "ipv6", &opt_ipv6, NULL, | |
220 | N_("use IPv6 addresses only"), | |
221 | PARSE_OPT_NOARG), | |
a32975f5 | 222 | |
1e1ea69f PT |
223 | OPT_END() |
224 | }; | |
225 | ||
2a747902 PT |
226 | /** |
227 | * Pushes "-q" or "-v" switches into arr to match the opt_verbosity level. | |
228 | */ | |
229 | static void argv_push_verbosity(struct argv_array *arr) | |
230 | { | |
231 | int verbosity; | |
232 | ||
233 | for (verbosity = opt_verbosity; verbosity > 0; verbosity--) | |
234 | argv_array_push(arr, "-v"); | |
235 | ||
236 | for (verbosity = opt_verbosity; verbosity < 0; verbosity++) | |
237 | argv_array_push(arr, "-q"); | |
238 | } | |
239 | ||
a32975f5 PT |
240 | /** |
241 | * Pushes "-f" switches into arr to match the opt_force level. | |
242 | */ | |
243 | static void argv_push_force(struct argv_array *arr) | |
244 | { | |
245 | int force = opt_force; | |
246 | while (force-- > 0) | |
247 | argv_array_push(arr, "-f"); | |
248 | } | |
249 | ||
41fca098 PT |
250 | /** |
251 | * Sets the GIT_REFLOG_ACTION environment variable to the concatenation of argv | |
252 | */ | |
253 | static void set_reflog_message(int argc, const char **argv) | |
254 | { | |
255 | int i; | |
256 | struct strbuf msg = STRBUF_INIT; | |
257 | ||
258 | for (i = 0; i < argc; i++) { | |
259 | if (i) | |
260 | strbuf_addch(&msg, ' '); | |
261 | strbuf_addstr(&msg, argv[i]); | |
262 | } | |
263 | ||
264 | setenv("GIT_REFLOG_ACTION", msg.buf, 0); | |
265 | ||
266 | strbuf_release(&msg); | |
267 | } | |
268 | ||
a9de9897 PT |
269 | /** |
270 | * If pull.ff is unset, returns NULL. If pull.ff is "true", returns "--ff". If | |
271 | * pull.ff is "false", returns "--no-ff". If pull.ff is "only", returns | |
272 | * "--ff-only". Otherwise, if pull.ff is set to an invalid value, die with an | |
273 | * error. | |
274 | */ | |
275 | static const char *config_get_ff(void) | |
276 | { | |
277 | const char *value; | |
278 | ||
279 | if (git_config_get_value("pull.ff", &value)) | |
280 | return NULL; | |
281 | ||
282 | switch (git_config_maybe_bool("pull.ff", value)) { | |
283 | case 0: | |
284 | return "--no-ff"; | |
285 | case 1: | |
286 | return "--ff"; | |
287 | } | |
288 | ||
289 | if (!strcmp(value, "only")) | |
290 | return "--ff-only"; | |
291 | ||
292 | die(_("Invalid value for pull.ff: %s"), value); | |
293 | } | |
294 | ||
81dbd768 PT |
295 | /** |
296 | * Returns the default configured value for --rebase. It first looks for the | |
297 | * value of "branch.$curr_branch.rebase", where $curr_branch is the current | |
298 | * branch, and if HEAD is detached or the configuration key does not exist, | |
299 | * looks for the value of "pull.rebase". If both configuration keys do not | |
300 | * exist, returns REBASE_FALSE. | |
301 | */ | |
302 | static enum rebase_type config_get_rebase(void) | |
303 | { | |
304 | struct branch *curr_branch = branch_get("HEAD"); | |
305 | const char *value; | |
306 | ||
307 | if (curr_branch) { | |
308 | char *key = xstrfmt("branch.%s.rebase", curr_branch->name); | |
309 | ||
310 | if (!git_config_get_value(key, &value)) { | |
311 | enum rebase_type ret = parse_config_rebase(key, value, 1); | |
312 | free(key); | |
313 | return ret; | |
314 | } | |
315 | ||
316 | free(key); | |
317 | } | |
318 | ||
319 | if (!git_config_get_value("pull.rebase", &value)) | |
320 | return parse_config_rebase("pull.rebase", value, 1); | |
321 | ||
322 | return REBASE_FALSE; | |
323 | } | |
324 | ||
c48d73bd MJ |
325 | /** |
326 | * Read config variables. | |
327 | */ | |
328 | static int git_pull_config(const char *var, const char *value, void *cb) | |
329 | { | |
330 | if (!strcmp(var, "rebase.autostash")) { | |
331 | config_autostash = git_config_bool(var, value); | |
332 | return 0; | |
333 | } | |
334 | return git_default_config(var, value, cb); | |
335 | } | |
336 | ||
44c175c7 PT |
337 | /** |
338 | * Appends merge candidates from FETCH_HEAD that are not marked not-for-merge | |
339 | * into merge_heads. | |
340 | */ | |
910650d2 | 341 | static void get_merge_heads(struct oid_array *merge_heads) |
44c175c7 | 342 | { |
ca03e067 | 343 | const char *filename = git_path_fetch_head(); |
44c175c7 PT |
344 | FILE *fp; |
345 | struct strbuf sb = STRBUF_INIT; | |
14bb40b3 | 346 | struct object_id oid; |
44c175c7 PT |
347 | |
348 | if (!(fp = fopen(filename, "r"))) | |
349 | die_errno(_("could not open '%s' for reading"), filename); | |
8f309aeb | 350 | while (strbuf_getline_lf(&sb, fp) != EOF) { |
14bb40b3 | 351 | if (get_oid_hex(sb.buf, &oid)) |
44c175c7 PT |
352 | continue; /* invalid line: does not start with SHA1 */ |
353 | if (starts_with(sb.buf + GIT_SHA1_HEXSZ, "\tnot-for-merge\t")) | |
354 | continue; /* ref is not-for-merge */ | |
910650d2 | 355 | oid_array_append(merge_heads, &oid); |
44c175c7 PT |
356 | } |
357 | fclose(fp); | |
358 | strbuf_release(&sb); | |
359 | } | |
360 | ||
361 | /** | |
362 | * Used by die_no_merge_candidates() as a for_each_remote() callback to | |
363 | * retrieve the name of the remote if the repository only has one remote. | |
364 | */ | |
365 | static int get_only_remote(struct remote *remote, void *cb_data) | |
366 | { | |
367 | const char **remote_name = cb_data; | |
368 | ||
369 | if (*remote_name) | |
370 | return -1; | |
371 | ||
372 | *remote_name = remote->name; | |
373 | return 0; | |
374 | } | |
375 | ||
376 | /** | |
377 | * Dies with the appropriate reason for why there are no merge candidates: | |
378 | * | |
379 | * 1. We fetched from a specific remote, and a refspec was given, but it ended | |
380 | * up not fetching anything. This is usually because the user provided a | |
381 | * wildcard refspec which had no matches on the remote end. | |
382 | * | |
383 | * 2. We fetched from a non-default remote, but didn't specify a branch to | |
384 | * merge. We can't use the configured one because it applies to the default | |
385 | * remote, thus the user must specify the branches to merge. | |
386 | * | |
387 | * 3. We fetched from the branch's or repo's default remote, but: | |
388 | * | |
389 | * a. We are not on a branch, so there will never be a configured branch to | |
390 | * merge with. | |
391 | * | |
392 | * b. We are on a branch, but there is no configured branch to merge with. | |
393 | * | |
394 | * 4. We fetched from the branch's or repo's default remote, but the configured | |
395 | * branch to merge didn't get fetched. (Either it doesn't exist, or wasn't | |
396 | * part of the configured fetch refspec.) | |
397 | */ | |
398 | static void NORETURN die_no_merge_candidates(const char *repo, const char **refspecs) | |
399 | { | |
400 | struct branch *curr_branch = branch_get("HEAD"); | |
401 | const char *remote = curr_branch ? curr_branch->remote_name : NULL; | |
402 | ||
403 | if (*refspecs) { | |
b7b31471 PT |
404 | if (opt_rebase) |
405 | fprintf_ln(stderr, _("There is no candidate for rebasing against among the refs that you just fetched.")); | |
406 | else | |
407 | fprintf_ln(stderr, _("There are no candidates for merging among the refs that you just fetched.")); | |
44c175c7 PT |
408 | fprintf_ln(stderr, _("Generally this means that you provided a wildcard refspec which had no\n" |
409 | "matches on the remote end.")); | |
410 | } else if (repo && curr_branch && (!remote || strcmp(repo, remote))) { | |
411 | fprintf_ln(stderr, _("You asked to pull from the remote '%s', but did not specify\n" | |
412 | "a branch. Because this is not the default configured remote\n" | |
413 | "for your current branch, you must specify a branch on the command line."), | |
414 | repo); | |
415 | } else if (!curr_branch) { | |
416 | fprintf_ln(stderr, _("You are not currently on a branch.")); | |
b7b31471 PT |
417 | if (opt_rebase) |
418 | fprintf_ln(stderr, _("Please specify which branch you want to rebase against.")); | |
419 | else | |
420 | fprintf_ln(stderr, _("Please specify which branch you want to merge with.")); | |
44c175c7 PT |
421 | fprintf_ln(stderr, _("See git-pull(1) for details.")); |
422 | fprintf(stderr, "\n"); | |
8a0de58a | 423 | fprintf_ln(stderr, " git pull %s %s", _("<remote>"), _("<branch>")); |
44c175c7 PT |
424 | fprintf(stderr, "\n"); |
425 | } else if (!curr_branch->merge_nr) { | |
426 | const char *remote_name = NULL; | |
427 | ||
428 | if (for_each_remote(get_only_remote, &remote_name) || !remote_name) | |
8a0de58a | 429 | remote_name = _("<remote>"); |
44c175c7 PT |
430 | |
431 | fprintf_ln(stderr, _("There is no tracking information for the current branch.")); | |
b7b31471 PT |
432 | if (opt_rebase) |
433 | fprintf_ln(stderr, _("Please specify which branch you want to rebase against.")); | |
434 | else | |
435 | fprintf_ln(stderr, _("Please specify which branch you want to merge with.")); | |
44c175c7 PT |
436 | fprintf_ln(stderr, _("See git-pull(1) for details.")); |
437 | fprintf(stderr, "\n"); | |
8a0de58a | 438 | fprintf_ln(stderr, " git pull %s %s", _("<remote>"), _("<branch>")); |
44c175c7 | 439 | fprintf(stderr, "\n"); |
daf9f649 VA |
440 | fprintf_ln(stderr, _("If you wish to set tracking information for this branch you can do so with:")); |
441 | fprintf(stderr, "\n"); | |
442 | fprintf_ln(stderr, " git branch --set-upstream-to=%s/%s %s\n", | |
443 | remote_name, _("<branch>"), curr_branch->name); | |
44c175c7 PT |
444 | } else |
445 | fprintf_ln(stderr, _("Your configuration specifies to merge with the ref '%s'\n" | |
446 | "from the remote, but no such ref was fetched."), | |
447 | *curr_branch->merge_name); | |
448 | exit(1); | |
449 | } | |
450 | ||
f2c5baa1 PT |
451 | /** |
452 | * Parses argv into [<repo> [<refspecs>...]], returning their values in `repo` | |
453 | * as a string and `refspecs` as a null-terminated array of strings. If `repo` | |
454 | * is not provided in argv, it is set to NULL. | |
455 | */ | |
456 | static void parse_repo_refspecs(int argc, const char **argv, const char **repo, | |
457 | const char ***refspecs) | |
458 | { | |
459 | if (argc > 0) { | |
460 | *repo = *argv++; | |
461 | argc--; | |
462 | } else | |
463 | *repo = NULL; | |
464 | *refspecs = argv; | |
465 | } | |
466 | ||
467 | /** | |
468 | * Runs git-fetch, returning its exit status. `repo` and `refspecs` are the | |
469 | * repository and refspecs to fetch, or NULL if they are not provided. | |
470 | */ | |
471 | static int run_fetch(const char *repo, const char **refspecs) | |
472 | { | |
473 | struct argv_array args = ARGV_ARRAY_INIT; | |
474 | int ret; | |
475 | ||
476 | argv_array_pushl(&args, "fetch", "--update-head-ok", NULL); | |
2a747902 PT |
477 | |
478 | /* Shared options */ | |
479 | argv_push_verbosity(&args); | |
480 | if (opt_progress) | |
481 | argv_array_push(&args, opt_progress); | |
482 | ||
a32975f5 PT |
483 | /* Options passed to git-fetch */ |
484 | if (opt_all) | |
485 | argv_array_push(&args, opt_all); | |
486 | if (opt_append) | |
487 | argv_array_push(&args, opt_append); | |
488 | if (opt_upload_pack) | |
489 | argv_array_push(&args, opt_upload_pack); | |
490 | argv_push_force(&args); | |
491 | if (opt_tags) | |
492 | argv_array_push(&args, opt_tags); | |
493 | if (opt_prune) | |
494 | argv_array_push(&args, opt_prune); | |
495 | if (opt_recurse_submodules) | |
496 | argv_array_push(&args, opt_recurse_submodules); | |
62104ba1 SB |
497 | if (max_children) |
498 | argv_array_push(&args, max_children); | |
a32975f5 PT |
499 | if (opt_dry_run) |
500 | argv_array_push(&args, "--dry-run"); | |
501 | if (opt_keep) | |
502 | argv_array_push(&args, opt_keep); | |
503 | if (opt_depth) | |
504 | argv_array_push(&args, opt_depth); | |
505 | if (opt_unshallow) | |
506 | argv_array_push(&args, opt_unshallow); | |
507 | if (opt_update_shallow) | |
508 | argv_array_push(&args, opt_update_shallow); | |
509 | if (opt_refmap) | |
510 | argv_array_push(&args, opt_refmap); | |
ffb4568a WS |
511 | if (opt_ipv4) |
512 | argv_array_push(&args, opt_ipv4); | |
513 | if (opt_ipv6) | |
514 | argv_array_push(&args, opt_ipv6); | |
a32975f5 | 515 | |
f2c5baa1 PT |
516 | if (repo) { |
517 | argv_array_push(&args, repo); | |
518 | argv_array_pushv(&args, refspecs); | |
519 | } else if (*refspecs) | |
520 | die("BUG: refspecs without repo?"); | |
521 | ret = run_command_v_opt(args.argv, RUN_GIT_CMD); | |
522 | argv_array_clear(&args); | |
523 | return ret; | |
524 | } | |
525 | ||
49ec402d PT |
526 | /** |
527 | * "Pulls into void" by branching off merge_head. | |
528 | */ | |
ee3051bd | 529 | static int pull_into_void(const struct object_id *merge_head, |
f9b11147 | 530 | const struct object_id *curr_head) |
49ec402d PT |
531 | { |
532 | /* | |
533 | * Two-way merge: we treat the index as based on an empty tree, | |
534 | * and try to fast-forward to HEAD. This ensures we will not lose | |
535 | * index/worktree changes that the user already made on the unborn | |
536 | * branch. | |
537 | */ | |
ee3051bd | 538 | if (checkout_fast_forward(EMPTY_TREE_SHA1_BIN, merge_head->hash, 0)) |
49ec402d PT |
539 | return 1; |
540 | ||
ee3051bd | 541 | if (update_ref("initial pull", "HEAD", merge_head->hash, curr_head->hash, 0, UPDATE_REFS_DIE_ON_ERR)) |
49ec402d PT |
542 | return 1; |
543 | ||
544 | return 0; | |
545 | } | |
546 | ||
f2c5baa1 PT |
547 | /** |
548 | * Runs git-merge, returning its exit status. | |
549 | */ | |
550 | static int run_merge(void) | |
551 | { | |
552 | int ret; | |
553 | struct argv_array args = ARGV_ARRAY_INIT; | |
554 | ||
555 | argv_array_pushl(&args, "merge", NULL); | |
2a747902 PT |
556 | |
557 | /* Shared options */ | |
558 | argv_push_verbosity(&args); | |
559 | if (opt_progress) | |
560 | argv_array_push(&args, opt_progress); | |
561 | ||
11b6d178 PT |
562 | /* Options passed to git-merge */ |
563 | if (opt_diffstat) | |
564 | argv_array_push(&args, opt_diffstat); | |
565 | if (opt_log) | |
566 | argv_array_push(&args, opt_log); | |
567 | if (opt_squash) | |
568 | argv_array_push(&args, opt_squash); | |
569 | if (opt_commit) | |
570 | argv_array_push(&args, opt_commit); | |
571 | if (opt_edit) | |
572 | argv_array_push(&args, opt_edit); | |
573 | if (opt_ff) | |
574 | argv_array_push(&args, opt_ff); | |
575 | if (opt_verify_signatures) | |
576 | argv_array_push(&args, opt_verify_signatures); | |
577 | argv_array_pushv(&args, opt_strategies.argv); | |
578 | argv_array_pushv(&args, opt_strategy_opts.argv); | |
579 | if (opt_gpg_sign) | |
580 | argv_array_push(&args, opt_gpg_sign); | |
09c2cb87 JH |
581 | if (opt_allow_unrelated_histories > 0) |
582 | argv_array_push(&args, "--allow-unrelated-histories"); | |
11b6d178 | 583 | |
f2c5baa1 PT |
584 | argv_array_push(&args, "FETCH_HEAD"); |
585 | ret = run_command_v_opt(args.argv, RUN_GIT_CMD); | |
586 | argv_array_clear(&args); | |
587 | return ret; | |
588 | } | |
589 | ||
1678b81e PT |
590 | /** |
591 | * Returns remote's upstream branch for the current branch. If remote is NULL, | |
592 | * the current branch's configured default remote is used. Returns NULL if | |
593 | * `remote` does not name a valid remote, HEAD does not point to a branch, | |
594 | * remote is not the branch's configured remote or the branch does not have any | |
595 | * configured upstream branch. | |
596 | */ | |
597 | static const char *get_upstream_branch(const char *remote) | |
598 | { | |
599 | struct remote *rm; | |
600 | struct branch *curr_branch; | |
601 | const char *curr_branch_remote; | |
602 | ||
603 | rm = remote_get(remote); | |
604 | if (!rm) | |
605 | return NULL; | |
606 | ||
607 | curr_branch = branch_get("HEAD"); | |
608 | if (!curr_branch) | |
609 | return NULL; | |
610 | ||
611 | curr_branch_remote = remote_for_branch(curr_branch, NULL); | |
612 | assert(curr_branch_remote); | |
613 | ||
614 | if (strcmp(curr_branch_remote, rm->name)) | |
615 | return NULL; | |
616 | ||
617 | return branch_get_upstream(curr_branch, NULL); | |
618 | } | |
619 | ||
620 | /** | |
621 | * Derives the remote tracking branch from the remote and refspec. | |
622 | * | |
623 | * FIXME: The current implementation assumes the default mapping of | |
624 | * refs/heads/<branch_name> to refs/remotes/<remote_name>/<branch_name>. | |
625 | */ | |
626 | static const char *get_tracking_branch(const char *remote, const char *refspec) | |
627 | { | |
628 | struct refspec *spec; | |
629 | const char *spec_src; | |
630 | const char *merge_branch; | |
631 | ||
632 | spec = parse_fetch_refspec(1, &refspec); | |
633 | spec_src = spec->src; | |
634 | if (!*spec_src || !strcmp(spec_src, "HEAD")) | |
635 | spec_src = "HEAD"; | |
636 | else if (skip_prefix(spec_src, "heads/", &spec_src)) | |
637 | ; | |
638 | else if (skip_prefix(spec_src, "refs/heads/", &spec_src)) | |
639 | ; | |
640 | else if (starts_with(spec_src, "refs/") || | |
641 | starts_with(spec_src, "tags/") || | |
642 | starts_with(spec_src, "remotes/")) | |
643 | spec_src = ""; | |
644 | ||
645 | if (*spec_src) { | |
646 | if (!strcmp(remote, ".")) | |
647 | merge_branch = mkpath("refs/heads/%s", spec_src); | |
648 | else | |
649 | merge_branch = mkpath("refs/remotes/%s/%s", remote, spec_src); | |
650 | } else | |
651 | merge_branch = NULL; | |
652 | ||
653 | free_refspec(1, spec); | |
654 | return merge_branch; | |
655 | } | |
656 | ||
657 | /** | |
658 | * Given the repo and refspecs, sets fork_point to the point at which the | |
659 | * current branch forked from its remote tracking branch. Returns 0 on success, | |
660 | * -1 on failure. | |
661 | */ | |
f9b11147 | 662 | static int get_rebase_fork_point(struct object_id *fork_point, const char *repo, |
1678b81e PT |
663 | const char *refspec) |
664 | { | |
665 | int ret; | |
666 | struct branch *curr_branch; | |
667 | const char *remote_branch; | |
668 | struct child_process cp = CHILD_PROCESS_INIT; | |
669 | struct strbuf sb = STRBUF_INIT; | |
670 | ||
671 | curr_branch = branch_get("HEAD"); | |
672 | if (!curr_branch) | |
673 | return -1; | |
674 | ||
675 | if (refspec) | |
676 | remote_branch = get_tracking_branch(repo, refspec); | |
677 | else | |
678 | remote_branch = get_upstream_branch(repo); | |
679 | ||
680 | if (!remote_branch) | |
681 | return -1; | |
682 | ||
683 | argv_array_pushl(&cp.args, "merge-base", "--fork-point", | |
684 | remote_branch, curr_branch->name, NULL); | |
685 | cp.no_stdin = 1; | |
686 | cp.no_stderr = 1; | |
687 | cp.git_cmd = 1; | |
688 | ||
689 | ret = capture_command(&cp, &sb, GIT_SHA1_HEXSZ); | |
690 | if (ret) | |
691 | goto cleanup; | |
692 | ||
f9b11147 | 693 | ret = get_oid_hex(sb.buf, fork_point); |
1678b81e PT |
694 | if (ret) |
695 | goto cleanup; | |
696 | ||
697 | cleanup: | |
698 | strbuf_release(&sb); | |
699 | return ret ? -1 : 0; | |
700 | } | |
701 | ||
702 | /** | |
703 | * Sets merge_base to the octopus merge base of curr_head, merge_head and | |
704 | * fork_point. Returns 0 if a merge base is found, 1 otherwise. | |
705 | */ | |
f9b11147 | 706 | static int get_octopus_merge_base(struct object_id *merge_base, |
707 | const struct object_id *curr_head, | |
ee3051bd | 708 | const struct object_id *merge_head, |
f9b11147 | 709 | const struct object_id *fork_point) |
1678b81e PT |
710 | { |
711 | struct commit_list *revs = NULL, *result; | |
712 | ||
f9b11147 | 713 | commit_list_insert(lookup_commit_reference(curr_head->hash), &revs); |
ee3051bd | 714 | commit_list_insert(lookup_commit_reference(merge_head->hash), &revs); |
f9b11147 | 715 | if (!is_null_oid(fork_point)) |
716 | commit_list_insert(lookup_commit_reference(fork_point->hash), &revs); | |
1678b81e PT |
717 | |
718 | result = reduce_heads(get_octopus_merge_bases(revs)); | |
719 | free_commit_list(revs); | |
720 | if (!result) | |
721 | return 1; | |
722 | ||
f9b11147 | 723 | oidcpy(merge_base, &result->item->object.oid); |
1678b81e PT |
724 | return 0; |
725 | } | |
726 | ||
727 | /** | |
728 | * Given the current HEAD SHA1, the merge head returned from git-fetch and the | |
729 | * fork point calculated by get_rebase_fork_point(), runs git-rebase with the | |
730 | * appropriate arguments and returns its exit status. | |
731 | */ | |
f9b11147 | 732 | static int run_rebase(const struct object_id *curr_head, |
ee3051bd | 733 | const struct object_id *merge_head, |
f9b11147 | 734 | const struct object_id *fork_point) |
1678b81e PT |
735 | { |
736 | int ret; | |
f9b11147 | 737 | struct object_id oct_merge_base; |
1678b81e PT |
738 | struct argv_array args = ARGV_ARRAY_INIT; |
739 | ||
f9b11147 | 740 | if (!get_octopus_merge_base(&oct_merge_base, curr_head, merge_head, fork_point)) |
741 | if (!is_null_oid(fork_point) && !oidcmp(&oct_merge_base, fork_point)) | |
1678b81e PT |
742 | fork_point = NULL; |
743 | ||
744 | argv_array_push(&args, "rebase"); | |
745 | ||
746 | /* Shared options */ | |
747 | argv_push_verbosity(&args); | |
748 | ||
749 | /* Options passed to git-rebase */ | |
750 | if (opt_rebase == REBASE_PRESERVE) | |
751 | argv_array_push(&args, "--preserve-merges"); | |
f5eb87b9 JS |
752 | else if (opt_rebase == REBASE_INTERACTIVE) |
753 | argv_array_push(&args, "--interactive"); | |
1678b81e PT |
754 | if (opt_diffstat) |
755 | argv_array_push(&args, opt_diffstat); | |
756 | argv_array_pushv(&args, opt_strategies.argv); | |
757 | argv_array_pushv(&args, opt_strategy_opts.argv); | |
758 | if (opt_gpg_sign) | |
759 | argv_array_push(&args, opt_gpg_sign); | |
f66398eb MJ |
760 | if (opt_autostash == 0) |
761 | argv_array_push(&args, "--no-autostash"); | |
762 | else if (opt_autostash == 1) | |
763 | argv_array_push(&args, "--autostash"); | |
c57e501c AH |
764 | if (opt_verify_signatures && |
765 | !strcmp(opt_verify_signatures, "--verify-signatures")) | |
766 | warning(_("ignoring --verify-signatures for rebase")); | |
1678b81e PT |
767 | |
768 | argv_array_push(&args, "--onto"); | |
ee3051bd | 769 | argv_array_push(&args, oid_to_hex(merge_head)); |
1678b81e | 770 | |
f9b11147 | 771 | if (fork_point && !is_null_oid(fork_point)) |
772 | argv_array_push(&args, oid_to_hex(fork_point)); | |
1678b81e | 773 | else |
ee3051bd | 774 | argv_array_push(&args, oid_to_hex(merge_head)); |
1678b81e PT |
775 | |
776 | ret = run_command_v_opt(args.argv, RUN_GIT_CMD); | |
777 | argv_array_clear(&args); | |
778 | return ret; | |
779 | } | |
780 | ||
1e1ea69f PT |
781 | int cmd_pull(int argc, const char **argv, const char *prefix) |
782 | { | |
f2c5baa1 | 783 | const char *repo, **refspecs; |
910650d2 | 784 | struct oid_array merge_heads = OID_ARRAY_INIT; |
f9b11147 | 785 | struct object_id orig_head, curr_head; |
786 | struct object_id rebase_fork_point; | |
f15e7cf5 | 787 | int autostash; |
f2c5baa1 | 788 | |
41fca098 PT |
789 | if (!getenv("GIT_REFLOG_ACTION")) |
790 | set_reflog_message(argc, argv); | |
791 | ||
1e1ea69f PT |
792 | argc = parse_options(argc, argv, prefix, pull_options, pull_usage, 0); |
793 | ||
f2c5baa1 PT |
794 | parse_repo_refspecs(argc, argv, &repo, &refspecs); |
795 | ||
a9de9897 PT |
796 | if (!opt_ff) |
797 | opt_ff = xstrdup_or_null(config_get_ff()); | |
798 | ||
81dbd768 PT |
799 | if (opt_rebase < 0) |
800 | opt_rebase = config_get_rebase(); | |
801 | ||
c48d73bd | 802 | git_config(git_pull_config, NULL); |
4a4cf9e8 PT |
803 | |
804 | if (read_cache_unmerged()) | |
8785c425 | 805 | die_resolve_conflict("pull"); |
4a4cf9e8 | 806 | |
ca03e067 | 807 | if (file_exists(git_path_merge_head())) |
4a4cf9e8 PT |
808 | die_conclude_merge(); |
809 | ||
f9b11147 | 810 | if (get_oid("HEAD", &orig_head)) |
811 | oidclr(&orig_head); | |
fe911b8c | 812 | |
f66398eb MJ |
813 | if (!opt_rebase && opt_autostash != -1) |
814 | die(_("--[no-]autostash option is only valid with --rebase.")); | |
815 | ||
f15e7cf5 | 816 | autostash = config_autostash; |
8944969c | 817 | if (opt_rebase) { |
f66398eb MJ |
818 | if (opt_autostash != -1) |
819 | autostash = opt_autostash; | |
53c76dc0 | 820 | |
f9b11147 | 821 | if (is_null_oid(&orig_head) && !is_cache_unborn()) |
8944969c PT |
822 | die(_("Updating an unborn branch with changes added to the index.")); |
823 | ||
53c76dc0 | 824 | if (!autostash) |
ea63b393 | 825 | require_clean_work_tree(N_("pull with rebase"), |
d8cc92ab | 826 | _("please commit or stash them."), 1, 0); |
8944969c | 827 | |
f9b11147 | 828 | if (get_rebase_fork_point(&rebase_fork_point, repo, *refspecs)) |
829 | oidclr(&rebase_fork_point); | |
8944969c | 830 | } |
1678b81e | 831 | |
f2c5baa1 PT |
832 | if (run_fetch(repo, refspecs)) |
833 | return 1; | |
834 | ||
a32975f5 PT |
835 | if (opt_dry_run) |
836 | return 0; | |
837 | ||
f9b11147 | 838 | if (get_oid("HEAD", &curr_head)) |
839 | oidclr(&curr_head); | |
fe911b8c | 840 | |
f9b11147 | 841 | if (!is_null_oid(&orig_head) && !is_null_oid(&curr_head) && |
842 | oidcmp(&orig_head, &curr_head)) { | |
fe911b8c PT |
843 | /* |
844 | * The fetch involved updating the current branch. | |
845 | * | |
846 | * The working tree and the index file are still based on | |
847 | * orig_head commit, but we are merging into curr_head. | |
848 | * Update the working tree to match curr_head. | |
849 | */ | |
850 | ||
851 | warning(_("fetch updated the current branch head.\n" | |
852 | "fast-forwarding your working tree from\n" | |
f9b11147 | 853 | "commit %s."), oid_to_hex(&orig_head)); |
fe911b8c | 854 | |
f9b11147 | 855 | if (checkout_fast_forward(orig_head.hash, curr_head.hash, 0)) |
fe911b8c PT |
856 | die(_("Cannot fast-forward your working tree.\n" |
857 | "After making sure that you saved anything precious from\n" | |
858 | "$ git diff %s\n" | |
859 | "output, run\n" | |
860 | "$ git reset --hard\n" | |
f9b11147 | 861 | "to recover."), oid_to_hex(&orig_head)); |
fe911b8c PT |
862 | } |
863 | ||
44c175c7 PT |
864 | get_merge_heads(&merge_heads); |
865 | ||
866 | if (!merge_heads.nr) | |
867 | die_no_merge_candidates(repo, refspecs); | |
868 | ||
f9b11147 | 869 | if (is_null_oid(&orig_head)) { |
49ec402d PT |
870 | if (merge_heads.nr > 1) |
871 | die(_("Cannot merge multiple branches into empty head.")); | |
ee3051bd | 872 | return pull_into_void(merge_heads.oid, &curr_head); |
33b842a1 JH |
873 | } |
874 | if (opt_rebase && merge_heads.nr > 1) | |
875 | die(_("Cannot rebase onto multiple branches.")); | |
876 | ||
877 | if (opt_rebase) { | |
f15e7cf5 TB |
878 | if (!autostash) { |
879 | struct commit_list *list = NULL; | |
880 | struct commit *merge_head, *head; | |
881 | ||
882 | head = lookup_commit_reference(orig_head.hash); | |
883 | commit_list_insert(head, &list); | |
884 | merge_head = lookup_commit_reference(merge_heads.oid[0].hash); | |
885 | if (is_descendant_of(merge_head, list)) { | |
886 | /* we can fast-forward this without invoking rebase */ | |
887 | opt_ff = "--ff-only"; | |
888 | return run_merge(); | |
889 | } | |
33b842a1 | 890 | } |
ee3051bd | 891 | return run_rebase(&curr_head, merge_heads.oid, &rebase_fork_point); |
33b842a1 | 892 | } else { |
49ec402d | 893 | return run_merge(); |
33b842a1 | 894 | } |
1e1ea69f | 895 | } |