From 0214730c9afaf732b3571f3f63416fea9f98a65c Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Wed, 9 Apr 2025 19:59:05 -0400 Subject: [PATCH] launcher: switch command quoting to shlex.quote Minor fix, but just in case, provides properly quoted commands for people to copy & paste. Change-Id: Ia9fce5c0df9f51cbed9d49861adcf6821251e46f Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/466821 Tested-by: Mike Frysinger Commit-Queue: Mike Frysinger Reviewed-by: Gavin Mak --- release/util.py | 8 ++------ repo | 11 ++++++++--- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/release/util.py b/release/util.py index df7a5638..c839b872 100644 --- a/release/util.py +++ b/release/util.py @@ -16,6 +16,7 @@ import os import re +import shlex import subprocess import sys @@ -35,12 +36,7 @@ KEYID_ECC = "E1F9040D7A3F6DAFAC897CD3D3B95DA243E48A39" def cmdstr(cmd): """Get a nicely quoted shell command.""" - ret = [] - for arg in cmd: - if not re.match(r"^[a-zA-Z0-9/_.=-]+$", arg): - arg = f'"{arg}"' - ret.append(arg) - return " ".join(ret) + return " ".join(shlex.quote(x) for x in cmd) def run(opts, cmd, check=True, **kwargs): diff --git a/repo b/repo index 782afb6e..2526ab03 100755 --- a/repo +++ b/repo @@ -57,9 +57,14 @@ class Trace: trace = Trace() +def cmdstr(cmd): + """Get a nicely quoted shell command.""" + return " ".join(shlex.quote(x) for x in cmd) + + def exec_command(cmd): """Execute |cmd| or return None on failure.""" - trace.print(":", " ".join(cmd)) + trace.print(":", cmdstr(cmd)) try: if platform.system() == "Windows": ret = subprocess.call(cmd) @@ -506,7 +511,7 @@ def run_command(cmd, **kwargs): # Run & package the results. proc = subprocess.Popen(cmd, **kwargs) (stdout, stderr) = proc.communicate(input=cmd_input) - dbg = ": " + " ".join(cmd) + dbg = ": " + cmdstr(cmd) if cmd_input is not None: dbg += " 0<|" if stdout == subprocess.PIPE: @@ -843,7 +848,7 @@ def _GetRepoConfig(name): return None else: print( - f"repo: error: git {' '.join(cmd)} failed:\n{ret.stderr}", + f"repo: error: git {cmdstr(cmd)} failed:\n{ret.stderr}", file=sys.stderr, ) # This will raise subprocess.CalledProcessError for us.