Blob


1 /*
2 * Copyright (c) 2019, 2020 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/queue.h>
19 #include <sys/stat.h>
20 #include <sys/types.h>
22 #include <ctype.h>
23 #include <dirent.h>
24 #include <err.h>
25 #include <errno.h>
26 #include <regex.h>
27 #include <stdarg.h>
28 #include <stdint.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
34 #include <got_object.h>
35 #include <got_reference.h>
36 #include <got_repository.h>
37 #include <got_path.h>
38 #include <got_cancel.h>
39 #include <got_worktree.h>
40 #include <got_diff.h>
41 #include <got_commit_graph.h>
42 #include <got_blame.h>
43 #include <got_privsep.h>
44 #include <got_opentemp.h>
46 #include <kcgi.h>
47 #include <kcgihtml.h>
49 #include "gotweb.h"
51 #ifndef nitems
52 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
53 #endif
55 struct gw_trans {
56 TAILQ_HEAD(headers, gw_header) gw_headers;
57 TAILQ_HEAD(dirs, gw_dir) gw_dirs;
58 struct got_repository *repo;
59 struct gw_dir *gw_dir;
60 struct gotweb_conf *gw_conf;
61 struct ktemplate *gw_tmpl;
62 struct khtmlreq *gw_html_req;
63 struct kreq *gw_req;
64 const struct got_error *error;
65 const char *repo_name;
66 char *repo_path;
67 char *commit_id;
68 char *next_id;
69 char *next_prev_id;
70 char *prev_id;
71 char *prev_prev_id;
72 const char *repo_file;
73 char *repo_folder;
74 const char *headref;
75 unsigned int action;
76 unsigned int page;
77 unsigned int repos_total;
78 enum kmime mime;
79 };
81 struct gw_header {
82 TAILQ_ENTRY(gw_header) entry;
83 struct got_reflist_head refs;
84 char *path;
86 char *refs_str;
87 char *commit_id; /* id_str1 */
88 char *parent_id; /* id_str2 */
89 char *tree_id;
90 char *author;
91 char *committer;
92 char *commit_msg;
93 time_t committer_time;
94 };
96 struct gw_dir {
97 TAILQ_ENTRY(gw_dir) entry;
98 char *name;
99 char *owner;
100 char *description;
101 char *url;
102 char *age;
103 char *path;
104 };
106 enum gw_key {
107 KEY_ACTION,
108 KEY_COMMIT_ID,
109 KEY_FILE,
110 KEY_FOLDER,
111 KEY_HEADREF,
112 KEY_PAGE,
113 KEY_PATH,
114 KEY_PREV_ID,
115 KEY_PREV_PREV_ID,
116 KEY__ZMAX
117 };
119 enum gw_tmpl {
120 TEMPL_CONTENT,
121 TEMPL_HEAD,
122 TEMPL_HEADER,
123 TEMPL_SEARCH,
124 TEMPL_SITEPATH,
125 TEMPL_SITEOWNER,
126 TEMPL_TITLE,
127 TEMPL__MAX
128 };
130 enum gw_ref_tm {
131 TM_DIFF,
132 TM_LONG,
133 };
135 enum gw_tags {
136 TAGBRIEF,
137 TAGFULL,
138 };
140 static const char *const gw_templs[TEMPL__MAX] = {
141 "content",
142 "head",
143 "header",
144 "search",
145 "sitepath",
146 "siteowner",
147 "title",
148 };
150 static const struct kvalid gw_keys[KEY__ZMAX] = {
151 { kvalid_stringne, "action" },
152 { kvalid_stringne, "commit" },
153 { kvalid_stringne, "file" },
154 { kvalid_stringne, "folder" },
155 { kvalid_stringne, "headref" },
156 { kvalid_int, "page" },
157 { kvalid_stringne, "path" },
158 { kvalid_stringne, "prev" },
159 { kvalid_stringne, "prev_prev" },
160 };
162 static struct gw_header *gw_init_header(void);
164 static void gw_free_header(struct gw_header *);
166 static int gw_template(size_t, void *);
168 static const struct got_error *gw_error(struct gw_trans *);
169 static const struct got_error *gw_init_gw_dir(struct gw_dir **, const char *);
170 static const struct got_error *gw_get_repo_description(char **,
171 struct gw_trans *, char *);
172 static const struct got_error *gw_get_repo_owner(char **, struct gw_trans *,
173 char *);
174 static const struct got_error *gw_get_time_str(char **, time_t, int);
175 static const struct got_error *gw_get_repo_age(char **, struct gw_trans *,
176 char *, const char *, int);
177 static const struct got_error *gw_output_file_blame(struct gw_trans *);
178 static const struct got_error *gw_output_blob_buf(struct gw_trans *);
179 static const struct got_error *gw_output_repo_tree(struct gw_trans *);
180 static const struct got_error *gw_output_diff(struct gw_trans *,
181 struct gw_header *);
182 static const struct got_error *gw_output_repo_tags(struct gw_trans *,
183 struct gw_header *, int, int);
184 static const struct got_error *gw_output_repo_heads(struct gw_trans *);
185 static const struct got_error *gw_output_site_link(struct gw_trans *);
186 static const struct got_error *gw_get_clone_url(char **, struct gw_trans *,
187 char *);
188 static const struct got_error *gw_colordiff_line(struct gw_trans *, char *);
190 static const struct got_error *gw_gen_commit_header(struct gw_trans *, char *,
191 char*);
192 static const struct got_error *gw_gen_diff_header(struct gw_trans *, char *,
193 char*);
194 static const struct got_error *gw_gen_author_header(struct gw_trans *,
195 const char *);
196 static const struct got_error *gw_gen_age_header(struct gw_trans *,
197 const char *);
198 static const struct got_error *gw_gen_committer_header(struct gw_trans *,
199 const char *);
200 static const struct got_error *gw_gen_commit_msg_header(struct gw_trans*,
201 char *);
202 static const struct got_error *gw_gen_tree_header(struct gw_trans *, char *);
203 static const struct got_error *gw_display_open(struct gw_trans *, enum khttp,
204 enum kmime);
205 static const struct got_error *gw_display_index(struct gw_trans *);
206 static const struct got_error *gw_get_header(struct gw_trans *,
207 struct gw_header *, int);
208 static const struct got_error *gw_get_commits(struct gw_trans *,
209 struct gw_header *, int,
210 struct got_object_id *);
211 static const struct got_error *gw_get_commit(struct gw_trans *,
212 struct gw_header *,
213 struct got_commit_object *,
214 struct got_object_id *);
215 static const struct got_error *gw_apply_unveil(const char *);
216 static const struct got_error *gw_blame_cb(void *, int, int,
217 struct got_object_id *);
218 static const struct got_error *gw_load_got_paths(struct gw_trans *);
219 static const struct got_error *gw_load_got_path(struct gw_trans *,
220 struct gw_dir *);
221 static const struct got_error *gw_parse_querystring(struct gw_trans *);
222 static const struct got_error *gw_blame(struct gw_trans *);
223 static const struct got_error *gw_blob(struct gw_trans *);
224 static const struct got_error *gw_diff(struct gw_trans *);
225 static const struct got_error *gw_index(struct gw_trans *);
226 static const struct got_error *gw_commits(struct gw_trans *);
227 static const struct got_error *gw_briefs(struct gw_trans *);
228 static const struct got_error *gw_summary(struct gw_trans *);
229 static const struct got_error *gw_tree(struct gw_trans *);
230 static const struct got_error *gw_tag(struct gw_trans *);
232 struct gw_query_action {
233 unsigned int func_id;
234 const char *func_name;
235 const struct got_error *(*func_main)(struct gw_trans *);
236 char *template;
237 };
239 enum gw_query_actions {
240 GW_BLAME,
241 GW_BLOB,
242 GW_BRIEFS,
243 GW_COMMITS,
244 GW_DIFF,
245 GW_ERR,
246 GW_INDEX,
247 GW_SUMMARY,
248 GW_TAG,
249 GW_TREE,
250 };
252 static struct gw_query_action gw_query_funcs[] = {
253 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
254 { GW_BLOB, "blob", NULL, NULL },
255 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
256 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
257 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
258 { GW_ERR, "error", gw_error, "gw_tmpl/err.tmpl" },
259 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
260 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
261 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
262 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
263 };
265 static const char *
266 gw_get_action_name(struct gw_trans *gw_trans)
268 return gw_query_funcs[gw_trans->action].func_name;
271 static const struct got_error *
272 gw_kcgi_error(enum kcgi_err kerr)
274 if (kerr == KCGI_OK)
275 return NULL;
277 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
278 return got_error(GOT_ERR_CANCELLED);
280 if (kerr == KCGI_ENOMEM)
281 return got_error_set_errno(ENOMEM,
282 kcgi_strerror(kerr));
284 if (kerr == KCGI_ENFILE)
285 return got_error_set_errno(ENFILE,
286 kcgi_strerror(kerr));
288 if (kerr == KCGI_EAGAIN)
289 return got_error_set_errno(EAGAIN,
290 kcgi_strerror(kerr));
292 if (kerr == KCGI_FORM)
293 return got_error_msg(GOT_ERR_IO,
294 kcgi_strerror(kerr));
296 return got_error_from_errno(kcgi_strerror(kerr));
299 static const struct got_error *
300 gw_apply_unveil(const char *repo_path)
302 const struct got_error *err;
304 if (repo_path && unveil(repo_path, "r") != 0)
305 return got_error_from_errno2("unveil", repo_path);
307 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
308 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
310 err = got_privsep_unveil_exec_helpers();
311 if (err != NULL)
312 return err;
314 if (unveil(NULL, NULL) != 0)
315 return got_error_from_errno("unveil");
317 return NULL;
320 static int
321 isbinary(const uint8_t *buf, size_t n)
323 size_t i;
325 for (i = 0; i < n; i++)
326 if (buf[i] == 0)
327 return 1;
328 return 0;
331 static const struct got_error *
332 gw_blame(struct gw_trans *gw_trans)
334 const struct got_error *error = NULL;
335 struct gw_header *header = NULL;
336 char *age = NULL;
337 enum kcgi_err kerr = KCGI_OK;
339 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
340 NULL) == -1)
341 return got_error_from_errno("pledge");
343 if ((header = gw_init_header()) == NULL)
344 return got_error_from_errno("malloc");
346 error = gw_apply_unveil(gw_trans->gw_dir->path);
347 if (error)
348 goto done;
350 /* check querystring */
351 if (gw_trans->repo_file == NULL) {
352 error = got_error_msg(GOT_ERR_QUERYSTRING,
353 "file required in querystring");
354 goto done;
356 if (gw_trans->commit_id == NULL) {
357 error = got_error_msg(GOT_ERR_QUERYSTRING,
358 "commit required in querystring");
359 goto done;
362 error = gw_get_header(gw_trans, header, 1);
363 if (error)
364 goto done;
365 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
366 "blame_header_wrapper", KATTR__MAX);
367 if (kerr != KCGI_OK)
368 goto done;
369 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
370 "blame_header", KATTR__MAX);
371 if (kerr != KCGI_OK)
372 goto done;
373 error = gw_get_time_str(&age, header->committer_time,
374 TM_LONG);
375 if (error)
376 goto done;
377 error = gw_gen_age_header(gw_trans, age ?age : "");
378 if (error)
379 goto done;
380 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
381 if (error)
382 goto done;
383 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
384 if (kerr != KCGI_OK)
385 goto done;
386 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
387 "dotted_line", KATTR__MAX);
388 if (kerr != KCGI_OK)
389 goto done;
390 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
391 if (kerr != KCGI_OK)
392 goto done;
394 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
395 "blame", KATTR__MAX);
396 if (kerr != KCGI_OK)
397 goto done;
398 error = gw_output_file_blame(gw_trans);
399 if (error)
400 goto done;
401 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
402 done:
403 gw_free_header(header);
404 if (error == NULL && kerr != KCGI_OK)
405 error = gw_kcgi_error(kerr);
406 return error;
409 static const struct got_error *
410 gw_blob(struct gw_trans *gw_trans)
412 const struct got_error *error = NULL, *err = NULL;
413 struct gw_header *header = NULL;
414 enum kcgi_err kerr = KCGI_OK;
416 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
417 NULL) == -1)
418 return got_error_from_errno("pledge");
420 if ((header = gw_init_header()) == NULL)
421 return got_error_from_errno("malloc");
423 error = gw_apply_unveil(gw_trans->gw_dir->path);
424 if (error)
425 goto done;
427 /* check querystring */
428 if (gw_trans->repo_file == NULL) {
429 error = got_error_msg(GOT_ERR_QUERYSTRING,
430 "file required in querystring");
431 goto done;
433 if (gw_trans->commit_id == NULL) {
434 error = got_error_msg(GOT_ERR_QUERYSTRING,
435 "commit required in querystring");
436 goto done;
438 error = gw_get_header(gw_trans, header, 1);
439 if (error)
440 goto done;
442 error = gw_output_blob_buf(gw_trans);
443 done:
445 if (error) {
446 gw_trans->mime = KMIME_TEXT_PLAIN;
447 err = gw_display_index(gw_trans);
448 if (err) {
449 error = err;
450 goto errored;
452 kerr = khttp_puts(gw_trans->gw_req, error->msg);
454 errored:
455 gw_free_header(header);
456 if (error == NULL && kerr != KCGI_OK)
457 error = gw_kcgi_error(kerr);
458 return error;
461 static const struct got_error *
462 gw_diff(struct gw_trans *gw_trans)
464 const struct got_error *error = NULL;
465 struct gw_header *header = NULL;
466 char *age = NULL;
467 enum kcgi_err kerr = KCGI_OK;
469 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
470 NULL) == -1)
471 return got_error_from_errno("pledge");
473 if ((header = gw_init_header()) == NULL)
474 return got_error_from_errno("malloc");
476 error = gw_apply_unveil(gw_trans->gw_dir->path);
477 if (error)
478 goto done;
480 error = gw_get_header(gw_trans, header, 1);
481 if (error)
482 goto done;
484 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
485 "diff_header_wrapper", KATTR__MAX);
486 if (kerr != KCGI_OK)
487 goto done;
488 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
489 "diff_header", KATTR__MAX);
490 if (kerr != KCGI_OK)
491 goto done;
492 error = gw_gen_diff_header(gw_trans, header->parent_id,
493 header->commit_id);
494 if (error)
495 goto done;
496 error = gw_gen_commit_header(gw_trans, header->commit_id,
497 header->refs_str);
498 if (error)
499 goto done;
500 error = gw_gen_tree_header(gw_trans, header->tree_id);
501 if (error)
502 goto done;
503 error = gw_gen_author_header(gw_trans, header->author);
504 if (error)
505 goto done;
506 error = gw_gen_committer_header(gw_trans, header->author);
507 if (error)
508 goto done;
509 error = gw_get_time_str(&age, header->committer_time,
510 TM_LONG);
511 if (error)
512 goto done;
513 error = gw_gen_age_header(gw_trans, age ?age : "");
514 if (error)
515 goto done;
516 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
517 if (error)
518 goto done;
519 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
520 if (kerr != KCGI_OK)
521 goto done;
522 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
523 "dotted_line", KATTR__MAX);
524 if (kerr != KCGI_OK)
525 goto done;
526 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
527 if (kerr != KCGI_OK)
528 goto done;
530 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
531 "diff", KATTR__MAX);
532 if (kerr != KCGI_OK)
533 goto done;
534 error = gw_output_diff(gw_trans, header);
535 if (error)
536 goto done;
538 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
539 done:
540 gw_free_header(header);
541 free(age);
542 if (error == NULL && kerr != KCGI_OK)
543 error = gw_kcgi_error(kerr);
544 return error;
547 static const struct got_error *
548 gw_index(struct gw_trans *gw_trans)
550 const struct got_error *error = NULL;
551 struct gw_dir *gw_dir = NULL;
552 char *href_next = NULL, *href_prev = NULL, *href_summary = NULL;
553 char *href_briefs = NULL, *href_commits = NULL, *href_tree = NULL;
554 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
555 enum kcgi_err kerr;
557 if (pledge("stdio rpath proc exec sendfd unveil",
558 NULL) == -1) {
559 error = got_error_from_errno("pledge");
560 return error;
563 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path);
564 if (error)
565 return error;
567 error = gw_load_got_paths(gw_trans);
568 if (error)
569 return error;
571 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
572 "index_header", KATTR__MAX);
573 if (kerr != KCGI_OK)
574 return gw_kcgi_error(kerr);
575 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
576 "index_header_project", KATTR__MAX);
577 if (kerr != KCGI_OK)
578 return gw_kcgi_error(kerr);
579 kerr = khtml_puts(gw_trans->gw_html_req, "Project");
580 if (kerr != KCGI_OK)
581 return gw_kcgi_error(kerr);
582 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
583 if (kerr != KCGI_OK)
584 return gw_kcgi_error(kerr);
586 if (gw_trans->gw_conf->got_show_repo_description) {
587 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
588 "index_header_description", KATTR__MAX);
589 if (kerr != KCGI_OK)
590 return gw_kcgi_error(kerr);
591 kerr = khtml_puts(gw_trans->gw_html_req, "Description");
592 if (kerr != KCGI_OK)
593 return gw_kcgi_error(kerr);
594 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
595 if (kerr != KCGI_OK)
596 return gw_kcgi_error(kerr);
599 if (gw_trans->gw_conf->got_show_repo_owner) {
600 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
601 "index_header_owner", KATTR__MAX);
602 if (kerr != KCGI_OK)
603 return gw_kcgi_error(kerr);
604 kerr = khtml_puts(gw_trans->gw_html_req, "Owner");
605 if (kerr != KCGI_OK)
606 return gw_kcgi_error(kerr);
607 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
608 if (kerr != KCGI_OK)
609 return gw_kcgi_error(kerr);
612 if (gw_trans->gw_conf->got_show_repo_age) {
613 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
614 "index_header_age", KATTR__MAX);
615 if (kerr != KCGI_OK)
616 return gw_kcgi_error(kerr);
617 kerr = khtml_puts(gw_trans->gw_html_req, "Last Change");
618 if (kerr != KCGI_OK)
619 return gw_kcgi_error(kerr);
620 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
621 if (kerr != KCGI_OK)
622 return gw_kcgi_error(kerr);
625 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
626 if (kerr != KCGI_OK)
627 return gw_kcgi_error(kerr);
629 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
630 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
631 "index_wrapper", KATTR__MAX);
632 if (kerr != KCGI_OK)
633 return gw_kcgi_error(kerr);
634 kerr = khtml_puts(gw_trans->gw_html_req,
635 "No repositories found in ");
636 if (kerr != KCGI_OK)
637 return gw_kcgi_error(kerr);
638 kerr = khtml_puts(gw_trans->gw_html_req,
639 gw_trans->gw_conf->got_repos_path);
640 if (kerr != KCGI_OK)
641 return gw_kcgi_error(kerr);
642 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
643 if (kerr != KCGI_OK)
644 return gw_kcgi_error(kerr);
645 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
646 "dotted_line", KATTR__MAX);
647 if (kerr != KCGI_OK)
648 return gw_kcgi_error(kerr);
649 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
650 if (kerr != KCGI_OK)
651 return gw_kcgi_error(kerr);
652 return error;
655 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
656 dir_c++;
658 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
659 if (gw_trans->page > 0 && (gw_trans->page *
660 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
661 prev_disp++;
662 continue;
665 prev_disp++;
667 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
668 "index_wrapper", KATTR__MAX);
669 if (kerr != KCGI_OK)
670 goto done;
672 if (asprintf(&href_summary, "?path=%s&action=summary",
673 gw_dir->name) == -1) {
674 error = got_error_from_errno("asprintf");
675 goto done;
677 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
678 "index_project", KATTR__MAX);
679 if (kerr != KCGI_OK)
680 goto done;
681 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
682 href_summary, KATTR__MAX);
683 if (kerr != KCGI_OK)
684 goto done;
685 kerr = khtml_puts(gw_trans->gw_html_req, gw_dir->name);
686 if (kerr != KCGI_OK)
687 goto done;
688 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
689 if (kerr != KCGI_OK)
690 goto done;
691 if (gw_trans->gw_conf->got_show_repo_description) {
692 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
693 KATTR_ID, "index_project_description", KATTR__MAX);
694 if (kerr != KCGI_OK)
695 goto done;
696 kerr = khtml_puts(gw_trans->gw_html_req,
697 gw_dir->description ? gw_dir->description : "");
698 if (kerr != KCGI_OK)
699 goto done;
700 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
701 if (kerr != KCGI_OK)
702 goto done;
704 if (gw_trans->gw_conf->got_show_repo_owner) {
705 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
706 KATTR_ID, "index_project_owner", KATTR__MAX);
707 if (kerr != KCGI_OK)
708 goto done;
709 kerr = khtml_puts(gw_trans->gw_html_req,
710 gw_dir->owner ? gw_dir->owner : "");
711 if (kerr != KCGI_OK)
712 goto done;
713 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
714 if (kerr != KCGI_OK)
715 goto done;
717 if (gw_trans->gw_conf->got_show_repo_age) {
718 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
719 KATTR_ID, "index_project_age", KATTR__MAX);
720 if (kerr != KCGI_OK)
721 goto done;
722 kerr = khtml_puts(gw_trans->gw_html_req,
723 gw_dir->age ? gw_dir->age : "");
724 if (kerr != KCGI_OK)
725 goto done;
726 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
727 if (kerr != KCGI_OK)
728 goto done;
731 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
732 "navs_wrapper", KATTR__MAX);
733 if (kerr != KCGI_OK)
734 goto done;
735 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
736 "navs", KATTR__MAX);
737 if (kerr != KCGI_OK)
738 goto done;
740 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
741 href_summary, KATTR__MAX);
742 if (kerr != KCGI_OK)
743 goto done;
744 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
745 if (kerr != KCGI_OK)
746 goto done;
747 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
748 if (kerr != KCGI_OK)
749 goto done;
751 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
752 if (kerr != KCGI_OK)
753 goto done;
755 if (asprintf(&href_briefs, "?path=%s&action=briefs",
756 gw_dir->name) == -1) {
757 error = got_error_from_errno("asprintf");
758 goto done;
760 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
761 href_briefs, KATTR__MAX);
762 if (kerr != KCGI_OK)
763 goto done;
764 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
765 if (kerr != KCGI_OK)
766 goto done;
767 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
768 if (kerr != KCGI_OK)
769 error = gw_kcgi_error(kerr);
771 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
772 if (kerr != KCGI_OK)
773 goto done;
775 if (asprintf(&href_commits, "?path=%s&action=commits",
776 gw_dir->name) == -1) {
777 error = got_error_from_errno("asprintf");
778 goto done;
780 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
781 href_commits, KATTR__MAX);
782 if (kerr != KCGI_OK)
783 goto done;
784 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
785 if (kerr != KCGI_OK)
786 goto done;
787 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
788 if (kerr != KCGI_OK)
789 goto done;
791 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
792 if (kerr != KCGI_OK)
793 goto done;
795 if (asprintf(&href_tree, "?path=%s&action=tree",
796 gw_dir->name) == -1) {
797 error = got_error_from_errno("asprintf");
798 goto done;
800 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
801 href_tree, KATTR__MAX);
802 if (kerr != KCGI_OK)
803 goto done;
804 kerr = khtml_puts(gw_trans->gw_html_req, "tree");
805 if (kerr != KCGI_OK)
806 goto done;
808 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
809 if (kerr != KCGI_OK)
810 goto done;
811 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
812 "dotted_line", KATTR__MAX);
813 if (kerr != KCGI_OK)
814 goto done;
815 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
816 if (kerr != KCGI_OK)
817 goto done;
819 free(href_summary);
820 href_summary = NULL;
821 free(href_briefs);
822 href_briefs = NULL;
823 free(href_commits);
824 href_commits = NULL;
825 free(href_tree);
826 href_tree = NULL;
828 if (gw_trans->gw_conf->got_max_repos_display == 0)
829 continue;
831 if ((next_disp == gw_trans->gw_conf->got_max_repos_display) ||
832 ((gw_trans->gw_conf->got_max_repos_display > 0) &&
833 (gw_trans->page > 0) &&
834 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
835 prev_disp == gw_trans->repos_total))) {
836 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
837 KATTR_ID, "np_wrapper", KATTR__MAX);
838 if (kerr != KCGI_OK)
839 goto done;
840 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
841 KATTR_ID, "nav_prev", KATTR__MAX);
842 if (kerr != KCGI_OK)
843 goto done;
846 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
847 (gw_trans->page > 0) &&
848 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
849 prev_disp == gw_trans->repos_total)) {
850 if (asprintf(&href_prev, "?page=%d",
851 gw_trans->page - 1) == -1) {
852 error = got_error_from_errno("asprintf");
853 goto done;
855 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
856 KATTR_HREF, href_prev, KATTR__MAX);
857 if (kerr != KCGI_OK)
858 goto done;
859 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
860 if (kerr != KCGI_OK)
861 goto done;
862 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
863 if (kerr != KCGI_OK)
864 goto done;
867 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
868 if (kerr != KCGI_OK)
869 return gw_kcgi_error(kerr);
871 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
872 next_disp == gw_trans->gw_conf->got_max_repos_display &&
873 dir_c != (gw_trans->page + 1) *
874 gw_trans->gw_conf->got_max_repos_display) {
875 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
876 KATTR_ID, "nav_next", KATTR__MAX);
877 if (kerr != KCGI_OK)
878 goto done;
879 if (asprintf(&href_next, "?page=%d",
880 gw_trans->page + 1) == -1) {
881 error = got_error_from_errno("calloc");
882 goto done;
884 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
885 KATTR_HREF, href_next, KATTR__MAX);
886 if (kerr != KCGI_OK)
887 goto done;
888 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
889 if (kerr != KCGI_OK)
890 goto done;
891 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
892 if (kerr != KCGI_OK)
893 goto done;
894 next_disp = 0;
895 break;
898 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
899 (gw_trans->page > 0) &&
900 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
901 prev_disp == gw_trans->repos_total)) {
902 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
903 if (kerr != KCGI_OK)
904 goto done;
906 next_disp++;
908 done:
909 free(href_prev);
910 free(href_next);
911 free(href_summary);
912 free(href_briefs);
913 free(href_commits);
914 free(href_tree);
915 if (error == NULL && kerr != KCGI_OK)
916 error = gw_kcgi_error(kerr);
917 return error;
920 static const struct got_error *
921 gw_commits(struct gw_trans *gw_trans)
923 const struct got_error *error = NULL;
924 struct gw_header *header = NULL, *n_header = NULL;
925 char *age = NULL, *href_diff = NULL, *href_blob = NULL;
926 char *href_prev = NULL, *href_next = NULL;
927 enum kcgi_err kerr = KCGI_OK;
929 if ((header = gw_init_header()) == NULL)
930 return got_error_from_errno("malloc");
932 if (pledge("stdio rpath proc exec sendfd unveil",
933 NULL) == -1) {
934 error = got_error_from_errno("pledge");
935 goto done;
938 error = gw_apply_unveil(gw_trans->gw_dir->path);
939 if (error)
940 goto done;
942 error = gw_get_header(gw_trans, header,
943 gw_trans->gw_conf->got_max_commits_display);
944 if (error)
945 goto done;
947 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
948 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
949 "commits_line_wrapper", KATTR__MAX);
950 if (kerr != KCGI_OK)
951 goto done;
952 error = gw_gen_commit_header(gw_trans, n_header->commit_id,
953 n_header->refs_str);
954 if (error)
955 goto done;
956 error = gw_gen_author_header(gw_trans, n_header->author);
957 if (error)
958 goto done;
959 error = gw_gen_committer_header(gw_trans, n_header->author);
960 if (error)
961 goto done;
962 error = gw_get_time_str(&age, n_header->committer_time,
963 TM_LONG);
964 if (error)
965 goto done;
966 error = gw_gen_age_header(gw_trans, age ?age : "");
967 if (error)
968 goto done;
969 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
970 if (kerr != KCGI_OK)
971 goto done;
973 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
974 "dotted_line", KATTR__MAX);
975 if (kerr != KCGI_OK)
976 goto done;
977 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
978 if (kerr != KCGI_OK)
979 goto done;
981 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
982 "commit", KATTR__MAX);
983 if (kerr != KCGI_OK)
984 goto done;
985 kerr = khttp_puts(gw_trans->gw_req, n_header->commit_msg);
986 if (kerr != KCGI_OK)
987 goto done;
988 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
989 if (kerr != KCGI_OK)
990 goto done;
992 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
993 gw_trans->repo_name, n_header->commit_id) == -1) {
994 error = got_error_from_errno("asprintf");
995 goto done;
997 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
998 KATTR_ID, "navs_wrapper", KATTR__MAX);
999 if (kerr != KCGI_OK)
1000 goto done;
1001 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1002 KATTR_ID, "navs", KATTR__MAX);
1003 if (kerr != KCGI_OK)
1004 goto done;
1005 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1006 KATTR_HREF, href_diff, KATTR__MAX);
1007 if (kerr != KCGI_OK)
1008 goto done;
1009 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1010 if (kerr != KCGI_OK)
1011 goto done;
1012 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1013 if (kerr != KCGI_OK)
1014 goto done;
1016 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1017 if (kerr != KCGI_OK)
1018 goto done;
1020 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
1021 gw_trans->repo_name, n_header->commit_id) == -1) {
1022 error = got_error_from_errno("asprintf");
1023 goto done;
1025 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1026 KATTR_HREF, href_blob, KATTR__MAX);
1027 if (kerr != KCGI_OK)
1028 goto done;
1029 khtml_puts(gw_trans->gw_html_req, "tree");
1030 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1031 if (kerr != KCGI_OK)
1032 goto done;
1033 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1034 if (kerr != KCGI_OK)
1035 goto done;
1037 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1038 "solid_line", KATTR__MAX);
1039 if (kerr != KCGI_OK)
1040 goto done;
1041 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1042 if (kerr != KCGI_OK)
1043 goto done;
1045 free(age);
1046 age = NULL;
1049 if (gw_trans->next_id || gw_trans->page > 0) {
1050 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1051 KATTR_ID, "np_wrapper", KATTR__MAX);
1052 if (kerr != KCGI_OK)
1053 goto done;
1054 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1055 KATTR_ID, "nav_prev", KATTR__MAX);
1056 if (kerr != KCGI_OK)
1057 goto done;
1060 if (gw_trans->page > 0 && gw_trans->prev_id) {
1061 if (asprintf(&href_prev,
1062 "?path=%s&page=%d&action=commits&commit=%s&prev=%s",
1063 gw_trans->repo_name, gw_trans->page - 1,
1064 gw_trans->prev_id ? gw_trans->prev_id : "",
1065 gw_trans->prev_prev_id ?
1066 gw_trans->prev_prev_id : "") == -1) {
1067 error = got_error_from_errno("asprintf");
1068 goto done;
1070 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1071 KATTR_HREF, href_prev, KATTR__MAX);
1072 if (kerr != KCGI_OK)
1073 goto done;
1074 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1075 if (kerr != KCGI_OK)
1076 goto done;
1077 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1078 if (kerr != KCGI_OK)
1079 goto done;
1082 if (gw_trans->next_id || gw_trans->page > 0) {
1083 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1084 if (kerr != KCGI_OK)
1085 return gw_kcgi_error(kerr);
1088 if (gw_trans->next_id) {
1089 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1090 KATTR_ID, "nav_next", KATTR__MAX);
1091 if (kerr != KCGI_OK)
1092 goto done;
1093 if (asprintf(&href_next,
1094 "?path=%s&page=%d&action=commits" \
1095 "&commit=%s&prev=%s&prev_prev=%s",
1096 gw_trans->repo_name, gw_trans->page + 1,
1097 gw_trans->next_id,
1098 gw_trans->next_prev_id ? gw_trans->next_prev_id : "",
1099 gw_trans->prev_id ?
1100 gw_trans->prev_id : "") == -1) {
1101 error = got_error_from_errno("calloc");
1102 goto done;
1104 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1105 KATTR_HREF, href_next, KATTR__MAX);
1106 if (kerr != KCGI_OK)
1107 goto done;
1108 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1109 if (kerr != KCGI_OK)
1110 goto done;
1111 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1112 if (kerr != KCGI_OK)
1113 goto done;
1116 if (gw_trans->next_id || gw_trans->page > 0) {
1117 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1118 if (kerr != KCGI_OK)
1119 goto done;
1121 done:
1122 gw_free_header(header);
1123 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1124 gw_free_header(n_header);
1125 free(age);
1126 free(href_next);
1127 free(href_prev);
1128 free(href_diff);
1129 free(href_blob);
1130 if (error == NULL && kerr != KCGI_OK)
1131 error = gw_kcgi_error(kerr);
1132 return error;
1135 static const struct got_error *
1136 gw_briefs(struct gw_trans *gw_trans)
1138 const struct got_error *error = NULL;
1139 struct gw_header *header = NULL, *n_header = NULL;
1140 char *age = NULL, *href_diff = NULL, *href_blob = NULL;
1141 char *href_prev = NULL, *href_next = NULL;
1142 char *newline, *smallerthan;
1143 enum kcgi_err kerr = KCGI_OK;
1145 if ((header = gw_init_header()) == NULL)
1146 return got_error_from_errno("malloc");
1148 if (pledge("stdio rpath proc exec sendfd unveil",
1149 NULL) == -1) {
1150 error = got_error_from_errno("pledge");
1151 goto done;
1154 if (gw_trans->action != GW_SUMMARY) {
1155 error = gw_apply_unveil(gw_trans->gw_dir->path);
1156 if (error)
1157 goto done;
1160 if (gw_trans->action == GW_SUMMARY)
1161 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
1162 else
1163 error = gw_get_header(gw_trans, header,
1164 gw_trans->gw_conf->got_max_commits_display);
1165 if (error)
1166 goto done;
1168 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
1169 error = gw_get_time_str(&age, n_header->committer_time,
1170 TM_DIFF);
1171 if (error)
1172 goto done;
1174 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1175 KATTR_ID, "briefs_wrapper", KATTR__MAX);
1176 if (kerr != KCGI_OK)
1177 goto done;
1179 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1180 KATTR_ID, "briefs_age", KATTR__MAX);
1181 if (kerr != KCGI_OK)
1182 goto done;
1183 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
1184 if (kerr != KCGI_OK)
1185 goto done;
1186 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1187 if (kerr != KCGI_OK)
1188 goto done;
1190 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1191 KATTR_ID, "briefs_author", KATTR__MAX);
1192 if (kerr != KCGI_OK)
1193 goto done;
1194 smallerthan = strchr(n_header->author, '<');
1195 if (smallerthan)
1196 *smallerthan = '\0';
1197 kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
1198 if (kerr != KCGI_OK)
1199 goto done;
1200 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1201 if (kerr != KCGI_OK)
1202 goto done;
1204 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
1205 gw_trans->repo_name, n_header->commit_id) == -1) {
1206 error = got_error_from_errno("asprintf");
1207 goto done;
1209 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1210 KATTR_ID, "briefs_log", KATTR__MAX);
1211 if (kerr != KCGI_OK)
1212 goto done;
1213 newline = strchr(n_header->commit_msg, '\n');
1214 if (newline)
1215 *newline = '\0';
1216 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1217 KATTR_HREF, href_diff, KATTR__MAX);
1218 if (kerr != KCGI_OK)
1219 goto done;
1220 kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
1221 if (kerr != KCGI_OK)
1222 goto done;
1223 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1224 if (kerr != KCGI_OK)
1225 goto done;
1227 if (n_header->refs_str) {
1228 kerr = khtml_puts(gw_trans->gw_html_req, " ");
1229 if (kerr != KCGI_OK)
1230 goto done;
1231 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
1232 KATTR_ID, "refs_str", KATTR__MAX);
1233 if (kerr != KCGI_OK)
1234 goto done;
1235 kerr = khtml_puts(gw_trans->gw_html_req, "(");
1236 if (kerr != KCGI_OK)
1237 goto done;
1238 kerr = khtml_puts(gw_trans->gw_html_req,
1239 n_header->refs_str);
1240 if (kerr != KCGI_OK)
1241 goto done;
1242 kerr = khtml_puts(gw_trans->gw_html_req, ")");
1243 if (kerr != KCGI_OK)
1244 goto done;
1245 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1246 if (kerr != KCGI_OK)
1247 goto done;
1250 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1251 if (kerr != KCGI_OK)
1252 goto done;
1254 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1255 KATTR_ID, "navs_wrapper", KATTR__MAX);
1256 if (kerr != KCGI_OK)
1257 goto done;
1258 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1259 KATTR_ID, "navs", KATTR__MAX);
1260 if (kerr != KCGI_OK)
1261 goto done;
1262 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1263 KATTR_HREF, href_diff, KATTR__MAX);
1264 if (kerr != KCGI_OK)
1265 goto done;
1266 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1267 if (kerr != KCGI_OK)
1268 goto done;
1269 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1270 if (kerr != KCGI_OK)
1271 goto done;
1273 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1274 if (kerr != KCGI_OK)
1275 goto done;
1277 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
1278 gw_trans->repo_name, n_header->commit_id) == -1) {
1279 error = got_error_from_errno("asprintf");
1280 goto done;
1282 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1283 KATTR_HREF, href_blob, KATTR__MAX);
1284 if (kerr != KCGI_OK)
1285 goto done;
1286 khtml_puts(gw_trans->gw_html_req, "tree");
1287 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1288 if (kerr != KCGI_OK)
1289 goto done;
1290 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1291 if (kerr != KCGI_OK)
1292 goto done;
1294 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1295 KATTR_ID, "dotted_line", KATTR__MAX);
1296 if (kerr != KCGI_OK)
1297 goto done;
1298 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1299 if (kerr != KCGI_OK)
1300 goto done;
1302 free(age);
1303 age = NULL;
1304 free(href_diff);
1305 href_diff = NULL;
1306 free(href_blob);
1307 href_blob = NULL;
1310 if (gw_trans->next_id || gw_trans->page > 0) {
1311 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1312 KATTR_ID, "np_wrapper", KATTR__MAX);
1313 if (kerr != KCGI_OK)
1314 goto done;
1315 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1316 KATTR_ID, "nav_prev", KATTR__MAX);
1317 if (kerr != KCGI_OK)
1318 goto done;
1321 if (gw_trans->page > 0 && gw_trans->prev_id) {
1322 if (asprintf(&href_prev,
1323 "?path=%s&page=%d&action=briefs&commit=%s&prev=%s",
1324 gw_trans->repo_name, gw_trans->page - 1,
1325 gw_trans->prev_id ? gw_trans->prev_id : "",
1326 gw_trans->prev_prev_id ?
1327 gw_trans->prev_prev_id : "") == -1) {
1328 error = got_error_from_errno("asprintf");
1329 goto done;
1331 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1332 KATTR_HREF, href_prev, KATTR__MAX);
1333 if (kerr != KCGI_OK)
1334 goto done;
1335 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1336 if (kerr != KCGI_OK)
1337 goto done;
1338 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1339 if (kerr != KCGI_OK)
1340 goto done;
1343 if (gw_trans->next_id || gw_trans->page > 0) {
1344 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1345 if (kerr != KCGI_OK)
1346 return gw_kcgi_error(kerr);
1349 if (gw_trans->next_id) {
1350 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1351 KATTR_ID, "nav_next", KATTR__MAX);
1352 if (kerr != KCGI_OK)
1353 goto done;
1354 if (asprintf(&href_next,
1355 "?path=%s&page=%d&action=briefs" \
1356 "&commit=%s&prev=%s&prev_prev=%s",
1357 gw_trans->repo_name, gw_trans->page + 1,
1358 gw_trans->next_id,
1359 gw_trans->next_prev_id ? gw_trans->next_prev_id : "",
1360 gw_trans->prev_id ?
1361 gw_trans->prev_id : "") == -1) {
1362 error = got_error_from_errno("calloc");
1363 goto done;
1365 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1366 KATTR_HREF, href_next, KATTR__MAX);
1367 if (kerr != KCGI_OK)
1368 goto done;
1369 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1370 if (kerr != KCGI_OK)
1371 goto done;
1372 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1373 if (kerr != KCGI_OK)
1374 goto done;
1377 if (gw_trans->next_id || gw_trans->page > 0) {
1378 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1379 if (kerr != KCGI_OK)
1380 goto done;
1382 done:
1383 gw_free_header(header);
1384 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1385 gw_free_header(n_header);
1386 free(age);
1387 free(href_next);
1388 free(href_prev);
1389 free(href_diff);
1390 free(href_blob);
1391 if (error == NULL && kerr != KCGI_OK)
1392 error = gw_kcgi_error(kerr);
1393 return error;
1396 static const struct got_error *
1397 gw_summary(struct gw_trans *gw_trans)
1399 const struct got_error *error = NULL;
1400 char *age = NULL;
1401 enum kcgi_err kerr = KCGI_OK;
1403 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1404 return got_error_from_errno("pledge");
1406 error = gw_apply_unveil(gw_trans->gw_dir->path);
1407 if (error)
1408 goto done;
1410 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1411 "summary_wrapper", KATTR__MAX);
1412 if (kerr != KCGI_OK)
1413 return gw_kcgi_error(kerr);
1415 if (gw_trans->gw_conf->got_show_repo_description &&
1416 gw_trans->gw_dir->description != NULL &&
1417 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1418 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1419 KATTR_ID, "description_title", KATTR__MAX);
1420 if (kerr != KCGI_OK)
1421 goto done;
1422 kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1423 if (kerr != KCGI_OK)
1424 goto done;
1425 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1426 if (kerr != KCGI_OK)
1427 goto done;
1428 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1429 KATTR_ID, "description", KATTR__MAX);
1430 if (kerr != KCGI_OK)
1431 goto done;
1432 kerr = khtml_puts(gw_trans->gw_html_req,
1433 gw_trans->gw_dir->description);
1434 if (kerr != KCGI_OK)
1435 goto done;
1436 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1437 if (kerr != KCGI_OK)
1438 goto done;
1441 if (gw_trans->gw_conf->got_show_repo_owner &&
1442 gw_trans->gw_dir->owner != NULL &&
1443 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
1444 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1445 KATTR_ID, "repo_owner_title", KATTR__MAX);
1446 if (kerr != KCGI_OK)
1447 goto done;
1448 kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1449 if (kerr != KCGI_OK)
1450 goto done;
1451 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1452 if (kerr != KCGI_OK)
1453 goto done;
1454 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1455 KATTR_ID, "repo_owner", KATTR__MAX);
1456 if (kerr != KCGI_OK)
1457 goto done;
1458 kerr = khtml_puts(gw_trans->gw_html_req,
1459 gw_trans->gw_dir->owner);
1460 if (kerr != KCGI_OK)
1461 goto done;
1462 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1463 if (kerr != KCGI_OK)
1464 goto done;
1467 if (gw_trans->gw_conf->got_show_repo_age) {
1468 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1469 NULL, TM_LONG);
1470 if (error)
1471 goto done;
1472 if (age != NULL) {
1473 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1474 KATTR_ID, "last_change_title", KATTR__MAX);
1475 if (kerr != KCGI_OK)
1476 goto done;
1477 kerr = khtml_puts(gw_trans->gw_html_req,
1478 "Last Change: ");
1479 if (kerr != KCGI_OK)
1480 goto done;
1481 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1482 if (kerr != KCGI_OK)
1483 goto done;
1484 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1485 KATTR_ID, "last_change", KATTR__MAX);
1486 if (kerr != KCGI_OK)
1487 goto done;
1488 kerr = khtml_puts(gw_trans->gw_html_req, age);
1489 if (kerr != KCGI_OK)
1490 goto done;
1491 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1492 if (kerr != KCGI_OK)
1493 goto done;
1497 if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1498 gw_trans->gw_dir->url != NULL &&
1499 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1500 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1501 KATTR_ID, "cloneurl_title", KATTR__MAX);
1502 if (kerr != KCGI_OK)
1503 goto done;
1504 kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1505 if (kerr != KCGI_OK)
1506 goto done;
1507 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1508 if (kerr != KCGI_OK)
1509 goto done;
1510 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1511 KATTR_ID, "cloneurl", KATTR__MAX);
1512 if (kerr != KCGI_OK)
1513 goto done;
1514 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1515 if (kerr != KCGI_OK)
1516 goto done;
1517 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1518 if (kerr != KCGI_OK)
1519 goto done;
1522 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1523 if (kerr != KCGI_OK)
1524 goto done;
1526 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1527 "briefs_title_wrapper", KATTR__MAX);
1528 if (kerr != KCGI_OK)
1529 goto done;
1530 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1531 "briefs_title", KATTR__MAX);
1532 if (kerr != KCGI_OK)
1533 goto done;
1534 kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1535 if (kerr != KCGI_OK)
1536 goto done;
1537 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1538 if (kerr != KCGI_OK)
1539 goto done;
1540 error = gw_briefs(gw_trans);
1541 if (error)
1542 goto done;
1544 error = gw_output_repo_tags(gw_trans, NULL, D_MAXSLCOMMDISP,
1545 TAGBRIEF);
1546 if (error)
1547 goto done;
1549 error = gw_output_repo_heads(gw_trans);
1550 done:
1551 free(age);
1552 if (error == NULL && kerr != KCGI_OK)
1553 error = gw_kcgi_error(kerr);
1554 return error;
1557 static const struct got_error *
1558 gw_tree(struct gw_trans *gw_trans)
1560 const struct got_error *error = NULL;
1561 struct gw_header *header = NULL;
1562 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1563 char *age = NULL;
1564 enum kcgi_err kerr;
1566 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1567 return got_error_from_errno("pledge");
1569 if ((header = gw_init_header()) == NULL)
1570 return got_error_from_errno("malloc");
1572 error = gw_apply_unveil(gw_trans->gw_dir->path);
1573 if (error)
1574 goto done;
1576 error = gw_get_header(gw_trans, header, 1);
1577 if (error)
1578 goto done;
1580 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1581 "tree_header_wrapper", KATTR__MAX);
1582 if (kerr != KCGI_OK)
1583 goto done;
1584 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1585 "tree_header", KATTR__MAX);
1586 if (kerr != KCGI_OK)
1587 goto done;
1588 error = gw_gen_tree_header(gw_trans, header->tree_id);
1589 if (error)
1590 goto done;
1591 error = gw_get_time_str(&age, header->committer_time,
1592 TM_LONG);
1593 if (error)
1594 goto done;
1595 error = gw_gen_age_header(gw_trans, age ?age : "");
1596 if (error)
1597 goto done;
1598 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1599 if (error)
1600 goto done;
1601 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1602 if (kerr != KCGI_OK)
1603 goto done;
1604 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1605 "dotted_line", KATTR__MAX);
1606 if (kerr != KCGI_OK)
1607 goto done;
1608 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1609 if (kerr != KCGI_OK)
1610 goto done;
1612 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1613 "tree", KATTR__MAX);
1614 if (kerr != KCGI_OK)
1615 goto done;
1616 error = gw_output_repo_tree(gw_trans);
1617 if (error)
1618 goto done;
1620 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1621 done:
1622 gw_free_header(header);
1623 free(tree_html_disp);
1624 free(tree_html);
1625 free(tree);
1626 free(age);
1627 if (error == NULL && kerr != KCGI_OK)
1628 error = gw_kcgi_error(kerr);
1629 return error;
1632 static const struct got_error *
1633 gw_tag(struct gw_trans *gw_trans)
1635 const struct got_error *error = NULL;
1636 struct gw_header *header = NULL;
1637 enum kcgi_err kerr;
1639 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1640 return got_error_from_errno("pledge");
1642 if ((header = gw_init_header()) == NULL)
1643 return got_error_from_errno("malloc");
1645 error = gw_apply_unveil(gw_trans->gw_dir->path);
1646 if (error)
1647 goto done;
1649 if (gw_trans->commit_id == NULL) {
1650 error = got_error_msg(GOT_ERR_QUERYSTRING,
1651 "commit required in querystring");
1652 goto done;
1655 error = gw_get_header(gw_trans, header, 1);
1656 if (error)
1657 goto done;
1659 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1660 "tag_header_wrapper", KATTR__MAX);
1661 if (kerr != KCGI_OK)
1662 goto done;
1663 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1664 "tag_header", KATTR__MAX);
1665 if (kerr != KCGI_OK)
1666 goto done;
1667 error = gw_gen_commit_header(gw_trans, header->commit_id,
1668 header->refs_str);
1669 if (error)
1670 goto done;
1671 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1672 if (error)
1673 goto done;
1674 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1675 if (kerr != KCGI_OK)
1676 goto done;
1677 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1678 "dotted_line", KATTR__MAX);
1679 if (kerr != KCGI_OK)
1680 goto done;
1681 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1682 if (kerr != KCGI_OK)
1683 goto done;
1685 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1686 "tree", KATTR__MAX);
1687 if (kerr != KCGI_OK)
1688 goto done;
1690 error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1691 if (error)
1692 goto done;
1694 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1695 done:
1696 gw_free_header(header);
1697 if (error == NULL && kerr != KCGI_OK)
1698 error = gw_kcgi_error(kerr);
1699 return error;
1702 static const struct got_error *
1703 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1705 const struct got_error *error = NULL;
1706 DIR *dt;
1707 char *dir_test;
1708 int opened = 0;
1710 if (asprintf(&dir_test, "%s/%s/%s",
1711 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1712 GOTWEB_GIT_DIR) == -1)
1713 return got_error_from_errno("asprintf");
1715 dt = opendir(dir_test);
1716 if (dt == NULL) {
1717 free(dir_test);
1718 } else {
1719 gw_dir->path = strdup(dir_test);
1720 if (gw_dir->path == NULL) {
1721 opened = 1;
1722 error = got_error_from_errno("strdup");
1723 goto errored;
1725 opened = 1;
1726 goto done;
1729 if (asprintf(&dir_test, "%s/%s/%s",
1730 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1731 GOTWEB_GOT_DIR) == -1) {
1732 dir_test = NULL;
1733 error = got_error_from_errno("asprintf");
1734 goto errored;
1737 dt = opendir(dir_test);
1738 if (dt == NULL)
1739 free(dir_test);
1740 else {
1741 opened = 1;
1742 error = got_error(GOT_ERR_NOT_GIT_REPO);
1743 goto errored;
1746 if (asprintf(&dir_test, "%s/%s",
1747 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1) {
1748 error = got_error_from_errno("asprintf");
1749 dir_test = NULL;
1750 goto errored;
1753 gw_dir->path = strdup(dir_test);
1754 if (gw_dir->path == NULL) {
1755 opened = 1;
1756 error = got_error_from_errno("strdup");
1757 goto errored;
1760 dt = opendir(dir_test);
1761 if (dt == NULL) {
1762 error = got_error_path(gw_dir->name, GOT_ERR_NOT_GIT_REPO);
1763 goto errored;
1764 } else
1765 opened = 1;
1766 done:
1767 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1768 gw_dir->path);
1769 if (error)
1770 goto errored;
1771 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1772 if (error)
1773 goto errored;
1774 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1775 NULL, TM_DIFF);
1776 if (error)
1777 goto errored;
1778 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1779 errored:
1780 free(dir_test);
1781 if (opened)
1782 if (dt && closedir(dt) == -1 && error == NULL)
1783 error = got_error_from_errno("closedir");
1784 return error;
1787 static const struct got_error *
1788 gw_load_got_paths(struct gw_trans *gw_trans)
1790 const struct got_error *error = NULL;
1791 DIR *d;
1792 struct dirent **sd_dent;
1793 struct gw_dir *gw_dir;
1794 struct stat st;
1795 unsigned int d_cnt, d_i;
1797 d = opendir(gw_trans->gw_conf->got_repos_path);
1798 if (d == NULL) {
1799 error = got_error_from_errno2("opendir",
1800 gw_trans->gw_conf->got_repos_path);
1801 return error;
1804 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1805 alphasort);
1806 if (d_cnt == -1) {
1807 error = got_error_from_errno2("scandir",
1808 gw_trans->gw_conf->got_repos_path);
1809 goto done;
1812 for (d_i = 0; d_i < d_cnt; d_i++) {
1813 if (gw_trans->gw_conf->got_max_repos > 0 &&
1814 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1815 break; /* account for parent and self */
1817 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1818 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1819 continue;
1821 error = gw_init_gw_dir(&gw_dir, sd_dent[d_i]->d_name);
1822 if (error)
1823 goto done;
1825 error = gw_load_got_path(gw_trans, gw_dir);
1826 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1827 error = NULL;
1828 continue;
1830 else if (error)
1831 goto done;
1833 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1834 !got_path_dir_is_empty(gw_dir->path)) {
1835 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1836 entry);
1837 gw_trans->repos_total++;
1840 done:
1841 if (d && closedir(d) == -1 && error == NULL)
1842 error = got_error_from_errno("closedir");
1843 return error;
1846 static const struct got_error *
1847 gw_parse_querystring(struct gw_trans *gw_trans)
1849 const struct got_error *error = NULL;
1850 struct kpair *p;
1851 struct gw_query_action *action = NULL;
1852 unsigned int i;
1854 if (gw_trans->gw_req->fieldnmap[0]) {
1855 return got_error(GOT_ERR_QUERYSTRING);
1856 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1857 /* define gw_trans->repo_path */
1858 gw_trans->repo_name = p->parsed.s;
1860 if (asprintf(&gw_trans->repo_path, "%s/%s",
1861 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
1862 return got_error_from_errno("asprintf");
1864 /* get action and set function */
1865 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION])) {
1866 for (i = 0; i < nitems(gw_query_funcs); i++) {
1867 action = &gw_query_funcs[i];
1868 if (action->func_name == NULL)
1869 continue;
1870 if (strcmp(action->func_name,
1871 p->parsed.s) == 0) {
1872 gw_trans->action = i;
1873 break;
1877 if (gw_trans->action == -1) {
1878 gw_trans->action = GW_ERR;
1879 gw_trans->error = got_error_msg(GOT_ERR_QUERYSTRING,
1880 p != NULL ? "bad action in querystring" :
1881 "no action in querystring");
1882 return error;
1885 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID])) {
1886 if (asprintf(&gw_trans->commit_id, "%s",
1887 p->parsed.s) == -1)
1888 return got_error_from_errno("asprintf");
1891 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1892 gw_trans->repo_file = p->parsed.s;
1894 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER])) {
1895 if (asprintf(&gw_trans->repo_folder, "%s",
1896 p->parsed.s) == -1)
1897 return got_error_from_errno("asprintf");
1900 if ((p = gw_trans->gw_req->fieldmap[KEY_PREV_ID])) {
1901 if (asprintf(&gw_trans->prev_id, "%s",
1902 p->parsed.s) == -1)
1903 return got_error_from_errno("asprintf");
1906 if ((p = gw_trans->gw_req->fieldmap[KEY_PREV_PREV_ID])) {
1907 if (asprintf(&gw_trans->prev_prev_id, "%s",
1908 p->parsed.s) == -1)
1909 return got_error_from_errno("asprintf");
1912 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
1913 gw_trans->headref = p->parsed.s;
1915 error = gw_init_gw_dir(&gw_trans->gw_dir, gw_trans->repo_name);
1916 if (error)
1917 return error;
1919 gw_trans->error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1920 } else
1921 gw_trans->action = GW_INDEX;
1923 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1924 gw_trans->page = p->parsed.i;
1926 return error;
1929 static const struct got_error *
1930 gw_init_gw_dir(struct gw_dir **gw_dir, const char *dir)
1932 const struct got_error *error;
1934 *gw_dir = malloc(sizeof(**gw_dir));
1935 if (*gw_dir == NULL)
1936 return got_error_from_errno("malloc");
1938 if (asprintf(&(*gw_dir)->name, "%s", dir) == -1) {
1939 error = got_error_from_errno("asprintf");
1940 free(*gw_dir);
1941 *gw_dir = NULL;
1942 return NULL;
1945 return NULL;
1948 static const struct got_error *
1949 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1951 enum kcgi_err kerr;
1953 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1954 if (kerr != KCGI_OK)
1955 return gw_kcgi_error(kerr);
1956 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1957 khttps[code]);
1958 if (kerr != KCGI_OK)
1959 return gw_kcgi_error(kerr);
1960 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1961 kmimetypes[mime]);
1962 if (kerr != KCGI_OK)
1963 return gw_kcgi_error(kerr);
1964 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
1965 "nosniff");
1966 if (kerr != KCGI_OK)
1967 return gw_kcgi_error(kerr);
1968 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1969 if (kerr != KCGI_OK)
1970 return gw_kcgi_error(kerr);
1971 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
1972 "1; mode=block");
1973 if (kerr != KCGI_OK)
1974 return gw_kcgi_error(kerr);
1976 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
1977 kerr = khttp_head(gw_trans->gw_req,
1978 kresps[KRESP_CONTENT_DISPOSITION],
1979 "attachment; filename=%s", gw_trans->repo_file);
1980 if (kerr != KCGI_OK)
1981 return gw_kcgi_error(kerr);
1984 kerr = khttp_body(gw_trans->gw_req);
1985 return gw_kcgi_error(kerr);
1988 static const struct got_error *
1989 gw_display_index(struct gw_trans *gw_trans)
1991 const struct got_error *error;
1992 enum kcgi_err kerr;
1994 /* catch early querystring errors */
1995 if (gw_trans->error)
1996 gw_trans->action = GW_ERR;
1998 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1999 if (error)
2000 return error;
2002 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
2003 if (kerr != KCGI_OK)
2004 return gw_kcgi_error(kerr);
2006 if (gw_trans->action != GW_BLOB) {
2007 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
2008 gw_query_funcs[gw_trans->action].template);
2009 if (kerr != KCGI_OK) {
2010 khtml_close(gw_trans->gw_html_req);
2011 return gw_kcgi_error(kerr);
2015 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
2018 static const struct got_error *
2019 gw_error(struct gw_trans *gw_trans)
2021 enum kcgi_err kerr;
2023 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->error->msg);
2025 return gw_kcgi_error(kerr);
2028 static int
2029 gw_template(size_t key, void *arg)
2031 const struct got_error *error = NULL;
2032 enum kcgi_err kerr;
2033 struct gw_trans *gw_trans = arg;
2034 char *img_src = NULL;
2036 switch (key) {
2037 case (TEMPL_HEAD):
2038 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2039 KATTR_NAME, "viewport",
2040 KATTR_CONTENT, "initial-scale=.75, user-scalable=yes",
2041 KATTR__MAX);
2042 if (kerr != KCGI_OK)
2043 return 0;
2044 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2045 if (kerr != KCGI_OK)
2046 return 0;
2047 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2048 KATTR_CHARSET, "utf-8",
2049 KATTR__MAX);
2050 if (kerr != KCGI_OK)
2051 return 0;
2052 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2053 if (kerr != KCGI_OK)
2054 return 0;
2055 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2056 KATTR_NAME, "msapplication-TileColor",
2057 KATTR_CONTENT, "#da532c", KATTR__MAX);
2058 if (kerr != KCGI_OK)
2059 return 0;
2060 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2061 if (kerr != KCGI_OK)
2062 return 0;
2063 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2064 KATTR_NAME, "theme-color",
2065 KATTR_CONTENT, "#ffffff", KATTR__MAX);
2066 if (kerr != KCGI_OK)
2067 return 0;
2068 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2069 if (kerr != KCGI_OK)
2070 return 0;
2071 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2072 KATTR_REL, "apple-touch-icon", KATTR_SIZES, "180x180",
2073 KATTR_HREF, "/apple-touch-icon.png", KATTR__MAX);
2074 if (kerr != KCGI_OK)
2075 return 0;
2076 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2077 if (kerr != KCGI_OK)
2078 return 0;
2079 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2080 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2081 "32x32", KATTR_HREF, "/favicon-32x32.png", KATTR__MAX);
2082 if (kerr != KCGI_OK)
2083 return 0;
2084 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2085 if (kerr != KCGI_OK)
2086 return 0;
2087 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2088 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2089 "16x16", KATTR_HREF, "/favicon-16x16.png", KATTR__MAX);
2090 if (kerr != KCGI_OK)
2091 return 0;
2092 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2093 if (kerr != KCGI_OK)
2094 return 0;
2095 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2096 KATTR_REL, "manifest", KATTR_HREF, "/site.webmanifest",
2097 KATTR__MAX);
2098 if (kerr != KCGI_OK)
2099 return 0;
2100 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2101 if (kerr != KCGI_OK)
2102 return 0;
2103 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2104 KATTR_REL, "mask-icon", KATTR_HREF,
2105 "/safari-pinned-tab.svg", KATTR__MAX);
2106 if (kerr != KCGI_OK)
2107 return 0;
2108 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2109 if (kerr != KCGI_OK)
2110 return 0;
2111 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2112 KATTR_REL, "stylesheet", KATTR_TYPE, "text/css",
2113 KATTR_HREF, "/gotweb.css", KATTR__MAX);
2114 if (kerr != KCGI_OK)
2115 return 0;
2116 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2117 if (kerr != KCGI_OK)
2118 return 0;
2119 break;
2120 case(TEMPL_HEADER):
2121 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2122 KATTR_ID, "got_link", KATTR__MAX);
2123 if (kerr != KCGI_OK)
2124 return 0;
2125 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2126 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
2127 KATTR_TARGET, "_sotd", KATTR__MAX);
2128 if (kerr != KCGI_OK)
2129 return 0;
2130 if (asprintf(&img_src, "/%s",
2131 gw_trans->gw_conf->got_logo) == -1)
2132 return 0;
2133 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
2134 KATTR_SRC, img_src, KATTR__MAX);
2135 if (kerr != KCGI_OK) {
2136 free(img_src);
2137 return 0;
2139 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2140 if (kerr != KCGI_OK) {
2141 free(img_src);
2142 return 0;
2144 break;
2145 case (TEMPL_SITEPATH):
2146 error = gw_output_site_link(gw_trans);
2147 if (error)
2148 return 0;
2149 break;
2150 case(TEMPL_TITLE):
2151 if (gw_trans->gw_conf->got_site_name != NULL) {
2152 kerr = khtml_puts(gw_trans->gw_html_req,
2153 gw_trans->gw_conf->got_site_name);
2154 if (kerr != KCGI_OK)
2155 return 0;
2157 break;
2158 case (TEMPL_SEARCH):
2159 break;
2160 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
2161 "search", KATTR__MAX);
2162 if (kerr != KCGI_OK)
2163 return 0;
2164 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
2165 KATTR_METHOD, "POST", KATTR__MAX);
2166 if (kerr != KCGI_OK)
2167 return 0;
2168 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
2169 "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
2170 KATTR_MAXLENGTH, "50", KATTR__MAX);
2171 if (kerr != KCGI_OK)
2172 return 0;
2173 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
2174 KATTR__MAX);
2175 if (kerr != KCGI_OK)
2176 return 0;
2177 kerr = khtml_puts(gw_trans->gw_html_req, "Search");
2178 if (kerr != KCGI_OK)
2179 return 0;
2180 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
2181 if (kerr != KCGI_OK)
2182 return 0;
2183 break;
2184 case(TEMPL_SITEOWNER):
2185 if (gw_trans->gw_conf->got_site_owner != NULL &&
2186 gw_trans->gw_conf->got_show_site_owner) {
2187 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2188 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
2189 if (kerr != KCGI_OK)
2190 return 0;
2191 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2192 KATTR_ID, "site_owner", KATTR__MAX);
2193 if (kerr != KCGI_OK)
2194 return 0;
2195 kerr = khtml_puts(gw_trans->gw_html_req,
2196 gw_trans->gw_conf->got_site_owner);
2197 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2198 if (kerr != KCGI_OK)
2199 return 0;
2201 break;
2202 case(TEMPL_CONTENT):
2203 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
2204 if (error) {
2205 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2206 KATTR_ID, "tmpl_err", KATTR__MAX);
2207 if (kerr != KCGI_OK)
2208 return 0;
2209 kerr = khttp_puts(gw_trans->gw_req, "Error: ");
2210 if (kerr != KCGI_OK)
2211 return 0;
2212 kerr = khttp_puts(gw_trans->gw_req, error->msg);
2213 if (kerr != KCGI_OK)
2214 return 0;
2215 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2216 if (kerr != KCGI_OK)
2217 return 0;
2219 break;
2220 default:
2221 return 0;
2223 return 1;
2226 static const struct got_error *
2227 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
2229 const struct got_error *error = NULL;
2230 enum kcgi_err kerr = KCGI_OK;
2232 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2233 KATTR_ID, "header_commit_title", KATTR__MAX);
2234 if (kerr != KCGI_OK)
2235 goto done;
2236 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
2237 if (kerr != KCGI_OK)
2238 goto done;
2239 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2240 if (kerr != KCGI_OK)
2241 goto done;
2242 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2243 KATTR_ID, "header_commit", KATTR__MAX);
2244 if (kerr != KCGI_OK)
2245 goto done;
2246 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2247 if (kerr != KCGI_OK)
2248 goto done;
2249 kerr = khtml_puts(gw_trans->gw_html_req, " ");
2250 if (kerr != KCGI_OK)
2251 goto done;
2252 if (str2 != NULL) {
2253 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
2254 KATTR_ID, "refs_str", KATTR__MAX);
2255 if (kerr != KCGI_OK)
2256 goto done;
2257 kerr = khtml_puts(gw_trans->gw_html_req, "(");
2258 if (kerr != KCGI_OK)
2259 goto done;
2260 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2261 if (kerr != KCGI_OK)
2262 goto done;
2263 kerr = khtml_puts(gw_trans->gw_html_req, ")");
2264 if (kerr != KCGI_OK)
2265 goto done;
2266 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2267 if (kerr != KCGI_OK)
2268 goto done;
2270 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2271 done:
2272 if (error == NULL && kerr != KCGI_OK)
2273 error = gw_kcgi_error(kerr);
2274 return error;
2277 static const struct got_error *
2278 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2280 const struct got_error *error = NULL;
2281 enum kcgi_err kerr = KCGI_OK;
2283 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2284 KATTR_ID, "header_diff_title", KATTR__MAX);
2285 if (kerr != KCGI_OK)
2286 goto done;
2287 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2288 if (kerr != KCGI_OK)
2289 goto done;
2290 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2291 if (kerr != KCGI_OK)
2292 goto done;
2293 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2294 KATTR_ID, "header_diff", KATTR__MAX);
2295 if (kerr != KCGI_OK)
2296 goto done;
2297 if (str1 != NULL) {
2298 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2299 if (kerr != KCGI_OK)
2300 goto done;
2302 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2303 if (kerr != KCGI_OK)
2304 goto done;
2305 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2306 if (kerr != KCGI_OK)
2307 goto done;
2308 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2309 done:
2310 if (error == NULL && kerr != KCGI_OK)
2311 error = gw_kcgi_error(kerr);
2312 return error;
2315 static const struct got_error *
2316 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2318 const struct got_error *error = NULL;
2319 enum kcgi_err kerr = KCGI_OK;
2321 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2322 KATTR_ID, "header_age_title", KATTR__MAX);
2323 if (kerr != KCGI_OK)
2324 goto done;
2325 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2326 if (kerr != KCGI_OK)
2327 goto done;
2328 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2329 if (kerr != KCGI_OK)
2330 goto done;
2331 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2332 KATTR_ID, "header_age", KATTR__MAX);
2333 if (kerr != KCGI_OK)
2334 goto done;
2335 kerr = khtml_puts(gw_trans->gw_html_req, str);
2336 if (kerr != KCGI_OK)
2337 goto done;
2338 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2339 done:
2340 if (error == NULL && kerr != KCGI_OK)
2341 error = gw_kcgi_error(kerr);
2342 return error;
2345 static const struct got_error *
2346 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2348 const struct got_error *error = NULL;
2349 enum kcgi_err kerr;
2351 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2352 KATTR_ID, "header_author_title", KATTR__MAX);
2353 if (kerr != KCGI_OK)
2354 goto done;
2355 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2356 if (kerr != KCGI_OK)
2357 goto done;
2358 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2359 if (kerr != KCGI_OK)
2360 goto done;
2361 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2362 KATTR_ID, "header_author", KATTR__MAX);
2363 if (kerr != KCGI_OK)
2364 goto done;
2365 kerr = khtml_puts(gw_trans->gw_html_req, str);
2366 if (kerr != KCGI_OK)
2367 goto done;
2368 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2369 done:
2370 if (error == NULL && kerr != KCGI_OK)
2371 error = gw_kcgi_error(kerr);
2372 return error;
2375 static const struct got_error *
2376 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2378 const struct got_error *error = NULL;
2379 enum kcgi_err kerr;
2381 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2382 KATTR_ID, "header_committer_title", KATTR__MAX);
2383 if (kerr != KCGI_OK)
2384 goto done;
2385 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2386 if (kerr != KCGI_OK)
2387 goto done;
2388 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2389 if (kerr != KCGI_OK)
2390 goto done;
2391 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2392 KATTR_ID, "header_committer", KATTR__MAX);
2393 if (kerr != KCGI_OK)
2394 goto done;
2395 kerr = khtml_puts(gw_trans->gw_html_req, str);
2396 if (kerr != KCGI_OK)
2397 goto done;
2398 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2399 done:
2400 if (error == NULL && kerr != KCGI_OK)
2401 error = gw_kcgi_error(kerr);
2402 return error;
2405 static const struct got_error *
2406 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2408 const struct got_error *error = NULL;
2409 enum kcgi_err kerr;
2411 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2412 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2413 if (kerr != KCGI_OK)
2414 goto done;
2415 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2416 if (kerr != KCGI_OK)
2417 goto done;
2418 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2419 if (kerr != KCGI_OK)
2420 goto done;
2421 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2422 KATTR_ID, "header_commit_msg", KATTR__MAX);
2423 if (kerr != KCGI_OK)
2424 goto done;
2425 kerr = khttp_puts(gw_trans->gw_req, str);
2426 if (kerr != KCGI_OK)
2427 goto done;
2428 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2429 done:
2430 if (error == NULL && kerr != KCGI_OK)
2431 error = gw_kcgi_error(kerr);
2432 return error;
2435 static const struct got_error *
2436 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2438 const struct got_error *error = NULL;
2439 enum kcgi_err kerr;
2441 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2442 KATTR_ID, "header_tree_title", KATTR__MAX);
2443 if (kerr != KCGI_OK)
2444 goto done;
2445 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2446 if (kerr != KCGI_OK)
2447 goto done;
2448 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2449 if (kerr != KCGI_OK)
2450 goto done;
2451 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2452 KATTR_ID, "header_tree", KATTR__MAX);
2453 if (kerr != KCGI_OK)
2454 goto done;
2455 kerr = khtml_puts(gw_trans->gw_html_req, str);
2456 if (kerr != KCGI_OK)
2457 goto done;
2458 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2459 done:
2460 if (error == NULL && kerr != KCGI_OK)
2461 error = gw_kcgi_error(kerr);
2462 return error;
2465 static const struct got_error *
2466 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2467 char *dir)
2469 const struct got_error *error = NULL;
2470 FILE *f = NULL;
2471 char *d_file = NULL;
2472 unsigned int len;
2473 size_t n;
2475 *description = NULL;
2476 if (gw_trans->gw_conf->got_show_repo_description == 0)
2477 return NULL;
2479 if (asprintf(&d_file, "%s/description", dir) == -1)
2480 return got_error_from_errno("asprintf");
2482 f = fopen(d_file, "r");
2483 if (f == NULL) {
2484 if (errno == ENOENT || errno == EACCES)
2485 return NULL;
2486 error = got_error_from_errno2("fopen", d_file);
2487 goto done;
2490 if (fseek(f, 0, SEEK_END) == -1) {
2491 error = got_ferror(f, GOT_ERR_IO);
2492 goto done;
2494 len = ftell(f);
2495 if (len == -1) {
2496 error = got_ferror(f, GOT_ERR_IO);
2497 goto done;
2499 if (fseek(f, 0, SEEK_SET) == -1) {
2500 error = got_ferror(f, GOT_ERR_IO);
2501 goto done;
2503 *description = calloc(len + 1, sizeof(**description));
2504 if (*description == NULL) {
2505 error = got_error_from_errno("calloc");
2506 goto done;
2509 n = fread(*description, 1, len, f);
2510 if (n == 0 && ferror(f))
2511 error = got_ferror(f, GOT_ERR_IO);
2512 done:
2513 if (f != NULL && fclose(f) == -1 && error == NULL)
2514 error = got_error_from_errno("fclose");
2515 free(d_file);
2516 return error;
2519 static const struct got_error *
2520 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2522 struct tm tm;
2523 time_t diff_time;
2524 char *years = "years ago", *months = "months ago";
2525 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2526 char *minutes = "minutes ago", *seconds = "seconds ago";
2527 char *now = "right now";
2528 char *s;
2529 char datebuf[29];
2531 *repo_age = NULL;
2533 switch (ref_tm) {
2534 case TM_DIFF:
2535 diff_time = time(NULL) - committer_time;
2536 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2537 if (asprintf(repo_age, "%lld %s",
2538 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2539 return got_error_from_errno("asprintf");
2540 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2541 if (asprintf(repo_age, "%lld %s",
2542 (diff_time / 60 / 60 / 24 / (365 / 12)),
2543 months) == -1)
2544 return got_error_from_errno("asprintf");
2545 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2546 if (asprintf(repo_age, "%lld %s",
2547 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2548 return got_error_from_errno("asprintf");
2549 } else if (diff_time > 60 * 60 * 24 * 2) {
2550 if (asprintf(repo_age, "%lld %s",
2551 (diff_time / 60 / 60 / 24), days) == -1)
2552 return got_error_from_errno("asprintf");
2553 } else if (diff_time > 60 * 60 * 2) {
2554 if (asprintf(repo_age, "%lld %s",
2555 (diff_time / 60 / 60), hours) == -1)
2556 return got_error_from_errno("asprintf");
2557 } else if (diff_time > 60 * 2) {
2558 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2559 minutes) == -1)
2560 return got_error_from_errno("asprintf");
2561 } else if (diff_time > 2) {
2562 if (asprintf(repo_age, "%lld %s", diff_time,
2563 seconds) == -1)
2564 return got_error_from_errno("asprintf");
2565 } else {
2566 if (asprintf(repo_age, "%s", now) == -1)
2567 return got_error_from_errno("asprintf");
2569 break;
2570 case TM_LONG:
2571 if (gmtime_r(&committer_time, &tm) == NULL)
2572 return got_error_from_errno("gmtime_r");
2574 s = asctime_r(&tm, datebuf);
2575 if (s == NULL)
2576 return got_error_from_errno("asctime_r");
2578 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2579 return got_error_from_errno("asprintf");
2580 break;
2582 return NULL;
2585 static const struct got_error *
2586 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2587 const char *refname, int ref_tm)
2589 const struct got_error *error = NULL;
2590 struct got_repository *repo = NULL;
2591 struct got_commit_object *commit = NULL;
2592 struct got_reflist_head refs;
2593 struct got_reflist_entry *re;
2594 time_t committer_time = 0, cmp_time = 0;
2596 *repo_age = NULL;
2597 SIMPLEQ_INIT(&refs);
2599 if (gw_trans->gw_conf->got_show_repo_age == 0)
2600 return NULL;
2602 if (gw_trans->repo)
2603 repo = gw_trans->repo;
2604 else {
2605 error = got_repo_open(&repo, dir, NULL);
2606 if (error)
2607 return error;
2610 error = got_ref_list(&refs, repo, "refs/heads",
2611 got_ref_cmp_by_name, NULL);
2612 if (error)
2613 goto done;
2616 * Find the youngest branch tip in the repository, or the age of
2617 * the a specific branch tip if a name was provided by the caller.
2619 SIMPLEQ_FOREACH(re, &refs, entry) {
2620 struct got_object_id *id = NULL;
2622 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
2623 continue;
2625 error = got_ref_resolve(&id, repo, re->ref);
2626 if (error)
2627 goto done;
2629 error = got_object_open_as_commit(&commit, repo, id);
2630 free(id);
2631 if (error)
2632 goto done;
2634 committer_time =
2635 got_object_commit_get_committer_time(commit);
2636 got_object_commit_close(commit);
2637 if (cmp_time < committer_time)
2638 cmp_time = committer_time;
2640 if (refname)
2641 break;
2644 if (cmp_time != 0) {
2645 committer_time = cmp_time;
2646 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2648 done:
2649 got_ref_list_free(&refs);
2650 if (gw_trans->repo == NULL)
2651 got_repo_close(repo);
2652 return error;
2655 static const struct got_error *
2656 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2658 const struct got_error *error;
2659 FILE *f = NULL;
2660 struct got_object_id *id1 = NULL, *id2 = NULL;
2661 char *label1 = NULL, *label2 = NULL, *line = NULL;
2662 int obj_type;
2663 size_t linesize = 0;
2664 ssize_t linelen;
2665 enum kcgi_err kerr = KCGI_OK;
2667 f = got_opentemp();
2668 if (f == NULL)
2669 return NULL;
2671 if (header->parent_id != NULL &&
2672 strncmp(header->parent_id, "/dev/null", 9) != 0) {
2673 error = got_repo_match_object_id(&id1, &label1,
2674 header->parent_id, GOT_OBJ_TYPE_ANY, 1, gw_trans->repo);
2675 if (error)
2676 goto done;
2679 error = got_repo_match_object_id(&id2, &label2,
2680 header->commit_id, GOT_OBJ_TYPE_ANY, 1, gw_trans->repo);
2681 if (error)
2682 goto done;
2684 error = got_object_get_type(&obj_type, gw_trans->repo, id2);
2685 if (error)
2686 goto done;
2687 switch (obj_type) {
2688 case GOT_OBJ_TYPE_BLOB:
2689 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
2690 gw_trans->repo, f);
2691 break;
2692 case GOT_OBJ_TYPE_TREE:
2693 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
2694 gw_trans->repo, f);
2695 break;
2696 case GOT_OBJ_TYPE_COMMIT:
2697 error = got_diff_objects_as_commits(id1, id2, 3, 0,
2698 gw_trans->repo, f);
2699 break;
2700 default:
2701 error = got_error(GOT_ERR_OBJ_TYPE);
2703 if (error)
2704 goto done;
2706 if (fseek(f, 0, SEEK_SET) == -1) {
2707 error = got_ferror(f, GOT_ERR_IO);
2708 goto done;
2711 while ((linelen = getline(&line, &linesize, f)) != -1) {
2712 error = gw_colordiff_line(gw_trans, line);
2713 if (error)
2714 goto done;
2715 /* XXX: KHTML_PRETTY breaks this */
2716 kerr = khtml_puts(gw_trans->gw_html_req, line);
2717 if (kerr != KCGI_OK)
2718 goto done;
2719 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2720 if (kerr != KCGI_OK)
2721 goto done;
2723 if (linelen == -1 && ferror(f))
2724 error = got_error_from_errno("getline");
2725 done:
2726 if (f && fclose(f) == -1 && error == NULL)
2727 error = got_error_from_errno("fclose");
2728 free(line);
2729 free(label1);
2730 free(label2);
2731 free(id1);
2732 free(id2);
2734 if (error == NULL && kerr != KCGI_OK)
2735 error = gw_kcgi_error(kerr);
2736 return error;
2739 static const struct got_error *
2740 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2742 const struct got_error *error = NULL;
2743 struct got_repository *repo;
2744 const char *gitconfig_owner;
2746 *owner = NULL;
2748 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2749 return NULL;
2751 error = got_repo_open(&repo, dir, NULL);
2752 if (error)
2753 return error;
2754 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2755 if (gitconfig_owner) {
2756 *owner = strdup(gitconfig_owner);
2757 if (*owner == NULL)
2758 error = got_error_from_errno("strdup");
2760 got_repo_close(repo);
2761 return error;
2764 static const struct got_error *
2765 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2767 const struct got_error *error = NULL;
2768 FILE *f;
2769 char *d_file = NULL;
2770 unsigned int len;
2771 size_t n;
2773 *url = NULL;
2775 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2776 return got_error_from_errno("asprintf");
2778 f = fopen(d_file, "r");
2779 if (f == NULL) {
2780 if (errno != ENOENT && errno != EACCES)
2781 error = got_error_from_errno2("fopen", d_file);
2782 goto done;
2785 if (fseek(f, 0, SEEK_END) == -1) {
2786 error = got_ferror(f, GOT_ERR_IO);
2787 goto done;
2789 len = ftell(f);
2790 if (len == -1) {
2791 error = got_ferror(f, GOT_ERR_IO);
2792 goto done;
2794 if (fseek(f, 0, SEEK_SET) == -1) {
2795 error = got_ferror(f, GOT_ERR_IO);
2796 goto done;
2799 *url = calloc(len + 1, sizeof(**url));
2800 if (*url == NULL) {
2801 error = got_error_from_errno("calloc");
2802 goto done;
2805 n = fread(*url, 1, len, f);
2806 if (n == 0 && ferror(f))
2807 error = got_ferror(f, GOT_ERR_IO);
2808 done:
2809 if (f && fclose(f) == -1 && error == NULL)
2810 error = got_error_from_errno("fclose");
2811 free(d_file);
2812 return NULL;
2815 static const struct got_error *
2816 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
2817 int limit, int tag_type)
2819 const struct got_error *error = NULL;
2820 struct got_reflist_head refs;
2821 struct got_reflist_entry *re;
2822 char *age = NULL;
2823 char *id_str = NULL, *newline, *href_commits = NULL;
2824 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
2825 struct got_tag_object *tag = NULL;
2826 enum kcgi_err kerr = KCGI_OK;
2827 int summary_header_displayed = 0;
2829 SIMPLEQ_INIT(&refs);
2831 error = got_ref_list(&refs, gw_trans->repo, "refs/tags",
2832 got_ref_cmp_tags, gw_trans->repo);
2833 if (error)
2834 goto done;
2836 SIMPLEQ_FOREACH(re, &refs, entry) {
2837 const char *refname;
2838 const char *tagger;
2839 const char *tag_commit;
2840 time_t tagger_time;
2841 struct got_object_id *id;
2842 struct got_commit_object *commit = NULL;
2844 refname = got_ref_get_name(re->ref);
2845 if (strncmp(refname, "refs/tags/", 10) != 0)
2846 continue;
2847 refname += 10;
2849 error = got_ref_resolve(&id, gw_trans->repo, re->ref);
2850 if (error)
2851 goto done;
2853 error = got_object_open_as_tag(&tag, gw_trans->repo, id);
2854 if (error) {
2855 if (error->code != GOT_ERR_OBJ_TYPE) {
2856 free(id);
2857 goto done;
2859 /* "lightweight" tag */
2860 error = got_object_open_as_commit(&commit,
2861 gw_trans->repo, id);
2862 if (error) {
2863 free(id);
2864 goto done;
2866 tagger = got_object_commit_get_committer(commit);
2867 tagger_time =
2868 got_object_commit_get_committer_time(commit);
2869 error = got_object_id_str(&id_str, id);
2870 free(id);
2871 } else {
2872 free(id);
2873 tagger = got_object_tag_get_tagger(tag);
2874 tagger_time = got_object_tag_get_tagger_time(tag);
2875 error = got_object_id_str(&id_str,
2876 got_object_tag_get_object_id(tag));
2878 if (error)
2879 goto done;
2881 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
2882 strlen(id_str)) != 0)
2883 continue;
2885 if (commit) {
2886 error = got_object_commit_get_logmsg(&tag_commit0,
2887 commit);
2888 if (error)
2889 goto done;
2890 got_object_commit_close(commit);
2891 } else {
2892 tag_commit0 = strdup(got_object_tag_get_message(tag));
2893 if (tag_commit0 == NULL) {
2894 error = got_error_from_errno("strdup");
2895 goto done;
2899 tag_commit = tag_commit0;
2900 while (*tag_commit == '\n')
2901 tag_commit++;
2903 switch (tag_type) {
2904 case TAGBRIEF:
2905 newline = strchr(tag_commit, '\n');
2906 if (newline)
2907 *newline = '\0';
2909 if (summary_header_displayed == 0) {
2910 kerr = khtml_attr(gw_trans->gw_html_req,
2911 KELEM_DIV, KATTR_ID,
2912 "summary_tags_title_wrapper", KATTR__MAX);
2913 if (kerr != KCGI_OK)
2914 goto done;
2915 kerr = khtml_attr(gw_trans->gw_html_req,
2916 KELEM_DIV, KATTR_ID,
2917 "summary_tags_title", KATTR__MAX);
2918 if (kerr != KCGI_OK)
2919 goto done;
2920 kerr = khtml_puts(gw_trans->gw_html_req,
2921 "Tags");
2922 if (kerr != KCGI_OK)
2923 goto done;
2924 kerr = khtml_closeelem(gw_trans->gw_html_req,
2925 2);
2926 if (kerr != KCGI_OK)
2927 goto done;
2928 kerr = khtml_attr(gw_trans->gw_html_req,
2929 KELEM_DIV, KATTR_ID,
2930 "summary_tags_content", KATTR__MAX);
2931 if (kerr != KCGI_OK)
2932 goto done;
2933 summary_header_displayed = 1;
2936 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2937 KATTR_ID, "tags_wrapper", KATTR__MAX);
2938 if (kerr != KCGI_OK)
2939 goto done;
2940 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2941 KATTR_ID, "tags_age", KATTR__MAX);
2942 if (kerr != KCGI_OK)
2943 goto done;
2944 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
2945 if (error)
2946 goto done;
2947 kerr = khtml_puts(gw_trans->gw_html_req,
2948 age ? age : "");
2949 if (kerr != KCGI_OK)
2950 goto done;
2951 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2952 if (kerr != KCGI_OK)
2953 goto done;
2954 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2955 KATTR_ID, "tags", KATTR__MAX);
2956 if (kerr != KCGI_OK)
2957 goto done;
2958 kerr = khtml_puts(gw_trans->gw_html_req, refname);
2959 if (kerr != KCGI_OK)
2960 goto done;
2961 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2962 if (kerr != KCGI_OK)
2963 goto done;
2964 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2965 KATTR_ID, "tags_name", KATTR__MAX);
2966 if (kerr != KCGI_OK)
2967 goto done;
2968 if (asprintf(&href_tag, "?path=%s&action=tag&commit=%s",
2969 gw_trans->repo_name, id_str) == -1) {
2970 error = got_error_from_errno("asprintf");
2971 goto done;
2973 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2974 KATTR_HREF, href_tag, KATTR__MAX);
2975 if (kerr != KCGI_OK)
2976 goto done;
2977 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
2978 if (kerr != KCGI_OK)
2979 goto done;
2980 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2981 if (kerr != KCGI_OK)
2982 goto done;
2984 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2985 KATTR_ID, "navs_wrapper", KATTR__MAX);
2986 if (kerr != KCGI_OK)
2987 goto done;
2988 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2989 KATTR_ID, "navs", KATTR__MAX);
2990 if (kerr != KCGI_OK)
2991 goto done;
2993 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2994 KATTR_HREF, href_tag, KATTR__MAX);
2995 if (kerr != KCGI_OK)
2996 goto done;
2997 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
2998 if (kerr != KCGI_OK)
2999 goto done;
3000 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3001 if (kerr != KCGI_OK)
3002 goto done;
3004 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3005 if (kerr != KCGI_OK)
3006 goto done;
3007 if (asprintf(&href_briefs,
3008 "?path=%s&action=briefs&commit=%s",
3009 gw_trans->repo_name, id_str) == -1) {
3010 error = got_error_from_errno("asprintf");
3011 goto done;
3013 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3014 KATTR_HREF, href_briefs, KATTR__MAX);
3015 if (kerr != KCGI_OK)
3016 goto done;
3017 kerr = khtml_puts(gw_trans->gw_html_req,
3018 "commit briefs");
3019 if (kerr != KCGI_OK)
3020 goto done;
3021 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3022 if (kerr != KCGI_OK)
3023 goto done;
3025 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3026 if (kerr != KCGI_OK)
3027 goto done;
3029 if (asprintf(&href_commits,
3030 "?path=%s&action=commits&commit=%s",
3031 gw_trans->repo_name, id_str) == -1) {
3032 error = got_error_from_errno("asprintf");
3033 goto done;
3035 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3036 KATTR_HREF, href_commits, KATTR__MAX);
3037 if (kerr != KCGI_OK)
3038 goto done;
3039 kerr = khtml_puts(gw_trans->gw_html_req,
3040 "commits");
3041 if (kerr != KCGI_OK)
3042 goto done;
3043 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3044 if (kerr != KCGI_OK)
3045 goto done;
3047 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3048 KATTR_ID, "dotted_line", KATTR__MAX);
3049 if (kerr != KCGI_OK)
3050 goto done;
3051 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3052 if (kerr != KCGI_OK)
3053 goto done;
3054 break;
3055 case TAGFULL:
3056 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3057 KATTR_ID, "tag_info_date_title", KATTR__MAX);
3058 if (kerr != KCGI_OK)
3059 goto done;
3060 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
3061 if (kerr != KCGI_OK)
3062 goto done;
3063 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3064 if (kerr != KCGI_OK)
3065 goto done;
3066 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3067 KATTR_ID, "tag_info_date", KATTR__MAX);
3068 if (kerr != KCGI_OK)
3069 goto done;
3070 error = gw_get_time_str(&age, tagger_time, TM_LONG);
3071 if (error)
3072 goto done;
3073 kerr = khtml_puts(gw_trans->gw_html_req,
3074 age ? age : "");
3075 if (kerr != KCGI_OK)
3076 goto done;
3077 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3078 if (kerr != KCGI_OK)
3079 goto done;
3081 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3082 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
3083 if (kerr != KCGI_OK)
3084 goto done;
3085 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
3086 if (kerr != KCGI_OK)
3087 goto done;
3088 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3089 if (kerr != KCGI_OK)
3090 goto done;
3091 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3092 KATTR_ID, "tag_info_date", KATTR__MAX);
3093 if (kerr != KCGI_OK)
3094 goto done;
3095 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
3096 if (kerr != KCGI_OK)
3097 goto done;
3098 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3099 if (kerr != KCGI_OK)
3100 goto done;
3102 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3103 KATTR_ID, "tag_info", KATTR__MAX);
3104 if (kerr != KCGI_OK)
3105 goto done;
3106 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
3107 if (kerr != KCGI_OK)
3108 goto done;
3109 break;
3110 default:
3111 break;
3113 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3114 if (kerr != KCGI_OK)
3115 goto done;
3117 if (limit && --limit == 0)
3118 break;
3120 if (tag)
3121 got_object_tag_close(tag);
3122 tag = NULL;
3123 free(id_str);
3124 id_str = NULL;
3125 free(age);
3126 age = NULL;
3127 free(tag_commit0);
3128 tag_commit0 = NULL;
3129 free(href_tag);
3130 href_tag = NULL;
3131 free(href_briefs);
3132 href_briefs = NULL;
3133 free(href_commits);
3134 href_commits = NULL;
3136 done:
3137 if (tag)
3138 got_object_tag_close(tag);
3139 free(id_str);
3140 free(age);
3141 free(tag_commit0);
3142 free(href_tag);
3143 free(href_briefs);
3144 free(href_commits);
3145 got_ref_list_free(&refs);
3146 if (error == NULL && kerr != KCGI_OK)
3147 error = gw_kcgi_error(kerr);
3148 return error;
3151 static void
3152 gw_free_header(struct gw_header *header)
3154 free(header->path);
3155 free(header->author);
3156 free(header->committer);
3157 free(header->refs_str);
3158 free(header->commit_id);
3159 free(header->parent_id);
3160 free(header->tree_id);
3161 free(header->commit_msg);
3164 static struct gw_header *
3165 gw_init_header()
3167 struct gw_header *header;
3169 header = malloc(sizeof(*header));
3170 if (header == NULL)
3171 return NULL;
3173 header->path = NULL;
3174 SIMPLEQ_INIT(&header->refs);
3176 header->refs_str = NULL;
3177 header->commit_id = NULL;
3178 header->committer = NULL;
3179 header->author = NULL;
3180 header->parent_id = NULL;
3181 header->tree_id = NULL;
3182 header->commit_msg = NULL;
3184 return header;
3187 static const struct got_error *
3188 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
3189 int limit, struct got_object_id *id)
3191 const struct got_error *error = NULL;
3192 struct got_commit_graph *graph = NULL;
3193 struct got_commit_object *commit = NULL;
3194 int chk_next = 0, chk_multi = 0, prev_set = 0;
3196 error = got_commit_graph_open(&graph, header->path, 0);
3197 if (error)
3198 return error;
3200 error = got_commit_graph_iter_start(graph, id, gw_trans->repo, NULL,
3201 NULL);
3202 if (error)
3203 goto done;
3205 for (;;) {
3206 error = got_commit_graph_iter_next(&id, graph, gw_trans->repo,
3207 NULL, NULL);
3208 if (error) {
3209 if (error->code == GOT_ERR_ITER_COMPLETED)
3210 error = NULL;
3211 goto done;
3213 if (id == NULL)
3214 goto done;
3216 error = got_object_open_as_commit(&commit, gw_trans->repo, id);
3217 if (error)
3218 goto done;
3219 if (limit == 1 && chk_multi == 0 &&
3220 gw_trans->gw_conf->got_max_commits_display != 1) {
3221 error = gw_get_commit(gw_trans, header, commit, id);
3222 if (error)
3223 goto done;
3224 } else {
3225 chk_multi = 1;
3226 struct gw_header *n_header = NULL;
3227 if ((n_header = gw_init_header()) == NULL) {
3228 error = got_error_from_errno("malloc");
3229 goto done;
3231 error = got_ref_list(&n_header->refs, gw_trans->repo,
3232 NULL, got_ref_cmp_by_name, NULL);
3233 if (error)
3234 goto done;
3236 error = gw_get_commit(gw_trans, n_header, commit, id);
3237 if (error)
3238 goto done;
3239 got_ref_list_free(&n_header->refs);
3242 * we have a commit_id now, so copy it to next_prev_id
3243 * for navigation through gw_briefs and gw_commits
3245 if (gw_trans->next_prev_id == NULL && prev_set == 0 &&
3246 (gw_trans->action == GW_BRIEFS ||
3247 gw_trans->action == GW_COMMITS ||
3248 gw_trans->action == GW_SUMMARY)) {
3249 prev_set = 1;
3250 gw_trans->next_prev_id =
3251 strdup(n_header->commit_id);
3252 if (gw_trans->next_prev_id == NULL) {
3253 error = got_error_from_errno("strdup");
3254 goto done;
3259 * check for one more commit before breaking,
3260 * so we know whether to navicate through gw_briefs
3261 * gw_commits and gw_summary
3263 if (chk_next && (gw_trans->action == GW_BRIEFS||
3264 gw_trans->action == GW_COMMITS ||
3265 gw_trans->action == GW_SUMMARY)) {
3266 gw_trans->next_id = strdup(n_header->commit_id);
3267 if (gw_trans->next_id == NULL)
3268 error = got_error_from_errno("strdup");
3269 goto done;
3272 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3273 entry);
3275 if (error || (limit && --limit == 0)) {
3276 if (chk_multi == 0)
3277 break;
3278 chk_next = 1;
3281 done:
3282 if (commit != NULL)
3283 got_object_commit_close(commit);
3284 if (graph)
3285 got_commit_graph_close(graph);
3286 return error;
3289 static const struct got_error *
3290 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header,
3291 struct got_commit_object *commit, struct got_object_id *id)
3293 const struct got_error *error = NULL;
3294 struct got_reflist_entry *re;
3295 struct got_object_id *id2 = NULL;
3296 struct got_object_qid *parent_id;
3297 char *commit_msg = NULL, *commit_msg0;
3299 /*print commit*/
3300 SIMPLEQ_FOREACH(re, &header->refs, entry) {
3301 char *s;
3302 const char *name;
3303 struct got_tag_object *tag = NULL;
3304 int cmp;
3306 if (got_ref_is_symbolic(re->ref))
3307 continue;
3309 name = got_ref_get_name(re->ref);
3310 if (strncmp(name, "refs/", 5) == 0)
3311 name += 5;
3312 if (strncmp(name, "got/", 4) == 0)
3313 continue;
3314 if (strncmp(name, "heads/", 6) == 0)
3315 name += 6;
3316 if (strncmp(name, "remotes/", 8) == 0)
3317 name += 8;
3318 if (strncmp(name, "tags/", 5) == 0) {
3319 error = got_object_open_as_tag(&tag, gw_trans->repo,
3320 re->id);
3321 if (error) {
3322 if (error->code != GOT_ERR_OBJ_TYPE)
3323 continue;
3325 * Ref points at something other
3326 * than a tag.
3328 error = NULL;
3329 tag = NULL;
3332 cmp = got_object_id_cmp(tag ?
3333 got_object_tag_get_object_id(tag) : re->id, id);
3334 if (tag)
3335 got_object_tag_close(tag);
3336 if (cmp != 0)
3337 continue;
3338 s = header->refs_str;
3339 if (asprintf(&header->refs_str, "%s%s%s", s ? s : "",
3340 s ? ", " : "", name) == -1) {
3341 error = got_error_from_errno("asprintf");
3342 free(s);
3343 header->refs_str = NULL;
3344 return error;
3346 free(s);
3349 error = got_object_id_str(&header->commit_id, id);
3350 if (error)
3351 return error;
3353 error = got_object_id_str(&header->tree_id,
3354 got_object_commit_get_tree_id(commit));
3355 if (error)
3356 return error;
3358 if (gw_trans->action == GW_DIFF) {
3359 parent_id = SIMPLEQ_FIRST(
3360 got_object_commit_get_parent_ids(commit));
3361 if (parent_id != NULL) {
3362 id2 = got_object_id_dup(parent_id->id);
3363 free (parent_id);
3364 error = got_object_id_str(&header->parent_id, id2);
3365 if (error)
3366 return error;
3367 free(id2);
3368 } else {
3369 header->parent_id = strdup("/dev/null");
3370 if (header->parent_id == NULL) {
3371 error = got_error_from_errno("strdup");
3372 return error;
3377 header->committer_time =
3378 got_object_commit_get_committer_time(commit);
3380 header->author =
3381 strdup(got_object_commit_get_author(commit));
3382 if (header->author == NULL) {
3383 error = got_error_from_errno("strdup");
3384 return error;
3386 header->committer =
3387 strdup(got_object_commit_get_committer(commit));
3388 if (header->committer == NULL) {
3389 error = got_error_from_errno("strdup");
3390 return error;
3392 error = got_object_commit_get_logmsg(&commit_msg0, commit);
3393 if (error)
3394 return error;
3396 commit_msg = commit_msg0;
3397 while (*commit_msg == '\n')
3398 commit_msg++;
3400 header->commit_msg = strdup(commit_msg);
3401 if (header->commit_msg == NULL)
3402 error = got_error_from_errno("strdup");
3403 free(commit_msg0);
3404 return error;
3407 static const struct got_error *
3408 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3410 const struct got_error *error = NULL;
3411 char *in_repo_path = NULL;
3412 struct got_object_id *id = NULL;
3414 error = got_repo_open(&gw_trans->repo, gw_trans->repo_path, NULL);
3415 if (error)
3416 return error;
3418 if (gw_trans->commit_id == NULL) {
3419 struct got_reference *head_ref;
3420 error = got_ref_open(&head_ref, gw_trans->repo,
3421 gw_trans->headref, 0);
3422 if (error)
3423 return error;
3425 error = got_ref_resolve(&id, gw_trans->repo, head_ref);
3426 got_ref_close(head_ref);
3427 if (error)
3428 return error;
3429 } else {
3430 struct got_reference *ref;
3432 error = got_ref_open(&ref, gw_trans->repo,
3433 gw_trans->commit_id, 0);
3434 if (error == NULL) {
3435 int obj_type;
3436 error = got_ref_resolve(&id, gw_trans->repo, ref);
3437 got_ref_close(ref);
3438 if (error)
3439 return error;
3440 error = got_object_get_type(&obj_type, gw_trans->repo,
3441 id);
3442 if (error)
3443 goto done;
3444 if (obj_type == GOT_OBJ_TYPE_TAG) {
3445 struct got_tag_object *tag;
3446 error = got_object_open_as_tag(&tag,
3447 gw_trans->repo, id);
3448 if (error)
3449 goto done;
3450 if (got_object_tag_get_object_type(tag) !=
3451 GOT_OBJ_TYPE_COMMIT) {
3452 got_object_tag_close(tag);
3453 error = got_error(GOT_ERR_OBJ_TYPE);
3454 goto done;
3456 free(id);
3457 id = got_object_id_dup(
3458 got_object_tag_get_object_id(tag));
3459 if (id == NULL)
3460 error = got_error_from_errno(
3461 "got_object_id_dup");
3462 got_object_tag_close(tag);
3463 if (error)
3464 goto done;
3465 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3466 error = got_error(GOT_ERR_OBJ_TYPE);
3467 goto done;
3470 error = got_repo_match_object_id_prefix(&id,
3471 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT,
3472 gw_trans->repo);
3473 if (error)
3474 goto done;
3477 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
3478 gw_trans->repo_path, 1);
3479 if (error)
3480 goto done;
3482 if (in_repo_path) {
3483 header->path = strdup(in_repo_path);
3484 if (header->path == NULL) {
3485 error = got_error_from_errno("strdup");
3486 goto done;
3490 error = got_ref_list(&header->refs, gw_trans->repo, NULL,
3491 got_ref_cmp_by_name, NULL);
3492 if (error)
3493 goto done;
3495 error = gw_get_commits(gw_trans, header, limit, id);
3496 done:
3497 got_ref_list_free(&header->refs);
3498 free(id);
3499 free(in_repo_path);
3500 return error;
3503 struct blame_line {
3504 int annotated;
3505 char *id_str;
3506 char *committer;
3507 char datebuf[11]; /* YYYY-MM-DD + NUL */
3510 struct gw_blame_cb_args {
3511 struct blame_line *lines;
3512 int nlines;
3513 int nlines_prec;
3514 int lineno_cur;
3515 off_t *line_offsets;
3516 FILE *f;
3517 struct got_repository *repo;
3518 struct gw_trans *gw_trans;
3521 static const struct got_error *
3522 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3524 const struct got_error *err = NULL;
3525 struct gw_blame_cb_args *a = arg;
3526 struct blame_line *bline;
3527 char *line = NULL;
3528 size_t linesize = 0;
3529 struct got_commit_object *commit = NULL;
3530 off_t offset;
3531 struct tm tm;
3532 time_t committer_time;
3533 enum kcgi_err kerr = KCGI_OK;
3535 if (nlines != a->nlines ||
3536 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3537 return got_error(GOT_ERR_RANGE);
3539 if (lineno == -1)
3540 return NULL; /* no change in this commit */
3542 /* Annotate this line. */
3543 bline = &a->lines[lineno - 1];
3544 if (bline->annotated)
3545 return NULL;
3546 err = got_object_id_str(&bline->id_str, id);
3547 if (err)
3548 return err;
3550 err = got_object_open_as_commit(&commit, a->repo, id);
3551 if (err)
3552 goto done;
3554 bline->committer = strdup(got_object_commit_get_committer(commit));
3555 if (bline->committer == NULL) {
3556 err = got_error_from_errno("strdup");
3557 goto done;
3560 committer_time = got_object_commit_get_committer_time(commit);
3561 if (localtime_r(&committer_time, &tm) == NULL)
3562 return got_error_from_errno("localtime_r");
3563 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3564 &tm) >= sizeof(bline->datebuf)) {
3565 err = got_error(GOT_ERR_NO_SPACE);
3566 goto done;
3568 bline->annotated = 1;
3570 /* Print lines annotated so far. */
3571 bline = &a->lines[a->lineno_cur - 1];
3572 if (!bline->annotated)
3573 goto done;
3575 offset = a->line_offsets[a->lineno_cur - 1];
3576 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3577 err = got_error_from_errno("fseeko");
3578 goto done;
3581 while (bline->annotated) {
3582 char *smallerthan, *at, *nl, *committer;
3583 char *lineno = NULL, *href_diff = NULL, *href_link = NULL;
3584 size_t len;
3586 if (getline(&line, &linesize, a->f) == -1) {
3587 if (ferror(a->f))
3588 err = got_error_from_errno("getline");
3589 break;
3592 committer = bline->committer;
3593 smallerthan = strchr(committer, '<');
3594 if (smallerthan && smallerthan[1] != '\0')
3595 committer = smallerthan + 1;
3596 at = strchr(committer, '@');
3597 if (at)
3598 *at = '\0';
3599 len = strlen(committer);
3600 if (len >= 9)
3601 committer[8] = '\0';
3603 nl = strchr(line, '\n');
3604 if (nl)
3605 *nl = '\0';
3607 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3608 "blame_wrapper", KATTR__MAX);
3609 if (kerr != KCGI_OK)
3610 goto err;
3611 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3612 "blame_number", KATTR__MAX);
3613 if (kerr != KCGI_OK)
3614 goto err;
3615 if (asprintf(&lineno, "%.*d", a->nlines_prec,
3616 a->lineno_cur) == -1)
3617 goto err;
3618 kerr = khtml_puts(a->gw_trans->gw_html_req, lineno);
3619 if (kerr != KCGI_OK)
3620 goto err;
3621 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3622 if (kerr != KCGI_OK)
3623 goto err;
3625 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3626 "blame_hash", KATTR__MAX);
3627 if (kerr != KCGI_OK)
3628 goto err;
3630 if (asprintf(&href_diff,
3631 "?path=%s&action=diff&commit=%s",
3632 a->gw_trans->repo_name, bline->id_str) == -1) {
3633 err = got_error_from_errno("asprintf");
3634 goto err;
3636 if (asprintf(&href_link, "%.8s", bline->id_str) == -1) {
3637 err = got_error_from_errno("asprintf");
3638 goto err;
3640 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3641 KATTR_HREF, href_diff, KATTR__MAX);
3642 if (kerr != KCGI_OK)
3643 goto done;
3644 kerr = khtml_puts(a->gw_trans->gw_html_req, href_link);
3645 if (kerr != KCGI_OK)
3646 goto err;
3647 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3648 if (kerr != KCGI_OK)
3649 goto err;
3651 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3652 "blame_date", KATTR__MAX);
3653 if (kerr != KCGI_OK)
3654 goto err;
3655 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3656 if (kerr != KCGI_OK)
3657 goto err;
3658 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3659 if (kerr != KCGI_OK)
3660 goto err;
3662 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3663 "blame_author", KATTR__MAX);
3664 if (kerr != KCGI_OK)
3665 goto err;
3666 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
3667 if (kerr != KCGI_OK)
3668 goto err;
3669 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3670 if (kerr != KCGI_OK)
3671 goto err;
3673 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3674 "blame_code", KATTR__MAX);
3675 if (kerr != KCGI_OK)
3676 goto err;
3677 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
3678 if (kerr != KCGI_OK)
3679 goto err;
3680 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3681 if (kerr != KCGI_OK)
3682 goto err;
3684 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3685 if (kerr != KCGI_OK)
3686 goto err;
3688 a->lineno_cur++;
3689 bline = &a->lines[a->lineno_cur - 1];
3690 err:
3691 free(lineno);
3692 free(href_diff);
3693 free(href_link);
3695 done:
3696 if (commit)
3697 got_object_commit_close(commit);
3698 free(line);
3699 if (err == NULL && kerr != KCGI_OK)
3700 err = gw_kcgi_error(kerr);
3701 return err;
3704 static const struct got_error *
3705 gw_output_file_blame(struct gw_trans *gw_trans)
3707 const struct got_error *error = NULL;
3708 struct got_object_id *obj_id = NULL;
3709 struct got_object_id *commit_id = NULL;
3710 struct got_blob_object *blob = NULL;
3711 char *path = NULL, *in_repo_path = NULL;
3712 struct gw_blame_cb_args bca;
3713 int i, obj_type;
3714 size_t filesize;
3716 if (asprintf(&path, "%s%s%s",
3717 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3718 gw_trans->repo_folder ? "/" : "",
3719 gw_trans->repo_file) == -1) {
3720 error = got_error_from_errno("asprintf");
3721 goto done;
3724 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path, 1);
3725 if (error)
3726 goto done;
3728 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
3729 GOT_OBJ_TYPE_COMMIT, 1, gw_trans->repo);
3730 if (error)
3731 goto done;
3733 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
3734 in_repo_path);
3735 if (error)
3736 goto done;
3738 if (obj_id == NULL) {
3739 error = got_error(GOT_ERR_NO_OBJ);
3740 goto done;
3743 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
3744 if (error)
3745 goto done;
3747 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3748 error = got_error(GOT_ERR_OBJ_TYPE);
3749 goto done;
3752 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
3753 if (error)
3754 goto done;
3756 bca.f = got_opentemp();
3757 if (bca.f == NULL) {
3758 error = got_error_from_errno("got_opentemp");
3759 goto done;
3761 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3762 &bca.line_offsets, bca.f, blob);
3763 if (error || bca.nlines == 0)
3764 goto done;
3766 /* Don't include \n at EOF in the blame line count. */
3767 if (bca.line_offsets[bca.nlines - 1] == filesize)
3768 bca.nlines--;
3770 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3771 if (bca.lines == NULL) {
3772 error = got_error_from_errno("calloc");
3773 goto done;
3775 bca.lineno_cur = 1;
3776 bca.nlines_prec = 0;
3777 i = bca.nlines;
3778 while (i > 0) {
3779 i /= 10;
3780 bca.nlines_prec++;
3782 bca.repo = gw_trans->repo;
3783 bca.gw_trans = gw_trans;
3785 error = got_blame(in_repo_path, commit_id, gw_trans->repo, gw_blame_cb,
3786 &bca, NULL, NULL);
3787 done:
3788 free(in_repo_path);
3789 free(commit_id);
3790 free(obj_id);
3791 free(path);
3793 if (blob) {
3794 free(bca.line_offsets);
3795 for (i = 0; i < bca.nlines; i++) {
3796 struct blame_line *bline = &bca.lines[i];
3797 free(bline->id_str);
3798 free(bline->committer);
3800 free(bca.lines);
3801 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3802 error = got_error_from_errno("fclose");
3804 if (blob)
3805 got_object_blob_close(blob);
3806 return error;
3809 static const struct got_error *
3810 gw_output_blob_buf(struct gw_trans *gw_trans)
3812 const struct got_error *error = NULL;
3813 struct got_object_id *obj_id = NULL;
3814 struct got_object_id *commit_id = NULL;
3815 struct got_blob_object *blob = NULL;
3816 char *path = NULL, *in_repo_path = NULL;
3817 int obj_type, set_mime = 0;
3818 size_t len, hdrlen;
3819 const uint8_t *buf;
3820 enum kcgi_err kerr = KCGI_OK;
3822 if (asprintf(&path, "%s%s%s",
3823 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3824 gw_trans->repo_folder ? "/" : "",
3825 gw_trans->repo_file) == -1) {
3826 error = got_error_from_errno("asprintf");
3827 goto done;
3830 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path, 1);
3831 if (error)
3832 goto done;
3834 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
3835 GOT_OBJ_TYPE_COMMIT, 1, gw_trans->repo);
3836 if (error)
3837 goto done;
3839 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
3840 in_repo_path);
3841 if (error)
3842 goto done;
3844 if (obj_id == NULL) {
3845 error = got_error(GOT_ERR_NO_OBJ);
3846 goto done;
3849 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
3850 if (error)
3851 goto done;
3853 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3854 error = got_error(GOT_ERR_OBJ_TYPE);
3855 goto done;
3858 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
3859 if (error)
3860 goto done;
3862 hdrlen = got_object_blob_get_hdrlen(blob);
3863 do {
3864 error = got_object_blob_read_block(&len, blob);
3865 if (error)
3866 goto done;
3867 buf = got_object_blob_get_read_buf(blob);
3870 * Skip blob object header first time around,
3871 * which also contains a zero byte.
3873 buf += hdrlen;
3874 if (set_mime == 0) {
3875 if (isbinary(buf, len - hdrlen))
3876 gw_trans->mime = KMIME_APP_OCTET_STREAM;
3877 else
3878 gw_trans->mime = KMIME_TEXT_PLAIN;
3879 set_mime = 1;
3880 error = gw_display_index(gw_trans);
3881 if (error)
3882 goto done;
3884 kerr = khttp_write(gw_trans->gw_req, buf, len - hdrlen);
3885 if (kerr != KCGI_OK)
3886 goto done;
3887 hdrlen = 0;
3888 } while (len != 0);
3889 done:
3890 free(in_repo_path);
3891 free(commit_id);
3892 free(obj_id);
3893 free(path);
3894 if (blob)
3895 got_object_blob_close(blob);
3896 if (error == NULL && kerr != KCGI_OK)
3897 error = gw_kcgi_error(kerr);
3898 return error;
3901 static const struct got_error *
3902 gw_output_repo_tree(struct gw_trans *gw_trans)
3904 const struct got_error *error = NULL;
3905 struct got_object_id *tree_id = NULL, *commit_id = NULL;
3906 struct got_tree_object *tree = NULL;
3907 char *path = NULL, *in_repo_path = NULL;
3908 char *id_str = NULL;
3909 char *build_folder = NULL;
3910 char *href_blob = NULL, *href_blame = NULL;
3911 const char *class = NULL;
3912 int nentries, i, class_flip = 0;
3913 enum kcgi_err kerr = KCGI_OK;
3915 if (gw_trans->repo_folder != NULL) {
3916 path = strdup(gw_trans->repo_folder);
3917 if (path == NULL) {
3918 error = got_error_from_errno("strdup");
3919 goto done;
3921 } else {
3922 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
3923 gw_trans->repo_path, 1);
3924 if (error)
3925 goto done;
3926 free(path);
3927 path = in_repo_path;
3930 if (gw_trans->commit_id == NULL) {
3931 struct got_reference *head_ref;
3932 error = got_ref_open(&head_ref, gw_trans->repo,
3933 gw_trans->headref, 0);
3934 if (error)
3935 goto done;
3936 error = got_ref_resolve(&commit_id, gw_trans->repo, head_ref);
3937 if (error)
3938 goto done;
3939 got_ref_close(head_ref);
3941 * gw_trans->commit_id was not parsed from the querystring
3942 * we hit this code path from gw_index, where we don't know the
3943 * commit values for the tree link yet, so set
3944 * gw_trans->commit_id here to continue further into the tree
3946 error = got_object_id_str(&gw_trans->commit_id, commit_id);
3947 if (error)
3948 goto done;
3950 } else {
3951 error = got_repo_match_object_id(&commit_id, NULL,
3952 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT, 1,
3953 gw_trans->repo);
3954 if (error)
3955 goto done;
3958 error = got_object_id_by_path(&tree_id, gw_trans->repo, commit_id,
3959 path);
3960 if (error)
3961 goto done;
3963 error = got_object_open_as_tree(&tree, gw_trans->repo, tree_id);
3964 if (error)
3965 goto done;
3967 nentries = got_object_tree_get_nentries(tree);
3968 for (i = 0; i < nentries; i++) {
3969 struct got_tree_entry *te;
3970 const char *modestr = "";
3971 mode_t mode;
3973 te = got_object_tree_get_entry(tree, i);
3975 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
3976 if (error)
3977 goto done;
3979 mode = got_tree_entry_get_mode(te);
3980 if (got_object_tree_entry_is_submodule(te))
3981 modestr = "$";
3982 else if (S_ISLNK(mode))
3983 modestr = "@";
3984 else if (S_ISDIR(mode))
3985 modestr = "/";
3986 else if (mode & S_IXUSR)
3987 modestr = "*";
3989 if (class_flip == 0) {
3990 class = "back_lightgray";
3991 class_flip = 1;
3992 } else {
3993 class = "back_white";
3994 class_flip = 0;
3997 if (S_ISDIR(mode)) {
3998 if (asprintf(&build_folder, "%s/%s",
3999 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4000 got_tree_entry_get_name(te)) == -1) {
4001 error = got_error_from_errno("asprintf");
4002 goto done;
4004 if (asprintf(&href_blob,
4005 "?path=%s&action=%s&commit=%s&folder=%s",
4006 gw_trans->repo_name, gw_get_action_name(gw_trans),
4007 gw_trans->commit_id, build_folder) == -1) {
4008 error = got_error_from_errno("asprintf");
4009 goto done;
4012 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4013 KATTR_ID, "tree_wrapper", KATTR__MAX);
4014 if (kerr != KCGI_OK)
4015 goto done;
4016 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4017 KATTR_ID, "tree_line", KATTR_CLASS, class,
4018 KATTR__MAX);
4019 if (kerr != KCGI_OK)
4020 goto done;
4021 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4022 KATTR_HREF, href_blob, KATTR_CLASS,
4023 "diff_directory", KATTR__MAX);
4024 if (kerr != KCGI_OK)
4025 goto done;
4026 kerr = khtml_puts(gw_trans->gw_html_req,
4027 got_tree_entry_get_name(te));
4028 if (kerr != KCGI_OK)
4029 goto done;
4030 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
4031 if (kerr != KCGI_OK)
4032 goto done;
4033 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4034 if (kerr != KCGI_OK)
4035 goto done;
4036 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4037 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
4038 KATTR__MAX);
4039 if (kerr != KCGI_OK)
4040 goto done;
4041 kerr = khtml_entity(gw_trans->gw_html_req,
4042 KENTITY_nbsp);
4043 if (kerr != KCGI_OK)
4044 goto done;
4045 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4046 if (kerr != KCGI_OK)
4047 goto done;
4048 } else {
4049 if (asprintf(&href_blob,
4050 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
4051 gw_trans->repo_name, "blob", gw_trans->commit_id,
4052 got_tree_entry_get_name(te),
4053 gw_trans->repo_folder ?
4054 gw_trans->repo_folder : "") == -1) {
4055 error = got_error_from_errno("asprintf");
4056 goto done;
4058 if (asprintf(&href_blame,
4059 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
4060 gw_trans->repo_name, "blame", gw_trans->commit_id,
4061 got_tree_entry_get_name(te),
4062 gw_trans->repo_folder ?
4063 gw_trans->repo_folder : "") == -1) {
4064 error = got_error_from_errno("asprintf");
4065 goto done;
4068 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4069 KATTR_ID, "tree_wrapper", KATTR__MAX);
4070 if (kerr != KCGI_OK)
4071 goto done;
4072 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4073 KATTR_ID, "tree_line", KATTR_CLASS, class,
4074 KATTR__MAX);
4075 if (kerr != KCGI_OK)
4076 goto done;
4077 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4078 KATTR_HREF, href_blob, KATTR__MAX);
4079 if (kerr != KCGI_OK)
4080 goto done;
4081 kerr = khtml_puts(gw_trans->gw_html_req,
4082 got_tree_entry_get_name(te));
4083 if (kerr != KCGI_OK)
4084 goto done;
4085 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
4086 if (kerr != KCGI_OK)
4087 goto done;
4088 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4089 if (kerr != KCGI_OK)
4090 goto done;
4091 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4092 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
4093 KATTR__MAX);
4094 if (kerr != KCGI_OK)
4095 goto done;
4097 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4098 KATTR_HREF, href_blob, KATTR__MAX);
4099 if (kerr != KCGI_OK)
4100 goto done;
4101 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
4102 if (kerr != KCGI_OK)
4103 goto done;
4104 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4105 if (kerr != KCGI_OK)
4106 goto done;
4108 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4109 if (kerr != KCGI_OK)
4110 goto done;
4112 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4113 KATTR_HREF, href_blame, KATTR__MAX);
4114 if (kerr != KCGI_OK)
4115 goto done;
4116 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
4117 if (kerr != KCGI_OK)
4118 goto done;
4120 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4121 if (kerr != KCGI_OK)
4122 goto done;
4124 free(id_str);
4125 id_str = NULL;
4126 free(href_blob);
4127 href_blob = NULL;
4128 free(build_folder);
4129 build_folder = NULL;
4131 done:
4132 if (tree)
4133 got_object_tree_close(tree);
4134 free(id_str);
4135 free(href_blob);
4136 free(href_blame);
4137 free(in_repo_path);
4138 free(tree_id);
4139 free(build_folder);
4140 if (error == NULL && kerr != KCGI_OK)
4141 error = gw_kcgi_error(kerr);
4142 return error;
4145 static const struct got_error *
4146 gw_output_repo_heads(struct gw_trans *gw_trans)
4148 const struct got_error *error = NULL;
4149 struct got_reflist_head refs;
4150 struct got_reflist_entry *re;
4151 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
4152 char *href_commits = NULL;
4153 enum kcgi_err kerr = KCGI_OK;
4155 SIMPLEQ_INIT(&refs);
4157 error = got_ref_list(&refs, gw_trans->repo, "refs/heads",
4158 got_ref_cmp_by_name, NULL);
4159 if (error)
4160 goto done;
4162 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4163 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
4164 if (kerr != KCGI_OK)
4165 goto done;
4166 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4167 KATTR_ID, "summary_heads_title", KATTR__MAX);
4168 if (kerr != KCGI_OK)
4169 goto done;
4170 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
4171 if (kerr != KCGI_OK)
4172 goto done;
4173 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4174 if (kerr != KCGI_OK)
4175 goto done;
4176 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4177 KATTR_ID, "summary_heads_content", KATTR__MAX);
4178 if (kerr != KCGI_OK)
4179 goto done;
4181 SIMPLEQ_FOREACH(re, &refs, entry) {
4182 const char *refname;
4184 if (got_ref_is_symbolic(re->ref))
4185 continue;
4187 refname = got_ref_get_name(re->ref);
4188 if (strncmp(refname, "refs/heads/", 11) != 0)
4189 continue;
4191 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
4192 refname, TM_DIFF);
4193 if (error)
4194 goto done;
4196 if (strncmp(refname, "refs/heads/", 11) == 0)
4197 refname += 11;
4199 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4200 KATTR_ID, "heads_wrapper", KATTR__MAX);
4201 if (kerr != KCGI_OK)
4202 goto done;
4203 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4204 KATTR_ID, "heads_age", KATTR__MAX);
4205 if (kerr != KCGI_OK)
4206 goto done;
4207 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
4208 if (kerr != KCGI_OK)
4209 goto done;
4210 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4211 if (kerr != KCGI_OK)
4212 goto done;
4213 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4214 KATTR_ID, "heads_space", KATTR__MAX);
4215 if (kerr != KCGI_OK)
4216 goto done;
4217 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
4218 if (kerr != KCGI_OK)
4219 goto done;
4220 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4221 if (kerr != KCGI_OK)
4222 goto done;
4223 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4224 KATTR_ID, "head", KATTR__MAX);
4225 if (kerr != KCGI_OK)
4226 goto done;
4227 if (asprintf(&href_summary,
4228 "?path=%s&action=summary&headref=%s",
4229 gw_trans->repo_name, refname) == -1) {
4230 error = got_error_from_errno("asprintf");
4231 goto done;
4233 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4234 href_summary, KATTR__MAX);
4235 kerr = khtml_puts(gw_trans->gw_html_req, refname);
4236 if (kerr != KCGI_OK)
4237 goto done;
4238 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4239 if (kerr != KCGI_OK)
4240 goto done;
4242 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4243 "navs_wrapper", KATTR__MAX);
4244 if (kerr != KCGI_OK)
4245 goto done;
4246 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4247 "navs", KATTR__MAX);
4248 if (kerr != KCGI_OK)
4249 goto done;
4251 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4252 href_summary, KATTR__MAX);
4253 if (kerr != KCGI_OK)
4254 goto done;
4255 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4256 if (kerr != KCGI_OK)
4257 goto done;
4258 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4259 if (kerr != KCGI_OK)
4260 goto done;
4262 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4263 if (kerr != KCGI_OK)
4264 goto done;
4265 if (asprintf(&href_briefs, "?path=%s&action=briefs&headref=%s",
4266 gw_trans->repo_name, refname) == -1) {
4267 error = got_error_from_errno("asprintf");
4268 goto done;
4270 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4271 href_briefs, KATTR__MAX);
4272 if (kerr != KCGI_OK)
4273 goto done;
4274 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4275 if (kerr != KCGI_OK)
4276 goto done;
4277 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4278 if (kerr != KCGI_OK)
4279 goto done;
4281 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4282 if (kerr != KCGI_OK)
4283 goto done;
4285 if (asprintf(&href_commits,
4286 "?path=%s&action=commits&headref=%s",
4287 gw_trans->repo_name, refname) == -1) {
4288 error = got_error_from_errno("asprintf");
4289 goto done;
4291 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4292 href_commits, KATTR__MAX);
4293 if (kerr != KCGI_OK)
4294 goto done;
4295 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4296 if (kerr != KCGI_OK)
4297 goto done;
4298 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4299 if (kerr != KCGI_OK)
4300 goto done;
4302 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4303 "dotted_line", KATTR__MAX);
4304 if (kerr != KCGI_OK)
4305 goto done;
4306 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4307 if (kerr != KCGI_OK)
4308 goto done;
4309 free(href_summary);
4310 href_summary = NULL;
4311 free(href_briefs);
4312 href_briefs = NULL;
4313 free(href_commits);
4314 href_commits = NULL;
4316 done:
4317 got_ref_list_free(&refs);
4318 free(href_summary);
4319 free(href_briefs);
4320 free(href_commits);
4321 return error;
4324 static const struct got_error *
4325 gw_output_site_link(struct gw_trans *gw_trans)
4327 const struct got_error *error = NULL;
4328 char *href_summary = NULL;
4329 enum kcgi_err kerr = KCGI_OK;
4331 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4332 "site_link", KATTR__MAX);
4333 if (kerr != KCGI_OK)
4334 goto done;
4335 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4336 KATTR__MAX);
4337 if (kerr != KCGI_OK)
4338 goto done;
4339 kerr = khtml_puts(gw_trans->gw_html_req,
4340 gw_trans->gw_conf->got_site_link);
4341 if (kerr != KCGI_OK)
4342 goto done;
4343 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4344 if (kerr != KCGI_OK)
4345 goto done;
4347 if (gw_trans->repo_name != NULL) {
4348 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4349 if (kerr != KCGI_OK)
4350 goto done;
4351 if (asprintf(&href_summary, "?path=%s&action=summary",
4352 gw_trans->repo_name) == -1)
4353 goto done;
4354 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4355 href_summary, KATTR__MAX);
4356 if (kerr != KCGI_OK)
4357 goto done;
4358 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4359 if (kerr != KCGI_OK)
4360 goto done;
4361 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4362 if (kerr != KCGI_OK)
4363 goto done;
4364 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4365 if (kerr != KCGI_OK)
4366 goto done;
4367 kerr = khtml_puts(gw_trans->gw_html_req,
4368 gw_get_action_name(gw_trans));
4369 if (kerr != KCGI_OK)
4370 goto done;
4373 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4374 if (kerr != KCGI_OK)
4375 goto done;
4376 done:
4377 free(href_summary);
4378 return error;
4381 static const struct got_error *
4382 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4384 const struct got_error *error = NULL;
4385 char *color = NULL;
4386 enum kcgi_err kerr = KCGI_OK;
4388 if (strncmp(buf, "-", 1) == 0)
4389 color = "diff_minus";
4390 else if (strncmp(buf, "+", 1) == 0)
4391 color = "diff_plus";
4392 else if (strncmp(buf, "@@", 2) == 0)
4393 color = "diff_chunk_header";
4394 else if (strncmp(buf, "@@", 2) == 0)
4395 color = "diff_chunk_header";
4396 else if (strncmp(buf, "commit +", 8) == 0)
4397 color = "diff_meta";
4398 else if (strncmp(buf, "commit -", 8) == 0)
4399 color = "diff_meta";
4400 else if (strncmp(buf, "blob +", 6) == 0)
4401 color = "diff_meta";
4402 else if (strncmp(buf, "blob -", 6) == 0)
4403 color = "diff_meta";
4404 else if (strncmp(buf, "file +", 6) == 0)
4405 color = "diff_meta";
4406 else if (strncmp(buf, "file -", 6) == 0)
4407 color = "diff_meta";
4408 else if (strncmp(buf, "from:", 5) == 0)
4409 color = "diff_author";
4410 else if (strncmp(buf, "via:", 4) == 0)
4411 color = "diff_author";
4412 else if (strncmp(buf, "date:", 5) == 0)
4413 color = "diff_date";
4414 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4415 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4416 if (error == NULL && kerr != KCGI_OK)
4417 error = gw_kcgi_error(kerr);
4418 return error;
4421 int
4422 main(int argc, char *argv[])
4424 const struct got_error *error = NULL;
4425 struct gw_trans *gw_trans;
4426 struct gw_dir *dir = NULL, *tdir;
4427 const char *page = "index";
4428 int gw_malloc = 1;
4429 enum kcgi_err kerr;
4431 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4432 errx(1, "malloc");
4434 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4435 errx(1, "malloc");
4437 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4438 errx(1, "malloc");
4440 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4441 errx(1, "malloc");
4443 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4444 if (kerr != KCGI_OK) {
4445 error = gw_kcgi_error(kerr);
4446 goto done;
4449 if ((gw_trans->gw_conf =
4450 malloc(sizeof(struct gotweb_conf))) == NULL) {
4451 gw_malloc = 0;
4452 error = got_error_from_errno("malloc");
4453 goto done;
4456 TAILQ_INIT(&gw_trans->gw_dirs);
4457 TAILQ_INIT(&gw_trans->gw_headers);
4459 gw_trans->action = -1;
4460 gw_trans->page = 0;
4461 gw_trans->repos_total = 0;
4462 gw_trans->repo_path = NULL;
4463 gw_trans->commit_id = NULL;
4464 gw_trans->next_id = NULL;
4465 gw_trans->next_prev_id = NULL;
4466 gw_trans->prev_id = NULL;
4467 gw_trans->prev_prev_id = NULL;
4468 gw_trans->headref = GOT_REF_HEAD;
4469 gw_trans->mime = KMIME_TEXT_HTML;
4470 gw_trans->gw_tmpl->key = gw_templs;
4471 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4472 gw_trans->gw_tmpl->arg = gw_trans;
4473 gw_trans->gw_tmpl->cb = gw_template;
4474 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
4475 if (error)
4476 goto done;
4478 error = gw_parse_querystring(gw_trans);
4479 if (error)
4480 goto done;
4482 if (gw_trans->action == GW_BLOB)
4483 error = gw_blob(gw_trans);
4484 else
4485 error = gw_display_index(gw_trans);
4486 done:
4487 if (gw_malloc) {
4488 free(gw_trans->gw_conf->got_repos_path);
4489 free(gw_trans->gw_conf->got_site_name);
4490 free(gw_trans->gw_conf->got_site_owner);
4491 free(gw_trans->gw_conf->got_site_link);
4492 free(gw_trans->gw_conf->got_logo);
4493 free(gw_trans->gw_conf->got_logo_url);
4494 free(gw_trans->gw_conf);
4495 free(gw_trans->commit_id);
4496 free(gw_trans->next_id);
4497 free(gw_trans->next_prev_id);
4498 free(gw_trans->prev_id);
4499 free(gw_trans->prev_prev_id);
4500 free(gw_trans->repo_path);
4501 if (gw_trans->repo)
4502 got_repo_close(gw_trans->repo);
4504 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4505 free(dir->name);
4506 free(dir->description);
4507 free(dir->age);
4508 free(dir->url);
4509 free(dir->path);
4510 free(dir);
4515 khttp_free(gw_trans->gw_req);
4516 return 0;