@matheusbsbs / matheus-bagliano / commits / 5035242c47
Fork 0

checkout: do not get confused with ambiguous tag/branch names

Although it is not advisable, we have always allowed a branch and a tag to have the same basename (i.e. it is not illegal to have refs/heads/frotz and refs/tags/frotz at the same time). When talking about a specific commit, the interpretation of 'frotz' has always been "use tag and then check branch", although we warn when ambiguities exist. However "git checkout $name" is defined to (1) first see if it matches the branch name, and if so switch to that branch; (2) otherwise it is an instruction to detach HEAD to point at the commit named by $name. We did not follow this definition when $name appeared under both refs/heads/ and refs/tags/ -- we switched to the branch but read the tree from the tagged commit, which was utterly bogus. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Jun 8, 2007 at 01:19 UTC 5035242c4785bd23c53827a1656b5f97394f724e
2 files changed +42 -1
git-checkout.sh
+2 -1
@@ -63,12 +63,13 @@ while [ "$#" != "0" ]; do
63 echo "unknown flag $arg"
64 exit 1
65 fi
66 - new="$rev"
66 new_name="$arg"
67 if git-show-ref --verify --quiet -- "refs/heads/$arg"
68 then
69 + rev=$(git-rev-parse --verify "refs/heads/$arg^0")
70 branch="$arg"
71 fi
72 + new="$rev"
73 elif rev=$(git-rev-parse --verify "$arg^{tree}" 2>/dev/null)
74 then
75 # checking out selected paths from a tree-ish.
t/t7201-co.sh
+40
@@ -190,4 +190,44 @@ test_expect_success 'checkout to detach HEAD with HEAD^0' '
190 fi
191 '
192
193 +test_expect_success 'checkout with ambiguous tag/branch names' '
194 +
195 + git tag both side &&
196 + git branch both master &&
197 + git reset --hard &&
198 + git checkout master &&
199 +
200 + git checkout both &&
201 + H=$(git rev-parse --verify HEAD) &&
202 + M=$(git show-ref -s --verify refs/heads/master) &&
203 + test "z$H" = "z$M" &&
204 + name=$(git symbolic-ref HEAD 2>/dev/null) &&
205 + test "z$name" = zrefs/heads/both
206 +
207 +'
208 +
209 +test_expect_success 'checkout with ambiguous tag/branch names' '
210 +
211 + git reset --hard &&
212 + git checkout master &&
213 +
214 + git tag frotz side &&
215 + git branch frotz master &&
216 + git reset --hard &&
217 + git checkout master &&
218 +
219 + git checkout tags/frotz &&
220 + H=$(git rev-parse --verify HEAD) &&
221 + S=$(git show-ref -s --verify refs/heads/side) &&
222 + test "z$H" = "z$S" &&
223 + if name=$(git symbolic-ref HEAD 2>/dev/null)
224 + then
225 + echo "Bad -- should have detached"
226 + false
227 + else
228 + : happy
229 + fi
230 +
231 +'
232 +
233 test_done