9
#include "exec_cmd.h"
10
#include "strbuf.h"
11
#include "quote.h"
12
+#include "hashmap.h"
13
+#include "string-list.h"
14
15
struct config_source {
16
struct config_source *prev;
35
long (*do_ftell)(struct config_source *c);
36
};
37
38
+struct config_set_element {
39
+ struct hashmap_entry ent;
40
+ char *key;
41
+ struct string_list value_list;
42
+};
43
+
44
static struct config_source *cf;
45
46
static int zlib_compression_seen;
47
48
+/*
49
+ * Default config_set that contains key-value pairs from the usual set of config
50
+ * config files (i.e repo specific .git/config, user wide ~/.gitconfig, XDG
51
+ * config file and the global /etc/gitconfig)
52
+ */
53
+static struct config_set the_config_set;
54
+
55
static int config_file_fgetc(struct config_source *conf)
56
{
57
return fgetc(conf->u.file);
1227
return git_config_with_options(fn, data, NULL, 1);
1228
}
1229
1230
+static struct config_set_element *configset_find_element(struct config_set *cs, const char *key)
1231
+{
1232
+ struct config_set_element k;
1233
+ struct config_set_element *found_entry;
1234
+ char *normalized_key;
1235
+ int ret;
1236
+ /*
1237
+ * `key` may come from the user, so normalize it before using it
1238
+ * for querying entries from the hashmap.
1239
+ */
1240
+ ret = git_config_parse_key(key, &normalized_key, NULL);
1241
+
1242
+ if (ret)
1243
+ return NULL;
1244
+
1245
+ hashmap_entry_init(&k, strhash(normalized_key));
1246
+ k.key = normalized_key;
1247
+ found_entry = hashmap_get(&cs->config_hash, &k, NULL);
1248
+ free(normalized_key);
1249
+ return found_entry;
1250
+}
1251
+
1252
+static int configset_add_value(struct config_set *cs, const char *key, const char *value)
1253
+{
1254
+ struct config_set_element *e;
1255
+ e = configset_find_element(cs, key);
1256
+ /*
1257
+ * Since the keys are being fed by git_config*() callback mechanism, they
1258
+ * are already normalized. So simply add them without any further munging.
1259
+ */
1260
+ if (!e) {
1261
+ e = xmalloc(sizeof(*e));
1262
+ hashmap_entry_init(e, strhash(key));
1263
+ e->key = xstrdup(key);
1264
+ string_list_init(&e->value_list, 1);
1265
+ hashmap_add(&cs->config_hash, e);
1266
+ }
1267
+ string_list_append_nodup(&e->value_list, value ? xstrdup(value) : NULL);
1268
+
1269
+ return 0;
1270
+}
1271
+
1272
+static int config_set_element_cmp(const struct config_set_element *e1,
1273
+ const struct config_set_element *e2, const void *unused)
1274
+{
1275
+ return strcmp(e1->key, e2->key);
1276
+}
1277
+
1278
+void git_configset_init(struct config_set *cs)
1279
+{
1280
+ hashmap_init(&cs->config_hash, (hashmap_cmp_fn)config_set_element_cmp, 0);
1281
+ cs->hash_initialized = 1;
1282
+}
1283
+
1284
+void git_configset_clear(struct config_set *cs)
1285
+{
1286
+ struct config_set_element *entry;
1287
+ struct hashmap_iter iter;
1288
+ if (!cs->hash_initialized)
1289
+ return;
1290
+
1291
+ hashmap_iter_init(&cs->config_hash, &iter);
1292
+ while ((entry = hashmap_iter_next(&iter))) {
1293
+ free(entry->key);
1294
+ string_list_clear(&entry->value_list, 0);
1295
+ }
1296
+ hashmap_free(&cs->config_hash, 1);
1297
+ cs->hash_initialized = 0;
1298
+}
1299
+
1300
+static int config_set_callback(const char *key, const char *value, void *cb)
1301
+{
1302
+ struct config_set *cs = cb;
1303
+ configset_add_value(cs, key, value);
1304
+ return 0;
1305
+}
1306
+
1307
+int git_configset_add_file(struct config_set *cs, const char *filename)
1308
+{
1309
+ return git_config_from_file(config_set_callback, filename, cs);
1310
+}
1311
+
1312
+int git_configset_get_value(struct config_set *cs, const char *key, const char **value)
1313
+{
1314
+ const struct string_list *values = NULL;
1315
+ /*
1316
+ * Follows "last one wins" semantic, i.e., if there are multiple matches for the
1317
+ * queried key in the files of the configset, the value returned will be the last
1318
+ * value in the value list for that key.
1319
+ */
1320
+ values = git_configset_get_value_multi(cs, key);
1321
+
1322
+ if (!values)
1323
+ return 1;
1324
+ assert(values->nr > 0);
1325
+ *value = values->items[values->nr - 1].string;
1326
+ return 0;
1327
+}
1328
+
1329
+const struct string_list *git_configset_get_value_multi(struct config_set *cs, const char *key)
1330
+{
1331
+ struct config_set_element *e = configset_find_element(cs, key);
1332
+ return e ? &e->value_list : NULL;
1333
+}
1334
+
1335
+int git_configset_get_string_const(struct config_set *cs, const char *key, const char **dest)
1336
+{
1337
+ const char *value;
1338
+ if (!git_configset_get_value(cs, key, &value))
1339
+ return git_config_string(dest, key, value);
1340
+ else
1341
+ return 1;
1342
+}
1343
+
1344
+int git_configset_get_string(struct config_set *cs, const char *key, char **dest)
1345
+{
1346
+ return git_configset_get_string_const(cs, key, (const char **)dest);
1347
+}
1348
+
1349
+int git_configset_get_int(struct config_set *cs, const char *key, int *dest)
1350
+{
1351
+ const char *value;
1352
+ if (!git_configset_get_value(cs, key, &value)) {
1353
+ *dest = git_config_int(key, value);
1354
+ return 0;
1355
+ } else
1356
+ return 1;
1357
+}
1358
+
1359
+int git_configset_get_ulong(struct config_set *cs, const char *key, unsigned long *dest)
1360
+{
1361
+ const char *value;
1362
+ if (!git_configset_get_value(cs, key, &value)) {
1363
+ *dest = git_config_ulong(key, value);
1364
+ return 0;
1365
+ } else
1366
+ return 1;
1367
+}
1368
+
1369
+int git_configset_get_bool(struct config_set *cs, const char *key, int *dest)
1370
+{
1371
+ const char *value;
1372
+ if (!git_configset_get_value(cs, key, &value)) {
1373
+ *dest = git_config_bool(key, value);
1374
+ return 0;
1375
+ } else
1376
+ return 1;
1377
+}
1378
+
1379
+int git_configset_get_bool_or_int(struct config_set *cs, const char *key,
1380
+ int *is_bool, int *dest)
1381
+{
1382
+ const char *value;
1383
+ if (!git_configset_get_value(cs, key, &value)) {
1384
+ *dest = git_config_bool_or_int(key, value, is_bool);
1385
+ return 0;
1386
+ } else
1387
+ return 1;
1388
+}
1389
+
1390
+int git_configset_get_maybe_bool(struct config_set *cs, const char *key, int *dest)
1391
+{
1392
+ const char *value;
1393
+ if (!git_configset_get_value(cs, key, &value)) {
1394
+ *dest = git_config_maybe_bool(key, value);
1395
+ if (*dest == -1)
1396
+ return -1;
1397
+ return 0;
1398
+ } else
1399
+ return 1;
1400
+}
1401
+
1402
+int git_configset_get_pathname(struct config_set *cs, const char *key, const char **dest)
1403
+{
1404
+ const char *value;
1405
+ if (!git_configset_get_value(cs, key, &value))
1406
+ return git_config_pathname(dest, key, value);
1407
+ else
1408
+ return 1;
1409
+}
1410
+
1411
+static void git_config_check_init(void)
1412
+{
1413
+ if (the_config_set.hash_initialized)
1414
+ return;
1415
+ git_configset_init(&the_config_set);
1416
+ git_config(config_set_callback, &the_config_set);
1417
+}
1418
+
1419
+void git_config_clear(void)
1420
+{
1421
+ if (!the_config_set.hash_initialized)
1422
+ return;
1423
+ git_configset_clear(&the_config_set);
1424
+}
1425
+
1426
+int git_config_get_value(const char *key, const char **value)
1427
+{
1428
+ git_config_check_init();
1429
+ return git_configset_get_value(&the_config_set, key, value);
1430
+}
1431
+
1432
+const struct string_list *git_config_get_value_multi(const char *key)
1433
+{
1434
+ git_config_check_init();
1435
+ return git_configset_get_value_multi(&the_config_set, key);
1436
+}
1437
+
1438
+int git_config_get_string_const(const char *key, const char **dest)
1439
+{
1440
+ git_config_check_init();
1441
+ return git_configset_get_string_const(&the_config_set, key, dest);
1442
+}
1443
+
1444
+int git_config_get_string(const char *key, char **dest)
1445
+{
1446
+ git_config_check_init();
1447
+ return git_config_get_string_const(key, (const char **)dest);
1448
+}
1449
+
1450
+int git_config_get_int(const char *key, int *dest)
1451
+{
1452
+ git_config_check_init();
1453
+ return git_configset_get_int(&the_config_set, key, dest);
1454
+}
1455
+
1456
+int git_config_get_ulong(const char *key, unsigned long *dest)
1457
+{
1458
+ git_config_check_init();
1459
+ return git_configset_get_ulong(&the_config_set, key, dest);
1460
+}
1461
+
1462
+int git_config_get_bool(const char *key, int *dest)
1463
+{
1464
+ git_config_check_init();
1465
+ return git_configset_get_bool(&the_config_set, key, dest);
1466
+}
1467
+
1468
+int git_config_get_bool_or_int(const char *key, int *is_bool, int *dest)
1469
+{
1470
+ git_config_check_init();
1471
+ return git_configset_get_bool_or_int(&the_config_set, key, is_bool, dest);
1472
+}
1473
+
1474
+int git_config_get_maybe_bool(const char *key, int *dest)
1475
+{
1476
+ git_config_check_init();
1477
+ return git_configset_get_maybe_bool(&the_config_set, key, dest);
1478
+}
1479
+
1480
+int git_config_get_pathname(const char *key, const char **dest)
1481
+{
1482
+ git_config_check_init();
1483
+ return git_configset_get_pathname(&the_config_set, key, dest);
1484
+}
1485
+
1486
/*
1487
* Find all the stuff for git_config_set() below.
1488
*/
1978
lock = NULL;
1979
ret = 0;
1980
1981
+ /* Invalidate the config cache */
1982
+ git_config_clear();
1983
+
1984
out_free:
1985
if (lock)
1986
rollback_lock_file(lock);