summaryrefslogtreecommitdiff
path: root/tools/clang-format/clang-format-diff.py
diff options
context:
space:
mode:
authorAlexander Kornienko <alexfh@google.com>2013-10-11 21:32:01 +0000
committerAlexander Kornienko <alexfh@google.com>2013-10-11 21:32:01 +0000
commitcf207a62a96adb6bbf99f5e9497da992cd76a8ff (patch)
treec9bf3b0c777b462a7663cfc694b960d7e7665c24 /tools/clang-format/clang-format-diff.py
parentae76f7f850a9101a20191b10241ca72c23dc40dd (diff)
downloadclang-cf207a62a96adb6bbf99f5e9497da992cd76a8ff.tar.gz
clang-cf207a62a96adb6bbf99f5e9497da992cd76a8ff.tar.bz2
clang-cf207a62a96adb6bbf99f5e9497da992cd76a8ff.tar.xz
Changed clang-format-diff.py to output diff by default. Added -i option to apply changes to files instead.
Summary: "svn diff|clang-format-diff.py" will just output the diff. Now it's possible to use: svn diff|clang-format-diff.py|patch -p0 as an equivalent to: svn diff|clang-format-diff.py -i ;) Reviewers: djasper Reviewed By: djasper CC: cfe-commits Differential Revision: http://llvm-reviews.chandlerc.com/D1840 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@192505 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'tools/clang-format/clang-format-diff.py')
-rwxr-xr-xtools/clang-format/clang-format-diff.py25
1 files changed, 22 insertions, 3 deletions
diff --git a/tools/clang-format/clang-format-diff.py b/tools/clang-format/clang-format-diff.py
index ca949b84ef..60b8fb730d 100755
--- a/tools/clang-format/clang-format-diff.py
+++ b/tools/clang-format/clang-format-diff.py
@@ -17,13 +17,16 @@ This script reads input from a unified diff and reformats all the changed
lines. This is useful to reformat all the lines touched by a specific patch.
Example usage for git users:
- git diff -U0 HEAD^ | clang-format-diff.py -p1
+ git diff -U0 HEAD^ | clang-format-diff.py -p1 -i
"""
import argparse
+import difflib
import re
+import string
import subprocess
+import StringIO
import sys
@@ -33,7 +36,11 @@ binary = 'clang-format'
def main():
parser = argparse.ArgumentParser(description=
- 'Reformat changed lines in diff.')
+ 'Reformat changed lines in diff. Without -i '
+ 'option just output the diff that would be'
+ 'introduced.')
+ parser.add_argument('-i', action='store_true', default=False,
+ help='apply edits to files instead of displaying a diff')
parser.add_argument('-p', default=0,
help='strip the smallest prefix containing P slashes')
parser.add_argument(
@@ -71,7 +78,9 @@ def main():
# Reformat files containing changes in place.
for filename, lines in lines_by_file.iteritems():
- command = [binary, '-i', filename]
+ command = [binary, filename]
+ if args.i:
+ command.append('-i')
command.extend(lines)
if args.style:
command.extend(['-style', args.style])
@@ -84,6 +93,16 @@ def main():
if p.returncode != 0:
sys.exit(p.returncode);
+ if not args.i:
+ with open(filename) as f:
+ code = f.readlines()
+ formatted_code = StringIO.StringIO(stdout).readlines()
+ diff = difflib.unified_diff(code, formatted_code,
+ filename, filename,
+ '(before formatting)', '(after formatting)')
+ diff_string = string.join(diff, '')
+ if len(diff_string) > 0:
+ print diff_string
if __name__ == '__main__':
main()