djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 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. |
| 18 | Example usage for git users: |
| 19 | |
| 20 | git diff -U0 HEAD^ | clang-format-diff.py -p1 |
| 21 | |
| 22 | """ |
| 23 | |
| 24 | import argparse |
| 25 | import re |
| 26 | import subprocess |
| 27 | import sys |
| 28 | |
| 29 | |
| 30 | # Change this to the full path if clang-format is not on the path. |
| 31 | binary = 'clang-format' |
| 32 | |
| 33 | |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 34 | def main(): |
| 35 | parser = argparse.ArgumentParser(description= |
alexfh | 3929305 | 2013-09-02 16:39:23 +0000 | [diff] [blame] | 36 | 'Reformat changed lines in diff.') |
djasper | be05ebc | 2013-05-30 11:50:20 +0000 | [diff] [blame] | 37 | parser.add_argument('-p', default=0, |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 38 | help='strip the smallest prefix containing P slashes') |
alexfh | 3929305 | 2013-09-02 16:39:23 +0000 | [diff] [blame] | 39 | parser.add_argument( |
| 40 | '-style', |
| 41 | help= |
| 42 | 'formatting style to apply (LLVM, Google, Chromium, Mozilla, WebKit)') |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 43 | args = parser.parse_args() |
| 44 | |
djasper | 75c3219 | 2013-09-18 12:14:09 +0000 | [diff] [blame] | 45 | # Extract changed lines for each file. |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 46 | filename = None |
djasper | 75c3219 | 2013-09-18 12:14:09 +0000 | [diff] [blame] | 47 | lines_by_file = {} |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 48 | for line in sys.stdin: |
| 49 | match = re.search('^\+\+\+\ (.*?/){%s}(\S*)' % args.p, line) |
| 50 | if match: |
| 51 | filename = match.group(2) |
| 52 | if filename == None: |
| 53 | continue |
| 54 | |
djasper | 75c3219 | 2013-09-18 12:14:09 +0000 | [diff] [blame] | 55 | # FIXME: Add other types containing C++/ObjC code. |
| 56 | if not (filename.endswith(".cpp") or filename.endswith(".cc") or |
| 57 | filename.endswith(".h")): |
| 58 | continue |
| 59 | |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 60 | match = re.search('^@@.*\+(\d+)(,(\d+))?', line) |
| 61 | if match: |
djasper | 75c3219 | 2013-09-18 12:14:09 +0000 | [diff] [blame] | 62 | start_line = int(match.group(1)) |
djasper | e3eb058 | 2013-10-02 13:59:03 +0000 | [diff] [blame] | 63 | line_count = 1 |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 64 | if match.group(3): |
djasper | e3eb058 | 2013-10-02 13:59:03 +0000 | [diff] [blame] | 65 | line_count = int(match.group(3)) |
| 66 | if line_count == 0: |
| 67 | continue |
| 68 | end_line = start_line + line_count - 1; |
djasper | 75c3219 | 2013-09-18 12:14:09 +0000 | [diff] [blame] | 69 | lines_by_file.setdefault(filename, []).extend( |
| 70 | ['-lines', str(start_line) + ':' + str(end_line)]) |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 71 | |
djasper | 75c3219 | 2013-09-18 12:14:09 +0000 | [diff] [blame] | 72 | # Reformat files containing changes in place. |
| 73 | for filename, lines in lines_by_file.iteritems(): |
| 74 | command = [binary, '-i', filename] |
| 75 | command.extend(lines) |
| 76 | if args.style: |
djasper | c49abf9 | 2013-09-21 10:05:02 +0000 | [diff] [blame] | 77 | command.extend(['-style', args.style]) |
djasper | 75c3219 | 2013-09-18 12:14:09 +0000 | [diff] [blame] | 78 | p = subprocess.Popen(command, stdout=subprocess.PIPE, |
| 79 | stderr=subprocess.PIPE, |
| 80 | stdin=subprocess.PIPE) |
| 81 | stdout, stderr = p.communicate() |
| 82 | if stderr: |
| 83 | print stderr |
djasper | 60dad2e | 2013-10-08 15:54:36 +0000 | [diff] [blame^] | 84 | if p.returncode != 0: |
| 85 | sys.exit(p.returncode); |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 86 | |
| 87 | |
| 88 | if __name__ == '__main__': |
| 89 | main() |