3939
4040
4141def 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."""
4343 added_files_result = subprocess .run (
4444 [
4545 "git" ,
@@ -60,7 +60,7 @@ def get_diff_files(ref_a: str, ref_b: str, filter_mode: str = "") -> set[Path]:
6060
6161
6262def 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."""
6464 diff_output = subprocess .run (
6565 [
6666 "git" ,
@@ -166,7 +166,7 @@ def process_touched_warnings(
166166 return warnings_touched
167167
168168
169- def check_and_annotate_changed (
169+ def annotate_diff (
170170 warnings : list [str ], ref_a : str = "main" , ref_b : str = "HEAD"
171171) -> None :
172172 """
@@ -183,8 +183,8 @@ def check_and_annotate_changed(
183183 warnings_touched = process_touched_warnings (warnings , ref_a , ref_b )
184184 print ("Emitting doc warnings matching modified lines:" )
185185 for warning in warnings_touched :
186- print (warning [0 ])
187186 print ("::warning file={file},line={line}::{msg}" .format_map (warning ))
187+ print (warning [0 ])
188188 if not warnings_touched :
189189 print ("None" )
190190
@@ -232,13 +232,14 @@ def fail_if_improved(
232232 return 0
233233
234234
235- def main () -> int :
235+ def main (argv : list [ str ] | None = None ) -> int :
236236 parser = argparse .ArgumentParser ()
237237 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)" ,
242243 )
243244 parser .add_argument (
244245 "--fail-if-regression" ,
@@ -250,7 +251,13 @@ def main() -> int:
250251 action = "store_true" ,
251252 help = "Fail if new files with no nits are found" ,
252253 )
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+ )
254261 exit_code = 0
255262
256263 wrong_directory_msg = "Must run this script from the repo root"
@@ -273,8 +280,8 @@ def main() -> int:
273280 if filename .strip () and not filename .startswith ("#" )
274281 }
275282
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 )
278285
279286 if args .fail_if_regression :
280287 exit_code += fail_if_regression (
0 commit comments