Blob


1 /*
2 * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/queue.h>
19 #include <errno.h>
20 #include <event.h>
21 #include <fcntl.h>
22 #include <imsg.h>
23 #include <sha2.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <limits.h>
28 #include <unistd.h>
30 #include "got_error.h"
31 #include "got_object.h"
32 #include "got_repository.h"
33 #include "got_path.h"
35 #include "got_lib_gitconfig.h"
36 #include "got_lib_delta.h"
37 #include "got_lib_object.h"
38 #include "got_lib_object_cache.h"
39 #include "got_lib_privsep.h"
40 #include "got_lib_pack.h"
41 #include "got_lib_repository.h"
43 static int
44 get_boolean_val(char *val)
45 {
46 return (strcasecmp(val, "true") == 0 ||
47 strcasecmp(val, "on") == 0 ||
48 strcasecmp(val, "yes") == 0 ||
49 strcmp(val, "1") == 0);
50 }
52 const struct got_error *
53 got_repo_read_gitconfig(int *gitconfig_repository_format_version,
54 char **gitconfig_author_name, char **gitconfig_author_email,
55 struct got_remote_repo **remotes, int *nremotes,
56 char **gitconfig_owner, char ***extnames, char ***extvals,
57 int *nextensions, const char *gitconfig_path)
58 {
59 const struct got_error *err = NULL;
60 struct got_gitconfig *gitconfig = NULL;
61 struct got_gitconfig_list *tags;
62 struct got_gitconfig_list_node *node;
63 int fd, i;
64 const char *author, *email, *owner;
66 *gitconfig_repository_format_version = 0;
67 if (extnames)
68 *extnames = NULL;
69 if (extvals)
70 *extvals = NULL;
71 if (nextensions)
72 *nextensions = 0;
73 *gitconfig_author_name = NULL;
74 *gitconfig_author_email = NULL;
75 if (remotes)
76 *remotes = NULL;
77 if (nremotes)
78 *nremotes = 0;
79 if (gitconfig_owner)
80 *gitconfig_owner = NULL;
82 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
83 if (fd == -1) {
84 if (errno == ENOENT)
85 return NULL;
86 return got_error_from_errno2("open", gitconfig_path);
87 }
89 err = got_gitconfig_open(&gitconfig, fd);
90 if (err)
91 goto done;
93 *gitconfig_repository_format_version = got_gitconfig_get_num(gitconfig,
94 "core", "repositoryformatversion", 0);
96 tags = got_gitconfig_get_tag_list(gitconfig, "extensions");
97 if (extnames && extvals && nextensions && tags) {
98 size_t numext = 0;
99 TAILQ_FOREACH(node, &tags->fields, link) {
100 char *ext = node->field;
101 char *val = got_gitconfig_get_str(gitconfig,
102 "extensions", ext);
103 if (get_boolean_val(val))
104 numext++;
106 *extnames = calloc(numext, sizeof(char *));
107 if (*extnames == NULL) {
108 err = got_error_from_errno("calloc");
109 goto done;
111 *extvals = calloc(numext, sizeof(char *));
112 if (*extvals == NULL) {
113 err = got_error_from_errno("calloc");
114 goto done;
116 TAILQ_FOREACH(node, &tags->fields, link) {
117 char *ext = node->field;
118 char *val = got_gitconfig_get_str(gitconfig,
119 "extensions", ext);
120 if (get_boolean_val(val)) {
121 char *extstr = NULL, *valstr = NULL;
123 extstr = strdup(ext);
124 if (extstr == NULL) {
125 err = got_error_from_errno("strdup");
126 goto done;
128 valstr = strdup(val);
129 if (valstr == NULL) {
130 err = got_error_from_errno("strdup");
131 goto done;
133 (*extnames)[(*nextensions)] = extstr;
134 (*extvals)[(*nextensions)] = valstr;
135 (*nextensions)++;
140 author = got_gitconfig_get_str(gitconfig, "user", "name");
141 if (author) {
142 *gitconfig_author_name = strdup(author);
143 if (*gitconfig_author_name == NULL) {
144 err = got_error_from_errno("strdup");
145 goto done;
149 email = got_gitconfig_get_str(gitconfig, "user", "email");
150 if (email) {
151 *gitconfig_author_email = strdup(email);
152 if (*gitconfig_author_email == NULL) {
153 err = got_error_from_errno("strdup");
154 goto done;
158 if (gitconfig_owner) {
159 owner = got_gitconfig_get_str(gitconfig, "gotweb", "owner");
160 if (owner == NULL)
161 owner = got_gitconfig_get_str(gitconfig, "gitweb",
162 "owner");
163 if (owner) {
164 *gitconfig_owner = strdup(owner);
165 if (*gitconfig_owner == NULL) {
166 err = got_error_from_errno("strdup");
167 goto done;
173 if (remotes && nremotes) {
174 struct got_gitconfig_list *sections;
175 size_t nalloc = 0;
176 err = got_gitconfig_get_section_list(&sections, gitconfig);
177 if (err)
178 return err;
179 TAILQ_FOREACH(node, &sections->fields, link) {
180 if (strncasecmp("remote \"", node->field, 8) != 0)
181 continue;
182 nalloc++;
185 *remotes = recallocarray(NULL, 0, nalloc, sizeof(**remotes));
186 if (*remotes == NULL) {
187 err = got_error_from_errno("recallocarray");
188 goto done;
191 i = 0;
192 TAILQ_FOREACH(node, &sections->fields, link) {
193 struct got_remote_repo *remote;
194 char *name, *end, *mirror;
195 const char *fetch_url, *send_url;
197 if (strncasecmp("remote \"", node->field, 8) != 0)
198 continue;
200 remote = &(*remotes)[i];
202 name = strdup(node->field + 8);
203 if (name == NULL) {
204 err = got_error_from_errno("strdup");
205 goto done;
207 end = strrchr(name, '"');
208 if (end)
209 *end = '\0';
210 remote->name = name;
212 fetch_url = got_gitconfig_get_str(gitconfig,
213 node->field, "url");
214 if (fetch_url == NULL) {
215 err = got_error(GOT_ERR_GITCONFIG_SYNTAX);
216 free(remote->name);
217 remote->name = NULL;
218 goto done;
220 remote->fetch_url = strdup(fetch_url);
221 if (remote->fetch_url == NULL) {
222 err = got_error_from_errno("strdup");
223 free(remote->name);
224 remote->name = NULL;
225 goto done;
228 send_url = got_gitconfig_get_str(gitconfig,
229 node->field, "pushurl");
230 if (send_url == NULL)
231 send_url = got_gitconfig_get_str(gitconfig,
232 node->field, "url");
233 if (send_url == NULL) {
234 err = got_error(GOT_ERR_GITCONFIG_SYNTAX);
235 free(remote->name);
236 remote->name = NULL;
237 free(remote->fetch_url);
238 remote->fetch_url = NULL;
239 goto done;
241 remote->send_url = strdup(send_url);
242 if (remote->send_url == NULL) {
243 err = got_error_from_errno("strdup");
244 free(remote->name);
245 remote->name = NULL;
246 free(remote->fetch_url);
247 remote->fetch_url = NULL;
248 goto done;
251 remote->mirror_references = 0;
252 mirror = got_gitconfig_get_str(gitconfig, node->field,
253 "mirror");
254 if (mirror != NULL && get_boolean_val(mirror))
255 remote->mirror_references = 1;
257 i++;
258 (*nremotes)++;
261 done:
262 if (fd != -1)
263 close(fd);
264 if (gitconfig)
265 got_gitconfig_close(gitconfig);
266 if (err) {
267 if (extnames && extvals && nextensions) {
268 for (i = 0; i < (*nextensions); i++) {
269 free((*extnames)[i]);
270 free((*extvals)[i]);
272 free(*extnames);
273 *extnames = NULL;
274 free(*extvals);
275 *extvals = NULL;
276 *nextensions = 0;
278 if (remotes && nremotes) {
279 for (i = 0; i < (*nremotes); i++) {
280 struct got_remote_repo *remote;
281 remote = &(*remotes)[i];
282 free(remote->name);
283 free(remote->fetch_url);
284 free(remote->send_url);
286 free(*remotes);
287 *remotes = NULL;
288 *nremotes = 0;
291 return err;