]>
Commit | Line | Data |
---|---|---|
303e5f4c | 1 | #!/bin/sh |
b33e9666 | 2 | |
1be0659e | 3 | USAGE='[-f] [-b <new_branch>] [-m] [<branch>] [<paths>...]' |
104f3e03 | 4 | SUBDIRECTORY_OK=Sometimes |
806f36d4 | 5 | . git-sh-setup |
b0bafe03 | 6 | |
969d326d | 7 | old_name=HEAD |
5a03e7f2 | 8 | old=$(git-rev-parse --verify $old_name 2>/dev/null) |
e8b11749 | 9 | new= |
969d326d | 10 | new_name= |
a79944d7 | 11 | force= |
e8b11749 | 12 | branch= |
91dcdfd3 | 13 | newbranch= |
969d326d | 14 | newbranch_log= |
1be0659e | 15 | merge= |
e8b11749 LT |
16 | while [ "$#" != "0" ]; do |
17 | arg="$1" | |
18 | shift | |
19 | case "$arg" in | |
91dcdfd3 LT |
20 | "-b") |
21 | newbranch="$1" | |
22 | shift | |
23 | [ -z "$newbranch" ] && | |
24 | die "git checkout: -b needs a branch name" | |
305e22c3 | 25 | git-show-ref --verify --quiet -- "refs/heads/$newbranch" && |
91dcdfd3 | 26 | die "git checkout: branch $newbranch already exists" |
03feddd6 | 27 | git-check-ref-format "heads/$newbranch" || |
babfaf8d | 28 | die "git checkout: we do not like '$newbranch' as a branch name." |
91dcdfd3 | 29 | ;; |
969d326d SP |
30 | "-l") |
31 | newbranch_log=1 | |
32 | ;; | |
303e5f4c | 33 | "-f") |
e8b11749 | 34 | force=1 |
303e5f4c | 35 | ;; |
1be0659e JH |
36 | -m) |
37 | merge=1 | |
38 | ;; | |
4aaa7027 JH |
39 | --) |
40 | break | |
41 | ;; | |
b0bafe03 CS |
42 | -*) |
43 | usage | |
44 | ;; | |
303e5f4c | 45 | *) |
4aaa7027 JH |
46 | if rev=$(git-rev-parse --verify "$arg^0" 2>/dev/null) |
47 | then | |
48 | if [ -z "$rev" ]; then | |
49 | echo "unknown flag $arg" | |
50 | exit 1 | |
51 | fi | |
52 | new="$rev" | |
969d326d | 53 | new_name="$arg^0" |
305e22c3 LT |
54 | if git-show-ref --verify --quiet -- "refs/heads/$arg" |
55 | then | |
4aaa7027 JH |
56 | branch="$arg" |
57 | fi | |
58 | elif rev=$(git-rev-parse --verify "$arg^{tree}" 2>/dev/null) | |
59 | then | |
60 | # checking out selected paths from a tree-ish. | |
61 | new="$rev" | |
969d326d | 62 | new_name="$arg^{tree}" |
4aaa7027 JH |
63 | branch= |
64 | else | |
65 | new= | |
969d326d | 66 | new_name= |
4aaa7027 JH |
67 | branch= |
68 | set x "$arg" "$@" | |
69 | shift | |
e8b11749 | 70 | fi |
2608003f JH |
71 | case "$1" in |
72 | --) | |
73 | shift ;; | |
74 | esac | |
4aaa7027 | 75 | break |
e8b11749 | 76 | ;; |
303e5f4c | 77 | esac |
303e5f4c LT |
78 | done |
79 | ||
897643cc JH |
80 | case "$force$merge" in |
81 | 11) | |
82 | die "git checkout: -f and -m are incompatible" | |
83 | esac | |
84 | ||
4aaa7027 JH |
85 | # The behaviour of the command with and without explicit path |
86 | # parameters is quite different. | |
87 | # | |
88 | # Without paths, we are checking out everything in the work tree, | |
89 | # possibly switching branches. This is the traditional behaviour. | |
91dcdfd3 | 90 | # |
4aaa7027 JH |
91 | # With paths, we are _never_ switching branch, but checking out |
92 | # the named paths from either index (when no rev is given), | |
93 | # or the named tree-ish (when rev is given). | |
94 | ||
95 | if test "$#" -ge 1 | |
96 | then | |
babfaf8d JW |
97 | hint= |
98 | if test "$#" -eq 1 | |
99 | then | |
100 | hint=" | |
101 | Did you intend to checkout '$@' which can not be resolved as commit?" | |
102 | fi | |
1be0659e | 103 | if test '' != "$newbranch$force$merge" |
4aaa7027 | 104 | then |
babfaf8d | 105 | die "git checkout: updating paths is incompatible with switching branches/forcing$hint" |
4aaa7027 JH |
106 | fi |
107 | if test '' != "$new" | |
108 | then | |
109 | # from a specific tree-ish; note that this is for | |
110 | # rescuing paths and is never meant to remove what | |
111 | # is not in the named tree-ish. | |
d0d14cf3 | 112 | git-ls-tree --full-name -r "$new" "$@" | |
4aaa7027 JH |
113 | git-update-index --index-info || exit $? |
114 | fi | |
bf7e1472 JH |
115 | |
116 | # Make sure the request is about existing paths. | |
117 | git-ls-files --error-unmatch -- "$@" >/dev/null || exit | |
118 | git-ls-files -- "$@" | | |
119 | git-checkout-index -f -u --stdin | |
4aaa7027 JH |
120 | exit $? |
121 | else | |
122 | # Make sure we did not fall back on $arg^{tree} codepath | |
123 | # since we are not checking out from an arbitrary tree-ish, | |
124 | # but switching branches. | |
125 | if test '' != "$new" | |
126 | then | |
127 | git-rev-parse --verify "$new^{commit}" >/dev/null 2>&1 || | |
128 | die "Cannot switch branch to a non-commit." | |
129 | fi | |
130 | fi | |
131 | ||
104f3e03 JH |
132 | # We are switching branches and checking out trees, so |
133 | # we *NEED* to be at the toplevel. | |
134 | cdup=$(git-rev-parse --show-cdup) | |
135 | if test ! -z "$cdup" | |
136 | then | |
137 | cd "$cdup" | |
138 | fi | |
139 | ||
969d326d | 140 | [ -z "$new" ] && new=$old && new_name="$old_name" |
4aaa7027 | 141 | |
91dcdfd3 LT |
142 | # If we don't have an old branch that we're switching to, |
143 | # and we don't have a new branch name for the target we | |
144 | # are switching to, then we'd better just be checking out | |
145 | # what we already had | |
4aaa7027 | 146 | |
91dcdfd3 LT |
147 | [ -z "$branch$newbranch" ] && |
148 | [ "$new" != "$old" ] && | |
babfaf8d JW |
149 | die "git checkout: to checkout the requested commit you need to specify |
150 | a name for a new branch which is created and switched to" | |
91dcdfd3 | 151 | |
5a03e7f2 SP |
152 | if [ "X$old" = X ] |
153 | then | |
154 | echo "warning: You do not appear to currently be on a branch." >&2 | |
155 | echo "warning: Forcing checkout of $new_name." >&2 | |
156 | force=1 | |
157 | fi | |
158 | ||
a79944d7 | 159 | if [ "$force" ] |
303e5f4c | 160 | then |
4170af82 | 161 | git-read-tree --reset -u $new |
303e5f4c | 162 | else |
7f88c846 | 163 | git-update-index --refresh >/dev/null |
11271480 | 164 | merge_error=$(git-read-tree -m -u --exclude-per-directory=.gitignore $old $new 2>&1) || ( |
1be0659e JH |
165 | case "$merge" in |
166 | '') | |
167 | echo >&2 "$merge_error" | |
168 | exit 1 ;; | |
19205acf JH |
169 | esac |
170 | ||
1be0659e JH |
171 | # Match the index to the working tree, and do a three-way. |
172 | git diff-files --name-only | git update-index --remove --stdin && | |
19205acf | 173 | work=`git write-tree` && |
abc02670 | 174 | git read-tree --reset -u $new && |
11271480 JH |
175 | git read-tree -m -u --aggressive --exclude-per-directory=.gitignore $old $new $work || |
176 | exit | |
1be0659e | 177 | |
19205acf JH |
178 | if result=`git write-tree 2>/dev/null` |
179 | then | |
1be0659e JH |
180 | echo >&2 "Trivially automerged." |
181 | else | |
182 | git merge-index -o git-merge-one-file -a | |
19205acf | 183 | fi |
1be0659e JH |
184 | |
185 | # Do not register the cleanly merged paths in the index yet. | |
186 | # this is not a real merge before committing, but just carrying | |
187 | # the working tree changes along. | |
188 | unmerged=`git ls-files -u` | |
189 | git read-tree --reset $new | |
190 | case "$unmerged" in | |
191 | '') ;; | |
192 | *) | |
193 | ( | |
194 | z40=0000000000000000000000000000000000000000 | |
195 | echo "$unmerged" | | |
196 | sed -e 's/^[0-7]* [0-9a-f]* /'"0 $z40 /" | |
197 | echo "$unmerged" | |
198 | ) | git update-index --index-info | |
199 | ;; | |
200 | esac | |
201 | exit 0 | |
19205acf | 202 | ) |
980d8ce5 | 203 | saved_err=$? |
504fe714 JH |
204 | if test "$saved_err" = 0 |
205 | then | |
206 | test "$new" = "$old" || git diff-index --name-status "$new" | |
207 | fi | |
980d8ce5 | 208 | (exit $saved_err) |
ef0bfa25 LT |
209 | fi |
210 | ||
211 | # | |
01385e27 | 212 | # Switch the HEAD pointer to the new branch if we |
ef0bfa25 LT |
213 | # checked out a branch head, and remove any potential |
214 | # old MERGE_HEAD's (subsequent commits will clearly not | |
215 | # be based on them, since we re-set the index) | |
216 | # | |
217 | if [ "$?" -eq 0 ]; then | |
91dcdfd3 | 218 | if [ "$newbranch" ]; then |
969d326d | 219 | if [ "$newbranch_log" ]; then |
d7fb7a37 SP |
220 | mkdir -p $(dirname "$GIT_DIR/logs/refs/heads/$newbranch") |
221 | touch "$GIT_DIR/logs/refs/heads/$newbranch" | |
969d326d SP |
222 | fi |
223 | git-update-ref -m "checkout: Created from $new_name" "refs/heads/$newbranch" $new || exit | |
91dcdfd3 LT |
224 | branch="$newbranch" |
225 | fi | |
8098a178 JH |
226 | [ "$branch" ] && |
227 | GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD "refs/heads/$branch" | |
ef0bfa25 | 228 | rm -f "$GIT_DIR/MERGE_HEAD" |
ab22707f TL |
229 | else |
230 | exit 1 | |
ef0bfa25 | 231 | fi |