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 4efc8dcb 2023-08-29 thomas
17 4efc8dcb 2023-08-29 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 99e30d93 2024-01-31 thomas static int
54 99e30d93 2024-01-31 thomas skip_node(struct got_gitconfig *gitconfig,
55 99e30d93 2024-01-31 thomas struct got_gitconfig_list_node *node)
56 99e30d93 2024-01-31 thomas {
57 99e30d93 2024-01-31 thomas /*
58 99e30d93 2024-01-31 thomas * Skip config nodes which do not describe remotes, and remotes
59 99e30d93 2024-01-31 thomas * which do not have a fetch URL defined (as used by git-annex).
60 99e30d93 2024-01-31 thomas */
61 99e30d93 2024-01-31 thomas return (strncasecmp("remote \"", node->field, 8) != 0 ||
62 99e30d93 2024-01-31 thomas got_gitconfig_get_str(gitconfig, node->field, "url") == NULL);
63 99e30d93 2024-01-31 thomas }
64 99e30d93 2024-01-31 thomas
65 3efd8e31 2022-10-23 thomas const struct got_error *
66 3efd8e31 2022-10-23 thomas got_repo_read_gitconfig(int *gitconfig_repository_format_version,
67 3efd8e31 2022-10-23 thomas char **gitconfig_author_name, char **gitconfig_author_email,
68 3efd8e31 2022-10-23 thomas struct got_remote_repo **remotes, int *nremotes,
69 01a217a7 2023-02-07 thomas char **gitconfig_owner, char ***extnames, char ***extvals,
70 01a217a7 2023-02-07 thomas int *nextensions, const char *gitconfig_path)
71 3efd8e31 2022-10-23 thomas {
72 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
73 3efd8e31 2022-10-23 thomas struct got_gitconfig *gitconfig = NULL;
74 3efd8e31 2022-10-23 thomas struct got_gitconfig_list *tags;
75 3efd8e31 2022-10-23 thomas struct got_gitconfig_list_node *node;
76 3efd8e31 2022-10-23 thomas int fd, i;
77 3efd8e31 2022-10-23 thomas const char *author, *email, *owner;
78 3efd8e31 2022-10-23 thomas
79 3efd8e31 2022-10-23 thomas *gitconfig_repository_format_version = 0;
80 01a217a7 2023-02-07 thomas if (extnames)
81 01a217a7 2023-02-07 thomas *extnames = NULL;
82 01a217a7 2023-02-07 thomas if (extvals)
83 01a217a7 2023-02-07 thomas *extvals = NULL;
84 3efd8e31 2022-10-23 thomas if (nextensions)
85 3efd8e31 2022-10-23 thomas *nextensions = 0;
86 3efd8e31 2022-10-23 thomas *gitconfig_author_name = NULL;
87 3efd8e31 2022-10-23 thomas *gitconfig_author_email = NULL;
88 3efd8e31 2022-10-23 thomas if (remotes)
89 3efd8e31 2022-10-23 thomas *remotes = NULL;
90 3efd8e31 2022-10-23 thomas if (nremotes)
91 3efd8e31 2022-10-23 thomas *nremotes = 0;
92 3efd8e31 2022-10-23 thomas if (gitconfig_owner)
93 3efd8e31 2022-10-23 thomas *gitconfig_owner = NULL;
94 3efd8e31 2022-10-23 thomas
95 3efd8e31 2022-10-23 thomas fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
96 3efd8e31 2022-10-23 thomas if (fd == -1) {
97 3efd8e31 2022-10-23 thomas if (errno == ENOENT)
98 3efd8e31 2022-10-23 thomas return NULL;
99 3efd8e31 2022-10-23 thomas return got_error_from_errno2("open", gitconfig_path);
100 3efd8e31 2022-10-23 thomas }
101 3efd8e31 2022-10-23 thomas
102 3efd8e31 2022-10-23 thomas err = got_gitconfig_open(&gitconfig, fd);
103 3efd8e31 2022-10-23 thomas if (err)
104 3efd8e31 2022-10-23 thomas goto done;
105 3efd8e31 2022-10-23 thomas
106 3efd8e31 2022-10-23 thomas *gitconfig_repository_format_version = got_gitconfig_get_num(gitconfig,
107 3efd8e31 2022-10-23 thomas "core", "repositoryformatversion", 0);
108 3efd8e31 2022-10-23 thomas
109 3efd8e31 2022-10-23 thomas tags = got_gitconfig_get_tag_list(gitconfig, "extensions");
110 01a217a7 2023-02-07 thomas if (extnames && extvals && nextensions && tags) {
111 3efd8e31 2022-10-23 thomas size_t numext = 0;
112 3efd8e31 2022-10-23 thomas TAILQ_FOREACH(node, &tags->fields, link) {
113 3efd8e31 2022-10-23 thomas char *ext = node->field;
114 3efd8e31 2022-10-23 thomas char *val = got_gitconfig_get_str(gitconfig,
115 3efd8e31 2022-10-23 thomas "extensions", ext);
116 3efd8e31 2022-10-23 thomas if (get_boolean_val(val))
117 3efd8e31 2022-10-23 thomas numext++;
118 3efd8e31 2022-10-23 thomas }
119 01a217a7 2023-02-07 thomas *extnames = calloc(numext, sizeof(char *));
120 01a217a7 2023-02-07 thomas if (*extnames == NULL) {
121 3efd8e31 2022-10-23 thomas err = got_error_from_errno("calloc");
122 3efd8e31 2022-10-23 thomas goto done;
123 3efd8e31 2022-10-23 thomas }
124 01a217a7 2023-02-07 thomas *extvals = calloc(numext, sizeof(char *));
125 01a217a7 2023-02-07 thomas if (*extvals == NULL) {
126 01a217a7 2023-02-07 thomas err = got_error_from_errno("calloc");
127 01a217a7 2023-02-07 thomas goto done;
128 01a217a7 2023-02-07 thomas }
129 3efd8e31 2022-10-23 thomas TAILQ_FOREACH(node, &tags->fields, link) {
130 3efd8e31 2022-10-23 thomas char *ext = node->field;
131 3efd8e31 2022-10-23 thomas char *val = got_gitconfig_get_str(gitconfig,
132 3efd8e31 2022-10-23 thomas "extensions", ext);
133 3efd8e31 2022-10-23 thomas if (get_boolean_val(val)) {
134 01a217a7 2023-02-07 thomas char *extstr = NULL, *valstr = NULL;
135 01a217a7 2023-02-07 thomas
136 01a217a7 2023-02-07 thomas extstr = strdup(ext);
137 3efd8e31 2022-10-23 thomas if (extstr == NULL) {
138 3efd8e31 2022-10-23 thomas err = got_error_from_errno("strdup");
139 3efd8e31 2022-10-23 thomas goto done;
140 3efd8e31 2022-10-23 thomas }
141 01a217a7 2023-02-07 thomas valstr = strdup(val);
142 01a217a7 2023-02-07 thomas if (valstr == NULL) {
143 01a217a7 2023-02-07 thomas err = got_error_from_errno("strdup");
144 01a217a7 2023-02-07 thomas goto done;
145 01a217a7 2023-02-07 thomas }
146 01a217a7 2023-02-07 thomas (*extnames)[(*nextensions)] = extstr;
147 01a217a7 2023-02-07 thomas (*extvals)[(*nextensions)] = valstr;
148 3efd8e31 2022-10-23 thomas (*nextensions)++;
149 3efd8e31 2022-10-23 thomas }
150 3efd8e31 2022-10-23 thomas }
151 3efd8e31 2022-10-23 thomas }
152 3efd8e31 2022-10-23 thomas
153 3efd8e31 2022-10-23 thomas author = got_gitconfig_get_str(gitconfig, "user", "name");
154 3efd8e31 2022-10-23 thomas if (author) {
155 3efd8e31 2022-10-23 thomas *gitconfig_author_name = strdup(author);
156 3efd8e31 2022-10-23 thomas if (*gitconfig_author_name == NULL) {
157 3efd8e31 2022-10-23 thomas err = got_error_from_errno("strdup");
158 3efd8e31 2022-10-23 thomas goto done;
159 3efd8e31 2022-10-23 thomas }
160 3efd8e31 2022-10-23 thomas }
161 3efd8e31 2022-10-23 thomas
162 3efd8e31 2022-10-23 thomas email = got_gitconfig_get_str(gitconfig, "user", "email");
163 3efd8e31 2022-10-23 thomas if (email) {
164 3efd8e31 2022-10-23 thomas *gitconfig_author_email = strdup(email);
165 3efd8e31 2022-10-23 thomas if (*gitconfig_author_email == NULL) {
166 3efd8e31 2022-10-23 thomas err = got_error_from_errno("strdup");
167 3efd8e31 2022-10-23 thomas goto done;
168 3efd8e31 2022-10-23 thomas }
169 3efd8e31 2022-10-23 thomas }
170 3efd8e31 2022-10-23 thomas
171 3efd8e31 2022-10-23 thomas if (gitconfig_owner) {
172 3efd8e31 2022-10-23 thomas owner = got_gitconfig_get_str(gitconfig, "gotweb", "owner");
173 3efd8e31 2022-10-23 thomas if (owner == NULL)
174 3efd8e31 2022-10-23 thomas owner = got_gitconfig_get_str(gitconfig, "gitweb",
175 3efd8e31 2022-10-23 thomas "owner");
176 3efd8e31 2022-10-23 thomas if (owner) {
177 3efd8e31 2022-10-23 thomas *gitconfig_owner = strdup(owner);
178 3efd8e31 2022-10-23 thomas if (*gitconfig_owner == NULL) {
179 3efd8e31 2022-10-23 thomas err = got_error_from_errno("strdup");
180 3efd8e31 2022-10-23 thomas goto done;
181 3efd8e31 2022-10-23 thomas }
182 3efd8e31 2022-10-23 thomas
183 3efd8e31 2022-10-23 thomas }
184 3efd8e31 2022-10-23 thomas }
185 3efd8e31 2022-10-23 thomas
186 3efd8e31 2022-10-23 thomas if (remotes && nremotes) {
187 3efd8e31 2022-10-23 thomas struct got_gitconfig_list *sections;
188 3efd8e31 2022-10-23 thomas size_t nalloc = 0;
189 3efd8e31 2022-10-23 thomas err = got_gitconfig_get_section_list(&sections, gitconfig);
190 3efd8e31 2022-10-23 thomas if (err)
191 3efd8e31 2022-10-23 thomas return err;
192 3efd8e31 2022-10-23 thomas TAILQ_FOREACH(node, &sections->fields, link) {
193 99e30d93 2024-01-31 thomas if (skip_node(gitconfig, node))
194 3efd8e31 2022-10-23 thomas continue;
195 3efd8e31 2022-10-23 thomas nalloc++;
196 3efd8e31 2022-10-23 thomas }
197 3efd8e31 2022-10-23 thomas
198 3efd8e31 2022-10-23 thomas *remotes = recallocarray(NULL, 0, nalloc, sizeof(**remotes));
199 3efd8e31 2022-10-23 thomas if (*remotes == NULL) {
200 3efd8e31 2022-10-23 thomas err = got_error_from_errno("recallocarray");
201 3efd8e31 2022-10-23 thomas goto done;
202 3efd8e31 2022-10-23 thomas }
203 3efd8e31 2022-10-23 thomas
204 3efd8e31 2022-10-23 thomas i = 0;
205 3efd8e31 2022-10-23 thomas TAILQ_FOREACH(node, &sections->fields, link) {
206 3efd8e31 2022-10-23 thomas struct got_remote_repo *remote;
207 3efd8e31 2022-10-23 thomas char *name, *end, *mirror;
208 3efd8e31 2022-10-23 thomas const char *fetch_url, *send_url;
209 3efd8e31 2022-10-23 thomas
210 99e30d93 2024-01-31 thomas if (skip_node(gitconfig, node) != 0)
211 3efd8e31 2022-10-23 thomas continue;
212 3efd8e31 2022-10-23 thomas
213 3efd8e31 2022-10-23 thomas remote = &(*remotes)[i];
214 3efd8e31 2022-10-23 thomas
215 3efd8e31 2022-10-23 thomas name = strdup(node->field + 8);
216 3efd8e31 2022-10-23 thomas if (name == NULL) {
217 3efd8e31 2022-10-23 thomas err = got_error_from_errno("strdup");
218 3efd8e31 2022-10-23 thomas goto done;
219 3efd8e31 2022-10-23 thomas }
220 3efd8e31 2022-10-23 thomas end = strrchr(name, '"');
221 3efd8e31 2022-10-23 thomas if (end)
222 3efd8e31 2022-10-23 thomas *end = '\0';
223 3efd8e31 2022-10-23 thomas remote->name = name;
224 3efd8e31 2022-10-23 thomas
225 3efd8e31 2022-10-23 thomas fetch_url = got_gitconfig_get_str(gitconfig,
226 3efd8e31 2022-10-23 thomas node->field, "url");
227 3efd8e31 2022-10-23 thomas remote->fetch_url = strdup(fetch_url);
228 3efd8e31 2022-10-23 thomas if (remote->fetch_url == NULL) {
229 3efd8e31 2022-10-23 thomas err = got_error_from_errno("strdup");
230 3efd8e31 2022-10-23 thomas free(remote->name);
231 3efd8e31 2022-10-23 thomas remote->name = NULL;
232 3efd8e31 2022-10-23 thomas goto done;
233 3efd8e31 2022-10-23 thomas }
234 3efd8e31 2022-10-23 thomas
235 3efd8e31 2022-10-23 thomas send_url = got_gitconfig_get_str(gitconfig,
236 3efd8e31 2022-10-23 thomas node->field, "pushurl");
237 3efd8e31 2022-10-23 thomas if (send_url == NULL)
238 3efd8e31 2022-10-23 thomas send_url = got_gitconfig_get_str(gitconfig,
239 3efd8e31 2022-10-23 thomas node->field, "url");
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;
248 3efd8e31 2022-10-23 thomas }
249 3efd8e31 2022-10-23 thomas
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;
255 3efd8e31 2022-10-23 thomas
256 3efd8e31 2022-10-23 thomas i++;
257 3efd8e31 2022-10-23 thomas (*nremotes)++;
258 3efd8e31 2022-10-23 thomas }
259 3efd8e31 2022-10-23 thomas }
260 3efd8e31 2022-10-23 thomas done:
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]);
270 01a217a7 2023-02-07 thomas }
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;
276 3efd8e31 2022-10-23 thomas }
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);
284 3efd8e31 2022-10-23 thomas }
285 3efd8e31 2022-10-23 thomas free(*remotes);
286 3efd8e31 2022-10-23 thomas *remotes = NULL;
287 3efd8e31 2022-10-23 thomas *nremotes = 0;
288 3efd8e31 2022-10-23 thomas }
289 3efd8e31 2022-10-23 thomas }
290 3efd8e31 2022-10-23 thomas return err;
291 3efd8e31 2022-10-23 thomas }