2 13b2bc37 2022-10-23 stsp * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
4 13b2bc37 2022-10-23 stsp * Permission to use, copy, modify, and distribute this software for any
5 13b2bc37 2022-10-23 stsp * purpose with or without fee is hereby granted, provided that the above
6 13b2bc37 2022-10-23 stsp * copyright notice and this permission notice appear in all copies.
8 13b2bc37 2022-10-23 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 13b2bc37 2022-10-23 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 13b2bc37 2022-10-23 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 13b2bc37 2022-10-23 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 13b2bc37 2022-10-23 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 13b2bc37 2022-10-23 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 13b2bc37 2022-10-23 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 13b2bc37 2022-10-23 stsp #include <sys/queue.h>
18 13b2bc37 2022-10-23 stsp #include <sys/tree.h>
20 13b2bc37 2022-10-23 stsp #include <errno.h>
21 13b2bc37 2022-10-23 stsp #include <event.h>
22 13b2bc37 2022-10-23 stsp #include <fcntl.h>
23 13b2bc37 2022-10-23 stsp #include <imsg.h>
24 13b2bc37 2022-10-23 stsp #include <sha1.h>
25 69c6accf 2023-02-04 op #include <sha2.h>
26 13b2bc37 2022-10-23 stsp #include <stdio.h>
27 13b2bc37 2022-10-23 stsp #include <stdlib.h>
28 13b2bc37 2022-10-23 stsp #include <string.h>
29 13b2bc37 2022-10-23 stsp #include <limits.h>
30 13b2bc37 2022-10-23 stsp #include <unistd.h>
32 13b2bc37 2022-10-23 stsp #include "got_error.h"
33 13b2bc37 2022-10-23 stsp #include "got_object.h"
34 13b2bc37 2022-10-23 stsp #include "got_repository.h"
35 13b2bc37 2022-10-23 stsp #include "got_path.h"
37 13b2bc37 2022-10-23 stsp #include "got_lib_gitconfig.h"
38 13b2bc37 2022-10-23 stsp #include "got_lib_delta.h"
39 13b2bc37 2022-10-23 stsp #include "got_lib_object.h"
40 13b2bc37 2022-10-23 stsp #include "got_lib_object_cache.h"
41 13b2bc37 2022-10-23 stsp #include "got_lib_privsep.h"
42 13b2bc37 2022-10-23 stsp #include "got_lib_pack.h"
43 13b2bc37 2022-10-23 stsp #include "got_lib_repository.h"
46 13b2bc37 2022-10-23 stsp get_boolean_val(char *val)
48 13b2bc37 2022-10-23 stsp return (strcasecmp(val, "true") == 0 ||
49 13b2bc37 2022-10-23 stsp strcasecmp(val, "on") == 0 ||
50 13b2bc37 2022-10-23 stsp strcasecmp(val, "yes") == 0 ||
51 13b2bc37 2022-10-23 stsp strcmp(val, "1") == 0);
54 13b2bc37 2022-10-23 stsp const struct got_error *
55 13b2bc37 2022-10-23 stsp got_repo_read_gitconfig(int *gitconfig_repository_format_version,
56 13b2bc37 2022-10-23 stsp char **gitconfig_author_name, char **gitconfig_author_email,
57 13b2bc37 2022-10-23 stsp struct got_remote_repo **remotes, int *nremotes,
58 92db0871 2023-02-04 op char **gitconfig_owner, char **objectformat,
59 92db0871 2023-02-04 op char ***extensions, int *nextensions,
60 13b2bc37 2022-10-23 stsp const char *gitconfig_path)
62 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
63 13b2bc37 2022-10-23 stsp struct got_gitconfig *gitconfig = NULL;
64 13b2bc37 2022-10-23 stsp struct got_gitconfig_list *tags;
65 13b2bc37 2022-10-23 stsp struct got_gitconfig_list_node *node;
67 13b2bc37 2022-10-23 stsp const char *author, *email, *owner;
69 13b2bc37 2022-10-23 stsp *gitconfig_repository_format_version = 0;
70 13b2bc37 2022-10-23 stsp if (extensions)
71 13b2bc37 2022-10-23 stsp *extensions = NULL;
72 13b2bc37 2022-10-23 stsp if (nextensions)
73 13b2bc37 2022-10-23 stsp *nextensions = 0;
74 13b2bc37 2022-10-23 stsp *gitconfig_author_name = NULL;
75 13b2bc37 2022-10-23 stsp *gitconfig_author_email = NULL;
76 13b2bc37 2022-10-23 stsp if (remotes)
77 13b2bc37 2022-10-23 stsp *remotes = NULL;
78 13b2bc37 2022-10-23 stsp if (nremotes)
79 13b2bc37 2022-10-23 stsp *nremotes = 0;
80 13b2bc37 2022-10-23 stsp if (gitconfig_owner)
81 13b2bc37 2022-10-23 stsp *gitconfig_owner = NULL;
83 13b2bc37 2022-10-23 stsp fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
84 13b2bc37 2022-10-23 stsp if (fd == -1) {
85 13b2bc37 2022-10-23 stsp if (errno == ENOENT)
86 13b2bc37 2022-10-23 stsp return NULL;
87 13b2bc37 2022-10-23 stsp return got_error_from_errno2("open", gitconfig_path);
90 13b2bc37 2022-10-23 stsp err = got_gitconfig_open(&gitconfig, fd);
94 13b2bc37 2022-10-23 stsp *gitconfig_repository_format_version = got_gitconfig_get_num(gitconfig,
95 13b2bc37 2022-10-23 stsp "core", "repositoryformatversion", 0);
97 13b2bc37 2022-10-23 stsp tags = got_gitconfig_get_tag_list(gitconfig, "extensions");
98 13b2bc37 2022-10-23 stsp if (extensions && nextensions && tags) {
99 13b2bc37 2022-10-23 stsp size_t numext = 0;
100 13b2bc37 2022-10-23 stsp TAILQ_FOREACH(node, &tags->fields, link) {
101 13b2bc37 2022-10-23 stsp char *ext = node->field;
102 13b2bc37 2022-10-23 stsp char *val = got_gitconfig_get_str(gitconfig,
103 13b2bc37 2022-10-23 stsp "extensions", ext);
104 13b2bc37 2022-10-23 stsp if (get_boolean_val(val))
107 13b2bc37 2022-10-23 stsp *extensions = calloc(numext, sizeof(char *));
108 13b2bc37 2022-10-23 stsp if (*extensions == NULL) {
109 13b2bc37 2022-10-23 stsp err = got_error_from_errno("calloc");
112 13b2bc37 2022-10-23 stsp TAILQ_FOREACH(node, &tags->fields, link) {
113 13b2bc37 2022-10-23 stsp char *ext = node->field;
114 13b2bc37 2022-10-23 stsp char *val = got_gitconfig_get_str(gitconfig,
115 13b2bc37 2022-10-23 stsp "extensions", ext);
116 13b2bc37 2022-10-23 stsp if (get_boolean_val(val)) {
117 13b2bc37 2022-10-23 stsp char *extstr = strdup(ext);
118 13b2bc37 2022-10-23 stsp if (extstr == NULL) {
119 13b2bc37 2022-10-23 stsp err = got_error_from_errno("strdup");
122 13b2bc37 2022-10-23 stsp (*extensions)[(*nextensions)] = extstr;
123 13b2bc37 2022-10-23 stsp (*nextensions)++;
125 01f02e60 2023-02-04 op if (objectformat && !strcmp(ext, "sha256")) {
126 92db0871 2023-02-04 op free(*objectformat);
127 01f02e60 2023-02-04 op *objectformat = strdup(val);
128 92db0871 2023-02-04 op if (*objectformat == NULL) {
129 92db0871 2023-02-04 op err = got_error_from_errno("strdup");
136 13b2bc37 2022-10-23 stsp author = got_gitconfig_get_str(gitconfig, "user", "name");
137 13b2bc37 2022-10-23 stsp if (author) {
138 13b2bc37 2022-10-23 stsp *gitconfig_author_name = strdup(author);
139 13b2bc37 2022-10-23 stsp if (*gitconfig_author_name == NULL) {
140 13b2bc37 2022-10-23 stsp err = got_error_from_errno("strdup");
145 13b2bc37 2022-10-23 stsp email = got_gitconfig_get_str(gitconfig, "user", "email");
146 13b2bc37 2022-10-23 stsp if (email) {
147 13b2bc37 2022-10-23 stsp *gitconfig_author_email = strdup(email);
148 13b2bc37 2022-10-23 stsp if (*gitconfig_author_email == NULL) {
149 13b2bc37 2022-10-23 stsp err = got_error_from_errno("strdup");
154 13b2bc37 2022-10-23 stsp if (gitconfig_owner) {
155 13b2bc37 2022-10-23 stsp owner = got_gitconfig_get_str(gitconfig, "gotweb", "owner");
156 13b2bc37 2022-10-23 stsp if (owner == NULL)
157 13b2bc37 2022-10-23 stsp owner = got_gitconfig_get_str(gitconfig, "gitweb",
159 13b2bc37 2022-10-23 stsp if (owner) {
160 13b2bc37 2022-10-23 stsp *gitconfig_owner = strdup(owner);
161 13b2bc37 2022-10-23 stsp if (*gitconfig_owner == NULL) {
162 13b2bc37 2022-10-23 stsp err = got_error_from_errno("strdup");
169 13b2bc37 2022-10-23 stsp if (remotes && nremotes) {
170 13b2bc37 2022-10-23 stsp struct got_gitconfig_list *sections;
171 13b2bc37 2022-10-23 stsp size_t nalloc = 0;
172 13b2bc37 2022-10-23 stsp err = got_gitconfig_get_section_list(§ions, gitconfig);
174 13b2bc37 2022-10-23 stsp return err;
175 13b2bc37 2022-10-23 stsp TAILQ_FOREACH(node, §ions->fields, link) {
176 13b2bc37 2022-10-23 stsp if (strncasecmp("remote \"", node->field, 8) != 0)
181 13b2bc37 2022-10-23 stsp *remotes = recallocarray(NULL, 0, nalloc, sizeof(**remotes));
182 13b2bc37 2022-10-23 stsp if (*remotes == NULL) {
183 13b2bc37 2022-10-23 stsp err = got_error_from_errno("recallocarray");
188 13b2bc37 2022-10-23 stsp TAILQ_FOREACH(node, §ions->fields, link) {
189 13b2bc37 2022-10-23 stsp struct got_remote_repo *remote;
190 13b2bc37 2022-10-23 stsp char *name, *end, *mirror;
191 13b2bc37 2022-10-23 stsp const char *fetch_url, *send_url;
193 13b2bc37 2022-10-23 stsp if (strncasecmp("remote \"", node->field, 8) != 0)
196 13b2bc37 2022-10-23 stsp remote = &(*remotes)[i];
198 13b2bc37 2022-10-23 stsp name = strdup(node->field + 8);
199 13b2bc37 2022-10-23 stsp if (name == NULL) {
200 13b2bc37 2022-10-23 stsp err = got_error_from_errno("strdup");
203 13b2bc37 2022-10-23 stsp end = strrchr(name, '"');
205 13b2bc37 2022-10-23 stsp *end = '\0';
206 13b2bc37 2022-10-23 stsp remote->name = name;
208 13b2bc37 2022-10-23 stsp fetch_url = got_gitconfig_get_str(gitconfig,
209 13b2bc37 2022-10-23 stsp node->field, "url");
210 13b2bc37 2022-10-23 stsp if (fetch_url == NULL) {
211 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_GITCONFIG_SYNTAX);
212 13b2bc37 2022-10-23 stsp free(remote->name);
213 13b2bc37 2022-10-23 stsp remote->name = NULL;
216 13b2bc37 2022-10-23 stsp remote->fetch_url = strdup(fetch_url);
217 13b2bc37 2022-10-23 stsp if (remote->fetch_url == NULL) {
218 13b2bc37 2022-10-23 stsp err = got_error_from_errno("strdup");
219 13b2bc37 2022-10-23 stsp free(remote->name);
220 13b2bc37 2022-10-23 stsp remote->name = NULL;
224 13b2bc37 2022-10-23 stsp send_url = got_gitconfig_get_str(gitconfig,
225 13b2bc37 2022-10-23 stsp node->field, "pushurl");
226 13b2bc37 2022-10-23 stsp if (send_url == NULL)
227 13b2bc37 2022-10-23 stsp send_url = got_gitconfig_get_str(gitconfig,
228 13b2bc37 2022-10-23 stsp node->field, "url");
229 13b2bc37 2022-10-23 stsp if (send_url == NULL) {
230 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_GITCONFIG_SYNTAX);
231 13b2bc37 2022-10-23 stsp free(remote->name);
232 13b2bc37 2022-10-23 stsp remote->name = NULL;
233 13b2bc37 2022-10-23 stsp free(remote->fetch_url);
234 13b2bc37 2022-10-23 stsp remote->fetch_url = NULL;
237 13b2bc37 2022-10-23 stsp remote->send_url = strdup(send_url);
238 13b2bc37 2022-10-23 stsp if (remote->send_url == NULL) {
239 13b2bc37 2022-10-23 stsp err = got_error_from_errno("strdup");
240 13b2bc37 2022-10-23 stsp free(remote->name);
241 13b2bc37 2022-10-23 stsp remote->name = NULL;
242 13b2bc37 2022-10-23 stsp free(remote->fetch_url);
243 13b2bc37 2022-10-23 stsp remote->fetch_url = NULL;
247 13b2bc37 2022-10-23 stsp remote->mirror_references = 0;
248 13b2bc37 2022-10-23 stsp mirror = got_gitconfig_get_str(gitconfig, node->field,
250 13b2bc37 2022-10-23 stsp if (mirror != NULL && get_boolean_val(mirror))
251 13b2bc37 2022-10-23 stsp remote->mirror_references = 1;
254 13b2bc37 2022-10-23 stsp (*nremotes)++;
258 13b2bc37 2022-10-23 stsp if (fd != -1)
260 13b2bc37 2022-10-23 stsp if (gitconfig)
261 13b2bc37 2022-10-23 stsp got_gitconfig_close(gitconfig);
263 13b2bc37 2022-10-23 stsp if (extensions && nextensions) {
264 13b2bc37 2022-10-23 stsp for (i = 0; i < (*nextensions); i++)
265 13b2bc37 2022-10-23 stsp free((*extensions)[i]);
266 13b2bc37 2022-10-23 stsp free(*extensions);
267 13b2bc37 2022-10-23 stsp *extensions = NULL;
268 13b2bc37 2022-10-23 stsp *nextensions = 0;
270 13b2bc37 2022-10-23 stsp if (remotes && nremotes) {
271 13b2bc37 2022-10-23 stsp for (i = 0; i < (*nremotes); i++) {
272 13b2bc37 2022-10-23 stsp struct got_remote_repo *remote;
273 13b2bc37 2022-10-23 stsp remote = &(*remotes)[i];
274 13b2bc37 2022-10-23 stsp free(remote->name);
275 13b2bc37 2022-10-23 stsp free(remote->fetch_url);
276 13b2bc37 2022-10-23 stsp free(remote->send_url);
278 13b2bc37 2022-10-23 stsp free(*remotes);
279 13b2bc37 2022-10-23 stsp *remotes = NULL;
280 13b2bc37 2022-10-23 stsp *nremotes = 0;
283 13b2bc37 2022-10-23 stsp return err;