Skip to content

Commit

Permalink
python refs vs refs
Browse files Browse the repository at this point in the history
  • Loading branch information
mcspr committed Jul 31, 2024
1 parent 697ea8b commit 249189e
Showing 1 changed file with 19 additions and 20 deletions.
39 changes: 19 additions & 20 deletions tests/restyle.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import subprocess
import contextlib

from dataclasses import dataclass
from dataclasses import dataclass, fields
from dataclasses import field as dataclass_field


GIT_ROOT = pathlib.Path(
subprocess.check_output(
Expand Down Expand Up @@ -89,11 +91,11 @@ class Changed:

@dataclass
class Context:
append_hunk: bool
deleted: bool
file: str
hunk: list[str]
markers: list[str]
append_hunk: bool = False
deleted: bool = False
file: str = ""
hunk: list[str] = dataclass_field(default_factory=list)
markers: list[str] = dataclass_field(default_factory=list)


# naive git-diff parser for clang-format aftercare
Expand All @@ -108,28 +110,24 @@ def changed_files():
universal_newlines=True,
)

def reset_context(line):
def reset_context(ctx):
other = Context()
for field in fields(other):
setattr(ctx, field.name, getattr(other, field.name))

def reset_with_line(ctx, line):
reset_context(ctx)
if line:
hunk = [line]
else:
hunk = []

return Context(
append_hunk=False,
deleted=False,
file="",
hunk=hunk,
markers=[],
)
ctx.hunk = [line]

def pop(out, context, line):
if ctx.file and ctx.hunk and ctx.markers:
out.append(Changed(ctx.file, "\n".join(ctx.hunk), ", ".join(ctx.markers)))

context = reset_context(line)
reset_with_line(context, line)

out = []
ctx = reset_context(None)
ctx = Context()

for line in proc.stdout.split("\n"):
# '--- a/path/to/changed/file' most likely
Expand Down Expand Up @@ -180,6 +178,7 @@ def warning_changed(changed: Changed):
SUMMARY_PATH = pathlib.Path(os.environ.get("GITHUB_STEP_SUMMARY", os.devnull))
SUMMARY_OUTPUT = SUMMARY_PATH.open("a")


def summary_diff(changed: Changed):
with contextlib.redirect_stdout(SUMMARY_OUTPUT):
print(f"# {changed.file} (suggested change)")
Expand Down

0 comments on commit 249189e

Please sign in to comment.