path: root/builtin-grep.c
AgeCommit message (Collapse)Author
2010-02-22Move 'builtin-*' into a 'builtin/' subdirectoryLinus Torvalds
This shrinks the top-level directory a bit, and makes it much more pleasant to use auto-completion on the thing. Instead of [torvalds@nehalem git]$ em buil<tab> Display all 180 possibilities? (y or n) [torvalds@nehalem git]$ em builtin-sh builtin-shortlog.c builtin-show-branch.c builtin-show-ref.c builtin-shortlog.o builtin-show-branch.o builtin-show-ref.o [torvalds@nehalem git]$ em builtin-shor<tab> builtin-shortlog.c builtin-shortlog.o [torvalds@nehalem git]$ em builtin-shortlog.c you get [torvalds@nehalem git]$ em buil<tab> [type] builtin/ builtin.h [torvalds@nehalem git]$ em builtin [auto-completes to] [torvalds@nehalem git]$ em builtin/sh<tab> [type] shortlog.c shortlog.o show-branch.c show-branch.o show-ref.c show-ref.o [torvalds@nehalem git]$ em builtin/sho [auto-completes to] [torvalds@nehalem git]$ em builtin/shor<tab> [type] shortlog.c shortlog.o [torvalds@nehalem git]$ em builtin/shortlog.c which doesn't seem all that different, but not having that annoying break in "Display all 180 possibilities?" is quite a relief. NOTE! If you do this in a clean tree (no object files etc), or using an editor that has auto-completion rules that ignores '*.o' files, you won't see that annoying 'Display all 180 possibilities?' message - it will just show the choices instead. I think bash has some cut-off around 100 choices or something. So the reason I see this is that I'm using an odd editory, and thus don't have the rules to cut down on auto-completion. But you can simulate that by using 'ls' instead, or something similar. Signed-off-by: Linus Torvalds <> Signed-off-by: Junio C Hamano <>
2010-02-17Merge branch 'maint'Junio C Hamano
* maint: Prepare release notes Fix use of mutex in threaded grep dwim_ref: fix dangling symref warning stash pop: remove 'apply' options during 'drop' invocation diff: make sure --output=/bad/path is caught Remove hyphen from "git-command" in two error messages
2010-02-17Fix use of mutex in threaded grepJunio C Hamano
The program can decide at runtime not to use threading even if the support is compiled in. In such a case, mutexes are not necessary and left uninitialized. But the code incorrectly tried to take and release the read_sha1_mutex unconditionally. Signed-off-by: Junio C Hamano <> Acked-by: Fredrik Kuivinen <>
2010-02-13Merge branch 'rs/git-dir-cleanup'Junio C Hamano
* rs/git-dir-cleanup: Resurrect "git grep --no-index" setenv(GIT_DIR) clean-up Conflicts: builtin-grep.c t/
2010-02-13Resurrect "git grep --no-index"Junio C Hamano
This reverts commit 3c8f6c8 (Revert 30816237 and 7e62265, 2010-02-05) as the issue has been sorted out.
2010-02-07accept "git grep -- pattern"Jeff King
Currently the only way to "quote" a grep pattern that might begin with a dash is to use "git grep -e pattern". This works just fine, and is also the way right way to do it on many traditional grep implemenations. Some people prefer to use "git grep -- pattern", however, as "--" is the usual "end of options" marker, and at least GNU grep and Solaris 10 grep support this. This patch makes that syntax work. There is a slight behavior change, in that "git grep -- $X" used to be interpreted as "grep for -- in $X". However, that usage is questionable. "--" is usually the end-of-options marker, so "git grep" was unlike many other greps in treating it as a literal pattern (e.g., both GNU grep and Solaris 10 grep will treat "grep --" as missing a pattern). Signed-off-by: Jeff King <> Signed-off-by: Junio C Hamano <>
2010-02-05Revert 30816237 and 7e62265Junio C Hamano
It seems that we have bad interaction with the code related to GIT_WORK_TREE and "grep --no-index", and broke running grep inside the .git directory. For now, just revert it and resurrect it after 1.7.0 ships. Signed-off-by: Junio C Hamano <>
2010-01-30grep: Fix two memory leaksDan McGee
We duplicate the grep_opt structure when using grep threads, but didn't later free either the patterns attached to this new structure or the structure itself. Signed-off-by: Dan McGee <> Signed-off-by: Junio C Hamano <>
2010-01-28grep --quiet: finishing touchesJunio C Hamano
Name the option "--quiet" not "--quick", document it, and add tests. Signed-off-by: Junio C Hamano <>
2010-01-26Threaded grepFredrik Kuivinen
Make git grep use threads when it is available. The results below are best of five runs in the Linux repository (on a box with two cores). With the patch: git grep qwerty 1.58user 0.55system 0:01.16elapsed 183%CPU (0avgtext+0avgdata 0maxresident)k 0inputs+800outputs (0major+5774minor)pagefaults 0swaps Without: git grep qwerty 1.59user 0.43system 0:02.02elapsed 100%CPU (0avgtext+0avgdata 0maxresident)k 0inputs+800outputs (0major+3716minor)pagefaults 0swaps And with a pattern with quite a few matches: With the patch: $ /usr/bin/time git grep void 5.61user 0.56system 0:03.44elapsed 179%CPU (0avgtext+0avgdata 0maxresident)k 0inputs+800outputs (0major+5587minor)pagefaults 0swaps Without: $ /usr/bin/time git grep void 5.36user 0.51system 0:05.87elapsed 100%CPU (0avgtext+0avgdata 0maxresident)k 0inputs+800outputs (0major+3693minor)pagefaults 0swaps In either case we gain about 40% by the threading. Signed-off-by: Fredrik Kuivinen <> Signed-off-by: Junio C Hamano <>
2010-01-25grep: expose "status-only" feature via -qJunio C Hamano
Teach "git grep" a new "-q" option to report the presense of a match via its exit status without showing any output, similar to how "grep -q" works. Internally "grep" engine already knew this "status-only" mode of operation because it needed to grep inside log message to filter commits when called from the "git log" machinery, and this patch only exposes it to the command line tool. A somewhat unfair benchmark in the Linux kernel directory shows a dramatic improvement: (with patch) $ time ../git.git/git grep -q linux HEAD ; echo $? real 0m0.030s user 0m0.004s sys 0m0.004s 0 (without patch) $ time git grep linux HEAD >/dev/null; echo $? real 0m4.432s user 0m4.272s sys 0m0.076s 0 This is "somewhat unfair" because I knew a file with such a string comes very early in the tree traversal (namely, ".gitignore"). Signed-off-by: Junio C Hamano <>
2010-01-20Merge branch 'jc/grep-lookahead'Junio C Hamano
* jc/grep-lookahead: grep --no-index: allow use of "git grep" outside a git repository grep: prepare to run outside of a work tree grep: rip out pessimization to use fixmatch() grep: rip out support for external grep grep: optimize built-in grep by skipping lines that do not hit Conflicts: builtin-grep.c t/
2010-01-19Merge branch 'maint'Junio C Hamano
* maint: Update draft release notes to grep: NUL terminate input from a file fast-import: tag may point to any object type
2010-01-19Merge branch 'jm/maint-1.6.5-grep-NUL-terminate' into maintJunio C Hamano
* jm/maint-1.6.5-grep-NUL-terminate: grep: NUL terminate input from a file
2010-01-19grep: NUL terminate input from a fileJim Meyering
Internally "git grep" runs regexec(3) that expects its input string to be NUL terminated. When searching inside blob data, read_sha1_file() automatically gives such a buffer, but builtin-grep.c forgot to put the NUL at the end, even though it allocated enough space for it. Signed-off-by: Jim Meyering <> Signed-off-by: Junio C Hamano <>
2010-01-15grep --no-index: allow use of "git grep" outside a git repositoryJunio C Hamano
Just like some people wanted diff features that are not found in other people's diff implementations outside of a git repository and added --no-index mode to the command, this adds --no-index mode to the "git grep" command. Also, inside a git repository, --no-index mode allows you to grep in untracked (but not ignored) files. Signed-off-by: Junio C Hamano <>
2010-01-15grep: prepare to run outside of a work treeJunio C Hamano
This moves the call to setup_git_directory() for running "grep" from the "git" wrapper to the implementation of the "grep" subcommand. A new variable "use_index" is always true at this stage in the series, and when it is on, we require that we are in a directory that is under git control. To make sure we die the same way, we make a second call into setup_git_directory() when we detect this situation. Signed-off-by: Junio C Hamano <>
2010-01-13Merge branch 'nd/sparse'Junio C Hamano
* nd/sparse: (25 commits) t7002: test for not using external grep on skip-worktree paths t7002: set test prerequisite "external-grep" if supported grep: do not do external grep on skip-worktree entries commit: correctly respect skip-worktree bit ie_match_stat(): do not ignore skip-worktree bit with CE_MATCH_IGNORE_VALID tests: rename duplicate t1009 sparse checkout: inhibit empty worktree Add tests for sparse checkout read-tree: add --no-sparse-checkout to disable sparse checkout support unpack-trees(): ignore worktree check outside checkout area unpack_trees(): apply $GIT_DIR/info/sparse-checkout to the final index unpack-trees(): "enable" sparse checkout and load $GIT_DIR/info/sparse-checkout unpack-trees.c: generalize verify_* functions unpack-trees(): add CE_WT_REMOVE to remove on worktree alone Introduce "sparse checkout" dir.c: export excluded_1() and add_excludes_from_file_1() excluded_1(): support exclude files in index unpack-trees(): carry skip-worktree bit over in merged_entry() Read .gitignore from index if it is skip-worktree Avoid writing to buffer in add_excludes_from_file_1() ... Conflicts: .gitignore Documentation/config.txt Documentation/git-update-index.txt Makefile entry.c t/
2010-01-13grep: rip out support for external grepJunio C Hamano
We still allow people to pass --[no-]ext-grep on the command line, but the option is ignored. Signed-off-by: Junio C Hamano <>
2010-01-12grep: -L should show empty filesJunio C Hamano
The -L (--files-without-match) option is supposed to show paths that produced no matches. When running the internal grep on work tree files, however, we had an optimization to just return on zero-sized files, without doing anything. This optimization doesn't matter too much in practice (a tracked empty file must be rare, or there is something wrong with your project); to produce results consistent with GNU grep, we should stop the optimization and show empty files as not having the given pattern. Signed-off-by: Junio C Hamano <>
2010-01-07t7002: test for not using external grep on skip-worktree pathsNguyễn Thái Ngọc Duy
Signed-off-by: Nguyễn Thái Ngọc Duy <> Signed-off-by: Junio C Hamano <>
2009-12-31grep: do not do external grep on skip-worktree entriesNguyễn Thái Ngọc Duy
Skip-worktree entries are not on disk. We cannot use external grep in such cases. Signed-off-by: Nguyễn Thái Ngọc Duy <> Signed-off-by: Junio C Hamano <>
2009-12-03Merge branch 'bc/grep-i-F' into maintJunio C Hamano
* bc/grep-i-F: grep: Allow case insensitive search of fixed-strings
2009-11-23Merge branch 'bc/grep-i-F'Junio C Hamano
* bc/grep-i-F: grep: Allow case insensitive search of fixed-strings
2009-11-23grep: unset GREP_OPTIONS before spawning external grepRené Scharfe
While we're at it, also unset GREP_COLOR and GREP_COLORS in case colouring is not enabled, to be on the safe side. The presence of these variables alone is not sufficient to trigger coloured output with GNU grep, but other implementations may behave differently. Signed-off-by: Rene Scharfe <> Signed-off-by: Junio C Hamano <>
2009-11-17grep: Allow case insensitive search of fixed-stringsBrian Collins
"git grep" currently an error when you combine the -F and -i flags. This isn't in line with how GNU grep handles it. This patch allows the simultaneous use of those flags. Signed-off-by: Jeff King <> Signed-off-by: Brian Collins <> Signed-off-by: Junio C Hamano <>
2009-11-10Show usage string for 'git grep -h'Jonathan Nieder
Clarification: the following description only talks about "git grep -h" without any other options and arguments. Such a change cannot be breaking backward compatibility. "grep -h" cannot be asking for suppressing filenames, as there is no match pattern specified. Signed-off-by: Jonathan Nieder <> Signed-off-by: Junio C Hamano <>
2009-10-17Merge branch 'maint-1.6.4' into maintJunio C Hamano
* maint-1.6.4: grep: do not segfault when -f is used
2009-10-17grep: do not segfault when -f is usedMatt Kraai
"git grep" would segfault if its -f option was used because it would try to use an uninitialized strbuf, so initialize the strbuf. Thanks to Johannes Sixt <> for the help with the test cases. Signed-off-by: Matt Kraai <> Signed-off-by: Junio C Hamano <>
2009-09-13Merge branch 'maint'Junio C Hamano
* maint: GIT svn: properly escape arguments for authors-prog http.c: remove verification of remote packs grep: accept relative paths outside current working directory grep: fix exit status if external_grep() punts Conflicts: GIT-VERSION-GEN RelNotes
2009-09-13Merge branch 'cb/maint-1.6.3-grep-relative-up' into maintJunio C Hamano
* cb/maint-1.6.3-grep-relative-up: grep: accept relative paths outside current working directory grep: fix exit status if external_grep() punts Conflicts: t/
2009-09-07grep: accept relative paths outside current working directoryClemens Buchacher
"git grep" would barf at relative paths pointing outside the current working directory (or subdirectories thereof). Use quote_path_relative(), which can handle such cases just fine. [jc: added tests.] Signed-off-by: Clemens Buchacher <> Signed-off-by: Junio C Hamano <>
2009-09-07grep: fix exit status if external_grep() puntsClemens Buchacher
If external_grep() is called and punts, grep_cache() mistakenly reported a hit, even if there were none. The bug can be triggered by calling "git grep --no-color" from a subdirectory. Signed-off-by: Clemens Buchacher <> Signed-off-by: Junio C Hamano <>
2009-08-24Teach Git to respect skip-worktree bit (reading part)Nguyễn Thái Ngọc Duy
grep: turn on --cached for files that is marked skip-worktree ls-files: do not check for deleted file that is marked skip-worktree update-index: ignore update request if it's skip-worktree, while still allows removing diff*: skip worktree version Signed-off-by: Nguyễn Thái Ngọc Duy <> Signed-off-by: Junio C Hamano <>
2009-07-23grep: Add --max-depth option.Michał Kiedrowicz
It is useful to grep directories non-recursively, e.g. when one wants to look for all files in the toplevel directory, but not in any subdirectory, or in Documentation/, but not in Documentation/technical/. This patch adds support for --max-depth <depth> option to git-grep. If it is given, git-grep descends at most <depth> levels of directories below paths specified on the command line. Note that if path specified on command line contains wildcards, this option makes no sense, e.g. $ git grep -l --max-depth 0 GNU -- 'contrib/*' (note the quotes) will search all files in contrib/, even in subdirectories, because '*' matches all files. Documentation updates, bash-completion and simple test cases are also provided. Signed-off-by: Michał Kiedrowicz <> Signed-off-by: Junio C Hamano <>
2009-07-09Merge branch 'rs/grep-p'Junio C Hamano
* rs/grep-p: grep: simplify -p output grep -p: support user defined regular expressions grep: add option -p/--show-function grep: handle pre context lines on demand grep: print context hunk marks between files grep: move context hunk mark handling into show_line() userdiff: add xdiff_clear_find_func()
2009-07-03grep: simplify -p outputRené Scharfe
It was found a bit too loud to show == separators between the function headers. Signed-off-by: Rene Scharfe <> Signed-off-by: Junio C Hamano <>
2009-07-02grep -p: support user defined regular expressionsRené Scharfe
Respect the userdiff attributes and config settings when looking for lines with function definitions in git grep -p. Signed-off-by: Rene Scharfe <> Signed-off-by: Junio C Hamano <>
2009-07-02grep: add option -p/--show-functionRené Scharfe
The new option -p instructs git grep to print the previous function definition as a context line, similar to diff -p. Such context lines are marked with an equal sign instead of a dash. This option complements the existing context options -A, -B, -C. Function definitions are detected using the same heuristic that diff uses. User defined regular expressions are not supported, yet. Signed-off-by: Rene Scharfe <> Signed-off-by: Junio C Hamano <>
2009-07-02grep: print context hunk marks between filesRené Scharfe
Print a hunk mark before matches from a new file are shown, in addition to the current behaviour of printing them if lines have been skipped. The result is easier to read, as (presumably unrelated) matches from different files are separated by a hunk mark. GNU grep does the same. Signed-off-by: Rene Scharfe <> Signed-off-by: Junio C Hamano <>
2009-06-27Convert existing die(..., strerror(errno)) to die_errno()Thomas Rast
Change calls to die(..., strerror(errno)) to use the new die_errno(). In the process, also make slight style adjustments: at least state _something_ about the function that failed (instead of just printing the pathname), and put paths in single quotes. Signed-off-by: Thomas Rast <> Signed-off-by: Junio C Hamano <>
2009-05-25parse-opts: prepare for OPT_FILENAMEStephen Boyd
To give OPT_FILENAME the prefix, we pass the prefix to parse_options() which passes the prefix to parse_options_start() which sets the prefix member of parse_opts_ctx accordingly. If there isn't a prefix in the calling context, passing NULL will suffice. Signed-off-by: Stephen Boyd <> Signed-off-by: Junio C Hamano <>
2009-05-21grep: make callback functions staticRené Scharfe
Suggested by Stephen Boyd: make the callback functions used for option parsing static. Signed-off-by: Rene Scharfe <> Signed-off-by: Junio C Hamano <>
2009-05-09grep: use parseoptRené Scharfe
Convert git-grep to parseopt. The bitfields in struct grep_opt are converted to full ints, increasing its size. This shouldn't be a problem as there is only a single instance in memory. Signed-off-by: Rene Scharfe <> Signed-off-by: Junio C Hamano <>
2009-05-09grep: remove global variable builtin_grepRené Scharfe
Replace the only global variable in builtin-grep.c, builtin_grep, by a local one and a function parameter with reversed meaning. Signed-off-by: Rene Scharfe <> Signed-off-by: Junio C Hamano <>
2009-04-21grep: don't support "grep.color"-like config optionsMarkus Heidelberg
color.grep and color.grep.* is the official and documented way to highlight grep matches. Comparable options like diff.color.* and status.color.* exist for backward compatibility reasons only and are not documented any more. Signed-off-by: Markus Heidelberg <> Signed-off-by: Junio C Hamano <>
2009-03-18grep: prefer builtin over external one when coloring resultsNguyễn Thái Ngọc Duy
As far as I know, not all grep programs support coloring, so we should rely on builtin grep. If you want external grep, set color.grep.external to empty string. Signed-off-by: Nguyễn Thái Ngọc Duy <> Signed-off-by: Junio C Hamano <>
2009-03-07grep: add support for coloring with external grepsRené Scharfe
Add the config variable color.grep.external, which can be used to switch on coloring of external greps. To enable auto coloring with GNU grep, one needs to set color.grep.external to --color=always to defeat the pager started by git grep. The value of the config variable will be passed to the external grep only if it would colorize internal grep's output, so automatic terminal detected works. The default is to not pass any option, because the external grep command could be a program without color support. Also set the environment variables GREP_COLOR and GREP_COLORS to pass the configured color for matches to the external grep. This works with GNU grep; other variables could be added as needed. Signed-off-by: Rene Scharfe <> Signed-off-by: Junio C Hamano <>
2009-03-07grep: color patterns in outputRené Scharfe
Coloring matches makes them easier to spot in the output. Add two options and two parameters: color.grep (to turn coloring on or off), color.grep.match (to set the color of matches), --color and --no-color (to turn coloring on or off, respectively). The output of external greps is not changed. This patch is based on earlier ones by Nguyễn Thái Ngọc Duy and Thiago Alves. Signed-off-by: Rene Scharfe <> Signed-off-by: Junio C Hamano <>
2009-02-03Merge branch 'maint-1.6.0' into maintJunio C Hamano
* maint-1.6.0: grep: pass -I (ignore binary) down to external grep