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