Merge branch 'jk/maint-1.6.5-reset-hard' into maint-1.6.5
[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 set_reflog_action "pull $*"
13 require_work_tree
14 cd_to_toplevel
15
16 test -z "$(git ls-files -u)" ||
17         die "You are in the middle of a conflicted merge."
18
19 strategy_args= diffstat= no_commit= squash= no_ff= log_arg= verbosity=
20 curr_branch=$(git symbolic-ref -q HEAD)
21 curr_branch_short=$(echo "$curr_branch" | sed "s|refs/heads/||")
22 rebase=$(git config --bool branch.$curr_branch_short.rebase)
23 while :
24 do
25         case "$1" in
26         -q|--quiet)
27                 verbosity="$verbosity -q" ;;
28         -v|--verbose)
29                 verbosity="$verbosity -v" ;;
30         -n|--no-stat|--no-summary)
31                 diffstat=--no-stat ;;
32         --stat|--summary)
33                 diffstat=--stat ;;
34         --log|--no-log)
35                 log_arg=$1 ;;
36         --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
37                 no_commit=--no-commit ;;
38         --c|--co|--com|--comm|--commi|--commit)
39                 no_commit=--commit ;;
40         --sq|--squ|--squa|--squas|--squash)
41                 squash=--squash ;;
42         --no-sq|--no-squ|--no-squa|--no-squas|--no-squash)
43                 squash=--no-squash ;;
44         --ff)
45                 no_ff=--ff ;;
46         --no-ff)
47                 no_ff=--no-ff ;;
48         -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
49                 --strateg=*|--strategy=*|\
50         -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
51                 case "$#,$1" in
52                 *,*=*)
53                         strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
54                 1,*)
55                         usage ;;
56                 *)
57                         strategy="$2"
58                         shift ;;
59                 esac
60                 strategy_args="${strategy_args}-s $strategy "
61                 ;;
62         -r|--r|--re|--reb|--reba|--rebas|--rebase)
63                 rebase=true
64                 ;;
65         --no-r|--no-re|--no-reb|--no-reba|--no-rebas|--no-rebase)
66                 rebase=false
67                 ;;
68         -h|--h|--he|--hel|--help)
69                 usage
70                 ;;
71         *)
72                 # Pass thru anything that may be meant for fetch.
73                 break
74                 ;;
75         esac
76         shift
77 done
78
79 error_on_no_merge_candidates () {
80         exec >&2
81         for opt
82         do
83                 case "$opt" in
84                 -t|--t|--ta|--tag|--tags)
85                         echo "Fetching tags only, you probably meant:"
86                         echo "  git fetch --tags"
87                         exit 1
88                 esac
89         done
90
91         if test true = "$rebase"
92         then
93                 op_type=rebase
94                 op_prep=against
95         else
96                 op_type=merge
97                 op_prep=with
98         fi
99
100         curr_branch=${curr_branch#refs/heads/}
101         upstream=$(git config "branch.$curr_branch.merge")
102         remote=$(git config "branch.$curr_branch.remote")
103
104         if [ $# -gt 1 ]; then
105                 if [ "$rebase" = true ]; then
106                         printf "There is no candidate for rebasing against "
107                 else
108                         printf "There are no candidates for merging "
109                 fi
110                 echo "among the refs that you just fetched."
111                 echo "Generally this means that you provided a wildcard refspec which had no"
112                 echo "matches on the remote end."
113         elif [ $# -gt 0 ] && [ "$1" != "$remote" ]; then
114                 echo "You asked to pull from the remote '$1', but did not specify"
115                 echo "a branch. Because this is not the default configured remote"
116                 echo "for your current branch, you must specify a branch on the command line."
117         elif [ -z "$curr_branch" ]; then
118                 echo "You are not currently on a branch, so I cannot use any"
119                 echo "'branch.<branchname>.merge' in your configuration file."
120                 echo "Please specify which remote branch you want to use on the command"
121                 echo "line and try again (e.g. 'git pull <repository> <refspec>')."
122                 echo "See git-pull(1) for details."
123         elif [ -z "$upstream" ]; then
124                 echo "You asked me to pull without telling me which branch you"
125                 echo "want to $op_type $op_prep, and 'branch.${curr_branch}.merge' in"
126                 echo "your configuration file does not tell me, either. Please"
127                 echo "specify which branch you want to use on the command line and"
128                 echo "try again (e.g. 'git pull <repository> <refspec>')."
129                 echo "See git-pull(1) for details."
130                 echo
131                 echo "If you often $op_type $op_prep the same branch, you may want to"
132                 echo "use something like the following in your configuration file:"
133                 echo
134                 echo "    [branch \"${curr_branch}\"]"
135                 echo "    remote = <nickname>"
136                 echo "    merge = <remote-ref>"
137                 test rebase = "$op_type" &&
138                         echo "    rebase = true"
139                 echo
140                 echo "    [remote \"<nickname>\"]"
141                 echo "    url = <url>"
142                 echo "    fetch = <refspec>"
143                 echo
144                 echo "See git-config(1) for details."
145         else
146                 echo "Your configuration specifies to $op_type $op_prep the ref '${upstream#refs/heads/}'"
147                 echo "from the remote, but no such ref was fetched."
148         fi
149         exit 1
150 }
151
152 test true = "$rebase" && {
153         if ! git rev-parse -q --verify HEAD >/dev/null
154         then
155                 # On an unborn branch
156                 if test -f "$GIT_DIR/index"
157                 then
158                         die "updating an unborn branch with changes added to the index"
159                 fi
160         else
161                 git update-index --ignore-submodules --refresh &&
162                 git diff-files --ignore-submodules --quiet &&
163                 git diff-index --ignore-submodules --cached --quiet HEAD -- ||
164                 die "refusing to pull with rebase: your working tree is not up-to-date"
165         fi
166         oldremoteref= &&
167         . git-parse-remote &&
168         remoteref="$(get_remote_merge_branch "$@" 2>/dev/null)" &&
169         oldremoteref="$(git rev-parse -q --verify "$remoteref")" &&
170         for reflog in $(git rev-list -g $remoteref 2>/dev/null)
171         do
172                 if test "$reflog" = "$(git merge-base $reflog $curr_branch)"
173                 then
174                         oldremoteref="$reflog"
175                         break
176                 fi
177         done
178 }
179 orig_head=$(git rev-parse -q --verify HEAD)
180 git fetch $verbosity --update-head-ok "$@" || exit 1
181
182 curr_head=$(git rev-parse -q --verify HEAD)
183 if test -n "$orig_head" && test "$curr_head" != "$orig_head"
184 then
185         # The fetch involved updating the current branch.
186
187         # The working tree and the index file is still based on the
188         # $orig_head commit, but we are merging into $curr_head.
189         # First update the working tree to match $curr_head.
190
191         echo >&2 "Warning: fetch updated the current branch head."
192         echo >&2 "Warning: fast forwarding your working tree from"
193         echo >&2 "Warning: commit $orig_head."
194         git update-index -q --refresh
195         git read-tree -u -m "$orig_head" "$curr_head" ||
196                 die 'Cannot fast-forward your working tree.
197 After making sure that you saved anything precious from
198 $ git diff '$orig_head'
199 output, run
200 $ git reset --hard
201 to recover.'
202
203 fi
204
205 merge_head=$(sed -e '/  not-for-merge   /d' \
206         -e 's/  .*//' "$GIT_DIR"/FETCH_HEAD | \
207         tr '\012' ' ')
208
209 case "$merge_head" in
210 '')
211         error_on_no_merge_candidates "$@"
212         ;;
213 ?*' '?*)
214         if test -z "$orig_head"
215         then
216                 die "Cannot merge multiple branches into empty head"
217         fi
218         if test true = "$rebase"
219         then
220                 die "Cannot rebase onto multiple branches"
221         fi
222         ;;
223 esac
224
225 if test -z "$orig_head"
226 then
227         git update-ref -m "initial pull" HEAD $merge_head "$curr_head" &&
228         git read-tree --reset -u HEAD || exit 1
229         exit
230 fi
231
232 merge_name=$(git fmt-merge-msg $log_arg <"$GIT_DIR/FETCH_HEAD") || exit
233 test true = "$rebase" &&
234         exec git-rebase $diffstat $strategy_args --onto $merge_head \
235         ${oldremoteref:-$merge_head}
236 exec git-merge $diffstat $no_commit $squash $no_ff $log_arg $strategy_args \
237         "$merge_name" HEAD $merge_head $verbosity