6b163687b6c5c236b11a8677ddb212aa862e6bb8
   1#include "builtin.h"
   2#include "cache.h"
   3#include "attr.h"
   4#include "quote.h"
   5#include "parse-options.h"
   6
   7static int all_attrs;
   8static int stdin_paths;
   9static const char * const check_attr_usage[] = {
  10"git check-attr [-a | --all | attr...] [--] pathname...",
  11"git check-attr --stdin [-a | --all | attr...] < <list-of-paths>",
  12NULL
  13};
  14
  15static int null_term_line;
  16
  17static const struct option check_attr_options[] = {
  18        OPT_BOOLEAN('a', "all", &all_attrs, "report all attributes set on file"),
  19        OPT_BOOLEAN(0 , "stdin", &stdin_paths, "read file names from stdin"),
  20        OPT_BOOLEAN('z', NULL, &null_term_line,
  21                "input paths are terminated by a null character"),
  22        OPT_END()
  23};
  24
  25static void output_attr(int cnt, struct git_attr_check *check,
  26        const char *file)
  27{
  28        int j;
  29        for (j = 0; j < cnt; j++) {
  30                const char *value = check[j].value;
  31
  32                if (ATTR_TRUE(value))
  33                        value = "set";
  34                else if (ATTR_FALSE(value))
  35                        value = "unset";
  36                else if (ATTR_UNSET(value))
  37                        value = "unspecified";
  38
  39                quote_c_style(file, NULL, stdout, 0);
  40                printf(": %s: %s\n", git_attr_name(check[j].attr), value);
  41        }
  42}
  43
  44static void check_attr(int cnt, struct git_attr_check *check,
  45        const char *file)
  46{
  47        if (check != NULL) {
  48                if (git_check_attr(file, cnt, check))
  49                        die("git_check_attr died");
  50                output_attr(cnt, check, file);
  51        } else {
  52                if (git_all_attrs(file, &cnt, &check))
  53                        die("git_all_attrs died");
  54                output_attr(cnt, check, file);
  55                free(check);
  56        }
  57}
  58
  59static void check_attr_stdin_paths(int cnt, struct git_attr_check *check)
  60{
  61        struct strbuf buf, nbuf;
  62        int line_termination = null_term_line ? 0 : '\n';
  63
  64        strbuf_init(&buf, 0);
  65        strbuf_init(&nbuf, 0);
  66        while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
  67                if (line_termination && buf.buf[0] == '"') {
  68                        strbuf_reset(&nbuf);
  69                        if (unquote_c_style(&nbuf, buf.buf, NULL))
  70                                die("line is badly quoted");
  71                        strbuf_swap(&buf, &nbuf);
  72                }
  73                check_attr(cnt, check, buf.buf);
  74                maybe_flush_or_die(stdout, "attribute to stdout");
  75        }
  76        strbuf_release(&buf);
  77        strbuf_release(&nbuf);
  78}
  79
  80static NORETURN void error_with_usage(const char *msg)
  81{
  82        error("%s", msg);
  83        usage_with_options(check_attr_usage, check_attr_options);
  84}
  85
  86int cmd_check_attr(int argc, const char **argv, const char *prefix)
  87{
  88        struct git_attr_check *check;
  89        int cnt, i, doubledash, filei;
  90
  91        argc = parse_options(argc, argv, prefix, check_attr_options,
  92                             check_attr_usage, PARSE_OPT_KEEP_DASHDASH);
  93
  94        if (read_cache() < 0) {
  95                die("invalid cache");
  96        }
  97
  98        doubledash = -1;
  99        for (i = 0; doubledash < 0 && i < argc; i++) {
 100                if (!strcmp(argv[i], "--"))
 101                        doubledash = i;
 102        }
 103
 104        /* Process --all and/or attribute arguments: */
 105        if (all_attrs) {
 106                if (doubledash >= 1)
 107                        error_with_usage("Attributes and --all both specified");
 108
 109                cnt = 0;
 110                filei = doubledash + 1;
 111        } else if (doubledash == 0) {
 112                error_with_usage("No attribute specified");
 113        } else if (doubledash < 0) {
 114                if (!argc)
 115                        error_with_usage("No attribute specified");
 116
 117                if (stdin_paths) {
 118                        /* Treat all arguments as attribute names. */
 119                        cnt = argc;
 120                        filei = argc;
 121                } else {
 122                        /* Treat exactly one argument as an attribute name. */
 123                        cnt = 1;
 124                        filei = 1;
 125                }
 126        } else {
 127                cnt = doubledash;
 128                filei = doubledash + 1;
 129        }
 130
 131        /* Check file argument(s): */
 132        if (stdin_paths) {
 133                if (filei < argc)
 134                        error_with_usage("Can't specify files with --stdin");
 135        } else {
 136                if (filei >= argc)
 137                        error_with_usage("No file specified");
 138        }
 139
 140        if (all_attrs) {
 141                check = NULL;
 142        } else {
 143                check = xcalloc(cnt, sizeof(*check));
 144                for (i = 0; i < cnt; i++) {
 145                        const char *name;
 146                        struct git_attr *a;
 147                        name = argv[i];
 148                        a = git_attr(name);
 149                        if (!a)
 150                                return error("%s: not a valid attribute name",
 151                                        name);
 152                        check[i].attr = a;
 153                }
 154        }
 155
 156        if (stdin_paths)
 157                check_attr_stdin_paths(cnt, check);
 158        else {
 159                for (i = filei; i < argc; i++)
 160                        check_attr(cnt, check, argv[i]);
 161                maybe_flush_or_die(stdout, "attribute to stdout");
 162        }
 163        return 0;
 164}