Blame


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