[clang] 191a395 - [git-clang-format] Add --diffstat parameter

via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 15 01:57:03 PDT 2021


Author: Roland Fischer
Date: 2021-10-15T09:56:51+01:00
New Revision: 191a395343b984d26d058c449cefe1a9ac0ff927

URL: https://github.com/llvm/llvm-project/commit/191a395343b984d26d058c449cefe1a9ac0ff927
DIFF: https://github.com/llvm/llvm-project/commit/191a395343b984d26d058c449cefe1a9ac0ff927.diff

LOG: [git-clang-format] Add --diffstat parameter

[git-clang-format][PR46815] Add diffstat functionality

Adding a --diffstat parameter to git-clang-format that essentially uses git diff --stat, i.e. lists the files needing
formatting. This is useful for CI integration or manual usage where one wants to list the files not properly formatted.

I use it for the Suricata project's github action (CI) integration that verifies proper formatting of a pull request
according to project guidelines where it's very helpful to say which files are not properly formatted. I find the list
of files much more useful than e.g. showing the diff in this case using git-clang-format --diff.

An alternative would be to take an additional parameter to diff, e.g. git-clang-format --diff --stat

The goal is not to provide the whole git diff --stat=... parameter functionality, just plain git diff --stat.

Reviewed By: MyDeveloperDay, JakeMerdichAMD

Differential Revision: https://reviews.llvm.org/D84375

Added: 
    

Modified: 
    clang/tools/clang-format/git-clang-format

Removed: 
    


################################################################################
diff  --git a/clang/tools/clang-format/git-clang-format b/clang/tools/clang-format/git-clang-format
index 30184725e1223..40e848d55a8c5 100755
--- a/clang/tools/clang-format/git-clang-format
+++ b/clang/tools/clang-format/git-clang-format
@@ -99,6 +99,8 @@ def main():
                  help='default commit to use if none is specified'),
   p.add_argument('--
diff ', action='store_true',
                  help='print a 
diff  instead of applying the changes')
+  p.add_argument('--
diff stat', action='store_true',
+                 help='print a 
diff stat instead of applying the changes')
   p.add_argument('--extensions',
                  default=config.get('clangformat.extensions',
                                     default_extensions),
@@ -176,6 +178,8 @@ def main():
       print('clang-format did not modify any files')
   elif opts.
diff :
     print_
diff (old_tree, new_tree)
+  elif opts.
diff stat:
+    print_
diff stat(old_tree, new_tree)
   else:
     changed_files = apply_changes(old_tree, new_tree, force=opts.force,
                                   patch_mode=opts.patch)
@@ -506,6 +510,17 @@ def print_
diff (old_tree, new_tree):
   subprocess.check_call(['git', '
diff ', '--
diff -filter=M', old_tree, new_tree,
                          '--'])
 
+def print_
diff stat(old_tree, new_tree):
+  """Print the 
diff stat between the two trees to stdout."""
+  # We use the porcelain '
diff ' and not plumbing '
diff -tree' because the output
+  # is expected to be viewed by the user, and only the former does nice things
+  # like color and pagination.
+  #
+  # We also only print modified files since `new_tree` only contains the files
+  # that were modified, so unmodified files would show as deleted without the
+  # filter.
+  subprocess.check_call(['git', '
diff ', '--
diff -filter=M', '--stat', old_tree, new_tree,
+                         '--'])
 
 def apply_changes(old_tree, new_tree, force=False, patch_mode=False):
   """Apply the changes in `new_tree` to the working directory.


        


More information about the cfe-commits mailing list