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

Re: [RFC 1/2] grep: only add delimiter if there isn't one already

From
Philip Oakley <philipoakley@iee.org>
Date
Jan 24, 2017, 20:50 UTC
Message-ID
<7611CDDCF2C34AFDB7995938228298B4@PhilipOakley>
In-Reply-To
<f90eba2a-ebfa-67f0-68c4-abacb05759ba@gmail.com>
From: "Jakub Narębski" <jnareb@gmail.com>
Show 16 quoted lines
>W dniu 23.01.2017 o 14:15, Stefan Hajnoczi pisze:
>> On Fri, Jan 20, 2017 at 10:16:31AM -0800, Junio C Hamano wrote:
>
>>> My only piece of advice to folks who feel that way is to learn Git
>>> more and get comfortable.  You can do neat things like
>>>
>>>    $ git grep -e pattern rev -- t ':!t/helper/'
>>>
>>> that you cannot do with "rev:t", for example ;-)
>>
>> Neat, thanks for showing the path exclusion syntax.  I wasn't aware of
>> it.
>
> That reminds me of mu TODO item: moving extended pathspec information
> from gitglossary(7) manpage (sic!) to to-be-created gitpathspec(7).
>
Good to see someone else also had it on a ToDo list..

Attached is my collation of all the different path spec info I found from trawling the man & guide pages to satisfy my ignorance... -- Philip

gitpathspec(7) ============

NAME ---- gitpathspec - How to specify a path or file to git

SYNOPSIS -------- $HOME/.config/git/ignore, $GIT_DIR/info/exclude, .gitignore

DESCRIPTION -----------

Pathspecs are used in a range of git functions. .gitignore .gitexclude gitsparse git-add -- pathspec git-checkout -- pathspec (after the double-dash) git grep (active/non-active wild card matching) git log (L#:<> pathspec limiters ?? what does it mean)

git rerere (uncontentious) git status (uncontentious) gitk (uncontentious, but see 'log' above) 'git' itself -- --literal-pathspecs Treat pathspecs literally (i.e. no globbing, no pathspec magic). This is equivalent to setting the GIT_LITERAL_PATHSPECS environment variable to 1.

--glob-pathspecs Add "glob" magic to all pathspec. This is equivalent to setting the GIT_GLOB_PATHSPECS environment variable to 1. Disabling globbing on individual pathspecs can be done using pathspec magic ":(literal)"

--noglob-pathspecs Add "literal" magic to all pathspec. This is equivalent to setting the GIT_NOGLOB_PATHSPECS environment variable to 1. Enabling globbing on individual pathspecs can be done using pathspec magic ":(glob)"

--icase-pathspecs Add "icase" magic to all pathspec. This is equivalent to setting the GIT_ICASE_PATHSPECS environment variable to 1.

see glossary-content pathspec Pattern used to limit paths in Git commands.

Pathspecs are used on the command line of "git ls-files", "git ls-tree", "git add", "git grep", "git diff", "git checkout", and many other commands to limit the scope of operations to some subset of the tree or worktree. See the documentation of each command for whether paths are relative to the current directory or toplevel. The pathspec syntax is as follows:
any path matches itself
the pathspec up to the last slash represents a directory prefix. The scope of that pathspec is limited to that subtree.
the rest of the pathspec is a pattern for the remainder of the pathname. Paths relative to the directory prefix will be matched against that pattern using fnmatch(3); in particular, * and ? can match directory separators.
For example, Documentation/*.jpg will match all .jpg files in the Documentation subtree, including Documentation/chapter_1/figure_1.jpg.
A pathspec that begins with a colon : has special meaning. In the short form, the leading colon : is followed by zero or more "magic signature" letters (which optionally is terminated by another colon :), and the remainder is the pattern to match against the path. The "magic signature" consists of ASCII symbols that are neither alphanumeric, glob, regex special charaters nor colon. The optional colon that terminates the "magic signature" can be omitted if the pattern begins with a character that does not belong to "magic signature" symbol set and is not a colon.
In the long form, the leading colon : is followed by a open parenthesis (, a comma-separated list of zero or more "magic words", and a close parentheses ), and the remainder is the pattern to match against the path.
A pathspec with only a colon means "there is no pathspec". This form should not be combined with other pathspec.

top The magic word top (magic signature: /) makes the pattern match from the root of the working tree, even when you are running the command from inside a subdirectory.

literal Wildcards in the pattern such as * or ? are treated as literal characters.

icase Case insensitive match.

glob Git treats the pattern as a shell glob suitable for consumption by fnmatch(3) with the FNM_PATHNAME flag: wildcards in the pattern will not match a / in the pathname. For example, "Documentation/*.html" matches "Documentation/git.html" but not "Documentation/ppc/ppc.html" or "tools/perf/Documentation/perf.html".

Two consecutive asterisks ("**") in patterns matched against full pathname may have special meaning:
A leading "**" followed by a slash means match in all directories. For example, "**/foo" matches file or directory "foo" anywhere, the same as pattern "foo". "**/foo/bar" matches file or directory "bar" anywhere that is directly under directory "foo".
A trailing "/**" matches everything inside. For example, "abc/**" matches all files inside directory "abc", relative to the location of the .gitignore file, with infinite depth.
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.
Other consecutive asterisks are considered invalid.
Glob magic is incompatible with literal magic.

exclude After a path matches any non-exclude pathspec, it will be run through all exclude pathspec (magic signature: !). If it matches, the path is ignored.

Which characters to escape(\) *:\/ ??

rooting (/*) of a path (a) $GIT_DIR (b) system root. directory (/) terminator (D/F conflict)

Compare with <path>; <paths>; and <file> 
[glob(7) patterns] (see grep)

` file specifies intentionally untracked files that Git should ignore. Files already tracked by Git are not affected; see the NOTES below for details.

a `pathspec` is specified by a pattern. When deciding whether a path matches a `pathspec` pattern, Git normally checks with the following order of precedence, from highest to lowest (within one level of precedence, the last matching pattern decides the outcome):

 * Patterns read from the command line for those commands that support
   them.
 * Patterns read from a `.pathspec` file in the same directory
   as the path, or in any parent directory, with patterns in the
   higher level files (up to the toplevel of the work tree) being overridden
   by those in lower level files down to the directory containing the file.
   These patterns match relative to the location of the
   `.pathspec` file.  A project normally includes such
   `.pathspec` files in its repository, containing patterns for
   files generated as part of the project build.
 * Patterns read from `$GIT_DIR/info/exclude`.
 * Patterns read from the file specified by the configuration
   variable 'core.excludesfile'.

Which file to place a pattern in depends on how the pattern is meant to be used.

 * Patterns which should be version-controlled and distributed to
   other repositories via clone (i.e., files that all developers will want
   to ignore) should go into a `.pathspec` file.
 * Patterns which are
   specific to a particular repository but which do not need to be shared
   with other related repositories (e.g., auxiliary files that live inside
   the repository but are specific to one user's workflow) should go into
   the `$GIT_DIR/info/exclude` file.
 * Patterns which a user wants Git to
   ignore in all situations (e.g., backup or temporary files generated by
   the user's editor of choice) generally go into a file specified by
   `core.excludesfile` in the user's `~/.gitconfig`. Its default value is
   $XDG_CONFIG_HOME/git/ignore. If $XDG_CONFIG_HOME is either not set or
   empty, $HOME/.config/git/ignore is used instead.

The underlying Git plumbing tools, such as 'git ls-files' and 'git read-tree', read `pathspec` patterns specified by command-line options, or from files specified by command-line options. Higher-level Git tools, such as 'git status' and 'git add', use patterns from the sources specified above.

PATTERN FORMAT --------------

 - A blank line matches no files, so it can serve as a separator
   for readability.
 - A line starting with # serves as a comment.
   Put a backslash ("`\`") in front of the first hash for patterns
   that begin with a hash.
 - An optional prefix "`!`" which negates the pattern; any
   matching file excluded by a previous pattern will become
   included again. It is not possible to re-include a file if a parent
   directory of that file is excluded. Git doesn't list excluded
   directories for performance reasons, so any patterns on contained
   files have no effect, no matter where they are defined.
   Put a backslash ("`\`") in front of the first "`!`" for patterns
   that begin with a literal "`!`", for example, "`\!important!.txt`".
 - If the pattern ends with a slash, it is removed for the
   purpose of the following description, but it would only find
   a match with a directory.  In other words, `foo/` will match a
   directory `foo` and paths underneath it, but will not match a
   regular file or a symbolic link `foo` (this is consistent
   with the way how pathspec works in general in Git).
 - If the pattern does not contain a slash '/', Git treats it as
   a shell glob pattern and checks for a match against the
   pathname relative to the location of the `.pathspec` file
   (relative to the toplevel of the work tree if not from a
   `.pathspec` file).
 - Otherwise, Git treats the pattern as a shell glob suitable
   for consumption by fnmatch(3) with the FNM_PATHNAME flag:
   wildcards in the pattern will not match a / in the pathname.
   For example, "Documentation/{asterisk}.html" matches
   "Documentation/git.html" but not "Documentation/ppc/ppc.html"
   or "tools/perf/Documentation/perf.html".
 - A leading slash matches the beginning of the pathname.
   For example, "/{asterisk}.c" matches "cat-file.c" but not
   "mozilla-sha1/sha1.c".

Two consecutive asterisks ("`**`") in patterns matched against full pathname may have special meaning:

 - A leading "`**`" followed by a slash means match in all
   directories. For example, "`**/foo`" matches file or directory
   "`foo`" anywhere, the same as pattern "`foo`". "`**/foo/bar`"
   matches file or directory "`bar`" anywhere that is directly
   under directory "`foo`".
 - A trailing "`/**`" matches everything inside. For example,
   "`abc/**`" matches all files inside directory "`abc`", relative
   to the location of the `.pathspec` file, with infinite depth.
 - 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.
 - Other consecutive asterisks are considered invalid.

NOTES -----

The purpose of pathspec files is to ensure that certain files not tracked by Git remain untracked.

To ignore uncommitted changes in a file that is already tracked, use 'git update-index {litdd}assume-unchanged'.

To stop tracking a file that is currently tracked, use 'git rm --cached'.

EXAMPLES --------

--------------------------------------------------------------
    $ git status
    [...]
    # Untracked files:
    [...]
    #       Documentation/foo.html
    #       Documentation/pathspec.html
    #       file.o
    #       lib.a
    #       src/internal.o
    [...]
    $ cat .git/info/exclude
    # ignore objects and archives, anywhere in the tree.
    *.[oa]
    $ cat Documentation/.pathspec
    # ignore generated html files,
    *.html
    # except foo.html which is maintained by hand
    !foo.html
    $ git status
    [...]
    # Untracked files:
    [...]
    #       Documentation/foo.html
    [...]
--------------------------------------------------------------
Another example:
--------------------------------------------------------------
    $ cat .pathspec
    vmlinux*
    $ ls arch/foo/kernel/vm*
    arch/foo/kernel/vmlinux.lds.S
    $ echo '!/vmlinux*' >arch/foo/kernel/.pathspec
--------------------------------------------------------------

The second .pathspec prevents Git from ignoring `arch/foo/kernel/vmlinux.lds.S`.

Example to exclude everything except a specific directory `foo/bar` (note the `/*` - without the slash, the wildcard would also exclude everything within `foo/bar`):

--------------------------------------------------------------
    $ cat .pathspec
    # exclude everything except directory foo/bar
    /*
    !/foo
    /foo/*
    !/foo/bar
--------------------------------------------------------------

SEE ALSO -------- linkgit:git-rm[1], linkgit:git-update-index[1], linkgit:gitrepository-layout[5], linkgit:git-check-ignore[1]

GIT --- Part of the linkgit:git[1] suite

Previous: Jakub NarębskiNext: Jeff King
Message 17 of 21 in “[RFC 0/2] grep: make output consistent with revision syntax”
  1. Stefan HajnocziJan 19, 2017
  2. 2/2 grep: use '/' delimiter for pathsStefan Hajnoczi, Jan 19, 2017
  3. Junio C HamanoJan 19, 2017
  4. Stefan HajnocziJan 20, 2017
  5. Jeff KingJan 20, 2017
  6. Junio C HamanoJan 20, 2017
  7. Stefan HajnocziJan 23, 2017
  8. Phil HordJan 24, 2017
  9. Brandon WilliamsJan 19, 2017
  10. Stefan HajnocziJan 20, 2017
  11. 1/2 grep: only add delimiter if there isn't one alreadyStefan Hajnoczi, Jan 19, 2017
  12. Junio C HamanoJan 19, 2017
  13. Stefan HajnocziJan 20, 2017
  14. Junio C HamanoJan 20, 2017
  15. Stefan HajnocziJan 23, 2017
  16. Jakub NarębskiJan 24, 2017
  17. Philip OakleyJan 24, 2017
  18. Jeff KingJan 19, 2017
  19. Brandon WilliamsJan 19, 2017
  20. Stefan HajnocziJan 20, 2017
  21. Jeff KingJan 20, 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.