Commit 650a9dff by Edward Thomson

config: rename config_entries to config_list

parent f85e0af2
/*
* Copyright (C) the libgit2 contributors. All rights reserved.
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#include "common.h"
#include "git2/sys/config.h"
#include "config.h"
typedef struct git_config_entries git_config_entries;
int git_config_entries_new(git_config_entries **out);
int git_config_entries_dup(git_config_entries **out, git_config_entries *entries);
int git_config_entries_dup_entry(git_config_entries *entries, const git_config_entry *entry);
void git_config_entries_incref(git_config_entries *entries);
void git_config_entries_free(git_config_entries *entries);
/* Add or append the new config option */
int git_config_entries_append(git_config_entries *entries, git_config_entry *entry);
int git_config_entries_get(git_config_entry **out, git_config_entries *entries, const char *key);
int git_config_entries_get_unique(git_config_entry **out, git_config_entries *entries, const char *key);
int git_config_entries_iterator_new(git_config_iterator **out, git_config_entries *entries);
......@@ -5,7 +5,7 @@
* a Linking Exception. For full terms see the included COPYING file.
*/
#include "config_entries.h"
#include "config_list.h"
typedef struct config_entry_list {
struct config_entry_list *next;
......@@ -18,36 +18,36 @@ typedef struct {
bool multivar;
} config_entry_map_head;
typedef struct config_entries_iterator {
typedef struct config_list_iterator {
git_config_iterator parent;
git_config_entries *entries;
git_config_list *list;
config_entry_list *head;
} config_entries_iterator;
} config_list_iterator;
struct git_config_entries {
struct git_config_list {
git_refcount rc;
git_strmap *map;
config_entry_list *list;
config_entry_list *entries;
};
int git_config_entries_new(git_config_entries **out)
int git_config_list_new(git_config_list **out)
{
git_config_entries *entries;
git_config_list *config_list;
int error;
entries = git__calloc(1, sizeof(git_config_entries));
GIT_ERROR_CHECK_ALLOC(entries);
GIT_REFCOUNT_INC(entries);
config_list = git__calloc(1, sizeof(git_config_list));
GIT_ERROR_CHECK_ALLOC(config_list);
GIT_REFCOUNT_INC(config_list);
if ((error = git_strmap_new(&entries->map)) < 0)
git__free(entries);
if ((error = git_strmap_new(&config_list->map)) < 0)
git__free(config_list);
else
*out = entries;
*out = config_list;
return error;
}
int git_config_entries_dup_entry(git_config_entries *entries, const git_config_entry *entry)
int git_config_list_dup_entry(git_config_list *config_list, const git_config_entry *entry)
{
git_config_entry *duplicated;
int error;
......@@ -65,7 +65,7 @@ int git_config_entries_dup_entry(git_config_entries *entries, const git_config_e
duplicated->level = entry->level;
duplicated->include_depth = entry->include_depth;
if ((error = git_config_entries_append(entries, duplicated)) < 0)
if ((error = git_config_list_append(config_list, duplicated)) < 0)
goto out;
out:
......@@ -77,78 +77,79 @@ out:
return error;
}
int git_config_entries_dup(git_config_entries **out, git_config_entries *entries)
int git_config_list_dup(git_config_list **out, git_config_list *config_list)
{
git_config_entries *result = NULL;
git_config_list *result = NULL;
config_entry_list *head;
int error;
if ((error = git_config_entries_new(&result)) < 0)
if ((error = git_config_list_new(&result)) < 0)
goto out;
for (head = entries->list; head; head = head->next)
if ((git_config_entries_dup_entry(result, head->entry)) < 0)
for (head = config_list->entries; head; head = head->next)
if ((git_config_list_dup_entry(result, head->entry)) < 0)
goto out;
*out = result;
result = NULL;
out:
git_config_entries_free(result);
git_config_list_free(result);
return error;
}
void git_config_entries_incref(git_config_entries *entries)
void git_config_list_incref(git_config_list *config_list)
{
GIT_REFCOUNT_INC(entries);
GIT_REFCOUNT_INC(config_list);
}
static void config_entries_free(git_config_entries *entries)
static void config_list_free(git_config_list *config_list)
{
config_entry_list *list = NULL, *next;
config_entry_list *entry_list = NULL, *next;
config_entry_map_head *head;
git_strmap_foreach_value(entries->map, head,
git__free((char *) head->entry->name); git__free(head)
);
git_strmap_free(entries->map);
list = entries->list;
while (list != NULL) {
next = list->next;
git__free((char *) list->entry->value);
git__free(list->entry);
git__free(list);
list = next;
git_strmap_foreach_value(config_list->map, head, {
git__free((char *) head->entry->name);
git__free(head);
});
git_strmap_free(config_list->map);
entry_list = config_list->entries;
while (entry_list != NULL) {
next = entry_list->next;
git__free((char *) entry_list->entry->value);
git__free(entry_list->entry);
git__free(entry_list);
entry_list = next;
}
git__free(entries);
git__free(config_list);
}
void git_config_entries_free(git_config_entries *entries)
void git_config_list_free(git_config_list *config_list)
{
if (entries)
GIT_REFCOUNT_DEC(entries, config_entries_free);
if (config_list)
GIT_REFCOUNT_DEC(config_list, config_list_free);
}
int git_config_entries_append(git_config_entries *entries, git_config_entry *entry)
int git_config_list_append(git_config_list *config_list, git_config_entry *entry)
{
config_entry_list *list_head;
config_entry_map_head *map_head;
if ((map_head = git_strmap_get(entries->map, entry->name)) != NULL) {
if ((map_head = git_strmap_get(config_list->map, entry->name)) != NULL) {
map_head->multivar = true;
/*
* This is a micro-optimization for configuration files
* with a lot of same keys. As for multivars the entry's
* key will be the same for all entries, we can just free
* key will be the same for all list, we can just free
* all except the first entry's name and just re-use it.
*/
git__free((char *) entry->name);
entry->name = map_head->entry->name;
} else {
map_head = git__calloc(1, sizeof(*map_head));
if ((git_strmap_set(entries->map, entry->name, map_head)) < 0)
if ((git_strmap_set(config_list->map, entry->name, map_head)) < 0)
return -1;
}
map_head->entry = entry;
......@@ -157,29 +158,29 @@ int git_config_entries_append(git_config_entries *entries, git_config_entry *ent
GIT_ERROR_CHECK_ALLOC(list_head);
list_head->entry = entry;
if (entries->list)
entries->list->last->next = list_head;
if (config_list->entries)
config_list->entries->last->next = list_head;
else
entries->list = list_head;
entries->list->last = list_head;
config_list->entries = list_head;
config_list->entries->last = list_head;
return 0;
}
int git_config_entries_get(git_config_entry **out, git_config_entries *entries, const char *key)
int git_config_list_get(git_config_entry **out, git_config_list *config_list, const char *key)
{
config_entry_map_head *entry;
if ((entry = git_strmap_get(entries->map, key)) == NULL)
if ((entry = git_strmap_get(config_list->map, key)) == NULL)
return GIT_ENOTFOUND;
*out = entry->entry;
return 0;
}
int git_config_entries_get_unique(git_config_entry **out, git_config_entries *entries, const char *key)
int git_config_list_get_unique(git_config_entry **out, git_config_list *config_list, const char *key)
{
config_entry_map_head *entry;
if ((entry = git_strmap_get(entries->map, key)) == NULL)
if ((entry = git_strmap_get(config_list->map, key)) == NULL)
return GIT_ENOTFOUND;
if (entry->multivar) {
......@@ -199,8 +200,8 @@ int git_config_entries_get_unique(git_config_entry **out, git_config_entries *en
static void config_iterator_free(git_config_iterator *iter)
{
config_entries_iterator *it = (config_entries_iterator *) iter;
git_config_entries_free(it->entries);
config_list_iterator *it = (config_list_iterator *) iter;
git_config_list_free(it->list);
git__free(it);
}
......@@ -208,7 +209,7 @@ static int config_iterator_next(
git_config_entry **entry,
git_config_iterator *iter)
{
config_entries_iterator *it = (config_entries_iterator *) iter;
config_list_iterator *it = (config_list_iterator *) iter;
if (!it->head)
return GIT_ITEROVER;
......@@ -219,18 +220,18 @@ static int config_iterator_next(
return 0;
}
int git_config_entries_iterator_new(git_config_iterator **out, git_config_entries *entries)
int git_config_list_iterator_new(git_config_iterator **out, git_config_list *config_list)
{
config_entries_iterator *it;
config_list_iterator *it;
it = git__calloc(1, sizeof(config_entries_iterator));
it = git__calloc(1, sizeof(config_list_iterator));
GIT_ERROR_CHECK_ALLOC(it);
it->parent.next = config_iterator_next;
it->parent.free = config_iterator_free;
it->head = entries->list;
it->entries = entries;
it->head = config_list->entries;
it->list = config_list;
git_config_entries_incref(entries);
git_config_list_incref(config_list);
*out = &it->parent;
return 0;
......
/*
* Copyright (C) the libgit2 contributors. All rights reserved.
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#include "common.h"
#include "git2/sys/config.h"
#include "config.h"
typedef struct git_config_list git_config_list;
int git_config_list_new(git_config_list **out);
int git_config_list_dup(git_config_list **out, git_config_list *list);
int git_config_list_dup_entry(git_config_list *list, const git_config_entry *entry);
void git_config_list_incref(git_config_list *list);
void git_config_list_free(git_config_list *list);
/* Add or append the new config option */
int git_config_list_append(git_config_list *list, git_config_entry *entry);
int git_config_list_get(git_config_entry **out, git_config_list *list, const char *key);
int git_config_list_get_unique(git_config_entry **out, git_config_list *list, const char *key);
int git_config_list_iterator_new(git_config_iterator **out, git_config_list *list);
......@@ -9,16 +9,16 @@
#include "config_backend.h"
#include "config_parse.h"
#include "config_entries.h"
#include "config_list.h"
typedef struct {
git_config_backend parent;
git_config_entries *entries;
git_config_list *config_list;
git_str cfg;
} config_memory_backend;
typedef struct {
git_config_entries *entries;
git_config_list *config_list;
git_config_level_t level;
} config_memory_parse_data;
......@@ -69,7 +69,7 @@ static int read_variable_cb(
entry->level = parse_data->level;
entry->include_depth = 0;
if ((result = git_config_entries_append(parse_data->entries, entry)) < 0)
if ((result = git_config_list_append(parse_data->config_list, entry)) < 0)
return result;
return result;
......@@ -87,7 +87,7 @@ static int config_memory_open(git_config_backend *backend, git_config_level_t le
if ((error = git_config_parser_init(&parser, "in-memory", memory_backend->cfg.ptr,
memory_backend->cfg.size)) < 0)
goto out;
parse_data.entries = memory_backend->entries;
parse_data.config_list = memory_backend->config_list;
parse_data.level = level;
if ((error = git_config_parse(&parser, NULL, read_variable_cb, NULL, NULL, &parse_data)) < 0)
......@@ -101,7 +101,7 @@ out:
static int config_memory_get(git_config_backend *backend, const char *key, git_config_entry **out)
{
config_memory_backend *memory_backend = (config_memory_backend *) backend;
return git_config_entries_get(out, memory_backend->entries, key);
return git_config_list_get(out, memory_backend->config_list, key);
}
static int config_memory_iterator(
......@@ -109,18 +109,18 @@ static int config_memory_iterator(
git_config_backend *backend)
{
config_memory_backend *memory_backend = (config_memory_backend *) backend;
git_config_entries *entries;
git_config_list *config_list;
int error;
if ((error = git_config_entries_dup(&entries, memory_backend->entries)) < 0)
if ((error = git_config_list_dup(&config_list, memory_backend->config_list)) < 0)
goto out;
if ((error = git_config_entries_iterator_new(iter, entries)) < 0)
if ((error = git_config_list_iterator_new(iter, config_list)) < 0)
goto out;
out:
/* Let iterator delete duplicated entries when it's done */
git_config_entries_free(entries);
/* Let iterator delete duplicated config_list when it's done */
git_config_list_free(config_list);
return error;
}
......@@ -177,7 +177,7 @@ static void config_memory_free(git_config_backend *_backend)
if (backend == NULL)
return;
git_config_entries_free(backend->entries);
git_config_list_free(backend->config_list);
git_str_dispose(&backend->cfg);
git__free(backend);
}
......@@ -189,13 +189,13 @@ int git_config_backend_from_string(git_config_backend **out, const char *cfg, si
backend = git__calloc(1, sizeof(config_memory_backend));
GIT_ERROR_CHECK_ALLOC(backend);
if (git_config_entries_new(&backend->entries) < 0) {
if (git_config_list_new(&backend->config_list) < 0) {
git__free(backend);
return -1;
}
if (git_str_set(&backend->cfg, cfg, len) < 0) {
git_config_entries_free(backend->entries);
git_config_list_free(backend->config_list);
git__free(backend);
return -1;
}
......
......@@ -8,12 +8,12 @@
#include "config_backend.h"
#include "config.h"
#include "config_entries.h"
#include "config_list.h"
typedef struct {
git_config_backend parent;
git_mutex values_mutex;
git_config_entries *entries;
git_config_list *config_list;
git_config_backend *source;
} config_snapshot_backend;
......@@ -28,30 +28,30 @@ static int config_snapshot_iterator(
struct git_config_backend *backend)
{
config_snapshot_backend *b = GIT_CONTAINER_OF(backend, config_snapshot_backend, parent);
git_config_entries *entries = NULL;
git_config_list *config_list = NULL;
int error;
if ((error = git_config_entries_dup(&entries, b->entries)) < 0 ||
(error = git_config_entries_iterator_new(iter, entries)) < 0)
if ((error = git_config_list_dup(&config_list, b->config_list)) < 0 ||
(error = git_config_list_iterator_new(iter, config_list)) < 0)
goto out;
out:
/* Let iterator delete duplicated entries when it's done */
git_config_entries_free(entries);
/* Let iterator delete duplicated config_list when it's done */
git_config_list_free(config_list);
return error;
}
/* release the map containing the entry as an equivalent to freeing it */
static void config_snapshot_entry_free(git_config_entry *entry)
{
git_config_entries *entries = (git_config_entries *) entry->payload;
git_config_entries_free(entries);
git_config_list *config_list = (git_config_list *) entry->payload;
git_config_list_free(config_list);
}
static int config_snapshot_get(git_config_backend *cfg, const char *key, git_config_entry **out)
{
config_snapshot_backend *b = GIT_CONTAINER_OF(cfg, config_snapshot_backend, parent);
git_config_entries *entries = NULL;
git_config_list *config_list = NULL;
git_config_entry *entry;
int error = 0;
......@@ -60,17 +60,17 @@ static int config_snapshot_get(git_config_backend *cfg, const char *key, git_con
return -1;
}
entries = b->entries;
git_config_entries_incref(entries);
config_list = b->config_list;
git_config_list_incref(config_list);
git_mutex_unlock(&b->values_mutex);
if ((error = (git_config_entries_get(&entry, entries, key))) < 0) {
git_config_entries_free(entries);
if ((error = (git_config_list_get(&entry, config_list, key))) < 0) {
git_config_list_free(config_list);
return error;
}
entry->free = config_snapshot_entry_free;
entry->payload = entries;
entry->payload = config_list;
*out = entry;
return 0;
......@@ -135,7 +135,7 @@ static void config_snapshot_free(git_config_backend *_backend)
if (backend == NULL)
return;
git_config_entries_free(backend->entries);
git_config_list_free(backend->config_list);
git_mutex_free(&backend->values_mutex);
git__free(backend);
}
......@@ -143,7 +143,7 @@ static void config_snapshot_free(git_config_backend *_backend)
static int config_snapshot_open(git_config_backend *cfg, git_config_level_t level, const git_repository *repo)
{
config_snapshot_backend *b = GIT_CONTAINER_OF(cfg, config_snapshot_backend, parent);
git_config_entries *entries = NULL;
git_config_list *config_list = NULL;
git_config_iterator *it = NULL;
git_config_entry *entry;
int error;
......@@ -152,12 +152,12 @@ static int config_snapshot_open(git_config_backend *cfg, git_config_level_t leve
GIT_UNUSED(level);
GIT_UNUSED(repo);
if ((error = git_config_entries_new(&entries)) < 0 ||
if ((error = git_config_list_new(&config_list)) < 0 ||
(error = b->source->iterator(&it, b->source)) < 0)
goto out;
while ((error = git_config_next(&entry, it)) == 0)
if ((error = git_config_entries_dup_entry(entries, entry)) < 0)
if ((error = git_config_list_dup_entry(config_list, entry)) < 0)
goto out;
if (error < 0) {
......@@ -166,12 +166,12 @@ static int config_snapshot_open(git_config_backend *cfg, git_config_level_t leve
error = 0;
}
b->entries = entries;
b->config_list = config_list;
out:
git_config_iterator_free(it);
if (error)
git_config_entries_free(entries);
git_config_list_free(config_list);
return error;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment