Browse Source

userdiff: support Bash

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>
maint
Victor Engmark 4 years ago committed by Junio C Hamano
parent
commit
2ff6c34612
  1. 3
      Documentation/gitattributes.txt
  2. 1
      t/t4018-diff-funcname.sh
  3. 4
      t/t4018/bash-arithmetic-function
  4. 6
      t/t4018/bash-bashism-style-compact
  5. 4
      t/t4018/bash-bashism-style-function
  6. 4
      t/t4018/bash-bashism-style-whitespace
  7. 4
      t/t4018/bash-conditional-function
  8. 6
      t/t4018/bash-missing-parentheses
  9. 4
      t/t4018/bash-mixed-style-compact
  10. 4
      t/t4018/bash-mixed-style-function
  11. 6
      t/t4018/bash-nested-functions
  12. 4
      t/t4018/bash-other-characters
  13. 4
      t/t4018/bash-posix-style-compact
  14. 4
      t/t4018/bash-posix-style-function
  15. 4
      t/t4018/bash-posix-style-whitespace
  16. 4
      t/t4018/bash-subshell-function
  17. 4
      t/t4018/bash-trailing-comment
  18. 21
      userdiff.c

3
Documentation/gitattributes.txt

@ -802,6 +802,9 @@ patterns are available: @@ -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.

1
t/t4018-diff-funcname.sh

@ -27,6 +27,7 @@ test_expect_success 'setup' ' @@ -27,6 +27,7 @@ test_expect_success 'setup' '

diffpatterns="
ada
bash
bibtex
cpp
csharp

4
t/t4018/bash-arithmetic-function

@ -0,0 +1,4 @@ @@ -0,0 +1,4 @@
RIGHT() ((

ChangeMe = "$x" + "$y"
))

6
t/t4018/bash-bashism-style-compact

@ -0,0 +1,6 @@ @@ -0,0 +1,6 @@
function RIGHT {
function InvalidSyntax{
:
echo 'ChangeMe'
}
}

4
t/t4018/bash-bashism-style-function

@ -0,0 +1,4 @@ @@ -0,0 +1,4 @@
function RIGHT {
:
echo 'ChangeMe'
}

4
t/t4018/bash-bashism-style-whitespace

@ -0,0 +1,4 @@ @@ -0,0 +1,4 @@
function RIGHT ( ) {

ChangeMe
}

4
t/t4018/bash-conditional-function

@ -0,0 +1,4 @@ @@ -0,0 +1,4 @@
RIGHT() [[ \

"$a" > "$ChangeMe"
]]

6
t/t4018/bash-missing-parentheses

@ -0,0 +1,6 @@ @@ -0,0 +1,6 @@
function RIGHT {
functionInvalidSyntax {
:
echo 'ChangeMe'
}
}

4
t/t4018/bash-mixed-style-compact

@ -0,0 +1,4 @@ @@ -0,0 +1,4 @@
function RIGHT(){
:
echo 'ChangeMe'
}

4
t/t4018/bash-mixed-style-function

@ -0,0 +1,4 @@ @@ -0,0 +1,4 @@
function RIGHT() {

ChangeMe
}

6
t/t4018/bash-nested-functions

@ -0,0 +1,6 @@ @@ -0,0 +1,6 @@
outer() {
RIGHT() {
:
echo 'ChangeMe'
}
}

4
t/t4018/bash-other-characters

@ -0,0 +1,4 @@ @@ -0,0 +1,4 @@
_RIGHT_0n() {

ChangeMe
}

4
t/t4018/bash-posix-style-compact

@ -0,0 +1,4 @@ @@ -0,0 +1,4 @@
RIGHT(){

ChangeMe
}

4
t/t4018/bash-posix-style-function

@ -0,0 +1,4 @@ @@ -0,0 +1,4 @@
RIGHT() {

ChangeMe
}

4
t/t4018/bash-posix-style-whitespace

@ -0,0 +1,4 @@ @@ -0,0 +1,4 @@
RIGHT ( ) {

ChangeMe
}

4
t/t4018/bash-subshell-function

@ -0,0 +1,4 @@ @@ -0,0 +1,4 @@
RIGHT() (

ChangeMe=2
)

4
t/t4018/bash-trailing-comment

@ -0,0 +1,4 @@ @@ -0,0 +1,4 @@
RIGHT() { # Comment

ChangeMe
}

21
userdiff.c

@ -23,6 +23,27 @@ IPATTERN("ada", @@ -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"

Loading…
Cancel
Save