2 * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 #include <sys/queue.h>
30 #include "got_error.h"
31 #include "got_object.h"
32 #include "got_repository.h"
35 #include "got_lib_gitconfig.h"
36 #include "got_lib_delta.h"
37 #include "got_lib_object.h"
38 #include "got_lib_object_cache.h"
39 #include "got_lib_privsep.h"
40 #include "got_lib_pack.h"
41 #include "got_lib_repository.h"
44 get_boolean_val(char *val)
46 return (strcasecmp(val, "true") == 0 ||
47 strcasecmp(val, "on") == 0 ||
48 strcasecmp(val, "yes") == 0 ||
49 strcmp(val, "1") == 0);
52 const struct got_error *
53 got_repo_read_gitconfig(int *gitconfig_repository_format_version,
54 char **gitconfig_author_name, char **gitconfig_author_email,
55 struct got_remote_repo **remotes, int *nremotes,
56 char **gitconfig_owner, char ***extnames, char ***extvals,
57 int *nextensions, const char *gitconfig_path)
59 const struct got_error *err = NULL;
60 struct got_gitconfig *gitconfig = NULL;
61 struct got_gitconfig_list *tags;
62 struct got_gitconfig_list_node *node;
64 const char *author, *email, *owner;
66 *gitconfig_repository_format_version = 0;
73 *gitconfig_author_name = NULL;
74 *gitconfig_author_email = NULL;
80 *gitconfig_owner = NULL;
82 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
86 return got_error_from_errno2("open", gitconfig_path);
89 err = got_gitconfig_open(&gitconfig, fd);
93 *gitconfig_repository_format_version = got_gitconfig_get_num(gitconfig,
94 "core", "repositoryformatversion", 0);
96 tags = got_gitconfig_get_tag_list(gitconfig, "extensions");
97 if (extnames && extvals && nextensions && tags) {
99 TAILQ_FOREACH(node, &tags->fields, link) {
100 char *ext = node->field;
101 char *val = got_gitconfig_get_str(gitconfig,
103 if (get_boolean_val(val))
106 *extnames = calloc(numext, sizeof(char *));
107 if (*extnames == NULL) {
108 err = got_error_from_errno("calloc");
111 *extvals = calloc(numext, sizeof(char *));
112 if (*extvals == NULL) {
113 err = got_error_from_errno("calloc");
116 TAILQ_FOREACH(node, &tags->fields, link) {
117 char *ext = node->field;
118 char *val = got_gitconfig_get_str(gitconfig,
120 if (get_boolean_val(val)) {
121 char *extstr = NULL, *valstr = NULL;
123 extstr = strdup(ext);
124 if (extstr == NULL) {
125 err = got_error_from_errno("strdup");
128 valstr = strdup(val);
129 if (valstr == NULL) {
130 err = got_error_from_errno("strdup");
133 (*extnames)[(*nextensions)] = extstr;
134 (*extvals)[(*nextensions)] = valstr;
140 author = got_gitconfig_get_str(gitconfig, "user", "name");
142 *gitconfig_author_name = strdup(author);
143 if (*gitconfig_author_name == NULL) {
144 err = got_error_from_errno("strdup");
149 email = got_gitconfig_get_str(gitconfig, "user", "email");
151 *gitconfig_author_email = strdup(email);
152 if (*gitconfig_author_email == NULL) {
153 err = got_error_from_errno("strdup");
158 if (gitconfig_owner) {
159 owner = got_gitconfig_get_str(gitconfig, "gotweb", "owner");
161 owner = got_gitconfig_get_str(gitconfig, "gitweb",
164 *gitconfig_owner = strdup(owner);
165 if (*gitconfig_owner == NULL) {
166 err = got_error_from_errno("strdup");
173 if (remotes && nremotes) {
174 struct got_gitconfig_list *sections;
176 err = got_gitconfig_get_section_list(§ions, gitconfig);
179 TAILQ_FOREACH(node, §ions->fields, link) {
180 if (strncasecmp("remote \"", node->field, 8) != 0)
185 *remotes = recallocarray(NULL, 0, nalloc, sizeof(**remotes));
186 if (*remotes == NULL) {
187 err = got_error_from_errno("recallocarray");
192 TAILQ_FOREACH(node, §ions->fields, link) {
193 struct got_remote_repo *remote;
194 char *name, *end, *mirror;
195 const char *fetch_url, *send_url;
197 if (strncasecmp("remote \"", node->field, 8) != 0)
200 remote = &(*remotes)[i];
202 name = strdup(node->field + 8);
204 err = got_error_from_errno("strdup");
207 end = strrchr(name, '"');
212 fetch_url = got_gitconfig_get_str(gitconfig,
214 if (fetch_url == NULL) {
215 err = got_error(GOT_ERR_GITCONFIG_SYNTAX);
220 remote->fetch_url = strdup(fetch_url);
221 if (remote->fetch_url == NULL) {
222 err = got_error_from_errno("strdup");
228 send_url = got_gitconfig_get_str(gitconfig,
229 node->field, "pushurl");
230 if (send_url == NULL)
231 send_url = got_gitconfig_get_str(gitconfig,
233 if (send_url == NULL) {
234 err = got_error(GOT_ERR_GITCONFIG_SYNTAX);
237 free(remote->fetch_url);
238 remote->fetch_url = NULL;
241 remote->send_url = strdup(send_url);
242 if (remote->send_url == NULL) {
243 err = got_error_from_errno("strdup");
246 free(remote->fetch_url);
247 remote->fetch_url = NULL;
251 remote->mirror_references = 0;
252 mirror = got_gitconfig_get_str(gitconfig, node->field,
254 if (mirror != NULL && get_boolean_val(mirror))
255 remote->mirror_references = 1;
265 got_gitconfig_close(gitconfig);
267 if (extnames && extvals && nextensions) {
268 for (i = 0; i < (*nextensions); i++) {
269 free((*extnames)[i]);
278 if (remotes && nremotes) {
279 for (i = 0; i < (*nremotes); i++) {
280 struct got_remote_repo *remote;
281 remote = &(*remotes)[i];
283 free(remote->fetch_url);
284 free(remote->send_url);