git-pull.sh 2.9 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 8
USAGE='[-n | --no-summary] [--no-commit] [-s strategy]... [<fetch-options>] <repo> <head>...'
LONG_USAGE='Fetch one or more remote refs and merge it/them into the current HEAD.'
9
. git-sh-setup
10

J
Junio C Hamano 已提交
11
strategy_args= no_summary= no_commit= squash=
12 13 14 15 16 17
while case "$#,$1" in 0) break ;; *,-*) ;; *) break ;; esac
do
	case "$1" in
	-n|--n|--no|--no-|--no-s|--no-su|--no-sum|--no-summ|\
		--no-summa|--no-summar|--no-summary)
		no_summary=-n ;;
18 19
	--no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
		no_commit=--no-commit ;;
J
Junio C Hamano 已提交
20 21
	--sq|--squ|--squa|--squas|--squash)
		squash=--squash ;;
22 23 24 25 26
	-s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
		--strateg=*|--strategy=*|\
	-s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
		case "$#,$1" in
		*,*=*)
D
Dennis Stosberg 已提交
27
			strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
28 29 30 31 32 33 34 35
		1,*)
			usage ;;
		*)
			strategy="$2"
			shift ;;
		esac
		strategy_args="${strategy_args}-s $strategy "
		;;
36 37 38
	-h|--h|--he|--hel|--help)
		usage
		;;
39
	-*)
40 41
		# Pass thru anything that is meant for fetch.
		break
42 43 44 45 46
		;;
	esac
	shift
done

47
orig_head=$(git-rev-parse --verify HEAD 2>/dev/null)
48
git-fetch --update-head-ok --reflog-action=pull "$@" || exit 1
49

50
curr_head=$(git-rev-parse --verify HEAD 2>/dev/null)
51 52 53 54 55 56 57 58 59
if test "$curr_head" != "$orig_head"
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.

	echo >&2 "Warning: fetch updated the current branch head."
60
	echo >&2 "Warning: fast forwarding your working tree from"
61
	echo >&2 "Warning: commit $orig_head."
62
	git-update-index --refresh 2>/dev/null
63
	git-read-tree -u -m "$orig_head" "$curr_head" ||
64 65 66 67 68 69 70
		die 'Cannot fast-forward your working tree.
After making sure that you saved anything precious from
$ git diff '$orig_head'
output, run
$ git reset --hard
to recover.'

71 72
fi

73 74 75
merge_head=$(sed -e '/	not-for-merge	/d' \
	-e 's/	.*//' "$GIT_DIR"/FETCH_HEAD | \
	tr '\012' ' ')
76 77

case "$merge_head" in
78 79 80 81
'')
	echo >&2 "No changes."
	exit 0
	;;
82
?*' '?*)
83 84 85 86 87
	if test -z "$orig_head"
	then
		echo >&2 "Cannot merge multiple branches into empty head"
		exit 1
	fi
88
	var=`git-repo-config --get pull.octopus`
89
	if test -n "$var"
90
	then
91
		strategy_default_args="-s $var"
92
	fi
93 94
	;;
*)
95
	var=`git-repo-config --get pull.twohead`
96 97
	if test -n "$var"
        then
98
		strategy_default_args="-s $var"
99
	fi
100 101 102
	;;
esac

103 104 105 106 107 108 109
if test -z "$orig_head"
then
	git-update-ref -m "initial pull" HEAD $merge_head "" &&
	git-read-tree --reset -u HEAD || exit 1
	exit
fi

110 111 112
case "$strategy_args" in
'')
	strategy_args=$strategy_default_args
113
	;;
114 115
esac

116
merge_name=$(git-fmt-merge-msg <"$GIT_DIR/FETCH_HEAD") || exit
117 118
git-merge "--reflog-action=pull $*" \
	$no_summary $no_commit $squash $strategy_args \
J
Junio C Hamano 已提交
119
	"$merge_name" HEAD $merge_head