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