Blame


1 8a35f56c 2022-07-16 thomas /*
2 8a35f56c 2022-07-16 thomas * Copyright (c) 2016, 2019, 2020-2022 Tracey Emery <tracey@traceyemery.net>
3 8a35f56c 2022-07-16 thomas * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org>
4 2ac684a4 2022-09-03 thomas * Copyright (c) 2014 Reyk Floeter <reyk@openbsd.org>
5 8a35f56c 2022-07-16 thomas * Copyright (c) 2013 David Gwynne <dlg@openbsd.org>
6 8a35f56c 2022-07-16 thomas * Copyright (c) 2013 Florian Obser <florian@openbsd.org>
7 8a35f56c 2022-07-16 thomas *
8 8a35f56c 2022-07-16 thomas * Permission to use, copy, modify, and distribute this software for any
9 8a35f56c 2022-07-16 thomas * purpose with or without fee is hereby granted, provided that the above
10 8a35f56c 2022-07-16 thomas * copyright notice and this permission notice appear in all copies.
11 8a35f56c 2022-07-16 thomas *
12 8a35f56c 2022-07-16 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 8a35f56c 2022-07-16 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 8a35f56c 2022-07-16 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 8a35f56c 2022-07-16 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 8a35f56c 2022-07-16 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 8a35f56c 2022-07-16 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 8a35f56c 2022-07-16 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 8a35f56c 2022-07-16 thomas */
20 8a35f56c 2022-07-16 thomas
21 8a35f56c 2022-07-16 thomas #include <net/if.h>
22 8a35f56c 2022-07-16 thomas #include <netinet/in.h>
23 3e12c168 2022-07-16 thomas #include <sys/queue.h>
24 8a35f56c 2022-07-16 thomas #include <sys/stat.h>
25 8a35f56c 2022-07-16 thomas #include <sys/types.h>
26 8a35f56c 2022-07-16 thomas
27 2ac684a4 2022-09-03 thomas #include <ctype.h>
28 8a35f56c 2022-07-16 thomas #include <dirent.h>
29 8a35f56c 2022-07-16 thomas #include <errno.h>
30 8a35f56c 2022-07-16 thomas #include <event.h>
31 4606e6d4 2022-11-23 thomas #include <fcntl.h>
32 4606e6d4 2022-11-23 thomas #include <imsg.h>
33 8a35f56c 2022-07-16 thomas #include <stdio.h>
34 8a35f56c 2022-07-16 thomas #include <stdlib.h>
35 8a35f56c 2022-07-16 thomas #include <string.h>
36 8a35f56c 2022-07-16 thomas #include <unistd.h>
37 8a35f56c 2022-07-16 thomas
38 8a35f56c 2022-07-16 thomas #include "got_error.h"
39 8a35f56c 2022-07-16 thomas #include "got_object.h"
40 8a35f56c 2022-07-16 thomas #include "got_reference.h"
41 8a35f56c 2022-07-16 thomas #include "got_repository.h"
42 8a35f56c 2022-07-16 thomas #include "got_path.h"
43 8a35f56c 2022-07-16 thomas #include "got_cancel.h"
44 8a35f56c 2022-07-16 thomas #include "got_worktree.h"
45 8a35f56c 2022-07-16 thomas #include "got_diff.h"
46 8a35f56c 2022-07-16 thomas #include "got_commit_graph.h"
47 8a35f56c 2022-07-16 thomas #include "got_blame.h"
48 8a35f56c 2022-07-16 thomas #include "got_privsep.h"
49 cb11302c 2022-12-30 thomas
50 cb11302c 2022-12-30 thomas #include "got_compat.h"
51 8a35f56c 2022-07-16 thomas
52 8a35f56c 2022-07-16 thomas #include "proc.h"
53 8a35f56c 2022-07-16 thomas #include "gotwebd.h"
54 d6795e9f 2022-12-30 thomas #include "tmpl.h"
55 ff36aeea 2022-07-16 thomas
56 8a35f56c 2022-07-16 thomas static const struct querystring_keys querystring_keys[] = {
57 8a35f56c 2022-07-16 thomas { "action", ACTION },
58 8a35f56c 2022-07-16 thomas { "commit", COMMIT },
59 8a35f56c 2022-07-16 thomas { "file", RFILE },
60 8a35f56c 2022-07-16 thomas { "folder", FOLDER },
61 8a35f56c 2022-07-16 thomas { "headref", HEADREF },
62 8a35f56c 2022-07-16 thomas { "index_page", INDEX_PAGE },
63 8a35f56c 2022-07-16 thomas { "path", PATH },
64 8a35f56c 2022-07-16 thomas { "page", PAGE },
65 8a35f56c 2022-07-16 thomas };
66 8a35f56c 2022-07-16 thomas
67 8a35f56c 2022-07-16 thomas static const struct action_keys action_keys[] = {
68 8a35f56c 2022-07-16 thomas { "blame", BLAME },
69 8a35f56c 2022-07-16 thomas { "blob", BLOB },
70 8a35f56c 2022-07-16 thomas { "briefs", BRIEFS },
71 8a35f56c 2022-07-16 thomas { "commits", COMMITS },
72 8a35f56c 2022-07-16 thomas { "diff", DIFF },
73 8a35f56c 2022-07-16 thomas { "error", ERR },
74 8a35f56c 2022-07-16 thomas { "index", INDEX },
75 8a35f56c 2022-07-16 thomas { "summary", SUMMARY },
76 8a35f56c 2022-07-16 thomas { "tag", TAG },
77 8a35f56c 2022-07-16 thomas { "tags", TAGS },
78 8a35f56c 2022-07-16 thomas { "tree", TREE },
79 d6795e9f 2022-12-30 thomas { "rss", RSS },
80 8a35f56c 2022-07-16 thomas };
81 8a35f56c 2022-07-16 thomas
82 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_init_querystring(struct querystring **);
83 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_parse_querystring(struct querystring **,
84 8a35f56c 2022-07-16 thomas char *);
85 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_assign_querystring(struct querystring **,
86 8a35f56c 2022-07-16 thomas char *, char *);
87 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_render_index(struct request *);
88 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
89 8a35f56c 2022-07-16 thomas const char *);
90 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_load_got_path(struct request *c,
91 8a35f56c 2022-07-16 thomas struct repo_dir *);
92 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_get_repo_description(char **,
93 4606e6d4 2022-11-23 thomas struct server *, const char *, int);
94 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_get_clone_url(char **, struct server *,
95 4606e6d4 2022-11-23 thomas const char *, int);
96 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_render_blame(struct request *);
97 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_render_diff(struct request *);
98 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_render_summary(struct request *);
99 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_render_tag(struct request *);
100 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_render_tags(struct request *);
101 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_render_tree(struct request *);
102 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_render_branches(struct request *);
103 e7e5fa49 2022-12-30 thomas
104 8a35f56c 2022-07-16 thomas static void gotweb_free_querystring(struct querystring *);
105 8a35f56c 2022-07-16 thomas static void gotweb_free_repo_dir(struct repo_dir *);
106 8a35f56c 2022-07-16 thomas
107 3f0158b4 2022-08-30 thomas struct server *gotweb_get_server(uint8_t *, uint8_t *);
108 8a35f56c 2022-07-16 thomas
109 8a35f56c 2022-07-16 thomas void
110 8a35f56c 2022-07-16 thomas gotweb_process_request(struct request *c)
111 8a35f56c 2022-07-16 thomas {
112 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL, *error2 = NULL;
113 8a35f56c 2022-07-16 thomas struct server *srv = NULL;
114 8a35f56c 2022-07-16 thomas struct querystring *qs = NULL;
115 8a35f56c 2022-07-16 thomas struct repo_dir *repo_dir = NULL;
116 8a35f56c 2022-07-16 thomas uint8_t err[] = "gotwebd experienced an error: ";
117 79393471 2022-08-27 thomas int r, html = 0;
118 8a35f56c 2022-07-16 thomas
119 8a35f56c 2022-07-16 thomas /* init the transport */
120 8a35f56c 2022-07-16 thomas error = gotweb_init_transport(&c->t);
121 8a35f56c 2022-07-16 thomas if (error) {
122 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
123 46aeda9a 2022-08-27 thomas return;
124 8a35f56c 2022-07-16 thomas }
125 8a35f56c 2022-07-16 thomas /* don't process any further if client disconnected */
126 8a35f56c 2022-07-16 thomas if (c->sock->client_status == CLIENT_DISCONNECT)
127 8a35f56c 2022-07-16 thomas return;
128 8a35f56c 2022-07-16 thomas /* get the gotwebd server */
129 3f0158b4 2022-08-30 thomas srv = gotweb_get_server(c->server_name, c->http_host);
130 8a35f56c 2022-07-16 thomas if (srv == NULL) {
131 8a35f56c 2022-07-16 thomas log_warnx("%s: error server is NULL", __func__);
132 8a35f56c 2022-07-16 thomas goto err;
133 8a35f56c 2022-07-16 thomas }
134 8a35f56c 2022-07-16 thomas c->srv = srv;
135 8a35f56c 2022-07-16 thomas /* parse our querystring */
136 8a35f56c 2022-07-16 thomas error = gotweb_init_querystring(&qs);
137 8a35f56c 2022-07-16 thomas if (error) {
138 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
139 8a35f56c 2022-07-16 thomas goto err;
140 8a35f56c 2022-07-16 thomas }
141 8a35f56c 2022-07-16 thomas c->t->qs = qs;
142 8a35f56c 2022-07-16 thomas error = gotweb_parse_querystring(&qs, c->querystring);
143 8a35f56c 2022-07-16 thomas if (error) {
144 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
145 8a35f56c 2022-07-16 thomas goto err;
146 8a35f56c 2022-07-16 thomas }
147 8a35f56c 2022-07-16 thomas
148 8a35f56c 2022-07-16 thomas /*
149 8a35f56c 2022-07-16 thomas * certain actions require a commit id in the querystring. this stops
150 8a35f56c 2022-07-16 thomas * bad actors from exploiting this by manually manipulating the
151 8a35f56c 2022-07-16 thomas * querystring.
152 8a35f56c 2022-07-16 thomas */
153 8a35f56c 2022-07-16 thomas
154 8a35f56c 2022-07-16 thomas if (qs->commit == NULL && (qs->action == BLAME || qs->action == BLOB ||
155 8a35f56c 2022-07-16 thomas qs->action == DIFF)) {
156 8a35f56c 2022-07-16 thomas error2 = got_error(GOT_ERR_QUERYSTRING);
157 8a35f56c 2022-07-16 thomas goto render;
158 8a35f56c 2022-07-16 thomas }
159 8a35f56c 2022-07-16 thomas
160 8a35f56c 2022-07-16 thomas if (qs->action != INDEX) {
161 8a35f56c 2022-07-16 thomas error = gotweb_init_repo_dir(&repo_dir, qs->path);
162 8a35f56c 2022-07-16 thomas if (error)
163 8a35f56c 2022-07-16 thomas goto done;
164 8a35f56c 2022-07-16 thomas error = gotweb_load_got_path(c, repo_dir);
165 8a35f56c 2022-07-16 thomas c->t->repo_dir = repo_dir;
166 8a35f56c 2022-07-16 thomas if (error && error->code != GOT_ERR_LONELY_PACKIDX)
167 8a35f56c 2022-07-16 thomas goto err;
168 8a35f56c 2022-07-16 thomas }
169 8a35f56c 2022-07-16 thomas
170 00475f9a 2023-01-02 thomas if (qs->action == BLOB) {
171 8a35f56c 2022-07-16 thomas error = got_get_repo_commits(c, 1);
172 8a35f56c 2022-07-16 thomas if (error)
173 8a35f56c 2022-07-16 thomas goto done;
174 8a35f56c 2022-07-16 thomas error = got_output_file_blob(c);
175 d6795e9f 2022-12-30 thomas if (error) {
176 d6795e9f 2022-12-30 thomas log_warnx("%s: %s", __func__, error->msg);
177 d6795e9f 2022-12-30 thomas goto err;
178 d6795e9f 2022-12-30 thomas }
179 d6795e9f 2022-12-30 thomas goto done;
180 d6795e9f 2022-12-30 thomas }
181 d6795e9f 2022-12-30 thomas
182 d6795e9f 2022-12-30 thomas if (qs->action == RSS) {
183 d7034a4e 2023-01-06 thomas error = gotweb_render_content_type_file(c,
184 d7034a4e 2023-01-06 thomas "application/rss+xml;charset=utf-8",
185 d7034a4e 2023-01-06 thomas repo_dir->name, ".rss");
186 d6795e9f 2022-12-30 thomas if (error) {
187 d6795e9f 2022-12-30 thomas log_warnx("%s: %s", __func__, error->msg);
188 d6795e9f 2022-12-30 thomas goto err;
189 d6795e9f 2022-12-30 thomas }
190 d6795e9f 2022-12-30 thomas
191 d6795e9f 2022-12-30 thomas error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
192 8a35f56c 2022-07-16 thomas if (error) {
193 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
194 8a35f56c 2022-07-16 thomas goto err;
195 8a35f56c 2022-07-16 thomas }
196 d6795e9f 2022-12-30 thomas if (gotweb_render_rss(c->tp) == -1)
197 d6795e9f 2022-12-30 thomas goto err;
198 8a35f56c 2022-07-16 thomas goto done;
199 43a44bce 2022-12-04 thomas }
200 43a44bce 2022-12-04 thomas
201 43a44bce 2022-12-04 thomas render:
202 43a44bce 2022-12-04 thomas error = gotweb_render_content_type(c, "text/html");
203 43a44bce 2022-12-04 thomas if (error) {
204 43a44bce 2022-12-04 thomas log_warnx("%s: %s", __func__, error->msg);
205 43a44bce 2022-12-04 thomas goto err;
206 8a35f56c 2022-07-16 thomas }
207 43a44bce 2022-12-04 thomas html = 1;
208 8a35f56c 2022-07-16 thomas
209 e7e5fa49 2022-12-30 thomas if (gotweb_render_header(c->tp) == -1)
210 8a35f56c 2022-07-16 thomas goto err;
211 8a35f56c 2022-07-16 thomas
212 8a35f56c 2022-07-16 thomas if (error2) {
213 8a35f56c 2022-07-16 thomas error = error2;
214 8a35f56c 2022-07-16 thomas goto err;
215 8a35f56c 2022-07-16 thomas }
216 8a35f56c 2022-07-16 thomas
217 8a35f56c 2022-07-16 thomas switch(qs->action) {
218 8a35f56c 2022-07-16 thomas case BLAME:
219 8a35f56c 2022-07-16 thomas error = gotweb_render_blame(c);
220 8a35f56c 2022-07-16 thomas if (error) {
221 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
222 8a35f56c 2022-07-16 thomas goto err;
223 8a35f56c 2022-07-16 thomas }
224 8a35f56c 2022-07-16 thomas break;
225 8a35f56c 2022-07-16 thomas case BRIEFS:
226 e7e5fa49 2022-12-30 thomas if (gotweb_render_briefs(c->tp) == -1)
227 8a35f56c 2022-07-16 thomas goto err;
228 8a35f56c 2022-07-16 thomas break;
229 8a35f56c 2022-07-16 thomas case COMMITS:
230 7ade8b27 2022-12-30 thomas error = got_get_repo_commits(c, srv->max_commits_display);
231 8a35f56c 2022-07-16 thomas if (error) {
232 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
233 8a35f56c 2022-07-16 thomas goto err;
234 8a35f56c 2022-07-16 thomas }
235 7ade8b27 2022-12-30 thomas if (gotweb_render_commits(c->tp) == -1)
236 7ade8b27 2022-12-30 thomas goto err;
237 8a35f56c 2022-07-16 thomas break;
238 8a35f56c 2022-07-16 thomas case DIFF:
239 8a35f56c 2022-07-16 thomas error = gotweb_render_diff(c);
240 8a35f56c 2022-07-16 thomas if (error) {
241 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
242 8a35f56c 2022-07-16 thomas goto err;
243 8a35f56c 2022-07-16 thomas }
244 8a35f56c 2022-07-16 thomas break;
245 8a35f56c 2022-07-16 thomas case INDEX:
246 8a35f56c 2022-07-16 thomas error = gotweb_render_index(c);
247 8a35f56c 2022-07-16 thomas if (error) {
248 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
249 8a35f56c 2022-07-16 thomas goto err;
250 8a35f56c 2022-07-16 thomas }
251 8a35f56c 2022-07-16 thomas break;
252 8a35f56c 2022-07-16 thomas case SUMMARY:
253 8a35f56c 2022-07-16 thomas error = gotweb_render_summary(c);
254 8a35f56c 2022-07-16 thomas if (error) {
255 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
256 8a35f56c 2022-07-16 thomas goto err;
257 8a35f56c 2022-07-16 thomas }
258 8a35f56c 2022-07-16 thomas break;
259 8a35f56c 2022-07-16 thomas case TAG:
260 8a35f56c 2022-07-16 thomas error = gotweb_render_tag(c);
261 8a35f56c 2022-07-16 thomas if (error) {
262 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
263 8a35f56c 2022-07-16 thomas goto err;
264 8a35f56c 2022-07-16 thomas }
265 8a35f56c 2022-07-16 thomas break;
266 8a35f56c 2022-07-16 thomas case TAGS:
267 8a35f56c 2022-07-16 thomas error = gotweb_render_tags(c);
268 8a35f56c 2022-07-16 thomas if (error) {
269 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
270 8a35f56c 2022-07-16 thomas goto err;
271 8a35f56c 2022-07-16 thomas }
272 8a35f56c 2022-07-16 thomas break;
273 8a35f56c 2022-07-16 thomas case TREE:
274 8a35f56c 2022-07-16 thomas error = gotweb_render_tree(c);
275 8a35f56c 2022-07-16 thomas if (error) {
276 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
277 8a35f56c 2022-07-16 thomas goto err;
278 8a35f56c 2022-07-16 thomas }
279 8a35f56c 2022-07-16 thomas break;
280 8a35f56c 2022-07-16 thomas case ERR:
281 8a35f56c 2022-07-16 thomas default:
282 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
283 79393471 2022-08-27 thomas "Erorr: Bad Querystring");
284 79393471 2022-08-27 thomas if (r == -1)
285 8a35f56c 2022-07-16 thomas goto err;
286 8a35f56c 2022-07-16 thomas break;
287 8a35f56c 2022-07-16 thomas }
288 8a35f56c 2022-07-16 thomas
289 8a35f56c 2022-07-16 thomas goto done;
290 8a35f56c 2022-07-16 thomas err:
291 79393471 2022-08-27 thomas if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
292 8a35f56c 2022-07-16 thomas return;
293 a5f25a12 2022-10-31 thomas if (fcgi_printf(c, "\n%s", err) == -1)
294 8a35f56c 2022-07-16 thomas return;
295 8a35f56c 2022-07-16 thomas if (error) {
296 79393471 2022-08-27 thomas if (fcgi_printf(c, "%s", error->msg) == -1)
297 8a35f56c 2022-07-16 thomas return;
298 8a35f56c 2022-07-16 thomas } else {
299 79393471 2022-08-27 thomas if (fcgi_printf(c, "see daemon logs for details") == -1)
300 8a35f56c 2022-07-16 thomas return;
301 8a35f56c 2022-07-16 thomas }
302 79393471 2022-08-27 thomas if (html && fcgi_printf(c, "</div>\n") == -1)
303 8a35f56c 2022-07-16 thomas return;
304 8a35f56c 2022-07-16 thomas done:
305 8a35f56c 2022-07-16 thomas if (html && srv != NULL)
306 e7e5fa49 2022-12-30 thomas gotweb_render_footer(c->tp);
307 8a35f56c 2022-07-16 thomas }
308 8a35f56c 2022-07-16 thomas
309 8a35f56c 2022-07-16 thomas struct server *
310 3f0158b4 2022-08-30 thomas gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
311 8a35f56c 2022-07-16 thomas {
312 8a35f56c 2022-07-16 thomas struct server *srv = NULL;
313 8a35f56c 2022-07-16 thomas
314 3f0158b4 2022-08-30 thomas /* check against the server name first */
315 8a35f56c 2022-07-16 thomas if (strlen(server_name) > 0)
316 90d63d47 2022-08-16 thomas TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
317 8a35f56c 2022-07-16 thomas if (strcmp(srv->name, server_name) == 0)
318 8a35f56c 2022-07-16 thomas goto done;
319 8a35f56c 2022-07-16 thomas
320 3f0158b4 2022-08-30 thomas /* check against subdomain second */
321 8a35f56c 2022-07-16 thomas if (strlen(subdomain) > 0)
322 90d63d47 2022-08-16 thomas TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
323 8a35f56c 2022-07-16 thomas if (strcmp(srv->name, subdomain) == 0)
324 8a35f56c 2022-07-16 thomas goto done;
325 8a35f56c 2022-07-16 thomas
326 8a35f56c 2022-07-16 thomas /* if those fail, send first server */
327 90d63d47 2022-08-16 thomas TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
328 8a35f56c 2022-07-16 thomas if (srv != NULL)
329 8a35f56c 2022-07-16 thomas break;
330 8a35f56c 2022-07-16 thomas done:
331 8a35f56c 2022-07-16 thomas return srv;
332 8a35f56c 2022-07-16 thomas };
333 8a35f56c 2022-07-16 thomas
334 8a35f56c 2022-07-16 thomas const struct got_error *
335 8a35f56c 2022-07-16 thomas gotweb_init_transport(struct transport **t)
336 8a35f56c 2022-07-16 thomas {
337 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
338 8a35f56c 2022-07-16 thomas
339 8a35f56c 2022-07-16 thomas *t = calloc(1, sizeof(**t));
340 8a35f56c 2022-07-16 thomas if (*t == NULL)
341 8a35f56c 2022-07-16 thomas return got_error_from_errno2("%s: calloc", __func__);
342 8a35f56c 2022-07-16 thomas
343 8a35f56c 2022-07-16 thomas TAILQ_INIT(&(*t)->repo_commits);
344 8a35f56c 2022-07-16 thomas TAILQ_INIT(&(*t)->repo_tags);
345 8a35f56c 2022-07-16 thomas
346 8a35f56c 2022-07-16 thomas (*t)->repo = NULL;
347 8a35f56c 2022-07-16 thomas (*t)->repo_dir = NULL;
348 8a35f56c 2022-07-16 thomas (*t)->qs = NULL;
349 8a35f56c 2022-07-16 thomas (*t)->next_id = NULL;
350 8a35f56c 2022-07-16 thomas (*t)->prev_id = NULL;
351 8a35f56c 2022-07-16 thomas (*t)->next_disp = 0;
352 8a35f56c 2022-07-16 thomas (*t)->prev_disp = 0;
353 8a35f56c 2022-07-16 thomas
354 8a35f56c 2022-07-16 thomas return error;
355 8a35f56c 2022-07-16 thomas }
356 8a35f56c 2022-07-16 thomas
357 8a35f56c 2022-07-16 thomas static const struct got_error *
358 8a35f56c 2022-07-16 thomas gotweb_init_querystring(struct querystring **qs)
359 8a35f56c 2022-07-16 thomas {
360 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
361 8a35f56c 2022-07-16 thomas
362 8a35f56c 2022-07-16 thomas *qs = calloc(1, sizeof(**qs));
363 8a35f56c 2022-07-16 thomas if (*qs == NULL)
364 8a35f56c 2022-07-16 thomas return got_error_from_errno2("%s: calloc", __func__);
365 8a35f56c 2022-07-16 thomas
366 8a35f56c 2022-07-16 thomas (*qs)->headref = strdup("HEAD");
367 8a35f56c 2022-07-16 thomas if ((*qs)->headref == NULL) {
368 84630254 2022-09-02 thomas free(*qs);
369 84630254 2022-09-02 thomas *qs = NULL;
370 8a35f56c 2022-07-16 thomas return got_error_from_errno2("%s: strdup", __func__);
371 8a35f56c 2022-07-16 thomas }
372 84630254 2022-09-02 thomas
373 84630254 2022-09-02 thomas (*qs)->action = INDEX;
374 84630254 2022-09-02 thomas (*qs)->commit = NULL;
375 84630254 2022-09-02 thomas (*qs)->file = NULL;
376 84630254 2022-09-02 thomas (*qs)->folder = NULL;
377 8a35f56c 2022-07-16 thomas (*qs)->index_page = 0;
378 8a35f56c 2022-07-16 thomas (*qs)->path = NULL;
379 8a35f56c 2022-07-16 thomas
380 8a35f56c 2022-07-16 thomas return error;
381 8a35f56c 2022-07-16 thomas }
382 8a35f56c 2022-07-16 thomas
383 8a35f56c 2022-07-16 thomas static const struct got_error *
384 8a35f56c 2022-07-16 thomas gotweb_parse_querystring(struct querystring **qs, char *qst)
385 8a35f56c 2022-07-16 thomas {
386 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
387 8a35f56c 2022-07-16 thomas char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
388 8a35f56c 2022-07-16 thomas char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
389 8a35f56c 2022-07-16 thomas
390 8a35f56c 2022-07-16 thomas if (qst == NULL)
391 8a35f56c 2022-07-16 thomas return error;
392 8a35f56c 2022-07-16 thomas
393 8a35f56c 2022-07-16 thomas tok1 = strdup(qst);
394 8a35f56c 2022-07-16 thomas if (tok1 == NULL)
395 8a35f56c 2022-07-16 thomas return got_error_from_errno2("%s: strdup", __func__);
396 8a35f56c 2022-07-16 thomas
397 8a35f56c 2022-07-16 thomas tok1_pair = tok1;
398 8a35f56c 2022-07-16 thomas tok1_end = tok1;
399 8a35f56c 2022-07-16 thomas
400 8a35f56c 2022-07-16 thomas while (tok1_pair != NULL) {
401 8a35f56c 2022-07-16 thomas strsep(&tok1_end, "&");
402 8a35f56c 2022-07-16 thomas
403 8a35f56c 2022-07-16 thomas tok2 = strdup(tok1_pair);
404 8a35f56c 2022-07-16 thomas if (tok2 == NULL) {
405 8a35f56c 2022-07-16 thomas free(tok1);
406 8a35f56c 2022-07-16 thomas return got_error_from_errno2("%s: strdup", __func__);
407 8a35f56c 2022-07-16 thomas }
408 8a35f56c 2022-07-16 thomas
409 8a35f56c 2022-07-16 thomas tok2_pair = tok2;
410 8a35f56c 2022-07-16 thomas tok2_end = tok2;
411 8a35f56c 2022-07-16 thomas
412 8a35f56c 2022-07-16 thomas while (tok2_pair != NULL) {
413 8a35f56c 2022-07-16 thomas strsep(&tok2_end, "=");
414 8a35f56c 2022-07-16 thomas if (tok2_end) {
415 8a35f56c 2022-07-16 thomas error = gotweb_assign_querystring(qs, tok2_pair,
416 8a35f56c 2022-07-16 thomas tok2_end);
417 8a35f56c 2022-07-16 thomas if (error)
418 8a35f56c 2022-07-16 thomas goto err;
419 8a35f56c 2022-07-16 thomas }
420 8a35f56c 2022-07-16 thomas tok2_pair = tok2_end;
421 8a35f56c 2022-07-16 thomas }
422 8a35f56c 2022-07-16 thomas free(tok2);
423 8a35f56c 2022-07-16 thomas tok1_pair = tok1_end;
424 8a35f56c 2022-07-16 thomas }
425 8a35f56c 2022-07-16 thomas free(tok1);
426 8a35f56c 2022-07-16 thomas return error;
427 8a35f56c 2022-07-16 thomas err:
428 8a35f56c 2022-07-16 thomas free(tok2);
429 8a35f56c 2022-07-16 thomas free(tok1);
430 8a35f56c 2022-07-16 thomas return error;
431 8a35f56c 2022-07-16 thomas }
432 8a35f56c 2022-07-16 thomas
433 2ac684a4 2022-09-03 thomas /*
434 2ac684a4 2022-09-03 thomas * Adapted from usr.sbin/httpd/httpd.c url_decode.
435 2ac684a4 2022-09-03 thomas */
436 8a35f56c 2022-07-16 thomas static const struct got_error *
437 2ac684a4 2022-09-03 thomas gotweb_urldecode(char *url)
438 2ac684a4 2022-09-03 thomas {
439 2ac684a4 2022-09-03 thomas char *p, *q;
440 2ac684a4 2022-09-03 thomas char hex[3];
441 2ac684a4 2022-09-03 thomas unsigned long x;
442 2ac684a4 2022-09-03 thomas
443 2ac684a4 2022-09-03 thomas hex[2] = '\0';
444 2ac684a4 2022-09-03 thomas p = q = url;
445 2ac684a4 2022-09-03 thomas
446 2ac684a4 2022-09-03 thomas while (*p != '\0') {
447 2ac684a4 2022-09-03 thomas switch (*p) {
448 2ac684a4 2022-09-03 thomas case '%':
449 2ac684a4 2022-09-03 thomas /* Encoding character is followed by two hex chars */
450 2ac684a4 2022-09-03 thomas if (!isxdigit((unsigned char)p[1]) ||
451 2ac684a4 2022-09-03 thomas !isxdigit((unsigned char)p[2]) ||
452 2ac684a4 2022-09-03 thomas (p[1] == '0' && p[2] == '0'))
453 2ac684a4 2022-09-03 thomas return got_error(GOT_ERR_BAD_QUERYSTRING);
454 2ac684a4 2022-09-03 thomas
455 2ac684a4 2022-09-03 thomas hex[0] = p[1];
456 2ac684a4 2022-09-03 thomas hex[1] = p[2];
457 2ac684a4 2022-09-03 thomas
458 2ac684a4 2022-09-03 thomas /*
459 2ac684a4 2022-09-03 thomas * We don't have to validate "hex" because it is
460 2ac684a4 2022-09-03 thomas * guaranteed to include two hex chars followed by nul.
461 2ac684a4 2022-09-03 thomas */
462 2ac684a4 2022-09-03 thomas x = strtoul(hex, NULL, 16);
463 2ac684a4 2022-09-03 thomas *q = (char)x;
464 2ac684a4 2022-09-03 thomas p += 2;
465 2ac684a4 2022-09-03 thomas break;
466 2ac684a4 2022-09-03 thomas default:
467 2ac684a4 2022-09-03 thomas *q = *p;
468 2ac684a4 2022-09-03 thomas break;
469 2ac684a4 2022-09-03 thomas }
470 2ac684a4 2022-09-03 thomas p++;
471 2ac684a4 2022-09-03 thomas q++;
472 2ac684a4 2022-09-03 thomas }
473 2ac684a4 2022-09-03 thomas *q = '\0';
474 2ac684a4 2022-09-03 thomas
475 2ac684a4 2022-09-03 thomas return NULL;
476 2ac684a4 2022-09-03 thomas }
477 2ac684a4 2022-09-03 thomas
478 2ac684a4 2022-09-03 thomas static const struct got_error *
479 8a35f56c 2022-07-16 thomas gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
480 8a35f56c 2022-07-16 thomas {
481 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
482 8a35f56c 2022-07-16 thomas const char *errstr;
483 8a35f56c 2022-07-16 thomas int a_cnt, el_cnt;
484 8a35f56c 2022-07-16 thomas
485 2ac684a4 2022-09-03 thomas error = gotweb_urldecode(value);
486 2ac684a4 2022-09-03 thomas if (error)
487 2ac684a4 2022-09-03 thomas return error;
488 2ac684a4 2022-09-03 thomas
489 8a35f56c 2022-07-16 thomas for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
490 8a35f56c 2022-07-16 thomas if (strcmp(key, querystring_keys[el_cnt].name) != 0)
491 8a35f56c 2022-07-16 thomas continue;
492 8a35f56c 2022-07-16 thomas
493 8a35f56c 2022-07-16 thomas switch (querystring_keys[el_cnt].element) {
494 8a35f56c 2022-07-16 thomas case ACTION:
495 8a35f56c 2022-07-16 thomas for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
496 8a35f56c 2022-07-16 thomas if (strcmp(value, action_keys[a_cnt].name) != 0)
497 8a35f56c 2022-07-16 thomas continue;
498 8a35f56c 2022-07-16 thomas else if (strcmp(value,
499 8a35f56c 2022-07-16 thomas action_keys[a_cnt].name) == 0){
500 8a35f56c 2022-07-16 thomas (*qs)->action =
501 8a35f56c 2022-07-16 thomas action_keys[a_cnt].action;
502 8a35f56c 2022-07-16 thomas goto qa_found;
503 8a35f56c 2022-07-16 thomas }
504 8a35f56c 2022-07-16 thomas }
505 8a35f56c 2022-07-16 thomas (*qs)->action = ERR;
506 8a35f56c 2022-07-16 thomas qa_found:
507 8a35f56c 2022-07-16 thomas break;
508 8a35f56c 2022-07-16 thomas case COMMIT:
509 8a35f56c 2022-07-16 thomas (*qs)->commit = strdup(value);
510 8a35f56c 2022-07-16 thomas if ((*qs)->commit == NULL) {
511 8a35f56c 2022-07-16 thomas error = got_error_from_errno2("%s: strdup",
512 8a35f56c 2022-07-16 thomas __func__);
513 8a35f56c 2022-07-16 thomas goto done;
514 8a35f56c 2022-07-16 thomas }
515 8a35f56c 2022-07-16 thomas break;
516 8a35f56c 2022-07-16 thomas case RFILE:
517 8a35f56c 2022-07-16 thomas (*qs)->file = strdup(value);
518 8a35f56c 2022-07-16 thomas if ((*qs)->file == NULL) {
519 8a35f56c 2022-07-16 thomas error = got_error_from_errno2("%s: strdup",
520 8a35f56c 2022-07-16 thomas __func__);
521 8a35f56c 2022-07-16 thomas goto done;
522 8a35f56c 2022-07-16 thomas }
523 8a35f56c 2022-07-16 thomas break;
524 8a35f56c 2022-07-16 thomas case FOLDER:
525 8a35f56c 2022-07-16 thomas (*qs)->folder = strdup(value);
526 8a35f56c 2022-07-16 thomas if ((*qs)->folder == NULL) {
527 8a35f56c 2022-07-16 thomas error = got_error_from_errno2("%s: strdup",
528 8a35f56c 2022-07-16 thomas __func__);
529 8a35f56c 2022-07-16 thomas goto done;
530 8a35f56c 2022-07-16 thomas }
531 8a35f56c 2022-07-16 thomas break;
532 8a35f56c 2022-07-16 thomas case HEADREF:
533 b0764984 2022-09-02 thomas free((*qs)->headref);
534 8a35f56c 2022-07-16 thomas (*qs)->headref = strdup(value);
535 8a35f56c 2022-07-16 thomas if ((*qs)->headref == NULL) {
536 8a35f56c 2022-07-16 thomas error = got_error_from_errno2("%s: strdup",
537 8a35f56c 2022-07-16 thomas __func__);
538 8a35f56c 2022-07-16 thomas goto done;
539 8a35f56c 2022-07-16 thomas }
540 8a35f56c 2022-07-16 thomas break;
541 8a35f56c 2022-07-16 thomas case INDEX_PAGE:
542 8a35f56c 2022-07-16 thomas if (strlen(value) == 0)
543 8a35f56c 2022-07-16 thomas break;
544 8a35f56c 2022-07-16 thomas (*qs)->index_page = strtonum(value, INT64_MIN,
545 8a35f56c 2022-07-16 thomas INT64_MAX, &errstr);
546 8a35f56c 2022-07-16 thomas if (errstr) {
547 8a35f56c 2022-07-16 thomas error = got_error_from_errno3("%s: strtonum %s",
548 8a35f56c 2022-07-16 thomas __func__, errstr);
549 8a35f56c 2022-07-16 thomas goto done;
550 8a35f56c 2022-07-16 thomas }
551 3d6d1fb0 2022-12-30 thomas if ((*qs)->index_page < 0)
552 8a35f56c 2022-07-16 thomas (*qs)->index_page = 0;
553 8a35f56c 2022-07-16 thomas break;
554 8a35f56c 2022-07-16 thomas case PATH:
555 8a35f56c 2022-07-16 thomas (*qs)->path = strdup(value);
556 8a35f56c 2022-07-16 thomas if ((*qs)->path == NULL) {
557 8a35f56c 2022-07-16 thomas error = got_error_from_errno2("%s: strdup",
558 8a35f56c 2022-07-16 thomas __func__);
559 8a35f56c 2022-07-16 thomas goto done;
560 8a35f56c 2022-07-16 thomas }
561 8a35f56c 2022-07-16 thomas break;
562 8a35f56c 2022-07-16 thomas case PAGE:
563 8a35f56c 2022-07-16 thomas if (strlen(value) == 0)
564 8a35f56c 2022-07-16 thomas break;
565 8a35f56c 2022-07-16 thomas (*qs)->page = strtonum(value, INT64_MIN,
566 8a35f56c 2022-07-16 thomas INT64_MAX, &errstr);
567 8a35f56c 2022-07-16 thomas if (errstr) {
568 8a35f56c 2022-07-16 thomas error = got_error_from_errno3("%s: strtonum %s",
569 8a35f56c 2022-07-16 thomas __func__, errstr);
570 8a35f56c 2022-07-16 thomas goto done;
571 8a35f56c 2022-07-16 thomas }
572 3d6d1fb0 2022-12-30 thomas if ((*qs)->page < 0)
573 8a35f56c 2022-07-16 thomas (*qs)->page = 0;
574 8a35f56c 2022-07-16 thomas break;
575 8a35f56c 2022-07-16 thomas default:
576 8a35f56c 2022-07-16 thomas break;
577 8a35f56c 2022-07-16 thomas }
578 8a35f56c 2022-07-16 thomas }
579 8a35f56c 2022-07-16 thomas done:
580 8a35f56c 2022-07-16 thomas return error;
581 8a35f56c 2022-07-16 thomas }
582 8a35f56c 2022-07-16 thomas
583 8a35f56c 2022-07-16 thomas void
584 8a35f56c 2022-07-16 thomas gotweb_free_repo_tag(struct repo_tag *rt)
585 8a35f56c 2022-07-16 thomas {
586 8a35f56c 2022-07-16 thomas if (rt != NULL) {
587 8a35f56c 2022-07-16 thomas free(rt->commit_id);
588 217813df 2022-09-02 thomas free(rt->tag_name);
589 217813df 2022-09-02 thomas free(rt->tag_commit);
590 217813df 2022-09-02 thomas free(rt->commit_msg);
591 8a35f56c 2022-07-16 thomas free(rt->tagger);
592 8a35f56c 2022-07-16 thomas }
593 8a35f56c 2022-07-16 thomas free(rt);
594 8a35f56c 2022-07-16 thomas }
595 8a35f56c 2022-07-16 thomas
596 8a35f56c 2022-07-16 thomas void
597 8a35f56c 2022-07-16 thomas gotweb_free_repo_commit(struct repo_commit *rc)
598 8a35f56c 2022-07-16 thomas {
599 8a35f56c 2022-07-16 thomas if (rc != NULL) {
600 8a35f56c 2022-07-16 thomas free(rc->path);
601 8a35f56c 2022-07-16 thomas free(rc->refs_str);
602 8a35f56c 2022-07-16 thomas free(rc->commit_id);
603 8a35f56c 2022-07-16 thomas free(rc->parent_id);
604 8a35f56c 2022-07-16 thomas free(rc->tree_id);
605 8a35f56c 2022-07-16 thomas free(rc->author);
606 8a35f56c 2022-07-16 thomas free(rc->committer);
607 8a35f56c 2022-07-16 thomas free(rc->commit_msg);
608 8a35f56c 2022-07-16 thomas }
609 8a35f56c 2022-07-16 thomas free(rc);
610 8a35f56c 2022-07-16 thomas }
611 8a35f56c 2022-07-16 thomas
612 8a35f56c 2022-07-16 thomas static void
613 8a35f56c 2022-07-16 thomas gotweb_free_querystring(struct querystring *qs)
614 8a35f56c 2022-07-16 thomas {
615 8a35f56c 2022-07-16 thomas if (qs != NULL) {
616 8a35f56c 2022-07-16 thomas free(qs->commit);
617 8a35f56c 2022-07-16 thomas free(qs->file);
618 8a35f56c 2022-07-16 thomas free(qs->folder);
619 8a35f56c 2022-07-16 thomas free(qs->headref);
620 8a35f56c 2022-07-16 thomas free(qs->path);
621 8a35f56c 2022-07-16 thomas }
622 8a35f56c 2022-07-16 thomas free(qs);
623 8a35f56c 2022-07-16 thomas }
624 8a35f56c 2022-07-16 thomas
625 8a35f56c 2022-07-16 thomas static void
626 8a35f56c 2022-07-16 thomas gotweb_free_repo_dir(struct repo_dir *repo_dir)
627 8a35f56c 2022-07-16 thomas {
628 8a35f56c 2022-07-16 thomas if (repo_dir != NULL) {
629 8a35f56c 2022-07-16 thomas free(repo_dir->name);
630 8a35f56c 2022-07-16 thomas free(repo_dir->owner);
631 8a35f56c 2022-07-16 thomas free(repo_dir->description);
632 8a35f56c 2022-07-16 thomas free(repo_dir->url);
633 8a35f56c 2022-07-16 thomas free(repo_dir->age);
634 8a35f56c 2022-07-16 thomas free(repo_dir->path);
635 8a35f56c 2022-07-16 thomas }
636 8a35f56c 2022-07-16 thomas free(repo_dir);
637 8a35f56c 2022-07-16 thomas }
638 8a35f56c 2022-07-16 thomas
639 8a35f56c 2022-07-16 thomas void
640 8a35f56c 2022-07-16 thomas gotweb_free_transport(struct transport *t)
641 8a35f56c 2022-07-16 thomas {
642 8a35f56c 2022-07-16 thomas struct repo_commit *rc = NULL, *trc = NULL;
643 8a35f56c 2022-07-16 thomas struct repo_tag *rt = NULL, *trt = NULL;
644 8a35f56c 2022-07-16 thomas
645 8a35f56c 2022-07-16 thomas TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
646 8a35f56c 2022-07-16 thomas TAILQ_REMOVE(&t->repo_commits, rc, entry);
647 8a35f56c 2022-07-16 thomas gotweb_free_repo_commit(rc);
648 8a35f56c 2022-07-16 thomas }
649 8a35f56c 2022-07-16 thomas TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
650 8a35f56c 2022-07-16 thomas TAILQ_REMOVE(&t->repo_tags, rt, entry);
651 8a35f56c 2022-07-16 thomas gotweb_free_repo_tag(rt);
652 8a35f56c 2022-07-16 thomas }
653 8a35f56c 2022-07-16 thomas gotweb_free_repo_dir(t->repo_dir);
654 8a35f56c 2022-07-16 thomas gotweb_free_querystring(t->qs);
655 6b42af1e 2022-09-02 thomas free(t->next_id);
656 6b42af1e 2022-09-02 thomas free(t->prev_id);
657 8a35f56c 2022-07-16 thomas free(t);
658 8a35f56c 2022-07-16 thomas }
659 8a35f56c 2022-07-16 thomas
660 8a35f56c 2022-07-16 thomas const struct got_error *
661 0cef9478 2023-01-06 thomas gotweb_render_content_type(struct request *c, const char *type)
662 8a35f56c 2022-07-16 thomas {
663 0b75e088 2022-08-27 thomas const char *csp = "default-src 'self'; script-src 'none'; "
664 0b75e088 2022-08-27 thomas "object-src 'none';";
665 0b75e088 2022-08-27 thomas
666 0b75e088 2022-08-27 thomas fcgi_printf(c,
667 0b75e088 2022-08-27 thomas "Content-Security-Policy: %s\r\n"
668 0b75e088 2022-08-27 thomas "Content-Type: %s\r\n\r\n",
669 0b75e088 2022-08-27 thomas csp, type);
670 79393471 2022-08-27 thomas return NULL;
671 8a35f56c 2022-07-16 thomas }
672 8a35f56c 2022-07-16 thomas
673 8a35f56c 2022-07-16 thomas const struct got_error *
674 c7c38295 2023-01-06 thomas gotweb_render_content_type_file(struct request *c, const char *type,
675 d7034a4e 2023-01-06 thomas const char *file, const char *suffix)
676 8a35f56c 2022-07-16 thomas {
677 79393471 2022-08-27 thomas fcgi_printf(c, "Content-type: %s\r\n"
678 d7034a4e 2023-01-06 thomas "Content-disposition: attachment; filename=%s%s\r\n\r\n",
679 d7034a4e 2023-01-06 thomas type, file, suffix ? suffix : "");
680 79393471 2022-08-27 thomas return NULL;
681 8a35f56c 2022-07-16 thomas }
682 8a35f56c 2022-07-16 thomas
683 2f4f0731 2022-12-30 thomas void
684 2f4f0731 2022-12-30 thomas gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
685 2f4f0731 2022-12-30 thomas struct gotweb_url *next, int *have_next)
686 8a35f56c 2022-07-16 thomas {
687 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
688 8a35f56c 2022-07-16 thomas struct querystring *qs = t->qs;
689 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
690 8a35f56c 2022-07-16 thomas
691 2f4f0731 2022-12-30 thomas *have_prev = *have_next = 0;
692 8a35f56c 2022-07-16 thomas
693 8a35f56c 2022-07-16 thomas switch(qs->action) {
694 8a35f56c 2022-07-16 thomas case INDEX:
695 8a35f56c 2022-07-16 thomas if (qs->index_page > 0) {
696 2f4f0731 2022-12-30 thomas *have_prev = 1;
697 2f4f0731 2022-12-30 thomas *prev = (struct gotweb_url){
698 55144267 2022-09-07 thomas .action = -1,
699 55144267 2022-09-07 thomas .index_page = qs->index_page - 1,
700 55144267 2022-09-07 thomas .page = -1,
701 55144267 2022-09-07 thomas };
702 8a35f56c 2022-07-16 thomas }
703 2f4f0731 2022-12-30 thomas if (t->next_disp == srv->max_repos_display &&
704 2f4f0731 2022-12-30 thomas t->repos_total != (qs->index_page + 1) *
705 2f4f0731 2022-12-30 thomas srv->max_repos_display) {
706 2f4f0731 2022-12-30 thomas *have_next = 1;
707 2f4f0731 2022-12-30 thomas *next = (struct gotweb_url){
708 2f4f0731 2022-12-30 thomas .action = -1,
709 2f4f0731 2022-12-30 thomas .index_page = qs->index_page + 1,
710 2f4f0731 2022-12-30 thomas .page = -1,
711 2f4f0731 2022-12-30 thomas };
712 2f4f0731 2022-12-30 thomas }
713 8a35f56c 2022-07-16 thomas break;
714 8a35f56c 2022-07-16 thomas case BRIEFS:
715 8a35f56c 2022-07-16 thomas if (t->prev_id && qs->commit != NULL &&
716 8a35f56c 2022-07-16 thomas strcmp(qs->commit, t->prev_id) != 0) {
717 2f4f0731 2022-12-30 thomas *have_prev = 1;
718 2f4f0731 2022-12-30 thomas *prev = (struct gotweb_url){
719 55144267 2022-09-07 thomas .action = BRIEFS,
720 55144267 2022-09-07 thomas .index_page = -1,
721 55144267 2022-09-07 thomas .page = qs->page - 1,
722 55144267 2022-09-07 thomas .path = qs->path,
723 55144267 2022-09-07 thomas .commit = t->prev_id,
724 55144267 2022-09-07 thomas .headref = qs->headref,
725 55144267 2022-09-07 thomas };
726 8a35f56c 2022-07-16 thomas }
727 2f4f0731 2022-12-30 thomas if (t->next_id) {
728 2f4f0731 2022-12-30 thomas *have_next = 1;
729 2f4f0731 2022-12-30 thomas *next = (struct gotweb_url){
730 2f4f0731 2022-12-30 thomas .action = BRIEFS,
731 2f4f0731 2022-12-30 thomas .index_page = -1,
732 2f4f0731 2022-12-30 thomas .page = qs->page + 1,
733 2f4f0731 2022-12-30 thomas .path = qs->path,
734 2f4f0731 2022-12-30 thomas .commit = t->next_id,
735 2f4f0731 2022-12-30 thomas .headref = qs->headref,
736 2f4f0731 2022-12-30 thomas };
737 2f4f0731 2022-12-30 thomas }
738 8a35f56c 2022-07-16 thomas break;
739 8a35f56c 2022-07-16 thomas case COMMITS:
740 8a35f56c 2022-07-16 thomas if (t->prev_id && qs->commit != NULL &&
741 8a35f56c 2022-07-16 thomas strcmp(qs->commit, t->prev_id) != 0) {
742 2f4f0731 2022-12-30 thomas *have_prev = 1;
743 2f4f0731 2022-12-30 thomas *prev = (struct gotweb_url){
744 8ea2e76e 2022-12-30 thomas .action = COMMITS,
745 55144267 2022-09-07 thomas .index_page = -1,
746 55144267 2022-09-07 thomas .page = qs->page - 1,
747 55144267 2022-09-07 thomas .path = qs->path,
748 55144267 2022-09-07 thomas .commit = t->prev_id,
749 55144267 2022-09-07 thomas .headref = qs->headref,
750 55144267 2022-09-07 thomas .folder = qs->folder,
751 55144267 2022-09-07 thomas .file = qs->file,
752 55144267 2022-09-07 thomas };
753 8a35f56c 2022-07-16 thomas }
754 2f4f0731 2022-12-30 thomas if (t->next_id) {
755 2f4f0731 2022-12-30 thomas *have_next = 1;
756 2f4f0731 2022-12-30 thomas *next = (struct gotweb_url){
757 8ea2e76e 2022-12-30 thomas .action = COMMITS,
758 55144267 2022-09-07 thomas .index_page = -1,
759 55144267 2022-09-07 thomas .page = qs->page + 1,
760 55144267 2022-09-07 thomas .path = qs->path,
761 55144267 2022-09-07 thomas .commit = t->next_id,
762 55144267 2022-09-07 thomas .headref = qs->headref,
763 55144267 2022-09-07 thomas .folder = qs->folder,
764 55144267 2022-09-07 thomas .file = qs->file,
765 55144267 2022-09-07 thomas };
766 8a35f56c 2022-07-16 thomas }
767 8a35f56c 2022-07-16 thomas break;
768 8a35f56c 2022-07-16 thomas case TAGS:
769 2f4f0731 2022-12-30 thomas if (t->prev_id && qs->commit != NULL &&
770 2f4f0731 2022-12-30 thomas strcmp(qs->commit, t->prev_id) != 0) {
771 2f4f0731 2022-12-30 thomas *have_prev = 1;
772 2f4f0731 2022-12-30 thomas *prev = (struct gotweb_url){
773 2f4f0731 2022-12-30 thomas .action = TAGS,
774 2f4f0731 2022-12-30 thomas .index_page = -1,
775 2f4f0731 2022-12-30 thomas .page = qs->page - 1,
776 2f4f0731 2022-12-30 thomas .path = qs->path,
777 2f4f0731 2022-12-30 thomas .commit = t->prev_id,
778 2f4f0731 2022-12-30 thomas .headref = qs->headref,
779 2f4f0731 2022-12-30 thomas };
780 2f4f0731 2022-12-30 thomas }
781 8a35f56c 2022-07-16 thomas if (t->next_id) {
782 2f4f0731 2022-12-30 thomas *have_next = 1;
783 2f4f0731 2022-12-30 thomas *next = (struct gotweb_url){
784 55144267 2022-09-07 thomas .action = TAGS,
785 55144267 2022-09-07 thomas .index_page = -1,
786 55144267 2022-09-07 thomas .page = qs->page + 1,
787 55144267 2022-09-07 thomas .path = qs->path,
788 55144267 2022-09-07 thomas .commit = t->next_id,
789 55144267 2022-09-07 thomas .headref = qs->headref,
790 55144267 2022-09-07 thomas };
791 8a35f56c 2022-07-16 thomas }
792 8a35f56c 2022-07-16 thomas break;
793 8a35f56c 2022-07-16 thomas }
794 8a35f56c 2022-07-16 thomas }
795 8a35f56c 2022-07-16 thomas
796 8a35f56c 2022-07-16 thomas static const struct got_error *
797 8a35f56c 2022-07-16 thomas gotweb_render_index(struct request *c)
798 8a35f56c 2022-07-16 thomas {
799 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
800 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
801 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
802 8a35f56c 2022-07-16 thomas struct querystring *qs = t->qs;
803 8a35f56c 2022-07-16 thomas struct repo_dir *repo_dir = NULL;
804 8a35f56c 2022-07-16 thomas DIR *d;
805 f530ab0e 2022-09-02 thomas struct dirent **sd_dent = NULL;
806 8a35f56c 2022-07-16 thomas unsigned int d_cnt, d_i, d_disp = 0;
807 24240f6a 2022-11-23 thomas unsigned int d_skipped = 0;
808 e7e5fa49 2022-12-30 thomas int type;
809 8a35f56c 2022-07-16 thomas
810 8a35f56c 2022-07-16 thomas d = opendir(srv->repos_path);
811 8a35f56c 2022-07-16 thomas if (d == NULL) {
812 8a35f56c 2022-07-16 thomas error = got_error_from_errno2("opendir", srv->repos_path);
813 8a35f56c 2022-07-16 thomas return error;
814 8a35f56c 2022-07-16 thomas }
815 8a35f56c 2022-07-16 thomas
816 8a35f56c 2022-07-16 thomas d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
817 8a35f56c 2022-07-16 thomas if (d_cnt == -1) {
818 f530ab0e 2022-09-02 thomas sd_dent = NULL;
819 8a35f56c 2022-07-16 thomas error = got_error_from_errno2("scandir", srv->repos_path);
820 8a35f56c 2022-07-16 thomas goto done;
821 8a35f56c 2022-07-16 thomas }
822 8a35f56c 2022-07-16 thomas
823 e7e5fa49 2022-12-30 thomas if (gotweb_render_repo_table_hdr(c->tp) == -1)
824 8a35f56c 2022-07-16 thomas goto done;
825 79393471 2022-08-27 thomas
826 8a35f56c 2022-07-16 thomas for (d_i = 0; d_i < d_cnt; d_i++) {
827 57e88d7c 2022-11-23 thomas if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
828 57e88d7c 2022-11-23 thomas break;
829 8a35f56c 2022-07-16 thomas
830 8a35f56c 2022-07-16 thomas if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
831 24240f6a 2022-11-23 thomas strcmp(sd_dent[d_i]->d_name, "..") == 0) {
832 24240f6a 2022-11-23 thomas d_skipped++;
833 24240f6a 2022-11-23 thomas continue;
834 24240f6a 2022-11-23 thomas }
835 24240f6a 2022-11-23 thomas
836 24240f6a 2022-11-23 thomas error = got_path_dirent_type(&type, srv->repos_path,
837 24240f6a 2022-11-23 thomas sd_dent[d_i]);
838 24240f6a 2022-11-23 thomas if (error)
839 24240f6a 2022-11-23 thomas goto done;
840 24240f6a 2022-11-23 thomas if (type != DT_DIR) {
841 24240f6a 2022-11-23 thomas d_skipped++;
842 8a35f56c 2022-07-16 thomas continue;
843 24240f6a 2022-11-23 thomas }
844 8a35f56c 2022-07-16 thomas
845 8a35f56c 2022-07-16 thomas if (qs->index_page > 0 && (qs->index_page *
846 8a35f56c 2022-07-16 thomas srv->max_repos_display) > t->prev_disp) {
847 8a35f56c 2022-07-16 thomas t->prev_disp++;
848 8a35f56c 2022-07-16 thomas continue;
849 8a35f56c 2022-07-16 thomas }
850 8a35f56c 2022-07-16 thomas
851 8a35f56c 2022-07-16 thomas error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
852 8a35f56c 2022-07-16 thomas if (error)
853 8a35f56c 2022-07-16 thomas goto done;
854 8a35f56c 2022-07-16 thomas
855 8a35f56c 2022-07-16 thomas error = gotweb_load_got_path(c, repo_dir);
856 8a35f56c 2022-07-16 thomas if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
857 8a35f56c 2022-07-16 thomas error = NULL;
858 8a35f56c 2022-07-16 thomas gotweb_free_repo_dir(repo_dir);
859 8a35f56c 2022-07-16 thomas repo_dir = NULL;
860 24240f6a 2022-11-23 thomas d_skipped++;
861 8a35f56c 2022-07-16 thomas continue;
862 8a35f56c 2022-07-16 thomas }
863 24240f6a 2022-11-23 thomas if (error && error->code != GOT_ERR_LONELY_PACKIDX)
864 24240f6a 2022-11-23 thomas goto done;
865 24240f6a 2022-11-23 thomas
866 8a35f56c 2022-07-16 thomas d_disp++;
867 8a35f56c 2022-07-16 thomas t->prev_disp++;
868 8a35f56c 2022-07-16 thomas
869 e7e5fa49 2022-12-30 thomas if (gotweb_render_repo_fragment(c->tp, repo_dir) == -1)
870 55144267 2022-09-07 thomas goto done;
871 55144267 2022-09-07 thomas
872 8a35f56c 2022-07-16 thomas gotweb_free_repo_dir(repo_dir);
873 8a35f56c 2022-07-16 thomas repo_dir = NULL;
874 8a35f56c 2022-07-16 thomas t->next_disp++;
875 8a35f56c 2022-07-16 thomas if (d_disp == srv->max_repos_display)
876 8a35f56c 2022-07-16 thomas break;
877 8a35f56c 2022-07-16 thomas }
878 24240f6a 2022-11-23 thomas t->repos_total = d_cnt - d_skipped;
879 24240f6a 2022-11-23 thomas
880 8a35f56c 2022-07-16 thomas if (srv->max_repos_display == 0)
881 79393471 2022-08-27 thomas goto done;
882 8a35f56c 2022-07-16 thomas if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
883 79393471 2022-08-27 thomas goto done;
884 8a35f56c 2022-07-16 thomas if (t->repos_total <= srv->max_repos ||
885 8a35f56c 2022-07-16 thomas t->repos_total <= srv->max_repos_display)
886 79393471 2022-08-27 thomas goto done;
887 8a35f56c 2022-07-16 thomas
888 2f4f0731 2022-12-30 thomas if (gotweb_render_navs(c->tp) == -1)
889 8a35f56c 2022-07-16 thomas goto done;
890 8a35f56c 2022-07-16 thomas done:
891 f530ab0e 2022-09-02 thomas if (sd_dent) {
892 f530ab0e 2022-09-02 thomas for (d_i = 0; d_i < d_cnt; d_i++)
893 f530ab0e 2022-09-02 thomas free(sd_dent[d_i]);
894 f530ab0e 2022-09-02 thomas free(sd_dent);
895 f530ab0e 2022-09-02 thomas }
896 8a35f56c 2022-07-16 thomas if (d != NULL && closedir(d) == EOF && error == NULL)
897 8a35f56c 2022-07-16 thomas error = got_error_from_errno("closedir");
898 8a35f56c 2022-07-16 thomas return error;
899 8a35f56c 2022-07-16 thomas }
900 8a35f56c 2022-07-16 thomas
901 8a35f56c 2022-07-16 thomas static const struct got_error *
902 8a35f56c 2022-07-16 thomas gotweb_render_blame(struct request *c)
903 8a35f56c 2022-07-16 thomas {
904 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
905 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
906 8a35f56c 2022-07-16 thomas struct repo_commit *rc = NULL;
907 255f4022 2022-08-27 thomas char *age = NULL, *msg = NULL;
908 79393471 2022-08-27 thomas int r;
909 8a35f56c 2022-07-16 thomas
910 8a35f56c 2022-07-16 thomas error = got_get_repo_commits(c, 1);
911 8a35f56c 2022-07-16 thomas if (error)
912 8a35f56c 2022-07-16 thomas return error;
913 8a35f56c 2022-07-16 thomas
914 8a35f56c 2022-07-16 thomas rc = TAILQ_FIRST(&t->repo_commits);
915 8a35f56c 2022-07-16 thomas
916 8a35f56c 2022-07-16 thomas error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
917 255f4022 2022-08-27 thomas if (error)
918 255f4022 2022-08-27 thomas goto done;
919 255f4022 2022-08-27 thomas error = gotweb_escape_html(&msg, rc->commit_msg);
920 8a35f56c 2022-07-16 thomas if (error)
921 8a35f56c 2022-07-16 thomas goto done;
922 8a35f56c 2022-07-16 thomas
923 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
924 79393471 2022-08-27 thomas "<div id='blame_title'>Blame</div>\n"
925 79393471 2022-08-27 thomas "</div>\n" /* #blame_title_wrapper */
926 79393471 2022-08-27 thomas "<div id='blame_content'>\n"
927 79393471 2022-08-27 thomas "<div id='blame_header_wrapper'>\n"
928 79393471 2022-08-27 thomas "<div id='blame_header'>\n"
929 79393471 2022-08-27 thomas "<div class='header_age_title'>Date:</div>\n"
930 79393471 2022-08-27 thomas "<div class='header_age'>%s</div>\n"
931 79393471 2022-08-27 thomas "<div id='header_commit_msg_title'>Message:</div>\n"
932 79393471 2022-08-27 thomas "<div id='header_commit_msg'>%s</div>\n"
933 79393471 2022-08-27 thomas "</div>\n" /* #blame_header */
934 79393471 2022-08-27 thomas "</div>\n" /* #blame_header_wrapper */
935 79393471 2022-08-27 thomas "<div class='dotted_line'></div>\n"
936 79393471 2022-08-27 thomas "<div id='blame'>\n",
937 2ad628aa 2022-08-31 thomas age,
938 255f4022 2022-08-27 thomas msg);
939 79393471 2022-08-27 thomas if (r == -1)
940 8a35f56c 2022-07-16 thomas goto done;
941 8a35f56c 2022-07-16 thomas
942 8a35f56c 2022-07-16 thomas error = got_output_file_blame(c);
943 8a35f56c 2022-07-16 thomas if (error)
944 8a35f56c 2022-07-16 thomas goto done;
945 8a35f56c 2022-07-16 thomas
946 79393471 2022-08-27 thomas fcgi_printf(c, "</div>\n" /* #blame */
947 79393471 2022-08-27 thomas "</div>\n"); /* #blame_content */
948 8a35f56c 2022-07-16 thomas done:
949 8a35f56c 2022-07-16 thomas free(age);
950 255f4022 2022-08-27 thomas free(msg);
951 8a35f56c 2022-07-16 thomas return error;
952 8a35f56c 2022-07-16 thomas }
953 8a35f56c 2022-07-16 thomas
954 8a35f56c 2022-07-16 thomas static const struct got_error *
955 8a35f56c 2022-07-16 thomas gotweb_render_branches(struct request *c)
956 8a35f56c 2022-07-16 thomas {
957 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
958 8a35f56c 2022-07-16 thomas struct got_reflist_head refs;
959 8a35f56c 2022-07-16 thomas struct got_reflist_entry *re;
960 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
961 8a35f56c 2022-07-16 thomas struct querystring *qs = t->qs;
962 8a35f56c 2022-07-16 thomas struct got_repository *repo = t->repo;
963 55144267 2022-09-07 thomas char *escaped_refname = NULL;
964 8a35f56c 2022-07-16 thomas char *age = NULL;
965 79393471 2022-08-27 thomas int r;
966 8a35f56c 2022-07-16 thomas
967 8a35f56c 2022-07-16 thomas TAILQ_INIT(&refs);
968 8a35f56c 2022-07-16 thomas
969 8a35f56c 2022-07-16 thomas error = got_ref_list(&refs, repo, "refs/heads",
970 8a35f56c 2022-07-16 thomas got_ref_cmp_by_name, NULL);
971 8a35f56c 2022-07-16 thomas if (error)
972 8a35f56c 2022-07-16 thomas goto done;
973 8a35f56c 2022-07-16 thomas
974 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
975 79393471 2022-08-27 thomas "<div id='branches_title'>Branches</div>\n"
976 79393471 2022-08-27 thomas "</div>\n" /* #branches_title_wrapper */
977 79393471 2022-08-27 thomas "<div id='branches_content'>\n");
978 79393471 2022-08-27 thomas if (r == -1)
979 8a35f56c 2022-07-16 thomas goto done;
980 8a35f56c 2022-07-16 thomas
981 8a35f56c 2022-07-16 thomas TAILQ_FOREACH(re, &refs, entry) {
982 255f4022 2022-08-27 thomas const char *refname = NULL;
983 8a35f56c 2022-07-16 thomas
984 8a35f56c 2022-07-16 thomas if (got_ref_is_symbolic(re->ref))
985 8a35f56c 2022-07-16 thomas continue;
986 8a35f56c 2022-07-16 thomas
987 255f4022 2022-08-27 thomas refname = got_ref_get_name(re->ref);
988 8a35f56c 2022-07-16 thomas if (refname == NULL) {
989 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
990 8a35f56c 2022-07-16 thomas goto done;
991 8a35f56c 2022-07-16 thomas }
992 8a35f56c 2022-07-16 thomas if (strncmp(refname, "refs/heads/", 11) != 0)
993 8a35f56c 2022-07-16 thomas continue;
994 8a35f56c 2022-07-16 thomas
995 6c7f10f7 2022-11-23 thomas error = got_get_repo_age(&age, c, refname, TM_DIFF);
996 8a35f56c 2022-07-16 thomas if (error)
997 8a35f56c 2022-07-16 thomas goto done;
998 8a35f56c 2022-07-16 thomas
999 8a35f56c 2022-07-16 thomas if (strncmp(refname, "refs/heads/", 11) == 0)
1000 8a35f56c 2022-07-16 thomas refname += 11;
1001 255f4022 2022-08-27 thomas error = gotweb_escape_html(&escaped_refname, refname);
1002 255f4022 2022-08-27 thomas if (error)
1003 255f4022 2022-08-27 thomas goto done;
1004 8a35f56c 2022-07-16 thomas
1005 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
1006 79393471 2022-08-27 thomas "<div class='branches_age'>%s</div>\n"
1007 79393471 2022-08-27 thomas "<div class='branches_space'>&nbsp;</div>\n"
1008 55144267 2022-09-07 thomas "<div class='branch'>", age);
1009 55144267 2022-09-07 thomas if (r == -1)
1010 55144267 2022-09-07 thomas goto done;
1011 55144267 2022-09-07 thomas
1012 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1013 55144267 2022-09-07 thomas .action = SUMMARY,
1014 55144267 2022-09-07 thomas .index_page = -1,
1015 55144267 2022-09-07 thomas .page = -1,
1016 55144267 2022-09-07 thomas .path = qs->path,
1017 55144267 2022-09-07 thomas .headref = refname,
1018 55144267 2022-09-07 thomas }, "%s", escaped_refname);
1019 55144267 2022-09-07 thomas if (r == -1)
1020 55144267 2022-09-07 thomas goto done;
1021 55144267 2022-09-07 thomas
1022 55144267 2022-09-07 thomas if (fcgi_printf(c, "</div>\n" /* .branch */
1023 79393471 2022-08-27 thomas "<div class='navs_wrapper'>\n"
1024 55144267 2022-09-07 thomas "<div class='navs'>") == -1)
1025 55144267 2022-09-07 thomas goto done;
1026 55144267 2022-09-07 thomas
1027 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1028 55144267 2022-09-07 thomas .action = SUMMARY,
1029 55144267 2022-09-07 thomas .index_page = -1,
1030 55144267 2022-09-07 thomas .page = -1,
1031 55144267 2022-09-07 thomas .path = qs->path,
1032 55144267 2022-09-07 thomas .headref = refname,
1033 55144267 2022-09-07 thomas }, "summary");
1034 55144267 2022-09-07 thomas if (r == -1)
1035 55144267 2022-09-07 thomas goto done;
1036 55144267 2022-09-07 thomas
1037 55144267 2022-09-07 thomas if (fcgi_printf(c, " | ") == -1)
1038 55144267 2022-09-07 thomas goto done;
1039 55144267 2022-09-07 thomas
1040 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1041 55144267 2022-09-07 thomas .action = BRIEFS,
1042 55144267 2022-09-07 thomas .index_page = -1,
1043 55144267 2022-09-07 thomas .page = -1,
1044 55144267 2022-09-07 thomas .path = qs->path,
1045 55144267 2022-09-07 thomas .headref = refname,
1046 55144267 2022-09-07 thomas }, "commit briefs");
1047 55144267 2022-09-07 thomas if (r == -1)
1048 55144267 2022-09-07 thomas goto done;
1049 55144267 2022-09-07 thomas
1050 55144267 2022-09-07 thomas if (fcgi_printf(c, " | ") == -1)
1051 55144267 2022-09-07 thomas goto done;
1052 55144267 2022-09-07 thomas
1053 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1054 55144267 2022-09-07 thomas .action = COMMITS,
1055 55144267 2022-09-07 thomas .index_page = -1,
1056 55144267 2022-09-07 thomas .page = -1,
1057 55144267 2022-09-07 thomas .path = qs->path,
1058 55144267 2022-09-07 thomas .headref = refname,
1059 55144267 2022-09-07 thomas }, "commits");
1060 55144267 2022-09-07 thomas if (r == -1)
1061 55144267 2022-09-07 thomas goto done;
1062 55144267 2022-09-07 thomas
1063 55144267 2022-09-07 thomas r = fcgi_printf(c, "</div>\n" /* .navs */
1064 55144267 2022-09-07 thomas "</div>\n" /* .navs_wrapper */
1065 79393471 2022-08-27 thomas "<div class='dotted_line'></div>\n"
1066 55144267 2022-09-07 thomas "</div>\n"); /* .branches_wrapper */
1067 79393471 2022-08-27 thomas if (r == -1)
1068 8a35f56c 2022-07-16 thomas goto done;
1069 8a35f56c 2022-07-16 thomas
1070 8a35f56c 2022-07-16 thomas free(age);
1071 8a35f56c 2022-07-16 thomas age = NULL;
1072 55144267 2022-09-07 thomas free(escaped_refname);
1073 55144267 2022-09-07 thomas escaped_refname = NULL;
1074 8a35f56c 2022-07-16 thomas }
1075 79393471 2022-08-27 thomas fcgi_printf(c, "</div>\n"); /* #branches_content */
1076 8a35f56c 2022-07-16 thomas done:
1077 f95e65a1 2022-09-02 thomas free(age);
1078 55144267 2022-09-07 thomas free(escaped_refname);
1079 f95e65a1 2022-09-02 thomas got_ref_list_free(&refs);
1080 8a35f56c 2022-07-16 thomas return error;
1081 8a35f56c 2022-07-16 thomas }
1082 8a35f56c 2022-07-16 thomas
1083 8a35f56c 2022-07-16 thomas static const struct got_error *
1084 8a35f56c 2022-07-16 thomas gotweb_render_tree(struct request *c)
1085 8a35f56c 2022-07-16 thomas {
1086 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1087 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1088 8a35f56c 2022-07-16 thomas struct repo_commit *rc = NULL;
1089 255f4022 2022-08-27 thomas char *age = NULL, *msg = NULL;
1090 79393471 2022-08-27 thomas int r;
1091 8a35f56c 2022-07-16 thomas
1092 8a35f56c 2022-07-16 thomas error = got_get_repo_commits(c, 1);
1093 8a35f56c 2022-07-16 thomas if (error)
1094 8a35f56c 2022-07-16 thomas return error;
1095 8a35f56c 2022-07-16 thomas
1096 8a35f56c 2022-07-16 thomas rc = TAILQ_FIRST(&t->repo_commits);
1097 8a35f56c 2022-07-16 thomas
1098 8a35f56c 2022-07-16 thomas error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1099 8a35f56c 2022-07-16 thomas if (error)
1100 8a35f56c 2022-07-16 thomas goto done;
1101 8a35f56c 2022-07-16 thomas
1102 255f4022 2022-08-27 thomas error = gotweb_escape_html(&msg, rc->commit_msg);
1103 255f4022 2022-08-27 thomas if (error)
1104 255f4022 2022-08-27 thomas goto done;
1105 255f4022 2022-08-27 thomas
1106 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div id='tree_title_wrapper'>\n"
1107 79393471 2022-08-27 thomas "<div id='tree_title'>Tree</div>\n"
1108 79393471 2022-08-27 thomas "</div>\n" /* #tree_title_wrapper */
1109 79393471 2022-08-27 thomas "<div id='tree_content'>\n"
1110 79393471 2022-08-27 thomas "<div id='tree_header_wrapper'>\n"
1111 79393471 2022-08-27 thomas "<div id='tree_header'>\n"
1112 79393471 2022-08-27 thomas "<div id='header_tree_title'>Tree:</div>\n"
1113 79393471 2022-08-27 thomas "<div id='header_tree'>%s</div>\n"
1114 79393471 2022-08-27 thomas "<div class='header_age_title'>Date:</div>\n"
1115 79393471 2022-08-27 thomas "<div class='header_age'>%s</div>\n"
1116 79393471 2022-08-27 thomas "<div id='header_commit_msg_title'>Message:</div>\n"
1117 79393471 2022-08-27 thomas "<div id='header_commit_msg'>%s</div>\n"
1118 79393471 2022-08-27 thomas "</div>\n" /* #tree_header */
1119 79393471 2022-08-27 thomas "</div>\n" /* #tree_header_wrapper */
1120 79393471 2022-08-27 thomas "<div class='dotted_line'></div>\n"
1121 79393471 2022-08-27 thomas "<div id='tree'>\n",
1122 79393471 2022-08-27 thomas rc->tree_id,
1123 2ad628aa 2022-08-31 thomas age,
1124 255f4022 2022-08-27 thomas msg);
1125 79393471 2022-08-27 thomas if (r == -1)
1126 8a35f56c 2022-07-16 thomas goto done;
1127 8a35f56c 2022-07-16 thomas
1128 8a35f56c 2022-07-16 thomas error = got_output_repo_tree(c);
1129 8a35f56c 2022-07-16 thomas if (error)
1130 8a35f56c 2022-07-16 thomas goto done;
1131 8a35f56c 2022-07-16 thomas
1132 79393471 2022-08-27 thomas fcgi_printf(c, "</div>\n"); /* #tree */
1133 79393471 2022-08-27 thomas fcgi_printf(c, "</div>\n"); /* #tree_content */
1134 8a35f56c 2022-07-16 thomas done:
1135 2ad628aa 2022-08-31 thomas free(age);
1136 255f4022 2022-08-27 thomas free(msg);
1137 8a35f56c 2022-07-16 thomas return error;
1138 8a35f56c 2022-07-16 thomas }
1139 8a35f56c 2022-07-16 thomas
1140 8a35f56c 2022-07-16 thomas static const struct got_error *
1141 8a35f56c 2022-07-16 thomas gotweb_render_diff(struct request *c)
1142 8a35f56c 2022-07-16 thomas {
1143 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1144 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1145 8a35f56c 2022-07-16 thomas struct repo_commit *rc = NULL;
1146 255f4022 2022-08-27 thomas char *age = NULL, *author = NULL, *msg = NULL;
1147 79393471 2022-08-27 thomas int r;
1148 8a35f56c 2022-07-16 thomas
1149 8a35f56c 2022-07-16 thomas error = got_get_repo_commits(c, 1);
1150 8a35f56c 2022-07-16 thomas if (error)
1151 8a35f56c 2022-07-16 thomas return error;
1152 8a35f56c 2022-07-16 thomas
1153 8a35f56c 2022-07-16 thomas rc = TAILQ_FIRST(&t->repo_commits);
1154 8a35f56c 2022-07-16 thomas
1155 8a35f56c 2022-07-16 thomas error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1156 8a35f56c 2022-07-16 thomas if (error)
1157 8a35f56c 2022-07-16 thomas goto done;
1158 8a35f56c 2022-07-16 thomas error = gotweb_escape_html(&author, rc->author);
1159 8a35f56c 2022-07-16 thomas if (error)
1160 8a35f56c 2022-07-16 thomas goto done;
1161 255f4022 2022-08-27 thomas error = gotweb_escape_html(&msg, rc->commit_msg);
1162 255f4022 2022-08-27 thomas if (error)
1163 255f4022 2022-08-27 thomas goto done;
1164 8a35f56c 2022-07-16 thomas
1165 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div id='diff_title_wrapper'>\n"
1166 79393471 2022-08-27 thomas "<div id='diff_title'>Commit Diff</div>\n"
1167 79393471 2022-08-27 thomas "</div>\n" /* #diff_title_wrapper */
1168 79393471 2022-08-27 thomas "<div id='diff_content'>\n"
1169 79393471 2022-08-27 thomas "<div id='diff_header_wrapper'>\n"
1170 79393471 2022-08-27 thomas "<div id='diff_header'>\n"
1171 79393471 2022-08-27 thomas "<div id='header_diff_title'>Diff:</div>\n"
1172 79393471 2022-08-27 thomas "<div id='header_diff'>%s<br />%s</div>\n"
1173 79393471 2022-08-27 thomas "<div class='header_commit_title'>Commit:</div>\n"
1174 79393471 2022-08-27 thomas "<div class='header_commit'>%s</div>\n"
1175 79393471 2022-08-27 thomas "<div id='header_tree_title'>Tree:</div>\n"
1176 79393471 2022-08-27 thomas "<div id='header_tree'>%s</div>\n"
1177 79393471 2022-08-27 thomas "<div class='header_author_title'>Author:</div>\n"
1178 79393471 2022-08-27 thomas "<div class='header_author'>%s</div>\n"
1179 79393471 2022-08-27 thomas "<div class='header_age_title'>Date:</div>\n"
1180 79393471 2022-08-27 thomas "<div class='header_age'>%s</div>\n"
1181 79393471 2022-08-27 thomas "<div id='header_commit_msg_title'>Message:</div>\n"
1182 79393471 2022-08-27 thomas "<div id='header_commit_msg'>%s</div>\n"
1183 79393471 2022-08-27 thomas "</div>\n" /* #diff_header */
1184 79393471 2022-08-27 thomas "</div>\n" /* #diff_header_wrapper */
1185 79393471 2022-08-27 thomas "<div class='dotted_line'></div>\n"
1186 79393471 2022-08-27 thomas "<div id='diff'>\n",
1187 79393471 2022-08-27 thomas rc->parent_id, rc->commit_id,
1188 79393471 2022-08-27 thomas rc->commit_id,
1189 79393471 2022-08-27 thomas rc->tree_id,
1190 255f4022 2022-08-27 thomas author,
1191 2ad628aa 2022-08-31 thomas age,
1192 255f4022 2022-08-27 thomas msg);
1193 79393471 2022-08-27 thomas if (r == -1)
1194 8a35f56c 2022-07-16 thomas goto done;
1195 8a35f56c 2022-07-16 thomas
1196 8a35f56c 2022-07-16 thomas error = got_output_repo_diff(c);
1197 8a35f56c 2022-07-16 thomas if (error)
1198 8a35f56c 2022-07-16 thomas goto done;
1199 8a35f56c 2022-07-16 thomas
1200 79393471 2022-08-27 thomas fcgi_printf(c, "</div>\n"); /* #diff */
1201 79393471 2022-08-27 thomas fcgi_printf(c, "</div>\n"); /* #diff_content */
1202 8a35f56c 2022-07-16 thomas done:
1203 8a35f56c 2022-07-16 thomas free(age);
1204 8a35f56c 2022-07-16 thomas free(author);
1205 255f4022 2022-08-27 thomas free(msg);
1206 8a35f56c 2022-07-16 thomas return error;
1207 8a35f56c 2022-07-16 thomas }
1208 8a35f56c 2022-07-16 thomas
1209 8a35f56c 2022-07-16 thomas static const struct got_error *
1210 8a35f56c 2022-07-16 thomas gotweb_render_summary(struct request *c)
1211 8a35f56c 2022-07-16 thomas {
1212 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1213 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1214 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
1215 79393471 2022-08-27 thomas int r;
1216 8a35f56c 2022-07-16 thomas
1217 79393471 2022-08-27 thomas if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1218 8a35f56c 2022-07-16 thomas goto done;
1219 8a35f56c 2022-07-16 thomas
1220 79393471 2022-08-27 thomas if (srv->show_repo_description) {
1221 79393471 2022-08-27 thomas r = fcgi_printf(c,
1222 79393471 2022-08-27 thomas "<div id='description_title'>Description:</div>\n"
1223 79393471 2022-08-27 thomas "<div id='description'>%s</div>\n",
1224 ddf2e5c2 2022-08-27 thomas t->repo_dir->description ? t->repo_dir->description : "");
1225 79393471 2022-08-27 thomas if (r == -1)
1226 79393471 2022-08-27 thomas goto done;
1227 79393471 2022-08-27 thomas }
1228 8a35f56c 2022-07-16 thomas
1229 79393471 2022-08-27 thomas if (srv->show_repo_owner) {
1230 79393471 2022-08-27 thomas r = fcgi_printf(c,
1231 79393471 2022-08-27 thomas "<div id='repo_owner_title'>Owner:</div>\n"
1232 79393471 2022-08-27 thomas "<div id='repo_owner'>%s</div>\n",
1233 ddf2e5c2 2022-08-27 thomas t->repo_dir->owner ? t->repo_dir->owner : "");
1234 79393471 2022-08-27 thomas if (r == -1)
1235 79393471 2022-08-27 thomas goto done;
1236 79393471 2022-08-27 thomas }
1237 8a35f56c 2022-07-16 thomas
1238 79393471 2022-08-27 thomas if (srv->show_repo_age) {
1239 79393471 2022-08-27 thomas r = fcgi_printf(c,
1240 79393471 2022-08-27 thomas "<div id='last_change_title'>Last Change:</div>\n"
1241 79393471 2022-08-27 thomas "<div id='last_change'>%s</div>\n",
1242 79393471 2022-08-27 thomas t->repo_dir->age);
1243 79393471 2022-08-27 thomas if (r == -1)
1244 79393471 2022-08-27 thomas goto done;
1245 79393471 2022-08-27 thomas }
1246 8a35f56c 2022-07-16 thomas
1247 79393471 2022-08-27 thomas if (srv->show_repo_cloneurl) {
1248 79393471 2022-08-27 thomas r = fcgi_printf(c,
1249 79393471 2022-08-27 thomas "<div id='cloneurl_title'>Clone URL:</div>\n"
1250 79393471 2022-08-27 thomas "<div id='cloneurl'>%s</div>\n",
1251 79393471 2022-08-27 thomas t->repo_dir->url ? t->repo_dir->url : "");
1252 79393471 2022-08-27 thomas if (r == -1)
1253 79393471 2022-08-27 thomas goto done;
1254 79393471 2022-08-27 thomas }
1255 8a35f56c 2022-07-16 thomas
1256 79393471 2022-08-27 thomas r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1257 79393471 2022-08-27 thomas if (r == -1)
1258 8a35f56c 2022-07-16 thomas goto done;
1259 8a35f56c 2022-07-16 thomas
1260 e7e5fa49 2022-12-30 thomas if (gotweb_render_briefs(c->tp) == -1)
1261 8a35f56c 2022-07-16 thomas goto done;
1262 8a35f56c 2022-07-16 thomas
1263 8a35f56c 2022-07-16 thomas error = gotweb_render_tags(c);
1264 8a35f56c 2022-07-16 thomas if (error) {
1265 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
1266 8a35f56c 2022-07-16 thomas goto done;
1267 8a35f56c 2022-07-16 thomas }
1268 8a35f56c 2022-07-16 thomas
1269 8a35f56c 2022-07-16 thomas error = gotweb_render_branches(c);
1270 8a35f56c 2022-07-16 thomas if (error)
1271 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
1272 8a35f56c 2022-07-16 thomas done:
1273 8a35f56c 2022-07-16 thomas return error;
1274 8a35f56c 2022-07-16 thomas }
1275 8a35f56c 2022-07-16 thomas
1276 8a35f56c 2022-07-16 thomas static const struct got_error *
1277 8a35f56c 2022-07-16 thomas gotweb_render_tag(struct request *c)
1278 8a35f56c 2022-07-16 thomas {
1279 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1280 8a35f56c 2022-07-16 thomas struct repo_tag *rt = NULL;
1281 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1282 255f4022 2022-08-27 thomas char *tagname = NULL, *age = NULL, *author = NULL, *msg = NULL;
1283 8a35f56c 2022-07-16 thomas
1284 8a35f56c 2022-07-16 thomas error = got_get_repo_tags(c, 1);
1285 8a35f56c 2022-07-16 thomas if (error)
1286 8a35f56c 2022-07-16 thomas goto done;
1287 8a35f56c 2022-07-16 thomas
1288 8a35f56c 2022-07-16 thomas if (t->tag_count == 0) {
1289 8a35f56c 2022-07-16 thomas error = got_error_set_errno(GOT_ERR_BAD_OBJ_ID,
1290 8a35f56c 2022-07-16 thomas "bad commit id");
1291 8a35f56c 2022-07-16 thomas goto done;
1292 8a35f56c 2022-07-16 thomas }
1293 8a35f56c 2022-07-16 thomas
1294 8a35f56c 2022-07-16 thomas rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
1295 8a35f56c 2022-07-16 thomas
1296 8a35f56c 2022-07-16 thomas error = gotweb_get_time_str(&age, rt->tagger_time, TM_LONG);
1297 8a35f56c 2022-07-16 thomas if (error)
1298 8a35f56c 2022-07-16 thomas goto done;
1299 8a35f56c 2022-07-16 thomas error = gotweb_escape_html(&author, rt->tagger);
1300 8a35f56c 2022-07-16 thomas if (error)
1301 8a35f56c 2022-07-16 thomas goto done;
1302 255f4022 2022-08-27 thomas error = gotweb_escape_html(&msg, rt->commit_msg);
1303 255f4022 2022-08-27 thomas if (error)
1304 255f4022 2022-08-27 thomas goto done;
1305 8a35f56c 2022-07-16 thomas
1306 c8d0196f 2022-09-02 thomas tagname = rt->tag_name;
1307 c8d0196f 2022-09-02 thomas if (strncmp(tagname, "refs/", 5) == 0)
1308 c8d0196f 2022-09-02 thomas tagname += 5;
1309 c8d0196f 2022-09-02 thomas error = gotweb_escape_html(&tagname, tagname);
1310 255f4022 2022-08-27 thomas if (error)
1311 255f4022 2022-08-27 thomas goto done;
1312 8a35f56c 2022-07-16 thomas
1313 79393471 2022-08-27 thomas fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1314 79393471 2022-08-27 thomas "<div id='tags_title'>Tag</div>\n"
1315 79393471 2022-08-27 thomas "</div>\n" /* #tags_title_wrapper */
1316 79393471 2022-08-27 thomas "<div id='tags_content'>\n"
1317 79393471 2022-08-27 thomas "<div id='tag_header_wrapper'>\n"
1318 79393471 2022-08-27 thomas "<div id='tag_header'>\n"
1319 79393471 2022-08-27 thomas "<div class='header_commit_title'>Commit:</div>\n"
1320 79393471 2022-08-27 thomas "<div class='header_commit'>%s"
1321 79393471 2022-08-27 thomas " <span class='refs_str'>(%s)</span></div>\n"
1322 79393471 2022-08-27 thomas "<div class='header_author_title'>Tagger:</div>\n"
1323 79393471 2022-08-27 thomas "<div class='header_author'>%s</div>\n"
1324 79393471 2022-08-27 thomas "<div class='header_age_title'>Date:</div>\n"
1325 79393471 2022-08-27 thomas "<div class='header_age'>%s</div>\n"
1326 79393471 2022-08-27 thomas "<div id='header_commit_msg_title'>Message:</div>\n"
1327 79393471 2022-08-27 thomas "<div id='header_commit_msg'>%s</div>\n"
1328 79393471 2022-08-27 thomas "</div>\n" /* #tag_header */
1329 79393471 2022-08-27 thomas "<div class='dotted_line'></div>\n"
1330 79393471 2022-08-27 thomas "<div id='tag_commit'>\n%s</div>"
1331 80ca8b0f 2022-09-06 thomas "</div>" /* #tag_header_wrapper */
1332 80ca8b0f 2022-09-06 thomas "</div>", /* #tags_content */
1333 79393471 2022-08-27 thomas rt->commit_id,
1334 255f4022 2022-08-27 thomas tagname,
1335 255f4022 2022-08-27 thomas author,
1336 2ad628aa 2022-08-31 thomas age,
1337 255f4022 2022-08-27 thomas msg,
1338 79393471 2022-08-27 thomas rt->tag_commit);
1339 8a35f56c 2022-07-16 thomas
1340 8a35f56c 2022-07-16 thomas done:
1341 8a35f56c 2022-07-16 thomas free(age);
1342 8a35f56c 2022-07-16 thomas free(author);
1343 255f4022 2022-08-27 thomas free(msg);
1344 8a35f56c 2022-07-16 thomas return error;
1345 8a35f56c 2022-07-16 thomas }
1346 8a35f56c 2022-07-16 thomas
1347 8a35f56c 2022-07-16 thomas static const struct got_error *
1348 8a35f56c 2022-07-16 thomas gotweb_render_tags(struct request *c)
1349 8a35f56c 2022-07-16 thomas {
1350 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1351 8a35f56c 2022-07-16 thomas struct repo_tag *rt = NULL;
1352 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
1353 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1354 8a35f56c 2022-07-16 thomas struct querystring *qs = t->qs;
1355 8a35f56c 2022-07-16 thomas struct repo_dir *repo_dir = t->repo_dir;
1356 255f4022 2022-08-27 thomas char *age = NULL, *tagname = NULL, *msg = NULL, *newline;
1357 79393471 2022-08-27 thomas int r, commit_found = 0;
1358 8a35f56c 2022-07-16 thomas
1359 8a35f56c 2022-07-16 thomas if (qs->action == BRIEFS) {
1360 8a35f56c 2022-07-16 thomas qs->action = TAGS;
1361 8a35f56c 2022-07-16 thomas error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1362 8a35f56c 2022-07-16 thomas } else
1363 8a35f56c 2022-07-16 thomas error = got_get_repo_tags(c, srv->max_commits_display);
1364 8a35f56c 2022-07-16 thomas if (error)
1365 8a35f56c 2022-07-16 thomas goto done;
1366 8a35f56c 2022-07-16 thomas
1367 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1368 79393471 2022-08-27 thomas "<div id='tags_title'>Tags</div>\n"
1369 79393471 2022-08-27 thomas "</div>\n" /* #tags_title_wrapper */
1370 79393471 2022-08-27 thomas "<div id='tags_content'>\n");
1371 79393471 2022-08-27 thomas if (r == -1)
1372 8a35f56c 2022-07-16 thomas goto done;
1373 8a35f56c 2022-07-16 thomas
1374 8a35f56c 2022-07-16 thomas if (t->tag_count == 0) {
1375 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div id='err_content'>%s\n</div>\n",
1376 79393471 2022-08-27 thomas "This repository contains no tags");
1377 79393471 2022-08-27 thomas if (r == -1)
1378 8a35f56c 2022-07-16 thomas goto done;
1379 8a35f56c 2022-07-16 thomas }
1380 8a35f56c 2022-07-16 thomas
1381 8a35f56c 2022-07-16 thomas TAILQ_FOREACH(rt, &t->repo_tags, entry) {
1382 8a35f56c 2022-07-16 thomas if (commit_found == 0 && qs->commit != NULL) {
1383 8a35f56c 2022-07-16 thomas if (strcmp(qs->commit, rt->commit_id) != 0)
1384 8a35f56c 2022-07-16 thomas continue;
1385 8a35f56c 2022-07-16 thomas else
1386 8a35f56c 2022-07-16 thomas commit_found = 1;
1387 8a35f56c 2022-07-16 thomas }
1388 8a35f56c 2022-07-16 thomas error = gotweb_get_time_str(&age, rt->tagger_time, TM_DIFF);
1389 8a35f56c 2022-07-16 thomas if (error)
1390 8a35f56c 2022-07-16 thomas goto done;
1391 8a35f56c 2022-07-16 thomas
1392 c8d0196f 2022-09-02 thomas tagname = rt->tag_name;
1393 c8d0196f 2022-09-02 thomas if (strncmp(tagname, "refs/tags/", 10) == 0)
1394 c8d0196f 2022-09-02 thomas tagname += 10;
1395 c8d0196f 2022-09-02 thomas error = gotweb_escape_html(&tagname, tagname);
1396 255f4022 2022-08-27 thomas if (error)
1397 255f4022 2022-08-27 thomas goto done;
1398 8a35f56c 2022-07-16 thomas
1399 8a35f56c 2022-07-16 thomas if (rt->tag_commit != NULL) {
1400 8a35f56c 2022-07-16 thomas newline = strchr(rt->tag_commit, '\n');
1401 8a35f56c 2022-07-16 thomas if (newline)
1402 8a35f56c 2022-07-16 thomas *newline = '\0';
1403 255f4022 2022-08-27 thomas error = gotweb_escape_html(&msg, rt->tag_commit);
1404 255f4022 2022-08-27 thomas if (error)
1405 255f4022 2022-08-27 thomas goto done;
1406 8a35f56c 2022-07-16 thomas }
1407 8a35f56c 2022-07-16 thomas
1408 55144267 2022-09-07 thomas if (fcgi_printf(c, "<div class='tag_age'>%s</div>\n"
1409 79393471 2022-08-27 thomas "<div class='tag'>%s</div>\n"
1410 55144267 2022-09-07 thomas "<div class='tag_log'>", age, tagname) == -1)
1411 55144267 2022-09-07 thomas goto done;
1412 55144267 2022-09-07 thomas
1413 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1414 55144267 2022-09-07 thomas .action = TAG,
1415 55144267 2022-09-07 thomas .index_page = -1,
1416 55144267 2022-09-07 thomas .page = -1,
1417 55144267 2022-09-07 thomas .path = repo_dir->name,
1418 55144267 2022-09-07 thomas .commit = rt->commit_id,
1419 55144267 2022-09-07 thomas }, "%s", msg ? msg : "");
1420 55144267 2022-09-07 thomas if (r == -1)
1421 55144267 2022-09-07 thomas goto done;
1422 55144267 2022-09-07 thomas
1423 55144267 2022-09-07 thomas if (fcgi_printf(c, "</div>\n" /* .tag_log */
1424 79393471 2022-08-27 thomas "<div class='navs_wrapper'>\n"
1425 55144267 2022-09-07 thomas "<div class='navs'>") == -1)
1426 55144267 2022-09-07 thomas goto done;
1427 55144267 2022-09-07 thomas
1428 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1429 55144267 2022-09-07 thomas .action = TAG,
1430 55144267 2022-09-07 thomas .index_page = -1,
1431 55144267 2022-09-07 thomas .page = -1,
1432 55144267 2022-09-07 thomas .path = repo_dir->name,
1433 55144267 2022-09-07 thomas .commit = rt->commit_id,
1434 55144267 2022-09-07 thomas }, "tag");
1435 55144267 2022-09-07 thomas if (r == -1)
1436 55144267 2022-09-07 thomas goto done;
1437 55144267 2022-09-07 thomas
1438 55144267 2022-09-07 thomas if (fcgi_printf(c, " | ") == -1)
1439 55144267 2022-09-07 thomas goto done;
1440 55144267 2022-09-07 thomas
1441 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1442 55144267 2022-09-07 thomas .action = BRIEFS,
1443 55144267 2022-09-07 thomas .index_page = -1,
1444 55144267 2022-09-07 thomas .page = -1,
1445 55144267 2022-09-07 thomas .path = repo_dir->name,
1446 55144267 2022-09-07 thomas .commit = rt->commit_id,
1447 55144267 2022-09-07 thomas }, "commit briefs");
1448 55144267 2022-09-07 thomas if (r == -1)
1449 55144267 2022-09-07 thomas goto done;
1450 55144267 2022-09-07 thomas
1451 55144267 2022-09-07 thomas if (fcgi_printf(c, " | ") == -1)
1452 55144267 2022-09-07 thomas goto done;
1453 55144267 2022-09-07 thomas
1454 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1455 55144267 2022-09-07 thomas .action = COMMITS,
1456 55144267 2022-09-07 thomas .index_page = -1,
1457 55144267 2022-09-07 thomas .page = -1,
1458 55144267 2022-09-07 thomas .path = repo_dir->name,
1459 55144267 2022-09-07 thomas .commit = rt->commit_id,
1460 55144267 2022-09-07 thomas }, "commits");
1461 55144267 2022-09-07 thomas if (r == -1)
1462 55144267 2022-09-07 thomas goto done;
1463 55144267 2022-09-07 thomas
1464 55144267 2022-09-07 thomas r = fcgi_printf(c,
1465 79393471 2022-08-27 thomas "</div>\n" /* .navs */
1466 79393471 2022-08-27 thomas "</div>\n" /* .navs_wrapper */
1467 55144267 2022-09-07 thomas "<div class='dotted_line'></div>\n");
1468 79393471 2022-08-27 thomas if (r == -1)
1469 8a35f56c 2022-07-16 thomas goto done;
1470 8a35f56c 2022-07-16 thomas
1471 8a35f56c 2022-07-16 thomas free(age);
1472 8a35f56c 2022-07-16 thomas age = NULL;
1473 255f4022 2022-08-27 thomas free(tagname);
1474 255f4022 2022-08-27 thomas tagname = NULL;
1475 255f4022 2022-08-27 thomas free(msg);
1476 255f4022 2022-08-27 thomas msg = NULL;
1477 8a35f56c 2022-07-16 thomas }
1478 8a35f56c 2022-07-16 thomas if (t->next_id || t->prev_id) {
1479 2f4f0731 2022-12-30 thomas if (gotweb_render_navs(c->tp) == -1)
1480 8a35f56c 2022-07-16 thomas goto done;
1481 8a35f56c 2022-07-16 thomas }
1482 79393471 2022-08-27 thomas fcgi_printf(c, "</div>\n"); /* #tags_content */
1483 8a35f56c 2022-07-16 thomas done:
1484 8a35f56c 2022-07-16 thomas free(age);
1485 255f4022 2022-08-27 thomas free(tagname);
1486 255f4022 2022-08-27 thomas free(msg);
1487 8a35f56c 2022-07-16 thomas return error;
1488 8a35f56c 2022-07-16 thomas }
1489 8a35f56c 2022-07-16 thomas
1490 8a35f56c 2022-07-16 thomas const struct got_error *
1491 8a35f56c 2022-07-16 thomas gotweb_escape_html(char **escaped_html, const char *orig_html)
1492 8a35f56c 2022-07-16 thomas {
1493 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1494 8a35f56c 2022-07-16 thomas struct escape_pair {
1495 8a35f56c 2022-07-16 thomas char c;
1496 8a35f56c 2022-07-16 thomas const char *s;
1497 8a35f56c 2022-07-16 thomas } esc[] = {
1498 8a35f56c 2022-07-16 thomas { '>', "&gt;" },
1499 8a35f56c 2022-07-16 thomas { '<', "&lt;" },
1500 8a35f56c 2022-07-16 thomas { '&', "&amp;" },
1501 8a35f56c 2022-07-16 thomas { '"', "&quot;" },
1502 8a35f56c 2022-07-16 thomas { '\'', "&apos;" },
1503 8a35f56c 2022-07-16 thomas { '\n', "<br />" },
1504 8a35f56c 2022-07-16 thomas };
1505 8a35f56c 2022-07-16 thomas size_t orig_len, len;
1506 8a35f56c 2022-07-16 thomas int i, j, x;
1507 8a35f56c 2022-07-16 thomas
1508 8a35f56c 2022-07-16 thomas orig_len = strlen(orig_html);
1509 8a35f56c 2022-07-16 thomas len = orig_len;
1510 8a35f56c 2022-07-16 thomas for (i = 0; i < orig_len; i++) {
1511 8a35f56c 2022-07-16 thomas for (j = 0; j < nitems(esc); j++) {
1512 8a35f56c 2022-07-16 thomas if (orig_html[i] != esc[j].c)
1513 8a35f56c 2022-07-16 thomas continue;
1514 8a35f56c 2022-07-16 thomas len += strlen(esc[j].s) - 1 /* escaped char */;
1515 8a35f56c 2022-07-16 thomas }
1516 8a35f56c 2022-07-16 thomas }
1517 8a35f56c 2022-07-16 thomas
1518 8a35f56c 2022-07-16 thomas *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
1519 8a35f56c 2022-07-16 thomas if (*escaped_html == NULL)
1520 8a35f56c 2022-07-16 thomas return got_error_from_errno("calloc");
1521 8a35f56c 2022-07-16 thomas
1522 8a35f56c 2022-07-16 thomas x = 0;
1523 8a35f56c 2022-07-16 thomas for (i = 0; i < orig_len; i++) {
1524 8a35f56c 2022-07-16 thomas int escaped = 0;
1525 8a35f56c 2022-07-16 thomas for (j = 0; j < nitems(esc); j++) {
1526 8a35f56c 2022-07-16 thomas if (orig_html[i] != esc[j].c)
1527 8a35f56c 2022-07-16 thomas continue;
1528 8a35f56c 2022-07-16 thomas
1529 8a35f56c 2022-07-16 thomas if (strlcat(*escaped_html, esc[j].s, len + 1)
1530 8a35f56c 2022-07-16 thomas >= len + 1) {
1531 8a35f56c 2022-07-16 thomas error = got_error(GOT_ERR_NO_SPACE);
1532 8a35f56c 2022-07-16 thomas goto done;
1533 8a35f56c 2022-07-16 thomas }
1534 8a35f56c 2022-07-16 thomas x += strlen(esc[j].s);
1535 8a35f56c 2022-07-16 thomas escaped = 1;
1536 8a35f56c 2022-07-16 thomas break;
1537 8a35f56c 2022-07-16 thomas }
1538 8a35f56c 2022-07-16 thomas if (!escaped) {
1539 8a35f56c 2022-07-16 thomas (*escaped_html)[x] = orig_html[i];
1540 8a35f56c 2022-07-16 thomas x++;
1541 8a35f56c 2022-07-16 thomas }
1542 8a35f56c 2022-07-16 thomas }
1543 8a35f56c 2022-07-16 thomas done:
1544 8a35f56c 2022-07-16 thomas if (error) {
1545 8a35f56c 2022-07-16 thomas free(*escaped_html);
1546 8a35f56c 2022-07-16 thomas *escaped_html = NULL;
1547 8a35f56c 2022-07-16 thomas } else {
1548 8a35f56c 2022-07-16 thomas (*escaped_html)[x] = '\0';
1549 8a35f56c 2022-07-16 thomas }
1550 8a35f56c 2022-07-16 thomas
1551 8a35f56c 2022-07-16 thomas return error;
1552 8a35f56c 2022-07-16 thomas }
1553 8a35f56c 2022-07-16 thomas
1554 55144267 2022-09-07 thomas static inline int
1555 55144267 2022-09-07 thomas should_urlencode(int c)
1556 55144267 2022-09-07 thomas {
1557 55144267 2022-09-07 thomas if (c <= ' ' || c >= 127)
1558 55144267 2022-09-07 thomas return 1;
1559 55144267 2022-09-07 thomas
1560 55144267 2022-09-07 thomas switch (c) {
1561 55144267 2022-09-07 thomas /* gen-delim */
1562 55144267 2022-09-07 thomas case ':':
1563 55144267 2022-09-07 thomas case '/':
1564 55144267 2022-09-07 thomas case '?':
1565 55144267 2022-09-07 thomas case '#':
1566 55144267 2022-09-07 thomas case '[':
1567 55144267 2022-09-07 thomas case ']':
1568 55144267 2022-09-07 thomas case '@':
1569 55144267 2022-09-07 thomas /* sub-delims */
1570 55144267 2022-09-07 thomas case '!':
1571 55144267 2022-09-07 thomas case '$':
1572 55144267 2022-09-07 thomas case '&':
1573 55144267 2022-09-07 thomas case '\'':
1574 55144267 2022-09-07 thomas case '(':
1575 55144267 2022-09-07 thomas case ')':
1576 55144267 2022-09-07 thomas case '*':
1577 55144267 2022-09-07 thomas case '+':
1578 55144267 2022-09-07 thomas case ',':
1579 55144267 2022-09-07 thomas case ';':
1580 55144267 2022-09-07 thomas case '=':
1581 55144267 2022-09-07 thomas return 1;
1582 55144267 2022-09-07 thomas default:
1583 55144267 2022-09-07 thomas return 0;
1584 55144267 2022-09-07 thomas }
1585 55144267 2022-09-07 thomas }
1586 55144267 2022-09-07 thomas
1587 55144267 2022-09-07 thomas static char *
1588 55144267 2022-09-07 thomas gotweb_urlencode(const char *str)
1589 55144267 2022-09-07 thomas {
1590 55144267 2022-09-07 thomas const char *s;
1591 55144267 2022-09-07 thomas char *escaped;
1592 55144267 2022-09-07 thomas size_t i, len;
1593 55144267 2022-09-07 thomas int a, b;
1594 55144267 2022-09-07 thomas
1595 55144267 2022-09-07 thomas len = 0;
1596 55144267 2022-09-07 thomas for (s = str; *s; ++s) {
1597 55144267 2022-09-07 thomas len++;
1598 55144267 2022-09-07 thomas if (should_urlencode(*s))
1599 55144267 2022-09-07 thomas len += 2;
1600 55144267 2022-09-07 thomas }
1601 55144267 2022-09-07 thomas
1602 55144267 2022-09-07 thomas escaped = calloc(1, len + 1);
1603 55144267 2022-09-07 thomas if (escaped == NULL)
1604 55144267 2022-09-07 thomas return NULL;
1605 55144267 2022-09-07 thomas
1606 55144267 2022-09-07 thomas i = 0;
1607 55144267 2022-09-07 thomas for (s = str; *s; ++s) {
1608 55144267 2022-09-07 thomas if (should_urlencode(*s)) {
1609 55144267 2022-09-07 thomas a = (*s & 0xF0) >> 4;
1610 55144267 2022-09-07 thomas b = (*s & 0x0F);
1611 55144267 2022-09-07 thomas
1612 55144267 2022-09-07 thomas escaped[i++] = '%';
1613 55144267 2022-09-07 thomas escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
1614 55144267 2022-09-07 thomas escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
1615 55144267 2022-09-07 thomas } else
1616 55144267 2022-09-07 thomas escaped[i++] = *s;
1617 55144267 2022-09-07 thomas }
1618 55144267 2022-09-07 thomas
1619 55144267 2022-09-07 thomas return escaped;
1620 55144267 2022-09-07 thomas }
1621 55144267 2022-09-07 thomas
1622 e7e5fa49 2022-12-30 thomas const char *
1623 e7e5fa49 2022-12-30 thomas gotweb_action_name(int action)
1624 55144267 2022-09-07 thomas {
1625 55144267 2022-09-07 thomas switch (action) {
1626 55144267 2022-09-07 thomas case BLAME:
1627 55144267 2022-09-07 thomas return "blame";
1628 55144267 2022-09-07 thomas case BLOB:
1629 55144267 2022-09-07 thomas return "blob";
1630 55144267 2022-09-07 thomas case BRIEFS:
1631 55144267 2022-09-07 thomas return "briefs";
1632 55144267 2022-09-07 thomas case COMMITS:
1633 55144267 2022-09-07 thomas return "commits";
1634 55144267 2022-09-07 thomas case DIFF:
1635 55144267 2022-09-07 thomas return "diff";
1636 55144267 2022-09-07 thomas case ERR:
1637 55144267 2022-09-07 thomas return "err";
1638 55144267 2022-09-07 thomas case INDEX:
1639 55144267 2022-09-07 thomas return "index";
1640 55144267 2022-09-07 thomas case SUMMARY:
1641 55144267 2022-09-07 thomas return "summary";
1642 55144267 2022-09-07 thomas case TAG:
1643 55144267 2022-09-07 thomas return "tag";
1644 55144267 2022-09-07 thomas case TAGS:
1645 55144267 2022-09-07 thomas return "tags";
1646 55144267 2022-09-07 thomas case TREE:
1647 55144267 2022-09-07 thomas return "tree";
1648 d6795e9f 2022-12-30 thomas case RSS:
1649 d6795e9f 2022-12-30 thomas return "rss";
1650 55144267 2022-09-07 thomas default:
1651 55144267 2022-09-07 thomas return NULL;
1652 55144267 2022-09-07 thomas }
1653 55144267 2022-09-07 thomas }
1654 55144267 2022-09-07 thomas
1655 e7e5fa49 2022-12-30 thomas int
1656 e7e5fa49 2022-12-30 thomas gotweb_render_url(struct request *c, struct gotweb_url *url)
1657 55144267 2022-09-07 thomas {
1658 55144267 2022-09-07 thomas const char *sep = "?", *action;
1659 55144267 2022-09-07 thomas char *tmp;
1660 55144267 2022-09-07 thomas int r;
1661 55144267 2022-09-07 thomas
1662 e7e5fa49 2022-12-30 thomas action = gotweb_action_name(url->action);
1663 55144267 2022-09-07 thomas if (action != NULL) {
1664 55144267 2022-09-07 thomas if (fcgi_printf(c, "?action=%s", action) == -1)
1665 55144267 2022-09-07 thomas return -1;
1666 55144267 2022-09-07 thomas sep = "&";
1667 55144267 2022-09-07 thomas }
1668 55144267 2022-09-07 thomas
1669 55144267 2022-09-07 thomas if (url->commit) {
1670 55144267 2022-09-07 thomas if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1671 55144267 2022-09-07 thomas return -1;
1672 55144267 2022-09-07 thomas sep = "&";
1673 55144267 2022-09-07 thomas }
1674 55144267 2022-09-07 thomas
1675 55144267 2022-09-07 thomas if (url->previd) {
1676 55144267 2022-09-07 thomas if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1677 55144267 2022-09-07 thomas return -1;
1678 55144267 2022-09-07 thomas sep = "&";
1679 55144267 2022-09-07 thomas }
1680 55144267 2022-09-07 thomas
1681 55144267 2022-09-07 thomas if (url->prevset) {
1682 55144267 2022-09-07 thomas if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1683 55144267 2022-09-07 thomas return -1;
1684 55144267 2022-09-07 thomas sep = "&";
1685 55144267 2022-09-07 thomas }
1686 55144267 2022-09-07 thomas
1687 55144267 2022-09-07 thomas if (url->file) {
1688 55144267 2022-09-07 thomas tmp = gotweb_urlencode(url->file);
1689 55144267 2022-09-07 thomas if (tmp == NULL)
1690 55144267 2022-09-07 thomas return -1;
1691 55144267 2022-09-07 thomas r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1692 55144267 2022-09-07 thomas free(tmp);
1693 55144267 2022-09-07 thomas if (r == -1)
1694 55144267 2022-09-07 thomas return -1;
1695 55144267 2022-09-07 thomas sep = "&";
1696 55144267 2022-09-07 thomas }
1697 55144267 2022-09-07 thomas
1698 55144267 2022-09-07 thomas if (url->folder) {
1699 55144267 2022-09-07 thomas tmp = gotweb_urlencode(url->folder);
1700 55144267 2022-09-07 thomas if (tmp == NULL)
1701 55144267 2022-09-07 thomas return -1;
1702 55144267 2022-09-07 thomas r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1703 55144267 2022-09-07 thomas free(tmp);
1704 55144267 2022-09-07 thomas if (r == -1)
1705 55144267 2022-09-07 thomas return -1;
1706 55144267 2022-09-07 thomas sep = "&";
1707 55144267 2022-09-07 thomas }
1708 55144267 2022-09-07 thomas
1709 55144267 2022-09-07 thomas if (url->headref) {
1710 55144267 2022-09-07 thomas tmp = gotweb_urlencode(url->headref);
1711 55144267 2022-09-07 thomas if (tmp == NULL)
1712 55144267 2022-09-07 thomas return -1;
1713 55144267 2022-09-07 thomas r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1714 55144267 2022-09-07 thomas free(tmp);
1715 55144267 2022-09-07 thomas if (r == -1)
1716 55144267 2022-09-07 thomas return -1;
1717 55144267 2022-09-07 thomas sep = "&";
1718 55144267 2022-09-07 thomas }
1719 55144267 2022-09-07 thomas
1720 55144267 2022-09-07 thomas if (url->index_page != -1) {
1721 55144267 2022-09-07 thomas if (fcgi_printf(c, "%sindex_page=%d", sep,
1722 55144267 2022-09-07 thomas url->index_page) == -1)
1723 55144267 2022-09-07 thomas return -1;
1724 55144267 2022-09-07 thomas sep = "&";
1725 55144267 2022-09-07 thomas }
1726 55144267 2022-09-07 thomas
1727 55144267 2022-09-07 thomas if (url->path) {
1728 55144267 2022-09-07 thomas tmp = gotweb_urlencode(url->path);
1729 55144267 2022-09-07 thomas if (tmp == NULL)
1730 55144267 2022-09-07 thomas return -1;
1731 55144267 2022-09-07 thomas r = fcgi_printf(c, "%spath=%s", sep, tmp);
1732 55144267 2022-09-07 thomas free(tmp);
1733 55144267 2022-09-07 thomas if (r == -1)
1734 55144267 2022-09-07 thomas return -1;
1735 55144267 2022-09-07 thomas sep = "&";
1736 55144267 2022-09-07 thomas }
1737 55144267 2022-09-07 thomas
1738 55144267 2022-09-07 thomas if (url->page != -1) {
1739 55144267 2022-09-07 thomas if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1740 55144267 2022-09-07 thomas return -1;
1741 55144267 2022-09-07 thomas sep = "&";
1742 55144267 2022-09-07 thomas }
1743 55144267 2022-09-07 thomas
1744 55144267 2022-09-07 thomas return 0;
1745 55144267 2022-09-07 thomas }
1746 55144267 2022-09-07 thomas
1747 55144267 2022-09-07 thomas int
1748 d6795e9f 2022-12-30 thomas gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1749 d6795e9f 2022-12-30 thomas {
1750 d6795e9f 2022-12-30 thomas struct template *tp = c->tp;
1751 d6795e9f 2022-12-30 thomas const char *proto = c->https ? "https" : "http";
1752 d6795e9f 2022-12-30 thomas
1753 d6795e9f 2022-12-30 thomas if (fcgi_puts(tp, proto) == -1 ||
1754 d6795e9f 2022-12-30 thomas fcgi_puts(tp, "://") == -1 ||
1755 d6795e9f 2022-12-30 thomas tp_htmlescape(tp, c->server_name) == -1 ||
1756 d6795e9f 2022-12-30 thomas tp_htmlescape(tp, c->document_uri) == -1)
1757 d6795e9f 2022-12-30 thomas return -1;
1758 d6795e9f 2022-12-30 thomas
1759 d6795e9f 2022-12-30 thomas return gotweb_render_url(c, url);
1760 d6795e9f 2022-12-30 thomas }
1761 d6795e9f 2022-12-30 thomas
1762 d6795e9f 2022-12-30 thomas int
1763 55144267 2022-09-07 thomas gotweb_link(struct request *c, struct gotweb_url *url, const char *fmt, ...)
1764 55144267 2022-09-07 thomas {
1765 55144267 2022-09-07 thomas va_list ap;
1766 55144267 2022-09-07 thomas int r;
1767 55144267 2022-09-07 thomas
1768 55144267 2022-09-07 thomas if (fcgi_printf(c, "<a href='") == -1)
1769 55144267 2022-09-07 thomas return -1;
1770 55144267 2022-09-07 thomas
1771 e7e5fa49 2022-12-30 thomas if (gotweb_render_url(c, url) == -1)
1772 55144267 2022-09-07 thomas return -1;
1773 55144267 2022-09-07 thomas
1774 55144267 2022-09-07 thomas if (fcgi_printf(c, "'>") == -1)
1775 55144267 2022-09-07 thomas return -1;
1776 55144267 2022-09-07 thomas
1777 55144267 2022-09-07 thomas va_start(ap, fmt);
1778 55144267 2022-09-07 thomas r = fcgi_vprintf(c, fmt, ap);
1779 55144267 2022-09-07 thomas va_end(ap);
1780 55144267 2022-09-07 thomas if (r == -1)
1781 55144267 2022-09-07 thomas return -1;
1782 55144267 2022-09-07 thomas
1783 55144267 2022-09-07 thomas if (fcgi_printf(c, "</a>"))
1784 55144267 2022-09-07 thomas return -1;
1785 55144267 2022-09-07 thomas return 0;
1786 55144267 2022-09-07 thomas }
1787 55144267 2022-09-07 thomas
1788 55e6cffd 2022-09-01 thomas static struct got_repository *
1789 55e6cffd 2022-09-01 thomas find_cached_repo(struct server *srv, const char *path)
1790 55e6cffd 2022-09-01 thomas {
1791 55e6cffd 2022-09-01 thomas int i;
1792 55e6cffd 2022-09-01 thomas
1793 55e6cffd 2022-09-01 thomas for (i = 0; i < srv->ncached_repos; i++) {
1794 55e6cffd 2022-09-01 thomas if (strcmp(srv->cached_repos[i].path, path) == 0)
1795 55e6cffd 2022-09-01 thomas return srv->cached_repos[i].repo;
1796 55e6cffd 2022-09-01 thomas }
1797 55e6cffd 2022-09-01 thomas
1798 55e6cffd 2022-09-01 thomas return NULL;
1799 55e6cffd 2022-09-01 thomas }
1800 55e6cffd 2022-09-01 thomas
1801 8a35f56c 2022-07-16 thomas static const struct got_error *
1802 55e6cffd 2022-09-01 thomas cache_repo(struct got_repository **new, struct server *srv,
1803 55e6cffd 2022-09-01 thomas struct repo_dir *repo_dir, struct socket *sock)
1804 55e6cffd 2022-09-01 thomas {
1805 55e6cffd 2022-09-01 thomas const struct got_error *error = NULL;
1806 55e6cffd 2022-09-01 thomas struct got_repository *repo;
1807 55e6cffd 2022-09-01 thomas struct cached_repo *cr;
1808 55e6cffd 2022-09-01 thomas int evicted = 0;
1809 55e6cffd 2022-09-01 thomas
1810 a004b24a 2022-09-06 thomas if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1811 55e6cffd 2022-09-01 thomas cr = &srv->cached_repos[srv->ncached_repos - 1];
1812 55e6cffd 2022-09-01 thomas error = got_repo_close(cr->repo);
1813 55e6cffd 2022-09-01 thomas memset(cr, 0, sizeof(*cr));
1814 55e6cffd 2022-09-01 thomas srv->ncached_repos--;
1815 55e6cffd 2022-09-01 thomas if (error)
1816 55e6cffd 2022-09-01 thomas return error;
1817 55e6cffd 2022-09-01 thomas memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1818 55e6cffd 2022-09-01 thomas srv->ncached_repos * sizeof(srv->cached_repos[0]));
1819 55e6cffd 2022-09-01 thomas cr = &srv->cached_repos[0];
1820 55e6cffd 2022-09-01 thomas evicted = 1;
1821 55e6cffd 2022-09-01 thomas } else {
1822 55e6cffd 2022-09-01 thomas cr = &srv->cached_repos[srv->ncached_repos];
1823 55e6cffd 2022-09-01 thomas }
1824 55e6cffd 2022-09-01 thomas
1825 55e6cffd 2022-09-01 thomas error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1826 55e6cffd 2022-09-01 thomas if (error) {
1827 55e6cffd 2022-09-01 thomas if (evicted) {
1828 55e6cffd 2022-09-01 thomas memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1829 55e6cffd 2022-09-01 thomas srv->ncached_repos * sizeof(srv->cached_repos[0]));
1830 55e6cffd 2022-09-01 thomas }
1831 55e6cffd 2022-09-01 thomas return error;
1832 55e6cffd 2022-09-01 thomas }
1833 55e6cffd 2022-09-01 thomas
1834 55e6cffd 2022-09-01 thomas if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1835 55e6cffd 2022-09-01 thomas >= sizeof(cr->path)) {
1836 55e6cffd 2022-09-01 thomas if (evicted) {
1837 55e6cffd 2022-09-01 thomas memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1838 55e6cffd 2022-09-01 thomas srv->ncached_repos * sizeof(srv->cached_repos[0]));
1839 55e6cffd 2022-09-01 thomas }
1840 55e6cffd 2022-09-01 thomas return got_error(GOT_ERR_NO_SPACE);
1841 55e6cffd 2022-09-01 thomas }
1842 55e6cffd 2022-09-01 thomas
1843 55e6cffd 2022-09-01 thomas cr->repo = repo;
1844 55e6cffd 2022-09-01 thomas srv->ncached_repos++;
1845 55e6cffd 2022-09-01 thomas *new = repo;
1846 55e6cffd 2022-09-01 thomas return NULL;
1847 55e6cffd 2022-09-01 thomas }
1848 55e6cffd 2022-09-01 thomas
1849 55e6cffd 2022-09-01 thomas static const struct got_error *
1850 8a35f56c 2022-07-16 thomas gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1851 8a35f56c 2022-07-16 thomas {
1852 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1853 8a35f56c 2022-07-16 thomas struct socket *sock = c->sock;
1854 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
1855 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1856 55e6cffd 2022-09-01 thomas struct got_repository *repo = NULL;
1857 8a35f56c 2022-07-16 thomas DIR *dt;
1858 8a35f56c 2022-07-16 thomas char *dir_test;
1859 8a35f56c 2022-07-16 thomas
1860 8a35f56c 2022-07-16 thomas if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1861 8a35f56c 2022-07-16 thomas GOTWEB_GIT_DIR) == -1)
1862 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
1863 8a35f56c 2022-07-16 thomas
1864 8a35f56c 2022-07-16 thomas dt = opendir(dir_test);
1865 8a35f56c 2022-07-16 thomas if (dt == NULL) {
1866 8a35f56c 2022-07-16 thomas free(dir_test);
1867 8a35f56c 2022-07-16 thomas } else {
1868 c2d3d9a0 2022-09-01 thomas repo_dir->path = dir_test;
1869 8a35f56c 2022-07-16 thomas dir_test = NULL;
1870 c2d3d9a0 2022-09-01 thomas goto done;
1871 8a35f56c 2022-07-16 thomas }
1872 8a35f56c 2022-07-16 thomas
1873 8a35f56c 2022-07-16 thomas if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1874 c2d3d9a0 2022-09-01 thomas repo_dir->name) == -1)
1875 c2d3d9a0 2022-09-01 thomas return got_error_from_errno("asprintf");
1876 8a35f56c 2022-07-16 thomas
1877 8a35f56c 2022-07-16 thomas dt = opendir(dir_test);
1878 8a35f56c 2022-07-16 thomas if (dt == NULL) {
1879 8a35f56c 2022-07-16 thomas error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1880 8a35f56c 2022-07-16 thomas goto err;
1881 c2d3d9a0 2022-09-01 thomas } else {
1882 c2d3d9a0 2022-09-01 thomas repo_dir->path = dir_test;
1883 c2d3d9a0 2022-09-01 thomas dir_test = NULL;
1884 c2d3d9a0 2022-09-01 thomas }
1885 c2d3d9a0 2022-09-01 thomas
1886 8a35f56c 2022-07-16 thomas done:
1887 3991b2a5 2022-10-31 thomas if (srv->respect_exportok &&
1888 3991b2a5 2022-10-31 thomas faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1889 3991b2a5 2022-10-31 thomas error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1890 3991b2a5 2022-10-31 thomas goto err;
1891 3991b2a5 2022-10-31 thomas }
1892 3991b2a5 2022-10-31 thomas
1893 55e6cffd 2022-09-01 thomas repo = find_cached_repo(srv, repo_dir->path);
1894 55e6cffd 2022-09-01 thomas if (repo == NULL) {
1895 55e6cffd 2022-09-01 thomas error = cache_repo(&repo, srv, repo_dir, sock);
1896 55e6cffd 2022-09-01 thomas if (error)
1897 55e6cffd 2022-09-01 thomas goto err;
1898 55e6cffd 2022-09-01 thomas }
1899 55e6cffd 2022-09-01 thomas t->repo = repo;
1900 8a35f56c 2022-07-16 thomas error = gotweb_get_repo_description(&repo_dir->description, srv,
1901 4606e6d4 2022-11-23 thomas repo_dir->path, dirfd(dt));
1902 8a35f56c 2022-07-16 thomas if (error)
1903 8a35f56c 2022-07-16 thomas goto err;
1904 6c7f10f7 2022-11-23 thomas error = got_get_repo_owner(&repo_dir->owner, c);
1905 8a35f56c 2022-07-16 thomas if (error)
1906 8a35f56c 2022-07-16 thomas goto err;
1907 6c7f10f7 2022-11-23 thomas error = got_get_repo_age(&repo_dir->age, c, NULL, TM_DIFF);
1908 8a35f56c 2022-07-16 thomas if (error)
1909 8a35f56c 2022-07-16 thomas goto err;
1910 4606e6d4 2022-11-23 thomas error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1911 4606e6d4 2022-11-23 thomas dirfd(dt));
1912 8a35f56c 2022-07-16 thomas err:
1913 8a35f56c 2022-07-16 thomas free(dir_test);
1914 c2d3d9a0 2022-09-01 thomas if (dt != NULL && closedir(dt) == EOF && error == NULL)
1915 c2d3d9a0 2022-09-01 thomas error = got_error_from_errno("closedir");
1916 8a35f56c 2022-07-16 thomas return error;
1917 8a35f56c 2022-07-16 thomas }
1918 8a35f56c 2022-07-16 thomas
1919 8a35f56c 2022-07-16 thomas static const struct got_error *
1920 8a35f56c 2022-07-16 thomas gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1921 8a35f56c 2022-07-16 thomas {
1922 8a35f56c 2022-07-16 thomas const struct got_error *error;
1923 8a35f56c 2022-07-16 thomas
1924 8a35f56c 2022-07-16 thomas *repo_dir = calloc(1, sizeof(**repo_dir));
1925 8a35f56c 2022-07-16 thomas if (*repo_dir == NULL)
1926 8a35f56c 2022-07-16 thomas return got_error_from_errno("calloc");
1927 8a35f56c 2022-07-16 thomas
1928 8a35f56c 2022-07-16 thomas if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1929 8a35f56c 2022-07-16 thomas error = got_error_from_errno("asprintf");
1930 8a35f56c 2022-07-16 thomas free(*repo_dir);
1931 8a35f56c 2022-07-16 thomas *repo_dir = NULL;
1932 8a35f56c 2022-07-16 thomas return error;
1933 8a35f56c 2022-07-16 thomas }
1934 8a35f56c 2022-07-16 thomas (*repo_dir)->owner = NULL;
1935 8a35f56c 2022-07-16 thomas (*repo_dir)->description = NULL;
1936 8a35f56c 2022-07-16 thomas (*repo_dir)->url = NULL;
1937 8a35f56c 2022-07-16 thomas (*repo_dir)->age = NULL;
1938 8a35f56c 2022-07-16 thomas (*repo_dir)->path = NULL;
1939 8a35f56c 2022-07-16 thomas
1940 8a35f56c 2022-07-16 thomas return NULL;
1941 8a35f56c 2022-07-16 thomas }
1942 8a35f56c 2022-07-16 thomas
1943 8a35f56c 2022-07-16 thomas static const struct got_error *
1944 4606e6d4 2022-11-23 thomas gotweb_get_repo_description(char **description, struct server *srv,
1945 4606e6d4 2022-11-23 thomas const char *dirpath, int dir)
1946 8a35f56c 2022-07-16 thomas {
1947 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1948 4606e6d4 2022-11-23 thomas struct stat sb;
1949 4606e6d4 2022-11-23 thomas int fd = -1;
1950 4606e6d4 2022-11-23 thomas off_t len;
1951 8a35f56c 2022-07-16 thomas
1952 8a35f56c 2022-07-16 thomas *description = NULL;
1953 8a35f56c 2022-07-16 thomas if (srv->show_repo_description == 0)
1954 8a35f56c 2022-07-16 thomas return NULL;
1955 8a35f56c 2022-07-16 thomas
1956 4606e6d4 2022-11-23 thomas fd = openat(dir, "description", O_RDONLY);
1957 4606e6d4 2022-11-23 thomas if (fd == -1) {
1958 4606e6d4 2022-11-23 thomas if (errno != ENOENT && errno != EACCES) {
1959 4606e6d4 2022-11-23 thomas error = got_error_from_errno_fmt("openat %s/%s",
1960 4606e6d4 2022-11-23 thomas dirpath, "description");
1961 4606e6d4 2022-11-23 thomas }
1962 8a35f56c 2022-07-16 thomas goto done;
1963 8a35f56c 2022-07-16 thomas }
1964 8a35f56c 2022-07-16 thomas
1965 4606e6d4 2022-11-23 thomas if (fstat(fd, &sb) == -1) {
1966 4606e6d4 2022-11-23 thomas error = got_error_from_errno_fmt("fstat %s/%s",
1967 4606e6d4 2022-11-23 thomas dirpath, "description");
1968 8a35f56c 2022-07-16 thomas goto done;
1969 8a35f56c 2022-07-16 thomas }
1970 8a35f56c 2022-07-16 thomas
1971 4606e6d4 2022-11-23 thomas len = sb.st_size;
1972 3e9a56b5 2022-12-01 thomas if (len > GOTWEBD_MAXDESCRSZ - 1)
1973 3e9a56b5 2022-12-01 thomas len = GOTWEBD_MAXDESCRSZ - 1;
1974 8a35f56c 2022-07-16 thomas
1975 8a35f56c 2022-07-16 thomas *description = calloc(len + 1, sizeof(**description));
1976 8a35f56c 2022-07-16 thomas if (*description == NULL) {
1977 8a35f56c 2022-07-16 thomas error = got_error_from_errno("calloc");
1978 8a35f56c 2022-07-16 thomas goto done;
1979 8a35f56c 2022-07-16 thomas }
1980 8a35f56c 2022-07-16 thomas
1981 4606e6d4 2022-11-23 thomas if (read(fd, *description, len) == -1)
1982 4606e6d4 2022-11-23 thomas error = got_error_from_errno("read");
1983 8a35f56c 2022-07-16 thomas done:
1984 4606e6d4 2022-11-23 thomas if (fd != -1 && close(fd) == -1 && error == NULL)
1985 4606e6d4 2022-11-23 thomas error = got_error_from_errno("close");
1986 8a35f56c 2022-07-16 thomas return error;
1987 8a35f56c 2022-07-16 thomas }
1988 8a35f56c 2022-07-16 thomas
1989 8a35f56c 2022-07-16 thomas static const struct got_error *
1990 4606e6d4 2022-11-23 thomas gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1991 4606e6d4 2022-11-23 thomas int dir)
1992 8a35f56c 2022-07-16 thomas {
1993 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1994 4606e6d4 2022-11-23 thomas struct stat sb;
1995 4606e6d4 2022-11-23 thomas int fd = -1;
1996 4606e6d4 2022-11-23 thomas off_t len;
1997 8a35f56c 2022-07-16 thomas
1998 8a35f56c 2022-07-16 thomas *url = NULL;
1999 8a35f56c 2022-07-16 thomas if (srv->show_repo_cloneurl == 0)
2000 8a35f56c 2022-07-16 thomas return NULL;
2001 8a35f56c 2022-07-16 thomas
2002 4606e6d4 2022-11-23 thomas fd = openat(dir, "cloneurl", O_RDONLY);
2003 4606e6d4 2022-11-23 thomas if (fd == -1) {
2004 4606e6d4 2022-11-23 thomas if (errno != ENOENT && errno != EACCES) {
2005 4606e6d4 2022-11-23 thomas error = got_error_from_errno_fmt("openat %s/%s",
2006 4606e6d4 2022-11-23 thomas dirpath, "cloneurl");
2007 4606e6d4 2022-11-23 thomas }
2008 8a35f56c 2022-07-16 thomas goto done;
2009 8a35f56c 2022-07-16 thomas }
2010 8a35f56c 2022-07-16 thomas
2011 4606e6d4 2022-11-23 thomas if (fstat(fd, &sb) == -1) {
2012 4606e6d4 2022-11-23 thomas error = got_error_from_errno_fmt("fstat %s/%s",
2013 4606e6d4 2022-11-23 thomas dirpath, "cloneurl");
2014 8a35f56c 2022-07-16 thomas goto done;
2015 8a35f56c 2022-07-16 thomas }
2016 8a35f56c 2022-07-16 thomas
2017 4606e6d4 2022-11-23 thomas len = sb.st_size;
2018 3e9a56b5 2022-12-01 thomas if (len > GOTWEBD_MAXCLONEURLSZ - 1)
2019 3e9a56b5 2022-12-01 thomas len = GOTWEBD_MAXCLONEURLSZ - 1;
2020 8a35f56c 2022-07-16 thomas
2021 8a35f56c 2022-07-16 thomas *url = calloc(len + 1, sizeof(**url));
2022 8a35f56c 2022-07-16 thomas if (*url == NULL) {
2023 8a35f56c 2022-07-16 thomas error = got_error_from_errno("calloc");
2024 8a35f56c 2022-07-16 thomas goto done;
2025 8a35f56c 2022-07-16 thomas }
2026 8a35f56c 2022-07-16 thomas
2027 4606e6d4 2022-11-23 thomas if (read(fd, *url, len) == -1)
2028 4606e6d4 2022-11-23 thomas error = got_error_from_errno("read");
2029 8a35f56c 2022-07-16 thomas done:
2030 4606e6d4 2022-11-23 thomas if (fd != -1 && close(fd) == -1 && error == NULL)
2031 4606e6d4 2022-11-23 thomas error = got_error_from_errno("close");
2032 8a35f56c 2022-07-16 thomas return error;
2033 8a35f56c 2022-07-16 thomas }
2034 8a35f56c 2022-07-16 thomas
2035 8a35f56c 2022-07-16 thomas const struct got_error *
2036 8a35f56c 2022-07-16 thomas gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2037 8a35f56c 2022-07-16 thomas {
2038 8a35f56c 2022-07-16 thomas struct tm tm;
2039 399ea8e4 2022-07-20 thomas long long diff_time;
2040 8a35f56c 2022-07-16 thomas const char *years = "years ago", *months = "months ago";
2041 8a35f56c 2022-07-16 thomas const char *weeks = "weeks ago", *days = "days ago";
2042 8a35f56c 2022-07-16 thomas const char *hours = "hours ago", *minutes = "minutes ago";
2043 8a35f56c 2022-07-16 thomas const char *seconds = "seconds ago", *now = "right now";
2044 8a35f56c 2022-07-16 thomas char *s;
2045 d6795e9f 2022-12-30 thomas char datebuf[64];
2046 d6795e9f 2022-12-30 thomas size_t r;
2047 8a35f56c 2022-07-16 thomas
2048 8a35f56c 2022-07-16 thomas *repo_age = NULL;
2049 8a35f56c 2022-07-16 thomas
2050 8a35f56c 2022-07-16 thomas switch (ref_tm) {
2051 8a35f56c 2022-07-16 thomas case TM_DIFF:
2052 8a35f56c 2022-07-16 thomas diff_time = time(NULL) - committer_time;
2053 8a35f56c 2022-07-16 thomas if (diff_time > 60 * 60 * 24 * 365 * 2) {
2054 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%lld %s",
2055 8a35f56c 2022-07-16 thomas (diff_time / 60 / 60 / 24 / 365), years) == -1)
2056 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2057 8a35f56c 2022-07-16 thomas } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2058 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%lld %s",
2059 8a35f56c 2022-07-16 thomas (diff_time / 60 / 60 / 24 / (365 / 12)),
2060 8a35f56c 2022-07-16 thomas months) == -1)
2061 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2062 8a35f56c 2022-07-16 thomas } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2063 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%lld %s",
2064 8a35f56c 2022-07-16 thomas (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2065 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2066 8a35f56c 2022-07-16 thomas } else if (diff_time > 60 * 60 * 24 * 2) {
2067 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%lld %s",
2068 8a35f56c 2022-07-16 thomas (diff_time / 60 / 60 / 24), days) == -1)
2069 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2070 8a35f56c 2022-07-16 thomas } else if (diff_time > 60 * 60 * 2) {
2071 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%lld %s",
2072 8a35f56c 2022-07-16 thomas (diff_time / 60 / 60), hours) == -1)
2073 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2074 8a35f56c 2022-07-16 thomas } else if (diff_time > 60 * 2) {
2075 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2076 8a35f56c 2022-07-16 thomas minutes) == -1)
2077 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2078 8a35f56c 2022-07-16 thomas } else if (diff_time > 2) {
2079 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%lld %s", diff_time,
2080 8a35f56c 2022-07-16 thomas seconds) == -1)
2081 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2082 8a35f56c 2022-07-16 thomas } else {
2083 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%s", now) == -1)
2084 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2085 8a35f56c 2022-07-16 thomas }
2086 8a35f56c 2022-07-16 thomas break;
2087 8a35f56c 2022-07-16 thomas case TM_LONG:
2088 8a35f56c 2022-07-16 thomas if (gmtime_r(&committer_time, &tm) == NULL)
2089 8a35f56c 2022-07-16 thomas return got_error_from_errno("gmtime_r");
2090 8a35f56c 2022-07-16 thomas
2091 8a35f56c 2022-07-16 thomas s = asctime_r(&tm, datebuf);
2092 8a35f56c 2022-07-16 thomas if (s == NULL)
2093 8a35f56c 2022-07-16 thomas return got_error_from_errno("asctime_r");
2094 8a35f56c 2022-07-16 thomas
2095 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2096 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2097 8a35f56c 2022-07-16 thomas break;
2098 d6795e9f 2022-12-30 thomas case TM_RFC822:
2099 d6795e9f 2022-12-30 thomas if (gmtime_r(&committer_time, &tm) == NULL)
2100 d6795e9f 2022-12-30 thomas return got_error_from_errno("gmtime_r");
2101 d6795e9f 2022-12-30 thomas
2102 d6795e9f 2022-12-30 thomas r = strftime(datebuf, sizeof(datebuf),
2103 d6795e9f 2022-12-30 thomas "%a, %d %b %Y %H:%M:%S GMT", &tm);
2104 d6795e9f 2022-12-30 thomas if (r == 0)
2105 d6795e9f 2022-12-30 thomas return got_error(GOT_ERR_NO_SPACE);
2106 d6795e9f 2022-12-30 thomas
2107 d6795e9f 2022-12-30 thomas *repo_age = strdup(datebuf);
2108 d6795e9f 2022-12-30 thomas if (*repo_age == NULL)
2109 d6795e9f 2022-12-30 thomas return got_error_from_errno("asprintf");
2110 d6795e9f 2022-12-30 thomas break;
2111 8a35f56c 2022-07-16 thomas }
2112 8a35f56c 2022-07-16 thomas return NULL;
2113 3e12c168 2022-07-16 thomas }