{"thread":{"id":"56261","subject":"[PATCH v5] userdiff: improve java hunk header regex","startedAt":"2021-08-11T14:53:54Z","lastAt":"2021-08-11T19:05:21Z","messageCount":5,"participants":["Tassilo Horn","Junio C Hamano"],"isPatch":true,"patchVersion":5,"patchTotal":null},"messages":[{"id":"432485","messageId":"20210811145341.43429-1-tsdh@gnu.org","threadId":"56261","inReplyTo":null,"subject":"[PATCH v5] userdiff: improve java hunk header regex","fromName":"Tassilo Horn","fromEmail":"tsdh@gnu.org","sentAt":"2021-08-11T14:53:41Z","receivedAt":"2021-08-11T14:53:54Z","isPatch":true,"sender":{"key":"tsdh@gnu.org","avatar":"https://avatars.githubusercontent.com/u/103854?v=4"},"body":"Currently, the git diff hunk headers show the wrong method signature if the\nmethod has a qualified return type, an array return type, or a generic return\ntype because the regex doesn't allow dots (.), [], or < and > in the return\ntype.  Also, type parameter declarations couldn't be matched.\n\nAdd several t4018 tests asserting the right hunk headers for different cases:\n\n  - enum constant change\n  - change in generic method with bounded type parameters\n  - change in generic method with wildcard\n  - field change in a nested class\n\nSigned-off-by: Tassilo Horn <tsdh@gnu.org>\n---\n t/t4018/java-class-member-function          | 6 +++++-\n t/t4018/java-enum-constant                  | 6 ++++++\n t/t4018/java-method-return-generic-bounded  | 9 +++++++++\n t/t4018/java-method-return-generic-wildcard | 9 +++++++++\n t/t4018/java-nested-field                   | 6 ++++++\n userdiff.c                                  | 6 +++++-\n 6 files changed, 40 insertions(+), 2 deletions(-)\n create mode 100644 t/t4018/java-enum-constant\n create mode 100644 t/t4018/java-method-return-generic-bounded\n create mode 100644 t/t4018/java-method-return-generic-wildcard\n create mode 100644 t/t4018/java-nested-field\n\ndiff --git a/t/t4018/java-class-member-function b/t/t4018/java-class-member-function\nindex 298bc7a71b..a8d7850412 100644\n--- a/t/t4018/java-class-member-function\n+++ b/t/t4018/java-class-member-function\n@@ -3,6 +3,10 @@ public class Beer\n \tint special;\n \tpublic static void main(String RIGHT[])\n \t{\n-\t\tSystem.out.print(\"ChangeMe\");\n+            someMethodCall();\n+            someOtherMethod(\"17\")\n+                .doThat();\n+            // Whatever\n+            System.out.print(\"ChangeMe\");\n \t}\n }\ndiff --git a/t/t4018/java-enum-constant b/t/t4018/java-enum-constant\nnew file mode 100644\nindex 0000000000..a1931c8379\n--- /dev/null\n+++ b/t/t4018/java-enum-constant\n@@ -0,0 +1,6 @@\n+private enum RIGHT {\n+    ONE,\n+    TWO,\n+    THREE,\n+    ChangeMe\n+}\ndiff --git a/t/t4018/java-method-return-generic-bounded b/t/t4018/java-method-return-generic-bounded\nnew file mode 100644\nindex 0000000000..66dd78c379\n--- /dev/null\n+++ b/t/t4018/java-method-return-generic-bounded\n@@ -0,0 +1,9 @@\n+class MyExample {\n+    public <T extends Bar & Foo<T>, R> Map<T, R[]> foo(String[] RIGHT) {\n+        someMethodCall();\n+        someOtherMethod()\n+            .doThat();\n+        // Whatever...\n+        return (List<T>) Arrays.asList(\"ChangeMe\");\n+    }\n+}\ndiff --git a/t/t4018/java-method-return-generic-wildcard b/t/t4018/java-method-return-generic-wildcard\nnew file mode 100644\nindex 0000000000..96e9e5f2c1\n--- /dev/null\n+++ b/t/t4018/java-method-return-generic-wildcard\n@@ -0,0 +1,9 @@\n+class MyExample {\n+    public List<? extends Comparable> foo(String[] RIGHT) {\n+        someMethodCall();\n+        someOtherMethod()\n+            .doThat();\n+        // Whatever...\n+        return Arrays.asList(\"ChangeMe\");\n+    }\n+}\ndiff --git a/t/t4018/java-nested-field b/t/t4018/java-nested-field\nnew file mode 100644\nindex 0000000000..d92d3ec688\n--- /dev/null\n+++ b/t/t4018/java-nested-field\n@@ -0,0 +1,6 @@\n+class MyExample {\n+    private static class RIGHT {\n+        // change an inner class field\n+        String inner = \"ChangeMe\";\n+    }\n+}\ndiff --git a/userdiff.c b/userdiff.c\nindex 3c3bbe38b0..6644931ce1 100644\n--- a/userdiff.c\n+++ b/userdiff.c\n@@ -142,7 +142,11 @@ PATTERNS(\"html\",\n \t \"[^<>= \\t]+\"),\n PATTERNS(\"java\",\n \t \"!^[ \\t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\\n\"\n-\t \"^[ \\t]*(([A-Za-z_][A-Za-z_0-9]*[ \\t]+)+[A-Za-z_][A-Za-z_0-9]*[ \\t]*\\\\([^;]*)$\",\n+         /* Class, enum, and interface declarations */\n+         \"^[ \\t]*(([a-z]+[ \\t]+)*(class|enum|interface)[ \\t]+[A-Za-z][A-Za-z0-9_$]*[ \\t]+.*)$\\n\"\n+         /* Method definitions; note that constructor signatures are not */\n+         /* matched because they are indistinguishable from method calls. */\n+         \"^[ \\t]*(([A-Za-z_<>&][][?&<>.,A-Za-z_0-9]*[ \\t]+)+[A-Za-z_][A-Za-z_0-9]*[ \\t]*\\\\([^;]*)$\",\n \t /* -- */\n \t \"[a-zA-Z_][a-zA-Z0-9_]*\"\n \t \"|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?\"\n-- \n2.32.0\n\n"},{"id":"432495","messageId":"xmqqa6lnaola.fsf@gitster.g","threadId":"56261","inReplyTo":"20210811145341.43429-1-tsdh@gnu.org","subject":"Re: [PATCH v5] userdiff: improve java hunk header regex","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2021-08-11T17:16:49Z","receivedAt":"2021-08-11T17:16:53Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Just a few whitespace nits that \"git am\" noticed for me.\n\nTassilo Horn <tsdh@gnu.org> writes:\n\n> diff --git a/t/t4018/java-class-member-function b/t/t4018/java-class-member-function\n> index 298bc7a71b..a8d7850412 100644\n> --- a/t/t4018/java-class-member-function\n> +++ b/t/t4018/java-class-member-function\n> @@ -3,6 +3,10 @@ public class Beer\n>  \tint special;\n>  \tpublic static void main(String RIGHT[])\n>  \t{\n> -\t\tSystem.out.print(\"ChangeMe\");\n> +            someMethodCall();\n> +            someOtherMethod(\"17\")\n> +                .doThat();\n> +            // Whatever\n> +            System.out.print(\"ChangeMe\");\n\nI notice that the original used HT (horizontal tab) to indent, but\nthe new one uses runs of SP (space).  This project has no written\npreference for coding style for Java, which means it would have been\nmore appreciated if the original style were kept.\n\n> diff --git a/t/t4018/java-enum-constant b/t/t4018/java-enum-constant\n> new file mode 100644\n> index 0000000000..a1931c8379\n> --- /dev/null\n> +++ b/t/t4018/java-enum-constant\n> @@ -0,0 +1,6 @@\n> +private enum RIGHT {\n> +    ONE,\n> +    TWO,\n> +    THREE,\n> +    ChangeMe\n> +}\n\nFor these new tests, you'd be the one setting what styles to use ;-)\nAfter all, we serve users from projects with different style, and\nhaving variety in our test patterns is not bad.\n\n> diff --git a/userdiff.c b/userdiff.c\n> index 3c3bbe38b0..6644931ce1 100644\n> --- a/userdiff.c\n> +++ b/userdiff.c\n> @@ -142,7 +142,11 @@ PATTERNS(\"html\",\n>  \t \"[^<>= \\t]+\"),\n>  PATTERNS(\"java\",\n>  \t \"!^[ \\t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\\n\"\n> -\t \"^[ \\t]*(([A-Za-z_][A-Za-z_0-9]*[ \\t]+)+[A-Za-z_][A-Za-z_0-9]*[ \\t]*\\\\([^;]*)$\",\n> +         /* Class, enum, and interface declarations */\n> +         \"^[ \\t]*(([a-z]+[ \\t]+)*(class|enum|interface)[ \\t]+[A-Za-z][A-Za-z0-9_$]*[ \\t]+.*)$\\n\"\n> +         /* Method definitions; note that constructor signatures are not */\n> +         /* matched because they are indistinguishable from method calls. */\n> +         \"^[ \\t]*(([A-Za-z_<>&][][?&<>.,A-Za-z_0-9]*[ \\t]+)+[A-Za-z_][A-Za-z_0-9]*[ \\t]*\\\\([^;]*)$\",\n>  \t /* -- */\n>  \t \"[a-zA-Z_][a-zA-Z0-9_]*\"\n>  \t \"|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?\"\n\nThis hunk does violate project convention that our codebase uses\nleading HT to indent (and align with extra SPs if needed).\n\n"},{"id":"432500","messageId":"87im0b6f92.fsf@gnu.org","threadId":"56261","inReplyTo":"xmqqa6lnaola.fsf@gitster.g","subject":"Re: [PATCH v5] userdiff: improve java hunk header regex","fromName":"Tassilo Horn","fromEmail":"tsdh@gnu.org","sentAt":"2021-08-11T17:42:59Z","receivedAt":"2021-08-11T17:52:15Z","isPatch":true,"sender":{"key":"tsdh@gnu.org","avatar":"https://avatars.githubusercontent.com/u/103854?v=4"},"body":"Junio C Hamano <gitster@pobox.com> writes:\n\n> Just a few whitespace nits that \"git am\" noticed for me.\n\nAh, indeed.  Should all be fixed in v6.\n\n>> diff --git a/t/t4018/java-class-member-function b/t/t4018/java-class-member-function\n>> index 298bc7a71b..a8d7850412 100644\n>> --- a/t/t4018/java-class-member-function\n>> +++ b/t/t4018/java-class-member-function\n>> @@ -3,6 +3,10 @@ public class Beer\n>>  \tint special;\n>>  \tpublic static void main(String RIGHT[])\n>>  \t{\n>> -\t\tSystem.out.print(\"ChangeMe\");\n>> +            someMethodCall();\n>> +            someOtherMethod(\"17\")\n>> +                .doThat();\n>> +            // Whatever\n>> +            System.out.print(\"ChangeMe\");\n>\n> I notice that the original used HT (horizontal tab) to indent, but\n> the new one uses runs of SP (space).  This project has no written\n> preference for coding style for Java, which means it would have been\n> more appreciated if the original style were kept.\n\nFixed in v6.\n\n>> diff --git a/t/t4018/java-enum-constant b/t/t4018/java-enum-constant\n>> new file mode 100644\n>> index 0000000000..a1931c8379\n>> --- /dev/null\n>> +++ b/t/t4018/java-enum-constant\n>> @@ -0,0 +1,6 @@\n>> +private enum RIGHT {\n>> +    ONE,\n>> +    TWO,\n>> +    THREE,\n>> +    ChangeMe\n>> +}\n>\n> For these new tests, you'd be the one setting what styles to use ;-)\n> After all, we serve users from projects with different style, and\n> having variety in our test patterns is not bad.\n\nI completely agree.\n\n>> diff --git a/userdiff.c b/userdiff.c\n>> index 3c3bbe38b0..6644931ce1 100644\n>> --- a/userdiff.c\n>> +++ b/userdiff.c\n>> @@ -142,7 +142,11 @@ PATTERNS(\"html\",\n>>  \t \"[^<>= \\t]+\"),\n>>  PATTERNS(\"java\",\n>>  \t \"!^[ \\t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\\n\"\n>> -\t \"^[ \\t]*(([A-Za-z_][A-Za-z_0-9]*[ \\t]+)+[A-Za-z_][A-Za-z_0-9]*[ \\t]*\\\\([^;]*)$\",\n>> +         /* Class, enum, and interface declarations */\n>> +         \"^[ \\t]*(([a-z]+[ \\t]+)*(class|enum|interface)[ \\t]+[A-Za-z][A-Za-z0-9_$]*[ \\t]+.*)$\\n\"\n>> +         /* Method definitions; note that constructor signatures are not */\n>> +         /* matched because they are indistinguishable from method calls. */\n>> +         \"^[ \\t]*(([A-Za-z_<>&][][?&<>.,A-Za-z_0-9]*[ \\t]+)+[A-Za-z_][A-Za-z_0-9]*[ \\t]*\\\\([^;]*)$\",\n>>  \t /* -- */\n>>  \t \"[a-zA-Z_][a-zA-Z0-9_]*\"\n>>  \t \"|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?\"\n>\n> This hunk does violate project convention that our codebase uses\n> leading HT to indent (and align with extra SPs if needed).\n\nAlso fixed in v6.\n\nThat leads to the question if you'd welcome a patch adding a\n.dir-locals.el to the repository with the right settings so that it'll\njust work for contributors using the One True Editor.  Would you?\n\nBye,\nTassilo\n"},{"id":"432503","messageId":"xmqqo8a397ib.fsf@gitster.g","threadId":"56261","inReplyTo":"87im0b6f92.fsf@gnu.org","subject":"Re: [PATCH v5] userdiff: improve java hunk header regex","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2021-08-11T18:11:08Z","receivedAt":"2021-08-11T18:11:12Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Tassilo Horn <tsdh@gnu.org> writes:\n\n> That leads to the question if you'd welcome a patch adding a\n> .dir-locals.el to the repository with the right settings so that it'll\n> just work for contributors using the One True Editor.  Would you?\n\nIf your one true editor honored what is in .editorconfig, perhaps\nthere was no need for us to be having this conversation?  I dunno.\n\nOur .gitattributes (the top one and also in subdirectories) try to\ngive reasonable whitespace rules for files, and specifically, files\nin t/t????/ directories that are used as test vectors completely\ndisable whitespace checking, while everywhere else we insist certain\nstyles in the top-level .gitattributes file.\n\nThanks.\n\n"},{"id":"432505","messageId":"87fsvf6bva.fsf@gnu.org","threadId":"56261","inReplyTo":"xmqqo8a397ib.fsf@gitster.g","subject":"Re: [PATCH v5] userdiff: improve java hunk header regex","fromName":"Tassilo Horn","fromEmail":"tsdh@gnu.org","sentAt":"2021-08-11T18:55:23Z","receivedAt":"2021-08-11T19:05:21Z","isPatch":true,"sender":{"key":"tsdh@gnu.org","avatar":"https://avatars.githubusercontent.com/u/103854?v=4"},"body":"Junio C Hamano <gitster@pobox.com> writes:\n\n>> That leads to the question if you'd welcome a patch adding a\n>> .dir-locals.el to the repository with the right settings so that\n>> it'll just work for contributors using the One True Editor.  Would\n>> you?\n>\n> If your one true editor honored what is in .editorconfig, perhaps\n> there was no need for us to be having this conversation?  I dunno.\n\nAh, I'm sorry, I wasn't aware of that.  I've installed the editorconfig\nEmacs plugin [1] which seems to do the trick.\n\nBye,\nTassilo\n\n[1] https://github.com/editorconfig/editorconfig-emacs\n"}]}