]> git.ipfire.org Git - thirdparty/git.git/blame - git-pull.sh
Make usage strings dash-less
[thirdparty/git.git] / git-pull.sh
CommitLineData
839a7a06
LT
1#!/bin/sh
2#
521003ff
JH
3# Copyright (c) 2005 Junio C Hamano
4#
5# Fetch one or more remote refs and merge it/them into the current HEAD.
6
d8abe148 7USAGE='[-n | --no-stat] [--[no-]commit] [--[no-]squash] [--[no-]ff] [-s strategy]... [<fetch-options>] <repo> <head>...'
806f36d4 8LONG_USAGE='Fetch one or more remote refs and merge it/them into the current HEAD.'
533b7039 9SUBDIRECTORY_OK=Yes
8f321a39 10OPTIONS_SPEC=
ae2b0f15 11. git-sh-setup
f9474132 12set_reflog_action "pull $*"
7eff28a9 13require_work_tree
533b7039 14cd_to_toplevel
b10ac50f 15
d1014a17 16test -z "$(git ls-files -u)" ||
533b7039 17 die "You are in the middle of a conflicted merge."
d1014a17 18
efb779f8 19strategy_args= no_stat= no_commit= squash= no_ff= log_arg=
cd67e4d4
JS
20curr_branch=$(git symbolic-ref -q HEAD)
21curr_branch_short=$(echo "$curr_branch" | sed "s|refs/heads/||")
22rebase=$(git config --bool branch.$curr_branch_short.rebase)
822f7c73 23while :
60fb5b2c
JH
24do
25 case "$1" in
d8abe148
SG
26 -n|--no-stat|--no-summary)
27 no_stat=-n ;;
28 --stat|--summary)
29 no_stat=$1 ;;
efb779f8
SG
30 --log|--no-log)
31 log_arg=$1 ;;
123ee3ca
JH
32 --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
33 no_commit=--no-commit ;;
5072a323
LH
34 --c|--co|--com|--comm|--commi|--commit)
35 no_commit=--commit ;;
7d0c6887
JH
36 --sq|--squ|--squa|--squas|--squash)
37 squash=--squash ;;
5072a323
LH
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 ;;
60fb5b2c
JH
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 *,*=*)
8096fae7 49 strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
60fb5b2c
JH
50 1,*)
51 usage ;;
52 *)
53 strategy="$2"
54 shift ;;
55 esac
56 strategy_args="${strategy_args}-s $strategy "
57 ;;
cd67e4d4
JS
58 -r|--r|--re|--reb|--reba|--rebas|--rebase)
59 rebase=true
60 ;;
61 --no-r|--no-re|--no-reb|--no-reba|--no-rebas|--no-rebase)
62 rebase=false
63 ;;
93d69d86
JL
64 -h|--h|--he|--hel|--help)
65 usage
66 ;;
822f7c73
DK
67 *)
68 # Pass thru anything that may be meant for fetch.
619e5a0e 69 break
60fb5b2c
JH
70 ;;
71 esac
72 shift
73done
74
441ed413
JH
75error_on_no_merge_candidates () {
76 exec >&2
77 for opt
78 do
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
86
87 curr_branch=${curr_branch#refs/heads/}
88
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
107}
108
c85c7927 109test true = "$rebase" && {
f9189cf8
JS
110 git update-index --refresh &&
111 git diff-files --quiet &&
112 git diff-index --cached --quiet HEAD -- ||
113 die "refusing to pull with rebase: your working tree is not up-to-date"
114
c85c7927
JS
115 . git-parse-remote &&
116 origin="$1"
117 test -z "$origin" && origin=$(get_default_remote)
118 reflist="$(get_remote_refs_for_fetch "$@" 2>/dev/null |
119 sed "s|refs/heads/\(.*\):|\1|")" &&
120 oldremoteref="$(git rev-parse --verify \
121 "refs/remotes/$origin/$reflist" 2>/dev/null)"
122}
5be60078 123orig_head=$(git rev-parse --verify HEAD 2>/dev/null)
1b1dd23f 124git fetch --update-head-ok "$@" || exit 1
b10ac50f 125
5be60078 126curr_head=$(git rev-parse --verify HEAD 2>/dev/null)
b10ac50f
JH
127if test "$curr_head" != "$orig_head"
128then
129 # The fetch involved updating the current branch.
130
131 # The working tree and the index file is still based on the
132 # $orig_head commit, but we are merging into $curr_head.
133 # First update the working tree to match $curr_head.
134
135 echo >&2 "Warning: fetch updated the current branch head."
cf46e7b8 136 echo >&2 "Warning: fast forwarding your working tree from"
a057f806 137 echo >&2 "Warning: commit $orig_head."
5be60078
JH
138 git update-index --refresh 2>/dev/null
139 git read-tree -u -m "$orig_head" "$curr_head" ||
8323124a
JH
140 die 'Cannot fast-forward your working tree.
141After making sure that you saved anything precious from
142$ git diff '$orig_head'
143output, run
144$ git reset --hard
145to recover.'
146
b10ac50f
JH
147fi
148
05dd8e2e
JH
149merge_head=$(sed -e '/ not-for-merge /d' \
150 -e 's/ .*//' "$GIT_DIR"/FETCH_HEAD | \
151 tr '\012' ' ')
e0bfc81e
JH
152
153case "$merge_head" in
521003ff 154'')
a74b1706 155 case $? in
441ed413
JH
156 0) error_on_no_merge_candidates "$@";;
157 1) echo >&2 "You are not currently on a branch; you must explicitly"
158 echo >&2 "specify which branch you wish to merge:"
159 echo >&2 " git pull <remote> <branch>"
160 exit 1;;
161 *) exit $?;;
a74b1706 162 esac
521003ff 163 ;;
60fb5b2c 164?*' '?*)
d09e79cb
LT
165 if test -z "$orig_head"
166 then
167 echo >&2 "Cannot merge multiple branches into empty head"
168 exit 1
169 fi
60fb5b2c
JH
170 ;;
171esac
172
d09e79cb
LT
173if test -z "$orig_head"
174then
5be60078
JH
175 git update-ref -m "initial pull" HEAD $merge_head "" &&
176 git read-tree --reset -u HEAD || exit 1
d09e79cb
LT
177 exit
178fi
179
efb779f8 180merge_name=$(git fmt-merge-msg $log_arg <"$GIT_DIR/FETCH_HEAD") || exit
c85c7927 181test true = "$rebase" &&
0d2dd191
JS
182 exec git-rebase $strategy_args --onto $merge_head \
183 ${oldremoteref:-$merge_head}
efb779f8 184exec git-merge $no_stat $no_commit $squash $no_ff $log_arg $strategy_args \
7d0c6887 185 "$merge_name" HEAD $merge_head