Skip to contentSkip to conversation

Minor userdiff stuff

PATCH In review Giuseppe Bilotta 3 participants 9 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

cover Minor userdiff stuff

The first commit introduces diff patterns for POSIX shells.

The second defines diff types for C, Perl and shell scripts in git.git

Giuseppe Bilotta (2):
Diff patterns for POSIX shells
Use specific diff rules for repo files

 .gitattributes |    5 +++--
 userdiff.c     |    3 +++
 2 files changed, 6 insertions(+), 2 deletions(-)
Signature · 1 line
1.7.6.451.gcb935.dirty
#1 lore raw

PATCH 1/2 Diff patterns for POSIX shells

All diffs following a function definition will have that function name as chunck header, but this is the best we can do with the current userdiff capabilities.

---
 userdiff.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
userdiff.c +3 -0
index 01d3a8b..70120c3 100644
@@ -107,6 +107,9 @@
 	 "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*"
 	 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?."
 	 "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"),
+PATTERNS("shell", "^[ \t]*([a-zA-Z_0-9]+)[ \t]*\\(\\).*",
+	 /* -- */
+	 "(--|\\$)?[a-zA-Z_0-9]+|&&|\\|\\|"),
 PATTERNS("bibtex", "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$",
 	 "[={}\"]|[^={}\" \t]+"),
 PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
Signature · 1 line
1.7.6.451.gcb935.dirty
#8 lore raw

PATCH 2/2 Use specific diff rules for repo files

In reply to Giuseppe Bilotta

Let's eat our own dogfood. (Also, this makes word diff much nicer on git's own repo.)

---
 .gitattributes |    5 +++--
 1 files changed, 3 insertions(+), 2 deletions(-)
.gitattributes +3 -2
index 5e98806..16930e6 100644
@@ -1,3 +1,4 @@
 * whitespace=!indent,trail,space
-*.[ch] whitespace=indent,trail,space
-*.sh whitespace=indent,trail,space
+*.[ch] whitespace=indent,trail,space diff=cpp
+*.sh whitespace=indent,trail,space diff=shell
+*.perl diff=perl
Signature · 1 line
1.7.6.451.gcb935.dirty
#2 lore raw

Re: [PATCH 1/2] Diff patterns for POSIX shells

In reply to Giuseppe Bilotta
Quoted 14 lines · Giuseppe Bilotta

All diffs following a function definition will have that function name as chunck header, but this is the best we can do with the current userdiff capabilities.

---
 userdiff.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
userdiff.c +0 -0
index 01d3a8b..70120c3 100644

userdiff.c line 112

Quoted 6 lines
userdiff.c +3 -0
@@ -107,6 +107,9 @@
 	 "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*"
 	 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?."
 	 "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"),
+PATTERNS("shell", "^[ \t]*([a-zA-Z_0-9]+)[ \t]*\\(\\).*",
+	 /* -- */
+	 "(--|\\$)?[a-zA-Z_0-9]+|&&|\\|\\|"),

Hmm, what is this "double-dash -- might be present before a name" about?

Quoted 3 lines
userdiff.c +0 -0
 PATTERNS("bibtex", "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$",
 	 "[={}\"]|[^={}\" \t]+"),
 PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
#3 lore raw

PATCH 1/1 Diff patterns for POSIX shells

All diffs following a function definition will have that function name as chunck header, but this is the best we can do with the current userdiff capabilities.

---
 userdiff.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
userdiff.c +3 -0
index 01d3a8b..94b1c47 100644
@@ -107,6 +107,9 @@
 	 "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*"
 	 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?."
 	 "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"),
+PATTERNS("shell", "^[ \t]*([a-zA-Z_0-9]+)[ \t]*\\(\\).*",
+	 /* -- */
+	 "\\$?[a-zA-Z_0-9]+|&&|\\|\\|"),
 PATTERNS("bibtex", "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$",
 	 "[={}\"]|[^={}\" \t]+"),
 PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
Signature · 1 line
1.7.6.485.gd947c.dirty
#4 lore raw

Re: [PATCH 1bis/2] Diff patterns for POSIX shells

Quoted 3 lines · Giuseppe Bilotta

All diffs following a function definition will have that function name as chunck header, but this is the best we can do with the current userdiff capabilities.

Curious as to how this would look in git.git, I tried "git log -p" before and after your patches, and diffed the result. I noticed two things:

1. Given a block of shell code like this:

foo() {
... do something ...
}

test_expect_success 'test foo' '
... the actual test ...
'

if we add new code after the test, the old regex would print:

@@ -1,2 +3,4 @@ test_expect_success 'test foo' '

and now we say:

@@ -1,2 +3,4 @@ foo

which seems more misleading. I know the function-matching code has no way to say "look for ^}, which signals end of function", so we can't be entirely accurate. But I wonder if the new heuristic (which seems to look for a name followed by parentheses) is actually any better than the old.

2. What would have printed before:

@@ -1,2 +3,4 @@ foo() {

now prints

@@ -1,2 +3,4 @@ foo

without the parentheses or brace. It looks like the similar C one keeps the parentheses, at least. I find that a bit more readable, as it is more clear that the line indicates a function, and not simply some top-level command.

-Peff

#5 lore raw

Re: [PATCH 1bis/2] Diff patterns for POSIX shells

Quoted 26 lines · Jeff King
Quoted 3 lines · Giuseppe Bilotta

All diffs following a function definition will have that function name as chunck header, but this is the best we can do with the current userdiff capabilities.

Curious as to how this would look in git.git, I tried "git log -p" before and after your patches, and diffed the result. I noticed two things:

 1. Given a block of shell code like this:

       foo() {
         ... do something ...
       }

       test_expect_success 'test foo' '
         ... the actual test ...
       '

    if we add new code after the test, the old regex would print:

       @@ -1,2 +3,4 @@ test_expect_success 'test foo' '

    and now we say:

Quoted 7 lines

       @@ -1,2 +3,4 @@ foo

    which seems more misleading. I know the function-matching code has no way to say "look for ^}, which signals end of function", so we can't be entirely accurate. But I wonder if the new heuristic (which seems to look for a name followed by parentheses) is actually any better than the old.

I'm not too satisfied with the solution either. I've been thinking about adding some important keywords such as for, while, if, until, case etc, but decided it would be too much overkill. And, it still woudln't work 'correctly' in a case such as this one you presented above.

Quoted 4 lines

 2. What would have printed before:

      @@ -1,2 +3,4 @@ foo() {

Quoted 8 lines

    now prints

      @@ -1,2 +3,4 @@ foo

    without the parentheses or brace. It looks like the similar C one keeps the parentheses, at least. I find that a bit more readable, as it is more clear that the line indicates a function, and not simply some top-level command.

Indeed. I'll change the regexp to include the parenthesis.

Signature · 1 line
Giuseppe "Oblomov" Bilotta
#7 lore raw

Re: [PATCH 1/2] Diff patterns for POSIX shells

In reply to Junio C Hamano
Quoted 11 lines · Junio C Hamano
Quoted 9 lines · Giuseppe Bilotta

All diffs following a function definition will have that function name as chunck header, but this is the best we can do with the current userdiff capabilities.

---
 userdiff.c |    3 +++

 1 files changed, 3 insertions(+), 0 deletions(-)
Quoted 14 lines
Quoted 11 lines
userdiff.c +0 -0
index 01d3a8b..70120c3 100644
@@ -107,6 +107,9 @@

       "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*" "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?." "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"), +PATTERNS("shell", "^[ \t]*([a-zA-Z_0-9]+)[ \t]*\\(\\).*",
+      /* -- */
+      "(--|\\$)?[a-zA-Z_0-9]+|&&|\\|\\|"),

Hmm, what is this "double-dash -- might be present before a name" about?

Now that's a good question. I think it was a brainfart while testing the regexp; came across a case switch where the candidate were options, and somehow decided that it was better to include the --. I'll prepare a patch without this stupidity.

Signature · 1 line
Giuseppe "Oblomov" Bilotta
#6 lore raw

Re: [PATCH 1bis/2] Diff patterns for POSIX shells

In reply to Jeff King
Quoted 9 lines · Jeff King
Quoted 3 lines · Giuseppe Bilotta

All diffs following a function definition will have that function name as chunck header, but this is the best we can do with the current userdiff capabilities.

Curious as to how this would look in git.git, I tried "git log -p" before and after your patches, and diffed the result. I noticed two things:

Hmm, sounds like a regression without much upside to me. Thanks for looking.