Skip to content

Commit 1769b47

Browse files
committed
missing end loop, dtc
1 parent 4d4c17c commit 1769b47

File tree

1 file changed

+49
-25
lines changed

1 file changed

+49
-25
lines changed

tests/restyle.py

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
import sys
66
import pathlib
77
import subprocess
8-
import collections
98
import contextlib
109

10+
from dataclasses import dataclass
11+
1112
GIT_ROOT = pathlib.Path(
1213
subprocess.check_output(
1314
["git", "rev-parse", "--show-toplevel"], universal_newlines=True
@@ -79,7 +80,20 @@ def find_arduino_files():
7980
}
8081

8182

82-
Changed = collections.namedtuple("changed", "file hunk lines")
83+
@dataclass
84+
class Changed:
85+
file: str
86+
hunk: list[str]
87+
lines: list[str]
88+
89+
90+
@dataclass
91+
class Context:
92+
append_hunk: bool
93+
deleted: bool
94+
file: str
95+
hunk: list[str]
96+
markers: list[str]
8397

8498

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

97-
out = []
111+
def reset_context(line):
112+
if line:
113+
hunk = [line]
114+
else:
115+
hunk = []
116+
117+
return Context(
118+
append_hunk=False,
119+
deleted=False,
120+
file="",
121+
hunk=hunk,
122+
markers=[],
123+
)
98124

99-
deleted = False
100-
file = ""
101-
lines = []
125+
def pop(out, context, line):
126+
if ctx.file and ctx.hunk and ctx.markers:
127+
out.append(Changed(ctx.file, "\n".join(ctx.hunk), ", ".join(ctx.markers)))
102128

103-
append_hunk = False
104-
hunk = []
129+
context = reset_context(line)
130+
131+
out = []
132+
ctx = reset_context(None)
105133

106134
for line in proc.stdout.split("\n"):
107135
# '--- a/path/to/changed/file' most likely
108136
if line.startswith("---"):
109-
if file and hunk and lines:
110-
out.append(Changed(file, "\n".join(hunk), ", ".join(lines)))
111-
112-
deleted = False
113-
file = ""
114-
lines = []
115-
116-
append_hunk = False
117-
hunk = [line]
137+
pop(out, ctx, line)
118138

119139
# '+++ b/path/to/changed/file' most likely
120140
# '+++ /dev/null' aka removed file
121141
elif line.startswith("+++"):
122-
hunk.append(line)
142+
ctx.hunk.append(line)
123143

124144
_, file = line.split(" ")
125145
deleted = "/dev/null" in file
126146
if not deleted:
127-
file = file[2:]
147+
ctx.file = file[2:]
148+
else:
149+
ctx.file = file
128150

129151
# @@ from-file-line-numbers to-file-line-numbers @@
130-
elif not deleted and line.startswith("@@"):
131-
hunk.append(line)
152+
elif not ctx.deleted and line.startswith("@@"):
153+
ctx.hunk.append(line)
132154

133155
_, _, numbers, _ = line.split(" ", 3)
134156
if "," in numbers:
@@ -137,12 +159,14 @@ def changed_files():
137159
numbers = numbers.replace("+", "")
138160
numbers = numbers.replace("-", "")
139161

140-
lines.append(numbers)
141-
append_hunk = True
162+
ctx.markers.append(numbers)
163+
ctx.append_hunk = True
142164

143165
# capture diff for the summary
144-
elif append_hunk and line.startswith(("+", "-", " ")):
145-
hunk.append(line)
166+
elif ctx.append_hunk and line.startswith(("+", "-", " ")):
167+
ctx.hunk.append(line)
168+
169+
pop(out, ctx, line)
146170

147171
return out
148172

0 commit comments

Comments
 (0)