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 | # |
| 5 | # The LLVM Compiler Infrastructure |
| 6 | # |
| 7 | # This file is distributed under the University of Illinois Open Source |
| 8 | # License. See LICENSE.TXT for details. |
| 9 | # |
| 10 | #===------------------------------------------------------------------------===# |
| 11 | |
| 12 | r""" |
| 13 | ClangFormat Diff Reformatter |
| 14 | ============================ |
| 15 | |
| 16 | This script reads input from a unified diff and reformats all the changed |
| 17 | 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] | 18 | Example usage for git/svn users: |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 19 | |
alexfh | c770b67 | 2013-10-11 21:32:01 +0000 | [diff] [blame] | 20 | git diff -U0 HEAD^ | clang-format-diff.py -p1 -i |
djasper | 7839535 | 2014-05-14 09:36:11 +0000 | [diff] [blame] | 21 | svn diff --diff-cmd=diff -x-U0 | clang-format-diff.py -i |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 22 | |
| 23 | """ |
| 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 |
alexfh | c770b67 | 2013-10-11 21:32:01 +0000 | [diff] [blame] | 28 | import string |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 29 | import subprocess |
alexfh | c770b67 | 2013-10-11 21:32:01 +0000 | [diff] [blame] | 30 | import StringIO |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 31 | import sys |
| 32 | |
| 33 | |
| 34 | # Change this to the full path if clang-format is not on the path. |
| 35 | binary = 'clang-format' |
| 36 | |
| 37 | |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 38 | def main(): |
| 39 | parser = argparse.ArgumentParser(description= |
alexfh | c770b67 | 2013-10-11 21:32:01 +0000 | [diff] [blame] | 40 | 'Reformat changed lines in diff. Without -i ' |
alp | 9b6f1cb | 2013-12-04 00:48:22 +0000 | [diff] [blame] | 41 | 'option just output the diff that would be ' |
alexfh | c770b67 | 2013-10-11 21:32:01 +0000 | [diff] [blame] | 42 | 'introduced.') |
| 43 | parser.add_argument('-i', action='store_true', default=False, |
| 44 | help='apply edits to files instead of displaying a diff') |
alp | cfbd31f | 2013-12-10 13:51:53 +0000 | [diff] [blame] | 45 | parser.add_argument('-p', metavar='NUM', default=0, |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 46 | help='strip the smallest prefix containing P slashes') |
alp | 51f6d9a | 2013-12-18 21:34:07 +0000 | [diff] [blame] | 47 | parser.add_argument('-regex', metavar='PATTERN', default=None, |
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 sensitive, overrides -iregex)') |
alexfh | 1b8fbd8 | 2013-12-16 10:57:30 +0000 | [diff] [blame] | 50 | parser.add_argument('-iregex', metavar='PATTERN', default= |
djasper | 3bbc964 | 2014-01-21 15:40:01 +0000 | [diff] [blame] | 51 | r'.*\.(cpp|cc|c\+\+|cxx|c|cl|h|hpp|m|mm|inc|js|proto' |
| 52 | r'|protodevel)', |
alexfh | 1b8fbd8 | 2013-12-16 10:57:30 +0000 | [diff] [blame] | 53 | help='custom pattern selecting file paths to reformat ' |
djasper | 1d353c9 | 2013-12-19 10:21:37 +0000 | [diff] [blame] | 54 | '(case insensitive, overridden by -regex)') |
alexfh | 3929305 | 2013-09-02 16:39:23 +0000 | [diff] [blame] | 55 | parser.add_argument( |
| 56 | '-style', |
| 57 | help= |
| 58 | 'formatting style to apply (LLVM, Google, Chromium, Mozilla, WebKit)') |
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: |
| 65 | match = re.search('^\+\+\+\ (.*?/){%s}(\S*)' % args.p, line) |
| 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 | |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 78 | match = re.search('^@@.*\+(\d+)(,(\d+))?', line) |
| 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 |
| 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. |
| 91 | for filename, lines in lines_by_file.iteritems(): |
alexfh | c770b67 | 2013-10-11 21:32:01 +0000 | [diff] [blame] | 92 | command = [binary, filename] |
| 93 | if args.i: |
| 94 | command.append('-i') |
djasper | 75c3219 | 2013-09-18 12:14:09 +0000 | [diff] [blame] | 95 | command.extend(lines) |
| 96 | if args.style: |
djasper | c49abf9 | 2013-09-21 10:05:02 +0000 | [diff] [blame] | 97 | command.extend(['-style', args.style]) |
djasper | 75c3219 | 2013-09-18 12:14:09 +0000 | [diff] [blame] | 98 | p = subprocess.Popen(command, stdout=subprocess.PIPE, |
alp | 367218e | 2013-12-05 08:14:54 +0000 | [diff] [blame] | 99 | stderr=None, stdin=subprocess.PIPE) |
djasper | 75c3219 | 2013-09-18 12:14:09 +0000 | [diff] [blame] | 100 | stdout, stderr = p.communicate() |
djasper | 60dad2e | 2013-10-08 15:54:36 +0000 | [diff] [blame] | 101 | if p.returncode != 0: |
| 102 | sys.exit(p.returncode); |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 103 | |
alexfh | c770b67 | 2013-10-11 21:32:01 +0000 | [diff] [blame] | 104 | if not args.i: |
| 105 | with open(filename) as f: |
| 106 | code = f.readlines() |
| 107 | formatted_code = StringIO.StringIO(stdout).readlines() |
| 108 | diff = difflib.unified_diff(code, formatted_code, |
| 109 | filename, filename, |
| 110 | '(before formatting)', '(after formatting)') |
| 111 | diff_string = string.join(diff, '') |
| 112 | if len(diff_string) > 0: |
alp | 367218e | 2013-12-05 08:14:54 +0000 | [diff] [blame] | 113 | sys.stdout.write(diff_string) |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 114 | |
| 115 | if __name__ == '__main__': |
| 116 | main() |