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 **objectformat,
59 char ***extensions, int *nextensions,
60 const char *gitconfig_path)
61 {
62 const struct got_error *err = NULL;
63 struct got_gitconfig *gitconfig = NULL;
64 struct got_gitconfig_list *tags;
65 struct got_gitconfig_list_node *node;
66 int fd, i;
67 const char *author, *email, *owner;
69 *gitconfig_repository_format_version = 0;
70 if (extensions)
71 *extensions = NULL;
72 if (nextensions)
73 *nextensions = 0;
74 *gitconfig_author_name = NULL;
75 *gitconfig_author_email = NULL;
76 if (remotes)
77 *remotes = NULL;
78 if (nremotes)
79 *nremotes = 0;
80 if (gitconfig_owner)
81 *gitconfig_owner = NULL;
83 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
84 if (fd == -1) {
85 if (errno == ENOENT)
86 return NULL;
87 return got_error_from_errno2("open", gitconfig_path);
88 }
90 err = got_gitconfig_open(&gitconfig, fd);
91 if (err)
92 goto done;
94 *gitconfig_repository_format_version = got_gitconfig_get_num(gitconfig,
95 "core", "repositoryformatversion", 0);
97 tags = got_gitconfig_get_tag_list(gitconfig, "extensions");
98 if (extensions && nextensions && tags) {
99 size_t numext = 0;
100 TAILQ_FOREACH(node, &tags->fields, link) {
101 char *ext = node->field;
102 char *val = got_gitconfig_get_str(gitconfig,
103 "extensions", ext);
104 if (get_boolean_val(val))
105 numext++;
107 *extensions = calloc(numext, sizeof(char *));
108 if (*extensions == NULL) {
109 err = got_error_from_errno("calloc");
110 goto done;
112 TAILQ_FOREACH(node, &tags->fields, link) {
113 char *ext = node->field;
114 char *val = got_gitconfig_get_str(gitconfig,
115 "extensions", ext);
116 if (get_boolean_val(val)) {
117 char *extstr = strdup(ext);
118 if (extstr == NULL) {
119 err = got_error_from_errno("strdup");
120 goto done;
122 (*extensions)[(*nextensions)] = extstr;
123 (*nextensions)++;
125 if (objectformat && !strcmp(ext, "sha256")) {
126 free(*objectformat);
127 *objectformat = strdup(val);
128 if (*objectformat == NULL) {
129 err = got_error_from_errno("strdup");
130 goto done;
136 author = got_gitconfig_get_str(gitconfig, "user", "name");
137 if (author) {
138 *gitconfig_author_name = strdup(author);
139 if (*gitconfig_author_name == NULL) {
140 err = got_error_from_errno("strdup");
141 goto done;
145 email = got_gitconfig_get_str(gitconfig, "user", "email");
146 if (email) {
147 *gitconfig_author_email = strdup(email);
148 if (*gitconfig_author_email == NULL) {
149 err = got_error_from_errno("strdup");
150 goto done;
154 if (gitconfig_owner) {
155 owner = got_gitconfig_get_str(gitconfig, "gotweb", "owner");
156 if (owner == NULL)
157 owner = got_gitconfig_get_str(gitconfig, "gitweb",
158 "owner");
159 if (owner) {
160 *gitconfig_owner = strdup(owner);
161 if (*gitconfig_owner == NULL) {
162 err = got_error_from_errno("strdup");
163 goto done;
169 if (remotes && nremotes) {
170 struct got_gitconfig_list *sections;
171 size_t nalloc = 0;
172 err = got_gitconfig_get_section_list(&sections, gitconfig);
173 if (err)
174 return err;
175 TAILQ_FOREACH(node, &sections->fields, link) {
176 if (strncasecmp("remote \"", node->field, 8) != 0)
177 continue;
178 nalloc++;
181 *remotes = recallocarray(NULL, 0, nalloc, sizeof(**remotes));
182 if (*remotes == NULL) {
183 err = got_error_from_errno("recallocarray");
184 goto done;
187 i = 0;
188 TAILQ_FOREACH(node, &sections->fields, link) {
189 struct got_remote_repo *remote;
190 char *name, *end, *mirror;
191 const char *fetch_url, *send_url;
193 if (strncasecmp("remote \"", node->field, 8) != 0)
194 continue;
196 remote = &(*remotes)[i];
198 name = strdup(node->field + 8);
199 if (name == NULL) {
200 err = got_error_from_errno("strdup");
201 goto done;
203 end = strrchr(name, '"');
204 if (end)
205 *end = '\0';
206 remote->name = name;
208 fetch_url = got_gitconfig_get_str(gitconfig,
209 node->field, "url");
210 if (fetch_url == NULL) {
211 err = got_error(GOT_ERR_GITCONFIG_SYNTAX);
212 free(remote->name);
213 remote->name = NULL;
214 goto done;
216 remote->fetch_url = strdup(fetch_url);
217 if (remote->fetch_url == NULL) {
218 err = got_error_from_errno("strdup");
219 free(remote->name);
220 remote->name = NULL;
221 goto done;
224 send_url = got_gitconfig_get_str(gitconfig,
225 node->field, "pushurl");
226 if (send_url == NULL)
227 send_url = got_gitconfig_get_str(gitconfig,
228 node->field, "url");
229 if (send_url == NULL) {
230 err = got_error(GOT_ERR_GITCONFIG_SYNTAX);
231 free(remote->name);
232 remote->name = NULL;
233 free(remote->fetch_url);
234 remote->fetch_url = NULL;
235 goto done;
237 remote->send_url = strdup(send_url);
238 if (remote->send_url == NULL) {
239 err = got_error_from_errno("strdup");
240 free(remote->name);
241 remote->name = NULL;
242 free(remote->fetch_url);
243 remote->fetch_url = NULL;
244 goto done;
247 remote->mirror_references = 0;
248 mirror = got_gitconfig_get_str(gitconfig, node->field,
249 "mirror");
250 if (mirror != NULL && get_boolean_val(mirror))
251 remote->mirror_references = 1;
253 i++;
254 (*nremotes)++;
257 done:
258 if (fd != -1)
259 close(fd);
260 if (gitconfig)
261 got_gitconfig_close(gitconfig);
262 if (err) {
263 if (extensions && nextensions) {
264 for (i = 0; i < (*nextensions); i++)
265 free((*extensions)[i]);
266 free(*extensions);
267 *extensions = NULL;
268 *nextensions = 0;
270 if (remotes && nremotes) {
271 for (i = 0; i < (*nremotes); i++) {
272 struct got_remote_repo *remote;
273 remote = &(*remotes)[i];
274 free(remote->name);
275 free(remote->fetch_url);
276 free(remote->send_url);
278 free(*remotes);
279 *remotes = NULL;
280 *nremotes = 0;
283 return err;