Fix an infinite loop in sq_quote_buf().
[git] / git-pull.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2005 Junio C Hamano
4 #
5 # Fetch one or more remote refs and merge it/them into the current HEAD.
6
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 . git-sh-setup
11 set_reflog_action "pull $*"
12 require_work_tree
13 cd_to_toplevel
14
15 test -z "$(git ls-files -u)" ||
16         die "You are in the middle of a conflicted merge."
17
18 strategy_args= no_summary= no_commit= squash= no_ff=
19 while :
20 do
21         case "$1" in
22         -n|--n|--no|--no-|--no-s|--no-su|--no-sum|--no-summ|\
23                 --no-summa|--no-summar|--no-summary)
24                 no_summary=-n ;;
25         --summary)
26                 no_summary=$1
27                 ;;
28         --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
29                 no_commit=--no-commit ;;
30         --c|--co|--com|--comm|--commi|--commit)
31                 no_commit=--commit ;;
32         --sq|--squ|--squa|--squas|--squash)
33                 squash=--squash ;;
34         --no-sq|--no-squ|--no-squa|--no-squas|--no-squash)
35                 squash=--no-squash ;;
36         --ff)
37                 no_ff=--ff ;;
38         --no-ff)
39                 no_ff=--no-ff ;;
40         -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
41                 --strateg=*|--strategy=*|\
42         -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
43                 case "$#,$1" in
44                 *,*=*)
45                         strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
46                 1,*)
47                         usage ;;
48                 *)
49                         strategy="$2"
50                         shift ;;
51                 esac
52                 strategy_args="${strategy_args}-s $strategy "
53                 ;;
54         -h|--h|--he|--hel|--help)
55                 usage
56                 ;;
57         *)
58                 # Pass thru anything that may be meant for fetch.
59                 break
60                 ;;
61         esac
62         shift
63 done
64
65 orig_head=$(git rev-parse --verify HEAD 2>/dev/null)
66 git-fetch --update-head-ok "$@" || exit 1
67
68 curr_head=$(git rev-parse --verify HEAD 2>/dev/null)
69 if test "$curr_head" != "$orig_head"
70 then
71         # The fetch involved updating the current branch.
72
73         # The working tree and the index file is still based on the
74         # $orig_head commit, but we are merging into $curr_head.
75         # First update the working tree to match $curr_head.
76
77         echo >&2 "Warning: fetch updated the current branch head."
78         echo >&2 "Warning: fast forwarding your working tree from"
79         echo >&2 "Warning: commit $orig_head."
80         git update-index --refresh 2>/dev/null
81         git read-tree -u -m "$orig_head" "$curr_head" ||
82                 die 'Cannot fast-forward your working tree.
83 After making sure that you saved anything precious from
84 $ git diff '$orig_head'
85 output, run
86 $ git reset --hard
87 to recover.'
88
89 fi
90
91 merge_head=$(sed -e '/  not-for-merge   /d' \
92         -e 's/  .*//' "$GIT_DIR"/FETCH_HEAD | \
93         tr '\012' ' ')
94
95 case "$merge_head" in
96 '')
97         curr_branch=$(git symbolic-ref -q HEAD)
98         case $? in
99           0) ;;
100           1) echo >&2 "You are not currently on a branch; you must explicitly"
101              echo >&2 "specify which branch you wish to merge:"
102              echo >&2 "  git pull <remote> <branch>"
103              exit 1;;
104           *) exit $?;;
105         esac
106         curr_branch=${curr_branch#refs/heads/}
107
108         echo >&2 "You asked me to pull without telling me which branch you"
109         echo >&2 "want to merge with, and 'branch.${curr_branch}.merge' in"
110         echo >&2 "your configuration file does not tell me either.  Please"
111         echo >&2 "name which branch you want to merge on the command line and"
112         echo >&2 "try again (e.g. 'git pull <repository> <refspec>')."
113         echo >&2 "See git-pull(1) for details on the refspec."
114         echo >&2
115         echo >&2 "If you often merge with the same branch, you may want to"
116         echo >&2 "configure the following variables in your configuration"
117         echo >&2 "file:"
118         echo >&2
119         echo >&2 "    branch.${curr_branch}.remote = <nickname>"
120         echo >&2 "    branch.${curr_branch}.merge = <remote-ref>"
121         echo >&2 "    remote.<nickname>.url = <url>"
122         echo >&2 "    remote.<nickname>.fetch = <refspec>"
123         echo >&2
124         echo >&2 "See git-config(1) for details."
125         exit 1
126         ;;
127 ?*' '?*)
128         if test -z "$orig_head"
129         then
130                 echo >&2 "Cannot merge multiple branches into empty head"
131                 exit 1
132         fi
133         ;;
134 esac
135
136 if test -z "$orig_head"
137 then
138         git update-ref -m "initial pull" HEAD $merge_head "" &&
139         git read-tree --reset -u HEAD || exit 1
140         exit
141 fi
142
143 merge_name=$(git fmt-merge-msg <"$GIT_DIR/FETCH_HEAD") || exit
144 exec git-merge $no_summary $no_commit $squash $no_ff $strategy_args \
145         "$merge_name" HEAD $merge_head