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