Skip to content

Commit

Permalink
Distinguish git worktree finding errors. (pantsbuild#21232)
Browse files Browse the repository at this point in the history
Previously we would report an inability to locate the `git` binary as
"you're not in a git worktree", which is confusing.

Now we properly distinguish the cases in the user-facing error message.
  • Loading branch information
benjyw authored Jul 30, 2024
1 parent 965ddef commit 81a1cdb
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ async def generate_python_from_setuptools_scm(
softwrap(
f"""
Trying to determine the version for the {request.protocol_target.address} target at
{request.protocol_target.address}, but you are not running in a git worktree.
{request.protocol_target.address}, but {maybe_git_worktree.failure_reason}.
"""
)
)
Expand Down
8 changes: 5 additions & 3 deletions src/python/pants/vcs/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ def _parse_filename(self, match: re.Match) -> str | None:
@dataclass(frozen=True)
class MaybeGitWorktree(EngineAwareReturnType):
git_worktree: GitWorktree | None = None
failure_reason: str | None = None # If git_worktree is None, the reason why.

def cacheable(self) -> bool:
return False
Expand All @@ -272,7 +273,7 @@ async def get_git_worktree(
maybe_git_binary: MaybeGitBinary,
) -> MaybeGitWorktree:
if not maybe_git_binary.git_binary:
return MaybeGitWorktree()
return MaybeGitWorktree(failure_reason="couldn't find `git` binary")

git_binary = maybe_git_binary.git_binary
cmd = ["rev-parse", "--show-toplevel"]
Expand All @@ -284,8 +285,9 @@ async def get_git_worktree(
else:
output = git_binary._invoke_unsandboxed(cmd)
except GitBinaryException as e:
logger.info(f"No git repository at {os.getcwd()}: {e!r}")
return MaybeGitWorktree()
failure_msg = f"no git repository at {os.getcwd()}: {e!r}"
logger.info(failure_msg)
return MaybeGitWorktree(failure_reason=failure_msg)

git_worktree = GitWorktree(
binary=git_binary,
Expand Down

0 comments on commit 81a1cdb

Please sign in to comment.