git-checkout.shon commit git-check-ref-format: reject funny ref names. (03feddd)
   1#!/bin/sh
   2. git-sh-setup || die "Not a git archive"
   3
   4old=$(git-rev-parse HEAD)
   5new=
   6force=
   7branch=
   8newbranch=
   9while [ "$#" != "0" ]; do
  10    arg="$1"
  11    shift
  12    case "$arg" in
  13        "-b")
  14                newbranch="$1"
  15                shift
  16                [ -z "$newbranch" ] &&
  17                        die "git checkout: -b needs a branch name"
  18                [ -e "$GIT_DIR/refs/heads/$newbranch" ] &&
  19                        die "git checkout: branch $newbranch already exists"
  20                git-check-ref-format "heads/$newbranch" ||
  21                        die "we do not like '$newbranch' as a branch name."
  22                ;;
  23        "-f")
  24                force=1
  25                ;;
  26        *)
  27                rev=$(git-rev-parse --verify "$arg^0" 2>/dev/null) ||
  28                        die "I don't know any '$arg'."
  29                if [ -z "$rev" ]; then
  30                        echo "unknown flag $arg"
  31                        exit 1
  32                fi
  33                if [ "$new" ]; then
  34                        echo "Multiple revisions?"
  35                        exit 1
  36                fi
  37                new="$rev"
  38                if [ -f "$GIT_DIR/refs/heads/$arg" ]; then
  39                        branch="$arg"
  40                fi
  41                ;;
  42    esac
  43done
  44[ -z "$new" ] && new=$old
  45
  46#
  47# If we don't have an old branch that we're switching to,
  48# and we don't have a new branch name for the target we
  49# are switching to, then we'd better just be checking out
  50# what we already had
  51#
  52[ -z "$branch$newbranch" ] &&
  53        [ "$new" != "$old" ] &&
  54        die "git checkout: you need to specify a new branch name"
  55
  56if [ "$force" ]
  57then
  58    git-read-tree --reset $new &&
  59        git-checkout-index -q -f -u -a
  60else
  61    git-update-index --refresh >/dev/null
  62    git-read-tree -m -u $old $new
  63fi
  64
  65# 
  66# Switch the HEAD pointer to the new branch if it we
  67# checked out a branch head, and remove any potential
  68# old MERGE_HEAD's (subsequent commits will clearly not
  69# be based on them, since we re-set the index)
  70#
  71if [ "$?" -eq 0 ]; then
  72        if [ "$newbranch" ]; then
  73                echo $new > "$GIT_DIR/refs/heads/$newbranch"
  74                branch="$newbranch"
  75        fi
  76        [ "$branch" ] &&
  77        GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD "refs/heads/$branch"
  78        rm -f "$GIT_DIR/MERGE_HEAD"
  79else
  80        exit 1
  81fi