Fix git-fetch while on detached HEAD not to give needlessly alarming errors
[git/jnareb-git.git] / git-pull.sh
blob959261757c1fd8df50f7894ba449b76d621f3c45
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] [-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
15 test -z "$(git ls-files -u)" ||
16 die "You are in the middle of a conflicted merge."
18 strategy_args= no_summary= no_commit= squash=
19 while case "$#,$1" in 0) break ;; *,-*) ;; *) break ;; esac
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 --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
26 no_commit=--no-commit ;;
27 --sq|--squ|--squa|--squas|--squash)
28 squash=--squash ;;
29 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
30 --strateg=*|--strategy=*|\
31 -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
32 case "$#,$1" in
33 *,*=*)
34 strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
35 1,*)
36 usage ;;
38 strategy="$2"
39 shift ;;
40 esac
41 strategy_args="${strategy_args}-s $strategy "
43 -h|--h|--he|--hel|--help)
44 usage
46 -*)
47 # Pass thru anything that is meant for fetch.
48 break
50 esac
51 shift
52 done
54 orig_head=$(git-rev-parse --verify HEAD 2>/dev/null)
55 git-fetch --update-head-ok "$@" || exit 1
57 curr_head=$(git-rev-parse --verify HEAD 2>/dev/null)
58 if test "$curr_head" != "$orig_head"
59 then
60 # The fetch involved updating the current branch.
62 # The working tree and the index file is still based on the
63 # $orig_head commit, but we are merging into $curr_head.
64 # First update the working tree to match $curr_head.
66 echo >&2 "Warning: fetch updated the current branch head."
67 echo >&2 "Warning: fast forwarding your working tree from"
68 echo >&2 "Warning: commit $orig_head."
69 git-update-index --refresh 2>/dev/null
70 git-read-tree -u -m "$orig_head" "$curr_head" ||
71 die 'Cannot fast-forward your working tree.
72 After making sure that you saved anything precious from
73 $ git diff '$orig_head'
74 output, run
75 $ git reset --hard
76 to recover.'
80 merge_head=$(sed -e '/ not-for-merge /d' \
81 -e 's/ .*//' "$GIT_DIR"/FETCH_HEAD | \
82 tr '\012' ' ')
84 case "$merge_head" in
85 '')
86 curr_branch=$(git-symbolic-ref HEAD | \
87 sed -e 's|^refs/heads/||')
88 echo >&2 "Warning: No merge candidate found because value of config option
89 \"branch.${curr_branch}.merge\" does not match any remote branch fetched."
90 echo >&2 "No changes."
91 exit 0
93 ?*' '?*)
94 if test -z "$orig_head"
95 then
96 echo >&2 "Cannot merge multiple branches into empty head"
97 exit 1
100 esac
102 if test -z "$orig_head"
103 then
104 git-update-ref -m "initial pull" HEAD $merge_head "" &&
105 git-read-tree --reset -u HEAD || exit 1
106 exit
109 merge_name=$(git-fmt-merge-msg <"$GIT_DIR/FETCH_HEAD") || exit
110 exec git-merge $no_summary $no_commit $squash $strategy_args \
111 "$merge_name" HEAD $merge_head