]> git.ipfire.org Git - thirdparty/git.git/blame - git-pull.sh
builtin-blame.c: remove unneeded memclr()
[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
5072a323 7USAGE='[-n | --no-summary] [--[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
5072a323 19strategy_args= no_summary= no_commit= squash= no_ff=
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
26 -n|--n|--no|--no-|--no-s|--no-su|--no-sum|--no-summ|\
27 --no-summa|--no-summar|--no-summary)
28 no_summary=-n ;;
51e7ecf4
AR
29 --summary)
30 no_summary=$1
31 ;;
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
5be60078 75orig_head=$(git rev-parse --verify HEAD 2>/dev/null)
f9474132 76git-fetch --update-head-ok "$@" || exit 1
b10ac50f 77
5be60078 78curr_head=$(git rev-parse --verify HEAD 2>/dev/null)
b10ac50f
JH
79if test "$curr_head" != "$orig_head"
80then
81 # The fetch involved updating the current branch.
82
83 # The working tree and the index file is still based on the
84 # $orig_head commit, but we are merging into $curr_head.
85 # First update the working tree to match $curr_head.
86
87 echo >&2 "Warning: fetch updated the current branch head."
cf46e7b8 88 echo >&2 "Warning: fast forwarding your working tree from"
a057f806 89 echo >&2 "Warning: commit $orig_head."
5be60078
JH
90 git update-index --refresh 2>/dev/null
91 git read-tree -u -m "$orig_head" "$curr_head" ||
8323124a
JH
92 die 'Cannot fast-forward your working tree.
93After making sure that you saved anything precious from
94$ git diff '$orig_head'
95output, run
96$ git reset --hard
97to recover.'
98
b10ac50f
JH
99fi
100
05dd8e2e
JH
101merge_head=$(sed -e '/ not-for-merge /d' \
102 -e 's/ .*//' "$GIT_DIR"/FETCH_HEAD | \
103 tr '\012' ' ')
e0bfc81e
JH
104
105case "$merge_head" in
521003ff 106'')
a74b1706
JK
107 case $? in
108 0) ;;
109 1) echo >&2 "You are not currently on a branch; you must explicitly"
110 echo >&2 "specify which branch you wish to merge:"
111 echo >&2 " git pull <remote> <branch>"
112 exit 1;;
113 *) exit $?;;
114 esac
115 curr_branch=${curr_branch#refs/heads/}
116
8fc293cb
FMQ
117 echo >&2 "You asked me to pull without telling me which branch you"
118 echo >&2 "want to merge with, and 'branch.${curr_branch}.merge' in"
119 echo >&2 "your configuration file does not tell me either. Please"
120 echo >&2 "name which branch you want to merge on the command line and"
121 echo >&2 "try again (e.g. 'git pull <repository> <refspec>')."
122 echo >&2 "See git-pull(1) for details on the refspec."
123 echo >&2
124 echo >&2 "If you often merge with the same branch, you may want to"
125 echo >&2 "configure the following variables in your configuration"
126 echo >&2 "file:"
127 echo >&2
128 echo >&2 " branch.${curr_branch}.remote = <nickname>"
129 echo >&2 " branch.${curr_branch}.merge = <remote-ref>"
130 echo >&2 " remote.<nickname>.url = <url>"
131 echo >&2 " remote.<nickname>.fetch = <refspec>"
132 echo >&2
133 echo >&2 "See git-config(1) for details."
134 exit 1
521003ff 135 ;;
60fb5b2c 136?*' '?*)
d09e79cb
LT
137 if test -z "$orig_head"
138 then
139 echo >&2 "Cannot merge multiple branches into empty head"
140 exit 1
141 fi
60fb5b2c
JH
142 ;;
143esac
144
d09e79cb
LT
145if test -z "$orig_head"
146then
5be60078
JH
147 git update-ref -m "initial pull" HEAD $merge_head "" &&
148 git read-tree --reset -u HEAD || exit 1
d09e79cb
LT
149 exit
150fi
151
5be60078 152merge_name=$(git fmt-merge-msg <"$GIT_DIR/FETCH_HEAD") || exit
cd67e4d4 153test true = "$rebase" && exec git-rebase $merge_head
5072a323 154exec git-merge $no_summary $no_commit $squash $no_ff $strategy_args \
7d0c6887 155 "$merge_name" HEAD $merge_head