4 * Based on git-pull.sh by Junio C Hamano
6 * Fetch one or more remote refs and merge it/them into the current HEAD.
8 #define USE_THE_INDEX_COMPATIBILITY_MACROS
12 #include "parse-options.h"
14 #include "run-command.h"
15 #include "sha1-array.h"
21 #include "submodule.h"
22 #include "submodule-config.h"
25 #include "wt-status.h"
26 #include "commit-reach.h"
38 * Parses the value of --rebase. If value is a false value, returns
39 * REBASE_FALSE. If value is a true value, returns REBASE_TRUE. If value is
40 * "merges", returns REBASE_MERGES. If value is "preserve", returns
41 * REBASE_PRESERVE. If value is a invalid value, dies with a fatal error if
42 * fatal is true, otherwise returns REBASE_INVALID.
44 static enum rebase_type
parse_config_rebase(const char *key
, const char *value
,
47 int v
= git_parse_maybe_bool(value
);
53 else if (!strcmp(value
, "preserve") || !strcmp(value
, "p"))
54 return REBASE_PRESERVE
;
55 else if (!strcmp(value
, "merges") || !strcmp(value
, "m"))
57 else if (!strcmp(value
, "interactive") || !strcmp(value
, "i"))
58 return REBASE_INTERACTIVE
;
61 die(_("Invalid value for %s: %s"), key
, value
);
63 error(_("Invalid value for %s: %s"), key
, value
);
65 return REBASE_INVALID
;
69 * Callback for --rebase, which parses arg with parse_config_rebase().
71 static int parse_opt_rebase(const struct option
*opt
, const char *arg
, int unset
)
73 enum rebase_type
*value
= opt
->value
;
76 *value
= parse_config_rebase("--rebase", arg
, 0);
78 *value
= unset
? REBASE_FALSE
: REBASE_TRUE
;
79 return *value
== REBASE_INVALID
? -1 : 0;
82 static const char * const pull_usage
[] = {
83 N_("git pull [<options>] [<repository> [<refspec>...]]"),
88 static int opt_verbosity
;
89 static char *opt_progress
;
90 static int recurse_submodules
= RECURSE_SUBMODULES_DEFAULT
;
92 /* Options passed to git-merge or git-rebase */
93 static enum rebase_type opt_rebase
= -1;
94 static char *opt_diffstat
;
96 static char *opt_signoff
;
97 static char *opt_squash
;
98 static char *opt_commit
;
99 static char *opt_edit
;
101 static char *opt_verify_signatures
;
102 static int opt_autostash
= -1;
103 static int config_autostash
;
104 static struct argv_array opt_strategies
= ARGV_ARRAY_INIT
;
105 static struct argv_array opt_strategy_opts
= ARGV_ARRAY_INIT
;
106 static char *opt_gpg_sign
;
107 static int opt_allow_unrelated_histories
;
109 /* Options passed to git-fetch */
110 static char *opt_all
;
111 static char *opt_append
;
112 static char *opt_upload_pack
;
113 static int opt_force
;
114 static char *opt_tags
;
115 static char *opt_prune
;
116 static char *max_children
;
117 static int opt_dry_run
;
118 static char *opt_keep
;
119 static char *opt_depth
;
120 static char *opt_unshallow
;
121 static char *opt_update_shallow
;
122 static char *opt_refmap
;
123 static char *opt_ipv4
;
124 static char *opt_ipv6
;
126 static struct option pull_options
[] = {
128 OPT__VERBOSITY(&opt_verbosity
),
129 OPT_PASSTHRU(0, "progress", &opt_progress
, NULL
,
130 N_("force progress reporting"),
132 { OPTION_CALLBACK
, 0, "recurse-submodules",
133 &recurse_submodules
, N_("on-demand"),
134 N_("control for recursive fetching of submodules"),
135 PARSE_OPT_OPTARG
, option_fetch_parse_recurse_submodules
},
137 /* Options passed to git-merge or git-rebase */
138 OPT_GROUP(N_("Options related to merging")),
139 { OPTION_CALLBACK
, 'r', "rebase", &opt_rebase
,
140 "(false|true|merges|preserve|interactive)",
141 N_("incorporate changes by rebasing rather than merging"),
142 PARSE_OPT_OPTARG
, parse_opt_rebase
},
143 OPT_PASSTHRU('n', NULL
, &opt_diffstat
, NULL
,
144 N_("do not show a diffstat at the end of the merge"),
145 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
),
146 OPT_PASSTHRU(0, "stat", &opt_diffstat
, NULL
,
147 N_("show a diffstat at the end of the merge"),
149 OPT_PASSTHRU(0, "summary", &opt_diffstat
, NULL
,
150 N_("(synonym to --stat)"),
151 PARSE_OPT_NOARG
| PARSE_OPT_HIDDEN
),
152 OPT_PASSTHRU(0, "log", &opt_log
, N_("n"),
153 N_("add (at most <n>) entries from shortlog to merge commit message"),
155 OPT_PASSTHRU(0, "signoff", &opt_signoff
, NULL
,
156 N_("add Signed-off-by:"),
158 OPT_PASSTHRU(0, "squash", &opt_squash
, NULL
,
159 N_("create a single commit instead of doing a merge"),
161 OPT_PASSTHRU(0, "commit", &opt_commit
, NULL
,
162 N_("perform a commit if the merge succeeds (default)"),
164 OPT_PASSTHRU(0, "edit", &opt_edit
, NULL
,
165 N_("edit message before committing"),
167 OPT_PASSTHRU(0, "ff", &opt_ff
, NULL
,
168 N_("allow fast-forward"),
170 OPT_PASSTHRU(0, "ff-only", &opt_ff
, NULL
,
171 N_("abort if fast-forward is not possible"),
172 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
),
173 OPT_PASSTHRU(0, "verify-signatures", &opt_verify_signatures
, NULL
,
174 N_("verify that the named commit has a valid GPG signature"),
176 OPT_BOOL(0, "autostash", &opt_autostash
,
177 N_("automatically stash/stash pop before and after rebase")),
178 OPT_PASSTHRU_ARGV('s', "strategy", &opt_strategies
, N_("strategy"),
179 N_("merge strategy to use"),
181 OPT_PASSTHRU_ARGV('X', "strategy-option", &opt_strategy_opts
,
183 N_("option for selected merge strategy"),
185 OPT_PASSTHRU('S', "gpg-sign", &opt_gpg_sign
, N_("key-id"),
186 N_("GPG sign commit"),
188 OPT_SET_INT(0, "allow-unrelated-histories",
189 &opt_allow_unrelated_histories
,
190 N_("allow merging unrelated histories"), 1),
192 /* Options passed to git-fetch */
193 OPT_GROUP(N_("Options related to fetching")),
194 OPT_PASSTHRU(0, "all", &opt_all
, NULL
,
195 N_("fetch from all remotes"),
197 OPT_PASSTHRU('a', "append", &opt_append
, NULL
,
198 N_("append to .git/FETCH_HEAD instead of overwriting"),
200 OPT_PASSTHRU(0, "upload-pack", &opt_upload_pack
, N_("path"),
201 N_("path to upload pack on remote end"),
203 OPT__FORCE(&opt_force
, N_("force overwrite of local branch"), 0),
204 OPT_PASSTHRU('t', "tags", &opt_tags
, NULL
,
205 N_("fetch all tags and associated objects"),
207 OPT_PASSTHRU('p', "prune", &opt_prune
, NULL
,
208 N_("prune remote-tracking branches no longer on remote"),
210 OPT_PASSTHRU('j', "jobs", &max_children
, N_("n"),
211 N_("number of submodules pulled in parallel"),
213 OPT_BOOL(0, "dry-run", &opt_dry_run
,
215 OPT_PASSTHRU('k', "keep", &opt_keep
, NULL
,
216 N_("keep downloaded pack"),
218 OPT_PASSTHRU(0, "depth", &opt_depth
, N_("depth"),
219 N_("deepen history of shallow clone"),
221 OPT_PASSTHRU(0, "unshallow", &opt_unshallow
, NULL
,
222 N_("convert to a complete repository"),
223 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
),
224 OPT_PASSTHRU(0, "update-shallow", &opt_update_shallow
, NULL
,
225 N_("accept refs that update .git/shallow"),
227 OPT_PASSTHRU(0, "refmap", &opt_refmap
, N_("refmap"),
228 N_("specify fetch refmap"),
230 OPT_PASSTHRU('4', "ipv4", &opt_ipv4
, NULL
,
231 N_("use IPv4 addresses only"),
233 OPT_PASSTHRU('6', "ipv6", &opt_ipv6
, NULL
,
234 N_("use IPv6 addresses only"),
241 * Pushes "-q" or "-v" switches into arr to match the opt_verbosity level.
243 static void argv_push_verbosity(struct argv_array
*arr
)
247 for (verbosity
= opt_verbosity
; verbosity
> 0; verbosity
--)
248 argv_array_push(arr
, "-v");
250 for (verbosity
= opt_verbosity
; verbosity
< 0; verbosity
++)
251 argv_array_push(arr
, "-q");
255 * Pushes "-f" switches into arr to match the opt_force level.
257 static void argv_push_force(struct argv_array
*arr
)
259 int force
= opt_force
;
261 argv_array_push(arr
, "-f");
265 * Sets the GIT_REFLOG_ACTION environment variable to the concatenation of argv
267 static void set_reflog_message(int argc
, const char **argv
)
270 struct strbuf msg
= STRBUF_INIT
;
272 for (i
= 0; i
< argc
; i
++) {
274 strbuf_addch(&msg
, ' ');
275 strbuf_addstr(&msg
, argv
[i
]);
278 setenv("GIT_REFLOG_ACTION", msg
.buf
, 0);
280 strbuf_release(&msg
);
284 * If pull.ff is unset, returns NULL. If pull.ff is "true", returns "--ff". If
285 * pull.ff is "false", returns "--no-ff". If pull.ff is "only", returns
286 * "--ff-only". Otherwise, if pull.ff is set to an invalid value, die with an
289 static const char *config_get_ff(void)
293 if (git_config_get_value("pull.ff", &value
))
296 switch (git_parse_maybe_bool(value
)) {
303 if (!strcmp(value
, "only"))
306 die(_("Invalid value for pull.ff: %s"), value
);
310 * Returns the default configured value for --rebase. It first looks for the
311 * value of "branch.$curr_branch.rebase", where $curr_branch is the current
312 * branch, and if HEAD is detached or the configuration key does not exist,
313 * looks for the value of "pull.rebase". If both configuration keys do not
314 * exist, returns REBASE_FALSE.
316 static enum rebase_type
config_get_rebase(void)
318 struct branch
*curr_branch
= branch_get("HEAD");
322 char *key
= xstrfmt("branch.%s.rebase", curr_branch
->name
);
324 if (!git_config_get_value(key
, &value
)) {
325 enum rebase_type ret
= parse_config_rebase(key
, value
, 1);
333 if (!git_config_get_value("pull.rebase", &value
))
334 return parse_config_rebase("pull.rebase", value
, 1);
340 * Read config variables.
342 static int git_pull_config(const char *var
, const char *value
, void *cb
)
344 if (!strcmp(var
, "rebase.autostash")) {
345 config_autostash
= git_config_bool(var
, value
);
347 } else if (!strcmp(var
, "submodule.recurse")) {
348 recurse_submodules
= git_config_bool(var
, value
) ?
349 RECURSE_SUBMODULES_ON
: RECURSE_SUBMODULES_OFF
;
352 return git_default_config(var
, value
, cb
);
356 * Appends merge candidates from FETCH_HEAD that are not marked not-for-merge
359 static void get_merge_heads(struct oid_array
*merge_heads
)
361 const char *filename
= git_path_fetch_head(the_repository
);
363 struct strbuf sb
= STRBUF_INIT
;
364 struct object_id oid
;
366 fp
= xfopen(filename
, "r");
367 while (strbuf_getline_lf(&sb
, fp
) != EOF
) {
368 if (get_oid_hex(sb
.buf
, &oid
))
369 continue; /* invalid line: does not start with SHA1 */
370 if (starts_with(sb
.buf
+ GIT_SHA1_HEXSZ
, "\tnot-for-merge\t"))
371 continue; /* ref is not-for-merge */
372 oid_array_append(merge_heads
, &oid
);
379 * Used by die_no_merge_candidates() as a for_each_remote() callback to
380 * retrieve the name of the remote if the repository only has one remote.
382 static int get_only_remote(struct remote
*remote
, void *cb_data
)
384 const char **remote_name
= cb_data
;
389 *remote_name
= remote
->name
;
394 * Dies with the appropriate reason for why there are no merge candidates:
396 * 1. We fetched from a specific remote, and a refspec was given, but it ended
397 * up not fetching anything. This is usually because the user provided a
398 * wildcard refspec which had no matches on the remote end.
400 * 2. We fetched from a non-default remote, but didn't specify a branch to
401 * merge. We can't use the configured one because it applies to the default
402 * remote, thus the user must specify the branches to merge.
404 * 3. We fetched from the branch's or repo's default remote, but:
406 * a. We are not on a branch, so there will never be a configured branch to
409 * b. We are on a branch, but there is no configured branch to merge with.
411 * 4. We fetched from the branch's or repo's default remote, but the configured
412 * branch to merge didn't get fetched. (Either it doesn't exist, or wasn't
413 * part of the configured fetch refspec.)
415 static void NORETURN
die_no_merge_candidates(const char *repo
, const char **refspecs
)
417 struct branch
*curr_branch
= branch_get("HEAD");
418 const char *remote
= curr_branch
? curr_branch
->remote_name
: NULL
;
422 fprintf_ln(stderr
, _("There is no candidate for rebasing against among the refs that you just fetched."));
424 fprintf_ln(stderr
, _("There are no candidates for merging among the refs that you just fetched."));
425 fprintf_ln(stderr
, _("Generally this means that you provided a wildcard refspec which had no\n"
426 "matches on the remote end."));
427 } else if (repo
&& curr_branch
&& (!remote
|| strcmp(repo
, remote
))) {
428 fprintf_ln(stderr
, _("You asked to pull from the remote '%s', but did not specify\n"
429 "a branch. Because this is not the default configured remote\n"
430 "for your current branch, you must specify a branch on the command line."),
432 } else if (!curr_branch
) {
433 fprintf_ln(stderr
, _("You are not currently on a branch."));
435 fprintf_ln(stderr
, _("Please specify which branch you want to rebase against."));
437 fprintf_ln(stderr
, _("Please specify which branch you want to merge with."));
438 fprintf_ln(stderr
, _("See git-pull(1) for details."));
439 fprintf(stderr
, "\n");
440 fprintf_ln(stderr
, " git pull %s %s", _("<remote>"), _("<branch>"));
441 fprintf(stderr
, "\n");
442 } else if (!curr_branch
->merge_nr
) {
443 const char *remote_name
= NULL
;
445 if (for_each_remote(get_only_remote
, &remote_name
) || !remote_name
)
446 remote_name
= _("<remote>");
448 fprintf_ln(stderr
, _("There is no tracking information for the current branch."));
450 fprintf_ln(stderr
, _("Please specify which branch you want to rebase against."));
452 fprintf_ln(stderr
, _("Please specify which branch you want to merge with."));
453 fprintf_ln(stderr
, _("See git-pull(1) for details."));
454 fprintf(stderr
, "\n");
455 fprintf_ln(stderr
, " git pull %s %s", _("<remote>"), _("<branch>"));
456 fprintf(stderr
, "\n");
457 fprintf_ln(stderr
, _("If you wish to set tracking information for this branch you can do so with:"));
458 fprintf(stderr
, "\n");
459 fprintf_ln(stderr
, " git branch --set-upstream-to=%s/%s %s\n",
460 remote_name
, _("<branch>"), curr_branch
->name
);
462 fprintf_ln(stderr
, _("Your configuration specifies to merge with the ref '%s'\n"
463 "from the remote, but no such ref was fetched."),
464 *curr_branch
->merge_name
);
469 * Parses argv into [<repo> [<refspecs>...]], returning their values in `repo`
470 * as a string and `refspecs` as a null-terminated array of strings. If `repo`
471 * is not provided in argv, it is set to NULL.
473 static void parse_repo_refspecs(int argc
, const char **argv
, const char **repo
,
474 const char ***refspecs
)
485 * Runs git-fetch, returning its exit status. `repo` and `refspecs` are the
486 * repository and refspecs to fetch, or NULL if they are not provided.
488 static int run_fetch(const char *repo
, const char **refspecs
)
490 struct argv_array args
= ARGV_ARRAY_INIT
;
493 argv_array_pushl(&args
, "fetch", "--update-head-ok", NULL
);
496 argv_push_verbosity(&args
);
498 argv_array_push(&args
, opt_progress
);
500 /* Options passed to git-fetch */
502 argv_array_push(&args
, opt_all
);
504 argv_array_push(&args
, opt_append
);
506 argv_array_push(&args
, opt_upload_pack
);
507 argv_push_force(&args
);
509 argv_array_push(&args
, opt_tags
);
511 argv_array_push(&args
, opt_prune
);
512 if (recurse_submodules
!= RECURSE_SUBMODULES_DEFAULT
)
513 switch (recurse_submodules
) {
514 case RECURSE_SUBMODULES_ON
:
515 argv_array_push(&args
, "--recurse-submodules=on");
517 case RECURSE_SUBMODULES_OFF
:
518 argv_array_push(&args
, "--recurse-submodules=no");
520 case RECURSE_SUBMODULES_ON_DEMAND
:
521 argv_array_push(&args
, "--recurse-submodules=on-demand");
524 BUG("submodule recursion option not understood");
527 argv_array_push(&args
, max_children
);
529 argv_array_push(&args
, "--dry-run");
531 argv_array_push(&args
, opt_keep
);
533 argv_array_push(&args
, opt_depth
);
535 argv_array_push(&args
, opt_unshallow
);
536 if (opt_update_shallow
)
537 argv_array_push(&args
, opt_update_shallow
);
539 argv_array_push(&args
, opt_refmap
);
541 argv_array_push(&args
, opt_ipv4
);
543 argv_array_push(&args
, opt_ipv6
);
546 argv_array_push(&args
, repo
);
547 argv_array_pushv(&args
, refspecs
);
548 } else if (*refspecs
)
549 BUG("refspecs without repo?");
550 ret
= run_command_v_opt(args
.argv
, RUN_GIT_CMD
);
551 argv_array_clear(&args
);
556 * "Pulls into void" by branching off merge_head.
558 static int pull_into_void(const struct object_id
*merge_head
,
559 const struct object_id
*curr_head
)
561 if (opt_verify_signatures
) {
562 struct commit
*commit
;
564 commit
= lookup_commit(the_repository
, merge_head
);
566 die(_("unable to access commit %s"),
567 oid_to_hex(merge_head
));
569 verify_merge_signature(commit
, opt_verbosity
);
573 * Two-way merge: we treat the index as based on an empty tree,
574 * and try to fast-forward to HEAD. This ensures we will not lose
575 * index/worktree changes that the user already made on the unborn
578 if (checkout_fast_forward(the_repository
,
579 the_hash_algo
->empty_tree
,
583 if (update_ref("initial pull", "HEAD", merge_head
, curr_head
, 0, UPDATE_REFS_DIE_ON_ERR
))
589 static int rebase_submodules(void)
591 struct child_process cp
= CHILD_PROCESS_INIT
;
595 argv_array_pushl(&cp
.args
, "submodule", "update",
596 "--recursive", "--rebase", NULL
);
597 argv_push_verbosity(&cp
.args
);
599 return run_command(&cp
);
602 static int update_submodules(void)
604 struct child_process cp
= CHILD_PROCESS_INIT
;
608 argv_array_pushl(&cp
.args
, "submodule", "update",
609 "--recursive", "--checkout", NULL
);
610 argv_push_verbosity(&cp
.args
);
612 return run_command(&cp
);
616 * Runs git-merge, returning its exit status.
618 static int run_merge(void)
621 struct argv_array args
= ARGV_ARRAY_INIT
;
623 argv_array_pushl(&args
, "merge", NULL
);
626 argv_push_verbosity(&args
);
628 argv_array_push(&args
, opt_progress
);
630 /* Options passed to git-merge */
632 argv_array_push(&args
, opt_diffstat
);
634 argv_array_push(&args
, opt_log
);
636 argv_array_push(&args
, opt_signoff
);
638 argv_array_push(&args
, opt_squash
);
640 argv_array_push(&args
, opt_commit
);
642 argv_array_push(&args
, opt_edit
);
644 argv_array_push(&args
, opt_ff
);
645 if (opt_verify_signatures
)
646 argv_array_push(&args
, opt_verify_signatures
);
647 argv_array_pushv(&args
, opt_strategies
.argv
);
648 argv_array_pushv(&args
, opt_strategy_opts
.argv
);
650 argv_array_push(&args
, opt_gpg_sign
);
651 if (opt_allow_unrelated_histories
> 0)
652 argv_array_push(&args
, "--allow-unrelated-histories");
654 argv_array_push(&args
, "FETCH_HEAD");
655 ret
= run_command_v_opt(args
.argv
, RUN_GIT_CMD
);
656 argv_array_clear(&args
);
661 * Returns remote's upstream branch for the current branch. If remote is NULL,
662 * the current branch's configured default remote is used. Returns NULL if
663 * `remote` does not name a valid remote, HEAD does not point to a branch,
664 * remote is not the branch's configured remote or the branch does not have any
665 * configured upstream branch.
667 static const char *get_upstream_branch(const char *remote
)
670 struct branch
*curr_branch
;
671 const char *curr_branch_remote
;
673 rm
= remote_get(remote
);
677 curr_branch
= branch_get("HEAD");
681 curr_branch_remote
= remote_for_branch(curr_branch
, NULL
);
682 assert(curr_branch_remote
);
684 if (strcmp(curr_branch_remote
, rm
->name
))
687 return branch_get_upstream(curr_branch
, NULL
);
691 * Derives the remote-tracking branch from the remote and refspec.
693 * FIXME: The current implementation assumes the default mapping of
694 * refs/heads/<branch_name> to refs/remotes/<remote_name>/<branch_name>.
696 static const char *get_tracking_branch(const char *remote
, const char *refspec
)
698 struct refspec_item spec
;
699 const char *spec_src
;
700 const char *merge_branch
;
702 refspec_item_init_or_die(&spec
, refspec
, REFSPEC_FETCH
);
704 if (!*spec_src
|| !strcmp(spec_src
, "HEAD"))
706 else if (skip_prefix(spec_src
, "heads/", &spec_src
))
708 else if (skip_prefix(spec_src
, "refs/heads/", &spec_src
))
710 else if (starts_with(spec_src
, "refs/") ||
711 starts_with(spec_src
, "tags/") ||
712 starts_with(spec_src
, "remotes/"))
716 if (!strcmp(remote
, "."))
717 merge_branch
= mkpath("refs/heads/%s", spec_src
);
719 merge_branch
= mkpath("refs/remotes/%s/%s", remote
, spec_src
);
723 refspec_item_clear(&spec
);
728 * Given the repo and refspecs, sets fork_point to the point at which the
729 * current branch forked from its remote-tracking branch. Returns 0 on success,
732 static int get_rebase_fork_point(struct object_id
*fork_point
, const char *repo
,
736 struct branch
*curr_branch
;
737 const char *remote_branch
;
738 struct child_process cp
= CHILD_PROCESS_INIT
;
739 struct strbuf sb
= STRBUF_INIT
;
741 curr_branch
= branch_get("HEAD");
746 remote_branch
= get_tracking_branch(repo
, refspec
);
748 remote_branch
= get_upstream_branch(repo
);
753 argv_array_pushl(&cp
.args
, "merge-base", "--fork-point",
754 remote_branch
, curr_branch
->name
, NULL
);
759 ret
= capture_command(&cp
, &sb
, GIT_SHA1_HEXSZ
);
763 ret
= get_oid_hex(sb
.buf
, fork_point
);
773 * Sets merge_base to the octopus merge base of curr_head, merge_head and
774 * fork_point. Returns 0 if a merge base is found, 1 otherwise.
776 static int get_octopus_merge_base(struct object_id
*merge_base
,
777 const struct object_id
*curr_head
,
778 const struct object_id
*merge_head
,
779 const struct object_id
*fork_point
)
781 struct commit_list
*revs
= NULL
, *result
;
783 commit_list_insert(lookup_commit_reference(the_repository
, curr_head
),
785 commit_list_insert(lookup_commit_reference(the_repository
, merge_head
),
787 if (!is_null_oid(fork_point
))
788 commit_list_insert(lookup_commit_reference(the_repository
, fork_point
),
791 result
= get_octopus_merge_bases(revs
);
792 free_commit_list(revs
);
793 reduce_heads_replace(&result
);
798 oidcpy(merge_base
, &result
->item
->object
.oid
);
799 free_commit_list(result
);
804 * Given the current HEAD SHA1, the merge head returned from git-fetch and the
805 * fork point calculated by get_rebase_fork_point(), runs git-rebase with the
806 * appropriate arguments and returns its exit status.
808 static int run_rebase(const struct object_id
*curr_head
,
809 const struct object_id
*merge_head
,
810 const struct object_id
*fork_point
)
813 struct object_id oct_merge_base
;
814 struct argv_array args
= ARGV_ARRAY_INIT
;
816 if (!get_octopus_merge_base(&oct_merge_base
, curr_head
, merge_head
, fork_point
))
817 if (!is_null_oid(fork_point
) && oideq(&oct_merge_base
, fork_point
))
820 argv_array_push(&args
, "rebase");
823 argv_push_verbosity(&args
);
825 /* Options passed to git-rebase */
826 if (opt_rebase
== REBASE_MERGES
)
827 argv_array_push(&args
, "--rebase-merges");
828 else if (opt_rebase
== REBASE_PRESERVE
)
829 argv_array_push(&args
, "--preserve-merges");
830 else if (opt_rebase
== REBASE_INTERACTIVE
)
831 argv_array_push(&args
, "--interactive");
833 argv_array_push(&args
, opt_diffstat
);
834 argv_array_pushv(&args
, opt_strategies
.argv
);
835 argv_array_pushv(&args
, opt_strategy_opts
.argv
);
837 argv_array_push(&args
, opt_gpg_sign
);
838 if (opt_autostash
== 0)
839 argv_array_push(&args
, "--no-autostash");
840 else if (opt_autostash
== 1)
841 argv_array_push(&args
, "--autostash");
842 if (opt_verify_signatures
&&
843 !strcmp(opt_verify_signatures
, "--verify-signatures"))
844 warning(_("ignoring --verify-signatures for rebase"));
846 argv_array_push(&args
, "--onto");
847 argv_array_push(&args
, oid_to_hex(merge_head
));
849 if (fork_point
&& !is_null_oid(fork_point
))
850 argv_array_push(&args
, oid_to_hex(fork_point
));
852 argv_array_push(&args
, oid_to_hex(merge_head
));
854 ret
= run_command_v_opt(args
.argv
, RUN_GIT_CMD
);
855 argv_array_clear(&args
);
859 int cmd_pull(int argc
, const char **argv
, const char *prefix
)
861 const char *repo
, **refspecs
;
862 struct oid_array merge_heads
= OID_ARRAY_INIT
;
863 struct object_id orig_head
, curr_head
;
864 struct object_id rebase_fork_point
;
867 if (!getenv("GIT_REFLOG_ACTION"))
868 set_reflog_message(argc
, argv
);
870 git_config(git_pull_config
, NULL
);
872 argc
= parse_options(argc
, argv
, prefix
, pull_options
, pull_usage
, 0);
874 parse_repo_refspecs(argc
, argv
, &repo
, &refspecs
);
877 opt_ff
= xstrdup_or_null(config_get_ff());
880 opt_rebase
= config_get_rebase();
882 if (read_cache_unmerged())
883 die_resolve_conflict("pull");
885 if (file_exists(git_path_merge_head(the_repository
)))
886 die_conclude_merge();
888 if (get_oid("HEAD", &orig_head
))
891 if (!opt_rebase
&& opt_autostash
!= -1)
892 die(_("--[no-]autostash option is only valid with --rebase."));
894 autostash
= config_autostash
;
896 if (opt_autostash
!= -1)
897 autostash
= opt_autostash
;
899 if (is_null_oid(&orig_head
) && !is_cache_unborn())
900 die(_("Updating an unborn branch with changes added to the index."));
903 require_clean_work_tree(the_repository
,
904 N_("pull with rebase"),
905 _("please commit or stash them."), 1, 0);
907 if (get_rebase_fork_point(&rebase_fork_point
, repo
, *refspecs
))
908 oidclr(&rebase_fork_point
);
911 if (run_fetch(repo
, refspecs
))
917 if (get_oid("HEAD", &curr_head
))
920 if (!is_null_oid(&orig_head
) && !is_null_oid(&curr_head
) &&
921 !oideq(&orig_head
, &curr_head
)) {
923 * The fetch involved updating the current branch.
925 * The working tree and the index file are still based on
926 * orig_head commit, but we are merging into curr_head.
927 * Update the working tree to match curr_head.
930 warning(_("fetch updated the current branch head.\n"
931 "fast-forwarding your working tree from\n"
932 "commit %s."), oid_to_hex(&orig_head
));
934 if (checkout_fast_forward(the_repository
, &orig_head
,
936 die(_("Cannot fast-forward your working tree.\n"
937 "After making sure that you saved anything precious from\n"
940 "$ git reset --hard\n"
941 "to recover."), oid_to_hex(&orig_head
));
944 get_merge_heads(&merge_heads
);
947 die_no_merge_candidates(repo
, refspecs
);
949 if (is_null_oid(&orig_head
)) {
950 if (merge_heads
.nr
> 1)
951 die(_("Cannot merge multiple branches into empty head."));
952 return pull_into_void(merge_heads
.oid
, &curr_head
);
954 if (opt_rebase
&& merge_heads
.nr
> 1)
955 die(_("Cannot rebase onto multiple branches."));
959 if ((recurse_submodules
== RECURSE_SUBMODULES_ON
||
960 recurse_submodules
== RECURSE_SUBMODULES_ON_DEMAND
) &&
961 submodule_touches_in_range(the_repository
, &rebase_fork_point
, &curr_head
))
962 die(_("cannot rebase with locally recorded submodule modifications"));
964 struct commit_list
*list
= NULL
;
965 struct commit
*merge_head
, *head
;
967 head
= lookup_commit_reference(the_repository
,
969 commit_list_insert(head
, &list
);
970 merge_head
= lookup_commit_reference(the_repository
,
971 &merge_heads
.oid
[0]);
972 if (is_descendant_of(merge_head
, list
)) {
973 /* we can fast-forward this without invoking rebase */
974 opt_ff
= "--ff-only";
978 ret
= run_rebase(&curr_head
, merge_heads
.oid
, &rebase_fork_point
);
980 if (!ret
&& (recurse_submodules
== RECURSE_SUBMODULES_ON
||
981 recurse_submodules
== RECURSE_SUBMODULES_ON_DEMAND
))
982 ret
= rebase_submodules();
986 int ret
= run_merge();
987 if (!ret
&& (recurse_submodules
== RECURSE_SUBMODULES_ON
||
988 recurse_submodules
== RECURSE_SUBMODULES_ON_DEMAND
))
989 ret
= update_submodules();