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