2020import argparse
2121import difflib
2222import json
23+ import os
2324import re
2425import subprocess
2526import sys
27+ import tempfile
2628from pathlib import Path
2729
2830# ---------------------------------------------------------------------------
@@ -203,9 +205,10 @@ def _unique_components(items: list[dict]) -> list[str]:
203205 return out
204206
205207
208+ # NOTE: _unique_components and _render_command are duplicated in post_render_comment.py
206209def _render_command (components : list [str ], use_all : bool = False ) -> str :
207210 if use_all or len (components ) > 30 :
208- return "azldev component render -a"
211+ return "azldev component render -a --clean-stale "
209212 return f"azldev component render { ' ' .join (components )} "
210213
211214
@@ -225,51 +228,53 @@ def generate_patch(
225228 if not paths :
226229 return b""
227230
228- # Write paths to a file to avoid ARG_MAX limits
229- pathspec_file = Path ("render-check-pathspec.txt" )
230- pathspec_file .write_text ("\n " .join (paths ), encoding = "utf-8" )
231+ # Write paths to temp files outside CWD (which may be an untrusted
232+ # PR checkout where symlinks could redirect writes).
233+ pathspec_fd , pathspec_path = tempfile .mkstemp (prefix = "render-check-" , suffix = ".txt" )
234+ with os .fdopen (pathspec_fd , "w" ) as f :
235+ f .write ("\n " .join (paths ))
231236
232237 # Mark untracked files as intent-to-add so git diff includes them
238+ extra_pathspec_path = None
233239 if extra_files :
234- extra_pathspec = Path ("render-check-extra-pathspec.txt" )
235- extra_pathspec .write_text ("\n " .join (extra_files ), encoding = "utf-8" )
240+ extra_fd , extra_pathspec_path = tempfile .mkstemp (
241+ prefix = "render-check-extra-" , suffix = ".txt"
242+ )
243+ with os .fdopen (extra_fd , "w" ) as f :
244+ f .write ("\n " .join (extra_files ))
236245 try :
237246 subprocess .run (
238- ["git" , "add" , "-N" , "--pathspec-from-file" , str ( extra_pathspec ) ],
247+ ["git" , "add" , "-N" , "--pathspec-from-file" , extra_pathspec_path ],
239248 check = True ,
240249 capture_output = True ,
241250 )
242251 except subprocess .CalledProcessError :
243252 pass
244- finally :
245- extra_pathspec .unlink (missing_ok = True )
246253
247254 try :
248255 result = subprocess .run (
249- ["git" , "diff" , "--pathspec-from-file" , str ( pathspec_file ) ],
256+ ["git" , "diff" , "--pathspec-from-file" , pathspec_path ],
250257 capture_output = True ,
251258 check = True ,
252259 )
253260 patch = result .stdout
254261 except subprocess .CalledProcessError :
255262 patch = b""
256263 finally :
257- pathspec_file .unlink (missing_ok = True )
264+ Path ( pathspec_path ) .unlink (missing_ok = True )
258265
259266 # Undo the intent-to-add so we don't leave index dirty
260- if extra_files :
261- extra_pathspec = Path ("render-check-extra-pathspec.txt" )
262- extra_pathspec .write_text ("\n " .join (extra_files ), encoding = "utf-8" )
267+ if extra_pathspec_path :
263268 try :
264269 subprocess .run (
265- ["git" , "reset" , "--pathspec-from-file" , str ( extra_pathspec ) ],
270+ ["git" , "reset" , "--pathspec-from-file" , extra_pathspec_path ],
266271 check = True ,
267272 capture_output = True ,
268273 )
269274 except subprocess .CalledProcessError :
270275 pass
271276 finally :
272- extra_pathspec .unlink (missing_ok = True )
277+ Path ( extra_pathspec_path ) .unlink (missing_ok = True )
273278
274279 return patch
275280
0 commit comments