{"thread":{"id":"22657","subject":"git-grep ignores untracked files","startedAt":"2010-02-15T00:35:37Z","lastAt":"2010-02-16T02:03:48Z","messageCount":5,"participants":["Mark Lodato","Junio C Hamano"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"134583","messageId":"1266194137-25653-1-git-send-email-lodatom@gmail.com","threadId":"22657","inReplyTo":null,"subject":"git-grep ignores untracked files","fromName":"Mark Lodato","fromEmail":"lodatom@gmail.com","sentAt":"2010-02-15T00:35:37Z","receivedAt":"2010-02-15T00:35:37Z","isPatch":false,"sender":{"key":"lodatom@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58860?v=4"},"body":"Currently, git-grep ignores untracked files in the working directory.\nFor example:\n\n    $ pattern=qazxswedc\n    $ echo $pattern > foo\n    $ git grep $pattern || echo not found\n    not found\n\nThis is annoying and counter-intuitive, since it *does* search modified\nfiles with unstaged changes.  More importantly, this is undocumented.\n\nThe situation is much worse when untracked files are given on the\ncommand line.  For example, the following command warns me that a file\ndoes not exist, as expected.\n\n    $ git grep $pattern does_not_exist\n    fatal: ambiguous argument 'does_not_exist': unknown revision or path not in the working tree.\n    Use '--' to separate paths from revisions\n\nHowever, if I give it an untracked file, it just tells me there's no match.\n\n    $ git grep $pattern foo || echo not found\n    not found\n\nIn this case, I would expect the program to either search that file\n(would be nice!) or die with an error message (I'll take this for now).\nFurthermore, when '--' is given, even the non-existent case fails\nsilently.\n\n    $ git grep $pattern -- does_not_exist || echo not found\n    not found\n\nSo, what I would I like to see?  Ideally, something like the following\nseries of patches:\n\n1. Document the current behavior in the man page.\n\n2. Issue a warning if any paths (pattern or non-pattern) fail to match\nany files.  Ideally, this would work in all cases (work tree, --cached,\nand trees).  If an untracked file matches a pattern that also matches\na tracked file, there would be no warning - not sure what's best in this\ncase.  This warning should probably be disable-able with a flag, say\n--no-warn-if-unmatched.\n\n3. If neither --cached nor trees are given, and a non-pattern filename\nis given that does not not exist in the cache, search this file as well.\n(That is, instead of issuing a warning, just search it.)  If the\nfilename is a directory, issue a warning.\n\n4. Add an --untracked flag that causes grep to search *all* files in the\nworking directory, not just tracked ones.  (This includes searches with\nand without a pathspec.)  The option would be incompatible with --cached\nor given trees.\n\n\nI post this wish list for several reasons.  First, to see if anyone\nagrees with me.  Second, to see if this patch series has a chance of\never being accepted.  Third, to implant this idea into someones head so\nthey write it for me :).  I may try to do it myself, but it will take me\na while.\n\nTo get started, here's a shot at #1.\n\nMark\n\n---- 8< ----\nFrom 697d97ca45b5c7abe8c84c3caae28cf839c668ac Mon Sep 17 00:00:00 2001\nFrom: Mark Lodato <lodatom@gmail.com>\nDate: Sun, 14 Feb 2010 19:28:55 -0500\nSubject: [PATCH] Documentation: clarify untracked files and <path>\n\nNote that only tracked files are searched, that non-matching paths are\nsilently ignored, and that <path> is a glob(7) pattern.\n\nSigned-off-by: Mark Lodato <lodatom@gmail.com>\n---\n Documentation/git-grep.txt |    8 +++++++-\n 1 files changed, 7 insertions(+), 1 deletions(-)\n\ndiff --git a/Documentation/git-grep.txt b/Documentation/git-grep.txt\nindex e019e76..fdc05ec 100644\n--- a/Documentation/git-grep.txt\n+++ b/Documentation/git-grep.txt\n@@ -27,7 +27,9 @@ SYNOPSIS\n DESCRIPTION\n -----------\n Look for specified patterns in the working tree files, blobs\n-registered in the index file, or given tree objects.\n+registered in the index file, or given tree objects.  Only tracked files in\n+the working tree are searched.  Paths that do not match are silently ignored,\n+including paths to untracked files.\n \n \n OPTIONS\n@@ -170,6 +172,10 @@ OPTIONS\n \tSignals the end of options; the rest of the parameters\n \tare <path> limiters.\n \n+<path>...::\n+\tOnly search files matching these wildcard patterns; see glob(7) for\n+\tthe format.  If not given, all tracked files in the tree are searched.\n+\n \n Example\n -------\n-- \n1.7.0\n"},{"id":"134588","messageId":"7vvddz5l1z.fsf@alter.siamese.dyndns.org","threadId":"22657","inReplyTo":"1266194137-25653-1-git-send-email-lodatom@gmail.com","subject":"Re: git-grep ignores untracked files","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2010-02-15T01:45:12Z","receivedAt":"2010-02-15T01:45:12Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":">  Look for specified patterns in the working tree files, blobs\n> -registered in the index file, or given tree objects.\n> +registered in the index file, or given tree objects.  Only tracked files in\n> +the working tree are searched.  Paths that do not match are silently ignored,\n> +including paths to untracked files.\n\nStrictly speaking, because git commands are about tracked files unless\nexplicitly stated otherwise, the phrase \"files in the work tree\" already\nmeans \"tracked\" ones.  It is understandable (but not excusable) that\npeople who wrote manual assumed the readers would have learned that from\nthe user manual without repeating.  Adding explicit description would be a\ngood thing to do.\n\nI think rewording the paragraph to \"... patterns in the tracked files in\nthe work tree, blobs registered in the index file, ... or given tree\nobjects.\"  would be a good balance to strike.  It gives enough information\nwithout making it too verbose.\n\nI doubt we want to have \"Only tracked files blah blah\".  Like all the\nnormal git commands, \"grep\" is about tracked contents, and I don't think\nit would help to repeat the obvious like pathspec filter will act as a\nfilter.  \"add <pathspec>\" is an exception in that it _is_ about untracked\npaths and that is why you get warnings for unmatched ones.\n\n    Side note: there will be --no-index option to let you run \"git grep\"\n    over files in a random directory.\n\n> +<path>...::\n> +\tOnly search files matching these wildcard patterns; see glob(7) for\n> +\tthe format.  If not given, all tracked files in the tree are searched.\n\nPlease do *not* \"see glob(7) for the format\".  Pathspec used for \"grep\"\n(and \"ls-files\") are \"leading path match or glob(7)\".  E.g. \"git grep\nfrotz t/\" looks for frotz in all files under \"t/\" recursively, and that\ndoes not have much to do with glob(7).  If we do not have a description\nalready, we may want to add these basics to git(1) or the user manual.\n\nThanks.\n"},{"id":"134683","messageId":"ca433831002151620q2b875c51md883f4b424231f15@mail.gmail.com","threadId":"22657","inReplyTo":"7vvddz5l1z.fsf@alter.siamese.dyndns.org","subject":"Re: git-grep ignores untracked files","fromName":"Mark Lodato","fromEmail":"lodatom@gmail.com","sentAt":"2010-02-16T00:20:30Z","receivedAt":"2010-02-16T00:20:30Z","isPatch":false,"sender":{"key":"lodatom@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58860?v=4"},"body":"On Sun, Feb 14, 2010 at 8:45 PM, Junio C Hamano <gitster@pobox.com> wrote:\n> I doubt we want to have \"Only tracked files blah blah\".  Like all the\n> normal git commands, \"grep\" is about tracked contents, and I don't think\n> it would help to repeat the obvious like pathspec filter will act as a\n> filter.  \"add <pathspec>\" is an exception in that it _is_ about untracked\n> paths and that is why you get warnings for unmatched ones.\n\nI think adding this information to the description of <path> would be\nsufficient.  I'll send a new patch in a minute.\n\n>    Side note: there will be --no-index option to let you run \"git grep\"\n>    over files in a random directory.\n\nAh, didn't see this.  So, it looks like most of my requests were\nalready done.  However, it may still be a good idea to DWIM when none\nof --cached, --no-index, or trees are given but a pathspec is given\nthat matches an untracked file in the working directory.  For example:\n\n$ git grep $pattern -- untracked_file\n\nIt is obvious that the user meant to specify --no-index.   However,\nI'm not sure where to draw the line.  What if they give tracked and\nuntracked files, or if they given a glob pattern that matches both?\n\nStill, the simple, \"one path is given, it's not a pattern, and it\nmatches exactly to an untracked file\" case should probably run with\n--no-index.\n\n>> +<path>...::\n>> +     Only search files matching these wildcard patterns; see glob(7) for\n>> +     the format.  If not given, all tracked files in the tree are searched.\n>\n> Please do *not* \"see glob(7) for the format\".  Pathspec used for \"grep\"\n> (and \"ls-files\") are \"leading path match or glob(7)\".  E.g. \"git grep\n> frotz t/\" looks for frotz in all files under \"t/\" recursively, and that\n> does not have much to do with glob(7).  If we do not have a description\n> already, we may want to add these basics to git(1) or the user manual.\n\nAh.  This is a major inconsistency in the documentation.  I have a lot\nto say about this, so I'll make this a separate thread.  For the\npatch, I'll just reference git-add(1), which is the does have a\ndescription, and then we can fix this properly in a future patch.\n"},{"id":"134684","messageId":"1266279940-13251-1-git-send-email-lodatom@gmail.com","threadId":"22657","inReplyTo":"7vvddz5l1z.fsf@alter.siamese.dyndns.org","subject":"[PATCHv2] grep documentation: clarify what files match","fromName":"Mark Lodato","fromEmail":"lodatom@gmail.com","sentAt":"2010-02-16T00:25:40Z","receivedAt":"2010-02-16T00:25:40Z","isPatch":false,"sender":{"key":"lodatom@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58860?v=4"},"body":"Clarify that git-grep(1) searches only tracked files, and that each\n<path> is a glob, as in git-add(1).  Add an example to show a simple use\ncase for searching all .c and .h files.\n\nThe meta-variable <path> should be changed to an official term for\na path glob, and the description for this should be in git(1), not\ngit-add(1).  However, we don't yet have such an official term, so just\nuse <path> and reference git-add(1) for now.\n\nSigned-off-by: Mark Lodato <lodatom@gmail.com>\n---\n Documentation/git-grep.txt |   13 ++++++++++---\n 1 files changed, 10 insertions(+), 3 deletions(-)\n\ndiff --git a/Documentation/git-grep.txt b/Documentation/git-grep.txt\nindex e019e76..7f24032 100644\n--- a/Documentation/git-grep.txt\n+++ b/Documentation/git-grep.txt\n@@ -26,8 +26,8 @@ SYNOPSIS\n \n DESCRIPTION\n -----------\n-Look for specified patterns in the working tree files, blobs\n-registered in the index file, or given tree objects.\n+Look for specified patterns in the tracked files in the working tree, blobs\n+registered in the index file, or blobs in given tree objects.\n \n \n OPTIONS\n@@ -49,7 +49,7 @@ OPTIONS\n \tDon't match the pattern in binary files.\n \n --max-depth <depth>::\n-\tFor each pathspec given on command line, descend at most <depth>\n+\tFor each <path> given on command line, descend at most <depth>\n \tlevels of directories. A negative value means no limit.\n \n -w::\n@@ -170,10 +170,17 @@ OPTIONS\n \tSignals the end of options; the rest of the parameters\n \tare <path> limiters.\n \n+<path>...::\n+\tIf given, limit the search to paths matching at least one pattern.\n+\tEach pattern is the same as <filepattern> of linkgit:git-add[1].\n \n Example\n -------\n \n+git grep 'time_t' -- '*.[ch]'::\n+\tLooks for `time_t` in all tracked .c and .h files in the working\n+\tdirectory.\n+\n git grep -e \\'#define\\' --and \\( -e MAX_PATH -e PATH_MAX \\)::\n \tLooks for a line that has `#define` and either `MAX_PATH` or\n \t`PATH_MAX`.\n-- \n1.7.0\n"},{"id":"134688","messageId":"7vfx52rl6j.fsf@alter.siamese.dyndns.org","threadId":"22657","inReplyTo":"1266279940-13251-1-git-send-email-lodatom@gmail.com","subject":"Re: [PATCHv2] grep documentation: clarify what files match","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2010-02-16T02:03:48Z","receivedAt":"2010-02-16T02:03:48Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Mark Lodato <lodatom@gmail.com> writes:\n\n> Clarify that git-grep(1) searches only tracked files, and that each\n> <path> is a glob, as in git-add(1).  Add an example to show a simple use\n> case for searching all .c and .h files.\n>\n> The meta-variable <path> should be changed to an official term for\n> a path glob, and the description for this should be in git(1), not\n> git-add(1).  However, we don't yet have such an official term, so just\n> use <path> and reference git-add(1) for now.\n>\n> Signed-off-by: Mark Lodato <lodatom@gmail.com>\n> ---\n\nLooks much better, even though I personally prefer a shorter \"work tree\"\nand also think the official name of the stuff is \"pathspec\".\n\nWill queue until an alternative materializes (if ever) ;-).\n\nThanks.\n"}]}