]> git.ipfire.org Git - thirdparty/git.git/blame - git-pull.sh
Merge branch 'sb/notes-parse-opt'
[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
f9474132 12set_reflog_action "pull $*"
7eff28a9 13require_work_tree
533b7039 14cd_to_toplevel
b10ac50f 15
d38a30df
MM
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
d1014a17 39
13474835 40strategy_args= diffstat= no_commit= squash= no_ff= ff_only=
9839018e 41log_arg= verbosity= progress=
ee2c7955 42merge_args=
cd67e4d4 43curr_branch=$(git symbolic-ref -q HEAD)
0d12e59f 44curr_branch_short="${curr_branch#refs/heads/}"
cd67e4d4 45rebase=$(git config --bool branch.$curr_branch_short.rebase)
822f7c73 46while :
60fb5b2c
JH
47do
48 case "$1" in
7f87aff2 49 -q|--quiet)
c6576f91 50 verbosity="$verbosity -q" ;;
7f87aff2 51 -v|--verbose)
c6576f91 52 verbosity="$verbosity -v" ;;
9839018e
TRC
53 --progress)
54 progress=--progress ;;
d8abe148 55 -n|--no-stat|--no-summary)
a334e125 56 diffstat=--no-stat ;;
d8abe148 57 --stat|--summary)
a334e125 58 diffstat=--stat ;;
efb779f8
SG
59 --log|--no-log)
60 log_arg=$1 ;;
123ee3ca
JH
61 --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
62 no_commit=--no-commit ;;
5072a323
LH
63 --c|--co|--com|--comm|--commi|--commit)
64 no_commit=--commit ;;
7d0c6887
JH
65 --sq|--squ|--squa|--squas|--squash)
66 squash=--squash ;;
5072a323
LH
67 --no-sq|--no-squ|--no-squa|--no-squas|--no-squash)
68 squash=--no-squash ;;
69 --ff)
70 no_ff=--ff ;;
71 --no-ff)
72 no_ff=--no-ff ;;
13474835
BG
73 --ff-only)
74 ff_only=--ff-only ;;
60fb5b2c
JH
75 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
76 --strateg=*|--strategy=*|\
77 -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
78 case "$#,$1" in
79 *,*=*)
8096fae7 80 strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
60fb5b2c
JH
81 1,*)
82 usage ;;
83 *)
84 strategy="$2"
85 shift ;;
86 esac
87 strategy_args="${strategy_args}-s $strategy "
88 ;;
ee2c7955
AP
89 -X*)
90 case "$#,$1" in
91 1,-X)
92 usage ;;
93 *,-X)
14e5d40c 94 xx="-X $(git rev-parse --sq-quote "$2")"
ee2c7955
AP
95 shift ;;
96 *,*)
14e5d40c 97 xx=$(git rev-parse --sq-quote "$1") ;;
ee2c7955
AP
98 esac
99 merge_args="$merge_args$xx "
100 ;;
cd67e4d4
JS
101 -r|--r|--re|--reb|--reba|--rebas|--rebase)
102 rebase=true
103 ;;
104 --no-r|--no-re|--no-reb|--no-reba|--no-rebas|--no-rebase)
105 rebase=false
106 ;;
93d69d86
JL
107 -h|--h|--he|--hel|--help)
108 usage
109 ;;
822f7c73
DK
110 *)
111 # Pass thru anything that may be meant for fetch.
619e5a0e 112 break
60fb5b2c
JH
113 ;;
114 esac
115 shift
116done
117
441ed413
JH
118error_on_no_merge_candidates () {
119 exec >&2
120 for opt
121 do
122 case "$opt" in
123 -t|--t|--ta|--tag|--tags)
124 echo "Fetching tags only, you probably meant:"
125 echo " git fetch --tags"
126 exit 1
127 esac
128 done
129
995fc2f7
JK
130 if test true = "$rebase"
131 then
132 op_type=rebase
133 op_prep=against
134 else
135 op_type=merge
136 op_prep=with
137 fi
138
441ed413 139 curr_branch=${curr_branch#refs/heads/}
a6dbf881 140 upstream=$(git config "branch.$curr_branch.merge")
a8c9bef4 141 remote=$(git config "branch.$curr_branch.remote")
441ed413 142
a8c9bef4 143 if [ $# -gt 1 ]; then
995fc2f7
JK
144 if [ "$rebase" = true ]; then
145 printf "There is no candidate for rebasing against "
146 else
147 printf "There are no candidates for merging "
148 fi
149 echo "among the refs that you just fetched."
a8c9bef4
JK
150 echo "Generally this means that you provided a wildcard refspec which had no"
151 echo "matches on the remote end."
152 elif [ $# -gt 0 ] && [ "$1" != "$remote" ]; then
153 echo "You asked to pull from the remote '$1', but did not specify"
995fc2f7 154 echo "a branch. Because this is not the default configured remote"
a8c9bef4
JK
155 echo "for your current branch, you must specify a branch on the command line."
156 elif [ -z "$curr_branch" ]; then
61e6108d
MM
157 echo "You are not currently on a branch, so I cannot use any"
158 echo "'branch.<branchname>.merge' in your configuration file."
995fc2f7 159 echo "Please specify which remote branch you want to use on the command"
61e6108d
MM
160 echo "line and try again (e.g. 'git pull <repository> <refspec>')."
161 echo "See git-pull(1) for details."
a6dbf881 162 elif [ -z "$upstream" ]; then
61e6108d 163 echo "You asked me to pull without telling me which branch you"
995fc2f7
JK
164 echo "want to $op_type $op_prep, and 'branch.${curr_branch}.merge' in"
165 echo "your configuration file does not tell me, either. Please"
166 echo "specify which branch you want to use on the command line and"
61e6108d
MM
167 echo "try again (e.g. 'git pull <repository> <refspec>')."
168 echo "See git-pull(1) for details."
169 echo
995fc2f7
JK
170 echo "If you often $op_type $op_prep the same branch, you may want to"
171 echo "use something like the following in your configuration file:"
172 echo
173 echo " [branch \"${curr_branch}\"]"
174 echo " remote = <nickname>"
175 echo " merge = <remote-ref>"
176 test rebase = "$op_type" &&
177 echo " rebase = true"
61e6108d 178 echo
995fc2f7
JK
179 echo " [remote \"<nickname>\"]"
180 echo " url = <url>"
181 echo " fetch = <refspec>"
61e6108d
MM
182 echo
183 echo "See git-config(1) for details."
a6dbf881 184 else
995fc2f7
JK
185 echo "Your configuration specifies to $op_type $op_prep the ref '${upstream#refs/heads/}'"
186 echo "from the remote, but no such ref was fetched."
61e6108d 187 fi
441ed413
JH
188 exit 1
189}
190
c85c7927 191test true = "$rebase" && {
19a7fcbf
JK
192 if ! git rev-parse -q --verify HEAD >/dev/null
193 then
194 # On an unborn branch
195 if test -f "$GIT_DIR/index"
196 then
197 die "updating an unborn branch with changes added to the index"
198 fi
199 else
200 git update-index --ignore-submodules --refresh &&
201 git diff-files --ignore-submodules --quiet &&
202 git diff-index --ignore-submodules --cached --quiet HEAD -- ||
203 die "refusing to pull with rebase: your working tree is not up-to-date"
204 fi
d44e7126 205 oldremoteref= &&
c85c7927 206 . git-parse-remote &&
d44e7126
SB
207 remoteref="$(get_remote_merge_branch "$@" 2>/dev/null)" &&
208 oldremoteref="$(git rev-parse -q --verify "$remoteref")" &&
209 for reflog in $(git rev-list -g $remoteref 2>/dev/null)
210 do
211 if test "$reflog" = "$(git merge-base $reflog $curr_branch)"
212 then
213 oldremoteref="$reflog"
214 break
215 fi
216 done
c85c7927 217}
2d179857 218orig_head=$(git rev-parse -q --verify HEAD)
9839018e 219git fetch $verbosity $progress --update-head-ok "$@" || exit 1
b10ac50f 220
2d179857 221curr_head=$(git rev-parse -q --verify HEAD)
b0ad11ea 222if test -n "$orig_head" && test "$curr_head" != "$orig_head"
b10ac50f
JH
223then
224 # The fetch involved updating the current branch.
225
226 # The working tree and the index file is still based on the
227 # $orig_head commit, but we are merging into $curr_head.
228 # First update the working tree to match $curr_head.
229
230 echo >&2 "Warning: fetch updated the current branch head."
a75d7b54 231 echo >&2 "Warning: fast-forwarding your working tree from"
a057f806 232 echo >&2 "Warning: commit $orig_head."
7bd93c1c 233 git update-index -q --refresh
5be60078 234 git read-tree -u -m "$orig_head" "$curr_head" ||
8323124a
JH
235 die 'Cannot fast-forward your working tree.
236After making sure that you saved anything precious from
237$ git diff '$orig_head'
238output, run
239$ git reset --hard
240to recover.'
241
b10ac50f
JH
242fi
243
05dd8e2e
JH
244merge_head=$(sed -e '/ not-for-merge /d' \
245 -e 's/ .*//' "$GIT_DIR"/FETCH_HEAD | \
246 tr '\012' ' ')
e0bfc81e
JH
247
248case "$merge_head" in
521003ff 249'')
4973aa22 250 error_on_no_merge_candidates "$@"
521003ff 251 ;;
60fb5b2c 252?*' '?*)
d09e79cb
LT
253 if test -z "$orig_head"
254 then
bc2bbc45 255 die "Cannot merge multiple branches into empty head"
d09e79cb 256 fi
51b2ead0
JS
257 if test true = "$rebase"
258 then
bc2bbc45 259 die "Cannot rebase onto multiple branches"
51b2ead0 260 fi
60fb5b2c
JH
261 ;;
262esac
263
d09e79cb
LT
264if test -z "$orig_head"
265then
b0ad11ea 266 git update-ref -m "initial pull" HEAD $merge_head "$curr_head" &&
5be60078 267 git read-tree --reset -u HEAD || exit 1
d09e79cb
LT
268 exit
269fi
270
efb779f8 271merge_name=$(git fmt-merge-msg $log_arg <"$GIT_DIR/FETCH_HEAD") || exit
14e5d40c
JH
272case "$rebase" in
273true)
274 eval="git-rebase $diffstat $strategy_args $merge_args"
275 eval="$eval --onto $merge_head ${oldremoteref:-$merge_head}"
276 ;;
277*)
278 eval="git-merge $diffstat $no_commit $squash $no_ff $ff_only"
279 eval="$eval $log_arg $strategy_args $merge_args"
7ecee331 280 eval="$eval \"\$merge_name\" HEAD $merge_head $verbosity"
14e5d40c
JH
281 ;;
282esac
283eval "exec $eval"