3 # Copyright (c) 2005 Junio C Hamano
5 # Fetch one or more remote refs and merge it/them into the current HEAD.
7 USAGE='[-n | --no-stat] [--[no-]commit] [--[no-]squash] [--[no-]ff] [-s strategy]... [<fetch-options>] <repo> <head>...'
8 LONG_USAGE='Fetch one or more remote refs and merge it/them into the current HEAD.'
12 set_reflog_action "pull $*"
16 test -z "$(git ls-files -u)" ||
17 die "You are in the middle of a conflicted merge."
19 strategy_args= no_stat= no_commit= squash= no_ff= log_arg=
20 curr_branch=$(git symbolic-ref -q HEAD)
21 curr_branch_short=$(echo "$curr_branch" | sed "s|refs/heads/||")
22 rebase=$(git config --bool branch.$curr_branch_short.rebase)
26 -n|--no-stat|--no-summary)
32 --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
33 no_commit=--no-commit ;;
34 --c|--co|--com|--comm|--commi|--commit)
36 --sq|--squ|--squa|--squas|--squash)
38 --no-sq|--no-squ|--no-squa|--no-squas|--no-squash)
44 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
45 --strateg=*|--strategy=*|\
46 -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
49 strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
56 strategy_args="${strategy_args}-s $strategy "
58 -r|--r|--re|--reb|--reba|--rebas|--rebase)
61 --no-r|--no-re|--no-reb|--no-reba|--no-rebas|--no-rebase)
64 -h|--h|--he|--hel|--help)
68 # Pass thru anything that may be meant for fetch.
75 error_on_no_merge_candidates () {
80 -t|--t|--ta|--tag|--tags)
81 echo "Fetching tags only, you probably meant:"
82 echo " git fetch --tags"
87 curr_branch=${curr_branch#refs/heads/}
89 echo "You asked me to pull without telling me which branch you"
90 echo "want to merge with, and 'branch.${curr_branch}.merge' in"
91 echo "your configuration file does not tell me either. Please"
92 echo "name which branch you want to merge on the command line and"
93 echo "try again (e.g. 'git pull <repository> <refspec>')."
94 echo "See git-pull(1) for details on the refspec."
96 echo "If you often merge with the same branch, you may want to"
97 echo "configure the following variables in your configuration"
100 echo " branch.${curr_branch}.remote = <nickname>"
101 echo " branch.${curr_branch}.merge = <remote-ref>"
102 echo " remote.<nickname>.url = <url>"
103 echo " remote.<nickname>.fetch = <refspec>"
105 echo "See git-config(1) for details."
109 test true = "$rebase" && {
110 git update-index --refresh &&
111 git diff-files --quiet &&
112 git diff-index --cached --quiet HEAD -- ||
113 die "refusing to pull with rebase: your working tree is not up-to-date"
115 . git-parse-remote &&
117 test -z "$origin" && origin=$(get_default_remote)
118 reflist="$(get_remote_refs_for_fetch "$@" 2>/dev/null |
119 sed "s|refs/heads/\(.*\):|\1|")" &&
120 oldremoteref="$(git rev-parse --verify \
121 "refs/remotes/$origin/$reflist" 2>/dev/null)"
123 orig_head=$(git rev-parse --verify HEAD 2>/dev/null)
124 git-fetch --update-head-ok "$@" || exit 1
126 curr_head=$(git rev-parse --verify HEAD 2>/dev/null)
127 if test "$curr_head" != "$orig_head"
129 # The fetch involved updating the current branch.
131 # The working tree and the index file is still based on the
132 # $orig_head commit, but we are merging into $curr_head.
133 # First update the working tree to match $curr_head.
135 echo >&2 "Warning: fetch updated the current branch head."
136 echo >&2 "Warning: fast forwarding your working tree from"
137 echo >&2 "Warning: commit $orig_head."
138 git update-index --refresh 2>/dev/null
139 git read-tree -u -m "$orig_head" "$curr_head" ||
140 die 'Cannot fast-forward your working tree.
141 After making sure that you saved anything precious from
142 $ git diff '$orig_head'
149 merge_head=$(sed -e '/ not-for-merge /d' \
150 -e 's/ .*//' "$GIT_DIR"/FETCH_HEAD | \
153 case "$merge_head" in
156 0) error_on_no_merge_candidates "$@";;
157 1) echo >&2 "You are not currently on a branch; you must explicitly"
158 echo >&2 "specify which branch you wish to merge:"
159 echo >&2 " git pull <remote> <branch>"
165 if test -z "$orig_head"
167 echo >&2 "Cannot merge multiple branches into empty head"
173 if test -z "$orig_head"
175 git update-ref -m "initial pull" HEAD $merge_head "" &&
176 git read-tree --reset -u HEAD || exit 1
180 merge_name=$(git fmt-merge-msg $log_arg <"$GIT_DIR/FETCH_HEAD") || exit
181 test true = "$rebase" &&
182 exec git-rebase $strategy_args --onto $merge_head \
183 ${oldremoteref:-$merge_head}
184 exec git-merge $no_stat $no_commit $squash $no_ff $log_arg $strategy_args \
185 "$merge_name" HEAD $merge_head