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