Bug 1389956 - extract clang format diff command generation into its own method r=sylvestre
authorXavier ALT <dex@phoenix-ind.net>
Wed, 23 Aug 2017 00:24:23 +0200
changeset 377189 6803e175c0f3
parent 377188 b3005c73d7ad
child 377190 6683845d49b5
push id49779
push usersledru@mozilla.com
push dateMon, 28 Aug 2017 07:49:24 +0000
treeherderautoland@6803e175c0f3 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssylvestre
bugs1389956
milestone57.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1389956 - extract clang format diff command generation into its own method r=sylvestre MozReview-Commit-ID: 8kbZcfMBe4J
tools/mach_commands.py
--- a/tools/mach_commands.py
+++ b/tools/mach_commands.py
@@ -251,47 +251,57 @@ class FormatProvider(MachCommandBase):
                 return 1
             with open(temp, "wb") as fh:
                 fh.write(data)
                 fh.close()
             os.chmod(temp, os.stat(temp).st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
             os.rename(temp, target)
         return target
 
+    # List of file extension to consider (should start with dot)
+    _format_include_extensions = ('.cpp', '.c', '.h')
+    # File contaning all paths to exclude from formatting
+    _format_ignore_file = '.clang-format-ignore'
+
+    def _get_clang_format_diff_command(self):
+        if self.repository.name == 'hg':
+            args = ["hg", "diff", "-U0", "-r" ".^"]
+            for dot_extension in self._format_include_extensions:
+                args += ['--include', 'glob:**{0}'.format(dot_extension)]
+            args += ['--exclude', 'listfile:{0}'.format(self._format_ignore_file)]
+        else:
+            args = ["git", "diff", "--no-color", "-U0", "HEAD", "--"]
+            for dot_extension in self._format_include_extensions:
+                args += ['*{0}'.format(dot_extension)]
+        return args
+
     def run_clang_format_diff(self, clang_format_diff, show):
         # Run clang-format on the diff
         # Note that this will potentially miss a lot things
         from subprocess import Popen, PIPE
 
-        if self.repository.name == 'hg':
-            diff_process = Popen(["hg", "diff", "-U0", "-r", ".^",
-                                  "--include", "glob:**.c", "--include", "glob:**.cpp",
-                                  "--include", "glob:**.h",
-                                  "--exclude", "listfile:.clang-format-ignore"], stdout=PIPE)
-        else:
-	    diff_process = Popen(["git", "diff", "--no-color", "-U0", "HEAD","--","*.c","*.cpp","*.h"], stdout=PIPE)
+        diff_process = Popen(self._get_clang_format_diff_command(), stdout=PIPE)
         args = [sys.executable, clang_format_diff, "-p1"]
         if not show:
             args.append("-i")
         cf_process = Popen(args, stdin=diff_process.stdout)
         return cf_process.communicate()[0]
 
     def generate_path_list(self, paths):
-        pathToThirdparty = os.path.join(self.topsrcdir,
-                                        ".clang-format-ignore")
+        pathToThirdparty = os.path.join(self.topsrcdir, self._format_ignore_file)
         ignored_dir = []
         for line in open(pathToThirdparty):
             # Remove comments and empty lines
             if line.startswith('#') or len(line.strip()) == 0:
                 continue
             ignored_dir.append(line.rstrip())
 
         # Generates the list of regexp
         ignored_dir_re = '(%s)' % '|'.join(ignored_dir)
-        extensions = ('.cpp', '.c', '.h')
+        extensions = self._format_include_extensions
 
         path_list = []
         for f in paths:
             if re.match(ignored_dir_re, f):
                 # Early exit if we have provided an ignored directory
                 print("clang-format: Ignored third party code '{0}'".format(f))
                 continue