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