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(GOT_TMPDIR_STR, "rwc") != 0)
300 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
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_path(gw_dir->name, GOT_ERR_NOT_GIT_REPO);
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 return got_error(GOT_ERR_QUERYSTRING);
1706 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1707 /* define gw_trans->repo_path */
1708 gw_trans->repo_name = p->parsed.s;
1710 if (asprintf(&gw_trans->repo_path, "%s/%s",
1711 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
1712 return got_error_from_errno("asprintf");
1714 /* get action and set function */
1715 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION])) {
1716 for (i = 0; i < nitems(gw_query_funcs); i++) {
1717 action = &gw_query_funcs[i];
1718 if (action->func_name == NULL)
1719 continue;
1720 if (strcmp(action->func_name,
1721 p->parsed.s) == 0) {
1722 gw_trans->action = i;
1723 break;
1727 if (gw_trans->action == -1) {
1728 gw_trans->action = GW_ERR;
1729 gw_trans->error = got_error_msg(GOT_ERR_QUERYSTRING,
1730 p != NULL ? "bad action in querystring" :
1731 "no action in querystring");
1732 return error;
1735 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID])) {
1736 if (asprintf(&gw_trans->commit_id, "%s",
1737 p->parsed.s) == -1)
1738 return got_error_from_errno("asprintf");
1741 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1742 gw_trans->repo_file = p->parsed.s;
1744 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER])) {
1745 if (asprintf(&gw_trans->repo_folder, "%s",
1746 p->parsed.s) == -1)
1747 return got_error_from_errno("asprintf");
1750 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
1751 gw_trans->headref = p->parsed.s;
1753 error = gw_init_gw_dir(&gw_trans->gw_dir, gw_trans->repo_name);
1754 if (error)
1755 return error;
1757 gw_trans->error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1758 } else
1759 gw_trans->action = GW_INDEX;
1761 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1762 gw_trans->page = p->parsed.i;
1764 return error;
1767 static const struct got_error *
1768 gw_init_gw_dir(struct gw_dir **gw_dir, const char *dir)
1770 const struct got_error *error;
1772 *gw_dir = malloc(sizeof(**gw_dir));
1773 if (*gw_dir == NULL)
1774 return got_error_from_errno("malloc");
1776 if (asprintf(&(*gw_dir)->name, "%s", dir) == -1) {
1777 error = got_error_from_errno("asprintf");
1778 free(*gw_dir);
1779 *gw_dir = NULL;
1780 return NULL;
1783 return NULL;
1786 static const struct got_error *
1787 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1789 enum kcgi_err kerr;
1791 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1792 if (kerr != KCGI_OK)
1793 return gw_kcgi_error(kerr);
1794 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1795 khttps[code]);
1796 if (kerr != KCGI_OK)
1797 return gw_kcgi_error(kerr);
1798 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1799 kmimetypes[mime]);
1800 if (kerr != KCGI_OK)
1801 return gw_kcgi_error(kerr);
1802 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
1803 "nosniff");
1804 if (kerr != KCGI_OK)
1805 return gw_kcgi_error(kerr);
1806 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1807 if (kerr != KCGI_OK)
1808 return gw_kcgi_error(kerr);
1809 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
1810 "1; mode=block");
1811 if (kerr != KCGI_OK)
1812 return gw_kcgi_error(kerr);
1814 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
1815 kerr = khttp_head(gw_trans->gw_req,
1816 kresps[KRESP_CONTENT_DISPOSITION],
1817 "attachment; filename=%s", gw_trans->repo_file);
1818 if (kerr != KCGI_OK)
1819 return gw_kcgi_error(kerr);
1822 kerr = khttp_body(gw_trans->gw_req);
1823 return gw_kcgi_error(kerr);
1826 static const struct got_error *
1827 gw_display_index(struct gw_trans *gw_trans)
1829 const struct got_error *error;
1830 enum kcgi_err kerr;
1832 /* catch early querystring errors */
1833 if (gw_trans->error)
1834 gw_trans->action = GW_ERR;
1836 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1837 if (error)
1838 return error;
1840 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1841 if (kerr != KCGI_OK)
1842 return gw_kcgi_error(kerr);
1844 if (gw_trans->action != GW_BLOB) {
1845 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1846 gw_query_funcs[gw_trans->action].template);
1847 if (kerr != KCGI_OK) {
1848 khtml_close(gw_trans->gw_html_req);
1849 return gw_kcgi_error(kerr);
1853 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
1856 static const struct got_error *
1857 gw_error(struct gw_trans *gw_trans)
1859 enum kcgi_err kerr;
1861 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->error->msg);
1863 return gw_kcgi_error(kerr);
1866 static int
1867 gw_template(size_t key, void *arg)
1869 const struct got_error *error = NULL;
1870 enum kcgi_err kerr;
1871 struct gw_trans *gw_trans = arg;
1872 char *img_src = NULL;
1874 switch (key) {
1875 case (TEMPL_HEAD):
1876 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1877 KATTR_NAME, "viewport",
1878 KATTR_CONTENT, "initial-scale=.75, user-scalable=yes",
1879 KATTR__MAX);
1880 if (kerr != KCGI_OK)
1881 return 0;
1882 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1883 if (kerr != KCGI_OK)
1884 return 0;
1885 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1886 KATTR_CHARSET, "utf-8",
1887 KATTR__MAX);
1888 if (kerr != KCGI_OK)
1889 return 0;
1890 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1891 if (kerr != KCGI_OK)
1892 return 0;
1893 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1894 KATTR_NAME, "msapplication-TileColor",
1895 KATTR_CONTENT, "#da532c", KATTR__MAX);
1896 if (kerr != KCGI_OK)
1897 return 0;
1898 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1899 if (kerr != KCGI_OK)
1900 return 0;
1901 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1902 KATTR_NAME, "theme-color",
1903 KATTR_CONTENT, "#ffffff", KATTR__MAX);
1904 if (kerr != KCGI_OK)
1905 return 0;
1906 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1907 if (kerr != KCGI_OK)
1908 return 0;
1909 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1910 KATTR_REL, "apple-touch-icon", KATTR_SIZES, "180x180",
1911 KATTR_HREF, "/apple-touch-icon.png", KATTR__MAX);
1912 if (kerr != KCGI_OK)
1913 return 0;
1914 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1915 if (kerr != KCGI_OK)
1916 return 0;
1917 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1918 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
1919 "32x32", KATTR_HREF, "/favicon-32x32.png", KATTR__MAX);
1920 if (kerr != KCGI_OK)
1921 return 0;
1922 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1923 if (kerr != KCGI_OK)
1924 return 0;
1925 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1926 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
1927 "16x16", KATTR_HREF, "/favicon-16x16.png", KATTR__MAX);
1928 if (kerr != KCGI_OK)
1929 return 0;
1930 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1931 if (kerr != KCGI_OK)
1932 return 0;
1933 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1934 KATTR_REL, "manifest", KATTR_HREF, "/site.webmanifest",
1935 KATTR__MAX);
1936 if (kerr != KCGI_OK)
1937 return 0;
1938 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1939 if (kerr != KCGI_OK)
1940 return 0;
1941 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1942 KATTR_REL, "mask-icon", KATTR_HREF,
1943 "/safari-pinned-tab.svg", KATTR__MAX);
1944 if (kerr != KCGI_OK)
1945 return 0;
1946 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1947 if (kerr != KCGI_OK)
1948 return 0;
1949 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1950 KATTR_REL, "stylesheet", KATTR_TYPE, "text/css",
1951 KATTR_HREF, "/gotweb.css", KATTR__MAX);
1952 if (kerr != KCGI_OK)
1953 return 0;
1954 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1955 if (kerr != KCGI_OK)
1956 return 0;
1957 break;
1958 case(TEMPL_HEADER):
1959 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1960 KATTR_ID, "got_link", KATTR__MAX);
1961 if (kerr != KCGI_OK)
1962 return 0;
1963 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1964 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
1965 KATTR_TARGET, "_sotd", KATTR__MAX);
1966 if (kerr != KCGI_OK)
1967 return 0;
1968 if (asprintf(&img_src, "/%s",
1969 gw_trans->gw_conf->got_logo) == -1)
1970 return 0;
1971 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
1972 KATTR_SRC, img_src, KATTR__MAX);
1973 if (kerr != KCGI_OK) {
1974 free(img_src);
1975 return 0;
1977 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1978 if (kerr != KCGI_OK) {
1979 free(img_src);
1980 return 0;
1982 break;
1983 case (TEMPL_SITEPATH):
1984 error = gw_output_site_link(gw_trans);
1985 if (error)
1986 return 0;
1987 break;
1988 case(TEMPL_TITLE):
1989 if (gw_trans->gw_conf->got_site_name != NULL) {
1990 kerr = khtml_puts(gw_trans->gw_html_req,
1991 gw_trans->gw_conf->got_site_name);
1992 if (kerr != KCGI_OK)
1993 return 0;
1995 break;
1996 case (TEMPL_SEARCH):
1997 break;
1998 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1999 "search", KATTR__MAX);
2000 if (kerr != KCGI_OK)
2001 return 0;
2002 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
2003 KATTR_METHOD, "POST", KATTR__MAX);
2004 if (kerr != KCGI_OK)
2005 return 0;
2006 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
2007 "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
2008 KATTR_MAXLENGTH, "50", KATTR__MAX);
2009 if (kerr != KCGI_OK)
2010 return 0;
2011 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
2012 KATTR__MAX);
2013 if (kerr != KCGI_OK)
2014 return 0;
2015 kerr = khtml_puts(gw_trans->gw_html_req, "Search");
2016 if (kerr != KCGI_OK)
2017 return 0;
2018 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
2019 if (kerr != KCGI_OK)
2020 return 0;
2021 break;
2022 case(TEMPL_SITEOWNER):
2023 if (gw_trans->gw_conf->got_site_owner != NULL &&
2024 gw_trans->gw_conf->got_show_site_owner) {
2025 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2026 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
2027 if (kerr != KCGI_OK)
2028 return 0;
2029 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2030 KATTR_ID, "site_owner", KATTR__MAX);
2031 if (kerr != KCGI_OK)
2032 return 0;
2033 kerr = khtml_puts(gw_trans->gw_html_req,
2034 gw_trans->gw_conf->got_site_owner);
2035 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2036 if (kerr != KCGI_OK)
2037 return 0;
2039 break;
2040 case(TEMPL_CONTENT):
2041 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
2042 if (error) {
2043 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2044 KATTR_ID, "tmpl_err", KATTR__MAX);
2045 if (kerr != KCGI_OK)
2046 return 0;
2047 kerr = khttp_puts(gw_trans->gw_req, "Error: ");
2048 if (kerr != KCGI_OK)
2049 return 0;
2050 kerr = khttp_puts(gw_trans->gw_req, error->msg);
2051 if (kerr != KCGI_OK)
2052 return 0;
2053 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2054 if (kerr != KCGI_OK)
2055 return 0;
2057 break;
2058 default:
2059 return 0;
2061 return 1;
2064 static const struct got_error *
2065 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
2067 const struct got_error *error = NULL;
2068 enum kcgi_err kerr = KCGI_OK;
2070 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2071 KATTR_ID, "header_commit_title", KATTR__MAX);
2072 if (kerr != KCGI_OK)
2073 goto done;
2074 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
2075 if (kerr != KCGI_OK)
2076 goto done;
2077 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2078 if (kerr != KCGI_OK)
2079 goto done;
2080 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2081 KATTR_ID, "header_commit", KATTR__MAX);
2082 if (kerr != KCGI_OK)
2083 goto done;
2084 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2085 if (kerr != KCGI_OK)
2086 goto done;
2087 kerr = khtml_puts(gw_trans->gw_html_req, " ");
2088 if (kerr != KCGI_OK)
2089 goto done;
2090 if (str2 != NULL) {
2091 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
2092 KATTR_ID, "refs_str", KATTR__MAX);
2093 if (kerr != KCGI_OK)
2094 goto done;
2095 kerr = khtml_puts(gw_trans->gw_html_req, "(");
2096 if (kerr != KCGI_OK)
2097 goto done;
2098 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2099 if (kerr != KCGI_OK)
2100 goto done;
2101 kerr = khtml_puts(gw_trans->gw_html_req, ")");
2102 if (kerr != KCGI_OK)
2103 goto done;
2104 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2105 if (kerr != KCGI_OK)
2106 goto done;
2108 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2109 done:
2110 if (error == NULL && kerr != KCGI_OK)
2111 error = gw_kcgi_error(kerr);
2112 return error;
2115 static const struct got_error *
2116 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2118 const struct got_error *error = NULL;
2119 enum kcgi_err kerr = KCGI_OK;
2121 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2122 KATTR_ID, "header_diff_title", KATTR__MAX);
2123 if (kerr != KCGI_OK)
2124 goto done;
2125 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2126 if (kerr != KCGI_OK)
2127 goto done;
2128 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2129 if (kerr != KCGI_OK)
2130 goto done;
2131 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2132 KATTR_ID, "header_diff", KATTR__MAX);
2133 if (kerr != KCGI_OK)
2134 goto done;
2135 if (str1 != NULL) {
2136 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2137 if (kerr != KCGI_OK)
2138 goto done;
2140 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2141 if (kerr != KCGI_OK)
2142 goto done;
2143 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2144 if (kerr != KCGI_OK)
2145 goto done;
2146 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2147 done:
2148 if (error == NULL && kerr != KCGI_OK)
2149 error = gw_kcgi_error(kerr);
2150 return error;
2153 static const struct got_error *
2154 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2156 const struct got_error *error = NULL;
2157 enum kcgi_err kerr = KCGI_OK;
2159 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2160 KATTR_ID, "header_age_title", KATTR__MAX);
2161 if (kerr != KCGI_OK)
2162 goto done;
2163 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2164 if (kerr != KCGI_OK)
2165 goto done;
2166 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2167 if (kerr != KCGI_OK)
2168 goto done;
2169 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2170 KATTR_ID, "header_age", KATTR__MAX);
2171 if (kerr != KCGI_OK)
2172 goto done;
2173 kerr = khtml_puts(gw_trans->gw_html_req, str);
2174 if (kerr != KCGI_OK)
2175 goto done;
2176 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2177 done:
2178 if (error == NULL && kerr != KCGI_OK)
2179 error = gw_kcgi_error(kerr);
2180 return error;
2183 static const struct got_error *
2184 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2186 const struct got_error *error = NULL;
2187 enum kcgi_err kerr;
2189 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2190 KATTR_ID, "header_author_title", KATTR__MAX);
2191 if (kerr != KCGI_OK)
2192 goto done;
2193 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2194 if (kerr != KCGI_OK)
2195 goto done;
2196 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2197 if (kerr != KCGI_OK)
2198 goto done;
2199 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2200 KATTR_ID, "header_author", KATTR__MAX);
2201 if (kerr != KCGI_OK)
2202 goto done;
2203 kerr = khtml_puts(gw_trans->gw_html_req, str);
2204 if (kerr != KCGI_OK)
2205 goto done;
2206 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2207 done:
2208 if (error == NULL && kerr != KCGI_OK)
2209 error = gw_kcgi_error(kerr);
2210 return error;
2213 static const struct got_error *
2214 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2216 const struct got_error *error = NULL;
2217 enum kcgi_err kerr;
2219 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2220 KATTR_ID, "header_committer_title", KATTR__MAX);
2221 if (kerr != KCGI_OK)
2222 goto done;
2223 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2224 if (kerr != KCGI_OK)
2225 goto done;
2226 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2227 if (kerr != KCGI_OK)
2228 goto done;
2229 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2230 KATTR_ID, "header_committer", KATTR__MAX);
2231 if (kerr != KCGI_OK)
2232 goto done;
2233 kerr = khtml_puts(gw_trans->gw_html_req, str);
2234 if (kerr != KCGI_OK)
2235 goto done;
2236 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2237 done:
2238 if (error == NULL && kerr != KCGI_OK)
2239 error = gw_kcgi_error(kerr);
2240 return error;
2243 static const struct got_error *
2244 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2246 const struct got_error *error = NULL;
2247 enum kcgi_err kerr;
2249 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2250 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2251 if (kerr != KCGI_OK)
2252 goto done;
2253 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2254 if (kerr != KCGI_OK)
2255 goto done;
2256 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2257 if (kerr != KCGI_OK)
2258 goto done;
2259 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2260 KATTR_ID, "header_commit_msg", KATTR__MAX);
2261 if (kerr != KCGI_OK)
2262 goto done;
2263 kerr = khttp_puts(gw_trans->gw_req, str);
2264 if (kerr != KCGI_OK)
2265 goto done;
2266 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2267 done:
2268 if (error == NULL && kerr != KCGI_OK)
2269 error = gw_kcgi_error(kerr);
2270 return error;
2273 static const struct got_error *
2274 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2276 const struct got_error *error = NULL;
2277 enum kcgi_err kerr;
2279 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2280 KATTR_ID, "header_tree_title", KATTR__MAX);
2281 if (kerr != KCGI_OK)
2282 goto done;
2283 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2284 if (kerr != KCGI_OK)
2285 goto done;
2286 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2287 if (kerr != KCGI_OK)
2288 goto done;
2289 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2290 KATTR_ID, "header_tree", KATTR__MAX);
2291 if (kerr != KCGI_OK)
2292 goto done;
2293 kerr = khtml_puts(gw_trans->gw_html_req, str);
2294 if (kerr != KCGI_OK)
2295 goto done;
2296 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2297 done:
2298 if (error == NULL && kerr != KCGI_OK)
2299 error = gw_kcgi_error(kerr);
2300 return error;
2303 static const struct got_error *
2304 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2305 char *dir)
2307 const struct got_error *error = NULL;
2308 FILE *f = NULL;
2309 char *d_file = NULL;
2310 unsigned int len;
2311 size_t n;
2313 *description = NULL;
2314 if (gw_trans->gw_conf->got_show_repo_description == 0)
2315 return NULL;
2317 if (asprintf(&d_file, "%s/description", dir) == -1)
2318 return got_error_from_errno("asprintf");
2320 f = fopen(d_file, "r");
2321 if (f == NULL) {
2322 if (errno == ENOENT || errno == EACCES)
2323 return NULL;
2324 error = got_error_from_errno2("fopen", d_file);
2325 goto done;
2328 if (fseek(f, 0, SEEK_END) == -1) {
2329 error = got_ferror(f, GOT_ERR_IO);
2330 goto done;
2332 len = ftell(f);
2333 if (len == -1) {
2334 error = got_ferror(f, GOT_ERR_IO);
2335 goto done;
2337 if (fseek(f, 0, SEEK_SET) == -1) {
2338 error = got_ferror(f, GOT_ERR_IO);
2339 goto done;
2341 *description = calloc(len + 1, sizeof(**description));
2342 if (*description == NULL) {
2343 error = got_error_from_errno("calloc");
2344 goto done;
2347 n = fread(*description, 1, len, f);
2348 if (n == 0 && ferror(f))
2349 error = got_ferror(f, GOT_ERR_IO);
2350 done:
2351 if (f != NULL && fclose(f) == -1 && error == NULL)
2352 error = got_error_from_errno("fclose");
2353 free(d_file);
2354 return error;
2357 static const struct got_error *
2358 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2360 struct tm tm;
2361 time_t diff_time;
2362 char *years = "years ago", *months = "months ago";
2363 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2364 char *minutes = "minutes ago", *seconds = "seconds ago";
2365 char *now = "right now";
2366 char *s;
2367 char datebuf[29];
2369 *repo_age = NULL;
2371 switch (ref_tm) {
2372 case TM_DIFF:
2373 diff_time = time(NULL) - committer_time;
2374 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2375 if (asprintf(repo_age, "%lld %s",
2376 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2377 return got_error_from_errno("asprintf");
2378 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2379 if (asprintf(repo_age, "%lld %s",
2380 (diff_time / 60 / 60 / 24 / (365 / 12)),
2381 months) == -1)
2382 return got_error_from_errno("asprintf");
2383 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2384 if (asprintf(repo_age, "%lld %s",
2385 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2386 return got_error_from_errno("asprintf");
2387 } else if (diff_time > 60 * 60 * 24 * 2) {
2388 if (asprintf(repo_age, "%lld %s",
2389 (diff_time / 60 / 60 / 24), days) == -1)
2390 return got_error_from_errno("asprintf");
2391 } else if (diff_time > 60 * 60 * 2) {
2392 if (asprintf(repo_age, "%lld %s",
2393 (diff_time / 60 / 60), hours) == -1)
2394 return got_error_from_errno("asprintf");
2395 } else if (diff_time > 60 * 2) {
2396 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2397 minutes) == -1)
2398 return got_error_from_errno("asprintf");
2399 } else if (diff_time > 2) {
2400 if (asprintf(repo_age, "%lld %s", diff_time,
2401 seconds) == -1)
2402 return got_error_from_errno("asprintf");
2403 } else {
2404 if (asprintf(repo_age, "%s", now) == -1)
2405 return got_error_from_errno("asprintf");
2407 break;
2408 case TM_LONG:
2409 if (gmtime_r(&committer_time, &tm) == NULL)
2410 return got_error_from_errno("gmtime_r");
2412 s = asctime_r(&tm, datebuf);
2413 if (s == NULL)
2414 return got_error_from_errno("asctime_r");
2416 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2417 return got_error_from_errno("asprintf");
2418 break;
2420 return NULL;
2423 static const struct got_error *
2424 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2425 const char *refname, int ref_tm)
2427 const struct got_error *error = NULL;
2428 struct got_repository *repo = NULL;
2429 struct got_commit_object *commit = NULL;
2430 struct got_reflist_head refs;
2431 struct got_reflist_entry *re;
2432 time_t committer_time = 0, cmp_time = 0;
2434 *repo_age = NULL;
2435 SIMPLEQ_INIT(&refs);
2437 if (gw_trans->gw_conf->got_show_repo_age == 0)
2438 return NULL;
2440 if (gw_trans->repo)
2441 repo = gw_trans->repo;
2442 else {
2443 error = got_repo_open(&repo, dir, NULL);
2444 if (error)
2445 return error;
2448 error = got_ref_list(&refs, repo, "refs/heads",
2449 got_ref_cmp_by_name, NULL);
2450 if (error)
2451 goto done;
2454 * Find the youngest branch tip in the repository, or the age of
2455 * the a specific branch tip if a name was provided by the caller.
2457 SIMPLEQ_FOREACH(re, &refs, entry) {
2458 struct got_object_id *id = NULL;
2460 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
2461 continue;
2463 error = got_ref_resolve(&id, repo, re->ref);
2464 if (error)
2465 goto done;
2467 error = got_object_open_as_commit(&commit, repo, id);
2468 free(id);
2469 if (error)
2470 goto done;
2472 committer_time =
2473 got_object_commit_get_committer_time(commit);
2474 got_object_commit_close(commit);
2475 if (cmp_time < committer_time)
2476 cmp_time = committer_time;
2478 if (refname)
2479 break;
2482 if (cmp_time != 0) {
2483 committer_time = cmp_time;
2484 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2486 done:
2487 got_ref_list_free(&refs);
2488 if (gw_trans->repo == NULL)
2489 got_repo_close(repo);
2490 return error;
2493 static const struct got_error *
2494 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2496 const struct got_error *error;
2497 FILE *f = NULL;
2498 struct got_object_id *id1 = NULL, *id2 = NULL;
2499 char *label1 = NULL, *label2 = NULL, *line = NULL;
2500 int obj_type;
2501 size_t linesize = 0;
2502 ssize_t linelen;
2503 enum kcgi_err kerr = KCGI_OK;
2505 f = got_opentemp();
2506 if (f == NULL)
2507 return NULL;
2509 if (header->parent_id != NULL &&
2510 strncmp(header->parent_id, "/dev/null", 9) != 0) {
2511 error = got_repo_match_object_id(&id1, &label1,
2512 header->parent_id, GOT_OBJ_TYPE_ANY, 1, gw_trans->repo);
2513 if (error)
2514 goto done;
2517 error = got_repo_match_object_id(&id2, &label2,
2518 header->commit_id, GOT_OBJ_TYPE_ANY, 1, gw_trans->repo);
2519 if (error)
2520 goto done;
2522 error = got_object_get_type(&obj_type, gw_trans->repo, id2);
2523 if (error)
2524 goto done;
2525 switch (obj_type) {
2526 case GOT_OBJ_TYPE_BLOB:
2527 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
2528 gw_trans->repo, f);
2529 break;
2530 case GOT_OBJ_TYPE_TREE:
2531 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
2532 gw_trans->repo, f);
2533 break;
2534 case GOT_OBJ_TYPE_COMMIT:
2535 error = got_diff_objects_as_commits(id1, id2, 3, 0,
2536 gw_trans->repo, f);
2537 break;
2538 default:
2539 error = got_error(GOT_ERR_OBJ_TYPE);
2541 if (error)
2542 goto done;
2544 if (fseek(f, 0, SEEK_SET) == -1) {
2545 error = got_ferror(f, GOT_ERR_IO);
2546 goto done;
2549 while ((linelen = getline(&line, &linesize, f)) != -1) {
2550 error = gw_colordiff_line(gw_trans, line);
2551 if (error)
2552 goto done;
2553 /* XXX: KHTML_PRETTY breaks this */
2554 kerr = khtml_puts(gw_trans->gw_html_req, line);
2555 if (kerr != KCGI_OK)
2556 goto done;
2557 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2558 if (kerr != KCGI_OK)
2559 goto done;
2561 if (linelen == -1 && ferror(f))
2562 error = got_error_from_errno("getline");
2563 done:
2564 if (f && fclose(f) == -1 && error == NULL)
2565 error = got_error_from_errno("fclose");
2566 free(line);
2567 free(label1);
2568 free(label2);
2569 free(id1);
2570 free(id2);
2572 if (error == NULL && kerr != KCGI_OK)
2573 error = gw_kcgi_error(kerr);
2574 return error;
2577 static const struct got_error *
2578 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2580 const struct got_error *error = NULL;
2581 struct got_repository *repo;
2582 const char *gitconfig_owner;
2584 *owner = NULL;
2586 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2587 return NULL;
2589 error = got_repo_open(&repo, dir, NULL);
2590 if (error)
2591 return error;
2592 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2593 if (gitconfig_owner) {
2594 *owner = strdup(gitconfig_owner);
2595 if (*owner == NULL)
2596 error = got_error_from_errno("strdup");
2598 got_repo_close(repo);
2599 return error;
2602 static const struct got_error *
2603 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2605 const struct got_error *error = NULL;
2606 FILE *f;
2607 char *d_file = NULL;
2608 unsigned int len;
2609 size_t n;
2611 *url = NULL;
2613 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2614 return got_error_from_errno("asprintf");
2616 f = fopen(d_file, "r");
2617 if (f == NULL) {
2618 if (errno != ENOENT && errno != EACCES)
2619 error = got_error_from_errno2("fopen", d_file);
2620 goto done;
2623 if (fseek(f, 0, SEEK_END) == -1) {
2624 error = got_ferror(f, GOT_ERR_IO);
2625 goto done;
2627 len = ftell(f);
2628 if (len == -1) {
2629 error = got_ferror(f, GOT_ERR_IO);
2630 goto done;
2632 if (fseek(f, 0, SEEK_SET) == -1) {
2633 error = got_ferror(f, GOT_ERR_IO);
2634 goto done;
2637 *url = calloc(len + 1, sizeof(**url));
2638 if (*url == NULL) {
2639 error = got_error_from_errno("calloc");
2640 goto done;
2643 n = fread(*url, 1, len, f);
2644 if (n == 0 && ferror(f))
2645 error = got_ferror(f, GOT_ERR_IO);
2646 done:
2647 if (f && fclose(f) == -1 && error == NULL)
2648 error = got_error_from_errno("fclose");
2649 free(d_file);
2650 return NULL;
2653 static const struct got_error *
2654 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
2655 int limit, int tag_type)
2657 const struct got_error *error = NULL;
2658 struct got_reflist_head refs;
2659 struct got_reflist_entry *re;
2660 char *age = NULL;
2661 char *id_str = NULL, *newline, *href_commits = NULL;
2662 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
2663 struct got_tag_object *tag = NULL;
2664 enum kcgi_err kerr = KCGI_OK;
2665 int summary_header_displayed = 0;
2667 SIMPLEQ_INIT(&refs);
2669 error = got_ref_list(&refs, gw_trans->repo, "refs/tags",
2670 got_ref_cmp_tags, gw_trans->repo);
2671 if (error)
2672 goto done;
2674 SIMPLEQ_FOREACH(re, &refs, entry) {
2675 const char *refname;
2676 const char *tagger;
2677 const char *tag_commit;
2678 time_t tagger_time;
2679 struct got_object_id *id;
2680 struct got_commit_object *commit = NULL;
2682 refname = got_ref_get_name(re->ref);
2683 if (strncmp(refname, "refs/tags/", 10) != 0)
2684 continue;
2685 refname += 10;
2687 error = got_ref_resolve(&id, gw_trans->repo, re->ref);
2688 if (error)
2689 goto done;
2691 error = got_object_open_as_tag(&tag, gw_trans->repo, id);
2692 if (error) {
2693 if (error->code != GOT_ERR_OBJ_TYPE) {
2694 free(id);
2695 goto done;
2697 /* "lightweight" tag */
2698 error = got_object_open_as_commit(&commit,
2699 gw_trans->repo, id);
2700 if (error) {
2701 free(id);
2702 goto done;
2704 tagger = got_object_commit_get_committer(commit);
2705 tagger_time =
2706 got_object_commit_get_committer_time(commit);
2707 error = got_object_id_str(&id_str, id);
2708 free(id);
2709 } else {
2710 free(id);
2711 tagger = got_object_tag_get_tagger(tag);
2712 tagger_time = got_object_tag_get_tagger_time(tag);
2713 error = got_object_id_str(&id_str,
2714 got_object_tag_get_object_id(tag));
2716 if (error)
2717 goto done;
2719 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
2720 strlen(id_str)) != 0)
2721 continue;
2723 if (commit) {
2724 error = got_object_commit_get_logmsg(&tag_commit0,
2725 commit);
2726 if (error)
2727 goto done;
2728 got_object_commit_close(commit);
2729 } else {
2730 tag_commit0 = strdup(got_object_tag_get_message(tag));
2731 if (tag_commit0 == NULL) {
2732 error = got_error_from_errno("strdup");
2733 goto done;
2737 tag_commit = tag_commit0;
2738 while (*tag_commit == '\n')
2739 tag_commit++;
2741 switch (tag_type) {
2742 case TAGBRIEF:
2743 newline = strchr(tag_commit, '\n');
2744 if (newline)
2745 *newline = '\0';
2747 if (summary_header_displayed == 0) {
2748 kerr = khtml_attr(gw_trans->gw_html_req,
2749 KELEM_DIV, KATTR_ID,
2750 "summary_tags_title_wrapper", KATTR__MAX);
2751 if (kerr != KCGI_OK)
2752 goto done;
2753 kerr = khtml_attr(gw_trans->gw_html_req,
2754 KELEM_DIV, KATTR_ID,
2755 "summary_tags_title", KATTR__MAX);
2756 if (kerr != KCGI_OK)
2757 goto done;
2758 kerr = khtml_puts(gw_trans->gw_html_req,
2759 "Tags");
2760 if (kerr != KCGI_OK)
2761 goto done;
2762 kerr = khtml_closeelem(gw_trans->gw_html_req,
2763 2);
2764 if (kerr != KCGI_OK)
2765 goto done;
2766 kerr = khtml_attr(gw_trans->gw_html_req,
2767 KELEM_DIV, KATTR_ID,
2768 "summary_tags_content", KATTR__MAX);
2769 if (kerr != KCGI_OK)
2770 goto done;
2771 summary_header_displayed = 1;
2774 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2775 KATTR_ID, "tags_wrapper", KATTR__MAX);
2776 if (kerr != KCGI_OK)
2777 goto done;
2778 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2779 KATTR_ID, "tags_age", KATTR__MAX);
2780 if (kerr != KCGI_OK)
2781 goto done;
2782 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
2783 if (error)
2784 goto done;
2785 kerr = khtml_puts(gw_trans->gw_html_req,
2786 age ? age : "");
2787 if (kerr != KCGI_OK)
2788 goto done;
2789 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2790 if (kerr != KCGI_OK)
2791 goto done;
2792 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2793 KATTR_ID, "tags", KATTR__MAX);
2794 if (kerr != KCGI_OK)
2795 goto done;
2796 kerr = khtml_puts(gw_trans->gw_html_req, refname);
2797 if (kerr != KCGI_OK)
2798 goto done;
2799 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2800 if (kerr != KCGI_OK)
2801 goto done;
2802 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2803 KATTR_ID, "tags_name", KATTR__MAX);
2804 if (kerr != KCGI_OK)
2805 goto done;
2806 if (asprintf(&href_tag, "?path=%s&action=tag&commit=%s",
2807 gw_trans->repo_name, id_str) == -1) {
2808 error = got_error_from_errno("asprintf");
2809 goto done;
2811 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2812 KATTR_HREF, href_tag, KATTR__MAX);
2813 if (kerr != KCGI_OK)
2814 goto done;
2815 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
2816 if (kerr != KCGI_OK)
2817 goto done;
2818 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2819 if (kerr != KCGI_OK)
2820 goto done;
2822 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2823 KATTR_ID, "navs_wrapper", KATTR__MAX);
2824 if (kerr != KCGI_OK)
2825 goto done;
2826 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2827 KATTR_ID, "navs", KATTR__MAX);
2828 if (kerr != KCGI_OK)
2829 goto done;
2831 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2832 KATTR_HREF, href_tag, KATTR__MAX);
2833 if (kerr != KCGI_OK)
2834 goto done;
2835 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
2836 if (kerr != KCGI_OK)
2837 goto done;
2838 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2839 if (kerr != KCGI_OK)
2840 goto done;
2842 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2843 if (kerr != KCGI_OK)
2844 goto done;
2845 if (asprintf(&href_briefs,
2846 "?path=%s&action=briefs&commit=%s",
2847 gw_trans->repo_name, id_str) == -1) {
2848 error = got_error_from_errno("asprintf");
2849 goto done;
2851 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2852 KATTR_HREF, href_briefs, KATTR__MAX);
2853 if (kerr != KCGI_OK)
2854 goto done;
2855 kerr = khtml_puts(gw_trans->gw_html_req,
2856 "commit briefs");
2857 if (kerr != KCGI_OK)
2858 goto done;
2859 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2860 if (kerr != KCGI_OK)
2861 goto done;
2863 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2864 if (kerr != KCGI_OK)
2865 goto done;
2867 if (asprintf(&href_commits,
2868 "?path=%s&action=commits&commit=%s",
2869 gw_trans->repo_name, id_str) == -1) {
2870 error = got_error_from_errno("asprintf");
2871 goto done;
2873 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2874 KATTR_HREF, href_commits, KATTR__MAX);
2875 if (kerr != KCGI_OK)
2876 goto done;
2877 kerr = khtml_puts(gw_trans->gw_html_req,
2878 "commits");
2879 if (kerr != KCGI_OK)
2880 goto done;
2881 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2882 if (kerr != KCGI_OK)
2883 goto done;
2885 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2886 KATTR_ID, "dotted_line", KATTR__MAX);
2887 if (kerr != KCGI_OK)
2888 goto done;
2889 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2890 if (kerr != KCGI_OK)
2891 goto done;
2892 break;
2893 case TAGFULL:
2894 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2895 KATTR_ID, "tag_info_date_title", KATTR__MAX);
2896 if (kerr != KCGI_OK)
2897 goto done;
2898 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
2899 if (kerr != KCGI_OK)
2900 goto done;
2901 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2902 if (kerr != KCGI_OK)
2903 goto done;
2904 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2905 KATTR_ID, "tag_info_date", KATTR__MAX);
2906 if (kerr != KCGI_OK)
2907 goto done;
2908 error = gw_get_time_str(&age, tagger_time, TM_LONG);
2909 if (error)
2910 goto done;
2911 kerr = khtml_puts(gw_trans->gw_html_req,
2912 age ? age : "");
2913 if (kerr != KCGI_OK)
2914 goto done;
2915 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2916 if (kerr != KCGI_OK)
2917 goto done;
2919 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2920 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
2921 if (kerr != KCGI_OK)
2922 goto done;
2923 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
2924 if (kerr != KCGI_OK)
2925 goto done;
2926 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2927 if (kerr != KCGI_OK)
2928 goto done;
2929 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2930 KATTR_ID, "tag_info_date", KATTR__MAX);
2931 if (kerr != KCGI_OK)
2932 goto done;
2933 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
2934 if (kerr != KCGI_OK)
2935 goto done;
2936 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2937 if (kerr != KCGI_OK)
2938 goto done;
2940 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2941 KATTR_ID, "tag_info", KATTR__MAX);
2942 if (kerr != KCGI_OK)
2943 goto done;
2944 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
2945 if (kerr != KCGI_OK)
2946 goto done;
2947 break;
2948 default:
2949 break;
2951 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2952 if (kerr != KCGI_OK)
2953 goto done;
2955 if (limit && --limit == 0)
2956 break;
2958 if (tag)
2959 got_object_tag_close(tag);
2960 tag = NULL;
2961 free(id_str);
2962 id_str = NULL;
2963 free(age);
2964 age = NULL;
2965 free(tag_commit0);
2966 tag_commit0 = NULL;
2967 free(href_tag);
2968 href_tag = NULL;
2969 free(href_briefs);
2970 href_briefs = NULL;
2971 free(href_commits);
2972 href_commits = NULL;
2974 done:
2975 if (tag)
2976 got_object_tag_close(tag);
2977 free(id_str);
2978 free(age);
2979 free(tag_commit0);
2980 free(href_tag);
2981 free(href_briefs);
2982 free(href_commits);
2983 got_ref_list_free(&refs);
2984 if (error == NULL && kerr != KCGI_OK)
2985 error = gw_kcgi_error(kerr);
2986 return error;
2989 static void
2990 gw_free_header(struct gw_header *header)
2992 free(header->path);
2993 free(header->author);
2994 free(header->committer);
2995 free(header->refs_str);
2996 free(header->commit_id);
2997 free(header->parent_id);
2998 free(header->tree_id);
2999 free(header->commit_msg);
3002 static struct gw_header *
3003 gw_init_header()
3005 struct gw_header *header;
3007 header = malloc(sizeof(*header));
3008 if (header == NULL)
3009 return NULL;
3011 header->path = NULL;
3012 SIMPLEQ_INIT(&header->refs);
3014 header->refs_str = NULL;
3015 header->commit_id = NULL;
3016 header->committer = NULL;
3017 header->author = NULL;
3018 header->parent_id = NULL;
3019 header->tree_id = NULL;
3020 header->commit_msg = NULL;
3022 return header;
3025 static const struct got_error *
3026 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
3027 int limit, struct got_object_id *id)
3029 const struct got_error *error = NULL;
3030 struct got_commit_graph *graph = NULL;
3031 struct got_commit_object *commit = NULL;
3033 error = got_commit_graph_open(&graph, header->path, 0);
3034 if (error)
3035 return error;
3037 error = got_commit_graph_iter_start(graph, id, gw_trans->repo, NULL,
3038 NULL);
3039 if (error)
3040 goto done;
3042 for (;;) {
3043 error = got_commit_graph_iter_next(&id, graph, gw_trans->repo,
3044 NULL, NULL);
3045 if (error) {
3046 if (error->code == GOT_ERR_ITER_COMPLETED)
3047 error = NULL;
3048 goto done;
3050 if (id == NULL)
3051 goto done;
3053 error = got_object_open_as_commit(&commit, gw_trans->repo, id);
3054 if (error)
3055 goto done;
3056 if (limit == 1) {
3057 error = gw_get_commit(gw_trans, header, commit, id);
3058 if (error)
3059 goto done;
3060 } else {
3061 struct gw_header *n_header = NULL;
3062 if ((n_header = gw_init_header()) == NULL) {
3063 error = got_error_from_errno("malloc");
3064 goto done;
3066 error = got_ref_list(&n_header->refs, gw_trans->repo,
3067 NULL, got_ref_cmp_by_name, NULL);
3068 if (error)
3069 goto done;
3071 error = gw_get_commit(gw_trans, n_header, commit, id);
3072 if (error)
3073 goto done;
3074 got_ref_list_free(&n_header->refs);
3075 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3076 entry);
3078 if (error || (limit && --limit == 0))
3079 break;
3081 done:
3082 if (commit != NULL)
3083 got_object_commit_close(commit);
3084 if (graph)
3085 got_commit_graph_close(graph);
3086 return error;
3089 static const struct got_error *
3090 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header,
3091 struct got_commit_object *commit, struct got_object_id *id)
3093 const struct got_error *error = NULL;
3094 struct got_reflist_entry *re;
3095 struct got_object_id *id2 = NULL;
3096 struct got_object_qid *parent_id;
3097 char *commit_msg = NULL, *commit_msg0;
3099 /*print commit*/
3100 SIMPLEQ_FOREACH(re, &header->refs, entry) {
3101 char *s;
3102 const char *name;
3103 struct got_tag_object *tag = NULL;
3104 int cmp;
3106 if (got_ref_is_symbolic(re->ref))
3107 continue;
3109 name = got_ref_get_name(re->ref);
3110 if (strncmp(name, "refs/", 5) == 0)
3111 name += 5;
3112 if (strncmp(name, "got/", 4) == 0)
3113 continue;
3114 if (strncmp(name, "heads/", 6) == 0)
3115 name += 6;
3116 if (strncmp(name, "remotes/", 8) == 0)
3117 name += 8;
3118 if (strncmp(name, "tags/", 5) == 0) {
3119 error = got_object_open_as_tag(&tag, gw_trans->repo,
3120 re->id);
3121 if (error) {
3122 if (error->code != GOT_ERR_OBJ_TYPE)
3123 continue;
3125 * Ref points at something other
3126 * than a tag.
3128 error = NULL;
3129 tag = NULL;
3132 cmp = got_object_id_cmp(tag ?
3133 got_object_tag_get_object_id(tag) : re->id, id);
3134 if (tag)
3135 got_object_tag_close(tag);
3136 if (cmp != 0)
3137 continue;
3138 s = header->refs_str;
3139 if (asprintf(&header->refs_str, "%s%s%s", s ? s : "",
3140 s ? ", " : "", name) == -1) {
3141 error = got_error_from_errno("asprintf");
3142 free(s);
3143 header->refs_str = NULL;
3144 return error;
3146 free(s);
3149 error = got_object_id_str(&header->commit_id, id);
3150 if (error)
3151 return error;
3153 error = got_object_id_str(&header->tree_id,
3154 got_object_commit_get_tree_id(commit));
3155 if (error)
3156 return error;
3158 if (gw_trans->action == GW_DIFF) {
3159 parent_id = SIMPLEQ_FIRST(
3160 got_object_commit_get_parent_ids(commit));
3161 if (parent_id != NULL) {
3162 id2 = got_object_id_dup(parent_id->id);
3163 free (parent_id);
3164 error = got_object_id_str(&header->parent_id, id2);
3165 if (error)
3166 return error;
3167 free(id2);
3168 } else {
3169 header->parent_id = strdup("/dev/null");
3170 if (header->parent_id == NULL) {
3171 error = got_error_from_errno("strdup");
3172 return error;
3177 header->committer_time =
3178 got_object_commit_get_committer_time(commit);
3180 header->author =
3181 strdup(got_object_commit_get_author(commit));
3182 if (header->author == NULL) {
3183 error = got_error_from_errno("strdup");
3184 return error;
3186 header->committer =
3187 strdup(got_object_commit_get_committer(commit));
3188 if (header->committer == NULL) {
3189 error = got_error_from_errno("strdup");
3190 return error;
3192 error = got_object_commit_get_logmsg(&commit_msg0, commit);
3193 if (error)
3194 return error;
3196 commit_msg = commit_msg0;
3197 while (*commit_msg == '\n')
3198 commit_msg++;
3200 header->commit_msg = strdup(commit_msg);
3201 if (header->commit_msg == NULL)
3202 error = got_error_from_errno("strdup");
3203 free(commit_msg0);
3204 return error;
3207 static const struct got_error *
3208 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3210 const struct got_error *error = NULL;
3211 char *in_repo_path = NULL;
3212 struct got_object_id *id = NULL;
3214 error = got_repo_open(&gw_trans->repo, gw_trans->repo_path, NULL);
3215 if (error)
3216 return error;
3218 if (gw_trans->commit_id == NULL) {
3219 struct got_reference *head_ref;
3220 error = got_ref_open(&head_ref, gw_trans->repo,
3221 gw_trans->headref, 0);
3222 if (error)
3223 return error;
3225 error = got_ref_resolve(&id, gw_trans->repo, head_ref);
3226 got_ref_close(head_ref);
3227 if (error)
3228 return error;
3229 } else {
3230 struct got_reference *ref;
3231 error = got_ref_open(&ref, gw_trans->repo,
3232 gw_trans->commit_id, 0);
3233 if (error == NULL) {
3234 int obj_type;
3235 error = got_ref_resolve(&id, gw_trans->repo, ref);
3236 got_ref_close(ref);
3237 if (error)
3238 return error;
3239 error = got_object_get_type(&obj_type, gw_trans->repo,
3240 id);
3241 if (error)
3242 goto done;
3243 if (obj_type == GOT_OBJ_TYPE_TAG) {
3244 struct got_tag_object *tag;
3245 error = got_object_open_as_tag(&tag,
3246 gw_trans->repo, id);
3247 if (error)
3248 goto done;
3249 if (got_object_tag_get_object_type(tag) !=
3250 GOT_OBJ_TYPE_COMMIT) {
3251 got_object_tag_close(tag);
3252 error = got_error(GOT_ERR_OBJ_TYPE);
3253 goto done;
3255 free(id);
3256 id = got_object_id_dup(
3257 got_object_tag_get_object_id(tag));
3258 if (id == NULL)
3259 error = got_error_from_errno(
3260 "got_object_id_dup");
3261 got_object_tag_close(tag);
3262 if (error)
3263 goto done;
3264 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3265 error = got_error(GOT_ERR_OBJ_TYPE);
3266 goto done;
3269 error = got_repo_match_object_id_prefix(&id,
3270 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT,
3271 gw_trans->repo);
3272 if (error)
3273 goto done;
3276 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
3277 gw_trans->repo_path, 1);
3278 if (error)
3279 goto done;
3281 if (in_repo_path) {
3282 header->path = strdup(in_repo_path);
3283 if (header->path == NULL) {
3284 error = got_error_from_errno("strdup");
3285 goto done;
3289 error = got_ref_list(&header->refs, gw_trans->repo, NULL,
3290 got_ref_cmp_by_name, NULL);
3291 if (error)
3292 goto done;
3294 error = gw_get_commits(gw_trans, header, limit, id);
3295 done:
3296 got_ref_list_free(&header->refs);
3297 free(id);
3298 free(in_repo_path);
3299 return error;
3302 struct blame_line {
3303 int annotated;
3304 char *id_str;
3305 char *committer;
3306 char datebuf[11]; /* YYYY-MM-DD + NUL */
3309 struct gw_blame_cb_args {
3310 struct blame_line *lines;
3311 int nlines;
3312 int nlines_prec;
3313 int lineno_cur;
3314 off_t *line_offsets;
3315 FILE *f;
3316 struct got_repository *repo;
3317 struct gw_trans *gw_trans;
3320 static const struct got_error *
3321 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3323 const struct got_error *err = NULL;
3324 struct gw_blame_cb_args *a = arg;
3325 struct blame_line *bline;
3326 char *line = NULL;
3327 size_t linesize = 0;
3328 struct got_commit_object *commit = NULL;
3329 off_t offset;
3330 struct tm tm;
3331 time_t committer_time;
3332 enum kcgi_err kerr = KCGI_OK;
3334 if (nlines != a->nlines ||
3335 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3336 return got_error(GOT_ERR_RANGE);
3338 if (lineno == -1)
3339 return NULL; /* no change in this commit */
3341 /* Annotate this line. */
3342 bline = &a->lines[lineno - 1];
3343 if (bline->annotated)
3344 return NULL;
3345 err = got_object_id_str(&bline->id_str, id);
3346 if (err)
3347 return err;
3349 err = got_object_open_as_commit(&commit, a->repo, id);
3350 if (err)
3351 goto done;
3353 bline->committer = strdup(got_object_commit_get_committer(commit));
3354 if (bline->committer == NULL) {
3355 err = got_error_from_errno("strdup");
3356 goto done;
3359 committer_time = got_object_commit_get_committer_time(commit);
3360 if (localtime_r(&committer_time, &tm) == NULL)
3361 return got_error_from_errno("localtime_r");
3362 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3363 &tm) >= sizeof(bline->datebuf)) {
3364 err = got_error(GOT_ERR_NO_SPACE);
3365 goto done;
3367 bline->annotated = 1;
3369 /* Print lines annotated so far. */
3370 bline = &a->lines[a->lineno_cur - 1];
3371 if (!bline->annotated)
3372 goto done;
3374 offset = a->line_offsets[a->lineno_cur - 1];
3375 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3376 err = got_error_from_errno("fseeko");
3377 goto done;
3380 while (bline->annotated) {
3381 char *smallerthan, *at, *nl, *committer;
3382 char *lineno = NULL, *href_diff = NULL, *href_link = NULL;
3383 size_t len;
3385 if (getline(&line, &linesize, a->f) == -1) {
3386 if (ferror(a->f))
3387 err = got_error_from_errno("getline");
3388 break;
3391 committer = bline->committer;
3392 smallerthan = strchr(committer, '<');
3393 if (smallerthan && smallerthan[1] != '\0')
3394 committer = smallerthan + 1;
3395 at = strchr(committer, '@');
3396 if (at)
3397 *at = '\0';
3398 len = strlen(committer);
3399 if (len >= 9)
3400 committer[8] = '\0';
3402 nl = strchr(line, '\n');
3403 if (nl)
3404 *nl = '\0';
3406 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3407 "blame_wrapper", KATTR__MAX);
3408 if (kerr != KCGI_OK)
3409 goto err;
3410 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3411 "blame_number", KATTR__MAX);
3412 if (kerr != KCGI_OK)
3413 goto err;
3414 if (asprintf(&lineno, "%.*d", a->nlines_prec,
3415 a->lineno_cur) == -1)
3416 goto err;
3417 kerr = khtml_puts(a->gw_trans->gw_html_req, lineno);
3418 if (kerr != KCGI_OK)
3419 goto err;
3420 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3421 if (kerr != KCGI_OK)
3422 goto err;
3424 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3425 "blame_hash", KATTR__MAX);
3426 if (kerr != KCGI_OK)
3427 goto err;
3429 if (asprintf(&href_diff,
3430 "?path=%s&action=diff&commit=%s",
3431 a->gw_trans->repo_name, bline->id_str) == -1) {
3432 err = got_error_from_errno("asprintf");
3433 goto err;
3435 if (asprintf(&href_link, "%.8s", bline->id_str) == -1) {
3436 err = got_error_from_errno("asprintf");
3437 goto err;
3439 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3440 KATTR_HREF, href_diff, KATTR__MAX);
3441 if (kerr != KCGI_OK)
3442 goto done;
3443 kerr = khtml_puts(a->gw_trans->gw_html_req, href_link);
3444 if (kerr != KCGI_OK)
3445 goto err;
3446 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3447 if (kerr != KCGI_OK)
3448 goto err;
3450 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3451 "blame_date", KATTR__MAX);
3452 if (kerr != KCGI_OK)
3453 goto err;
3454 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3455 if (kerr != KCGI_OK)
3456 goto err;
3457 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3458 if (kerr != KCGI_OK)
3459 goto err;
3461 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3462 "blame_author", KATTR__MAX);
3463 if (kerr != KCGI_OK)
3464 goto err;
3465 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
3466 if (kerr != KCGI_OK)
3467 goto err;
3468 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3469 if (kerr != KCGI_OK)
3470 goto err;
3472 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3473 "blame_code", KATTR__MAX);
3474 if (kerr != KCGI_OK)
3475 goto err;
3476 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
3477 if (kerr != KCGI_OK)
3478 goto err;
3479 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3480 if (kerr != KCGI_OK)
3481 goto err;
3483 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3484 if (kerr != KCGI_OK)
3485 goto err;
3487 a->lineno_cur++;
3488 bline = &a->lines[a->lineno_cur - 1];
3489 err:
3490 free(lineno);
3491 free(href_diff);
3492 free(href_link);
3494 done:
3495 if (commit)
3496 got_object_commit_close(commit);
3497 free(line);
3498 if (err == NULL && kerr != KCGI_OK)
3499 err = gw_kcgi_error(kerr);
3500 return err;
3503 static const struct got_error *
3504 gw_output_file_blame(struct gw_trans *gw_trans)
3506 const struct got_error *error = NULL;
3507 struct got_object_id *obj_id = NULL;
3508 struct got_object_id *commit_id = NULL;
3509 struct got_blob_object *blob = NULL;
3510 char *path = NULL, *in_repo_path = NULL;
3511 struct gw_blame_cb_args bca;
3512 int i, obj_type;
3513 size_t filesize;
3515 if (asprintf(&path, "%s%s%s",
3516 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3517 gw_trans->repo_folder ? "/" : "",
3518 gw_trans->repo_file) == -1) {
3519 error = got_error_from_errno("asprintf");
3520 goto done;
3523 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path, 1);
3524 if (error)
3525 goto done;
3527 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
3528 GOT_OBJ_TYPE_COMMIT, 1, gw_trans->repo);
3529 if (error)
3530 goto done;
3532 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
3533 in_repo_path);
3534 if (error)
3535 goto done;
3537 if (obj_id == NULL) {
3538 error = got_error(GOT_ERR_NO_OBJ);
3539 goto done;
3542 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
3543 if (error)
3544 goto done;
3546 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3547 error = got_error(GOT_ERR_OBJ_TYPE);
3548 goto done;
3551 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
3552 if (error)
3553 goto done;
3555 bca.f = got_opentemp();
3556 if (bca.f == NULL) {
3557 error = got_error_from_errno("got_opentemp");
3558 goto done;
3560 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3561 &bca.line_offsets, bca.f, blob);
3562 if (error || bca.nlines == 0)
3563 goto done;
3565 /* Don't include \n at EOF in the blame line count. */
3566 if (bca.line_offsets[bca.nlines - 1] == filesize)
3567 bca.nlines--;
3569 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3570 if (bca.lines == NULL) {
3571 error = got_error_from_errno("calloc");
3572 goto done;
3574 bca.lineno_cur = 1;
3575 bca.nlines_prec = 0;
3576 i = bca.nlines;
3577 while (i > 0) {
3578 i /= 10;
3579 bca.nlines_prec++;
3581 bca.repo = gw_trans->repo;
3582 bca.gw_trans = gw_trans;
3584 error = got_blame(in_repo_path, commit_id, gw_trans->repo, gw_blame_cb,
3585 &bca, NULL, NULL);
3586 done:
3587 free(in_repo_path);
3588 free(commit_id);
3589 free(obj_id);
3590 free(path);
3592 if (blob) {
3593 free(bca.line_offsets);
3594 for (i = 0; i < bca.nlines; i++) {
3595 struct blame_line *bline = &bca.lines[i];
3596 free(bline->id_str);
3597 free(bline->committer);
3599 free(bca.lines);
3600 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3601 error = got_error_from_errno("fclose");
3603 if (blob)
3604 got_object_blob_close(blob);
3605 return error;
3608 static const struct got_error *
3609 gw_output_blob_buf(struct gw_trans *gw_trans)
3611 const struct got_error *error = NULL;
3612 struct got_object_id *obj_id = NULL;
3613 struct got_object_id *commit_id = NULL;
3614 struct got_blob_object *blob = NULL;
3615 char *path = NULL, *in_repo_path = NULL;
3616 int obj_type, set_mime = 0;
3617 size_t len, hdrlen;
3618 const uint8_t *buf;
3619 enum kcgi_err kerr = KCGI_OK;
3621 if (asprintf(&path, "%s%s%s",
3622 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3623 gw_trans->repo_folder ? "/" : "",
3624 gw_trans->repo_file) == -1) {
3625 error = got_error_from_errno("asprintf");
3626 goto done;
3629 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path, 1);
3630 if (error)
3631 goto done;
3633 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
3634 GOT_OBJ_TYPE_COMMIT, 1, gw_trans->repo);
3635 if (error)
3636 goto done;
3638 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
3639 in_repo_path);
3640 if (error)
3641 goto done;
3643 if (obj_id == NULL) {
3644 error = got_error(GOT_ERR_NO_OBJ);
3645 goto done;
3648 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
3649 if (error)
3650 goto done;
3652 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3653 error = got_error(GOT_ERR_OBJ_TYPE);
3654 goto done;
3657 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
3658 if (error)
3659 goto done;
3661 hdrlen = got_object_blob_get_hdrlen(blob);
3662 do {
3663 error = got_object_blob_read_block(&len, blob);
3664 if (error)
3665 goto done;
3666 buf = got_object_blob_get_read_buf(blob);
3669 * Skip blob object header first time around,
3670 * which also contains a zero byte.
3672 buf += hdrlen;
3673 if (set_mime == 0) {
3674 if (isbinary(buf, len - hdrlen))
3675 gw_trans->mime = KMIME_APP_OCTET_STREAM;
3676 else
3677 gw_trans->mime = KMIME_TEXT_PLAIN;
3678 set_mime = 1;
3679 error = gw_display_index(gw_trans);
3680 if (error)
3681 goto done;
3683 kerr = khttp_write(gw_trans->gw_req, buf, len - hdrlen);
3684 if (kerr != KCGI_OK)
3685 goto done;
3686 hdrlen = 0;
3687 } while (len != 0);
3688 done:
3689 free(in_repo_path);
3690 free(commit_id);
3691 free(obj_id);
3692 free(path);
3693 if (blob)
3694 got_object_blob_close(blob);
3695 if (error == NULL && kerr != KCGI_OK)
3696 error = gw_kcgi_error(kerr);
3697 return error;
3700 static const struct got_error *
3701 gw_output_repo_tree(struct gw_trans *gw_trans)
3703 const struct got_error *error = NULL;
3704 struct got_object_id *tree_id = NULL, *commit_id = NULL;
3705 struct got_tree_object *tree = NULL;
3706 char *path = NULL, *in_repo_path = NULL;
3707 char *id_str = NULL;
3708 char *build_folder = NULL;
3709 char *href_blob = NULL, *href_blame = NULL;
3710 const char *class = NULL;
3711 int nentries, i, class_flip = 0;
3712 enum kcgi_err kerr = KCGI_OK;
3714 if (gw_trans->repo_folder != NULL) {
3715 path = strdup(gw_trans->repo_folder);
3716 if (path == NULL) {
3717 error = got_error_from_errno("strdup");
3718 goto done;
3720 } else {
3721 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
3722 gw_trans->repo_path, 1);
3723 if (error)
3724 goto done;
3725 free(path);
3726 path = in_repo_path;
3729 if (gw_trans->commit_id == NULL) {
3730 struct got_reference *head_ref;
3731 error = got_ref_open(&head_ref, gw_trans->repo,
3732 gw_trans->headref, 0);
3733 if (error)
3734 goto done;
3735 error = got_ref_resolve(&commit_id, gw_trans->repo, head_ref);
3736 if (error)
3737 goto done;
3738 got_ref_close(head_ref);
3740 * gw_trans->commit_id was not parsed from the querystring
3741 * we hit this code path from gw_index, where we don't know the
3742 * commit values for the tree link yet, so set
3743 * gw_trans->commit_id here to continue further into the tree
3745 error = got_object_id_str(&gw_trans->commit_id, commit_id);
3746 if (error)
3747 goto done;
3749 } else {
3750 error = got_repo_match_object_id(&commit_id, NULL,
3751 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT, 1,
3752 gw_trans->repo);
3753 if (error)
3754 goto done;
3757 error = got_object_id_by_path(&tree_id, gw_trans->repo, commit_id,
3758 path);
3759 if (error)
3760 goto done;
3762 error = got_object_open_as_tree(&tree, gw_trans->repo, tree_id);
3763 if (error)
3764 goto done;
3766 nentries = got_object_tree_get_nentries(tree);
3767 for (i = 0; i < nentries; i++) {
3768 struct got_tree_entry *te;
3769 const char *modestr = "";
3770 mode_t mode;
3772 te = got_object_tree_get_entry(tree, i);
3774 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
3775 if (error)
3776 goto done;
3778 mode = got_tree_entry_get_mode(te);
3779 if (got_object_tree_entry_is_submodule(te))
3780 modestr = "$";
3781 else if (S_ISLNK(mode))
3782 modestr = "@";
3783 else if (S_ISDIR(mode))
3784 modestr = "/";
3785 else if (mode & S_IXUSR)
3786 modestr = "*";
3788 if (class_flip == 0) {
3789 class = "back_lightgray";
3790 class_flip = 1;
3791 } else {
3792 class = "back_white";
3793 class_flip = 0;
3796 if (S_ISDIR(mode)) {
3797 if (asprintf(&build_folder, "%s/%s",
3798 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3799 got_tree_entry_get_name(te)) == -1) {
3800 error = got_error_from_errno("asprintf");
3801 goto done;
3803 if (asprintf(&href_blob,
3804 "?path=%s&action=%s&commit=%s&folder=%s",
3805 gw_trans->repo_name, gw_get_action_name(gw_trans),
3806 gw_trans->commit_id, build_folder) == -1) {
3807 error = got_error_from_errno("asprintf");
3808 goto done;
3811 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3812 KATTR_ID, "tree_wrapper", KATTR__MAX);
3813 if (kerr != KCGI_OK)
3814 goto done;
3815 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3816 KATTR_ID, "tree_line", KATTR_CLASS, class,
3817 KATTR__MAX);
3818 if (kerr != KCGI_OK)
3819 goto done;
3820 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3821 KATTR_HREF, href_blob, KATTR_CLASS,
3822 "diff_directory", KATTR__MAX);
3823 if (kerr != KCGI_OK)
3824 goto done;
3825 kerr = khtml_puts(gw_trans->gw_html_req,
3826 got_tree_entry_get_name(te));
3827 if (kerr != KCGI_OK)
3828 goto done;
3829 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3830 if (kerr != KCGI_OK)
3831 goto done;
3832 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3833 if (kerr != KCGI_OK)
3834 goto done;
3835 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3836 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
3837 KATTR__MAX);
3838 if (kerr != KCGI_OK)
3839 goto done;
3840 kerr = khtml_entity(gw_trans->gw_html_req,
3841 KENTITY_nbsp);
3842 if (kerr != KCGI_OK)
3843 goto done;
3844 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3845 if (kerr != KCGI_OK)
3846 goto done;
3847 } else {
3848 if (asprintf(&href_blob,
3849 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3850 gw_trans->repo_name, "blob", gw_trans->commit_id,
3851 got_tree_entry_get_name(te),
3852 gw_trans->repo_folder ?
3853 gw_trans->repo_folder : "") == -1) {
3854 error = got_error_from_errno("asprintf");
3855 goto done;
3857 if (asprintf(&href_blame,
3858 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3859 gw_trans->repo_name, "blame", gw_trans->commit_id,
3860 got_tree_entry_get_name(te),
3861 gw_trans->repo_folder ?
3862 gw_trans->repo_folder : "") == -1) {
3863 error = got_error_from_errno("asprintf");
3864 goto done;
3867 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3868 KATTR_ID, "tree_wrapper", KATTR__MAX);
3869 if (kerr != KCGI_OK)
3870 goto done;
3871 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3872 KATTR_ID, "tree_line", KATTR_CLASS, class,
3873 KATTR__MAX);
3874 if (kerr != KCGI_OK)
3875 goto done;
3876 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3877 KATTR_HREF, href_blob, KATTR__MAX);
3878 if (kerr != KCGI_OK)
3879 goto done;
3880 kerr = khtml_puts(gw_trans->gw_html_req,
3881 got_tree_entry_get_name(te));
3882 if (kerr != KCGI_OK)
3883 goto done;
3884 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3885 if (kerr != KCGI_OK)
3886 goto done;
3887 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3888 if (kerr != KCGI_OK)
3889 goto done;
3890 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3891 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
3892 KATTR__MAX);
3893 if (kerr != KCGI_OK)
3894 goto done;
3896 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3897 KATTR_HREF, href_blob, KATTR__MAX);
3898 if (kerr != KCGI_OK)
3899 goto done;
3900 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
3901 if (kerr != KCGI_OK)
3902 goto done;
3903 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3904 if (kerr != KCGI_OK)
3905 goto done;
3907 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3908 if (kerr != KCGI_OK)
3909 goto done;
3911 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3912 KATTR_HREF, href_blame, KATTR__MAX);
3913 if (kerr != KCGI_OK)
3914 goto done;
3915 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
3916 if (kerr != KCGI_OK)
3917 goto done;
3919 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3920 if (kerr != KCGI_OK)
3921 goto done;
3923 free(id_str);
3924 id_str = NULL;
3925 free(href_blob);
3926 href_blob = NULL;
3927 free(build_folder);
3928 build_folder = NULL;
3930 done:
3931 if (tree)
3932 got_object_tree_close(tree);
3933 free(id_str);
3934 free(href_blob);
3935 free(href_blame);
3936 free(in_repo_path);
3937 free(tree_id);
3938 free(build_folder);
3939 if (error == NULL && kerr != KCGI_OK)
3940 error = gw_kcgi_error(kerr);
3941 return error;
3944 static const struct got_error *
3945 gw_output_repo_heads(struct gw_trans *gw_trans)
3947 const struct got_error *error = NULL;
3948 struct got_reflist_head refs;
3949 struct got_reflist_entry *re;
3950 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
3951 char *href_commits = NULL;
3952 enum kcgi_err kerr = KCGI_OK;
3954 SIMPLEQ_INIT(&refs);
3956 error = got_ref_list(&refs, gw_trans->repo, "refs/heads",
3957 got_ref_cmp_by_name, NULL);
3958 if (error)
3959 goto done;
3961 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3962 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
3963 if (kerr != KCGI_OK)
3964 goto done;
3965 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3966 KATTR_ID, "summary_heads_title", KATTR__MAX);
3967 if (kerr != KCGI_OK)
3968 goto done;
3969 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
3970 if (kerr != KCGI_OK)
3971 goto done;
3972 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3973 if (kerr != KCGI_OK)
3974 goto done;
3975 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3976 KATTR_ID, "summary_heads_content", KATTR__MAX);
3977 if (kerr != KCGI_OK)
3978 goto done;
3980 SIMPLEQ_FOREACH(re, &refs, entry) {
3981 const char *refname;
3983 if (got_ref_is_symbolic(re->ref))
3984 continue;
3986 refname = got_ref_get_name(re->ref);
3987 if (strncmp(refname, "refs/heads/", 11) != 0)
3988 continue;
3990 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
3991 refname, TM_DIFF);
3992 if (error)
3993 goto done;
3995 if (strncmp(refname, "refs/heads/", 11) == 0)
3996 refname += 11;
3998 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3999 KATTR_ID, "heads_wrapper", KATTR__MAX);
4000 if (kerr != KCGI_OK)
4001 goto done;
4002 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4003 KATTR_ID, "heads_age", KATTR__MAX);
4004 if (kerr != KCGI_OK)
4005 goto done;
4006 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
4007 if (kerr != KCGI_OK)
4008 goto done;
4009 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4010 if (kerr != KCGI_OK)
4011 goto done;
4012 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4013 KATTR_ID, "heads_space", KATTR__MAX);
4014 if (kerr != KCGI_OK)
4015 goto done;
4016 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
4017 if (kerr != KCGI_OK)
4018 goto done;
4019 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4020 if (kerr != KCGI_OK)
4021 goto done;
4022 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4023 KATTR_ID, "head", KATTR__MAX);
4024 if (kerr != KCGI_OK)
4025 goto done;
4026 if (asprintf(&href_summary,
4027 "?path=%s&action=summary&headref=%s",
4028 gw_trans->repo_name, refname) == -1) {
4029 error = got_error_from_errno("asprintf");
4030 goto done;
4032 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4033 href_summary, KATTR__MAX);
4034 kerr = khtml_puts(gw_trans->gw_html_req, refname);
4035 if (kerr != KCGI_OK)
4036 goto done;
4037 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4038 if (kerr != KCGI_OK)
4039 goto done;
4041 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4042 "navs_wrapper", KATTR__MAX);
4043 if (kerr != KCGI_OK)
4044 goto done;
4045 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4046 "navs", KATTR__MAX);
4047 if (kerr != KCGI_OK)
4048 goto done;
4050 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4051 href_summary, KATTR__MAX);
4052 if (kerr != KCGI_OK)
4053 goto done;
4054 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4055 if (kerr != KCGI_OK)
4056 goto done;
4057 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4058 if (kerr != KCGI_OK)
4059 goto done;
4061 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4062 if (kerr != KCGI_OK)
4063 goto done;
4064 if (asprintf(&href_briefs, "?path=%s&action=briefs&headref=%s",
4065 gw_trans->repo_name, refname) == -1) {
4066 error = got_error_from_errno("asprintf");
4067 goto done;
4069 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4070 href_briefs, KATTR__MAX);
4071 if (kerr != KCGI_OK)
4072 goto done;
4073 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4074 if (kerr != KCGI_OK)
4075 goto done;
4076 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4077 if (kerr != KCGI_OK)
4078 goto done;
4080 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4081 if (kerr != KCGI_OK)
4082 goto done;
4084 if (asprintf(&href_commits,
4085 "?path=%s&action=commits&headref=%s",
4086 gw_trans->repo_name, refname) == -1) {
4087 error = got_error_from_errno("asprintf");
4088 goto done;
4090 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4091 href_commits, KATTR__MAX);
4092 if (kerr != KCGI_OK)
4093 goto done;
4094 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4095 if (kerr != KCGI_OK)
4096 goto done;
4097 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4098 if (kerr != KCGI_OK)
4099 goto done;
4101 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4102 "dotted_line", KATTR__MAX);
4103 if (kerr != KCGI_OK)
4104 goto done;
4105 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4106 if (kerr != KCGI_OK)
4107 goto done;
4108 free(href_summary);
4109 href_summary = NULL;
4110 free(href_briefs);
4111 href_briefs = NULL;
4112 free(href_commits);
4113 href_commits = NULL;
4115 done:
4116 got_ref_list_free(&refs);
4117 free(href_summary);
4118 free(href_briefs);
4119 free(href_commits);
4120 return error;
4123 static const struct got_error *
4124 gw_output_site_link(struct gw_trans *gw_trans)
4126 const struct got_error *error = NULL;
4127 char *href_summary = NULL;
4128 enum kcgi_err kerr = KCGI_OK;
4130 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4131 "site_link", KATTR__MAX);
4132 if (kerr != KCGI_OK)
4133 goto done;
4134 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4135 KATTR__MAX);
4136 if (kerr != KCGI_OK)
4137 goto done;
4138 kerr = khtml_puts(gw_trans->gw_html_req,
4139 gw_trans->gw_conf->got_site_link);
4140 if (kerr != KCGI_OK)
4141 goto done;
4142 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4143 if (kerr != KCGI_OK)
4144 goto done;
4146 if (gw_trans->repo_name != NULL) {
4147 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4148 if (kerr != KCGI_OK)
4149 goto done;
4150 if (asprintf(&href_summary, "?path=%s&action=summary",
4151 gw_trans->repo_name) == -1)
4152 goto done;
4153 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4154 href_summary, KATTR__MAX);
4155 if (kerr != KCGI_OK)
4156 goto done;
4157 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4158 if (kerr != KCGI_OK)
4159 goto done;
4160 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4161 if (kerr != KCGI_OK)
4162 goto done;
4163 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4164 if (kerr != KCGI_OK)
4165 goto done;
4166 kerr = khtml_puts(gw_trans->gw_html_req,
4167 gw_get_action_name(gw_trans));
4168 if (kerr != KCGI_OK)
4169 goto done;
4172 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4173 if (kerr != KCGI_OK)
4174 goto done;
4175 done:
4176 free(href_summary);
4177 return error;
4180 static const struct got_error *
4181 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4183 const struct got_error *error = NULL;
4184 char *color = NULL;
4185 enum kcgi_err kerr = KCGI_OK;
4187 if (strncmp(buf, "-", 1) == 0)
4188 color = "diff_minus";
4189 else if (strncmp(buf, "+", 1) == 0)
4190 color = "diff_plus";
4191 else if (strncmp(buf, "@@", 2) == 0)
4192 color = "diff_chunk_header";
4193 else if (strncmp(buf, "@@", 2) == 0)
4194 color = "diff_chunk_header";
4195 else if (strncmp(buf, "commit +", 8) == 0)
4196 color = "diff_meta";
4197 else if (strncmp(buf, "commit -", 8) == 0)
4198 color = "diff_meta";
4199 else if (strncmp(buf, "blob +", 6) == 0)
4200 color = "diff_meta";
4201 else if (strncmp(buf, "blob -", 6) == 0)
4202 color = "diff_meta";
4203 else if (strncmp(buf, "file +", 6) == 0)
4204 color = "diff_meta";
4205 else if (strncmp(buf, "file -", 6) == 0)
4206 color = "diff_meta";
4207 else if (strncmp(buf, "from:", 5) == 0)
4208 color = "diff_author";
4209 else if (strncmp(buf, "via:", 4) == 0)
4210 color = "diff_author";
4211 else if (strncmp(buf, "date:", 5) == 0)
4212 color = "diff_date";
4213 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4214 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4215 if (error == NULL && kerr != KCGI_OK)
4216 error = gw_kcgi_error(kerr);
4217 return error;
4220 int
4221 main(int argc, char *argv[])
4223 const struct got_error *error = NULL;
4224 struct gw_trans *gw_trans;
4225 struct gw_dir *dir = NULL, *tdir;
4226 const char *page = "index";
4227 int gw_malloc = 1;
4228 enum kcgi_err kerr;
4230 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4231 errx(1, "malloc");
4233 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4234 errx(1, "malloc");
4236 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4237 errx(1, "malloc");
4239 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4240 errx(1, "malloc");
4242 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4243 if (kerr != KCGI_OK) {
4244 error = gw_kcgi_error(kerr);
4245 goto done;
4248 if ((gw_trans->gw_conf =
4249 malloc(sizeof(struct gotweb_conf))) == NULL) {
4250 gw_malloc = 0;
4251 error = got_error_from_errno("malloc");
4252 goto done;
4255 TAILQ_INIT(&gw_trans->gw_dirs);
4256 TAILQ_INIT(&gw_trans->gw_headers);
4258 gw_trans->action = -1;
4259 gw_trans->page = 0;
4260 gw_trans->repos_total = 0;
4261 gw_trans->repo_path = NULL;
4262 gw_trans->commit_id = NULL;
4263 gw_trans->headref = GOT_REF_HEAD;
4264 gw_trans->mime = KMIME_TEXT_HTML;
4265 gw_trans->gw_tmpl->key = gw_templs;
4266 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4267 gw_trans->gw_tmpl->arg = gw_trans;
4268 gw_trans->gw_tmpl->cb = gw_template;
4269 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
4270 if (error)
4271 goto done;
4273 error = gw_parse_querystring(gw_trans);
4274 if (error)
4275 goto done;
4277 if (gw_trans->action == GW_BLOB)
4278 error = gw_blob(gw_trans);
4279 else
4280 error = gw_display_index(gw_trans);
4281 done:
4282 if (gw_malloc) {
4283 free(gw_trans->gw_conf->got_repos_path);
4284 free(gw_trans->gw_conf->got_site_name);
4285 free(gw_trans->gw_conf->got_site_owner);
4286 free(gw_trans->gw_conf->got_site_link);
4287 free(gw_trans->gw_conf->got_logo);
4288 free(gw_trans->gw_conf->got_logo_url);
4289 free(gw_trans->gw_conf);
4290 free(gw_trans->commit_id);
4291 free(gw_trans->repo_path);
4292 if (gw_trans->repo)
4293 got_repo_close(gw_trans->repo);
4295 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4296 free(dir->name);
4297 free(dir->description);
4298 free(dir->age);
4299 free(dir->url);
4300 free(dir->path);
4301 free(dir);
4306 khttp_free(gw_trans->gw_req);
4307 return 0;