]> git.ipfire.org Git - thirdparty/git.git/blob - git-checkout.sh
Merge branch 'jc/detached-head'
[thirdparty/git.git] / git-checkout.sh
1 #!/bin/sh
2
3 USAGE='[-f] [-b <new_branch>] [-m] [<branch>] [<paths>...]'
4 SUBDIRECTORY_OK=Sometimes
5 . git-sh-setup
6
7 old_name=HEAD
8 old=$(git-rev-parse --verify $old_name 2>/dev/null)
9 oldbranch=$(git-symbolic-ref $old_name 2>/dev/null)
10 new=
11 new_name=
12 force=
13 branch=
14 newbranch=
15 newbranch_log=
16 merge=
17 LF='
18 '
19 while [ "$#" != "0" ]; do
20 arg="$1"
21 shift
22 case "$arg" in
23 "-b")
24 newbranch="$1"
25 shift
26 [ -z "$newbranch" ] &&
27 die "git checkout: -b needs a branch name"
28 git-show-ref --verify --quiet -- "refs/heads/$newbranch" &&
29 die "git checkout: branch $newbranch already exists"
30 git-check-ref-format "heads/$newbranch" ||
31 die "git checkout: we do not like '$newbranch' as a branch name."
32 ;;
33 "-l")
34 newbranch_log=1
35 ;;
36 "-f")
37 force=1
38 ;;
39 -m)
40 merge=1
41 ;;
42 --)
43 break
44 ;;
45 -*)
46 usage
47 ;;
48 *)
49 if rev=$(git-rev-parse --verify "$arg^0" 2>/dev/null)
50 then
51 if [ -z "$rev" ]; then
52 echo "unknown flag $arg"
53 exit 1
54 fi
55 new="$rev"
56 new_name="$arg"
57 if git-show-ref --verify --quiet -- "refs/heads/$arg"
58 then
59 branch="$arg"
60 fi
61 elif rev=$(git-rev-parse --verify "$arg^{tree}" 2>/dev/null)
62 then
63 # checking out selected paths from a tree-ish.
64 new="$rev"
65 new_name="$arg^{tree}"
66 branch=
67 else
68 new=
69 new_name=
70 branch=
71 set x "$arg" "$@"
72 shift
73 fi
74 case "$1" in
75 --)
76 shift ;;
77 esac
78 break
79 ;;
80 esac
81 done
82
83 case "$force$merge" in
84 11)
85 die "git checkout: -f and -m are incompatible"
86 esac
87
88 # The behaviour of the command with and without explicit path
89 # parameters is quite different.
90 #
91 # Without paths, we are checking out everything in the work tree,
92 # possibly switching branches. This is the traditional behaviour.
93 #
94 # With paths, we are _never_ switching branch, but checking out
95 # the named paths from either index (when no rev is given),
96 # or the named tree-ish (when rev is given).
97
98 if test "$#" -ge 1
99 then
100 hint=
101 if test "$#" -eq 1
102 then
103 hint="
104 Did you intend to checkout '$@' which can not be resolved as commit?"
105 fi
106 if test '' != "$newbranch$force$merge"
107 then
108 die "git checkout: updating paths is incompatible with switching branches/forcing$hint"
109 fi
110 if test '' != "$new"
111 then
112 # from a specific tree-ish; note that this is for
113 # rescuing paths and is never meant to remove what
114 # is not in the named tree-ish.
115 git-ls-tree --full-name -r "$new" "$@" |
116 git-update-index --index-info || exit $?
117 fi
118
119 # Make sure the request is about existing paths.
120 git-ls-files --error-unmatch -- "$@" >/dev/null || exit
121 git-ls-files -- "$@" |
122 git-checkout-index -f -u --stdin
123 exit $?
124 else
125 # Make sure we did not fall back on $arg^{tree} codepath
126 # since we are not checking out from an arbitrary tree-ish,
127 # but switching branches.
128 if test '' != "$new"
129 then
130 git-rev-parse --verify "$new^{commit}" >/dev/null 2>&1 ||
131 die "Cannot switch branch to a non-commit."
132 fi
133 fi
134
135 # We are switching branches and checking out trees, so
136 # we *NEED* to be at the toplevel.
137 cdup=$(git-rev-parse --show-cdup)
138 if test ! -z "$cdup"
139 then
140 cd "$cdup"
141 fi
142
143 [ -z "$new" ] && new=$old && new_name="$old_name"
144
145 # If we don't have an existing branch that we're switching to,
146 # and we don't have a new branch name for the target we
147 # are switching to, then we are detaching our HEAD from any
148 # branch. However, if "git checkout HEAD" detaches the HEAD
149 # from the current branch, even though that may be logically
150 # correct, it feels somewhat funny. More importantly, we do not
151 # want "git checkout" nor "git checkout -f" to detach HEAD.
152
153 detached=
154 detach_warn=
155
156 if test -z "$branch$newbranch" && test "$new" != "$old"
157 then
158 detached="$new"
159 if test -n "$oldbranch"
160 then
161 detach_warn="warning: you are not on ANY branch anymore.
162 If you meant to create a new branch from the commit, you need -b to
163 associate a new branch with the wanted checkout. Example:
164 git checkout -b <new_branch_name> $arg"
165 fi
166 elif test -z "$oldbranch" && test -n "$branch"
167 then
168 # Coming back...
169 if test -z "$force"
170 then
171 git show-ref -d -s | grep "$old" >/dev/null || {
172 echo >&2 \
173 "You are not on any branch and switching to branch '$new_name'
174 may lose your changes. At this point, you can do one of two things:
175 (1) Decide it is Ok and say 'git checkout -f $new_name';
176 (2) Start a new branch from the current commit, by saying
177 'git checkout -b <branch-name>'.
178 Leaving your HEAD detached; not switching to branch '$new_name'."
179 exit 1;
180 }
181 fi
182 fi
183
184 if [ "X$old" = X ]
185 then
186 echo >&2 "warning: You appear to be on a branch yet to be born."
187 echo >&2 "warning: Forcing checkout of $new_name."
188 force=1
189 fi
190
191 if [ "$force" ]
192 then
193 git-read-tree --reset -u $new
194 else
195 git-update-index --refresh >/dev/null
196 merge_error=$(git-read-tree -m -u --exclude-per-directory=.gitignore $old $new 2>&1) || (
197 case "$merge" in
198 '')
199 echo >&2 "$merge_error"
200 exit 1 ;;
201 esac
202
203 # Match the index to the working tree, and do a three-way.
204 git diff-files --name-only | git update-index --remove --stdin &&
205 work=`git write-tree` &&
206 git read-tree --reset -u $new &&
207 git read-tree -m -u --aggressive --exclude-per-directory=.gitignore $old $new $work ||
208 exit
209
210 if result=`git write-tree 2>/dev/null`
211 then
212 echo >&2 "Trivially automerged."
213 else
214 git merge-index -o git-merge-one-file -a
215 fi
216
217 # Do not register the cleanly merged paths in the index yet.
218 # this is not a real merge before committing, but just carrying
219 # the working tree changes along.
220 unmerged=`git ls-files -u`
221 git read-tree --reset $new
222 case "$unmerged" in
223 '') ;;
224 *)
225 (
226 z40=0000000000000000000000000000000000000000
227 echo "$unmerged" |
228 sed -e 's/^[0-7]* [0-9a-f]* /'"0 $z40 /"
229 echo "$unmerged"
230 ) | git update-index --index-info
231 ;;
232 esac
233 exit 0
234 )
235 saved_err=$?
236 if test "$saved_err" = 0
237 then
238 test "$new" = "$old" || git diff-index --name-status "$new"
239 fi
240 (exit $saved_err)
241 fi
242
243 #
244 # Switch the HEAD pointer to the new branch if we
245 # checked out a branch head, and remove any potential
246 # old MERGE_HEAD's (subsequent commits will clearly not
247 # be based on them, since we re-set the index)
248 #
249 if [ "$?" -eq 0 ]; then
250 if [ "$newbranch" ]; then
251 if [ "$newbranch_log" ]; then
252 mkdir -p $(dirname "$GIT_DIR/logs/refs/heads/$newbranch")
253 touch "$GIT_DIR/logs/refs/heads/$newbranch"
254 fi
255 git-update-ref -m "checkout: Created from $new_name" "refs/heads/$newbranch" $new || exit
256 branch="$newbranch"
257 fi
258 if test -n "$branch"
259 then
260 GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD "refs/heads/$branch"
261 elif test -n "$detached"
262 then
263 # NEEDSWORK: we would want a command to detach the HEAD
264 # atomically, instead of this handcrafted command sequence.
265 # Perhaps:
266 # git update-ref --detach HEAD $new
267 # or something like that...
268 #
269 echo "$detached" >"$GIT_DIR/HEAD.new" &&
270 mv "$GIT_DIR/HEAD.new" "$GIT_DIR/HEAD" ||
271 die "Cannot detach HEAD"
272 if test -n "$detach_warn"
273 then
274 echo >&2 "$detach_warn"
275 fi
276 fi
277 rm -f "$GIT_DIR/MERGE_HEAD"
278 else
279 exit 1
280 fi