Repository: envoyproxy/envoy
File: tools/code_format/check_format.py (Python)

Code before:
    def execute_command(self, command, error_message, file_path, regex=None):
        regex = regex or self.config.re["line_number"]
        try:
            output = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT).strip()
            if output:
                return output.decode('utf-8').split("\n")
            return []
        except subprocess.CalledProcessError as e:
            if (e.returncode != 0 and e.returncode != 1):
                return [
                    f"ERROR: something went wrong while executing: {e.cmd}\n{e.output.decode()}"
                ]
            # In case we can't find any line numbers, record an error message first.
            error_messages = [f"{error_message} for file: {file_path}\n{e.output.decode()}"]
            for line in e.output.decode('utf-8').splitlines():
                for num in regex.findall(line):
                    error_messages.append("  %s:%s" % (file_path, num))
            return error_messages


    def clang_format(self, file_path, check=False):
        result = []
        quoted_path = shlex.quote(file_path)
        command = (
            f"{self.config.clang_format_path} {quoted_path} | diff {quoted_path} -"
            if check else f"{self.config.clang_format_path} -i {quoted_path}")

        if check:
            result = self.execute_command(command, "clang-format check failed", file_path)
        else:
            ret = subprocess.run(
                [self.config.clang_format_path, "-i", file_path],
                capture_output=True)
            if ret.returncode != 0:
                result = [f"clang-format rewrite error: {file_path}"]

        return result

    def check_format(self, file_path, fail_on_diff=False):
        error_messages = []
        orig_error_messages = []
        # Apply fixes first, if asked, and then run checks. If we wind up attempting to fix
        # an issue, but there's still an error, that's a problem.
        check_build_path = (
            self.is_build_file(file_path) or self.is_starlark_file(file_path)
            or self.is_workspace_file(file_path))
        if check_build_path:
            if self.operation_type == "fix":
                orig_error_messages = self.check_build_path(file_path)
                if orig_error_messages:
                    error_messages.extend(

Review comment (suggestion):
same comment as on other pr - no need fror the conditional command setting as its not used for one branch now

Code after:
    #   - "7a8,9"
    def execute_command(self, command, error_message, file_path, regex=None):
        regex = regex or self.config.re["line_number"]
        try:
            output = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT).strip()
            if output:
                return output.decode('utf-8').split("\n")
            return []
        except subprocess.CalledProcessError as e:
            if (e.returncode != 0 and e.returncode != 1):
                return [
                    f"ERROR: something went wrong while executing: {e.cmd}\n{e.output.decode()}"
                ]
            # In case we can't find any line numbers, record an error message first.
            error_messages = [f"{error_message} for file: {file_path}\n{e.output.decode()}"]
            for line in e.output.decode('utf-8').splitlines():
                for num in regex.findall(line):
                    error_messages.append("  %s:%s" % (file_path, num))
            return error_messages

    def clang_format(self, file_path, check=False):
        result = []
        quoted_path = shlex.quote(file_path)
        if check:
            command = f"{self.config.clang_format_path} {quoted_path} | diff {quoted_path} -"
            result = self.execute_command(command, "clang-format check failed", file_path)
        else:
            ret = subprocess.run(
                [self.config.clang_format_path, "-i", file_path],
                capture_output=True,
                text=True
            )
            if ret.returncode != 0:
                result = [f"clang-format rewrite error: {file_path}"]
        return result

    def check_format(self, file_path, fail_on_diff=False):
        error_messages = []
        orig_error_messages = []
        # Apply fixes first, if asked, and then run checks. If we wind up attempting to fix
        # an issue, but there's still an error, that's a problem.
        check_build_path = (
            self.is_build_file(file_path) or self.is_starlark_file(file_path)
            or self.is_workspace_file(file_path))
        if check_build_path:
            if self.operation_type == "fix":
                orig_error_messages = self.check_build_path(file_path)
                if orig_error_messages:
                    error_messages.extend(
                        [*self.fix_build_path(file_path), *self.check_build_path(file_path)])
            else: