Merge branch 'nd/no-the-index'
[gitweb.git] / builtin / grep.c
index f9678f19e4aff52ae82ee1ff5d9842950b943f43..601f801158f097b302dcf6615016bb4cefbc0225 100644 (file)
@@ -489,7 +489,7 @@ static int grep_cache(struct grep_opt *opt, struct repository *repo,
        }
 
        if (repo_read_index(repo) < 0)
-               die("index file corrupt");
+               die(_("index file corrupt"));
 
        for (nr = 0; nr < repo->index->cache_nr; nr++) {
                const struct cache_entry *ce = repo->index->cache[nr];
@@ -497,7 +497,7 @@ static int grep_cache(struct grep_opt *opt, struct repository *repo,
                strbuf_addstr(&name, ce->name);
 
                if (S_ISREG(ce->ce_mode) &&
-                   match_pathspec(pathspec, name.buf, name.len, 0, NULL,
+                   match_pathspec(repo->index, pathspec, name.buf, name.len, 0, NULL,
                                   S_ISDIR(ce->ce_mode) ||
                                   S_ISGITLINK(ce->ce_mode))) {
                        /*
@@ -515,7 +515,7 @@ static int grep_cache(struct grep_opt *opt, struct repository *repo,
                                hit |= grep_file(opt, name.buf);
                        }
                } else if (recurse_submodules && S_ISGITLINK(ce->ce_mode) &&
-                          submodule_path_match(pathspec, name.buf, NULL)) {
+                          submodule_path_match(repo->index, pathspec, name.buf, NULL)) {
                        hit |= grep_submodule(opt, repo, pathspec, NULL, ce->name, ce->name);
                } else {
                        continue;
@@ -679,7 +679,7 @@ static int grep_directory(struct grep_opt *opt, const struct pathspec *pathspec,
 
        fill_directory(&dir, &the_index, pathspec);
        for (i = 0; i < dir.nr; i++) {
-               if (!dir_path_match(dir.entries[i], pathspec, 0, NULL))
+               if (!dir_path_match(&the_index, dir.entries[i], pathspec, 0, NULL))
                        continue;
                hit |= grep_file(opt, dir.entries[i]->name);
                if (hit && opt->status_only)
@@ -844,6 +844,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
                OPT_BOOL_F('z', "null", &opt.null_following_name,
                           N_("print NUL after filenames"),
                           PARSE_OPT_NOCOMPLETE),
+               OPT_BOOL('o', "only-matching", &opt.only_matching,
+                       N_("show only matching parts of a line")),
                OPT_BOOL('c', "count", &opt.count,
                        N_("show the number of matches instead of matching lines")),
                OPT__COLOR(&opt.color, N_("highlight matches")),
@@ -961,7 +963,11 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
        }
 
        if (!opt.pattern_list)
-               die(_("no pattern given."));
+               die(_("no pattern given"));
+
+       /* --only-matching has no effect with --invert. */
+       if (opt.invert)
+               opt.only_matching = 0;
 
        /*
         * We have to find "--" in a separate pass, because its presence
@@ -1087,19 +1093,19 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
        }
 
        if (recurse_submodules && (!use_index || untracked))
-               die(_("option not supported with --recurse-submodules."));
+               die(_("option not supported with --recurse-submodules"));
 
        if (!show_in_pager && !opt.status_only)
                setup_pager();
 
        if (!use_index && (untracked || cached))
-               die(_("--cached or --untracked cannot be used with --no-index."));
+               die(_("--cached or --untracked cannot be used with --no-index"));
 
        if (!use_index || untracked) {
                int use_exclude = (opt_exclude < 0) ? use_index : !!opt_exclude;
                hit = grep_directory(&opt, &pathspec, use_exclude, use_index);
        } else if (0 <= opt_exclude) {
-               die(_("--[no-]exclude-standard cannot be used for tracked contents."));
+               die(_("--[no-]exclude-standard cannot be used for tracked contents"));
        } else if (!list.nr) {
                if (!cached)
                        setup_work_tree();
@@ -1107,7 +1113,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
                hit = grep_cache(&opt, the_repository, &pathspec, cached);
        } else {
                if (cached)
-                       die(_("both --cached and trees are given."));
+                       die(_("both --cached and trees are given"));
 
                hit = grep_objects(&opt, &pathspec, &list);
        }