checkout: automerge local changes while switching branches.
[git.git] / git-checkout.sh
blob76e6a41c6cd9a0e5c87b64132f6af02570f00983
1 #!/bin/sh
3 USAGE='[-f] [-b <new_branch>] [<branch>] [<paths>...]'
4 SUBDIRECTORY_OK=Sometimes
5 . git-sh-setup
7 old=$(git-rev-parse HEAD)
8 new=
9 force=
10 branch=
11 newbranch=
12 while [ "$#" != "0" ]; do
13 arg="$1"
14 shift
15 case "$arg" in
16 "-b")
17 newbranch="$1"
18 shift
19 [ -z "$newbranch" ] &&
20 die "git checkout: -b needs a branch name"
21 [ -e "$GIT_DIR/refs/heads/$newbranch" ] &&
22 die "git checkout: branch $newbranch already exists"
23 git-check-ref-format "heads/$newbranch" ||
24 die "we do not like '$newbranch' as a branch name."
26 "-f")
27 force=1
29 --)
30 break
32 -*)
33 usage
36 if rev=$(git-rev-parse --verify "$arg^0" 2>/dev/null)
37 then
38 if [ -z "$rev" ]; then
39 echo "unknown flag $arg"
40 exit 1
42 new="$rev"
43 if [ -f "$GIT_DIR/refs/heads/$arg" ]; then
44 branch="$arg"
46 elif rev=$(git-rev-parse --verify "$arg^{tree}" 2>/dev/null)
47 then
48 # checking out selected paths from a tree-ish.
49 new="$rev"
50 branch=
51 else
52 new=
53 branch=
54 set x "$arg" "$@"
55 shift
57 break
59 esac
60 done
62 # The behaviour of the command with and without explicit path
63 # parameters is quite different.
65 # Without paths, we are checking out everything in the work tree,
66 # possibly switching branches. This is the traditional behaviour.
68 # With paths, we are _never_ switching branch, but checking out
69 # the named paths from either index (when no rev is given),
70 # or the named tree-ish (when rev is given).
72 if test "$#" -ge 1
73 then
74 if test '' != "$newbranch$force"
75 then
76 die "updating paths and switching branches or forcing are incompatible."
78 if test '' != "$new"
79 then
80 # from a specific tree-ish; note that this is for
81 # rescuing paths and is never meant to remove what
82 # is not in the named tree-ish.
83 git-ls-tree --full-name -r "$new" "$@" |
84 git-update-index --index-info || exit $?
86 git-checkout-index -f -u -- "$@"
87 exit $?
88 else
89 # Make sure we did not fall back on $arg^{tree} codepath
90 # since we are not checking out from an arbitrary tree-ish,
91 # but switching branches.
92 if test '' != "$new"
93 then
94 git-rev-parse --verify "$new^{commit}" >/dev/null 2>&1 ||
95 die "Cannot switch branch to a non-commit."
99 # We are switching branches and checking out trees, so
100 # we *NEED* to be at the toplevel.
101 cdup=$(git-rev-parse --show-cdup)
102 if test ! -z "$cdup"
103 then
104 cd "$cdup"
107 [ -z "$new" ] && new=$old
109 # If we don't have an old branch that we're switching to,
110 # and we don't have a new branch name for the target we
111 # are switching to, then we'd better just be checking out
112 # what we already had
114 [ -z "$branch$newbranch" ] &&
115 [ "$new" != "$old" ] &&
116 die "git checkout: you need to specify a new branch name"
118 if [ "$force" ]
119 then
120 git-read-tree --reset $new &&
121 git-checkout-index -q -f -u -a
122 else
123 git-update-index --refresh >/dev/null
124 git-read-tree -m -u $old $new || (
125 echo >&2 -n "Try automerge [y/N]? "
126 read yesno
127 case "$yesno" in [yY]*) ;; *) exit 1 ;; esac
129 # NEEDSWORK: We may want to reset the index from the $new for
130 # these paths after the automerge happens, but it is not done
131 # yet. Probably we need to leave unmerged ones alone, and
132 # yank the object name & mode from $new for cleanly merged
133 # paths and stuff them in the index.
135 names=`git diff-files --name-only`
136 case "$names" in
137 '') ;;
139 echo "$names" | git update-index --remove --stdin ;;
140 esac
142 work=`git write-tree` &&
143 git read-tree -m -u $old $work $new || exit
144 if result=`git write-tree 2>/dev/null`
145 then
146 echo >&2 "Trivially automerged." ;# can this even happen?
147 exit 0
149 git merge-index -o git-merge-one-file -a
154 # Switch the HEAD pointer to the new branch if we
155 # checked out a branch head, and remove any potential
156 # old MERGE_HEAD's (subsequent commits will clearly not
157 # be based on them, since we re-set the index)
159 if [ "$?" -eq 0 ]; then
160 if [ "$newbranch" ]; then
161 leading=`expr "refs/heads/$newbranch" : '\(.*\)/'` &&
162 mkdir -p "$GIT_DIR/$leading" &&
163 echo $new >"$GIT_DIR/refs/heads/$newbranch" || exit
164 branch="$newbranch"
166 [ "$branch" ] &&
167 GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD "refs/heads/$branch"
168 rm -f "$GIT_DIR/MERGE_HEAD"
169 else
170 exit 1