Lines Matching +full:check +full:- +full:clang +full:- +full:python
3 # ===- git-clang-format - ClangFormat Git Integration -------*- python -*--=== #
7 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
9 # ===----------------------------------------------------------------------=== #
12 clang-format git integration
15 This file provides a clang-format integration for git. Put it somewhere in your
16 path and ensure that it is executable. Then, "git clang-format" will invoke
17 clang-format on the changes in current files or a specific commit.
20 git clang-format -h
22 Requires Python 2.7 or Python 3
36 "git clang-format [OPTIONS] [<commit>] [<commit>|--staged] [--] [<file>...]"
40 If zero or one commits are given, run clang-format on all lines that differ
46 git clang-format
49 git clang-format HEAD~1
52 git clang-format main
54 If two commits are given (requires --diff), run clang-format on all lines in the
57 The following git-config settings set the default of the corresponding option:
64 # Name of the temporary index file in which save the output of clang-format.
66 temp_index_basename = "clang-format-index"
75 # In order to keep '--' yet allow options after positionals, we need to
76 # check for '--' ourselves. (Setting nargs='*' throws away the '--', while
80 idx = argv.index("--")
89 # From clang/lib/Frontend/FrontendOptions.cpp, all lower case
109 # Other languages that clang-format supports
138 "--binary",
139 default=config.get("clangformat.binary", "clang-format"),
140 help="path to clang-format",
143 "--commit",
148 "--diff",
153 "--diffstat",
158 "--extensions",
161 "comma-separated list of file extensions to format, "
162 "excluding the period and case-insensitive"
166 "-f",
167 "--force",
172 "-p", "--patch", action="store_true", help="select hunks interactively"
175 "-q",
176 "--quiet",
182 "--staged",
183 "--cached",
188 "--style",
190 help="passed to clang-format",
193 "-v",
194 "--verbose",
200 "--diff_from_common_commit",
225 opts.verbose -= opts.quiet
233 die("--staged is not allowed when two commits are given")
235 die("--diff is required when two commits are given")
238 "--diff_from_common_commit is only allowed when two commits are "
261 "ignored by clang-format):"
266 print("Running clang-format on the following files:")
293 print("clang-format did not modify any files")
316 which is a dictionary mapping option name (in lower case) to either "--bool"
317 or "--int"."""
321 for entry in run("git", "config", "--list", "--null").split("\0"):
326 # A setting with no '=' ('\n' with --null) is implicitly 'true'
336 """Interpret `args` as "[commits] [--] [files]" and return (commits, files).
338 It is assumed that "--" and everything that follows has been removed from
341 If "--" is present (i.e., `dash_dash` is non-empty), the arguments to its
380 run("git", "rev-parse", value, verbose=False)
395 cmd = ["git", "cat-file", "-t", value]
419 differences between the working directory (or stage if --staged is used) and
421 both specified commits, filtered on `files` (if non-empty).
423 git_tool = "diff-index"
426 git_tool = "diff-tree"
430 extra_args += ["--cached"]
432 cmd = ["git", git_tool, "-p", "-U0"] + extra_args + commits + ["--"]
445 The input must have been produced with ``-U0``, meaning unidiff format with
454 match = re.search(r"^@@ -[0-9,]+ \+(\d+)(,(\d+))?", line)
492 """Delete every key in `dictionary` that is ignored by clang-format."""
493 ignored_files = run(binary, "-list-ignored", *dictionary.keys())
503 toplevel = run("git", "rev-parse", "--show-toplevel")
510 Returns the object ID (SHA-1) of the created tree."""
511 return create_tree(filenames, "--stdin")
523 "ls-files",
524 "--stage",
525 "-z",
526 "--",
538 return create_tree(index_contents_generator(), "--index-info")
542 changed_lines, revision=None, binary="clang-format", style=None
544 """Run clang-format on each file and save the result to a git tree.
546 Returns the object ID (SHA-1) of the created tree."""
553 return container.iteritems() # Python 2
555 return container.items() # Python 3
563 "ls-tree",
570 "ls-files",
571 "--stage",
572 "--",
598 return create_tree(index_info_generator(), "--index-info")
604 If mode is '--stdin', it must be a list of filenames. If mode is
605 '--index-info' is must be a list of values suitable for "git update-index
606 --index-info", such as "<mode> <SP> <sha1> <TAB> <filename>". Any other
608 assert mode in ("--stdin", "--index-info")
609 cmd = ["git", "update-index", "--add", "-z", mode]
617 tree_id = run("git", "write-tree")
625 binary="clang-format",
629 """Run clang-format on the given file and save the result to a git blob.
633 run clang-format on the file in the index.
635 Returns the object ID (SHA-1) of the created blob."""
638 clang_format_cmd.extend(["--style=" + style])
641 "--lines=%s:%s" % (start_line, start_line + line_count - 1)
646 clang_format_cmd.extend(["--assume-filename=" + filename])
649 "cat-file",
676 "hash-object",
677 "-w",
678 "--path=" + filename,
679 "--stdin",
717 gitdir = run("git", "rev-parse", "--git-dir")
720 tree = "--empty"
721 run("git", "read-tree", "--index-output=" + path, tree)
727 # We use the porcelain 'diff' and not plumbing 'diff-tree' because the
735 ["git", "diff", "--diff-filter=M", "--exit-code", old_tree, new_tree]
741 # We use the porcelain 'diff' and not plumbing 'diff-tree' because the
752 "--diff-filter=M",
753 "--exit-code",
754 "--stat",
765 `patch_mode`, runs `git checkout --patch` to select hunks interactively."""
769 "diff-tree",
770 "--diff-filter=M",
771 "-r",
772 "-z",
773 "--name-only",
782 "git", "diff-files", "--name-status", *changed_files
801 subprocess.run(["git", "checkout", "--patch", new_tree], check=True)
805 run("git", "checkout-index", "-f", "--", *changed_files)
851 # Encode to UTF-8 to get binary data.
854 return str_input.encode("utf-8")
860 return bytes_input.encode("utf-8")
865 return to_string(bytes_input.decode("utf-8"))