Blob


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