2014-09-03 04:59:13 +08:00
|
|
|
#!/usr/bin/env python
|
2013-03-20 17:53:23 +08:00
|
|
|
#
|
|
|
|
#===- clang-format-diff.py - ClangFormat Diff Reformatter ----*- python -*--===#
|
|
|
|
#
|
2019-01-19 16:50:56 +08:00
|
|
|
# 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
|
2013-03-20 17:53:23 +08:00
|
|
|
#
|
|
|
|
#===------------------------------------------------------------------------===#
|
|
|
|
|
2019-07-24 01:34:18 +08:00
|
|
|
"""
|
2013-03-20 17:53:23 +08:00
|
|
|
This script reads input from a unified diff and reformats all the changed
|
|
|
|
lines. This is useful to reformat all the lines touched by a specific patch.
|
2014-05-14 17:36:11 +08:00
|
|
|
Example usage for git/svn users:
|
2013-03-20 17:53:23 +08:00
|
|
|
|
2016-12-04 07:22:45 +08:00
|
|
|
git diff -U0 --no-color HEAD^ | clang-format-diff.py -p1 -i
|
2014-05-14 17:36:11 +08:00
|
|
|
svn diff --diff-cmd=diff -x-U0 | clang-format-diff.py -i
|
2013-03-20 17:53:23 +08:00
|
|
|
|
|
|
|
"""
|
2018-12-19 00:07:37 +08:00
|
|
|
from __future__ import absolute_import, division, print_function
|
2013-03-20 17:53:23 +08:00
|
|
|
|
|
|
|
import argparse
|
2013-10-12 05:32:01 +08:00
|
|
|
import difflib
|
2013-03-20 17:53:23 +08:00
|
|
|
import re
|
|
|
|
import subprocess
|
|
|
|
import sys
|
2019-01-03 22:26:56 +08:00
|
|
|
|
|
|
|
if sys.version_info.major >= 3:
|
|
|
|
from io import StringIO
|
|
|
|
else:
|
|
|
|
from io import BytesIO as StringIO
|
2013-03-20 17:53:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2019-07-24 01:34:18 +08:00
|
|
|
parser = argparse.ArgumentParser(description=__doc__,
|
|
|
|
formatter_class=
|
|
|
|
argparse.RawDescriptionHelpFormatter)
|
2013-10-12 05:32:01 +08:00
|
|
|
parser.add_argument('-i', action='store_true', default=False,
|
|
|
|
help='apply edits to files instead of displaying a diff')
|
2013-12-10 21:51:53 +08:00
|
|
|
parser.add_argument('-p', metavar='NUM', default=0,
|
2013-03-20 17:53:23 +08:00
|
|
|
help='strip the smallest prefix containing P slashes')
|
2013-12-19 05:34:07 +08:00
|
|
|
parser.add_argument('-regex', metavar='PATTERN', default=None,
|
2013-12-16 18:57:30 +08:00
|
|
|
help='custom pattern selecting file paths to reformat '
|
2013-12-19 18:21:37 +08:00
|
|
|
'(case sensitive, overrides -iregex)')
|
2013-12-16 18:57:30 +08:00
|
|
|
parser.add_argument('-iregex', metavar='PATTERN', default=
|
2015-03-11 22:58:38 +08:00
|
|
|
r'.*\.(cpp|cc|c\+\+|cxx|c|cl|h|hpp|m|mm|inc|js|ts|proto'
|
2014-12-09 03:39:03 +08:00
|
|
|
r'|protodevel|java)',
|
2013-12-16 18:57:30 +08:00
|
|
|
help='custom pattern selecting file paths to reformat '
|
2013-12-19 18:21:37 +08:00
|
|
|
'(case insensitive, overridden by -regex)')
|
2015-10-08 01:00:20 +08:00
|
|
|
parser.add_argument('-sort-includes', action='store_true', default=False,
|
|
|
|
help='let clang-format sort include blocks')
|
2014-11-14 21:27:28 +08:00
|
|
|
parser.add_argument('-v', '--verbose', action='store_true',
|
|
|
|
help='be more verbose, ineffective without -i')
|
2016-01-21 02:55:57 +08:00
|
|
|
parser.add_argument('-style',
|
|
|
|
help='formatting style to apply (LLVM, Google, Chromium, '
|
|
|
|
'Mozilla, WebKit)')
|
|
|
|
parser.add_argument('-binary', default='clang-format',
|
|
|
|
help='location of binary to use for clang-format')
|
2013-03-20 17:53:23 +08:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2013-09-18 20:14:09 +08:00
|
|
|
# Extract changed lines for each file.
|
2013-03-20 17:53:23 +08:00
|
|
|
filename = None
|
2013-09-18 20:14:09 +08:00
|
|
|
lines_by_file = {}
|
2013-03-20 17:53:23 +08:00
|
|
|
for line in sys.stdin:
|
2019-02-11 23:03:17 +08:00
|
|
|
match = re.search(r'^\+\+\+\ (.*?/){%s}(\S*)' % args.p, line)
|
2013-03-20 17:53:23 +08:00
|
|
|
if match:
|
|
|
|
filename = match.group(2)
|
|
|
|
if filename == None:
|
|
|
|
continue
|
|
|
|
|
2013-12-19 05:34:07 +08:00
|
|
|
if args.regex is not None:
|
|
|
|
if not re.match('^%s$' % args.regex, filename):
|
2013-12-16 18:57:30 +08:00
|
|
|
continue
|
|
|
|
else:
|
2013-12-19 05:34:07 +08:00
|
|
|
if not re.match('^%s$' % args.iregex, filename, re.IGNORECASE):
|
2013-12-16 18:57:30 +08:00
|
|
|
continue
|
2013-09-18 20:14:09 +08:00
|
|
|
|
2019-02-11 23:03:17 +08:00
|
|
|
match = re.search(r'^@@.*\+(\d+)(,(\d+))?', line)
|
2013-03-20 17:53:23 +08:00
|
|
|
if match:
|
2013-09-18 20:14:09 +08:00
|
|
|
start_line = int(match.group(1))
|
2013-10-02 21:59:03 +08:00
|
|
|
line_count = 1
|
2013-03-20 17:53:23 +08:00
|
|
|
if match.group(3):
|
2013-10-02 21:59:03 +08:00
|
|
|
line_count = int(match.group(3))
|
|
|
|
if line_count == 0:
|
|
|
|
continue
|
2018-08-03 18:04:58 +08:00
|
|
|
end_line = start_line + line_count - 1
|
2013-09-18 20:14:09 +08:00
|
|
|
lines_by_file.setdefault(filename, []).extend(
|
|
|
|
['-lines', str(start_line) + ':' + str(end_line)])
|
|
|
|
|
|
|
|
# Reformat files containing changes in place.
|
2018-08-03 18:04:58 +08:00
|
|
|
for filename, lines in lines_by_file.items():
|
2014-11-14 21:27:28 +08:00
|
|
|
if args.i and args.verbose:
|
2018-08-03 18:04:58 +08:00
|
|
|
print('Formatting {}'.format(filename))
|
2016-01-21 02:55:57 +08:00
|
|
|
command = [args.binary, filename]
|
2013-10-12 05:32:01 +08:00
|
|
|
if args.i:
|
|
|
|
command.append('-i')
|
2015-10-08 01:00:20 +08:00
|
|
|
if args.sort_includes:
|
|
|
|
command.append('-sort-includes')
|
2013-09-18 20:14:09 +08:00
|
|
|
command.extend(lines)
|
|
|
|
if args.style:
|
2013-09-21 18:05:02 +08:00
|
|
|
command.extend(['-style', args.style])
|
2018-08-03 18:04:58 +08:00
|
|
|
p = subprocess.Popen(command,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=None,
|
|
|
|
stdin=subprocess.PIPE,
|
|
|
|
universal_newlines=True)
|
2013-09-18 20:14:09 +08:00
|
|
|
stdout, stderr = p.communicate()
|
2013-10-08 23:54:36 +08:00
|
|
|
if p.returncode != 0:
|
2018-08-03 18:04:58 +08:00
|
|
|
sys.exit(p.returncode)
|
2013-03-20 17:53:23 +08:00
|
|
|
|
2013-10-12 05:32:01 +08:00
|
|
|
if not args.i:
|
|
|
|
with open(filename) as f:
|
|
|
|
code = f.readlines()
|
2018-08-03 18:04:58 +08:00
|
|
|
formatted_code = StringIO(stdout).readlines()
|
2013-10-12 05:32:01 +08:00
|
|
|
diff = difflib.unified_diff(code, formatted_code,
|
|
|
|
filename, filename,
|
|
|
|
'(before formatting)', '(after formatting)')
|
2018-08-03 18:04:58 +08:00
|
|
|
diff_string = ''.join(diff)
|
2013-10-12 05:32:01 +08:00
|
|
|
if len(diff_string) > 0:
|
2013-12-05 16:14:54 +08:00
|
|
|
sys.stdout.write(diff_string)
|
2013-03-20 17:53:23 +08:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|