git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Ivan Tham <pickfire@riseup.net>
To: git@vger.kernel.org
Subject: [PATCH v2] userdiff: add build-in pattern for shell
Date: Fri, 31 Mar 2017 02:08:29 +0800	[thread overview]
Message-ID: <20170330180829.27879-1-pickfire@riseup.net> (raw)
In-Reply-To: <20170329165331.17742-1-pickfire@riseup.net>

Shell are widely used but comes with lots of different patterns. The
build-in pattern aim for POSIX-like shells with some additions:

- Notably ${g//re/s} and ${g#cut}
- Bashisms such as "function"

Signed-off-by: Ivan Tham <pickfire@riseup.net>
---
 Documentation/gitattributes.txt |  2 ++
 t/t4034-diff-words.sh           |  1 +
 t/t4034/sh/expect               | 14 ++++++++++++++
 t/t4034/sh/post                 |  7 +++++++
 t/t4034/sh/pre                  |  7 +++++++
 userdiff.c                      |  5 +++++
 6 files changed, 36 insertions(+)
 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 a53d093ca..f8440119d 100644
--- a/Documentation/gitattributes.txt
+++ b/Documentation/gitattributes.txt
@@ -706,6 +706,8 @@ patterns are available:
 
 - `ruby` suitable for source code in the Ruby language.
 
+- `sh` suitable for source code in POSIX-like shells.
+
 - `tex` suitable for source code for LaTeX documents.
 
 
diff --git a/t/t4034-diff-words.sh b/t/t4034-diff-words.sh
index 912df9122..2eb662f89 100755
--- a/t/t4034-diff-words.sh
+++ b/t/t4034-diff-words.sh
@@ -313,6 +313,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 000000000..e7b0a9ae3
--- /dev/null
+++ b/t/t4034/sh/expect
@@ -0,0 +1,14 @@
+<BOLD>diff --git a/pre b/post<RESET>
+<BOLD>index 7bb0d15..df3845b 100644<RESET>
+<BOLD>--- a/pre<RESET>
+<BOLD>+++ b/post<RESET>
+<CYAN>@@ -1,7 +1,7 @@<RESET>
+echo <GREEN>"<RESET>Hello world<RED>!<RESET>
+<RED>bomb<RESET><GREEN>?"<RESET>
+<GREEN>fork<RESET>(){ <RED>bomb<RESET><GREEN>fork<RESET>|<RED>bomb<RESET><GREEN>fork<RESET>& }
+<RED>; bomb<RESET>
+<RED>a<RESET><GREEN>x<RESET>=<RED>1 a<RESET><GREEN>2 x<RESET>=$((<RED>a<RESET><GREEN>x<RESET>+<RED>1<RESET><GREEN>2<RESET>)) <RED>a<RESET><GREEN>x<RESET>=$((<RED>a-1<RESET><GREEN>x-2<RESET>)) <RED>a<RESET><GREEN>x<RESET>=$((<RED>a<RESET><GREEN>x<RESET>*<RED>1<RESET><GREEN>2<RESET>)) <RED>a<RESET><GREEN>x<RESET>=$((<RED>a<RESET><GREEN>x<RESET>/<RED>1<RESET><GREEN>2<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>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>a<RESET><GREEN>x<RESET>=${<RED>a<RESET><GREEN>x<RESET>//<RED>a<RESET><GREEN>x<RESET>/<RED>a<RESET><GREEN>x<RESET>}
+command <RED>-h -v<RESET><GREEN>--help=all -q<RESET> | xargs -- echo <GREEN>2<RESET>><RED>&1 &<RESET><GREEN>/dev/null<RESET>
+[ <RED>$a -eq $b<RESET><GREEN>$x -ne $y<RESET> ]&&echo <RED>aa<RESET><GREEN>xx<RESET>||echo <RED>bb<RESET><GREEN>yy<RESET>
+[ "<RED>$a<RESET><GREEN>$x<RESET>"!=<RED>1<RESET><GREEN>2<RESET> ] && echo <RED>a<RESET><GREEN>x<RESET> || echo <RED>b<RESET><GREEN>y<RESET>
diff --git a/t/t4034/sh/post b/t/t4034/sh/post
new file mode 100644
index 000000000..df3845b4f
--- /dev/null
+++ b/t/t4034/sh/post
@@ -0,0 +1,7 @@
+echo "Hello world?"
+fork(){ fork|fork& }
+x=2 x=$((x+2)) x=$((x-2)) x=$((x*2)) x=$((x/2))
+x=$(x) x=`x` x=${x#x*} x=${x%x*} x=${x//x/x}
+command --help=all -q | xargs -- echo 2>/dev/null
+[ $x -ne $y ]&&echo xx||echo yy
+[ "$x"!=2 ] && echo x || echo y
diff --git a/t/t4034/sh/pre b/t/t4034/sh/pre
new file mode 100644
index 000000000..7bb0d1562
--- /dev/null
+++ b/t/t4034/sh/pre
@@ -0,0 +1,7 @@
+echo Hello world!
+bomb(){ bomb|bomb& }; bomb
+a=1 a=$((a+1)) a=$((a-1)) a=$((a*1)) a=$((a/1))
+a=$(a) a=`a` a=${a#a*} a=${a%a*} a=${a//a/a}
+command -h -v | xargs -- echo >&1 &
+[ $a -eq $b ]&&echo aa||echo bb
+[ "$a"!=1 ] && echo a || echo b
diff --git a/userdiff.c b/userdiff.c
index 8b732e40b..72ffd85f1 100644
--- a/userdiff.c
+++ b/userdiff.c
@@ -148,6 +148,11 @@ PATTERNS("csharp",
 	 "[a-zA-Z_][a-zA-Z0-9_]*"
 	 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
 	 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
+PATTERNS("sh",
+	 "^[ \t]*(function )?[A-Za-z_][A-Za-z_0-9]*[ \t]*\\(\\)[\t]*\\{?$",
+	 /* -- */
+	 "(\\$|--?)?([a-zA-Z_][a-zA-Z0-9._]*|[0-9]+|#|\\?)|--" /* command/param */
+	 "|\\$[({]|[)}]|[-+*/=!]=?|[\\]&%#/|]{1,2}|[<>]{1,3}|[ \t]#.*"),
 IPATTERN("css",
 	 "![:;][[:space:]]*$\n"
 	 "^[_a-z0-9].*$",
-- 
2.12.2.609.gf7d0c115f


      parent reply	other threads:[~2017-03-30 18:08 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-03-29 16:53 [PATCH] userdiff: add build-in pattern for shell Ivan Tham
2017-03-29 17:39 ` Junio C Hamano
2017-03-30  2:28   ` Pickfire
2017-03-30  6:25     ` Junio C Hamano
2017-03-30  7:20       ` Ivan Tham
2017-03-30 18:08 ` Ivan Tham [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170330180829.27879-1-pickfire@riseup.net \
    --to=pickfire@riseup.net \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).