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