Skip to content

Pre-commit hook fails when pyproject.toml is staged without dependency-line changes #6

Description

@gkennos

The canonical pre-commit hook can exit with status 1 when pyproject.toml is staged but the staged diff does not contain any quoted dependency lines.

This is separate from #3

That issue covers the macOS/BSD sed portability problem where \s leaves the diff marker in the extracted package name. This issue covers the empty-match path under set -euo pipefail

The hook currently builds dependency candidates with a pipeline like:

changed_names=$(git diff --cached -- pyproject.toml \
    | grep -E '^[+-]\s*"[A-Za-z0-9][A-Za-z0-9._-]*' \
    | sed -E 's/^[+-]\s*"([A-Za-z0-9][A-Za-z0-9._-]*).*/\1/' \
    | sort -u)

If pyproject.toml is staged but no dependency-like line matches, grep exits 1

Because the hook uses set -euo pipefail, the command substitution exits before the later empty-result guard can run

proposed fix:

changed_names=$(git diff --cached -- pyproject.toml \
    | sed -nE 's/^[+-][[:space:]]*"([A-Za-z0-9][A-Za-z0-9._-]*).*/\1/p' \
    | sort -u)

this will also fix #3

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions