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>
29 #include "got_error.h"
30 #include "got_object.h"
31 #include "got_repository.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"
43 get_boolean_val(char *val)
45 return (strcasecmp(val, "true") == 0 ||
46 strcasecmp(val, "on") == 0 ||
47 strcasecmp(val, "yes") == 0 ||
48 strcmp(val, "1") == 0);
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)
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;
63 const char *author, *email, *owner;
65 *gitconfig_repository_format_version = 0;
70 *gitconfig_author_name = NULL;
71 *gitconfig_author_email = NULL;
77 *gitconfig_owner = NULL;
79 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
83 return got_error_from_errno2("open", gitconfig_path);
86 err = got_gitconfig_open(&gitconfig, fd);
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) {
96 TAILQ_FOREACH(node, &tags->fields, link) {
97 char *ext = node->field;
98 char *val = got_gitconfig_get_str(gitconfig,
100 if (get_boolean_val(val))
103 *extensions = calloc(numext, sizeof(char *));
104 if (*extensions == NULL) {
105 err = got_error_from_errno("calloc");
108 TAILQ_FOREACH(node, &tags->fields, link) {
109 char *ext = node->field;
110 char *val = got_gitconfig_get_str(gitconfig,
112 if (get_boolean_val(val)) {
113 char *extstr = strdup(ext);
114 if (extstr == NULL) {
115 err = got_error_from_errno("strdup");
118 (*extensions)[(*nextensions)] = extstr;
124 author = got_gitconfig_get_str(gitconfig, "user", "name");
126 *gitconfig_author_name = strdup(author);
127 if (*gitconfig_author_name == NULL) {
128 err = got_error_from_errno("strdup");
133 email = got_gitconfig_get_str(gitconfig, "user", "email");
135 *gitconfig_author_email = strdup(email);
136 if (*gitconfig_author_email == NULL) {
137 err = got_error_from_errno("strdup");
142 if (gitconfig_owner) {
143 owner = got_gitconfig_get_str(gitconfig, "gotweb", "owner");
145 owner = got_gitconfig_get_str(gitconfig, "gitweb",
148 *gitconfig_owner = strdup(owner);
149 if (*gitconfig_owner == NULL) {
150 err = got_error_from_errno("strdup");
157 if (remotes && nremotes) {
158 struct got_gitconfig_list *sections;
160 err = got_gitconfig_get_section_list(§ions, gitconfig);
163 TAILQ_FOREACH(node, §ions->fields, link) {
164 if (strncasecmp("remote \"", node->field, 8) != 0)
169 *remotes = recallocarray(NULL, 0, nalloc, sizeof(**remotes));
170 if (*remotes == NULL) {
171 err = got_error_from_errno("recallocarray");
176 TAILQ_FOREACH(node, §ions->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)
184 remote = &(*remotes)[i];
186 name = strdup(node->field + 8);
188 err = got_error_from_errno("strdup");
191 end = strrchr(name, '"');
196 fetch_url = got_gitconfig_get_str(gitconfig,
198 if (fetch_url == NULL) {
199 err = got_error(GOT_ERR_GITCONFIG_SYNTAX);
204 remote->fetch_url = strdup(fetch_url);
205 if (remote->fetch_url == NULL) {
206 err = got_error_from_errno("strdup");
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,
217 if (send_url == NULL) {
218 err = got_error(GOT_ERR_GITCONFIG_SYNTAX);
221 free(remote->fetch_url);
222 remote->fetch_url = NULL;
225 remote->send_url = strdup(send_url);
226 if (remote->send_url == NULL) {
227 err = got_error_from_errno("strdup");
230 free(remote->fetch_url);
231 remote->fetch_url = NULL;
235 remote->mirror_references = 0;
236 mirror = got_gitconfig_get_str(gitconfig, node->field,
238 if (mirror != NULL && get_boolean_val(mirror))
239 remote->mirror_references = 1;
249 got_gitconfig_close(gitconfig);
251 if (extensions && nextensions) {
252 for (i = 0; i < (*nextensions); i++)
253 free((*extensions)[i]);
258 if (remotes && nremotes) {
259 for (i = 0; i < (*nremotes); i++) {
260 struct got_remote_repo *remote;
261 remote = &(*remotes)[i];
263 free(remote->fetch_url);
264 free(remote->send_url);