Lines Matching +full:commits +full:-
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('--')
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',
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,
126 opts.verbose -= opts.quiet
129 commits, files = interpret_args(opts.args, dash_dash, opts.commit)
130 if len(commits) > 1:
132 die('--diff is required when two commits are given')
134 if len(commits) > 2:
135 die('at most two commits allowed; %d given' % len(commits))
136 changed_lines = compute_diff_and_extract_lines(commits, files)
147 print('Running clang-format on the following files:')
156 if len(commits) > 1:
157 old_tree = commits[1]
159 revision=commits[1],
172 print('clang-format did not modify any files')
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
209 left (if present) are taken as commits. Otherwise, the arguments are checked
210 from left to right if they are commits or files. If commits are not given,
214 commits = [default_commit]
216 commits = args
217 for commit in commits:
226 commits = []
230 commits.append(args.pop(0))
231 if not commits:
232 commits = [default_commit]
235 commits = [default_commit]
237 return commits, files
244 run('git', 'rev-parse', value, verbose=False)
257 cmd = ['git', 'cat-file', '-t', value]
265 def compute_diff_and_extract_lines(commits, files): argument
267 diff_process = compute_diff(commits, files)
277 def compute_diff(commits, files): argument
278 """Return a subprocess object producing the diff from `commits`.
282 one was specified, or the difference between both specified commits, filtered
283 on `files` (if non-empty). Zero context lines are used in the patch."""
284 git_tool = 'diff-index'
285 if len(commits) > 1:
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',
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)
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'))