blob: b896ffce932bd036c7fe24f636d763507fa0bd88 [file] [log] [blame]
ed509e8b92014-09-02 20:59:13 +00001#!/usr/bin/env python
djasper7f663602013-03-20 09:53:23 +00002#
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
12r"""
13ClangFormat Diff Reformatter
14============================
15
16This script reads input from a unified diff and reformats all the changed
17lines. This is useful to reformat all the lines touched by a specific patch.
djasper78395352014-05-14 09:36:11 +000018Example usage for git/svn users:
djasper7f663602013-03-20 09:53:23 +000019
alexfhc770b672013-10-11 21:32:01 +000020 git diff -U0 HEAD^ | clang-format-diff.py -p1 -i
djasper78395352014-05-14 09:36:11 +000021 svn diff --diff-cmd=diff -x-U0 | clang-format-diff.py -i
djasper7f663602013-03-20 09:53:23 +000022
23"""
24
25import argparse
alexfhc770b672013-10-11 21:32:01 +000026import difflib
djasper7f663602013-03-20 09:53:23 +000027import re
alexfhc770b672013-10-11 21:32:01 +000028import string
djasper7f663602013-03-20 09:53:23 +000029import subprocess
alexfhc770b672013-10-11 21:32:01 +000030import StringIO
djasper7f663602013-03-20 09:53:23 +000031import sys
32
33
34# Change this to the full path if clang-format is not on the path.
35binary = 'clang-format'
36
37
djasper7f663602013-03-20 09:53:23 +000038def main():
39 parser = argparse.ArgumentParser(description=
alexfhc770b672013-10-11 21:32:01 +000040 'Reformat changed lines in diff. Without -i '
alp9b6f1cb2013-12-04 00:48:22 +000041 'option just output the diff that would be '
alexfhc770b672013-10-11 21:32:01 +000042 'introduced.')
43 parser.add_argument('-i', action='store_true', default=False,
44 help='apply edits to files instead of displaying a diff')
alpcfbd31f2013-12-10 13:51:53 +000045 parser.add_argument('-p', metavar='NUM', default=0,
djasper7f663602013-03-20 09:53:23 +000046 help='strip the smallest prefix containing P slashes')
alp51f6d9a2013-12-18 21:34:07 +000047 parser.add_argument('-regex', metavar='PATTERN', default=None,
alexfh1b8fbd82013-12-16 10:57:30 +000048 help='custom pattern selecting file paths to reformat '
djasper1d353c92013-12-19 10:21:37 +000049 '(case sensitive, overrides -iregex)')
alexfh1b8fbd82013-12-16 10:57:30 +000050 parser.add_argument('-iregex', metavar='PATTERN', default=
djasper3bbc9642014-01-21 15:40:01 +000051 r'.*\.(cpp|cc|c\+\+|cxx|c|cl|h|hpp|m|mm|inc|js|proto'
52 r'|protodevel)',
alexfh1b8fbd82013-12-16 10:57:30 +000053 help='custom pattern selecting file paths to reformat '
djasper1d353c92013-12-19 10:21:37 +000054 '(case insensitive, overridden by -regex)')
alexfh39293052013-09-02 16:39:23 +000055 parser.add_argument(
56 '-style',
57 help=
58 'formatting style to apply (LLVM, Google, Chromium, Mozilla, WebKit)')
djasper7f663602013-03-20 09:53:23 +000059 args = parser.parse_args()
60
djasper75c32192013-09-18 12:14:09 +000061 # Extract changed lines for each file.
djasper7f663602013-03-20 09:53:23 +000062 filename = None
djasper75c32192013-09-18 12:14:09 +000063 lines_by_file = {}
djasper7f663602013-03-20 09:53:23 +000064 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
alp51f6d9a2013-12-18 21:34:07 +000071 if args.regex is not None:
72 if not re.match('^%s$' % args.regex, filename):
alexfh1b8fbd82013-12-16 10:57:30 +000073 continue
74 else:
alp51f6d9a2013-12-18 21:34:07 +000075 if not re.match('^%s$' % args.iregex, filename, re.IGNORECASE):
alexfh1b8fbd82013-12-16 10:57:30 +000076 continue
djasper75c32192013-09-18 12:14:09 +000077
djasper7f663602013-03-20 09:53:23 +000078 match = re.search('^@@.*\+(\d+)(,(\d+))?', line)
79 if match:
djasper75c32192013-09-18 12:14:09 +000080 start_line = int(match.group(1))
djaspere3eb0582013-10-02 13:59:03 +000081 line_count = 1
djasper7f663602013-03-20 09:53:23 +000082 if match.group(3):
djaspere3eb0582013-10-02 13:59:03 +000083 line_count = int(match.group(3))
84 if line_count == 0:
85 continue
86 end_line = start_line + line_count - 1;
djasper75c32192013-09-18 12:14:09 +000087 lines_by_file.setdefault(filename, []).extend(
88 ['-lines', str(start_line) + ':' + str(end_line)])
djasper7f663602013-03-20 09:53:23 +000089
djasper75c32192013-09-18 12:14:09 +000090 # Reformat files containing changes in place.
91 for filename, lines in lines_by_file.iteritems():
alexfhc770b672013-10-11 21:32:01 +000092 command = [binary, filename]
93 if args.i:
94 command.append('-i')
djasper75c32192013-09-18 12:14:09 +000095 command.extend(lines)
96 if args.style:
djasperc49abf92013-09-21 10:05:02 +000097 command.extend(['-style', args.style])
djasper75c32192013-09-18 12:14:09 +000098 p = subprocess.Popen(command, stdout=subprocess.PIPE,
alp367218e2013-12-05 08:14:54 +000099 stderr=None, stdin=subprocess.PIPE)
djasper75c32192013-09-18 12:14:09 +0000100 stdout, stderr = p.communicate()
djasper60dad2e2013-10-08 15:54:36 +0000101 if p.returncode != 0:
102 sys.exit(p.returncode);
djasper7f663602013-03-20 09:53:23 +0000103
alexfhc770b672013-10-11 21:32:01 +0000104 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:
alp367218e2013-12-05 08:14:54 +0000113 sys.stdout.write(diff_string)
djasper7f663602013-03-20 09:53:23 +0000114
115if __name__ == '__main__':
116 main()