Blob


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