GIT 1.1.1
[git/vmiklos.git] / git-checkout.sh
blob3bbd1117739b02572aece86030bb9f02f3110d34
1 #!/bin/sh
3 USAGE='[-f] [-b <new_branch>] [<branch>] [<paths>...]'
4 SUBDIRECTORY_OK=Sometimes
5 . git-sh-setup
7 old=$(git-rev-parse HEAD)
8 new=
9 force=
10 branch=
11 newbranch=
12 while [ "$#" != "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."
26 "-f")
27 force=1
29 --)
30 break
32 -*)
33 usage
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
42 new="$rev"
43 if [ -f "$GIT_DIR/refs/heads/$arg" ]; then
44 branch="$arg"
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
57 break
59 esac
60 done
62 # The behaviour of the command with and without explicit path
63 # parameters is quite different.
65 # Without paths, we are checking out everything in the work tree,
66 # possibly switching branches. This is the traditional behaviour.
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).
72 if test "$#" -ge 1
73 then
74 if test '' != "$newbranch$force"
75 then
76 die "updating paths and switching branches or forcing are incompatible."
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 $?
86 git-checkout-index -f -u -- "$@"
87 exit $?
88 else
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."
99 # We are switching branches and checking out trees, so
100 # we *NEED* to be at the toplevel.
101 cdup=$(git-rev-parse --show-cdup)
102 if test ! -z "$cdup"
103 then
104 cd "$cdup"
107 [ -z "$new" ] && new=$old
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
114 [ -z "$branch$newbranch" ] &&
115 [ "$new" != "$old" ] &&
116 die "git checkout: you need to specify a new branch name"
118 if [ "$force" ]
119 then
120 git-read-tree --reset $new &&
121 git-checkout-index -q -f -u -a
122 else
123 git-update-index --refresh >/dev/null
124 git-read-tree -m -u $old $new
128 # Switch the HEAD pointer to the new branch if we
129 # checked out a branch head, and remove any potential
130 # old MERGE_HEAD's (subsequent commits will clearly not
131 # be based on them, since we re-set the index)
133 if [ "$?" -eq 0 ]; then
134 if [ "$newbranch" ]; then
135 leading=`expr "refs/heads/$newbranch" : '\(.*\)/'` &&
136 mkdir -p "$GIT_DIR/$leading" &&
137 echo $new >"$GIT_DIR/refs/heads/$newbranch" || exit
138 branch="$newbranch"
140 [ "$branch" ] &&
141 GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD "refs/heads/$branch"
142 rm -f "$GIT_DIR/MERGE_HEAD"
143 else
144 exit 1