Line | Count | Source (jump to first uncovered line) |
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 | | |
9 | | #include "builtin.h" |
10 | | #include "advice.h" |
11 | | #include "config.h" |
12 | | #include "gettext.h" |
13 | | #include "hex.h" |
14 | | #include "merge.h" |
15 | | #include "object-name.h" |
16 | | #include "parse-options.h" |
17 | | #include "run-command.h" |
18 | | #include "oid-array.h" |
19 | | #include "remote.h" |
20 | | #include "dir.h" |
21 | | #include "path.h" |
22 | | #include "read-cache-ll.h" |
23 | | #include "rebase.h" |
24 | | #include "refs.h" |
25 | | #include "refspec.h" |
26 | | #include "submodule.h" |
27 | | #include "submodule-config.h" |
28 | | #include "wt-status.h" |
29 | | #include "commit-reach.h" |
30 | | #include "sequencer.h" |
31 | | |
32 | | /** |
33 | | * Parses the value of --rebase. If value is a false value, returns |
34 | | * REBASE_FALSE. If value is a true value, returns REBASE_TRUE. If value is |
35 | | * "merges", returns REBASE_MERGES. If value is a invalid value, dies with |
36 | | * a fatal error if fatal is true, otherwise returns REBASE_INVALID. |
37 | | */ |
38 | | static enum rebase_type parse_config_rebase(const char *key, const char *value, |
39 | | int fatal) |
40 | 0 | { |
41 | 0 | enum rebase_type v = rebase_parse_value(value); |
42 | 0 | if (v != REBASE_INVALID) |
43 | 0 | return v; |
44 | | |
45 | 0 | if (fatal) |
46 | 0 | die(_("invalid value for '%s': '%s'"), key, value); |
47 | 0 | else |
48 | 0 | error(_("invalid value for '%s': '%s'"), key, value); |
49 | | |
50 | 0 | return REBASE_INVALID; |
51 | 0 | } |
52 | | |
53 | | /** |
54 | | * Callback for --rebase, which parses arg with parse_config_rebase(). |
55 | | */ |
56 | | static int parse_opt_rebase(const struct option *opt, const char *arg, int unset) |
57 | 0 | { |
58 | 0 | enum rebase_type *value = opt->value; |
59 | |
|
60 | 0 | if (arg) |
61 | 0 | *value = parse_config_rebase("--rebase", arg, 0); |
62 | 0 | else |
63 | 0 | *value = unset ? REBASE_FALSE : REBASE_TRUE; |
64 | 0 | return *value == REBASE_INVALID ? -1 : 0; |
65 | 0 | } |
66 | | |
67 | | static const char * const pull_usage[] = { |
68 | | N_("git pull [<options>] [<repository> [<refspec>...]]"), |
69 | | NULL |
70 | | }; |
71 | | |
72 | | /* Shared options */ |
73 | | static int opt_verbosity; |
74 | | static const char *opt_progress; |
75 | | static int recurse_submodules = RECURSE_SUBMODULES_DEFAULT; |
76 | | static int recurse_submodules_cli = RECURSE_SUBMODULES_DEFAULT; |
77 | | |
78 | | /* Options passed to git-merge or git-rebase */ |
79 | | static enum rebase_type opt_rebase = -1; |
80 | | static const char *opt_diffstat; |
81 | | static const char *opt_log; |
82 | | static const char *opt_signoff; |
83 | | static const char *opt_squash; |
84 | | static const char *opt_commit; |
85 | | static const char *opt_edit; |
86 | | static const char *cleanup_arg; |
87 | | static const char *opt_ff; |
88 | | static const char *opt_verify_signatures; |
89 | | static const char *opt_verify; |
90 | | static int opt_autostash = -1; |
91 | | static int config_autostash; |
92 | | static int check_trust_level = 1; |
93 | | static struct strvec opt_strategies = STRVEC_INIT; |
94 | | static struct strvec opt_strategy_opts = STRVEC_INIT; |
95 | | static const char *opt_gpg_sign; |
96 | | static int opt_allow_unrelated_histories; |
97 | | |
98 | | /* Options passed to git-fetch */ |
99 | | static const char *opt_all; |
100 | | static const char *opt_append; |
101 | | static const char *opt_upload_pack; |
102 | | static int opt_force; |
103 | | static const char *opt_tags; |
104 | | static const char *opt_prune; |
105 | | static const char *max_children; |
106 | | static int opt_dry_run; |
107 | | static const char *opt_keep; |
108 | | static const char *opt_depth; |
109 | | static const char *opt_unshallow; |
110 | | static const char *opt_update_shallow; |
111 | | static const char *opt_refmap; |
112 | | static const char *opt_ipv4; |
113 | | static const char *opt_ipv6; |
114 | | static int opt_show_forced_updates = -1; |
115 | | static const char *set_upstream; |
116 | | static struct strvec opt_fetch = STRVEC_INIT; |
117 | | |
118 | | static struct option pull_options[] = { |
119 | | /* Shared options */ |
120 | | OPT__VERBOSITY(&opt_verbosity), |
121 | | OPT_PASSTHRU(0, "progress", &opt_progress, NULL, |
122 | | N_("force progress reporting"), |
123 | | PARSE_OPT_NOARG), |
124 | | OPT_CALLBACK_F(0, "recurse-submodules", |
125 | | &recurse_submodules_cli, N_("on-demand"), |
126 | | N_("control for recursive fetching of submodules"), |
127 | | PARSE_OPT_OPTARG, option_fetch_parse_recurse_submodules), |
128 | | |
129 | | /* Options passed to git-merge or git-rebase */ |
130 | | OPT_GROUP(N_("Options related to merging")), |
131 | | OPT_CALLBACK_F('r', "rebase", &opt_rebase, |
132 | | "(false|true|merges|interactive)", |
133 | | N_("incorporate changes by rebasing rather than merging"), |
134 | | PARSE_OPT_OPTARG, parse_opt_rebase), |
135 | | OPT_PASSTHRU('n', NULL, &opt_diffstat, NULL, |
136 | | N_("do not show a diffstat at the end of the merge"), |
137 | | PARSE_OPT_NOARG | PARSE_OPT_NONEG), |
138 | | OPT_PASSTHRU(0, "stat", &opt_diffstat, NULL, |
139 | | N_("show a diffstat at the end of the merge"), |
140 | | PARSE_OPT_NOARG), |
141 | | OPT_PASSTHRU(0, "summary", &opt_diffstat, NULL, |
142 | | N_("(synonym to --stat)"), |
143 | | PARSE_OPT_NOARG | PARSE_OPT_HIDDEN), |
144 | | OPT_PASSTHRU(0, "log", &opt_log, N_("n"), |
145 | | N_("add (at most <n>) entries from shortlog to merge commit message"), |
146 | | PARSE_OPT_OPTARG), |
147 | | OPT_PASSTHRU(0, "signoff", &opt_signoff, NULL, |
148 | | N_("add a Signed-off-by trailer"), |
149 | | PARSE_OPT_OPTARG), |
150 | | OPT_PASSTHRU(0, "squash", &opt_squash, NULL, |
151 | | N_("create a single commit instead of doing a merge"), |
152 | | PARSE_OPT_NOARG), |
153 | | OPT_PASSTHRU(0, "commit", &opt_commit, NULL, |
154 | | N_("perform a commit if the merge succeeds (default)"), |
155 | | PARSE_OPT_NOARG), |
156 | | OPT_PASSTHRU(0, "edit", &opt_edit, NULL, |
157 | | N_("edit message before committing"), |
158 | | PARSE_OPT_NOARG), |
159 | | OPT_CLEANUP(&cleanup_arg), |
160 | | OPT_PASSTHRU(0, "ff", &opt_ff, NULL, |
161 | | N_("allow fast-forward"), |
162 | | PARSE_OPT_NOARG), |
163 | | OPT_PASSTHRU(0, "ff-only", &opt_ff, NULL, |
164 | | N_("abort if fast-forward is not possible"), |
165 | | PARSE_OPT_NOARG | PARSE_OPT_NONEG), |
166 | | OPT_PASSTHRU(0, "verify", &opt_verify, NULL, |
167 | | N_("control use of pre-merge-commit and commit-msg hooks"), |
168 | | PARSE_OPT_NOARG), |
169 | | OPT_PASSTHRU(0, "verify-signatures", &opt_verify_signatures, NULL, |
170 | | N_("verify that the named commit has a valid GPG signature"), |
171 | | PARSE_OPT_NOARG), |
172 | | OPT_BOOL(0, "autostash", &opt_autostash, |
173 | | N_("automatically stash/stash pop before and after")), |
174 | | OPT_PASSTHRU_ARGV('s', "strategy", &opt_strategies, N_("strategy"), |
175 | | N_("merge strategy to use"), |
176 | | 0), |
177 | | OPT_PASSTHRU_ARGV('X', "strategy-option", &opt_strategy_opts, |
178 | | N_("option=value"), |
179 | | N_("option for selected merge strategy"), |
180 | | 0), |
181 | | OPT_PASSTHRU('S', "gpg-sign", &opt_gpg_sign, N_("key-id"), |
182 | | N_("GPG sign commit"), |
183 | | PARSE_OPT_OPTARG), |
184 | | OPT_SET_INT(0, "allow-unrelated-histories", |
185 | | &opt_allow_unrelated_histories, |
186 | | N_("allow merging unrelated histories"), 1), |
187 | | |
188 | | /* Options passed to git-fetch */ |
189 | | OPT_GROUP(N_("Options related to fetching")), |
190 | | OPT_PASSTHRU(0, "all", &opt_all, NULL, |
191 | | N_("fetch from all remotes"), |
192 | | PARSE_OPT_NOARG), |
193 | | OPT_PASSTHRU('a', "append", &opt_append, NULL, |
194 | | N_("append to .git/FETCH_HEAD instead of overwriting"), |
195 | | PARSE_OPT_NOARG), |
196 | | OPT_PASSTHRU(0, "upload-pack", &opt_upload_pack, N_("path"), |
197 | | N_("path to upload pack on remote end"), |
198 | | 0), |
199 | | OPT__FORCE(&opt_force, N_("force overwrite of local branch"), 0), |
200 | | OPT_PASSTHRU('t', "tags", &opt_tags, NULL, |
201 | | N_("fetch all tags and associated objects"), |
202 | | PARSE_OPT_NOARG), |
203 | | OPT_PASSTHRU('p', "prune", &opt_prune, NULL, |
204 | | N_("prune remote-tracking branches no longer on remote"), |
205 | | PARSE_OPT_NOARG), |
206 | | OPT_PASSTHRU('j', "jobs", &max_children, N_("n"), |
207 | | N_("number of submodules pulled in parallel"), |
208 | | PARSE_OPT_OPTARG), |
209 | | OPT_BOOL(0, "dry-run", &opt_dry_run, |
210 | | N_("dry run")), |
211 | | OPT_PASSTHRU('k', "keep", &opt_keep, NULL, |
212 | | N_("keep downloaded pack"), |
213 | | PARSE_OPT_NOARG), |
214 | | OPT_PASSTHRU(0, "depth", &opt_depth, N_("depth"), |
215 | | N_("deepen history of shallow clone"), |
216 | | 0), |
217 | | OPT_PASSTHRU_ARGV(0, "shallow-since", &opt_fetch, N_("time"), |
218 | | N_("deepen history of shallow repository based on time"), |
219 | | 0), |
220 | | OPT_PASSTHRU_ARGV(0, "shallow-exclude", &opt_fetch, N_("revision"), |
221 | | N_("deepen history of shallow clone, excluding rev"), |
222 | | 0), |
223 | | OPT_PASSTHRU_ARGV(0, "deepen", &opt_fetch, N_("n"), |
224 | | N_("deepen history of shallow clone"), |
225 | | 0), |
226 | | OPT_PASSTHRU(0, "unshallow", &opt_unshallow, NULL, |
227 | | N_("convert to a complete repository"), |
228 | | PARSE_OPT_NONEG | PARSE_OPT_NOARG), |
229 | | OPT_PASSTHRU(0, "update-shallow", &opt_update_shallow, NULL, |
230 | | N_("accept refs that update .git/shallow"), |
231 | | PARSE_OPT_NOARG), |
232 | | OPT_PASSTHRU(0, "refmap", &opt_refmap, N_("refmap"), |
233 | | N_("specify fetch refmap"), |
234 | | PARSE_OPT_NONEG), |
235 | | OPT_PASSTHRU_ARGV('o', "server-option", &opt_fetch, |
236 | | N_("server-specific"), |
237 | | N_("option to transmit"), |
238 | | 0), |
239 | | OPT_PASSTHRU('4', "ipv4", &opt_ipv4, NULL, |
240 | | N_("use IPv4 addresses only"), |
241 | | PARSE_OPT_NOARG), |
242 | | OPT_PASSTHRU('6', "ipv6", &opt_ipv6, NULL, |
243 | | N_("use IPv6 addresses only"), |
244 | | PARSE_OPT_NOARG), |
245 | | OPT_PASSTHRU_ARGV(0, "negotiation-tip", &opt_fetch, N_("revision"), |
246 | | N_("report that we have only objects reachable from this object"), |
247 | | 0), |
248 | | OPT_BOOL(0, "show-forced-updates", &opt_show_forced_updates, |
249 | | N_("check for forced-updates on all updated branches")), |
250 | | OPT_PASSTHRU(0, "set-upstream", &set_upstream, NULL, |
251 | | N_("set upstream for git pull/fetch"), |
252 | | PARSE_OPT_NOARG), |
253 | | |
254 | | OPT_END() |
255 | | }; |
256 | | |
257 | | /** |
258 | | * Pushes "-q" or "-v" switches into arr to match the opt_verbosity level. |
259 | | */ |
260 | | static void argv_push_verbosity(struct strvec *arr) |
261 | 0 | { |
262 | 0 | int verbosity; |
263 | |
|
264 | 0 | for (verbosity = opt_verbosity; verbosity > 0; verbosity--) |
265 | 0 | strvec_push(arr, "-v"); |
266 | |
|
267 | 0 | for (verbosity = opt_verbosity; verbosity < 0; verbosity++) |
268 | 0 | strvec_push(arr, "-q"); |
269 | 0 | } |
270 | | |
271 | | /** |
272 | | * Pushes "-f" switches into arr to match the opt_force level. |
273 | | */ |
274 | | static void argv_push_force(struct strvec *arr) |
275 | 0 | { |
276 | 0 | int force = opt_force; |
277 | 0 | while (force-- > 0) |
278 | 0 | strvec_push(arr, "-f"); |
279 | 0 | } |
280 | | |
281 | | /** |
282 | | * Sets the GIT_REFLOG_ACTION environment variable to the concatenation of argv |
283 | | */ |
284 | | static void set_reflog_message(int argc, const char **argv) |
285 | 0 | { |
286 | 0 | int i; |
287 | 0 | struct strbuf msg = STRBUF_INIT; |
288 | |
|
289 | 0 | for (i = 0; i < argc; i++) { |
290 | 0 | if (i) |
291 | 0 | strbuf_addch(&msg, ' '); |
292 | 0 | strbuf_addstr(&msg, argv[i]); |
293 | 0 | } |
294 | |
|
295 | 0 | setenv("GIT_REFLOG_ACTION", msg.buf, 0); |
296 | |
|
297 | 0 | strbuf_release(&msg); |
298 | 0 | } |
299 | | |
300 | | /** |
301 | | * If pull.ff is unset, returns NULL. If pull.ff is "true", returns "--ff". If |
302 | | * pull.ff is "false", returns "--no-ff". If pull.ff is "only", returns |
303 | | * "--ff-only". Otherwise, if pull.ff is set to an invalid value, die with an |
304 | | * error. |
305 | | */ |
306 | | static const char *config_get_ff(void) |
307 | 0 | { |
308 | 0 | const char *value; |
309 | |
|
310 | 0 | if (git_config_get_value("pull.ff", &value)) |
311 | 0 | return NULL; |
312 | | |
313 | 0 | switch (git_parse_maybe_bool(value)) { |
314 | 0 | case 0: |
315 | 0 | return "--no-ff"; |
316 | 0 | case 1: |
317 | 0 | return "--ff"; |
318 | 0 | } |
319 | | |
320 | 0 | if (!strcmp(value, "only")) |
321 | 0 | return "--ff-only"; |
322 | | |
323 | 0 | die(_("invalid value for '%s': '%s'"), "pull.ff", value); |
324 | 0 | } |
325 | | |
326 | | /** |
327 | | * Returns the default configured value for --rebase. It first looks for the |
328 | | * value of "branch.$curr_branch.rebase", where $curr_branch is the current |
329 | | * branch, and if HEAD is detached or the configuration key does not exist, |
330 | | * looks for the value of "pull.rebase". If both configuration keys do not |
331 | | * exist, returns REBASE_FALSE. |
332 | | */ |
333 | | static enum rebase_type config_get_rebase(int *rebase_unspecified) |
334 | 0 | { |
335 | 0 | struct branch *curr_branch = branch_get("HEAD"); |
336 | 0 | const char *value; |
337 | |
|
338 | 0 | if (curr_branch) { |
339 | 0 | char *key = xstrfmt("branch.%s.rebase", curr_branch->name); |
340 | |
|
341 | 0 | if (!git_config_get_value(key, &value)) { |
342 | 0 | enum rebase_type ret = parse_config_rebase(key, value, 1); |
343 | 0 | free(key); |
344 | 0 | return ret; |
345 | 0 | } |
346 | | |
347 | 0 | free(key); |
348 | 0 | } |
349 | | |
350 | 0 | if (!git_config_get_value("pull.rebase", &value)) |
351 | 0 | return parse_config_rebase("pull.rebase", value, 1); |
352 | | |
353 | 0 | *rebase_unspecified = 1; |
354 | |
|
355 | 0 | return REBASE_FALSE; |
356 | 0 | } |
357 | | |
358 | | /** |
359 | | * Read config variables. |
360 | | */ |
361 | | static int git_pull_config(const char *var, const char *value, |
362 | | const struct config_context *ctx, void *cb) |
363 | 0 | { |
364 | 0 | if (!strcmp(var, "rebase.autostash")) { |
365 | 0 | config_autostash = git_config_bool(var, value); |
366 | 0 | return 0; |
367 | 0 | } else if (!strcmp(var, "submodule.recurse")) { |
368 | 0 | recurse_submodules = git_config_bool(var, value) ? |
369 | 0 | RECURSE_SUBMODULES_ON : RECURSE_SUBMODULES_OFF; |
370 | 0 | return 0; |
371 | 0 | } else if (!strcmp(var, "gpg.mintrustlevel")) { |
372 | 0 | check_trust_level = 0; |
373 | 0 | } |
374 | | |
375 | 0 | return git_default_config(var, value, ctx, cb); |
376 | 0 | } |
377 | | |
378 | | /** |
379 | | * Appends merge candidates from FETCH_HEAD that are not marked not-for-merge |
380 | | * into merge_heads. |
381 | | */ |
382 | | static void get_merge_heads(struct oid_array *merge_heads) |
383 | 0 | { |
384 | 0 | const char *filename = git_path_fetch_head(the_repository); |
385 | 0 | FILE *fp; |
386 | 0 | struct strbuf sb = STRBUF_INIT; |
387 | 0 | struct object_id oid; |
388 | |
|
389 | 0 | fp = xfopen(filename, "r"); |
390 | 0 | while (strbuf_getline_lf(&sb, fp) != EOF) { |
391 | 0 | const char *p; |
392 | 0 | if (parse_oid_hex(sb.buf, &oid, &p)) |
393 | 0 | continue; /* invalid line: does not start with object ID */ |
394 | 0 | if (starts_with(p, "\tnot-for-merge\t")) |
395 | 0 | continue; /* ref is not-for-merge */ |
396 | 0 | oid_array_append(merge_heads, &oid); |
397 | 0 | } |
398 | 0 | fclose(fp); |
399 | 0 | strbuf_release(&sb); |
400 | 0 | } |
401 | | |
402 | | /** |
403 | | * Used by die_no_merge_candidates() as a for_each_remote() callback to |
404 | | * retrieve the name of the remote if the repository only has one remote. |
405 | | */ |
406 | | static int get_only_remote(struct remote *remote, void *cb_data) |
407 | 0 | { |
408 | 0 | const char **remote_name = cb_data; |
409 | |
|
410 | 0 | if (*remote_name) |
411 | 0 | return -1; |
412 | | |
413 | 0 | *remote_name = remote->name; |
414 | 0 | return 0; |
415 | 0 | } |
416 | | |
417 | | /** |
418 | | * Dies with the appropriate reason for why there are no merge candidates: |
419 | | * |
420 | | * 1. We fetched from a specific remote, and a refspec was given, but it ended |
421 | | * up not fetching anything. This is usually because the user provided a |
422 | | * wildcard refspec which had no matches on the remote end. |
423 | | * |
424 | | * 2. We fetched from a non-default remote, but didn't specify a branch to |
425 | | * merge. We can't use the configured one because it applies to the default |
426 | | * remote, thus the user must specify the branches to merge. |
427 | | * |
428 | | * 3. We fetched from the branch's or repo's default remote, but: |
429 | | * |
430 | | * a. We are not on a branch, so there will never be a configured branch to |
431 | | * merge with. |
432 | | * |
433 | | * b. We are on a branch, but there is no configured branch to merge with. |
434 | | * |
435 | | * 4. We fetched from the branch's or repo's default remote, but the configured |
436 | | * branch to merge didn't get fetched. (Either it doesn't exist, or wasn't |
437 | | * part of the configured fetch refspec.) |
438 | | */ |
439 | | static void NORETURN die_no_merge_candidates(const char *repo, const char **refspecs) |
440 | 0 | { |
441 | 0 | struct branch *curr_branch = branch_get("HEAD"); |
442 | 0 | const char *remote = curr_branch ? curr_branch->remote_name : NULL; |
443 | |
|
444 | 0 | if (*refspecs) { |
445 | 0 | if (opt_rebase) |
446 | 0 | fprintf_ln(stderr, _("There is no candidate for rebasing against among the refs that you just fetched.")); |
447 | 0 | else |
448 | 0 | fprintf_ln(stderr, _("There are no candidates for merging among the refs that you just fetched.")); |
449 | 0 | fprintf_ln(stderr, _("Generally this means that you provided a wildcard refspec which had no\n" |
450 | 0 | "matches on the remote end.")); |
451 | 0 | } else if (repo && curr_branch && (!remote || strcmp(repo, remote))) { |
452 | 0 | fprintf_ln(stderr, _("You asked to pull from the remote '%s', but did not specify\n" |
453 | 0 | "a branch. Because this is not the default configured remote\n" |
454 | 0 | "for your current branch, you must specify a branch on the command line."), |
455 | 0 | repo); |
456 | 0 | } else if (!curr_branch) { |
457 | 0 | fprintf_ln(stderr, _("You are not currently on a branch.")); |
458 | 0 | if (opt_rebase) |
459 | 0 | fprintf_ln(stderr, _("Please specify which branch you want to rebase against.")); |
460 | 0 | else |
461 | 0 | fprintf_ln(stderr, _("Please specify which branch you want to merge with.")); |
462 | 0 | fprintf_ln(stderr, _("See git-pull(1) for details.")); |
463 | 0 | fprintf(stderr, "\n"); |
464 | 0 | fprintf_ln(stderr, " git pull %s %s", _("<remote>"), _("<branch>")); |
465 | 0 | fprintf(stderr, "\n"); |
466 | 0 | } else if (!curr_branch->merge_nr) { |
467 | 0 | const char *remote_name = NULL; |
468 | |
|
469 | 0 | if (for_each_remote(get_only_remote, &remote_name) || !remote_name) |
470 | 0 | remote_name = _("<remote>"); |
471 | |
|
472 | 0 | fprintf_ln(stderr, _("There is no tracking information for the current branch.")); |
473 | 0 | if (opt_rebase) |
474 | 0 | fprintf_ln(stderr, _("Please specify which branch you want to rebase against.")); |
475 | 0 | else |
476 | 0 | fprintf_ln(stderr, _("Please specify which branch you want to merge with.")); |
477 | 0 | fprintf_ln(stderr, _("See git-pull(1) for details.")); |
478 | 0 | fprintf(stderr, "\n"); |
479 | 0 | fprintf_ln(stderr, " git pull %s %s", _("<remote>"), _("<branch>")); |
480 | 0 | fprintf(stderr, "\n"); |
481 | 0 | fprintf_ln(stderr, _("If you wish to set tracking information for this branch you can do so with:")); |
482 | 0 | fprintf(stderr, "\n"); |
483 | 0 | fprintf_ln(stderr, " git branch --set-upstream-to=%s/%s %s\n", |
484 | 0 | remote_name, _("<branch>"), curr_branch->name); |
485 | 0 | } else |
486 | 0 | fprintf_ln(stderr, _("Your configuration specifies to merge with the ref '%s'\n" |
487 | 0 | "from the remote, but no such ref was fetched."), |
488 | 0 | *curr_branch->merge_name); |
489 | 0 | exit(1); |
490 | 0 | } |
491 | | |
492 | | /** |
493 | | * Parses argv into [<repo> [<refspecs>...]], returning their values in `repo` |
494 | | * as a string and `refspecs` as a null-terminated array of strings. If `repo` |
495 | | * is not provided in argv, it is set to NULL. |
496 | | */ |
497 | | static void parse_repo_refspecs(int argc, const char **argv, const char **repo, |
498 | | const char ***refspecs) |
499 | 0 | { |
500 | 0 | if (argc > 0) { |
501 | 0 | *repo = *argv++; |
502 | 0 | argc--; |
503 | 0 | } else |
504 | 0 | *repo = NULL; |
505 | 0 | *refspecs = argv; |
506 | 0 | } |
507 | | |
508 | | /** |
509 | | * Runs git-fetch, returning its exit status. `repo` and `refspecs` are the |
510 | | * repository and refspecs to fetch, or NULL if they are not provided. |
511 | | */ |
512 | | static int run_fetch(const char *repo, const char **refspecs) |
513 | 0 | { |
514 | 0 | struct child_process cmd = CHILD_PROCESS_INIT; |
515 | |
|
516 | 0 | strvec_pushl(&cmd.args, "fetch", "--update-head-ok", NULL); |
517 | | |
518 | | /* Shared options */ |
519 | 0 | argv_push_verbosity(&cmd.args); |
520 | 0 | if (opt_progress) |
521 | 0 | strvec_push(&cmd.args, opt_progress); |
522 | | |
523 | | /* Options passed to git-fetch */ |
524 | 0 | if (opt_all) |
525 | 0 | strvec_push(&cmd.args, opt_all); |
526 | 0 | if (opt_append) |
527 | 0 | strvec_push(&cmd.args, opt_append); |
528 | 0 | if (opt_upload_pack) |
529 | 0 | strvec_push(&cmd.args, opt_upload_pack); |
530 | 0 | argv_push_force(&cmd.args); |
531 | 0 | if (opt_tags) |
532 | 0 | strvec_push(&cmd.args, opt_tags); |
533 | 0 | if (opt_prune) |
534 | 0 | strvec_push(&cmd.args, opt_prune); |
535 | 0 | if (recurse_submodules_cli != RECURSE_SUBMODULES_DEFAULT) |
536 | 0 | switch (recurse_submodules_cli) { |
537 | 0 | case RECURSE_SUBMODULES_ON: |
538 | 0 | strvec_push(&cmd.args, "--recurse-submodules=on"); |
539 | 0 | break; |
540 | 0 | case RECURSE_SUBMODULES_OFF: |
541 | 0 | strvec_push(&cmd.args, "--recurse-submodules=no"); |
542 | 0 | break; |
543 | 0 | case RECURSE_SUBMODULES_ON_DEMAND: |
544 | 0 | strvec_push(&cmd.args, "--recurse-submodules=on-demand"); |
545 | 0 | break; |
546 | 0 | default: |
547 | 0 | BUG("submodule recursion option not understood"); |
548 | 0 | } |
549 | 0 | if (max_children) |
550 | 0 | strvec_push(&cmd.args, max_children); |
551 | 0 | if (opt_dry_run) |
552 | 0 | strvec_push(&cmd.args, "--dry-run"); |
553 | 0 | if (opt_keep) |
554 | 0 | strvec_push(&cmd.args, opt_keep); |
555 | 0 | if (opt_depth) |
556 | 0 | strvec_push(&cmd.args, opt_depth); |
557 | 0 | if (opt_unshallow) |
558 | 0 | strvec_push(&cmd.args, opt_unshallow); |
559 | 0 | if (opt_update_shallow) |
560 | 0 | strvec_push(&cmd.args, opt_update_shallow); |
561 | 0 | if (opt_refmap) |
562 | 0 | strvec_push(&cmd.args, opt_refmap); |
563 | 0 | if (opt_ipv4) |
564 | 0 | strvec_push(&cmd.args, opt_ipv4); |
565 | 0 | if (opt_ipv6) |
566 | 0 | strvec_push(&cmd.args, opt_ipv6); |
567 | 0 | if (opt_show_forced_updates > 0) |
568 | 0 | strvec_push(&cmd.args, "--show-forced-updates"); |
569 | 0 | else if (opt_show_forced_updates == 0) |
570 | 0 | strvec_push(&cmd.args, "--no-show-forced-updates"); |
571 | 0 | if (set_upstream) |
572 | 0 | strvec_push(&cmd.args, set_upstream); |
573 | 0 | strvec_pushv(&cmd.args, opt_fetch.v); |
574 | |
|
575 | 0 | if (repo) { |
576 | 0 | strvec_push(&cmd.args, repo); |
577 | 0 | strvec_pushv(&cmd.args, refspecs); |
578 | 0 | } else if (*refspecs) |
579 | 0 | BUG("refspecs without repo?"); |
580 | 0 | cmd.git_cmd = 1; |
581 | 0 | cmd.close_object_store = 1; |
582 | 0 | return run_command(&cmd); |
583 | 0 | } |
584 | | |
585 | | /** |
586 | | * "Pulls into void" by branching off merge_head. |
587 | | */ |
588 | | static int pull_into_void(const struct object_id *merge_head, |
589 | | const struct object_id *curr_head) |
590 | 0 | { |
591 | 0 | if (opt_verify_signatures) { |
592 | 0 | struct commit *commit; |
593 | |
|
594 | 0 | commit = lookup_commit(the_repository, merge_head); |
595 | 0 | if (!commit) |
596 | 0 | die(_("unable to access commit %s"), |
597 | 0 | oid_to_hex(merge_head)); |
598 | | |
599 | 0 | verify_merge_signature(commit, opt_verbosity, |
600 | 0 | check_trust_level); |
601 | 0 | } |
602 | | |
603 | | /* |
604 | | * Two-way merge: we treat the index as based on an empty tree, |
605 | | * and try to fast-forward to HEAD. This ensures we will not lose |
606 | | * index/worktree changes that the user already made on the unborn |
607 | | * branch. |
608 | | */ |
609 | 0 | if (checkout_fast_forward(the_repository, |
610 | 0 | the_hash_algo->empty_tree, |
611 | 0 | merge_head, 0)) |
612 | 0 | return 1; |
613 | | |
614 | 0 | if (refs_update_ref(get_main_ref_store(the_repository), "initial pull", "HEAD", merge_head, curr_head, 0, UPDATE_REFS_DIE_ON_ERR)) |
615 | 0 | return 1; |
616 | | |
617 | 0 | return 0; |
618 | 0 | } |
619 | | |
620 | | static int rebase_submodules(void) |
621 | 0 | { |
622 | 0 | struct child_process cp = CHILD_PROCESS_INIT; |
623 | |
|
624 | 0 | cp.git_cmd = 1; |
625 | 0 | cp.no_stdin = 1; |
626 | 0 | strvec_pushl(&cp.args, "submodule", "update", |
627 | 0 | "--recursive", "--rebase", NULL); |
628 | 0 | argv_push_verbosity(&cp.args); |
629 | |
|
630 | 0 | return run_command(&cp); |
631 | 0 | } |
632 | | |
633 | | static int update_submodules(void) |
634 | 0 | { |
635 | 0 | struct child_process cp = CHILD_PROCESS_INIT; |
636 | |
|
637 | 0 | cp.git_cmd = 1; |
638 | 0 | cp.no_stdin = 1; |
639 | 0 | strvec_pushl(&cp.args, "submodule", "update", |
640 | 0 | "--recursive", "--checkout", NULL); |
641 | 0 | argv_push_verbosity(&cp.args); |
642 | |
|
643 | 0 | return run_command(&cp); |
644 | 0 | } |
645 | | |
646 | | /** |
647 | | * Runs git-merge, returning its exit status. |
648 | | */ |
649 | | static int run_merge(void) |
650 | 0 | { |
651 | 0 | struct child_process cmd = CHILD_PROCESS_INIT; |
652 | |
|
653 | 0 | strvec_pushl(&cmd.args, "merge", NULL); |
654 | | |
655 | | /* Shared options */ |
656 | 0 | argv_push_verbosity(&cmd.args); |
657 | 0 | if (opt_progress) |
658 | 0 | strvec_push(&cmd.args, opt_progress); |
659 | | |
660 | | /* Options passed to git-merge */ |
661 | 0 | if (opt_diffstat) |
662 | 0 | strvec_push(&cmd.args, opt_diffstat); |
663 | 0 | if (opt_log) |
664 | 0 | strvec_push(&cmd.args, opt_log); |
665 | 0 | if (opt_signoff) |
666 | 0 | strvec_push(&cmd.args, opt_signoff); |
667 | 0 | if (opt_squash) |
668 | 0 | strvec_push(&cmd.args, opt_squash); |
669 | 0 | if (opt_commit) |
670 | 0 | strvec_push(&cmd.args, opt_commit); |
671 | 0 | if (opt_edit) |
672 | 0 | strvec_push(&cmd.args, opt_edit); |
673 | 0 | if (cleanup_arg) |
674 | 0 | strvec_pushf(&cmd.args, "--cleanup=%s", cleanup_arg); |
675 | 0 | if (opt_ff) |
676 | 0 | strvec_push(&cmd.args, opt_ff); |
677 | 0 | if (opt_verify) |
678 | 0 | strvec_push(&cmd.args, opt_verify); |
679 | 0 | if (opt_verify_signatures) |
680 | 0 | strvec_push(&cmd.args, opt_verify_signatures); |
681 | 0 | strvec_pushv(&cmd.args, opt_strategies.v); |
682 | 0 | strvec_pushv(&cmd.args, opt_strategy_opts.v); |
683 | 0 | if (opt_gpg_sign) |
684 | 0 | strvec_push(&cmd.args, opt_gpg_sign); |
685 | 0 | if (opt_autostash == 0) |
686 | 0 | strvec_push(&cmd.args, "--no-autostash"); |
687 | 0 | else if (opt_autostash == 1) |
688 | 0 | strvec_push(&cmd.args, "--autostash"); |
689 | 0 | if (opt_allow_unrelated_histories > 0) |
690 | 0 | strvec_push(&cmd.args, "--allow-unrelated-histories"); |
691 | |
|
692 | 0 | strvec_push(&cmd.args, "FETCH_HEAD"); |
693 | 0 | cmd.git_cmd = 1; |
694 | 0 | return run_command(&cmd); |
695 | 0 | } |
696 | | |
697 | | /** |
698 | | * Returns remote's upstream branch for the current branch. If remote is NULL, |
699 | | * the current branch's configured default remote is used. Returns NULL if |
700 | | * `remote` does not name a valid remote, HEAD does not point to a branch, |
701 | | * remote is not the branch's configured remote or the branch does not have any |
702 | | * configured upstream branch. |
703 | | */ |
704 | | static const char *get_upstream_branch(const char *remote) |
705 | 0 | { |
706 | 0 | struct remote *rm; |
707 | 0 | struct branch *curr_branch; |
708 | 0 | const char *curr_branch_remote; |
709 | |
|
710 | 0 | rm = remote_get(remote); |
711 | 0 | if (!rm) |
712 | 0 | return NULL; |
713 | | |
714 | 0 | curr_branch = branch_get("HEAD"); |
715 | 0 | if (!curr_branch) |
716 | 0 | return NULL; |
717 | | |
718 | 0 | curr_branch_remote = remote_for_branch(curr_branch, NULL); |
719 | 0 | assert(curr_branch_remote); |
720 | | |
721 | 0 | if (strcmp(curr_branch_remote, rm->name)) |
722 | 0 | return NULL; |
723 | | |
724 | 0 | return branch_get_upstream(curr_branch, NULL); |
725 | 0 | } |
726 | | |
727 | | /** |
728 | | * Derives the remote-tracking branch from the remote and refspec. |
729 | | * |
730 | | * FIXME: The current implementation assumes the default mapping of |
731 | | * refs/heads/<branch_name> to refs/remotes/<remote_name>/<branch_name>. |
732 | | */ |
733 | | static const char *get_tracking_branch(const char *remote, const char *refspec) |
734 | 0 | { |
735 | 0 | struct refspec_item spec; |
736 | 0 | const char *spec_src; |
737 | 0 | const char *merge_branch; |
738 | |
|
739 | 0 | refspec_item_init_or_die(&spec, refspec, REFSPEC_FETCH); |
740 | 0 | spec_src = spec.src; |
741 | 0 | if (!*spec_src || !strcmp(spec_src, "HEAD")) |
742 | 0 | spec_src = "HEAD"; |
743 | 0 | else if (skip_prefix(spec_src, "heads/", &spec_src)) |
744 | 0 | ; |
745 | 0 | else if (skip_prefix(spec_src, "refs/heads/", &spec_src)) |
746 | 0 | ; |
747 | 0 | else if (starts_with(spec_src, "refs/") || |
748 | 0 | starts_with(spec_src, "tags/") || |
749 | 0 | starts_with(spec_src, "remotes/")) |
750 | 0 | spec_src = ""; |
751 | |
|
752 | 0 | if (*spec_src) { |
753 | 0 | if (!strcmp(remote, ".")) |
754 | 0 | merge_branch = mkpath("refs/heads/%s", spec_src); |
755 | 0 | else |
756 | 0 | merge_branch = mkpath("refs/remotes/%s/%s", remote, spec_src); |
757 | 0 | } else |
758 | 0 | merge_branch = NULL; |
759 | |
|
760 | 0 | refspec_item_clear(&spec); |
761 | 0 | return merge_branch; |
762 | 0 | } |
763 | | |
764 | | /** |
765 | | * Given the repo and refspecs, sets fork_point to the point at which the |
766 | | * current branch forked from its remote-tracking branch. Returns 0 on success, |
767 | | * -1 on failure. |
768 | | */ |
769 | | static int get_rebase_fork_point(struct object_id *fork_point, const char *repo, |
770 | | const char *refspec) |
771 | 0 | { |
772 | 0 | int ret; |
773 | 0 | struct branch *curr_branch; |
774 | 0 | const char *remote_branch; |
775 | 0 | struct child_process cp = CHILD_PROCESS_INIT; |
776 | 0 | struct strbuf sb = STRBUF_INIT; |
777 | |
|
778 | 0 | curr_branch = branch_get("HEAD"); |
779 | 0 | if (!curr_branch) |
780 | 0 | return -1; |
781 | | |
782 | 0 | if (refspec) |
783 | 0 | remote_branch = get_tracking_branch(repo, refspec); |
784 | 0 | else |
785 | 0 | remote_branch = get_upstream_branch(repo); |
786 | |
|
787 | 0 | if (!remote_branch) |
788 | 0 | return -1; |
789 | | |
790 | 0 | strvec_pushl(&cp.args, "merge-base", "--fork-point", |
791 | 0 | remote_branch, curr_branch->name, NULL); |
792 | 0 | cp.no_stdin = 1; |
793 | 0 | cp.no_stderr = 1; |
794 | 0 | cp.git_cmd = 1; |
795 | |
|
796 | 0 | ret = capture_command(&cp, &sb, GIT_MAX_HEXSZ); |
797 | 0 | if (ret) |
798 | 0 | goto cleanup; |
799 | | |
800 | 0 | ret = get_oid_hex(sb.buf, fork_point); |
801 | 0 | if (ret) |
802 | 0 | goto cleanup; |
803 | | |
804 | 0 | cleanup: |
805 | 0 | strbuf_release(&sb); |
806 | 0 | return ret ? -1 : 0; |
807 | 0 | } |
808 | | |
809 | | /** |
810 | | * Sets merge_base to the octopus merge base of curr_head, merge_head and |
811 | | * fork_point. Returns 0 if a merge base is found, 1 otherwise. |
812 | | */ |
813 | | static int get_octopus_merge_base(struct object_id *merge_base, |
814 | | const struct object_id *curr_head, |
815 | | const struct object_id *merge_head, |
816 | | const struct object_id *fork_point) |
817 | 0 | { |
818 | 0 | struct commit_list *revs = NULL, *result = NULL; |
819 | |
|
820 | 0 | commit_list_insert(lookup_commit_reference(the_repository, curr_head), |
821 | 0 | &revs); |
822 | 0 | commit_list_insert(lookup_commit_reference(the_repository, merge_head), |
823 | 0 | &revs); |
824 | 0 | if (!is_null_oid(fork_point)) |
825 | 0 | commit_list_insert(lookup_commit_reference(the_repository, fork_point), |
826 | 0 | &revs); |
827 | |
|
828 | 0 | if (get_octopus_merge_bases(revs, &result) < 0) |
829 | 0 | exit(128); |
830 | 0 | free_commit_list(revs); |
831 | 0 | reduce_heads_replace(&result); |
832 | |
|
833 | 0 | if (!result) |
834 | 0 | return 1; |
835 | | |
836 | 0 | oidcpy(merge_base, &result->item->object.oid); |
837 | 0 | free_commit_list(result); |
838 | 0 | return 0; |
839 | 0 | } |
840 | | |
841 | | /** |
842 | | * Given the current HEAD oid, the merge head returned from git-fetch and the |
843 | | * fork point calculated by get_rebase_fork_point(), compute the <newbase> and |
844 | | * <upstream> arguments to use for the upcoming git-rebase invocation. |
845 | | */ |
846 | | static int get_rebase_newbase_and_upstream(struct object_id *newbase, |
847 | | struct object_id *upstream, |
848 | | const struct object_id *curr_head, |
849 | | const struct object_id *merge_head, |
850 | | const struct object_id *fork_point) |
851 | 0 | { |
852 | 0 | struct object_id oct_merge_base; |
853 | |
|
854 | 0 | if (!get_octopus_merge_base(&oct_merge_base, curr_head, merge_head, fork_point)) |
855 | 0 | if (!is_null_oid(fork_point) && oideq(&oct_merge_base, fork_point)) |
856 | 0 | fork_point = NULL; |
857 | |
|
858 | 0 | if (fork_point && !is_null_oid(fork_point)) |
859 | 0 | oidcpy(upstream, fork_point); |
860 | 0 | else |
861 | 0 | oidcpy(upstream, merge_head); |
862 | |
|
863 | 0 | oidcpy(newbase, merge_head); |
864 | |
|
865 | 0 | return 0; |
866 | 0 | } |
867 | | |
868 | | /** |
869 | | * Given the <newbase> and <upstream> calculated by |
870 | | * get_rebase_newbase_and_upstream(), runs git-rebase with the |
871 | | * appropriate arguments and returns its exit status. |
872 | | */ |
873 | | static int run_rebase(const struct object_id *newbase, |
874 | | const struct object_id *upstream) |
875 | 0 | { |
876 | 0 | struct child_process cmd = CHILD_PROCESS_INIT; |
877 | |
|
878 | 0 | strvec_push(&cmd.args, "rebase"); |
879 | | |
880 | | /* Shared options */ |
881 | 0 | argv_push_verbosity(&cmd.args); |
882 | | |
883 | | /* Options passed to git-rebase */ |
884 | 0 | if (opt_rebase == REBASE_MERGES) |
885 | 0 | strvec_push(&cmd.args, "--rebase-merges"); |
886 | 0 | else if (opt_rebase == REBASE_INTERACTIVE) |
887 | 0 | strvec_push(&cmd.args, "--interactive"); |
888 | 0 | if (opt_diffstat) |
889 | 0 | strvec_push(&cmd.args, opt_diffstat); |
890 | 0 | strvec_pushv(&cmd.args, opt_strategies.v); |
891 | 0 | strvec_pushv(&cmd.args, opt_strategy_opts.v); |
892 | 0 | if (opt_gpg_sign) |
893 | 0 | strvec_push(&cmd.args, opt_gpg_sign); |
894 | 0 | if (opt_signoff) |
895 | 0 | strvec_push(&cmd.args, opt_signoff); |
896 | 0 | if (opt_autostash == 0) |
897 | 0 | strvec_push(&cmd.args, "--no-autostash"); |
898 | 0 | else if (opt_autostash == 1) |
899 | 0 | strvec_push(&cmd.args, "--autostash"); |
900 | 0 | if (opt_verify_signatures && |
901 | 0 | !strcmp(opt_verify_signatures, "--verify-signatures")) |
902 | 0 | warning(_("ignoring --verify-signatures for rebase")); |
903 | |
|
904 | 0 | strvec_push(&cmd.args, "--onto"); |
905 | 0 | strvec_push(&cmd.args, oid_to_hex(newbase)); |
906 | |
|
907 | 0 | strvec_push(&cmd.args, oid_to_hex(upstream)); |
908 | |
|
909 | 0 | cmd.git_cmd = 1; |
910 | 0 | return run_command(&cmd); |
911 | 0 | } |
912 | | |
913 | | static int get_can_ff(struct object_id *orig_head, |
914 | | struct oid_array *merge_heads) |
915 | 0 | { |
916 | 0 | int ret; |
917 | 0 | struct commit_list *list = NULL; |
918 | 0 | struct commit *merge_head, *head; |
919 | 0 | struct object_id *orig_merge_head; |
920 | |
|
921 | 0 | if (merge_heads->nr > 1) |
922 | 0 | return 0; |
923 | | |
924 | 0 | orig_merge_head = &merge_heads->oid[0]; |
925 | 0 | head = lookup_commit_reference(the_repository, orig_head); |
926 | 0 | commit_list_insert(head, &list); |
927 | 0 | merge_head = lookup_commit_reference(the_repository, orig_merge_head); |
928 | 0 | ret = repo_is_descendant_of(the_repository, merge_head, list); |
929 | 0 | free_commit_list(list); |
930 | 0 | if (ret < 0) |
931 | 0 | exit(128); |
932 | 0 | return ret; |
933 | 0 | } |
934 | | |
935 | | /* |
936 | | * Is orig_head a descendant of _all_ merge_heads? |
937 | | * Unfortunately is_descendant_of() cannot be used as it asks |
938 | | * if orig_head is a descendant of at least one of them. |
939 | | */ |
940 | | static int already_up_to_date(struct object_id *orig_head, |
941 | | struct oid_array *merge_heads) |
942 | 0 | { |
943 | 0 | int i; |
944 | 0 | struct commit *ours; |
945 | |
|
946 | 0 | ours = lookup_commit_reference(the_repository, orig_head); |
947 | 0 | for (i = 0; i < merge_heads->nr; i++) { |
948 | 0 | struct commit_list *list = NULL; |
949 | 0 | struct commit *theirs; |
950 | 0 | int ok; |
951 | |
|
952 | 0 | theirs = lookup_commit_reference(the_repository, &merge_heads->oid[i]); |
953 | 0 | commit_list_insert(theirs, &list); |
954 | 0 | ok = repo_is_descendant_of(the_repository, ours, list); |
955 | 0 | free_commit_list(list); |
956 | 0 | if (ok < 0) |
957 | 0 | exit(128); |
958 | 0 | if (!ok) |
959 | 0 | return 0; |
960 | 0 | } |
961 | 0 | return 1; |
962 | 0 | } |
963 | | |
964 | | static void show_advice_pull_non_ff(void) |
965 | 0 | { |
966 | 0 | advise(_("You have divergent branches and need to specify how to reconcile them.\n" |
967 | 0 | "You can do so by running one of the following commands sometime before\n" |
968 | 0 | "your next pull:\n" |
969 | 0 | "\n" |
970 | 0 | " git config pull.rebase false # merge\n" |
971 | 0 | " git config pull.rebase true # rebase\n" |
972 | 0 | " git config pull.ff only # fast-forward only\n" |
973 | 0 | "\n" |
974 | 0 | "You can replace \"git config\" with \"git config --global\" to set a default\n" |
975 | 0 | "preference for all repositories. You can also pass --rebase, --no-rebase,\n" |
976 | 0 | "or --ff-only on the command line to override the configured default per\n" |
977 | 0 | "invocation.\n")); |
978 | 0 | } |
979 | | |
980 | | int cmd_pull(int argc, const char **argv, const char *prefix) |
981 | 0 | { |
982 | 0 | const char *repo, **refspecs; |
983 | 0 | struct oid_array merge_heads = OID_ARRAY_INIT; |
984 | 0 | struct object_id orig_head, curr_head; |
985 | 0 | struct object_id rebase_fork_point; |
986 | 0 | int rebase_unspecified = 0; |
987 | 0 | int can_ff; |
988 | 0 | int divergent; |
989 | 0 | int ret; |
990 | |
|
991 | 0 | if (!getenv("GIT_REFLOG_ACTION")) |
992 | 0 | set_reflog_message(argc, argv); |
993 | |
|
994 | 0 | git_config(git_pull_config, NULL); |
995 | 0 | if (the_repository->gitdir) { |
996 | 0 | prepare_repo_settings(the_repository); |
997 | 0 | the_repository->settings.command_requires_full_index = 0; |
998 | 0 | } |
999 | |
|
1000 | 0 | argc = parse_options(argc, argv, prefix, pull_options, pull_usage, 0); |
1001 | |
|
1002 | 0 | if (recurse_submodules_cli != RECURSE_SUBMODULES_DEFAULT) |
1003 | 0 | recurse_submodules = recurse_submodules_cli; |
1004 | |
|
1005 | 0 | if (cleanup_arg) |
1006 | | /* |
1007 | | * this only checks the validity of cleanup_arg; we don't need |
1008 | | * a valid value for use_editor |
1009 | | */ |
1010 | 0 | get_cleanup_mode(cleanup_arg, 0); |
1011 | |
|
1012 | 0 | parse_repo_refspecs(argc, argv, &repo, &refspecs); |
1013 | |
|
1014 | 0 | if (!opt_ff) { |
1015 | 0 | opt_ff = xstrdup_or_null(config_get_ff()); |
1016 | | /* |
1017 | | * A subtle point: opt_ff was set on the line above via |
1018 | | * reading from config. opt_rebase, in contrast, is set |
1019 | | * before this point via command line options. The setting |
1020 | | * of opt_rebase via reading from config (using |
1021 | | * config_get_rebase()) does not happen until later. We |
1022 | | * are relying on the next if-condition happening before |
1023 | | * the config_get_rebase() call so that an explicit |
1024 | | * "--rebase" can override a config setting of |
1025 | | * pull.ff=only. |
1026 | | */ |
1027 | 0 | if (opt_rebase >= 0 && opt_ff && !strcmp(opt_ff, "--ff-only")) |
1028 | 0 | opt_ff = "--ff"; |
1029 | 0 | } |
1030 | |
|
1031 | 0 | if (opt_rebase < 0) |
1032 | 0 | opt_rebase = config_get_rebase(&rebase_unspecified); |
1033 | |
|
1034 | 0 | if (repo_read_index_unmerged(the_repository)) |
1035 | 0 | die_resolve_conflict("pull"); |
1036 | | |
1037 | 0 | if (file_exists(git_path_merge_head(the_repository))) |
1038 | 0 | die_conclude_merge(); |
1039 | | |
1040 | 0 | if (repo_get_oid(the_repository, "HEAD", &orig_head)) |
1041 | 0 | oidclr(&orig_head, the_repository->hash_algo); |
1042 | |
|
1043 | 0 | if (opt_rebase) { |
1044 | 0 | if (opt_autostash == -1) |
1045 | 0 | opt_autostash = config_autostash; |
1046 | |
|
1047 | 0 | if (is_null_oid(&orig_head) && !is_index_unborn(the_repository->index)) |
1048 | 0 | die(_("Updating an unborn branch with changes added to the index.")); |
1049 | | |
1050 | 0 | if (!opt_autostash) |
1051 | 0 | require_clean_work_tree(the_repository, |
1052 | 0 | N_("pull with rebase"), |
1053 | 0 | _("Please commit or stash them."), 1, 0); |
1054 | |
|
1055 | 0 | if (get_rebase_fork_point(&rebase_fork_point, repo, *refspecs)) |
1056 | 0 | oidclr(&rebase_fork_point, the_repository->hash_algo); |
1057 | 0 | } |
1058 | | |
1059 | 0 | if (run_fetch(repo, refspecs)) |
1060 | 0 | return 1; |
1061 | | |
1062 | 0 | if (opt_dry_run) |
1063 | 0 | return 0; |
1064 | | |
1065 | 0 | if (repo_get_oid(the_repository, "HEAD", &curr_head)) |
1066 | 0 | oidclr(&curr_head, the_repository->hash_algo); |
1067 | |
|
1068 | 0 | if (!is_null_oid(&orig_head) && !is_null_oid(&curr_head) && |
1069 | 0 | !oideq(&orig_head, &curr_head)) { |
1070 | | /* |
1071 | | * The fetch involved updating the current branch. |
1072 | | * |
1073 | | * The working tree and the index file are still based on |
1074 | | * orig_head commit, but we are merging into curr_head. |
1075 | | * Update the working tree to match curr_head. |
1076 | | */ |
1077 | |
|
1078 | 0 | warning(_("fetch updated the current branch head.\n" |
1079 | 0 | "fast-forwarding your working tree from\n" |
1080 | 0 | "commit %s."), oid_to_hex(&orig_head)); |
1081 | |
|
1082 | 0 | if (checkout_fast_forward(the_repository, &orig_head, |
1083 | 0 | &curr_head, 0)) |
1084 | 0 | die(_("Cannot fast-forward your working tree.\n" |
1085 | 0 | "After making sure that you saved anything precious from\n" |
1086 | 0 | "$ git diff %s\n" |
1087 | 0 | "output, run\n" |
1088 | 0 | "$ git reset --hard\n" |
1089 | 0 | "to recover."), oid_to_hex(&orig_head)); |
1090 | 0 | } |
1091 | | |
1092 | 0 | get_merge_heads(&merge_heads); |
1093 | |
|
1094 | 0 | if (!merge_heads.nr) |
1095 | 0 | die_no_merge_candidates(repo, refspecs); |
1096 | | |
1097 | 0 | if (is_null_oid(&orig_head)) { |
1098 | 0 | if (merge_heads.nr > 1) |
1099 | 0 | die(_("Cannot merge multiple branches into empty head.")); |
1100 | 0 | ret = pull_into_void(merge_heads.oid, &curr_head); |
1101 | 0 | goto cleanup; |
1102 | 0 | } |
1103 | 0 | if (merge_heads.nr > 1) { |
1104 | 0 | if (opt_rebase) |
1105 | 0 | die(_("Cannot rebase onto multiple branches.")); |
1106 | 0 | if (opt_ff && !strcmp(opt_ff, "--ff-only")) |
1107 | 0 | die(_("Cannot fast-forward to multiple branches.")); |
1108 | 0 | } |
1109 | | |
1110 | 0 | can_ff = get_can_ff(&orig_head, &merge_heads); |
1111 | 0 | divergent = !can_ff && !already_up_to_date(&orig_head, &merge_heads); |
1112 | | |
1113 | | /* ff-only takes precedence over rebase */ |
1114 | 0 | if (opt_ff && !strcmp(opt_ff, "--ff-only")) { |
1115 | 0 | if (divergent) |
1116 | 0 | die_ff_impossible(); |
1117 | 0 | opt_rebase = REBASE_FALSE; |
1118 | 0 | } |
1119 | | /* If no action specified and we can't fast forward, then warn. */ |
1120 | 0 | if (!opt_ff && rebase_unspecified && divergent) { |
1121 | 0 | show_advice_pull_non_ff(); |
1122 | 0 | die(_("Need to specify how to reconcile divergent branches.")); |
1123 | 0 | } |
1124 | | |
1125 | 0 | if (opt_rebase) { |
1126 | 0 | struct object_id newbase; |
1127 | 0 | struct object_id upstream; |
1128 | 0 | get_rebase_newbase_and_upstream(&newbase, &upstream, &curr_head, |
1129 | 0 | merge_heads.oid, &rebase_fork_point); |
1130 | |
|
1131 | 0 | if ((recurse_submodules == RECURSE_SUBMODULES_ON || |
1132 | 0 | recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND) && |
1133 | 0 | submodule_touches_in_range(the_repository, &upstream, &curr_head)) |
1134 | 0 | die(_("cannot rebase with locally recorded submodule modifications")); |
1135 | | |
1136 | 0 | if (can_ff) { |
1137 | | /* we can fast-forward this without invoking rebase */ |
1138 | 0 | opt_ff = "--ff-only"; |
1139 | 0 | ret = run_merge(); |
1140 | 0 | } else { |
1141 | 0 | ret = run_rebase(&newbase, &upstream); |
1142 | 0 | } |
1143 | |
|
1144 | 0 | if (!ret && (recurse_submodules == RECURSE_SUBMODULES_ON || |
1145 | 0 | recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND)) |
1146 | 0 | ret = rebase_submodules(); |
1147 | |
|
1148 | 0 | goto cleanup; |
1149 | 0 | } else { |
1150 | 0 | ret = run_merge(); |
1151 | 0 | if (!ret && (recurse_submodules == RECURSE_SUBMODULES_ON || |
1152 | 0 | recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND)) |
1153 | 0 | ret = update_submodules(); |
1154 | 0 | goto cleanup; |
1155 | 0 | } |
1156 | | |
1157 | 0 | cleanup: |
1158 | 0 | oid_array_clear(&merge_heads); |
1159 | 0 | return ret; |
1160 | 0 | } |