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