resolve_gitlink_ref_recursive(): change to work with struct ref_cache
[gitweb.git] / builtin / check-attr.c
index b0d2ebc3dcff91024eed22255d6b707ad8676a79..44c421eb0fe9cd8947d6666d15d790bc241ee7f3 100644 (file)
@@ -5,6 +5,7 @@
 #include "parse-options.h"
 
 static int all_attrs;
+static int cached_attrs;
 static int stdin_paths;
 static const char * const check_attr_usage[] = {
 "git check-attr [-a | --all | attr...] [--] pathname...",
@@ -16,6 +17,7 @@ static int null_term_line;
 
 static const struct option check_attr_options[] = {
        OPT_BOOLEAN('a', "all", &all_attrs, "report all attributes set on file"),
+       OPT_BOOLEAN(0,  "cached", &cached_attrs, "use .gitattributes only from the index"),
        OPT_BOOLEAN(0 , "stdin", &stdin_paths, "read file names from stdin"),
        OPT_BOOLEAN('z', NULL, &null_term_line,
                "input paths are terminated by a null character"),
@@ -41,22 +43,26 @@ static void output_attr(int cnt, struct git_attr_check *check,
        }
 }
 
-static void check_attr(int cnt, struct git_attr_check *check,
-       const char *file)
+static void check_attr(const char *prefix, int cnt,
+       struct git_attr_check *check, const char *file)
 {
+       char *full_path =
+               prefix_path(prefix, prefix ? strlen(prefix) : 0, file);
        if (check != NULL) {
-               if (git_checkattr(file, cnt, check))
-                       die("git_checkattr died");
+               if (git_check_attr(full_path, cnt, check))
+                       die("git_check_attr died");
                output_attr(cnt, check, file);
        } else {
-               if (git_all_attrs(file, &cnt, &check))
+               if (git_all_attrs(full_path, &cnt, &check))
                        die("git_all_attrs died");
                output_attr(cnt, check, file);
                free(check);
        }
+       free(full_path);
 }
 
-static void check_attr_stdin_paths(int cnt, struct git_attr_check *check)
+static void check_attr_stdin_paths(const char *prefix, int cnt,
+       struct git_attr_check *check)
 {
        struct strbuf buf, nbuf;
        int line_termination = null_term_line ? 0 : '\n';
@@ -70,7 +76,7 @@ static void check_attr_stdin_paths(int cnt, struct git_attr_check *check)
                                die("line is badly quoted");
                        strbuf_swap(&buf, &nbuf);
                }
-               check_attr(cnt, check, buf.buf);
+               check_attr(prefix, cnt, check, buf.buf);
                maybe_flush_or_die(stdout, "attribute to stdout");
        }
        strbuf_release(&buf);
@@ -88,6 +94,8 @@ int cmd_check_attr(int argc, const char **argv, const char *prefix)
        struct git_attr_check *check;
        int cnt, i, doubledash, filei;
 
+       git_config(git_default_config, NULL);
+
        argc = parse_options(argc, argv, prefix, check_attr_options,
                             check_attr_usage, PARSE_OPT_KEEP_DASHDASH);
 
@@ -95,6 +103,9 @@ int cmd_check_attr(int argc, const char **argv, const char *prefix)
                die("invalid cache");
        }
 
+       if (cached_attrs)
+               git_attr_set_direction(GIT_ATTR_INDEX, NULL);
+
        doubledash = -1;
        for (i = 0; doubledash < 0 && i < argc; i++) {
                if (!strcmp(argv[i], "--"))
@@ -111,15 +122,18 @@ int cmd_check_attr(int argc, const char **argv, const char *prefix)
        } else if (doubledash == 0) {
                error_with_usage("No attribute specified");
        } else if (doubledash < 0) {
-               /*
-                * There is no double dash; treat the first
-                * argument as an attribute.
-                */
                if (!argc)
                        error_with_usage("No attribute specified");
 
-               cnt = 1;
-               filei = 1;
+               if (stdin_paths) {
+                       /* Treat all arguments as attribute names. */
+                       cnt = argc;
+                       filei = argc;
+               } else {
+                       /* Treat exactly one argument as an attribute name. */
+                       cnt = 1;
+                       filei = 1;
+               }
        } else {
                cnt = doubledash;
                filei = doubledash + 1;
@@ -151,10 +165,10 @@ int cmd_check_attr(int argc, const char **argv, const char *prefix)
        }
 
        if (stdin_paths)
-               check_attr_stdin_paths(cnt, check);
+               check_attr_stdin_paths(prefix, cnt, check);
        else {
                for (i = filei; i < argc; i++)
-                       check_attr(cnt, check, argv[i]);
+                       check_attr(prefix, cnt, check, argv[i]);
                maybe_flush_or_die(stdout, "attribute to stdout");
        }
        return 0;