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 static int
55 skip_node(struct got_gitconfig *gitconfig,
56 struct got_gitconfig_list_node *node)
57 {
58 /*
59 * Skip config nodes which do not describe remotes, and remotes
60 * which do not have a fetch URL defined (as used by git-annex).
61 */
62 return (strncasecmp("remote \"", node->field, 8) != 0 ||
63 got_gitconfig_get_str(gitconfig, node->field, "url") == NULL);
64 }
66 const struct got_error *
67 got_repo_read_gitconfig(int *gitconfig_repository_format_version,
68 char **gitconfig_author_name, char **gitconfig_author_email,
69 struct got_remote_repo **remotes, int *nremotes,
70 char **gitconfig_owner, char ***extnames, char ***extvals,
71 int *nextensions, const char *gitconfig_path)
72 {
73 const struct got_error *err = NULL;
74 struct got_gitconfig *gitconfig = NULL;
75 struct got_gitconfig_list *tags;
76 struct got_gitconfig_list_node *node;
77 int fd, i;
78 const char *author, *email, *owner;
80 *gitconfig_repository_format_version = 0;
81 if (extnames)
82 *extnames = NULL;
83 if (extvals)
84 *extvals = NULL;
85 if (nextensions)
86 *nextensions = 0;
87 *gitconfig_author_name = NULL;
88 *gitconfig_author_email = NULL;
89 if (remotes)
90 *remotes = NULL;
91 if (nremotes)
92 *nremotes = 0;
93 if (gitconfig_owner)
94 *gitconfig_owner = NULL;
96 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
97 if (fd == -1) {
98 if (errno == ENOENT)
99 return NULL;
100 return got_error_from_errno2("open", gitconfig_path);
103 err = got_gitconfig_open(&gitconfig, fd);
104 if (err)
105 goto done;
107 *gitconfig_repository_format_version = got_gitconfig_get_num(gitconfig,
108 "core", "repositoryformatversion", 0);
110 tags = got_gitconfig_get_tag_list(gitconfig, "extensions");
111 if (extnames && extvals && nextensions && tags) {
112 size_t numext = 0;
113 TAILQ_FOREACH(node, &tags->fields, link) {
114 char *ext = node->field;
115 char *val = got_gitconfig_get_str(gitconfig,
116 "extensions", ext);
117 if (get_boolean_val(val))
118 numext++;
120 *extnames = calloc(numext, sizeof(char *));
121 if (*extnames == NULL) {
122 err = got_error_from_errno("calloc");
123 goto done;
125 *extvals = calloc(numext, sizeof(char *));
126 if (*extvals == NULL) {
127 err = got_error_from_errno("calloc");
128 goto done;
130 TAILQ_FOREACH(node, &tags->fields, link) {
131 char *ext = node->field;
132 char *val = got_gitconfig_get_str(gitconfig,
133 "extensions", ext);
134 if (get_boolean_val(val)) {
135 char *extstr = NULL, *valstr = NULL;
137 extstr = strdup(ext);
138 if (extstr == NULL) {
139 err = got_error_from_errno("strdup");
140 goto done;
142 valstr = strdup(val);
143 if (valstr == NULL) {
144 err = got_error_from_errno("strdup");
145 goto done;
147 (*extnames)[(*nextensions)] = extstr;
148 (*extvals)[(*nextensions)] = valstr;
149 (*nextensions)++;
154 author = got_gitconfig_get_str(gitconfig, "user", "name");
155 if (author) {
156 *gitconfig_author_name = strdup(author);
157 if (*gitconfig_author_name == NULL) {
158 err = got_error_from_errno("strdup");
159 goto done;
163 email = got_gitconfig_get_str(gitconfig, "user", "email");
164 if (email) {
165 *gitconfig_author_email = strdup(email);
166 if (*gitconfig_author_email == NULL) {
167 err = got_error_from_errno("strdup");
168 goto done;
172 if (gitconfig_owner) {
173 owner = got_gitconfig_get_str(gitconfig, "gotweb", "owner");
174 if (owner == NULL)
175 owner = got_gitconfig_get_str(gitconfig, "gitweb",
176 "owner");
177 if (owner) {
178 *gitconfig_owner = strdup(owner);
179 if (*gitconfig_owner == NULL) {
180 err = got_error_from_errno("strdup");
181 goto done;
187 if (remotes && nremotes) {
188 struct got_gitconfig_list *sections;
189 size_t nalloc = 0;
190 err = got_gitconfig_get_section_list(&sections, gitconfig);
191 if (err)
192 return err;
193 TAILQ_FOREACH(node, &sections->fields, link) {
194 if (skip_node(gitconfig, node))
195 continue;
196 nalloc++;
199 *remotes = recallocarray(NULL, 0, nalloc, sizeof(**remotes));
200 if (*remotes == NULL) {
201 err = got_error_from_errno("recallocarray");
202 goto done;
205 i = 0;
206 TAILQ_FOREACH(node, &sections->fields, link) {
207 struct got_remote_repo *remote;
208 char *name, *end, *mirror;
209 const char *fetch_url, *send_url;
211 if (skip_node(gitconfig, node) != 0)
212 continue;
214 remote = &(*remotes)[i];
216 name = strdup(node->field + 8);
217 if (name == NULL) {
218 err = got_error_from_errno("strdup");
219 goto done;
221 end = strrchr(name, '"');
222 if (end)
223 *end = '\0';
224 remote->name = name;
226 fetch_url = got_gitconfig_get_str(gitconfig,
227 node->field, "url");
228 remote->fetch_url = strdup(fetch_url);
229 if (remote->fetch_url == NULL) {
230 err = got_error_from_errno("strdup");
231 free(remote->name);
232 remote->name = NULL;
233 goto done;
236 send_url = got_gitconfig_get_str(gitconfig,
237 node->field, "pushurl");
238 if (send_url == NULL)
239 send_url = got_gitconfig_get_str(gitconfig,
240 node->field, "url");
241 remote->send_url = strdup(send_url);
242 if (remote->send_url == NULL) {
243 err = got_error_from_errno("strdup");
244 free(remote->name);
245 remote->name = NULL;
246 free(remote->fetch_url);
247 remote->fetch_url = NULL;
248 goto done;
251 remote->mirror_references = 0;
252 mirror = got_gitconfig_get_str(gitconfig, node->field,
253 "mirror");
254 if (mirror != NULL && get_boolean_val(mirror))
255 remote->mirror_references = 1;
257 i++;
258 (*nremotes)++;
261 done:
262 if (fd != -1)
263 close(fd);
264 if (gitconfig)
265 got_gitconfig_close(gitconfig);
266 if (err) {
267 if (extnames && extvals && nextensions) {
268 for (i = 0; i < (*nextensions); i++) {
269 free((*extnames)[i]);
270 free((*extvals)[i]);
272 free(*extnames);
273 *extnames = NULL;
274 free(*extvals);
275 *extvals = NULL;
276 *nextensions = 0;
278 if (remotes && nremotes) {
279 for (i = 0; i < (*nremotes); i++) {
280 struct got_remote_repo *remote;
281 remote = &(*remotes)[i];
282 free(remote->name);
283 free(remote->fetch_url);
284 free(remote->send_url);
286 free(*remotes);
287 *remotes = NULL;
288 *nremotes = 0;
291 return err;