.git-hooks/pre-commit |   53 +++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 48 insertions(+), 5 deletions(-)

New commits:
commit c819aa06069ec3ea4f2f51d26a77f455740b828f
Author:     Stephan Bergmann <[email protected]>
AuthorDate: Mon Nov 7 19:59:01 2022 +0100
Commit:     Stephan Bergmann <[email protected]>
CommitDate: Fri Nov 11 17:18:43 2022 +0100

    .git-hook: Emit some warning about clang-format and renamed files
    
    ...to avoid misguided clang-format'ing of previously excluded files, as
    discussed in the comment at
    
<https://gerrit.libreoffice.org/c/core/+/142387/4#message-ce27921261661fe7488ef0564657dbb5b42fb5fa>
    "sc: factor out common code in make files".
    
    (Though this still doesn't warn about cases where some excluded file got 
renamed
    and the excludelist wasn't updated and the user already erroneously
    clang-format'ed the renamed file before this commit attempt.   Also, I don't
    know how best to integrate this with libreoffice.autostyle, so just ignore
    libreoffice.autostyle for now when any suspicious renames are detected.)
    
    Change-Id: I8d176ce536548b67f5b2af100f579f362764b06b
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/142394
    Tested-by: Jenkins
    Reviewed-by: Stephan Bergmann <[email protected]>

diff --git a/.git-hooks/pre-commit b/.git-hooks/pre-commit
index a3cef10ee5f4..51c1de751672 100755
--- a/.git-hooks/pre-commit
+++ b/.git-hooks/pre-commit
@@ -184,6 +184,7 @@ sub check_style($)
     my ($h) = @_;
     my $src = ClangFormat::get_extension_regex();
     my @bad_names = ();
+    my @bad_renames = ();
     my $clang_format = ClangFormat::find();
 
     ## Check if ClangFormat has get_excludelist or the old
@@ -193,6 +194,17 @@ sub check_style($)
     if ($@) { $excluded_list_names = ClangFormat::get_blacklist(); }
     else    { $excluded_list_names = ClangFormat::get_excludelist(); }
 
+    # Get a list of renamed files.
+    my %renames; # key is target pathname, value is source pathname
+    open (IN, "git diff-index --cached --find-renames --diff-filter=R 
--name-status $h |")
+        || die "Cannot run git diff.";
+    while (my $line = <IN>)
+    {
+        chomp $line;
+        $line =~ /^[^\t]+\t([^\t]+)\t([^\t]+)$/ ||  die "Unexpected response 
line: $line";
+        $renames{$2} = $1;
+    }
+
     # Get a list of non-deleted changed files.
     open (FILES, "git diff-index --cached --diff-filter=AM --name-only $h |") 
||  die "Cannot run git diff.";
     while (my $filename = <FILES>)
@@ -233,21 +245,52 @@ sub check_style($)
             }
             if (!ClangFormat::check_style($clang_format, $filename))
             {
-                push @bad_names, $filename;
+                if (defined($renames{$filename}))
+                {
+                    push @bad_renames, $filename;
+                }
+                else
+                {
+                    push @bad_names, $filename;
+                }
             }
         }
     }
 
     # Enforce style.
-    if (scalar @bad_names)
+    if (scalar @bad_names || scalar @bad_renames)
     {
         my $autostyle = `git config libreoffice.autostyle`;
         chomp $autostyle;
-        if ($autostyle ne "true")
+        if ($autostyle ne "true" or scalar @bad_renames)
         {
             print("\nThe above differences were found between the code to 
commit \n");
-            print("and the clang-format rules. You can apply these changes 
with:\n");
-            print("\n$clang_format -i " . join(" ", @bad_names) . "\n\n");
+            print("and the clang-format rules.\n");
+            if (scalar @bad_names)
+            {
+                print("You can apply these changes with:\n");
+                print("\n$clang_format -i " . join(" ", @bad_names) . "\n\n");
+            }
+            if (scalar @bad_renames)
+            {
+                print("\nATTENTION: Of files detected as renamed by git, the 
following ones are\n");
+                print("not clang-format'ed and are not listed in the 
excludelist. If they are\n");
+                print("renames of previously excluded files, they should be 
added to the\n");
+                print("excludelist:\n\n");
+                foreach my $name (@bad_renames)
+                {
+                    if (exists($excluded_list_names->{$renames{$name}}))
+                    {
+                        print("* $name got renamed from $renames{$name},\n");
+                        print("    which is even still listed in the 
excludelist!\n");
+                    }
+                    else
+                    {
+                        print("* $name\n");
+                    }
+                }
+                print("\n");
+            }
             print("Aborting commit. Apply changes and commit again or skip 
checking\n");
             print("with --no-verify (not recommended).\n");
             exit(1);

Reply via email to