eb28b291b6d1b7d77306bb3437b9955671e45d3a
   1#!/bin/sh
   2
   3USAGE='[-f] [-b <new_branch>] [-m] [<branch>] [<paths>...]'
   4SUBDIRECTORY_OK=Sometimes
   5. git-sh-setup
   6
   7old_name=HEAD
   8old=$(git-rev-parse --verify $old_name 2>/dev/null)
   9new=
  10new_name=
  11force=
  12branch=
  13newbranch=
  14newbranch_log=
  15merge=
  16while [ "$#" != "0" ]; do
  17    arg="$1"
  18    shift
  19    case "$arg" in
  20        "-b")
  21                newbranch="$1"
  22                shift
  23                [ -z "$newbranch" ] &&
  24                        die "git checkout: -b needs a branch name"
  25                git-show-ref --verify --quiet -- "refs/heads/$newbranch" &&
  26                        die "git checkout: branch $newbranch already exists"
  27                git-check-ref-format "heads/$newbranch" ||
  28                        die "git checkout: we do not like '$newbranch' as a branch name."
  29                ;;
  30        "-l")
  31                newbranch_log=1
  32                ;;
  33        "-f")
  34                force=1
  35                ;;
  36        -m)
  37                merge=1
  38                ;;
  39        --)
  40                break
  41                ;;
  42        -*)
  43                usage
  44                ;;
  45        *)
  46                if rev=$(git-rev-parse --verify "$arg^0" 2>/dev/null)
  47                then
  48                        if [ -z "$rev" ]; then
  49                                echo "unknown flag $arg"
  50                                exit 1
  51                        fi
  52                        new="$rev"
  53                        new_name="$arg^0"
  54                        if git-show-ref --verify --quiet -- "refs/heads/$arg"
  55                        then
  56                                branch="$arg"
  57                        fi
  58                elif rev=$(git-rev-parse --verify "$arg^{tree}" 2>/dev/null)
  59                then
  60                        # checking out selected paths from a tree-ish.
  61                        new="$rev"
  62                        new_name="$arg^{tree}"
  63                        branch=
  64                else
  65                        new=
  66                        new_name=
  67                        branch=
  68                        set x "$arg" "$@"
  69                        shift
  70                fi
  71                case "$1" in
  72                --)
  73                        shift ;;
  74                esac
  75                break
  76                ;;
  77    esac
  78done
  79
  80case "$force$merge" in
  8111)
  82        die "git checkout: -f and -m are incompatible"
  83esac
  84
  85# The behaviour of the command with and without explicit path
  86# parameters is quite different.
  87#
  88# Without paths, we are checking out everything in the work tree,
  89# possibly switching branches.  This is the traditional behaviour.
  90#
  91# With paths, we are _never_ switching branch, but checking out
  92# the named paths from either index (when no rev is given),
  93# or the named tree-ish (when rev is given).
  94
  95if test "$#" -ge 1
  96then
  97        hint=
  98        if test "$#" -eq 1
  99        then
 100                hint="
 101Did you intend to checkout '$@' which can not be resolved as commit?"
 102        fi
 103        if test '' != "$newbranch$force$merge"
 104        then
 105                die "git checkout: updating paths is incompatible with switching branches/forcing$hint"
 106        fi
 107        if test '' != "$new"
 108        then
 109                # from a specific tree-ish; note that this is for
 110                # rescuing paths and is never meant to remove what
 111                # is not in the named tree-ish.
 112                git-ls-tree --full-name -r "$new" "$@" |
 113                git-update-index --index-info || exit $?
 114        fi
 115        git-checkout-index -f -u -- "$@"
 116        exit $?
 117else
 118        # Make sure we did not fall back on $arg^{tree} codepath
 119        # since we are not checking out from an arbitrary tree-ish,
 120        # but switching branches.
 121        if test '' != "$new"
 122        then
 123                git-rev-parse --verify "$new^{commit}" >/dev/null 2>&1 ||
 124                die "Cannot switch branch to a non-commit."
 125        fi
 126fi
 127
 128# We are switching branches and checking out trees, so
 129# we *NEED* to be at the toplevel.
 130cdup=$(git-rev-parse --show-cdup)
 131if test ! -z "$cdup"
 132then
 133        cd "$cdup"
 134fi
 135
 136[ -z "$new" ] && new=$old && new_name="$old_name"
 137
 138# If we don't have an old branch that we're switching to,
 139# and we don't have a new branch name for the target we
 140# are switching to, then we'd better just be checking out
 141# what we already had
 142
 143[ -z "$branch$newbranch" ] &&
 144        [ "$new" != "$old" ] &&
 145        die "git checkout: to checkout the requested commit you need to specify 
 146              a name for a new branch which is created and switched to"
 147
 148if [ "X$old" = X ]
 149then
 150        echo "warning: You do not appear to currently be on a branch." >&2
 151        echo "warning: Forcing checkout of $new_name." >&2
 152        force=1
 153fi
 154
 155if [ "$force" ]
 156then
 157    git-read-tree --reset -u $new
 158else
 159    git-update-index --refresh >/dev/null
 160    merge_error=$(git-read-tree -m -u $old $new 2>&1) || (
 161        case "$merge" in
 162        '')
 163                echo >&2 "$merge_error"
 164                exit 1 ;;
 165        esac
 166
 167        # Match the index to the working tree, and do a three-way.
 168        git diff-files --name-only | git update-index --remove --stdin &&
 169        work=`git write-tree` &&
 170        git read-tree --reset -u $new &&
 171        git read-tree -m -u --aggressive $old $new $work || exit
 172
 173        if result=`git write-tree 2>/dev/null`
 174        then
 175            echo >&2 "Trivially automerged."
 176        else
 177            git merge-index -o git-merge-one-file -a
 178        fi
 179
 180        # Do not register the cleanly merged paths in the index yet.
 181        # this is not a real merge before committing, but just carrying
 182        # the working tree changes along.
 183        unmerged=`git ls-files -u`
 184        git read-tree --reset $new
 185        case "$unmerged" in
 186        '')     ;;
 187        *)
 188                (
 189                        z40=0000000000000000000000000000000000000000
 190                        echo "$unmerged" |
 191                        sed -e 's/^[0-7]* [0-9a-f]* /'"0 $z40 /"
 192                        echo "$unmerged"
 193                ) | git update-index --index-info
 194                ;;
 195        esac
 196        exit 0
 197    )
 198    saved_err=$?
 199    if test "$saved_err" = 0
 200    then
 201        test "$new" = "$old" || git diff-index --name-status "$new"
 202    fi
 203    (exit $saved_err)
 204fi
 205
 206# 
 207# Switch the HEAD pointer to the new branch if we
 208# checked out a branch head, and remove any potential
 209# old MERGE_HEAD's (subsequent commits will clearly not
 210# be based on them, since we re-set the index)
 211#
 212if [ "$?" -eq 0 ]; then
 213        if [ "$newbranch" ]; then
 214                if [ "$newbranch_log" ]; then
 215                        mkdir -p $(dirname "$GIT_DIR/logs/refs/heads/$newbranch")
 216                        touch "$GIT_DIR/logs/refs/heads/$newbranch"
 217                fi
 218                git-update-ref -m "checkout: Created from $new_name" "refs/heads/$newbranch" $new || exit
 219                branch="$newbranch"
 220        fi
 221        [ "$branch" ] &&
 222        GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD "refs/heads/$branch"
 223        rm -f "$GIT_DIR/MERGE_HEAD"
 224else
 225        exit 1
 226fi