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