]> git.ipfire.org Git - thirdparty/git.git/blame - git-checkout.sh
Merge in fixes up to 1.0.3 maintenance branch.
[thirdparty/git.git] / git-checkout.sh
CommitLineData
303e5f4c 1#!/bin/sh
b33e9666 2
806f36d4
FK
3USAGE='[-f] [-b <new_branch>] [<branch>] [<paths>...]'
4. git-sh-setup
b0bafe03 5
303e5f4c 6old=$(git-rev-parse HEAD)
e8b11749 7new=
a79944d7 8force=
e8b11749 9branch=
91dcdfd3 10newbranch=
e8b11749
LT
11while [ "$#" != "0" ]; do
12 arg="$1"
13 shift
14 case "$arg" in
91dcdfd3
LT
15 "-b")
16 newbranch="$1"
17 shift
18 [ -z "$newbranch" ] &&
19 die "git checkout: -b needs a branch name"
20 [ -e "$GIT_DIR/refs/heads/$newbranch" ] &&
21 die "git checkout: branch $newbranch already exists"
03feddd6
JH
22 git-check-ref-format "heads/$newbranch" ||
23 die "we do not like '$newbranch' as a branch name."
91dcdfd3 24 ;;
303e5f4c 25 "-f")
e8b11749 26 force=1
303e5f4c 27 ;;
4aaa7027
JH
28 --)
29 break
30 ;;
b0bafe03
CS
31 -*)
32 usage
33 ;;
303e5f4c 34 *)
4aaa7027
JH
35 if rev=$(git-rev-parse --verify "$arg^0" 2>/dev/null)
36 then
37 if [ -z "$rev" ]; then
38 echo "unknown flag $arg"
39 exit 1
40 fi
41 new="$rev"
42 if [ -f "$GIT_DIR/refs/heads/$arg" ]; then
43 branch="$arg"
44 fi
45 elif rev=$(git-rev-parse --verify "$arg^{tree}" 2>/dev/null)
46 then
47 # checking out selected paths from a tree-ish.
48 new="$rev"
49 branch=
50 else
51 new=
52 branch=
53 set x "$arg" "$@"
54 shift
e8b11749 55 fi
4aaa7027 56 break
e8b11749 57 ;;
303e5f4c 58 esac
303e5f4c
LT
59done
60
4aaa7027
JH
61# The behaviour of the command with and without explicit path
62# parameters is quite different.
63#
64# Without paths, we are checking out everything in the work tree,
65# possibly switching branches. This is the traditional behaviour.
91dcdfd3 66#
4aaa7027
JH
67# With paths, we are _never_ switching branch, but checking out
68# the named paths from either index (when no rev is given),
69# or the named tree-ish (when rev is given).
70
71if test "$#" -ge 1
72then
73 if test '' != "$newbranch$force"
74 then
75 die "updating paths and switching branches or forcing are incompatible."
76 fi
77 if test '' != "$new"
78 then
79 # from a specific tree-ish; note that this is for
80 # rescuing paths and is never meant to remove what
81 # is not in the named tree-ish.
82 git-ls-tree -r "$new" "$@" |
4aaa7027
JH
83 git-update-index --index-info || exit $?
84 fi
85 git-checkout-index -f -u -- "$@"
86 exit $?
87else
88 # Make sure we did not fall back on $arg^{tree} codepath
89 # since we are not checking out from an arbitrary tree-ish,
90 # but switching branches.
91 if test '' != "$new"
92 then
93 git-rev-parse --verify "$new^{commit}" >/dev/null 2>&1 ||
94 die "Cannot switch branch to a non-commit."
95 fi
96fi
97
98[ -z "$new" ] && new=$old
99
91dcdfd3
LT
100# If we don't have an old branch that we're switching to,
101# and we don't have a new branch name for the target we
102# are switching to, then we'd better just be checking out
103# what we already had
4aaa7027 104
91dcdfd3
LT
105[ -z "$branch$newbranch" ] &&
106 [ "$new" != "$old" ] &&
107 die "git checkout: you need to specify a new branch name"
108
a79944d7 109if [ "$force" ]
303e5f4c
LT
110then
111 git-read-tree --reset $new &&
215a7ad1 112 git-checkout-index -q -f -u -a
303e5f4c 113else
7f88c846 114 git-update-index --refresh >/dev/null
a79944d7 115 git-read-tree -m -u $old $new
ef0bfa25
LT
116fi
117
118#
01385e27 119# Switch the HEAD pointer to the new branch if we
ef0bfa25
LT
120# checked out a branch head, and remove any potential
121# old MERGE_HEAD's (subsequent commits will clearly not
122# be based on them, since we re-set the index)
123#
124if [ "$?" -eq 0 ]; then
91dcdfd3 125 if [ "$newbranch" ]; then
13d1cc36
JH
126 leading=`expr "refs/heads/$newbranch" : '\(.*\)/'` &&
127 mkdir -p "$GIT_DIR/$leading" &&
128 echo $new >"$GIT_DIR/refs/heads/$newbranch" || exit
91dcdfd3
LT
129 branch="$newbranch"
130 fi
8098a178
JH
131 [ "$branch" ] &&
132 GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD "refs/heads/$branch"
ef0bfa25 133 rm -f "$GIT_DIR/MERGE_HEAD"
ab22707f
TL
134else
135 exit 1
ef0bfa25 136fi