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