39
39
40
40
41
41
def get_diff_files (ref_a : str , ref_b : str , filter_mode : str = "" ) -> set [Path ]:
42
- """List the files changed between two Gif refs, filtered by change type."""
42
+ """List the files changed between two Git refs, filtered by change type."""
43
43
added_files_result = subprocess .run (
44
44
[
45
45
"git" ,
@@ -60,7 +60,7 @@ def get_diff_files(ref_a: str, ref_b: str, filter_mode: str = "") -> set[Path]:
60
60
61
61
62
62
def get_diff_lines (ref_a : str , ref_b : str , file : Path ) -> list [int ]:
63
- """List the lines changed between two Gif refs for a specific file."""
63
+ """List the lines changed between two Git refs for a specific file."""
64
64
diff_output = subprocess .run (
65
65
[
66
66
"git" ,
@@ -166,7 +166,7 @@ def process_touched_warnings(
166
166
return warnings_touched
167
167
168
168
169
- def check_and_annotate_changed (
169
+ def annotate_diff (
170
170
warnings : list [str ], ref_a : str = "main" , ref_b : str = "HEAD"
171
171
) -> None :
172
172
"""
@@ -183,8 +183,8 @@ def check_and_annotate_changed(
183
183
warnings_touched = process_touched_warnings (warnings , ref_a , ref_b )
184
184
print ("Emitting doc warnings matching modified lines:" )
185
185
for warning in warnings_touched :
186
- print (warning [0 ])
187
186
print ("::warning file={file},line={line}::{msg}" .format_map (warning ))
187
+ print (warning [0 ])
188
188
if not warnings_touched :
189
189
print ("None" )
190
190
@@ -232,13 +232,14 @@ def fail_if_improved(
232
232
return 0
233
233
234
234
235
- def main () -> int :
235
+ def main (argv : list [ str ] | None = None ) -> int :
236
236
parser = argparse .ArgumentParser ()
237
237
parser .add_argument (
238
- "--check-and-annotate-changed" ,
239
- nargs = 2 ,
240
- help = "Annotate lines changed between two refs "
241
- "with warnings on GitHub Actions" ,
238
+ "--annotate-diff" ,
239
+ nargs = "*" ,
240
+ metavar = ("BASE_REF" , "HEAD_REF" ),
241
+ help = "Add GitHub Actions annotations on the diff for warnings on "
242
+ "lines changed between the given refs (main and HEAD, by default)" ,
242
243
)
243
244
parser .add_argument (
244
245
"--fail-if-regression" ,
@@ -250,7 +251,13 @@ def main() -> int:
250
251
action = "store_true" ,
251
252
help = "Fail if new files with no nits are found" ,
252
253
)
253
- args = parser .parse_args ()
254
+
255
+ args = parser .parse_args (argv )
256
+ if args .annotate_diff is not None and len (args .annotate_diff ) > 2 :
257
+ parser .error (
258
+ "--annotate-diff takes between 0 and 2 ref args, not "
259
+ f"{ len (args .annotate_diff )} { tuple (args .annotate_diff )} "
260
+ )
254
261
exit_code = 0
255
262
256
263
wrong_directory_msg = "Must run this script from the repo root"
@@ -273,8 +280,8 @@ def main() -> int:
273
280
if filename .strip () and not filename .startswith ("#" )
274
281
}
275
282
276
- if args .check_and_annotate_changed :
277
- check_and_annotate_changed (warnings , * args .check_and_annotate_changed )
283
+ if args .annotate_diff is not None :
284
+ annotate_diff (warnings , * args .annotate_diff )
278
285
279
286
if args .fail_if_regression :
280
287
exit_code += fail_if_regression (
0 commit comments