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>
31 #include "got_error.h"
32 #include "got_object.h"
33 #include "got_repository.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"
45 get_boolean_val(char *val)
47 return (strcasecmp(val, "true") == 0 ||
48 strcasecmp(val, "on") == 0 ||
49 strcasecmp(val, "yes") == 0 ||
50 strcmp(val, "1") == 0);
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)
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;
65 const char *author, *email, *owner;
67 *gitconfig_repository_format_version = 0;
72 *gitconfig_author_name = NULL;
73 *gitconfig_author_email = NULL;
79 *gitconfig_owner = NULL;
81 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
85 return got_error_from_errno2("open", gitconfig_path);
88 err = got_gitconfig_open(&gitconfig, fd);
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) {
98 TAILQ_FOREACH(node, &tags->fields, link) {
99 char *ext = node->field;
100 char *val = got_gitconfig_get_str(gitconfig,
102 if (get_boolean_val(val))
105 *extensions = calloc(numext, sizeof(char *));
106 if (*extensions == NULL) {
107 err = got_error_from_errno("calloc");
110 TAILQ_FOREACH(node, &tags->fields, link) {
111 char *ext = node->field;
112 char *val = got_gitconfig_get_str(gitconfig,
114 if (get_boolean_val(val)) {
115 char *extstr = strdup(ext);
116 if (extstr == NULL) {
117 err = got_error_from_errno("strdup");
120 (*extensions)[(*nextensions)] = extstr;
126 author = got_gitconfig_get_str(gitconfig, "user", "name");
128 *gitconfig_author_name = strdup(author);
129 if (*gitconfig_author_name == NULL) {
130 err = got_error_from_errno("strdup");
135 email = got_gitconfig_get_str(gitconfig, "user", "email");
137 *gitconfig_author_email = strdup(email);
138 if (*gitconfig_author_email == NULL) {
139 err = got_error_from_errno("strdup");
144 if (gitconfig_owner) {
145 owner = got_gitconfig_get_str(gitconfig, "gotweb", "owner");
147 owner = got_gitconfig_get_str(gitconfig, "gitweb",
150 *gitconfig_owner = strdup(owner);
151 if (*gitconfig_owner == NULL) {
152 err = got_error_from_errno("strdup");
159 if (remotes && nremotes) {
160 struct got_gitconfig_list *sections;
162 err = got_gitconfig_get_section_list(§ions, gitconfig);
165 TAILQ_FOREACH(node, §ions->fields, link) {
166 if (strncasecmp("remote \"", node->field, 8) != 0)
171 *remotes = recallocarray(NULL, 0, nalloc, sizeof(**remotes));
172 if (*remotes == NULL) {
173 err = got_error_from_errno("recallocarray");
178 TAILQ_FOREACH(node, §ions->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)
186 remote = &(*remotes)[i];
188 name = strdup(node->field + 8);
190 err = got_error_from_errno("strdup");
193 end = strrchr(name, '"');
198 fetch_url = got_gitconfig_get_str(gitconfig,
200 if (fetch_url == NULL) {
201 err = got_error(GOT_ERR_GITCONFIG_SYNTAX);
206 remote->fetch_url = strdup(fetch_url);
207 if (remote->fetch_url == NULL) {
208 err = got_error_from_errno("strdup");
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,
219 if (send_url == NULL) {
220 err = got_error(GOT_ERR_GITCONFIG_SYNTAX);
223 free(remote->fetch_url);
224 remote->fetch_url = NULL;
227 remote->send_url = strdup(send_url);
228 if (remote->send_url == NULL) {
229 err = got_error_from_errno("strdup");
232 free(remote->fetch_url);
233 remote->fetch_url = NULL;
237 remote->mirror_references = 0;
238 mirror = got_gitconfig_get_str(gitconfig, node->field,
240 if (mirror != NULL && get_boolean_val(mirror))
241 remote->mirror_references = 1;
251 got_gitconfig_close(gitconfig);
253 if (extensions && nextensions) {
254 for (i = 0; i < (*nextensions); i++)
255 free((*extensions)[i]);
260 if (remotes && nremotes) {
261 for (i = 0; i < (*nremotes); i++) {
262 struct got_remote_repo *remote;
263 remote = &(*remotes)[i];
265 free(remote->fetch_url);
266 free(remote->send_url);