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 ***extnames, char ***extvals,
59 int *nextensions, const char *gitconfig_path)
61 const struct got_error *err = NULL;
62 struct got_gitconfig *gitconfig = NULL;
63 struct got_gitconfig_list *tags;
64 struct got_gitconfig_list_node *node;
66 const char *author, *email, *owner;
68 *gitconfig_repository_format_version = 0;
75 *gitconfig_author_name = NULL;
76 *gitconfig_author_email = NULL;
82 *gitconfig_owner = NULL;
84 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
88 return got_error_from_errno2("open", gitconfig_path);
91 err = got_gitconfig_open(&gitconfig, fd);
95 *gitconfig_repository_format_version = got_gitconfig_get_num(gitconfig,
96 "core", "repositoryformatversion", 0);
98 tags = got_gitconfig_get_tag_list(gitconfig, "extensions");
99 if (extnames && extvals && nextensions && tags) {
101 TAILQ_FOREACH(node, &tags->fields, link) {
102 char *ext = node->field;
103 char *val = got_gitconfig_get_str(gitconfig,
105 if (get_boolean_val(val))
108 *extnames = calloc(numext, sizeof(char *));
109 if (*extnames == NULL) {
110 err = got_error_from_errno("calloc");
113 *extvals = calloc(numext, sizeof(char *));
114 if (*extvals == NULL) {
115 err = got_error_from_errno("calloc");
118 TAILQ_FOREACH(node, &tags->fields, link) {
119 char *ext = node->field;
120 char *val = got_gitconfig_get_str(gitconfig,
122 if (get_boolean_val(val)) {
123 char *extstr = NULL, *valstr = NULL;
125 extstr = strdup(ext);
126 if (extstr == NULL) {
127 err = got_error_from_errno("strdup");
130 valstr = strdup(val);
131 if (valstr == NULL) {
132 err = got_error_from_errno("strdup");
135 (*extnames)[(*nextensions)] = extstr;
136 (*extvals)[(*nextensions)] = valstr;
142 author = got_gitconfig_get_str(gitconfig, "user", "name");
144 *gitconfig_author_name = strdup(author);
145 if (*gitconfig_author_name == NULL) {
146 err = got_error_from_errno("strdup");
151 email = got_gitconfig_get_str(gitconfig, "user", "email");
153 *gitconfig_author_email = strdup(email);
154 if (*gitconfig_author_email == NULL) {
155 err = got_error_from_errno("strdup");
160 if (gitconfig_owner) {
161 owner = got_gitconfig_get_str(gitconfig, "gotweb", "owner");
163 owner = got_gitconfig_get_str(gitconfig, "gitweb",
166 *gitconfig_owner = strdup(owner);
167 if (*gitconfig_owner == NULL) {
168 err = got_error_from_errno("strdup");
175 if (remotes && nremotes) {
176 struct got_gitconfig_list *sections;
178 err = got_gitconfig_get_section_list(§ions, gitconfig);
181 TAILQ_FOREACH(node, §ions->fields, link) {
182 if (strncasecmp("remote \"", node->field, 8) != 0)
187 *remotes = recallocarray(NULL, 0, nalloc, sizeof(**remotes));
188 if (*remotes == NULL) {
189 err = got_error_from_errno("recallocarray");
194 TAILQ_FOREACH(node, §ions->fields, link) {
195 struct got_remote_repo *remote;
196 char *name, *end, *mirror;
197 const char *fetch_url, *send_url;
199 if (strncasecmp("remote \"", node->field, 8) != 0)
202 remote = &(*remotes)[i];
204 name = strdup(node->field + 8);
206 err = got_error_from_errno("strdup");
209 end = strrchr(name, '"');
214 fetch_url = got_gitconfig_get_str(gitconfig,
216 if (fetch_url == NULL) {
217 err = got_error(GOT_ERR_GITCONFIG_SYNTAX);
222 remote->fetch_url = strdup(fetch_url);
223 if (remote->fetch_url == NULL) {
224 err = got_error_from_errno("strdup");
230 send_url = got_gitconfig_get_str(gitconfig,
231 node->field, "pushurl");
232 if (send_url == NULL)
233 send_url = got_gitconfig_get_str(gitconfig,
235 if (send_url == NULL) {
236 err = got_error(GOT_ERR_GITCONFIG_SYNTAX);
239 free(remote->fetch_url);
240 remote->fetch_url = NULL;
243 remote->send_url = strdup(send_url);
244 if (remote->send_url == NULL) {
245 err = got_error_from_errno("strdup");
248 free(remote->fetch_url);
249 remote->fetch_url = NULL;
253 remote->mirror_references = 0;
254 mirror = got_gitconfig_get_str(gitconfig, node->field,
256 if (mirror != NULL && get_boolean_val(mirror))
257 remote->mirror_references = 1;
267 got_gitconfig_close(gitconfig);
269 if (extnames && extvals && nextensions) {
270 for (i = 0; i < (*nextensions); i++) {
271 free((*extnames)[i]);
280 if (remotes && nremotes) {
281 for (i = 0; i < (*nremotes); i++) {
282 struct got_remote_repo *remote;
283 remote = &(*remotes)[i];
285 free(remote->fetch_url);
286 free(remote->send_url);