git-checkout.shon commit checkout: automerge local changes while switching branches. (19205ac)
   1#!/bin/sh
   2
   3USAGE='[-f] [-b <new_branch>] [<branch>] [<paths>...]'
   4SUBDIRECTORY_OK=Sometimes
   5. git-sh-setup
   6
   7old=$(git-rev-parse HEAD)
   8new=
   9force=
  10branch=
  11newbranch=
  12while [ "$#" != "0" ]; do
  13    arg="$1"
  14    shift
  15    case "$arg" in
  16        "-b")
  17                newbranch="$1"
  18                shift
  19                [ -z "$newbranch" ] &&
  20                        die "git checkout: -b needs a branch name"
  21                [ -e "$GIT_DIR/refs/heads/$newbranch" ] &&
  22                        die "git checkout: branch $newbranch already exists"
  23                git-check-ref-format "heads/$newbranch" ||
  24                        die "we do not like '$newbranch' as a branch name."
  25                ;;
  26        "-f")
  27                force=1
  28                ;;
  29        --)
  30                break
  31                ;;
  32        -*)
  33                usage
  34                ;;
  35        *)
  36                if rev=$(git-rev-parse --verify "$arg^0" 2>/dev/null)
  37                then
  38                        if [ -z "$rev" ]; then
  39                                echo "unknown flag $arg"
  40                                exit 1
  41                        fi
  42                        new="$rev"
  43                        if [ -f "$GIT_DIR/refs/heads/$arg" ]; then
  44                                branch="$arg"
  45                        fi
  46                elif rev=$(git-rev-parse --verify "$arg^{tree}" 2>/dev/null)
  47                then
  48                        # checking out selected paths from a tree-ish.
  49                        new="$rev"
  50                        branch=
  51                else
  52                        new=
  53                        branch=
  54                        set x "$arg" "$@"
  55                        shift
  56                fi
  57                break
  58                ;;
  59    esac
  60done
  61
  62# The behaviour of the command with and without explicit path
  63# parameters is quite different.
  64#
  65# Without paths, we are checking out everything in the work tree,
  66# possibly switching branches.  This is the traditional behaviour.
  67#
  68# With paths, we are _never_ switching branch, but checking out
  69# the named paths from either index (when no rev is given),
  70# or the named tree-ish (when rev is given).
  71
  72if test "$#" -ge 1
  73then
  74        if test '' != "$newbranch$force"
  75        then
  76                die "updating paths and switching branches or forcing are incompatible."
  77        fi
  78        if test '' != "$new"
  79        then
  80                # from a specific tree-ish; note that this is for
  81                # rescuing paths and is never meant to remove what
  82                # is not in the named tree-ish.
  83                git-ls-tree --full-name -r "$new" "$@" |
  84                git-update-index --index-info || exit $?
  85        fi
  86        git-checkout-index -f -u -- "$@"
  87        exit $?
  88else
  89        # Make sure we did not fall back on $arg^{tree} codepath
  90        # since we are not checking out from an arbitrary tree-ish,
  91        # but switching branches.
  92        if test '' != "$new"
  93        then
  94                git-rev-parse --verify "$new^{commit}" >/dev/null 2>&1 ||
  95                die "Cannot switch branch to a non-commit."
  96        fi
  97fi
  98
  99# We are switching branches and checking out trees, so
 100# we *NEED* to be at the toplevel.
 101cdup=$(git-rev-parse --show-cdup)
 102if test ! -z "$cdup"
 103then
 104        cd "$cdup"
 105fi
 106
 107[ -z "$new" ] && new=$old
 108
 109# If we don't have an old branch that we're switching to,
 110# and we don't have a new branch name for the target we
 111# are switching to, then we'd better just be checking out
 112# what we already had
 113
 114[ -z "$branch$newbranch" ] &&
 115        [ "$new" != "$old" ] &&
 116        die "git checkout: you need to specify a new branch name"
 117
 118if [ "$force" ]
 119then
 120    git-read-tree --reset $new &&
 121        git-checkout-index -q -f -u -a
 122else
 123    git-update-index --refresh >/dev/null
 124    git-read-tree -m -u $old $new || (
 125        echo >&2 -n "Try automerge [y/N]? "
 126        read yesno
 127        case "$yesno" in [yY]*) ;; *) exit 1 ;; esac
 128
 129        # NEEDSWORK: We may want to reset the index from the $new for
 130        # these paths after the automerge happens, but it is not done
 131        # yet.  Probably we need to leave unmerged ones alone, and
 132        # yank the object name & mode from $new for cleanly merged
 133        # paths and stuff them in the index.
 134
 135        names=`git diff-files --name-only`
 136        case "$names" in
 137        '')     ;;
 138        *)
 139                echo "$names" | git update-index --remove --stdin ;;
 140        esac
 141
 142        work=`git write-tree` &&
 143        git read-tree -m -u $old $work $new || exit
 144        if result=`git write-tree 2>/dev/null`
 145        then
 146            echo >&2 "Trivially automerged." ;# can this even happen?
 147            exit 0
 148        fi
 149        git merge-index -o git-merge-one-file -a
 150    )
 151fi
 152
 153# 
 154# Switch the HEAD pointer to the new branch if we
 155# checked out a branch head, and remove any potential
 156# old MERGE_HEAD's (subsequent commits will clearly not
 157# be based on them, since we re-set the index)
 158#
 159if [ "$?" -eq 0 ]; then
 160        if [ "$newbranch" ]; then
 161                leading=`expr "refs/heads/$newbranch" : '\(.*\)/'` &&
 162                mkdir -p "$GIT_DIR/$leading" &&
 163                echo $new >"$GIT_DIR/refs/heads/$newbranch" || exit
 164                branch="$newbranch"
 165        fi
 166        [ "$branch" ] &&
 167        GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD "refs/heads/$branch"
 168        rm -f "$GIT_DIR/MERGE_HEAD"
 169else
 170        exit 1
 171fi