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 "buf.h"
50 #include "gotweb.h"
51 #include "gotweb_ui.h"
53 #ifndef nitems
54 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
55 #endif
57 struct gw_trans {
58 TAILQ_HEAD(headers, gw_header) gw_headers;
59 TAILQ_HEAD(dirs, gw_dir) gw_dirs;
60 struct gw_dir *gw_dir;
61 struct gotweb_conf *gw_conf;
62 struct ktemplate *gw_tmpl;
63 struct khtmlreq *gw_html_req;
64 struct kreq *gw_req;
65 char *repo_name;
66 char *repo_path;
67 char *commit;
68 char *repo_file;
69 char *repo_folder;
70 char *action_name;
71 char *headref;
72 unsigned int action;
73 unsigned int page;
74 unsigned int repos_total;
75 enum kmime mime;
76 };
78 struct gw_header {
79 TAILQ_ENTRY(gw_header) entry;
80 struct got_repository *repo;
81 struct got_reflist_head refs;
82 struct got_commit_object *commit;
83 struct got_object_id *id;
84 char *path;
86 char *refs_str;
87 char *commit_id; /* id_str1 */
88 char *parent_id; /* id_str2 */
89 char *tree_id;
90 const char *author;
91 const 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__ZMAX
115 };
117 enum gw_tmpl {
118 TEMPL_CONTENT,
119 TEMPL_HEAD,
120 TEMPL_HEADER,
121 TEMPL_SEARCH,
122 TEMPL_SITEPATH,
123 TEMPL_SITEOWNER,
124 TEMPL_TITLE,
125 TEMPL__MAX
126 };
128 enum gw_ref_tm {
129 TM_DIFF,
130 TM_LONG,
131 };
133 enum gw_tags {
134 TAGBRIEF,
135 TAGFULL,
136 };
138 static const char *const gw_templs[TEMPL__MAX] = {
139 "content",
140 "head",
141 "header",
142 "search",
143 "sitepath",
144 "siteowner",
145 "title",
146 };
148 static const struct kvalid gw_keys[KEY__ZMAX] = {
149 { kvalid_stringne, "action" },
150 { kvalid_stringne, "commit" },
151 { kvalid_stringne, "file" },
152 { kvalid_stringne, "folder" },
153 { kvalid_stringne, "headref" },
154 { kvalid_int, "page" },
155 { kvalid_stringne, "path" },
156 };
158 static struct gw_dir *gw_init_gw_dir(char *);
159 static struct gw_header *gw_init_header(void);
161 static const struct got_error *gw_get_repo_description(char **,
162 struct gw_trans *, char *);
163 static const struct got_error *gw_get_repo_owner(char **, struct gw_trans *,
164 char *);
165 static const struct got_error *gw_get_time_str(char **, time_t, int);
166 static const struct got_error *gw_get_repo_age(char **, struct gw_trans *,
167 char *, char *, int);
168 static const struct got_error *gw_output_file_blame(struct gw_trans *);
169 static const struct got_error *gw_output_blob_buf(struct gw_trans *);
170 static const struct got_error *gw_output_repo_tree(struct gw_trans *);
171 static const struct got_error *gw_output_diff(struct gw_trans *,
172 struct gw_header *);
173 static const struct got_error *gw_output_repo_tags(struct gw_trans *,
174 struct gw_header *, int, int);
175 static const struct got_error *gw_get_repo_heads(char **, struct gw_trans *);
176 static const struct got_error *gw_get_clone_url(char **, struct gw_trans *,
177 char *);
178 static char *gw_get_site_link(struct gw_trans *);
179 static const struct got_error *gw_html_escape(char **, const char *);
180 static const struct got_error *gw_colordiff_line(struct gw_trans *, char *);
182 static const struct got_error *gw_gen_commit_header(struct gw_trans *, char *,
183 char*);
184 static const struct got_error *gw_gen_diff_header(struct gw_trans *, char *,
185 char*);
186 static const struct got_error *gw_gen_author_header(struct gw_trans *,
187 const char *);
188 static const struct got_error *gw_gen_age_header(struct gw_trans *,
189 const char *);
190 static const struct got_error *gw_gen_committer_header(struct gw_trans *,
191 const char *);
192 static const struct got_error *gw_gen_commit_msg_header(struct gw_trans*,
193 char *);
194 static const struct got_error *gw_gen_tree_header(struct gw_trans *, char *);
196 static void gw_free_headers(struct gw_header *);
197 static const struct got_error* gw_display_open(struct gw_trans *, enum khttp,
198 enum kmime);
199 static const struct got_error* gw_display_index(struct gw_trans *);
200 static void gw_display_error(struct gw_trans *,
201 const struct got_error *);
203 static int gw_template(size_t, void *);
205 static const struct got_error* gw_get_header(struct gw_trans *,
206 struct gw_header *, int);
207 static const struct got_error* gw_get_commits(struct gw_trans *,
208 struct gw_header *, int);
209 static const struct got_error* gw_get_commit(struct gw_trans *,
210 struct gw_header *);
211 static const struct got_error* gw_apply_unveil(const char *, const char *);
212 static const struct got_error* gw_blame_cb(void *, int, int,
213 struct got_object_id *);
214 static const struct got_error* gw_load_got_paths(struct gw_trans *);
215 static const struct got_error* gw_load_got_path(struct gw_trans *,
216 struct gw_dir *);
217 static const struct got_error* gw_parse_querystring(struct gw_trans *);
219 static const struct got_error* gw_blame(struct gw_trans *);
220 static const struct got_error* gw_blob(struct gw_trans *);
221 static const struct got_error* gw_diff(struct gw_trans *);
222 static const struct got_error* gw_index(struct gw_trans *);
223 static const struct got_error* gw_commits(struct gw_trans *);
224 static const struct got_error* gw_briefs(struct gw_trans *);
225 static const struct got_error* gw_summary(struct gw_trans *);
226 static const struct got_error* gw_tree(struct gw_trans *);
227 static const struct got_error* gw_tag(struct gw_trans *);
229 struct gw_query_action {
230 unsigned int func_id;
231 const char *func_name;
232 const struct got_error *(*func_main)(struct gw_trans *);
233 char *template;
234 };
236 enum gw_query_actions {
237 GW_BLAME,
238 GW_BLOB,
239 GW_BRIEFS,
240 GW_COMMITS,
241 GW_DIFF,
242 GW_ERR,
243 GW_INDEX,
244 GW_SUMMARY,
245 GW_TAG,
246 GW_TREE,
247 };
249 static struct gw_query_action gw_query_funcs[] = {
250 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
251 { GW_BLOB, "blob", NULL, NULL },
252 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
253 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
254 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
255 { GW_ERR, NULL, NULL, "gw_tmpl/err.tmpl" },
256 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
257 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
258 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
259 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
260 };
262 static const struct got_error *
263 gw_kcgi_error(enum kcgi_err kerr)
265 if (kerr == KCGI_OK)
266 return NULL;
268 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
269 return got_error(GOT_ERR_CANCELLED);
271 if (kerr == KCGI_ENOMEM)
272 return got_error_set_errno(ENOMEM,
273 kcgi_strerror(kerr));
275 if (kerr == KCGI_ENFILE)
276 return got_error_set_errno(ENFILE,
277 kcgi_strerror(kerr));
279 if (kerr == KCGI_EAGAIN)
280 return got_error_set_errno(EAGAIN,
281 kcgi_strerror(kerr));
283 if (kerr == KCGI_FORM)
284 return got_error_msg(GOT_ERR_IO,
285 kcgi_strerror(kerr));
287 return got_error_from_errno(kcgi_strerror(kerr));
290 static const struct got_error *
291 gw_apply_unveil(const char *repo_path, const char *repo_file)
293 const struct got_error *err;
295 if (repo_path && repo_file) {
296 char *full_path;
297 if (asprintf(&full_path, "%s/%s", repo_path, repo_file) == -1)
298 return got_error_from_errno("asprintf unveil");
299 if (unveil(full_path, "r") != 0)
300 return got_error_from_errno2("unveil", full_path);
303 if (repo_path && unveil(repo_path, "r") != 0)
304 return got_error_from_errno2("unveil", repo_path);
306 if (unveil("/tmp", "rwc") != 0)
307 return got_error_from_errno2("unveil", "/tmp");
309 err = got_privsep_unveil_exec_helpers();
310 if (err != NULL)
311 return err;
313 if (unveil(NULL, NULL) != 0)
314 return got_error_from_errno("unveil");
316 return NULL;
319 static const struct got_error *
320 gw_empty_string(char **s)
322 *s = strdup("");
323 if (*s == NULL)
324 return got_error_from_errno("strdup");
325 return NULL;
328 static int
329 isbinary(const uint8_t *buf, size_t n)
331 size_t i;
333 for (i = 0; i < n; i++)
334 if (buf[i] == 0)
335 return 1;
336 return 0;
339 static const struct got_error *
340 gw_blame(struct gw_trans *gw_trans)
342 const struct got_error *error = NULL;
343 struct gw_header *header = NULL;
344 char *age = NULL, *escaped_commit_msg = NULL;
345 enum kcgi_err kerr;
347 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
348 NULL) == -1)
349 return got_error_from_errno("pledge");
351 if ((header = gw_init_header()) == NULL)
352 return got_error_from_errno("malloc");
354 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
355 if (error)
356 goto done;
358 error = gw_get_header(gw_trans, header, 1);
359 if (error)
360 goto done;
362 /* blame header */
363 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
364 "blame_header_wrapper", KATTR__MAX);
365 if (kerr != KCGI_OK)
366 goto done;
367 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
368 "blame_header", KATTR__MAX);
369 if (kerr != KCGI_OK)
370 goto done;
371 error = gw_get_time_str(&age, header->committer_time,
372 TM_LONG);
373 if (error)
374 goto done;
375 error = gw_gen_age_header(gw_trans, age ?age : "");
376 if (error)
377 goto done;
378 /*
379 * XXX: keeping this for now, since kcgihtml does not convert
380 * \n into <br /> yet.
381 */
382 error = gw_html_escape(&escaped_commit_msg, header->commit_msg);
383 if (error)
384 goto done;
385 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
386 if (error)
387 goto done;
388 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
389 if (kerr != KCGI_OK)
390 goto done;
391 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
392 "dotted_line", KATTR__MAX);
393 if (kerr != KCGI_OK)
394 goto done;
395 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
396 if (kerr != KCGI_OK)
397 goto done;
399 /* blame */
400 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
401 "blame", KATTR__MAX);
402 if (kerr != KCGI_OK)
403 goto done;
404 error = gw_output_file_blame(gw_trans);
405 if (error)
406 goto done;
407 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
408 done:
409 got_ref_list_free(&header->refs);
410 gw_free_headers(header);
411 free(escaped_commit_msg);
412 if (error == NULL && kerr != KCGI_OK)
413 error = gw_kcgi_error(kerr);
414 return error;
417 static const struct got_error *
418 gw_blob(struct gw_trans *gw_trans)
420 const struct got_error *error = NULL;
421 struct gw_header *header = NULL;
423 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
424 NULL) == -1)
425 return got_error_from_errno("pledge");
427 if ((header = gw_init_header()) == NULL)
428 return got_error_from_errno("malloc");
430 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
431 if (error)
432 goto done;
434 error = gw_get_header(gw_trans, header, 1);
435 if (error)
436 goto done;
438 error = gw_output_blob_buf(gw_trans);
439 done:
440 got_ref_list_free(&header->refs);
441 gw_free_headers(header);
442 return error;
445 static const struct got_error *
446 gw_diff(struct gw_trans *gw_trans)
448 const struct got_error *error = NULL;
449 struct gw_header *header = NULL;
450 char *age = NULL, *escaped_commit_msg = NULL;
451 enum kcgi_err kerr = KCGI_OK;
453 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
454 NULL) == -1)
455 return got_error_from_errno("pledge");
457 if ((header = gw_init_header()) == NULL)
458 return got_error_from_errno("malloc");
460 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
461 if (error)
462 goto done;
464 error = gw_get_header(gw_trans, header, 1);
465 if (error)
466 goto done;
468 /* diff header */
469 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
470 "diff_header_wrapper", KATTR__MAX);
471 if (kerr != KCGI_OK)
472 goto done;
473 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
474 "diff_header", KATTR__MAX);
475 if (kerr != KCGI_OK)
476 goto done;
477 error = gw_gen_diff_header(gw_trans, header->parent_id,
478 header->commit_id);
479 if (error)
480 goto done;
481 error = gw_gen_commit_header(gw_trans, header->commit_id,
482 header->refs_str);
483 if (error)
484 goto done;
485 error = gw_gen_tree_header(gw_trans, header->tree_id);
486 if (error)
487 goto done;
488 error = gw_gen_author_header(gw_trans, header->author);
489 if (error)
490 goto done;
491 error = gw_gen_committer_header(gw_trans, header->author);
492 if (error)
493 goto done;
494 error = gw_get_time_str(&age, header->committer_time,
495 TM_LONG);
496 if (error)
497 goto done;
498 error = gw_gen_age_header(gw_trans, age ?age : "");
499 if (error)
500 goto done;
501 /*
502 * XXX: keeping this for now, since kcgihtml does not convert
503 * \n into <br /> yet.
504 */
505 error = gw_html_escape(&escaped_commit_msg, header->commit_msg);
506 if (error)
507 goto done;
508 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
509 if (error)
510 goto done;
511 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
512 if (kerr != KCGI_OK)
513 goto done;
514 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
515 "dotted_line", KATTR__MAX);
516 if (kerr != KCGI_OK)
517 goto done;
518 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
519 if (kerr != KCGI_OK)
520 goto done;
522 /* diff */
523 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
524 "diff", KATTR__MAX);
525 if (kerr != KCGI_OK)
526 goto done;
527 error = gw_output_diff(gw_trans, header);
528 if (error)
529 goto done;
531 /* diff content close */
532 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
533 if (kerr != KCGI_OK)
534 goto done;
535 done:
536 got_ref_list_free(&header->refs);
537 gw_free_headers(header);
538 free(age);
539 free(escaped_commit_msg);
540 if (error == NULL && kerr != KCGI_OK)
541 error = gw_kcgi_error(kerr);
542 return error;
545 static const struct got_error *
546 gw_index(struct gw_trans *gw_trans)
548 const struct got_error *error = NULL;
549 struct gw_dir *gw_dir = NULL;
550 char *html, *navs, *next, *prev;
551 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
552 enum kcgi_err kerr;
554 if (pledge("stdio rpath proc exec sendfd unveil",
555 NULL) == -1) {
556 error = got_error_from_errno("pledge");
557 return error;
560 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
561 if (error)
562 return error;
564 error = gw_load_got_paths(gw_trans);
565 if (error)
566 return error;
568 kerr = khttp_puts(gw_trans->gw_req, index_projects_header);
569 if (kerr != KCGI_OK)
570 return gw_kcgi_error(kerr);
572 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
573 if (asprintf(&html, index_projects_empty,
574 gw_trans->gw_conf->got_repos_path) == -1)
575 return got_error_from_errno("asprintf");
576 kerr = khttp_puts(gw_trans->gw_req, html);
577 if (kerr != KCGI_OK)
578 error = gw_kcgi_error(kerr);
579 free(html);
580 return error;
583 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
584 dir_c++;
586 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
587 if (gw_trans->page > 0 && (gw_trans->page *
588 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
589 prev_disp++;
590 continue;
593 prev_disp++;
595 if (error)
596 return error;
597 if(asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
598 gw_dir->name, gw_dir->name) == -1)
599 return got_error_from_errno("asprintf");
601 if (asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
602 gw_dir->description, gw_dir->owner ? gw_dir->owner : "",
603 gw_dir->age,
604 navs) == -1)
605 return got_error_from_errno("asprintf");
607 kerr = khttp_puts(gw_trans->gw_req, html);
608 free(navs);
609 free(html);
610 if (kerr != KCGI_OK)
611 return gw_kcgi_error(kerr);
613 if (gw_trans->gw_conf->got_max_repos_display == 0)
614 continue;
616 if (next_disp == gw_trans->gw_conf->got_max_repos_display) {
617 kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
618 if (kerr != KCGI_OK)
619 return gw_kcgi_error(kerr);
620 } else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
621 (gw_trans->page > 0) &&
622 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
623 prev_disp == gw_trans->repos_total)) {
624 kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
625 if (kerr != KCGI_OK)
626 return gw_kcgi_error(kerr);
629 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
630 (gw_trans->page > 0) &&
631 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
632 prev_disp == gw_trans->repos_total)) {
633 if (asprintf(&prev, nav_prev, gw_trans->page - 1) == -1)
634 return got_error_from_errno("asprintf");
635 kerr = khttp_puts(gw_trans->gw_req, prev);
636 free(prev);
637 if (kerr != KCGI_OK)
638 return gw_kcgi_error(kerr);
641 kerr = khttp_puts(gw_trans->gw_req, div_end);
642 if (kerr != KCGI_OK)
643 return gw_kcgi_error(kerr);
645 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
646 next_disp == gw_trans->gw_conf->got_max_repos_display &&
647 dir_c != (gw_trans->page + 1) *
648 gw_trans->gw_conf->got_max_repos_display) {
649 if (asprintf(&next, nav_next, gw_trans->page + 1) == -1)
650 return got_error_from_errno("calloc");
651 kerr = khttp_puts(gw_trans->gw_req, next);
652 free(next);
653 if (kerr != KCGI_OK)
654 return gw_kcgi_error(kerr);
655 kerr = khttp_puts(gw_trans->gw_req, div_end);
656 if (kerr != KCGI_OK)
657 error = gw_kcgi_error(kerr);
658 next_disp = 0;
659 break;
662 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
663 (gw_trans->page > 0) &&
664 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
665 prev_disp == gw_trans->repos_total)) {
666 kerr = khttp_puts(gw_trans->gw_req, div_end);
667 if (kerr != KCGI_OK)
668 return gw_kcgi_error(kerr);
671 next_disp++;
673 return error;
676 static const struct got_error *
677 gw_commits(struct gw_trans *gw_trans)
679 const struct got_error *error = NULL;
680 struct gw_header *header = NULL, *n_header = NULL;
681 char *age = NULL, *escaped_commit_msg = NULL;
682 char *href_diff = NULL, *href_blob = NULL;
683 enum kcgi_err kerr = KCGI_OK;
685 if ((header = gw_init_header()) == NULL)
686 return got_error_from_errno("malloc");
688 if (pledge("stdio rpath proc exec sendfd unveil",
689 NULL) == -1) {
690 error = got_error_from_errno("pledge");
691 goto done;
694 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
695 if (error)
696 goto done;
698 error = gw_get_header(gw_trans, header,
699 gw_trans->gw_conf->got_max_commits_display);
700 if (error)
701 goto done;
703 /* commit content */
704 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
705 /* commit line */
706 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
707 "commits_line_wrapper", KATTR__MAX);
708 if (kerr != KCGI_OK)
709 goto done;
710 error = gw_gen_commit_header(gw_trans, n_header->commit_id,
711 n_header->refs_str);
712 if (error)
713 goto done;
714 error = gw_gen_author_header(gw_trans, n_header->author);
715 if (error)
716 goto done;
717 error = gw_gen_committer_header(gw_trans, n_header->author);
718 if (error)
719 goto done;
720 error = gw_get_time_str(&age, n_header->committer_time,
721 TM_LONG);
722 if (error)
723 goto done;
724 error = gw_gen_age_header(gw_trans, age ?age : "");
725 if (error)
726 goto done;
727 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
728 if (kerr != KCGI_OK)
729 goto done;
731 /* dotted line */
732 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
733 "dotted_line", KATTR__MAX);
734 if (kerr != KCGI_OK)
735 goto done;
736 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
737 if (kerr != KCGI_OK)
738 goto done;
740 /* commit */
741 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
742 "commit", KATTR__MAX);
743 if (kerr != KCGI_OK)
744 goto done;
745 /*
746 * XXX: keeping this for now, since kcgihtml does not convert
747 * \n into <br /> yet.
748 */
749 error = gw_html_escape(&escaped_commit_msg,
750 n_header->commit_msg);
751 if (error)
752 goto done;
753 kerr = khttp_puts(gw_trans->gw_req, escaped_commit_msg);
754 if (kerr != KCGI_OK)
755 goto done;
756 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
757 if (kerr != KCGI_OK)
758 goto done;
760 /* navs */
762 /* XXX: create gen code for this */
763 /* build diff nav */
764 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
765 gw_trans->repo_name, n_header->commit_id) == -1) {
766 error = got_error_from_errno("asprintf");
767 goto done;
769 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
770 KATTR_ID, "navs_wrapper", KATTR__MAX);
771 if (kerr != KCGI_OK)
772 goto done;
773 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
774 KATTR_ID, "navs", KATTR__MAX);
775 if (kerr != KCGI_OK)
776 goto done;
777 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
778 KATTR_HREF, href_diff, KATTR__MAX);
779 if (kerr != KCGI_OK)
780 goto done;
781 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
782 if (kerr != KCGI_OK)
783 goto done;
784 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
785 if (kerr != KCGI_OK)
786 goto done;
788 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
789 if (kerr != KCGI_OK)
790 goto done;
792 /* XXX: create gen code for this */
793 /* build tree nav */
794 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
795 gw_trans->repo_name, n_header->commit_id) == -1) {
796 error = got_error_from_errno("asprintf");
797 goto done;
799 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
800 KATTR_HREF, href_blob, KATTR__MAX);
801 if (kerr != KCGI_OK)
802 goto done;
803 khtml_puts(gw_trans->gw_html_req, "tree");
804 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
805 if (kerr != KCGI_OK)
806 goto done;
807 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
808 if (kerr != KCGI_OK)
809 goto done;
811 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
812 "solid_line", KATTR__MAX);
813 if (kerr != KCGI_OK)
814 goto done;
815 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
816 if (kerr != KCGI_OK)
817 goto done;
819 free(age);
820 age = NULL;
821 free(escaped_commit_msg);
822 escaped_commit_msg = NULL;
824 done:
825 got_ref_list_free(&header->refs);
826 gw_free_headers(header);
827 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
828 gw_free_headers(n_header);
829 free(age);
830 free(href_diff);
831 free(href_blob);
832 free(escaped_commit_msg);
833 if (error == NULL && kerr != KCGI_OK)
834 error = gw_kcgi_error(kerr);
835 return error;
838 static const struct got_error *
839 gw_briefs(struct gw_trans *gw_trans)
841 const struct got_error *error = NULL;
842 struct gw_header *header = NULL, *n_header = NULL;
843 char *age = NULL, *age_html = NULL;
844 char *href_diff = NULL, *href_blob = NULL;
845 char *newline, *smallerthan;
846 enum kcgi_err kerr = KCGI_OK;
848 if ((header = gw_init_header()) == NULL)
849 return got_error_from_errno("malloc");
851 if (pledge("stdio rpath proc exec sendfd unveil",
852 NULL) == -1) {
853 error = got_error_from_errno("pledge");
854 goto done;
857 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
858 if (error)
859 goto done;
861 if (gw_trans->action == GW_SUMMARY)
862 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
863 else
864 error = gw_get_header(gw_trans, header,
865 gw_trans->gw_conf->got_max_commits_display);
866 if (error)
867 goto done;
869 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
870 error = gw_get_time_str(&age, n_header->committer_time,
871 TM_DIFF);
872 if (error)
873 goto done;
875 /* briefs wrapper */
876 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
877 KATTR_ID, "briefs_wrapper", KATTR__MAX);
878 if (kerr != KCGI_OK)
879 goto done;
881 /* briefs age */
882 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
883 KATTR_ID, "briefs_age", KATTR__MAX);
884 if (kerr != KCGI_OK)
885 goto done;
886 if (asprintf(&age_html, "%s", age ? age : "") == -1) {
887 error = got_error_from_errno("asprintf");
888 goto done;
890 kerr = khtml_puts(gw_trans->gw_html_req, age_html);
891 if (kerr != KCGI_OK)
892 goto done;
893 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
894 if (kerr != KCGI_OK)
895 goto done;
897 /* briefs author */
898 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
899 KATTR_ID, "briefs_author", KATTR__MAX);
900 if (kerr != KCGI_OK)
901 goto done;
902 smallerthan = strchr(n_header->author, '<');
903 if (smallerthan)
904 *smallerthan = '\0';
905 kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
906 if (kerr != KCGI_OK)
907 goto done;
908 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
909 if (kerr != KCGI_OK)
910 goto done;
912 /* briefs log */
913 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
914 gw_trans->repo_name, n_header->commit_id) == -1) {
915 error = got_error_from_errno("asprintf");
916 goto done;
918 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
919 KATTR_ID, "briefs_log", KATTR__MAX);
920 if (kerr != KCGI_OK)
921 goto done;
922 newline = strchr(n_header->commit_msg, '\n');
923 if (newline)
924 *newline = '\0';
925 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
926 KATTR_HREF, href_diff, KATTR__MAX);
927 if (kerr != KCGI_OK)
928 goto done;
929 kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
930 if (kerr != KCGI_OK)
931 goto done;
932 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
933 if (kerr != KCGI_OK)
934 goto done;
936 /* build diff nav */
937 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
938 KATTR_ID, "navs_wrapper", KATTR__MAX);
939 if (kerr != KCGI_OK)
940 goto done;
941 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
942 KATTR_ID, "navs", KATTR__MAX);
943 if (kerr != KCGI_OK)
944 goto done;
945 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
946 KATTR_HREF, href_diff, KATTR__MAX);
947 if (kerr != KCGI_OK)
948 goto done;
949 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
950 if (kerr != KCGI_OK)
951 goto done;
952 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
953 if (kerr != KCGI_OK)
954 goto done;
956 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
957 if (kerr != KCGI_OK)
958 goto done;
960 /* build tree nav */
961 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
962 gw_trans->repo_name, n_header->commit_id) == -1) {
963 error = got_error_from_errno("asprintf");
964 goto done;
966 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
967 KATTR_HREF, href_blob, KATTR__MAX);
968 if (kerr != KCGI_OK)
969 goto done;
970 khtml_puts(gw_trans->gw_html_req, "tree");
971 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
972 if (kerr != KCGI_OK)
973 goto done;
974 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
975 if (kerr != KCGI_OK)
976 goto done;
978 /* dotted line */
979 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
980 KATTR_ID, "dotted_line", KATTR__MAX);
981 if (kerr != KCGI_OK)
982 goto done;
983 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
984 if (kerr != KCGI_OK)
985 goto done;
987 free(age);
988 age = NULL;
989 free(age_html);
990 age_html = NULL;
991 free(href_diff);
992 href_diff = NULL;
993 free(href_blob);
994 href_blob = NULL;
996 done:
997 got_ref_list_free(&header->refs);
998 gw_free_headers(header);
999 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1000 gw_free_headers(n_header);
1001 free(age);
1002 free(age_html);
1003 free(href_diff);
1004 free(href_blob);
1005 if (error == NULL && kerr != KCGI_OK)
1006 error = gw_kcgi_error(kerr);
1007 return error;
1010 static const struct got_error *
1011 gw_summary(struct gw_trans *gw_trans)
1013 const struct got_error *error = NULL;
1014 char *age = NULL, *tags = NULL, *heads = NULL;
1015 enum kcgi_err kerr = KCGI_OK;
1017 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1018 return got_error_from_errno("pledge");
1020 /* unveil is applied with gw_briefs below */
1022 /* summary wrapper */
1023 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1024 "summary_wrapper", KATTR__MAX);
1025 if (kerr != KCGI_OK)
1026 return gw_kcgi_error(kerr);
1028 /* description */
1029 if (gw_trans->gw_conf->got_show_repo_description &&
1030 gw_trans->gw_dir->description != NULL &&
1031 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1032 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1033 KATTR_ID, "description_title", KATTR__MAX);
1034 if (kerr != KCGI_OK)
1035 goto done;
1036 kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1037 if (kerr != KCGI_OK)
1038 goto done;
1039 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1040 if (kerr != KCGI_OK)
1041 goto done;
1042 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1043 KATTR_ID, "description", KATTR__MAX);
1044 if (kerr != KCGI_OK)
1045 goto done;
1046 kerr = khtml_puts(gw_trans->gw_html_req,
1047 gw_trans->gw_dir->description);
1048 if (kerr != KCGI_OK)
1049 goto done;
1050 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1051 if (kerr != KCGI_OK)
1052 goto done;
1055 /* repo owner */
1056 if (gw_trans->gw_conf->got_show_repo_owner &&
1057 gw_trans->gw_dir->owner != NULL) {
1058 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1059 KATTR_ID, "repo_owner_title", KATTR__MAX);
1060 if (kerr != KCGI_OK)
1061 goto done;
1062 kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1063 if (kerr != KCGI_OK)
1064 goto done;
1065 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1066 if (kerr != KCGI_OK)
1067 goto done;
1068 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1069 KATTR_ID, "repo_owner", KATTR__MAX);
1070 if (kerr != KCGI_OK)
1071 goto done;
1072 kerr = khtml_puts(gw_trans->gw_html_req,
1073 gw_trans->gw_dir->owner);
1074 if (kerr != KCGI_OK)
1075 goto done;
1076 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1077 if (kerr != KCGI_OK)
1078 goto done;
1081 /* last change */
1082 if (gw_trans->gw_conf->got_show_repo_age) {
1083 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1084 "refs/heads", TM_LONG);
1085 if (error)
1086 goto done;
1087 if (age != NULL) {
1088 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1089 KATTR_ID, "last_change_title", KATTR__MAX);
1090 if (kerr != KCGI_OK)
1091 goto done;
1092 kerr = khtml_puts(gw_trans->gw_html_req,
1093 "Last Change: ");
1094 if (kerr != KCGI_OK)
1095 goto done;
1096 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1097 if (kerr != KCGI_OK)
1098 goto done;
1099 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1100 KATTR_ID, "last_change", KATTR__MAX);
1101 if (kerr != KCGI_OK)
1102 goto done;
1103 kerr = khtml_puts(gw_trans->gw_html_req, age);
1104 if (kerr != KCGI_OK)
1105 goto done;
1106 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1107 if (kerr != KCGI_OK)
1108 goto done;
1112 /* cloneurl */
1113 if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1114 gw_trans->gw_dir->url != NULL &&
1115 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1116 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1117 KATTR_ID, "cloneurl_title", KATTR__MAX);
1118 if (kerr != KCGI_OK)
1119 goto done;
1120 kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1121 if (kerr != KCGI_OK)
1122 goto done;
1123 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1124 if (kerr != KCGI_OK)
1125 goto done;
1126 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1127 KATTR_ID, "cloneurl", KATTR__MAX);
1128 if (kerr != KCGI_OK)
1129 goto done;
1130 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1131 if (kerr != KCGI_OK)
1132 goto done;
1133 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1134 if (kerr != KCGI_OK)
1135 goto done;
1138 /* close summary wrapper */
1139 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1140 if (kerr != KCGI_OK)
1141 goto done;
1143 /* commit briefs header */
1144 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1145 "briefs_title_wrapper", KATTR__MAX);
1146 if (kerr != KCGI_OK)
1147 goto done;
1148 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1149 "briefs_title", KATTR__MAX);
1150 if (kerr != KCGI_OK)
1151 goto done;
1152 kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1153 if (kerr != KCGI_OK)
1154 goto done;
1155 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1156 if (kerr != KCGI_OK)
1157 goto done;
1158 error = gw_briefs(gw_trans);
1159 if (error)
1160 goto done;
1162 /* tags */
1163 error = gw_output_repo_tags(gw_trans, NULL, D_MAXSLCOMMDISP,
1164 TAGBRIEF);
1165 if (error)
1166 goto done;
1168 /* heads */
1169 error = gw_get_repo_heads(&heads, gw_trans);
1170 if (error)
1171 goto done;
1172 if (heads != NULL && strcmp(heads, "") != 0) {
1173 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1174 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
1175 if (kerr != KCGI_OK)
1176 goto done;
1177 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1178 KATTR_ID, "summary_heads_title", KATTR__MAX);
1179 if (kerr != KCGI_OK)
1180 goto done;
1181 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
1182 if (kerr != KCGI_OK)
1183 goto done;
1184 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1185 if (kerr != KCGI_OK)
1186 goto done;
1187 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1188 KATTR_ID, "summary_heads_content", KATTR__MAX);
1189 if (kerr != KCGI_OK)
1190 goto done;
1191 kerr = khttp_puts(gw_trans->gw_req, heads);
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 done:
1199 free(age);
1200 free(tags);
1201 free(heads);
1202 if (error == NULL && kerr != KCGI_OK)
1203 error = gw_kcgi_error(kerr);
1204 return error;
1207 static const struct got_error *
1208 gw_tree(struct gw_trans *gw_trans)
1210 const struct got_error *error = NULL;
1211 struct gw_header *header = NULL;
1212 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1213 char *age = NULL, *age_html = NULL, *escaped_commit_msg = NULL;
1214 enum kcgi_err kerr;
1216 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1217 return got_error_from_errno("pledge");
1219 if ((header = gw_init_header()) == NULL)
1220 return got_error_from_errno("malloc");
1222 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
1223 if (error)
1224 goto done;
1226 error = gw_get_header(gw_trans, header, 1);
1227 if (error)
1228 goto done;
1230 /* tree header */
1231 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1232 "tree_header_wrapper", KATTR__MAX);
1233 if (kerr != KCGI_OK)
1234 goto done;
1235 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1236 "tree_header", KATTR__MAX);
1237 if (kerr != KCGI_OK)
1238 goto done;
1239 error = gw_gen_tree_header(gw_trans, header->tree_id);
1240 if (error)
1241 goto done;
1242 error = gw_get_time_str(&age, header->committer_time,
1243 TM_LONG);
1244 if (error)
1245 goto done;
1246 error = gw_gen_age_header(gw_trans, age ?age : "");
1247 if (error)
1248 goto done;
1250 * XXX: keeping this for now, since kcgihtml does not convert
1251 * \n into <br /> yet.
1253 error = gw_html_escape(&escaped_commit_msg, header->commit_msg);
1254 if (error)
1255 goto done;
1256 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1257 if (error)
1258 goto done;
1259 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1260 if (kerr != KCGI_OK)
1261 goto done;
1262 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1263 "dotted_line", KATTR__MAX);
1264 if (kerr != KCGI_OK)
1265 goto done;
1266 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1267 if (kerr != KCGI_OK)
1268 goto done;
1270 /* tree */
1271 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1272 "tree", KATTR__MAX);
1273 if (kerr != KCGI_OK)
1274 goto done;
1275 error = gw_output_repo_tree(gw_trans);
1276 if (error)
1277 goto done;
1279 /* tree content close */
1280 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1281 done:
1282 got_ref_list_free(&header->refs);
1283 gw_free_headers(header);
1284 free(tree_html_disp);
1285 free(tree_html);
1286 free(tree);
1287 free(age);
1288 free(age_html);
1289 free(escaped_commit_msg);
1290 if (error == NULL && kerr != KCGI_OK)
1291 error = gw_kcgi_error(kerr);
1292 return error;
1295 static const struct got_error *
1296 gw_tag(struct gw_trans *gw_trans)
1298 const struct got_error *error = NULL;
1299 struct gw_header *header = NULL;
1300 char *escaped_commit_msg = NULL;
1301 enum kcgi_err kerr;
1303 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1304 return got_error_from_errno("pledge");
1306 if ((header = gw_init_header()) == NULL)
1307 return got_error_from_errno("malloc");
1309 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
1310 if (error)
1311 goto done;
1313 error = gw_get_header(gw_trans, header, 1);
1314 if (error)
1315 goto done;
1317 /* tag header */
1318 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1319 "tag_header_wrapper", KATTR__MAX);
1320 if (kerr != KCGI_OK)
1321 goto done;
1322 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1323 "tag_header", KATTR__MAX);
1324 if (kerr != KCGI_OK)
1325 goto done;
1326 error = gw_gen_commit_header(gw_trans, header->commit_id,
1327 header->refs_str);
1328 if (error)
1329 goto done;
1331 * XXX: keeping this for now, since kcgihtml does not convert
1332 * \n into <br /> yet.
1334 error = gw_html_escape(&escaped_commit_msg, header->commit_msg);
1335 if (error)
1336 goto done;
1337 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1338 if (error)
1339 goto done;
1340 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1341 if (kerr != KCGI_OK)
1342 goto done;
1343 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1344 "dotted_line", KATTR__MAX);
1345 if (kerr != KCGI_OK)
1346 goto done;
1347 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1348 if (kerr != KCGI_OK)
1349 goto done;
1351 /* tag */
1352 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1353 "tree", KATTR__MAX);
1354 if (kerr != KCGI_OK)
1355 goto done;
1357 error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1358 if (error)
1359 goto done;
1361 /* tag content close */
1362 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1363 done:
1364 got_ref_list_free(&header->refs);
1365 gw_free_headers(header);
1366 free(escaped_commit_msg);
1367 if (error == NULL && kerr != KCGI_OK)
1368 error = gw_kcgi_error(kerr);
1369 return error;
1372 static const struct got_error *
1373 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1375 const struct got_error *error = NULL;
1376 DIR *dt;
1377 char *dir_test;
1378 int opened = 0;
1380 if (asprintf(&dir_test, "%s/%s/%s",
1381 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1382 GOTWEB_GIT_DIR) == -1)
1383 return got_error_from_errno("asprintf");
1385 dt = opendir(dir_test);
1386 if (dt == NULL) {
1387 free(dir_test);
1388 } else {
1389 gw_dir->path = strdup(dir_test);
1390 opened = 1;
1391 goto done;
1394 if (asprintf(&dir_test, "%s/%s/%s",
1395 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1396 GOTWEB_GOT_DIR) == -1)
1397 return got_error_from_errno("asprintf");
1399 dt = opendir(dir_test);
1400 if (dt == NULL)
1401 free(dir_test);
1402 else {
1403 opened = 1;
1404 error = got_error(GOT_ERR_NOT_GIT_REPO);
1405 goto errored;
1408 if (asprintf(&dir_test, "%s/%s",
1409 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1)
1410 return got_error_from_errno("asprintf");
1412 gw_dir->path = strdup(dir_test);
1413 done:
1414 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1415 gw_dir->path);
1416 if (error)
1417 goto errored;
1418 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1419 if (error)
1420 goto errored;
1421 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1422 "refs/heads", TM_DIFF);
1423 if (error)
1424 goto errored;
1425 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1426 errored:
1427 free(dir_test);
1428 if (opened)
1429 closedir(dt);
1430 return error;
1433 static const struct got_error *
1434 gw_load_got_paths(struct gw_trans *gw_trans)
1436 const struct got_error *error = NULL;
1437 DIR *d;
1438 struct dirent **sd_dent;
1439 struct gw_dir *gw_dir;
1440 struct stat st;
1441 unsigned int d_cnt, d_i;
1443 d = opendir(gw_trans->gw_conf->got_repos_path);
1444 if (d == NULL) {
1445 error = got_error_from_errno2("opendir",
1446 gw_trans->gw_conf->got_repos_path);
1447 return error;
1450 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1451 alphasort);
1452 if (d_cnt == -1) {
1453 error = got_error_from_errno2("scandir",
1454 gw_trans->gw_conf->got_repos_path);
1455 return error;
1458 for (d_i = 0; d_i < d_cnt; d_i++) {
1459 if (gw_trans->gw_conf->got_max_repos > 0 &&
1460 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1461 break; /* account for parent and self */
1463 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1464 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1465 continue;
1467 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
1468 return got_error_from_errno("gw_dir malloc");
1470 error = gw_load_got_path(gw_trans, gw_dir);
1471 if (error && error->code == GOT_ERR_NOT_GIT_REPO)
1472 continue;
1473 else if (error)
1474 return error;
1476 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1477 !got_path_dir_is_empty(gw_dir->path)) {
1478 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1479 entry);
1480 gw_trans->repos_total++;
1484 closedir(d);
1485 return error;
1488 static const struct got_error *
1489 gw_parse_querystring(struct gw_trans *gw_trans)
1491 const struct got_error *error = NULL;
1492 struct kpair *p;
1493 struct gw_query_action *action = NULL;
1494 unsigned int i;
1496 if (gw_trans->gw_req->fieldnmap[0]) {
1497 error = got_error_from_errno("bad parse");
1498 return error;
1499 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1500 /* define gw_trans->repo_path */
1501 if (asprintf(&gw_trans->repo_name, "%s", p->parsed.s) == -1)
1502 return got_error_from_errno("asprintf");
1504 if (asprintf(&gw_trans->repo_path, "%s/%s",
1505 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
1506 return got_error_from_errno("asprintf");
1508 /* get action and set function */
1509 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
1510 for (i = 0; i < nitems(gw_query_funcs); i++) {
1511 action = &gw_query_funcs[i];
1512 if (action->func_name == NULL)
1513 continue;
1515 if (strcmp(action->func_name,
1516 p->parsed.s) == 0) {
1517 gw_trans->action = i;
1518 if (asprintf(&gw_trans->action_name,
1519 "%s", action->func_name) == -1)
1520 return
1521 got_error_from_errno(
1522 "asprintf");
1524 break;
1527 action = NULL;
1530 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
1531 if (asprintf(&gw_trans->commit, "%s",
1532 p->parsed.s) == -1)
1533 return got_error_from_errno("asprintf");
1535 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1536 if (asprintf(&gw_trans->repo_file, "%s",
1537 p->parsed.s) == -1)
1538 return got_error_from_errno("asprintf");
1540 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER]))
1541 if (asprintf(&gw_trans->repo_folder, "%s",
1542 p->parsed.s) == -1)
1543 return got_error_from_errno("asprintf");
1545 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
1546 if (asprintf(&gw_trans->headref, "%s",
1547 p->parsed.s) == -1)
1548 return got_error_from_errno("asprintf");
1550 if (action == NULL) {
1551 error = got_error_from_errno("invalid action");
1552 return error;
1554 if ((gw_trans->gw_dir =
1555 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
1556 return got_error_from_errno("gw_dir malloc");
1558 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1559 if (error)
1560 return error;
1561 } else
1562 gw_trans->action = GW_INDEX;
1564 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1565 gw_trans->page = p->parsed.i;
1567 return error;
1570 static struct gw_dir *
1571 gw_init_gw_dir(char *dir)
1573 struct gw_dir *gw_dir;
1575 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
1576 return NULL;
1578 if (asprintf(&gw_dir->name, "%s", dir) == -1)
1579 return NULL;
1581 return gw_dir;
1584 static const struct got_error *
1585 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1587 enum kcgi_err kerr;
1589 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1590 if (kerr != KCGI_OK)
1591 return gw_kcgi_error(kerr);
1592 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1593 khttps[code]);
1594 if (kerr != KCGI_OK)
1595 return gw_kcgi_error(kerr);
1596 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1597 kmimetypes[mime]);
1598 if (kerr != KCGI_OK)
1599 return gw_kcgi_error(kerr);
1600 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
1601 "nosniff");
1602 if (kerr != KCGI_OK)
1603 return gw_kcgi_error(kerr);
1604 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1605 if (kerr != KCGI_OK)
1606 return gw_kcgi_error(kerr);
1607 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
1608 "1; mode=block");
1609 if (kerr != KCGI_OK)
1610 return gw_kcgi_error(kerr);
1612 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
1613 kerr = khttp_head(gw_trans->gw_req,
1614 kresps[KRESP_CONTENT_DISPOSITION],
1615 "attachment; filename=%s", gw_trans->repo_file);
1616 if (kerr != KCGI_OK)
1617 return gw_kcgi_error(kerr);
1620 kerr = khttp_body(gw_trans->gw_req);
1621 return gw_kcgi_error(kerr);
1624 static const struct got_error *
1625 gw_display_index(struct gw_trans *gw_trans)
1627 const struct got_error *error;
1628 enum kcgi_err kerr;
1630 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1631 if (error)
1632 return error;
1634 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1635 if (kerr != KCGI_OK)
1636 return gw_kcgi_error(kerr);
1638 if (gw_trans->action != GW_BLOB) {
1639 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1640 gw_query_funcs[gw_trans->action].template);
1641 if (kerr != KCGI_OK) {
1642 khtml_close(gw_trans->gw_html_req);
1643 return gw_kcgi_error(kerr);
1647 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
1650 static void
1651 gw_display_error(struct gw_trans *gw_trans, const struct got_error *err)
1653 if (gw_display_open(gw_trans, KHTTP_200, gw_trans->mime) != NULL)
1654 return;
1656 if (khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0) != KCGI_OK)
1657 return;
1658 khtml_puts(gw_trans->gw_html_req, err->msg);
1659 khtml_close(gw_trans->gw_html_req);
1662 static int
1663 gw_template(size_t key, void *arg)
1665 const struct got_error *error = NULL;
1666 enum kcgi_err kerr;
1667 struct gw_trans *gw_trans = arg;
1668 char *gw_site_link, *img_src = NULL;
1670 switch (key) {
1671 case (TEMPL_HEAD):
1672 kerr = khttp_puts(gw_trans->gw_req, head);
1673 if (kerr != KCGI_OK)
1674 return 0;
1675 break;
1676 case(TEMPL_HEADER):
1677 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1678 KATTR_ID, "got_link", KATTR__MAX);
1679 if (kerr != KCGI_OK)
1680 return 0;
1681 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1682 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
1683 KATTR_TARGET, "_sotd", KATTR__MAX);
1684 if (kerr != KCGI_OK)
1685 return 0;
1686 if (asprintf(&img_src, "/%s",
1687 gw_trans->gw_conf->got_logo) == -1)
1688 return 0;
1689 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
1690 KATTR_SRC, img_src, KATTR__MAX);
1691 if (kerr != KCGI_OK) {
1692 free(img_src);
1693 return 0;
1695 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1696 if (kerr != KCGI_OK) {
1697 free(img_src);
1698 return 0;
1700 break;
1701 case (TEMPL_SITEPATH):
1702 gw_site_link = gw_get_site_link(gw_trans);
1703 if (gw_site_link != NULL) {
1704 kerr = khttp_puts(gw_trans->gw_req, gw_site_link);
1705 if (kerr != KCGI_OK) {
1706 free(gw_site_link);
1707 return 0;
1710 free(gw_site_link);
1711 break;
1712 case(TEMPL_TITLE):
1713 if (gw_trans->gw_conf->got_site_name != NULL) {
1714 kerr = khtml_puts(gw_trans->gw_html_req,
1715 gw_trans->gw_conf->got_site_name);
1716 if (kerr != KCGI_OK)
1717 return 0;
1719 break;
1720 case (TEMPL_SEARCH):
1721 kerr = khttp_puts(gw_trans->gw_req, search);
1722 if (kerr != KCGI_OK)
1723 return 0;
1724 break;
1725 case(TEMPL_SITEOWNER):
1726 if (gw_trans->gw_conf->got_site_owner != NULL &&
1727 gw_trans->gw_conf->got_show_site_owner) {
1728 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1729 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
1730 if (kerr != KCGI_OK)
1731 return 0;
1732 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1733 KATTR_ID, "site_owner", KATTR__MAX);
1734 if (kerr != KCGI_OK)
1735 return 0;
1736 kerr = khtml_puts(gw_trans->gw_html_req,
1737 gw_trans->gw_conf->got_site_owner);
1738 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1739 if (kerr != KCGI_OK)
1740 return 0;
1742 break;
1743 case(TEMPL_CONTENT):
1744 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1745 if (error) {
1746 kerr = khttp_puts(gw_trans->gw_req, error->msg);
1747 if (kerr != KCGI_OK)
1748 return 0;
1750 break;
1751 default:
1752 return 0;
1754 return 1;
1757 static const struct got_error *
1758 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
1760 const struct got_error *error = NULL;
1761 char *ref_str = NULL;
1762 enum kcgi_err kerr = KCGI_OK;
1764 if (strcmp(str2, "") != 0) {
1765 if (asprintf(&ref_str, "(%s)", str2) == -1) {
1766 error = got_error_from_errno("asprintf");
1767 goto done;
1769 } else
1770 ref_str = strdup("");
1772 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1773 KATTR_ID, "header_commit_title", KATTR__MAX);
1774 if (kerr != KCGI_OK)
1775 goto done;
1776 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
1777 if (kerr != KCGI_OK)
1778 goto done;
1779 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1780 if (kerr != KCGI_OK)
1781 goto done;
1782 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1783 KATTR_ID, "header_commit", KATTR__MAX);
1784 if (kerr != KCGI_OK)
1785 goto done;
1786 kerr = khtml_puts(gw_trans->gw_html_req, str1);
1787 if (kerr != KCGI_OK)
1788 goto done;
1789 kerr = khtml_puts(gw_trans->gw_html_req, " ");
1790 if (kerr != KCGI_OK)
1791 goto done;
1792 kerr = khtml_puts(gw_trans->gw_html_req, ref_str);
1793 if (kerr != KCGI_OK)
1794 goto done;
1795 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1796 done:
1797 if (error == NULL && kerr != KCGI_OK)
1798 error = gw_kcgi_error(kerr);
1799 return error;
1802 static const struct got_error *
1803 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
1805 const struct got_error *error = NULL;
1806 enum kcgi_err kerr = KCGI_OK;
1808 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1809 KATTR_ID, "header_diff_title", KATTR__MAX);
1810 if (kerr != KCGI_OK)
1811 goto done;
1812 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
1813 if (kerr != KCGI_OK)
1814 goto done;
1815 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1816 if (kerr != KCGI_OK)
1817 goto done;
1818 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1819 KATTR_ID, "header_diff", KATTR__MAX);
1820 if (kerr != KCGI_OK)
1821 goto done;
1822 kerr = khtml_puts(gw_trans->gw_html_req, str1);
1823 if (kerr != KCGI_OK)
1824 goto done;
1825 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
1826 if (kerr != KCGI_OK)
1827 goto done;
1828 kerr = khtml_puts(gw_trans->gw_html_req, str2);
1829 if (kerr != KCGI_OK)
1830 goto done;
1831 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1832 done:
1833 if (error == NULL && kerr != KCGI_OK)
1834 error = gw_kcgi_error(kerr);
1835 return error;
1838 static const struct got_error *
1839 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
1841 const struct got_error *error = NULL;
1842 enum kcgi_err kerr = KCGI_OK;
1844 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1845 KATTR_ID, "header_age_title", KATTR__MAX);
1846 if (kerr != KCGI_OK)
1847 goto done;
1848 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
1849 if (kerr != KCGI_OK)
1850 goto done;
1851 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1852 if (kerr != KCGI_OK)
1853 goto done;
1854 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1855 KATTR_ID, "header_age", KATTR__MAX);
1856 if (kerr != KCGI_OK)
1857 goto done;
1858 kerr = khtml_puts(gw_trans->gw_html_req, str);
1859 if (kerr != KCGI_OK)
1860 goto done;
1861 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1862 done:
1863 if (error == NULL && kerr != KCGI_OK)
1864 error = gw_kcgi_error(kerr);
1865 return error;
1868 static const struct got_error *
1869 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
1871 const struct got_error *error = NULL;
1872 enum kcgi_err kerr;
1874 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1875 KATTR_ID, "header_author_title", KATTR__MAX);
1876 if (kerr != KCGI_OK)
1877 goto done;
1878 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
1879 if (kerr != KCGI_OK)
1880 goto done;
1881 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1882 if (kerr != KCGI_OK)
1883 goto done;
1884 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1885 KATTR_ID, "header_author", KATTR__MAX);
1886 if (kerr != KCGI_OK)
1887 goto done;
1888 kerr = khtml_puts(gw_trans->gw_html_req, str);
1889 if (kerr != KCGI_OK)
1890 goto done;
1891 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1892 done:
1893 if (error == NULL && kerr != KCGI_OK)
1894 error = gw_kcgi_error(kerr);
1895 return error;
1898 static const struct got_error *
1899 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
1901 const struct got_error *error = NULL;
1902 enum kcgi_err kerr;
1904 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1905 KATTR_ID, "header_committer_title", KATTR__MAX);
1906 if (kerr != KCGI_OK)
1907 goto done;
1908 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
1909 if (kerr != KCGI_OK)
1910 goto done;
1911 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1912 if (kerr != KCGI_OK)
1913 goto done;
1914 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1915 KATTR_ID, "header_committer", KATTR__MAX);
1916 if (kerr != KCGI_OK)
1917 goto done;
1918 kerr = khtml_puts(gw_trans->gw_html_req, str);
1919 if (kerr != KCGI_OK)
1920 goto done;
1921 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1922 done:
1923 if (error == NULL && kerr != KCGI_OK)
1924 error = gw_kcgi_error(kerr);
1925 return error;
1928 static const struct got_error *
1929 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
1931 const struct got_error *error = NULL;
1932 enum kcgi_err kerr;
1934 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1935 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
1936 if (kerr != KCGI_OK)
1937 goto done;
1938 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
1939 if (kerr != KCGI_OK)
1940 goto done;
1941 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1942 if (kerr != KCGI_OK)
1943 goto done;
1944 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1945 KATTR_ID, "header_commit_msg", KATTR__MAX);
1946 if (kerr != KCGI_OK)
1947 goto done;
1948 kerr = khttp_puts(gw_trans->gw_req, str);
1949 if (kerr != KCGI_OK)
1950 goto done;
1951 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1952 done:
1953 if (error == NULL && kerr != KCGI_OK)
1954 error = gw_kcgi_error(kerr);
1955 return error;
1958 static const struct got_error *
1959 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
1961 const struct got_error *error = NULL;
1962 enum kcgi_err kerr;
1964 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1965 KATTR_ID, "header_tree_title", KATTR__MAX);
1966 if (kerr != KCGI_OK)
1967 goto done;
1968 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
1969 if (kerr != KCGI_OK)
1970 goto done;
1971 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1972 if (kerr != KCGI_OK)
1973 goto done;
1974 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1975 KATTR_ID, "header_tree", KATTR__MAX);
1976 if (kerr != KCGI_OK)
1977 goto done;
1978 kerr = khtml_puts(gw_trans->gw_html_req, str);
1979 if (kerr != KCGI_OK)
1980 goto done;
1981 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1982 done:
1983 if (error == NULL && kerr != KCGI_OK)
1984 error = gw_kcgi_error(kerr);
1985 return error;
1988 static const struct got_error *
1989 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
1990 char *dir)
1992 const struct got_error *error = NULL;
1993 FILE *f = NULL;
1994 char *d_file = NULL;
1995 unsigned int len;
1996 size_t n;
1998 *description = NULL;
1999 if (gw_trans->gw_conf->got_show_repo_description == 0)
2000 return gw_empty_string(description);
2002 if (asprintf(&d_file, "%s/description", dir) == -1)
2003 return got_error_from_errno("asprintf");
2005 f = fopen(d_file, "r");
2006 if (f == NULL) {
2007 if (errno == ENOENT || errno == EACCES)
2008 return gw_empty_string(description);
2009 error = got_error_from_errno2("fopen", d_file);
2010 goto done;
2013 if (fseek(f, 0, SEEK_END) == -1) {
2014 error = got_ferror(f, GOT_ERR_IO);
2015 goto done;
2017 len = ftell(f);
2018 if (len == -1) {
2019 error = got_ferror(f, GOT_ERR_IO);
2020 goto done;
2022 if (fseek(f, 0, SEEK_SET) == -1) {
2023 error = got_ferror(f, GOT_ERR_IO);
2024 goto done;
2026 *description = calloc(len + 1, sizeof(**description));
2027 if (*description == NULL) {
2028 error = got_error_from_errno("calloc");
2029 goto done;
2032 n = fread(*description, 1, len, f);
2033 if (n == 0 && ferror(f))
2034 error = got_ferror(f, GOT_ERR_IO);
2035 done:
2036 if (f != NULL && fclose(f) == -1 && error == NULL)
2037 error = got_error_from_errno("fclose");
2038 free(d_file);
2039 return error;
2042 static const struct got_error *
2043 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2045 struct tm tm;
2046 time_t diff_time;
2047 char *years = "years ago", *months = "months ago";
2048 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2049 char *minutes = "minutes ago", *seconds = "seconds ago";
2050 char *now = "right now";
2051 char *s;
2052 char datebuf[29];
2054 *repo_age = NULL;
2056 switch (ref_tm) {
2057 case TM_DIFF:
2058 diff_time = time(NULL) - committer_time;
2059 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2060 if (asprintf(repo_age, "%lld %s",
2061 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2062 return got_error_from_errno("asprintf");
2063 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2064 if (asprintf(repo_age, "%lld %s",
2065 (diff_time / 60 / 60 / 24 / (365 / 12)),
2066 months) == -1)
2067 return got_error_from_errno("asprintf");
2068 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2069 if (asprintf(repo_age, "%lld %s",
2070 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2071 return got_error_from_errno("asprintf");
2072 } else if (diff_time > 60 * 60 * 24 * 2) {
2073 if (asprintf(repo_age, "%lld %s",
2074 (diff_time / 60 / 60 / 24), days) == -1)
2075 return got_error_from_errno("asprintf");
2076 } else if (diff_time > 60 * 60 * 2) {
2077 if (asprintf(repo_age, "%lld %s",
2078 (diff_time / 60 / 60), hours) == -1)
2079 return got_error_from_errno("asprintf");
2080 } else if (diff_time > 60 * 2) {
2081 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2082 minutes) == -1)
2083 return got_error_from_errno("asprintf");
2084 } else if (diff_time > 2) {
2085 if (asprintf(repo_age, "%lld %s", diff_time,
2086 seconds) == -1)
2087 return got_error_from_errno("asprintf");
2088 } else {
2089 if (asprintf(repo_age, "%s", now) == -1)
2090 return got_error_from_errno("asprintf");
2092 break;
2093 case TM_LONG:
2094 if (gmtime_r(&committer_time, &tm) == NULL)
2095 return got_error_from_errno("gmtime_r");
2097 s = asctime_r(&tm, datebuf);
2098 if (s == NULL)
2099 return got_error_from_errno("asctime_r");
2101 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2102 return got_error_from_errno("asprintf");
2103 break;
2105 return NULL;
2108 static const struct got_error *
2109 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2110 char *repo_ref, int ref_tm)
2112 const struct got_error *error = NULL;
2113 struct got_object_id *id = NULL;
2114 struct got_repository *repo = NULL;
2115 struct got_commit_object *commit = NULL;
2116 struct got_reflist_head refs;
2117 struct got_reflist_entry *re;
2118 struct got_reference *head_ref;
2119 int is_head = 0;
2120 time_t committer_time = 0, cmp_time = 0;
2121 const char *refname;
2123 *repo_age = NULL;
2124 SIMPLEQ_INIT(&refs);
2126 if (repo_ref == NULL)
2127 return NULL;
2129 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
2130 is_head = 1;
2132 if (gw_trans->gw_conf->got_show_repo_age == 0)
2133 return NULL;
2135 error = got_repo_open(&repo, dir, NULL);
2136 if (error)
2137 goto done;
2139 if (is_head)
2140 error = got_ref_list(&refs, repo, "refs/heads",
2141 got_ref_cmp_by_name, NULL);
2142 else
2143 error = got_ref_list(&refs, repo, repo_ref,
2144 got_ref_cmp_by_name, NULL);
2145 if (error)
2146 goto done;
2148 SIMPLEQ_FOREACH(re, &refs, entry) {
2149 if (is_head)
2150 refname = strdup(repo_ref);
2151 else
2152 refname = got_ref_get_name(re->ref);
2153 error = got_ref_open(&head_ref, repo, refname, 0);
2154 if (error)
2155 goto done;
2157 error = got_ref_resolve(&id, repo, head_ref);
2158 got_ref_close(head_ref);
2159 if (error)
2160 goto done;
2162 error = got_object_open_as_commit(&commit, repo, id);
2163 if (error)
2164 goto done;
2166 committer_time =
2167 got_object_commit_get_committer_time(commit);
2169 if (cmp_time < committer_time)
2170 cmp_time = committer_time;
2173 if (cmp_time != 0) {
2174 committer_time = cmp_time;
2175 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2177 done:
2178 got_ref_list_free(&refs);
2179 free(id);
2180 return error;
2183 static const struct got_error *
2184 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2186 const struct got_error *error;
2187 FILE *f = NULL;
2188 struct got_object_id *id1 = NULL, *id2 = NULL;
2189 char *label1 = NULL, *label2 = NULL, *line = NULL;
2190 int obj_type;
2191 size_t linesize = 0;
2192 ssize_t linelen;
2193 enum kcgi_err kerr = KCGI_OK;
2195 f = got_opentemp();
2196 if (f == NULL)
2197 return NULL;
2199 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
2200 if (error)
2201 goto done;
2203 if (strncmp(header->parent_id, "/dev/null", 9) != 0) {
2204 error = got_repo_match_object_id(&id1, &label1,
2205 header->parent_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2206 if (error)
2207 goto done;
2210 error = got_repo_match_object_id(&id2, &label2,
2211 header->commit_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2212 if (error)
2213 goto done;
2215 error = got_object_get_type(&obj_type, header->repo, id2);
2216 if (error)
2217 goto done;
2218 switch (obj_type) {
2219 case GOT_OBJ_TYPE_BLOB:
2220 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
2221 header->repo, f);
2222 break;
2223 case GOT_OBJ_TYPE_TREE:
2224 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
2225 header->repo, f);
2226 break;
2227 case GOT_OBJ_TYPE_COMMIT:
2228 error = got_diff_objects_as_commits(id1, id2, 3, 0,
2229 header->repo, f);
2230 break;
2231 default:
2232 error = got_error(GOT_ERR_OBJ_TYPE);
2234 if (error)
2235 goto done;
2237 if (fseek(f, 0, SEEK_SET) == -1) {
2238 error = got_ferror(f, GOT_ERR_IO);
2239 goto done;
2242 while ((linelen = getline(&line, &linesize, f)) != -1) {
2243 error = gw_colordiff_line(gw_trans, line);
2244 if (error)
2245 goto done;
2246 /* XXX: KHTML_PRETTY breaks this */
2247 kerr = khtml_puts(gw_trans->gw_html_req, line);
2248 if (kerr != KCGI_OK)
2249 goto done;
2250 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2251 if (kerr != KCGI_OK)
2252 goto done;
2254 if (linelen == -1 && ferror(f))
2255 error = got_error_from_errno("getline");
2256 done:
2257 if (f && fclose(f) == -1 && error == NULL)
2258 error = got_error_from_errno("fclose");
2259 free(line);
2260 free(label1);
2261 free(label2);
2262 free(id1);
2263 free(id2);
2265 if (error == NULL && kerr != KCGI_OK)
2266 error = gw_kcgi_error(kerr);
2267 return error;
2270 static const struct got_error *
2271 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2273 const struct got_error *error = NULL;
2274 struct got_repository *repo;
2275 const char *gitconfig_owner;
2277 *owner = NULL;
2279 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2280 return NULL;
2282 error = got_repo_open(&repo, dir, NULL);
2283 if (error)
2284 return error;
2285 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2286 if (gitconfig_owner) {
2287 *owner = strdup(gitconfig_owner);
2288 if (*owner == NULL)
2289 error = got_error_from_errno("strdup");
2291 got_repo_close(repo);
2292 return error;
2295 static const struct got_error *
2296 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2298 const struct got_error *error = NULL;
2299 FILE *f;
2300 char *d_file = NULL;
2301 unsigned int len;
2302 size_t n;
2304 *url = NULL;
2306 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2307 return got_error_from_errno("asprintf");
2309 f = fopen(d_file, "r");
2310 if (f == NULL) {
2311 if (errno != ENOENT && errno != EACCES)
2312 error = got_error_from_errno2("fopen", d_file);
2313 goto done;
2316 if (fseek(f, 0, SEEK_END) == -1) {
2317 error = got_ferror(f, GOT_ERR_IO);
2318 goto done;
2320 len = ftell(f);
2321 if (len == -1) {
2322 error = got_ferror(f, GOT_ERR_IO);
2323 goto done;
2325 if (fseek(f, 0, SEEK_SET) == -1) {
2326 error = got_ferror(f, GOT_ERR_IO);
2327 goto done;
2330 *url = calloc(len + 1, sizeof(**url));
2331 if (*url == NULL) {
2332 error = got_error_from_errno("calloc");
2333 goto done;
2336 n = fread(*url, 1, len, f);
2337 if (n == 0 && ferror(f))
2338 error = got_ferror(f, GOT_ERR_IO);
2339 done:
2340 if (f && fclose(f) == -1 && error == NULL)
2341 error = got_error_from_errno("fclose");
2342 free(d_file);
2343 return NULL;
2346 static const struct got_error *
2347 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
2348 int limit, int tag_type)
2350 const struct got_error *error = NULL;
2351 struct got_repository *repo = NULL;
2352 struct got_reflist_head refs;
2353 struct got_reflist_entry *re;
2354 char *age = NULL;
2355 char *escaped_tag_commit = NULL;
2356 char *id_str = NULL, *refstr = NULL, *newline, *href_commits = NULL;
2357 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
2358 struct got_tag_object *tag = NULL;
2359 enum kcgi_err kerr = KCGI_OK;
2360 int summary_header_displayed = 0;
2362 SIMPLEQ_INIT(&refs);
2364 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2365 if (error)
2366 goto done;
2368 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
2369 if (error)
2370 goto done;
2372 SIMPLEQ_FOREACH(re, &refs, entry) {
2373 const char *refname;
2374 const char *tagger;
2375 const char *tag_commit;
2376 time_t tagger_time;
2377 struct got_object_id *id;
2379 refname = got_ref_get_name(re->ref);
2380 if (strncmp(refname, "refs/tags/", 10) != 0)
2381 continue;
2382 refname += 10;
2383 refstr = got_ref_to_str(re->ref);
2384 if (refstr == NULL) {
2385 error = got_error_from_errno("got_ref_to_str");
2386 goto done;
2389 error = got_ref_resolve(&id, repo, re->ref);
2390 if (error)
2391 goto done;
2394 * XXX: some of my repos are failing here. need to investigate.
2395 * currently setting error to NULL so no error is returned,
2396 * which stops Heads from being displayed on gw_summary.
2398 * got ref -l lists refs and first tag ref above can be
2399 * displayed
2401 * got tag -l will list tags just fine, so I don't know what
2402 * is happening.
2404 error = got_object_open_as_tag(&tag, repo, id);
2405 free(id);
2406 if (error) {
2407 error = NULL;
2408 goto done;
2411 tagger = got_object_tag_get_tagger(tag);
2412 tagger_time = got_object_tag_get_tagger_time(tag);
2414 error = got_object_id_str(&id_str,
2415 got_object_tag_get_object_id(tag));
2416 if (error)
2417 goto done;
2419 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
2420 strlen(id_str)) != 0)
2421 continue;
2423 tag_commit0 = strdup(got_object_tag_get_message(tag));
2424 if (tag_commit0 == NULL) {
2425 error = got_error_from_errno("strdup");
2426 goto done;
2429 tag_commit = tag_commit0;
2430 while (*tag_commit == '\n')
2431 tag_commit++;
2433 switch (tag_type) {
2434 case TAGBRIEF:
2435 newline = strchr(tag_commit, '\n');
2436 if (newline)
2437 *newline = '\0';
2439 if (summary_header_displayed == 0) {
2440 kerr = khtml_attr(gw_trans->gw_html_req,
2441 KELEM_DIV, KATTR_ID,
2442 "summary_tags_title_wrapper", KATTR__MAX);
2443 if (kerr != KCGI_OK)
2444 goto done;
2445 kerr = khtml_attr(gw_trans->gw_html_req,
2446 KELEM_DIV, KATTR_ID,
2447 "summary_tags_title", KATTR__MAX);
2448 if (kerr != KCGI_OK)
2449 goto done;
2450 kerr = khtml_puts(gw_trans->gw_html_req,
2451 "Tags");
2452 if (kerr != KCGI_OK)
2453 goto done;
2454 kerr = khtml_closeelem(gw_trans->gw_html_req,
2455 2);
2456 if (kerr != KCGI_OK)
2457 goto done;
2458 kerr = khtml_attr(gw_trans->gw_html_req,
2459 KELEM_DIV, KATTR_ID,
2460 "summary_tags_content", KATTR__MAX);
2461 if (kerr != KCGI_OK)
2462 goto done;
2463 summary_header_displayed = 1;
2466 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2467 KATTR_ID, "tags_wrapper", KATTR__MAX);
2468 if (kerr != KCGI_OK)
2469 goto done;
2470 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2471 KATTR_ID, "tags_age", KATTR__MAX);
2472 if (kerr != KCGI_OK)
2473 goto done;
2474 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
2475 if (error)
2476 goto done;
2477 kerr = khtml_puts(gw_trans->gw_html_req,
2478 age ? age : "");
2479 if (kerr != KCGI_OK)
2480 goto done;
2481 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2482 if (kerr != KCGI_OK)
2483 goto done;
2484 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2485 KATTR_ID, "tags", KATTR__MAX);
2486 if (kerr != KCGI_OK)
2487 goto done;
2488 kerr = khtml_puts(gw_trans->gw_html_req, refname);
2489 if (kerr != KCGI_OK)
2490 goto done;
2491 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2492 if (kerr != KCGI_OK)
2493 goto done;
2494 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2495 KATTR_ID, "tags_name", KATTR__MAX);
2496 if (kerr != KCGI_OK)
2497 goto done;
2498 if (asprintf(&href_tag, "?path=%s&action=tag&commit=%s",
2499 gw_trans->repo_name, id_str) == -1) {
2500 error = got_error_from_errno("asprintf");
2501 goto done;
2503 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2504 KATTR_HREF, href_tag, KATTR__MAX);
2505 if (kerr != KCGI_OK)
2506 goto done;
2507 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
2508 if (kerr != KCGI_OK)
2509 goto done;
2510 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2511 if (kerr != KCGI_OK)
2512 goto done;
2514 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2515 KATTR_ID, "navs_wrapper", KATTR__MAX);
2516 if (kerr != KCGI_OK)
2517 goto done;
2518 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2519 KATTR_ID, "navs", KATTR__MAX);
2520 if (kerr != KCGI_OK)
2521 goto done;
2523 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2524 KATTR_HREF, href_tag, KATTR__MAX);
2525 if (kerr != KCGI_OK)
2526 goto done;
2527 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
2528 if (kerr != KCGI_OK)
2529 goto done;
2530 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2531 if (kerr != KCGI_OK)
2532 goto done;
2534 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2535 if (kerr != KCGI_OK)
2536 goto done;
2538 if (asprintf(&href_briefs,
2539 "?path=%s&action=briefs&commit=%s",
2540 gw_trans->repo_name, id_str) == -1) {
2541 error = got_error_from_errno("asprintf");
2542 goto done;
2544 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2545 KATTR_HREF, href_briefs, KATTR__MAX);
2546 if (kerr != KCGI_OK)
2547 goto done;
2548 kerr = khtml_puts(gw_trans->gw_html_req,
2549 "commit briefs");
2550 if (kerr != KCGI_OK)
2551 goto done;
2552 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2553 if (kerr != KCGI_OK)
2554 goto done;
2556 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2557 if (kerr != KCGI_OK)
2558 goto done;
2560 if (asprintf(&href_commits,
2561 "?path=%s&action=commits&commit=%s",
2562 gw_trans->repo_name, id_str) == -1) {
2563 error = got_error_from_errno("asprintf");
2564 goto done;
2566 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2567 KATTR_HREF, href_commits, KATTR__MAX);
2568 if (kerr != KCGI_OK)
2569 goto done;
2570 kerr = khtml_puts(gw_trans->gw_html_req,
2571 "commits");
2572 if (kerr != KCGI_OK)
2573 goto done;
2574 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2575 if (kerr != KCGI_OK)
2576 goto done;
2578 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2579 KATTR_ID, "dotted_line", KATTR__MAX);
2580 if (kerr != KCGI_OK)
2581 goto done;
2582 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2583 if (kerr != KCGI_OK)
2584 goto done;
2585 break;
2586 case TAGFULL:
2587 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2588 KATTR_ID, "tag_info_date_title", KATTR__MAX);
2589 if (kerr != KCGI_OK)
2590 goto done;
2591 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
2592 if (kerr != KCGI_OK)
2593 goto done;
2594 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2595 if (kerr != KCGI_OK)
2596 goto done;
2597 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2598 KATTR_ID, "tag_info_date", KATTR__MAX);
2599 if (kerr != KCGI_OK)
2600 goto done;
2601 error = gw_get_time_str(&age, tagger_time, TM_LONG);
2602 if (error)
2603 goto done;
2604 kerr = khtml_puts(gw_trans->gw_html_req,
2605 age ? age : "");
2606 if (kerr != KCGI_OK)
2607 goto done;
2608 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2609 if (kerr != KCGI_OK)
2610 goto done;
2612 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2613 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
2614 if (kerr != KCGI_OK)
2615 goto done;
2616 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
2617 if (kerr != KCGI_OK)
2618 goto done;
2619 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2620 if (kerr != KCGI_OK)
2621 goto done;
2622 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2623 KATTR_ID, "tag_info_date", KATTR__MAX);
2624 if (kerr != KCGI_OK)
2625 goto done;
2626 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
2627 if (kerr != KCGI_OK)
2628 goto done;
2629 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2630 if (kerr != KCGI_OK)
2631 goto done;
2633 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2634 KATTR_ID, "tag_info", KATTR__MAX);
2635 if (kerr != KCGI_OK)
2636 goto done;
2638 * XXX: keeping this for now, since kcgihtml does not
2639 * convert \n into <br /> yet.
2641 error = gw_html_escape(&escaped_tag_commit, tag_commit);
2642 if (error)
2643 goto done;
2644 kerr = khttp_puts(gw_trans->gw_req, escaped_tag_commit);
2645 if (kerr != KCGI_OK)
2646 goto done;
2647 break;
2648 default:
2649 break;
2651 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2652 if (kerr != KCGI_OK)
2653 goto done;
2655 if (limit && --limit == 0)
2656 break;
2658 got_object_tag_close(tag);
2659 tag = NULL;
2660 free(id_str);
2661 id_str = NULL;
2662 free(refstr);
2663 refstr = NULL;
2664 free(age);
2665 age = NULL;
2666 free(escaped_tag_commit);
2667 escaped_tag_commit = NULL;
2668 free(tag_commit0);
2669 tag_commit0 = NULL;
2670 free(href_tag);
2671 href_tag = NULL;
2672 free(href_briefs);
2673 href_briefs = NULL;
2674 free(href_commits);
2675 href_commits = NULL;
2677 done:
2678 if (tag)
2679 got_object_tag_close(tag);
2680 free(id_str);
2681 free(refstr);
2682 free(age);
2683 free(escaped_tag_commit);
2684 free(tag_commit0);
2685 free(href_tag);
2686 free(href_briefs);
2687 free(href_commits);
2688 got_ref_list_free(&refs);
2689 if (repo)
2690 got_repo_close(repo);
2691 if (error == NULL && kerr != KCGI_OK)
2692 error = gw_kcgi_error(kerr);
2693 return error;
2696 static void
2697 gw_free_headers(struct gw_header *header)
2699 free(header->id);
2700 free(header->path);
2701 if (header->commit != NULL)
2702 got_object_commit_close(header->commit);
2703 if (header->repo)
2704 got_repo_close(header->repo);
2705 free(header->refs_str);
2706 free(header->commit_id);
2707 free(header->parent_id);
2708 free(header->tree_id);
2709 free(header->commit_msg);
2712 static struct gw_header *
2713 gw_init_header()
2715 struct gw_header *header;
2717 header = malloc(sizeof(*header));
2718 if (header == NULL)
2719 return NULL;
2721 header->repo = NULL;
2722 header->commit = NULL;
2723 header->id = NULL;
2724 header->path = NULL;
2725 SIMPLEQ_INIT(&header->refs);
2727 return header;
2730 static const struct got_error *
2731 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
2732 int limit)
2734 const struct got_error *error = NULL;
2735 struct got_commit_graph *graph = NULL;
2737 error = got_commit_graph_open(&graph, header->path, 0);
2738 if (error)
2739 goto done;
2741 error = got_commit_graph_iter_start(graph, header->id, header->repo,
2742 NULL, NULL);
2743 if (error)
2744 goto done;
2746 for (;;) {
2747 error = got_commit_graph_iter_next(&header->id, graph,
2748 header->repo, NULL, NULL);
2749 if (error) {
2750 if (error->code == GOT_ERR_ITER_COMPLETED)
2751 error = NULL;
2752 goto done;
2754 if (header->id == NULL)
2755 goto done;
2757 error = got_object_open_as_commit(&header->commit, header->repo,
2758 header->id);
2759 if (error)
2760 goto done;
2762 error = gw_get_commit(gw_trans, header);
2763 if (limit > 1) {
2764 struct gw_header *n_header = NULL;
2765 if ((n_header = gw_init_header()) == NULL) {
2766 error = got_error_from_errno("malloc");
2767 goto done;
2770 n_header->refs_str = strdup(header->refs_str);
2771 n_header->commit_id = strdup(header->commit_id);
2772 n_header->parent_id = strdup(header->parent_id);
2773 n_header->tree_id = strdup(header->tree_id);
2774 n_header->author = strdup(header->author);
2775 n_header->committer = strdup(header->committer);
2776 n_header->commit_msg = strdup(header->commit_msg);
2777 n_header->committer_time = header->committer_time;
2778 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
2779 entry);
2781 if (error || (limit && --limit == 0))
2782 break;
2784 done:
2785 if (graph)
2786 got_commit_graph_close(graph);
2787 return error;
2790 static const struct got_error *
2791 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
2793 const struct got_error *error = NULL;
2794 struct got_reflist_entry *re;
2795 struct got_object_id *id2 = NULL;
2796 struct got_object_qid *parent_id;
2797 char *refs_str = NULL, *commit_msg = NULL, *commit_msg0;
2799 /*print commit*/
2800 SIMPLEQ_FOREACH(re, &header->refs, entry) {
2801 char *s;
2802 const char *name;
2803 struct got_tag_object *tag = NULL;
2804 int cmp;
2806 name = got_ref_get_name(re->ref);
2807 if (strcmp(name, GOT_REF_HEAD) == 0)
2808 continue;
2809 if (strncmp(name, "refs/", 5) == 0)
2810 name += 5;
2811 if (strncmp(name, "got/", 4) == 0)
2812 continue;
2813 if (strncmp(name, "heads/", 6) == 0)
2814 name += 6;
2815 if (strncmp(name, "remotes/", 8) == 0)
2816 name += 8;
2817 if (strncmp(name, "tags/", 5) == 0) {
2818 error = got_object_open_as_tag(&tag, header->repo,
2819 re->id);
2820 if (error) {
2821 if (error->code != GOT_ERR_OBJ_TYPE)
2822 continue;
2824 * Ref points at something other
2825 * than a tag.
2827 error = NULL;
2828 tag = NULL;
2831 cmp = got_object_id_cmp(tag ?
2832 got_object_tag_get_object_id(tag) : re->id, header->id);
2833 if (tag)
2834 got_object_tag_close(tag);
2835 if (cmp != 0)
2836 continue;
2837 s = refs_str;
2838 if (asprintf(&refs_str, "%s%s%s", s ? s : "",
2839 s ? ", " : "", name) == -1) {
2840 error = got_error_from_errno("asprintf");
2841 free(s);
2842 return error;
2844 header->refs_str = strdup(refs_str);
2845 free(s);
2848 if (refs_str == NULL)
2849 header->refs_str = strdup("");
2850 free(refs_str);
2852 error = got_object_id_str(&header->commit_id, header->id);
2853 if (error)
2854 return error;
2856 error = got_object_id_str(&header->tree_id,
2857 got_object_commit_get_tree_id(header->commit));
2858 if (error)
2859 return error;
2861 if (gw_trans->action == GW_DIFF) {
2862 parent_id = SIMPLEQ_FIRST(
2863 got_object_commit_get_parent_ids(header->commit));
2864 if (parent_id != NULL) {
2865 id2 = got_object_id_dup(parent_id->id);
2866 free (parent_id);
2867 error = got_object_id_str(&header->parent_id, id2);
2868 if (error)
2869 return error;
2870 free(id2);
2871 } else
2872 header->parent_id = strdup("/dev/null");
2873 } else
2874 header->parent_id = strdup("");
2876 header->committer_time =
2877 got_object_commit_get_committer_time(header->commit);
2879 header->author =
2880 got_object_commit_get_author(header->commit);
2881 header->committer =
2882 got_object_commit_get_committer(header->commit);
2883 if (error)
2884 return error;
2886 /* XXX Doesn't the log message require escaping? */
2887 error = got_object_commit_get_logmsg(&commit_msg0, header->commit);
2888 if (error)
2889 return error;
2891 commit_msg = commit_msg0;
2892 while (*commit_msg == '\n')
2893 commit_msg++;
2895 header->commit_msg = strdup(commit_msg);
2896 free(commit_msg0);
2897 return error;
2900 static const struct got_error *
2901 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
2903 const struct got_error *error = NULL;
2904 char *in_repo_path = NULL;
2906 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
2907 if (error)
2908 return error;
2910 if (gw_trans->commit == NULL) {
2911 struct got_reference *head_ref;
2912 error = got_ref_open(&head_ref, header->repo,
2913 gw_trans->headref, 0);
2914 if (error)
2915 return error;
2917 error = got_ref_resolve(&header->id, header->repo, head_ref);
2918 got_ref_close(head_ref);
2919 if (error)
2920 return error;
2922 error = got_object_open_as_commit(&header->commit,
2923 header->repo, header->id);
2924 } else {
2925 struct got_reference *ref;
2926 error = got_ref_open(&ref, header->repo, gw_trans->commit, 0);
2927 if (error == NULL) {
2928 int obj_type;
2929 error = got_ref_resolve(&header->id, header->repo, ref);
2930 got_ref_close(ref);
2931 if (error)
2932 return error;
2933 error = got_object_get_type(&obj_type, header->repo,
2934 header->id);
2935 if (error)
2936 return error;
2937 if (obj_type == GOT_OBJ_TYPE_TAG) {
2938 struct got_tag_object *tag;
2939 error = got_object_open_as_tag(&tag,
2940 header->repo, header->id);
2941 if (error)
2942 return error;
2943 if (got_object_tag_get_object_type(tag) !=
2944 GOT_OBJ_TYPE_COMMIT) {
2945 got_object_tag_close(tag);
2946 error = got_error(GOT_ERR_OBJ_TYPE);
2947 return error;
2949 free(header->id);
2950 header->id = got_object_id_dup(
2951 got_object_tag_get_object_id(tag));
2952 if (header->id == NULL)
2953 error = got_error_from_errno(
2954 "got_object_id_dup");
2955 got_object_tag_close(tag);
2956 if (error)
2957 return error;
2958 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2959 error = got_error(GOT_ERR_OBJ_TYPE);
2960 return error;
2962 error = got_object_open_as_commit(&header->commit,
2963 header->repo, header->id);
2964 if (error)
2965 return error;
2967 if (header->commit == NULL) {
2968 error = got_repo_match_object_id_prefix(&header->id,
2969 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2970 header->repo);
2971 if (error)
2972 return error;
2974 error = got_repo_match_object_id_prefix(&header->id,
2975 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2976 header->repo);
2979 error = got_repo_map_path(&in_repo_path, header->repo,
2980 gw_trans->repo_path, 1);
2981 if (error)
2982 return error;
2984 if (in_repo_path) {
2985 header->path = strdup(in_repo_path);
2987 free(in_repo_path);
2989 error = got_ref_list(&header->refs, header->repo, NULL,
2990 got_ref_cmp_by_name, NULL);
2991 if (error)
2992 return error;
2994 error = gw_get_commits(gw_trans, header, limit);
2995 return error;
2998 struct blame_line {
2999 int annotated;
3000 char *id_str;
3001 char *committer;
3002 char datebuf[11]; /* YYYY-MM-DD + NUL */
3005 struct gw_blame_cb_args {
3006 struct blame_line *lines;
3007 int nlines;
3008 int nlines_prec;
3009 int lineno_cur;
3010 off_t *line_offsets;
3011 FILE *f;
3012 struct got_repository *repo;
3013 struct gw_trans *gw_trans;
3016 static const struct got_error *
3017 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3019 const struct got_error *err = NULL;
3020 struct gw_blame_cb_args *a = arg;
3021 struct blame_line *bline;
3022 char *line = NULL;
3023 size_t linesize = 0;
3024 struct got_commit_object *commit = NULL;
3025 off_t offset;
3026 struct tm tm;
3027 time_t committer_time;
3028 enum kcgi_err kerr = KCGI_OK;
3030 if (nlines != a->nlines ||
3031 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3032 return got_error(GOT_ERR_RANGE);
3034 if (lineno == -1)
3035 return NULL; /* no change in this commit */
3037 /* Annotate this line. */
3038 bline = &a->lines[lineno - 1];
3039 if (bline->annotated)
3040 return NULL;
3041 err = got_object_id_str(&bline->id_str, id);
3042 if (err)
3043 return err;
3045 err = got_object_open_as_commit(&commit, a->repo, id);
3046 if (err)
3047 goto done;
3049 bline->committer = strdup(got_object_commit_get_committer(commit));
3050 if (bline->committer == NULL) {
3051 err = got_error_from_errno("strdup");
3052 goto done;
3055 committer_time = got_object_commit_get_committer_time(commit);
3056 if (localtime_r(&committer_time, &tm) == NULL)
3057 return got_error_from_errno("localtime_r");
3058 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3059 &tm) >= sizeof(bline->datebuf)) {
3060 err = got_error(GOT_ERR_NO_SPACE);
3061 goto done;
3063 bline->annotated = 1;
3065 /* Print lines annotated so far. */
3066 bline = &a->lines[a->lineno_cur - 1];
3067 if (!bline->annotated)
3068 goto done;
3070 offset = a->line_offsets[a->lineno_cur - 1];
3071 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3072 err = got_error_from_errno("fseeko");
3073 goto done;
3076 while (bline->annotated) {
3077 char *smallerthan, *at, *nl, *committer;
3078 char *lineno = NULL, *href_diff = NULL, *href_link = NULL;
3079 size_t len;
3081 if (getline(&line, &linesize, a->f) == -1) {
3082 if (ferror(a->f))
3083 err = got_error_from_errno("getline");
3084 break;
3087 committer = bline->committer;
3088 smallerthan = strchr(committer, '<');
3089 if (smallerthan && smallerthan[1] != '\0')
3090 committer = smallerthan + 1;
3091 at = strchr(committer, '@');
3092 if (at)
3093 *at = '\0';
3094 len = strlen(committer);
3095 if (len >= 9)
3096 committer[8] = '\0';
3098 nl = strchr(line, '\n');
3099 if (nl)
3100 *nl = '\0';
3102 if (a->gw_trans->repo_folder == NULL)
3103 a->gw_trans->repo_folder = strdup("");
3104 if (a->gw_trans->repo_folder == NULL)
3105 goto err;
3107 /* blame line */
3108 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3109 "blame_wrapper", KATTR__MAX);
3110 if (kerr != KCGI_OK)
3111 goto err;
3112 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3113 "blame_number", KATTR__MAX);
3114 if (kerr != KCGI_OK)
3115 goto err;
3116 if (asprintf(&lineno, "%.*d", a->nlines_prec,
3117 a->lineno_cur) == -1)
3118 goto err;
3119 kerr = khtml_puts(a->gw_trans->gw_html_req, lineno);
3120 if (kerr != KCGI_OK)
3121 goto err;
3122 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3123 if (kerr != KCGI_OK)
3124 goto err;
3126 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3127 "blame_hash", KATTR__MAX);
3128 if (kerr != KCGI_OK)
3129 goto err;
3130 if (asprintf(&href_diff,
3131 "?path=%s&action=diff&commit=%s&file=%s&folder=%s",
3132 a->gw_trans->repo_name, bline->id_str,
3133 a->gw_trans->repo_file, a->gw_trans->repo_folder) == -1) {
3134 err = got_error_from_errno("asprintf");
3135 goto err;
3137 if (asprintf(&href_link, "%.8s", bline->id_str) == -1) {
3138 err = got_error_from_errno("asprintf");
3139 goto err;
3141 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3142 KATTR_HREF, href_diff, KATTR__MAX);
3143 if (kerr != KCGI_OK)
3144 goto done;
3145 kerr = khtml_puts(a->gw_trans->gw_html_req, href_link);
3146 if (kerr != KCGI_OK)
3147 goto err;
3148 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3149 if (kerr != KCGI_OK)
3150 goto err;
3152 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3153 "blame_date", KATTR__MAX);
3154 if (kerr != KCGI_OK)
3155 goto err;
3156 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3157 if (kerr != KCGI_OK)
3158 goto err;
3159 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3160 if (kerr != KCGI_OK)
3161 goto err;
3163 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3164 "blame_author", KATTR__MAX);
3165 if (kerr != KCGI_OK)
3166 goto err;
3167 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
3168 if (kerr != KCGI_OK)
3169 goto err;
3170 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3171 if (kerr != KCGI_OK)
3172 goto err;
3174 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3175 "blame_code", KATTR__MAX);
3176 if (kerr != KCGI_OK)
3177 goto err;
3178 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
3179 if (kerr != KCGI_OK)
3180 goto err;
3181 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3182 if (kerr != KCGI_OK)
3183 goto err;
3185 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3186 if (kerr != KCGI_OK)
3187 goto err;
3189 a->lineno_cur++;
3190 bline = &a->lines[a->lineno_cur - 1];
3191 err:
3192 free(lineno);
3193 free(href_diff);
3194 free(href_link);
3196 done:
3197 if (commit)
3198 got_object_commit_close(commit);
3199 free(line);
3200 if (err == NULL && kerr != KCGI_OK)
3201 err = gw_kcgi_error(kerr);
3202 return err;
3205 static const struct got_error *
3206 gw_output_file_blame(struct gw_trans *gw_trans)
3208 const struct got_error *error = NULL;
3209 struct got_repository *repo = NULL;
3210 struct got_object_id *obj_id = NULL;
3211 struct got_object_id *commit_id = NULL;
3212 struct got_blob_object *blob = NULL;
3213 char *path = NULL, *in_repo_path = NULL;
3214 struct gw_blame_cb_args bca;
3215 int i, obj_type;
3216 size_t filesize;
3218 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3219 if (error)
3220 return error;
3222 if (asprintf(&path, "%s%s%s",
3223 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3224 gw_trans->repo_folder ? "/" : "",
3225 gw_trans->repo_file) == -1) {
3226 error = got_error_from_errno("asprintf");
3227 goto done;
3230 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3231 if (error)
3232 goto done;
3234 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3235 GOT_OBJ_TYPE_COMMIT, 1, repo);
3236 if (error)
3237 goto done;
3239 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3240 if (error)
3241 goto done;
3243 if (obj_id == NULL) {
3244 error = got_error(GOT_ERR_NO_OBJ);
3245 goto done;
3248 error = got_object_get_type(&obj_type, repo, obj_id);
3249 if (error)
3250 goto done;
3252 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3253 error = got_error(GOT_ERR_OBJ_TYPE);
3254 goto done;
3257 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3258 if (error)
3259 goto done;
3261 bca.f = got_opentemp();
3262 if (bca.f == NULL) {
3263 error = got_error_from_errno("got_opentemp");
3264 goto done;
3266 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3267 &bca.line_offsets, bca.f, blob);
3268 if (error || bca.nlines == 0)
3269 goto done;
3271 /* Don't include \n at EOF in the blame line count. */
3272 if (bca.line_offsets[bca.nlines - 1] == filesize)
3273 bca.nlines--;
3275 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3276 if (bca.lines == NULL) {
3277 error = got_error_from_errno("calloc");
3278 goto done;
3280 bca.lineno_cur = 1;
3281 bca.nlines_prec = 0;
3282 i = bca.nlines;
3283 while (i > 0) {
3284 i /= 10;
3285 bca.nlines_prec++;
3287 bca.repo = repo;
3288 bca.gw_trans = gw_trans;
3290 error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
3291 NULL, NULL);
3292 if (error)
3293 goto done;
3294 done:
3295 free(bca.line_offsets);
3296 free(in_repo_path);
3297 free(commit_id);
3298 free(obj_id);
3299 free(path);
3301 for (i = 0; i < bca.nlines; i++) {
3302 struct blame_line *bline = &bca.lines[i];
3303 free(bline->id_str);
3304 free(bline->committer);
3306 free(bca.lines);
3307 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3308 error = got_error_from_errno("fclose");
3309 if (blob)
3310 got_object_blob_close(blob);
3311 if (repo)
3312 got_repo_close(repo);
3313 return error;
3316 static const struct got_error *
3317 gw_output_blob_buf(struct gw_trans *gw_trans)
3319 const struct got_error *error = NULL;
3320 struct got_repository *repo = NULL;
3321 struct got_object_id *obj_id = NULL;
3322 struct got_object_id *commit_id = NULL;
3323 struct got_blob_object *blob = NULL;
3324 char *path = NULL, *in_repo_path = NULL;
3325 int obj_type, set_mime = 0;
3326 size_t len, hdrlen;
3327 const uint8_t *buf;
3328 enum kcgi_err kerr = KCGI_OK;
3330 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3331 if (error)
3332 return error;
3334 if (asprintf(&path, "%s%s%s",
3335 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3336 gw_trans->repo_folder ? "/" : "",
3337 gw_trans->repo_file) == -1) {
3338 error = got_error_from_errno("asprintf");
3339 goto done;
3342 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3343 if (error)
3344 goto done;
3346 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3347 GOT_OBJ_TYPE_COMMIT, 1, repo);
3348 if (error)
3349 goto done;
3351 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3352 if (error)
3353 goto done;
3355 if (obj_id == NULL) {
3356 error = got_error(GOT_ERR_NO_OBJ);
3357 goto done;
3360 error = got_object_get_type(&obj_type, repo, obj_id);
3361 if (error)
3362 goto done;
3364 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3365 error = got_error(GOT_ERR_OBJ_TYPE);
3366 goto done;
3369 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3370 if (error)
3371 goto done;
3373 hdrlen = got_object_blob_get_hdrlen(blob);
3374 do {
3375 error = got_object_blob_read_block(&len, blob);
3376 if (error)
3377 goto done;
3378 buf = got_object_blob_get_read_buf(blob);
3381 * Skip blob object header first time around,
3382 * which also contains a zero byte.
3384 buf += hdrlen;
3385 if (set_mime == 0) {
3386 if (isbinary(buf, len - hdrlen))
3387 gw_trans->mime = KMIME_APP_OCTET_STREAM;
3388 else
3389 gw_trans->mime = KMIME_TEXT_PLAIN;
3390 set_mime = 1;
3391 error = gw_display_index(gw_trans);
3392 if (error)
3393 goto done;
3395 khttp_write(gw_trans->gw_req, buf, len - hdrlen);
3396 hdrlen = 0;
3397 } while (len != 0);
3398 done:
3399 free(in_repo_path);
3400 free(commit_id);
3401 free(obj_id);
3402 free(path);
3403 if (blob)
3404 got_object_blob_close(blob);
3405 if (repo)
3406 got_repo_close(repo);
3407 if (error == NULL && kerr != KCGI_OK)
3408 error = gw_kcgi_error(kerr);
3409 return error;
3412 static const struct got_error *
3413 gw_output_repo_tree(struct gw_trans *gw_trans)
3415 const struct got_error *error = NULL;
3416 struct got_repository *repo = NULL;
3417 struct got_object_id *tree_id = NULL, *commit_id = NULL;
3418 struct got_tree_object *tree = NULL;
3419 char *path = NULL, *in_repo_path = NULL;
3420 char *id_str = NULL;
3421 char *build_folder = NULL;
3422 char *href_blob = NULL, *href_blame = NULL;
3423 const char *class = NULL;
3424 int nentries, i, class_flip = 0;
3425 enum kcgi_err kerr = KCGI_OK;
3427 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3428 if (error)
3429 goto done;
3431 if (gw_trans->repo_folder != NULL)
3432 path = strdup(gw_trans->repo_folder);
3433 else {
3434 error = got_repo_map_path(&in_repo_path, repo,
3435 gw_trans->repo_path, 1);
3436 if (error)
3437 goto done;
3438 free(path);
3439 path = in_repo_path;
3442 if (gw_trans->commit == NULL) {
3443 struct got_reference *head_ref;
3444 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
3445 if (error)
3446 goto done;
3447 error = got_ref_resolve(&commit_id, repo, head_ref);
3448 if (error)
3449 goto done;
3450 got_ref_close(head_ref);
3452 } else {
3453 error = got_repo_match_object_id(&commit_id, NULL,
3454 gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
3455 if (error)
3456 goto done;
3459 error = got_object_id_str(&gw_trans->commit, commit_id);
3460 if (error)
3461 goto done;
3463 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
3464 if (error)
3465 goto done;
3467 error = got_object_open_as_tree(&tree, repo, tree_id);
3468 if (error)
3469 goto done;
3471 nentries = got_object_tree_get_nentries(tree);
3472 for (i = 0; i < nentries; i++) {
3473 struct got_tree_entry *te;
3474 const char *modestr = "";
3475 mode_t mode;
3477 te = got_object_tree_get_entry(tree, i);
3479 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
3480 if (error)
3481 goto done;
3483 mode = got_tree_entry_get_mode(te);
3484 if (got_object_tree_entry_is_submodule(te))
3485 modestr = "$";
3486 else if (S_ISLNK(mode))
3487 modestr = "@";
3488 else if (S_ISDIR(mode))
3489 modestr = "/";
3490 else if (mode & S_IXUSR)
3491 modestr = "*";
3493 if (class_flip == 0) {
3494 class = "back_lightgray";
3495 class_flip = 1;
3496 } else {
3497 class = "back_white";
3498 class_flip = 0;
3501 if (S_ISDIR(mode)) {
3502 if (asprintf(&build_folder, "%s/%s",
3503 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3504 got_tree_entry_get_name(te)) == -1) {
3505 error = got_error_from_errno(
3506 "asprintf");
3507 goto done;
3509 if (asprintf(&href_blob,
3510 "?path=%s&action=%s&commit=%s&folder=%s",
3511 gw_trans->repo_name, gw_trans->action_name,
3512 gw_trans->commit, build_folder) == -1) {
3513 error = got_error_from_errno("asprintf");
3514 goto done;
3517 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3518 KATTR_ID, "tree_wrapper", KATTR__MAX);
3519 if (kerr != KCGI_OK)
3520 goto done;
3521 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3522 KATTR_ID, "tree_line", KATTR_CLASS, class,
3523 KATTR__MAX);
3524 if (kerr != KCGI_OK)
3525 goto done;
3526 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3527 KATTR_HREF, href_blob, KATTR_CLASS,
3528 "diff_directory", KATTR__MAX);
3529 if (kerr != KCGI_OK)
3530 goto done;
3531 kerr = khtml_puts(gw_trans->gw_html_req,
3532 got_tree_entry_get_name(te));
3533 if (kerr != KCGI_OK)
3534 goto done;
3535 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3536 if (kerr != KCGI_OK)
3537 goto done;
3538 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3539 if (kerr != KCGI_OK)
3540 goto done;
3541 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3542 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
3543 KATTR__MAX);
3544 if (kerr != KCGI_OK)
3545 goto done;
3546 kerr = khtml_entity(gw_trans->gw_html_req,
3547 KENTITY_nbsp);
3548 if (kerr != KCGI_OK)
3549 goto done;
3550 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3551 if (kerr != KCGI_OK)
3552 goto done;
3553 } else {
3554 if (asprintf(&href_blob,
3555 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3556 gw_trans->repo_name, "blob", gw_trans->commit,
3557 got_tree_entry_get_name(te),
3558 gw_trans->repo_folder ?
3559 gw_trans->repo_folder : "") == -1) {
3560 error = got_error_from_errno("asprintf");
3561 goto done;
3563 if (asprintf(&href_blame,
3564 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3565 gw_trans->repo_name, "blame", gw_trans->commit,
3566 got_tree_entry_get_name(te),
3567 gw_trans->repo_folder ?
3568 gw_trans->repo_folder : "") == -1) {
3569 error = got_error_from_errno("asprintf");
3570 goto done;
3573 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3574 KATTR_ID, "tree_wrapper", KATTR__MAX);
3575 if (kerr != KCGI_OK)
3576 goto done;
3577 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3578 KATTR_ID, "tree_line", KATTR_CLASS, class,
3579 KATTR__MAX);
3580 if (kerr != KCGI_OK)
3581 goto done;
3582 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3583 KATTR_HREF, href_blob, KATTR__MAX);
3584 if (kerr != KCGI_OK)
3585 goto done;
3586 kerr = khtml_puts(gw_trans->gw_html_req,
3587 got_tree_entry_get_name(te));
3588 if (kerr != KCGI_OK)
3589 goto done;
3590 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3591 if (kerr != KCGI_OK)
3592 goto done;
3593 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3594 if (kerr != KCGI_OK)
3595 goto done;
3596 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3597 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
3598 KATTR__MAX);
3599 if (kerr != KCGI_OK)
3600 goto done;
3602 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3603 KATTR_HREF, href_blob, KATTR__MAX);
3604 if (kerr != KCGI_OK)
3605 goto done;
3606 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
3607 if (kerr != KCGI_OK)
3608 goto done;
3609 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3610 if (kerr != KCGI_OK)
3611 goto done;
3613 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3614 if (kerr != KCGI_OK)
3615 goto done;
3617 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3618 KATTR_HREF, href_blame, KATTR__MAX);
3619 if (kerr != KCGI_OK)
3620 goto done;
3621 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
3622 if (kerr != KCGI_OK)
3623 goto done;
3625 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3626 if (kerr != KCGI_OK)
3627 goto done;
3629 free(id_str);
3630 id_str = NULL;
3631 free(href_blob);
3632 href_blob = NULL;
3633 free(build_folder);
3634 build_folder = NULL;
3636 done:
3637 if (tree)
3638 got_object_tree_close(tree);
3639 if (repo)
3640 got_repo_close(repo);
3641 free(id_str);
3642 free(href_blob);
3643 free(href_blame);
3644 free(in_repo_path);
3645 free(tree_id);
3646 free(build_folder);
3647 if (error == NULL && kerr != KCGI_OK)
3648 error = gw_kcgi_error(kerr);
3649 return error;
3652 static const struct got_error *
3653 gw_get_repo_heads(char **head_html, struct gw_trans *gw_trans)
3655 const struct got_error *error = NULL;
3656 struct got_repository *repo = NULL;
3657 struct got_reflist_head refs;
3658 struct got_reflist_entry *re;
3659 char *head_row = NULL, *head_navs_disp = NULL, *age = NULL;
3660 struct buf *diffbuf = NULL;
3661 size_t newsize;
3663 *head_html = NULL;
3665 SIMPLEQ_INIT(&refs);
3667 error = buf_alloc(&diffbuf, 0);
3668 if (error)
3669 return NULL;
3671 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3672 if (error)
3673 goto done;
3675 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
3676 NULL);
3677 if (error)
3678 goto done;
3680 SIMPLEQ_FOREACH(re, &refs, entry) {
3681 char *refname;
3683 refname = strdup(got_ref_get_name(re->ref));
3684 if (refname == NULL) {
3685 error = got_error_from_errno("got_ref_to_str");
3686 goto done;
3689 if (strncmp(refname, "refs/heads/", 11) != 0) {
3690 free(refname);
3691 continue;
3694 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
3695 refname, TM_DIFF);
3696 if (error)
3697 goto done;
3699 if (asprintf(&head_navs_disp, heads_navs, gw_trans->repo_name,
3700 refname, gw_trans->repo_name, refname,
3701 gw_trans->repo_name, refname, gw_trans->repo_name,
3702 refname) == -1) {
3703 error = got_error_from_errno("asprintf");
3704 goto done;
3707 if (strncmp(refname, "refs/heads/", 11) == 0)
3708 refname += 11;
3710 if (asprintf(&head_row, heads_row, age, refname,
3711 head_navs_disp) == -1) {
3712 error = got_error_from_errno("asprintf");
3713 goto done;
3716 error = buf_puts(&newsize, diffbuf, head_row);
3718 free(head_navs_disp);
3719 free(head_row);
3722 if (buf_len(diffbuf) > 0) {
3723 error = buf_putc(diffbuf, '\0');
3724 *head_html = strdup(buf_get(diffbuf));
3725 if (*head_html == NULL)
3726 error = got_error_from_errno("strdup");
3728 done:
3729 buf_free(diffbuf);
3730 got_ref_list_free(&refs);
3731 if (repo)
3732 got_repo_close(repo);
3733 return error;
3736 static char *
3737 gw_get_site_link(struct gw_trans *gw_trans)
3739 char *link = NULL, *repo = NULL, *action = NULL;
3741 if (gw_trans->repo_name != NULL &&
3742 asprintf(&repo, " / <a href='?path=%s&action=summary'>%s</a>",
3743 gw_trans->repo_name, gw_trans->repo_name) == -1)
3744 return NULL;
3746 if (gw_trans->action_name != NULL &&
3747 asprintf(&action, " / %s", gw_trans->action_name) == -1) {
3748 free(repo);
3749 return NULL;
3752 if (asprintf(&link, site_link, GOTWEB,
3753 gw_trans->gw_conf->got_site_link,
3754 repo ? repo : "", action ? action : "") == -1) {
3755 free(repo);
3756 free(action);
3757 return NULL;
3760 free(repo);
3761 free(action);
3762 return link;
3765 static const struct got_error *
3766 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
3768 const struct got_error *error = NULL;
3769 char *color = NULL;
3770 enum kcgi_err kerr = KCGI_OK;
3772 if (strncmp(buf, "-", 1) == 0)
3773 color = "diff_minus";
3774 else if (strncmp(buf, "+", 1) == 0)
3775 color = "diff_plus";
3776 else if (strncmp(buf, "@@", 2) == 0)
3777 color = "diff_chunk_header";
3778 else if (strncmp(buf, "@@", 2) == 0)
3779 color = "diff_chunk_header";
3780 else if (strncmp(buf, "commit +", 8) == 0)
3781 color = "diff_meta";
3782 else if (strncmp(buf, "commit -", 8) == 0)
3783 color = "diff_meta";
3784 else if (strncmp(buf, "blob +", 6) == 0)
3785 color = "diff_meta";
3786 else if (strncmp(buf, "blob -", 6) == 0)
3787 color = "diff_meta";
3788 else if (strncmp(buf, "file +", 6) == 0)
3789 color = "diff_meta";
3790 else if (strncmp(buf, "file -", 6) == 0)
3791 color = "diff_meta";
3792 else if (strncmp(buf, "from:", 5) == 0)
3793 color = "diff_author";
3794 else if (strncmp(buf, "via:", 4) == 0)
3795 color = "diff_author";
3796 else if (strncmp(buf, "date:", 5) == 0)
3797 color = "diff_date";
3798 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3799 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
3800 if (error == NULL && kerr != KCGI_OK)
3801 error = gw_kcgi_error(kerr);
3802 return error;
3806 * XXX This function should not exist.
3807 * We should let khtml_puts(3) handle HTML escaping.
3809 static const struct got_error *
3810 gw_html_escape(char **escaped_html, const char *orig_html)
3812 const struct got_error *error = NULL;
3813 struct escape_pair {
3814 char c;
3815 const char *s;
3816 } esc[] = {
3817 { '>', "&gt;" },
3818 { '<', "&lt;" },
3819 { '&', "&amp;" },
3820 { '"', "&quot;" },
3821 { '\'', "&apos;" },
3822 { '\n', "<br />" },
3824 size_t orig_len, len;
3825 int i, j, x;
3827 orig_len = strlen(orig_html);
3828 len = orig_len;
3829 for (i = 0; i < orig_len; i++) {
3830 for (j = 0; j < nitems(esc); j++) {
3831 if (orig_html[i] != esc[j].c)
3832 continue;
3833 len += strlen(esc[j].s) - 1 /* escaped char */;
3837 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
3838 if (*escaped_html == NULL)
3839 return got_error_from_errno("calloc");
3841 x = 0;
3842 for (i = 0; i < orig_len; i++) {
3843 int escaped = 0;
3844 for (j = 0; j < nitems(esc); j++) {
3845 if (orig_html[i] != esc[j].c)
3846 continue;
3848 if (strlcat(*escaped_html, esc[j].s, len + 1)
3849 >= len + 1) {
3850 error = got_error(GOT_ERR_NO_SPACE);
3851 goto done;
3853 x += strlen(esc[j].s);
3854 escaped = 1;
3855 break;
3857 if (!escaped) {
3858 (*escaped_html)[x] = orig_html[i];
3859 x++;
3862 done:
3863 if (error) {
3864 free(*escaped_html);
3865 *escaped_html = NULL;
3866 } else {
3867 (*escaped_html)[x] = '\0';
3869 return error;
3872 int
3873 main(int argc, char *argv[])
3875 const struct got_error *error = NULL;
3876 struct gw_trans *gw_trans;
3877 struct gw_dir *dir = NULL, *tdir;
3878 const char *page = "index";
3879 int gw_malloc = 1;
3880 enum kcgi_err kerr;
3882 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
3883 errx(1, "malloc");
3885 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
3886 errx(1, "malloc");
3888 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
3889 errx(1, "malloc");
3891 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
3892 errx(1, "malloc");
3894 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
3895 if (kerr != KCGI_OK) {
3896 error = gw_kcgi_error(kerr);
3897 goto done;
3900 if ((gw_trans->gw_conf =
3901 malloc(sizeof(struct gotweb_conf))) == NULL) {
3902 gw_malloc = 0;
3903 error = got_error_from_errno("malloc");
3904 goto done;
3907 TAILQ_INIT(&gw_trans->gw_dirs);
3908 TAILQ_INIT(&gw_trans->gw_headers);
3910 gw_trans->page = 0;
3911 gw_trans->repos_total = 0;
3912 gw_trans->repo_path = NULL;
3913 gw_trans->commit = NULL;
3914 gw_trans->headref = strdup(GOT_REF_HEAD);
3915 gw_trans->mime = KMIME_TEXT_HTML;
3916 gw_trans->gw_tmpl->key = gw_templs;
3917 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
3918 gw_trans->gw_tmpl->arg = gw_trans;
3919 gw_trans->gw_tmpl->cb = gw_template;
3920 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
3921 if (error)
3922 goto done;
3924 error = gw_parse_querystring(gw_trans);
3925 if (error)
3926 goto done;
3928 if (gw_trans->action == GW_BLOB)
3929 error = gw_blob(gw_trans);
3930 else
3931 error = gw_display_index(gw_trans);
3932 done:
3933 if (error) {
3934 gw_trans->mime = KMIME_TEXT_PLAIN;
3935 gw_trans->action = GW_ERR;
3936 gw_display_error(gw_trans, error);
3938 if (gw_malloc) {
3939 free(gw_trans->gw_conf->got_repos_path);
3940 free(gw_trans->gw_conf->got_site_name);
3941 free(gw_trans->gw_conf->got_site_owner);
3942 free(gw_trans->gw_conf->got_site_link);
3943 free(gw_trans->gw_conf->got_logo);
3944 free(gw_trans->gw_conf->got_logo_url);
3945 free(gw_trans->gw_conf);
3946 free(gw_trans->commit);
3947 free(gw_trans->repo_path);
3948 free(gw_trans->repo_name);
3949 free(gw_trans->repo_file);
3950 free(gw_trans->action_name);
3951 free(gw_trans->headref);
3953 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
3954 free(dir->name);
3955 free(dir->description);
3956 free(dir->age);
3957 free(dir->url);
3958 free(dir->path);
3959 free(dir);
3964 khttp_free(gw_trans->gw_req);
3965 return 0;