Skip to content

Commit e25c556

Browse files
committed
[clang-format][NFC] Reformat git-clang-format with black -l80
1 parent dca2ed3 commit e25c556

File tree

1 file changed

+110
-39
lines changed

1 file changed

+110
-39
lines changed

clang/tools/clang-format/git-clang-format

Lines changed: 110 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#!/usr/bin/env python3
22
#
3-
# ===- git-clang-format - ClangFormat Git Integration ---------*- python -*--===#
3+
# ===- git-clang-format - ClangFormat Git Integration -------*- python -*--=== #
44
#
55
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
66
# See https://llvm.org/LICENSE.txt for license information.
77
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
88
#
9-
# ===------------------------------------------------------------------------===#
9+
# ===----------------------------------------------------------------------=== #
1010

1111
r"""
1212
clang-format git integration
@@ -32,7 +32,9 @@ import re
3232
import subprocess
3333
import sys
3434

35-
usage = "git clang-format [OPTIONS] [<commit>] [<commit>|--staged] " "[--] [<file>...]"
35+
usage = (
36+
"git clang-format [OPTIONS] [<commit>] [<commit>|--staged] [--] [<file>...]"
37+
)
3638

3739
desc = """
3840
If zero or one commits are given, run clang-format on all lines that differ
@@ -161,13 +163,20 @@ def main():
161163
),
162164
),
163165
p.add_argument(
164-
"-f", "--force", action="store_true", help="allow changes to unstaged files"
166+
"-f",
167+
"--force",
168+
action="store_true",
169+
help="allow changes to unstaged files",
165170
)
166171
p.add_argument(
167172
"-p", "--patch", action="store_true", help="select hunks interactively"
168173
)
169174
p.add_argument(
170-
"-q", "--quiet", action="count", default=0, help="print less information"
175+
"-q",
176+
"--quiet",
177+
action="count",
178+
default=0,
179+
help="print less information",
171180
)
172181
p.add_argument(
173182
"--staged",
@@ -181,7 +190,11 @@ def main():
181190
help="passed to clang-format",
182191
),
183192
p.add_argument(
184-
"-v", "--verbose", action="count", default=0, help="print extra information"
193+
"-v",
194+
"--verbose",
195+
action="count",
196+
default=0,
197+
help="print extra information",
185198
)
186199
p.add_argument(
187200
"--diff_from_common_commit",
@@ -221,7 +234,10 @@ def main():
221234
if not opts.diff:
222235
die("--diff is required when two commits are given")
223236
elif opts.diff_from_common_commit:
224-
die("--diff_from_common_commit is only allowed when two commits are given")
237+
die(
238+
"--diff_from_common_commit is only allowed when two commits are "
239+
"given"
240+
)
225241

226242
if os.path.dirname(opts.binary):
227243
opts.binary = os.path.abspath(opts.binary)
@@ -296,9 +312,9 @@ def main():
296312
def load_git_config(non_string_options=None):
297313
"""Return the git configuration as a dictionary.
298314
299-
All options are assumed to be strings unless in `non_string_options`, in which
300-
is a dictionary mapping option name (in lower case) to either "--bool" or
301-
"--int"."""
315+
All options are assumed to be strings unless in `non_string_options`, in
316+
which is a dictionary mapping option name (in lower case) to either "--bool"
317+
or "--int"."""
302318
if non_string_options is None:
303319
non_string_options = {}
304320
out = {}
@@ -323,9 +339,9 @@ def interpret_args(args, dash_dash, default_commit):
323339
args and placed in `dash_dash`.
324340
325341
If "--" is present (i.e., `dash_dash` is non-empty), the arguments to its
326-
left (if present) are taken as commits. Otherwise, the arguments are checked
327-
from left to right if they are commits or files. If commits are not given,
328-
a list with `default_commit` is used."""
342+
left (if present) are taken as commits. Otherwise, the arguments are
343+
checked from left to right if they are commits or files. If commits are not
344+
given, a list with `default_commit` is used."""
329345
if dash_dash:
330346
if len(args) == 0:
331347
commits = [default_commit]
@@ -367,7 +383,10 @@ def disambiguate_revision(value):
367383
return False
368384
if object_type in ("commit", "tag"):
369385
return True
370-
die("`%s` is a %s, but a commit or filename was expected" % (value, object_type))
386+
die(
387+
"`%s` is a %s, but a commit or filename was expected"
388+
% (value, object_type)
389+
)
371390

372391

373392
def get_object_type(value):
@@ -442,7 +461,9 @@ def extract_lines(patch_file):
442461
line_count = 1
443462
if start_line == 0:
444463
continue
445-
matches.setdefault(filename, []).append(Range(start_line, line_count))
464+
matches.setdefault(filename, []).append(
465+
Range(start_line, line_count)
466+
)
446467
return matches
447468

448469

@@ -497,9 +518,19 @@ def create_tree_from_index(filenames):
497518

498519
def index_contents_generator():
499520
for filename in filenames:
500-
git_ls_files_cmd = ["git", "ls-files", "--stage", "-z", "--", filename]
521+
git_ls_files_cmd = [
522+
"git",
523+
"ls-files",
524+
"--stage",
525+
"-z",
526+
"--",
527+
filename,
528+
]
501529
git_ls_files = subprocess.Popen(
502-
git_ls_files_cmd, env=env, stdin=subprocess.PIPE, stdout=subprocess.PIPE
530+
git_ls_files_cmd,
531+
env=env,
532+
stdin=subprocess.PIPE,
533+
stdout=subprocess.PIPE,
503534
)
504535
stdout = git_ls_files.communicate()[0]
505536
yield convert_string(stdout.split(b"\0")[0])
@@ -534,7 +565,13 @@ def run_clang_format_and_save_to_tree(
534565
os.path.basename(filename),
535566
]
536567
else:
537-
git_metadata_cmd = ["git", "ls-files", "--stage", "--", filename]
568+
git_metadata_cmd = [
569+
"git",
570+
"ls-files",
571+
"--stage",
572+
"--",
573+
filename,
574+
]
538575
git_metadata = subprocess.Popen(
539576
git_metadata_cmd,
540577
env=env,
@@ -566,8 +603,8 @@ def create_tree(input_lines, mode):
566603
567604
If mode is '--stdin', it must be a list of filenames. If mode is
568605
'--index-info' is must be a list of values suitable for "git update-index
569-
--index-info", such as "<mode> <SP> <sha1> <TAB> <filename>". Any other mode
570-
is invalid."""
606+
--index-info", such as "<mode> <SP> <sha1> <TAB> <filename>". Any other
607+
mode is invalid."""
571608
assert mode in ("--stdin", "--index-info")
572609
cmd = ["git", "update-index", "--add", "-z", mode]
573610
with temporary_index_file():
@@ -582,13 +619,18 @@ def create_tree(input_lines, mode):
582619

583620

584621
def clang_format_to_blob(
585-
filename, line_ranges, revision=None, binary="clang-format", style=None, env=None
622+
filename,
623+
line_ranges,
624+
revision=None,
625+
binary="clang-format",
626+
style=None,
627+
env=None,
586628
):
587629
"""Run clang-format on the given file and save the result to a git blob.
588630
589631
Runs on the file in `revision` if not None, or on the file in the working
590-
directory if `revision` is None. Revision can be set to an empty string to run
591-
clang-format on the file in the index.
632+
directory if `revision` is None. Revision can be set to an empty string to
633+
run clang-format on the file in the index.
592634
593635
Returns the object ID (SHA-1) of the created blob."""
594636
clang_format_cmd = [binary]
@@ -602,7 +644,12 @@ def clang_format_to_blob(
602644
)
603645
if revision is not None:
604646
clang_format_cmd.extend(["--assume-filename=" + filename])
605-
git_show_cmd = ["git", "cat-file", "blob", "%s:%s" % (revision, filename)]
647+
git_show_cmd = [
648+
"git",
649+
"cat-file",
650+
"blob",
651+
"%s:%s" % (revision, filename),
652+
]
606653
git_show = subprocess.Popen(
607654
git_show_cmd, env=env, stdin=subprocess.PIPE, stdout=subprocess.PIPE
608655
)
@@ -624,7 +671,13 @@ def clang_format_to_blob(
624671
else:
625672
raise
626673
clang_format_stdin.close()
627-
hash_object_cmd = ["git", "hash-object", "-w", "--path=" + filename, "--stdin"]
674+
hash_object_cmd = [
675+
"git",
676+
"hash-object",
677+
"-w",
678+
"--path=" + filename,
679+
"--stdin",
680+
]
628681
hash_object = subprocess.Popen(
629682
hash_object_cmd, stdin=clang_format.stdout, stdout=subprocess.PIPE
630683
)
@@ -641,8 +694,8 @@ def clang_format_to_blob(
641694

642695
@contextlib.contextmanager
643696
def temporary_index_file(tree=None):
644-
"""Context manager for setting GIT_INDEX_FILE to a temporary file and deleting
645-
the file afterward."""
697+
"""Context manager for setting GIT_INDEX_FILE to a temporary file and
698+
deleting the file afterward."""
646699
index_path = create_temporary_index(tree)
647700
old_index_path = os.environ.get("GIT_INDEX_FILE")
648701
os.environ["GIT_INDEX_FILE"] = index_path
@@ -671,9 +724,9 @@ def create_temporary_index(tree=None):
671724

672725
def print_diff(old_tree, new_tree):
673726
"""Print the diff between the two trees to stdout."""
674-
# We use the porcelain 'diff' and not plumbing 'diff-tree' because the output
675-
# is expected to be viewed by the user, and only the former does nice things
676-
# like color and pagination.
727+
# We use the porcelain 'diff' and not plumbing 'diff-tree' because the
728+
# output is expected to be viewed by the user, and only the former does nice
729+
# things like color and pagination.
677730
#
678731
# We also only print modified files since `new_tree` only contains the files
679732
# that were modified, so unmodified files would show as deleted without the
@@ -685,15 +738,23 @@ def print_diff(old_tree, new_tree):
685738

686739
def print_diffstat(old_tree, new_tree):
687740
"""Print the diffstat between the two trees to stdout."""
688-
# We use the porcelain 'diff' and not plumbing 'diff-tree' because the output
689-
# is expected to be viewed by the user, and only the former does nice things
690-
# like color and pagination.
741+
# We use the porcelain 'diff' and not plumbing 'diff-tree' because the
742+
# output is expected to be viewed by the user, and only the former does nice
743+
# things like color and pagination.
691744
#
692745
# We also only print modified files since `new_tree` only contains the files
693746
# that were modified, so unmodified files would show as deleted without the
694747
# filter.
695748
return subprocess.run(
696-
["git", "diff", "--diff-filter=M", "--exit-code", "--stat", old_tree, new_tree]
749+
[
750+
"git",
751+
"diff",
752+
"--diff-filter=M",
753+
"--exit-code",
754+
"--stat",
755+
old_tree,
756+
new_tree,
757+
]
697758
).returncode
698759

699760

@@ -717,10 +778,13 @@ def apply_changes(old_tree, new_tree, force=False, patch_mode=False):
717778
.split("\0")
718779
)
719780
if not force:
720-
unstaged_files = run("git", "diff-files", "--name-status", *changed_files)
781+
unstaged_files = run(
782+
"git", "diff-files", "--name-status", *changed_files
783+
)
721784
if unstaged_files:
722785
print(
723-
"The following files would be modified but " "have unstaged changes:",
786+
"The following files would be modified but have unstaged "
787+
"changes:",
724788
file=sys.stderr,
725789
)
726790
print(unstaged_files, file=sys.stderr)
@@ -749,7 +813,10 @@ def run(*args, **kwargs):
749813
for name in kwargs:
750814
raise TypeError("run() got an unexpected keyword argument '%s'" % name)
751815
p = subprocess.Popen(
752-
args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE
816+
args,
817+
stdout=subprocess.PIPE,
818+
stderr=subprocess.PIPE,
819+
stdin=subprocess.PIPE,
753820
)
754821
stdout, stderr = p.communicate(input=stdin)
755822

@@ -759,13 +826,17 @@ def run(*args, **kwargs):
759826
if p.returncode == 0:
760827
if stderr:
761828
if verbose:
762-
print("`%s` printed to stderr:" % " ".join(args), file=sys.stderr)
829+
print(
830+
"`%s` printed to stderr:" % " ".join(args), file=sys.stderr
831+
)
763832
print(stderr.rstrip(), file=sys.stderr)
764833
if strip:
765834
stdout = stdout.rstrip("\r\n")
766835
return stdout
767836
if verbose:
768-
print("`%s` returned %s" % (" ".join(args), p.returncode), file=sys.stderr)
837+
print(
838+
"`%s` returned %s" % (" ".join(args), p.returncode), file=sys.stderr
839+
)
769840
if stderr:
770841
print(stderr.rstrip(), file=sys.stderr)
771842
sys.exit(2)

0 commit comments

Comments
 (0)