[PATCH v2] userdiff: funcname and word patterns for sh

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Add regexp based on the "Shell Command Language" specifications.
Because of the lax syntax of sh, some corner cases may not be
handled properly.

Signed-off-by: Adrien Schildknecht <adrien+dev@xxxxxxxxxxx>
---
 Documentation/gitattributes.txt |  2 ++
 t/t4018-diff-funcname.sh        |  1 +
 t/t4018/sh-function             |  4 ++++
 t/t4018/sh-function-comment     |  6 ++++++
 t/t4018/sh-function-nested      |  7 +++++++
 t/t4018/sh-function-prefix      |  4 ++++
 t/t4018/sh-function-string      |  4 ++++
 t/t4034-diff-words.sh           |  1 +
 t/t4034/sh/expect               | 46 +++++++++++++++++++++++++++++++++++++++++
 t/t4034/sh/post                 | 36 ++++++++++++++++++++++++++++++++
 t/t4034/sh/pre                  | 36 ++++++++++++++++++++++++++++++++
 userdiff.c                      |  7 +++++++
 12 files changed, 154 insertions(+)
 create mode 100644 t/t4018/sh-function
 create mode 100644 t/t4018/sh-function-comment
 create mode 100644 t/t4018/sh-function-nested
 create mode 100644 t/t4018/sh-function-prefix
 create mode 100644 t/t4018/sh-function-string
 create mode 100644 t/t4034/sh/expect
 create mode 100644 t/t4034/sh/post
 create mode 100644 t/t4034/sh/pre

diff --git a/Documentation/gitattributes.txt b/Documentation/gitattributes.txt
index c892ffa..fb13761 100644
--- a/Documentation/gitattributes.txt
+++ b/Documentation/gitattributes.txt
@@ -545,6 +545,8 @@ patterns are available:
 
 - `ruby` suitable for source code in the Ruby language.
 
+- `sh` suitable for source code in the Shell language.
+
 - `tex` suitable for source code for LaTeX documents.
 
 
diff --git a/t/t4018-diff-funcname.sh b/t/t4018-diff-funcname.sh
index 1dbaa38..960b6e4 100755
--- a/t/t4018-diff-funcname.sh
+++ b/t/t4018-diff-funcname.sh
@@ -40,6 +40,7 @@ diffpatterns="
 	php
 	python
 	ruby
+	sh
 	tex
 	custom1
 	custom2
diff --git a/t/t4018/sh-function b/t/t4018/sh-function
new file mode 100644
index 0000000..1d5096a
--- /dev/null
+++ b/t/t4018/sh-function
@@ -0,0 +1,4 @@
+RIGHT_foo()
+{
+	ChangeMe;
+}
diff --git a/t/t4018/sh-function-comment b/t/t4018/sh-function-comment
new file mode 100644
index 0000000..f340ee7
--- /dev/null
+++ b/t/t4018/sh-function-comment
@@ -0,0 +1,6 @@
+  RIGHT_foo() # echo 'foo'
+#bar()
+{
+	#bar()
+	ChangeMe;
+}
diff --git a/t/t4018/sh-function-nested b/t/t4018/sh-function-nested
new file mode 100644
index 0000000..d3263f3
--- /dev/null
+++ b/t/t4018/sh-function-nested
@@ -0,0 +1,7 @@
+foo (     )
+{
+	RIGHT_bar()
+	{
+		echo ChangeMe
+	}
+}
diff --git a/t/t4018/sh-function-prefix b/t/t4018/sh-function-prefix
new file mode 100644
index 0000000..e96eb7a
--- /dev/null
+++ b/t/t4018/sh-function-prefix
@@ -0,0 +1,4 @@
+function RIGHT_foo()
+{
+	ChangeMe;
+}
diff --git a/t/t4018/sh-function-string b/t/t4018/sh-function-string
new file mode 100644
index 0000000..f60a899
--- /dev/null
+++ b/t/t4018/sh-function-string
@@ -0,0 +1,4 @@
+RIGHT_foo() {
+	echo "bar()"
+	ChangeMe;
+}
diff --git a/t/t4034-diff-words.sh b/t/t4034-diff-words.sh
index f2f55fc..24fd9aa 100755
--- a/t/t4034-diff-words.sh
+++ b/t/t4034-diff-words.sh
@@ -312,6 +312,7 @@ test_language_driver perl
 test_language_driver php
 test_language_driver python
 test_language_driver ruby
+test_language_driver sh
 test_language_driver tex
 
 test_expect_success 'word-diff with diff.sbe' '
diff --git a/t/t4034/sh/expect b/t/t4034/sh/expect
new file mode 100644
index 0000000..1c6de2d
--- /dev/null
+++ b/t/t4034/sh/expect
@@ -0,0 +1,46 @@
+<BOLD>diff --git a/pre b/post<RESET>
+<BOLD>index 20229c3..ab791db 100644<RESET>
+<BOLD>--- a/pre<RESET>
+<BOLD>+++ b/post<RESET>
+<CYAN>@@ -1,36 +1,36 @@<RESET>
+foo() {ls&echo}<RESET>
+$((<RED>a<RESET><GREEN>x<RESET>++))
+$((<RED>a<RESET><GREEN>x<RESET>--))
+$((--<RED>a<RESET><GREEN>x<RESET>))
+$((++<RED>a<RESET><GREEN>x<RESET>))
+$((<RED>a<RESET><GREEN>x<RESET>*<RED>b<RESET><GREEN>y<RESET>))
+$((<RED>a<RESET><GREEN>x<RESET>&<RED>b<RESET><GREEN>y<RESET>))
+$((<RED>a<RESET><GREEN>x<RESET>**<RED>b<RESET><GREEN>y<RESET>))
+$((<RED>a<RESET><GREEN>x<RESET>/<RED>b<RESET><GREEN>y<RESET>))
+$((<RED>a<RESET><GREEN>x<RESET>%<RED>b<RESET><GREEN>y<RESET>))
+$((<RED>a<RESET><GREEN>x<RESET>+<RED>b<RESET><GREEN>y<RESET>))
+$((<RED>a<RESET><GREEN>x<RESET>-<RED>b<RESET><GREEN>y<RESET>))
+[ <RED>a<RESET><GREEN>x<RESET><=<RED>b<RESET><GREEN>y<RESET> ]
+[ <RED>a<RESET><GREEN>x<RESET>>=<RED>b<RESET><GREEN>y<RESET> ]
+[ <RED>a<RESET><GREEN>x<RESET>==<RED>b<RESET><GREEN>y<RESET> ]
+[ <RED>a<RESET><GREEN>x<RESET>!=<RED>b<RESET><GREEN>y<RESET> ]
+<RED>a<RESET><GREEN>x<RESET><<<RED>b a<RESET><GREEN>y x<RESET>>><RED>b a<RESET><GREEN>y x<RESET><<-<RED>b a<RESET><GREEN>y x<RESET><<RED>b a<RESET><GREEN>y x<RESET>><RED>b a<RESET><GREEN>y x<RESET>>|<RED>b a<RESET><GREEN>y x<RESET><&<RED>b a<RESET><GREEN>y x<RESET>>&<RED>b a<RESET><GREEN>y x<RESET><><RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>&<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>&&<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>|<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>||<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>=<RED>b<RESET><GREEN>y<RESET>
+$((<RED>a<RESET><GREEN>x<RESET>+=<RED>b<RESET><GREEN>y<RESET>))
+$((<RED>a<RESET><GREEN>x<RESET>-=<RED>b<RESET><GREEN>y<RESET>))
+$((<RED>a<RESET><GREEN>x<RESET>*=<RED>b<RESET><GREEN>y<RESET>))
+$((<RED>a<RESET><GREEN>x<RESET>/=<RED>b<RESET><GREEN>y<RESET>))
+$((<RED>a<RESET><GREEN>x<RESET>%=<RED>b<RESET><GREEN>y<RESET>))
+$((<RED>a<RESET><GREEN>x<RESET><<=<RED>b<RESET><GREEN>y<RESET>))
+$((<RED>a<RESET><GREEN>x<RESET>>>=<RED>b<RESET><GREEN>y<RESET>))
+$((<RED>a<RESET><GREEN>x<RESET>&=<RED>b<RESET><GREEN>y<RESET>))
+$((<RED>a<RESET><GREEN>x<RESET>^=<RED>b<RESET><GREEN>y<RESET>))
+$((<RED>a<RESET><GREEN>x<RESET>|=<RED>b<RESET><GREEN>y<RESET>))
+*)<RED>a<RESET><GREEN>b<RESET>;;ls
+FOO=42&<RED>foo<RESET><GREEN>fro<RESET>
+BAR=$(0)<RESET>
+[[ $<RED>a<RESET><GREEN>x<RESET> -eq $<RED>b<RESET><GREEN>y<RESET> ]]
diff --git a/t/t4034/sh/post b/t/t4034/sh/post
new file mode 100644
index 0000000..ab791db
--- /dev/null
+++ b/t/t4034/sh/post
@@ -0,0 +1,36 @@
+foo() {ls&echo}
+$((x++))
+$((x--))
+$((--x))
+$((++x))
+$((x*y))
+$((x&y))
+$((x**y))
+$((x/y))
+$((x%y))
+$((x+y))
+$((x-y))
+[ x<=y ]
+[ x>=y ]
+[ x==y ]
+[ x!=y ]
+x<<y x>>y x<<-y x<y x>y x>|y x<&y x>&y x<>y
+x&y
+x&&y
+x|y
+x||y
+x=y
+$((x+=y))
+$((x-=y))
+$((x*=y))
+$((x/=y))
+$((x%=y))
+$((x<<=y))
+$((x>>=y))
+$((x&=y))
+$((x^=y))
+$((x|=y))
+*)b;;ls
+FOO=42&fro
+BAR=$(0)
+[[ $x -eq $y ]]
diff --git a/t/t4034/sh/pre b/t/t4034/sh/pre
new file mode 100644
index 0000000..20229c3
--- /dev/null
+++ b/t/t4034/sh/pre
@@ -0,0 +1,36 @@
+foo() {ls&echo}
+$((a++))
+$((a--))
+$((--a))
+$((++a))
+$((a*b))
+$((a&b))
+$((a**b))
+$((a/b))
+$((a%b))
+$((a+b))
+$((a-b))
+[ a<=b ]
+[ a>=b ]
+[ a==b ]
+[ a!=b ]
+a<<b a>>b a<<-b a<b a>b a>|b a<&b a>&b a<>b
+a&b
+a&&b
+a|b
+a||b
+a=b
+$((a+=b))
+$((a-=b))
+$((a*=b))
+$((a/=b))
+$((a%=b))
+$((a<<=b))
+$((a>>=b))
+$((a&=b))
+$((a^=b))
+$((a|=b))
+*)a;;ls
+FOO=42&foo
+BAR=$(0)
+[[ $a -eq $b ]]
diff --git a/userdiff.c b/userdiff.c
index 2ccbee5..40b3dc0 100644
--- a/userdiff.c
+++ b/userdiff.c
@@ -146,6 +146,13 @@ PATTERNS("csharp",
 	 "[a-zA-Z_][a-zA-Z0-9_]*"
 	 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
 	 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
+PATTERNS("sh",
+	"^([ \t]*(function[ \t]+)?[a-zA-Z_][a-zA-Z0-9_]*[ \t]*\\([ \t]*\\).*)$",
+	/* -- */
+	 "[a-zA-Z0-9_]+"
+	 "|[-+0-9]+"
+	 "|[-+*/<>%&^|=!]=|>>=?|<<=?|\\+\\+|--|\\*\\*|&&|\\|\\||\\[\\[|\\]\\]"
+	 "|>\\||[<>]+&|<>|<<-|;;"),
 { "default", NULL, -1, { NULL, 0 } },
 };
 #undef PATTERNS
-- 
Adrien Schildknecht
http://schischi.me

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]