File tree Expand file tree Collapse file tree 2 files changed +16
-3
lines changed
Expand file tree Collapse file tree 2 files changed +16
-3
lines changed Original file line number Diff line number Diff line change @@ -1046,9 +1046,9 @@ def active_branch(self) -> Head:
10461046 :class:`~git.refs.head.Head` to the active branch
10471047 """
10481048 try :
1049- return Head (self , self .git .symbolic_ref ("HEAD" ).strip ())
1049+ return Head (self , self .git .symbolic_ref ("-q" , " HEAD" ).strip ())
10501050 except GitCommandError as err :
1051- if err .status == 1 or "not a symbolic ref" in str ( err ). lower () :
1051+ if err .status == 1 :
10521052 raise TypeError ("HEAD is a detached symbolic reference as it points to a commit" ) from err
10531053 raise
10541054
Original file line number Diff line number Diff line change @@ -973,9 +973,22 @@ def test_empty_repo_reftable_active_branch(self, rw_dir):
973973 raise
974974
975975 repo = Repo (rw_dir )
976- self .assertEqual (repo .active_branch .name , "master" )
976+ expected_active_branch_path = git .symbolic_ref ("HEAD" ).strip ()
977+ expected_active_branch_name = git .symbolic_ref ("--short" , "HEAD" ).strip ()
978+ self .assertEqual (repo .active_branch .path , expected_active_branch_path )
979+ self .assertEqual (repo .active_branch .name , expected_active_branch_name )
977980 assert not repo .active_branch .is_valid (), "Branch is yet to be born"
978981
982+ @with_rw_directory
983+ def test_active_branch_raises_type_error_when_head_is_detached (self , rw_dir ):
984+ repo = Repo .init (rw_dir )
985+ with open (osp .join (rw_dir , "a.txt" ), "w" ) as f :
986+ f .write ("a" )
987+ repo .index .add (["a.txt" ])
988+ repo .index .commit ("initial commit" )
989+ repo .git .checkout (repo .head .commit .hexsha )
990+ self .assertRaisesRegex (TypeError , "detached symbolic reference" , lambda : repo .active_branch )
991+
979992 def test_merge_base (self ):
980993 repo = self .rorepo
981994 c1 = "f6aa8d1"
You can’t perform that action at this time.
0 commit comments