Merge branch 'bw/config-h'
[gitweb.git] / t / helper / test-config.c
index d143cd72223dec9c947fbf84d0f72945745a09c6..1a7b8bd3d650fe1111c77115d2c68644b6ac9edb 100644 (file)
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "string-list.h"
 
 /*
@@ -25,6 +26,9 @@
  *                             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":
  *
  */
 
+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)
 {
@@ -39,6 +83,14 @@ int cmd_main(int argc, const char **argv)
        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 +186,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]);