git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] userdiff: add build-in pattern for shell

From
Junio C Hamano <gitster@pobox.com>
Date
Mar 29, 2017, 17:39 UTC
Message-ID
<xmqqzig49e4j.fsf@gitster.mtv.corp.google.com>
In-Reply-To
<20170329165331.17742-1-pickfire@riseup.net>
Ivan Tham <pickfire@riseup.net> writes:
Show 28 quoted lines
> Shell are widely used but comes with lots of different patterns. The
> build-in pattern aim for POSIX-compatible shells with some additions:
>
> - Notably ${g//re/s} and ${g#cut}
> - "function" from bash
>
> 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..1bad72df2 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-compatible shells.

The new test you added seems to show that this is not limited to POSIX shells but also understands bashisms like ${x//x/x}. Perhaps drop "POSIX-compatible" from here.

Show 10 quoted lines
> diff --git a/userdiff.c b/userdiff.c
> index 8b732e40b..8d5127fb6 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]*\\{?$",

There is something funky going on around parentheses on this line. The ones around "function " is meant to be syntactic metacharacters to produce a group in the regexp so that you can apply '?' (i.e. zero or one occurrence) to it. But I think the second pair of parentheses that appears later on the line, which enclose nothing, are meant to be literal? E.g. "hello (){\n\techo world;\n}\n" They would need some quoting, perhaps like

	...[ \t]*\\(\\)[\t]*....
> +	 /* -- */
> +	 "(\\$|--?)?([a-zA-Z_][a-zA-Z0-9._]*|[0-9]+|#)|--" /* command/param */
TBH, I have no idea what this line-noise is doing.

$foobar, $4, --foobar, foobar, 123 and -- can be seen easily out of these patterns. I am not sure what --# would be (perhaps you meant to only catch $# and --# is included by accident, in which case it is understandable). It feels a bit strange to see that $# is supported but not $?; --foo but not --foo=bar; foobar but not "foo bar" inside a dq-pair.

> +	 "|\\$[({]|[)}]|[-+*/=!]=?|[\\]&%#/|]{1,2}|[<>]{1,3}|[ \t]#.*"),
And this one is even more dense.
Previous: Ivan ThamNext: Pickfire
Message 2 of 6 in “userdiff: add build-in pattern for shell”
  1. userdiff: add build-in pattern for shellIvan Tham, Mar 29, 2017
  2. Junio C HamanoMar 29, 2017
  3. PickfireMar 30, 2017
  4. Junio C HamanoMar 30, 2017
  5. Ivan ThamMar 30, 2017
  6. userdiff: add build-in pattern for shellIvan Tham, Mar 30, 2017

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.