]> git.ipfire.org Git - thirdparty/git.git/blame - git-checkout.sh
[PATCH] fetch.c: Remove redundant TO_FETCH flag
[thirdparty/git.git] / git-checkout.sh
CommitLineData
303e5f4c 1#!/bin/sh
215a7ad1 2. git-sh-setup || die "Not a git archive"
b33e9666 3
303e5f4c 4old=$(git-rev-parse HEAD)
e8b11749 5new=
a79944d7 6force=
e8b11749 7branch=
91dcdfd3 8newbranch=
e8b11749
LT
9while [ "$#" != "0" ]; do
10 arg="$1"
11 shift
12 case "$arg" in
91dcdfd3
LT
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 ;;
303e5f4c 21 "-f")
e8b11749 22 force=1
303e5f4c
LT
23 ;;
24 *)
6b82d16b 25 rev=$(git-rev-parse --verify "$arg^0" 2>/dev/null) ||
3b944aac 26 die "I don't know any '$arg'."
e8b11749
LT
27 if [ -z "$rev" ]; then
28 echo "unknown flag $arg"
29 exit 1
30 fi
31 if [ "$new" ]; then
32 echo "Multiple revisions?"
33 exit 1
34 fi
35 new="$rev"
9661c256 36 if [ -f "$GIT_DIR/refs/heads/$arg" ]; then
e8b11749
LT
37 branch="$arg"
38 fi
39 ;;
303e5f4c 40 esac
303e5f4c 41done
dc148411 42[ -z "$new" ] && new=$old
303e5f4c 43
91dcdfd3
LT
44#
45# If we don't have an old branch that we're switching to,
46# and we don't have a new branch name for the target we
47# are switching to, then we'd better just be checking out
48# what we already had
49#
50[ -z "$branch$newbranch" ] &&
51 [ "$new" != "$old" ] &&
52 die "git checkout: you need to specify a new branch name"
53
a79944d7 54if [ "$force" ]
303e5f4c
LT
55then
56 git-read-tree --reset $new &&
215a7ad1 57 git-checkout-index -q -f -u -a
303e5f4c 58else
7f88c846 59 git-update-index --refresh >/dev/null
a79944d7 60 git-read-tree -m -u $old $new
ef0bfa25
LT
61fi
62
63#
64# Switch the HEAD pointer to the new branch if it we
65# checked out a branch head, and remove any potential
66# old MERGE_HEAD's (subsequent commits will clearly not
67# be based on them, since we re-set the index)
68#
69if [ "$?" -eq 0 ]; then
91dcdfd3
LT
70 if [ "$newbranch" ]; then
71 echo $new > "$GIT_DIR/refs/heads/$newbranch"
72 branch="$newbranch"
73 fi
ef0bfa25
LT
74 [ "$branch" ] && ln -sf "refs/heads/$branch" "$GIT_DIR/HEAD"
75 rm -f "$GIT_DIR/MERGE_HEAD"
ab22707f
TL
76else
77 exit 1
ef0bfa25 78fi