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_error.h>
35 #include <got_object.h>
36 #include <got_reference.h>
37 #include <got_repository.h>
38 #include <got_path.h>
39 #include <got_cancel.h>
40 #include <got_worktree.h>
41 #include <got_diff.h>
42 #include <got_commit_graph.h>
43 #include <got_blame.h>
44 #include <got_privsep.h>
45 #include <got_opentemp.h>
47 #include <kcgi.h>
48 #include <kcgihtml.h>
50 #include "gotweb.h"
52 #ifndef nitems
53 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
54 #endif
56 struct gw_trans {
57 TAILQ_HEAD(headers, gw_header) gw_headers;
58 TAILQ_HEAD(dirs, gw_dir) gw_dirs;
59 struct got_repository *repo;
60 struct gw_dir *gw_dir;
61 struct gotweb_config *gw_conf;
62 struct ktemplate *gw_tmpl;
63 struct khtmlreq *gw_html_req;
64 struct kreq *gw_req;
65 const struct got_error *error;
66 const char *repo_name;
67 char *repo_path;
68 char *commit_id;
69 char *next_id;
70 char *next_prev_id;
71 char *prev_id;
72 char *prev_prev_id;
73 const char *repo_file;
74 char *repo_folder;
75 const char *headref;
76 unsigned int action;
77 unsigned int page;
78 unsigned int repos_total;
79 enum kmime mime;
80 };
82 struct gw_header {
83 TAILQ_ENTRY(gw_header) entry;
84 struct got_reflist_head refs;
85 char *path;
87 char *refs_str;
88 char *commit_id; /* id_str1 */
89 char *parent_id; /* id_str2 */
90 char *tree_id;
91 char *author;
92 char *committer;
93 char *commit_msg;
94 time_t committer_time;
95 };
97 struct gw_dir {
98 TAILQ_ENTRY(gw_dir) entry;
99 char *name;
100 char *owner;
101 char *description;
102 char *url;
103 char *age;
104 char *path;
105 };
107 enum gw_key {
108 KEY_ACTION,
109 KEY_COMMIT_ID,
110 KEY_FILE,
111 KEY_FOLDER,
112 KEY_HEADREF,
113 KEY_PAGE,
114 KEY_PATH,
115 KEY_PREV_ID,
116 KEY_PREV_PREV_ID,
117 KEY__ZMAX
118 };
120 enum gw_tmpl {
121 TEMPL_CONTENT,
122 TEMPL_HEAD,
123 TEMPL_HEADER,
124 TEMPL_SEARCH,
125 TEMPL_SITEPATH,
126 TEMPL_SITEOWNER,
127 TEMPL_TITLE,
128 TEMPL__MAX
129 };
131 enum gw_ref_tm {
132 TM_DIFF,
133 TM_LONG,
134 };
136 enum gw_tags_type {
137 TAGBRIEF,
138 TAGFULL,
139 };
141 static const char *const gw_templs[TEMPL__MAX] = {
142 "content",
143 "head",
144 "header",
145 "search",
146 "sitepath",
147 "siteowner",
148 "title",
149 };
151 static const struct kvalid gw_keys[KEY__ZMAX] = {
152 { kvalid_stringne, "action" },
153 { kvalid_stringne, "commit" },
154 { kvalid_stringne, "file" },
155 { kvalid_stringne, "folder" },
156 { kvalid_stringne, "headref" },
157 { kvalid_int, "page" },
158 { kvalid_stringne, "path" },
159 { kvalid_stringne, "prev" },
160 { kvalid_stringne, "prev_prev" },
161 };
163 static struct gw_header *gw_init_header(void);
165 static void gw_free_header(struct gw_header *);
167 static int gw_template(size_t, void *);
169 static const struct got_error *gw_error(struct gw_trans *);
170 static const struct got_error *gw_init_gw_dir(struct gw_dir **, const char *);
171 static const struct got_error *gw_get_repo_description(char **,
172 struct gw_trans *, char *);
173 static const struct got_error *gw_get_repo_owner(char **, struct gw_trans *,
174 char *);
175 static const struct got_error *gw_get_time_str(char **, time_t, int);
176 static const struct got_error *gw_get_repo_age(char **, struct gw_trans *,
177 char *, const char *, int);
178 static const struct got_error *gw_output_file_blame(struct gw_trans *,
179 struct gw_header *);
180 static const struct got_error *gw_output_blob_buf(struct gw_trans *,
181 struct gw_header *);
182 static const struct got_error *gw_output_repo_tree(struct gw_trans *,
183 struct gw_header *);
184 static const struct got_error *gw_output_diff(struct gw_trans *,
185 struct gw_header *);
186 static const struct got_error *gw_output_repo_tags(struct gw_trans *,
187 struct gw_header *, int, int);
188 static const struct got_error *gw_output_repo_heads(struct gw_trans *);
189 static const struct got_error *gw_output_site_link(struct gw_trans *);
190 static const struct got_error *gw_get_clone_url(char **, struct gw_trans *,
191 char *);
192 static const struct got_error *gw_colordiff_line(struct gw_trans *, char *);
194 static const struct got_error *gw_gen_commit_header(struct gw_trans *, char *,
195 char*);
196 static const struct got_error *gw_gen_diff_header(struct gw_trans *, char *,
197 char*);
198 static const struct got_error *gw_gen_author_header(struct gw_trans *,
199 const char *);
200 static const struct got_error *gw_gen_age_header(struct gw_trans *,
201 const char *);
202 static const struct got_error *gw_gen_committer_header(struct gw_trans *,
203 const char *);
204 static const struct got_error *gw_gen_commit_msg_header(struct gw_trans*,
205 char *);
206 static const struct got_error *gw_gen_tree_header(struct gw_trans *, char *);
207 static const struct got_error *gw_display_open(struct gw_trans *, enum khttp,
208 enum kmime);
209 static const struct got_error *gw_display_index(struct gw_trans *);
210 static const struct got_error *gw_get_header(struct gw_trans *,
211 struct gw_header *, int);
212 static const struct got_error *gw_get_commits(struct gw_trans *,
213 struct gw_header *, int,
214 struct got_object_id *);
215 static const struct got_error *gw_get_commit(struct gw_trans *,
216 struct gw_header *,
217 struct got_commit_object *,
218 struct got_object_id *);
219 static const struct got_error *gw_apply_unveil(const char *);
220 static const struct got_error *gw_blame_cb(void *, int, int,
221 struct got_object_id *);
222 static const struct got_error *gw_load_got_paths(struct gw_trans *);
223 static const struct got_error *gw_load_got_path(struct gw_trans *,
224 struct gw_dir *);
225 static const struct got_error *gw_parse_querystring(struct gw_trans *);
226 static const struct got_error *gw_blame(struct gw_trans *);
227 static const struct got_error *gw_blob(struct gw_trans *);
228 static const struct got_error *gw_diff(struct gw_trans *);
229 static const struct got_error *gw_index(struct gw_trans *);
230 static const struct got_error *gw_commits(struct gw_trans *);
231 static const struct got_error *gw_briefs(struct gw_trans *);
232 static const struct got_error *gw_summary(struct gw_trans *);
233 static const struct got_error *gw_tree(struct gw_trans *);
234 static const struct got_error *gw_tag(struct gw_trans *);
235 static const struct got_error *gw_tags(struct gw_trans *);
237 struct gw_query_action {
238 unsigned int func_id;
239 const char *func_name;
240 const struct got_error *(*func_main)(struct gw_trans *);
241 char *template;
242 };
244 enum gw_query_actions {
245 GW_BLAME,
246 GW_BLOB,
247 GW_BRIEFS,
248 GW_COMMITS,
249 GW_DIFF,
250 GW_ERR,
251 GW_INDEX,
252 GW_SUMMARY,
253 GW_TAG,
254 GW_TAGS,
255 GW_TREE,
256 };
258 static struct gw_query_action gw_query_funcs[] = {
259 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
260 { GW_BLOB, "blob", NULL, NULL },
261 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
262 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
263 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
264 { GW_ERR, "error", gw_error, "gw_tmpl/err.tmpl" },
265 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
266 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
267 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
268 { GW_TAGS, "tags", gw_tags, "gw_tmpl/tags.tmpl" },
269 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
270 };
272 static const char *
273 gw_get_action_name(struct gw_trans *gw_trans)
275 return gw_query_funcs[gw_trans->action].func_name;
278 static const struct got_error *
279 gw_kcgi_error(enum kcgi_err kerr)
281 if (kerr == KCGI_OK)
282 return NULL;
284 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
285 return got_error(GOT_ERR_CANCELLED);
287 if (kerr == KCGI_ENOMEM)
288 return got_error_set_errno(ENOMEM,
289 kcgi_strerror(kerr));
291 if (kerr == KCGI_ENFILE)
292 return got_error_set_errno(ENFILE,
293 kcgi_strerror(kerr));
295 if (kerr == KCGI_EAGAIN)
296 return got_error_set_errno(EAGAIN,
297 kcgi_strerror(kerr));
299 if (kerr == KCGI_FORM)
300 return got_error_msg(GOT_ERR_IO,
301 kcgi_strerror(kerr));
303 return got_error_from_errno(kcgi_strerror(kerr));
306 static const struct got_error *
307 gw_apply_unveil(const char *repo_path)
309 const struct got_error *err;
311 if (repo_path && unveil(repo_path, "r") != 0)
312 return got_error_from_errno2("unveil", repo_path);
314 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
315 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
317 err = got_privsep_unveil_exec_helpers();
318 if (err != NULL)
319 return err;
321 if (unveil(NULL, NULL) != 0)
322 return got_error_from_errno("unveil");
324 return NULL;
327 static int
328 isbinary(const uint8_t *buf, size_t n)
330 size_t i;
332 for (i = 0; i < n; i++)
333 if (buf[i] == 0)
334 return 1;
335 return 0;
338 static const struct got_error *
339 gw_blame(struct gw_trans *gw_trans)
341 const struct got_error *error = NULL;
342 struct gw_header *header = NULL;
343 char *age = NULL;
344 enum kcgi_err kerr = KCGI_OK;
346 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
347 NULL) == -1)
348 return got_error_from_errno("pledge");
350 if ((header = gw_init_header()) == NULL)
351 return got_error_from_errno("malloc");
353 error = gw_apply_unveil(gw_trans->gw_dir->path);
354 if (error)
355 goto done;
357 /* check querystring */
358 if (gw_trans->repo_file == NULL) {
359 error = got_error_msg(GOT_ERR_QUERYSTRING,
360 "file required in querystring");
361 goto done;
363 if (gw_trans->commit_id == NULL) {
364 error = got_error_msg(GOT_ERR_QUERYSTRING,
365 "commit required in querystring");
366 goto done;
369 error = gw_get_header(gw_trans, header, 1);
370 if (error)
371 goto done;
372 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
373 "blame_header_wrapper", KATTR__MAX);
374 if (kerr != KCGI_OK)
375 goto done;
376 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
377 "blame_header", KATTR__MAX);
378 if (kerr != KCGI_OK)
379 goto done;
380 error = gw_get_time_str(&age, header->committer_time,
381 TM_LONG);
382 if (error)
383 goto done;
384 error = gw_gen_age_header(gw_trans, age ?age : "");
385 if (error)
386 goto done;
387 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
388 if (error)
389 goto done;
390 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
391 if (kerr != KCGI_OK)
392 goto done;
393 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
394 "dotted_line", KATTR__MAX);
395 if (kerr != KCGI_OK)
396 goto done;
397 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
398 if (kerr != KCGI_OK)
399 goto done;
401 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
402 "blame", KATTR__MAX);
403 if (kerr != KCGI_OK)
404 goto done;
405 error = gw_output_file_blame(gw_trans, header);
406 if (error)
407 goto done;
408 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
409 done:
410 gw_free_header(header);
411 if (error == NULL && kerr != KCGI_OK)
412 error = gw_kcgi_error(kerr);
413 return error;
416 static const struct got_error *
417 gw_blob(struct gw_trans *gw_trans)
419 const struct got_error *error = NULL, *err = NULL;
420 struct gw_header *header = NULL;
421 enum kcgi_err kerr = KCGI_OK;
423 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
424 NULL) == -1)
425 return got_error_from_errno("pledge");
427 if ((header = gw_init_header()) == NULL)
428 return got_error_from_errno("malloc");
430 error = gw_apply_unveil(gw_trans->gw_dir->path);
431 if (error)
432 goto done;
434 /* check querystring */
435 if (gw_trans->repo_file == NULL) {
436 error = got_error_msg(GOT_ERR_QUERYSTRING,
437 "file required in querystring");
438 goto done;
440 if (gw_trans->commit_id == NULL) {
441 error = got_error_msg(GOT_ERR_QUERYSTRING,
442 "commit required in querystring");
443 goto done;
445 error = gw_get_header(gw_trans, header, 1);
446 if (error)
447 goto done;
449 error = gw_output_blob_buf(gw_trans, header);
450 done:
452 if (error) {
453 gw_trans->mime = KMIME_TEXT_PLAIN;
454 err = gw_display_index(gw_trans);
455 if (err) {
456 error = err;
457 goto errored;
459 kerr = khttp_puts(gw_trans->gw_req, error->msg);
461 errored:
462 gw_free_header(header);
463 if (error == NULL && kerr != KCGI_OK)
464 error = gw_kcgi_error(kerr);
465 return error;
468 static const struct got_error *
469 gw_diff(struct gw_trans *gw_trans)
471 const struct got_error *error = NULL;
472 struct gw_header *header = NULL;
473 char *age = NULL;
474 enum kcgi_err kerr = KCGI_OK;
476 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
477 NULL) == -1)
478 return got_error_from_errno("pledge");
480 if ((header = gw_init_header()) == NULL)
481 return got_error_from_errno("malloc");
483 error = gw_apply_unveil(gw_trans->gw_dir->path);
484 if (error)
485 goto done;
487 error = gw_get_header(gw_trans, header, 1);
488 if (error)
489 goto done;
491 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
492 "diff_header_wrapper", KATTR__MAX);
493 if (kerr != KCGI_OK)
494 goto done;
495 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
496 "diff_header", KATTR__MAX);
497 if (kerr != KCGI_OK)
498 goto done;
499 error = gw_gen_diff_header(gw_trans, header->parent_id,
500 header->commit_id);
501 if (error)
502 goto done;
503 error = gw_gen_commit_header(gw_trans, header->commit_id,
504 header->refs_str);
505 if (error)
506 goto done;
507 error = gw_gen_tree_header(gw_trans, header->tree_id);
508 if (error)
509 goto done;
510 error = gw_gen_author_header(gw_trans, header->author);
511 if (error)
512 goto done;
513 error = gw_gen_committer_header(gw_trans, header->author);
514 if (error)
515 goto done;
516 error = gw_get_time_str(&age, header->committer_time,
517 TM_LONG);
518 if (error)
519 goto done;
520 error = gw_gen_age_header(gw_trans, age ?age : "");
521 if (error)
522 goto done;
523 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
524 if (error)
525 goto done;
526 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
527 if (kerr != KCGI_OK)
528 goto done;
529 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
530 "dotted_line", KATTR__MAX);
531 if (kerr != KCGI_OK)
532 goto done;
533 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
534 if (kerr != KCGI_OK)
535 goto done;
537 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
538 "diff", KATTR__MAX);
539 if (kerr != KCGI_OK)
540 goto done;
541 error = gw_output_diff(gw_trans, header);
542 if (error)
543 goto done;
545 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
546 done:
547 gw_free_header(header);
548 free(age);
549 if (error == NULL && kerr != KCGI_OK)
550 error = gw_kcgi_error(kerr);
551 return error;
554 static const struct got_error *
555 gw_index(struct gw_trans *gw_trans)
557 const struct got_error *error = NULL;
558 struct gw_dir *gw_dir = NULL;
559 char *href_next = NULL, *href_prev = NULL, *href_summary = NULL;
560 char *href_briefs = NULL, *href_commits = NULL, *href_tree = NULL;
561 char *href_tags = NULL;
562 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
563 enum kcgi_err kerr = KCGI_OK;
565 if (pledge("stdio rpath proc exec sendfd unveil",
566 NULL) == -1) {
567 error = got_error_from_errno("pledge");
568 return error;
571 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path);
572 if (error)
573 return error;
575 error = gw_load_got_paths(gw_trans);
576 if (error)
577 return error;
579 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
580 "index_header", KATTR__MAX);
581 if (kerr != KCGI_OK)
582 return gw_kcgi_error(kerr);
583 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
584 "index_header_project", KATTR__MAX);
585 if (kerr != KCGI_OK)
586 return gw_kcgi_error(kerr);
587 kerr = khtml_puts(gw_trans->gw_html_req, "Project");
588 if (kerr != KCGI_OK)
589 return gw_kcgi_error(kerr);
590 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
591 if (kerr != KCGI_OK)
592 return gw_kcgi_error(kerr);
594 if (gw_trans->gw_conf->got_show_repo_description) {
595 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
596 "index_header_description", KATTR__MAX);
597 if (kerr != KCGI_OK)
598 return gw_kcgi_error(kerr);
599 kerr = khtml_puts(gw_trans->gw_html_req, "Description");
600 if (kerr != KCGI_OK)
601 return gw_kcgi_error(kerr);
602 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
603 if (kerr != KCGI_OK)
604 return gw_kcgi_error(kerr);
607 if (gw_trans->gw_conf->got_show_repo_owner) {
608 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
609 "index_header_owner", KATTR__MAX);
610 if (kerr != KCGI_OK)
611 return gw_kcgi_error(kerr);
612 kerr = khtml_puts(gw_trans->gw_html_req, "Owner");
613 if (kerr != KCGI_OK)
614 return gw_kcgi_error(kerr);
615 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
616 if (kerr != KCGI_OK)
617 return gw_kcgi_error(kerr);
620 if (gw_trans->gw_conf->got_show_repo_age) {
621 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
622 "index_header_age", KATTR__MAX);
623 if (kerr != KCGI_OK)
624 return gw_kcgi_error(kerr);
625 kerr = khtml_puts(gw_trans->gw_html_req, "Last Change");
626 if (kerr != KCGI_OK)
627 return gw_kcgi_error(kerr);
628 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
629 if (kerr != KCGI_OK)
630 return gw_kcgi_error(kerr);
633 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
634 if (kerr != KCGI_OK)
635 return gw_kcgi_error(kerr);
637 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
638 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
639 "index_wrapper", KATTR__MAX);
640 if (kerr != KCGI_OK)
641 return gw_kcgi_error(kerr);
642 kerr = khtml_printf(gw_trans->gw_html_req,
643 "No repositories found in %s",
644 gw_trans->gw_conf->got_repos_path);
645 if (kerr != KCGI_OK)
646 return gw_kcgi_error(kerr);
647 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
648 if (kerr != KCGI_OK)
649 return gw_kcgi_error(kerr);
650 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
651 "dotted_line", KATTR__MAX);
652 if (kerr != KCGI_OK)
653 return gw_kcgi_error(kerr);
654 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
655 if (kerr != KCGI_OK)
656 return gw_kcgi_error(kerr);
657 return error;
660 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
661 dir_c++;
663 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
664 if (gw_trans->page > 0 && (gw_trans->page *
665 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
666 prev_disp++;
667 continue;
670 prev_disp++;
672 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
673 "index_wrapper", KATTR__MAX);
674 if (kerr != KCGI_OK)
675 goto done;
677 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
678 gw_dir->name, "action", "summary", NULL);
679 if (href_summary == NULL) {
680 error = got_error_from_errno("khttp_urlpart");
681 goto done;
683 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
684 "index_project", KATTR__MAX);
685 if (kerr != KCGI_OK)
686 goto done;
687 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
688 href_summary, KATTR__MAX);
689 if (kerr != KCGI_OK)
690 goto done;
691 kerr = khtml_puts(gw_trans->gw_html_req, gw_dir->name);
692 if (kerr != KCGI_OK)
693 goto done;
694 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
695 if (kerr != KCGI_OK)
696 goto done;
697 if (gw_trans->gw_conf->got_show_repo_description) {
698 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
699 KATTR_ID, "index_project_description", KATTR__MAX);
700 if (kerr != KCGI_OK)
701 goto done;
702 kerr = khtml_puts(gw_trans->gw_html_req,
703 gw_dir->description ? gw_dir->description : "");
704 if (kerr != KCGI_OK)
705 goto done;
706 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
707 if (kerr != KCGI_OK)
708 goto done;
710 if (gw_trans->gw_conf->got_show_repo_owner) {
711 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
712 KATTR_ID, "index_project_owner", KATTR__MAX);
713 if (kerr != KCGI_OK)
714 goto done;
715 kerr = khtml_puts(gw_trans->gw_html_req,
716 gw_dir->owner ? gw_dir->owner : "");
717 if (kerr != KCGI_OK)
718 goto done;
719 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
720 if (kerr != KCGI_OK)
721 goto done;
723 if (gw_trans->gw_conf->got_show_repo_age) {
724 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
725 KATTR_ID, "index_project_age", KATTR__MAX);
726 if (kerr != KCGI_OK)
727 goto done;
728 kerr = khtml_puts(gw_trans->gw_html_req,
729 gw_dir->age ? gw_dir->age : "");
730 if (kerr != KCGI_OK)
731 goto done;
732 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
733 if (kerr != KCGI_OK)
734 goto done;
737 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
738 "navs_wrapper", KATTR__MAX);
739 if (kerr != KCGI_OK)
740 goto done;
741 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
742 "navs", KATTR__MAX);
743 if (kerr != KCGI_OK)
744 goto done;
746 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
747 href_summary, KATTR__MAX);
748 if (kerr != KCGI_OK)
749 goto done;
750 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
751 if (kerr != KCGI_OK)
752 goto done;
753 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
754 if (kerr != KCGI_OK)
755 goto done;
757 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
758 if (kerr != KCGI_OK)
759 goto done;
761 href_briefs = khttp_urlpart(NULL, NULL, "gotweb", "path",
762 gw_dir->name, "action", "briefs", NULL);
763 if (href_briefs == NULL) {
764 error = got_error_from_errno("khttp_urlpart");
765 goto done;
767 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
768 href_briefs, KATTR__MAX);
769 if (kerr != KCGI_OK)
770 goto done;
771 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
772 if (kerr != KCGI_OK)
773 goto done;
774 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
775 if (kerr != KCGI_OK)
776 error = gw_kcgi_error(kerr);
778 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
779 if (kerr != KCGI_OK)
780 goto done;
782 href_commits = khttp_urlpart(NULL, NULL, "gotweb", "path",
783 gw_dir->name, "action", "commits", NULL);
784 if (href_commits == NULL) {
785 error = got_error_from_errno("khttp_urlpart");
786 goto done;
788 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
789 href_commits, KATTR__MAX);
790 if (kerr != KCGI_OK)
791 goto done;
792 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
793 if (kerr != KCGI_OK)
794 goto done;
795 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
796 if (kerr != KCGI_OK)
797 goto done;
799 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
800 if (kerr != KCGI_OK)
801 goto done;
803 href_tags = khttp_urlpart(NULL, NULL, "gotweb", "path",
804 gw_dir->name, "action", "tags", NULL);
805 if (href_tags == NULL) {
806 error = got_error_from_errno("khttp_urlpart");
807 goto done;
809 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
810 href_tags, KATTR__MAX);
811 if (kerr != KCGI_OK)
812 goto done;
813 kerr = khtml_puts(gw_trans->gw_html_req, "tags");
814 if (kerr != KCGI_OK)
815 goto done;
816 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
817 if (kerr != KCGI_OK)
818 goto done;
820 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
821 if (kerr != KCGI_OK)
822 goto done;
824 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
825 gw_dir->name, "action", "tree", NULL);
826 if (href_tree == NULL) {
827 error = got_error_from_errno("khttp_urlpart");
828 goto done;
830 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
831 href_tree, KATTR__MAX);
832 if (kerr != KCGI_OK)
833 goto done;
834 kerr = khtml_puts(gw_trans->gw_html_req, "tree");
835 if (kerr != KCGI_OK)
836 goto done;
838 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
839 if (kerr != KCGI_OK)
840 goto done;
841 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
842 "dotted_line", KATTR__MAX);
843 if (kerr != KCGI_OK)
844 goto done;
845 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
846 if (kerr != KCGI_OK)
847 goto done;
849 free(href_summary);
850 href_summary = NULL;
851 free(href_briefs);
852 href_briefs = NULL;
853 free(href_commits);
854 href_commits = NULL;
855 free(href_tags);
856 href_tags = NULL;
857 free(href_tree);
858 href_tree = NULL;
860 if (gw_trans->gw_conf->got_max_repos_display == 0)
861 continue;
863 if ((next_disp == gw_trans->gw_conf->got_max_repos_display) ||
864 ((gw_trans->gw_conf->got_max_repos_display > 0) &&
865 (gw_trans->page > 0) &&
866 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
867 prev_disp == gw_trans->repos_total))) {
868 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
869 KATTR_ID, "np_wrapper", KATTR__MAX);
870 if (kerr != KCGI_OK)
871 goto done;
872 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
873 KATTR_ID, "nav_prev", KATTR__MAX);
874 if (kerr != KCGI_OK)
875 goto done;
878 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
879 (gw_trans->page > 0) &&
880 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
881 prev_disp == gw_trans->repos_total)) {
882 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "page",
883 KATTRX_INT, (int64_t)(gw_trans->page - 1), NULL);
884 if (href_prev == NULL) {
885 error = got_error_from_errno("khttp_urlpartx");
886 goto done;
888 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
889 KATTR_HREF, href_prev, KATTR__MAX);
890 if (kerr != KCGI_OK)
891 goto done;
892 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
893 if (kerr != KCGI_OK)
894 goto done;
895 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
896 if (kerr != KCGI_OK)
897 goto done;
900 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
901 if (kerr != KCGI_OK)
902 return gw_kcgi_error(kerr);
904 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
905 next_disp == gw_trans->gw_conf->got_max_repos_display &&
906 dir_c != (gw_trans->page + 1) *
907 gw_trans->gw_conf->got_max_repos_display) {
908 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
909 KATTR_ID, "nav_next", KATTR__MAX);
910 if (kerr != KCGI_OK)
911 goto done;
912 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "page",
913 KATTRX_INT, (int64_t)(gw_trans->page + 1), NULL);
914 if (href_next == NULL) {
915 error = got_error_from_errno("khttp_urlpartx");
916 goto done;
918 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
919 KATTR_HREF, href_next, KATTR__MAX);
920 if (kerr != KCGI_OK)
921 goto done;
922 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
923 if (kerr != KCGI_OK)
924 goto done;
925 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
926 if (kerr != KCGI_OK)
927 goto done;
928 next_disp = 0;
929 break;
932 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
933 (gw_trans->page > 0) &&
934 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
935 prev_disp == gw_trans->repos_total)) {
936 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
937 if (kerr != KCGI_OK)
938 goto done;
940 next_disp++;
942 done:
943 free(href_prev);
944 free(href_next);
945 free(href_summary);
946 free(href_briefs);
947 free(href_commits);
948 free(href_tags);
949 free(href_tree);
950 if (error == NULL && kerr != KCGI_OK)
951 error = gw_kcgi_error(kerr);
952 return error;
955 static const struct got_error *
956 gw_commits(struct gw_trans *gw_trans)
958 const struct got_error *error = NULL;
959 struct gw_header *header = NULL, *n_header = NULL;
960 char *age = NULL, *href_diff = NULL, *href_tree = NULL;
961 char *href_prev = NULL, *href_next = NULL;
962 enum kcgi_err kerr = KCGI_OK;
964 if ((header = gw_init_header()) == NULL)
965 return got_error_from_errno("malloc");
967 if (pledge("stdio rpath proc exec sendfd unveil",
968 NULL) == -1) {
969 error = got_error_from_errno("pledge");
970 goto done;
973 error = gw_apply_unveil(gw_trans->gw_dir->path);
974 if (error)
975 goto done;
977 error = gw_get_header(gw_trans, header,
978 gw_trans->gw_conf->got_max_commits_display);
979 if (error)
980 goto done;
982 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
983 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
984 "commits_line_wrapper", KATTR__MAX);
985 if (kerr != KCGI_OK)
986 goto done;
987 error = gw_gen_commit_header(gw_trans, n_header->commit_id,
988 n_header->refs_str);
989 if (error)
990 goto done;
991 error = gw_gen_author_header(gw_trans, n_header->author);
992 if (error)
993 goto done;
994 error = gw_gen_committer_header(gw_trans, n_header->author);
995 if (error)
996 goto done;
997 error = gw_get_time_str(&age, n_header->committer_time,
998 TM_LONG);
999 if (error)
1000 goto done;
1001 error = gw_gen_age_header(gw_trans, age ?age : "");
1002 if (error)
1003 goto done;
1004 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1005 if (kerr != KCGI_OK)
1006 goto done;
1008 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1009 "dotted_line", KATTR__MAX);
1010 if (kerr != KCGI_OK)
1011 goto done;
1012 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1013 if (kerr != KCGI_OK)
1014 goto done;
1016 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1017 "commit", KATTR__MAX);
1018 if (kerr != KCGI_OK)
1019 goto done;
1020 kerr = khttp_puts(gw_trans->gw_req, n_header->commit_msg);
1021 if (kerr != KCGI_OK)
1022 goto done;
1023 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1024 if (kerr != KCGI_OK)
1025 goto done;
1027 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
1028 gw_trans->repo_name, "action", "diff", "commit",
1029 n_header->commit_id, NULL);
1030 if (href_diff == NULL) {
1031 error = got_error_from_errno("khttp_urlpart");
1032 goto done;
1034 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1035 KATTR_ID, "navs_wrapper", KATTR__MAX);
1036 if (kerr != KCGI_OK)
1037 goto done;
1038 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1039 KATTR_ID, "navs", KATTR__MAX);
1040 if (kerr != KCGI_OK)
1041 goto done;
1042 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1043 KATTR_HREF, href_diff, KATTR__MAX);
1044 if (kerr != KCGI_OK)
1045 goto done;
1046 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1047 if (kerr != KCGI_OK)
1048 goto done;
1049 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1050 if (kerr != KCGI_OK)
1051 goto done;
1053 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1054 if (kerr != KCGI_OK)
1055 goto done;
1057 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
1058 gw_trans->repo_name, "action", "tree", "commit",
1059 n_header->commit_id, NULL);
1060 if (href_tree == NULL) {
1061 error = got_error_from_errno("khttp_urlpart");
1062 goto done;
1064 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1065 KATTR_HREF, href_tree, KATTR__MAX);
1066 if (kerr != KCGI_OK)
1067 goto done;
1068 khtml_puts(gw_trans->gw_html_req, "tree");
1069 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1070 if (kerr != KCGI_OK)
1071 goto done;
1072 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1073 if (kerr != KCGI_OK)
1074 goto done;
1076 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1077 "solid_line", KATTR__MAX);
1078 if (kerr != KCGI_OK)
1079 goto done;
1080 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1081 if (kerr != KCGI_OK)
1082 goto done;
1084 free(age);
1085 age = NULL;
1088 if (gw_trans->next_id || gw_trans->page > 0) {
1089 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1090 KATTR_ID, "np_wrapper", KATTR__MAX);
1091 if (kerr != KCGI_OK)
1092 goto done;
1093 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1094 KATTR_ID, "nav_prev", KATTR__MAX);
1095 if (kerr != KCGI_OK)
1096 goto done;
1099 if (gw_trans->page > 0 && gw_trans->prev_id) {
1100 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1101 KATTRX_STRING, gw_trans->repo_name, "page",
1102 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1103 KATTRX_STRING, "commits", "commit", KATTRX_STRING,
1104 gw_trans->prev_id ? gw_trans->prev_id : "", "prev",
1105 KATTRX_STRING, gw_trans->prev_prev_id ?
1106 gw_trans->prev_prev_id : "", NULL);
1107 if (href_prev == NULL) {
1108 error = got_error_from_errno("khttp_urlpartx");
1109 goto done;
1111 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1112 KATTR_HREF, href_prev, KATTR__MAX);
1113 if (kerr != KCGI_OK)
1114 goto done;
1115 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1116 if (kerr != KCGI_OK)
1117 goto done;
1118 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1119 if (kerr != KCGI_OK)
1120 goto done;
1123 if (gw_trans->next_id || gw_trans->page > 0) {
1124 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1125 if (kerr != KCGI_OK)
1126 return gw_kcgi_error(kerr);
1129 if (gw_trans->next_id) {
1130 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1131 KATTR_ID, "nav_next", KATTR__MAX);
1132 if (kerr != KCGI_OK)
1133 goto done;
1134 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1135 KATTRX_STRING, gw_trans->repo_name, "page",
1136 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1137 KATTRX_STRING, "commits", "commit", KATTRX_STRING,
1138 gw_trans->next_id, "prev", KATTRX_STRING,
1139 gw_trans->next_prev_id ? gw_trans->next_prev_id : "",
1140 "prev_prev", KATTRX_STRING, gw_trans->prev_prev_id ?
1141 gw_trans->prev_prev_id : "", NULL);
1142 if (href_next == NULL) {
1143 error = got_error_from_errno("khttp_urlpartx");
1144 goto done;
1146 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1147 KATTR_HREF, href_next, KATTR__MAX);
1148 if (kerr != KCGI_OK)
1149 goto done;
1150 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1151 if (kerr != KCGI_OK)
1152 goto done;
1153 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1154 if (kerr != KCGI_OK)
1155 goto done;
1158 if (gw_trans->next_id || gw_trans->page > 0) {
1159 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1160 if (kerr != KCGI_OK)
1161 goto done;
1163 done:
1164 gw_free_header(header);
1165 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1166 gw_free_header(n_header);
1167 free(age);
1168 free(href_next);
1169 free(href_prev);
1170 free(href_diff);
1171 free(href_tree);
1172 if (error == NULL && kerr != KCGI_OK)
1173 error = gw_kcgi_error(kerr);
1174 return error;
1177 static const struct got_error *
1178 gw_briefs(struct gw_trans *gw_trans)
1180 const struct got_error *error = NULL;
1181 struct gw_header *header = NULL, *n_header = NULL;
1182 char *age = NULL, *href_diff = NULL, *href_tree = NULL;
1183 char *href_prev = NULL, *href_next = NULL;
1184 char *newline, *smallerthan;
1185 enum kcgi_err kerr = KCGI_OK;
1187 if ((header = gw_init_header()) == NULL)
1188 return got_error_from_errno("malloc");
1190 if (pledge("stdio rpath proc exec sendfd unveil",
1191 NULL) == -1) {
1192 error = got_error_from_errno("pledge");
1193 goto done;
1196 if (gw_trans->action != GW_SUMMARY) {
1197 error = gw_apply_unveil(gw_trans->gw_dir->path);
1198 if (error)
1199 goto done;
1202 if (gw_trans->action == GW_SUMMARY)
1203 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
1204 else
1205 error = gw_get_header(gw_trans, header,
1206 gw_trans->gw_conf->got_max_commits_display);
1207 if (error)
1208 goto done;
1210 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
1211 error = gw_get_time_str(&age, n_header->committer_time,
1212 TM_DIFF);
1213 if (error)
1214 goto done;
1216 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1217 KATTR_ID, "briefs_wrapper", KATTR__MAX);
1218 if (kerr != KCGI_OK)
1219 goto done;
1221 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1222 KATTR_ID, "briefs_age", KATTR__MAX);
1223 if (kerr != KCGI_OK)
1224 goto done;
1225 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
1226 if (kerr != KCGI_OK)
1227 goto done;
1228 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1229 if (kerr != KCGI_OK)
1230 goto done;
1232 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1233 KATTR_ID, "briefs_author", KATTR__MAX);
1234 if (kerr != KCGI_OK)
1235 goto done;
1236 smallerthan = strchr(n_header->author, '<');
1237 if (smallerthan)
1238 *smallerthan = '\0';
1239 kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
1240 if (kerr != KCGI_OK)
1241 goto done;
1242 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1243 if (kerr != KCGI_OK)
1244 goto done;
1246 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
1247 gw_trans->repo_name, "action", "diff", "commit",
1248 n_header->commit_id, NULL);
1249 if (href_diff == NULL) {
1250 error = got_error_from_errno("khttp_urlpart");
1251 goto done;
1253 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1254 KATTR_ID, "briefs_log", KATTR__MAX);
1255 if (kerr != KCGI_OK)
1256 goto done;
1257 newline = strchr(n_header->commit_msg, '\n');
1258 if (newline)
1259 *newline = '\0';
1260 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1261 KATTR_HREF, href_diff, KATTR__MAX);
1262 if (kerr != KCGI_OK)
1263 goto done;
1264 kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
1265 if (kerr != KCGI_OK)
1266 goto done;
1267 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1268 if (kerr != KCGI_OK)
1269 goto done;
1271 if (n_header->refs_str) {
1272 kerr = khtml_puts(gw_trans->gw_html_req, " ");
1273 if (kerr != KCGI_OK)
1274 goto done;
1275 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
1276 KATTR_ID, "refs_str", KATTR__MAX);
1277 if (kerr != KCGI_OK)
1278 goto done;
1279 kerr = khtml_printf(gw_trans->gw_html_req, "(%s)",
1280 n_header->refs_str);
1281 if (kerr != KCGI_OK)
1282 goto done;
1283 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1284 if (kerr != KCGI_OK)
1285 goto done;
1288 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1289 if (kerr != KCGI_OK)
1290 goto done;
1292 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1293 KATTR_ID, "navs_wrapper", KATTR__MAX);
1294 if (kerr != KCGI_OK)
1295 goto done;
1296 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1297 KATTR_ID, "navs", KATTR__MAX);
1298 if (kerr != KCGI_OK)
1299 goto done;
1300 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1301 KATTR_HREF, href_diff, KATTR__MAX);
1302 if (kerr != KCGI_OK)
1303 goto done;
1304 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1305 if (kerr != KCGI_OK)
1306 goto done;
1307 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1308 if (kerr != KCGI_OK)
1309 goto done;
1311 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1312 if (kerr != KCGI_OK)
1313 goto done;
1315 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
1316 gw_trans->repo_name, "action", "tree", "commit",
1317 n_header->commit_id, NULL);
1318 if (href_tree == NULL) {
1319 error = got_error_from_errno("khttp_urlpart");
1320 goto done;
1322 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1323 KATTR_HREF, href_tree, KATTR__MAX);
1324 if (kerr != KCGI_OK)
1325 goto done;
1326 khtml_puts(gw_trans->gw_html_req, "tree");
1327 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1328 if (kerr != KCGI_OK)
1329 goto done;
1330 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1331 if (kerr != KCGI_OK)
1332 goto done;
1334 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1335 KATTR_ID, "dotted_line", KATTR__MAX);
1336 if (kerr != KCGI_OK)
1337 goto done;
1338 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1339 if (kerr != KCGI_OK)
1340 goto done;
1342 free(age);
1343 age = NULL;
1344 free(href_diff);
1345 href_diff = NULL;
1346 free(href_tree);
1347 href_tree = NULL;
1350 if (gw_trans->next_id || gw_trans->page > 0) {
1351 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1352 KATTR_ID, "np_wrapper", KATTR__MAX);
1353 if (kerr != KCGI_OK)
1354 goto done;
1355 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1356 KATTR_ID, "nav_prev", KATTR__MAX);
1357 if (kerr != KCGI_OK)
1358 goto done;
1361 if (gw_trans->page > 0 && gw_trans->prev_id) {
1362 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1363 KATTRX_STRING, gw_trans->repo_name, "page",
1364 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1365 KATTRX_STRING, "briefs", "commit", KATTRX_STRING,
1366 gw_trans->prev_id ? gw_trans->prev_id : "", "prev",
1367 KATTRX_STRING, gw_trans->prev_prev_id ?
1368 gw_trans->prev_prev_id : "", NULL);
1369 if (href_prev == NULL) {
1370 error = got_error_from_errno("khttp_urlpartx");
1371 goto done;
1373 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1374 KATTR_HREF, href_prev, KATTR__MAX);
1375 if (kerr != KCGI_OK)
1376 goto done;
1377 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1378 if (kerr != KCGI_OK)
1379 goto done;
1380 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1381 if (kerr != KCGI_OK)
1382 goto done;
1385 if (gw_trans->next_id || gw_trans->page > 0) {
1386 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1387 if (kerr != KCGI_OK)
1388 return gw_kcgi_error(kerr);
1391 if (gw_trans->next_id) {
1392 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1393 KATTR_ID, "nav_next", KATTR__MAX);
1394 if (kerr != KCGI_OK)
1395 goto done;
1397 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1398 KATTRX_STRING, gw_trans->repo_name, "page",
1399 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1400 KATTRX_STRING, "briefs", "commit", KATTRX_STRING,
1401 gw_trans->next_id, "prev", KATTRX_STRING,
1402 gw_trans->next_prev_id ? gw_trans->next_prev_id : "",
1403 "prev_prev", KATTRX_STRING, gw_trans->prev_id ?
1404 gw_trans->prev_id : "", NULL);
1405 if (href_next == NULL) {
1406 error = got_error_from_errno("khttp_urlpartx");
1407 goto done;
1409 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1410 KATTR_HREF, href_next, KATTR__MAX);
1411 if (kerr != KCGI_OK)
1412 goto done;
1413 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1414 if (kerr != KCGI_OK)
1415 goto done;
1416 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1417 if (kerr != KCGI_OK)
1418 goto done;
1421 if (gw_trans->next_id || gw_trans->page > 0) {
1422 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1423 if (kerr != KCGI_OK)
1424 goto done;
1426 done:
1427 gw_free_header(header);
1428 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1429 gw_free_header(n_header);
1430 free(age);
1431 free(href_next);
1432 free(href_prev);
1433 free(href_diff);
1434 free(href_tree);
1435 if (error == NULL && kerr != KCGI_OK)
1436 error = gw_kcgi_error(kerr);
1437 return error;
1440 static const struct got_error *
1441 gw_summary(struct gw_trans *gw_trans)
1443 const struct got_error *error = NULL;
1444 char *age = NULL;
1445 enum kcgi_err kerr = KCGI_OK;
1447 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1448 return got_error_from_errno("pledge");
1450 error = gw_apply_unveil(gw_trans->gw_dir->path);
1451 if (error)
1452 goto done;
1454 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1455 "summary_wrapper", KATTR__MAX);
1456 if (kerr != KCGI_OK)
1457 return gw_kcgi_error(kerr);
1459 if (gw_trans->gw_conf->got_show_repo_description &&
1460 gw_trans->gw_dir->description != NULL &&
1461 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1462 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1463 KATTR_ID, "description_title", KATTR__MAX);
1464 if (kerr != KCGI_OK)
1465 goto done;
1466 kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1467 if (kerr != KCGI_OK)
1468 goto done;
1469 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1470 if (kerr != KCGI_OK)
1471 goto done;
1472 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1473 KATTR_ID, "description", KATTR__MAX);
1474 if (kerr != KCGI_OK)
1475 goto done;
1476 kerr = khtml_puts(gw_trans->gw_html_req,
1477 gw_trans->gw_dir->description);
1478 if (kerr != KCGI_OK)
1479 goto done;
1480 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1481 if (kerr != KCGI_OK)
1482 goto done;
1485 if (gw_trans->gw_conf->got_show_repo_owner &&
1486 gw_trans->gw_dir->owner != NULL &&
1487 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
1488 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1489 KATTR_ID, "repo_owner_title", KATTR__MAX);
1490 if (kerr != KCGI_OK)
1491 goto done;
1492 kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1493 if (kerr != KCGI_OK)
1494 goto done;
1495 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1496 if (kerr != KCGI_OK)
1497 goto done;
1498 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1499 KATTR_ID, "repo_owner", KATTR__MAX);
1500 if (kerr != KCGI_OK)
1501 goto done;
1502 kerr = khtml_puts(gw_trans->gw_html_req,
1503 gw_trans->gw_dir->owner);
1504 if (kerr != KCGI_OK)
1505 goto done;
1506 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1507 if (kerr != KCGI_OK)
1508 goto done;
1511 if (gw_trans->gw_conf->got_show_repo_age) {
1512 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1513 NULL, TM_LONG);
1514 if (error)
1515 goto done;
1516 if (age != NULL) {
1517 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1518 KATTR_ID, "last_change_title", KATTR__MAX);
1519 if (kerr != KCGI_OK)
1520 goto done;
1521 kerr = khtml_puts(gw_trans->gw_html_req,
1522 "Last Change: ");
1523 if (kerr != KCGI_OK)
1524 goto done;
1525 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1526 if (kerr != KCGI_OK)
1527 goto done;
1528 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1529 KATTR_ID, "last_change", KATTR__MAX);
1530 if (kerr != KCGI_OK)
1531 goto done;
1532 kerr = khtml_puts(gw_trans->gw_html_req, age);
1533 if (kerr != KCGI_OK)
1534 goto done;
1535 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1536 if (kerr != KCGI_OK)
1537 goto done;
1541 if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1542 gw_trans->gw_dir->url != NULL &&
1543 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1544 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1545 KATTR_ID, "cloneurl_title", KATTR__MAX);
1546 if (kerr != KCGI_OK)
1547 goto done;
1548 kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1549 if (kerr != KCGI_OK)
1550 goto done;
1551 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1552 if (kerr != KCGI_OK)
1553 goto done;
1554 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1555 KATTR_ID, "cloneurl", KATTR__MAX);
1556 if (kerr != KCGI_OK)
1557 goto done;
1558 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1559 if (kerr != KCGI_OK)
1560 goto done;
1561 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1562 if (kerr != KCGI_OK)
1563 goto done;
1566 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1567 if (kerr != KCGI_OK)
1568 goto done;
1570 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1571 "briefs_title_wrapper", KATTR__MAX);
1572 if (kerr != KCGI_OK)
1573 goto done;
1574 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1575 "briefs_title", KATTR__MAX);
1576 if (kerr != KCGI_OK)
1577 goto done;
1578 kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1579 if (kerr != KCGI_OK)
1580 goto done;
1581 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1582 if (kerr != KCGI_OK)
1583 goto done;
1584 error = gw_briefs(gw_trans);
1585 if (error)
1586 goto done;
1588 error = gw_tags(gw_trans);
1589 if (error)
1590 goto done;
1592 error = gw_output_repo_heads(gw_trans);
1593 done:
1594 free(age);
1595 if (error == NULL && kerr != KCGI_OK)
1596 error = gw_kcgi_error(kerr);
1597 return error;
1600 static const struct got_error *
1601 gw_tree(struct gw_trans *gw_trans)
1603 const struct got_error *error = NULL;
1604 struct gw_header *header = NULL;
1605 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1606 char *age = NULL;
1607 enum kcgi_err kerr = KCGI_OK;
1609 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1610 return got_error_from_errno("pledge");
1612 if ((header = gw_init_header()) == NULL)
1613 return got_error_from_errno("malloc");
1615 error = gw_apply_unveil(gw_trans->gw_dir->path);
1616 if (error)
1617 goto done;
1619 error = gw_get_header(gw_trans, header, 1);
1620 if (error)
1621 goto done;
1623 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1624 "tree_header_wrapper", KATTR__MAX);
1625 if (kerr != KCGI_OK)
1626 goto done;
1627 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1628 "tree_header", KATTR__MAX);
1629 if (kerr != KCGI_OK)
1630 goto done;
1631 error = gw_gen_tree_header(gw_trans, header->tree_id);
1632 if (error)
1633 goto done;
1634 error = gw_get_time_str(&age, header->committer_time,
1635 TM_LONG);
1636 if (error)
1637 goto done;
1638 error = gw_gen_age_header(gw_trans, age ?age : "");
1639 if (error)
1640 goto done;
1641 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1642 if (error)
1643 goto done;
1644 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1645 if (kerr != KCGI_OK)
1646 goto done;
1647 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1648 "dotted_line", KATTR__MAX);
1649 if (kerr != KCGI_OK)
1650 goto done;
1651 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1652 if (kerr != KCGI_OK)
1653 goto done;
1655 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1656 "tree", KATTR__MAX);
1657 if (kerr != KCGI_OK)
1658 goto done;
1659 error = gw_output_repo_tree(gw_trans, header);
1660 if (error)
1661 goto done;
1663 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1664 done:
1665 gw_free_header(header);
1666 free(tree_html_disp);
1667 free(tree_html);
1668 free(tree);
1669 free(age);
1670 if (error == NULL && kerr != KCGI_OK)
1671 error = gw_kcgi_error(kerr);
1672 return error;
1675 static const struct got_error *
1676 gw_tags(struct gw_trans *gw_trans)
1678 const struct got_error *error = NULL;
1679 struct gw_header *header = NULL;
1680 char *href_next = NULL, *href_prev = NULL;
1681 enum kcgi_err kerr = KCGI_OK;
1683 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1684 return got_error_from_errno("pledge");
1686 if ((header = gw_init_header()) == NULL)
1687 return got_error_from_errno("malloc");
1689 if (gw_trans->action != GW_SUMMARY) {
1690 error = gw_apply_unveil(gw_trans->gw_dir->path);
1691 if (error)
1692 goto done;
1695 error = gw_get_header(gw_trans, header, 1);
1696 if (error)
1697 goto done;
1699 if (gw_trans->action == GW_SUMMARY) {
1700 gw_trans->next_id = NULL;
1701 error = gw_output_repo_tags(gw_trans, header,
1702 D_MAXSLCOMMDISP, TAGBRIEF);
1703 if (error)
1704 goto done;
1705 } else {
1706 error = gw_output_repo_tags(gw_trans, header,
1707 gw_trans->gw_conf->got_max_commits_display, TAGBRIEF);
1708 if (error)
1709 goto done;
1712 if (gw_trans->next_id || gw_trans->page > 0) {
1713 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1714 KATTR_ID, "np_wrapper", KATTR__MAX);
1715 if (kerr != KCGI_OK)
1716 goto done;
1717 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1718 KATTR_ID, "nav_prev", KATTR__MAX);
1719 if (kerr != KCGI_OK)
1720 goto done;
1723 if (gw_trans->page > 0 && gw_trans->prev_id) {
1724 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1725 KATTRX_STRING, gw_trans->repo_name, "page",
1726 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1727 KATTRX_STRING, "tags", "commit", KATTRX_STRING,
1728 gw_trans->prev_id ? gw_trans->prev_id : "", "prev",
1729 KATTRX_STRING, gw_trans->prev_prev_id ?
1730 gw_trans->prev_prev_id : "", NULL);
1731 if (href_prev == NULL) {
1732 error = got_error_from_errno("khttp_urlpartx");
1733 goto done;
1735 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1736 KATTR_HREF, href_prev, KATTR__MAX);
1737 if (kerr != KCGI_OK)
1738 goto done;
1739 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1740 if (kerr != KCGI_OK)
1741 goto done;
1742 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1743 if (kerr != KCGI_OK)
1744 goto done;
1747 if (gw_trans->next_id || gw_trans->page > 0) {
1748 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1749 if (kerr != KCGI_OK)
1750 return gw_kcgi_error(kerr);
1753 if (gw_trans->next_id) {
1754 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1755 KATTR_ID, "nav_next", KATTR__MAX);
1756 if (kerr != KCGI_OK)
1757 goto done;
1758 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1759 KATTRX_STRING, gw_trans->repo_name, "page",
1760 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1761 KATTRX_STRING, "tags", "commit", KATTRX_STRING,
1762 gw_trans->next_id, "prev", KATTRX_STRING,
1763 gw_trans->next_prev_id ? gw_trans->next_prev_id : "",
1764 "prev_prev", KATTRX_STRING, gw_trans->prev_id ?
1765 gw_trans->prev_id : "", NULL);
1766 if (href_next == NULL) {
1767 error = got_error_from_errno("khttp_urlpartx");
1768 goto done;
1770 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1771 KATTR_HREF, href_next, KATTR__MAX);
1772 if (kerr != KCGI_OK)
1773 goto done;
1774 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1775 if (kerr != KCGI_OK)
1776 goto done;
1777 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1778 if (kerr != KCGI_OK)
1779 goto done;
1782 if (gw_trans->next_id || gw_trans->page > 0) {
1783 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1784 if (kerr != KCGI_OK)
1785 goto done;
1787 done:
1788 gw_free_header(header);
1789 free(href_next);
1790 free(href_prev);
1791 if (error == NULL && kerr != KCGI_OK)
1792 error = gw_kcgi_error(kerr);
1793 return error;
1796 static const struct got_error *
1797 gw_tag(struct gw_trans *gw_trans)
1799 const struct got_error *error = NULL;
1800 struct gw_header *header = NULL;
1801 enum kcgi_err kerr = KCGI_OK;
1803 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1804 return got_error_from_errno("pledge");
1806 if ((header = gw_init_header()) == NULL)
1807 return got_error_from_errno("malloc");
1809 error = gw_apply_unveil(gw_trans->gw_dir->path);
1810 if (error)
1811 goto done;
1813 if (gw_trans->commit_id == NULL) {
1814 error = got_error_msg(GOT_ERR_QUERYSTRING,
1815 "commit required in querystring");
1816 goto done;
1819 error = gw_get_header(gw_trans, header, 1);
1820 if (error)
1821 goto done;
1823 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1824 "tag_header_wrapper", KATTR__MAX);
1825 if (kerr != KCGI_OK)
1826 goto done;
1827 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1828 "tag_header", KATTR__MAX);
1829 if (kerr != KCGI_OK)
1830 goto done;
1831 error = gw_gen_commit_header(gw_trans, header->commit_id,
1832 header->refs_str);
1833 if (error)
1834 goto done;
1835 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1836 if (error)
1837 goto done;
1838 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1839 if (kerr != KCGI_OK)
1840 goto done;
1841 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1842 "dotted_line", KATTR__MAX);
1843 if (kerr != KCGI_OK)
1844 goto done;
1845 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1846 if (kerr != KCGI_OK)
1847 goto done;
1849 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1850 "tree", KATTR__MAX);
1851 if (kerr != KCGI_OK)
1852 goto done;
1854 error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1855 if (error)
1856 goto done;
1858 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1859 done:
1860 gw_free_header(header);
1861 if (error == NULL && kerr != KCGI_OK)
1862 error = gw_kcgi_error(kerr);
1863 return error;
1866 static const struct got_error *
1867 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1869 const struct got_error *error = NULL;
1870 DIR *dt;
1871 char *dir_test;
1872 int opened = 0;
1874 if (asprintf(&dir_test, "%s/%s/%s",
1875 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1876 GOTWEB_GIT_DIR) == -1)
1877 return got_error_from_errno("asprintf");
1879 dt = opendir(dir_test);
1880 if (dt == NULL) {
1881 free(dir_test);
1882 } else {
1883 gw_dir->path = strdup(dir_test);
1884 if (gw_dir->path == NULL) {
1885 opened = 1;
1886 error = got_error_from_errno("strdup");
1887 goto errored;
1889 opened = 1;
1890 goto done;
1893 if (asprintf(&dir_test, "%s/%s/%s",
1894 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1895 GOTWEB_GOT_DIR) == -1) {
1896 dir_test = NULL;
1897 error = got_error_from_errno("asprintf");
1898 goto errored;
1901 dt = opendir(dir_test);
1902 if (dt == NULL)
1903 free(dir_test);
1904 else {
1905 opened = 1;
1906 error = got_error(GOT_ERR_NOT_GIT_REPO);
1907 goto errored;
1910 if (asprintf(&dir_test, "%s/%s",
1911 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1) {
1912 error = got_error_from_errno("asprintf");
1913 dir_test = NULL;
1914 goto errored;
1917 gw_dir->path = strdup(dir_test);
1918 if (gw_dir->path == NULL) {
1919 opened = 1;
1920 error = got_error_from_errno("strdup");
1921 goto errored;
1924 dt = opendir(dir_test);
1925 if (dt == NULL) {
1926 error = got_error_path(gw_dir->name, GOT_ERR_NOT_GIT_REPO);
1927 goto errored;
1928 } else
1929 opened = 1;
1930 done:
1931 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1932 gw_dir->path);
1933 if (error)
1934 goto errored;
1935 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1936 if (error)
1937 goto errored;
1938 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1939 NULL, TM_DIFF);
1940 if (error)
1941 goto errored;
1942 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1943 errored:
1944 free(dir_test);
1945 if (opened)
1946 if (dt && closedir(dt) == -1 && error == NULL)
1947 error = got_error_from_errno("closedir");
1948 return error;
1951 static const struct got_error *
1952 gw_load_got_paths(struct gw_trans *gw_trans)
1954 const struct got_error *error = NULL;
1955 DIR *d;
1956 struct dirent **sd_dent;
1957 struct gw_dir *gw_dir;
1958 struct stat st;
1959 unsigned int d_cnt, d_i;
1961 d = opendir(gw_trans->gw_conf->got_repos_path);
1962 if (d == NULL) {
1963 error = got_error_from_errno2("opendir",
1964 gw_trans->gw_conf->got_repos_path);
1965 return error;
1968 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1969 alphasort);
1970 if (d_cnt == -1) {
1971 error = got_error_from_errno2("scandir",
1972 gw_trans->gw_conf->got_repos_path);
1973 goto done;
1976 for (d_i = 0; d_i < d_cnt; d_i++) {
1977 if (gw_trans->gw_conf->got_max_repos > 0 &&
1978 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1979 break; /* account for parent and self */
1981 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1982 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1983 continue;
1985 error = gw_init_gw_dir(&gw_dir, sd_dent[d_i]->d_name);
1986 if (error)
1987 goto done;
1989 error = gw_load_got_path(gw_trans, gw_dir);
1990 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1991 error = NULL;
1992 continue;
1994 else if (error)
1995 goto done;
1997 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1998 !got_path_dir_is_empty(gw_dir->path)) {
1999 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
2000 entry);
2001 gw_trans->repos_total++;
2004 done:
2005 if (d && closedir(d) == -1 && error == NULL)
2006 error = got_error_from_errno("closedir");
2007 return error;
2010 static const struct got_error *
2011 gw_parse_querystring(struct gw_trans *gw_trans)
2013 const struct got_error *error = NULL;
2014 struct kpair *p;
2015 struct gw_query_action *action = NULL;
2016 unsigned int i;
2018 if (gw_trans->gw_req->fieldnmap[0]) {
2019 return got_error(GOT_ERR_QUERYSTRING);
2020 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
2021 /* define gw_trans->repo_path */
2022 gw_trans->repo_name = p->parsed.s;
2024 if (asprintf(&gw_trans->repo_path, "%s/%s",
2025 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
2026 return got_error_from_errno("asprintf");
2028 /* get action and set function */
2029 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION])) {
2030 for (i = 0; i < nitems(gw_query_funcs); i++) {
2031 action = &gw_query_funcs[i];
2032 if (action->func_name == NULL)
2033 continue;
2034 if (strcmp(action->func_name,
2035 p->parsed.s) == 0) {
2036 gw_trans->action = i;
2037 break;
2041 if (gw_trans->action == -1) {
2042 gw_trans->action = GW_ERR;
2043 gw_trans->error = got_error_msg(GOT_ERR_QUERYSTRING,
2044 p != NULL ? "bad action in querystring" :
2045 "no action in querystring");
2046 return error;
2049 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID])) {
2050 if (asprintf(&gw_trans->commit_id, "%s",
2051 p->parsed.s) == -1)
2052 return got_error_from_errno("asprintf");
2055 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
2056 gw_trans->repo_file = p->parsed.s;
2058 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER])) {
2059 if (asprintf(&gw_trans->repo_folder, "%s",
2060 p->parsed.s) == -1)
2061 return got_error_from_errno("asprintf");
2064 if ((p = gw_trans->gw_req->fieldmap[KEY_PREV_ID])) {
2065 if (asprintf(&gw_trans->prev_id, "%s",
2066 p->parsed.s) == -1)
2067 return got_error_from_errno("asprintf");
2070 if ((p = gw_trans->gw_req->fieldmap[KEY_PREV_PREV_ID])) {
2071 if (asprintf(&gw_trans->prev_prev_id, "%s",
2072 p->parsed.s) == -1)
2073 return got_error_from_errno("asprintf");
2076 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
2077 gw_trans->headref = p->parsed.s;
2079 error = gw_init_gw_dir(&gw_trans->gw_dir, gw_trans->repo_name);
2080 if (error)
2081 return error;
2083 gw_trans->error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
2084 } else
2085 gw_trans->action = GW_INDEX;
2087 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
2088 gw_trans->page = p->parsed.i;
2090 return error;
2093 static const struct got_error *
2094 gw_init_gw_dir(struct gw_dir **gw_dir, const char *dir)
2096 const struct got_error *error;
2098 *gw_dir = malloc(sizeof(**gw_dir));
2099 if (*gw_dir == NULL)
2100 return got_error_from_errno("malloc");
2102 if (asprintf(&(*gw_dir)->name, "%s", dir) == -1) {
2103 error = got_error_from_errno("asprintf");
2104 free(*gw_dir);
2105 *gw_dir = NULL;
2106 return error;
2109 return NULL;
2112 static const struct got_error *
2113 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
2115 enum kcgi_err kerr = KCGI_OK;
2117 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
2118 if (kerr != KCGI_OK)
2119 return gw_kcgi_error(kerr);
2120 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
2121 khttps[code]);
2122 if (kerr != KCGI_OK)
2123 return gw_kcgi_error(kerr);
2124 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
2125 kmimetypes[mime]);
2126 if (kerr != KCGI_OK)
2127 return gw_kcgi_error(kerr);
2128 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
2129 "nosniff");
2130 if (kerr != KCGI_OK)
2131 return gw_kcgi_error(kerr);
2132 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
2133 if (kerr != KCGI_OK)
2134 return gw_kcgi_error(kerr);
2135 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
2136 "1; mode=block");
2137 if (kerr != KCGI_OK)
2138 return gw_kcgi_error(kerr);
2140 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
2141 kerr = khttp_head(gw_trans->gw_req,
2142 kresps[KRESP_CONTENT_DISPOSITION],
2143 "attachment; filename=%s", gw_trans->repo_file);
2144 if (kerr != KCGI_OK)
2145 return gw_kcgi_error(kerr);
2148 kerr = khttp_body(gw_trans->gw_req);
2149 return gw_kcgi_error(kerr);
2152 static const struct got_error *
2153 gw_display_index(struct gw_trans *gw_trans)
2155 const struct got_error *error;
2156 enum kcgi_err kerr = KCGI_OK;
2158 /* catch early querystring errors */
2159 if (gw_trans->error)
2160 gw_trans->action = GW_ERR;
2162 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
2163 if (error)
2164 return error;
2166 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
2167 if (kerr != KCGI_OK)
2168 return gw_kcgi_error(kerr);
2170 if (gw_trans->action != GW_BLOB) {
2171 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
2172 gw_query_funcs[gw_trans->action].template);
2173 if (kerr != KCGI_OK) {
2174 khtml_close(gw_trans->gw_html_req);
2175 return gw_kcgi_error(kerr);
2179 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
2182 static const struct got_error *
2183 gw_error(struct gw_trans *gw_trans)
2185 enum kcgi_err kerr = KCGI_OK;
2187 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->error->msg);
2189 return gw_kcgi_error(kerr);
2192 static int
2193 gw_template(size_t key, void *arg)
2195 const struct got_error *error = NULL;
2196 enum kcgi_err kerr = KCGI_OK;
2197 struct gw_trans *gw_trans = arg;
2198 char *ati = NULL, *fic32 = NULL, *fic16 = NULL;
2199 char *swm = NULL, *spt = NULL, *css = NULL, *logo = NULL;
2201 if (asprintf(&ati, "%s%s", gw_trans->gw_conf->got_www_path,
2202 "/apple-touch-icon.png") == -1)
2203 goto err;
2204 if (asprintf(&fic32, "%s%s", gw_trans->gw_conf->got_www_path,
2205 "/favicon-32x32.png") == -1)
2206 goto err;
2207 if (asprintf(&fic16, "%s%s", gw_trans->gw_conf->got_www_path,
2208 "/favicon-16x16.png") == -1)
2209 goto err;
2210 if (asprintf(&swm, "%s%s", gw_trans->gw_conf->got_www_path,
2211 "/site.webmanifest") == -1)
2212 goto err;
2213 if (asprintf(&spt, "%s%s", gw_trans->gw_conf->got_www_path,
2214 "/safari-pinned-tab.svg") == -1)
2215 goto err;
2216 if (asprintf(&css, "%s%s", gw_trans->gw_conf->got_www_path,
2217 "/gotweb.css") == -1)
2218 goto err;
2219 if (asprintf(&logo, "%s%s%s", gw_trans->gw_conf->got_www_path,
2220 gw_trans->gw_conf->got_www_path ? "/" : "",
2221 gw_trans->gw_conf->got_logo) == -1)
2222 goto err;
2224 switch (key) {
2225 case (TEMPL_HEAD):
2226 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2227 KATTR_NAME, "viewport",
2228 KATTR_CONTENT, "initial-scale=.75, user-scalable=yes",
2229 KATTR__MAX);
2230 if (kerr != KCGI_OK)
2231 return 0;
2232 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2233 if (kerr != KCGI_OK)
2234 return 0;
2235 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2236 KATTR_CHARSET, "utf-8",
2237 KATTR__MAX);
2238 if (kerr != KCGI_OK)
2239 return 0;
2240 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2241 if (kerr != KCGI_OK)
2242 return 0;
2243 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2244 KATTR_NAME, "msapplication-TileColor",
2245 KATTR_CONTENT, "#da532c", KATTR__MAX);
2246 if (kerr != KCGI_OK)
2247 return 0;
2248 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2249 if (kerr != KCGI_OK)
2250 return 0;
2251 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2252 KATTR_NAME, "theme-color",
2253 KATTR_CONTENT, "#ffffff", KATTR__MAX);
2254 if (kerr != KCGI_OK)
2255 return 0;
2256 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2257 if (kerr != KCGI_OK)
2258 return 0;
2259 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2260 KATTR_REL, "apple-touch-icon", KATTR_SIZES, "180x180",
2261 KATTR_HREF, ati, KATTR__MAX);
2262 if (kerr != KCGI_OK)
2263 return 0;
2264 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2265 if (kerr != KCGI_OK)
2266 return 0;
2267 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2268 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2269 "32x32", KATTR_HREF, fic32, KATTR__MAX);
2270 if (kerr != KCGI_OK)
2271 return 0;
2272 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2273 if (kerr != KCGI_OK)
2274 return 0;
2275 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2276 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2277 "16x16", KATTR_HREF, fic16, KATTR__MAX);
2278 if (kerr != KCGI_OK)
2279 return 0;
2280 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2281 if (kerr != KCGI_OK)
2282 return 0;
2283 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2284 KATTR_REL, "manifest", KATTR_HREF, swm,
2285 KATTR__MAX);
2286 if (kerr != KCGI_OK)
2287 return 0;
2288 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2289 if (kerr != KCGI_OK)
2290 return 0;
2291 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2292 KATTR_REL, "mask-icon", KATTR_HREF,
2293 spt, KATTR__MAX);
2294 if (kerr != KCGI_OK)
2295 return 0;
2296 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2297 if (kerr != KCGI_OK)
2298 return 0;
2299 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2300 KATTR_REL, "stylesheet", KATTR_TYPE, "text/css",
2301 KATTR_HREF, css, KATTR__MAX);
2302 if (kerr != KCGI_OK)
2303 return 0;
2304 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2305 if (kerr != KCGI_OK)
2306 return 0;
2307 break;
2308 case(TEMPL_HEADER):
2309 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2310 KATTR_ID, "got_link", KATTR__MAX);
2311 if (kerr != KCGI_OK)
2312 return 0;
2313 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2314 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
2315 KATTR_TARGET, "_sotd", KATTR__MAX);
2316 if (kerr != KCGI_OK)
2317 return 0;
2318 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
2319 KATTR_SRC, logo, KATTR__MAX);
2320 if (kerr != KCGI_OK)
2321 return 0;
2322 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2323 if (kerr != KCGI_OK)
2324 return 0;
2325 break;
2326 case (TEMPL_SITEPATH):
2327 error = gw_output_site_link(gw_trans);
2328 if (error)
2329 return 0;
2330 break;
2331 case(TEMPL_TITLE):
2332 if (gw_trans->gw_conf->got_site_name != NULL) {
2333 kerr = khtml_puts(gw_trans->gw_html_req,
2334 gw_trans->gw_conf->got_site_name);
2335 if (kerr != KCGI_OK)
2336 return 0;
2338 break;
2339 case (TEMPL_SEARCH):
2340 break;
2341 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
2342 "search", KATTR__MAX);
2343 if (kerr != KCGI_OK)
2344 return 0;
2345 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
2346 KATTR_METHOD, "POST", KATTR__MAX);
2347 if (kerr != KCGI_OK)
2348 return 0;
2349 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
2350 "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
2351 KATTR_MAXLENGTH, "50", KATTR__MAX);
2352 if (kerr != KCGI_OK)
2353 return 0;
2354 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
2355 KATTR__MAX);
2356 if (kerr != KCGI_OK)
2357 return 0;
2358 kerr = khtml_puts(gw_trans->gw_html_req, "Search");
2359 if (kerr != KCGI_OK)
2360 return 0;
2361 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
2362 if (kerr != KCGI_OK)
2363 return 0;
2364 break;
2365 case(TEMPL_SITEOWNER):
2366 if (gw_trans->gw_conf->got_site_owner != NULL &&
2367 gw_trans->gw_conf->got_show_site_owner) {
2368 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2369 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
2370 if (kerr != KCGI_OK)
2371 return 0;
2372 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2373 KATTR_ID, "site_owner", KATTR__MAX);
2374 if (kerr != KCGI_OK)
2375 return 0;
2376 kerr = khtml_puts(gw_trans->gw_html_req,
2377 gw_trans->gw_conf->got_site_owner);
2378 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2379 if (kerr != KCGI_OK)
2380 return 0;
2382 break;
2383 case(TEMPL_CONTENT):
2384 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
2385 if (error) {
2386 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2387 KATTR_ID, "tmpl_err", KATTR__MAX);
2388 if (kerr != KCGI_OK)
2389 return 0;
2390 kerr = khttp_printf(gw_trans->gw_req, "Error: %s",
2391 error->msg);
2392 if (kerr != KCGI_OK)
2393 return 0;
2394 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2395 if (kerr != KCGI_OK)
2396 return 0;
2398 break;
2399 default:
2400 return 0;
2402 free(ati);
2403 free(fic32);
2404 free(fic16);
2405 free(swm);
2406 free(spt);
2407 free(css);
2408 free(logo);
2409 return 1;
2410 err:
2411 free(ati);
2412 free(fic32);
2413 free(fic16);
2414 free(swm);
2415 free(spt);
2416 free(css);
2417 free(logo);
2418 return 0;
2421 static const struct got_error *
2422 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
2424 const struct got_error *error = NULL;
2425 enum kcgi_err kerr = KCGI_OK;
2427 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2428 KATTR_ID, "header_commit_title", KATTR__MAX);
2429 if (kerr != KCGI_OK)
2430 goto done;
2431 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
2432 if (kerr != KCGI_OK)
2433 goto done;
2434 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2435 if (kerr != KCGI_OK)
2436 goto done;
2437 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2438 KATTR_ID, "header_commit", KATTR__MAX);
2439 if (kerr != KCGI_OK)
2440 goto done;
2441 kerr = khtml_printf(gw_trans->gw_html_req, "%s ", str1);
2442 if (kerr != KCGI_OK)
2443 goto done;
2444 if (str2 != NULL) {
2445 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
2446 KATTR_ID, "refs_str", KATTR__MAX);
2447 if (kerr != KCGI_OK)
2448 goto done;
2449 kerr = khtml_printf(gw_trans->gw_html_req, "(%s)", str2);
2450 if (kerr != KCGI_OK)
2451 goto done;
2452 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2453 if (kerr != KCGI_OK)
2454 goto done;
2456 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2457 done:
2458 if (error == NULL && kerr != KCGI_OK)
2459 error = gw_kcgi_error(kerr);
2460 return error;
2463 static const struct got_error *
2464 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2466 const struct got_error *error = NULL;
2467 enum kcgi_err kerr = KCGI_OK;
2469 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2470 KATTR_ID, "header_diff_title", KATTR__MAX);
2471 if (kerr != KCGI_OK)
2472 goto done;
2473 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2474 if (kerr != KCGI_OK)
2475 goto done;
2476 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2477 if (kerr != KCGI_OK)
2478 goto done;
2479 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2480 KATTR_ID, "header_diff", KATTR__MAX);
2481 if (kerr != KCGI_OK)
2482 goto done;
2483 if (str1 != NULL) {
2484 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2485 if (kerr != KCGI_OK)
2486 goto done;
2488 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2489 if (kerr != KCGI_OK)
2490 goto done;
2491 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2492 if (kerr != KCGI_OK)
2493 goto done;
2494 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2495 done:
2496 if (error == NULL && kerr != KCGI_OK)
2497 error = gw_kcgi_error(kerr);
2498 return error;
2501 static const struct got_error *
2502 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2504 const struct got_error *error = NULL;
2505 enum kcgi_err kerr = KCGI_OK;
2507 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2508 KATTR_ID, "header_age_title", KATTR__MAX);
2509 if (kerr != KCGI_OK)
2510 goto done;
2511 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2512 if (kerr != KCGI_OK)
2513 goto done;
2514 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2515 if (kerr != KCGI_OK)
2516 goto done;
2517 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2518 KATTR_ID, "header_age", KATTR__MAX);
2519 if (kerr != KCGI_OK)
2520 goto done;
2521 kerr = khtml_puts(gw_trans->gw_html_req, str);
2522 if (kerr != KCGI_OK)
2523 goto done;
2524 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2525 done:
2526 if (error == NULL && kerr != KCGI_OK)
2527 error = gw_kcgi_error(kerr);
2528 return error;
2531 static const struct got_error *
2532 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2534 const struct got_error *error = NULL;
2535 enum kcgi_err kerr = KCGI_OK;
2537 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2538 KATTR_ID, "header_author_title", KATTR__MAX);
2539 if (kerr != KCGI_OK)
2540 goto done;
2541 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2542 if (kerr != KCGI_OK)
2543 goto done;
2544 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2545 if (kerr != KCGI_OK)
2546 goto done;
2547 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2548 KATTR_ID, "header_author", KATTR__MAX);
2549 if (kerr != KCGI_OK)
2550 goto done;
2551 kerr = khtml_puts(gw_trans->gw_html_req, str);
2552 if (kerr != KCGI_OK)
2553 goto done;
2554 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2555 done:
2556 if (error == NULL && kerr != KCGI_OK)
2557 error = gw_kcgi_error(kerr);
2558 return error;
2561 static const struct got_error *
2562 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2564 const struct got_error *error = NULL;
2565 enum kcgi_err kerr = KCGI_OK;
2567 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2568 KATTR_ID, "header_committer_title", KATTR__MAX);
2569 if (kerr != KCGI_OK)
2570 goto done;
2571 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2572 if (kerr != KCGI_OK)
2573 goto done;
2574 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2575 if (kerr != KCGI_OK)
2576 goto done;
2577 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2578 KATTR_ID, "header_committer", KATTR__MAX);
2579 if (kerr != KCGI_OK)
2580 goto done;
2581 kerr = khtml_puts(gw_trans->gw_html_req, str);
2582 if (kerr != KCGI_OK)
2583 goto done;
2584 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2585 done:
2586 if (error == NULL && kerr != KCGI_OK)
2587 error = gw_kcgi_error(kerr);
2588 return error;
2591 static const struct got_error *
2592 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2594 const struct got_error *error = NULL;
2595 enum kcgi_err kerr = KCGI_OK;
2597 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2598 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2599 if (kerr != KCGI_OK)
2600 goto done;
2601 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2602 if (kerr != KCGI_OK)
2603 goto done;
2604 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2605 if (kerr != KCGI_OK)
2606 goto done;
2607 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2608 KATTR_ID, "header_commit_msg", KATTR__MAX);
2609 if (kerr != KCGI_OK)
2610 goto done;
2611 kerr = khttp_puts(gw_trans->gw_req, str);
2612 if (kerr != KCGI_OK)
2613 goto done;
2614 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2615 done:
2616 if (error == NULL && kerr != KCGI_OK)
2617 error = gw_kcgi_error(kerr);
2618 return error;
2621 static const struct got_error *
2622 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2624 const struct got_error *error = NULL;
2625 enum kcgi_err kerr = KCGI_OK;
2627 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2628 KATTR_ID, "header_tree_title", KATTR__MAX);
2629 if (kerr != KCGI_OK)
2630 goto done;
2631 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2632 if (kerr != KCGI_OK)
2633 goto done;
2634 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2635 if (kerr != KCGI_OK)
2636 goto done;
2637 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2638 KATTR_ID, "header_tree", KATTR__MAX);
2639 if (kerr != KCGI_OK)
2640 goto done;
2641 kerr = khtml_puts(gw_trans->gw_html_req, str);
2642 if (kerr != KCGI_OK)
2643 goto done;
2644 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2645 done:
2646 if (error == NULL && kerr != KCGI_OK)
2647 error = gw_kcgi_error(kerr);
2648 return error;
2651 static const struct got_error *
2652 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2653 char *dir)
2655 const struct got_error *error = NULL;
2656 FILE *f = NULL;
2657 char *d_file = NULL;
2658 unsigned int len;
2659 size_t n;
2661 *description = NULL;
2662 if (gw_trans->gw_conf->got_show_repo_description == 0)
2663 return NULL;
2665 if (asprintf(&d_file, "%s/description", dir) == -1)
2666 return got_error_from_errno("asprintf");
2668 f = fopen(d_file, "r");
2669 if (f == NULL) {
2670 if (errno == ENOENT || errno == EACCES)
2671 return NULL;
2672 error = got_error_from_errno2("fopen", d_file);
2673 goto done;
2676 if (fseek(f, 0, SEEK_END) == -1) {
2677 error = got_ferror(f, GOT_ERR_IO);
2678 goto done;
2680 len = ftell(f);
2681 if (len == -1) {
2682 error = got_ferror(f, GOT_ERR_IO);
2683 goto done;
2685 if (fseek(f, 0, SEEK_SET) == -1) {
2686 error = got_ferror(f, GOT_ERR_IO);
2687 goto done;
2689 *description = calloc(len + 1, sizeof(**description));
2690 if (*description == NULL) {
2691 error = got_error_from_errno("calloc");
2692 goto done;
2695 n = fread(*description, 1, len, f);
2696 if (n == 0 && ferror(f))
2697 error = got_ferror(f, GOT_ERR_IO);
2698 done:
2699 if (f != NULL && fclose(f) == EOF && error == NULL)
2700 error = got_error_from_errno("fclose");
2701 free(d_file);
2702 return error;
2705 static const struct got_error *
2706 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2708 struct tm tm;
2709 time_t diff_time;
2710 char *years = "years ago", *months = "months ago";
2711 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2712 char *minutes = "minutes ago", *seconds = "seconds ago";
2713 char *now = "right now";
2714 char *s;
2715 char datebuf[29];
2717 *repo_age = NULL;
2719 switch (ref_tm) {
2720 case TM_DIFF:
2721 diff_time = time(NULL) - committer_time;
2722 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2723 if (asprintf(repo_age, "%lld %s",
2724 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2725 return got_error_from_errno("asprintf");
2726 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2727 if (asprintf(repo_age, "%lld %s",
2728 (diff_time / 60 / 60 / 24 / (365 / 12)),
2729 months) == -1)
2730 return got_error_from_errno("asprintf");
2731 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2732 if (asprintf(repo_age, "%lld %s",
2733 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2734 return got_error_from_errno("asprintf");
2735 } else if (diff_time > 60 * 60 * 24 * 2) {
2736 if (asprintf(repo_age, "%lld %s",
2737 (diff_time / 60 / 60 / 24), days) == -1)
2738 return got_error_from_errno("asprintf");
2739 } else if (diff_time > 60 * 60 * 2) {
2740 if (asprintf(repo_age, "%lld %s",
2741 (diff_time / 60 / 60), hours) == -1)
2742 return got_error_from_errno("asprintf");
2743 } else if (diff_time > 60 * 2) {
2744 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2745 minutes) == -1)
2746 return got_error_from_errno("asprintf");
2747 } else if (diff_time > 2) {
2748 if (asprintf(repo_age, "%lld %s", diff_time,
2749 seconds) == -1)
2750 return got_error_from_errno("asprintf");
2751 } else {
2752 if (asprintf(repo_age, "%s", now) == -1)
2753 return got_error_from_errno("asprintf");
2755 break;
2756 case TM_LONG:
2757 if (gmtime_r(&committer_time, &tm) == NULL)
2758 return got_error_from_errno("gmtime_r");
2760 s = asctime_r(&tm, datebuf);
2761 if (s == NULL)
2762 return got_error_from_errno("asctime_r");
2764 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2765 return got_error_from_errno("asprintf");
2766 break;
2768 return NULL;
2771 static const struct got_error *
2772 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2773 const char *refname, int ref_tm)
2775 const struct got_error *error = NULL;
2776 struct got_repository *repo = NULL;
2777 struct got_commit_object *commit = NULL;
2778 struct got_reflist_head refs;
2779 struct got_reflist_entry *re;
2780 time_t committer_time = 0, cmp_time = 0;
2782 *repo_age = NULL;
2783 TAILQ_INIT(&refs);
2785 if (gw_trans->gw_conf->got_show_repo_age == 0)
2786 return NULL;
2788 if (gw_trans->repo)
2789 repo = gw_trans->repo;
2790 else {
2791 error = got_repo_open(&repo, dir, NULL);
2792 if (error)
2793 return error;
2796 error = got_ref_list(&refs, repo, "refs/heads",
2797 got_ref_cmp_by_name, NULL);
2798 if (error)
2799 goto done;
2802 * Find the youngest branch tip in the repository, or the age of
2803 * the a specific branch tip if a name was provided by the caller.
2805 TAILQ_FOREACH(re, &refs, entry) {
2806 struct got_object_id *id = NULL;
2808 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
2809 continue;
2811 error = got_ref_resolve(&id, repo, re->ref);
2812 if (error)
2813 goto done;
2815 error = got_object_open_as_commit(&commit, repo, id);
2816 free(id);
2817 if (error)
2818 goto done;
2820 committer_time =
2821 got_object_commit_get_committer_time(commit);
2822 got_object_commit_close(commit);
2823 if (cmp_time < committer_time)
2824 cmp_time = committer_time;
2826 if (refname)
2827 break;
2830 if (cmp_time != 0) {
2831 committer_time = cmp_time;
2832 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2834 done:
2835 got_ref_list_free(&refs);
2836 if (gw_trans->repo == NULL)
2837 got_repo_close(repo);
2838 return error;
2841 static const struct got_error *
2842 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2844 const struct got_error *error;
2845 FILE *f = NULL;
2846 struct got_object_id *id1 = NULL, *id2 = NULL;
2847 char *label1 = NULL, *label2 = NULL, *line = NULL;
2848 int obj_type;
2849 size_t linesize = 0;
2850 ssize_t linelen;
2851 enum kcgi_err kerr = KCGI_OK;
2853 f = got_opentemp();
2854 if (f == NULL)
2855 return NULL;
2857 if (header->parent_id != NULL &&
2858 strncmp(header->parent_id, "/dev/null", 9) != 0) {
2859 error = got_repo_match_object_id(&id1, &label1,
2860 header->parent_id, GOT_OBJ_TYPE_ANY,
2861 &header->refs, gw_trans->repo);
2862 if (error)
2863 goto done;
2866 error = got_repo_match_object_id(&id2, &label2,
2867 header->commit_id, GOT_OBJ_TYPE_ANY, &header->refs,
2868 gw_trans->repo);
2869 if (error)
2870 goto done;
2872 error = got_object_get_type(&obj_type, gw_trans->repo, id2);
2873 if (error)
2874 goto done;
2875 switch (obj_type) {
2876 case GOT_OBJ_TYPE_BLOB:
2877 error = got_diff_objects_as_blobs(NULL, NULL, id1, id2,
2878 NULL, NULL, 3, 0, 0, gw_trans->repo, f);
2879 break;
2880 case GOT_OBJ_TYPE_TREE:
2881 error = got_diff_objects_as_trees(NULL, NULL, id1, id2,
2882 "", "", 3, 0, 0, gw_trans->repo, f);
2883 break;
2884 case GOT_OBJ_TYPE_COMMIT:
2885 error = got_diff_objects_as_commits(NULL, NULL, id1, id2,
2886 3, 0, 0, gw_trans->repo, f);
2887 break;
2888 default:
2889 error = got_error(GOT_ERR_OBJ_TYPE);
2891 if (error)
2892 goto done;
2894 if (fseek(f, 0, SEEK_SET) == -1) {
2895 error = got_ferror(f, GOT_ERR_IO);
2896 goto done;
2899 while ((linelen = getline(&line, &linesize, f)) != -1) {
2900 error = gw_colordiff_line(gw_trans, line);
2901 if (error)
2902 goto done;
2903 /* XXX: KHTML_PRETTY breaks this */
2904 kerr = khtml_puts(gw_trans->gw_html_req, line);
2905 if (kerr != KCGI_OK)
2906 goto done;
2907 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2908 if (kerr != KCGI_OK)
2909 goto done;
2911 if (linelen == -1 && ferror(f))
2912 error = got_error_from_errno("getline");
2913 done:
2914 if (f && fclose(f) == EOF && error == NULL)
2915 error = got_error_from_errno("fclose");
2916 free(line);
2917 free(label1);
2918 free(label2);
2919 free(id1);
2920 free(id2);
2922 if (error == NULL && kerr != KCGI_OK)
2923 error = gw_kcgi_error(kerr);
2924 return error;
2927 static const struct got_error *
2928 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2930 const struct got_error *error = NULL;
2931 struct got_repository *repo;
2932 const char *gitconfig_owner;
2934 *owner = NULL;
2936 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2937 return NULL;
2939 error = got_repo_open(&repo, dir, NULL);
2940 if (error)
2941 return error;
2942 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2943 if (gitconfig_owner) {
2944 *owner = strdup(gitconfig_owner);
2945 if (*owner == NULL)
2946 error = got_error_from_errno("strdup");
2948 got_repo_close(repo);
2949 return error;
2952 static const struct got_error *
2953 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2955 const struct got_error *error = NULL;
2956 FILE *f;
2957 char *d_file = NULL;
2958 unsigned int len;
2959 size_t n;
2961 *url = NULL;
2963 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2964 return got_error_from_errno("asprintf");
2966 f = fopen(d_file, "r");
2967 if (f == NULL) {
2968 if (errno != ENOENT && errno != EACCES)
2969 error = got_error_from_errno2("fopen", d_file);
2970 goto done;
2973 if (fseek(f, 0, SEEK_END) == -1) {
2974 error = got_ferror(f, GOT_ERR_IO);
2975 goto done;
2977 len = ftell(f);
2978 if (len == -1) {
2979 error = got_ferror(f, GOT_ERR_IO);
2980 goto done;
2982 if (fseek(f, 0, SEEK_SET) == -1) {
2983 error = got_ferror(f, GOT_ERR_IO);
2984 goto done;
2987 *url = calloc(len + 1, sizeof(**url));
2988 if (*url == NULL) {
2989 error = got_error_from_errno("calloc");
2990 goto done;
2993 n = fread(*url, 1, len, f);
2994 if (n == 0 && ferror(f))
2995 error = got_ferror(f, GOT_ERR_IO);
2996 done:
2997 if (f && fclose(f) == EOF && error == NULL)
2998 error = got_error_from_errno("fclose");
2999 free(d_file);
3000 return NULL;
3003 static const struct got_error *
3004 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
3005 int limit, int tag_type)
3007 const struct got_error *error = NULL;
3008 struct got_reflist_head refs;
3009 struct got_reflist_entry *re;
3010 char *age = NULL;
3011 char *id_str = NULL, *newline, *href_commits = NULL;
3012 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
3013 struct got_tag_object *tag = NULL;
3014 enum kcgi_err kerr = KCGI_OK;
3015 int summary_header_displayed = 0, start_tag = 0, chk_next = 0;
3016 int prev_set = 0, tag_count = 0;
3018 TAILQ_INIT(&refs);
3020 error = got_ref_list(&refs, gw_trans->repo, "refs/tags",
3021 got_ref_cmp_tags, gw_trans->repo);
3022 if (error)
3023 goto done;
3025 TAILQ_FOREACH(re, &refs, entry) {
3026 const char *refname;
3027 const char *tagger;
3028 const char *tag_commit;
3029 time_t tagger_time;
3030 struct got_object_id *id;
3031 struct got_commit_object *commit = NULL;
3033 refname = got_ref_get_name(re->ref);
3034 if (strncmp(refname, "refs/tags/", 10) != 0)
3035 continue;
3036 refname += 10;
3038 error = got_ref_resolve(&id, gw_trans->repo, re->ref);
3039 if (error)
3040 goto done;
3042 error = got_object_open_as_tag(&tag, gw_trans->repo, id);
3043 if (error) {
3044 if (error->code != GOT_ERR_OBJ_TYPE) {
3045 free(id);
3046 goto done;
3048 /* "lightweight" tag */
3049 error = got_object_open_as_commit(&commit,
3050 gw_trans->repo, id);
3051 if (error) {
3052 free(id);
3053 goto done;
3055 tagger = got_object_commit_get_committer(commit);
3056 tagger_time =
3057 got_object_commit_get_committer_time(commit);
3058 error = got_object_id_str(&id_str, id);
3059 free(id);
3060 } else {
3061 free(id);
3062 tagger = got_object_tag_get_tagger(tag);
3063 tagger_time = got_object_tag_get_tagger_time(tag);
3064 error = got_object_id_str(&id_str,
3065 got_object_tag_get_object_id(tag));
3067 if (error)
3068 goto done;
3070 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
3071 strlen(id_str)) != 0)
3072 continue;
3074 if (tag_type == TAGBRIEF && gw_trans->commit_id &&
3075 start_tag == 0 && strncmp(id_str, header->commit_id,
3076 strlen(id_str)) != 0) {
3077 continue;
3078 } else {
3079 start_tag = 1;
3082 tag_count++;
3084 if (prev_set == 0 && start_tag == 1) {
3085 gw_trans->next_prev_id = strdup(id_str);
3086 if (gw_trans->next_prev_id == NULL) {
3087 error = got_error_from_errno("strdup");
3088 goto done;
3090 prev_set = 1;
3093 if (chk_next) {
3094 gw_trans->next_id = strdup(id_str);
3095 if (gw_trans->next_id == NULL)
3096 error = got_error_from_errno("strdup");
3097 goto done;
3100 if (commit) {
3101 error = got_object_commit_get_logmsg(&tag_commit0,
3102 commit);
3103 if (error)
3104 goto done;
3105 got_object_commit_close(commit);
3106 } else {
3107 tag_commit0 = strdup(got_object_tag_get_message(tag));
3108 if (tag_commit0 == NULL) {
3109 error = got_error_from_errno("strdup");
3110 goto done;
3114 tag_commit = tag_commit0;
3115 while (*tag_commit == '\n')
3116 tag_commit++;
3118 switch (tag_type) {
3119 case TAGBRIEF:
3120 newline = strchr(tag_commit, '\n');
3121 if (newline)
3122 *newline = '\0';
3124 if (summary_header_displayed == 0) {
3125 kerr = khtml_attr(gw_trans->gw_html_req,
3126 KELEM_DIV, KATTR_ID,
3127 "summary_tags_title_wrapper", KATTR__MAX);
3128 if (kerr != KCGI_OK)
3129 goto done;
3130 kerr = khtml_attr(gw_trans->gw_html_req,
3131 KELEM_DIV, KATTR_ID,
3132 "summary_tags_title", KATTR__MAX);
3133 if (kerr != KCGI_OK)
3134 goto done;
3135 kerr = khtml_puts(gw_trans->gw_html_req,
3136 "Tags");
3137 if (kerr != KCGI_OK)
3138 goto done;
3139 kerr = khtml_closeelem(gw_trans->gw_html_req,
3140 2);
3141 if (kerr != KCGI_OK)
3142 goto done;
3143 kerr = khtml_attr(gw_trans->gw_html_req,
3144 KELEM_DIV, KATTR_ID,
3145 "summary_tags_content", KATTR__MAX);
3146 if (kerr != KCGI_OK)
3147 goto done;
3148 summary_header_displayed = 1;
3151 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3152 KATTR_ID, "tag_wrapper", KATTR__MAX);
3153 if (kerr != KCGI_OK)
3154 goto done;
3155 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3156 KATTR_ID, "tag_age", KATTR__MAX);
3157 if (kerr != KCGI_OK)
3158 goto done;
3159 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
3160 if (error)
3161 goto done;
3162 kerr = khtml_puts(gw_trans->gw_html_req,
3163 age ? age : "");
3164 if (kerr != KCGI_OK)
3165 goto done;
3166 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3167 if (kerr != KCGI_OK)
3168 goto done;
3169 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3170 KATTR_ID, "tag", KATTR__MAX);
3171 if (kerr != KCGI_OK)
3172 goto done;
3173 kerr = khtml_puts(gw_trans->gw_html_req, refname);
3174 if (kerr != KCGI_OK)
3175 goto done;
3176 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3177 if (kerr != KCGI_OK)
3178 goto done;
3179 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3180 KATTR_ID, "tag_name", KATTR__MAX);
3181 if (kerr != KCGI_OK)
3182 goto done;
3184 href_tag = khttp_urlpart(NULL, NULL, "gotweb", "path",
3185 gw_trans->repo_name, "action", "tag", "commit",
3186 id_str, NULL);
3187 if (href_tag == NULL) {
3188 error = got_error_from_errno("khttp_urlpart");
3189 goto done;
3191 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3192 KATTR_HREF, href_tag, KATTR__MAX);
3193 if (kerr != KCGI_OK)
3194 goto done;
3195 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
3196 if (kerr != KCGI_OK)
3197 goto done;
3198 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3199 if (kerr != KCGI_OK)
3200 goto done;
3202 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3203 KATTR_ID, "navs_wrapper", KATTR__MAX);
3204 if (kerr != KCGI_OK)
3205 goto done;
3206 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3207 KATTR_ID, "navs", KATTR__MAX);
3208 if (kerr != KCGI_OK)
3209 goto done;
3211 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3212 KATTR_HREF, href_tag, KATTR__MAX);
3213 if (kerr != KCGI_OK)
3214 goto done;
3215 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
3216 if (kerr != KCGI_OK)
3217 goto done;
3218 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3219 if (kerr != KCGI_OK)
3220 goto done;
3222 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3223 if (kerr != KCGI_OK)
3224 goto done;
3226 href_briefs = khttp_urlpart(NULL, NULL, "gotweb",
3227 "path", gw_trans->repo_name, "action", "briefs",
3228 "commit", id_str, NULL);
3229 if (href_briefs == NULL) {
3230 error = got_error_from_errno("khttp_urlpart");
3231 goto done;
3233 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3234 KATTR_HREF, href_briefs, KATTR__MAX);
3235 if (kerr != KCGI_OK)
3236 goto done;
3237 kerr = khtml_puts(gw_trans->gw_html_req,
3238 "commit briefs");
3239 if (kerr != KCGI_OK)
3240 goto done;
3241 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3242 if (kerr != KCGI_OK)
3243 goto done;
3245 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3246 if (kerr != KCGI_OK)
3247 goto done;
3249 href_commits = khttp_urlpart(NULL, NULL, "gotweb",
3250 "path", gw_trans->repo_name, "action", "commits",
3251 "commit", id_str, NULL);
3252 if (href_commits == NULL) {
3253 error = got_error_from_errno("khttp_urlpart");
3254 goto done;
3256 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3257 KATTR_HREF, href_commits, KATTR__MAX);
3258 if (kerr != KCGI_OK)
3259 goto done;
3260 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
3261 if (kerr != KCGI_OK)
3262 goto done;
3263 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3264 if (kerr != KCGI_OK)
3265 goto done;
3267 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3268 KATTR_ID, "dotted_line", KATTR__MAX);
3269 if (kerr != KCGI_OK)
3270 goto done;
3271 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3272 if (kerr != KCGI_OK)
3273 goto done;
3274 break;
3275 case TAGFULL:
3276 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3277 KATTR_ID, "tag_info_date_title", KATTR__MAX);
3278 if (kerr != KCGI_OK)
3279 goto done;
3280 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
3281 if (kerr != KCGI_OK)
3282 goto done;
3283 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3284 if (kerr != KCGI_OK)
3285 goto done;
3286 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3287 KATTR_ID, "tag_info_date", KATTR__MAX);
3288 if (kerr != KCGI_OK)
3289 goto done;
3290 error = gw_get_time_str(&age, tagger_time, TM_LONG);
3291 if (error)
3292 goto done;
3293 kerr = khtml_puts(gw_trans->gw_html_req,
3294 age ? age : "");
3295 if (kerr != KCGI_OK)
3296 goto done;
3297 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3298 if (kerr != KCGI_OK)
3299 goto done;
3301 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3302 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
3303 if (kerr != KCGI_OK)
3304 goto done;
3305 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
3306 if (kerr != KCGI_OK)
3307 goto done;
3308 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3309 if (kerr != KCGI_OK)
3310 goto done;
3311 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3312 KATTR_ID, "tag_info_date", KATTR__MAX);
3313 if (kerr != KCGI_OK)
3314 goto done;
3315 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
3316 if (kerr != KCGI_OK)
3317 goto done;
3318 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3319 if (kerr != KCGI_OK)
3320 goto done;
3322 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3323 KATTR_ID, "tag_info", KATTR__MAX);
3324 if (kerr != KCGI_OK)
3325 goto done;
3326 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
3327 if (kerr != KCGI_OK)
3328 goto done;
3329 break;
3330 default:
3331 break;
3333 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3334 if (kerr != KCGI_OK)
3335 goto done;
3337 if (limit && --limit == 0)
3338 chk_next = 1;
3340 if (tag)
3341 got_object_tag_close(tag);
3342 tag = NULL;
3343 free(id_str);
3344 id_str = NULL;
3345 free(age);
3346 age = NULL;
3347 free(tag_commit0);
3348 tag_commit0 = NULL;
3349 free(href_tag);
3350 href_tag = NULL;
3351 free(href_briefs);
3352 href_briefs = NULL;
3353 free(href_commits);
3354 href_commits = NULL;
3356 if (tag_count == 0) {
3357 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3358 "summary_tags_title_wrapper", KATTR__MAX);
3359 if (kerr != KCGI_OK)
3360 goto done;
3361 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3362 "summary_tags_title", KATTR__MAX);
3363 if (kerr != KCGI_OK)
3364 goto done;
3365 kerr = khtml_puts(gw_trans->gw_html_req, "Tags");
3366 if (kerr != KCGI_OK)
3367 goto done;
3368 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3369 if (kerr != KCGI_OK)
3370 goto done;
3371 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3372 "summary_tags_content", KATTR__MAX);
3373 if (kerr != KCGI_OK)
3374 goto done;
3375 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3376 "tags_info", KATTR__MAX);
3377 if (kerr != KCGI_OK)
3378 goto done;
3379 kerr = khttp_puts(gw_trans->gw_req,
3380 "There are no tags for this repo.");
3381 if (kerr != KCGI_OK)
3382 goto done;
3383 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3385 done:
3386 if (tag)
3387 got_object_tag_close(tag);
3388 free(id_str);
3389 free(age);
3390 free(tag_commit0);
3391 free(href_tag);
3392 free(href_briefs);
3393 free(href_commits);
3394 got_ref_list_free(&refs);
3395 if (error == NULL && kerr != KCGI_OK)
3396 error = gw_kcgi_error(kerr);
3397 return error;
3400 static void
3401 gw_free_header(struct gw_header *header)
3403 free(header->path);
3404 free(header->author);
3405 free(header->committer);
3406 free(header->refs_str);
3407 free(header->commit_id);
3408 free(header->parent_id);
3409 free(header->tree_id);
3410 free(header->commit_msg);
3413 static struct gw_header *
3414 gw_init_header()
3416 struct gw_header *header;
3418 header = malloc(sizeof(*header));
3419 if (header == NULL)
3420 return NULL;
3422 header->path = NULL;
3423 TAILQ_INIT(&header->refs);
3425 header->refs_str = NULL;
3426 header->commit_id = NULL;
3427 header->committer = NULL;
3428 header->author = NULL;
3429 header->parent_id = NULL;
3430 header->tree_id = NULL;
3431 header->commit_msg = NULL;
3433 return header;
3436 static const struct got_error *
3437 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
3438 int limit, struct got_object_id *id)
3440 const struct got_error *error = NULL;
3441 struct got_commit_graph *graph = NULL;
3442 struct got_commit_object *commit = NULL;
3443 int chk_next = 0, chk_multi = 0, prev_set = 0;
3445 error = got_commit_graph_open(&graph, header->path, 0);
3446 if (error)
3447 return error;
3449 error = got_commit_graph_iter_start(graph, id, gw_trans->repo, NULL,
3450 NULL);
3451 if (error)
3452 goto done;
3454 for (;;) {
3455 error = got_commit_graph_iter_next(&id, graph, gw_trans->repo,
3456 NULL, NULL);
3457 if (error) {
3458 if (error->code == GOT_ERR_ITER_COMPLETED)
3459 error = NULL;
3460 goto done;
3462 if (id == NULL)
3463 goto done;
3465 error = got_object_open_as_commit(&commit, gw_trans->repo, id);
3466 if (error)
3467 goto done;
3468 if (limit == 1 && chk_multi == 0 &&
3469 gw_trans->gw_conf->got_max_commits_display != 1) {
3470 error = gw_get_commit(gw_trans, header, commit, id);
3471 if (error)
3472 goto done;
3473 } else {
3474 chk_multi = 1;
3475 struct gw_header *n_header = NULL;
3476 if ((n_header = gw_init_header()) == NULL) {
3477 error = got_error_from_errno("malloc");
3478 goto done;
3480 error = got_ref_list(&n_header->refs, gw_trans->repo,
3481 NULL, got_ref_cmp_by_name, NULL);
3482 if (error)
3483 goto done;
3485 error = gw_get_commit(gw_trans, n_header, commit, id);
3486 if (error)
3487 goto done;
3488 got_ref_list_free(&n_header->refs);
3491 * we have a commit_id now, so copy it to next_prev_id
3492 * for navigation through gw_briefs and gw_commits
3494 if (gw_trans->next_prev_id == NULL && prev_set == 0 &&
3495 (gw_trans->action == GW_BRIEFS ||
3496 gw_trans->action == GW_COMMITS ||
3497 gw_trans->action == GW_SUMMARY)) {
3498 prev_set = 1;
3499 gw_trans->next_prev_id =
3500 strdup(n_header->commit_id);
3501 if (gw_trans->next_prev_id == NULL) {
3502 error = got_error_from_errno("strdup");
3503 goto done;
3508 * check for one more commit before breaking,
3509 * so we know whether to navicate through gw_briefs
3510 * gw_commits and gw_summary
3512 if (chk_next && (gw_trans->action == GW_BRIEFS||
3513 gw_trans->action == GW_COMMITS ||
3514 gw_trans->action == GW_SUMMARY)) {
3515 gw_trans->next_id = strdup(n_header->commit_id);
3516 if (gw_trans->next_id == NULL)
3517 error = got_error_from_errno("strdup");
3518 goto done;
3521 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3522 entry);
3524 if (error || (limit && --limit == 0)) {
3525 if (chk_multi == 0)
3526 break;
3527 chk_next = 1;
3530 done:
3531 if (commit != NULL)
3532 got_object_commit_close(commit);
3533 if (graph)
3534 got_commit_graph_close(graph);
3535 return error;
3538 static const struct got_error *
3539 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header,
3540 struct got_commit_object *commit, struct got_object_id *id)
3542 const struct got_error *error = NULL;
3543 struct got_reflist_entry *re;
3544 struct got_object_id *id2 = NULL;
3545 struct got_object_qid *parent_id;
3546 char *commit_msg = NULL, *commit_msg0;
3548 /*print commit*/
3549 TAILQ_FOREACH(re, &header->refs, entry) {
3550 char *s;
3551 const char *name;
3552 struct got_tag_object *tag = NULL;
3553 struct got_object_id *ref_id;
3554 int cmp;
3556 if (got_ref_is_symbolic(re->ref))
3557 continue;
3559 name = got_ref_get_name(re->ref);
3560 if (strncmp(name, "refs/", 5) == 0)
3561 name += 5;
3562 if (strncmp(name, "got/", 4) == 0)
3563 continue;
3564 if (strncmp(name, "heads/", 6) == 0)
3565 name += 6;
3566 if (strncmp(name, "remotes/", 8) == 0) {
3567 name += 8;
3568 s = strstr(name, "/" GOT_REF_HEAD);
3569 if (s != NULL && s[strlen(s)] == '\0')
3570 continue;
3572 error = got_ref_resolve(&ref_id, gw_trans->repo, re->ref);
3573 if (error)
3574 return error;
3575 if (strncmp(name, "tags/", 5) == 0) {
3576 error = got_object_open_as_tag(&tag, gw_trans->repo,
3577 ref_id);
3578 if (error) {
3579 if (error->code != GOT_ERR_OBJ_TYPE) {
3580 free(ref_id);
3581 continue;
3584 * Ref points at something other
3585 * than a tag.
3587 error = NULL;
3588 tag = NULL;
3591 cmp = got_object_id_cmp(tag ?
3592 got_object_tag_get_object_id(tag) : ref_id, id);
3593 free(ref_id);
3594 if (tag)
3595 got_object_tag_close(tag);
3596 if (cmp != 0)
3597 continue;
3598 s = header->refs_str;
3599 if (asprintf(&header->refs_str, "%s%s%s", s ? s : "",
3600 s ? ", " : "", name) == -1) {
3601 error = got_error_from_errno("asprintf");
3602 free(s);
3603 header->refs_str = NULL;
3604 return error;
3606 free(s);
3609 error = got_object_id_str(&header->commit_id, id);
3610 if (error)
3611 return error;
3613 error = got_object_id_str(&header->tree_id,
3614 got_object_commit_get_tree_id(commit));
3615 if (error)
3616 return error;
3618 if (gw_trans->action == GW_DIFF) {
3619 parent_id = SIMPLEQ_FIRST(
3620 got_object_commit_get_parent_ids(commit));
3621 if (parent_id != NULL) {
3622 id2 = got_object_id_dup(parent_id->id);
3623 free (parent_id);
3624 error = got_object_id_str(&header->parent_id, id2);
3625 if (error)
3626 return error;
3627 free(id2);
3628 } else {
3629 header->parent_id = strdup("/dev/null");
3630 if (header->parent_id == NULL) {
3631 error = got_error_from_errno("strdup");
3632 return error;
3637 header->committer_time =
3638 got_object_commit_get_committer_time(commit);
3640 header->author =
3641 strdup(got_object_commit_get_author(commit));
3642 if (header->author == NULL) {
3643 error = got_error_from_errno("strdup");
3644 return error;
3646 header->committer =
3647 strdup(got_object_commit_get_committer(commit));
3648 if (header->committer == NULL) {
3649 error = got_error_from_errno("strdup");
3650 return error;
3652 error = got_object_commit_get_logmsg(&commit_msg0, commit);
3653 if (error)
3654 return error;
3656 commit_msg = commit_msg0;
3657 while (*commit_msg == '\n')
3658 commit_msg++;
3660 header->commit_msg = strdup(commit_msg);
3661 if (header->commit_msg == NULL)
3662 error = got_error_from_errno("strdup");
3663 free(commit_msg0);
3664 return error;
3667 static const struct got_error *
3668 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3670 const struct got_error *error = NULL;
3671 char *in_repo_path = NULL;
3672 struct got_object_id *id = NULL;
3674 error = got_repo_open(&gw_trans->repo, gw_trans->repo_path, NULL);
3675 if (error)
3676 return error;
3678 if (gw_trans->commit_id == NULL) {
3679 struct got_reference *head_ref;
3680 error = got_ref_open(&head_ref, gw_trans->repo,
3681 gw_trans->headref, 0);
3682 if (error)
3683 return error;
3685 error = got_ref_resolve(&id, gw_trans->repo, head_ref);
3686 got_ref_close(head_ref);
3687 if (error)
3688 return error;
3689 } else {
3690 struct got_reference *ref;
3692 error = got_ref_open(&ref, gw_trans->repo,
3693 gw_trans->commit_id, 0);
3694 if (error == NULL) {
3695 int obj_type;
3696 error = got_ref_resolve(&id, gw_trans->repo, ref);
3697 got_ref_close(ref);
3698 if (error)
3699 return error;
3700 error = got_object_get_type(&obj_type, gw_trans->repo,
3701 id);
3702 if (error)
3703 goto done;
3704 if (obj_type == GOT_OBJ_TYPE_TAG) {
3705 struct got_tag_object *tag;
3706 error = got_object_open_as_tag(&tag,
3707 gw_trans->repo, id);
3708 if (error)
3709 goto done;
3710 if (got_object_tag_get_object_type(tag) !=
3711 GOT_OBJ_TYPE_COMMIT) {
3712 got_object_tag_close(tag);
3713 error = got_error(GOT_ERR_OBJ_TYPE);
3714 goto done;
3716 free(id);
3717 id = got_object_id_dup(
3718 got_object_tag_get_object_id(tag));
3719 if (id == NULL)
3720 error = got_error_from_errno(
3721 "got_object_id_dup");
3722 got_object_tag_close(tag);
3723 if (error)
3724 goto done;
3725 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3726 error = got_error(GOT_ERR_OBJ_TYPE);
3727 goto done;
3730 error = got_repo_match_object_id_prefix(&id,
3731 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT,
3732 gw_trans->repo);
3733 if (error)
3734 goto done;
3737 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
3738 gw_trans->repo_path);
3739 if (error)
3740 goto done;
3742 if (in_repo_path) {
3743 header->path = strdup(in_repo_path);
3744 if (header->path == NULL) {
3745 error = got_error_from_errno("strdup");
3746 goto done;
3750 error = got_ref_list(&header->refs, gw_trans->repo, NULL,
3751 got_ref_cmp_by_name, NULL);
3752 if (error)
3753 goto done;
3755 error = gw_get_commits(gw_trans, header, limit, id);
3756 done:
3757 got_ref_list_free(&header->refs);
3758 free(id);
3759 free(in_repo_path);
3760 return error;
3763 struct blame_line {
3764 int annotated;
3765 char *id_str;
3766 char *committer;
3767 char datebuf[11]; /* YYYY-MM-DD + NUL */
3770 struct gw_blame_cb_args {
3771 struct blame_line *lines;
3772 int nlines;
3773 int nlines_prec;
3774 int lineno_cur;
3775 off_t *line_offsets;
3776 FILE *f;
3777 struct got_repository *repo;
3778 struct gw_trans *gw_trans;
3781 static const struct got_error *
3782 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3784 const struct got_error *err = NULL;
3785 struct gw_blame_cb_args *a = arg;
3786 struct blame_line *bline;
3787 char *line = NULL;
3788 size_t linesize = 0;
3789 struct got_commit_object *commit = NULL;
3790 off_t offset;
3791 struct tm tm;
3792 time_t committer_time;
3793 enum kcgi_err kerr = KCGI_OK;
3795 if (nlines != a->nlines ||
3796 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3797 return got_error(GOT_ERR_RANGE);
3799 if (lineno == -1)
3800 return NULL; /* no change in this commit */
3802 /* Annotate this line. */
3803 bline = &a->lines[lineno - 1];
3804 if (bline->annotated)
3805 return NULL;
3806 err = got_object_id_str(&bline->id_str, id);
3807 if (err)
3808 return err;
3810 err = got_object_open_as_commit(&commit, a->repo, id);
3811 if (err)
3812 goto done;
3814 bline->committer = strdup(got_object_commit_get_committer(commit));
3815 if (bline->committer == NULL) {
3816 err = got_error_from_errno("strdup");
3817 goto done;
3820 committer_time = got_object_commit_get_committer_time(commit);
3821 if (localtime_r(&committer_time, &tm) == NULL)
3822 return got_error_from_errno("localtime_r");
3823 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3824 &tm) == 0) {
3825 err = got_error(GOT_ERR_NO_SPACE);
3826 goto done;
3828 bline->annotated = 1;
3830 /* Print lines annotated so far. */
3831 bline = &a->lines[a->lineno_cur - 1];
3832 if (!bline->annotated)
3833 goto done;
3835 offset = a->line_offsets[a->lineno_cur - 1];
3836 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3837 err = got_error_from_errno("fseeko");
3838 goto done;
3841 while (bline->annotated) {
3842 char *smallerthan, *at, *nl, *committer;
3843 char *href_diff = NULL;
3844 size_t len;
3846 if (getline(&line, &linesize, a->f) == -1) {
3847 if (ferror(a->f))
3848 err = got_error_from_errno("getline");
3849 break;
3852 committer = bline->committer;
3853 smallerthan = strchr(committer, '<');
3854 if (smallerthan && smallerthan[1] != '\0')
3855 committer = smallerthan + 1;
3856 at = strchr(committer, '@');
3857 if (at)
3858 *at = '\0';
3859 len = strlen(committer);
3860 if (len >= 9)
3861 committer[8] = '\0';
3863 nl = strchr(line, '\n');
3864 if (nl)
3865 *nl = '\0';
3867 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3868 "blame_wrapper", KATTR__MAX);
3869 if (kerr != KCGI_OK)
3870 goto err;
3871 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3872 "blame_number", KATTR__MAX);
3873 if (kerr != KCGI_OK)
3874 goto err;
3875 kerr = khtml_printf(a->gw_trans->gw_html_req, "%.*d",
3876 a->nlines_prec, a->lineno_cur);
3877 if (kerr != KCGI_OK)
3878 goto err;
3879 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3880 if (kerr != KCGI_OK)
3881 goto err;
3883 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3884 "blame_hash", KATTR__MAX);
3885 if (kerr != KCGI_OK)
3886 goto err;
3888 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
3889 a->gw_trans->repo_name, "action", "diff", "commit",
3890 bline->id_str, NULL);
3891 if (href_diff == NULL) {
3892 err = got_error_from_errno("khttp_urlpart");
3893 goto done;
3895 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3896 KATTR_HREF, href_diff, KATTR__MAX);
3897 if (kerr != KCGI_OK)
3898 goto done;
3899 kerr = khtml_printf(a->gw_trans->gw_html_req, "%.8s",
3900 bline->id_str);
3901 if (kerr != KCGI_OK)
3902 goto err;
3903 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3904 if (kerr != KCGI_OK)
3905 goto err;
3907 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3908 "blame_date", KATTR__MAX);
3909 if (kerr != KCGI_OK)
3910 goto err;
3911 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3912 if (kerr != KCGI_OK)
3913 goto err;
3914 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3915 if (kerr != KCGI_OK)
3916 goto err;
3918 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3919 "blame_author", KATTR__MAX);
3920 if (kerr != KCGI_OK)
3921 goto err;
3922 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
3923 if (kerr != KCGI_OK)
3924 goto err;
3925 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3926 if (kerr != KCGI_OK)
3927 goto err;
3929 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3930 "blame_code", KATTR__MAX);
3931 if (kerr != KCGI_OK)
3932 goto err;
3933 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
3934 if (kerr != KCGI_OK)
3935 goto err;
3936 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3937 if (kerr != KCGI_OK)
3938 goto err;
3940 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3941 if (kerr != KCGI_OK)
3942 goto err;
3944 a->lineno_cur++;
3945 bline = &a->lines[a->lineno_cur - 1];
3946 err:
3947 free(href_diff);
3949 done:
3950 if (commit)
3951 got_object_commit_close(commit);
3952 free(line);
3953 if (err == NULL && kerr != KCGI_OK)
3954 err = gw_kcgi_error(kerr);
3955 return err;
3958 static const struct got_error *
3959 gw_output_file_blame(struct gw_trans *gw_trans, struct gw_header *header)
3961 const struct got_error *error = NULL;
3962 struct got_object_id *obj_id = NULL;
3963 struct got_object_id *commit_id = NULL;
3964 struct got_blob_object *blob = NULL;
3965 char *path = NULL, *in_repo_path = NULL;
3966 struct gw_blame_cb_args bca;
3967 int i, obj_type;
3968 off_t filesize;
3970 if (asprintf(&path, "%s%s%s",
3971 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3972 gw_trans->repo_folder ? "/" : "",
3973 gw_trans->repo_file) == -1) {
3974 error = got_error_from_errno("asprintf");
3975 goto done;
3978 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path);
3979 if (error)
3980 goto done;
3982 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
3983 GOT_OBJ_TYPE_COMMIT, &header->refs, gw_trans->repo);
3984 if (error)
3985 goto done;
3987 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
3988 in_repo_path);
3989 if (error)
3990 goto done;
3992 if (obj_id == NULL) {
3993 error = got_error(GOT_ERR_NO_OBJ);
3994 goto done;
3997 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
3998 if (error)
3999 goto done;
4001 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4002 error = got_error(GOT_ERR_OBJ_TYPE);
4003 goto done;
4006 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
4007 if (error)
4008 goto done;
4010 bca.f = got_opentemp();
4011 if (bca.f == NULL) {
4012 error = got_error_from_errno("got_opentemp");
4013 goto done;
4015 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4016 &bca.line_offsets, bca.f, blob);
4017 if (error || bca.nlines == 0)
4018 goto done;
4020 /* Don't include \n at EOF in the blame line count. */
4021 if (bca.line_offsets[bca.nlines - 1] == filesize)
4022 bca.nlines--;
4024 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4025 if (bca.lines == NULL) {
4026 error = got_error_from_errno("calloc");
4027 goto done;
4029 bca.lineno_cur = 1;
4030 bca.nlines_prec = 0;
4031 i = bca.nlines;
4032 while (i > 0) {
4033 i /= 10;
4034 bca.nlines_prec++;
4036 bca.repo = gw_trans->repo;
4037 bca.gw_trans = gw_trans;
4039 error = got_blame(in_repo_path, commit_id, gw_trans->repo, gw_blame_cb,
4040 &bca, NULL, NULL);
4041 done:
4042 free(in_repo_path);
4043 free(commit_id);
4044 free(obj_id);
4045 free(path);
4047 if (blob) {
4048 free(bca.line_offsets);
4049 for (i = 0; i < bca.nlines; i++) {
4050 struct blame_line *bline = &bca.lines[i];
4051 free(bline->id_str);
4052 free(bline->committer);
4054 free(bca.lines);
4055 if (bca.f && fclose(bca.f) == EOF && error == NULL)
4056 error = got_error_from_errno("fclose");
4058 if (blob)
4059 got_object_blob_close(blob);
4060 return error;
4063 static const struct got_error *
4064 gw_output_blob_buf(struct gw_trans *gw_trans, struct gw_header *header)
4066 const struct got_error *error = NULL;
4067 struct got_object_id *obj_id = NULL;
4068 struct got_object_id *commit_id = NULL;
4069 struct got_blob_object *blob = NULL;
4070 char *path = NULL, *in_repo_path = NULL;
4071 int obj_type, set_mime = 0;
4072 size_t len, hdrlen;
4073 const uint8_t *buf;
4074 enum kcgi_err kerr = KCGI_OK;
4076 if (asprintf(&path, "%s%s%s",
4077 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4078 gw_trans->repo_folder ? "/" : "",
4079 gw_trans->repo_file) == -1) {
4080 error = got_error_from_errno("asprintf");
4081 goto done;
4084 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path);
4085 if (error)
4086 goto done;
4088 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
4089 GOT_OBJ_TYPE_COMMIT, &header->refs, gw_trans->repo);
4090 if (error)
4091 goto done;
4093 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
4094 in_repo_path);
4095 if (error)
4096 goto done;
4098 if (obj_id == NULL) {
4099 error = got_error(GOT_ERR_NO_OBJ);
4100 goto done;
4103 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
4104 if (error)
4105 goto done;
4107 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4108 error = got_error(GOT_ERR_OBJ_TYPE);
4109 goto done;
4112 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
4113 if (error)
4114 goto done;
4116 hdrlen = got_object_blob_get_hdrlen(blob);
4117 do {
4118 error = got_object_blob_read_block(&len, blob);
4119 if (error)
4120 goto done;
4121 buf = got_object_blob_get_read_buf(blob);
4124 * Skip blob object header first time around,
4125 * which also contains a zero byte.
4127 buf += hdrlen;
4128 if (set_mime == 0) {
4129 if (isbinary(buf, len - hdrlen))
4130 gw_trans->mime = KMIME_APP_OCTET_STREAM;
4131 else
4132 gw_trans->mime = KMIME_TEXT_PLAIN;
4133 set_mime = 1;
4134 error = gw_display_index(gw_trans);
4135 if (error)
4136 goto done;
4138 kerr = khttp_write(gw_trans->gw_req, buf, len - hdrlen);
4139 if (kerr != KCGI_OK)
4140 goto done;
4141 hdrlen = 0;
4142 } while (len != 0);
4143 done:
4144 free(in_repo_path);
4145 free(commit_id);
4146 free(obj_id);
4147 free(path);
4148 if (blob)
4149 got_object_blob_close(blob);
4150 if (error == NULL && kerr != KCGI_OK)
4151 error = gw_kcgi_error(kerr);
4152 return error;
4155 static const struct got_error *
4156 gw_output_repo_tree(struct gw_trans *gw_trans, struct gw_header *header)
4158 const struct got_error *error = NULL;
4159 struct got_object_id *tree_id = NULL, *commit_id = NULL;
4160 struct got_tree_object *tree = NULL;
4161 char *path = NULL, *in_repo_path = NULL;
4162 char *id_str = NULL;
4163 char *build_folder = NULL;
4164 char *href_blob = NULL, *href_blame = NULL;
4165 const char *class = NULL;
4166 int nentries, i, class_flip = 0;
4167 enum kcgi_err kerr = KCGI_OK;
4169 if (gw_trans->repo_folder != NULL) {
4170 path = strdup(gw_trans->repo_folder);
4171 if (path == NULL) {
4172 error = got_error_from_errno("strdup");
4173 goto done;
4175 } else {
4176 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
4177 gw_trans->repo_path);
4178 if (error)
4179 goto done;
4180 free(path);
4181 path = in_repo_path;
4184 if (gw_trans->commit_id == NULL) {
4185 struct got_reference *head_ref;
4186 error = got_ref_open(&head_ref, gw_trans->repo,
4187 gw_trans->headref, 0);
4188 if (error)
4189 goto done;
4190 error = got_ref_resolve(&commit_id, gw_trans->repo, head_ref);
4191 if (error)
4192 goto done;
4193 got_ref_close(head_ref);
4195 * gw_trans->commit_id was not parsed from the querystring
4196 * we hit this code path from gw_index, where we don't know the
4197 * commit values for the tree link yet, so set
4198 * gw_trans->commit_id here to continue further into the tree
4200 error = got_object_id_str(&gw_trans->commit_id, commit_id);
4201 if (error)
4202 goto done;
4204 } else {
4205 error = got_repo_match_object_id(&commit_id, NULL,
4206 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT, &header->refs,
4207 gw_trans->repo);
4208 if (error)
4209 goto done;
4212 error = got_object_id_by_path(&tree_id, gw_trans->repo, commit_id,
4213 path);
4214 if (error)
4215 goto done;
4217 error = got_object_open_as_tree(&tree, gw_trans->repo, tree_id);
4218 if (error)
4219 goto done;
4221 nentries = got_object_tree_get_nentries(tree);
4222 for (i = 0; i < nentries; i++) {
4223 struct got_tree_entry *te;
4224 const char *modestr = "";
4225 mode_t mode;
4227 te = got_object_tree_get_entry(tree, i);
4229 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
4230 if (error)
4231 goto done;
4233 mode = got_tree_entry_get_mode(te);
4234 if (got_object_tree_entry_is_submodule(te))
4235 modestr = "$";
4236 else if (S_ISLNK(mode))
4237 modestr = "@";
4238 else if (S_ISDIR(mode))
4239 modestr = "/";
4240 else if (mode & S_IXUSR)
4241 modestr = "*";
4243 if (class_flip == 0) {
4244 class = "back_lightgray";
4245 class_flip = 1;
4246 } else {
4247 class = "back_white";
4248 class_flip = 0;
4251 if (S_ISDIR(mode)) {
4252 if (asprintf(&build_folder, "%s/%s",
4253 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4254 got_tree_entry_get_name(te)) == -1) {
4255 error = got_error_from_errno("asprintf");
4256 goto done;
4259 href_blob = khttp_urlpart(NULL, NULL, "gotweb", "path",
4260 gw_trans->repo_name, "action",
4261 gw_get_action_name(gw_trans), "commit",
4262 gw_trans->commit_id, "folder", build_folder, NULL);
4263 if (href_blob == NULL) {
4264 error = got_error_from_errno("khttp_urlpart");
4265 goto done;
4267 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4268 KATTR_ID, "tree_wrapper", KATTR__MAX);
4269 if (kerr != KCGI_OK)
4270 goto done;
4271 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4272 KATTR_ID, "tree_line", KATTR_CLASS, class,
4273 KATTR__MAX);
4274 if (kerr != KCGI_OK)
4275 goto done;
4276 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4277 KATTR_HREF, href_blob, KATTR_CLASS,
4278 "diff_directory", KATTR__MAX);
4279 if (kerr != KCGI_OK)
4280 goto done;
4281 kerr = khtml_printf(gw_trans->gw_html_req, "%s%s",
4282 got_tree_entry_get_name(te), modestr);
4283 if (kerr != KCGI_OK)
4284 goto done;
4285 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4286 if (kerr != KCGI_OK)
4287 goto done;
4288 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4289 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
4290 KATTR__MAX);
4291 if (kerr != KCGI_OK)
4292 goto done;
4293 kerr = khtml_entity(gw_trans->gw_html_req,
4294 KENTITY_nbsp);
4295 if (kerr != KCGI_OK)
4296 goto done;
4297 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4298 if (kerr != KCGI_OK)
4299 goto done;
4300 } else {
4301 href_blob = khttp_urlpart(NULL, NULL, "gotweb", "path",
4302 gw_trans->repo_name, "action", "blob", "commit",
4303 gw_trans->commit_id, "file",
4304 got_tree_entry_get_name(te), "folder",
4305 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4306 NULL);
4307 if (href_blob == NULL) {
4308 error = got_error_from_errno("khttp_urlpart");
4309 goto done;
4311 href_blame = khttp_urlpart(NULL, NULL, "gotweb", "path",
4312 gw_trans->repo_name, "action", "blame", "commit",
4313 gw_trans->commit_id, "file",
4314 got_tree_entry_get_name(te), "folder",
4315 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4316 NULL);
4317 if (href_blame == NULL) {
4318 error = got_error_from_errno("khttp_urlpart");
4319 goto done;
4321 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4322 KATTR_ID, "tree_wrapper", KATTR__MAX);
4323 if (kerr != KCGI_OK)
4324 goto done;
4325 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4326 KATTR_ID, "tree_line", KATTR_CLASS, class,
4327 KATTR__MAX);
4328 if (kerr != KCGI_OK)
4329 goto done;
4330 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4331 KATTR_HREF, href_blob, KATTR__MAX);
4332 if (kerr != KCGI_OK)
4333 goto done;
4334 kerr = khtml_printf(gw_trans->gw_html_req, "%s%s",
4335 got_tree_entry_get_name(te), modestr);
4336 if (kerr != KCGI_OK)
4337 goto done;
4338 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4339 if (kerr != KCGI_OK)
4340 goto done;
4341 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4342 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
4343 KATTR__MAX);
4344 if (kerr != KCGI_OK)
4345 goto done;
4347 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4348 KATTR_HREF, href_blob, KATTR__MAX);
4349 if (kerr != KCGI_OK)
4350 goto done;
4351 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
4352 if (kerr != KCGI_OK)
4353 goto done;
4354 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4355 if (kerr != KCGI_OK)
4356 goto done;
4358 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4359 if (kerr != KCGI_OK)
4360 goto done;
4362 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4363 KATTR_HREF, href_blame, KATTR__MAX);
4364 if (kerr != KCGI_OK)
4365 goto done;
4366 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
4367 if (kerr != KCGI_OK)
4368 goto done;
4370 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4371 if (kerr != KCGI_OK)
4372 goto done;
4374 free(id_str);
4375 id_str = NULL;
4376 free(href_blob);
4377 href_blob = NULL;
4378 free(build_folder);
4379 build_folder = NULL;
4381 done:
4382 if (tree)
4383 got_object_tree_close(tree);
4384 free(id_str);
4385 free(href_blob);
4386 free(href_blame);
4387 free(in_repo_path);
4388 free(tree_id);
4389 free(build_folder);
4390 if (error == NULL && kerr != KCGI_OK)
4391 error = gw_kcgi_error(kerr);
4392 return error;
4395 static const struct got_error *
4396 gw_output_repo_heads(struct gw_trans *gw_trans)
4398 const struct got_error *error = NULL;
4399 struct got_reflist_head refs;
4400 struct got_reflist_entry *re;
4401 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
4402 char *href_commits = NULL;
4403 enum kcgi_err kerr = KCGI_OK;
4405 TAILQ_INIT(&refs);
4407 error = got_ref_list(&refs, gw_trans->repo, "refs/heads",
4408 got_ref_cmp_by_name, NULL);
4409 if (error)
4410 goto done;
4412 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4413 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
4414 if (kerr != KCGI_OK)
4415 goto done;
4416 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4417 KATTR_ID, "summary_heads_title", KATTR__MAX);
4418 if (kerr != KCGI_OK)
4419 goto done;
4420 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
4421 if (kerr != KCGI_OK)
4422 goto done;
4423 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4424 if (kerr != KCGI_OK)
4425 goto done;
4426 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4427 KATTR_ID, "summary_heads_content", KATTR__MAX);
4428 if (kerr != KCGI_OK)
4429 goto done;
4431 TAILQ_FOREACH(re, &refs, entry) {
4432 const char *refname;
4434 if (got_ref_is_symbolic(re->ref))
4435 continue;
4437 refname = got_ref_get_name(re->ref);
4438 if (strncmp(refname, "refs/heads/", 11) != 0)
4439 continue;
4441 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
4442 refname, TM_DIFF);
4443 if (error)
4444 goto done;
4446 if (strncmp(refname, "refs/heads/", 11) == 0)
4447 refname += 11;
4449 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4450 KATTR_ID, "heads_wrapper", KATTR__MAX);
4451 if (kerr != KCGI_OK)
4452 goto done;
4453 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4454 KATTR_ID, "heads_age", KATTR__MAX);
4455 if (kerr != KCGI_OK)
4456 goto done;
4457 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
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;
4463 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4464 KATTR_ID, "heads_space", KATTR__MAX);
4465 if (kerr != KCGI_OK)
4466 goto done;
4467 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
4468 if (kerr != KCGI_OK)
4469 goto done;
4470 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4471 if (kerr != KCGI_OK)
4472 goto done;
4473 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4474 KATTR_ID, "head", KATTR__MAX);
4475 if (kerr != KCGI_OK)
4476 goto done;
4478 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
4479 gw_trans->repo_name, "action", "summary", "headref",
4480 refname, NULL);
4481 if (href_summary == NULL) {
4482 error = got_error_from_errno("khttp_urlpart");
4483 goto done;
4485 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4486 href_summary, KATTR__MAX);
4487 kerr = khtml_puts(gw_trans->gw_html_req, refname);
4488 if (kerr != KCGI_OK)
4489 goto done;
4490 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4491 if (kerr != KCGI_OK)
4492 goto done;
4494 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4495 "navs_wrapper", KATTR__MAX);
4496 if (kerr != KCGI_OK)
4497 goto done;
4498 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4499 "navs", KATTR__MAX);
4500 if (kerr != KCGI_OK)
4501 goto done;
4503 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4504 href_summary, KATTR__MAX);
4505 if (kerr != KCGI_OK)
4506 goto done;
4507 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4508 if (kerr != KCGI_OK)
4509 goto done;
4510 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4511 if (kerr != KCGI_OK)
4512 goto done;
4514 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4515 if (kerr != KCGI_OK)
4516 goto done;
4518 href_briefs = khttp_urlpart(NULL, NULL, "gotweb", "path",
4519 gw_trans->repo_name, "action", "briefs", "headref",
4520 refname, NULL);
4521 if (href_briefs == NULL) {
4522 error = got_error_from_errno("khttp_urlpart");
4523 goto done;
4525 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4526 href_briefs, KATTR__MAX);
4527 if (kerr != KCGI_OK)
4528 goto done;
4529 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4530 if (kerr != KCGI_OK)
4531 goto done;
4532 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4533 if (kerr != KCGI_OK)
4534 goto done;
4536 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4537 if (kerr != KCGI_OK)
4538 goto done;
4540 href_commits = khttp_urlpart(NULL, NULL, "gotweb", "path",
4541 gw_trans->repo_name, "action", "commits", "headref",
4542 refname, NULL);
4543 if (href_commits == NULL) {
4544 error = got_error_from_errno("khttp_urlpart");
4545 goto done;
4547 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4548 href_commits, KATTR__MAX);
4549 if (kerr != KCGI_OK)
4550 goto done;
4551 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4552 if (kerr != KCGI_OK)
4553 goto done;
4554 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4555 if (kerr != KCGI_OK)
4556 goto done;
4558 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4559 "dotted_line", KATTR__MAX);
4560 if (kerr != KCGI_OK)
4561 goto done;
4562 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4563 if (kerr != KCGI_OK)
4564 goto done;
4565 free(href_summary);
4566 href_summary = NULL;
4567 free(href_briefs);
4568 href_briefs = NULL;
4569 free(href_commits);
4570 href_commits = NULL;
4572 done:
4573 got_ref_list_free(&refs);
4574 free(href_summary);
4575 free(href_briefs);
4576 free(href_commits);
4577 return error;
4580 static const struct got_error *
4581 gw_output_site_link(struct gw_trans *gw_trans)
4583 const struct got_error *error = NULL;
4584 char *href_summary = NULL;
4585 enum kcgi_err kerr = KCGI_OK;
4587 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4588 "site_link", KATTR__MAX);
4589 if (kerr != KCGI_OK)
4590 goto done;
4591 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4592 KATTR__MAX);
4593 if (kerr != KCGI_OK)
4594 goto done;
4595 kerr = khtml_puts(gw_trans->gw_html_req,
4596 gw_trans->gw_conf->got_site_link);
4597 if (kerr != KCGI_OK)
4598 goto done;
4599 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4600 if (kerr != KCGI_OK)
4601 goto done;
4603 if (gw_trans->repo_name != NULL) {
4604 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4605 if (kerr != KCGI_OK)
4606 goto done;
4608 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
4609 gw_trans->repo_name, "action", "summary", NULL);
4610 if (href_summary == NULL) {
4611 error = got_error_from_errno("khttp_urlpart");
4612 goto done;
4614 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4615 href_summary, KATTR__MAX);
4616 if (kerr != KCGI_OK)
4617 goto done;
4618 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4619 if (kerr != KCGI_OK)
4620 goto done;
4621 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4622 if (kerr != KCGI_OK)
4623 goto done;
4624 kerr = khtml_printf(gw_trans->gw_html_req, " / %s",
4625 gw_get_action_name(gw_trans));
4626 if (kerr != KCGI_OK)
4627 goto done;
4630 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4631 if (kerr != KCGI_OK)
4632 goto done;
4633 done:
4634 free(href_summary);
4635 if (error == NULL && kerr != KCGI_OK)
4636 error = gw_kcgi_error(kerr);
4637 return error;
4640 static const struct got_error *
4641 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4643 const struct got_error *error = NULL;
4644 char *color = NULL;
4645 enum kcgi_err kerr = KCGI_OK;
4647 if (strncmp(buf, "-", 1) == 0)
4648 color = "diff_minus";
4649 else if (strncmp(buf, "+", 1) == 0)
4650 color = "diff_plus";
4651 else if (strncmp(buf, "@@", 2) == 0)
4652 color = "diff_chunk_header";
4653 else if (strncmp(buf, "@@", 2) == 0)
4654 color = "diff_chunk_header";
4655 else if (strncmp(buf, "commit +", 8) == 0)
4656 color = "diff_meta";
4657 else if (strncmp(buf, "commit -", 8) == 0)
4658 color = "diff_meta";
4659 else if (strncmp(buf, "blob +", 6) == 0)
4660 color = "diff_meta";
4661 else if (strncmp(buf, "blob -", 6) == 0)
4662 color = "diff_meta";
4663 else if (strncmp(buf, "file +", 6) == 0)
4664 color = "diff_meta";
4665 else if (strncmp(buf, "file -", 6) == 0)
4666 color = "diff_meta";
4667 else if (strncmp(buf, "from:", 5) == 0)
4668 color = "diff_author";
4669 else if (strncmp(buf, "via:", 4) == 0)
4670 color = "diff_author";
4671 else if (strncmp(buf, "date:", 5) == 0)
4672 color = "diff_date";
4673 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4674 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4675 if (error == NULL && kerr != KCGI_OK)
4676 error = gw_kcgi_error(kerr);
4677 return error;
4680 int
4681 main(int argc, char *argv[])
4683 const struct got_error *error = NULL;
4684 struct gw_trans *gw_trans;
4685 struct gw_dir *dir = NULL, *tdir;
4686 const char *page = "index";
4687 enum kcgi_err kerr = KCGI_OK;
4689 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4690 errx(1, "malloc");
4692 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4693 errx(1, "malloc");
4695 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4696 errx(1, "malloc");
4698 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4699 errx(1, "malloc");
4701 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4702 if (kerr != KCGI_OK) {
4703 error = gw_kcgi_error(kerr);
4704 goto done;
4707 TAILQ_INIT(&gw_trans->gw_dirs);
4708 TAILQ_INIT(&gw_trans->gw_headers);
4710 gw_trans->action = -1;
4711 gw_trans->page = 0;
4712 gw_trans->repos_total = 0;
4713 gw_trans->repo_path = NULL;
4714 gw_trans->commit_id = NULL;
4715 gw_trans->next_id = NULL;
4716 gw_trans->next_prev_id = NULL;
4717 gw_trans->prev_id = NULL;
4718 gw_trans->prev_prev_id = NULL;
4719 gw_trans->headref = GOT_REF_HEAD;
4720 gw_trans->mime = KMIME_TEXT_HTML;
4721 gw_trans->gw_tmpl->key = gw_templs;
4722 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4723 gw_trans->gw_tmpl->arg = gw_trans;
4724 gw_trans->gw_tmpl->cb = gw_template;
4726 error = parse_gotweb_config(&gw_trans->gw_conf, GOTWEB_CONF);
4727 if (error)
4728 goto done;
4730 error = gw_parse_querystring(gw_trans);
4731 if (error)
4732 goto done;
4734 if (gw_trans->action == GW_BLOB)
4735 error = gw_blob(gw_trans);
4736 else
4737 error = gw_display_index(gw_trans);
4738 done:
4739 free(gw_trans->gw_conf->got_repos_path);
4740 free(gw_trans->gw_conf->got_www_path);
4741 free(gw_trans->gw_conf->got_site_name);
4742 free(gw_trans->gw_conf->got_site_owner);
4743 free(gw_trans->gw_conf->got_site_link);
4744 free(gw_trans->gw_conf->got_logo);
4745 free(gw_trans->gw_conf->got_logo_url);
4746 free(gw_trans->gw_conf);
4747 free(gw_trans->commit_id);
4748 free(gw_trans->next_id);
4749 free(gw_trans->next_prev_id);
4750 free(gw_trans->prev_id);
4751 free(gw_trans->prev_prev_id);
4752 free(gw_trans->repo_path);
4753 if (gw_trans->repo)
4754 got_repo_close(gw_trans->repo);
4756 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4757 free(dir->name);
4758 free(dir->description);
4759 free(dir->age);
4760 free(dir->url);
4761 free(dir->path);
4762 free(dir);
4765 khttp_free(gw_trans->gw_req);
4766 return 0;