5
5
import sys
6
6
import pathlib
7
7
import subprocess
8
- import collections
9
8
import contextlib
10
9
10
+ from dataclasses import dataclass
11
+
11
12
GIT_ROOT = pathlib .Path (
12
13
subprocess .check_output (
13
14
["git" , "rev-parse" , "--show-toplevel" ], universal_newlines = True
@@ -79,7 +80,20 @@ def find_arduino_files():
79
80
}
80
81
81
82
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 ]
83
97
84
98
85
99
# naive git-diff parser for clang-format aftercare
@@ -94,41 +108,49 @@ def changed_files():
94
108
universal_newlines = True ,
95
109
)
96
110
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
+ )
98
124
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 )))
102
128
103
- append_hunk = False
104
- hunk = []
129
+ context = reset_context (line )
130
+
131
+ out = []
132
+ ctx = reset_context (None )
105
133
106
134
for line in proc .stdout .split ("\n " ):
107
135
# '--- a/path/to/changed/file' most likely
108
136
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 )
118
138
119
139
# '+++ b/path/to/changed/file' most likely
120
140
# '+++ /dev/null' aka removed file
121
141
elif line .startswith ("+++" ):
122
- hunk .append (line )
142
+ ctx . hunk .append (line )
123
143
124
144
_ , file = line .split (" " )
125
145
deleted = "/dev/null" in file
126
146
if not deleted :
127
- file = file [2 :]
147
+ ctx .file = file [2 :]
148
+ else :
149
+ ctx .file = file
128
150
129
151
# @@ 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 )
132
154
133
155
_ , _ , numbers , _ = line .split (" " , 3 )
134
156
if "," in numbers :
@@ -137,12 +159,14 @@ def changed_files():
137
159
numbers = numbers .replace ("+" , "" )
138
160
numbers = numbers .replace ("-" , "" )
139
161
140
- lines .append (numbers )
141
- append_hunk = True
162
+ ctx . markers .append (numbers )
163
+ ctx . append_hunk = True
142
164
143
165
# 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 )
146
170
147
171
return out
148
172
0 commit comments