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_puts(gw_trans->gw_html_req,
639 "No repositories found in ");
640 if (kerr != KCGI_OK)
641 return gw_kcgi_error(kerr);
642 kerr = khtml_puts(gw_trans->gw_html_req,
643 gw_trans->gw_conf->got_repos_path);
644 if (kerr != KCGI_OK)
645 return gw_kcgi_error(kerr);
646 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
647 if (kerr != KCGI_OK)
648 return gw_kcgi_error(kerr);
649 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
650 "dotted_line", KATTR__MAX);
651 if (kerr != KCGI_OK)
652 return gw_kcgi_error(kerr);
653 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
654 if (kerr != KCGI_OK)
655 return gw_kcgi_error(kerr);
656 return error;
659 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
660 dir_c++;
662 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
663 if (gw_trans->page > 0 && (gw_trans->page *
664 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
665 prev_disp++;
666 continue;
669 prev_disp++;
671 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
672 "index_wrapper", KATTR__MAX);
673 if (kerr != KCGI_OK)
674 goto done;
676 if (asprintf(&href_summary, "?path=%s&action=summary",
677 gw_dir->name) == -1) {
678 error = got_error_from_errno("asprintf");
679 goto done;
681 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
682 "index_project", KATTR__MAX);
683 if (kerr != KCGI_OK)
684 goto done;
685 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
686 href_summary, KATTR__MAX);
687 if (kerr != KCGI_OK)
688 goto done;
689 kerr = khtml_puts(gw_trans->gw_html_req, gw_dir->name);
690 if (kerr != KCGI_OK)
691 goto done;
692 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
693 if (kerr != KCGI_OK)
694 goto done;
695 if (gw_trans->gw_conf->got_show_repo_description) {
696 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
697 KATTR_ID, "index_project_description", KATTR__MAX);
698 if (kerr != KCGI_OK)
699 goto done;
700 kerr = khtml_puts(gw_trans->gw_html_req,
701 gw_dir->description ? gw_dir->description : "");
702 if (kerr != KCGI_OK)
703 goto done;
704 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
705 if (kerr != KCGI_OK)
706 goto done;
708 if (gw_trans->gw_conf->got_show_repo_owner) {
709 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
710 KATTR_ID, "index_project_owner", KATTR__MAX);
711 if (kerr != KCGI_OK)
712 goto done;
713 kerr = khtml_puts(gw_trans->gw_html_req,
714 gw_dir->owner ? gw_dir->owner : "");
715 if (kerr != KCGI_OK)
716 goto done;
717 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
718 if (kerr != KCGI_OK)
719 goto done;
721 if (gw_trans->gw_conf->got_show_repo_age) {
722 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
723 KATTR_ID, "index_project_age", KATTR__MAX);
724 if (kerr != KCGI_OK)
725 goto done;
726 kerr = khtml_puts(gw_trans->gw_html_req,
727 gw_dir->age ? gw_dir->age : "");
728 if (kerr != KCGI_OK)
729 goto done;
730 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
731 if (kerr != KCGI_OK)
732 goto done;
735 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
736 "navs_wrapper", KATTR__MAX);
737 if (kerr != KCGI_OK)
738 goto done;
739 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
740 "navs", KATTR__MAX);
741 if (kerr != KCGI_OK)
742 goto done;
744 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
745 href_summary, KATTR__MAX);
746 if (kerr != KCGI_OK)
747 goto done;
748 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
749 if (kerr != KCGI_OK)
750 goto done;
751 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
752 if (kerr != KCGI_OK)
753 goto done;
755 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
756 if (kerr != KCGI_OK)
757 goto done;
759 if (asprintf(&href_briefs, "?path=%s&action=briefs",
760 gw_dir->name) == -1) {
761 error = got_error_from_errno("asprintf");
762 goto done;
764 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
765 href_briefs, KATTR__MAX);
766 if (kerr != KCGI_OK)
767 goto done;
768 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
769 if (kerr != KCGI_OK)
770 goto done;
771 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
772 if (kerr != KCGI_OK)
773 error = gw_kcgi_error(kerr);
775 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
776 if (kerr != KCGI_OK)
777 goto done;
779 if (asprintf(&href_commits, "?path=%s&action=commits",
780 gw_dir->name) == -1) {
781 error = got_error_from_errno("asprintf");
782 goto done;
784 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
785 href_commits, KATTR__MAX);
786 if (kerr != KCGI_OK)
787 goto done;
788 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
789 if (kerr != KCGI_OK)
790 goto done;
791 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
792 if (kerr != KCGI_OK)
793 goto done;
795 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
796 if (kerr != KCGI_OK)
797 goto done;
799 if (asprintf(&href_tags, "?path=%s&action=tags",
800 gw_dir->name) == -1) {
801 error = got_error_from_errno("asprintf");
802 goto done;
804 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
805 href_tags, KATTR__MAX);
806 if (kerr != KCGI_OK)
807 goto done;
808 kerr = khtml_puts(gw_trans->gw_html_req, "tags");
809 if (kerr != KCGI_OK)
810 goto done;
811 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
812 if (kerr != KCGI_OK)
813 goto done;
815 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
816 if (kerr != KCGI_OK)
817 goto done;
819 if (asprintf(&href_tree, "?path=%s&action=tree",
820 gw_dir->name) == -1) {
821 error = got_error_from_errno("asprintf");
822 goto done;
824 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
825 href_tree, KATTR__MAX);
826 if (kerr != KCGI_OK)
827 goto done;
828 kerr = khtml_puts(gw_trans->gw_html_req, "tree");
829 if (kerr != KCGI_OK)
830 goto done;
832 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
833 if (kerr != KCGI_OK)
834 goto done;
835 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
836 "dotted_line", KATTR__MAX);
837 if (kerr != KCGI_OK)
838 goto done;
839 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
840 if (kerr != KCGI_OK)
841 goto done;
843 free(href_summary);
844 href_summary = NULL;
845 free(href_briefs);
846 href_briefs = NULL;
847 free(href_commits);
848 href_commits = NULL;
849 free(href_tags);
850 href_tags = NULL;
851 free(href_tree);
852 href_tree = NULL;
854 if (gw_trans->gw_conf->got_max_repos_display == 0)
855 continue;
857 if ((next_disp == gw_trans->gw_conf->got_max_repos_display) ||
858 ((gw_trans->gw_conf->got_max_repos_display > 0) &&
859 (gw_trans->page > 0) &&
860 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
861 prev_disp == gw_trans->repos_total))) {
862 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
863 KATTR_ID, "np_wrapper", KATTR__MAX);
864 if (kerr != KCGI_OK)
865 goto done;
866 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
867 KATTR_ID, "nav_prev", KATTR__MAX);
868 if (kerr != KCGI_OK)
869 goto done;
872 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
873 (gw_trans->page > 0) &&
874 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
875 prev_disp == gw_trans->repos_total)) {
876 if (asprintf(&href_prev, "?page=%d",
877 gw_trans->page - 1) == -1) {
878 error = got_error_from_errno("asprintf");
879 goto done;
881 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
882 KATTR_HREF, href_prev, KATTR__MAX);
883 if (kerr != KCGI_OK)
884 goto done;
885 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
886 if (kerr != KCGI_OK)
887 goto done;
888 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
889 if (kerr != KCGI_OK)
890 goto done;
893 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
894 if (kerr != KCGI_OK)
895 return gw_kcgi_error(kerr);
897 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
898 next_disp == gw_trans->gw_conf->got_max_repos_display &&
899 dir_c != (gw_trans->page + 1) *
900 gw_trans->gw_conf->got_max_repos_display) {
901 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
902 KATTR_ID, "nav_next", KATTR__MAX);
903 if (kerr != KCGI_OK)
904 goto done;
905 if (asprintf(&href_next, "?page=%d",
906 gw_trans->page + 1) == -1) {
907 error = got_error_from_errno("calloc");
908 goto done;
910 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
911 KATTR_HREF, href_next, KATTR__MAX);
912 if (kerr != KCGI_OK)
913 goto done;
914 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
915 if (kerr != KCGI_OK)
916 goto done;
917 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
918 if (kerr != KCGI_OK)
919 goto done;
920 next_disp = 0;
921 break;
924 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
925 (gw_trans->page > 0) &&
926 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
927 prev_disp == gw_trans->repos_total)) {
928 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
929 if (kerr != KCGI_OK)
930 goto done;
932 next_disp++;
934 done:
935 free(href_prev);
936 free(href_next);
937 free(href_summary);
938 free(href_briefs);
939 free(href_commits);
940 free(href_tags);
941 free(href_tree);
942 if (error == NULL && kerr != KCGI_OK)
943 error = gw_kcgi_error(kerr);
944 return error;
947 static const struct got_error *
948 gw_commits(struct gw_trans *gw_trans)
950 const struct got_error *error = NULL;
951 struct gw_header *header = NULL, *n_header = NULL;
952 char *age = NULL, *href_diff = NULL, *href_blob = NULL;
953 char *href_prev = NULL, *href_next = NULL;
954 enum kcgi_err kerr = KCGI_OK;
956 if ((header = gw_init_header()) == NULL)
957 return got_error_from_errno("malloc");
959 if (pledge("stdio rpath proc exec sendfd unveil",
960 NULL) == -1) {
961 error = got_error_from_errno("pledge");
962 goto done;
965 error = gw_apply_unveil(gw_trans->gw_dir->path);
966 if (error)
967 goto done;
969 error = gw_get_header(gw_trans, header,
970 gw_trans->gw_conf->got_max_commits_display);
971 if (error)
972 goto done;
974 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
975 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
976 "commits_line_wrapper", KATTR__MAX);
977 if (kerr != KCGI_OK)
978 goto done;
979 error = gw_gen_commit_header(gw_trans, n_header->commit_id,
980 n_header->refs_str);
981 if (error)
982 goto done;
983 error = gw_gen_author_header(gw_trans, n_header->author);
984 if (error)
985 goto done;
986 error = gw_gen_committer_header(gw_trans, n_header->author);
987 if (error)
988 goto done;
989 error = gw_get_time_str(&age, n_header->committer_time,
990 TM_LONG);
991 if (error)
992 goto done;
993 error = gw_gen_age_header(gw_trans, age ?age : "");
994 if (error)
995 goto done;
996 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
997 if (kerr != KCGI_OK)
998 goto done;
1000 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1001 "dotted_line", KATTR__MAX);
1002 if (kerr != KCGI_OK)
1003 goto done;
1004 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1005 if (kerr != KCGI_OK)
1006 goto done;
1008 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1009 "commit", KATTR__MAX);
1010 if (kerr != KCGI_OK)
1011 goto done;
1012 kerr = khttp_puts(gw_trans->gw_req, n_header->commit_msg);
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 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
1020 gw_trans->repo_name, n_header->commit_id) == -1) {
1021 error = got_error_from_errno("asprintf");
1022 goto done;
1024 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1025 KATTR_ID, "navs_wrapper", KATTR__MAX);
1026 if (kerr != KCGI_OK)
1027 goto done;
1028 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1029 KATTR_ID, "navs", KATTR__MAX);
1030 if (kerr != KCGI_OK)
1031 goto done;
1032 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1033 KATTR_HREF, href_diff, KATTR__MAX);
1034 if (kerr != KCGI_OK)
1035 goto done;
1036 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1037 if (kerr != KCGI_OK)
1038 goto done;
1039 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1040 if (kerr != KCGI_OK)
1041 goto done;
1043 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1044 if (kerr != KCGI_OK)
1045 goto done;
1047 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
1048 gw_trans->repo_name, n_header->commit_id) == -1) {
1049 error = got_error_from_errno("asprintf");
1050 goto done;
1052 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1053 KATTR_HREF, href_blob, KATTR__MAX);
1054 if (kerr != KCGI_OK)
1055 goto done;
1056 khtml_puts(gw_trans->gw_html_req, "tree");
1057 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1058 if (kerr != KCGI_OK)
1059 goto done;
1060 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1061 if (kerr != KCGI_OK)
1062 goto done;
1064 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1065 "solid_line", KATTR__MAX);
1066 if (kerr != KCGI_OK)
1067 goto done;
1068 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1069 if (kerr != KCGI_OK)
1070 goto done;
1072 free(age);
1073 age = NULL;
1076 if (gw_trans->next_id || gw_trans->page > 0) {
1077 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1078 KATTR_ID, "np_wrapper", KATTR__MAX);
1079 if (kerr != KCGI_OK)
1080 goto done;
1081 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1082 KATTR_ID, "nav_prev", KATTR__MAX);
1083 if (kerr != KCGI_OK)
1084 goto done;
1087 if (gw_trans->page > 0 && gw_trans->prev_id) {
1088 if (asprintf(&href_prev,
1089 "?path=%s&page=%d&action=commits&commit=%s&prev=%s",
1090 gw_trans->repo_name, gw_trans->page - 1,
1091 gw_trans->prev_id ? gw_trans->prev_id : "",
1092 gw_trans->prev_prev_id ?
1093 gw_trans->prev_prev_id : "") == -1) {
1094 error = got_error_from_errno("asprintf");
1095 goto done;
1097 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1098 KATTR_HREF, href_prev, KATTR__MAX);
1099 if (kerr != KCGI_OK)
1100 goto done;
1101 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1102 if (kerr != KCGI_OK)
1103 goto done;
1104 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1105 if (kerr != KCGI_OK)
1106 goto done;
1109 if (gw_trans->next_id || gw_trans->page > 0) {
1110 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1111 if (kerr != KCGI_OK)
1112 return gw_kcgi_error(kerr);
1115 if (gw_trans->next_id) {
1116 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1117 KATTR_ID, "nav_next", KATTR__MAX);
1118 if (kerr != KCGI_OK)
1119 goto done;
1120 if (asprintf(&href_next,
1121 "?path=%s&page=%d&action=commits" \
1122 "&commit=%s&prev=%s&prev_prev=%s",
1123 gw_trans->repo_name, gw_trans->page + 1,
1124 gw_trans->next_id,
1125 gw_trans->next_prev_id ? gw_trans->next_prev_id : "",
1126 gw_trans->prev_id ?
1127 gw_trans->prev_id : "") == -1) {
1128 error = got_error_from_errno("calloc");
1129 goto done;
1131 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1132 KATTR_HREF, href_next, KATTR__MAX);
1133 if (kerr != KCGI_OK)
1134 goto done;
1135 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1136 if (kerr != KCGI_OK)
1137 goto done;
1138 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1139 if (kerr != KCGI_OK)
1140 goto done;
1143 if (gw_trans->next_id || gw_trans->page > 0) {
1144 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1145 if (kerr != KCGI_OK)
1146 goto done;
1148 done:
1149 gw_free_header(header);
1150 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1151 gw_free_header(n_header);
1152 free(age);
1153 free(href_next);
1154 free(href_prev);
1155 free(href_diff);
1156 free(href_blob);
1157 if (error == NULL && kerr != KCGI_OK)
1158 error = gw_kcgi_error(kerr);
1159 return error;
1162 static const struct got_error *
1163 gw_briefs(struct gw_trans *gw_trans)
1165 const struct got_error *error = NULL;
1166 struct gw_header *header = NULL, *n_header = NULL;
1167 char *age = NULL, *href_diff = NULL, *href_blob = NULL;
1168 char *href_prev = NULL, *href_next = NULL;
1169 char *newline, *smallerthan;
1170 enum kcgi_err kerr = KCGI_OK;
1172 if ((header = gw_init_header()) == NULL)
1173 return got_error_from_errno("malloc");
1175 if (pledge("stdio rpath proc exec sendfd unveil",
1176 NULL) == -1) {
1177 error = got_error_from_errno("pledge");
1178 goto done;
1181 if (gw_trans->action != GW_SUMMARY) {
1182 error = gw_apply_unveil(gw_trans->gw_dir->path);
1183 if (error)
1184 goto done;
1187 if (gw_trans->action == GW_SUMMARY)
1188 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
1189 else
1190 error = gw_get_header(gw_trans, header,
1191 gw_trans->gw_conf->got_max_commits_display);
1192 if (error)
1193 goto done;
1195 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
1196 error = gw_get_time_str(&age, n_header->committer_time,
1197 TM_DIFF);
1198 if (error)
1199 goto done;
1201 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1202 KATTR_ID, "briefs_wrapper", KATTR__MAX);
1203 if (kerr != KCGI_OK)
1204 goto done;
1206 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1207 KATTR_ID, "briefs_age", KATTR__MAX);
1208 if (kerr != KCGI_OK)
1209 goto done;
1210 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
1211 if (kerr != KCGI_OK)
1212 goto done;
1213 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1214 if (kerr != KCGI_OK)
1215 goto done;
1217 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1218 KATTR_ID, "briefs_author", KATTR__MAX);
1219 if (kerr != KCGI_OK)
1220 goto done;
1221 smallerthan = strchr(n_header->author, '<');
1222 if (smallerthan)
1223 *smallerthan = '\0';
1224 kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
1225 if (kerr != KCGI_OK)
1226 goto done;
1227 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1228 if (kerr != KCGI_OK)
1229 goto done;
1231 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
1232 gw_trans->repo_name, n_header->commit_id) == -1) {
1233 error = got_error_from_errno("asprintf");
1234 goto done;
1236 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1237 KATTR_ID, "briefs_log", KATTR__MAX);
1238 if (kerr != KCGI_OK)
1239 goto done;
1240 newline = strchr(n_header->commit_msg, '\n');
1241 if (newline)
1242 *newline = '\0';
1243 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1244 KATTR_HREF, href_diff, KATTR__MAX);
1245 if (kerr != KCGI_OK)
1246 goto done;
1247 kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
1248 if (kerr != KCGI_OK)
1249 goto done;
1250 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1251 if (kerr != KCGI_OK)
1252 goto done;
1254 if (n_header->refs_str) {
1255 kerr = khtml_puts(gw_trans->gw_html_req, " ");
1256 if (kerr != KCGI_OK)
1257 goto done;
1258 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
1259 KATTR_ID, "refs_str", KATTR__MAX);
1260 if (kerr != KCGI_OK)
1261 goto done;
1262 kerr = khtml_puts(gw_trans->gw_html_req, "(");
1263 if (kerr != KCGI_OK)
1264 goto done;
1265 kerr = khtml_puts(gw_trans->gw_html_req,
1266 n_header->refs_str);
1267 if (kerr != KCGI_OK)
1268 goto done;
1269 kerr = khtml_puts(gw_trans->gw_html_req, ")");
1270 if (kerr != KCGI_OK)
1271 goto done;
1272 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1273 if (kerr != KCGI_OK)
1274 goto done;
1277 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1278 if (kerr != KCGI_OK)
1279 goto done;
1281 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1282 KATTR_ID, "navs_wrapper", KATTR__MAX);
1283 if (kerr != KCGI_OK)
1284 goto done;
1285 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1286 KATTR_ID, "navs", KATTR__MAX);
1287 if (kerr != KCGI_OK)
1288 goto done;
1289 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1290 KATTR_HREF, href_diff, KATTR__MAX);
1291 if (kerr != KCGI_OK)
1292 goto done;
1293 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1294 if (kerr != KCGI_OK)
1295 goto done;
1296 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1297 if (kerr != KCGI_OK)
1298 goto done;
1300 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1301 if (kerr != KCGI_OK)
1302 goto done;
1304 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
1305 gw_trans->repo_name, n_header->commit_id) == -1) {
1306 error = got_error_from_errno("asprintf");
1307 goto done;
1309 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1310 KATTR_HREF, href_blob, KATTR__MAX);
1311 if (kerr != KCGI_OK)
1312 goto done;
1313 khtml_puts(gw_trans->gw_html_req, "tree");
1314 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1315 if (kerr != KCGI_OK)
1316 goto done;
1317 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1318 if (kerr != KCGI_OK)
1319 goto done;
1321 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1322 KATTR_ID, "dotted_line", KATTR__MAX);
1323 if (kerr != KCGI_OK)
1324 goto done;
1325 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1326 if (kerr != KCGI_OK)
1327 goto done;
1329 free(age);
1330 age = NULL;
1331 free(href_diff);
1332 href_diff = NULL;
1333 free(href_blob);
1334 href_blob = NULL;
1337 if (gw_trans->next_id || gw_trans->page > 0) {
1338 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1339 KATTR_ID, "np_wrapper", KATTR__MAX);
1340 if (kerr != KCGI_OK)
1341 goto done;
1342 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1343 KATTR_ID, "nav_prev", KATTR__MAX);
1344 if (kerr != KCGI_OK)
1345 goto done;
1348 if (gw_trans->page > 0 && gw_trans->prev_id) {
1349 if (asprintf(&href_prev,
1350 "?path=%s&page=%d&action=briefs&commit=%s&prev=%s",
1351 gw_trans->repo_name, gw_trans->page - 1,
1352 gw_trans->prev_id ? gw_trans->prev_id : "",
1353 gw_trans->prev_prev_id ?
1354 gw_trans->prev_prev_id : "") == -1) {
1355 error = got_error_from_errno("asprintf");
1356 goto done;
1358 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1359 KATTR_HREF, href_prev, KATTR__MAX);
1360 if (kerr != KCGI_OK)
1361 goto done;
1362 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1363 if (kerr != KCGI_OK)
1364 goto done;
1365 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1366 if (kerr != KCGI_OK)
1367 goto done;
1370 if (gw_trans->next_id || gw_trans->page > 0) {
1371 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1372 if (kerr != KCGI_OK)
1373 return gw_kcgi_error(kerr);
1376 if (gw_trans->next_id) {
1377 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1378 KATTR_ID, "nav_next", KATTR__MAX);
1379 if (kerr != KCGI_OK)
1380 goto done;
1381 if (asprintf(&href_next,
1382 "?path=%s&page=%d&action=briefs" \
1383 "&commit=%s&prev=%s&prev_prev=%s",
1384 gw_trans->repo_name, gw_trans->page + 1,
1385 gw_trans->next_id,
1386 gw_trans->next_prev_id ? gw_trans->next_prev_id : "",
1387 gw_trans->prev_id ?
1388 gw_trans->prev_id : "") == -1) {
1389 error = got_error_from_errno("calloc");
1390 goto done;
1392 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1393 KATTR_HREF, href_next, KATTR__MAX);
1394 if (kerr != KCGI_OK)
1395 goto done;
1396 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1397 if (kerr != KCGI_OK)
1398 goto done;
1399 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1400 if (kerr != KCGI_OK)
1401 goto done;
1404 if (gw_trans->next_id || gw_trans->page > 0) {
1405 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1406 if (kerr != KCGI_OK)
1407 goto done;
1409 done:
1410 gw_free_header(header);
1411 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1412 gw_free_header(n_header);
1413 free(age);
1414 free(href_next);
1415 free(href_prev);
1416 free(href_diff);
1417 free(href_blob);
1418 if (error == NULL && kerr != KCGI_OK)
1419 error = gw_kcgi_error(kerr);
1420 return error;
1423 static const struct got_error *
1424 gw_summary(struct gw_trans *gw_trans)
1426 const struct got_error *error = NULL;
1427 char *age = NULL;
1428 enum kcgi_err kerr = KCGI_OK;
1430 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1431 return got_error_from_errno("pledge");
1433 error = gw_apply_unveil(gw_trans->gw_dir->path);
1434 if (error)
1435 goto done;
1437 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1438 "summary_wrapper", KATTR__MAX);
1439 if (kerr != KCGI_OK)
1440 return gw_kcgi_error(kerr);
1442 if (gw_trans->gw_conf->got_show_repo_description &&
1443 gw_trans->gw_dir->description != NULL &&
1444 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1445 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1446 KATTR_ID, "description_title", KATTR__MAX);
1447 if (kerr != KCGI_OK)
1448 goto done;
1449 kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1450 if (kerr != KCGI_OK)
1451 goto done;
1452 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1453 if (kerr != KCGI_OK)
1454 goto done;
1455 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1456 KATTR_ID, "description", KATTR__MAX);
1457 if (kerr != KCGI_OK)
1458 goto done;
1459 kerr = khtml_puts(gw_trans->gw_html_req,
1460 gw_trans->gw_dir->description);
1461 if (kerr != KCGI_OK)
1462 goto done;
1463 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1464 if (kerr != KCGI_OK)
1465 goto done;
1468 if (gw_trans->gw_conf->got_show_repo_owner &&
1469 gw_trans->gw_dir->owner != NULL &&
1470 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
1471 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1472 KATTR_ID, "repo_owner_title", KATTR__MAX);
1473 if (kerr != KCGI_OK)
1474 goto done;
1475 kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
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, "repo_owner", KATTR__MAX);
1483 if (kerr != KCGI_OK)
1484 goto done;
1485 kerr = khtml_puts(gw_trans->gw_html_req,
1486 gw_trans->gw_dir->owner);
1487 if (kerr != KCGI_OK)
1488 goto done;
1489 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1490 if (kerr != KCGI_OK)
1491 goto done;
1494 if (gw_trans->gw_conf->got_show_repo_age) {
1495 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1496 NULL, TM_LONG);
1497 if (error)
1498 goto done;
1499 if (age != NULL) {
1500 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1501 KATTR_ID, "last_change_title", KATTR__MAX);
1502 if (kerr != KCGI_OK)
1503 goto done;
1504 kerr = khtml_puts(gw_trans->gw_html_req,
1505 "Last Change: ");
1506 if (kerr != KCGI_OK)
1507 goto done;
1508 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1509 if (kerr != KCGI_OK)
1510 goto done;
1511 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1512 KATTR_ID, "last_change", KATTR__MAX);
1513 if (kerr != KCGI_OK)
1514 goto done;
1515 kerr = khtml_puts(gw_trans->gw_html_req, age);
1516 if (kerr != KCGI_OK)
1517 goto done;
1518 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1519 if (kerr != KCGI_OK)
1520 goto done;
1524 if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1525 gw_trans->gw_dir->url != NULL &&
1526 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1527 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1528 KATTR_ID, "cloneurl_title", KATTR__MAX);
1529 if (kerr != KCGI_OK)
1530 goto done;
1531 kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1532 if (kerr != KCGI_OK)
1533 goto done;
1534 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1535 if (kerr != KCGI_OK)
1536 goto done;
1537 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1538 KATTR_ID, "cloneurl", KATTR__MAX);
1539 if (kerr != KCGI_OK)
1540 goto done;
1541 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1542 if (kerr != KCGI_OK)
1543 goto done;
1544 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1545 if (kerr != KCGI_OK)
1546 goto done;
1549 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1550 if (kerr != KCGI_OK)
1551 goto done;
1553 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1554 "briefs_title_wrapper", KATTR__MAX);
1555 if (kerr != KCGI_OK)
1556 goto done;
1557 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1558 "briefs_title", KATTR__MAX);
1559 if (kerr != KCGI_OK)
1560 goto done;
1561 kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1562 if (kerr != KCGI_OK)
1563 goto done;
1564 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1565 if (kerr != KCGI_OK)
1566 goto done;
1567 error = gw_briefs(gw_trans);
1568 if (error)
1569 goto done;
1571 error = gw_tags(gw_trans);
1572 if (error)
1573 goto done;
1575 error = gw_output_repo_heads(gw_trans);
1576 done:
1577 free(age);
1578 if (error == NULL && kerr != KCGI_OK)
1579 error = gw_kcgi_error(kerr);
1580 return error;
1583 static const struct got_error *
1584 gw_tree(struct gw_trans *gw_trans)
1586 const struct got_error *error = NULL;
1587 struct gw_header *header = NULL;
1588 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1589 char *age = NULL;
1590 enum kcgi_err kerr = KCGI_OK;
1592 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1593 return got_error_from_errno("pledge");
1595 if ((header = gw_init_header()) == NULL)
1596 return got_error_from_errno("malloc");
1598 error = gw_apply_unveil(gw_trans->gw_dir->path);
1599 if (error)
1600 goto done;
1602 error = gw_get_header(gw_trans, header, 1);
1603 if (error)
1604 goto done;
1606 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1607 "tree_header_wrapper", KATTR__MAX);
1608 if (kerr != KCGI_OK)
1609 goto done;
1610 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1611 "tree_header", KATTR__MAX);
1612 if (kerr != KCGI_OK)
1613 goto done;
1614 error = gw_gen_tree_header(gw_trans, header->tree_id);
1615 if (error)
1616 goto done;
1617 error = gw_get_time_str(&age, header->committer_time,
1618 TM_LONG);
1619 if (error)
1620 goto done;
1621 error = gw_gen_age_header(gw_trans, age ?age : "");
1622 if (error)
1623 goto done;
1624 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1625 if (error)
1626 goto done;
1627 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1628 if (kerr != KCGI_OK)
1629 goto done;
1630 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1631 "dotted_line", KATTR__MAX);
1632 if (kerr != KCGI_OK)
1633 goto done;
1634 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1635 if (kerr != KCGI_OK)
1636 goto done;
1638 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1639 "tree", KATTR__MAX);
1640 if (kerr != KCGI_OK)
1641 goto done;
1642 error = gw_output_repo_tree(gw_trans);
1643 if (error)
1644 goto done;
1646 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1647 done:
1648 gw_free_header(header);
1649 free(tree_html_disp);
1650 free(tree_html);
1651 free(tree);
1652 free(age);
1653 if (error == NULL && kerr != KCGI_OK)
1654 error = gw_kcgi_error(kerr);
1655 return error;
1658 static const struct got_error *
1659 gw_tags(struct gw_trans *gw_trans)
1661 const struct got_error *error = NULL;
1662 struct gw_header *header = NULL;
1663 char *href_next = NULL, *href_prev = NULL;
1664 enum kcgi_err kerr = KCGI_OK;
1666 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1667 return got_error_from_errno("pledge");
1669 if ((header = gw_init_header()) == NULL)
1670 return got_error_from_errno("malloc");
1672 if (gw_trans->action != GW_SUMMARY) {
1673 error = gw_apply_unveil(gw_trans->gw_dir->path);
1674 if (error)
1675 goto done;
1678 error = gw_get_header(gw_trans, header, 1);
1679 if (error)
1680 goto done;
1682 if (gw_trans->action == GW_SUMMARY) {
1683 error = gw_output_repo_tags(gw_trans, header,
1684 D_MAXSLCOMMDISP, TAGBRIEF);
1685 if (error)
1686 goto done;
1687 } else {
1688 error = gw_output_repo_tags(gw_trans, header,
1689 gw_trans->gw_conf->got_max_commits_display, TAGBRIEF);
1690 if (error)
1691 goto done;
1694 if (gw_trans->next_id || gw_trans->page > 0) {
1695 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1696 KATTR_ID, "np_wrapper", KATTR__MAX);
1697 if (kerr != KCGI_OK)
1698 goto done;
1699 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1700 KATTR_ID, "nav_prev", KATTR__MAX);
1701 if (kerr != KCGI_OK)
1702 goto done;
1705 if (gw_trans->page > 0 && gw_trans->prev_id) {
1706 if (asprintf(&href_prev,
1707 "?path=%s&page=%d&action=tags&commit=%s&prev=%s",
1708 gw_trans->repo_name, gw_trans->page - 1,
1709 gw_trans->prev_id ? gw_trans->prev_id : "",
1710 gw_trans->prev_prev_id ?
1711 gw_trans->prev_prev_id : "") == -1) {
1712 error = got_error_from_errno("asprintf");
1713 goto done;
1715 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1716 KATTR_HREF, href_prev, KATTR__MAX);
1717 if (kerr != KCGI_OK)
1718 goto done;
1719 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1720 if (kerr != KCGI_OK)
1721 goto done;
1722 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1723 if (kerr != KCGI_OK)
1724 goto done;
1727 if (gw_trans->next_id || gw_trans->page > 0) {
1728 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1729 if (kerr != KCGI_OK)
1730 return gw_kcgi_error(kerr);
1733 if (gw_trans->next_id) {
1734 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1735 KATTR_ID, "nav_next", KATTR__MAX);
1736 if (kerr != KCGI_OK)
1737 goto done;
1738 if (asprintf(&href_next,
1739 "?path=%s&page=%d&action=tags" \
1740 "&commit=%s&prev=%s&prev_prev=%s",
1741 gw_trans->repo_name, gw_trans->page + 1,
1742 gw_trans->next_id,
1743 gw_trans->next_prev_id ? gw_trans->next_prev_id : "",
1744 gw_trans->prev_id ?
1745 gw_trans->prev_id : "") == -1) {
1746 error = got_error_from_errno("calloc");
1747 goto done;
1749 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1750 KATTR_HREF, href_next, KATTR__MAX);
1751 if (kerr != KCGI_OK)
1752 goto done;
1753 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1754 if (kerr != KCGI_OK)
1755 goto done;
1756 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1757 if (kerr != KCGI_OK)
1758 goto done;
1761 if (gw_trans->next_id || gw_trans->page > 0) {
1762 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1763 if (kerr != KCGI_OK)
1764 goto done;
1766 done:
1767 gw_free_header(header);
1768 free(href_next);
1769 free(href_prev);
1770 if (error == NULL && kerr != KCGI_OK)
1771 error = gw_kcgi_error(kerr);
1772 return error;
1775 static const struct got_error *
1776 gw_tag(struct gw_trans *gw_trans)
1778 const struct got_error *error = NULL;
1779 struct gw_header *header = NULL;
1780 enum kcgi_err kerr = KCGI_OK;
1782 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1783 return got_error_from_errno("pledge");
1785 if ((header = gw_init_header()) == NULL)
1786 return got_error_from_errno("malloc");
1788 error = gw_apply_unveil(gw_trans->gw_dir->path);
1789 if (error)
1790 goto done;
1792 if (gw_trans->commit_id == NULL) {
1793 error = got_error_msg(GOT_ERR_QUERYSTRING,
1794 "commit required in querystring");
1795 goto done;
1798 error = gw_get_header(gw_trans, header, 1);
1799 if (error)
1800 goto done;
1802 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1803 "tag_header_wrapper", KATTR__MAX);
1804 if (kerr != KCGI_OK)
1805 goto done;
1806 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1807 "tag_header", KATTR__MAX);
1808 if (kerr != KCGI_OK)
1809 goto done;
1810 error = gw_gen_commit_header(gw_trans, header->commit_id,
1811 header->refs_str);
1812 if (error)
1813 goto done;
1814 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1815 if (error)
1816 goto done;
1817 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1818 if (kerr != KCGI_OK)
1819 goto done;
1820 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1821 "dotted_line", KATTR__MAX);
1822 if (kerr != KCGI_OK)
1823 goto done;
1824 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1825 if (kerr != KCGI_OK)
1826 goto done;
1828 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1829 "tree", KATTR__MAX);
1830 if (kerr != KCGI_OK)
1831 goto done;
1833 error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1834 if (error)
1835 goto done;
1837 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1838 done:
1839 gw_free_header(header);
1840 if (error == NULL && kerr != KCGI_OK)
1841 error = gw_kcgi_error(kerr);
1842 return error;
1845 static const struct got_error *
1846 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1848 const struct got_error *error = NULL;
1849 DIR *dt;
1850 char *dir_test;
1851 int opened = 0;
1853 if (asprintf(&dir_test, "%s/%s/%s",
1854 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1855 GOTWEB_GIT_DIR) == -1)
1856 return got_error_from_errno("asprintf");
1858 dt = opendir(dir_test);
1859 if (dt == NULL) {
1860 free(dir_test);
1861 } else {
1862 gw_dir->path = strdup(dir_test);
1863 if (gw_dir->path == NULL) {
1864 opened = 1;
1865 error = got_error_from_errno("strdup");
1866 goto errored;
1868 opened = 1;
1869 goto done;
1872 if (asprintf(&dir_test, "%s/%s/%s",
1873 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1874 GOTWEB_GOT_DIR) == -1) {
1875 dir_test = NULL;
1876 error = got_error_from_errno("asprintf");
1877 goto errored;
1880 dt = opendir(dir_test);
1881 if (dt == NULL)
1882 free(dir_test);
1883 else {
1884 opened = 1;
1885 error = got_error(GOT_ERR_NOT_GIT_REPO);
1886 goto errored;
1889 if (asprintf(&dir_test, "%s/%s",
1890 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1) {
1891 error = got_error_from_errno("asprintf");
1892 dir_test = NULL;
1893 goto errored;
1896 gw_dir->path = strdup(dir_test);
1897 if (gw_dir->path == NULL) {
1898 opened = 1;
1899 error = got_error_from_errno("strdup");
1900 goto errored;
1903 dt = opendir(dir_test);
1904 if (dt == NULL) {
1905 error = got_error_path(gw_dir->name, GOT_ERR_NOT_GIT_REPO);
1906 goto errored;
1907 } else
1908 opened = 1;
1909 done:
1910 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1911 gw_dir->path);
1912 if (error)
1913 goto errored;
1914 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1915 if (error)
1916 goto errored;
1917 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1918 NULL, TM_DIFF);
1919 if (error)
1920 goto errored;
1921 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1922 errored:
1923 free(dir_test);
1924 if (opened)
1925 if (dt && closedir(dt) == -1 && error == NULL)
1926 error = got_error_from_errno("closedir");
1927 return error;
1930 static const struct got_error *
1931 gw_load_got_paths(struct gw_trans *gw_trans)
1933 const struct got_error *error = NULL;
1934 DIR *d;
1935 struct dirent **sd_dent;
1936 struct gw_dir *gw_dir;
1937 struct stat st;
1938 unsigned int d_cnt, d_i;
1940 d = opendir(gw_trans->gw_conf->got_repos_path);
1941 if (d == NULL) {
1942 error = got_error_from_errno2("opendir",
1943 gw_trans->gw_conf->got_repos_path);
1944 return error;
1947 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1948 alphasort);
1949 if (d_cnt == -1) {
1950 error = got_error_from_errno2("scandir",
1951 gw_trans->gw_conf->got_repos_path);
1952 goto done;
1955 for (d_i = 0; d_i < d_cnt; d_i++) {
1956 if (gw_trans->gw_conf->got_max_repos > 0 &&
1957 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1958 break; /* account for parent and self */
1960 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1961 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1962 continue;
1964 error = gw_init_gw_dir(&gw_dir, sd_dent[d_i]->d_name);
1965 if (error)
1966 goto done;
1968 error = gw_load_got_path(gw_trans, gw_dir);
1969 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1970 error = NULL;
1971 continue;
1973 else if (error)
1974 goto done;
1976 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1977 !got_path_dir_is_empty(gw_dir->path)) {
1978 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1979 entry);
1980 gw_trans->repos_total++;
1983 done:
1984 if (d && closedir(d) == -1 && error == NULL)
1985 error = got_error_from_errno("closedir");
1986 return error;
1989 static const struct got_error *
1990 gw_parse_querystring(struct gw_trans *gw_trans)
1992 const struct got_error *error = NULL;
1993 struct kpair *p;
1994 struct gw_query_action *action = NULL;
1995 unsigned int i;
1997 if (gw_trans->gw_req->fieldnmap[0]) {
1998 return got_error(GOT_ERR_QUERYSTRING);
1999 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
2000 /* define gw_trans->repo_path */
2001 gw_trans->repo_name = p->parsed.s;
2003 if (asprintf(&gw_trans->repo_path, "%s/%s",
2004 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
2005 return got_error_from_errno("asprintf");
2007 /* get action and set function */
2008 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION])) {
2009 for (i = 0; i < nitems(gw_query_funcs); i++) {
2010 action = &gw_query_funcs[i];
2011 if (action->func_name == NULL)
2012 continue;
2013 if (strcmp(action->func_name,
2014 p->parsed.s) == 0) {
2015 gw_trans->action = i;
2016 break;
2020 if (gw_trans->action == -1) {
2021 gw_trans->action = GW_ERR;
2022 gw_trans->error = got_error_msg(GOT_ERR_QUERYSTRING,
2023 p != NULL ? "bad action in querystring" :
2024 "no action in querystring");
2025 return error;
2028 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID])) {
2029 if (asprintf(&gw_trans->commit_id, "%s",
2030 p->parsed.s) == -1)
2031 return got_error_from_errno("asprintf");
2034 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
2035 gw_trans->repo_file = p->parsed.s;
2037 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER])) {
2038 if (asprintf(&gw_trans->repo_folder, "%s",
2039 p->parsed.s) == -1)
2040 return got_error_from_errno("asprintf");
2043 if ((p = gw_trans->gw_req->fieldmap[KEY_PREV_ID])) {
2044 if (asprintf(&gw_trans->prev_id, "%s",
2045 p->parsed.s) == -1)
2046 return got_error_from_errno("asprintf");
2049 if ((p = gw_trans->gw_req->fieldmap[KEY_PREV_PREV_ID])) {
2050 if (asprintf(&gw_trans->prev_prev_id, "%s",
2051 p->parsed.s) == -1)
2052 return got_error_from_errno("asprintf");
2055 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
2056 gw_trans->headref = p->parsed.s;
2058 error = gw_init_gw_dir(&gw_trans->gw_dir, gw_trans->repo_name);
2059 if (error)
2060 return error;
2062 gw_trans->error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
2063 } else
2064 gw_trans->action = GW_INDEX;
2066 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
2067 gw_trans->page = p->parsed.i;
2069 return error;
2072 static const struct got_error *
2073 gw_init_gw_dir(struct gw_dir **gw_dir, const char *dir)
2075 const struct got_error *error;
2077 *gw_dir = malloc(sizeof(**gw_dir));
2078 if (*gw_dir == NULL)
2079 return got_error_from_errno("malloc");
2081 if (asprintf(&(*gw_dir)->name, "%s", dir) == -1) {
2082 error = got_error_from_errno("asprintf");
2083 free(*gw_dir);
2084 *gw_dir = NULL;
2085 return NULL;
2088 return NULL;
2091 static const struct got_error *
2092 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
2094 enum kcgi_err kerr = KCGI_OK;
2096 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
2097 if (kerr != KCGI_OK)
2098 return gw_kcgi_error(kerr);
2099 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
2100 khttps[code]);
2101 if (kerr != KCGI_OK)
2102 return gw_kcgi_error(kerr);
2103 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
2104 kmimetypes[mime]);
2105 if (kerr != KCGI_OK)
2106 return gw_kcgi_error(kerr);
2107 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
2108 "nosniff");
2109 if (kerr != KCGI_OK)
2110 return gw_kcgi_error(kerr);
2111 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
2112 if (kerr != KCGI_OK)
2113 return gw_kcgi_error(kerr);
2114 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
2115 "1; mode=block");
2116 if (kerr != KCGI_OK)
2117 return gw_kcgi_error(kerr);
2119 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
2120 kerr = khttp_head(gw_trans->gw_req,
2121 kresps[KRESP_CONTENT_DISPOSITION],
2122 "attachment; filename=%s", gw_trans->repo_file);
2123 if (kerr != KCGI_OK)
2124 return gw_kcgi_error(kerr);
2127 kerr = khttp_body(gw_trans->gw_req);
2128 return gw_kcgi_error(kerr);
2131 static const struct got_error *
2132 gw_display_index(struct gw_trans *gw_trans)
2134 const struct got_error *error;
2135 enum kcgi_err kerr = KCGI_OK;
2137 /* catch early querystring errors */
2138 if (gw_trans->error)
2139 gw_trans->action = GW_ERR;
2141 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
2142 if (error)
2143 return error;
2145 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
2146 if (kerr != KCGI_OK)
2147 return gw_kcgi_error(kerr);
2149 if (gw_trans->action != GW_BLOB) {
2150 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
2151 gw_query_funcs[gw_trans->action].template);
2152 if (kerr != KCGI_OK) {
2153 khtml_close(gw_trans->gw_html_req);
2154 return gw_kcgi_error(kerr);
2158 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
2161 static const struct got_error *
2162 gw_error(struct gw_trans *gw_trans)
2164 enum kcgi_err kerr = KCGI_OK;
2166 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->error->msg);
2168 return gw_kcgi_error(kerr);
2171 static int
2172 gw_template(size_t key, void *arg)
2174 const struct got_error *error = NULL;
2175 enum kcgi_err kerr = KCGI_OK;
2176 struct gw_trans *gw_trans = arg;
2177 char *img_src = NULL;
2179 switch (key) {
2180 case (TEMPL_HEAD):
2181 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2182 KATTR_NAME, "viewport",
2183 KATTR_CONTENT, "initial-scale=.75, user-scalable=yes",
2184 KATTR__MAX);
2185 if (kerr != KCGI_OK)
2186 return 0;
2187 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2188 if (kerr != KCGI_OK)
2189 return 0;
2190 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2191 KATTR_CHARSET, "utf-8",
2192 KATTR__MAX);
2193 if (kerr != KCGI_OK)
2194 return 0;
2195 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2196 if (kerr != KCGI_OK)
2197 return 0;
2198 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2199 KATTR_NAME, "msapplication-TileColor",
2200 KATTR_CONTENT, "#da532c", KATTR__MAX);
2201 if (kerr != KCGI_OK)
2202 return 0;
2203 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2204 if (kerr != KCGI_OK)
2205 return 0;
2206 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2207 KATTR_NAME, "theme-color",
2208 KATTR_CONTENT, "#ffffff", KATTR__MAX);
2209 if (kerr != KCGI_OK)
2210 return 0;
2211 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2212 if (kerr != KCGI_OK)
2213 return 0;
2214 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2215 KATTR_REL, "apple-touch-icon", KATTR_SIZES, "180x180",
2216 KATTR_HREF, "/apple-touch-icon.png", KATTR__MAX);
2217 if (kerr != KCGI_OK)
2218 return 0;
2219 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2220 if (kerr != KCGI_OK)
2221 return 0;
2222 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2223 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2224 "32x32", KATTR_HREF, "/favicon-32x32.png", KATTR__MAX);
2225 if (kerr != KCGI_OK)
2226 return 0;
2227 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2228 if (kerr != KCGI_OK)
2229 return 0;
2230 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2231 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2232 "16x16", KATTR_HREF, "/favicon-16x16.png", KATTR__MAX);
2233 if (kerr != KCGI_OK)
2234 return 0;
2235 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2236 if (kerr != KCGI_OK)
2237 return 0;
2238 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2239 KATTR_REL, "manifest", KATTR_HREF, "/site.webmanifest",
2240 KATTR__MAX);
2241 if (kerr != KCGI_OK)
2242 return 0;
2243 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2244 if (kerr != KCGI_OK)
2245 return 0;
2246 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2247 KATTR_REL, "mask-icon", KATTR_HREF,
2248 "/safari-pinned-tab.svg", KATTR__MAX);
2249 if (kerr != KCGI_OK)
2250 return 0;
2251 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2252 if (kerr != KCGI_OK)
2253 return 0;
2254 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2255 KATTR_REL, "stylesheet", KATTR_TYPE, "text/css",
2256 KATTR_HREF, "/gotweb.css", KATTR__MAX);
2257 if (kerr != KCGI_OK)
2258 return 0;
2259 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2260 if (kerr != KCGI_OK)
2261 return 0;
2262 break;
2263 case(TEMPL_HEADER):
2264 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2265 KATTR_ID, "got_link", KATTR__MAX);
2266 if (kerr != KCGI_OK)
2267 return 0;
2268 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2269 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
2270 KATTR_TARGET, "_sotd", KATTR__MAX);
2271 if (kerr != KCGI_OK)
2272 return 0;
2273 if (asprintf(&img_src, "/%s",
2274 gw_trans->gw_conf->got_logo) == -1)
2275 return 0;
2276 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
2277 KATTR_SRC, img_src, KATTR__MAX);
2278 if (kerr != KCGI_OK) {
2279 free(img_src);
2280 return 0;
2282 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2283 if (kerr != KCGI_OK) {
2284 free(img_src);
2285 return 0;
2287 break;
2288 case (TEMPL_SITEPATH):
2289 error = gw_output_site_link(gw_trans);
2290 if (error)
2291 return 0;
2292 break;
2293 case(TEMPL_TITLE):
2294 if (gw_trans->gw_conf->got_site_name != NULL) {
2295 kerr = khtml_puts(gw_trans->gw_html_req,
2296 gw_trans->gw_conf->got_site_name);
2297 if (kerr != KCGI_OK)
2298 return 0;
2300 break;
2301 case (TEMPL_SEARCH):
2302 break;
2303 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
2304 "search", KATTR__MAX);
2305 if (kerr != KCGI_OK)
2306 return 0;
2307 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
2308 KATTR_METHOD, "POST", KATTR__MAX);
2309 if (kerr != KCGI_OK)
2310 return 0;
2311 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
2312 "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
2313 KATTR_MAXLENGTH, "50", KATTR__MAX);
2314 if (kerr != KCGI_OK)
2315 return 0;
2316 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
2317 KATTR__MAX);
2318 if (kerr != KCGI_OK)
2319 return 0;
2320 kerr = khtml_puts(gw_trans->gw_html_req, "Search");
2321 if (kerr != KCGI_OK)
2322 return 0;
2323 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
2324 if (kerr != KCGI_OK)
2325 return 0;
2326 break;
2327 case(TEMPL_SITEOWNER):
2328 if (gw_trans->gw_conf->got_site_owner != NULL &&
2329 gw_trans->gw_conf->got_show_site_owner) {
2330 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2331 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
2332 if (kerr != KCGI_OK)
2333 return 0;
2334 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2335 KATTR_ID, "site_owner", KATTR__MAX);
2336 if (kerr != KCGI_OK)
2337 return 0;
2338 kerr = khtml_puts(gw_trans->gw_html_req,
2339 gw_trans->gw_conf->got_site_owner);
2340 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2341 if (kerr != KCGI_OK)
2342 return 0;
2344 break;
2345 case(TEMPL_CONTENT):
2346 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
2347 if (error) {
2348 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2349 KATTR_ID, "tmpl_err", KATTR__MAX);
2350 if (kerr != KCGI_OK)
2351 return 0;
2352 kerr = khttp_puts(gw_trans->gw_req, "Error: ");
2353 if (kerr != KCGI_OK)
2354 return 0;
2355 kerr = khttp_puts(gw_trans->gw_req, error->msg);
2356 if (kerr != KCGI_OK)
2357 return 0;
2358 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2359 if (kerr != KCGI_OK)
2360 return 0;
2362 break;
2363 default:
2364 return 0;
2366 return 1;
2369 static const struct got_error *
2370 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
2372 const struct got_error *error = NULL;
2373 enum kcgi_err kerr = KCGI_OK;
2375 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2376 KATTR_ID, "header_commit_title", KATTR__MAX);
2377 if (kerr != KCGI_OK)
2378 goto done;
2379 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
2380 if (kerr != KCGI_OK)
2381 goto done;
2382 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2383 if (kerr != KCGI_OK)
2384 goto done;
2385 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2386 KATTR_ID, "header_commit", KATTR__MAX);
2387 if (kerr != KCGI_OK)
2388 goto done;
2389 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2390 if (kerr != KCGI_OK)
2391 goto done;
2392 kerr = khtml_puts(gw_trans->gw_html_req, " ");
2393 if (kerr != KCGI_OK)
2394 goto done;
2395 if (str2 != NULL) {
2396 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
2397 KATTR_ID, "refs_str", KATTR__MAX);
2398 if (kerr != KCGI_OK)
2399 goto done;
2400 kerr = khtml_puts(gw_trans->gw_html_req, "(");
2401 if (kerr != KCGI_OK)
2402 goto done;
2403 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2404 if (kerr != KCGI_OK)
2405 goto done;
2406 kerr = khtml_puts(gw_trans->gw_html_req, ")");
2407 if (kerr != KCGI_OK)
2408 goto done;
2409 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2410 if (kerr != KCGI_OK)
2411 goto done;
2413 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2414 done:
2415 if (error == NULL && kerr != KCGI_OK)
2416 error = gw_kcgi_error(kerr);
2417 return error;
2420 static const struct got_error *
2421 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2423 const struct got_error *error = NULL;
2424 enum kcgi_err kerr = KCGI_OK;
2426 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2427 KATTR_ID, "header_diff_title", KATTR__MAX);
2428 if (kerr != KCGI_OK)
2429 goto done;
2430 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2431 if (kerr != KCGI_OK)
2432 goto done;
2433 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2434 if (kerr != KCGI_OK)
2435 goto done;
2436 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2437 KATTR_ID, "header_diff", KATTR__MAX);
2438 if (kerr != KCGI_OK)
2439 goto done;
2440 if (str1 != NULL) {
2441 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2442 if (kerr != KCGI_OK)
2443 goto done;
2445 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2446 if (kerr != KCGI_OK)
2447 goto done;
2448 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2449 if (kerr != KCGI_OK)
2450 goto done;
2451 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2452 done:
2453 if (error == NULL && kerr != KCGI_OK)
2454 error = gw_kcgi_error(kerr);
2455 return error;
2458 static const struct got_error *
2459 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2461 const struct got_error *error = NULL;
2462 enum kcgi_err kerr = KCGI_OK;
2464 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2465 KATTR_ID, "header_age_title", KATTR__MAX);
2466 if (kerr != KCGI_OK)
2467 goto done;
2468 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2469 if (kerr != KCGI_OK)
2470 goto done;
2471 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2472 if (kerr != KCGI_OK)
2473 goto done;
2474 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2475 KATTR_ID, "header_age", KATTR__MAX);
2476 if (kerr != KCGI_OK)
2477 goto done;
2478 kerr = khtml_puts(gw_trans->gw_html_req, str);
2479 if (kerr != KCGI_OK)
2480 goto done;
2481 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2482 done:
2483 if (error == NULL && kerr != KCGI_OK)
2484 error = gw_kcgi_error(kerr);
2485 return error;
2488 static const struct got_error *
2489 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2491 const struct got_error *error = NULL;
2492 enum kcgi_err kerr = KCGI_OK;
2494 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2495 KATTR_ID, "header_author_title", KATTR__MAX);
2496 if (kerr != KCGI_OK)
2497 goto done;
2498 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2499 if (kerr != KCGI_OK)
2500 goto done;
2501 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2502 if (kerr != KCGI_OK)
2503 goto done;
2504 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2505 KATTR_ID, "header_author", KATTR__MAX);
2506 if (kerr != KCGI_OK)
2507 goto done;
2508 kerr = khtml_puts(gw_trans->gw_html_req, str);
2509 if (kerr != KCGI_OK)
2510 goto done;
2511 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2512 done:
2513 if (error == NULL && kerr != KCGI_OK)
2514 error = gw_kcgi_error(kerr);
2515 return error;
2518 static const struct got_error *
2519 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2521 const struct got_error *error = NULL;
2522 enum kcgi_err kerr = KCGI_OK;
2524 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2525 KATTR_ID, "header_committer_title", KATTR__MAX);
2526 if (kerr != KCGI_OK)
2527 goto done;
2528 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2529 if (kerr != KCGI_OK)
2530 goto done;
2531 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2532 if (kerr != KCGI_OK)
2533 goto done;
2534 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2535 KATTR_ID, "header_committer", KATTR__MAX);
2536 if (kerr != KCGI_OK)
2537 goto done;
2538 kerr = khtml_puts(gw_trans->gw_html_req, str);
2539 if (kerr != KCGI_OK)
2540 goto done;
2541 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2542 done:
2543 if (error == NULL && kerr != KCGI_OK)
2544 error = gw_kcgi_error(kerr);
2545 return error;
2548 static const struct got_error *
2549 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2551 const struct got_error *error = NULL;
2552 enum kcgi_err kerr = KCGI_OK;
2554 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2555 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2556 if (kerr != KCGI_OK)
2557 goto done;
2558 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2559 if (kerr != KCGI_OK)
2560 goto done;
2561 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2562 if (kerr != KCGI_OK)
2563 goto done;
2564 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2565 KATTR_ID, "header_commit_msg", KATTR__MAX);
2566 if (kerr != KCGI_OK)
2567 goto done;
2568 kerr = khttp_puts(gw_trans->gw_req, str);
2569 if (kerr != KCGI_OK)
2570 goto done;
2571 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2572 done:
2573 if (error == NULL && kerr != KCGI_OK)
2574 error = gw_kcgi_error(kerr);
2575 return error;
2578 static const struct got_error *
2579 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2581 const struct got_error *error = NULL;
2582 enum kcgi_err kerr = KCGI_OK;
2584 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2585 KATTR_ID, "header_tree_title", KATTR__MAX);
2586 if (kerr != KCGI_OK)
2587 goto done;
2588 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2589 if (kerr != KCGI_OK)
2590 goto done;
2591 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2592 if (kerr != KCGI_OK)
2593 goto done;
2594 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2595 KATTR_ID, "header_tree", KATTR__MAX);
2596 if (kerr != KCGI_OK)
2597 goto done;
2598 kerr = khtml_puts(gw_trans->gw_html_req, str);
2599 if (kerr != KCGI_OK)
2600 goto done;
2601 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2602 done:
2603 if (error == NULL && kerr != KCGI_OK)
2604 error = gw_kcgi_error(kerr);
2605 return error;
2608 static const struct got_error *
2609 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2610 char *dir)
2612 const struct got_error *error = NULL;
2613 FILE *f = NULL;
2614 char *d_file = NULL;
2615 unsigned int len;
2616 size_t n;
2618 *description = NULL;
2619 if (gw_trans->gw_conf->got_show_repo_description == 0)
2620 return NULL;
2622 if (asprintf(&d_file, "%s/description", dir) == -1)
2623 return got_error_from_errno("asprintf");
2625 f = fopen(d_file, "r");
2626 if (f == NULL) {
2627 if (errno == ENOENT || errno == EACCES)
2628 return NULL;
2629 error = got_error_from_errno2("fopen", d_file);
2630 goto done;
2633 if (fseek(f, 0, SEEK_END) == -1) {
2634 error = got_ferror(f, GOT_ERR_IO);
2635 goto done;
2637 len = ftell(f);
2638 if (len == -1) {
2639 error = got_ferror(f, GOT_ERR_IO);
2640 goto done;
2642 if (fseek(f, 0, SEEK_SET) == -1) {
2643 error = got_ferror(f, GOT_ERR_IO);
2644 goto done;
2646 *description = calloc(len + 1, sizeof(**description));
2647 if (*description == NULL) {
2648 error = got_error_from_errno("calloc");
2649 goto done;
2652 n = fread(*description, 1, len, f);
2653 if (n == 0 && ferror(f))
2654 error = got_ferror(f, GOT_ERR_IO);
2655 done:
2656 if (f != NULL && fclose(f) == -1 && error == NULL)
2657 error = got_error_from_errno("fclose");
2658 free(d_file);
2659 return error;
2662 static const struct got_error *
2663 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2665 struct tm tm;
2666 time_t diff_time;
2667 char *years = "years ago", *months = "months ago";
2668 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2669 char *minutes = "minutes ago", *seconds = "seconds ago";
2670 char *now = "right now";
2671 char *s;
2672 char datebuf[29];
2674 *repo_age = NULL;
2676 switch (ref_tm) {
2677 case TM_DIFF:
2678 diff_time = time(NULL) - committer_time;
2679 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2680 if (asprintf(repo_age, "%lld %s",
2681 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2682 return got_error_from_errno("asprintf");
2683 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2684 if (asprintf(repo_age, "%lld %s",
2685 (diff_time / 60 / 60 / 24 / (365 / 12)),
2686 months) == -1)
2687 return got_error_from_errno("asprintf");
2688 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2689 if (asprintf(repo_age, "%lld %s",
2690 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2691 return got_error_from_errno("asprintf");
2692 } else if (diff_time > 60 * 60 * 24 * 2) {
2693 if (asprintf(repo_age, "%lld %s",
2694 (diff_time / 60 / 60 / 24), days) == -1)
2695 return got_error_from_errno("asprintf");
2696 } else if (diff_time > 60 * 60 * 2) {
2697 if (asprintf(repo_age, "%lld %s",
2698 (diff_time / 60 / 60), hours) == -1)
2699 return got_error_from_errno("asprintf");
2700 } else if (diff_time > 60 * 2) {
2701 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2702 minutes) == -1)
2703 return got_error_from_errno("asprintf");
2704 } else if (diff_time > 2) {
2705 if (asprintf(repo_age, "%lld %s", diff_time,
2706 seconds) == -1)
2707 return got_error_from_errno("asprintf");
2708 } else {
2709 if (asprintf(repo_age, "%s", now) == -1)
2710 return got_error_from_errno("asprintf");
2712 break;
2713 case TM_LONG:
2714 if (gmtime_r(&committer_time, &tm) == NULL)
2715 return got_error_from_errno("gmtime_r");
2717 s = asctime_r(&tm, datebuf);
2718 if (s == NULL)
2719 return got_error_from_errno("asctime_r");
2721 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2722 return got_error_from_errno("asprintf");
2723 break;
2725 return NULL;
2728 static const struct got_error *
2729 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2730 const char *refname, int ref_tm)
2732 const struct got_error *error = NULL;
2733 struct got_repository *repo = NULL;
2734 struct got_commit_object *commit = NULL;
2735 struct got_reflist_head refs;
2736 struct got_reflist_entry *re;
2737 time_t committer_time = 0, cmp_time = 0;
2739 *repo_age = NULL;
2740 SIMPLEQ_INIT(&refs);
2742 if (gw_trans->gw_conf->got_show_repo_age == 0)
2743 return NULL;
2745 if (gw_trans->repo)
2746 repo = gw_trans->repo;
2747 else {
2748 error = got_repo_open(&repo, dir, NULL);
2749 if (error)
2750 return error;
2753 error = got_ref_list(&refs, repo, "refs/heads",
2754 got_ref_cmp_by_name, NULL);
2755 if (error)
2756 goto done;
2759 * Find the youngest branch tip in the repository, or the age of
2760 * the a specific branch tip if a name was provided by the caller.
2762 SIMPLEQ_FOREACH(re, &refs, entry) {
2763 struct got_object_id *id = NULL;
2765 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
2766 continue;
2768 error = got_ref_resolve(&id, repo, re->ref);
2769 if (error)
2770 goto done;
2772 error = got_object_open_as_commit(&commit, repo, id);
2773 free(id);
2774 if (error)
2775 goto done;
2777 committer_time =
2778 got_object_commit_get_committer_time(commit);
2779 got_object_commit_close(commit);
2780 if (cmp_time < committer_time)
2781 cmp_time = committer_time;
2783 if (refname)
2784 break;
2787 if (cmp_time != 0) {
2788 committer_time = cmp_time;
2789 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2791 done:
2792 got_ref_list_free(&refs);
2793 if (gw_trans->repo == NULL)
2794 got_repo_close(repo);
2795 return error;
2798 static const struct got_error *
2799 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2801 const struct got_error *error;
2802 FILE *f = NULL;
2803 struct got_object_id *id1 = NULL, *id2 = NULL;
2804 char *label1 = NULL, *label2 = NULL, *line = NULL;
2805 int obj_type;
2806 size_t linesize = 0;
2807 ssize_t linelen;
2808 enum kcgi_err kerr = KCGI_OK;
2810 f = got_opentemp();
2811 if (f == NULL)
2812 return NULL;
2814 if (header->parent_id != NULL &&
2815 strncmp(header->parent_id, "/dev/null", 9) != 0) {
2816 error = got_repo_match_object_id(&id1, &label1,
2817 header->parent_id, GOT_OBJ_TYPE_ANY, 1, gw_trans->repo);
2818 if (error)
2819 goto done;
2822 error = got_repo_match_object_id(&id2, &label2,
2823 header->commit_id, GOT_OBJ_TYPE_ANY, 1, gw_trans->repo);
2824 if (error)
2825 goto done;
2827 error = got_object_get_type(&obj_type, gw_trans->repo, id2);
2828 if (error)
2829 goto done;
2830 switch (obj_type) {
2831 case GOT_OBJ_TYPE_BLOB:
2832 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
2833 gw_trans->repo, f);
2834 break;
2835 case GOT_OBJ_TYPE_TREE:
2836 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
2837 gw_trans->repo, f);
2838 break;
2839 case GOT_OBJ_TYPE_COMMIT:
2840 error = got_diff_objects_as_commits(id1, id2, 3, 0,
2841 gw_trans->repo, f);
2842 break;
2843 default:
2844 error = got_error(GOT_ERR_OBJ_TYPE);
2846 if (error)
2847 goto done;
2849 if (fseek(f, 0, SEEK_SET) == -1) {
2850 error = got_ferror(f, GOT_ERR_IO);
2851 goto done;
2854 while ((linelen = getline(&line, &linesize, f)) != -1) {
2855 error = gw_colordiff_line(gw_trans, line);
2856 if (error)
2857 goto done;
2858 /* XXX: KHTML_PRETTY breaks this */
2859 kerr = khtml_puts(gw_trans->gw_html_req, line);
2860 if (kerr != KCGI_OK)
2861 goto done;
2862 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2863 if (kerr != KCGI_OK)
2864 goto done;
2866 if (linelen == -1 && ferror(f))
2867 error = got_error_from_errno("getline");
2868 done:
2869 if (f && fclose(f) == -1 && error == NULL)
2870 error = got_error_from_errno("fclose");
2871 free(line);
2872 free(label1);
2873 free(label2);
2874 free(id1);
2875 free(id2);
2877 if (error == NULL && kerr != KCGI_OK)
2878 error = gw_kcgi_error(kerr);
2879 return error;
2882 static const struct got_error *
2883 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2885 const struct got_error *error = NULL;
2886 struct got_repository *repo;
2887 const char *gitconfig_owner;
2889 *owner = NULL;
2891 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2892 return NULL;
2894 error = got_repo_open(&repo, dir, NULL);
2895 if (error)
2896 return error;
2897 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2898 if (gitconfig_owner) {
2899 *owner = strdup(gitconfig_owner);
2900 if (*owner == NULL)
2901 error = got_error_from_errno("strdup");
2903 got_repo_close(repo);
2904 return error;
2907 static const struct got_error *
2908 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2910 const struct got_error *error = NULL;
2911 FILE *f;
2912 char *d_file = NULL;
2913 unsigned int len;
2914 size_t n;
2916 *url = NULL;
2918 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2919 return got_error_from_errno("asprintf");
2921 f = fopen(d_file, "r");
2922 if (f == NULL) {
2923 if (errno != ENOENT && errno != EACCES)
2924 error = got_error_from_errno2("fopen", d_file);
2925 goto done;
2928 if (fseek(f, 0, SEEK_END) == -1) {
2929 error = got_ferror(f, GOT_ERR_IO);
2930 goto done;
2932 len = ftell(f);
2933 if (len == -1) {
2934 error = got_ferror(f, GOT_ERR_IO);
2935 goto done;
2937 if (fseek(f, 0, SEEK_SET) == -1) {
2938 error = got_ferror(f, GOT_ERR_IO);
2939 goto done;
2942 *url = calloc(len + 1, sizeof(**url));
2943 if (*url == NULL) {
2944 error = got_error_from_errno("calloc");
2945 goto done;
2948 n = fread(*url, 1, len, f);
2949 if (n == 0 && ferror(f))
2950 error = got_ferror(f, GOT_ERR_IO);
2951 done:
2952 if (f && fclose(f) == -1 && error == NULL)
2953 error = got_error_from_errno("fclose");
2954 free(d_file);
2955 return NULL;
2958 static const struct got_error *
2959 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
2960 int limit, int tag_type)
2962 const struct got_error *error = NULL;
2963 struct got_reflist_head refs;
2964 struct got_reflist_entry *re;
2965 char *age = NULL;
2966 char *id_str = NULL, *newline, *href_commits = NULL;
2967 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
2968 struct got_tag_object *tag = NULL;
2969 enum kcgi_err kerr = KCGI_OK;
2970 int summary_header_displayed = 0, start_tag = 0, chk_next = 0;
2971 int prev_set = 0, tag_count = 0;
2973 SIMPLEQ_INIT(&refs);
2975 error = got_ref_list(&refs, gw_trans->repo, "refs/tags",
2976 got_ref_cmp_tags, gw_trans->repo);
2977 if (error)
2978 goto done;
2980 SIMPLEQ_FOREACH(re, &refs, entry) {
2981 const char *refname;
2982 const char *tagger;
2983 const char *tag_commit;
2984 time_t tagger_time;
2985 struct got_object_id *id;
2986 struct got_commit_object *commit = NULL;
2988 refname = got_ref_get_name(re->ref);
2989 if (strncmp(refname, "refs/tags/", 10) != 0)
2990 continue;
2991 refname += 10;
2993 error = got_ref_resolve(&id, gw_trans->repo, re->ref);
2994 if (error)
2995 goto done;
2997 error = got_object_open_as_tag(&tag, gw_trans->repo, id);
2998 if (error) {
2999 if (error->code != GOT_ERR_OBJ_TYPE) {
3000 free(id);
3001 goto done;
3003 /* "lightweight" tag */
3004 error = got_object_open_as_commit(&commit,
3005 gw_trans->repo, id);
3006 if (error) {
3007 free(id);
3008 goto done;
3010 tagger = got_object_commit_get_committer(commit);
3011 tagger_time =
3012 got_object_commit_get_committer_time(commit);
3013 error = got_object_id_str(&id_str, id);
3014 free(id);
3015 } else {
3016 free(id);
3017 tagger = got_object_tag_get_tagger(tag);
3018 tagger_time = got_object_tag_get_tagger_time(tag);
3019 error = got_object_id_str(&id_str,
3020 got_object_tag_get_object_id(tag));
3022 if (error)
3023 goto done;
3025 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
3026 strlen(id_str)) != 0)
3027 continue;
3029 if (tag_type == TAGBRIEF && gw_trans->commit_id &&
3030 start_tag == 0 && strncmp(id_str, header->commit_id,
3031 strlen(id_str)) != 0) {
3032 continue;
3033 } else {
3034 start_tag = 1;
3037 tag_count++;
3039 if (prev_set == 0 && start_tag == 1) {
3040 gw_trans->next_prev_id = strdup(id_str);
3041 if (gw_trans->next_prev_id == NULL) {
3042 error = got_error_from_errno("strdup");
3043 goto done;
3045 prev_set = 1;
3048 if (chk_next) {
3049 gw_trans->next_id = strdup(id_str);
3050 if (gw_trans->next_id == NULL)
3051 error = got_error_from_errno("strdup");
3052 goto done;
3055 if (commit) {
3056 error = got_object_commit_get_logmsg(&tag_commit0,
3057 commit);
3058 if (error)
3059 goto done;
3060 got_object_commit_close(commit);
3061 } else {
3062 tag_commit0 = strdup(got_object_tag_get_message(tag));
3063 if (tag_commit0 == NULL) {
3064 error = got_error_from_errno("strdup");
3065 goto done;
3069 tag_commit = tag_commit0;
3070 while (*tag_commit == '\n')
3071 tag_commit++;
3073 switch (tag_type) {
3074 case TAGBRIEF:
3075 newline = strchr(tag_commit, '\n');
3076 if (newline)
3077 *newline = '\0';
3079 if (summary_header_displayed == 0) {
3080 kerr = khtml_attr(gw_trans->gw_html_req,
3081 KELEM_DIV, KATTR_ID,
3082 "summary_tags_title_wrapper", KATTR__MAX);
3083 if (kerr != KCGI_OK)
3084 goto done;
3085 kerr = khtml_attr(gw_trans->gw_html_req,
3086 KELEM_DIV, KATTR_ID,
3087 "summary_tags_title", KATTR__MAX);
3088 if (kerr != KCGI_OK)
3089 goto done;
3090 kerr = khtml_puts(gw_trans->gw_html_req,
3091 "Tags");
3092 if (kerr != KCGI_OK)
3093 goto done;
3094 kerr = khtml_closeelem(gw_trans->gw_html_req,
3095 2);
3096 if (kerr != KCGI_OK)
3097 goto done;
3098 kerr = khtml_attr(gw_trans->gw_html_req,
3099 KELEM_DIV, KATTR_ID,
3100 "summary_tags_content", KATTR__MAX);
3101 if (kerr != KCGI_OK)
3102 goto done;
3103 summary_header_displayed = 1;
3106 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3107 KATTR_ID, "tag_wrapper", KATTR__MAX);
3108 if (kerr != KCGI_OK)
3109 goto done;
3110 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3111 KATTR_ID, "tag_age", KATTR__MAX);
3112 if (kerr != KCGI_OK)
3113 goto done;
3114 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
3115 if (error)
3116 goto done;
3117 kerr = khtml_puts(gw_trans->gw_html_req,
3118 age ? age : "");
3119 if (kerr != KCGI_OK)
3120 goto done;
3121 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3122 if (kerr != KCGI_OK)
3123 goto done;
3124 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3125 KATTR_ID, "tag", KATTR__MAX);
3126 if (kerr != KCGI_OK)
3127 goto done;
3128 kerr = khtml_puts(gw_trans->gw_html_req, refname);
3129 if (kerr != KCGI_OK)
3130 goto done;
3131 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3132 if (kerr != KCGI_OK)
3133 goto done;
3134 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3135 KATTR_ID, "tag_name", KATTR__MAX);
3136 if (kerr != KCGI_OK)
3137 goto done;
3138 if (asprintf(&href_tag, "?path=%s&action=tag&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_tag, KATTR__MAX);
3145 if (kerr != KCGI_OK)
3146 goto done;
3147 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
3148 if (kerr != KCGI_OK)
3149 goto done;
3150 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3151 if (kerr != KCGI_OK)
3152 goto done;
3154 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3155 KATTR_ID, "navs_wrapper", KATTR__MAX);
3156 if (kerr != KCGI_OK)
3157 goto done;
3158 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3159 KATTR_ID, "navs", KATTR__MAX);
3160 if (kerr != KCGI_OK)
3161 goto done;
3163 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3164 KATTR_HREF, href_tag, KATTR__MAX);
3165 if (kerr != KCGI_OK)
3166 goto done;
3167 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
3168 if (kerr != KCGI_OK)
3169 goto done;
3170 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3171 if (kerr != KCGI_OK)
3172 goto done;
3174 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3175 if (kerr != KCGI_OK)
3176 goto done;
3177 if (asprintf(&href_briefs,
3178 "?path=%s&action=briefs&commit=%s",
3179 gw_trans->repo_name, id_str) == -1) {
3180 error = got_error_from_errno("asprintf");
3181 goto done;
3183 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3184 KATTR_HREF, href_briefs, KATTR__MAX);
3185 if (kerr != KCGI_OK)
3186 goto done;
3187 kerr = khtml_puts(gw_trans->gw_html_req,
3188 "commit briefs");
3189 if (kerr != KCGI_OK)
3190 goto done;
3191 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3192 if (kerr != KCGI_OK)
3193 goto done;
3195 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3196 if (kerr != KCGI_OK)
3197 goto done;
3199 if (asprintf(&href_commits,
3200 "?path=%s&action=commits&commit=%s",
3201 gw_trans->repo_name, id_str) == -1) {
3202 error = got_error_from_errno("asprintf");
3203 goto done;
3205 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3206 KATTR_HREF, href_commits, KATTR__MAX);
3207 if (kerr != KCGI_OK)
3208 goto done;
3209 kerr = khtml_puts(gw_trans->gw_html_req,
3210 "commits");
3211 if (kerr != KCGI_OK)
3212 goto done;
3213 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3214 if (kerr != KCGI_OK)
3215 goto done;
3217 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3218 KATTR_ID, "dotted_line", KATTR__MAX);
3219 if (kerr != KCGI_OK)
3220 goto done;
3221 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3222 if (kerr != KCGI_OK)
3223 goto done;
3224 break;
3225 case TAGFULL:
3226 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3227 KATTR_ID, "tag_info_date_title", KATTR__MAX);
3228 if (kerr != KCGI_OK)
3229 goto done;
3230 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
3231 if (kerr != KCGI_OK)
3232 goto done;
3233 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3234 if (kerr != KCGI_OK)
3235 goto done;
3236 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3237 KATTR_ID, "tag_info_date", KATTR__MAX);
3238 if (kerr != KCGI_OK)
3239 goto done;
3240 error = gw_get_time_str(&age, tagger_time, TM_LONG);
3241 if (error)
3242 goto done;
3243 kerr = khtml_puts(gw_trans->gw_html_req,
3244 age ? age : "");
3245 if (kerr != KCGI_OK)
3246 goto done;
3247 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3248 if (kerr != KCGI_OK)
3249 goto done;
3251 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3252 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
3253 if (kerr != KCGI_OK)
3254 goto done;
3255 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
3256 if (kerr != KCGI_OK)
3257 goto done;
3258 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3259 if (kerr != KCGI_OK)
3260 goto done;
3261 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3262 KATTR_ID, "tag_info_date", KATTR__MAX);
3263 if (kerr != KCGI_OK)
3264 goto done;
3265 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
3266 if (kerr != KCGI_OK)
3267 goto done;
3268 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3269 if (kerr != KCGI_OK)
3270 goto done;
3272 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3273 KATTR_ID, "tag_info", KATTR__MAX);
3274 if (kerr != KCGI_OK)
3275 goto done;
3276 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
3277 if (kerr != KCGI_OK)
3278 goto done;
3279 break;
3280 default:
3281 break;
3283 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3284 if (kerr != KCGI_OK)
3285 goto done;
3287 if (limit && --limit == 0)
3288 chk_next = 1;
3290 if (tag)
3291 got_object_tag_close(tag);
3292 tag = NULL;
3293 free(id_str);
3294 id_str = NULL;
3295 free(age);
3296 age = NULL;
3297 free(tag_commit0);
3298 tag_commit0 = NULL;
3299 free(href_tag);
3300 href_tag = NULL;
3301 free(href_briefs);
3302 href_briefs = NULL;
3303 free(href_commits);
3304 href_commits = NULL;
3306 if (tag_count == 0) {
3307 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3308 "summary_tags_title_wrapper", KATTR__MAX);
3309 if (kerr != KCGI_OK)
3310 goto done;
3311 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3312 "summary_tags_title", KATTR__MAX);
3313 if (kerr != KCGI_OK)
3314 goto done;
3315 kerr = khtml_puts(gw_trans->gw_html_req, "Tags");
3316 if (kerr != KCGI_OK)
3317 goto done;
3318 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3319 if (kerr != KCGI_OK)
3320 goto done;
3321 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3322 "summary_tags_content", KATTR__MAX);
3323 if (kerr != KCGI_OK)
3324 goto done;
3325 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3326 "tags_info", KATTR__MAX);
3327 if (kerr != KCGI_OK)
3328 goto done;
3329 kerr = khttp_puts(gw_trans->gw_req,
3330 "There are no tags for this repo.");
3331 if (kerr != KCGI_OK)
3332 goto done;
3333 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3335 done:
3336 if (tag)
3337 got_object_tag_close(tag);
3338 free(id_str);
3339 free(age);
3340 free(tag_commit0);
3341 free(href_tag);
3342 free(href_briefs);
3343 free(href_commits);
3344 got_ref_list_free(&refs);
3345 if (error == NULL && kerr != KCGI_OK)
3346 error = gw_kcgi_error(kerr);
3347 return error;
3350 static void
3351 gw_free_header(struct gw_header *header)
3353 free(header->path);
3354 free(header->author);
3355 free(header->committer);
3356 free(header->refs_str);
3357 free(header->commit_id);
3358 free(header->parent_id);
3359 free(header->tree_id);
3360 free(header->commit_msg);
3363 static struct gw_header *
3364 gw_init_header()
3366 struct gw_header *header;
3368 header = malloc(sizeof(*header));
3369 if (header == NULL)
3370 return NULL;
3372 header->path = NULL;
3373 SIMPLEQ_INIT(&header->refs);
3375 header->refs_str = NULL;
3376 header->commit_id = NULL;
3377 header->committer = NULL;
3378 header->author = NULL;
3379 header->parent_id = NULL;
3380 header->tree_id = NULL;
3381 header->commit_msg = NULL;
3383 return header;
3386 static const struct got_error *
3387 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
3388 int limit, struct got_object_id *id)
3390 const struct got_error *error = NULL;
3391 struct got_commit_graph *graph = NULL;
3392 struct got_commit_object *commit = NULL;
3393 int chk_next = 0, chk_multi = 0, prev_set = 0;
3395 error = got_commit_graph_open(&graph, header->path, 0);
3396 if (error)
3397 return error;
3399 error = got_commit_graph_iter_start(graph, id, gw_trans->repo, NULL,
3400 NULL);
3401 if (error)
3402 goto done;
3404 for (;;) {
3405 error = got_commit_graph_iter_next(&id, graph, gw_trans->repo,
3406 NULL, NULL);
3407 if (error) {
3408 if (error->code == GOT_ERR_ITER_COMPLETED)
3409 error = NULL;
3410 goto done;
3412 if (id == NULL)
3413 goto done;
3415 error = got_object_open_as_commit(&commit, gw_trans->repo, id);
3416 if (error)
3417 goto done;
3418 if (limit == 1 && chk_multi == 0 &&
3419 gw_trans->gw_conf->got_max_commits_display != 1) {
3420 error = gw_get_commit(gw_trans, header, commit, id);
3421 if (error)
3422 goto done;
3423 } else {
3424 chk_multi = 1;
3425 struct gw_header *n_header = NULL;
3426 if ((n_header = gw_init_header()) == NULL) {
3427 error = got_error_from_errno("malloc");
3428 goto done;
3430 error = got_ref_list(&n_header->refs, gw_trans->repo,
3431 NULL, got_ref_cmp_by_name, NULL);
3432 if (error)
3433 goto done;
3435 error = gw_get_commit(gw_trans, n_header, commit, id);
3436 if (error)
3437 goto done;
3438 got_ref_list_free(&n_header->refs);
3441 * we have a commit_id now, so copy it to next_prev_id
3442 * for navigation through gw_briefs and gw_commits
3444 if (gw_trans->next_prev_id == NULL && prev_set == 0 &&
3445 (gw_trans->action == GW_BRIEFS ||
3446 gw_trans->action == GW_COMMITS ||
3447 gw_trans->action == GW_SUMMARY)) {
3448 prev_set = 1;
3449 gw_trans->next_prev_id =
3450 strdup(n_header->commit_id);
3451 if (gw_trans->next_prev_id == NULL) {
3452 error = got_error_from_errno("strdup");
3453 goto done;
3458 * check for one more commit before breaking,
3459 * so we know whether to navicate through gw_briefs
3460 * gw_commits and gw_summary
3462 if (chk_next && (gw_trans->action == GW_BRIEFS||
3463 gw_trans->action == GW_COMMITS ||
3464 gw_trans->action == GW_SUMMARY)) {
3465 gw_trans->next_id = strdup(n_header->commit_id);
3466 if (gw_trans->next_id == NULL)
3467 error = got_error_from_errno("strdup");
3468 goto done;
3471 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3472 entry);
3474 if (error || (limit && --limit == 0)) {
3475 if (chk_multi == 0)
3476 break;
3477 chk_next = 1;
3480 done:
3481 if (commit != NULL)
3482 got_object_commit_close(commit);
3483 if (graph)
3484 got_commit_graph_close(graph);
3485 return error;
3488 static const struct got_error *
3489 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header,
3490 struct got_commit_object *commit, struct got_object_id *id)
3492 const struct got_error *error = NULL;
3493 struct got_reflist_entry *re;
3494 struct got_object_id *id2 = NULL;
3495 struct got_object_qid *parent_id;
3496 char *commit_msg = NULL, *commit_msg0;
3498 /*print commit*/
3499 SIMPLEQ_FOREACH(re, &header->refs, entry) {
3500 char *s;
3501 const char *name;
3502 struct got_tag_object *tag = NULL;
3503 int cmp;
3505 if (got_ref_is_symbolic(re->ref))
3506 continue;
3508 name = got_ref_get_name(re->ref);
3509 if (strncmp(name, "refs/", 5) == 0)
3510 name += 5;
3511 if (strncmp(name, "got/", 4) == 0)
3512 continue;
3513 if (strncmp(name, "heads/", 6) == 0)
3514 name += 6;
3515 if (strncmp(name, "remotes/", 8) == 0)
3516 name += 8;
3517 if (strncmp(name, "tags/", 5) == 0) {
3518 error = got_object_open_as_tag(&tag, gw_trans->repo,
3519 re->id);
3520 if (error) {
3521 if (error->code != GOT_ERR_OBJ_TYPE)
3522 continue;
3524 * Ref points at something other
3525 * than a tag.
3527 error = NULL;
3528 tag = NULL;
3531 cmp = got_object_id_cmp(tag ?
3532 got_object_tag_get_object_id(tag) : re->id, id);
3533 if (tag)
3534 got_object_tag_close(tag);
3535 if (cmp != 0)
3536 continue;
3537 s = header->refs_str;
3538 if (asprintf(&header->refs_str, "%s%s%s", s ? s : "",
3539 s ? ", " : "", name) == -1) {
3540 error = got_error_from_errno("asprintf");
3541 free(s);
3542 header->refs_str = NULL;
3543 return error;
3545 free(s);
3548 error = got_object_id_str(&header->commit_id, id);
3549 if (error)
3550 return error;
3552 error = got_object_id_str(&header->tree_id,
3553 got_object_commit_get_tree_id(commit));
3554 if (error)
3555 return error;
3557 if (gw_trans->action == GW_DIFF) {
3558 parent_id = SIMPLEQ_FIRST(
3559 got_object_commit_get_parent_ids(commit));
3560 if (parent_id != NULL) {
3561 id2 = got_object_id_dup(parent_id->id);
3562 free (parent_id);
3563 error = got_object_id_str(&header->parent_id, id2);
3564 if (error)
3565 return error;
3566 free(id2);
3567 } else {
3568 header->parent_id = strdup("/dev/null");
3569 if (header->parent_id == NULL) {
3570 error = got_error_from_errno("strdup");
3571 return error;
3576 header->committer_time =
3577 got_object_commit_get_committer_time(commit);
3579 header->author =
3580 strdup(got_object_commit_get_author(commit));
3581 if (header->author == NULL) {
3582 error = got_error_from_errno("strdup");
3583 return error;
3585 header->committer =
3586 strdup(got_object_commit_get_committer(commit));
3587 if (header->committer == NULL) {
3588 error = got_error_from_errno("strdup");
3589 return error;
3591 error = got_object_commit_get_logmsg(&commit_msg0, commit);
3592 if (error)
3593 return error;
3595 commit_msg = commit_msg0;
3596 while (*commit_msg == '\n')
3597 commit_msg++;
3599 header->commit_msg = strdup(commit_msg);
3600 if (header->commit_msg == NULL)
3601 error = got_error_from_errno("strdup");
3602 free(commit_msg0);
3603 return error;
3606 static const struct got_error *
3607 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3609 const struct got_error *error = NULL;
3610 char *in_repo_path = NULL;
3611 struct got_object_id *id = NULL;
3613 error = got_repo_open(&gw_trans->repo, gw_trans->repo_path, NULL);
3614 if (error)
3615 return error;
3617 if (gw_trans->commit_id == NULL) {
3618 struct got_reference *head_ref;
3619 error = got_ref_open(&head_ref, gw_trans->repo,
3620 gw_trans->headref, 0);
3621 if (error)
3622 return error;
3624 error = got_ref_resolve(&id, gw_trans->repo, head_ref);
3625 got_ref_close(head_ref);
3626 if (error)
3627 return error;
3628 } else {
3629 struct got_reference *ref;
3631 error = got_ref_open(&ref, gw_trans->repo,
3632 gw_trans->commit_id, 0);
3633 if (error == NULL) {
3634 int obj_type;
3635 error = got_ref_resolve(&id, gw_trans->repo, ref);
3636 got_ref_close(ref);
3637 if (error)
3638 return error;
3639 error = got_object_get_type(&obj_type, gw_trans->repo,
3640 id);
3641 if (error)
3642 goto done;
3643 if (obj_type == GOT_OBJ_TYPE_TAG) {
3644 struct got_tag_object *tag;
3645 error = got_object_open_as_tag(&tag,
3646 gw_trans->repo, id);
3647 if (error)
3648 goto done;
3649 if (got_object_tag_get_object_type(tag) !=
3650 GOT_OBJ_TYPE_COMMIT) {
3651 got_object_tag_close(tag);
3652 error = got_error(GOT_ERR_OBJ_TYPE);
3653 goto done;
3655 free(id);
3656 id = got_object_id_dup(
3657 got_object_tag_get_object_id(tag));
3658 if (id == NULL)
3659 error = got_error_from_errno(
3660 "got_object_id_dup");
3661 got_object_tag_close(tag);
3662 if (error)
3663 goto done;
3664 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3665 error = got_error(GOT_ERR_OBJ_TYPE);
3666 goto done;
3669 error = got_repo_match_object_id_prefix(&id,
3670 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT,
3671 gw_trans->repo);
3672 if (error)
3673 goto done;
3676 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
3677 gw_trans->repo_path, 1);
3678 if (error)
3679 goto done;
3681 if (in_repo_path) {
3682 header->path = strdup(in_repo_path);
3683 if (header->path == NULL) {
3684 error = got_error_from_errno("strdup");
3685 goto done;
3689 error = got_ref_list(&header->refs, gw_trans->repo, NULL,
3690 got_ref_cmp_by_name, NULL);
3691 if (error)
3692 goto done;
3694 error = gw_get_commits(gw_trans, header, limit, id);
3695 done:
3696 got_ref_list_free(&header->refs);
3697 free(id);
3698 free(in_repo_path);
3699 return error;
3702 struct blame_line {
3703 int annotated;
3704 char *id_str;
3705 char *committer;
3706 char datebuf[11]; /* YYYY-MM-DD + NUL */
3709 struct gw_blame_cb_args {
3710 struct blame_line *lines;
3711 int nlines;
3712 int nlines_prec;
3713 int lineno_cur;
3714 off_t *line_offsets;
3715 FILE *f;
3716 struct got_repository *repo;
3717 struct gw_trans *gw_trans;
3720 static const struct got_error *
3721 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3723 const struct got_error *err = NULL;
3724 struct gw_blame_cb_args *a = arg;
3725 struct blame_line *bline;
3726 char *line = NULL;
3727 size_t linesize = 0;
3728 struct got_commit_object *commit = NULL;
3729 off_t offset;
3730 struct tm tm;
3731 time_t committer_time;
3732 enum kcgi_err kerr = KCGI_OK;
3734 if (nlines != a->nlines ||
3735 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3736 return got_error(GOT_ERR_RANGE);
3738 if (lineno == -1)
3739 return NULL; /* no change in this commit */
3741 /* Annotate this line. */
3742 bline = &a->lines[lineno - 1];
3743 if (bline->annotated)
3744 return NULL;
3745 err = got_object_id_str(&bline->id_str, id);
3746 if (err)
3747 return err;
3749 err = got_object_open_as_commit(&commit, a->repo, id);
3750 if (err)
3751 goto done;
3753 bline->committer = strdup(got_object_commit_get_committer(commit));
3754 if (bline->committer == NULL) {
3755 err = got_error_from_errno("strdup");
3756 goto done;
3759 committer_time = got_object_commit_get_committer_time(commit);
3760 if (localtime_r(&committer_time, &tm) == NULL)
3761 return got_error_from_errno("localtime_r");
3762 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3763 &tm) >= sizeof(bline->datebuf)) {
3764 err = got_error(GOT_ERR_NO_SPACE);
3765 goto done;
3767 bline->annotated = 1;
3769 /* Print lines annotated so far. */
3770 bline = &a->lines[a->lineno_cur - 1];
3771 if (!bline->annotated)
3772 goto done;
3774 offset = a->line_offsets[a->lineno_cur - 1];
3775 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3776 err = got_error_from_errno("fseeko");
3777 goto done;
3780 while (bline->annotated) {
3781 char *smallerthan, *at, *nl, *committer;
3782 char *lineno = NULL, *href_diff = NULL, *href_link = NULL;
3783 size_t len;
3785 if (getline(&line, &linesize, a->f) == -1) {
3786 if (ferror(a->f))
3787 err = got_error_from_errno("getline");
3788 break;
3791 committer = bline->committer;
3792 smallerthan = strchr(committer, '<');
3793 if (smallerthan && smallerthan[1] != '\0')
3794 committer = smallerthan + 1;
3795 at = strchr(committer, '@');
3796 if (at)
3797 *at = '\0';
3798 len = strlen(committer);
3799 if (len >= 9)
3800 committer[8] = '\0';
3802 nl = strchr(line, '\n');
3803 if (nl)
3804 *nl = '\0';
3806 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3807 "blame_wrapper", KATTR__MAX);
3808 if (kerr != KCGI_OK)
3809 goto err;
3810 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3811 "blame_number", KATTR__MAX);
3812 if (kerr != KCGI_OK)
3813 goto err;
3814 if (asprintf(&lineno, "%.*d", a->nlines_prec,
3815 a->lineno_cur) == -1)
3816 goto err;
3817 kerr = khtml_puts(a->gw_trans->gw_html_req, lineno);
3818 if (kerr != KCGI_OK)
3819 goto err;
3820 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3821 if (kerr != KCGI_OK)
3822 goto err;
3824 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3825 "blame_hash", KATTR__MAX);
3826 if (kerr != KCGI_OK)
3827 goto err;
3829 if (asprintf(&href_diff,
3830 "?path=%s&action=diff&commit=%s",
3831 a->gw_trans->repo_name, bline->id_str) == -1) {
3832 err = got_error_from_errno("asprintf");
3833 goto err;
3835 if (asprintf(&href_link, "%.8s", bline->id_str) == -1) {
3836 err = got_error_from_errno("asprintf");
3837 goto err;
3839 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3840 KATTR_HREF, href_diff, KATTR__MAX);
3841 if (kerr != KCGI_OK)
3842 goto done;
3843 kerr = khtml_puts(a->gw_trans->gw_html_req, href_link);
3844 if (kerr != KCGI_OK)
3845 goto err;
3846 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3847 if (kerr != KCGI_OK)
3848 goto err;
3850 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3851 "blame_date", KATTR__MAX);
3852 if (kerr != KCGI_OK)
3853 goto err;
3854 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3855 if (kerr != KCGI_OK)
3856 goto err;
3857 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3858 if (kerr != KCGI_OK)
3859 goto err;
3861 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3862 "blame_author", KATTR__MAX);
3863 if (kerr != KCGI_OK)
3864 goto err;
3865 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
3866 if (kerr != KCGI_OK)
3867 goto err;
3868 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3869 if (kerr != KCGI_OK)
3870 goto err;
3872 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3873 "blame_code", KATTR__MAX);
3874 if (kerr != KCGI_OK)
3875 goto err;
3876 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
3877 if (kerr != KCGI_OK)
3878 goto err;
3879 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3880 if (kerr != KCGI_OK)
3881 goto err;
3883 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3884 if (kerr != KCGI_OK)
3885 goto err;
3887 a->lineno_cur++;
3888 bline = &a->lines[a->lineno_cur - 1];
3889 err:
3890 free(lineno);
3891 free(href_diff);
3892 free(href_link);
3894 done:
3895 if (commit)
3896 got_object_commit_close(commit);
3897 free(line);
3898 if (err == NULL && kerr != KCGI_OK)
3899 err = gw_kcgi_error(kerr);
3900 return err;
3903 static const struct got_error *
3904 gw_output_file_blame(struct gw_trans *gw_trans)
3906 const struct got_error *error = NULL;
3907 struct got_object_id *obj_id = NULL;
3908 struct got_object_id *commit_id = NULL;
3909 struct got_blob_object *blob = NULL;
3910 char *path = NULL, *in_repo_path = NULL;
3911 struct gw_blame_cb_args bca;
3912 int i, obj_type;
3913 size_t filesize;
3915 if (asprintf(&path, "%s%s%s",
3916 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3917 gw_trans->repo_folder ? "/" : "",
3918 gw_trans->repo_file) == -1) {
3919 error = got_error_from_errno("asprintf");
3920 goto done;
3923 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path, 1);
3924 if (error)
3925 goto done;
3927 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
3928 GOT_OBJ_TYPE_COMMIT, 1, gw_trans->repo);
3929 if (error)
3930 goto done;
3932 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
3933 in_repo_path);
3934 if (error)
3935 goto done;
3937 if (obj_id == NULL) {
3938 error = got_error(GOT_ERR_NO_OBJ);
3939 goto done;
3942 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
3943 if (error)
3944 goto done;
3946 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3947 error = got_error(GOT_ERR_OBJ_TYPE);
3948 goto done;
3951 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
3952 if (error)
3953 goto done;
3955 bca.f = got_opentemp();
3956 if (bca.f == NULL) {
3957 error = got_error_from_errno("got_opentemp");
3958 goto done;
3960 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3961 &bca.line_offsets, bca.f, blob);
3962 if (error || bca.nlines == 0)
3963 goto done;
3965 /* Don't include \n at EOF in the blame line count. */
3966 if (bca.line_offsets[bca.nlines - 1] == filesize)
3967 bca.nlines--;
3969 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3970 if (bca.lines == NULL) {
3971 error = got_error_from_errno("calloc");
3972 goto done;
3974 bca.lineno_cur = 1;
3975 bca.nlines_prec = 0;
3976 i = bca.nlines;
3977 while (i > 0) {
3978 i /= 10;
3979 bca.nlines_prec++;
3981 bca.repo = gw_trans->repo;
3982 bca.gw_trans = gw_trans;
3984 error = got_blame(in_repo_path, commit_id, gw_trans->repo, gw_blame_cb,
3985 &bca, NULL, NULL);
3986 done:
3987 free(in_repo_path);
3988 free(commit_id);
3989 free(obj_id);
3990 free(path);
3992 if (blob) {
3993 free(bca.line_offsets);
3994 for (i = 0; i < bca.nlines; i++) {
3995 struct blame_line *bline = &bca.lines[i];
3996 free(bline->id_str);
3997 free(bline->committer);
3999 free(bca.lines);
4000 if (bca.f && fclose(bca.f) == EOF && error == NULL)
4001 error = got_error_from_errno("fclose");
4003 if (blob)
4004 got_object_blob_close(blob);
4005 return error;
4008 static const struct got_error *
4009 gw_output_blob_buf(struct gw_trans *gw_trans)
4011 const struct got_error *error = NULL;
4012 struct got_object_id *obj_id = NULL;
4013 struct got_object_id *commit_id = NULL;
4014 struct got_blob_object *blob = NULL;
4015 char *path = NULL, *in_repo_path = NULL;
4016 int obj_type, set_mime = 0;
4017 size_t len, hdrlen;
4018 const uint8_t *buf;
4019 enum kcgi_err kerr = KCGI_OK;
4021 if (asprintf(&path, "%s%s%s",
4022 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4023 gw_trans->repo_folder ? "/" : "",
4024 gw_trans->repo_file) == -1) {
4025 error = got_error_from_errno("asprintf");
4026 goto done;
4029 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path, 1);
4030 if (error)
4031 goto done;
4033 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
4034 GOT_OBJ_TYPE_COMMIT, 1, gw_trans->repo);
4035 if (error)
4036 goto done;
4038 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
4039 in_repo_path);
4040 if (error)
4041 goto done;
4043 if (obj_id == NULL) {
4044 error = got_error(GOT_ERR_NO_OBJ);
4045 goto done;
4048 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
4049 if (error)
4050 goto done;
4052 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4053 error = got_error(GOT_ERR_OBJ_TYPE);
4054 goto done;
4057 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
4058 if (error)
4059 goto done;
4061 hdrlen = got_object_blob_get_hdrlen(blob);
4062 do {
4063 error = got_object_blob_read_block(&len, blob);
4064 if (error)
4065 goto done;
4066 buf = got_object_blob_get_read_buf(blob);
4069 * Skip blob object header first time around,
4070 * which also contains a zero byte.
4072 buf += hdrlen;
4073 if (set_mime == 0) {
4074 if (isbinary(buf, len - hdrlen))
4075 gw_trans->mime = KMIME_APP_OCTET_STREAM;
4076 else
4077 gw_trans->mime = KMIME_TEXT_PLAIN;
4078 set_mime = 1;
4079 error = gw_display_index(gw_trans);
4080 if (error)
4081 goto done;
4083 kerr = khttp_write(gw_trans->gw_req, buf, len - hdrlen);
4084 if (kerr != KCGI_OK)
4085 goto done;
4086 hdrlen = 0;
4087 } while (len != 0);
4088 done:
4089 free(in_repo_path);
4090 free(commit_id);
4091 free(obj_id);
4092 free(path);
4093 if (blob)
4094 got_object_blob_close(blob);
4095 if (error == NULL && kerr != KCGI_OK)
4096 error = gw_kcgi_error(kerr);
4097 return error;
4100 static const struct got_error *
4101 gw_output_repo_tree(struct gw_trans *gw_trans)
4103 const struct got_error *error = NULL;
4104 struct got_object_id *tree_id = NULL, *commit_id = NULL;
4105 struct got_tree_object *tree = NULL;
4106 char *path = NULL, *in_repo_path = NULL;
4107 char *id_str = NULL;
4108 char *build_folder = NULL;
4109 char *href_blob = NULL, *href_blame = NULL;
4110 const char *class = NULL;
4111 int nentries, i, class_flip = 0;
4112 enum kcgi_err kerr = KCGI_OK;
4114 if (gw_trans->repo_folder != NULL) {
4115 path = strdup(gw_trans->repo_folder);
4116 if (path == NULL) {
4117 error = got_error_from_errno("strdup");
4118 goto done;
4120 } else {
4121 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
4122 gw_trans->repo_path, 1);
4123 if (error)
4124 goto done;
4125 free(path);
4126 path = in_repo_path;
4129 if (gw_trans->commit_id == NULL) {
4130 struct got_reference *head_ref;
4131 error = got_ref_open(&head_ref, gw_trans->repo,
4132 gw_trans->headref, 0);
4133 if (error)
4134 goto done;
4135 error = got_ref_resolve(&commit_id, gw_trans->repo, head_ref);
4136 if (error)
4137 goto done;
4138 got_ref_close(head_ref);
4140 * gw_trans->commit_id was not parsed from the querystring
4141 * we hit this code path from gw_index, where we don't know the
4142 * commit values for the tree link yet, so set
4143 * gw_trans->commit_id here to continue further into the tree
4145 error = got_object_id_str(&gw_trans->commit_id, commit_id);
4146 if (error)
4147 goto done;
4149 } else {
4150 error = got_repo_match_object_id(&commit_id, NULL,
4151 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT, 1,
4152 gw_trans->repo);
4153 if (error)
4154 goto done;
4157 error = got_object_id_by_path(&tree_id, gw_trans->repo, commit_id,
4158 path);
4159 if (error)
4160 goto done;
4162 error = got_object_open_as_tree(&tree, gw_trans->repo, tree_id);
4163 if (error)
4164 goto done;
4166 nentries = got_object_tree_get_nentries(tree);
4167 for (i = 0; i < nentries; i++) {
4168 struct got_tree_entry *te;
4169 const char *modestr = "";
4170 mode_t mode;
4172 te = got_object_tree_get_entry(tree, i);
4174 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
4175 if (error)
4176 goto done;
4178 mode = got_tree_entry_get_mode(te);
4179 if (got_object_tree_entry_is_submodule(te))
4180 modestr = "$";
4181 else if (S_ISLNK(mode))
4182 modestr = "@";
4183 else if (S_ISDIR(mode))
4184 modestr = "/";
4185 else if (mode & S_IXUSR)
4186 modestr = "*";
4188 if (class_flip == 0) {
4189 class = "back_lightgray";
4190 class_flip = 1;
4191 } else {
4192 class = "back_white";
4193 class_flip = 0;
4196 if (S_ISDIR(mode)) {
4197 if (asprintf(&build_folder, "%s/%s",
4198 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4199 got_tree_entry_get_name(te)) == -1) {
4200 error = got_error_from_errno("asprintf");
4201 goto done;
4203 if (asprintf(&href_blob,
4204 "?path=%s&action=%s&commit=%s&folder=%s",
4205 gw_trans->repo_name, gw_get_action_name(gw_trans),
4206 gw_trans->commit_id, build_folder) == -1) {
4207 error = got_error_from_errno("asprintf");
4208 goto done;
4211 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4212 KATTR_ID, "tree_wrapper", KATTR__MAX);
4213 if (kerr != KCGI_OK)
4214 goto done;
4215 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4216 KATTR_ID, "tree_line", KATTR_CLASS, class,
4217 KATTR__MAX);
4218 if (kerr != KCGI_OK)
4219 goto done;
4220 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4221 KATTR_HREF, href_blob, KATTR_CLASS,
4222 "diff_directory", KATTR__MAX);
4223 if (kerr != KCGI_OK)
4224 goto done;
4225 kerr = khtml_puts(gw_trans->gw_html_req,
4226 got_tree_entry_get_name(te));
4227 if (kerr != KCGI_OK)
4228 goto done;
4229 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
4230 if (kerr != KCGI_OK)
4231 goto done;
4232 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4233 if (kerr != KCGI_OK)
4234 goto done;
4235 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4236 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
4237 KATTR__MAX);
4238 if (kerr != KCGI_OK)
4239 goto done;
4240 kerr = khtml_entity(gw_trans->gw_html_req,
4241 KENTITY_nbsp);
4242 if (kerr != KCGI_OK)
4243 goto done;
4244 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4245 if (kerr != KCGI_OK)
4246 goto done;
4247 } else {
4248 if (asprintf(&href_blob,
4249 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
4250 gw_trans->repo_name, "blob", gw_trans->commit_id,
4251 got_tree_entry_get_name(te),
4252 gw_trans->repo_folder ?
4253 gw_trans->repo_folder : "") == -1) {
4254 error = got_error_from_errno("asprintf");
4255 goto done;
4257 if (asprintf(&href_blame,
4258 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
4259 gw_trans->repo_name, "blame", gw_trans->commit_id,
4260 got_tree_entry_get_name(te),
4261 gw_trans->repo_folder ?
4262 gw_trans->repo_folder : "") == -1) {
4263 error = got_error_from_errno("asprintf");
4264 goto done;
4267 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4268 KATTR_ID, "tree_wrapper", KATTR__MAX);
4269 if (kerr != KCGI_OK)
4270 goto done;
4271 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4272 KATTR_ID, "tree_line", KATTR_CLASS, class,
4273 KATTR__MAX);
4274 if (kerr != KCGI_OK)
4275 goto done;
4276 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4277 KATTR_HREF, href_blob, KATTR__MAX);
4278 if (kerr != KCGI_OK)
4279 goto done;
4280 kerr = khtml_puts(gw_trans->gw_html_req,
4281 got_tree_entry_get_name(te));
4282 if (kerr != KCGI_OK)
4283 goto done;
4284 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
4285 if (kerr != KCGI_OK)
4286 goto done;
4287 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4288 if (kerr != KCGI_OK)
4289 goto done;
4290 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4291 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
4292 KATTR__MAX);
4293 if (kerr != KCGI_OK)
4294 goto done;
4296 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4297 KATTR_HREF, href_blob, KATTR__MAX);
4298 if (kerr != KCGI_OK)
4299 goto done;
4300 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
4301 if (kerr != KCGI_OK)
4302 goto done;
4303 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4304 if (kerr != KCGI_OK)
4305 goto done;
4307 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4308 if (kerr != KCGI_OK)
4309 goto done;
4311 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4312 KATTR_HREF, href_blame, KATTR__MAX);
4313 if (kerr != KCGI_OK)
4314 goto done;
4315 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
4316 if (kerr != KCGI_OK)
4317 goto done;
4319 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4320 if (kerr != KCGI_OK)
4321 goto done;
4323 free(id_str);
4324 id_str = NULL;
4325 free(href_blob);
4326 href_blob = NULL;
4327 free(build_folder);
4328 build_folder = NULL;
4330 done:
4331 if (tree)
4332 got_object_tree_close(tree);
4333 free(id_str);
4334 free(href_blob);
4335 free(href_blame);
4336 free(in_repo_path);
4337 free(tree_id);
4338 free(build_folder);
4339 if (error == NULL && kerr != KCGI_OK)
4340 error = gw_kcgi_error(kerr);
4341 return error;
4344 static const struct got_error *
4345 gw_output_repo_heads(struct gw_trans *gw_trans)
4347 const struct got_error *error = NULL;
4348 struct got_reflist_head refs;
4349 struct got_reflist_entry *re;
4350 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
4351 char *href_commits = NULL;
4352 enum kcgi_err kerr = KCGI_OK;
4354 SIMPLEQ_INIT(&refs);
4356 error = got_ref_list(&refs, gw_trans->repo, "refs/heads",
4357 got_ref_cmp_by_name, NULL);
4358 if (error)
4359 goto done;
4361 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4362 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
4363 if (kerr != KCGI_OK)
4364 goto done;
4365 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4366 KATTR_ID, "summary_heads_title", KATTR__MAX);
4367 if (kerr != KCGI_OK)
4368 goto done;
4369 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
4370 if (kerr != KCGI_OK)
4371 goto done;
4372 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4373 if (kerr != KCGI_OK)
4374 goto done;
4375 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4376 KATTR_ID, "summary_heads_content", KATTR__MAX);
4377 if (kerr != KCGI_OK)
4378 goto done;
4380 SIMPLEQ_FOREACH(re, &refs, entry) {
4381 const char *refname;
4383 if (got_ref_is_symbolic(re->ref))
4384 continue;
4386 refname = got_ref_get_name(re->ref);
4387 if (strncmp(refname, "refs/heads/", 11) != 0)
4388 continue;
4390 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
4391 refname, TM_DIFF);
4392 if (error)
4393 goto done;
4395 if (strncmp(refname, "refs/heads/", 11) == 0)
4396 refname += 11;
4398 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4399 KATTR_ID, "heads_wrapper", KATTR__MAX);
4400 if (kerr != KCGI_OK)
4401 goto done;
4402 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4403 KATTR_ID, "heads_age", KATTR__MAX);
4404 if (kerr != KCGI_OK)
4405 goto done;
4406 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
4407 if (kerr != KCGI_OK)
4408 goto done;
4409 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4410 if (kerr != KCGI_OK)
4411 goto done;
4412 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4413 KATTR_ID, "heads_space", KATTR__MAX);
4414 if (kerr != KCGI_OK)
4415 goto done;
4416 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
4417 if (kerr != KCGI_OK)
4418 goto done;
4419 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4420 if (kerr != KCGI_OK)
4421 goto done;
4422 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4423 KATTR_ID, "head", KATTR__MAX);
4424 if (kerr != KCGI_OK)
4425 goto done;
4426 if (asprintf(&href_summary,
4427 "?path=%s&action=summary&headref=%s",
4428 gw_trans->repo_name, refname) == -1) {
4429 error = got_error_from_errno("asprintf");
4430 goto done;
4432 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4433 href_summary, KATTR__MAX);
4434 kerr = khtml_puts(gw_trans->gw_html_req, refname);
4435 if (kerr != KCGI_OK)
4436 goto done;
4437 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4438 if (kerr != KCGI_OK)
4439 goto done;
4441 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4442 "navs_wrapper", KATTR__MAX);
4443 if (kerr != KCGI_OK)
4444 goto done;
4445 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4446 "navs", KATTR__MAX);
4447 if (kerr != KCGI_OK)
4448 goto done;
4450 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4451 href_summary, KATTR__MAX);
4452 if (kerr != KCGI_OK)
4453 goto done;
4454 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4455 if (kerr != KCGI_OK)
4456 goto done;
4457 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4458 if (kerr != KCGI_OK)
4459 goto done;
4461 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4462 if (kerr != KCGI_OK)
4463 goto done;
4464 if (asprintf(&href_briefs, "?path=%s&action=briefs&headref=%s",
4465 gw_trans->repo_name, refname) == -1) {
4466 error = got_error_from_errno("asprintf");
4467 goto done;
4469 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4470 href_briefs, KATTR__MAX);
4471 if (kerr != KCGI_OK)
4472 goto done;
4473 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4474 if (kerr != KCGI_OK)
4475 goto done;
4476 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4477 if (kerr != KCGI_OK)
4478 goto done;
4480 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4481 if (kerr != KCGI_OK)
4482 goto done;
4484 if (asprintf(&href_commits,
4485 "?path=%s&action=commits&headref=%s",
4486 gw_trans->repo_name, refname) == -1) {
4487 error = got_error_from_errno("asprintf");
4488 goto done;
4490 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4491 href_commits, KATTR__MAX);
4492 if (kerr != KCGI_OK)
4493 goto done;
4494 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4495 if (kerr != KCGI_OK)
4496 goto done;
4497 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4498 if (kerr != KCGI_OK)
4499 goto done;
4501 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4502 "dotted_line", KATTR__MAX);
4503 if (kerr != KCGI_OK)
4504 goto done;
4505 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4506 if (kerr != KCGI_OK)
4507 goto done;
4508 free(href_summary);
4509 href_summary = NULL;
4510 free(href_briefs);
4511 href_briefs = NULL;
4512 free(href_commits);
4513 href_commits = NULL;
4515 done:
4516 got_ref_list_free(&refs);
4517 free(href_summary);
4518 free(href_briefs);
4519 free(href_commits);
4520 return error;
4523 static const struct got_error *
4524 gw_output_site_link(struct gw_trans *gw_trans)
4526 const struct got_error *error = NULL;
4527 char *href_summary = NULL;
4528 enum kcgi_err kerr = KCGI_OK;
4530 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4531 "site_link", KATTR__MAX);
4532 if (kerr != KCGI_OK)
4533 goto done;
4534 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4535 KATTR__MAX);
4536 if (kerr != KCGI_OK)
4537 goto done;
4538 kerr = khtml_puts(gw_trans->gw_html_req,
4539 gw_trans->gw_conf->got_site_link);
4540 if (kerr != KCGI_OK)
4541 goto done;
4542 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4543 if (kerr != KCGI_OK)
4544 goto done;
4546 if (gw_trans->repo_name != NULL) {
4547 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4548 if (kerr != KCGI_OK)
4549 goto done;
4550 if (asprintf(&href_summary, "?path=%s&action=summary",
4551 gw_trans->repo_name) == -1)
4552 goto done;
4553 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4554 href_summary, KATTR__MAX);
4555 if (kerr != KCGI_OK)
4556 goto done;
4557 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4558 if (kerr != KCGI_OK)
4559 goto done;
4560 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4561 if (kerr != KCGI_OK)
4562 goto done;
4563 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4564 if (kerr != KCGI_OK)
4565 goto done;
4566 kerr = khtml_puts(gw_trans->gw_html_req,
4567 gw_get_action_name(gw_trans));
4568 if (kerr != KCGI_OK)
4569 goto done;
4572 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4573 if (kerr != KCGI_OK)
4574 goto done;
4575 done:
4576 free(href_summary);
4577 return error;
4580 static const struct got_error *
4581 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4583 const struct got_error *error = NULL;
4584 char *color = NULL;
4585 enum kcgi_err kerr = KCGI_OK;
4587 if (strncmp(buf, "-", 1) == 0)
4588 color = "diff_minus";
4589 else if (strncmp(buf, "+", 1) == 0)
4590 color = "diff_plus";
4591 else if (strncmp(buf, "@@", 2) == 0)
4592 color = "diff_chunk_header";
4593 else if (strncmp(buf, "@@", 2) == 0)
4594 color = "diff_chunk_header";
4595 else if (strncmp(buf, "commit +", 8) == 0)
4596 color = "diff_meta";
4597 else if (strncmp(buf, "commit -", 8) == 0)
4598 color = "diff_meta";
4599 else if (strncmp(buf, "blob +", 6) == 0)
4600 color = "diff_meta";
4601 else if (strncmp(buf, "blob -", 6) == 0)
4602 color = "diff_meta";
4603 else if (strncmp(buf, "file +", 6) == 0)
4604 color = "diff_meta";
4605 else if (strncmp(buf, "file -", 6) == 0)
4606 color = "diff_meta";
4607 else if (strncmp(buf, "from:", 5) == 0)
4608 color = "diff_author";
4609 else if (strncmp(buf, "via:", 4) == 0)
4610 color = "diff_author";
4611 else if (strncmp(buf, "date:", 5) == 0)
4612 color = "diff_date";
4613 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4614 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4615 if (error == NULL && kerr != KCGI_OK)
4616 error = gw_kcgi_error(kerr);
4617 return error;
4620 int
4621 main(int argc, char *argv[])
4623 const struct got_error *error = NULL;
4624 struct gw_trans *gw_trans;
4625 struct gw_dir *dir = NULL, *tdir;
4626 const char *page = "index";
4627 int gw_malloc = 1;
4628 enum kcgi_err kerr = KCGI_OK;
4630 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4631 errx(1, "malloc");
4633 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4634 errx(1, "malloc");
4636 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4637 errx(1, "malloc");
4639 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4640 errx(1, "malloc");
4642 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4643 if (kerr != KCGI_OK) {
4644 error = gw_kcgi_error(kerr);
4645 goto done;
4648 if ((gw_trans->gw_conf =
4649 malloc(sizeof(struct gotweb_conf))) == NULL) {
4650 gw_malloc = 0;
4651 error = got_error_from_errno("malloc");
4652 goto done;
4655 TAILQ_INIT(&gw_trans->gw_dirs);
4656 TAILQ_INIT(&gw_trans->gw_headers);
4658 gw_trans->action = -1;
4659 gw_trans->page = 0;
4660 gw_trans->repos_total = 0;
4661 gw_trans->repo_path = NULL;
4662 gw_trans->commit_id = NULL;
4663 gw_trans->next_id = NULL;
4664 gw_trans->next_prev_id = NULL;
4665 gw_trans->prev_id = NULL;
4666 gw_trans->prev_prev_id = NULL;
4667 gw_trans->headref = GOT_REF_HEAD;
4668 gw_trans->mime = KMIME_TEXT_HTML;
4669 gw_trans->gw_tmpl->key = gw_templs;
4670 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4671 gw_trans->gw_tmpl->arg = gw_trans;
4672 gw_trans->gw_tmpl->cb = gw_template;
4673 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
4674 if (error)
4675 goto done;
4677 error = gw_parse_querystring(gw_trans);
4678 if (error)
4679 goto done;
4681 if (gw_trans->action == GW_BLOB)
4682 error = gw_blob(gw_trans);
4683 else
4684 error = gw_display_index(gw_trans);
4685 done:
4686 if (gw_malloc) {
4687 free(gw_trans->gw_conf->got_repos_path);
4688 free(gw_trans->gw_conf->got_site_name);
4689 free(gw_trans->gw_conf->got_site_owner);
4690 free(gw_trans->gw_conf->got_site_link);
4691 free(gw_trans->gw_conf->got_logo);
4692 free(gw_trans->gw_conf->got_logo_url);
4693 free(gw_trans->gw_conf);
4694 free(gw_trans->commit_id);
4695 free(gw_trans->next_id);
4696 free(gw_trans->next_prev_id);
4697 free(gw_trans->prev_id);
4698 free(gw_trans->prev_prev_id);
4699 free(gw_trans->repo_path);
4700 if (gw_trans->repo)
4701 got_repo_close(gw_trans->repo);
4703 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4704 free(dir->name);
4705 free(dir->description);
4706 free(dir->age);
4707 free(dir->url);
4708 free(dir->path);
4709 free(dir);
4714 khttp_free(gw_trans->gw_req);
4715 return 0;