Bug 965120 - Git support for mach clang-format; r=gps
authorAnthony Jones <ajones@mozilla.com>
Thu, 30 Jan 2014 14:27:44 +1300
changeset 171968 f99313fc00b5823ce92ba4671727c7209d692389
parent 171967 4a77a99c84d52788958b2647d82b8bd8bc010cdb
child 171969 acdd3135860fe5908d0aa3dd22985d2c5328dfa7
push idunknown
push userunknown
push dateunknown
reviewersgps
bugs965120
milestone29.0a1
Bug 965120 - Git support for mach clang-format; r=gps
tools/mach_commands.py
--- a/tools/mach_commands.py
+++ b/tools/mach_commands.py
@@ -349,23 +349,32 @@ class FormatProvider(MachCommandBase):
             if not clang_format_diff:
                 return 1
 
         except urllib2.HTTPError as e:
             print("HTTP error {0}: {1}".format(e.code, e.reason))
             return 1
 
         from subprocess import Popen, PIPE
-        p1 = Popen(["hg", "diff", "-U0", "-r", "tip^", "--include", "glob:**.c", "--include", "glob:**.cpp",
-                   "--include", "glob:**.h", "--exclude", "listfile:.clang-format-ignore"], stdout=PIPE)
+
+        if os.path.exists(".hg"):
+            diff_process = Popen(["hg", "diff", "-U0", "-r", "tip^",
+                                  "--include", "glob:**.c", "--include", "glob:**.cpp", "--include", "glob:**.h",
+                                  "--exclude", "listfile:.clang-format-ignore"], stdout=PIPE)
+        else:
+            git_process = Popen(["git", "diff", "-U0", "HEAD^"], stdout=PIPE)
+            diff_process = Popen(["filterdiff", "--include=*.h", "--include=*.cpp",
+                                  "--exclude-from-file=.clang-format-ignore"],
+                                 stdin=git_process.stdout, stdout=PIPE)
+
         args = [sys.executable, clang_format_diff, "-p1"]
         if not show:
            args.append("-i")
-        p2 = Popen(args, stdin=p1.stdout)
-        return p2.communicate()[0]
+        cf_process = Popen(args, stdin=diff_process.stdout)
+        return cf_process.communicate()[0]
 
     def locate_or_fetch(self, root):
         target = os.path.join(self._mach_context.state_dir, os.path.basename(root))
         if not os.path.exists(target):
             site = "https://people.mozilla.org/~ajones/clang-format/"
             if self.prompt and raw_input("Download clang-format executables from {0} (yN)? ".format(site)).lower() != 'y':
                 print("Download aborted.")
                 return 1