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