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 const char *repo_file;
69 char *repo_folder;
70 const char *headref;
71 unsigned int action;
72 unsigned int page;
73 unsigned int repos_total;
74 enum kmime mime;
75 };
77 struct gw_header {
78 TAILQ_ENTRY(gw_header) entry;
79 struct got_reflist_head refs;
80 char *path;
82 char *refs_str;
83 char *commit_id; /* id_str1 */
84 char *parent_id; /* id_str2 */
85 char *tree_id;
86 char *author;
87 char *committer;
88 char *commit_msg;
89 time_t committer_time;
90 };
92 struct gw_dir {
93 TAILQ_ENTRY(gw_dir) entry;
94 char *name;
95 char *owner;
96 char *description;
97 char *url;
98 char *age;
99 char *path;
100 };
102 enum gw_key {
103 KEY_ACTION,
104 KEY_COMMIT_ID,
105 KEY_FILE,
106 KEY_FOLDER,
107 KEY_HEADREF,
108 KEY_PAGE,
109 KEY_PATH,
110 KEY__ZMAX
111 };
113 enum gw_tmpl {
114 TEMPL_CONTENT,
115 TEMPL_HEAD,
116 TEMPL_HEADER,
117 TEMPL_SEARCH,
118 TEMPL_SITEPATH,
119 TEMPL_SITEOWNER,
120 TEMPL_TITLE,
121 TEMPL__MAX
122 };
124 enum gw_ref_tm {
125 TM_DIFF,
126 TM_LONG,
127 };
129 enum gw_tags {
130 TAGBRIEF,
131 TAGFULL,
132 };
134 static const char *const gw_templs[TEMPL__MAX] = {
135 "content",
136 "head",
137 "header",
138 "search",
139 "sitepath",
140 "siteowner",
141 "title",
142 };
144 static const struct kvalid gw_keys[KEY__ZMAX] = {
145 { kvalid_stringne, "action" },
146 { kvalid_stringne, "commit" },
147 { kvalid_stringne, "file" },
148 { kvalid_stringne, "folder" },
149 { kvalid_stringne, "headref" },
150 { kvalid_int, "page" },
151 { kvalid_stringne, "path" },
152 };
154 static struct gw_header *gw_init_header(void);
156 static void gw_free_header(struct gw_header *);
158 static int gw_template(size_t, void *);
160 static const struct got_error *gw_error(struct gw_trans *);
161 static const struct got_error *gw_init_gw_dir(struct gw_dir **, const char *);
162 static const struct got_error *gw_get_repo_description(char **,
163 struct gw_trans *, char *);
164 static const struct got_error *gw_get_repo_owner(char **, struct gw_trans *,
165 char *);
166 static const struct got_error *gw_get_time_str(char **, time_t, int);
167 static const struct got_error *gw_get_repo_age(char **, struct gw_trans *,
168 char *, const char *, int);
169 static const struct got_error *gw_output_file_blame(struct gw_trans *);
170 static const struct got_error *gw_output_blob_buf(struct gw_trans *);
171 static const struct got_error *gw_output_repo_tree(struct gw_trans *);
172 static const struct got_error *gw_output_diff(struct gw_trans *,
173 struct gw_header *);
174 static const struct got_error *gw_output_repo_tags(struct gw_trans *,
175 struct gw_header *, int, int);
176 static const struct got_error *gw_output_repo_heads(struct gw_trans *);
177 static const struct got_error *gw_output_site_link(struct gw_trans *);
178 static const struct got_error *gw_get_clone_url(char **, struct gw_trans *,
179 char *);
180 static const struct got_error *gw_colordiff_line(struct gw_trans *, char *);
182 static const struct got_error *gw_gen_commit_header(struct gw_trans *, char *,
183 char*);
184 static const struct got_error *gw_gen_diff_header(struct gw_trans *, char *,
185 char*);
186 static const struct got_error *gw_gen_author_header(struct gw_trans *,
187 const char *);
188 static const struct got_error *gw_gen_age_header(struct gw_trans *,
189 const char *);
190 static const struct got_error *gw_gen_committer_header(struct gw_trans *,
191 const char *);
192 static const struct got_error *gw_gen_commit_msg_header(struct gw_trans*,
193 char *);
194 static const struct got_error *gw_gen_tree_header(struct gw_trans *, char *);
195 static const struct got_error *gw_display_open(struct gw_trans *, enum khttp,
196 enum kmime);
197 static const struct got_error *gw_display_index(struct gw_trans *);
198 static const struct got_error *gw_get_header(struct gw_trans *,
199 struct gw_header *, int);
200 static const struct got_error *gw_get_commits(struct gw_trans *,
201 struct gw_header *, int,
202 struct got_object_id *);
203 static const struct got_error *gw_get_commit(struct gw_trans *,
204 struct gw_header *,
205 struct got_commit_object *,
206 struct got_object_id *);
207 static const struct got_error *gw_apply_unveil(const char *);
208 static const struct got_error *gw_blame_cb(void *, int, int,
209 struct got_object_id *);
210 static const struct got_error *gw_load_got_paths(struct gw_trans *);
211 static const struct got_error *gw_load_got_path(struct gw_trans *,
212 struct gw_dir *);
213 static const struct got_error *gw_parse_querystring(struct gw_trans *);
214 static const struct got_error *gw_blame(struct gw_trans *);
215 static const struct got_error *gw_blob(struct gw_trans *);
216 static const struct got_error *gw_diff(struct gw_trans *);
217 static const struct got_error *gw_index(struct gw_trans *);
218 static const struct got_error *gw_commits(struct gw_trans *);
219 static const struct got_error *gw_briefs(struct gw_trans *);
220 static const struct got_error *gw_summary(struct gw_trans *);
221 static const struct got_error *gw_tree(struct gw_trans *);
222 static const struct got_error *gw_tag(struct gw_trans *);
224 struct gw_query_action {
225 unsigned int func_id;
226 const char *func_name;
227 const struct got_error *(*func_main)(struct gw_trans *);
228 char *template;
229 };
231 enum gw_query_actions {
232 GW_BLAME,
233 GW_BLOB,
234 GW_BRIEFS,
235 GW_COMMITS,
236 GW_DIFF,
237 GW_ERR,
238 GW_INDEX,
239 GW_SUMMARY,
240 GW_TAG,
241 GW_TREE,
242 };
244 static struct gw_query_action gw_query_funcs[] = {
245 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
246 { GW_BLOB, "blob", NULL, NULL },
247 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
248 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
249 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
250 { GW_ERR, "error", gw_error, "gw_tmpl/err.tmpl" },
251 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
252 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
253 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
254 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
255 };
257 static const char *
258 gw_get_action_name(struct gw_trans *gw_trans)
260 return gw_query_funcs[gw_trans->action].func_name;
263 static const struct got_error *
264 gw_kcgi_error(enum kcgi_err kerr)
266 if (kerr == KCGI_OK)
267 return NULL;
269 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
270 return got_error(GOT_ERR_CANCELLED);
272 if (kerr == KCGI_ENOMEM)
273 return got_error_set_errno(ENOMEM,
274 kcgi_strerror(kerr));
276 if (kerr == KCGI_ENFILE)
277 return got_error_set_errno(ENFILE,
278 kcgi_strerror(kerr));
280 if (kerr == KCGI_EAGAIN)
281 return got_error_set_errno(EAGAIN,
282 kcgi_strerror(kerr));
284 if (kerr == KCGI_FORM)
285 return got_error_msg(GOT_ERR_IO,
286 kcgi_strerror(kerr));
288 return got_error_from_errno(kcgi_strerror(kerr));
291 static const struct got_error *
292 gw_apply_unveil(const char *repo_path)
294 const struct got_error *err;
296 if (repo_path && unveil(repo_path, "r") != 0)
297 return got_error_from_errno2("unveil", repo_path);
299 if (unveil("/tmp", "rwc") != 0)
300 return got_error_from_errno2("unveil", "/tmp");
302 err = got_privsep_unveil_exec_helpers();
303 if (err != NULL)
304 return err;
306 if (unveil(NULL, NULL) != 0)
307 return got_error_from_errno("unveil");
309 return NULL;
312 static int
313 isbinary(const uint8_t *buf, size_t n)
315 size_t i;
317 for (i = 0; i < n; i++)
318 if (buf[i] == 0)
319 return 1;
320 return 0;
323 static const struct got_error *
324 gw_blame(struct gw_trans *gw_trans)
326 const struct got_error *error = NULL;
327 struct gw_header *header = NULL;
328 char *age = NULL;
329 enum kcgi_err kerr = KCGI_OK;
331 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
332 NULL) == -1)
333 return got_error_from_errno("pledge");
335 if ((header = gw_init_header()) == NULL)
336 return got_error_from_errno("malloc");
338 error = gw_apply_unveil(gw_trans->gw_dir->path);
339 if (error)
340 goto done;
342 /* check querystring */
343 if (gw_trans->repo_file == NULL) {
344 error = got_error_msg(GOT_ERR_QUERYSTRING,
345 "file required in querystring");
346 goto done;
348 if (gw_trans->commit_id == NULL) {
349 error = got_error_msg(GOT_ERR_QUERYSTRING,
350 "commit required in querystring");
351 goto done;
354 error = gw_get_header(gw_trans, header, 1);
355 if (error)
356 goto done;
357 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
358 "blame_header_wrapper", KATTR__MAX);
359 if (kerr != KCGI_OK)
360 goto done;
361 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
362 "blame_header", KATTR__MAX);
363 if (kerr != KCGI_OK)
364 goto done;
365 error = gw_get_time_str(&age, header->committer_time,
366 TM_LONG);
367 if (error)
368 goto done;
369 error = gw_gen_age_header(gw_trans, age ?age : "");
370 if (error)
371 goto done;
372 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
373 if (error)
374 goto done;
375 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
376 if (kerr != KCGI_OK)
377 goto done;
378 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
379 "dotted_line", KATTR__MAX);
380 if (kerr != KCGI_OK)
381 goto done;
382 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
383 if (kerr != KCGI_OK)
384 goto done;
386 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
387 "blame", KATTR__MAX);
388 if (kerr != KCGI_OK)
389 goto done;
390 error = gw_output_file_blame(gw_trans);
391 if (error)
392 goto done;
393 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
394 done:
395 gw_free_header(header);
396 if (error == NULL && kerr != KCGI_OK)
397 error = gw_kcgi_error(kerr);
398 return error;
401 static const struct got_error *
402 gw_blob(struct gw_trans *gw_trans)
404 const struct got_error *error = NULL, *err = NULL;
405 struct gw_header *header = NULL;
406 enum kcgi_err kerr = KCGI_OK;
408 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
409 NULL) == -1)
410 return got_error_from_errno("pledge");
412 if ((header = gw_init_header()) == NULL)
413 return got_error_from_errno("malloc");
415 error = gw_apply_unveil(gw_trans->gw_dir->path);
416 if (error)
417 goto done;
419 /* check querystring */
420 if (gw_trans->repo_file == NULL) {
421 error = got_error_msg(GOT_ERR_QUERYSTRING,
422 "file required in querystring");
423 goto done;
425 if (gw_trans->commit_id == NULL) {
426 error = got_error_msg(GOT_ERR_QUERYSTRING,
427 "commit required in querystring");
428 goto done;
430 error = gw_get_header(gw_trans, header, 1);
431 if (error)
432 goto done;
434 error = gw_output_blob_buf(gw_trans);
435 done:
437 if (error) {
438 gw_trans->mime = KMIME_TEXT_PLAIN;
439 err = gw_display_index(gw_trans);
440 if (err) {
441 error = err;
442 goto errored;
444 kerr = khttp_puts(gw_trans->gw_req, error->msg);
446 errored:
447 gw_free_header(header);
448 if (error == NULL && kerr != KCGI_OK)
449 error = gw_kcgi_error(kerr);
450 return error;
453 static const struct got_error *
454 gw_diff(struct gw_trans *gw_trans)
456 const struct got_error *error = NULL;
457 struct gw_header *header = NULL;
458 char *age = NULL;
459 enum kcgi_err kerr = KCGI_OK;
461 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
462 NULL) == -1)
463 return got_error_from_errno("pledge");
465 if ((header = gw_init_header()) == NULL)
466 return got_error_from_errno("malloc");
468 error = gw_apply_unveil(gw_trans->gw_dir->path);
469 if (error)
470 goto done;
472 error = gw_get_header(gw_trans, header, 1);
473 if (error)
474 goto done;
476 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
477 "diff_header_wrapper", KATTR__MAX);
478 if (kerr != KCGI_OK)
479 goto done;
480 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
481 "diff_header", KATTR__MAX);
482 if (kerr != KCGI_OK)
483 goto done;
484 error = gw_gen_diff_header(gw_trans, header->parent_id,
485 header->commit_id);
486 if (error)
487 goto done;
488 error = gw_gen_commit_header(gw_trans, header->commit_id,
489 header->refs_str);
490 if (error)
491 goto done;
492 error = gw_gen_tree_header(gw_trans, header->tree_id);
493 if (error)
494 goto done;
495 error = gw_gen_author_header(gw_trans, header->author);
496 if (error)
497 goto done;
498 error = gw_gen_committer_header(gw_trans, header->author);
499 if (error)
500 goto done;
501 error = gw_get_time_str(&age, header->committer_time,
502 TM_LONG);
503 if (error)
504 goto done;
505 error = gw_gen_age_header(gw_trans, age ?age : "");
506 if (error)
507 goto done;
508 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
509 if (error)
510 goto done;
511 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
512 if (kerr != KCGI_OK)
513 goto done;
514 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
515 "dotted_line", KATTR__MAX);
516 if (kerr != KCGI_OK)
517 goto done;
518 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
519 if (kerr != KCGI_OK)
520 goto done;
522 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
523 "diff", KATTR__MAX);
524 if (kerr != KCGI_OK)
525 goto done;
526 error = gw_output_diff(gw_trans, header);
527 if (error)
528 goto done;
530 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
531 done:
532 gw_free_header(header);
533 free(age);
534 if (error == NULL && kerr != KCGI_OK)
535 error = gw_kcgi_error(kerr);
536 return error;
539 static const struct got_error *
540 gw_index(struct gw_trans *gw_trans)
542 const struct got_error *error = NULL;
543 struct gw_dir *gw_dir = NULL;
544 char *href_next = NULL, *href_prev = NULL, *href_summary = NULL;
545 char *href_briefs = NULL, *href_commits = NULL, *href_tree = NULL;
546 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
547 enum kcgi_err kerr;
549 if (pledge("stdio rpath proc exec sendfd unveil",
550 NULL) == -1) {
551 error = got_error_from_errno("pledge");
552 return error;
555 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path);
556 if (error)
557 return error;
559 error = gw_load_got_paths(gw_trans);
560 if (error)
561 return error;
563 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
564 "index_header", KATTR__MAX);
565 if (kerr != KCGI_OK)
566 return gw_kcgi_error(kerr);
567 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
568 "index_header_project", KATTR__MAX);
569 if (kerr != KCGI_OK)
570 return gw_kcgi_error(kerr);
571 kerr = khtml_puts(gw_trans->gw_html_req, "Project");
572 if (kerr != KCGI_OK)
573 return gw_kcgi_error(kerr);
574 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
575 if (kerr != KCGI_OK)
576 return gw_kcgi_error(kerr);
578 if (gw_trans->gw_conf->got_show_repo_description) {
579 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
580 "index_header_description", KATTR__MAX);
581 if (kerr != KCGI_OK)
582 return gw_kcgi_error(kerr);
583 kerr = khtml_puts(gw_trans->gw_html_req, "Description");
584 if (kerr != KCGI_OK)
585 return gw_kcgi_error(kerr);
586 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
587 if (kerr != KCGI_OK)
588 return gw_kcgi_error(kerr);
591 if (gw_trans->gw_conf->got_show_repo_owner) {
592 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
593 "index_header_owner", KATTR__MAX);
594 if (kerr != KCGI_OK)
595 return gw_kcgi_error(kerr);
596 kerr = khtml_puts(gw_trans->gw_html_req, "Owner");
597 if (kerr != KCGI_OK)
598 return gw_kcgi_error(kerr);
599 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
600 if (kerr != KCGI_OK)
601 return gw_kcgi_error(kerr);
604 if (gw_trans->gw_conf->got_show_repo_age) {
605 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
606 "index_header_age", KATTR__MAX);
607 if (kerr != KCGI_OK)
608 return gw_kcgi_error(kerr);
609 kerr = khtml_puts(gw_trans->gw_html_req, "Last Change");
610 if (kerr != KCGI_OK)
611 return gw_kcgi_error(kerr);
612 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
613 if (kerr != KCGI_OK)
614 return gw_kcgi_error(kerr);
617 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
618 if (kerr != KCGI_OK)
619 return gw_kcgi_error(kerr);
621 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
622 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
623 "index_wrapper", KATTR__MAX);
624 if (kerr != KCGI_OK)
625 return gw_kcgi_error(kerr);
626 kerr = khtml_puts(gw_trans->gw_html_req,
627 "No repositories found in ");
628 if (kerr != KCGI_OK)
629 return gw_kcgi_error(kerr);
630 kerr = khtml_puts(gw_trans->gw_html_req,
631 gw_trans->gw_conf->got_repos_path);
632 if (kerr != KCGI_OK)
633 return gw_kcgi_error(kerr);
634 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
635 if (kerr != KCGI_OK)
636 return gw_kcgi_error(kerr);
637 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
638 "dotted_line", KATTR__MAX);
639 if (kerr != KCGI_OK)
640 return gw_kcgi_error(kerr);
641 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
642 if (kerr != KCGI_OK)
643 return gw_kcgi_error(kerr);
644 return error;
647 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
648 dir_c++;
650 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
651 if (gw_trans->page > 0 && (gw_trans->page *
652 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
653 prev_disp++;
654 continue;
657 prev_disp++;
659 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
660 "index_wrapper", KATTR__MAX);
661 if (kerr != KCGI_OK)
662 goto done;
664 if (asprintf(&href_summary, "?path=%s&action=summary",
665 gw_dir->name) == -1) {
666 error = got_error_from_errno("asprintf");
667 goto done;
669 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
670 "index_project", KATTR__MAX);
671 if (kerr != KCGI_OK)
672 goto done;
673 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
674 href_summary, KATTR__MAX);
675 if (kerr != KCGI_OK)
676 goto done;
677 kerr = khtml_puts(gw_trans->gw_html_req, gw_dir->name);
678 if (kerr != KCGI_OK)
679 goto done;
680 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
681 if (kerr != KCGI_OK)
682 goto done;
683 if (gw_trans->gw_conf->got_show_repo_description) {
684 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
685 KATTR_ID, "index_project_description", KATTR__MAX);
686 if (kerr != KCGI_OK)
687 goto done;
688 kerr = khtml_puts(gw_trans->gw_html_req,
689 gw_dir->description ? gw_dir->description : "");
690 if (kerr != KCGI_OK)
691 goto done;
692 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
693 if (kerr != KCGI_OK)
694 goto done;
696 if (gw_trans->gw_conf->got_show_repo_owner) {
697 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
698 KATTR_ID, "index_project_owner", KATTR__MAX);
699 if (kerr != KCGI_OK)
700 goto done;
701 kerr = khtml_puts(gw_trans->gw_html_req,
702 gw_dir->owner ? gw_dir->owner : "");
703 if (kerr != KCGI_OK)
704 goto done;
705 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
706 if (kerr != KCGI_OK)
707 goto done;
709 if (gw_trans->gw_conf->got_show_repo_age) {
710 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
711 KATTR_ID, "index_project_age", KATTR__MAX);
712 if (kerr != KCGI_OK)
713 goto done;
714 kerr = khtml_puts(gw_trans->gw_html_req,
715 gw_dir->age ? gw_dir->age : "");
716 if (kerr != KCGI_OK)
717 goto done;
718 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
719 if (kerr != KCGI_OK)
720 goto done;
723 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
724 "navs_wrapper", KATTR__MAX);
725 if (kerr != KCGI_OK)
726 goto done;
727 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
728 "navs", KATTR__MAX);
729 if (kerr != KCGI_OK)
730 goto done;
732 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
733 href_summary, KATTR__MAX);
734 if (kerr != KCGI_OK)
735 goto done;
736 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
737 if (kerr != KCGI_OK)
738 goto done;
739 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
740 if (kerr != KCGI_OK)
741 goto done;
743 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
744 if (kerr != KCGI_OK)
745 goto done;
747 if (asprintf(&href_briefs, "?path=%s&action=briefs",
748 gw_dir->name) == -1) {
749 error = got_error_from_errno("asprintf");
750 goto done;
752 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
753 href_briefs, KATTR__MAX);
754 if (kerr != KCGI_OK)
755 goto done;
756 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
757 if (kerr != KCGI_OK)
758 goto done;
759 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
760 if (kerr != KCGI_OK)
761 error = gw_kcgi_error(kerr);
763 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
764 if (kerr != KCGI_OK)
765 goto done;
767 if (asprintf(&href_commits, "?path=%s&action=commits",
768 gw_dir->name) == -1) {
769 error = got_error_from_errno("asprintf");
770 goto done;
772 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
773 href_commits, KATTR__MAX);
774 if (kerr != KCGI_OK)
775 goto done;
776 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
777 if (kerr != KCGI_OK)
778 goto done;
779 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
780 if (kerr != KCGI_OK)
781 goto done;
783 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
784 if (kerr != KCGI_OK)
785 goto done;
787 if (asprintf(&href_tree, "?path=%s&action=tree",
788 gw_dir->name) == -1) {
789 error = got_error_from_errno("asprintf");
790 goto done;
792 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
793 href_tree, KATTR__MAX);
794 if (kerr != KCGI_OK)
795 goto done;
796 kerr = khtml_puts(gw_trans->gw_html_req, "tree");
797 if (kerr != KCGI_OK)
798 goto done;
800 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
801 if (kerr != KCGI_OK)
802 goto done;
803 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
804 "dotted_line", KATTR__MAX);
805 if (kerr != KCGI_OK)
806 goto done;
807 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
808 if (kerr != KCGI_OK)
809 goto done;
811 free(href_summary);
812 href_summary = NULL;
813 free(href_briefs);
814 href_briefs = NULL;
815 free(href_commits);
816 href_commits = NULL;
817 free(href_tree);
818 href_tree = NULL;
820 if (gw_trans->gw_conf->got_max_repos_display == 0)
821 continue;
823 if (next_disp == gw_trans->gw_conf->got_max_repos_display) {
824 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
825 KATTR_ID, "np_wrapper", KATTR__MAX);
826 if (kerr != KCGI_OK)
827 goto done;
828 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
829 KATTR_ID, "nav_prev", KATTR__MAX);
830 if (kerr != KCGI_OK)
831 goto done;
832 } else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
833 (gw_trans->page > 0) &&
834 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
835 prev_disp == gw_trans->repos_total)) {
836 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
837 KATTR_ID, "np_wrapper", KATTR__MAX);
838 if (kerr != KCGI_OK)
839 goto done;
840 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
841 KATTR_ID, "nav_prev", KATTR__MAX);
842 if (kerr != KCGI_OK)
843 goto done;
846 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
847 (gw_trans->page > 0) &&
848 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
849 prev_disp == gw_trans->repos_total)) {
850 if (asprintf(&href_prev, "?page=%d",
851 gw_trans->page - 1) == -1) {
852 error = got_error_from_errno("asprintf");
853 goto done;
855 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
856 KATTR_HREF, href_prev, KATTR__MAX);
857 if (kerr != KCGI_OK)
858 goto done;
859 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
860 if (kerr != KCGI_OK)
861 goto done;
862 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
863 if (kerr != KCGI_OK)
864 goto done;
867 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
868 if (kerr != KCGI_OK)
869 return gw_kcgi_error(kerr);
871 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
872 next_disp == gw_trans->gw_conf->got_max_repos_display &&
873 dir_c != (gw_trans->page + 1) *
874 gw_trans->gw_conf->got_max_repos_display) {
875 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
876 KATTR_ID, "nav_next", KATTR__MAX);
877 if (kerr != KCGI_OK)
878 goto done;
879 if (asprintf(&href_next, "?page=%d",
880 gw_trans->page + 1) == -1) {
881 error = got_error_from_errno("calloc");
882 goto done;
884 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
885 KATTR_HREF, href_next, KATTR__MAX);
886 if (kerr != KCGI_OK)
887 goto done;
888 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
889 if (kerr != KCGI_OK)
890 goto done;
891 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
892 if (kerr != KCGI_OK)
893 goto done;
894 next_disp = 0;
895 break;
898 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
899 (gw_trans->page > 0) &&
900 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
901 prev_disp == gw_trans->repos_total)) {
902 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
903 if (kerr != KCGI_OK)
904 goto done;
906 next_disp++;
908 done:
909 free(href_prev);
910 free(href_next);
911 free(href_summary);
912 free(href_briefs);
913 free(href_commits);
914 free(href_tree);
915 if (error == NULL && kerr != KCGI_OK)
916 error = gw_kcgi_error(kerr);
917 return error;
920 static const struct got_error *
921 gw_commits(struct gw_trans *gw_trans)
923 const struct got_error *error = NULL;
924 struct gw_header *header = NULL, *n_header = NULL;
925 char *age = NULL;
926 char *href_diff = NULL, *href_blob = NULL;
927 enum kcgi_err kerr = KCGI_OK;
929 if ((header = gw_init_header()) == NULL)
930 return got_error_from_errno("malloc");
932 if (pledge("stdio rpath proc exec sendfd unveil",
933 NULL) == -1) {
934 error = got_error_from_errno("pledge");
935 goto done;
938 error = gw_apply_unveil(gw_trans->gw_dir->path);
939 if (error)
940 goto done;
942 error = gw_get_header(gw_trans, header,
943 gw_trans->gw_conf->got_max_commits_display);
944 if (error)
945 goto done;
947 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
948 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
949 "commits_line_wrapper", KATTR__MAX);
950 if (kerr != KCGI_OK)
951 goto done;
952 error = gw_gen_commit_header(gw_trans, n_header->commit_id,
953 n_header->refs_str);
954 if (error)
955 goto done;
956 error = gw_gen_author_header(gw_trans, n_header->author);
957 if (error)
958 goto done;
959 error = gw_gen_committer_header(gw_trans, n_header->author);
960 if (error)
961 goto done;
962 error = gw_get_time_str(&age, n_header->committer_time,
963 TM_LONG);
964 if (error)
965 goto done;
966 error = gw_gen_age_header(gw_trans, age ?age : "");
967 if (error)
968 goto done;
969 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
970 if (kerr != KCGI_OK)
971 goto done;
973 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
974 "dotted_line", KATTR__MAX);
975 if (kerr != KCGI_OK)
976 goto done;
977 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
978 if (kerr != KCGI_OK)
979 goto done;
981 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
982 "commit", KATTR__MAX);
983 if (kerr != KCGI_OK)
984 goto done;
985 kerr = khttp_puts(gw_trans->gw_req, n_header->commit_msg);
986 if (kerr != KCGI_OK)
987 goto done;
988 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
989 if (kerr != KCGI_OK)
990 goto done;
992 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
993 gw_trans->repo_name, n_header->commit_id) == -1) {
994 error = got_error_from_errno("asprintf");
995 goto done;
997 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
998 KATTR_ID, "navs_wrapper", KATTR__MAX);
999 if (kerr != KCGI_OK)
1000 goto done;
1001 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1002 KATTR_ID, "navs", KATTR__MAX);
1003 if (kerr != KCGI_OK)
1004 goto done;
1005 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1006 KATTR_HREF, href_diff, KATTR__MAX);
1007 if (kerr != KCGI_OK)
1008 goto done;
1009 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1010 if (kerr != KCGI_OK)
1011 goto done;
1012 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1013 if (kerr != KCGI_OK)
1014 goto done;
1016 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1017 if (kerr != KCGI_OK)
1018 goto done;
1020 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
1021 gw_trans->repo_name, n_header->commit_id) == -1) {
1022 error = got_error_from_errno("asprintf");
1023 goto done;
1025 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1026 KATTR_HREF, href_blob, KATTR__MAX);
1027 if (kerr != KCGI_OK)
1028 goto done;
1029 khtml_puts(gw_trans->gw_html_req, "tree");
1030 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1031 if (kerr != KCGI_OK)
1032 goto done;
1033 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1034 if (kerr != KCGI_OK)
1035 goto done;
1037 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1038 "solid_line", KATTR__MAX);
1039 if (kerr != KCGI_OK)
1040 goto done;
1041 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1042 if (kerr != KCGI_OK)
1043 goto done;
1045 free(age);
1046 age = NULL;
1048 done:
1049 gw_free_header(header);
1050 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1051 gw_free_header(n_header);
1052 free(age);
1053 free(href_diff);
1054 free(href_blob);
1055 if (error == NULL && kerr != KCGI_OK)
1056 error = gw_kcgi_error(kerr);
1057 return error;
1060 static const struct got_error *
1061 gw_briefs(struct gw_trans *gw_trans)
1063 const struct got_error *error = NULL;
1064 struct gw_header *header = NULL, *n_header = NULL;
1065 char *age = NULL;
1066 char *href_diff = NULL, *href_blob = NULL;
1067 char *newline, *smallerthan;
1068 enum kcgi_err kerr = KCGI_OK;
1070 if ((header = gw_init_header()) == NULL)
1071 return got_error_from_errno("malloc");
1073 if (pledge("stdio rpath proc exec sendfd unveil",
1074 NULL) == -1) {
1075 error = got_error_from_errno("pledge");
1076 goto done;
1079 if (gw_trans->action != GW_SUMMARY) {
1080 error = gw_apply_unveil(gw_trans->gw_dir->path);
1081 if (error)
1082 goto done;
1085 if (gw_trans->action == GW_SUMMARY)
1086 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
1087 else
1088 error = gw_get_header(gw_trans, header,
1089 gw_trans->gw_conf->got_max_commits_display);
1090 if (error)
1091 goto done;
1093 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
1094 error = gw_get_time_str(&age, n_header->committer_time,
1095 TM_DIFF);
1096 if (error)
1097 goto done;
1099 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1100 KATTR_ID, "briefs_wrapper", KATTR__MAX);
1101 if (kerr != KCGI_OK)
1102 goto done;
1104 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1105 KATTR_ID, "briefs_age", KATTR__MAX);
1106 if (kerr != KCGI_OK)
1107 goto done;
1108 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
1109 if (kerr != KCGI_OK)
1110 goto done;
1111 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1112 if (kerr != KCGI_OK)
1113 goto done;
1115 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1116 KATTR_ID, "briefs_author", KATTR__MAX);
1117 if (kerr != KCGI_OK)
1118 goto done;
1119 smallerthan = strchr(n_header->author, '<');
1120 if (smallerthan)
1121 *smallerthan = '\0';
1122 kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
1123 if (kerr != KCGI_OK)
1124 goto done;
1125 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1126 if (kerr != KCGI_OK)
1127 goto done;
1129 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
1130 gw_trans->repo_name, n_header->commit_id) == -1) {
1131 error = got_error_from_errno("asprintf");
1132 goto done;
1134 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1135 KATTR_ID, "briefs_log", KATTR__MAX);
1136 if (kerr != KCGI_OK)
1137 goto done;
1138 newline = strchr(n_header->commit_msg, '\n');
1139 if (newline)
1140 *newline = '\0';
1141 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1142 KATTR_HREF, href_diff, KATTR__MAX);
1143 if (kerr != KCGI_OK)
1144 goto done;
1145 kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
1146 if (kerr != KCGI_OK)
1147 goto done;
1148 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1149 if (kerr != KCGI_OK)
1150 goto done;
1152 if (n_header->refs_str) {
1153 kerr = khtml_puts(gw_trans->gw_html_req, " ");
1154 if (kerr != KCGI_OK)
1155 goto done;
1156 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
1157 KATTR_ID, "refs_str", KATTR__MAX);
1158 if (kerr != KCGI_OK)
1159 goto done;
1160 kerr = khtml_puts(gw_trans->gw_html_req, "(");
1161 if (kerr != KCGI_OK)
1162 goto done;
1163 kerr = khtml_puts(gw_trans->gw_html_req,
1164 n_header->refs_str);
1165 if (kerr != KCGI_OK)
1166 goto done;
1167 kerr = khtml_puts(gw_trans->gw_html_req, ")");
1168 if (kerr != KCGI_OK)
1169 goto done;
1170 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1171 if (kerr != KCGI_OK)
1172 goto done;
1175 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1176 if (kerr != KCGI_OK)
1177 goto done;
1179 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1180 KATTR_ID, "navs_wrapper", KATTR__MAX);
1181 if (kerr != KCGI_OK)
1182 goto done;
1183 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1184 KATTR_ID, "navs", KATTR__MAX);
1185 if (kerr != KCGI_OK)
1186 goto done;
1187 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1188 KATTR_HREF, href_diff, KATTR__MAX);
1189 if (kerr != KCGI_OK)
1190 goto done;
1191 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
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_puts(gw_trans->gw_html_req, " | ");
1199 if (kerr != KCGI_OK)
1200 goto done;
1202 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
1203 gw_trans->repo_name, n_header->commit_id) == -1) {
1204 error = got_error_from_errno("asprintf");
1205 goto done;
1207 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1208 KATTR_HREF, href_blob, KATTR__MAX);
1209 if (kerr != KCGI_OK)
1210 goto done;
1211 khtml_puts(gw_trans->gw_html_req, "tree");
1212 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1213 if (kerr != KCGI_OK)
1214 goto done;
1215 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1216 if (kerr != KCGI_OK)
1217 goto done;
1219 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1220 KATTR_ID, "dotted_line", KATTR__MAX);
1221 if (kerr != KCGI_OK)
1222 goto done;
1223 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1224 if (kerr != KCGI_OK)
1225 goto done;
1227 free(age);
1228 age = NULL;
1229 free(href_diff);
1230 href_diff = NULL;
1231 free(href_blob);
1232 href_blob = NULL;
1234 done:
1235 gw_free_header(header);
1236 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1237 gw_free_header(n_header);
1238 free(age);
1239 free(href_diff);
1240 free(href_blob);
1241 if (error == NULL && kerr != KCGI_OK)
1242 error = gw_kcgi_error(kerr);
1243 return error;
1246 static const struct got_error *
1247 gw_summary(struct gw_trans *gw_trans)
1249 const struct got_error *error = NULL;
1250 char *age = NULL;
1251 enum kcgi_err kerr = KCGI_OK;
1253 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1254 return got_error_from_errno("pledge");
1256 error = gw_apply_unveil(gw_trans->gw_dir->path);
1257 if (error)
1258 goto done;
1260 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1261 "summary_wrapper", KATTR__MAX);
1262 if (kerr != KCGI_OK)
1263 return gw_kcgi_error(kerr);
1265 if (gw_trans->gw_conf->got_show_repo_description &&
1266 gw_trans->gw_dir->description != NULL &&
1267 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1268 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1269 KATTR_ID, "description_title", KATTR__MAX);
1270 if (kerr != KCGI_OK)
1271 goto done;
1272 kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1273 if (kerr != KCGI_OK)
1274 goto done;
1275 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1276 if (kerr != KCGI_OK)
1277 goto done;
1278 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1279 KATTR_ID, "description", KATTR__MAX);
1280 if (kerr != KCGI_OK)
1281 goto done;
1282 kerr = khtml_puts(gw_trans->gw_html_req,
1283 gw_trans->gw_dir->description);
1284 if (kerr != KCGI_OK)
1285 goto done;
1286 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1287 if (kerr != KCGI_OK)
1288 goto done;
1291 if (gw_trans->gw_conf->got_show_repo_owner &&
1292 gw_trans->gw_dir->owner != NULL &&
1293 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
1294 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1295 KATTR_ID, "repo_owner_title", KATTR__MAX);
1296 if (kerr != KCGI_OK)
1297 goto done;
1298 kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1299 if (kerr != KCGI_OK)
1300 goto done;
1301 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1302 if (kerr != KCGI_OK)
1303 goto done;
1304 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1305 KATTR_ID, "repo_owner", KATTR__MAX);
1306 if (kerr != KCGI_OK)
1307 goto done;
1308 kerr = khtml_puts(gw_trans->gw_html_req,
1309 gw_trans->gw_dir->owner);
1310 if (kerr != KCGI_OK)
1311 goto done;
1312 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1313 if (kerr != KCGI_OK)
1314 goto done;
1317 if (gw_trans->gw_conf->got_show_repo_age) {
1318 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1319 NULL, TM_LONG);
1320 if (error)
1321 goto done;
1322 if (age != NULL) {
1323 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1324 KATTR_ID, "last_change_title", KATTR__MAX);
1325 if (kerr != KCGI_OK)
1326 goto done;
1327 kerr = khtml_puts(gw_trans->gw_html_req,
1328 "Last Change: ");
1329 if (kerr != KCGI_OK)
1330 goto done;
1331 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1332 if (kerr != KCGI_OK)
1333 goto done;
1334 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1335 KATTR_ID, "last_change", KATTR__MAX);
1336 if (kerr != KCGI_OK)
1337 goto done;
1338 kerr = khtml_puts(gw_trans->gw_html_req, age);
1339 if (kerr != KCGI_OK)
1340 goto done;
1341 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1342 if (kerr != KCGI_OK)
1343 goto done;
1347 if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1348 gw_trans->gw_dir->url != NULL &&
1349 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1350 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1351 KATTR_ID, "cloneurl_title", KATTR__MAX);
1352 if (kerr != KCGI_OK)
1353 goto done;
1354 kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1355 if (kerr != KCGI_OK)
1356 goto done;
1357 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1358 if (kerr != KCGI_OK)
1359 goto done;
1360 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1361 KATTR_ID, "cloneurl", KATTR__MAX);
1362 if (kerr != KCGI_OK)
1363 goto done;
1364 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1365 if (kerr != KCGI_OK)
1366 goto done;
1367 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1368 if (kerr != KCGI_OK)
1369 goto done;
1372 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1373 if (kerr != KCGI_OK)
1374 goto done;
1376 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1377 "briefs_title_wrapper", KATTR__MAX);
1378 if (kerr != KCGI_OK)
1379 goto done;
1380 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1381 "briefs_title", KATTR__MAX);
1382 if (kerr != KCGI_OK)
1383 goto done;
1384 kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1385 if (kerr != KCGI_OK)
1386 goto done;
1387 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1388 if (kerr != KCGI_OK)
1389 goto done;
1390 error = gw_briefs(gw_trans);
1391 if (error)
1392 goto done;
1394 error = gw_output_repo_tags(gw_trans, NULL, D_MAXSLCOMMDISP,
1395 TAGBRIEF);
1396 if (error)
1397 goto done;
1399 error = gw_output_repo_heads(gw_trans);
1400 done:
1401 free(age);
1402 if (error == NULL && kerr != KCGI_OK)
1403 error = gw_kcgi_error(kerr);
1404 return error;
1407 static const struct got_error *
1408 gw_tree(struct gw_trans *gw_trans)
1410 const struct got_error *error = NULL;
1411 struct gw_header *header = NULL;
1412 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1413 char *age = NULL;
1414 enum kcgi_err kerr;
1416 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1417 return got_error_from_errno("pledge");
1419 if ((header = gw_init_header()) == NULL)
1420 return got_error_from_errno("malloc");
1422 error = gw_apply_unveil(gw_trans->gw_dir->path);
1423 if (error)
1424 goto done;
1426 error = gw_get_header(gw_trans, header, 1);
1427 if (error)
1428 goto done;
1430 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1431 "tree_header_wrapper", KATTR__MAX);
1432 if (kerr != KCGI_OK)
1433 goto done;
1434 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1435 "tree_header", KATTR__MAX);
1436 if (kerr != KCGI_OK)
1437 goto done;
1438 error = gw_gen_tree_header(gw_trans, header->tree_id);
1439 if (error)
1440 goto done;
1441 error = gw_get_time_str(&age, header->committer_time,
1442 TM_LONG);
1443 if (error)
1444 goto done;
1445 error = gw_gen_age_header(gw_trans, age ?age : "");
1446 if (error)
1447 goto done;
1448 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1449 if (error)
1450 goto done;
1451 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1452 if (kerr != KCGI_OK)
1453 goto done;
1454 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1455 "dotted_line", KATTR__MAX);
1456 if (kerr != KCGI_OK)
1457 goto done;
1458 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1459 if (kerr != KCGI_OK)
1460 goto done;
1462 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1463 "tree", KATTR__MAX);
1464 if (kerr != KCGI_OK)
1465 goto done;
1466 error = gw_output_repo_tree(gw_trans);
1467 if (error)
1468 goto done;
1470 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1471 done:
1472 gw_free_header(header);
1473 free(tree_html_disp);
1474 free(tree_html);
1475 free(tree);
1476 free(age);
1477 if (error == NULL && kerr != KCGI_OK)
1478 error = gw_kcgi_error(kerr);
1479 return error;
1482 static const struct got_error *
1483 gw_tag(struct gw_trans *gw_trans)
1485 const struct got_error *error = NULL;
1486 struct gw_header *header = NULL;
1487 enum kcgi_err kerr;
1489 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1490 return got_error_from_errno("pledge");
1492 if ((header = gw_init_header()) == NULL)
1493 return got_error_from_errno("malloc");
1495 error = gw_apply_unveil(gw_trans->gw_dir->path);
1496 if (error)
1497 goto done;
1499 if (gw_trans->commit_id == NULL) {
1500 error = got_error_msg(GOT_ERR_QUERYSTRING,
1501 "commit required in querystring");
1502 goto done;
1505 error = gw_get_header(gw_trans, header, 1);
1506 if (error)
1507 goto done;
1509 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1510 "tag_header_wrapper", KATTR__MAX);
1511 if (kerr != KCGI_OK)
1512 goto done;
1513 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1514 "tag_header", KATTR__MAX);
1515 if (kerr != KCGI_OK)
1516 goto done;
1517 error = gw_gen_commit_header(gw_trans, header->commit_id,
1518 header->refs_str);
1519 if (error)
1520 goto done;
1521 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1522 if (error)
1523 goto done;
1524 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1525 if (kerr != KCGI_OK)
1526 goto done;
1527 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1528 "dotted_line", KATTR__MAX);
1529 if (kerr != KCGI_OK)
1530 goto done;
1531 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1532 if (kerr != KCGI_OK)
1533 goto done;
1535 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1536 "tree", KATTR__MAX);
1537 if (kerr != KCGI_OK)
1538 goto done;
1540 error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1541 if (error)
1542 goto done;
1544 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1545 done:
1546 gw_free_header(header);
1547 if (error == NULL && kerr != KCGI_OK)
1548 error = gw_kcgi_error(kerr);
1549 return error;
1552 static const struct got_error *
1553 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1555 const struct got_error *error = NULL;
1556 DIR *dt;
1557 char *dir_test;
1558 int opened = 0;
1560 if (asprintf(&dir_test, "%s/%s/%s",
1561 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1562 GOTWEB_GIT_DIR) == -1)
1563 return got_error_from_errno("asprintf");
1565 dt = opendir(dir_test);
1566 if (dt == NULL) {
1567 free(dir_test);
1568 } else {
1569 gw_dir->path = strdup(dir_test);
1570 if (gw_dir->path == NULL) {
1571 opened = 1;
1572 error = got_error_from_errno("strdup");
1573 goto errored;
1575 opened = 1;
1576 goto done;
1579 if (asprintf(&dir_test, "%s/%s/%s",
1580 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1581 GOTWEB_GOT_DIR) == -1) {
1582 dir_test = NULL;
1583 error = got_error_from_errno("asprintf");
1584 goto errored;
1587 dt = opendir(dir_test);
1588 if (dt == NULL)
1589 free(dir_test);
1590 else {
1591 opened = 1;
1592 error = got_error(GOT_ERR_NOT_GIT_REPO);
1593 goto errored;
1596 if (asprintf(&dir_test, "%s/%s",
1597 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1) {
1598 error = got_error_from_errno("asprintf");
1599 dir_test = NULL;
1600 goto errored;
1603 gw_dir->path = strdup(dir_test);
1604 if (gw_dir->path == NULL) {
1605 opened = 1;
1606 error = got_error_from_errno("strdup");
1607 goto errored;
1610 dt = opendir(dir_test);
1611 if (dt == NULL) {
1612 error = got_error_from_errno2("bad path", dir_test);
1613 goto errored;
1614 } else
1615 opened = 1;
1616 done:
1617 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1618 gw_dir->path);
1619 if (error)
1620 goto errored;
1621 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1622 if (error)
1623 goto errored;
1624 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1625 NULL, TM_DIFF);
1626 if (error)
1627 goto errored;
1628 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1629 errored:
1630 free(dir_test);
1631 if (opened)
1632 if (dt && closedir(dt) == -1 && error == NULL)
1633 error = got_error_from_errno("closedir");
1634 return error;
1637 static const struct got_error *
1638 gw_load_got_paths(struct gw_trans *gw_trans)
1640 const struct got_error *error = NULL;
1641 DIR *d;
1642 struct dirent **sd_dent;
1643 struct gw_dir *gw_dir;
1644 struct stat st;
1645 unsigned int d_cnt, d_i;
1647 d = opendir(gw_trans->gw_conf->got_repos_path);
1648 if (d == NULL) {
1649 error = got_error_from_errno2("opendir",
1650 gw_trans->gw_conf->got_repos_path);
1651 return error;
1654 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1655 alphasort);
1656 if (d_cnt == -1) {
1657 error = got_error_from_errno2("scandir",
1658 gw_trans->gw_conf->got_repos_path);
1659 goto done;
1662 for (d_i = 0; d_i < d_cnt; d_i++) {
1663 if (gw_trans->gw_conf->got_max_repos > 0 &&
1664 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1665 break; /* account for parent and self */
1667 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1668 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1669 continue;
1671 error = gw_init_gw_dir(&gw_dir, sd_dent[d_i]->d_name);
1672 if (error)
1673 goto done;
1675 error = gw_load_got_path(gw_trans, gw_dir);
1676 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1677 error = NULL;
1678 continue;
1680 else if (error)
1681 goto done;
1683 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1684 !got_path_dir_is_empty(gw_dir->path)) {
1685 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1686 entry);
1687 gw_trans->repos_total++;
1690 done:
1691 if (d && closedir(d) == -1 && error == NULL)
1692 error = got_error_from_errno("closedir");
1693 return error;
1696 static const struct got_error *
1697 gw_parse_querystring(struct gw_trans *gw_trans)
1699 const struct got_error *error = NULL;
1700 struct kpair *p;
1701 struct gw_query_action *action = NULL;
1702 unsigned int i;
1704 if (gw_trans->gw_req->fieldnmap[0]) {
1705 error = got_error_from_errno("bad parse");
1706 return error;
1707 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1708 /* define gw_trans->repo_path */
1709 gw_trans->repo_name = p->parsed.s;
1711 if (asprintf(&gw_trans->repo_path, "%s/%s",
1712 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
1713 return got_error_from_errno("asprintf");
1715 /* get action and set function */
1716 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION])) {
1717 for (i = 0; i < nitems(gw_query_funcs); i++) {
1718 action = &gw_query_funcs[i];
1719 if (action->func_name == NULL)
1720 continue;
1721 if (strcmp(action->func_name,
1722 p->parsed.s) == 0) {
1723 gw_trans->action = i;
1724 break;
1728 if (gw_trans->action == -1) {
1729 gw_trans->action = GW_ERR;
1730 gw_trans->error = got_error_from_errno("bad action");
1731 return error;
1734 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID])) {
1735 if (asprintf(&gw_trans->commit_id, "%s",
1736 p->parsed.s) == -1)
1737 return got_error_from_errno("asprintf");
1740 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1741 gw_trans->repo_file = p->parsed.s;
1743 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER])) {
1744 if (asprintf(&gw_trans->repo_folder, "%s",
1745 p->parsed.s) == -1)
1746 return got_error_from_errno("asprintf");
1749 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
1750 gw_trans->headref = p->parsed.s;
1752 error = gw_init_gw_dir(&gw_trans->gw_dir, gw_trans->repo_name);
1753 if (error)
1754 return error;
1756 gw_trans->error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1757 } else
1758 gw_trans->action = GW_INDEX;
1760 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1761 gw_trans->page = p->parsed.i;
1763 return error;
1766 static const struct got_error *
1767 gw_init_gw_dir(struct gw_dir **gw_dir, const char *dir)
1769 const struct got_error *error;
1771 *gw_dir = malloc(sizeof(**gw_dir));
1772 if (*gw_dir == NULL)
1773 return got_error_from_errno("malloc");
1775 if (asprintf(&(*gw_dir)->name, "%s", dir) == -1) {
1776 error = got_error_from_errno("asprintf");
1777 free(*gw_dir);
1778 *gw_dir = NULL;
1779 return NULL;
1782 return NULL;
1785 static const struct got_error *
1786 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1788 enum kcgi_err kerr;
1790 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1791 if (kerr != KCGI_OK)
1792 return gw_kcgi_error(kerr);
1793 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1794 khttps[code]);
1795 if (kerr != KCGI_OK)
1796 return gw_kcgi_error(kerr);
1797 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1798 kmimetypes[mime]);
1799 if (kerr != KCGI_OK)
1800 return gw_kcgi_error(kerr);
1801 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
1802 "nosniff");
1803 if (kerr != KCGI_OK)
1804 return gw_kcgi_error(kerr);
1805 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1806 if (kerr != KCGI_OK)
1807 return gw_kcgi_error(kerr);
1808 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
1809 "1; mode=block");
1810 if (kerr != KCGI_OK)
1811 return gw_kcgi_error(kerr);
1813 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
1814 kerr = khttp_head(gw_trans->gw_req,
1815 kresps[KRESP_CONTENT_DISPOSITION],
1816 "attachment; filename=%s", gw_trans->repo_file);
1817 if (kerr != KCGI_OK)
1818 return gw_kcgi_error(kerr);
1821 kerr = khttp_body(gw_trans->gw_req);
1822 return gw_kcgi_error(kerr);
1825 static const struct got_error *
1826 gw_display_index(struct gw_trans *gw_trans)
1828 const struct got_error *error;
1829 enum kcgi_err kerr;
1831 /* catch early querystring errors */
1832 if (gw_trans->error)
1833 gw_trans->action = GW_ERR;
1835 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1836 if (error)
1837 return error;
1839 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1840 if (kerr != KCGI_OK)
1841 return gw_kcgi_error(kerr);
1843 if (gw_trans->action != GW_BLOB) {
1844 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1845 gw_query_funcs[gw_trans->action].template);
1846 if (kerr != KCGI_OK) {
1847 khtml_close(gw_trans->gw_html_req);
1848 return gw_kcgi_error(kerr);
1852 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
1855 static const struct got_error *
1856 gw_error(struct gw_trans *gw_trans)
1858 enum kcgi_err kerr;
1860 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->error->msg);
1862 return gw_kcgi_error(kerr);
1865 static int
1866 gw_template(size_t key, void *arg)
1868 const struct got_error *error = NULL;
1869 enum kcgi_err kerr;
1870 struct gw_trans *gw_trans = arg;
1871 char *img_src = NULL;
1873 switch (key) {
1874 case (TEMPL_HEAD):
1875 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1876 KATTR_NAME, "viewport",
1877 KATTR_CONTENT, "initial-scale=.75, user-scalable=yes",
1878 KATTR__MAX);
1879 if (kerr != KCGI_OK)
1880 return 0;
1881 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1882 if (kerr != KCGI_OK)
1883 return 0;
1884 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1885 KATTR_CHARSET, "utf-8",
1886 KATTR__MAX);
1887 if (kerr != KCGI_OK)
1888 return 0;
1889 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1890 if (kerr != KCGI_OK)
1891 return 0;
1892 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1893 KATTR_NAME, "msapplication-TileColor",
1894 KATTR_CONTENT, "#da532c", KATTR__MAX);
1895 if (kerr != KCGI_OK)
1896 return 0;
1897 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1898 if (kerr != KCGI_OK)
1899 return 0;
1900 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1901 KATTR_NAME, "theme-color",
1902 KATTR_CONTENT, "#ffffff", KATTR__MAX);
1903 if (kerr != KCGI_OK)
1904 return 0;
1905 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1906 if (kerr != KCGI_OK)
1907 return 0;
1908 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1909 KATTR_REL, "apple-touch-icon", KATTR_SIZES, "180x180",
1910 KATTR_HREF, "/apple-touch-icon.png", KATTR__MAX);
1911 if (kerr != KCGI_OK)
1912 return 0;
1913 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1914 if (kerr != KCGI_OK)
1915 return 0;
1916 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1917 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
1918 "32x32", KATTR_HREF, "/favicon-32x32.png", KATTR__MAX);
1919 if (kerr != KCGI_OK)
1920 return 0;
1921 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1922 if (kerr != KCGI_OK)
1923 return 0;
1924 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1925 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
1926 "16x16", KATTR_HREF, "/favicon-16x16.png", KATTR__MAX);
1927 if (kerr != KCGI_OK)
1928 return 0;
1929 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1930 if (kerr != KCGI_OK)
1931 return 0;
1932 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1933 KATTR_REL, "manifest", KATTR_HREF, "/site.webmanifest",
1934 KATTR__MAX);
1935 if (kerr != KCGI_OK)
1936 return 0;
1937 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1938 if (kerr != KCGI_OK)
1939 return 0;
1940 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1941 KATTR_REL, "mask-icon", KATTR_HREF,
1942 "/safari-pinned-tab.svg", KATTR__MAX);
1943 if (kerr != KCGI_OK)
1944 return 0;
1945 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1946 if (kerr != KCGI_OK)
1947 return 0;
1948 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1949 KATTR_REL, "stylesheet", KATTR_TYPE, "text/css",
1950 KATTR_HREF, "/gotweb.css", KATTR__MAX);
1951 if (kerr != KCGI_OK)
1952 return 0;
1953 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1954 if (kerr != KCGI_OK)
1955 return 0;
1956 break;
1957 case(TEMPL_HEADER):
1958 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1959 KATTR_ID, "got_link", KATTR__MAX);
1960 if (kerr != KCGI_OK)
1961 return 0;
1962 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1963 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
1964 KATTR_TARGET, "_sotd", KATTR__MAX);
1965 if (kerr != KCGI_OK)
1966 return 0;
1967 if (asprintf(&img_src, "/%s",
1968 gw_trans->gw_conf->got_logo) == -1)
1969 return 0;
1970 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
1971 KATTR_SRC, img_src, KATTR__MAX);
1972 if (kerr != KCGI_OK) {
1973 free(img_src);
1974 return 0;
1976 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1977 if (kerr != KCGI_OK) {
1978 free(img_src);
1979 return 0;
1981 break;
1982 case (TEMPL_SITEPATH):
1983 error = gw_output_site_link(gw_trans);
1984 if (error)
1985 return 0;
1986 break;
1987 case(TEMPL_TITLE):
1988 if (gw_trans->gw_conf->got_site_name != NULL) {
1989 kerr = khtml_puts(gw_trans->gw_html_req,
1990 gw_trans->gw_conf->got_site_name);
1991 if (kerr != KCGI_OK)
1992 return 0;
1994 break;
1995 case (TEMPL_SEARCH):
1996 break;
1997 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1998 "search", KATTR__MAX);
1999 if (kerr != KCGI_OK)
2000 return 0;
2001 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
2002 KATTR_METHOD, "POST", KATTR__MAX);
2003 if (kerr != KCGI_OK)
2004 return 0;
2005 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
2006 "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
2007 KATTR_MAXLENGTH, "50", KATTR__MAX);
2008 if (kerr != KCGI_OK)
2009 return 0;
2010 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
2011 KATTR__MAX);
2012 if (kerr != KCGI_OK)
2013 return 0;
2014 kerr = khtml_puts(gw_trans->gw_html_req, "Search");
2015 if (kerr != KCGI_OK)
2016 return 0;
2017 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
2018 if (kerr != KCGI_OK)
2019 return 0;
2020 break;
2021 case(TEMPL_SITEOWNER):
2022 if (gw_trans->gw_conf->got_site_owner != NULL &&
2023 gw_trans->gw_conf->got_show_site_owner) {
2024 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2025 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
2026 if (kerr != KCGI_OK)
2027 return 0;
2028 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2029 KATTR_ID, "site_owner", KATTR__MAX);
2030 if (kerr != KCGI_OK)
2031 return 0;
2032 kerr = khtml_puts(gw_trans->gw_html_req,
2033 gw_trans->gw_conf->got_site_owner);
2034 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2035 if (kerr != KCGI_OK)
2036 return 0;
2038 break;
2039 case(TEMPL_CONTENT):
2040 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
2041 if (error) {
2042 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2043 KATTR_ID, "tmpl_err", KATTR__MAX);
2044 if (kerr != KCGI_OK)
2045 return 0;
2046 kerr = khttp_puts(gw_trans->gw_req, "Error: ");
2047 if (kerr != KCGI_OK)
2048 return 0;
2049 kerr = khttp_puts(gw_trans->gw_req, error->msg);
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;
2056 break;
2057 default:
2058 return 0;
2060 return 1;
2063 static const struct got_error *
2064 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
2066 const struct got_error *error = NULL;
2067 enum kcgi_err kerr = KCGI_OK;
2069 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2070 KATTR_ID, "header_commit_title", KATTR__MAX);
2071 if (kerr != KCGI_OK)
2072 goto done;
2073 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
2074 if (kerr != KCGI_OK)
2075 goto done;
2076 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2077 if (kerr != KCGI_OK)
2078 goto done;
2079 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2080 KATTR_ID, "header_commit", KATTR__MAX);
2081 if (kerr != KCGI_OK)
2082 goto done;
2083 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2084 if (kerr != KCGI_OK)
2085 goto done;
2086 kerr = khtml_puts(gw_trans->gw_html_req, " ");
2087 if (kerr != KCGI_OK)
2088 goto done;
2089 if (str2 != NULL) {
2090 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
2091 KATTR_ID, "refs_str", KATTR__MAX);
2092 if (kerr != KCGI_OK)
2093 goto done;
2094 kerr = khtml_puts(gw_trans->gw_html_req, "(");
2095 if (kerr != KCGI_OK)
2096 goto done;
2097 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2098 if (kerr != KCGI_OK)
2099 goto done;
2100 kerr = khtml_puts(gw_trans->gw_html_req, ")");
2101 if (kerr != KCGI_OK)
2102 goto done;
2103 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2104 if (kerr != KCGI_OK)
2105 goto done;
2107 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2108 done:
2109 if (error == NULL && kerr != KCGI_OK)
2110 error = gw_kcgi_error(kerr);
2111 return error;
2114 static const struct got_error *
2115 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2117 const struct got_error *error = NULL;
2118 enum kcgi_err kerr = KCGI_OK;
2120 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2121 KATTR_ID, "header_diff_title", KATTR__MAX);
2122 if (kerr != KCGI_OK)
2123 goto done;
2124 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2125 if (kerr != KCGI_OK)
2126 goto done;
2127 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2128 if (kerr != KCGI_OK)
2129 goto done;
2130 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2131 KATTR_ID, "header_diff", KATTR__MAX);
2132 if (kerr != KCGI_OK)
2133 goto done;
2134 if (str1 != NULL) {
2135 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2136 if (kerr != KCGI_OK)
2137 goto done;
2139 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2140 if (kerr != KCGI_OK)
2141 goto done;
2142 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2143 if (kerr != KCGI_OK)
2144 goto done;
2145 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2146 done:
2147 if (error == NULL && kerr != KCGI_OK)
2148 error = gw_kcgi_error(kerr);
2149 return error;
2152 static const struct got_error *
2153 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2155 const struct got_error *error = NULL;
2156 enum kcgi_err kerr = KCGI_OK;
2158 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2159 KATTR_ID, "header_age_title", KATTR__MAX);
2160 if (kerr != KCGI_OK)
2161 goto done;
2162 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2163 if (kerr != KCGI_OK)
2164 goto done;
2165 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2166 if (kerr != KCGI_OK)
2167 goto done;
2168 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2169 KATTR_ID, "header_age", KATTR__MAX);
2170 if (kerr != KCGI_OK)
2171 goto done;
2172 kerr = khtml_puts(gw_trans->gw_html_req, str);
2173 if (kerr != KCGI_OK)
2174 goto done;
2175 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2176 done:
2177 if (error == NULL && kerr != KCGI_OK)
2178 error = gw_kcgi_error(kerr);
2179 return error;
2182 static const struct got_error *
2183 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2185 const struct got_error *error = NULL;
2186 enum kcgi_err kerr;
2188 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2189 KATTR_ID, "header_author_title", KATTR__MAX);
2190 if (kerr != KCGI_OK)
2191 goto done;
2192 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2193 if (kerr != KCGI_OK)
2194 goto done;
2195 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2196 if (kerr != KCGI_OK)
2197 goto done;
2198 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2199 KATTR_ID, "header_author", KATTR__MAX);
2200 if (kerr != KCGI_OK)
2201 goto done;
2202 kerr = khtml_puts(gw_trans->gw_html_req, str);
2203 if (kerr != KCGI_OK)
2204 goto done;
2205 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2206 done:
2207 if (error == NULL && kerr != KCGI_OK)
2208 error = gw_kcgi_error(kerr);
2209 return error;
2212 static const struct got_error *
2213 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2215 const struct got_error *error = NULL;
2216 enum kcgi_err kerr;
2218 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2219 KATTR_ID, "header_committer_title", KATTR__MAX);
2220 if (kerr != KCGI_OK)
2221 goto done;
2222 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2223 if (kerr != KCGI_OK)
2224 goto done;
2225 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2226 if (kerr != KCGI_OK)
2227 goto done;
2228 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2229 KATTR_ID, "header_committer", KATTR__MAX);
2230 if (kerr != KCGI_OK)
2231 goto done;
2232 kerr = khtml_puts(gw_trans->gw_html_req, str);
2233 if (kerr != KCGI_OK)
2234 goto done;
2235 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2236 done:
2237 if (error == NULL && kerr != KCGI_OK)
2238 error = gw_kcgi_error(kerr);
2239 return error;
2242 static const struct got_error *
2243 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2245 const struct got_error *error = NULL;
2246 enum kcgi_err kerr;
2248 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2249 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2250 if (kerr != KCGI_OK)
2251 goto done;
2252 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2253 if (kerr != KCGI_OK)
2254 goto done;
2255 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2256 if (kerr != KCGI_OK)
2257 goto done;
2258 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2259 KATTR_ID, "header_commit_msg", KATTR__MAX);
2260 if (kerr != KCGI_OK)
2261 goto done;
2262 kerr = khttp_puts(gw_trans->gw_req, str);
2263 if (kerr != KCGI_OK)
2264 goto done;
2265 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2266 done:
2267 if (error == NULL && kerr != KCGI_OK)
2268 error = gw_kcgi_error(kerr);
2269 return error;
2272 static const struct got_error *
2273 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2275 const struct got_error *error = NULL;
2276 enum kcgi_err kerr;
2278 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2279 KATTR_ID, "header_tree_title", KATTR__MAX);
2280 if (kerr != KCGI_OK)
2281 goto done;
2282 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2283 if (kerr != KCGI_OK)
2284 goto done;
2285 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2286 if (kerr != KCGI_OK)
2287 goto done;
2288 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2289 KATTR_ID, "header_tree", KATTR__MAX);
2290 if (kerr != KCGI_OK)
2291 goto done;
2292 kerr = khtml_puts(gw_trans->gw_html_req, str);
2293 if (kerr != KCGI_OK)
2294 goto done;
2295 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2296 done:
2297 if (error == NULL && kerr != KCGI_OK)
2298 error = gw_kcgi_error(kerr);
2299 return error;
2302 static const struct got_error *
2303 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2304 char *dir)
2306 const struct got_error *error = NULL;
2307 FILE *f = NULL;
2308 char *d_file = NULL;
2309 unsigned int len;
2310 size_t n;
2312 *description = NULL;
2313 if (gw_trans->gw_conf->got_show_repo_description == 0)
2314 return NULL;
2316 if (asprintf(&d_file, "%s/description", dir) == -1)
2317 return got_error_from_errno("asprintf");
2319 f = fopen(d_file, "r");
2320 if (f == NULL) {
2321 if (errno == ENOENT || errno == EACCES)
2322 return NULL;
2323 error = got_error_from_errno2("fopen", d_file);
2324 goto done;
2327 if (fseek(f, 0, SEEK_END) == -1) {
2328 error = got_ferror(f, GOT_ERR_IO);
2329 goto done;
2331 len = ftell(f);
2332 if (len == -1) {
2333 error = got_ferror(f, GOT_ERR_IO);
2334 goto done;
2336 if (fseek(f, 0, SEEK_SET) == -1) {
2337 error = got_ferror(f, GOT_ERR_IO);
2338 goto done;
2340 *description = calloc(len + 1, sizeof(**description));
2341 if (*description == NULL) {
2342 error = got_error_from_errno("calloc");
2343 goto done;
2346 n = fread(*description, 1, len, f);
2347 if (n == 0 && ferror(f))
2348 error = got_ferror(f, GOT_ERR_IO);
2349 done:
2350 if (f != NULL && fclose(f) == -1 && error == NULL)
2351 error = got_error_from_errno("fclose");
2352 free(d_file);
2353 return error;
2356 static const struct got_error *
2357 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2359 struct tm tm;
2360 time_t diff_time;
2361 char *years = "years ago", *months = "months ago";
2362 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2363 char *minutes = "minutes ago", *seconds = "seconds ago";
2364 char *now = "right now";
2365 char *s;
2366 char datebuf[29];
2368 *repo_age = NULL;
2370 switch (ref_tm) {
2371 case TM_DIFF:
2372 diff_time = time(NULL) - committer_time;
2373 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2374 if (asprintf(repo_age, "%lld %s",
2375 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2376 return got_error_from_errno("asprintf");
2377 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2378 if (asprintf(repo_age, "%lld %s",
2379 (diff_time / 60 / 60 / 24 / (365 / 12)),
2380 months) == -1)
2381 return got_error_from_errno("asprintf");
2382 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2383 if (asprintf(repo_age, "%lld %s",
2384 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2385 return got_error_from_errno("asprintf");
2386 } else if (diff_time > 60 * 60 * 24 * 2) {
2387 if (asprintf(repo_age, "%lld %s",
2388 (diff_time / 60 / 60 / 24), days) == -1)
2389 return got_error_from_errno("asprintf");
2390 } else if (diff_time > 60 * 60 * 2) {
2391 if (asprintf(repo_age, "%lld %s",
2392 (diff_time / 60 / 60), hours) == -1)
2393 return got_error_from_errno("asprintf");
2394 } else if (diff_time > 60 * 2) {
2395 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2396 minutes) == -1)
2397 return got_error_from_errno("asprintf");
2398 } else if (diff_time > 2) {
2399 if (asprintf(repo_age, "%lld %s", diff_time,
2400 seconds) == -1)
2401 return got_error_from_errno("asprintf");
2402 } else {
2403 if (asprintf(repo_age, "%s", now) == -1)
2404 return got_error_from_errno("asprintf");
2406 break;
2407 case TM_LONG:
2408 if (gmtime_r(&committer_time, &tm) == NULL)
2409 return got_error_from_errno("gmtime_r");
2411 s = asctime_r(&tm, datebuf);
2412 if (s == NULL)
2413 return got_error_from_errno("asctime_r");
2415 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2416 return got_error_from_errno("asprintf");
2417 break;
2419 return NULL;
2422 static const struct got_error *
2423 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2424 const char *refname, int ref_tm)
2426 const struct got_error *error = NULL;
2427 struct got_repository *repo = NULL;
2428 struct got_commit_object *commit = NULL;
2429 struct got_reflist_head refs;
2430 struct got_reflist_entry *re;
2431 time_t committer_time = 0, cmp_time = 0;
2433 *repo_age = NULL;
2434 SIMPLEQ_INIT(&refs);
2436 if (gw_trans->gw_conf->got_show_repo_age == 0)
2437 return NULL;
2439 if (gw_trans->repo)
2440 repo = gw_trans->repo;
2441 else {
2442 error = got_repo_open(&repo, dir, NULL);
2443 if (error)
2444 return error;
2447 error = got_ref_list(&refs, repo, "refs/heads",
2448 got_ref_cmp_by_name, NULL);
2449 if (error)
2450 goto done;
2453 * Find the youngest branch tip in the repository, or the age of
2454 * the a specific branch tip if a name was provided by the caller.
2456 SIMPLEQ_FOREACH(re, &refs, entry) {
2457 struct got_object_id *id = NULL;
2459 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
2460 continue;
2462 error = got_ref_resolve(&id, repo, re->ref);
2463 if (error)
2464 goto done;
2466 error = got_object_open_as_commit(&commit, repo, id);
2467 free(id);
2468 if (error)
2469 goto done;
2471 committer_time =
2472 got_object_commit_get_committer_time(commit);
2473 got_object_commit_close(commit);
2474 if (cmp_time < committer_time)
2475 cmp_time = committer_time;
2477 if (refname)
2478 break;
2481 if (cmp_time != 0) {
2482 committer_time = cmp_time;
2483 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2485 done:
2486 got_ref_list_free(&refs);
2487 if (gw_trans->repo == NULL)
2488 got_repo_close(repo);
2489 return error;
2492 static const struct got_error *
2493 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2495 const struct got_error *error;
2496 FILE *f = NULL;
2497 struct got_object_id *id1 = NULL, *id2 = NULL;
2498 char *label1 = NULL, *label2 = NULL, *line = NULL;
2499 int obj_type;
2500 size_t linesize = 0;
2501 ssize_t linelen;
2502 enum kcgi_err kerr = KCGI_OK;
2504 f = got_opentemp();
2505 if (f == NULL)
2506 return NULL;
2508 if (header->parent_id != NULL &&
2509 strncmp(header->parent_id, "/dev/null", 9) != 0) {
2510 error = got_repo_match_object_id(&id1, &label1,
2511 header->parent_id, GOT_OBJ_TYPE_ANY, 1, gw_trans->repo);
2512 if (error)
2513 goto done;
2516 error = got_repo_match_object_id(&id2, &label2,
2517 header->commit_id, GOT_OBJ_TYPE_ANY, 1, gw_trans->repo);
2518 if (error)
2519 goto done;
2521 error = got_object_get_type(&obj_type, gw_trans->repo, id2);
2522 if (error)
2523 goto done;
2524 switch (obj_type) {
2525 case GOT_OBJ_TYPE_BLOB:
2526 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
2527 gw_trans->repo, f);
2528 break;
2529 case GOT_OBJ_TYPE_TREE:
2530 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
2531 gw_trans->repo, f);
2532 break;
2533 case GOT_OBJ_TYPE_COMMIT:
2534 error = got_diff_objects_as_commits(id1, id2, 3, 0,
2535 gw_trans->repo, f);
2536 break;
2537 default:
2538 error = got_error(GOT_ERR_OBJ_TYPE);
2540 if (error)
2541 goto done;
2543 if (fseek(f, 0, SEEK_SET) == -1) {
2544 error = got_ferror(f, GOT_ERR_IO);
2545 goto done;
2548 while ((linelen = getline(&line, &linesize, f)) != -1) {
2549 error = gw_colordiff_line(gw_trans, line);
2550 if (error)
2551 goto done;
2552 /* XXX: KHTML_PRETTY breaks this */
2553 kerr = khtml_puts(gw_trans->gw_html_req, line);
2554 if (kerr != KCGI_OK)
2555 goto done;
2556 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2557 if (kerr != KCGI_OK)
2558 goto done;
2560 if (linelen == -1 && ferror(f))
2561 error = got_error_from_errno("getline");
2562 done:
2563 if (f && fclose(f) == -1 && error == NULL)
2564 error = got_error_from_errno("fclose");
2565 free(line);
2566 free(label1);
2567 free(label2);
2568 free(id1);
2569 free(id2);
2571 if (error == NULL && kerr != KCGI_OK)
2572 error = gw_kcgi_error(kerr);
2573 return error;
2576 static const struct got_error *
2577 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2579 const struct got_error *error = NULL;
2580 struct got_repository *repo;
2581 const char *gitconfig_owner;
2583 *owner = NULL;
2585 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2586 return NULL;
2588 error = got_repo_open(&repo, dir, NULL);
2589 if (error)
2590 return error;
2591 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2592 if (gitconfig_owner) {
2593 *owner = strdup(gitconfig_owner);
2594 if (*owner == NULL)
2595 error = got_error_from_errno("strdup");
2597 got_repo_close(repo);
2598 return error;
2601 static const struct got_error *
2602 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2604 const struct got_error *error = NULL;
2605 FILE *f;
2606 char *d_file = NULL;
2607 unsigned int len;
2608 size_t n;
2610 *url = NULL;
2612 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2613 return got_error_from_errno("asprintf");
2615 f = fopen(d_file, "r");
2616 if (f == NULL) {
2617 if (errno != ENOENT && errno != EACCES)
2618 error = got_error_from_errno2("fopen", d_file);
2619 goto done;
2622 if (fseek(f, 0, SEEK_END) == -1) {
2623 error = got_ferror(f, GOT_ERR_IO);
2624 goto done;
2626 len = ftell(f);
2627 if (len == -1) {
2628 error = got_ferror(f, GOT_ERR_IO);
2629 goto done;
2631 if (fseek(f, 0, SEEK_SET) == -1) {
2632 error = got_ferror(f, GOT_ERR_IO);
2633 goto done;
2636 *url = calloc(len + 1, sizeof(**url));
2637 if (*url == NULL) {
2638 error = got_error_from_errno("calloc");
2639 goto done;
2642 n = fread(*url, 1, len, f);
2643 if (n == 0 && ferror(f))
2644 error = got_ferror(f, GOT_ERR_IO);
2645 done:
2646 if (f && fclose(f) == -1 && error == NULL)
2647 error = got_error_from_errno("fclose");
2648 free(d_file);
2649 return NULL;
2652 static const struct got_error *
2653 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
2654 int limit, int tag_type)
2656 const struct got_error *error = NULL;
2657 struct got_reflist_head refs;
2658 struct got_reflist_entry *re;
2659 char *age = NULL;
2660 char *id_str = NULL, *newline, *href_commits = NULL;
2661 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
2662 struct got_tag_object *tag = NULL;
2663 enum kcgi_err kerr = KCGI_OK;
2664 int summary_header_displayed = 0;
2666 SIMPLEQ_INIT(&refs);
2668 error = got_ref_list(&refs, gw_trans->repo, "refs/tags",
2669 got_ref_cmp_tags, gw_trans->repo);
2670 if (error)
2671 goto done;
2673 SIMPLEQ_FOREACH(re, &refs, entry) {
2674 const char *refname;
2675 const char *tagger;
2676 const char *tag_commit;
2677 time_t tagger_time;
2678 struct got_object_id *id;
2679 struct got_commit_object *commit = NULL;
2681 refname = got_ref_get_name(re->ref);
2682 if (strncmp(refname, "refs/tags/", 10) != 0)
2683 continue;
2684 refname += 10;
2686 error = got_ref_resolve(&id, gw_trans->repo, re->ref);
2687 if (error)
2688 goto done;
2690 error = got_object_open_as_tag(&tag, gw_trans->repo, id);
2691 if (error) {
2692 if (error->code != GOT_ERR_OBJ_TYPE) {
2693 free(id);
2694 goto done;
2696 /* "lightweight" tag */
2697 error = got_object_open_as_commit(&commit,
2698 gw_trans->repo, id);
2699 if (error) {
2700 free(id);
2701 goto done;
2703 tagger = got_object_commit_get_committer(commit);
2704 tagger_time =
2705 got_object_commit_get_committer_time(commit);
2706 error = got_object_id_str(&id_str, id);
2707 free(id);
2708 } else {
2709 free(id);
2710 tagger = got_object_tag_get_tagger(tag);
2711 tagger_time = got_object_tag_get_tagger_time(tag);
2712 error = got_object_id_str(&id_str,
2713 got_object_tag_get_object_id(tag));
2715 if (error)
2716 goto done;
2718 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
2719 strlen(id_str)) != 0)
2720 continue;
2722 if (commit) {
2723 error = got_object_commit_get_logmsg(&tag_commit0,
2724 commit);
2725 if (error)
2726 goto done;
2727 got_object_commit_close(commit);
2728 } else {
2729 tag_commit0 = strdup(got_object_tag_get_message(tag));
2730 if (tag_commit0 == NULL) {
2731 error = got_error_from_errno("strdup");
2732 goto done;
2736 tag_commit = tag_commit0;
2737 while (*tag_commit == '\n')
2738 tag_commit++;
2740 switch (tag_type) {
2741 case TAGBRIEF:
2742 newline = strchr(tag_commit, '\n');
2743 if (newline)
2744 *newline = '\0';
2746 if (summary_header_displayed == 0) {
2747 kerr = khtml_attr(gw_trans->gw_html_req,
2748 KELEM_DIV, KATTR_ID,
2749 "summary_tags_title_wrapper", KATTR__MAX);
2750 if (kerr != KCGI_OK)
2751 goto done;
2752 kerr = khtml_attr(gw_trans->gw_html_req,
2753 KELEM_DIV, KATTR_ID,
2754 "summary_tags_title", KATTR__MAX);
2755 if (kerr != KCGI_OK)
2756 goto done;
2757 kerr = khtml_puts(gw_trans->gw_html_req,
2758 "Tags");
2759 if (kerr != KCGI_OK)
2760 goto done;
2761 kerr = khtml_closeelem(gw_trans->gw_html_req,
2762 2);
2763 if (kerr != KCGI_OK)
2764 goto done;
2765 kerr = khtml_attr(gw_trans->gw_html_req,
2766 KELEM_DIV, KATTR_ID,
2767 "summary_tags_content", KATTR__MAX);
2768 if (kerr != KCGI_OK)
2769 goto done;
2770 summary_header_displayed = 1;
2773 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2774 KATTR_ID, "tags_wrapper", KATTR__MAX);
2775 if (kerr != KCGI_OK)
2776 goto done;
2777 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2778 KATTR_ID, "tags_age", KATTR__MAX);
2779 if (kerr != KCGI_OK)
2780 goto done;
2781 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
2782 if (error)
2783 goto done;
2784 kerr = khtml_puts(gw_trans->gw_html_req,
2785 age ? age : "");
2786 if (kerr != KCGI_OK)
2787 goto done;
2788 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2789 if (kerr != KCGI_OK)
2790 goto done;
2791 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2792 KATTR_ID, "tags", KATTR__MAX);
2793 if (kerr != KCGI_OK)
2794 goto done;
2795 kerr = khtml_puts(gw_trans->gw_html_req, refname);
2796 if (kerr != KCGI_OK)
2797 goto done;
2798 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2799 if (kerr != KCGI_OK)
2800 goto done;
2801 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2802 KATTR_ID, "tags_name", KATTR__MAX);
2803 if (kerr != KCGI_OK)
2804 goto done;
2805 if (asprintf(&href_tag, "?path=%s&action=tag&commit=%s",
2806 gw_trans->repo_name, id_str) == -1) {
2807 error = got_error_from_errno("asprintf");
2808 goto done;
2810 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2811 KATTR_HREF, href_tag, KATTR__MAX);
2812 if (kerr != KCGI_OK)
2813 goto done;
2814 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
2815 if (kerr != KCGI_OK)
2816 goto done;
2817 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2818 if (kerr != KCGI_OK)
2819 goto done;
2821 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2822 KATTR_ID, "navs_wrapper", KATTR__MAX);
2823 if (kerr != KCGI_OK)
2824 goto done;
2825 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2826 KATTR_ID, "navs", KATTR__MAX);
2827 if (kerr != KCGI_OK)
2828 goto done;
2830 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2831 KATTR_HREF, href_tag, KATTR__MAX);
2832 if (kerr != KCGI_OK)
2833 goto done;
2834 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
2835 if (kerr != KCGI_OK)
2836 goto done;
2837 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2838 if (kerr != KCGI_OK)
2839 goto done;
2841 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2842 if (kerr != KCGI_OK)
2843 goto done;
2844 if (asprintf(&href_briefs,
2845 "?path=%s&action=briefs&commit=%s",
2846 gw_trans->repo_name, id_str) == -1) {
2847 error = got_error_from_errno("asprintf");
2848 goto done;
2850 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2851 KATTR_HREF, href_briefs, KATTR__MAX);
2852 if (kerr != KCGI_OK)
2853 goto done;
2854 kerr = khtml_puts(gw_trans->gw_html_req,
2855 "commit briefs");
2856 if (kerr != KCGI_OK)
2857 goto done;
2858 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2859 if (kerr != KCGI_OK)
2860 goto done;
2862 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2863 if (kerr != KCGI_OK)
2864 goto done;
2866 if (asprintf(&href_commits,
2867 "?path=%s&action=commits&commit=%s",
2868 gw_trans->repo_name, id_str) == -1) {
2869 error = got_error_from_errno("asprintf");
2870 goto done;
2872 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2873 KATTR_HREF, href_commits, KATTR__MAX);
2874 if (kerr != KCGI_OK)
2875 goto done;
2876 kerr = khtml_puts(gw_trans->gw_html_req,
2877 "commits");
2878 if (kerr != KCGI_OK)
2879 goto done;
2880 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2881 if (kerr != KCGI_OK)
2882 goto done;
2884 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2885 KATTR_ID, "dotted_line", KATTR__MAX);
2886 if (kerr != KCGI_OK)
2887 goto done;
2888 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2889 if (kerr != KCGI_OK)
2890 goto done;
2891 break;
2892 case TAGFULL:
2893 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2894 KATTR_ID, "tag_info_date_title", KATTR__MAX);
2895 if (kerr != KCGI_OK)
2896 goto done;
2897 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
2898 if (kerr != KCGI_OK)
2899 goto done;
2900 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2901 if (kerr != KCGI_OK)
2902 goto done;
2903 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2904 KATTR_ID, "tag_info_date", KATTR__MAX);
2905 if (kerr != KCGI_OK)
2906 goto done;
2907 error = gw_get_time_str(&age, tagger_time, TM_LONG);
2908 if (error)
2909 goto done;
2910 kerr = khtml_puts(gw_trans->gw_html_req,
2911 age ? age : "");
2912 if (kerr != KCGI_OK)
2913 goto done;
2914 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2915 if (kerr != KCGI_OK)
2916 goto done;
2918 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2919 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
2920 if (kerr != KCGI_OK)
2921 goto done;
2922 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
2923 if (kerr != KCGI_OK)
2924 goto done;
2925 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2926 if (kerr != KCGI_OK)
2927 goto done;
2928 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2929 KATTR_ID, "tag_info_date", KATTR__MAX);
2930 if (kerr != KCGI_OK)
2931 goto done;
2932 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
2933 if (kerr != KCGI_OK)
2934 goto done;
2935 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2936 if (kerr != KCGI_OK)
2937 goto done;
2939 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2940 KATTR_ID, "tag_info", KATTR__MAX);
2941 if (kerr != KCGI_OK)
2942 goto done;
2943 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
2944 if (kerr != KCGI_OK)
2945 goto done;
2946 break;
2947 default:
2948 break;
2950 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2951 if (kerr != KCGI_OK)
2952 goto done;
2954 if (limit && --limit == 0)
2955 break;
2957 if (tag)
2958 got_object_tag_close(tag);
2959 tag = NULL;
2960 free(id_str);
2961 id_str = NULL;
2962 free(age);
2963 age = NULL;
2964 free(tag_commit0);
2965 tag_commit0 = NULL;
2966 free(href_tag);
2967 href_tag = NULL;
2968 free(href_briefs);
2969 href_briefs = NULL;
2970 free(href_commits);
2971 href_commits = NULL;
2973 done:
2974 if (tag)
2975 got_object_tag_close(tag);
2976 free(id_str);
2977 free(age);
2978 free(tag_commit0);
2979 free(href_tag);
2980 free(href_briefs);
2981 free(href_commits);
2982 got_ref_list_free(&refs);
2983 if (error == NULL && kerr != KCGI_OK)
2984 error = gw_kcgi_error(kerr);
2985 return error;
2988 static void
2989 gw_free_header(struct gw_header *header)
2991 free(header->path);
2992 free(header->author);
2993 free(header->committer);
2994 free(header->refs_str);
2995 free(header->commit_id);
2996 free(header->parent_id);
2997 free(header->tree_id);
2998 free(header->commit_msg);
3001 static struct gw_header *
3002 gw_init_header()
3004 struct gw_header *header;
3006 header = malloc(sizeof(*header));
3007 if (header == NULL)
3008 return NULL;
3010 header->path = NULL;
3011 SIMPLEQ_INIT(&header->refs);
3013 header->refs_str = NULL;
3014 header->commit_id = NULL;
3015 header->committer = NULL;
3016 header->author = NULL;
3017 header->parent_id = NULL;
3018 header->tree_id = NULL;
3019 header->commit_msg = NULL;
3021 return header;
3024 static const struct got_error *
3025 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
3026 int limit, struct got_object_id *id)
3028 const struct got_error *error = NULL;
3029 struct got_commit_graph *graph = NULL;
3030 struct got_commit_object *commit = NULL;
3032 error = got_commit_graph_open(&graph, header->path, 0);
3033 if (error)
3034 return error;
3036 error = got_commit_graph_iter_start(graph, id, gw_trans->repo, NULL,
3037 NULL);
3038 if (error)
3039 goto done;
3041 for (;;) {
3042 error = got_commit_graph_iter_next(&id, graph, gw_trans->repo,
3043 NULL, NULL);
3044 if (error) {
3045 if (error->code == GOT_ERR_ITER_COMPLETED)
3046 error = NULL;
3047 goto done;
3049 if (id == NULL)
3050 goto done;
3052 error = got_object_open_as_commit(&commit, gw_trans->repo, id);
3053 if (error)
3054 goto done;
3055 if (limit == 1) {
3056 error = gw_get_commit(gw_trans, header, commit, id);
3057 if (error)
3058 goto done;
3059 } else {
3060 struct gw_header *n_header = NULL;
3061 if ((n_header = gw_init_header()) == NULL) {
3062 error = got_error_from_errno("malloc");
3063 goto done;
3065 error = got_ref_list(&n_header->refs, gw_trans->repo,
3066 NULL, got_ref_cmp_by_name, NULL);
3067 if (error)
3068 goto done;
3070 error = gw_get_commit(gw_trans, n_header, commit, id);
3071 if (error)
3072 goto done;
3073 got_ref_list_free(&n_header->refs);
3074 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3075 entry);
3077 if (error || (limit && --limit == 0))
3078 break;
3080 done:
3081 if (commit != NULL)
3082 got_object_commit_close(commit);
3083 if (graph)
3084 got_commit_graph_close(graph);
3085 return error;
3088 static const struct got_error *
3089 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header,
3090 struct got_commit_object *commit, struct got_object_id *id)
3092 const struct got_error *error = NULL;
3093 struct got_reflist_entry *re;
3094 struct got_object_id *id2 = NULL;
3095 struct got_object_qid *parent_id;
3096 char *commit_msg = NULL, *commit_msg0;
3098 /*print commit*/
3099 SIMPLEQ_FOREACH(re, &header->refs, entry) {
3100 char *s;
3101 const char *name;
3102 struct got_tag_object *tag = NULL;
3103 int cmp;
3105 if (got_ref_is_symbolic(re->ref))
3106 continue;
3108 name = got_ref_get_name(re->ref);
3109 if (strncmp(name, "refs/", 5) == 0)
3110 name += 5;
3111 if (strncmp(name, "got/", 4) == 0)
3112 continue;
3113 if (strncmp(name, "heads/", 6) == 0)
3114 name += 6;
3115 if (strncmp(name, "remotes/", 8) == 0)
3116 name += 8;
3117 if (strncmp(name, "tags/", 5) == 0) {
3118 error = got_object_open_as_tag(&tag, gw_trans->repo,
3119 re->id);
3120 if (error) {
3121 if (error->code != GOT_ERR_OBJ_TYPE)
3122 continue;
3124 * Ref points at something other
3125 * than a tag.
3127 error = NULL;
3128 tag = NULL;
3131 cmp = got_object_id_cmp(tag ?
3132 got_object_tag_get_object_id(tag) : re->id, id);
3133 if (tag)
3134 got_object_tag_close(tag);
3135 if (cmp != 0)
3136 continue;
3137 s = header->refs_str;
3138 if (asprintf(&header->refs_str, "%s%s%s", s ? s : "",
3139 s ? ", " : "", name) == -1) {
3140 error = got_error_from_errno("asprintf");
3141 free(s);
3142 header->refs_str = NULL;
3143 return error;
3145 free(s);
3148 error = got_object_id_str(&header->commit_id, id);
3149 if (error)
3150 return error;
3152 error = got_object_id_str(&header->tree_id,
3153 got_object_commit_get_tree_id(commit));
3154 if (error)
3155 return error;
3157 if (gw_trans->action == GW_DIFF) {
3158 parent_id = SIMPLEQ_FIRST(
3159 got_object_commit_get_parent_ids(commit));
3160 if (parent_id != NULL) {
3161 id2 = got_object_id_dup(parent_id->id);
3162 free (parent_id);
3163 error = got_object_id_str(&header->parent_id, id2);
3164 if (error)
3165 return error;
3166 free(id2);
3167 } else {
3168 header->parent_id = strdup("/dev/null");
3169 if (header->parent_id == NULL) {
3170 error = got_error_from_errno("strdup");
3171 return error;
3176 header->committer_time =
3177 got_object_commit_get_committer_time(commit);
3179 header->author =
3180 strdup(got_object_commit_get_author(commit));
3181 if (header->author == NULL) {
3182 error = got_error_from_errno("strdup");
3183 return error;
3185 header->committer =
3186 strdup(got_object_commit_get_committer(commit));
3187 if (header->committer == NULL) {
3188 error = got_error_from_errno("strdup");
3189 return error;
3191 error = got_object_commit_get_logmsg(&commit_msg0, commit);
3192 if (error)
3193 return error;
3195 commit_msg = commit_msg0;
3196 while (*commit_msg == '\n')
3197 commit_msg++;
3199 header->commit_msg = strdup(commit_msg);
3200 if (header->commit_msg == NULL)
3201 error = got_error_from_errno("strdup");
3202 free(commit_msg0);
3203 return error;
3206 static const struct got_error *
3207 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3209 const struct got_error *error = NULL;
3210 char *in_repo_path = NULL;
3211 struct got_object_id *id = NULL;
3213 error = got_repo_open(&gw_trans->repo, gw_trans->repo_path, NULL);
3214 if (error)
3215 return error;
3217 if (gw_trans->commit_id == NULL) {
3218 struct got_reference *head_ref;
3219 error = got_ref_open(&head_ref, gw_trans->repo,
3220 gw_trans->headref, 0);
3221 if (error)
3222 return error;
3224 error = got_ref_resolve(&id, gw_trans->repo, head_ref);
3225 got_ref_close(head_ref);
3226 if (error)
3227 return error;
3228 } else {
3229 struct got_reference *ref;
3230 error = got_ref_open(&ref, gw_trans->repo,
3231 gw_trans->commit_id, 0);
3232 if (error == NULL) {
3233 int obj_type;
3234 error = got_ref_resolve(&id, gw_trans->repo, ref);
3235 got_ref_close(ref);
3236 if (error)
3237 return error;
3238 error = got_object_get_type(&obj_type, gw_trans->repo,
3239 id);
3240 if (error)
3241 goto done;
3242 if (obj_type == GOT_OBJ_TYPE_TAG) {
3243 struct got_tag_object *tag;
3244 error = got_object_open_as_tag(&tag,
3245 gw_trans->repo, id);
3246 if (error)
3247 goto done;
3248 if (got_object_tag_get_object_type(tag) !=
3249 GOT_OBJ_TYPE_COMMIT) {
3250 got_object_tag_close(tag);
3251 error = got_error(GOT_ERR_OBJ_TYPE);
3252 goto done;
3254 free(id);
3255 id = got_object_id_dup(
3256 got_object_tag_get_object_id(tag));
3257 if (id == NULL)
3258 error = got_error_from_errno(
3259 "got_object_id_dup");
3260 got_object_tag_close(tag);
3261 if (error)
3262 goto done;
3263 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3264 error = got_error(GOT_ERR_OBJ_TYPE);
3265 goto done;
3268 error = got_repo_match_object_id_prefix(&id,
3269 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT,
3270 gw_trans->repo);
3271 if (error)
3272 goto done;
3275 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
3276 gw_trans->repo_path, 1);
3277 if (error)
3278 goto done;
3280 if (in_repo_path) {
3281 header->path = strdup(in_repo_path);
3282 if (header->path == NULL) {
3283 error = got_error_from_errno("strdup");
3284 goto done;
3288 error = got_ref_list(&header->refs, gw_trans->repo, NULL,
3289 got_ref_cmp_by_name, NULL);
3290 if (error)
3291 goto done;
3293 error = gw_get_commits(gw_trans, header, limit, id);
3294 done:
3295 got_ref_list_free(&header->refs);
3296 free(id);
3297 free(in_repo_path);
3298 return error;
3301 struct blame_line {
3302 int annotated;
3303 char *id_str;
3304 char *committer;
3305 char datebuf[11]; /* YYYY-MM-DD + NUL */
3308 struct gw_blame_cb_args {
3309 struct blame_line *lines;
3310 int nlines;
3311 int nlines_prec;
3312 int lineno_cur;
3313 off_t *line_offsets;
3314 FILE *f;
3315 struct got_repository *repo;
3316 struct gw_trans *gw_trans;
3319 static const struct got_error *
3320 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3322 const struct got_error *err = NULL;
3323 struct gw_blame_cb_args *a = arg;
3324 struct blame_line *bline;
3325 char *line = NULL;
3326 size_t linesize = 0;
3327 struct got_commit_object *commit = NULL;
3328 off_t offset;
3329 struct tm tm;
3330 time_t committer_time;
3331 enum kcgi_err kerr = KCGI_OK;
3333 if (nlines != a->nlines ||
3334 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3335 return got_error(GOT_ERR_RANGE);
3337 if (lineno == -1)
3338 return NULL; /* no change in this commit */
3340 /* Annotate this line. */
3341 bline = &a->lines[lineno - 1];
3342 if (bline->annotated)
3343 return NULL;
3344 err = got_object_id_str(&bline->id_str, id);
3345 if (err)
3346 return err;
3348 err = got_object_open_as_commit(&commit, a->repo, id);
3349 if (err)
3350 goto done;
3352 bline->committer = strdup(got_object_commit_get_committer(commit));
3353 if (bline->committer == NULL) {
3354 err = got_error_from_errno("strdup");
3355 goto done;
3358 committer_time = got_object_commit_get_committer_time(commit);
3359 if (localtime_r(&committer_time, &tm) == NULL)
3360 return got_error_from_errno("localtime_r");
3361 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3362 &tm) >= sizeof(bline->datebuf)) {
3363 err = got_error(GOT_ERR_NO_SPACE);
3364 goto done;
3366 bline->annotated = 1;
3368 /* Print lines annotated so far. */
3369 bline = &a->lines[a->lineno_cur - 1];
3370 if (!bline->annotated)
3371 goto done;
3373 offset = a->line_offsets[a->lineno_cur - 1];
3374 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3375 err = got_error_from_errno("fseeko");
3376 goto done;
3379 while (bline->annotated) {
3380 char *smallerthan, *at, *nl, *committer;
3381 char *lineno = NULL, *href_diff = NULL, *href_link = NULL;
3382 size_t len;
3384 if (getline(&line, &linesize, a->f) == -1) {
3385 if (ferror(a->f))
3386 err = got_error_from_errno("getline");
3387 break;
3390 committer = bline->committer;
3391 smallerthan = strchr(committer, '<');
3392 if (smallerthan && smallerthan[1] != '\0')
3393 committer = smallerthan + 1;
3394 at = strchr(committer, '@');
3395 if (at)
3396 *at = '\0';
3397 len = strlen(committer);
3398 if (len >= 9)
3399 committer[8] = '\0';
3401 nl = strchr(line, '\n');
3402 if (nl)
3403 *nl = '\0';
3405 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3406 "blame_wrapper", KATTR__MAX);
3407 if (kerr != KCGI_OK)
3408 goto err;
3409 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3410 "blame_number", KATTR__MAX);
3411 if (kerr != KCGI_OK)
3412 goto err;
3413 if (asprintf(&lineno, "%.*d", a->nlines_prec,
3414 a->lineno_cur) == -1)
3415 goto err;
3416 kerr = khtml_puts(a->gw_trans->gw_html_req, lineno);
3417 if (kerr != KCGI_OK)
3418 goto err;
3419 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3420 if (kerr != KCGI_OK)
3421 goto err;
3423 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3424 "blame_hash", KATTR__MAX);
3425 if (kerr != KCGI_OK)
3426 goto err;
3428 if (asprintf(&href_diff,
3429 "?path=%s&action=diff&commit=%s&file=%s&folder=%s",
3430 a->gw_trans->repo_name, bline->id_str,
3431 a->gw_trans->repo_file, a->gw_trans->repo_folder ?
3432 a->gw_trans->repo_folder : "") == -1) {
3433 err = got_error_from_errno("asprintf");
3434 goto err;
3436 if (asprintf(&href_link, "%.8s", bline->id_str) == -1) {
3437 err = got_error_from_errno("asprintf");
3438 goto err;
3440 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3441 KATTR_HREF, href_diff, KATTR__MAX);
3442 if (kerr != KCGI_OK)
3443 goto done;
3444 kerr = khtml_puts(a->gw_trans->gw_html_req, href_link);
3445 if (kerr != KCGI_OK)
3446 goto err;
3447 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3448 if (kerr != KCGI_OK)
3449 goto err;
3451 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3452 "blame_date", KATTR__MAX);
3453 if (kerr != KCGI_OK)
3454 goto err;
3455 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3456 if (kerr != KCGI_OK)
3457 goto err;
3458 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3459 if (kerr != KCGI_OK)
3460 goto err;
3462 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3463 "blame_author", KATTR__MAX);
3464 if (kerr != KCGI_OK)
3465 goto err;
3466 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
3467 if (kerr != KCGI_OK)
3468 goto err;
3469 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3470 if (kerr != KCGI_OK)
3471 goto err;
3473 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3474 "blame_code", KATTR__MAX);
3475 if (kerr != KCGI_OK)
3476 goto err;
3477 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
3478 if (kerr != KCGI_OK)
3479 goto err;
3480 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3481 if (kerr != KCGI_OK)
3482 goto err;
3484 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3485 if (kerr != KCGI_OK)
3486 goto err;
3488 a->lineno_cur++;
3489 bline = &a->lines[a->lineno_cur - 1];
3490 err:
3491 free(lineno);
3492 free(href_diff);
3493 free(href_link);
3495 done:
3496 if (commit)
3497 got_object_commit_close(commit);
3498 free(line);
3499 if (err == NULL && kerr != KCGI_OK)
3500 err = gw_kcgi_error(kerr);
3501 return err;
3504 static const struct got_error *
3505 gw_output_file_blame(struct gw_trans *gw_trans)
3507 const struct got_error *error = NULL;
3508 struct got_object_id *obj_id = NULL;
3509 struct got_object_id *commit_id = NULL;
3510 struct got_blob_object *blob = NULL;
3511 char *path = NULL, *in_repo_path = NULL;
3512 struct gw_blame_cb_args bca;
3513 int i, obj_type;
3514 size_t filesize;
3516 if (asprintf(&path, "%s%s%s",
3517 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3518 gw_trans->repo_folder ? "/" : "",
3519 gw_trans->repo_file) == -1) {
3520 error = got_error_from_errno("asprintf");
3521 goto done;
3524 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path, 1);
3525 if (error)
3526 goto done;
3528 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
3529 GOT_OBJ_TYPE_COMMIT, 1, gw_trans->repo);
3530 if (error)
3531 goto done;
3533 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
3534 in_repo_path);
3535 if (error)
3536 goto done;
3538 if (obj_id == NULL) {
3539 error = got_error(GOT_ERR_NO_OBJ);
3540 goto done;
3543 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
3544 if (error)
3545 goto done;
3547 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3548 error = got_error(GOT_ERR_OBJ_TYPE);
3549 goto done;
3552 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
3553 if (error)
3554 goto done;
3556 bca.f = got_opentemp();
3557 if (bca.f == NULL) {
3558 error = got_error_from_errno("got_opentemp");
3559 goto done;
3561 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3562 &bca.line_offsets, bca.f, blob);
3563 if (error || bca.nlines == 0)
3564 goto done;
3566 /* Don't include \n at EOF in the blame line count. */
3567 if (bca.line_offsets[bca.nlines - 1] == filesize)
3568 bca.nlines--;
3570 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3571 if (bca.lines == NULL) {
3572 error = got_error_from_errno("calloc");
3573 goto done;
3575 bca.lineno_cur = 1;
3576 bca.nlines_prec = 0;
3577 i = bca.nlines;
3578 while (i > 0) {
3579 i /= 10;
3580 bca.nlines_prec++;
3582 bca.repo = gw_trans->repo;
3583 bca.gw_trans = gw_trans;
3585 error = got_blame(in_repo_path, commit_id, gw_trans->repo, gw_blame_cb,
3586 &bca, NULL, NULL);
3587 done:
3588 free(in_repo_path);
3589 free(commit_id);
3590 free(obj_id);
3591 free(path);
3593 if (blob) {
3594 free(bca.line_offsets);
3595 for (i = 0; i < bca.nlines; i++) {
3596 struct blame_line *bline = &bca.lines[i];
3597 free(bline->id_str);
3598 free(bline->committer);
3600 free(bca.lines);
3601 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3602 error = got_error_from_errno("fclose");
3604 if (blob)
3605 got_object_blob_close(blob);
3606 return error;
3609 static const struct got_error *
3610 gw_output_blob_buf(struct gw_trans *gw_trans)
3612 const struct got_error *error = NULL;
3613 struct got_object_id *obj_id = NULL;
3614 struct got_object_id *commit_id = NULL;
3615 struct got_blob_object *blob = NULL;
3616 char *path = NULL, *in_repo_path = NULL;
3617 int obj_type, set_mime = 0;
3618 size_t len, hdrlen;
3619 const uint8_t *buf;
3620 enum kcgi_err kerr = KCGI_OK;
3622 if (asprintf(&path, "%s%s%s",
3623 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3624 gw_trans->repo_folder ? "/" : "",
3625 gw_trans->repo_file) == -1) {
3626 error = got_error_from_errno("asprintf");
3627 goto done;
3630 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path, 1);
3631 if (error)
3632 goto done;
3634 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
3635 GOT_OBJ_TYPE_COMMIT, 1, gw_trans->repo);
3636 if (error)
3637 goto done;
3639 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
3640 in_repo_path);
3641 if (error)
3642 goto done;
3644 if (obj_id == NULL) {
3645 error = got_error(GOT_ERR_NO_OBJ);
3646 goto done;
3649 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
3650 if (error)
3651 goto done;
3653 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3654 error = got_error(GOT_ERR_OBJ_TYPE);
3655 goto done;
3658 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
3659 if (error)
3660 goto done;
3662 hdrlen = got_object_blob_get_hdrlen(blob);
3663 do {
3664 error = got_object_blob_read_block(&len, blob);
3665 if (error)
3666 goto done;
3667 buf = got_object_blob_get_read_buf(blob);
3670 * Skip blob object header first time around,
3671 * which also contains a zero byte.
3673 buf += hdrlen;
3674 if (set_mime == 0) {
3675 if (isbinary(buf, len - hdrlen))
3676 gw_trans->mime = KMIME_APP_OCTET_STREAM;
3677 else
3678 gw_trans->mime = KMIME_TEXT_PLAIN;
3679 set_mime = 1;
3680 error = gw_display_index(gw_trans);
3681 if (error)
3682 goto done;
3684 kerr = khttp_write(gw_trans->gw_req, buf, len - hdrlen);
3685 hdrlen = 0;
3686 } while (len != 0);
3687 done:
3688 free(in_repo_path);
3689 free(commit_id);
3690 free(obj_id);
3691 free(path);
3692 if (blob)
3693 got_object_blob_close(blob);
3694 if (error == NULL && kerr != KCGI_OK)
3695 error = gw_kcgi_error(kerr);
3696 return error;
3699 static const struct got_error *
3700 gw_output_repo_tree(struct gw_trans *gw_trans)
3702 const struct got_error *error = NULL;
3703 struct got_object_id *tree_id = NULL, *commit_id = NULL;
3704 struct got_tree_object *tree = NULL;
3705 char *path = NULL, *in_repo_path = NULL;
3706 char *id_str = NULL;
3707 char *build_folder = NULL;
3708 char *href_blob = NULL, *href_blame = NULL;
3709 const char *class = NULL;
3710 int nentries, i, class_flip = 0;
3711 enum kcgi_err kerr = KCGI_OK;
3713 if (gw_trans->repo_folder != NULL) {
3714 path = strdup(gw_trans->repo_folder);
3715 if (path == NULL) {
3716 error = got_error_from_errno("strdup");
3717 goto done;
3719 } else {
3720 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
3721 gw_trans->repo_path, 1);
3722 if (error)
3723 goto done;
3724 free(path);
3725 path = in_repo_path;
3728 if (gw_trans->commit_id == NULL) {
3729 struct got_reference *head_ref;
3730 error = got_ref_open(&head_ref, gw_trans->repo,
3731 gw_trans->headref, 0);
3732 if (error)
3733 goto done;
3734 error = got_ref_resolve(&commit_id, gw_trans->repo, head_ref);
3735 if (error)
3736 goto done;
3737 got_ref_close(head_ref);
3739 * gw_trans->commit_id was not parsed from the querystring
3740 * we hit this code path from gw_index, where we don't know the
3741 * commit values for the tree link yet, so set
3742 * gw_trans->commit_id here to continue further into the tree
3744 error = got_object_id_str(&gw_trans->commit_id, commit_id);
3745 if (error)
3746 goto done;
3748 } else {
3749 error = got_repo_match_object_id(&commit_id, NULL,
3750 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT, 1,
3751 gw_trans->repo);
3752 if (error)
3753 goto done;
3756 error = got_object_id_by_path(&tree_id, gw_trans->repo, commit_id,
3757 path);
3758 if (error)
3759 goto done;
3761 error = got_object_open_as_tree(&tree, gw_trans->repo, tree_id);
3762 if (error)
3763 goto done;
3765 nentries = got_object_tree_get_nentries(tree);
3766 for (i = 0; i < nentries; i++) {
3767 struct got_tree_entry *te;
3768 const char *modestr = "";
3769 mode_t mode;
3771 te = got_object_tree_get_entry(tree, i);
3773 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
3774 if (error)
3775 goto done;
3777 mode = got_tree_entry_get_mode(te);
3778 if (got_object_tree_entry_is_submodule(te))
3779 modestr = "$";
3780 else if (S_ISLNK(mode))
3781 modestr = "@";
3782 else if (S_ISDIR(mode))
3783 modestr = "/";
3784 else if (mode & S_IXUSR)
3785 modestr = "*";
3787 if (class_flip == 0) {
3788 class = "back_lightgray";
3789 class_flip = 1;
3790 } else {
3791 class = "back_white";
3792 class_flip = 0;
3795 if (S_ISDIR(mode)) {
3796 if (asprintf(&build_folder, "%s/%s",
3797 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3798 got_tree_entry_get_name(te)) == -1) {
3799 error = got_error_from_errno("asprintf");
3800 goto done;
3802 if (asprintf(&href_blob,
3803 "?path=%s&action=%s&commit=%s&folder=%s",
3804 gw_trans->repo_name, gw_get_action_name(gw_trans),
3805 gw_trans->commit_id, build_folder) == -1) {
3806 error = got_error_from_errno("asprintf");
3807 goto done;
3810 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3811 KATTR_ID, "tree_wrapper", KATTR__MAX);
3812 if (kerr != KCGI_OK)
3813 goto done;
3814 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3815 KATTR_ID, "tree_line", KATTR_CLASS, class,
3816 KATTR__MAX);
3817 if (kerr != KCGI_OK)
3818 goto done;
3819 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3820 KATTR_HREF, href_blob, KATTR_CLASS,
3821 "diff_directory", KATTR__MAX);
3822 if (kerr != KCGI_OK)
3823 goto done;
3824 kerr = khtml_puts(gw_trans->gw_html_req,
3825 got_tree_entry_get_name(te));
3826 if (kerr != KCGI_OK)
3827 goto done;
3828 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3829 if (kerr != KCGI_OK)
3830 goto done;
3831 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3832 if (kerr != KCGI_OK)
3833 goto done;
3834 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3835 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
3836 KATTR__MAX);
3837 if (kerr != KCGI_OK)
3838 goto done;
3839 kerr = khtml_entity(gw_trans->gw_html_req,
3840 KENTITY_nbsp);
3841 if (kerr != KCGI_OK)
3842 goto done;
3843 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3844 if (kerr != KCGI_OK)
3845 goto done;
3846 } else {
3847 if (asprintf(&href_blob,
3848 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3849 gw_trans->repo_name, "blob", gw_trans->commit_id,
3850 got_tree_entry_get_name(te),
3851 gw_trans->repo_folder ?
3852 gw_trans->repo_folder : "") == -1) {
3853 error = got_error_from_errno("asprintf");
3854 goto done;
3856 if (asprintf(&href_blame,
3857 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3858 gw_trans->repo_name, "blame", gw_trans->commit_id,
3859 got_tree_entry_get_name(te),
3860 gw_trans->repo_folder ?
3861 gw_trans->repo_folder : "") == -1) {
3862 error = got_error_from_errno("asprintf");
3863 goto done;
3866 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3867 KATTR_ID, "tree_wrapper", KATTR__MAX);
3868 if (kerr != KCGI_OK)
3869 goto done;
3870 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3871 KATTR_ID, "tree_line", KATTR_CLASS, class,
3872 KATTR__MAX);
3873 if (kerr != KCGI_OK)
3874 goto done;
3875 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3876 KATTR_HREF, href_blob, KATTR__MAX);
3877 if (kerr != KCGI_OK)
3878 goto done;
3879 kerr = khtml_puts(gw_trans->gw_html_req,
3880 got_tree_entry_get_name(te));
3881 if (kerr != KCGI_OK)
3882 goto done;
3883 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3884 if (kerr != KCGI_OK)
3885 goto done;
3886 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3887 if (kerr != KCGI_OK)
3888 goto done;
3889 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3890 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
3891 KATTR__MAX);
3892 if (kerr != KCGI_OK)
3893 goto done;
3895 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3896 KATTR_HREF, href_blob, KATTR__MAX);
3897 if (kerr != KCGI_OK)
3898 goto done;
3899 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
3900 if (kerr != KCGI_OK)
3901 goto done;
3902 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3903 if (kerr != KCGI_OK)
3904 goto done;
3906 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3907 if (kerr != KCGI_OK)
3908 goto done;
3910 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3911 KATTR_HREF, href_blame, KATTR__MAX);
3912 if (kerr != KCGI_OK)
3913 goto done;
3914 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
3915 if (kerr != KCGI_OK)
3916 goto done;
3918 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3919 if (kerr != KCGI_OK)
3920 goto done;
3922 free(id_str);
3923 id_str = NULL;
3924 free(href_blob);
3925 href_blob = NULL;
3926 free(build_folder);
3927 build_folder = NULL;
3929 done:
3930 if (tree)
3931 got_object_tree_close(tree);
3932 free(id_str);
3933 free(href_blob);
3934 free(href_blame);
3935 free(in_repo_path);
3936 free(tree_id);
3937 free(build_folder);
3938 if (error == NULL && kerr != KCGI_OK)
3939 error = gw_kcgi_error(kerr);
3940 return error;
3943 static const struct got_error *
3944 gw_output_repo_heads(struct gw_trans *gw_trans)
3946 const struct got_error *error = NULL;
3947 struct got_reflist_head refs;
3948 struct got_reflist_entry *re;
3949 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
3950 char *href_commits = NULL;
3951 enum kcgi_err kerr = KCGI_OK;
3953 SIMPLEQ_INIT(&refs);
3955 error = got_ref_list(&refs, gw_trans->repo, "refs/heads",
3956 got_ref_cmp_by_name, NULL);
3957 if (error)
3958 goto done;
3960 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3961 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
3962 if (kerr != KCGI_OK)
3963 goto done;
3964 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3965 KATTR_ID, "summary_heads_title", KATTR__MAX);
3966 if (kerr != KCGI_OK)
3967 goto done;
3968 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
3969 if (kerr != KCGI_OK)
3970 goto done;
3971 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3972 if (kerr != KCGI_OK)
3973 goto done;
3974 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3975 KATTR_ID, "summary_heads_content", KATTR__MAX);
3976 if (kerr != KCGI_OK)
3977 goto done;
3979 SIMPLEQ_FOREACH(re, &refs, entry) {
3980 const char *refname;
3982 if (got_ref_is_symbolic(re->ref))
3983 continue;
3985 refname = got_ref_get_name(re->ref);
3986 if (refname == NULL) {
3987 error = got_error_from_errno("got_ref_to_str");
3988 goto done;
3991 if (strncmp(refname, "refs/heads/", 11) != 0)
3992 continue;
3994 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
3995 refname, TM_DIFF);
3996 if (error)
3997 goto done;
3999 if (strncmp(refname, "refs/heads/", 11) == 0)
4000 refname += 11;
4002 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4003 KATTR_ID, "heads_wrapper", KATTR__MAX);
4004 if (kerr != KCGI_OK)
4005 goto done;
4006 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4007 KATTR_ID, "heads_age", KATTR__MAX);
4008 if (kerr != KCGI_OK)
4009 goto done;
4010 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
4011 if (kerr != KCGI_OK)
4012 goto done;
4013 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4014 if (kerr != KCGI_OK)
4015 goto done;
4016 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4017 KATTR_ID, "heads_space", KATTR__MAX);
4018 if (kerr != KCGI_OK)
4019 goto done;
4020 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
4021 if (kerr != KCGI_OK)
4022 goto done;
4023 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4024 if (kerr != KCGI_OK)
4025 goto done;
4026 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4027 KATTR_ID, "head", KATTR__MAX);
4028 if (kerr != KCGI_OK)
4029 goto done;
4030 if (asprintf(&href_summary,
4031 "?path=%s&action=summary&headref=%s",
4032 gw_trans->repo_name, refname) == -1) {
4033 error = got_error_from_errno("asprintf");
4034 goto done;
4036 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4037 href_summary, KATTR__MAX);
4038 kerr = khtml_puts(gw_trans->gw_html_req, refname);
4039 if (kerr != KCGI_OK)
4040 goto done;
4041 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4042 if (kerr != KCGI_OK)
4043 goto done;
4045 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4046 "navs_wrapper", KATTR__MAX);
4047 if (kerr != KCGI_OK)
4048 goto done;
4049 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4050 "navs", KATTR__MAX);
4051 if (kerr != KCGI_OK)
4052 goto done;
4054 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4055 href_summary, KATTR__MAX);
4056 if (kerr != KCGI_OK)
4057 goto done;
4058 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4059 if (kerr != KCGI_OK)
4060 goto done;
4061 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4062 if (kerr != KCGI_OK)
4063 goto done;
4065 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4066 if (kerr != KCGI_OK)
4067 goto done;
4068 if (asprintf(&href_briefs, "?path=%s&action=briefs&headref=%s",
4069 gw_trans->repo_name, refname) == -1) {
4070 error = got_error_from_errno("asprintf");
4071 goto done;
4073 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4074 href_briefs, KATTR__MAX);
4075 if (kerr != KCGI_OK)
4076 goto done;
4077 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4078 if (kerr != KCGI_OK)
4079 goto done;
4080 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4081 if (kerr != KCGI_OK)
4082 goto done;
4084 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4085 if (kerr != KCGI_OK)
4086 goto done;
4088 if (asprintf(&href_commits,
4089 "?path=%s&action=commits&headref=%s",
4090 gw_trans->repo_name, refname) == -1) {
4091 error = got_error_from_errno("asprintf");
4092 goto done;
4094 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4095 href_commits, KATTR__MAX);
4096 if (kerr != KCGI_OK)
4097 goto done;
4098 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4099 if (kerr != KCGI_OK)
4100 goto done;
4101 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4102 if (kerr != KCGI_OK)
4103 goto done;
4105 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4106 "dotted_line", KATTR__MAX);
4107 if (kerr != KCGI_OK)
4108 goto done;
4109 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4110 if (kerr != KCGI_OK)
4111 goto done;
4112 free(href_summary);
4113 href_summary = NULL;
4114 free(href_briefs);
4115 href_briefs = NULL;
4116 free(href_commits);
4117 href_commits = NULL;
4119 done:
4120 got_ref_list_free(&refs);
4121 free(href_summary);
4122 free(href_briefs);
4123 free(href_commits);
4124 return error;
4127 static const struct got_error *
4128 gw_output_site_link(struct gw_trans *gw_trans)
4130 const struct got_error *error = NULL;
4131 char *href_summary = NULL;
4132 enum kcgi_err kerr = KCGI_OK;
4134 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4135 "site_link", KATTR__MAX);
4136 if (kerr != KCGI_OK)
4137 goto done;
4138 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4139 KATTR__MAX);
4140 if (kerr != KCGI_OK)
4141 goto done;
4142 kerr = khtml_puts(gw_trans->gw_html_req,
4143 gw_trans->gw_conf->got_site_link);
4144 if (kerr != KCGI_OK)
4145 goto done;
4146 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4147 if (kerr != KCGI_OK)
4148 goto done;
4150 if (gw_trans->repo_name != NULL) {
4151 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4152 if (kerr != KCGI_OK)
4153 goto done;
4154 if (asprintf(&href_summary, "?path=%s&action=summary",
4155 gw_trans->repo_name) == -1)
4156 goto done;
4157 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4158 href_summary, KATTR__MAX);
4159 if (kerr != KCGI_OK)
4160 goto done;
4161 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4162 if (kerr != KCGI_OK)
4163 goto done;
4164 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4165 if (kerr != KCGI_OK)
4166 goto done;
4167 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4168 if (kerr != KCGI_OK)
4169 goto done;
4170 kerr = khtml_puts(gw_trans->gw_html_req,
4171 gw_get_action_name(gw_trans));
4172 if (kerr != KCGI_OK)
4173 goto done;
4176 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4177 if (kerr != KCGI_OK)
4178 goto done;
4179 done:
4180 free(href_summary);
4181 return error;
4184 static const struct got_error *
4185 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4187 const struct got_error *error = NULL;
4188 char *color = NULL;
4189 enum kcgi_err kerr = KCGI_OK;
4191 if (strncmp(buf, "-", 1) == 0)
4192 color = "diff_minus";
4193 else if (strncmp(buf, "+", 1) == 0)
4194 color = "diff_plus";
4195 else if (strncmp(buf, "@@", 2) == 0)
4196 color = "diff_chunk_header";
4197 else if (strncmp(buf, "@@", 2) == 0)
4198 color = "diff_chunk_header";
4199 else if (strncmp(buf, "commit +", 8) == 0)
4200 color = "diff_meta";
4201 else if (strncmp(buf, "commit -", 8) == 0)
4202 color = "diff_meta";
4203 else if (strncmp(buf, "blob +", 6) == 0)
4204 color = "diff_meta";
4205 else if (strncmp(buf, "blob -", 6) == 0)
4206 color = "diff_meta";
4207 else if (strncmp(buf, "file +", 6) == 0)
4208 color = "diff_meta";
4209 else if (strncmp(buf, "file -", 6) == 0)
4210 color = "diff_meta";
4211 else if (strncmp(buf, "from:", 5) == 0)
4212 color = "diff_author";
4213 else if (strncmp(buf, "via:", 4) == 0)
4214 color = "diff_author";
4215 else if (strncmp(buf, "date:", 5) == 0)
4216 color = "diff_date";
4217 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4218 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4219 if (error == NULL && kerr != KCGI_OK)
4220 error = gw_kcgi_error(kerr);
4221 return error;
4224 int
4225 main(int argc, char *argv[])
4227 const struct got_error *error = NULL;
4228 struct gw_trans *gw_trans;
4229 struct gw_dir *dir = NULL, *tdir;
4230 const char *page = "index";
4231 int gw_malloc = 1;
4232 enum kcgi_err kerr;
4234 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4235 errx(1, "malloc");
4237 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4238 errx(1, "malloc");
4240 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4241 errx(1, "malloc");
4243 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4244 errx(1, "malloc");
4246 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4247 if (kerr != KCGI_OK) {
4248 error = gw_kcgi_error(kerr);
4249 goto done;
4252 if ((gw_trans->gw_conf =
4253 malloc(sizeof(struct gotweb_conf))) == NULL) {
4254 gw_malloc = 0;
4255 error = got_error_from_errno("malloc");
4256 goto done;
4259 TAILQ_INIT(&gw_trans->gw_dirs);
4260 TAILQ_INIT(&gw_trans->gw_headers);
4262 gw_trans->action = -1;
4263 gw_trans->page = 0;
4264 gw_trans->repos_total = 0;
4265 gw_trans->repo_path = NULL;
4266 gw_trans->commit_id = NULL;
4267 gw_trans->headref = GOT_REF_HEAD;
4268 gw_trans->mime = KMIME_TEXT_HTML;
4269 gw_trans->gw_tmpl->key = gw_templs;
4270 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4271 gw_trans->gw_tmpl->arg = gw_trans;
4272 gw_trans->gw_tmpl->cb = gw_template;
4273 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
4274 if (error)
4275 goto done;
4277 error = gw_parse_querystring(gw_trans);
4278 if (error)
4279 goto done;
4281 if (gw_trans->action == GW_BLOB)
4282 error = gw_blob(gw_trans);
4283 else
4284 error = gw_display_index(gw_trans);
4285 done:
4286 if (gw_malloc) {
4287 free(gw_trans->gw_conf->got_repos_path);
4288 free(gw_trans->gw_conf->got_site_name);
4289 free(gw_trans->gw_conf->got_site_owner);
4290 free(gw_trans->gw_conf->got_site_link);
4291 free(gw_trans->gw_conf->got_logo);
4292 free(gw_trans->gw_conf->got_logo_url);
4293 free(gw_trans->gw_conf);
4294 free(gw_trans->commit_id);
4295 free(gw_trans->repo_path);
4296 if (gw_trans->repo)
4297 got_repo_close(gw_trans->repo);
4299 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4300 free(dir->name);
4301 free(dir->description);
4302 free(dir->age);
4303 free(dir->url);
4304 free(dir->path);
4305 free(dir);
4310 khttp_free(gw_trans->gw_req);
4311 return 0;