Merge branch 'jm/format-patch-no-auto-n-when-k-is-given' into maint
[git/dscho.git] / contrib / examples / git-checkout.sh
blob1a7689a48f07a6ed2bb156f745bfea19a10e3eb9
1 #!/bin/sh
3 OPTIONS_KEEPDASHDASH=t
4 OPTIONS_SPEC="\
5 git-checkout [options] [<branch>] [<paths>...]
6 --
7 b= create a new branch started at <branch>
8 l create the new branch's reflog
9 track arrange that the new branch tracks the remote branch
10 f proceed even if the index or working tree is not HEAD
11 m merge local modifications into the new branch
12 q,quiet be quiet
14 SUBDIRECTORY_OK=Sometimes
15 . git-sh-setup
16 require_work_tree
18 old_name=HEAD
19 old=$(git rev-parse --verify $old_name 2>/dev/null)
20 oldbranch=$(git symbolic-ref $old_name 2>/dev/null)
21 new=
22 new_name=
23 force=
24 branch=
25 track=
26 newbranch=
27 newbranch_log=
28 merge=
29 quiet=
30 v=-v
31 LF='
34 while test $# != 0; do
35 case "$1" in
36 -b)
37 shift
38 newbranch="$1"
39 [ -z "$newbranch" ] &&
40 die "git checkout: -b needs a branch name"
41 git show-ref --verify --quiet -- "refs/heads/$newbranch" &&
42 die "git checkout: branch $newbranch already exists"
43 git check-ref-format "heads/$newbranch" ||
44 die "git checkout: we do not like '$newbranch' as a branch name."
46 -l)
47 newbranch_log=-l
49 --track|--no-track)
50 track="$1"
52 -f)
53 force=1
55 -m)
56 merge=1
58 -q|--quiet)
59 quiet=1
62 --)
63 shift
64 break
67 usage
69 esac
70 shift
71 done
73 arg="$1"
74 rev=$(git rev-parse --verify "$arg" 2>/dev/null)
75 if rev=$(git rev-parse --verify "$rev^0" 2>/dev/null)
76 then
77 [ -z "$rev" ] && die "unknown flag $arg"
78 new_name="$arg"
79 if git show-ref --verify --quiet -- "refs/heads/$arg"
80 then
81 rev=$(git rev-parse --verify "refs/heads/$arg^0")
82 branch="$arg"
84 new="$rev"
85 shift
86 elif rev=$(git rev-parse --verify "$rev^{tree}" 2>/dev/null)
87 then
88 # checking out selected paths from a tree-ish.
89 new="$rev"
90 new_name="$rev^{tree}"
91 shift
93 [ "$1" = "--" ] && shift
95 case "$newbranch,$track" in
96 ,--*)
97 die "git checkout: --track and --no-track require -b"
98 esac
100 case "$force$merge" in
102 die "git checkout: -f and -m are incompatible"
103 esac
105 # The behaviour of the command with and without explicit path
106 # parameters is quite different.
108 # Without paths, we are checking out everything in the work tree,
109 # possibly switching branches. This is the traditional behaviour.
111 # With paths, we are _never_ switching branch, but checking out
112 # the named paths from either index (when no rev is given),
113 # or the named tree-ish (when rev is given).
115 if test "$#" -ge 1
116 then
117 hint=
118 if test "$#" -eq 1
119 then
120 hint="
121 Did you intend to checkout '$@' which can not be resolved as commit?"
123 if test '' != "$newbranch$force$merge"
124 then
125 die "git checkout: updating paths is incompatible with switching branches/forcing$hint"
127 if test '' != "$new"
128 then
129 # from a specific tree-ish; note that this is for
130 # rescuing paths and is never meant to remove what
131 # is not in the named tree-ish.
132 git ls-tree --full-name -r "$new" "$@" |
133 git update-index --index-info || exit $?
136 # Make sure the request is about existing paths.
137 git ls-files --full-name --error-unmatch -- "$@" >/dev/null || exit
138 git ls-files --full-name -- "$@" |
139 (cd_to_toplevel && git checkout-index -f -u --stdin)
141 # Run a post-checkout hook -- the HEAD does not change so the
142 # current HEAD is passed in for both args
143 if test -x "$GIT_DIR"/hooks/post-checkout; then
144 "$GIT_DIR"/hooks/post-checkout $old $old 0
147 exit $?
148 else
149 # Make sure we did not fall back on $arg^{tree} codepath
150 # since we are not checking out from an arbitrary tree-ish,
151 # but switching branches.
152 if test '' != "$new"
153 then
154 git rev-parse --verify "$new^{commit}" >/dev/null 2>&1 ||
155 die "Cannot switch branch to a non-commit."
159 # We are switching branches and checking out trees, so
160 # we *NEED* to be at the toplevel.
161 cd_to_toplevel
163 [ -z "$new" ] && new=$old && new_name="$old_name"
165 # If we don't have an existing branch that we're switching to,
166 # and we don't have a new branch name for the target we
167 # are switching to, then we are detaching our HEAD from any
168 # branch. However, if "git checkout HEAD" detaches the HEAD
169 # from the current branch, even though that may be logically
170 # correct, it feels somewhat funny. More importantly, we do not
171 # want "git checkout" nor "git checkout -f" to detach HEAD.
173 detached=
174 detach_warn=
176 describe_detached_head () {
177 test -n "$quiet" || {
178 printf >&2 "$1 "
179 GIT_PAGER= git log >&2 -1 --pretty=oneline --abbrev-commit "$2" --
183 if test -z "$branch$newbranch" && test "$new_name" != "$old_name"
184 then
185 detached="$new"
186 if test -n "$oldbranch" && test -z "$quiet"
187 then
188 detach_warn="Note: moving to \"$new_name\" which isn't a local branch
189 If you want to create a new branch from this checkout, you may do so
190 (now or later) by using -b with the checkout command again. Example:
191 git checkout -b <new_branch_name>"
193 elif test -z "$oldbranch" && test "$new" != "$old"
194 then
195 describe_detached_head 'Previous HEAD position was' "$old"
198 if [ "X$old" = X ]
199 then
200 if test -z "$quiet"
201 then
202 echo >&2 "warning: You appear to be on a branch yet to be born."
203 echo >&2 "warning: Forcing checkout of $new_name."
205 force=1
208 if [ "$force" ]
209 then
210 git read-tree $v --reset -u $new
211 else
212 git update-index --refresh >/dev/null
213 git read-tree $v -m -u --exclude-per-directory=.gitignore $old $new || (
214 case "$merge,$v" in
216 exit 1 ;;
218 ;; # quiet
220 echo >&2 "Falling back to 3-way merge..." ;;
221 esac
223 # Match the index to the working tree, and do a three-way.
224 git diff-files --name-only | git update-index --remove --stdin &&
225 work=`git write-tree` &&
226 git read-tree $v --reset -u $new || exit
228 eval GITHEAD_$new='${new_name:-${branch:-$new}}' &&
229 eval GITHEAD_$work=local &&
230 export GITHEAD_$new GITHEAD_$work &&
231 git merge-recursive $old -- $new $work
233 # Do not register the cleanly merged paths in the index yet.
234 # this is not a real merge before committing, but just carrying
235 # the working tree changes along.
236 unmerged=`git ls-files -u`
237 git read-tree $v --reset $new
238 case "$unmerged" in
239 '') ;;
242 z40=0000000000000000000000000000000000000000
243 echo "$unmerged" |
244 sed -e 's/^[0-7]* [0-9a-f]* /'"0 $z40 /"
245 echo "$unmerged"
246 ) | git update-index --index-info
248 esac
249 exit 0
251 saved_err=$?
252 if test "$saved_err" = 0 && test -z "$quiet"
253 then
254 git diff-index --name-status "$new"
256 (exit $saved_err)
260 # Switch the HEAD pointer to the new branch if we
261 # checked out a branch head, and remove any potential
262 # old MERGE_HEAD's (subsequent commits will clearly not
263 # be based on them, since we re-set the index)
265 if [ "$?" -eq 0 ]; then
266 if [ "$newbranch" ]; then
267 git branch $track $newbranch_log "$newbranch" "$new_name" || exit
268 branch="$newbranch"
270 if test -n "$branch"
271 then
272 old_branch_name=`expr "z$oldbranch" : 'zrefs/heads/\(.*\)'`
273 GIT_DIR="$GIT_DIR" git symbolic-ref -m "checkout: moving from ${old_branch_name:-$old} to $branch" HEAD "refs/heads/$branch"
274 if test -n "$quiet"
275 then
276 true # nothing
277 elif test "refs/heads/$branch" = "$oldbranch"
278 then
279 echo >&2 "Already on branch \"$branch\""
280 else
281 echo >&2 "Switched to${newbranch:+ a new} branch \"$branch\""
283 elif test -n "$detached"
284 then
285 old_branch_name=`expr "z$oldbranch" : 'zrefs/heads/\(.*\)'`
286 git update-ref --no-deref -m "checkout: moving from ${old_branch_name:-$old} to $arg" HEAD "$detached" ||
287 die "Cannot detach HEAD"
288 if test -n "$detach_warn"
289 then
290 echo >&2 "$detach_warn"
292 describe_detached_head 'HEAD is now at' HEAD
294 rm -f "$GIT_DIR/MERGE_HEAD"
295 else
296 exit 1
299 # Run a post-checkout hook
300 if test -x "$GIT_DIR"/hooks/post-checkout; then
301 "$GIT_DIR"/hooks/post-checkout $old $new 1