]>
Commit | Line | Data |
---|---|---|
303e5f4c | 1 | #!/bin/sh |
b33e9666 | 2 | |
806f36d4 FK |
3 | USAGE='[-f] [-b <new_branch>] [<branch>] [<paths>...]' |
4 | . git-sh-setup | |
b0bafe03 | 5 | |
303e5f4c | 6 | old=$(git-rev-parse HEAD) |
e8b11749 | 7 | new= |
a79944d7 | 8 | force= |
e8b11749 | 9 | branch= |
91dcdfd3 | 10 | newbranch= |
e8b11749 LT |
11 | while [ "$#" != "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 |
59 | done |
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 | ||
71 | if test "$#" -ge 1 | |
72 | then | |
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 $? | |
87 | else | |
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 | |
96 | fi | |
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 | 109 | if [ "$force" ] |
303e5f4c LT |
110 | then |
111 | git-read-tree --reset $new && | |
215a7ad1 | 112 | git-checkout-index -q -f -u -a |
303e5f4c | 113 | else |
7f88c846 | 114 | git-update-index --refresh >/dev/null |
a79944d7 | 115 | git-read-tree -m -u $old $new |
ef0bfa25 LT |
116 | fi |
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 | # | |
124 | if [ "$?" -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 |
134 | else |
135 | exit 1 | |
ef0bfa25 | 136 | fi |