]> git.ipfire.org Git - thirdparty/git.git/blame - git-pull.sh
remote.c: refactor creation of new dst ref
[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
806f36d4
FK
7USAGE='[-n | --no-summary] [--no-commit] [-s strategy]... [<fetch-options>] <repo> <head>...'
8LONG_USAGE='Fetch one or more remote refs and merge it/them into the current HEAD.'
533b7039 9SUBDIRECTORY_OK=Yes
ae2b0f15 10. git-sh-setup
f9474132 11set_reflog_action "pull $*"
7eff28a9 12require_work_tree
533b7039 13cd_to_toplevel
b10ac50f 14
d1014a17 15test -z "$(git ls-files -u)" ||
533b7039 16 die "You are in the middle of a conflicted merge."
d1014a17 17
7d0c6887 18strategy_args= no_summary= no_commit= squash=
60fb5b2c
JH
19while case "$#,$1" in 0) break ;; *,-*) ;; *) break ;; esac
20do
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 ;;
51e7ecf4
AR
25 --summary)
26 no_summary=$1
27 ;;
123ee3ca
JH
28 --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
29 no_commit=--no-commit ;;
7d0c6887
JH
30 --sq|--squ|--squa|--squas|--squash)
31 squash=--squash ;;
60fb5b2c
JH
32 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
33 --strateg=*|--strategy=*|\
34 -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
35 case "$#,$1" in
36 *,*=*)
8096fae7 37 strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
60fb5b2c
JH
38 1,*)
39 usage ;;
40 *)
41 strategy="$2"
42 shift ;;
43 esac
44 strategy_args="${strategy_args}-s $strategy "
45 ;;
93d69d86
JL
46 -h|--h|--he|--hel|--help)
47 usage
48 ;;
60fb5b2c 49 -*)
619e5a0e
JH
50 # Pass thru anything that is meant for fetch.
51 break
60fb5b2c
JH
52 ;;
53 esac
54 shift
55done
56
d09e79cb 57orig_head=$(git-rev-parse --verify HEAD 2>/dev/null)
f9474132 58git-fetch --update-head-ok "$@" || exit 1
b10ac50f 59
d09e79cb 60curr_head=$(git-rev-parse --verify HEAD 2>/dev/null)
b10ac50f
JH
61if test "$curr_head" != "$orig_head"
62then
63 # The fetch involved updating the current branch.
64
65 # The working tree and the index file is still based on the
66 # $orig_head commit, but we are merging into $curr_head.
67 # First update the working tree to match $curr_head.
68
69 echo >&2 "Warning: fetch updated the current branch head."
cf46e7b8 70 echo >&2 "Warning: fast forwarding your working tree from"
a057f806 71 echo >&2 "Warning: commit $orig_head."
cf46e7b8 72 git-update-index --refresh 2>/dev/null
b10ac50f 73 git-read-tree -u -m "$orig_head" "$curr_head" ||
8323124a
JH
74 die 'Cannot fast-forward your working tree.
75After making sure that you saved anything precious from
76$ git diff '$orig_head'
77output, run
78$ git reset --hard
79to recover.'
80
b10ac50f
JH
81fi
82
05dd8e2e
JH
83merge_head=$(sed -e '/ not-for-merge /d' \
84 -e 's/ .*//' "$GIT_DIR"/FETCH_HEAD | \
85 tr '\012' ' ')
e0bfc81e
JH
86
87case "$merge_head" in
521003ff 88'')
a74b1706
JK
89 curr_branch=$(git-symbolic-ref -q HEAD)
90 case $? in
91 0) ;;
92 1) echo >&2 "You are not currently on a branch; you must explicitly"
93 echo >&2 "specify which branch you wish to merge:"
94 echo >&2 " git pull <remote> <branch>"
95 exit 1;;
96 *) exit $?;;
97 esac
98 curr_branch=${curr_branch#refs/heads/}
99
4363dfbe
JW
100 echo >&2 "Warning: No merge candidate found because value of config option
101 \"branch.${curr_branch}.merge\" does not match any remote branch fetched."
521003ff
JH
102 echo >&2 "No changes."
103 exit 0
104 ;;
60fb5b2c 105?*' '?*)
d09e79cb
LT
106 if test -z "$orig_head"
107 then
108 echo >&2 "Cannot merge multiple branches into empty head"
109 exit 1
110 fi
60fb5b2c
JH
111 ;;
112esac
113
d09e79cb
LT
114if test -z "$orig_head"
115then
116 git-update-ref -m "initial pull" HEAD $merge_head "" &&
117 git-read-tree --reset -u HEAD || exit 1
118 exit
119fi
120
86378b32 121merge_name=$(git-fmt-merge-msg <"$GIT_DIR/FETCH_HEAD") || exit
bf699582 122exec git-merge $no_summary $no_commit $squash $strategy_args \
7d0c6887 123 "$merge_name" HEAD $merge_head