Merge branch 'da/maint-mergetool-twoway' into maint
[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 . git-sh-i18n
13 set_reflog_action "pull${1+ $*}"
14 require_work_tree_exists
15 cd_to_toplevel
16
17
18 die_conflict () {
19     git diff-index --cached --name-status -r --ignore-submodules HEAD --
20     if [ $(git config --bool --get advice.resolveConflict || echo true) = "true" ]; then
21         die "$(gettext "Pull is not possible because you have unmerged files.
22 Please, fix them up in the work tree, and then use 'git add/rm <file>'
23 as appropriate to mark resolution, or use 'git commit -a'.")"
24     else
25         die "$(gettext "Pull is not possible because you have unmerged files.")"
26     fi
27 }
28
29 die_merge () {
30     if [ $(git config --bool --get advice.resolveConflict || echo true) = "true" ]; then
31         die "$(gettext "You have not concluded your merge (MERGE_HEAD exists).
32 Please, commit your changes before you can merge.")"
33     else
34         die "$(gettext "You have not concluded your merge (MERGE_HEAD exists).")"
35     fi
36 }
37
38 test -z "$(git ls-files -u)" || die_conflict
39 test -f "$GIT_DIR/MERGE_HEAD" && die_merge
40
41 strategy_args= diffstat= no_commit= squash= no_ff= ff_only=
42 log_arg= verbosity= progress= recurse_submodules=
43 merge_args=
44 curr_branch=$(git symbolic-ref -q HEAD)
45 curr_branch_short="${curr_branch#refs/heads/}"
46 rebase=$(git config --bool branch.$curr_branch_short.rebase)
47 if test -z "$rebase"
48 then
49         rebase=$(git config --bool pull.rebase)
50 fi
51 dry_run=
52 while :
53 do
54         case "$1" in
55         -q|--quiet)
56                 verbosity="$verbosity -q" ;;
57         -v|--verbose)
58                 verbosity="$verbosity -v" ;;
59         --progress)
60                 progress=--progress ;;
61         --no-progress)
62                 progress=--no-progress ;;
63         -n|--no-stat|--no-summary)
64                 diffstat=--no-stat ;;
65         --stat|--summary)
66                 diffstat=--stat ;;
67         --log|--no-log)
68                 log_arg=$1 ;;
69         --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
70                 no_commit=--no-commit ;;
71         --c|--co|--com|--comm|--commi|--commit)
72                 no_commit=--commit ;;
73         --sq|--squ|--squa|--squas|--squash)
74                 squash=--squash ;;
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 ;;
81         --ff-only)
82                 ff_only=--ff-only ;;
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                 *,*=*)
88                         strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
89                 1,*)
90                         usage ;;
91                 *)
92                         strategy="$2"
93                         shift ;;
94                 esac
95                 strategy_args="${strategy_args}-s $strategy "
96                 ;;
97         -X*)
98                 case "$#,$1" in
99                 1,-X)
100                         usage ;;
101                 *,-X)
102                         xx="-X $(git rev-parse --sq-quote "$2")"
103                         shift ;;
104                 *,*)
105                         xx=$(git rev-parse --sq-quote "$1") ;;
106                 esac
107                 merge_args="$merge_args$xx "
108                 ;;
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                 ;;
115         --recurse-submodules)
116                 recurse_submodules=--recurse-submodules
117                 ;;
118         --recurse-submodules=*)
119                 recurse_submodules="$1"
120                 ;;
121         --no-recurse-submodules)
122                 recurse_submodules=--no-recurse-submodules
123                 ;;
124         --d|--dr|--dry|--dry-|--dry-r|--dry-ru|--dry-run)
125                 dry_run=--dry-run
126                 ;;
127         -h|--help-all)
128                 usage
129                 ;;
130         *)
131                 # Pass thru anything that may be meant for fetch.
132                 break
133                 ;;
134         esac
135         shift
136 done
137
138 error_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
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
159         curr_branch=${curr_branch#refs/heads/}
160         upstream=$(git config "branch.$curr_branch.merge")
161         remote=$(git config "branch.$curr_branch.remote")
162
163         if [ $# -gt 1 ]; then
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."
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"
174                 echo "a branch. Because this is not the default configured remote"
175                 echo "for your current branch, you must specify a branch on the command line."
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>"
180         else
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."
183         fi
184         exit 1
185 }
186
187 test true = "$rebase" && {
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
193                         die "$(gettext "updating an unborn branch with changes added to the index")"
194                 fi
195         else
196                 require_clean_work_tree "pull with rebase" "Please commit or stash them."
197         fi
198         oldremoteref= &&
199         . git-parse-remote &&
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
210 }
211 orig_head=$(git rev-parse -q --verify HEAD)
212 git fetch $verbosity $progress $dry_run $recurse_submodules --update-head-ok "$@" || exit 1
213 test -z "$dry_run" || exit 0
214
215 curr_head=$(git rev-parse -q --verify HEAD)
216 if test -n "$orig_head" && test "$curr_head" != "$orig_head"
217 then
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
224         eval_gettextln "Warning: fetch updated the current branch head.
225 Warning: fast-forwarding your working tree from
226 Warning: commit \$orig_head." >&2
227         git update-index -q --refresh
228         git read-tree -u -m "$orig_head" "$curr_head" ||
229                 die "$(eval_gettext "Cannot fast-forward your working tree.
230 After making sure that you saved anything precious from
231 $ git diff \$orig_head
232 output, run
233 $ git reset --hard
234 to recover.")"
235
236 fi
237
238 merge_head=$(sed -e '/  not-for-merge   /d' \
239         -e 's/  .*//' "$GIT_DIR"/FETCH_HEAD | \
240         tr '\012' ' ')
241
242 case "$merge_head" in
243 '')
244         error_on_no_merge_candidates "$@"
245         ;;
246 ?*' '?*)
247         if test -z "$orig_head"
248         then
249                 die "$(gettext "Cannot merge multiple branches into empty head")"
250         fi
251         if test true = "$rebase"
252         then
253                 die "$(gettext "Cannot rebase onto multiple branches")"
254         fi
255         ;;
256 esac
257
258 if test -z "$orig_head"
259 then
260         git update-ref -m "initial pull" HEAD $merge_head "$curr_head" &&
261         git read-tree -m -u HEAD || exit 1
262         exit
263 fi
264
265 if test true = "$rebase"
266 then
267         o=$(git show-branch --merge-base $curr_branch $merge_head $oldremoteref)
268         if test "$oldremoteref" = "$o"
269         then
270                 unset oldremoteref
271         fi
272 fi
273
274 merge_name=$(git fmt-merge-msg $log_arg <"$GIT_DIR/FETCH_HEAD") || exit
275 case "$rebase" in
276 true)
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"
282         eval="$eval  $log_arg $strategy_args $merge_args $verbosity $progress"
283         eval="$eval \"\$merge_name\" HEAD $merge_head"
284         ;;
285 esac
286 eval "exec $eval"