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