1#!/bin/sh
2#
3# Copyright (c) 2005 Junio C Hamano
4#
5# Fetch one or more remote refs and merge it/them into the current HEAD.
6
7USAGE='[-n | --no-stat] [--[no-]commit] [--[no-]squash] [--[no-]ff] [-s strategy]... [<fetch-options>] <repo> <head>...'
8LONG_USAGE='Fetch one or more remote refs and merge it/them into the current HEAD.'
9SUBDIRECTORY_OK=Yes
10OPTIONS_SPEC=
11. git-sh-setup
12set_reflog_action "pull $*"
13require_work_tree
14cd_to_toplevel
15
16
17die_conflict () {
18 git diff-index --cached --name-status -r --ignore-submodules HEAD --
19 if [ $(git config --bool --get advice.resolveConflict || echo true) = "true" ]; then
20 die "Pull is not possible because you have unmerged files.
21Please, fix them up in the work tree, and then use 'git add/rm <file>'
22as appropriate to mark resolution, or use 'git commit -a'."
23 else
24 die "Pull is not possible because you have unmerged files."
25 fi
26}
27
28die_merge () {
29 if [ $(git config --bool --get advice.resolveConflict || echo true) = "true" ]; then
30 die "You have not concluded your merge (MERGE_HEAD exists).
31Please, commit your changes before you can merge."
32 else
33 die "You have not concluded your merge (MERGE_HEAD exists)."
34 fi
35}
36
37test -z "$(git ls-files -u)" || die_conflict
38test -f "$GIT_DIR/MERGE_HEAD" && die_merge
39
40strategy_args= diffstat= no_commit= squash= no_ff= ff_only=
41log_arg= verbosity= progress= recurse_submodules=
42merge_args=
43curr_branch=$(git symbolic-ref -q HEAD)
44curr_branch_short="${curr_branch#refs/heads/}"
45rebase=$(git config --bool branch.$curr_branch_short.rebase)
46dry_run=
47while :
48do
49 case "$1" in
50 -q|--quiet)
51 verbosity="$verbosity -q" ;;
52 -v|--verbose)
53 verbosity="$verbosity -v" ;;
54 --progress)
55 progress=--progress ;;
56 -n|--no-stat|--no-summary)
57 diffstat=--no-stat ;;
58 --stat|--summary)
59 diffstat=--stat ;;
60 --log|--no-log)
61 log_arg=$1 ;;
62 --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
63 no_commit=--no-commit ;;
64 --c|--co|--com|--comm|--commi|--commit)
65 no_commit=--commit ;;
66 --sq|--squ|--squa|--squas|--squash)
67 squash=--squash ;;
68 --no-sq|--no-squ|--no-squa|--no-squas|--no-squash)
69 squash=--no-squash ;;
70 --ff)
71 no_ff=--ff ;;
72 --no-ff)
73 no_ff=--no-ff ;;
74 --ff-only)
75 ff_only=--ff-only ;;
76 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
77 --strateg=*|--strategy=*|\
78 -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
79 case "$#,$1" in
80 *,*=*)
81 strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
82 1,*)
83 usage ;;
84 *)
85 strategy="$2"
86 shift ;;
87 esac
88 strategy_args="${strategy_args}-s $strategy "
89 ;;
90 -X*)
91 case "$#,$1" in
92 1,-X)
93 usage ;;
94 *,-X)
95 xx="-X $(git rev-parse --sq-quote "$2")"
96 shift ;;
97 *,*)
98 xx=$(git rev-parse --sq-quote "$1") ;;
99 esac
100 merge_args="$merge_args$xx "
101 ;;
102 -r|--r|--re|--reb|--reba|--rebas|--rebase)
103 rebase=true
104 ;;
105 --no-r|--no-re|--no-reb|--no-reba|--no-rebas|--no-rebase)
106 rebase=false
107 ;;
108 --recurse-submodules)
109 recurse_submodules=--recurse-submodules
110 ;;
111 --no-recurse-submodules)
112 recurse_submodules=--no-recurse-submodules
113 ;;
114 --d|--dr|--dry|--dry-|--dry-r|--dry-ru|--dry-run)
115 dry_run=--dry-run
116 ;;
117 -h|--h|--he|--hel|--help)
118 usage
119 ;;
120 *)
121 # Pass thru anything that may be meant for fetch.
122 break
123 ;;
124 esac
125 shift
126done
127
128error_on_no_merge_candidates () {
129 exec >&2
130 for opt
131 do
132 case "$opt" in
133 -t|--t|--ta|--tag|--tags)
134 echo "Fetching tags only, you probably meant:"
135 echo " git fetch --tags"
136 exit 1
137 esac
138 done
139
140 if test true = "$rebase"
141 then
142 op_type=rebase
143 op_prep=against
144 else
145 op_type=merge
146 op_prep=with
147 fi
148
149 curr_branch=${curr_branch#refs/heads/}
150 upstream=$(git config "branch.$curr_branch.merge")
151 remote=$(git config "branch.$curr_branch.remote")
152
153 if [ $# -gt 1 ]; then
154 if [ "$rebase" = true ]; then
155 printf "There is no candidate for rebasing against "
156 else
157 printf "There are no candidates for merging "
158 fi
159 echo "among the refs that you just fetched."
160 echo "Generally this means that you provided a wildcard refspec which had no"
161 echo "matches on the remote end."
162 elif [ $# -gt 0 ] && [ "$1" != "$remote" ]; then
163 echo "You asked to pull from the remote '$1', but did not specify"
164 echo "a branch. Because this is not the default configured remote"
165 echo "for your current branch, you must specify a branch on the command line."
166 elif [ -z "$curr_branch" -o -z "$upstream" ]; then
167 . git-parse-remote
168 error_on_missing_default_upstream "pull" $op_type $op_prep \
169 "git pull <repository> <refspec>"
170 else
171 echo "Your configuration specifies to $op_type $op_prep the ref '${upstream#refs/heads/}'"
172 echo "from the remote, but no such ref was fetched."
173 fi
174 exit 1
175}
176
177test true = "$rebase" && {
178 if ! git rev-parse -q --verify HEAD >/dev/null
179 then
180 # On an unborn branch
181 if test -f "$GIT_DIR/index"
182 then
183 die "updating an unborn branch with changes added to the index"
184 fi
185 else
186 require_clean_work_tree "pull with rebase" "Please commit or stash them."
187 fi
188 oldremoteref= &&
189 . git-parse-remote &&
190 remoteref="$(get_remote_merge_branch "$@" 2>/dev/null)" &&
191 oldremoteref="$(git rev-parse -q --verify "$remoteref")" &&
192 for reflog in $(git rev-list -g $remoteref 2>/dev/null)
193 do
194 if test "$reflog" = "$(git merge-base $reflog $curr_branch)"
195 then
196 oldremoteref="$reflog"
197 break
198 fi
199 done
200}
201orig_head=$(git rev-parse -q --verify HEAD)
202git fetch $verbosity $progress $dry_run $recurse_submodules --update-head-ok "$@" || exit 1
203test -z "$dry_run" || exit 0
204
205curr_head=$(git rev-parse -q --verify HEAD)
206if test -n "$orig_head" && test "$curr_head" != "$orig_head"
207then
208 # The fetch involved updating the current branch.
209
210 # The working tree and the index file is still based on the
211 # $orig_head commit, but we are merging into $curr_head.
212 # First update the working tree to match $curr_head.
213
214 echo >&2 "Warning: fetch updated the current branch head."
215 echo >&2 "Warning: fast-forwarding your working tree from"
216 echo >&2 "Warning: commit $orig_head."
217 git update-index -q --refresh
218 git read-tree -u -m "$orig_head" "$curr_head" ||
219 die 'Cannot fast-forward your working tree.
220After making sure that you saved anything precious from
221$ git diff '$orig_head'
222output, run
223$ git reset --hard
224to recover.'
225
226fi
227
228merge_head=$(sed -e '/ not-for-merge /d' \
229 -e 's/ .*//' "$GIT_DIR"/FETCH_HEAD | \
230 tr '\012' ' ')
231
232case "$merge_head" in
233'')
234 error_on_no_merge_candidates "$@"
235 ;;
236?*' '?*)
237 if test -z "$orig_head"
238 then
239 die "Cannot merge multiple branches into empty head"
240 fi
241 if test true = "$rebase"
242 then
243 die "Cannot rebase onto multiple branches"
244 fi
245 ;;
246esac
247
248if test -z "$orig_head"
249then
250 git update-ref -m "initial pull" HEAD $merge_head "$curr_head" &&
251 git read-tree --reset -u HEAD || exit 1
252 exit
253fi
254
255if test true = "$rebase"
256then
257 o=$(git show-branch --merge-base $curr_branch $merge_head $oldremoteref)
258 if test "$oldremoteref" = "$o"
259 then
260 unset oldremoteref
261 fi
262fi
263
264merge_name=$(git fmt-merge-msg $log_arg <"$GIT_DIR/FETCH_HEAD") || exit
265case "$rebase" in
266true)
267 eval="git-rebase $diffstat $strategy_args $merge_args"
268 eval="$eval --onto $merge_head ${oldremoteref:-$merge_head}"
269 ;;
270*)
271 eval="git-merge $diffstat $no_commit $squash $no_ff $ff_only"
272 eval="$eval $log_arg $strategy_args $merge_args"
273 eval="$eval \"\$merge_name\" HEAD $merge_head $verbosity"
274 ;;
275esac
276eval "exec $eval"