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 const struct got_error *close_err = got_repo_close(repo);
2838 if (error == NULL)
2839 error = close_err;
2841 return error;
2844 static const struct got_error *
2845 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2847 const struct got_error *error;
2848 FILE *f = NULL;
2849 struct got_object_id *id1 = NULL, *id2 = NULL;
2850 char *label1 = NULL, *label2 = NULL, *line = NULL;
2851 int obj_type;
2852 size_t linesize = 0;
2853 ssize_t linelen;
2854 enum kcgi_err kerr = KCGI_OK;
2856 f = got_opentemp();
2857 if (f == NULL)
2858 return NULL;
2860 if (header->parent_id != NULL &&
2861 strncmp(header->parent_id, "/dev/null", 9) != 0) {
2862 error = got_repo_match_object_id(&id1, &label1,
2863 header->parent_id, GOT_OBJ_TYPE_ANY,
2864 &header->refs, gw_trans->repo);
2865 if (error)
2866 goto done;
2869 error = got_repo_match_object_id(&id2, &label2,
2870 header->commit_id, GOT_OBJ_TYPE_ANY, &header->refs,
2871 gw_trans->repo);
2872 if (error)
2873 goto done;
2875 error = got_object_get_type(&obj_type, gw_trans->repo, id2);
2876 if (error)
2877 goto done;
2878 switch (obj_type) {
2879 case GOT_OBJ_TYPE_BLOB:
2880 error = got_diff_objects_as_blobs(NULL, NULL, id1, id2,
2881 NULL, NULL, 3, 0, 0, gw_trans->repo, f);
2882 break;
2883 case GOT_OBJ_TYPE_TREE:
2884 error = got_diff_objects_as_trees(NULL, NULL, id1, id2,
2885 "", "", 3, 0, 0, gw_trans->repo, f);
2886 break;
2887 case GOT_OBJ_TYPE_COMMIT:
2888 error = got_diff_objects_as_commits(NULL, NULL, id1, id2,
2889 3, 0, 0, gw_trans->repo, f);
2890 break;
2891 default:
2892 error = got_error(GOT_ERR_OBJ_TYPE);
2894 if (error)
2895 goto done;
2897 if (fseek(f, 0, SEEK_SET) == -1) {
2898 error = got_ferror(f, GOT_ERR_IO);
2899 goto done;
2902 while ((linelen = getline(&line, &linesize, f)) != -1) {
2903 error = gw_colordiff_line(gw_trans, line);
2904 if (error)
2905 goto done;
2906 /* XXX: KHTML_PRETTY breaks this */
2907 kerr = khtml_puts(gw_trans->gw_html_req, line);
2908 if (kerr != KCGI_OK)
2909 goto done;
2910 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2911 if (kerr != KCGI_OK)
2912 goto done;
2914 if (linelen == -1 && ferror(f))
2915 error = got_error_from_errno("getline");
2916 done:
2917 if (f && fclose(f) == EOF && error == NULL)
2918 error = got_error_from_errno("fclose");
2919 free(line);
2920 free(label1);
2921 free(label2);
2922 free(id1);
2923 free(id2);
2925 if (error == NULL && kerr != KCGI_OK)
2926 error = gw_kcgi_error(kerr);
2927 return error;
2930 static const struct got_error *
2931 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2933 const struct got_error *error = NULL, *close_err;
2934 struct got_repository *repo;
2935 const char *gitconfig_owner;
2937 *owner = NULL;
2939 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2940 return NULL;
2942 error = got_repo_open(&repo, dir, NULL);
2943 if (error)
2944 return error;
2945 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2946 if (gitconfig_owner) {
2947 *owner = strdup(gitconfig_owner);
2948 if (*owner == NULL)
2949 error = got_error_from_errno("strdup");
2951 close_err = got_repo_close(repo);
2952 if (error == NULL)
2953 error = close_err;
2954 return error;
2957 static const struct got_error *
2958 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2960 const struct got_error *error = NULL;
2961 FILE *f;
2962 char *d_file = NULL;
2963 unsigned int len;
2964 size_t n;
2966 *url = NULL;
2968 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2969 return got_error_from_errno("asprintf");
2971 f = fopen(d_file, "r");
2972 if (f == NULL) {
2973 if (errno != ENOENT && errno != EACCES)
2974 error = got_error_from_errno2("fopen", d_file);
2975 goto done;
2978 if (fseek(f, 0, SEEK_END) == -1) {
2979 error = got_ferror(f, GOT_ERR_IO);
2980 goto done;
2982 len = ftell(f);
2983 if (len == -1) {
2984 error = got_ferror(f, GOT_ERR_IO);
2985 goto done;
2987 if (fseek(f, 0, SEEK_SET) == -1) {
2988 error = got_ferror(f, GOT_ERR_IO);
2989 goto done;
2992 *url = calloc(len + 1, sizeof(**url));
2993 if (*url == NULL) {
2994 error = got_error_from_errno("calloc");
2995 goto done;
2998 n = fread(*url, 1, len, f);
2999 if (n == 0 && ferror(f))
3000 error = got_ferror(f, GOT_ERR_IO);
3001 done:
3002 if (f && fclose(f) == EOF && error == NULL)
3003 error = got_error_from_errno("fclose");
3004 free(d_file);
3005 return NULL;
3008 static const struct got_error *
3009 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
3010 int limit, int tag_type)
3012 const struct got_error *error = NULL;
3013 struct got_reflist_head refs;
3014 struct got_reflist_entry *re;
3015 char *age = NULL;
3016 char *id_str = NULL, *newline, *href_commits = NULL;
3017 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
3018 struct got_tag_object *tag = NULL;
3019 enum kcgi_err kerr = KCGI_OK;
3020 int summary_header_displayed = 0, start_tag = 0, chk_next = 0;
3021 int prev_set = 0, tag_count = 0;
3023 TAILQ_INIT(&refs);
3025 error = got_ref_list(&refs, gw_trans->repo, "refs/tags",
3026 got_ref_cmp_tags, gw_trans->repo);
3027 if (error)
3028 goto done;
3030 TAILQ_FOREACH(re, &refs, entry) {
3031 const char *refname;
3032 const char *tagger;
3033 const char *tag_commit;
3034 time_t tagger_time;
3035 struct got_object_id *id;
3036 struct got_commit_object *commit = NULL;
3038 refname = got_ref_get_name(re->ref);
3039 if (strncmp(refname, "refs/tags/", 10) != 0)
3040 continue;
3041 refname += 10;
3043 error = got_ref_resolve(&id, gw_trans->repo, re->ref);
3044 if (error)
3045 goto done;
3047 error = got_object_open_as_tag(&tag, gw_trans->repo, id);
3048 if (error) {
3049 if (error->code != GOT_ERR_OBJ_TYPE) {
3050 free(id);
3051 goto done;
3053 /* "lightweight" tag */
3054 error = got_object_open_as_commit(&commit,
3055 gw_trans->repo, id);
3056 if (error) {
3057 free(id);
3058 goto done;
3060 tagger = got_object_commit_get_committer(commit);
3061 tagger_time =
3062 got_object_commit_get_committer_time(commit);
3063 error = got_object_id_str(&id_str, id);
3064 free(id);
3065 } else {
3066 free(id);
3067 tagger = got_object_tag_get_tagger(tag);
3068 tagger_time = got_object_tag_get_tagger_time(tag);
3069 error = got_object_id_str(&id_str,
3070 got_object_tag_get_object_id(tag));
3072 if (error)
3073 goto done;
3075 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
3076 strlen(id_str)) != 0)
3077 continue;
3079 if (tag_type == TAGBRIEF && gw_trans->commit_id &&
3080 start_tag == 0 && strncmp(id_str, header->commit_id,
3081 strlen(id_str)) != 0) {
3082 continue;
3083 } else {
3084 start_tag = 1;
3087 tag_count++;
3089 if (prev_set == 0 && start_tag == 1) {
3090 gw_trans->next_prev_id = strdup(id_str);
3091 if (gw_trans->next_prev_id == NULL) {
3092 error = got_error_from_errno("strdup");
3093 goto done;
3095 prev_set = 1;
3098 if (chk_next) {
3099 gw_trans->next_id = strdup(id_str);
3100 if (gw_trans->next_id == NULL)
3101 error = got_error_from_errno("strdup");
3102 goto done;
3105 if (commit) {
3106 error = got_object_commit_get_logmsg(&tag_commit0,
3107 commit);
3108 if (error)
3109 goto done;
3110 got_object_commit_close(commit);
3111 } else {
3112 tag_commit0 = strdup(got_object_tag_get_message(tag));
3113 if (tag_commit0 == NULL) {
3114 error = got_error_from_errno("strdup");
3115 goto done;
3119 tag_commit = tag_commit0;
3120 while (*tag_commit == '\n')
3121 tag_commit++;
3123 switch (tag_type) {
3124 case TAGBRIEF:
3125 newline = strchr(tag_commit, '\n');
3126 if (newline)
3127 *newline = '\0';
3129 if (summary_header_displayed == 0) {
3130 kerr = khtml_attr(gw_trans->gw_html_req,
3131 KELEM_DIV, KATTR_ID,
3132 "summary_tags_title_wrapper", KATTR__MAX);
3133 if (kerr != KCGI_OK)
3134 goto done;
3135 kerr = khtml_attr(gw_trans->gw_html_req,
3136 KELEM_DIV, KATTR_ID,
3137 "summary_tags_title", KATTR__MAX);
3138 if (kerr != KCGI_OK)
3139 goto done;
3140 kerr = khtml_puts(gw_trans->gw_html_req,
3141 "Tags");
3142 if (kerr != KCGI_OK)
3143 goto done;
3144 kerr = khtml_closeelem(gw_trans->gw_html_req,
3145 2);
3146 if (kerr != KCGI_OK)
3147 goto done;
3148 kerr = khtml_attr(gw_trans->gw_html_req,
3149 KELEM_DIV, KATTR_ID,
3150 "summary_tags_content", KATTR__MAX);
3151 if (kerr != KCGI_OK)
3152 goto done;
3153 summary_header_displayed = 1;
3156 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3157 KATTR_ID, "tag_wrapper", KATTR__MAX);
3158 if (kerr != KCGI_OK)
3159 goto done;
3160 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3161 KATTR_ID, "tag_age", KATTR__MAX);
3162 if (kerr != KCGI_OK)
3163 goto done;
3164 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
3165 if (error)
3166 goto done;
3167 kerr = khtml_puts(gw_trans->gw_html_req,
3168 age ? age : "");
3169 if (kerr != KCGI_OK)
3170 goto done;
3171 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3172 if (kerr != KCGI_OK)
3173 goto done;
3174 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3175 KATTR_ID, "tag", KATTR__MAX);
3176 if (kerr != KCGI_OK)
3177 goto done;
3178 kerr = khtml_puts(gw_trans->gw_html_req, refname);
3179 if (kerr != KCGI_OK)
3180 goto done;
3181 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3182 if (kerr != KCGI_OK)
3183 goto done;
3184 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3185 KATTR_ID, "tag_name", KATTR__MAX);
3186 if (kerr != KCGI_OK)
3187 goto done;
3189 href_tag = khttp_urlpart(NULL, NULL, "gotweb", "path",
3190 gw_trans->repo_name, "action", "tag", "commit",
3191 id_str, NULL);
3192 if (href_tag == NULL) {
3193 error = got_error_from_errno("khttp_urlpart");
3194 goto done;
3196 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3197 KATTR_HREF, href_tag, KATTR__MAX);
3198 if (kerr != KCGI_OK)
3199 goto done;
3200 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
3201 if (kerr != KCGI_OK)
3202 goto done;
3203 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3204 if (kerr != KCGI_OK)
3205 goto done;
3207 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3208 KATTR_ID, "navs_wrapper", KATTR__MAX);
3209 if (kerr != KCGI_OK)
3210 goto done;
3211 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3212 KATTR_ID, "navs", KATTR__MAX);
3213 if (kerr != KCGI_OK)
3214 goto done;
3216 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3217 KATTR_HREF, href_tag, KATTR__MAX);
3218 if (kerr != KCGI_OK)
3219 goto done;
3220 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
3221 if (kerr != KCGI_OK)
3222 goto done;
3223 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3224 if (kerr != KCGI_OK)
3225 goto done;
3227 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3228 if (kerr != KCGI_OK)
3229 goto done;
3231 href_briefs = khttp_urlpart(NULL, NULL, "gotweb",
3232 "path", gw_trans->repo_name, "action", "briefs",
3233 "commit", id_str, NULL);
3234 if (href_briefs == NULL) {
3235 error = got_error_from_errno("khttp_urlpart");
3236 goto done;
3238 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3239 KATTR_HREF, href_briefs, KATTR__MAX);
3240 if (kerr != KCGI_OK)
3241 goto done;
3242 kerr = khtml_puts(gw_trans->gw_html_req,
3243 "commit briefs");
3244 if (kerr != KCGI_OK)
3245 goto done;
3246 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3247 if (kerr != KCGI_OK)
3248 goto done;
3250 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3251 if (kerr != KCGI_OK)
3252 goto done;
3254 href_commits = khttp_urlpart(NULL, NULL, "gotweb",
3255 "path", gw_trans->repo_name, "action", "commits",
3256 "commit", id_str, NULL);
3257 if (href_commits == NULL) {
3258 error = got_error_from_errno("khttp_urlpart");
3259 goto done;
3261 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3262 KATTR_HREF, href_commits, KATTR__MAX);
3263 if (kerr != KCGI_OK)
3264 goto done;
3265 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
3266 if (kerr != KCGI_OK)
3267 goto done;
3268 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3269 if (kerr != KCGI_OK)
3270 goto done;
3272 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3273 KATTR_ID, "dotted_line", KATTR__MAX);
3274 if (kerr != KCGI_OK)
3275 goto done;
3276 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3277 if (kerr != KCGI_OK)
3278 goto done;
3279 break;
3280 case TAGFULL:
3281 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3282 KATTR_ID, "tag_info_date_title", KATTR__MAX);
3283 if (kerr != KCGI_OK)
3284 goto done;
3285 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
3286 if (kerr != KCGI_OK)
3287 goto done;
3288 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3289 if (kerr != KCGI_OK)
3290 goto done;
3291 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3292 KATTR_ID, "tag_info_date", KATTR__MAX);
3293 if (kerr != KCGI_OK)
3294 goto done;
3295 error = gw_get_time_str(&age, tagger_time, TM_LONG);
3296 if (error)
3297 goto done;
3298 kerr = khtml_puts(gw_trans->gw_html_req,
3299 age ? age : "");
3300 if (kerr != KCGI_OK)
3301 goto done;
3302 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3303 if (kerr != KCGI_OK)
3304 goto done;
3306 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3307 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
3308 if (kerr != KCGI_OK)
3309 goto done;
3310 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
3311 if (kerr != KCGI_OK)
3312 goto done;
3313 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3314 if (kerr != KCGI_OK)
3315 goto done;
3316 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3317 KATTR_ID, "tag_info_date", KATTR__MAX);
3318 if (kerr != KCGI_OK)
3319 goto done;
3320 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
3321 if (kerr != KCGI_OK)
3322 goto done;
3323 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3324 if (kerr != KCGI_OK)
3325 goto done;
3327 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3328 KATTR_ID, "tag_info", KATTR__MAX);
3329 if (kerr != KCGI_OK)
3330 goto done;
3331 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
3332 if (kerr != KCGI_OK)
3333 goto done;
3334 break;
3335 default:
3336 break;
3338 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3339 if (kerr != KCGI_OK)
3340 goto done;
3342 if (limit && --limit == 0)
3343 chk_next = 1;
3345 if (tag)
3346 got_object_tag_close(tag);
3347 tag = NULL;
3348 free(id_str);
3349 id_str = NULL;
3350 free(age);
3351 age = NULL;
3352 free(tag_commit0);
3353 tag_commit0 = NULL;
3354 free(href_tag);
3355 href_tag = NULL;
3356 free(href_briefs);
3357 href_briefs = NULL;
3358 free(href_commits);
3359 href_commits = NULL;
3361 if (tag_count == 0) {
3362 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3363 "summary_tags_title_wrapper", KATTR__MAX);
3364 if (kerr != KCGI_OK)
3365 goto done;
3366 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3367 "summary_tags_title", KATTR__MAX);
3368 if (kerr != KCGI_OK)
3369 goto done;
3370 kerr = khtml_puts(gw_trans->gw_html_req, "Tags");
3371 if (kerr != KCGI_OK)
3372 goto done;
3373 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3374 if (kerr != KCGI_OK)
3375 goto done;
3376 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3377 "summary_tags_content", KATTR__MAX);
3378 if (kerr != KCGI_OK)
3379 goto done;
3380 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3381 "tags_info", KATTR__MAX);
3382 if (kerr != KCGI_OK)
3383 goto done;
3384 kerr = khttp_puts(gw_trans->gw_req,
3385 "There are no tags for this repo.");
3386 if (kerr != KCGI_OK)
3387 goto done;
3388 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3390 done:
3391 if (tag)
3392 got_object_tag_close(tag);
3393 free(id_str);
3394 free(age);
3395 free(tag_commit0);
3396 free(href_tag);
3397 free(href_briefs);
3398 free(href_commits);
3399 got_ref_list_free(&refs);
3400 if (error == NULL && kerr != KCGI_OK)
3401 error = gw_kcgi_error(kerr);
3402 return error;
3405 static void
3406 gw_free_header(struct gw_header *header)
3408 free(header->path);
3409 free(header->author);
3410 free(header->committer);
3411 free(header->refs_str);
3412 free(header->commit_id);
3413 free(header->parent_id);
3414 free(header->tree_id);
3415 free(header->commit_msg);
3418 static struct gw_header *
3419 gw_init_header()
3421 struct gw_header *header;
3423 header = malloc(sizeof(*header));
3424 if (header == NULL)
3425 return NULL;
3427 header->path = NULL;
3428 TAILQ_INIT(&header->refs);
3430 header->refs_str = NULL;
3431 header->commit_id = NULL;
3432 header->committer = NULL;
3433 header->author = NULL;
3434 header->parent_id = NULL;
3435 header->tree_id = NULL;
3436 header->commit_msg = NULL;
3438 return header;
3441 static const struct got_error *
3442 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
3443 int limit, struct got_object_id *id)
3445 const struct got_error *error = NULL;
3446 struct got_commit_graph *graph = NULL;
3447 struct got_commit_object *commit = NULL;
3448 int chk_next = 0, chk_multi = 0, prev_set = 0;
3450 error = got_commit_graph_open(&graph, header->path, 0);
3451 if (error)
3452 return error;
3454 error = got_commit_graph_iter_start(graph, id, gw_trans->repo, NULL,
3455 NULL);
3456 if (error)
3457 goto done;
3459 for (;;) {
3460 error = got_commit_graph_iter_next(&id, graph, gw_trans->repo,
3461 NULL, NULL);
3462 if (error) {
3463 if (error->code == GOT_ERR_ITER_COMPLETED)
3464 error = NULL;
3465 goto done;
3467 if (id == NULL)
3468 goto done;
3470 error = got_object_open_as_commit(&commit, gw_trans->repo, id);
3471 if (error)
3472 goto done;
3473 if (limit == 1 && chk_multi == 0 &&
3474 gw_trans->gw_conf->got_max_commits_display != 1) {
3475 error = gw_get_commit(gw_trans, header, commit, id);
3476 if (error)
3477 goto done;
3478 } else {
3479 chk_multi = 1;
3480 struct gw_header *n_header = NULL;
3481 if ((n_header = gw_init_header()) == NULL) {
3482 error = got_error_from_errno("malloc");
3483 goto done;
3485 error = got_ref_list(&n_header->refs, gw_trans->repo,
3486 NULL, got_ref_cmp_by_name, NULL);
3487 if (error)
3488 goto done;
3490 error = gw_get_commit(gw_trans, n_header, commit, id);
3491 if (error)
3492 goto done;
3493 got_ref_list_free(&n_header->refs);
3496 * we have a commit_id now, so copy it to next_prev_id
3497 * for navigation through gw_briefs and gw_commits
3499 if (gw_trans->next_prev_id == NULL && prev_set == 0 &&
3500 (gw_trans->action == GW_BRIEFS ||
3501 gw_trans->action == GW_COMMITS ||
3502 gw_trans->action == GW_SUMMARY)) {
3503 prev_set = 1;
3504 gw_trans->next_prev_id =
3505 strdup(n_header->commit_id);
3506 if (gw_trans->next_prev_id == NULL) {
3507 error = got_error_from_errno("strdup");
3508 goto done;
3513 * check for one more commit before breaking,
3514 * so we know whether to navicate through gw_briefs
3515 * gw_commits and gw_summary
3517 if (chk_next && (gw_trans->action == GW_BRIEFS||
3518 gw_trans->action == GW_COMMITS ||
3519 gw_trans->action == GW_SUMMARY)) {
3520 gw_trans->next_id = strdup(n_header->commit_id);
3521 if (gw_trans->next_id == NULL)
3522 error = got_error_from_errno("strdup");
3523 goto done;
3526 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3527 entry);
3529 if (error || (limit && --limit == 0)) {
3530 if (chk_multi == 0)
3531 break;
3532 chk_next = 1;
3535 done:
3536 if (commit != NULL)
3537 got_object_commit_close(commit);
3538 if (graph)
3539 got_commit_graph_close(graph);
3540 return error;
3543 static const struct got_error *
3544 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header,
3545 struct got_commit_object *commit, struct got_object_id *id)
3547 const struct got_error *error = NULL;
3548 struct got_reflist_entry *re;
3549 struct got_object_id *id2 = NULL;
3550 struct got_object_qid *parent_id;
3551 char *commit_msg = NULL, *commit_msg0;
3553 /*print commit*/
3554 TAILQ_FOREACH(re, &header->refs, entry) {
3555 char *s;
3556 const char *name;
3557 struct got_tag_object *tag = NULL;
3558 struct got_object_id *ref_id;
3559 int cmp;
3561 if (got_ref_is_symbolic(re->ref))
3562 continue;
3564 name = got_ref_get_name(re->ref);
3565 if (strncmp(name, "refs/", 5) == 0)
3566 name += 5;
3567 if (strncmp(name, "got/", 4) == 0)
3568 continue;
3569 if (strncmp(name, "heads/", 6) == 0)
3570 name += 6;
3571 if (strncmp(name, "remotes/", 8) == 0) {
3572 name += 8;
3573 s = strstr(name, "/" GOT_REF_HEAD);
3574 if (s != NULL && s[strlen(s)] == '\0')
3575 continue;
3577 error = got_ref_resolve(&ref_id, gw_trans->repo, re->ref);
3578 if (error)
3579 return error;
3580 if (strncmp(name, "tags/", 5) == 0) {
3581 error = got_object_open_as_tag(&tag, gw_trans->repo,
3582 ref_id);
3583 if (error) {
3584 if (error->code != GOT_ERR_OBJ_TYPE) {
3585 free(ref_id);
3586 continue;
3589 * Ref points at something other
3590 * than a tag.
3592 error = NULL;
3593 tag = NULL;
3596 cmp = got_object_id_cmp(tag ?
3597 got_object_tag_get_object_id(tag) : ref_id, id);
3598 free(ref_id);
3599 if (tag)
3600 got_object_tag_close(tag);
3601 if (cmp != 0)
3602 continue;
3603 s = header->refs_str;
3604 if (asprintf(&header->refs_str, "%s%s%s", s ? s : "",
3605 s ? ", " : "", name) == -1) {
3606 error = got_error_from_errno("asprintf");
3607 free(s);
3608 header->refs_str = NULL;
3609 return error;
3611 free(s);
3614 error = got_object_id_str(&header->commit_id, id);
3615 if (error)
3616 return error;
3618 error = got_object_id_str(&header->tree_id,
3619 got_object_commit_get_tree_id(commit));
3620 if (error)
3621 return error;
3623 if (gw_trans->action == GW_DIFF) {
3624 parent_id = SIMPLEQ_FIRST(
3625 got_object_commit_get_parent_ids(commit));
3626 if (parent_id != NULL) {
3627 id2 = got_object_id_dup(parent_id->id);
3628 free (parent_id);
3629 error = got_object_id_str(&header->parent_id, id2);
3630 if (error)
3631 return error;
3632 free(id2);
3633 } else {
3634 header->parent_id = strdup("/dev/null");
3635 if (header->parent_id == NULL) {
3636 error = got_error_from_errno("strdup");
3637 return error;
3642 header->committer_time =
3643 got_object_commit_get_committer_time(commit);
3645 header->author =
3646 strdup(got_object_commit_get_author(commit));
3647 if (header->author == NULL) {
3648 error = got_error_from_errno("strdup");
3649 return error;
3651 header->committer =
3652 strdup(got_object_commit_get_committer(commit));
3653 if (header->committer == NULL) {
3654 error = got_error_from_errno("strdup");
3655 return error;
3657 error = got_object_commit_get_logmsg(&commit_msg0, commit);
3658 if (error)
3659 return error;
3661 commit_msg = commit_msg0;
3662 while (*commit_msg == '\n')
3663 commit_msg++;
3665 header->commit_msg = strdup(commit_msg);
3666 if (header->commit_msg == NULL)
3667 error = got_error_from_errno("strdup");
3668 free(commit_msg0);
3669 return error;
3672 static const struct got_error *
3673 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3675 const struct got_error *error = NULL;
3676 char *in_repo_path = NULL;
3677 struct got_object_id *id = NULL;
3679 error = got_repo_open(&gw_trans->repo, gw_trans->repo_path, NULL);
3680 if (error)
3681 return error;
3683 if (gw_trans->commit_id == NULL) {
3684 struct got_reference *head_ref;
3685 error = got_ref_open(&head_ref, gw_trans->repo,
3686 gw_trans->headref, 0);
3687 if (error)
3688 return error;
3690 error = got_ref_resolve(&id, gw_trans->repo, head_ref);
3691 got_ref_close(head_ref);
3692 if (error)
3693 return error;
3694 } else {
3695 struct got_reference *ref;
3697 error = got_ref_open(&ref, gw_trans->repo,
3698 gw_trans->commit_id, 0);
3699 if (error == NULL) {
3700 int obj_type;
3701 error = got_ref_resolve(&id, gw_trans->repo, ref);
3702 got_ref_close(ref);
3703 if (error)
3704 return error;
3705 error = got_object_get_type(&obj_type, gw_trans->repo,
3706 id);
3707 if (error)
3708 goto done;
3709 if (obj_type == GOT_OBJ_TYPE_TAG) {
3710 struct got_tag_object *tag;
3711 error = got_object_open_as_tag(&tag,
3712 gw_trans->repo, id);
3713 if (error)
3714 goto done;
3715 if (got_object_tag_get_object_type(tag) !=
3716 GOT_OBJ_TYPE_COMMIT) {
3717 got_object_tag_close(tag);
3718 error = got_error(GOT_ERR_OBJ_TYPE);
3719 goto done;
3721 free(id);
3722 id = got_object_id_dup(
3723 got_object_tag_get_object_id(tag));
3724 if (id == NULL)
3725 error = got_error_from_errno(
3726 "got_object_id_dup");
3727 got_object_tag_close(tag);
3728 if (error)
3729 goto done;
3730 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3731 error = got_error(GOT_ERR_OBJ_TYPE);
3732 goto done;
3735 error = got_repo_match_object_id_prefix(&id,
3736 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT,
3737 gw_trans->repo);
3738 if (error)
3739 goto done;
3742 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
3743 gw_trans->repo_path);
3744 if (error)
3745 goto done;
3747 if (in_repo_path) {
3748 header->path = strdup(in_repo_path);
3749 if (header->path == NULL) {
3750 error = got_error_from_errno("strdup");
3751 goto done;
3755 error = got_ref_list(&header->refs, gw_trans->repo, NULL,
3756 got_ref_cmp_by_name, NULL);
3757 if (error)
3758 goto done;
3760 error = gw_get_commits(gw_trans, header, limit, id);
3761 done:
3762 got_ref_list_free(&header->refs);
3763 free(id);
3764 free(in_repo_path);
3765 return error;
3768 struct blame_line {
3769 int annotated;
3770 char *id_str;
3771 char *committer;
3772 char datebuf[11]; /* YYYY-MM-DD + NUL */
3775 struct gw_blame_cb_args {
3776 struct blame_line *lines;
3777 int nlines;
3778 int nlines_prec;
3779 int lineno_cur;
3780 off_t *line_offsets;
3781 FILE *f;
3782 struct got_repository *repo;
3783 struct gw_trans *gw_trans;
3786 static const struct got_error *
3787 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3789 const struct got_error *err = NULL;
3790 struct gw_blame_cb_args *a = arg;
3791 struct blame_line *bline;
3792 char *line = NULL;
3793 size_t linesize = 0;
3794 struct got_commit_object *commit = NULL;
3795 off_t offset;
3796 struct tm tm;
3797 time_t committer_time;
3798 enum kcgi_err kerr = KCGI_OK;
3800 if (nlines != a->nlines ||
3801 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3802 return got_error(GOT_ERR_RANGE);
3804 if (lineno == -1)
3805 return NULL; /* no change in this commit */
3807 /* Annotate this line. */
3808 bline = &a->lines[lineno - 1];
3809 if (bline->annotated)
3810 return NULL;
3811 err = got_object_id_str(&bline->id_str, id);
3812 if (err)
3813 return err;
3815 err = got_object_open_as_commit(&commit, a->repo, id);
3816 if (err)
3817 goto done;
3819 bline->committer = strdup(got_object_commit_get_committer(commit));
3820 if (bline->committer == NULL) {
3821 err = got_error_from_errno("strdup");
3822 goto done;
3825 committer_time = got_object_commit_get_committer_time(commit);
3826 if (localtime_r(&committer_time, &tm) == NULL)
3827 return got_error_from_errno("localtime_r");
3828 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3829 &tm) == 0) {
3830 err = got_error(GOT_ERR_NO_SPACE);
3831 goto done;
3833 bline->annotated = 1;
3835 /* Print lines annotated so far. */
3836 bline = &a->lines[a->lineno_cur - 1];
3837 if (!bline->annotated)
3838 goto done;
3840 offset = a->line_offsets[a->lineno_cur - 1];
3841 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3842 err = got_error_from_errno("fseeko");
3843 goto done;
3846 while (bline->annotated) {
3847 char *smallerthan, *at, *nl, *committer;
3848 char *href_diff = NULL;
3849 size_t len;
3851 if (getline(&line, &linesize, a->f) == -1) {
3852 if (ferror(a->f))
3853 err = got_error_from_errno("getline");
3854 break;
3857 committer = bline->committer;
3858 smallerthan = strchr(committer, '<');
3859 if (smallerthan && smallerthan[1] != '\0')
3860 committer = smallerthan + 1;
3861 at = strchr(committer, '@');
3862 if (at)
3863 *at = '\0';
3864 len = strlen(committer);
3865 if (len >= 9)
3866 committer[8] = '\0';
3868 nl = strchr(line, '\n');
3869 if (nl)
3870 *nl = '\0';
3872 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3873 "blame_wrapper", KATTR__MAX);
3874 if (kerr != KCGI_OK)
3875 goto err;
3876 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3877 "blame_number", KATTR__MAX);
3878 if (kerr != KCGI_OK)
3879 goto err;
3880 kerr = khtml_printf(a->gw_trans->gw_html_req, "%.*d",
3881 a->nlines_prec, a->lineno_cur);
3882 if (kerr != KCGI_OK)
3883 goto err;
3884 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3885 if (kerr != KCGI_OK)
3886 goto err;
3888 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3889 "blame_hash", KATTR__MAX);
3890 if (kerr != KCGI_OK)
3891 goto err;
3893 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
3894 a->gw_trans->repo_name, "action", "diff", "commit",
3895 bline->id_str, NULL);
3896 if (href_diff == NULL) {
3897 err = got_error_from_errno("khttp_urlpart");
3898 goto done;
3900 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3901 KATTR_HREF, href_diff, KATTR__MAX);
3902 if (kerr != KCGI_OK)
3903 goto done;
3904 kerr = khtml_printf(a->gw_trans->gw_html_req, "%.8s",
3905 bline->id_str);
3906 if (kerr != KCGI_OK)
3907 goto err;
3908 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3909 if (kerr != KCGI_OK)
3910 goto err;
3912 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3913 "blame_date", KATTR__MAX);
3914 if (kerr != KCGI_OK)
3915 goto err;
3916 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3917 if (kerr != KCGI_OK)
3918 goto err;
3919 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3920 if (kerr != KCGI_OK)
3921 goto err;
3923 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3924 "blame_author", KATTR__MAX);
3925 if (kerr != KCGI_OK)
3926 goto err;
3927 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
3928 if (kerr != KCGI_OK)
3929 goto err;
3930 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3931 if (kerr != KCGI_OK)
3932 goto err;
3934 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3935 "blame_code", KATTR__MAX);
3936 if (kerr != KCGI_OK)
3937 goto err;
3938 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
3939 if (kerr != KCGI_OK)
3940 goto err;
3941 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3942 if (kerr != KCGI_OK)
3943 goto err;
3945 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3946 if (kerr != KCGI_OK)
3947 goto err;
3949 a->lineno_cur++;
3950 bline = &a->lines[a->lineno_cur - 1];
3951 err:
3952 free(href_diff);
3954 done:
3955 if (commit)
3956 got_object_commit_close(commit);
3957 free(line);
3958 if (err == NULL && kerr != KCGI_OK)
3959 err = gw_kcgi_error(kerr);
3960 return err;
3963 static const struct got_error *
3964 gw_output_file_blame(struct gw_trans *gw_trans, struct gw_header *header)
3966 const struct got_error *error = NULL;
3967 struct got_object_id *obj_id = NULL;
3968 struct got_object_id *commit_id = NULL;
3969 struct got_blob_object *blob = NULL;
3970 char *path = NULL, *in_repo_path = NULL;
3971 struct gw_blame_cb_args bca;
3972 int i, obj_type;
3973 off_t filesize;
3975 if (asprintf(&path, "%s%s%s",
3976 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3977 gw_trans->repo_folder ? "/" : "",
3978 gw_trans->repo_file) == -1) {
3979 error = got_error_from_errno("asprintf");
3980 goto done;
3983 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path);
3984 if (error)
3985 goto done;
3987 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
3988 GOT_OBJ_TYPE_COMMIT, &header->refs, gw_trans->repo);
3989 if (error)
3990 goto done;
3992 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
3993 in_repo_path);
3994 if (error)
3995 goto done;
3997 if (obj_id == NULL) {
3998 error = got_error(GOT_ERR_NO_OBJ);
3999 goto done;
4002 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
4003 if (error)
4004 goto done;
4006 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4007 error = got_error(GOT_ERR_OBJ_TYPE);
4008 goto done;
4011 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
4012 if (error)
4013 goto done;
4015 bca.f = got_opentemp();
4016 if (bca.f == NULL) {
4017 error = got_error_from_errno("got_opentemp");
4018 goto done;
4020 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4021 &bca.line_offsets, bca.f, blob);
4022 if (error || bca.nlines == 0)
4023 goto done;
4025 /* Don't include \n at EOF in the blame line count. */
4026 if (bca.line_offsets[bca.nlines - 1] == filesize)
4027 bca.nlines--;
4029 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4030 if (bca.lines == NULL) {
4031 error = got_error_from_errno("calloc");
4032 goto done;
4034 bca.lineno_cur = 1;
4035 bca.nlines_prec = 0;
4036 i = bca.nlines;
4037 while (i > 0) {
4038 i /= 10;
4039 bca.nlines_prec++;
4041 bca.repo = gw_trans->repo;
4042 bca.gw_trans = gw_trans;
4044 error = got_blame(in_repo_path, commit_id, gw_trans->repo, gw_blame_cb,
4045 &bca, NULL, NULL);
4046 done:
4047 free(in_repo_path);
4048 free(commit_id);
4049 free(obj_id);
4050 free(path);
4052 if (blob) {
4053 free(bca.line_offsets);
4054 for (i = 0; i < bca.nlines; i++) {
4055 struct blame_line *bline = &bca.lines[i];
4056 free(bline->id_str);
4057 free(bline->committer);
4059 free(bca.lines);
4060 if (bca.f && fclose(bca.f) == EOF && error == NULL)
4061 error = got_error_from_errno("fclose");
4063 if (blob)
4064 got_object_blob_close(blob);
4065 return error;
4068 static const struct got_error *
4069 gw_output_blob_buf(struct gw_trans *gw_trans, struct gw_header *header)
4071 const struct got_error *error = NULL;
4072 struct got_object_id *obj_id = NULL;
4073 struct got_object_id *commit_id = NULL;
4074 struct got_blob_object *blob = NULL;
4075 char *path = NULL, *in_repo_path = NULL;
4076 int obj_type, set_mime = 0;
4077 size_t len, hdrlen;
4078 const uint8_t *buf;
4079 enum kcgi_err kerr = KCGI_OK;
4081 if (asprintf(&path, "%s%s%s",
4082 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4083 gw_trans->repo_folder ? "/" : "",
4084 gw_trans->repo_file) == -1) {
4085 error = got_error_from_errno("asprintf");
4086 goto done;
4089 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path);
4090 if (error)
4091 goto done;
4093 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
4094 GOT_OBJ_TYPE_COMMIT, &header->refs, gw_trans->repo);
4095 if (error)
4096 goto done;
4098 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
4099 in_repo_path);
4100 if (error)
4101 goto done;
4103 if (obj_id == NULL) {
4104 error = got_error(GOT_ERR_NO_OBJ);
4105 goto done;
4108 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
4109 if (error)
4110 goto done;
4112 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4113 error = got_error(GOT_ERR_OBJ_TYPE);
4114 goto done;
4117 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
4118 if (error)
4119 goto done;
4121 hdrlen = got_object_blob_get_hdrlen(blob);
4122 do {
4123 error = got_object_blob_read_block(&len, blob);
4124 if (error)
4125 goto done;
4126 buf = got_object_blob_get_read_buf(blob);
4129 * Skip blob object header first time around,
4130 * which also contains a zero byte.
4132 buf += hdrlen;
4133 if (set_mime == 0) {
4134 if (isbinary(buf, len - hdrlen))
4135 gw_trans->mime = KMIME_APP_OCTET_STREAM;
4136 else
4137 gw_trans->mime = KMIME_TEXT_PLAIN;
4138 set_mime = 1;
4139 error = gw_display_index(gw_trans);
4140 if (error)
4141 goto done;
4143 kerr = khttp_write(gw_trans->gw_req, buf, len - hdrlen);
4144 if (kerr != KCGI_OK)
4145 goto done;
4146 hdrlen = 0;
4147 } while (len != 0);
4148 done:
4149 free(in_repo_path);
4150 free(commit_id);
4151 free(obj_id);
4152 free(path);
4153 if (blob)
4154 got_object_blob_close(blob);
4155 if (error == NULL && kerr != KCGI_OK)
4156 error = gw_kcgi_error(kerr);
4157 return error;
4160 static const struct got_error *
4161 gw_output_repo_tree(struct gw_trans *gw_trans, struct gw_header *header)
4163 const struct got_error *error = NULL;
4164 struct got_object_id *tree_id = NULL, *commit_id = NULL;
4165 struct got_tree_object *tree = NULL;
4166 char *path = NULL, *in_repo_path = NULL;
4167 char *id_str = NULL;
4168 char *build_folder = NULL;
4169 char *href_blob = NULL, *href_blame = NULL;
4170 const char *class = NULL;
4171 int nentries, i, class_flip = 0;
4172 enum kcgi_err kerr = KCGI_OK;
4174 if (gw_trans->repo_folder != NULL) {
4175 path = strdup(gw_trans->repo_folder);
4176 if (path == NULL) {
4177 error = got_error_from_errno("strdup");
4178 goto done;
4180 } else {
4181 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
4182 gw_trans->repo_path);
4183 if (error)
4184 goto done;
4185 free(path);
4186 path = in_repo_path;
4189 if (gw_trans->commit_id == NULL) {
4190 struct got_reference *head_ref;
4191 error = got_ref_open(&head_ref, gw_trans->repo,
4192 gw_trans->headref, 0);
4193 if (error)
4194 goto done;
4195 error = got_ref_resolve(&commit_id, gw_trans->repo, head_ref);
4196 if (error)
4197 goto done;
4198 got_ref_close(head_ref);
4200 * gw_trans->commit_id was not parsed from the querystring
4201 * we hit this code path from gw_index, where we don't know the
4202 * commit values for the tree link yet, so set
4203 * gw_trans->commit_id here to continue further into the tree
4205 error = got_object_id_str(&gw_trans->commit_id, commit_id);
4206 if (error)
4207 goto done;
4209 } else {
4210 error = got_repo_match_object_id(&commit_id, NULL,
4211 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT, &header->refs,
4212 gw_trans->repo);
4213 if (error)
4214 goto done;
4217 error = got_object_id_by_path(&tree_id, gw_trans->repo, commit_id,
4218 path);
4219 if (error)
4220 goto done;
4222 error = got_object_open_as_tree(&tree, gw_trans->repo, tree_id);
4223 if (error)
4224 goto done;
4226 nentries = got_object_tree_get_nentries(tree);
4227 for (i = 0; i < nentries; i++) {
4228 struct got_tree_entry *te;
4229 const char *modestr = "";
4230 mode_t mode;
4232 te = got_object_tree_get_entry(tree, i);
4234 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
4235 if (error)
4236 goto done;
4238 mode = got_tree_entry_get_mode(te);
4239 if (got_object_tree_entry_is_submodule(te))
4240 modestr = "$";
4241 else if (S_ISLNK(mode))
4242 modestr = "@";
4243 else if (S_ISDIR(mode))
4244 modestr = "/";
4245 else if (mode & S_IXUSR)
4246 modestr = "*";
4248 if (class_flip == 0) {
4249 class = "back_lightgray";
4250 class_flip = 1;
4251 } else {
4252 class = "back_white";
4253 class_flip = 0;
4256 if (S_ISDIR(mode)) {
4257 if (asprintf(&build_folder, "%s/%s",
4258 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4259 got_tree_entry_get_name(te)) == -1) {
4260 error = got_error_from_errno("asprintf");
4261 goto done;
4264 href_blob = khttp_urlpart(NULL, NULL, "gotweb", "path",
4265 gw_trans->repo_name, "action",
4266 gw_get_action_name(gw_trans), "commit",
4267 gw_trans->commit_id, "folder", build_folder, NULL);
4268 if (href_blob == NULL) {
4269 error = got_error_from_errno("khttp_urlpart");
4270 goto done;
4272 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4273 KATTR_ID, "tree_wrapper", KATTR__MAX);
4274 if (kerr != KCGI_OK)
4275 goto done;
4276 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4277 KATTR_ID, "tree_line", KATTR_CLASS, class,
4278 KATTR__MAX);
4279 if (kerr != KCGI_OK)
4280 goto done;
4281 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4282 KATTR_HREF, href_blob, KATTR_CLASS,
4283 "diff_directory", KATTR__MAX);
4284 if (kerr != KCGI_OK)
4285 goto done;
4286 kerr = khtml_printf(gw_trans->gw_html_req, "%s%s",
4287 got_tree_entry_get_name(te), modestr);
4288 if (kerr != KCGI_OK)
4289 goto done;
4290 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4291 if (kerr != KCGI_OK)
4292 goto done;
4293 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4294 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
4295 KATTR__MAX);
4296 if (kerr != KCGI_OK)
4297 goto done;
4298 kerr = khtml_entity(gw_trans->gw_html_req,
4299 KENTITY_nbsp);
4300 if (kerr != KCGI_OK)
4301 goto done;
4302 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4303 if (kerr != KCGI_OK)
4304 goto done;
4305 } else {
4306 href_blob = khttp_urlpart(NULL, NULL, "gotweb", "path",
4307 gw_trans->repo_name, "action", "blob", "commit",
4308 gw_trans->commit_id, "file",
4309 got_tree_entry_get_name(te), "folder",
4310 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4311 NULL);
4312 if (href_blob == NULL) {
4313 error = got_error_from_errno("khttp_urlpart");
4314 goto done;
4316 href_blame = khttp_urlpart(NULL, NULL, "gotweb", "path",
4317 gw_trans->repo_name, "action", "blame", "commit",
4318 gw_trans->commit_id, "file",
4319 got_tree_entry_get_name(te), "folder",
4320 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4321 NULL);
4322 if (href_blame == NULL) {
4323 error = got_error_from_errno("khttp_urlpart");
4324 goto done;
4326 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4327 KATTR_ID, "tree_wrapper", KATTR__MAX);
4328 if (kerr != KCGI_OK)
4329 goto done;
4330 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4331 KATTR_ID, "tree_line", KATTR_CLASS, class,
4332 KATTR__MAX);
4333 if (kerr != KCGI_OK)
4334 goto done;
4335 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4336 KATTR_HREF, href_blob, KATTR__MAX);
4337 if (kerr != KCGI_OK)
4338 goto done;
4339 kerr = khtml_printf(gw_trans->gw_html_req, "%s%s",
4340 got_tree_entry_get_name(te), modestr);
4341 if (kerr != KCGI_OK)
4342 goto done;
4343 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4344 if (kerr != KCGI_OK)
4345 goto done;
4346 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4347 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
4348 KATTR__MAX);
4349 if (kerr != KCGI_OK)
4350 goto done;
4352 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4353 KATTR_HREF, href_blob, KATTR__MAX);
4354 if (kerr != KCGI_OK)
4355 goto done;
4356 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
4357 if (kerr != KCGI_OK)
4358 goto done;
4359 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4360 if (kerr != KCGI_OK)
4361 goto done;
4363 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4364 if (kerr != KCGI_OK)
4365 goto done;
4367 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4368 KATTR_HREF, href_blame, KATTR__MAX);
4369 if (kerr != KCGI_OK)
4370 goto done;
4371 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
4372 if (kerr != KCGI_OK)
4373 goto done;
4375 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4376 if (kerr != KCGI_OK)
4377 goto done;
4379 free(id_str);
4380 id_str = NULL;
4381 free(href_blob);
4382 href_blob = NULL;
4383 free(build_folder);
4384 build_folder = NULL;
4386 done:
4387 if (tree)
4388 got_object_tree_close(tree);
4389 free(id_str);
4390 free(href_blob);
4391 free(href_blame);
4392 free(in_repo_path);
4393 free(tree_id);
4394 free(build_folder);
4395 if (error == NULL && kerr != KCGI_OK)
4396 error = gw_kcgi_error(kerr);
4397 return error;
4400 static const struct got_error *
4401 gw_output_repo_heads(struct gw_trans *gw_trans)
4403 const struct got_error *error = NULL;
4404 struct got_reflist_head refs;
4405 struct got_reflist_entry *re;
4406 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
4407 char *href_commits = NULL;
4408 enum kcgi_err kerr = KCGI_OK;
4410 TAILQ_INIT(&refs);
4412 error = got_ref_list(&refs, gw_trans->repo, "refs/heads",
4413 got_ref_cmp_by_name, NULL);
4414 if (error)
4415 goto done;
4417 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4418 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
4419 if (kerr != KCGI_OK)
4420 goto done;
4421 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4422 KATTR_ID, "summary_heads_title", KATTR__MAX);
4423 if (kerr != KCGI_OK)
4424 goto done;
4425 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
4426 if (kerr != KCGI_OK)
4427 goto done;
4428 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4429 if (kerr != KCGI_OK)
4430 goto done;
4431 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4432 KATTR_ID, "summary_heads_content", KATTR__MAX);
4433 if (kerr != KCGI_OK)
4434 goto done;
4436 TAILQ_FOREACH(re, &refs, entry) {
4437 const char *refname;
4439 if (got_ref_is_symbolic(re->ref))
4440 continue;
4442 refname = got_ref_get_name(re->ref);
4443 if (strncmp(refname, "refs/heads/", 11) != 0)
4444 continue;
4446 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
4447 refname, TM_DIFF);
4448 if (error)
4449 goto done;
4451 if (strncmp(refname, "refs/heads/", 11) == 0)
4452 refname += 11;
4454 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4455 KATTR_ID, "heads_wrapper", KATTR__MAX);
4456 if (kerr != KCGI_OK)
4457 goto done;
4458 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4459 KATTR_ID, "heads_age", KATTR__MAX);
4460 if (kerr != KCGI_OK)
4461 goto done;
4462 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
4463 if (kerr != KCGI_OK)
4464 goto done;
4465 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4466 if (kerr != KCGI_OK)
4467 goto done;
4468 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4469 KATTR_ID, "heads_space", KATTR__MAX);
4470 if (kerr != KCGI_OK)
4471 goto done;
4472 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
4473 if (kerr != KCGI_OK)
4474 goto done;
4475 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4476 if (kerr != KCGI_OK)
4477 goto done;
4478 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4479 KATTR_ID, "head", KATTR__MAX);
4480 if (kerr != KCGI_OK)
4481 goto done;
4483 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
4484 gw_trans->repo_name, "action", "summary", "headref",
4485 refname, NULL);
4486 if (href_summary == NULL) {
4487 error = got_error_from_errno("khttp_urlpart");
4488 goto done;
4490 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4491 href_summary, KATTR__MAX);
4492 kerr = khtml_puts(gw_trans->gw_html_req, refname);
4493 if (kerr != KCGI_OK)
4494 goto done;
4495 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4496 if (kerr != KCGI_OK)
4497 goto done;
4499 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4500 "navs_wrapper", KATTR__MAX);
4501 if (kerr != KCGI_OK)
4502 goto done;
4503 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4504 "navs", KATTR__MAX);
4505 if (kerr != KCGI_OK)
4506 goto done;
4508 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4509 href_summary, KATTR__MAX);
4510 if (kerr != KCGI_OK)
4511 goto done;
4512 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4513 if (kerr != KCGI_OK)
4514 goto done;
4515 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4516 if (kerr != KCGI_OK)
4517 goto done;
4519 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4520 if (kerr != KCGI_OK)
4521 goto done;
4523 href_briefs = khttp_urlpart(NULL, NULL, "gotweb", "path",
4524 gw_trans->repo_name, "action", "briefs", "headref",
4525 refname, NULL);
4526 if (href_briefs == NULL) {
4527 error = got_error_from_errno("khttp_urlpart");
4528 goto done;
4530 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4531 href_briefs, KATTR__MAX);
4532 if (kerr != KCGI_OK)
4533 goto done;
4534 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4535 if (kerr != KCGI_OK)
4536 goto done;
4537 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4538 if (kerr != KCGI_OK)
4539 goto done;
4541 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4542 if (kerr != KCGI_OK)
4543 goto done;
4545 href_commits = khttp_urlpart(NULL, NULL, "gotweb", "path",
4546 gw_trans->repo_name, "action", "commits", "headref",
4547 refname, NULL);
4548 if (href_commits == NULL) {
4549 error = got_error_from_errno("khttp_urlpart");
4550 goto done;
4552 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4553 href_commits, KATTR__MAX);
4554 if (kerr != KCGI_OK)
4555 goto done;
4556 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4557 if (kerr != KCGI_OK)
4558 goto done;
4559 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4560 if (kerr != KCGI_OK)
4561 goto done;
4563 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4564 "dotted_line", KATTR__MAX);
4565 if (kerr != KCGI_OK)
4566 goto done;
4567 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4568 if (kerr != KCGI_OK)
4569 goto done;
4570 free(href_summary);
4571 href_summary = NULL;
4572 free(href_briefs);
4573 href_briefs = NULL;
4574 free(href_commits);
4575 href_commits = NULL;
4577 done:
4578 got_ref_list_free(&refs);
4579 free(href_summary);
4580 free(href_briefs);
4581 free(href_commits);
4582 return error;
4585 static const struct got_error *
4586 gw_output_site_link(struct gw_trans *gw_trans)
4588 const struct got_error *error = NULL;
4589 char *href_summary = NULL;
4590 enum kcgi_err kerr = KCGI_OK;
4592 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4593 "site_link", KATTR__MAX);
4594 if (kerr != KCGI_OK)
4595 goto done;
4596 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4597 KATTR__MAX);
4598 if (kerr != KCGI_OK)
4599 goto done;
4600 kerr = khtml_puts(gw_trans->gw_html_req,
4601 gw_trans->gw_conf->got_site_link);
4602 if (kerr != KCGI_OK)
4603 goto done;
4604 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4605 if (kerr != KCGI_OK)
4606 goto done;
4608 if (gw_trans->repo_name != NULL) {
4609 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4610 if (kerr != KCGI_OK)
4611 goto done;
4613 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
4614 gw_trans->repo_name, "action", "summary", NULL);
4615 if (href_summary == NULL) {
4616 error = got_error_from_errno("khttp_urlpart");
4617 goto done;
4619 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4620 href_summary, KATTR__MAX);
4621 if (kerr != KCGI_OK)
4622 goto done;
4623 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4624 if (kerr != KCGI_OK)
4625 goto done;
4626 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4627 if (kerr != KCGI_OK)
4628 goto done;
4629 kerr = khtml_printf(gw_trans->gw_html_req, " / %s",
4630 gw_get_action_name(gw_trans));
4631 if (kerr != KCGI_OK)
4632 goto done;
4635 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4636 if (kerr != KCGI_OK)
4637 goto done;
4638 done:
4639 free(href_summary);
4640 if (error == NULL && kerr != KCGI_OK)
4641 error = gw_kcgi_error(kerr);
4642 return error;
4645 static const struct got_error *
4646 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4648 const struct got_error *error = NULL;
4649 char *color = NULL;
4650 enum kcgi_err kerr = KCGI_OK;
4652 if (strncmp(buf, "-", 1) == 0)
4653 color = "diff_minus";
4654 else if (strncmp(buf, "+", 1) == 0)
4655 color = "diff_plus";
4656 else if (strncmp(buf, "@@", 2) == 0)
4657 color = "diff_chunk_header";
4658 else if (strncmp(buf, "@@", 2) == 0)
4659 color = "diff_chunk_header";
4660 else if (strncmp(buf, "commit +", 8) == 0)
4661 color = "diff_meta";
4662 else if (strncmp(buf, "commit -", 8) == 0)
4663 color = "diff_meta";
4664 else if (strncmp(buf, "blob +", 6) == 0)
4665 color = "diff_meta";
4666 else if (strncmp(buf, "blob -", 6) == 0)
4667 color = "diff_meta";
4668 else if (strncmp(buf, "file +", 6) == 0)
4669 color = "diff_meta";
4670 else if (strncmp(buf, "file -", 6) == 0)
4671 color = "diff_meta";
4672 else if (strncmp(buf, "from:", 5) == 0)
4673 color = "diff_author";
4674 else if (strncmp(buf, "via:", 4) == 0)
4675 color = "diff_author";
4676 else if (strncmp(buf, "date:", 5) == 0)
4677 color = "diff_date";
4678 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4679 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4680 if (error == NULL && kerr != KCGI_OK)
4681 error = gw_kcgi_error(kerr);
4682 return error;
4685 int
4686 main(int argc, char *argv[])
4688 const struct got_error *error = NULL, *error2 = NULL;
4689 struct gw_trans *gw_trans;
4690 struct gw_dir *dir = NULL, *tdir;
4691 const char *page = "index";
4692 enum kcgi_err kerr = KCGI_OK;
4694 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4695 errx(1, "malloc");
4697 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4698 errx(1, "malloc");
4700 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4701 errx(1, "malloc");
4703 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4704 errx(1, "malloc");
4706 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4707 if (kerr != KCGI_OK) {
4708 error = gw_kcgi_error(kerr);
4709 goto done;
4712 TAILQ_INIT(&gw_trans->gw_dirs);
4713 TAILQ_INIT(&gw_trans->gw_headers);
4715 gw_trans->action = -1;
4716 gw_trans->page = 0;
4717 gw_trans->repos_total = 0;
4718 gw_trans->repo_path = NULL;
4719 gw_trans->commit_id = NULL;
4720 gw_trans->next_id = NULL;
4721 gw_trans->next_prev_id = NULL;
4722 gw_trans->prev_id = NULL;
4723 gw_trans->prev_prev_id = NULL;
4724 gw_trans->headref = GOT_REF_HEAD;
4725 gw_trans->mime = KMIME_TEXT_HTML;
4726 gw_trans->gw_tmpl->key = gw_templs;
4727 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4728 gw_trans->gw_tmpl->arg = gw_trans;
4729 gw_trans->gw_tmpl->cb = gw_template;
4731 error = parse_gotweb_config(&gw_trans->gw_conf, GOTWEB_CONF);
4732 if (error)
4733 goto done;
4735 error = gw_parse_querystring(gw_trans);
4736 if (error)
4737 goto done;
4739 if (gw_trans->action == GW_BLOB)
4740 error = gw_blob(gw_trans);
4741 else
4742 error = gw_display_index(gw_trans);
4743 done:
4744 if (gw_trans->repo) {
4745 const struct got_error *close_err;
4746 close_err = got_repo_close(gw_trans->repo);
4747 if (error == NULL)
4748 error = close_err;
4750 if (error) {
4751 gw_trans->error = error;
4752 gw_trans->action = GW_ERR;
4753 error2 = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
4754 if (error2)
4755 goto cleanup; /* we can't display an error page */
4756 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
4757 if (kerr != KCGI_OK)
4758 goto cleanup; /* we can't display an error page */
4759 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
4760 gw_query_funcs[gw_trans->action].template);
4761 if (kerr != KCGI_OK) {
4762 khtml_close(gw_trans->gw_html_req);
4763 goto cleanup; /* we can't display an error page */
4767 cleanup:
4768 free(gw_trans->gw_conf->got_repos_path);
4769 free(gw_trans->gw_conf->got_www_path);
4770 free(gw_trans->gw_conf->got_site_name);
4771 free(gw_trans->gw_conf->got_site_owner);
4772 free(gw_trans->gw_conf->got_site_link);
4773 free(gw_trans->gw_conf->got_logo);
4774 free(gw_trans->gw_conf->got_logo_url);
4775 free(gw_trans->gw_conf);
4776 free(gw_trans->commit_id);
4777 free(gw_trans->next_id);
4778 free(gw_trans->next_prev_id);
4779 free(gw_trans->prev_id);
4780 free(gw_trans->prev_prev_id);
4781 free(gw_trans->repo_path);
4782 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4783 free(dir->name);
4784 free(dir->description);
4785 free(dir->age);
4786 free(dir->url);
4787 free(dir->path);
4788 free(dir);
4791 khttp_free(gw_trans->gw_req);
4792 return 0;