Skip to content

Commit

Permalink
Get revrange from env vars with -r :PRE-COMMIT:
Browse files Browse the repository at this point in the history
Fixes #103
  • Loading branch information
akaihola committed Dec 26, 2020
1 parent a31f219 commit de19598
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
- id: darker
name: darker
description: "Black reformatting to Python files only in regions changed since last commit"
entry: darker
entry: "darker -r :PRE-COMMIT:"
language: python
language_version: python3
require_serial: true
Expand Down
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ These features will be included in the next release:

Added
-----
- Get revision range from pre-commit_'s ``PRE_COMMIT_FROM_REF`` and
``PRE_COMMIT_TO_REF`` environment variables when using the ``--revision :PRE-COMMIT:``
option
- Configure a pre-commit hook for Darker itself

Fixed
Expand Down
20 changes: 19 additions & 1 deletion src/darker/git.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Helpers for listing modified files and getting unmodified content from Git"""

import logging
import os
import re
import sys
from dataclasses import dataclass
Expand All @@ -22,8 +23,11 @@


# A colon is an invalid character in tag/branch names. Use that in the special value for
# denoting the working tree as one of the "revisions" in revision ranges.
# - denoting the working tree as one of the "revisions" in revision ranges
# - referring to the `PRE_COMMIT_FROM_REF` and `PRE_COMMIT_TO_REF` environment variables
# for determining the revision range
WORKTREE = ":WORKTREE:"
PRE_COMMIT_FROM_TO_REFS = ":PRE-COMMIT:"


def git_get_content_at_revision(path: Path, revision: str, cwd: Path) -> List[str]:
Expand Down Expand Up @@ -89,6 +93,20 @@ def parse(cls, revision_range: str) -> "RevisionRange":
RevisionRange(rev1='a', rev2=':WORKTREE:', use_common_ancestor=True)
"""
if revision_range == PRE_COMMIT_FROM_TO_REFS:
try:
return cls(
os.environ["PRE_COMMIT_FROM_REF"],
os.environ["PRE_COMMIT_TO_REF"],
use_common_ancestor=True,
)
except KeyError:
logger.error(
"The environment variables PRE_COMMIT_FROM_REF and"
" PRE_COMMIT_TO_REF must be defined when using -r / --revision"
" :PRE-COMMIT:"
)
sys.exit(123)
match = COMMIT_RANGE_RE.match(revision_range)
if match:
rev1, range_dots, rev2 = match.groups()
Expand Down
23 changes: 23 additions & 0 deletions src/darker/tests/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# pylint: disable=redefined-outer-name

import os
from pathlib import Path
from unittest.mock import patch

Expand All @@ -15,6 +16,7 @@
should_reformat_file,
)
from darker.tests.conftest import GitRepoFixture
from darker.tests.helpers import raises_if_exception


@pytest.mark.parametrize(
Expand Down Expand Up @@ -271,6 +273,27 @@ def test_git_get_modified_files_revision_range(
assert {path.name for path in result} == expect


@pytest.mark.parametrize(
"environ, expect",
[
({}, SystemExit),
({"PRE_COMMIT_FROM_REF": "old"}, SystemExit),
({"PRE_COMMIT_TO_REF": "new"}, SystemExit),
({"PRE_COMMIT_FROM_REF": "old", "PRE_COMMIT_TO_REF": "new"}, ["old", "new"]),
],
)
def test_revisionrange_parse_pre_commit(environ, expect):
"""RevisionRange.parse(':PRE-COMMIT:') gets the range from environment variables"""
with patch.dict(os.environ, environ), raises_if_exception(expect):

result = RevisionRange.parse(":PRE-COMMIT:")

expect_rev1, expect_rev2 = expect
assert result.rev1 == expect_rev1
assert result.rev2 == expect_rev2
assert result.use_common_ancestor


edited_linenums_differ_cases = pytest.mark.parametrize(
"context_lines, expect",
[
Expand Down

0 comments on commit de19598

Please sign in to comment.