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