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 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
833 KATTR_ID, "np_wrapper", KATTR__MAX);
834 if (kerr != KCGI_OK)
835 goto done;
836 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
837 KATTR_ID, "nav_prev", KATTR__MAX);
838 if (kerr != KCGI_OK)
839 goto done;
840 } else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
841 (gw_trans->page > 0) &&
842 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
843 prev_disp == gw_trans->repos_total)) {
844 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
845 KATTR_ID, "np_wrapper", KATTR__MAX);
846 if (kerr != KCGI_OK)
847 goto done;
848 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
849 KATTR_ID, "nav_prev", KATTR__MAX);
850 if (kerr != KCGI_OK)
851 goto done;
854 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
855 (gw_trans->page > 0) &&
856 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
857 prev_disp == gw_trans->repos_total)) {
858 if (asprintf(&href_prev, "?page=%d",
859 gw_trans->page - 1) == -1) {
860 error = got_error_from_errno("asprintf");
861 goto done;
863 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
864 KATTR_HREF, href_prev, KATTR__MAX);
865 if (kerr != KCGI_OK)
866 goto done;
867 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
868 if (kerr != KCGI_OK)
869 goto done;
870 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
871 if (kerr != KCGI_OK)
872 goto done;
875 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
876 if (kerr != KCGI_OK)
877 return gw_kcgi_error(kerr);
879 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
880 next_disp == gw_trans->gw_conf->got_max_repos_display &&
881 dir_c != (gw_trans->page + 1) *
882 gw_trans->gw_conf->got_max_repos_display) {
883 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
884 KATTR_ID, "nav_next", KATTR__MAX);
885 if (kerr != KCGI_OK)
886 goto done;
887 if (asprintf(&href_next, "?page=%d",
888 gw_trans->page + 1) == -1) {
889 error = got_error_from_errno("calloc");
890 goto done;
892 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
893 KATTR_HREF, href_next, KATTR__MAX);
894 if (kerr != KCGI_OK)
895 goto done;
896 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
897 if (kerr != KCGI_OK)
898 goto done;
899 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
900 if (kerr != KCGI_OK)
901 goto done;
902 next_disp = 0;
903 break;
906 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
907 (gw_trans->page > 0) &&
908 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
909 prev_disp == gw_trans->repos_total)) {
910 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
911 if (kerr != KCGI_OK)
912 goto done;
914 next_disp++;
916 done:
917 free(href_prev);
918 free(href_next);
919 free(href_summary);
920 free(href_briefs);
921 free(href_commits);
922 free(href_tree);
923 if (error == NULL && kerr != KCGI_OK)
924 error = gw_kcgi_error(kerr);
925 return error;
928 static const struct got_error *
929 gw_commits(struct gw_trans *gw_trans)
931 const struct got_error *error = NULL;
932 struct gw_header *header = NULL, *n_header = NULL;
933 char *age = NULL, *href_diff = NULL, *href_blob = NULL;
934 char *href_prev = NULL, *href_next = NULL;
935 enum kcgi_err kerr = KCGI_OK;
937 if ((header = gw_init_header()) == NULL)
938 return got_error_from_errno("malloc");
940 if (pledge("stdio rpath proc exec sendfd unveil",
941 NULL) == -1) {
942 error = got_error_from_errno("pledge");
943 goto done;
946 error = gw_apply_unveil(gw_trans->gw_dir->path);
947 if (error)
948 goto done;
950 error = gw_get_header(gw_trans, header,
951 gw_trans->gw_conf->got_max_commits_display);
952 if (error)
953 goto done;
955 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
956 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
957 "commits_line_wrapper", KATTR__MAX);
958 if (kerr != KCGI_OK)
959 goto done;
960 error = gw_gen_commit_header(gw_trans, n_header->commit_id,
961 n_header->refs_str);
962 if (error)
963 goto done;
964 error = gw_gen_author_header(gw_trans, n_header->author);
965 if (error)
966 goto done;
967 error = gw_gen_committer_header(gw_trans, n_header->author);
968 if (error)
969 goto done;
970 error = gw_get_time_str(&age, n_header->committer_time,
971 TM_LONG);
972 if (error)
973 goto done;
974 error = gw_gen_age_header(gw_trans, age ?age : "");
975 if (error)
976 goto done;
977 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
978 if (kerr != KCGI_OK)
979 goto done;
981 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
982 "dotted_line", KATTR__MAX);
983 if (kerr != KCGI_OK)
984 goto done;
985 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
986 if (kerr != KCGI_OK)
987 goto done;
989 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
990 "commit", KATTR__MAX);
991 if (kerr != KCGI_OK)
992 goto done;
993 kerr = khttp_puts(gw_trans->gw_req, n_header->commit_msg);
994 if (kerr != KCGI_OK)
995 goto done;
996 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
997 if (kerr != KCGI_OK)
998 goto done;
1000 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
1001 gw_trans->repo_name, n_header->commit_id) == -1) {
1002 error = got_error_from_errno("asprintf");
1003 goto done;
1005 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1006 KATTR_ID, "navs_wrapper", KATTR__MAX);
1007 if (kerr != KCGI_OK)
1008 goto done;
1009 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1010 KATTR_ID, "navs", KATTR__MAX);
1011 if (kerr != KCGI_OK)
1012 goto done;
1013 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1014 KATTR_HREF, href_diff, KATTR__MAX);
1015 if (kerr != KCGI_OK)
1016 goto done;
1017 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1018 if (kerr != KCGI_OK)
1019 goto done;
1020 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1021 if (kerr != KCGI_OK)
1022 goto done;
1024 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1025 if (kerr != KCGI_OK)
1026 goto done;
1028 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
1029 gw_trans->repo_name, n_header->commit_id) == -1) {
1030 error = got_error_from_errno("asprintf");
1031 goto done;
1033 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1034 KATTR_HREF, href_blob, KATTR__MAX);
1035 if (kerr != KCGI_OK)
1036 goto done;
1037 khtml_puts(gw_trans->gw_html_req, "tree");
1038 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1039 if (kerr != KCGI_OK)
1040 goto done;
1041 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1042 if (kerr != KCGI_OK)
1043 goto done;
1045 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1046 "solid_line", KATTR__MAX);
1047 if (kerr != KCGI_OK)
1048 goto done;
1049 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1050 if (kerr != KCGI_OK)
1051 goto done;
1053 free(age);
1054 age = NULL;
1057 if (gw_trans->next_id || gw_trans->page > 0) {
1058 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1059 KATTR_ID, "np_wrapper", KATTR__MAX);
1060 if (kerr != KCGI_OK)
1061 goto done;
1062 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1063 KATTR_ID, "nav_prev", KATTR__MAX);
1064 if (kerr != KCGI_OK)
1065 goto done;
1068 if (gw_trans->page > 0) {
1069 if (asprintf(&href_prev,
1070 "?path=%s&page=%d&action=commits&commit=%s&prev=%s",
1071 gw_trans->repo_name, gw_trans->page - 1,
1072 gw_trans->prev_id ? gw_trans->prev_id : "",
1073 gw_trans->prev_prev_id ?
1074 gw_trans->prev_prev_id : "") == -1) {
1075 error = got_error_from_errno("asprintf");
1076 goto done;
1078 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1079 KATTR_HREF, href_prev, KATTR__MAX);
1080 if (kerr != KCGI_OK)
1081 goto done;
1082 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1083 if (kerr != KCGI_OK)
1084 goto done;
1085 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1086 if (kerr != KCGI_OK)
1087 goto done;
1090 if (gw_trans->next_id || gw_trans->page > 0) {
1091 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1092 if (kerr != KCGI_OK)
1093 return gw_kcgi_error(kerr);
1096 if (gw_trans->next_id) {
1097 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1098 KATTR_ID, "nav_next", KATTR__MAX);
1099 if (kerr != KCGI_OK)
1100 goto done;
1101 if (asprintf(&href_next,
1102 "?path=%s&page=%d&action=commits" \
1103 "&commit=%s&prev=%s&prev_prev=%s",
1104 gw_trans->repo_name, gw_trans->page + 1,
1105 gw_trans->next_id,
1106 gw_trans->next_prev_id ? gw_trans->next_prev_id : "",
1107 gw_trans->prev_id ?
1108 gw_trans->prev_id : "") == -1) {
1109 error = got_error_from_errno("calloc");
1110 goto done;
1112 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1113 KATTR_HREF, href_next, KATTR__MAX);
1114 if (kerr != KCGI_OK)
1115 goto done;
1116 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1117 if (kerr != KCGI_OK)
1118 goto done;
1119 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1120 if (kerr != KCGI_OK)
1121 goto done;
1124 if (gw_trans->next_id || gw_trans->page > 0) {
1125 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1126 if (kerr != KCGI_OK)
1127 goto done;
1129 done:
1130 gw_free_header(header);
1131 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1132 gw_free_header(n_header);
1133 free(age);
1134 free(href_next);
1135 free(href_prev);
1136 free(href_diff);
1137 free(href_blob);
1138 if (error == NULL && kerr != KCGI_OK)
1139 error = gw_kcgi_error(kerr);
1140 return error;
1143 static const struct got_error *
1144 gw_briefs(struct gw_trans *gw_trans)
1146 const struct got_error *error = NULL;
1147 struct gw_header *header = NULL, *n_header = NULL;
1148 char *age = NULL, *href_diff = NULL, *href_blob = NULL;
1149 char *href_prev = NULL, *href_next = NULL;
1150 char *newline, *smallerthan;
1151 enum kcgi_err kerr = KCGI_OK;
1153 if ((header = gw_init_header()) == NULL)
1154 return got_error_from_errno("malloc");
1156 if (pledge("stdio rpath proc exec sendfd unveil",
1157 NULL) == -1) {
1158 error = got_error_from_errno("pledge");
1159 goto done;
1162 if (gw_trans->action != GW_SUMMARY) {
1163 error = gw_apply_unveil(gw_trans->gw_dir->path);
1164 if (error)
1165 goto done;
1168 if (gw_trans->action == GW_SUMMARY)
1169 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
1170 else
1171 error = gw_get_header(gw_trans, header,
1172 gw_trans->gw_conf->got_max_commits_display);
1173 if (error)
1174 goto done;
1176 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
1177 error = gw_get_time_str(&age, n_header->committer_time,
1178 TM_DIFF);
1179 if (error)
1180 goto done;
1182 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1183 KATTR_ID, "briefs_wrapper", KATTR__MAX);
1184 if (kerr != KCGI_OK)
1185 goto done;
1187 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1188 KATTR_ID, "briefs_age", KATTR__MAX);
1189 if (kerr != KCGI_OK)
1190 goto done;
1191 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
1192 if (kerr != KCGI_OK)
1193 goto done;
1194 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1195 if (kerr != KCGI_OK)
1196 goto done;
1198 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1199 KATTR_ID, "briefs_author", KATTR__MAX);
1200 if (kerr != KCGI_OK)
1201 goto done;
1202 smallerthan = strchr(n_header->author, '<');
1203 if (smallerthan)
1204 *smallerthan = '\0';
1205 kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
1206 if (kerr != KCGI_OK)
1207 goto done;
1208 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1209 if (kerr != KCGI_OK)
1210 goto done;
1212 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
1213 gw_trans->repo_name, n_header->commit_id) == -1) {
1214 error = got_error_from_errno("asprintf");
1215 goto done;
1217 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1218 KATTR_ID, "briefs_log", KATTR__MAX);
1219 if (kerr != KCGI_OK)
1220 goto done;
1221 newline = strchr(n_header->commit_msg, '\n');
1222 if (newline)
1223 *newline = '\0';
1224 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1225 KATTR_HREF, href_diff, KATTR__MAX);
1226 if (kerr != KCGI_OK)
1227 goto done;
1228 kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
1229 if (kerr != KCGI_OK)
1230 goto done;
1231 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1232 if (kerr != KCGI_OK)
1233 goto done;
1235 if (n_header->refs_str) {
1236 kerr = khtml_puts(gw_trans->gw_html_req, " ");
1237 if (kerr != KCGI_OK)
1238 goto done;
1239 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
1240 KATTR_ID, "refs_str", KATTR__MAX);
1241 if (kerr != KCGI_OK)
1242 goto done;
1243 kerr = khtml_puts(gw_trans->gw_html_req, "(");
1244 if (kerr != KCGI_OK)
1245 goto done;
1246 kerr = khtml_puts(gw_trans->gw_html_req,
1247 n_header->refs_str);
1248 if (kerr != KCGI_OK)
1249 goto done;
1250 kerr = khtml_puts(gw_trans->gw_html_req, ")");
1251 if (kerr != KCGI_OK)
1252 goto done;
1253 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1254 if (kerr != KCGI_OK)
1255 goto done;
1258 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1259 if (kerr != KCGI_OK)
1260 goto done;
1262 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1263 KATTR_ID, "navs_wrapper", KATTR__MAX);
1264 if (kerr != KCGI_OK)
1265 goto done;
1266 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1267 KATTR_ID, "navs", KATTR__MAX);
1268 if (kerr != KCGI_OK)
1269 goto done;
1270 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1271 KATTR_HREF, href_diff, KATTR__MAX);
1272 if (kerr != KCGI_OK)
1273 goto done;
1274 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1275 if (kerr != KCGI_OK)
1276 goto done;
1277 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1278 if (kerr != KCGI_OK)
1279 goto done;
1281 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1282 if (kerr != KCGI_OK)
1283 goto done;
1285 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
1286 gw_trans->repo_name, n_header->commit_id) == -1) {
1287 error = got_error_from_errno("asprintf");
1288 goto done;
1290 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1291 KATTR_HREF, href_blob, KATTR__MAX);
1292 if (kerr != KCGI_OK)
1293 goto done;
1294 khtml_puts(gw_trans->gw_html_req, "tree");
1295 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1296 if (kerr != KCGI_OK)
1297 goto done;
1298 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1299 if (kerr != KCGI_OK)
1300 goto done;
1302 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1303 KATTR_ID, "dotted_line", KATTR__MAX);
1304 if (kerr != KCGI_OK)
1305 goto done;
1306 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1307 if (kerr != KCGI_OK)
1308 goto done;
1310 free(age);
1311 age = NULL;
1312 free(href_diff);
1313 href_diff = NULL;
1314 free(href_blob);
1315 href_blob = NULL;
1318 if (gw_trans->next_id || gw_trans->page > 0) {
1319 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1320 KATTR_ID, "np_wrapper", KATTR__MAX);
1321 if (kerr != KCGI_OK)
1322 goto done;
1323 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1324 KATTR_ID, "nav_prev", KATTR__MAX);
1325 if (kerr != KCGI_OK)
1326 goto done;
1329 if (gw_trans->page > 0) {
1330 if (asprintf(&href_prev,
1331 "?path=%s&page=%d&action=briefs&commit=%s&prev=%s",
1332 gw_trans->repo_name, gw_trans->page - 1,
1333 gw_trans->prev_id ? gw_trans->prev_id : "",
1334 gw_trans->prev_prev_id ?
1335 gw_trans->prev_prev_id : "") == -1) {
1336 error = got_error_from_errno("asprintf");
1337 goto done;
1339 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1340 KATTR_HREF, href_prev, KATTR__MAX);
1341 if (kerr != KCGI_OK)
1342 goto done;
1343 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1344 if (kerr != KCGI_OK)
1345 goto done;
1346 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1347 if (kerr != KCGI_OK)
1348 goto done;
1351 if (gw_trans->next_id || gw_trans->page > 0) {
1352 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1353 if (kerr != KCGI_OK)
1354 return gw_kcgi_error(kerr);
1357 if (gw_trans->next_id) {
1358 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1359 KATTR_ID, "nav_next", KATTR__MAX);
1360 if (kerr != KCGI_OK)
1361 goto done;
1362 if (asprintf(&href_next,
1363 "?path=%s&page=%d&action=briefs" \
1364 "&commit=%s&prev=%s&prev_prev=%s",
1365 gw_trans->repo_name, gw_trans->page + 1,
1366 gw_trans->next_id,
1367 gw_trans->next_prev_id ? gw_trans->next_prev_id : "",
1368 gw_trans->prev_id ?
1369 gw_trans->prev_id : "") == -1) {
1370 error = got_error_from_errno("calloc");
1371 goto done;
1373 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1374 KATTR_HREF, href_next, KATTR__MAX);
1375 if (kerr != KCGI_OK)
1376 goto done;
1377 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1378 if (kerr != KCGI_OK)
1379 goto done;
1380 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1381 if (kerr != KCGI_OK)
1382 goto done;
1385 if (gw_trans->next_id || gw_trans->page > 0) {
1386 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1387 if (kerr != KCGI_OK)
1388 goto done;
1390 done:
1391 gw_free_header(header);
1392 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1393 gw_free_header(n_header);
1394 free(age);
1395 free(href_next);
1396 free(href_prev);
1397 free(href_diff);
1398 free(href_blob);
1399 if (error == NULL && kerr != KCGI_OK)
1400 error = gw_kcgi_error(kerr);
1401 return error;
1404 static const struct got_error *
1405 gw_summary(struct gw_trans *gw_trans)
1407 const struct got_error *error = NULL;
1408 char *age = NULL;
1409 enum kcgi_err kerr = KCGI_OK;
1411 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1412 return got_error_from_errno("pledge");
1414 error = gw_apply_unveil(gw_trans->gw_dir->path);
1415 if (error)
1416 goto done;
1418 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1419 "summary_wrapper", KATTR__MAX);
1420 if (kerr != KCGI_OK)
1421 return gw_kcgi_error(kerr);
1423 if (gw_trans->gw_conf->got_show_repo_description &&
1424 gw_trans->gw_dir->description != NULL &&
1425 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1426 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1427 KATTR_ID, "description_title", KATTR__MAX);
1428 if (kerr != KCGI_OK)
1429 goto done;
1430 kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1431 if (kerr != KCGI_OK)
1432 goto done;
1433 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1434 if (kerr != KCGI_OK)
1435 goto done;
1436 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1437 KATTR_ID, "description", KATTR__MAX);
1438 if (kerr != KCGI_OK)
1439 goto done;
1440 kerr = khtml_puts(gw_trans->gw_html_req,
1441 gw_trans->gw_dir->description);
1442 if (kerr != KCGI_OK)
1443 goto done;
1444 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1445 if (kerr != KCGI_OK)
1446 goto done;
1449 if (gw_trans->gw_conf->got_show_repo_owner &&
1450 gw_trans->gw_dir->owner != NULL &&
1451 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
1452 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1453 KATTR_ID, "repo_owner_title", KATTR__MAX);
1454 if (kerr != KCGI_OK)
1455 goto done;
1456 kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1457 if (kerr != KCGI_OK)
1458 goto done;
1459 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1460 if (kerr != KCGI_OK)
1461 goto done;
1462 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1463 KATTR_ID, "repo_owner", KATTR__MAX);
1464 if (kerr != KCGI_OK)
1465 goto done;
1466 kerr = khtml_puts(gw_trans->gw_html_req,
1467 gw_trans->gw_dir->owner);
1468 if (kerr != KCGI_OK)
1469 goto done;
1470 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1471 if (kerr != KCGI_OK)
1472 goto done;
1475 if (gw_trans->gw_conf->got_show_repo_age) {
1476 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1477 NULL, TM_LONG);
1478 if (error)
1479 goto done;
1480 if (age != NULL) {
1481 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1482 KATTR_ID, "last_change_title", KATTR__MAX);
1483 if (kerr != KCGI_OK)
1484 goto done;
1485 kerr = khtml_puts(gw_trans->gw_html_req,
1486 "Last Change: ");
1487 if (kerr != KCGI_OK)
1488 goto done;
1489 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1490 if (kerr != KCGI_OK)
1491 goto done;
1492 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1493 KATTR_ID, "last_change", KATTR__MAX);
1494 if (kerr != KCGI_OK)
1495 goto done;
1496 kerr = khtml_puts(gw_trans->gw_html_req, age);
1497 if (kerr != KCGI_OK)
1498 goto done;
1499 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1500 if (kerr != KCGI_OK)
1501 goto done;
1505 if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1506 gw_trans->gw_dir->url != NULL &&
1507 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1508 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1509 KATTR_ID, "cloneurl_title", KATTR__MAX);
1510 if (kerr != KCGI_OK)
1511 goto done;
1512 kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1513 if (kerr != KCGI_OK)
1514 goto done;
1515 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1516 if (kerr != KCGI_OK)
1517 goto done;
1518 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1519 KATTR_ID, "cloneurl", KATTR__MAX);
1520 if (kerr != KCGI_OK)
1521 goto done;
1522 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1523 if (kerr != KCGI_OK)
1524 goto done;
1525 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1526 if (kerr != KCGI_OK)
1527 goto done;
1530 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1531 if (kerr != KCGI_OK)
1532 goto done;
1534 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1535 "briefs_title_wrapper", KATTR__MAX);
1536 if (kerr != KCGI_OK)
1537 goto done;
1538 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1539 "briefs_title", KATTR__MAX);
1540 if (kerr != KCGI_OK)
1541 goto done;
1542 kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1543 if (kerr != KCGI_OK)
1544 goto done;
1545 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1546 if (kerr != KCGI_OK)
1547 goto done;
1548 error = gw_briefs(gw_trans);
1549 if (error)
1550 goto done;
1552 error = gw_output_repo_tags(gw_trans, NULL, D_MAXSLCOMMDISP,
1553 TAGBRIEF);
1554 if (error)
1555 goto done;
1557 error = gw_output_repo_heads(gw_trans);
1558 done:
1559 free(age);
1560 if (error == NULL && kerr != KCGI_OK)
1561 error = gw_kcgi_error(kerr);
1562 return error;
1565 static const struct got_error *
1566 gw_tree(struct gw_trans *gw_trans)
1568 const struct got_error *error = NULL;
1569 struct gw_header *header = NULL;
1570 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1571 char *age = NULL;
1572 enum kcgi_err kerr;
1574 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1575 return got_error_from_errno("pledge");
1577 if ((header = gw_init_header()) == NULL)
1578 return got_error_from_errno("malloc");
1580 error = gw_apply_unveil(gw_trans->gw_dir->path);
1581 if (error)
1582 goto done;
1584 error = gw_get_header(gw_trans, header, 1);
1585 if (error)
1586 goto done;
1588 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1589 "tree_header_wrapper", KATTR__MAX);
1590 if (kerr != KCGI_OK)
1591 goto done;
1592 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1593 "tree_header", KATTR__MAX);
1594 if (kerr != KCGI_OK)
1595 goto done;
1596 error = gw_gen_tree_header(gw_trans, header->tree_id);
1597 if (error)
1598 goto done;
1599 error = gw_get_time_str(&age, header->committer_time,
1600 TM_LONG);
1601 if (error)
1602 goto done;
1603 error = gw_gen_age_header(gw_trans, age ?age : "");
1604 if (error)
1605 goto done;
1606 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1607 if (error)
1608 goto done;
1609 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1610 if (kerr != KCGI_OK)
1611 goto done;
1612 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1613 "dotted_line", KATTR__MAX);
1614 if (kerr != KCGI_OK)
1615 goto done;
1616 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1617 if (kerr != KCGI_OK)
1618 goto done;
1620 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1621 "tree", KATTR__MAX);
1622 if (kerr != KCGI_OK)
1623 goto done;
1624 error = gw_output_repo_tree(gw_trans);
1625 if (error)
1626 goto done;
1628 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1629 done:
1630 gw_free_header(header);
1631 free(tree_html_disp);
1632 free(tree_html);
1633 free(tree);
1634 free(age);
1635 if (error == NULL && kerr != KCGI_OK)
1636 error = gw_kcgi_error(kerr);
1637 return error;
1640 static const struct got_error *
1641 gw_tag(struct gw_trans *gw_trans)
1643 const struct got_error *error = NULL;
1644 struct gw_header *header = NULL;
1645 enum kcgi_err kerr;
1647 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1648 return got_error_from_errno("pledge");
1650 if ((header = gw_init_header()) == NULL)
1651 return got_error_from_errno("malloc");
1653 error = gw_apply_unveil(gw_trans->gw_dir->path);
1654 if (error)
1655 goto done;
1657 if (gw_trans->commit_id == NULL) {
1658 error = got_error_msg(GOT_ERR_QUERYSTRING,
1659 "commit required in querystring");
1660 goto done;
1663 error = gw_get_header(gw_trans, header, 1);
1664 if (error)
1665 goto done;
1667 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1668 "tag_header_wrapper", KATTR__MAX);
1669 if (kerr != KCGI_OK)
1670 goto done;
1671 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1672 "tag_header", KATTR__MAX);
1673 if (kerr != KCGI_OK)
1674 goto done;
1675 error = gw_gen_commit_header(gw_trans, header->commit_id,
1676 header->refs_str);
1677 if (error)
1678 goto done;
1679 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1680 if (error)
1681 goto done;
1682 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1683 if (kerr != KCGI_OK)
1684 goto done;
1685 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1686 "dotted_line", KATTR__MAX);
1687 if (kerr != KCGI_OK)
1688 goto done;
1689 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1690 if (kerr != KCGI_OK)
1691 goto done;
1693 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1694 "tree", KATTR__MAX);
1695 if (kerr != KCGI_OK)
1696 goto done;
1698 error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1699 if (error)
1700 goto done;
1702 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1703 done:
1704 gw_free_header(header);
1705 if (error == NULL && kerr != KCGI_OK)
1706 error = gw_kcgi_error(kerr);
1707 return error;
1710 static const struct got_error *
1711 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1713 const struct got_error *error = NULL;
1714 DIR *dt;
1715 char *dir_test;
1716 int opened = 0;
1718 if (asprintf(&dir_test, "%s/%s/%s",
1719 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1720 GOTWEB_GIT_DIR) == -1)
1721 return got_error_from_errno("asprintf");
1723 dt = opendir(dir_test);
1724 if (dt == NULL) {
1725 free(dir_test);
1726 } else {
1727 gw_dir->path = strdup(dir_test);
1728 if (gw_dir->path == NULL) {
1729 opened = 1;
1730 error = got_error_from_errno("strdup");
1731 goto errored;
1733 opened = 1;
1734 goto done;
1737 if (asprintf(&dir_test, "%s/%s/%s",
1738 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1739 GOTWEB_GOT_DIR) == -1) {
1740 dir_test = NULL;
1741 error = got_error_from_errno("asprintf");
1742 goto errored;
1745 dt = opendir(dir_test);
1746 if (dt == NULL)
1747 free(dir_test);
1748 else {
1749 opened = 1;
1750 error = got_error(GOT_ERR_NOT_GIT_REPO);
1751 goto errored;
1754 if (asprintf(&dir_test, "%s/%s",
1755 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1) {
1756 error = got_error_from_errno("asprintf");
1757 dir_test = NULL;
1758 goto errored;
1761 gw_dir->path = strdup(dir_test);
1762 if (gw_dir->path == NULL) {
1763 opened = 1;
1764 error = got_error_from_errno("strdup");
1765 goto errored;
1768 dt = opendir(dir_test);
1769 if (dt == NULL) {
1770 error = got_error_path(gw_dir->name, GOT_ERR_NOT_GIT_REPO);
1771 goto errored;
1772 } else
1773 opened = 1;
1774 done:
1775 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1776 gw_dir->path);
1777 if (error)
1778 goto errored;
1779 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1780 if (error)
1781 goto errored;
1782 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1783 NULL, TM_DIFF);
1784 if (error)
1785 goto errored;
1786 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1787 errored:
1788 free(dir_test);
1789 if (opened)
1790 if (dt && closedir(dt) == -1 && error == NULL)
1791 error = got_error_from_errno("closedir");
1792 return error;
1795 static const struct got_error *
1796 gw_load_got_paths(struct gw_trans *gw_trans)
1798 const struct got_error *error = NULL;
1799 DIR *d;
1800 struct dirent **sd_dent;
1801 struct gw_dir *gw_dir;
1802 struct stat st;
1803 unsigned int d_cnt, d_i;
1805 d = opendir(gw_trans->gw_conf->got_repos_path);
1806 if (d == NULL) {
1807 error = got_error_from_errno2("opendir",
1808 gw_trans->gw_conf->got_repos_path);
1809 return error;
1812 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1813 alphasort);
1814 if (d_cnt == -1) {
1815 error = got_error_from_errno2("scandir",
1816 gw_trans->gw_conf->got_repos_path);
1817 goto done;
1820 for (d_i = 0; d_i < d_cnt; d_i++) {
1821 if (gw_trans->gw_conf->got_max_repos > 0 &&
1822 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1823 break; /* account for parent and self */
1825 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1826 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1827 continue;
1829 error = gw_init_gw_dir(&gw_dir, sd_dent[d_i]->d_name);
1830 if (error)
1831 goto done;
1833 error = gw_load_got_path(gw_trans, gw_dir);
1834 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1835 error = NULL;
1836 continue;
1838 else if (error)
1839 goto done;
1841 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1842 !got_path_dir_is_empty(gw_dir->path)) {
1843 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1844 entry);
1845 gw_trans->repos_total++;
1848 done:
1849 if (d && closedir(d) == -1 && error == NULL)
1850 error = got_error_from_errno("closedir");
1851 return error;
1854 static const struct got_error *
1855 gw_parse_querystring(struct gw_trans *gw_trans)
1857 const struct got_error *error = NULL;
1858 struct kpair *p;
1859 struct gw_query_action *action = NULL;
1860 unsigned int i;
1862 if (gw_trans->gw_req->fieldnmap[0]) {
1863 return got_error(GOT_ERR_QUERYSTRING);
1864 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1865 /* define gw_trans->repo_path */
1866 gw_trans->repo_name = p->parsed.s;
1868 if (asprintf(&gw_trans->repo_path, "%s/%s",
1869 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
1870 return got_error_from_errno("asprintf");
1872 /* get action and set function */
1873 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION])) {
1874 for (i = 0; i < nitems(gw_query_funcs); i++) {
1875 action = &gw_query_funcs[i];
1876 if (action->func_name == NULL)
1877 continue;
1878 if (strcmp(action->func_name,
1879 p->parsed.s) == 0) {
1880 gw_trans->action = i;
1881 break;
1885 if (gw_trans->action == -1) {
1886 gw_trans->action = GW_ERR;
1887 gw_trans->error = got_error_msg(GOT_ERR_QUERYSTRING,
1888 p != NULL ? "bad action in querystring" :
1889 "no action in querystring");
1890 return error;
1893 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID])) {
1894 if (asprintf(&gw_trans->commit_id, "%s",
1895 p->parsed.s) == -1)
1896 return got_error_from_errno("asprintf");
1899 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1900 gw_trans->repo_file = p->parsed.s;
1902 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER])) {
1903 if (asprintf(&gw_trans->repo_folder, "%s",
1904 p->parsed.s) == -1)
1905 return got_error_from_errno("asprintf");
1908 if ((p = gw_trans->gw_req->fieldmap[KEY_PREV_ID])) {
1909 if (asprintf(&gw_trans->prev_id, "%s",
1910 p->parsed.s) == -1)
1911 return got_error_from_errno("asprintf");
1914 if ((p = gw_trans->gw_req->fieldmap[KEY_PREV_PREV_ID])) {
1915 if (asprintf(&gw_trans->prev_prev_id, "%s",
1916 p->parsed.s) == -1)
1917 return got_error_from_errno("asprintf");
1920 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
1921 gw_trans->headref = p->parsed.s;
1923 error = gw_init_gw_dir(&gw_trans->gw_dir, gw_trans->repo_name);
1924 if (error)
1925 return error;
1927 gw_trans->error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1928 } else
1929 gw_trans->action = GW_INDEX;
1931 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1932 gw_trans->page = p->parsed.i;
1934 return error;
1937 static const struct got_error *
1938 gw_init_gw_dir(struct gw_dir **gw_dir, const char *dir)
1940 const struct got_error *error;
1942 *gw_dir = malloc(sizeof(**gw_dir));
1943 if (*gw_dir == NULL)
1944 return got_error_from_errno("malloc");
1946 if (asprintf(&(*gw_dir)->name, "%s", dir) == -1) {
1947 error = got_error_from_errno("asprintf");
1948 free(*gw_dir);
1949 *gw_dir = NULL;
1950 return NULL;
1953 return NULL;
1956 static const struct got_error *
1957 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1959 enum kcgi_err kerr;
1961 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1962 if (kerr != KCGI_OK)
1963 return gw_kcgi_error(kerr);
1964 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1965 khttps[code]);
1966 if (kerr != KCGI_OK)
1967 return gw_kcgi_error(kerr);
1968 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1969 kmimetypes[mime]);
1970 if (kerr != KCGI_OK)
1971 return gw_kcgi_error(kerr);
1972 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
1973 "nosniff");
1974 if (kerr != KCGI_OK)
1975 return gw_kcgi_error(kerr);
1976 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1977 if (kerr != KCGI_OK)
1978 return gw_kcgi_error(kerr);
1979 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
1980 "1; mode=block");
1981 if (kerr != KCGI_OK)
1982 return gw_kcgi_error(kerr);
1984 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
1985 kerr = khttp_head(gw_trans->gw_req,
1986 kresps[KRESP_CONTENT_DISPOSITION],
1987 "attachment; filename=%s", gw_trans->repo_file);
1988 if (kerr != KCGI_OK)
1989 return gw_kcgi_error(kerr);
1992 kerr = khttp_body(gw_trans->gw_req);
1993 return gw_kcgi_error(kerr);
1996 static const struct got_error *
1997 gw_display_index(struct gw_trans *gw_trans)
1999 const struct got_error *error;
2000 enum kcgi_err kerr;
2002 /* catch early querystring errors */
2003 if (gw_trans->error)
2004 gw_trans->action = GW_ERR;
2006 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
2007 if (error)
2008 return error;
2010 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
2011 if (kerr != KCGI_OK)
2012 return gw_kcgi_error(kerr);
2014 if (gw_trans->action != GW_BLOB) {
2015 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
2016 gw_query_funcs[gw_trans->action].template);
2017 if (kerr != KCGI_OK) {
2018 khtml_close(gw_trans->gw_html_req);
2019 return gw_kcgi_error(kerr);
2023 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
2026 static const struct got_error *
2027 gw_error(struct gw_trans *gw_trans)
2029 enum kcgi_err kerr;
2031 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->error->msg);
2033 return gw_kcgi_error(kerr);
2036 static int
2037 gw_template(size_t key, void *arg)
2039 const struct got_error *error = NULL;
2040 enum kcgi_err kerr;
2041 struct gw_trans *gw_trans = arg;
2042 char *img_src = NULL;
2044 switch (key) {
2045 case (TEMPL_HEAD):
2046 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2047 KATTR_NAME, "viewport",
2048 KATTR_CONTENT, "initial-scale=.75, user-scalable=yes",
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_CHARSET, "utf-8",
2057 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, "msapplication-TileColor",
2065 KATTR_CONTENT, "#da532c", 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_META,
2072 KATTR_NAME, "theme-color",
2073 KATTR_CONTENT, "#ffffff", 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, "apple-touch-icon", KATTR_SIZES, "180x180",
2081 KATTR_HREF, "/apple-touch-icon.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 "32x32", KATTR_HREF, "/favicon-32x32.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, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2097 "16x16", KATTR_HREF, "/favicon-16x16.png", 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, "manifest", KATTR_HREF, "/site.webmanifest",
2105 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, "mask-icon", KATTR_HREF,
2113 "/safari-pinned-tab.svg", 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 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2120 KATTR_REL, "stylesheet", KATTR_TYPE, "text/css",
2121 KATTR_HREF, "/gotweb.css", KATTR__MAX);
2122 if (kerr != KCGI_OK)
2123 return 0;
2124 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2125 if (kerr != KCGI_OK)
2126 return 0;
2127 break;
2128 case(TEMPL_HEADER):
2129 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2130 KATTR_ID, "got_link", KATTR__MAX);
2131 if (kerr != KCGI_OK)
2132 return 0;
2133 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2134 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
2135 KATTR_TARGET, "_sotd", KATTR__MAX);
2136 if (kerr != KCGI_OK)
2137 return 0;
2138 if (asprintf(&img_src, "/%s",
2139 gw_trans->gw_conf->got_logo) == -1)
2140 return 0;
2141 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
2142 KATTR_SRC, img_src, KATTR__MAX);
2143 if (kerr != KCGI_OK) {
2144 free(img_src);
2145 return 0;
2147 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2148 if (kerr != KCGI_OK) {
2149 free(img_src);
2150 return 0;
2152 break;
2153 case (TEMPL_SITEPATH):
2154 error = gw_output_site_link(gw_trans);
2155 if (error)
2156 return 0;
2157 break;
2158 case(TEMPL_TITLE):
2159 if (gw_trans->gw_conf->got_site_name != NULL) {
2160 kerr = khtml_puts(gw_trans->gw_html_req,
2161 gw_trans->gw_conf->got_site_name);
2162 if (kerr != KCGI_OK)
2163 return 0;
2165 break;
2166 case (TEMPL_SEARCH):
2167 break;
2168 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
2169 "search", KATTR__MAX);
2170 if (kerr != KCGI_OK)
2171 return 0;
2172 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
2173 KATTR_METHOD, "POST", KATTR__MAX);
2174 if (kerr != KCGI_OK)
2175 return 0;
2176 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
2177 "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
2178 KATTR_MAXLENGTH, "50", KATTR__MAX);
2179 if (kerr != KCGI_OK)
2180 return 0;
2181 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
2182 KATTR__MAX);
2183 if (kerr != KCGI_OK)
2184 return 0;
2185 kerr = khtml_puts(gw_trans->gw_html_req, "Search");
2186 if (kerr != KCGI_OK)
2187 return 0;
2188 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
2189 if (kerr != KCGI_OK)
2190 return 0;
2191 break;
2192 case(TEMPL_SITEOWNER):
2193 if (gw_trans->gw_conf->got_site_owner != NULL &&
2194 gw_trans->gw_conf->got_show_site_owner) {
2195 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2196 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
2197 if (kerr != KCGI_OK)
2198 return 0;
2199 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2200 KATTR_ID, "site_owner", KATTR__MAX);
2201 if (kerr != KCGI_OK)
2202 return 0;
2203 kerr = khtml_puts(gw_trans->gw_html_req,
2204 gw_trans->gw_conf->got_site_owner);
2205 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2206 if (kerr != KCGI_OK)
2207 return 0;
2209 break;
2210 case(TEMPL_CONTENT):
2211 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
2212 if (error) {
2213 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2214 KATTR_ID, "tmpl_err", KATTR__MAX);
2215 if (kerr != KCGI_OK)
2216 return 0;
2217 kerr = khttp_puts(gw_trans->gw_req, "Error: ");
2218 if (kerr != KCGI_OK)
2219 return 0;
2220 kerr = khttp_puts(gw_trans->gw_req, error->msg);
2221 if (kerr != KCGI_OK)
2222 return 0;
2223 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2224 if (kerr != KCGI_OK)
2225 return 0;
2227 break;
2228 default:
2229 return 0;
2231 return 1;
2234 static const struct got_error *
2235 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
2237 const struct got_error *error = NULL;
2238 enum kcgi_err kerr = KCGI_OK;
2240 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2241 KATTR_ID, "header_commit_title", KATTR__MAX);
2242 if (kerr != KCGI_OK)
2243 goto done;
2244 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
2245 if (kerr != KCGI_OK)
2246 goto done;
2247 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2248 if (kerr != KCGI_OK)
2249 goto done;
2250 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2251 KATTR_ID, "header_commit", KATTR__MAX);
2252 if (kerr != KCGI_OK)
2253 goto done;
2254 kerr = khtml_puts(gw_trans->gw_html_req, str1);
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 if (str2 != NULL) {
2261 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
2262 KATTR_ID, "refs_str", KATTR__MAX);
2263 if (kerr != KCGI_OK)
2264 goto done;
2265 kerr = khtml_puts(gw_trans->gw_html_req, "(");
2266 if (kerr != KCGI_OK)
2267 goto done;
2268 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2269 if (kerr != KCGI_OK)
2270 goto done;
2271 kerr = khtml_puts(gw_trans->gw_html_req, ")");
2272 if (kerr != KCGI_OK)
2273 goto done;
2274 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2275 if (kerr != KCGI_OK)
2276 goto done;
2278 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2279 done:
2280 if (error == NULL && kerr != KCGI_OK)
2281 error = gw_kcgi_error(kerr);
2282 return error;
2285 static const struct got_error *
2286 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2288 const struct got_error *error = NULL;
2289 enum kcgi_err kerr = KCGI_OK;
2291 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2292 KATTR_ID, "header_diff_title", KATTR__MAX);
2293 if (kerr != KCGI_OK)
2294 goto done;
2295 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2296 if (kerr != KCGI_OK)
2297 goto done;
2298 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2299 if (kerr != KCGI_OK)
2300 goto done;
2301 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2302 KATTR_ID, "header_diff", KATTR__MAX);
2303 if (kerr != KCGI_OK)
2304 goto done;
2305 if (str1 != NULL) {
2306 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2307 if (kerr != KCGI_OK)
2308 goto done;
2310 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2311 if (kerr != KCGI_OK)
2312 goto done;
2313 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2314 if (kerr != KCGI_OK)
2315 goto done;
2316 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2317 done:
2318 if (error == NULL && kerr != KCGI_OK)
2319 error = gw_kcgi_error(kerr);
2320 return error;
2323 static const struct got_error *
2324 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2326 const struct got_error *error = NULL;
2327 enum kcgi_err kerr = KCGI_OK;
2329 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2330 KATTR_ID, "header_age_title", KATTR__MAX);
2331 if (kerr != KCGI_OK)
2332 goto done;
2333 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2334 if (kerr != KCGI_OK)
2335 goto done;
2336 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2337 if (kerr != KCGI_OK)
2338 goto done;
2339 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2340 KATTR_ID, "header_age", KATTR__MAX);
2341 if (kerr != KCGI_OK)
2342 goto done;
2343 kerr = khtml_puts(gw_trans->gw_html_req, str);
2344 if (kerr != KCGI_OK)
2345 goto done;
2346 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2347 done:
2348 if (error == NULL && kerr != KCGI_OK)
2349 error = gw_kcgi_error(kerr);
2350 return error;
2353 static const struct got_error *
2354 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2356 const struct got_error *error = NULL;
2357 enum kcgi_err kerr;
2359 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2360 KATTR_ID, "header_author_title", KATTR__MAX);
2361 if (kerr != KCGI_OK)
2362 goto done;
2363 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2364 if (kerr != KCGI_OK)
2365 goto done;
2366 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2367 if (kerr != KCGI_OK)
2368 goto done;
2369 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2370 KATTR_ID, "header_author", KATTR__MAX);
2371 if (kerr != KCGI_OK)
2372 goto done;
2373 kerr = khtml_puts(gw_trans->gw_html_req, str);
2374 if (kerr != KCGI_OK)
2375 goto done;
2376 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2377 done:
2378 if (error == NULL && kerr != KCGI_OK)
2379 error = gw_kcgi_error(kerr);
2380 return error;
2383 static const struct got_error *
2384 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2386 const struct got_error *error = NULL;
2387 enum kcgi_err kerr;
2389 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2390 KATTR_ID, "header_committer_title", KATTR__MAX);
2391 if (kerr != KCGI_OK)
2392 goto done;
2393 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2394 if (kerr != KCGI_OK)
2395 goto done;
2396 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2397 if (kerr != KCGI_OK)
2398 goto done;
2399 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2400 KATTR_ID, "header_committer", KATTR__MAX);
2401 if (kerr != KCGI_OK)
2402 goto done;
2403 kerr = khtml_puts(gw_trans->gw_html_req, str);
2404 if (kerr != KCGI_OK)
2405 goto done;
2406 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2407 done:
2408 if (error == NULL && kerr != KCGI_OK)
2409 error = gw_kcgi_error(kerr);
2410 return error;
2413 static const struct got_error *
2414 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2416 const struct got_error *error = NULL;
2417 enum kcgi_err kerr;
2419 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2420 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2421 if (kerr != KCGI_OK)
2422 goto done;
2423 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2424 if (kerr != KCGI_OK)
2425 goto done;
2426 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2427 if (kerr != KCGI_OK)
2428 goto done;
2429 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2430 KATTR_ID, "header_commit_msg", KATTR__MAX);
2431 if (kerr != KCGI_OK)
2432 goto done;
2433 kerr = khttp_puts(gw_trans->gw_req, str);
2434 if (kerr != KCGI_OK)
2435 goto done;
2436 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2437 done:
2438 if (error == NULL && kerr != KCGI_OK)
2439 error = gw_kcgi_error(kerr);
2440 return error;
2443 static const struct got_error *
2444 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2446 const struct got_error *error = NULL;
2447 enum kcgi_err kerr;
2449 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2450 KATTR_ID, "header_tree_title", KATTR__MAX);
2451 if (kerr != KCGI_OK)
2452 goto done;
2453 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2454 if (kerr != KCGI_OK)
2455 goto done;
2456 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2457 if (kerr != KCGI_OK)
2458 goto done;
2459 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2460 KATTR_ID, "header_tree", KATTR__MAX);
2461 if (kerr != KCGI_OK)
2462 goto done;
2463 kerr = khtml_puts(gw_trans->gw_html_req, str);
2464 if (kerr != KCGI_OK)
2465 goto done;
2466 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2467 done:
2468 if (error == NULL && kerr != KCGI_OK)
2469 error = gw_kcgi_error(kerr);
2470 return error;
2473 static const struct got_error *
2474 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2475 char *dir)
2477 const struct got_error *error = NULL;
2478 FILE *f = NULL;
2479 char *d_file = NULL;
2480 unsigned int len;
2481 size_t n;
2483 *description = NULL;
2484 if (gw_trans->gw_conf->got_show_repo_description == 0)
2485 return NULL;
2487 if (asprintf(&d_file, "%s/description", dir) == -1)
2488 return got_error_from_errno("asprintf");
2490 f = fopen(d_file, "r");
2491 if (f == NULL) {
2492 if (errno == ENOENT || errno == EACCES)
2493 return NULL;
2494 error = got_error_from_errno2("fopen", d_file);
2495 goto done;
2498 if (fseek(f, 0, SEEK_END) == -1) {
2499 error = got_ferror(f, GOT_ERR_IO);
2500 goto done;
2502 len = ftell(f);
2503 if (len == -1) {
2504 error = got_ferror(f, GOT_ERR_IO);
2505 goto done;
2507 if (fseek(f, 0, SEEK_SET) == -1) {
2508 error = got_ferror(f, GOT_ERR_IO);
2509 goto done;
2511 *description = calloc(len + 1, sizeof(**description));
2512 if (*description == NULL) {
2513 error = got_error_from_errno("calloc");
2514 goto done;
2517 n = fread(*description, 1, len, f);
2518 if (n == 0 && ferror(f))
2519 error = got_ferror(f, GOT_ERR_IO);
2520 done:
2521 if (f != NULL && fclose(f) == -1 && error == NULL)
2522 error = got_error_from_errno("fclose");
2523 free(d_file);
2524 return error;
2527 static const struct got_error *
2528 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2530 struct tm tm;
2531 time_t diff_time;
2532 char *years = "years ago", *months = "months ago";
2533 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2534 char *minutes = "minutes ago", *seconds = "seconds ago";
2535 char *now = "right now";
2536 char *s;
2537 char datebuf[29];
2539 *repo_age = NULL;
2541 switch (ref_tm) {
2542 case TM_DIFF:
2543 diff_time = time(NULL) - committer_time;
2544 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2545 if (asprintf(repo_age, "%lld %s",
2546 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2547 return got_error_from_errno("asprintf");
2548 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2549 if (asprintf(repo_age, "%lld %s",
2550 (diff_time / 60 / 60 / 24 / (365 / 12)),
2551 months) == -1)
2552 return got_error_from_errno("asprintf");
2553 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2554 if (asprintf(repo_age, "%lld %s",
2555 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2556 return got_error_from_errno("asprintf");
2557 } else if (diff_time > 60 * 60 * 24 * 2) {
2558 if (asprintf(repo_age, "%lld %s",
2559 (diff_time / 60 / 60 / 24), days) == -1)
2560 return got_error_from_errno("asprintf");
2561 } else if (diff_time > 60 * 60 * 2) {
2562 if (asprintf(repo_age, "%lld %s",
2563 (diff_time / 60 / 60), hours) == -1)
2564 return got_error_from_errno("asprintf");
2565 } else if (diff_time > 60 * 2) {
2566 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2567 minutes) == -1)
2568 return got_error_from_errno("asprintf");
2569 } else if (diff_time > 2) {
2570 if (asprintf(repo_age, "%lld %s", diff_time,
2571 seconds) == -1)
2572 return got_error_from_errno("asprintf");
2573 } else {
2574 if (asprintf(repo_age, "%s", now) == -1)
2575 return got_error_from_errno("asprintf");
2577 break;
2578 case TM_LONG:
2579 if (gmtime_r(&committer_time, &tm) == NULL)
2580 return got_error_from_errno("gmtime_r");
2582 s = asctime_r(&tm, datebuf);
2583 if (s == NULL)
2584 return got_error_from_errno("asctime_r");
2586 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2587 return got_error_from_errno("asprintf");
2588 break;
2590 return NULL;
2593 static const struct got_error *
2594 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2595 const char *refname, int ref_tm)
2597 const struct got_error *error = NULL;
2598 struct got_repository *repo = NULL;
2599 struct got_commit_object *commit = NULL;
2600 struct got_reflist_head refs;
2601 struct got_reflist_entry *re;
2602 time_t committer_time = 0, cmp_time = 0;
2604 *repo_age = NULL;
2605 SIMPLEQ_INIT(&refs);
2607 if (gw_trans->gw_conf->got_show_repo_age == 0)
2608 return NULL;
2610 if (gw_trans->repo)
2611 repo = gw_trans->repo;
2612 else {
2613 error = got_repo_open(&repo, dir, NULL);
2614 if (error)
2615 return error;
2618 error = got_ref_list(&refs, repo, "refs/heads",
2619 got_ref_cmp_by_name, NULL);
2620 if (error)
2621 goto done;
2624 * Find the youngest branch tip in the repository, or the age of
2625 * the a specific branch tip if a name was provided by the caller.
2627 SIMPLEQ_FOREACH(re, &refs, entry) {
2628 struct got_object_id *id = NULL;
2630 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
2631 continue;
2633 error = got_ref_resolve(&id, repo, re->ref);
2634 if (error)
2635 goto done;
2637 error = got_object_open_as_commit(&commit, repo, id);
2638 free(id);
2639 if (error)
2640 goto done;
2642 committer_time =
2643 got_object_commit_get_committer_time(commit);
2644 got_object_commit_close(commit);
2645 if (cmp_time < committer_time)
2646 cmp_time = committer_time;
2648 if (refname)
2649 break;
2652 if (cmp_time != 0) {
2653 committer_time = cmp_time;
2654 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2656 done:
2657 got_ref_list_free(&refs);
2658 if (gw_trans->repo == NULL)
2659 got_repo_close(repo);
2660 return error;
2663 static const struct got_error *
2664 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2666 const struct got_error *error;
2667 FILE *f = NULL;
2668 struct got_object_id *id1 = NULL, *id2 = NULL;
2669 char *label1 = NULL, *label2 = NULL, *line = NULL;
2670 int obj_type;
2671 size_t linesize = 0;
2672 ssize_t linelen;
2673 enum kcgi_err kerr = KCGI_OK;
2675 f = got_opentemp();
2676 if (f == NULL)
2677 return NULL;
2679 if (header->parent_id != NULL &&
2680 strncmp(header->parent_id, "/dev/null", 9) != 0) {
2681 error = got_repo_match_object_id(&id1, &label1,
2682 header->parent_id, GOT_OBJ_TYPE_ANY, 1, gw_trans->repo);
2683 if (error)
2684 goto done;
2687 error = got_repo_match_object_id(&id2, &label2,
2688 header->commit_id, GOT_OBJ_TYPE_ANY, 1, gw_trans->repo);
2689 if (error)
2690 goto done;
2692 error = got_object_get_type(&obj_type, gw_trans->repo, id2);
2693 if (error)
2694 goto done;
2695 switch (obj_type) {
2696 case GOT_OBJ_TYPE_BLOB:
2697 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
2698 gw_trans->repo, f);
2699 break;
2700 case GOT_OBJ_TYPE_TREE:
2701 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
2702 gw_trans->repo, f);
2703 break;
2704 case GOT_OBJ_TYPE_COMMIT:
2705 error = got_diff_objects_as_commits(id1, id2, 3, 0,
2706 gw_trans->repo, f);
2707 break;
2708 default:
2709 error = got_error(GOT_ERR_OBJ_TYPE);
2711 if (error)
2712 goto done;
2714 if (fseek(f, 0, SEEK_SET) == -1) {
2715 error = got_ferror(f, GOT_ERR_IO);
2716 goto done;
2719 while ((linelen = getline(&line, &linesize, f)) != -1) {
2720 error = gw_colordiff_line(gw_trans, line);
2721 if (error)
2722 goto done;
2723 /* XXX: KHTML_PRETTY breaks this */
2724 kerr = khtml_puts(gw_trans->gw_html_req, line);
2725 if (kerr != KCGI_OK)
2726 goto done;
2727 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2728 if (kerr != KCGI_OK)
2729 goto done;
2731 if (linelen == -1 && ferror(f))
2732 error = got_error_from_errno("getline");
2733 done:
2734 if (f && fclose(f) == -1 && error == NULL)
2735 error = got_error_from_errno("fclose");
2736 free(line);
2737 free(label1);
2738 free(label2);
2739 free(id1);
2740 free(id2);
2742 if (error == NULL && kerr != KCGI_OK)
2743 error = gw_kcgi_error(kerr);
2744 return error;
2747 static const struct got_error *
2748 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2750 const struct got_error *error = NULL;
2751 struct got_repository *repo;
2752 const char *gitconfig_owner;
2754 *owner = NULL;
2756 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2757 return NULL;
2759 error = got_repo_open(&repo, dir, NULL);
2760 if (error)
2761 return error;
2762 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2763 if (gitconfig_owner) {
2764 *owner = strdup(gitconfig_owner);
2765 if (*owner == NULL)
2766 error = got_error_from_errno("strdup");
2768 got_repo_close(repo);
2769 return error;
2772 static const struct got_error *
2773 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2775 const struct got_error *error = NULL;
2776 FILE *f;
2777 char *d_file = NULL;
2778 unsigned int len;
2779 size_t n;
2781 *url = NULL;
2783 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2784 return got_error_from_errno("asprintf");
2786 f = fopen(d_file, "r");
2787 if (f == NULL) {
2788 if (errno != ENOENT && errno != EACCES)
2789 error = got_error_from_errno2("fopen", d_file);
2790 goto done;
2793 if (fseek(f, 0, SEEK_END) == -1) {
2794 error = got_ferror(f, GOT_ERR_IO);
2795 goto done;
2797 len = ftell(f);
2798 if (len == -1) {
2799 error = got_ferror(f, GOT_ERR_IO);
2800 goto done;
2802 if (fseek(f, 0, SEEK_SET) == -1) {
2803 error = got_ferror(f, GOT_ERR_IO);
2804 goto done;
2807 *url = calloc(len + 1, sizeof(**url));
2808 if (*url == NULL) {
2809 error = got_error_from_errno("calloc");
2810 goto done;
2813 n = fread(*url, 1, len, f);
2814 if (n == 0 && ferror(f))
2815 error = got_ferror(f, GOT_ERR_IO);
2816 done:
2817 if (f && fclose(f) == -1 && error == NULL)
2818 error = got_error_from_errno("fclose");
2819 free(d_file);
2820 return NULL;
2823 static const struct got_error *
2824 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
2825 int limit, int tag_type)
2827 const struct got_error *error = NULL;
2828 struct got_reflist_head refs;
2829 struct got_reflist_entry *re;
2830 char *age = NULL;
2831 char *id_str = NULL, *newline, *href_commits = NULL;
2832 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
2833 struct got_tag_object *tag = NULL;
2834 enum kcgi_err kerr = KCGI_OK;
2835 int summary_header_displayed = 0;
2837 SIMPLEQ_INIT(&refs);
2839 error = got_ref_list(&refs, gw_trans->repo, "refs/tags",
2840 got_ref_cmp_tags, gw_trans->repo);
2841 if (error)
2842 goto done;
2844 SIMPLEQ_FOREACH(re, &refs, entry) {
2845 const char *refname;
2846 const char *tagger;
2847 const char *tag_commit;
2848 time_t tagger_time;
2849 struct got_object_id *id;
2850 struct got_commit_object *commit = NULL;
2852 refname = got_ref_get_name(re->ref);
2853 if (strncmp(refname, "refs/tags/", 10) != 0)
2854 continue;
2855 refname += 10;
2857 error = got_ref_resolve(&id, gw_trans->repo, re->ref);
2858 if (error)
2859 goto done;
2861 error = got_object_open_as_tag(&tag, gw_trans->repo, id);
2862 if (error) {
2863 if (error->code != GOT_ERR_OBJ_TYPE) {
2864 free(id);
2865 goto done;
2867 /* "lightweight" tag */
2868 error = got_object_open_as_commit(&commit,
2869 gw_trans->repo, id);
2870 if (error) {
2871 free(id);
2872 goto done;
2874 tagger = got_object_commit_get_committer(commit);
2875 tagger_time =
2876 got_object_commit_get_committer_time(commit);
2877 error = got_object_id_str(&id_str, id);
2878 free(id);
2879 } else {
2880 free(id);
2881 tagger = got_object_tag_get_tagger(tag);
2882 tagger_time = got_object_tag_get_tagger_time(tag);
2883 error = got_object_id_str(&id_str,
2884 got_object_tag_get_object_id(tag));
2886 if (error)
2887 goto done;
2889 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
2890 strlen(id_str)) != 0)
2891 continue;
2893 if (commit) {
2894 error = got_object_commit_get_logmsg(&tag_commit0,
2895 commit);
2896 if (error)
2897 goto done;
2898 got_object_commit_close(commit);
2899 } else {
2900 tag_commit0 = strdup(got_object_tag_get_message(tag));
2901 if (tag_commit0 == NULL) {
2902 error = got_error_from_errno("strdup");
2903 goto done;
2907 tag_commit = tag_commit0;
2908 while (*tag_commit == '\n')
2909 tag_commit++;
2911 switch (tag_type) {
2912 case TAGBRIEF:
2913 newline = strchr(tag_commit, '\n');
2914 if (newline)
2915 *newline = '\0';
2917 if (summary_header_displayed == 0) {
2918 kerr = khtml_attr(gw_trans->gw_html_req,
2919 KELEM_DIV, KATTR_ID,
2920 "summary_tags_title_wrapper", KATTR__MAX);
2921 if (kerr != KCGI_OK)
2922 goto done;
2923 kerr = khtml_attr(gw_trans->gw_html_req,
2924 KELEM_DIV, KATTR_ID,
2925 "summary_tags_title", KATTR__MAX);
2926 if (kerr != KCGI_OK)
2927 goto done;
2928 kerr = khtml_puts(gw_trans->gw_html_req,
2929 "Tags");
2930 if (kerr != KCGI_OK)
2931 goto done;
2932 kerr = khtml_closeelem(gw_trans->gw_html_req,
2933 2);
2934 if (kerr != KCGI_OK)
2935 goto done;
2936 kerr = khtml_attr(gw_trans->gw_html_req,
2937 KELEM_DIV, KATTR_ID,
2938 "summary_tags_content", KATTR__MAX);
2939 if (kerr != KCGI_OK)
2940 goto done;
2941 summary_header_displayed = 1;
2944 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2945 KATTR_ID, "tags_wrapper", KATTR__MAX);
2946 if (kerr != KCGI_OK)
2947 goto done;
2948 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2949 KATTR_ID, "tags_age", KATTR__MAX);
2950 if (kerr != KCGI_OK)
2951 goto done;
2952 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
2953 if (error)
2954 goto done;
2955 kerr = khtml_puts(gw_trans->gw_html_req,
2956 age ? age : "");
2957 if (kerr != KCGI_OK)
2958 goto done;
2959 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2960 if (kerr != KCGI_OK)
2961 goto done;
2962 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2963 KATTR_ID, "tags", KATTR__MAX);
2964 if (kerr != KCGI_OK)
2965 goto done;
2966 kerr = khtml_puts(gw_trans->gw_html_req, refname);
2967 if (kerr != KCGI_OK)
2968 goto done;
2969 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2970 if (kerr != KCGI_OK)
2971 goto done;
2972 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2973 KATTR_ID, "tags_name", KATTR__MAX);
2974 if (kerr != KCGI_OK)
2975 goto done;
2976 if (asprintf(&href_tag, "?path=%s&action=tag&commit=%s",
2977 gw_trans->repo_name, id_str) == -1) {
2978 error = got_error_from_errno("asprintf");
2979 goto done;
2981 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2982 KATTR_HREF, href_tag, KATTR__MAX);
2983 if (kerr != KCGI_OK)
2984 goto done;
2985 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
2986 if (kerr != KCGI_OK)
2987 goto done;
2988 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2989 if (kerr != KCGI_OK)
2990 goto done;
2992 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2993 KATTR_ID, "navs_wrapper", KATTR__MAX);
2994 if (kerr != KCGI_OK)
2995 goto done;
2996 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2997 KATTR_ID, "navs", KATTR__MAX);
2998 if (kerr != KCGI_OK)
2999 goto done;
3001 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3002 KATTR_HREF, href_tag, KATTR__MAX);
3003 if (kerr != KCGI_OK)
3004 goto done;
3005 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
3006 if (kerr != KCGI_OK)
3007 goto done;
3008 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3009 if (kerr != KCGI_OK)
3010 goto done;
3012 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3013 if (kerr != KCGI_OK)
3014 goto done;
3015 if (asprintf(&href_briefs,
3016 "?path=%s&action=briefs&commit=%s",
3017 gw_trans->repo_name, id_str) == -1) {
3018 error = got_error_from_errno("asprintf");
3019 goto done;
3021 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3022 KATTR_HREF, href_briefs, KATTR__MAX);
3023 if (kerr != KCGI_OK)
3024 goto done;
3025 kerr = khtml_puts(gw_trans->gw_html_req,
3026 "commit briefs");
3027 if (kerr != KCGI_OK)
3028 goto done;
3029 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3030 if (kerr != KCGI_OK)
3031 goto done;
3033 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3034 if (kerr != KCGI_OK)
3035 goto done;
3037 if (asprintf(&href_commits,
3038 "?path=%s&action=commits&commit=%s",
3039 gw_trans->repo_name, id_str) == -1) {
3040 error = got_error_from_errno("asprintf");
3041 goto done;
3043 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3044 KATTR_HREF, href_commits, KATTR__MAX);
3045 if (kerr != KCGI_OK)
3046 goto done;
3047 kerr = khtml_puts(gw_trans->gw_html_req,
3048 "commits");
3049 if (kerr != KCGI_OK)
3050 goto done;
3051 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3052 if (kerr != KCGI_OK)
3053 goto done;
3055 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3056 KATTR_ID, "dotted_line", KATTR__MAX);
3057 if (kerr != KCGI_OK)
3058 goto done;
3059 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3060 if (kerr != KCGI_OK)
3061 goto done;
3062 break;
3063 case TAGFULL:
3064 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3065 KATTR_ID, "tag_info_date_title", KATTR__MAX);
3066 if (kerr != KCGI_OK)
3067 goto done;
3068 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
3069 if (kerr != KCGI_OK)
3070 goto done;
3071 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3072 if (kerr != KCGI_OK)
3073 goto done;
3074 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3075 KATTR_ID, "tag_info_date", KATTR__MAX);
3076 if (kerr != KCGI_OK)
3077 goto done;
3078 error = gw_get_time_str(&age, tagger_time, TM_LONG);
3079 if (error)
3080 goto done;
3081 kerr = khtml_puts(gw_trans->gw_html_req,
3082 age ? age : "");
3083 if (kerr != KCGI_OK)
3084 goto done;
3085 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3086 if (kerr != KCGI_OK)
3087 goto done;
3089 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3090 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
3091 if (kerr != KCGI_OK)
3092 goto done;
3093 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
3094 if (kerr != KCGI_OK)
3095 goto done;
3096 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3097 if (kerr != KCGI_OK)
3098 goto done;
3099 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3100 KATTR_ID, "tag_info_date", KATTR__MAX);
3101 if (kerr != KCGI_OK)
3102 goto done;
3103 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
3104 if (kerr != KCGI_OK)
3105 goto done;
3106 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3107 if (kerr != KCGI_OK)
3108 goto done;
3110 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3111 KATTR_ID, "tag_info", KATTR__MAX);
3112 if (kerr != KCGI_OK)
3113 goto done;
3114 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
3115 if (kerr != KCGI_OK)
3116 goto done;
3117 break;
3118 default:
3119 break;
3121 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3122 if (kerr != KCGI_OK)
3123 goto done;
3125 if (limit && --limit == 0)
3126 break;
3128 if (tag)
3129 got_object_tag_close(tag);
3130 tag = NULL;
3131 free(id_str);
3132 id_str = NULL;
3133 free(age);
3134 age = NULL;
3135 free(tag_commit0);
3136 tag_commit0 = NULL;
3137 free(href_tag);
3138 href_tag = NULL;
3139 free(href_briefs);
3140 href_briefs = NULL;
3141 free(href_commits);
3142 href_commits = NULL;
3144 done:
3145 if (tag)
3146 got_object_tag_close(tag);
3147 free(id_str);
3148 free(age);
3149 free(tag_commit0);
3150 free(href_tag);
3151 free(href_briefs);
3152 free(href_commits);
3153 got_ref_list_free(&refs);
3154 if (error == NULL && kerr != KCGI_OK)
3155 error = gw_kcgi_error(kerr);
3156 return error;
3159 static void
3160 gw_free_header(struct gw_header *header)
3162 free(header->path);
3163 free(header->author);
3164 free(header->committer);
3165 free(header->refs_str);
3166 free(header->commit_id);
3167 free(header->parent_id);
3168 free(header->tree_id);
3169 free(header->commit_msg);
3172 static struct gw_header *
3173 gw_init_header()
3175 struct gw_header *header;
3177 header = malloc(sizeof(*header));
3178 if (header == NULL)
3179 return NULL;
3181 header->path = NULL;
3182 SIMPLEQ_INIT(&header->refs);
3184 header->refs_str = NULL;
3185 header->commit_id = NULL;
3186 header->committer = NULL;
3187 header->author = NULL;
3188 header->parent_id = NULL;
3189 header->tree_id = NULL;
3190 header->commit_msg = NULL;
3192 return header;
3195 static const struct got_error *
3196 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
3197 int limit, struct got_object_id *id)
3199 const struct got_error *error = NULL;
3200 struct got_commit_graph *graph = NULL;
3201 struct got_commit_object *commit = NULL;
3202 int chk_next = 0, chk_multi = 0, prev_set = 0;
3204 error = got_commit_graph_open(&graph, header->path, 0);
3205 if (error)
3206 return error;
3208 error = got_commit_graph_iter_start(graph, id, gw_trans->repo, NULL,
3209 NULL);
3210 if (error)
3211 goto done;
3213 for (;;) {
3214 error = got_commit_graph_iter_next(&id, graph, gw_trans->repo,
3215 NULL, NULL);
3216 if (error) {
3217 if (error->code == GOT_ERR_ITER_COMPLETED)
3218 error = NULL;
3219 goto done;
3221 if (id == NULL)
3222 goto done;
3224 error = got_object_open_as_commit(&commit, gw_trans->repo, id);
3225 if (error)
3226 goto done;
3227 if (limit == 1 && chk_multi == 0) {
3228 error = gw_get_commit(gw_trans, header, commit, id);
3229 if (error)
3230 goto done;
3231 } else {
3232 chk_multi = 1;
3233 struct gw_header *n_header = NULL;
3234 if ((n_header = gw_init_header()) == NULL) {
3235 error = got_error_from_errno("malloc");
3236 goto done;
3238 error = got_ref_list(&n_header->refs, gw_trans->repo,
3239 NULL, got_ref_cmp_by_name, NULL);
3240 if (error)
3241 goto done;
3243 error = gw_get_commit(gw_trans, n_header, commit, id);
3244 if (error)
3245 goto done;
3246 got_ref_list_free(&n_header->refs);
3249 * we have a commit_id now, so copy it to next_prev_id
3250 * for navigation through gw_briefs and gw_commits
3252 if (gw_trans->next_prev_id == NULL && prev_set == 0 &&
3253 (gw_trans->action == GW_BRIEFS ||
3254 gw_trans->action == GW_COMMITS ||
3255 gw_trans->action == GW_SUMMARY)) {
3256 prev_set = 1;
3257 gw_trans->next_prev_id =
3258 strdup(n_header->commit_id);
3259 if (gw_trans->next_prev_id == NULL) {
3260 error = got_error_from_errno("strdup");
3261 goto done;
3266 * check for one more commit before breaking,
3267 * so we know whether to navicate through gw_briefs
3268 * gw_commits and gw_summary
3270 if (chk_next && (gw_trans->action == GW_BRIEFS||
3271 gw_trans->action == GW_COMMITS ||
3272 gw_trans->action == GW_SUMMARY)) {
3273 gw_trans->next_id = strdup(n_header->commit_id);
3274 if (gw_trans->next_id == NULL)
3275 error = got_error_from_errno("strdup");
3276 goto done;
3279 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3280 entry);
3282 if (error || (limit && --limit == 0)) {
3283 if (chk_multi == 0)
3284 break;
3285 chk_next = 1;
3288 done:
3289 if (commit != NULL)
3290 got_object_commit_close(commit);
3291 if (graph)
3292 got_commit_graph_close(graph);
3293 return error;
3296 static const struct got_error *
3297 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header,
3298 struct got_commit_object *commit, struct got_object_id *id)
3300 const struct got_error *error = NULL;
3301 struct got_reflist_entry *re;
3302 struct got_object_id *id2 = NULL;
3303 struct got_object_qid *parent_id;
3304 char *commit_msg = NULL, *commit_msg0;
3306 /*print commit*/
3307 SIMPLEQ_FOREACH(re, &header->refs, entry) {
3308 char *s;
3309 const char *name;
3310 struct got_tag_object *tag = NULL;
3311 int cmp;
3313 if (got_ref_is_symbolic(re->ref))
3314 continue;
3316 name = got_ref_get_name(re->ref);
3317 if (strncmp(name, "refs/", 5) == 0)
3318 name += 5;
3319 if (strncmp(name, "got/", 4) == 0)
3320 continue;
3321 if (strncmp(name, "heads/", 6) == 0)
3322 name += 6;
3323 if (strncmp(name, "remotes/", 8) == 0)
3324 name += 8;
3325 if (strncmp(name, "tags/", 5) == 0) {
3326 error = got_object_open_as_tag(&tag, gw_trans->repo,
3327 re->id);
3328 if (error) {
3329 if (error->code != GOT_ERR_OBJ_TYPE)
3330 continue;
3332 * Ref points at something other
3333 * than a tag.
3335 error = NULL;
3336 tag = NULL;
3339 cmp = got_object_id_cmp(tag ?
3340 got_object_tag_get_object_id(tag) : re->id, id);
3341 if (tag)
3342 got_object_tag_close(tag);
3343 if (cmp != 0)
3344 continue;
3345 s = header->refs_str;
3346 if (asprintf(&header->refs_str, "%s%s%s", s ? s : "",
3347 s ? ", " : "", name) == -1) {
3348 error = got_error_from_errno("asprintf");
3349 free(s);
3350 header->refs_str = NULL;
3351 return error;
3353 free(s);
3356 error = got_object_id_str(&header->commit_id, id);
3357 if (error)
3358 return error;
3360 error = got_object_id_str(&header->tree_id,
3361 got_object_commit_get_tree_id(commit));
3362 if (error)
3363 return error;
3365 if (gw_trans->action == GW_DIFF) {
3366 parent_id = SIMPLEQ_FIRST(
3367 got_object_commit_get_parent_ids(commit));
3368 if (parent_id != NULL) {
3369 id2 = got_object_id_dup(parent_id->id);
3370 free (parent_id);
3371 error = got_object_id_str(&header->parent_id, id2);
3372 if (error)
3373 return error;
3374 free(id2);
3375 } else {
3376 header->parent_id = strdup("/dev/null");
3377 if (header->parent_id == NULL) {
3378 error = got_error_from_errno("strdup");
3379 return error;
3384 header->committer_time =
3385 got_object_commit_get_committer_time(commit);
3387 header->author =
3388 strdup(got_object_commit_get_author(commit));
3389 if (header->author == NULL) {
3390 error = got_error_from_errno("strdup");
3391 return error;
3393 header->committer =
3394 strdup(got_object_commit_get_committer(commit));
3395 if (header->committer == NULL) {
3396 error = got_error_from_errno("strdup");
3397 return error;
3399 error = got_object_commit_get_logmsg(&commit_msg0, commit);
3400 if (error)
3401 return error;
3403 commit_msg = commit_msg0;
3404 while (*commit_msg == '\n')
3405 commit_msg++;
3407 header->commit_msg = strdup(commit_msg);
3408 if (header->commit_msg == NULL)
3409 error = got_error_from_errno("strdup");
3410 free(commit_msg0);
3411 return error;
3414 static const struct got_error *
3415 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3417 const struct got_error *error = NULL;
3418 char *in_repo_path = NULL;
3419 struct got_object_id *id = NULL;
3421 error = got_repo_open(&gw_trans->repo, gw_trans->repo_path, NULL);
3422 if (error)
3423 return error;
3425 if (gw_trans->commit_id == NULL) {
3426 struct got_reference *head_ref;
3427 error = got_ref_open(&head_ref, gw_trans->repo,
3428 gw_trans->headref, 0);
3429 if (error)
3430 return error;
3432 error = got_ref_resolve(&id, gw_trans->repo, head_ref);
3433 got_ref_close(head_ref);
3434 if (error)
3435 return error;
3436 } else {
3437 struct got_reference *ref;
3439 error = got_ref_open(&ref, gw_trans->repo,
3440 gw_trans->commit_id, 0);
3441 if (error == NULL) {
3442 int obj_type;
3443 error = got_ref_resolve(&id, gw_trans->repo, ref);
3444 got_ref_close(ref);
3445 if (error)
3446 return error;
3447 error = got_object_get_type(&obj_type, gw_trans->repo,
3448 id);
3449 if (error)
3450 goto done;
3451 if (obj_type == GOT_OBJ_TYPE_TAG) {
3452 struct got_tag_object *tag;
3453 error = got_object_open_as_tag(&tag,
3454 gw_trans->repo, id);
3455 if (error)
3456 goto done;
3457 if (got_object_tag_get_object_type(tag) !=
3458 GOT_OBJ_TYPE_COMMIT) {
3459 got_object_tag_close(tag);
3460 error = got_error(GOT_ERR_OBJ_TYPE);
3461 goto done;
3463 free(id);
3464 id = got_object_id_dup(
3465 got_object_tag_get_object_id(tag));
3466 if (id == NULL)
3467 error = got_error_from_errno(
3468 "got_object_id_dup");
3469 got_object_tag_close(tag);
3470 if (error)
3471 goto done;
3472 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3473 error = got_error(GOT_ERR_OBJ_TYPE);
3474 goto done;
3477 error = got_repo_match_object_id_prefix(&id,
3478 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT,
3479 gw_trans->repo);
3480 if (error)
3481 goto done;
3484 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
3485 gw_trans->repo_path, 1);
3486 if (error)
3487 goto done;
3489 if (in_repo_path) {
3490 header->path = strdup(in_repo_path);
3491 if (header->path == NULL) {
3492 error = got_error_from_errno("strdup");
3493 goto done;
3497 error = got_ref_list(&header->refs, gw_trans->repo, NULL,
3498 got_ref_cmp_by_name, NULL);
3499 if (error)
3500 goto done;
3502 error = gw_get_commits(gw_trans, header, limit, id);
3503 done:
3504 got_ref_list_free(&header->refs);
3505 free(id);
3506 free(in_repo_path);
3507 return error;
3510 struct blame_line {
3511 int annotated;
3512 char *id_str;
3513 char *committer;
3514 char datebuf[11]; /* YYYY-MM-DD + NUL */
3517 struct gw_blame_cb_args {
3518 struct blame_line *lines;
3519 int nlines;
3520 int nlines_prec;
3521 int lineno_cur;
3522 off_t *line_offsets;
3523 FILE *f;
3524 struct got_repository *repo;
3525 struct gw_trans *gw_trans;
3528 static const struct got_error *
3529 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3531 const struct got_error *err = NULL;
3532 struct gw_blame_cb_args *a = arg;
3533 struct blame_line *bline;
3534 char *line = NULL;
3535 size_t linesize = 0;
3536 struct got_commit_object *commit = NULL;
3537 off_t offset;
3538 struct tm tm;
3539 time_t committer_time;
3540 enum kcgi_err kerr = KCGI_OK;
3542 if (nlines != a->nlines ||
3543 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3544 return got_error(GOT_ERR_RANGE);
3546 if (lineno == -1)
3547 return NULL; /* no change in this commit */
3549 /* Annotate this line. */
3550 bline = &a->lines[lineno - 1];
3551 if (bline->annotated)
3552 return NULL;
3553 err = got_object_id_str(&bline->id_str, id);
3554 if (err)
3555 return err;
3557 err = got_object_open_as_commit(&commit, a->repo, id);
3558 if (err)
3559 goto done;
3561 bline->committer = strdup(got_object_commit_get_committer(commit));
3562 if (bline->committer == NULL) {
3563 err = got_error_from_errno("strdup");
3564 goto done;
3567 committer_time = got_object_commit_get_committer_time(commit);
3568 if (localtime_r(&committer_time, &tm) == NULL)
3569 return got_error_from_errno("localtime_r");
3570 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3571 &tm) >= sizeof(bline->datebuf)) {
3572 err = got_error(GOT_ERR_NO_SPACE);
3573 goto done;
3575 bline->annotated = 1;
3577 /* Print lines annotated so far. */
3578 bline = &a->lines[a->lineno_cur - 1];
3579 if (!bline->annotated)
3580 goto done;
3582 offset = a->line_offsets[a->lineno_cur - 1];
3583 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3584 err = got_error_from_errno("fseeko");
3585 goto done;
3588 while (bline->annotated) {
3589 char *smallerthan, *at, *nl, *committer;
3590 char *lineno = NULL, *href_diff = NULL, *href_link = NULL;
3591 size_t len;
3593 if (getline(&line, &linesize, a->f) == -1) {
3594 if (ferror(a->f))
3595 err = got_error_from_errno("getline");
3596 break;
3599 committer = bline->committer;
3600 smallerthan = strchr(committer, '<');
3601 if (smallerthan && smallerthan[1] != '\0')
3602 committer = smallerthan + 1;
3603 at = strchr(committer, '@');
3604 if (at)
3605 *at = '\0';
3606 len = strlen(committer);
3607 if (len >= 9)
3608 committer[8] = '\0';
3610 nl = strchr(line, '\n');
3611 if (nl)
3612 *nl = '\0';
3614 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3615 "blame_wrapper", KATTR__MAX);
3616 if (kerr != KCGI_OK)
3617 goto err;
3618 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3619 "blame_number", KATTR__MAX);
3620 if (kerr != KCGI_OK)
3621 goto err;
3622 if (asprintf(&lineno, "%.*d", a->nlines_prec,
3623 a->lineno_cur) == -1)
3624 goto err;
3625 kerr = khtml_puts(a->gw_trans->gw_html_req, lineno);
3626 if (kerr != KCGI_OK)
3627 goto err;
3628 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3629 if (kerr != KCGI_OK)
3630 goto err;
3632 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3633 "blame_hash", KATTR__MAX);
3634 if (kerr != KCGI_OK)
3635 goto err;
3637 if (asprintf(&href_diff,
3638 "?path=%s&action=diff&commit=%s",
3639 a->gw_trans->repo_name, bline->id_str) == -1) {
3640 err = got_error_from_errno("asprintf");
3641 goto err;
3643 if (asprintf(&href_link, "%.8s", bline->id_str) == -1) {
3644 err = got_error_from_errno("asprintf");
3645 goto err;
3647 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3648 KATTR_HREF, href_diff, KATTR__MAX);
3649 if (kerr != KCGI_OK)
3650 goto done;
3651 kerr = khtml_puts(a->gw_trans->gw_html_req, href_link);
3652 if (kerr != KCGI_OK)
3653 goto err;
3654 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3655 if (kerr != KCGI_OK)
3656 goto err;
3658 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3659 "blame_date", KATTR__MAX);
3660 if (kerr != KCGI_OK)
3661 goto err;
3662 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3663 if (kerr != KCGI_OK)
3664 goto err;
3665 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3666 if (kerr != KCGI_OK)
3667 goto err;
3669 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3670 "blame_author", KATTR__MAX);
3671 if (kerr != KCGI_OK)
3672 goto err;
3673 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
3674 if (kerr != KCGI_OK)
3675 goto err;
3676 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3677 if (kerr != KCGI_OK)
3678 goto err;
3680 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3681 "blame_code", KATTR__MAX);
3682 if (kerr != KCGI_OK)
3683 goto err;
3684 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
3685 if (kerr != KCGI_OK)
3686 goto err;
3687 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3688 if (kerr != KCGI_OK)
3689 goto err;
3691 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3692 if (kerr != KCGI_OK)
3693 goto err;
3695 a->lineno_cur++;
3696 bline = &a->lines[a->lineno_cur - 1];
3697 err:
3698 free(lineno);
3699 free(href_diff);
3700 free(href_link);
3702 done:
3703 if (commit)
3704 got_object_commit_close(commit);
3705 free(line);
3706 if (err == NULL && kerr != KCGI_OK)
3707 err = gw_kcgi_error(kerr);
3708 return err;
3711 static const struct got_error *
3712 gw_output_file_blame(struct gw_trans *gw_trans)
3714 const struct got_error *error = NULL;
3715 struct got_object_id *obj_id = NULL;
3716 struct got_object_id *commit_id = NULL;
3717 struct got_blob_object *blob = NULL;
3718 char *path = NULL, *in_repo_path = NULL;
3719 struct gw_blame_cb_args bca;
3720 int i, obj_type;
3721 size_t filesize;
3723 if (asprintf(&path, "%s%s%s",
3724 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3725 gw_trans->repo_folder ? "/" : "",
3726 gw_trans->repo_file) == -1) {
3727 error = got_error_from_errno("asprintf");
3728 goto done;
3731 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path, 1);
3732 if (error)
3733 goto done;
3735 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
3736 GOT_OBJ_TYPE_COMMIT, 1, gw_trans->repo);
3737 if (error)
3738 goto done;
3740 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
3741 in_repo_path);
3742 if (error)
3743 goto done;
3745 if (obj_id == NULL) {
3746 error = got_error(GOT_ERR_NO_OBJ);
3747 goto done;
3750 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
3751 if (error)
3752 goto done;
3754 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3755 error = got_error(GOT_ERR_OBJ_TYPE);
3756 goto done;
3759 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
3760 if (error)
3761 goto done;
3763 bca.f = got_opentemp();
3764 if (bca.f == NULL) {
3765 error = got_error_from_errno("got_opentemp");
3766 goto done;
3768 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3769 &bca.line_offsets, bca.f, blob);
3770 if (error || bca.nlines == 0)
3771 goto done;
3773 /* Don't include \n at EOF in the blame line count. */
3774 if (bca.line_offsets[bca.nlines - 1] == filesize)
3775 bca.nlines--;
3777 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3778 if (bca.lines == NULL) {
3779 error = got_error_from_errno("calloc");
3780 goto done;
3782 bca.lineno_cur = 1;
3783 bca.nlines_prec = 0;
3784 i = bca.nlines;
3785 while (i > 0) {
3786 i /= 10;
3787 bca.nlines_prec++;
3789 bca.repo = gw_trans->repo;
3790 bca.gw_trans = gw_trans;
3792 error = got_blame(in_repo_path, commit_id, gw_trans->repo, gw_blame_cb,
3793 &bca, NULL, NULL);
3794 done:
3795 free(in_repo_path);
3796 free(commit_id);
3797 free(obj_id);
3798 free(path);
3800 if (blob) {
3801 free(bca.line_offsets);
3802 for (i = 0; i < bca.nlines; i++) {
3803 struct blame_line *bline = &bca.lines[i];
3804 free(bline->id_str);
3805 free(bline->committer);
3807 free(bca.lines);
3808 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3809 error = got_error_from_errno("fclose");
3811 if (blob)
3812 got_object_blob_close(blob);
3813 return error;
3816 static const struct got_error *
3817 gw_output_blob_buf(struct gw_trans *gw_trans)
3819 const struct got_error *error = NULL;
3820 struct got_object_id *obj_id = NULL;
3821 struct got_object_id *commit_id = NULL;
3822 struct got_blob_object *blob = NULL;
3823 char *path = NULL, *in_repo_path = NULL;
3824 int obj_type, set_mime = 0;
3825 size_t len, hdrlen;
3826 const uint8_t *buf;
3827 enum kcgi_err kerr = KCGI_OK;
3829 if (asprintf(&path, "%s%s%s",
3830 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3831 gw_trans->repo_folder ? "/" : "",
3832 gw_trans->repo_file) == -1) {
3833 error = got_error_from_errno("asprintf");
3834 goto done;
3837 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path, 1);
3838 if (error)
3839 goto done;
3841 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
3842 GOT_OBJ_TYPE_COMMIT, 1, gw_trans->repo);
3843 if (error)
3844 goto done;
3846 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
3847 in_repo_path);
3848 if (error)
3849 goto done;
3851 if (obj_id == NULL) {
3852 error = got_error(GOT_ERR_NO_OBJ);
3853 goto done;
3856 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
3857 if (error)
3858 goto done;
3860 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3861 error = got_error(GOT_ERR_OBJ_TYPE);
3862 goto done;
3865 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
3866 if (error)
3867 goto done;
3869 hdrlen = got_object_blob_get_hdrlen(blob);
3870 do {
3871 error = got_object_blob_read_block(&len, blob);
3872 if (error)
3873 goto done;
3874 buf = got_object_blob_get_read_buf(blob);
3877 * Skip blob object header first time around,
3878 * which also contains a zero byte.
3880 buf += hdrlen;
3881 if (set_mime == 0) {
3882 if (isbinary(buf, len - hdrlen))
3883 gw_trans->mime = KMIME_APP_OCTET_STREAM;
3884 else
3885 gw_trans->mime = KMIME_TEXT_PLAIN;
3886 set_mime = 1;
3887 error = gw_display_index(gw_trans);
3888 if (error)
3889 goto done;
3891 kerr = khttp_write(gw_trans->gw_req, buf, len - hdrlen);
3892 if (kerr != KCGI_OK)
3893 goto done;
3894 hdrlen = 0;
3895 } while (len != 0);
3896 done:
3897 free(in_repo_path);
3898 free(commit_id);
3899 free(obj_id);
3900 free(path);
3901 if (blob)
3902 got_object_blob_close(blob);
3903 if (error == NULL && kerr != KCGI_OK)
3904 error = gw_kcgi_error(kerr);
3905 return error;
3908 static const struct got_error *
3909 gw_output_repo_tree(struct gw_trans *gw_trans)
3911 const struct got_error *error = NULL;
3912 struct got_object_id *tree_id = NULL, *commit_id = NULL;
3913 struct got_tree_object *tree = NULL;
3914 char *path = NULL, *in_repo_path = NULL;
3915 char *id_str = NULL;
3916 char *build_folder = NULL;
3917 char *href_blob = NULL, *href_blame = NULL;
3918 const char *class = NULL;
3919 int nentries, i, class_flip = 0;
3920 enum kcgi_err kerr = KCGI_OK;
3922 if (gw_trans->repo_folder != NULL) {
3923 path = strdup(gw_trans->repo_folder);
3924 if (path == NULL) {
3925 error = got_error_from_errno("strdup");
3926 goto done;
3928 } else {
3929 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
3930 gw_trans->repo_path, 1);
3931 if (error)
3932 goto done;
3933 free(path);
3934 path = in_repo_path;
3937 if (gw_trans->commit_id == NULL) {
3938 struct got_reference *head_ref;
3939 error = got_ref_open(&head_ref, gw_trans->repo,
3940 gw_trans->headref, 0);
3941 if (error)
3942 goto done;
3943 error = got_ref_resolve(&commit_id, gw_trans->repo, head_ref);
3944 if (error)
3945 goto done;
3946 got_ref_close(head_ref);
3948 * gw_trans->commit_id was not parsed from the querystring
3949 * we hit this code path from gw_index, where we don't know the
3950 * commit values for the tree link yet, so set
3951 * gw_trans->commit_id here to continue further into the tree
3953 error = got_object_id_str(&gw_trans->commit_id, commit_id);
3954 if (error)
3955 goto done;
3957 } else {
3958 error = got_repo_match_object_id(&commit_id, NULL,
3959 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT, 1,
3960 gw_trans->repo);
3961 if (error)
3962 goto done;
3965 error = got_object_id_by_path(&tree_id, gw_trans->repo, commit_id,
3966 path);
3967 if (error)
3968 goto done;
3970 error = got_object_open_as_tree(&tree, gw_trans->repo, tree_id);
3971 if (error)
3972 goto done;
3974 nentries = got_object_tree_get_nentries(tree);
3975 for (i = 0; i < nentries; i++) {
3976 struct got_tree_entry *te;
3977 const char *modestr = "";
3978 mode_t mode;
3980 te = got_object_tree_get_entry(tree, i);
3982 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
3983 if (error)
3984 goto done;
3986 mode = got_tree_entry_get_mode(te);
3987 if (got_object_tree_entry_is_submodule(te))
3988 modestr = "$";
3989 else if (S_ISLNK(mode))
3990 modestr = "@";
3991 else if (S_ISDIR(mode))
3992 modestr = "/";
3993 else if (mode & S_IXUSR)
3994 modestr = "*";
3996 if (class_flip == 0) {
3997 class = "back_lightgray";
3998 class_flip = 1;
3999 } else {
4000 class = "back_white";
4001 class_flip = 0;
4004 if (S_ISDIR(mode)) {
4005 if (asprintf(&build_folder, "%s/%s",
4006 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4007 got_tree_entry_get_name(te)) == -1) {
4008 error = got_error_from_errno("asprintf");
4009 goto done;
4011 if (asprintf(&href_blob,
4012 "?path=%s&action=%s&commit=%s&folder=%s",
4013 gw_trans->repo_name, gw_get_action_name(gw_trans),
4014 gw_trans->commit_id, build_folder) == -1) {
4015 error = got_error_from_errno("asprintf");
4016 goto done;
4019 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4020 KATTR_ID, "tree_wrapper", KATTR__MAX);
4021 if (kerr != KCGI_OK)
4022 goto done;
4023 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4024 KATTR_ID, "tree_line", KATTR_CLASS, class,
4025 KATTR__MAX);
4026 if (kerr != KCGI_OK)
4027 goto done;
4028 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4029 KATTR_HREF, href_blob, KATTR_CLASS,
4030 "diff_directory", KATTR__MAX);
4031 if (kerr != KCGI_OK)
4032 goto done;
4033 kerr = khtml_puts(gw_trans->gw_html_req,
4034 got_tree_entry_get_name(te));
4035 if (kerr != KCGI_OK)
4036 goto done;
4037 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
4038 if (kerr != KCGI_OK)
4039 goto done;
4040 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4041 if (kerr != KCGI_OK)
4042 goto done;
4043 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4044 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
4045 KATTR__MAX);
4046 if (kerr != KCGI_OK)
4047 goto done;
4048 kerr = khtml_entity(gw_trans->gw_html_req,
4049 KENTITY_nbsp);
4050 if (kerr != KCGI_OK)
4051 goto done;
4052 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4053 if (kerr != KCGI_OK)
4054 goto done;
4055 } else {
4056 if (asprintf(&href_blob,
4057 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
4058 gw_trans->repo_name, "blob", gw_trans->commit_id,
4059 got_tree_entry_get_name(te),
4060 gw_trans->repo_folder ?
4061 gw_trans->repo_folder : "") == -1) {
4062 error = got_error_from_errno("asprintf");
4063 goto done;
4065 if (asprintf(&href_blame,
4066 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
4067 gw_trans->repo_name, "blame", gw_trans->commit_id,
4068 got_tree_entry_get_name(te),
4069 gw_trans->repo_folder ?
4070 gw_trans->repo_folder : "") == -1) {
4071 error = got_error_from_errno("asprintf");
4072 goto done;
4075 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4076 KATTR_ID, "tree_wrapper", KATTR__MAX);
4077 if (kerr != KCGI_OK)
4078 goto done;
4079 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4080 KATTR_ID, "tree_line", KATTR_CLASS, class,
4081 KATTR__MAX);
4082 if (kerr != KCGI_OK)
4083 goto done;
4084 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4085 KATTR_HREF, href_blob, KATTR__MAX);
4086 if (kerr != KCGI_OK)
4087 goto done;
4088 kerr = khtml_puts(gw_trans->gw_html_req,
4089 got_tree_entry_get_name(te));
4090 if (kerr != KCGI_OK)
4091 goto done;
4092 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
4093 if (kerr != KCGI_OK)
4094 goto done;
4095 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4096 if (kerr != KCGI_OK)
4097 goto done;
4098 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4099 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
4100 KATTR__MAX);
4101 if (kerr != KCGI_OK)
4102 goto done;
4104 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4105 KATTR_HREF, href_blob, KATTR__MAX);
4106 if (kerr != KCGI_OK)
4107 goto done;
4108 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
4109 if (kerr != KCGI_OK)
4110 goto done;
4111 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4112 if (kerr != KCGI_OK)
4113 goto done;
4115 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4116 if (kerr != KCGI_OK)
4117 goto done;
4119 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4120 KATTR_HREF, href_blame, KATTR__MAX);
4121 if (kerr != KCGI_OK)
4122 goto done;
4123 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
4124 if (kerr != KCGI_OK)
4125 goto done;
4127 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4128 if (kerr != KCGI_OK)
4129 goto done;
4131 free(id_str);
4132 id_str = NULL;
4133 free(href_blob);
4134 href_blob = NULL;
4135 free(build_folder);
4136 build_folder = NULL;
4138 done:
4139 if (tree)
4140 got_object_tree_close(tree);
4141 free(id_str);
4142 free(href_blob);
4143 free(href_blame);
4144 free(in_repo_path);
4145 free(tree_id);
4146 free(build_folder);
4147 if (error == NULL && kerr != KCGI_OK)
4148 error = gw_kcgi_error(kerr);
4149 return error;
4152 static const struct got_error *
4153 gw_output_repo_heads(struct gw_trans *gw_trans)
4155 const struct got_error *error = NULL;
4156 struct got_reflist_head refs;
4157 struct got_reflist_entry *re;
4158 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
4159 char *href_commits = NULL;
4160 enum kcgi_err kerr = KCGI_OK;
4162 SIMPLEQ_INIT(&refs);
4164 error = got_ref_list(&refs, gw_trans->repo, "refs/heads",
4165 got_ref_cmp_by_name, NULL);
4166 if (error)
4167 goto done;
4169 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4170 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
4171 if (kerr != KCGI_OK)
4172 goto done;
4173 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4174 KATTR_ID, "summary_heads_title", KATTR__MAX);
4175 if (kerr != KCGI_OK)
4176 goto done;
4177 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
4178 if (kerr != KCGI_OK)
4179 goto done;
4180 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4181 if (kerr != KCGI_OK)
4182 goto done;
4183 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4184 KATTR_ID, "summary_heads_content", KATTR__MAX);
4185 if (kerr != KCGI_OK)
4186 goto done;
4188 SIMPLEQ_FOREACH(re, &refs, entry) {
4189 const char *refname;
4191 if (got_ref_is_symbolic(re->ref))
4192 continue;
4194 refname = got_ref_get_name(re->ref);
4195 if (strncmp(refname, "refs/heads/", 11) != 0)
4196 continue;
4198 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
4199 refname, TM_DIFF);
4200 if (error)
4201 goto done;
4203 if (strncmp(refname, "refs/heads/", 11) == 0)
4204 refname += 11;
4206 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4207 KATTR_ID, "heads_wrapper", KATTR__MAX);
4208 if (kerr != KCGI_OK)
4209 goto done;
4210 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4211 KATTR_ID, "heads_age", KATTR__MAX);
4212 if (kerr != KCGI_OK)
4213 goto done;
4214 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
4215 if (kerr != KCGI_OK)
4216 goto done;
4217 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4218 if (kerr != KCGI_OK)
4219 goto done;
4220 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4221 KATTR_ID, "heads_space", KATTR__MAX);
4222 if (kerr != KCGI_OK)
4223 goto done;
4224 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
4225 if (kerr != KCGI_OK)
4226 goto done;
4227 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4228 if (kerr != KCGI_OK)
4229 goto done;
4230 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4231 KATTR_ID, "head", KATTR__MAX);
4232 if (kerr != KCGI_OK)
4233 goto done;
4234 if (asprintf(&href_summary,
4235 "?path=%s&action=summary&headref=%s",
4236 gw_trans->repo_name, refname) == -1) {
4237 error = got_error_from_errno("asprintf");
4238 goto done;
4240 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4241 href_summary, KATTR__MAX);
4242 kerr = khtml_puts(gw_trans->gw_html_req, refname);
4243 if (kerr != KCGI_OK)
4244 goto done;
4245 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4246 if (kerr != KCGI_OK)
4247 goto done;
4249 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4250 "navs_wrapper", KATTR__MAX);
4251 if (kerr != KCGI_OK)
4252 goto done;
4253 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4254 "navs", KATTR__MAX);
4255 if (kerr != KCGI_OK)
4256 goto done;
4258 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4259 href_summary, KATTR__MAX);
4260 if (kerr != KCGI_OK)
4261 goto done;
4262 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4263 if (kerr != KCGI_OK)
4264 goto done;
4265 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4266 if (kerr != KCGI_OK)
4267 goto done;
4269 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4270 if (kerr != KCGI_OK)
4271 goto done;
4272 if (asprintf(&href_briefs, "?path=%s&action=briefs&headref=%s",
4273 gw_trans->repo_name, refname) == -1) {
4274 error = got_error_from_errno("asprintf");
4275 goto done;
4277 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4278 href_briefs, KATTR__MAX);
4279 if (kerr != KCGI_OK)
4280 goto done;
4281 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4282 if (kerr != KCGI_OK)
4283 goto done;
4284 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4285 if (kerr != KCGI_OK)
4286 goto done;
4288 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4289 if (kerr != KCGI_OK)
4290 goto done;
4292 if (asprintf(&href_commits,
4293 "?path=%s&action=commits&headref=%s",
4294 gw_trans->repo_name, refname) == -1) {
4295 error = got_error_from_errno("asprintf");
4296 goto done;
4298 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4299 href_commits, KATTR__MAX);
4300 if (kerr != KCGI_OK)
4301 goto done;
4302 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4303 if (kerr != KCGI_OK)
4304 goto done;
4305 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4306 if (kerr != KCGI_OK)
4307 goto done;
4309 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4310 "dotted_line", KATTR__MAX);
4311 if (kerr != KCGI_OK)
4312 goto done;
4313 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4314 if (kerr != KCGI_OK)
4315 goto done;
4316 free(href_summary);
4317 href_summary = NULL;
4318 free(href_briefs);
4319 href_briefs = NULL;
4320 free(href_commits);
4321 href_commits = NULL;
4323 done:
4324 got_ref_list_free(&refs);
4325 free(href_summary);
4326 free(href_briefs);
4327 free(href_commits);
4328 return error;
4331 static const struct got_error *
4332 gw_output_site_link(struct gw_trans *gw_trans)
4334 const struct got_error *error = NULL;
4335 char *href_summary = NULL;
4336 enum kcgi_err kerr = KCGI_OK;
4338 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4339 "site_link", KATTR__MAX);
4340 if (kerr != KCGI_OK)
4341 goto done;
4342 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4343 KATTR__MAX);
4344 if (kerr != KCGI_OK)
4345 goto done;
4346 kerr = khtml_puts(gw_trans->gw_html_req,
4347 gw_trans->gw_conf->got_site_link);
4348 if (kerr != KCGI_OK)
4349 goto done;
4350 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4351 if (kerr != KCGI_OK)
4352 goto done;
4354 if (gw_trans->repo_name != NULL) {
4355 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4356 if (kerr != KCGI_OK)
4357 goto done;
4358 if (asprintf(&href_summary, "?path=%s&action=summary",
4359 gw_trans->repo_name) == -1)
4360 goto done;
4361 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4362 href_summary, KATTR__MAX);
4363 if (kerr != KCGI_OK)
4364 goto done;
4365 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4366 if (kerr != KCGI_OK)
4367 goto done;
4368 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4369 if (kerr != KCGI_OK)
4370 goto done;
4371 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4372 if (kerr != KCGI_OK)
4373 goto done;
4374 kerr = khtml_puts(gw_trans->gw_html_req,
4375 gw_get_action_name(gw_trans));
4376 if (kerr != KCGI_OK)
4377 goto done;
4380 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4381 if (kerr != KCGI_OK)
4382 goto done;
4383 done:
4384 free(href_summary);
4385 return error;
4388 static const struct got_error *
4389 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4391 const struct got_error *error = NULL;
4392 char *color = NULL;
4393 enum kcgi_err kerr = KCGI_OK;
4395 if (strncmp(buf, "-", 1) == 0)
4396 color = "diff_minus";
4397 else if (strncmp(buf, "+", 1) == 0)
4398 color = "diff_plus";
4399 else if (strncmp(buf, "@@", 2) == 0)
4400 color = "diff_chunk_header";
4401 else if (strncmp(buf, "@@", 2) == 0)
4402 color = "diff_chunk_header";
4403 else if (strncmp(buf, "commit +", 8) == 0)
4404 color = "diff_meta";
4405 else if (strncmp(buf, "commit -", 8) == 0)
4406 color = "diff_meta";
4407 else if (strncmp(buf, "blob +", 6) == 0)
4408 color = "diff_meta";
4409 else if (strncmp(buf, "blob -", 6) == 0)
4410 color = "diff_meta";
4411 else if (strncmp(buf, "file +", 6) == 0)
4412 color = "diff_meta";
4413 else if (strncmp(buf, "file -", 6) == 0)
4414 color = "diff_meta";
4415 else if (strncmp(buf, "from:", 5) == 0)
4416 color = "diff_author";
4417 else if (strncmp(buf, "via:", 4) == 0)
4418 color = "diff_author";
4419 else if (strncmp(buf, "date:", 5) == 0)
4420 color = "diff_date";
4421 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4422 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4423 if (error == NULL && kerr != KCGI_OK)
4424 error = gw_kcgi_error(kerr);
4425 return error;
4428 int
4429 main(int argc, char *argv[])
4431 const struct got_error *error = NULL;
4432 struct gw_trans *gw_trans;
4433 struct gw_dir *dir = NULL, *tdir;
4434 const char *page = "index";
4435 int gw_malloc = 1;
4436 enum kcgi_err kerr;
4438 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4439 errx(1, "malloc");
4441 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4442 errx(1, "malloc");
4444 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4445 errx(1, "malloc");
4447 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4448 errx(1, "malloc");
4450 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4451 if (kerr != KCGI_OK) {
4452 error = gw_kcgi_error(kerr);
4453 goto done;
4456 if ((gw_trans->gw_conf =
4457 malloc(sizeof(struct gotweb_conf))) == NULL) {
4458 gw_malloc = 0;
4459 error = got_error_from_errno("malloc");
4460 goto done;
4463 TAILQ_INIT(&gw_trans->gw_dirs);
4464 TAILQ_INIT(&gw_trans->gw_headers);
4466 gw_trans->action = -1;
4467 gw_trans->page = 0;
4468 gw_trans->repos_total = 0;
4469 gw_trans->repo_path = NULL;
4470 gw_trans->commit_id = NULL;
4471 gw_trans->next_id = NULL;
4472 gw_trans->next_prev_id = NULL;
4473 gw_trans->prev_id = NULL;
4474 gw_trans->prev_prev_id = NULL;
4475 gw_trans->headref = GOT_REF_HEAD;
4476 gw_trans->mime = KMIME_TEXT_HTML;
4477 gw_trans->gw_tmpl->key = gw_templs;
4478 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4479 gw_trans->gw_tmpl->arg = gw_trans;
4480 gw_trans->gw_tmpl->cb = gw_template;
4481 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
4482 if (error)
4483 goto done;
4485 error = gw_parse_querystring(gw_trans);
4486 if (error)
4487 goto done;
4489 if (gw_trans->action == GW_BLOB)
4490 error = gw_blob(gw_trans);
4491 else
4492 error = gw_display_index(gw_trans);
4493 done:
4494 if (gw_malloc) {
4495 free(gw_trans->gw_conf->got_repos_path);
4496 free(gw_trans->gw_conf->got_site_name);
4497 free(gw_trans->gw_conf->got_site_owner);
4498 free(gw_trans->gw_conf->got_site_link);
4499 free(gw_trans->gw_conf->got_logo);
4500 free(gw_trans->gw_conf->got_logo_url);
4501 free(gw_trans->gw_conf);
4502 free(gw_trans->commit_id);
4503 free(gw_trans->next_id);
4504 free(gw_trans->next_prev_id);
4505 free(gw_trans->prev_id);
4506 free(gw_trans->prev_prev_id);
4507 free(gw_trans->repo_path);
4508 if (gw_trans->repo)
4509 got_repo_close(gw_trans->repo);
4511 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4512 free(dir->name);
4513 free(dir->description);
4514 free(dir->age);
4515 free(dir->url);
4516 free(dir->path);
4517 free(dir);
4522 khttp_free(gw_trans->gw_req);
4523 return 0;