Merge branch 'jc/for-each-ref' into jc/ref-locking
[git/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=$(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 git-show-ref --verify --quiet -- "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."
29 ;;
30 "-l")
31 newbranch_log=1
32 ;;
33 "-f")
34 force=1
35 ;;
36 -m)
37 merge=1
38 ;;
39 --)
40 break
41 ;;
42 -*)
43 usage
44 ;;
45 *)
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"
53 new_name="$arg^0"
54 if git-show-ref --verify --quiet -- "refs/heads/$arg"
55 then
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"
62 new_name="$arg^{tree}"
63 branch=
64 else
65 new=
66 new_name=
67 branch=
68 set x "$arg" "$@"
69 shift
70 fi
71 case "$1" in
72 --)
73 shift ;;
74 esac
75 break
76 ;;
77 esac
78 done
79
80 # The behaviour of the command with and without explicit path
81 # parameters is quite different.
82 #
83 # Without paths, we are checking out everything in the work tree,
84 # possibly switching branches. This is the traditional behaviour.
85 #
86 # With paths, we are _never_ switching branch, but checking out
87 # the named paths from either index (when no rev is given),
88 # or the named tree-ish (when rev is given).
89
90 if test "$#" -ge 1
91 then
92 hint=
93 if test "$#" -eq 1
94 then
95 hint="
96 Did you intend to checkout '$@' which can not be resolved as commit?"
97 fi
98 if test '' != "$newbranch$force$merge"
99 then
100 die "git checkout: updating paths is incompatible with switching branches/forcing$hint"
101 fi
102 if test '' != "$new"
103 then
104 # from a specific tree-ish; note that this is for
105 # rescuing paths and is never meant to remove what
106 # is not in the named tree-ish.
107 git-ls-tree --full-name -r "$new" "$@" |
108 git-update-index --index-info || exit $?
109 fi
110 git-checkout-index -f -u -- "$@"
111 exit $?
112 else
113 # Make sure we did not fall back on $arg^{tree} codepath
114 # since we are not checking out from an arbitrary tree-ish,
115 # but switching branches.
116 if test '' != "$new"
117 then
118 git-rev-parse --verify "$new^{commit}" >/dev/null 2>&1 ||
119 die "Cannot switch branch to a non-commit."
120 fi
121 fi
122
123 # We are switching branches and checking out trees, so
124 # we *NEED* to be at the toplevel.
125 cdup=$(git-rev-parse --show-cdup)
126 if test ! -z "$cdup"
127 then
128 cd "$cdup"
129 fi
130
131 [ -z "$new" ] && new=$old && new_name="$old_name"
132
133 # If we don't have an old branch that we're switching to,
134 # and we don't have a new branch name for the target we
135 # are switching to, then we'd better just be checking out
136 # what we already had
137
138 [ -z "$branch$newbranch" ] &&
139 [ "$new" != "$old" ] &&
140 die "git checkout: to checkout the requested commit you need to specify
141 a name for a new branch which is created and switched to"
142
143 if [ "$force" ]
144 then
145 git-read-tree --reset -u $new
146 else
147 git-update-index --refresh >/dev/null
148 merge_error=$(git-read-tree -m -u $old $new 2>&1) || (
149 case "$merge" in
150 '')
151 echo >&2 "$merge_error"
152 exit 1 ;;
153 esac
154
155 # Match the index to the working tree, and do a three-way.
156 git diff-files --name-only | git update-index --remove --stdin &&
157 work=`git write-tree` &&
158 git read-tree --reset -u $new &&
159 git read-tree -m -u --aggressive $old $new $work || exit
160
161 if result=`git write-tree 2>/dev/null`
162 then
163 echo >&2 "Trivially automerged."
164 else
165 git merge-index -o git-merge-one-file -a
166 fi
167
168 # Do not register the cleanly merged paths in the index yet.
169 # this is not a real merge before committing, but just carrying
170 # the working tree changes along.
171 unmerged=`git ls-files -u`
172 git read-tree --reset $new
173 case "$unmerged" in
174 '') ;;
175 *)
176 (
177 z40=0000000000000000000000000000000000000000
178 echo "$unmerged" |
179 sed -e 's/^[0-7]* [0-9a-f]* /'"0 $z40 /"
180 echo "$unmerged"
181 ) | git update-index --index-info
182 ;;
183 esac
184 exit 0
185 )
186 saved_err=$?
187 if test "$saved_err" = 0
188 then
189 test "$new" = "$old" || git diff-index --name-status "$new"
190 fi
191 (exit $saved_err)
192 fi
193
194 #
195 # Switch the HEAD pointer to the new branch if we
196 # checked out a branch head, and remove any potential
197 # old MERGE_HEAD's (subsequent commits will clearly not
198 # be based on them, since we re-set the index)
199 #
200 if [ "$?" -eq 0 ]; then
201 if [ "$newbranch" ]; then
202 if [ "$newbranch_log" ]; then
203 mkdir -p $(dirname "$GIT_DIR/logs/refs/heads/$newbranch")
204 touch "$GIT_DIR/logs/refs/heads/$newbranch"
205 fi
206 git-update-ref -m "checkout: Created from $new_name" "refs/heads/$newbranch" $new || exit
207 branch="$newbranch"
208 fi
209 [ "$branch" ] &&
210 GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD "refs/heads/$branch"
211 rm -f "$GIT_DIR/MERGE_HEAD"
212 else
213 exit 1
214 fi