ed | 509e8b9 | 2014-09-02 20:59:13 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 2 | # |
| 3 | #===- clang-format-diff.py - ClangFormat Diff Reformatter ----*- python -*--===# |
| 4 | # |
chandlerc | 079ee0b | 2019-01-19 08:50:56 +0000 | [diff] [blame^] | 5 | # Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 6 | # See https://llvm.org/LICENSE.txt for license information. |
| 7 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 8 | # |
| 9 | #===------------------------------------------------------------------------===# |
| 10 | |
| 11 | r""" |
| 12 | ClangFormat Diff Reformatter |
| 13 | ============================ |
| 14 | |
| 15 | This script reads input from a unified diff and reformats all the changed |
| 16 | lines. This is useful to reformat all the lines touched by a specific patch. |
djasper | 7839535 | 2014-05-14 09:36:11 +0000 | [diff] [blame] | 17 | Example usage for git/svn users: |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 18 | |
sylvestre | ad8850a | 2016-12-03 23:22:45 +0000 | [diff] [blame] | 19 | git diff -U0 --no-color HEAD^ | clang-format-diff.py -p1 -i |
djasper | 7839535 | 2014-05-14 09:36:11 +0000 | [diff] [blame] | 20 | svn diff --diff-cmd=diff -x-U0 | clang-format-diff.py -i |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 21 | |
| 22 | """ |
serge_sans_paille | 0561334 | 2018-12-18 16:07:37 +0000 | [diff] [blame] | 23 | from __future__ import absolute_import, division, print_function |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 24 | |
| 25 | import argparse |
alexfh | c770b67 | 2013-10-11 21:32:01 +0000 | [diff] [blame] | 26 | import difflib |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 27 | import re |
| 28 | import subprocess |
| 29 | import sys |
serge_sans_paille | 2757fe9 | 2019-01-03 14:26:56 +0000 | [diff] [blame] | 30 | |
| 31 | if sys.version_info.major >= 3: |
| 32 | from io import StringIO |
| 33 | else: |
| 34 | from io import BytesIO as StringIO |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 35 | |
| 36 | |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 37 | def main(): |
| 38 | parser = argparse.ArgumentParser(description= |
alexfh | c770b67 | 2013-10-11 21:32:01 +0000 | [diff] [blame] | 39 | 'Reformat changed lines in diff. Without -i ' |
alp | 9b6f1cb | 2013-12-04 00:48:22 +0000 | [diff] [blame] | 40 | 'option just output the diff that would be ' |
alexfh | c770b67 | 2013-10-11 21:32:01 +0000 | [diff] [blame] | 41 | 'introduced.') |
| 42 | parser.add_argument('-i', action='store_true', default=False, |
| 43 | help='apply edits to files instead of displaying a diff') |
alp | cfbd31f | 2013-12-10 13:51:53 +0000 | [diff] [blame] | 44 | parser.add_argument('-p', metavar='NUM', default=0, |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 45 | help='strip the smallest prefix containing P slashes') |
alp | 51f6d9a | 2013-12-18 21:34:07 +0000 | [diff] [blame] | 46 | parser.add_argument('-regex', metavar='PATTERN', default=None, |
alexfh | 1b8fbd8 | 2013-12-16 10:57:30 +0000 | [diff] [blame] | 47 | help='custom pattern selecting file paths to reformat ' |
djasper | 1d353c9 | 2013-12-19 10:21:37 +0000 | [diff] [blame] | 48 | '(case sensitive, overrides -iregex)') |
alexfh | 1b8fbd8 | 2013-12-16 10:57:30 +0000 | [diff] [blame] | 49 | parser.add_argument('-iregex', metavar='PATTERN', default= |
djasper | cab6841 | 2015-03-11 14:58:38 +0000 | [diff] [blame] | 50 | r'.*\.(cpp|cc|c\+\+|cxx|c|cl|h|hpp|m|mm|inc|js|ts|proto' |
djasper | 81edd55 | 2014-12-08 19:39:03 +0000 | [diff] [blame] | 51 | r'|protodevel|java)', |
alexfh | 1b8fbd8 | 2013-12-16 10:57:30 +0000 | [diff] [blame] | 52 | help='custom pattern selecting file paths to reformat ' |
djasper | 1d353c9 | 2013-12-19 10:21:37 +0000 | [diff] [blame] | 53 | '(case insensitive, overridden by -regex)') |
djasper | ea5da0e | 2015-10-07 17:00:20 +0000 | [diff] [blame] | 54 | parser.add_argument('-sort-includes', action='store_true', default=False, |
| 55 | help='let clang-format sort include blocks') |
djasper | ceb8871 | 2014-11-14 13:27:28 +0000 | [diff] [blame] | 56 | parser.add_argument('-v', '--verbose', action='store_true', |
| 57 | help='be more verbose, ineffective without -i') |
djasper | a72164d | 2016-01-20 18:55:57 +0000 | [diff] [blame] | 58 | parser.add_argument('-style', |
| 59 | help='formatting style to apply (LLVM, Google, Chromium, ' |
| 60 | 'Mozilla, WebKit)') |
| 61 | parser.add_argument('-binary', default='clang-format', |
| 62 | help='location of binary to use for clang-format') |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 63 | args = parser.parse_args() |
| 64 | |
djasper | 75c3219 | 2013-09-18 12:14:09 +0000 | [diff] [blame] | 65 | # Extract changed lines for each file. |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 66 | filename = None |
djasper | 75c3219 | 2013-09-18 12:14:09 +0000 | [diff] [blame] | 67 | lines_by_file = {} |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 68 | for line in sys.stdin: |
| 69 | match = re.search('^\+\+\+\ (.*?/){%s}(\S*)' % args.p, line) |
| 70 | if match: |
| 71 | filename = match.group(2) |
| 72 | if filename == None: |
| 73 | continue |
| 74 | |
alp | 51f6d9a | 2013-12-18 21:34:07 +0000 | [diff] [blame] | 75 | if args.regex is not None: |
| 76 | if not re.match('^%s$' % args.regex, filename): |
alexfh | 1b8fbd8 | 2013-12-16 10:57:30 +0000 | [diff] [blame] | 77 | continue |
| 78 | else: |
alp | 51f6d9a | 2013-12-18 21:34:07 +0000 | [diff] [blame] | 79 | if not re.match('^%s$' % args.iregex, filename, re.IGNORECASE): |
alexfh | 1b8fbd8 | 2013-12-16 10:57:30 +0000 | [diff] [blame] | 80 | continue |
djasper | 75c3219 | 2013-09-18 12:14:09 +0000 | [diff] [blame] | 81 | |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 82 | match = re.search('^@@.*\+(\d+)(,(\d+))?', line) |
| 83 | if match: |
djasper | 75c3219 | 2013-09-18 12:14:09 +0000 | [diff] [blame] | 84 | start_line = int(match.group(1)) |
djasper | e3eb058 | 2013-10-02 13:59:03 +0000 | [diff] [blame] | 85 | line_count = 1 |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 86 | if match.group(3): |
djasper | e3eb058 | 2013-10-02 13:59:03 +0000 | [diff] [blame] | 87 | line_count = int(match.group(3)) |
| 88 | if line_count == 0: |
| 89 | continue |
krasimir | e863ca5 | 2018-08-03 10:04:58 +0000 | [diff] [blame] | 90 | end_line = start_line + line_count - 1 |
djasper | 75c3219 | 2013-09-18 12:14:09 +0000 | [diff] [blame] | 91 | lines_by_file.setdefault(filename, []).extend( |
| 92 | ['-lines', str(start_line) + ':' + str(end_line)]) |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 93 | |
djasper | 75c3219 | 2013-09-18 12:14:09 +0000 | [diff] [blame] | 94 | # Reformat files containing changes in place. |
krasimir | e863ca5 | 2018-08-03 10:04:58 +0000 | [diff] [blame] | 95 | for filename, lines in lines_by_file.items(): |
djasper | ceb8871 | 2014-11-14 13:27:28 +0000 | [diff] [blame] | 96 | if args.i and args.verbose: |
krasimir | e863ca5 | 2018-08-03 10:04:58 +0000 | [diff] [blame] | 97 | print('Formatting {}'.format(filename)) |
djasper | a72164d | 2016-01-20 18:55:57 +0000 | [diff] [blame] | 98 | command = [args.binary, filename] |
alexfh | c770b67 | 2013-10-11 21:32:01 +0000 | [diff] [blame] | 99 | if args.i: |
| 100 | command.append('-i') |
djasper | ea5da0e | 2015-10-07 17:00:20 +0000 | [diff] [blame] | 101 | if args.sort_includes: |
| 102 | command.append('-sort-includes') |
djasper | 75c3219 | 2013-09-18 12:14:09 +0000 | [diff] [blame] | 103 | command.extend(lines) |
| 104 | if args.style: |
djasper | c49abf9 | 2013-09-21 10:05:02 +0000 | [diff] [blame] | 105 | command.extend(['-style', args.style]) |
krasimir | e863ca5 | 2018-08-03 10:04:58 +0000 | [diff] [blame] | 106 | p = subprocess.Popen(command, |
| 107 | stdout=subprocess.PIPE, |
| 108 | stderr=None, |
| 109 | stdin=subprocess.PIPE, |
| 110 | universal_newlines=True) |
djasper | 75c3219 | 2013-09-18 12:14:09 +0000 | [diff] [blame] | 111 | stdout, stderr = p.communicate() |
djasper | 60dad2e | 2013-10-08 15:54:36 +0000 | [diff] [blame] | 112 | if p.returncode != 0: |
krasimir | e863ca5 | 2018-08-03 10:04:58 +0000 | [diff] [blame] | 113 | sys.exit(p.returncode) |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 114 | |
alexfh | c770b67 | 2013-10-11 21:32:01 +0000 | [diff] [blame] | 115 | if not args.i: |
| 116 | with open(filename) as f: |
| 117 | code = f.readlines() |
krasimir | e863ca5 | 2018-08-03 10:04:58 +0000 | [diff] [blame] | 118 | formatted_code = StringIO(stdout).readlines() |
alexfh | c770b67 | 2013-10-11 21:32:01 +0000 | [diff] [blame] | 119 | diff = difflib.unified_diff(code, formatted_code, |
| 120 | filename, filename, |
| 121 | '(before formatting)', '(after formatting)') |
krasimir | e863ca5 | 2018-08-03 10:04:58 +0000 | [diff] [blame] | 122 | diff_string = ''.join(diff) |
alexfh | c770b67 | 2013-10-11 21:32:01 +0000 | [diff] [blame] | 123 | if len(diff_string) > 0: |
alp | 367218e | 2013-12-05 08:14:54 +0000 | [diff] [blame] | 124 | sys.stdout.write(diff_string) |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 125 | |
| 126 | if __name__ == '__main__': |
| 127 | main() |