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 if (asprintf(&href_prev,
1661 "?path=%s&page=%d&action=tags&commit=%s&prev=%s",
1662 gw_trans->repo_name, gw_trans->page - 1,
1663 gw_trans->prev_id ? gw_trans->prev_id : "",
1664 gw_trans->prev_prev_id ?
1665 gw_trans->prev_prev_id : "") == -1) {
1666 error = got_error_from_errno("asprintf");
1667 goto done;
1669 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1670 KATTR_HREF, href_prev, KATTR__MAX);
1671 if (kerr != KCGI_OK)
1672 goto done;
1673 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1674 if (kerr != KCGI_OK)
1675 goto done;
1676 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1677 if (kerr != KCGI_OK)
1678 goto done;
1681 if (gw_trans->next_id || gw_trans->page > 0) {
1682 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1683 if (kerr != KCGI_OK)
1684 return gw_kcgi_error(kerr);
1687 if (gw_trans->next_id) {
1688 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1689 KATTR_ID, "nav_next", KATTR__MAX);
1690 if (kerr != KCGI_OK)
1691 goto done;
1692 if (asprintf(&href_next,
1693 "?path=%s&page=%d&action=tags" \
1694 "&commit=%s&prev=%s&prev_prev=%s",
1695 gw_trans->repo_name, gw_trans->page + 1,
1696 gw_trans->next_id,
1697 gw_trans->next_prev_id ? gw_trans->next_prev_id : "",
1698 gw_trans->prev_id ?
1699 gw_trans->prev_id : "") == -1) {
1700 error = got_error_from_errno("calloc");
1701 goto done;
1703 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1704 KATTR_HREF, href_next, KATTR__MAX);
1705 if (kerr != KCGI_OK)
1706 goto done;
1707 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1708 if (kerr != KCGI_OK)
1709 goto done;
1710 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1711 if (kerr != KCGI_OK)
1712 goto done;
1715 if (gw_trans->next_id || gw_trans->page > 0) {
1716 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1717 if (kerr != KCGI_OK)
1718 goto done;
1720 done:
1721 gw_free_header(header);
1722 free(href_next);
1723 free(href_prev);
1724 if (error == NULL && kerr != KCGI_OK)
1725 error = gw_kcgi_error(kerr);
1726 return error;
1729 static const struct got_error *
1730 gw_tag(struct gw_trans *gw_trans)
1732 const struct got_error *error = NULL;
1733 struct gw_header *header = NULL;
1734 enum kcgi_err kerr = KCGI_OK;
1736 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1737 return got_error_from_errno("pledge");
1739 if ((header = gw_init_header()) == NULL)
1740 return got_error_from_errno("malloc");
1742 error = gw_apply_unveil(gw_trans->gw_dir->path);
1743 if (error)
1744 goto done;
1746 if (gw_trans->commit_id == NULL) {
1747 error = got_error_msg(GOT_ERR_QUERYSTRING,
1748 "commit required in querystring");
1749 goto done;
1752 error = gw_get_header(gw_trans, header, 1);
1753 if (error)
1754 goto done;
1756 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1757 "tag_header_wrapper", KATTR__MAX);
1758 if (kerr != KCGI_OK)
1759 goto done;
1760 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1761 "tag_header", KATTR__MAX);
1762 if (kerr != KCGI_OK)
1763 goto done;
1764 error = gw_gen_commit_header(gw_trans, header->commit_id,
1765 header->refs_str);
1766 if (error)
1767 goto done;
1768 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1769 if (error)
1770 goto done;
1771 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1772 if (kerr != KCGI_OK)
1773 goto done;
1774 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1775 "dotted_line", KATTR__MAX);
1776 if (kerr != KCGI_OK)
1777 goto done;
1778 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1779 if (kerr != KCGI_OK)
1780 goto done;
1782 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1783 "tree", KATTR__MAX);
1784 if (kerr != KCGI_OK)
1785 goto done;
1787 error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1788 if (error)
1789 goto done;
1791 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1792 done:
1793 gw_free_header(header);
1794 if (error == NULL && kerr != KCGI_OK)
1795 error = gw_kcgi_error(kerr);
1796 return error;
1799 static const struct got_error *
1800 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1802 const struct got_error *error = NULL;
1803 DIR *dt;
1804 char *dir_test;
1805 int opened = 0;
1807 if (asprintf(&dir_test, "%s/%s/%s",
1808 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1809 GOTWEB_GIT_DIR) == -1)
1810 return got_error_from_errno("asprintf");
1812 dt = opendir(dir_test);
1813 if (dt == NULL) {
1814 free(dir_test);
1815 } else {
1816 gw_dir->path = strdup(dir_test);
1817 if (gw_dir->path == NULL) {
1818 opened = 1;
1819 error = got_error_from_errno("strdup");
1820 goto errored;
1822 opened = 1;
1823 goto done;
1826 if (asprintf(&dir_test, "%s/%s/%s",
1827 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1828 GOTWEB_GOT_DIR) == -1) {
1829 dir_test = NULL;
1830 error = got_error_from_errno("asprintf");
1831 goto errored;
1834 dt = opendir(dir_test);
1835 if (dt == NULL)
1836 free(dir_test);
1837 else {
1838 opened = 1;
1839 error = got_error(GOT_ERR_NOT_GIT_REPO);
1840 goto errored;
1843 if (asprintf(&dir_test, "%s/%s",
1844 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1) {
1845 error = got_error_from_errno("asprintf");
1846 dir_test = NULL;
1847 goto errored;
1850 gw_dir->path = strdup(dir_test);
1851 if (gw_dir->path == NULL) {
1852 opened = 1;
1853 error = got_error_from_errno("strdup");
1854 goto errored;
1857 dt = opendir(dir_test);
1858 if (dt == NULL) {
1859 error = got_error_path(gw_dir->name, GOT_ERR_NOT_GIT_REPO);
1860 goto errored;
1861 } else
1862 opened = 1;
1863 done:
1864 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1865 gw_dir->path);
1866 if (error)
1867 goto errored;
1868 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1869 if (error)
1870 goto errored;
1871 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1872 NULL, TM_DIFF);
1873 if (error)
1874 goto errored;
1875 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1876 errored:
1877 free(dir_test);
1878 if (opened)
1879 if (dt && closedir(dt) == -1 && error == NULL)
1880 error = got_error_from_errno("closedir");
1881 return error;
1884 static const struct got_error *
1885 gw_load_got_paths(struct gw_trans *gw_trans)
1887 const struct got_error *error = NULL;
1888 DIR *d;
1889 struct dirent **sd_dent;
1890 struct gw_dir *gw_dir;
1891 struct stat st;
1892 unsigned int d_cnt, d_i;
1894 d = opendir(gw_trans->gw_conf->got_repos_path);
1895 if (d == NULL) {
1896 error = got_error_from_errno2("opendir",
1897 gw_trans->gw_conf->got_repos_path);
1898 return error;
1901 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1902 alphasort);
1903 if (d_cnt == -1) {
1904 error = got_error_from_errno2("scandir",
1905 gw_trans->gw_conf->got_repos_path);
1906 goto done;
1909 for (d_i = 0; d_i < d_cnt; d_i++) {
1910 if (gw_trans->gw_conf->got_max_repos > 0 &&
1911 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1912 break; /* account for parent and self */
1914 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1915 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1916 continue;
1918 error = gw_init_gw_dir(&gw_dir, sd_dent[d_i]->d_name);
1919 if (error)
1920 goto done;
1922 error = gw_load_got_path(gw_trans, gw_dir);
1923 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1924 error = NULL;
1925 continue;
1927 else if (error)
1928 goto done;
1930 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1931 !got_path_dir_is_empty(gw_dir->path)) {
1932 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1933 entry);
1934 gw_trans->repos_total++;
1937 done:
1938 if (d && closedir(d) == -1 && error == NULL)
1939 error = got_error_from_errno("closedir");
1940 return error;
1943 static const struct got_error *
1944 gw_parse_querystring(struct gw_trans *gw_trans)
1946 const struct got_error *error = NULL;
1947 struct kpair *p;
1948 struct gw_query_action *action = NULL;
1949 unsigned int i;
1951 if (gw_trans->gw_req->fieldnmap[0]) {
1952 return got_error(GOT_ERR_QUERYSTRING);
1953 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1954 /* define gw_trans->repo_path */
1955 gw_trans->repo_name = p->parsed.s;
1957 if (asprintf(&gw_trans->repo_path, "%s/%s",
1958 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
1959 return got_error_from_errno("asprintf");
1961 /* get action and set function */
1962 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION])) {
1963 for (i = 0; i < nitems(gw_query_funcs); i++) {
1964 action = &gw_query_funcs[i];
1965 if (action->func_name == NULL)
1966 continue;
1967 if (strcmp(action->func_name,
1968 p->parsed.s) == 0) {
1969 gw_trans->action = i;
1970 break;
1974 if (gw_trans->action == -1) {
1975 gw_trans->action = GW_ERR;
1976 gw_trans->error = got_error_msg(GOT_ERR_QUERYSTRING,
1977 p != NULL ? "bad action in querystring" :
1978 "no action in querystring");
1979 return error;
1982 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID])) {
1983 if (asprintf(&gw_trans->commit_id, "%s",
1984 p->parsed.s) == -1)
1985 return got_error_from_errno("asprintf");
1988 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1989 gw_trans->repo_file = p->parsed.s;
1991 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER])) {
1992 if (asprintf(&gw_trans->repo_folder, "%s",
1993 p->parsed.s) == -1)
1994 return got_error_from_errno("asprintf");
1997 if ((p = gw_trans->gw_req->fieldmap[KEY_PREV_ID])) {
1998 if (asprintf(&gw_trans->prev_id, "%s",
1999 p->parsed.s) == -1)
2000 return got_error_from_errno("asprintf");
2003 if ((p = gw_trans->gw_req->fieldmap[KEY_PREV_PREV_ID])) {
2004 if (asprintf(&gw_trans->prev_prev_id, "%s",
2005 p->parsed.s) == -1)
2006 return got_error_from_errno("asprintf");
2009 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
2010 gw_trans->headref = p->parsed.s;
2012 error = gw_init_gw_dir(&gw_trans->gw_dir, gw_trans->repo_name);
2013 if (error)
2014 return error;
2016 gw_trans->error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
2017 } else
2018 gw_trans->action = GW_INDEX;
2020 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
2021 gw_trans->page = p->parsed.i;
2023 return error;
2026 static const struct got_error *
2027 gw_init_gw_dir(struct gw_dir **gw_dir, const char *dir)
2029 const struct got_error *error;
2031 *gw_dir = malloc(sizeof(**gw_dir));
2032 if (*gw_dir == NULL)
2033 return got_error_from_errno("malloc");
2035 if (asprintf(&(*gw_dir)->name, "%s", dir) == -1) {
2036 error = got_error_from_errno("asprintf");
2037 free(*gw_dir);
2038 *gw_dir = NULL;
2039 return NULL;
2042 return NULL;
2045 static const struct got_error *
2046 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
2048 enum kcgi_err kerr = KCGI_OK;
2050 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
2051 if (kerr != KCGI_OK)
2052 return gw_kcgi_error(kerr);
2053 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
2054 khttps[code]);
2055 if (kerr != KCGI_OK)
2056 return gw_kcgi_error(kerr);
2057 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
2058 kmimetypes[mime]);
2059 if (kerr != KCGI_OK)
2060 return gw_kcgi_error(kerr);
2061 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
2062 "nosniff");
2063 if (kerr != KCGI_OK)
2064 return gw_kcgi_error(kerr);
2065 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
2066 if (kerr != KCGI_OK)
2067 return gw_kcgi_error(kerr);
2068 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
2069 "1; mode=block");
2070 if (kerr != KCGI_OK)
2071 return gw_kcgi_error(kerr);
2073 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
2074 kerr = khttp_head(gw_trans->gw_req,
2075 kresps[KRESP_CONTENT_DISPOSITION],
2076 "attachment; filename=%s", gw_trans->repo_file);
2077 if (kerr != KCGI_OK)
2078 return gw_kcgi_error(kerr);
2081 kerr = khttp_body(gw_trans->gw_req);
2082 return gw_kcgi_error(kerr);
2085 static const struct got_error *
2086 gw_display_index(struct gw_trans *gw_trans)
2088 const struct got_error *error;
2089 enum kcgi_err kerr = KCGI_OK;
2091 /* catch early querystring errors */
2092 if (gw_trans->error)
2093 gw_trans->action = GW_ERR;
2095 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
2096 if (error)
2097 return error;
2099 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
2100 if (kerr != KCGI_OK)
2101 return gw_kcgi_error(kerr);
2103 if (gw_trans->action != GW_BLOB) {
2104 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
2105 gw_query_funcs[gw_trans->action].template);
2106 if (kerr != KCGI_OK) {
2107 khtml_close(gw_trans->gw_html_req);
2108 return gw_kcgi_error(kerr);
2112 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
2115 static const struct got_error *
2116 gw_error(struct gw_trans *gw_trans)
2118 enum kcgi_err kerr = KCGI_OK;
2120 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->error->msg);
2122 return gw_kcgi_error(kerr);
2125 static int
2126 gw_template(size_t key, void *arg)
2128 const struct got_error *error = NULL;
2129 enum kcgi_err kerr = KCGI_OK;
2130 struct gw_trans *gw_trans = arg;
2131 char *img_src = NULL;
2133 switch (key) {
2134 case (TEMPL_HEAD):
2135 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2136 KATTR_NAME, "viewport",
2137 KATTR_CONTENT, "initial-scale=.75, user-scalable=yes",
2138 KATTR__MAX);
2139 if (kerr != KCGI_OK)
2140 return 0;
2141 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2142 if (kerr != KCGI_OK)
2143 return 0;
2144 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2145 KATTR_CHARSET, "utf-8",
2146 KATTR__MAX);
2147 if (kerr != KCGI_OK)
2148 return 0;
2149 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2150 if (kerr != KCGI_OK)
2151 return 0;
2152 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2153 KATTR_NAME, "msapplication-TileColor",
2154 KATTR_CONTENT, "#da532c", KATTR__MAX);
2155 if (kerr != KCGI_OK)
2156 return 0;
2157 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2158 if (kerr != KCGI_OK)
2159 return 0;
2160 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2161 KATTR_NAME, "theme-color",
2162 KATTR_CONTENT, "#ffffff", KATTR__MAX);
2163 if (kerr != KCGI_OK)
2164 return 0;
2165 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2166 if (kerr != KCGI_OK)
2167 return 0;
2168 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2169 KATTR_REL, "apple-touch-icon", KATTR_SIZES, "180x180",
2170 KATTR_HREF, "/apple-touch-icon.png", KATTR__MAX);
2171 if (kerr != KCGI_OK)
2172 return 0;
2173 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2174 if (kerr != KCGI_OK)
2175 return 0;
2176 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2177 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2178 "32x32", KATTR_HREF, "/favicon-32x32.png", KATTR__MAX);
2179 if (kerr != KCGI_OK)
2180 return 0;
2181 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2182 if (kerr != KCGI_OK)
2183 return 0;
2184 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2185 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2186 "16x16", KATTR_HREF, "/favicon-16x16.png", KATTR__MAX);
2187 if (kerr != KCGI_OK)
2188 return 0;
2189 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2190 if (kerr != KCGI_OK)
2191 return 0;
2192 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2193 KATTR_REL, "manifest", KATTR_HREF, "/site.webmanifest",
2194 KATTR__MAX);
2195 if (kerr != KCGI_OK)
2196 return 0;
2197 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2198 if (kerr != KCGI_OK)
2199 return 0;
2200 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2201 KATTR_REL, "mask-icon", KATTR_HREF,
2202 "/safari-pinned-tab.svg", KATTR__MAX);
2203 if (kerr != KCGI_OK)
2204 return 0;
2205 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2206 if (kerr != KCGI_OK)
2207 return 0;
2208 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2209 KATTR_REL, "stylesheet", KATTR_TYPE, "text/css",
2210 KATTR_HREF, "/gotweb.css", KATTR__MAX);
2211 if (kerr != KCGI_OK)
2212 return 0;
2213 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2214 if (kerr != KCGI_OK)
2215 return 0;
2216 break;
2217 case(TEMPL_HEADER):
2218 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2219 KATTR_ID, "got_link", KATTR__MAX);
2220 if (kerr != KCGI_OK)
2221 return 0;
2222 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2223 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
2224 KATTR_TARGET, "_sotd", KATTR__MAX);
2225 if (kerr != KCGI_OK)
2226 return 0;
2227 if (asprintf(&img_src, "/%s",
2228 gw_trans->gw_conf->got_logo) == -1)
2229 return 0;
2230 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
2231 KATTR_SRC, img_src, KATTR__MAX);
2232 if (kerr != KCGI_OK) {
2233 free(img_src);
2234 return 0;
2236 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2237 if (kerr != KCGI_OK) {
2238 free(img_src);
2239 return 0;
2241 break;
2242 case (TEMPL_SITEPATH):
2243 error = gw_output_site_link(gw_trans);
2244 if (error)
2245 return 0;
2246 break;
2247 case(TEMPL_TITLE):
2248 if (gw_trans->gw_conf->got_site_name != NULL) {
2249 kerr = khtml_puts(gw_trans->gw_html_req,
2250 gw_trans->gw_conf->got_site_name);
2251 if (kerr != KCGI_OK)
2252 return 0;
2254 break;
2255 case (TEMPL_SEARCH):
2256 break;
2257 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
2258 "search", KATTR__MAX);
2259 if (kerr != KCGI_OK)
2260 return 0;
2261 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
2262 KATTR_METHOD, "POST", KATTR__MAX);
2263 if (kerr != KCGI_OK)
2264 return 0;
2265 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
2266 "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
2267 KATTR_MAXLENGTH, "50", KATTR__MAX);
2268 if (kerr != KCGI_OK)
2269 return 0;
2270 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
2271 KATTR__MAX);
2272 if (kerr != KCGI_OK)
2273 return 0;
2274 kerr = khtml_puts(gw_trans->gw_html_req, "Search");
2275 if (kerr != KCGI_OK)
2276 return 0;
2277 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
2278 if (kerr != KCGI_OK)
2279 return 0;
2280 break;
2281 case(TEMPL_SITEOWNER):
2282 if (gw_trans->gw_conf->got_site_owner != NULL &&
2283 gw_trans->gw_conf->got_show_site_owner) {
2284 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2285 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
2286 if (kerr != KCGI_OK)
2287 return 0;
2288 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2289 KATTR_ID, "site_owner", KATTR__MAX);
2290 if (kerr != KCGI_OK)
2291 return 0;
2292 kerr = khtml_puts(gw_trans->gw_html_req,
2293 gw_trans->gw_conf->got_site_owner);
2294 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2295 if (kerr != KCGI_OK)
2296 return 0;
2298 break;
2299 case(TEMPL_CONTENT):
2300 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
2301 if (error) {
2302 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2303 KATTR_ID, "tmpl_err", KATTR__MAX);
2304 if (kerr != KCGI_OK)
2305 return 0;
2306 kerr = khttp_printf(gw_trans->gw_req, "Error: %s",
2307 error->msg);
2308 if (kerr != KCGI_OK)
2309 return 0;
2310 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2311 if (kerr != KCGI_OK)
2312 return 0;
2314 break;
2315 default:
2316 return 0;
2318 return 1;
2321 static const struct got_error *
2322 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
2324 const struct got_error *error = NULL;
2325 enum kcgi_err kerr = KCGI_OK;
2327 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2328 KATTR_ID, "header_commit_title", KATTR__MAX);
2329 if (kerr != KCGI_OK)
2330 goto done;
2331 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
2332 if (kerr != KCGI_OK)
2333 goto done;
2334 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2335 if (kerr != KCGI_OK)
2336 goto done;
2337 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2338 KATTR_ID, "header_commit", KATTR__MAX);
2339 if (kerr != KCGI_OK)
2340 goto done;
2341 kerr = khtml_printf(gw_trans->gw_html_req, "%s ", str1);
2342 if (kerr != KCGI_OK)
2343 goto done;
2344 if (str2 != NULL) {
2345 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
2346 KATTR_ID, "refs_str", KATTR__MAX);
2347 if (kerr != KCGI_OK)
2348 goto done;
2349 kerr = khtml_printf(gw_trans->gw_html_req, "(%s)", str2);
2350 if (kerr != KCGI_OK)
2351 goto done;
2352 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2353 if (kerr != KCGI_OK)
2354 goto done;
2356 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2357 done:
2358 if (error == NULL && kerr != KCGI_OK)
2359 error = gw_kcgi_error(kerr);
2360 return error;
2363 static const struct got_error *
2364 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2366 const struct got_error *error = NULL;
2367 enum kcgi_err kerr = KCGI_OK;
2369 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2370 KATTR_ID, "header_diff_title", KATTR__MAX);
2371 if (kerr != KCGI_OK)
2372 goto done;
2373 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2374 if (kerr != KCGI_OK)
2375 goto done;
2376 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2377 if (kerr != KCGI_OK)
2378 goto done;
2379 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2380 KATTR_ID, "header_diff", KATTR__MAX);
2381 if (kerr != KCGI_OK)
2382 goto done;
2383 if (str1 != NULL) {
2384 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2385 if (kerr != KCGI_OK)
2386 goto done;
2388 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2389 if (kerr != KCGI_OK)
2390 goto done;
2391 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2392 if (kerr != KCGI_OK)
2393 goto done;
2394 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2395 done:
2396 if (error == NULL && kerr != KCGI_OK)
2397 error = gw_kcgi_error(kerr);
2398 return error;
2401 static const struct got_error *
2402 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2404 const struct got_error *error = NULL;
2405 enum kcgi_err kerr = KCGI_OK;
2407 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2408 KATTR_ID, "header_age_title", KATTR__MAX);
2409 if (kerr != KCGI_OK)
2410 goto done;
2411 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2412 if (kerr != KCGI_OK)
2413 goto done;
2414 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2415 if (kerr != KCGI_OK)
2416 goto done;
2417 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2418 KATTR_ID, "header_age", KATTR__MAX);
2419 if (kerr != KCGI_OK)
2420 goto done;
2421 kerr = khtml_puts(gw_trans->gw_html_req, str);
2422 if (kerr != KCGI_OK)
2423 goto done;
2424 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2425 done:
2426 if (error == NULL && kerr != KCGI_OK)
2427 error = gw_kcgi_error(kerr);
2428 return error;
2431 static const struct got_error *
2432 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2434 const struct got_error *error = NULL;
2435 enum kcgi_err kerr = KCGI_OK;
2437 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2438 KATTR_ID, "header_author_title", KATTR__MAX);
2439 if (kerr != KCGI_OK)
2440 goto done;
2441 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2442 if (kerr != KCGI_OK)
2443 goto done;
2444 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2445 if (kerr != KCGI_OK)
2446 goto done;
2447 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2448 KATTR_ID, "header_author", KATTR__MAX);
2449 if (kerr != KCGI_OK)
2450 goto done;
2451 kerr = khtml_puts(gw_trans->gw_html_req, str);
2452 if (kerr != KCGI_OK)
2453 goto done;
2454 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2455 done:
2456 if (error == NULL && kerr != KCGI_OK)
2457 error = gw_kcgi_error(kerr);
2458 return error;
2461 static const struct got_error *
2462 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2464 const struct got_error *error = NULL;
2465 enum kcgi_err kerr = KCGI_OK;
2467 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2468 KATTR_ID, "header_committer_title", KATTR__MAX);
2469 if (kerr != KCGI_OK)
2470 goto done;
2471 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2472 if (kerr != KCGI_OK)
2473 goto done;
2474 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2475 if (kerr != KCGI_OK)
2476 goto done;
2477 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2478 KATTR_ID, "header_committer", KATTR__MAX);
2479 if (kerr != KCGI_OK)
2480 goto done;
2481 kerr = khtml_puts(gw_trans->gw_html_req, str);
2482 if (kerr != KCGI_OK)
2483 goto done;
2484 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2485 done:
2486 if (error == NULL && kerr != KCGI_OK)
2487 error = gw_kcgi_error(kerr);
2488 return error;
2491 static const struct got_error *
2492 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2494 const struct got_error *error = NULL;
2495 enum kcgi_err kerr = KCGI_OK;
2497 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2498 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2499 if (kerr != KCGI_OK)
2500 goto done;
2501 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2502 if (kerr != KCGI_OK)
2503 goto done;
2504 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2505 if (kerr != KCGI_OK)
2506 goto done;
2507 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2508 KATTR_ID, "header_commit_msg", KATTR__MAX);
2509 if (kerr != KCGI_OK)
2510 goto done;
2511 kerr = khttp_puts(gw_trans->gw_req, str);
2512 if (kerr != KCGI_OK)
2513 goto done;
2514 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2515 done:
2516 if (error == NULL && kerr != KCGI_OK)
2517 error = gw_kcgi_error(kerr);
2518 return error;
2521 static const struct got_error *
2522 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2524 const struct got_error *error = NULL;
2525 enum kcgi_err kerr = KCGI_OK;
2527 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2528 KATTR_ID, "header_tree_title", KATTR__MAX);
2529 if (kerr != KCGI_OK)
2530 goto done;
2531 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2532 if (kerr != KCGI_OK)
2533 goto done;
2534 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2535 if (kerr != KCGI_OK)
2536 goto done;
2537 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2538 KATTR_ID, "header_tree", KATTR__MAX);
2539 if (kerr != KCGI_OK)
2540 goto done;
2541 kerr = khtml_puts(gw_trans->gw_html_req, str);
2542 if (kerr != KCGI_OK)
2543 goto done;
2544 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2545 done:
2546 if (error == NULL && kerr != KCGI_OK)
2547 error = gw_kcgi_error(kerr);
2548 return error;
2551 static const struct got_error *
2552 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2553 char *dir)
2555 const struct got_error *error = NULL;
2556 FILE *f = NULL;
2557 char *d_file = NULL;
2558 unsigned int len;
2559 size_t n;
2561 *description = NULL;
2562 if (gw_trans->gw_conf->got_show_repo_description == 0)
2563 return NULL;
2565 if (asprintf(&d_file, "%s/description", dir) == -1)
2566 return got_error_from_errno("asprintf");
2568 f = fopen(d_file, "r");
2569 if (f == NULL) {
2570 if (errno == ENOENT || errno == EACCES)
2571 return NULL;
2572 error = got_error_from_errno2("fopen", d_file);
2573 goto done;
2576 if (fseek(f, 0, SEEK_END) == -1) {
2577 error = got_ferror(f, GOT_ERR_IO);
2578 goto done;
2580 len = ftell(f);
2581 if (len == -1) {
2582 error = got_ferror(f, GOT_ERR_IO);
2583 goto done;
2585 if (fseek(f, 0, SEEK_SET) == -1) {
2586 error = got_ferror(f, GOT_ERR_IO);
2587 goto done;
2589 *description = calloc(len + 1, sizeof(**description));
2590 if (*description == NULL) {
2591 error = got_error_from_errno("calloc");
2592 goto done;
2595 n = fread(*description, 1, len, f);
2596 if (n == 0 && ferror(f))
2597 error = got_ferror(f, GOT_ERR_IO);
2598 done:
2599 if (f != NULL && fclose(f) == -1 && error == NULL)
2600 error = got_error_from_errno("fclose");
2601 free(d_file);
2602 return error;
2605 static const struct got_error *
2606 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2608 struct tm tm;
2609 time_t diff_time;
2610 char *years = "years ago", *months = "months ago";
2611 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2612 char *minutes = "minutes ago", *seconds = "seconds ago";
2613 char *now = "right now";
2614 char *s;
2615 char datebuf[29];
2617 *repo_age = NULL;
2619 switch (ref_tm) {
2620 case TM_DIFF:
2621 diff_time = time(NULL) - committer_time;
2622 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2623 if (asprintf(repo_age, "%lld %s",
2624 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2625 return got_error_from_errno("asprintf");
2626 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2627 if (asprintf(repo_age, "%lld %s",
2628 (diff_time / 60 / 60 / 24 / (365 / 12)),
2629 months) == -1)
2630 return got_error_from_errno("asprintf");
2631 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2632 if (asprintf(repo_age, "%lld %s",
2633 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2634 return got_error_from_errno("asprintf");
2635 } else if (diff_time > 60 * 60 * 24 * 2) {
2636 if (asprintf(repo_age, "%lld %s",
2637 (diff_time / 60 / 60 / 24), days) == -1)
2638 return got_error_from_errno("asprintf");
2639 } else if (diff_time > 60 * 60 * 2) {
2640 if (asprintf(repo_age, "%lld %s",
2641 (diff_time / 60 / 60), hours) == -1)
2642 return got_error_from_errno("asprintf");
2643 } else if (diff_time > 60 * 2) {
2644 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2645 minutes) == -1)
2646 return got_error_from_errno("asprintf");
2647 } else if (diff_time > 2) {
2648 if (asprintf(repo_age, "%lld %s", diff_time,
2649 seconds) == -1)
2650 return got_error_from_errno("asprintf");
2651 } else {
2652 if (asprintf(repo_age, "%s", now) == -1)
2653 return got_error_from_errno("asprintf");
2655 break;
2656 case TM_LONG:
2657 if (gmtime_r(&committer_time, &tm) == NULL)
2658 return got_error_from_errno("gmtime_r");
2660 s = asctime_r(&tm, datebuf);
2661 if (s == NULL)
2662 return got_error_from_errno("asctime_r");
2664 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2665 return got_error_from_errno("asprintf");
2666 break;
2668 return NULL;
2671 static const struct got_error *
2672 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2673 const char *refname, int ref_tm)
2675 const struct got_error *error = NULL;
2676 struct got_repository *repo = NULL;
2677 struct got_commit_object *commit = NULL;
2678 struct got_reflist_head refs;
2679 struct got_reflist_entry *re;
2680 time_t committer_time = 0, cmp_time = 0;
2682 *repo_age = NULL;
2683 SIMPLEQ_INIT(&refs);
2685 if (gw_trans->gw_conf->got_show_repo_age == 0)
2686 return NULL;
2688 if (gw_trans->repo)
2689 repo = gw_trans->repo;
2690 else {
2691 error = got_repo_open(&repo, dir, NULL);
2692 if (error)
2693 return error;
2696 error = got_ref_list(&refs, repo, "refs/heads",
2697 got_ref_cmp_by_name, NULL);
2698 if (error)
2699 goto done;
2702 * Find the youngest branch tip in the repository, or the age of
2703 * the a specific branch tip if a name was provided by the caller.
2705 SIMPLEQ_FOREACH(re, &refs, entry) {
2706 struct got_object_id *id = NULL;
2708 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
2709 continue;
2711 error = got_ref_resolve(&id, repo, re->ref);
2712 if (error)
2713 goto done;
2715 error = got_object_open_as_commit(&commit, repo, id);
2716 free(id);
2717 if (error)
2718 goto done;
2720 committer_time =
2721 got_object_commit_get_committer_time(commit);
2722 got_object_commit_close(commit);
2723 if (cmp_time < committer_time)
2724 cmp_time = committer_time;
2726 if (refname)
2727 break;
2730 if (cmp_time != 0) {
2731 committer_time = cmp_time;
2732 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2734 done:
2735 got_ref_list_free(&refs);
2736 if (gw_trans->repo == NULL)
2737 got_repo_close(repo);
2738 return error;
2741 static const struct got_error *
2742 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2744 const struct got_error *error;
2745 FILE *f = NULL;
2746 struct got_object_id *id1 = NULL, *id2 = NULL;
2747 char *label1 = NULL, *label2 = NULL, *line = NULL;
2748 int obj_type;
2749 size_t linesize = 0;
2750 ssize_t linelen;
2751 enum kcgi_err kerr = KCGI_OK;
2753 f = got_opentemp();
2754 if (f == NULL)
2755 return NULL;
2757 if (header->parent_id != NULL &&
2758 strncmp(header->parent_id, "/dev/null", 9) != 0) {
2759 error = got_repo_match_object_id(&id1, &label1,
2760 header->parent_id, GOT_OBJ_TYPE_ANY, 1, gw_trans->repo);
2761 if (error)
2762 goto done;
2765 error = got_repo_match_object_id(&id2, &label2,
2766 header->commit_id, GOT_OBJ_TYPE_ANY, 1, gw_trans->repo);
2767 if (error)
2768 goto done;
2770 error = got_object_get_type(&obj_type, gw_trans->repo, id2);
2771 if (error)
2772 goto done;
2773 switch (obj_type) {
2774 case GOT_OBJ_TYPE_BLOB:
2775 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
2776 gw_trans->repo, f);
2777 break;
2778 case GOT_OBJ_TYPE_TREE:
2779 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
2780 gw_trans->repo, f);
2781 break;
2782 case GOT_OBJ_TYPE_COMMIT:
2783 error = got_diff_objects_as_commits(id1, id2, 3, 0,
2784 gw_trans->repo, f);
2785 break;
2786 default:
2787 error = got_error(GOT_ERR_OBJ_TYPE);
2789 if (error)
2790 goto done;
2792 if (fseek(f, 0, SEEK_SET) == -1) {
2793 error = got_ferror(f, GOT_ERR_IO);
2794 goto done;
2797 while ((linelen = getline(&line, &linesize, f)) != -1) {
2798 error = gw_colordiff_line(gw_trans, line);
2799 if (error)
2800 goto done;
2801 /* XXX: KHTML_PRETTY breaks this */
2802 kerr = khtml_puts(gw_trans->gw_html_req, line);
2803 if (kerr != KCGI_OK)
2804 goto done;
2805 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2806 if (kerr != KCGI_OK)
2807 goto done;
2809 if (linelen == -1 && ferror(f))
2810 error = got_error_from_errno("getline");
2811 done:
2812 if (f && fclose(f) == -1 && error == NULL)
2813 error = got_error_from_errno("fclose");
2814 free(line);
2815 free(label1);
2816 free(label2);
2817 free(id1);
2818 free(id2);
2820 if (error == NULL && kerr != KCGI_OK)
2821 error = gw_kcgi_error(kerr);
2822 return error;
2825 static const struct got_error *
2826 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2828 const struct got_error *error = NULL;
2829 struct got_repository *repo;
2830 const char *gitconfig_owner;
2832 *owner = NULL;
2834 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2835 return NULL;
2837 error = got_repo_open(&repo, dir, NULL);
2838 if (error)
2839 return error;
2840 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2841 if (gitconfig_owner) {
2842 *owner = strdup(gitconfig_owner);
2843 if (*owner == NULL)
2844 error = got_error_from_errno("strdup");
2846 got_repo_close(repo);
2847 return error;
2850 static const struct got_error *
2851 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2853 const struct got_error *error = NULL;
2854 FILE *f;
2855 char *d_file = NULL;
2856 unsigned int len;
2857 size_t n;
2859 *url = NULL;
2861 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2862 return got_error_from_errno("asprintf");
2864 f = fopen(d_file, "r");
2865 if (f == NULL) {
2866 if (errno != ENOENT && errno != EACCES)
2867 error = got_error_from_errno2("fopen", d_file);
2868 goto done;
2871 if (fseek(f, 0, SEEK_END) == -1) {
2872 error = got_ferror(f, GOT_ERR_IO);
2873 goto done;
2875 len = ftell(f);
2876 if (len == -1) {
2877 error = got_ferror(f, GOT_ERR_IO);
2878 goto done;
2880 if (fseek(f, 0, SEEK_SET) == -1) {
2881 error = got_ferror(f, GOT_ERR_IO);
2882 goto done;
2885 *url = calloc(len + 1, sizeof(**url));
2886 if (*url == NULL) {
2887 error = got_error_from_errno("calloc");
2888 goto done;
2891 n = fread(*url, 1, len, f);
2892 if (n == 0 && ferror(f))
2893 error = got_ferror(f, GOT_ERR_IO);
2894 done:
2895 if (f && fclose(f) == -1 && error == NULL)
2896 error = got_error_from_errno("fclose");
2897 free(d_file);
2898 return NULL;
2901 static const struct got_error *
2902 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
2903 int limit, int tag_type)
2905 const struct got_error *error = NULL;
2906 struct got_reflist_head refs;
2907 struct got_reflist_entry *re;
2908 char *age = NULL;
2909 char *id_str = NULL, *newline, *href_commits = NULL;
2910 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
2911 struct got_tag_object *tag = NULL;
2912 enum kcgi_err kerr = KCGI_OK;
2913 int summary_header_displayed = 0, start_tag = 0, chk_next = 0;
2914 int prev_set = 0, tag_count = 0;
2916 SIMPLEQ_INIT(&refs);
2918 error = got_ref_list(&refs, gw_trans->repo, "refs/tags",
2919 got_ref_cmp_tags, gw_trans->repo);
2920 if (error)
2921 goto done;
2923 SIMPLEQ_FOREACH(re, &refs, entry) {
2924 const char *refname;
2925 const char *tagger;
2926 const char *tag_commit;
2927 time_t tagger_time;
2928 struct got_object_id *id;
2929 struct got_commit_object *commit = NULL;
2931 refname = got_ref_get_name(re->ref);
2932 if (strncmp(refname, "refs/tags/", 10) != 0)
2933 continue;
2934 refname += 10;
2936 error = got_ref_resolve(&id, gw_trans->repo, re->ref);
2937 if (error)
2938 goto done;
2940 error = got_object_open_as_tag(&tag, gw_trans->repo, id);
2941 if (error) {
2942 if (error->code != GOT_ERR_OBJ_TYPE) {
2943 free(id);
2944 goto done;
2946 /* "lightweight" tag */
2947 error = got_object_open_as_commit(&commit,
2948 gw_trans->repo, id);
2949 if (error) {
2950 free(id);
2951 goto done;
2953 tagger = got_object_commit_get_committer(commit);
2954 tagger_time =
2955 got_object_commit_get_committer_time(commit);
2956 error = got_object_id_str(&id_str, id);
2957 free(id);
2958 } else {
2959 free(id);
2960 tagger = got_object_tag_get_tagger(tag);
2961 tagger_time = got_object_tag_get_tagger_time(tag);
2962 error = got_object_id_str(&id_str,
2963 got_object_tag_get_object_id(tag));
2965 if (error)
2966 goto done;
2968 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
2969 strlen(id_str)) != 0)
2970 continue;
2972 if (tag_type == TAGBRIEF && gw_trans->commit_id &&
2973 start_tag == 0 && strncmp(id_str, header->commit_id,
2974 strlen(id_str)) != 0) {
2975 continue;
2976 } else {
2977 start_tag = 1;
2980 tag_count++;
2982 if (prev_set == 0 && start_tag == 1) {
2983 gw_trans->next_prev_id = strdup(id_str);
2984 if (gw_trans->next_prev_id == NULL) {
2985 error = got_error_from_errno("strdup");
2986 goto done;
2988 prev_set = 1;
2991 if (chk_next) {
2992 gw_trans->next_id = strdup(id_str);
2993 if (gw_trans->next_id == NULL)
2994 error = got_error_from_errno("strdup");
2995 goto done;
2998 if (commit) {
2999 error = got_object_commit_get_logmsg(&tag_commit0,
3000 commit);
3001 if (error)
3002 goto done;
3003 got_object_commit_close(commit);
3004 } else {
3005 tag_commit0 = strdup(got_object_tag_get_message(tag));
3006 if (tag_commit0 == NULL) {
3007 error = got_error_from_errno("strdup");
3008 goto done;
3012 tag_commit = tag_commit0;
3013 while (*tag_commit == '\n')
3014 tag_commit++;
3016 switch (tag_type) {
3017 case TAGBRIEF:
3018 newline = strchr(tag_commit, '\n');
3019 if (newline)
3020 *newline = '\0';
3022 if (summary_header_displayed == 0) {
3023 kerr = khtml_attr(gw_trans->gw_html_req,
3024 KELEM_DIV, KATTR_ID,
3025 "summary_tags_title_wrapper", KATTR__MAX);
3026 if (kerr != KCGI_OK)
3027 goto done;
3028 kerr = khtml_attr(gw_trans->gw_html_req,
3029 KELEM_DIV, KATTR_ID,
3030 "summary_tags_title", KATTR__MAX);
3031 if (kerr != KCGI_OK)
3032 goto done;
3033 kerr = khtml_puts(gw_trans->gw_html_req,
3034 "Tags");
3035 if (kerr != KCGI_OK)
3036 goto done;
3037 kerr = khtml_closeelem(gw_trans->gw_html_req,
3038 2);
3039 if (kerr != KCGI_OK)
3040 goto done;
3041 kerr = khtml_attr(gw_trans->gw_html_req,
3042 KELEM_DIV, KATTR_ID,
3043 "summary_tags_content", KATTR__MAX);
3044 if (kerr != KCGI_OK)
3045 goto done;
3046 summary_header_displayed = 1;
3049 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3050 KATTR_ID, "tag_wrapper", KATTR__MAX);
3051 if (kerr != KCGI_OK)
3052 goto done;
3053 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3054 KATTR_ID, "tag_age", KATTR__MAX);
3055 if (kerr != KCGI_OK)
3056 goto done;
3057 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
3058 if (error)
3059 goto done;
3060 kerr = khtml_puts(gw_trans->gw_html_req,
3061 age ? age : "");
3062 if (kerr != KCGI_OK)
3063 goto done;
3064 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3065 if (kerr != KCGI_OK)
3066 goto done;
3067 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3068 KATTR_ID, "tag", KATTR__MAX);
3069 if (kerr != KCGI_OK)
3070 goto done;
3071 kerr = khtml_puts(gw_trans->gw_html_req, refname);
3072 if (kerr != KCGI_OK)
3073 goto done;
3074 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3075 if (kerr != KCGI_OK)
3076 goto done;
3077 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3078 KATTR_ID, "tag_name", KATTR__MAX);
3079 if (kerr != KCGI_OK)
3080 goto done;
3081 if (asprintf(&href_tag, "?path=%s&action=tag&commit=%s",
3082 gw_trans->repo_name, id_str) == -1) {
3083 error = got_error_from_errno("asprintf");
3084 goto done;
3086 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3087 KATTR_HREF, href_tag, KATTR__MAX);
3088 if (kerr != KCGI_OK)
3089 goto done;
3090 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
3091 if (kerr != KCGI_OK)
3092 goto done;
3093 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3094 if (kerr != KCGI_OK)
3095 goto done;
3097 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3098 KATTR_ID, "navs_wrapper", KATTR__MAX);
3099 if (kerr != KCGI_OK)
3100 goto done;
3101 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3102 KATTR_ID, "navs", KATTR__MAX);
3103 if (kerr != KCGI_OK)
3104 goto done;
3106 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3107 KATTR_HREF, href_tag, KATTR__MAX);
3108 if (kerr != KCGI_OK)
3109 goto done;
3110 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
3111 if (kerr != KCGI_OK)
3112 goto done;
3113 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3114 if (kerr != KCGI_OK)
3115 goto done;
3117 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3118 if (kerr != KCGI_OK)
3119 goto done;
3120 if (asprintf(&href_briefs,
3121 "?path=%s&action=briefs&commit=%s",
3122 gw_trans->repo_name, id_str) == -1) {
3123 error = got_error_from_errno("asprintf");
3124 goto done;
3126 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3127 KATTR_HREF, href_briefs, KATTR__MAX);
3128 if (kerr != KCGI_OK)
3129 goto done;
3130 kerr = khtml_puts(gw_trans->gw_html_req,
3131 "commit briefs");
3132 if (kerr != KCGI_OK)
3133 goto done;
3134 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3135 if (kerr != KCGI_OK)
3136 goto done;
3138 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3139 if (kerr != KCGI_OK)
3140 goto done;
3142 if (asprintf(&href_commits,
3143 "?path=%s&action=commits&commit=%s",
3144 gw_trans->repo_name, id_str) == -1) {
3145 error = got_error_from_errno("asprintf");
3146 goto done;
3148 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3149 KATTR_HREF, href_commits, KATTR__MAX);
3150 if (kerr != KCGI_OK)
3151 goto done;
3152 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
3153 if (kerr != KCGI_OK)
3154 goto done;
3155 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3156 if (kerr != KCGI_OK)
3157 goto done;
3159 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3160 KATTR_ID, "dotted_line", KATTR__MAX);
3161 if (kerr != KCGI_OK)
3162 goto done;
3163 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3164 if (kerr != KCGI_OK)
3165 goto done;
3166 break;
3167 case TAGFULL:
3168 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3169 KATTR_ID, "tag_info_date_title", KATTR__MAX);
3170 if (kerr != KCGI_OK)
3171 goto done;
3172 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
3173 if (kerr != KCGI_OK)
3174 goto done;
3175 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3176 if (kerr != KCGI_OK)
3177 goto done;
3178 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3179 KATTR_ID, "tag_info_date", KATTR__MAX);
3180 if (kerr != KCGI_OK)
3181 goto done;
3182 error = gw_get_time_str(&age, tagger_time, TM_LONG);
3183 if (error)
3184 goto done;
3185 kerr = khtml_puts(gw_trans->gw_html_req,
3186 age ? age : "");
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;
3193 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3194 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
3195 if (kerr != KCGI_OK)
3196 goto done;
3197 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
3198 if (kerr != KCGI_OK)
3199 goto done;
3200 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3201 if (kerr != KCGI_OK)
3202 goto done;
3203 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3204 KATTR_ID, "tag_info_date", KATTR__MAX);
3205 if (kerr != KCGI_OK)
3206 goto done;
3207 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
3208 if (kerr != KCGI_OK)
3209 goto done;
3210 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3211 if (kerr != KCGI_OK)
3212 goto done;
3214 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3215 KATTR_ID, "tag_info", KATTR__MAX);
3216 if (kerr != KCGI_OK)
3217 goto done;
3218 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
3219 if (kerr != KCGI_OK)
3220 goto done;
3221 break;
3222 default:
3223 break;
3225 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3226 if (kerr != KCGI_OK)
3227 goto done;
3229 if (limit && --limit == 0)
3230 chk_next = 1;
3232 if (tag)
3233 got_object_tag_close(tag);
3234 tag = NULL;
3235 free(id_str);
3236 id_str = NULL;
3237 free(age);
3238 age = NULL;
3239 free(tag_commit0);
3240 tag_commit0 = NULL;
3241 free(href_tag);
3242 href_tag = NULL;
3243 free(href_briefs);
3244 href_briefs = NULL;
3245 free(href_commits);
3246 href_commits = NULL;
3248 if (tag_count == 0) {
3249 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3250 "summary_tags_title_wrapper", KATTR__MAX);
3251 if (kerr != KCGI_OK)
3252 goto done;
3253 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3254 "summary_tags_title", KATTR__MAX);
3255 if (kerr != KCGI_OK)
3256 goto done;
3257 kerr = khtml_puts(gw_trans->gw_html_req, "Tags");
3258 if (kerr != KCGI_OK)
3259 goto done;
3260 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3261 if (kerr != KCGI_OK)
3262 goto done;
3263 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3264 "summary_tags_content", KATTR__MAX);
3265 if (kerr != KCGI_OK)
3266 goto done;
3267 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3268 "tags_info", KATTR__MAX);
3269 if (kerr != KCGI_OK)
3270 goto done;
3271 kerr = khttp_puts(gw_trans->gw_req,
3272 "There are no tags for this repo.");
3273 if (kerr != KCGI_OK)
3274 goto done;
3275 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3277 done:
3278 if (tag)
3279 got_object_tag_close(tag);
3280 free(id_str);
3281 free(age);
3282 free(tag_commit0);
3283 free(href_tag);
3284 free(href_briefs);
3285 free(href_commits);
3286 got_ref_list_free(&refs);
3287 if (error == NULL && kerr != KCGI_OK)
3288 error = gw_kcgi_error(kerr);
3289 return error;
3292 static void
3293 gw_free_header(struct gw_header *header)
3295 free(header->path);
3296 free(header->author);
3297 free(header->committer);
3298 free(header->refs_str);
3299 free(header->commit_id);
3300 free(header->parent_id);
3301 free(header->tree_id);
3302 free(header->commit_msg);
3305 static struct gw_header *
3306 gw_init_header()
3308 struct gw_header *header;
3310 header = malloc(sizeof(*header));
3311 if (header == NULL)
3312 return NULL;
3314 header->path = NULL;
3315 SIMPLEQ_INIT(&header->refs);
3317 header->refs_str = NULL;
3318 header->commit_id = NULL;
3319 header->committer = NULL;
3320 header->author = NULL;
3321 header->parent_id = NULL;
3322 header->tree_id = NULL;
3323 header->commit_msg = NULL;
3325 return header;
3328 static const struct got_error *
3329 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
3330 int limit, struct got_object_id *id)
3332 const struct got_error *error = NULL;
3333 struct got_commit_graph *graph = NULL;
3334 struct got_commit_object *commit = NULL;
3335 int chk_next = 0, chk_multi = 0, prev_set = 0;
3337 error = got_commit_graph_open(&graph, header->path, 0);
3338 if (error)
3339 return error;
3341 error = got_commit_graph_iter_start(graph, id, gw_trans->repo, NULL,
3342 NULL);
3343 if (error)
3344 goto done;
3346 for (;;) {
3347 error = got_commit_graph_iter_next(&id, graph, gw_trans->repo,
3348 NULL, NULL);
3349 if (error) {
3350 if (error->code == GOT_ERR_ITER_COMPLETED)
3351 error = NULL;
3352 goto done;
3354 if (id == NULL)
3355 goto done;
3357 error = got_object_open_as_commit(&commit, gw_trans->repo, id);
3358 if (error)
3359 goto done;
3360 if (limit == 1 && chk_multi == 0 &&
3361 gw_trans->gw_conf->got_max_commits_display != 1) {
3362 error = gw_get_commit(gw_trans, header, commit, id);
3363 if (error)
3364 goto done;
3365 } else {
3366 chk_multi = 1;
3367 struct gw_header *n_header = NULL;
3368 if ((n_header = gw_init_header()) == NULL) {
3369 error = got_error_from_errno("malloc");
3370 goto done;
3372 error = got_ref_list(&n_header->refs, gw_trans->repo,
3373 NULL, got_ref_cmp_by_name, NULL);
3374 if (error)
3375 goto done;
3377 error = gw_get_commit(gw_trans, n_header, commit, id);
3378 if (error)
3379 goto done;
3380 got_ref_list_free(&n_header->refs);
3383 * we have a commit_id now, so copy it to next_prev_id
3384 * for navigation through gw_briefs and gw_commits
3386 if (gw_trans->next_prev_id == NULL && prev_set == 0 &&
3387 (gw_trans->action == GW_BRIEFS ||
3388 gw_trans->action == GW_COMMITS ||
3389 gw_trans->action == GW_SUMMARY)) {
3390 prev_set = 1;
3391 gw_trans->next_prev_id =
3392 strdup(n_header->commit_id);
3393 if (gw_trans->next_prev_id == NULL) {
3394 error = got_error_from_errno("strdup");
3395 goto done;
3400 * check for one more commit before breaking,
3401 * so we know whether to navicate through gw_briefs
3402 * gw_commits and gw_summary
3404 if (chk_next && (gw_trans->action == GW_BRIEFS||
3405 gw_trans->action == GW_COMMITS ||
3406 gw_trans->action == GW_SUMMARY)) {
3407 gw_trans->next_id = strdup(n_header->commit_id);
3408 if (gw_trans->next_id == NULL)
3409 error = got_error_from_errno("strdup");
3410 goto done;
3413 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3414 entry);
3416 if (error || (limit && --limit == 0)) {
3417 if (chk_multi == 0)
3418 break;
3419 chk_next = 1;
3422 done:
3423 if (commit != NULL)
3424 got_object_commit_close(commit);
3425 if (graph)
3426 got_commit_graph_close(graph);
3427 return error;
3430 static const struct got_error *
3431 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header,
3432 struct got_commit_object *commit, struct got_object_id *id)
3434 const struct got_error *error = NULL;
3435 struct got_reflist_entry *re;
3436 struct got_object_id *id2 = NULL;
3437 struct got_object_qid *parent_id;
3438 char *commit_msg = NULL, *commit_msg0;
3440 /*print commit*/
3441 SIMPLEQ_FOREACH(re, &header->refs, entry) {
3442 char *s;
3443 const char *name;
3444 struct got_tag_object *tag = NULL;
3445 int cmp;
3447 if (got_ref_is_symbolic(re->ref))
3448 continue;
3450 name = got_ref_get_name(re->ref);
3451 if (strncmp(name, "refs/", 5) == 0)
3452 name += 5;
3453 if (strncmp(name, "got/", 4) == 0)
3454 continue;
3455 if (strncmp(name, "heads/", 6) == 0)
3456 name += 6;
3457 if (strncmp(name, "remotes/", 8) == 0)
3458 name += 8;
3459 if (strncmp(name, "tags/", 5) == 0) {
3460 error = got_object_open_as_tag(&tag, gw_trans->repo,
3461 re->id);
3462 if (error) {
3463 if (error->code != GOT_ERR_OBJ_TYPE)
3464 continue;
3466 * Ref points at something other
3467 * than a tag.
3469 error = NULL;
3470 tag = NULL;
3473 cmp = got_object_id_cmp(tag ?
3474 got_object_tag_get_object_id(tag) : re->id, id);
3475 if (tag)
3476 got_object_tag_close(tag);
3477 if (cmp != 0)
3478 continue;
3479 s = header->refs_str;
3480 if (asprintf(&header->refs_str, "%s%s%s", s ? s : "",
3481 s ? ", " : "", name) == -1) {
3482 error = got_error_from_errno("asprintf");
3483 free(s);
3484 header->refs_str = NULL;
3485 return error;
3487 free(s);
3490 error = got_object_id_str(&header->commit_id, id);
3491 if (error)
3492 return error;
3494 error = got_object_id_str(&header->tree_id,
3495 got_object_commit_get_tree_id(commit));
3496 if (error)
3497 return error;
3499 if (gw_trans->action == GW_DIFF) {
3500 parent_id = SIMPLEQ_FIRST(
3501 got_object_commit_get_parent_ids(commit));
3502 if (parent_id != NULL) {
3503 id2 = got_object_id_dup(parent_id->id);
3504 free (parent_id);
3505 error = got_object_id_str(&header->parent_id, id2);
3506 if (error)
3507 return error;
3508 free(id2);
3509 } else {
3510 header->parent_id = strdup("/dev/null");
3511 if (header->parent_id == NULL) {
3512 error = got_error_from_errno("strdup");
3513 return error;
3518 header->committer_time =
3519 got_object_commit_get_committer_time(commit);
3521 header->author =
3522 strdup(got_object_commit_get_author(commit));
3523 if (header->author == NULL) {
3524 error = got_error_from_errno("strdup");
3525 return error;
3527 header->committer =
3528 strdup(got_object_commit_get_committer(commit));
3529 if (header->committer == NULL) {
3530 error = got_error_from_errno("strdup");
3531 return error;
3533 error = got_object_commit_get_logmsg(&commit_msg0, commit);
3534 if (error)
3535 return error;
3537 commit_msg = commit_msg0;
3538 while (*commit_msg == '\n')
3539 commit_msg++;
3541 header->commit_msg = strdup(commit_msg);
3542 if (header->commit_msg == NULL)
3543 error = got_error_from_errno("strdup");
3544 free(commit_msg0);
3545 return error;
3548 static const struct got_error *
3549 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3551 const struct got_error *error = NULL;
3552 char *in_repo_path = NULL;
3553 struct got_object_id *id = NULL;
3555 error = got_repo_open(&gw_trans->repo, gw_trans->repo_path, NULL);
3556 if (error)
3557 return error;
3559 if (gw_trans->commit_id == NULL) {
3560 struct got_reference *head_ref;
3561 error = got_ref_open(&head_ref, gw_trans->repo,
3562 gw_trans->headref, 0);
3563 if (error)
3564 return error;
3566 error = got_ref_resolve(&id, gw_trans->repo, head_ref);
3567 got_ref_close(head_ref);
3568 if (error)
3569 return error;
3570 } else {
3571 struct got_reference *ref;
3573 error = got_ref_open(&ref, gw_trans->repo,
3574 gw_trans->commit_id, 0);
3575 if (error == NULL) {
3576 int obj_type;
3577 error = got_ref_resolve(&id, gw_trans->repo, ref);
3578 got_ref_close(ref);
3579 if (error)
3580 return error;
3581 error = got_object_get_type(&obj_type, gw_trans->repo,
3582 id);
3583 if (error)
3584 goto done;
3585 if (obj_type == GOT_OBJ_TYPE_TAG) {
3586 struct got_tag_object *tag;
3587 error = got_object_open_as_tag(&tag,
3588 gw_trans->repo, id);
3589 if (error)
3590 goto done;
3591 if (got_object_tag_get_object_type(tag) !=
3592 GOT_OBJ_TYPE_COMMIT) {
3593 got_object_tag_close(tag);
3594 error = got_error(GOT_ERR_OBJ_TYPE);
3595 goto done;
3597 free(id);
3598 id = got_object_id_dup(
3599 got_object_tag_get_object_id(tag));
3600 if (id == NULL)
3601 error = got_error_from_errno(
3602 "got_object_id_dup");
3603 got_object_tag_close(tag);
3604 if (error)
3605 goto done;
3606 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3607 error = got_error(GOT_ERR_OBJ_TYPE);
3608 goto done;
3611 error = got_repo_match_object_id_prefix(&id,
3612 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT,
3613 gw_trans->repo);
3614 if (error)
3615 goto done;
3618 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
3619 gw_trans->repo_path, 1);
3620 if (error)
3621 goto done;
3623 if (in_repo_path) {
3624 header->path = strdup(in_repo_path);
3625 if (header->path == NULL) {
3626 error = got_error_from_errno("strdup");
3627 goto done;
3631 error = got_ref_list(&header->refs, gw_trans->repo, NULL,
3632 got_ref_cmp_by_name, NULL);
3633 if (error)
3634 goto done;
3636 error = gw_get_commits(gw_trans, header, limit, id);
3637 done:
3638 got_ref_list_free(&header->refs);
3639 free(id);
3640 free(in_repo_path);
3641 return error;
3644 struct blame_line {
3645 int annotated;
3646 char *id_str;
3647 char *committer;
3648 char datebuf[11]; /* YYYY-MM-DD + NUL */
3651 struct gw_blame_cb_args {
3652 struct blame_line *lines;
3653 int nlines;
3654 int nlines_prec;
3655 int lineno_cur;
3656 off_t *line_offsets;
3657 FILE *f;
3658 struct got_repository *repo;
3659 struct gw_trans *gw_trans;
3662 static const struct got_error *
3663 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3665 const struct got_error *err = NULL;
3666 struct gw_blame_cb_args *a = arg;
3667 struct blame_line *bline;
3668 char *line = NULL;
3669 size_t linesize = 0;
3670 struct got_commit_object *commit = NULL;
3671 off_t offset;
3672 struct tm tm;
3673 time_t committer_time;
3674 enum kcgi_err kerr = KCGI_OK;
3676 if (nlines != a->nlines ||
3677 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3678 return got_error(GOT_ERR_RANGE);
3680 if (lineno == -1)
3681 return NULL; /* no change in this commit */
3683 /* Annotate this line. */
3684 bline = &a->lines[lineno - 1];
3685 if (bline->annotated)
3686 return NULL;
3687 err = got_object_id_str(&bline->id_str, id);
3688 if (err)
3689 return err;
3691 err = got_object_open_as_commit(&commit, a->repo, id);
3692 if (err)
3693 goto done;
3695 bline->committer = strdup(got_object_commit_get_committer(commit));
3696 if (bline->committer == NULL) {
3697 err = got_error_from_errno("strdup");
3698 goto done;
3701 committer_time = got_object_commit_get_committer_time(commit);
3702 if (localtime_r(&committer_time, &tm) == NULL)
3703 return got_error_from_errno("localtime_r");
3704 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3705 &tm) >= sizeof(bline->datebuf)) {
3706 err = got_error(GOT_ERR_NO_SPACE);
3707 goto done;
3709 bline->annotated = 1;
3711 /* Print lines annotated so far. */
3712 bline = &a->lines[a->lineno_cur - 1];
3713 if (!bline->annotated)
3714 goto done;
3716 offset = a->line_offsets[a->lineno_cur - 1];
3717 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3718 err = got_error_from_errno("fseeko");
3719 goto done;
3722 while (bline->annotated) {
3723 char *smallerthan, *at, *nl, *committer;
3724 char *href_diff = NULL;
3725 size_t len;
3727 if (getline(&line, &linesize, a->f) == -1) {
3728 if (ferror(a->f))
3729 err = got_error_from_errno("getline");
3730 break;
3733 committer = bline->committer;
3734 smallerthan = strchr(committer, '<');
3735 if (smallerthan && smallerthan[1] != '\0')
3736 committer = smallerthan + 1;
3737 at = strchr(committer, '@');
3738 if (at)
3739 *at = '\0';
3740 len = strlen(committer);
3741 if (len >= 9)
3742 committer[8] = '\0';
3744 nl = strchr(line, '\n');
3745 if (nl)
3746 *nl = '\0';
3748 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3749 "blame_wrapper", KATTR__MAX);
3750 if (kerr != KCGI_OK)
3751 goto err;
3752 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3753 "blame_number", KATTR__MAX);
3754 if (kerr != KCGI_OK)
3755 goto err;
3756 kerr = khtml_printf(a->gw_trans->gw_html_req, "%.*d",
3757 a->nlines_prec, a->lineno_cur);
3758 if (kerr != KCGI_OK)
3759 goto err;
3760 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3761 if (kerr != KCGI_OK)
3762 goto err;
3764 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3765 "blame_hash", KATTR__MAX);
3766 if (kerr != KCGI_OK)
3767 goto err;
3769 if (asprintf(&href_diff,
3770 "?path=%s&action=diff&commit=%s",
3771 a->gw_trans->repo_name, bline->id_str) == -1) {
3772 err = got_error_from_errno("asprintf");
3773 goto err;
3775 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3776 KATTR_HREF, href_diff, KATTR__MAX);
3777 if (kerr != KCGI_OK)
3778 goto done;
3779 kerr = khtml_printf(a->gw_trans->gw_html_req, "%.8s",
3780 bline->id_str);
3781 if (kerr != KCGI_OK)
3782 goto err;
3783 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3784 if (kerr != KCGI_OK)
3785 goto err;
3787 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3788 "blame_date", KATTR__MAX);
3789 if (kerr != KCGI_OK)
3790 goto err;
3791 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3792 if (kerr != KCGI_OK)
3793 goto err;
3794 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3795 if (kerr != KCGI_OK)
3796 goto err;
3798 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3799 "blame_author", KATTR__MAX);
3800 if (kerr != KCGI_OK)
3801 goto err;
3802 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
3803 if (kerr != KCGI_OK)
3804 goto err;
3805 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3806 if (kerr != KCGI_OK)
3807 goto err;
3809 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3810 "blame_code", KATTR__MAX);
3811 if (kerr != KCGI_OK)
3812 goto err;
3813 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
3814 if (kerr != KCGI_OK)
3815 goto err;
3816 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3817 if (kerr != KCGI_OK)
3818 goto err;
3820 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3821 if (kerr != KCGI_OK)
3822 goto err;
3824 a->lineno_cur++;
3825 bline = &a->lines[a->lineno_cur - 1];
3826 err:
3827 free(href_diff);
3829 done:
3830 if (commit)
3831 got_object_commit_close(commit);
3832 free(line);
3833 if (err == NULL && kerr != KCGI_OK)
3834 err = gw_kcgi_error(kerr);
3835 return err;
3838 static const struct got_error *
3839 gw_output_file_blame(struct gw_trans *gw_trans)
3841 const struct got_error *error = NULL;
3842 struct got_object_id *obj_id = NULL;
3843 struct got_object_id *commit_id = NULL;
3844 struct got_blob_object *blob = NULL;
3845 char *path = NULL, *in_repo_path = NULL;
3846 struct gw_blame_cb_args bca;
3847 int i, obj_type;
3848 size_t filesize;
3850 if (asprintf(&path, "%s%s%s",
3851 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3852 gw_trans->repo_folder ? "/" : "",
3853 gw_trans->repo_file) == -1) {
3854 error = got_error_from_errno("asprintf");
3855 goto done;
3858 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path, 1);
3859 if (error)
3860 goto done;
3862 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
3863 GOT_OBJ_TYPE_COMMIT, 1, gw_trans->repo);
3864 if (error)
3865 goto done;
3867 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
3868 in_repo_path);
3869 if (error)
3870 goto done;
3872 if (obj_id == NULL) {
3873 error = got_error(GOT_ERR_NO_OBJ);
3874 goto done;
3877 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
3878 if (error)
3879 goto done;
3881 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3882 error = got_error(GOT_ERR_OBJ_TYPE);
3883 goto done;
3886 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
3887 if (error)
3888 goto done;
3890 bca.f = got_opentemp();
3891 if (bca.f == NULL) {
3892 error = got_error_from_errno("got_opentemp");
3893 goto done;
3895 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3896 &bca.line_offsets, bca.f, blob);
3897 if (error || bca.nlines == 0)
3898 goto done;
3900 /* Don't include \n at EOF in the blame line count. */
3901 if (bca.line_offsets[bca.nlines - 1] == filesize)
3902 bca.nlines--;
3904 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3905 if (bca.lines == NULL) {
3906 error = got_error_from_errno("calloc");
3907 goto done;
3909 bca.lineno_cur = 1;
3910 bca.nlines_prec = 0;
3911 i = bca.nlines;
3912 while (i > 0) {
3913 i /= 10;
3914 bca.nlines_prec++;
3916 bca.repo = gw_trans->repo;
3917 bca.gw_trans = gw_trans;
3919 error = got_blame(in_repo_path, commit_id, gw_trans->repo, gw_blame_cb,
3920 &bca, NULL, NULL);
3921 done:
3922 free(in_repo_path);
3923 free(commit_id);
3924 free(obj_id);
3925 free(path);
3927 if (blob) {
3928 free(bca.line_offsets);
3929 for (i = 0; i < bca.nlines; i++) {
3930 struct blame_line *bline = &bca.lines[i];
3931 free(bline->id_str);
3932 free(bline->committer);
3934 free(bca.lines);
3935 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3936 error = got_error_from_errno("fclose");
3938 if (blob)
3939 got_object_blob_close(blob);
3940 return error;
3943 static const struct got_error *
3944 gw_output_blob_buf(struct gw_trans *gw_trans)
3946 const struct got_error *error = NULL;
3947 struct got_object_id *obj_id = NULL;
3948 struct got_object_id *commit_id = NULL;
3949 struct got_blob_object *blob = NULL;
3950 char *path = NULL, *in_repo_path = NULL;
3951 int obj_type, set_mime = 0;
3952 size_t len, hdrlen;
3953 const uint8_t *buf;
3954 enum kcgi_err kerr = KCGI_OK;
3956 if (asprintf(&path, "%s%s%s",
3957 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3958 gw_trans->repo_folder ? "/" : "",
3959 gw_trans->repo_file) == -1) {
3960 error = got_error_from_errno("asprintf");
3961 goto done;
3964 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path, 1);
3965 if (error)
3966 goto done;
3968 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
3969 GOT_OBJ_TYPE_COMMIT, 1, gw_trans->repo);
3970 if (error)
3971 goto done;
3973 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
3974 in_repo_path);
3975 if (error)
3976 goto done;
3978 if (obj_id == NULL) {
3979 error = got_error(GOT_ERR_NO_OBJ);
3980 goto done;
3983 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
3984 if (error)
3985 goto done;
3987 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3988 error = got_error(GOT_ERR_OBJ_TYPE);
3989 goto done;
3992 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
3993 if (error)
3994 goto done;
3996 hdrlen = got_object_blob_get_hdrlen(blob);
3997 do {
3998 error = got_object_blob_read_block(&len, blob);
3999 if (error)
4000 goto done;
4001 buf = got_object_blob_get_read_buf(blob);
4004 * Skip blob object header first time around,
4005 * which also contains a zero byte.
4007 buf += hdrlen;
4008 if (set_mime == 0) {
4009 if (isbinary(buf, len - hdrlen))
4010 gw_trans->mime = KMIME_APP_OCTET_STREAM;
4011 else
4012 gw_trans->mime = KMIME_TEXT_PLAIN;
4013 set_mime = 1;
4014 error = gw_display_index(gw_trans);
4015 if (error)
4016 goto done;
4018 kerr = khttp_write(gw_trans->gw_req, buf, len - hdrlen);
4019 if (kerr != KCGI_OK)
4020 goto done;
4021 hdrlen = 0;
4022 } while (len != 0);
4023 done:
4024 free(in_repo_path);
4025 free(commit_id);
4026 free(obj_id);
4027 free(path);
4028 if (blob)
4029 got_object_blob_close(blob);
4030 if (error == NULL && kerr != KCGI_OK)
4031 error = gw_kcgi_error(kerr);
4032 return error;
4035 static const struct got_error *
4036 gw_output_repo_tree(struct gw_trans *gw_trans)
4038 const struct got_error *error = NULL;
4039 struct got_object_id *tree_id = NULL, *commit_id = NULL;
4040 struct got_tree_object *tree = NULL;
4041 char *path = NULL, *in_repo_path = NULL;
4042 char *id_str = NULL;
4043 char *build_folder = NULL;
4044 char *href_blob = NULL, *href_blame = NULL;
4045 const char *class = NULL;
4046 int nentries, i, class_flip = 0;
4047 enum kcgi_err kerr = KCGI_OK;
4049 if (gw_trans->repo_folder != NULL) {
4050 path = strdup(gw_trans->repo_folder);
4051 if (path == NULL) {
4052 error = got_error_from_errno("strdup");
4053 goto done;
4055 } else {
4056 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
4057 gw_trans->repo_path, 1);
4058 if (error)
4059 goto done;
4060 free(path);
4061 path = in_repo_path;
4064 if (gw_trans->commit_id == NULL) {
4065 struct got_reference *head_ref;
4066 error = got_ref_open(&head_ref, gw_trans->repo,
4067 gw_trans->headref, 0);
4068 if (error)
4069 goto done;
4070 error = got_ref_resolve(&commit_id, gw_trans->repo, head_ref);
4071 if (error)
4072 goto done;
4073 got_ref_close(head_ref);
4075 * gw_trans->commit_id was not parsed from the querystring
4076 * we hit this code path from gw_index, where we don't know the
4077 * commit values for the tree link yet, so set
4078 * gw_trans->commit_id here to continue further into the tree
4080 error = got_object_id_str(&gw_trans->commit_id, commit_id);
4081 if (error)
4082 goto done;
4084 } else {
4085 error = got_repo_match_object_id(&commit_id, NULL,
4086 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT, 1,
4087 gw_trans->repo);
4088 if (error)
4089 goto done;
4092 error = got_object_id_by_path(&tree_id, gw_trans->repo, commit_id,
4093 path);
4094 if (error)
4095 goto done;
4097 error = got_object_open_as_tree(&tree, gw_trans->repo, tree_id);
4098 if (error)
4099 goto done;
4101 nentries = got_object_tree_get_nentries(tree);
4102 for (i = 0; i < nentries; i++) {
4103 struct got_tree_entry *te;
4104 const char *modestr = "";
4105 mode_t mode;
4107 te = got_object_tree_get_entry(tree, i);
4109 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
4110 if (error)
4111 goto done;
4113 mode = got_tree_entry_get_mode(te);
4114 if (got_object_tree_entry_is_submodule(te))
4115 modestr = "$";
4116 else if (S_ISLNK(mode))
4117 modestr = "@";
4118 else if (S_ISDIR(mode))
4119 modestr = "/";
4120 else if (mode & S_IXUSR)
4121 modestr = "*";
4123 if (class_flip == 0) {
4124 class = "back_lightgray";
4125 class_flip = 1;
4126 } else {
4127 class = "back_white";
4128 class_flip = 0;
4131 if (S_ISDIR(mode)) {
4132 if (asprintf(&build_folder, "%s/%s",
4133 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4134 got_tree_entry_get_name(te)) == -1) {
4135 error = got_error_from_errno("asprintf");
4136 goto done;
4138 if (asprintf(&href_blob,
4139 "?path=%s&action=%s&commit=%s&folder=%s",
4140 gw_trans->repo_name, gw_get_action_name(gw_trans),
4141 gw_trans->commit_id, build_folder) == -1) {
4142 error = got_error_from_errno("asprintf");
4143 goto done;
4146 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4147 KATTR_ID, "tree_wrapper", KATTR__MAX);
4148 if (kerr != KCGI_OK)
4149 goto done;
4150 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4151 KATTR_ID, "tree_line", KATTR_CLASS, class,
4152 KATTR__MAX);
4153 if (kerr != KCGI_OK)
4154 goto done;
4155 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4156 KATTR_HREF, href_blob, KATTR_CLASS,
4157 "diff_directory", KATTR__MAX);
4158 if (kerr != KCGI_OK)
4159 goto done;
4160 kerr = khtml_printf(gw_trans->gw_html_req, "%s%s",
4161 got_tree_entry_get_name(te), modestr);
4162 if (kerr != KCGI_OK)
4163 goto done;
4164 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4165 if (kerr != KCGI_OK)
4166 goto done;
4167 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4168 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
4169 KATTR__MAX);
4170 if (kerr != KCGI_OK)
4171 goto done;
4172 kerr = khtml_entity(gw_trans->gw_html_req,
4173 KENTITY_nbsp);
4174 if (kerr != KCGI_OK)
4175 goto done;
4176 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4177 if (kerr != KCGI_OK)
4178 goto done;
4179 } else {
4180 if (asprintf(&href_blob,
4181 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
4182 gw_trans->repo_name, "blob", gw_trans->commit_id,
4183 got_tree_entry_get_name(te),
4184 gw_trans->repo_folder ?
4185 gw_trans->repo_folder : "") == -1) {
4186 error = got_error_from_errno("asprintf");
4187 goto done;
4189 if (asprintf(&href_blame,
4190 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
4191 gw_trans->repo_name, "blame", gw_trans->commit_id,
4192 got_tree_entry_get_name(te),
4193 gw_trans->repo_folder ?
4194 gw_trans->repo_folder : "") == -1) {
4195 error = got_error_from_errno("asprintf");
4196 goto done;
4199 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4200 KATTR_ID, "tree_wrapper", KATTR__MAX);
4201 if (kerr != KCGI_OK)
4202 goto done;
4203 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4204 KATTR_ID, "tree_line", KATTR_CLASS, class,
4205 KATTR__MAX);
4206 if (kerr != KCGI_OK)
4207 goto done;
4208 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4209 KATTR_HREF, href_blob, KATTR__MAX);
4210 if (kerr != KCGI_OK)
4211 goto done;
4212 kerr = khtml_printf(gw_trans->gw_html_req, "%s%s",
4213 got_tree_entry_get_name(te), modestr);
4214 if (kerr != KCGI_OK)
4215 goto done;
4216 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4217 if (kerr != KCGI_OK)
4218 goto done;
4219 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4220 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
4221 KATTR__MAX);
4222 if (kerr != KCGI_OK)
4223 goto done;
4225 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4226 KATTR_HREF, href_blob, KATTR__MAX);
4227 if (kerr != KCGI_OK)
4228 goto done;
4229 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
4230 if (kerr != KCGI_OK)
4231 goto done;
4232 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4233 if (kerr != KCGI_OK)
4234 goto done;
4236 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4237 if (kerr != KCGI_OK)
4238 goto done;
4240 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4241 KATTR_HREF, href_blame, KATTR__MAX);
4242 if (kerr != KCGI_OK)
4243 goto done;
4244 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
4245 if (kerr != KCGI_OK)
4246 goto done;
4248 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4249 if (kerr != KCGI_OK)
4250 goto done;
4252 free(id_str);
4253 id_str = NULL;
4254 free(href_blob);
4255 href_blob = NULL;
4256 free(build_folder);
4257 build_folder = NULL;
4259 done:
4260 if (tree)
4261 got_object_tree_close(tree);
4262 free(id_str);
4263 free(href_blob);
4264 free(href_blame);
4265 free(in_repo_path);
4266 free(tree_id);
4267 free(build_folder);
4268 if (error == NULL && kerr != KCGI_OK)
4269 error = gw_kcgi_error(kerr);
4270 return error;
4273 static const struct got_error *
4274 gw_output_repo_heads(struct gw_trans *gw_trans)
4276 const struct got_error *error = NULL;
4277 struct got_reflist_head refs;
4278 struct got_reflist_entry *re;
4279 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
4280 char *href_commits = NULL;
4281 enum kcgi_err kerr = KCGI_OK;
4283 SIMPLEQ_INIT(&refs);
4285 error = got_ref_list(&refs, gw_trans->repo, "refs/heads",
4286 got_ref_cmp_by_name, NULL);
4287 if (error)
4288 goto done;
4290 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4291 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
4292 if (kerr != KCGI_OK)
4293 goto done;
4294 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4295 KATTR_ID, "summary_heads_title", KATTR__MAX);
4296 if (kerr != KCGI_OK)
4297 goto done;
4298 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
4299 if (kerr != KCGI_OK)
4300 goto done;
4301 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4302 if (kerr != KCGI_OK)
4303 goto done;
4304 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4305 KATTR_ID, "summary_heads_content", KATTR__MAX);
4306 if (kerr != KCGI_OK)
4307 goto done;
4309 SIMPLEQ_FOREACH(re, &refs, entry) {
4310 const char *refname;
4312 if (got_ref_is_symbolic(re->ref))
4313 continue;
4315 refname = got_ref_get_name(re->ref);
4316 if (strncmp(refname, "refs/heads/", 11) != 0)
4317 continue;
4319 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
4320 refname, TM_DIFF);
4321 if (error)
4322 goto done;
4324 if (strncmp(refname, "refs/heads/", 11) == 0)
4325 refname += 11;
4327 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4328 KATTR_ID, "heads_wrapper", KATTR__MAX);
4329 if (kerr != KCGI_OK)
4330 goto done;
4331 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4332 KATTR_ID, "heads_age", KATTR__MAX);
4333 if (kerr != KCGI_OK)
4334 goto done;
4335 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
4336 if (kerr != KCGI_OK)
4337 goto done;
4338 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4339 if (kerr != KCGI_OK)
4340 goto done;
4341 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4342 KATTR_ID, "heads_space", KATTR__MAX);
4343 if (kerr != KCGI_OK)
4344 goto done;
4345 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
4346 if (kerr != KCGI_OK)
4347 goto done;
4348 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4349 if (kerr != KCGI_OK)
4350 goto done;
4351 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4352 KATTR_ID, "head", KATTR__MAX);
4353 if (kerr != KCGI_OK)
4354 goto done;
4355 if (asprintf(&href_summary,
4356 "?path=%s&action=summary&headref=%s",
4357 gw_trans->repo_name, refname) == -1) {
4358 error = got_error_from_errno("asprintf");
4359 goto done;
4361 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4362 href_summary, KATTR__MAX);
4363 kerr = khtml_puts(gw_trans->gw_html_req, refname);
4364 if (kerr != KCGI_OK)
4365 goto done;
4366 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4367 if (kerr != KCGI_OK)
4368 goto done;
4370 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4371 "navs_wrapper", KATTR__MAX);
4372 if (kerr != KCGI_OK)
4373 goto done;
4374 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4375 "navs", KATTR__MAX);
4376 if (kerr != KCGI_OK)
4377 goto done;
4379 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4380 href_summary, KATTR__MAX);
4381 if (kerr != KCGI_OK)
4382 goto done;
4383 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4384 if (kerr != KCGI_OK)
4385 goto done;
4386 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4387 if (kerr != KCGI_OK)
4388 goto done;
4390 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4391 if (kerr != KCGI_OK)
4392 goto done;
4393 if (asprintf(&href_briefs, "?path=%s&action=briefs&headref=%s",
4394 gw_trans->repo_name, refname) == -1) {
4395 error = got_error_from_errno("asprintf");
4396 goto done;
4398 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4399 href_briefs, KATTR__MAX);
4400 if (kerr != KCGI_OK)
4401 goto done;
4402 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4403 if (kerr != KCGI_OK)
4404 goto done;
4405 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4406 if (kerr != KCGI_OK)
4407 goto done;
4409 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4410 if (kerr != KCGI_OK)
4411 goto done;
4413 if (asprintf(&href_commits,
4414 "?path=%s&action=commits&headref=%s",
4415 gw_trans->repo_name, refname) == -1) {
4416 error = got_error_from_errno("asprintf");
4417 goto done;
4419 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4420 href_commits, KATTR__MAX);
4421 if (kerr != KCGI_OK)
4422 goto done;
4423 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4424 if (kerr != KCGI_OK)
4425 goto done;
4426 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4427 if (kerr != KCGI_OK)
4428 goto done;
4430 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4431 "dotted_line", KATTR__MAX);
4432 if (kerr != KCGI_OK)
4433 goto done;
4434 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4435 if (kerr != KCGI_OK)
4436 goto done;
4437 free(href_summary);
4438 href_summary = NULL;
4439 free(href_briefs);
4440 href_briefs = NULL;
4441 free(href_commits);
4442 href_commits = NULL;
4444 done:
4445 got_ref_list_free(&refs);
4446 free(href_summary);
4447 free(href_briefs);
4448 free(href_commits);
4449 return error;
4452 static const struct got_error *
4453 gw_output_site_link(struct gw_trans *gw_trans)
4455 const struct got_error *error = NULL;
4456 char *href_summary = NULL;
4457 enum kcgi_err kerr = KCGI_OK;
4459 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4460 "site_link", KATTR__MAX);
4461 if (kerr != KCGI_OK)
4462 goto done;
4463 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4464 KATTR__MAX);
4465 if (kerr != KCGI_OK)
4466 goto done;
4467 kerr = khtml_puts(gw_trans->gw_html_req,
4468 gw_trans->gw_conf->got_site_link);
4469 if (kerr != KCGI_OK)
4470 goto done;
4471 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4472 if (kerr != KCGI_OK)
4473 goto done;
4475 if (gw_trans->repo_name != NULL) {
4476 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4477 if (kerr != KCGI_OK)
4478 goto done;
4479 if (asprintf(&href_summary, "?path=%s&action=summary",
4480 gw_trans->repo_name) == -1)
4481 goto done;
4482 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4483 href_summary, KATTR__MAX);
4484 if (kerr != KCGI_OK)
4485 goto done;
4486 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4487 if (kerr != KCGI_OK)
4488 goto done;
4489 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4490 if (kerr != KCGI_OK)
4491 goto done;
4492 kerr = khtml_printf(gw_trans->gw_html_req, " / %s",
4493 gw_get_action_name(gw_trans));
4494 if (kerr != KCGI_OK)
4495 goto done;
4498 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4499 if (kerr != KCGI_OK)
4500 goto done;
4501 done:
4502 free(href_summary);
4503 return error;
4506 static const struct got_error *
4507 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4509 const struct got_error *error = NULL;
4510 char *color = NULL;
4511 enum kcgi_err kerr = KCGI_OK;
4513 if (strncmp(buf, "-", 1) == 0)
4514 color = "diff_minus";
4515 else if (strncmp(buf, "+", 1) == 0)
4516 color = "diff_plus";
4517 else if (strncmp(buf, "@@", 2) == 0)
4518 color = "diff_chunk_header";
4519 else if (strncmp(buf, "@@", 2) == 0)
4520 color = "diff_chunk_header";
4521 else if (strncmp(buf, "commit +", 8) == 0)
4522 color = "diff_meta";
4523 else if (strncmp(buf, "commit -", 8) == 0)
4524 color = "diff_meta";
4525 else if (strncmp(buf, "blob +", 6) == 0)
4526 color = "diff_meta";
4527 else if (strncmp(buf, "blob -", 6) == 0)
4528 color = "diff_meta";
4529 else if (strncmp(buf, "file +", 6) == 0)
4530 color = "diff_meta";
4531 else if (strncmp(buf, "file -", 6) == 0)
4532 color = "diff_meta";
4533 else if (strncmp(buf, "from:", 5) == 0)
4534 color = "diff_author";
4535 else if (strncmp(buf, "via:", 4) == 0)
4536 color = "diff_author";
4537 else if (strncmp(buf, "date:", 5) == 0)
4538 color = "diff_date";
4539 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4540 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4541 if (error == NULL && kerr != KCGI_OK)
4542 error = gw_kcgi_error(kerr);
4543 return error;
4546 int
4547 main(int argc, char *argv[])
4549 const struct got_error *error = NULL;
4550 struct gw_trans *gw_trans;
4551 struct gw_dir *dir = NULL, *tdir;
4552 const char *page = "index";
4553 int gw_malloc = 1;
4554 enum kcgi_err kerr = KCGI_OK;
4556 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4557 errx(1, "malloc");
4559 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4560 errx(1, "malloc");
4562 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4563 errx(1, "malloc");
4565 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4566 errx(1, "malloc");
4568 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4569 if (kerr != KCGI_OK) {
4570 error = gw_kcgi_error(kerr);
4571 goto done;
4574 if ((gw_trans->gw_conf =
4575 malloc(sizeof(struct gotweb_conf))) == NULL) {
4576 gw_malloc = 0;
4577 error = got_error_from_errno("malloc");
4578 goto done;
4581 TAILQ_INIT(&gw_trans->gw_dirs);
4582 TAILQ_INIT(&gw_trans->gw_headers);
4584 gw_trans->action = -1;
4585 gw_trans->page = 0;
4586 gw_trans->repos_total = 0;
4587 gw_trans->repo_path = NULL;
4588 gw_trans->commit_id = NULL;
4589 gw_trans->next_id = NULL;
4590 gw_trans->next_prev_id = NULL;
4591 gw_trans->prev_id = NULL;
4592 gw_trans->prev_prev_id = NULL;
4593 gw_trans->headref = GOT_REF_HEAD;
4594 gw_trans->mime = KMIME_TEXT_HTML;
4595 gw_trans->gw_tmpl->key = gw_templs;
4596 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4597 gw_trans->gw_tmpl->arg = gw_trans;
4598 gw_trans->gw_tmpl->cb = gw_template;
4599 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
4600 if (error)
4601 goto done;
4603 error = gw_parse_querystring(gw_trans);
4604 if (error)
4605 goto done;
4607 if (gw_trans->action == GW_BLOB)
4608 error = gw_blob(gw_trans);
4609 else
4610 error = gw_display_index(gw_trans);
4611 done:
4612 if (gw_malloc) {
4613 free(gw_trans->gw_conf->got_repos_path);
4614 free(gw_trans->gw_conf->got_site_name);
4615 free(gw_trans->gw_conf->got_site_owner);
4616 free(gw_trans->gw_conf->got_site_link);
4617 free(gw_trans->gw_conf->got_logo);
4618 free(gw_trans->gw_conf->got_logo_url);
4619 free(gw_trans->gw_conf);
4620 free(gw_trans->commit_id);
4621 free(gw_trans->next_id);
4622 free(gw_trans->next_prev_id);
4623 free(gw_trans->prev_id);
4624 free(gw_trans->prev_prev_id);
4625 free(gw_trans->repo_path);
4626 if (gw_trans->repo)
4627 got_repo_close(gw_trans->repo);
4629 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4630 free(dir->name);
4631 free(dir->description);
4632 free(dir->age);
4633 free(dir->url);
4634 free(dir->path);
4635 free(dir);
4640 khttp_free(gw_trans->gw_req);
4641 return 0;