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