fix a typo in CHANGES
[got-portable.git] / lib / read_gitconfig.c
blob2996b7d372efa99bd37a6df25a128bf219e796ff
1 /*
2 * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include "got_compat.h"
19 #include <sys/queue.h>
21 #include <errno.h>
22 #include <event.h>
23 #include <fcntl.h>
24 #include <imsg.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <limits.h>
29 #include <unistd.h>
31 #include "got_error.h"
32 #include "got_object.h"
33 #include "got_repository.h"
34 #include "got_path.h"
36 #include "got_lib_gitconfig.h"
37 #include "got_lib_delta.h"
38 #include "got_lib_hash.h"
39 #include "got_lib_object.h"
40 #include "got_lib_object_cache.h"
41 #include "got_lib_privsep.h"
42 #include "got_lib_pack.h"
43 #include "got_lib_repository.h"
45 static int
46 get_boolean_val(char *val)
48 return (strcasecmp(val, "true") == 0 ||
49 strcasecmp(val, "on") == 0 ||
50 strcasecmp(val, "yes") == 0 ||
51 strcmp(val, "1") == 0);
54 static int
55 skip_node(struct got_gitconfig *gitconfig,
56 struct got_gitconfig_list_node *node)
59 * Skip config nodes which do not describe remotes, and remotes
60 * which do not have a fetch URL defined (as used by git-annex).
62 return (strncasecmp("remote \"", node->field, 8) != 0 ||
63 got_gitconfig_get_str(gitconfig, node->field, "url") == NULL);
66 const struct got_error *
67 got_repo_read_gitconfig(int *gitconfig_repository_format_version,
68 char **gitconfig_author_name, char **gitconfig_author_email,
69 struct got_remote_repo **remotes, int *nremotes,
70 char **gitconfig_owner, char ***extnames, char ***extvals,
71 int *nextensions, const char *gitconfig_path)
73 const struct got_error *err = NULL;
74 struct got_gitconfig *gitconfig = NULL;
75 struct got_gitconfig_list *tags;
76 struct got_gitconfig_list_node *node;
77 int fd, i;
78 const char *author, *email, *owner;
80 *gitconfig_repository_format_version = 0;
81 if (extnames)
82 *extnames = NULL;
83 if (extvals)
84 *extvals = NULL;
85 if (nextensions)
86 *nextensions = 0;
87 *gitconfig_author_name = NULL;
88 *gitconfig_author_email = NULL;
89 if (remotes)
90 *remotes = NULL;
91 if (nremotes)
92 *nremotes = 0;
93 if (gitconfig_owner)
94 *gitconfig_owner = NULL;
96 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
97 if (fd == -1) {
98 if (errno == ENOENT)
99 return NULL;
100 return got_error_from_errno2("open", gitconfig_path);
103 err = got_gitconfig_open(&gitconfig, fd);
104 if (err)
105 goto done;
107 *gitconfig_repository_format_version = got_gitconfig_get_num(gitconfig,
108 "core", "repositoryformatversion", 0);
110 tags = got_gitconfig_get_tag_list(gitconfig, "extensions");
111 if (extnames && extvals && nextensions && tags) {
112 size_t numext = 0;
113 TAILQ_FOREACH(node, &tags->fields, link)
114 numext++;
115 *extnames = calloc(numext, sizeof(char *));
116 if (*extnames == NULL) {
117 err = got_error_from_errno("calloc");
118 goto done;
120 *extvals = calloc(numext, sizeof(char *));
121 if (*extvals == NULL) {
122 err = got_error_from_errno("calloc");
123 goto done;
125 TAILQ_FOREACH(node, &tags->fields, link) {
126 char *ext = node->field;
127 char *val = got_gitconfig_get_str(gitconfig,
128 "extensions", ext);
129 char *extstr = NULL, *valstr = NULL;
131 extstr = strdup(ext);
132 if (extstr == NULL) {
133 err = got_error_from_errno("strdup");
134 goto done;
136 valstr = strdup(val);
137 if (valstr == NULL) {
138 err = got_error_from_errno("strdup");
139 goto done;
141 (*extnames)[(*nextensions)] = extstr;
142 (*extvals)[(*nextensions)] = valstr;
143 (*nextensions)++;
147 author = got_gitconfig_get_str(gitconfig, "user", "name");
148 if (author) {
149 *gitconfig_author_name = strdup(author);
150 if (*gitconfig_author_name == NULL) {
151 err = got_error_from_errno("strdup");
152 goto done;
156 email = got_gitconfig_get_str(gitconfig, "user", "email");
157 if (email) {
158 *gitconfig_author_email = strdup(email);
159 if (*gitconfig_author_email == NULL) {
160 err = got_error_from_errno("strdup");
161 goto done;
165 if (gitconfig_owner) {
166 owner = got_gitconfig_get_str(gitconfig, "gotweb", "owner");
167 if (owner == NULL)
168 owner = got_gitconfig_get_str(gitconfig, "gitweb",
169 "owner");
170 if (owner) {
171 *gitconfig_owner = strdup(owner);
172 if (*gitconfig_owner == NULL) {
173 err = got_error_from_errno("strdup");
174 goto done;
180 if (remotes && nremotes) {
181 struct got_gitconfig_list *sections;
182 size_t nalloc = 0;
183 err = got_gitconfig_get_section_list(&sections, gitconfig);
184 if (err)
185 return err;
186 TAILQ_FOREACH(node, &sections->fields, link) {
187 if (skip_node(gitconfig, node))
188 continue;
189 nalloc++;
192 *remotes = recallocarray(NULL, 0, nalloc, sizeof(**remotes));
193 if (*remotes == NULL) {
194 err = got_error_from_errno("recallocarray");
195 goto done;
198 i = 0;
199 TAILQ_FOREACH(node, &sections->fields, link) {
200 struct got_remote_repo *remote;
201 char *name, *end, *mirror;
202 const char *fetch_url, *send_url;
204 if (skip_node(gitconfig, node) != 0)
205 continue;
207 remote = &(*remotes)[i];
209 name = strdup(node->field + 8);
210 if (name == NULL) {
211 err = got_error_from_errno("strdup");
212 goto done;
214 end = strrchr(name, '"');
215 if (end)
216 *end = '\0';
217 remote->name = name;
219 fetch_url = got_gitconfig_get_str(gitconfig,
220 node->field, "url");
221 remote->fetch_url = strdup(fetch_url);
222 if (remote->fetch_url == NULL) {
223 err = got_error_from_errno("strdup");
224 free(remote->name);
225 remote->name = NULL;
226 goto done;
229 send_url = got_gitconfig_get_str(gitconfig,
230 node->field, "pushurl");
231 if (send_url == NULL)
232 send_url = got_gitconfig_get_str(gitconfig,
233 node->field, "url");
234 remote->send_url = strdup(send_url);
235 if (remote->send_url == NULL) {
236 err = got_error_from_errno("strdup");
237 free(remote->name);
238 remote->name = NULL;
239 free(remote->fetch_url);
240 remote->fetch_url = NULL;
241 goto done;
244 remote->mirror_references = 0;
245 mirror = got_gitconfig_get_str(gitconfig, node->field,
246 "mirror");
247 if (mirror != NULL && get_boolean_val(mirror))
248 remote->mirror_references = 1;
250 i++;
251 (*nremotes)++;
254 done:
255 if (fd != -1)
256 close(fd);
257 if (gitconfig)
258 got_gitconfig_close(gitconfig);
259 if (err) {
260 if (extnames && extvals && nextensions) {
261 for (i = 0; i < (*nextensions); i++) {
262 free((*extnames)[i]);
263 free((*extvals)[i]);
265 free(*extnames);
266 *extnames = NULL;
267 free(*extvals);
268 *extvals = NULL;
269 *nextensions = 0;
271 if (remotes && nremotes) {
272 for (i = 0; i < (*nremotes); i++) {
273 struct got_remote_repo *remote;
274 remote = &(*remotes)[i];
275 free(remote->name);
276 free(remote->fetch_url);
277 free(remote->send_url);
279 free(*remotes);
280 *remotes = NULL;
281 *nremotes = 0;
284 return err;