]>
Commit | Line | Data |
---|---|---|
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 | 7 | USAGE='[-n | --no-stat] [--[no-]commit] [--[no-]squash] [--[no-]ff] [-s strategy]... [<fetch-options>] <repo> <head>...' |
806f36d4 | 8 | LONG_USAGE='Fetch one or more remote refs and merge it/them into the current HEAD.' |
533b7039 | 9 | SUBDIRECTORY_OK=Yes |
8f321a39 | 10 | OPTIONS_SPEC= |
ae2b0f15 | 11 | . git-sh-setup |
f9474132 | 12 | set_reflog_action "pull $*" |
7eff28a9 | 13 | require_work_tree |
533b7039 | 14 | cd_to_toplevel |
b10ac50f | 15 | |
d38a30df MM |
16 | |
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 | } | |
27 | ||
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 | } | |
36 | ||
37 | test -z "$(git ls-files -u)" || die_conflict | |
38 | test -f "$GIT_DIR/MERGE_HEAD" && die_merge | |
d1014a17 | 39 | |
13474835 | 40 | strategy_args= diffstat= no_commit= squash= no_ff= ff_only= |
9839018e | 41 | log_arg= verbosity= progress= |
ee2c7955 | 42 | merge_args= |
cd67e4d4 | 43 | curr_branch=$(git symbolic-ref -q HEAD) |
0d12e59f | 44 | curr_branch_short="${curr_branch#refs/heads/}" |
cd67e4d4 | 45 | rebase=$(git config --bool branch.$curr_branch_short.rebase) |
29609e68 | 46 | dry_run= |
822f7c73 | 47 | while : |
60fb5b2c JH |
48 | do |
49 | case "$1" in | |
7f87aff2 | 50 | -q|--quiet) |
c6576f91 | 51 | verbosity="$verbosity -q" ;; |
7f87aff2 | 52 | -v|--verbose) |
c6576f91 | 53 | verbosity="$verbosity -v" ;; |
9839018e TRC |
54 | --progress) |
55 | progress=--progress ;; | |
d8abe148 | 56 | -n|--no-stat|--no-summary) |
a334e125 | 57 | diffstat=--no-stat ;; |
d8abe148 | 58 | --stat|--summary) |
a334e125 | 59 | diffstat=--stat ;; |
efb779f8 SG |
60 | --log|--no-log) |
61 | log_arg=$1 ;; | |
123ee3ca JH |
62 | --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit) |
63 | no_commit=--no-commit ;; | |
5072a323 LH |
64 | --c|--co|--com|--comm|--commi|--commit) |
65 | no_commit=--commit ;; | |
7d0c6887 JH |
66 | --sq|--squ|--squa|--squas|--squash) |
67 | squash=--squash ;; | |
5072a323 LH |
68 | --no-sq|--no-squ|--no-squa|--no-squas|--no-squash) |
69 | squash=--no-squash ;; | |
70 | --ff) | |
71 | no_ff=--ff ;; | |
72 | --no-ff) | |
73 | no_ff=--no-ff ;; | |
13474835 BG |
74 | --ff-only) |
75 | ff_only=--ff-only ;; | |
60fb5b2c JH |
76 | -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\ |
77 | --strateg=*|--strategy=*|\ | |
78 | -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy) | |
79 | case "$#,$1" in | |
80 | *,*=*) | |
8096fae7 | 81 | strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;; |
60fb5b2c JH |
82 | 1,*) |
83 | usage ;; | |
84 | *) | |
85 | strategy="$2" | |
86 | shift ;; | |
87 | esac | |
88 | strategy_args="${strategy_args}-s $strategy " | |
89 | ;; | |
ee2c7955 AP |
90 | -X*) |
91 | case "$#,$1" in | |
92 | 1,-X) | |
93 | usage ;; | |
94 | *,-X) | |
14e5d40c | 95 | xx="-X $(git rev-parse --sq-quote "$2")" |
ee2c7955 AP |
96 | shift ;; |
97 | *,*) | |
14e5d40c | 98 | xx=$(git rev-parse --sq-quote "$1") ;; |
ee2c7955 AP |
99 | esac |
100 | merge_args="$merge_args$xx " | |
101 | ;; | |
cd67e4d4 JS |
102 | -r|--r|--re|--reb|--reba|--rebas|--rebase) |
103 | rebase=true | |
104 | ;; | |
105 | --no-r|--no-re|--no-reb|--no-reba|--no-rebas|--no-rebase) | |
106 | rebase=false | |
107 | ;; | |
29609e68 JK |
108 | --d|--dr|--dry|--dry-|--dry-r|--dry-ru|--dry-run) |
109 | dry_run=--dry-run | |
110 | ;; | |
93d69d86 JL |
111 | -h|--h|--he|--hel|--help) |
112 | usage | |
113 | ;; | |
822f7c73 DK |
114 | *) |
115 | # Pass thru anything that may be meant for fetch. | |
619e5a0e | 116 | break |
60fb5b2c JH |
117 | ;; |
118 | esac | |
119 | shift | |
120 | done | |
121 | ||
441ed413 JH |
122 | error_on_no_merge_candidates () { |
123 | exec >&2 | |
124 | for opt | |
125 | do | |
126 | case "$opt" in | |
127 | -t|--t|--ta|--tag|--tags) | |
128 | echo "Fetching tags only, you probably meant:" | |
129 | echo " git fetch --tags" | |
130 | exit 1 | |
131 | esac | |
132 | done | |
133 | ||
995fc2f7 JK |
134 | if test true = "$rebase" |
135 | then | |
136 | op_type=rebase | |
137 | op_prep=against | |
138 | else | |
139 | op_type=merge | |
140 | op_prep=with | |
141 | fi | |
142 | ||
441ed413 | 143 | curr_branch=${curr_branch#refs/heads/} |
a6dbf881 | 144 | upstream=$(git config "branch.$curr_branch.merge") |
a8c9bef4 | 145 | remote=$(git config "branch.$curr_branch.remote") |
441ed413 | 146 | |
a8c9bef4 | 147 | if [ $# -gt 1 ]; then |
995fc2f7 JK |
148 | if [ "$rebase" = true ]; then |
149 | printf "There is no candidate for rebasing against " | |
150 | else | |
151 | printf "There are no candidates for merging " | |
152 | fi | |
153 | echo "among the refs that you just fetched." | |
a8c9bef4 JK |
154 | echo "Generally this means that you provided a wildcard refspec which had no" |
155 | echo "matches on the remote end." | |
156 | elif [ $# -gt 0 ] && [ "$1" != "$remote" ]; then | |
157 | echo "You asked to pull from the remote '$1', but did not specify" | |
995fc2f7 | 158 | echo "a branch. Because this is not the default configured remote" |
a8c9bef4 JK |
159 | echo "for your current branch, you must specify a branch on the command line." |
160 | elif [ -z "$curr_branch" ]; then | |
61e6108d MM |
161 | echo "You are not currently on a branch, so I cannot use any" |
162 | echo "'branch.<branchname>.merge' in your configuration file." | |
995fc2f7 | 163 | echo "Please specify which remote branch you want to use on the command" |
61e6108d MM |
164 | echo "line and try again (e.g. 'git pull <repository> <refspec>')." |
165 | echo "See git-pull(1) for details." | |
a6dbf881 | 166 | elif [ -z "$upstream" ]; then |
61e6108d | 167 | echo "You asked me to pull without telling me which branch you" |
995fc2f7 JK |
168 | echo "want to $op_type $op_prep, and 'branch.${curr_branch}.merge' in" |
169 | echo "your configuration file does not tell me, either. Please" | |
170 | echo "specify which branch you want to use on the command line and" | |
61e6108d MM |
171 | echo "try again (e.g. 'git pull <repository> <refspec>')." |
172 | echo "See git-pull(1) for details." | |
173 | echo | |
995fc2f7 JK |
174 | echo "If you often $op_type $op_prep the same branch, you may want to" |
175 | echo "use something like the following in your configuration file:" | |
176 | echo | |
177 | echo " [branch \"${curr_branch}\"]" | |
178 | echo " remote = <nickname>" | |
179 | echo " merge = <remote-ref>" | |
180 | test rebase = "$op_type" && | |
181 | echo " rebase = true" | |
61e6108d | 182 | echo |
995fc2f7 JK |
183 | echo " [remote \"<nickname>\"]" |
184 | echo " url = <url>" | |
185 | echo " fetch = <refspec>" | |
61e6108d MM |
186 | echo |
187 | echo "See git-config(1) for details." | |
a6dbf881 | 188 | else |
995fc2f7 JK |
189 | echo "Your configuration specifies to $op_type $op_prep the ref '${upstream#refs/heads/}'" |
190 | echo "from the remote, but no such ref was fetched." | |
61e6108d | 191 | fi |
441ed413 JH |
192 | exit 1 |
193 | } | |
194 | ||
c85c7927 | 195 | test true = "$rebase" && { |
19a7fcbf JK |
196 | if ! git rev-parse -q --verify HEAD >/dev/null |
197 | then | |
198 | # On an unborn branch | |
199 | if test -f "$GIT_DIR/index" | |
200 | then | |
201 | die "updating an unborn branch with changes added to the index" | |
202 | fi | |
203 | else | |
204 | git update-index --ignore-submodules --refresh && | |
205 | git diff-files --ignore-submodules --quiet && | |
206 | git diff-index --ignore-submodules --cached --quiet HEAD -- || | |
207 | die "refusing to pull with rebase: your working tree is not up-to-date" | |
208 | fi | |
d44e7126 | 209 | oldremoteref= && |
c85c7927 | 210 | . git-parse-remote && |
d44e7126 SB |
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 | |
c85c7927 | 221 | } |
2d179857 | 222 | orig_head=$(git rev-parse -q --verify HEAD) |
29609e68 JK |
223 | git fetch $verbosity $progress $dry_run --update-head-ok "$@" || exit 1 |
224 | test -z "$dry_run" || exit 0 | |
b10ac50f | 225 | |
2d179857 | 226 | curr_head=$(git rev-parse -q --verify HEAD) |
b0ad11ea | 227 | if test -n "$orig_head" && test "$curr_head" != "$orig_head" |
b10ac50f JH |
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 | echo >&2 "Warning: fetch updated the current branch head." | |
a75d7b54 | 236 | echo >&2 "Warning: fast-forwarding your working tree from" |
a057f806 | 237 | echo >&2 "Warning: commit $orig_head." |
7bd93c1c | 238 | git update-index -q --refresh |
5be60078 | 239 | git read-tree -u -m "$orig_head" "$curr_head" || |
8323124a JH |
240 | die '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 | ||
b10ac50f JH |
247 | fi |
248 | ||
05dd8e2e JH |
249 | merge_head=$(sed -e '/ not-for-merge /d' \ |
250 | -e 's/ .*//' "$GIT_DIR"/FETCH_HEAD | \ | |
251 | tr '\012' ' ') | |
e0bfc81e JH |
252 | |
253 | case "$merge_head" in | |
521003ff | 254 | '') |
4973aa22 | 255 | error_on_no_merge_candidates "$@" |
521003ff | 256 | ;; |
60fb5b2c | 257 | ?*' '?*) |
d09e79cb LT |
258 | if test -z "$orig_head" |
259 | then | |
bc2bbc45 | 260 | die "Cannot merge multiple branches into empty head" |
d09e79cb | 261 | fi |
51b2ead0 JS |
262 | if test true = "$rebase" |
263 | then | |
bc2bbc45 | 264 | die "Cannot rebase onto multiple branches" |
51b2ead0 | 265 | fi |
60fb5b2c JH |
266 | ;; |
267 | esac | |
268 | ||
d09e79cb LT |
269 | if test -z "$orig_head" |
270 | then | |
b0ad11ea | 271 | git update-ref -m "initial pull" HEAD $merge_head "$curr_head" && |
5be60078 | 272 | git read-tree --reset -u HEAD || exit 1 |
d09e79cb LT |
273 | exit |
274 | fi | |
275 | ||
efb779f8 | 276 | merge_name=$(git fmt-merge-msg $log_arg <"$GIT_DIR/FETCH_HEAD") || exit |
14e5d40c JH |
277 | case "$rebase" in |
278 | true) | |
279 | eval="git-rebase $diffstat $strategy_args $merge_args" | |
280 | eval="$eval --onto $merge_head ${oldremoteref:-$merge_head}" | |
281 | ;; | |
282 | *) | |
283 | eval="git-merge $diffstat $no_commit $squash $no_ff $ff_only" | |
284 | eval="$eval $log_arg $strategy_args $merge_args" | |
7ecee331 | 285 | eval="$eval \"\$merge_name\" HEAD $merge_head $verbosity" |
14e5d40c JH |
286 | ;; |
287 | esac | |
288 | eval "exec $eval" |