Blob


1 /*
2 * Copyright (c) 2019, 2020 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/queue.h>
19 #include <sys/stat.h>
20 #include <sys/types.h>
22 #include <ctype.h>
23 #include <dirent.h>
24 #include <err.h>
25 #include <errno.h>
26 #include <regex.h>
27 #include <stdarg.h>
28 #include <stdint.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
34 #include <got_object.h>
35 #include <got_reference.h>
36 #include <got_repository.h>
37 #include <got_path.h>
38 #include <got_cancel.h>
39 #include <got_worktree.h>
40 #include <got_diff.h>
41 #include <got_commit_graph.h>
42 #include <got_blame.h>
43 #include <got_privsep.h>
44 #include <got_opentemp.h>
46 #include <kcgi.h>
47 #include <kcgihtml.h>
49 #include "gotweb.h"
51 #ifndef nitems
52 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
53 #endif
55 struct gw_trans {
56 TAILQ_HEAD(headers, gw_header) gw_headers;
57 TAILQ_HEAD(dirs, gw_dir) gw_dirs;
58 struct got_repository *repo;
59 struct gw_dir *gw_dir;
60 struct gotweb_conf *gw_conf;
61 struct ktemplate *gw_tmpl;
62 struct khtmlreq *gw_html_req;
63 struct kreq *gw_req;
64 const struct got_error *error;
65 const char *repo_name;
66 char *repo_path;
67 char *commit_id;
68 char *next_id;
69 char *next_prev_id;
70 char *prev_id;
71 char *prev_prev_id;
72 const char *repo_file;
73 char *repo_folder;
74 const char *headref;
75 unsigned int action;
76 unsigned int page;
77 unsigned int repos_total;
78 enum kmime mime;
79 };
81 struct gw_header {
82 TAILQ_ENTRY(gw_header) entry;
83 struct got_reflist_head refs;
84 char *path;
86 char *refs_str;
87 char *commit_id; /* id_str1 */
88 char *parent_id; /* id_str2 */
89 char *tree_id;
90 char *author;
91 char *committer;
92 char *commit_msg;
93 time_t committer_time;
94 };
96 struct gw_dir {
97 TAILQ_ENTRY(gw_dir) entry;
98 char *name;
99 char *owner;
100 char *description;
101 char *url;
102 char *age;
103 char *path;
104 };
106 enum gw_key {
107 KEY_ACTION,
108 KEY_COMMIT_ID,
109 KEY_FILE,
110 KEY_FOLDER,
111 KEY_HEADREF,
112 KEY_PAGE,
113 KEY_PATH,
114 KEY_PREV_ID,
115 KEY_PREV_PREV_ID,
116 KEY__ZMAX
117 };
119 enum gw_tmpl {
120 TEMPL_CONTENT,
121 TEMPL_HEAD,
122 TEMPL_HEADER,
123 TEMPL_SEARCH,
124 TEMPL_SITEPATH,
125 TEMPL_SITEOWNER,
126 TEMPL_TITLE,
127 TEMPL__MAX
128 };
130 enum gw_ref_tm {
131 TM_DIFF,
132 TM_LONG,
133 };
135 enum gw_tags_type {
136 TAGBRIEF,
137 TAGFULL,
138 };
140 static const char *const gw_templs[TEMPL__MAX] = {
141 "content",
142 "head",
143 "header",
144 "search",
145 "sitepath",
146 "siteowner",
147 "title",
148 };
150 static const struct kvalid gw_keys[KEY__ZMAX] = {
151 { kvalid_stringne, "action" },
152 { kvalid_stringne, "commit" },
153 { kvalid_stringne, "file" },
154 { kvalid_stringne, "folder" },
155 { kvalid_stringne, "headref" },
156 { kvalid_int, "page" },
157 { kvalid_stringne, "path" },
158 { kvalid_stringne, "prev" },
159 { kvalid_stringne, "prev_prev" },
160 };
162 static struct gw_header *gw_init_header(void);
164 static void gw_free_header(struct gw_header *);
166 static int gw_template(size_t, void *);
168 static const struct got_error *gw_error(struct gw_trans *);
169 static const struct got_error *gw_init_gw_dir(struct gw_dir **, const char *);
170 static const struct got_error *gw_get_repo_description(char **,
171 struct gw_trans *, char *);
172 static const struct got_error *gw_get_repo_owner(char **, struct gw_trans *,
173 char *);
174 static const struct got_error *gw_get_time_str(char **, time_t, int);
175 static const struct got_error *gw_get_repo_age(char **, struct gw_trans *,
176 char *, const char *, int);
177 static const struct got_error *gw_output_file_blame(struct gw_trans *);
178 static const struct got_error *gw_output_blob_buf(struct gw_trans *);
179 static const struct got_error *gw_output_repo_tree(struct gw_trans *);
180 static const struct got_error *gw_output_diff(struct gw_trans *,
181 struct gw_header *);
182 static const struct got_error *gw_output_repo_tags(struct gw_trans *,
183 struct gw_header *, int, int);
184 static const struct got_error *gw_output_repo_heads(struct gw_trans *);
185 static const struct got_error *gw_output_site_link(struct gw_trans *);
186 static const struct got_error *gw_get_clone_url(char **, struct gw_trans *,
187 char *);
188 static const struct got_error *gw_colordiff_line(struct gw_trans *, char *);
190 static const struct got_error *gw_gen_commit_header(struct gw_trans *, char *,
191 char*);
192 static const struct got_error *gw_gen_diff_header(struct gw_trans *, char *,
193 char*);
194 static const struct got_error *gw_gen_author_header(struct gw_trans *,
195 const char *);
196 static const struct got_error *gw_gen_age_header(struct gw_trans *,
197 const char *);
198 static const struct got_error *gw_gen_committer_header(struct gw_trans *,
199 const char *);
200 static const struct got_error *gw_gen_commit_msg_header(struct gw_trans*,
201 char *);
202 static const struct got_error *gw_gen_tree_header(struct gw_trans *, char *);
203 static const struct got_error *gw_display_open(struct gw_trans *, enum khttp,
204 enum kmime);
205 static const struct got_error *gw_display_index(struct gw_trans *);
206 static const struct got_error *gw_get_header(struct gw_trans *,
207 struct gw_header *, int);
208 static const struct got_error *gw_get_commits(struct gw_trans *,
209 struct gw_header *, int,
210 struct got_object_id *);
211 static const struct got_error *gw_get_commit(struct gw_trans *,
212 struct gw_header *,
213 struct got_commit_object *,
214 struct got_object_id *);
215 static const struct got_error *gw_apply_unveil(const char *);
216 static const struct got_error *gw_blame_cb(void *, int, int,
217 struct got_object_id *);
218 static const struct got_error *gw_load_got_paths(struct gw_trans *);
219 static const struct got_error *gw_load_got_path(struct gw_trans *,
220 struct gw_dir *);
221 static const struct got_error *gw_parse_querystring(struct gw_trans *);
222 static const struct got_error *gw_blame(struct gw_trans *);
223 static const struct got_error *gw_blob(struct gw_trans *);
224 static const struct got_error *gw_diff(struct gw_trans *);
225 static const struct got_error *gw_index(struct gw_trans *);
226 static const struct got_error *gw_commits(struct gw_trans *);
227 static const struct got_error *gw_briefs(struct gw_trans *);
228 static const struct got_error *gw_summary(struct gw_trans *);
229 static const struct got_error *gw_tree(struct gw_trans *);
230 static const struct got_error *gw_tag(struct gw_trans *);
231 static const struct got_error *gw_tags(struct gw_trans *);
233 struct gw_query_action {
234 unsigned int func_id;
235 const char *func_name;
236 const struct got_error *(*func_main)(struct gw_trans *);
237 char *template;
238 };
240 enum gw_query_actions {
241 GW_BLAME,
242 GW_BLOB,
243 GW_BRIEFS,
244 GW_COMMITS,
245 GW_DIFF,
246 GW_ERR,
247 GW_INDEX,
248 GW_SUMMARY,
249 GW_TAG,
250 GW_TAGS,
251 GW_TREE,
252 };
254 static struct gw_query_action gw_query_funcs[] = {
255 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
256 { GW_BLOB, "blob", NULL, NULL },
257 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
258 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
259 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
260 { GW_ERR, "error", gw_error, "gw_tmpl/err.tmpl" },
261 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
262 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
263 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
264 { GW_TAGS, "tags", gw_tags, "gw_tmpl/tags.tmpl" },
265 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
266 };
268 static const char *
269 gw_get_action_name(struct gw_trans *gw_trans)
271 return gw_query_funcs[gw_trans->action].func_name;
274 static const struct got_error *
275 gw_kcgi_error(enum kcgi_err kerr)
277 if (kerr == KCGI_OK)
278 return NULL;
280 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
281 return got_error(GOT_ERR_CANCELLED);
283 if (kerr == KCGI_ENOMEM)
284 return got_error_set_errno(ENOMEM,
285 kcgi_strerror(kerr));
287 if (kerr == KCGI_ENFILE)
288 return got_error_set_errno(ENFILE,
289 kcgi_strerror(kerr));
291 if (kerr == KCGI_EAGAIN)
292 return got_error_set_errno(EAGAIN,
293 kcgi_strerror(kerr));
295 if (kerr == KCGI_FORM)
296 return got_error_msg(GOT_ERR_IO,
297 kcgi_strerror(kerr));
299 return got_error_from_errno(kcgi_strerror(kerr));
302 static const struct got_error *
303 gw_apply_unveil(const char *repo_path)
305 const struct got_error *err;
307 if (repo_path && unveil(repo_path, "r") != 0)
308 return got_error_from_errno2("unveil", repo_path);
310 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
311 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
313 err = got_privsep_unveil_exec_helpers();
314 if (err != NULL)
315 return err;
317 if (unveil(NULL, NULL) != 0)
318 return got_error_from_errno("unveil");
320 return NULL;
323 static int
324 isbinary(const uint8_t *buf, size_t n)
326 size_t i;
328 for (i = 0; i < n; i++)
329 if (buf[i] == 0)
330 return 1;
331 return 0;
334 static const struct got_error *
335 gw_blame(struct gw_trans *gw_trans)
337 const struct got_error *error = NULL;
338 struct gw_header *header = NULL;
339 char *age = NULL;
340 enum kcgi_err kerr = KCGI_OK;
342 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
343 NULL) == -1)
344 return got_error_from_errno("pledge");
346 if ((header = gw_init_header()) == NULL)
347 return got_error_from_errno("malloc");
349 error = gw_apply_unveil(gw_trans->gw_dir->path);
350 if (error)
351 goto done;
353 /* check querystring */
354 if (gw_trans->repo_file == NULL) {
355 error = got_error_msg(GOT_ERR_QUERYSTRING,
356 "file required in querystring");
357 goto done;
359 if (gw_trans->commit_id == NULL) {
360 error = got_error_msg(GOT_ERR_QUERYSTRING,
361 "commit required in querystring");
362 goto done;
365 error = gw_get_header(gw_trans, header, 1);
366 if (error)
367 goto done;
368 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
369 "blame_header_wrapper", KATTR__MAX);
370 if (kerr != KCGI_OK)
371 goto done;
372 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
373 "blame_header", KATTR__MAX);
374 if (kerr != KCGI_OK)
375 goto done;
376 error = gw_get_time_str(&age, header->committer_time,
377 TM_LONG);
378 if (error)
379 goto done;
380 error = gw_gen_age_header(gw_trans, age ?age : "");
381 if (error)
382 goto done;
383 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
384 if (error)
385 goto done;
386 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
387 if (kerr != KCGI_OK)
388 goto done;
389 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
390 "dotted_line", KATTR__MAX);
391 if (kerr != KCGI_OK)
392 goto done;
393 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
394 if (kerr != KCGI_OK)
395 goto done;
397 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
398 "blame", KATTR__MAX);
399 if (kerr != KCGI_OK)
400 goto done;
401 error = gw_output_file_blame(gw_trans);
402 if (error)
403 goto done;
404 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
405 done:
406 gw_free_header(header);
407 if (error == NULL && kerr != KCGI_OK)
408 error = gw_kcgi_error(kerr);
409 return error;
412 static const struct got_error *
413 gw_blob(struct gw_trans *gw_trans)
415 const struct got_error *error = NULL, *err = NULL;
416 struct gw_header *header = NULL;
417 enum kcgi_err kerr = KCGI_OK;
419 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
420 NULL) == -1)
421 return got_error_from_errno("pledge");
423 if ((header = gw_init_header()) == NULL)
424 return got_error_from_errno("malloc");
426 error = gw_apply_unveil(gw_trans->gw_dir->path);
427 if (error)
428 goto done;
430 /* check querystring */
431 if (gw_trans->repo_file == NULL) {
432 error = got_error_msg(GOT_ERR_QUERYSTRING,
433 "file required in querystring");
434 goto done;
436 if (gw_trans->commit_id == NULL) {
437 error = got_error_msg(GOT_ERR_QUERYSTRING,
438 "commit required in querystring");
439 goto done;
441 error = gw_get_header(gw_trans, header, 1);
442 if (error)
443 goto done;
445 error = gw_output_blob_buf(gw_trans);
446 done:
448 if (error) {
449 gw_trans->mime = KMIME_TEXT_PLAIN;
450 err = gw_display_index(gw_trans);
451 if (err) {
452 error = err;
453 goto errored;
455 kerr = khttp_puts(gw_trans->gw_req, error->msg);
457 errored:
458 gw_free_header(header);
459 if (error == NULL && kerr != KCGI_OK)
460 error = gw_kcgi_error(kerr);
461 return error;
464 static const struct got_error *
465 gw_diff(struct gw_trans *gw_trans)
467 const struct got_error *error = NULL;
468 struct gw_header *header = NULL;
469 char *age = NULL;
470 enum kcgi_err kerr = KCGI_OK;
472 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
473 NULL) == -1)
474 return got_error_from_errno("pledge");
476 if ((header = gw_init_header()) == NULL)
477 return got_error_from_errno("malloc");
479 error = gw_apply_unveil(gw_trans->gw_dir->path);
480 if (error)
481 goto done;
483 error = gw_get_header(gw_trans, header, 1);
484 if (error)
485 goto done;
487 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
488 "diff_header_wrapper", KATTR__MAX);
489 if (kerr != KCGI_OK)
490 goto done;
491 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
492 "diff_header", KATTR__MAX);
493 if (kerr != KCGI_OK)
494 goto done;
495 error = gw_gen_diff_header(gw_trans, header->parent_id,
496 header->commit_id);
497 if (error)
498 goto done;
499 error = gw_gen_commit_header(gw_trans, header->commit_id,
500 header->refs_str);
501 if (error)
502 goto done;
503 error = gw_gen_tree_header(gw_trans, header->tree_id);
504 if (error)
505 goto done;
506 error = gw_gen_author_header(gw_trans, header->author);
507 if (error)
508 goto done;
509 error = gw_gen_committer_header(gw_trans, header->author);
510 if (error)
511 goto done;
512 error = gw_get_time_str(&age, header->committer_time,
513 TM_LONG);
514 if (error)
515 goto done;
516 error = gw_gen_age_header(gw_trans, age ?age : "");
517 if (error)
518 goto done;
519 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
520 if (error)
521 goto done;
522 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
523 if (kerr != KCGI_OK)
524 goto done;
525 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
526 "dotted_line", KATTR__MAX);
527 if (kerr != KCGI_OK)
528 goto done;
529 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
530 if (kerr != KCGI_OK)
531 goto done;
533 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
534 "diff", KATTR__MAX);
535 if (kerr != KCGI_OK)
536 goto done;
537 error = gw_output_diff(gw_trans, header);
538 if (error)
539 goto done;
541 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
542 done:
543 gw_free_header(header);
544 free(age);
545 if (error == NULL && kerr != KCGI_OK)
546 error = gw_kcgi_error(kerr);
547 return error;
550 static const struct got_error *
551 gw_index(struct gw_trans *gw_trans)
553 const struct got_error *error = NULL;
554 struct gw_dir *gw_dir = NULL;
555 char *href_next = NULL, *href_prev = NULL, *href_summary = NULL;
556 char *href_briefs = NULL, *href_commits = NULL, *href_tree = NULL;
557 char *href_tags = NULL;
558 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
559 enum kcgi_err kerr = KCGI_OK;
561 if (pledge("stdio rpath proc exec sendfd unveil",
562 NULL) == -1) {
563 error = got_error_from_errno("pledge");
564 return error;
567 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path);
568 if (error)
569 return error;
571 error = gw_load_got_paths(gw_trans);
572 if (error)
573 return error;
575 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
576 "index_header", KATTR__MAX);
577 if (kerr != KCGI_OK)
578 return gw_kcgi_error(kerr);
579 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
580 "index_header_project", KATTR__MAX);
581 if (kerr != KCGI_OK)
582 return gw_kcgi_error(kerr);
583 kerr = khtml_puts(gw_trans->gw_html_req, "Project");
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);
590 if (gw_trans->gw_conf->got_show_repo_description) {
591 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
592 "index_header_description", KATTR__MAX);
593 if (kerr != KCGI_OK)
594 return gw_kcgi_error(kerr);
595 kerr = khtml_puts(gw_trans->gw_html_req, "Description");
596 if (kerr != KCGI_OK)
597 return gw_kcgi_error(kerr);
598 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
599 if (kerr != KCGI_OK)
600 return gw_kcgi_error(kerr);
603 if (gw_trans->gw_conf->got_show_repo_owner) {
604 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
605 "index_header_owner", KATTR__MAX);
606 if (kerr != KCGI_OK)
607 return gw_kcgi_error(kerr);
608 kerr = khtml_puts(gw_trans->gw_html_req, "Owner");
609 if (kerr != KCGI_OK)
610 return gw_kcgi_error(kerr);
611 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
612 if (kerr != KCGI_OK)
613 return gw_kcgi_error(kerr);
616 if (gw_trans->gw_conf->got_show_repo_age) {
617 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
618 "index_header_age", KATTR__MAX);
619 if (kerr != KCGI_OK)
620 return gw_kcgi_error(kerr);
621 kerr = khtml_puts(gw_trans->gw_html_req, "Last Change");
622 if (kerr != KCGI_OK)
623 return gw_kcgi_error(kerr);
624 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
625 if (kerr != KCGI_OK)
626 return gw_kcgi_error(kerr);
629 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
630 if (kerr != KCGI_OK)
631 return gw_kcgi_error(kerr);
633 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
634 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
635 "index_wrapper", KATTR__MAX);
636 if (kerr != KCGI_OK)
637 return gw_kcgi_error(kerr);
638 kerr = khtml_printf(gw_trans->gw_html_req,
639 "No repositories found in %s",
640 gw_trans->gw_conf->got_repos_path);
641 if (kerr != KCGI_OK)
642 return gw_kcgi_error(kerr);
643 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
644 if (kerr != KCGI_OK)
645 return gw_kcgi_error(kerr);
646 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
647 "dotted_line", KATTR__MAX);
648 if (kerr != KCGI_OK)
649 return gw_kcgi_error(kerr);
650 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
651 if (kerr != KCGI_OK)
652 return gw_kcgi_error(kerr);
653 return error;
656 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
657 dir_c++;
659 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
660 if (gw_trans->page > 0 && (gw_trans->page *
661 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
662 prev_disp++;
663 continue;
666 prev_disp++;
668 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
669 "index_wrapper", KATTR__MAX);
670 if (kerr != KCGI_OK)
671 goto done;
673 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
674 gw_dir->name, "action", "summary", NULL);
675 if (href_summary == NULL) {
676 error = got_error_from_errno("khttp_urlpart");
677 goto done;
679 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
680 "index_project", KATTR__MAX);
681 if (kerr != KCGI_OK)
682 goto done;
683 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
684 href_summary, KATTR__MAX);
685 if (kerr != KCGI_OK)
686 goto done;
687 kerr = khtml_puts(gw_trans->gw_html_req, gw_dir->name);
688 if (kerr != KCGI_OK)
689 goto done;
690 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
691 if (kerr != KCGI_OK)
692 goto done;
693 if (gw_trans->gw_conf->got_show_repo_description) {
694 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
695 KATTR_ID, "index_project_description", KATTR__MAX);
696 if (kerr != KCGI_OK)
697 goto done;
698 kerr = khtml_puts(gw_trans->gw_html_req,
699 gw_dir->description ? gw_dir->description : "");
700 if (kerr != KCGI_OK)
701 goto done;
702 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
703 if (kerr != KCGI_OK)
704 goto done;
706 if (gw_trans->gw_conf->got_show_repo_owner) {
707 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
708 KATTR_ID, "index_project_owner", KATTR__MAX);
709 if (kerr != KCGI_OK)
710 goto done;
711 kerr = khtml_puts(gw_trans->gw_html_req,
712 gw_dir->owner ? gw_dir->owner : "");
713 if (kerr != KCGI_OK)
714 goto done;
715 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
716 if (kerr != KCGI_OK)
717 goto done;
719 if (gw_trans->gw_conf->got_show_repo_age) {
720 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
721 KATTR_ID, "index_project_age", KATTR__MAX);
722 if (kerr != KCGI_OK)
723 goto done;
724 kerr = khtml_puts(gw_trans->gw_html_req,
725 gw_dir->age ? gw_dir->age : "");
726 if (kerr != KCGI_OK)
727 goto done;
728 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
729 if (kerr != KCGI_OK)
730 goto done;
733 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
734 "navs_wrapper", KATTR__MAX);
735 if (kerr != KCGI_OK)
736 goto done;
737 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
738 "navs", KATTR__MAX);
739 if (kerr != KCGI_OK)
740 goto done;
742 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
743 href_summary, KATTR__MAX);
744 if (kerr != KCGI_OK)
745 goto done;
746 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
747 if (kerr != KCGI_OK)
748 goto done;
749 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
750 if (kerr != KCGI_OK)
751 goto done;
753 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
754 if (kerr != KCGI_OK)
755 goto done;
757 href_briefs = khttp_urlpart(NULL, NULL, "gotweb", "path",
758 gw_dir->name, "action", "briefs", NULL);
759 if (href_briefs == NULL) {
760 error = got_error_from_errno("khttp_urlpart");
761 goto done;
763 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
764 href_briefs, KATTR__MAX);
765 if (kerr != KCGI_OK)
766 goto done;
767 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
768 if (kerr != KCGI_OK)
769 goto done;
770 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
771 if (kerr != KCGI_OK)
772 error = gw_kcgi_error(kerr);
774 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
775 if (kerr != KCGI_OK)
776 goto done;
778 href_commits = khttp_urlpart(NULL, NULL, "gotweb", "path",
779 gw_dir->name, "action", "commits", NULL);
780 if (href_commits == NULL) {
781 error = got_error_from_errno("khttp_urlpart");
782 goto done;
784 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
785 href_commits, KATTR__MAX);
786 if (kerr != KCGI_OK)
787 goto done;
788 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
789 if (kerr != KCGI_OK)
790 goto done;
791 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
792 if (kerr != KCGI_OK)
793 goto done;
795 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
796 if (kerr != KCGI_OK)
797 goto done;
799 href_tags = khttp_urlpart(NULL, NULL, "gotweb", "path",
800 gw_dir->name, "action", "tags", NULL);
801 if (href_tags == NULL) {
802 error = got_error_from_errno("khttp_urlpart");
803 goto done;
805 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
806 href_tags, KATTR__MAX);
807 if (kerr != KCGI_OK)
808 goto done;
809 kerr = khtml_puts(gw_trans->gw_html_req, "tags");
810 if (kerr != KCGI_OK)
811 goto done;
812 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
813 if (kerr != KCGI_OK)
814 goto done;
816 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
817 if (kerr != KCGI_OK)
818 goto done;
820 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
821 gw_dir->name, "action", "tree", NULL);
822 if (href_tree == NULL) {
823 error = got_error_from_errno("khttp_urlpart");
824 goto done;
826 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
827 href_tree, KATTR__MAX);
828 if (kerr != KCGI_OK)
829 goto done;
830 kerr = khtml_puts(gw_trans->gw_html_req, "tree");
831 if (kerr != KCGI_OK)
832 goto done;
834 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
835 if (kerr != KCGI_OK)
836 goto done;
837 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
838 "dotted_line", KATTR__MAX);
839 if (kerr != KCGI_OK)
840 goto done;
841 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
842 if (kerr != KCGI_OK)
843 goto done;
845 free(href_summary);
846 href_summary = NULL;
847 free(href_briefs);
848 href_briefs = NULL;
849 free(href_commits);
850 href_commits = NULL;
851 free(href_tags);
852 href_tags = NULL;
853 free(href_tree);
854 href_tree = NULL;
856 if (gw_trans->gw_conf->got_max_repos_display == 0)
857 continue;
859 if ((next_disp == gw_trans->gw_conf->got_max_repos_display) ||
860 ((gw_trans->gw_conf->got_max_repos_display > 0) &&
861 (gw_trans->page > 0) &&
862 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
863 prev_disp == gw_trans->repos_total))) {
864 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
865 KATTR_ID, "np_wrapper", KATTR__MAX);
866 if (kerr != KCGI_OK)
867 goto done;
868 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
869 KATTR_ID, "nav_prev", KATTR__MAX);
870 if (kerr != KCGI_OK)
871 goto done;
874 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
875 (gw_trans->page > 0) &&
876 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
877 prev_disp == gw_trans->repos_total)) {
878 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "page",
879 KATTRX_INT, (int64_t)(gw_trans->page - 1), NULL);
880 if (href_prev == NULL) {
881 error = got_error_from_errno("khttp_urlpartx");
882 goto done;
884 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
885 KATTR_HREF, href_prev, KATTR__MAX);
886 if (kerr != KCGI_OK)
887 goto done;
888 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
889 if (kerr != KCGI_OK)
890 goto done;
891 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
892 if (kerr != KCGI_OK)
893 goto done;
896 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
897 if (kerr != KCGI_OK)
898 return gw_kcgi_error(kerr);
900 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
901 next_disp == gw_trans->gw_conf->got_max_repos_display &&
902 dir_c != (gw_trans->page + 1) *
903 gw_trans->gw_conf->got_max_repos_display) {
904 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
905 KATTR_ID, "nav_next", KATTR__MAX);
906 if (kerr != KCGI_OK)
907 goto done;
908 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "page",
909 KATTRX_INT, (int64_t)(gw_trans->page + 1), NULL);
910 if (href_next == NULL) {
911 error = got_error_from_errno("khttp_urlpartx");
912 goto done;
914 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
915 KATTR_HREF, href_next, KATTR__MAX);
916 if (kerr != KCGI_OK)
917 goto done;
918 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
919 if (kerr != KCGI_OK)
920 goto done;
921 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
922 if (kerr != KCGI_OK)
923 goto done;
924 next_disp = 0;
925 break;
928 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
929 (gw_trans->page > 0) &&
930 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
931 prev_disp == gw_trans->repos_total)) {
932 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
933 if (kerr != KCGI_OK)
934 goto done;
936 next_disp++;
938 done:
939 free(href_prev);
940 free(href_next);
941 free(href_summary);
942 free(href_briefs);
943 free(href_commits);
944 free(href_tags);
945 free(href_tree);
946 if (error == NULL && kerr != KCGI_OK)
947 error = gw_kcgi_error(kerr);
948 return error;
951 static const struct got_error *
952 gw_commits(struct gw_trans *gw_trans)
954 const struct got_error *error = NULL;
955 struct gw_header *header = NULL, *n_header = NULL;
956 char *age = NULL, *href_diff = NULL, *href_tree = NULL;
957 char *href_prev = NULL, *href_next = NULL;
958 enum kcgi_err kerr = KCGI_OK;
960 if ((header = gw_init_header()) == NULL)
961 return got_error_from_errno("malloc");
963 if (pledge("stdio rpath proc exec sendfd unveil",
964 NULL) == -1) {
965 error = got_error_from_errno("pledge");
966 goto done;
969 error = gw_apply_unveil(gw_trans->gw_dir->path);
970 if (error)
971 goto done;
973 error = gw_get_header(gw_trans, header,
974 gw_trans->gw_conf->got_max_commits_display);
975 if (error)
976 goto done;
978 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
979 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
980 "commits_line_wrapper", KATTR__MAX);
981 if (kerr != KCGI_OK)
982 goto done;
983 error = gw_gen_commit_header(gw_trans, n_header->commit_id,
984 n_header->refs_str);
985 if (error)
986 goto done;
987 error = gw_gen_author_header(gw_trans, n_header->author);
988 if (error)
989 goto done;
990 error = gw_gen_committer_header(gw_trans, n_header->author);
991 if (error)
992 goto done;
993 error = gw_get_time_str(&age, n_header->committer_time,
994 TM_LONG);
995 if (error)
996 goto done;
997 error = gw_gen_age_header(gw_trans, age ?age : "");
998 if (error)
999 goto done;
1000 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1001 if (kerr != KCGI_OK)
1002 goto done;
1004 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1005 "dotted_line", KATTR__MAX);
1006 if (kerr != KCGI_OK)
1007 goto done;
1008 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1009 if (kerr != KCGI_OK)
1010 goto done;
1012 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1013 "commit", KATTR__MAX);
1014 if (kerr != KCGI_OK)
1015 goto done;
1016 kerr = khttp_puts(gw_trans->gw_req, n_header->commit_msg);
1017 if (kerr != KCGI_OK)
1018 goto done;
1019 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1020 if (kerr != KCGI_OK)
1021 goto done;
1023 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
1024 gw_trans->repo_name, "action", "diff", "commit",
1025 n_header->commit_id, NULL);
1026 if (href_diff == NULL) {
1027 error = got_error_from_errno("khttp_urlpart");
1028 goto done;
1030 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1031 KATTR_ID, "navs_wrapper", KATTR__MAX);
1032 if (kerr != KCGI_OK)
1033 goto done;
1034 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1035 KATTR_ID, "navs", KATTR__MAX);
1036 if (kerr != KCGI_OK)
1037 goto done;
1038 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1039 KATTR_HREF, href_diff, KATTR__MAX);
1040 if (kerr != KCGI_OK)
1041 goto done;
1042 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1043 if (kerr != KCGI_OK)
1044 goto done;
1045 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1046 if (kerr != KCGI_OK)
1047 goto done;
1049 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1050 if (kerr != KCGI_OK)
1051 goto done;
1053 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
1054 gw_trans->repo_name, "action", "tree", "commit",
1055 n_header->commit_id, NULL);
1056 if (href_tree == NULL) {
1057 error = got_error_from_errno("khttp_urlpart");
1058 goto done;
1060 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1061 KATTR_HREF, href_tree, KATTR__MAX);
1062 if (kerr != KCGI_OK)
1063 goto done;
1064 khtml_puts(gw_trans->gw_html_req, "tree");
1065 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1066 if (kerr != KCGI_OK)
1067 goto done;
1068 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1069 if (kerr != KCGI_OK)
1070 goto done;
1072 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1073 "solid_line", KATTR__MAX);
1074 if (kerr != KCGI_OK)
1075 goto done;
1076 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1077 if (kerr != KCGI_OK)
1078 goto done;
1080 free(age);
1081 age = NULL;
1084 if (gw_trans->next_id || gw_trans->page > 0) {
1085 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1086 KATTR_ID, "np_wrapper", KATTR__MAX);
1087 if (kerr != KCGI_OK)
1088 goto done;
1089 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1090 KATTR_ID, "nav_prev", KATTR__MAX);
1091 if (kerr != KCGI_OK)
1092 goto done;
1095 if (gw_trans->page > 0 && gw_trans->prev_id) {
1096 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1097 KATTRX_STRING, gw_trans->repo_name, "page",
1098 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1099 KATTRX_STRING, "commits", "commit", KATTRX_STRING,
1100 gw_trans->prev_id ? gw_trans->prev_id : "", "prev",
1101 KATTRX_STRING, gw_trans->prev_prev_id ?
1102 gw_trans->prev_prev_id : "", NULL);
1103 if (href_prev == NULL) {
1104 error = got_error_from_errno("khttp_urlpartx");
1105 goto done;
1107 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1108 KATTR_HREF, href_prev, KATTR__MAX);
1109 if (kerr != KCGI_OK)
1110 goto done;
1111 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1112 if (kerr != KCGI_OK)
1113 goto done;
1114 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1115 if (kerr != KCGI_OK)
1116 goto done;
1119 if (gw_trans->next_id || gw_trans->page > 0) {
1120 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1121 if (kerr != KCGI_OK)
1122 return gw_kcgi_error(kerr);
1125 if (gw_trans->next_id) {
1126 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1127 KATTR_ID, "nav_next", KATTR__MAX);
1128 if (kerr != KCGI_OK)
1129 goto done;
1130 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1131 KATTRX_STRING, gw_trans->repo_name, "page",
1132 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1133 KATTRX_STRING, "commits", "commit", KATTRX_STRING,
1134 gw_trans->next_id, "prev", KATTRX_STRING,
1135 gw_trans->next_prev_id ? gw_trans->next_prev_id : "",
1136 "prev_prev", KATTRX_STRING, gw_trans->prev_prev_id ?
1137 gw_trans->prev_prev_id : "", NULL);
1138 if (href_next == NULL) {
1139 error = got_error_from_errno("khttp_urlpartx");
1140 goto done;
1142 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1143 KATTR_HREF, href_next, KATTR__MAX);
1144 if (kerr != KCGI_OK)
1145 goto done;
1146 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1147 if (kerr != KCGI_OK)
1148 goto done;
1149 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1150 if (kerr != KCGI_OK)
1151 goto done;
1154 if (gw_trans->next_id || gw_trans->page > 0) {
1155 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1156 if (kerr != KCGI_OK)
1157 goto done;
1159 done:
1160 gw_free_header(header);
1161 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1162 gw_free_header(n_header);
1163 free(age);
1164 free(href_next);
1165 free(href_prev);
1166 free(href_diff);
1167 free(href_tree);
1168 if (error == NULL && kerr != KCGI_OK)
1169 error = gw_kcgi_error(kerr);
1170 return error;
1173 static const struct got_error *
1174 gw_briefs(struct gw_trans *gw_trans)
1176 const struct got_error *error = NULL;
1177 struct gw_header *header = NULL, *n_header = NULL;
1178 char *age = NULL, *href_diff = NULL, *href_tree = NULL;
1179 char *href_prev = NULL, *href_next = NULL;
1180 char *newline, *smallerthan;
1181 enum kcgi_err kerr = KCGI_OK;
1183 if ((header = gw_init_header()) == NULL)
1184 return got_error_from_errno("malloc");
1186 if (pledge("stdio rpath proc exec sendfd unveil",
1187 NULL) == -1) {
1188 error = got_error_from_errno("pledge");
1189 goto done;
1192 if (gw_trans->action != GW_SUMMARY) {
1193 error = gw_apply_unveil(gw_trans->gw_dir->path);
1194 if (error)
1195 goto done;
1198 if (gw_trans->action == GW_SUMMARY)
1199 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
1200 else
1201 error = gw_get_header(gw_trans, header,
1202 gw_trans->gw_conf->got_max_commits_display);
1203 if (error)
1204 goto done;
1206 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
1207 error = gw_get_time_str(&age, n_header->committer_time,
1208 TM_DIFF);
1209 if (error)
1210 goto done;
1212 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1213 KATTR_ID, "briefs_wrapper", KATTR__MAX);
1214 if (kerr != KCGI_OK)
1215 goto done;
1217 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1218 KATTR_ID, "briefs_age", KATTR__MAX);
1219 if (kerr != KCGI_OK)
1220 goto done;
1221 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
1222 if (kerr != KCGI_OK)
1223 goto done;
1224 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1225 if (kerr != KCGI_OK)
1226 goto done;
1228 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1229 KATTR_ID, "briefs_author", KATTR__MAX);
1230 if (kerr != KCGI_OK)
1231 goto done;
1232 smallerthan = strchr(n_header->author, '<');
1233 if (smallerthan)
1234 *smallerthan = '\0';
1235 kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
1236 if (kerr != KCGI_OK)
1237 goto done;
1238 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1239 if (kerr != KCGI_OK)
1240 goto done;
1242 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
1243 gw_trans->repo_name, "action", "diff", "commit",
1244 n_header->commit_id, NULL);
1245 if (href_diff == NULL) {
1246 error = got_error_from_errno("khttp_urlpart");
1247 goto done;
1249 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1250 KATTR_ID, "briefs_log", KATTR__MAX);
1251 if (kerr != KCGI_OK)
1252 goto done;
1253 newline = strchr(n_header->commit_msg, '\n');
1254 if (newline)
1255 *newline = '\0';
1256 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1257 KATTR_HREF, href_diff, KATTR__MAX);
1258 if (kerr != KCGI_OK)
1259 goto done;
1260 kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
1261 if (kerr != KCGI_OK)
1262 goto done;
1263 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1264 if (kerr != KCGI_OK)
1265 goto done;
1267 if (n_header->refs_str) {
1268 kerr = khtml_puts(gw_trans->gw_html_req, " ");
1269 if (kerr != KCGI_OK)
1270 goto done;
1271 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
1272 KATTR_ID, "refs_str", KATTR__MAX);
1273 if (kerr != KCGI_OK)
1274 goto done;
1275 kerr = khtml_printf(gw_trans->gw_html_req, "(%s)",
1276 n_header->refs_str);
1277 if (kerr != KCGI_OK)
1278 goto done;
1279 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1280 if (kerr != KCGI_OK)
1281 goto done;
1284 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1285 if (kerr != KCGI_OK)
1286 goto done;
1288 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1289 KATTR_ID, "navs_wrapper", KATTR__MAX);
1290 if (kerr != KCGI_OK)
1291 goto done;
1292 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1293 KATTR_ID, "navs", KATTR__MAX);
1294 if (kerr != KCGI_OK)
1295 goto done;
1296 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1297 KATTR_HREF, href_diff, KATTR__MAX);
1298 if (kerr != KCGI_OK)
1299 goto done;
1300 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1301 if (kerr != KCGI_OK)
1302 goto done;
1303 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1304 if (kerr != KCGI_OK)
1305 goto done;
1307 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1308 if (kerr != KCGI_OK)
1309 goto done;
1311 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
1312 gw_trans->repo_name, "action", "tree", "commit",
1313 n_header->commit_id, NULL);
1314 if (href_tree == NULL) {
1315 error = got_error_from_errno("khttp_urlpart");
1316 goto done;
1318 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1319 KATTR_HREF, href_tree, KATTR__MAX);
1320 if (kerr != KCGI_OK)
1321 goto done;
1322 khtml_puts(gw_trans->gw_html_req, "tree");
1323 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1324 if (kerr != KCGI_OK)
1325 goto done;
1326 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1327 if (kerr != KCGI_OK)
1328 goto done;
1330 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1331 KATTR_ID, "dotted_line", KATTR__MAX);
1332 if (kerr != KCGI_OK)
1333 goto done;
1334 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1335 if (kerr != KCGI_OK)
1336 goto done;
1338 free(age);
1339 age = NULL;
1340 free(href_diff);
1341 href_diff = NULL;
1342 free(href_tree);
1343 href_tree = NULL;
1346 if (gw_trans->next_id || gw_trans->page > 0) {
1347 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1348 KATTR_ID, "np_wrapper", KATTR__MAX);
1349 if (kerr != KCGI_OK)
1350 goto done;
1351 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1352 KATTR_ID, "nav_prev", KATTR__MAX);
1353 if (kerr != KCGI_OK)
1354 goto done;
1357 if (gw_trans->page > 0 && gw_trans->prev_id) {
1358 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1359 KATTRX_STRING, gw_trans->repo_name, "page",
1360 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1361 KATTRX_STRING, "briefs", "commit", KATTRX_STRING,
1362 gw_trans->prev_id ? gw_trans->prev_id : "", "prev",
1363 KATTRX_STRING, gw_trans->prev_prev_id ?
1364 gw_trans->prev_prev_id : "", NULL);
1365 if (href_prev == NULL) {
1366 error = got_error_from_errno("khttp_urlpartx");
1367 goto done;
1369 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1370 KATTR_HREF, href_prev, KATTR__MAX);
1371 if (kerr != KCGI_OK)
1372 goto done;
1373 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1374 if (kerr != KCGI_OK)
1375 goto done;
1376 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1377 if (kerr != KCGI_OK)
1378 goto done;
1381 if (gw_trans->next_id || gw_trans->page > 0) {
1382 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1383 if (kerr != KCGI_OK)
1384 return gw_kcgi_error(kerr);
1387 if (gw_trans->next_id) {
1388 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1389 KATTR_ID, "nav_next", KATTR__MAX);
1390 if (kerr != KCGI_OK)
1391 goto done;
1393 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1394 KATTRX_STRING, gw_trans->repo_name, "page",
1395 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1396 KATTRX_STRING, "briefs", "commit", KATTRX_STRING,
1397 gw_trans->next_id, "prev", KATTRX_STRING,
1398 gw_trans->next_prev_id ? gw_trans->next_prev_id : "",
1399 "prev_prev", KATTRX_STRING, gw_trans->prev_id ?
1400 gw_trans->prev_id : "", NULL);
1401 if (href_next == NULL) {
1402 error = got_error_from_errno("khttp_urlpartx");
1403 goto done;
1405 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1406 KATTR_HREF, href_next, KATTR__MAX);
1407 if (kerr != KCGI_OK)
1408 goto done;
1409 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1410 if (kerr != KCGI_OK)
1411 goto done;
1412 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1413 if (kerr != KCGI_OK)
1414 goto done;
1417 if (gw_trans->next_id || gw_trans->page > 0) {
1418 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1419 if (kerr != KCGI_OK)
1420 goto done;
1422 done:
1423 gw_free_header(header);
1424 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1425 gw_free_header(n_header);
1426 free(age);
1427 free(href_next);
1428 free(href_prev);
1429 free(href_diff);
1430 free(href_tree);
1431 if (error == NULL && kerr != KCGI_OK)
1432 error = gw_kcgi_error(kerr);
1433 return error;
1436 static const struct got_error *
1437 gw_summary(struct gw_trans *gw_trans)
1439 const struct got_error *error = NULL;
1440 char *age = NULL;
1441 enum kcgi_err kerr = KCGI_OK;
1443 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1444 return got_error_from_errno("pledge");
1446 error = gw_apply_unveil(gw_trans->gw_dir->path);
1447 if (error)
1448 goto done;
1450 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1451 "summary_wrapper", KATTR__MAX);
1452 if (kerr != KCGI_OK)
1453 return gw_kcgi_error(kerr);
1455 if (gw_trans->gw_conf->got_show_repo_description &&
1456 gw_trans->gw_dir->description != NULL &&
1457 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1458 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1459 KATTR_ID, "description_title", KATTR__MAX);
1460 if (kerr != KCGI_OK)
1461 goto done;
1462 kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1463 if (kerr != KCGI_OK)
1464 goto done;
1465 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1466 if (kerr != KCGI_OK)
1467 goto done;
1468 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1469 KATTR_ID, "description", KATTR__MAX);
1470 if (kerr != KCGI_OK)
1471 goto done;
1472 kerr = khtml_puts(gw_trans->gw_html_req,
1473 gw_trans->gw_dir->description);
1474 if (kerr != KCGI_OK)
1475 goto done;
1476 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1477 if (kerr != KCGI_OK)
1478 goto done;
1481 if (gw_trans->gw_conf->got_show_repo_owner &&
1482 gw_trans->gw_dir->owner != NULL &&
1483 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
1484 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1485 KATTR_ID, "repo_owner_title", KATTR__MAX);
1486 if (kerr != KCGI_OK)
1487 goto done;
1488 kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1489 if (kerr != KCGI_OK)
1490 goto done;
1491 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1492 if (kerr != KCGI_OK)
1493 goto done;
1494 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1495 KATTR_ID, "repo_owner", KATTR__MAX);
1496 if (kerr != KCGI_OK)
1497 goto done;
1498 kerr = khtml_puts(gw_trans->gw_html_req,
1499 gw_trans->gw_dir->owner);
1500 if (kerr != KCGI_OK)
1501 goto done;
1502 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1503 if (kerr != KCGI_OK)
1504 goto done;
1507 if (gw_trans->gw_conf->got_show_repo_age) {
1508 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1509 NULL, TM_LONG);
1510 if (error)
1511 goto done;
1512 if (age != NULL) {
1513 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1514 KATTR_ID, "last_change_title", KATTR__MAX);
1515 if (kerr != KCGI_OK)
1516 goto done;
1517 kerr = khtml_puts(gw_trans->gw_html_req,
1518 "Last Change: ");
1519 if (kerr != KCGI_OK)
1520 goto done;
1521 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1522 if (kerr != KCGI_OK)
1523 goto done;
1524 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1525 KATTR_ID, "last_change", KATTR__MAX);
1526 if (kerr != KCGI_OK)
1527 goto done;
1528 kerr = khtml_puts(gw_trans->gw_html_req, age);
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;
1537 if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1538 gw_trans->gw_dir->url != NULL &&
1539 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1540 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1541 KATTR_ID, "cloneurl_title", KATTR__MAX);
1542 if (kerr != KCGI_OK)
1543 goto done;
1544 kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1545 if (kerr != KCGI_OK)
1546 goto done;
1547 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1548 if (kerr != KCGI_OK)
1549 goto done;
1550 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1551 KATTR_ID, "cloneurl", KATTR__MAX);
1552 if (kerr != KCGI_OK)
1553 goto done;
1554 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1555 if (kerr != KCGI_OK)
1556 goto done;
1557 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1558 if (kerr != KCGI_OK)
1559 goto done;
1562 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1563 if (kerr != KCGI_OK)
1564 goto done;
1566 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1567 "briefs_title_wrapper", KATTR__MAX);
1568 if (kerr != KCGI_OK)
1569 goto done;
1570 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1571 "briefs_title", KATTR__MAX);
1572 if (kerr != KCGI_OK)
1573 goto done;
1574 kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1575 if (kerr != KCGI_OK)
1576 goto done;
1577 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1578 if (kerr != KCGI_OK)
1579 goto done;
1580 error = gw_briefs(gw_trans);
1581 if (error)
1582 goto done;
1584 error = gw_tags(gw_trans);
1585 if (error)
1586 goto done;
1588 error = gw_output_repo_heads(gw_trans);
1589 done:
1590 free(age);
1591 if (error == NULL && kerr != KCGI_OK)
1592 error = gw_kcgi_error(kerr);
1593 return error;
1596 static const struct got_error *
1597 gw_tree(struct gw_trans *gw_trans)
1599 const struct got_error *error = NULL;
1600 struct gw_header *header = NULL;
1601 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1602 char *age = NULL;
1603 enum kcgi_err kerr = KCGI_OK;
1605 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1606 return got_error_from_errno("pledge");
1608 if ((header = gw_init_header()) == NULL)
1609 return got_error_from_errno("malloc");
1611 error = gw_apply_unveil(gw_trans->gw_dir->path);
1612 if (error)
1613 goto done;
1615 error = gw_get_header(gw_trans, header, 1);
1616 if (error)
1617 goto done;
1619 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1620 "tree_header_wrapper", KATTR__MAX);
1621 if (kerr != KCGI_OK)
1622 goto done;
1623 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1624 "tree_header", KATTR__MAX);
1625 if (kerr != KCGI_OK)
1626 goto done;
1627 error = gw_gen_tree_header(gw_trans, header->tree_id);
1628 if (error)
1629 goto done;
1630 error = gw_get_time_str(&age, header->committer_time,
1631 TM_LONG);
1632 if (error)
1633 goto done;
1634 error = gw_gen_age_header(gw_trans, age ?age : "");
1635 if (error)
1636 goto done;
1637 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1638 if (error)
1639 goto done;
1640 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1641 if (kerr != KCGI_OK)
1642 goto done;
1643 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1644 "dotted_line", KATTR__MAX);
1645 if (kerr != KCGI_OK)
1646 goto done;
1647 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1648 if (kerr != KCGI_OK)
1649 goto done;
1651 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1652 "tree", KATTR__MAX);
1653 if (kerr != KCGI_OK)
1654 goto done;
1655 error = gw_output_repo_tree(gw_trans);
1656 if (error)
1657 goto done;
1659 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1660 done:
1661 gw_free_header(header);
1662 free(tree_html_disp);
1663 free(tree_html);
1664 free(tree);
1665 free(age);
1666 if (error == NULL && kerr != KCGI_OK)
1667 error = gw_kcgi_error(kerr);
1668 return error;
1671 static const struct got_error *
1672 gw_tags(struct gw_trans *gw_trans)
1674 const struct got_error *error = NULL;
1675 struct gw_header *header = NULL;
1676 char *href_next = NULL, *href_prev = NULL;
1677 enum kcgi_err kerr = KCGI_OK;
1679 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1680 return got_error_from_errno("pledge");
1682 if ((header = gw_init_header()) == NULL)
1683 return got_error_from_errno("malloc");
1685 if (gw_trans->action != GW_SUMMARY) {
1686 error = gw_apply_unveil(gw_trans->gw_dir->path);
1687 if (error)
1688 goto done;
1691 error = gw_get_header(gw_trans, header, 1);
1692 if (error)
1693 goto done;
1695 if (gw_trans->action == GW_SUMMARY) {
1696 gw_trans->next_id = NULL;
1697 error = gw_output_repo_tags(gw_trans, header,
1698 D_MAXSLCOMMDISP, TAGBRIEF);
1699 if (error)
1700 goto done;
1701 } else {
1702 error = gw_output_repo_tags(gw_trans, header,
1703 gw_trans->gw_conf->got_max_commits_display, TAGBRIEF);
1704 if (error)
1705 goto done;
1708 if (gw_trans->next_id || gw_trans->page > 0) {
1709 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1710 KATTR_ID, "np_wrapper", KATTR__MAX);
1711 if (kerr != KCGI_OK)
1712 goto done;
1713 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1714 KATTR_ID, "nav_prev", KATTR__MAX);
1715 if (kerr != KCGI_OK)
1716 goto done;
1719 if (gw_trans->page > 0 && gw_trans->prev_id) {
1720 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1721 KATTRX_STRING, gw_trans->repo_name, "page",
1722 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1723 KATTRX_STRING, "tags", "commit", KATTRX_STRING,
1724 gw_trans->prev_id ? gw_trans->prev_id : "", "prev",
1725 KATTRX_STRING, gw_trans->prev_prev_id ?
1726 gw_trans->prev_prev_id : "", NULL);
1727 if (href_prev == NULL) {
1728 error = got_error_from_errno("khttp_urlpartx");
1729 goto done;
1731 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1732 KATTR_HREF, href_prev, KATTR__MAX);
1733 if (kerr != KCGI_OK)
1734 goto done;
1735 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1736 if (kerr != KCGI_OK)
1737 goto done;
1738 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1739 if (kerr != KCGI_OK)
1740 goto done;
1743 if (gw_trans->next_id || gw_trans->page > 0) {
1744 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1745 if (kerr != KCGI_OK)
1746 return gw_kcgi_error(kerr);
1749 if (gw_trans->next_id) {
1750 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1751 KATTR_ID, "nav_next", KATTR__MAX);
1752 if (kerr != KCGI_OK)
1753 goto done;
1754 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1755 KATTRX_STRING, gw_trans->repo_name, "page",
1756 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1757 KATTRX_STRING, "tags", "commit", KATTRX_STRING,
1758 gw_trans->next_id, "prev", KATTRX_STRING,
1759 gw_trans->next_prev_id ? gw_trans->next_prev_id : "",
1760 "prev_prev", KATTRX_STRING, gw_trans->prev_id ?
1761 gw_trans->prev_id : "", NULL);
1762 if (href_next == NULL) {
1763 error = got_error_from_errno("khttp_urlpartx");
1764 goto done;
1766 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1767 KATTR_HREF, href_next, KATTR__MAX);
1768 if (kerr != KCGI_OK)
1769 goto done;
1770 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1771 if (kerr != KCGI_OK)
1772 goto done;
1773 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1774 if (kerr != KCGI_OK)
1775 goto done;
1778 if (gw_trans->next_id || gw_trans->page > 0) {
1779 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1780 if (kerr != KCGI_OK)
1781 goto done;
1783 done:
1784 gw_free_header(header);
1785 free(href_next);
1786 free(href_prev);
1787 if (error == NULL && kerr != KCGI_OK)
1788 error = gw_kcgi_error(kerr);
1789 return error;
1792 static const struct got_error *
1793 gw_tag(struct gw_trans *gw_trans)
1795 const struct got_error *error = NULL;
1796 struct gw_header *header = NULL;
1797 enum kcgi_err kerr = KCGI_OK;
1799 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1800 return got_error_from_errno("pledge");
1802 if ((header = gw_init_header()) == NULL)
1803 return got_error_from_errno("malloc");
1805 error = gw_apply_unveil(gw_trans->gw_dir->path);
1806 if (error)
1807 goto done;
1809 if (gw_trans->commit_id == NULL) {
1810 error = got_error_msg(GOT_ERR_QUERYSTRING,
1811 "commit required in querystring");
1812 goto done;
1815 error = gw_get_header(gw_trans, header, 1);
1816 if (error)
1817 goto done;
1819 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1820 "tag_header_wrapper", KATTR__MAX);
1821 if (kerr != KCGI_OK)
1822 goto done;
1823 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1824 "tag_header", KATTR__MAX);
1825 if (kerr != KCGI_OK)
1826 goto done;
1827 error = gw_gen_commit_header(gw_trans, header->commit_id,
1828 header->refs_str);
1829 if (error)
1830 goto done;
1831 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1832 if (error)
1833 goto done;
1834 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1835 if (kerr != KCGI_OK)
1836 goto done;
1837 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1838 "dotted_line", KATTR__MAX);
1839 if (kerr != KCGI_OK)
1840 goto done;
1841 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1842 if (kerr != KCGI_OK)
1843 goto done;
1845 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1846 "tree", KATTR__MAX);
1847 if (kerr != KCGI_OK)
1848 goto done;
1850 error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1851 if (error)
1852 goto done;
1854 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1855 done:
1856 gw_free_header(header);
1857 if (error == NULL && kerr != KCGI_OK)
1858 error = gw_kcgi_error(kerr);
1859 return error;
1862 static const struct got_error *
1863 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1865 const struct got_error *error = NULL;
1866 DIR *dt;
1867 char *dir_test;
1868 int opened = 0;
1870 if (asprintf(&dir_test, "%s/%s/%s",
1871 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1872 GOTWEB_GIT_DIR) == -1)
1873 return got_error_from_errno("asprintf");
1875 dt = opendir(dir_test);
1876 if (dt == NULL) {
1877 free(dir_test);
1878 } else {
1879 gw_dir->path = strdup(dir_test);
1880 if (gw_dir->path == NULL) {
1881 opened = 1;
1882 error = got_error_from_errno("strdup");
1883 goto errored;
1885 opened = 1;
1886 goto done;
1889 if (asprintf(&dir_test, "%s/%s/%s",
1890 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1891 GOTWEB_GOT_DIR) == -1) {
1892 dir_test = NULL;
1893 error = got_error_from_errno("asprintf");
1894 goto errored;
1897 dt = opendir(dir_test);
1898 if (dt == NULL)
1899 free(dir_test);
1900 else {
1901 opened = 1;
1902 error = got_error(GOT_ERR_NOT_GIT_REPO);
1903 goto errored;
1906 if (asprintf(&dir_test, "%s/%s",
1907 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1) {
1908 error = got_error_from_errno("asprintf");
1909 dir_test = NULL;
1910 goto errored;
1913 gw_dir->path = strdup(dir_test);
1914 if (gw_dir->path == NULL) {
1915 opened = 1;
1916 error = got_error_from_errno("strdup");
1917 goto errored;
1920 dt = opendir(dir_test);
1921 if (dt == NULL) {
1922 error = got_error_path(gw_dir->name, GOT_ERR_NOT_GIT_REPO);
1923 goto errored;
1924 } else
1925 opened = 1;
1926 done:
1927 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1928 gw_dir->path);
1929 if (error)
1930 goto errored;
1931 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1932 if (error)
1933 goto errored;
1934 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1935 NULL, TM_DIFF);
1936 if (error)
1937 goto errored;
1938 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1939 errored:
1940 free(dir_test);
1941 if (opened)
1942 if (dt && closedir(dt) == -1 && error == NULL)
1943 error = got_error_from_errno("closedir");
1944 return error;
1947 static const struct got_error *
1948 gw_load_got_paths(struct gw_trans *gw_trans)
1950 const struct got_error *error = NULL;
1951 DIR *d;
1952 struct dirent **sd_dent;
1953 struct gw_dir *gw_dir;
1954 struct stat st;
1955 unsigned int d_cnt, d_i;
1957 d = opendir(gw_trans->gw_conf->got_repos_path);
1958 if (d == NULL) {
1959 error = got_error_from_errno2("opendir",
1960 gw_trans->gw_conf->got_repos_path);
1961 return error;
1964 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1965 alphasort);
1966 if (d_cnt == -1) {
1967 error = got_error_from_errno2("scandir",
1968 gw_trans->gw_conf->got_repos_path);
1969 goto done;
1972 for (d_i = 0; d_i < d_cnt; d_i++) {
1973 if (gw_trans->gw_conf->got_max_repos > 0 &&
1974 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1975 break; /* account for parent and self */
1977 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1978 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1979 continue;
1981 error = gw_init_gw_dir(&gw_dir, sd_dent[d_i]->d_name);
1982 if (error)
1983 goto done;
1985 error = gw_load_got_path(gw_trans, gw_dir);
1986 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1987 error = NULL;
1988 continue;
1990 else if (error)
1991 goto done;
1993 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1994 !got_path_dir_is_empty(gw_dir->path)) {
1995 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1996 entry);
1997 gw_trans->repos_total++;
2000 done:
2001 if (d && closedir(d) == -1 && error == NULL)
2002 error = got_error_from_errno("closedir");
2003 return error;
2006 static const struct got_error *
2007 gw_parse_querystring(struct gw_trans *gw_trans)
2009 const struct got_error *error = NULL;
2010 struct kpair *p;
2011 struct gw_query_action *action = NULL;
2012 unsigned int i;
2014 if (gw_trans->gw_req->fieldnmap[0]) {
2015 return got_error(GOT_ERR_QUERYSTRING);
2016 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
2017 /* define gw_trans->repo_path */
2018 gw_trans->repo_name = p->parsed.s;
2020 if (asprintf(&gw_trans->repo_path, "%s/%s",
2021 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
2022 return got_error_from_errno("asprintf");
2024 /* get action and set function */
2025 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION])) {
2026 for (i = 0; i < nitems(gw_query_funcs); i++) {
2027 action = &gw_query_funcs[i];
2028 if (action->func_name == NULL)
2029 continue;
2030 if (strcmp(action->func_name,
2031 p->parsed.s) == 0) {
2032 gw_trans->action = i;
2033 break;
2037 if (gw_trans->action == -1) {
2038 gw_trans->action = GW_ERR;
2039 gw_trans->error = got_error_msg(GOT_ERR_QUERYSTRING,
2040 p != NULL ? "bad action in querystring" :
2041 "no action in querystring");
2042 return error;
2045 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID])) {
2046 if (asprintf(&gw_trans->commit_id, "%s",
2047 p->parsed.s) == -1)
2048 return got_error_from_errno("asprintf");
2051 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
2052 gw_trans->repo_file = p->parsed.s;
2054 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER])) {
2055 if (asprintf(&gw_trans->repo_folder, "%s",
2056 p->parsed.s) == -1)
2057 return got_error_from_errno("asprintf");
2060 if ((p = gw_trans->gw_req->fieldmap[KEY_PREV_ID])) {
2061 if (asprintf(&gw_trans->prev_id, "%s",
2062 p->parsed.s) == -1)
2063 return got_error_from_errno("asprintf");
2066 if ((p = gw_trans->gw_req->fieldmap[KEY_PREV_PREV_ID])) {
2067 if (asprintf(&gw_trans->prev_prev_id, "%s",
2068 p->parsed.s) == -1)
2069 return got_error_from_errno("asprintf");
2072 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
2073 gw_trans->headref = p->parsed.s;
2075 error = gw_init_gw_dir(&gw_trans->gw_dir, gw_trans->repo_name);
2076 if (error)
2077 return error;
2079 gw_trans->error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
2080 } else
2081 gw_trans->action = GW_INDEX;
2083 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
2084 gw_trans->page = p->parsed.i;
2086 return error;
2089 static const struct got_error *
2090 gw_init_gw_dir(struct gw_dir **gw_dir, const char *dir)
2092 const struct got_error *error;
2094 *gw_dir = malloc(sizeof(**gw_dir));
2095 if (*gw_dir == NULL)
2096 return got_error_from_errno("malloc");
2098 if (asprintf(&(*gw_dir)->name, "%s", dir) == -1) {
2099 error = got_error_from_errno("asprintf");
2100 free(*gw_dir);
2101 *gw_dir = NULL;
2102 return NULL;
2105 return NULL;
2108 static const struct got_error *
2109 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
2111 enum kcgi_err kerr = KCGI_OK;
2113 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
2114 if (kerr != KCGI_OK)
2115 return gw_kcgi_error(kerr);
2116 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
2117 khttps[code]);
2118 if (kerr != KCGI_OK)
2119 return gw_kcgi_error(kerr);
2120 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
2121 kmimetypes[mime]);
2122 if (kerr != KCGI_OK)
2123 return gw_kcgi_error(kerr);
2124 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
2125 "nosniff");
2126 if (kerr != KCGI_OK)
2127 return gw_kcgi_error(kerr);
2128 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
2129 if (kerr != KCGI_OK)
2130 return gw_kcgi_error(kerr);
2131 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
2132 "1; mode=block");
2133 if (kerr != KCGI_OK)
2134 return gw_kcgi_error(kerr);
2136 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
2137 kerr = khttp_head(gw_trans->gw_req,
2138 kresps[KRESP_CONTENT_DISPOSITION],
2139 "attachment; filename=%s", gw_trans->repo_file);
2140 if (kerr != KCGI_OK)
2141 return gw_kcgi_error(kerr);
2144 kerr = khttp_body(gw_trans->gw_req);
2145 return gw_kcgi_error(kerr);
2148 static const struct got_error *
2149 gw_display_index(struct gw_trans *gw_trans)
2151 const struct got_error *error;
2152 enum kcgi_err kerr = KCGI_OK;
2154 /* catch early querystring errors */
2155 if (gw_trans->error)
2156 gw_trans->action = GW_ERR;
2158 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
2159 if (error)
2160 return error;
2162 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
2163 if (kerr != KCGI_OK)
2164 return gw_kcgi_error(kerr);
2166 if (gw_trans->action != GW_BLOB) {
2167 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
2168 gw_query_funcs[gw_trans->action].template);
2169 if (kerr != KCGI_OK) {
2170 khtml_close(gw_trans->gw_html_req);
2171 return gw_kcgi_error(kerr);
2175 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
2178 static const struct got_error *
2179 gw_error(struct gw_trans *gw_trans)
2181 enum kcgi_err kerr = KCGI_OK;
2183 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->error->msg);
2185 return gw_kcgi_error(kerr);
2188 static int
2189 gw_template(size_t key, void *arg)
2191 const struct got_error *error = NULL;
2192 enum kcgi_err kerr = KCGI_OK;
2193 struct gw_trans *gw_trans = arg;
2194 char *img_src = NULL;
2196 switch (key) {
2197 case (TEMPL_HEAD):
2198 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2199 KATTR_NAME, "viewport",
2200 KATTR_CONTENT, "initial-scale=.75, user-scalable=yes",
2201 KATTR__MAX);
2202 if (kerr != KCGI_OK)
2203 return 0;
2204 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2205 if (kerr != KCGI_OK)
2206 return 0;
2207 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2208 KATTR_CHARSET, "utf-8",
2209 KATTR__MAX);
2210 if (kerr != KCGI_OK)
2211 return 0;
2212 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2213 if (kerr != KCGI_OK)
2214 return 0;
2215 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2216 KATTR_NAME, "msapplication-TileColor",
2217 KATTR_CONTENT, "#da532c", KATTR__MAX);
2218 if (kerr != KCGI_OK)
2219 return 0;
2220 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2221 if (kerr != KCGI_OK)
2222 return 0;
2223 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2224 KATTR_NAME, "theme-color",
2225 KATTR_CONTENT, "#ffffff", KATTR__MAX);
2226 if (kerr != KCGI_OK)
2227 return 0;
2228 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2229 if (kerr != KCGI_OK)
2230 return 0;
2231 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2232 KATTR_REL, "apple-touch-icon", KATTR_SIZES, "180x180",
2233 KATTR_HREF, "/apple-touch-icon.png", KATTR__MAX);
2234 if (kerr != KCGI_OK)
2235 return 0;
2236 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2237 if (kerr != KCGI_OK)
2238 return 0;
2239 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2240 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2241 "32x32", KATTR_HREF, "/favicon-32x32.png", KATTR__MAX);
2242 if (kerr != KCGI_OK)
2243 return 0;
2244 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2245 if (kerr != KCGI_OK)
2246 return 0;
2247 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2248 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2249 "16x16", KATTR_HREF, "/favicon-16x16.png", KATTR__MAX);
2250 if (kerr != KCGI_OK)
2251 return 0;
2252 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2253 if (kerr != KCGI_OK)
2254 return 0;
2255 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2256 KATTR_REL, "manifest", KATTR_HREF, "/site.webmanifest",
2257 KATTR__MAX);
2258 if (kerr != KCGI_OK)
2259 return 0;
2260 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2261 if (kerr != KCGI_OK)
2262 return 0;
2263 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2264 KATTR_REL, "mask-icon", KATTR_HREF,
2265 "/safari-pinned-tab.svg", KATTR__MAX);
2266 if (kerr != KCGI_OK)
2267 return 0;
2268 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2269 if (kerr != KCGI_OK)
2270 return 0;
2271 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2272 KATTR_REL, "stylesheet", KATTR_TYPE, "text/css",
2273 KATTR_HREF, "/gotweb.css", KATTR__MAX);
2274 if (kerr != KCGI_OK)
2275 return 0;
2276 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2277 if (kerr != KCGI_OK)
2278 return 0;
2279 break;
2280 case(TEMPL_HEADER):
2281 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2282 KATTR_ID, "got_link", KATTR__MAX);
2283 if (kerr != KCGI_OK)
2284 return 0;
2285 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2286 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
2287 KATTR_TARGET, "_sotd", KATTR__MAX);
2288 if (kerr != KCGI_OK)
2289 return 0;
2290 if (asprintf(&img_src, "/%s",
2291 gw_trans->gw_conf->got_logo) == -1)
2292 return 0;
2293 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
2294 KATTR_SRC, img_src, KATTR__MAX);
2295 if (kerr != KCGI_OK) {
2296 free(img_src);
2297 return 0;
2299 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2300 if (kerr != KCGI_OK) {
2301 free(img_src);
2302 return 0;
2304 break;
2305 case (TEMPL_SITEPATH):
2306 error = gw_output_site_link(gw_trans);
2307 if (error)
2308 return 0;
2309 break;
2310 case(TEMPL_TITLE):
2311 if (gw_trans->gw_conf->got_site_name != NULL) {
2312 kerr = khtml_puts(gw_trans->gw_html_req,
2313 gw_trans->gw_conf->got_site_name);
2314 if (kerr != KCGI_OK)
2315 return 0;
2317 break;
2318 case (TEMPL_SEARCH):
2319 break;
2320 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
2321 "search", KATTR__MAX);
2322 if (kerr != KCGI_OK)
2323 return 0;
2324 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
2325 KATTR_METHOD, "POST", KATTR__MAX);
2326 if (kerr != KCGI_OK)
2327 return 0;
2328 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
2329 "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
2330 KATTR_MAXLENGTH, "50", KATTR__MAX);
2331 if (kerr != KCGI_OK)
2332 return 0;
2333 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
2334 KATTR__MAX);
2335 if (kerr != KCGI_OK)
2336 return 0;
2337 kerr = khtml_puts(gw_trans->gw_html_req, "Search");
2338 if (kerr != KCGI_OK)
2339 return 0;
2340 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
2341 if (kerr != KCGI_OK)
2342 return 0;
2343 break;
2344 case(TEMPL_SITEOWNER):
2345 if (gw_trans->gw_conf->got_site_owner != NULL &&
2346 gw_trans->gw_conf->got_show_site_owner) {
2347 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2348 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
2349 if (kerr != KCGI_OK)
2350 return 0;
2351 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2352 KATTR_ID, "site_owner", KATTR__MAX);
2353 if (kerr != KCGI_OK)
2354 return 0;
2355 kerr = khtml_puts(gw_trans->gw_html_req,
2356 gw_trans->gw_conf->got_site_owner);
2357 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2358 if (kerr != KCGI_OK)
2359 return 0;
2361 break;
2362 case(TEMPL_CONTENT):
2363 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
2364 if (error) {
2365 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2366 KATTR_ID, "tmpl_err", KATTR__MAX);
2367 if (kerr != KCGI_OK)
2368 return 0;
2369 kerr = khttp_printf(gw_trans->gw_req, "Error: %s",
2370 error->msg);
2371 if (kerr != KCGI_OK)
2372 return 0;
2373 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2374 if (kerr != KCGI_OK)
2375 return 0;
2377 break;
2378 default:
2379 return 0;
2381 return 1;
2384 static const struct got_error *
2385 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
2387 const struct got_error *error = NULL;
2388 enum kcgi_err kerr = KCGI_OK;
2390 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2391 KATTR_ID, "header_commit_title", KATTR__MAX);
2392 if (kerr != KCGI_OK)
2393 goto done;
2394 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
2395 if (kerr != KCGI_OK)
2396 goto done;
2397 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2398 if (kerr != KCGI_OK)
2399 goto done;
2400 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2401 KATTR_ID, "header_commit", KATTR__MAX);
2402 if (kerr != KCGI_OK)
2403 goto done;
2404 kerr = khtml_printf(gw_trans->gw_html_req, "%s ", str1);
2405 if (kerr != KCGI_OK)
2406 goto done;
2407 if (str2 != NULL) {
2408 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
2409 KATTR_ID, "refs_str", KATTR__MAX);
2410 if (kerr != KCGI_OK)
2411 goto done;
2412 kerr = khtml_printf(gw_trans->gw_html_req, "(%s)", str2);
2413 if (kerr != KCGI_OK)
2414 goto done;
2415 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2416 if (kerr != KCGI_OK)
2417 goto done;
2419 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2420 done:
2421 if (error == NULL && kerr != KCGI_OK)
2422 error = gw_kcgi_error(kerr);
2423 return error;
2426 static const struct got_error *
2427 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2429 const struct got_error *error = NULL;
2430 enum kcgi_err kerr = KCGI_OK;
2432 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2433 KATTR_ID, "header_diff_title", KATTR__MAX);
2434 if (kerr != KCGI_OK)
2435 goto done;
2436 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2437 if (kerr != KCGI_OK)
2438 goto done;
2439 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2440 if (kerr != KCGI_OK)
2441 goto done;
2442 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2443 KATTR_ID, "header_diff", KATTR__MAX);
2444 if (kerr != KCGI_OK)
2445 goto done;
2446 if (str1 != NULL) {
2447 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2448 if (kerr != KCGI_OK)
2449 goto done;
2451 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2452 if (kerr != KCGI_OK)
2453 goto done;
2454 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2455 if (kerr != KCGI_OK)
2456 goto done;
2457 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2458 done:
2459 if (error == NULL && kerr != KCGI_OK)
2460 error = gw_kcgi_error(kerr);
2461 return error;
2464 static const struct got_error *
2465 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2467 const struct got_error *error = NULL;
2468 enum kcgi_err kerr = KCGI_OK;
2470 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2471 KATTR_ID, "header_age_title", KATTR__MAX);
2472 if (kerr != KCGI_OK)
2473 goto done;
2474 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2475 if (kerr != KCGI_OK)
2476 goto done;
2477 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2478 if (kerr != KCGI_OK)
2479 goto done;
2480 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2481 KATTR_ID, "header_age", KATTR__MAX);
2482 if (kerr != KCGI_OK)
2483 goto done;
2484 kerr = khtml_puts(gw_trans->gw_html_req, str);
2485 if (kerr != KCGI_OK)
2486 goto done;
2487 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2488 done:
2489 if (error == NULL && kerr != KCGI_OK)
2490 error = gw_kcgi_error(kerr);
2491 return error;
2494 static const struct got_error *
2495 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2497 const struct got_error *error = NULL;
2498 enum kcgi_err kerr = KCGI_OK;
2500 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2501 KATTR_ID, "header_author_title", KATTR__MAX);
2502 if (kerr != KCGI_OK)
2503 goto done;
2504 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2505 if (kerr != KCGI_OK)
2506 goto done;
2507 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2508 if (kerr != KCGI_OK)
2509 goto done;
2510 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2511 KATTR_ID, "header_author", KATTR__MAX);
2512 if (kerr != KCGI_OK)
2513 goto done;
2514 kerr = khtml_puts(gw_trans->gw_html_req, str);
2515 if (kerr != KCGI_OK)
2516 goto done;
2517 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2518 done:
2519 if (error == NULL && kerr != KCGI_OK)
2520 error = gw_kcgi_error(kerr);
2521 return error;
2524 static const struct got_error *
2525 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2527 const struct got_error *error = NULL;
2528 enum kcgi_err kerr = KCGI_OK;
2530 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2531 KATTR_ID, "header_committer_title", KATTR__MAX);
2532 if (kerr != KCGI_OK)
2533 goto done;
2534 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2535 if (kerr != KCGI_OK)
2536 goto done;
2537 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2538 if (kerr != KCGI_OK)
2539 goto done;
2540 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2541 KATTR_ID, "header_committer", KATTR__MAX);
2542 if (kerr != KCGI_OK)
2543 goto done;
2544 kerr = khtml_puts(gw_trans->gw_html_req, str);
2545 if (kerr != KCGI_OK)
2546 goto done;
2547 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2548 done:
2549 if (error == NULL && kerr != KCGI_OK)
2550 error = gw_kcgi_error(kerr);
2551 return error;
2554 static const struct got_error *
2555 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2557 const struct got_error *error = NULL;
2558 enum kcgi_err kerr = KCGI_OK;
2560 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2561 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2562 if (kerr != KCGI_OK)
2563 goto done;
2564 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2565 if (kerr != KCGI_OK)
2566 goto done;
2567 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2568 if (kerr != KCGI_OK)
2569 goto done;
2570 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2571 KATTR_ID, "header_commit_msg", KATTR__MAX);
2572 if (kerr != KCGI_OK)
2573 goto done;
2574 kerr = khttp_puts(gw_trans->gw_req, str);
2575 if (kerr != KCGI_OK)
2576 goto done;
2577 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2578 done:
2579 if (error == NULL && kerr != KCGI_OK)
2580 error = gw_kcgi_error(kerr);
2581 return error;
2584 static const struct got_error *
2585 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2587 const struct got_error *error = NULL;
2588 enum kcgi_err kerr = KCGI_OK;
2590 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2591 KATTR_ID, "header_tree_title", KATTR__MAX);
2592 if (kerr != KCGI_OK)
2593 goto done;
2594 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2595 if (kerr != KCGI_OK)
2596 goto done;
2597 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2598 if (kerr != KCGI_OK)
2599 goto done;
2600 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2601 KATTR_ID, "header_tree", KATTR__MAX);
2602 if (kerr != KCGI_OK)
2603 goto done;
2604 kerr = khtml_puts(gw_trans->gw_html_req, str);
2605 if (kerr != KCGI_OK)
2606 goto done;
2607 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2608 done:
2609 if (error == NULL && kerr != KCGI_OK)
2610 error = gw_kcgi_error(kerr);
2611 return error;
2614 static const struct got_error *
2615 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2616 char *dir)
2618 const struct got_error *error = NULL;
2619 FILE *f = NULL;
2620 char *d_file = NULL;
2621 unsigned int len;
2622 size_t n;
2624 *description = NULL;
2625 if (gw_trans->gw_conf->got_show_repo_description == 0)
2626 return NULL;
2628 if (asprintf(&d_file, "%s/description", dir) == -1)
2629 return got_error_from_errno("asprintf");
2631 f = fopen(d_file, "r");
2632 if (f == NULL) {
2633 if (errno == ENOENT || errno == EACCES)
2634 return NULL;
2635 error = got_error_from_errno2("fopen", d_file);
2636 goto done;
2639 if (fseek(f, 0, SEEK_END) == -1) {
2640 error = got_ferror(f, GOT_ERR_IO);
2641 goto done;
2643 len = ftell(f);
2644 if (len == -1) {
2645 error = got_ferror(f, GOT_ERR_IO);
2646 goto done;
2648 if (fseek(f, 0, SEEK_SET) == -1) {
2649 error = got_ferror(f, GOT_ERR_IO);
2650 goto done;
2652 *description = calloc(len + 1, sizeof(**description));
2653 if (*description == NULL) {
2654 error = got_error_from_errno("calloc");
2655 goto done;
2658 n = fread(*description, 1, len, f);
2659 if (n == 0 && ferror(f))
2660 error = got_ferror(f, GOT_ERR_IO);
2661 done:
2662 if (f != NULL && fclose(f) == -1 && error == NULL)
2663 error = got_error_from_errno("fclose");
2664 free(d_file);
2665 return error;
2668 static const struct got_error *
2669 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2671 struct tm tm;
2672 time_t diff_time;
2673 char *years = "years ago", *months = "months ago";
2674 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2675 char *minutes = "minutes ago", *seconds = "seconds ago";
2676 char *now = "right now";
2677 char *s;
2678 char datebuf[29];
2680 *repo_age = NULL;
2682 switch (ref_tm) {
2683 case TM_DIFF:
2684 diff_time = time(NULL) - committer_time;
2685 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2686 if (asprintf(repo_age, "%lld %s",
2687 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2688 return got_error_from_errno("asprintf");
2689 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2690 if (asprintf(repo_age, "%lld %s",
2691 (diff_time / 60 / 60 / 24 / (365 / 12)),
2692 months) == -1)
2693 return got_error_from_errno("asprintf");
2694 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2695 if (asprintf(repo_age, "%lld %s",
2696 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2697 return got_error_from_errno("asprintf");
2698 } else if (diff_time > 60 * 60 * 24 * 2) {
2699 if (asprintf(repo_age, "%lld %s",
2700 (diff_time / 60 / 60 / 24), days) == -1)
2701 return got_error_from_errno("asprintf");
2702 } else if (diff_time > 60 * 60 * 2) {
2703 if (asprintf(repo_age, "%lld %s",
2704 (diff_time / 60 / 60), hours) == -1)
2705 return got_error_from_errno("asprintf");
2706 } else if (diff_time > 60 * 2) {
2707 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2708 minutes) == -1)
2709 return got_error_from_errno("asprintf");
2710 } else if (diff_time > 2) {
2711 if (asprintf(repo_age, "%lld %s", diff_time,
2712 seconds) == -1)
2713 return got_error_from_errno("asprintf");
2714 } else {
2715 if (asprintf(repo_age, "%s", now) == -1)
2716 return got_error_from_errno("asprintf");
2718 break;
2719 case TM_LONG:
2720 if (gmtime_r(&committer_time, &tm) == NULL)
2721 return got_error_from_errno("gmtime_r");
2723 s = asctime_r(&tm, datebuf);
2724 if (s == NULL)
2725 return got_error_from_errno("asctime_r");
2727 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2728 return got_error_from_errno("asprintf");
2729 break;
2731 return NULL;
2734 static const struct got_error *
2735 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2736 const char *refname, int ref_tm)
2738 const struct got_error *error = NULL;
2739 struct got_repository *repo = NULL;
2740 struct got_commit_object *commit = NULL;
2741 struct got_reflist_head refs;
2742 struct got_reflist_entry *re;
2743 time_t committer_time = 0, cmp_time = 0;
2745 *repo_age = NULL;
2746 SIMPLEQ_INIT(&refs);
2748 if (gw_trans->gw_conf->got_show_repo_age == 0)
2749 return NULL;
2751 if (gw_trans->repo)
2752 repo = gw_trans->repo;
2753 else {
2754 error = got_repo_open(&repo, dir, NULL);
2755 if (error)
2756 return error;
2759 error = got_ref_list(&refs, repo, "refs/heads",
2760 got_ref_cmp_by_name, NULL);
2761 if (error)
2762 goto done;
2765 * Find the youngest branch tip in the repository, or the age of
2766 * the a specific branch tip if a name was provided by the caller.
2768 SIMPLEQ_FOREACH(re, &refs, entry) {
2769 struct got_object_id *id = NULL;
2771 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
2772 continue;
2774 error = got_ref_resolve(&id, repo, re->ref);
2775 if (error)
2776 goto done;
2778 error = got_object_open_as_commit(&commit, repo, id);
2779 free(id);
2780 if (error)
2781 goto done;
2783 committer_time =
2784 got_object_commit_get_committer_time(commit);
2785 got_object_commit_close(commit);
2786 if (cmp_time < committer_time)
2787 cmp_time = committer_time;
2789 if (refname)
2790 break;
2793 if (cmp_time != 0) {
2794 committer_time = cmp_time;
2795 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2797 done:
2798 got_ref_list_free(&refs);
2799 if (gw_trans->repo == NULL)
2800 got_repo_close(repo);
2801 return error;
2804 static const struct got_error *
2805 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2807 const struct got_error *error;
2808 FILE *f = NULL;
2809 struct got_object_id *id1 = NULL, *id2 = NULL;
2810 char *label1 = NULL, *label2 = NULL, *line = NULL;
2811 int obj_type;
2812 size_t linesize = 0;
2813 ssize_t linelen;
2814 enum kcgi_err kerr = KCGI_OK;
2816 f = got_opentemp();
2817 if (f == NULL)
2818 return NULL;
2820 if (header->parent_id != NULL &&
2821 strncmp(header->parent_id, "/dev/null", 9) != 0) {
2822 error = got_repo_match_object_id(&id1, &label1,
2823 header->parent_id, GOT_OBJ_TYPE_ANY, 1, gw_trans->repo);
2824 if (error)
2825 goto done;
2828 error = got_repo_match_object_id(&id2, &label2,
2829 header->commit_id, GOT_OBJ_TYPE_ANY, 1, gw_trans->repo);
2830 if (error)
2831 goto done;
2833 error = got_object_get_type(&obj_type, gw_trans->repo, id2);
2834 if (error)
2835 goto done;
2836 switch (obj_type) {
2837 case GOT_OBJ_TYPE_BLOB:
2838 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
2839 gw_trans->repo, f);
2840 break;
2841 case GOT_OBJ_TYPE_TREE:
2842 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
2843 gw_trans->repo, f);
2844 break;
2845 case GOT_OBJ_TYPE_COMMIT:
2846 error = got_diff_objects_as_commits(id1, id2, 3, 0,
2847 gw_trans->repo, f);
2848 break;
2849 default:
2850 error = got_error(GOT_ERR_OBJ_TYPE);
2852 if (error)
2853 goto done;
2855 if (fseek(f, 0, SEEK_SET) == -1) {
2856 error = got_ferror(f, GOT_ERR_IO);
2857 goto done;
2860 while ((linelen = getline(&line, &linesize, f)) != -1) {
2861 error = gw_colordiff_line(gw_trans, line);
2862 if (error)
2863 goto done;
2864 /* XXX: KHTML_PRETTY breaks this */
2865 kerr = khtml_puts(gw_trans->gw_html_req, line);
2866 if (kerr != KCGI_OK)
2867 goto done;
2868 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2869 if (kerr != KCGI_OK)
2870 goto done;
2872 if (linelen == -1 && ferror(f))
2873 error = got_error_from_errno("getline");
2874 done:
2875 if (f && fclose(f) == -1 && error == NULL)
2876 error = got_error_from_errno("fclose");
2877 free(line);
2878 free(label1);
2879 free(label2);
2880 free(id1);
2881 free(id2);
2883 if (error == NULL && kerr != KCGI_OK)
2884 error = gw_kcgi_error(kerr);
2885 return error;
2888 static const struct got_error *
2889 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2891 const struct got_error *error = NULL;
2892 struct got_repository *repo;
2893 const char *gitconfig_owner;
2895 *owner = NULL;
2897 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2898 return NULL;
2900 error = got_repo_open(&repo, dir, NULL);
2901 if (error)
2902 return error;
2903 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2904 if (gitconfig_owner) {
2905 *owner = strdup(gitconfig_owner);
2906 if (*owner == NULL)
2907 error = got_error_from_errno("strdup");
2909 got_repo_close(repo);
2910 return error;
2913 static const struct got_error *
2914 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2916 const struct got_error *error = NULL;
2917 FILE *f;
2918 char *d_file = NULL;
2919 unsigned int len;
2920 size_t n;
2922 *url = NULL;
2924 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2925 return got_error_from_errno("asprintf");
2927 f = fopen(d_file, "r");
2928 if (f == NULL) {
2929 if (errno != ENOENT && errno != EACCES)
2930 error = got_error_from_errno2("fopen", d_file);
2931 goto done;
2934 if (fseek(f, 0, SEEK_END) == -1) {
2935 error = got_ferror(f, GOT_ERR_IO);
2936 goto done;
2938 len = ftell(f);
2939 if (len == -1) {
2940 error = got_ferror(f, GOT_ERR_IO);
2941 goto done;
2943 if (fseek(f, 0, SEEK_SET) == -1) {
2944 error = got_ferror(f, GOT_ERR_IO);
2945 goto done;
2948 *url = calloc(len + 1, sizeof(**url));
2949 if (*url == NULL) {
2950 error = got_error_from_errno("calloc");
2951 goto done;
2954 n = fread(*url, 1, len, f);
2955 if (n == 0 && ferror(f))
2956 error = got_ferror(f, GOT_ERR_IO);
2957 done:
2958 if (f && fclose(f) == -1 && error == NULL)
2959 error = got_error_from_errno("fclose");
2960 free(d_file);
2961 return NULL;
2964 static const struct got_error *
2965 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
2966 int limit, int tag_type)
2968 const struct got_error *error = NULL;
2969 struct got_reflist_head refs;
2970 struct got_reflist_entry *re;
2971 char *age = NULL;
2972 char *id_str = NULL, *newline, *href_commits = NULL;
2973 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
2974 struct got_tag_object *tag = NULL;
2975 enum kcgi_err kerr = KCGI_OK;
2976 int summary_header_displayed = 0, start_tag = 0, chk_next = 0;
2977 int prev_set = 0, tag_count = 0;
2979 SIMPLEQ_INIT(&refs);
2981 error = got_ref_list(&refs, gw_trans->repo, "refs/tags",
2982 got_ref_cmp_tags, gw_trans->repo);
2983 if (error)
2984 goto done;
2986 SIMPLEQ_FOREACH(re, &refs, entry) {
2987 const char *refname;
2988 const char *tagger;
2989 const char *tag_commit;
2990 time_t tagger_time;
2991 struct got_object_id *id;
2992 struct got_commit_object *commit = NULL;
2994 refname = got_ref_get_name(re->ref);
2995 if (strncmp(refname, "refs/tags/", 10) != 0)
2996 continue;
2997 refname += 10;
2999 error = got_ref_resolve(&id, gw_trans->repo, re->ref);
3000 if (error)
3001 goto done;
3003 error = got_object_open_as_tag(&tag, gw_trans->repo, id);
3004 if (error) {
3005 if (error->code != GOT_ERR_OBJ_TYPE) {
3006 free(id);
3007 goto done;
3009 /* "lightweight" tag */
3010 error = got_object_open_as_commit(&commit,
3011 gw_trans->repo, id);
3012 if (error) {
3013 free(id);
3014 goto done;
3016 tagger = got_object_commit_get_committer(commit);
3017 tagger_time =
3018 got_object_commit_get_committer_time(commit);
3019 error = got_object_id_str(&id_str, id);
3020 free(id);
3021 } else {
3022 free(id);
3023 tagger = got_object_tag_get_tagger(tag);
3024 tagger_time = got_object_tag_get_tagger_time(tag);
3025 error = got_object_id_str(&id_str,
3026 got_object_tag_get_object_id(tag));
3028 if (error)
3029 goto done;
3031 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
3032 strlen(id_str)) != 0)
3033 continue;
3035 if (tag_type == TAGBRIEF && gw_trans->commit_id &&
3036 start_tag == 0 && strncmp(id_str, header->commit_id,
3037 strlen(id_str)) != 0) {
3038 continue;
3039 } else {
3040 start_tag = 1;
3043 tag_count++;
3045 if (prev_set == 0 && start_tag == 1) {
3046 gw_trans->next_prev_id = strdup(id_str);
3047 if (gw_trans->next_prev_id == NULL) {
3048 error = got_error_from_errno("strdup");
3049 goto done;
3051 prev_set = 1;
3054 if (chk_next) {
3055 gw_trans->next_id = strdup(id_str);
3056 if (gw_trans->next_id == NULL)
3057 error = got_error_from_errno("strdup");
3058 goto done;
3061 if (commit) {
3062 error = got_object_commit_get_logmsg(&tag_commit0,
3063 commit);
3064 if (error)
3065 goto done;
3066 got_object_commit_close(commit);
3067 } else {
3068 tag_commit0 = strdup(got_object_tag_get_message(tag));
3069 if (tag_commit0 == NULL) {
3070 error = got_error_from_errno("strdup");
3071 goto done;
3075 tag_commit = tag_commit0;
3076 while (*tag_commit == '\n')
3077 tag_commit++;
3079 switch (tag_type) {
3080 case TAGBRIEF:
3081 newline = strchr(tag_commit, '\n');
3082 if (newline)
3083 *newline = '\0';
3085 if (summary_header_displayed == 0) {
3086 kerr = khtml_attr(gw_trans->gw_html_req,
3087 KELEM_DIV, KATTR_ID,
3088 "summary_tags_title_wrapper", KATTR__MAX);
3089 if (kerr != KCGI_OK)
3090 goto done;
3091 kerr = khtml_attr(gw_trans->gw_html_req,
3092 KELEM_DIV, KATTR_ID,
3093 "summary_tags_title", KATTR__MAX);
3094 if (kerr != KCGI_OK)
3095 goto done;
3096 kerr = khtml_puts(gw_trans->gw_html_req,
3097 "Tags");
3098 if (kerr != KCGI_OK)
3099 goto done;
3100 kerr = khtml_closeelem(gw_trans->gw_html_req,
3101 2);
3102 if (kerr != KCGI_OK)
3103 goto done;
3104 kerr = khtml_attr(gw_trans->gw_html_req,
3105 KELEM_DIV, KATTR_ID,
3106 "summary_tags_content", KATTR__MAX);
3107 if (kerr != KCGI_OK)
3108 goto done;
3109 summary_header_displayed = 1;
3112 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3113 KATTR_ID, "tag_wrapper", KATTR__MAX);
3114 if (kerr != KCGI_OK)
3115 goto done;
3116 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3117 KATTR_ID, "tag_age", KATTR__MAX);
3118 if (kerr != KCGI_OK)
3119 goto done;
3120 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
3121 if (error)
3122 goto done;
3123 kerr = khtml_puts(gw_trans->gw_html_req,
3124 age ? age : "");
3125 if (kerr != KCGI_OK)
3126 goto done;
3127 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3128 if (kerr != KCGI_OK)
3129 goto done;
3130 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3131 KATTR_ID, "tag", KATTR__MAX);
3132 if (kerr != KCGI_OK)
3133 goto done;
3134 kerr = khtml_puts(gw_trans->gw_html_req, refname);
3135 if (kerr != KCGI_OK)
3136 goto done;
3137 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3138 if (kerr != KCGI_OK)
3139 goto done;
3140 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3141 KATTR_ID, "tag_name", KATTR__MAX);
3142 if (kerr != KCGI_OK)
3143 goto done;
3145 href_tag = khttp_urlpart(NULL, NULL, "gotweb", "path",
3146 gw_trans->repo_name, "action", "tag", "commit",
3147 id_str, NULL);
3148 if (href_tag == NULL) {
3149 error = got_error_from_errno("khttp_urlpart");
3150 goto done;
3152 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3153 KATTR_HREF, href_tag, KATTR__MAX);
3154 if (kerr != KCGI_OK)
3155 goto done;
3156 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
3157 if (kerr != KCGI_OK)
3158 goto done;
3159 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3160 if (kerr != KCGI_OK)
3161 goto done;
3163 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3164 KATTR_ID, "navs_wrapper", KATTR__MAX);
3165 if (kerr != KCGI_OK)
3166 goto done;
3167 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3168 KATTR_ID, "navs", KATTR__MAX);
3169 if (kerr != KCGI_OK)
3170 goto done;
3172 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3173 KATTR_HREF, href_tag, KATTR__MAX);
3174 if (kerr != KCGI_OK)
3175 goto done;
3176 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
3177 if (kerr != KCGI_OK)
3178 goto done;
3179 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3180 if (kerr != KCGI_OK)
3181 goto done;
3183 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3184 if (kerr != KCGI_OK)
3185 goto done;
3187 href_briefs = khttp_urlpart(NULL, NULL, "gotweb",
3188 "path", gw_trans->repo_name, "action", "briefs",
3189 "commit", id_str, NULL);
3190 if (href_briefs == NULL) {
3191 error = got_error_from_errno("khttp_urlpart");
3192 goto done;
3194 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3195 KATTR_HREF, href_briefs, KATTR__MAX);
3196 if (kerr != KCGI_OK)
3197 goto done;
3198 kerr = khtml_puts(gw_trans->gw_html_req,
3199 "commit briefs");
3200 if (kerr != KCGI_OK)
3201 goto done;
3202 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3203 if (kerr != KCGI_OK)
3204 goto done;
3206 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3207 if (kerr != KCGI_OK)
3208 goto done;
3210 href_commits = khttp_urlpart(NULL, NULL, "gotweb",
3211 "path", gw_trans->repo_name, "action", "commits",
3212 "commit", id_str, NULL);
3213 if (href_commits == NULL) {
3214 error = got_error_from_errno("khttp_urlpart");
3215 goto done;
3217 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3218 KATTR_HREF, href_commits, KATTR__MAX);
3219 if (kerr != KCGI_OK)
3220 goto done;
3221 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
3222 if (kerr != KCGI_OK)
3223 goto done;
3224 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3225 if (kerr != KCGI_OK)
3226 goto done;
3228 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3229 KATTR_ID, "dotted_line", KATTR__MAX);
3230 if (kerr != KCGI_OK)
3231 goto done;
3232 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3233 if (kerr != KCGI_OK)
3234 goto done;
3235 break;
3236 case TAGFULL:
3237 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3238 KATTR_ID, "tag_info_date_title", KATTR__MAX);
3239 if (kerr != KCGI_OK)
3240 goto done;
3241 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
3242 if (kerr != KCGI_OK)
3243 goto done;
3244 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3245 if (kerr != KCGI_OK)
3246 goto done;
3247 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3248 KATTR_ID, "tag_info_date", KATTR__MAX);
3249 if (kerr != KCGI_OK)
3250 goto done;
3251 error = gw_get_time_str(&age, tagger_time, TM_LONG);
3252 if (error)
3253 goto done;
3254 kerr = khtml_puts(gw_trans->gw_html_req,
3255 age ? age : "");
3256 if (kerr != KCGI_OK)
3257 goto done;
3258 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3259 if (kerr != KCGI_OK)
3260 goto done;
3262 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3263 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
3264 if (kerr != KCGI_OK)
3265 goto done;
3266 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
3267 if (kerr != KCGI_OK)
3268 goto done;
3269 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3270 if (kerr != KCGI_OK)
3271 goto done;
3272 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3273 KATTR_ID, "tag_info_date", KATTR__MAX);
3274 if (kerr != KCGI_OK)
3275 goto done;
3276 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
3277 if (kerr != KCGI_OK)
3278 goto done;
3279 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3280 if (kerr != KCGI_OK)
3281 goto done;
3283 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3284 KATTR_ID, "tag_info", KATTR__MAX);
3285 if (kerr != KCGI_OK)
3286 goto done;
3287 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
3288 if (kerr != KCGI_OK)
3289 goto done;
3290 break;
3291 default:
3292 break;
3294 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3295 if (kerr != KCGI_OK)
3296 goto done;
3298 if (limit && --limit == 0)
3299 chk_next = 1;
3301 if (tag)
3302 got_object_tag_close(tag);
3303 tag = NULL;
3304 free(id_str);
3305 id_str = NULL;
3306 free(age);
3307 age = NULL;
3308 free(tag_commit0);
3309 tag_commit0 = NULL;
3310 free(href_tag);
3311 href_tag = NULL;
3312 free(href_briefs);
3313 href_briefs = NULL;
3314 free(href_commits);
3315 href_commits = NULL;
3317 if (tag_count == 0) {
3318 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3319 "summary_tags_title_wrapper", KATTR__MAX);
3320 if (kerr != KCGI_OK)
3321 goto done;
3322 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3323 "summary_tags_title", KATTR__MAX);
3324 if (kerr != KCGI_OK)
3325 goto done;
3326 kerr = khtml_puts(gw_trans->gw_html_req, "Tags");
3327 if (kerr != KCGI_OK)
3328 goto done;
3329 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3330 if (kerr != KCGI_OK)
3331 goto done;
3332 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3333 "summary_tags_content", KATTR__MAX);
3334 if (kerr != KCGI_OK)
3335 goto done;
3336 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3337 "tags_info", KATTR__MAX);
3338 if (kerr != KCGI_OK)
3339 goto done;
3340 kerr = khttp_puts(gw_trans->gw_req,
3341 "There are no tags for this repo.");
3342 if (kerr != KCGI_OK)
3343 goto done;
3344 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3346 done:
3347 if (tag)
3348 got_object_tag_close(tag);
3349 free(id_str);
3350 free(age);
3351 free(tag_commit0);
3352 free(href_tag);
3353 free(href_briefs);
3354 free(href_commits);
3355 got_ref_list_free(&refs);
3356 if (error == NULL && kerr != KCGI_OK)
3357 error = gw_kcgi_error(kerr);
3358 return error;
3361 static void
3362 gw_free_header(struct gw_header *header)
3364 free(header->path);
3365 free(header->author);
3366 free(header->committer);
3367 free(header->refs_str);
3368 free(header->commit_id);
3369 free(header->parent_id);
3370 free(header->tree_id);
3371 free(header->commit_msg);
3374 static struct gw_header *
3375 gw_init_header()
3377 struct gw_header *header;
3379 header = malloc(sizeof(*header));
3380 if (header == NULL)
3381 return NULL;
3383 header->path = NULL;
3384 SIMPLEQ_INIT(&header->refs);
3386 header->refs_str = NULL;
3387 header->commit_id = NULL;
3388 header->committer = NULL;
3389 header->author = NULL;
3390 header->parent_id = NULL;
3391 header->tree_id = NULL;
3392 header->commit_msg = NULL;
3394 return header;
3397 static const struct got_error *
3398 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
3399 int limit, struct got_object_id *id)
3401 const struct got_error *error = NULL;
3402 struct got_commit_graph *graph = NULL;
3403 struct got_commit_object *commit = NULL;
3404 int chk_next = 0, chk_multi = 0, prev_set = 0;
3406 error = got_commit_graph_open(&graph, header->path, 0);
3407 if (error)
3408 return error;
3410 error = got_commit_graph_iter_start(graph, id, gw_trans->repo, NULL,
3411 NULL);
3412 if (error)
3413 goto done;
3415 for (;;) {
3416 error = got_commit_graph_iter_next(&id, graph, gw_trans->repo,
3417 NULL, NULL);
3418 if (error) {
3419 if (error->code == GOT_ERR_ITER_COMPLETED)
3420 error = NULL;
3421 goto done;
3423 if (id == NULL)
3424 goto done;
3426 error = got_object_open_as_commit(&commit, gw_trans->repo, id);
3427 if (error)
3428 goto done;
3429 if (limit == 1 && chk_multi == 0 &&
3430 gw_trans->gw_conf->got_max_commits_display != 1) {
3431 error = gw_get_commit(gw_trans, header, commit, id);
3432 if (error)
3433 goto done;
3434 } else {
3435 chk_multi = 1;
3436 struct gw_header *n_header = NULL;
3437 if ((n_header = gw_init_header()) == NULL) {
3438 error = got_error_from_errno("malloc");
3439 goto done;
3441 error = got_ref_list(&n_header->refs, gw_trans->repo,
3442 NULL, got_ref_cmp_by_name, NULL);
3443 if (error)
3444 goto done;
3446 error = gw_get_commit(gw_trans, n_header, commit, id);
3447 if (error)
3448 goto done;
3449 got_ref_list_free(&n_header->refs);
3452 * we have a commit_id now, so copy it to next_prev_id
3453 * for navigation through gw_briefs and gw_commits
3455 if (gw_trans->next_prev_id == NULL && prev_set == 0 &&
3456 (gw_trans->action == GW_BRIEFS ||
3457 gw_trans->action == GW_COMMITS ||
3458 gw_trans->action == GW_SUMMARY)) {
3459 prev_set = 1;
3460 gw_trans->next_prev_id =
3461 strdup(n_header->commit_id);
3462 if (gw_trans->next_prev_id == NULL) {
3463 error = got_error_from_errno("strdup");
3464 goto done;
3469 * check for one more commit before breaking,
3470 * so we know whether to navicate through gw_briefs
3471 * gw_commits and gw_summary
3473 if (chk_next && (gw_trans->action == GW_BRIEFS||
3474 gw_trans->action == GW_COMMITS ||
3475 gw_trans->action == GW_SUMMARY)) {
3476 gw_trans->next_id = strdup(n_header->commit_id);
3477 if (gw_trans->next_id == NULL)
3478 error = got_error_from_errno("strdup");
3479 goto done;
3482 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3483 entry);
3485 if (error || (limit && --limit == 0)) {
3486 if (chk_multi == 0)
3487 break;
3488 chk_next = 1;
3491 done:
3492 if (commit != NULL)
3493 got_object_commit_close(commit);
3494 if (graph)
3495 got_commit_graph_close(graph);
3496 return error;
3499 static const struct got_error *
3500 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header,
3501 struct got_commit_object *commit, struct got_object_id *id)
3503 const struct got_error *error = NULL;
3504 struct got_reflist_entry *re;
3505 struct got_object_id *id2 = NULL;
3506 struct got_object_qid *parent_id;
3507 char *commit_msg = NULL, *commit_msg0;
3509 /*print commit*/
3510 SIMPLEQ_FOREACH(re, &header->refs, entry) {
3511 char *s;
3512 const char *name;
3513 struct got_tag_object *tag = NULL;
3514 int cmp;
3516 if (got_ref_is_symbolic(re->ref))
3517 continue;
3519 name = got_ref_get_name(re->ref);
3520 if (strncmp(name, "refs/", 5) == 0)
3521 name += 5;
3522 if (strncmp(name, "got/", 4) == 0)
3523 continue;
3524 if (strncmp(name, "heads/", 6) == 0)
3525 name += 6;
3526 if (strncmp(name, "remotes/", 8) == 0)
3527 name += 8;
3528 if (strncmp(name, "tags/", 5) == 0) {
3529 error = got_object_open_as_tag(&tag, gw_trans->repo,
3530 re->id);
3531 if (error) {
3532 if (error->code != GOT_ERR_OBJ_TYPE)
3533 continue;
3535 * Ref points at something other
3536 * than a tag.
3538 error = NULL;
3539 tag = NULL;
3542 cmp = got_object_id_cmp(tag ?
3543 got_object_tag_get_object_id(tag) : re->id, id);
3544 if (tag)
3545 got_object_tag_close(tag);
3546 if (cmp != 0)
3547 continue;
3548 s = header->refs_str;
3549 if (asprintf(&header->refs_str, "%s%s%s", s ? s : "",
3550 s ? ", " : "", name) == -1) {
3551 error = got_error_from_errno("asprintf");
3552 free(s);
3553 header->refs_str = NULL;
3554 return error;
3556 free(s);
3559 error = got_object_id_str(&header->commit_id, id);
3560 if (error)
3561 return error;
3563 error = got_object_id_str(&header->tree_id,
3564 got_object_commit_get_tree_id(commit));
3565 if (error)
3566 return error;
3568 if (gw_trans->action == GW_DIFF) {
3569 parent_id = SIMPLEQ_FIRST(
3570 got_object_commit_get_parent_ids(commit));
3571 if (parent_id != NULL) {
3572 id2 = got_object_id_dup(parent_id->id);
3573 free (parent_id);
3574 error = got_object_id_str(&header->parent_id, id2);
3575 if (error)
3576 return error;
3577 free(id2);
3578 } else {
3579 header->parent_id = strdup("/dev/null");
3580 if (header->parent_id == NULL) {
3581 error = got_error_from_errno("strdup");
3582 return error;
3587 header->committer_time =
3588 got_object_commit_get_committer_time(commit);
3590 header->author =
3591 strdup(got_object_commit_get_author(commit));
3592 if (header->author == NULL) {
3593 error = got_error_from_errno("strdup");
3594 return error;
3596 header->committer =
3597 strdup(got_object_commit_get_committer(commit));
3598 if (header->committer == NULL) {
3599 error = got_error_from_errno("strdup");
3600 return error;
3602 error = got_object_commit_get_logmsg(&commit_msg0, commit);
3603 if (error)
3604 return error;
3606 commit_msg = commit_msg0;
3607 while (*commit_msg == '\n')
3608 commit_msg++;
3610 header->commit_msg = strdup(commit_msg);
3611 if (header->commit_msg == NULL)
3612 error = got_error_from_errno("strdup");
3613 free(commit_msg0);
3614 return error;
3617 static const struct got_error *
3618 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3620 const struct got_error *error = NULL;
3621 char *in_repo_path = NULL;
3622 struct got_object_id *id = NULL;
3624 error = got_repo_open(&gw_trans->repo, gw_trans->repo_path, NULL);
3625 if (error)
3626 return error;
3628 if (gw_trans->commit_id == NULL) {
3629 struct got_reference *head_ref;
3630 error = got_ref_open(&head_ref, gw_trans->repo,
3631 gw_trans->headref, 0);
3632 if (error)
3633 return error;
3635 error = got_ref_resolve(&id, gw_trans->repo, head_ref);
3636 got_ref_close(head_ref);
3637 if (error)
3638 return error;
3639 } else {
3640 struct got_reference *ref;
3642 error = got_ref_open(&ref, gw_trans->repo,
3643 gw_trans->commit_id, 0);
3644 if (error == NULL) {
3645 int obj_type;
3646 error = got_ref_resolve(&id, gw_trans->repo, ref);
3647 got_ref_close(ref);
3648 if (error)
3649 return error;
3650 error = got_object_get_type(&obj_type, gw_trans->repo,
3651 id);
3652 if (error)
3653 goto done;
3654 if (obj_type == GOT_OBJ_TYPE_TAG) {
3655 struct got_tag_object *tag;
3656 error = got_object_open_as_tag(&tag,
3657 gw_trans->repo, id);
3658 if (error)
3659 goto done;
3660 if (got_object_tag_get_object_type(tag) !=
3661 GOT_OBJ_TYPE_COMMIT) {
3662 got_object_tag_close(tag);
3663 error = got_error(GOT_ERR_OBJ_TYPE);
3664 goto done;
3666 free(id);
3667 id = got_object_id_dup(
3668 got_object_tag_get_object_id(tag));
3669 if (id == NULL)
3670 error = got_error_from_errno(
3671 "got_object_id_dup");
3672 got_object_tag_close(tag);
3673 if (error)
3674 goto done;
3675 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3676 error = got_error(GOT_ERR_OBJ_TYPE);
3677 goto done;
3680 error = got_repo_match_object_id_prefix(&id,
3681 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT,
3682 gw_trans->repo);
3683 if (error)
3684 goto done;
3687 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
3688 gw_trans->repo_path, 1);
3689 if (error)
3690 goto done;
3692 if (in_repo_path) {
3693 header->path = strdup(in_repo_path);
3694 if (header->path == NULL) {
3695 error = got_error_from_errno("strdup");
3696 goto done;
3700 error = got_ref_list(&header->refs, gw_trans->repo, NULL,
3701 got_ref_cmp_by_name, NULL);
3702 if (error)
3703 goto done;
3705 error = gw_get_commits(gw_trans, header, limit, id);
3706 done:
3707 got_ref_list_free(&header->refs);
3708 free(id);
3709 free(in_repo_path);
3710 return error;
3713 struct blame_line {
3714 int annotated;
3715 char *id_str;
3716 char *committer;
3717 char datebuf[11]; /* YYYY-MM-DD + NUL */
3720 struct gw_blame_cb_args {
3721 struct blame_line *lines;
3722 int nlines;
3723 int nlines_prec;
3724 int lineno_cur;
3725 off_t *line_offsets;
3726 FILE *f;
3727 struct got_repository *repo;
3728 struct gw_trans *gw_trans;
3731 static const struct got_error *
3732 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3734 const struct got_error *err = NULL;
3735 struct gw_blame_cb_args *a = arg;
3736 struct blame_line *bline;
3737 char *line = NULL;
3738 size_t linesize = 0;
3739 struct got_commit_object *commit = NULL;
3740 off_t offset;
3741 struct tm tm;
3742 time_t committer_time;
3743 enum kcgi_err kerr = KCGI_OK;
3745 if (nlines != a->nlines ||
3746 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3747 return got_error(GOT_ERR_RANGE);
3749 if (lineno == -1)
3750 return NULL; /* no change in this commit */
3752 /* Annotate this line. */
3753 bline = &a->lines[lineno - 1];
3754 if (bline->annotated)
3755 return NULL;
3756 err = got_object_id_str(&bline->id_str, id);
3757 if (err)
3758 return err;
3760 err = got_object_open_as_commit(&commit, a->repo, id);
3761 if (err)
3762 goto done;
3764 bline->committer = strdup(got_object_commit_get_committer(commit));
3765 if (bline->committer == NULL) {
3766 err = got_error_from_errno("strdup");
3767 goto done;
3770 committer_time = got_object_commit_get_committer_time(commit);
3771 if (localtime_r(&committer_time, &tm) == NULL)
3772 return got_error_from_errno("localtime_r");
3773 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3774 &tm) >= sizeof(bline->datebuf)) {
3775 err = got_error(GOT_ERR_NO_SPACE);
3776 goto done;
3778 bline->annotated = 1;
3780 /* Print lines annotated so far. */
3781 bline = &a->lines[a->lineno_cur - 1];
3782 if (!bline->annotated)
3783 goto done;
3785 offset = a->line_offsets[a->lineno_cur - 1];
3786 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3787 err = got_error_from_errno("fseeko");
3788 goto done;
3791 while (bline->annotated) {
3792 char *smallerthan, *at, *nl, *committer;
3793 char *href_diff = NULL;
3794 size_t len;
3796 if (getline(&line, &linesize, a->f) == -1) {
3797 if (ferror(a->f))
3798 err = got_error_from_errno("getline");
3799 break;
3802 committer = bline->committer;
3803 smallerthan = strchr(committer, '<');
3804 if (smallerthan && smallerthan[1] != '\0')
3805 committer = smallerthan + 1;
3806 at = strchr(committer, '@');
3807 if (at)
3808 *at = '\0';
3809 len = strlen(committer);
3810 if (len >= 9)
3811 committer[8] = '\0';
3813 nl = strchr(line, '\n');
3814 if (nl)
3815 *nl = '\0';
3817 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3818 "blame_wrapper", KATTR__MAX);
3819 if (kerr != KCGI_OK)
3820 goto err;
3821 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3822 "blame_number", KATTR__MAX);
3823 if (kerr != KCGI_OK)
3824 goto err;
3825 kerr = khtml_printf(a->gw_trans->gw_html_req, "%.*d",
3826 a->nlines_prec, a->lineno_cur);
3827 if (kerr != KCGI_OK)
3828 goto err;
3829 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3830 if (kerr != KCGI_OK)
3831 goto err;
3833 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3834 "blame_hash", KATTR__MAX);
3835 if (kerr != KCGI_OK)
3836 goto err;
3838 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
3839 a->gw_trans->repo_name, "action", "diff", "commit",
3840 bline->id_str, NULL);
3841 if (href_diff == NULL) {
3842 err = got_error_from_errno("khttp_urlpart");
3843 goto done;
3845 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3846 KATTR_HREF, href_diff, KATTR__MAX);
3847 if (kerr != KCGI_OK)
3848 goto done;
3849 kerr = khtml_printf(a->gw_trans->gw_html_req, "%.8s",
3850 bline->id_str);
3851 if (kerr != KCGI_OK)
3852 goto err;
3853 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3854 if (kerr != KCGI_OK)
3855 goto err;
3857 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3858 "blame_date", KATTR__MAX);
3859 if (kerr != KCGI_OK)
3860 goto err;
3861 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3862 if (kerr != KCGI_OK)
3863 goto err;
3864 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3865 if (kerr != KCGI_OK)
3866 goto err;
3868 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3869 "blame_author", KATTR__MAX);
3870 if (kerr != KCGI_OK)
3871 goto err;
3872 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
3873 if (kerr != KCGI_OK)
3874 goto err;
3875 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3876 if (kerr != KCGI_OK)
3877 goto err;
3879 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3880 "blame_code", KATTR__MAX);
3881 if (kerr != KCGI_OK)
3882 goto err;
3883 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
3884 if (kerr != KCGI_OK)
3885 goto err;
3886 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3887 if (kerr != KCGI_OK)
3888 goto err;
3890 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3891 if (kerr != KCGI_OK)
3892 goto err;
3894 a->lineno_cur++;
3895 bline = &a->lines[a->lineno_cur - 1];
3896 err:
3897 free(href_diff);
3899 done:
3900 if (commit)
3901 got_object_commit_close(commit);
3902 free(line);
3903 if (err == NULL && kerr != KCGI_OK)
3904 err = gw_kcgi_error(kerr);
3905 return err;
3908 static const struct got_error *
3909 gw_output_file_blame(struct gw_trans *gw_trans)
3911 const struct got_error *error = NULL;
3912 struct got_object_id *obj_id = NULL;
3913 struct got_object_id *commit_id = NULL;
3914 struct got_blob_object *blob = NULL;
3915 char *path = NULL, *in_repo_path = NULL;
3916 struct gw_blame_cb_args bca;
3917 int i, obj_type;
3918 size_t filesize;
3920 if (asprintf(&path, "%s%s%s",
3921 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3922 gw_trans->repo_folder ? "/" : "",
3923 gw_trans->repo_file) == -1) {
3924 error = got_error_from_errno("asprintf");
3925 goto done;
3928 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path, 1);
3929 if (error)
3930 goto done;
3932 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
3933 GOT_OBJ_TYPE_COMMIT, 1, gw_trans->repo);
3934 if (error)
3935 goto done;
3937 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
3938 in_repo_path);
3939 if (error)
3940 goto done;
3942 if (obj_id == NULL) {
3943 error = got_error(GOT_ERR_NO_OBJ);
3944 goto done;
3947 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
3948 if (error)
3949 goto done;
3951 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3952 error = got_error(GOT_ERR_OBJ_TYPE);
3953 goto done;
3956 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
3957 if (error)
3958 goto done;
3960 bca.f = got_opentemp();
3961 if (bca.f == NULL) {
3962 error = got_error_from_errno("got_opentemp");
3963 goto done;
3965 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3966 &bca.line_offsets, bca.f, blob);
3967 if (error || bca.nlines == 0)
3968 goto done;
3970 /* Don't include \n at EOF in the blame line count. */
3971 if (bca.line_offsets[bca.nlines - 1] == filesize)
3972 bca.nlines--;
3974 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3975 if (bca.lines == NULL) {
3976 error = got_error_from_errno("calloc");
3977 goto done;
3979 bca.lineno_cur = 1;
3980 bca.nlines_prec = 0;
3981 i = bca.nlines;
3982 while (i > 0) {
3983 i /= 10;
3984 bca.nlines_prec++;
3986 bca.repo = gw_trans->repo;
3987 bca.gw_trans = gw_trans;
3989 error = got_blame(in_repo_path, commit_id, gw_trans->repo, gw_blame_cb,
3990 &bca, NULL, NULL);
3991 done:
3992 free(in_repo_path);
3993 free(commit_id);
3994 free(obj_id);
3995 free(path);
3997 if (blob) {
3998 free(bca.line_offsets);
3999 for (i = 0; i < bca.nlines; i++) {
4000 struct blame_line *bline = &bca.lines[i];
4001 free(bline->id_str);
4002 free(bline->committer);
4004 free(bca.lines);
4005 if (bca.f && fclose(bca.f) == EOF && error == NULL)
4006 error = got_error_from_errno("fclose");
4008 if (blob)
4009 got_object_blob_close(blob);
4010 return error;
4013 static const struct got_error *
4014 gw_output_blob_buf(struct gw_trans *gw_trans)
4016 const struct got_error *error = NULL;
4017 struct got_object_id *obj_id = NULL;
4018 struct got_object_id *commit_id = NULL;
4019 struct got_blob_object *blob = NULL;
4020 char *path = NULL, *in_repo_path = NULL;
4021 int obj_type, set_mime = 0;
4022 size_t len, hdrlen;
4023 const uint8_t *buf;
4024 enum kcgi_err kerr = KCGI_OK;
4026 if (asprintf(&path, "%s%s%s",
4027 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4028 gw_trans->repo_folder ? "/" : "",
4029 gw_trans->repo_file) == -1) {
4030 error = got_error_from_errno("asprintf");
4031 goto done;
4034 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path, 1);
4035 if (error)
4036 goto done;
4038 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
4039 GOT_OBJ_TYPE_COMMIT, 1, gw_trans->repo);
4040 if (error)
4041 goto done;
4043 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
4044 in_repo_path);
4045 if (error)
4046 goto done;
4048 if (obj_id == NULL) {
4049 error = got_error(GOT_ERR_NO_OBJ);
4050 goto done;
4053 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
4054 if (error)
4055 goto done;
4057 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4058 error = got_error(GOT_ERR_OBJ_TYPE);
4059 goto done;
4062 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
4063 if (error)
4064 goto done;
4066 hdrlen = got_object_blob_get_hdrlen(blob);
4067 do {
4068 error = got_object_blob_read_block(&len, blob);
4069 if (error)
4070 goto done;
4071 buf = got_object_blob_get_read_buf(blob);
4074 * Skip blob object header first time around,
4075 * which also contains a zero byte.
4077 buf += hdrlen;
4078 if (set_mime == 0) {
4079 if (isbinary(buf, len - hdrlen))
4080 gw_trans->mime = KMIME_APP_OCTET_STREAM;
4081 else
4082 gw_trans->mime = KMIME_TEXT_PLAIN;
4083 set_mime = 1;
4084 error = gw_display_index(gw_trans);
4085 if (error)
4086 goto done;
4088 kerr = khttp_write(gw_trans->gw_req, buf, len - hdrlen);
4089 if (kerr != KCGI_OK)
4090 goto done;
4091 hdrlen = 0;
4092 } while (len != 0);
4093 done:
4094 free(in_repo_path);
4095 free(commit_id);
4096 free(obj_id);
4097 free(path);
4098 if (blob)
4099 got_object_blob_close(blob);
4100 if (error == NULL && kerr != KCGI_OK)
4101 error = gw_kcgi_error(kerr);
4102 return error;
4105 static const struct got_error *
4106 gw_output_repo_tree(struct gw_trans *gw_trans)
4108 const struct got_error *error = NULL;
4109 struct got_object_id *tree_id = NULL, *commit_id = NULL;
4110 struct got_tree_object *tree = NULL;
4111 char *path = NULL, *in_repo_path = NULL;
4112 char *id_str = NULL;
4113 char *build_folder = NULL;
4114 char *href_blob = NULL, *href_blame = NULL;
4115 const char *class = NULL;
4116 int nentries, i, class_flip = 0;
4117 enum kcgi_err kerr = KCGI_OK;
4119 if (gw_trans->repo_folder != NULL) {
4120 path = strdup(gw_trans->repo_folder);
4121 if (path == NULL) {
4122 error = got_error_from_errno("strdup");
4123 goto done;
4125 } else {
4126 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
4127 gw_trans->repo_path, 1);
4128 if (error)
4129 goto done;
4130 free(path);
4131 path = in_repo_path;
4134 if (gw_trans->commit_id == NULL) {
4135 struct got_reference *head_ref;
4136 error = got_ref_open(&head_ref, gw_trans->repo,
4137 gw_trans->headref, 0);
4138 if (error)
4139 goto done;
4140 error = got_ref_resolve(&commit_id, gw_trans->repo, head_ref);
4141 if (error)
4142 goto done;
4143 got_ref_close(head_ref);
4145 * gw_trans->commit_id was not parsed from the querystring
4146 * we hit this code path from gw_index, where we don't know the
4147 * commit values for the tree link yet, so set
4148 * gw_trans->commit_id here to continue further into the tree
4150 error = got_object_id_str(&gw_trans->commit_id, commit_id);
4151 if (error)
4152 goto done;
4154 } else {
4155 error = got_repo_match_object_id(&commit_id, NULL,
4156 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT, 1,
4157 gw_trans->repo);
4158 if (error)
4159 goto done;
4162 error = got_object_id_by_path(&tree_id, gw_trans->repo, commit_id,
4163 path);
4164 if (error)
4165 goto done;
4167 error = got_object_open_as_tree(&tree, gw_trans->repo, tree_id);
4168 if (error)
4169 goto done;
4171 nentries = got_object_tree_get_nentries(tree);
4172 for (i = 0; i < nentries; i++) {
4173 struct got_tree_entry *te;
4174 const char *modestr = "";
4175 mode_t mode;
4177 te = got_object_tree_get_entry(tree, i);
4179 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
4180 if (error)
4181 goto done;
4183 mode = got_tree_entry_get_mode(te);
4184 if (got_object_tree_entry_is_submodule(te))
4185 modestr = "$";
4186 else if (S_ISLNK(mode))
4187 modestr = "@";
4188 else if (S_ISDIR(mode))
4189 modestr = "/";
4190 else if (mode & S_IXUSR)
4191 modestr = "*";
4193 if (class_flip == 0) {
4194 class = "back_lightgray";
4195 class_flip = 1;
4196 } else {
4197 class = "back_white";
4198 class_flip = 0;
4201 if (S_ISDIR(mode)) {
4202 if (asprintf(&build_folder, "%s/%s",
4203 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4204 got_tree_entry_get_name(te)) == -1) {
4205 error = got_error_from_errno("asprintf");
4206 goto done;
4209 href_blob = khttp_urlpart(NULL, NULL, "gotweb", "path",
4210 gw_trans->repo_name, "action",
4211 gw_get_action_name(gw_trans), "commit",
4212 gw_trans->commit_id, "folder", build_folder, NULL);
4213 if (href_blob == NULL) {
4214 error = got_error_from_errno("khttp_urlpart");
4215 goto done;
4217 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4218 KATTR_ID, "tree_wrapper", KATTR__MAX);
4219 if (kerr != KCGI_OK)
4220 goto done;
4221 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4222 KATTR_ID, "tree_line", KATTR_CLASS, class,
4223 KATTR__MAX);
4224 if (kerr != KCGI_OK)
4225 goto done;
4226 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4227 KATTR_HREF, href_blob, KATTR_CLASS,
4228 "diff_directory", KATTR__MAX);
4229 if (kerr != KCGI_OK)
4230 goto done;
4231 kerr = khtml_printf(gw_trans->gw_html_req, "%s%s",
4232 got_tree_entry_get_name(te), modestr);
4233 if (kerr != KCGI_OK)
4234 goto done;
4235 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4236 if (kerr != KCGI_OK)
4237 goto done;
4238 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4239 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
4240 KATTR__MAX);
4241 if (kerr != KCGI_OK)
4242 goto done;
4243 kerr = khtml_entity(gw_trans->gw_html_req,
4244 KENTITY_nbsp);
4245 if (kerr != KCGI_OK)
4246 goto done;
4247 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4248 if (kerr != KCGI_OK)
4249 goto done;
4250 } else {
4251 href_blob = khttp_urlpart(NULL, NULL, "gotweb", "path",
4252 gw_trans->repo_name, "action", "blob", "commit",
4253 gw_trans->commit_id, "file",
4254 got_tree_entry_get_name(te), "folder",
4255 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4256 NULL);
4257 if (href_blob == NULL) {
4258 error = got_error_from_errno("khttp_urlpart");
4259 goto done;
4261 href_blame = khttp_urlpart(NULL, NULL, "gotweb", "path",
4262 gw_trans->repo_name, "action", "blame", "commit",
4263 gw_trans->commit_id, "file",
4264 got_tree_entry_get_name(te), "folder",
4265 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4266 NULL);
4267 if (href_blame == NULL) {
4268 error = got_error_from_errno("khttp_urlpart");
4269 goto done;
4271 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4272 KATTR_ID, "tree_wrapper", KATTR__MAX);
4273 if (kerr != KCGI_OK)
4274 goto done;
4275 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4276 KATTR_ID, "tree_line", KATTR_CLASS, class,
4277 KATTR__MAX);
4278 if (kerr != KCGI_OK)
4279 goto done;
4280 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4281 KATTR_HREF, href_blob, KATTR__MAX);
4282 if (kerr != KCGI_OK)
4283 goto done;
4284 kerr = khtml_printf(gw_trans->gw_html_req, "%s%s",
4285 got_tree_entry_get_name(te), modestr);
4286 if (kerr != KCGI_OK)
4287 goto done;
4288 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4289 if (kerr != KCGI_OK)
4290 goto done;
4291 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4292 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
4293 KATTR__MAX);
4294 if (kerr != KCGI_OK)
4295 goto done;
4297 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4298 KATTR_HREF, href_blob, KATTR__MAX);
4299 if (kerr != KCGI_OK)
4300 goto done;
4301 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
4302 if (kerr != KCGI_OK)
4303 goto done;
4304 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4305 if (kerr != KCGI_OK)
4306 goto done;
4308 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4309 if (kerr != KCGI_OK)
4310 goto done;
4312 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4313 KATTR_HREF, href_blame, KATTR__MAX);
4314 if (kerr != KCGI_OK)
4315 goto done;
4316 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
4317 if (kerr != KCGI_OK)
4318 goto done;
4320 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4321 if (kerr != KCGI_OK)
4322 goto done;
4324 free(id_str);
4325 id_str = NULL;
4326 free(href_blob);
4327 href_blob = NULL;
4328 free(build_folder);
4329 build_folder = NULL;
4331 done:
4332 if (tree)
4333 got_object_tree_close(tree);
4334 free(id_str);
4335 free(href_blob);
4336 free(href_blame);
4337 free(in_repo_path);
4338 free(tree_id);
4339 free(build_folder);
4340 if (error == NULL && kerr != KCGI_OK)
4341 error = gw_kcgi_error(kerr);
4342 return error;
4345 static const struct got_error *
4346 gw_output_repo_heads(struct gw_trans *gw_trans)
4348 const struct got_error *error = NULL;
4349 struct got_reflist_head refs;
4350 struct got_reflist_entry *re;
4351 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
4352 char *href_commits = NULL;
4353 enum kcgi_err kerr = KCGI_OK;
4355 SIMPLEQ_INIT(&refs);
4357 error = got_ref_list(&refs, gw_trans->repo, "refs/heads",
4358 got_ref_cmp_by_name, NULL);
4359 if (error)
4360 goto done;
4362 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4363 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
4364 if (kerr != KCGI_OK)
4365 goto done;
4366 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4367 KATTR_ID, "summary_heads_title", KATTR__MAX);
4368 if (kerr != KCGI_OK)
4369 goto done;
4370 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
4371 if (kerr != KCGI_OK)
4372 goto done;
4373 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4374 if (kerr != KCGI_OK)
4375 goto done;
4376 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4377 KATTR_ID, "summary_heads_content", KATTR__MAX);
4378 if (kerr != KCGI_OK)
4379 goto done;
4381 SIMPLEQ_FOREACH(re, &refs, entry) {
4382 const char *refname;
4384 if (got_ref_is_symbolic(re->ref))
4385 continue;
4387 refname = got_ref_get_name(re->ref);
4388 if (strncmp(refname, "refs/heads/", 11) != 0)
4389 continue;
4391 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
4392 refname, TM_DIFF);
4393 if (error)
4394 goto done;
4396 if (strncmp(refname, "refs/heads/", 11) == 0)
4397 refname += 11;
4399 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4400 KATTR_ID, "heads_wrapper", KATTR__MAX);
4401 if (kerr != KCGI_OK)
4402 goto done;
4403 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4404 KATTR_ID, "heads_age", KATTR__MAX);
4405 if (kerr != KCGI_OK)
4406 goto done;
4407 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
4408 if (kerr != KCGI_OK)
4409 goto done;
4410 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4411 if (kerr != KCGI_OK)
4412 goto done;
4413 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4414 KATTR_ID, "heads_space", KATTR__MAX);
4415 if (kerr != KCGI_OK)
4416 goto done;
4417 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
4418 if (kerr != KCGI_OK)
4419 goto done;
4420 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4421 if (kerr != KCGI_OK)
4422 goto done;
4423 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4424 KATTR_ID, "head", KATTR__MAX);
4425 if (kerr != KCGI_OK)
4426 goto done;
4428 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
4429 gw_trans->repo_name, "action", "summary", "headref",
4430 refname, NULL);
4431 if (href_summary == NULL) {
4432 error = got_error_from_errno("khttp_urlpart");
4433 goto done;
4435 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4436 href_summary, KATTR__MAX);
4437 kerr = khtml_puts(gw_trans->gw_html_req, refname);
4438 if (kerr != KCGI_OK)
4439 goto done;
4440 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4441 if (kerr != KCGI_OK)
4442 goto done;
4444 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4445 "navs_wrapper", KATTR__MAX);
4446 if (kerr != KCGI_OK)
4447 goto done;
4448 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4449 "navs", KATTR__MAX);
4450 if (kerr != KCGI_OK)
4451 goto done;
4453 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4454 href_summary, KATTR__MAX);
4455 if (kerr != KCGI_OK)
4456 goto done;
4457 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4458 if (kerr != KCGI_OK)
4459 goto done;
4460 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4461 if (kerr != KCGI_OK)
4462 goto done;
4464 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4465 if (kerr != KCGI_OK)
4466 goto done;
4468 href_briefs = khttp_urlpart(NULL, NULL, "gotweb", "path",
4469 gw_trans->repo_name, "action", "briefs", "headref",
4470 refname, NULL);
4471 if (href_briefs == NULL) {
4472 error = got_error_from_errno("khttp_urlpart");
4473 goto done;
4475 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4476 href_briefs, KATTR__MAX);
4477 if (kerr != KCGI_OK)
4478 goto done;
4479 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4480 if (kerr != KCGI_OK)
4481 goto done;
4482 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4483 if (kerr != KCGI_OK)
4484 goto done;
4486 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4487 if (kerr != KCGI_OK)
4488 goto done;
4490 href_commits = khttp_urlpart(NULL, NULL, "gotweb", "path",
4491 gw_trans->repo_name, "action", "commits", "headref",
4492 refname, NULL);
4493 if (href_commits == NULL) {
4494 error = got_error_from_errno("khttp_urlpart");
4495 goto done;
4497 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4498 href_commits, KATTR__MAX);
4499 if (kerr != KCGI_OK)
4500 goto done;
4501 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4502 if (kerr != KCGI_OK)
4503 goto done;
4504 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4505 if (kerr != KCGI_OK)
4506 goto done;
4508 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4509 "dotted_line", KATTR__MAX);
4510 if (kerr != KCGI_OK)
4511 goto done;
4512 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4513 if (kerr != KCGI_OK)
4514 goto done;
4515 free(href_summary);
4516 href_summary = NULL;
4517 free(href_briefs);
4518 href_briefs = NULL;
4519 free(href_commits);
4520 href_commits = NULL;
4522 done:
4523 got_ref_list_free(&refs);
4524 free(href_summary);
4525 free(href_briefs);
4526 free(href_commits);
4527 return error;
4530 static const struct got_error *
4531 gw_output_site_link(struct gw_trans *gw_trans)
4533 const struct got_error *error = NULL;
4534 char *href_summary = NULL;
4535 enum kcgi_err kerr = KCGI_OK;
4537 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4538 "site_link", KATTR__MAX);
4539 if (kerr != KCGI_OK)
4540 goto done;
4541 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4542 KATTR__MAX);
4543 if (kerr != KCGI_OK)
4544 goto done;
4545 kerr = khtml_puts(gw_trans->gw_html_req,
4546 gw_trans->gw_conf->got_site_link);
4547 if (kerr != KCGI_OK)
4548 goto done;
4549 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4550 if (kerr != KCGI_OK)
4551 goto done;
4553 if (gw_trans->repo_name != NULL) {
4554 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4555 if (kerr != KCGI_OK)
4556 goto done;
4558 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
4559 gw_trans->repo_name, "action", "summary", NULL);
4560 if (href_summary == NULL) {
4561 error = got_error_from_errno("khttp_urlpart");
4562 goto done;
4564 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4565 href_summary, KATTR__MAX);
4566 if (kerr != KCGI_OK)
4567 goto done;
4568 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4569 if (kerr != KCGI_OK)
4570 goto done;
4571 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4572 if (kerr != KCGI_OK)
4573 goto done;
4574 kerr = khtml_printf(gw_trans->gw_html_req, " / %s",
4575 gw_get_action_name(gw_trans));
4576 if (kerr != KCGI_OK)
4577 goto done;
4580 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4581 if (kerr != KCGI_OK)
4582 goto done;
4583 done:
4584 free(href_summary);
4585 if (error == NULL && kerr != KCGI_OK)
4586 error = gw_kcgi_error(kerr);
4587 return error;
4590 static const struct got_error *
4591 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4593 const struct got_error *error = NULL;
4594 char *color = NULL;
4595 enum kcgi_err kerr = KCGI_OK;
4597 if (strncmp(buf, "-", 1) == 0)
4598 color = "diff_minus";
4599 else if (strncmp(buf, "+", 1) == 0)
4600 color = "diff_plus";
4601 else if (strncmp(buf, "@@", 2) == 0)
4602 color = "diff_chunk_header";
4603 else if (strncmp(buf, "@@", 2) == 0)
4604 color = "diff_chunk_header";
4605 else if (strncmp(buf, "commit +", 8) == 0)
4606 color = "diff_meta";
4607 else if (strncmp(buf, "commit -", 8) == 0)
4608 color = "diff_meta";
4609 else if (strncmp(buf, "blob +", 6) == 0)
4610 color = "diff_meta";
4611 else if (strncmp(buf, "blob -", 6) == 0)
4612 color = "diff_meta";
4613 else if (strncmp(buf, "file +", 6) == 0)
4614 color = "diff_meta";
4615 else if (strncmp(buf, "file -", 6) == 0)
4616 color = "diff_meta";
4617 else if (strncmp(buf, "from:", 5) == 0)
4618 color = "diff_author";
4619 else if (strncmp(buf, "via:", 4) == 0)
4620 color = "diff_author";
4621 else if (strncmp(buf, "date:", 5) == 0)
4622 color = "diff_date";
4623 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4624 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4625 if (error == NULL && kerr != KCGI_OK)
4626 error = gw_kcgi_error(kerr);
4627 return error;
4630 int
4631 main(int argc, char *argv[])
4633 const struct got_error *error = NULL;
4634 struct gw_trans *gw_trans;
4635 struct gw_dir *dir = NULL, *tdir;
4636 const char *page = "index";
4637 int gw_malloc = 1;
4638 enum kcgi_err kerr = KCGI_OK;
4640 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4641 errx(1, "malloc");
4643 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4644 errx(1, "malloc");
4646 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4647 errx(1, "malloc");
4649 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4650 errx(1, "malloc");
4652 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4653 if (kerr != KCGI_OK) {
4654 error = gw_kcgi_error(kerr);
4655 goto done;
4658 if ((gw_trans->gw_conf =
4659 malloc(sizeof(struct gotweb_conf))) == NULL) {
4660 gw_malloc = 0;
4661 error = got_error_from_errno("malloc");
4662 goto done;
4665 TAILQ_INIT(&gw_trans->gw_dirs);
4666 TAILQ_INIT(&gw_trans->gw_headers);
4668 gw_trans->action = -1;
4669 gw_trans->page = 0;
4670 gw_trans->repos_total = 0;
4671 gw_trans->repo_path = NULL;
4672 gw_trans->commit_id = NULL;
4673 gw_trans->next_id = NULL;
4674 gw_trans->next_prev_id = NULL;
4675 gw_trans->prev_id = NULL;
4676 gw_trans->prev_prev_id = NULL;
4677 gw_trans->headref = GOT_REF_HEAD;
4678 gw_trans->mime = KMIME_TEXT_HTML;
4679 gw_trans->gw_tmpl->key = gw_templs;
4680 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4681 gw_trans->gw_tmpl->arg = gw_trans;
4682 gw_trans->gw_tmpl->cb = gw_template;
4683 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
4684 if (error)
4685 goto done;
4687 error = gw_parse_querystring(gw_trans);
4688 if (error)
4689 goto done;
4691 if (gw_trans->action == GW_BLOB)
4692 error = gw_blob(gw_trans);
4693 else
4694 error = gw_display_index(gw_trans);
4695 done:
4696 if (gw_malloc) {
4697 free(gw_trans->gw_conf->got_repos_path);
4698 free(gw_trans->gw_conf->got_site_name);
4699 free(gw_trans->gw_conf->got_site_owner);
4700 free(gw_trans->gw_conf->got_site_link);
4701 free(gw_trans->gw_conf->got_logo);
4702 free(gw_trans->gw_conf->got_logo_url);
4703 free(gw_trans->gw_conf);
4704 free(gw_trans->commit_id);
4705 free(gw_trans->next_id);
4706 free(gw_trans->next_prev_id);
4707 free(gw_trans->prev_id);
4708 free(gw_trans->prev_prev_id);
4709 free(gw_trans->repo_path);
4710 if (gw_trans->repo)
4711 got_repo_close(gw_trans->repo);
4713 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4714 free(dir->name);
4715 free(dir->description);
4716 free(dir->age);
4717 free(dir->url);
4718 free(dir->path);
4719 free(dir);
4724 khttp_free(gw_trans->gw_req);
4725 return 0;