]> git.ipfire.org Git - thirdparty/git.git/blame - git-pull.sh
config: drop cf validity check in get_next_char()
[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=
efed0022 42log_arg= verbosity= progress= recurse_submodules= verify_signatures=
85808300 43merge_args= edit=
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 ;;
85808300
LT
73 -e|--edit)
74 edit=--edit ;;
75 --no-edit)
76 edit=--no-edit ;;
7d0c6887
JH
77 --sq|--squ|--squa|--squas|--squash)
78 squash=--squash ;;
5072a323
LH
79 --no-sq|--no-squ|--no-squa|--no-squas|--no-squash)
80 squash=--no-squash ;;
81 --ff)
82 no_ff=--ff ;;
83 --no-ff)
84 no_ff=--no-ff ;;
13474835
BG
85 --ff-only)
86 ff_only=--ff-only ;;
60fb5b2c
JH
87 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
88 --strateg=*|--strategy=*|\
89 -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
90 case "$#,$1" in
91 *,*=*)
8096fae7 92 strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
60fb5b2c
JH
93 1,*)
94 usage ;;
95 *)
96 strategy="$2"
97 shift ;;
98 esac
99 strategy_args="${strategy_args}-s $strategy "
100 ;;
ee2c7955
AP
101 -X*)
102 case "$#,$1" in
103 1,-X)
104 usage ;;
105 *,-X)
14e5d40c 106 xx="-X $(git rev-parse --sq-quote "$2")"
ee2c7955
AP
107 shift ;;
108 *,*)
14e5d40c 109 xx=$(git rev-parse --sq-quote "$1") ;;
ee2c7955
AP
110 esac
111 merge_args="$merge_args$xx "
112 ;;
cd67e4d4
JS
113 -r|--r|--re|--reb|--reba|--rebas|--rebase)
114 rebase=true
115 ;;
116 --no-r|--no-re|--no-reb|--no-reba|--no-rebas|--no-rebase)
117 rebase=false
118 ;;
7dce19d3
JL
119 --recurse-submodules)
120 recurse_submodules=--recurse-submodules
121 ;;
8f0700dd
JL
122 --recurse-submodules=*)
123 recurse_submodules="$1"
124 ;;
be254a0e
JL
125 --no-recurse-submodules)
126 recurse_submodules=--no-recurse-submodules
127 ;;
efed0022
SG
128 --verify-signatures)
129 verify_signatures=--verify-signatures
130 ;;
131 --no-verify-signatures)
132 verify_signatures=--no-verify-signatures
133 ;;
29609e68
JK
134 --d|--dr|--dry|--dry-|--dry-r|--dry-ru|--dry-run)
135 dry_run=--dry-run
136 ;;
87182b17 137 -h|--help-all)
93d69d86
JL
138 usage
139 ;;
822f7c73
DK
140 *)
141 # Pass thru anything that may be meant for fetch.
619e5a0e 142 break
60fb5b2c
JH
143 ;;
144 esac
145 shift
146done
147
441ed413
JH
148error_on_no_merge_candidates () {
149 exec >&2
150 for opt
151 do
152 case "$opt" in
153 -t|--t|--ta|--tag|--tags)
154 echo "Fetching tags only, you probably meant:"
155 echo " git fetch --tags"
156 exit 1
157 esac
158 done
159
995fc2f7
JK
160 if test true = "$rebase"
161 then
162 op_type=rebase
163 op_prep=against
164 else
165 op_type=merge
166 op_prep=with
167 fi
168
441ed413 169 curr_branch=${curr_branch#refs/heads/}
a6dbf881 170 upstream=$(git config "branch.$curr_branch.merge")
a8c9bef4 171 remote=$(git config "branch.$curr_branch.remote")
441ed413 172
a8c9bef4 173 if [ $# -gt 1 ]; then
995fc2f7
JK
174 if [ "$rebase" = true ]; then
175 printf "There is no candidate for rebasing against "
176 else
177 printf "There are no candidates for merging "
178 fi
179 echo "among the refs that you just fetched."
a8c9bef4
JK
180 echo "Generally this means that you provided a wildcard refspec which had no"
181 echo "matches on the remote end."
182 elif [ $# -gt 0 ] && [ "$1" != "$remote" ]; then
183 echo "You asked to pull from the remote '$1', but did not specify"
995fc2f7 184 echo "a branch. Because this is not the default configured remote"
a8c9bef4 185 echo "for your current branch, you must specify a branch on the command line."
15a147e6
MZ
186 elif [ -z "$curr_branch" -o -z "$upstream" ]; then
187 . git-parse-remote
188 error_on_missing_default_upstream "pull" $op_type $op_prep \
3c02396a 189 "git pull <remote> <branch>"
a6dbf881 190 else
995fc2f7
JK
191 echo "Your configuration specifies to $op_type $op_prep the ref '${upstream#refs/heads/}'"
192 echo "from the remote, but no such ref was fetched."
61e6108d 193 fi
441ed413
JH
194 exit 1
195}
196
c85c7927 197test true = "$rebase" && {
19a7fcbf
JK
198 if ! git rev-parse -q --verify HEAD >/dev/null
199 then
200 # On an unborn branch
201 if test -f "$GIT_DIR/index"
202 then
85af5f82 203 die "$(gettext "updating an unborn branch with changes added to the index")"
19a7fcbf
JK
204 fi
205 else
92c62a3f 206 require_clean_work_tree "pull with rebase" "Please commit or stash them."
19a7fcbf 207 fi
d44e7126 208 oldremoteref= &&
e980765c 209 test -n "$curr_branch" &&
c85c7927 210 . git-parse-remote &&
d44e7126
SB
211 remoteref="$(get_remote_merge_branch "$@" 2>/dev/null)" &&
212 oldremoteref="$(git rev-parse -q --verify "$remoteref")" &&
213 for reflog in $(git rev-list -g $remoteref 2>/dev/null)
214 do
215 if test "$reflog" = "$(git merge-base $reflog $curr_branch)"
216 then
217 oldremoteref="$reflog"
218 break
219 fi
220 done
c85c7927 221}
2d179857 222orig_head=$(git rev-parse -q --verify HEAD)
7dce19d3 223git fetch $verbosity $progress $dry_run $recurse_submodules --update-head-ok "$@" || exit 1
29609e68 224test -z "$dry_run" || exit 0
b10ac50f 225
2d179857 226curr_head=$(git rev-parse -q --verify HEAD)
b0ad11ea 227if test -n "$orig_head" && test "$curr_head" != "$orig_head"
b10ac50f
JH
228then
229 # The fetch involved updating the current branch.
230
231 # The working tree and the index file is still based on the
232 # $orig_head commit, but we are merging into $curr_head.
233 # First update the working tree to match $curr_head.
234
c2b1a95c 235 eval_gettextln "Warning: fetch updated the current branch head.
3320a973 236Warning: fast-forwarding your working tree from
c2b1a95c 237Warning: commit \$orig_head." >&2
7bd93c1c 238 git update-index -q --refresh
5be60078 239 git read-tree -u -m "$orig_head" "$curr_head" ||
9f35aaa9 240 die "$(eval_gettext "Cannot fast-forward your working tree.
8323124a 241After making sure that you saved anything precious from
9f35aaa9 242$ git diff \$orig_head
8323124a
JH
243output, run
244$ git reset --hard
9f35aaa9 245to recover.")"
8323124a 246
b10ac50f
JH
247fi
248
05dd8e2e
JH
249merge_head=$(sed -e '/ not-for-merge /d' \
250 -e 's/ .*//' "$GIT_DIR"/FETCH_HEAD | \
251 tr '\012' ' ')
e0bfc81e
JH
252
253case "$merge_head" in
521003ff 254'')
4973aa22 255 error_on_no_merge_candidates "$@"
521003ff 256 ;;
60fb5b2c 257?*' '?*)
d09e79cb
LT
258 if test -z "$orig_head"
259 then
85af5f82 260 die "$(gettext "Cannot merge multiple branches into empty head")"
d09e79cb 261 fi
51b2ead0
JS
262 if test true = "$rebase"
263 then
85af5f82 264 die "$(gettext "Cannot rebase onto multiple branches")"
51b2ead0 265 fi
60fb5b2c
JH
266 ;;
267esac
268
d09e79cb
LT
269if test -z "$orig_head"
270then
b0ad11ea 271 git update-ref -m "initial pull" HEAD $merge_head "$curr_head" &&
4b3ffe51 272 git read-tree -m -u HEAD || exit 1
d09e79cb
LT
273 exit
274fi
275
cf65426d
EN
276if test true = "$rebase"
277then
278 o=$(git show-branch --merge-base $curr_branch $merge_head $oldremoteref)
279 if test "$oldremoteref" = "$o"
280 then
281 unset oldremoteref
282 fi
283fi
284
efb779f8 285merge_name=$(git fmt-merge-msg $log_arg <"$GIT_DIR/FETCH_HEAD") || exit
14e5d40c
JH
286case "$rebase" in
287true)
ce4c4d4e 288 eval="git-rebase $diffstat $strategy_args $merge_args $verbosity"
14e5d40c
JH
289 eval="$eval --onto $merge_head ${oldremoteref:-$merge_head}"
290 ;;
291*)
efed0022 292 eval="git-merge $diffstat $no_commit $verify_signatures $edit $squash $no_ff $ff_only"
bebd2fd7
JK
293 eval="$eval $log_arg $strategy_args $merge_args $verbosity $progress"
294 eval="$eval \"\$merge_name\" HEAD $merge_head"
14e5d40c
JH
295 ;;
296esac
297eval "exec $eval"