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/stat.h>
19 #include <sys/types.h>
21 #include <ctype.h>
22 #include <dirent.h>
23 #include <err.h>
24 #include <errno.h>
25 #include <regex.h>
26 #include <sha1.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_commit_object *,
218 struct got_object_id *);
219 static const struct got_error *gw_load_got_paths(struct gw_trans *);
220 static const struct got_error *gw_load_got_path(struct gw_trans *,
221 struct gw_dir *);
222 static const struct got_error *gw_parse_querystring(struct gw_trans *);
223 static const struct got_error *gw_blame(struct gw_trans *);
224 static const struct got_error *gw_blob(struct gw_trans *);
225 static const struct got_error *gw_diff(struct gw_trans *);
226 static const struct got_error *gw_index(struct gw_trans *);
227 static const struct got_error *gw_commits(struct gw_trans *);
228 static const struct got_error *gw_briefs(struct gw_trans *);
229 static const struct got_error *gw_summary(struct gw_trans *);
230 static const struct got_error *gw_tree(struct gw_trans *);
231 static const struct got_error *gw_tag(struct gw_trans *);
232 static const struct got_error *gw_tags(struct gw_trans *);
234 struct gw_query_action {
235 unsigned int func_id;
236 const char *func_name;
237 const struct got_error *(*func_main)(struct gw_trans *);
238 char *template;
239 };
241 enum gw_query_actions {
242 GW_BLAME,
243 GW_BLOB,
244 GW_BRIEFS,
245 GW_COMMITS,
246 GW_DIFF,
247 GW_ERR,
248 GW_INDEX,
249 GW_SUMMARY,
250 GW_TAG,
251 GW_TAGS,
252 GW_TREE,
253 };
255 static const struct gw_query_action gw_query_funcs[] = {
256 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
257 { GW_BLOB, "blob", NULL, NULL },
258 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
259 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
260 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
261 { GW_ERR, "error", gw_error, "gw_tmpl/err.tmpl" },
262 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
263 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
264 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
265 { GW_TAGS, "tags", gw_tags, "gw_tmpl/tags.tmpl" },
266 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
267 };
269 static const char *
270 gw_get_action_name(struct gw_trans *gw_trans)
272 return gw_query_funcs[gw_trans->action].func_name;
275 static const struct got_error *
276 gw_kcgi_error(enum kcgi_err kerr)
278 if (kerr == KCGI_OK)
279 return NULL;
281 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
282 return got_error(GOT_ERR_CANCELLED);
284 if (kerr == KCGI_ENOMEM)
285 return got_error_set_errno(ENOMEM,
286 kcgi_strerror(kerr));
288 if (kerr == KCGI_ENFILE)
289 return got_error_set_errno(ENFILE,
290 kcgi_strerror(kerr));
292 if (kerr == KCGI_EAGAIN)
293 return got_error_set_errno(EAGAIN,
294 kcgi_strerror(kerr));
296 if (kerr == KCGI_FORM)
297 return got_error_msg(GOT_ERR_IO,
298 kcgi_strerror(kerr));
300 return got_error_from_errno(kcgi_strerror(kerr));
303 static const struct got_error *
304 gw_apply_unveil(const char *repo_path)
306 const struct got_error *err;
308 #ifdef PROFILE
309 if (unveil("gmon.out", "rwc") != 0)
310 return got_error_from_errno2("unveil", "gmon.out");
311 #endif
312 if (repo_path && unveil(repo_path, "r") != 0)
313 return got_error_from_errno2("unveil", repo_path);
315 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
316 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
318 err = got_privsep_unveil_exec_helpers();
319 if (err != NULL)
320 return err;
322 if (unveil(NULL, NULL) != 0)
323 return got_error_from_errno("unveil");
325 return NULL;
328 static int
329 isbinary(const uint8_t *buf, size_t n)
331 size_t i;
333 for (i = 0; i < n; i++)
334 if (buf[i] == 0)
335 return 1;
336 return 0;
339 static const struct got_error *
340 gw_blame(struct gw_trans *gw_trans)
342 const struct got_error *error = NULL;
343 struct gw_header *header = NULL;
344 char *age = NULL;
345 enum kcgi_err kerr = KCGI_OK;
347 #ifndef PROFILE
348 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
349 NULL) == -1)
350 return got_error_from_errno("pledge");
351 #endif
352 if ((header = gw_init_header()) == NULL)
353 return got_error_from_errno("malloc");
355 error = gw_apply_unveil(gw_trans->gw_dir->path);
356 if (error)
357 goto done;
359 /* check querystring */
360 if (gw_trans->repo_file == NULL) {
361 error = got_error_msg(GOT_ERR_QUERYSTRING,
362 "file required in querystring");
363 goto done;
365 if (gw_trans->commit_id == NULL) {
366 error = got_error_msg(GOT_ERR_QUERYSTRING,
367 "commit required in querystring");
368 goto done;
371 error = gw_get_header(gw_trans, header, 1);
372 if (error)
373 goto done;
374 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
375 "blame_header_wrapper", KATTR__MAX);
376 if (kerr != KCGI_OK)
377 goto done;
378 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
379 "blame_header", KATTR__MAX);
380 if (kerr != KCGI_OK)
381 goto done;
382 error = gw_get_time_str(&age, header->committer_time,
383 TM_LONG);
384 if (error)
385 goto done;
386 error = gw_gen_age_header(gw_trans, age ?age : "");
387 if (error)
388 goto done;
389 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
390 if (error)
391 goto done;
392 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
393 if (kerr != KCGI_OK)
394 goto done;
395 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
396 "dotted_line", KATTR__MAX);
397 if (kerr != KCGI_OK)
398 goto done;
399 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
400 if (kerr != KCGI_OK)
401 goto done;
403 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
404 "blame", KATTR__MAX);
405 if (kerr != KCGI_OK)
406 goto done;
407 error = gw_output_file_blame(gw_trans, header);
408 if (error)
409 goto done;
410 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
411 done:
412 gw_free_header(header);
413 if (error == NULL && kerr != KCGI_OK)
414 error = gw_kcgi_error(kerr);
415 return error;
418 static const struct got_error *
419 gw_blob(struct gw_trans *gw_trans)
421 const struct got_error *error = NULL, *err = NULL;
422 struct gw_header *header = NULL;
423 enum kcgi_err kerr = KCGI_OK;
425 #ifndef PROFILE
426 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
427 NULL) == -1)
428 return got_error_from_errno("pledge");
429 #endif
430 if ((header = gw_init_header()) == NULL)
431 return got_error_from_errno("malloc");
433 error = gw_apply_unveil(gw_trans->gw_dir->path);
434 if (error)
435 goto done;
437 /* check querystring */
438 if (gw_trans->repo_file == NULL) {
439 error = got_error_msg(GOT_ERR_QUERYSTRING,
440 "file required in querystring");
441 goto done;
443 if (gw_trans->commit_id == NULL) {
444 error = got_error_msg(GOT_ERR_QUERYSTRING,
445 "commit required in querystring");
446 goto done;
448 error = gw_get_header(gw_trans, header, 1);
449 if (error)
450 goto done;
452 error = gw_output_blob_buf(gw_trans, header);
453 done:
455 if (error) {
456 gw_trans->mime = KMIME_TEXT_PLAIN;
457 err = gw_display_index(gw_trans);
458 if (err) {
459 error = err;
460 goto errored;
462 kerr = khttp_puts(gw_trans->gw_req, error->msg);
464 errored:
465 gw_free_header(header);
466 if (error == NULL && kerr != KCGI_OK)
467 error = gw_kcgi_error(kerr);
468 return error;
471 static const struct got_error *
472 gw_diff(struct gw_trans *gw_trans)
474 const struct got_error *error = NULL;
475 struct gw_header *header = NULL;
476 char *age = NULL;
477 enum kcgi_err kerr = KCGI_OK;
479 #ifndef PROFILE
480 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
481 NULL) == -1)
482 return got_error_from_errno("pledge");
483 #endif
484 if ((header = gw_init_header()) == NULL)
485 return got_error_from_errno("malloc");
487 error = gw_apply_unveil(gw_trans->gw_dir->path);
488 if (error)
489 goto done;
491 error = gw_get_header(gw_trans, header, 1);
492 if (error)
493 goto done;
495 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
496 "diff_header_wrapper", KATTR__MAX);
497 if (kerr != KCGI_OK)
498 goto done;
499 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
500 "diff_header", KATTR__MAX);
501 if (kerr != KCGI_OK)
502 goto done;
503 error = gw_gen_diff_header(gw_trans, header->parent_id,
504 header->commit_id);
505 if (error)
506 goto done;
507 error = gw_gen_commit_header(gw_trans, header->commit_id,
508 header->refs_str);
509 if (error)
510 goto done;
511 error = gw_gen_tree_header(gw_trans, header->tree_id);
512 if (error)
513 goto done;
514 error = gw_gen_author_header(gw_trans, header->author);
515 if (error)
516 goto done;
517 error = gw_gen_committer_header(gw_trans, header->author);
518 if (error)
519 goto done;
520 error = gw_get_time_str(&age, header->committer_time,
521 TM_LONG);
522 if (error)
523 goto done;
524 error = gw_gen_age_header(gw_trans, age ?age : "");
525 if (error)
526 goto done;
527 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
528 if (error)
529 goto done;
530 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
531 if (kerr != KCGI_OK)
532 goto done;
533 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
534 "dotted_line", KATTR__MAX);
535 if (kerr != KCGI_OK)
536 goto done;
537 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
538 if (kerr != KCGI_OK)
539 goto done;
541 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
542 "diff", KATTR__MAX);
543 if (kerr != KCGI_OK)
544 goto done;
545 error = gw_output_diff(gw_trans, header);
546 if (error)
547 goto done;
549 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
550 done:
551 gw_free_header(header);
552 free(age);
553 if (error == NULL && kerr != KCGI_OK)
554 error = gw_kcgi_error(kerr);
555 return error;
558 static const struct got_error *
559 gw_index(struct gw_trans *gw_trans)
561 const struct got_error *error = NULL;
562 struct gw_dir *gw_dir = NULL;
563 char *href_next = NULL, *href_prev = NULL, *href_summary = NULL;
564 char *href_briefs = NULL, *href_commits = NULL, *href_tree = NULL;
565 char *href_tags = NULL;
566 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
567 enum kcgi_err kerr = KCGI_OK;
569 #ifndef PROFILE
570 if (pledge("stdio rpath proc exec sendfd unveil",
571 NULL) == -1) {
572 error = got_error_from_errno("pledge");
573 return error;
575 #endif
576 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path);
577 if (error)
578 return error;
580 error = gw_load_got_paths(gw_trans);
581 if (error)
582 return error;
584 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
585 "index_header", KATTR__MAX);
586 if (kerr != KCGI_OK)
587 return gw_kcgi_error(kerr);
588 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
589 "index_header_project", KATTR__MAX);
590 if (kerr != KCGI_OK)
591 return gw_kcgi_error(kerr);
592 kerr = khtml_puts(gw_trans->gw_html_req, "Project");
593 if (kerr != KCGI_OK)
594 return gw_kcgi_error(kerr);
595 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
596 if (kerr != KCGI_OK)
597 return gw_kcgi_error(kerr);
599 if (gw_trans->gw_conf->got_show_repo_description) {
600 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
601 "index_header_description", KATTR__MAX);
602 if (kerr != KCGI_OK)
603 return gw_kcgi_error(kerr);
604 kerr = khtml_puts(gw_trans->gw_html_req, "Description");
605 if (kerr != KCGI_OK)
606 return gw_kcgi_error(kerr);
607 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
608 if (kerr != KCGI_OK)
609 return gw_kcgi_error(kerr);
612 if (gw_trans->gw_conf->got_show_repo_owner) {
613 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
614 "index_header_owner", KATTR__MAX);
615 if (kerr != KCGI_OK)
616 return gw_kcgi_error(kerr);
617 kerr = khtml_puts(gw_trans->gw_html_req, "Owner");
618 if (kerr != KCGI_OK)
619 return gw_kcgi_error(kerr);
620 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
621 if (kerr != KCGI_OK)
622 return gw_kcgi_error(kerr);
625 if (gw_trans->gw_conf->got_show_repo_age) {
626 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
627 "index_header_age", KATTR__MAX);
628 if (kerr != KCGI_OK)
629 return gw_kcgi_error(kerr);
630 kerr = khtml_puts(gw_trans->gw_html_req, "Last Change");
631 if (kerr != KCGI_OK)
632 return gw_kcgi_error(kerr);
633 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
634 if (kerr != KCGI_OK)
635 return gw_kcgi_error(kerr);
638 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
639 if (kerr != KCGI_OK)
640 return gw_kcgi_error(kerr);
642 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
643 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
644 "index_wrapper", KATTR__MAX);
645 if (kerr != KCGI_OK)
646 return gw_kcgi_error(kerr);
647 kerr = khtml_printf(gw_trans->gw_html_req,
648 "No repositories found in %s",
649 gw_trans->gw_conf->got_repos_path);
650 if (kerr != KCGI_OK)
651 return gw_kcgi_error(kerr);
652 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
653 if (kerr != KCGI_OK)
654 return gw_kcgi_error(kerr);
655 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
656 "dotted_line", KATTR__MAX);
657 if (kerr != KCGI_OK)
658 return gw_kcgi_error(kerr);
659 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
660 if (kerr != KCGI_OK)
661 return gw_kcgi_error(kerr);
662 return error;
665 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
666 dir_c++;
668 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
669 if (gw_trans->page > 0 && (gw_trans->page *
670 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
671 prev_disp++;
672 continue;
675 prev_disp++;
677 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
678 "index_wrapper", KATTR__MAX);
679 if (kerr != KCGI_OK)
680 goto done;
682 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
683 gw_dir->name, "action", "summary", NULL);
684 if (href_summary == NULL) {
685 error = got_error_from_errno("khttp_urlpart");
686 goto done;
688 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
689 "index_project", KATTR__MAX);
690 if (kerr != KCGI_OK)
691 goto done;
692 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
693 href_summary, KATTR__MAX);
694 if (kerr != KCGI_OK)
695 goto done;
696 kerr = khtml_puts(gw_trans->gw_html_req, gw_dir->name);
697 if (kerr != KCGI_OK)
698 goto done;
699 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
700 if (kerr != KCGI_OK)
701 goto done;
702 if (gw_trans->gw_conf->got_show_repo_description) {
703 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
704 KATTR_ID, "index_project_description", KATTR__MAX);
705 if (kerr != KCGI_OK)
706 goto done;
707 kerr = khtml_puts(gw_trans->gw_html_req,
708 gw_dir->description ? gw_dir->description : "");
709 if (kerr != KCGI_OK)
710 goto done;
711 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
712 if (kerr != KCGI_OK)
713 goto done;
715 if (gw_trans->gw_conf->got_show_repo_owner) {
716 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
717 KATTR_ID, "index_project_owner", KATTR__MAX);
718 if (kerr != KCGI_OK)
719 goto done;
720 kerr = khtml_puts(gw_trans->gw_html_req,
721 gw_dir->owner ? gw_dir->owner : "");
722 if (kerr != KCGI_OK)
723 goto done;
724 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
725 if (kerr != KCGI_OK)
726 goto done;
728 if (gw_trans->gw_conf->got_show_repo_age) {
729 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
730 KATTR_ID, "index_project_age", KATTR__MAX);
731 if (kerr != KCGI_OK)
732 goto done;
733 kerr = khtml_puts(gw_trans->gw_html_req,
734 gw_dir->age ? gw_dir->age : "");
735 if (kerr != KCGI_OK)
736 goto done;
737 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
738 if (kerr != KCGI_OK)
739 goto done;
742 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
743 "navs_wrapper", KATTR__MAX);
744 if (kerr != KCGI_OK)
745 goto done;
746 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
747 "navs", KATTR__MAX);
748 if (kerr != KCGI_OK)
749 goto done;
751 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
752 href_summary, KATTR__MAX);
753 if (kerr != KCGI_OK)
754 goto done;
755 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
756 if (kerr != KCGI_OK)
757 goto done;
758 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
759 if (kerr != KCGI_OK)
760 goto done;
762 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
763 if (kerr != KCGI_OK)
764 goto done;
766 href_briefs = khttp_urlpart(NULL, NULL, "gotweb", "path",
767 gw_dir->name, "action", "briefs", NULL);
768 if (href_briefs == NULL) {
769 error = got_error_from_errno("khttp_urlpart");
770 goto done;
772 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
773 href_briefs, KATTR__MAX);
774 if (kerr != KCGI_OK)
775 goto done;
776 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
777 if (kerr != KCGI_OK)
778 goto done;
779 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
780 if (kerr != KCGI_OK)
781 error = gw_kcgi_error(kerr);
783 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
784 if (kerr != KCGI_OK)
785 goto done;
787 href_commits = khttp_urlpart(NULL, NULL, "gotweb", "path",
788 gw_dir->name, "action", "commits", NULL);
789 if (href_commits == NULL) {
790 error = got_error_from_errno("khttp_urlpart");
791 goto done;
793 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
794 href_commits, KATTR__MAX);
795 if (kerr != KCGI_OK)
796 goto done;
797 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
798 if (kerr != KCGI_OK)
799 goto done;
800 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
801 if (kerr != KCGI_OK)
802 goto done;
804 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
805 if (kerr != KCGI_OK)
806 goto done;
808 href_tags = khttp_urlpart(NULL, NULL, "gotweb", "path",
809 gw_dir->name, "action", "tags", NULL);
810 if (href_tags == NULL) {
811 error = got_error_from_errno("khttp_urlpart");
812 goto done;
814 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
815 href_tags, KATTR__MAX);
816 if (kerr != KCGI_OK)
817 goto done;
818 kerr = khtml_puts(gw_trans->gw_html_req, "tags");
819 if (kerr != KCGI_OK)
820 goto done;
821 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
822 if (kerr != KCGI_OK)
823 goto done;
825 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
826 if (kerr != KCGI_OK)
827 goto done;
829 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
830 gw_dir->name, "action", "tree", NULL);
831 if (href_tree == NULL) {
832 error = got_error_from_errno("khttp_urlpart");
833 goto done;
835 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
836 href_tree, KATTR__MAX);
837 if (kerr != KCGI_OK)
838 goto done;
839 kerr = khtml_puts(gw_trans->gw_html_req, "tree");
840 if (kerr != KCGI_OK)
841 goto done;
843 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
844 if (kerr != KCGI_OK)
845 goto done;
846 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
847 "dotted_line", KATTR__MAX);
848 if (kerr != KCGI_OK)
849 goto done;
850 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
851 if (kerr != KCGI_OK)
852 goto done;
854 free(href_summary);
855 href_summary = NULL;
856 free(href_briefs);
857 href_briefs = NULL;
858 free(href_commits);
859 href_commits = NULL;
860 free(href_tags);
861 href_tags = NULL;
862 free(href_tree);
863 href_tree = NULL;
865 if (gw_trans->gw_conf->got_max_repos_display == 0)
866 continue;
868 if ((next_disp == gw_trans->gw_conf->got_max_repos_display) ||
869 ((gw_trans->gw_conf->got_max_repos_display > 0) &&
870 (gw_trans->page > 0) &&
871 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
872 prev_disp == gw_trans->repos_total))) {
873 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
874 KATTR_ID, "np_wrapper", KATTR__MAX);
875 if (kerr != KCGI_OK)
876 goto done;
877 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
878 KATTR_ID, "nav_prev", KATTR__MAX);
879 if (kerr != KCGI_OK)
880 goto done;
883 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
884 (gw_trans->page > 0) &&
885 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
886 prev_disp == gw_trans->repos_total)) {
887 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "page",
888 KATTRX_INT, (int64_t)(gw_trans->page - 1), NULL);
889 if (href_prev == NULL) {
890 error = got_error_from_errno("khttp_urlpartx");
891 goto done;
893 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
894 KATTR_HREF, href_prev, KATTR__MAX);
895 if (kerr != KCGI_OK)
896 goto done;
897 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
898 if (kerr != KCGI_OK)
899 goto done;
900 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
901 if (kerr != KCGI_OK)
902 goto done;
905 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
906 if (kerr != KCGI_OK)
907 return gw_kcgi_error(kerr);
909 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
910 next_disp == gw_trans->gw_conf->got_max_repos_display &&
911 dir_c != (gw_trans->page + 1) *
912 gw_trans->gw_conf->got_max_repos_display) {
913 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
914 KATTR_ID, "nav_next", KATTR__MAX);
915 if (kerr != KCGI_OK)
916 goto done;
917 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "page",
918 KATTRX_INT, (int64_t)(gw_trans->page + 1), NULL);
919 if (href_next == NULL) {
920 error = got_error_from_errno("khttp_urlpartx");
921 goto done;
923 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
924 KATTR_HREF, href_next, KATTR__MAX);
925 if (kerr != KCGI_OK)
926 goto done;
927 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
928 if (kerr != KCGI_OK)
929 goto done;
930 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
931 if (kerr != KCGI_OK)
932 goto done;
933 next_disp = 0;
934 break;
937 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
938 (gw_trans->page > 0) &&
939 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
940 prev_disp == gw_trans->repos_total)) {
941 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
942 if (kerr != KCGI_OK)
943 goto done;
945 next_disp++;
947 done:
948 free(href_prev);
949 free(href_next);
950 free(href_summary);
951 free(href_briefs);
952 free(href_commits);
953 free(href_tags);
954 free(href_tree);
955 if (error == NULL && kerr != KCGI_OK)
956 error = gw_kcgi_error(kerr);
957 return error;
960 static const struct got_error *
961 gw_commits(struct gw_trans *gw_trans)
963 const struct got_error *error = NULL;
964 struct gw_header *header = NULL, *n_header = NULL;
965 char *age = NULL, *href_diff = NULL, *href_tree = NULL;
966 char *href_prev = NULL, *href_next = NULL;
967 enum kcgi_err kerr = KCGI_OK;
968 int commit_found = 0;
970 if ((header = gw_init_header()) == NULL)
971 return got_error_from_errno("malloc");
973 #ifndef PROFILE
974 if (pledge("stdio rpath proc exec sendfd unveil",
975 NULL) == -1) {
976 error = got_error_from_errno("pledge");
977 goto done;
979 #endif
980 error = gw_apply_unveil(gw_trans->gw_dir->path);
981 if (error)
982 goto done;
984 error = gw_get_header(gw_trans, header,
985 gw_trans->gw_conf->got_max_commits_display);
986 if (error)
987 goto done;
989 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
990 if (commit_found == 0 && gw_trans->commit_id != NULL) {
991 if (strcmp(gw_trans->commit_id,
992 n_header->commit_id) != 0)
993 continue;
994 else
995 commit_found = 1;
997 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
998 "commits_line_wrapper", KATTR__MAX);
999 if (kerr != KCGI_OK)
1000 goto done;
1001 error = gw_gen_commit_header(gw_trans, n_header->commit_id,
1002 n_header->refs_str);
1003 if (error)
1004 goto done;
1005 error = gw_gen_author_header(gw_trans, n_header->author);
1006 if (error)
1007 goto done;
1008 error = gw_gen_committer_header(gw_trans, n_header->author);
1009 if (error)
1010 goto done;
1011 error = gw_get_time_str(&age, n_header->committer_time,
1012 TM_LONG);
1013 if (error)
1014 goto done;
1015 error = gw_gen_age_header(gw_trans, age ?age : "");
1016 if (error)
1017 goto done;
1018 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1019 if (kerr != KCGI_OK)
1020 goto done;
1022 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1023 "dotted_line", KATTR__MAX);
1024 if (kerr != KCGI_OK)
1025 goto done;
1026 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1027 if (kerr != KCGI_OK)
1028 goto done;
1030 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1031 "commit", KATTR__MAX);
1032 if (kerr != KCGI_OK)
1033 goto done;
1034 kerr = khttp_puts(gw_trans->gw_req, n_header->commit_msg);
1035 if (kerr != KCGI_OK)
1036 goto done;
1037 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1038 if (kerr != KCGI_OK)
1039 goto done;
1041 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
1042 gw_trans->repo_name, "action", "diff", "commit",
1043 n_header->commit_id, NULL);
1044 if (href_diff == NULL) {
1045 error = got_error_from_errno("khttp_urlpart");
1046 goto done;
1048 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1049 KATTR_ID, "navs_wrapper", KATTR__MAX);
1050 if (kerr != KCGI_OK)
1051 goto done;
1052 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1053 KATTR_ID, "navs", KATTR__MAX);
1054 if (kerr != KCGI_OK)
1055 goto done;
1056 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1057 KATTR_HREF, href_diff, KATTR__MAX);
1058 if (kerr != KCGI_OK)
1059 goto done;
1060 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1061 if (kerr != KCGI_OK)
1062 goto done;
1063 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1064 if (kerr != KCGI_OK)
1065 goto done;
1067 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1068 if (kerr != KCGI_OK)
1069 goto done;
1071 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
1072 gw_trans->repo_name, "action", "tree", "commit",
1073 n_header->commit_id, NULL);
1074 if (href_tree == NULL) {
1075 error = got_error_from_errno("khttp_urlpart");
1076 goto done;
1078 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1079 KATTR_HREF, href_tree, KATTR__MAX);
1080 if (kerr != KCGI_OK)
1081 goto done;
1082 khtml_puts(gw_trans->gw_html_req, "tree");
1083 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1084 if (kerr != KCGI_OK)
1085 goto done;
1086 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1087 if (kerr != KCGI_OK)
1088 goto done;
1090 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1091 "solid_line", KATTR__MAX);
1092 if (kerr != KCGI_OK)
1093 goto done;
1094 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1095 if (kerr != KCGI_OK)
1096 goto done;
1098 free(age);
1099 age = NULL;
1102 if (gw_trans->next_id || gw_trans->prev_id) {
1103 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1104 KATTR_ID, "np_wrapper", KATTR__MAX);
1105 if (kerr != KCGI_OK)
1106 goto done;
1107 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1108 KATTR_ID, "nav_prev", KATTR__MAX);
1109 if (kerr != KCGI_OK)
1110 goto done;
1113 if (gw_trans->prev_id) {
1114 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1115 KATTRX_STRING, gw_trans->repo_name, "page",
1116 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1117 KATTRX_STRING, "commits", "commit", KATTRX_STRING,
1118 gw_trans->prev_id ? gw_trans->prev_id : "", NULL);
1119 if (href_prev == NULL) {
1120 error = got_error_from_errno("khttp_urlpartx");
1121 goto done;
1123 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1124 KATTR_HREF, href_prev, KATTR__MAX);
1125 if (kerr != KCGI_OK)
1126 goto done;
1127 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1128 if (kerr != KCGI_OK)
1129 goto done;
1130 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1131 if (kerr != KCGI_OK)
1132 goto done;
1135 if (gw_trans->next_id || gw_trans->page > 0) {
1136 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1137 if (kerr != KCGI_OK)
1138 return gw_kcgi_error(kerr);
1141 if (gw_trans->next_id) {
1142 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1143 KATTR_ID, "nav_next", KATTR__MAX);
1144 if (kerr != KCGI_OK)
1145 goto done;
1146 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1147 KATTRX_STRING, gw_trans->repo_name, "page",
1148 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1149 KATTRX_STRING, "commits", "commit", KATTRX_STRING,
1150 gw_trans->next_id, NULL);
1151 if (href_next == NULL) {
1152 error = got_error_from_errno("khttp_urlpartx");
1153 goto done;
1155 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1156 KATTR_HREF, href_next, KATTR__MAX);
1157 if (kerr != KCGI_OK)
1158 goto done;
1159 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1160 if (kerr != KCGI_OK)
1161 goto done;
1162 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1163 if (kerr != KCGI_OK)
1164 goto done;
1167 if (gw_trans->next_id || gw_trans->page > 0) {
1168 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1169 if (kerr != KCGI_OK)
1170 goto done;
1172 done:
1173 gw_free_header(header);
1174 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1175 gw_free_header(n_header);
1176 free(age);
1177 free(href_next);
1178 free(href_prev);
1179 free(href_diff);
1180 free(href_tree);
1181 if (error == NULL && kerr != KCGI_OK)
1182 error = gw_kcgi_error(kerr);
1183 return error;
1186 static const struct got_error *
1187 gw_briefs(struct gw_trans *gw_trans)
1189 const struct got_error *error = NULL;
1190 struct gw_header *header = NULL, *n_header = NULL;
1191 char *age = NULL, *href_diff = NULL, *href_tree = NULL;
1192 char *href_prev = NULL, *href_next = NULL;
1193 char *newline, *smallerthan;
1194 enum kcgi_err kerr = KCGI_OK;
1195 int commit_found = 0;
1197 if ((header = gw_init_header()) == NULL)
1198 return got_error_from_errno("malloc");
1200 #ifndef PROFILE
1201 if (pledge("stdio rpath proc exec sendfd unveil",
1202 NULL) == -1) {
1203 error = got_error_from_errno("pledge");
1204 goto done;
1206 #endif
1207 if (gw_trans->action != GW_SUMMARY) {
1208 error = gw_apply_unveil(gw_trans->gw_dir->path);
1209 if (error)
1210 goto done;
1213 if (gw_trans->action == GW_SUMMARY)
1214 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
1215 else
1216 error = gw_get_header(gw_trans, header,
1217 gw_trans->gw_conf->got_max_commits_display);
1218 if (error)
1219 goto done;
1221 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
1222 if (commit_found == 0 && gw_trans->commit_id != NULL) {
1223 if (strcmp(gw_trans->commit_id,
1224 n_header->commit_id) != 0)
1225 continue;
1226 else
1227 commit_found = 1;
1229 error = gw_get_time_str(&age, n_header->committer_time,
1230 TM_DIFF);
1231 if (error)
1232 goto done;
1234 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1235 KATTR_ID, "briefs_wrapper", KATTR__MAX);
1236 if (kerr != KCGI_OK)
1237 goto done;
1239 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1240 KATTR_ID, "briefs_age", KATTR__MAX);
1241 if (kerr != KCGI_OK)
1242 goto done;
1243 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
1244 if (kerr != KCGI_OK)
1245 goto done;
1246 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1247 if (kerr != KCGI_OK)
1248 goto done;
1250 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1251 KATTR_ID, "briefs_author", KATTR__MAX);
1252 if (kerr != KCGI_OK)
1253 goto done;
1254 smallerthan = strchr(n_header->author, '<');
1255 if (smallerthan)
1256 *smallerthan = '\0';
1257 kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
1258 if (kerr != KCGI_OK)
1259 goto done;
1260 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1261 if (kerr != KCGI_OK)
1262 goto done;
1264 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
1265 gw_trans->repo_name, "action", "diff", "commit",
1266 n_header->commit_id, NULL);
1267 if (href_diff == NULL) {
1268 error = got_error_from_errno("khttp_urlpart");
1269 goto done;
1271 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1272 KATTR_ID, "briefs_log", KATTR__MAX);
1273 if (kerr != KCGI_OK)
1274 goto done;
1275 newline = strchr(n_header->commit_msg, '\n');
1276 if (newline)
1277 *newline = '\0';
1278 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1279 KATTR_HREF, href_diff, KATTR__MAX);
1280 if (kerr != KCGI_OK)
1281 goto done;
1282 kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
1283 if (kerr != KCGI_OK)
1284 goto done;
1285 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1286 if (kerr != KCGI_OK)
1287 goto done;
1289 if (n_header->refs_str) {
1290 kerr = khtml_puts(gw_trans->gw_html_req, " ");
1291 if (kerr != KCGI_OK)
1292 goto done;
1293 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
1294 KATTR_ID, "refs_str", KATTR__MAX);
1295 if (kerr != KCGI_OK)
1296 goto done;
1297 kerr = khtml_printf(gw_trans->gw_html_req, "(%s)",
1298 n_header->refs_str);
1299 if (kerr != KCGI_OK)
1300 goto done;
1301 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1302 if (kerr != KCGI_OK)
1303 goto done;
1306 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1307 if (kerr != KCGI_OK)
1308 goto done;
1310 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1311 KATTR_ID, "navs_wrapper", KATTR__MAX);
1312 if (kerr != KCGI_OK)
1313 goto done;
1314 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1315 KATTR_ID, "navs", KATTR__MAX);
1316 if (kerr != KCGI_OK)
1317 goto done;
1318 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1319 KATTR_HREF, href_diff, KATTR__MAX);
1320 if (kerr != KCGI_OK)
1321 goto done;
1322 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1323 if (kerr != KCGI_OK)
1324 goto done;
1325 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1326 if (kerr != KCGI_OK)
1327 goto done;
1329 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1330 if (kerr != KCGI_OK)
1331 goto done;
1333 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
1334 gw_trans->repo_name, "action", "tree", "commit",
1335 n_header->commit_id, NULL);
1336 if (href_tree == NULL) {
1337 error = got_error_from_errno("khttp_urlpart");
1338 goto done;
1340 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1341 KATTR_HREF, href_tree, KATTR__MAX);
1342 if (kerr != KCGI_OK)
1343 goto done;
1344 khtml_puts(gw_trans->gw_html_req, "tree");
1345 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1346 if (kerr != KCGI_OK)
1347 goto done;
1348 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1349 if (kerr != KCGI_OK)
1350 goto done;
1352 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1353 KATTR_ID, "dotted_line", KATTR__MAX);
1354 if (kerr != KCGI_OK)
1355 goto done;
1356 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1357 if (kerr != KCGI_OK)
1358 goto done;
1360 free(age);
1361 age = NULL;
1362 free(href_diff);
1363 href_diff = NULL;
1364 free(href_tree);
1365 href_tree = NULL;
1368 if (gw_trans->next_id || gw_trans->prev_id) {
1369 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1370 KATTR_ID, "np_wrapper", KATTR__MAX);
1371 if (kerr != KCGI_OK)
1372 goto done;
1373 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1374 KATTR_ID, "nav_prev", KATTR__MAX);
1375 if (kerr != KCGI_OK)
1376 goto done;
1379 if (gw_trans->prev_id) {
1380 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1381 KATTRX_STRING, gw_trans->repo_name, "page",
1382 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1383 KATTRX_STRING, "briefs", "commit", KATTRX_STRING,
1384 gw_trans->prev_id ? gw_trans->prev_id : "", NULL);
1385 if (href_prev == NULL) {
1386 error = got_error_from_errno("khttp_urlpartx");
1387 goto done;
1389 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1390 KATTR_HREF, href_prev, KATTR__MAX);
1391 if (kerr != KCGI_OK)
1392 goto done;
1393 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1394 if (kerr != KCGI_OK)
1395 goto done;
1396 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1397 if (kerr != KCGI_OK)
1398 goto done;
1401 if (gw_trans->next_id || gw_trans->page > 0) {
1402 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1403 if (kerr != KCGI_OK)
1404 return gw_kcgi_error(kerr);
1407 if (gw_trans->next_id) {
1408 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1409 KATTR_ID, "nav_next", KATTR__MAX);
1410 if (kerr != KCGI_OK)
1411 goto done;
1413 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1414 KATTRX_STRING, gw_trans->repo_name, "page",
1415 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1416 KATTRX_STRING, "briefs", "commit", KATTRX_STRING,
1417 gw_trans->next_id, NULL);
1418 if (href_next == NULL) {
1419 error = got_error_from_errno("khttp_urlpartx");
1420 goto done;
1422 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1423 KATTR_HREF, href_next, KATTR__MAX);
1424 if (kerr != KCGI_OK)
1425 goto done;
1426 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1427 if (kerr != KCGI_OK)
1428 goto done;
1429 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1430 if (kerr != KCGI_OK)
1431 goto done;
1434 if (gw_trans->next_id || gw_trans->page > 0) {
1435 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1436 if (kerr != KCGI_OK)
1437 goto done;
1439 done:
1440 gw_free_header(header);
1441 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1442 gw_free_header(n_header);
1443 free(age);
1444 free(href_next);
1445 free(href_prev);
1446 free(href_diff);
1447 free(href_tree);
1448 if (error == NULL && kerr != KCGI_OK)
1449 error = gw_kcgi_error(kerr);
1450 return error;
1453 static const struct got_error *
1454 gw_summary(struct gw_trans *gw_trans)
1456 const struct got_error *error = NULL;
1457 char *age = NULL;
1458 enum kcgi_err kerr = KCGI_OK;
1460 #ifndef PROFILE
1461 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1462 return got_error_from_errno("pledge");
1463 #endif
1464 error = gw_apply_unveil(gw_trans->gw_dir->path);
1465 if (error)
1466 goto done;
1468 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1469 "summary_wrapper", KATTR__MAX);
1470 if (kerr != KCGI_OK)
1471 return gw_kcgi_error(kerr);
1473 if (gw_trans->gw_conf->got_show_repo_description &&
1474 gw_trans->gw_dir->description != NULL &&
1475 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1476 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1477 KATTR_ID, "description_title", KATTR__MAX);
1478 if (kerr != KCGI_OK)
1479 goto done;
1480 kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1481 if (kerr != KCGI_OK)
1482 goto done;
1483 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1484 if (kerr != KCGI_OK)
1485 goto done;
1486 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1487 KATTR_ID, "description", KATTR__MAX);
1488 if (kerr != KCGI_OK)
1489 goto done;
1490 kerr = khtml_puts(gw_trans->gw_html_req,
1491 gw_trans->gw_dir->description);
1492 if (kerr != KCGI_OK)
1493 goto done;
1494 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1495 if (kerr != KCGI_OK)
1496 goto done;
1499 if (gw_trans->gw_conf->got_show_repo_owner &&
1500 gw_trans->gw_dir->owner != NULL &&
1501 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
1502 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1503 KATTR_ID, "repo_owner_title", KATTR__MAX);
1504 if (kerr != KCGI_OK)
1505 goto done;
1506 kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1507 if (kerr != KCGI_OK)
1508 goto done;
1509 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1510 if (kerr != KCGI_OK)
1511 goto done;
1512 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1513 KATTR_ID, "repo_owner", KATTR__MAX);
1514 if (kerr != KCGI_OK)
1515 goto done;
1516 kerr = khtml_puts(gw_trans->gw_html_req,
1517 gw_trans->gw_dir->owner);
1518 if (kerr != KCGI_OK)
1519 goto done;
1520 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1521 if (kerr != KCGI_OK)
1522 goto done;
1525 if (gw_trans->gw_conf->got_show_repo_age) {
1526 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1527 NULL, TM_LONG);
1528 if (error)
1529 goto done;
1530 if (age != NULL) {
1531 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1532 KATTR_ID, "last_change_title", KATTR__MAX);
1533 if (kerr != KCGI_OK)
1534 goto done;
1535 kerr = khtml_puts(gw_trans->gw_html_req,
1536 "Last Change: ");
1537 if (kerr != KCGI_OK)
1538 goto done;
1539 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1540 if (kerr != KCGI_OK)
1541 goto done;
1542 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1543 KATTR_ID, "last_change", KATTR__MAX);
1544 if (kerr != KCGI_OK)
1545 goto done;
1546 kerr = khtml_puts(gw_trans->gw_html_req, age);
1547 if (kerr != KCGI_OK)
1548 goto done;
1549 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1550 if (kerr != KCGI_OK)
1551 goto done;
1555 if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1556 gw_trans->gw_dir->url != NULL &&
1557 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1558 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1559 KATTR_ID, "cloneurl_title", KATTR__MAX);
1560 if (kerr != KCGI_OK)
1561 goto done;
1562 kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1563 if (kerr != KCGI_OK)
1564 goto done;
1565 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1566 if (kerr != KCGI_OK)
1567 goto done;
1568 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1569 KATTR_ID, "cloneurl", KATTR__MAX);
1570 if (kerr != KCGI_OK)
1571 goto done;
1572 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1573 if (kerr != KCGI_OK)
1574 goto done;
1575 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1576 if (kerr != KCGI_OK)
1577 goto done;
1580 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1581 if (kerr != KCGI_OK)
1582 goto done;
1584 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1585 "briefs_title_wrapper", KATTR__MAX);
1586 if (kerr != KCGI_OK)
1587 goto done;
1588 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1589 "briefs_title", KATTR__MAX);
1590 if (kerr != KCGI_OK)
1591 goto done;
1592 kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1593 if (kerr != KCGI_OK)
1594 goto done;
1595 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1596 if (kerr != KCGI_OK)
1597 goto done;
1598 error = gw_briefs(gw_trans);
1599 if (error)
1600 goto done;
1602 error = gw_tags(gw_trans);
1603 if (error)
1604 goto done;
1606 error = gw_output_repo_heads(gw_trans);
1607 done:
1608 free(age);
1609 if (error == NULL && kerr != KCGI_OK)
1610 error = gw_kcgi_error(kerr);
1611 return error;
1614 static const struct got_error *
1615 gw_tree(struct gw_trans *gw_trans)
1617 const struct got_error *error = NULL;
1618 struct gw_header *header = NULL;
1619 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1620 char *age = NULL;
1621 enum kcgi_err kerr = KCGI_OK;
1623 #ifndef PROFILE
1624 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1625 return got_error_from_errno("pledge");
1626 #endif
1627 if ((header = gw_init_header()) == NULL)
1628 return got_error_from_errno("malloc");
1630 error = gw_apply_unveil(gw_trans->gw_dir->path);
1631 if (error)
1632 goto done;
1634 error = gw_get_header(gw_trans, header, 1);
1635 if (error)
1636 goto done;
1638 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1639 "tree_header_wrapper", KATTR__MAX);
1640 if (kerr != KCGI_OK)
1641 goto done;
1642 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1643 "tree_header", KATTR__MAX);
1644 if (kerr != KCGI_OK)
1645 goto done;
1646 error = gw_gen_tree_header(gw_trans, header->tree_id);
1647 if (error)
1648 goto done;
1649 error = gw_get_time_str(&age, header->committer_time,
1650 TM_LONG);
1651 if (error)
1652 goto done;
1653 error = gw_gen_age_header(gw_trans, age ?age : "");
1654 if (error)
1655 goto done;
1656 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1657 if (error)
1658 goto done;
1659 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1660 if (kerr != KCGI_OK)
1661 goto done;
1662 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1663 "dotted_line", KATTR__MAX);
1664 if (kerr != KCGI_OK)
1665 goto done;
1666 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1667 if (kerr != KCGI_OK)
1668 goto done;
1670 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1671 "tree", KATTR__MAX);
1672 if (kerr != KCGI_OK)
1673 goto done;
1674 error = gw_output_repo_tree(gw_trans, header);
1675 if (error)
1676 goto done;
1678 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1679 done:
1680 gw_free_header(header);
1681 free(tree_html_disp);
1682 free(tree_html);
1683 free(tree);
1684 free(age);
1685 if (error == NULL && kerr != KCGI_OK)
1686 error = gw_kcgi_error(kerr);
1687 return error;
1690 static const struct got_error *
1691 gw_tags(struct gw_trans *gw_trans)
1693 const struct got_error *error = NULL;
1694 struct gw_header *header = NULL;
1695 char *href_next = NULL, *href_prev = NULL;
1696 enum kcgi_err kerr = KCGI_OK;
1698 #ifndef PROFILE
1699 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1700 return got_error_from_errno("pledge");
1701 #endif
1702 if ((header = gw_init_header()) == NULL)
1703 return got_error_from_errno("malloc");
1705 if (gw_trans->action != GW_SUMMARY) {
1706 error = gw_apply_unveil(gw_trans->gw_dir->path);
1707 if (error)
1708 goto done;
1711 error = gw_get_header(gw_trans, header, 1);
1712 if (error)
1713 goto done;
1715 if (gw_trans->action == GW_SUMMARY) {
1716 gw_trans->next_id = NULL;
1717 error = gw_output_repo_tags(gw_trans, header,
1718 D_MAXSLCOMMDISP, TAGBRIEF);
1719 if (error)
1720 goto done;
1721 } else {
1722 error = gw_output_repo_tags(gw_trans, header,
1723 gw_trans->gw_conf->got_max_commits_display, TAGBRIEF);
1724 if (error)
1725 goto done;
1728 if (gw_trans->next_id || gw_trans->page > 0) {
1729 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1730 KATTR_ID, "np_wrapper", KATTR__MAX);
1731 if (kerr != KCGI_OK)
1732 goto done;
1733 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1734 KATTR_ID, "nav_prev", KATTR__MAX);
1735 if (kerr != KCGI_OK)
1736 goto done;
1739 if (gw_trans->prev_id) {
1740 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1741 KATTRX_STRING, gw_trans->repo_name, "page",
1742 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1743 KATTRX_STRING, "tags", "commit", KATTRX_STRING,
1744 gw_trans->prev_id ? gw_trans->prev_id : "", NULL);
1745 if (href_prev == NULL) {
1746 error = got_error_from_errno("khttp_urlpartx");
1747 goto done;
1749 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1750 KATTR_HREF, href_prev, KATTR__MAX);
1751 if (kerr != KCGI_OK)
1752 goto done;
1753 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1754 if (kerr != KCGI_OK)
1755 goto done;
1756 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1757 if (kerr != KCGI_OK)
1758 goto done;
1761 if (gw_trans->next_id || gw_trans->page > 0) {
1762 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1763 if (kerr != KCGI_OK)
1764 return gw_kcgi_error(kerr);
1767 if (gw_trans->next_id) {
1768 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1769 KATTR_ID, "nav_next", KATTR__MAX);
1770 if (kerr != KCGI_OK)
1771 goto done;
1772 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1773 KATTRX_STRING, gw_trans->repo_name, "page",
1774 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1775 KATTRX_STRING, "tags", "commit", KATTRX_STRING,
1776 gw_trans->next_id, NULL);
1777 if (href_next == NULL) {
1778 error = got_error_from_errno("khttp_urlpartx");
1779 goto done;
1781 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1782 KATTR_HREF, href_next, KATTR__MAX);
1783 if (kerr != KCGI_OK)
1784 goto done;
1785 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1786 if (kerr != KCGI_OK)
1787 goto done;
1788 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1789 if (kerr != KCGI_OK)
1790 goto done;
1793 if (gw_trans->next_id || gw_trans->page > 0) {
1794 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1795 if (kerr != KCGI_OK)
1796 goto done;
1798 done:
1799 gw_free_header(header);
1800 free(href_next);
1801 free(href_prev);
1802 if (error == NULL && kerr != KCGI_OK)
1803 error = gw_kcgi_error(kerr);
1804 return error;
1807 static const struct got_error *
1808 gw_tag(struct gw_trans *gw_trans)
1810 const struct got_error *error = NULL;
1811 struct gw_header *header = NULL;
1812 enum kcgi_err kerr = KCGI_OK;
1814 #ifndef PROFILE
1815 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1816 return got_error_from_errno("pledge");
1817 #endif
1818 if ((header = gw_init_header()) == NULL)
1819 return got_error_from_errno("malloc");
1821 error = gw_apply_unveil(gw_trans->gw_dir->path);
1822 if (error)
1823 goto done;
1825 if (gw_trans->commit_id == NULL) {
1826 error = got_error_msg(GOT_ERR_QUERYSTRING,
1827 "commit required in querystring");
1828 goto done;
1831 error = gw_get_header(gw_trans, header, 1);
1832 if (error)
1833 goto done;
1835 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1836 "tag_header_wrapper", KATTR__MAX);
1837 if (kerr != KCGI_OK)
1838 goto done;
1839 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1840 "tag_header", KATTR__MAX);
1841 if (kerr != KCGI_OK)
1842 goto done;
1843 error = gw_gen_commit_header(gw_trans, header->commit_id,
1844 header->refs_str);
1845 if (error)
1846 goto done;
1847 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1848 if (error)
1849 goto done;
1850 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1851 if (kerr != KCGI_OK)
1852 goto done;
1853 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1854 "dotted_line", KATTR__MAX);
1855 if (kerr != KCGI_OK)
1856 goto done;
1857 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1858 if (kerr != KCGI_OK)
1859 goto done;
1861 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1862 "tree", KATTR__MAX);
1863 if (kerr != KCGI_OK)
1864 goto done;
1866 error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1867 if (error)
1868 goto done;
1870 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1871 done:
1872 gw_free_header(header);
1873 if (error == NULL && kerr != KCGI_OK)
1874 error = gw_kcgi_error(kerr);
1875 return error;
1878 static const struct got_error *
1879 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1881 const struct got_error *error = NULL;
1882 DIR *dt;
1883 char *dir_test;
1884 int opened = 0;
1886 if (asprintf(&dir_test, "%s/%s/%s",
1887 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1888 GOTWEB_GIT_DIR) == -1)
1889 return got_error_from_errno("asprintf");
1891 dt = opendir(dir_test);
1892 if (dt == NULL) {
1893 free(dir_test);
1894 } else {
1895 gw_dir->path = strdup(dir_test);
1896 if (gw_dir->path == NULL) {
1897 opened = 1;
1898 error = got_error_from_errno("strdup");
1899 goto errored;
1901 opened = 1;
1902 goto done;
1905 if (asprintf(&dir_test, "%s/%s/%s",
1906 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1907 GOTWEB_GOT_DIR) == -1) {
1908 dir_test = NULL;
1909 error = got_error_from_errno("asprintf");
1910 goto errored;
1913 dt = opendir(dir_test);
1914 if (dt == NULL)
1915 free(dir_test);
1916 else {
1917 opened = 1;
1918 error = got_error(GOT_ERR_NOT_GIT_REPO);
1919 goto errored;
1922 if (asprintf(&dir_test, "%s/%s",
1923 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1) {
1924 error = got_error_from_errno("asprintf");
1925 dir_test = NULL;
1926 goto errored;
1929 gw_dir->path = strdup(dir_test);
1930 if (gw_dir->path == NULL) {
1931 opened = 1;
1932 error = got_error_from_errno("strdup");
1933 goto errored;
1936 dt = opendir(dir_test);
1937 if (dt == NULL) {
1938 error = got_error_path(gw_dir->name, GOT_ERR_NOT_GIT_REPO);
1939 goto errored;
1940 } else
1941 opened = 1;
1942 done:
1943 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1944 gw_dir->path);
1945 if (error)
1946 goto errored;
1947 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1948 if (error)
1949 goto errored;
1950 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1951 NULL, TM_DIFF);
1952 if (error)
1953 goto errored;
1954 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1955 errored:
1956 free(dir_test);
1957 if (opened)
1958 if (dt && closedir(dt) == -1 && error == NULL)
1959 error = got_error_from_errno("closedir");
1960 return error;
1963 static const struct got_error *
1964 gw_load_got_paths(struct gw_trans *gw_trans)
1966 const struct got_error *error = NULL;
1967 DIR *d;
1968 struct dirent **sd_dent;
1969 struct gw_dir *gw_dir;
1970 struct stat st;
1971 unsigned int d_cnt, d_i;
1973 d = opendir(gw_trans->gw_conf->got_repos_path);
1974 if (d == NULL) {
1975 error = got_error_from_errno2("opendir",
1976 gw_trans->gw_conf->got_repos_path);
1977 return error;
1980 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1981 alphasort);
1982 if (d_cnt == -1) {
1983 error = got_error_from_errno2("scandir",
1984 gw_trans->gw_conf->got_repos_path);
1985 goto done;
1988 for (d_i = 0; d_i < d_cnt; d_i++) {
1989 if (gw_trans->gw_conf->got_max_repos > 0 &&
1990 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1991 break; /* account for parent and self */
1993 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1994 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1995 continue;
1997 error = gw_init_gw_dir(&gw_dir, sd_dent[d_i]->d_name);
1998 if (error)
1999 goto done;
2001 error = gw_load_got_path(gw_trans, gw_dir);
2002 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
2003 error = NULL;
2004 continue;
2005 } else if (error && error->code != GOT_ERR_LONELY_PACKIDX)
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 const 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, "re");
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 NULL, "", "", 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 NULL, 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, "re");
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 struct got_object_id *id;
3391 struct got_commit_object *commit = NULL;
3393 refname = got_ref_get_name(re->ref);
3394 if (strncmp(refname, "refs/tags/", 10) != 0)
3395 continue;
3396 refname += 10;
3398 error = got_ref_resolve(&id, gw_trans->repo, re->ref);
3399 if (error)
3400 goto done;
3402 error = got_object_open_as_tag(&tag, gw_trans->repo, id);
3403 if (error) {
3404 if (error->code != GOT_ERR_OBJ_TYPE) {
3405 free(id);
3406 goto done;
3408 /* "lightweight" tag */
3409 error = got_object_open_as_commit(&commit,
3410 gw_trans->repo, id);
3411 if (error) {
3412 free(id);
3413 goto done;
3415 error = got_object_id_str(&id_str, id);
3416 free(id);
3417 } else {
3418 free(id);
3419 error = got_object_id_str(&id_str,
3420 got_object_tag_get_object_id(tag));
3422 if (error)
3423 goto done;
3425 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
3426 strlen(id_str)) != 0)
3427 continue;
3429 if (commit_found == 0 && tag_type == TAGBRIEF &&
3430 gw_trans->commit_id != NULL &&
3431 strncmp(id_str, gw_trans->commit_id, strlen(id_str)) != 0)
3432 continue;
3433 else
3434 commit_found = 1;
3436 if (gw_trans->commit_id != NULL &&
3437 strcmp(id_str, gw_trans->commit_id) != 0 &&
3438 (re == TAILQ_FIRST(&refs) ||
3439 c_cnt == gw_trans->gw_conf->got_max_commits_display)) {
3440 gw_trans->prev_id = strdup(id_str);
3441 if (gw_trans->prev_id == NULL) {
3442 error = got_error_from_errno("strdup");
3443 goto done;
3445 break;
3447 c_cnt++;
3449 done:
3450 if (tag)
3451 got_object_tag_close(tag);
3452 free(id_str);
3453 free(age);
3454 free(tag_commit0);
3455 free(href_tag);
3456 free(href_briefs);
3457 free(href_commits);
3458 got_ref_list_free(&refs);
3459 if (error == NULL && kerr != KCGI_OK)
3460 error = gw_kcgi_error(kerr);
3461 return error;
3464 static void
3465 gw_free_header(struct gw_header *header)
3467 free(header->path);
3468 free(header->author);
3469 free(header->committer);
3470 free(header->refs_str);
3471 free(header->commit_id);
3472 free(header->parent_id);
3473 free(header->tree_id);
3474 free(header->commit_msg);
3477 static struct gw_header *
3478 gw_init_header()
3480 struct gw_header *header;
3482 header = malloc(sizeof(*header));
3483 if (header == NULL)
3484 return NULL;
3486 header->path = NULL;
3487 TAILQ_INIT(&header->refs);
3489 header->refs_str = NULL;
3490 header->commit_id = NULL;
3491 header->committer = NULL;
3492 header->author = NULL;
3493 header->parent_id = NULL;
3494 header->tree_id = NULL;
3495 header->commit_msg = NULL;
3497 return header;
3500 static const struct got_error *
3501 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
3502 int limit, struct got_object_id *id)
3504 const struct got_error *error = NULL;
3505 struct got_commit_graph *graph = NULL;
3506 struct got_commit_object *commit = NULL;
3507 int chk_next = 0, chk_multi = 0, c_cnt = 0, commit_found = 0;
3508 struct gw_header *t_header = NULL;
3510 error = got_commit_graph_open(&graph, header->path, 0);
3511 if (error)
3512 return error;
3514 error = got_commit_graph_iter_start(graph, id, gw_trans->repo, NULL,
3515 NULL);
3516 if (error)
3517 goto err;
3519 for (;;) {
3520 error = got_commit_graph_iter_next(&id, graph, gw_trans->repo,
3521 NULL, NULL);
3522 if (error) {
3523 if (error->code == GOT_ERR_ITER_COMPLETED)
3524 error = NULL;
3525 goto done;
3527 if (id == NULL)
3528 goto err;
3530 error = got_object_open_as_commit(&commit, gw_trans->repo, id);
3531 if (error)
3532 goto err;
3533 if (limit == 1 && chk_multi == 0 &&
3534 gw_trans->gw_conf->got_max_commits_display != 1) {
3535 error = gw_get_commit(gw_trans, header, commit, id);
3536 if (error)
3537 goto err;
3538 commit_found = 1;
3539 } else {
3540 chk_multi = 1;
3541 struct gw_header *n_header = NULL;
3542 if ((n_header = gw_init_header()) == NULL) {
3543 error = got_error_from_errno("malloc");
3544 goto err;
3546 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3547 entry);
3548 error = got_ref_list(&n_header->refs, gw_trans->repo,
3549 NULL, got_ref_cmp_by_name, NULL);
3550 if (error)
3551 goto err;
3553 error = gw_get_commit(gw_trans, n_header, commit, id);
3554 if (error)
3555 goto err;
3556 got_ref_list_free(&n_header->refs);
3558 if (gw_trans->commit_id != NULL) {
3559 if (strcmp(gw_trans->commit_id,
3560 n_header->commit_id) == 0)
3561 commit_found = 1;
3562 } else
3563 commit_found = 1;
3566 * check for one more commit before breaking,
3567 * so we know whether to navigate through gw_briefs
3568 * gw_commits and gw_summary
3570 if (chk_next && (gw_trans->action == GW_BRIEFS ||
3571 gw_trans->action == GW_COMMITS ||
3572 gw_trans->action == GW_SUMMARY)) {
3573 gw_trans->next_id = strdup(n_header->commit_id);
3574 if (gw_trans->next_id == NULL)
3575 error = got_error_from_errno("strdup");
3576 TAILQ_REMOVE(&gw_trans->gw_headers, n_header,
3577 entry);
3578 goto done;
3582 if (commit_found == 1 && (error || (limit && --limit == 0))) {
3583 if (chk_multi == 0)
3584 break;
3585 chk_next = 1;
3588 done:
3589 if (gw_trans->prev_id == NULL && gw_trans->commit_id != NULL &&
3590 (gw_trans->action == GW_BRIEFS || gw_trans->action == GW_COMMITS)) {
3591 commit_found = 0;
3592 TAILQ_FOREACH_REVERSE(t_header, &gw_trans->gw_headers,
3593 headers, entry) {
3594 if (commit_found == 0 &&
3595 strcmp(gw_trans->commit_id,
3596 t_header->commit_id) != 0)
3597 continue;
3598 else
3599 commit_found = 1;
3600 if (gw_trans->commit_id != NULL &&
3601 strcmp(gw_trans->commit_id,
3602 t_header->commit_id) != 0 &&
3603 (c_cnt == gw_trans->gw_conf->got_max_commits_display
3604 || t_header ==
3605 TAILQ_FIRST(&gw_trans->gw_headers))) {
3606 gw_trans->prev_id = strdup(t_header->commit_id);
3607 if (gw_trans->prev_id == NULL)
3608 error = got_error_from_errno("strdup");
3609 break;
3611 c_cnt++;
3614 err:
3615 if (commit != NULL)
3616 got_object_commit_close(commit);
3617 if (graph)
3618 got_commit_graph_close(graph);
3619 return error;
3622 static const struct got_error *
3623 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header,
3624 struct got_commit_object *commit, struct got_object_id *id)
3626 const struct got_error *error = NULL;
3627 struct got_reflist_entry *re;
3628 struct got_object_id *id2 = NULL;
3629 struct got_object_qid *parent_id;
3630 char *commit_msg = NULL, *commit_msg0;
3632 /*print commit*/
3633 TAILQ_FOREACH(re, &header->refs, entry) {
3634 char *s;
3635 const char *name;
3636 struct got_tag_object *tag = NULL;
3637 struct got_object_id *ref_id;
3638 int cmp;
3640 if (got_ref_is_symbolic(re->ref))
3641 continue;
3643 name = got_ref_get_name(re->ref);
3644 if (strncmp(name, "refs/", 5) == 0)
3645 name += 5;
3646 if (strncmp(name, "got/", 4) == 0)
3647 continue;
3648 if (strncmp(name, "heads/", 6) == 0)
3649 name += 6;
3650 if (strncmp(name, "remotes/", 8) == 0) {
3651 name += 8;
3652 s = strstr(name, "/" GOT_REF_HEAD);
3653 if (s != NULL && s[strlen(s)] == '\0')
3654 continue;
3656 error = got_ref_resolve(&ref_id, gw_trans->repo, re->ref);
3657 if (error)
3658 return error;
3659 if (strncmp(name, "tags/", 5) == 0) {
3660 error = got_object_open_as_tag(&tag, gw_trans->repo,
3661 ref_id);
3662 if (error) {
3663 if (error->code != GOT_ERR_OBJ_TYPE) {
3664 free(ref_id);
3665 continue;
3668 * Ref points at something other
3669 * than a tag.
3671 error = NULL;
3672 tag = NULL;
3675 cmp = got_object_id_cmp(tag ?
3676 got_object_tag_get_object_id(tag) : ref_id, id);
3677 free(ref_id);
3678 if (tag)
3679 got_object_tag_close(tag);
3680 if (cmp != 0)
3681 continue;
3682 s = header->refs_str;
3683 if (asprintf(&header->refs_str, "%s%s%s", s ? s : "",
3684 s ? ", " : "", name) == -1) {
3685 error = got_error_from_errno("asprintf");
3686 free(s);
3687 header->refs_str = NULL;
3688 return error;
3690 free(s);
3693 error = got_object_id_str(&header->commit_id, id);
3694 if (error)
3695 return error;
3697 error = got_object_id_str(&header->tree_id,
3698 got_object_commit_get_tree_id(commit));
3699 if (error)
3700 return error;
3702 if (gw_trans->action == GW_DIFF) {
3703 parent_id = STAILQ_FIRST(
3704 got_object_commit_get_parent_ids(commit));
3705 if (parent_id != NULL) {
3706 id2 = got_object_id_dup(&parent_id->id);
3707 free (parent_id);
3708 error = got_object_id_str(&header->parent_id, id2);
3709 if (error)
3710 return error;
3711 free(id2);
3712 } else {
3713 header->parent_id = strdup("/dev/null");
3714 if (header->parent_id == NULL) {
3715 error = got_error_from_errno("strdup");
3716 return error;
3721 header->committer_time =
3722 got_object_commit_get_committer_time(commit);
3724 header->author =
3725 strdup(got_object_commit_get_author(commit));
3726 if (header->author == NULL) {
3727 error = got_error_from_errno("strdup");
3728 return error;
3730 header->committer =
3731 strdup(got_object_commit_get_committer(commit));
3732 if (header->committer == NULL) {
3733 error = got_error_from_errno("strdup");
3734 return error;
3736 error = got_object_commit_get_logmsg(&commit_msg0, commit);
3737 if (error)
3738 return error;
3740 commit_msg = commit_msg0;
3741 while (*commit_msg == '\n')
3742 commit_msg++;
3744 header->commit_msg = strdup(commit_msg);
3745 if (header->commit_msg == NULL)
3746 error = got_error_from_errno("strdup");
3747 free(commit_msg0);
3748 return error;
3751 static const struct got_error *
3752 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3754 const struct got_error *error = NULL;
3755 char *in_repo_path = NULL;
3756 struct got_object_id *id = NULL;
3757 struct got_reference *ref;
3759 error = got_repo_open(&gw_trans->repo, gw_trans->repo_path, NULL);
3760 if (error)
3761 return error;
3763 if (gw_trans->commit_id == NULL || gw_trans->action == GW_COMMITS ||
3764 gw_trans->action == GW_BRIEFS || gw_trans->action == GW_SUMMARY ||
3765 gw_trans->action == GW_TAGS) {
3766 error = got_ref_open(&ref, gw_trans->repo,
3767 gw_trans->headref, 0);
3768 if (error)
3769 return error;
3771 error = got_ref_resolve(&id, gw_trans->repo, ref);
3772 got_ref_close(ref);
3773 if (error)
3774 return error;
3775 } else {
3776 error = got_ref_open(&ref, gw_trans->repo,
3777 gw_trans->commit_id, 0);
3778 if (error == NULL) {
3779 int obj_type;
3780 error = got_ref_resolve(&id, gw_trans->repo, ref);
3781 got_ref_close(ref);
3782 if (error)
3783 return error;
3784 error = got_object_get_type(&obj_type, gw_trans->repo,
3785 id);
3786 if (error)
3787 goto done;
3788 if (obj_type == GOT_OBJ_TYPE_TAG) {
3789 struct got_tag_object *tag;
3790 error = got_object_open_as_tag(&tag,
3791 gw_trans->repo, id);
3792 if (error)
3793 goto done;
3794 if (got_object_tag_get_object_type(tag) !=
3795 GOT_OBJ_TYPE_COMMIT) {
3796 got_object_tag_close(tag);
3797 error = got_error(GOT_ERR_OBJ_TYPE);
3798 goto done;
3800 free(id);
3801 id = got_object_id_dup(
3802 got_object_tag_get_object_id(tag));
3803 if (id == NULL)
3804 error = got_error_from_errno(
3805 "got_object_id_dup");
3806 got_object_tag_close(tag);
3807 if (error)
3808 goto done;
3809 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3810 error = got_error(GOT_ERR_OBJ_TYPE);
3811 goto done;
3814 error = got_repo_match_object_id_prefix(&id,
3815 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT,
3816 gw_trans->repo);
3817 if (error)
3818 goto done;
3821 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
3822 gw_trans->repo_path);
3823 if (error)
3824 goto done;
3826 if (in_repo_path) {
3827 header->path = strdup(in_repo_path);
3828 if (header->path == NULL) {
3829 error = got_error_from_errno("strdup");
3830 goto done;
3834 error = got_ref_list(&header->refs, gw_trans->repo, NULL,
3835 got_ref_cmp_by_name, NULL);
3836 if (error)
3837 goto done;
3839 error = gw_get_commits(gw_trans, header, limit, id);
3840 done:
3841 got_ref_list_free(&header->refs);
3842 free(id);
3843 free(in_repo_path);
3844 return error;
3847 struct blame_line {
3848 int annotated;
3849 char *id_str;
3850 char *committer;
3851 char datebuf[11]; /* YYYY-MM-DD + NUL */
3854 struct gw_blame_cb_args {
3855 struct blame_line *lines;
3856 int nlines;
3857 int nlines_prec;
3858 int lineno_cur;
3859 off_t *line_offsets;
3860 FILE *f;
3861 struct got_repository *repo;
3862 struct gw_trans *gw_trans;
3865 static const struct got_error *
3866 gw_blame_cb(void *arg, int nlines, int lineno,
3867 struct got_commit_object *commit, struct got_object_id *id)
3869 const struct got_error *err = NULL;
3870 struct gw_blame_cb_args *a = arg;
3871 struct blame_line *bline;
3872 char *line = NULL;
3873 size_t linesize = 0;
3874 off_t offset;
3875 struct tm tm;
3876 time_t committer_time;
3877 enum kcgi_err kerr = KCGI_OK;
3879 if (nlines != a->nlines ||
3880 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3881 return got_error(GOT_ERR_RANGE);
3883 if (lineno == -1)
3884 return NULL; /* no change in this commit */
3886 /* Annotate this line. */
3887 bline = &a->lines[lineno - 1];
3888 if (bline->annotated)
3889 return NULL;
3890 err = got_object_id_str(&bline->id_str, id);
3891 if (err)
3892 return err;
3894 bline->committer = strdup(got_object_commit_get_committer(commit));
3895 if (bline->committer == NULL) {
3896 err = got_error_from_errno("strdup");
3897 goto done;
3900 committer_time = got_object_commit_get_committer_time(commit);
3901 if (gmtime_r(&committer_time, &tm) == NULL)
3902 return got_error_from_errno("gmtime_r");
3903 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3904 &tm) == 0) {
3905 err = got_error(GOT_ERR_NO_SPACE);
3906 goto done;
3908 bline->annotated = 1;
3910 /* Print lines annotated so far. */
3911 bline = &a->lines[a->lineno_cur - 1];
3912 if (!bline->annotated)
3913 goto done;
3915 offset = a->line_offsets[a->lineno_cur - 1];
3916 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3917 err = got_error_from_errno("fseeko");
3918 goto done;
3921 while (bline->annotated) {
3922 char *smallerthan, *at, *nl, *committer;
3923 char *href_diff = NULL;
3924 size_t len;
3926 if (getline(&line, &linesize, a->f) == -1) {
3927 if (ferror(a->f))
3928 err = got_error_from_errno("getline");
3929 break;
3932 committer = bline->committer;
3933 smallerthan = strchr(committer, '<');
3934 if (smallerthan && smallerthan[1] != '\0')
3935 committer = smallerthan + 1;
3936 at = strchr(committer, '@');
3937 if (at)
3938 *at = '\0';
3939 len = strlen(committer);
3940 if (len >= 9)
3941 committer[8] = '\0';
3943 nl = strchr(line, '\n');
3944 if (nl)
3945 *nl = '\0';
3947 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3948 "blame_wrapper", KATTR__MAX);
3949 if (kerr != KCGI_OK)
3950 goto err;
3951 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3952 "blame_number", KATTR__MAX);
3953 if (kerr != KCGI_OK)
3954 goto err;
3955 kerr = khtml_printf(a->gw_trans->gw_html_req, "%.*d",
3956 a->nlines_prec, a->lineno_cur);
3957 if (kerr != KCGI_OK)
3958 goto err;
3959 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3960 if (kerr != KCGI_OK)
3961 goto err;
3963 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3964 "blame_hash", KATTR__MAX);
3965 if (kerr != KCGI_OK)
3966 goto err;
3968 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
3969 a->gw_trans->repo_name, "action", "diff", "commit",
3970 bline->id_str, NULL);
3971 if (href_diff == NULL) {
3972 err = got_error_from_errno("khttp_urlpart");
3973 goto done;
3975 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3976 KATTR_HREF, href_diff, KATTR__MAX);
3977 if (kerr != KCGI_OK)
3978 goto done;
3979 kerr = khtml_printf(a->gw_trans->gw_html_req, "%.8s",
3980 bline->id_str);
3981 if (kerr != KCGI_OK)
3982 goto err;
3983 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3984 if (kerr != KCGI_OK)
3985 goto err;
3987 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3988 "blame_date", KATTR__MAX);
3989 if (kerr != KCGI_OK)
3990 goto err;
3991 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3992 if (kerr != KCGI_OK)
3993 goto err;
3994 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3995 if (kerr != KCGI_OK)
3996 goto err;
3998 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3999 "blame_author", KATTR__MAX);
4000 if (kerr != KCGI_OK)
4001 goto err;
4002 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
4003 if (kerr != KCGI_OK)
4004 goto err;
4005 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
4006 if (kerr != KCGI_OK)
4007 goto err;
4009 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4010 "blame_code", KATTR__MAX);
4011 if (kerr != KCGI_OK)
4012 goto err;
4013 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
4014 if (kerr != KCGI_OK)
4015 goto err;
4016 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
4017 if (kerr != KCGI_OK)
4018 goto err;
4020 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
4021 if (kerr != KCGI_OK)
4022 goto err;
4024 a->lineno_cur++;
4025 bline = &a->lines[a->lineno_cur - 1];
4026 err:
4027 free(href_diff);
4029 done:
4030 free(line);
4031 if (err == NULL && kerr != KCGI_OK)
4032 err = gw_kcgi_error(kerr);
4033 return err;
4036 static const struct got_error *
4037 gw_output_file_blame(struct gw_trans *gw_trans, struct gw_header *header)
4039 const struct got_error *error = NULL;
4040 struct got_object_id *obj_id = NULL;
4041 struct got_object_id *commit_id = NULL;
4042 struct got_commit_object *commit = NULL;
4043 struct got_blob_object *blob = NULL;
4044 char *path = NULL, *in_repo_path = NULL;
4045 struct gw_blame_cb_args bca;
4046 int i, obj_type;
4047 off_t filesize;
4049 memset(&bca, 0, sizeof(bca));
4051 if (asprintf(&path, "%s%s%s",
4052 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4053 gw_trans->repo_folder ? "/" : "",
4054 gw_trans->repo_file) == -1) {
4055 error = got_error_from_errno("asprintf");
4056 goto done;
4059 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path);
4060 if (error)
4061 goto done;
4063 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
4064 GOT_OBJ_TYPE_COMMIT, &header->refs, gw_trans->repo);
4065 if (error)
4066 goto done;
4068 error = got_object_open_as_commit(&commit, gw_trans->repo, commit_id);
4069 if (error)
4070 goto done;
4072 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit,
4073 in_repo_path);
4074 if (error)
4075 goto done;
4077 if (obj_id == NULL) {
4078 error = got_error(GOT_ERR_NO_OBJ);
4079 goto done;
4082 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
4083 if (error)
4084 goto done;
4086 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4087 error = got_error(GOT_ERR_OBJ_TYPE);
4088 goto done;
4091 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
4092 if (error)
4093 goto done;
4095 bca.f = got_opentemp();
4096 if (bca.f == NULL) {
4097 error = got_error_from_errno("got_opentemp");
4098 goto done;
4100 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4101 &bca.line_offsets, bca.f, blob);
4102 if (error || bca.nlines == 0)
4103 goto done;
4105 /* Don't include \n at EOF in the blame line count. */
4106 if (bca.line_offsets[bca.nlines - 1] == filesize)
4107 bca.nlines--;
4109 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4110 if (bca.lines == NULL) {
4111 error = got_error_from_errno("calloc");
4112 goto done;
4114 bca.lineno_cur = 1;
4115 bca.nlines_prec = 0;
4116 i = bca.nlines;
4117 while (i > 0) {
4118 i /= 10;
4119 bca.nlines_prec++;
4121 bca.repo = gw_trans->repo;
4122 bca.gw_trans = gw_trans;
4124 error = got_blame(in_repo_path, commit_id, gw_trans->repo, gw_blame_cb,
4125 &bca, NULL, NULL);
4126 done:
4127 free(in_repo_path);
4128 free(commit_id);
4129 free(obj_id);
4130 free(path);
4132 if (blob) {
4133 free(bca.line_offsets);
4134 for (i = 0; i < bca.nlines; i++) {
4135 struct blame_line *bline = &bca.lines[i];
4136 free(bline->id_str);
4137 free(bline->committer);
4139 free(bca.lines);
4140 if (bca.f && fclose(bca.f) == EOF && error == NULL)
4141 error = got_error_from_errno("fclose");
4143 if (blob)
4144 got_object_blob_close(blob);
4145 if (commit)
4146 got_object_commit_close(commit);
4147 return error;
4150 static const struct got_error *
4151 gw_output_blob_buf(struct gw_trans *gw_trans, struct gw_header *header)
4153 const struct got_error *error = NULL;
4154 struct got_object_id *obj_id = NULL;
4155 struct got_object_id *commit_id = NULL;
4156 struct got_commit_object *commit = NULL;
4157 struct got_blob_object *blob = NULL;
4158 char *path = NULL, *in_repo_path = NULL;
4159 int obj_type, set_mime = 0;
4160 size_t len, hdrlen;
4161 const uint8_t *buf;
4162 enum kcgi_err kerr = KCGI_OK;
4164 if (asprintf(&path, "%s%s%s",
4165 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4166 gw_trans->repo_folder ? "/" : "",
4167 gw_trans->repo_file) == -1) {
4168 error = got_error_from_errno("asprintf");
4169 goto done;
4172 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path);
4173 if (error)
4174 goto done;
4176 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
4177 GOT_OBJ_TYPE_COMMIT, &header->refs, gw_trans->repo);
4178 if (error)
4179 goto done;
4181 error = got_object_open_as_commit(&commit, gw_trans->repo, commit_id);
4182 if (error)
4183 goto done;
4185 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit,
4186 in_repo_path);
4187 if (error)
4188 goto done;
4190 if (obj_id == NULL) {
4191 error = got_error(GOT_ERR_NO_OBJ);
4192 goto done;
4195 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
4196 if (error)
4197 goto done;
4199 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4200 error = got_error(GOT_ERR_OBJ_TYPE);
4201 goto done;
4204 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
4205 if (error)
4206 goto done;
4208 hdrlen = got_object_blob_get_hdrlen(blob);
4209 do {
4210 error = got_object_blob_read_block(&len, blob);
4211 if (error)
4212 goto done;
4213 buf = got_object_blob_get_read_buf(blob);
4216 * Skip blob object header first time around,
4217 * which also contains a zero byte.
4219 buf += hdrlen;
4220 if (set_mime == 0) {
4221 if (isbinary(buf, len - hdrlen))
4222 gw_trans->mime = KMIME_APP_OCTET_STREAM;
4223 else
4224 gw_trans->mime = KMIME_TEXT_PLAIN;
4225 set_mime = 1;
4226 error = gw_display_index(gw_trans);
4227 if (error)
4228 goto done;
4230 kerr = khttp_write(gw_trans->gw_req, buf, len - hdrlen);
4231 if (kerr != KCGI_OK)
4232 goto done;
4233 hdrlen = 0;
4234 } while (len != 0);
4235 done:
4236 free(in_repo_path);
4237 free(commit_id);
4238 free(obj_id);
4239 free(path);
4240 if (blob)
4241 got_object_blob_close(blob);
4242 if (commit)
4243 got_object_commit_close(commit);
4244 if (error == NULL && kerr != KCGI_OK)
4245 error = gw_kcgi_error(kerr);
4246 return error;
4249 static const struct got_error *
4250 gw_output_repo_tree(struct gw_trans *gw_trans, struct gw_header *header)
4252 const struct got_error *error = NULL;
4253 struct got_object_id *tree_id = NULL, *commit_id = NULL;
4254 struct got_tree_object *tree = NULL;
4255 struct got_commit_object *commit = NULL;
4256 char *path = NULL, *in_repo_path = NULL;
4257 char *id_str = NULL;
4258 char *build_folder = NULL;
4259 char *href_blob = NULL, *href_blame = NULL;
4260 const char *class = NULL;
4261 int nentries, i, class_flip = 0;
4262 enum kcgi_err kerr = KCGI_OK;
4264 if (gw_trans->repo_folder != NULL) {
4265 path = strdup(gw_trans->repo_folder);
4266 if (path == NULL) {
4267 error = got_error_from_errno("strdup");
4268 goto done;
4270 } else {
4271 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
4272 gw_trans->repo_path);
4273 if (error)
4274 goto done;
4275 free(path);
4276 path = in_repo_path;
4279 if (gw_trans->commit_id == NULL) {
4280 struct got_reference *head_ref;
4281 error = got_ref_open(&head_ref, gw_trans->repo,
4282 gw_trans->headref, 0);
4283 if (error)
4284 goto done;
4285 error = got_ref_resolve(&commit_id, gw_trans->repo, head_ref);
4286 if (error)
4287 goto done;
4288 got_ref_close(head_ref);
4290 * gw_trans->commit_id was not parsed from the querystring
4291 * we hit this code path from gw_index, where we don't know the
4292 * commit values for the tree link yet, so set
4293 * gw_trans->commit_id here to continue further into the tree
4295 error = got_object_id_str(&gw_trans->commit_id, commit_id);
4296 if (error)
4297 goto done;
4299 } else {
4300 error = got_repo_match_object_id(&commit_id, NULL,
4301 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT, &header->refs,
4302 gw_trans->repo);
4303 if (error)
4304 goto done;
4307 error = got_object_open_as_commit(&commit, gw_trans->repo, commit_id);
4308 if (error)
4309 goto done;
4311 error = got_object_id_by_path(&tree_id, gw_trans->repo, commit,
4312 path);
4313 if (error)
4314 goto done;
4316 error = got_object_open_as_tree(&tree, gw_trans->repo, tree_id);
4317 if (error)
4318 goto done;
4320 nentries = got_object_tree_get_nentries(tree);
4321 for (i = 0; i < nentries; i++) {
4322 struct got_tree_entry *te;
4323 const char *modestr = "";
4324 mode_t mode;
4326 te = got_object_tree_get_entry(tree, i);
4328 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
4329 if (error)
4330 goto done;
4332 mode = got_tree_entry_get_mode(te);
4333 if (got_object_tree_entry_is_submodule(te))
4334 modestr = "$";
4335 else if (S_ISLNK(mode))
4336 modestr = "@";
4337 else if (S_ISDIR(mode))
4338 modestr = "/";
4339 else if (mode & S_IXUSR)
4340 modestr = "*";
4342 if (class_flip == 0) {
4343 class = "back_lightgray";
4344 class_flip = 1;
4345 } else {
4346 class = "back_white";
4347 class_flip = 0;
4350 if (S_ISDIR(mode)) {
4351 if (asprintf(&build_folder, "%s/%s",
4352 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4353 got_tree_entry_get_name(te)) == -1) {
4354 error = got_error_from_errno("asprintf");
4355 goto done;
4358 href_blob = khttp_urlpart(NULL, NULL, "gotweb", "path",
4359 gw_trans->repo_name, "action",
4360 gw_get_action_name(gw_trans), "commit",
4361 gw_trans->commit_id, "folder", build_folder, NULL);
4362 if (href_blob == NULL) {
4363 error = got_error_from_errno("khttp_urlpart");
4364 goto done;
4366 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4367 KATTR_ID, "tree_wrapper", KATTR__MAX);
4368 if (kerr != KCGI_OK)
4369 goto done;
4370 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4371 KATTR_ID, "tree_line", KATTR_CLASS, class,
4372 KATTR__MAX);
4373 if (kerr != KCGI_OK)
4374 goto done;
4375 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4376 KATTR_HREF, href_blob, KATTR_CLASS,
4377 "diff_directory", KATTR__MAX);
4378 if (kerr != KCGI_OK)
4379 goto done;
4380 kerr = khtml_printf(gw_trans->gw_html_req, "%s%s",
4381 got_tree_entry_get_name(te), modestr);
4382 if (kerr != KCGI_OK)
4383 goto done;
4384 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4385 if (kerr != KCGI_OK)
4386 goto done;
4387 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4388 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
4389 KATTR__MAX);
4390 if (kerr != KCGI_OK)
4391 goto done;
4392 kerr = khtml_entity(gw_trans->gw_html_req,
4393 KENTITY_nbsp);
4394 if (kerr != KCGI_OK)
4395 goto done;
4396 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4397 if (kerr != KCGI_OK)
4398 goto done;
4399 } else {
4400 href_blob = khttp_urlpart(NULL, NULL, "gotweb", "path",
4401 gw_trans->repo_name, "action", "blob", "commit",
4402 gw_trans->commit_id, "file",
4403 got_tree_entry_get_name(te), "folder",
4404 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4405 NULL);
4406 if (href_blob == NULL) {
4407 error = got_error_from_errno("khttp_urlpart");
4408 goto done;
4410 href_blame = khttp_urlpart(NULL, NULL, "gotweb", "path",
4411 gw_trans->repo_name, "action", "blame", "commit",
4412 gw_trans->commit_id, "file",
4413 got_tree_entry_get_name(te), "folder",
4414 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4415 NULL);
4416 if (href_blame == NULL) {
4417 error = got_error_from_errno("khttp_urlpart");
4418 goto done;
4420 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4421 KATTR_ID, "tree_wrapper", KATTR__MAX);
4422 if (kerr != KCGI_OK)
4423 goto done;
4424 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4425 KATTR_ID, "tree_line", KATTR_CLASS, class,
4426 KATTR__MAX);
4427 if (kerr != KCGI_OK)
4428 goto done;
4429 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4430 KATTR_HREF, href_blob, KATTR__MAX);
4431 if (kerr != KCGI_OK)
4432 goto done;
4433 kerr = khtml_printf(gw_trans->gw_html_req, "%s%s",
4434 got_tree_entry_get_name(te), modestr);
4435 if (kerr != KCGI_OK)
4436 goto done;
4437 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4438 if (kerr != KCGI_OK)
4439 goto done;
4440 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4441 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
4442 KATTR__MAX);
4443 if (kerr != KCGI_OK)
4444 goto done;
4446 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4447 KATTR_HREF, href_blob, KATTR__MAX);
4448 if (kerr != KCGI_OK)
4449 goto done;
4450 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
4451 if (kerr != KCGI_OK)
4452 goto done;
4453 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4454 if (kerr != KCGI_OK)
4455 goto done;
4457 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4458 if (kerr != KCGI_OK)
4459 goto done;
4461 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4462 KATTR_HREF, href_blame, KATTR__MAX);
4463 if (kerr != KCGI_OK)
4464 goto done;
4465 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
4466 if (kerr != KCGI_OK)
4467 goto done;
4469 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4470 if (kerr != KCGI_OK)
4471 goto done;
4473 free(id_str);
4474 id_str = NULL;
4475 free(href_blob);
4476 href_blob = NULL;
4477 free(build_folder);
4478 build_folder = NULL;
4480 done:
4481 if (tree)
4482 got_object_tree_close(tree);
4483 if (commit)
4484 got_object_commit_close(commit);
4485 free(id_str);
4486 free(href_blob);
4487 free(href_blame);
4488 free(in_repo_path);
4489 free(tree_id);
4490 free(build_folder);
4491 if (error == NULL && kerr != KCGI_OK)
4492 error = gw_kcgi_error(kerr);
4493 return error;
4496 static const struct got_error *
4497 gw_output_repo_heads(struct gw_trans *gw_trans)
4499 const struct got_error *error = NULL;
4500 struct got_reflist_head refs;
4501 struct got_reflist_entry *re;
4502 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
4503 char *href_commits = NULL;
4504 enum kcgi_err kerr = KCGI_OK;
4506 TAILQ_INIT(&refs);
4508 error = got_ref_list(&refs, gw_trans->repo, "refs/heads",
4509 got_ref_cmp_by_name, NULL);
4510 if (error)
4511 goto done;
4513 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4514 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
4515 if (kerr != KCGI_OK)
4516 goto done;
4517 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4518 KATTR_ID, "summary_heads_title", KATTR__MAX);
4519 if (kerr != KCGI_OK)
4520 goto done;
4521 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
4522 if (kerr != KCGI_OK)
4523 goto done;
4524 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4525 if (kerr != KCGI_OK)
4526 goto done;
4527 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4528 KATTR_ID, "summary_heads_content", KATTR__MAX);
4529 if (kerr != KCGI_OK)
4530 goto done;
4532 TAILQ_FOREACH(re, &refs, entry) {
4533 const char *refname;
4535 if (got_ref_is_symbolic(re->ref))
4536 continue;
4538 refname = got_ref_get_name(re->ref);
4539 if (strncmp(refname, "refs/heads/", 11) != 0)
4540 continue;
4542 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
4543 refname, TM_DIFF);
4544 if (error)
4545 goto done;
4547 if (strncmp(refname, "refs/heads/", 11) == 0)
4548 refname += 11;
4550 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4551 KATTR_ID, "heads_wrapper", KATTR__MAX);
4552 if (kerr != KCGI_OK)
4553 goto done;
4554 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4555 KATTR_ID, "heads_age", KATTR__MAX);
4556 if (kerr != KCGI_OK)
4557 goto done;
4558 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
4559 if (kerr != KCGI_OK)
4560 goto done;
4561 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4562 if (kerr != KCGI_OK)
4563 goto done;
4564 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4565 KATTR_ID, "heads_space", KATTR__MAX);
4566 if (kerr != KCGI_OK)
4567 goto done;
4568 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
4569 if (kerr != KCGI_OK)
4570 goto done;
4571 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4572 if (kerr != KCGI_OK)
4573 goto done;
4574 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4575 KATTR_ID, "head", KATTR__MAX);
4576 if (kerr != KCGI_OK)
4577 goto done;
4579 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
4580 gw_trans->repo_name, "action", "summary", "headref",
4581 refname, NULL);
4582 if (href_summary == NULL) {
4583 error = got_error_from_errno("khttp_urlpart");
4584 goto done;
4586 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4587 href_summary, KATTR__MAX);
4588 kerr = khtml_puts(gw_trans->gw_html_req, refname);
4589 if (kerr != KCGI_OK)
4590 goto done;
4591 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4592 if (kerr != KCGI_OK)
4593 goto done;
4595 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4596 "navs_wrapper", KATTR__MAX);
4597 if (kerr != KCGI_OK)
4598 goto done;
4599 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4600 "navs", KATTR__MAX);
4601 if (kerr != KCGI_OK)
4602 goto done;
4604 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4605 href_summary, KATTR__MAX);
4606 if (kerr != KCGI_OK)
4607 goto done;
4608 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4609 if (kerr != KCGI_OK)
4610 goto done;
4611 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4612 if (kerr != KCGI_OK)
4613 goto done;
4615 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4616 if (kerr != KCGI_OK)
4617 goto done;
4619 href_briefs = khttp_urlpart(NULL, NULL, "gotweb", "path",
4620 gw_trans->repo_name, "action", "briefs", "headref",
4621 refname, NULL);
4622 if (href_briefs == NULL) {
4623 error = got_error_from_errno("khttp_urlpart");
4624 goto done;
4626 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4627 href_briefs, KATTR__MAX);
4628 if (kerr != KCGI_OK)
4629 goto done;
4630 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4631 if (kerr != KCGI_OK)
4632 goto done;
4633 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4634 if (kerr != KCGI_OK)
4635 goto done;
4637 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4638 if (kerr != KCGI_OK)
4639 goto done;
4641 href_commits = khttp_urlpart(NULL, NULL, "gotweb", "path",
4642 gw_trans->repo_name, "action", "commits", "headref",
4643 refname, NULL);
4644 if (href_commits == NULL) {
4645 error = got_error_from_errno("khttp_urlpart");
4646 goto done;
4648 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4649 href_commits, KATTR__MAX);
4650 if (kerr != KCGI_OK)
4651 goto done;
4652 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4653 if (kerr != KCGI_OK)
4654 goto done;
4655 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4656 if (kerr != KCGI_OK)
4657 goto done;
4659 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4660 "dotted_line", KATTR__MAX);
4661 if (kerr != KCGI_OK)
4662 goto done;
4663 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4664 if (kerr != KCGI_OK)
4665 goto done;
4666 free(href_summary);
4667 href_summary = NULL;
4668 free(href_briefs);
4669 href_briefs = NULL;
4670 free(href_commits);
4671 href_commits = NULL;
4673 done:
4674 got_ref_list_free(&refs);
4675 free(href_summary);
4676 free(href_briefs);
4677 free(href_commits);
4678 return error;
4681 static const struct got_error *
4682 gw_output_site_link(struct gw_trans *gw_trans)
4684 const struct got_error *error = NULL;
4685 char *href_summary = NULL;
4686 enum kcgi_err kerr = KCGI_OK;
4688 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4689 "site_link", KATTR__MAX);
4690 if (kerr != KCGI_OK)
4691 goto done;
4692 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4693 KATTR__MAX);
4694 if (kerr != KCGI_OK)
4695 goto done;
4696 kerr = khtml_puts(gw_trans->gw_html_req,
4697 gw_trans->gw_conf->got_site_link);
4698 if (kerr != KCGI_OK)
4699 goto done;
4700 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4701 if (kerr != KCGI_OK)
4702 goto done;
4704 if (gw_trans->repo_name != NULL) {
4705 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4706 if (kerr != KCGI_OK)
4707 goto done;
4709 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
4710 gw_trans->repo_name, "action", "summary", NULL);
4711 if (href_summary == NULL) {
4712 error = got_error_from_errno("khttp_urlpart");
4713 goto done;
4715 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4716 href_summary, KATTR__MAX);
4717 if (kerr != KCGI_OK)
4718 goto done;
4719 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4720 if (kerr != KCGI_OK)
4721 goto done;
4722 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4723 if (kerr != KCGI_OK)
4724 goto done;
4725 kerr = khtml_printf(gw_trans->gw_html_req, " / %s",
4726 gw_get_action_name(gw_trans));
4727 if (kerr != KCGI_OK)
4728 goto done;
4731 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4732 if (kerr != KCGI_OK)
4733 goto done;
4734 done:
4735 free(href_summary);
4736 if (error == NULL && kerr != KCGI_OK)
4737 error = gw_kcgi_error(kerr);
4738 return error;
4741 static const struct got_error *
4742 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4744 const struct got_error *error = NULL;
4745 char *color = NULL;
4746 enum kcgi_err kerr = KCGI_OK;
4748 if (strncmp(buf, "-", 1) == 0)
4749 color = "diff_minus";
4750 else if (strncmp(buf, "+", 1) == 0)
4751 color = "diff_plus";
4752 else if (strncmp(buf, "@@", 2) == 0)
4753 color = "diff_chunk_header";
4754 else if (strncmp(buf, "@@", 2) == 0)
4755 color = "diff_chunk_header";
4756 else if (strncmp(buf, "commit +", 8) == 0)
4757 color = "diff_meta";
4758 else if (strncmp(buf, "commit -", 8) == 0)
4759 color = "diff_meta";
4760 else if (strncmp(buf, "blob +", 6) == 0)
4761 color = "diff_meta";
4762 else if (strncmp(buf, "blob -", 6) == 0)
4763 color = "diff_meta";
4764 else if (strncmp(buf, "file +", 6) == 0)
4765 color = "diff_meta";
4766 else if (strncmp(buf, "file -", 6) == 0)
4767 color = "diff_meta";
4768 else if (strncmp(buf, "from:", 5) == 0)
4769 color = "diff_author";
4770 else if (strncmp(buf, "via:", 4) == 0)
4771 color = "diff_author";
4772 else if (strncmp(buf, "date:", 5) == 0)
4773 color = "diff_date";
4774 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4775 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4776 if (error == NULL && kerr != KCGI_OK)
4777 error = gw_kcgi_error(kerr);
4778 return error;
4781 int
4782 main(int argc, char *argv[])
4784 const struct got_error *error = NULL, *error2 = NULL;
4785 struct gw_trans *gw_trans;
4786 struct gw_dir *dir = NULL, *tdir;
4787 const char *page = "index";
4788 enum kcgi_err kerr = KCGI_OK;
4790 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4791 errx(1, "malloc");
4793 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4794 errx(1, "malloc");
4796 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4797 errx(1, "malloc");
4799 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4800 errx(1, "malloc");
4802 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4803 if (kerr != KCGI_OK) {
4804 error = gw_kcgi_error(kerr);
4805 goto done;
4808 TAILQ_INIT(&gw_trans->gw_dirs);
4809 TAILQ_INIT(&gw_trans->gw_headers);
4811 gw_trans->action = -1;
4812 gw_trans->page = 0;
4813 gw_trans->repos_total = 0;
4814 gw_trans->repo_path = NULL;
4815 gw_trans->commit_id = NULL;
4816 gw_trans->next_id = NULL;
4817 gw_trans->prev_id = NULL;
4818 gw_trans->headref = GOT_REF_HEAD;
4819 gw_trans->mime = KMIME_TEXT_HTML;
4820 gw_trans->gw_tmpl->key = gw_templs;
4821 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4822 gw_trans->gw_tmpl->arg = gw_trans;
4823 gw_trans->gw_tmpl->cb = gw_template;
4825 error = parse_gotweb_config(&gw_trans->gw_conf, GOTWEB_CONF);
4826 if (error)
4827 goto done;
4829 error = gw_parse_querystring(gw_trans);
4830 if (error)
4831 goto done;
4833 if (gw_trans->action == GW_BLOB)
4834 error = gw_blob(gw_trans);
4835 else
4836 error = gw_display_index(gw_trans);
4837 done:
4838 if (gw_trans->repo) {
4839 const struct got_error *close_err;
4840 close_err = got_repo_close(gw_trans->repo);
4841 if (error == NULL)
4842 error = close_err;
4844 if (error) {
4845 gw_trans->error = error;
4846 gw_trans->action = GW_ERR;
4847 error2 = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
4848 if (error2)
4849 goto cleanup; /* we can't display an error page */
4850 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
4851 if (kerr != KCGI_OK)
4852 goto cleanup; /* we can't display an error page */
4853 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
4854 gw_query_funcs[gw_trans->action].template);
4855 if (kerr != KCGI_OK) {
4856 khtml_close(gw_trans->gw_html_req);
4857 goto cleanup; /* we can't display an error page */
4861 cleanup:
4862 free(gw_trans->gw_conf->got_repos_path);
4863 free(gw_trans->gw_conf->got_www_path);
4864 free(gw_trans->gw_conf->got_site_name);
4865 free(gw_trans->gw_conf->got_site_owner);
4866 free(gw_trans->gw_conf->got_site_link);
4867 free(gw_trans->gw_conf->got_logo);
4868 free(gw_trans->gw_conf->got_logo_url);
4869 free(gw_trans->gw_conf);
4870 free(gw_trans->commit_id);
4871 free(gw_trans->next_id);
4872 free(gw_trans->prev_id);
4873 free(gw_trans->repo_path);
4874 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4875 free(dir->name);
4876 free(dir->description);
4877 free(dir->age);
4878 free(dir->url);
4879 free(dir->path);
4880 free(dir);
4883 khttp_free(gw_trans->gw_req);
4884 return 0;