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_get_file_blame_blob(char **,
169 struct gw_trans *);
170 static const struct got_error *gw_get_file_read_blob(char **, size_t *,
171 struct gw_trans *);
172 static const struct got_error *gw_get_repo_tree(char **, struct gw_trans *);
173 static const struct got_error *gw_get_diff(char **, struct gw_trans *,
174 struct gw_header *);
175 static const struct got_error *gw_get_repo_tags(char **, struct gw_trans *,
176 struct gw_header *, int, int);
177 static const struct got_error *gw_get_repo_heads(char **, struct gw_trans *);
178 static const struct got_error *gw_get_clone_url(char **, struct gw_trans *,
179 char *);
180 static char *gw_get_site_link(struct gw_trans *);
181 static const struct got_error *gw_html_escape(char **, const char *);
182 static const struct got_error *gw_colordiff_line(char **, char *);
184 static const struct got_error *gw_gen_commit_header(struct gw_trans *, char *,
185 char*);
186 static char *gw_gen_commit_header_old(char *, char*);
187 static char *gw_gen_diff_header(char *, char*);
188 static const struct got_error *gw_gen_author_header(struct gw_trans *,
189 const char *);
190 static char *gw_gen_author_header_old(const char *);
191 static const struct got_error *gw_gen_age_header(struct gw_trans *,
192 const char *);
193 static const struct got_error *gw_gen_committer_header(struct gw_trans *,
194 const char *);
195 static char *gw_gen_committer_header_old(const char *);
196 static char *gw_gen_commit_msg_header(char *);
197 static char *gw_gen_tree_header(char *);
199 static void gw_free_headers(struct gw_header *);
200 static const struct got_error* gw_display_open(struct gw_trans *, enum khttp,
201 enum kmime);
202 static const struct got_error* gw_display_index(struct gw_trans *);
203 static void gw_display_error(struct gw_trans *,
204 const struct got_error *);
206 static int gw_template(size_t, void *);
208 static const struct got_error* gw_get_header(struct gw_trans *,
209 struct gw_header *, int);
210 static const struct got_error* gw_get_commits(struct gw_trans *,
211 struct gw_header *, int);
212 static const struct got_error* gw_get_commit(struct gw_trans *,
213 struct gw_header *);
214 static const struct got_error* gw_apply_unveil(const char *, const char *);
215 static const struct got_error* gw_blame_cb(void *, int, int,
216 struct got_object_id *);
217 static const struct got_error* gw_load_got_paths(struct gw_trans *);
218 static const struct got_error* gw_load_got_path(struct gw_trans *,
219 struct gw_dir *);
220 static const struct got_error* gw_parse_querystring(struct gw_trans *);
222 static const struct got_error* gw_blame(struct gw_trans *);
223 static const struct got_error* gw_blob(struct gw_trans *);
224 static const struct got_error* gw_diff(struct gw_trans *);
225 static const struct got_error* gw_index(struct gw_trans *);
226 static const struct got_error* gw_commits(struct gw_trans *);
227 static const struct got_error* gw_briefs(struct gw_trans *);
228 static const struct got_error* gw_summary(struct gw_trans *);
229 static const struct got_error* gw_tree(struct gw_trans *);
230 static const struct got_error* gw_tag(struct gw_trans *);
232 struct gw_query_action {
233 unsigned int func_id;
234 const char *func_name;
235 const struct got_error *(*func_main)(struct gw_trans *);
236 char *template;
237 };
239 enum gw_query_actions {
240 GW_BLAME,
241 GW_BLOB,
242 GW_BRIEFS,
243 GW_COMMITS,
244 GW_DIFF,
245 GW_ERR,
246 GW_INDEX,
247 GW_SUMMARY,
248 GW_TAG,
249 GW_TREE,
250 };
252 static struct gw_query_action gw_query_funcs[] = {
253 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
254 { GW_BLOB, "blob", NULL, NULL },
255 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
256 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
257 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
258 { GW_ERR, NULL, NULL, "gw_tmpl/err.tmpl" },
259 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
260 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
261 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
262 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
263 };
265 static const struct got_error *
266 gw_kcgi_error(enum kcgi_err kerr)
268 if (kerr == KCGI_OK)
269 return NULL;
271 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
272 return got_error(GOT_ERR_CANCELLED);
274 if (kerr == KCGI_ENOMEM)
275 return got_error_set_errno(ENOMEM, kcgi_strerror(kerr));
277 if (kerr == KCGI_ENFILE)
278 return got_error_set_errno(ENFILE, kcgi_strerror(kerr));
280 if (kerr == KCGI_EAGAIN)
281 return got_error_set_errno(EAGAIN, kcgi_strerror(kerr));
283 if (kerr == KCGI_FORM)
284 return got_error_msg(GOT_ERR_IO, kcgi_strerror(kerr));
286 return got_error_from_errno(kcgi_strerror(kerr));
289 static const struct got_error *
290 gw_apply_unveil(const char *repo_path, const char *repo_file)
292 const struct got_error *err;
294 if (repo_path && repo_file) {
295 char *full_path;
296 if (asprintf(&full_path, "%s/%s", repo_path, repo_file) == -1)
297 return got_error_from_errno("asprintf unveil");
298 if (unveil(full_path, "r") != 0)
299 return got_error_from_errno2("unveil", full_path);
302 if (repo_path && unveil(repo_path, "r") != 0)
303 return got_error_from_errno2("unveil", repo_path);
305 if (unveil("/tmp", "rwc") != 0)
306 return got_error_from_errno2("unveil", "/tmp");
308 err = got_privsep_unveil_exec_helpers();
309 if (err != NULL)
310 return err;
312 if (unveil(NULL, NULL) != 0)
313 return got_error_from_errno("unveil");
315 return NULL;
318 static const struct got_error *
319 gw_empty_string(char **s)
321 *s = strdup("");
322 if (*s == NULL)
323 return got_error_from_errno("strdup");
324 return NULL;
327 static int
328 isbinary(const char *buf, size_t n)
330 return (memchr(buf, '\0', n) != NULL);
333 static const struct got_error *
334 gw_blame(struct gw_trans *gw_trans)
336 const struct got_error *error = NULL;
337 struct gw_header *header = NULL;
338 char *blame = NULL, *blame_html = NULL, *blame_html_disp = NULL;
339 char *age = NULL, *age_html = NULL, *escaped_commit_msg = NULL;
340 enum kcgi_err kerr;
342 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
343 NULL) == -1)
344 return got_error_from_errno("pledge");
346 if ((header = gw_init_header()) == NULL)
347 return got_error_from_errno("malloc");
349 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
350 if (error)
351 goto done;
353 error = gw_get_header(gw_trans, header, 1);
354 if (error)
355 goto done;
357 error = gw_get_file_blame_blob(&blame_html, gw_trans);
358 if (error)
359 goto done;
361 error = gw_get_time_str(&age, header->committer_time, TM_LONG);
362 if (error)
363 goto done;
364 if (asprintf(&age_html, header_age_html, age ? age : "") == -1) {
365 error = got_error_from_errno("asprintf");
366 goto done;
369 error = gw_html_escape(&escaped_commit_msg, header->commit_msg);
370 if (error)
371 goto done;
372 if (asprintf(&blame_html_disp, blame_header, age_html,
373 gw_gen_commit_msg_header(escaped_commit_msg), blame_html) == -1) {
374 error = got_error_from_errno("asprintf");
375 goto done;
378 if (asprintf(&blame, blame_wrapper, blame_html_disp) == -1) {
379 error = got_error_from_errno("asprintf");
380 goto done;
383 kerr = khttp_puts(gw_trans->gw_req, blame);
384 if (kerr != KCGI_OK)
385 error = gw_kcgi_error(kerr);
386 done:
387 got_ref_list_free(&header->refs);
388 gw_free_headers(header);
389 free(blame_html_disp);
390 free(blame_html);
391 free(blame);
392 free(escaped_commit_msg);
393 return error;
396 static const struct got_error *
397 gw_blob(struct gw_trans *gw_trans)
399 const struct got_error *error = NULL;
400 struct gw_header *header = NULL;
401 char *content = NULL;
402 size_t filesize = 0;
403 enum kcgi_err kerr;
405 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
406 NULL) == -1)
407 return got_error_from_errno("pledge");
409 if ((header = gw_init_header()) == NULL)
410 return got_error_from_errno("malloc");
412 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
413 if (error)
414 goto done;
416 error = gw_get_header(gw_trans, header, 1);
417 if (error)
418 goto done;
420 error = gw_get_file_read_blob(&content, &filesize, gw_trans);
421 if (error)
422 goto done;
424 if (isbinary(content, filesize))
425 gw_trans->mime = KMIME_APP_OCTET_STREAM;
426 else
427 gw_trans->mime = KMIME_TEXT_PLAIN;
429 error = gw_display_index(gw_trans);
430 if (error)
431 goto done;
433 kerr = khttp_write(gw_trans->gw_req, content, filesize);
434 if (kerr != KCGI_OK)
435 error = gw_kcgi_error(kerr);
436 done:
437 got_ref_list_free(&header->refs);
438 gw_free_headers(header);
439 free(content);
440 return error;
443 static const struct got_error *
444 gw_diff(struct gw_trans *gw_trans)
446 const struct got_error *error = NULL;
447 struct gw_header *header = NULL;
448 char *diff = NULL, *diff_html = NULL, *diff_html_disp = NULL;
449 char *age = NULL, *age_html = NULL, *escaped_commit_msg = NULL;
450 enum kcgi_err kerr;
452 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
453 NULL) == -1)
454 return got_error_from_errno("pledge");
456 if ((header = gw_init_header()) == NULL)
457 return got_error_from_errno("malloc");
459 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
460 if (error)
461 goto done;
463 error = gw_get_header(gw_trans, header, 1);
464 if (error)
465 goto done;
467 error = gw_get_diff(&diff_html, gw_trans, header);
468 if (error)
469 goto done;
471 error = gw_get_time_str(&age, header->committer_time, TM_LONG);
472 if (error)
473 goto done;
474 if (asprintf(&age_html, header_age_html, age ? age : "") == -1) {
475 error = got_error_from_errno("asprintf");
476 goto done;
478 error = gw_html_escape(&escaped_commit_msg, header->commit_msg);
479 if (error)
480 goto done;
481 if (asprintf(&diff_html_disp, diff_header,
482 gw_gen_diff_header(header->parent_id, header->commit_id),
483 gw_gen_commit_header_old(header->commit_id, header->refs_str),
484 gw_gen_tree_header(header->tree_id),
485 gw_gen_author_header_old(header->author),
486 gw_gen_committer_header_old(header->committer), age_html,
487 gw_gen_commit_msg_header(escaped_commit_msg),
488 diff_html ? diff_html : "") == -1) {
489 error = got_error_from_errno("asprintf");
490 goto done;
493 if (asprintf(&diff, diff_wrapper, diff_html_disp) == -1) {
494 error = got_error_from_errno("asprintf");
495 goto done;
498 kerr = khttp_puts(gw_trans->gw_req, diff);
499 if (kerr != KCGI_OK)
500 error = gw_kcgi_error(kerr);
501 done:
502 got_ref_list_free(&header->refs);
503 gw_free_headers(header);
504 free(diff_html_disp);
505 free(diff_html);
506 free(diff);
507 free(age);
508 free(age_html);
509 free(escaped_commit_msg);
510 return error;
513 static const struct got_error *
514 gw_index(struct gw_trans *gw_trans)
516 const struct got_error *error = NULL;
517 struct gw_dir *gw_dir = NULL;
518 char *html, *navs, *next, *prev;
519 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
520 enum kcgi_err kerr;
522 if (pledge("stdio rpath proc exec sendfd unveil",
523 NULL) == -1) {
524 error = got_error_from_errno("pledge");
525 return error;
528 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
529 if (error)
530 return error;
532 error = gw_load_got_paths(gw_trans);
533 if (error)
534 return error;
536 kerr = khttp_puts(gw_trans->gw_req, index_projects_header);
537 if (kerr != KCGI_OK)
538 return gw_kcgi_error(kerr);
540 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
541 if (asprintf(&html, index_projects_empty,
542 gw_trans->gw_conf->got_repos_path) == -1)
543 return got_error_from_errno("asprintf");
544 kerr = khttp_puts(gw_trans->gw_req, html);
545 if (kerr != KCGI_OK)
546 error = gw_kcgi_error(kerr);
547 free(html);
548 return error;
551 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
552 dir_c++;
554 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
555 if (gw_trans->page > 0 && (gw_trans->page *
556 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
557 prev_disp++;
558 continue;
561 prev_disp++;
563 if (error)
564 return error;
565 if(asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
566 gw_dir->name, gw_dir->name) == -1)
567 return got_error_from_errno("asprintf");
569 if (asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
570 gw_dir->description, gw_dir->owner ? gw_dir->owner : "",
571 gw_dir->age,
572 navs) == -1)
573 return got_error_from_errno("asprintf");
575 kerr = khttp_puts(gw_trans->gw_req, html);
576 free(navs);
577 free(html);
578 if (kerr != KCGI_OK)
579 return gw_kcgi_error(kerr);
581 if (gw_trans->gw_conf->got_max_repos_display == 0)
582 continue;
584 if (next_disp == gw_trans->gw_conf->got_max_repos_display) {
585 kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
586 if (kerr != KCGI_OK)
587 return gw_kcgi_error(kerr);
588 } else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
589 (gw_trans->page > 0) &&
590 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
591 prev_disp == gw_trans->repos_total)) {
592 kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
593 if (kerr != KCGI_OK)
594 return gw_kcgi_error(kerr);
597 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
598 (gw_trans->page > 0) &&
599 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
600 prev_disp == gw_trans->repos_total)) {
601 if (asprintf(&prev, nav_prev, gw_trans->page - 1) == -1)
602 return got_error_from_errno("asprintf");
603 kerr = khttp_puts(gw_trans->gw_req, prev);
604 free(prev);
605 if (kerr != KCGI_OK)
606 return gw_kcgi_error(kerr);
609 kerr = khttp_puts(gw_trans->gw_req, div_end);
610 if (kerr != KCGI_OK)
611 return gw_kcgi_error(kerr);
613 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
614 next_disp == gw_trans->gw_conf->got_max_repos_display &&
615 dir_c != (gw_trans->page + 1) *
616 gw_trans->gw_conf->got_max_repos_display) {
617 if (asprintf(&next, nav_next, gw_trans->page + 1) == -1)
618 return got_error_from_errno("calloc");
619 kerr = khttp_puts(gw_trans->gw_req, next);
620 free(next);
621 if (kerr != KCGI_OK)
622 return gw_kcgi_error(kerr);
623 kerr = khttp_puts(gw_trans->gw_req, div_end);
624 if (kerr != KCGI_OK)
625 error = gw_kcgi_error(kerr);
626 next_disp = 0;
627 break;
630 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
631 (gw_trans->page > 0) &&
632 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
633 prev_disp == gw_trans->repos_total)) {
634 kerr = khttp_puts(gw_trans->gw_req, div_end);
635 if (kerr != KCGI_OK)
636 return gw_kcgi_error(kerr);
639 next_disp++;
641 return error;
644 static const struct got_error *
645 gw_commits(struct gw_trans *gw_trans)
647 const struct got_error *error = NULL;
648 struct gw_header *header = NULL, *n_header = NULL;
649 char *age = NULL, *age_html = NULL, *escaped_commit_msg = NULL;
650 char *href_diff = NULL, *href_tree = NULL;
651 enum kcgi_err kerr = KCGI_OK;
653 if ((header = gw_init_header()) == NULL)
654 return got_error_from_errno("malloc");
656 if (pledge("stdio rpath proc exec sendfd unveil",
657 NULL) == -1) {
658 error = got_error_from_errno("pledge");
659 goto done;
662 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
663 if (error)
664 goto done;
666 error = gw_get_header(gw_trans, header,
667 gw_trans->gw_conf->got_max_commits_display);
668 if (error)
669 goto done;
671 /* commit briefs header */
672 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
673 "commits_title_wrapper", KATTR__MAX);
674 if (kerr != KCGI_OK)
675 goto done;
676 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
677 "commits_title", KATTR__MAX);
678 if (kerr != KCGI_OK)
679 goto done;
680 kerr = khtml_puts(gw_trans->gw_html_req, "Commits");
681 if (kerr != KCGI_OK)
682 goto done;
683 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
684 if (kerr != KCGI_OK)
685 goto done;
687 /* commit content */
688 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
689 "commits_content", KATTR__MAX);
690 if (kerr != KCGI_OK)
691 goto done;
692 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
693 /* commit line */
694 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
695 "commits_line_wrapper", KATTR__MAX);
696 if (kerr != KCGI_OK)
697 goto done;
698 error = gw_gen_commit_header(gw_trans, n_header->commit_id,
699 n_header->refs_str);
700 error = gw_gen_author_header(gw_trans, n_header->author);
701 if (error)
702 goto done;
703 error = gw_gen_committer_header(gw_trans, n_header->author);
704 if (error)
705 goto done;
706 error = gw_get_time_str(&age, n_header->committer_time,
707 TM_LONG);
708 if (error)
709 goto done;
710 error = gw_gen_age_header(gw_trans, age ?age : "");
711 if (error)
712 goto done;
713 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
714 if (kerr != KCGI_OK)
715 goto done;
717 /* dotted line */
718 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
719 "dotted_line", KATTR__MAX);
720 if (kerr != KCGI_OK)
721 goto done;
722 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
723 if (kerr != KCGI_OK)
724 goto done;
726 /* commit */
727 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
728 "commit", KATTR__MAX);
729 if (kerr != KCGI_OK)
730 goto done;
731 /*
732 * XXX: keeping this for now, since kcgihtml does not convert
733 * \n into <br /> yet.
734 */
735 error = gw_html_escape(&escaped_commit_msg,
736 n_header->commit_msg);
737 if (error)
738 goto done;
739 kerr = khttp_puts(gw_trans->gw_req, escaped_commit_msg);
740 if (kerr != KCGI_OK)
741 goto done;
742 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
743 if (kerr != KCGI_OK)
744 goto done;
746 /* navs */
748 /* XXX: create gen code for this */
749 /* build diff nav */
750 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
751 gw_trans->repo_name, n_header->commit_id) == -1) {
752 error = got_error_from_errno("asprintf");
753 goto done;
755 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
756 KATTR_ID, "navs_wrapper", KATTR__MAX);
757 if (kerr != KCGI_OK)
758 goto done;
759 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
760 KATTR_ID, "navs", KATTR__MAX);
761 if (kerr != KCGI_OK)
762 goto done;
763 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
764 KATTR_HREF, href_diff, KATTR__MAX);
765 if (kerr != KCGI_OK)
766 goto done;
767 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
768 if (kerr != KCGI_OK)
769 goto done;
770 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
771 if (kerr != KCGI_OK)
772 goto done;
774 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
775 if (kerr != KCGI_OK)
776 goto done;
778 /* XXX: create gen code for this */
779 /* build tree nav */
780 if (asprintf(&href_tree, "?path=%s&action=tree&commit=%s",
781 gw_trans->repo_name, n_header->commit_id) == -1) {
782 error = got_error_from_errno("asprintf");
783 goto done;
785 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
786 KATTR_HREF, href_tree, KATTR__MAX);
787 if (kerr != KCGI_OK)
788 goto done;
789 khtml_puts(gw_trans->gw_html_req, "tree");
790 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
791 if (kerr != KCGI_OK)
792 goto done;
793 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
794 if (kerr != KCGI_OK)
795 goto done;
797 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
798 "solid_line", KATTR__MAX);
799 if (kerr != KCGI_OK)
800 goto done;
801 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
802 if (kerr != KCGI_OK)
803 goto done;
805 free(age);
806 age = NULL;
807 free(age_html);
808 age_html = NULL;
809 free(escaped_commit_msg);
810 escaped_commit_msg = NULL;
812 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
813 done:
814 got_ref_list_free(&header->refs);
815 gw_free_headers(header);
816 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
817 gw_free_headers(n_header);
818 free(age);
819 free(age_html);
820 free(href_diff);
821 free(href_tree);
822 free(escaped_commit_msg);
823 if (error == NULL && kerr != KCGI_OK)
824 error = gw_kcgi_error(kerr);
825 return error;
828 static const struct got_error *
829 gw_briefs(struct gw_trans *gw_trans)
831 const struct got_error *error = NULL;
832 struct gw_header *header = NULL, *n_header = NULL;
833 char *age = NULL, *age_html = NULL;
834 char *href_diff = NULL, *href_tree = NULL;
835 char *newline, *smallerthan;
836 enum kcgi_err kerr = KCGI_OK;
838 if ((header = gw_init_header()) == NULL)
839 return got_error_from_errno("malloc");
841 if (pledge("stdio rpath proc exec sendfd unveil",
842 NULL) == -1) {
843 error = got_error_from_errno("pledge");
844 goto done;
847 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
848 if (error)
849 goto done;
851 if (gw_trans->action == GW_SUMMARY)
852 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
853 else
854 error = gw_get_header(gw_trans, header,
855 gw_trans->gw_conf->got_max_commits_display);
856 if (error)
857 goto done;
859 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
860 error = gw_get_time_str(&age, n_header->committer_time,
861 TM_DIFF);
862 if (error)
863 goto done;
865 /* briefs wrapper */
866 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
867 KATTR_ID, "briefs_wrapper", KATTR__MAX);
868 if (kerr != KCGI_OK)
869 goto done;
871 /* briefs age */
872 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
873 KATTR_ID, "briefs_age", KATTR__MAX);
874 if (kerr != KCGI_OK)
875 goto done;
876 if (asprintf(&age_html, "%s", age ? age : "") == -1) {
877 error = got_error_from_errno("asprintf");
878 goto done;
880 kerr = khtml_puts(gw_trans->gw_html_req, age_html);
881 if (kerr != KCGI_OK)
882 goto done;
883 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
884 if (kerr != KCGI_OK)
885 goto done;
887 /* briefs author */
888 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
889 KATTR_ID, "briefs_author", KATTR__MAX);
890 if (kerr != KCGI_OK)
891 goto done;
892 smallerthan = strchr(n_header->author, '<');
893 if (smallerthan)
894 *smallerthan = '\0';
895 kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
896 if (kerr != KCGI_OK)
897 goto done;
898 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
899 if (kerr != KCGI_OK)
900 goto done;
902 /* briefs log */
903 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
904 gw_trans->repo_name, n_header->commit_id) == -1) {
905 error = got_error_from_errno("asprintf");
906 goto done;
908 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
909 KATTR_ID, "briefs_log", KATTR__MAX);
910 if (kerr != KCGI_OK)
911 goto done;
912 newline = strchr(n_header->commit_msg, '\n');
913 if (newline)
914 *newline = '\0';
915 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
916 KATTR_HREF, href_diff, KATTR__MAX);
917 if (kerr != KCGI_OK)
918 goto done;
919 kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
920 if (kerr != KCGI_OK)
921 goto done;
922 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
923 if (kerr != KCGI_OK)
924 goto done;
926 /* build diff nav */
927 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
928 KATTR_ID, "navs_wrapper", KATTR__MAX);
929 if (kerr != KCGI_OK)
930 goto done;
931 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
932 KATTR_ID, "navs", KATTR__MAX);
933 if (kerr != KCGI_OK)
934 goto done;
935 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
936 KATTR_HREF, href_diff, KATTR__MAX);
937 if (kerr != KCGI_OK)
938 goto done;
939 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
940 if (kerr != KCGI_OK)
941 goto done;
942 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
943 if (kerr != KCGI_OK)
944 goto done;
946 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
947 if (kerr != KCGI_OK)
948 goto done;
950 /* build tree nav */
951 if (asprintf(&href_tree, "?path=%s&action=tree&commit=%s",
952 gw_trans->repo_name, n_header->commit_id) == -1) {
953 error = got_error_from_errno("asprintf");
954 goto done;
956 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
957 KATTR_HREF, href_tree, KATTR__MAX);
958 if (kerr != KCGI_OK)
959 goto done;
960 khtml_puts(gw_trans->gw_html_req, "tree");
961 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
962 if (kerr != KCGI_OK)
963 goto done;
964 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
965 if (kerr != KCGI_OK)
966 goto done;
968 /* dotted line */
969 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
970 KATTR_ID, "dotted_line", KATTR__MAX);
971 if (kerr != KCGI_OK)
972 goto done;
973 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
974 if (kerr != KCGI_OK)
975 goto done;
977 free(age);
978 age = NULL;
979 free(age_html);
980 age_html = NULL;
981 free(href_diff);
982 href_diff = NULL;
983 free(href_tree);
984 href_tree = NULL;
986 done:
987 got_ref_list_free(&header->refs);
988 gw_free_headers(header);
989 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
990 gw_free_headers(n_header);
991 free(age);
992 free(age_html);
993 free(href_diff);
994 free(href_tree);
995 if (error == NULL && kerr != KCGI_OK)
996 error = gw_kcgi_error(kerr);
997 return error;
1000 static const struct got_error *
1001 gw_summary(struct gw_trans *gw_trans)
1003 const struct got_error *error = NULL;
1004 char *age = NULL, *tags = NULL, *heads = NULL;
1005 enum kcgi_err kerr = KCGI_OK;
1007 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1008 return got_error_from_errno("pledge");
1010 /* unveil is applied with gw_briefs below */
1012 /* summary wrapper */
1013 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1014 "summary_wrapper", KATTR__MAX);
1015 if (kerr != KCGI_OK)
1016 return gw_kcgi_error(kerr);
1018 /* description */
1019 if (gw_trans->gw_conf->got_show_repo_description &&
1020 gw_trans->gw_dir->description != NULL &&
1021 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1022 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1023 KATTR_ID, "description_title", KATTR__MAX);
1024 if (kerr != KCGI_OK)
1025 goto done;
1026 kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1027 if (kerr != KCGI_OK)
1028 goto done;
1029 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1030 if (kerr != KCGI_OK)
1031 goto done;
1032 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1033 KATTR_ID, "description", KATTR__MAX);
1034 if (kerr != KCGI_OK)
1035 goto done;
1036 kerr = khtml_puts(gw_trans->gw_html_req,
1037 gw_trans->gw_dir->description);
1038 if (kerr != KCGI_OK)
1039 goto done;
1040 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1041 if (kerr != KCGI_OK)
1042 goto done;
1045 /* repo owner */
1046 if (gw_trans->gw_conf->got_show_repo_owner &&
1047 gw_trans->gw_dir->owner != NULL) {
1048 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1049 KATTR_ID, "repo_owner_title", KATTR__MAX);
1050 if (kerr != KCGI_OK)
1051 goto done;
1052 kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1053 if (kerr != KCGI_OK)
1054 goto done;
1055 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1056 if (kerr != KCGI_OK)
1057 goto done;
1058 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1059 KATTR_ID, "repo_owner", KATTR__MAX);
1060 if (kerr != KCGI_OK)
1061 goto done;
1062 kerr = khtml_puts(gw_trans->gw_html_req,
1063 gw_trans->gw_dir->owner);
1064 if (kerr != KCGI_OK)
1065 goto done;
1066 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1067 if (kerr != KCGI_OK)
1068 goto done;
1071 /* last change */
1072 if (gw_trans->gw_conf->got_show_repo_age) {
1073 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1074 "refs/heads", TM_LONG);
1075 if (error)
1076 goto done;
1077 if (age != NULL) {
1078 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1079 KATTR_ID, "last_change_title", KATTR__MAX);
1080 if (kerr != KCGI_OK)
1081 goto done;
1082 kerr = khtml_puts(gw_trans->gw_html_req,
1083 "Last Change: ");
1084 if (kerr != KCGI_OK)
1085 goto done;
1086 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1087 if (kerr != KCGI_OK)
1088 goto done;
1089 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1090 KATTR_ID, "last_change", KATTR__MAX);
1091 if (kerr != KCGI_OK)
1092 goto done;
1093 kerr = khtml_puts(gw_trans->gw_html_req, age);
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;
1102 /* cloneurl */
1103 if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1104 gw_trans->gw_dir->url != NULL &&
1105 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1106 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1107 KATTR_ID, "cloneurl_title", KATTR__MAX);
1108 if (kerr != KCGI_OK)
1109 goto done;
1110 kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1111 if (kerr != KCGI_OK)
1112 goto done;
1113 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1114 if (kerr != KCGI_OK)
1115 goto done;
1116 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1117 KATTR_ID, "cloneurl", KATTR__MAX);
1118 if (kerr != KCGI_OK)
1119 goto done;
1120 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->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;
1128 /* close summary wrapper */
1129 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1130 if (kerr != KCGI_OK)
1131 goto done;
1133 /* commit briefs header */
1134 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1135 "briefs_title_wrapper", KATTR__MAX);
1136 if (kerr != KCGI_OK)
1137 goto done;
1138 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1139 "briefs_title", KATTR__MAX);
1140 if (kerr != KCGI_OK)
1141 goto done;
1142 kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1143 if (kerr != KCGI_OK)
1144 goto done;
1145 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1146 if (kerr != KCGI_OK)
1147 goto done;
1148 error = gw_briefs(gw_trans);
1149 if (error)
1150 goto done;
1152 /* tags */
1153 error = gw_get_repo_tags(&tags, gw_trans, NULL, D_MAXSLCOMMDISP,
1154 TAGBRIEF);
1155 if (error)
1156 goto done;
1158 if (tags != NULL && strcmp(tags, "") != 0) {
1159 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1160 KATTR_ID, "summary_tags_title_wrapper", KATTR__MAX);
1161 if (kerr != KCGI_OK)
1162 goto done;
1163 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1164 KATTR_ID, "summary_tags_title", KATTR__MAX);
1165 if (kerr != KCGI_OK)
1166 goto done;
1167 kerr = khtml_puts(gw_trans->gw_html_req, "Tags");
1168 if (kerr != KCGI_OK)
1169 goto done;
1170 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1171 if (kerr != KCGI_OK)
1172 goto done;
1173 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1174 KATTR_ID, "summary_tags_content", KATTR__MAX);
1175 if (kerr != KCGI_OK)
1176 goto done;
1177 kerr = khttp_puts(gw_trans->gw_req, tags);
1178 if (kerr != KCGI_OK)
1179 goto done;
1180 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1181 if (kerr != KCGI_OK)
1182 goto done;
1185 /* heads */
1186 error = gw_get_repo_heads(&heads, gw_trans);
1187 if (error)
1188 goto done;
1189 if (heads != NULL && strcmp(heads, "") != 0) {
1190 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1191 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
1192 if (kerr != KCGI_OK)
1193 goto done;
1194 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1195 KATTR_ID, "summary_heads_title", KATTR__MAX);
1196 if (kerr != KCGI_OK)
1197 goto done;
1198 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
1199 if (kerr != KCGI_OK)
1200 goto done;
1201 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1202 if (kerr != KCGI_OK)
1203 goto done;
1204 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1205 KATTR_ID, "summary_heads_content", KATTR__MAX);
1206 if (kerr != KCGI_OK)
1207 goto done;
1208 kerr = khttp_puts(gw_trans->gw_req, heads);
1209 if (kerr != KCGI_OK)
1210 goto done;
1211 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1212 if (kerr != KCGI_OK)
1213 goto done;
1215 done:
1216 if (kerr != KCGI_OK)
1217 error = gw_kcgi_error(kerr);
1218 free(age);
1219 free(tags);
1220 free(heads);
1221 return error;
1224 static const struct got_error *
1225 gw_tree(struct gw_trans *gw_trans)
1227 const struct got_error *error = NULL;
1228 struct gw_header *header = NULL;
1229 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1230 char *age = NULL, *age_html = NULL, *escaped_commit_msg = NULL;
1231 enum kcgi_err kerr;
1233 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1234 return got_error_from_errno("pledge");
1236 if ((header = gw_init_header()) == NULL)
1237 return got_error_from_errno("malloc");
1239 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
1240 if (error)
1241 goto done;
1243 error = gw_get_header(gw_trans, header, 1);
1244 if (error)
1245 goto done;
1247 error = gw_get_repo_tree(&tree_html, gw_trans);
1248 if (error)
1249 goto done;
1251 error = gw_get_time_str(&age, header->committer_time, TM_LONG);
1252 if (error)
1253 goto done;
1254 if (asprintf(&age_html, header_age_html, age ? age : "") == -1) {
1255 error = got_error_from_errno("asprintf");
1256 goto done;
1258 error = gw_html_escape(&escaped_commit_msg, header->commit_msg);
1259 if (error)
1260 goto done;
1261 if (asprintf(&tree_html_disp, tree_header, age_html,
1262 gw_gen_commit_msg_header(escaped_commit_msg),
1263 tree_html ? tree_html : "") == -1) {
1264 error = got_error_from_errno("asprintf");
1265 goto done;
1268 if (asprintf(&tree, tree_wrapper, tree_html_disp) == -1) {
1269 error = got_error_from_errno("asprintf");
1270 goto done;
1273 kerr = khttp_puts(gw_trans->gw_req, tree);
1274 if (kerr != KCGI_OK)
1275 error = gw_kcgi_error(kerr);
1276 done:
1277 got_ref_list_free(&header->refs);
1278 gw_free_headers(header);
1279 free(tree_html_disp);
1280 free(tree_html);
1281 free(tree);
1282 free(age);
1283 free(age_html);
1284 free(escaped_commit_msg);
1285 return error;
1288 static const struct got_error *
1289 gw_tag(struct gw_trans *gw_trans)
1291 const struct got_error *error = NULL;
1292 struct gw_header *header = NULL;
1293 char *tag = NULL, *tag_html = NULL, *tag_html_disp = NULL;
1294 char *escaped_commit_msg = NULL;
1295 enum kcgi_err kerr;
1297 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1298 return got_error_from_errno("pledge");
1300 if ((header = gw_init_header()) == NULL)
1301 return got_error_from_errno("malloc");
1303 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
1304 if (error)
1305 goto done;
1307 error = gw_get_header(gw_trans, header, 1);
1308 if (error)
1309 goto done;
1311 error = gw_get_repo_tags(&tag_html, gw_trans, header, 1, TAGFULL);
1312 if (error)
1313 goto done;
1315 error = gw_html_escape(&escaped_commit_msg, header->commit_msg);
1316 if (error)
1317 goto done;
1318 if (asprintf(&tag_html_disp, tag_header,
1319 gw_gen_commit_header_old(header->commit_id, header->refs_str),
1320 gw_gen_commit_msg_header(escaped_commit_msg),
1321 tag_html ? tag_html : "") == -1) {
1322 error = got_error_from_errno("asprintf");
1323 goto done;
1326 if (asprintf(&tag, tag_wrapper, tag_html_disp) == -1) {
1327 error = got_error_from_errno("asprintf");
1328 goto done;
1331 kerr = khttp_puts(gw_trans->gw_req, tag);
1332 if (kerr != KCGI_OK)
1333 error = gw_kcgi_error(kerr);
1334 done:
1335 got_ref_list_free(&header->refs);
1336 gw_free_headers(header);
1337 free(tag_html_disp);
1338 free(tag_html);
1339 free(tag);
1340 free(escaped_commit_msg);
1341 return error;
1344 static const struct got_error *
1345 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1347 const struct got_error *error = NULL;
1348 DIR *dt;
1349 char *dir_test;
1350 int opened = 0;
1352 if (asprintf(&dir_test, "%s/%s/%s",
1353 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1354 GOTWEB_GIT_DIR) == -1)
1355 return got_error_from_errno("asprintf");
1357 dt = opendir(dir_test);
1358 if (dt == NULL) {
1359 free(dir_test);
1360 } else {
1361 gw_dir->path = strdup(dir_test);
1362 opened = 1;
1363 goto done;
1366 if (asprintf(&dir_test, "%s/%s/%s",
1367 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1368 GOTWEB_GOT_DIR) == -1)
1369 return got_error_from_errno("asprintf");
1371 dt = opendir(dir_test);
1372 if (dt == NULL)
1373 free(dir_test);
1374 else {
1375 opened = 1;
1376 error = got_error(GOT_ERR_NOT_GIT_REPO);
1377 goto errored;
1380 if (asprintf(&dir_test, "%s/%s",
1381 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1)
1382 return got_error_from_errno("asprintf");
1384 gw_dir->path = strdup(dir_test);
1386 done:
1387 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1388 gw_dir->path);
1389 if (error)
1390 goto errored;
1391 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1392 if (error)
1393 goto errored;
1394 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1395 "refs/heads", TM_DIFF);
1396 if (error)
1397 goto errored;
1398 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1399 errored:
1400 free(dir_test);
1401 if (opened)
1402 closedir(dt);
1403 return error;
1406 static const struct got_error *
1407 gw_load_got_paths(struct gw_trans *gw_trans)
1409 const struct got_error *error = NULL;
1410 DIR *d;
1411 struct dirent **sd_dent;
1412 struct gw_dir *gw_dir;
1413 struct stat st;
1414 unsigned int d_cnt, d_i;
1416 d = opendir(gw_trans->gw_conf->got_repos_path);
1417 if (d == NULL) {
1418 error = got_error_from_errno2("opendir",
1419 gw_trans->gw_conf->got_repos_path);
1420 return error;
1423 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1424 alphasort);
1425 if (d_cnt == -1) {
1426 error = got_error_from_errno2("scandir",
1427 gw_trans->gw_conf->got_repos_path);
1428 return error;
1431 for (d_i = 0; d_i < d_cnt; d_i++) {
1432 if (gw_trans->gw_conf->got_max_repos > 0 &&
1433 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1434 break; /* account for parent and self */
1436 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1437 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1438 continue;
1440 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
1441 return got_error_from_errno("gw_dir malloc");
1443 error = gw_load_got_path(gw_trans, gw_dir);
1444 if (error && error->code == GOT_ERR_NOT_GIT_REPO)
1445 continue;
1446 else if (error)
1447 return error;
1449 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1450 !got_path_dir_is_empty(gw_dir->path)) {
1451 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1452 entry);
1453 gw_trans->repos_total++;
1457 closedir(d);
1458 return error;
1461 static const struct got_error *
1462 gw_parse_querystring(struct gw_trans *gw_trans)
1464 const struct got_error *error = NULL;
1465 struct kpair *p;
1466 struct gw_query_action *action = NULL;
1467 unsigned int i;
1469 if (gw_trans->gw_req->fieldnmap[0]) {
1470 error = got_error_from_errno("bad parse");
1471 return error;
1472 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1473 /* define gw_trans->repo_path */
1474 if (asprintf(&gw_trans->repo_name, "%s", p->parsed.s) == -1)
1475 return got_error_from_errno("asprintf");
1477 if (asprintf(&gw_trans->repo_path, "%s/%s",
1478 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
1479 return got_error_from_errno("asprintf");
1481 /* get action and set function */
1482 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
1483 for (i = 0; i < nitems(gw_query_funcs); i++) {
1484 action = &gw_query_funcs[i];
1485 if (action->func_name == NULL)
1486 continue;
1488 if (strcmp(action->func_name,
1489 p->parsed.s) == 0) {
1490 gw_trans->action = i;
1491 if (asprintf(&gw_trans->action_name,
1492 "%s", action->func_name) == -1)
1493 return
1494 got_error_from_errno(
1495 "asprintf");
1497 break;
1500 action = NULL;
1503 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
1504 if (asprintf(&gw_trans->commit, "%s",
1505 p->parsed.s) == -1)
1506 return got_error_from_errno("asprintf");
1508 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1509 if (asprintf(&gw_trans->repo_file, "%s",
1510 p->parsed.s) == -1)
1511 return got_error_from_errno("asprintf");
1513 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER]))
1514 if (asprintf(&gw_trans->repo_folder, "%s",
1515 p->parsed.s) == -1)
1516 return got_error_from_errno("asprintf");
1518 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
1519 if (asprintf(&gw_trans->headref, "%s",
1520 p->parsed.s) == -1)
1521 return got_error_from_errno("asprintf");
1523 if (action == NULL) {
1524 error = got_error_from_errno("invalid action");
1525 return error;
1527 if ((gw_trans->gw_dir =
1528 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
1529 return got_error_from_errno("gw_dir malloc");
1531 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1532 if (error)
1533 return error;
1534 } else
1535 gw_trans->action = GW_INDEX;
1537 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1538 gw_trans->page = p->parsed.i;
1540 return error;
1543 static struct gw_dir *
1544 gw_init_gw_dir(char *dir)
1546 struct gw_dir *gw_dir;
1548 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
1549 return NULL;
1551 if (asprintf(&gw_dir->name, "%s", dir) == -1)
1552 return NULL;
1554 return gw_dir;
1557 static const struct got_error *
1558 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1560 enum kcgi_err kerr;
1562 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1563 if (kerr != KCGI_OK)
1564 return gw_kcgi_error(kerr);
1565 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1566 khttps[code]);
1567 if (kerr != KCGI_OK)
1568 return gw_kcgi_error(kerr);
1569 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1570 kmimetypes[mime]);
1571 if (kerr != KCGI_OK)
1572 return gw_kcgi_error(kerr);
1573 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
1574 "nosniff");
1575 if (kerr != KCGI_OK)
1576 return gw_kcgi_error(kerr);
1577 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1578 if (kerr != KCGI_OK)
1579 return gw_kcgi_error(kerr);
1580 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
1581 "1; mode=block");
1582 if (kerr != KCGI_OK)
1583 return gw_kcgi_error(kerr);
1585 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
1586 kerr = khttp_head(gw_trans->gw_req,
1587 kresps[KRESP_CONTENT_DISPOSITION],
1588 "attachment; filename=%s", gw_trans->repo_file);
1589 if (kerr != KCGI_OK)
1590 return gw_kcgi_error(kerr);
1593 kerr = khttp_body(gw_trans->gw_req);
1594 return gw_kcgi_error(kerr);
1597 static const struct got_error *
1598 gw_display_index(struct gw_trans *gw_trans)
1600 const struct got_error *error;
1601 enum kcgi_err kerr;
1603 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1604 if (error)
1605 return error;
1607 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, KHTML_PRETTY);
1608 if (kerr)
1609 return gw_kcgi_error(kerr);
1611 if (gw_trans->action != GW_BLOB) {
1612 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1613 gw_query_funcs[gw_trans->action].template);
1614 if (kerr != KCGI_OK) {
1615 khtml_close(gw_trans->gw_html_req);
1616 return gw_kcgi_error(kerr);
1620 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
1623 static void
1624 gw_display_error(struct gw_trans *gw_trans, const struct got_error *err)
1626 if (gw_display_open(gw_trans, KHTTP_200, gw_trans->mime) != NULL)
1627 return;
1629 if (khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0) != KCGI_OK)
1630 return;
1631 khtml_puts(gw_trans->gw_html_req, err->msg);
1632 khtml_close(gw_trans->gw_html_req);
1635 static int
1636 gw_template(size_t key, void *arg)
1638 const struct got_error *error = NULL;
1639 enum kcgi_err kerr;
1640 struct gw_trans *gw_trans = arg;
1641 char *gw_site_link, *img_src = NULL;
1643 switch (key) {
1644 case (TEMPL_HEAD):
1645 kerr = khttp_puts(gw_trans->gw_req, head);
1646 if (kerr != KCGI_OK)
1647 return 0;
1648 break;
1649 case(TEMPL_HEADER):
1650 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1651 KATTR_ID, "got_link", KATTR__MAX);
1652 if (kerr != KCGI_OK)
1653 return 0;
1654 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1655 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
1656 KATTR_TARGET, "_sotd", KATTR__MAX);
1657 if (kerr != KCGI_OK)
1658 return 0;
1659 if (asprintf(&img_src, "/%s",
1660 gw_trans->gw_conf->got_logo) == -1)
1661 return 0;
1662 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
1663 KATTR_SRC, img_src, KATTR__MAX);
1664 if (kerr != KCGI_OK) {
1665 free(img_src);
1666 return 0;
1668 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1669 if (kerr != KCGI_OK) {
1670 free(img_src);
1671 return 0;
1673 break;
1674 case (TEMPL_SITEPATH):
1675 gw_site_link = gw_get_site_link(gw_trans);
1676 if (gw_site_link != NULL) {
1677 kerr = khttp_puts(gw_trans->gw_req, gw_site_link);
1678 if (kerr != KCGI_OK) {
1679 free(gw_site_link);
1680 return 0;
1683 free(gw_site_link);
1684 break;
1685 case(TEMPL_TITLE):
1686 if (gw_trans->gw_conf->got_site_name != NULL) {
1687 kerr = khtml_puts(gw_trans->gw_html_req,
1688 gw_trans->gw_conf->got_site_name);
1689 if (kerr != KCGI_OK)
1690 return 0;
1692 break;
1693 case (TEMPL_SEARCH):
1694 kerr = khttp_puts(gw_trans->gw_req, search);
1695 if (kerr != KCGI_OK)
1696 return 0;
1697 break;
1698 case(TEMPL_SITEOWNER):
1699 if (gw_trans->gw_conf->got_site_owner != NULL &&
1700 gw_trans->gw_conf->got_show_site_owner) {
1701 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1702 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
1703 if (kerr != KCGI_OK)
1704 return 0;
1705 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1706 KATTR_ID, "site_owner", KATTR__MAX);
1707 if (kerr != KCGI_OK)
1708 return 0;
1709 kerr = khtml_puts(gw_trans->gw_html_req,
1710 gw_trans->gw_conf->got_site_owner);
1711 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1712 if (kerr != KCGI_OK)
1713 return 0;
1715 break;
1716 case(TEMPL_CONTENT):
1717 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1718 if (error) {
1719 kerr = khttp_puts(gw_trans->gw_req, error->msg);
1720 if (kerr != KCGI_OK)
1721 return 0;
1723 break;
1724 default:
1725 return 0;
1727 return 1;
1730 static char *
1731 gw_gen_commit_header_old(char *str1, char *str2)
1733 char *return_html = NULL, *ref_str = NULL;
1735 if (strcmp(str2, "") != 0) {
1736 if (asprintf(&ref_str, "(%s)", str2) == -1) {
1737 return_html = strdup("");
1738 return return_html;
1740 } else
1741 ref_str = strdup("");
1743 if (asprintf(&return_html, header_commit_html, str1, ref_str) == -1)
1744 return_html = strdup("");
1746 free(ref_str);
1747 return return_html;
1750 static const struct got_error *
1751 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
1753 const struct got_error *error = NULL;
1754 char *ref_str = NULL;
1755 enum kcgi_err kerr = KCGI_OK;
1757 if (strcmp(str2, "") != 0) {
1758 if (asprintf(&ref_str, "(%s)", str2) == -1) {
1759 error = got_error_from_errno("asprintf");
1760 goto done;
1762 } else
1763 ref_str = strdup("");
1765 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1766 KATTR_ID, "header_commit_title", KATTR__MAX);
1767 if (kerr != KCGI_OK)
1768 goto done;
1769 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
1770 if (kerr != KCGI_OK)
1771 goto done;
1772 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1773 if (kerr != KCGI_OK)
1774 goto done;
1775 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1776 KATTR_ID, "header_commit", KATTR__MAX);
1777 if (kerr != KCGI_OK)
1778 goto done;
1779 kerr = khtml_puts(gw_trans->gw_html_req, str1);
1780 if (kerr != KCGI_OK)
1781 goto done;
1782 kerr = khtml_puts(gw_trans->gw_html_req, " ");
1783 if (kerr != KCGI_OK)
1784 goto done;
1785 kerr = khtml_puts(gw_trans->gw_html_req, ref_str);
1786 if (kerr != KCGI_OK)
1787 goto done;
1788 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1789 if (kerr != KCGI_OK)
1790 goto done;
1791 done:
1792 if (error == NULL && kerr != KCGI_OK)
1793 error = gw_kcgi_error(kerr);
1794 return error;
1797 static char *
1798 gw_gen_diff_header(char *str1, char *str2)
1800 char *return_html = NULL;
1802 if (asprintf(&return_html, header_diff_html, str1, str2) == -1)
1803 return_html = strdup("");
1805 return return_html;
1808 /* XXX: slated for deletion */
1809 static char *
1810 gw_gen_author_header_old(const char *str)
1812 const struct got_error *error = NULL;
1813 char *return_html = NULL;
1814 char *escaped_html = NULL;
1816 error = gw_html_escape(&escaped_html, str);
1817 if (error)
1818 return strdup("");
1819 if (asprintf(&return_html, header_author_html, escaped_html) == -1)
1820 return_html = strdup("");
1822 free(escaped_html);
1823 return return_html;
1826 static const struct got_error *
1827 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
1829 const struct got_error *error = NULL;
1830 enum kcgi_err kerr = KCGI_OK;
1832 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1833 KATTR_ID, "header_age_title", KATTR__MAX);
1834 if (kerr != KCGI_OK)
1835 goto done;
1836 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
1837 if (kerr != KCGI_OK)
1838 goto done;
1839 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1840 if (kerr != KCGI_OK)
1841 goto done;
1842 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1843 KATTR_ID, "header_age", KATTR__MAX);
1844 if (kerr != KCGI_OK)
1845 goto done;
1846 kerr = khtml_puts(gw_trans->gw_html_req, str);
1847 if (kerr != KCGI_OK)
1848 goto done;
1849 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1850 if (kerr != KCGI_OK)
1851 goto done;
1852 done:
1853 if (error == NULL && kerr != KCGI_OK)
1854 error = gw_kcgi_error(kerr);
1855 return error;
1858 static const struct got_error *
1859 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
1861 const struct got_error *error = NULL;
1862 enum kcgi_err kerr;
1864 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1865 KATTR_ID, "header_author_title", KATTR__MAX);
1866 if (kerr != KCGI_OK)
1867 goto done;
1868 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
1869 if (kerr != KCGI_OK)
1870 goto done;
1871 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1872 if (kerr != KCGI_OK)
1873 goto done;
1874 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1875 KATTR_ID, "header_author", KATTR__MAX);
1876 if (kerr != KCGI_OK)
1877 goto done;
1878 kerr = khtml_puts(gw_trans->gw_html_req, str);
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 done:
1885 if (error == NULL && kerr != KCGI_OK)
1886 error = gw_kcgi_error(kerr);
1887 return error;
1890 static const struct got_error *
1891 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
1893 const struct got_error *error = NULL;
1894 enum kcgi_err kerr;
1896 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1897 KATTR_ID, "header_committer_title", KATTR__MAX);
1898 if (kerr != KCGI_OK)
1899 goto done;
1900 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
1901 if (kerr != KCGI_OK)
1902 goto done;
1903 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1904 if (kerr != KCGI_OK)
1905 goto done;
1906 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1907 KATTR_ID, "header_committer", KATTR__MAX);
1908 if (kerr != KCGI_OK)
1909 goto done;
1910 kerr = khtml_puts(gw_trans->gw_html_req, str);
1911 if (kerr != KCGI_OK)
1912 goto done;
1913 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1914 if (kerr != KCGI_OK)
1915 goto done;
1916 done:
1917 if (error == NULL && kerr != KCGI_OK)
1918 error = gw_kcgi_error(kerr);
1919 return error;
1922 /* XXX: slated for deletion */
1923 static char *
1924 gw_gen_committer_header_old(const char *str)
1926 const struct got_error *error = NULL;
1927 char *return_html = NULL;
1928 char *escaped_html = NULL;
1930 error = gw_html_escape(&escaped_html, str);
1931 if (error)
1932 return strdup("");
1933 if (asprintf(&return_html, header_committer_html, escaped_html) == -1)
1934 return_html = strdup("");
1936 free(escaped_html);
1937 return return_html;
1940 static char *
1941 gw_gen_commit_msg_header(char *str)
1943 char *return_html = NULL;
1945 if (asprintf(&return_html, header_commit_msg_html, str) == -1)
1946 return_html = strdup("");
1948 return return_html;
1951 static char *
1952 gw_gen_tree_header(char *str)
1954 char *return_html = NULL;
1956 if (asprintf(&return_html, header_tree_html, str) == -1)
1957 return_html = strdup("");
1959 return return_html;
1962 static const struct got_error *
1963 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
1964 char *dir)
1966 const struct got_error *error = NULL;
1967 FILE *f = NULL;
1968 char *d_file = NULL;
1969 unsigned int len;
1970 size_t n;
1972 *description = NULL;
1973 if (gw_trans->gw_conf->got_show_repo_description == 0)
1974 return gw_empty_string(description);
1976 if (asprintf(&d_file, "%s/description", dir) == -1)
1977 return got_error_from_errno("asprintf");
1979 f = fopen(d_file, "r");
1980 if (f == NULL) {
1981 if (errno == ENOENT || errno == EACCES)
1982 return gw_empty_string(description);
1983 error = got_error_from_errno2("fopen", d_file);
1984 goto done;
1987 if (fseek(f, 0, SEEK_END) == -1) {
1988 error = got_ferror(f, GOT_ERR_IO);
1989 goto done;
1991 len = ftell(f);
1992 if (len == -1) {
1993 error = got_ferror(f, GOT_ERR_IO);
1994 goto done;
1996 if (fseek(f, 0, SEEK_SET) == -1) {
1997 error = got_ferror(f, GOT_ERR_IO);
1998 goto done;
2000 *description = calloc(len + 1, sizeof(**description));
2001 if (*description == NULL) {
2002 error = got_error_from_errno("calloc");
2003 goto done;
2006 n = fread(*description, 1, len, f);
2007 if (n == 0 && ferror(f))
2008 error = got_ferror(f, GOT_ERR_IO);
2009 done:
2010 if (f != NULL && fclose(f) == -1 && error == NULL)
2011 error = got_error_from_errno("fclose");
2012 free(d_file);
2013 return error;
2016 static const struct got_error *
2017 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2019 struct tm tm;
2020 time_t diff_time;
2021 char *years = "years ago", *months = "months ago";
2022 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2023 char *minutes = "minutes ago", *seconds = "seconds ago";
2024 char *now = "right now";
2025 char *s;
2026 char datebuf[29];
2028 *repo_age = NULL;
2030 switch (ref_tm) {
2031 case TM_DIFF:
2032 diff_time = time(NULL) - committer_time;
2033 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2034 if (asprintf(repo_age, "%lld %s",
2035 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2036 return got_error_from_errno("asprintf");
2037 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2038 if (asprintf(repo_age, "%lld %s",
2039 (diff_time / 60 / 60 / 24 / (365 / 12)),
2040 months) == -1)
2041 return got_error_from_errno("asprintf");
2042 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2043 if (asprintf(repo_age, "%lld %s",
2044 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2045 return got_error_from_errno("asprintf");
2046 } else if (diff_time > 60 * 60 * 24 * 2) {
2047 if (asprintf(repo_age, "%lld %s",
2048 (diff_time / 60 / 60 / 24), days) == -1)
2049 return got_error_from_errno("asprintf");
2050 } else if (diff_time > 60 * 60 * 2) {
2051 if (asprintf(repo_age, "%lld %s",
2052 (diff_time / 60 / 60), hours) == -1)
2053 return got_error_from_errno("asprintf");
2054 } else if (diff_time > 60 * 2) {
2055 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2056 minutes) == -1)
2057 return got_error_from_errno("asprintf");
2058 } else if (diff_time > 2) {
2059 if (asprintf(repo_age, "%lld %s", diff_time,
2060 seconds) == -1)
2061 return got_error_from_errno("asprintf");
2062 } else {
2063 if (asprintf(repo_age, "%s", now) == -1)
2064 return got_error_from_errno("asprintf");
2066 break;
2067 case TM_LONG:
2068 if (gmtime_r(&committer_time, &tm) == NULL)
2069 return got_error_from_errno("gmtime_r");
2071 s = asctime_r(&tm, datebuf);
2072 if (s == NULL)
2073 return got_error_from_errno("asctime_r");
2075 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2076 return got_error_from_errno("asprintf");
2077 break;
2079 return NULL;
2082 static const struct got_error *
2083 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2084 char *repo_ref, int ref_tm)
2086 const struct got_error *error = NULL;
2087 struct got_object_id *id = NULL;
2088 struct got_repository *repo = NULL;
2089 struct got_commit_object *commit = NULL;
2090 struct got_reflist_head refs;
2091 struct got_reflist_entry *re;
2092 struct got_reference *head_ref;
2093 int is_head = 0;
2094 time_t committer_time = 0, cmp_time = 0;
2095 const char *refname;
2097 *repo_age = NULL;
2098 SIMPLEQ_INIT(&refs);
2100 if (repo_ref == NULL)
2101 return NULL;
2103 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
2104 is_head = 1;
2106 if (gw_trans->gw_conf->got_show_repo_age == 0)
2107 return NULL;
2109 error = got_repo_open(&repo, dir, NULL);
2110 if (error)
2111 goto done;
2113 if (is_head)
2114 error = got_ref_list(&refs, repo, "refs/heads",
2115 got_ref_cmp_by_name, NULL);
2116 else
2117 error = got_ref_list(&refs, repo, repo_ref,
2118 got_ref_cmp_by_name, NULL);
2119 if (error)
2120 goto done;
2122 SIMPLEQ_FOREACH(re, &refs, entry) {
2123 if (is_head)
2124 refname = strdup(repo_ref);
2125 else
2126 refname = got_ref_get_name(re->ref);
2127 error = got_ref_open(&head_ref, repo, refname, 0);
2128 if (error)
2129 goto done;
2131 error = got_ref_resolve(&id, repo, head_ref);
2132 got_ref_close(head_ref);
2133 if (error)
2134 goto done;
2136 error = got_object_open_as_commit(&commit, repo, id);
2137 if (error)
2138 goto done;
2140 committer_time =
2141 got_object_commit_get_committer_time(commit);
2143 if (cmp_time < committer_time)
2144 cmp_time = committer_time;
2147 if (cmp_time != 0) {
2148 committer_time = cmp_time;
2149 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2151 done:
2152 got_ref_list_free(&refs);
2153 free(id);
2154 return error;
2157 static const struct got_error *
2158 gw_get_diff(char **diff_html, struct gw_trans *gw_trans,
2159 struct gw_header *header)
2161 const struct got_error *error;
2162 FILE *f = NULL;
2163 struct got_object_id *id1 = NULL, *id2 = NULL;
2164 struct buf *diffbuf = NULL;
2165 char *label1 = NULL, *label2 = NULL, *line = NULL;
2166 char *diff_line_html = NULL;
2167 int obj_type;
2168 size_t newsize, linesize = 0;
2169 ssize_t linelen;
2171 f = got_opentemp();
2172 if (f == NULL)
2173 return NULL;
2175 error = buf_alloc(&diffbuf, 0);
2176 if (error)
2177 goto done;
2179 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
2180 if (error)
2181 goto done;
2183 if (strncmp(header->parent_id, "/dev/null", 9) != 0) {
2184 error = got_repo_match_object_id(&id1, &label1,
2185 header->parent_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2186 if (error)
2187 goto done;
2190 error = got_repo_match_object_id(&id2, &label2,
2191 header->commit_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2192 if (error)
2193 goto done;
2195 error = got_object_get_type(&obj_type, header->repo, id2);
2196 if (error)
2197 goto done;
2198 switch (obj_type) {
2199 case GOT_OBJ_TYPE_BLOB:
2200 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
2201 header->repo, f);
2202 break;
2203 case GOT_OBJ_TYPE_TREE:
2204 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
2205 header->repo, f);
2206 break;
2207 case GOT_OBJ_TYPE_COMMIT:
2208 error = got_diff_objects_as_commits(id1, id2, 3, 0,
2209 header->repo, f);
2210 break;
2211 default:
2212 error = got_error(GOT_ERR_OBJ_TYPE);
2214 if (error)
2215 goto done;
2217 if (fseek(f, 0, SEEK_SET) == -1) {
2218 error = got_ferror(f, GOT_ERR_IO);
2219 goto done;
2222 while ((linelen = getline(&line, &linesize, f)) != -1) {
2223 char *escaped_line;
2224 error = gw_html_escape(&escaped_line, line);
2225 if (error)
2226 goto done;
2228 error = gw_colordiff_line(&diff_line_html, escaped_line);
2229 if (error)
2230 goto done;
2232 error = buf_puts(&newsize, diffbuf, diff_line_html);
2233 if (error)
2234 goto done;
2236 error = buf_puts(&newsize, diffbuf, div_end);
2237 if (error)
2238 goto done;
2240 if (linelen == -1 && ferror(f)) {
2241 error = got_error_from_errno("getline");
2242 goto done;
2245 if (buf_len(diffbuf) > 0) {
2246 error = buf_putc(diffbuf, '\0');
2247 if (error)
2248 goto done;
2249 *diff_html = strdup(buf_get(diffbuf));
2250 if (*diff_html == NULL)
2251 error = got_error_from_errno("strdup");
2253 done:
2254 if (f && fclose(f) == -1 && error == NULL)
2255 error = got_error_from_errno("fclose");
2256 free(diff_line_html);
2257 free(line);
2258 free(diffbuf);
2259 free(label1);
2260 free(label2);
2261 free(id1);
2262 free(id2);
2264 return error;
2267 static const struct got_error *
2268 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2270 const struct got_error *error = NULL;
2271 struct got_repository *repo;
2272 const char *gitconfig_owner;
2274 *owner = NULL;
2276 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2277 return NULL;
2279 error = got_repo_open(&repo, dir, NULL);
2280 if (error)
2281 return error;
2282 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2283 if (gitconfig_owner) {
2284 *owner = strdup(gitconfig_owner);
2285 if (*owner == NULL)
2286 error = got_error_from_errno("strdup");
2288 got_repo_close(repo);
2289 return error;
2292 static const struct got_error *
2293 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2295 const struct got_error *error = NULL;
2296 FILE *f;
2297 char *d_file = NULL;
2298 unsigned int len;
2299 size_t n;
2301 *url = NULL;
2303 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2304 return got_error_from_errno("asprintf");
2306 f = fopen(d_file, "r");
2307 if (f == NULL) {
2308 if (errno != ENOENT && errno != EACCES)
2309 error = got_error_from_errno2("fopen", d_file);
2310 goto done;
2313 if (fseek(f, 0, SEEK_END) == -1) {
2314 error = got_ferror(f, GOT_ERR_IO);
2315 goto done;
2317 len = ftell(f);
2318 if (len == -1) {
2319 error = got_ferror(f, GOT_ERR_IO);
2320 goto done;
2322 if (fseek(f, 0, SEEK_SET) == -1) {
2323 error = got_ferror(f, GOT_ERR_IO);
2324 goto done;
2327 *url = calloc(len + 1, sizeof(**url));
2328 if (*url == NULL) {
2329 error = got_error_from_errno("calloc");
2330 goto done;
2333 n = fread(*url, 1, len, f);
2334 if (n == 0 && ferror(f))
2335 error = got_ferror(f, GOT_ERR_IO);
2336 done:
2337 if (f && fclose(f) == -1 && error == NULL)
2338 error = got_error_from_errno("fclose");
2339 free(d_file);
2340 return NULL;
2343 static const struct got_error *
2344 gw_get_repo_tags(char **tag_html, struct gw_trans *gw_trans,
2345 struct gw_header *header, int limit, int tag_type)
2347 const struct got_error *error = NULL;
2348 struct got_repository *repo = NULL;
2349 struct got_reflist_head refs;
2350 struct got_reflist_entry *re;
2351 char *tag_row = NULL, *tags_navs_disp = NULL;
2352 char *age = NULL, *age_html = NULL, *newline;
2353 char *escaped_tagger = NULL, *escaped_tag_commit = NULL;
2354 char *id_str = NULL, *refstr = NULL;
2355 char *tag_commit0 = NULL;
2356 struct buf *diffbuf = NULL;
2357 size_t newsize;
2358 struct got_tag_object *tag = NULL;
2360 *tag_html = NULL;
2362 SIMPLEQ_INIT(&refs);
2364 error = buf_alloc(&diffbuf, 0);
2365 if (error)
2366 return NULL;
2368 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2369 if (error)
2370 goto done;
2372 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
2373 if (error)
2374 goto done;
2376 SIMPLEQ_FOREACH(re, &refs, entry) {
2377 const char *refname;
2378 const char *tagger;
2379 const char *tag_commit;
2380 time_t tagger_time;
2381 struct got_object_id *id;
2383 refname = got_ref_get_name(re->ref);
2384 if (strncmp(refname, "refs/tags/", 10) != 0)
2385 continue;
2386 refname += 10;
2387 refstr = got_ref_to_str(re->ref);
2388 if (refstr == NULL) {
2389 error = got_error_from_errno("got_ref_to_str");
2390 goto done;
2393 error = got_ref_resolve(&id, repo, re->ref);
2394 if (error)
2395 goto done;
2396 error = got_object_open_as_tag(&tag, repo, id);
2397 free(id);
2398 if (error)
2399 goto done;
2401 tagger = got_object_tag_get_tagger(tag);
2402 tagger_time = got_object_tag_get_tagger_time(tag);
2404 error = got_object_id_str(&id_str,
2405 got_object_tag_get_object_id(tag));
2406 if (error)
2407 goto done;
2409 tag_commit0 = strdup(got_object_tag_get_message(tag));
2410 if (tag_commit0 == NULL) {
2411 error = got_error_from_errno("strdup");
2412 goto done;
2415 tag_commit = tag_commit0;
2416 while (*tag_commit == '\n')
2417 tag_commit++;
2419 switch (tag_type) {
2420 case TAGBRIEF:
2421 newline = strchr(tag_commit, '\n');
2422 if (newline)
2423 *newline = '\0';
2425 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
2426 if (error)
2427 goto done;
2429 if (asprintf(&tags_navs_disp, tags_navs,
2430 gw_trans->repo_name, id_str, gw_trans->repo_name,
2431 id_str, gw_trans->repo_name, id_str,
2432 gw_trans->repo_name, id_str) == -1) {
2433 error = got_error_from_errno("asprintf");
2434 goto done;
2437 if (asprintf(&tag_row, tags_row, age ? age : "",
2438 refname, tag_commit, tags_navs_disp) == -1) {
2439 error = got_error_from_errno("asprintf");
2440 goto done;
2443 free(tags_navs_disp);
2444 break;
2445 case TAGFULL:
2446 error = gw_get_time_str(&age, tagger_time, TM_LONG);
2447 if (error)
2448 goto done;
2449 error = gw_html_escape(&escaped_tagger, tagger);
2450 if (error)
2451 goto done;
2452 error = gw_html_escape(&escaped_tag_commit, tag_commit);
2453 if (error)
2454 goto done;
2455 if (asprintf(&tag_row, tag_info, age ? age : "",
2456 escaped_tagger, escaped_tag_commit) == -1) {
2457 error = got_error_from_errno("asprintf");
2458 goto done;
2460 break;
2461 default:
2462 break;
2465 error = buf_puts(&newsize, diffbuf, tag_row);
2466 if (error)
2467 goto done;
2469 if (limit && --limit == 0)
2470 break;
2472 got_object_tag_close(tag);
2473 tag = NULL;
2474 free(id_str);
2475 id_str = NULL;
2476 free(refstr);
2477 refstr = NULL;
2478 free(age);
2479 age = NULL;
2480 free(age_html);
2481 age_html = NULL;
2482 free(escaped_tagger);
2483 escaped_tagger = NULL;
2484 free(escaped_tag_commit);
2485 escaped_tag_commit = NULL;
2486 free(tag_commit0);
2487 tag_commit0 = NULL;
2488 free(tag_row);
2489 tag_row = NULL;
2492 if (buf_len(diffbuf) > 0) {
2493 error = buf_putc(diffbuf, '\0');
2494 *tag_html = strdup(buf_get(diffbuf));
2495 if (*tag_html == NULL)
2496 error = got_error_from_errno("strdup");
2498 done:
2499 if (tag)
2500 got_object_tag_close(tag);
2501 free(id_str);
2502 free(refstr);
2503 free(age);
2504 free(age_html);
2505 free(escaped_tagger);
2506 free(escaped_tag_commit);
2507 free(tag_commit0);
2508 free(tag_row);
2509 buf_free(diffbuf);
2510 got_ref_list_free(&refs);
2511 if (repo)
2512 got_repo_close(repo);
2513 return error;
2516 static void
2517 gw_free_headers(struct gw_header *header)
2519 free(header->id);
2520 free(header->path);
2521 if (header->commit != NULL)
2522 got_object_commit_close(header->commit);
2523 if (header->repo)
2524 got_repo_close(header->repo);
2525 free(header->refs_str);
2526 free(header->commit_id);
2527 free(header->parent_id);
2528 free(header->tree_id);
2529 free(header->commit_msg);
2532 static struct gw_header *
2533 gw_init_header()
2535 struct gw_header *header;
2537 header = malloc(sizeof(*header));
2538 if (header == NULL)
2539 return NULL;
2541 header->repo = NULL;
2542 header->commit = NULL;
2543 header->id = NULL;
2544 header->path = NULL;
2545 SIMPLEQ_INIT(&header->refs);
2547 return header;
2550 static const struct got_error *
2551 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
2552 int limit)
2554 const struct got_error *error = NULL;
2555 struct got_commit_graph *graph = NULL;
2557 error = got_commit_graph_open(&graph, header->path, 0);
2558 if (error)
2559 goto done;
2561 error = got_commit_graph_iter_start(graph, header->id, header->repo,
2562 NULL, NULL);
2563 if (error)
2564 goto done;
2566 for (;;) {
2567 error = got_commit_graph_iter_next(&header->id, graph,
2568 header->repo, NULL, NULL);
2569 if (error) {
2570 if (error->code == GOT_ERR_ITER_COMPLETED)
2571 error = NULL;
2572 goto done;
2574 if (header->id == NULL)
2575 goto done;
2577 error = got_object_open_as_commit(&header->commit, header->repo,
2578 header->id);
2579 if (error)
2580 goto done;
2582 error = gw_get_commit(gw_trans, header);
2583 if (limit > 1) {
2584 struct gw_header *n_header = NULL;
2585 if ((n_header = gw_init_header()) == NULL) {
2586 error = got_error_from_errno("malloc");
2587 goto done;
2590 n_header->refs_str = strdup(header->refs_str);
2591 n_header->commit_id = strdup(header->commit_id);
2592 n_header->parent_id = strdup(header->parent_id);
2593 n_header->tree_id = strdup(header->tree_id);
2594 n_header->author = strdup(header->author);
2595 n_header->committer = strdup(header->committer);
2596 n_header->commit_msg = strdup(header->commit_msg);
2597 n_header->committer_time = header->committer_time;
2598 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
2599 entry);
2601 if (error || (limit && --limit == 0))
2602 break;
2604 done:
2605 if (graph)
2606 got_commit_graph_close(graph);
2607 return error;
2610 static const struct got_error *
2611 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
2613 const struct got_error *error = NULL;
2614 struct got_reflist_entry *re;
2615 struct got_object_id *id2 = NULL;
2616 struct got_object_qid *parent_id;
2617 char *refs_str = NULL, *commit_msg = NULL, *commit_msg0;
2619 /*print commit*/
2620 SIMPLEQ_FOREACH(re, &header->refs, entry) {
2621 char *s;
2622 const char *name;
2623 struct got_tag_object *tag = NULL;
2624 int cmp;
2626 name = got_ref_get_name(re->ref);
2627 if (strcmp(name, GOT_REF_HEAD) == 0)
2628 continue;
2629 if (strncmp(name, "refs/", 5) == 0)
2630 name += 5;
2631 if (strncmp(name, "got/", 4) == 0)
2632 continue;
2633 if (strncmp(name, "heads/", 6) == 0)
2634 name += 6;
2635 if (strncmp(name, "remotes/", 8) == 0)
2636 name += 8;
2637 if (strncmp(name, "tags/", 5) == 0) {
2638 error = got_object_open_as_tag(&tag, header->repo,
2639 re->id);
2640 if (error) {
2641 if (error->code != GOT_ERR_OBJ_TYPE)
2642 continue;
2644 * Ref points at something other
2645 * than a tag.
2647 error = NULL;
2648 tag = NULL;
2651 cmp = got_object_id_cmp(tag ?
2652 got_object_tag_get_object_id(tag) : re->id, header->id);
2653 if (tag)
2654 got_object_tag_close(tag);
2655 if (cmp != 0)
2656 continue;
2657 s = refs_str;
2658 if (asprintf(&refs_str, "%s%s%s", s ? s : "",
2659 s ? ", " : "", name) == -1) {
2660 error = got_error_from_errno("asprintf");
2661 free(s);
2662 return error;
2664 header->refs_str = strdup(refs_str);
2665 free(s);
2668 if (refs_str == NULL)
2669 header->refs_str = strdup("");
2670 free(refs_str);
2672 error = got_object_id_str(&header->commit_id, header->id);
2673 if (error)
2674 return error;
2676 error = got_object_id_str(&header->tree_id,
2677 got_object_commit_get_tree_id(header->commit));
2678 if (error)
2679 return error;
2681 if (gw_trans->action == GW_DIFF) {
2682 parent_id = SIMPLEQ_FIRST(
2683 got_object_commit_get_parent_ids(header->commit));
2684 if (parent_id != NULL) {
2685 id2 = got_object_id_dup(parent_id->id);
2686 free (parent_id);
2687 error = got_object_id_str(&header->parent_id, id2);
2688 if (error)
2689 return error;
2690 free(id2);
2691 } else
2692 header->parent_id = strdup("/dev/null");
2693 } else
2694 header->parent_id = strdup("");
2696 header->committer_time =
2697 got_object_commit_get_committer_time(header->commit);
2699 header->author =
2700 got_object_commit_get_author(header->commit);
2701 header->committer =
2702 got_object_commit_get_committer(header->commit);
2703 if (error)
2704 return error;
2706 /* XXX Doesn't the log message require escaping? */
2707 error = got_object_commit_get_logmsg(&commit_msg0, header->commit);
2708 if (error)
2709 return error;
2711 commit_msg = commit_msg0;
2712 while (*commit_msg == '\n')
2713 commit_msg++;
2715 header->commit_msg = strdup(commit_msg);
2716 free(commit_msg0);
2717 return error;
2720 static const struct got_error *
2721 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
2723 const struct got_error *error = NULL;
2724 char *in_repo_path = NULL;
2726 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
2727 if (error)
2728 return error;
2730 if (gw_trans->commit == NULL) {
2731 struct got_reference *head_ref;
2732 error = got_ref_open(&head_ref, header->repo,
2733 gw_trans->headref, 0);
2734 if (error)
2735 return error;
2737 error = got_ref_resolve(&header->id, header->repo, head_ref);
2738 got_ref_close(head_ref);
2739 if (error)
2740 return error;
2742 error = got_object_open_as_commit(&header->commit,
2743 header->repo, header->id);
2744 } else {
2745 struct got_reference *ref;
2746 error = got_ref_open(&ref, header->repo, gw_trans->commit, 0);
2747 if (error == NULL) {
2748 int obj_type;
2749 error = got_ref_resolve(&header->id, header->repo, ref);
2750 got_ref_close(ref);
2751 if (error)
2752 return error;
2753 error = got_object_get_type(&obj_type, header->repo,
2754 header->id);
2755 if (error)
2756 return error;
2757 if (obj_type == GOT_OBJ_TYPE_TAG) {
2758 struct got_tag_object *tag;
2759 error = got_object_open_as_tag(&tag,
2760 header->repo, header->id);
2761 if (error)
2762 return error;
2763 if (got_object_tag_get_object_type(tag) !=
2764 GOT_OBJ_TYPE_COMMIT) {
2765 got_object_tag_close(tag);
2766 error = got_error(GOT_ERR_OBJ_TYPE);
2767 return error;
2769 free(header->id);
2770 header->id = got_object_id_dup(
2771 got_object_tag_get_object_id(tag));
2772 if (header->id == NULL)
2773 error = got_error_from_errno(
2774 "got_object_id_dup");
2775 got_object_tag_close(tag);
2776 if (error)
2777 return error;
2778 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2779 error = got_error(GOT_ERR_OBJ_TYPE);
2780 return error;
2782 error = got_object_open_as_commit(&header->commit,
2783 header->repo, header->id);
2784 if (error)
2785 return error;
2787 if (header->commit == NULL) {
2788 error = got_repo_match_object_id_prefix(&header->id,
2789 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2790 header->repo);
2791 if (error)
2792 return error;
2794 error = got_repo_match_object_id_prefix(&header->id,
2795 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2796 header->repo);
2799 error = got_repo_map_path(&in_repo_path, header->repo,
2800 gw_trans->repo_path, 1);
2801 if (error)
2802 return error;
2804 if (in_repo_path) {
2805 header->path = strdup(in_repo_path);
2807 free(in_repo_path);
2809 error = got_ref_list(&header->refs, header->repo, NULL,
2810 got_ref_cmp_by_name, NULL);
2811 if (error)
2812 return error;
2814 error = gw_get_commits(gw_trans, header, limit);
2815 return error;
2818 struct blame_line {
2819 int annotated;
2820 char *id_str;
2821 char *committer;
2822 char datebuf[11]; /* YYYY-MM-DD + NUL */
2825 struct gw_blame_cb_args {
2826 struct blame_line *lines;
2827 int nlines;
2828 int nlines_prec;
2829 int lineno_cur;
2830 off_t *line_offsets;
2831 FILE *f;
2832 struct got_repository *repo;
2833 struct gw_trans *gw_trans;
2834 struct buf *blamebuf;
2837 static const struct got_error *
2838 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2840 const struct got_error *err = NULL;
2841 struct gw_blame_cb_args *a = arg;
2842 struct blame_line *bline;
2843 char *line = NULL;
2844 size_t linesize = 0, newsize;
2845 struct got_commit_object *commit = NULL;
2846 off_t offset;
2847 struct tm tm;
2848 time_t committer_time;
2850 if (nlines != a->nlines ||
2851 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2852 return got_error(GOT_ERR_RANGE);
2854 if (lineno == -1)
2855 return NULL; /* no change in this commit */
2857 /* Annotate this line. */
2858 bline = &a->lines[lineno - 1];
2859 if (bline->annotated)
2860 return NULL;
2861 err = got_object_id_str(&bline->id_str, id);
2862 if (err)
2863 return err;
2865 err = got_object_open_as_commit(&commit, a->repo, id);
2866 if (err)
2867 goto done;
2869 bline->committer = strdup(got_object_commit_get_committer(commit));
2870 if (bline->committer == NULL) {
2871 err = got_error_from_errno("strdup");
2872 goto done;
2875 committer_time = got_object_commit_get_committer_time(commit);
2876 if (localtime_r(&committer_time, &tm) == NULL)
2877 return got_error_from_errno("localtime_r");
2878 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2879 &tm) >= sizeof(bline->datebuf)) {
2880 err = got_error(GOT_ERR_NO_SPACE);
2881 goto done;
2883 bline->annotated = 1;
2885 /* Print lines annotated so far. */
2886 bline = &a->lines[a->lineno_cur - 1];
2887 if (!bline->annotated)
2888 goto done;
2890 offset = a->line_offsets[a->lineno_cur - 1];
2891 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2892 err = got_error_from_errno("fseeko");
2893 goto done;
2896 while (bline->annotated) {
2897 char *smallerthan, *at, *nl, *committer, *blame_row = NULL,
2898 *line_escape = NULL;
2899 size_t len;
2901 if (getline(&line, &linesize, a->f) == -1) {
2902 if (ferror(a->f))
2903 err = got_error_from_errno("getline");
2904 break;
2907 committer = bline->committer;
2908 smallerthan = strchr(committer, '<');
2909 if (smallerthan && smallerthan[1] != '\0')
2910 committer = smallerthan + 1;
2911 at = strchr(committer, '@');
2912 if (at)
2913 *at = '\0';
2914 len = strlen(committer);
2915 if (len >= 9)
2916 committer[8] = '\0';
2918 nl = strchr(line, '\n');
2919 if (nl)
2920 *nl = '\0';
2922 err = gw_html_escape(&line_escape, line);
2923 if (err)
2924 goto err;
2926 if (a->gw_trans->repo_folder == NULL)
2927 a->gw_trans->repo_folder = strdup("");
2928 if (a->gw_trans->repo_folder == NULL)
2929 goto err;
2930 asprintf(&blame_row, blame_line, a->nlines_prec, a->lineno_cur,
2931 a->gw_trans->repo_name, bline->id_str,
2932 a->gw_trans->repo_file, a->gw_trans->repo_folder,
2933 bline->id_str, bline->datebuf, committer, line_escape);
2934 a->lineno_cur++;
2935 err = buf_puts(&newsize, a->blamebuf, blame_row);
2936 if (err)
2937 return err;
2939 bline = &a->lines[a->lineno_cur - 1];
2940 err:
2941 free(line_escape);
2942 free(blame_row);
2944 done:
2945 if (commit)
2946 got_object_commit_close(commit);
2947 free(line);
2948 return err;
2951 static const struct got_error *
2952 gw_get_file_blame_blob(char **blame_html, struct gw_trans *gw_trans)
2954 const struct got_error *error = NULL;
2955 struct got_repository *repo = NULL;
2956 struct got_object_id *obj_id = NULL;
2957 struct got_object_id *commit_id = NULL;
2958 struct got_blob_object *blob = NULL;
2959 char *path = NULL, *in_repo_path = NULL;
2960 struct gw_blame_cb_args bca;
2961 int i, obj_type;
2962 size_t filesize;
2964 *blame_html = NULL;
2966 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2967 if (error)
2968 return error;
2970 if (asprintf(&path, "%s%s%s",
2971 gw_trans->repo_folder ? gw_trans->repo_folder : "",
2972 gw_trans->repo_folder ? "/" : "",
2973 gw_trans->repo_file) == -1) {
2974 error = got_error_from_errno("asprintf");
2975 goto done;
2978 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2979 if (error)
2980 goto done;
2982 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
2983 GOT_OBJ_TYPE_COMMIT, 1, repo);
2984 if (error)
2985 goto done;
2987 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2988 if (error)
2989 goto done;
2991 if (obj_id == NULL) {
2992 error = got_error(GOT_ERR_NO_OBJ);
2993 goto done;
2996 error = got_object_get_type(&obj_type, repo, obj_id);
2997 if (error)
2998 goto done;
3000 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3001 error = got_error(GOT_ERR_OBJ_TYPE);
3002 goto done;
3005 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3006 if (error)
3007 goto done;
3009 error = buf_alloc(&bca.blamebuf, 0);
3010 if (error)
3011 goto done;
3013 bca.f = got_opentemp();
3014 if (bca.f == NULL) {
3015 error = got_error_from_errno("got_opentemp");
3016 goto done;
3018 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3019 &bca.line_offsets, bca.f, blob);
3020 if (error || bca.nlines == 0)
3021 goto done;
3023 /* Don't include \n at EOF in the blame line count. */
3024 if (bca.line_offsets[bca.nlines - 1] == filesize)
3025 bca.nlines--;
3027 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3028 if (bca.lines == NULL) {
3029 error = got_error_from_errno("calloc");
3030 goto done;
3032 bca.lineno_cur = 1;
3033 bca.nlines_prec = 0;
3034 i = bca.nlines;
3035 while (i > 0) {
3036 i /= 10;
3037 bca.nlines_prec++;
3039 bca.repo = repo;
3040 bca.gw_trans = gw_trans;
3042 error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
3043 NULL, NULL);
3044 if (error)
3045 goto done;
3046 if (buf_len(bca.blamebuf) > 0) {
3047 error = buf_putc(bca.blamebuf, '\0');
3048 if (error)
3049 goto done;
3050 *blame_html = strdup(buf_get(bca.blamebuf));
3051 if (*blame_html == NULL) {
3052 error = got_error_from_errno("strdup");
3053 goto done;
3056 done:
3057 free(bca.line_offsets);
3058 free(bca.blamebuf);
3059 free(in_repo_path);
3060 free(commit_id);
3061 free(obj_id);
3062 free(path);
3064 for (i = 0; i < bca.nlines; i++) {
3065 struct blame_line *bline = &bca.lines[i];
3066 free(bline->id_str);
3067 free(bline->committer);
3069 free(bca.lines);
3070 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3071 error = got_error_from_errno("fclose");
3072 if (blob)
3073 got_object_blob_close(blob);
3074 if (repo)
3075 got_repo_close(repo);
3076 return error;
3079 static const struct got_error *
3080 gw_get_file_read_blob(char **blobstr, size_t *filesize, struct gw_trans *gw_trans)
3082 const struct got_error *error = NULL;
3083 struct got_repository *repo = NULL;
3084 struct got_object_id *obj_id = NULL;
3085 struct got_object_id *commit_id = NULL;
3086 struct got_blob_object *blob = NULL;
3087 char *path = NULL, *in_repo_path = NULL;
3088 int obj_type;
3089 size_t n;
3090 FILE *f = NULL;
3092 *blobstr = NULL;
3093 *filesize = 0;
3095 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3096 if (error)
3097 return error;
3099 if (asprintf(&path, "%s%s%s",
3100 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3101 gw_trans->repo_folder ? "/" : "",
3102 gw_trans->repo_file) == -1) {
3103 error = got_error_from_errno("asprintf");
3104 goto done;
3107 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3108 if (error)
3109 goto done;
3111 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3112 GOT_OBJ_TYPE_COMMIT, 1, repo);
3113 if (error)
3114 goto done;
3116 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3117 if (error)
3118 goto done;
3120 if (obj_id == NULL) {
3121 error = got_error(GOT_ERR_NO_OBJ);
3122 goto done;
3125 error = got_object_get_type(&obj_type, repo, obj_id);
3126 if (error)
3127 goto done;
3129 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3130 error = got_error(GOT_ERR_OBJ_TYPE);
3131 goto done;
3134 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3135 if (error)
3136 goto done;
3138 f = got_opentemp();
3139 if (f == NULL) {
3140 error = got_error_from_errno("got_opentemp");
3141 goto done;
3143 error = got_object_blob_dump_to_file(filesize, NULL, NULL, f, blob);
3144 if (error)
3145 goto done;
3147 /* XXX This will fail on large files... */
3148 *blobstr = calloc(*filesize + 1, sizeof(**blobstr));
3149 if (*blobstr == NULL) {
3150 error = got_error_from_errno("calloc");
3151 goto done;
3154 n = fread(*blobstr, 1, *filesize, f);
3155 if (n == 0) {
3156 if (ferror(f))
3157 error = got_ferror(f, GOT_ERR_IO);
3158 goto done;
3160 done:
3161 free(in_repo_path);
3162 free(commit_id);
3163 free(obj_id);
3164 free(path);
3165 if (blob)
3166 got_object_blob_close(blob);
3167 if (repo)
3168 got_repo_close(repo);
3169 if (f != NULL && fclose(f) == -1 && error == NULL)
3170 error = got_error_from_errno("fclose");
3171 if (error) {
3172 free(*blobstr);
3173 *blobstr = NULL;
3174 *filesize = 0;
3176 return error;
3179 static const struct got_error *
3180 gw_get_repo_tree(char **tree_html, struct gw_trans *gw_trans)
3182 const struct got_error *error = NULL;
3183 struct got_repository *repo = NULL;
3184 struct got_object_id *tree_id = NULL, *commit_id = NULL;
3185 struct got_tree_object *tree = NULL;
3186 struct buf *diffbuf = NULL;
3187 size_t newsize;
3188 char *path = NULL, *in_repo_path = NULL, *tree_row = NULL;
3189 char *id_str = NULL;
3190 char *build_folder = NULL;
3191 char *url_html = NULL;
3192 const char *class = NULL;
3193 int nentries, i, class_flip = 0;
3195 *tree_html = NULL;
3197 error = buf_alloc(&diffbuf, 0);
3198 if (error)
3199 return error;
3201 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3202 if (error)
3203 goto done;
3205 if (gw_trans->repo_folder != NULL)
3206 path = strdup(gw_trans->repo_folder);
3207 else {
3208 error = got_repo_map_path(&in_repo_path, repo,
3209 gw_trans->repo_path, 1);
3210 if (error)
3211 goto done;
3212 free(path);
3213 path = in_repo_path;
3216 if (gw_trans->commit == NULL) {
3217 struct got_reference *head_ref;
3218 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
3219 if (error)
3220 goto done;
3221 error = got_ref_resolve(&commit_id, repo, head_ref);
3222 if (error)
3223 goto done;
3224 got_ref_close(head_ref);
3226 } else {
3227 error = got_repo_match_object_id(&commit_id, NULL,
3228 gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
3229 if (error)
3230 goto done;
3233 error = got_object_id_str(&gw_trans->commit, commit_id);
3234 if (error)
3235 goto done;
3237 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
3238 if (error)
3239 goto done;
3241 error = got_object_open_as_tree(&tree, repo, tree_id);
3242 if (error)
3243 goto done;
3245 nentries = got_object_tree_get_nentries(tree);
3246 for (i = 0; i < nentries; i++) {
3247 struct got_tree_entry *te;
3248 const char *modestr = "";
3249 mode_t mode;
3251 te = got_object_tree_get_entry(tree, i);
3253 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
3254 if (error)
3255 goto done;
3257 mode = got_tree_entry_get_mode(te);
3258 if (got_object_tree_entry_is_submodule(te))
3259 modestr = "$";
3260 else if (S_ISLNK(mode))
3261 modestr = "@";
3262 else if (S_ISDIR(mode))
3263 modestr = "/";
3264 else if (mode & S_IXUSR)
3265 modestr = "*";
3267 if (class_flip == 0) {
3268 class = "back_lightgray";
3269 class_flip = 1;
3270 } else {
3271 class = "back_white";
3272 class_flip = 0;
3275 if (S_ISDIR(mode)) {
3276 if (asprintf(&build_folder, "%s%s%s",
3277 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3278 gw_trans->repo_folder ? "/" : "",
3279 got_tree_entry_get_name(te)) == -1) {
3280 error = got_error_from_errno(
3281 "asprintf");
3282 goto done;
3285 if (asprintf(&url_html, folder_html,
3286 gw_trans->repo_name, gw_trans->action_name,
3287 gw_trans->commit, build_folder,
3288 got_tree_entry_get_name(te), modestr) == -1) {
3289 error = got_error_from_errno("asprintf");
3290 goto done;
3292 if (asprintf(&tree_row, tree_line, class, url_html,
3293 class) == -1) {
3294 error = got_error_from_errno("asprintf");
3295 goto done;
3297 } else {
3298 if (asprintf(&url_html, file_html, gw_trans->repo_name,
3299 "blob", gw_trans->commit,
3300 got_tree_entry_get_name(te),
3301 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3302 got_tree_entry_get_name(te), modestr) == -1) {
3303 error = got_error_from_errno("asprintf");
3304 goto done;
3307 if (asprintf(&tree_row, tree_line_with_navs, class,
3308 url_html, class, gw_trans->repo_name, "blob",
3309 gw_trans->commit, got_tree_entry_get_name(te),
3310 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3311 "blob", gw_trans->repo_name,
3312 "blame", gw_trans->commit,
3313 got_tree_entry_get_name(te),
3314 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3315 "blame") == -1) {
3316 error = got_error_from_errno("asprintf");
3317 goto done;
3321 error = buf_puts(&newsize, diffbuf, tree_row);
3322 if (error)
3323 goto done;
3325 free(id_str);
3326 id_str = NULL;
3327 free(url_html);
3328 url_html = NULL;
3329 free(tree_row);
3330 tree_row = NULL;
3331 free(build_folder);
3332 build_folder = NULL;
3335 if (buf_len(diffbuf) > 0) {
3336 error = buf_putc(diffbuf, '\0');
3337 if (error)
3338 goto done;
3339 *tree_html = strdup(buf_get(diffbuf));
3340 if (*tree_html == NULL) {
3341 error = got_error_from_errno("strdup");
3342 goto done;
3345 done:
3346 if (tree)
3347 got_object_tree_close(tree);
3348 if (repo)
3349 got_repo_close(repo);
3351 free(id_str);
3352 free(url_html);
3353 free(tree_row);
3354 free(in_repo_path);
3355 free(tree_id);
3356 free(diffbuf);
3357 free(build_folder);
3358 return error;
3361 static const struct got_error *
3362 gw_get_repo_heads(char **head_html, struct gw_trans *gw_trans)
3364 const struct got_error *error = NULL;
3365 struct got_repository *repo = NULL;
3366 struct got_reflist_head refs;
3367 struct got_reflist_entry *re;
3368 char *head_row = NULL, *head_navs_disp = NULL, *age = NULL;
3369 struct buf *diffbuf = NULL;
3370 size_t newsize;
3372 *head_html = NULL;
3374 SIMPLEQ_INIT(&refs);
3376 error = buf_alloc(&diffbuf, 0);
3377 if (error)
3378 return NULL;
3380 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3381 if (error)
3382 goto done;
3384 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
3385 NULL);
3386 if (error)
3387 goto done;
3389 SIMPLEQ_FOREACH(re, &refs, entry) {
3390 char *refname;
3392 refname = strdup(got_ref_get_name(re->ref));
3393 if (refname == NULL) {
3394 error = got_error_from_errno("got_ref_to_str");
3395 goto done;
3398 if (strncmp(refname, "refs/heads/", 11) != 0) {
3399 free(refname);
3400 continue;
3403 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
3404 refname, TM_DIFF);
3405 if (error)
3406 goto done;
3408 if (asprintf(&head_navs_disp, heads_navs, gw_trans->repo_name,
3409 refname, gw_trans->repo_name, refname,
3410 gw_trans->repo_name, refname, gw_trans->repo_name,
3411 refname) == -1) {
3412 error = got_error_from_errno("asprintf");
3413 goto done;
3416 if (strncmp(refname, "refs/heads/", 11) == 0)
3417 refname += 11;
3419 if (asprintf(&head_row, heads_row, age, refname,
3420 head_navs_disp) == -1) {
3421 error = got_error_from_errno("asprintf");
3422 goto done;
3425 error = buf_puts(&newsize, diffbuf, head_row);
3427 free(head_navs_disp);
3428 free(head_row);
3431 if (buf_len(diffbuf) > 0) {
3432 error = buf_putc(diffbuf, '\0');
3433 *head_html = strdup(buf_get(diffbuf));
3434 if (*head_html == NULL)
3435 error = got_error_from_errno("strdup");
3437 done:
3438 buf_free(diffbuf);
3439 got_ref_list_free(&refs);
3440 if (repo)
3441 got_repo_close(repo);
3442 return error;
3445 static char *
3446 gw_get_site_link(struct gw_trans *gw_trans)
3448 char *link = NULL, *repo = NULL, *action = NULL;
3450 if (gw_trans->repo_name != NULL &&
3451 asprintf(&repo, " / <a href='?path=%s&action=summary'>%s</a>",
3452 gw_trans->repo_name, gw_trans->repo_name) == -1)
3453 return NULL;
3455 if (gw_trans->action_name != NULL &&
3456 asprintf(&action, " / %s", gw_trans->action_name) == -1) {
3457 free(repo);
3458 return NULL;
3461 if (asprintf(&link, site_link, GOTWEB,
3462 gw_trans->gw_conf->got_site_link,
3463 repo ? repo : "", action ? action : "") == -1) {
3464 free(repo);
3465 free(action);
3466 return NULL;
3469 free(repo);
3470 free(action);
3471 return link;
3474 static const struct got_error *
3475 gw_colordiff_line(char **colorized_line, char *buf)
3477 const struct got_error *error = NULL;
3478 char *div_diff_line_div = NULL, *color = NULL;
3479 struct buf *diffbuf = NULL;
3480 size_t newsize;
3482 *colorized_line = NULL;
3484 error = buf_alloc(&diffbuf, 0);
3485 if (error)
3486 return error;
3488 if (strncmp(buf, "-", 1) == 0)
3489 color = "diff_minus";
3490 else if (strncmp(buf, "+", 1) == 0)
3491 color = "diff_plus";
3492 else if (strncmp(buf, "@@", 2) == 0)
3493 color = "diff_chunk_header";
3494 else if (strncmp(buf, "@@", 2) == 0)
3495 color = "diff_chunk_header";
3496 else if (strncmp(buf, "commit +", 8) == 0)
3497 color = "diff_meta";
3498 else if (strncmp(buf, "commit -", 8) == 0)
3499 color = "diff_meta";
3500 else if (strncmp(buf, "blob +", 6) == 0)
3501 color = "diff_meta";
3502 else if (strncmp(buf, "blob -", 6) == 0)
3503 color = "diff_meta";
3504 else if (strncmp(buf, "file +", 6) == 0)
3505 color = "diff_meta";
3506 else if (strncmp(buf, "file -", 6) == 0)
3507 color = "diff_meta";
3508 else if (strncmp(buf, "from:", 5) == 0)
3509 color = "diff_author";
3510 else if (strncmp(buf, "via:", 4) == 0)
3511 color = "diff_author";
3512 else if (strncmp(buf, "date:", 5) == 0)
3513 color = "diff_date";
3515 if (asprintf(&div_diff_line_div, div_diff_line, color ? color : "")
3516 == -1) {
3517 error = got_error_from_errno("asprintf");
3518 goto done;
3521 error = buf_puts(&newsize, diffbuf, div_diff_line_div);
3522 if (error)
3523 goto done;
3525 error = buf_puts(&newsize, diffbuf, buf);
3526 if (error)
3527 goto done;
3529 if (buf_len(diffbuf) > 0) {
3530 error = buf_putc(diffbuf, '\0');
3531 *colorized_line = strdup(buf_get(diffbuf));
3532 if (*colorized_line == NULL)
3533 error = got_error_from_errno("strdup");
3535 done:
3536 free(diffbuf);
3537 free(div_diff_line_div);
3538 return error;
3542 * XXX This function should not exist.
3543 * We should let khtml_puts(3) handle HTML escaping.
3545 static const struct got_error *
3546 gw_html_escape(char **escaped_html, const char *orig_html)
3548 const struct got_error *error = NULL;
3549 struct escape_pair {
3550 char c;
3551 const char *s;
3552 } esc[] = {
3553 { '>', "&gt;" },
3554 { '<', "&lt;" },
3555 { '&', "&amp;" },
3556 { '"', "&quot;" },
3557 { '\'', "&apos;" },
3558 { '\n', "<br />" },
3560 size_t orig_len, len;
3561 int i, j, x;
3563 orig_len = strlen(orig_html);
3564 len = orig_len;
3565 for (i = 0; i < orig_len; i++) {
3566 for (j = 0; j < nitems(esc); j++) {
3567 if (orig_html[i] != esc[j].c)
3568 continue;
3569 len += strlen(esc[j].s) - 1 /* escaped char */;
3573 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
3574 if (*escaped_html == NULL)
3575 return got_error_from_errno("calloc");
3577 x = 0;
3578 for (i = 0; i < orig_len; i++) {
3579 int escaped = 0;
3580 for (j = 0; j < nitems(esc); j++) {
3581 if (orig_html[i] != esc[j].c)
3582 continue;
3584 if (strlcat(*escaped_html, esc[j].s, len + 1)
3585 >= len + 1) {
3586 error = got_error(GOT_ERR_NO_SPACE);
3587 goto done;
3589 x += strlen(esc[j].s);
3590 escaped = 1;
3591 break;
3593 if (!escaped) {
3594 (*escaped_html)[x] = orig_html[i];
3595 x++;
3598 done:
3599 if (error) {
3600 free(*escaped_html);
3601 *escaped_html = NULL;
3602 } else {
3603 (*escaped_html)[x] = '\0';
3605 return error;
3608 int
3609 main(int argc, char *argv[])
3611 const struct got_error *error = NULL;
3612 struct gw_trans *gw_trans;
3613 struct gw_dir *dir = NULL, *tdir;
3614 const char *page = "index";
3615 int gw_malloc = 1;
3616 enum kcgi_err kerr;
3618 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
3619 errx(1, "malloc");
3621 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
3622 errx(1, "malloc");
3624 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
3625 errx(1, "malloc");
3627 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
3628 errx(1, "malloc");
3630 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
3631 if (kerr != KCGI_OK) {
3632 error = gw_kcgi_error(kerr);
3633 goto done;
3636 if ((gw_trans->gw_conf =
3637 malloc(sizeof(struct gotweb_conf))) == NULL) {
3638 gw_malloc = 0;
3639 error = got_error_from_errno("malloc");
3640 goto done;
3643 TAILQ_INIT(&gw_trans->gw_dirs);
3644 TAILQ_INIT(&gw_trans->gw_headers);
3646 gw_trans->page = 0;
3647 gw_trans->repos_total = 0;
3648 gw_trans->repo_path = NULL;
3649 gw_trans->commit = NULL;
3650 gw_trans->headref = strdup(GOT_REF_HEAD);
3651 gw_trans->mime = KMIME_TEXT_HTML;
3652 gw_trans->gw_tmpl->key = gw_templs;
3653 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
3654 gw_trans->gw_tmpl->arg = gw_trans;
3655 gw_trans->gw_tmpl->cb = gw_template;
3656 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
3657 if (error)
3658 goto done;
3660 error = gw_parse_querystring(gw_trans);
3661 if (error)
3662 goto done;
3664 if (gw_trans->action == GW_BLOB)
3665 error = gw_blob(gw_trans);
3666 else
3667 error = gw_display_index(gw_trans);
3668 done:
3669 if (error) {
3670 gw_trans->mime = KMIME_TEXT_PLAIN;
3671 gw_trans->action = GW_ERR;
3672 gw_display_error(gw_trans, error);
3674 if (gw_malloc) {
3675 free(gw_trans->gw_conf->got_repos_path);
3676 free(gw_trans->gw_conf->got_site_name);
3677 free(gw_trans->gw_conf->got_site_owner);
3678 free(gw_trans->gw_conf->got_site_link);
3679 free(gw_trans->gw_conf->got_logo);
3680 free(gw_trans->gw_conf->got_logo_url);
3681 free(gw_trans->gw_conf);
3682 free(gw_trans->commit);
3683 free(gw_trans->repo_path);
3684 free(gw_trans->repo_name);
3685 free(gw_trans->repo_file);
3686 free(gw_trans->action_name);
3687 free(gw_trans->headref);
3689 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
3690 free(dir->name);
3691 free(dir->description);
3692 free(dir->age);
3693 free(dir->url);
3694 free(dir->path);
3695 free(dir);
3700 khttp_free(gw_trans->gw_req);
3701 return 0;