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