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>
32 #include "got_error.h"
33 #include "got_object.h"
34 #include "got_repository.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"
46 get_boolean_val(char *val)
48 return (strcasecmp(val, "true") == 0 ||
49 strcasecmp(val, "on") == 0 ||
50 strcasecmp(val, "yes") == 0 ||
51 strcmp(val, "1") == 0);
54 const struct got_error *
55 got_repo_read_gitconfig(int *gitconfig_repository_format_version,
56 char **gitconfig_author_name, char **gitconfig_author_email,
57 struct got_remote_repo **remotes, int *nremotes,
58 char **gitconfig_owner, char **objectformat,
59 char ***extensions, int *nextensions,
60 const char *gitconfig_path)
62 const struct got_error *err = NULL;
63 struct got_gitconfig *gitconfig = NULL;
64 struct got_gitconfig_list *tags;
65 struct got_gitconfig_list_node *node;
67 const char *author, *email, *owner;
69 *gitconfig_repository_format_version = 0;
74 *gitconfig_author_name = NULL;
75 *gitconfig_author_email = NULL;
81 *gitconfig_owner = NULL;
83 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
87 return got_error_from_errno2("open", gitconfig_path);
90 err = got_gitconfig_open(&gitconfig, fd);
94 *gitconfig_repository_format_version = got_gitconfig_get_num(gitconfig,
95 "core", "repositoryformatversion", 0);
97 tags = got_gitconfig_get_tag_list(gitconfig, "extensions");
98 if (extensions && nextensions && tags) {
100 TAILQ_FOREACH(node, &tags->fields, link) {
101 char *ext = node->field;
102 char *val = got_gitconfig_get_str(gitconfig,
104 if (get_boolean_val(val))
107 *extensions = calloc(numext, sizeof(char *));
108 if (*extensions == NULL) {
109 err = got_error_from_errno("calloc");
112 TAILQ_FOREACH(node, &tags->fields, link) {
113 char *ext = node->field;
114 char *val = got_gitconfig_get_str(gitconfig,
116 if (get_boolean_val(val)) {
117 char *extstr = strdup(ext);
118 if (extstr == NULL) {
119 err = got_error_from_errno("strdup");
122 (*extensions)[(*nextensions)] = extstr;
125 if (objectformat && !strcmp(ext, "sha256")) {
127 *objectformat = strdup(val);
128 if (*objectformat == NULL) {
129 err = got_error_from_errno("strdup");
136 author = got_gitconfig_get_str(gitconfig, "user", "name");
138 *gitconfig_author_name = strdup(author);
139 if (*gitconfig_author_name == NULL) {
140 err = got_error_from_errno("strdup");
145 email = got_gitconfig_get_str(gitconfig, "user", "email");
147 *gitconfig_author_email = strdup(email);
148 if (*gitconfig_author_email == NULL) {
149 err = got_error_from_errno("strdup");
154 if (gitconfig_owner) {
155 owner = got_gitconfig_get_str(gitconfig, "gotweb", "owner");
157 owner = got_gitconfig_get_str(gitconfig, "gitweb",
160 *gitconfig_owner = strdup(owner);
161 if (*gitconfig_owner == NULL) {
162 err = got_error_from_errno("strdup");
169 if (remotes && nremotes) {
170 struct got_gitconfig_list *sections;
172 err = got_gitconfig_get_section_list(§ions, gitconfig);
175 TAILQ_FOREACH(node, §ions->fields, link) {
176 if (strncasecmp("remote \"", node->field, 8) != 0)
181 *remotes = recallocarray(NULL, 0, nalloc, sizeof(**remotes));
182 if (*remotes == NULL) {
183 err = got_error_from_errno("recallocarray");
188 TAILQ_FOREACH(node, §ions->fields, link) {
189 struct got_remote_repo *remote;
190 char *name, *end, *mirror;
191 const char *fetch_url, *send_url;
193 if (strncasecmp("remote \"", node->field, 8) != 0)
196 remote = &(*remotes)[i];
198 name = strdup(node->field + 8);
200 err = got_error_from_errno("strdup");
203 end = strrchr(name, '"');
208 fetch_url = got_gitconfig_get_str(gitconfig,
210 if (fetch_url == NULL) {
211 err = got_error(GOT_ERR_GITCONFIG_SYNTAX);
216 remote->fetch_url = strdup(fetch_url);
217 if (remote->fetch_url == NULL) {
218 err = got_error_from_errno("strdup");
224 send_url = got_gitconfig_get_str(gitconfig,
225 node->field, "pushurl");
226 if (send_url == NULL)
227 send_url = got_gitconfig_get_str(gitconfig,
229 if (send_url == NULL) {
230 err = got_error(GOT_ERR_GITCONFIG_SYNTAX);
233 free(remote->fetch_url);
234 remote->fetch_url = NULL;
237 remote->send_url = strdup(send_url);
238 if (remote->send_url == NULL) {
239 err = got_error_from_errno("strdup");
242 free(remote->fetch_url);
243 remote->fetch_url = NULL;
247 remote->mirror_references = 0;
248 mirror = got_gitconfig_get_str(gitconfig, node->field,
250 if (mirror != NULL && get_boolean_val(mirror))
251 remote->mirror_references = 1;
261 got_gitconfig_close(gitconfig);
263 if (extensions && nextensions) {
264 for (i = 0; i < (*nextensions); i++)
265 free((*extensions)[i]);
270 if (remotes && nremotes) {
271 for (i = 0; i < (*nremotes); i++) {
272 struct got_remote_repo *remote;
273 remote = &(*remotes)[i];
275 free(remote->fetch_url);
276 free(remote->send_url);