Fetches the root of the git repo. This corresponds to the root directory in the case of a working tree, or the `.git/` directory in the case of a quarantine during pre-receive. :param wd: working directory, defaults to None :return: absolute path to the git root, as a string.
(wd: Optional[Union[str, Path]] = None)
| 160 | |
| 161 | |
| 162 | def get_git_root(wd: Optional[Union[str, Path]] = None) -> Path: |
| 163 | """ |
| 164 | Fetches the root of the git repo. |
| 165 | This corresponds to the root directory in the case of a working tree, |
| 166 | or the `.git/` directory in the case of a quarantine during pre-receive. |
| 167 | |
| 168 | :param wd: working directory, defaults to None |
| 169 | :return: absolute path to the git root, as a string. |
| 170 | """ |
| 171 | if wd is None: |
| 172 | wd = Path.cwd() |
| 173 | else: |
| 174 | wd = Path(wd) |
| 175 | check_git_dir(wd) |
| 176 | top_level = _git_rev_parse(option="--show-toplevel", wd=wd) |
| 177 | if top_level is not None: |
| 178 | return Path(top_level) |
| 179 | root = _git_rev_parse(option="--git-dir", wd=wd) |
| 180 | if root is None: |
| 181 | raise NotAGitDirectory() |
| 182 | return Path(root).resolve() |
| 183 | |
| 184 | |
| 185 | def check_git_dir(wd: Optional[Union[str, Path]] = None) -> None: |
no test coverage detected