Skip to content

Commit 9dff60f

Browse files
committed
Clean up _git_toplevel()
Instead of asking git what the relative path of the current directory is from the root of the repository and then reverse-engineering the root, simply call git rev-parse --show-toplevel. This option was added in git 1.7.0 back in 2010, so I think it's fair to assume it exists.
1 parent 9b6f031 commit 9dff60f

File tree

1 file changed

+2
-26
lines changed
  • src/setuptools_scm/_file_finders

1 file changed

+2
-26
lines changed

Diff for: src/setuptools_scm/_file_finders/git.py

+2-26
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,8 @@
2020
def _git_toplevel(path: str) -> str | None:
2121
try:
2222
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
4925
except subprocess.CalledProcessError:
5026
# git returned error, we are not in a git repo
5127
return None

0 commit comments

Comments
 (0)