@@ -57,13 +57,15 @@ def get_root() -> str:
5757 root = os .path .abspath (
5858 cmd_output ('git' , 'rev-parse' , '--show-cdup' )[1 ].strip (),
5959 )
60- git_dir = os .path .abspath (get_git_dir ())
60+ inside_git_dir = cmd_output (
61+ 'git' , 'rev-parse' , '--is-inside-git-dir' ,
62+ )[1 ].strip ()
6163 except CalledProcessError :
6264 raise FatalError (
6365 'git failed. Is it installed, and are you in a Git repository '
6466 'directory?' ,
6567 )
66- if os . path . samefile ( root , git_dir ) :
68+ if inside_git_dir != 'false' :
6769 raise FatalError (
6870 'git toplevel unexpectedly empty! make sure you are not '
6971 'inside the `.git` directory of your repository.' ,
@@ -72,15 +74,25 @@ def get_root() -> str:
7274
7375
7476def get_git_dir (git_root : str = '.' ) -> str :
75- opts = ( '--git-common- dir' , '--git-dir' )
76- _ , out , _ = cmd_output ('git' , 'rev-parse' , * opts , cwd = git_root )
77- for line , opt in zip ( out .splitlines (), opts ):
78- if line != opt : # pragma: no branch (git < 2.5)
79- return os .path .normpath (os .path .join (git_root , line ))
77+ opt = '--git-dir'
78+ _ , out , _ = cmd_output ('git' , 'rev-parse' , opt , cwd = git_root )
79+ git_dir = out .strip ()
80+ if git_dir != opt :
81+ return os .path .normpath (os .path .join (git_root , git_dir ))
8082 else :
8183 raise AssertionError ('unreachable: no git dir' )
8284
8385
86+ def get_git_common_dir (git_root : str = '.' ) -> str :
87+ opt = '--git-common-dir'
88+ _ , out , _ = cmd_output ('git' , 'rev-parse' , opt , cwd = git_root )
89+ git_common_dir = out .strip ()
90+ if git_common_dir != opt :
91+ return os .path .normpath (os .path .join (git_root , git_common_dir ))
92+ else : # pragma: no cover (git < 2.5)
93+ return get_git_dir (git_root )
94+
95+
8496def get_remote_url (git_root : str ) -> str :
8597 _ , out , _ = cmd_output ('git' , 'config' , 'remote.origin.url' , cwd = git_root )
8698 return out .strip ()
0 commit comments