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