builtin / check-ignore.con commit Merge branch 'jk/revision-pruning-optim' (f4c214b)
   1#include "builtin.h"
   2#include "cache.h"
   3#include "config.h"
   4#include "dir.h"
   5#include "quote.h"
   6#include "pathspec.h"
   7#include "parse-options.h"
   8#include "submodule.h"
   9
  10static int quiet, verbose, stdin_paths, show_non_matching, no_index;
  11static const char * const check_ignore_usage[] = {
  12"git check-ignore [<options>] <pathname>...",
  13"git check-ignore [<options>] --stdin",
  14NULL
  15};
  16
  17static int nul_term_line;
  18
  19static const struct option check_ignore_options[] = {
  20        OPT__QUIET(&quiet, N_("suppress progress reporting")),
  21        OPT__VERBOSE(&verbose, N_("be verbose")),
  22        OPT_GROUP(""),
  23        OPT_BOOL(0, "stdin", &stdin_paths,
  24                 N_("read file names from stdin")),
  25        OPT_BOOL('z', NULL, &nul_term_line,
  26                 N_("terminate input and output records by a NUL character")),
  27        OPT_BOOL('n', "non-matching", &show_non_matching,
  28                 N_("show non-matching input paths")),
  29        OPT_BOOL(0, "no-index", &no_index,
  30                 N_("ignore index when checking")),
  31        OPT_END()
  32};
  33
  34static void output_exclude(const char *path, struct exclude *exclude)
  35{
  36        char *bang  = (exclude && exclude->flags & EXC_FLAG_NEGATIVE)  ? "!" : "";
  37        char *slash = (exclude && exclude->flags & EXC_FLAG_MUSTBEDIR) ? "/" : "";
  38        if (!nul_term_line) {
  39                if (!verbose) {
  40                        write_name_quoted(path, stdout, '\n');
  41                } else {
  42                        if (exclude) {
  43                                quote_c_style(exclude->el->src, NULL, stdout, 0);
  44                                printf(":%d:%s%s%s\t",
  45                                       exclude->srcpos,
  46                                       bang, exclude->pattern, slash);
  47                        }
  48                        else {
  49                                printf("::\t");
  50                        }
  51                        quote_c_style(path, NULL, stdout, 0);
  52                        fputc('\n', stdout);
  53                }
  54        } else {
  55                if (!verbose) {
  56                        printf("%s%c", path, '\0');
  57                } else {
  58                        if (exclude)
  59                                printf("%s%c%d%c%s%s%s%c%s%c",
  60                                       exclude->el->src, '\0',
  61                                       exclude->srcpos, '\0',
  62                                       bang, exclude->pattern, slash, '\0',
  63                                       path, '\0');
  64                        else
  65                                printf("%c%c%c%s%c", '\0', '\0', '\0', path, '\0');
  66                }
  67        }
  68}
  69
  70static int check_ignore(struct dir_struct *dir,
  71                        const char *prefix, int argc, const char **argv)
  72{
  73        const char *full_path;
  74        char *seen;
  75        int num_ignored = 0, dtype = DT_UNKNOWN, i;
  76        struct exclude *exclude;
  77        struct pathspec pathspec;
  78
  79        if (!argc) {
  80                if (!quiet)
  81                        fprintf(stderr, "no pathspec given.\n");
  82                return 0;
  83        }
  84
  85        /*
  86         * check-ignore just needs paths. Magic beyond :/ is really
  87         * irrelevant.
  88         */
  89        parse_pathspec(&pathspec,
  90                       PATHSPEC_ALL_MAGIC & ~PATHSPEC_FROMTOP,
  91                       PATHSPEC_SYMLINK_LEADING_PATH |
  92                       PATHSPEC_KEEP_ORDER,
  93                       prefix, argv);
  94
  95        die_path_inside_submodule(&the_index, &pathspec);
  96
  97        /*
  98         * look for pathspecs matching entries in the index, since these
  99         * should not be ignored, in order to be consistent with
 100         * 'git status', 'git add' etc.
 101         */
 102        seen = find_pathspecs_matching_against_index(&pathspec, &the_index);
 103        for (i = 0; i < pathspec.nr; i++) {
 104                full_path = pathspec.items[i].match;
 105                exclude = NULL;
 106                if (!seen[i]) {
 107                        exclude = last_exclude_matching(dir, &the_index,
 108                                                        full_path, &dtype);
 109                }
 110                if (!quiet && (exclude || show_non_matching))
 111                        output_exclude(pathspec.items[i].original, exclude);
 112                if (exclude)
 113                        num_ignored++;
 114        }
 115        free(seen);
 116
 117        return num_ignored;
 118}
 119
 120static int check_ignore_stdin_paths(struct dir_struct *dir, const char *prefix)
 121{
 122        struct strbuf buf = STRBUF_INIT;
 123        struct strbuf unquoted = STRBUF_INIT;
 124        char *pathspec[2] = { NULL, NULL };
 125        strbuf_getline_fn getline_fn;
 126        int num_ignored = 0;
 127
 128        getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
 129        while (getline_fn(&buf, stdin) != EOF) {
 130                if (!nul_term_line && buf.buf[0] == '"') {
 131                        strbuf_reset(&unquoted);
 132                        if (unquote_c_style(&unquoted, buf.buf, NULL))
 133                                die("line is badly quoted");
 134                        strbuf_swap(&buf, &unquoted);
 135                }
 136                pathspec[0] = buf.buf;
 137                num_ignored += check_ignore(dir, prefix,
 138                                            1, (const char **)pathspec);
 139                maybe_flush_or_die(stdout, "check-ignore to stdout");
 140        }
 141        strbuf_release(&buf);
 142        strbuf_release(&unquoted);
 143        return num_ignored;
 144}
 145
 146int cmd_check_ignore(int argc, const char **argv, const char *prefix)
 147{
 148        int num_ignored;
 149        struct dir_struct dir;
 150
 151        git_config(git_default_config, NULL);
 152
 153        argc = parse_options(argc, argv, prefix, check_ignore_options,
 154                             check_ignore_usage, 0);
 155
 156        if (stdin_paths) {
 157                if (argc > 0)
 158                        die(_("cannot specify pathnames with --stdin"));
 159        } else {
 160                if (nul_term_line)
 161                        die(_("-z only makes sense with --stdin"));
 162                if (argc == 0)
 163                        die(_("no path specified"));
 164        }
 165        if (quiet) {
 166                if (argc > 1)
 167                        die(_("--quiet is only valid with a single pathname"));
 168                if (verbose)
 169                        die(_("cannot have both --quiet and --verbose"));
 170        }
 171        if (show_non_matching && !verbose)
 172                die(_("--non-matching is only valid with --verbose"));
 173
 174        /* read_cache() is only necessary so we can watch out for submodules. */
 175        if (!no_index && read_cache() < 0)
 176                die(_("index file corrupt"));
 177
 178        memset(&dir, 0, sizeof(dir));
 179        setup_standard_excludes(&dir);
 180
 181        if (stdin_paths) {
 182                num_ignored = check_ignore_stdin_paths(&dir, prefix);
 183        } else {
 184                num_ignored = check_ignore(&dir, prefix, argc, argv);
 185                maybe_flush_or_die(stdout, "ignore to stdout");
 186        }
 187
 188        clear_directory(&dir);
 189
 190        return !num_ignored;
 191}