git-pull: allow pulling into an empty repository
[git/dscho.git] / git-pull.sh
blobe23beb685d15447ac114c58d1795ed15bef88c9d
1 #!/bin/sh
3 # Copyright (c) 2005 Junio C Hamano
5 # Fetch one or more remote refs and merge it/them into the current HEAD.
7 USAGE='[-n | --no-summary] [--no-commit] [-s strategy]... [<fetch-options>] <repo> <head>...'
8 LONG_USAGE='Fetch one or more remote refs and merge it/them into the current HEAD.'
9 . git-sh-setup
11 strategy_args= no_summary= no_commit= squash=
12 while case "$#,$1" in 0) break ;; *,-*) ;; *) break ;; esac
14 case "$1" in
15 -n|--n|--no|--no-|--no-s|--no-su|--no-sum|--no-summ|\
16 --no-summa|--no-summar|--no-summary)
17 no_summary=-n ;;
18 --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
19 no_commit=--no-commit ;;
20 --sq|--squ|--squa|--squas|--squash)
21 squash=--squash ;;
22 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
23 --strateg=*|--strategy=*|\
24 -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
25 case "$#,$1" in
26 *,*=*)
27 strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
28 1,*)
29 usage ;;
31 strategy="$2"
32 shift ;;
33 esac
34 strategy_args="${strategy_args}-s $strategy "
36 -h|--h|--he|--hel|--help)
37 usage
39 -*)
40 # Pass thru anything that is meant for fetch.
41 break
43 esac
44 shift
45 done
47 orig_head=$(git-rev-parse --verify HEAD 2>/dev/null)
48 git-fetch --update-head-ok --reflog-action=pull "$@" || exit 1
50 curr_head=$(git-rev-parse --verify HEAD 2>/dev/null)
51 if test "$curr_head" != "$orig_head"
52 then
53 # The fetch involved updating the current branch.
55 # The working tree and the index file is still based on the
56 # $orig_head commit, but we are merging into $curr_head.
57 # First update the working tree to match $curr_head.
59 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 die 'Cannot fast-forward your working tree.
65 After making sure that you saved anything precious from
66 $ git diff '$orig_head'
67 output, run
68 $ git reset --hard
69 to recover.'
73 merge_head=$(sed -e '/ not-for-merge /d' \
74 -e 's/ .*//' "$GIT_DIR"/FETCH_HEAD | \
75 tr '\012' ' ')
77 case "$merge_head" in
78 '')
79 echo >&2 "No changes."
80 exit 0
82 ?*' '?*)
83 if test -z "$orig_head"
84 then
85 echo >&2 "Cannot merge multiple branches into empty head"
86 exit 1
88 var=`git-repo-config --get pull.octopus`
89 if test -n "$var"
90 then
91 strategy_default_args="-s $var"
95 var=`git-repo-config --get pull.twohead`
96 if test -n "$var"
97 then
98 strategy_default_args="-s $var"
101 esac
103 if test -z "$orig_head"
104 then
105 git-update-ref -m "initial pull" HEAD $merge_head "" &&
106 git-read-tree --reset -u HEAD || exit 1
107 exit
110 case "$strategy_args" in
112 strategy_args=$strategy_default_args
114 esac
116 merge_name=$(git-fmt-merge-msg <"$GIT_DIR/FETCH_HEAD") || exit
117 git-merge "--reflog-action=pull $*" \
118 $no_summary $no_commit $squash $strategy_args \
119 "$merge_name" HEAD $merge_head