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