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