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 "got_compat.h"
19 #include <sys/queue.h>
21 #include <errno.h>
22 #include <event.h>
23 #include <fcntl.h>
24 #include <imsg.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 static int
54 skip_node(struct got_gitconfig *gitconfig,
55 struct got_gitconfig_list_node *node)
56 {
57 /*
58 * Skip config nodes which do not describe remotes, and remotes
59 * which do not have a fetch URL defined (as used by git-annex).
60 */
61 return (strncasecmp("remote \"", node->field, 8) != 0 ||
62 got_gitconfig_get_str(gitconfig, node->field, "url") == NULL);
63 }
65 const struct got_error *
66 got_repo_read_gitconfig(int *gitconfig_repository_format_version,
67 char **gitconfig_author_name, char **gitconfig_author_email,
68 struct got_remote_repo **remotes, int *nremotes,
69 char **gitconfig_owner, char ***extnames, char ***extvals,
70 int *nextensions, const char *gitconfig_path)
71 {
72 const struct got_error *err = NULL;
73 struct got_gitconfig *gitconfig = NULL;
74 struct got_gitconfig_list *tags;
75 struct got_gitconfig_list_node *node;
76 int fd, i;
77 const char *author, *email, *owner;
79 *gitconfig_repository_format_version = 0;
80 if (extnames)
81 *extnames = NULL;
82 if (extvals)
83 *extvals = NULL;
84 if (nextensions)
85 *nextensions = 0;
86 *gitconfig_author_name = NULL;
87 *gitconfig_author_email = NULL;
88 if (remotes)
89 *remotes = NULL;
90 if (nremotes)
91 *nremotes = 0;
92 if (gitconfig_owner)
93 *gitconfig_owner = NULL;
95 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
96 if (fd == -1) {
97 if (errno == ENOENT)
98 return NULL;
99 return got_error_from_errno2("open", gitconfig_path);
102 err = got_gitconfig_open(&gitconfig, fd);
103 if (err)
104 goto done;
106 *gitconfig_repository_format_version = got_gitconfig_get_num(gitconfig,
107 "core", "repositoryformatversion", 0);
109 tags = got_gitconfig_get_tag_list(gitconfig, "extensions");
110 if (extnames && extvals && nextensions && tags) {
111 size_t numext = 0;
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 numext++;
119 *extnames = calloc(numext, sizeof(char *));
120 if (*extnames == NULL) {
121 err = got_error_from_errno("calloc");
122 goto done;
124 *extvals = calloc(numext, sizeof(char *));
125 if (*extvals == NULL) {
126 err = got_error_from_errno("calloc");
127 goto done;
129 TAILQ_FOREACH(node, &tags->fields, link) {
130 char *ext = node->field;
131 char *val = got_gitconfig_get_str(gitconfig,
132 "extensions", ext);
133 if (get_boolean_val(val)) {
134 char *extstr = NULL, *valstr = NULL;
136 extstr = strdup(ext);
137 if (extstr == NULL) {
138 err = got_error_from_errno("strdup");
139 goto done;
141 valstr = strdup(val);
142 if (valstr == NULL) {
143 err = got_error_from_errno("strdup");
144 goto done;
146 (*extnames)[(*nextensions)] = extstr;
147 (*extvals)[(*nextensions)] = valstr;
148 (*nextensions)++;
153 author = got_gitconfig_get_str(gitconfig, "user", "name");
154 if (author) {
155 *gitconfig_author_name = strdup(author);
156 if (*gitconfig_author_name == NULL) {
157 err = got_error_from_errno("strdup");
158 goto done;
162 email = got_gitconfig_get_str(gitconfig, "user", "email");
163 if (email) {
164 *gitconfig_author_email = strdup(email);
165 if (*gitconfig_author_email == NULL) {
166 err = got_error_from_errno("strdup");
167 goto done;
171 if (gitconfig_owner) {
172 owner = got_gitconfig_get_str(gitconfig, "gotweb", "owner");
173 if (owner == NULL)
174 owner = got_gitconfig_get_str(gitconfig, "gitweb",
175 "owner");
176 if (owner) {
177 *gitconfig_owner = strdup(owner);
178 if (*gitconfig_owner == NULL) {
179 err = got_error_from_errno("strdup");
180 goto done;
186 if (remotes && nremotes) {
187 struct got_gitconfig_list *sections;
188 size_t nalloc = 0;
189 err = got_gitconfig_get_section_list(&sections, gitconfig);
190 if (err)
191 return err;
192 TAILQ_FOREACH(node, &sections->fields, link) {
193 if (skip_node(gitconfig, node))
194 continue;
195 nalloc++;
198 *remotes = recallocarray(NULL, 0, nalloc, sizeof(**remotes));
199 if (*remotes == NULL) {
200 err = got_error_from_errno("recallocarray");
201 goto done;
204 i = 0;
205 TAILQ_FOREACH(node, &sections->fields, link) {
206 struct got_remote_repo *remote;
207 char *name, *end, *mirror;
208 const char *fetch_url, *send_url;
210 if (skip_node(gitconfig, node) != 0)
211 continue;
213 remote = &(*remotes)[i];
215 name = strdup(node->field + 8);
216 if (name == NULL) {
217 err = got_error_from_errno("strdup");
218 goto done;
220 end = strrchr(name, '"');
221 if (end)
222 *end = '\0';
223 remote->name = name;
225 fetch_url = got_gitconfig_get_str(gitconfig,
226 node->field, "url");
227 remote->fetch_url = strdup(fetch_url);
228 if (remote->fetch_url == NULL) {
229 err = got_error_from_errno("strdup");
230 free(remote->name);
231 remote->name = NULL;
232 goto done;
235 send_url = got_gitconfig_get_str(gitconfig,
236 node->field, "pushurl");
237 if (send_url == NULL)
238 send_url = got_gitconfig_get_str(gitconfig,
239 node->field, "url");
240 remote->send_url = strdup(send_url);
241 if (remote->send_url == NULL) {
242 err = got_error_from_errno("strdup");
243 free(remote->name);
244 remote->name = NULL;
245 free(remote->fetch_url);
246 remote->fetch_url = NULL;
247 goto done;
250 remote->mirror_references = 0;
251 mirror = got_gitconfig_get_str(gitconfig, node->field,
252 "mirror");
253 if (mirror != NULL && get_boolean_val(mirror))
254 remote->mirror_references = 1;
256 i++;
257 (*nremotes)++;
260 done:
261 if (fd != -1)
262 close(fd);
263 if (gitconfig)
264 got_gitconfig_close(gitconfig);
265 if (err) {
266 if (extnames && extvals && nextensions) {
267 for (i = 0; i < (*nextensions); i++) {
268 free((*extnames)[i]);
269 free((*extvals)[i]);
271 free(*extnames);
272 *extnames = NULL;
273 free(*extvals);
274 *extvals = NULL;
275 *nextensions = 0;
277 if (remotes && nremotes) {
278 for (i = 0; i < (*nremotes); i++) {
279 struct got_remote_repo *remote;
280 remote = &(*remotes)[i];
281 free(remote->name);
282 free(remote->fetch_url);
283 free(remote->send_url);
285 free(*remotes);
286 *remotes = NULL;
287 *nremotes = 0;
290 return err;