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