diff --git a/docs/changelog.d/+quiet-revision.bugfix.rst b/docs/changelog.d/+quiet-revision.bugfix.rst new file mode 100644 index 0000000..cd694c0 --- /dev/null +++ b/docs/changelog.d/+quiet-revision.bugfix.rst @@ -0,0 +1,2 @@ +A wheel built from an sdist no longer warns that git cannot read the source revision when the sdist names the +commit, as when uv builds it under the empty ``.git`` file it keeps in its cache. diff --git a/docs/reference/configuration.rst b/docs/reference/configuration.rst index 1112a52..7d352b9 100644 --- a/docs/reference/configuration.rst +++ b/docs/reference/configuration.rst @@ -110,8 +110,9 @@ The SBOM names the commit the wheel was built from. It takes the first source th A commit must be a full hash of 40 or 64 hex characters. A checkout with changes, including untracked files that are not ignored, gets a ``:vcs-dirty`` property, and the link to the revision says so; its sdist records no revision. -When git cannot read the checkout, the build warns and moves on to the next source. git runs with ``core.fsmonitor`` -off, so a repository's configuration runs no command. +When git cannot read the checkout, the build moves on to the next source, and warns with git's error only when none of +them names the commit. uv unpacks an sdist under an empty ``.git`` file that git rejects, so this keeps its builds +quiet. git runs with ``core.fsmonitor`` off, so a repository's configuration runs no command. ******************* Validation limits diff --git a/src/python_sbom/_collect/_revision.py b/src/python_sbom/_collect/_revision.py index b6af9d1..89271f9 100644 --- a/src/python_sbom/_collect/_revision.py +++ b/src/python_sbom/_collect/_revision.py @@ -36,17 +36,16 @@ def default_revision(root: Path, /) -> Revision | None: # the variable exists for builds from an sdist, so it wins over whatever checkout the sdist was unpacked into if commit := os.environ.get("PYTHON_SBOM_COMMIT"): return _commit_revision(commit, "PYTHON_SBOM_COMMIT") - if (found := _git_revision(root)) is not None: - return found - # the sdist build wrote it from the revision it resolved, so it is at least as exact as an archival file - if (recorded := root / REVISION_FILE).is_file(): - return _commit_revision(recorded.read_text(encoding="utf-8").strip().removeprefix("node: "), REVISION_FILE) - archival = root / ".git_archival.txt" - if archival.is_file() and ( - match := re.search(rf"^node: ({_COMMIT.pattern})$", archival.read_text(encoding="utf-8"), re.MULTILINE) - ): - return Revision(commit=match[1], dirty=False) - return None + try: + if (found := _git_revision(root)) is not None: + return found + except subprocess.CalledProcessError as error: + # uv unpacks an sdist under an empty .git file that git rejects, and the sdist's record then names the commit, + # so a git failure is worth a warning only when nothing else does + if (recorded := _recorded_revision(root)) is None: + warnings.warn(f"git cannot read the source revision in {root}: {error.stderr.strip()}", stacklevel=2) + return recorded + return _recorded_revision(root) def _git_revision(root: Path, /) -> Revision | None: @@ -62,9 +61,9 @@ def _git_revision(root: Path, /) -> Revision | None: # untracked files count, since the build packages every file .gitignore lets through changes = _git(git, root, "status", "--porcelain", "--", ".") except subprocess.CalledProcessError as error: - if error.returncode != 1: # 1: the project is untracked, or the checkout has no commit yet - warnings.warn(f"git cannot read the source revision in {root}: {error.stderr.strip()}", stacklevel=2) - return None + if error.returncode == 1: # the project is untracked, or the checkout has no commit yet + return None + raise return Revision(commit=commit, dirty=bool(changes)) @@ -79,6 +78,18 @@ def _git(git: str, root: Path, *arguments: str) -> str: ).stdout.strip() +def _recorded_revision(root: Path, /) -> Revision | None: + # the sdist build wrote it from the revision it resolved, so it is at least as exact as an archival file + if (recorded := root / REVISION_FILE).is_file(): + return _commit_revision(recorded.read_text(encoding="utf-8").strip().removeprefix("node: "), REVISION_FILE) + archival = root / ".git_archival.txt" + if archival.is_file() and ( + match := re.search(rf"^node: ({_COMMIT.pattern})$", archival.read_text(encoding="utf-8"), re.MULTILINE) + ): + return Revision(commit=match[1], dirty=False) + return None + + def _commit_revision(commit: str, origin: str) -> Revision: # the commit lands in the source link, so anything but a full hash could point the link elsewhere if not _COMMIT.fullmatch(commit): diff --git a/tests/_collect/test_revision.py b/tests/_collect/test_revision.py index e6f23ed..e328757 100644 --- a/tests/_collect/test_revision.py +++ b/tests/_collect/test_revision.py @@ -141,21 +141,35 @@ def test_git_revision_without_git(checkout: Path, monkeypatch: pytest.MonkeyPatc assert default_revision(checkout) is None -@pytest.mark.parametrize( - "git_file", - [ +@pytest.fixture( + params=[ # a worktree copied without the main checkout, as into a container pytest.param("gitdir: {root}/missing\n", id="missing-gitdir"), # the marker uv leaves in an unpacked sdist it builds from pytest.param("", id="empty"), - ], + ] ) -def test_git_failure_warns_and_falls_back(tmp_path: Path, git_file: str) -> None: - (tmp_path / ".git").write_text(git_file.format(root=tmp_path.as_posix()), encoding="utf-8") +def unreadable_checkout(tmp_path: Path, request: pytest.FixtureRequest) -> Path: + (tmp_path / ".git").write_text(request.param.format(root=tmp_path.as_posix()), encoding="utf-8") (tmp_path / "pyproject.toml").write_text("[project]\n", encoding="utf-8") - (tmp_path / ".git_archival.txt").write_text(f"node: {'f' * 40}\n", encoding="utf-8") + return tmp_path + + +@pytest.mark.parametrize( + "record", + [ + pytest.param(REVISION_FILE, id="revision-file"), + pytest.param(".git_archival.txt", id="archival"), + ], +) +def test_git_failure_falls_back_quietly(unreadable_checkout: Path, record: str) -> None: + (unreadable_checkout / record).write_text(f"node: {'f' * 40}\n", encoding="utf-8") + assert default_revision(unreadable_checkout) == Revision(commit="f" * 40, dirty=False) + + +def test_git_failure_warns_without_fallback(unreadable_checkout: Path) -> None: with pytest.warns(UserWarning, match="git cannot read the source revision in .*: fatal: "): - assert default_revision(tmp_path) == Revision(commit="f" * 40, dirty=False) + assert default_revision(unreadable_checkout) is None def test_git_status_failure_leaves_revision_unknown(checkout: Path, git: Git) -> None: