| 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 |
|
| } config_entry_list; |
| 15 |
|
| |
| 16 |
|
| typedef struct { |
| 17 |
|
| git_config_entry *entry; |
| 18 |
|
| bool multivar; |
| 19 |
|
| } config_entry_map_head; |
| 20 |
|
| |
| 21 |
|
| typedef struct config_entries_iterator { |
| 22 |
|
| git_config_iterator parent; |
| 23 |
|
| git_config_entries *entries; |
| 24 |
|
| config_entry_list *head; |
| 25 |
|
| } config_entries_iterator; |
| 26 |
|
| |
| 27 |
|
| struct git_config_entries { |
| 28 |
|
| git_refcount rc; |
| 29 |
|
| git_strmap *map; |
| 30 |
|
| config_entry_list *list; |
| 31 |
|
| }; |
| 32 |
|
| |
| 33 |
2366 |
| int git_config_entries_new(git_config_entries **out) |
| 34 |
|
| { |
| 35 |
|
| git_config_entries *entries; |
| 36 |
|
| int error; |
| 37 |
|
| |
| 38 |
2366 |
| entries = git__calloc(1, sizeof(git_config_entries)); |
| 39 |
2366 |
▶ | GIT_ERROR_CHECK_ALLOC(entries); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ERROR_CHECK_ALLOC(entries); |
0 |
2366 |
2366 |
|
| 40 |
2366 |
| GIT_REFCOUNT_INC(entries); |
| 41 |
|
| |
| 42 |
2366 |
▶ | if ((error = git_strmap_new(&entries->map)) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((error = git_strmap_new(&entries->map)) < 0) |
0 |
2366 |
2366 |
|
| 43 |
0 |
| git__free(entries); |
| 44 |
|
| else |
| 45 |
2366 |
| *out = entries; |
| 46 |
|
| |
| 47 |
2366 |
| return error; |
| 48 |
|
| } |
| 49 |
|
| |
| 50 |
16144 |
| int git_config_entries_dup_entry(git_config_entries *entries, const git_config_entry *entry) |
| 51 |
|
| { |
| 52 |
|
| git_config_entry *duplicated; |
| 53 |
|
| int error; |
| 54 |
|
| |
| 55 |
16144 |
| duplicated = git__calloc(1, sizeof(git_config_entry)); |
| 56 |
16144 |
▶ | GIT_ERROR_CHECK_ALLOC(duplicated); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ERROR_CHECK_ALLOC(duplicated); |
0 |
16144 |
16144 |
|
| 57 |
|
| |
| 58 |
16144 |
| duplicated->name = git__strdup(entry->name); |
| 59 |
16144 |
▶ | GIT_ERROR_CHECK_ALLOC(duplicated->name); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ERROR_CHECK_ALLOC(duplicated->name); |
0 |
16144 |
16144 |
|
| 60 |
|
| |
| 61 |
16144 |
▶ | if (entry->value) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (entry->value) { |
16144 |
0 |
16144 |
|
| 62 |
16144 |
| duplicated->value = git__strdup(entry->value); |
| 63 |
16144 |
▶ | GIT_ERROR_CHECK_ALLOC(duplicated->value); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ERROR_CHECK_ALLOC(duplicated->value); |
0 |
16144 |
16144 |
|
| 64 |
|
| } |
| 65 |
16144 |
| duplicated->level = entry->level; |
| 66 |
16144 |
| duplicated->include_depth = entry->include_depth; |
| 67 |
|
| |
| 68 |
16144 |
▶ | if ((error = git_config_entries_append(entries, duplicated)) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((error = git_config_entries_append(entries, duplicated)) < 0) |
16144 |
0 |
16144 |
|
| 69 |
0 |
| goto out; |
| 70 |
|
| |
| 71 |
16144 |
| out: |
| 72 |
16144 |
▶ | if (error && duplicated) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (error && duplicated) { |
0 |
16144 |
16144 |
| if (error && duplicated) { |
0 |
0 |
0 |
|
| 73 |
0 |
| git__free((char *) duplicated->name); |
| 74 |
0 |
| git__free((char *) duplicated->value); |
| 75 |
0 |
| git__free(duplicated); |
| 76 |
|
| } |
| 77 |
16144 |
| return error; |
| 78 |
|
| } |
| 79 |
|
| |
| 80 |
1145 |
| int git_config_entries_dup(git_config_entries **out, git_config_entries *entries) |
| 81 |
|
| { |
| 82 |
1145 |
| git_config_entries *result = NULL; |
| 83 |
|
| config_entry_list *head; |
| 84 |
|
| int error; |
| 85 |
|
| |
| 86 |
1145 |
▶ | if ((error = git_config_entries_new(&result)) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((error = git_config_entries_new(&result)) < 0) |
0 |
1145 |
1145 |
|
| 87 |
0 |
| goto out; |
| 88 |
|
| |
| 89 |
9806 |
▶ | for (head = entries->list; head; head = head->next) |
Branch: 1 branch
| Branch | True | False | Total |
| for (head = entries->list; head; head = head->next) |
8661 |
1145 |
9806 |
|
| 90 |
8661 |
▶ | if ((git_config_entries_dup_entry(result, head->entry)) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((git_config_entries_dup_entry(result, head->entry)) < 0) |
0 |
8661 |
8661 |
|
| 91 |
0 |
| goto out; |
| 92 |
|
| |
| 93 |
1145 |
| *out = result; |
| 94 |
1145 |
| result = NULL; |
| 95 |
|
| |
| 96 |
1145 |
| out: |
| 97 |
1145 |
| git_config_entries_free(result); |
| 98 |
1145 |
| return error; |
| 99 |
|
| } |
| 100 |
|
| |
| 101 |
7463 |
| void git_config_entries_incref(git_config_entries *entries) |
| 102 |
|
| { |
| 103 |
7463 |
| GIT_REFCOUNT_INC(entries); |
| 104 |
7463 |
| } |
| 105 |
|
| |
| 106 |
2362 |
| static void config_entries_free(git_config_entries *entries) |
| 107 |
|
| { |
| 108 |
2362 |
| config_entry_list *list = NULL, *next; |
| 109 |
|
| config_entry_map_head *head; |
| 110 |
|
| |
| 111 |
19141 |
▶ | git_strmap_foreach_value(entries->map, head, |
Branch: 1 branch
| Branch | True | False | Total |
| git_strmap_foreach_value(entries->map, head, |
16779 |
2362 |
19141 |
|
| 112 |
|
| git__free((char *) head->entry->name); git__free(head) |
| 113 |
|
| ); |
| 114 |
2362 |
| git_strmap_free(entries->map); |
| 115 |
|
| |
| 116 |
2362 |
| list = entries->list; |
| 117 |
20059 |
▶ | while (list != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (list != NULL) { |
17697 |
2362 |
20059 |
|
| 118 |
17697 |
| next = list->next; |
| 119 |
17697 |
| git__free((char *) list->entry->value); |
| 120 |
17697 |
| git__free(list->entry); |
| 121 |
17697 |
| git__free(list); |
| 122 |
17697 |
| list = next; |
| 123 |
|
| } |
| 124 |
|
| |
| 125 |
2362 |
| git__free(entries); |
| 126 |
2362 |
| } |
| 127 |
|
| |
| 128 |
11039 |
| void git_config_entries_free(git_config_entries *entries) |
| 129 |
|
| { |
| 130 |
11039 |
▶ | if (entries) |
Branch: 1 branch
| Branch | True | False | Total |
| if (entries) |
9825 |
1214 |
11039 |
|
| 131 |
9825 |
▶ | GIT_REFCOUNT_DEC(entries, config_entries_free); |
Branch: 2 branches
| Branch | True | False | Total |
| GIT_REFCOUNT_DEC(entries, config_entries_free); |
2362 |
7463 |
9825 |
| GIT_REFCOUNT_DEC(entries, config_entries_free); |
2362 |
0 |
2362 |
|
| 132 |
11039 |
| } |
| 133 |
|
| |
| 134 |
17705 |
| int git_config_entries_append(git_config_entries *entries, git_config_entry *entry) |
| 135 |
|
| { |
| 136 |
|
| config_entry_list *list_head; |
| 137 |
|
| config_entry_map_head *map_head; |
| 138 |
|
| |
| 139 |
17705 |
▶ | if ((map_head = git_strmap_get(entries->map, entry->name)) != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((map_head = git_strmap_get(entries->map, entry->name)) != NULL) { |
918 |
16787 |
17705 |
|
| 140 |
918 |
| map_head->multivar = true; |
| 141 |
|
| /* |
| 142 |
|
| * This is a micro-optimization for configuration files |
| 143 |
|
| * with a lot of same keys. As for multivars the entry's |
| 144 |
|
| * key will be the same for all entries, we can just free |
| 145 |
|
| * all except the first entry's name and just re-use it. |
| 146 |
|
| */ |
| 147 |
918 |
| git__free((char *) entry->name); |
| 148 |
918 |
| entry->name = map_head->entry->name; |
| 149 |
|
| } else { |
| 150 |
16787 |
| map_head = git__calloc(1, sizeof(*map_head)); |
| 151 |
16787 |
▶ | if ((git_strmap_set(entries->map, entry->name, map_head)) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((git_strmap_set(entries->map, entry->name, map_head)) < 0) |
0 |
16787 |
16787 |
|
| 152 |
0 |
| return -1; |
| 153 |
|
| } |
| 154 |
17705 |
| map_head->entry = entry; |
| 155 |
|
| |
| 156 |
17705 |
| list_head = git__calloc(1, sizeof(config_entry_list)); |
| 157 |
17705 |
▶ | GIT_ERROR_CHECK_ALLOC(list_head); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ERROR_CHECK_ALLOC(list_head); |
0 |
17705 |
17705 |
|
| 158 |
17705 |
| list_head->entry = entry; |
| 159 |
|
| |
| 160 |
17705 |
▶ | if (entries->list) |
Branch: 1 branch
| Branch | True | False | Total |
| if (entries->list) |
15347 |
2358 |
17705 |
|
| 161 |
15347 |
| entries->list->last->next = list_head; |
| 162 |
|
| else |
| 163 |
2358 |
| entries->list = list_head; |
| 164 |
17705 |
| entries->list->last = list_head; |
| 165 |
|
| |
| 166 |
17705 |
| return 0; |
| 167 |
|
| } |
| 168 |
|
| |
| 169 |
5223 |
| int git_config_entries_get(git_config_entry **out, git_config_entries *entries, const char *key) |
| 170 |
|
| { |
| 171 |
|
| config_entry_map_head *entry; |
| 172 |
5223 |
▶ | if ((entry = git_strmap_get(entries->map, key)) == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((entry = git_strmap_get(entries->map, key)) == NULL) |
4323 |
900 |
5223 |
|
| 173 |
4323 |
| return GIT_ENOTFOUND; |
| 174 |
900 |
| *out = entry->entry; |
| 175 |
900 |
| return 0; |
| 176 |
|
| } |
| 177 |
|
| |
| 178 |
56 |
| int git_config_entries_get_unique(git_config_entry **out, git_config_entries *entries, const char *key) |
| 179 |
|
| { |
| 180 |
|
| config_entry_map_head *entry; |
| 181 |
|
| |
| 182 |
56 |
▶ | if ((entry = git_strmap_get(entries->map, key)) == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((entry = git_strmap_get(entries->map, key)) == NULL) |
47 |
9 |
56 |
|
| 183 |
47 |
| return GIT_ENOTFOUND; |
| 184 |
|
| |
| 185 |
9 |
▶ | if (entry->multivar) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (entry->multivar) { |
0 |
9 |
9 |
|
| 186 |
0 |
| git_error_set(GIT_ERROR_CONFIG, "entry is not unique due to being a multivar"); |
| 187 |
0 |
| return -1; |
| 188 |
|
| } |
| 189 |
|
| |
| 190 |
9 |
▶ | if (entry->entry->include_depth) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (entry->entry->include_depth) { |
0 |
9 |
9 |
|
| 191 |
0 |
| git_error_set(GIT_ERROR_CONFIG, "entry is not unique due to being included"); |
| 192 |
0 |
| return -1; |
| 193 |
|
| } |
| 194 |
|
| |
| 195 |
9 |
| *out = entry->entry; |
| 196 |
|
| |
| 197 |
9 |
| return 0; |
| 198 |
|
| } |
| 199 |
|
| |
| 200 |
1145 |
| static void config_iterator_free(git_config_iterator *iter) |
| 201 |
|
| { |
| 202 |
1145 |
| config_entries_iterator *it = (config_entries_iterator *) iter; |
| 203 |
1145 |
| git_config_entries_free(it->entries); |
| 204 |
1145 |
| git__free(it); |
| 205 |
1145 |
| } |
| 206 |
|
| |
| 207 |
9805 |
| static int config_iterator_next( |
| 208 |
|
| git_config_entry **entry, |
| 209 |
|
| git_config_iterator *iter) |
| 210 |
|
| { |
| 211 |
9805 |
| config_entries_iterator *it = (config_entries_iterator *) iter; |
| 212 |
|
| |
| 213 |
9805 |
▶ | if (!it->head) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!it->head) |
1144 |
8661 |
9805 |
|
| 214 |
1144 |
| return GIT_ITEROVER; |
| 215 |
|
| |
| 216 |
8661 |
| *entry = it->head->entry; |
| 217 |
8661 |
| it->head = it->head->next; |
| 218 |
|
| |
| 219 |
8661 |
| return 0; |
| 220 |
|
| } |
| 221 |
|
| |
| 222 |
1145 |
| int git_config_entries_iterator_new(git_config_iterator **out, git_config_entries *entries) |
| 223 |
|
| { |
| 224 |
|
| config_entries_iterator *it; |
| 225 |
|
| |
| 226 |
1145 |
| it = git__calloc(1, sizeof(config_entries_iterator)); |
| 227 |
1145 |
▶ | GIT_ERROR_CHECK_ALLOC(it); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ERROR_CHECK_ALLOC(it); |
0 |
1145 |
1145 |
|
| 228 |
1145 |
| it->parent.next = config_iterator_next; |
| 229 |
1145 |
| it->parent.free = config_iterator_free; |
| 230 |
1145 |
| it->head = entries->list; |
| 231 |
1145 |
| it->entries = entries; |
| 232 |
|
| |
| 233 |
1145 |
| git_config_entries_incref(entries); |
| 234 |
1145 |
| *out = &it->parent; |
| 235 |
|
| |
| 236 |
1145 |
| return 0; |
| 237 |
|
| } |