Blame


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