Code

pull: introduce a pull.rebase option to enable --rebase
[git.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.
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
17 die_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.
21 Please, fix them up in the work tree, and then use 'git add/rm <file>'
22 as 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 }
28 die_merge () {
29     if [ $(git config --bool --get advice.resolveConflict || echo true) = "true" ]; then
30         die "You have not concluded your merge (MERGE_HEAD exists).
31 Please, commit your changes before you can merge."
32     else
33         die "You have not concluded your merge (MERGE_HEAD exists)."
34     fi
35 }
37 test -z "$(git ls-files -u)" || die_conflict
38 test -f "$GIT_DIR/MERGE_HEAD" && die_merge
40 strategy_args= diffstat= no_commit= squash= no_ff= ff_only=
41 log_arg= verbosity= progress= recurse_submodules=
42 merge_args=
43 curr_branch=$(git symbolic-ref -q HEAD)
44 curr_branch_short="${curr_branch#refs/heads/}"
45 rebase=$(git config --bool branch.$curr_branch_short.rebase)
46 if test -z "$rebase"
47 then
48         rebase=$(git config --bool pull.rebase)
49 fi
50 dry_run=
51 while :
52 do
53         case "$1" in
54         -q|--quiet)
55                 verbosity="$verbosity -q" ;;
56         -v|--verbose)
57                 verbosity="$verbosity -v" ;;
58         --progress)
59                 progress=--progress ;;
60         --no-progress)
61                 progress=--no-progress ;;
62         -n|--no-stat|--no-summary)
63                 diffstat=--no-stat ;;
64         --stat|--summary)
65                 diffstat=--stat ;;
66         --log|--no-log)
67                 log_arg=$1 ;;
68         --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
69                 no_commit=--no-commit ;;
70         --c|--co|--com|--comm|--commi|--commit)
71                 no_commit=--commit ;;
72         --sq|--squ|--squa|--squas|--squash)
73                 squash=--squash ;;
74         --no-sq|--no-squ|--no-squa|--no-squas|--no-squash)
75                 squash=--no-squash ;;
76         --ff)
77                 no_ff=--ff ;;
78         --no-ff)
79                 no_ff=--no-ff ;;
80         --ff-only)
81                 ff_only=--ff-only ;;
82         -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
83                 --strateg=*|--strategy=*|\
84         -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
85                 case "$#,$1" in
86                 *,*=*)
87                         strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
88                 1,*)
89                         usage ;;
90                 *)
91                         strategy="$2"
92                         shift ;;
93                 esac
94                 strategy_args="${strategy_args}-s $strategy "
95                 ;;
96         -X*)
97                 case "$#,$1" in
98                 1,-X)
99                         usage ;;
100                 *,-X)
101                         xx="-X $(git rev-parse --sq-quote "$2")"
102                         shift ;;
103                 *,*)
104                         xx=$(git rev-parse --sq-quote "$1") ;;
105                 esac
106                 merge_args="$merge_args$xx "
107                 ;;
108         -r|--r|--re|--reb|--reba|--rebas|--rebase)
109                 rebase=true
110                 ;;
111         --no-r|--no-re|--no-reb|--no-reba|--no-rebas|--no-rebase)
112                 rebase=false
113                 ;;
114         --recurse-submodules)
115                 recurse_submodules=--recurse-submodules
116                 ;;
117         --recurse-submodules=*)
118                 recurse_submodules="$1"
119                 ;;
120         --no-recurse-submodules)
121                 recurse_submodules=--no-recurse-submodules
122                 ;;
123         --d|--dr|--dry|--dry-|--dry-r|--dry-ru|--dry-run)
124                 dry_run=--dry-run
125                 ;;
126         -h|--h|--he|--hel|--help|--help-|--help-a|--help-al|--help-all)
127                 usage
128                 ;;
129         *)
130                 # Pass thru anything that may be meant for fetch.
131                 break
132                 ;;
133         esac
134         shift
135 done
137 error_on_no_merge_candidates () {
138         exec >&2
139         for opt
140         do
141                 case "$opt" in
142                 -t|--t|--ta|--tag|--tags)
143                         echo "Fetching tags only, you probably meant:"
144                         echo "  git fetch --tags"
145                         exit 1
146                 esac
147         done
149         if test true = "$rebase"
150         then
151                 op_type=rebase
152                 op_prep=against
153         else
154                 op_type=merge
155                 op_prep=with
156         fi
158         curr_branch=${curr_branch#refs/heads/}
159         upstream=$(git config "branch.$curr_branch.merge")
160         remote=$(git config "branch.$curr_branch.remote")
162         if [ $# -gt 1 ]; then
163                 if [ "$rebase" = true ]; then
164                         printf "There is no candidate for rebasing against "
165                 else
166                         printf "There are no candidates for merging "
167                 fi
168                 echo "among the refs that you just fetched."
169                 echo "Generally this means that you provided a wildcard refspec which had no"
170                 echo "matches on the remote end."
171         elif [ $# -gt 0 ] && [ "$1" != "$remote" ]; then
172                 echo "You asked to pull from the remote '$1', but did not specify"
173                 echo "a branch. Because this is not the default configured remote"
174                 echo "for your current branch, you must specify a branch on the command line."
175         elif [ -z "$curr_branch" -o -z "$upstream" ]; then
176                 . git-parse-remote
177                 error_on_missing_default_upstream "pull" $op_type $op_prep \
178                         "git pull <repository> <refspec>"
179         else
180                 echo "Your configuration specifies to $op_type $op_prep the ref '${upstream#refs/heads/}'"
181                 echo "from the remote, but no such ref was fetched."
182         fi
183         exit 1
186 test true = "$rebase" && {
187         if ! git rev-parse -q --verify HEAD >/dev/null
188         then
189                 # On an unborn branch
190                 if test -f "$GIT_DIR/index"
191                 then
192                         die "updating an unborn branch with changes added to the index"
193                 fi
194         else
195                 require_clean_work_tree "pull with rebase" "Please commit or stash them."
196         fi
197         oldremoteref= &&
198         . git-parse-remote &&
199         remoteref="$(get_remote_merge_branch "$@" 2>/dev/null)" &&
200         oldremoteref="$(git rev-parse -q --verify "$remoteref")" &&
201         for reflog in $(git rev-list -g $remoteref 2>/dev/null)
202         do
203                 if test "$reflog" = "$(git merge-base $reflog $curr_branch)"
204                 then
205                         oldremoteref="$reflog"
206                         break
207                 fi
208         done
210 orig_head=$(git rev-parse -q --verify HEAD)
211 git fetch $verbosity $progress $dry_run $recurse_submodules --update-head-ok "$@" || exit 1
212 test -z "$dry_run" || exit 0
214 curr_head=$(git rev-parse -q --verify HEAD)
215 if test -n "$orig_head" && test "$curr_head" != "$orig_head"
216 then
217         # The fetch involved updating the current branch.
219         # The working tree and the index file is still based on the
220         # $orig_head commit, but we are merging into $curr_head.
221         # First update the working tree to match $curr_head.
223         echo >&2 "Warning: fetch updated the current branch head."
224         echo >&2 "Warning: fast-forwarding your working tree from"
225         echo >&2 "Warning: commit $orig_head."
226         git update-index -q --refresh
227         git read-tree -u -m "$orig_head" "$curr_head" ||
228                 die 'Cannot fast-forward your working tree.
229 After making sure that you saved anything precious from
230 $ git diff '$orig_head'
231 output, run
232 $ git reset --hard
233 to recover.'
235 fi
237 merge_head=$(sed -e '/  not-for-merge   /d' \
238         -e 's/  .*//' "$GIT_DIR"/FETCH_HEAD | \
239         tr '\012' ' ')
241 case "$merge_head" in
242 '')
243         error_on_no_merge_candidates "$@"
244         ;;
245 ?*' '?*)
246         if test -z "$orig_head"
247         then
248                 die "Cannot merge multiple branches into empty head"
249         fi
250         if test true = "$rebase"
251         then
252                 die "Cannot rebase onto multiple branches"
253         fi
254         ;;
255 esac
257 if test -z "$orig_head"
258 then
259         git update-ref -m "initial pull" HEAD $merge_head "$curr_head" &&
260         git read-tree -m -u HEAD || exit 1
261         exit
262 fi
264 if test true = "$rebase"
265 then
266         o=$(git show-branch --merge-base $curr_branch $merge_head $oldremoteref)
267         if test "$oldremoteref" = "$o"
268         then
269                 unset oldremoteref
270         fi
271 fi
273 merge_name=$(git fmt-merge-msg $log_arg <"$GIT_DIR/FETCH_HEAD") || exit
274 case "$rebase" in
275 true)
276         eval="git-rebase $diffstat $strategy_args $merge_args"
277         eval="$eval --onto $merge_head ${oldremoteref:-$merge_head}"
278         ;;
279 *)
280         eval="git-merge $diffstat $no_commit $squash $no_ff $ff_only"
281         eval="$eval  $log_arg $strategy_args $merge_args $verbosity $progress"
282         eval="$eval \"\$merge_name\" HEAD $merge_head"
283         ;;
284 esac
285 eval "exec $eval"