@@ -34,6 +34,51 @@ def test_git_checkout_nonexistent_branch(test_repository):
3434 git_checkout (test_repository , "nonexistent-branch" )
3535
3636
37+ def test_git_checkout_sha_reports_detached_head (test_repository ):
38+ """rev_parse accepts a sha, so the reply must not claim a branch switch."""
39+ sha = test_repository .head .commit .hexsha
40+ result = git_checkout (test_repository , sha )
41+
42+ assert test_repository .head .is_detached
43+ assert "detached" in result
44+ assert "Switched to branch" not in result
45+
46+
47+ @pytest .mark .parametrize ("revision" , ["HEAD~1" , "refs/heads/feature" , "origin/main" ])
48+ def test_git_checkout_non_branch_revisions_report_detached_head (
49+ test_repository , tmp_path , revision
50+ ):
51+ """The revisions an agent is most likely to send: a relative ref, a full ref name, and a
52+ remote-tracking ref. None of them is a branch, so none may be reported as a branch switch."""
53+ test_repository .git .branch ("feature" )
54+ test_repository .index .commit ("second commit" )
55+ git .Repo .init (tmp_path / "remote.git" , bare = True )
56+ test_repository .create_remote ("origin" , str (tmp_path / "remote.git" ))
57+ test_repository .git .push ("origin" , "HEAD:refs/heads/main" )
58+
59+ result = git_checkout (test_repository , revision )
60+
61+ assert test_repository .head .is_detached
62+ assert result .startswith ("HEAD is now detached at " )
63+
64+
65+ def test_git_checkout_tag_reports_detached_head (test_repository ):
66+ test_repository .create_tag ("v1" )
67+ result = git_checkout (test_repository , "v1" )
68+
69+ assert test_repository .head .is_detached
70+ assert "detached" in result
71+ assert "Switched to branch" not in result
72+
73+
74+ def test_git_checkout_branch_name_still_reports_branch (test_repository ):
75+ test_repository .git .branch ("attached-checkout" )
76+ result = git_checkout (test_repository , "attached-checkout" )
77+
78+ assert not test_repository .head .is_detached
79+ assert result == "Switched to branch 'attached-checkout'"
80+
81+
3782def test_git_branch_local (test_repository ):
3883 test_repository .git .branch ("new-branch-local" )
3984 result = git_branch (test_repository , "local" )
0 commit comments