]>
Commit | Line | Data |
---|---|---|
303e5f4c | 1 | #!/bin/sh |
b33e9666 | 2 | |
806f36d4 | 3 | USAGE='[-f] [-b <new_branch>] [<branch>] [<paths>...]' |
104f3e03 | 4 | SUBDIRECTORY_OK=Sometimes |
806f36d4 | 5 | . git-sh-setup |
b0bafe03 | 6 | |
303e5f4c | 7 | old=$(git-rev-parse HEAD) |
e8b11749 | 8 | new= |
a79944d7 | 9 | force= |
e8b11749 | 10 | branch= |
91dcdfd3 | 11 | newbranch= |
e8b11749 LT |
12 | while [ "$#" != "0" ]; do |
13 | arg="$1" | |
14 | shift | |
15 | case "$arg" in | |
91dcdfd3 LT |
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" | |
03feddd6 JH |
23 | git-check-ref-format "heads/$newbranch" || |
24 | die "we do not like '$newbranch' as a branch name." | |
91dcdfd3 | 25 | ;; |
303e5f4c | 26 | "-f") |
e8b11749 | 27 | force=1 |
303e5f4c | 28 | ;; |
4aaa7027 JH |
29 | --) |
30 | break | |
31 | ;; | |
b0bafe03 CS |
32 | -*) |
33 | usage | |
34 | ;; | |
303e5f4c | 35 | *) |
4aaa7027 JH |
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 | |
e8b11749 | 56 | fi |
4aaa7027 | 57 | break |
e8b11749 | 58 | ;; |
303e5f4c | 59 | esac |
303e5f4c LT |
60 | done |
61 | ||
4aaa7027 JH |
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. | |
91dcdfd3 | 67 | # |
4aaa7027 JH |
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 | ||
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." | |
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. | |
d0d14cf3 | 83 | git-ls-tree --full-name -r "$new" "$@" | |
4aaa7027 JH |
84 | git-update-index --index-info || exit $? |
85 | fi | |
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." | |
96 | fi | |
97 | fi | |
98 | ||
104f3e03 JH |
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" | |
105 | fi | |
106 | ||
4aaa7027 JH |
107 | [ -z "$new" ] && new=$old |
108 | ||
91dcdfd3 LT |
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 | |
4aaa7027 | 113 | |
91dcdfd3 LT |
114 | [ -z "$branch$newbranch" ] && |
115 | [ "$new" != "$old" ] && | |
116 | die "git checkout: you need to specify a new branch name" | |
117 | ||
a79944d7 | 118 | if [ "$force" ] |
303e5f4c LT |
119 | then |
120 | git-read-tree --reset $new && | |
215a7ad1 | 121 | git-checkout-index -q -f -u -a |
303e5f4c | 122 | else |
7f88c846 | 123 | git-update-index --refresh >/dev/null |
a79944d7 | 124 | git-read-tree -m -u $old $new |
ef0bfa25 LT |
125 | fi |
126 | ||
127 | # | |
01385e27 | 128 | # Switch the HEAD pointer to the new branch if we |
ef0bfa25 LT |
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) | |
132 | # | |
133 | if [ "$?" -eq 0 ]; then | |
91dcdfd3 | 134 | if [ "$newbranch" ]; then |
13d1cc36 JH |
135 | leading=`expr "refs/heads/$newbranch" : '\(.*\)/'` && |
136 | mkdir -p "$GIT_DIR/$leading" && | |
137 | echo $new >"$GIT_DIR/refs/heads/$newbranch" || exit | |
91dcdfd3 LT |
138 | branch="$newbranch" |
139 | fi | |
8098a178 JH |
140 | [ "$branch" ] && |
141 | GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD "refs/heads/$branch" | |
ef0bfa25 | 142 | rm -f "$GIT_DIR/MERGE_HEAD" |
ab22707f TL |
143 | else |
144 | exit 1 | |
ef0bfa25 | 145 | fi |