builtin / check-attr.con commit Merge branch 'js/doc-symref-in-proto-v1' (47e1019)
   1#define USE_THE_INDEX_COMPATIBILITY_MACROS
   2#include "builtin.h"
   3#include "cache.h"
   4#include "config.h"
   5#include "attr.h"
   6#include "quote.h"
   7#include "parse-options.h"
   8
   9static int all_attrs;
  10static int cached_attrs;
  11static int stdin_paths;
  12static const char * const check_attr_usage[] = {
  13N_("git check-attr [-a | --all | <attr>...] [--] <pathname>..."),
  14N_("git check-attr --stdin [-z] [-a | --all | <attr>...]"),
  15NULL
  16};
  17
  18static int nul_term_line;
  19
  20static const struct option check_attr_options[] = {
  21        OPT_BOOL('a', "all", &all_attrs, N_("report all attributes set on file")),
  22        OPT_BOOL(0,  "cached", &cached_attrs, N_("use .gitattributes only from the index")),
  23        OPT_BOOL(0 , "stdin", &stdin_paths, N_("read file names from stdin")),
  24        OPT_BOOL('z', NULL, &nul_term_line,
  25                 N_("terminate input and output records by a NUL character")),
  26        OPT_END()
  27};
  28
  29static void output_attr(struct attr_check *check, const char *file)
  30{
  31        int j;
  32        int cnt = check->nr;
  33
  34        for (j = 0; j < cnt; j++) {
  35                const char *value = check->items[j].value;
  36
  37                if (ATTR_TRUE(value))
  38                        value = "set";
  39                else if (ATTR_FALSE(value))
  40                        value = "unset";
  41                else if (ATTR_UNSET(value))
  42                        value = "unspecified";
  43
  44                if (nul_term_line) {
  45                        printf("%s%c" /* path */
  46                               "%s%c" /* attrname */
  47                               "%s%c" /* attrvalue */,
  48                               file, 0,
  49                               git_attr_name(check->items[j].attr), 0, value, 0);
  50                } else {
  51                        quote_c_style(file, NULL, stdout, 0);
  52                        printf(": %s: %s\n",
  53                               git_attr_name(check->items[j].attr), value);
  54                }
  55        }
  56}
  57
  58static void check_attr(const char *prefix,
  59                       struct attr_check *check,
  60                       int collect_all,
  61                       const char *file)
  62{
  63        char *full_path =
  64                prefix_path(prefix, prefix ? strlen(prefix) : 0, file);
  65
  66        if (collect_all) {
  67                git_all_attrs(&the_index, full_path, check);
  68        } else {
  69                git_check_attr(&the_index, full_path, check);
  70        }
  71        output_attr(check, file);
  72
  73        free(full_path);
  74}
  75
  76static void check_attr_stdin_paths(const char *prefix,
  77                                   struct attr_check *check,
  78                                   int collect_all)
  79{
  80        struct strbuf buf = STRBUF_INIT;
  81        struct strbuf unquoted = STRBUF_INIT;
  82        strbuf_getline_fn getline_fn;
  83
  84        getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
  85        while (getline_fn(&buf, stdin) != EOF) {
  86                if (!nul_term_line && buf.buf[0] == '"') {
  87                        strbuf_reset(&unquoted);
  88                        if (unquote_c_style(&unquoted, buf.buf, NULL))
  89                                die("line is badly quoted");
  90                        strbuf_swap(&buf, &unquoted);
  91                }
  92                check_attr(prefix, check, collect_all, buf.buf);
  93                maybe_flush_or_die(stdout, "attribute to stdout");
  94        }
  95        strbuf_release(&buf);
  96        strbuf_release(&unquoted);
  97}
  98
  99static NORETURN void error_with_usage(const char *msg)
 100{
 101        error("%s", msg);
 102        usage_with_options(check_attr_usage, check_attr_options);
 103}
 104
 105int cmd_check_attr(int argc, const char **argv, const char *prefix)
 106{
 107        struct attr_check *check;
 108        int cnt, i, doubledash, filei;
 109
 110        if (!is_bare_repository())
 111                setup_work_tree();
 112
 113        git_config(git_default_config, NULL);
 114
 115        argc = parse_options(argc, argv, prefix, check_attr_options,
 116                             check_attr_usage, PARSE_OPT_KEEP_DASHDASH);
 117
 118        if (read_cache() < 0) {
 119                die("invalid cache");
 120        }
 121
 122        if (cached_attrs)
 123                git_attr_set_direction(GIT_ATTR_INDEX);
 124
 125        doubledash = -1;
 126        for (i = 0; doubledash < 0 && i < argc; i++) {
 127                if (!strcmp(argv[i], "--"))
 128                        doubledash = i;
 129        }
 130
 131        /* Process --all and/or attribute arguments: */
 132        if (all_attrs) {
 133                if (doubledash >= 1)
 134                        error_with_usage("Attributes and --all both specified");
 135
 136                cnt = 0;
 137                filei = doubledash + 1;
 138        } else if (doubledash == 0) {
 139                error_with_usage("No attribute specified");
 140        } else if (doubledash < 0) {
 141                if (!argc)
 142                        error_with_usage("No attribute specified");
 143
 144                if (stdin_paths) {
 145                        /* Treat all arguments as attribute names. */
 146                        cnt = argc;
 147                        filei = argc;
 148                } else {
 149                        /* Treat exactly one argument as an attribute name. */
 150                        cnt = 1;
 151                        filei = 1;
 152                }
 153        } else {
 154                cnt = doubledash;
 155                filei = doubledash + 1;
 156        }
 157
 158        /* Check file argument(s): */
 159        if (stdin_paths) {
 160                if (filei < argc)
 161                        error_with_usage("Can't specify files with --stdin");
 162        } else {
 163                if (filei >= argc)
 164                        error_with_usage("No file specified");
 165        }
 166
 167        check = attr_check_alloc();
 168        if (!all_attrs) {
 169                for (i = 0; i < cnt; i++) {
 170                        const struct git_attr *a = git_attr(argv[i]);
 171
 172                        if (!a)
 173                                return error("%s: not a valid attribute name",
 174                                             argv[i]);
 175                        attr_check_append(check, a);
 176                }
 177        }
 178
 179        if (stdin_paths)
 180                check_attr_stdin_paths(prefix, check, all_attrs);
 181        else {
 182                for (i = filei; i < argc; i++)
 183                        check_attr(prefix, check, all_attrs, argv[i]);
 184                maybe_flush_or_die(stdout, "attribute to stdout");
 185        }
 186
 187        attr_check_free(check);
 188        return 0;
 189}