blob: 23adb077c95342454248e8705d417144451e89f0 [file] [log] [blame]
Ed Schoutenbf041d92014-09-02 20:59:13 +00001#!/usr/bin/env python
Daniel Jasper9be2c5c2013-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.
Daniel Jasper2e8600c2014-05-14 09:36:11 +000018Example usage for git/svn users:
Daniel Jasper9be2c5c2013-03-20 09:53:23 +000019
Alexander Kornienko95c009a2013-10-11 21:32:01 +000020 git diff -U0 HEAD^ | clang-format-diff.py -p1 -i
Daniel Jasper2e8600c2014-05-14 09:36:11 +000021 svn diff --diff-cmd=diff -x-U0 | clang-format-diff.py -i
Daniel Jasper9be2c5c2013-03-20 09:53:23 +000022
23"""
24
25import argparse
Alexander Kornienko95c009a2013-10-11 21:32:01 +000026import difflib
Daniel Jasper9be2c5c2013-03-20 09:53:23 +000027import re
Alexander Kornienko95c009a2013-10-11 21:32:01 +000028import string
Daniel Jasper9be2c5c2013-03-20 09:53:23 +000029import subprocess
Alexander Kornienko95c009a2013-10-11 21:32:01 +000030import StringIO
Daniel Jasper9be2c5c2013-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
Daniel Jasper9be2c5c2013-03-20 09:53:23 +000038def main():
39 parser = argparse.ArgumentParser(description=
Alexander Kornienko95c009a2013-10-11 21:32:01 +000040 'Reformat changed lines in diff. Without -i '
Alp Toker0e7f6da2013-12-04 00:48:22 +000041 'option just output the diff that would be '
Alexander Kornienko95c009a2013-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')
Alp Toker8c7cbdf2013-12-10 13:51:53 +000045 parser.add_argument('-p', metavar='NUM', default=0,
Daniel Jasper9be2c5c2013-03-20 09:53:23 +000046 help='strip the smallest prefix containing P slashes')
Alp Toker679bf012013-12-18 21:34:07 +000047 parser.add_argument('-regex', metavar='PATTERN', default=None,
Alexander Kornienko25c68382013-12-16 10:57:30 +000048 help='custom pattern selecting file paths to reformat '
Daniel Jasperdb7933a2013-12-19 10:21:37 +000049 '(case sensitive, overrides -iregex)')
Alexander Kornienko25c68382013-12-16 10:57:30 +000050 parser.add_argument('-iregex', metavar='PATTERN', default=
Daniel Jasper21589122014-01-21 15:40:01 +000051 r'.*\.(cpp|cc|c\+\+|cxx|c|cl|h|hpp|m|mm|inc|js|proto'
Daniel Jasper7c627a02014-12-08 19:39:03 +000052 r'|protodevel|java)',
Alexander Kornienko25c68382013-12-16 10:57:30 +000053 help='custom pattern selecting file paths to reformat '
Daniel Jasperdb7933a2013-12-19 10:21:37 +000054 '(case insensitive, overridden by -regex)')
Daniel Jasperc6706882014-11-14 13:27:28 +000055 parser.add_argument('-v', '--verbose', action='store_true',
56 help='be more verbose, ineffective without -i')
Alexander Kornienkoe3648fb2013-09-02 16:39:23 +000057 parser.add_argument(
58 '-style',
59 help=
60 'formatting style to apply (LLVM, Google, Chromium, Mozilla, WebKit)')
Daniel Jasper9be2c5c2013-03-20 09:53:23 +000061 args = parser.parse_args()
62
Daniel Jasperdcab7fb2013-09-18 12:14:09 +000063 # Extract changed lines for each file.
Daniel Jasper9be2c5c2013-03-20 09:53:23 +000064 filename = None
Daniel Jasperdcab7fb2013-09-18 12:14:09 +000065 lines_by_file = {}
Daniel Jasper9be2c5c2013-03-20 09:53:23 +000066 for line in sys.stdin:
67 match = re.search('^\+\+\+\ (.*?/){%s}(\S*)' % args.p, line)
68 if match:
69 filename = match.group(2)
70 if filename == None:
71 continue
72
Alp Toker679bf012013-12-18 21:34:07 +000073 if args.regex is not None:
74 if not re.match('^%s$' % args.regex, filename):
Alexander Kornienko25c68382013-12-16 10:57:30 +000075 continue
76 else:
Alp Toker679bf012013-12-18 21:34:07 +000077 if not re.match('^%s$' % args.iregex, filename, re.IGNORECASE):
Alexander Kornienko25c68382013-12-16 10:57:30 +000078 continue
Daniel Jasperdcab7fb2013-09-18 12:14:09 +000079
Daniel Jasper9be2c5c2013-03-20 09:53:23 +000080 match = re.search('^@@.*\+(\d+)(,(\d+))?', line)
81 if match:
Daniel Jasperdcab7fb2013-09-18 12:14:09 +000082 start_line = int(match.group(1))
Daniel Jasper164c8e12013-10-02 13:59:03 +000083 line_count = 1
Daniel Jasper9be2c5c2013-03-20 09:53:23 +000084 if match.group(3):
Daniel Jasper164c8e12013-10-02 13:59:03 +000085 line_count = int(match.group(3))
86 if line_count == 0:
87 continue
88 end_line = start_line + line_count - 1;
Daniel Jasperdcab7fb2013-09-18 12:14:09 +000089 lines_by_file.setdefault(filename, []).extend(
90 ['-lines', str(start_line) + ':' + str(end_line)])
Daniel Jasper9be2c5c2013-03-20 09:53:23 +000091
Daniel Jasperdcab7fb2013-09-18 12:14:09 +000092 # Reformat files containing changes in place.
93 for filename, lines in lines_by_file.iteritems():
Daniel Jasperc6706882014-11-14 13:27:28 +000094 if args.i and args.verbose:
95 print 'Formatting', filename
Alexander Kornienko95c009a2013-10-11 21:32:01 +000096 command = [binary, filename]
97 if args.i:
98 command.append('-i')
Daniel Jasperdcab7fb2013-09-18 12:14:09 +000099 command.extend(lines)
100 if args.style:
Daniel Jaspercf627f02013-09-21 10:05:02 +0000101 command.extend(['-style', args.style])
Daniel Jasperdcab7fb2013-09-18 12:14:09 +0000102 p = subprocess.Popen(command, stdout=subprocess.PIPE,
Alp Tokerfcf30322013-12-05 08:14:54 +0000103 stderr=None, stdin=subprocess.PIPE)
Daniel Jasperdcab7fb2013-09-18 12:14:09 +0000104 stdout, stderr = p.communicate()
Daniel Jaspere8845ad2013-10-08 15:54:36 +0000105 if p.returncode != 0:
106 sys.exit(p.returncode);
Daniel Jasper9be2c5c2013-03-20 09:53:23 +0000107
Alexander Kornienko95c009a2013-10-11 21:32:01 +0000108 if not args.i:
109 with open(filename) as f:
110 code = f.readlines()
111 formatted_code = StringIO.StringIO(stdout).readlines()
112 diff = difflib.unified_diff(code, formatted_code,
113 filename, filename,
114 '(before formatting)', '(after formatting)')
115 diff_string = string.join(diff, '')
116 if len(diff_string) > 0:
Alp Tokerfcf30322013-12-05 08:14:54 +0000117 sys.stdout.write(diff_string)
Daniel Jasper9be2c5c2013-03-20 09:53:23 +0000118
119if __name__ == '__main__':
120 main()