# Glob patterns w/ **; zero or more?

2 messages from 2023-04-16 to 2023-04-16. Participants: Maël Nison, Andreas Schwab.
Thread: https://gitlist.dev/t/59605

## Maël Nison, 2023-04-16 18:40

Subject: Glob patterns w/ **; zero or more?
Message-ID: <CANiF4iAaWB9j+Mma0-yfp6GvoQuM4k_OBAXn0keR+vCg8PQjmA@mail.gmail.com>
URL: https://gitlist.dev/e/CANiF4iAaWB9j%2BMma0-yfp6GvoQuM4k_OBAXn0keR%2BvCg8PQjmA%40mail.gmail.com

```
Hi,

I noticed that, in a repo with a single `main.c` file, `git ls-files
'./**/main.c'` (note the surrounding quotes, to avoid shell globbing)
returns no result even though `git ls-files main.c` does. It however
can find any `main.c` file located in a subdirectory, suggesting `**`
is interpreted as "one or more" rather than "zero or more". Can you
confirm it'd be a bug? I checked in both 2.38 and 2.40.

For reference, the documentation is explicit that `**` is "zero or
more", not "one or more", and it matches the behaviour from other glob
implementations (emphasis mine):

> A slash followed by two consecutive asterisks then a slash matches ***zero or
> more directories***. For example, "a/**/b" matches "a/b", "a/x/b", "a/x/y/b" and so on.

Also quoting the bash documentation for reference:

> globstar
> If set, the pattern ‘**’ used in a filename expansion context will match all files
> and zero or more directories and subdirectories. If the pattern is followed by
> a ‘/’, only directories and subdirectories match.

```

## Andreas Schwab, 2023-04-16 19:04

Subject: Re: Glob patterns w/ **; zero or more?
Message-ID: <87ttxfsl5w.fsf@igel.home>
URL: https://gitlist.dev/e/87ttxfsl5w.fsf%40igel.home
In-Reply-To: <CANiF4iAaWB9j+Mma0-yfp6GvoQuM4k_OBAXn0keR+vCg8PQjmA@mail.gmail.com>

```
On Apr 16 2023, Maël Nison wrote:

> I noticed that, in a repo with a single `main.c` file, `git ls-files
> './**/main.c'` (note the surrounding quotes, to avoid shell globbing)
> returns no result even though `git ls-files main.c` does. It however
> can find any `main.c` file located in a subdirectory, suggesting `**`
> is interpreted as "one or more" rather than "zero or more". Can you
> confirm it'd be a bug? I checked in both 2.38 and 2.40.

By default, pathspec matching does not take "**" specially, making it
equivalen to "*", but it can match "/".  Thus "./**/main.c" is the same
as "*/main.c" (the leading "./" always matches) and matches paths with
at least one "/" in it (thus "*/main.c" does not match "main.c").

If you use ":(glob)./**/main.c", it uses shell glob matching, where "**"
is special and "/**/" can match "/", but "*" does not match "/".

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."

```
