What is the definition of `pattern` for git ls-remote?

1.2k views Asked by At

What type of "pattern" does git ls-remote take?

man git-ls-remote says:

   <refs>...
       When unspecified, all references, after filtering done with --heads
       and --tags, are shown. When <refs>... are specified, only references
       matching the given patterns are displayed.

It is a POSIX shell glob, regex, gitignore pattern, ...?

2

There are 2 answers

0
Benoit Blanchon On

Indeed, the documentation of ls-remote doesn't say much, but you can find this information in other pages. For example the documentation for git tag -l says:

The pattern is a shell wildcard (i.e., matched using fnmatch(3)).

As far as I can tell git only supports the basic glob syntax but doesn't support "extended patterns" provided by the FNM_EXTMATCH.

I think the filtering is actually implemented by wildmatch(), so the behavior may differ from the standard fnmatch.

0
VonC On

Note that Git 2.40 (Q1 2023) does slightly update the definition of a pattern for git ls-remote.

See commit d9ec3b0, commit baebde7 (10 Feb 2023) by Jeff King (peff).
(Merged by Junio C Hamano -- gitster -- in commit 6aac634, 22 Feb 2023)

doc/ls-remote: clarify pattern format

Signed-off-by: Jeff King

We document that you can specify "refs" to ls-remote, but we don't explain any further than that they are "matched" as patterns.
Since this can be interpreted in a lot of ways, let's clarify that they are tail-matched globs.

Likewise, let's use the word "patterns" to refer to them consistently, rather than "refs" (both here and in the quick "-h" help), and mention more explicitly that only one pattern needs to be matched (though there is also an example already that shows this in action).

git ls-remote now includes in its man page:

[--symref] [<repository> [<patterns>...]]

git ls-remote now includes in its man page:

<patterns>...:

When unspecified, all references, after filtering done with --heads and --tags, are shown.

When <patterns>... are specified, only references matching one or more of the given patterns are displayed.
Each pattern is interpreted as a glob which is matched against the "tail" of a ref, starting either from the start of the ref (so a full name like refs/heads/foo matches) or from a slash separator (so bar matches refs/heads/bar but not refs/heads/foobar).