t / helper / test-config.con commit Merge branch 'sb/blame-config-doc' (4da9f59)
   1#include "cache.h"
   2#include "config.h"
   3#include "string-list.h"
   4
   5/*
   6 * This program exposes the C API of the configuration mechanism
   7 * as a set of simple commands in order to facilitate testing.
   8 *
   9 * Reads stdin and prints result of command to stdout:
  10 *
  11 * get_value -> prints the value with highest priority for the entered key
  12 *
  13 * get_value_multi -> prints all values for the entered key in increasing order
  14 *                   of priority
  15 *
  16 * get_int -> print integer value for the entered key or die
  17 *
  18 * get_bool -> print bool value for the entered key or die
  19 *
  20 * get_string -> print string value for the entered key or die
  21 *
  22 * configset_get_value -> returns value with the highest priority for the entered key
  23 *                      from a config_set constructed from files entered as arguments.
  24 *
  25 * configset_get_value_multi -> returns value_list for the entered key sorted in
  26 *                              ascending order of priority from a config_set
  27 *                              constructed from files entered as arguments.
  28 *
  29 * iterate -> iterate over all values using git_config(), and print some
  30 *            data for each
  31 *
  32 * Examples:
  33 *
  34 * To print the value with highest priority for key "foo.bAr Baz.rock":
  35 *      test-config get_value "foo.bAr Baz.rock"
  36 *
  37 */
  38
  39static const char *scope_name(enum config_scope scope)
  40{
  41        switch (scope) {
  42        case CONFIG_SCOPE_SYSTEM:
  43                return "system";
  44        case CONFIG_SCOPE_GLOBAL:
  45                return "global";
  46        case CONFIG_SCOPE_REPO:
  47                return "repo";
  48        case CONFIG_SCOPE_CMDLINE:
  49                return "cmdline";
  50        default:
  51                return "unknown";
  52        }
  53}
  54static int iterate_cb(const char *var, const char *value, void *data)
  55{
  56        static int nr;
  57
  58        if (nr++)
  59                putchar('\n');
  60
  61        printf("key=%s\n", var);
  62        printf("value=%s\n", value ? value : "(null)");
  63        printf("origin=%s\n", current_config_origin_type());
  64        printf("name=%s\n", current_config_name());
  65        printf("scope=%s\n", scope_name(current_config_scope()));
  66
  67        return 0;
  68}
  69
  70static int early_config_cb(const char *var, const char *value, void *vdata)
  71{
  72        const char *key = vdata;
  73
  74        if (!strcmp(key, var))
  75                printf("%s\n", value);
  76
  77        return 0;
  78}
  79
  80int cmd_main(int argc, const char **argv)
  81{
  82        int i, val;
  83        const char *v;
  84        const struct string_list *strptr;
  85        struct config_set cs;
  86
  87        if (argc == 3 && !strcmp(argv[1], "read_early_config")) {
  88                read_early_config(early_config_cb, (void *)argv[2]);
  89                return 0;
  90        }
  91
  92        setup_git_directory();
  93
  94        git_configset_init(&cs);
  95
  96        if (argc < 2) {
  97                fprintf(stderr, "Please, provide a command name on the command-line\n");
  98                goto exit1;
  99        } else if (argc == 3 && !strcmp(argv[1], "get_value")) {
 100                if (!git_config_get_value(argv[2], &v)) {
 101                        if (!v)
 102                                printf("(NULL)\n");
 103                        else
 104                                printf("%s\n", v);
 105                        goto exit0;
 106                } else {
 107                        printf("Value not found for \"%s\"\n", argv[2]);
 108                        goto exit1;
 109                }
 110        } else if (argc == 3 && !strcmp(argv[1], "get_value_multi")) {
 111                strptr = git_config_get_value_multi(argv[2]);
 112                if (strptr) {
 113                        for (i = 0; i < strptr->nr; i++) {
 114                                v = strptr->items[i].string;
 115                                if (!v)
 116                                        printf("(NULL)\n");
 117                                else
 118                                        printf("%s\n", v);
 119                        }
 120                        goto exit0;
 121                } else {
 122                        printf("Value not found for \"%s\"\n", argv[2]);
 123                        goto exit1;
 124                }
 125        } else if (argc == 3 && !strcmp(argv[1], "get_int")) {
 126                if (!git_config_get_int(argv[2], &val)) {
 127                        printf("%d\n", val);
 128                        goto exit0;
 129                } else {
 130                        printf("Value not found for \"%s\"\n", argv[2]);
 131                        goto exit1;
 132                }
 133        } else if (argc == 3 && !strcmp(argv[1], "get_bool")) {
 134                if (!git_config_get_bool(argv[2], &val)) {
 135                        printf("%d\n", val);
 136                        goto exit0;
 137                } else {
 138                        printf("Value not found for \"%s\"\n", argv[2]);
 139                        goto exit1;
 140                }
 141        } else if (argc == 3 && !strcmp(argv[1], "get_string")) {
 142                if (!git_config_get_string_const(argv[2], &v)) {
 143                        printf("%s\n", v);
 144                        goto exit0;
 145                } else {
 146                        printf("Value not found for \"%s\"\n", argv[2]);
 147                        goto exit1;
 148                }
 149        } else if (!strcmp(argv[1], "configset_get_value")) {
 150                for (i = 3; i < argc; i++) {
 151                        int err;
 152                        if ((err = git_configset_add_file(&cs, argv[i]))) {
 153                                fprintf(stderr, "Error (%d) reading configuration file %s.\n", err, argv[i]);
 154                                goto exit2;
 155                        }
 156                }
 157                if (!git_configset_get_value(&cs, argv[2], &v)) {
 158                        if (!v)
 159                                printf("(NULL)\n");
 160                        else
 161                                printf("%s\n", v);
 162                        goto exit0;
 163                } else {
 164                        printf("Value not found for \"%s\"\n", argv[2]);
 165                        goto exit1;
 166                }
 167        } else if (!strcmp(argv[1], "configset_get_value_multi")) {
 168                for (i = 3; i < argc; i++) {
 169                        int err;
 170                        if ((err = git_configset_add_file(&cs, argv[i]))) {
 171                                fprintf(stderr, "Error (%d) reading configuration file %s.\n", err, argv[i]);
 172                                goto exit2;
 173                        }
 174                }
 175                strptr = git_configset_get_value_multi(&cs, argv[2]);
 176                if (strptr) {
 177                        for (i = 0; i < strptr->nr; i++) {
 178                                v = strptr->items[i].string;
 179                                if (!v)
 180                                        printf("(NULL)\n");
 181                                else
 182                                        printf("%s\n", v);
 183                        }
 184                        goto exit0;
 185                } else {
 186                        printf("Value not found for \"%s\"\n", argv[2]);
 187                        goto exit1;
 188                }
 189        } else if (!strcmp(argv[1], "iterate")) {
 190                git_config(iterate_cb, NULL);
 191                goto exit0;
 192        }
 193
 194        die("%s: Please check the syntax and the function name", argv[0]);
 195
 196exit0:
 197        git_configset_clear(&cs);
 198        return 0;
 199
 200exit1:
 201        git_configset_clear(&cs);
 202        return 1;
 203
 204exit2:
 205        git_configset_clear(&cs);
 206        return 2;
 207}