Blob


1 /*
2 * Copyright (c) 2019, 2020 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/queue.h>
19 #include <sys/stat.h>
20 #include <sys/types.h>
22 #include <ctype.h>
23 #include <dirent.h>
24 #include <err.h>
25 #include <errno.h>
26 #include <regex.h>
27 #include <stdarg.h>
28 #include <stdint.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
34 #include <got_object.h>
35 #include <got_reference.h>
36 #include <got_repository.h>
37 #include <got_path.h>
38 #include <got_cancel.h>
39 #include <got_worktree.h>
40 #include <got_diff.h>
41 #include <got_commit_graph.h>
42 #include <got_blame.h>
43 #include <got_privsep.h>
44 #include <got_opentemp.h>
46 #include <kcgi.h>
47 #include <kcgihtml.h>
49 #include "gotweb.h"
51 #ifndef nitems
52 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
53 #endif
55 struct gw_trans {
56 TAILQ_HEAD(headers, gw_header) gw_headers;
57 TAILQ_HEAD(dirs, gw_dir) gw_dirs;
58 struct got_repository *repo;
59 struct gw_dir *gw_dir;
60 struct gotweb_conf *gw_conf;
61 struct ktemplate *gw_tmpl;
62 struct khtmlreq *gw_html_req;
63 struct kreq *gw_req;
64 const struct got_error *error;
65 const char *repo_name;
66 char *repo_path;
67 char *commit_id;
68 char *next_id;
69 char *next_prev_id;
70 char *prev_id;
71 char *prev_prev_id;
72 const char *repo_file;
73 char *repo_folder;
74 const char *headref;
75 unsigned int action;
76 unsigned int page;
77 unsigned int repos_total;
78 enum kmime mime;
79 };
81 struct gw_header {
82 TAILQ_ENTRY(gw_header) entry;
83 struct got_reflist_head refs;
84 char *path;
86 char *refs_str;
87 char *commit_id; /* id_str1 */
88 char *parent_id; /* id_str2 */
89 char *tree_id;
90 char *author;
91 char *committer;
92 char *commit_msg;
93 time_t committer_time;
94 };
96 struct gw_dir {
97 TAILQ_ENTRY(gw_dir) entry;
98 char *name;
99 char *owner;
100 char *description;
101 char *url;
102 char *age;
103 char *path;
104 };
106 enum gw_key {
107 KEY_ACTION,
108 KEY_COMMIT_ID,
109 KEY_FILE,
110 KEY_FOLDER,
111 KEY_HEADREF,
112 KEY_PAGE,
113 KEY_PATH,
114 KEY_PREV_ID,
115 KEY_PREV_PREV_ID,
116 KEY__ZMAX
117 };
119 enum gw_tmpl {
120 TEMPL_CONTENT,
121 TEMPL_HEAD,
122 TEMPL_HEADER,
123 TEMPL_SEARCH,
124 TEMPL_SITEPATH,
125 TEMPL_SITEOWNER,
126 TEMPL_TITLE,
127 TEMPL__MAX
128 };
130 enum gw_ref_tm {
131 TM_DIFF,
132 TM_LONG,
133 };
135 enum gw_tags_type {
136 TAGBRIEF,
137 TAGFULL,
138 };
140 static const char *const gw_templs[TEMPL__MAX] = {
141 "content",
142 "head",
143 "header",
144 "search",
145 "sitepath",
146 "siteowner",
147 "title",
148 };
150 static const struct kvalid gw_keys[KEY__ZMAX] = {
151 { kvalid_stringne, "action" },
152 { kvalid_stringne, "commit" },
153 { kvalid_stringne, "file" },
154 { kvalid_stringne, "folder" },
155 { kvalid_stringne, "headref" },
156 { kvalid_int, "page" },
157 { kvalid_stringne, "path" },
158 { kvalid_stringne, "prev" },
159 { kvalid_stringne, "prev_prev" },
160 };
162 static struct gw_header *gw_init_header(void);
164 static void gw_free_header(struct gw_header *);
166 static int gw_template(size_t, void *);
168 static const struct got_error *gw_error(struct gw_trans *);
169 static const struct got_error *gw_init_gw_dir(struct gw_dir **, const char *);
170 static const struct got_error *gw_get_repo_description(char **,
171 struct gw_trans *, char *);
172 static const struct got_error *gw_get_repo_owner(char **, struct gw_trans *,
173 char *);
174 static const struct got_error *gw_get_time_str(char **, time_t, int);
175 static const struct got_error *gw_get_repo_age(char **, struct gw_trans *,
176 char *, const char *, int);
177 static const struct got_error *gw_output_file_blame(struct gw_trans *);
178 static const struct got_error *gw_output_blob_buf(struct gw_trans *);
179 static const struct got_error *gw_output_repo_tree(struct gw_trans *);
180 static const struct got_error *gw_output_diff(struct gw_trans *,
181 struct gw_header *);
182 static const struct got_error *gw_output_repo_tags(struct gw_trans *,
183 struct gw_header *, int, int);
184 static const struct got_error *gw_output_repo_heads(struct gw_trans *);
185 static const struct got_error *gw_output_site_link(struct gw_trans *);
186 static const struct got_error *gw_get_clone_url(char **, struct gw_trans *,
187 char *);
188 static const struct got_error *gw_colordiff_line(struct gw_trans *, char *);
190 static const struct got_error *gw_gen_commit_header(struct gw_trans *, char *,
191 char*);
192 static const struct got_error *gw_gen_diff_header(struct gw_trans *, char *,
193 char*);
194 static const struct got_error *gw_gen_author_header(struct gw_trans *,
195 const char *);
196 static const struct got_error *gw_gen_age_header(struct gw_trans *,
197 const char *);
198 static const struct got_error *gw_gen_committer_header(struct gw_trans *,
199 const char *);
200 static const struct got_error *gw_gen_commit_msg_header(struct gw_trans*,
201 char *);
202 static const struct got_error *gw_gen_tree_header(struct gw_trans *, char *);
203 static const struct got_error *gw_display_open(struct gw_trans *, enum khttp,
204 enum kmime);
205 static const struct got_error *gw_display_index(struct gw_trans *);
206 static const struct got_error *gw_get_header(struct gw_trans *,
207 struct gw_header *, int);
208 static const struct got_error *gw_get_commits(struct gw_trans *,
209 struct gw_header *, int,
210 struct got_object_id *);
211 static const struct got_error *gw_get_commit(struct gw_trans *,
212 struct gw_header *,
213 struct got_commit_object *,
214 struct got_object_id *);
215 static const struct got_error *gw_apply_unveil(const char *);
216 static const struct got_error *gw_blame_cb(void *, int, int,
217 struct got_object_id *);
218 static const struct got_error *gw_load_got_paths(struct gw_trans *);
219 static const struct got_error *gw_load_got_path(struct gw_trans *,
220 struct gw_dir *);
221 static const struct got_error *gw_parse_querystring(struct gw_trans *);
222 static const struct got_error *gw_blame(struct gw_trans *);
223 static const struct got_error *gw_blob(struct gw_trans *);
224 static const struct got_error *gw_diff(struct gw_trans *);
225 static const struct got_error *gw_index(struct gw_trans *);
226 static const struct got_error *gw_commits(struct gw_trans *);
227 static const struct got_error *gw_briefs(struct gw_trans *);
228 static const struct got_error *gw_summary(struct gw_trans *);
229 static const struct got_error *gw_tree(struct gw_trans *);
230 static const struct got_error *gw_tag(struct gw_trans *);
231 static const struct got_error *gw_tags(struct gw_trans *);
233 struct gw_query_action {
234 unsigned int func_id;
235 const char *func_name;
236 const struct got_error *(*func_main)(struct gw_trans *);
237 char *template;
238 };
240 enum gw_query_actions {
241 GW_BLAME,
242 GW_BLOB,
243 GW_BRIEFS,
244 GW_COMMITS,
245 GW_DIFF,
246 GW_ERR,
247 GW_INDEX,
248 GW_SUMMARY,
249 GW_TAG,
250 GW_TAGS,
251 GW_TREE,
252 };
254 static struct gw_query_action gw_query_funcs[] = {
255 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
256 { GW_BLOB, "blob", NULL, NULL },
257 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
258 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
259 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
260 { GW_ERR, "error", gw_error, "gw_tmpl/err.tmpl" },
261 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
262 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
263 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
264 { GW_TAGS, "tags", gw_tags, "gw_tmpl/tags.tmpl" },
265 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
266 };
268 static const char *
269 gw_get_action_name(struct gw_trans *gw_trans)
271 return gw_query_funcs[gw_trans->action].func_name;
274 static const struct got_error *
275 gw_kcgi_error(enum kcgi_err kerr)
277 if (kerr == KCGI_OK)
278 return NULL;
280 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
281 return got_error(GOT_ERR_CANCELLED);
283 if (kerr == KCGI_ENOMEM)
284 return got_error_set_errno(ENOMEM,
285 kcgi_strerror(kerr));
287 if (kerr == KCGI_ENFILE)
288 return got_error_set_errno(ENFILE,
289 kcgi_strerror(kerr));
291 if (kerr == KCGI_EAGAIN)
292 return got_error_set_errno(EAGAIN,
293 kcgi_strerror(kerr));
295 if (kerr == KCGI_FORM)
296 return got_error_msg(GOT_ERR_IO,
297 kcgi_strerror(kerr));
299 return got_error_from_errno(kcgi_strerror(kerr));
302 static const struct got_error *
303 gw_apply_unveil(const char *repo_path)
305 const struct got_error *err;
307 if (repo_path && unveil(repo_path, "r") != 0)
308 return got_error_from_errno2("unveil", repo_path);
310 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
311 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
313 err = got_privsep_unveil_exec_helpers();
314 if (err != NULL)
315 return err;
317 if (unveil(NULL, NULL) != 0)
318 return got_error_from_errno("unveil");
320 return NULL;
323 static int
324 isbinary(const uint8_t *buf, size_t n)
326 size_t i;
328 for (i = 0; i < n; i++)
329 if (buf[i] == 0)
330 return 1;
331 return 0;
334 static const struct got_error *
335 gw_blame(struct gw_trans *gw_trans)
337 const struct got_error *error = NULL;
338 struct gw_header *header = NULL;
339 char *age = NULL;
340 enum kcgi_err kerr = KCGI_OK;
342 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
343 NULL) == -1)
344 return got_error_from_errno("pledge");
346 if ((header = gw_init_header()) == NULL)
347 return got_error_from_errno("malloc");
349 error = gw_apply_unveil(gw_trans->gw_dir->path);
350 if (error)
351 goto done;
353 /* check querystring */
354 if (gw_trans->repo_file == NULL) {
355 error = got_error_msg(GOT_ERR_QUERYSTRING,
356 "file required in querystring");
357 goto done;
359 if (gw_trans->commit_id == NULL) {
360 error = got_error_msg(GOT_ERR_QUERYSTRING,
361 "commit required in querystring");
362 goto done;
365 error = gw_get_header(gw_trans, header, 1);
366 if (error)
367 goto done;
368 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
369 "blame_header_wrapper", KATTR__MAX);
370 if (kerr != KCGI_OK)
371 goto done;
372 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
373 "blame_header", KATTR__MAX);
374 if (kerr != KCGI_OK)
375 goto done;
376 error = gw_get_time_str(&age, header->committer_time,
377 TM_LONG);
378 if (error)
379 goto done;
380 error = gw_gen_age_header(gw_trans, age ?age : "");
381 if (error)
382 goto done;
383 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
384 if (error)
385 goto done;
386 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
387 if (kerr != KCGI_OK)
388 goto done;
389 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
390 "dotted_line", KATTR__MAX);
391 if (kerr != KCGI_OK)
392 goto done;
393 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
394 if (kerr != KCGI_OK)
395 goto done;
397 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
398 "blame", KATTR__MAX);
399 if (kerr != KCGI_OK)
400 goto done;
401 error = gw_output_file_blame(gw_trans);
402 if (error)
403 goto done;
404 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
405 done:
406 gw_free_header(header);
407 if (error == NULL && kerr != KCGI_OK)
408 error = gw_kcgi_error(kerr);
409 return error;
412 static const struct got_error *
413 gw_blob(struct gw_trans *gw_trans)
415 const struct got_error *error = NULL, *err = NULL;
416 struct gw_header *header = NULL;
417 enum kcgi_err kerr = KCGI_OK;
419 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
420 NULL) == -1)
421 return got_error_from_errno("pledge");
423 if ((header = gw_init_header()) == NULL)
424 return got_error_from_errno("malloc");
426 error = gw_apply_unveil(gw_trans->gw_dir->path);
427 if (error)
428 goto done;
430 /* check querystring */
431 if (gw_trans->repo_file == NULL) {
432 error = got_error_msg(GOT_ERR_QUERYSTRING,
433 "file required in querystring");
434 goto done;
436 if (gw_trans->commit_id == NULL) {
437 error = got_error_msg(GOT_ERR_QUERYSTRING,
438 "commit required in querystring");
439 goto done;
441 error = gw_get_header(gw_trans, header, 1);
442 if (error)
443 goto done;
445 error = gw_output_blob_buf(gw_trans);
446 done:
448 if (error) {
449 gw_trans->mime = KMIME_TEXT_PLAIN;
450 err = gw_display_index(gw_trans);
451 if (err) {
452 error = err;
453 goto errored;
455 kerr = khttp_puts(gw_trans->gw_req, error->msg);
457 errored:
458 gw_free_header(header);
459 if (error == NULL && kerr != KCGI_OK)
460 error = gw_kcgi_error(kerr);
461 return error;
464 static const struct got_error *
465 gw_diff(struct gw_trans *gw_trans)
467 const struct got_error *error = NULL;
468 struct gw_header *header = NULL;
469 char *age = NULL;
470 enum kcgi_err kerr = KCGI_OK;
472 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
473 NULL) == -1)
474 return got_error_from_errno("pledge");
476 if ((header = gw_init_header()) == NULL)
477 return got_error_from_errno("malloc");
479 error = gw_apply_unveil(gw_trans->gw_dir->path);
480 if (error)
481 goto done;
483 error = gw_get_header(gw_trans, header, 1);
484 if (error)
485 goto done;
487 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
488 "diff_header_wrapper", KATTR__MAX);
489 if (kerr != KCGI_OK)
490 goto done;
491 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
492 "diff_header", KATTR__MAX);
493 if (kerr != KCGI_OK)
494 goto done;
495 error = gw_gen_diff_header(gw_trans, header->parent_id,
496 header->commit_id);
497 if (error)
498 goto done;
499 error = gw_gen_commit_header(gw_trans, header->commit_id,
500 header->refs_str);
501 if (error)
502 goto done;
503 error = gw_gen_tree_header(gw_trans, header->tree_id);
504 if (error)
505 goto done;
506 error = gw_gen_author_header(gw_trans, header->author);
507 if (error)
508 goto done;
509 error = gw_gen_committer_header(gw_trans, header->author);
510 if (error)
511 goto done;
512 error = gw_get_time_str(&age, header->committer_time,
513 TM_LONG);
514 if (error)
515 goto done;
516 error = gw_gen_age_header(gw_trans, age ?age : "");
517 if (error)
518 goto done;
519 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
520 if (error)
521 goto done;
522 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
523 if (kerr != KCGI_OK)
524 goto done;
525 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
526 "dotted_line", KATTR__MAX);
527 if (kerr != KCGI_OK)
528 goto done;
529 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
530 if (kerr != KCGI_OK)
531 goto done;
533 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
534 "diff", KATTR__MAX);
535 if (kerr != KCGI_OK)
536 goto done;
537 error = gw_output_diff(gw_trans, header);
538 if (error)
539 goto done;
541 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
542 done:
543 gw_free_header(header);
544 free(age);
545 if (error == NULL && kerr != KCGI_OK)
546 error = gw_kcgi_error(kerr);
547 return error;
550 static const struct got_error *
551 gw_index(struct gw_trans *gw_trans)
553 const struct got_error *error = NULL;
554 struct gw_dir *gw_dir = NULL;
555 char *href_next = NULL, *href_prev = NULL, *href_summary = NULL;
556 char *href_briefs = NULL, *href_commits = NULL, *href_tree = NULL;
557 char *href_tags = NULL;
558 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
559 enum kcgi_err kerr = KCGI_OK;
561 if (pledge("stdio rpath proc exec sendfd unveil",
562 NULL) == -1) {
563 error = got_error_from_errno("pledge");
564 return error;
567 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path);
568 if (error)
569 return error;
571 error = gw_load_got_paths(gw_trans);
572 if (error)
573 return error;
575 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
576 "index_header", KATTR__MAX);
577 if (kerr != KCGI_OK)
578 return gw_kcgi_error(kerr);
579 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
580 "index_header_project", KATTR__MAX);
581 if (kerr != KCGI_OK)
582 return gw_kcgi_error(kerr);
583 kerr = khtml_puts(gw_trans->gw_html_req, "Project");
584 if (kerr != KCGI_OK)
585 return gw_kcgi_error(kerr);
586 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
587 if (kerr != KCGI_OK)
588 return gw_kcgi_error(kerr);
590 if (gw_trans->gw_conf->got_show_repo_description) {
591 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
592 "index_header_description", KATTR__MAX);
593 if (kerr != KCGI_OK)
594 return gw_kcgi_error(kerr);
595 kerr = khtml_puts(gw_trans->gw_html_req, "Description");
596 if (kerr != KCGI_OK)
597 return gw_kcgi_error(kerr);
598 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
599 if (kerr != KCGI_OK)
600 return gw_kcgi_error(kerr);
603 if (gw_trans->gw_conf->got_show_repo_owner) {
604 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
605 "index_header_owner", KATTR__MAX);
606 if (kerr != KCGI_OK)
607 return gw_kcgi_error(kerr);
608 kerr = khtml_puts(gw_trans->gw_html_req, "Owner");
609 if (kerr != KCGI_OK)
610 return gw_kcgi_error(kerr);
611 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
612 if (kerr != KCGI_OK)
613 return gw_kcgi_error(kerr);
616 if (gw_trans->gw_conf->got_show_repo_age) {
617 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
618 "index_header_age", KATTR__MAX);
619 if (kerr != KCGI_OK)
620 return gw_kcgi_error(kerr);
621 kerr = khtml_puts(gw_trans->gw_html_req, "Last Change");
622 if (kerr != KCGI_OK)
623 return gw_kcgi_error(kerr);
624 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
625 if (kerr != KCGI_OK)
626 return gw_kcgi_error(kerr);
629 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
630 if (kerr != KCGI_OK)
631 return gw_kcgi_error(kerr);
633 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
634 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
635 "index_wrapper", KATTR__MAX);
636 if (kerr != KCGI_OK)
637 return gw_kcgi_error(kerr);
638 kerr = khtml_printf(gw_trans->gw_html_req,
639 "No repositories found in %s",
640 gw_trans->gw_conf->got_repos_path);
641 if (kerr != KCGI_OK)
642 return gw_kcgi_error(kerr);
643 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
644 if (kerr != KCGI_OK)
645 return gw_kcgi_error(kerr);
646 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
647 "dotted_line", KATTR__MAX);
648 if (kerr != KCGI_OK)
649 return gw_kcgi_error(kerr);
650 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
651 if (kerr != KCGI_OK)
652 return gw_kcgi_error(kerr);
653 return error;
656 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
657 dir_c++;
659 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
660 if (gw_trans->page > 0 && (gw_trans->page *
661 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
662 prev_disp++;
663 continue;
666 prev_disp++;
668 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
669 "index_wrapper", KATTR__MAX);
670 if (kerr != KCGI_OK)
671 goto done;
673 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
674 gw_dir->name, "action", "summary", NULL);
675 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
676 "index_project", KATTR__MAX);
677 if (kerr != KCGI_OK)
678 goto done;
679 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
680 href_summary, KATTR__MAX);
681 if (kerr != KCGI_OK)
682 goto done;
683 kerr = khtml_puts(gw_trans->gw_html_req, gw_dir->name);
684 if (kerr != KCGI_OK)
685 goto done;
686 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
687 if (kerr != KCGI_OK)
688 goto done;
689 if (gw_trans->gw_conf->got_show_repo_description) {
690 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
691 KATTR_ID, "index_project_description", KATTR__MAX);
692 if (kerr != KCGI_OK)
693 goto done;
694 kerr = khtml_puts(gw_trans->gw_html_req,
695 gw_dir->description ? gw_dir->description : "");
696 if (kerr != KCGI_OK)
697 goto done;
698 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
699 if (kerr != KCGI_OK)
700 goto done;
702 if (gw_trans->gw_conf->got_show_repo_owner) {
703 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
704 KATTR_ID, "index_project_owner", KATTR__MAX);
705 if (kerr != KCGI_OK)
706 goto done;
707 kerr = khtml_puts(gw_trans->gw_html_req,
708 gw_dir->owner ? gw_dir->owner : "");
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_age) {
716 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
717 KATTR_ID, "index_project_age", KATTR__MAX);
718 if (kerr != KCGI_OK)
719 goto done;
720 kerr = khtml_puts(gw_trans->gw_html_req,
721 gw_dir->age ? gw_dir->age : "");
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;
729 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
730 "navs_wrapper", KATTR__MAX);
731 if (kerr != KCGI_OK)
732 goto done;
733 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
734 "navs", KATTR__MAX);
735 if (kerr != KCGI_OK)
736 goto done;
738 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
739 href_summary, KATTR__MAX);
740 if (kerr != KCGI_OK)
741 goto done;
742 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
743 if (kerr != KCGI_OK)
744 goto done;
745 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
746 if (kerr != KCGI_OK)
747 goto done;
749 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
750 if (kerr != KCGI_OK)
751 goto done;
753 href_briefs = khttp_urlpart(NULL, NULL, "gotweb", "path",
754 gw_dir->name, "action", "briefs", NULL);
755 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
756 href_briefs, KATTR__MAX);
757 if (kerr != KCGI_OK)
758 goto done;
759 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
760 if (kerr != KCGI_OK)
761 goto done;
762 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
763 if (kerr != KCGI_OK)
764 error = gw_kcgi_error(kerr);
766 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
767 if (kerr != KCGI_OK)
768 goto done;
770 href_commits = khttp_urlpart(NULL, NULL, "gotweb", "path",
771 gw_dir->name, "action", "commits", NULL);
772 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
773 href_commits, KATTR__MAX);
774 if (kerr != KCGI_OK)
775 goto done;
776 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
777 if (kerr != KCGI_OK)
778 goto done;
779 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
780 if (kerr != KCGI_OK)
781 goto done;
783 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
784 if (kerr != KCGI_OK)
785 goto done;
787 href_tags = khttp_urlpart(NULL, NULL, "gotweb", "path",
788 gw_dir->name, "action", "tags", NULL);
789 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
790 href_tags, KATTR__MAX);
791 if (kerr != KCGI_OK)
792 goto done;
793 kerr = khtml_puts(gw_trans->gw_html_req, "tags");
794 if (kerr != KCGI_OK)
795 goto done;
796 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
797 if (kerr != KCGI_OK)
798 goto done;
800 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
801 if (kerr != KCGI_OK)
802 goto done;
804 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
805 gw_dir->name, "action", "tree", NULL);
806 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
807 href_tree, KATTR__MAX);
808 if (kerr != KCGI_OK)
809 goto done;
810 kerr = khtml_puts(gw_trans->gw_html_req, "tree");
811 if (kerr != KCGI_OK)
812 goto done;
814 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
815 if (kerr != KCGI_OK)
816 goto done;
817 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
818 "dotted_line", KATTR__MAX);
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 free(href_summary);
826 href_summary = NULL;
827 free(href_briefs);
828 href_briefs = NULL;
829 free(href_commits);
830 href_commits = NULL;
831 free(href_tags);
832 href_tags = NULL;
833 free(href_tree);
834 href_tree = NULL;
836 if (gw_trans->gw_conf->got_max_repos_display == 0)
837 continue;
839 if ((next_disp == gw_trans->gw_conf->got_max_repos_display) ||
840 ((gw_trans->gw_conf->got_max_repos_display > 0) &&
841 (gw_trans->page > 0) &&
842 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
843 prev_disp == gw_trans->repos_total))) {
844 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
845 KATTR_ID, "np_wrapper", KATTR__MAX);
846 if (kerr != KCGI_OK)
847 goto done;
848 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
849 KATTR_ID, "nav_prev", KATTR__MAX);
850 if (kerr != KCGI_OK)
851 goto done;
854 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
855 (gw_trans->page > 0) &&
856 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
857 prev_disp == gw_trans->repos_total)) {
858 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "page",
859 KATTRX_INT, (int64_t)(gw_trans->page - 1), NULL);
860 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
861 KATTR_HREF, href_prev, KATTR__MAX);
862 if (kerr != KCGI_OK)
863 goto done;
864 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
865 if (kerr != KCGI_OK)
866 goto done;
867 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
868 if (kerr != KCGI_OK)
869 goto done;
872 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
873 if (kerr != KCGI_OK)
874 return gw_kcgi_error(kerr);
876 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
877 next_disp == gw_trans->gw_conf->got_max_repos_display &&
878 dir_c != (gw_trans->page + 1) *
879 gw_trans->gw_conf->got_max_repos_display) {
880 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
881 KATTR_ID, "nav_next", KATTR__MAX);
882 if (kerr != KCGI_OK)
883 goto done;
884 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "page",
885 KATTRX_INT, (int64_t)(gw_trans->page + 1), NULL);
886 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
887 KATTR_HREF, href_next, KATTR__MAX);
888 if (kerr != KCGI_OK)
889 goto done;
890 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
891 if (kerr != KCGI_OK)
892 goto done;
893 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
894 if (kerr != KCGI_OK)
895 goto done;
896 next_disp = 0;
897 break;
900 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
901 (gw_trans->page > 0) &&
902 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
903 prev_disp == gw_trans->repos_total)) {
904 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
905 if (kerr != KCGI_OK)
906 goto done;
908 next_disp++;
910 done:
911 free(href_prev);
912 free(href_next);
913 free(href_summary);
914 free(href_briefs);
915 free(href_commits);
916 free(href_tags);
917 free(href_tree);
918 if (error == NULL && kerr != KCGI_OK)
919 error = gw_kcgi_error(kerr);
920 return error;
923 static const struct got_error *
924 gw_commits(struct gw_trans *gw_trans)
926 const struct got_error *error = NULL;
927 struct gw_header *header = NULL, *n_header = NULL;
928 char *age = NULL, *href_diff = NULL, *href_blob = NULL;
929 char *href_prev = NULL, *href_next = NULL;
930 enum kcgi_err kerr = KCGI_OK;
932 if ((header = gw_init_header()) == NULL)
933 return got_error_from_errno("malloc");
935 if (pledge("stdio rpath proc exec sendfd unveil",
936 NULL) == -1) {
937 error = got_error_from_errno("pledge");
938 goto done;
941 error = gw_apply_unveil(gw_trans->gw_dir->path);
942 if (error)
943 goto done;
945 error = gw_get_header(gw_trans, header,
946 gw_trans->gw_conf->got_max_commits_display);
947 if (error)
948 goto done;
950 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
951 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
952 "commits_line_wrapper", KATTR__MAX);
953 if (kerr != KCGI_OK)
954 goto done;
955 error = gw_gen_commit_header(gw_trans, n_header->commit_id,
956 n_header->refs_str);
957 if (error)
958 goto done;
959 error = gw_gen_author_header(gw_trans, n_header->author);
960 if (error)
961 goto done;
962 error = gw_gen_committer_header(gw_trans, n_header->author);
963 if (error)
964 goto done;
965 error = gw_get_time_str(&age, n_header->committer_time,
966 TM_LONG);
967 if (error)
968 goto done;
969 error = gw_gen_age_header(gw_trans, age ?age : "");
970 if (error)
971 goto done;
972 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
973 if (kerr != KCGI_OK)
974 goto done;
976 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
977 "dotted_line", KATTR__MAX);
978 if (kerr != KCGI_OK)
979 goto done;
980 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
981 if (kerr != KCGI_OK)
982 goto done;
984 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
985 "commit", KATTR__MAX);
986 if (kerr != KCGI_OK)
987 goto done;
988 kerr = khttp_puts(gw_trans->gw_req, n_header->commit_msg);
989 if (kerr != KCGI_OK)
990 goto done;
991 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
992 if (kerr != KCGI_OK)
993 goto done;
995 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
996 gw_trans->repo_name, n_header->commit_id) == -1) {
997 error = got_error_from_errno("asprintf");
998 goto done;
1000 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1001 KATTR_ID, "navs_wrapper", KATTR__MAX);
1002 if (kerr != KCGI_OK)
1003 goto done;
1004 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1005 KATTR_ID, "navs", KATTR__MAX);
1006 if (kerr != KCGI_OK)
1007 goto done;
1008 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1009 KATTR_HREF, href_diff, KATTR__MAX);
1010 if (kerr != KCGI_OK)
1011 goto done;
1012 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1013 if (kerr != KCGI_OK)
1014 goto done;
1015 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1016 if (kerr != KCGI_OK)
1017 goto done;
1019 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1020 if (kerr != KCGI_OK)
1021 goto done;
1023 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
1024 gw_trans->repo_name, n_header->commit_id) == -1) {
1025 error = got_error_from_errno("asprintf");
1026 goto done;
1028 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1029 KATTR_HREF, href_blob, KATTR__MAX);
1030 if (kerr != KCGI_OK)
1031 goto done;
1032 khtml_puts(gw_trans->gw_html_req, "tree");
1033 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1034 if (kerr != KCGI_OK)
1035 goto done;
1036 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1037 if (kerr != KCGI_OK)
1038 goto done;
1040 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1041 "solid_line", KATTR__MAX);
1042 if (kerr != KCGI_OK)
1043 goto done;
1044 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1045 if (kerr != KCGI_OK)
1046 goto done;
1048 free(age);
1049 age = NULL;
1052 if (gw_trans->next_id || gw_trans->page > 0) {
1053 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1054 KATTR_ID, "np_wrapper", KATTR__MAX);
1055 if (kerr != KCGI_OK)
1056 goto done;
1057 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1058 KATTR_ID, "nav_prev", KATTR__MAX);
1059 if (kerr != KCGI_OK)
1060 goto done;
1063 if (gw_trans->page > 0 && gw_trans->prev_id) {
1064 if (asprintf(&href_prev,
1065 "?path=%s&page=%d&action=commits&commit=%s&prev=%s",
1066 gw_trans->repo_name, gw_trans->page - 1,
1067 gw_trans->prev_id ? gw_trans->prev_id : "",
1068 gw_trans->prev_prev_id ?
1069 gw_trans->prev_prev_id : "") == -1) {
1070 error = got_error_from_errno("asprintf");
1071 goto done;
1073 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1074 KATTR_HREF, href_prev, KATTR__MAX);
1075 if (kerr != KCGI_OK)
1076 goto done;
1077 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1078 if (kerr != KCGI_OK)
1079 goto done;
1080 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1081 if (kerr != KCGI_OK)
1082 goto done;
1085 if (gw_trans->next_id || gw_trans->page > 0) {
1086 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1087 if (kerr != KCGI_OK)
1088 return gw_kcgi_error(kerr);
1091 if (gw_trans->next_id) {
1092 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1093 KATTR_ID, "nav_next", KATTR__MAX);
1094 if (kerr != KCGI_OK)
1095 goto done;
1096 if (asprintf(&href_next,
1097 "?path=%s&page=%d&action=commits" \
1098 "&commit=%s&prev=%s&prev_prev=%s",
1099 gw_trans->repo_name, gw_trans->page + 1,
1100 gw_trans->next_id,
1101 gw_trans->next_prev_id ? gw_trans->next_prev_id : "",
1102 gw_trans->prev_id ?
1103 gw_trans->prev_id : "") == -1) {
1104 error = got_error_from_errno("calloc");
1105 goto done;
1107 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1108 KATTR_HREF, href_next, KATTR__MAX);
1109 if (kerr != KCGI_OK)
1110 goto done;
1111 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1112 if (kerr != KCGI_OK)
1113 goto done;
1114 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1115 if (kerr != KCGI_OK)
1116 goto done;
1119 if (gw_trans->next_id || gw_trans->page > 0) {
1120 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1121 if (kerr != KCGI_OK)
1122 goto done;
1124 done:
1125 gw_free_header(header);
1126 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1127 gw_free_header(n_header);
1128 free(age);
1129 free(href_next);
1130 free(href_prev);
1131 free(href_diff);
1132 free(href_blob);
1133 if (error == NULL && kerr != KCGI_OK)
1134 error = gw_kcgi_error(kerr);
1135 return error;
1138 static const struct got_error *
1139 gw_briefs(struct gw_trans *gw_trans)
1141 const struct got_error *error = NULL;
1142 struct gw_header *header = NULL, *n_header = NULL;
1143 char *age = NULL, *href_diff = NULL, *href_blob = NULL;
1144 char *href_prev = NULL, *href_next = NULL;
1145 char *newline, *smallerthan;
1146 enum kcgi_err kerr = KCGI_OK;
1148 if ((header = gw_init_header()) == NULL)
1149 return got_error_from_errno("malloc");
1151 if (pledge("stdio rpath proc exec sendfd unveil",
1152 NULL) == -1) {
1153 error = got_error_from_errno("pledge");
1154 goto done;
1157 if (gw_trans->action != GW_SUMMARY) {
1158 error = gw_apply_unveil(gw_trans->gw_dir->path);
1159 if (error)
1160 goto done;
1163 if (gw_trans->action == GW_SUMMARY)
1164 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
1165 else
1166 error = gw_get_header(gw_trans, header,
1167 gw_trans->gw_conf->got_max_commits_display);
1168 if (error)
1169 goto done;
1171 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
1172 error = gw_get_time_str(&age, n_header->committer_time,
1173 TM_DIFF);
1174 if (error)
1175 goto done;
1177 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1178 KATTR_ID, "briefs_wrapper", KATTR__MAX);
1179 if (kerr != KCGI_OK)
1180 goto done;
1182 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1183 KATTR_ID, "briefs_age", KATTR__MAX);
1184 if (kerr != KCGI_OK)
1185 goto done;
1186 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
1187 if (kerr != KCGI_OK)
1188 goto done;
1189 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1190 if (kerr != KCGI_OK)
1191 goto done;
1193 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1194 KATTR_ID, "briefs_author", KATTR__MAX);
1195 if (kerr != KCGI_OK)
1196 goto done;
1197 smallerthan = strchr(n_header->author, '<');
1198 if (smallerthan)
1199 *smallerthan = '\0';
1200 kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
1201 if (kerr != KCGI_OK)
1202 goto done;
1203 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1204 if (kerr != KCGI_OK)
1205 goto done;
1207 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
1208 gw_trans->repo_name, n_header->commit_id) == -1) {
1209 error = got_error_from_errno("asprintf");
1210 goto done;
1212 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1213 KATTR_ID, "briefs_log", KATTR__MAX);
1214 if (kerr != KCGI_OK)
1215 goto done;
1216 newline = strchr(n_header->commit_msg, '\n');
1217 if (newline)
1218 *newline = '\0';
1219 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1220 KATTR_HREF, href_diff, KATTR__MAX);
1221 if (kerr != KCGI_OK)
1222 goto done;
1223 kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
1224 if (kerr != KCGI_OK)
1225 goto done;
1226 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1227 if (kerr != KCGI_OK)
1228 goto done;
1230 if (n_header->refs_str) {
1231 kerr = khtml_puts(gw_trans->gw_html_req, " ");
1232 if (kerr != KCGI_OK)
1233 goto done;
1234 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
1235 KATTR_ID, "refs_str", KATTR__MAX);
1236 if (kerr != KCGI_OK)
1237 goto done;
1238 kerr = khtml_printf(gw_trans->gw_html_req, "(%s)",
1239 n_header->refs_str);
1240 if (kerr != KCGI_OK)
1241 goto done;
1242 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1243 if (kerr != KCGI_OK)
1244 goto done;
1247 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1248 if (kerr != KCGI_OK)
1249 goto done;
1251 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1252 KATTR_ID, "navs_wrapper", KATTR__MAX);
1253 if (kerr != KCGI_OK)
1254 goto done;
1255 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1256 KATTR_ID, "navs", KATTR__MAX);
1257 if (kerr != KCGI_OK)
1258 goto done;
1259 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1260 KATTR_HREF, href_diff, KATTR__MAX);
1261 if (kerr != KCGI_OK)
1262 goto done;
1263 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1264 if (kerr != KCGI_OK)
1265 goto done;
1266 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1267 if (kerr != KCGI_OK)
1268 goto done;
1270 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1271 if (kerr != KCGI_OK)
1272 goto done;
1274 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
1275 gw_trans->repo_name, n_header->commit_id) == -1) {
1276 error = got_error_from_errno("asprintf");
1277 goto done;
1279 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1280 KATTR_HREF, href_blob, KATTR__MAX);
1281 if (kerr != KCGI_OK)
1282 goto done;
1283 khtml_puts(gw_trans->gw_html_req, "tree");
1284 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1285 if (kerr != KCGI_OK)
1286 goto done;
1287 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1288 if (kerr != KCGI_OK)
1289 goto done;
1291 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1292 KATTR_ID, "dotted_line", KATTR__MAX);
1293 if (kerr != KCGI_OK)
1294 goto done;
1295 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1296 if (kerr != KCGI_OK)
1297 goto done;
1299 free(age);
1300 age = NULL;
1301 free(href_diff);
1302 href_diff = NULL;
1303 free(href_blob);
1304 href_blob = NULL;
1307 if (gw_trans->next_id || gw_trans->page > 0) {
1308 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1309 KATTR_ID, "np_wrapper", KATTR__MAX);
1310 if (kerr != KCGI_OK)
1311 goto done;
1312 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1313 KATTR_ID, "nav_prev", KATTR__MAX);
1314 if (kerr != KCGI_OK)
1315 goto done;
1318 if (gw_trans->page > 0 && gw_trans->prev_id) {
1319 if (asprintf(&href_prev,
1320 "?path=%s&page=%d&action=briefs&commit=%s&prev=%s",
1321 gw_trans->repo_name, gw_trans->page - 1,
1322 gw_trans->prev_id ? gw_trans->prev_id : "",
1323 gw_trans->prev_prev_id ?
1324 gw_trans->prev_prev_id : "") == -1) {
1325 error = got_error_from_errno("asprintf");
1326 goto done;
1328 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1329 KATTR_HREF, href_prev, KATTR__MAX);
1330 if (kerr != KCGI_OK)
1331 goto done;
1332 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1333 if (kerr != KCGI_OK)
1334 goto done;
1335 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1336 if (kerr != KCGI_OK)
1337 goto done;
1340 if (gw_trans->next_id || gw_trans->page > 0) {
1341 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1342 if (kerr != KCGI_OK)
1343 return gw_kcgi_error(kerr);
1346 if (gw_trans->next_id) {
1347 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1348 KATTR_ID, "nav_next", KATTR__MAX);
1349 if (kerr != KCGI_OK)
1350 goto done;
1351 if (asprintf(&href_next,
1352 "?path=%s&page=%d&action=briefs" \
1353 "&commit=%s&prev=%s&prev_prev=%s",
1354 gw_trans->repo_name, gw_trans->page + 1,
1355 gw_trans->next_id,
1356 gw_trans->next_prev_id ? gw_trans->next_prev_id : "",
1357 gw_trans->prev_id ?
1358 gw_trans->prev_id : "") == -1) {
1359 error = got_error_from_errno("calloc");
1360 goto done;
1362 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1363 KATTR_HREF, href_next, KATTR__MAX);
1364 if (kerr != KCGI_OK)
1365 goto done;
1366 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1367 if (kerr != KCGI_OK)
1368 goto done;
1369 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1370 if (kerr != KCGI_OK)
1371 goto done;
1374 if (gw_trans->next_id || gw_trans->page > 0) {
1375 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1376 if (kerr != KCGI_OK)
1377 goto done;
1379 done:
1380 gw_free_header(header);
1381 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1382 gw_free_header(n_header);
1383 free(age);
1384 free(href_next);
1385 free(href_prev);
1386 free(href_diff);
1387 free(href_blob);
1388 if (error == NULL && kerr != KCGI_OK)
1389 error = gw_kcgi_error(kerr);
1390 return error;
1393 static const struct got_error *
1394 gw_summary(struct gw_trans *gw_trans)
1396 const struct got_error *error = NULL;
1397 char *age = NULL;
1398 enum kcgi_err kerr = KCGI_OK;
1400 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1401 return got_error_from_errno("pledge");
1403 error = gw_apply_unveil(gw_trans->gw_dir->path);
1404 if (error)
1405 goto done;
1407 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1408 "summary_wrapper", KATTR__MAX);
1409 if (kerr != KCGI_OK)
1410 return gw_kcgi_error(kerr);
1412 if (gw_trans->gw_conf->got_show_repo_description &&
1413 gw_trans->gw_dir->description != NULL &&
1414 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1415 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1416 KATTR_ID, "description_title", KATTR__MAX);
1417 if (kerr != KCGI_OK)
1418 goto done;
1419 kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1420 if (kerr != KCGI_OK)
1421 goto done;
1422 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1423 if (kerr != KCGI_OK)
1424 goto done;
1425 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1426 KATTR_ID, "description", KATTR__MAX);
1427 if (kerr != KCGI_OK)
1428 goto done;
1429 kerr = khtml_puts(gw_trans->gw_html_req,
1430 gw_trans->gw_dir->description);
1431 if (kerr != KCGI_OK)
1432 goto done;
1433 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1434 if (kerr != KCGI_OK)
1435 goto done;
1438 if (gw_trans->gw_conf->got_show_repo_owner &&
1439 gw_trans->gw_dir->owner != NULL &&
1440 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
1441 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1442 KATTR_ID, "repo_owner_title", KATTR__MAX);
1443 if (kerr != KCGI_OK)
1444 goto done;
1445 kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1446 if (kerr != KCGI_OK)
1447 goto done;
1448 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1449 if (kerr != KCGI_OK)
1450 goto done;
1451 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1452 KATTR_ID, "repo_owner", KATTR__MAX);
1453 if (kerr != KCGI_OK)
1454 goto done;
1455 kerr = khtml_puts(gw_trans->gw_html_req,
1456 gw_trans->gw_dir->owner);
1457 if (kerr != KCGI_OK)
1458 goto done;
1459 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1460 if (kerr != KCGI_OK)
1461 goto done;
1464 if (gw_trans->gw_conf->got_show_repo_age) {
1465 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1466 NULL, TM_LONG);
1467 if (error)
1468 goto done;
1469 if (age != NULL) {
1470 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1471 KATTR_ID, "last_change_title", KATTR__MAX);
1472 if (kerr != KCGI_OK)
1473 goto done;
1474 kerr = khtml_puts(gw_trans->gw_html_req,
1475 "Last Change: ");
1476 if (kerr != KCGI_OK)
1477 goto done;
1478 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1479 if (kerr != KCGI_OK)
1480 goto done;
1481 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1482 KATTR_ID, "last_change", KATTR__MAX);
1483 if (kerr != KCGI_OK)
1484 goto done;
1485 kerr = khtml_puts(gw_trans->gw_html_req, age);
1486 if (kerr != KCGI_OK)
1487 goto done;
1488 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1489 if (kerr != KCGI_OK)
1490 goto done;
1494 if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1495 gw_trans->gw_dir->url != NULL &&
1496 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1497 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1498 KATTR_ID, "cloneurl_title", KATTR__MAX);
1499 if (kerr != KCGI_OK)
1500 goto done;
1501 kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1502 if (kerr != KCGI_OK)
1503 goto done;
1504 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1505 if (kerr != KCGI_OK)
1506 goto done;
1507 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1508 KATTR_ID, "cloneurl", KATTR__MAX);
1509 if (kerr != KCGI_OK)
1510 goto done;
1511 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1512 if (kerr != KCGI_OK)
1513 goto done;
1514 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1515 if (kerr != KCGI_OK)
1516 goto done;
1519 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1520 if (kerr != KCGI_OK)
1521 goto done;
1523 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1524 "briefs_title_wrapper", KATTR__MAX);
1525 if (kerr != KCGI_OK)
1526 goto done;
1527 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1528 "briefs_title", KATTR__MAX);
1529 if (kerr != KCGI_OK)
1530 goto done;
1531 kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1532 if (kerr != KCGI_OK)
1533 goto done;
1534 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1535 if (kerr != KCGI_OK)
1536 goto done;
1537 error = gw_briefs(gw_trans);
1538 if (error)
1539 goto done;
1541 error = gw_tags(gw_trans);
1542 if (error)
1543 goto done;
1545 error = gw_output_repo_heads(gw_trans);
1546 done:
1547 free(age);
1548 if (error == NULL && kerr != KCGI_OK)
1549 error = gw_kcgi_error(kerr);
1550 return error;
1553 static const struct got_error *
1554 gw_tree(struct gw_trans *gw_trans)
1556 const struct got_error *error = NULL;
1557 struct gw_header *header = NULL;
1558 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1559 char *age = NULL;
1560 enum kcgi_err kerr = KCGI_OK;
1562 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1563 return got_error_from_errno("pledge");
1565 if ((header = gw_init_header()) == NULL)
1566 return got_error_from_errno("malloc");
1568 error = gw_apply_unveil(gw_trans->gw_dir->path);
1569 if (error)
1570 goto done;
1572 error = gw_get_header(gw_trans, header, 1);
1573 if (error)
1574 goto done;
1576 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1577 "tree_header_wrapper", KATTR__MAX);
1578 if (kerr != KCGI_OK)
1579 goto done;
1580 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1581 "tree_header", KATTR__MAX);
1582 if (kerr != KCGI_OK)
1583 goto done;
1584 error = gw_gen_tree_header(gw_trans, header->tree_id);
1585 if (error)
1586 goto done;
1587 error = gw_get_time_str(&age, header->committer_time,
1588 TM_LONG);
1589 if (error)
1590 goto done;
1591 error = gw_gen_age_header(gw_trans, age ?age : "");
1592 if (error)
1593 goto done;
1594 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1595 if (error)
1596 goto done;
1597 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1598 if (kerr != KCGI_OK)
1599 goto done;
1600 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1601 "dotted_line", KATTR__MAX);
1602 if (kerr != KCGI_OK)
1603 goto done;
1604 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1605 if (kerr != KCGI_OK)
1606 goto done;
1608 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1609 "tree", KATTR__MAX);
1610 if (kerr != KCGI_OK)
1611 goto done;
1612 error = gw_output_repo_tree(gw_trans);
1613 if (error)
1614 goto done;
1616 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1617 done:
1618 gw_free_header(header);
1619 free(tree_html_disp);
1620 free(tree_html);
1621 free(tree);
1622 free(age);
1623 if (error == NULL && kerr != KCGI_OK)
1624 error = gw_kcgi_error(kerr);
1625 return error;
1628 static const struct got_error *
1629 gw_tags(struct gw_trans *gw_trans)
1631 const struct got_error *error = NULL;
1632 struct gw_header *header = NULL;
1633 char *href_next = NULL, *href_prev = NULL;
1634 enum kcgi_err kerr = KCGI_OK;
1636 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1637 return got_error_from_errno("pledge");
1639 if ((header = gw_init_header()) == NULL)
1640 return got_error_from_errno("malloc");
1642 if (gw_trans->action != GW_SUMMARY) {
1643 error = gw_apply_unveil(gw_trans->gw_dir->path);
1644 if (error)
1645 goto done;
1648 error = gw_get_header(gw_trans, header, 1);
1649 if (error)
1650 goto done;
1652 if (gw_trans->action == GW_SUMMARY) {
1653 gw_trans->next_id = NULL;
1654 error = gw_output_repo_tags(gw_trans, header,
1655 D_MAXSLCOMMDISP, TAGBRIEF);
1656 if (error)
1657 goto done;
1658 } else {
1659 error = gw_output_repo_tags(gw_trans, header,
1660 gw_trans->gw_conf->got_max_commits_display, TAGBRIEF);
1661 if (error)
1662 goto done;
1665 if (gw_trans->next_id || gw_trans->page > 0) {
1666 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1667 KATTR_ID, "np_wrapper", KATTR__MAX);
1668 if (kerr != KCGI_OK)
1669 goto done;
1670 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1671 KATTR_ID, "nav_prev", KATTR__MAX);
1672 if (kerr != KCGI_OK)
1673 goto done;
1676 if (gw_trans->page > 0 && gw_trans->prev_id) {
1677 if (asprintf(&href_prev,
1678 "?path=%s&page=%d&action=tags&commit=%s&prev=%s",
1679 gw_trans->repo_name, gw_trans->page - 1,
1680 gw_trans->prev_id ? gw_trans->prev_id : "",
1681 gw_trans->prev_prev_id ?
1682 gw_trans->prev_prev_id : "") == -1) {
1683 error = got_error_from_errno("asprintf");
1684 goto done;
1686 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1687 KATTR_HREF, href_prev, KATTR__MAX);
1688 if (kerr != KCGI_OK)
1689 goto done;
1690 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1691 if (kerr != KCGI_OK)
1692 goto done;
1693 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1694 if (kerr != KCGI_OK)
1695 goto done;
1698 if (gw_trans->next_id || gw_trans->page > 0) {
1699 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1700 if (kerr != KCGI_OK)
1701 return gw_kcgi_error(kerr);
1704 if (gw_trans->next_id) {
1705 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1706 KATTR_ID, "nav_next", KATTR__MAX);
1707 if (kerr != KCGI_OK)
1708 goto done;
1709 if (asprintf(&href_next,
1710 "?path=%s&page=%d&action=tags" \
1711 "&commit=%s&prev=%s&prev_prev=%s",
1712 gw_trans->repo_name, gw_trans->page + 1,
1713 gw_trans->next_id,
1714 gw_trans->next_prev_id ? gw_trans->next_prev_id : "",
1715 gw_trans->prev_id ?
1716 gw_trans->prev_id : "") == -1) {
1717 error = got_error_from_errno("calloc");
1718 goto done;
1720 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1721 KATTR_HREF, href_next, KATTR__MAX);
1722 if (kerr != KCGI_OK)
1723 goto done;
1724 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1725 if (kerr != KCGI_OK)
1726 goto done;
1727 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1728 if (kerr != KCGI_OK)
1729 goto done;
1732 if (gw_trans->next_id || gw_trans->page > 0) {
1733 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1734 if (kerr != KCGI_OK)
1735 goto done;
1737 done:
1738 gw_free_header(header);
1739 free(href_next);
1740 free(href_prev);
1741 if (error == NULL && kerr != KCGI_OK)
1742 error = gw_kcgi_error(kerr);
1743 return error;
1746 static const struct got_error *
1747 gw_tag(struct gw_trans *gw_trans)
1749 const struct got_error *error = NULL;
1750 struct gw_header *header = NULL;
1751 enum kcgi_err kerr = KCGI_OK;
1753 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1754 return got_error_from_errno("pledge");
1756 if ((header = gw_init_header()) == NULL)
1757 return got_error_from_errno("malloc");
1759 error = gw_apply_unveil(gw_trans->gw_dir->path);
1760 if (error)
1761 goto done;
1763 if (gw_trans->commit_id == NULL) {
1764 error = got_error_msg(GOT_ERR_QUERYSTRING,
1765 "commit required in querystring");
1766 goto done;
1769 error = gw_get_header(gw_trans, header, 1);
1770 if (error)
1771 goto done;
1773 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1774 "tag_header_wrapper", KATTR__MAX);
1775 if (kerr != KCGI_OK)
1776 goto done;
1777 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1778 "tag_header", KATTR__MAX);
1779 if (kerr != KCGI_OK)
1780 goto done;
1781 error = gw_gen_commit_header(gw_trans, header->commit_id,
1782 header->refs_str);
1783 if (error)
1784 goto done;
1785 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1786 if (error)
1787 goto done;
1788 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1789 if (kerr != KCGI_OK)
1790 goto done;
1791 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1792 "dotted_line", KATTR__MAX);
1793 if (kerr != KCGI_OK)
1794 goto done;
1795 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1796 if (kerr != KCGI_OK)
1797 goto done;
1799 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1800 "tree", KATTR__MAX);
1801 if (kerr != KCGI_OK)
1802 goto done;
1804 error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1805 if (error)
1806 goto done;
1808 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1809 done:
1810 gw_free_header(header);
1811 if (error == NULL && kerr != KCGI_OK)
1812 error = gw_kcgi_error(kerr);
1813 return error;
1816 static const struct got_error *
1817 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1819 const struct got_error *error = NULL;
1820 DIR *dt;
1821 char *dir_test;
1822 int opened = 0;
1824 if (asprintf(&dir_test, "%s/%s/%s",
1825 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1826 GOTWEB_GIT_DIR) == -1)
1827 return got_error_from_errno("asprintf");
1829 dt = opendir(dir_test);
1830 if (dt == NULL) {
1831 free(dir_test);
1832 } else {
1833 gw_dir->path = strdup(dir_test);
1834 if (gw_dir->path == NULL) {
1835 opened = 1;
1836 error = got_error_from_errno("strdup");
1837 goto errored;
1839 opened = 1;
1840 goto done;
1843 if (asprintf(&dir_test, "%s/%s/%s",
1844 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1845 GOTWEB_GOT_DIR) == -1) {
1846 dir_test = NULL;
1847 error = got_error_from_errno("asprintf");
1848 goto errored;
1851 dt = opendir(dir_test);
1852 if (dt == NULL)
1853 free(dir_test);
1854 else {
1855 opened = 1;
1856 error = got_error(GOT_ERR_NOT_GIT_REPO);
1857 goto errored;
1860 if (asprintf(&dir_test, "%s/%s",
1861 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1) {
1862 error = got_error_from_errno("asprintf");
1863 dir_test = NULL;
1864 goto errored;
1867 gw_dir->path = strdup(dir_test);
1868 if (gw_dir->path == NULL) {
1869 opened = 1;
1870 error = got_error_from_errno("strdup");
1871 goto errored;
1874 dt = opendir(dir_test);
1875 if (dt == NULL) {
1876 error = got_error_path(gw_dir->name, GOT_ERR_NOT_GIT_REPO);
1877 goto errored;
1878 } else
1879 opened = 1;
1880 done:
1881 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1882 gw_dir->path);
1883 if (error)
1884 goto errored;
1885 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1886 if (error)
1887 goto errored;
1888 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1889 NULL, TM_DIFF);
1890 if (error)
1891 goto errored;
1892 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1893 errored:
1894 free(dir_test);
1895 if (opened)
1896 if (dt && closedir(dt) == -1 && error == NULL)
1897 error = got_error_from_errno("closedir");
1898 return error;
1901 static const struct got_error *
1902 gw_load_got_paths(struct gw_trans *gw_trans)
1904 const struct got_error *error = NULL;
1905 DIR *d;
1906 struct dirent **sd_dent;
1907 struct gw_dir *gw_dir;
1908 struct stat st;
1909 unsigned int d_cnt, d_i;
1911 d = opendir(gw_trans->gw_conf->got_repos_path);
1912 if (d == NULL) {
1913 error = got_error_from_errno2("opendir",
1914 gw_trans->gw_conf->got_repos_path);
1915 return error;
1918 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1919 alphasort);
1920 if (d_cnt == -1) {
1921 error = got_error_from_errno2("scandir",
1922 gw_trans->gw_conf->got_repos_path);
1923 goto done;
1926 for (d_i = 0; d_i < d_cnt; d_i++) {
1927 if (gw_trans->gw_conf->got_max_repos > 0 &&
1928 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1929 break; /* account for parent and self */
1931 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1932 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1933 continue;
1935 error = gw_init_gw_dir(&gw_dir, sd_dent[d_i]->d_name);
1936 if (error)
1937 goto done;
1939 error = gw_load_got_path(gw_trans, gw_dir);
1940 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1941 error = NULL;
1942 continue;
1944 else if (error)
1945 goto done;
1947 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1948 !got_path_dir_is_empty(gw_dir->path)) {
1949 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1950 entry);
1951 gw_trans->repos_total++;
1954 done:
1955 if (d && closedir(d) == -1 && error == NULL)
1956 error = got_error_from_errno("closedir");
1957 return error;
1960 static const struct got_error *
1961 gw_parse_querystring(struct gw_trans *gw_trans)
1963 const struct got_error *error = NULL;
1964 struct kpair *p;
1965 struct gw_query_action *action = NULL;
1966 unsigned int i;
1968 if (gw_trans->gw_req->fieldnmap[0]) {
1969 return got_error(GOT_ERR_QUERYSTRING);
1970 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1971 /* define gw_trans->repo_path */
1972 gw_trans->repo_name = p->parsed.s;
1974 if (asprintf(&gw_trans->repo_path, "%s/%s",
1975 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
1976 return got_error_from_errno("asprintf");
1978 /* get action and set function */
1979 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION])) {
1980 for (i = 0; i < nitems(gw_query_funcs); i++) {
1981 action = &gw_query_funcs[i];
1982 if (action->func_name == NULL)
1983 continue;
1984 if (strcmp(action->func_name,
1985 p->parsed.s) == 0) {
1986 gw_trans->action = i;
1987 break;
1991 if (gw_trans->action == -1) {
1992 gw_trans->action = GW_ERR;
1993 gw_trans->error = got_error_msg(GOT_ERR_QUERYSTRING,
1994 p != NULL ? "bad action in querystring" :
1995 "no action in querystring");
1996 return error;
1999 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID])) {
2000 if (asprintf(&gw_trans->commit_id, "%s",
2001 p->parsed.s) == -1)
2002 return got_error_from_errno("asprintf");
2005 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
2006 gw_trans->repo_file = p->parsed.s;
2008 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER])) {
2009 if (asprintf(&gw_trans->repo_folder, "%s",
2010 p->parsed.s) == -1)
2011 return got_error_from_errno("asprintf");
2014 if ((p = gw_trans->gw_req->fieldmap[KEY_PREV_ID])) {
2015 if (asprintf(&gw_trans->prev_id, "%s",
2016 p->parsed.s) == -1)
2017 return got_error_from_errno("asprintf");
2020 if ((p = gw_trans->gw_req->fieldmap[KEY_PREV_PREV_ID])) {
2021 if (asprintf(&gw_trans->prev_prev_id, "%s",
2022 p->parsed.s) == -1)
2023 return got_error_from_errno("asprintf");
2026 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
2027 gw_trans->headref = p->parsed.s;
2029 error = gw_init_gw_dir(&gw_trans->gw_dir, gw_trans->repo_name);
2030 if (error)
2031 return error;
2033 gw_trans->error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
2034 } else
2035 gw_trans->action = GW_INDEX;
2037 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
2038 gw_trans->page = p->parsed.i;
2040 return error;
2043 static const struct got_error *
2044 gw_init_gw_dir(struct gw_dir **gw_dir, const char *dir)
2046 const struct got_error *error;
2048 *gw_dir = malloc(sizeof(**gw_dir));
2049 if (*gw_dir == NULL)
2050 return got_error_from_errno("malloc");
2052 if (asprintf(&(*gw_dir)->name, "%s", dir) == -1) {
2053 error = got_error_from_errno("asprintf");
2054 free(*gw_dir);
2055 *gw_dir = NULL;
2056 return NULL;
2059 return NULL;
2062 static const struct got_error *
2063 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
2065 enum kcgi_err kerr = KCGI_OK;
2067 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
2068 if (kerr != KCGI_OK)
2069 return gw_kcgi_error(kerr);
2070 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
2071 khttps[code]);
2072 if (kerr != KCGI_OK)
2073 return gw_kcgi_error(kerr);
2074 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
2075 kmimetypes[mime]);
2076 if (kerr != KCGI_OK)
2077 return gw_kcgi_error(kerr);
2078 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
2079 "nosniff");
2080 if (kerr != KCGI_OK)
2081 return gw_kcgi_error(kerr);
2082 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
2083 if (kerr != KCGI_OK)
2084 return gw_kcgi_error(kerr);
2085 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
2086 "1; mode=block");
2087 if (kerr != KCGI_OK)
2088 return gw_kcgi_error(kerr);
2090 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
2091 kerr = khttp_head(gw_trans->gw_req,
2092 kresps[KRESP_CONTENT_DISPOSITION],
2093 "attachment; filename=%s", gw_trans->repo_file);
2094 if (kerr != KCGI_OK)
2095 return gw_kcgi_error(kerr);
2098 kerr = khttp_body(gw_trans->gw_req);
2099 return gw_kcgi_error(kerr);
2102 static const struct got_error *
2103 gw_display_index(struct gw_trans *gw_trans)
2105 const struct got_error *error;
2106 enum kcgi_err kerr = KCGI_OK;
2108 /* catch early querystring errors */
2109 if (gw_trans->error)
2110 gw_trans->action = GW_ERR;
2112 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
2113 if (error)
2114 return error;
2116 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
2117 if (kerr != KCGI_OK)
2118 return gw_kcgi_error(kerr);
2120 if (gw_trans->action != GW_BLOB) {
2121 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
2122 gw_query_funcs[gw_trans->action].template);
2123 if (kerr != KCGI_OK) {
2124 khtml_close(gw_trans->gw_html_req);
2125 return gw_kcgi_error(kerr);
2129 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
2132 static const struct got_error *
2133 gw_error(struct gw_trans *gw_trans)
2135 enum kcgi_err kerr = KCGI_OK;
2137 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->error->msg);
2139 return gw_kcgi_error(kerr);
2142 static int
2143 gw_template(size_t key, void *arg)
2145 const struct got_error *error = NULL;
2146 enum kcgi_err kerr = KCGI_OK;
2147 struct gw_trans *gw_trans = arg;
2148 char *img_src = NULL;
2150 switch (key) {
2151 case (TEMPL_HEAD):
2152 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2153 KATTR_NAME, "viewport",
2154 KATTR_CONTENT, "initial-scale=.75, user-scalable=yes",
2155 KATTR__MAX);
2156 if (kerr != KCGI_OK)
2157 return 0;
2158 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2159 if (kerr != KCGI_OK)
2160 return 0;
2161 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2162 KATTR_CHARSET, "utf-8",
2163 KATTR__MAX);
2164 if (kerr != KCGI_OK)
2165 return 0;
2166 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2167 if (kerr != KCGI_OK)
2168 return 0;
2169 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2170 KATTR_NAME, "msapplication-TileColor",
2171 KATTR_CONTENT, "#da532c", KATTR__MAX);
2172 if (kerr != KCGI_OK)
2173 return 0;
2174 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2175 if (kerr != KCGI_OK)
2176 return 0;
2177 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2178 KATTR_NAME, "theme-color",
2179 KATTR_CONTENT, "#ffffff", KATTR__MAX);
2180 if (kerr != KCGI_OK)
2181 return 0;
2182 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2183 if (kerr != KCGI_OK)
2184 return 0;
2185 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2186 KATTR_REL, "apple-touch-icon", KATTR_SIZES, "180x180",
2187 KATTR_HREF, "/apple-touch-icon.png", KATTR__MAX);
2188 if (kerr != KCGI_OK)
2189 return 0;
2190 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2191 if (kerr != KCGI_OK)
2192 return 0;
2193 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2194 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2195 "32x32", KATTR_HREF, "/favicon-32x32.png", KATTR__MAX);
2196 if (kerr != KCGI_OK)
2197 return 0;
2198 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2199 if (kerr != KCGI_OK)
2200 return 0;
2201 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2202 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2203 "16x16", KATTR_HREF, "/favicon-16x16.png", KATTR__MAX);
2204 if (kerr != KCGI_OK)
2205 return 0;
2206 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2207 if (kerr != KCGI_OK)
2208 return 0;
2209 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2210 KATTR_REL, "manifest", KATTR_HREF, "/site.webmanifest",
2211 KATTR__MAX);
2212 if (kerr != KCGI_OK)
2213 return 0;
2214 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2215 if (kerr != KCGI_OK)
2216 return 0;
2217 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2218 KATTR_REL, "mask-icon", KATTR_HREF,
2219 "/safari-pinned-tab.svg", KATTR__MAX);
2220 if (kerr != KCGI_OK)
2221 return 0;
2222 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2223 if (kerr != KCGI_OK)
2224 return 0;
2225 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2226 KATTR_REL, "stylesheet", KATTR_TYPE, "text/css",
2227 KATTR_HREF, "/gotweb.css", KATTR__MAX);
2228 if (kerr != KCGI_OK)
2229 return 0;
2230 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2231 if (kerr != KCGI_OK)
2232 return 0;
2233 break;
2234 case(TEMPL_HEADER):
2235 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2236 KATTR_ID, "got_link", KATTR__MAX);
2237 if (kerr != KCGI_OK)
2238 return 0;
2239 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2240 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
2241 KATTR_TARGET, "_sotd", KATTR__MAX);
2242 if (kerr != KCGI_OK)
2243 return 0;
2244 if (asprintf(&img_src, "/%s",
2245 gw_trans->gw_conf->got_logo) == -1)
2246 return 0;
2247 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
2248 KATTR_SRC, img_src, KATTR__MAX);
2249 if (kerr != KCGI_OK) {
2250 free(img_src);
2251 return 0;
2253 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2254 if (kerr != KCGI_OK) {
2255 free(img_src);
2256 return 0;
2258 break;
2259 case (TEMPL_SITEPATH):
2260 error = gw_output_site_link(gw_trans);
2261 if (error)
2262 return 0;
2263 break;
2264 case(TEMPL_TITLE):
2265 if (gw_trans->gw_conf->got_site_name != NULL) {
2266 kerr = khtml_puts(gw_trans->gw_html_req,
2267 gw_trans->gw_conf->got_site_name);
2268 if (kerr != KCGI_OK)
2269 return 0;
2271 break;
2272 case (TEMPL_SEARCH):
2273 break;
2274 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
2275 "search", KATTR__MAX);
2276 if (kerr != KCGI_OK)
2277 return 0;
2278 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
2279 KATTR_METHOD, "POST", KATTR__MAX);
2280 if (kerr != KCGI_OK)
2281 return 0;
2282 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
2283 "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
2284 KATTR_MAXLENGTH, "50", KATTR__MAX);
2285 if (kerr != KCGI_OK)
2286 return 0;
2287 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
2288 KATTR__MAX);
2289 if (kerr != KCGI_OK)
2290 return 0;
2291 kerr = khtml_puts(gw_trans->gw_html_req, "Search");
2292 if (kerr != KCGI_OK)
2293 return 0;
2294 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
2295 if (kerr != KCGI_OK)
2296 return 0;
2297 break;
2298 case(TEMPL_SITEOWNER):
2299 if (gw_trans->gw_conf->got_site_owner != NULL &&
2300 gw_trans->gw_conf->got_show_site_owner) {
2301 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2302 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
2303 if (kerr != KCGI_OK)
2304 return 0;
2305 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2306 KATTR_ID, "site_owner", KATTR__MAX);
2307 if (kerr != KCGI_OK)
2308 return 0;
2309 kerr = khtml_puts(gw_trans->gw_html_req,
2310 gw_trans->gw_conf->got_site_owner);
2311 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2312 if (kerr != KCGI_OK)
2313 return 0;
2315 break;
2316 case(TEMPL_CONTENT):
2317 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
2318 if (error) {
2319 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2320 KATTR_ID, "tmpl_err", KATTR__MAX);
2321 if (kerr != KCGI_OK)
2322 return 0;
2323 kerr = khttp_printf(gw_trans->gw_req, "Error: %s",
2324 error->msg);
2325 if (kerr != KCGI_OK)
2326 return 0;
2327 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2328 if (kerr != KCGI_OK)
2329 return 0;
2331 break;
2332 default:
2333 return 0;
2335 return 1;
2338 static const struct got_error *
2339 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
2341 const struct got_error *error = NULL;
2342 enum kcgi_err kerr = KCGI_OK;
2344 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2345 KATTR_ID, "header_commit_title", KATTR__MAX);
2346 if (kerr != KCGI_OK)
2347 goto done;
2348 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
2349 if (kerr != KCGI_OK)
2350 goto done;
2351 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2352 if (kerr != KCGI_OK)
2353 goto done;
2354 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2355 KATTR_ID, "header_commit", KATTR__MAX);
2356 if (kerr != KCGI_OK)
2357 goto done;
2358 kerr = khtml_printf(gw_trans->gw_html_req, "%s ", str1);
2359 if (kerr != KCGI_OK)
2360 goto done;
2361 if (str2 != NULL) {
2362 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
2363 KATTR_ID, "refs_str", KATTR__MAX);
2364 if (kerr != KCGI_OK)
2365 goto done;
2366 kerr = khtml_printf(gw_trans->gw_html_req, "(%s)", str2);
2367 if (kerr != KCGI_OK)
2368 goto done;
2369 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2370 if (kerr != KCGI_OK)
2371 goto done;
2373 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2374 done:
2375 if (error == NULL && kerr != KCGI_OK)
2376 error = gw_kcgi_error(kerr);
2377 return error;
2380 static const struct got_error *
2381 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2383 const struct got_error *error = NULL;
2384 enum kcgi_err kerr = KCGI_OK;
2386 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2387 KATTR_ID, "header_diff_title", KATTR__MAX);
2388 if (kerr != KCGI_OK)
2389 goto done;
2390 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2391 if (kerr != KCGI_OK)
2392 goto done;
2393 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2394 if (kerr != KCGI_OK)
2395 goto done;
2396 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2397 KATTR_ID, "header_diff", KATTR__MAX);
2398 if (kerr != KCGI_OK)
2399 goto done;
2400 if (str1 != NULL) {
2401 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2402 if (kerr != KCGI_OK)
2403 goto done;
2405 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2406 if (kerr != KCGI_OK)
2407 goto done;
2408 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2409 if (kerr != KCGI_OK)
2410 goto done;
2411 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2412 done:
2413 if (error == NULL && kerr != KCGI_OK)
2414 error = gw_kcgi_error(kerr);
2415 return error;
2418 static const struct got_error *
2419 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2421 const struct got_error *error = NULL;
2422 enum kcgi_err kerr = KCGI_OK;
2424 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2425 KATTR_ID, "header_age_title", KATTR__MAX);
2426 if (kerr != KCGI_OK)
2427 goto done;
2428 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2429 if (kerr != KCGI_OK)
2430 goto done;
2431 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2432 if (kerr != KCGI_OK)
2433 goto done;
2434 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2435 KATTR_ID, "header_age", KATTR__MAX);
2436 if (kerr != KCGI_OK)
2437 goto done;
2438 kerr = khtml_puts(gw_trans->gw_html_req, str);
2439 if (kerr != KCGI_OK)
2440 goto done;
2441 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2442 done:
2443 if (error == NULL && kerr != KCGI_OK)
2444 error = gw_kcgi_error(kerr);
2445 return error;
2448 static const struct got_error *
2449 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2451 const struct got_error *error = NULL;
2452 enum kcgi_err kerr = KCGI_OK;
2454 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2455 KATTR_ID, "header_author_title", KATTR__MAX);
2456 if (kerr != KCGI_OK)
2457 goto done;
2458 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2459 if (kerr != KCGI_OK)
2460 goto done;
2461 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2462 if (kerr != KCGI_OK)
2463 goto done;
2464 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2465 KATTR_ID, "header_author", KATTR__MAX);
2466 if (kerr != KCGI_OK)
2467 goto done;
2468 kerr = khtml_puts(gw_trans->gw_html_req, str);
2469 if (kerr != KCGI_OK)
2470 goto done;
2471 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2472 done:
2473 if (error == NULL && kerr != KCGI_OK)
2474 error = gw_kcgi_error(kerr);
2475 return error;
2478 static const struct got_error *
2479 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2481 const struct got_error *error = NULL;
2482 enum kcgi_err kerr = KCGI_OK;
2484 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2485 KATTR_ID, "header_committer_title", KATTR__MAX);
2486 if (kerr != KCGI_OK)
2487 goto done;
2488 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2489 if (kerr != KCGI_OK)
2490 goto done;
2491 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2492 if (kerr != KCGI_OK)
2493 goto done;
2494 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2495 KATTR_ID, "header_committer", KATTR__MAX);
2496 if (kerr != KCGI_OK)
2497 goto done;
2498 kerr = khtml_puts(gw_trans->gw_html_req, str);
2499 if (kerr != KCGI_OK)
2500 goto done;
2501 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2502 done:
2503 if (error == NULL && kerr != KCGI_OK)
2504 error = gw_kcgi_error(kerr);
2505 return error;
2508 static const struct got_error *
2509 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2511 const struct got_error *error = NULL;
2512 enum kcgi_err kerr = KCGI_OK;
2514 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2515 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2516 if (kerr != KCGI_OK)
2517 goto done;
2518 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2519 if (kerr != KCGI_OK)
2520 goto done;
2521 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2522 if (kerr != KCGI_OK)
2523 goto done;
2524 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2525 KATTR_ID, "header_commit_msg", KATTR__MAX);
2526 if (kerr != KCGI_OK)
2527 goto done;
2528 kerr = khttp_puts(gw_trans->gw_req, str);
2529 if (kerr != KCGI_OK)
2530 goto done;
2531 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2532 done:
2533 if (error == NULL && kerr != KCGI_OK)
2534 error = gw_kcgi_error(kerr);
2535 return error;
2538 static const struct got_error *
2539 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2541 const struct got_error *error = NULL;
2542 enum kcgi_err kerr = KCGI_OK;
2544 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2545 KATTR_ID, "header_tree_title", KATTR__MAX);
2546 if (kerr != KCGI_OK)
2547 goto done;
2548 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2549 if (kerr != KCGI_OK)
2550 goto done;
2551 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2552 if (kerr != KCGI_OK)
2553 goto done;
2554 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2555 KATTR_ID, "header_tree", KATTR__MAX);
2556 if (kerr != KCGI_OK)
2557 goto done;
2558 kerr = khtml_puts(gw_trans->gw_html_req, str);
2559 if (kerr != KCGI_OK)
2560 goto done;
2561 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2562 done:
2563 if (error == NULL && kerr != KCGI_OK)
2564 error = gw_kcgi_error(kerr);
2565 return error;
2568 static const struct got_error *
2569 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2570 char *dir)
2572 const struct got_error *error = NULL;
2573 FILE *f = NULL;
2574 char *d_file = NULL;
2575 unsigned int len;
2576 size_t n;
2578 *description = NULL;
2579 if (gw_trans->gw_conf->got_show_repo_description == 0)
2580 return NULL;
2582 if (asprintf(&d_file, "%s/description", dir) == -1)
2583 return got_error_from_errno("asprintf");
2585 f = fopen(d_file, "r");
2586 if (f == NULL) {
2587 if (errno == ENOENT || errno == EACCES)
2588 return NULL;
2589 error = got_error_from_errno2("fopen", d_file);
2590 goto done;
2593 if (fseek(f, 0, SEEK_END) == -1) {
2594 error = got_ferror(f, GOT_ERR_IO);
2595 goto done;
2597 len = ftell(f);
2598 if (len == -1) {
2599 error = got_ferror(f, GOT_ERR_IO);
2600 goto done;
2602 if (fseek(f, 0, SEEK_SET) == -1) {
2603 error = got_ferror(f, GOT_ERR_IO);
2604 goto done;
2606 *description = calloc(len + 1, sizeof(**description));
2607 if (*description == NULL) {
2608 error = got_error_from_errno("calloc");
2609 goto done;
2612 n = fread(*description, 1, len, f);
2613 if (n == 0 && ferror(f))
2614 error = got_ferror(f, GOT_ERR_IO);
2615 done:
2616 if (f != NULL && fclose(f) == -1 && error == NULL)
2617 error = got_error_from_errno("fclose");
2618 free(d_file);
2619 return error;
2622 static const struct got_error *
2623 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2625 struct tm tm;
2626 time_t diff_time;
2627 char *years = "years ago", *months = "months ago";
2628 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2629 char *minutes = "minutes ago", *seconds = "seconds ago";
2630 char *now = "right now";
2631 char *s;
2632 char datebuf[29];
2634 *repo_age = NULL;
2636 switch (ref_tm) {
2637 case TM_DIFF:
2638 diff_time = time(NULL) - committer_time;
2639 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2640 if (asprintf(repo_age, "%lld %s",
2641 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2642 return got_error_from_errno("asprintf");
2643 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2644 if (asprintf(repo_age, "%lld %s",
2645 (diff_time / 60 / 60 / 24 / (365 / 12)),
2646 months) == -1)
2647 return got_error_from_errno("asprintf");
2648 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2649 if (asprintf(repo_age, "%lld %s",
2650 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2651 return got_error_from_errno("asprintf");
2652 } else if (diff_time > 60 * 60 * 24 * 2) {
2653 if (asprintf(repo_age, "%lld %s",
2654 (diff_time / 60 / 60 / 24), days) == -1)
2655 return got_error_from_errno("asprintf");
2656 } else if (diff_time > 60 * 60 * 2) {
2657 if (asprintf(repo_age, "%lld %s",
2658 (diff_time / 60 / 60), hours) == -1)
2659 return got_error_from_errno("asprintf");
2660 } else if (diff_time > 60 * 2) {
2661 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2662 minutes) == -1)
2663 return got_error_from_errno("asprintf");
2664 } else if (diff_time > 2) {
2665 if (asprintf(repo_age, "%lld %s", diff_time,
2666 seconds) == -1)
2667 return got_error_from_errno("asprintf");
2668 } else {
2669 if (asprintf(repo_age, "%s", now) == -1)
2670 return got_error_from_errno("asprintf");
2672 break;
2673 case TM_LONG:
2674 if (gmtime_r(&committer_time, &tm) == NULL)
2675 return got_error_from_errno("gmtime_r");
2677 s = asctime_r(&tm, datebuf);
2678 if (s == NULL)
2679 return got_error_from_errno("asctime_r");
2681 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2682 return got_error_from_errno("asprintf");
2683 break;
2685 return NULL;
2688 static const struct got_error *
2689 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2690 const char *refname, int ref_tm)
2692 const struct got_error *error = NULL;
2693 struct got_repository *repo = NULL;
2694 struct got_commit_object *commit = NULL;
2695 struct got_reflist_head refs;
2696 struct got_reflist_entry *re;
2697 time_t committer_time = 0, cmp_time = 0;
2699 *repo_age = NULL;
2700 SIMPLEQ_INIT(&refs);
2702 if (gw_trans->gw_conf->got_show_repo_age == 0)
2703 return NULL;
2705 if (gw_trans->repo)
2706 repo = gw_trans->repo;
2707 else {
2708 error = got_repo_open(&repo, dir, NULL);
2709 if (error)
2710 return error;
2713 error = got_ref_list(&refs, repo, "refs/heads",
2714 got_ref_cmp_by_name, NULL);
2715 if (error)
2716 goto done;
2719 * Find the youngest branch tip in the repository, or the age of
2720 * the a specific branch tip if a name was provided by the caller.
2722 SIMPLEQ_FOREACH(re, &refs, entry) {
2723 struct got_object_id *id = NULL;
2725 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
2726 continue;
2728 error = got_ref_resolve(&id, repo, re->ref);
2729 if (error)
2730 goto done;
2732 error = got_object_open_as_commit(&commit, repo, id);
2733 free(id);
2734 if (error)
2735 goto done;
2737 committer_time =
2738 got_object_commit_get_committer_time(commit);
2739 got_object_commit_close(commit);
2740 if (cmp_time < committer_time)
2741 cmp_time = committer_time;
2743 if (refname)
2744 break;
2747 if (cmp_time != 0) {
2748 committer_time = cmp_time;
2749 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2751 done:
2752 got_ref_list_free(&refs);
2753 if (gw_trans->repo == NULL)
2754 got_repo_close(repo);
2755 return error;
2758 static const struct got_error *
2759 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2761 const struct got_error *error;
2762 FILE *f = NULL;
2763 struct got_object_id *id1 = NULL, *id2 = NULL;
2764 char *label1 = NULL, *label2 = NULL, *line = NULL;
2765 int obj_type;
2766 size_t linesize = 0;
2767 ssize_t linelen;
2768 enum kcgi_err kerr = KCGI_OK;
2770 f = got_opentemp();
2771 if (f == NULL)
2772 return NULL;
2774 if (header->parent_id != NULL &&
2775 strncmp(header->parent_id, "/dev/null", 9) != 0) {
2776 error = got_repo_match_object_id(&id1, &label1,
2777 header->parent_id, GOT_OBJ_TYPE_ANY, 1, gw_trans->repo);
2778 if (error)
2779 goto done;
2782 error = got_repo_match_object_id(&id2, &label2,
2783 header->commit_id, GOT_OBJ_TYPE_ANY, 1, gw_trans->repo);
2784 if (error)
2785 goto done;
2787 error = got_object_get_type(&obj_type, gw_trans->repo, id2);
2788 if (error)
2789 goto done;
2790 switch (obj_type) {
2791 case GOT_OBJ_TYPE_BLOB:
2792 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
2793 gw_trans->repo, f);
2794 break;
2795 case GOT_OBJ_TYPE_TREE:
2796 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
2797 gw_trans->repo, f);
2798 break;
2799 case GOT_OBJ_TYPE_COMMIT:
2800 error = got_diff_objects_as_commits(id1, id2, 3, 0,
2801 gw_trans->repo, f);
2802 break;
2803 default:
2804 error = got_error(GOT_ERR_OBJ_TYPE);
2806 if (error)
2807 goto done;
2809 if (fseek(f, 0, SEEK_SET) == -1) {
2810 error = got_ferror(f, GOT_ERR_IO);
2811 goto done;
2814 while ((linelen = getline(&line, &linesize, f)) != -1) {
2815 error = gw_colordiff_line(gw_trans, line);
2816 if (error)
2817 goto done;
2818 /* XXX: KHTML_PRETTY breaks this */
2819 kerr = khtml_puts(gw_trans->gw_html_req, line);
2820 if (kerr != KCGI_OK)
2821 goto done;
2822 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2823 if (kerr != KCGI_OK)
2824 goto done;
2826 if (linelen == -1 && ferror(f))
2827 error = got_error_from_errno("getline");
2828 done:
2829 if (f && fclose(f) == -1 && error == NULL)
2830 error = got_error_from_errno("fclose");
2831 free(line);
2832 free(label1);
2833 free(label2);
2834 free(id1);
2835 free(id2);
2837 if (error == NULL && kerr != KCGI_OK)
2838 error = gw_kcgi_error(kerr);
2839 return error;
2842 static const struct got_error *
2843 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2845 const struct got_error *error = NULL;
2846 struct got_repository *repo;
2847 const char *gitconfig_owner;
2849 *owner = NULL;
2851 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2852 return NULL;
2854 error = got_repo_open(&repo, dir, NULL);
2855 if (error)
2856 return error;
2857 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2858 if (gitconfig_owner) {
2859 *owner = strdup(gitconfig_owner);
2860 if (*owner == NULL)
2861 error = got_error_from_errno("strdup");
2863 got_repo_close(repo);
2864 return error;
2867 static const struct got_error *
2868 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2870 const struct got_error *error = NULL;
2871 FILE *f;
2872 char *d_file = NULL;
2873 unsigned int len;
2874 size_t n;
2876 *url = NULL;
2878 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2879 return got_error_from_errno("asprintf");
2881 f = fopen(d_file, "r");
2882 if (f == NULL) {
2883 if (errno != ENOENT && errno != EACCES)
2884 error = got_error_from_errno2("fopen", d_file);
2885 goto done;
2888 if (fseek(f, 0, SEEK_END) == -1) {
2889 error = got_ferror(f, GOT_ERR_IO);
2890 goto done;
2892 len = ftell(f);
2893 if (len == -1) {
2894 error = got_ferror(f, GOT_ERR_IO);
2895 goto done;
2897 if (fseek(f, 0, SEEK_SET) == -1) {
2898 error = got_ferror(f, GOT_ERR_IO);
2899 goto done;
2902 *url = calloc(len + 1, sizeof(**url));
2903 if (*url == NULL) {
2904 error = got_error_from_errno("calloc");
2905 goto done;
2908 n = fread(*url, 1, len, f);
2909 if (n == 0 && ferror(f))
2910 error = got_ferror(f, GOT_ERR_IO);
2911 done:
2912 if (f && fclose(f) == -1 && error == NULL)
2913 error = got_error_from_errno("fclose");
2914 free(d_file);
2915 return NULL;
2918 static const struct got_error *
2919 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
2920 int limit, int tag_type)
2922 const struct got_error *error = NULL;
2923 struct got_reflist_head refs;
2924 struct got_reflist_entry *re;
2925 char *age = NULL;
2926 char *id_str = NULL, *newline, *href_commits = NULL;
2927 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
2928 struct got_tag_object *tag = NULL;
2929 enum kcgi_err kerr = KCGI_OK;
2930 int summary_header_displayed = 0, start_tag = 0, chk_next = 0;
2931 int prev_set = 0, tag_count = 0;
2933 SIMPLEQ_INIT(&refs);
2935 error = got_ref_list(&refs, gw_trans->repo, "refs/tags",
2936 got_ref_cmp_tags, gw_trans->repo);
2937 if (error)
2938 goto done;
2940 SIMPLEQ_FOREACH(re, &refs, entry) {
2941 const char *refname;
2942 const char *tagger;
2943 const char *tag_commit;
2944 time_t tagger_time;
2945 struct got_object_id *id;
2946 struct got_commit_object *commit = NULL;
2948 refname = got_ref_get_name(re->ref);
2949 if (strncmp(refname, "refs/tags/", 10) != 0)
2950 continue;
2951 refname += 10;
2953 error = got_ref_resolve(&id, gw_trans->repo, re->ref);
2954 if (error)
2955 goto done;
2957 error = got_object_open_as_tag(&tag, gw_trans->repo, id);
2958 if (error) {
2959 if (error->code != GOT_ERR_OBJ_TYPE) {
2960 free(id);
2961 goto done;
2963 /* "lightweight" tag */
2964 error = got_object_open_as_commit(&commit,
2965 gw_trans->repo, id);
2966 if (error) {
2967 free(id);
2968 goto done;
2970 tagger = got_object_commit_get_committer(commit);
2971 tagger_time =
2972 got_object_commit_get_committer_time(commit);
2973 error = got_object_id_str(&id_str, id);
2974 free(id);
2975 } else {
2976 free(id);
2977 tagger = got_object_tag_get_tagger(tag);
2978 tagger_time = got_object_tag_get_tagger_time(tag);
2979 error = got_object_id_str(&id_str,
2980 got_object_tag_get_object_id(tag));
2982 if (error)
2983 goto done;
2985 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
2986 strlen(id_str)) != 0)
2987 continue;
2989 if (tag_type == TAGBRIEF && gw_trans->commit_id &&
2990 start_tag == 0 && strncmp(id_str, header->commit_id,
2991 strlen(id_str)) != 0) {
2992 continue;
2993 } else {
2994 start_tag = 1;
2997 tag_count++;
2999 if (prev_set == 0 && start_tag == 1) {
3000 gw_trans->next_prev_id = strdup(id_str);
3001 if (gw_trans->next_prev_id == NULL) {
3002 error = got_error_from_errno("strdup");
3003 goto done;
3005 prev_set = 1;
3008 if (chk_next) {
3009 gw_trans->next_id = strdup(id_str);
3010 if (gw_trans->next_id == NULL)
3011 error = got_error_from_errno("strdup");
3012 goto done;
3015 if (commit) {
3016 error = got_object_commit_get_logmsg(&tag_commit0,
3017 commit);
3018 if (error)
3019 goto done;
3020 got_object_commit_close(commit);
3021 } else {
3022 tag_commit0 = strdup(got_object_tag_get_message(tag));
3023 if (tag_commit0 == NULL) {
3024 error = got_error_from_errno("strdup");
3025 goto done;
3029 tag_commit = tag_commit0;
3030 while (*tag_commit == '\n')
3031 tag_commit++;
3033 switch (tag_type) {
3034 case TAGBRIEF:
3035 newline = strchr(tag_commit, '\n');
3036 if (newline)
3037 *newline = '\0';
3039 if (summary_header_displayed == 0) {
3040 kerr = khtml_attr(gw_trans->gw_html_req,
3041 KELEM_DIV, KATTR_ID,
3042 "summary_tags_title_wrapper", KATTR__MAX);
3043 if (kerr != KCGI_OK)
3044 goto done;
3045 kerr = khtml_attr(gw_trans->gw_html_req,
3046 KELEM_DIV, KATTR_ID,
3047 "summary_tags_title", KATTR__MAX);
3048 if (kerr != KCGI_OK)
3049 goto done;
3050 kerr = khtml_puts(gw_trans->gw_html_req,
3051 "Tags");
3052 if (kerr != KCGI_OK)
3053 goto done;
3054 kerr = khtml_closeelem(gw_trans->gw_html_req,
3055 2);
3056 if (kerr != KCGI_OK)
3057 goto done;
3058 kerr = khtml_attr(gw_trans->gw_html_req,
3059 KELEM_DIV, KATTR_ID,
3060 "summary_tags_content", KATTR__MAX);
3061 if (kerr != KCGI_OK)
3062 goto done;
3063 summary_header_displayed = 1;
3066 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3067 KATTR_ID, "tag_wrapper", KATTR__MAX);
3068 if (kerr != KCGI_OK)
3069 goto done;
3070 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3071 KATTR_ID, "tag_age", KATTR__MAX);
3072 if (kerr != KCGI_OK)
3073 goto done;
3074 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
3075 if (error)
3076 goto done;
3077 kerr = khtml_puts(gw_trans->gw_html_req,
3078 age ? age : "");
3079 if (kerr != KCGI_OK)
3080 goto done;
3081 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3082 if (kerr != KCGI_OK)
3083 goto done;
3084 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3085 KATTR_ID, "tag", KATTR__MAX);
3086 if (kerr != KCGI_OK)
3087 goto done;
3088 kerr = khtml_puts(gw_trans->gw_html_req, refname);
3089 if (kerr != KCGI_OK)
3090 goto done;
3091 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3092 if (kerr != KCGI_OK)
3093 goto done;
3094 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3095 KATTR_ID, "tag_name", KATTR__MAX);
3096 if (kerr != KCGI_OK)
3097 goto done;
3098 if (asprintf(&href_tag, "?path=%s&action=tag&commit=%s",
3099 gw_trans->repo_name, id_str) == -1) {
3100 error = got_error_from_errno("asprintf");
3101 goto done;
3103 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3104 KATTR_HREF, href_tag, KATTR__MAX);
3105 if (kerr != KCGI_OK)
3106 goto done;
3107 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
3108 if (kerr != KCGI_OK)
3109 goto done;
3110 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3111 if (kerr != KCGI_OK)
3112 goto done;
3114 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3115 KATTR_ID, "navs_wrapper", KATTR__MAX);
3116 if (kerr != KCGI_OK)
3117 goto done;
3118 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3119 KATTR_ID, "navs", KATTR__MAX);
3120 if (kerr != KCGI_OK)
3121 goto done;
3123 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3124 KATTR_HREF, href_tag, KATTR__MAX);
3125 if (kerr != KCGI_OK)
3126 goto done;
3127 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
3128 if (kerr != KCGI_OK)
3129 goto done;
3130 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3131 if (kerr != KCGI_OK)
3132 goto done;
3134 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3135 if (kerr != KCGI_OK)
3136 goto done;
3137 if (asprintf(&href_briefs,
3138 "?path=%s&action=briefs&commit=%s",
3139 gw_trans->repo_name, id_str) == -1) {
3140 error = got_error_from_errno("asprintf");
3141 goto done;
3143 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3144 KATTR_HREF, href_briefs, KATTR__MAX);
3145 if (kerr != KCGI_OK)
3146 goto done;
3147 kerr = khtml_puts(gw_trans->gw_html_req,
3148 "commit briefs");
3149 if (kerr != KCGI_OK)
3150 goto done;
3151 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3152 if (kerr != KCGI_OK)
3153 goto done;
3155 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3156 if (kerr != KCGI_OK)
3157 goto done;
3159 if (asprintf(&href_commits,
3160 "?path=%s&action=commits&commit=%s",
3161 gw_trans->repo_name, id_str) == -1) {
3162 error = got_error_from_errno("asprintf");
3163 goto done;
3165 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3166 KATTR_HREF, href_commits, KATTR__MAX);
3167 if (kerr != KCGI_OK)
3168 goto done;
3169 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
3170 if (kerr != KCGI_OK)
3171 goto done;
3172 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3173 if (kerr != KCGI_OK)
3174 goto done;
3176 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3177 KATTR_ID, "dotted_line", KATTR__MAX);
3178 if (kerr != KCGI_OK)
3179 goto done;
3180 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3181 if (kerr != KCGI_OK)
3182 goto done;
3183 break;
3184 case TAGFULL:
3185 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3186 KATTR_ID, "tag_info_date_title", KATTR__MAX);
3187 if (kerr != KCGI_OK)
3188 goto done;
3189 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
3190 if (kerr != KCGI_OK)
3191 goto done;
3192 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3193 if (kerr != KCGI_OK)
3194 goto done;
3195 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3196 KATTR_ID, "tag_info_date", KATTR__MAX);
3197 if (kerr != KCGI_OK)
3198 goto done;
3199 error = gw_get_time_str(&age, tagger_time, TM_LONG);
3200 if (error)
3201 goto done;
3202 kerr = khtml_puts(gw_trans->gw_html_req,
3203 age ? age : "");
3204 if (kerr != KCGI_OK)
3205 goto done;
3206 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3207 if (kerr != KCGI_OK)
3208 goto done;
3210 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3211 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
3212 if (kerr != KCGI_OK)
3213 goto done;
3214 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
3215 if (kerr != KCGI_OK)
3216 goto done;
3217 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3218 if (kerr != KCGI_OK)
3219 goto done;
3220 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3221 KATTR_ID, "tag_info_date", KATTR__MAX);
3222 if (kerr != KCGI_OK)
3223 goto done;
3224 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
3225 if (kerr != KCGI_OK)
3226 goto done;
3227 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3228 if (kerr != KCGI_OK)
3229 goto done;
3231 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3232 KATTR_ID, "tag_info", KATTR__MAX);
3233 if (kerr != KCGI_OK)
3234 goto done;
3235 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
3236 if (kerr != KCGI_OK)
3237 goto done;
3238 break;
3239 default:
3240 break;
3242 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3243 if (kerr != KCGI_OK)
3244 goto done;
3246 if (limit && --limit == 0)
3247 chk_next = 1;
3249 if (tag)
3250 got_object_tag_close(tag);
3251 tag = NULL;
3252 free(id_str);
3253 id_str = NULL;
3254 free(age);
3255 age = NULL;
3256 free(tag_commit0);
3257 tag_commit0 = NULL;
3258 free(href_tag);
3259 href_tag = NULL;
3260 free(href_briefs);
3261 href_briefs = NULL;
3262 free(href_commits);
3263 href_commits = NULL;
3265 if (tag_count == 0) {
3266 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3267 "summary_tags_title_wrapper", KATTR__MAX);
3268 if (kerr != KCGI_OK)
3269 goto done;
3270 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3271 "summary_tags_title", KATTR__MAX);
3272 if (kerr != KCGI_OK)
3273 goto done;
3274 kerr = khtml_puts(gw_trans->gw_html_req, "Tags");
3275 if (kerr != KCGI_OK)
3276 goto done;
3277 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3278 if (kerr != KCGI_OK)
3279 goto done;
3280 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3281 "summary_tags_content", KATTR__MAX);
3282 if (kerr != KCGI_OK)
3283 goto done;
3284 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3285 "tags_info", KATTR__MAX);
3286 if (kerr != KCGI_OK)
3287 goto done;
3288 kerr = khttp_puts(gw_trans->gw_req,
3289 "There are no tags for this repo.");
3290 if (kerr != KCGI_OK)
3291 goto done;
3292 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3294 done:
3295 if (tag)
3296 got_object_tag_close(tag);
3297 free(id_str);
3298 free(age);
3299 free(tag_commit0);
3300 free(href_tag);
3301 free(href_briefs);
3302 free(href_commits);
3303 got_ref_list_free(&refs);
3304 if (error == NULL && kerr != KCGI_OK)
3305 error = gw_kcgi_error(kerr);
3306 return error;
3309 static void
3310 gw_free_header(struct gw_header *header)
3312 free(header->path);
3313 free(header->author);
3314 free(header->committer);
3315 free(header->refs_str);
3316 free(header->commit_id);
3317 free(header->parent_id);
3318 free(header->tree_id);
3319 free(header->commit_msg);
3322 static struct gw_header *
3323 gw_init_header()
3325 struct gw_header *header;
3327 header = malloc(sizeof(*header));
3328 if (header == NULL)
3329 return NULL;
3331 header->path = NULL;
3332 SIMPLEQ_INIT(&header->refs);
3334 header->refs_str = NULL;
3335 header->commit_id = NULL;
3336 header->committer = NULL;
3337 header->author = NULL;
3338 header->parent_id = NULL;
3339 header->tree_id = NULL;
3340 header->commit_msg = NULL;
3342 return header;
3345 static const struct got_error *
3346 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
3347 int limit, struct got_object_id *id)
3349 const struct got_error *error = NULL;
3350 struct got_commit_graph *graph = NULL;
3351 struct got_commit_object *commit = NULL;
3352 int chk_next = 0, chk_multi = 0, prev_set = 0;
3354 error = got_commit_graph_open(&graph, header->path, 0);
3355 if (error)
3356 return error;
3358 error = got_commit_graph_iter_start(graph, id, gw_trans->repo, NULL,
3359 NULL);
3360 if (error)
3361 goto done;
3363 for (;;) {
3364 error = got_commit_graph_iter_next(&id, graph, gw_trans->repo,
3365 NULL, NULL);
3366 if (error) {
3367 if (error->code == GOT_ERR_ITER_COMPLETED)
3368 error = NULL;
3369 goto done;
3371 if (id == NULL)
3372 goto done;
3374 error = got_object_open_as_commit(&commit, gw_trans->repo, id);
3375 if (error)
3376 goto done;
3377 if (limit == 1 && chk_multi == 0 &&
3378 gw_trans->gw_conf->got_max_commits_display != 1) {
3379 error = gw_get_commit(gw_trans, header, commit, id);
3380 if (error)
3381 goto done;
3382 } else {
3383 chk_multi = 1;
3384 struct gw_header *n_header = NULL;
3385 if ((n_header = gw_init_header()) == NULL) {
3386 error = got_error_from_errno("malloc");
3387 goto done;
3389 error = got_ref_list(&n_header->refs, gw_trans->repo,
3390 NULL, got_ref_cmp_by_name, NULL);
3391 if (error)
3392 goto done;
3394 error = gw_get_commit(gw_trans, n_header, commit, id);
3395 if (error)
3396 goto done;
3397 got_ref_list_free(&n_header->refs);
3400 * we have a commit_id now, so copy it to next_prev_id
3401 * for navigation through gw_briefs and gw_commits
3403 if (gw_trans->next_prev_id == NULL && prev_set == 0 &&
3404 (gw_trans->action == GW_BRIEFS ||
3405 gw_trans->action == GW_COMMITS ||
3406 gw_trans->action == GW_SUMMARY)) {
3407 prev_set = 1;
3408 gw_trans->next_prev_id =
3409 strdup(n_header->commit_id);
3410 if (gw_trans->next_prev_id == NULL) {
3411 error = got_error_from_errno("strdup");
3412 goto done;
3417 * check for one more commit before breaking,
3418 * so we know whether to navicate through gw_briefs
3419 * gw_commits and gw_summary
3421 if (chk_next && (gw_trans->action == GW_BRIEFS||
3422 gw_trans->action == GW_COMMITS ||
3423 gw_trans->action == GW_SUMMARY)) {
3424 gw_trans->next_id = strdup(n_header->commit_id);
3425 if (gw_trans->next_id == NULL)
3426 error = got_error_from_errno("strdup");
3427 goto done;
3430 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3431 entry);
3433 if (error || (limit && --limit == 0)) {
3434 if (chk_multi == 0)
3435 break;
3436 chk_next = 1;
3439 done:
3440 if (commit != NULL)
3441 got_object_commit_close(commit);
3442 if (graph)
3443 got_commit_graph_close(graph);
3444 return error;
3447 static const struct got_error *
3448 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header,
3449 struct got_commit_object *commit, struct got_object_id *id)
3451 const struct got_error *error = NULL;
3452 struct got_reflist_entry *re;
3453 struct got_object_id *id2 = NULL;
3454 struct got_object_qid *parent_id;
3455 char *commit_msg = NULL, *commit_msg0;
3457 /*print commit*/
3458 SIMPLEQ_FOREACH(re, &header->refs, entry) {
3459 char *s;
3460 const char *name;
3461 struct got_tag_object *tag = NULL;
3462 int cmp;
3464 if (got_ref_is_symbolic(re->ref))
3465 continue;
3467 name = got_ref_get_name(re->ref);
3468 if (strncmp(name, "refs/", 5) == 0)
3469 name += 5;
3470 if (strncmp(name, "got/", 4) == 0)
3471 continue;
3472 if (strncmp(name, "heads/", 6) == 0)
3473 name += 6;
3474 if (strncmp(name, "remotes/", 8) == 0)
3475 name += 8;
3476 if (strncmp(name, "tags/", 5) == 0) {
3477 error = got_object_open_as_tag(&tag, gw_trans->repo,
3478 re->id);
3479 if (error) {
3480 if (error->code != GOT_ERR_OBJ_TYPE)
3481 continue;
3483 * Ref points at something other
3484 * than a tag.
3486 error = NULL;
3487 tag = NULL;
3490 cmp = got_object_id_cmp(tag ?
3491 got_object_tag_get_object_id(tag) : re->id, id);
3492 if (tag)
3493 got_object_tag_close(tag);
3494 if (cmp != 0)
3495 continue;
3496 s = header->refs_str;
3497 if (asprintf(&header->refs_str, "%s%s%s", s ? s : "",
3498 s ? ", " : "", name) == -1) {
3499 error = got_error_from_errno("asprintf");
3500 free(s);
3501 header->refs_str = NULL;
3502 return error;
3504 free(s);
3507 error = got_object_id_str(&header->commit_id, id);
3508 if (error)
3509 return error;
3511 error = got_object_id_str(&header->tree_id,
3512 got_object_commit_get_tree_id(commit));
3513 if (error)
3514 return error;
3516 if (gw_trans->action == GW_DIFF) {
3517 parent_id = SIMPLEQ_FIRST(
3518 got_object_commit_get_parent_ids(commit));
3519 if (parent_id != NULL) {
3520 id2 = got_object_id_dup(parent_id->id);
3521 free (parent_id);
3522 error = got_object_id_str(&header->parent_id, id2);
3523 if (error)
3524 return error;
3525 free(id2);
3526 } else {
3527 header->parent_id = strdup("/dev/null");
3528 if (header->parent_id == NULL) {
3529 error = got_error_from_errno("strdup");
3530 return error;
3535 header->committer_time =
3536 got_object_commit_get_committer_time(commit);
3538 header->author =
3539 strdup(got_object_commit_get_author(commit));
3540 if (header->author == NULL) {
3541 error = got_error_from_errno("strdup");
3542 return error;
3544 header->committer =
3545 strdup(got_object_commit_get_committer(commit));
3546 if (header->committer == NULL) {
3547 error = got_error_from_errno("strdup");
3548 return error;
3550 error = got_object_commit_get_logmsg(&commit_msg0, commit);
3551 if (error)
3552 return error;
3554 commit_msg = commit_msg0;
3555 while (*commit_msg == '\n')
3556 commit_msg++;
3558 header->commit_msg = strdup(commit_msg);
3559 if (header->commit_msg == NULL)
3560 error = got_error_from_errno("strdup");
3561 free(commit_msg0);
3562 return error;
3565 static const struct got_error *
3566 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3568 const struct got_error *error = NULL;
3569 char *in_repo_path = NULL;
3570 struct got_object_id *id = NULL;
3572 error = got_repo_open(&gw_trans->repo, gw_trans->repo_path, NULL);
3573 if (error)
3574 return error;
3576 if (gw_trans->commit_id == NULL) {
3577 struct got_reference *head_ref;
3578 error = got_ref_open(&head_ref, gw_trans->repo,
3579 gw_trans->headref, 0);
3580 if (error)
3581 return error;
3583 error = got_ref_resolve(&id, gw_trans->repo, head_ref);
3584 got_ref_close(head_ref);
3585 if (error)
3586 return error;
3587 } else {
3588 struct got_reference *ref;
3590 error = got_ref_open(&ref, gw_trans->repo,
3591 gw_trans->commit_id, 0);
3592 if (error == NULL) {
3593 int obj_type;
3594 error = got_ref_resolve(&id, gw_trans->repo, ref);
3595 got_ref_close(ref);
3596 if (error)
3597 return error;
3598 error = got_object_get_type(&obj_type, gw_trans->repo,
3599 id);
3600 if (error)
3601 goto done;
3602 if (obj_type == GOT_OBJ_TYPE_TAG) {
3603 struct got_tag_object *tag;
3604 error = got_object_open_as_tag(&tag,
3605 gw_trans->repo, id);
3606 if (error)
3607 goto done;
3608 if (got_object_tag_get_object_type(tag) !=
3609 GOT_OBJ_TYPE_COMMIT) {
3610 got_object_tag_close(tag);
3611 error = got_error(GOT_ERR_OBJ_TYPE);
3612 goto done;
3614 free(id);
3615 id = got_object_id_dup(
3616 got_object_tag_get_object_id(tag));
3617 if (id == NULL)
3618 error = got_error_from_errno(
3619 "got_object_id_dup");
3620 got_object_tag_close(tag);
3621 if (error)
3622 goto done;
3623 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3624 error = got_error(GOT_ERR_OBJ_TYPE);
3625 goto done;
3628 error = got_repo_match_object_id_prefix(&id,
3629 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT,
3630 gw_trans->repo);
3631 if (error)
3632 goto done;
3635 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
3636 gw_trans->repo_path, 1);
3637 if (error)
3638 goto done;
3640 if (in_repo_path) {
3641 header->path = strdup(in_repo_path);
3642 if (header->path == NULL) {
3643 error = got_error_from_errno("strdup");
3644 goto done;
3648 error = got_ref_list(&header->refs, gw_trans->repo, NULL,
3649 got_ref_cmp_by_name, NULL);
3650 if (error)
3651 goto done;
3653 error = gw_get_commits(gw_trans, header, limit, id);
3654 done:
3655 got_ref_list_free(&header->refs);
3656 free(id);
3657 free(in_repo_path);
3658 return error;
3661 struct blame_line {
3662 int annotated;
3663 char *id_str;
3664 char *committer;
3665 char datebuf[11]; /* YYYY-MM-DD + NUL */
3668 struct gw_blame_cb_args {
3669 struct blame_line *lines;
3670 int nlines;
3671 int nlines_prec;
3672 int lineno_cur;
3673 off_t *line_offsets;
3674 FILE *f;
3675 struct got_repository *repo;
3676 struct gw_trans *gw_trans;
3679 static const struct got_error *
3680 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3682 const struct got_error *err = NULL;
3683 struct gw_blame_cb_args *a = arg;
3684 struct blame_line *bline;
3685 char *line = NULL;
3686 size_t linesize = 0;
3687 struct got_commit_object *commit = NULL;
3688 off_t offset;
3689 struct tm tm;
3690 time_t committer_time;
3691 enum kcgi_err kerr = KCGI_OK;
3693 if (nlines != a->nlines ||
3694 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3695 return got_error(GOT_ERR_RANGE);
3697 if (lineno == -1)
3698 return NULL; /* no change in this commit */
3700 /* Annotate this line. */
3701 bline = &a->lines[lineno - 1];
3702 if (bline->annotated)
3703 return NULL;
3704 err = got_object_id_str(&bline->id_str, id);
3705 if (err)
3706 return err;
3708 err = got_object_open_as_commit(&commit, a->repo, id);
3709 if (err)
3710 goto done;
3712 bline->committer = strdup(got_object_commit_get_committer(commit));
3713 if (bline->committer == NULL) {
3714 err = got_error_from_errno("strdup");
3715 goto done;
3718 committer_time = got_object_commit_get_committer_time(commit);
3719 if (localtime_r(&committer_time, &tm) == NULL)
3720 return got_error_from_errno("localtime_r");
3721 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3722 &tm) >= sizeof(bline->datebuf)) {
3723 err = got_error(GOT_ERR_NO_SPACE);
3724 goto done;
3726 bline->annotated = 1;
3728 /* Print lines annotated so far. */
3729 bline = &a->lines[a->lineno_cur - 1];
3730 if (!bline->annotated)
3731 goto done;
3733 offset = a->line_offsets[a->lineno_cur - 1];
3734 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3735 err = got_error_from_errno("fseeko");
3736 goto done;
3739 while (bline->annotated) {
3740 char *smallerthan, *at, *nl, *committer;
3741 char *href_diff = NULL;
3742 size_t len;
3744 if (getline(&line, &linesize, a->f) == -1) {
3745 if (ferror(a->f))
3746 err = got_error_from_errno("getline");
3747 break;
3750 committer = bline->committer;
3751 smallerthan = strchr(committer, '<');
3752 if (smallerthan && smallerthan[1] != '\0')
3753 committer = smallerthan + 1;
3754 at = strchr(committer, '@');
3755 if (at)
3756 *at = '\0';
3757 len = strlen(committer);
3758 if (len >= 9)
3759 committer[8] = '\0';
3761 nl = strchr(line, '\n');
3762 if (nl)
3763 *nl = '\0';
3765 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3766 "blame_wrapper", KATTR__MAX);
3767 if (kerr != KCGI_OK)
3768 goto err;
3769 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3770 "blame_number", KATTR__MAX);
3771 if (kerr != KCGI_OK)
3772 goto err;
3773 kerr = khtml_printf(a->gw_trans->gw_html_req, "%.*d",
3774 a->nlines_prec, a->lineno_cur);
3775 if (kerr != KCGI_OK)
3776 goto err;
3777 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3778 if (kerr != KCGI_OK)
3779 goto err;
3781 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3782 "blame_hash", KATTR__MAX);
3783 if (kerr != KCGI_OK)
3784 goto err;
3786 if (asprintf(&href_diff,
3787 "?path=%s&action=diff&commit=%s",
3788 a->gw_trans->repo_name, bline->id_str) == -1) {
3789 err = got_error_from_errno("asprintf");
3790 goto err;
3792 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3793 KATTR_HREF, href_diff, KATTR__MAX);
3794 if (kerr != KCGI_OK)
3795 goto done;
3796 kerr = khtml_printf(a->gw_trans->gw_html_req, "%.8s",
3797 bline->id_str);
3798 if (kerr != KCGI_OK)
3799 goto err;
3800 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3801 if (kerr != KCGI_OK)
3802 goto err;
3804 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3805 "blame_date", KATTR__MAX);
3806 if (kerr != KCGI_OK)
3807 goto err;
3808 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3809 if (kerr != KCGI_OK)
3810 goto err;
3811 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3812 if (kerr != KCGI_OK)
3813 goto err;
3815 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3816 "blame_author", KATTR__MAX);
3817 if (kerr != KCGI_OK)
3818 goto err;
3819 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
3820 if (kerr != KCGI_OK)
3821 goto err;
3822 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3823 if (kerr != KCGI_OK)
3824 goto err;
3826 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3827 "blame_code", KATTR__MAX);
3828 if (kerr != KCGI_OK)
3829 goto err;
3830 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
3831 if (kerr != KCGI_OK)
3832 goto err;
3833 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3834 if (kerr != KCGI_OK)
3835 goto err;
3837 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3838 if (kerr != KCGI_OK)
3839 goto err;
3841 a->lineno_cur++;
3842 bline = &a->lines[a->lineno_cur - 1];
3843 err:
3844 free(href_diff);
3846 done:
3847 if (commit)
3848 got_object_commit_close(commit);
3849 free(line);
3850 if (err == NULL && kerr != KCGI_OK)
3851 err = gw_kcgi_error(kerr);
3852 return err;
3855 static const struct got_error *
3856 gw_output_file_blame(struct gw_trans *gw_trans)
3858 const struct got_error *error = NULL;
3859 struct got_object_id *obj_id = NULL;
3860 struct got_object_id *commit_id = NULL;
3861 struct got_blob_object *blob = NULL;
3862 char *path = NULL, *in_repo_path = NULL;
3863 struct gw_blame_cb_args bca;
3864 int i, obj_type;
3865 size_t filesize;
3867 if (asprintf(&path, "%s%s%s",
3868 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3869 gw_trans->repo_folder ? "/" : "",
3870 gw_trans->repo_file) == -1) {
3871 error = got_error_from_errno("asprintf");
3872 goto done;
3875 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path, 1);
3876 if (error)
3877 goto done;
3879 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
3880 GOT_OBJ_TYPE_COMMIT, 1, gw_trans->repo);
3881 if (error)
3882 goto done;
3884 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
3885 in_repo_path);
3886 if (error)
3887 goto done;
3889 if (obj_id == NULL) {
3890 error = got_error(GOT_ERR_NO_OBJ);
3891 goto done;
3894 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
3895 if (error)
3896 goto done;
3898 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3899 error = got_error(GOT_ERR_OBJ_TYPE);
3900 goto done;
3903 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
3904 if (error)
3905 goto done;
3907 bca.f = got_opentemp();
3908 if (bca.f == NULL) {
3909 error = got_error_from_errno("got_opentemp");
3910 goto done;
3912 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3913 &bca.line_offsets, bca.f, blob);
3914 if (error || bca.nlines == 0)
3915 goto done;
3917 /* Don't include \n at EOF in the blame line count. */
3918 if (bca.line_offsets[bca.nlines - 1] == filesize)
3919 bca.nlines--;
3921 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3922 if (bca.lines == NULL) {
3923 error = got_error_from_errno("calloc");
3924 goto done;
3926 bca.lineno_cur = 1;
3927 bca.nlines_prec = 0;
3928 i = bca.nlines;
3929 while (i > 0) {
3930 i /= 10;
3931 bca.nlines_prec++;
3933 bca.repo = gw_trans->repo;
3934 bca.gw_trans = gw_trans;
3936 error = got_blame(in_repo_path, commit_id, gw_trans->repo, gw_blame_cb,
3937 &bca, NULL, NULL);
3938 done:
3939 free(in_repo_path);
3940 free(commit_id);
3941 free(obj_id);
3942 free(path);
3944 if (blob) {
3945 free(bca.line_offsets);
3946 for (i = 0; i < bca.nlines; i++) {
3947 struct blame_line *bline = &bca.lines[i];
3948 free(bline->id_str);
3949 free(bline->committer);
3951 free(bca.lines);
3952 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3953 error = got_error_from_errno("fclose");
3955 if (blob)
3956 got_object_blob_close(blob);
3957 return error;
3960 static const struct got_error *
3961 gw_output_blob_buf(struct gw_trans *gw_trans)
3963 const struct got_error *error = NULL;
3964 struct got_object_id *obj_id = NULL;
3965 struct got_object_id *commit_id = NULL;
3966 struct got_blob_object *blob = NULL;
3967 char *path = NULL, *in_repo_path = NULL;
3968 int obj_type, set_mime = 0;
3969 size_t len, hdrlen;
3970 const uint8_t *buf;
3971 enum kcgi_err kerr = KCGI_OK;
3973 if (asprintf(&path, "%s%s%s",
3974 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3975 gw_trans->repo_folder ? "/" : "",
3976 gw_trans->repo_file) == -1) {
3977 error = got_error_from_errno("asprintf");
3978 goto done;
3981 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path, 1);
3982 if (error)
3983 goto done;
3985 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
3986 GOT_OBJ_TYPE_COMMIT, 1, gw_trans->repo);
3987 if (error)
3988 goto done;
3990 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
3991 in_repo_path);
3992 if (error)
3993 goto done;
3995 if (obj_id == NULL) {
3996 error = got_error(GOT_ERR_NO_OBJ);
3997 goto done;
4000 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
4001 if (error)
4002 goto done;
4004 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4005 error = got_error(GOT_ERR_OBJ_TYPE);
4006 goto done;
4009 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
4010 if (error)
4011 goto done;
4013 hdrlen = got_object_blob_get_hdrlen(blob);
4014 do {
4015 error = got_object_blob_read_block(&len, blob);
4016 if (error)
4017 goto done;
4018 buf = got_object_blob_get_read_buf(blob);
4021 * Skip blob object header first time around,
4022 * which also contains a zero byte.
4024 buf += hdrlen;
4025 if (set_mime == 0) {
4026 if (isbinary(buf, len - hdrlen))
4027 gw_trans->mime = KMIME_APP_OCTET_STREAM;
4028 else
4029 gw_trans->mime = KMIME_TEXT_PLAIN;
4030 set_mime = 1;
4031 error = gw_display_index(gw_trans);
4032 if (error)
4033 goto done;
4035 kerr = khttp_write(gw_trans->gw_req, buf, len - hdrlen);
4036 if (kerr != KCGI_OK)
4037 goto done;
4038 hdrlen = 0;
4039 } while (len != 0);
4040 done:
4041 free(in_repo_path);
4042 free(commit_id);
4043 free(obj_id);
4044 free(path);
4045 if (blob)
4046 got_object_blob_close(blob);
4047 if (error == NULL && kerr != KCGI_OK)
4048 error = gw_kcgi_error(kerr);
4049 return error;
4052 static const struct got_error *
4053 gw_output_repo_tree(struct gw_trans *gw_trans)
4055 const struct got_error *error = NULL;
4056 struct got_object_id *tree_id = NULL, *commit_id = NULL;
4057 struct got_tree_object *tree = NULL;
4058 char *path = NULL, *in_repo_path = NULL;
4059 char *id_str = NULL;
4060 char *build_folder = NULL;
4061 char *href_blob = NULL, *href_blame = NULL;
4062 const char *class = NULL;
4063 int nentries, i, class_flip = 0;
4064 enum kcgi_err kerr = KCGI_OK;
4066 if (gw_trans->repo_folder != NULL) {
4067 path = strdup(gw_trans->repo_folder);
4068 if (path == NULL) {
4069 error = got_error_from_errno("strdup");
4070 goto done;
4072 } else {
4073 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
4074 gw_trans->repo_path, 1);
4075 if (error)
4076 goto done;
4077 free(path);
4078 path = in_repo_path;
4081 if (gw_trans->commit_id == NULL) {
4082 struct got_reference *head_ref;
4083 error = got_ref_open(&head_ref, gw_trans->repo,
4084 gw_trans->headref, 0);
4085 if (error)
4086 goto done;
4087 error = got_ref_resolve(&commit_id, gw_trans->repo, head_ref);
4088 if (error)
4089 goto done;
4090 got_ref_close(head_ref);
4092 * gw_trans->commit_id was not parsed from the querystring
4093 * we hit this code path from gw_index, where we don't know the
4094 * commit values for the tree link yet, so set
4095 * gw_trans->commit_id here to continue further into the tree
4097 error = got_object_id_str(&gw_trans->commit_id, commit_id);
4098 if (error)
4099 goto done;
4101 } else {
4102 error = got_repo_match_object_id(&commit_id, NULL,
4103 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT, 1,
4104 gw_trans->repo);
4105 if (error)
4106 goto done;
4109 error = got_object_id_by_path(&tree_id, gw_trans->repo, commit_id,
4110 path);
4111 if (error)
4112 goto done;
4114 error = got_object_open_as_tree(&tree, gw_trans->repo, tree_id);
4115 if (error)
4116 goto done;
4118 nentries = got_object_tree_get_nentries(tree);
4119 for (i = 0; i < nentries; i++) {
4120 struct got_tree_entry *te;
4121 const char *modestr = "";
4122 mode_t mode;
4124 te = got_object_tree_get_entry(tree, i);
4126 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
4127 if (error)
4128 goto done;
4130 mode = got_tree_entry_get_mode(te);
4131 if (got_object_tree_entry_is_submodule(te))
4132 modestr = "$";
4133 else if (S_ISLNK(mode))
4134 modestr = "@";
4135 else if (S_ISDIR(mode))
4136 modestr = "/";
4137 else if (mode & S_IXUSR)
4138 modestr = "*";
4140 if (class_flip == 0) {
4141 class = "back_lightgray";
4142 class_flip = 1;
4143 } else {
4144 class = "back_white";
4145 class_flip = 0;
4148 if (S_ISDIR(mode)) {
4149 if (asprintf(&build_folder, "%s/%s",
4150 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4151 got_tree_entry_get_name(te)) == -1) {
4152 error = got_error_from_errno("asprintf");
4153 goto done;
4155 if (asprintf(&href_blob,
4156 "?path=%s&action=%s&commit=%s&folder=%s",
4157 gw_trans->repo_name, gw_get_action_name(gw_trans),
4158 gw_trans->commit_id, build_folder) == -1) {
4159 error = got_error_from_errno("asprintf");
4160 goto done;
4163 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4164 KATTR_ID, "tree_wrapper", KATTR__MAX);
4165 if (kerr != KCGI_OK)
4166 goto done;
4167 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4168 KATTR_ID, "tree_line", KATTR_CLASS, class,
4169 KATTR__MAX);
4170 if (kerr != KCGI_OK)
4171 goto done;
4172 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4173 KATTR_HREF, href_blob, KATTR_CLASS,
4174 "diff_directory", KATTR__MAX);
4175 if (kerr != KCGI_OK)
4176 goto done;
4177 kerr = khtml_printf(gw_trans->gw_html_req, "%s%s",
4178 got_tree_entry_get_name(te), modestr);
4179 if (kerr != KCGI_OK)
4180 goto done;
4181 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4182 if (kerr != KCGI_OK)
4183 goto done;
4184 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4185 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
4186 KATTR__MAX);
4187 if (kerr != KCGI_OK)
4188 goto done;
4189 kerr = khtml_entity(gw_trans->gw_html_req,
4190 KENTITY_nbsp);
4191 if (kerr != KCGI_OK)
4192 goto done;
4193 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4194 if (kerr != KCGI_OK)
4195 goto done;
4196 } else {
4197 if (asprintf(&href_blob,
4198 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
4199 gw_trans->repo_name, "blob", gw_trans->commit_id,
4200 got_tree_entry_get_name(te),
4201 gw_trans->repo_folder ?
4202 gw_trans->repo_folder : "") == -1) {
4203 error = got_error_from_errno("asprintf");
4204 goto done;
4206 if (asprintf(&href_blame,
4207 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
4208 gw_trans->repo_name, "blame", gw_trans->commit_id,
4209 got_tree_entry_get_name(te),
4210 gw_trans->repo_folder ?
4211 gw_trans->repo_folder : "") == -1) {
4212 error = got_error_from_errno("asprintf");
4213 goto done;
4216 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4217 KATTR_ID, "tree_wrapper", KATTR__MAX);
4218 if (kerr != KCGI_OK)
4219 goto done;
4220 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4221 KATTR_ID, "tree_line", KATTR_CLASS, class,
4222 KATTR__MAX);
4223 if (kerr != KCGI_OK)
4224 goto done;
4225 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4226 KATTR_HREF, href_blob, KATTR__MAX);
4227 if (kerr != KCGI_OK)
4228 goto done;
4229 kerr = khtml_printf(gw_trans->gw_html_req, "%s%s",
4230 got_tree_entry_get_name(te), modestr);
4231 if (kerr != KCGI_OK)
4232 goto done;
4233 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4234 if (kerr != KCGI_OK)
4235 goto done;
4236 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4237 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
4238 KATTR__MAX);
4239 if (kerr != KCGI_OK)
4240 goto done;
4242 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4243 KATTR_HREF, href_blob, KATTR__MAX);
4244 if (kerr != KCGI_OK)
4245 goto done;
4246 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
4247 if (kerr != KCGI_OK)
4248 goto done;
4249 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4250 if (kerr != KCGI_OK)
4251 goto done;
4253 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4254 if (kerr != KCGI_OK)
4255 goto done;
4257 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4258 KATTR_HREF, href_blame, KATTR__MAX);
4259 if (kerr != KCGI_OK)
4260 goto done;
4261 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
4262 if (kerr != KCGI_OK)
4263 goto done;
4265 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4266 if (kerr != KCGI_OK)
4267 goto done;
4269 free(id_str);
4270 id_str = NULL;
4271 free(href_blob);
4272 href_blob = NULL;
4273 free(build_folder);
4274 build_folder = NULL;
4276 done:
4277 if (tree)
4278 got_object_tree_close(tree);
4279 free(id_str);
4280 free(href_blob);
4281 free(href_blame);
4282 free(in_repo_path);
4283 free(tree_id);
4284 free(build_folder);
4285 if (error == NULL && kerr != KCGI_OK)
4286 error = gw_kcgi_error(kerr);
4287 return error;
4290 static const struct got_error *
4291 gw_output_repo_heads(struct gw_trans *gw_trans)
4293 const struct got_error *error = NULL;
4294 struct got_reflist_head refs;
4295 struct got_reflist_entry *re;
4296 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
4297 char *href_commits = NULL;
4298 enum kcgi_err kerr = KCGI_OK;
4300 SIMPLEQ_INIT(&refs);
4302 error = got_ref_list(&refs, gw_trans->repo, "refs/heads",
4303 got_ref_cmp_by_name, NULL);
4304 if (error)
4305 goto done;
4307 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4308 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
4309 if (kerr != KCGI_OK)
4310 goto done;
4311 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4312 KATTR_ID, "summary_heads_title", KATTR__MAX);
4313 if (kerr != KCGI_OK)
4314 goto done;
4315 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
4316 if (kerr != KCGI_OK)
4317 goto done;
4318 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4319 if (kerr != KCGI_OK)
4320 goto done;
4321 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4322 KATTR_ID, "summary_heads_content", KATTR__MAX);
4323 if (kerr != KCGI_OK)
4324 goto done;
4326 SIMPLEQ_FOREACH(re, &refs, entry) {
4327 const char *refname;
4329 if (got_ref_is_symbolic(re->ref))
4330 continue;
4332 refname = got_ref_get_name(re->ref);
4333 if (strncmp(refname, "refs/heads/", 11) != 0)
4334 continue;
4336 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
4337 refname, TM_DIFF);
4338 if (error)
4339 goto done;
4341 if (strncmp(refname, "refs/heads/", 11) == 0)
4342 refname += 11;
4344 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4345 KATTR_ID, "heads_wrapper", KATTR__MAX);
4346 if (kerr != KCGI_OK)
4347 goto done;
4348 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4349 KATTR_ID, "heads_age", KATTR__MAX);
4350 if (kerr != KCGI_OK)
4351 goto done;
4352 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
4353 if (kerr != KCGI_OK)
4354 goto done;
4355 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4356 if (kerr != KCGI_OK)
4357 goto done;
4358 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4359 KATTR_ID, "heads_space", KATTR__MAX);
4360 if (kerr != KCGI_OK)
4361 goto done;
4362 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
4363 if (kerr != KCGI_OK)
4364 goto done;
4365 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4366 if (kerr != KCGI_OK)
4367 goto done;
4368 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4369 KATTR_ID, "head", KATTR__MAX);
4370 if (kerr != KCGI_OK)
4371 goto done;
4372 if (asprintf(&href_summary,
4373 "?path=%s&action=summary&headref=%s",
4374 gw_trans->repo_name, refname) == -1) {
4375 error = got_error_from_errno("asprintf");
4376 goto done;
4378 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4379 href_summary, KATTR__MAX);
4380 kerr = khtml_puts(gw_trans->gw_html_req, refname);
4381 if (kerr != KCGI_OK)
4382 goto done;
4383 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4384 if (kerr != KCGI_OK)
4385 goto done;
4387 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4388 "navs_wrapper", KATTR__MAX);
4389 if (kerr != KCGI_OK)
4390 goto done;
4391 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4392 "navs", KATTR__MAX);
4393 if (kerr != KCGI_OK)
4394 goto done;
4396 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4397 href_summary, KATTR__MAX);
4398 if (kerr != KCGI_OK)
4399 goto done;
4400 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4401 if (kerr != KCGI_OK)
4402 goto done;
4403 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4404 if (kerr != KCGI_OK)
4405 goto done;
4407 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4408 if (kerr != KCGI_OK)
4409 goto done;
4410 if (asprintf(&href_briefs, "?path=%s&action=briefs&headref=%s",
4411 gw_trans->repo_name, refname) == -1) {
4412 error = got_error_from_errno("asprintf");
4413 goto done;
4415 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4416 href_briefs, KATTR__MAX);
4417 if (kerr != KCGI_OK)
4418 goto done;
4419 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4420 if (kerr != KCGI_OK)
4421 goto done;
4422 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4423 if (kerr != KCGI_OK)
4424 goto done;
4426 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4427 if (kerr != KCGI_OK)
4428 goto done;
4430 if (asprintf(&href_commits,
4431 "?path=%s&action=commits&headref=%s",
4432 gw_trans->repo_name, refname) == -1) {
4433 error = got_error_from_errno("asprintf");
4434 goto done;
4436 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4437 href_commits, KATTR__MAX);
4438 if (kerr != KCGI_OK)
4439 goto done;
4440 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4441 if (kerr != KCGI_OK)
4442 goto done;
4443 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4444 if (kerr != KCGI_OK)
4445 goto done;
4447 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4448 "dotted_line", KATTR__MAX);
4449 if (kerr != KCGI_OK)
4450 goto done;
4451 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4452 if (kerr != KCGI_OK)
4453 goto done;
4454 free(href_summary);
4455 href_summary = NULL;
4456 free(href_briefs);
4457 href_briefs = NULL;
4458 free(href_commits);
4459 href_commits = NULL;
4461 done:
4462 got_ref_list_free(&refs);
4463 free(href_summary);
4464 free(href_briefs);
4465 free(href_commits);
4466 return error;
4469 static const struct got_error *
4470 gw_output_site_link(struct gw_trans *gw_trans)
4472 const struct got_error *error = NULL;
4473 char *href_summary = NULL;
4474 enum kcgi_err kerr = KCGI_OK;
4476 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4477 "site_link", KATTR__MAX);
4478 if (kerr != KCGI_OK)
4479 goto done;
4480 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4481 KATTR__MAX);
4482 if (kerr != KCGI_OK)
4483 goto done;
4484 kerr = khtml_puts(gw_trans->gw_html_req,
4485 gw_trans->gw_conf->got_site_link);
4486 if (kerr != KCGI_OK)
4487 goto done;
4488 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4489 if (kerr != KCGI_OK)
4490 goto done;
4492 if (gw_trans->repo_name != NULL) {
4493 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4494 if (kerr != KCGI_OK)
4495 goto done;
4496 if (asprintf(&href_summary, "?path=%s&action=summary",
4497 gw_trans->repo_name) == -1)
4498 goto done;
4499 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4500 href_summary, KATTR__MAX);
4501 if (kerr != KCGI_OK)
4502 goto done;
4503 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4504 if (kerr != KCGI_OK)
4505 goto done;
4506 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4507 if (kerr != KCGI_OK)
4508 goto done;
4509 kerr = khtml_printf(gw_trans->gw_html_req, " / %s",
4510 gw_get_action_name(gw_trans));
4511 if (kerr != KCGI_OK)
4512 goto done;
4515 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4516 if (kerr != KCGI_OK)
4517 goto done;
4518 done:
4519 free(href_summary);
4520 return error;
4523 static const struct got_error *
4524 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4526 const struct got_error *error = NULL;
4527 char *color = NULL;
4528 enum kcgi_err kerr = KCGI_OK;
4530 if (strncmp(buf, "-", 1) == 0)
4531 color = "diff_minus";
4532 else if (strncmp(buf, "+", 1) == 0)
4533 color = "diff_plus";
4534 else if (strncmp(buf, "@@", 2) == 0)
4535 color = "diff_chunk_header";
4536 else if (strncmp(buf, "@@", 2) == 0)
4537 color = "diff_chunk_header";
4538 else if (strncmp(buf, "commit +", 8) == 0)
4539 color = "diff_meta";
4540 else if (strncmp(buf, "commit -", 8) == 0)
4541 color = "diff_meta";
4542 else if (strncmp(buf, "blob +", 6) == 0)
4543 color = "diff_meta";
4544 else if (strncmp(buf, "blob -", 6) == 0)
4545 color = "diff_meta";
4546 else if (strncmp(buf, "file +", 6) == 0)
4547 color = "diff_meta";
4548 else if (strncmp(buf, "file -", 6) == 0)
4549 color = "diff_meta";
4550 else if (strncmp(buf, "from:", 5) == 0)
4551 color = "diff_author";
4552 else if (strncmp(buf, "via:", 4) == 0)
4553 color = "diff_author";
4554 else if (strncmp(buf, "date:", 5) == 0)
4555 color = "diff_date";
4556 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4557 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4558 if (error == NULL && kerr != KCGI_OK)
4559 error = gw_kcgi_error(kerr);
4560 return error;
4563 int
4564 main(int argc, char *argv[])
4566 const struct got_error *error = NULL;
4567 struct gw_trans *gw_trans;
4568 struct gw_dir *dir = NULL, *tdir;
4569 const char *page = "index";
4570 int gw_malloc = 1;
4571 enum kcgi_err kerr = KCGI_OK;
4573 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4574 errx(1, "malloc");
4576 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4577 errx(1, "malloc");
4579 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4580 errx(1, "malloc");
4582 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4583 errx(1, "malloc");
4585 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4586 if (kerr != KCGI_OK) {
4587 error = gw_kcgi_error(kerr);
4588 goto done;
4591 if ((gw_trans->gw_conf =
4592 malloc(sizeof(struct gotweb_conf))) == NULL) {
4593 gw_malloc = 0;
4594 error = got_error_from_errno("malloc");
4595 goto done;
4598 TAILQ_INIT(&gw_trans->gw_dirs);
4599 TAILQ_INIT(&gw_trans->gw_headers);
4601 gw_trans->action = -1;
4602 gw_trans->page = 0;
4603 gw_trans->repos_total = 0;
4604 gw_trans->repo_path = NULL;
4605 gw_trans->commit_id = NULL;
4606 gw_trans->next_id = NULL;
4607 gw_trans->next_prev_id = NULL;
4608 gw_trans->prev_id = NULL;
4609 gw_trans->prev_prev_id = NULL;
4610 gw_trans->headref = GOT_REF_HEAD;
4611 gw_trans->mime = KMIME_TEXT_HTML;
4612 gw_trans->gw_tmpl->key = gw_templs;
4613 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4614 gw_trans->gw_tmpl->arg = gw_trans;
4615 gw_trans->gw_tmpl->cb = gw_template;
4616 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
4617 if (error)
4618 goto done;
4620 error = gw_parse_querystring(gw_trans);
4621 if (error)
4622 goto done;
4624 if (gw_trans->action == GW_BLOB)
4625 error = gw_blob(gw_trans);
4626 else
4627 error = gw_display_index(gw_trans);
4628 done:
4629 if (gw_malloc) {
4630 free(gw_trans->gw_conf->got_repos_path);
4631 free(gw_trans->gw_conf->got_site_name);
4632 free(gw_trans->gw_conf->got_site_owner);
4633 free(gw_trans->gw_conf->got_site_link);
4634 free(gw_trans->gw_conf->got_logo);
4635 free(gw_trans->gw_conf->got_logo_url);
4636 free(gw_trans->gw_conf);
4637 free(gw_trans->commit_id);
4638 free(gw_trans->next_id);
4639 free(gw_trans->next_prev_id);
4640 free(gw_trans->prev_id);
4641 free(gw_trans->prev_prev_id);
4642 free(gw_trans->repo_path);
4643 if (gw_trans->repo)
4644 got_repo_close(gw_trans->repo);
4646 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4647 free(dir->name);
4648 free(dir->description);
4649 free(dir->age);
4650 free(dir->url);
4651 free(dir->path);
4652 free(dir);
4657 khttp_free(gw_trans->gw_req);
4658 return 0;