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