git_config_entry *entry;
} config_entry_list;
+typedef struct {
+ git_config_entry *entry;
+ bool multivar;
+} config_entry_map_head;
+
typedef struct config_entries_iterator {
git_config_iterator parent;
git_config_entries *entries;
config_entry_list *list;
};
-static void config_entry_list_free(config_entry_list *list)
-{
- config_entry_list *next;
-
- while (list != NULL) {
- next = list->next;
-
- git__free((char*) list->entry->name);
- git__free((char *) list->entry->value);
- git__free(list->entry);
- git__free(list);
-
- list = next;
- };
-}
-
-static void config_entry_list_append(config_entry_list **list, config_entry_list *entry)
-{
- if (*list)
- (*list)->last->next = entry;
- else
- *list = entry;
- (*list)->last = entry;
-}
-
int git_config_entries_new(git_config_entries **out)
{
git_config_entries *entries;
GIT_ERROR_CHECK_ALLOC(entries);
GIT_REFCOUNT_INC(entries);
- if ((error = git_strmap_alloc(&entries->map)) < 0)
+ if ((error = git_strmap_new(&entries->map)) < 0)
git__free(entries);
else
*out = entries;
return error;
}
+int git_config_entries_dup_entry(git_config_entries *entries, const git_config_entry *entry)
+{
+ git_config_entry *duplicated;
+ int error;
+
+ duplicated = git__calloc(1, sizeof(git_config_entry));
+ GIT_ERROR_CHECK_ALLOC(duplicated);
+
+ duplicated->name = git__strdup(entry->name);
+ GIT_ERROR_CHECK_ALLOC(duplicated->name);
+
+ if (entry->value) {
+ duplicated->value = git__strdup(entry->value);
+ GIT_ERROR_CHECK_ALLOC(duplicated->value);
+ }
+ duplicated->level = entry->level;
+ duplicated->include_depth = entry->include_depth;
+
+ if ((error = git_config_entries_append(entries, duplicated)) < 0)
+ goto out;
+
+out:
+ if (error && duplicated) {
+ git__free((char *) duplicated->name);
+ git__free((char *) duplicated->value);
+ git__free(duplicated);
+ }
+ return error;
+}
+
int git_config_entries_dup(git_config_entries **out, git_config_entries *entries)
{
git_config_entries *result = NULL;
if ((error = git_config_entries_new(&result)) < 0)
goto out;
- for (head = entries->list; head; head = head->next) {
- git_config_entry *dup;
-
- dup = git__calloc(1, sizeof(git_config_entry));
- dup->name = git__strdup(head->entry->name);
- GIT_ERROR_CHECK_ALLOC(dup->name);
- if (head->entry->value) {
- dup->value = git__strdup(head->entry->value);
- GIT_ERROR_CHECK_ALLOC(dup->value);
- }
- dup->level = head->entry->level;
- dup->include_depth = head->entry->include_depth;
-
- if ((error = git_config_entries_append(result, dup)) < 0)
+ for (head = entries->list; head; head = head->next)
+ if ((git_config_entries_dup_entry(result, head->entry)) < 0)
goto out;
- }
*out = result;
result = NULL;
static void config_entries_free(git_config_entries *entries)
{
config_entry_list *list = NULL, *next;
+ config_entry_map_head *head;
- git_strmap_foreach_value(entries->map, list, config_entry_list_free(list));
+ 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;
}
int git_config_entries_append(git_config_entries *entries, git_config_entry *entry)
{
- config_entry_list *existing, *var;
- int error = 0;
- size_t pos;
+ config_entry_list *list_head;
+ config_entry_map_head *map_head;
- var = git__calloc(1, sizeof(config_entry_list));
- GIT_ERROR_CHECK_ALLOC(var);
- var->entry = entry;
-
- pos = git_strmap_lookup_index(entries->map, entry->name);
- if (!git_strmap_valid_index(entries->map, pos)) {
+ if ((map_head = git_strmap_get(entries->map, entry->name)) != NULL) {
+ map_head->multivar = true;
/*
- * We only ever inspect `last` from the first config
- * entry in a multivar. In case where this new entry is
- * the first one in the entry map, it will also be the
- * last one at the time of adding it, which is
- * why we set `last` here to itself. Otherwise we
- * do not have to set `last` and leave it set to
- * `NULL`.
+ * 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
+ * all except the first entry's name and just re-use it.
*/
- var->last = var;
-
- git_strmap_insert(entries->map, entry->name, var, &error);
-
- if (error > 0)
- error = 0;
+ git__free((char *) entry->name);
+ entry->name = map_head->entry->name;
} else {
- existing = git_strmap_value_at(entries->map, pos);
- config_entry_list_append(&existing, var);
+ map_head = git__calloc(1, sizeof(*map_head));
+ if ((git_strmap_set(entries->map, entry->name, map_head)) < 0)
+ return -1;
}
+ map_head->entry = entry;
- var = git__calloc(1, sizeof(config_entry_list));
- GIT_ERROR_CHECK_ALLOC(var);
- var->entry = entry;
- config_entry_list_append(&entries->list, var);
-
- return error;
-}
-
-int config_entry_get(config_entry_list **out, git_config_entries *entries, const char *key)
-{
- size_t pos;
-
- pos = git_strmap_lookup_index(entries->map, key);
+ list_head = git__calloc(1, sizeof(config_entry_list));
+ GIT_ERROR_CHECK_ALLOC(list_head);
+ list_head->entry = entry;
- /* no error message; the config system will write one */
- if (!git_strmap_valid_index(entries->map, pos))
- return GIT_ENOTFOUND;
-
- *out = git_strmap_value_at(entries->map, pos);
+ if (entries->list)
+ entries->list->last->next = list_head;
+ else
+ entries->list = list_head;
+ entries->list->last = list_head;
return 0;
}
int git_config_entries_get(git_config_entry **out, git_config_entries *entries, const char *key)
{
- config_entry_list *entry;
- int error;
-
- if ((error = config_entry_get(&entry, entries, key)) < 0)
- return error;
- *out = entry->last->entry;
-
+ config_entry_map_head *entry;
+ if ((entry = git_strmap_get(entries->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)
{
- config_entry_list *entry;
- int error;
+ config_entry_map_head *entry;
- if ((error = config_entry_get(&entry, entries, key)) < 0)
- return error;
+ if ((entry = git_strmap_get(entries->map, key)) == NULL)
+ return GIT_ENOTFOUND;
- if (entry->next != NULL) {
+ if (entry->multivar) {
git_error_set(GIT_ERROR_CONFIG, "entry is not unique due to being a multivar");
return -1;
}
return 0;
}
-void config_iterator_free(git_config_iterator *iter)
+static void config_iterator_free(git_config_iterator *iter)
{
config_entries_iterator *it = (config_entries_iterator *) iter;
git_config_entries_free(it->entries);
git__free(it);
}
-int config_iterator_next(
+static int config_iterator_next(
git_config_entry **entry,
git_config_iterator *iter)
{