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