]> git.proxmox.com Git - libgit2.git/blob - src/config_entries.c
Update branching information in d/gbp.conf
[libgit2.git] / src / config_entries.c
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7
8 #include "config_entries.h"
9
10 typedef struct config_entry_list {
11 struct config_entry_list *next;
12 struct config_entry_list *last;
13 git_config_entry *entry;
14 bool first;
15 } config_entry_list;
16
17 typedef struct config_entries_iterator {
18 git_config_iterator parent;
19 git_config_entries *entries;
20 config_entry_list *head;
21 } config_entries_iterator;
22
23 struct git_config_entries {
24 git_refcount rc;
25 git_strmap *map;
26 config_entry_list *list;
27 };
28
29 int git_config_entries_new(git_config_entries **out)
30 {
31 git_config_entries *entries;
32 int error;
33
34 entries = git__calloc(1, sizeof(git_config_entries));
35 GIT_ERROR_CHECK_ALLOC(entries);
36 GIT_REFCOUNT_INC(entries);
37
38 if ((error = git_strmap_new(&entries->map)) < 0)
39 git__free(entries);
40 else
41 *out = entries;
42
43 return error;
44 }
45
46 int git_config_entries_dup_entry(git_config_entries *entries, const git_config_entry *entry)
47 {
48 git_config_entry *duplicated;
49 int error;
50
51 duplicated = git__calloc(1, sizeof(git_config_entry));
52 GIT_ERROR_CHECK_ALLOC(duplicated);
53
54 duplicated->name = git__strdup(entry->name);
55 GIT_ERROR_CHECK_ALLOC(duplicated->name);
56
57 if (entry->value) {
58 duplicated->value = git__strdup(entry->value);
59 GIT_ERROR_CHECK_ALLOC(duplicated->value);
60 }
61 duplicated->level = entry->level;
62 duplicated->include_depth = entry->include_depth;
63
64 if ((error = git_config_entries_append(entries, duplicated)) < 0)
65 goto out;
66
67 out:
68 if (error && duplicated) {
69 git__free((char *) duplicated->name);
70 git__free((char *) duplicated->value);
71 git__free(duplicated);
72 }
73 return error;
74 }
75
76 int git_config_entries_dup(git_config_entries **out, git_config_entries *entries)
77 {
78 git_config_entries *result = NULL;
79 config_entry_list *head;
80 int error;
81
82 if ((error = git_config_entries_new(&result)) < 0)
83 goto out;
84
85 for (head = entries->list; head; head = head->next)
86 if ((git_config_entries_dup_entry(result, head->entry)) < 0)
87 goto out;
88
89 *out = result;
90 result = NULL;
91
92 out:
93 git_config_entries_free(result);
94 return error;
95 }
96
97 void git_config_entries_incref(git_config_entries *entries)
98 {
99 GIT_REFCOUNT_INC(entries);
100 }
101
102 static void config_entries_free(git_config_entries *entries)
103 {
104 config_entry_list *list = NULL, *next;
105
106 git_strmap_free(entries->map);
107
108 list = entries->list;
109 while (list != NULL) {
110 next = list->next;
111 if (list->first)
112 git__free((char *) list->entry->name);
113 git__free((char *) list->entry->value);
114 git__free(list->entry);
115 git__free(list);
116 list = next;
117 }
118
119 git__free(entries);
120 }
121
122 void git_config_entries_free(git_config_entries *entries)
123 {
124 if (entries)
125 GIT_REFCOUNT_DEC(entries, config_entries_free);
126 }
127
128 int git_config_entries_append(git_config_entries *entries, git_config_entry *entry)
129 {
130 config_entry_list *existing, *head;
131
132 head = git__calloc(1, sizeof(config_entry_list));
133 GIT_ERROR_CHECK_ALLOC(head);
134 head->entry = entry;
135
136 /*
137 * This is a micro-optimization for configuration files
138 * with a lot of same keys. As for multivars the entry's
139 * key will be the same for all entries, we can just free
140 * all except the first entry's name and just re-use it.
141 */
142 if ((existing = git_strmap_get(entries->map, entry->name)) != NULL) {
143 git__free((char *) entry->name);
144 entry->name = existing->entry->name;
145 } else {
146 head->first = 1;
147 }
148
149 if (entries->list)
150 entries->list->last->next = head;
151 else
152 entries->list = head;
153 entries->list->last = head;
154
155 if (git_strmap_set(entries->map, entry->name, head) < 0)
156 return -1;
157
158 return 0;
159 }
160
161 int git_config_entries_get(git_config_entry **out, git_config_entries *entries, const char *key)
162 {
163 config_entry_list *entry;
164 if ((entry = git_strmap_get(entries->map, key)) == NULL)
165 return GIT_ENOTFOUND;
166 *out = entry->entry;
167 return 0;
168 }
169
170 int git_config_entries_get_unique(git_config_entry **out, git_config_entries *entries, const char *key)
171 {
172 config_entry_list *entry;
173
174 if ((entry = git_strmap_get(entries->map, key)) == NULL)
175 return GIT_ENOTFOUND;
176
177 if (!entry->first) {
178 git_error_set(GIT_ERROR_CONFIG, "entry is not unique due to being a multivar");
179 return -1;
180 }
181
182 if (entry->entry->include_depth) {
183 git_error_set(GIT_ERROR_CONFIG, "entry is not unique due to being included");
184 return -1;
185 }
186
187 *out = entry->entry;
188
189 return 0;
190 }
191
192 static void config_iterator_free(git_config_iterator *iter)
193 {
194 config_entries_iterator *it = (config_entries_iterator *) iter;
195 git_config_entries_free(it->entries);
196 git__free(it);
197 }
198
199 static int config_iterator_next(
200 git_config_entry **entry,
201 git_config_iterator *iter)
202 {
203 config_entries_iterator *it = (config_entries_iterator *) iter;
204
205 if (!it->head)
206 return GIT_ITEROVER;
207
208 *entry = it->head->entry;
209 it->head = it->head->next;
210
211 return 0;
212 }
213
214 int git_config_entries_iterator_new(git_config_iterator **out, git_config_entries *entries)
215 {
216 config_entries_iterator *it;
217
218 it = git__calloc(1, sizeof(config_entries_iterator));
219 GIT_ERROR_CHECK_ALLOC(it);
220 it->parent.next = config_iterator_next;
221 it->parent.free = config_iterator_free;
222 it->head = entries->list;
223 it->entries = entries;
224
225 git_config_entries_incref(entries);
226 *out = &it->parent;
227
228 return 0;
229 }