--- 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