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