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