sha1_name: refactor reinterpret()
[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= verify_signatures=
43 merge_args= edit=
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         -e|--edit)
74                 edit=--edit ;;
75         --no-edit)
76                 edit=--no-edit ;;
77         --sq|--squ|--squa|--squas|--squash)
78                 squash=--squash ;;
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 ;;
85         --ff-only)
86                 ff_only=--ff-only ;;
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                 *,*=*)
92                         strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
93                 1,*)
94                         usage ;;
95                 *)
96                         strategy="$2"
97                         shift ;;
98                 esac
99                 strategy_args="${strategy_args}-s $strategy "
100                 ;;
101         -X*)
102                 case "$#,$1" in
103                 1,-X)
104                         usage ;;
105                 *,-X)
106                         xx="-X $(git rev-parse --sq-quote "$2")"
107                         shift ;;
108                 *,*)
109                         xx=$(git rev-parse --sq-quote "$1") ;;
110                 esac
111                 merge_args="$merge_args$xx "
112                 ;;
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                 ;;
119         --recurse-submodules)
120                 recurse_submodules=--recurse-submodules
121                 ;;
122         --recurse-submodules=*)
123                 recurse_submodules="$1"
124                 ;;
125         --no-recurse-submodules)
126                 recurse_submodules=--no-recurse-submodules
127                 ;;
128         --verify-signatures)
129                 verify_signatures=--verify-signatures
130                 ;;
131         --no-verify-signatures)
132                 verify_signatures=--no-verify-signatures
133                 ;;
134         --d|--dr|--dry|--dry-|--dry-r|--dry-ru|--dry-run)
135                 dry_run=--dry-run
136                 ;;
137         -h|--help-all)
138                 usage
139                 ;;
140         *)
141                 # Pass thru anything that may be meant for fetch.
142                 break
143                 ;;
144         esac
145         shift
146 done
147
148 error_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
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
169         curr_branch=${curr_branch#refs/heads/}
170         upstream=$(git config "branch.$curr_branch.merge")
171         remote=$(git config "branch.$curr_branch.remote")
172
173         if [ $# -gt 1 ]; then
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."
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"
184                 echo "a branch. Because this is not the default configured remote"
185                 echo "for your current branch, you must specify a branch on the command line."
186         elif [ -z "$curr_branch" -o -z "$upstream" ]; then
187                 . git-parse-remote
188                 error_on_missing_default_upstream "pull" $op_type $op_prep \
189                         "git pull <remote> <branch>"
190         else
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."
193         fi
194         exit 1
195 }
196
197 test true = "$rebase" && {
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
203                         die "$(gettext "updating an unborn branch with changes added to the index")"
204                 fi
205         else
206                 require_clean_work_tree "pull with rebase" "Please commit or stash them."
207         fi
208         oldremoteref= &&
209         test -n "$curr_branch" &&
210         . git-parse-remote &&
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
221 }
222 orig_head=$(git rev-parse -q --verify HEAD)
223 git fetch $verbosity $progress $dry_run $recurse_submodules --update-head-ok "$@" || exit 1
224 test -z "$dry_run" || exit 0
225
226 curr_head=$(git rev-parse -q --verify HEAD)
227 if test -n "$orig_head" && test "$curr_head" != "$orig_head"
228 then
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
235         eval_gettextln "Warning: fetch updated the current branch head.
236 Warning: fast-forwarding your working tree from
237 Warning: commit \$orig_head." >&2
238         git update-index -q --refresh
239         git read-tree -u -m "$orig_head" "$curr_head" ||
240                 die "$(eval_gettext "Cannot fast-forward your working tree.
241 After making sure that you saved anything precious from
242 $ git diff \$orig_head
243 output, run
244 $ git reset --hard
245 to recover.")"
246
247 fi
248
249 merge_head=$(sed -e '/  not-for-merge   /d' \
250         -e 's/  .*//' "$GIT_DIR"/FETCH_HEAD | \
251         tr '\012' ' ')
252
253 case "$merge_head" in
254 '')
255         error_on_no_merge_candidates "$@"
256         ;;
257 ?*' '?*)
258         if test -z "$orig_head"
259         then
260                 die "$(gettext "Cannot merge multiple branches into empty head")"
261         fi
262         if test true = "$rebase"
263         then
264                 die "$(gettext "Cannot rebase onto multiple branches")"
265         fi
266         ;;
267 esac
268
269 if test -z "$orig_head"
270 then
271         git update-ref -m "initial pull" HEAD $merge_head "$curr_head" &&
272         git read-tree -m -u HEAD || exit 1
273         exit
274 fi
275
276 if test true = "$rebase"
277 then
278         o=$(git show-branch --merge-base $curr_branch $merge_head $oldremoteref)
279         if test "$oldremoteref" = "$o"
280         then
281                 unset oldremoteref
282         fi
283 fi
284
285 merge_name=$(git fmt-merge-msg $log_arg <"$GIT_DIR/FETCH_HEAD") || exit
286 case "$rebase" in
287 true)
288         eval="git-rebase $diffstat $strategy_args $merge_args $verbosity"
289         eval="$eval --onto $merge_head ${oldremoteref:-$merge_head}"
290         ;;
291 *)
292         eval="git-merge $diffstat $no_commit $verify_signatures $edit $squash $no_ff $ff_only"
293         eval="$eval  $log_arg $strategy_args $merge_args $verbosity $progress"
294         eval="$eval \"\$merge_name\" HEAD $merge_head"
295         ;;
296 esac
297 eval "exec $eval"