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 static const struct got_error *gw_output_blob_buf(struct gw_trans *);
180 static const struct got_error *gw_output_repo_tree(struct gw_trans *);
181 static const struct got_error *gw_output_diff(struct gw_trans *,
182 struct gw_header *);
183 static const struct got_error *gw_output_repo_tags(struct gw_trans *,
184 struct gw_header *, int, int);
185 static const struct got_error *gw_output_repo_heads(struct gw_trans *);
186 static const struct got_error *gw_output_site_link(struct gw_trans *);
187 static const struct got_error *gw_get_clone_url(char **, struct gw_trans *,
188 char *);
189 static const struct got_error *gw_colordiff_line(struct gw_trans *, char *);
191 static const struct got_error *gw_gen_commit_header(struct gw_trans *, char *,
192 char*);
193 static const struct got_error *gw_gen_diff_header(struct gw_trans *, char *,
194 char*);
195 static const struct got_error *gw_gen_author_header(struct gw_trans *,
196 const char *);
197 static const struct got_error *gw_gen_age_header(struct gw_trans *,
198 const char *);
199 static const struct got_error *gw_gen_committer_header(struct gw_trans *,
200 const char *);
201 static const struct got_error *gw_gen_commit_msg_header(struct gw_trans*,
202 char *);
203 static const struct got_error *gw_gen_tree_header(struct gw_trans *, char *);
204 static const struct got_error *gw_display_open(struct gw_trans *, enum khttp,
205 enum kmime);
206 static const struct got_error *gw_display_index(struct gw_trans *);
207 static const struct got_error *gw_get_header(struct gw_trans *,
208 struct gw_header *, int);
209 static const struct got_error *gw_get_commits(struct gw_trans *,
210 struct gw_header *, int,
211 struct got_object_id *);
212 static const struct got_error *gw_get_commit(struct gw_trans *,
213 struct gw_header *,
214 struct got_commit_object *,
215 struct got_object_id *);
216 static const struct got_error *gw_apply_unveil(const char *);
217 static const struct got_error *gw_blame_cb(void *, int, int,
218 struct got_object_id *);
219 static const struct got_error *gw_load_got_paths(struct gw_trans *);
220 static const struct got_error *gw_load_got_path(struct gw_trans *,
221 struct gw_dir *);
222 static const struct got_error *gw_parse_querystring(struct gw_trans *);
223 static const struct got_error *gw_blame(struct gw_trans *);
224 static const struct got_error *gw_blob(struct gw_trans *);
225 static const struct got_error *gw_diff(struct gw_trans *);
226 static const struct got_error *gw_index(struct gw_trans *);
227 static const struct got_error *gw_commits(struct gw_trans *);
228 static const struct got_error *gw_briefs(struct gw_trans *);
229 static const struct got_error *gw_summary(struct gw_trans *);
230 static const struct got_error *gw_tree(struct gw_trans *);
231 static const struct got_error *gw_tag(struct gw_trans *);
232 static const struct got_error *gw_tags(struct gw_trans *);
234 struct gw_query_action {
235 unsigned int func_id;
236 const char *func_name;
237 const struct got_error *(*func_main)(struct gw_trans *);
238 char *template;
239 };
241 enum gw_query_actions {
242 GW_BLAME,
243 GW_BLOB,
244 GW_BRIEFS,
245 GW_COMMITS,
246 GW_DIFF,
247 GW_ERR,
248 GW_INDEX,
249 GW_SUMMARY,
250 GW_TAG,
251 GW_TAGS,
252 GW_TREE,
253 };
255 static struct gw_query_action gw_query_funcs[] = {
256 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
257 { GW_BLOB, "blob", NULL, NULL },
258 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
259 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
260 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
261 { GW_ERR, "error", gw_error, "gw_tmpl/err.tmpl" },
262 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
263 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
264 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
265 { GW_TAGS, "tags", gw_tags, "gw_tmpl/tags.tmpl" },
266 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
267 };
269 static const char *
270 gw_get_action_name(struct gw_trans *gw_trans)
272 return gw_query_funcs[gw_trans->action].func_name;
275 static const struct got_error *
276 gw_kcgi_error(enum kcgi_err kerr)
278 if (kerr == KCGI_OK)
279 return NULL;
281 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
282 return got_error(GOT_ERR_CANCELLED);
284 if (kerr == KCGI_ENOMEM)
285 return got_error_set_errno(ENOMEM,
286 kcgi_strerror(kerr));
288 if (kerr == KCGI_ENFILE)
289 return got_error_set_errno(ENFILE,
290 kcgi_strerror(kerr));
292 if (kerr == KCGI_EAGAIN)
293 return got_error_set_errno(EAGAIN,
294 kcgi_strerror(kerr));
296 if (kerr == KCGI_FORM)
297 return got_error_msg(GOT_ERR_IO,
298 kcgi_strerror(kerr));
300 return got_error_from_errno(kcgi_strerror(kerr));
303 static const struct got_error *
304 gw_apply_unveil(const char *repo_path)
306 const struct got_error *err;
308 if (repo_path && unveil(repo_path, "r") != 0)
309 return got_error_from_errno2("unveil", repo_path);
311 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
312 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
314 err = got_privsep_unveil_exec_helpers();
315 if (err != NULL)
316 return err;
318 if (unveil(NULL, NULL) != 0)
319 return got_error_from_errno("unveil");
321 return NULL;
324 static int
325 isbinary(const uint8_t *buf, size_t n)
327 size_t i;
329 for (i = 0; i < n; i++)
330 if (buf[i] == 0)
331 return 1;
332 return 0;
335 static const struct got_error *
336 gw_blame(struct gw_trans *gw_trans)
338 const struct got_error *error = NULL;
339 struct gw_header *header = NULL;
340 char *age = NULL;
341 enum kcgi_err kerr = KCGI_OK;
343 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
344 NULL) == -1)
345 return got_error_from_errno("pledge");
347 if ((header = gw_init_header()) == NULL)
348 return got_error_from_errno("malloc");
350 error = gw_apply_unveil(gw_trans->gw_dir->path);
351 if (error)
352 goto done;
354 /* check querystring */
355 if (gw_trans->repo_file == NULL) {
356 error = got_error_msg(GOT_ERR_QUERYSTRING,
357 "file required in querystring");
358 goto done;
360 if (gw_trans->commit_id == NULL) {
361 error = got_error_msg(GOT_ERR_QUERYSTRING,
362 "commit required in querystring");
363 goto done;
366 error = gw_get_header(gw_trans, header, 1);
367 if (error)
368 goto done;
369 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
370 "blame_header_wrapper", KATTR__MAX);
371 if (kerr != KCGI_OK)
372 goto done;
373 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
374 "blame_header", KATTR__MAX);
375 if (kerr != KCGI_OK)
376 goto done;
377 error = gw_get_time_str(&age, header->committer_time,
378 TM_LONG);
379 if (error)
380 goto done;
381 error = gw_gen_age_header(gw_trans, age ?age : "");
382 if (error)
383 goto done;
384 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
385 if (error)
386 goto done;
387 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
388 if (kerr != KCGI_OK)
389 goto done;
390 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
391 "dotted_line", KATTR__MAX);
392 if (kerr != KCGI_OK)
393 goto done;
394 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
395 if (kerr != KCGI_OK)
396 goto done;
398 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
399 "blame", KATTR__MAX);
400 if (kerr != KCGI_OK)
401 goto done;
402 error = gw_output_file_blame(gw_trans);
403 if (error)
404 goto done;
405 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
406 done:
407 gw_free_header(header);
408 if (error == NULL && kerr != KCGI_OK)
409 error = gw_kcgi_error(kerr);
410 return error;
413 static const struct got_error *
414 gw_blob(struct gw_trans *gw_trans)
416 const struct got_error *error = NULL, *err = NULL;
417 struct gw_header *header = NULL;
418 enum kcgi_err kerr = KCGI_OK;
420 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
421 NULL) == -1)
422 return got_error_from_errno("pledge");
424 if ((header = gw_init_header()) == NULL)
425 return got_error_from_errno("malloc");
427 error = gw_apply_unveil(gw_trans->gw_dir->path);
428 if (error)
429 goto done;
431 /* check querystring */
432 if (gw_trans->repo_file == NULL) {
433 error = got_error_msg(GOT_ERR_QUERYSTRING,
434 "file required in querystring");
435 goto done;
437 if (gw_trans->commit_id == NULL) {
438 error = got_error_msg(GOT_ERR_QUERYSTRING,
439 "commit required in querystring");
440 goto done;
442 error = gw_get_header(gw_trans, header, 1);
443 if (error)
444 goto done;
446 error = gw_output_blob_buf(gw_trans);
447 done:
449 if (error) {
450 gw_trans->mime = KMIME_TEXT_PLAIN;
451 err = gw_display_index(gw_trans);
452 if (err) {
453 error = err;
454 goto errored;
456 kerr = khttp_puts(gw_trans->gw_req, error->msg);
458 errored:
459 gw_free_header(header);
460 if (error == NULL && kerr != KCGI_OK)
461 error = gw_kcgi_error(kerr);
462 return error;
465 static const struct got_error *
466 gw_diff(struct gw_trans *gw_trans)
468 const struct got_error *error = NULL;
469 struct gw_header *header = NULL;
470 char *age = NULL;
471 enum kcgi_err kerr = KCGI_OK;
473 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
474 NULL) == -1)
475 return got_error_from_errno("pledge");
477 if ((header = gw_init_header()) == NULL)
478 return got_error_from_errno("malloc");
480 error = gw_apply_unveil(gw_trans->gw_dir->path);
481 if (error)
482 goto done;
484 error = gw_get_header(gw_trans, header, 1);
485 if (error)
486 goto done;
488 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
489 "diff_header_wrapper", KATTR__MAX);
490 if (kerr != KCGI_OK)
491 goto done;
492 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
493 "diff_header", KATTR__MAX);
494 if (kerr != KCGI_OK)
495 goto done;
496 error = gw_gen_diff_header(gw_trans, header->parent_id,
497 header->commit_id);
498 if (error)
499 goto done;
500 error = gw_gen_commit_header(gw_trans, header->commit_id,
501 header->refs_str);
502 if (error)
503 goto done;
504 error = gw_gen_tree_header(gw_trans, header->tree_id);
505 if (error)
506 goto done;
507 error = gw_gen_author_header(gw_trans, header->author);
508 if (error)
509 goto done;
510 error = gw_gen_committer_header(gw_trans, header->author);
511 if (error)
512 goto done;
513 error = gw_get_time_str(&age, header->committer_time,
514 TM_LONG);
515 if (error)
516 goto done;
517 error = gw_gen_age_header(gw_trans, age ?age : "");
518 if (error)
519 goto done;
520 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
521 if (error)
522 goto done;
523 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
524 if (kerr != KCGI_OK)
525 goto done;
526 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
527 "dotted_line", KATTR__MAX);
528 if (kerr != KCGI_OK)
529 goto done;
530 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
531 if (kerr != KCGI_OK)
532 goto done;
534 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
535 "diff", KATTR__MAX);
536 if (kerr != KCGI_OK)
537 goto done;
538 error = gw_output_diff(gw_trans, header);
539 if (error)
540 goto done;
542 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
543 done:
544 gw_free_header(header);
545 free(age);
546 if (error == NULL && kerr != KCGI_OK)
547 error = gw_kcgi_error(kerr);
548 return error;
551 static const struct got_error *
552 gw_index(struct gw_trans *gw_trans)
554 const struct got_error *error = NULL;
555 struct gw_dir *gw_dir = NULL;
556 char *href_next = NULL, *href_prev = NULL, *href_summary = NULL;
557 char *href_briefs = NULL, *href_commits = NULL, *href_tree = NULL;
558 char *href_tags = NULL;
559 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
560 enum kcgi_err kerr = KCGI_OK;
562 if (pledge("stdio rpath proc exec sendfd unveil",
563 NULL) == -1) {
564 error = got_error_from_errno("pledge");
565 return error;
568 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path);
569 if (error)
570 return error;
572 error = gw_load_got_paths(gw_trans);
573 if (error)
574 return error;
576 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
577 "index_header", KATTR__MAX);
578 if (kerr != KCGI_OK)
579 return gw_kcgi_error(kerr);
580 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
581 "index_header_project", KATTR__MAX);
582 if (kerr != KCGI_OK)
583 return gw_kcgi_error(kerr);
584 kerr = khtml_puts(gw_trans->gw_html_req, "Project");
585 if (kerr != KCGI_OK)
586 return gw_kcgi_error(kerr);
587 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
588 if (kerr != KCGI_OK)
589 return gw_kcgi_error(kerr);
591 if (gw_trans->gw_conf->got_show_repo_description) {
592 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
593 "index_header_description", KATTR__MAX);
594 if (kerr != KCGI_OK)
595 return gw_kcgi_error(kerr);
596 kerr = khtml_puts(gw_trans->gw_html_req, "Description");
597 if (kerr != KCGI_OK)
598 return gw_kcgi_error(kerr);
599 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
600 if (kerr != KCGI_OK)
601 return gw_kcgi_error(kerr);
604 if (gw_trans->gw_conf->got_show_repo_owner) {
605 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
606 "index_header_owner", KATTR__MAX);
607 if (kerr != KCGI_OK)
608 return gw_kcgi_error(kerr);
609 kerr = khtml_puts(gw_trans->gw_html_req, "Owner");
610 if (kerr != KCGI_OK)
611 return gw_kcgi_error(kerr);
612 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
613 if (kerr != KCGI_OK)
614 return gw_kcgi_error(kerr);
617 if (gw_trans->gw_conf->got_show_repo_age) {
618 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
619 "index_header_age", KATTR__MAX);
620 if (kerr != KCGI_OK)
621 return gw_kcgi_error(kerr);
622 kerr = khtml_puts(gw_trans->gw_html_req, "Last Change");
623 if (kerr != KCGI_OK)
624 return gw_kcgi_error(kerr);
625 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
626 if (kerr != KCGI_OK)
627 return gw_kcgi_error(kerr);
630 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
631 if (kerr != KCGI_OK)
632 return gw_kcgi_error(kerr);
634 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
635 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
636 "index_wrapper", KATTR__MAX);
637 if (kerr != KCGI_OK)
638 return gw_kcgi_error(kerr);
639 kerr = khtml_printf(gw_trans->gw_html_req,
640 "No repositories found in %s",
641 gw_trans->gw_conf->got_repos_path);
642 if (kerr != KCGI_OK)
643 return gw_kcgi_error(kerr);
644 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
645 if (kerr != KCGI_OK)
646 return gw_kcgi_error(kerr);
647 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
648 "dotted_line", KATTR__MAX);
649 if (kerr != KCGI_OK)
650 return gw_kcgi_error(kerr);
651 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
652 if (kerr != KCGI_OK)
653 return gw_kcgi_error(kerr);
654 return error;
657 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
658 dir_c++;
660 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
661 if (gw_trans->page > 0 && (gw_trans->page *
662 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
663 prev_disp++;
664 continue;
667 prev_disp++;
669 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
670 "index_wrapper", KATTR__MAX);
671 if (kerr != KCGI_OK)
672 goto done;
674 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
675 gw_dir->name, "action", "summary", NULL);
676 if (href_summary == NULL) {
677 error = got_error_from_errno("khttp_urlpart");
678 goto done;
680 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
681 "index_project", KATTR__MAX);
682 if (kerr != KCGI_OK)
683 goto done;
684 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
685 href_summary, KATTR__MAX);
686 if (kerr != KCGI_OK)
687 goto done;
688 kerr = khtml_puts(gw_trans->gw_html_req, gw_dir->name);
689 if (kerr != KCGI_OK)
690 goto done;
691 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
692 if (kerr != KCGI_OK)
693 goto done;
694 if (gw_trans->gw_conf->got_show_repo_description) {
695 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
696 KATTR_ID, "index_project_description", KATTR__MAX);
697 if (kerr != KCGI_OK)
698 goto done;
699 kerr = khtml_puts(gw_trans->gw_html_req,
700 gw_dir->description ? gw_dir->description : "");
701 if (kerr != KCGI_OK)
702 goto done;
703 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
704 if (kerr != KCGI_OK)
705 goto done;
707 if (gw_trans->gw_conf->got_show_repo_owner) {
708 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
709 KATTR_ID, "index_project_owner", KATTR__MAX);
710 if (kerr != KCGI_OK)
711 goto done;
712 kerr = khtml_puts(gw_trans->gw_html_req,
713 gw_dir->owner ? gw_dir->owner : "");
714 if (kerr != KCGI_OK)
715 goto done;
716 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
717 if (kerr != KCGI_OK)
718 goto done;
720 if (gw_trans->gw_conf->got_show_repo_age) {
721 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
722 KATTR_ID, "index_project_age", KATTR__MAX);
723 if (kerr != KCGI_OK)
724 goto done;
725 kerr = khtml_puts(gw_trans->gw_html_req,
726 gw_dir->age ? gw_dir->age : "");
727 if (kerr != KCGI_OK)
728 goto done;
729 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
730 if (kerr != KCGI_OK)
731 goto done;
734 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
735 "navs_wrapper", KATTR__MAX);
736 if (kerr != KCGI_OK)
737 goto done;
738 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
739 "navs", KATTR__MAX);
740 if (kerr != KCGI_OK)
741 goto done;
743 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
744 href_summary, KATTR__MAX);
745 if (kerr != KCGI_OK)
746 goto done;
747 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
748 if (kerr != KCGI_OK)
749 goto done;
750 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
751 if (kerr != KCGI_OK)
752 goto done;
754 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
755 if (kerr != KCGI_OK)
756 goto done;
758 href_briefs = khttp_urlpart(NULL, NULL, "gotweb", "path",
759 gw_dir->name, "action", "briefs", NULL);
760 if (href_briefs == NULL) {
761 error = got_error_from_errno("khttp_urlpart");
762 goto done;
764 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
765 href_briefs, KATTR__MAX);
766 if (kerr != KCGI_OK)
767 goto done;
768 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
769 if (kerr != KCGI_OK)
770 goto done;
771 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
772 if (kerr != KCGI_OK)
773 error = gw_kcgi_error(kerr);
775 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
776 if (kerr != KCGI_OK)
777 goto done;
779 href_commits = khttp_urlpart(NULL, NULL, "gotweb", "path",
780 gw_dir->name, "action", "commits", NULL);
781 if (href_commits == NULL) {
782 error = got_error_from_errno("khttp_urlpart");
783 goto done;
785 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
786 href_commits, KATTR__MAX);
787 if (kerr != KCGI_OK)
788 goto done;
789 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
790 if (kerr != KCGI_OK)
791 goto done;
792 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
793 if (kerr != KCGI_OK)
794 goto done;
796 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
797 if (kerr != KCGI_OK)
798 goto done;
800 href_tags = khttp_urlpart(NULL, NULL, "gotweb", "path",
801 gw_dir->name, "action", "tags", NULL);
802 if (href_tags == NULL) {
803 error = got_error_from_errno("khttp_urlpart");
804 goto done;
806 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
807 href_tags, KATTR__MAX);
808 if (kerr != KCGI_OK)
809 goto done;
810 kerr = khtml_puts(gw_trans->gw_html_req, "tags");
811 if (kerr != KCGI_OK)
812 goto done;
813 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
814 if (kerr != KCGI_OK)
815 goto done;
817 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
818 if (kerr != KCGI_OK)
819 goto done;
821 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
822 gw_dir->name, "action", "tree", NULL);
823 if (href_tree == NULL) {
824 error = got_error_from_errno("khttp_urlpart");
825 goto done;
827 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
828 href_tree, KATTR__MAX);
829 if (kerr != KCGI_OK)
830 goto done;
831 kerr = khtml_puts(gw_trans->gw_html_req, "tree");
832 if (kerr != KCGI_OK)
833 goto done;
835 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
836 if (kerr != KCGI_OK)
837 goto done;
838 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
839 "dotted_line", KATTR__MAX);
840 if (kerr != KCGI_OK)
841 goto done;
842 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
843 if (kerr != KCGI_OK)
844 goto done;
846 free(href_summary);
847 href_summary = NULL;
848 free(href_briefs);
849 href_briefs = NULL;
850 free(href_commits);
851 href_commits = NULL;
852 free(href_tags);
853 href_tags = NULL;
854 free(href_tree);
855 href_tree = NULL;
857 if (gw_trans->gw_conf->got_max_repos_display == 0)
858 continue;
860 if ((next_disp == gw_trans->gw_conf->got_max_repos_display) ||
861 ((gw_trans->gw_conf->got_max_repos_display > 0) &&
862 (gw_trans->page > 0) &&
863 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
864 prev_disp == gw_trans->repos_total))) {
865 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
866 KATTR_ID, "np_wrapper", KATTR__MAX);
867 if (kerr != KCGI_OK)
868 goto done;
869 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
870 KATTR_ID, "nav_prev", KATTR__MAX);
871 if (kerr != KCGI_OK)
872 goto done;
875 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
876 (gw_trans->page > 0) &&
877 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
878 prev_disp == gw_trans->repos_total)) {
879 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "page",
880 KATTRX_INT, (int64_t)(gw_trans->page - 1), NULL);
881 if (href_prev == NULL) {
882 error = got_error_from_errno("khttp_urlpartx");
883 goto done;
885 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
886 KATTR_HREF, href_prev, KATTR__MAX);
887 if (kerr != KCGI_OK)
888 goto done;
889 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
890 if (kerr != KCGI_OK)
891 goto done;
892 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
893 if (kerr != KCGI_OK)
894 goto done;
897 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
898 if (kerr != KCGI_OK)
899 return gw_kcgi_error(kerr);
901 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
902 next_disp == gw_trans->gw_conf->got_max_repos_display &&
903 dir_c != (gw_trans->page + 1) *
904 gw_trans->gw_conf->got_max_repos_display) {
905 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
906 KATTR_ID, "nav_next", KATTR__MAX);
907 if (kerr != KCGI_OK)
908 goto done;
909 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "page",
910 KATTRX_INT, (int64_t)(gw_trans->page + 1), NULL);
911 if (href_next == NULL) {
912 error = got_error_from_errno("khttp_urlpartx");
913 goto done;
915 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
916 KATTR_HREF, href_next, KATTR__MAX);
917 if (kerr != KCGI_OK)
918 goto done;
919 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
920 if (kerr != KCGI_OK)
921 goto done;
922 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
923 if (kerr != KCGI_OK)
924 goto done;
925 next_disp = 0;
926 break;
929 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
930 (gw_trans->page > 0) &&
931 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
932 prev_disp == gw_trans->repos_total)) {
933 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
934 if (kerr != KCGI_OK)
935 goto done;
937 next_disp++;
939 done:
940 free(href_prev);
941 free(href_next);
942 free(href_summary);
943 free(href_briefs);
944 free(href_commits);
945 free(href_tags);
946 free(href_tree);
947 if (error == NULL && kerr != KCGI_OK)
948 error = gw_kcgi_error(kerr);
949 return error;
952 static const struct got_error *
953 gw_commits(struct gw_trans *gw_trans)
955 const struct got_error *error = NULL;
956 struct gw_header *header = NULL, *n_header = NULL;
957 char *age = NULL, *href_diff = NULL, *href_tree = NULL;
958 char *href_prev = NULL, *href_next = NULL;
959 enum kcgi_err kerr = KCGI_OK;
961 if ((header = gw_init_header()) == NULL)
962 return got_error_from_errno("malloc");
964 if (pledge("stdio rpath proc exec sendfd unveil",
965 NULL) == -1) {
966 error = got_error_from_errno("pledge");
967 goto done;
970 error = gw_apply_unveil(gw_trans->gw_dir->path);
971 if (error)
972 goto done;
974 error = gw_get_header(gw_trans, header,
975 gw_trans->gw_conf->got_max_commits_display);
976 if (error)
977 goto done;
979 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
980 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
981 "commits_line_wrapper", KATTR__MAX);
982 if (kerr != KCGI_OK)
983 goto done;
984 error = gw_gen_commit_header(gw_trans, n_header->commit_id,
985 n_header->refs_str);
986 if (error)
987 goto done;
988 error = gw_gen_author_header(gw_trans, n_header->author);
989 if (error)
990 goto done;
991 error = gw_gen_committer_header(gw_trans, n_header->author);
992 if (error)
993 goto done;
994 error = gw_get_time_str(&age, n_header->committer_time,
995 TM_LONG);
996 if (error)
997 goto done;
998 error = gw_gen_age_header(gw_trans, age ?age : "");
999 if (error)
1000 goto done;
1001 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1002 if (kerr != KCGI_OK)
1003 goto done;
1005 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1006 "dotted_line", KATTR__MAX);
1007 if (kerr != KCGI_OK)
1008 goto done;
1009 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1010 if (kerr != KCGI_OK)
1011 goto done;
1013 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1014 "commit", KATTR__MAX);
1015 if (kerr != KCGI_OK)
1016 goto done;
1017 kerr = khttp_puts(gw_trans->gw_req, n_header->commit_msg);
1018 if (kerr != KCGI_OK)
1019 goto done;
1020 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1021 if (kerr != KCGI_OK)
1022 goto done;
1024 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
1025 gw_trans->repo_name, "action", "diff", "commit",
1026 n_header->commit_id, NULL);
1027 if (href_diff == NULL) {
1028 error = got_error_from_errno("khttp_urlpart");
1029 goto done;
1031 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1032 KATTR_ID, "navs_wrapper", KATTR__MAX);
1033 if (kerr != KCGI_OK)
1034 goto done;
1035 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1036 KATTR_ID, "navs", KATTR__MAX);
1037 if (kerr != KCGI_OK)
1038 goto done;
1039 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1040 KATTR_HREF, href_diff, KATTR__MAX);
1041 if (kerr != KCGI_OK)
1042 goto done;
1043 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1044 if (kerr != KCGI_OK)
1045 goto done;
1046 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1047 if (kerr != KCGI_OK)
1048 goto done;
1050 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1051 if (kerr != KCGI_OK)
1052 goto done;
1054 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
1055 gw_trans->repo_name, "action", "tree", "commit",
1056 n_header->commit_id, NULL);
1057 if (href_tree == NULL) {
1058 error = got_error_from_errno("khttp_urlpart");
1059 goto done;
1061 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1062 KATTR_HREF, href_tree, KATTR__MAX);
1063 if (kerr != KCGI_OK)
1064 goto done;
1065 khtml_puts(gw_trans->gw_html_req, "tree");
1066 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1067 if (kerr != KCGI_OK)
1068 goto done;
1069 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1070 if (kerr != KCGI_OK)
1071 goto done;
1073 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1074 "solid_line", KATTR__MAX);
1075 if (kerr != KCGI_OK)
1076 goto done;
1077 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1078 if (kerr != KCGI_OK)
1079 goto done;
1081 free(age);
1082 age = NULL;
1085 if (gw_trans->next_id || gw_trans->page > 0) {
1086 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1087 KATTR_ID, "np_wrapper", KATTR__MAX);
1088 if (kerr != KCGI_OK)
1089 goto done;
1090 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1091 KATTR_ID, "nav_prev", KATTR__MAX);
1092 if (kerr != KCGI_OK)
1093 goto done;
1096 if (gw_trans->page > 0 && gw_trans->prev_id) {
1097 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1098 KATTRX_STRING, gw_trans->repo_name, "page",
1099 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1100 KATTRX_STRING, "commits", "commit", KATTRX_STRING,
1101 gw_trans->prev_id ? gw_trans->prev_id : "", "prev",
1102 KATTRX_STRING, gw_trans->prev_prev_id ?
1103 gw_trans->prev_prev_id : "", NULL);
1104 if (href_prev == NULL) {
1105 error = got_error_from_errno("khttp_urlpartx");
1106 goto done;
1108 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1109 KATTR_HREF, href_prev, KATTR__MAX);
1110 if (kerr != KCGI_OK)
1111 goto done;
1112 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1113 if (kerr != KCGI_OK)
1114 goto done;
1115 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1116 if (kerr != KCGI_OK)
1117 goto done;
1120 if (gw_trans->next_id || gw_trans->page > 0) {
1121 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1122 if (kerr != KCGI_OK)
1123 return gw_kcgi_error(kerr);
1126 if (gw_trans->next_id) {
1127 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1128 KATTR_ID, "nav_next", KATTR__MAX);
1129 if (kerr != KCGI_OK)
1130 goto done;
1131 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1132 KATTRX_STRING, gw_trans->repo_name, "page",
1133 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1134 KATTRX_STRING, "commits", "commit", KATTRX_STRING,
1135 gw_trans->next_id, "prev", KATTRX_STRING,
1136 gw_trans->next_prev_id ? gw_trans->next_prev_id : "",
1137 "prev_prev", KATTRX_STRING, gw_trans->prev_prev_id ?
1138 gw_trans->prev_prev_id : "", NULL);
1139 if (href_next == NULL) {
1140 error = got_error_from_errno("khttp_urlpartx");
1141 goto done;
1143 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1144 KATTR_HREF, href_next, KATTR__MAX);
1145 if (kerr != KCGI_OK)
1146 goto done;
1147 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1148 if (kerr != KCGI_OK)
1149 goto done;
1150 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1151 if (kerr != KCGI_OK)
1152 goto done;
1155 if (gw_trans->next_id || gw_trans->page > 0) {
1156 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1157 if (kerr != KCGI_OK)
1158 goto done;
1160 done:
1161 gw_free_header(header);
1162 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1163 gw_free_header(n_header);
1164 free(age);
1165 free(href_next);
1166 free(href_prev);
1167 free(href_diff);
1168 free(href_tree);
1169 if (error == NULL && kerr != KCGI_OK)
1170 error = gw_kcgi_error(kerr);
1171 return error;
1174 static const struct got_error *
1175 gw_briefs(struct gw_trans *gw_trans)
1177 const struct got_error *error = NULL;
1178 struct gw_header *header = NULL, *n_header = NULL;
1179 char *age = NULL, *href_diff = NULL, *href_tree = NULL;
1180 char *href_prev = NULL, *href_next = NULL;
1181 char *newline, *smallerthan;
1182 enum kcgi_err kerr = KCGI_OK;
1184 if ((header = gw_init_header()) == NULL)
1185 return got_error_from_errno("malloc");
1187 if (pledge("stdio rpath proc exec sendfd unveil",
1188 NULL) == -1) {
1189 error = got_error_from_errno("pledge");
1190 goto done;
1193 if (gw_trans->action != GW_SUMMARY) {
1194 error = gw_apply_unveil(gw_trans->gw_dir->path);
1195 if (error)
1196 goto done;
1199 if (gw_trans->action == GW_SUMMARY)
1200 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
1201 else
1202 error = gw_get_header(gw_trans, header,
1203 gw_trans->gw_conf->got_max_commits_display);
1204 if (error)
1205 goto done;
1207 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
1208 error = gw_get_time_str(&age, n_header->committer_time,
1209 TM_DIFF);
1210 if (error)
1211 goto done;
1213 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1214 KATTR_ID, "briefs_wrapper", KATTR__MAX);
1215 if (kerr != KCGI_OK)
1216 goto done;
1218 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1219 KATTR_ID, "briefs_age", KATTR__MAX);
1220 if (kerr != KCGI_OK)
1221 goto done;
1222 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
1223 if (kerr != KCGI_OK)
1224 goto done;
1225 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1226 if (kerr != KCGI_OK)
1227 goto done;
1229 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1230 KATTR_ID, "briefs_author", KATTR__MAX);
1231 if (kerr != KCGI_OK)
1232 goto done;
1233 smallerthan = strchr(n_header->author, '<');
1234 if (smallerthan)
1235 *smallerthan = '\0';
1236 kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
1237 if (kerr != KCGI_OK)
1238 goto done;
1239 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1240 if (kerr != KCGI_OK)
1241 goto done;
1243 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
1244 gw_trans->repo_name, "action", "diff", "commit",
1245 n_header->commit_id, NULL);
1246 if (href_diff == NULL) {
1247 error = got_error_from_errno("khttp_urlpart");
1248 goto done;
1250 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1251 KATTR_ID, "briefs_log", KATTR__MAX);
1252 if (kerr != KCGI_OK)
1253 goto done;
1254 newline = strchr(n_header->commit_msg, '\n');
1255 if (newline)
1256 *newline = '\0';
1257 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1258 KATTR_HREF, href_diff, KATTR__MAX);
1259 if (kerr != KCGI_OK)
1260 goto done;
1261 kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
1262 if (kerr != KCGI_OK)
1263 goto done;
1264 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1265 if (kerr != KCGI_OK)
1266 goto done;
1268 if (n_header->refs_str) {
1269 kerr = khtml_puts(gw_trans->gw_html_req, " ");
1270 if (kerr != KCGI_OK)
1271 goto done;
1272 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
1273 KATTR_ID, "refs_str", KATTR__MAX);
1274 if (kerr != KCGI_OK)
1275 goto done;
1276 kerr = khtml_printf(gw_trans->gw_html_req, "(%s)",
1277 n_header->refs_str);
1278 if (kerr != KCGI_OK)
1279 goto done;
1280 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1281 if (kerr != KCGI_OK)
1282 goto done;
1285 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1286 if (kerr != KCGI_OK)
1287 goto done;
1289 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1290 KATTR_ID, "navs_wrapper", KATTR__MAX);
1291 if (kerr != KCGI_OK)
1292 goto done;
1293 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1294 KATTR_ID, "navs", KATTR__MAX);
1295 if (kerr != KCGI_OK)
1296 goto done;
1297 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1298 KATTR_HREF, href_diff, KATTR__MAX);
1299 if (kerr != KCGI_OK)
1300 goto done;
1301 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1302 if (kerr != KCGI_OK)
1303 goto done;
1304 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1305 if (kerr != KCGI_OK)
1306 goto done;
1308 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1309 if (kerr != KCGI_OK)
1310 goto done;
1312 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
1313 gw_trans->repo_name, "action", "tree", "commit",
1314 n_header->commit_id, NULL);
1315 if (href_tree == NULL) {
1316 error = got_error_from_errno("khttp_urlpart");
1317 goto done;
1319 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1320 KATTR_HREF, href_tree, KATTR__MAX);
1321 if (kerr != KCGI_OK)
1322 goto done;
1323 khtml_puts(gw_trans->gw_html_req, "tree");
1324 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1325 if (kerr != KCGI_OK)
1326 goto done;
1327 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1328 if (kerr != KCGI_OK)
1329 goto done;
1331 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1332 KATTR_ID, "dotted_line", KATTR__MAX);
1333 if (kerr != KCGI_OK)
1334 goto done;
1335 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1336 if (kerr != KCGI_OK)
1337 goto done;
1339 free(age);
1340 age = NULL;
1341 free(href_diff);
1342 href_diff = NULL;
1343 free(href_tree);
1344 href_tree = NULL;
1347 if (gw_trans->next_id || gw_trans->page > 0) {
1348 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1349 KATTR_ID, "np_wrapper", KATTR__MAX);
1350 if (kerr != KCGI_OK)
1351 goto done;
1352 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1353 KATTR_ID, "nav_prev", KATTR__MAX);
1354 if (kerr != KCGI_OK)
1355 goto done;
1358 if (gw_trans->page > 0 && gw_trans->prev_id) {
1359 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1360 KATTRX_STRING, gw_trans->repo_name, "page",
1361 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1362 KATTRX_STRING, "briefs", "commit", KATTRX_STRING,
1363 gw_trans->prev_id ? gw_trans->prev_id : "", "prev",
1364 KATTRX_STRING, gw_trans->prev_prev_id ?
1365 gw_trans->prev_prev_id : "", NULL);
1366 if (href_prev == NULL) {
1367 error = got_error_from_errno("khttp_urlpartx");
1368 goto done;
1370 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1371 KATTR_HREF, href_prev, KATTR__MAX);
1372 if (kerr != KCGI_OK)
1373 goto done;
1374 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1375 if (kerr != KCGI_OK)
1376 goto done;
1377 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1378 if (kerr != KCGI_OK)
1379 goto done;
1382 if (gw_trans->next_id || gw_trans->page > 0) {
1383 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1384 if (kerr != KCGI_OK)
1385 return gw_kcgi_error(kerr);
1388 if (gw_trans->next_id) {
1389 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1390 KATTR_ID, "nav_next", KATTR__MAX);
1391 if (kerr != KCGI_OK)
1392 goto done;
1394 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1395 KATTRX_STRING, gw_trans->repo_name, "page",
1396 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1397 KATTRX_STRING, "briefs", "commit", KATTRX_STRING,
1398 gw_trans->next_id, "prev", KATTRX_STRING,
1399 gw_trans->next_prev_id ? gw_trans->next_prev_id : "",
1400 "prev_prev", KATTRX_STRING, gw_trans->prev_id ?
1401 gw_trans->prev_id : "", NULL);
1402 if (href_next == NULL) {
1403 error = got_error_from_errno("khttp_urlpartx");
1404 goto done;
1406 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1407 KATTR_HREF, href_next, KATTR__MAX);
1408 if (kerr != KCGI_OK)
1409 goto done;
1410 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1411 if (kerr != KCGI_OK)
1412 goto done;
1413 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1414 if (kerr != KCGI_OK)
1415 goto done;
1418 if (gw_trans->next_id || gw_trans->page > 0) {
1419 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1420 if (kerr != KCGI_OK)
1421 goto done;
1423 done:
1424 gw_free_header(header);
1425 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1426 gw_free_header(n_header);
1427 free(age);
1428 free(href_next);
1429 free(href_prev);
1430 free(href_diff);
1431 free(href_tree);
1432 if (error == NULL && kerr != KCGI_OK)
1433 error = gw_kcgi_error(kerr);
1434 return error;
1437 static const struct got_error *
1438 gw_summary(struct gw_trans *gw_trans)
1440 const struct got_error *error = NULL;
1441 char *age = NULL;
1442 enum kcgi_err kerr = KCGI_OK;
1444 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1445 return got_error_from_errno("pledge");
1447 error = gw_apply_unveil(gw_trans->gw_dir->path);
1448 if (error)
1449 goto done;
1451 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1452 "summary_wrapper", KATTR__MAX);
1453 if (kerr != KCGI_OK)
1454 return gw_kcgi_error(kerr);
1456 if (gw_trans->gw_conf->got_show_repo_description &&
1457 gw_trans->gw_dir->description != NULL &&
1458 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1459 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1460 KATTR_ID, "description_title", KATTR__MAX);
1461 if (kerr != KCGI_OK)
1462 goto done;
1463 kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1464 if (kerr != KCGI_OK)
1465 goto done;
1466 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1467 if (kerr != KCGI_OK)
1468 goto done;
1469 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1470 KATTR_ID, "description", KATTR__MAX);
1471 if (kerr != KCGI_OK)
1472 goto done;
1473 kerr = khtml_puts(gw_trans->gw_html_req,
1474 gw_trans->gw_dir->description);
1475 if (kerr != KCGI_OK)
1476 goto done;
1477 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1478 if (kerr != KCGI_OK)
1479 goto done;
1482 if (gw_trans->gw_conf->got_show_repo_owner &&
1483 gw_trans->gw_dir->owner != NULL &&
1484 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
1485 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1486 KATTR_ID, "repo_owner_title", KATTR__MAX);
1487 if (kerr != KCGI_OK)
1488 goto done;
1489 kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1490 if (kerr != KCGI_OK)
1491 goto done;
1492 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1493 if (kerr != KCGI_OK)
1494 goto done;
1495 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1496 KATTR_ID, "repo_owner", KATTR__MAX);
1497 if (kerr != KCGI_OK)
1498 goto done;
1499 kerr = khtml_puts(gw_trans->gw_html_req,
1500 gw_trans->gw_dir->owner);
1501 if (kerr != KCGI_OK)
1502 goto done;
1503 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1504 if (kerr != KCGI_OK)
1505 goto done;
1508 if (gw_trans->gw_conf->got_show_repo_age) {
1509 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1510 NULL, TM_LONG);
1511 if (error)
1512 goto done;
1513 if (age != NULL) {
1514 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1515 KATTR_ID, "last_change_title", KATTR__MAX);
1516 if (kerr != KCGI_OK)
1517 goto done;
1518 kerr = khtml_puts(gw_trans->gw_html_req,
1519 "Last Change: ");
1520 if (kerr != KCGI_OK)
1521 goto done;
1522 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1523 if (kerr != KCGI_OK)
1524 goto done;
1525 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1526 KATTR_ID, "last_change", KATTR__MAX);
1527 if (kerr != KCGI_OK)
1528 goto done;
1529 kerr = khtml_puts(gw_trans->gw_html_req, age);
1530 if (kerr != KCGI_OK)
1531 goto done;
1532 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1533 if (kerr != KCGI_OK)
1534 goto done;
1538 if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1539 gw_trans->gw_dir->url != NULL &&
1540 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1541 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1542 KATTR_ID, "cloneurl_title", KATTR__MAX);
1543 if (kerr != KCGI_OK)
1544 goto done;
1545 kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1546 if (kerr != KCGI_OK)
1547 goto done;
1548 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1549 if (kerr != KCGI_OK)
1550 goto done;
1551 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1552 KATTR_ID, "cloneurl", KATTR__MAX);
1553 if (kerr != KCGI_OK)
1554 goto done;
1555 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1556 if (kerr != KCGI_OK)
1557 goto done;
1558 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1559 if (kerr != KCGI_OK)
1560 goto done;
1563 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1564 if (kerr != KCGI_OK)
1565 goto done;
1567 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1568 "briefs_title_wrapper", KATTR__MAX);
1569 if (kerr != KCGI_OK)
1570 goto done;
1571 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1572 "briefs_title", KATTR__MAX);
1573 if (kerr != KCGI_OK)
1574 goto done;
1575 kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1576 if (kerr != KCGI_OK)
1577 goto done;
1578 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1579 if (kerr != KCGI_OK)
1580 goto done;
1581 error = gw_briefs(gw_trans);
1582 if (error)
1583 goto done;
1585 error = gw_tags(gw_trans);
1586 if (error)
1587 goto done;
1589 error = gw_output_repo_heads(gw_trans);
1590 done:
1591 free(age);
1592 if (error == NULL && kerr != KCGI_OK)
1593 error = gw_kcgi_error(kerr);
1594 return error;
1597 static const struct got_error *
1598 gw_tree(struct gw_trans *gw_trans)
1600 const struct got_error *error = NULL;
1601 struct gw_header *header = NULL;
1602 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1603 char *age = NULL;
1604 enum kcgi_err kerr = KCGI_OK;
1606 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1607 return got_error_from_errno("pledge");
1609 if ((header = gw_init_header()) == NULL)
1610 return got_error_from_errno("malloc");
1612 error = gw_apply_unveil(gw_trans->gw_dir->path);
1613 if (error)
1614 goto done;
1616 error = gw_get_header(gw_trans, header, 1);
1617 if (error)
1618 goto done;
1620 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1621 "tree_header_wrapper", KATTR__MAX);
1622 if (kerr != KCGI_OK)
1623 goto done;
1624 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1625 "tree_header", KATTR__MAX);
1626 if (kerr != KCGI_OK)
1627 goto done;
1628 error = gw_gen_tree_header(gw_trans, header->tree_id);
1629 if (error)
1630 goto done;
1631 error = gw_get_time_str(&age, header->committer_time,
1632 TM_LONG);
1633 if (error)
1634 goto done;
1635 error = gw_gen_age_header(gw_trans, age ?age : "");
1636 if (error)
1637 goto done;
1638 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1639 if (error)
1640 goto done;
1641 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1642 if (kerr != KCGI_OK)
1643 goto done;
1644 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1645 "dotted_line", KATTR__MAX);
1646 if (kerr != KCGI_OK)
1647 goto done;
1648 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1649 if (kerr != KCGI_OK)
1650 goto done;
1652 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1653 "tree", KATTR__MAX);
1654 if (kerr != KCGI_OK)
1655 goto done;
1656 error = gw_output_repo_tree(gw_trans);
1657 if (error)
1658 goto done;
1660 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1661 done:
1662 gw_free_header(header);
1663 free(tree_html_disp);
1664 free(tree_html);
1665 free(tree);
1666 free(age);
1667 if (error == NULL && kerr != KCGI_OK)
1668 error = gw_kcgi_error(kerr);
1669 return error;
1672 static const struct got_error *
1673 gw_tags(struct gw_trans *gw_trans)
1675 const struct got_error *error = NULL;
1676 struct gw_header *header = NULL;
1677 char *href_next = NULL, *href_prev = NULL;
1678 enum kcgi_err kerr = KCGI_OK;
1680 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1681 return got_error_from_errno("pledge");
1683 if ((header = gw_init_header()) == NULL)
1684 return got_error_from_errno("malloc");
1686 if (gw_trans->action != GW_SUMMARY) {
1687 error = gw_apply_unveil(gw_trans->gw_dir->path);
1688 if (error)
1689 goto done;
1692 error = gw_get_header(gw_trans, header, 1);
1693 if (error)
1694 goto done;
1696 if (gw_trans->action == GW_SUMMARY) {
1697 gw_trans->next_id = NULL;
1698 error = gw_output_repo_tags(gw_trans, header,
1699 D_MAXSLCOMMDISP, TAGBRIEF);
1700 if (error)
1701 goto done;
1702 } else {
1703 error = gw_output_repo_tags(gw_trans, header,
1704 gw_trans->gw_conf->got_max_commits_display, TAGBRIEF);
1705 if (error)
1706 goto done;
1709 if (gw_trans->next_id || gw_trans->page > 0) {
1710 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1711 KATTR_ID, "np_wrapper", KATTR__MAX);
1712 if (kerr != KCGI_OK)
1713 goto done;
1714 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1715 KATTR_ID, "nav_prev", KATTR__MAX);
1716 if (kerr != KCGI_OK)
1717 goto done;
1720 if (gw_trans->page > 0 && gw_trans->prev_id) {
1721 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1722 KATTRX_STRING, gw_trans->repo_name, "page",
1723 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1724 KATTRX_STRING, "tags", "commit", KATTRX_STRING,
1725 gw_trans->prev_id ? gw_trans->prev_id : "", "prev",
1726 KATTRX_STRING, gw_trans->prev_prev_id ?
1727 gw_trans->prev_prev_id : "", NULL);
1728 if (href_prev == NULL) {
1729 error = got_error_from_errno("khttp_urlpartx");
1730 goto done;
1732 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1733 KATTR_HREF, href_prev, KATTR__MAX);
1734 if (kerr != KCGI_OK)
1735 goto done;
1736 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1737 if (kerr != KCGI_OK)
1738 goto done;
1739 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1740 if (kerr != KCGI_OK)
1741 goto done;
1744 if (gw_trans->next_id || gw_trans->page > 0) {
1745 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1746 if (kerr != KCGI_OK)
1747 return gw_kcgi_error(kerr);
1750 if (gw_trans->next_id) {
1751 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1752 KATTR_ID, "nav_next", KATTR__MAX);
1753 if (kerr != KCGI_OK)
1754 goto done;
1755 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1756 KATTRX_STRING, gw_trans->repo_name, "page",
1757 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1758 KATTRX_STRING, "tags", "commit", KATTRX_STRING,
1759 gw_trans->next_id, "prev", KATTRX_STRING,
1760 gw_trans->next_prev_id ? gw_trans->next_prev_id : "",
1761 "prev_prev", KATTRX_STRING, gw_trans->prev_id ?
1762 gw_trans->prev_id : "", NULL);
1763 if (href_next == NULL) {
1764 error = got_error_from_errno("khttp_urlpartx");
1765 goto done;
1767 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1768 KATTR_HREF, href_next, KATTR__MAX);
1769 if (kerr != KCGI_OK)
1770 goto done;
1771 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1772 if (kerr != KCGI_OK)
1773 goto done;
1774 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1775 if (kerr != KCGI_OK)
1776 goto done;
1779 if (gw_trans->next_id || gw_trans->page > 0) {
1780 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1781 if (kerr != KCGI_OK)
1782 goto done;
1784 done:
1785 gw_free_header(header);
1786 free(href_next);
1787 free(href_prev);
1788 if (error == NULL && kerr != KCGI_OK)
1789 error = gw_kcgi_error(kerr);
1790 return error;
1793 static const struct got_error *
1794 gw_tag(struct gw_trans *gw_trans)
1796 const struct got_error *error = NULL;
1797 struct gw_header *header = NULL;
1798 enum kcgi_err kerr = KCGI_OK;
1800 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1801 return got_error_from_errno("pledge");
1803 if ((header = gw_init_header()) == NULL)
1804 return got_error_from_errno("malloc");
1806 error = gw_apply_unveil(gw_trans->gw_dir->path);
1807 if (error)
1808 goto done;
1810 if (gw_trans->commit_id == NULL) {
1811 error = got_error_msg(GOT_ERR_QUERYSTRING,
1812 "commit required in querystring");
1813 goto done;
1816 error = gw_get_header(gw_trans, header, 1);
1817 if (error)
1818 goto done;
1820 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1821 "tag_header_wrapper", KATTR__MAX);
1822 if (kerr != KCGI_OK)
1823 goto done;
1824 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1825 "tag_header", KATTR__MAX);
1826 if (kerr != KCGI_OK)
1827 goto done;
1828 error = gw_gen_commit_header(gw_trans, header->commit_id,
1829 header->refs_str);
1830 if (error)
1831 goto done;
1832 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1833 if (error)
1834 goto done;
1835 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1836 if (kerr != KCGI_OK)
1837 goto done;
1838 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1839 "dotted_line", KATTR__MAX);
1840 if (kerr != KCGI_OK)
1841 goto done;
1842 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1843 if (kerr != KCGI_OK)
1844 goto done;
1846 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1847 "tree", KATTR__MAX);
1848 if (kerr != KCGI_OK)
1849 goto done;
1851 error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1852 if (error)
1853 goto done;
1855 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1856 done:
1857 gw_free_header(header);
1858 if (error == NULL && kerr != KCGI_OK)
1859 error = gw_kcgi_error(kerr);
1860 return error;
1863 static const struct got_error *
1864 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1866 const struct got_error *error = NULL;
1867 DIR *dt;
1868 char *dir_test;
1869 int opened = 0;
1871 if (asprintf(&dir_test, "%s/%s/%s",
1872 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1873 GOTWEB_GIT_DIR) == -1)
1874 return got_error_from_errno("asprintf");
1876 dt = opendir(dir_test);
1877 if (dt == NULL) {
1878 free(dir_test);
1879 } else {
1880 gw_dir->path = strdup(dir_test);
1881 if (gw_dir->path == NULL) {
1882 opened = 1;
1883 error = got_error_from_errno("strdup");
1884 goto errored;
1886 opened = 1;
1887 goto done;
1890 if (asprintf(&dir_test, "%s/%s/%s",
1891 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1892 GOTWEB_GOT_DIR) == -1) {
1893 dir_test = NULL;
1894 error = got_error_from_errno("asprintf");
1895 goto errored;
1898 dt = opendir(dir_test);
1899 if (dt == NULL)
1900 free(dir_test);
1901 else {
1902 opened = 1;
1903 error = got_error(GOT_ERR_NOT_GIT_REPO);
1904 goto errored;
1907 if (asprintf(&dir_test, "%s/%s",
1908 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1) {
1909 error = got_error_from_errno("asprintf");
1910 dir_test = NULL;
1911 goto errored;
1914 gw_dir->path = strdup(dir_test);
1915 if (gw_dir->path == NULL) {
1916 opened = 1;
1917 error = got_error_from_errno("strdup");
1918 goto errored;
1921 dt = opendir(dir_test);
1922 if (dt == NULL) {
1923 error = got_error_path(gw_dir->name, GOT_ERR_NOT_GIT_REPO);
1924 goto errored;
1925 } else
1926 opened = 1;
1927 done:
1928 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1929 gw_dir->path);
1930 if (error)
1931 goto errored;
1932 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1933 if (error)
1934 goto errored;
1935 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1936 NULL, TM_DIFF);
1937 if (error)
1938 goto errored;
1939 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1940 errored:
1941 free(dir_test);
1942 if (opened)
1943 if (dt && closedir(dt) == -1 && error == NULL)
1944 error = got_error_from_errno("closedir");
1945 return error;
1948 static const struct got_error *
1949 gw_load_got_paths(struct gw_trans *gw_trans)
1951 const struct got_error *error = NULL;
1952 DIR *d;
1953 struct dirent **sd_dent;
1954 struct gw_dir *gw_dir;
1955 struct stat st;
1956 unsigned int d_cnt, d_i;
1958 d = opendir(gw_trans->gw_conf->got_repos_path);
1959 if (d == NULL) {
1960 error = got_error_from_errno2("opendir",
1961 gw_trans->gw_conf->got_repos_path);
1962 return error;
1965 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1966 alphasort);
1967 if (d_cnt == -1) {
1968 error = got_error_from_errno2("scandir",
1969 gw_trans->gw_conf->got_repos_path);
1970 goto done;
1973 for (d_i = 0; d_i < d_cnt; d_i++) {
1974 if (gw_trans->gw_conf->got_max_repos > 0 &&
1975 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1976 break; /* account for parent and self */
1978 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1979 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1980 continue;
1982 error = gw_init_gw_dir(&gw_dir, sd_dent[d_i]->d_name);
1983 if (error)
1984 goto done;
1986 error = gw_load_got_path(gw_trans, gw_dir);
1987 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1988 error = NULL;
1989 continue;
1991 else if (error)
1992 goto done;
1994 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1995 !got_path_dir_is_empty(gw_dir->path)) {
1996 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1997 entry);
1998 gw_trans->repos_total++;
2001 done:
2002 if (d && closedir(d) == -1 && error == NULL)
2003 error = got_error_from_errno("closedir");
2004 return error;
2007 static const struct got_error *
2008 gw_parse_querystring(struct gw_trans *gw_trans)
2010 const struct got_error *error = NULL;
2011 struct kpair *p;
2012 struct gw_query_action *action = NULL;
2013 unsigned int i;
2015 if (gw_trans->gw_req->fieldnmap[0]) {
2016 return got_error(GOT_ERR_QUERYSTRING);
2017 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
2018 /* define gw_trans->repo_path */
2019 gw_trans->repo_name = p->parsed.s;
2021 if (asprintf(&gw_trans->repo_path, "%s/%s",
2022 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
2023 return got_error_from_errno("asprintf");
2025 /* get action and set function */
2026 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION])) {
2027 for (i = 0; i < nitems(gw_query_funcs); i++) {
2028 action = &gw_query_funcs[i];
2029 if (action->func_name == NULL)
2030 continue;
2031 if (strcmp(action->func_name,
2032 p->parsed.s) == 0) {
2033 gw_trans->action = i;
2034 break;
2038 if (gw_trans->action == -1) {
2039 gw_trans->action = GW_ERR;
2040 gw_trans->error = got_error_msg(GOT_ERR_QUERYSTRING,
2041 p != NULL ? "bad action in querystring" :
2042 "no action in querystring");
2043 return error;
2046 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID])) {
2047 if (asprintf(&gw_trans->commit_id, "%s",
2048 p->parsed.s) == -1)
2049 return got_error_from_errno("asprintf");
2052 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
2053 gw_trans->repo_file = p->parsed.s;
2055 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER])) {
2056 if (asprintf(&gw_trans->repo_folder, "%s",
2057 p->parsed.s) == -1)
2058 return got_error_from_errno("asprintf");
2061 if ((p = gw_trans->gw_req->fieldmap[KEY_PREV_ID])) {
2062 if (asprintf(&gw_trans->prev_id, "%s",
2063 p->parsed.s) == -1)
2064 return got_error_from_errno("asprintf");
2067 if ((p = gw_trans->gw_req->fieldmap[KEY_PREV_PREV_ID])) {
2068 if (asprintf(&gw_trans->prev_prev_id, "%s",
2069 p->parsed.s) == -1)
2070 return got_error_from_errno("asprintf");
2073 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
2074 gw_trans->headref = p->parsed.s;
2076 error = gw_init_gw_dir(&gw_trans->gw_dir, gw_trans->repo_name);
2077 if (error)
2078 return error;
2080 gw_trans->error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
2081 } else
2082 gw_trans->action = GW_INDEX;
2084 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
2085 gw_trans->page = p->parsed.i;
2087 return error;
2090 static const struct got_error *
2091 gw_init_gw_dir(struct gw_dir **gw_dir, const char *dir)
2093 const struct got_error *error;
2095 *gw_dir = malloc(sizeof(**gw_dir));
2096 if (*gw_dir == NULL)
2097 return got_error_from_errno("malloc");
2099 if (asprintf(&(*gw_dir)->name, "%s", dir) == -1) {
2100 error = got_error_from_errno("asprintf");
2101 free(*gw_dir);
2102 *gw_dir = NULL;
2103 return error;
2106 return NULL;
2109 static const struct got_error *
2110 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
2112 enum kcgi_err kerr = KCGI_OK;
2114 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
2115 if (kerr != KCGI_OK)
2116 return gw_kcgi_error(kerr);
2117 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
2118 khttps[code]);
2119 if (kerr != KCGI_OK)
2120 return gw_kcgi_error(kerr);
2121 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
2122 kmimetypes[mime]);
2123 if (kerr != KCGI_OK)
2124 return gw_kcgi_error(kerr);
2125 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
2126 "nosniff");
2127 if (kerr != KCGI_OK)
2128 return gw_kcgi_error(kerr);
2129 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
2130 if (kerr != KCGI_OK)
2131 return gw_kcgi_error(kerr);
2132 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
2133 "1; mode=block");
2134 if (kerr != KCGI_OK)
2135 return gw_kcgi_error(kerr);
2137 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
2138 kerr = khttp_head(gw_trans->gw_req,
2139 kresps[KRESP_CONTENT_DISPOSITION],
2140 "attachment; filename=%s", gw_trans->repo_file);
2141 if (kerr != KCGI_OK)
2142 return gw_kcgi_error(kerr);
2145 kerr = khttp_body(gw_trans->gw_req);
2146 return gw_kcgi_error(kerr);
2149 static const struct got_error *
2150 gw_display_index(struct gw_trans *gw_trans)
2152 const struct got_error *error;
2153 enum kcgi_err kerr = KCGI_OK;
2155 /* catch early querystring errors */
2156 if (gw_trans->error)
2157 gw_trans->action = GW_ERR;
2159 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
2160 if (error)
2161 return error;
2163 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
2164 if (kerr != KCGI_OK)
2165 return gw_kcgi_error(kerr);
2167 if (gw_trans->action != GW_BLOB) {
2168 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
2169 gw_query_funcs[gw_trans->action].template);
2170 if (kerr != KCGI_OK) {
2171 khtml_close(gw_trans->gw_html_req);
2172 return gw_kcgi_error(kerr);
2176 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
2179 static const struct got_error *
2180 gw_error(struct gw_trans *gw_trans)
2182 enum kcgi_err kerr = KCGI_OK;
2184 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->error->msg);
2186 return gw_kcgi_error(kerr);
2189 static int
2190 gw_template(size_t key, void *arg)
2192 const struct got_error *error = NULL;
2193 enum kcgi_err kerr = KCGI_OK;
2194 struct gw_trans *gw_trans = arg;
2195 char *ati = NULL, *fic32 = NULL, *fic16 = NULL;
2196 char *swm = NULL, *spt = NULL, *css = NULL, *logo = NULL;
2198 if (asprintf(&ati, "%s%s", gw_trans->gw_conf->got_www_path,
2199 "/apple-touch-icon.png") == -1)
2200 goto err;
2201 if (asprintf(&fic32, "%s%s", gw_trans->gw_conf->got_www_path,
2202 "/favicon-32x32.png") == -1)
2203 goto err;
2204 if (asprintf(&fic16, "%s%s", gw_trans->gw_conf->got_www_path,
2205 "/favicon-16x16.png") == -1)
2206 goto err;
2207 if (asprintf(&swm, "%s%s", gw_trans->gw_conf->got_www_path,
2208 "/site.webmanifest") == -1)
2209 goto err;
2210 if (asprintf(&spt, "%s%s", gw_trans->gw_conf->got_www_path,
2211 "/safari-pinned-tab.svg") == -1)
2212 goto err;
2213 if (asprintf(&css, "%s%s", gw_trans->gw_conf->got_www_path,
2214 "/gotweb.css") == -1)
2215 goto err;
2216 if (asprintf(&logo, "%s%s%s", gw_trans->gw_conf->got_www_path,
2217 gw_trans->gw_conf->got_www_path ? "/" : "",
2218 gw_trans->gw_conf->got_logo) == -1)
2219 goto err;
2221 switch (key) {
2222 case (TEMPL_HEAD):
2223 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2224 KATTR_NAME, "viewport",
2225 KATTR_CONTENT, "initial-scale=.75, user-scalable=yes",
2226 KATTR__MAX);
2227 if (kerr != KCGI_OK)
2228 return 0;
2229 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2230 if (kerr != KCGI_OK)
2231 return 0;
2232 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2233 KATTR_CHARSET, "utf-8",
2234 KATTR__MAX);
2235 if (kerr != KCGI_OK)
2236 return 0;
2237 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2238 if (kerr != KCGI_OK)
2239 return 0;
2240 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2241 KATTR_NAME, "msapplication-TileColor",
2242 KATTR_CONTENT, "#da532c", KATTR__MAX);
2243 if (kerr != KCGI_OK)
2244 return 0;
2245 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2246 if (kerr != KCGI_OK)
2247 return 0;
2248 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2249 KATTR_NAME, "theme-color",
2250 KATTR_CONTENT, "#ffffff", KATTR__MAX);
2251 if (kerr != KCGI_OK)
2252 return 0;
2253 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2254 if (kerr != KCGI_OK)
2255 return 0;
2256 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2257 KATTR_REL, "apple-touch-icon", KATTR_SIZES, "180x180",
2258 KATTR_HREF, ati, KATTR__MAX);
2259 if (kerr != KCGI_OK)
2260 return 0;
2261 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2262 if (kerr != KCGI_OK)
2263 return 0;
2264 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2265 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2266 "32x32", KATTR_HREF, fic32, KATTR__MAX);
2267 if (kerr != KCGI_OK)
2268 return 0;
2269 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2270 if (kerr != KCGI_OK)
2271 return 0;
2272 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2273 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2274 "16x16", KATTR_HREF, fic16, KATTR__MAX);
2275 if (kerr != KCGI_OK)
2276 return 0;
2277 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2278 if (kerr != KCGI_OK)
2279 return 0;
2280 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2281 KATTR_REL, "manifest", KATTR_HREF, swm,
2282 KATTR__MAX);
2283 if (kerr != KCGI_OK)
2284 return 0;
2285 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2286 if (kerr != KCGI_OK)
2287 return 0;
2288 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2289 KATTR_REL, "mask-icon", KATTR_HREF,
2290 spt, KATTR__MAX);
2291 if (kerr != KCGI_OK)
2292 return 0;
2293 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2294 if (kerr != KCGI_OK)
2295 return 0;
2296 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2297 KATTR_REL, "stylesheet", KATTR_TYPE, "text/css",
2298 KATTR_HREF, css, KATTR__MAX);
2299 if (kerr != KCGI_OK)
2300 return 0;
2301 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2302 if (kerr != KCGI_OK)
2303 return 0;
2304 break;
2305 case(TEMPL_HEADER):
2306 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2307 KATTR_ID, "got_link", KATTR__MAX);
2308 if (kerr != KCGI_OK)
2309 return 0;
2310 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2311 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
2312 KATTR_TARGET, "_sotd", KATTR__MAX);
2313 if (kerr != KCGI_OK)
2314 return 0;
2315 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
2316 KATTR_SRC, logo, KATTR__MAX);
2317 if (kerr != KCGI_OK)
2318 return 0;
2319 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2320 if (kerr != KCGI_OK)
2321 return 0;
2322 break;
2323 case (TEMPL_SITEPATH):
2324 error = gw_output_site_link(gw_trans);
2325 if (error)
2326 return 0;
2327 break;
2328 case(TEMPL_TITLE):
2329 if (gw_trans->gw_conf->got_site_name != NULL) {
2330 kerr = khtml_puts(gw_trans->gw_html_req,
2331 gw_trans->gw_conf->got_site_name);
2332 if (kerr != KCGI_OK)
2333 return 0;
2335 break;
2336 case (TEMPL_SEARCH):
2337 break;
2338 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
2339 "search", KATTR__MAX);
2340 if (kerr != KCGI_OK)
2341 return 0;
2342 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
2343 KATTR_METHOD, "POST", KATTR__MAX);
2344 if (kerr != KCGI_OK)
2345 return 0;
2346 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
2347 "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
2348 KATTR_MAXLENGTH, "50", KATTR__MAX);
2349 if (kerr != KCGI_OK)
2350 return 0;
2351 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
2352 KATTR__MAX);
2353 if (kerr != KCGI_OK)
2354 return 0;
2355 kerr = khtml_puts(gw_trans->gw_html_req, "Search");
2356 if (kerr != KCGI_OK)
2357 return 0;
2358 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
2359 if (kerr != KCGI_OK)
2360 return 0;
2361 break;
2362 case(TEMPL_SITEOWNER):
2363 if (gw_trans->gw_conf->got_site_owner != NULL &&
2364 gw_trans->gw_conf->got_show_site_owner) {
2365 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2366 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
2367 if (kerr != KCGI_OK)
2368 return 0;
2369 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2370 KATTR_ID, "site_owner", KATTR__MAX);
2371 if (kerr != KCGI_OK)
2372 return 0;
2373 kerr = khtml_puts(gw_trans->gw_html_req,
2374 gw_trans->gw_conf->got_site_owner);
2375 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2376 if (kerr != KCGI_OK)
2377 return 0;
2379 break;
2380 case(TEMPL_CONTENT):
2381 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
2382 if (error) {
2383 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2384 KATTR_ID, "tmpl_err", KATTR__MAX);
2385 if (kerr != KCGI_OK)
2386 return 0;
2387 kerr = khttp_printf(gw_trans->gw_req, "Error: %s",
2388 error->msg);
2389 if (kerr != KCGI_OK)
2390 return 0;
2391 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2392 if (kerr != KCGI_OK)
2393 return 0;
2395 break;
2396 default:
2397 return 0;
2399 free(ati);
2400 free(fic32);
2401 free(fic16);
2402 free(swm);
2403 free(spt);
2404 free(css);
2405 free(logo);
2406 return 1;
2407 err:
2408 free(ati);
2409 free(fic32);
2410 free(fic16);
2411 free(swm);
2412 free(spt);
2413 free(css);
2414 free(logo);
2415 return 0;
2418 static const struct got_error *
2419 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
2421 const struct got_error *error = NULL;
2422 enum kcgi_err kerr = KCGI_OK;
2424 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2425 KATTR_ID, "header_commit_title", KATTR__MAX);
2426 if (kerr != KCGI_OK)
2427 goto done;
2428 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
2429 if (kerr != KCGI_OK)
2430 goto done;
2431 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2432 if (kerr != KCGI_OK)
2433 goto done;
2434 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2435 KATTR_ID, "header_commit", KATTR__MAX);
2436 if (kerr != KCGI_OK)
2437 goto done;
2438 kerr = khtml_printf(gw_trans->gw_html_req, "%s ", str1);
2439 if (kerr != KCGI_OK)
2440 goto done;
2441 if (str2 != NULL) {
2442 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
2443 KATTR_ID, "refs_str", KATTR__MAX);
2444 if (kerr != KCGI_OK)
2445 goto done;
2446 kerr = khtml_printf(gw_trans->gw_html_req, "(%s)", str2);
2447 if (kerr != KCGI_OK)
2448 goto done;
2449 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2450 if (kerr != KCGI_OK)
2451 goto done;
2453 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2454 done:
2455 if (error == NULL && kerr != KCGI_OK)
2456 error = gw_kcgi_error(kerr);
2457 return error;
2460 static const struct got_error *
2461 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2463 const struct got_error *error = NULL;
2464 enum kcgi_err kerr = KCGI_OK;
2466 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2467 KATTR_ID, "header_diff_title", KATTR__MAX);
2468 if (kerr != KCGI_OK)
2469 goto done;
2470 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2471 if (kerr != KCGI_OK)
2472 goto done;
2473 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2474 if (kerr != KCGI_OK)
2475 goto done;
2476 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2477 KATTR_ID, "header_diff", KATTR__MAX);
2478 if (kerr != KCGI_OK)
2479 goto done;
2480 if (str1 != NULL) {
2481 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2482 if (kerr != KCGI_OK)
2483 goto done;
2485 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2486 if (kerr != KCGI_OK)
2487 goto done;
2488 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2489 if (kerr != KCGI_OK)
2490 goto done;
2491 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2492 done:
2493 if (error == NULL && kerr != KCGI_OK)
2494 error = gw_kcgi_error(kerr);
2495 return error;
2498 static const struct got_error *
2499 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2501 const struct got_error *error = NULL;
2502 enum kcgi_err kerr = KCGI_OK;
2504 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2505 KATTR_ID, "header_age_title", KATTR__MAX);
2506 if (kerr != KCGI_OK)
2507 goto done;
2508 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2509 if (kerr != KCGI_OK)
2510 goto done;
2511 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2512 if (kerr != KCGI_OK)
2513 goto done;
2514 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2515 KATTR_ID, "header_age", KATTR__MAX);
2516 if (kerr != KCGI_OK)
2517 goto done;
2518 kerr = khtml_puts(gw_trans->gw_html_req, str);
2519 if (kerr != KCGI_OK)
2520 goto done;
2521 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2522 done:
2523 if (error == NULL && kerr != KCGI_OK)
2524 error = gw_kcgi_error(kerr);
2525 return error;
2528 static const struct got_error *
2529 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2531 const struct got_error *error = NULL;
2532 enum kcgi_err kerr = KCGI_OK;
2534 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2535 KATTR_ID, "header_author_title", KATTR__MAX);
2536 if (kerr != KCGI_OK)
2537 goto done;
2538 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2539 if (kerr != KCGI_OK)
2540 goto done;
2541 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2542 if (kerr != KCGI_OK)
2543 goto done;
2544 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2545 KATTR_ID, "header_author", KATTR__MAX);
2546 if (kerr != KCGI_OK)
2547 goto done;
2548 kerr = khtml_puts(gw_trans->gw_html_req, str);
2549 if (kerr != KCGI_OK)
2550 goto done;
2551 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2552 done:
2553 if (error == NULL && kerr != KCGI_OK)
2554 error = gw_kcgi_error(kerr);
2555 return error;
2558 static const struct got_error *
2559 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2561 const struct got_error *error = NULL;
2562 enum kcgi_err kerr = KCGI_OK;
2564 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2565 KATTR_ID, "header_committer_title", KATTR__MAX);
2566 if (kerr != KCGI_OK)
2567 goto done;
2568 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2569 if (kerr != KCGI_OK)
2570 goto done;
2571 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2572 if (kerr != KCGI_OK)
2573 goto done;
2574 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2575 KATTR_ID, "header_committer", KATTR__MAX);
2576 if (kerr != KCGI_OK)
2577 goto done;
2578 kerr = khtml_puts(gw_trans->gw_html_req, str);
2579 if (kerr != KCGI_OK)
2580 goto done;
2581 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2582 done:
2583 if (error == NULL && kerr != KCGI_OK)
2584 error = gw_kcgi_error(kerr);
2585 return error;
2588 static const struct got_error *
2589 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2591 const struct got_error *error = NULL;
2592 enum kcgi_err kerr = KCGI_OK;
2594 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2595 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2596 if (kerr != KCGI_OK)
2597 goto done;
2598 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2599 if (kerr != KCGI_OK)
2600 goto done;
2601 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2602 if (kerr != KCGI_OK)
2603 goto done;
2604 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2605 KATTR_ID, "header_commit_msg", KATTR__MAX);
2606 if (kerr != KCGI_OK)
2607 goto done;
2608 kerr = khttp_puts(gw_trans->gw_req, str);
2609 if (kerr != KCGI_OK)
2610 goto done;
2611 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2612 done:
2613 if (error == NULL && kerr != KCGI_OK)
2614 error = gw_kcgi_error(kerr);
2615 return error;
2618 static const struct got_error *
2619 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2621 const struct got_error *error = NULL;
2622 enum kcgi_err kerr = KCGI_OK;
2624 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2625 KATTR_ID, "header_tree_title", KATTR__MAX);
2626 if (kerr != KCGI_OK)
2627 goto done;
2628 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2629 if (kerr != KCGI_OK)
2630 goto done;
2631 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2632 if (kerr != KCGI_OK)
2633 goto done;
2634 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2635 KATTR_ID, "header_tree", KATTR__MAX);
2636 if (kerr != KCGI_OK)
2637 goto done;
2638 kerr = khtml_puts(gw_trans->gw_html_req, str);
2639 if (kerr != KCGI_OK)
2640 goto done;
2641 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2642 done:
2643 if (error == NULL && kerr != KCGI_OK)
2644 error = gw_kcgi_error(kerr);
2645 return error;
2648 static const struct got_error *
2649 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2650 char *dir)
2652 const struct got_error *error = NULL;
2653 FILE *f = NULL;
2654 char *d_file = NULL;
2655 unsigned int len;
2656 size_t n;
2658 *description = NULL;
2659 if (gw_trans->gw_conf->got_show_repo_description == 0)
2660 return NULL;
2662 if (asprintf(&d_file, "%s/description", dir) == -1)
2663 return got_error_from_errno("asprintf");
2665 f = fopen(d_file, "r");
2666 if (f == NULL) {
2667 if (errno == ENOENT || errno == EACCES)
2668 return NULL;
2669 error = got_error_from_errno2("fopen", d_file);
2670 goto done;
2673 if (fseek(f, 0, SEEK_END) == -1) {
2674 error = got_ferror(f, GOT_ERR_IO);
2675 goto done;
2677 len = ftell(f);
2678 if (len == -1) {
2679 error = got_ferror(f, GOT_ERR_IO);
2680 goto done;
2682 if (fseek(f, 0, SEEK_SET) == -1) {
2683 error = got_ferror(f, GOT_ERR_IO);
2684 goto done;
2686 *description = calloc(len + 1, sizeof(**description));
2687 if (*description == NULL) {
2688 error = got_error_from_errno("calloc");
2689 goto done;
2692 n = fread(*description, 1, len, f);
2693 if (n == 0 && ferror(f))
2694 error = got_ferror(f, GOT_ERR_IO);
2695 done:
2696 if (f != NULL && fclose(f) == -1 && error == NULL)
2697 error = got_error_from_errno("fclose");
2698 free(d_file);
2699 return error;
2702 static const struct got_error *
2703 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2705 struct tm tm;
2706 time_t diff_time;
2707 char *years = "years ago", *months = "months ago";
2708 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2709 char *minutes = "minutes ago", *seconds = "seconds ago";
2710 char *now = "right now";
2711 char *s;
2712 char datebuf[29];
2714 *repo_age = NULL;
2716 switch (ref_tm) {
2717 case TM_DIFF:
2718 diff_time = time(NULL) - committer_time;
2719 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2720 if (asprintf(repo_age, "%lld %s",
2721 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2722 return got_error_from_errno("asprintf");
2723 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2724 if (asprintf(repo_age, "%lld %s",
2725 (diff_time / 60 / 60 / 24 / (365 / 12)),
2726 months) == -1)
2727 return got_error_from_errno("asprintf");
2728 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2729 if (asprintf(repo_age, "%lld %s",
2730 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2731 return got_error_from_errno("asprintf");
2732 } else if (diff_time > 60 * 60 * 24 * 2) {
2733 if (asprintf(repo_age, "%lld %s",
2734 (diff_time / 60 / 60 / 24), days) == -1)
2735 return got_error_from_errno("asprintf");
2736 } else if (diff_time > 60 * 60 * 2) {
2737 if (asprintf(repo_age, "%lld %s",
2738 (diff_time / 60 / 60), hours) == -1)
2739 return got_error_from_errno("asprintf");
2740 } else if (diff_time > 60 * 2) {
2741 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2742 minutes) == -1)
2743 return got_error_from_errno("asprintf");
2744 } else if (diff_time > 2) {
2745 if (asprintf(repo_age, "%lld %s", diff_time,
2746 seconds) == -1)
2747 return got_error_from_errno("asprintf");
2748 } else {
2749 if (asprintf(repo_age, "%s", now) == -1)
2750 return got_error_from_errno("asprintf");
2752 break;
2753 case TM_LONG:
2754 if (gmtime_r(&committer_time, &tm) == NULL)
2755 return got_error_from_errno("gmtime_r");
2757 s = asctime_r(&tm, datebuf);
2758 if (s == NULL)
2759 return got_error_from_errno("asctime_r");
2761 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2762 return got_error_from_errno("asprintf");
2763 break;
2765 return NULL;
2768 static const struct got_error *
2769 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2770 const char *refname, int ref_tm)
2772 const struct got_error *error = NULL;
2773 struct got_repository *repo = NULL;
2774 struct got_commit_object *commit = NULL;
2775 struct got_reflist_head refs;
2776 struct got_reflist_entry *re;
2777 time_t committer_time = 0, cmp_time = 0;
2779 *repo_age = NULL;
2780 SIMPLEQ_INIT(&refs);
2782 if (gw_trans->gw_conf->got_show_repo_age == 0)
2783 return NULL;
2785 if (gw_trans->repo)
2786 repo = gw_trans->repo;
2787 else {
2788 error = got_repo_open(&repo, dir, NULL);
2789 if (error)
2790 return error;
2793 error = got_ref_list(&refs, repo, "refs/heads",
2794 got_ref_cmp_by_name, NULL);
2795 if (error)
2796 goto done;
2799 * Find the youngest branch tip in the repository, or the age of
2800 * the a specific branch tip if a name was provided by the caller.
2802 SIMPLEQ_FOREACH(re, &refs, entry) {
2803 struct got_object_id *id = NULL;
2805 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
2806 continue;
2808 error = got_ref_resolve(&id, repo, re->ref);
2809 if (error)
2810 goto done;
2812 error = got_object_open_as_commit(&commit, repo, id);
2813 free(id);
2814 if (error)
2815 goto done;
2817 committer_time =
2818 got_object_commit_get_committer_time(commit);
2819 got_object_commit_close(commit);
2820 if (cmp_time < committer_time)
2821 cmp_time = committer_time;
2823 if (refname)
2824 break;
2827 if (cmp_time != 0) {
2828 committer_time = cmp_time;
2829 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2831 done:
2832 got_ref_list_free(&refs);
2833 if (gw_trans->repo == NULL)
2834 got_repo_close(repo);
2835 return error;
2838 static const struct got_error *
2839 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2841 const struct got_error *error;
2842 FILE *f = NULL;
2843 struct got_object_id *id1 = NULL, *id2 = NULL;
2844 char *label1 = NULL, *label2 = NULL, *line = NULL;
2845 int obj_type;
2846 size_t linesize = 0;
2847 ssize_t linelen;
2848 enum kcgi_err kerr = KCGI_OK;
2850 f = got_opentemp();
2851 if (f == NULL)
2852 return NULL;
2854 if (header->parent_id != NULL &&
2855 strncmp(header->parent_id, "/dev/null", 9) != 0) {
2856 error = got_repo_match_object_id(&id1, &label1,
2857 header->parent_id, GOT_OBJ_TYPE_ANY, 1, gw_trans->repo);
2858 if (error)
2859 goto done;
2862 error = got_repo_match_object_id(&id2, &label2,
2863 header->commit_id, GOT_OBJ_TYPE_ANY, 1, gw_trans->repo);
2864 if (error)
2865 goto done;
2867 error = got_object_get_type(&obj_type, gw_trans->repo, id2);
2868 if (error)
2869 goto done;
2870 switch (obj_type) {
2871 case GOT_OBJ_TYPE_BLOB:
2872 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
2873 gw_trans->repo, f);
2874 break;
2875 case GOT_OBJ_TYPE_TREE:
2876 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
2877 gw_trans->repo, f);
2878 break;
2879 case GOT_OBJ_TYPE_COMMIT:
2880 error = got_diff_objects_as_commits(id1, id2, 3, 0,
2881 gw_trans->repo, f);
2882 break;
2883 default:
2884 error = got_error(GOT_ERR_OBJ_TYPE);
2886 if (error)
2887 goto done;
2889 if (fseek(f, 0, SEEK_SET) == -1) {
2890 error = got_ferror(f, GOT_ERR_IO);
2891 goto done;
2894 while ((linelen = getline(&line, &linesize, f)) != -1) {
2895 error = gw_colordiff_line(gw_trans, line);
2896 if (error)
2897 goto done;
2898 /* XXX: KHTML_PRETTY breaks this */
2899 kerr = khtml_puts(gw_trans->gw_html_req, line);
2900 if (kerr != KCGI_OK)
2901 goto done;
2902 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2903 if (kerr != KCGI_OK)
2904 goto done;
2906 if (linelen == -1 && ferror(f))
2907 error = got_error_from_errno("getline");
2908 done:
2909 if (f && fclose(f) == -1 && error == NULL)
2910 error = got_error_from_errno("fclose");
2911 free(line);
2912 free(label1);
2913 free(label2);
2914 free(id1);
2915 free(id2);
2917 if (error == NULL && kerr != KCGI_OK)
2918 error = gw_kcgi_error(kerr);
2919 return error;
2922 static const struct got_error *
2923 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2925 const struct got_error *error = NULL;
2926 struct got_repository *repo;
2927 const char *gitconfig_owner;
2929 *owner = NULL;
2931 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2932 return NULL;
2934 error = got_repo_open(&repo, dir, NULL);
2935 if (error)
2936 return error;
2937 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2938 if (gitconfig_owner) {
2939 *owner = strdup(gitconfig_owner);
2940 if (*owner == NULL)
2941 error = got_error_from_errno("strdup");
2943 got_repo_close(repo);
2944 return error;
2947 static const struct got_error *
2948 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2950 const struct got_error *error = NULL;
2951 FILE *f;
2952 char *d_file = NULL;
2953 unsigned int len;
2954 size_t n;
2956 *url = NULL;
2958 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2959 return got_error_from_errno("asprintf");
2961 f = fopen(d_file, "r");
2962 if (f == NULL) {
2963 if (errno != ENOENT && errno != EACCES)
2964 error = got_error_from_errno2("fopen", d_file);
2965 goto done;
2968 if (fseek(f, 0, SEEK_END) == -1) {
2969 error = got_ferror(f, GOT_ERR_IO);
2970 goto done;
2972 len = ftell(f);
2973 if (len == -1) {
2974 error = got_ferror(f, GOT_ERR_IO);
2975 goto done;
2977 if (fseek(f, 0, SEEK_SET) == -1) {
2978 error = got_ferror(f, GOT_ERR_IO);
2979 goto done;
2982 *url = calloc(len + 1, sizeof(**url));
2983 if (*url == NULL) {
2984 error = got_error_from_errno("calloc");
2985 goto done;
2988 n = fread(*url, 1, len, f);
2989 if (n == 0 && ferror(f))
2990 error = got_ferror(f, GOT_ERR_IO);
2991 done:
2992 if (f && fclose(f) == -1 && error == NULL)
2993 error = got_error_from_errno("fclose");
2994 free(d_file);
2995 return NULL;
2998 static const struct got_error *
2999 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
3000 int limit, int tag_type)
3002 const struct got_error *error = NULL;
3003 struct got_reflist_head refs;
3004 struct got_reflist_entry *re;
3005 char *age = NULL;
3006 char *id_str = NULL, *newline, *href_commits = NULL;
3007 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
3008 struct got_tag_object *tag = NULL;
3009 enum kcgi_err kerr = KCGI_OK;
3010 int summary_header_displayed = 0, start_tag = 0, chk_next = 0;
3011 int prev_set = 0, tag_count = 0;
3013 SIMPLEQ_INIT(&refs);
3015 error = got_ref_list(&refs, gw_trans->repo, "refs/tags",
3016 got_ref_cmp_tags, gw_trans->repo);
3017 if (error)
3018 goto done;
3020 SIMPLEQ_FOREACH(re, &refs, entry) {
3021 const char *refname;
3022 const char *tagger;
3023 const char *tag_commit;
3024 time_t tagger_time;
3025 struct got_object_id *id;
3026 struct got_commit_object *commit = NULL;
3028 refname = got_ref_get_name(re->ref);
3029 if (strncmp(refname, "refs/tags/", 10) != 0)
3030 continue;
3031 refname += 10;
3033 error = got_ref_resolve(&id, gw_trans->repo, re->ref);
3034 if (error)
3035 goto done;
3037 error = got_object_open_as_tag(&tag, gw_trans->repo, id);
3038 if (error) {
3039 if (error->code != GOT_ERR_OBJ_TYPE) {
3040 free(id);
3041 goto done;
3043 /* "lightweight" tag */
3044 error = got_object_open_as_commit(&commit,
3045 gw_trans->repo, id);
3046 if (error) {
3047 free(id);
3048 goto done;
3050 tagger = got_object_commit_get_committer(commit);
3051 tagger_time =
3052 got_object_commit_get_committer_time(commit);
3053 error = got_object_id_str(&id_str, id);
3054 free(id);
3055 } else {
3056 free(id);
3057 tagger = got_object_tag_get_tagger(tag);
3058 tagger_time = got_object_tag_get_tagger_time(tag);
3059 error = got_object_id_str(&id_str,
3060 got_object_tag_get_object_id(tag));
3062 if (error)
3063 goto done;
3065 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
3066 strlen(id_str)) != 0)
3067 continue;
3069 if (tag_type == TAGBRIEF && gw_trans->commit_id &&
3070 start_tag == 0 && strncmp(id_str, header->commit_id,
3071 strlen(id_str)) != 0) {
3072 continue;
3073 } else {
3074 start_tag = 1;
3077 tag_count++;
3079 if (prev_set == 0 && start_tag == 1) {
3080 gw_trans->next_prev_id = strdup(id_str);
3081 if (gw_trans->next_prev_id == NULL) {
3082 error = got_error_from_errno("strdup");
3083 goto done;
3085 prev_set = 1;
3088 if (chk_next) {
3089 gw_trans->next_id = strdup(id_str);
3090 if (gw_trans->next_id == NULL)
3091 error = got_error_from_errno("strdup");
3092 goto done;
3095 if (commit) {
3096 error = got_object_commit_get_logmsg(&tag_commit0,
3097 commit);
3098 if (error)
3099 goto done;
3100 got_object_commit_close(commit);
3101 } else {
3102 tag_commit0 = strdup(got_object_tag_get_message(tag));
3103 if (tag_commit0 == NULL) {
3104 error = got_error_from_errno("strdup");
3105 goto done;
3109 tag_commit = tag_commit0;
3110 while (*tag_commit == '\n')
3111 tag_commit++;
3113 switch (tag_type) {
3114 case TAGBRIEF:
3115 newline = strchr(tag_commit, '\n');
3116 if (newline)
3117 *newline = '\0';
3119 if (summary_header_displayed == 0) {
3120 kerr = khtml_attr(gw_trans->gw_html_req,
3121 KELEM_DIV, KATTR_ID,
3122 "summary_tags_title_wrapper", KATTR__MAX);
3123 if (kerr != KCGI_OK)
3124 goto done;
3125 kerr = khtml_attr(gw_trans->gw_html_req,
3126 KELEM_DIV, KATTR_ID,
3127 "summary_tags_title", KATTR__MAX);
3128 if (kerr != KCGI_OK)
3129 goto done;
3130 kerr = khtml_puts(gw_trans->gw_html_req,
3131 "Tags");
3132 if (kerr != KCGI_OK)
3133 goto done;
3134 kerr = khtml_closeelem(gw_trans->gw_html_req,
3135 2);
3136 if (kerr != KCGI_OK)
3137 goto done;
3138 kerr = khtml_attr(gw_trans->gw_html_req,
3139 KELEM_DIV, KATTR_ID,
3140 "summary_tags_content", KATTR__MAX);
3141 if (kerr != KCGI_OK)
3142 goto done;
3143 summary_header_displayed = 1;
3146 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3147 KATTR_ID, "tag_wrapper", KATTR__MAX);
3148 if (kerr != KCGI_OK)
3149 goto done;
3150 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3151 KATTR_ID, "tag_age", KATTR__MAX);
3152 if (kerr != KCGI_OK)
3153 goto done;
3154 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
3155 if (error)
3156 goto done;
3157 kerr = khtml_puts(gw_trans->gw_html_req,
3158 age ? age : "");
3159 if (kerr != KCGI_OK)
3160 goto done;
3161 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3162 if (kerr != KCGI_OK)
3163 goto done;
3164 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3165 KATTR_ID, "tag", KATTR__MAX);
3166 if (kerr != KCGI_OK)
3167 goto done;
3168 kerr = khtml_puts(gw_trans->gw_html_req, refname);
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_name", KATTR__MAX);
3176 if (kerr != KCGI_OK)
3177 goto done;
3179 href_tag = khttp_urlpart(NULL, NULL, "gotweb", "path",
3180 gw_trans->repo_name, "action", "tag", "commit",
3181 id_str, NULL);
3182 if (href_tag == NULL) {
3183 error = got_error_from_errno("khttp_urlpart");
3184 goto done;
3186 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3187 KATTR_HREF, href_tag, KATTR__MAX);
3188 if (kerr != KCGI_OK)
3189 goto done;
3190 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
3191 if (kerr != KCGI_OK)
3192 goto done;
3193 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3194 if (kerr != KCGI_OK)
3195 goto done;
3197 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3198 KATTR_ID, "navs_wrapper", KATTR__MAX);
3199 if (kerr != KCGI_OK)
3200 goto done;
3201 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3202 KATTR_ID, "navs", KATTR__MAX);
3203 if (kerr != KCGI_OK)
3204 goto done;
3206 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3207 KATTR_HREF, href_tag, KATTR__MAX);
3208 if (kerr != KCGI_OK)
3209 goto done;
3210 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
3211 if (kerr != KCGI_OK)
3212 goto done;
3213 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3214 if (kerr != KCGI_OK)
3215 goto done;
3217 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3218 if (kerr != KCGI_OK)
3219 goto done;
3221 href_briefs = khttp_urlpart(NULL, NULL, "gotweb",
3222 "path", gw_trans->repo_name, "action", "briefs",
3223 "commit", id_str, NULL);
3224 if (href_briefs == NULL) {
3225 error = got_error_from_errno("khttp_urlpart");
3226 goto done;
3228 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3229 KATTR_HREF, href_briefs, KATTR__MAX);
3230 if (kerr != KCGI_OK)
3231 goto done;
3232 kerr = khtml_puts(gw_trans->gw_html_req,
3233 "commit briefs");
3234 if (kerr != KCGI_OK)
3235 goto done;
3236 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3237 if (kerr != KCGI_OK)
3238 goto done;
3240 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3241 if (kerr != KCGI_OK)
3242 goto done;
3244 href_commits = khttp_urlpart(NULL, NULL, "gotweb",
3245 "path", gw_trans->repo_name, "action", "commits",
3246 "commit", id_str, NULL);
3247 if (href_commits == NULL) {
3248 error = got_error_from_errno("khttp_urlpart");
3249 goto done;
3251 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3252 KATTR_HREF, href_commits, KATTR__MAX);
3253 if (kerr != KCGI_OK)
3254 goto done;
3255 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
3256 if (kerr != KCGI_OK)
3257 goto done;
3258 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3259 if (kerr != KCGI_OK)
3260 goto done;
3262 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3263 KATTR_ID, "dotted_line", KATTR__MAX);
3264 if (kerr != KCGI_OK)
3265 goto done;
3266 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3267 if (kerr != KCGI_OK)
3268 goto done;
3269 break;
3270 case TAGFULL:
3271 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3272 KATTR_ID, "tag_info_date_title", KATTR__MAX);
3273 if (kerr != KCGI_OK)
3274 goto done;
3275 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
3276 if (kerr != KCGI_OK)
3277 goto done;
3278 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3279 if (kerr != KCGI_OK)
3280 goto done;
3281 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3282 KATTR_ID, "tag_info_date", KATTR__MAX);
3283 if (kerr != KCGI_OK)
3284 goto done;
3285 error = gw_get_time_str(&age, tagger_time, TM_LONG);
3286 if (error)
3287 goto done;
3288 kerr = khtml_puts(gw_trans->gw_html_req,
3289 age ? age : "");
3290 if (kerr != KCGI_OK)
3291 goto done;
3292 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3293 if (kerr != KCGI_OK)
3294 goto done;
3296 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3297 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
3298 if (kerr != KCGI_OK)
3299 goto done;
3300 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
3301 if (kerr != KCGI_OK)
3302 goto done;
3303 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3304 if (kerr != KCGI_OK)
3305 goto done;
3306 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3307 KATTR_ID, "tag_info_date", 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;
3317 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3318 KATTR_ID, "tag_info", KATTR__MAX);
3319 if (kerr != KCGI_OK)
3320 goto done;
3321 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
3322 if (kerr != KCGI_OK)
3323 goto done;
3324 break;
3325 default:
3326 break;
3328 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3329 if (kerr != KCGI_OK)
3330 goto done;
3332 if (limit && --limit == 0)
3333 chk_next = 1;
3335 if (tag)
3336 got_object_tag_close(tag);
3337 tag = NULL;
3338 free(id_str);
3339 id_str = NULL;
3340 free(age);
3341 age = NULL;
3342 free(tag_commit0);
3343 tag_commit0 = NULL;
3344 free(href_tag);
3345 href_tag = NULL;
3346 free(href_briefs);
3347 href_briefs = NULL;
3348 free(href_commits);
3349 href_commits = NULL;
3351 if (tag_count == 0) {
3352 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3353 "summary_tags_title_wrapper", KATTR__MAX);
3354 if (kerr != KCGI_OK)
3355 goto done;
3356 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3357 "summary_tags_title", KATTR__MAX);
3358 if (kerr != KCGI_OK)
3359 goto done;
3360 kerr = khtml_puts(gw_trans->gw_html_req, "Tags");
3361 if (kerr != KCGI_OK)
3362 goto done;
3363 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3364 if (kerr != KCGI_OK)
3365 goto done;
3366 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3367 "summary_tags_content", KATTR__MAX);
3368 if (kerr != KCGI_OK)
3369 goto done;
3370 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3371 "tags_info", KATTR__MAX);
3372 if (kerr != KCGI_OK)
3373 goto done;
3374 kerr = khttp_puts(gw_trans->gw_req,
3375 "There are no tags for this repo.");
3376 if (kerr != KCGI_OK)
3377 goto done;
3378 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3380 done:
3381 if (tag)
3382 got_object_tag_close(tag);
3383 free(id_str);
3384 free(age);
3385 free(tag_commit0);
3386 free(href_tag);
3387 free(href_briefs);
3388 free(href_commits);
3389 got_ref_list_free(&refs);
3390 if (error == NULL && kerr != KCGI_OK)
3391 error = gw_kcgi_error(kerr);
3392 return error;
3395 static void
3396 gw_free_header(struct gw_header *header)
3398 free(header->path);
3399 free(header->author);
3400 free(header->committer);
3401 free(header->refs_str);
3402 free(header->commit_id);
3403 free(header->parent_id);
3404 free(header->tree_id);
3405 free(header->commit_msg);
3408 static struct gw_header *
3409 gw_init_header()
3411 struct gw_header *header;
3413 header = malloc(sizeof(*header));
3414 if (header == NULL)
3415 return NULL;
3417 header->path = NULL;
3418 SIMPLEQ_INIT(&header->refs);
3420 header->refs_str = NULL;
3421 header->commit_id = NULL;
3422 header->committer = NULL;
3423 header->author = NULL;
3424 header->parent_id = NULL;
3425 header->tree_id = NULL;
3426 header->commit_msg = NULL;
3428 return header;
3431 static const struct got_error *
3432 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
3433 int limit, struct got_object_id *id)
3435 const struct got_error *error = NULL;
3436 struct got_commit_graph *graph = NULL;
3437 struct got_commit_object *commit = NULL;
3438 int chk_next = 0, chk_multi = 0, prev_set = 0;
3440 error = got_commit_graph_open(&graph, header->path, 0);
3441 if (error)
3442 return error;
3444 error = got_commit_graph_iter_start(graph, id, gw_trans->repo, NULL,
3445 NULL);
3446 if (error)
3447 goto done;
3449 for (;;) {
3450 error = got_commit_graph_iter_next(&id, graph, gw_trans->repo,
3451 NULL, NULL);
3452 if (error) {
3453 if (error->code == GOT_ERR_ITER_COMPLETED)
3454 error = NULL;
3455 goto done;
3457 if (id == NULL)
3458 goto done;
3460 error = got_object_open_as_commit(&commit, gw_trans->repo, id);
3461 if (error)
3462 goto done;
3463 if (limit == 1 && chk_multi == 0 &&
3464 gw_trans->gw_conf->got_max_commits_display != 1) {
3465 error = gw_get_commit(gw_trans, header, commit, id);
3466 if (error)
3467 goto done;
3468 } else {
3469 chk_multi = 1;
3470 struct gw_header *n_header = NULL;
3471 if ((n_header = gw_init_header()) == NULL) {
3472 error = got_error_from_errno("malloc");
3473 goto done;
3475 error = got_ref_list(&n_header->refs, gw_trans->repo,
3476 NULL, got_ref_cmp_by_name, NULL);
3477 if (error)
3478 goto done;
3480 error = gw_get_commit(gw_trans, n_header, commit, id);
3481 if (error)
3482 goto done;
3483 got_ref_list_free(&n_header->refs);
3486 * we have a commit_id now, so copy it to next_prev_id
3487 * for navigation through gw_briefs and gw_commits
3489 if (gw_trans->next_prev_id == NULL && prev_set == 0 &&
3490 (gw_trans->action == GW_BRIEFS ||
3491 gw_trans->action == GW_COMMITS ||
3492 gw_trans->action == GW_SUMMARY)) {
3493 prev_set = 1;
3494 gw_trans->next_prev_id =
3495 strdup(n_header->commit_id);
3496 if (gw_trans->next_prev_id == NULL) {
3497 error = got_error_from_errno("strdup");
3498 goto done;
3503 * check for one more commit before breaking,
3504 * so we know whether to navicate through gw_briefs
3505 * gw_commits and gw_summary
3507 if (chk_next && (gw_trans->action == GW_BRIEFS||
3508 gw_trans->action == GW_COMMITS ||
3509 gw_trans->action == GW_SUMMARY)) {
3510 gw_trans->next_id = strdup(n_header->commit_id);
3511 if (gw_trans->next_id == NULL)
3512 error = got_error_from_errno("strdup");
3513 goto done;
3516 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3517 entry);
3519 if (error || (limit && --limit == 0)) {
3520 if (chk_multi == 0)
3521 break;
3522 chk_next = 1;
3525 done:
3526 if (commit != NULL)
3527 got_object_commit_close(commit);
3528 if (graph)
3529 got_commit_graph_close(graph);
3530 return error;
3533 static const struct got_error *
3534 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header,
3535 struct got_commit_object *commit, struct got_object_id *id)
3537 const struct got_error *error = NULL;
3538 struct got_reflist_entry *re;
3539 struct got_object_id *id2 = NULL;
3540 struct got_object_qid *parent_id;
3541 char *commit_msg = NULL, *commit_msg0;
3543 /*print commit*/
3544 SIMPLEQ_FOREACH(re, &header->refs, entry) {
3545 char *s;
3546 const char *name;
3547 struct got_tag_object *tag = NULL;
3548 struct got_object_id *ref_id;
3549 int cmp;
3551 if (got_ref_is_symbolic(re->ref))
3552 continue;
3554 name = got_ref_get_name(re->ref);
3555 if (strncmp(name, "refs/", 5) == 0)
3556 name += 5;
3557 if (strncmp(name, "got/", 4) == 0)
3558 continue;
3559 if (strncmp(name, "heads/", 6) == 0)
3560 name += 6;
3561 if (strncmp(name, "remotes/", 8) == 0) {
3562 name += 8;
3563 s = strstr(name, "/" GOT_REF_HEAD);
3564 if (s != NULL && s[strlen(s)] == '\0')
3565 continue;
3567 error = got_ref_resolve(&ref_id, gw_trans->repo, re->ref);
3568 if (error)
3569 return error;
3570 if (strncmp(name, "tags/", 5) == 0) {
3571 error = got_object_open_as_tag(&tag, gw_trans->repo,
3572 ref_id);
3573 if (error) {
3574 if (error->code != GOT_ERR_OBJ_TYPE) {
3575 free(ref_id);
3576 continue;
3579 * Ref points at something other
3580 * than a tag.
3582 error = NULL;
3583 tag = NULL;
3586 cmp = got_object_id_cmp(tag ?
3587 got_object_tag_get_object_id(tag) : ref_id, id);
3588 free(ref_id);
3589 if (tag)
3590 got_object_tag_close(tag);
3591 if (cmp != 0)
3592 continue;
3593 s = header->refs_str;
3594 if (asprintf(&header->refs_str, "%s%s%s", s ? s : "",
3595 s ? ", " : "", name) == -1) {
3596 error = got_error_from_errno("asprintf");
3597 free(s);
3598 header->refs_str = NULL;
3599 return error;
3601 free(s);
3604 error = got_object_id_str(&header->commit_id, id);
3605 if (error)
3606 return error;
3608 error = got_object_id_str(&header->tree_id,
3609 got_object_commit_get_tree_id(commit));
3610 if (error)
3611 return error;
3613 if (gw_trans->action == GW_DIFF) {
3614 parent_id = SIMPLEQ_FIRST(
3615 got_object_commit_get_parent_ids(commit));
3616 if (parent_id != NULL) {
3617 id2 = got_object_id_dup(parent_id->id);
3618 free (parent_id);
3619 error = got_object_id_str(&header->parent_id, id2);
3620 if (error)
3621 return error;
3622 free(id2);
3623 } else {
3624 header->parent_id = strdup("/dev/null");
3625 if (header->parent_id == NULL) {
3626 error = got_error_from_errno("strdup");
3627 return error;
3632 header->committer_time =
3633 got_object_commit_get_committer_time(commit);
3635 header->author =
3636 strdup(got_object_commit_get_author(commit));
3637 if (header->author == NULL) {
3638 error = got_error_from_errno("strdup");
3639 return error;
3641 header->committer =
3642 strdup(got_object_commit_get_committer(commit));
3643 if (header->committer == NULL) {
3644 error = got_error_from_errno("strdup");
3645 return error;
3647 error = got_object_commit_get_logmsg(&commit_msg0, commit);
3648 if (error)
3649 return error;
3651 commit_msg = commit_msg0;
3652 while (*commit_msg == '\n')
3653 commit_msg++;
3655 header->commit_msg = strdup(commit_msg);
3656 if (header->commit_msg == NULL)
3657 error = got_error_from_errno("strdup");
3658 free(commit_msg0);
3659 return error;
3662 static const struct got_error *
3663 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3665 const struct got_error *error = NULL;
3666 char *in_repo_path = NULL;
3667 struct got_object_id *id = NULL;
3669 error = got_repo_open(&gw_trans->repo, gw_trans->repo_path, NULL);
3670 if (error)
3671 return error;
3673 if (gw_trans->commit_id == NULL) {
3674 struct got_reference *head_ref;
3675 error = got_ref_open(&head_ref, gw_trans->repo,
3676 gw_trans->headref, 0);
3677 if (error)
3678 return error;
3680 error = got_ref_resolve(&id, gw_trans->repo, head_ref);
3681 got_ref_close(head_ref);
3682 if (error)
3683 return error;
3684 } else {
3685 struct got_reference *ref;
3687 error = got_ref_open(&ref, gw_trans->repo,
3688 gw_trans->commit_id, 0);
3689 if (error == NULL) {
3690 int obj_type;
3691 error = got_ref_resolve(&id, gw_trans->repo, ref);
3692 got_ref_close(ref);
3693 if (error)
3694 return error;
3695 error = got_object_get_type(&obj_type, gw_trans->repo,
3696 id);
3697 if (error)
3698 goto done;
3699 if (obj_type == GOT_OBJ_TYPE_TAG) {
3700 struct got_tag_object *tag;
3701 error = got_object_open_as_tag(&tag,
3702 gw_trans->repo, id);
3703 if (error)
3704 goto done;
3705 if (got_object_tag_get_object_type(tag) !=
3706 GOT_OBJ_TYPE_COMMIT) {
3707 got_object_tag_close(tag);
3708 error = got_error(GOT_ERR_OBJ_TYPE);
3709 goto done;
3711 free(id);
3712 id = got_object_id_dup(
3713 got_object_tag_get_object_id(tag));
3714 if (id == NULL)
3715 error = got_error_from_errno(
3716 "got_object_id_dup");
3717 got_object_tag_close(tag);
3718 if (error)
3719 goto done;
3720 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3721 error = got_error(GOT_ERR_OBJ_TYPE);
3722 goto done;
3725 error = got_repo_match_object_id_prefix(&id,
3726 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT,
3727 gw_trans->repo);
3728 if (error)
3729 goto done;
3732 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
3733 gw_trans->repo_path, 1);
3734 if (error)
3735 goto done;
3737 if (in_repo_path) {
3738 header->path = strdup(in_repo_path);
3739 if (header->path == NULL) {
3740 error = got_error_from_errno("strdup");
3741 goto done;
3745 error = got_ref_list(&header->refs, gw_trans->repo, NULL,
3746 got_ref_cmp_by_name, NULL);
3747 if (error)
3748 goto done;
3750 error = gw_get_commits(gw_trans, header, limit, id);
3751 done:
3752 got_ref_list_free(&header->refs);
3753 free(id);
3754 free(in_repo_path);
3755 return error;
3758 struct blame_line {
3759 int annotated;
3760 char *id_str;
3761 char *committer;
3762 char datebuf[11]; /* YYYY-MM-DD + NUL */
3765 struct gw_blame_cb_args {
3766 struct blame_line *lines;
3767 int nlines;
3768 int nlines_prec;
3769 int lineno_cur;
3770 off_t *line_offsets;
3771 FILE *f;
3772 struct got_repository *repo;
3773 struct gw_trans *gw_trans;
3776 static const struct got_error *
3777 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3779 const struct got_error *err = NULL;
3780 struct gw_blame_cb_args *a = arg;
3781 struct blame_line *bline;
3782 char *line = NULL;
3783 size_t linesize = 0;
3784 struct got_commit_object *commit = NULL;
3785 off_t offset;
3786 struct tm tm;
3787 time_t committer_time;
3788 enum kcgi_err kerr = KCGI_OK;
3790 if (nlines != a->nlines ||
3791 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3792 return got_error(GOT_ERR_RANGE);
3794 if (lineno == -1)
3795 return NULL; /* no change in this commit */
3797 /* Annotate this line. */
3798 bline = &a->lines[lineno - 1];
3799 if (bline->annotated)
3800 return NULL;
3801 err = got_object_id_str(&bline->id_str, id);
3802 if (err)
3803 return err;
3805 err = got_object_open_as_commit(&commit, a->repo, id);
3806 if (err)
3807 goto done;
3809 bline->committer = strdup(got_object_commit_get_committer(commit));
3810 if (bline->committer == NULL) {
3811 err = got_error_from_errno("strdup");
3812 goto done;
3815 committer_time = got_object_commit_get_committer_time(commit);
3816 if (localtime_r(&committer_time, &tm) == NULL)
3817 return got_error_from_errno("localtime_r");
3818 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3819 &tm) >= sizeof(bline->datebuf)) {
3820 err = got_error(GOT_ERR_NO_SPACE);
3821 goto done;
3823 bline->annotated = 1;
3825 /* Print lines annotated so far. */
3826 bline = &a->lines[a->lineno_cur - 1];
3827 if (!bline->annotated)
3828 goto done;
3830 offset = a->line_offsets[a->lineno_cur - 1];
3831 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3832 err = got_error_from_errno("fseeko");
3833 goto done;
3836 while (bline->annotated) {
3837 char *smallerthan, *at, *nl, *committer;
3838 char *href_diff = NULL;
3839 size_t len;
3841 if (getline(&line, &linesize, a->f) == -1) {
3842 if (ferror(a->f))
3843 err = got_error_from_errno("getline");
3844 break;
3847 committer = bline->committer;
3848 smallerthan = strchr(committer, '<');
3849 if (smallerthan && smallerthan[1] != '\0')
3850 committer = smallerthan + 1;
3851 at = strchr(committer, '@');
3852 if (at)
3853 *at = '\0';
3854 len = strlen(committer);
3855 if (len >= 9)
3856 committer[8] = '\0';
3858 nl = strchr(line, '\n');
3859 if (nl)
3860 *nl = '\0';
3862 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3863 "blame_wrapper", KATTR__MAX);
3864 if (kerr != KCGI_OK)
3865 goto err;
3866 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3867 "blame_number", KATTR__MAX);
3868 if (kerr != KCGI_OK)
3869 goto err;
3870 kerr = khtml_printf(a->gw_trans->gw_html_req, "%.*d",
3871 a->nlines_prec, a->lineno_cur);
3872 if (kerr != KCGI_OK)
3873 goto err;
3874 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3875 if (kerr != KCGI_OK)
3876 goto err;
3878 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3879 "blame_hash", KATTR__MAX);
3880 if (kerr != KCGI_OK)
3881 goto err;
3883 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
3884 a->gw_trans->repo_name, "action", "diff", "commit",
3885 bline->id_str, NULL);
3886 if (href_diff == NULL) {
3887 err = got_error_from_errno("khttp_urlpart");
3888 goto done;
3890 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3891 KATTR_HREF, href_diff, KATTR__MAX);
3892 if (kerr != KCGI_OK)
3893 goto done;
3894 kerr = khtml_printf(a->gw_trans->gw_html_req, "%.8s",
3895 bline->id_str);
3896 if (kerr != KCGI_OK)
3897 goto err;
3898 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3899 if (kerr != KCGI_OK)
3900 goto err;
3902 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3903 "blame_date", KATTR__MAX);
3904 if (kerr != KCGI_OK)
3905 goto err;
3906 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3907 if (kerr != KCGI_OK)
3908 goto err;
3909 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3910 if (kerr != KCGI_OK)
3911 goto err;
3913 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3914 "blame_author", KATTR__MAX);
3915 if (kerr != KCGI_OK)
3916 goto err;
3917 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
3918 if (kerr != KCGI_OK)
3919 goto err;
3920 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3921 if (kerr != KCGI_OK)
3922 goto err;
3924 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3925 "blame_code", KATTR__MAX);
3926 if (kerr != KCGI_OK)
3927 goto err;
3928 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
3929 if (kerr != KCGI_OK)
3930 goto err;
3931 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3932 if (kerr != KCGI_OK)
3933 goto err;
3935 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3936 if (kerr != KCGI_OK)
3937 goto err;
3939 a->lineno_cur++;
3940 bline = &a->lines[a->lineno_cur - 1];
3941 err:
3942 free(href_diff);
3944 done:
3945 if (commit)
3946 got_object_commit_close(commit);
3947 free(line);
3948 if (err == NULL && kerr != KCGI_OK)
3949 err = gw_kcgi_error(kerr);
3950 return err;
3953 static const struct got_error *
3954 gw_output_file_blame(struct gw_trans *gw_trans)
3956 const struct got_error *error = NULL;
3957 struct got_object_id *obj_id = NULL;
3958 struct got_object_id *commit_id = NULL;
3959 struct got_blob_object *blob = NULL;
3960 char *path = NULL, *in_repo_path = NULL;
3961 struct gw_blame_cb_args bca;
3962 int i, obj_type;
3963 size_t filesize;
3965 if (asprintf(&path, "%s%s%s",
3966 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3967 gw_trans->repo_folder ? "/" : "",
3968 gw_trans->repo_file) == -1) {
3969 error = got_error_from_errno("asprintf");
3970 goto done;
3973 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path, 1);
3974 if (error)
3975 goto done;
3977 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
3978 GOT_OBJ_TYPE_COMMIT, 1, gw_trans->repo);
3979 if (error)
3980 goto done;
3982 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
3983 in_repo_path);
3984 if (error)
3985 goto done;
3987 if (obj_id == NULL) {
3988 error = got_error(GOT_ERR_NO_OBJ);
3989 goto done;
3992 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
3993 if (error)
3994 goto done;
3996 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3997 error = got_error(GOT_ERR_OBJ_TYPE);
3998 goto done;
4001 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
4002 if (error)
4003 goto done;
4005 bca.f = got_opentemp();
4006 if (bca.f == NULL) {
4007 error = got_error_from_errno("got_opentemp");
4008 goto done;
4010 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4011 &bca.line_offsets, bca.f, blob);
4012 if (error || bca.nlines == 0)
4013 goto done;
4015 /* Don't include \n at EOF in the blame line count. */
4016 if (bca.line_offsets[bca.nlines - 1] == filesize)
4017 bca.nlines--;
4019 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4020 if (bca.lines == NULL) {
4021 error = got_error_from_errno("calloc");
4022 goto done;
4024 bca.lineno_cur = 1;
4025 bca.nlines_prec = 0;
4026 i = bca.nlines;
4027 while (i > 0) {
4028 i /= 10;
4029 bca.nlines_prec++;
4031 bca.repo = gw_trans->repo;
4032 bca.gw_trans = gw_trans;
4034 error = got_blame(in_repo_path, commit_id, gw_trans->repo, gw_blame_cb,
4035 &bca, NULL, NULL);
4036 done:
4037 free(in_repo_path);
4038 free(commit_id);
4039 free(obj_id);
4040 free(path);
4042 if (blob) {
4043 free(bca.line_offsets);
4044 for (i = 0; i < bca.nlines; i++) {
4045 struct blame_line *bline = &bca.lines[i];
4046 free(bline->id_str);
4047 free(bline->committer);
4049 free(bca.lines);
4050 if (bca.f && fclose(bca.f) == EOF && error == NULL)
4051 error = got_error_from_errno("fclose");
4053 if (blob)
4054 got_object_blob_close(blob);
4055 return error;
4058 static const struct got_error *
4059 gw_output_blob_buf(struct gw_trans *gw_trans)
4061 const struct got_error *error = NULL;
4062 struct got_object_id *obj_id = NULL;
4063 struct got_object_id *commit_id = NULL;
4064 struct got_blob_object *blob = NULL;
4065 char *path = NULL, *in_repo_path = NULL;
4066 int obj_type, set_mime = 0;
4067 size_t len, hdrlen;
4068 const uint8_t *buf;
4069 enum kcgi_err kerr = KCGI_OK;
4071 if (asprintf(&path, "%s%s%s",
4072 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4073 gw_trans->repo_folder ? "/" : "",
4074 gw_trans->repo_file) == -1) {
4075 error = got_error_from_errno("asprintf");
4076 goto done;
4079 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path, 1);
4080 if (error)
4081 goto done;
4083 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
4084 GOT_OBJ_TYPE_COMMIT, 1, gw_trans->repo);
4085 if (error)
4086 goto done;
4088 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
4089 in_repo_path);
4090 if (error)
4091 goto done;
4093 if (obj_id == NULL) {
4094 error = got_error(GOT_ERR_NO_OBJ);
4095 goto done;
4098 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
4099 if (error)
4100 goto done;
4102 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4103 error = got_error(GOT_ERR_OBJ_TYPE);
4104 goto done;
4107 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
4108 if (error)
4109 goto done;
4111 hdrlen = got_object_blob_get_hdrlen(blob);
4112 do {
4113 error = got_object_blob_read_block(&len, blob);
4114 if (error)
4115 goto done;
4116 buf = got_object_blob_get_read_buf(blob);
4119 * Skip blob object header first time around,
4120 * which also contains a zero byte.
4122 buf += hdrlen;
4123 if (set_mime == 0) {
4124 if (isbinary(buf, len - hdrlen))
4125 gw_trans->mime = KMIME_APP_OCTET_STREAM;
4126 else
4127 gw_trans->mime = KMIME_TEXT_PLAIN;
4128 set_mime = 1;
4129 error = gw_display_index(gw_trans);
4130 if (error)
4131 goto done;
4133 kerr = khttp_write(gw_trans->gw_req, buf, len - hdrlen);
4134 if (kerr != KCGI_OK)
4135 goto done;
4136 hdrlen = 0;
4137 } while (len != 0);
4138 done:
4139 free(in_repo_path);
4140 free(commit_id);
4141 free(obj_id);
4142 free(path);
4143 if (blob)
4144 got_object_blob_close(blob);
4145 if (error == NULL && kerr != KCGI_OK)
4146 error = gw_kcgi_error(kerr);
4147 return error;
4150 static const struct got_error *
4151 gw_output_repo_tree(struct gw_trans *gw_trans)
4153 const struct got_error *error = NULL;
4154 struct got_object_id *tree_id = NULL, *commit_id = NULL;
4155 struct got_tree_object *tree = NULL;
4156 char *path = NULL, *in_repo_path = NULL;
4157 char *id_str = NULL;
4158 char *build_folder = NULL;
4159 char *href_blob = NULL, *href_blame = NULL;
4160 const char *class = NULL;
4161 int nentries, i, class_flip = 0;
4162 enum kcgi_err kerr = KCGI_OK;
4164 if (gw_trans->repo_folder != NULL) {
4165 path = strdup(gw_trans->repo_folder);
4166 if (path == NULL) {
4167 error = got_error_from_errno("strdup");
4168 goto done;
4170 } else {
4171 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
4172 gw_trans->repo_path, 1);
4173 if (error)
4174 goto done;
4175 free(path);
4176 path = in_repo_path;
4179 if (gw_trans->commit_id == NULL) {
4180 struct got_reference *head_ref;
4181 error = got_ref_open(&head_ref, gw_trans->repo,
4182 gw_trans->headref, 0);
4183 if (error)
4184 goto done;
4185 error = got_ref_resolve(&commit_id, gw_trans->repo, head_ref);
4186 if (error)
4187 goto done;
4188 got_ref_close(head_ref);
4190 * gw_trans->commit_id was not parsed from the querystring
4191 * we hit this code path from gw_index, where we don't know the
4192 * commit values for the tree link yet, so set
4193 * gw_trans->commit_id here to continue further into the tree
4195 error = got_object_id_str(&gw_trans->commit_id, commit_id);
4196 if (error)
4197 goto done;
4199 } else {
4200 error = got_repo_match_object_id(&commit_id, NULL,
4201 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT, 1,
4202 gw_trans->repo);
4203 if (error)
4204 goto done;
4207 error = got_object_id_by_path(&tree_id, gw_trans->repo, commit_id,
4208 path);
4209 if (error)
4210 goto done;
4212 error = got_object_open_as_tree(&tree, gw_trans->repo, tree_id);
4213 if (error)
4214 goto done;
4216 nentries = got_object_tree_get_nentries(tree);
4217 for (i = 0; i < nentries; i++) {
4218 struct got_tree_entry *te;
4219 const char *modestr = "";
4220 mode_t mode;
4222 te = got_object_tree_get_entry(tree, i);
4224 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
4225 if (error)
4226 goto done;
4228 mode = got_tree_entry_get_mode(te);
4229 if (got_object_tree_entry_is_submodule(te))
4230 modestr = "$";
4231 else if (S_ISLNK(mode))
4232 modestr = "@";
4233 else if (S_ISDIR(mode))
4234 modestr = "/";
4235 else if (mode & S_IXUSR)
4236 modestr = "*";
4238 if (class_flip == 0) {
4239 class = "back_lightgray";
4240 class_flip = 1;
4241 } else {
4242 class = "back_white";
4243 class_flip = 0;
4246 if (S_ISDIR(mode)) {
4247 if (asprintf(&build_folder, "%s/%s",
4248 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4249 got_tree_entry_get_name(te)) == -1) {
4250 error = got_error_from_errno("asprintf");
4251 goto done;
4254 href_blob = khttp_urlpart(NULL, NULL, "gotweb", "path",
4255 gw_trans->repo_name, "action",
4256 gw_get_action_name(gw_trans), "commit",
4257 gw_trans->commit_id, "folder", build_folder, NULL);
4258 if (href_blob == NULL) {
4259 error = got_error_from_errno("khttp_urlpart");
4260 goto done;
4262 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4263 KATTR_ID, "tree_wrapper", KATTR__MAX);
4264 if (kerr != KCGI_OK)
4265 goto done;
4266 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4267 KATTR_ID, "tree_line", KATTR_CLASS, class,
4268 KATTR__MAX);
4269 if (kerr != KCGI_OK)
4270 goto done;
4271 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4272 KATTR_HREF, href_blob, KATTR_CLASS,
4273 "diff_directory", KATTR__MAX);
4274 if (kerr != KCGI_OK)
4275 goto done;
4276 kerr = khtml_printf(gw_trans->gw_html_req, "%s%s",
4277 got_tree_entry_get_name(te), modestr);
4278 if (kerr != KCGI_OK)
4279 goto done;
4280 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4281 if (kerr != KCGI_OK)
4282 goto done;
4283 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4284 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
4285 KATTR__MAX);
4286 if (kerr != KCGI_OK)
4287 goto done;
4288 kerr = khtml_entity(gw_trans->gw_html_req,
4289 KENTITY_nbsp);
4290 if (kerr != KCGI_OK)
4291 goto done;
4292 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4293 if (kerr != KCGI_OK)
4294 goto done;
4295 } else {
4296 href_blob = khttp_urlpart(NULL, NULL, "gotweb", "path",
4297 gw_trans->repo_name, "action", "blob", "commit",
4298 gw_trans->commit_id, "file",
4299 got_tree_entry_get_name(te), "folder",
4300 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4301 NULL);
4302 if (href_blob == NULL) {
4303 error = got_error_from_errno("khttp_urlpart");
4304 goto done;
4306 href_blame = khttp_urlpart(NULL, NULL, "gotweb", "path",
4307 gw_trans->repo_name, "action", "blame", "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_blame == NULL) {
4313 error = got_error_from_errno("khttp_urlpart");
4314 goto done;
4316 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4317 KATTR_ID, "tree_wrapper", KATTR__MAX);
4318 if (kerr != KCGI_OK)
4319 goto done;
4320 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4321 KATTR_ID, "tree_line", KATTR_CLASS, class,
4322 KATTR__MAX);
4323 if (kerr != KCGI_OK)
4324 goto done;
4325 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4326 KATTR_HREF, href_blob, KATTR__MAX);
4327 if (kerr != KCGI_OK)
4328 goto done;
4329 kerr = khtml_printf(gw_trans->gw_html_req, "%s%s",
4330 got_tree_entry_get_name(te), modestr);
4331 if (kerr != KCGI_OK)
4332 goto done;
4333 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4334 if (kerr != KCGI_OK)
4335 goto done;
4336 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4337 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
4338 KATTR__MAX);
4339 if (kerr != KCGI_OK)
4340 goto done;
4342 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4343 KATTR_HREF, href_blob, KATTR__MAX);
4344 if (kerr != KCGI_OK)
4345 goto done;
4346 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
4347 if (kerr != KCGI_OK)
4348 goto done;
4349 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4350 if (kerr != KCGI_OK)
4351 goto done;
4353 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4354 if (kerr != KCGI_OK)
4355 goto done;
4357 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4358 KATTR_HREF, href_blame, KATTR__MAX);
4359 if (kerr != KCGI_OK)
4360 goto done;
4361 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
4362 if (kerr != KCGI_OK)
4363 goto done;
4365 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4366 if (kerr != KCGI_OK)
4367 goto done;
4369 free(id_str);
4370 id_str = NULL;
4371 free(href_blob);
4372 href_blob = NULL;
4373 free(build_folder);
4374 build_folder = NULL;
4376 done:
4377 if (tree)
4378 got_object_tree_close(tree);
4379 free(id_str);
4380 free(href_blob);
4381 free(href_blame);
4382 free(in_repo_path);
4383 free(tree_id);
4384 free(build_folder);
4385 if (error == NULL && kerr != KCGI_OK)
4386 error = gw_kcgi_error(kerr);
4387 return error;
4390 static const struct got_error *
4391 gw_output_repo_heads(struct gw_trans *gw_trans)
4393 const struct got_error *error = NULL;
4394 struct got_reflist_head refs;
4395 struct got_reflist_entry *re;
4396 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
4397 char *href_commits = NULL;
4398 enum kcgi_err kerr = KCGI_OK;
4400 SIMPLEQ_INIT(&refs);
4402 error = got_ref_list(&refs, gw_trans->repo, "refs/heads",
4403 got_ref_cmp_by_name, NULL);
4404 if (error)
4405 goto done;
4407 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4408 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
4409 if (kerr != KCGI_OK)
4410 goto done;
4411 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4412 KATTR_ID, "summary_heads_title", KATTR__MAX);
4413 if (kerr != KCGI_OK)
4414 goto done;
4415 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
4416 if (kerr != KCGI_OK)
4417 goto done;
4418 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4419 if (kerr != KCGI_OK)
4420 goto done;
4421 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4422 KATTR_ID, "summary_heads_content", KATTR__MAX);
4423 if (kerr != KCGI_OK)
4424 goto done;
4426 SIMPLEQ_FOREACH(re, &refs, entry) {
4427 const char *refname;
4429 if (got_ref_is_symbolic(re->ref))
4430 continue;
4432 refname = got_ref_get_name(re->ref);
4433 if (strncmp(refname, "refs/heads/", 11) != 0)
4434 continue;
4436 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
4437 refname, TM_DIFF);
4438 if (error)
4439 goto done;
4441 if (strncmp(refname, "refs/heads/", 11) == 0)
4442 refname += 11;
4444 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4445 KATTR_ID, "heads_wrapper", KATTR__MAX);
4446 if (kerr != KCGI_OK)
4447 goto done;
4448 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4449 KATTR_ID, "heads_age", KATTR__MAX);
4450 if (kerr != KCGI_OK)
4451 goto done;
4452 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
4453 if (kerr != KCGI_OK)
4454 goto done;
4455 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4456 if (kerr != KCGI_OK)
4457 goto done;
4458 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4459 KATTR_ID, "heads_space", KATTR__MAX);
4460 if (kerr != KCGI_OK)
4461 goto done;
4462 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
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, "head", KATTR__MAX);
4470 if (kerr != KCGI_OK)
4471 goto done;
4473 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
4474 gw_trans->repo_name, "action", "summary", "headref",
4475 refname, NULL);
4476 if (href_summary == NULL) {
4477 error = got_error_from_errno("khttp_urlpart");
4478 goto done;
4480 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4481 href_summary, KATTR__MAX);
4482 kerr = khtml_puts(gw_trans->gw_html_req, refname);
4483 if (kerr != KCGI_OK)
4484 goto done;
4485 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4486 if (kerr != KCGI_OK)
4487 goto done;
4489 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4490 "navs_wrapper", KATTR__MAX);
4491 if (kerr != KCGI_OK)
4492 goto done;
4493 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4494 "navs", KATTR__MAX);
4495 if (kerr != KCGI_OK)
4496 goto done;
4498 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4499 href_summary, KATTR__MAX);
4500 if (kerr != KCGI_OK)
4501 goto done;
4502 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4503 if (kerr != KCGI_OK)
4504 goto done;
4505 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4506 if (kerr != KCGI_OK)
4507 goto done;
4509 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4510 if (kerr != KCGI_OK)
4511 goto done;
4513 href_briefs = khttp_urlpart(NULL, NULL, "gotweb", "path",
4514 gw_trans->repo_name, "action", "briefs", "headref",
4515 refname, NULL);
4516 if (href_briefs == NULL) {
4517 error = got_error_from_errno("khttp_urlpart");
4518 goto done;
4520 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4521 href_briefs, KATTR__MAX);
4522 if (kerr != KCGI_OK)
4523 goto done;
4524 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4525 if (kerr != KCGI_OK)
4526 goto done;
4527 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4528 if (kerr != KCGI_OK)
4529 goto done;
4531 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4532 if (kerr != KCGI_OK)
4533 goto done;
4535 href_commits = khttp_urlpart(NULL, NULL, "gotweb", "path",
4536 gw_trans->repo_name, "action", "commits", "headref",
4537 refname, NULL);
4538 if (href_commits == NULL) {
4539 error = got_error_from_errno("khttp_urlpart");
4540 goto done;
4542 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4543 href_commits, KATTR__MAX);
4544 if (kerr != KCGI_OK)
4545 goto done;
4546 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4547 if (kerr != KCGI_OK)
4548 goto done;
4549 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4550 if (kerr != KCGI_OK)
4551 goto done;
4553 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4554 "dotted_line", KATTR__MAX);
4555 if (kerr != KCGI_OK)
4556 goto done;
4557 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4558 if (kerr != KCGI_OK)
4559 goto done;
4560 free(href_summary);
4561 href_summary = NULL;
4562 free(href_briefs);
4563 href_briefs = NULL;
4564 free(href_commits);
4565 href_commits = NULL;
4567 done:
4568 got_ref_list_free(&refs);
4569 free(href_summary);
4570 free(href_briefs);
4571 free(href_commits);
4572 return error;
4575 static const struct got_error *
4576 gw_output_site_link(struct gw_trans *gw_trans)
4578 const struct got_error *error = NULL;
4579 char *href_summary = NULL;
4580 enum kcgi_err kerr = KCGI_OK;
4582 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4583 "site_link", KATTR__MAX);
4584 if (kerr != KCGI_OK)
4585 goto done;
4586 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4587 KATTR__MAX);
4588 if (kerr != KCGI_OK)
4589 goto done;
4590 kerr = khtml_puts(gw_trans->gw_html_req,
4591 gw_trans->gw_conf->got_site_link);
4592 if (kerr != KCGI_OK)
4593 goto done;
4594 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4595 if (kerr != KCGI_OK)
4596 goto done;
4598 if (gw_trans->repo_name != NULL) {
4599 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4600 if (kerr != KCGI_OK)
4601 goto done;
4603 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
4604 gw_trans->repo_name, "action", "summary", NULL);
4605 if (href_summary == NULL) {
4606 error = got_error_from_errno("khttp_urlpart");
4607 goto done;
4609 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4610 href_summary, KATTR__MAX);
4611 if (kerr != KCGI_OK)
4612 goto done;
4613 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4614 if (kerr != KCGI_OK)
4615 goto done;
4616 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4617 if (kerr != KCGI_OK)
4618 goto done;
4619 kerr = khtml_printf(gw_trans->gw_html_req, " / %s",
4620 gw_get_action_name(gw_trans));
4621 if (kerr != KCGI_OK)
4622 goto done;
4625 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4626 if (kerr != KCGI_OK)
4627 goto done;
4628 done:
4629 free(href_summary);
4630 if (error == NULL && kerr != KCGI_OK)
4631 error = gw_kcgi_error(kerr);
4632 return error;
4635 static const struct got_error *
4636 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4638 const struct got_error *error = NULL;
4639 char *color = NULL;
4640 enum kcgi_err kerr = KCGI_OK;
4642 if (strncmp(buf, "-", 1) == 0)
4643 color = "diff_minus";
4644 else if (strncmp(buf, "+", 1) == 0)
4645 color = "diff_plus";
4646 else if (strncmp(buf, "@@", 2) == 0)
4647 color = "diff_chunk_header";
4648 else if (strncmp(buf, "@@", 2) == 0)
4649 color = "diff_chunk_header";
4650 else if (strncmp(buf, "commit +", 8) == 0)
4651 color = "diff_meta";
4652 else if (strncmp(buf, "commit -", 8) == 0)
4653 color = "diff_meta";
4654 else if (strncmp(buf, "blob +", 6) == 0)
4655 color = "diff_meta";
4656 else if (strncmp(buf, "blob -", 6) == 0)
4657 color = "diff_meta";
4658 else if (strncmp(buf, "file +", 6) == 0)
4659 color = "diff_meta";
4660 else if (strncmp(buf, "file -", 6) == 0)
4661 color = "diff_meta";
4662 else if (strncmp(buf, "from:", 5) == 0)
4663 color = "diff_author";
4664 else if (strncmp(buf, "via:", 4) == 0)
4665 color = "diff_author";
4666 else if (strncmp(buf, "date:", 5) == 0)
4667 color = "diff_date";
4668 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4669 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4670 if (error == NULL && kerr != KCGI_OK)
4671 error = gw_kcgi_error(kerr);
4672 return error;
4675 int
4676 main(int argc, char *argv[])
4678 const struct got_error *error = NULL;
4679 struct gw_trans *gw_trans;
4680 struct gw_dir *dir = NULL, *tdir;
4681 const char *page = "index";
4682 int gw_malloc = 1;
4683 enum kcgi_err kerr = KCGI_OK;
4685 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4686 errx(1, "malloc");
4688 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4689 errx(1, "malloc");
4691 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4692 errx(1, "malloc");
4694 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4695 errx(1, "malloc");
4697 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4698 if (kerr != KCGI_OK) {
4699 error = gw_kcgi_error(kerr);
4700 goto done;
4703 TAILQ_INIT(&gw_trans->gw_dirs);
4704 TAILQ_INIT(&gw_trans->gw_headers);
4706 gw_trans->action = -1;
4707 gw_trans->page = 0;
4708 gw_trans->repos_total = 0;
4709 gw_trans->repo_path = NULL;
4710 gw_trans->commit_id = NULL;
4711 gw_trans->next_id = NULL;
4712 gw_trans->next_prev_id = NULL;
4713 gw_trans->prev_id = NULL;
4714 gw_trans->prev_prev_id = NULL;
4715 gw_trans->headref = GOT_REF_HEAD;
4716 gw_trans->mime = KMIME_TEXT_HTML;
4717 gw_trans->gw_tmpl->key = gw_templs;
4718 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4719 gw_trans->gw_tmpl->arg = gw_trans;
4720 gw_trans->gw_tmpl->cb = gw_template;
4722 error = parse_gotweb_config(&gw_trans->gw_conf, GOTWEB_CONF);
4723 if (error)
4724 goto done;
4726 error = gw_parse_querystring(gw_trans);
4727 if (error)
4728 goto done;
4730 if (gw_trans->action == GW_BLOB)
4731 error = gw_blob(gw_trans);
4732 else
4733 error = gw_display_index(gw_trans);
4734 done:
4735 if (gw_malloc) {
4736 free(gw_trans->gw_conf->got_repos_path);
4737 free(gw_trans->gw_conf->got_www_path);
4738 free(gw_trans->gw_conf->got_site_name);
4739 free(gw_trans->gw_conf->got_site_owner);
4740 free(gw_trans->gw_conf->got_site_link);
4741 free(gw_trans->gw_conf->got_logo);
4742 free(gw_trans->gw_conf->got_logo_url);
4743 free(gw_trans->gw_conf);
4744 free(gw_trans->commit_id);
4745 free(gw_trans->next_id);
4746 free(gw_trans->next_prev_id);
4747 free(gw_trans->prev_id);
4748 free(gw_trans->prev_prev_id);
4749 free(gw_trans->repo_path);
4750 if (gw_trans->repo)
4751 got_repo_close(gw_trans->repo);
4753 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4754 free(dir->name);
4755 free(dir->description);
4756 free(dir->age);
4757 free(dir->url);
4758 free(dir->path);
4759 free(dir);
4764 khttp_free(gw_trans->gw_req);
4765 return 0;