2 3efd8e31 2022-10-23 thomas * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
4 3efd8e31 2022-10-23 thomas * Permission to use, copy, modify, and distribute this software for any
5 3efd8e31 2022-10-23 thomas * purpose with or without fee is hereby granted, provided that the above
6 3efd8e31 2022-10-23 thomas * copyright notice and this permission notice appear in all copies.
8 3efd8e31 2022-10-23 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 3efd8e31 2022-10-23 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 3efd8e31 2022-10-23 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 3efd8e31 2022-10-23 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 3efd8e31 2022-10-23 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 3efd8e31 2022-10-23 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 3efd8e31 2022-10-23 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 3efd8e31 2022-10-23 thomas #include <sys/queue.h>
19 3efd8e31 2022-10-23 thomas #include <errno.h>
20 3efd8e31 2022-10-23 thomas #include <event.h>
21 3efd8e31 2022-10-23 thomas #include <fcntl.h>
22 3efd8e31 2022-10-23 thomas #include <imsg.h>
23 3efd8e31 2022-10-23 thomas #include <stdio.h>
24 3efd8e31 2022-10-23 thomas #include <stdlib.h>
25 3efd8e31 2022-10-23 thomas #include <string.h>
26 3efd8e31 2022-10-23 thomas #include <limits.h>
27 3efd8e31 2022-10-23 thomas #include <unistd.h>
29 3efd8e31 2022-10-23 thomas #include "got_error.h"
30 3efd8e31 2022-10-23 thomas #include "got_object.h"
31 3efd8e31 2022-10-23 thomas #include "got_repository.h"
32 3efd8e31 2022-10-23 thomas #include "got_path.h"
34 3efd8e31 2022-10-23 thomas #include "got_lib_gitconfig.h"
35 3efd8e31 2022-10-23 thomas #include "got_lib_delta.h"
36 3efd8e31 2022-10-23 thomas #include "got_lib_object.h"
37 3efd8e31 2022-10-23 thomas #include "got_lib_object_cache.h"
38 3efd8e31 2022-10-23 thomas #include "got_lib_privsep.h"
39 3efd8e31 2022-10-23 thomas #include "got_lib_pack.h"
40 3efd8e31 2022-10-23 thomas #include "got_lib_repository.h"
42 3efd8e31 2022-10-23 thomas static int
43 3efd8e31 2022-10-23 thomas get_boolean_val(char *val)
45 3efd8e31 2022-10-23 thomas return (strcasecmp(val, "true") == 0 ||
46 3efd8e31 2022-10-23 thomas strcasecmp(val, "on") == 0 ||
47 3efd8e31 2022-10-23 thomas strcasecmp(val, "yes") == 0 ||
48 3efd8e31 2022-10-23 thomas strcmp(val, "1") == 0);
51 3efd8e31 2022-10-23 thomas const struct got_error *
52 3efd8e31 2022-10-23 thomas got_repo_read_gitconfig(int *gitconfig_repository_format_version,
53 3efd8e31 2022-10-23 thomas char **gitconfig_author_name, char **gitconfig_author_email,
54 3efd8e31 2022-10-23 thomas struct got_remote_repo **remotes, int *nremotes,
55 01a217a7 2023-02-07 thomas char **gitconfig_owner, char ***extnames, char ***extvals,
56 01a217a7 2023-02-07 thomas int *nextensions, const char *gitconfig_path)
58 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
59 3efd8e31 2022-10-23 thomas struct got_gitconfig *gitconfig = NULL;
60 3efd8e31 2022-10-23 thomas struct got_gitconfig_list *tags;
61 3efd8e31 2022-10-23 thomas struct got_gitconfig_list_node *node;
62 3efd8e31 2022-10-23 thomas int fd, i;
63 3efd8e31 2022-10-23 thomas const char *author, *email, *owner;
65 3efd8e31 2022-10-23 thomas *gitconfig_repository_format_version = 0;
66 01a217a7 2023-02-07 thomas if (extnames)
67 01a217a7 2023-02-07 thomas *extnames = NULL;
68 01a217a7 2023-02-07 thomas if (extvals)
69 01a217a7 2023-02-07 thomas *extvals = NULL;
70 3efd8e31 2022-10-23 thomas if (nextensions)
71 3efd8e31 2022-10-23 thomas *nextensions = 0;
72 3efd8e31 2022-10-23 thomas *gitconfig_author_name = NULL;
73 3efd8e31 2022-10-23 thomas *gitconfig_author_email = NULL;
74 3efd8e31 2022-10-23 thomas if (remotes)
75 3efd8e31 2022-10-23 thomas *remotes = NULL;
76 3efd8e31 2022-10-23 thomas if (nremotes)
77 3efd8e31 2022-10-23 thomas *nremotes = 0;
78 3efd8e31 2022-10-23 thomas if (gitconfig_owner)
79 3efd8e31 2022-10-23 thomas *gitconfig_owner = NULL;
81 3efd8e31 2022-10-23 thomas fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
82 3efd8e31 2022-10-23 thomas if (fd == -1) {
83 3efd8e31 2022-10-23 thomas if (errno == ENOENT)
84 3efd8e31 2022-10-23 thomas return NULL;
85 3efd8e31 2022-10-23 thomas return got_error_from_errno2("open", gitconfig_path);
88 3efd8e31 2022-10-23 thomas err = got_gitconfig_open(&gitconfig, fd);
90 3efd8e31 2022-10-23 thomas goto done;
92 3efd8e31 2022-10-23 thomas *gitconfig_repository_format_version = got_gitconfig_get_num(gitconfig,
93 3efd8e31 2022-10-23 thomas "core", "repositoryformatversion", 0);
95 3efd8e31 2022-10-23 thomas tags = got_gitconfig_get_tag_list(gitconfig, "extensions");
96 01a217a7 2023-02-07 thomas if (extnames && extvals && nextensions && tags) {
97 3efd8e31 2022-10-23 thomas size_t numext = 0;
98 3efd8e31 2022-10-23 thomas TAILQ_FOREACH(node, &tags->fields, link) {
99 3efd8e31 2022-10-23 thomas char *ext = node->field;
100 3efd8e31 2022-10-23 thomas char *val = got_gitconfig_get_str(gitconfig,
101 3efd8e31 2022-10-23 thomas "extensions", ext);
102 3efd8e31 2022-10-23 thomas if (get_boolean_val(val))
103 3efd8e31 2022-10-23 thomas numext++;
105 01a217a7 2023-02-07 thomas *extnames = calloc(numext, sizeof(char *));
106 01a217a7 2023-02-07 thomas if (*extnames == NULL) {
107 3efd8e31 2022-10-23 thomas err = got_error_from_errno("calloc");
108 3efd8e31 2022-10-23 thomas goto done;
110 01a217a7 2023-02-07 thomas *extvals = calloc(numext, sizeof(char *));
111 01a217a7 2023-02-07 thomas if (*extvals == NULL) {
112 01a217a7 2023-02-07 thomas err = got_error_from_errno("calloc");
113 01a217a7 2023-02-07 thomas goto done;
115 3efd8e31 2022-10-23 thomas TAILQ_FOREACH(node, &tags->fields, link) {
116 3efd8e31 2022-10-23 thomas char *ext = node->field;
117 3efd8e31 2022-10-23 thomas char *val = got_gitconfig_get_str(gitconfig,
118 3efd8e31 2022-10-23 thomas "extensions", ext);
119 3efd8e31 2022-10-23 thomas if (get_boolean_val(val)) {
120 01a217a7 2023-02-07 thomas char *extstr = NULL, *valstr = NULL;
122 01a217a7 2023-02-07 thomas extstr = strdup(ext);
123 3efd8e31 2022-10-23 thomas if (extstr == NULL) {
124 3efd8e31 2022-10-23 thomas err = got_error_from_errno("strdup");
125 3efd8e31 2022-10-23 thomas goto done;
127 01a217a7 2023-02-07 thomas valstr = strdup(val);
128 01a217a7 2023-02-07 thomas if (valstr == NULL) {
129 01a217a7 2023-02-07 thomas err = got_error_from_errno("strdup");
130 01a217a7 2023-02-07 thomas goto done;
132 01a217a7 2023-02-07 thomas (*extnames)[(*nextensions)] = extstr;
133 01a217a7 2023-02-07 thomas (*extvals)[(*nextensions)] = valstr;
134 3efd8e31 2022-10-23 thomas (*nextensions)++;
139 3efd8e31 2022-10-23 thomas author = got_gitconfig_get_str(gitconfig, "user", "name");
140 3efd8e31 2022-10-23 thomas if (author) {
141 3efd8e31 2022-10-23 thomas *gitconfig_author_name = strdup(author);
142 3efd8e31 2022-10-23 thomas if (*gitconfig_author_name == NULL) {
143 3efd8e31 2022-10-23 thomas err = got_error_from_errno("strdup");
144 3efd8e31 2022-10-23 thomas goto done;
148 3efd8e31 2022-10-23 thomas email = got_gitconfig_get_str(gitconfig, "user", "email");
149 3efd8e31 2022-10-23 thomas if (email) {
150 3efd8e31 2022-10-23 thomas *gitconfig_author_email = strdup(email);
151 3efd8e31 2022-10-23 thomas if (*gitconfig_author_email == NULL) {
152 3efd8e31 2022-10-23 thomas err = got_error_from_errno("strdup");
153 3efd8e31 2022-10-23 thomas goto done;
157 3efd8e31 2022-10-23 thomas if (gitconfig_owner) {
158 3efd8e31 2022-10-23 thomas owner = got_gitconfig_get_str(gitconfig, "gotweb", "owner");
159 3efd8e31 2022-10-23 thomas if (owner == NULL)
160 3efd8e31 2022-10-23 thomas owner = got_gitconfig_get_str(gitconfig, "gitweb",
161 3efd8e31 2022-10-23 thomas "owner");
162 3efd8e31 2022-10-23 thomas if (owner) {
163 3efd8e31 2022-10-23 thomas *gitconfig_owner = strdup(owner);
164 3efd8e31 2022-10-23 thomas if (*gitconfig_owner == NULL) {
165 3efd8e31 2022-10-23 thomas err = got_error_from_errno("strdup");
166 3efd8e31 2022-10-23 thomas goto done;
172 3efd8e31 2022-10-23 thomas if (remotes && nremotes) {
173 3efd8e31 2022-10-23 thomas struct got_gitconfig_list *sections;
174 3efd8e31 2022-10-23 thomas size_t nalloc = 0;
175 3efd8e31 2022-10-23 thomas err = got_gitconfig_get_section_list(§ions, gitconfig);
177 3efd8e31 2022-10-23 thomas return err;
178 3efd8e31 2022-10-23 thomas TAILQ_FOREACH(node, §ions->fields, link) {
179 3efd8e31 2022-10-23 thomas if (strncasecmp("remote \"", node->field, 8) != 0)
180 3efd8e31 2022-10-23 thomas continue;
181 3efd8e31 2022-10-23 thomas nalloc++;
184 3efd8e31 2022-10-23 thomas *remotes = recallocarray(NULL, 0, nalloc, sizeof(**remotes));
185 3efd8e31 2022-10-23 thomas if (*remotes == NULL) {
186 3efd8e31 2022-10-23 thomas err = got_error_from_errno("recallocarray");
187 3efd8e31 2022-10-23 thomas goto done;
191 3efd8e31 2022-10-23 thomas TAILQ_FOREACH(node, §ions->fields, link) {
192 3efd8e31 2022-10-23 thomas struct got_remote_repo *remote;
193 3efd8e31 2022-10-23 thomas char *name, *end, *mirror;
194 3efd8e31 2022-10-23 thomas const char *fetch_url, *send_url;
196 3efd8e31 2022-10-23 thomas if (strncasecmp("remote \"", node->field, 8) != 0)
197 3efd8e31 2022-10-23 thomas continue;
199 3efd8e31 2022-10-23 thomas remote = &(*remotes)[i];
201 3efd8e31 2022-10-23 thomas name = strdup(node->field + 8);
202 3efd8e31 2022-10-23 thomas if (name == NULL) {
203 3efd8e31 2022-10-23 thomas err = got_error_from_errno("strdup");
204 3efd8e31 2022-10-23 thomas goto done;
206 3efd8e31 2022-10-23 thomas end = strrchr(name, '"');
208 3efd8e31 2022-10-23 thomas *end = '\0';
209 3efd8e31 2022-10-23 thomas remote->name = name;
211 3efd8e31 2022-10-23 thomas fetch_url = got_gitconfig_get_str(gitconfig,
212 3efd8e31 2022-10-23 thomas node->field, "url");
213 3efd8e31 2022-10-23 thomas if (fetch_url == NULL) {
214 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_GITCONFIG_SYNTAX);
215 3efd8e31 2022-10-23 thomas free(remote->name);
216 3efd8e31 2022-10-23 thomas remote->name = NULL;
217 3efd8e31 2022-10-23 thomas goto done;
219 3efd8e31 2022-10-23 thomas remote->fetch_url = strdup(fetch_url);
220 3efd8e31 2022-10-23 thomas if (remote->fetch_url == NULL) {
221 3efd8e31 2022-10-23 thomas err = got_error_from_errno("strdup");
222 3efd8e31 2022-10-23 thomas free(remote->name);
223 3efd8e31 2022-10-23 thomas remote->name = NULL;
224 3efd8e31 2022-10-23 thomas goto done;
227 3efd8e31 2022-10-23 thomas send_url = got_gitconfig_get_str(gitconfig,
228 3efd8e31 2022-10-23 thomas node->field, "pushurl");
229 3efd8e31 2022-10-23 thomas if (send_url == NULL)
230 3efd8e31 2022-10-23 thomas send_url = got_gitconfig_get_str(gitconfig,
231 3efd8e31 2022-10-23 thomas node->field, "url");
232 3efd8e31 2022-10-23 thomas if (send_url == NULL) {
233 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_GITCONFIG_SYNTAX);
234 3efd8e31 2022-10-23 thomas free(remote->name);
235 3efd8e31 2022-10-23 thomas remote->name = NULL;
236 3efd8e31 2022-10-23 thomas free(remote->fetch_url);
237 3efd8e31 2022-10-23 thomas remote->fetch_url = NULL;
238 3efd8e31 2022-10-23 thomas goto done;
240 3efd8e31 2022-10-23 thomas remote->send_url = strdup(send_url);
241 3efd8e31 2022-10-23 thomas if (remote->send_url == NULL) {
242 3efd8e31 2022-10-23 thomas err = got_error_from_errno("strdup");
243 3efd8e31 2022-10-23 thomas free(remote->name);
244 3efd8e31 2022-10-23 thomas remote->name = NULL;
245 3efd8e31 2022-10-23 thomas free(remote->fetch_url);
246 3efd8e31 2022-10-23 thomas remote->fetch_url = NULL;
247 3efd8e31 2022-10-23 thomas goto done;
250 3efd8e31 2022-10-23 thomas remote->mirror_references = 0;
251 3efd8e31 2022-10-23 thomas mirror = got_gitconfig_get_str(gitconfig, node->field,
252 3efd8e31 2022-10-23 thomas "mirror");
253 3efd8e31 2022-10-23 thomas if (mirror != NULL && get_boolean_val(mirror))
254 3efd8e31 2022-10-23 thomas remote->mirror_references = 1;
257 3efd8e31 2022-10-23 thomas (*nremotes)++;
261 3efd8e31 2022-10-23 thomas if (fd != -1)
262 3efd8e31 2022-10-23 thomas close(fd);
263 3efd8e31 2022-10-23 thomas if (gitconfig)
264 3efd8e31 2022-10-23 thomas got_gitconfig_close(gitconfig);
265 3efd8e31 2022-10-23 thomas if (err) {
266 01a217a7 2023-02-07 thomas if (extnames && extvals && nextensions) {
267 01a217a7 2023-02-07 thomas for (i = 0; i < (*nextensions); i++) {
268 01a217a7 2023-02-07 thomas free((*extnames)[i]);
269 01a217a7 2023-02-07 thomas free((*extvals)[i]);
271 01a217a7 2023-02-07 thomas free(*extnames);
272 01a217a7 2023-02-07 thomas *extnames = NULL;
273 01a217a7 2023-02-07 thomas free(*extvals);
274 01a217a7 2023-02-07 thomas *extvals = NULL;
275 3efd8e31 2022-10-23 thomas *nextensions = 0;
277 3efd8e31 2022-10-23 thomas if (remotes && nremotes) {
278 3efd8e31 2022-10-23 thomas for (i = 0; i < (*nremotes); i++) {
279 3efd8e31 2022-10-23 thomas struct got_remote_repo *remote;
280 3efd8e31 2022-10-23 thomas remote = &(*remotes)[i];
281 3efd8e31 2022-10-23 thomas free(remote->name);
282 3efd8e31 2022-10-23 thomas free(remote->fetch_url);
283 3efd8e31 2022-10-23 thomas free(remote->send_url);
285 3efd8e31 2022-10-23 thomas free(*remotes);
286 3efd8e31 2022-10-23 thomas *remotes = NULL;
287 3efd8e31 2022-10-23 thomas *nremotes = 0;
290 3efd8e31 2022-10-23 thomas return err;