|
20 | 20 | def _git_toplevel(path: str) -> str | None:
|
21 | 21 | try:
|
22 | 22 | cwd = os.path.abspath(path or ".")
|
23 |
| - res = _run(["git", "rev-parse", "HEAD"], cwd=cwd) |
24 |
| - if res.returncode: |
25 |
| - # BAIL if there is no commit |
26 |
| - log.error("listing git files failed - pretending there aren't any") |
27 |
| - return None |
28 |
| - res = _run( |
29 |
| - ["git", "rev-parse", "--show-prefix"], |
30 |
| - cwd=cwd, |
31 |
| - ) |
32 |
| - if res.returncode: |
33 |
| - return None |
34 |
| - out = res.stdout[:-1] # remove the trailing pathsep |
35 |
| - if not out: |
36 |
| - out = cwd |
37 |
| - else: |
38 |
| - # Here, ``out`` is a relative path to root of git. |
39 |
| - # ``cwd`` is absolute path to current working directory. |
40 |
| - # the below method removes the length of ``out`` from |
41 |
| - # ``cwd``, which gives the git toplevel |
42 |
| - assert cwd.replace("\\", "/").endswith(out), f"cwd={cwd!r}\nout={out!r}" |
43 |
| - # In windows cwd contains ``\`` which should be replaced by ``/`` |
44 |
| - # for this assertion to work. Length of string isn't changed by replace |
45 |
| - # ``\\`` is just and escape for `\` |
46 |
| - out = cwd[: -len(out)] |
47 |
| - log.debug("find files toplevel %s", out) |
48 |
| - return norm_real(out) |
| 23 | + res = _run(["git", "rev-parse", "--show-toplevel"], cwd=cwd, check=True) |
| 24 | + return res.stdout |
49 | 25 | except subprocess.CalledProcessError:
|
50 | 26 | # git returned error, we are not in a git repo
|
51 | 27 | return None
|
|
0 commit comments