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 *prev_id;
71 const char *repo_file;
72 char *repo_folder;
73 const char *headref;
74 unsigned int action;
75 unsigned int page;
76 unsigned int repos_total;
77 enum kmime mime;
78 };
80 struct gw_header {
81 TAILQ_ENTRY(gw_header) entry;
82 struct got_reflist_head refs;
83 char *path;
85 char *refs_str;
86 char *commit_id; /* id_str1 */
87 char *parent_id; /* id_str2 */
88 char *tree_id;
89 char *author;
90 char *committer;
91 char *commit_msg;
92 time_t committer_time;
93 };
95 struct gw_dir {
96 TAILQ_ENTRY(gw_dir) entry;
97 char *name;
98 char *owner;
99 char *description;
100 char *url;
101 char *age;
102 char *path;
103 };
105 enum gw_key {
106 KEY_ACTION,
107 KEY_COMMIT_ID,
108 KEY_FILE,
109 KEY_FOLDER,
110 KEY_HEADREF,
111 KEY_PAGE,
112 KEY_PATH,
113 KEY_PREV_ID,
114 KEY__ZMAX
115 };
117 enum gw_tmpl {
118 TEMPL_CONTENT,
119 TEMPL_HEAD,
120 TEMPL_HEADER,
121 TEMPL_SEARCH,
122 TEMPL_SITEPATH,
123 TEMPL_SITEOWNER,
124 TEMPL_TITLE,
125 TEMPL__MAX
126 };
128 enum gw_ref_tm {
129 TM_DIFF,
130 TM_LONG,
131 };
133 enum gw_tags_type {
134 TAGBRIEF,
135 TAGFULL,
136 };
138 static const char *const gw_templs[TEMPL__MAX] = {
139 "content",
140 "head",
141 "header",
142 "search",
143 "sitepath",
144 "siteowner",
145 "title",
146 };
148 static const struct kvalid gw_keys[KEY__ZMAX] = {
149 { kvalid_stringne, "action" },
150 { kvalid_stringne, "commit" },
151 { kvalid_stringne, "file" },
152 { kvalid_stringne, "folder" },
153 { kvalid_stringne, "headref" },
154 { kvalid_int, "page" },
155 { kvalid_stringne, "path" },
156 { kvalid_stringne, "prev" },
157 };
159 static struct gw_header *gw_init_header(void);
161 static void gw_free_header(struct gw_header *);
163 static int gw_template(size_t, void *);
165 static const struct got_error *gw_error(struct gw_trans *);
166 static const struct got_error *gw_init_gw_dir(struct gw_dir **, const char *);
167 static const struct got_error *gw_get_repo_description(char **,
168 struct gw_trans *, char *);
169 static const struct got_error *gw_get_repo_owner(char **, struct gw_trans *,
170 char *);
171 static const struct got_error *gw_get_time_str(char **, time_t, int);
172 static const struct got_error *gw_get_repo_age(char **, struct gw_trans *,
173 char *, const char *, int);
174 static const struct got_error *gw_output_file_blame(struct gw_trans *,
175 struct gw_header *);
176 static const struct got_error *gw_output_blob_buf(struct gw_trans *,
177 struct gw_header *);
178 static const struct got_error *gw_output_repo_tree(struct gw_trans *,
179 struct gw_header *);
180 static const struct got_error *gw_output_diff(struct gw_trans *,
181 struct gw_header *);
182 static const struct got_error *gw_output_repo_tags(struct gw_trans *,
183 struct gw_header *, int, int);
184 static const struct got_error *gw_output_repo_heads(struct gw_trans *);
185 static const struct got_error *gw_output_site_link(struct gw_trans *);
186 static const struct got_error *gw_get_clone_url(char **, struct gw_trans *,
187 char *);
188 static const struct got_error *gw_colordiff_line(struct gw_trans *, char *);
190 static const struct got_error *gw_gen_commit_header(struct gw_trans *, char *,
191 char*);
192 static const struct got_error *gw_gen_diff_header(struct gw_trans *, char *,
193 char*);
194 static const struct got_error *gw_gen_author_header(struct gw_trans *,
195 const char *);
196 static const struct got_error *gw_gen_age_header(struct gw_trans *,
197 const char *);
198 static const struct got_error *gw_gen_committer_header(struct gw_trans *,
199 const char *);
200 static const struct got_error *gw_gen_commit_msg_header(struct gw_trans*,
201 char *);
202 static const struct got_error *gw_gen_tree_header(struct gw_trans *, char *);
203 static const struct got_error *gw_display_open(struct gw_trans *, enum khttp,
204 enum kmime);
205 static const struct got_error *gw_display_index(struct gw_trans *);
206 static const struct got_error *gw_get_header(struct gw_trans *,
207 struct gw_header *, int);
208 static const struct got_error *gw_get_commits(struct gw_trans *,
209 struct gw_header *, int,
210 struct got_object_id *);
211 static const struct got_error *gw_get_commit(struct gw_trans *,
212 struct gw_header *,
213 struct got_commit_object *,
214 struct got_object_id *);
215 static const struct got_error *gw_apply_unveil(const char *);
216 static const struct got_error *gw_blame_cb(void *, int, int,
217 struct got_object_id *);
218 static const struct got_error *gw_load_got_paths(struct gw_trans *);
219 static const struct got_error *gw_load_got_path(struct gw_trans *,
220 struct gw_dir *);
221 static const struct got_error *gw_parse_querystring(struct gw_trans *);
222 static const struct got_error *gw_blame(struct gw_trans *);
223 static const struct got_error *gw_blob(struct gw_trans *);
224 static const struct got_error *gw_diff(struct gw_trans *);
225 static const struct got_error *gw_index(struct gw_trans *);
226 static const struct got_error *gw_commits(struct gw_trans *);
227 static const struct got_error *gw_briefs(struct gw_trans *);
228 static const struct got_error *gw_summary(struct gw_trans *);
229 static const struct got_error *gw_tree(struct gw_trans *);
230 static const struct got_error *gw_tag(struct gw_trans *);
231 static const struct got_error *gw_tags(struct gw_trans *);
233 struct gw_query_action {
234 unsigned int func_id;
235 const char *func_name;
236 const struct got_error *(*func_main)(struct gw_trans *);
237 char *template;
238 };
240 enum gw_query_actions {
241 GW_BLAME,
242 GW_BLOB,
243 GW_BRIEFS,
244 GW_COMMITS,
245 GW_DIFF,
246 GW_ERR,
247 GW_INDEX,
248 GW_SUMMARY,
249 GW_TAG,
250 GW_TAGS,
251 GW_TREE,
252 };
254 static struct gw_query_action gw_query_funcs[] = {
255 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
256 { GW_BLOB, "blob", NULL, NULL },
257 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
258 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
259 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
260 { GW_ERR, "error", gw_error, "gw_tmpl/err.tmpl" },
261 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
262 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
263 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
264 { GW_TAGS, "tags", gw_tags, "gw_tmpl/tags.tmpl" },
265 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
266 };
268 static const char *
269 gw_get_action_name(struct gw_trans *gw_trans)
271 return gw_query_funcs[gw_trans->action].func_name;
274 static const struct got_error *
275 gw_kcgi_error(enum kcgi_err kerr)
277 if (kerr == KCGI_OK)
278 return NULL;
280 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
281 return got_error(GOT_ERR_CANCELLED);
283 if (kerr == KCGI_ENOMEM)
284 return got_error_set_errno(ENOMEM,
285 kcgi_strerror(kerr));
287 if (kerr == KCGI_ENFILE)
288 return got_error_set_errno(ENFILE,
289 kcgi_strerror(kerr));
291 if (kerr == KCGI_EAGAIN)
292 return got_error_set_errno(EAGAIN,
293 kcgi_strerror(kerr));
295 if (kerr == KCGI_FORM)
296 return got_error_msg(GOT_ERR_IO,
297 kcgi_strerror(kerr));
299 return got_error_from_errno(kcgi_strerror(kerr));
302 static const struct got_error *
303 gw_apply_unveil(const char *repo_path)
305 const struct got_error *err;
307 #ifdef PROFILE
308 if (unveil("gmon.out", "rwc") != 0)
309 return got_error_from_errno2("unveil", "gmon.out");
310 #endif
311 if (repo_path && unveil(repo_path, "r") != 0)
312 return got_error_from_errno2("unveil", repo_path);
314 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
315 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
317 err = got_privsep_unveil_exec_helpers();
318 if (err != NULL)
319 return err;
321 if (unveil(NULL, NULL) != 0)
322 return got_error_from_errno("unveil");
324 return NULL;
327 static int
328 isbinary(const uint8_t *buf, size_t n)
330 size_t i;
332 for (i = 0; i < n; i++)
333 if (buf[i] == 0)
334 return 1;
335 return 0;
338 static const struct got_error *
339 gw_blame(struct gw_trans *gw_trans)
341 const struct got_error *error = NULL;
342 struct gw_header *header = NULL;
343 char *age = NULL;
344 enum kcgi_err kerr = KCGI_OK;
346 #ifndef PROFILE
347 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
348 NULL) == -1)
349 return got_error_from_errno("pledge");
350 #endif
351 if ((header = gw_init_header()) == NULL)
352 return got_error_from_errno("malloc");
354 error = gw_apply_unveil(gw_trans->gw_dir->path);
355 if (error)
356 goto done;
358 /* check querystring */
359 if (gw_trans->repo_file == NULL) {
360 error = got_error_msg(GOT_ERR_QUERYSTRING,
361 "file required in querystring");
362 goto done;
364 if (gw_trans->commit_id == NULL) {
365 error = got_error_msg(GOT_ERR_QUERYSTRING,
366 "commit required in querystring");
367 goto done;
370 error = gw_get_header(gw_trans, header, 1);
371 if (error)
372 goto done;
373 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
374 "blame_header_wrapper", KATTR__MAX);
375 if (kerr != KCGI_OK)
376 goto done;
377 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
378 "blame_header", KATTR__MAX);
379 if (kerr != KCGI_OK)
380 goto done;
381 error = gw_get_time_str(&age, header->committer_time,
382 TM_LONG);
383 if (error)
384 goto done;
385 error = gw_gen_age_header(gw_trans, age ?age : "");
386 if (error)
387 goto done;
388 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
389 if (error)
390 goto done;
391 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
392 if (kerr != KCGI_OK)
393 goto done;
394 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
395 "dotted_line", KATTR__MAX);
396 if (kerr != KCGI_OK)
397 goto done;
398 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
399 if (kerr != KCGI_OK)
400 goto done;
402 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
403 "blame", KATTR__MAX);
404 if (kerr != KCGI_OK)
405 goto done;
406 error = gw_output_file_blame(gw_trans, header);
407 if (error)
408 goto done;
409 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
410 done:
411 gw_free_header(header);
412 if (error == NULL && kerr != KCGI_OK)
413 error = gw_kcgi_error(kerr);
414 return error;
417 static const struct got_error *
418 gw_blob(struct gw_trans *gw_trans)
420 const struct got_error *error = NULL, *err = NULL;
421 struct gw_header *header = NULL;
422 enum kcgi_err kerr = KCGI_OK;
424 #ifndef PROFILE
425 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
426 NULL) == -1)
427 return got_error_from_errno("pledge");
428 #endif
429 if ((header = gw_init_header()) == NULL)
430 return got_error_from_errno("malloc");
432 error = gw_apply_unveil(gw_trans->gw_dir->path);
433 if (error)
434 goto done;
436 /* check querystring */
437 if (gw_trans->repo_file == NULL) {
438 error = got_error_msg(GOT_ERR_QUERYSTRING,
439 "file required in querystring");
440 goto done;
442 if (gw_trans->commit_id == NULL) {
443 error = got_error_msg(GOT_ERR_QUERYSTRING,
444 "commit required in querystring");
445 goto done;
447 error = gw_get_header(gw_trans, header, 1);
448 if (error)
449 goto done;
451 error = gw_output_blob_buf(gw_trans, header);
452 done:
454 if (error) {
455 gw_trans->mime = KMIME_TEXT_PLAIN;
456 err = gw_display_index(gw_trans);
457 if (err) {
458 error = err;
459 goto errored;
461 kerr = khttp_puts(gw_trans->gw_req, error->msg);
463 errored:
464 gw_free_header(header);
465 if (error == NULL && kerr != KCGI_OK)
466 error = gw_kcgi_error(kerr);
467 return error;
470 static const struct got_error *
471 gw_diff(struct gw_trans *gw_trans)
473 const struct got_error *error = NULL;
474 struct gw_header *header = NULL;
475 char *age = NULL;
476 enum kcgi_err kerr = KCGI_OK;
478 #ifndef PROFILE
479 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
480 NULL) == -1)
481 return got_error_from_errno("pledge");
482 #endif
483 if ((header = gw_init_header()) == NULL)
484 return got_error_from_errno("malloc");
486 error = gw_apply_unveil(gw_trans->gw_dir->path);
487 if (error)
488 goto done;
490 error = gw_get_header(gw_trans, header, 1);
491 if (error)
492 goto done;
494 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
495 "diff_header_wrapper", KATTR__MAX);
496 if (kerr != KCGI_OK)
497 goto done;
498 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
499 "diff_header", KATTR__MAX);
500 if (kerr != KCGI_OK)
501 goto done;
502 error = gw_gen_diff_header(gw_trans, header->parent_id,
503 header->commit_id);
504 if (error)
505 goto done;
506 error = gw_gen_commit_header(gw_trans, header->commit_id,
507 header->refs_str);
508 if (error)
509 goto done;
510 error = gw_gen_tree_header(gw_trans, header->tree_id);
511 if (error)
512 goto done;
513 error = gw_gen_author_header(gw_trans, header->author);
514 if (error)
515 goto done;
516 error = gw_gen_committer_header(gw_trans, header->author);
517 if (error)
518 goto done;
519 error = gw_get_time_str(&age, header->committer_time,
520 TM_LONG);
521 if (error)
522 goto done;
523 error = gw_gen_age_header(gw_trans, age ?age : "");
524 if (error)
525 goto done;
526 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
527 if (error)
528 goto done;
529 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
530 if (kerr != KCGI_OK)
531 goto done;
532 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
533 "dotted_line", KATTR__MAX);
534 if (kerr != KCGI_OK)
535 goto done;
536 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
537 if (kerr != KCGI_OK)
538 goto done;
540 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
541 "diff", KATTR__MAX);
542 if (kerr != KCGI_OK)
543 goto done;
544 error = gw_output_diff(gw_trans, header);
545 if (error)
546 goto done;
548 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
549 done:
550 gw_free_header(header);
551 free(age);
552 if (error == NULL && kerr != KCGI_OK)
553 error = gw_kcgi_error(kerr);
554 return error;
557 static const struct got_error *
558 gw_index(struct gw_trans *gw_trans)
560 const struct got_error *error = NULL;
561 struct gw_dir *gw_dir = NULL;
562 char *href_next = NULL, *href_prev = NULL, *href_summary = NULL;
563 char *href_briefs = NULL, *href_commits = NULL, *href_tree = NULL;
564 char *href_tags = NULL;
565 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
566 enum kcgi_err kerr = KCGI_OK;
568 #ifndef PROFILE
569 if (pledge("stdio rpath proc exec sendfd unveil",
570 NULL) == -1) {
571 error = got_error_from_errno("pledge");
572 return error;
574 #endif
575 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path);
576 if (error)
577 return error;
579 error = gw_load_got_paths(gw_trans);
580 if (error)
581 return error;
583 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
584 "index_header", KATTR__MAX);
585 if (kerr != KCGI_OK)
586 return gw_kcgi_error(kerr);
587 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
588 "index_header_project", KATTR__MAX);
589 if (kerr != KCGI_OK)
590 return gw_kcgi_error(kerr);
591 kerr = khtml_puts(gw_trans->gw_html_req, "Project");
592 if (kerr != KCGI_OK)
593 return gw_kcgi_error(kerr);
594 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
595 if (kerr != KCGI_OK)
596 return gw_kcgi_error(kerr);
598 if (gw_trans->gw_conf->got_show_repo_description) {
599 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
600 "index_header_description", KATTR__MAX);
601 if (kerr != KCGI_OK)
602 return gw_kcgi_error(kerr);
603 kerr = khtml_puts(gw_trans->gw_html_req, "Description");
604 if (kerr != KCGI_OK)
605 return gw_kcgi_error(kerr);
606 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
607 if (kerr != KCGI_OK)
608 return gw_kcgi_error(kerr);
611 if (gw_trans->gw_conf->got_show_repo_owner) {
612 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
613 "index_header_owner", KATTR__MAX);
614 if (kerr != KCGI_OK)
615 return gw_kcgi_error(kerr);
616 kerr = khtml_puts(gw_trans->gw_html_req, "Owner");
617 if (kerr != KCGI_OK)
618 return gw_kcgi_error(kerr);
619 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
620 if (kerr != KCGI_OK)
621 return gw_kcgi_error(kerr);
624 if (gw_trans->gw_conf->got_show_repo_age) {
625 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
626 "index_header_age", KATTR__MAX);
627 if (kerr != KCGI_OK)
628 return gw_kcgi_error(kerr);
629 kerr = khtml_puts(gw_trans->gw_html_req, "Last Change");
630 if (kerr != KCGI_OK)
631 return gw_kcgi_error(kerr);
632 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
633 if (kerr != KCGI_OK)
634 return gw_kcgi_error(kerr);
637 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
638 if (kerr != KCGI_OK)
639 return gw_kcgi_error(kerr);
641 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
642 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
643 "index_wrapper", KATTR__MAX);
644 if (kerr != KCGI_OK)
645 return gw_kcgi_error(kerr);
646 kerr = khtml_printf(gw_trans->gw_html_req,
647 "No repositories found in %s",
648 gw_trans->gw_conf->got_repos_path);
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 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
655 "dotted_line", KATTR__MAX);
656 if (kerr != KCGI_OK)
657 return gw_kcgi_error(kerr);
658 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
659 if (kerr != KCGI_OK)
660 return gw_kcgi_error(kerr);
661 return error;
664 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
665 dir_c++;
667 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
668 if (gw_trans->page > 0 && (gw_trans->page *
669 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
670 prev_disp++;
671 continue;
674 prev_disp++;
676 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
677 "index_wrapper", KATTR__MAX);
678 if (kerr != KCGI_OK)
679 goto done;
681 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
682 gw_dir->name, "action", "summary", NULL);
683 if (href_summary == NULL) {
684 error = got_error_from_errno("khttp_urlpart");
685 goto done;
687 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
688 "index_project", KATTR__MAX);
689 if (kerr != KCGI_OK)
690 goto done;
691 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
692 href_summary, KATTR__MAX);
693 if (kerr != KCGI_OK)
694 goto done;
695 kerr = khtml_puts(gw_trans->gw_html_req, gw_dir->name);
696 if (kerr != KCGI_OK)
697 goto done;
698 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
699 if (kerr != KCGI_OK)
700 goto done;
701 if (gw_trans->gw_conf->got_show_repo_description) {
702 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
703 KATTR_ID, "index_project_description", KATTR__MAX);
704 if (kerr != KCGI_OK)
705 goto done;
706 kerr = khtml_puts(gw_trans->gw_html_req,
707 gw_dir->description ? gw_dir->description : "");
708 if (kerr != KCGI_OK)
709 goto done;
710 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
711 if (kerr != KCGI_OK)
712 goto done;
714 if (gw_trans->gw_conf->got_show_repo_owner) {
715 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
716 KATTR_ID, "index_project_owner", KATTR__MAX);
717 if (kerr != KCGI_OK)
718 goto done;
719 kerr = khtml_puts(gw_trans->gw_html_req,
720 gw_dir->owner ? gw_dir->owner : "");
721 if (kerr != KCGI_OK)
722 goto done;
723 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
724 if (kerr != KCGI_OK)
725 goto done;
727 if (gw_trans->gw_conf->got_show_repo_age) {
728 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
729 KATTR_ID, "index_project_age", KATTR__MAX);
730 if (kerr != KCGI_OK)
731 goto done;
732 kerr = khtml_puts(gw_trans->gw_html_req,
733 gw_dir->age ? gw_dir->age : "");
734 if (kerr != KCGI_OK)
735 goto done;
736 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
737 if (kerr != KCGI_OK)
738 goto done;
741 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
742 "navs_wrapper", KATTR__MAX);
743 if (kerr != KCGI_OK)
744 goto done;
745 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
746 "navs", KATTR__MAX);
747 if (kerr != KCGI_OK)
748 goto done;
750 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
751 href_summary, KATTR__MAX);
752 if (kerr != KCGI_OK)
753 goto done;
754 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
755 if (kerr != KCGI_OK)
756 goto done;
757 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
758 if (kerr != KCGI_OK)
759 goto done;
761 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
762 if (kerr != KCGI_OK)
763 goto done;
765 href_briefs = khttp_urlpart(NULL, NULL, "gotweb", "path",
766 gw_dir->name, "action", "briefs", NULL);
767 if (href_briefs == NULL) {
768 error = got_error_from_errno("khttp_urlpart");
769 goto done;
771 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
772 href_briefs, KATTR__MAX);
773 if (kerr != KCGI_OK)
774 goto done;
775 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
776 if (kerr != KCGI_OK)
777 goto done;
778 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
779 if (kerr != KCGI_OK)
780 error = gw_kcgi_error(kerr);
782 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
783 if (kerr != KCGI_OK)
784 goto done;
786 href_commits = khttp_urlpart(NULL, NULL, "gotweb", "path",
787 gw_dir->name, "action", "commits", NULL);
788 if (href_commits == NULL) {
789 error = got_error_from_errno("khttp_urlpart");
790 goto done;
792 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
793 href_commits, KATTR__MAX);
794 if (kerr != KCGI_OK)
795 goto done;
796 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
797 if (kerr != KCGI_OK)
798 goto done;
799 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
800 if (kerr != KCGI_OK)
801 goto done;
803 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
804 if (kerr != KCGI_OK)
805 goto done;
807 href_tags = khttp_urlpart(NULL, NULL, "gotweb", "path",
808 gw_dir->name, "action", "tags", NULL);
809 if (href_tags == NULL) {
810 error = got_error_from_errno("khttp_urlpart");
811 goto done;
813 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
814 href_tags, KATTR__MAX);
815 if (kerr != KCGI_OK)
816 goto done;
817 kerr = khtml_puts(gw_trans->gw_html_req, "tags");
818 if (kerr != KCGI_OK)
819 goto done;
820 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
821 if (kerr != KCGI_OK)
822 goto done;
824 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
825 if (kerr != KCGI_OK)
826 goto done;
828 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
829 gw_dir->name, "action", "tree", NULL);
830 if (href_tree == NULL) {
831 error = got_error_from_errno("khttp_urlpart");
832 goto done;
834 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
835 href_tree, KATTR__MAX);
836 if (kerr != KCGI_OK)
837 goto done;
838 kerr = khtml_puts(gw_trans->gw_html_req, "tree");
839 if (kerr != KCGI_OK)
840 goto done;
842 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
843 if (kerr != KCGI_OK)
844 goto done;
845 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
846 "dotted_line", KATTR__MAX);
847 if (kerr != KCGI_OK)
848 goto done;
849 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
850 if (kerr != KCGI_OK)
851 goto done;
853 free(href_summary);
854 href_summary = NULL;
855 free(href_briefs);
856 href_briefs = NULL;
857 free(href_commits);
858 href_commits = NULL;
859 free(href_tags);
860 href_tags = NULL;
861 free(href_tree);
862 href_tree = NULL;
864 if (gw_trans->gw_conf->got_max_repos_display == 0)
865 continue;
867 if ((next_disp == gw_trans->gw_conf->got_max_repos_display) ||
868 ((gw_trans->gw_conf->got_max_repos_display > 0) &&
869 (gw_trans->page > 0) &&
870 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
871 prev_disp == gw_trans->repos_total))) {
872 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
873 KATTR_ID, "np_wrapper", KATTR__MAX);
874 if (kerr != KCGI_OK)
875 goto done;
876 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
877 KATTR_ID, "nav_prev", KATTR__MAX);
878 if (kerr != KCGI_OK)
879 goto done;
882 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
883 (gw_trans->page > 0) &&
884 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
885 prev_disp == gw_trans->repos_total)) {
886 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "page",
887 KATTRX_INT, (int64_t)(gw_trans->page - 1), NULL);
888 if (href_prev == NULL) {
889 error = got_error_from_errno("khttp_urlpartx");
890 goto done;
892 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
893 KATTR_HREF, href_prev, KATTR__MAX);
894 if (kerr != KCGI_OK)
895 goto done;
896 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
897 if (kerr != KCGI_OK)
898 goto done;
899 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
900 if (kerr != KCGI_OK)
901 goto done;
904 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
905 if (kerr != KCGI_OK)
906 return gw_kcgi_error(kerr);
908 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
909 next_disp == gw_trans->gw_conf->got_max_repos_display &&
910 dir_c != (gw_trans->page + 1) *
911 gw_trans->gw_conf->got_max_repos_display) {
912 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
913 KATTR_ID, "nav_next", KATTR__MAX);
914 if (kerr != KCGI_OK)
915 goto done;
916 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "page",
917 KATTRX_INT, (int64_t)(gw_trans->page + 1), NULL);
918 if (href_next == NULL) {
919 error = got_error_from_errno("khttp_urlpartx");
920 goto done;
922 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
923 KATTR_HREF, href_next, KATTR__MAX);
924 if (kerr != KCGI_OK)
925 goto done;
926 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
927 if (kerr != KCGI_OK)
928 goto done;
929 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
930 if (kerr != KCGI_OK)
931 goto done;
932 next_disp = 0;
933 break;
936 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
937 (gw_trans->page > 0) &&
938 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
939 prev_disp == gw_trans->repos_total)) {
940 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
941 if (kerr != KCGI_OK)
942 goto done;
944 next_disp++;
946 done:
947 free(href_prev);
948 free(href_next);
949 free(href_summary);
950 free(href_briefs);
951 free(href_commits);
952 free(href_tags);
953 free(href_tree);
954 if (error == NULL && kerr != KCGI_OK)
955 error = gw_kcgi_error(kerr);
956 return error;
959 static const struct got_error *
960 gw_commits(struct gw_trans *gw_trans)
962 const struct got_error *error = NULL;
963 struct gw_header *header = NULL, *n_header = NULL;
964 char *age = NULL, *href_diff = NULL, *href_tree = NULL;
965 char *href_prev = NULL, *href_next = NULL;
966 enum kcgi_err kerr = KCGI_OK;
967 int commit_found = 0;
969 if ((header = gw_init_header()) == NULL)
970 return got_error_from_errno("malloc");
972 #ifndef PROFILE
973 if (pledge("stdio rpath proc exec sendfd unveil",
974 NULL) == -1) {
975 error = got_error_from_errno("pledge");
976 goto done;
978 #endif
979 error = gw_apply_unveil(gw_trans->gw_dir->path);
980 if (error)
981 goto done;
983 error = gw_get_header(gw_trans, header,
984 gw_trans->gw_conf->got_max_commits_display);
985 if (error)
986 goto done;
988 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
989 if (commit_found == 0 && gw_trans->commit_id != NULL) {
990 if (strcmp(gw_trans->commit_id,
991 n_header->commit_id) != 0)
992 continue;
993 else
994 commit_found = 1;
996 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
997 "commits_line_wrapper", KATTR__MAX);
998 if (kerr != KCGI_OK)
999 goto done;
1000 error = gw_gen_commit_header(gw_trans, n_header->commit_id,
1001 n_header->refs_str);
1002 if (error)
1003 goto done;
1004 error = gw_gen_author_header(gw_trans, n_header->author);
1005 if (error)
1006 goto done;
1007 error = gw_gen_committer_header(gw_trans, n_header->author);
1008 if (error)
1009 goto done;
1010 error = gw_get_time_str(&age, n_header->committer_time,
1011 TM_LONG);
1012 if (error)
1013 goto done;
1014 error = gw_gen_age_header(gw_trans, age ?age : "");
1015 if (error)
1016 goto done;
1017 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1018 if (kerr != KCGI_OK)
1019 goto done;
1021 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1022 "dotted_line", KATTR__MAX);
1023 if (kerr != KCGI_OK)
1024 goto done;
1025 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1026 if (kerr != KCGI_OK)
1027 goto done;
1029 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1030 "commit", KATTR__MAX);
1031 if (kerr != KCGI_OK)
1032 goto done;
1033 kerr = khttp_puts(gw_trans->gw_req, n_header->commit_msg);
1034 if (kerr != KCGI_OK)
1035 goto done;
1036 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1037 if (kerr != KCGI_OK)
1038 goto done;
1040 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
1041 gw_trans->repo_name, "action", "diff", "commit",
1042 n_header->commit_id, NULL);
1043 if (href_diff == NULL) {
1044 error = got_error_from_errno("khttp_urlpart");
1045 goto done;
1047 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1048 KATTR_ID, "navs_wrapper", KATTR__MAX);
1049 if (kerr != KCGI_OK)
1050 goto done;
1051 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1052 KATTR_ID, "navs", KATTR__MAX);
1053 if (kerr != KCGI_OK)
1054 goto done;
1055 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1056 KATTR_HREF, href_diff, KATTR__MAX);
1057 if (kerr != KCGI_OK)
1058 goto done;
1059 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1060 if (kerr != KCGI_OK)
1061 goto done;
1062 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1063 if (kerr != KCGI_OK)
1064 goto done;
1066 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1067 if (kerr != KCGI_OK)
1068 goto done;
1070 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
1071 gw_trans->repo_name, "action", "tree", "commit",
1072 n_header->commit_id, NULL);
1073 if (href_tree == NULL) {
1074 error = got_error_from_errno("khttp_urlpart");
1075 goto done;
1077 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1078 KATTR_HREF, href_tree, KATTR__MAX);
1079 if (kerr != KCGI_OK)
1080 goto done;
1081 khtml_puts(gw_trans->gw_html_req, "tree");
1082 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1083 if (kerr != KCGI_OK)
1084 goto done;
1085 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1086 if (kerr != KCGI_OK)
1087 goto done;
1089 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1090 "solid_line", KATTR__MAX);
1091 if (kerr != KCGI_OK)
1092 goto done;
1093 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1094 if (kerr != KCGI_OK)
1095 goto done;
1097 free(age);
1098 age = NULL;
1101 if (gw_trans->next_id || gw_trans->prev_id) {
1102 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1103 KATTR_ID, "np_wrapper", KATTR__MAX);
1104 if (kerr != KCGI_OK)
1105 goto done;
1106 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1107 KATTR_ID, "nav_prev", KATTR__MAX);
1108 if (kerr != KCGI_OK)
1109 goto done;
1112 if (gw_trans->prev_id) {
1113 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1114 KATTRX_STRING, gw_trans->repo_name, "page",
1115 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1116 KATTRX_STRING, "commits", "commit", KATTRX_STRING,
1117 gw_trans->prev_id ? gw_trans->prev_id : "", NULL);
1118 if (href_prev == NULL) {
1119 error = got_error_from_errno("khttp_urlpartx");
1120 goto done;
1122 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1123 KATTR_HREF, href_prev, KATTR__MAX);
1124 if (kerr != KCGI_OK)
1125 goto done;
1126 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1127 if (kerr != KCGI_OK)
1128 goto done;
1129 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1130 if (kerr != KCGI_OK)
1131 goto done;
1134 if (gw_trans->next_id || gw_trans->page > 0) {
1135 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1136 if (kerr != KCGI_OK)
1137 return gw_kcgi_error(kerr);
1140 if (gw_trans->next_id) {
1141 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1142 KATTR_ID, "nav_next", KATTR__MAX);
1143 if (kerr != KCGI_OK)
1144 goto done;
1145 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1146 KATTRX_STRING, gw_trans->repo_name, "page",
1147 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1148 KATTRX_STRING, "commits", "commit", KATTRX_STRING,
1149 gw_trans->next_id, NULL);
1150 if (href_next == NULL) {
1151 error = got_error_from_errno("khttp_urlpartx");
1152 goto done;
1154 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1155 KATTR_HREF, href_next, KATTR__MAX);
1156 if (kerr != KCGI_OK)
1157 goto done;
1158 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1159 if (kerr != KCGI_OK)
1160 goto done;
1161 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1162 if (kerr != KCGI_OK)
1163 goto done;
1166 if (gw_trans->next_id || gw_trans->page > 0) {
1167 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1168 if (kerr != KCGI_OK)
1169 goto done;
1171 done:
1172 gw_free_header(header);
1173 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1174 gw_free_header(n_header);
1175 free(age);
1176 free(href_next);
1177 free(href_prev);
1178 free(href_diff);
1179 free(href_tree);
1180 if (error == NULL && kerr != KCGI_OK)
1181 error = gw_kcgi_error(kerr);
1182 return error;
1185 static const struct got_error *
1186 gw_briefs(struct gw_trans *gw_trans)
1188 const struct got_error *error = NULL;
1189 struct gw_header *header = NULL, *n_header = NULL;
1190 char *age = NULL, *href_diff = NULL, *href_tree = NULL;
1191 char *href_prev = NULL, *href_next = NULL;
1192 char *newline, *smallerthan;
1193 enum kcgi_err kerr = KCGI_OK;
1194 int commit_found = 0;
1196 if ((header = gw_init_header()) == NULL)
1197 return got_error_from_errno("malloc");
1199 #ifndef PROFILE
1200 if (pledge("stdio rpath proc exec sendfd unveil",
1201 NULL) == -1) {
1202 error = got_error_from_errno("pledge");
1203 goto done;
1205 #endif
1206 if (gw_trans->action != GW_SUMMARY) {
1207 error = gw_apply_unveil(gw_trans->gw_dir->path);
1208 if (error)
1209 goto done;
1212 if (gw_trans->action == GW_SUMMARY)
1213 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
1214 else
1215 error = gw_get_header(gw_trans, header,
1216 gw_trans->gw_conf->got_max_commits_display);
1217 if (error)
1218 goto done;
1220 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
1221 if (commit_found == 0 && gw_trans->commit_id != NULL) {
1222 if (strcmp(gw_trans->commit_id,
1223 n_header->commit_id) != 0)
1224 continue;
1225 else
1226 commit_found = 1;
1228 error = gw_get_time_str(&age, n_header->committer_time,
1229 TM_DIFF);
1230 if (error)
1231 goto done;
1233 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1234 KATTR_ID, "briefs_wrapper", KATTR__MAX);
1235 if (kerr != KCGI_OK)
1236 goto done;
1238 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1239 KATTR_ID, "briefs_age", KATTR__MAX);
1240 if (kerr != KCGI_OK)
1241 goto done;
1242 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
1243 if (kerr != KCGI_OK)
1244 goto done;
1245 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1246 if (kerr != KCGI_OK)
1247 goto done;
1249 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1250 KATTR_ID, "briefs_author", KATTR__MAX);
1251 if (kerr != KCGI_OK)
1252 goto done;
1253 smallerthan = strchr(n_header->author, '<');
1254 if (smallerthan)
1255 *smallerthan = '\0';
1256 kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
1257 if (kerr != KCGI_OK)
1258 goto done;
1259 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1260 if (kerr != KCGI_OK)
1261 goto done;
1263 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
1264 gw_trans->repo_name, "action", "diff", "commit",
1265 n_header->commit_id, NULL);
1266 if (href_diff == NULL) {
1267 error = got_error_from_errno("khttp_urlpart");
1268 goto done;
1270 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1271 KATTR_ID, "briefs_log", KATTR__MAX);
1272 if (kerr != KCGI_OK)
1273 goto done;
1274 newline = strchr(n_header->commit_msg, '\n');
1275 if (newline)
1276 *newline = '\0';
1277 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1278 KATTR_HREF, href_diff, KATTR__MAX);
1279 if (kerr != KCGI_OK)
1280 goto done;
1281 kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
1282 if (kerr != KCGI_OK)
1283 goto done;
1284 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1285 if (kerr != KCGI_OK)
1286 goto done;
1288 if (n_header->refs_str) {
1289 kerr = khtml_puts(gw_trans->gw_html_req, " ");
1290 if (kerr != KCGI_OK)
1291 goto done;
1292 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
1293 KATTR_ID, "refs_str", KATTR__MAX);
1294 if (kerr != KCGI_OK)
1295 goto done;
1296 kerr = khtml_printf(gw_trans->gw_html_req, "(%s)",
1297 n_header->refs_str);
1298 if (kerr != KCGI_OK)
1299 goto done;
1300 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1301 if (kerr != KCGI_OK)
1302 goto done;
1305 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1306 if (kerr != KCGI_OK)
1307 goto done;
1309 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1310 KATTR_ID, "navs_wrapper", KATTR__MAX);
1311 if (kerr != KCGI_OK)
1312 goto done;
1313 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1314 KATTR_ID, "navs", KATTR__MAX);
1315 if (kerr != KCGI_OK)
1316 goto done;
1317 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1318 KATTR_HREF, href_diff, KATTR__MAX);
1319 if (kerr != KCGI_OK)
1320 goto done;
1321 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1322 if (kerr != KCGI_OK)
1323 goto done;
1324 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1325 if (kerr != KCGI_OK)
1326 goto done;
1328 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1329 if (kerr != KCGI_OK)
1330 goto done;
1332 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
1333 gw_trans->repo_name, "action", "tree", "commit",
1334 n_header->commit_id, NULL);
1335 if (href_tree == NULL) {
1336 error = got_error_from_errno("khttp_urlpart");
1337 goto done;
1339 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1340 KATTR_HREF, href_tree, KATTR__MAX);
1341 if (kerr != KCGI_OK)
1342 goto done;
1343 khtml_puts(gw_trans->gw_html_req, "tree");
1344 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1345 if (kerr != KCGI_OK)
1346 goto done;
1347 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1348 if (kerr != KCGI_OK)
1349 goto done;
1351 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1352 KATTR_ID, "dotted_line", KATTR__MAX);
1353 if (kerr != KCGI_OK)
1354 goto done;
1355 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1356 if (kerr != KCGI_OK)
1357 goto done;
1359 free(age);
1360 age = NULL;
1361 free(href_diff);
1362 href_diff = NULL;
1363 free(href_tree);
1364 href_tree = NULL;
1367 if (gw_trans->next_id || gw_trans->prev_id) {
1368 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1369 KATTR_ID, "np_wrapper", KATTR__MAX);
1370 if (kerr != KCGI_OK)
1371 goto done;
1372 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1373 KATTR_ID, "nav_prev", KATTR__MAX);
1374 if (kerr != KCGI_OK)
1375 goto done;
1378 if (gw_trans->prev_id) {
1379 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1380 KATTRX_STRING, gw_trans->repo_name, "page",
1381 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1382 KATTRX_STRING, "briefs", "commit", KATTRX_STRING,
1383 gw_trans->prev_id ? gw_trans->prev_id : "", NULL);
1384 if (href_prev == NULL) {
1385 error = got_error_from_errno("khttp_urlpartx");
1386 goto done;
1388 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1389 KATTR_HREF, href_prev, KATTR__MAX);
1390 if (kerr != KCGI_OK)
1391 goto done;
1392 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1393 if (kerr != KCGI_OK)
1394 goto done;
1395 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1396 if (kerr != KCGI_OK)
1397 goto done;
1400 if (gw_trans->next_id || gw_trans->page > 0) {
1401 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1402 if (kerr != KCGI_OK)
1403 return gw_kcgi_error(kerr);
1406 if (gw_trans->next_id) {
1407 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1408 KATTR_ID, "nav_next", KATTR__MAX);
1409 if (kerr != KCGI_OK)
1410 goto done;
1412 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1413 KATTRX_STRING, gw_trans->repo_name, "page",
1414 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1415 KATTRX_STRING, "briefs", "commit", KATTRX_STRING,
1416 gw_trans->next_id, NULL);
1417 if (href_next == NULL) {
1418 error = got_error_from_errno("khttp_urlpartx");
1419 goto done;
1421 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1422 KATTR_HREF, href_next, KATTR__MAX);
1423 if (kerr != KCGI_OK)
1424 goto done;
1425 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1426 if (kerr != KCGI_OK)
1427 goto done;
1428 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1429 if (kerr != KCGI_OK)
1430 goto done;
1433 if (gw_trans->next_id || gw_trans->page > 0) {
1434 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1435 if (kerr != KCGI_OK)
1436 goto done;
1438 done:
1439 gw_free_header(header);
1440 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1441 gw_free_header(n_header);
1442 free(age);
1443 free(href_next);
1444 free(href_prev);
1445 free(href_diff);
1446 free(href_tree);
1447 if (error == NULL && kerr != KCGI_OK)
1448 error = gw_kcgi_error(kerr);
1449 return error;
1452 static const struct got_error *
1453 gw_summary(struct gw_trans *gw_trans)
1455 const struct got_error *error = NULL;
1456 char *age = NULL;
1457 enum kcgi_err kerr = KCGI_OK;
1459 #ifndef PROFILE
1460 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1461 return got_error_from_errno("pledge");
1462 #endif
1463 error = gw_apply_unveil(gw_trans->gw_dir->path);
1464 if (error)
1465 goto done;
1467 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1468 "summary_wrapper", KATTR__MAX);
1469 if (kerr != KCGI_OK)
1470 return gw_kcgi_error(kerr);
1472 if (gw_trans->gw_conf->got_show_repo_description &&
1473 gw_trans->gw_dir->description != NULL &&
1474 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1475 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1476 KATTR_ID, "description_title", KATTR__MAX);
1477 if (kerr != KCGI_OK)
1478 goto done;
1479 kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1480 if (kerr != KCGI_OK)
1481 goto done;
1482 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1483 if (kerr != KCGI_OK)
1484 goto done;
1485 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1486 KATTR_ID, "description", KATTR__MAX);
1487 if (kerr != KCGI_OK)
1488 goto done;
1489 kerr = khtml_puts(gw_trans->gw_html_req,
1490 gw_trans->gw_dir->description);
1491 if (kerr != KCGI_OK)
1492 goto done;
1493 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1494 if (kerr != KCGI_OK)
1495 goto done;
1498 if (gw_trans->gw_conf->got_show_repo_owner &&
1499 gw_trans->gw_dir->owner != NULL &&
1500 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
1501 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1502 KATTR_ID, "repo_owner_title", KATTR__MAX);
1503 if (kerr != KCGI_OK)
1504 goto done;
1505 kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1506 if (kerr != KCGI_OK)
1507 goto done;
1508 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1509 if (kerr != KCGI_OK)
1510 goto done;
1511 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1512 KATTR_ID, "repo_owner", KATTR__MAX);
1513 if (kerr != KCGI_OK)
1514 goto done;
1515 kerr = khtml_puts(gw_trans->gw_html_req,
1516 gw_trans->gw_dir->owner);
1517 if (kerr != KCGI_OK)
1518 goto done;
1519 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1520 if (kerr != KCGI_OK)
1521 goto done;
1524 if (gw_trans->gw_conf->got_show_repo_age) {
1525 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1526 NULL, TM_LONG);
1527 if (error)
1528 goto done;
1529 if (age != NULL) {
1530 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1531 KATTR_ID, "last_change_title", KATTR__MAX);
1532 if (kerr != KCGI_OK)
1533 goto done;
1534 kerr = khtml_puts(gw_trans->gw_html_req,
1535 "Last Change: ");
1536 if (kerr != KCGI_OK)
1537 goto done;
1538 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1539 if (kerr != KCGI_OK)
1540 goto done;
1541 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1542 KATTR_ID, "last_change", KATTR__MAX);
1543 if (kerr != KCGI_OK)
1544 goto done;
1545 kerr = khtml_puts(gw_trans->gw_html_req, age);
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;
1554 if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1555 gw_trans->gw_dir->url != NULL &&
1556 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1557 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1558 KATTR_ID, "cloneurl_title", KATTR__MAX);
1559 if (kerr != KCGI_OK)
1560 goto done;
1561 kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1562 if (kerr != KCGI_OK)
1563 goto done;
1564 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1565 if (kerr != KCGI_OK)
1566 goto done;
1567 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1568 KATTR_ID, "cloneurl", KATTR__MAX);
1569 if (kerr != KCGI_OK)
1570 goto done;
1571 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1572 if (kerr != KCGI_OK)
1573 goto done;
1574 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1575 if (kerr != KCGI_OK)
1576 goto done;
1579 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1580 if (kerr != KCGI_OK)
1581 goto done;
1583 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1584 "briefs_title_wrapper", KATTR__MAX);
1585 if (kerr != KCGI_OK)
1586 goto done;
1587 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1588 "briefs_title", KATTR__MAX);
1589 if (kerr != KCGI_OK)
1590 goto done;
1591 kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1592 if (kerr != KCGI_OK)
1593 goto done;
1594 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1595 if (kerr != KCGI_OK)
1596 goto done;
1597 error = gw_briefs(gw_trans);
1598 if (error)
1599 goto done;
1601 error = gw_tags(gw_trans);
1602 if (error)
1603 goto done;
1605 error = gw_output_repo_heads(gw_trans);
1606 done:
1607 free(age);
1608 if (error == NULL && kerr != KCGI_OK)
1609 error = gw_kcgi_error(kerr);
1610 return error;
1613 static const struct got_error *
1614 gw_tree(struct gw_trans *gw_trans)
1616 const struct got_error *error = NULL;
1617 struct gw_header *header = NULL;
1618 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1619 char *age = NULL;
1620 enum kcgi_err kerr = KCGI_OK;
1622 #ifndef PROFILE
1623 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1624 return got_error_from_errno("pledge");
1625 #endif
1626 if ((header = gw_init_header()) == NULL)
1627 return got_error_from_errno("malloc");
1629 error = gw_apply_unveil(gw_trans->gw_dir->path);
1630 if (error)
1631 goto done;
1633 error = gw_get_header(gw_trans, header, 1);
1634 if (error)
1635 goto done;
1637 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1638 "tree_header_wrapper", KATTR__MAX);
1639 if (kerr != KCGI_OK)
1640 goto done;
1641 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1642 "tree_header", KATTR__MAX);
1643 if (kerr != KCGI_OK)
1644 goto done;
1645 error = gw_gen_tree_header(gw_trans, header->tree_id);
1646 if (error)
1647 goto done;
1648 error = gw_get_time_str(&age, header->committer_time,
1649 TM_LONG);
1650 if (error)
1651 goto done;
1652 error = gw_gen_age_header(gw_trans, age ?age : "");
1653 if (error)
1654 goto done;
1655 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1656 if (error)
1657 goto done;
1658 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1659 if (kerr != KCGI_OK)
1660 goto done;
1661 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1662 "dotted_line", KATTR__MAX);
1663 if (kerr != KCGI_OK)
1664 goto done;
1665 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1666 if (kerr != KCGI_OK)
1667 goto done;
1669 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1670 "tree", KATTR__MAX);
1671 if (kerr != KCGI_OK)
1672 goto done;
1673 error = gw_output_repo_tree(gw_trans, header);
1674 if (error)
1675 goto done;
1677 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1678 done:
1679 gw_free_header(header);
1680 free(tree_html_disp);
1681 free(tree_html);
1682 free(tree);
1683 free(age);
1684 if (error == NULL && kerr != KCGI_OK)
1685 error = gw_kcgi_error(kerr);
1686 return error;
1689 static const struct got_error *
1690 gw_tags(struct gw_trans *gw_trans)
1692 const struct got_error *error = NULL;
1693 struct gw_header *header = NULL;
1694 char *href_next = NULL, *href_prev = NULL;
1695 enum kcgi_err kerr = KCGI_OK;
1697 #ifndef PROFILE
1698 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1699 return got_error_from_errno("pledge");
1700 #endif
1701 if ((header = gw_init_header()) == NULL)
1702 return got_error_from_errno("malloc");
1704 if (gw_trans->action != GW_SUMMARY) {
1705 error = gw_apply_unveil(gw_trans->gw_dir->path);
1706 if (error)
1707 goto done;
1710 error = gw_get_header(gw_trans, header, 1);
1711 if (error)
1712 goto done;
1714 if (gw_trans->action == GW_SUMMARY) {
1715 gw_trans->next_id = NULL;
1716 error = gw_output_repo_tags(gw_trans, header,
1717 D_MAXSLCOMMDISP, TAGBRIEF);
1718 if (error)
1719 goto done;
1720 } else {
1721 error = gw_output_repo_tags(gw_trans, header,
1722 gw_trans->gw_conf->got_max_commits_display, TAGBRIEF);
1723 if (error)
1724 goto done;
1727 if (gw_trans->next_id || gw_trans->page > 0) {
1728 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1729 KATTR_ID, "np_wrapper", KATTR__MAX);
1730 if (kerr != KCGI_OK)
1731 goto done;
1732 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1733 KATTR_ID, "nav_prev", KATTR__MAX);
1734 if (kerr != KCGI_OK)
1735 goto done;
1738 if (gw_trans->prev_id) {
1739 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1740 KATTRX_STRING, gw_trans->repo_name, "page",
1741 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1742 KATTRX_STRING, "tags", "commit", KATTRX_STRING,
1743 gw_trans->prev_id ? gw_trans->prev_id : "", NULL);
1744 if (href_prev == NULL) {
1745 error = got_error_from_errno("khttp_urlpartx");
1746 goto done;
1748 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1749 KATTR_HREF, href_prev, KATTR__MAX);
1750 if (kerr != KCGI_OK)
1751 goto done;
1752 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1753 if (kerr != KCGI_OK)
1754 goto done;
1755 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1756 if (kerr != KCGI_OK)
1757 goto done;
1760 if (gw_trans->next_id || gw_trans->page > 0) {
1761 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1762 if (kerr != KCGI_OK)
1763 return gw_kcgi_error(kerr);
1766 if (gw_trans->next_id) {
1767 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1768 KATTR_ID, "nav_next", KATTR__MAX);
1769 if (kerr != KCGI_OK)
1770 goto done;
1771 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1772 KATTRX_STRING, gw_trans->repo_name, "page",
1773 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1774 KATTRX_STRING, "tags", "commit", KATTRX_STRING,
1775 gw_trans->next_id, NULL);
1776 if (href_next == NULL) {
1777 error = got_error_from_errno("khttp_urlpartx");
1778 goto done;
1780 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1781 KATTR_HREF, href_next, KATTR__MAX);
1782 if (kerr != KCGI_OK)
1783 goto done;
1784 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1785 if (kerr != KCGI_OK)
1786 goto done;
1787 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1788 if (kerr != KCGI_OK)
1789 goto done;
1792 if (gw_trans->next_id || gw_trans->page > 0) {
1793 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1794 if (kerr != KCGI_OK)
1795 goto done;
1797 done:
1798 gw_free_header(header);
1799 free(href_next);
1800 free(href_prev);
1801 if (error == NULL && kerr != KCGI_OK)
1802 error = gw_kcgi_error(kerr);
1803 return error;
1806 static const struct got_error *
1807 gw_tag(struct gw_trans *gw_trans)
1809 const struct got_error *error = NULL;
1810 struct gw_header *header = NULL;
1811 enum kcgi_err kerr = KCGI_OK;
1813 #ifndef PROFILE
1814 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1815 return got_error_from_errno("pledge");
1816 #endif
1817 if ((header = gw_init_header()) == NULL)
1818 return got_error_from_errno("malloc");
1820 error = gw_apply_unveil(gw_trans->gw_dir->path);
1821 if (error)
1822 goto done;
1824 if (gw_trans->commit_id == NULL) {
1825 error = got_error_msg(GOT_ERR_QUERYSTRING,
1826 "commit required in querystring");
1827 goto done;
1830 error = gw_get_header(gw_trans, header, 1);
1831 if (error)
1832 goto done;
1834 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1835 "tag_header_wrapper", KATTR__MAX);
1836 if (kerr != KCGI_OK)
1837 goto done;
1838 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1839 "tag_header", KATTR__MAX);
1840 if (kerr != KCGI_OK)
1841 goto done;
1842 error = gw_gen_commit_header(gw_trans, header->commit_id,
1843 header->refs_str);
1844 if (error)
1845 goto done;
1846 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1847 if (error)
1848 goto done;
1849 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1850 if (kerr != KCGI_OK)
1851 goto done;
1852 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1853 "dotted_line", KATTR__MAX);
1854 if (kerr != KCGI_OK)
1855 goto done;
1856 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1857 if (kerr != KCGI_OK)
1858 goto done;
1860 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1861 "tree", KATTR__MAX);
1862 if (kerr != KCGI_OK)
1863 goto done;
1865 error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1866 if (error)
1867 goto done;
1869 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1870 done:
1871 gw_free_header(header);
1872 if (error == NULL && kerr != KCGI_OK)
1873 error = gw_kcgi_error(kerr);
1874 return error;
1877 static const struct got_error *
1878 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1880 const struct got_error *error = NULL;
1881 DIR *dt;
1882 char *dir_test;
1883 int opened = 0;
1885 if (asprintf(&dir_test, "%s/%s/%s",
1886 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1887 GOTWEB_GIT_DIR) == -1)
1888 return got_error_from_errno("asprintf");
1890 dt = opendir(dir_test);
1891 if (dt == NULL) {
1892 free(dir_test);
1893 } else {
1894 gw_dir->path = strdup(dir_test);
1895 if (gw_dir->path == NULL) {
1896 opened = 1;
1897 error = got_error_from_errno("strdup");
1898 goto errored;
1900 opened = 1;
1901 goto done;
1904 if (asprintf(&dir_test, "%s/%s/%s",
1905 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1906 GOTWEB_GOT_DIR) == -1) {
1907 dir_test = NULL;
1908 error = got_error_from_errno("asprintf");
1909 goto errored;
1912 dt = opendir(dir_test);
1913 if (dt == NULL)
1914 free(dir_test);
1915 else {
1916 opened = 1;
1917 error = got_error(GOT_ERR_NOT_GIT_REPO);
1918 goto errored;
1921 if (asprintf(&dir_test, "%s/%s",
1922 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1) {
1923 error = got_error_from_errno("asprintf");
1924 dir_test = NULL;
1925 goto errored;
1928 gw_dir->path = strdup(dir_test);
1929 if (gw_dir->path == NULL) {
1930 opened = 1;
1931 error = got_error_from_errno("strdup");
1932 goto errored;
1935 dt = opendir(dir_test);
1936 if (dt == NULL) {
1937 error = got_error_path(gw_dir->name, GOT_ERR_NOT_GIT_REPO);
1938 goto errored;
1939 } else
1940 opened = 1;
1941 done:
1942 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1943 gw_dir->path);
1944 if (error)
1945 goto errored;
1946 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1947 if (error)
1948 goto errored;
1949 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1950 NULL, TM_DIFF);
1951 if (error)
1952 goto errored;
1953 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1954 errored:
1955 free(dir_test);
1956 if (opened)
1957 if (dt && closedir(dt) == -1 && error == NULL)
1958 error = got_error_from_errno("closedir");
1959 return error;
1962 static const struct got_error *
1963 gw_load_got_paths(struct gw_trans *gw_trans)
1965 const struct got_error *error = NULL;
1966 DIR *d;
1967 struct dirent **sd_dent;
1968 struct gw_dir *gw_dir;
1969 struct stat st;
1970 unsigned int d_cnt, d_i;
1972 d = opendir(gw_trans->gw_conf->got_repos_path);
1973 if (d == NULL) {
1974 error = got_error_from_errno2("opendir",
1975 gw_trans->gw_conf->got_repos_path);
1976 return error;
1979 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1980 alphasort);
1981 if (d_cnt == -1) {
1982 error = got_error_from_errno2("scandir",
1983 gw_trans->gw_conf->got_repos_path);
1984 goto done;
1987 for (d_i = 0; d_i < d_cnt; d_i++) {
1988 if (gw_trans->gw_conf->got_max_repos > 0 &&
1989 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1990 break; /* account for parent and self */
1992 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1993 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1994 continue;
1996 error = gw_init_gw_dir(&gw_dir, sd_dent[d_i]->d_name);
1997 if (error)
1998 goto done;
2000 error = gw_load_got_path(gw_trans, gw_dir);
2001 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
2002 error = NULL;
2003 continue;
2005 else if (error)
2006 goto done;
2008 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
2009 !got_path_dir_is_empty(gw_dir->path)) {
2010 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
2011 entry);
2012 gw_trans->repos_total++;
2015 done:
2016 if (d && closedir(d) == -1 && error == NULL)
2017 error = got_error_from_errno("closedir");
2018 return error;
2021 static const struct got_error *
2022 gw_parse_querystring(struct gw_trans *gw_trans)
2024 const struct got_error *error = NULL;
2025 struct kpair *p;
2026 struct gw_query_action *action = NULL;
2027 unsigned int i;
2029 if (gw_trans->gw_req->fieldnmap[0]) {
2030 return got_error(GOT_ERR_QUERYSTRING);
2031 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
2032 /* define gw_trans->repo_path */
2033 gw_trans->repo_name = p->parsed.s;
2035 if (asprintf(&gw_trans->repo_path, "%s/%s",
2036 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
2037 return got_error_from_errno("asprintf");
2039 /* get action and set function */
2040 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION])) {
2041 for (i = 0; i < nitems(gw_query_funcs); i++) {
2042 action = &gw_query_funcs[i];
2043 if (action->func_name == NULL)
2044 continue;
2045 if (strcmp(action->func_name,
2046 p->parsed.s) == 0) {
2047 gw_trans->action = i;
2048 break;
2052 if (gw_trans->action == -1) {
2053 gw_trans->action = GW_ERR;
2054 gw_trans->error = got_error_msg(GOT_ERR_QUERYSTRING,
2055 p != NULL ? "bad action in querystring" :
2056 "no action in querystring");
2057 return error;
2060 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID])) {
2061 if (asprintf(&gw_trans->commit_id, "%s",
2062 p->parsed.s) == -1)
2063 return got_error_from_errno("asprintf");
2066 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
2067 gw_trans->repo_file = p->parsed.s;
2069 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER])) {
2070 if (asprintf(&gw_trans->repo_folder, "%s",
2071 p->parsed.s) == -1)
2072 return got_error_from_errno("asprintf");
2075 if ((p = gw_trans->gw_req->fieldmap[KEY_PREV_ID])) {
2076 if (asprintf(&gw_trans->prev_id, "%s",
2077 p->parsed.s) == -1)
2078 return got_error_from_errno("asprintf");
2081 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
2082 gw_trans->headref = p->parsed.s;
2084 error = gw_init_gw_dir(&gw_trans->gw_dir, gw_trans->repo_name);
2085 if (error)
2086 return error;
2088 gw_trans->error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
2089 } else
2090 gw_trans->action = GW_INDEX;
2092 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
2093 gw_trans->page = p->parsed.i;
2095 return error;
2098 static const struct got_error *
2099 gw_init_gw_dir(struct gw_dir **gw_dir, const char *dir)
2101 const struct got_error *error;
2103 *gw_dir = malloc(sizeof(**gw_dir));
2104 if (*gw_dir == NULL)
2105 return got_error_from_errno("malloc");
2107 if (asprintf(&(*gw_dir)->name, "%s", dir) == -1) {
2108 error = got_error_from_errno("asprintf");
2109 free(*gw_dir);
2110 *gw_dir = NULL;
2111 return error;
2114 return NULL;
2117 static const struct got_error *
2118 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
2120 enum kcgi_err kerr = KCGI_OK;
2122 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
2123 if (kerr != KCGI_OK)
2124 return gw_kcgi_error(kerr);
2125 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
2126 khttps[code]);
2127 if (kerr != KCGI_OK)
2128 return gw_kcgi_error(kerr);
2129 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
2130 kmimetypes[mime]);
2131 if (kerr != KCGI_OK)
2132 return gw_kcgi_error(kerr);
2133 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
2134 "nosniff");
2135 if (kerr != KCGI_OK)
2136 return gw_kcgi_error(kerr);
2137 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
2138 if (kerr != KCGI_OK)
2139 return gw_kcgi_error(kerr);
2140 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
2141 "1; mode=block");
2142 if (kerr != KCGI_OK)
2143 return gw_kcgi_error(kerr);
2145 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
2146 kerr = khttp_head(gw_trans->gw_req,
2147 kresps[KRESP_CONTENT_DISPOSITION],
2148 "attachment; filename=%s", gw_trans->repo_file);
2149 if (kerr != KCGI_OK)
2150 return gw_kcgi_error(kerr);
2153 kerr = khttp_body(gw_trans->gw_req);
2154 return gw_kcgi_error(kerr);
2157 static const struct got_error *
2158 gw_display_index(struct gw_trans *gw_trans)
2160 const struct got_error *error;
2161 enum kcgi_err kerr = KCGI_OK;
2163 /* catch early querystring errors */
2164 if (gw_trans->error)
2165 gw_trans->action = GW_ERR;
2167 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
2168 if (error)
2169 return error;
2171 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
2172 if (kerr != KCGI_OK)
2173 return gw_kcgi_error(kerr);
2175 if (gw_trans->action != GW_BLOB) {
2176 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
2177 gw_query_funcs[gw_trans->action].template);
2178 if (kerr != KCGI_OK) {
2179 khtml_close(gw_trans->gw_html_req);
2180 return gw_kcgi_error(kerr);
2184 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
2187 static const struct got_error *
2188 gw_error(struct gw_trans *gw_trans)
2190 enum kcgi_err kerr = KCGI_OK;
2192 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->error->msg);
2194 return gw_kcgi_error(kerr);
2197 static int
2198 gw_template(size_t key, void *arg)
2200 const struct got_error *error = NULL;
2201 enum kcgi_err kerr = KCGI_OK;
2202 struct gw_trans *gw_trans = arg;
2203 char *ati = NULL, *fic32 = NULL, *fic16 = NULL;
2204 char *swm = NULL, *spt = NULL, *css = NULL, *logo = NULL;
2206 if (asprintf(&ati, "%s%s", gw_trans->gw_conf->got_www_path,
2207 "/apple-touch-icon.png") == -1)
2208 goto err;
2209 if (asprintf(&fic32, "%s%s", gw_trans->gw_conf->got_www_path,
2210 "/favicon-32x32.png") == -1)
2211 goto err;
2212 if (asprintf(&fic16, "%s%s", gw_trans->gw_conf->got_www_path,
2213 "/favicon-16x16.png") == -1)
2214 goto err;
2215 if (asprintf(&swm, "%s%s", gw_trans->gw_conf->got_www_path,
2216 "/site.webmanifest") == -1)
2217 goto err;
2218 if (asprintf(&spt, "%s%s", gw_trans->gw_conf->got_www_path,
2219 "/safari-pinned-tab.svg") == -1)
2220 goto err;
2221 if (asprintf(&css, "%s%s", gw_trans->gw_conf->got_www_path,
2222 "/gotweb.css") == -1)
2223 goto err;
2224 if (asprintf(&logo, "%s%s%s", gw_trans->gw_conf->got_www_path,
2225 gw_trans->gw_conf->got_www_path ? "/" : "",
2226 gw_trans->gw_conf->got_logo) == -1)
2227 goto err;
2229 switch (key) {
2230 case (TEMPL_HEAD):
2231 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2232 KATTR_NAME, "viewport",
2233 KATTR_CONTENT, "initial-scale=.75, user-scalable=yes",
2234 KATTR__MAX);
2235 if (kerr != KCGI_OK)
2236 return 0;
2237 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2238 if (kerr != KCGI_OK)
2239 return 0;
2240 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2241 KATTR_CHARSET, "utf-8",
2242 KATTR__MAX);
2243 if (kerr != KCGI_OK)
2244 return 0;
2245 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2246 if (kerr != KCGI_OK)
2247 return 0;
2248 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2249 KATTR_NAME, "msapplication-TileColor",
2250 KATTR_CONTENT, "#da532c", 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_META,
2257 KATTR_NAME, "theme-color",
2258 KATTR_CONTENT, "#ffffff", 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, "apple-touch-icon", KATTR_SIZES, "180x180",
2266 KATTR_HREF, ati, KATTR__MAX);
2267 if (kerr != KCGI_OK)
2268 return 0;
2269 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2270 if (kerr != KCGI_OK)
2271 return 0;
2272 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2273 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2274 "32x32", KATTR_HREF, fic32, KATTR__MAX);
2275 if (kerr != KCGI_OK)
2276 return 0;
2277 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2278 if (kerr != KCGI_OK)
2279 return 0;
2280 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2281 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2282 "16x16", KATTR_HREF, fic16, KATTR__MAX);
2283 if (kerr != KCGI_OK)
2284 return 0;
2285 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2286 if (kerr != KCGI_OK)
2287 return 0;
2288 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2289 KATTR_REL, "manifest", KATTR_HREF, swm,
2290 KATTR__MAX);
2291 if (kerr != KCGI_OK)
2292 return 0;
2293 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2294 if (kerr != KCGI_OK)
2295 return 0;
2296 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2297 KATTR_REL, "mask-icon", KATTR_HREF,
2298 spt, KATTR__MAX);
2299 if (kerr != KCGI_OK)
2300 return 0;
2301 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2302 if (kerr != KCGI_OK)
2303 return 0;
2304 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2305 KATTR_REL, "stylesheet", KATTR_TYPE, "text/css",
2306 KATTR_HREF, css, KATTR__MAX);
2307 if (kerr != KCGI_OK)
2308 return 0;
2309 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2310 if (kerr != KCGI_OK)
2311 return 0;
2312 break;
2313 case(TEMPL_HEADER):
2314 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2315 KATTR_ID, "got_link", KATTR__MAX);
2316 if (kerr != KCGI_OK)
2317 return 0;
2318 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2319 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
2320 KATTR_TARGET, "_sotd", KATTR__MAX);
2321 if (kerr != KCGI_OK)
2322 return 0;
2323 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
2324 KATTR_SRC, logo, KATTR__MAX);
2325 if (kerr != KCGI_OK)
2326 return 0;
2327 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2328 if (kerr != KCGI_OK)
2329 return 0;
2330 break;
2331 case (TEMPL_SITEPATH):
2332 error = gw_output_site_link(gw_trans);
2333 if (error)
2334 return 0;
2335 break;
2336 case(TEMPL_TITLE):
2337 if (gw_trans->gw_conf->got_site_name != NULL) {
2338 kerr = khtml_puts(gw_trans->gw_html_req,
2339 gw_trans->gw_conf->got_site_name);
2340 if (kerr != KCGI_OK)
2341 return 0;
2343 break;
2344 case (TEMPL_SEARCH):
2345 break;
2346 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
2347 "search", KATTR__MAX);
2348 if (kerr != KCGI_OK)
2349 return 0;
2350 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
2351 KATTR_METHOD, "POST", KATTR__MAX);
2352 if (kerr != KCGI_OK)
2353 return 0;
2354 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
2355 "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
2356 KATTR_MAXLENGTH, "50", KATTR__MAX);
2357 if (kerr != KCGI_OK)
2358 return 0;
2359 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
2360 KATTR__MAX);
2361 if (kerr != KCGI_OK)
2362 return 0;
2363 kerr = khtml_puts(gw_trans->gw_html_req, "Search");
2364 if (kerr != KCGI_OK)
2365 return 0;
2366 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
2367 if (kerr != KCGI_OK)
2368 return 0;
2369 break;
2370 case(TEMPL_SITEOWNER):
2371 if (gw_trans->gw_conf->got_site_owner != NULL &&
2372 gw_trans->gw_conf->got_show_site_owner) {
2373 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2374 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
2375 if (kerr != KCGI_OK)
2376 return 0;
2377 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2378 KATTR_ID, "site_owner", KATTR__MAX);
2379 if (kerr != KCGI_OK)
2380 return 0;
2381 kerr = khtml_puts(gw_trans->gw_html_req,
2382 gw_trans->gw_conf->got_site_owner);
2383 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2384 if (kerr != KCGI_OK)
2385 return 0;
2387 break;
2388 case(TEMPL_CONTENT):
2389 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
2390 if (error) {
2391 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2392 KATTR_ID, "tmpl_err", KATTR__MAX);
2393 if (kerr != KCGI_OK)
2394 return 0;
2395 kerr = khttp_printf(gw_trans->gw_req, "Error: %s",
2396 error->msg);
2397 if (kerr != KCGI_OK)
2398 return 0;
2399 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2400 if (kerr != KCGI_OK)
2401 return 0;
2403 break;
2404 default:
2405 return 0;
2407 free(ati);
2408 free(fic32);
2409 free(fic16);
2410 free(swm);
2411 free(spt);
2412 free(css);
2413 free(logo);
2414 return 1;
2415 err:
2416 free(ati);
2417 free(fic32);
2418 free(fic16);
2419 free(swm);
2420 free(spt);
2421 free(css);
2422 free(logo);
2423 return 0;
2426 static const struct got_error *
2427 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
2429 const struct got_error *error = NULL;
2430 enum kcgi_err kerr = KCGI_OK;
2432 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2433 KATTR_ID, "header_commit_title", KATTR__MAX);
2434 if (kerr != KCGI_OK)
2435 goto done;
2436 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
2437 if (kerr != KCGI_OK)
2438 goto done;
2439 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2440 if (kerr != KCGI_OK)
2441 goto done;
2442 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2443 KATTR_ID, "header_commit", KATTR__MAX);
2444 if (kerr != KCGI_OK)
2445 goto done;
2446 kerr = khtml_printf(gw_trans->gw_html_req, "%s ", str1);
2447 if (kerr != KCGI_OK)
2448 goto done;
2449 if (str2 != NULL) {
2450 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
2451 KATTR_ID, "refs_str", KATTR__MAX);
2452 if (kerr != KCGI_OK)
2453 goto done;
2454 kerr = khtml_printf(gw_trans->gw_html_req, "(%s)", str2);
2455 if (kerr != KCGI_OK)
2456 goto done;
2457 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2458 if (kerr != KCGI_OK)
2459 goto done;
2461 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2462 done:
2463 if (error == NULL && kerr != KCGI_OK)
2464 error = gw_kcgi_error(kerr);
2465 return error;
2468 static const struct got_error *
2469 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2471 const struct got_error *error = NULL;
2472 enum kcgi_err kerr = KCGI_OK;
2474 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2475 KATTR_ID, "header_diff_title", KATTR__MAX);
2476 if (kerr != KCGI_OK)
2477 goto done;
2478 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2479 if (kerr != KCGI_OK)
2480 goto done;
2481 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2482 if (kerr != KCGI_OK)
2483 goto done;
2484 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2485 KATTR_ID, "header_diff", KATTR__MAX);
2486 if (kerr != KCGI_OK)
2487 goto done;
2488 if (str1 != NULL) {
2489 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2490 if (kerr != KCGI_OK)
2491 goto done;
2493 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2494 if (kerr != KCGI_OK)
2495 goto done;
2496 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2497 if (kerr != KCGI_OK)
2498 goto done;
2499 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2500 done:
2501 if (error == NULL && kerr != KCGI_OK)
2502 error = gw_kcgi_error(kerr);
2503 return error;
2506 static const struct got_error *
2507 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2509 const struct got_error *error = NULL;
2510 enum kcgi_err kerr = KCGI_OK;
2512 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2513 KATTR_ID, "header_age_title", KATTR__MAX);
2514 if (kerr != KCGI_OK)
2515 goto done;
2516 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2517 if (kerr != KCGI_OK)
2518 goto done;
2519 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2520 if (kerr != KCGI_OK)
2521 goto done;
2522 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2523 KATTR_ID, "header_age", KATTR__MAX);
2524 if (kerr != KCGI_OK)
2525 goto done;
2526 kerr = khtml_puts(gw_trans->gw_html_req, str);
2527 if (kerr != KCGI_OK)
2528 goto done;
2529 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2530 done:
2531 if (error == NULL && kerr != KCGI_OK)
2532 error = gw_kcgi_error(kerr);
2533 return error;
2536 static const struct got_error *
2537 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2539 const struct got_error *error = NULL;
2540 enum kcgi_err kerr = KCGI_OK;
2542 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2543 KATTR_ID, "header_author_title", KATTR__MAX);
2544 if (kerr != KCGI_OK)
2545 goto done;
2546 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2547 if (kerr != KCGI_OK)
2548 goto done;
2549 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2550 if (kerr != KCGI_OK)
2551 goto done;
2552 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2553 KATTR_ID, "header_author", KATTR__MAX);
2554 if (kerr != KCGI_OK)
2555 goto done;
2556 kerr = khtml_puts(gw_trans->gw_html_req, str);
2557 if (kerr != KCGI_OK)
2558 goto done;
2559 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2560 done:
2561 if (error == NULL && kerr != KCGI_OK)
2562 error = gw_kcgi_error(kerr);
2563 return error;
2566 static const struct got_error *
2567 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2569 const struct got_error *error = NULL;
2570 enum kcgi_err kerr = KCGI_OK;
2572 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2573 KATTR_ID, "header_committer_title", KATTR__MAX);
2574 if (kerr != KCGI_OK)
2575 goto done;
2576 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2577 if (kerr != KCGI_OK)
2578 goto done;
2579 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2580 if (kerr != KCGI_OK)
2581 goto done;
2582 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2583 KATTR_ID, "header_committer", KATTR__MAX);
2584 if (kerr != KCGI_OK)
2585 goto done;
2586 kerr = khtml_puts(gw_trans->gw_html_req, str);
2587 if (kerr != KCGI_OK)
2588 goto done;
2589 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2590 done:
2591 if (error == NULL && kerr != KCGI_OK)
2592 error = gw_kcgi_error(kerr);
2593 return error;
2596 static const struct got_error *
2597 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2599 const struct got_error *error = NULL;
2600 enum kcgi_err kerr = KCGI_OK;
2602 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2603 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2604 if (kerr != KCGI_OK)
2605 goto done;
2606 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2607 if (kerr != KCGI_OK)
2608 goto done;
2609 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2610 if (kerr != KCGI_OK)
2611 goto done;
2612 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2613 KATTR_ID, "header_commit_msg", KATTR__MAX);
2614 if (kerr != KCGI_OK)
2615 goto done;
2616 kerr = khttp_puts(gw_trans->gw_req, str);
2617 if (kerr != KCGI_OK)
2618 goto done;
2619 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2620 done:
2621 if (error == NULL && kerr != KCGI_OK)
2622 error = gw_kcgi_error(kerr);
2623 return error;
2626 static const struct got_error *
2627 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2629 const struct got_error *error = NULL;
2630 enum kcgi_err kerr = KCGI_OK;
2632 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2633 KATTR_ID, "header_tree_title", KATTR__MAX);
2634 if (kerr != KCGI_OK)
2635 goto done;
2636 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2637 if (kerr != KCGI_OK)
2638 goto done;
2639 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2640 if (kerr != KCGI_OK)
2641 goto done;
2642 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2643 KATTR_ID, "header_tree", KATTR__MAX);
2644 if (kerr != KCGI_OK)
2645 goto done;
2646 kerr = khtml_puts(gw_trans->gw_html_req, str);
2647 if (kerr != KCGI_OK)
2648 goto done;
2649 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2650 done:
2651 if (error == NULL && kerr != KCGI_OK)
2652 error = gw_kcgi_error(kerr);
2653 return error;
2656 static const struct got_error *
2657 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2658 char *dir)
2660 const struct got_error *error = NULL;
2661 FILE *f = NULL;
2662 char *d_file = NULL;
2663 unsigned int len;
2664 size_t n;
2666 *description = NULL;
2667 if (gw_trans->gw_conf->got_show_repo_description == 0)
2668 return NULL;
2670 if (asprintf(&d_file, "%s/description", dir) == -1)
2671 return got_error_from_errno("asprintf");
2673 f = fopen(d_file, "r");
2674 if (f == NULL) {
2675 if (errno == ENOENT || errno == EACCES)
2676 return NULL;
2677 error = got_error_from_errno2("fopen", d_file);
2678 goto done;
2681 if (fseek(f, 0, SEEK_END) == -1) {
2682 error = got_ferror(f, GOT_ERR_IO);
2683 goto done;
2685 len = ftell(f);
2686 if (len == -1) {
2687 error = got_ferror(f, GOT_ERR_IO);
2688 goto done;
2690 if (fseek(f, 0, SEEK_SET) == -1) {
2691 error = got_ferror(f, GOT_ERR_IO);
2692 goto done;
2694 *description = calloc(len + 1, sizeof(**description));
2695 if (*description == NULL) {
2696 error = got_error_from_errno("calloc");
2697 goto done;
2700 n = fread(*description, 1, len, f);
2701 if (n == 0 && ferror(f))
2702 error = got_ferror(f, GOT_ERR_IO);
2703 done:
2704 if (f != NULL && fclose(f) == EOF && error == NULL)
2705 error = got_error_from_errno("fclose");
2706 free(d_file);
2707 return error;
2710 static const struct got_error *
2711 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2713 struct tm tm;
2714 time_t diff_time;
2715 char *years = "years ago", *months = "months ago";
2716 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2717 char *minutes = "minutes ago", *seconds = "seconds ago";
2718 char *now = "right now";
2719 char *s;
2720 char datebuf[29];
2722 *repo_age = NULL;
2724 switch (ref_tm) {
2725 case TM_DIFF:
2726 diff_time = time(NULL) - committer_time;
2727 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2728 if (asprintf(repo_age, "%lld %s",
2729 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2730 return got_error_from_errno("asprintf");
2731 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2732 if (asprintf(repo_age, "%lld %s",
2733 (diff_time / 60 / 60 / 24 / (365 / 12)),
2734 months) == -1)
2735 return got_error_from_errno("asprintf");
2736 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2737 if (asprintf(repo_age, "%lld %s",
2738 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2739 return got_error_from_errno("asprintf");
2740 } else if (diff_time > 60 * 60 * 24 * 2) {
2741 if (asprintf(repo_age, "%lld %s",
2742 (diff_time / 60 / 60 / 24), days) == -1)
2743 return got_error_from_errno("asprintf");
2744 } else if (diff_time > 60 * 60 * 2) {
2745 if (asprintf(repo_age, "%lld %s",
2746 (diff_time / 60 / 60), hours) == -1)
2747 return got_error_from_errno("asprintf");
2748 } else if (diff_time > 60 * 2) {
2749 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2750 minutes) == -1)
2751 return got_error_from_errno("asprintf");
2752 } else if (diff_time > 2) {
2753 if (asprintf(repo_age, "%lld %s", diff_time,
2754 seconds) == -1)
2755 return got_error_from_errno("asprintf");
2756 } else {
2757 if (asprintf(repo_age, "%s", now) == -1)
2758 return got_error_from_errno("asprintf");
2760 break;
2761 case TM_LONG:
2762 if (gmtime_r(&committer_time, &tm) == NULL)
2763 return got_error_from_errno("gmtime_r");
2765 s = asctime_r(&tm, datebuf);
2766 if (s == NULL)
2767 return got_error_from_errno("asctime_r");
2769 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2770 return got_error_from_errno("asprintf");
2771 break;
2773 return NULL;
2776 static const struct got_error *
2777 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2778 const char *refname, int ref_tm)
2780 const struct got_error *error = NULL;
2781 struct got_repository *repo = NULL;
2782 struct got_commit_object *commit = NULL;
2783 struct got_reflist_head refs;
2784 struct got_reflist_entry *re;
2785 time_t committer_time = 0, cmp_time = 0;
2787 *repo_age = NULL;
2788 TAILQ_INIT(&refs);
2790 if (gw_trans->gw_conf->got_show_repo_age == 0)
2791 return NULL;
2793 if (gw_trans->repo)
2794 repo = gw_trans->repo;
2795 else {
2796 error = got_repo_open(&repo, dir, NULL);
2797 if (error)
2798 return error;
2801 error = got_ref_list(&refs, repo, "refs/heads",
2802 got_ref_cmp_by_name, NULL);
2803 if (error)
2804 goto done;
2807 * Find the youngest branch tip in the repository, or the age of
2808 * the a specific branch tip if a name was provided by the caller.
2810 TAILQ_FOREACH(re, &refs, entry) {
2811 struct got_object_id *id = NULL;
2813 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
2814 continue;
2816 error = got_ref_resolve(&id, repo, re->ref);
2817 if (error)
2818 goto done;
2820 error = got_object_open_as_commit(&commit, repo, id);
2821 free(id);
2822 if (error)
2823 goto done;
2825 committer_time =
2826 got_object_commit_get_committer_time(commit);
2827 got_object_commit_close(commit);
2828 if (cmp_time < committer_time)
2829 cmp_time = committer_time;
2831 if (refname)
2832 break;
2835 if (cmp_time != 0) {
2836 committer_time = cmp_time;
2837 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2839 done:
2840 got_ref_list_free(&refs);
2841 if (gw_trans->repo == NULL) {
2842 const struct got_error *close_err = got_repo_close(repo);
2843 if (error == NULL)
2844 error = close_err;
2846 return error;
2849 static const struct got_error *
2850 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2852 const struct got_error *error;
2853 FILE *f = NULL;
2854 struct got_object_id *id1 = NULL, *id2 = NULL;
2855 char *label1 = NULL, *label2 = NULL, *line = NULL;
2856 int obj_type;
2857 size_t linesize = 0;
2858 ssize_t linelen;
2859 enum kcgi_err kerr = KCGI_OK;
2861 f = got_opentemp();
2862 if (f == NULL)
2863 return NULL;
2865 if (header->parent_id != NULL &&
2866 strncmp(header->parent_id, "/dev/null", 9) != 0) {
2867 error = got_repo_match_object_id(&id1, &label1,
2868 header->parent_id, GOT_OBJ_TYPE_ANY,
2869 &header->refs, gw_trans->repo);
2870 if (error)
2871 goto done;
2874 error = got_repo_match_object_id(&id2, &label2,
2875 header->commit_id, GOT_OBJ_TYPE_ANY, &header->refs,
2876 gw_trans->repo);
2877 if (error)
2878 goto done;
2880 error = got_object_get_type(&obj_type, gw_trans->repo, id2);
2881 if (error)
2882 goto done;
2883 switch (obj_type) {
2884 case GOT_OBJ_TYPE_BLOB:
2885 error = got_diff_objects_as_blobs(NULL, NULL, id1, id2,
2886 NULL, NULL, 3, 0, 0, gw_trans->repo, f);
2887 break;
2888 case GOT_OBJ_TYPE_TREE:
2889 error = got_diff_objects_as_trees(NULL, NULL, id1, id2,
2890 "", "", 3, 0, 0, gw_trans->repo, f);
2891 break;
2892 case GOT_OBJ_TYPE_COMMIT:
2893 error = got_diff_objects_as_commits(NULL, NULL, id1, id2,
2894 3, 0, 0, gw_trans->repo, f);
2895 break;
2896 default:
2897 error = got_error(GOT_ERR_OBJ_TYPE);
2899 if (error)
2900 goto done;
2902 if (fseek(f, 0, SEEK_SET) == -1) {
2903 error = got_ferror(f, GOT_ERR_IO);
2904 goto done;
2907 while ((linelen = getline(&line, &linesize, f)) != -1) {
2908 error = gw_colordiff_line(gw_trans, line);
2909 if (error)
2910 goto done;
2911 /* XXX: KHTML_PRETTY breaks this */
2912 kerr = khtml_puts(gw_trans->gw_html_req, line);
2913 if (kerr != KCGI_OK)
2914 goto done;
2915 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2916 if (kerr != KCGI_OK)
2917 goto done;
2919 if (linelen == -1 && ferror(f))
2920 error = got_error_from_errno("getline");
2921 done:
2922 if (f && fclose(f) == EOF && error == NULL)
2923 error = got_error_from_errno("fclose");
2924 free(line);
2925 free(label1);
2926 free(label2);
2927 free(id1);
2928 free(id2);
2930 if (error == NULL && kerr != KCGI_OK)
2931 error = gw_kcgi_error(kerr);
2932 return error;
2935 static const struct got_error *
2936 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2938 const struct got_error *error = NULL, *close_err;
2939 struct got_repository *repo;
2940 const char *gitconfig_owner;
2942 *owner = NULL;
2944 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2945 return NULL;
2947 error = got_repo_open(&repo, dir, NULL);
2948 if (error)
2949 return error;
2950 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2951 if (gitconfig_owner) {
2952 *owner = strdup(gitconfig_owner);
2953 if (*owner == NULL)
2954 error = got_error_from_errno("strdup");
2956 close_err = got_repo_close(repo);
2957 if (error == NULL)
2958 error = close_err;
2959 return error;
2962 static const struct got_error *
2963 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2965 const struct got_error *error = NULL;
2966 FILE *f;
2967 char *d_file = NULL;
2968 unsigned int len;
2969 size_t n;
2971 *url = NULL;
2973 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2974 return got_error_from_errno("asprintf");
2976 f = fopen(d_file, "r");
2977 if (f == NULL) {
2978 if (errno != ENOENT && errno != EACCES)
2979 error = got_error_from_errno2("fopen", d_file);
2980 goto done;
2983 if (fseek(f, 0, SEEK_END) == -1) {
2984 error = got_ferror(f, GOT_ERR_IO);
2985 goto done;
2987 len = ftell(f);
2988 if (len == -1) {
2989 error = got_ferror(f, GOT_ERR_IO);
2990 goto done;
2992 if (fseek(f, 0, SEEK_SET) == -1) {
2993 error = got_ferror(f, GOT_ERR_IO);
2994 goto done;
2997 *url = calloc(len + 1, sizeof(**url));
2998 if (*url == NULL) {
2999 error = got_error_from_errno("calloc");
3000 goto done;
3003 n = fread(*url, 1, len, f);
3004 if (n == 0 && ferror(f))
3005 error = got_ferror(f, GOT_ERR_IO);
3006 done:
3007 if (f && fclose(f) == EOF && error == NULL)
3008 error = got_error_from_errno("fclose");
3009 free(d_file);
3010 return NULL;
3013 static const struct got_error *
3014 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
3015 int limit, int tag_type)
3017 const struct got_error *error = NULL;
3018 struct got_reflist_head refs;
3019 struct got_reflist_entry *re;
3020 char *age = NULL;
3021 char *id_str = NULL, *newline, *href_commits = NULL;
3022 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
3023 struct got_tag_object *tag = NULL;
3024 enum kcgi_err kerr = KCGI_OK;
3025 int summary_header_displayed = 0, chk_next = 0;
3026 int tag_count = 0, commit_found = 0, c_cnt = 0;
3028 TAILQ_INIT(&refs);
3030 error = got_ref_list(&refs, gw_trans->repo, "refs/tags",
3031 got_ref_cmp_tags, gw_trans->repo);
3032 if (error)
3033 goto done;
3035 TAILQ_FOREACH(re, &refs, entry) {
3036 const char *refname;
3037 const char *tagger;
3038 const char *tag_commit;
3039 time_t tagger_time;
3040 struct got_object_id *id;
3041 struct got_commit_object *commit = NULL;
3043 refname = got_ref_get_name(re->ref);
3044 if (strncmp(refname, "refs/tags/", 10) != 0)
3045 continue;
3046 refname += 10;
3048 error = got_ref_resolve(&id, gw_trans->repo, re->ref);
3049 if (error)
3050 goto done;
3052 error = got_object_open_as_tag(&tag, gw_trans->repo, id);
3053 if (error) {
3054 if (error->code != GOT_ERR_OBJ_TYPE) {
3055 free(id);
3056 goto done;
3058 /* "lightweight" tag */
3059 error = got_object_open_as_commit(&commit,
3060 gw_trans->repo, id);
3061 if (error) {
3062 free(id);
3063 goto done;
3065 tagger = got_object_commit_get_committer(commit);
3066 tagger_time =
3067 got_object_commit_get_committer_time(commit);
3068 error = got_object_id_str(&id_str, id);
3069 free(id);
3070 } else {
3071 free(id);
3072 tagger = got_object_tag_get_tagger(tag);
3073 tagger_time = got_object_tag_get_tagger_time(tag);
3074 error = got_object_id_str(&id_str,
3075 got_object_tag_get_object_id(tag));
3077 if (error)
3078 goto done;
3080 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
3081 strlen(id_str)) != 0)
3082 continue;
3084 if (tag_type == TAGBRIEF && gw_trans->commit_id &&
3085 commit_found == 0 && strncmp(id_str, gw_trans->commit_id,
3086 strlen(id_str)) != 0)
3087 continue;
3088 else
3089 commit_found = 1;
3091 tag_count++;
3093 if (chk_next) {
3094 gw_trans->next_id = strdup(id_str);
3095 if (gw_trans->next_id == NULL)
3096 error = got_error_from_errno("strdup");
3097 goto prev;
3100 if (commit) {
3101 error = got_object_commit_get_logmsg(&tag_commit0,
3102 commit);
3103 if (error)
3104 goto done;
3105 got_object_commit_close(commit);
3106 } else {
3107 tag_commit0 = strdup(got_object_tag_get_message(tag));
3108 if (tag_commit0 == NULL) {
3109 error = got_error_from_errno("strdup");
3110 goto done;
3114 tag_commit = tag_commit0;
3115 while (*tag_commit == '\n')
3116 tag_commit++;
3118 switch (tag_type) {
3119 case TAGBRIEF:
3120 newline = strchr(tag_commit, '\n');
3121 if (newline)
3122 *newline = '\0';
3124 if (summary_header_displayed == 0) {
3125 kerr = khtml_attr(gw_trans->gw_html_req,
3126 KELEM_DIV, KATTR_ID,
3127 "summary_tags_title_wrapper", KATTR__MAX);
3128 if (kerr != KCGI_OK)
3129 goto done;
3130 kerr = khtml_attr(gw_trans->gw_html_req,
3131 KELEM_DIV, KATTR_ID,
3132 "summary_tags_title", KATTR__MAX);
3133 if (kerr != KCGI_OK)
3134 goto done;
3135 kerr = khtml_puts(gw_trans->gw_html_req,
3136 "Tags");
3137 if (kerr != KCGI_OK)
3138 goto done;
3139 kerr = khtml_closeelem(gw_trans->gw_html_req,
3140 2);
3141 if (kerr != KCGI_OK)
3142 goto done;
3143 kerr = khtml_attr(gw_trans->gw_html_req,
3144 KELEM_DIV, KATTR_ID,
3145 "summary_tags_content", KATTR__MAX);
3146 if (kerr != KCGI_OK)
3147 goto done;
3148 summary_header_displayed = 1;
3151 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3152 KATTR_ID, "tag_wrapper", KATTR__MAX);
3153 if (kerr != KCGI_OK)
3154 goto done;
3155 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3156 KATTR_ID, "tag_age", KATTR__MAX);
3157 if (kerr != KCGI_OK)
3158 goto done;
3159 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
3160 if (error)
3161 goto done;
3162 kerr = khtml_puts(gw_trans->gw_html_req,
3163 age ? age : "");
3164 if (kerr != KCGI_OK)
3165 goto done;
3166 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3167 if (kerr != KCGI_OK)
3168 goto done;
3169 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3170 KATTR_ID, "tag", KATTR__MAX);
3171 if (kerr != KCGI_OK)
3172 goto done;
3173 kerr = khtml_puts(gw_trans->gw_html_req, refname);
3174 if (kerr != KCGI_OK)
3175 goto done;
3176 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3177 if (kerr != KCGI_OK)
3178 goto done;
3179 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3180 KATTR_ID, "tag_name", KATTR__MAX);
3181 if (kerr != KCGI_OK)
3182 goto done;
3184 href_tag = khttp_urlpart(NULL, NULL, "gotweb", "path",
3185 gw_trans->repo_name, "action", "tag", "commit",
3186 id_str, NULL);
3187 if (href_tag == NULL) {
3188 error = got_error_from_errno("khttp_urlpart");
3189 goto done;
3191 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3192 KATTR_HREF, href_tag, KATTR__MAX);
3193 if (kerr != KCGI_OK)
3194 goto done;
3195 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
3196 if (kerr != KCGI_OK)
3197 goto done;
3198 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3199 if (kerr != KCGI_OK)
3200 goto done;
3202 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3203 KATTR_ID, "navs_wrapper", KATTR__MAX);
3204 if (kerr != KCGI_OK)
3205 goto done;
3206 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3207 KATTR_ID, "navs", KATTR__MAX);
3208 if (kerr != KCGI_OK)
3209 goto done;
3211 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3212 KATTR_HREF, href_tag, KATTR__MAX);
3213 if (kerr != KCGI_OK)
3214 goto done;
3215 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
3216 if (kerr != KCGI_OK)
3217 goto done;
3218 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3219 if (kerr != KCGI_OK)
3220 goto done;
3222 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3223 if (kerr != KCGI_OK)
3224 goto done;
3226 href_briefs = khttp_urlpart(NULL, NULL, "gotweb",
3227 "path", gw_trans->repo_name, "action", "briefs",
3228 "commit", id_str, NULL);
3229 if (href_briefs == NULL) {
3230 error = got_error_from_errno("khttp_urlpart");
3231 goto done;
3233 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3234 KATTR_HREF, href_briefs, KATTR__MAX);
3235 if (kerr != KCGI_OK)
3236 goto done;
3237 kerr = khtml_puts(gw_trans->gw_html_req,
3238 "commit briefs");
3239 if (kerr != KCGI_OK)
3240 goto done;
3241 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3242 if (kerr != KCGI_OK)
3243 goto done;
3245 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3246 if (kerr != KCGI_OK)
3247 goto done;
3249 href_commits = khttp_urlpart(NULL, NULL, "gotweb",
3250 "path", gw_trans->repo_name, "action", "commits",
3251 "commit", id_str, NULL);
3252 if (href_commits == NULL) {
3253 error = got_error_from_errno("khttp_urlpart");
3254 goto done;
3256 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3257 KATTR_HREF, href_commits, KATTR__MAX);
3258 if (kerr != KCGI_OK)
3259 goto done;
3260 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
3261 if (kerr != KCGI_OK)
3262 goto done;
3263 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3264 if (kerr != KCGI_OK)
3265 goto done;
3267 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3268 KATTR_ID, "dotted_line", KATTR__MAX);
3269 if (kerr != KCGI_OK)
3270 goto done;
3271 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3272 if (kerr != KCGI_OK)
3273 goto done;
3274 break;
3275 case TAGFULL:
3276 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3277 KATTR_ID, "tag_info_date_title", KATTR__MAX);
3278 if (kerr != KCGI_OK)
3279 goto done;
3280 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
3281 if (kerr != KCGI_OK)
3282 goto done;
3283 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3284 if (kerr != KCGI_OK)
3285 goto done;
3286 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3287 KATTR_ID, "tag_info_date", KATTR__MAX);
3288 if (kerr != KCGI_OK)
3289 goto done;
3290 error = gw_get_time_str(&age, tagger_time, TM_LONG);
3291 if (error)
3292 goto done;
3293 kerr = khtml_puts(gw_trans->gw_html_req,
3294 age ? age : "");
3295 if (kerr != KCGI_OK)
3296 goto done;
3297 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3298 if (kerr != KCGI_OK)
3299 goto done;
3301 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3302 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
3303 if (kerr != KCGI_OK)
3304 goto done;
3305 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
3306 if (kerr != KCGI_OK)
3307 goto done;
3308 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3309 if (kerr != KCGI_OK)
3310 goto done;
3311 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3312 KATTR_ID, "tag_info_date", KATTR__MAX);
3313 if (kerr != KCGI_OK)
3314 goto done;
3315 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
3316 if (kerr != KCGI_OK)
3317 goto done;
3318 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3319 if (kerr != KCGI_OK)
3320 goto done;
3322 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3323 KATTR_ID, "tag_info", KATTR__MAX);
3324 if (kerr != KCGI_OK)
3325 goto done;
3326 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
3327 if (kerr != KCGI_OK)
3328 goto done;
3329 break;
3330 default:
3331 break;
3333 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3334 if (kerr != KCGI_OK)
3335 goto done;
3337 if (limit && --limit == 0)
3338 chk_next = 1;
3340 if (tag)
3341 got_object_tag_close(tag);
3342 tag = NULL;
3343 free(id_str);
3344 id_str = NULL;
3345 free(age);
3346 age = NULL;
3347 free(tag_commit0);
3348 tag_commit0 = NULL;
3349 free(href_tag);
3350 href_tag = NULL;
3351 free(href_briefs);
3352 href_briefs = NULL;
3353 free(href_commits);
3354 href_commits = NULL;
3356 if (tag_count == 0) {
3357 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3358 "summary_tags_title_wrapper", KATTR__MAX);
3359 if (kerr != KCGI_OK)
3360 goto done;
3361 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3362 "summary_tags_title", KATTR__MAX);
3363 if (kerr != KCGI_OK)
3364 goto done;
3365 kerr = khtml_puts(gw_trans->gw_html_req, "Tags");
3366 if (kerr != KCGI_OK)
3367 goto done;
3368 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3369 if (kerr != KCGI_OK)
3370 goto done;
3371 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3372 "summary_tags_content", KATTR__MAX);
3373 if (kerr != KCGI_OK)
3374 goto done;
3375 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3376 "tags_info", KATTR__MAX);
3377 if (kerr != KCGI_OK)
3378 goto done;
3379 kerr = khttp_puts(gw_trans->gw_req,
3380 "There are no tags for this repo.");
3381 if (kerr != KCGI_OK)
3382 goto done;
3383 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3384 goto done;
3386 prev:
3387 commit_found = 0;
3388 TAILQ_FOREACH_REVERSE(re, &refs, got_reflist_head, entry) {
3389 const char *refname;
3390 const char *tagger;
3391 time_t tagger_time;
3392 struct got_object_id *id;
3393 struct got_commit_object *commit = NULL;
3395 refname = got_ref_get_name(re->ref);
3396 if (strncmp(refname, "refs/tags/", 10) != 0)
3397 continue;
3398 refname += 10;
3400 error = got_ref_resolve(&id, gw_trans->repo, re->ref);
3401 if (error)
3402 goto done;
3404 error = got_object_open_as_tag(&tag, gw_trans->repo, id);
3405 if (error) {
3406 if (error->code != GOT_ERR_OBJ_TYPE) {
3407 free(id);
3408 goto done;
3410 /* "lightweight" tag */
3411 error = got_object_open_as_commit(&commit,
3412 gw_trans->repo, id);
3413 if (error) {
3414 free(id);
3415 goto done;
3417 tagger = got_object_commit_get_committer(commit);
3418 tagger_time =
3419 got_object_commit_get_committer_time(commit);
3420 error = got_object_id_str(&id_str, id);
3421 free(id);
3422 } else {
3423 free(id);
3424 tagger = got_object_tag_get_tagger(tag);
3425 tagger_time = got_object_tag_get_tagger_time(tag);
3426 error = got_object_id_str(&id_str,
3427 got_object_tag_get_object_id(tag));
3429 if (error)
3430 goto done;
3432 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
3433 strlen(id_str)) != 0)
3434 continue;
3436 if (commit_found == 0 && tag_type == TAGBRIEF &&
3437 gw_trans->commit_id != NULL &&
3438 strncmp(id_str, gw_trans->commit_id, strlen(id_str)) != 0)
3439 continue;
3440 else
3441 commit_found = 1;
3443 if (gw_trans->commit_id != NULL &&
3444 strcmp(id_str, gw_trans->commit_id) != 0 &&
3445 (re == TAILQ_FIRST(&refs) ||
3446 c_cnt == gw_trans->gw_conf->got_max_commits_display)) {
3447 gw_trans->prev_id = strdup(id_str);
3448 if (gw_trans->prev_id == NULL) {
3449 error = got_error_from_errno("strdup");
3450 goto done;
3452 break;
3454 c_cnt++;
3456 done:
3457 if (tag)
3458 got_object_tag_close(tag);
3459 free(id_str);
3460 free(age);
3461 free(tag_commit0);
3462 free(href_tag);
3463 free(href_briefs);
3464 free(href_commits);
3465 got_ref_list_free(&refs);
3466 if (error == NULL && kerr != KCGI_OK)
3467 error = gw_kcgi_error(kerr);
3468 return error;
3471 static void
3472 gw_free_header(struct gw_header *header)
3474 free(header->path);
3475 free(header->author);
3476 free(header->committer);
3477 free(header->refs_str);
3478 free(header->commit_id);
3479 free(header->parent_id);
3480 free(header->tree_id);
3481 free(header->commit_msg);
3484 static struct gw_header *
3485 gw_init_header()
3487 struct gw_header *header;
3489 header = malloc(sizeof(*header));
3490 if (header == NULL)
3491 return NULL;
3493 header->path = NULL;
3494 TAILQ_INIT(&header->refs);
3496 header->refs_str = NULL;
3497 header->commit_id = NULL;
3498 header->committer = NULL;
3499 header->author = NULL;
3500 header->parent_id = NULL;
3501 header->tree_id = NULL;
3502 header->commit_msg = NULL;
3504 return header;
3507 static const struct got_error *
3508 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
3509 int limit, struct got_object_id *id)
3511 const struct got_error *error = NULL;
3512 struct got_commit_graph *graph = NULL;
3513 struct got_commit_object *commit = NULL;
3514 int chk_next = 0, chk_multi = 0, c_cnt = 0, commit_found = 0;
3515 struct gw_header *t_header = NULL;
3517 error = got_commit_graph_open(&graph, header->path, 0);
3518 if (error)
3519 return error;
3521 error = got_commit_graph_iter_start(graph, id, gw_trans->repo, NULL,
3522 NULL);
3523 if (error)
3524 goto done;
3526 for (;;) {
3527 error = got_commit_graph_iter_next(&id, graph, gw_trans->repo,
3528 NULL, NULL);
3529 if (error) {
3530 if (error->code == GOT_ERR_ITER_COMPLETED)
3531 error = NULL;
3532 goto err;
3534 if (id == NULL)
3535 goto err;
3537 error = got_object_open_as_commit(&commit, gw_trans->repo, id);
3538 if (error)
3539 goto err;
3540 if (limit == 1 && chk_multi == 0 &&
3541 gw_trans->gw_conf->got_max_commits_display != 1) {
3542 error = gw_get_commit(gw_trans, header, commit, id);
3543 if (error)
3544 goto err;
3545 commit_found = 1;
3546 } else {
3547 chk_multi = 1;
3548 struct gw_header *n_header = NULL;
3549 if ((n_header = gw_init_header()) == NULL) {
3550 error = got_error_from_errno("malloc");
3551 goto err;
3553 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3554 entry);
3555 error = got_ref_list(&n_header->refs, gw_trans->repo,
3556 NULL, got_ref_cmp_by_name, NULL);
3557 if (error)
3558 goto err;
3560 error = gw_get_commit(gw_trans, n_header, commit, id);
3561 if (error)
3562 goto err;
3563 got_ref_list_free(&n_header->refs);
3565 if (gw_trans->commit_id != NULL) {
3566 if (strcmp(gw_trans->commit_id,
3567 n_header->commit_id) == 0)
3568 commit_found = 1;
3569 } else
3570 commit_found = 1;
3573 * check for one more commit before breaking,
3574 * so we know whether to navicate through gw_briefs
3575 * gw_commits and gw_summary
3577 if (chk_next && (gw_trans->action == GW_BRIEFS ||
3578 gw_trans->action == GW_COMMITS ||
3579 gw_trans->action == GW_SUMMARY)) {
3580 gw_trans->next_id = strdup(n_header->commit_id);
3581 if (gw_trans->next_id == NULL)
3582 error = got_error_from_errno("strdup");
3583 TAILQ_REMOVE(&gw_trans->gw_headers, n_header,
3584 entry);
3585 goto done;
3589 if (commit_found == 1 && (error || (limit && --limit == 0))) {
3590 if (chk_multi == 0)
3591 break;
3592 chk_next = 1;
3595 done:
3596 if (gw_trans->prev_id == NULL && gw_trans->commit_id != NULL &&
3597 (gw_trans->action == GW_BRIEFS || gw_trans->action == GW_COMMITS ||
3598 gw_trans->action == GW_SUMMARY)) {
3599 commit_found = 0;
3600 TAILQ_FOREACH_REVERSE(t_header, &gw_trans->gw_headers,
3601 headers, entry) {
3602 if (commit_found == 0 &&
3603 strcmp(gw_trans->commit_id,
3604 t_header->commit_id) != 0)
3605 continue;
3606 else
3607 commit_found = 1;
3608 if (gw_trans->commit_id != NULL &&
3609 strcmp(gw_trans->commit_id,
3610 t_header->commit_id) != 0 &&
3611 (c_cnt == gw_trans->gw_conf->got_max_commits_display
3612 || t_header ==
3613 TAILQ_FIRST(&gw_trans->gw_headers))) {
3614 gw_trans->prev_id = strdup(t_header->commit_id);
3615 if (gw_trans->prev_id == NULL)
3616 error = got_error_from_errno("strdup");
3617 break;
3619 c_cnt++;
3623 err:
3624 if (commit != NULL)
3625 got_object_commit_close(commit);
3626 if (graph)
3627 got_commit_graph_close(graph);
3628 return error;
3631 static const struct got_error *
3632 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header,
3633 struct got_commit_object *commit, struct got_object_id *id)
3635 const struct got_error *error = NULL;
3636 struct got_reflist_entry *re;
3637 struct got_object_id *id2 = NULL;
3638 struct got_object_qid *parent_id;
3639 char *commit_msg = NULL, *commit_msg0;
3641 /*print commit*/
3642 TAILQ_FOREACH(re, &header->refs, entry) {
3643 char *s;
3644 const char *name;
3645 struct got_tag_object *tag = NULL;
3646 struct got_object_id *ref_id;
3647 int cmp;
3649 if (got_ref_is_symbolic(re->ref))
3650 continue;
3652 name = got_ref_get_name(re->ref);
3653 if (strncmp(name, "refs/", 5) == 0)
3654 name += 5;
3655 if (strncmp(name, "got/", 4) == 0)
3656 continue;
3657 if (strncmp(name, "heads/", 6) == 0)
3658 name += 6;
3659 if (strncmp(name, "remotes/", 8) == 0) {
3660 name += 8;
3661 s = strstr(name, "/" GOT_REF_HEAD);
3662 if (s != NULL && s[strlen(s)] == '\0')
3663 continue;
3665 error = got_ref_resolve(&ref_id, gw_trans->repo, re->ref);
3666 if (error)
3667 return error;
3668 if (strncmp(name, "tags/", 5) == 0) {
3669 error = got_object_open_as_tag(&tag, gw_trans->repo,
3670 ref_id);
3671 if (error) {
3672 if (error->code != GOT_ERR_OBJ_TYPE) {
3673 free(ref_id);
3674 continue;
3677 * Ref points at something other
3678 * than a tag.
3680 error = NULL;
3681 tag = NULL;
3684 cmp = got_object_id_cmp(tag ?
3685 got_object_tag_get_object_id(tag) : ref_id, id);
3686 free(ref_id);
3687 if (tag)
3688 got_object_tag_close(tag);
3689 if (cmp != 0)
3690 continue;
3691 s = header->refs_str;
3692 if (asprintf(&header->refs_str, "%s%s%s", s ? s : "",
3693 s ? ", " : "", name) == -1) {
3694 error = got_error_from_errno("asprintf");
3695 free(s);
3696 header->refs_str = NULL;
3697 return error;
3699 free(s);
3702 error = got_object_id_str(&header->commit_id, id);
3703 if (error)
3704 return error;
3706 error = got_object_id_str(&header->tree_id,
3707 got_object_commit_get_tree_id(commit));
3708 if (error)
3709 return error;
3711 if (gw_trans->action == GW_DIFF) {
3712 parent_id = STAILQ_FIRST(
3713 got_object_commit_get_parent_ids(commit));
3714 if (parent_id != NULL) {
3715 id2 = got_object_id_dup(parent_id->id);
3716 free (parent_id);
3717 error = got_object_id_str(&header->parent_id, id2);
3718 if (error)
3719 return error;
3720 free(id2);
3721 } else {
3722 header->parent_id = strdup("/dev/null");
3723 if (header->parent_id == NULL) {
3724 error = got_error_from_errno("strdup");
3725 return error;
3730 header->committer_time =
3731 got_object_commit_get_committer_time(commit);
3733 header->author =
3734 strdup(got_object_commit_get_author(commit));
3735 if (header->author == NULL) {
3736 error = got_error_from_errno("strdup");
3737 return error;
3739 header->committer =
3740 strdup(got_object_commit_get_committer(commit));
3741 if (header->committer == NULL) {
3742 error = got_error_from_errno("strdup");
3743 return error;
3745 error = got_object_commit_get_logmsg(&commit_msg0, commit);
3746 if (error)
3747 return error;
3749 commit_msg = commit_msg0;
3750 while (*commit_msg == '\n')
3751 commit_msg++;
3753 header->commit_msg = strdup(commit_msg);
3754 if (header->commit_msg == NULL)
3755 error = got_error_from_errno("strdup");
3756 free(commit_msg0);
3757 return error;
3760 static const struct got_error *
3761 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3763 const struct got_error *error = NULL;
3764 char *in_repo_path = NULL;
3765 struct got_object_id *id = NULL;
3766 struct got_reference *ref;
3768 error = got_repo_open(&gw_trans->repo, gw_trans->repo_path, NULL);
3769 if (error)
3770 return error;
3772 if (gw_trans->commit_id == NULL || gw_trans->action == GW_COMMITS ||
3773 gw_trans->action == GW_BRIEFS || gw_trans->action == GW_SUMMARY ||
3774 gw_trans->action == GW_TAGS) {
3775 error = got_ref_open(&ref, gw_trans->repo,
3776 gw_trans->headref, 0);
3777 if (error)
3778 return error;
3780 error = got_ref_resolve(&id, gw_trans->repo, ref);
3781 got_ref_close(ref);
3782 if (error)
3783 return error;
3784 } else {
3785 error = got_ref_open(&ref, gw_trans->repo,
3786 gw_trans->commit_id, 0);
3787 if (error == NULL) {
3788 int obj_type;
3789 error = got_ref_resolve(&id, gw_trans->repo, ref);
3790 got_ref_close(ref);
3791 if (error)
3792 return error;
3793 error = got_object_get_type(&obj_type, gw_trans->repo,
3794 id);
3795 if (error)
3796 goto done;
3797 if (obj_type == GOT_OBJ_TYPE_TAG) {
3798 struct got_tag_object *tag;
3799 error = got_object_open_as_tag(&tag,
3800 gw_trans->repo, id);
3801 if (error)
3802 goto done;
3803 if (got_object_tag_get_object_type(tag) !=
3804 GOT_OBJ_TYPE_COMMIT) {
3805 got_object_tag_close(tag);
3806 error = got_error(GOT_ERR_OBJ_TYPE);
3807 goto done;
3809 free(id);
3810 id = got_object_id_dup(
3811 got_object_tag_get_object_id(tag));
3812 if (id == NULL)
3813 error = got_error_from_errno(
3814 "got_object_id_dup");
3815 got_object_tag_close(tag);
3816 if (error)
3817 goto done;
3818 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3819 error = got_error(GOT_ERR_OBJ_TYPE);
3820 goto done;
3823 error = got_repo_match_object_id_prefix(&id,
3824 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT,
3825 gw_trans->repo);
3826 if (error)
3827 goto done;
3830 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
3831 gw_trans->repo_path);
3832 if (error)
3833 goto done;
3835 if (in_repo_path) {
3836 header->path = strdup(in_repo_path);
3837 if (header->path == NULL) {
3838 error = got_error_from_errno("strdup");
3839 goto done;
3843 error = got_ref_list(&header->refs, gw_trans->repo, NULL,
3844 got_ref_cmp_by_name, NULL);
3845 if (error)
3846 goto done;
3848 error = gw_get_commits(gw_trans, header, limit, id);
3849 done:
3850 got_ref_list_free(&header->refs);
3851 free(id);
3852 free(in_repo_path);
3853 return error;
3856 struct blame_line {
3857 int annotated;
3858 char *id_str;
3859 char *committer;
3860 char datebuf[11]; /* YYYY-MM-DD + NUL */
3863 struct gw_blame_cb_args {
3864 struct blame_line *lines;
3865 int nlines;
3866 int nlines_prec;
3867 int lineno_cur;
3868 off_t *line_offsets;
3869 FILE *f;
3870 struct got_repository *repo;
3871 struct gw_trans *gw_trans;
3874 static const struct got_error *
3875 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3877 const struct got_error *err = NULL;
3878 struct gw_blame_cb_args *a = arg;
3879 struct blame_line *bline;
3880 char *line = NULL;
3881 size_t linesize = 0;
3882 struct got_commit_object *commit = NULL;
3883 off_t offset;
3884 struct tm tm;
3885 time_t committer_time;
3886 enum kcgi_err kerr = KCGI_OK;
3888 if (nlines != a->nlines ||
3889 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3890 return got_error(GOT_ERR_RANGE);
3892 if (lineno == -1)
3893 return NULL; /* no change in this commit */
3895 /* Annotate this line. */
3896 bline = &a->lines[lineno - 1];
3897 if (bline->annotated)
3898 return NULL;
3899 err = got_object_id_str(&bline->id_str, id);
3900 if (err)
3901 return err;
3903 err = got_object_open_as_commit(&commit, a->repo, id);
3904 if (err)
3905 goto done;
3907 bline->committer = strdup(got_object_commit_get_committer(commit));
3908 if (bline->committer == NULL) {
3909 err = got_error_from_errno("strdup");
3910 goto done;
3913 committer_time = got_object_commit_get_committer_time(commit);
3914 if (localtime_r(&committer_time, &tm) == NULL)
3915 return got_error_from_errno("localtime_r");
3916 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3917 &tm) == 0) {
3918 err = got_error(GOT_ERR_NO_SPACE);
3919 goto done;
3921 bline->annotated = 1;
3923 /* Print lines annotated so far. */
3924 bline = &a->lines[a->lineno_cur - 1];
3925 if (!bline->annotated)
3926 goto done;
3928 offset = a->line_offsets[a->lineno_cur - 1];
3929 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3930 err = got_error_from_errno("fseeko");
3931 goto done;
3934 while (bline->annotated) {
3935 char *smallerthan, *at, *nl, *committer;
3936 char *href_diff = NULL;
3937 size_t len;
3939 if (getline(&line, &linesize, a->f) == -1) {
3940 if (ferror(a->f))
3941 err = got_error_from_errno("getline");
3942 break;
3945 committer = bline->committer;
3946 smallerthan = strchr(committer, '<');
3947 if (smallerthan && smallerthan[1] != '\0')
3948 committer = smallerthan + 1;
3949 at = strchr(committer, '@');
3950 if (at)
3951 *at = '\0';
3952 len = strlen(committer);
3953 if (len >= 9)
3954 committer[8] = '\0';
3956 nl = strchr(line, '\n');
3957 if (nl)
3958 *nl = '\0';
3960 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3961 "blame_wrapper", KATTR__MAX);
3962 if (kerr != KCGI_OK)
3963 goto err;
3964 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3965 "blame_number", KATTR__MAX);
3966 if (kerr != KCGI_OK)
3967 goto err;
3968 kerr = khtml_printf(a->gw_trans->gw_html_req, "%.*d",
3969 a->nlines_prec, a->lineno_cur);
3970 if (kerr != KCGI_OK)
3971 goto err;
3972 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3973 if (kerr != KCGI_OK)
3974 goto err;
3976 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3977 "blame_hash", KATTR__MAX);
3978 if (kerr != KCGI_OK)
3979 goto err;
3981 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
3982 a->gw_trans->repo_name, "action", "diff", "commit",
3983 bline->id_str, NULL);
3984 if (href_diff == NULL) {
3985 err = got_error_from_errno("khttp_urlpart");
3986 goto done;
3988 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3989 KATTR_HREF, href_diff, KATTR__MAX);
3990 if (kerr != KCGI_OK)
3991 goto done;
3992 kerr = khtml_printf(a->gw_trans->gw_html_req, "%.8s",
3993 bline->id_str);
3994 if (kerr != KCGI_OK)
3995 goto err;
3996 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3997 if (kerr != KCGI_OK)
3998 goto err;
4000 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4001 "blame_date", KATTR__MAX);
4002 if (kerr != KCGI_OK)
4003 goto err;
4004 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
4005 if (kerr != KCGI_OK)
4006 goto err;
4007 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
4008 if (kerr != KCGI_OK)
4009 goto err;
4011 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4012 "blame_author", KATTR__MAX);
4013 if (kerr != KCGI_OK)
4014 goto err;
4015 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
4016 if (kerr != KCGI_OK)
4017 goto err;
4018 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
4019 if (kerr != KCGI_OK)
4020 goto err;
4022 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4023 "blame_code", KATTR__MAX);
4024 if (kerr != KCGI_OK)
4025 goto err;
4026 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
4027 if (kerr != KCGI_OK)
4028 goto err;
4029 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
4030 if (kerr != KCGI_OK)
4031 goto err;
4033 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
4034 if (kerr != KCGI_OK)
4035 goto err;
4037 a->lineno_cur++;
4038 bline = &a->lines[a->lineno_cur - 1];
4039 err:
4040 free(href_diff);
4042 done:
4043 if (commit)
4044 got_object_commit_close(commit);
4045 free(line);
4046 if (err == NULL && kerr != KCGI_OK)
4047 err = gw_kcgi_error(kerr);
4048 return err;
4051 static const struct got_error *
4052 gw_output_file_blame(struct gw_trans *gw_trans, struct gw_header *header)
4054 const struct got_error *error = NULL;
4055 struct got_object_id *obj_id = NULL;
4056 struct got_object_id *commit_id = NULL;
4057 struct got_blob_object *blob = NULL;
4058 char *path = NULL, *in_repo_path = NULL;
4059 struct gw_blame_cb_args bca;
4060 int i, obj_type;
4061 off_t filesize;
4063 if (asprintf(&path, "%s%s%s",
4064 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4065 gw_trans->repo_folder ? "/" : "",
4066 gw_trans->repo_file) == -1) {
4067 error = got_error_from_errno("asprintf");
4068 goto done;
4071 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path);
4072 if (error)
4073 goto done;
4075 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
4076 GOT_OBJ_TYPE_COMMIT, &header->refs, gw_trans->repo);
4077 if (error)
4078 goto done;
4080 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
4081 in_repo_path);
4082 if (error)
4083 goto done;
4085 if (obj_id == NULL) {
4086 error = got_error(GOT_ERR_NO_OBJ);
4087 goto done;
4090 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
4091 if (error)
4092 goto done;
4094 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4095 error = got_error(GOT_ERR_OBJ_TYPE);
4096 goto done;
4099 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
4100 if (error)
4101 goto done;
4103 bca.f = got_opentemp();
4104 if (bca.f == NULL) {
4105 error = got_error_from_errno("got_opentemp");
4106 goto done;
4108 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4109 &bca.line_offsets, bca.f, blob);
4110 if (error || bca.nlines == 0)
4111 goto done;
4113 /* Don't include \n at EOF in the blame line count. */
4114 if (bca.line_offsets[bca.nlines - 1] == filesize)
4115 bca.nlines--;
4117 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4118 if (bca.lines == NULL) {
4119 error = got_error_from_errno("calloc");
4120 goto done;
4122 bca.lineno_cur = 1;
4123 bca.nlines_prec = 0;
4124 i = bca.nlines;
4125 while (i > 0) {
4126 i /= 10;
4127 bca.nlines_prec++;
4129 bca.repo = gw_trans->repo;
4130 bca.gw_trans = gw_trans;
4132 error = got_blame(in_repo_path, commit_id, gw_trans->repo, gw_blame_cb,
4133 &bca, NULL, NULL);
4134 done:
4135 free(in_repo_path);
4136 free(commit_id);
4137 free(obj_id);
4138 free(path);
4140 if (blob) {
4141 free(bca.line_offsets);
4142 for (i = 0; i < bca.nlines; i++) {
4143 struct blame_line *bline = &bca.lines[i];
4144 free(bline->id_str);
4145 free(bline->committer);
4147 free(bca.lines);
4148 if (bca.f && fclose(bca.f) == EOF && error == NULL)
4149 error = got_error_from_errno("fclose");
4151 if (blob)
4152 got_object_blob_close(blob);
4153 return error;
4156 static const struct got_error *
4157 gw_output_blob_buf(struct gw_trans *gw_trans, struct gw_header *header)
4159 const struct got_error *error = NULL;
4160 struct got_object_id *obj_id = NULL;
4161 struct got_object_id *commit_id = NULL;
4162 struct got_blob_object *blob = NULL;
4163 char *path = NULL, *in_repo_path = NULL;
4164 int obj_type, set_mime = 0;
4165 size_t len, hdrlen;
4166 const uint8_t *buf;
4167 enum kcgi_err kerr = KCGI_OK;
4169 if (asprintf(&path, "%s%s%s",
4170 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4171 gw_trans->repo_folder ? "/" : "",
4172 gw_trans->repo_file) == -1) {
4173 error = got_error_from_errno("asprintf");
4174 goto done;
4177 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path);
4178 if (error)
4179 goto done;
4181 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
4182 GOT_OBJ_TYPE_COMMIT, &header->refs, gw_trans->repo);
4183 if (error)
4184 goto done;
4186 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
4187 in_repo_path);
4188 if (error)
4189 goto done;
4191 if (obj_id == NULL) {
4192 error = got_error(GOT_ERR_NO_OBJ);
4193 goto done;
4196 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
4197 if (error)
4198 goto done;
4200 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4201 error = got_error(GOT_ERR_OBJ_TYPE);
4202 goto done;
4205 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
4206 if (error)
4207 goto done;
4209 hdrlen = got_object_blob_get_hdrlen(blob);
4210 do {
4211 error = got_object_blob_read_block(&len, blob);
4212 if (error)
4213 goto done;
4214 buf = got_object_blob_get_read_buf(blob);
4217 * Skip blob object header first time around,
4218 * which also contains a zero byte.
4220 buf += hdrlen;
4221 if (set_mime == 0) {
4222 if (isbinary(buf, len - hdrlen))
4223 gw_trans->mime = KMIME_APP_OCTET_STREAM;
4224 else
4225 gw_trans->mime = KMIME_TEXT_PLAIN;
4226 set_mime = 1;
4227 error = gw_display_index(gw_trans);
4228 if (error)
4229 goto done;
4231 kerr = khttp_write(gw_trans->gw_req, buf, len - hdrlen);
4232 if (kerr != KCGI_OK)
4233 goto done;
4234 hdrlen = 0;
4235 } while (len != 0);
4236 done:
4237 free(in_repo_path);
4238 free(commit_id);
4239 free(obj_id);
4240 free(path);
4241 if (blob)
4242 got_object_blob_close(blob);
4243 if (error == NULL && kerr != KCGI_OK)
4244 error = gw_kcgi_error(kerr);
4245 return error;
4248 static const struct got_error *
4249 gw_output_repo_tree(struct gw_trans *gw_trans, struct gw_header *header)
4251 const struct got_error *error = NULL;
4252 struct got_object_id *tree_id = NULL, *commit_id = NULL;
4253 struct got_tree_object *tree = NULL;
4254 char *path = NULL, *in_repo_path = NULL;
4255 char *id_str = NULL;
4256 char *build_folder = NULL;
4257 char *href_blob = NULL, *href_blame = NULL;
4258 const char *class = NULL;
4259 int nentries, i, class_flip = 0;
4260 enum kcgi_err kerr = KCGI_OK;
4262 if (gw_trans->repo_folder != NULL) {
4263 path = strdup(gw_trans->repo_folder);
4264 if (path == NULL) {
4265 error = got_error_from_errno("strdup");
4266 goto done;
4268 } else {
4269 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
4270 gw_trans->repo_path);
4271 if (error)
4272 goto done;
4273 free(path);
4274 path = in_repo_path;
4277 if (gw_trans->commit_id == NULL) {
4278 struct got_reference *head_ref;
4279 error = got_ref_open(&head_ref, gw_trans->repo,
4280 gw_trans->headref, 0);
4281 if (error)
4282 goto done;
4283 error = got_ref_resolve(&commit_id, gw_trans->repo, head_ref);
4284 if (error)
4285 goto done;
4286 got_ref_close(head_ref);
4288 * gw_trans->commit_id was not parsed from the querystring
4289 * we hit this code path from gw_index, where we don't know the
4290 * commit values for the tree link yet, so set
4291 * gw_trans->commit_id here to continue further into the tree
4293 error = got_object_id_str(&gw_trans->commit_id, commit_id);
4294 if (error)
4295 goto done;
4297 } else {
4298 error = got_repo_match_object_id(&commit_id, NULL,
4299 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT, &header->refs,
4300 gw_trans->repo);
4301 if (error)
4302 goto done;
4305 error = got_object_id_by_path(&tree_id, gw_trans->repo, commit_id,
4306 path);
4307 if (error)
4308 goto done;
4310 error = got_object_open_as_tree(&tree, gw_trans->repo, tree_id);
4311 if (error)
4312 goto done;
4314 nentries = got_object_tree_get_nentries(tree);
4315 for (i = 0; i < nentries; i++) {
4316 struct got_tree_entry *te;
4317 const char *modestr = "";
4318 mode_t mode;
4320 te = got_object_tree_get_entry(tree, i);
4322 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
4323 if (error)
4324 goto done;
4326 mode = got_tree_entry_get_mode(te);
4327 if (got_object_tree_entry_is_submodule(te))
4328 modestr = "$";
4329 else if (S_ISLNK(mode))
4330 modestr = "@";
4331 else if (S_ISDIR(mode))
4332 modestr = "/";
4333 else if (mode & S_IXUSR)
4334 modestr = "*";
4336 if (class_flip == 0) {
4337 class = "back_lightgray";
4338 class_flip = 1;
4339 } else {
4340 class = "back_white";
4341 class_flip = 0;
4344 if (S_ISDIR(mode)) {
4345 if (asprintf(&build_folder, "%s/%s",
4346 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4347 got_tree_entry_get_name(te)) == -1) {
4348 error = got_error_from_errno("asprintf");
4349 goto done;
4352 href_blob = khttp_urlpart(NULL, NULL, "gotweb", "path",
4353 gw_trans->repo_name, "action",
4354 gw_get_action_name(gw_trans), "commit",
4355 gw_trans->commit_id, "folder", build_folder, NULL);
4356 if (href_blob == NULL) {
4357 error = got_error_from_errno("khttp_urlpart");
4358 goto done;
4360 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4361 KATTR_ID, "tree_wrapper", KATTR__MAX);
4362 if (kerr != KCGI_OK)
4363 goto done;
4364 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4365 KATTR_ID, "tree_line", KATTR_CLASS, class,
4366 KATTR__MAX);
4367 if (kerr != KCGI_OK)
4368 goto done;
4369 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4370 KATTR_HREF, href_blob, KATTR_CLASS,
4371 "diff_directory", KATTR__MAX);
4372 if (kerr != KCGI_OK)
4373 goto done;
4374 kerr = khtml_printf(gw_trans->gw_html_req, "%s%s",
4375 got_tree_entry_get_name(te), modestr);
4376 if (kerr != KCGI_OK)
4377 goto done;
4378 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4379 if (kerr != KCGI_OK)
4380 goto done;
4381 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4382 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
4383 KATTR__MAX);
4384 if (kerr != KCGI_OK)
4385 goto done;
4386 kerr = khtml_entity(gw_trans->gw_html_req,
4387 KENTITY_nbsp);
4388 if (kerr != KCGI_OK)
4389 goto done;
4390 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4391 if (kerr != KCGI_OK)
4392 goto done;
4393 } else {
4394 href_blob = khttp_urlpart(NULL, NULL, "gotweb", "path",
4395 gw_trans->repo_name, "action", "blob", "commit",
4396 gw_trans->commit_id, "file",
4397 got_tree_entry_get_name(te), "folder",
4398 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4399 NULL);
4400 if (href_blob == NULL) {
4401 error = got_error_from_errno("khttp_urlpart");
4402 goto done;
4404 href_blame = khttp_urlpart(NULL, NULL, "gotweb", "path",
4405 gw_trans->repo_name, "action", "blame", "commit",
4406 gw_trans->commit_id, "file",
4407 got_tree_entry_get_name(te), "folder",
4408 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4409 NULL);
4410 if (href_blame == NULL) {
4411 error = got_error_from_errno("khttp_urlpart");
4412 goto done;
4414 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4415 KATTR_ID, "tree_wrapper", KATTR__MAX);
4416 if (kerr != KCGI_OK)
4417 goto done;
4418 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4419 KATTR_ID, "tree_line", KATTR_CLASS, class,
4420 KATTR__MAX);
4421 if (kerr != KCGI_OK)
4422 goto done;
4423 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4424 KATTR_HREF, href_blob, KATTR__MAX);
4425 if (kerr != KCGI_OK)
4426 goto done;
4427 kerr = khtml_printf(gw_trans->gw_html_req, "%s%s",
4428 got_tree_entry_get_name(te), modestr);
4429 if (kerr != KCGI_OK)
4430 goto done;
4431 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4432 if (kerr != KCGI_OK)
4433 goto done;
4434 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4435 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
4436 KATTR__MAX);
4437 if (kerr != KCGI_OK)
4438 goto done;
4440 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4441 KATTR_HREF, href_blob, KATTR__MAX);
4442 if (kerr != KCGI_OK)
4443 goto done;
4444 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
4445 if (kerr != KCGI_OK)
4446 goto done;
4447 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4448 if (kerr != KCGI_OK)
4449 goto done;
4451 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4452 if (kerr != KCGI_OK)
4453 goto done;
4455 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4456 KATTR_HREF, href_blame, KATTR__MAX);
4457 if (kerr != KCGI_OK)
4458 goto done;
4459 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
4460 if (kerr != KCGI_OK)
4461 goto done;
4463 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4464 if (kerr != KCGI_OK)
4465 goto done;
4467 free(id_str);
4468 id_str = NULL;
4469 free(href_blob);
4470 href_blob = NULL;
4471 free(build_folder);
4472 build_folder = NULL;
4474 done:
4475 if (tree)
4476 got_object_tree_close(tree);
4477 free(id_str);
4478 free(href_blob);
4479 free(href_blame);
4480 free(in_repo_path);
4481 free(tree_id);
4482 free(build_folder);
4483 if (error == NULL && kerr != KCGI_OK)
4484 error = gw_kcgi_error(kerr);
4485 return error;
4488 static const struct got_error *
4489 gw_output_repo_heads(struct gw_trans *gw_trans)
4491 const struct got_error *error = NULL;
4492 struct got_reflist_head refs;
4493 struct got_reflist_entry *re;
4494 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
4495 char *href_commits = NULL;
4496 enum kcgi_err kerr = KCGI_OK;
4498 TAILQ_INIT(&refs);
4500 error = got_ref_list(&refs, gw_trans->repo, "refs/heads",
4501 got_ref_cmp_by_name, NULL);
4502 if (error)
4503 goto done;
4505 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4506 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
4507 if (kerr != KCGI_OK)
4508 goto done;
4509 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4510 KATTR_ID, "summary_heads_title", KATTR__MAX);
4511 if (kerr != KCGI_OK)
4512 goto done;
4513 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
4514 if (kerr != KCGI_OK)
4515 goto done;
4516 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4517 if (kerr != KCGI_OK)
4518 goto done;
4519 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4520 KATTR_ID, "summary_heads_content", KATTR__MAX);
4521 if (kerr != KCGI_OK)
4522 goto done;
4524 TAILQ_FOREACH(re, &refs, entry) {
4525 const char *refname;
4527 if (got_ref_is_symbolic(re->ref))
4528 continue;
4530 refname = got_ref_get_name(re->ref);
4531 if (strncmp(refname, "refs/heads/", 11) != 0)
4532 continue;
4534 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
4535 refname, TM_DIFF);
4536 if (error)
4537 goto done;
4539 if (strncmp(refname, "refs/heads/", 11) == 0)
4540 refname += 11;
4542 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4543 KATTR_ID, "heads_wrapper", KATTR__MAX);
4544 if (kerr != KCGI_OK)
4545 goto done;
4546 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4547 KATTR_ID, "heads_age", KATTR__MAX);
4548 if (kerr != KCGI_OK)
4549 goto done;
4550 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
4551 if (kerr != KCGI_OK)
4552 goto done;
4553 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4554 if (kerr != KCGI_OK)
4555 goto done;
4556 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4557 KATTR_ID, "heads_space", KATTR__MAX);
4558 if (kerr != KCGI_OK)
4559 goto done;
4560 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
4561 if (kerr != KCGI_OK)
4562 goto done;
4563 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4564 if (kerr != KCGI_OK)
4565 goto done;
4566 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4567 KATTR_ID, "head", KATTR__MAX);
4568 if (kerr != KCGI_OK)
4569 goto done;
4571 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
4572 gw_trans->repo_name, "action", "summary", "headref",
4573 refname, NULL);
4574 if (href_summary == NULL) {
4575 error = got_error_from_errno("khttp_urlpart");
4576 goto done;
4578 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4579 href_summary, KATTR__MAX);
4580 kerr = khtml_puts(gw_trans->gw_html_req, refname);
4581 if (kerr != KCGI_OK)
4582 goto done;
4583 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4584 if (kerr != KCGI_OK)
4585 goto done;
4587 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4588 "navs_wrapper", KATTR__MAX);
4589 if (kerr != KCGI_OK)
4590 goto done;
4591 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4592 "navs", KATTR__MAX);
4593 if (kerr != KCGI_OK)
4594 goto done;
4596 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4597 href_summary, KATTR__MAX);
4598 if (kerr != KCGI_OK)
4599 goto done;
4600 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4601 if (kerr != KCGI_OK)
4602 goto done;
4603 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4604 if (kerr != KCGI_OK)
4605 goto done;
4607 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4608 if (kerr != KCGI_OK)
4609 goto done;
4611 href_briefs = khttp_urlpart(NULL, NULL, "gotweb", "path",
4612 gw_trans->repo_name, "action", "briefs", "headref",
4613 refname, NULL);
4614 if (href_briefs == NULL) {
4615 error = got_error_from_errno("khttp_urlpart");
4616 goto done;
4618 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4619 href_briefs, KATTR__MAX);
4620 if (kerr != KCGI_OK)
4621 goto done;
4622 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4623 if (kerr != KCGI_OK)
4624 goto done;
4625 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4626 if (kerr != KCGI_OK)
4627 goto done;
4629 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4630 if (kerr != KCGI_OK)
4631 goto done;
4633 href_commits = khttp_urlpart(NULL, NULL, "gotweb", "path",
4634 gw_trans->repo_name, "action", "commits", "headref",
4635 refname, NULL);
4636 if (href_commits == NULL) {
4637 error = got_error_from_errno("khttp_urlpart");
4638 goto done;
4640 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4641 href_commits, KATTR__MAX);
4642 if (kerr != KCGI_OK)
4643 goto done;
4644 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4645 if (kerr != KCGI_OK)
4646 goto done;
4647 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4648 if (kerr != KCGI_OK)
4649 goto done;
4651 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4652 "dotted_line", KATTR__MAX);
4653 if (kerr != KCGI_OK)
4654 goto done;
4655 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4656 if (kerr != KCGI_OK)
4657 goto done;
4658 free(href_summary);
4659 href_summary = NULL;
4660 free(href_briefs);
4661 href_briefs = NULL;
4662 free(href_commits);
4663 href_commits = NULL;
4665 done:
4666 got_ref_list_free(&refs);
4667 free(href_summary);
4668 free(href_briefs);
4669 free(href_commits);
4670 return error;
4673 static const struct got_error *
4674 gw_output_site_link(struct gw_trans *gw_trans)
4676 const struct got_error *error = NULL;
4677 char *href_summary = NULL;
4678 enum kcgi_err kerr = KCGI_OK;
4680 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4681 "site_link", KATTR__MAX);
4682 if (kerr != KCGI_OK)
4683 goto done;
4684 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4685 KATTR__MAX);
4686 if (kerr != KCGI_OK)
4687 goto done;
4688 kerr = khtml_puts(gw_trans->gw_html_req,
4689 gw_trans->gw_conf->got_site_link);
4690 if (kerr != KCGI_OK)
4691 goto done;
4692 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4693 if (kerr != KCGI_OK)
4694 goto done;
4696 if (gw_trans->repo_name != NULL) {
4697 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4698 if (kerr != KCGI_OK)
4699 goto done;
4701 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
4702 gw_trans->repo_name, "action", "summary", NULL);
4703 if (href_summary == NULL) {
4704 error = got_error_from_errno("khttp_urlpart");
4705 goto done;
4707 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4708 href_summary, KATTR__MAX);
4709 if (kerr != KCGI_OK)
4710 goto done;
4711 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4712 if (kerr != KCGI_OK)
4713 goto done;
4714 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4715 if (kerr != KCGI_OK)
4716 goto done;
4717 kerr = khtml_printf(gw_trans->gw_html_req, " / %s",
4718 gw_get_action_name(gw_trans));
4719 if (kerr != KCGI_OK)
4720 goto done;
4723 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4724 if (kerr != KCGI_OK)
4725 goto done;
4726 done:
4727 free(href_summary);
4728 if (error == NULL && kerr != KCGI_OK)
4729 error = gw_kcgi_error(kerr);
4730 return error;
4733 static const struct got_error *
4734 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4736 const struct got_error *error = NULL;
4737 char *color = NULL;
4738 enum kcgi_err kerr = KCGI_OK;
4740 if (strncmp(buf, "-", 1) == 0)
4741 color = "diff_minus";
4742 else if (strncmp(buf, "+", 1) == 0)
4743 color = "diff_plus";
4744 else if (strncmp(buf, "@@", 2) == 0)
4745 color = "diff_chunk_header";
4746 else if (strncmp(buf, "@@", 2) == 0)
4747 color = "diff_chunk_header";
4748 else if (strncmp(buf, "commit +", 8) == 0)
4749 color = "diff_meta";
4750 else if (strncmp(buf, "commit -", 8) == 0)
4751 color = "diff_meta";
4752 else if (strncmp(buf, "blob +", 6) == 0)
4753 color = "diff_meta";
4754 else if (strncmp(buf, "blob -", 6) == 0)
4755 color = "diff_meta";
4756 else if (strncmp(buf, "file +", 6) == 0)
4757 color = "diff_meta";
4758 else if (strncmp(buf, "file -", 6) == 0)
4759 color = "diff_meta";
4760 else if (strncmp(buf, "from:", 5) == 0)
4761 color = "diff_author";
4762 else if (strncmp(buf, "via:", 4) == 0)
4763 color = "diff_author";
4764 else if (strncmp(buf, "date:", 5) == 0)
4765 color = "diff_date";
4766 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4767 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4768 if (error == NULL && kerr != KCGI_OK)
4769 error = gw_kcgi_error(kerr);
4770 return error;
4773 int
4774 main(int argc, char *argv[])
4776 const struct got_error *error = NULL, *error2 = NULL;
4777 struct gw_trans *gw_trans;
4778 struct gw_dir *dir = NULL, *tdir;
4779 const char *page = "index";
4780 enum kcgi_err kerr = KCGI_OK;
4782 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4783 errx(1, "malloc");
4785 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4786 errx(1, "malloc");
4788 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4789 errx(1, "malloc");
4791 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4792 errx(1, "malloc");
4794 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4795 if (kerr != KCGI_OK) {
4796 error = gw_kcgi_error(kerr);
4797 goto done;
4800 TAILQ_INIT(&gw_trans->gw_dirs);
4801 TAILQ_INIT(&gw_trans->gw_headers);
4803 gw_trans->action = -1;
4804 gw_trans->page = 0;
4805 gw_trans->repos_total = 0;
4806 gw_trans->repo_path = NULL;
4807 gw_trans->commit_id = NULL;
4808 gw_trans->next_id = NULL;
4809 gw_trans->prev_id = NULL;
4810 gw_trans->headref = GOT_REF_HEAD;
4811 gw_trans->mime = KMIME_TEXT_HTML;
4812 gw_trans->gw_tmpl->key = gw_templs;
4813 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4814 gw_trans->gw_tmpl->arg = gw_trans;
4815 gw_trans->gw_tmpl->cb = gw_template;
4817 error = parse_gotweb_config(&gw_trans->gw_conf, GOTWEB_CONF);
4818 if (error)
4819 goto done;
4821 error = gw_parse_querystring(gw_trans);
4822 if (error)
4823 goto done;
4825 if (gw_trans->action == GW_BLOB)
4826 error = gw_blob(gw_trans);
4827 else
4828 error = gw_display_index(gw_trans);
4829 done:
4830 if (gw_trans->repo) {
4831 const struct got_error *close_err;
4832 close_err = got_repo_close(gw_trans->repo);
4833 if (error == NULL)
4834 error = close_err;
4836 if (error) {
4837 gw_trans->error = error;
4838 gw_trans->action = GW_ERR;
4839 error2 = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
4840 if (error2)
4841 goto cleanup; /* we can't display an error page */
4842 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
4843 if (kerr != KCGI_OK)
4844 goto cleanup; /* we can't display an error page */
4845 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
4846 gw_query_funcs[gw_trans->action].template);
4847 if (kerr != KCGI_OK) {
4848 khtml_close(gw_trans->gw_html_req);
4849 goto cleanup; /* we can't display an error page */
4853 cleanup:
4854 free(gw_trans->gw_conf->got_repos_path);
4855 free(gw_trans->gw_conf->got_www_path);
4856 free(gw_trans->gw_conf->got_site_name);
4857 free(gw_trans->gw_conf->got_site_owner);
4858 free(gw_trans->gw_conf->got_site_link);
4859 free(gw_trans->gw_conf->got_logo);
4860 free(gw_trans->gw_conf->got_logo_url);
4861 free(gw_trans->gw_conf);
4862 free(gw_trans->commit_id);
4863 free(gw_trans->next_id);
4864 free(gw_trans->prev_id);
4865 free(gw_trans->repo_path);
4866 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4867 free(dir->name);
4868 free(dir->description);
4869 free(dir->age);
4870 free(dir->url);
4871 free(dir->path);
4872 free(dir);
4875 khttp_free(gw_trans->gw_req);
4876 return 0;