|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +#===- google-java-format-diff.py - google-java-format Diff Reformatter -----===# |
| 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 | +google-java-format 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/svn users: |
| 19 | +
|
| 20 | + git diff -U0 HEAD^ | google-java-format-diff.py -p1 -i |
| 21 | + svn diff --diff-cmd=diff -x-U0 | google-java-format-diff.py -i |
| 22 | +
|
| 23 | +""" |
| 24 | + |
| 25 | +import argparse |
| 26 | +import difflib |
| 27 | +import re |
| 28 | +import string |
| 29 | +import subprocess |
| 30 | +import StringIO |
| 31 | +import sys |
| 32 | + |
| 33 | +binary = '/usr/bin/google-java-format' |
| 34 | + |
| 35 | +def main(): |
| 36 | + parser = argparse.ArgumentParser(description= |
| 37 | + 'Reformat changed lines in diff. Without -i ' |
| 38 | + 'option just output the diff that would be ' |
| 39 | + 'introduced.') |
| 40 | + parser.add_argument('-i', action='store_true', default=False, |
| 41 | + help='apply edits to files instead of displaying a diff') |
| 42 | + |
| 43 | + parser.add_argument('-p', metavar='NUM', default=0, |
| 44 | + help='strip the smallest prefix containing P slashes') |
| 45 | + parser.add_argument('-regex', metavar='PATTERN', default=None, |
| 46 | + help='custom pattern selecting file paths to reformat ' |
| 47 | + '(case sensitive, overrides -iregex)') |
| 48 | + parser.add_argument('-iregex', metavar='PATTERN', default=r'.*\.java', |
| 49 | + help='custom pattern selecting file paths to reformat ' |
| 50 | + '(case insensitive, overridden by -regex)') |
| 51 | + parser.add_argument('-v', '--verbose', action='store_true', |
| 52 | + help='be more verbose, ineffective without -i') |
| 53 | + args = parser.parse_args() |
| 54 | + |
| 55 | + # Extract changed lines for each file. |
| 56 | + filename = None |
| 57 | + lines_by_file = {} |
| 58 | + |
| 59 | + for line in sys.stdin: |
| 60 | + match = re.search('^\+\+\+\ (.*?/){%s}(\S*)' % args.p, line) |
| 61 | + if match: |
| 62 | + filename = match.group(2) |
| 63 | + if filename == None: |
| 64 | + continue |
| 65 | + |
| 66 | + if args.regex is not None: |
| 67 | + if not re.match('^%s$' % args.regex, filename): |
| 68 | + continue |
| 69 | + else: |
| 70 | + if not re.match('^%s$' % args.iregex, filename, re.IGNORECASE): |
| 71 | + continue |
| 72 | + |
| 73 | + match = re.search('^@@.*\+(\d+)(,(\d+))?', line) |
| 74 | + if match: |
| 75 | + start_line = int(match.group(1)) |
| 76 | + line_count = 1 |
| 77 | + if match.group(3): |
| 78 | + line_count = int(match.group(3)) |
| 79 | + if line_count == 0: |
| 80 | + continue |
| 81 | + end_line = start_line + line_count - 1; |
| 82 | + lines_by_file.setdefault(filename, []).extend( |
| 83 | + ['-lines', str(start_line) + ':' + str(end_line)]) |
| 84 | + |
| 85 | + # Reformat files containing changes in place. |
| 86 | + for filename, lines in lines_by_file.iteritems(): |
| 87 | + if args.i and args.verbose: |
| 88 | + print 'Formatting', filename |
| 89 | + command = [binary] |
| 90 | + if args.i: |
| 91 | + command.append('-i') |
| 92 | + command.extend(lines) |
| 93 | + command.append(filename) |
| 94 | + p = subprocess.Popen(command, stdout=subprocess.PIPE, |
| 95 | + stderr=None, stdin=subprocess.PIPE) |
| 96 | + stdout, stderr = p.communicate() |
| 97 | + if p.returncode != 0: |
| 98 | + sys.exit(p.returncode); |
| 99 | + |
| 100 | + if not args.i: |
| 101 | + with open(filename) as f: |
| 102 | + code = f.readlines() |
| 103 | + formatted_code = StringIO.StringIO(stdout).readlines() |
| 104 | + diff = difflib.unified_diff(code, formatted_code, |
| 105 | + filename, filename, |
| 106 | + '(before formatting)', '(after formatting)') |
| 107 | + diff_string = string.join(diff, '') |
| 108 | + if len(diff_string) > 0: |
| 109 | + sys.stdout.write(diff_string) |
| 110 | + |
| 111 | +if __name__ == '__main__': |
| 112 | + main() |
0 commit comments