Author: Owen Pan Date: 2024-12-05T23:18:24-08:00 New Revision: e25c556abeb9ae5f82da42cd26b9dae8462a7197
URL: https://github.com/llvm/llvm-project/commit/e25c556abeb9ae5f82da42cd26b9dae8462a7197 DIFF: https://github.com/llvm/llvm-project/commit/e25c556abeb9ae5f82da42cd26b9dae8462a7197.diff LOG: [clang-format][NFC] Reformat git-clang-format with `black -l80` Added: Modified: clang/tools/clang-format/git-clang-format Removed: ################################################################################ diff --git a/clang/tools/clang-format/git-clang-format b/clang/tools/clang-format/git-clang-format index cdfdccfa64a197..da271bbe6e3a07 100755 --- a/clang/tools/clang-format/git-clang-format +++ b/clang/tools/clang-format/git-clang-format @@ -1,12 +1,12 @@ #!/usr/bin/env python3 # -# ===- git-clang-format - ClangFormat Git Integration ---------*- python -*--===# +# ===- git-clang-format - ClangFormat Git Integration -------*- python -*--=== # # # Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. # See https://llvm.org/LICENSE.txt for license information. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception # -# ===------------------------------------------------------------------------===# +# ===----------------------------------------------------------------------=== # r""" clang-format git integration @@ -32,7 +32,9 @@ import re import subprocess import sys -usage = "git clang-format [OPTIONS] [<commit>] [<commit>|--staged] " "[--] [<file>...]" +usage = ( + "git clang-format [OPTIONS] [<commit>] [<commit>|--staged] [--] [<file>...]" +) desc = """ If zero or one commits are given, run clang-format on all lines that diff er @@ -161,13 +163,20 @@ def main(): ), ), p.add_argument( - "-f", "--force", action="store_true", help="allow changes to unstaged files" + "-f", + "--force", + action="store_true", + help="allow changes to unstaged files", ) p.add_argument( "-p", "--patch", action="store_true", help="select hunks interactively" ) p.add_argument( - "-q", "--quiet", action="count", default=0, help="print less information" + "-q", + "--quiet", + action="count", + default=0, + help="print less information", ) p.add_argument( "--staged", @@ -181,7 +190,11 @@ def main(): help="passed to clang-format", ), p.add_argument( - "-v", "--verbose", action="count", default=0, help="print extra information" + "-v", + "--verbose", + action="count", + default=0, + help="print extra information", ) p.add_argument( "-- diff _from_common_commit", @@ -221,7 +234,10 @@ def main(): if not opts. diff : die("-- diff is required when two commits are given") elif opts. diff _from_common_commit: - die("-- diff _from_common_commit is only allowed when two commits are given") + die( + "-- diff _from_common_commit is only allowed when two commits are " + "given" + ) if os.path.dirname(opts.binary): opts.binary = os.path.abspath(opts.binary) @@ -296,9 +312,9 @@ def main(): def load_git_config(non_string_options=None): """Return the git configuration as a dictionary. - All options are assumed to be strings unless in `non_string_options`, in which - is a dictionary mapping option name (in lower case) to either "--bool" or - "--int".""" + All options are assumed to be strings unless in `non_string_options`, in + which is a dictionary mapping option name (in lower case) to either "--bool" + or "--int".""" if non_string_options is None: non_string_options = {} out = {} @@ -323,9 +339,9 @@ def interpret_args(args, dash_dash, default_commit): args and placed in `dash_dash`. If "--" is present (i.e., `dash_dash` is non-empty), the arguments to its - left (if present) are taken as commits. Otherwise, the arguments are checked - from left to right if they are commits or files. If commits are not given, - a list with `default_commit` is used.""" + left (if present) are taken as commits. Otherwise, the arguments are + checked from left to right if they are commits or files. If commits are not + given, a list with `default_commit` is used.""" if dash_dash: if len(args) == 0: commits = [default_commit] @@ -367,7 +383,10 @@ def disambiguate_revision(value): return False if object_type in ("commit", "tag"): return True - die("`%s` is a %s, but a commit or filename was expected" % (value, object_type)) + die( + "`%s` is a %s, but a commit or filename was expected" + % (value, object_type) + ) def get_object_type(value): @@ -442,7 +461,9 @@ def extract_lines(patch_file): line_count = 1 if start_line == 0: continue - matches.setdefault(filename, []).append(Range(start_line, line_count)) + matches.setdefault(filename, []).append( + Range(start_line, line_count) + ) return matches @@ -497,9 +518,19 @@ def create_tree_from_index(filenames): def index_contents_generator(): for filename in filenames: - git_ls_files_cmd = ["git", "ls-files", "--stage", "-z", "--", filename] + git_ls_files_cmd = [ + "git", + "ls-files", + "--stage", + "-z", + "--", + filename, + ] git_ls_files = subprocess.Popen( - git_ls_files_cmd, env=env, stdin=subprocess.PIPE, stdout=subprocess.PIPE + git_ls_files_cmd, + env=env, + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, ) stdout = git_ls_files.communicate()[0] yield convert_string(stdout.split(b"\0")[0]) @@ -534,7 +565,13 @@ def run_clang_format_and_save_to_tree( os.path.basename(filename), ] else: - git_metadata_cmd = ["git", "ls-files", "--stage", "--", filename] + git_metadata_cmd = [ + "git", + "ls-files", + "--stage", + "--", + filename, + ] git_metadata = subprocess.Popen( git_metadata_cmd, env=env, @@ -566,8 +603,8 @@ def create_tree(input_lines, mode): If mode is '--stdin', it must be a list of filenames. If mode is '--index-info' is must be a list of values suitable for "git update-index - --index-info", such as "<mode> <SP> <sha1> <TAB> <filename>". Any other mode - is invalid.""" + --index-info", such as "<mode> <SP> <sha1> <TAB> <filename>". Any other + mode is invalid.""" assert mode in ("--stdin", "--index-info") cmd = ["git", "update-index", "--add", "-z", mode] with temporary_index_file(): @@ -582,13 +619,18 @@ def create_tree(input_lines, mode): def clang_format_to_blob( - filename, line_ranges, revision=None, binary="clang-format", style=None, env=None + filename, + line_ranges, + revision=None, + binary="clang-format", + style=None, + env=None, ): """Run clang-format on the given file and save the result to a git blob. Runs on the file in `revision` if not None, or on the file in the working - directory if `revision` is None. Revision can be set to an empty string to run - clang-format on the file in the index. + directory if `revision` is None. Revision can be set to an empty string to + run clang-format on the file in the index. Returns the object ID (SHA-1) of the created blob.""" clang_format_cmd = [binary] @@ -602,7 +644,12 @@ def clang_format_to_blob( ) if revision is not None: clang_format_cmd.extend(["--assume-filename=" + filename]) - git_show_cmd = ["git", "cat-file", "blob", "%s:%s" % (revision, filename)] + git_show_cmd = [ + "git", + "cat-file", + "blob", + "%s:%s" % (revision, filename), + ] git_show = subprocess.Popen( git_show_cmd, env=env, stdin=subprocess.PIPE, stdout=subprocess.PIPE ) @@ -624,7 +671,13 @@ def clang_format_to_blob( else: raise clang_format_stdin.close() - hash_object_cmd = ["git", "hash-object", "-w", "--path=" + filename, "--stdin"] + hash_object_cmd = [ + "git", + "hash-object", + "-w", + "--path=" + filename, + "--stdin", + ] hash_object = subprocess.Popen( hash_object_cmd, stdin=clang_format.stdout, stdout=subprocess.PIPE ) @@ -641,8 +694,8 @@ def clang_format_to_blob( @contextlib.contextmanager def temporary_index_file(tree=None): - """Context manager for setting GIT_INDEX_FILE to a temporary file and deleting - the file afterward.""" + """Context manager for setting GIT_INDEX_FILE to a temporary file and + deleting the file afterward.""" index_path = create_temporary_index(tree) old_index_path = os.environ.get("GIT_INDEX_FILE") os.environ["GIT_INDEX_FILE"] = index_path @@ -671,9 +724,9 @@ def create_temporary_index(tree=None): def print_ diff (old_tree, new_tree): """Print the diff between the two trees to stdout.""" - # We use the porcelain ' diff ' and not plumbing ' diff -tree' because the output - # is expected to be viewed by the user, and only the former does nice things - # like color and pagination. + # We use the porcelain ' diff ' and not plumbing ' diff -tree' because the + # output is expected to be viewed by the user, and only the former does nice + # things like color and pagination. # # We also only print modified files since `new_tree` only contains the files # that were modified, so unmodified files would show as deleted without the @@ -685,15 +738,23 @@ def print_ diff (old_tree, new_tree): def print_ diff stat(old_tree, new_tree): """Print the diff stat between the two trees to stdout.""" - # We use the porcelain ' diff ' and not plumbing ' diff -tree' because the output - # is expected to be viewed by the user, and only the former does nice things - # like color and pagination. + # We use the porcelain ' diff ' and not plumbing ' diff -tree' because the + # output is expected to be viewed by the user, and only the former does nice + # things like color and pagination. # # We also only print modified files since `new_tree` only contains the files # that were modified, so unmodified files would show as deleted without the # filter. return subprocess.run( - ["git", " diff ", "-- diff -filter=M", "--exit-code", "--stat", old_tree, new_tree] + [ + "git", + " diff ", + "-- diff -filter=M", + "--exit-code", + "--stat", + old_tree, + new_tree, + ] ).returncode @@ -717,10 +778,13 @@ def apply_changes(old_tree, new_tree, force=False, patch_mode=False): .split("\0") ) if not force: - unstaged_files = run("git", " diff -files", "--name-status", *changed_files) + unstaged_files = run( + "git", " diff -files", "--name-status", *changed_files + ) if unstaged_files: print( - "The following files would be modified but " "have unstaged changes:", + "The following files would be modified but have unstaged " + "changes:", file=sys.stderr, ) print(unstaged_files, file=sys.stderr) @@ -749,7 +813,10 @@ def run(*args, **kwargs): for name in kwargs: raise TypeError("run() got an unexpected keyword argument '%s'" % name) p = subprocess.Popen( - args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE + args, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + stdin=subprocess.PIPE, ) stdout, stderr = p.communicate(input=stdin) @@ -759,13 +826,17 @@ def run(*args, **kwargs): if p.returncode == 0: if stderr: if verbose: - print("`%s` printed to stderr:" % " ".join(args), file=sys.stderr) + print( + "`%s` printed to stderr:" % " ".join(args), file=sys.stderr + ) print(stderr.rstrip(), file=sys.stderr) if strip: stdout = stdout.rstrip("\r\n") return stdout if verbose: - print("`%s` returned %s" % (" ".join(args), p.returncode), file=sys.stderr) + print( + "`%s` returned %s" % (" ".join(args), p.returncode), file=sys.stderr + ) if stderr: print(stderr.rstrip(), file=sys.stderr) sys.exit(2) _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits