Blame


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