read-cache: add post-index-change hook
[gitweb.git] / t / helper / test-config.c
index d143cd72223dec9c947fbf84d0f72945745a09c6..214003d5b2f9bbe978d5aa4d9c9ab3f9b8a990da 100644 (file)
@@ -1,4 +1,6 @@
+#include "test-tool.h"
 #include "cache.h"
+#include "config.h"
 #include "string-list.h"
 
 /*
  *                             ascending order of priority from a config_set
  *                             constructed from files entered as arguments.
  *
+ * iterate -> iterate over all values using git_config(), and print some
+ *            data for each
+ *
  * Examples:
  *
  * To print the value with highest priority for key "foo.bAr Baz.rock":
- *     test-config get_value "foo.bAr Baz.rock"
+ *     test-tool config get_value "foo.bAr Baz.rock"
  *
  */
 
+static const char *scope_name(enum config_scope scope)
+{
+       switch (scope) {
+       case CONFIG_SCOPE_SYSTEM:
+               return "system";
+       case CONFIG_SCOPE_GLOBAL:
+               return "global";
+       case CONFIG_SCOPE_REPO:
+               return "repo";
+       case CONFIG_SCOPE_CMDLINE:
+               return "cmdline";
+       default:
+               return "unknown";
+       }
+}
+static int iterate_cb(const char *var, const char *value, void *data)
+{
+       static int nr;
+
+       if (nr++)
+               putchar('\n');
+
+       printf("key=%s\n", var);
+       printf("value=%s\n", value ? value : "(null)");
+       printf("origin=%s\n", current_config_origin_type());
+       printf("name=%s\n", current_config_name());
+       printf("scope=%s\n", scope_name(current_config_scope()));
+
+       return 0;
+}
+
+static int early_config_cb(const char *var, const char *value, void *vdata)
+{
+       const char *key = vdata;
+
+       if (!strcmp(key, var))
+               printf("%s\n", value);
+
+       return 0;
+}
 
-int cmd_main(int argc, const char **argv)
+int cmd__config(int argc, const char **argv)
 {
        int i, val;
        const char *v;
        const struct string_list *strptr;
        struct config_set cs;
+
+       if (argc == 3 && !strcmp(argv[1], "read_early_config")) {
+               read_early_config(early_config_cb, (void *)argv[2]);
+               return 0;
+       }
+
+       setup_git_directory();
+
        git_configset_init(&cs);
 
        if (argc < 2) {
@@ -134,6 +187,9 @@ int cmd_main(int argc, const char **argv)
                        printf("Value not found for \"%s\"\n", argv[2]);
                        goto exit1;
                }
+       } else if (!strcmp(argv[1], "iterate")) {
+               git_config(iterate_cb, NULL);
+               goto exit0;
        }
 
        die("%s: Please check the syntax and the function name", argv[0]);