]> git.ipfire.org Git - thirdparty/git.git/commitdiff
userdiff: support Bash
authorVictor Engmark <victor@engmark.name>
Wed, 21 Oct 2020 23:45:08 +0000 (12:45 +1300)
committerJunio C Hamano <gitster@pobox.com>
Thu, 22 Oct 2020 17:29:30 +0000 (10:29 -0700)
Support POSIX, bashism and mixed function declarations, all four
compound command types, trailing comments and mixed whitespace.

Even though Bash allows locale-dependent characters in function names
<https://unix.stackexchange.com/a/245336/3645>, only detect function
names with characters allowed by POSIX.1-2017
<https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_235>
for simplicity. This should cover the vast majority of use cases, and
produces system-agnostic results.

Since a word pattern has to be specified, but there is no easy way to
know the default word pattern, use the default `IFS` characters for a
starter. A later patch can improve this.

Signed-off-by: Victor Engmark <victor@engmark.name>
Acked-by: Johannes Sixt <j6t@kdbg.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
18 files changed:
Documentation/gitattributes.txt
t/t4018-diff-funcname.sh
t/t4018/bash-arithmetic-function [new file with mode: 0644]
t/t4018/bash-bashism-style-compact [new file with mode: 0644]
t/t4018/bash-bashism-style-function [new file with mode: 0644]
t/t4018/bash-bashism-style-whitespace [new file with mode: 0644]
t/t4018/bash-conditional-function [new file with mode: 0644]
t/t4018/bash-missing-parentheses [new file with mode: 0644]
t/t4018/bash-mixed-style-compact [new file with mode: 0644]
t/t4018/bash-mixed-style-function [new file with mode: 0644]
t/t4018/bash-nested-functions [new file with mode: 0644]
t/t4018/bash-other-characters [new file with mode: 0644]
t/t4018/bash-posix-style-compact [new file with mode: 0644]
t/t4018/bash-posix-style-function [new file with mode: 0644]
t/t4018/bash-posix-style-whitespace [new file with mode: 0644]
t/t4018/bash-subshell-function [new file with mode: 0644]
t/t4018/bash-trailing-comment [new file with mode: 0644]
userdiff.c

index 2d0a03715be65edfc4c8be6ca8f2a959ddde7540..e84e104f9325526bad7dca6043e897ad21b41906 100644 (file)
@@ -802,6 +802,9 @@ patterns are available:
 
 - `ada` suitable for source code in the Ada language.
 
+- `bash` suitable for source code in the Bourne-Again SHell language.
+  Covers a superset of POSIX shell function definitions.
+
 - `bibtex` suitable for files with BibTeX coded references.
 
 - `cpp` suitable for source code in the C and C++ languages.
index 9d077975791c1c002b4a927dfae71de594c8855f..9675bc17db276a51b1c5dc1b64ff1e94e1b0fc09 100755 (executable)
@@ -27,6 +27,7 @@ test_expect_success 'setup' '
 
 diffpatterns="
        ada
+       bash
        bibtex
        cpp
        csharp
diff --git a/t/t4018/bash-arithmetic-function b/t/t4018/bash-arithmetic-function
new file mode 100644 (file)
index 0000000..c0b276c
--- /dev/null
@@ -0,0 +1,4 @@
+RIGHT() ((
+
+    ChangeMe = "$x" + "$y"
+))
diff --git a/t/t4018/bash-bashism-style-compact b/t/t4018/bash-bashism-style-compact
new file mode 100644 (file)
index 0000000..1ca3126
--- /dev/null
@@ -0,0 +1,6 @@
+function RIGHT {
+    function InvalidSyntax{
+        :
+        echo 'ChangeMe'
+    }
+}
diff --git a/t/t4018/bash-bashism-style-function b/t/t4018/bash-bashism-style-function
new file mode 100644 (file)
index 0000000..f1de4fa
--- /dev/null
@@ -0,0 +1,4 @@
+function RIGHT {
+    :
+    echo 'ChangeMe'
+}
diff --git a/t/t4018/bash-bashism-style-whitespace b/t/t4018/bash-bashism-style-whitespace
new file mode 100644 (file)
index 0000000..ade85dd
--- /dev/null
@@ -0,0 +1,4 @@
+        function       RIGHT   (       )       {
+
+           ChangeMe
+        }
diff --git a/t/t4018/bash-conditional-function b/t/t4018/bash-conditional-function
new file mode 100644 (file)
index 0000000..c5949e8
--- /dev/null
@@ -0,0 +1,4 @@
+RIGHT() [[ \
+
+    "$a" > "$ChangeMe"
+]]
diff --git a/t/t4018/bash-missing-parentheses b/t/t4018/bash-missing-parentheses
new file mode 100644 (file)
index 0000000..8c8a05d
--- /dev/null
@@ -0,0 +1,6 @@
+function RIGHT {
+    functionInvalidSyntax {
+        :
+        echo 'ChangeMe'
+    }
+}
diff --git a/t/t4018/bash-mixed-style-compact b/t/t4018/bash-mixed-style-compact
new file mode 100644 (file)
index 0000000..d9364cb
--- /dev/null
@@ -0,0 +1,4 @@
+function RIGHT(){
+    :
+    echo 'ChangeMe'
+}
diff --git a/t/t4018/bash-mixed-style-function b/t/t4018/bash-mixed-style-function
new file mode 100644 (file)
index 0000000..555f9b2
--- /dev/null
@@ -0,0 +1,4 @@
+function RIGHT() {
+
+    ChangeMe
+}
diff --git a/t/t4018/bash-nested-functions b/t/t4018/bash-nested-functions
new file mode 100644 (file)
index 0000000..2c9237e
--- /dev/null
@@ -0,0 +1,6 @@
+outer() {
+    RIGHT() {
+        :
+        echo 'ChangeMe'
+    }
+}
diff --git a/t/t4018/bash-other-characters b/t/t4018/bash-other-characters
new file mode 100644 (file)
index 0000000..a3f390d
--- /dev/null
@@ -0,0 +1,4 @@
+_RIGHT_0n() {
+
+    ChangeMe
+}
diff --git a/t/t4018/bash-posix-style-compact b/t/t4018/bash-posix-style-compact
new file mode 100644 (file)
index 0000000..045bd20
--- /dev/null
@@ -0,0 +1,4 @@
+RIGHT(){
+
+    ChangeMe
+}
diff --git a/t/t4018/bash-posix-style-function b/t/t4018/bash-posix-style-function
new file mode 100644 (file)
index 0000000..a4d1448
--- /dev/null
@@ -0,0 +1,4 @@
+RIGHT() {
+
+    ChangeMe
+}
diff --git a/t/t4018/bash-posix-style-whitespace b/t/t4018/bash-posix-style-whitespace
new file mode 100644 (file)
index 0000000..4d984f0
--- /dev/null
@@ -0,0 +1,4 @@
+        RIGHT  (       )       {
+
+           ChangeMe
+        }
diff --git a/t/t4018/bash-subshell-function b/t/t4018/bash-subshell-function
new file mode 100644 (file)
index 0000000..80baa09
--- /dev/null
@@ -0,0 +1,4 @@
+RIGHT() (
+
+    ChangeMe=2
+)
diff --git a/t/t4018/bash-trailing-comment b/t/t4018/bash-trailing-comment
new file mode 100644 (file)
index 0000000..f1edbed
--- /dev/null
@@ -0,0 +1,4 @@
+RIGHT() { # Comment
+
+    ChangeMe
+}
index fde02f225b2b9ed17a8246913c3bddfa6483c98b..eb698eaca782b51ff1b2e6b22aed24d1159da6ec 100644 (file)
@@ -23,6 +23,27 @@ IPATTERN("ada",
         "[a-zA-Z][a-zA-Z0-9_]*"
         "|[-+]?[0-9][0-9#_.aAbBcCdDeEfF]*([eE][+-]?[0-9_]+)?"
         "|=>|\\.\\.|\\*\\*|:=|/=|>=|<=|<<|>>|<>"),
+PATTERNS("bash",
+        /* Optional leading indentation */
+        "^[ \t]*"
+        /* Start of captured text */
+        "("
+        "("
+            /* POSIX identifier with mandatory parentheses */
+            "[a-zA-Z_][a-zA-Z0-9_]*[ \t]*\\([ \t]*\\))"
+        "|"
+            /* Bashism identifier with optional parentheses */
+            "(function[ \t]+[a-zA-Z_][a-zA-Z0-9_]*(([ \t]*\\([ \t]*\\))|([ \t]+))"
+        ")"
+        /* Optional whitespace */
+        "[ \t]*"
+        /* Compound command starting with `{`, `(`, `((` or `[[` */
+        "(\\{|\\(\\(?|\\[\\[)"
+        /* End of captured text */
+        ")",
+        /* -- */
+        /* Characters not in the default $IFS value */
+        "[^ \t]+"),
 PATTERNS("dts",
         "!;\n"
         "!=\n"