git-pull.shon commit Add tests for git push'es mirror mode (8e806ad)
   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
   7USAGE='[-n | --no-summary] [--[no-]commit] [--[no-]squash] [--[no-]ff] [-s strategy]... [<fetch-options>] <repo> <head>...'
   8LONG_USAGE='Fetch one or more remote refs and merge it/them into the current HEAD.'
   9SUBDIRECTORY_OK=Yes
  10. git-sh-setup
  11set_reflog_action "pull $*"
  12require_work_tree
  13cd_to_toplevel
  14
  15test -z "$(git ls-files -u)" ||
  16        die "You are in the middle of a conflicted merge."
  17
  18strategy_args= no_summary= no_commit= squash= no_ff=
  19while :
  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 ;;
  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
  63done
  64
  65orig_head=$(git rev-parse --verify HEAD 2>/dev/null)
  66git-fetch --update-head-ok "$@" || exit 1
  67
  68curr_head=$(git rev-parse --verify HEAD 2>/dev/null)
  69if test "$curr_head" != "$orig_head"
  70then
  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.
  83After making sure that you saved anything precious from
  84$ git diff '$orig_head'
  85output, run
  86$ git reset --hard
  87to recover.'
  88
  89fi
  90
  91merge_head=$(sed -e '/  not-for-merge   /d' \
  92        -e 's/  .*//' "$GIT_DIR"/FETCH_HEAD | \
  93        tr '\012' ' ')
  94
  95case "$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        ;;
 134esac
 135
 136if test -z "$orig_head"
 137then
 138        git update-ref -m "initial pull" HEAD $merge_head "" &&
 139        git read-tree --reset -u HEAD || exit 1
 140        exit
 141fi
 142
 143merge_name=$(git fmt-merge-msg <"$GIT_DIR/FETCH_HEAD") || exit
 144exec git-merge $no_summary $no_commit $squash $no_ff $strategy_args \
 145        "$merge_name" HEAD $merge_head