Bug 1392012 - ./clang-format with git should exclude files from .clang-format-ignore r?sylvestre draft
authorXavier ALT <dex@phoenix-ind.net>
Thu, 24 Aug 2017 23:41:44 +0200
changeset 652460 727e6e1c368d
parent 652459 7013321d361c
child 728098 867bd5a86f65
push id76067
push userbmo:dex@phoenix-ind.net
push dateThu, 24 Aug 2017 21:43:18 +0000
reviewerssylvestre
bugs1392012
milestone57.0a1
Bug 1392012 - ./clang-format with git should exclude files from .clang-format-ignore r?sylvestre MozReview-Commit-ID: KsgPnN9aDTC
tools/mach_commands.py
--- a/tools/mach_commands.py
+++ b/tools/mach_commands.py
@@ -266,16 +266,30 @@ class FormatProvider(MachCommandBase):
             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)]
+            # git-diff doesn't support an 'exclude-from-files' param, but
+            # allow to add individual exclude pattern since v1.9, see
+            # https://git-scm.com/docs/gitglossary#gitglossary-aiddefpathspecapathspec
+            with open(self._format_ignore_file, 'rb') as exclude_pattern_file:
+                for pattern in exclude_pattern_file.readlines():
+                    pattern = pattern.rstrip()
+                    pattern = pattern.replace('.*', '**')
+                    if not pattern or pattern.startswith('#'):
+                        continue  # empty or comment
+                    magics = ['exclude']
+                    if pattern.startswith('^'):
+                        magics += ['top']
+                        pattern = pattern[1:]
+                    args += [':({0}){1}'.format(','.join(magics), pattern)]
         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
 
         diff_process = Popen(self._get_clang_format_diff_command(), stdout=PIPE)