Blame


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