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);
55 skip_node(struct got_gitconfig *gitconfig,
56 struct got_gitconfig_list_node *node)
59 * Skip config nodes which do not describe remotes, and remotes
60 * which do not have a fetch URL defined (as used by git-annex).
62 return (strncasecmp("remote \"", node->field, 8) != 0 ||
63 got_gitconfig_get_str(gitconfig, node->field, "url") == NULL);
66 const struct got_error *
67 got_repo_read_gitconfig(int *gitconfig_repository_format_version,
68 char **gitconfig_author_name, char **gitconfig_author_email,
69 struct got_remote_repo **remotes, int *nremotes,
70 char **gitconfig_owner, char ***extnames, char ***extvals,
71 int *nextensions, const char *gitconfig_path)
73 const struct got_error *err = NULL;
74 struct got_gitconfig *gitconfig = NULL;
75 struct got_gitconfig_list *tags;
76 struct got_gitconfig_list_node *node;
78 const char *author, *email, *owner;
80 *gitconfig_repository_format_version = 0;
87 *gitconfig_author_name = NULL;
88 *gitconfig_author_email = NULL;
94 *gitconfig_owner = NULL;
96 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
100 return got_error_from_errno2("open", gitconfig_path);
103 err = got_gitconfig_open(&gitconfig, fd);
107 *gitconfig_repository_format_version = got_gitconfig_get_num(gitconfig,
108 "core", "repositoryformatversion", 0);
110 tags = got_gitconfig_get_tag_list(gitconfig, "extensions");
111 if (extnames && extvals && nextensions && tags) {
113 TAILQ_FOREACH(node, &tags->fields, link) {
114 char *ext = node->field;
115 char *val = got_gitconfig_get_str(gitconfig,
117 if (get_boolean_val(val))
120 *extnames = calloc(numext, sizeof(char *));
121 if (*extnames == NULL) {
122 err = got_error_from_errno("calloc");
125 *extvals = calloc(numext, sizeof(char *));
126 if (*extvals == NULL) {
127 err = got_error_from_errno("calloc");
130 TAILQ_FOREACH(node, &tags->fields, link) {
131 char *ext = node->field;
132 char *val = got_gitconfig_get_str(gitconfig,
134 if (get_boolean_val(val)) {
135 char *extstr = NULL, *valstr = NULL;
137 extstr = strdup(ext);
138 if (extstr == NULL) {
139 err = got_error_from_errno("strdup");
142 valstr = strdup(val);
143 if (valstr == NULL) {
144 err = got_error_from_errno("strdup");
147 (*extnames)[(*nextensions)] = extstr;
148 (*extvals)[(*nextensions)] = valstr;
154 author = got_gitconfig_get_str(gitconfig, "user", "name");
156 *gitconfig_author_name = strdup(author);
157 if (*gitconfig_author_name == NULL) {
158 err = got_error_from_errno("strdup");
163 email = got_gitconfig_get_str(gitconfig, "user", "email");
165 *gitconfig_author_email = strdup(email);
166 if (*gitconfig_author_email == NULL) {
167 err = got_error_from_errno("strdup");
172 if (gitconfig_owner) {
173 owner = got_gitconfig_get_str(gitconfig, "gotweb", "owner");
175 owner = got_gitconfig_get_str(gitconfig, "gitweb",
178 *gitconfig_owner = strdup(owner);
179 if (*gitconfig_owner == NULL) {
180 err = got_error_from_errno("strdup");
187 if (remotes && nremotes) {
188 struct got_gitconfig_list *sections;
190 err = got_gitconfig_get_section_list(§ions, gitconfig);
193 TAILQ_FOREACH(node, §ions->fields, link) {
194 if (skip_node(gitconfig, node))
199 *remotes = recallocarray(NULL, 0, nalloc, sizeof(**remotes));
200 if (*remotes == NULL) {
201 err = got_error_from_errno("recallocarray");
206 TAILQ_FOREACH(node, §ions->fields, link) {
207 struct got_remote_repo *remote;
208 char *name, *end, *mirror;
209 const char *fetch_url, *send_url;
211 if (skip_node(gitconfig, node) != 0)
214 remote = &(*remotes)[i];
216 name = strdup(node->field + 8);
218 err = got_error_from_errno("strdup");
221 end = strrchr(name, '"');
226 fetch_url = got_gitconfig_get_str(gitconfig,
228 remote->fetch_url = strdup(fetch_url);
229 if (remote->fetch_url == NULL) {
230 err = got_error_from_errno("strdup");
236 send_url = got_gitconfig_get_str(gitconfig,
237 node->field, "pushurl");
238 if (send_url == NULL)
239 send_url = got_gitconfig_get_str(gitconfig,
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);