Skip to content

Commit

Permalink
missing end loop, dtc
Browse files Browse the repository at this point in the history
  • Loading branch information
mcspr committed Jul 31, 2024
1 parent 4d4c17c commit 1769b47
Showing 1 changed file with 49 additions and 25 deletions.
74 changes: 49 additions & 25 deletions tests/restyle.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
import sys
import pathlib
import subprocess
import collections
import contextlib

from dataclasses import dataclass

GIT_ROOT = pathlib.Path(
subprocess.check_output(
["git", "rev-parse", "--show-toplevel"], universal_newlines=True
Expand Down Expand Up @@ -79,7 +80,20 @@ def find_arduino_files():
}


Changed = collections.namedtuple("changed", "file hunk lines")
@dataclass
class Changed:
file: str
hunk: list[str]
lines: list[str]


@dataclass
class Context:
append_hunk: bool
deleted: bool
file: str
hunk: list[str]
markers: list[str]


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

out = []
def reset_context(line):
if line:
hunk = [line]
else:
hunk = []

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

deleted = False
file = ""
lines = []
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)))

append_hunk = False
hunk = []
context = reset_context(line)

out = []
ctx = reset_context(None)

for line in proc.stdout.split("\n"):
# '--- a/path/to/changed/file' most likely
if line.startswith("---"):
if file and hunk and lines:
out.append(Changed(file, "\n".join(hunk), ", ".join(lines)))

deleted = False
file = ""
lines = []

append_hunk = False
hunk = [line]
pop(out, ctx, line)

# '+++ b/path/to/changed/file' most likely
# '+++ /dev/null' aka removed file
elif line.startswith("+++"):
hunk.append(line)
ctx.hunk.append(line)

_, file = line.split(" ")
deleted = "/dev/null" in file
if not deleted:
file = file[2:]
ctx.file = file[2:]
else:
ctx.file = file

# @@ from-file-line-numbers to-file-line-numbers @@
elif not deleted and line.startswith("@@"):
hunk.append(line)
elif not ctx.deleted and line.startswith("@@"):
ctx.hunk.append(line)

_, _, numbers, _ = line.split(" ", 3)
if "," in numbers:
Expand All @@ -137,12 +159,14 @@ def changed_files():
numbers = numbers.replace("+", "")
numbers = numbers.replace("-", "")

lines.append(numbers)
append_hunk = True
ctx.markers.append(numbers)
ctx.append_hunk = True

# capture diff for the summary
elif append_hunk and line.startswith(("+", "-", " ")):
hunk.append(line)
elif ctx.append_hunk and line.startswith(("+", "-", " ")):
ctx.hunk.append(line)

pop(out, ctx, line)

return out

Expand Down

0 comments on commit 1769b47

Please sign in to comment.