builtin / grep.con commit builtin/grep.c: make configuration callback more reusable (15fabd1)
   1/*
   2 * Builtin "git grep"
   3 *
   4 * Copyright (c) 2006 Junio C Hamano
   5 */
   6#include "cache.h"
   7#include "blob.h"
   8#include "tree.h"
   9#include "commit.h"
  10#include "tag.h"
  11#include "tree-walk.h"
  12#include "builtin.h"
  13#include "parse-options.h"
  14#include "string-list.h"
  15#include "run-command.h"
  16#include "userdiff.h"
  17#include "grep.h"
  18#include "quote.h"
  19#include "dir.h"
  20
  21static char const * const grep_usage[] = {
  22        N_("git grep [options] [-e] <pattern> [<rev>...] [[--] <path>...]"),
  23        NULL
  24};
  25
  26static int use_threads = 1;
  27
  28#ifndef NO_PTHREADS
  29#define THREADS 8
  30static pthread_t threads[THREADS];
  31
  32/* We use one producer thread and THREADS consumer
  33 * threads. The producer adds struct work_items to 'todo' and the
  34 * consumers pick work items from the same array.
  35 */
  36struct work_item {
  37        struct grep_source source;
  38        char done;
  39        struct strbuf out;
  40};
  41
  42/* In the range [todo_done, todo_start) in 'todo' we have work_items
  43 * that have been or are processed by a consumer thread. We haven't
  44 * written the result for these to stdout yet.
  45 *
  46 * The work_items in [todo_start, todo_end) are waiting to be picked
  47 * up by a consumer thread.
  48 *
  49 * The ranges are modulo TODO_SIZE.
  50 */
  51#define TODO_SIZE 128
  52static struct work_item todo[TODO_SIZE];
  53static int todo_start;
  54static int todo_end;
  55static int todo_done;
  56
  57/* Has all work items been added? */
  58static int all_work_added;
  59
  60/* This lock protects all the variables above. */
  61static pthread_mutex_t grep_mutex;
  62
  63static inline void grep_lock(void)
  64{
  65        if (use_threads)
  66                pthread_mutex_lock(&grep_mutex);
  67}
  68
  69static inline void grep_unlock(void)
  70{
  71        if (use_threads)
  72                pthread_mutex_unlock(&grep_mutex);
  73}
  74
  75/* Signalled when a new work_item is added to todo. */
  76static pthread_cond_t cond_add;
  77
  78/* Signalled when the result from one work_item is written to
  79 * stdout.
  80 */
  81static pthread_cond_t cond_write;
  82
  83/* Signalled when we are finished with everything. */
  84static pthread_cond_t cond_result;
  85
  86static int skip_first_line;
  87
  88static void add_work(struct grep_opt *opt, enum grep_source_type type,
  89                     const char *name, const void *id)
  90{
  91        grep_lock();
  92
  93        while ((todo_end+1) % ARRAY_SIZE(todo) == todo_done) {
  94                pthread_cond_wait(&cond_write, &grep_mutex);
  95        }
  96
  97        grep_source_init(&todo[todo_end].source, type, name, id);
  98        if (opt->binary != GREP_BINARY_TEXT)
  99                grep_source_load_driver(&todo[todo_end].source);
 100        todo[todo_end].done = 0;
 101        strbuf_reset(&todo[todo_end].out);
 102        todo_end = (todo_end + 1) % ARRAY_SIZE(todo);
 103
 104        pthread_cond_signal(&cond_add);
 105        grep_unlock();
 106}
 107
 108static struct work_item *get_work(void)
 109{
 110        struct work_item *ret;
 111
 112        grep_lock();
 113        while (todo_start == todo_end && !all_work_added) {
 114                pthread_cond_wait(&cond_add, &grep_mutex);
 115        }
 116
 117        if (todo_start == todo_end && all_work_added) {
 118                ret = NULL;
 119        } else {
 120                ret = &todo[todo_start];
 121                todo_start = (todo_start + 1) % ARRAY_SIZE(todo);
 122        }
 123        grep_unlock();
 124        return ret;
 125}
 126
 127static void work_done(struct work_item *w)
 128{
 129        int old_done;
 130
 131        grep_lock();
 132        w->done = 1;
 133        old_done = todo_done;
 134        for(; todo[todo_done].done && todo_done != todo_start;
 135            todo_done = (todo_done+1) % ARRAY_SIZE(todo)) {
 136                w = &todo[todo_done];
 137                if (w->out.len) {
 138                        const char *p = w->out.buf;
 139                        size_t len = w->out.len;
 140
 141                        /* Skip the leading hunk mark of the first file. */
 142                        if (skip_first_line) {
 143                                while (len) {
 144                                        len--;
 145                                        if (*p++ == '\n')
 146                                                break;
 147                                }
 148                                skip_first_line = 0;
 149                        }
 150
 151                        write_or_die(1, p, len);
 152                }
 153                grep_source_clear(&w->source);
 154        }
 155
 156        if (old_done != todo_done)
 157                pthread_cond_signal(&cond_write);
 158
 159        if (all_work_added && todo_done == todo_end)
 160                pthread_cond_signal(&cond_result);
 161
 162        grep_unlock();
 163}
 164
 165static void *run(void *arg)
 166{
 167        int hit = 0;
 168        struct grep_opt *opt = arg;
 169
 170        while (1) {
 171                struct work_item *w = get_work();
 172                if (!w)
 173                        break;
 174
 175                opt->output_priv = w;
 176                hit |= grep_source(opt, &w->source);
 177                grep_source_clear_data(&w->source);
 178                work_done(w);
 179        }
 180        free_grep_patterns(arg);
 181        free(arg);
 182
 183        return (void*) (intptr_t) hit;
 184}
 185
 186static void strbuf_out(struct grep_opt *opt, const void *buf, size_t size)
 187{
 188        struct work_item *w = opt->output_priv;
 189        strbuf_add(&w->out, buf, size);
 190}
 191
 192static void start_threads(struct grep_opt *opt)
 193{
 194        int i;
 195
 196        pthread_mutex_init(&grep_mutex, NULL);
 197        pthread_mutex_init(&grep_read_mutex, NULL);
 198        pthread_mutex_init(&grep_attr_mutex, NULL);
 199        pthread_cond_init(&cond_add, NULL);
 200        pthread_cond_init(&cond_write, NULL);
 201        pthread_cond_init(&cond_result, NULL);
 202        grep_use_locks = 1;
 203
 204        for (i = 0; i < ARRAY_SIZE(todo); i++) {
 205                strbuf_init(&todo[i].out, 0);
 206        }
 207
 208        for (i = 0; i < ARRAY_SIZE(threads); i++) {
 209                int err;
 210                struct grep_opt *o = grep_opt_dup(opt);
 211                o->output = strbuf_out;
 212                o->debug = 0;
 213                compile_grep_patterns(o);
 214                err = pthread_create(&threads[i], NULL, run, o);
 215
 216                if (err)
 217                        die(_("grep: failed to create thread: %s"),
 218                            strerror(err));
 219        }
 220}
 221
 222static int wait_all(void)
 223{
 224        int hit = 0;
 225        int i;
 226
 227        grep_lock();
 228        all_work_added = 1;
 229
 230        /* Wait until all work is done. */
 231        while (todo_done != todo_end)
 232                pthread_cond_wait(&cond_result, &grep_mutex);
 233
 234        /* Wake up all the consumer threads so they can see that there
 235         * is no more work to do.
 236         */
 237        pthread_cond_broadcast(&cond_add);
 238        grep_unlock();
 239
 240        for (i = 0; i < ARRAY_SIZE(threads); i++) {
 241                void *h;
 242                pthread_join(threads[i], &h);
 243                hit |= (int) (intptr_t) h;
 244        }
 245
 246        pthread_mutex_destroy(&grep_mutex);
 247        pthread_mutex_destroy(&grep_read_mutex);
 248        pthread_mutex_destroy(&grep_attr_mutex);
 249        pthread_cond_destroy(&cond_add);
 250        pthread_cond_destroy(&cond_write);
 251        pthread_cond_destroy(&cond_result);
 252        grep_use_locks = 0;
 253
 254        return hit;
 255}
 256#else /* !NO_PTHREADS */
 257
 258static int wait_all(void)
 259{
 260        return 0;
 261}
 262#endif
 263
 264static int parse_pattern_type_arg(const char *opt, const char *arg)
 265{
 266        if (!strcmp(arg, "default"))
 267                return GREP_PATTERN_TYPE_UNSPECIFIED;
 268        else if (!strcmp(arg, "basic"))
 269                return GREP_PATTERN_TYPE_BRE;
 270        else if (!strcmp(arg, "extended"))
 271                return GREP_PATTERN_TYPE_ERE;
 272        else if (!strcmp(arg, "fixed"))
 273                return GREP_PATTERN_TYPE_FIXED;
 274        else if (!strcmp(arg, "perl"))
 275                return GREP_PATTERN_TYPE_PCRE;
 276        die("bad %s argument: %s", opt, arg);
 277}
 278
 279static void grep_pattern_type_options(const int pattern_type, struct grep_opt *opt)
 280{
 281        switch (pattern_type) {
 282        case GREP_PATTERN_TYPE_UNSPECIFIED:
 283                /* fall through */
 284
 285        case GREP_PATTERN_TYPE_BRE:
 286                opt->fixed = 0;
 287                opt->pcre = 0;
 288                opt->regflags &= ~REG_EXTENDED;
 289                break;
 290
 291        case GREP_PATTERN_TYPE_ERE:
 292                opt->fixed = 0;
 293                opt->pcre = 0;
 294                opt->regflags |= REG_EXTENDED;
 295                break;
 296
 297        case GREP_PATTERN_TYPE_FIXED:
 298                opt->fixed = 1;
 299                opt->pcre = 0;
 300                opt->regflags &= ~REG_EXTENDED;
 301                break;
 302
 303        case GREP_PATTERN_TYPE_PCRE:
 304                opt->fixed = 0;
 305                opt->pcre = 1;
 306                opt->regflags &= ~REG_EXTENDED;
 307                break;
 308        }
 309}
 310
 311static struct grep_opt grep_defaults;
 312
 313/*
 314 * Initialize the grep_defaults template with hardcoded defaults.
 315 * We could let the compiler do this, but without C99 initializers
 316 * the code gets unwieldy and unreadable, so...
 317 */
 318static void init_grep_defaults(void)
 319{
 320        struct grep_opt *opt = &grep_defaults;
 321
 322        memset(opt, 0, sizeof(*opt));
 323        opt->relative = 1;
 324        opt->pathname = 1;
 325        opt->regflags = REG_NEWLINE;
 326        opt->max_depth = -1;
 327        opt->pattern_type_option = GREP_PATTERN_TYPE_UNSPECIFIED;
 328        opt->extended_regexp_option = 0;
 329        strcpy(opt->color_context, "");
 330        strcpy(opt->color_filename, "");
 331        strcpy(opt->color_function, "");
 332        strcpy(opt->color_lineno, "");
 333        strcpy(opt->color_match, GIT_COLOR_BOLD_RED);
 334        strcpy(opt->color_selected, "");
 335        strcpy(opt->color_sep, GIT_COLOR_CYAN);
 336        opt->color = -1;
 337}
 338
 339/*
 340 * Read the configuration file once and store it in
 341 * the grep_defaults template.
 342 */
 343static int grep_config(const char *var, const char *value, void *cb)
 344{
 345        struct grep_opt *opt = &grep_defaults;
 346        char *color = NULL;
 347
 348        if (userdiff_config(var, value) < 0)
 349                return -1;
 350
 351        if (!strcmp(var, "grep.extendedregexp")) {
 352                if (git_config_bool(var, value))
 353                        opt->extended_regexp_option = 1;
 354                else
 355                        opt->extended_regexp_option = 0;
 356                return 0;
 357        }
 358
 359        if (!strcmp(var, "grep.patterntype")) {
 360                opt->pattern_type_option = parse_pattern_type_arg(var, value);
 361                return 0;
 362        }
 363
 364        if (!strcmp(var, "grep.linenumber")) {
 365                opt->linenum = git_config_bool(var, value);
 366                return 0;
 367        }
 368
 369        if (!strcmp(var, "color.grep"))
 370                opt->color = git_config_colorbool(var, value);
 371        else if (!strcmp(var, "color.grep.context"))
 372                color = opt->color_context;
 373        else if (!strcmp(var, "color.grep.filename"))
 374                color = opt->color_filename;
 375        else if (!strcmp(var, "color.grep.function"))
 376                color = opt->color_function;
 377        else if (!strcmp(var, "color.grep.linenumber"))
 378                color = opt->color_lineno;
 379        else if (!strcmp(var, "color.grep.match"))
 380                color = opt->color_match;
 381        else if (!strcmp(var, "color.grep.selected"))
 382                color = opt->color_selected;
 383        else if (!strcmp(var, "color.grep.separator"))
 384                color = opt->color_sep;
 385
 386        if (color) {
 387                if (!value)
 388                        return config_error_nonbool(var);
 389                color_parse(value, var, color);
 390        }
 391        return 0;
 392}
 393
 394/*
 395 * Initialize one instance of grep_opt and copy the
 396 * default values from the template we read the configuration
 397 * information in an earlier call to git_config(grep_config).
 398 */
 399static void grep_init(struct grep_opt *opt, const char *prefix)
 400{
 401        struct grep_opt *def = &grep_defaults;
 402
 403        memset(opt, 0, sizeof(*opt));
 404        opt->prefix = prefix;
 405        opt->prefix_length = (prefix && *prefix) ? strlen(prefix) : 0;
 406        opt->pattern_tail = &opt->pattern_list;
 407        opt->header_tail = &opt->header_list;
 408
 409        opt->color = def->color;
 410        opt->extended_regexp_option = def->extended_regexp_option;
 411        opt->pattern_type_option = def->pattern_type_option;
 412        opt->linenum = def->linenum;
 413        opt->max_depth = def->max_depth;
 414        opt->pathname = def->pathname;
 415        opt->regflags = def->regflags;
 416        opt->relative = def->relative;
 417
 418        strcpy(opt->color_context, def->color_context);
 419        strcpy(opt->color_filename, def->color_filename);
 420        strcpy(opt->color_function, def->color_function);
 421        strcpy(opt->color_lineno, def->color_lineno);
 422        strcpy(opt->color_match, def->color_match);
 423        strcpy(opt->color_selected, def->color_selected);
 424        strcpy(opt->color_sep, def->color_sep);
 425}
 426
 427static int grep_cmd_config(const char *var, const char *value, void *cb)
 428{
 429        int st = grep_config(var, value, cb);
 430        if (git_color_default_config(var, value, cb) < 0)
 431                st = -1;
 432        return st;
 433}
 434
 435static void *lock_and_read_sha1_file(const unsigned char *sha1, enum object_type *type, unsigned long *size)
 436{
 437        void *data;
 438
 439        grep_read_lock();
 440        data = read_sha1_file(sha1, type, size);
 441        grep_read_unlock();
 442        return data;
 443}
 444
 445static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1,
 446                     const char *filename, int tree_name_len)
 447{
 448        struct strbuf pathbuf = STRBUF_INIT;
 449
 450        if (opt->relative && opt->prefix_length) {
 451                quote_path_relative(filename + tree_name_len, -1, &pathbuf,
 452                                    opt->prefix);
 453                strbuf_insert(&pathbuf, 0, filename, tree_name_len);
 454        } else {
 455                strbuf_addstr(&pathbuf, filename);
 456        }
 457
 458#ifndef NO_PTHREADS
 459        if (use_threads) {
 460                add_work(opt, GREP_SOURCE_SHA1, pathbuf.buf, sha1);
 461                strbuf_release(&pathbuf);
 462                return 0;
 463        } else
 464#endif
 465        {
 466                struct grep_source gs;
 467                int hit;
 468
 469                grep_source_init(&gs, GREP_SOURCE_SHA1, pathbuf.buf, sha1);
 470                strbuf_release(&pathbuf);
 471                hit = grep_source(opt, &gs);
 472
 473                grep_source_clear(&gs);
 474                return hit;
 475        }
 476}
 477
 478static int grep_file(struct grep_opt *opt, const char *filename)
 479{
 480        struct strbuf buf = STRBUF_INIT;
 481
 482        if (opt->relative && opt->prefix_length)
 483                quote_path_relative(filename, -1, &buf, opt->prefix);
 484        else
 485                strbuf_addstr(&buf, filename);
 486
 487#ifndef NO_PTHREADS
 488        if (use_threads) {
 489                add_work(opt, GREP_SOURCE_FILE, buf.buf, filename);
 490                strbuf_release(&buf);
 491                return 0;
 492        } else
 493#endif
 494        {
 495                struct grep_source gs;
 496                int hit;
 497
 498                grep_source_init(&gs, GREP_SOURCE_FILE, buf.buf, filename);
 499                strbuf_release(&buf);
 500                hit = grep_source(opt, &gs);
 501
 502                grep_source_clear(&gs);
 503                return hit;
 504        }
 505}
 506
 507static void append_path(struct grep_opt *opt, const void *data, size_t len)
 508{
 509        struct string_list *path_list = opt->output_priv;
 510
 511        if (len == 1 && *(const char *)data == '\0')
 512                return;
 513        string_list_append(path_list, xstrndup(data, len));
 514}
 515
 516static void run_pager(struct grep_opt *opt, const char *prefix)
 517{
 518        struct string_list *path_list = opt->output_priv;
 519        const char **argv = xmalloc(sizeof(const char *) * (path_list->nr + 1));
 520        int i, status;
 521
 522        for (i = 0; i < path_list->nr; i++)
 523                argv[i] = path_list->items[i].string;
 524        argv[path_list->nr] = NULL;
 525
 526        if (prefix && chdir(prefix))
 527                die(_("Failed to chdir: %s"), prefix);
 528        status = run_command_v_opt(argv, RUN_USING_SHELL);
 529        if (status)
 530                exit(status);
 531        free(argv);
 532}
 533
 534static int grep_cache(struct grep_opt *opt, const struct pathspec *pathspec, int cached)
 535{
 536        int hit = 0;
 537        int nr;
 538        read_cache();
 539
 540        for (nr = 0; nr < active_nr; nr++) {
 541                struct cache_entry *ce = active_cache[nr];
 542                if (!S_ISREG(ce->ce_mode))
 543                        continue;
 544                if (!match_pathspec_depth(pathspec, ce->name, ce_namelen(ce), 0, NULL))
 545                        continue;
 546                /*
 547                 * If CE_VALID is on, we assume worktree file and its cache entry
 548                 * are identical, even if worktree file has been modified, so use
 549                 * cache version instead
 550                 */
 551                if (cached || (ce->ce_flags & CE_VALID) || ce_skip_worktree(ce)) {
 552                        if (ce_stage(ce))
 553                                continue;
 554                        hit |= grep_sha1(opt, ce->sha1, ce->name, 0);
 555                }
 556                else
 557                        hit |= grep_file(opt, ce->name);
 558                if (ce_stage(ce)) {
 559                        do {
 560                                nr++;
 561                        } while (nr < active_nr &&
 562                                 !strcmp(ce->name, active_cache[nr]->name));
 563                        nr--; /* compensate for loop control */
 564                }
 565                if (hit && opt->status_only)
 566                        break;
 567        }
 568        return hit;
 569}
 570
 571static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
 572                     struct tree_desc *tree, struct strbuf *base, int tn_len)
 573{
 574        int hit = 0;
 575        enum interesting match = entry_not_interesting;
 576        struct name_entry entry;
 577        int old_baselen = base->len;
 578
 579        while (tree_entry(tree, &entry)) {
 580                int te_len = tree_entry_len(&entry);
 581
 582                if (match != all_entries_interesting) {
 583                        match = tree_entry_interesting(&entry, base, tn_len, pathspec);
 584                        if (match == all_entries_not_interesting)
 585                                break;
 586                        if (match == entry_not_interesting)
 587                                continue;
 588                }
 589
 590                strbuf_add(base, entry.path, te_len);
 591
 592                if (S_ISREG(entry.mode)) {
 593                        hit |= grep_sha1(opt, entry.sha1, base->buf, tn_len);
 594                }
 595                else if (S_ISDIR(entry.mode)) {
 596                        enum object_type type;
 597                        struct tree_desc sub;
 598                        void *data;
 599                        unsigned long size;
 600
 601                        data = lock_and_read_sha1_file(entry.sha1, &type, &size);
 602                        if (!data)
 603                                die(_("unable to read tree (%s)"),
 604                                    sha1_to_hex(entry.sha1));
 605
 606                        strbuf_addch(base, '/');
 607                        init_tree_desc(&sub, data, size);
 608                        hit |= grep_tree(opt, pathspec, &sub, base, tn_len);
 609                        free(data);
 610                }
 611                strbuf_setlen(base, old_baselen);
 612
 613                if (hit && opt->status_only)
 614                        break;
 615        }
 616        return hit;
 617}
 618
 619static int grep_object(struct grep_opt *opt, const struct pathspec *pathspec,
 620                       struct object *obj, const char *name)
 621{
 622        if (obj->type == OBJ_BLOB)
 623                return grep_sha1(opt, obj->sha1, name, 0);
 624        if (obj->type == OBJ_COMMIT || obj->type == OBJ_TREE) {
 625                struct tree_desc tree;
 626                void *data;
 627                unsigned long size;
 628                struct strbuf base;
 629                int hit, len;
 630
 631                grep_read_lock();
 632                data = read_object_with_reference(obj->sha1, tree_type,
 633                                                  &size, NULL);
 634                grep_read_unlock();
 635
 636                if (!data)
 637                        die(_("unable to read tree (%s)"), sha1_to_hex(obj->sha1));
 638
 639                len = name ? strlen(name) : 0;
 640                strbuf_init(&base, PATH_MAX + len + 1);
 641                if (len) {
 642                        strbuf_add(&base, name, len);
 643                        strbuf_addch(&base, ':');
 644                }
 645                init_tree_desc(&tree, data, size);
 646                hit = grep_tree(opt, pathspec, &tree, &base, base.len);
 647                strbuf_release(&base);
 648                free(data);
 649                return hit;
 650        }
 651        die(_("unable to grep from object of type %s"), typename(obj->type));
 652}
 653
 654static int grep_objects(struct grep_opt *opt, const struct pathspec *pathspec,
 655                        const struct object_array *list)
 656{
 657        unsigned int i;
 658        int hit = 0;
 659        const unsigned int nr = list->nr;
 660
 661        for (i = 0; i < nr; i++) {
 662                struct object *real_obj;
 663                real_obj = deref_tag(list->objects[i].item, NULL, 0);
 664                if (grep_object(opt, pathspec, real_obj, list->objects[i].name)) {
 665                        hit = 1;
 666                        if (opt->status_only)
 667                                break;
 668                }
 669        }
 670        return hit;
 671}
 672
 673static int grep_directory(struct grep_opt *opt, const struct pathspec *pathspec,
 674                          int exc_std)
 675{
 676        struct dir_struct dir;
 677        int i, hit = 0;
 678
 679        memset(&dir, 0, sizeof(dir));
 680        if (exc_std)
 681                setup_standard_excludes(&dir);
 682
 683        fill_directory(&dir, pathspec->raw);
 684        for (i = 0; i < dir.nr; i++) {
 685                const char *name = dir.entries[i]->name;
 686                int namelen = strlen(name);
 687                if (!match_pathspec_depth(pathspec, name, namelen, 0, NULL))
 688                        continue;
 689                hit |= grep_file(opt, dir.entries[i]->name);
 690                if (hit && opt->status_only)
 691                        break;
 692        }
 693        return hit;
 694}
 695
 696static int context_callback(const struct option *opt, const char *arg,
 697                            int unset)
 698{
 699        struct grep_opt *grep_opt = opt->value;
 700        int value;
 701        const char *endp;
 702
 703        if (unset) {
 704                grep_opt->pre_context = grep_opt->post_context = 0;
 705                return 0;
 706        }
 707        value = strtol(arg, (char **)&endp, 10);
 708        if (*endp) {
 709                return error(_("switch `%c' expects a numerical value"),
 710                             opt->short_name);
 711        }
 712        grep_opt->pre_context = grep_opt->post_context = value;
 713        return 0;
 714}
 715
 716static int file_callback(const struct option *opt, const char *arg, int unset)
 717{
 718        struct grep_opt *grep_opt = opt->value;
 719        int from_stdin = !strcmp(arg, "-");
 720        FILE *patterns;
 721        int lno = 0;
 722        struct strbuf sb = STRBUF_INIT;
 723
 724        patterns = from_stdin ? stdin : fopen(arg, "r");
 725        if (!patterns)
 726                die_errno(_("cannot open '%s'"), arg);
 727        while (strbuf_getline(&sb, patterns, '\n') == 0) {
 728                /* ignore empty line like grep does */
 729                if (sb.len == 0)
 730                        continue;
 731
 732                append_grep_pat(grep_opt, sb.buf, sb.len, arg, ++lno,
 733                                GREP_PATTERN);
 734        }
 735        if (!from_stdin)
 736                fclose(patterns);
 737        strbuf_release(&sb);
 738        return 0;
 739}
 740
 741static int not_callback(const struct option *opt, const char *arg, int unset)
 742{
 743        struct grep_opt *grep_opt = opt->value;
 744        append_grep_pattern(grep_opt, "--not", "command line", 0, GREP_NOT);
 745        return 0;
 746}
 747
 748static int and_callback(const struct option *opt, const char *arg, int unset)
 749{
 750        struct grep_opt *grep_opt = opt->value;
 751        append_grep_pattern(grep_opt, "--and", "command line", 0, GREP_AND);
 752        return 0;
 753}
 754
 755static int open_callback(const struct option *opt, const char *arg, int unset)
 756{
 757        struct grep_opt *grep_opt = opt->value;
 758        append_grep_pattern(grep_opt, "(", "command line", 0, GREP_OPEN_PAREN);
 759        return 0;
 760}
 761
 762static int close_callback(const struct option *opt, const char *arg, int unset)
 763{
 764        struct grep_opt *grep_opt = opt->value;
 765        append_grep_pattern(grep_opt, ")", "command line", 0, GREP_CLOSE_PAREN);
 766        return 0;
 767}
 768
 769static int pattern_callback(const struct option *opt, const char *arg,
 770                            int unset)
 771{
 772        struct grep_opt *grep_opt = opt->value;
 773        append_grep_pattern(grep_opt, arg, "-e option", 0, GREP_PATTERN);
 774        return 0;
 775}
 776
 777static int help_callback(const struct option *opt, const char *arg, int unset)
 778{
 779        return -1;
 780}
 781
 782int cmd_grep(int argc, const char **argv, const char *prefix)
 783{
 784        int hit = 0;
 785        int cached = 0, untracked = 0, opt_exclude = -1;
 786        int seen_dashdash = 0;
 787        int external_grep_allowed__ignored;
 788        const char *show_in_pager = NULL, *default_pager = "dummy";
 789        struct grep_opt opt;
 790        struct object_array list = OBJECT_ARRAY_INIT;
 791        const char **paths = NULL;
 792        struct pathspec pathspec;
 793        struct string_list path_list = STRING_LIST_INIT_NODUP;
 794        int i;
 795        int dummy;
 796        int use_index = 1;
 797        int pattern_type_arg = GREP_PATTERN_TYPE_UNSPECIFIED;
 798
 799        struct option options[] = {
 800                OPT_BOOLEAN(0, "cached", &cached,
 801                        N_("search in index instead of in the work tree")),
 802                OPT_NEGBIT(0, "no-index", &use_index,
 803                         N_("find in contents not managed by git"), 1),
 804                OPT_BOOLEAN(0, "untracked", &untracked,
 805                        N_("search in both tracked and untracked files")),
 806                OPT_SET_INT(0, "exclude-standard", &opt_exclude,
 807                            N_("search also in ignored files"), 1),
 808                OPT_GROUP(""),
 809                OPT_BOOLEAN('v', "invert-match", &opt.invert,
 810                        N_("show non-matching lines")),
 811                OPT_BOOLEAN('i', "ignore-case", &opt.ignore_case,
 812                        N_("case insensitive matching")),
 813                OPT_BOOLEAN('w', "word-regexp", &opt.word_regexp,
 814                        N_("match patterns only at word boundaries")),
 815                OPT_SET_INT('a', "text", &opt.binary,
 816                        N_("process binary files as text"), GREP_BINARY_TEXT),
 817                OPT_SET_INT('I', NULL, &opt.binary,
 818                        N_("don't match patterns in binary files"),
 819                        GREP_BINARY_NOMATCH),
 820                { OPTION_INTEGER, 0, "max-depth", &opt.max_depth, N_("depth"),
 821                        N_("descend at most <depth> levels"), PARSE_OPT_NONEG,
 822                        NULL, 1 },
 823                OPT_GROUP(""),
 824                OPT_SET_INT('E', "extended-regexp", &pattern_type_arg,
 825                            N_("use extended POSIX regular expressions"),
 826                            GREP_PATTERN_TYPE_ERE),
 827                OPT_SET_INT('G', "basic-regexp", &pattern_type_arg,
 828                            N_("use basic POSIX regular expressions (default)"),
 829                            GREP_PATTERN_TYPE_BRE),
 830                OPT_SET_INT('F', "fixed-strings", &pattern_type_arg,
 831                            N_("interpret patterns as fixed strings"),
 832                            GREP_PATTERN_TYPE_FIXED),
 833                OPT_SET_INT('P', "perl-regexp", &pattern_type_arg,
 834                            N_("use Perl-compatible regular expressions"),
 835                            GREP_PATTERN_TYPE_PCRE),
 836                OPT_GROUP(""),
 837                OPT_BOOLEAN('n', "line-number", &opt.linenum, N_("show line numbers")),
 838                OPT_NEGBIT('h', NULL, &opt.pathname, N_("don't show filenames"), 1),
 839                OPT_BIT('H', NULL, &opt.pathname, N_("show filenames"), 1),
 840                OPT_NEGBIT(0, "full-name", &opt.relative,
 841                        N_("show filenames relative to top directory"), 1),
 842                OPT_BOOLEAN('l', "files-with-matches", &opt.name_only,
 843                        N_("show only filenames instead of matching lines")),
 844                OPT_BOOLEAN(0, "name-only", &opt.name_only,
 845                        N_("synonym for --files-with-matches")),
 846                OPT_BOOLEAN('L', "files-without-match",
 847                        &opt.unmatch_name_only,
 848                        N_("show only the names of files without match")),
 849                OPT_BOOLEAN('z', "null", &opt.null_following_name,
 850                        N_("print NUL after filenames")),
 851                OPT_BOOLEAN('c', "count", &opt.count,
 852                        N_("show the number of matches instead of matching lines")),
 853                OPT__COLOR(&opt.color, N_("highlight matches")),
 854                OPT_BOOLEAN(0, "break", &opt.file_break,
 855                        N_("print empty line between matches from different files")),
 856                OPT_BOOLEAN(0, "heading", &opt.heading,
 857                        N_("show filename only once above matches from same file")),
 858                OPT_GROUP(""),
 859                OPT_CALLBACK('C', "context", &opt, N_("n"),
 860                        N_("show <n> context lines before and after matches"),
 861                        context_callback),
 862                OPT_INTEGER('B', "before-context", &opt.pre_context,
 863                        N_("show <n> context lines before matches")),
 864                OPT_INTEGER('A', "after-context", &opt.post_context,
 865                        N_("show <n> context lines after matches")),
 866                OPT_NUMBER_CALLBACK(&opt, N_("shortcut for -C NUM"),
 867                        context_callback),
 868                OPT_BOOLEAN('p', "show-function", &opt.funcname,
 869                        N_("show a line with the function name before matches")),
 870                OPT_BOOLEAN('W', "function-context", &opt.funcbody,
 871                        N_("show the surrounding function")),
 872                OPT_GROUP(""),
 873                OPT_CALLBACK('f', NULL, &opt, N_("file"),
 874                        N_("read patterns from file"), file_callback),
 875                { OPTION_CALLBACK, 'e', NULL, &opt, N_("pattern"),
 876                        N_("match <pattern>"), PARSE_OPT_NONEG, pattern_callback },
 877                { OPTION_CALLBACK, 0, "and", &opt, NULL,
 878                  N_("combine patterns specified with -e"),
 879                  PARSE_OPT_NOARG | PARSE_OPT_NONEG, and_callback },
 880                OPT_BOOLEAN(0, "or", &dummy, ""),
 881                { OPTION_CALLBACK, 0, "not", &opt, NULL, "",
 882                  PARSE_OPT_NOARG | PARSE_OPT_NONEG, not_callback },
 883                { OPTION_CALLBACK, '(', NULL, &opt, NULL, "",
 884                  PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH,
 885                  open_callback },
 886                { OPTION_CALLBACK, ')', NULL, &opt, NULL, "",
 887                  PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH,
 888                  close_callback },
 889                OPT__QUIET(&opt.status_only,
 890                           N_("indicate hit with exit status without output")),
 891                OPT_BOOLEAN(0, "all-match", &opt.all_match,
 892                        N_("show only matches from files that match all patterns")),
 893                { OPTION_SET_INT, 0, "debug", &opt.debug, NULL,
 894                  N_("show parse tree for grep expression"),
 895                  PARSE_OPT_NOARG | PARSE_OPT_HIDDEN, NULL, 1 },
 896                OPT_GROUP(""),
 897                { OPTION_STRING, 'O', "open-files-in-pager", &show_in_pager,
 898                        N_("pager"), N_("show matching files in the pager"),
 899                        PARSE_OPT_OPTARG, NULL, (intptr_t)default_pager },
 900                OPT_BOOLEAN(0, "ext-grep", &external_grep_allowed__ignored,
 901                            N_("allow calling of grep(1) (ignored by this build)")),
 902                { OPTION_CALLBACK, 0, "help-all", &options, NULL, N_("show usage"),
 903                  PARSE_OPT_HIDDEN | PARSE_OPT_NOARG, help_callback },
 904                OPT_END()
 905        };
 906
 907        /*
 908         * 'git grep -h', unlike 'git grep -h <pattern>', is a request
 909         * to show usage information and exit.
 910         */
 911        if (argc == 2 && !strcmp(argv[1], "-h"))
 912                usage_with_options(grep_usage, options);
 913
 914        init_grep_defaults();
 915        git_config(grep_cmd_config, NULL);
 916        grep_init(&opt, prefix);
 917
 918        /*
 919         * If there is no -- then the paths must exist in the working
 920         * tree.  If there is no explicit pattern specified with -e or
 921         * -f, we take the first unrecognized non option to be the
 922         * pattern, but then what follows it must be zero or more
 923         * valid refs up to the -- (if exists), and then existing
 924         * paths.  If there is an explicit pattern, then the first
 925         * unrecognized non option is the beginning of the refs list
 926         * that continues up to the -- (if exists), and then paths.
 927         */
 928        argc = parse_options(argc, argv, prefix, options, grep_usage,
 929                             PARSE_OPT_KEEP_DASHDASH |
 930                             PARSE_OPT_STOP_AT_NON_OPTION |
 931                             PARSE_OPT_NO_INTERNAL_HELP);
 932
 933        if (pattern_type_arg != GREP_PATTERN_TYPE_UNSPECIFIED)
 934                grep_pattern_type_options(pattern_type_arg, &opt);
 935        else if (opt.pattern_type_option != GREP_PATTERN_TYPE_UNSPECIFIED)
 936                grep_pattern_type_options(opt.pattern_type_option, &opt);
 937        else if (opt.extended_regexp_option)
 938                grep_pattern_type_options(GREP_PATTERN_TYPE_ERE, &opt);
 939
 940        if (use_index && !startup_info->have_repository)
 941                /* die the same way as if we did it at the beginning */
 942                setup_git_directory();
 943
 944        /*
 945         * skip a -- separator; we know it cannot be
 946         * separating revisions from pathnames if
 947         * we haven't even had any patterns yet
 948         */
 949        if (argc > 0 && !opt.pattern_list && !strcmp(argv[0], "--")) {
 950                argv++;
 951                argc--;
 952        }
 953
 954        /* First unrecognized non-option token */
 955        if (argc > 0 && !opt.pattern_list) {
 956                append_grep_pattern(&opt, argv[0], "command line", 0,
 957                                    GREP_PATTERN);
 958                argv++;
 959                argc--;
 960        }
 961
 962        if (show_in_pager == default_pager)
 963                show_in_pager = git_pager(1);
 964        if (show_in_pager) {
 965                opt.color = 0;
 966                opt.name_only = 1;
 967                opt.null_following_name = 1;
 968                opt.output_priv = &path_list;
 969                opt.output = append_path;
 970                string_list_append(&path_list, show_in_pager);
 971                use_threads = 0;
 972        }
 973
 974        if (!opt.pattern_list)
 975                die(_("no pattern given."));
 976        if (!opt.fixed && opt.ignore_case)
 977                opt.regflags |= REG_ICASE;
 978
 979        compile_grep_patterns(&opt);
 980
 981        /* Check revs and then paths */
 982        for (i = 0; i < argc; i++) {
 983                const char *arg = argv[i];
 984                unsigned char sha1[20];
 985                /* Is it a rev? */
 986                if (!get_sha1(arg, sha1)) {
 987                        struct object *object = parse_object(sha1);
 988                        if (!object)
 989                                die(_("bad object %s"), arg);
 990                        add_object_array(object, arg, &list);
 991                        continue;
 992                }
 993                if (!strcmp(arg, "--")) {
 994                        i++;
 995                        seen_dashdash = 1;
 996                }
 997                break;
 998        }
 999
1000#ifndef NO_PTHREADS
1001        if (list.nr || cached || online_cpus() == 1)
1002                use_threads = 0;
1003#else
1004        use_threads = 0;
1005#endif
1006
1007#ifndef NO_PTHREADS
1008        if (use_threads) {
1009                if (!(opt.name_only || opt.unmatch_name_only || opt.count)
1010                    && (opt.pre_context || opt.post_context ||
1011                        opt.file_break || opt.funcbody))
1012                        skip_first_line = 1;
1013                start_threads(&opt);
1014        }
1015#endif
1016
1017        /* The rest are paths */
1018        if (!seen_dashdash) {
1019                int j;
1020                for (j = i; j < argc; j++)
1021                        verify_filename(prefix, argv[j], j == i);
1022        }
1023
1024        paths = get_pathspec(prefix, argv + i);
1025        init_pathspec(&pathspec, paths);
1026        pathspec.max_depth = opt.max_depth;
1027        pathspec.recursive = 1;
1028
1029        if (show_in_pager && (cached || list.nr))
1030                die(_("--open-files-in-pager only works on the worktree"));
1031
1032        if (show_in_pager && opt.pattern_list && !opt.pattern_list->next) {
1033                const char *pager = path_list.items[0].string;
1034                int len = strlen(pager);
1035
1036                if (len > 4 && is_dir_sep(pager[len - 5]))
1037                        pager += len - 4;
1038
1039                if (!strcmp("less", pager) || !strcmp("vi", pager)) {
1040                        struct strbuf buf = STRBUF_INIT;
1041                        strbuf_addf(&buf, "+/%s%s",
1042                                        strcmp("less", pager) ? "" : "*",
1043                                        opt.pattern_list->pattern);
1044                        string_list_append(&path_list, buf.buf);
1045                        strbuf_detach(&buf, NULL);
1046                }
1047        }
1048
1049        if (!show_in_pager)
1050                setup_pager();
1051
1052        if (!use_index && (untracked || cached))
1053                die(_("--cached or --untracked cannot be used with --no-index."));
1054
1055        if (!use_index || untracked) {
1056                int use_exclude = (opt_exclude < 0) ? use_index : !!opt_exclude;
1057                if (list.nr)
1058                        die(_("--no-index or --untracked cannot be used with revs."));
1059                hit = grep_directory(&opt, &pathspec, use_exclude);
1060        } else if (0 <= opt_exclude) {
1061                die(_("--[no-]exclude-standard cannot be used for tracked contents."));
1062        } else if (!list.nr) {
1063                if (!cached)
1064                        setup_work_tree();
1065
1066                hit = grep_cache(&opt, &pathspec, cached);
1067        } else {
1068                if (cached)
1069                        die(_("both --cached and trees are given."));
1070                hit = grep_objects(&opt, &pathspec, &list);
1071        }
1072
1073        if (use_threads)
1074                hit |= wait_all();
1075        if (hit && show_in_pager)
1076                run_pager(&opt, prefix);
1077        free_grep_patterns(&opt);
1078        return !hit;
1079}