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