You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by mo...@apache.org on 2021/11/24 02:41:11 UTC

[incubator-doris] branch master updated: [chore](clang-format)(license-eye) Add Clang Format/Skywalking eyes github action (#7132)

This is an automated email from the ASF dual-hosted git repository.

morningman pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-doris.git


The following commit(s) were added to refs/heads/master by this push:
     new e74bfea  [chore](clang-format)(license-eye) Add Clang Format/Skywalking eyes github action (#7132)
e74bfea is described below

commit e74bfea8e493a2085a40391eb6bef656728c6d43
Author: Mingyu Chen <mo...@gmail.com>
AuthorDate: Wed Nov 24 10:41:02 2021 +0800

    [chore](clang-format)(license-eye) Add Clang Format/Skywalking eyes github action (#7132)
    
    1. The clang format action will be triggered when a PR is submitted.
    2. Skywalking eyes actions will be triggered when a PR is submitted and after merging to master branch.
---
 .clang-format-ignore                               |   9 +
 .../workflows/clang-format.yml                     |  36 +-
 .../workflows/license-eyes.yml                     |  38 +-
 .licenserc.yaml                                    |   2 +
 LICENSE.txt                                        |  34 +-
 build-support/check-format.sh                      |   2 +-
 build-support/clang-format.sh                      |   2 +-
 build-support/lintutils.py                         | 111 -----
 build-support/run_clang_format.py                  | 534 ++++++++++++++++-----
 docs/en/developer-guide/cpp-format-code.md         |  78 +--
 docs/zh-CN/developer-guide/cpp-format-code.md      |  28 +-
 11 files changed, 554 insertions(+), 320 deletions(-)

diff --git a/.clang-format-ignore b/.clang-format-ignore
new file mode 100644
index 0000000..9275d0e
--- /dev/null
+++ b/.clang-format-ignore
@@ -0,0 +1,9 @@
+# Please add code that you don’t want to be formatted by clang here
+
+be/src/gutil/*
+be/src/glibc-compatibility/*
+be/src/util/sse2neo.h
+be/src/util/sse2neon.h
+be/src/util/mustache/mustache.h
+be/src/util/mustache/mustache.cc
+be/src/util/utf8_check.cpp
diff --git a/build-support/clang-format.sh b/.github/workflows/clang-format.yml
old mode 100755
new mode 100644
similarity index 58%
copy from build-support/clang-format.sh
copy to .github/workflows/clang-format.yml
index 8682b10..28dc5ae
--- a/build-support/clang-format.sh
+++ b/.github/workflows/clang-format.yml
@@ -1,4 +1,3 @@
-#!/usr/bin/env bash
 # Licensed to the Apache Software Foundation (ASF) under one
 # or more contributor license agreements.  See the NOTICE file
 # distributed with this work for additional information
@@ -15,21 +14,22 @@
 # KIND, either express or implied.  See the License for the
 # specific language governing permissions and limitations
 # under the License.
+#
 
-##############################################################
-# This script run the clang-format to check and fix
-# cplusplus source files.
-##############################################################
-
-set -eo pipefail
-
-ROOT=`dirname "$0"`
-ROOT=`cd "$ROOT"; pwd`
-
-export DORIS_HOME=`cd "${ROOT}/.."; pwd`
-
-CLANG_FORMAT=${CLANG_FORMAT_BINARY:=$(which clang-format)}
-
-python3 ${DORIS_HOME}/build-support/run_clang_format.py --clang_format_binary="${CLANG_FORMAT}" --fix --source_dirs="${DORIS_HOME}/be/src","${DORIS_HOME}/be/test"
-
-
+---
+name: BE Code Fomatter
+on: pull_request
+jobs:
+  clang-format:
+    name: "Clang Fomatter"
+    runs-on: ubuntu-latest
+    steps:
+      - name: "Checkout ${{ github.ref }} ( ${{ github.sha }} )"
+        uses: actions/checkout@v2
+      - name: "Format it!"
+        uses: DoozyX/clang-format-lint-action@v0.13
+        id: be_clang_format
+        with:
+          source: 'be/src be/test'
+          clangFormatVersion: 10
+          inplace: False
diff --git a/build-support/clang-format.sh b/.github/workflows/license-eyes.yml
old mode 100755
new mode 100644
similarity index 58%
copy from build-support/clang-format.sh
copy to .github/workflows/license-eyes.yml
index 8682b10..c9b4cd2
--- a/build-support/clang-format.sh
+++ b/.github/workflows/license-eyes.yml
@@ -1,4 +1,3 @@
-#!/usr/bin/env bash
 # Licensed to the Apache Software Foundation (ASF) under one
 # or more contributor license agreements.  See the NOTICE file
 # distributed with this work for additional information
@@ -15,21 +14,22 @@
 # KIND, either express or implied.  See the License for the
 # specific language governing permissions and limitations
 # under the License.
-
-##############################################################
-# This script run the clang-format to check and fix
-# cplusplus source files.
-##############################################################
-
-set -eo pipefail
-
-ROOT=`dirname "$0"`
-ROOT=`cd "$ROOT"; pwd`
-
-export DORIS_HOME=`cd "${ROOT}/.."; pwd`
-
-CLANG_FORMAT=${CLANG_FORMAT_BINARY:=$(which clang-format)}
-
-python3 ${DORIS_HOME}/build-support/run_clang_format.py --clang_format_binary="${CLANG_FORMAT}" --fix --source_dirs="${DORIS_HOME}/be/src","${DORIS_HOME}/be/test"
-
-
+#
+---
+name: License Check
+on:
+  pull_request:
+  push:
+    branches:
+      - master
+jobs:
+  license-check:
+    name: "License Check"
+    runs-on: ubuntu-latest
+    steps:
+      - name: "Checkout ${{ github.ref }} ( ${{ github.sha }} )"
+        uses: actions/checkout@v2
+      - name: Check License
+        uses: apache/skywalking-eyes@v0.2.0
+        env:
+          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
diff --git a/.licenserc.yaml b/.licenserc.yaml
index 7f65d6e..3529365 100644
--- a/.licenserc.yaml
+++ b/.licenserc.yaml
@@ -11,6 +11,7 @@ header:
     - 'NOTICE'
     - 'DISCLAIMER'
     - '.clang-format'
+    - '.clang-format-ignore'
     - '.gitattributes'
     - '.gitignore'
     - '.gitmodules'
@@ -48,5 +49,6 @@ header:
     - 'be/src/util/sse2neo.h'
     - 'be/src/util/sse2neon.h'
     - 'be/src/util/utf8_check.cpp'
+    - 'build-support/run_clang_format.py'
 
   comment: on-failure
diff --git a/LICENSE.txt b/LICENSE.txt
index a5aada2..81bc3fa 100644
--- a/LICENSE.txt
+++ b/LICENSE.txt
@@ -543,4 +543,36 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-SOFTWARE.
\ No newline at end of file
+SOFTWARE.
+
+--------------------------------------------------------------------------------
+
+build-support/run_clang_format.py: MIT license
+
+This script is copied from:
+https://github.com/DoozyX/clang-format-lint-action/blob/master/run-clang-format.py
+
+which is a wrapper script around clang-format, suitable for linting multiple files
+and to use for continuous integration.
+
+MIT License
+
+Copyright (c) 2019 Slobodan Kletnikov
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git a/build-support/check-format.sh b/build-support/check-format.sh
index 633c617..f247fa0 100755
--- a/build-support/check-format.sh
+++ b/build-support/check-format.sh
@@ -30,5 +30,5 @@ export DORIS_HOME=`cd "${ROOT}/.."; pwd`
 
 CLANG_FORMAT=${CLANG_FORMAT_BINARY:=$(which clang-format)}
 
-python3 ${DORIS_HOME}/build-support/run_clang_format.py --clang_format_binary="${CLANG_FORMAT}" --source_dirs="${DORIS_HOME}/be/src,${DORIS_HOME}/be/test" --quiet
+python3 ${DORIS_HOME}/build-support/run_clang_format.py "--clang-format-executable" "${CLANG_FORMAT}" "-r" "--style" "file" "--inplace" "false" "--extensions" "c,h,C,H,cpp,hpp,cc,hh,c++,h++,cxx,hxx" "--exclude" "none" "be/src be/test"
 
diff --git a/build-support/clang-format.sh b/build-support/clang-format.sh
index 8682b10..04464fc 100755
--- a/build-support/clang-format.sh
+++ b/build-support/clang-format.sh
@@ -30,6 +30,6 @@ export DORIS_HOME=`cd "${ROOT}/.."; pwd`
 
 CLANG_FORMAT=${CLANG_FORMAT_BINARY:=$(which clang-format)}
 
-python3 ${DORIS_HOME}/build-support/run_clang_format.py --clang_format_binary="${CLANG_FORMAT}" --fix --source_dirs="${DORIS_HOME}/be/src","${DORIS_HOME}/be/test"
+python3 ${DORIS_HOME}/build-support/run_clang_format.py "--clang-format-executable" "${CLANG_FORMAT}" "-r" "--style" "file" "--inplace" "true" "--extensions" "c,h,C,H,cpp,hpp,cc,hh,c++,h++,cxx,hxx" "--exclude" "none" "be/src be/test"
 
 
diff --git a/build-support/lintutils.py b/build-support/lintutils.py
deleted file mode 100644
index e651a3a..0000000
--- a/build-support/lintutils.py
+++ /dev/null
@@ -1,111 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements.  See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership.  The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License.  You may obtain a copy of the License at
-#
-#   http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing,
-# software distributed under the License is distributed on an
-# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-# KIND, either express or implied.  See the License for the
-# specific language governing permissions and limitations
-# under the License.
-#
-# Modified from Apache Arrow project.
-
-import multiprocessing as mp
-import os
-from fnmatch import fnmatch
-from subprocess import Popen
-
-
-def chunk(seq, n):
-    """
-    divide a sequence into equal sized chunks
-    (the last chunk may be smaller, but won't be empty)
-    """
-    chunks = []
-    some = []
-    for element in seq:
-        if len(some) == n:
-            chunks.append(some)
-            some = []
-        some.append(element)
-    if len(some) > 0:
-        chunks.append(some)
-    return chunks
-
-
-def dechunk(chunks):
-    "flatten chunks into a single list"
-    seq = []
-    for chunk in chunks:
-        seq.extend(chunk)
-    return seq
-
-
-def run_parallel(cmds, **kwargs):
-    """
-    Run each of cmds (with shared **kwargs) using subprocess.Popen
-    then wait for all of them to complete.
-    Runs batches of multiprocessing.cpu_count() * 2 from cmds
-    returns a list of tuples containing each process'
-    returncode, stdout, stderr
-    """
-    complete = []
-    for cmds_batch in chunk(cmds, mp.cpu_count() * 2):
-        procs_batch = [Popen(cmd, **kwargs) for cmd in cmds_batch]
-        for proc in procs_batch:
-            stdout, stderr = proc.communicate()
-            complete.append((proc.returncode, stdout, stderr))
-    return complete
-
-
-_source_extensions = '''
-.h
-.cc
-.cpp
-'''.split()
-
-
-def get_sources(source_dir, exclude_globs=[]):
-    sources = []
-    for directory, subdirs, basenames in os.walk(source_dir):
-        for path in [os.path.join(directory, basename)
-                     for basename in basenames]:
-            # filter out non-source files
-            if os.path.splitext(path)[1] not in _source_extensions:
-                continue
-
-            path = os.path.abspath(path)
-
-            # filter out files that match the globs in the globs file
-            if any([fnmatch(path, glob) for glob in exclude_globs]):
-               continue
-
-            sources.append(path)
-    return sources
-
-
-def stdout_pathcolonline(completed_process, filenames):
-    """
-    given a completed process which may have reported some files as problematic
-    by printing the path name followed by ':' then a line number, examine
-    stdout and return the set of actually reported file names
-    """
-    returncode, stdout, stderr = completed_process
-    bfilenames = set()
-    for filename in filenames:
-        bfilenames.add(filename.encode('utf-8') + b':')
-    problem_files = set()
-    for line in stdout.splitlines():
-        for filename in bfilenames:
-            if line.startswith(filename):
-                problem_files.add(filename.decode('utf-8'))
-                bfilenames.remove(filename)
-                break
-    return problem_files, stdout
diff --git a/build-support/run_clang_format.py b/build-support/run_clang_format.py
old mode 100644
new mode 100755
index f19c10c..6ce898c
--- a/build-support/run_clang_format.py
+++ b/build-support/run_clang_format.py
@@ -1,144 +1,412 @@
 #!/usr/bin/env python
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements.  See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership.  The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License.  You may obtain a copy of the License at
-#
-#   http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing,
-# software distributed under the License is distributed on an
-# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-# KIND, either express or implied.  See the License for the
-# specific language governing permissions and limitations
-# under the License.
-#
-# Modified from Apache Arrow project.
-
-from __future__ import print_function
-import lintutils
-from subprocess import PIPE
+"""A wrapper script around clang-format, suitable for linting multiple files
+and to use for continuous integration.
+This is an alternative API for the clang-format command line.
+It runs over multiple files and directories in parallel.
+A diff output is produced and a sensible exit code is returned.
+
+Copied from:
+https://github.com/DoozyX/clang-format-lint-action/blob/master/run-clang-format.py
+"""
+
+from __future__ import print_function, unicode_literals
+
 import argparse
+import codecs
 import difflib
-import multiprocessing as mp
+import errno
+import fnmatch
+import io
+import multiprocessing
+import os
+import signal
+import subprocess
 import sys
+import traceback
+from distutils.util import strtobool
+
 from functools import partial
 
+try:
+    from subprocess import DEVNULL  # py3k
+except ImportError:
+    DEVNULL = open(os.devnull, "wb")
+
+
+DEFAULT_EXTENSIONS = 'c,h,C,H,cpp,hpp,cc,hh,c++,h++,cxx,hxx'
+DEFAULT_CLANG_FORMAT_IGNORE = '.clang-format-ignore'
+
+
+class ExitStatus:
+    SUCCESS = 0
+    DIFF = 1
+    TROUBLE = 2
+
+
+def excludes_from_file(ignore_file):
+    excludes = []
+    try:
+        with io.open(ignore_file, 'r', encoding='utf-8') as f:
+            for line in f:
+                if line.startswith('#'):
+                    # ignore comments
+                    continue
+                pattern = line.rstrip()
+                if not pattern:
+                    # allow empty lines
+                    continue
+                excludes.append(pattern)
+    except EnvironmentError as e:
+        if e.errno != errno.ENOENT:
+            raise
+    return excludes
+
+
+def list_files(files, recursive=False, extensions=None, exclude=None):
+    if extensions is None:
+        extensions = []
+    if exclude is None:
+        exclude = []
+
+    out = []
+    for file in files:
+        if recursive and os.path.isdir(file):
+            for dirpath, dnames, fnames in os.walk(file):
+                fpaths = [os.path.join(dirpath, fname) for fname in fnames]
+                for pattern in exclude:
+                    # os.walk() supports trimming down the dnames list
+                    # by modifying it in-place,
+                    # to avoid unnecessary directory listings.
+                    dnames[:] = [
+                        x for x in dnames
+                        if
+                        not fnmatch.fnmatch(os.path.join(dirpath, x), pattern)
+                    ]
+                    fpaths = [
+                        x for x in fpaths if not fnmatch.fnmatch(x, pattern)
+                    ]
+                for f in fpaths:
+                    ext = os.path.splitext(f)[1][1:]
+                    if ext in extensions:
+                        out.append(f)
+        else:
+            out.append(file)
+    return out
+
+
+def make_diff(file, original, reformatted):
+    return list(
+        difflib.unified_diff(
+            original,
+            reformatted,
+            fromfile='{}\t(original)'.format(file),
+            tofile='{}\t(reformatted)'.format(file),
+            n=3))
+
+
+class DiffError(Exception):
+    def __init__(self, message, errs=None):
+        super(DiffError, self).__init__(message)
+        self.errs = errs or []
+
+
+class UnexpectedError(Exception):
+    def __init__(self, message, exc=None):
+        super(UnexpectedError, self).__init__(message)
+        self.formatted_traceback = traceback.format_exc()
+        self.exc = exc
+
+
+def run_clang_format_diff_wrapper(args, file):
+    try:
+        ret = run_clang_format_diff(args, file)
+        return ret
+    except DiffError:
+        raise
+    except Exception as e:
+        raise UnexpectedError('{}: {}: {}'.format(file, e.__class__.__name__,
+                                                  e), e)
+
+
+def run_clang_format_diff(args, file):
+    try:
+        with io.open(file, 'r', encoding='utf-8') as f:
+            original = f.readlines()
+    except IOError as exc:
+        raise DiffError(str(exc))
+    invocation = [args.clang_format_executable, file]
+    if args.style:
+        invocation.append('-style=' + args.style)
+    if args.inplace:
+        invocation.append('-i')
+
+    # Use of utf-8 to decode the process output.
+    #
+    # Hopefully, this is the correct thing to do.
+    #
+    # It's done due to the following assumptions (which may be incorrect):
+    # - clang-format will returns the bytes read from the files as-is,
+    #   without conversion, and it is already assumed that the files use utf-8.
+    # - if the diagnostics were internationalized, they would use utf-8:
+    #   > Adding Translations to Clang
+    #   >
+    #   > Not possible yet!
+    #   > Diagnostic strings should be written in UTF-8,
+    #   > the client can translate to the relevant code page if needed.
+    #   > Each translation completely replaces the format string
+    #   > for the diagnostic.
+    #   > -- http://clang.llvm.org/docs/InternalsManual.html#internals-diag-translation
+    #
+    # It's not pretty, due to Python 2 & 3 compatibility.
+    encoding_py3 = {}
+    if sys.version_info[0] >= 3:
+        encoding_py3['encoding'] = 'utf-8'
+
+    try:
+        proc = subprocess.Popen(
+            invocation,
+            stdout=subprocess.PIPE,
+            stderr=subprocess.PIPE,
+            universal_newlines=True,
+            **encoding_py3)
+    except OSError as exc:
+        raise DiffError(
+            "Command '{}' failed to start: {}".format(
+                subprocess.list2cmdline(invocation), exc
+            )
+        )
+    proc_stdout = proc.stdout
+    proc_stderr = proc.stderr
+    if sys.version_info[0] < 3:
+        # make the pipes compatible with Python 3,
+        # reading lines should output unicode
+        encoding = 'utf-8'
+        proc_stdout = codecs.getreader(encoding)(proc_stdout)
+        proc_stderr = codecs.getreader(encoding)(proc_stderr)
+    # hopefully the stderr pipe won't get full and block the process
+    outs = list(proc_stdout.readlines())
+    errs = list(proc_stderr.readlines())
+    proc.wait()
+    if proc.returncode:
+        raise DiffError(
+            "Command '{}' returned non-zero exit status {}".format(
+                subprocess.list2cmdline(invocation), proc.returncode
+            ),
+            errs,
+        )
+    return make_diff(file, original, outs), errs
+
+
+def bold_red(s):
+    return '\x1b[1m\x1b[31m' + s + '\x1b[0m'
 
-# examine the output of clang-format and if changes are
-# present assemble a (unified)patch of the difference
-def _check_one_file(filename, formatted):
-    with open(filename, "rb") as reader:
-        original = reader.read()
-
-    if formatted != original:
-        # Run the equivalent of diff -u
-        diff = list(difflib.unified_diff(
-            original.decode('utf8').splitlines(True),
-            formatted.decode('utf8').splitlines(True),
-            fromfile=filename,
-            tofile="{} (after clang format)".format(
-                filename)))
+
+def colorize(diff_lines):
+    def bold(s):
+        return '\x1b[1m' + s + '\x1b[0m'
+
+    def cyan(s):
+        return '\x1b[36m' + s + '\x1b[0m'
+
+    def green(s):
+        return '\x1b[32m' + s + '\x1b[0m'
+
+    def red(s):
+        return '\x1b[31m' + s + '\x1b[0m'
+
+    for line in diff_lines:
+        if line[:4] in ['--- ', '+++ ']:
+            yield bold(line)
+        elif line.startswith('@@ '):
+            yield cyan(line)
+        elif line.startswith('+'):
+            yield green(line)
+        elif line.startswith('-'):
+            yield red(line)
+        else:
+            yield line
+
+
+def print_diff(diff_lines, use_color):
+    if use_color:
+        diff_lines = colorize(diff_lines)
+    if sys.version_info[0] < 3:
+        sys.stdout.writelines((l.encode('utf-8') for l in diff_lines))
+    else:
+        sys.stdout.writelines(diff_lines)
+
+
+def print_trouble(prog, message, use_colors):
+    error_text = 'error:'
+    if use_colors:
+        error_text = bold_red(error_text)
+    print("{}: {} {}".format(prog, error_text, message), file=sys.stderr)
+
+
+def split_list_arg(arg):
+    """
+    If arg is a list containing a single argument it is split into multiple elements.
+    Otherwise it is returned unchanged
+    Workaround for GHA not allowing list arguments
+    """
+    return arg[0].split() if len(arg) == 1 else arg
+
+
+def main():
+    parser = argparse.ArgumentParser(description=__doc__)
+    parser.add_argument(
+        '--clang-format-executable',
+        metavar='EXECUTABLE',
+        help='path to the clang-format executable',
+        default='clang-format')
+    parser.add_argument(
+        '--extensions',
+        help='comma separated list of file extensions (default: {})'.format(
+            DEFAULT_EXTENSIONS),
+        default=DEFAULT_EXTENSIONS)
+    parser.add_argument(
+        '-r',
+        '--recursive',
+        action='store_true',
+        help='run recursively over directories')
+    parser.add_argument('files', metavar='file', nargs='+')
+    parser.add_argument(
+        '-q',
+        '--quiet',
+        action='store_true',
+        help="disable output, useful for the exit code")
+    parser.add_argument(
+        '-j',
+        metavar='N',
+        type=int,
+        default=0,
+        help='run N clang-format jobs in parallel'
+        ' (default number of cpus + 1)')
+    parser.add_argument(
+        '--color',
+        default='auto',
+        choices=['auto', 'always', 'never'],
+        help='show colored diff (default: auto)')
+    parser.add_argument(
+        '-e',
+        '--exclude',
+        metavar='PATTERN',
+        action='append',
+        default=[],
+        help='exclude paths matching the given glob-like pattern(s)'
+        ' from recursive search')
+    parser.add_argument(
+        '--style',
+        help='Formatting style to use (default: file)',
+        default='file')
+    parser.add_argument(
+        '-i',
+        '--inplace',
+        type=lambda x: bool(strtobool(x)),
+        default=False,
+        help='Just fix files (`clang-format -i`) instead of returning a diff')
+
+    args = parser.parse_args()
+
+    # use default signal handling, like diff return SIGINT value on ^C
+    # https://bugs.python.org/issue14229#msg156446
+    signal.signal(signal.SIGINT, signal.SIG_DFL)
+    try:
+        signal.SIGPIPE
+    except AttributeError:
+        # compatibility, SIGPIPE does not exist on Windows
+        pass
     else:
-        diff = None
-
-    return filename, diff
-
-def _check_dir(arguments, source_dir, exclude_globs):
-    formatted_filenames = []
-    for path in lintutils.get_sources(source_dir, exclude_globs):
-            formatted_filenames.append(str(path))
-
-    if arguments.fix:
-        if not arguments.quiet:
-            print("\n".join(map(lambda x: "Formatting {}".format(x),
-                                formatted_filenames)))
-
-        # Break clang-format invocations into chunks: each invocation formats
-        # 16 files. Wait for all processes to complete
-        results = lintutils.run_parallel([
-            [arguments.clang_format_binary, "-style=file", "-i"] + some
-            for some in lintutils.chunk(formatted_filenames, 16)
-        ])
-        for returncode, stdout, stderr in results:
-            # if any clang-format reported a parse error, bubble it
-            if returncode != 0:
-                sys.exit(returncode)
+        signal.signal(signal.SIGPIPE, signal.SIG_DFL)
+
+    colored_stdout = False
+    colored_stderr = False
+    if args.color == 'always':
+        colored_stdout = True
+        colored_stderr = True
+    elif args.color == 'auto':
+        colored_stdout = sys.stdout.isatty()
+        colored_stderr = sys.stderr.isatty()
+
+    version_invocation = [args.clang_format_executable, str("--version")]
+    try:
+        subprocess.check_call(version_invocation, stdout=DEVNULL)
+    except subprocess.CalledProcessError as e:
+        print_trouble(parser.prog, str(e), use_colors=colored_stderr)
+        return ExitStatus.TROUBLE
+    except OSError as e:
+        print_trouble(
+            parser.prog,
+            "Command '{}' failed to start: {}".format(
+                subprocess.list2cmdline(version_invocation), e
+            ),
+            use_colors=colored_stderr,
+        )
+        return ExitStatus.TROUBLE
+
+    retcode = ExitStatus.SUCCESS
+
+    excludes = excludes_from_file(DEFAULT_CLANG_FORMAT_IGNORE)
+    excludes.extend(split_list_arg(args.exclude))
+
+    files = list_files(
+        split_list_arg(args.files),
+        recursive=args.recursive,
+        exclude=excludes,
+        extensions=args.extensions.split(','))
 
+    if not files:
+        print_trouble(parser.prog, 'No files found', use_colors=colored_stderr)
+        return ExitStatus.TROUBLE
+
+    if not args.quiet:
+      print('Processing %s files: %s' % (len(files), ', '.join(files)))
+
+    njobs = args.j
+    if njobs == 0:
+        njobs = multiprocessing.cpu_count() + 1
+    njobs = min(len(files), njobs)
+
+    if njobs == 1:
+        # execute directly instead of in a pool,
+        # less overhead, simpler stacktraces
+        it = (run_clang_format_diff_wrapper(args, file) for file in files)
+        pool = None
     else:
-        # run an instance of clang-format for each source file in parallel,
-        # then wait for all processes to complete
-        results = lintutils.run_parallel([
-            [arguments.clang_format_binary, "-style=file", filename]
-            for filename in formatted_filenames
-        ], stdout=PIPE, stderr=PIPE)
-
-        checker_args = []
-        for filename, res in zip(formatted_filenames, results):
-            # if any clang-format reported a parse error, bubble it
-            returncode, stdout, stderr = res
-            if returncode != 0:
-                print(stderr)
-                sys.exit(returncode)
-            checker_args.append((filename, stdout))
-
-        error = False
-        pool = mp.Pool()
+        pool = multiprocessing.Pool(njobs)
+        it = pool.imap_unordered(
+            partial(run_clang_format_diff_wrapper, args), files)
+    while True:
         try:
-            # check the output from each invocation of clang-format in parallel
-            for filename, diff in pool.starmap(_check_one_file, checker_args):
-                if not arguments.quiet:
-                    print("Checking {}".format(filename))
-                if diff:
-                    print("{} had clang-format style issues".format(filename))
-                    # Print out the diff to stderr
-                    error = True
-                    # pad with a newline
-                    print(file=sys.stderr)
-                    sys.stderr.writelines(diff)
-        except Exception:
-            error = True
-            raise
-        finally:
-            pool.terminate()
-            pool.join()
-        sys.exit(1 if error else 0)
-
-
-if __name__ == "__main__":
-    parser = argparse.ArgumentParser(
-        description="Runs clang-format on all of the source "
-        "files. If --fix is specified enforce format by "
-        "modifying in place, otherwise compare the output "
-        "with the existing file and output any necessary "
-        "changes as a patch in unified diff format")
-    parser.add_argument("--clang_format_binary",
-                        required=True,
-                        help="Path to the clang-format binary")
-    parser.add_argument("--exclude_globs",
-                        help="Filename containing globs for files "
-                        "that should be excluded from the checks")
-    parser.add_argument("--source_dirs",
-                        required=True,
-                        help="Comma-separated root directories of the source code")
-    parser.add_argument("--fix", default=False,
-                        action="store_true",
-                        help="If specified, will re-format the source "
-                        "code instead of comparing the re-formatted "
-                        "output, defaults to %(default)s")
-    parser.add_argument("--quiet", default=False,
-                        action="store_true",
-                        help="If specified, only print errors")
-    arguments = parser.parse_args()
-
-    exclude_globs = []
-    if arguments.exclude_globs:
-        with open(arguments.exclude_globs) as f:
-            exclude_globs.extend(line.strip() for line in f)
-
-    for source_dir in arguments.source_dirs.split(','):
-        if len(source_dir) > 0:
-            _check_dir(arguments, source_dir, exclude_globs)
+            outs, errs = next(it)
+        except StopIteration:
+            break
+        except DiffError as e:
+            print_trouble(parser.prog, str(e), use_colors=colored_stderr)
+            retcode = ExitStatus.TROUBLE
+            sys.stderr.writelines(e.errs)
+        except UnexpectedError as e:
+            print_trouble(parser.prog, str(e), use_colors=colored_stderr)
+            sys.stderr.write(e.formatted_traceback)
+            retcode = ExitStatus.TROUBLE
+            # stop at the first unexpected error,
+            # something could be very wrong,
+            # don't process all files unnecessarily
+            if pool:
+                pool.terminate()
+            break
+        else:
+            sys.stderr.writelines(errs)
+            if outs == []:
+                continue
+            if not args.inplace:
+                if not args.quiet:
+                    print_diff(outs, use_color=colored_stdout)
+                if retcode == ExitStatus.SUCCESS:
+                    retcode = ExitStatus.DIFF
+
+    return retcode
+
+
+if __name__ == '__main__':
+    sys.exit(main())
diff --git a/docs/en/developer-guide/cpp-format-code.md b/docs/en/developer-guide/cpp-format-code.md
index 7f9e075..27340be 100644
--- a/docs/en/developer-guide/cpp-format-code.md
+++ b/docs/en/developer-guide/cpp-format-code.md
@@ -24,58 +24,72 @@ specific language governing permissions and limitations
 under the License.
 -->
 
-# C++ Format Code
-Doris use `Clang-format` to automatically check the format of your source code.
+# C++ code formatting
 
-## Code Style
-Doris Code Style is based on Google's, makes a few changes. The customized .clang-format
-file is in the root dir of Doris.
-Now, .clang-format file only works on clang-format-8.0.1+.
+Doris uses clang-format for code formatting, and provides a package script in the build-support directory:
 
-## Preparing
-You should install clang-format, or you can use clang-format plugins which support by IDEs or Editors.
+* `clang-format.sh`.
 
-### Install clang-format
-Ubuntu: `apt-get install clang-format` 
+    Format the C/C++ code in the `be/src` and `be/test` directories.
+
+* `check-format.sh`.
+
+    Check the C/C++ code format in the `be/src` and `be/test` directories, and output diff, but the content of the file will not be modified.
+
+## Code style customization
+
+The code style of Doris is slightly modified on the basis of Google Style and is customized as a `.clang-format` file located in the root directory of Doris.
+
+Currently, the `.clang-format` configuration file is adapted to versions above clang-format-8.0.1.
+
+The code that you do not want to be formatted is recorded in the `.clang-format-ignore` file. These codes usually come from third-party code bases, and it is recommended to keep the original code style.
+
+## Environmental preparation
+
+You need to download and install clang-format, or you can use the clang-format plug-in provided by IDE or Editor, as described below.
+
+### Download and install clang-format
+
+Ubuntu: `apt-get install clang-format`
+
+The current version is 10.0, you can also specify the old version, for example: `apt-get install clang-format-9`
 
 Mac: `brew install clang-format`
 
-The current release is 10.0, you can specify old version, e.g.
- 
- `apt-get install clang-format-9`
+Centos 7:
+
+The version of clang-format installed by centos yum is too old and supports too few StyleOptions. It is recommended to compile version 10.0 from source code.
+
+### clang-format plugin
 
-Centos 7: 
+Clion IDE can use the plug-in "ClangFormat", search and download in `File->Setting->Plugins`. But the version can’t match
+The version of the clang-format program matches, judging from the supported StyleOption, it should be lower than clang-format-9.0.
 
-The version of clang-format installed by yum is too old. Compiling clang from source
-is recommended.
+## How to use
 
-### Clang-format plugins
-Clion IDE supports the plugin "ClangFormat", you can search in `File->Setting->Plugins`
- and download it.
-But the version is not match with clang-format. Judging from the options supported, 
-the version is lower than clang-format-9.0.
+### Command line operation
 
-## Usage
+cd to the root directory of Doris, and then execute the following command:
 
-### CMD
-Change directory to the root directory of Doris sources and run the following command:
 `build-support/clang-format.sh`
 
-NOTE: Python3 is required to run the `clang-format.sh` script.
+> Note: The `clang-format.sh` script requires python 3 to be installed on your machine
+
+### Use clang-format in IDE or Editor
 
-### Using clang-format in IDEs or Editors
 #### Clion
-If using the plugin 'ClangFormat' in Clion, choose `Reformat Code` or press the keyboard 
-shortcut.
+
+If Clion uses a plug-in, just click `Reformat Code`.
 
 #### VS Code
-VS Code needs install the extension 'Clang-Format', and specify the executable path of 
-clang-format in settings.
 
-Open the vs code configuration page and search `clang_format`, fill the box as follows.
+VS Code needs to install the extension Clang-Format, but you need to provide the location of the clang-format execution program.
+
+Open the VS Code configuration page, directly search for "clang_format", and fill in
 
 ```
 "clang_format_path": "$clang-format path$",
 "clang_format_style": "file"
 ```
-Then, right click the file and choose `Format Document`.
+
+Then, right-click on `Format Document`.
diff --git a/docs/zh-CN/developer-guide/cpp-format-code.md b/docs/zh-CN/developer-guide/cpp-format-code.md
index 92e6309..07c817c 100644
--- a/docs/zh-CN/developer-guide/cpp-format-code.md
+++ b/docs/zh-CN/developer-guide/cpp-format-code.md
@@ -26,17 +26,30 @@ under the License.
 
 # C++ 代码格式化
 
-Doris使用clang-format进行代码格式化,并在build-support目录下提供了封装脚本`clang-format.sh`.
+Doris使用clang-format进行代码格式化,并在build-support目录下提供了封装脚本:
+
+* `clang-format.sh`.
+
+    格式化 `be/src` 和 `be/test` 目录下的 C/C++ 代码。
+
+* `check-format.sh`.
+
+    检查 `be/src` 和 `be/test` 目录下的 C/C++ 代码格式,并将 diff 输出,但不会修改文件内容。
 
 ## 代码风格定制
-Doris的代码风格在Google Style的基础上稍有改动,定制为.clang-format文件,位于Doris根目录。
 
-目前,.clang-format配置文件适配clang-format-8.0.1以上的版本。
+Doris的代码风格在Google Style的基础上稍有改动,定制为 `.clang-format` 文件,位于Doris根目录。
+
+目前,`.clang-format` 配置文件适配clang-format-8.0.1以上的版本。
+
+`.clang-format-ignore` 文件中记录了不希望被格式化的代码。这些代码通常来自第三方代码库,建议保持原有代码风格。
 
 ## 环境准备
+
 需要下载安装clang-format,也可使用IDE或Editor提供的clang-format插件,下面分别介绍。
 
 ### 下载安装clang-format
+
 Ubuntu: `apt-get install clang-format` 
 
 当前版本为10.0,也可指定旧版本,例如: `apt-get install clang-format-9`
@@ -48,21 +61,28 @@ Centos 7:
 centos yum安装的clang-format版本过老,支持的StyleOption太少,建议源码编译10.0版本。
 
 ### clang-format插件
+
 Clion IDE可使用插件"ClangFormat",`File->Setting->Plugins`搜索下载。但版本无法和
 clang-format程序的版本匹配,从支持的StyleOption上看,应该是低于clang-format-9.0。
 
 ## 使用方式
 
 ### 命令行运行
+
 cd到Doris根目录下,然后执行如下命令:
+
 `build-support/clang-format.sh`
 
-注:`clang-format.sh`脚本要求您的机器上安装了python 3
+> 注:`clang-format.sh`脚本要求您的机器上安装了python 3
 
 ### 在IDE或Editor中使用clang-format
+
 #### Clion
+
 Clion如果使用插件,点击`Reformat Code`即可。
+
 #### VS Code
+
 VS Code需安装扩展程序Clang-Format,但需要自行提供clang-format执行程序的位置。
 
 打开VS Code配置页面,直接搜索"clang_format",填上

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org