blob: 122db49ff73c764f2cb89f774d74b14344b84c1d [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#
chandlerc079ee0b2019-01-19 08:50:56 +00005# 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
djasper7f663602013-03-20 09:53:23 +00008#
9#===------------------------------------------------------------------------===#
10
nico97599942019-07-23 17:34:18 +000011"""
djasper7f663602013-03-20 09:53:23 +000012This script reads input from a unified diff and reformats all the changed
13lines. This is useful to reformat all the lines touched by a specific patch.
djasper78395352014-05-14 09:36:11 +000014Example usage for git/svn users:
djasper7f663602013-03-20 09:53:23 +000015
sylvestread8850a2016-12-03 23:22:45 +000016 git diff -U0 --no-color HEAD^ | clang-format-diff.py -p1 -i
djasper78395352014-05-14 09:36:11 +000017 svn diff --diff-cmd=diff -x-U0 | clang-format-diff.py -i
djasper7f663602013-03-20 09:53:23 +000018
19"""
serge_sans_paille05613342018-12-18 16:07:37 +000020from __future__ import absolute_import, division, print_function
djasper7f663602013-03-20 09:53:23 +000021
22import argparse
alexfhc770b672013-10-11 21:32:01 +000023import difflib
djasper7f663602013-03-20 09:53:23 +000024import re
25import subprocess
26import sys
serge_sans_paille2757fe92019-01-03 14:26:56 +000027
28if sys.version_info.major >= 3:
29 from io import StringIO
30else:
31 from io import BytesIO as StringIO
djasper7f663602013-03-20 09:53:23 +000032
33
djasper7f663602013-03-20 09:53:23 +000034def main():
nico97599942019-07-23 17:34:18 +000035 parser = argparse.ArgumentParser(description=__doc__,
36 formatter_class=
37 argparse.RawDescriptionHelpFormatter)
alexfhc770b672013-10-11 21:32:01 +000038 parser.add_argument('-i', action='store_true', default=False,
39 help='apply edits to files instead of displaying a diff')
alpcfbd31f2013-12-10 13:51:53 +000040 parser.add_argument('-p', metavar='NUM', default=0,
djasper7f663602013-03-20 09:53:23 +000041 help='strip the smallest prefix containing P slashes')
alp51f6d9a2013-12-18 21:34:07 +000042 parser.add_argument('-regex', metavar='PATTERN', default=None,
alexfh1b8fbd82013-12-16 10:57:30 +000043 help='custom pattern selecting file paths to reformat '
djasper1d353c92013-12-19 10:21:37 +000044 '(case sensitive, overrides -iregex)')
alexfh1b8fbd82013-12-16 10:57:30 +000045 parser.add_argument('-iregex', metavar='PATTERN', default=
paulhoad8b4035c2019-09-24 14:00:06 +000046 r'.*\.(cpp|cc|c\+\+|cxx|c|cl|h|hh|hpp|m|mm|inc|js|ts|proto'
47 r'|protodevel|java|cs)',
alexfh1b8fbd82013-12-16 10:57:30 +000048 help='custom pattern selecting file paths to reformat '
djasper1d353c92013-12-19 10:21:37 +000049 '(case insensitive, overridden by -regex)')
djasperea5da0e2015-10-07 17:00:20 +000050 parser.add_argument('-sort-includes', action='store_true', default=False,
51 help='let clang-format sort include blocks')
djasperceb88712014-11-14 13:27:28 +000052 parser.add_argument('-v', '--verbose', action='store_true',
53 help='be more verbose, ineffective without -i')
djaspera72164d2016-01-20 18:55:57 +000054 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')
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:
serge_sans_paillea3caf452019-02-11 15:03:17 +000065 match = re.search(r'^\+\+\+\ (.*?/){%s}(\S*)' % args.p, line)
djasper7f663602013-03-20 09:53:23 +000066 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
serge_sans_paillea3caf452019-02-11 15:03:17 +000078 match = re.search(r'^@@.*\+(\d+)(,(\d+))?', line)
djasper7f663602013-03-20 09:53:23 +000079 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
krasimire863ca52018-08-03 10:04:58 +000086 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.
krasimire863ca52018-08-03 10:04:58 +000091 for filename, lines in lines_by_file.items():
djasperceb88712014-11-14 13:27:28 +000092 if args.i and args.verbose:
krasimire863ca52018-08-03 10:04:58 +000093 print('Formatting {}'.format(filename))
djaspera72164d2016-01-20 18:55:57 +000094 command = [args.binary, filename]
alexfhc770b672013-10-11 21:32:01 +000095 if args.i:
96 command.append('-i')
djasperea5da0e2015-10-07 17:00:20 +000097 if args.sort_includes:
98 command.append('-sort-includes')
djasper75c32192013-09-18 12:14:09 +000099 command.extend(lines)
100 if args.style:
djasperc49abf92013-09-21 10:05:02 +0000101 command.extend(['-style', args.style])
krasimire863ca52018-08-03 10:04:58 +0000102 p = subprocess.Popen(command,
103 stdout=subprocess.PIPE,
104 stderr=None,
105 stdin=subprocess.PIPE,
106 universal_newlines=True)
djasper75c32192013-09-18 12:14:09 +0000107 stdout, stderr = p.communicate()
djasper60dad2e2013-10-08 15:54:36 +0000108 if p.returncode != 0:
krasimire863ca52018-08-03 10:04:58 +0000109 sys.exit(p.returncode)
djasper7f663602013-03-20 09:53:23 +0000110
alexfhc770b672013-10-11 21:32:01 +0000111 if not args.i:
112 with open(filename) as f:
113 code = f.readlines()
krasimire863ca52018-08-03 10:04:58 +0000114 formatted_code = StringIO(stdout).readlines()
alexfhc770b672013-10-11 21:32:01 +0000115 diff = difflib.unified_diff(code, formatted_code,
116 filename, filename,
117 '(before formatting)', '(after formatting)')
krasimire863ca52018-08-03 10:04:58 +0000118 diff_string = ''.join(diff)
alexfhc770b672013-10-11 21:32:01 +0000119 if len(diff_string) > 0:
alp367218e2013-12-05 08:14:54 +0000120 sys.stdout.write(diff_string)
djasper7f663602013-03-20 09:53:23 +0000121
122if __name__ == '__main__':
123 main()