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 "got_compat.h"
19 #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);
54 skip_node(struct got_gitconfig *gitconfig,
55 struct got_gitconfig_list_node *node)
58 * Skip config nodes which do not describe remotes, and remotes
59 * which do not have a fetch URL defined (as used by git-annex).
61 return (strncasecmp("remote \"", node->field, 8) != 0 ||
62 got_gitconfig_get_str(gitconfig, node->field, "url") == NULL);
65 const struct got_error *
66 got_repo_read_gitconfig(int *gitconfig_repository_format_version,
67 char **gitconfig_author_name, char **gitconfig_author_email,
68 struct got_remote_repo **remotes, int *nremotes,
69 char **gitconfig_owner, char ***extnames, char ***extvals,
70 int *nextensions, const char *gitconfig_path)
72 const struct got_error *err = NULL;
73 struct got_gitconfig *gitconfig = NULL;
74 struct got_gitconfig_list *tags;
75 struct got_gitconfig_list_node *node;
77 const char *author, *email, *owner;
79 *gitconfig_repository_format_version = 0;
86 *gitconfig_author_name = NULL;
87 *gitconfig_author_email = NULL;
93 *gitconfig_owner = NULL;
95 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
99 return got_error_from_errno2("open", gitconfig_path);
102 err = got_gitconfig_open(&gitconfig, fd);
106 *gitconfig_repository_format_version = got_gitconfig_get_num(gitconfig,
107 "core", "repositoryformatversion", 0);
109 tags = got_gitconfig_get_tag_list(gitconfig, "extensions");
110 if (extnames && extvals && nextensions && tags) {
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))
119 *extnames = calloc(numext, sizeof(char *));
120 if (*extnames == NULL) {
121 err = got_error_from_errno("calloc");
124 *extvals = calloc(numext, sizeof(char *));
125 if (*extvals == NULL) {
126 err = got_error_from_errno("calloc");
129 TAILQ_FOREACH(node, &tags->fields, link) {
130 char *ext = node->field;
131 char *val = got_gitconfig_get_str(gitconfig,
133 if (get_boolean_val(val)) {
134 char *extstr = NULL, *valstr = NULL;
136 extstr = strdup(ext);
137 if (extstr == NULL) {
138 err = got_error_from_errno("strdup");
141 valstr = strdup(val);
142 if (valstr == NULL) {
143 err = got_error_from_errno("strdup");
146 (*extnames)[(*nextensions)] = extstr;
147 (*extvals)[(*nextensions)] = valstr;
153 author = got_gitconfig_get_str(gitconfig, "user", "name");
155 *gitconfig_author_name = strdup(author);
156 if (*gitconfig_author_name == NULL) {
157 err = got_error_from_errno("strdup");
162 email = got_gitconfig_get_str(gitconfig, "user", "email");
164 *gitconfig_author_email = strdup(email);
165 if (*gitconfig_author_email == NULL) {
166 err = got_error_from_errno("strdup");
171 if (gitconfig_owner) {
172 owner = got_gitconfig_get_str(gitconfig, "gotweb", "owner");
174 owner = got_gitconfig_get_str(gitconfig, "gitweb",
177 *gitconfig_owner = strdup(owner);
178 if (*gitconfig_owner == NULL) {
179 err = got_error_from_errno("strdup");
186 if (remotes && nremotes) {
187 struct got_gitconfig_list *sections;
189 err = got_gitconfig_get_section_list(§ions, gitconfig);
192 TAILQ_FOREACH(node, §ions->fields, link) {
193 if (skip_node(gitconfig, node))
198 *remotes = recallocarray(NULL, 0, nalloc, sizeof(**remotes));
199 if (*remotes == NULL) {
200 err = got_error_from_errno("recallocarray");
205 TAILQ_FOREACH(node, §ions->fields, link) {
206 struct got_remote_repo *remote;
207 char *name, *end, *mirror;
208 const char *fetch_url, *send_url;
210 if (skip_node(gitconfig, node) != 0)
213 remote = &(*remotes)[i];
215 name = strdup(node->field + 8);
217 err = got_error_from_errno("strdup");
220 end = strrchr(name, '"');
225 fetch_url = got_gitconfig_get_str(gitconfig,
227 remote->fetch_url = strdup(fetch_url);
228 if (remote->fetch_url == NULL) {
229 err = got_error_from_errno("strdup");
235 send_url = got_gitconfig_get_str(gitconfig,
236 node->field, "pushurl");
237 if (send_url == NULL)
238 send_url = got_gitconfig_get_str(gitconfig,
240 remote->send_url = strdup(send_url);
241 if (remote->send_url == NULL) {
242 err = got_error_from_errno("strdup");
245 free(remote->fetch_url);
246 remote->fetch_url = NULL;
250 remote->mirror_references = 0;
251 mirror = got_gitconfig_get_str(gitconfig, node->field,
253 if (mirror != NULL && get_boolean_val(mirror))
254 remote->mirror_references = 1;
264 got_gitconfig_close(gitconfig);
266 if (extnames && extvals && nextensions) {
267 for (i = 0; i < (*nextensions); i++) {
268 free((*extnames)[i]);
277 if (remotes && nremotes) {
278 for (i = 0; i < (*nremotes); i++) {
279 struct got_remote_repo *remote;
280 remote = &(*remotes)[i];
282 free(remote->fetch_url);
283 free(remote->send_url);