git-pull.sh 7.4 KB
Newer Older
1 2
#!/bin/sh
#
3 4 5 6
# Copyright (c) 2005 Junio C Hamano
#
# 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
. git-sh-i18n
13
set_reflog_action "pull${1+ $*}"
14
require_work_tree_exists
15
cd_to_toplevel
16

17 18 19 20

die_conflict () {
    git diff-index --cached --name-status -r --ignore-submodules HEAD --
    if [ $(git config --bool --get advice.resolveConflict || echo true) = "true" ]; then
21
	die "$(gettext "Pull is not possible because you have unmerged files.
22
Please, fix them up in the work tree, and then use 'git add/rm <file>'
23
as appropriate to mark resolution, or use 'git commit -a'.")"
24
    else
25
	die "$(gettext "Pull is not possible because you have unmerged files.")"
26 27 28 29 30
    fi
}

die_merge () {
    if [ $(git config --bool --get advice.resolveConflict || echo true) = "true" ]; then
31 32
	die "$(gettext "You have not concluded your merge (MERGE_HEAD exists).
Please, commit your changes before you can merge.")"
33
    else
34
	die "$(gettext "You have not concluded your merge (MERGE_HEAD exists).")"
35 36 37 38 39
    fi
}

test -z "$(git ls-files -u)" || die_conflict
test -f "$GIT_DIR/MERGE_HEAD" && die_merge
40

41
strategy_args= diffstat= no_commit= squash= no_ff= ff_only=
42
log_arg= verbosity= progress= recurse_submodules=
43
merge_args=
44
curr_branch=$(git symbolic-ref -q HEAD)
45
curr_branch_short="${curr_branch#refs/heads/}"
46
rebase=$(git config --bool branch.$curr_branch_short.rebase)
47 48 49 50
if test -z "$rebase"
then
	rebase=$(git config --bool pull.rebase)
fi
J
Jeff King 已提交
51
dry_run=
52
while :
53 54
do
	case "$1" in
T
Tuncer Ayaz 已提交
55
	-q|--quiet)
56
		verbosity="$verbosity -q" ;;
T
Tuncer Ayaz 已提交
57
	-v|--verbose)
58
		verbosity="$verbosity -v" ;;
T
Tay Ray Chuan 已提交
59 60
	--progress)
		progress=--progress ;;
J
Jeff King 已提交
61 62
	--no-progress)
		progress=--no-progress ;;
63
	-n|--no-stat|--no-summary)
64
		diffstat=--no-stat ;;
65
	--stat|--summary)
66
		diffstat=--stat ;;
67 68
	--log|--no-log)
		log_arg=$1 ;;
69 70
	--no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
		no_commit=--no-commit ;;
71 72
	--c|--co|--com|--comm|--commi|--commit)
		no_commit=--commit ;;
J
Junio C Hamano 已提交
73 74
	--sq|--squ|--squa|--squas|--squash)
		squash=--squash ;;
75 76 77 78 79 80
	--no-sq|--no-squ|--no-squa|--no-squas|--no-squash)
		squash=--no-squash ;;
	--ff)
		no_ff=--ff ;;
	--no-ff)
		no_ff=--no-ff ;;
81 82
	--ff-only)
		ff_only=--ff-only ;;
83 84 85 86 87
	-s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
		--strateg=*|--strategy=*|\
	-s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
		case "$#,$1" in
		*,*=*)
D
Dennis Stosberg 已提交
88
			strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
89 90 91 92 93 94 95 96
		1,*)
			usage ;;
		*)
			strategy="$2"
			shift ;;
		esac
		strategy_args="${strategy_args}-s $strategy "
		;;
97 98 99 100 101
	-X*)
		case "$#,$1" in
		1,-X)
			usage ;;
		*,-X)
J
Junio C Hamano 已提交
102
			xx="-X $(git rev-parse --sq-quote "$2")"
103 104
			shift ;;
		*,*)
J
Junio C Hamano 已提交
105
			xx=$(git rev-parse --sq-quote "$1") ;;
106 107 108
		esac
		merge_args="$merge_args$xx "
		;;
109 110 111 112 113 114
	-r|--r|--re|--reb|--reba|--rebas|--rebase)
		rebase=true
		;;
	--no-r|--no-re|--no-reb|--no-reba|--no-rebas|--no-rebase)
		rebase=false
		;;
115 116 117
	--recurse-submodules)
		recurse_submodules=--recurse-submodules
		;;
118 119 120
	--recurse-submodules=*)
		recurse_submodules="$1"
		;;
121 122 123
	--no-recurse-submodules)
		recurse_submodules=--no-recurse-submodules
		;;
J
Jeff King 已提交
124 125 126
	--d|--dr|--dry|--dry-|--dry-r|--dry-ru|--dry-run)
		dry_run=--dry-run
		;;
127
	-h|--help-all)
128 129
		usage
		;;
130 131
	*)
		# Pass thru anything that may be meant for fetch.
132
		break
133 134 135 136 137
		;;
	esac
	shift
done

138 139 140 141 142 143 144 145 146 147 148 149
error_on_no_merge_candidates () {
	exec >&2
	for opt
	do
		case "$opt" in
		-t|--t|--ta|--tag|--tags)
			echo "Fetching tags only, you probably meant:"
			echo "  git fetch --tags"
			exit 1
		esac
	done

150 151 152 153 154 155 156 157 158
	if test true = "$rebase"
	then
		op_type=rebase
		op_prep=against
	else
		op_type=merge
		op_prep=with
	fi

159
	curr_branch=${curr_branch#refs/heads/}
160
	upstream=$(git config "branch.$curr_branch.merge")
161
	remote=$(git config "branch.$curr_branch.remote")
162

163
	if [ $# -gt 1 ]; then
164 165 166 167 168 169
		if [ "$rebase" = true ]; then
			printf "There is no candidate for rebasing against "
		else
			printf "There are no candidates for merging "
		fi
		echo "among the refs that you just fetched."
170 171 172 173
		echo "Generally this means that you provided a wildcard refspec which had no"
		echo "matches on the remote end."
	elif [ $# -gt 0 ] && [ "$1" != "$remote" ]; then
		echo "You asked to pull from the remote '$1', but did not specify"
174
		echo "a branch. Because this is not the default configured remote"
175
		echo "for your current branch, you must specify a branch on the command line."
176 177 178 179
	elif [ -z "$curr_branch" -o -z "$upstream" ]; then
		. git-parse-remote
		error_on_missing_default_upstream "pull" $op_type $op_prep \
			"git pull <repository> <refspec>"
180
	else
181 182
		echo "Your configuration specifies to $op_type $op_prep the ref '${upstream#refs/heads/}'"
		echo "from the remote, but no such ref was fetched."
183
	fi
184 185 186
	exit 1
}

187
test true = "$rebase" && {
188 189 190 191 192
	if ! git rev-parse -q --verify HEAD >/dev/null
	then
		# On an unborn branch
		if test -f "$GIT_DIR/index"
		then
193
			die "$(gettext "updating an unborn branch with changes added to the index")"
194 195
		fi
	else
196
		require_clean_work_tree "pull with rebase" "Please commit or stash them."
197
	fi
198
	oldremoteref= &&
199
	. git-parse-remote &&
200 201 202 203 204 205 206 207 208 209
	remoteref="$(get_remote_merge_branch "$@" 2>/dev/null)" &&
	oldremoteref="$(git rev-parse -q --verify "$remoteref")" &&
	for reflog in $(git rev-list -g $remoteref 2>/dev/null)
	do
		if test "$reflog" = "$(git merge-base $reflog $curr_branch)"
		then
			oldremoteref="$reflog"
			break
		fi
	done
210
}
M
Miklos Vajna 已提交
211
orig_head=$(git rev-parse -q --verify HEAD)
212
git fetch $verbosity $progress $dry_run $recurse_submodules --update-head-ok "$@" || exit 1
J
Jeff King 已提交
213
test -z "$dry_run" || exit 0
214

M
Miklos Vajna 已提交
215
curr_head=$(git rev-parse -q --verify HEAD)
216
if test -n "$orig_head" && test "$curr_head" != "$orig_head"
217 218 219 220 221 222 223
then
	# The fetch involved updating the current branch.

	# The working tree and the index file is still based on the
	# $orig_head commit, but we are merging into $curr_head.
	# First update the working tree to match $curr_head.

224
	eval_gettextln "Warning: fetch updated the current branch head.
225
Warning: fast-forwarding your working tree from
226
Warning: commit \$orig_head." >&2
227
	git update-index -q --refresh
228
	git read-tree -u -m "$orig_head" "$curr_head" ||
229
		die "$(eval_gettext "Cannot fast-forward your working tree.
230
After making sure that you saved anything precious from
231
$ git diff \$orig_head
232 233
output, run
$ git reset --hard
234
to recover.")"
235

236 237
fi

238 239 240
merge_head=$(sed -e '/	not-for-merge	/d' \
	-e 's/	.*//' "$GIT_DIR"/FETCH_HEAD | \
	tr '\012' ' ')
241 242

case "$merge_head" in
243
'')
J
Junio C Hamano 已提交
244
	error_on_no_merge_candidates "$@"
245
	;;
246
?*' '?*)
247 248
	if test -z "$orig_head"
	then
249
		die "$(gettext "Cannot merge multiple branches into empty head")"
250
	fi
251 252
	if test true = "$rebase"
	then
253
		die "$(gettext "Cannot rebase onto multiple branches")"
254
	fi
255 256 257
	;;
esac

258 259
if test -z "$orig_head"
then
260
	git update-ref -m "initial pull" HEAD $merge_head "$curr_head" &&
261
	git read-tree -m -u HEAD || exit 1
262 263 264
	exit
fi

265 266 267 268 269 270 271 272 273
if test true = "$rebase"
then
	o=$(git show-branch --merge-base $curr_branch $merge_head $oldremoteref)
	if test "$oldremoteref" = "$o"
	then
		unset oldremoteref
	fi
fi

274
merge_name=$(git fmt-merge-msg $log_arg <"$GIT_DIR/FETCH_HEAD") || exit
J
Junio C Hamano 已提交
275 276 277 278 279 280 281
case "$rebase" in
true)
	eval="git-rebase $diffstat $strategy_args $merge_args"
	eval="$eval --onto $merge_head ${oldremoteref:-$merge_head}"
	;;
*)
	eval="git-merge $diffstat $no_commit $squash $no_ff $ff_only"
J
Jeff King 已提交
282 283
	eval="$eval  $log_arg $strategy_args $merge_args $verbosity $progress"
	eval="$eval \"\$merge_name\" HEAD $merge_head"
J
Junio C Hamano 已提交
284 285 286
	;;
esac
eval "exec $eval"