@@ -427,3 +427,115 @@ def test_status_modified_staged_then_modified(repo_init_with_commit, git2cpp_pat
427427 assert "Changes not staged for commit" in out
428428 # Ensure the filename appears (it should appear in one or both sections)
429429 assert "initial.txt" in out
430+
431+
432+ def test_status_detached_at_tag (repo_init_with_commit , git2cpp_path , tmp_path ):
433+ subprocess .run ([git2cpp_path , "tag" , "v1.0" ], cwd = tmp_path , check = True )
434+ subprocess .run ([git2cpp_path , "checkout" , "v1.0" ], cwd = tmp_path , check = True )
435+
436+ p = subprocess .run (
437+ [git2cpp_path , "status" ], capture_output = True , cwd = tmp_path , text = True , check = True
438+ )
439+ out = strip_ansi_colours (p .stdout )
440+ assert out .splitlines ()[0 ] == "HEAD detached at v1.0"
441+ assert "On branch" not in out
442+
443+
444+ def test_status_detached_at_sha (repo_init_with_commit , git2cpp_path , tmp_path ):
445+ p_sha = subprocess .run (
446+ [git2cpp_path , "rev-parse" , "HEAD" ],
447+ capture_output = True ,
448+ cwd = tmp_path ,
449+ text = True ,
450+ check = True ,
451+ )
452+ sha = p_sha .stdout .strip ()
453+ subprocess .run ([git2cpp_path , "checkout" , sha ], cwd = tmp_path , check = True )
454+
455+ p = subprocess .run (
456+ [git2cpp_path , "status" ], capture_output = True , cwd = tmp_path , text = True , check = True
457+ )
458+ out = strip_ansi_colours (p .stdout )
459+ assert out .splitlines ()[0 ] == f"HEAD detached at { sha [:7 ]} "
460+ assert "On branch" not in out
461+
462+
463+ def test_status_detached_from (repo_init_with_commit , git2cpp_path , tmp_path ):
464+ subprocess .run ([git2cpp_path , "tag" , "v1.0" ], cwd = tmp_path , check = True )
465+ subprocess .run ([git2cpp_path , "checkout" , "v1.0" ], cwd = tmp_path , check = True )
466+
467+ second = tmp_path / "second.txt"
468+ second .write_text ("second" )
469+ subprocess .run ([git2cpp_path , "add" , "second.txt" ], cwd = tmp_path , check = True )
470+ subprocess .run ([git2cpp_path , "commit" , "-m" , "Second commit" ], cwd = tmp_path , check = True )
471+
472+ p = subprocess .run (
473+ [git2cpp_path , "status" ], capture_output = True , cwd = tmp_path , text = True , check = True
474+ )
475+ out = strip_ansi_colours (p .stdout )
476+ assert out .splitlines ()[0 ] == "HEAD detached from v1.0"
477+ assert "On branch" not in out
478+
479+
480+ def test_status_detached_at_remote_branch (commit_env_config , git2cpp_path , tmp_path ):
481+ repo_path = tmp_path / "repo"
482+ repo_path .mkdir ()
483+ subprocess .run ([git2cpp_path , "init" , "-b" , "main" ], cwd = repo_path , check = True )
484+ (repo_path / "file.txt" ).write_text ("initial" )
485+ subprocess .run ([git2cpp_path , "add" , "file.txt" ], cwd = repo_path , check = True )
486+ subprocess .run ([git2cpp_path , "commit" , "-m" , "initial" ], cwd = repo_path , check = True )
487+
488+ clone_path = tmp_path / "clone"
489+ subprocess .run ([git2cpp_path , "clone" , str (repo_path ), str (clone_path )], check = True )
490+ subprocess .run ([git2cpp_path , "checkout" , "origin/main" ], cwd = clone_path , check = True )
491+
492+ p = subprocess .run (
493+ [git2cpp_path , "status" ], capture_output = True , cwd = clone_path , text = True , check = True
494+ )
495+ out = strip_ansi_colours (p .stdout )
496+ assert out .splitlines ()[0 ] == "HEAD detached at origin/main"
497+ assert "On branch" not in out
498+
499+
500+ def test_status_detached_no_reflog (repo_init_with_commit , git2cpp_path , tmp_path ):
501+ p_sha = subprocess .run (
502+ [git2cpp_path , "rev-parse" , "HEAD" ],
503+ capture_output = True ,
504+ cwd = tmp_path ,
505+ text = True ,
506+ check = True ,
507+ )
508+ sha = p_sha .stdout .strip ()
509+ subprocess .run ([git2cpp_path , "checkout" , sha ], cwd = tmp_path , check = True )
510+ os .remove (tmp_path / ".git" / "logs" / "HEAD" )
511+
512+ p = subprocess .run (
513+ [git2cpp_path , "status" ], capture_output = True , cwd = tmp_path , text = True , check = True
514+ )
515+ out = strip_ansi_colours (p .stdout )
516+ assert out .splitlines ()[0 ] == "Not currently on any branch."
517+ assert "On branch" not in out
518+
519+
520+ def test_status_short_branch_detached (repo_init_with_commit , git2cpp_path , tmp_path ):
521+ subprocess .run ([git2cpp_path , "tag" , "v1.0" ], cwd = tmp_path , check = True )
522+ subprocess .run ([git2cpp_path , "checkout" , "v1.0" ], cwd = tmp_path , check = True )
523+
524+ p = subprocess .run (
525+ [git2cpp_path , "status" , "-sb" ], capture_output = True , cwd = tmp_path , text = True , check = True
526+ )
527+ assert strip_ansi_colours (p .stdout ) == "## HEAD (no branch)\n "
528+
529+
530+ @pytest .mark .parametrize ("dirty" , [False , True ])
531+ def test_status_short_branch_newline (repo_init_with_commit , git2cpp_path , tmp_path , dirty ):
532+ if dirty :
533+ (tmp_path / "new.txt" ).write_text ("new" )
534+ expected = "## main\n ?? new.txt\n "
535+ else :
536+ expected = "## main\n "
537+
538+ p = subprocess .run (
539+ [git2cpp_path , "status" , "-sb" ], capture_output = True , cwd = tmp_path , text = True , check = True
540+ )
541+ assert strip_ansi_colours (p .stdout ) == expected
0 commit comments