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