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 intiterate_cb(const char*var,const char*value,void*data) 39{ 40static int nr; 41 42if(nr++) 43putchar('\n'); 44 45printf("key=%s\n", var); 46printf("value=%s\n", value ? value :"(null)"); 47printf("origin=%s\n",current_config_origin_type()); 48printf("name=%s\n",current_config_name()); 49 50return0; 51} 52 53intmain(int argc,char**argv) 54{ 55int i, val; 56const char*v; 57const struct string_list *strptr; 58struct config_set cs; 59git_configset_init(&cs); 60 61if(argc <2) { 62fprintf(stderr,"Please, provide a command name on the command-line\n"); 63goto exit1; 64}else if(argc ==3&& !strcmp(argv[1],"get_value")) { 65if(!git_config_get_value(argv[2], &v)) { 66if(!v) 67printf("(NULL)\n"); 68else 69printf("%s\n", v); 70goto exit0; 71}else{ 72printf("Value not found for\"%s\"\n", argv[2]); 73goto exit1; 74} 75}else if(argc ==3&& !strcmp(argv[1],"get_value_multi")) { 76 strptr =git_config_get_value_multi(argv[2]); 77if(strptr) { 78for(i =0; i < strptr->nr; i++) { 79 v = strptr->items[i].string; 80if(!v) 81printf("(NULL)\n"); 82else 83printf("%s\n", v); 84} 85goto exit0; 86}else{ 87printf("Value not found for\"%s\"\n", argv[2]); 88goto exit1; 89} 90}else if(argc ==3&& !strcmp(argv[1],"get_int")) { 91if(!git_config_get_int(argv[2], &val)) { 92printf("%d\n", val); 93goto exit0; 94}else{ 95printf("Value not found for\"%s\"\n", argv[2]); 96goto exit1; 97} 98}else if(argc ==3&& !strcmp(argv[1],"get_bool")) { 99if(!git_config_get_bool(argv[2], &val)) { 100printf("%d\n", val); 101goto exit0; 102}else{ 103printf("Value not found for\"%s\"\n", argv[2]); 104goto exit1; 105} 106}else if(argc ==3&& !strcmp(argv[1],"get_string")) { 107if(!git_config_get_string_const(argv[2], &v)) { 108printf("%s\n", v); 109goto exit0; 110}else{ 111printf("Value not found for\"%s\"\n", argv[2]); 112goto exit1; 113} 114}else if(!strcmp(argv[1],"configset_get_value")) { 115for(i =3; i < argc; i++) { 116int err; 117if((err =git_configset_add_file(&cs, argv[i]))) { 118fprintf(stderr,"Error (%d) reading configuration file%s.\n", err, argv[i]); 119goto exit2; 120} 121} 122if(!git_configset_get_value(&cs, argv[2], &v)) { 123if(!v) 124printf("(NULL)\n"); 125else 126printf("%s\n", v); 127goto exit0; 128}else{ 129printf("Value not found for\"%s\"\n", argv[2]); 130goto exit1; 131} 132}else if(!strcmp(argv[1],"configset_get_value_multi")) { 133for(i =3; i < argc; i++) { 134int err; 135if((err =git_configset_add_file(&cs, argv[i]))) { 136fprintf(stderr,"Error (%d) reading configuration file%s.\n", err, argv[i]); 137goto exit2; 138} 139} 140 strptr =git_configset_get_value_multi(&cs, argv[2]); 141if(strptr) { 142for(i =0; i < strptr->nr; i++) { 143 v = strptr->items[i].string; 144if(!v) 145printf("(NULL)\n"); 146else 147printf("%s\n", v); 148} 149goto exit0; 150}else{ 151printf("Value not found for\"%s\"\n", argv[2]); 152goto exit1; 153} 154}else if(!strcmp(argv[1],"iterate")) { 155git_config(iterate_cb, NULL); 156goto exit0; 157} 158 159die("%s: Please check the syntax and the function name", argv[0]); 160 161exit0: 162git_configset_clear(&cs); 163return0; 164 165exit1: 166git_configset_clear(&cs); 167return1; 168 169exit2: 170git_configset_clear(&cs); 171return2; 172}