gitweb.cgi: git_blame2: Revision blocks now have alternating colors
[git/jnareb-git.git] / git-checkout.sh
blob5613bfc403f7a4208d2a85de73ec33229e5a3f10
1 #!/bin/sh
3 USAGE='[-f] [-b <new_branch>] [-m] [<branch>] [<paths>...]'
4 SUBDIRECTORY_OK=Sometimes
5 . git-sh-setup
7 old=$(git-rev-parse HEAD)
8 old_name=HEAD
9 new=
10 new_name=
11 force=
12 branch=
13 newbranch=
14 newbranch_log=
15 merge=
16 while [ "$#" != "0" ]; do
17 arg="$1"
18 shift
19 case "$arg" in
20 "-b")
21 newbranch="$1"
22 shift
23 [ -z "$newbranch" ] &&
24 die "git checkout: -b needs a branch name"
25 [ -e "$GIT_DIR/refs/heads/$newbranch" ] &&
26 die "git checkout: branch $newbranch already exists"
27 git-check-ref-format "heads/$newbranch" ||
28 die "git checkout: we do not like '$newbranch' as a branch name."
30 "-l")
31 newbranch_log=1
33 "-f")
34 force=1
36 -m)
37 merge=1
39 --)
40 break
42 -*)
43 usage
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
52 new="$rev"
53 new_name="$arg^0"
54 if [ -f "$GIT_DIR/refs/heads/$arg" ]; then
55 branch="$arg"
57 elif rev=$(git-rev-parse --verify "$arg^{tree}" 2>/dev/null)
58 then
59 # checking out selected paths from a tree-ish.
60 new="$rev"
61 new_name="$arg^{tree}"
62 branch=
63 else
64 new=
65 new_name=
66 branch=
67 set x "$arg" "$@"
68 shift
70 break
72 esac
73 done
75 # The behaviour of the command with and without explicit path
76 # parameters is quite different.
78 # Without paths, we are checking out everything in the work tree,
79 # possibly switching branches. This is the traditional behaviour.
81 # With paths, we are _never_ switching branch, but checking out
82 # the named paths from either index (when no rev is given),
83 # or the named tree-ish (when rev is given).
85 if test "$#" -ge 1
86 then
87 hint=
88 if test "$#" -eq 1
89 then
90 hint="
91 Did you intend to checkout '$@' which can not be resolved as commit?"
93 if test '' != "$newbranch$force$merge"
94 then
95 die "git checkout: updating paths is incompatible with switching branches/forcing$hint"
97 if test '' != "$new"
98 then
99 # from a specific tree-ish; note that this is for
100 # rescuing paths and is never meant to remove what
101 # is not in the named tree-ish.
102 git-ls-tree --full-name -r "$new" "$@" |
103 git-update-index --index-info || exit $?
105 git-checkout-index -f -u -- "$@"
106 exit $?
107 else
108 # Make sure we did not fall back on $arg^{tree} codepath
109 # since we are not checking out from an arbitrary tree-ish,
110 # but switching branches.
111 if test '' != "$new"
112 then
113 git-rev-parse --verify "$new^{commit}" >/dev/null 2>&1 ||
114 die "Cannot switch branch to a non-commit."
118 # We are switching branches and checking out trees, so
119 # we *NEED* to be at the toplevel.
120 cdup=$(git-rev-parse --show-cdup)
121 if test ! -z "$cdup"
122 then
123 cd "$cdup"
126 [ -z "$new" ] && new=$old && new_name="$old_name"
128 # If we don't have an old branch that we're switching to,
129 # and we don't have a new branch name for the target we
130 # are switching to, then we'd better just be checking out
131 # what we already had
133 [ -z "$branch$newbranch" ] &&
134 [ "$new" != "$old" ] &&
135 die "git checkout: to checkout the requested commit you need to specify
136 a name for a new branch which is created and switched to"
138 if [ "$force" ]
139 then
140 git-read-tree --reset -u $new
141 else
142 git-update-index --refresh >/dev/null
143 merge_error=$(git-read-tree -m -u $old $new 2>&1) || (
144 case "$merge" in
146 echo >&2 "$merge_error"
147 exit 1 ;;
148 esac
150 # Match the index to the working tree, and do a three-way.
151 git diff-files --name-only | git update-index --remove --stdin &&
152 work=`git write-tree` &&
153 git read-tree --reset -u $new &&
154 git read-tree -m -u --aggressive $old $new $work || exit
156 if result=`git write-tree 2>/dev/null`
157 then
158 echo >&2 "Trivially automerged."
159 else
160 git merge-index -o git-merge-one-file -a
163 # Do not register the cleanly merged paths in the index yet.
164 # this is not a real merge before committing, but just carrying
165 # the working tree changes along.
166 unmerged=`git ls-files -u`
167 git read-tree --reset $new
168 case "$unmerged" in
169 '') ;;
172 z40=0000000000000000000000000000000000000000
173 echo "$unmerged" |
174 sed -e 's/^[0-7]* [0-9a-f]* /'"0 $z40 /"
175 echo "$unmerged"
176 ) | git update-index --index-info
178 esac
179 exit 0
181 saved_err=$?
182 if test "$saved_err" = 0
183 then
184 test "$new" = "$old" || git diff-index --name-status "$new"
186 (exit $saved_err)
190 # Switch the HEAD pointer to the new branch if we
191 # checked out a branch head, and remove any potential
192 # old MERGE_HEAD's (subsequent commits will clearly not
193 # be based on them, since we re-set the index)
195 if [ "$?" -eq 0 ]; then
196 if [ "$newbranch" ]; then
197 if [ "$newbranch_log" ]; then
198 mkdir -p $(dirname "$GIT_DIR/logs/refs/heads/$newbranch")
199 touch "$GIT_DIR/logs/refs/heads/$newbranch"
201 git-update-ref -m "checkout: Created from $new_name" "refs/heads/$newbranch" $new || exit
202 branch="$newbranch"
204 [ "$branch" ] &&
205 GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD "refs/heads/$branch"
206 rm -f "$GIT_DIR/MERGE_HEAD"
207 else
208 exit 1