Allow callers of unpack_trees() to handle failure
[debian-git.git] / git-pull.sh
blobfa97b0f3562ab34b7cf1b7b68b293d9174121088
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] [--[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 set_reflog_action "pull $*"
13 require_work_tree
14 cd_to_toplevel
16 test -z "$(git ls-files -u)" ||
17 die "You are in the middle of a conflicted merge."
19 strategy_args= no_summary= no_commit= squash= no_ff=
20 curr_branch=$(git symbolic-ref -q HEAD)
21 curr_branch_short=$(echo "$curr_branch" | sed "s|refs/heads/||")
22 rebase=$(git config --bool branch.$curr_branch_short.rebase)
23 while :
25 case "$1" in
26 -n|--n|--no|--no-|--no-s|--no-su|--no-sum|--no-summ|\
27 --no-summa|--no-summar|--no-summary)
28 no_summary=-n ;;
29 --summary)
30 no_summary=$1
32 --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
33 no_commit=--no-commit ;;
34 --c|--co|--com|--comm|--commi|--commit)
35 no_commit=--commit ;;
36 --sq|--squ|--squa|--squas|--squash)
37 squash=--squash ;;
38 --no-sq|--no-squ|--no-squa|--no-squas|--no-squash)
39 squash=--no-squash ;;
40 --ff)
41 no_ff=--ff ;;
42 --no-ff)
43 no_ff=--no-ff ;;
44 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
45 --strateg=*|--strategy=*|\
46 -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
47 case "$#,$1" in
48 *,*=*)
49 strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
50 1,*)
51 usage ;;
53 strategy="$2"
54 shift ;;
55 esac
56 strategy_args="${strategy_args}-s $strategy "
58 -r|--r|--re|--reb|--reba|--rebas|--rebase)
59 rebase=true
61 --no-r|--no-re|--no-reb|--no-reba|--no-rebas|--no-rebase)
62 rebase=false
64 -h|--h|--he|--hel|--help)
65 usage
68 # Pass thru anything that may be meant for fetch.
69 break
71 esac
72 shift
73 done
75 error_on_no_merge_candidates () {
76 exec >&2
77 for opt
79 case "$opt" in
80 -t|--t|--ta|--tag|--tags)
81 echo "Fetching tags only, you probably meant:"
82 echo " git fetch --tags"
83 exit 1
84 esac
85 done
87 curr_branch=${curr_branch#refs/heads/}
89 echo "You asked me to pull without telling me which branch you"
90 echo "want to merge with, and 'branch.${curr_branch}.merge' in"
91 echo "your configuration file does not tell me either. Please"
92 echo "name which branch you want to merge on the command line and"
93 echo "try again (e.g. 'git pull <repository> <refspec>')."
94 echo "See git-pull(1) for details on the refspec."
95 echo
96 echo "If you often merge with the same branch, you may want to"
97 echo "configure the following variables in your configuration"
98 echo "file:"
99 echo
100 echo " branch.${curr_branch}.remote = <nickname>"
101 echo " branch.${curr_branch}.merge = <remote-ref>"
102 echo " remote.<nickname>.url = <url>"
103 echo " remote.<nickname>.fetch = <refspec>"
104 echo
105 echo "See git-config(1) for details."
106 exit 1
109 orig_head=$(git rev-parse --verify HEAD 2>/dev/null)
110 git-fetch --update-head-ok "$@" || exit 1
112 curr_head=$(git rev-parse --verify HEAD 2>/dev/null)
113 if test "$curr_head" != "$orig_head"
114 then
115 # The fetch involved updating the current branch.
117 # The working tree and the index file is still based on the
118 # $orig_head commit, but we are merging into $curr_head.
119 # First update the working tree to match $curr_head.
121 echo >&2 "Warning: fetch updated the current branch head."
122 echo >&2 "Warning: fast forwarding your working tree from"
123 echo >&2 "Warning: commit $orig_head."
124 git update-index --refresh 2>/dev/null
125 git read-tree -u -m "$orig_head" "$curr_head" ||
126 die 'Cannot fast-forward your working tree.
127 After making sure that you saved anything precious from
128 $ git diff '$orig_head'
129 output, run
130 $ git reset --hard
131 to recover.'
135 merge_head=$(sed -e '/ not-for-merge /d' \
136 -e 's/ .*//' "$GIT_DIR"/FETCH_HEAD | \
137 tr '\012' ' ')
139 case "$merge_head" in
141 case $? in
142 0) error_on_no_merge_candidates "$@";;
143 1) echo >&2 "You are not currently on a branch; you must explicitly"
144 echo >&2 "specify which branch you wish to merge:"
145 echo >&2 " git pull <remote> <branch>"
146 exit 1;;
147 *) exit $?;;
148 esac
150 ?*' '?*)
151 if test -z "$orig_head"
152 then
153 echo >&2 "Cannot merge multiple branches into empty head"
154 exit 1
157 esac
159 if test -z "$orig_head"
160 then
161 git update-ref -m "initial pull" HEAD $merge_head "" &&
162 git read-tree --reset -u HEAD || exit 1
163 exit
166 merge_name=$(git fmt-merge-msg <"$GIT_DIR/FETCH_HEAD") || exit
167 test true = "$rebase" && exec git-rebase $merge_head
168 exec git-merge $no_summary $no_commit $squash $no_ff $strategy_args \
169 "$merge_name" HEAD $merge_head