builtin-check-attr.con commit check-attr: add an internal check_attr() function (41038c5)
   1#include "builtin.h"
   2#include "cache.h"
   3#include "attr.h"
   4#include "quote.h"
   5
   6static const char check_attr_usage[] =
   7"git check-attr attr... [--] pathname...";
   8
   9static void check_attr(int cnt, struct git_attr_check *check,
  10        const char** name, const char *file)
  11{
  12        int j;
  13        if (git_checkattr(file, cnt, check))
  14                die("git_checkattr died");
  15        for (j = 0; j < cnt; j++) {
  16                const char *value = check[j].value;
  17
  18                if (ATTR_TRUE(value))
  19                        value = "set";
  20                else if (ATTR_FALSE(value))
  21                        value = "unset";
  22                else if (ATTR_UNSET(value))
  23                        value = "unspecified";
  24
  25                quote_c_style(file, NULL, stdout, 0);
  26                printf(": %s: %s\n", name[j], value);
  27        }
  28}
  29
  30int cmd_check_attr(int argc, const char **argv, const char *prefix)
  31{
  32        struct git_attr_check *check;
  33        int cnt, i, doubledash;
  34
  35        if (read_cache() < 0) {
  36                die("invalid cache");
  37        }
  38
  39        doubledash = -1;
  40        for (i = 1; doubledash < 0 && i < argc; i++) {
  41                if (!strcmp(argv[i], "--"))
  42                        doubledash = i;
  43        }
  44
  45        /* If there is no double dash, we handle only one attribute */
  46        if (doubledash < 0) {
  47                cnt = 1;
  48                doubledash = 1;
  49        } else
  50                cnt = doubledash - 1;
  51        doubledash++;
  52
  53        if (cnt <= 0 || argc < doubledash)
  54                usage(check_attr_usage);
  55        check = xcalloc(cnt, sizeof(*check));
  56        for (i = 0; i < cnt; i++) {
  57                const char *name;
  58                struct git_attr *a;
  59                name = argv[i + 1];
  60                a = git_attr(name, strlen(name));
  61                if (!a)
  62                        return error("%s: not a valid attribute name", name);
  63                check[i].attr = a;
  64        }
  65
  66        for (i = doubledash; i < argc; i++)
  67                check_attr(cnt, check, argv+1, argv[i]);
  68        maybe_flush_or_die(stdout, "attribute to stdout");
  69        return 0;
  70}