7
7
import subprocess
8
8
import contextlib
9
9
10
- from dataclasses import dataclass
10
+ from dataclasses import dataclass , fields
11
+ from dataclasses import field as dataclass_field
12
+
11
13
12
14
GIT_ROOT = pathlib .Path (
13
15
subprocess .check_output (
@@ -89,11 +91,11 @@ class Changed:
89
91
90
92
@dataclass
91
93
class Context :
92
- append_hunk : bool
93
- deleted : bool
94
- file : str
95
- hunk : list [str ]
96
- markers : list [str ]
94
+ append_hunk : bool = False
95
+ deleted : bool = False
96
+ file : str = ""
97
+ hunk : list [str ] = dataclass_field ( default_factory = list )
98
+ markers : list [str ] = dataclass_field ( default_factory = list )
97
99
98
100
99
101
# naive git-diff parser for clang-format aftercare
@@ -108,28 +110,24 @@ def changed_files():
108
110
universal_newlines = True ,
109
111
)
110
112
111
- def reset_context (line ):
113
+ def reset_context (ctx ):
114
+ other = Context ()
115
+ for field in fields (other ):
116
+ setattr (ctx , field .name , getattr (other , field .name ))
117
+
118
+ def reset_with_line (ctx , line ):
119
+ reset_context (ctx )
112
120
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
- )
121
+ ctx .hunk = [line ]
124
122
125
123
def pop (out , context , line ):
126
124
if ctx .file and ctx .hunk and ctx .markers :
127
125
out .append (Changed (ctx .file , "\n " .join (ctx .hunk ), ", " .join (ctx .markers )))
128
126
129
- context = reset_context ( line )
127
+ reset_with_line ( context , line )
130
128
131
129
out = []
132
- ctx = reset_context ( None )
130
+ ctx = Context ( )
133
131
134
132
for line in proc .stdout .split ("\n " ):
135
133
# '--- a/path/to/changed/file' most likely
@@ -180,6 +178,7 @@ def warning_changed(changed: Changed):
180
178
SUMMARY_PATH = pathlib .Path (os .environ .get ("GITHUB_STEP_SUMMARY" , os .devnull ))
181
179
SUMMARY_OUTPUT = SUMMARY_PATH .open ("a" )
182
180
181
+
183
182
def summary_diff (changed : Changed ):
184
183
with contextlib .redirect_stdout (SUMMARY_OUTPUT ):
185
184
print (f"# { changed .file } (suggested change)" )
0 commit comments