{"thread":{"id":"56242","subject":"[PATCH] userdiff: improve java regex for generic return types","startedAt":"2021-08-09T20:34:10Z","lastAt":"2021-08-10T05:13:52Z","messageCount":3,"participants":["Tassilo Horn","Jeff King"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"432332","messageId":"20210809203308.30128-1-tsdh@gnu.org","threadId":"56242","inReplyTo":null,"subject":"[PATCH] userdiff: improve java regex for generic return types","fromName":"Tassilo Horn","fromEmail":"tsdh@gnu.org","sentAt":"2021-08-09T20:33:08Z","receivedAt":"2021-08-09T20:34:10Z","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 generic return type because the regex doesn't allow < and > in the\nreturn type.  This patch adds those.\n\nBug repro: In a repository with .gitattributes containing \"*.java diff=java\"\nand a java file\n\n--8<---------------cut here---------------start------------->8---\nclass MyExample {\n    public void firstMethod() {\n        // Whatever...\n    }\n\n    public List<Integer> secondMethod() {\n        // Here is some comment,\n        // and here is more,\n        // and here is even more,\n        // followed by this,\n        // and that,\n        // and even more.\n        return Arrays.asList(1, 2, 3, 4, 5);\n    }\n}\n--8<---------------cut here---------------end--------------->8---\n\nwhen adding the number 6 to the Arrays.asList() call in secondMethod(), the git\ndiff one gets is\n\n--8<---------------cut here---------------start------------->8---\ndiff --git a/src/main/java/MyExample.java b/src/main/java/MyExample.java\nindex a0f1a6b..ea37a98 100644\n--- a/src/main/java/MyExample.java\n+++ b/src/main/java/MyExample.java\n@@ -14,6 +14,6 @@ public void firstMethod() {\n         // followed by this,\n         // and that,\n         // and even more.\n-        return Arrays.asList(1, 2, 3, 4, 5);\n+        return Arrays.asList(1, 2, 3, 4, 5, 6);\n     }\n }\n--8<---------------cut here---------------end--------------->8---\n\nwhere the hunk header shows the signature of firstMethod whereas it should show\nthe signature of secondMethod.\n\nSigned-off-by: Tassilo Horn <tsdh@gnu.org>\n---\n userdiff.c | 2 +-\n 1 file changed, 1 insertion(+), 1 deletion(-)\n\ndiff --git a/userdiff.c b/userdiff.c\nindex 3c3bbe38b0..a244ad7ab1 100644\n--- a/userdiff.c\n+++ b/userdiff.c\n@@ -142,7 +142,7 @@ 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+\t \"^[ \\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":"432333","messageId":"YRGXhqW0BmKQdeZu@coredump.intra.peff.net","threadId":"56242","inReplyTo":"20210809203308.30128-1-tsdh@gnu.org","subject":"Re: [PATCH] userdiff: improve java regex for generic return types","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2021-08-09T21:00:54Z","receivedAt":"2021-08-09T21:00:56Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Mon, Aug 09, 2021 at 10:33:08PM +0200, Tassilo Horn wrote:\n\n> Currently, the git diff hunk headers show the wrong method signature if the\n> method has a generic return type because the regex doesn't allow < and > in the\n> return type.  This patch adds those.\n\nThanks. It has been long enough since I used Java that there were no\ngenerics back then, but I will take your word that this is what they\nlook like. ;)\n\nThe patch itself looks OK to me, but...\n\n> when adding the number 6 to the Arrays.asList() call in secondMethod(), the git\n> diff one gets is\n> \n> --8<---------------cut here---------------start------------->8---\n> diff --git a/src/main/java/MyExample.java b/src/main/java/MyExample.java\n> index a0f1a6b..ea37a98 100644\n> --- a/src/main/java/MyExample.java\n> +++ b/src/main/java/MyExample.java\n> @@ -14,6 +14,6 @@ public void firstMethod() {\n>          // followed by this,\n>          // and that,\n>          // and even more.\n> -        return Arrays.asList(1, 2, 3, 4, 5);\n> +        return Arrays.asList(1, 2, 3, 4, 5, 6);\n>      }\n>  }\n> --8<---------------cut here---------------end--------------->8---\n\n...this diff in the commit message will screw up git-am. The usual\nprocedure is to omit the scissors lines and just indent it.\n\nBut...\n\n>  userdiff.c | 2 +-\n>  1 file changed, 1 insertion(+), 1 deletion(-)\n\n...even better than explaining it is adding a regression test, perhaps\nlike this one:\n\ndiff --git a/t/t4018/java-return-generic b/t/t4018/java-return-generic\nnew file mode 100644\nindex 0000000000..d030c26184\n--- /dev/null\n+++ b/t/t4018/java-return-generic\n@@ -0,0 +1,10 @@\n+class MyExample {\n+    public void firstMethod() {\n+        // Whatever...\n+    }\n+\n+    public List<String> secondMethod(String RIGHT[]) {\n+        // Whatever...\n+        return Arrays.asList(\"ChangeMe\");\n+    }\n+}\n\n(the \"ChangeMe\" line will be changed, and we'll expect that the line\nwith \"RIGHT\" on it is found. We use -U1 to reduce the need for filler).\n\n-Peff\n"},{"id":"432349","messageId":"87bl65napl.fsf@gnu.org","threadId":"56242","inReplyTo":"YRGXhqW0BmKQdeZu@coredump.intra.peff.net","subject":"Re: [PATCH] userdiff: improve java regex for generic return types","fromName":"Tassilo Horn","fromEmail":"tsdh@gnu.org","sentAt":"2021-08-10T04:51:12Z","receivedAt":"2021-08-10T05:13:52Z","isPatch":true,"sender":{"key":"tsdh@gnu.org","avatar":"https://avatars.githubusercontent.com/u/103854?v=4"},"body":"Jeff King <peff@peff.net> writes:\n\nHi Jeff,\n\n> The patch itself looks OK to me, but...\n\nthanks for the detailed comments.  I'll submit a new version of the\npatch soon.\n\nBye,\nTassilo\n"}]}