• Home
  • Raw
  • Download

Lines Matching +full:clang +full:- +full:format +full:- +full:diff

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
35 usage = 'git clang-format [OPTIONS] [<commit>] [<commit>] [--] [<file>...]'
38 If zero or one commits are given, run clang-format on all lines that differ
42 If two commits are given (requires --diff), run clang-format on all lines in the
45 The following git-config settings set the default of the corresponding option:
52 # Name of the temporary index file in which save the output of clang-format.
54 temp_index_basename = 'clang-format-index'
63 # In order to keep '--' yet allow options after positionals, we need to
64 # check for '--' ourselves. (Setting nargs='*' throws away the '--', while
68 idx = argv.index('--')
76 # From clang/lib/Frontend/FrontendOptions.cpp, all lower case
82 # Other languages that clang-format supports
93 p.add_argument('--binary',
94 default=config.get('clangformat.binary', 'clang-format'),
95 help='path to clang-format'),
96 p.add_argument('--commit',
99 p.add_argument('--diff', action='store_true',
100 help='print a diff instead of applying the changes')
101 p.add_argument('--extensions',
104 help=('comma-separated list of file extensions to format, '
105 'excluding the period and case-insensitive')),
106 p.add_argument('-f', '--force', action='store_true',
108 p.add_argument('-p', '--patch', action='store_true',
110 p.add_argument('-q', '--quiet', action='count', default=0,
112 p.add_argument('--style',
114 help='passed to clang-format'),
115 p.add_argument('-v', '--verbose', action='count', default=0,
121 help='revision from which to compute the diff')
126 opts.verbose -= opts.quiet
131 if not opts.diff:
132 die('--diff is required when two commits are given')
147 print('Running clang-format on the following files:')
151 print('no modified files to format')
153 # The computed diff outputs absolute paths, so we must cd before accessing
172 print('clang-format did not modify any files')
173 elif opts.diff:
188 is a dictionary mapping option name (in lower case) to either "--bool" or
189 "--int"."""
193 for entry in run('git', 'config', '--list', '--null').split('\0'):
203 """Interpret `args` as "[commits] [--] [files]" and return (commits, files).
205 It is assumed that "--" and everything that follows has been removed from
208 If "--" is present (i.e., `dash_dash` is non-empty), the arguments to its
244 run('git', 'rev-parse', value, verbose=False)
257 cmd = ['git', 'cat-file', '-t', value]
278 """Return a subprocess object producing the diff from `commits`.
283 on `files` (if non-empty). Zero context lines are used in the patch."""
284 git_tool = 'diff-index'
286 git_tool = 'diff-tree'
287 cmd = ['git', git_tool, '-p', '-U0'] + commits + ['--']
300 The input must have been produced with ``-U0``, meaning unidiff format with
309 match = re.search(r'^@@ -[0-9,]+ \+(\d+)(,(\d+))?', line)
336 toplevel = run('git', 'rev-parse', '--show-toplevel')
343 Returns the object ID (SHA-1) of the created tree."""
344 return create_tree(filenames, '--stdin')
348 binary='clang-format', style=None):
349 """Run clang-format on each file and save the result to a git tree.
351 Returns the object ID (SHA-1) of the created tree."""
360 git_metadata_cmd = ['git', 'ls-tree',
369 # Adjust python3 octal format so that it matches what git expects
377 return create_tree(index_info_generator(), '--index-info')
383 If mode is '--stdin', it must be a list of filenames. If mode is
384 '--index-info' is must be a list of values suitable for "git update-index
385 --index-info", such as "<mode> <SP> <sha1> <TAB> <filename>". Any other mode
387 assert mode in ('--stdin', '--index-info')
388 cmd = ['git', 'update-index', '--add', '-z', mode]
396 tree_id = run('git', 'write-tree')
401 binary='clang-format', style=None):
402 """Run clang-format on the given file and save the result to a git blob.
407 Returns the object ID (SHA-1) of the created blob."""
410 clang_format_cmd.extend(['-style='+style])
412 '-lines=%s:%s' % (start_line, start_line+line_count-1)
415 clang_format_cmd.extend(['-assume-filename='+filename])
416 git_show_cmd = ['git', 'cat-file', 'blob', '%s:%s' % (revision, filename)]
436 hash_object_cmd = ['git', 'hash-object', '-w', '--path='+filename, '--stdin']
472 gitdir = run('git', 'rev-parse', '--git-dir')
475 tree = '--empty'
476 run('git', 'read-tree', '--index-output='+path, tree)
481 """Print the diff between the two trees to stdout."""
482 # We use the porcelain 'diff' and not plumbing 'diff-tree' because the output
489 subprocess.check_call(['git', 'diff', '--diff-filter=M', old_tree, new_tree,
490 '--'])
497 `patch_mode`, runs `git checkout --patch` to select hunks interactively."""
498 changed_files = run('git', 'diff-tree', '--diff-filter=M', '-r', '-z',
499 '--name-only', old_tree,
502 unstaged_files = run('git', 'diff-files', '--name-status', *changed_files)
517 subprocess.check_call(['git', 'checkout', '--patch', new_tree])
521 run('git', 'checkout-index', '-a', '-f')
559 # Encode to UTF-8 to get binary data.
562 return str_input.encode('utf-8')
568 return bytes_input.encode('utf-8')
573 return to_string(bytes_input.decode('utf-8'))