Skip to content

Clean up _git_toplevel() #1105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 2 additions & 27 deletions src/setuptools_scm/_file_finders/git.py
Original file line number Diff line number Diff line change
@@ -19,33 +19,8 @@

def _git_toplevel(path: str) -> str | None:
try:
cwd = os.path.abspath(path or ".")
res = _run(["git", "rev-parse", "HEAD"], cwd=cwd)
if res.returncode:
# BAIL if there is no commit
log.error("listing git files failed - pretending there aren't any")
return None
res = _run(
["git", "rev-parse", "--show-prefix"],
cwd=cwd,
)
if res.returncode:
return None
out = res.stdout[:-1] # remove the trailing pathsep
if not out:
out = cwd
else:
# Here, ``out`` is a relative path to root of git.
# ``cwd`` is absolute path to current working directory.
# the below method removes the length of ``out`` from
# ``cwd``, which gives the git toplevel
assert cwd.replace("\\", "/").endswith(out), f"cwd={cwd!r}\nout={out!r}"
# In windows cwd contains ``\`` which should be replaced by ``/``
# for this assertion to work. Length of string isn't changed by replace
# ``\\`` is just and escape for `\`
out = cwd[: -len(out)]
log.debug("find files toplevel %s", out)
return norm_real(out)
res = _run(["git", "rev-parse", "--show-toplevel"], cwd=path, check=True)
return res.stdout
except subprocess.CalledProcessError:
# git returned error, we are not in a git repo
return None