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