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 *img_src = NULL;
2197 switch (key) {
2198 case (TEMPL_HEAD):
2199 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2200 KATTR_NAME, "viewport",
2201 KATTR_CONTENT, "initial-scale=.75, user-scalable=yes",
2202 KATTR__MAX);
2203 if (kerr != KCGI_OK)
2204 return 0;
2205 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2206 if (kerr != KCGI_OK)
2207 return 0;
2208 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2209 KATTR_CHARSET, "utf-8",
2210 KATTR__MAX);
2211 if (kerr != KCGI_OK)
2212 return 0;
2213 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2214 if (kerr != KCGI_OK)
2215 return 0;
2216 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2217 KATTR_NAME, "msapplication-TileColor",
2218 KATTR_CONTENT, "#da532c", KATTR__MAX);
2219 if (kerr != KCGI_OK)
2220 return 0;
2221 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2222 if (kerr != KCGI_OK)
2223 return 0;
2224 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2225 KATTR_NAME, "theme-color",
2226 KATTR_CONTENT, "#ffffff", 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_LINK,
2233 KATTR_REL, "apple-touch-icon", KATTR_SIZES, "180x180",
2234 KATTR_HREF, "/apple-touch-icon.png", 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_LINK,
2241 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2242 "32x32", KATTR_HREF, "/favicon-32x32.png", 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_LINK,
2249 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2250 "16x16", KATTR_HREF, "/favicon-16x16.png", 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, "manifest", KATTR_HREF, "/site.webmanifest",
2258 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, "mask-icon", KATTR_HREF,
2266 "/safari-pinned-tab.svg", 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, "stylesheet", KATTR_TYPE, "text/css",
2274 KATTR_HREF, "/gotweb.css", 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 break;
2281 case(TEMPL_HEADER):
2282 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2283 KATTR_ID, "got_link", KATTR__MAX);
2284 if (kerr != KCGI_OK)
2285 return 0;
2286 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2287 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
2288 KATTR_TARGET, "_sotd", KATTR__MAX);
2289 if (kerr != KCGI_OK)
2290 return 0;
2291 if (asprintf(&img_src, "/%s",
2292 gw_trans->gw_conf->got_logo) == -1)
2293 return 0;
2294 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
2295 KATTR_SRC, img_src, KATTR__MAX);
2296 if (kerr != KCGI_OK) {
2297 free(img_src);
2298 return 0;
2300 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2301 if (kerr != KCGI_OK) {
2302 free(img_src);
2303 return 0;
2305 break;
2306 case (TEMPL_SITEPATH):
2307 error = gw_output_site_link(gw_trans);
2308 if (error)
2309 return 0;
2310 break;
2311 case(TEMPL_TITLE):
2312 if (gw_trans->gw_conf->got_site_name != NULL) {
2313 kerr = khtml_puts(gw_trans->gw_html_req,
2314 gw_trans->gw_conf->got_site_name);
2315 if (kerr != KCGI_OK)
2316 return 0;
2318 break;
2319 case (TEMPL_SEARCH):
2320 break;
2321 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
2322 "search", KATTR__MAX);
2323 if (kerr != KCGI_OK)
2324 return 0;
2325 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
2326 KATTR_METHOD, "POST", KATTR__MAX);
2327 if (kerr != KCGI_OK)
2328 return 0;
2329 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
2330 "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
2331 KATTR_MAXLENGTH, "50", KATTR__MAX);
2332 if (kerr != KCGI_OK)
2333 return 0;
2334 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
2335 KATTR__MAX);
2336 if (kerr != KCGI_OK)
2337 return 0;
2338 kerr = khtml_puts(gw_trans->gw_html_req, "Search");
2339 if (kerr != KCGI_OK)
2340 return 0;
2341 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
2342 if (kerr != KCGI_OK)
2343 return 0;
2344 break;
2345 case(TEMPL_SITEOWNER):
2346 if (gw_trans->gw_conf->got_site_owner != NULL &&
2347 gw_trans->gw_conf->got_show_site_owner) {
2348 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2349 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
2350 if (kerr != KCGI_OK)
2351 return 0;
2352 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2353 KATTR_ID, "site_owner", KATTR__MAX);
2354 if (kerr != KCGI_OK)
2355 return 0;
2356 kerr = khtml_puts(gw_trans->gw_html_req,
2357 gw_trans->gw_conf->got_site_owner);
2358 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2359 if (kerr != KCGI_OK)
2360 return 0;
2362 break;
2363 case(TEMPL_CONTENT):
2364 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
2365 if (error) {
2366 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2367 KATTR_ID, "tmpl_err", KATTR__MAX);
2368 if (kerr != KCGI_OK)
2369 return 0;
2370 kerr = khttp_printf(gw_trans->gw_req, "Error: %s",
2371 error->msg);
2372 if (kerr != KCGI_OK)
2373 return 0;
2374 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2375 if (kerr != KCGI_OK)
2376 return 0;
2378 break;
2379 default:
2380 return 0;
2382 return 1;
2385 static const struct got_error *
2386 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
2388 const struct got_error *error = NULL;
2389 enum kcgi_err kerr = KCGI_OK;
2391 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2392 KATTR_ID, "header_commit_title", KATTR__MAX);
2393 if (kerr != KCGI_OK)
2394 goto done;
2395 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
2396 if (kerr != KCGI_OK)
2397 goto done;
2398 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2399 if (kerr != KCGI_OK)
2400 goto done;
2401 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2402 KATTR_ID, "header_commit", KATTR__MAX);
2403 if (kerr != KCGI_OK)
2404 goto done;
2405 kerr = khtml_printf(gw_trans->gw_html_req, "%s ", str1);
2406 if (kerr != KCGI_OK)
2407 goto done;
2408 if (str2 != NULL) {
2409 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
2410 KATTR_ID, "refs_str", KATTR__MAX);
2411 if (kerr != KCGI_OK)
2412 goto done;
2413 kerr = khtml_printf(gw_trans->gw_html_req, "(%s)", str2);
2414 if (kerr != KCGI_OK)
2415 goto done;
2416 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2417 if (kerr != KCGI_OK)
2418 goto done;
2420 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2421 done:
2422 if (error == NULL && kerr != KCGI_OK)
2423 error = gw_kcgi_error(kerr);
2424 return error;
2427 static const struct got_error *
2428 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2430 const struct got_error *error = NULL;
2431 enum kcgi_err kerr = KCGI_OK;
2433 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2434 KATTR_ID, "header_diff_title", KATTR__MAX);
2435 if (kerr != KCGI_OK)
2436 goto done;
2437 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2438 if (kerr != KCGI_OK)
2439 goto done;
2440 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2441 if (kerr != KCGI_OK)
2442 goto done;
2443 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2444 KATTR_ID, "header_diff", KATTR__MAX);
2445 if (kerr != KCGI_OK)
2446 goto done;
2447 if (str1 != NULL) {
2448 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2449 if (kerr != KCGI_OK)
2450 goto done;
2452 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2453 if (kerr != KCGI_OK)
2454 goto done;
2455 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2456 if (kerr != KCGI_OK)
2457 goto done;
2458 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2459 done:
2460 if (error == NULL && kerr != KCGI_OK)
2461 error = gw_kcgi_error(kerr);
2462 return error;
2465 static const struct got_error *
2466 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2468 const struct got_error *error = NULL;
2469 enum kcgi_err kerr = KCGI_OK;
2471 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2472 KATTR_ID, "header_age_title", KATTR__MAX);
2473 if (kerr != KCGI_OK)
2474 goto done;
2475 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2476 if (kerr != KCGI_OK)
2477 goto done;
2478 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2479 if (kerr != KCGI_OK)
2480 goto done;
2481 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2482 KATTR_ID, "header_age", KATTR__MAX);
2483 if (kerr != KCGI_OK)
2484 goto done;
2485 kerr = khtml_puts(gw_trans->gw_html_req, str);
2486 if (kerr != KCGI_OK)
2487 goto done;
2488 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2489 done:
2490 if (error == NULL && kerr != KCGI_OK)
2491 error = gw_kcgi_error(kerr);
2492 return error;
2495 static const struct got_error *
2496 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2498 const struct got_error *error = NULL;
2499 enum kcgi_err kerr = KCGI_OK;
2501 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2502 KATTR_ID, "header_author_title", KATTR__MAX);
2503 if (kerr != KCGI_OK)
2504 goto done;
2505 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2506 if (kerr != KCGI_OK)
2507 goto done;
2508 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2509 if (kerr != KCGI_OK)
2510 goto done;
2511 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2512 KATTR_ID, "header_author", KATTR__MAX);
2513 if (kerr != KCGI_OK)
2514 goto done;
2515 kerr = khtml_puts(gw_trans->gw_html_req, str);
2516 if (kerr != KCGI_OK)
2517 goto done;
2518 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2519 done:
2520 if (error == NULL && kerr != KCGI_OK)
2521 error = gw_kcgi_error(kerr);
2522 return error;
2525 static const struct got_error *
2526 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2528 const struct got_error *error = NULL;
2529 enum kcgi_err kerr = KCGI_OK;
2531 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2532 KATTR_ID, "header_committer_title", KATTR__MAX);
2533 if (kerr != KCGI_OK)
2534 goto done;
2535 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2536 if (kerr != KCGI_OK)
2537 goto done;
2538 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2539 if (kerr != KCGI_OK)
2540 goto done;
2541 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2542 KATTR_ID, "header_committer", KATTR__MAX);
2543 if (kerr != KCGI_OK)
2544 goto done;
2545 kerr = khtml_puts(gw_trans->gw_html_req, str);
2546 if (kerr != KCGI_OK)
2547 goto done;
2548 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2549 done:
2550 if (error == NULL && kerr != KCGI_OK)
2551 error = gw_kcgi_error(kerr);
2552 return error;
2555 static const struct got_error *
2556 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2558 const struct got_error *error = NULL;
2559 enum kcgi_err kerr = KCGI_OK;
2561 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2562 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2563 if (kerr != KCGI_OK)
2564 goto done;
2565 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2566 if (kerr != KCGI_OK)
2567 goto done;
2568 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2569 if (kerr != KCGI_OK)
2570 goto done;
2571 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2572 KATTR_ID, "header_commit_msg", KATTR__MAX);
2573 if (kerr != KCGI_OK)
2574 goto done;
2575 kerr = khttp_puts(gw_trans->gw_req, str);
2576 if (kerr != KCGI_OK)
2577 goto done;
2578 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2579 done:
2580 if (error == NULL && kerr != KCGI_OK)
2581 error = gw_kcgi_error(kerr);
2582 return error;
2585 static const struct got_error *
2586 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2588 const struct got_error *error = NULL;
2589 enum kcgi_err kerr = KCGI_OK;
2591 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2592 KATTR_ID, "header_tree_title", KATTR__MAX);
2593 if (kerr != KCGI_OK)
2594 goto done;
2595 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2596 if (kerr != KCGI_OK)
2597 goto done;
2598 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2599 if (kerr != KCGI_OK)
2600 goto done;
2601 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2602 KATTR_ID, "header_tree", KATTR__MAX);
2603 if (kerr != KCGI_OK)
2604 goto done;
2605 kerr = khtml_puts(gw_trans->gw_html_req, str);
2606 if (kerr != KCGI_OK)
2607 goto done;
2608 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2609 done:
2610 if (error == NULL && kerr != KCGI_OK)
2611 error = gw_kcgi_error(kerr);
2612 return error;
2615 static const struct got_error *
2616 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2617 char *dir)
2619 const struct got_error *error = NULL;
2620 FILE *f = NULL;
2621 char *d_file = NULL;
2622 unsigned int len;
2623 size_t n;
2625 *description = NULL;
2626 if (gw_trans->gw_conf->got_show_repo_description == 0)
2627 return NULL;
2629 if (asprintf(&d_file, "%s/description", dir) == -1)
2630 return got_error_from_errno("asprintf");
2632 f = fopen(d_file, "r");
2633 if (f == NULL) {
2634 if (errno == ENOENT || errno == EACCES)
2635 return NULL;
2636 error = got_error_from_errno2("fopen", d_file);
2637 goto done;
2640 if (fseek(f, 0, SEEK_END) == -1) {
2641 error = got_ferror(f, GOT_ERR_IO);
2642 goto done;
2644 len = ftell(f);
2645 if (len == -1) {
2646 error = got_ferror(f, GOT_ERR_IO);
2647 goto done;
2649 if (fseek(f, 0, SEEK_SET) == -1) {
2650 error = got_ferror(f, GOT_ERR_IO);
2651 goto done;
2653 *description = calloc(len + 1, sizeof(**description));
2654 if (*description == NULL) {
2655 error = got_error_from_errno("calloc");
2656 goto done;
2659 n = fread(*description, 1, len, f);
2660 if (n == 0 && ferror(f))
2661 error = got_ferror(f, GOT_ERR_IO);
2662 done:
2663 if (f != NULL && fclose(f) == -1 && error == NULL)
2664 error = got_error_from_errno("fclose");
2665 free(d_file);
2666 return error;
2669 static const struct got_error *
2670 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2672 struct tm tm;
2673 time_t diff_time;
2674 char *years = "years ago", *months = "months ago";
2675 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2676 char *minutes = "minutes ago", *seconds = "seconds ago";
2677 char *now = "right now";
2678 char *s;
2679 char datebuf[29];
2681 *repo_age = NULL;
2683 switch (ref_tm) {
2684 case TM_DIFF:
2685 diff_time = time(NULL) - committer_time;
2686 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2687 if (asprintf(repo_age, "%lld %s",
2688 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2689 return got_error_from_errno("asprintf");
2690 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2691 if (asprintf(repo_age, "%lld %s",
2692 (diff_time / 60 / 60 / 24 / (365 / 12)),
2693 months) == -1)
2694 return got_error_from_errno("asprintf");
2695 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2696 if (asprintf(repo_age, "%lld %s",
2697 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2698 return got_error_from_errno("asprintf");
2699 } else if (diff_time > 60 * 60 * 24 * 2) {
2700 if (asprintf(repo_age, "%lld %s",
2701 (diff_time / 60 / 60 / 24), days) == -1)
2702 return got_error_from_errno("asprintf");
2703 } else if (diff_time > 60 * 60 * 2) {
2704 if (asprintf(repo_age, "%lld %s",
2705 (diff_time / 60 / 60), hours) == -1)
2706 return got_error_from_errno("asprintf");
2707 } else if (diff_time > 60 * 2) {
2708 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2709 minutes) == -1)
2710 return got_error_from_errno("asprintf");
2711 } else if (diff_time > 2) {
2712 if (asprintf(repo_age, "%lld %s", diff_time,
2713 seconds) == -1)
2714 return got_error_from_errno("asprintf");
2715 } else {
2716 if (asprintf(repo_age, "%s", now) == -1)
2717 return got_error_from_errno("asprintf");
2719 break;
2720 case TM_LONG:
2721 if (gmtime_r(&committer_time, &tm) == NULL)
2722 return got_error_from_errno("gmtime_r");
2724 s = asctime_r(&tm, datebuf);
2725 if (s == NULL)
2726 return got_error_from_errno("asctime_r");
2728 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2729 return got_error_from_errno("asprintf");
2730 break;
2732 return NULL;
2735 static const struct got_error *
2736 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2737 const char *refname, int ref_tm)
2739 const struct got_error *error = NULL;
2740 struct got_repository *repo = NULL;
2741 struct got_commit_object *commit = NULL;
2742 struct got_reflist_head refs;
2743 struct got_reflist_entry *re;
2744 time_t committer_time = 0, cmp_time = 0;
2746 *repo_age = NULL;
2747 SIMPLEQ_INIT(&refs);
2749 if (gw_trans->gw_conf->got_show_repo_age == 0)
2750 return NULL;
2752 if (gw_trans->repo)
2753 repo = gw_trans->repo;
2754 else {
2755 error = got_repo_open(&repo, dir, NULL);
2756 if (error)
2757 return error;
2760 error = got_ref_list(&refs, repo, "refs/heads",
2761 got_ref_cmp_by_name, NULL);
2762 if (error)
2763 goto done;
2766 * Find the youngest branch tip in the repository, or the age of
2767 * the a specific branch tip if a name was provided by the caller.
2769 SIMPLEQ_FOREACH(re, &refs, entry) {
2770 struct got_object_id *id = NULL;
2772 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
2773 continue;
2775 error = got_ref_resolve(&id, repo, re->ref);
2776 if (error)
2777 goto done;
2779 error = got_object_open_as_commit(&commit, repo, id);
2780 free(id);
2781 if (error)
2782 goto done;
2784 committer_time =
2785 got_object_commit_get_committer_time(commit);
2786 got_object_commit_close(commit);
2787 if (cmp_time < committer_time)
2788 cmp_time = committer_time;
2790 if (refname)
2791 break;
2794 if (cmp_time != 0) {
2795 committer_time = cmp_time;
2796 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2798 done:
2799 got_ref_list_free(&refs);
2800 if (gw_trans->repo == NULL)
2801 got_repo_close(repo);
2802 return error;
2805 static const struct got_error *
2806 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2808 const struct got_error *error;
2809 FILE *f = NULL;
2810 struct got_object_id *id1 = NULL, *id2 = NULL;
2811 char *label1 = NULL, *label2 = NULL, *line = NULL;
2812 int obj_type;
2813 size_t linesize = 0;
2814 ssize_t linelen;
2815 enum kcgi_err kerr = KCGI_OK;
2817 f = got_opentemp();
2818 if (f == NULL)
2819 return NULL;
2821 if (header->parent_id != NULL &&
2822 strncmp(header->parent_id, "/dev/null", 9) != 0) {
2823 error = got_repo_match_object_id(&id1, &label1,
2824 header->parent_id, GOT_OBJ_TYPE_ANY, 1, gw_trans->repo);
2825 if (error)
2826 goto done;
2829 error = got_repo_match_object_id(&id2, &label2,
2830 header->commit_id, GOT_OBJ_TYPE_ANY, 1, gw_trans->repo);
2831 if (error)
2832 goto done;
2834 error = got_object_get_type(&obj_type, gw_trans->repo, id2);
2835 if (error)
2836 goto done;
2837 switch (obj_type) {
2838 case GOT_OBJ_TYPE_BLOB:
2839 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
2840 gw_trans->repo, f);
2841 break;
2842 case GOT_OBJ_TYPE_TREE:
2843 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
2844 gw_trans->repo, f);
2845 break;
2846 case GOT_OBJ_TYPE_COMMIT:
2847 error = got_diff_objects_as_commits(id1, id2, 3, 0,
2848 gw_trans->repo, f);
2849 break;
2850 default:
2851 error = got_error(GOT_ERR_OBJ_TYPE);
2853 if (error)
2854 goto done;
2856 if (fseek(f, 0, SEEK_SET) == -1) {
2857 error = got_ferror(f, GOT_ERR_IO);
2858 goto done;
2861 while ((linelen = getline(&line, &linesize, f)) != -1) {
2862 error = gw_colordiff_line(gw_trans, line);
2863 if (error)
2864 goto done;
2865 /* XXX: KHTML_PRETTY breaks this */
2866 kerr = khtml_puts(gw_trans->gw_html_req, line);
2867 if (kerr != KCGI_OK)
2868 goto done;
2869 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2870 if (kerr != KCGI_OK)
2871 goto done;
2873 if (linelen == -1 && ferror(f))
2874 error = got_error_from_errno("getline");
2875 done:
2876 if (f && fclose(f) == -1 && error == NULL)
2877 error = got_error_from_errno("fclose");
2878 free(line);
2879 free(label1);
2880 free(label2);
2881 free(id1);
2882 free(id2);
2884 if (error == NULL && kerr != KCGI_OK)
2885 error = gw_kcgi_error(kerr);
2886 return error;
2889 static const struct got_error *
2890 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2892 const struct got_error *error = NULL;
2893 struct got_repository *repo;
2894 const char *gitconfig_owner;
2896 *owner = NULL;
2898 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2899 return NULL;
2901 error = got_repo_open(&repo, dir, NULL);
2902 if (error)
2903 return error;
2904 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2905 if (gitconfig_owner) {
2906 *owner = strdup(gitconfig_owner);
2907 if (*owner == NULL)
2908 error = got_error_from_errno("strdup");
2910 got_repo_close(repo);
2911 return error;
2914 static const struct got_error *
2915 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2917 const struct got_error *error = NULL;
2918 FILE *f;
2919 char *d_file = NULL;
2920 unsigned int len;
2921 size_t n;
2923 *url = NULL;
2925 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2926 return got_error_from_errno("asprintf");
2928 f = fopen(d_file, "r");
2929 if (f == NULL) {
2930 if (errno != ENOENT && errno != EACCES)
2931 error = got_error_from_errno2("fopen", d_file);
2932 goto done;
2935 if (fseek(f, 0, SEEK_END) == -1) {
2936 error = got_ferror(f, GOT_ERR_IO);
2937 goto done;
2939 len = ftell(f);
2940 if (len == -1) {
2941 error = got_ferror(f, GOT_ERR_IO);
2942 goto done;
2944 if (fseek(f, 0, SEEK_SET) == -1) {
2945 error = got_ferror(f, GOT_ERR_IO);
2946 goto done;
2949 *url = calloc(len + 1, sizeof(**url));
2950 if (*url == NULL) {
2951 error = got_error_from_errno("calloc");
2952 goto done;
2955 n = fread(*url, 1, len, f);
2956 if (n == 0 && ferror(f))
2957 error = got_ferror(f, GOT_ERR_IO);
2958 done:
2959 if (f && fclose(f) == -1 && error == NULL)
2960 error = got_error_from_errno("fclose");
2961 free(d_file);
2962 return NULL;
2965 static const struct got_error *
2966 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
2967 int limit, int tag_type)
2969 const struct got_error *error = NULL;
2970 struct got_reflist_head refs;
2971 struct got_reflist_entry *re;
2972 char *age = NULL;
2973 char *id_str = NULL, *newline, *href_commits = NULL;
2974 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
2975 struct got_tag_object *tag = NULL;
2976 enum kcgi_err kerr = KCGI_OK;
2977 int summary_header_displayed = 0, start_tag = 0, chk_next = 0;
2978 int prev_set = 0, tag_count = 0;
2980 SIMPLEQ_INIT(&refs);
2982 error = got_ref_list(&refs, gw_trans->repo, "refs/tags",
2983 got_ref_cmp_tags, gw_trans->repo);
2984 if (error)
2985 goto done;
2987 SIMPLEQ_FOREACH(re, &refs, entry) {
2988 const char *refname;
2989 const char *tagger;
2990 const char *tag_commit;
2991 time_t tagger_time;
2992 struct got_object_id *id;
2993 struct got_commit_object *commit = NULL;
2995 refname = got_ref_get_name(re->ref);
2996 if (strncmp(refname, "refs/tags/", 10) != 0)
2997 continue;
2998 refname += 10;
3000 error = got_ref_resolve(&id, gw_trans->repo, re->ref);
3001 if (error)
3002 goto done;
3004 error = got_object_open_as_tag(&tag, gw_trans->repo, id);
3005 if (error) {
3006 if (error->code != GOT_ERR_OBJ_TYPE) {
3007 free(id);
3008 goto done;
3010 /* "lightweight" tag */
3011 error = got_object_open_as_commit(&commit,
3012 gw_trans->repo, id);
3013 if (error) {
3014 free(id);
3015 goto done;
3017 tagger = got_object_commit_get_committer(commit);
3018 tagger_time =
3019 got_object_commit_get_committer_time(commit);
3020 error = got_object_id_str(&id_str, id);
3021 free(id);
3022 } else {
3023 free(id);
3024 tagger = got_object_tag_get_tagger(tag);
3025 tagger_time = got_object_tag_get_tagger_time(tag);
3026 error = got_object_id_str(&id_str,
3027 got_object_tag_get_object_id(tag));
3029 if (error)
3030 goto done;
3032 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
3033 strlen(id_str)) != 0)
3034 continue;
3036 if (tag_type == TAGBRIEF && gw_trans->commit_id &&
3037 start_tag == 0 && strncmp(id_str, header->commit_id,
3038 strlen(id_str)) != 0) {
3039 continue;
3040 } else {
3041 start_tag = 1;
3044 tag_count++;
3046 if (prev_set == 0 && start_tag == 1) {
3047 gw_trans->next_prev_id = strdup(id_str);
3048 if (gw_trans->next_prev_id == NULL) {
3049 error = got_error_from_errno("strdup");
3050 goto done;
3052 prev_set = 1;
3055 if (chk_next) {
3056 gw_trans->next_id = strdup(id_str);
3057 if (gw_trans->next_id == NULL)
3058 error = got_error_from_errno("strdup");
3059 goto done;
3062 if (commit) {
3063 error = got_object_commit_get_logmsg(&tag_commit0,
3064 commit);
3065 if (error)
3066 goto done;
3067 got_object_commit_close(commit);
3068 } else {
3069 tag_commit0 = strdup(got_object_tag_get_message(tag));
3070 if (tag_commit0 == NULL) {
3071 error = got_error_from_errno("strdup");
3072 goto done;
3076 tag_commit = tag_commit0;
3077 while (*tag_commit == '\n')
3078 tag_commit++;
3080 switch (tag_type) {
3081 case TAGBRIEF:
3082 newline = strchr(tag_commit, '\n');
3083 if (newline)
3084 *newline = '\0';
3086 if (summary_header_displayed == 0) {
3087 kerr = khtml_attr(gw_trans->gw_html_req,
3088 KELEM_DIV, KATTR_ID,
3089 "summary_tags_title_wrapper", KATTR__MAX);
3090 if (kerr != KCGI_OK)
3091 goto done;
3092 kerr = khtml_attr(gw_trans->gw_html_req,
3093 KELEM_DIV, KATTR_ID,
3094 "summary_tags_title", KATTR__MAX);
3095 if (kerr != KCGI_OK)
3096 goto done;
3097 kerr = khtml_puts(gw_trans->gw_html_req,
3098 "Tags");
3099 if (kerr != KCGI_OK)
3100 goto done;
3101 kerr = khtml_closeelem(gw_trans->gw_html_req,
3102 2);
3103 if (kerr != KCGI_OK)
3104 goto done;
3105 kerr = khtml_attr(gw_trans->gw_html_req,
3106 KELEM_DIV, KATTR_ID,
3107 "summary_tags_content", KATTR__MAX);
3108 if (kerr != KCGI_OK)
3109 goto done;
3110 summary_header_displayed = 1;
3113 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3114 KATTR_ID, "tag_wrapper", KATTR__MAX);
3115 if (kerr != KCGI_OK)
3116 goto done;
3117 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3118 KATTR_ID, "tag_age", KATTR__MAX);
3119 if (kerr != KCGI_OK)
3120 goto done;
3121 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
3122 if (error)
3123 goto done;
3124 kerr = khtml_puts(gw_trans->gw_html_req,
3125 age ? age : "");
3126 if (kerr != KCGI_OK)
3127 goto done;
3128 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3129 if (kerr != KCGI_OK)
3130 goto done;
3131 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3132 KATTR_ID, "tag", KATTR__MAX);
3133 if (kerr != KCGI_OK)
3134 goto done;
3135 kerr = khtml_puts(gw_trans->gw_html_req, refname);
3136 if (kerr != KCGI_OK)
3137 goto done;
3138 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3139 if (kerr != KCGI_OK)
3140 goto done;
3141 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3142 KATTR_ID, "tag_name", KATTR__MAX);
3143 if (kerr != KCGI_OK)
3144 goto done;
3146 href_tag = khttp_urlpart(NULL, NULL, "gotweb", "path",
3147 gw_trans->repo_name, "action", "tag", "commit",
3148 id_str, NULL);
3149 if (href_tag == NULL) {
3150 error = got_error_from_errno("khttp_urlpart");
3151 goto done;
3153 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3154 KATTR_HREF, href_tag, KATTR__MAX);
3155 if (kerr != KCGI_OK)
3156 goto done;
3157 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
3158 if (kerr != KCGI_OK)
3159 goto done;
3160 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3161 if (kerr != KCGI_OK)
3162 goto done;
3164 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3165 KATTR_ID, "navs_wrapper", KATTR__MAX);
3166 if (kerr != KCGI_OK)
3167 goto done;
3168 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3169 KATTR_ID, "navs", KATTR__MAX);
3170 if (kerr != KCGI_OK)
3171 goto done;
3173 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3174 KATTR_HREF, href_tag, KATTR__MAX);
3175 if (kerr != KCGI_OK)
3176 goto done;
3177 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
3178 if (kerr != KCGI_OK)
3179 goto done;
3180 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3181 if (kerr != KCGI_OK)
3182 goto done;
3184 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3185 if (kerr != KCGI_OK)
3186 goto done;
3188 href_briefs = khttp_urlpart(NULL, NULL, "gotweb",
3189 "path", gw_trans->repo_name, "action", "briefs",
3190 "commit", id_str, NULL);
3191 if (href_briefs == NULL) {
3192 error = got_error_from_errno("khttp_urlpart");
3193 goto done;
3195 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3196 KATTR_HREF, href_briefs, KATTR__MAX);
3197 if (kerr != KCGI_OK)
3198 goto done;
3199 kerr = khtml_puts(gw_trans->gw_html_req,
3200 "commit briefs");
3201 if (kerr != KCGI_OK)
3202 goto done;
3203 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3204 if (kerr != KCGI_OK)
3205 goto done;
3207 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3208 if (kerr != KCGI_OK)
3209 goto done;
3211 href_commits = khttp_urlpart(NULL, NULL, "gotweb",
3212 "path", gw_trans->repo_name, "action", "commits",
3213 "commit", id_str, NULL);
3214 if (href_commits == NULL) {
3215 error = got_error_from_errno("khttp_urlpart");
3216 goto done;
3218 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3219 KATTR_HREF, href_commits, KATTR__MAX);
3220 if (kerr != KCGI_OK)
3221 goto done;
3222 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
3223 if (kerr != KCGI_OK)
3224 goto done;
3225 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3226 if (kerr != KCGI_OK)
3227 goto done;
3229 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3230 KATTR_ID, "dotted_line", KATTR__MAX);
3231 if (kerr != KCGI_OK)
3232 goto done;
3233 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3234 if (kerr != KCGI_OK)
3235 goto done;
3236 break;
3237 case TAGFULL:
3238 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3239 KATTR_ID, "tag_info_date_title", KATTR__MAX);
3240 if (kerr != KCGI_OK)
3241 goto done;
3242 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
3243 if (kerr != KCGI_OK)
3244 goto done;
3245 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3246 if (kerr != KCGI_OK)
3247 goto done;
3248 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3249 KATTR_ID, "tag_info_date", KATTR__MAX);
3250 if (kerr != KCGI_OK)
3251 goto done;
3252 error = gw_get_time_str(&age, tagger_time, TM_LONG);
3253 if (error)
3254 goto done;
3255 kerr = khtml_puts(gw_trans->gw_html_req,
3256 age ? age : "");
3257 if (kerr != KCGI_OK)
3258 goto done;
3259 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3260 if (kerr != KCGI_OK)
3261 goto done;
3263 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3264 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
3265 if (kerr != KCGI_OK)
3266 goto done;
3267 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
3268 if (kerr != KCGI_OK)
3269 goto done;
3270 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3271 if (kerr != KCGI_OK)
3272 goto done;
3273 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3274 KATTR_ID, "tag_info_date", KATTR__MAX);
3275 if (kerr != KCGI_OK)
3276 goto done;
3277 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
3278 if (kerr != KCGI_OK)
3279 goto done;
3280 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3281 if (kerr != KCGI_OK)
3282 goto done;
3284 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3285 KATTR_ID, "tag_info", KATTR__MAX);
3286 if (kerr != KCGI_OK)
3287 goto done;
3288 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
3289 if (kerr != KCGI_OK)
3290 goto done;
3291 break;
3292 default:
3293 break;
3295 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3296 if (kerr != KCGI_OK)
3297 goto done;
3299 if (limit && --limit == 0)
3300 chk_next = 1;
3302 if (tag)
3303 got_object_tag_close(tag);
3304 tag = NULL;
3305 free(id_str);
3306 id_str = NULL;
3307 free(age);
3308 age = NULL;
3309 free(tag_commit0);
3310 tag_commit0 = NULL;
3311 free(href_tag);
3312 href_tag = NULL;
3313 free(href_briefs);
3314 href_briefs = NULL;
3315 free(href_commits);
3316 href_commits = NULL;
3318 if (tag_count == 0) {
3319 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3320 "summary_tags_title_wrapper", KATTR__MAX);
3321 if (kerr != KCGI_OK)
3322 goto done;
3323 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3324 "summary_tags_title", KATTR__MAX);
3325 if (kerr != KCGI_OK)
3326 goto done;
3327 kerr = khtml_puts(gw_trans->gw_html_req, "Tags");
3328 if (kerr != KCGI_OK)
3329 goto done;
3330 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3331 if (kerr != KCGI_OK)
3332 goto done;
3333 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3334 "summary_tags_content", KATTR__MAX);
3335 if (kerr != KCGI_OK)
3336 goto done;
3337 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3338 "tags_info", KATTR__MAX);
3339 if (kerr != KCGI_OK)
3340 goto done;
3341 kerr = khttp_puts(gw_trans->gw_req,
3342 "There are no tags for this repo.");
3343 if (kerr != KCGI_OK)
3344 goto done;
3345 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3347 done:
3348 if (tag)
3349 got_object_tag_close(tag);
3350 free(id_str);
3351 free(age);
3352 free(tag_commit0);
3353 free(href_tag);
3354 free(href_briefs);
3355 free(href_commits);
3356 got_ref_list_free(&refs);
3357 if (error == NULL && kerr != KCGI_OK)
3358 error = gw_kcgi_error(kerr);
3359 return error;
3362 static void
3363 gw_free_header(struct gw_header *header)
3365 free(header->path);
3366 free(header->author);
3367 free(header->committer);
3368 free(header->refs_str);
3369 free(header->commit_id);
3370 free(header->parent_id);
3371 free(header->tree_id);
3372 free(header->commit_msg);
3375 static struct gw_header *
3376 gw_init_header()
3378 struct gw_header *header;
3380 header = malloc(sizeof(*header));
3381 if (header == NULL)
3382 return NULL;
3384 header->path = NULL;
3385 SIMPLEQ_INIT(&header->refs);
3387 header->refs_str = NULL;
3388 header->commit_id = NULL;
3389 header->committer = NULL;
3390 header->author = NULL;
3391 header->parent_id = NULL;
3392 header->tree_id = NULL;
3393 header->commit_msg = NULL;
3395 return header;
3398 static const struct got_error *
3399 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
3400 int limit, struct got_object_id *id)
3402 const struct got_error *error = NULL;
3403 struct got_commit_graph *graph = NULL;
3404 struct got_commit_object *commit = NULL;
3405 int chk_next = 0, chk_multi = 0, prev_set = 0;
3407 error = got_commit_graph_open(&graph, header->path, 0);
3408 if (error)
3409 return error;
3411 error = got_commit_graph_iter_start(graph, id, gw_trans->repo, NULL,
3412 NULL);
3413 if (error)
3414 goto done;
3416 for (;;) {
3417 error = got_commit_graph_iter_next(&id, graph, gw_trans->repo,
3418 NULL, NULL);
3419 if (error) {
3420 if (error->code == GOT_ERR_ITER_COMPLETED)
3421 error = NULL;
3422 goto done;
3424 if (id == NULL)
3425 goto done;
3427 error = got_object_open_as_commit(&commit, gw_trans->repo, id);
3428 if (error)
3429 goto done;
3430 if (limit == 1 && chk_multi == 0 &&
3431 gw_trans->gw_conf->got_max_commits_display != 1) {
3432 error = gw_get_commit(gw_trans, header, commit, id);
3433 if (error)
3434 goto done;
3435 } else {
3436 chk_multi = 1;
3437 struct gw_header *n_header = NULL;
3438 if ((n_header = gw_init_header()) == NULL) {
3439 error = got_error_from_errno("malloc");
3440 goto done;
3442 error = got_ref_list(&n_header->refs, gw_trans->repo,
3443 NULL, got_ref_cmp_by_name, NULL);
3444 if (error)
3445 goto done;
3447 error = gw_get_commit(gw_trans, n_header, commit, id);
3448 if (error)
3449 goto done;
3450 got_ref_list_free(&n_header->refs);
3453 * we have a commit_id now, so copy it to next_prev_id
3454 * for navigation through gw_briefs and gw_commits
3456 if (gw_trans->next_prev_id == NULL && prev_set == 0 &&
3457 (gw_trans->action == GW_BRIEFS ||
3458 gw_trans->action == GW_COMMITS ||
3459 gw_trans->action == GW_SUMMARY)) {
3460 prev_set = 1;
3461 gw_trans->next_prev_id =
3462 strdup(n_header->commit_id);
3463 if (gw_trans->next_prev_id == NULL) {
3464 error = got_error_from_errno("strdup");
3465 goto done;
3470 * check for one more commit before breaking,
3471 * so we know whether to navicate through gw_briefs
3472 * gw_commits and gw_summary
3474 if (chk_next && (gw_trans->action == GW_BRIEFS||
3475 gw_trans->action == GW_COMMITS ||
3476 gw_trans->action == GW_SUMMARY)) {
3477 gw_trans->next_id = strdup(n_header->commit_id);
3478 if (gw_trans->next_id == NULL)
3479 error = got_error_from_errno("strdup");
3480 goto done;
3483 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3484 entry);
3486 if (error || (limit && --limit == 0)) {
3487 if (chk_multi == 0)
3488 break;
3489 chk_next = 1;
3492 done:
3493 if (commit != NULL)
3494 got_object_commit_close(commit);
3495 if (graph)
3496 got_commit_graph_close(graph);
3497 return error;
3500 static const struct got_error *
3501 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header,
3502 struct got_commit_object *commit, struct got_object_id *id)
3504 const struct got_error *error = NULL;
3505 struct got_reflist_entry *re;
3506 struct got_object_id *id2 = NULL;
3507 struct got_object_qid *parent_id;
3508 char *commit_msg = NULL, *commit_msg0;
3510 /*print commit*/
3511 SIMPLEQ_FOREACH(re, &header->refs, entry) {
3512 char *s;
3513 const char *name;
3514 struct got_tag_object *tag = NULL;
3515 int cmp;
3517 if (got_ref_is_symbolic(re->ref))
3518 continue;
3520 name = got_ref_get_name(re->ref);
3521 if (strncmp(name, "refs/", 5) == 0)
3522 name += 5;
3523 if (strncmp(name, "got/", 4) == 0)
3524 continue;
3525 if (strncmp(name, "heads/", 6) == 0)
3526 name += 6;
3527 if (strncmp(name, "remotes/", 8) == 0)
3528 name += 8;
3529 if (strncmp(name, "tags/", 5) == 0) {
3530 error = got_object_open_as_tag(&tag, gw_trans->repo,
3531 re->id);
3532 if (error) {
3533 if (error->code != GOT_ERR_OBJ_TYPE)
3534 continue;
3536 * Ref points at something other
3537 * than a tag.
3539 error = NULL;
3540 tag = NULL;
3543 cmp = got_object_id_cmp(tag ?
3544 got_object_tag_get_object_id(tag) : re->id, id);
3545 if (tag)
3546 got_object_tag_close(tag);
3547 if (cmp != 0)
3548 continue;
3549 s = header->refs_str;
3550 if (asprintf(&header->refs_str, "%s%s%s", s ? s : "",
3551 s ? ", " : "", name) == -1) {
3552 error = got_error_from_errno("asprintf");
3553 free(s);
3554 header->refs_str = NULL;
3555 return error;
3557 free(s);
3560 error = got_object_id_str(&header->commit_id, id);
3561 if (error)
3562 return error;
3564 error = got_object_id_str(&header->tree_id,
3565 got_object_commit_get_tree_id(commit));
3566 if (error)
3567 return error;
3569 if (gw_trans->action == GW_DIFF) {
3570 parent_id = SIMPLEQ_FIRST(
3571 got_object_commit_get_parent_ids(commit));
3572 if (parent_id != NULL) {
3573 id2 = got_object_id_dup(parent_id->id);
3574 free (parent_id);
3575 error = got_object_id_str(&header->parent_id, id2);
3576 if (error)
3577 return error;
3578 free(id2);
3579 } else {
3580 header->parent_id = strdup("/dev/null");
3581 if (header->parent_id == NULL) {
3582 error = got_error_from_errno("strdup");
3583 return error;
3588 header->committer_time =
3589 got_object_commit_get_committer_time(commit);
3591 header->author =
3592 strdup(got_object_commit_get_author(commit));
3593 if (header->author == NULL) {
3594 error = got_error_from_errno("strdup");
3595 return error;
3597 header->committer =
3598 strdup(got_object_commit_get_committer(commit));
3599 if (header->committer == NULL) {
3600 error = got_error_from_errno("strdup");
3601 return error;
3603 error = got_object_commit_get_logmsg(&commit_msg0, commit);
3604 if (error)
3605 return error;
3607 commit_msg = commit_msg0;
3608 while (*commit_msg == '\n')
3609 commit_msg++;
3611 header->commit_msg = strdup(commit_msg);
3612 if (header->commit_msg == NULL)
3613 error = got_error_from_errno("strdup");
3614 free(commit_msg0);
3615 return error;
3618 static const struct got_error *
3619 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3621 const struct got_error *error = NULL;
3622 char *in_repo_path = NULL;
3623 struct got_object_id *id = NULL;
3625 error = got_repo_open(&gw_trans->repo, gw_trans->repo_path, NULL);
3626 if (error)
3627 return error;
3629 if (gw_trans->commit_id == NULL) {
3630 struct got_reference *head_ref;
3631 error = got_ref_open(&head_ref, gw_trans->repo,
3632 gw_trans->headref, 0);
3633 if (error)
3634 return error;
3636 error = got_ref_resolve(&id, gw_trans->repo, head_ref);
3637 got_ref_close(head_ref);
3638 if (error)
3639 return error;
3640 } else {
3641 struct got_reference *ref;
3643 error = got_ref_open(&ref, gw_trans->repo,
3644 gw_trans->commit_id, 0);
3645 if (error == NULL) {
3646 int obj_type;
3647 error = got_ref_resolve(&id, gw_trans->repo, ref);
3648 got_ref_close(ref);
3649 if (error)
3650 return error;
3651 error = got_object_get_type(&obj_type, gw_trans->repo,
3652 id);
3653 if (error)
3654 goto done;
3655 if (obj_type == GOT_OBJ_TYPE_TAG) {
3656 struct got_tag_object *tag;
3657 error = got_object_open_as_tag(&tag,
3658 gw_trans->repo, id);
3659 if (error)
3660 goto done;
3661 if (got_object_tag_get_object_type(tag) !=
3662 GOT_OBJ_TYPE_COMMIT) {
3663 got_object_tag_close(tag);
3664 error = got_error(GOT_ERR_OBJ_TYPE);
3665 goto done;
3667 free(id);
3668 id = got_object_id_dup(
3669 got_object_tag_get_object_id(tag));
3670 if (id == NULL)
3671 error = got_error_from_errno(
3672 "got_object_id_dup");
3673 got_object_tag_close(tag);
3674 if (error)
3675 goto done;
3676 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3677 error = got_error(GOT_ERR_OBJ_TYPE);
3678 goto done;
3681 error = got_repo_match_object_id_prefix(&id,
3682 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT,
3683 gw_trans->repo);
3684 if (error)
3685 goto done;
3688 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
3689 gw_trans->repo_path, 1);
3690 if (error)
3691 goto done;
3693 if (in_repo_path) {
3694 header->path = strdup(in_repo_path);
3695 if (header->path == NULL) {
3696 error = got_error_from_errno("strdup");
3697 goto done;
3701 error = got_ref_list(&header->refs, gw_trans->repo, NULL,
3702 got_ref_cmp_by_name, NULL);
3703 if (error)
3704 goto done;
3706 error = gw_get_commits(gw_trans, header, limit, id);
3707 done:
3708 got_ref_list_free(&header->refs);
3709 free(id);
3710 free(in_repo_path);
3711 return error;
3714 struct blame_line {
3715 int annotated;
3716 char *id_str;
3717 char *committer;
3718 char datebuf[11]; /* YYYY-MM-DD + NUL */
3721 struct gw_blame_cb_args {
3722 struct blame_line *lines;
3723 int nlines;
3724 int nlines_prec;
3725 int lineno_cur;
3726 off_t *line_offsets;
3727 FILE *f;
3728 struct got_repository *repo;
3729 struct gw_trans *gw_trans;
3732 static const struct got_error *
3733 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3735 const struct got_error *err = NULL;
3736 struct gw_blame_cb_args *a = arg;
3737 struct blame_line *bline;
3738 char *line = NULL;
3739 size_t linesize = 0;
3740 struct got_commit_object *commit = NULL;
3741 off_t offset;
3742 struct tm tm;
3743 time_t committer_time;
3744 enum kcgi_err kerr = KCGI_OK;
3746 if (nlines != a->nlines ||
3747 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3748 return got_error(GOT_ERR_RANGE);
3750 if (lineno == -1)
3751 return NULL; /* no change in this commit */
3753 /* Annotate this line. */
3754 bline = &a->lines[lineno - 1];
3755 if (bline->annotated)
3756 return NULL;
3757 err = got_object_id_str(&bline->id_str, id);
3758 if (err)
3759 return err;
3761 err = got_object_open_as_commit(&commit, a->repo, id);
3762 if (err)
3763 goto done;
3765 bline->committer = strdup(got_object_commit_get_committer(commit));
3766 if (bline->committer == NULL) {
3767 err = got_error_from_errno("strdup");
3768 goto done;
3771 committer_time = got_object_commit_get_committer_time(commit);
3772 if (localtime_r(&committer_time, &tm) == NULL)
3773 return got_error_from_errno("localtime_r");
3774 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3775 &tm) >= sizeof(bline->datebuf)) {
3776 err = got_error(GOT_ERR_NO_SPACE);
3777 goto done;
3779 bline->annotated = 1;
3781 /* Print lines annotated so far. */
3782 bline = &a->lines[a->lineno_cur - 1];
3783 if (!bline->annotated)
3784 goto done;
3786 offset = a->line_offsets[a->lineno_cur - 1];
3787 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3788 err = got_error_from_errno("fseeko");
3789 goto done;
3792 while (bline->annotated) {
3793 char *smallerthan, *at, *nl, *committer;
3794 char *href_diff = NULL;
3795 size_t len;
3797 if (getline(&line, &linesize, a->f) == -1) {
3798 if (ferror(a->f))
3799 err = got_error_from_errno("getline");
3800 break;
3803 committer = bline->committer;
3804 smallerthan = strchr(committer, '<');
3805 if (smallerthan && smallerthan[1] != '\0')
3806 committer = smallerthan + 1;
3807 at = strchr(committer, '@');
3808 if (at)
3809 *at = '\0';
3810 len = strlen(committer);
3811 if (len >= 9)
3812 committer[8] = '\0';
3814 nl = strchr(line, '\n');
3815 if (nl)
3816 *nl = '\0';
3818 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3819 "blame_wrapper", KATTR__MAX);
3820 if (kerr != KCGI_OK)
3821 goto err;
3822 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3823 "blame_number", KATTR__MAX);
3824 if (kerr != KCGI_OK)
3825 goto err;
3826 kerr = khtml_printf(a->gw_trans->gw_html_req, "%.*d",
3827 a->nlines_prec, a->lineno_cur);
3828 if (kerr != KCGI_OK)
3829 goto err;
3830 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3831 if (kerr != KCGI_OK)
3832 goto err;
3834 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3835 "blame_hash", KATTR__MAX);
3836 if (kerr != KCGI_OK)
3837 goto err;
3839 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
3840 a->gw_trans->repo_name, "action", "diff", "commit",
3841 bline->id_str, NULL);
3842 if (href_diff == NULL) {
3843 err = got_error_from_errno("khttp_urlpart");
3844 goto done;
3846 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3847 KATTR_HREF, href_diff, KATTR__MAX);
3848 if (kerr != KCGI_OK)
3849 goto done;
3850 kerr = khtml_printf(a->gw_trans->gw_html_req, "%.8s",
3851 bline->id_str);
3852 if (kerr != KCGI_OK)
3853 goto err;
3854 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3855 if (kerr != KCGI_OK)
3856 goto err;
3858 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3859 "blame_date", KATTR__MAX);
3860 if (kerr != KCGI_OK)
3861 goto err;
3862 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3863 if (kerr != KCGI_OK)
3864 goto err;
3865 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3866 if (kerr != KCGI_OK)
3867 goto err;
3869 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3870 "blame_author", KATTR__MAX);
3871 if (kerr != KCGI_OK)
3872 goto err;
3873 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
3874 if (kerr != KCGI_OK)
3875 goto err;
3876 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3877 if (kerr != KCGI_OK)
3878 goto err;
3880 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3881 "blame_code", KATTR__MAX);
3882 if (kerr != KCGI_OK)
3883 goto err;
3884 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
3885 if (kerr != KCGI_OK)
3886 goto err;
3887 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3888 if (kerr != KCGI_OK)
3889 goto err;
3891 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3892 if (kerr != KCGI_OK)
3893 goto err;
3895 a->lineno_cur++;
3896 bline = &a->lines[a->lineno_cur - 1];
3897 err:
3898 free(href_diff);
3900 done:
3901 if (commit)
3902 got_object_commit_close(commit);
3903 free(line);
3904 if (err == NULL && kerr != KCGI_OK)
3905 err = gw_kcgi_error(kerr);
3906 return err;
3909 static const struct got_error *
3910 gw_output_file_blame(struct gw_trans *gw_trans)
3912 const struct got_error *error = NULL;
3913 struct got_object_id *obj_id = NULL;
3914 struct got_object_id *commit_id = NULL;
3915 struct got_blob_object *blob = NULL;
3916 char *path = NULL, *in_repo_path = NULL;
3917 struct gw_blame_cb_args bca;
3918 int i, obj_type;
3919 size_t filesize;
3921 if (asprintf(&path, "%s%s%s",
3922 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3923 gw_trans->repo_folder ? "/" : "",
3924 gw_trans->repo_file) == -1) {
3925 error = got_error_from_errno("asprintf");
3926 goto done;
3929 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path, 1);
3930 if (error)
3931 goto done;
3933 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
3934 GOT_OBJ_TYPE_COMMIT, 1, gw_trans->repo);
3935 if (error)
3936 goto done;
3938 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
3939 in_repo_path);
3940 if (error)
3941 goto done;
3943 if (obj_id == NULL) {
3944 error = got_error(GOT_ERR_NO_OBJ);
3945 goto done;
3948 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
3949 if (error)
3950 goto done;
3952 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3953 error = got_error(GOT_ERR_OBJ_TYPE);
3954 goto done;
3957 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
3958 if (error)
3959 goto done;
3961 bca.f = got_opentemp();
3962 if (bca.f == NULL) {
3963 error = got_error_from_errno("got_opentemp");
3964 goto done;
3966 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3967 &bca.line_offsets, bca.f, blob);
3968 if (error || bca.nlines == 0)
3969 goto done;
3971 /* Don't include \n at EOF in the blame line count. */
3972 if (bca.line_offsets[bca.nlines - 1] == filesize)
3973 bca.nlines--;
3975 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3976 if (bca.lines == NULL) {
3977 error = got_error_from_errno("calloc");
3978 goto done;
3980 bca.lineno_cur = 1;
3981 bca.nlines_prec = 0;
3982 i = bca.nlines;
3983 while (i > 0) {
3984 i /= 10;
3985 bca.nlines_prec++;
3987 bca.repo = gw_trans->repo;
3988 bca.gw_trans = gw_trans;
3990 error = got_blame(in_repo_path, commit_id, gw_trans->repo, gw_blame_cb,
3991 &bca, NULL, NULL);
3992 done:
3993 free(in_repo_path);
3994 free(commit_id);
3995 free(obj_id);
3996 free(path);
3998 if (blob) {
3999 free(bca.line_offsets);
4000 for (i = 0; i < bca.nlines; i++) {
4001 struct blame_line *bline = &bca.lines[i];
4002 free(bline->id_str);
4003 free(bline->committer);
4005 free(bca.lines);
4006 if (bca.f && fclose(bca.f) == EOF && error == NULL)
4007 error = got_error_from_errno("fclose");
4009 if (blob)
4010 got_object_blob_close(blob);
4011 return error;
4014 static const struct got_error *
4015 gw_output_blob_buf(struct gw_trans *gw_trans)
4017 const struct got_error *error = NULL;
4018 struct got_object_id *obj_id = NULL;
4019 struct got_object_id *commit_id = NULL;
4020 struct got_blob_object *blob = NULL;
4021 char *path = NULL, *in_repo_path = NULL;
4022 int obj_type, set_mime = 0;
4023 size_t len, hdrlen;
4024 const uint8_t *buf;
4025 enum kcgi_err kerr = KCGI_OK;
4027 if (asprintf(&path, "%s%s%s",
4028 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4029 gw_trans->repo_folder ? "/" : "",
4030 gw_trans->repo_file) == -1) {
4031 error = got_error_from_errno("asprintf");
4032 goto done;
4035 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path, 1);
4036 if (error)
4037 goto done;
4039 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
4040 GOT_OBJ_TYPE_COMMIT, 1, gw_trans->repo);
4041 if (error)
4042 goto done;
4044 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
4045 in_repo_path);
4046 if (error)
4047 goto done;
4049 if (obj_id == NULL) {
4050 error = got_error(GOT_ERR_NO_OBJ);
4051 goto done;
4054 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
4055 if (error)
4056 goto done;
4058 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4059 error = got_error(GOT_ERR_OBJ_TYPE);
4060 goto done;
4063 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
4064 if (error)
4065 goto done;
4067 hdrlen = got_object_blob_get_hdrlen(blob);
4068 do {
4069 error = got_object_blob_read_block(&len, blob);
4070 if (error)
4071 goto done;
4072 buf = got_object_blob_get_read_buf(blob);
4075 * Skip blob object header first time around,
4076 * which also contains a zero byte.
4078 buf += hdrlen;
4079 if (set_mime == 0) {
4080 if (isbinary(buf, len - hdrlen))
4081 gw_trans->mime = KMIME_APP_OCTET_STREAM;
4082 else
4083 gw_trans->mime = KMIME_TEXT_PLAIN;
4084 set_mime = 1;
4085 error = gw_display_index(gw_trans);
4086 if (error)
4087 goto done;
4089 kerr = khttp_write(gw_trans->gw_req, buf, len - hdrlen);
4090 if (kerr != KCGI_OK)
4091 goto done;
4092 hdrlen = 0;
4093 } while (len != 0);
4094 done:
4095 free(in_repo_path);
4096 free(commit_id);
4097 free(obj_id);
4098 free(path);
4099 if (blob)
4100 got_object_blob_close(blob);
4101 if (error == NULL && kerr != KCGI_OK)
4102 error = gw_kcgi_error(kerr);
4103 return error;
4106 static const struct got_error *
4107 gw_output_repo_tree(struct gw_trans *gw_trans)
4109 const struct got_error *error = NULL;
4110 struct got_object_id *tree_id = NULL, *commit_id = NULL;
4111 struct got_tree_object *tree = NULL;
4112 char *path = NULL, *in_repo_path = NULL;
4113 char *id_str = NULL;
4114 char *build_folder = NULL;
4115 char *href_blob = NULL, *href_blame = NULL;
4116 const char *class = NULL;
4117 int nentries, i, class_flip = 0;
4118 enum kcgi_err kerr = KCGI_OK;
4120 if (gw_trans->repo_folder != NULL) {
4121 path = strdup(gw_trans->repo_folder);
4122 if (path == NULL) {
4123 error = got_error_from_errno("strdup");
4124 goto done;
4126 } else {
4127 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
4128 gw_trans->repo_path, 1);
4129 if (error)
4130 goto done;
4131 free(path);
4132 path = in_repo_path;
4135 if (gw_trans->commit_id == NULL) {
4136 struct got_reference *head_ref;
4137 error = got_ref_open(&head_ref, gw_trans->repo,
4138 gw_trans->headref, 0);
4139 if (error)
4140 goto done;
4141 error = got_ref_resolve(&commit_id, gw_trans->repo, head_ref);
4142 if (error)
4143 goto done;
4144 got_ref_close(head_ref);
4146 * gw_trans->commit_id was not parsed from the querystring
4147 * we hit this code path from gw_index, where we don't know the
4148 * commit values for the tree link yet, so set
4149 * gw_trans->commit_id here to continue further into the tree
4151 error = got_object_id_str(&gw_trans->commit_id, commit_id);
4152 if (error)
4153 goto done;
4155 } else {
4156 error = got_repo_match_object_id(&commit_id, NULL,
4157 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT, 1,
4158 gw_trans->repo);
4159 if (error)
4160 goto done;
4163 error = got_object_id_by_path(&tree_id, gw_trans->repo, commit_id,
4164 path);
4165 if (error)
4166 goto done;
4168 error = got_object_open_as_tree(&tree, gw_trans->repo, tree_id);
4169 if (error)
4170 goto done;
4172 nentries = got_object_tree_get_nentries(tree);
4173 for (i = 0; i < nentries; i++) {
4174 struct got_tree_entry *te;
4175 const char *modestr = "";
4176 mode_t mode;
4178 te = got_object_tree_get_entry(tree, i);
4180 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
4181 if (error)
4182 goto done;
4184 mode = got_tree_entry_get_mode(te);
4185 if (got_object_tree_entry_is_submodule(te))
4186 modestr = "$";
4187 else if (S_ISLNK(mode))
4188 modestr = "@";
4189 else if (S_ISDIR(mode))
4190 modestr = "/";
4191 else if (mode & S_IXUSR)
4192 modestr = "*";
4194 if (class_flip == 0) {
4195 class = "back_lightgray";
4196 class_flip = 1;
4197 } else {
4198 class = "back_white";
4199 class_flip = 0;
4202 if (S_ISDIR(mode)) {
4203 if (asprintf(&build_folder, "%s/%s",
4204 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4205 got_tree_entry_get_name(te)) == -1) {
4206 error = got_error_from_errno("asprintf");
4207 goto done;
4210 href_blob = khttp_urlpart(NULL, NULL, "gotweb", "path",
4211 gw_trans->repo_name, "action",
4212 gw_get_action_name(gw_trans), "commit",
4213 gw_trans->commit_id, "folder", build_folder, NULL);
4214 if (href_blob == NULL) {
4215 error = got_error_from_errno("khttp_urlpart");
4216 goto done;
4218 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4219 KATTR_ID, "tree_wrapper", KATTR__MAX);
4220 if (kerr != KCGI_OK)
4221 goto done;
4222 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4223 KATTR_ID, "tree_line", KATTR_CLASS, class,
4224 KATTR__MAX);
4225 if (kerr != KCGI_OK)
4226 goto done;
4227 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4228 KATTR_HREF, href_blob, KATTR_CLASS,
4229 "diff_directory", KATTR__MAX);
4230 if (kerr != KCGI_OK)
4231 goto done;
4232 kerr = khtml_printf(gw_trans->gw_html_req, "%s%s",
4233 got_tree_entry_get_name(te), modestr);
4234 if (kerr != KCGI_OK)
4235 goto done;
4236 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4237 if (kerr != KCGI_OK)
4238 goto done;
4239 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4240 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
4241 KATTR__MAX);
4242 if (kerr != KCGI_OK)
4243 goto done;
4244 kerr = khtml_entity(gw_trans->gw_html_req,
4245 KENTITY_nbsp);
4246 if (kerr != KCGI_OK)
4247 goto done;
4248 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4249 if (kerr != KCGI_OK)
4250 goto done;
4251 } else {
4252 href_blob = khttp_urlpart(NULL, NULL, "gotweb", "path",
4253 gw_trans->repo_name, "action", "blob", "commit",
4254 gw_trans->commit_id, "file",
4255 got_tree_entry_get_name(te), "folder",
4256 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4257 NULL);
4258 if (href_blob == NULL) {
4259 error = got_error_from_errno("khttp_urlpart");
4260 goto done;
4262 href_blame = khttp_urlpart(NULL, NULL, "gotweb", "path",
4263 gw_trans->repo_name, "action", "blame", "commit",
4264 gw_trans->commit_id, "file",
4265 got_tree_entry_get_name(te), "folder",
4266 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4267 NULL);
4268 if (href_blame == NULL) {
4269 error = got_error_from_errno("khttp_urlpart");
4270 goto done;
4272 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4273 KATTR_ID, "tree_wrapper", KATTR__MAX);
4274 if (kerr != KCGI_OK)
4275 goto done;
4276 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4277 KATTR_ID, "tree_line", KATTR_CLASS, class,
4278 KATTR__MAX);
4279 if (kerr != KCGI_OK)
4280 goto done;
4281 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4282 KATTR_HREF, href_blob, KATTR__MAX);
4283 if (kerr != KCGI_OK)
4284 goto done;
4285 kerr = khtml_printf(gw_trans->gw_html_req, "%s%s",
4286 got_tree_entry_get_name(te), modestr);
4287 if (kerr != KCGI_OK)
4288 goto done;
4289 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4290 if (kerr != KCGI_OK)
4291 goto done;
4292 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4293 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
4294 KATTR__MAX);
4295 if (kerr != KCGI_OK)
4296 goto done;
4298 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4299 KATTR_HREF, href_blob, KATTR__MAX);
4300 if (kerr != KCGI_OK)
4301 goto done;
4302 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
4303 if (kerr != KCGI_OK)
4304 goto done;
4305 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4306 if (kerr != KCGI_OK)
4307 goto done;
4309 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4310 if (kerr != KCGI_OK)
4311 goto done;
4313 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4314 KATTR_HREF, href_blame, KATTR__MAX);
4315 if (kerr != KCGI_OK)
4316 goto done;
4317 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
4318 if (kerr != KCGI_OK)
4319 goto done;
4321 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4322 if (kerr != KCGI_OK)
4323 goto done;
4325 free(id_str);
4326 id_str = NULL;
4327 free(href_blob);
4328 href_blob = NULL;
4329 free(build_folder);
4330 build_folder = NULL;
4332 done:
4333 if (tree)
4334 got_object_tree_close(tree);
4335 free(id_str);
4336 free(href_blob);
4337 free(href_blame);
4338 free(in_repo_path);
4339 free(tree_id);
4340 free(build_folder);
4341 if (error == NULL && kerr != KCGI_OK)
4342 error = gw_kcgi_error(kerr);
4343 return error;
4346 static const struct got_error *
4347 gw_output_repo_heads(struct gw_trans *gw_trans)
4349 const struct got_error *error = NULL;
4350 struct got_reflist_head refs;
4351 struct got_reflist_entry *re;
4352 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
4353 char *href_commits = NULL;
4354 enum kcgi_err kerr = KCGI_OK;
4356 SIMPLEQ_INIT(&refs);
4358 error = got_ref_list(&refs, gw_trans->repo, "refs/heads",
4359 got_ref_cmp_by_name, NULL);
4360 if (error)
4361 goto done;
4363 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4364 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
4365 if (kerr != KCGI_OK)
4366 goto done;
4367 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4368 KATTR_ID, "summary_heads_title", KATTR__MAX);
4369 if (kerr != KCGI_OK)
4370 goto done;
4371 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
4372 if (kerr != KCGI_OK)
4373 goto done;
4374 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4375 if (kerr != KCGI_OK)
4376 goto done;
4377 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4378 KATTR_ID, "summary_heads_content", KATTR__MAX);
4379 if (kerr != KCGI_OK)
4380 goto done;
4382 SIMPLEQ_FOREACH(re, &refs, entry) {
4383 const char *refname;
4385 if (got_ref_is_symbolic(re->ref))
4386 continue;
4388 refname = got_ref_get_name(re->ref);
4389 if (strncmp(refname, "refs/heads/", 11) != 0)
4390 continue;
4392 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
4393 refname, TM_DIFF);
4394 if (error)
4395 goto done;
4397 if (strncmp(refname, "refs/heads/", 11) == 0)
4398 refname += 11;
4400 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4401 KATTR_ID, "heads_wrapper", KATTR__MAX);
4402 if (kerr != KCGI_OK)
4403 goto done;
4404 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4405 KATTR_ID, "heads_age", KATTR__MAX);
4406 if (kerr != KCGI_OK)
4407 goto done;
4408 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
4409 if (kerr != KCGI_OK)
4410 goto done;
4411 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4412 if (kerr != KCGI_OK)
4413 goto done;
4414 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4415 KATTR_ID, "heads_space", KATTR__MAX);
4416 if (kerr != KCGI_OK)
4417 goto done;
4418 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
4419 if (kerr != KCGI_OK)
4420 goto done;
4421 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4422 if (kerr != KCGI_OK)
4423 goto done;
4424 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4425 KATTR_ID, "head", KATTR__MAX);
4426 if (kerr != KCGI_OK)
4427 goto done;
4429 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
4430 gw_trans->repo_name, "action", "summary", "headref",
4431 refname, NULL);
4432 if (href_summary == NULL) {
4433 error = got_error_from_errno("khttp_urlpart");
4434 goto done;
4436 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4437 href_summary, KATTR__MAX);
4438 kerr = khtml_puts(gw_trans->gw_html_req, refname);
4439 if (kerr != KCGI_OK)
4440 goto done;
4441 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4442 if (kerr != KCGI_OK)
4443 goto done;
4445 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4446 "navs_wrapper", KATTR__MAX);
4447 if (kerr != KCGI_OK)
4448 goto done;
4449 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4450 "navs", KATTR__MAX);
4451 if (kerr != KCGI_OK)
4452 goto done;
4454 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4455 href_summary, KATTR__MAX);
4456 if (kerr != KCGI_OK)
4457 goto done;
4458 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4459 if (kerr != KCGI_OK)
4460 goto done;
4461 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4462 if (kerr != KCGI_OK)
4463 goto done;
4465 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4466 if (kerr != KCGI_OK)
4467 goto done;
4469 href_briefs = khttp_urlpart(NULL, NULL, "gotweb", "path",
4470 gw_trans->repo_name, "action", "briefs", "headref",
4471 refname, NULL);
4472 if (href_briefs == NULL) {
4473 error = got_error_from_errno("khttp_urlpart");
4474 goto done;
4476 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4477 href_briefs, KATTR__MAX);
4478 if (kerr != KCGI_OK)
4479 goto done;
4480 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4481 if (kerr != KCGI_OK)
4482 goto done;
4483 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4484 if (kerr != KCGI_OK)
4485 goto done;
4487 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4488 if (kerr != KCGI_OK)
4489 goto done;
4491 href_commits = khttp_urlpart(NULL, NULL, "gotweb", "path",
4492 gw_trans->repo_name, "action", "commits", "headref",
4493 refname, NULL);
4494 if (href_commits == NULL) {
4495 error = got_error_from_errno("khttp_urlpart");
4496 goto done;
4498 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4499 href_commits, KATTR__MAX);
4500 if (kerr != KCGI_OK)
4501 goto done;
4502 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4503 if (kerr != KCGI_OK)
4504 goto done;
4505 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4506 if (kerr != KCGI_OK)
4507 goto done;
4509 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4510 "dotted_line", KATTR__MAX);
4511 if (kerr != KCGI_OK)
4512 goto done;
4513 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4514 if (kerr != KCGI_OK)
4515 goto done;
4516 free(href_summary);
4517 href_summary = NULL;
4518 free(href_briefs);
4519 href_briefs = NULL;
4520 free(href_commits);
4521 href_commits = NULL;
4523 done:
4524 got_ref_list_free(&refs);
4525 free(href_summary);
4526 free(href_briefs);
4527 free(href_commits);
4528 return error;
4531 static const struct got_error *
4532 gw_output_site_link(struct gw_trans *gw_trans)
4534 const struct got_error *error = NULL;
4535 char *href_summary = NULL;
4536 enum kcgi_err kerr = KCGI_OK;
4538 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4539 "site_link", KATTR__MAX);
4540 if (kerr != KCGI_OK)
4541 goto done;
4542 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4543 KATTR__MAX);
4544 if (kerr != KCGI_OK)
4545 goto done;
4546 kerr = khtml_puts(gw_trans->gw_html_req,
4547 gw_trans->gw_conf->got_site_link);
4548 if (kerr != KCGI_OK)
4549 goto done;
4550 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4551 if (kerr != KCGI_OK)
4552 goto done;
4554 if (gw_trans->repo_name != NULL) {
4555 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4556 if (kerr != KCGI_OK)
4557 goto done;
4559 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
4560 gw_trans->repo_name, "action", "summary", NULL);
4561 if (href_summary == NULL) {
4562 error = got_error_from_errno("khttp_urlpart");
4563 goto done;
4565 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4566 href_summary, KATTR__MAX);
4567 if (kerr != KCGI_OK)
4568 goto done;
4569 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4570 if (kerr != KCGI_OK)
4571 goto done;
4572 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4573 if (kerr != KCGI_OK)
4574 goto done;
4575 kerr = khtml_printf(gw_trans->gw_html_req, " / %s",
4576 gw_get_action_name(gw_trans));
4577 if (kerr != KCGI_OK)
4578 goto done;
4581 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4582 if (kerr != KCGI_OK)
4583 goto done;
4584 done:
4585 free(href_summary);
4586 if (error == NULL && kerr != KCGI_OK)
4587 error = gw_kcgi_error(kerr);
4588 return error;
4591 static const struct got_error *
4592 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4594 const struct got_error *error = NULL;
4595 char *color = NULL;
4596 enum kcgi_err kerr = KCGI_OK;
4598 if (strncmp(buf, "-", 1) == 0)
4599 color = "diff_minus";
4600 else if (strncmp(buf, "+", 1) == 0)
4601 color = "diff_plus";
4602 else if (strncmp(buf, "@@", 2) == 0)
4603 color = "diff_chunk_header";
4604 else if (strncmp(buf, "@@", 2) == 0)
4605 color = "diff_chunk_header";
4606 else if (strncmp(buf, "commit +", 8) == 0)
4607 color = "diff_meta";
4608 else if (strncmp(buf, "commit -", 8) == 0)
4609 color = "diff_meta";
4610 else if (strncmp(buf, "blob +", 6) == 0)
4611 color = "diff_meta";
4612 else if (strncmp(buf, "blob -", 6) == 0)
4613 color = "diff_meta";
4614 else if (strncmp(buf, "file +", 6) == 0)
4615 color = "diff_meta";
4616 else if (strncmp(buf, "file -", 6) == 0)
4617 color = "diff_meta";
4618 else if (strncmp(buf, "from:", 5) == 0)
4619 color = "diff_author";
4620 else if (strncmp(buf, "via:", 4) == 0)
4621 color = "diff_author";
4622 else if (strncmp(buf, "date:", 5) == 0)
4623 color = "diff_date";
4624 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4625 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4626 if (error == NULL && kerr != KCGI_OK)
4627 error = gw_kcgi_error(kerr);
4628 return error;
4631 int
4632 main(int argc, char *argv[])
4634 const struct got_error *error = NULL;
4635 struct gw_trans *gw_trans;
4636 struct gw_dir *dir = NULL, *tdir;
4637 const char *page = "index";
4638 int gw_malloc = 1;
4639 enum kcgi_err kerr = KCGI_OK;
4641 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4642 errx(1, "malloc");
4644 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4645 errx(1, "malloc");
4647 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4648 errx(1, "malloc");
4650 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4651 errx(1, "malloc");
4653 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4654 if (kerr != KCGI_OK) {
4655 error = gw_kcgi_error(kerr);
4656 goto done;
4659 TAILQ_INIT(&gw_trans->gw_dirs);
4660 TAILQ_INIT(&gw_trans->gw_headers);
4662 gw_trans->action = -1;
4663 gw_trans->page = 0;
4664 gw_trans->repos_total = 0;
4665 gw_trans->repo_path = NULL;
4666 gw_trans->commit_id = NULL;
4667 gw_trans->next_id = NULL;
4668 gw_trans->next_prev_id = NULL;
4669 gw_trans->prev_id = NULL;
4670 gw_trans->prev_prev_id = NULL;
4671 gw_trans->headref = GOT_REF_HEAD;
4672 gw_trans->mime = KMIME_TEXT_HTML;
4673 gw_trans->gw_tmpl->key = gw_templs;
4674 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4675 gw_trans->gw_tmpl->arg = gw_trans;
4676 gw_trans->gw_tmpl->cb = gw_template;
4678 error = parse_gotweb_config(&gw_trans->gw_conf, GOTWEB_CONF);
4679 if (error)
4680 goto done;
4682 error = gw_parse_querystring(gw_trans);
4683 if (error)
4684 goto done;
4686 if (gw_trans->action == GW_BLOB)
4687 error = gw_blob(gw_trans);
4688 else
4689 error = gw_display_index(gw_trans);
4690 done:
4691 if (gw_malloc) {
4692 free(gw_trans->gw_conf->got_repos_path);
4693 free(gw_trans->gw_conf->got_site_name);
4694 free(gw_trans->gw_conf->got_site_owner);
4695 free(gw_trans->gw_conf->got_site_link);
4696 free(gw_trans->gw_conf->got_logo);
4697 free(gw_trans->gw_conf->got_logo_url);
4698 free(gw_trans->gw_conf);
4699 free(gw_trans->commit_id);
4700 free(gw_trans->next_id);
4701 free(gw_trans->next_prev_id);
4702 free(gw_trans->prev_id);
4703 free(gw_trans->prev_prev_id);
4704 free(gw_trans->repo_path);
4705 if (gw_trans->repo)
4706 got_repo_close(gw_trans->repo);
4708 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4709 free(dir->name);
4710 free(dir->description);
4711 free(dir->age);
4712 free(dir->url);
4713 free(dir->path);
4714 free(dir);
4719 khttp_free(gw_trans->gw_req);
4720 return 0;