Blob


1 /*
2 * Copyright (c) 2019, 2020 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/queue.h>
19 #include <sys/stat.h>
20 #include <sys/types.h>
22 #include <ctype.h>
23 #include <dirent.h>
24 #include <err.h>
25 #include <errno.h>
26 #include <regex.h>
27 #include <stdarg.h>
28 #include <stdint.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
34 #include <got_object.h>
35 #include <got_reference.h>
36 #include <got_repository.h>
37 #include <got_path.h>
38 #include <got_cancel.h>
39 #include <got_worktree.h>
40 #include <got_diff.h>
41 #include <got_commit_graph.h>
42 #include <got_blame.h>
43 #include <got_privsep.h>
44 #include <got_opentemp.h>
46 #include <kcgi.h>
47 #include <kcgihtml.h>
49 #include "buf.h"
50 #include "gotweb.h"
51 #include "gotweb_ui.h"
53 #ifndef nitems
54 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
55 #endif
57 struct gw_trans {
58 TAILQ_HEAD(headers, gw_header) gw_headers;
59 TAILQ_HEAD(dirs, gw_dir) gw_dirs;
60 struct gw_dir *gw_dir;
61 struct gotweb_conf *gw_conf;
62 struct ktemplate *gw_tmpl;
63 struct khtmlreq *gw_html_req;
64 struct kreq *gw_req;
65 char *repo_name;
66 char *repo_path;
67 char *commit;
68 char *repo_file;
69 char *repo_folder;
70 char *action_name;
71 char *headref;
72 unsigned int action;
73 unsigned int page;
74 unsigned int repos_total;
75 enum kmime mime;
76 };
78 struct gw_header {
79 TAILQ_ENTRY(gw_header) entry;
80 struct got_repository *repo;
81 struct got_reflist_head refs;
82 struct got_commit_object *commit;
83 struct got_object_id *id;
84 char *path;
86 char *refs_str;
87 char *commit_id; /* id_str1 */
88 char *parent_id; /* id_str2 */
89 char *tree_id;
90 const char *author;
91 const char *committer;
92 char *commit_msg;
93 time_t committer_time;
94 };
96 struct gw_dir {
97 TAILQ_ENTRY(gw_dir) entry;
98 char *name;
99 char *owner;
100 char *description;
101 char *url;
102 char *age;
103 char *path;
104 };
106 enum gw_key {
107 KEY_ACTION,
108 KEY_COMMIT_ID,
109 KEY_FILE,
110 KEY_FOLDER,
111 KEY_HEADREF,
112 KEY_PAGE,
113 KEY_PATH,
114 KEY__ZMAX
115 };
117 enum gw_tmpl {
118 TEMPL_CONTENT,
119 TEMPL_HEAD,
120 TEMPL_HEADER,
121 TEMPL_SEARCH,
122 TEMPL_SITEPATH,
123 TEMPL_SITEOWNER,
124 TEMPL_TITLE,
125 TEMPL__MAX
126 };
128 enum gw_ref_tm {
129 TM_DIFF,
130 TM_LONG,
131 };
133 enum gw_tags {
134 TAGBRIEF,
135 TAGFULL,
136 };
138 static const char *const gw_templs[TEMPL__MAX] = {
139 "content",
140 "head",
141 "header",
142 "search",
143 "sitepath",
144 "siteowner",
145 "title",
146 };
148 static const struct kvalid gw_keys[KEY__ZMAX] = {
149 { kvalid_stringne, "action" },
150 { kvalid_stringne, "commit" },
151 { kvalid_stringne, "file" },
152 { kvalid_stringne, "folder" },
153 { kvalid_stringne, "headref" },
154 { kvalid_int, "page" },
155 { kvalid_stringne, "path" },
156 };
158 static struct gw_dir *gw_init_gw_dir(char *);
159 static struct gw_header *gw_init_header(void);
161 static const struct got_error *gw_get_repo_description(char **,
162 struct gw_trans *, char *);
163 static const struct got_error *gw_get_repo_owner(char **, struct gw_trans *,
164 char *);
165 static const struct got_error *gw_get_time_str(char **, time_t, int);
166 static const struct got_error *gw_get_repo_age(char **, struct gw_trans *,
167 char *, char *, int);
168 static const struct got_error *gw_output_file_blame(struct gw_trans *);
169 static const struct got_error *gw_output_blob_buf(struct gw_trans *);
170 static const struct got_error *gw_output_repo_tree(struct gw_trans *);
171 static const struct got_error *gw_output_diff(struct gw_trans *,
172 struct gw_header *);
173 static const struct got_error *gw_output_repo_tags(struct gw_trans *,
174 struct gw_header *, int, int);
175 static const struct got_error *gw_get_repo_heads(char **, struct gw_trans *);
176 static const struct got_error *gw_get_clone_url(char **, struct gw_trans *,
177 char *);
178 static char *gw_get_site_link(struct gw_trans *);
179 static const struct got_error *gw_html_escape(char **, const char *);
180 static const struct got_error *gw_colordiff_line(struct gw_trans *, char *);
182 static const struct got_error *gw_gen_commit_header(struct gw_trans *, char *,
183 char*);
184 static const struct got_error *gw_gen_diff_header(struct gw_trans *, char *,
185 char*);
186 static const struct got_error *gw_gen_author_header(struct gw_trans *,
187 const char *);
188 static const struct got_error *gw_gen_age_header(struct gw_trans *,
189 const char *);
190 static const struct got_error *gw_gen_committer_header(struct gw_trans *,
191 const char *);
192 static const struct got_error *gw_gen_commit_msg_header(struct gw_trans*,
193 char *);
194 static const struct got_error *gw_gen_tree_header(struct gw_trans *, char *);
196 static void gw_free_headers(struct gw_header *);
197 static const struct got_error* gw_display_open(struct gw_trans *, enum khttp,
198 enum kmime);
199 static const struct got_error* gw_display_index(struct gw_trans *);
200 static void gw_display_error(struct gw_trans *,
201 const struct got_error *);
203 static int gw_template(size_t, void *);
205 static const struct got_error* gw_get_header(struct gw_trans *,
206 struct gw_header *, int);
207 static const struct got_error* gw_get_commits(struct gw_trans *,
208 struct gw_header *, int);
209 static const struct got_error* gw_get_commit(struct gw_trans *,
210 struct gw_header *);
211 static const struct got_error* gw_apply_unveil(const char *, const char *);
212 static const struct got_error* gw_blame_cb(void *, int, int,
213 struct got_object_id *);
214 static const struct got_error* gw_load_got_paths(struct gw_trans *);
215 static const struct got_error* gw_load_got_path(struct gw_trans *,
216 struct gw_dir *);
217 static const struct got_error* gw_parse_querystring(struct gw_trans *);
219 static const struct got_error* gw_blame(struct gw_trans *);
220 static const struct got_error* gw_blob(struct gw_trans *);
221 static const struct got_error* gw_diff(struct gw_trans *);
222 static const struct got_error* gw_index(struct gw_trans *);
223 static const struct got_error* gw_commits(struct gw_trans *);
224 static const struct got_error* gw_briefs(struct gw_trans *);
225 static const struct got_error* gw_summary(struct gw_trans *);
226 static const struct got_error* gw_tree(struct gw_trans *);
227 static const struct got_error* gw_tag(struct gw_trans *);
229 struct gw_query_action {
230 unsigned int func_id;
231 const char *func_name;
232 const struct got_error *(*func_main)(struct gw_trans *);
233 char *template;
234 };
236 enum gw_query_actions {
237 GW_BLAME,
238 GW_BLOB,
239 GW_BRIEFS,
240 GW_COMMITS,
241 GW_DIFF,
242 GW_ERR,
243 GW_INDEX,
244 GW_SUMMARY,
245 GW_TAG,
246 GW_TREE,
247 };
249 static struct gw_query_action gw_query_funcs[] = {
250 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
251 { GW_BLOB, "blob", NULL, NULL },
252 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
253 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
254 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
255 { GW_ERR, NULL, NULL, "gw_tmpl/err.tmpl" },
256 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
257 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
258 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
259 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
260 };
262 static const struct got_error *
263 gw_kcgi_error(enum kcgi_err kerr)
265 if (kerr == KCGI_OK)
266 return NULL;
268 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
269 return got_error(GOT_ERR_CANCELLED);
271 if (kerr == KCGI_ENOMEM)
272 return got_error_set_errno(ENOMEM,
273 kcgi_strerror(kerr));
275 if (kerr == KCGI_ENFILE)
276 return got_error_set_errno(ENFILE,
277 kcgi_strerror(kerr));
279 if (kerr == KCGI_EAGAIN)
280 return got_error_set_errno(EAGAIN,
281 kcgi_strerror(kerr));
283 if (kerr == KCGI_FORM)
284 return got_error_msg(GOT_ERR_IO,
285 kcgi_strerror(kerr));
287 return got_error_from_errno(kcgi_strerror(kerr));
290 static const struct got_error *
291 gw_apply_unveil(const char *repo_path, const char *repo_file)
293 const struct got_error *err;
295 if (repo_path && repo_file) {
296 char *full_path;
297 if (asprintf(&full_path, "%s/%s", repo_path, repo_file) == -1)
298 return got_error_from_errno("asprintf unveil");
299 if (unveil(full_path, "r") != 0)
300 return got_error_from_errno2("unveil", full_path);
303 if (repo_path && unveil(repo_path, "r") != 0)
304 return got_error_from_errno2("unveil", repo_path);
306 if (unveil("/tmp", "rwc") != 0)
307 return got_error_from_errno2("unveil", "/tmp");
309 err = got_privsep_unveil_exec_helpers();
310 if (err != NULL)
311 return err;
313 if (unveil(NULL, NULL) != 0)
314 return got_error_from_errno("unveil");
316 return NULL;
319 static const struct got_error *
320 gw_empty_string(char **s)
322 *s = strdup("");
323 if (*s == NULL)
324 return got_error_from_errno("strdup");
325 return NULL;
328 static int
329 isbinary(const uint8_t *buf, size_t n)
331 size_t i;
333 for (i = 0; i < n; i++)
334 if (buf[i] == 0)
335 return 1;
336 return 0;
339 static const struct got_error *
340 gw_blame(struct gw_trans *gw_trans)
342 const struct got_error *error = NULL;
343 struct gw_header *header = NULL;
344 char *age = NULL, *escaped_commit_msg = NULL;
345 enum kcgi_err kerr;
347 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
348 NULL) == -1)
349 return got_error_from_errno("pledge");
351 if ((header = gw_init_header()) == NULL)
352 return got_error_from_errno("malloc");
354 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
355 if (error)
356 goto done;
358 error = gw_get_header(gw_trans, header, 1);
359 if (error)
360 goto done;
362 /* blame header */
363 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
364 "blame_header_wrapper", KATTR__MAX);
365 if (kerr != KCGI_OK)
366 goto done;
367 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
368 "blame_header", KATTR__MAX);
369 if (kerr != KCGI_OK)
370 goto done;
371 error = gw_get_time_str(&age, header->committer_time,
372 TM_LONG);
373 if (error)
374 goto done;
375 error = gw_gen_age_header(gw_trans, age ?age : "");
376 if (error)
377 goto done;
378 /*
379 * XXX: keeping this for now, since kcgihtml does not convert
380 * \n into <br /> yet.
381 */
382 error = gw_html_escape(&escaped_commit_msg, header->commit_msg);
383 if (error)
384 goto done;
385 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
386 if (error)
387 goto done;
388 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
389 if (kerr != KCGI_OK)
390 goto done;
391 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
392 "dotted_line", KATTR__MAX);
393 if (kerr != KCGI_OK)
394 goto done;
395 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
396 if (kerr != KCGI_OK)
397 goto done;
399 /* blame */
400 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
401 "blame", KATTR__MAX);
402 if (kerr != KCGI_OK)
403 goto done;
404 error = gw_output_file_blame(gw_trans);
405 if (error)
406 goto done;
407 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
408 done:
409 got_ref_list_free(&header->refs);
410 gw_free_headers(header);
411 free(escaped_commit_msg);
412 if (error == NULL && kerr != KCGI_OK)
413 error = gw_kcgi_error(kerr);
414 return error;
417 static const struct got_error *
418 gw_blob(struct gw_trans *gw_trans)
420 const struct got_error *error = NULL;
421 struct gw_header *header = NULL;
423 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
424 NULL) == -1)
425 return got_error_from_errno("pledge");
427 if ((header = gw_init_header()) == NULL)
428 return got_error_from_errno("malloc");
430 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
431 if (error)
432 goto done;
434 error = gw_get_header(gw_trans, header, 1);
435 if (error)
436 goto done;
438 error = gw_output_blob_buf(gw_trans);
439 done:
440 got_ref_list_free(&header->refs);
441 gw_free_headers(header);
442 return error;
445 static const struct got_error *
446 gw_diff(struct gw_trans *gw_trans)
448 const struct got_error *error = NULL;
449 struct gw_header *header = NULL;
450 char *age = NULL, *escaped_commit_msg = NULL;
451 enum kcgi_err kerr = KCGI_OK;
453 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
454 NULL) == -1)
455 return got_error_from_errno("pledge");
457 if ((header = gw_init_header()) == NULL)
458 return got_error_from_errno("malloc");
460 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
461 if (error)
462 goto done;
464 error = gw_get_header(gw_trans, header, 1);
465 if (error)
466 goto done;
468 /* diff header */
469 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
470 "diff_header_wrapper", KATTR__MAX);
471 if (kerr != KCGI_OK)
472 goto done;
473 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
474 "diff_header", KATTR__MAX);
475 if (kerr != KCGI_OK)
476 goto done;
477 error = gw_gen_diff_header(gw_trans, header->parent_id,
478 header->commit_id);
479 if (error)
480 goto done;
481 error = gw_gen_commit_header(gw_trans, header->commit_id,
482 header->refs_str);
483 if (error)
484 goto done;
485 error = gw_gen_tree_header(gw_trans, header->tree_id);
486 if (error)
487 goto done;
488 error = gw_gen_author_header(gw_trans, header->author);
489 if (error)
490 goto done;
491 error = gw_gen_committer_header(gw_trans, header->author);
492 if (error)
493 goto done;
494 error = gw_get_time_str(&age, header->committer_time,
495 TM_LONG);
496 if (error)
497 goto done;
498 error = gw_gen_age_header(gw_trans, age ?age : "");
499 if (error)
500 goto done;
501 /*
502 * XXX: keeping this for now, since kcgihtml does not convert
503 * \n into <br /> yet.
504 */
505 error = gw_html_escape(&escaped_commit_msg, header->commit_msg);
506 if (error)
507 goto done;
508 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
509 if (error)
510 goto done;
511 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
512 if (kerr != KCGI_OK)
513 goto done;
514 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
515 "dotted_line", KATTR__MAX);
516 if (kerr != KCGI_OK)
517 goto done;
518 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
519 if (kerr != KCGI_OK)
520 goto done;
522 /* diff */
523 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
524 "diff", KATTR__MAX);
525 if (kerr != KCGI_OK)
526 goto done;
527 error = gw_output_diff(gw_trans, header);
528 if (error)
529 goto done;
531 /* diff content close */
532 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
533 if (kerr != KCGI_OK)
534 goto done;
535 done:
536 got_ref_list_free(&header->refs);
537 gw_free_headers(header);
538 free(age);
539 free(escaped_commit_msg);
540 if (error == NULL && kerr != KCGI_OK)
541 error = gw_kcgi_error(kerr);
542 return error;
545 static const struct got_error *
546 gw_index(struct gw_trans *gw_trans)
548 const struct got_error *error = NULL;
549 struct gw_dir *gw_dir = NULL;
550 char *html, *navs, *next, *prev;
551 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
552 enum kcgi_err kerr;
554 if (pledge("stdio rpath proc exec sendfd unveil",
555 NULL) == -1) {
556 error = got_error_from_errno("pledge");
557 return error;
560 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
561 if (error)
562 return error;
564 error = gw_load_got_paths(gw_trans);
565 if (error)
566 return error;
568 kerr = khttp_puts(gw_trans->gw_req, index_projects_header);
569 if (kerr != KCGI_OK)
570 return gw_kcgi_error(kerr);
572 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
573 if (asprintf(&html, index_projects_empty,
574 gw_trans->gw_conf->got_repos_path) == -1)
575 return got_error_from_errno("asprintf");
576 kerr = khttp_puts(gw_trans->gw_req, html);
577 if (kerr != KCGI_OK)
578 error = gw_kcgi_error(kerr);
579 free(html);
580 return error;
583 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
584 dir_c++;
586 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
587 if (gw_trans->page > 0 && (gw_trans->page *
588 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
589 prev_disp++;
590 continue;
593 prev_disp++;
595 if (error)
596 return error;
597 if(asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
598 gw_dir->name, gw_dir->name) == -1)
599 return got_error_from_errno("asprintf");
601 if (asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
602 gw_dir->description, gw_dir->owner ? gw_dir->owner : "",
603 gw_dir->age,
604 navs) == -1)
605 return got_error_from_errno("asprintf");
607 kerr = khttp_puts(gw_trans->gw_req, html);
608 free(navs);
609 free(html);
610 if (kerr != KCGI_OK)
611 return gw_kcgi_error(kerr);
613 if (gw_trans->gw_conf->got_max_repos_display == 0)
614 continue;
616 if (next_disp == gw_trans->gw_conf->got_max_repos_display) {
617 kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
618 if (kerr != KCGI_OK)
619 return gw_kcgi_error(kerr);
620 } else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
621 (gw_trans->page > 0) &&
622 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
623 prev_disp == gw_trans->repos_total)) {
624 kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
625 if (kerr != KCGI_OK)
626 return gw_kcgi_error(kerr);
629 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
630 (gw_trans->page > 0) &&
631 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
632 prev_disp == gw_trans->repos_total)) {
633 if (asprintf(&prev, nav_prev, gw_trans->page - 1) == -1)
634 return got_error_from_errno("asprintf");
635 kerr = khttp_puts(gw_trans->gw_req, prev);
636 free(prev);
637 if (kerr != KCGI_OK)
638 return gw_kcgi_error(kerr);
641 kerr = khttp_puts(gw_trans->gw_req, div_end);
642 if (kerr != KCGI_OK)
643 return gw_kcgi_error(kerr);
645 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
646 next_disp == gw_trans->gw_conf->got_max_repos_display &&
647 dir_c != (gw_trans->page + 1) *
648 gw_trans->gw_conf->got_max_repos_display) {
649 if (asprintf(&next, nav_next, gw_trans->page + 1) == -1)
650 return got_error_from_errno("calloc");
651 kerr = khttp_puts(gw_trans->gw_req, next);
652 free(next);
653 if (kerr != KCGI_OK)
654 return gw_kcgi_error(kerr);
655 kerr = khttp_puts(gw_trans->gw_req, div_end);
656 if (kerr != KCGI_OK)
657 error = gw_kcgi_error(kerr);
658 next_disp = 0;
659 break;
662 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
663 (gw_trans->page > 0) &&
664 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
665 prev_disp == gw_trans->repos_total)) {
666 kerr = khttp_puts(gw_trans->gw_req, div_end);
667 if (kerr != KCGI_OK)
668 return gw_kcgi_error(kerr);
671 next_disp++;
673 return error;
676 static const struct got_error *
677 gw_commits(struct gw_trans *gw_trans)
679 const struct got_error *error = NULL;
680 struct gw_header *header = NULL, *n_header = NULL;
681 char *age = NULL, *escaped_commit_msg = NULL;
682 char *href_diff = NULL, *href_blob = NULL;
683 enum kcgi_err kerr = KCGI_OK;
685 if ((header = gw_init_header()) == NULL)
686 return got_error_from_errno("malloc");
688 if (pledge("stdio rpath proc exec sendfd unveil",
689 NULL) == -1) {
690 error = got_error_from_errno("pledge");
691 goto done;
694 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
695 if (error)
696 goto done;
698 error = gw_get_header(gw_trans, header,
699 gw_trans->gw_conf->got_max_commits_display);
700 if (error)
701 goto done;
703 /* commit content */
704 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
705 /* commit line */
706 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
707 "commits_line_wrapper", KATTR__MAX);
708 if (kerr != KCGI_OK)
709 goto done;
710 error = gw_gen_commit_header(gw_trans, n_header->commit_id,
711 n_header->refs_str);
712 if (error)
713 goto done;
714 error = gw_gen_author_header(gw_trans, n_header->author);
715 if (error)
716 goto done;
717 error = gw_gen_committer_header(gw_trans, n_header->author);
718 if (error)
719 goto done;
720 error = gw_get_time_str(&age, n_header->committer_time,
721 TM_LONG);
722 if (error)
723 goto done;
724 error = gw_gen_age_header(gw_trans, age ?age : "");
725 if (error)
726 goto done;
727 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
728 if (kerr != KCGI_OK)
729 goto done;
731 /* dotted line */
732 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
733 "dotted_line", KATTR__MAX);
734 if (kerr != KCGI_OK)
735 goto done;
736 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
737 if (kerr != KCGI_OK)
738 goto done;
740 /* commit */
741 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
742 "commit", KATTR__MAX);
743 if (kerr != KCGI_OK)
744 goto done;
745 /*
746 * XXX: keeping this for now, since kcgihtml does not convert
747 * \n into <br /> yet.
748 */
749 error = gw_html_escape(&escaped_commit_msg,
750 n_header->commit_msg);
751 if (error)
752 goto done;
753 kerr = khttp_puts(gw_trans->gw_req, escaped_commit_msg);
754 if (kerr != KCGI_OK)
755 goto done;
756 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
757 if (kerr != KCGI_OK)
758 goto done;
760 /* navs */
762 /* XXX: create gen code for this */
763 /* build diff nav */
764 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
765 gw_trans->repo_name, n_header->commit_id) == -1) {
766 error = got_error_from_errno("asprintf");
767 goto done;
769 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
770 KATTR_ID, "navs_wrapper", KATTR__MAX);
771 if (kerr != KCGI_OK)
772 goto done;
773 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
774 KATTR_ID, "navs", KATTR__MAX);
775 if (kerr != KCGI_OK)
776 goto done;
777 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
778 KATTR_HREF, href_diff, KATTR__MAX);
779 if (kerr != KCGI_OK)
780 goto done;
781 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
782 if (kerr != KCGI_OK)
783 goto done;
784 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
785 if (kerr != KCGI_OK)
786 goto done;
788 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
789 if (kerr != KCGI_OK)
790 goto done;
792 /* XXX: create gen code for this */
793 /* build tree nav */
794 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
795 gw_trans->repo_name, n_header->commit_id) == -1) {
796 error = got_error_from_errno("asprintf");
797 goto done;
799 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
800 KATTR_HREF, href_blob, KATTR__MAX);
801 if (kerr != KCGI_OK)
802 goto done;
803 khtml_puts(gw_trans->gw_html_req, "tree");
804 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
805 if (kerr != KCGI_OK)
806 goto done;
807 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
808 if (kerr != KCGI_OK)
809 goto done;
811 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
812 "solid_line", KATTR__MAX);
813 if (kerr != KCGI_OK)
814 goto done;
815 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
816 if (kerr != KCGI_OK)
817 goto done;
819 free(age);
820 age = NULL;
821 free(escaped_commit_msg);
822 escaped_commit_msg = NULL;
824 done:
825 got_ref_list_free(&header->refs);
826 gw_free_headers(header);
827 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
828 gw_free_headers(n_header);
829 free(age);
830 free(href_diff);
831 free(href_blob);
832 free(escaped_commit_msg);
833 if (error == NULL && kerr != KCGI_OK)
834 error = gw_kcgi_error(kerr);
835 return error;
838 static const struct got_error *
839 gw_briefs(struct gw_trans *gw_trans)
841 const struct got_error *error = NULL;
842 struct gw_header *header = NULL, *n_header = NULL;
843 char *age = NULL, *age_html = NULL;
844 char *href_diff = NULL, *href_blob = NULL;
845 char *newline, *smallerthan;
846 enum kcgi_err kerr = KCGI_OK;
848 if ((header = gw_init_header()) == NULL)
849 return got_error_from_errno("malloc");
851 if (pledge("stdio rpath proc exec sendfd unveil",
852 NULL) == -1) {
853 error = got_error_from_errno("pledge");
854 goto done;
857 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
858 if (error)
859 goto done;
861 if (gw_trans->action == GW_SUMMARY)
862 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
863 else
864 error = gw_get_header(gw_trans, header,
865 gw_trans->gw_conf->got_max_commits_display);
866 if (error)
867 goto done;
869 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
870 error = gw_get_time_str(&age, n_header->committer_time,
871 TM_DIFF);
872 if (error)
873 goto done;
875 /* briefs wrapper */
876 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
877 KATTR_ID, "briefs_wrapper", KATTR__MAX);
878 if (kerr != KCGI_OK)
879 goto done;
881 /* briefs age */
882 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
883 KATTR_ID, "briefs_age", KATTR__MAX);
884 if (kerr != KCGI_OK)
885 goto done;
886 if (asprintf(&age_html, "%s", age ? age : "") == -1) {
887 error = got_error_from_errno("asprintf");
888 goto done;
890 kerr = khtml_puts(gw_trans->gw_html_req, age_html);
891 if (kerr != KCGI_OK)
892 goto done;
893 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
894 if (kerr != KCGI_OK)
895 goto done;
897 /* briefs author */
898 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
899 KATTR_ID, "briefs_author", KATTR__MAX);
900 if (kerr != KCGI_OK)
901 goto done;
902 smallerthan = strchr(n_header->author, '<');
903 if (smallerthan)
904 *smallerthan = '\0';
905 kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
906 if (kerr != KCGI_OK)
907 goto done;
908 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
909 if (kerr != KCGI_OK)
910 goto done;
912 /* briefs log */
913 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
914 gw_trans->repo_name, n_header->commit_id) == -1) {
915 error = got_error_from_errno("asprintf");
916 goto done;
918 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
919 KATTR_ID, "briefs_log", KATTR__MAX);
920 if (kerr != KCGI_OK)
921 goto done;
922 newline = strchr(n_header->commit_msg, '\n');
923 if (newline)
924 *newline = '\0';
925 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
926 KATTR_HREF, href_diff, KATTR__MAX);
927 if (kerr != KCGI_OK)
928 goto done;
929 kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
930 if (kerr != KCGI_OK)
931 goto done;
932 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
933 if (kerr != KCGI_OK)
934 goto done;
936 /* build diff nav */
937 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
938 KATTR_ID, "navs_wrapper", KATTR__MAX);
939 if (kerr != KCGI_OK)
940 goto done;
941 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
942 KATTR_ID, "navs", KATTR__MAX);
943 if (kerr != KCGI_OK)
944 goto done;
945 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
946 KATTR_HREF, href_diff, KATTR__MAX);
947 if (kerr != KCGI_OK)
948 goto done;
949 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
950 if (kerr != KCGI_OK)
951 goto done;
952 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
953 if (kerr != KCGI_OK)
954 goto done;
956 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
957 if (kerr != KCGI_OK)
958 goto done;
960 /* build tree nav */
961 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
962 gw_trans->repo_name, n_header->commit_id) == -1) {
963 error = got_error_from_errno("asprintf");
964 goto done;
966 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
967 KATTR_HREF, href_blob, KATTR__MAX);
968 if (kerr != KCGI_OK)
969 goto done;
970 khtml_puts(gw_trans->gw_html_req, "tree");
971 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
972 if (kerr != KCGI_OK)
973 goto done;
974 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
975 if (kerr != KCGI_OK)
976 goto done;
978 /* dotted line */
979 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
980 KATTR_ID, "dotted_line", KATTR__MAX);
981 if (kerr != KCGI_OK)
982 goto done;
983 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
984 if (kerr != KCGI_OK)
985 goto done;
987 free(age);
988 age = NULL;
989 free(age_html);
990 age_html = NULL;
991 free(href_diff);
992 href_diff = NULL;
993 free(href_blob);
994 href_blob = NULL;
996 done:
997 got_ref_list_free(&header->refs);
998 gw_free_headers(header);
999 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1000 gw_free_headers(n_header);
1001 free(age);
1002 free(age_html);
1003 free(href_diff);
1004 free(href_blob);
1005 if (error == NULL && kerr != KCGI_OK)
1006 error = gw_kcgi_error(kerr);
1007 return error;
1010 static const struct got_error *
1011 gw_summary(struct gw_trans *gw_trans)
1013 const struct got_error *error = NULL;
1014 char *age = NULL, *tags = NULL, *heads = NULL;
1015 enum kcgi_err kerr = KCGI_OK;
1017 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1018 return got_error_from_errno("pledge");
1020 /* unveil is applied with gw_briefs below */
1022 /* summary wrapper */
1023 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1024 "summary_wrapper", KATTR__MAX);
1025 if (kerr != KCGI_OK)
1026 return gw_kcgi_error(kerr);
1028 /* description */
1029 if (gw_trans->gw_conf->got_show_repo_description &&
1030 gw_trans->gw_dir->description != NULL &&
1031 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1032 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1033 KATTR_ID, "description_title", KATTR__MAX);
1034 if (kerr != KCGI_OK)
1035 goto done;
1036 kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1037 if (kerr != KCGI_OK)
1038 goto done;
1039 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1040 if (kerr != KCGI_OK)
1041 goto done;
1042 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1043 KATTR_ID, "description", KATTR__MAX);
1044 if (kerr != KCGI_OK)
1045 goto done;
1046 kerr = khtml_puts(gw_trans->gw_html_req,
1047 gw_trans->gw_dir->description);
1048 if (kerr != KCGI_OK)
1049 goto done;
1050 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1051 if (kerr != KCGI_OK)
1052 goto done;
1055 /* repo owner */
1056 if (gw_trans->gw_conf->got_show_repo_owner &&
1057 gw_trans->gw_dir->owner != NULL) {
1058 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1059 KATTR_ID, "repo_owner_title", KATTR__MAX);
1060 if (kerr != KCGI_OK)
1061 goto done;
1062 kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1063 if (kerr != KCGI_OK)
1064 goto done;
1065 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1066 if (kerr != KCGI_OK)
1067 goto done;
1068 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1069 KATTR_ID, "repo_owner", KATTR__MAX);
1070 if (kerr != KCGI_OK)
1071 goto done;
1072 kerr = khtml_puts(gw_trans->gw_html_req,
1073 gw_trans->gw_dir->owner);
1074 if (kerr != KCGI_OK)
1075 goto done;
1076 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1077 if (kerr != KCGI_OK)
1078 goto done;
1081 /* last change */
1082 if (gw_trans->gw_conf->got_show_repo_age) {
1083 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1084 "refs/heads", TM_LONG);
1085 if (error)
1086 goto done;
1087 if (age != NULL) {
1088 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1089 KATTR_ID, "last_change_title", KATTR__MAX);
1090 if (kerr != KCGI_OK)
1091 goto done;
1092 kerr = khtml_puts(gw_trans->gw_html_req,
1093 "Last Change: ");
1094 if (kerr != KCGI_OK)
1095 goto done;
1096 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1097 if (kerr != KCGI_OK)
1098 goto done;
1099 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1100 KATTR_ID, "last_change", KATTR__MAX);
1101 if (kerr != KCGI_OK)
1102 goto done;
1103 kerr = khtml_puts(gw_trans->gw_html_req, age);
1104 if (kerr != KCGI_OK)
1105 goto done;
1106 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1107 if (kerr != KCGI_OK)
1108 goto done;
1112 /* cloneurl */
1113 if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1114 gw_trans->gw_dir->url != NULL &&
1115 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1116 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1117 KATTR_ID, "cloneurl_title", KATTR__MAX);
1118 if (kerr != KCGI_OK)
1119 goto done;
1120 kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1121 if (kerr != KCGI_OK)
1122 goto done;
1123 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1124 if (kerr != KCGI_OK)
1125 goto done;
1126 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1127 KATTR_ID, "cloneurl", KATTR__MAX);
1128 if (kerr != KCGI_OK)
1129 goto done;
1130 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1131 if (kerr != KCGI_OK)
1132 goto done;
1133 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1134 if (kerr != KCGI_OK)
1135 goto done;
1138 /* close summary wrapper */
1139 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1140 if (kerr != KCGI_OK)
1141 goto done;
1143 /* commit briefs header */
1144 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1145 "briefs_title_wrapper", KATTR__MAX);
1146 if (kerr != KCGI_OK)
1147 goto done;
1148 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1149 "briefs_title", KATTR__MAX);
1150 if (kerr != KCGI_OK)
1151 goto done;
1152 kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1153 if (kerr != KCGI_OK)
1154 goto done;
1155 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1156 if (kerr != KCGI_OK)
1157 goto done;
1158 error = gw_briefs(gw_trans);
1159 if (error)
1160 goto done;
1162 /* tags */
1163 error = gw_output_repo_tags(gw_trans, NULL, D_MAXSLCOMMDISP,
1164 TAGBRIEF);
1165 if (error)
1166 goto done;
1168 /* heads */
1169 error = gw_get_repo_heads(&heads, gw_trans);
1170 if (error)
1171 goto done;
1172 if (heads != NULL && strcmp(heads, "") != 0) {
1173 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1174 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
1175 if (kerr != KCGI_OK)
1176 goto done;
1177 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1178 KATTR_ID, "summary_heads_title", KATTR__MAX);
1179 if (kerr != KCGI_OK)
1180 goto done;
1181 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
1182 if (kerr != KCGI_OK)
1183 goto done;
1184 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1185 if (kerr != KCGI_OK)
1186 goto done;
1187 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1188 KATTR_ID, "summary_heads_content", KATTR__MAX);
1189 if (kerr != KCGI_OK)
1190 goto done;
1191 kerr = khttp_puts(gw_trans->gw_req, heads);
1192 if (kerr != KCGI_OK)
1193 goto done;
1194 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1195 if (kerr != KCGI_OK)
1196 goto done;
1198 done:
1199 free(age);
1200 free(tags);
1201 free(heads);
1202 if (error == NULL && kerr != KCGI_OK)
1203 error = gw_kcgi_error(kerr);
1204 return error;
1207 static const struct got_error *
1208 gw_tree(struct gw_trans *gw_trans)
1210 const struct got_error *error = NULL;
1211 struct gw_header *header = NULL;
1212 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1213 char *age = NULL, *age_html = NULL, *escaped_commit_msg = NULL;
1214 enum kcgi_err kerr;
1216 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1217 return got_error_from_errno("pledge");
1219 if ((header = gw_init_header()) == NULL)
1220 return got_error_from_errno("malloc");
1222 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
1223 if (error)
1224 goto done;
1226 error = gw_get_header(gw_trans, header, 1);
1227 if (error)
1228 goto done;
1230 /* tree header */
1231 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1232 "tree_header_wrapper", KATTR__MAX);
1233 if (kerr != KCGI_OK)
1234 goto done;
1235 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1236 "tree_header", KATTR__MAX);
1237 if (kerr != KCGI_OK)
1238 goto done;
1239 error = gw_gen_tree_header(gw_trans, header->tree_id);
1240 if (error)
1241 goto done;
1242 error = gw_get_time_str(&age, header->committer_time,
1243 TM_LONG);
1244 if (error)
1245 goto done;
1246 error = gw_gen_age_header(gw_trans, age ?age : "");
1247 if (error)
1248 goto done;
1250 * XXX: keeping this for now, since kcgihtml does not convert
1251 * \n into <br /> yet.
1253 error = gw_html_escape(&escaped_commit_msg, header->commit_msg);
1254 if (error)
1255 goto done;
1256 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1257 if (error)
1258 goto done;
1259 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1260 if (kerr != KCGI_OK)
1261 goto done;
1262 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1263 "dotted_line", KATTR__MAX);
1264 if (kerr != KCGI_OK)
1265 goto done;
1266 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1267 if (kerr != KCGI_OK)
1268 goto done;
1270 /* tree */
1271 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1272 "tree", KATTR__MAX);
1273 if (kerr != KCGI_OK)
1274 goto done;
1275 error = gw_output_repo_tree(gw_trans);
1276 if (error)
1277 goto done;
1279 /* tree content close */
1280 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1281 done:
1282 got_ref_list_free(&header->refs);
1283 gw_free_headers(header);
1284 free(tree_html_disp);
1285 free(tree_html);
1286 free(tree);
1287 free(age);
1288 free(age_html);
1289 free(escaped_commit_msg);
1290 if (error == NULL && kerr != KCGI_OK)
1291 error = gw_kcgi_error(kerr);
1292 return error;
1295 static const struct got_error *
1296 gw_tag(struct gw_trans *gw_trans)
1298 const struct got_error *error = NULL;
1299 struct gw_header *header = NULL;
1300 char *escaped_commit_msg = NULL;
1301 enum kcgi_err kerr;
1303 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1304 return got_error_from_errno("pledge");
1306 if ((header = gw_init_header()) == NULL)
1307 return got_error_from_errno("malloc");
1309 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
1310 if (error)
1311 goto done;
1313 error = gw_get_header(gw_trans, header, 1);
1314 if (error)
1315 goto done;
1317 /* tag header */
1318 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1319 "tag_header_wrapper", KATTR__MAX);
1320 if (kerr != KCGI_OK)
1321 goto done;
1322 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1323 "tag_header", KATTR__MAX);
1324 if (kerr != KCGI_OK)
1325 goto done;
1326 error = gw_gen_commit_header(gw_trans, header->commit_id,
1327 header->refs_str);
1328 if (error)
1329 goto done;
1331 * XXX: keeping this for now, since kcgihtml does not convert
1332 * \n into <br /> yet.
1334 error = gw_html_escape(&escaped_commit_msg, header->commit_msg);
1335 if (error)
1336 goto done;
1337 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1338 if (error)
1339 goto done;
1340 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1341 if (kerr != KCGI_OK)
1342 goto done;
1343 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1344 "dotted_line", KATTR__MAX);
1345 if (kerr != KCGI_OK)
1346 goto done;
1347 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1348 if (kerr != KCGI_OK)
1349 goto done;
1351 /* tag */
1352 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1353 "tree", KATTR__MAX);
1354 if (kerr != KCGI_OK)
1355 goto done;
1357 error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1358 if (error)
1359 goto done;
1361 /* tag content close */
1362 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1363 done:
1364 got_ref_list_free(&header->refs);
1365 gw_free_headers(header);
1366 free(escaped_commit_msg);
1367 if (error == NULL && kerr != KCGI_OK)
1368 error = gw_kcgi_error(kerr);
1369 return error;
1372 static const struct got_error *
1373 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1375 const struct got_error *error = NULL;
1376 DIR *dt;
1377 char *dir_test;
1378 int opened = 0;
1380 if (asprintf(&dir_test, "%s/%s/%s",
1381 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1382 GOTWEB_GIT_DIR) == -1)
1383 return got_error_from_errno("asprintf");
1385 dt = opendir(dir_test);
1386 if (dt == NULL) {
1387 free(dir_test);
1388 } else {
1389 gw_dir->path = strdup(dir_test);
1390 opened = 1;
1391 goto done;
1394 if (asprintf(&dir_test, "%s/%s/%s",
1395 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1396 GOTWEB_GOT_DIR) == -1)
1397 return got_error_from_errno("asprintf");
1399 dt = opendir(dir_test);
1400 if (dt == NULL)
1401 free(dir_test);
1402 else {
1403 opened = 1;
1404 error = got_error(GOT_ERR_NOT_GIT_REPO);
1405 goto errored;
1408 if (asprintf(&dir_test, "%s/%s",
1409 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1)
1410 return got_error_from_errno("asprintf");
1412 gw_dir->path = strdup(dir_test);
1413 done:
1414 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1415 gw_dir->path);
1416 if (error)
1417 goto errored;
1418 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1419 if (error)
1420 goto errored;
1421 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1422 "refs/heads", TM_DIFF);
1423 if (error)
1424 goto errored;
1425 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1426 errored:
1427 free(dir_test);
1428 if (opened)
1429 closedir(dt);
1430 return error;
1433 static const struct got_error *
1434 gw_load_got_paths(struct gw_trans *gw_trans)
1436 const struct got_error *error = NULL;
1437 DIR *d;
1438 struct dirent **sd_dent;
1439 struct gw_dir *gw_dir;
1440 struct stat st;
1441 unsigned int d_cnt, d_i;
1443 d = opendir(gw_trans->gw_conf->got_repos_path);
1444 if (d == NULL) {
1445 error = got_error_from_errno2("opendir",
1446 gw_trans->gw_conf->got_repos_path);
1447 return error;
1450 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1451 alphasort);
1452 if (d_cnt == -1) {
1453 error = got_error_from_errno2("scandir",
1454 gw_trans->gw_conf->got_repos_path);
1455 return error;
1458 for (d_i = 0; d_i < d_cnt; d_i++) {
1459 if (gw_trans->gw_conf->got_max_repos > 0 &&
1460 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1461 break; /* account for parent and self */
1463 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1464 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1465 continue;
1467 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
1468 return got_error_from_errno("gw_dir malloc");
1470 error = gw_load_got_path(gw_trans, gw_dir);
1471 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1472 error = NULL;
1473 continue;
1475 else if (error)
1476 return error;
1478 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1479 !got_path_dir_is_empty(gw_dir->path)) {
1480 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1481 entry);
1482 gw_trans->repos_total++;
1486 closedir(d);
1487 return error;
1490 static const struct got_error *
1491 gw_parse_querystring(struct gw_trans *gw_trans)
1493 const struct got_error *error = NULL;
1494 struct kpair *p;
1495 struct gw_query_action *action = NULL;
1496 unsigned int i;
1498 if (gw_trans->gw_req->fieldnmap[0]) {
1499 error = got_error_from_errno("bad parse");
1500 return error;
1501 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1502 /* define gw_trans->repo_path */
1503 if (asprintf(&gw_trans->repo_name, "%s", p->parsed.s) == -1)
1504 return got_error_from_errno("asprintf");
1506 if (asprintf(&gw_trans->repo_path, "%s/%s",
1507 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
1508 return got_error_from_errno("asprintf");
1510 /* get action and set function */
1511 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
1512 for (i = 0; i < nitems(gw_query_funcs); i++) {
1513 action = &gw_query_funcs[i];
1514 if (action->func_name == NULL)
1515 continue;
1517 if (strcmp(action->func_name,
1518 p->parsed.s) == 0) {
1519 gw_trans->action = i;
1520 if (asprintf(&gw_trans->action_name,
1521 "%s", action->func_name) == -1)
1522 return
1523 got_error_from_errno(
1524 "asprintf");
1526 break;
1529 action = NULL;
1532 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
1533 if (asprintf(&gw_trans->commit, "%s",
1534 p->parsed.s) == -1)
1535 return got_error_from_errno("asprintf");
1537 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1538 if (asprintf(&gw_trans->repo_file, "%s",
1539 p->parsed.s) == -1)
1540 return got_error_from_errno("asprintf");
1542 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER]))
1543 if (asprintf(&gw_trans->repo_folder, "%s",
1544 p->parsed.s) == -1)
1545 return got_error_from_errno("asprintf");
1547 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
1548 if (asprintf(&gw_trans->headref, "%s",
1549 p->parsed.s) == -1)
1550 return got_error_from_errno("asprintf");
1552 if (action == NULL) {
1553 error = got_error_from_errno("invalid action");
1554 return error;
1556 if ((gw_trans->gw_dir =
1557 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
1558 return got_error_from_errno("gw_dir malloc");
1560 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1561 if (error)
1562 return error;
1563 } else
1564 gw_trans->action = GW_INDEX;
1566 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1567 gw_trans->page = p->parsed.i;
1569 return error;
1572 static struct gw_dir *
1573 gw_init_gw_dir(char *dir)
1575 struct gw_dir *gw_dir;
1577 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
1578 return NULL;
1580 if (asprintf(&gw_dir->name, "%s", dir) == -1)
1581 return NULL;
1583 return gw_dir;
1586 static const struct got_error *
1587 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1589 enum kcgi_err kerr;
1591 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1592 if (kerr != KCGI_OK)
1593 return gw_kcgi_error(kerr);
1594 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1595 khttps[code]);
1596 if (kerr != KCGI_OK)
1597 return gw_kcgi_error(kerr);
1598 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1599 kmimetypes[mime]);
1600 if (kerr != KCGI_OK)
1601 return gw_kcgi_error(kerr);
1602 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
1603 "nosniff");
1604 if (kerr != KCGI_OK)
1605 return gw_kcgi_error(kerr);
1606 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1607 if (kerr != KCGI_OK)
1608 return gw_kcgi_error(kerr);
1609 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
1610 "1; mode=block");
1611 if (kerr != KCGI_OK)
1612 return gw_kcgi_error(kerr);
1614 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
1615 kerr = khttp_head(gw_trans->gw_req,
1616 kresps[KRESP_CONTENT_DISPOSITION],
1617 "attachment; filename=%s", gw_trans->repo_file);
1618 if (kerr != KCGI_OK)
1619 return gw_kcgi_error(kerr);
1622 kerr = khttp_body(gw_trans->gw_req);
1623 return gw_kcgi_error(kerr);
1626 static const struct got_error *
1627 gw_display_index(struct gw_trans *gw_trans)
1629 const struct got_error *error;
1630 enum kcgi_err kerr;
1632 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1633 if (error)
1634 return error;
1636 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1637 if (kerr != KCGI_OK)
1638 return gw_kcgi_error(kerr);
1640 if (gw_trans->action != GW_BLOB) {
1641 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1642 gw_query_funcs[gw_trans->action].template);
1643 if (kerr != KCGI_OK) {
1644 khtml_close(gw_trans->gw_html_req);
1645 return gw_kcgi_error(kerr);
1649 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
1652 static void
1653 gw_display_error(struct gw_trans *gw_trans, const struct got_error *err)
1655 if (gw_display_open(gw_trans, KHTTP_200, gw_trans->mime) != NULL)
1656 return;
1658 if (khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0) != KCGI_OK)
1659 return;
1660 khtml_puts(gw_trans->gw_html_req, err->msg);
1661 khtml_close(gw_trans->gw_html_req);
1664 static int
1665 gw_template(size_t key, void *arg)
1667 const struct got_error *error = NULL;
1668 enum kcgi_err kerr;
1669 struct gw_trans *gw_trans = arg;
1670 char *gw_site_link, *img_src = NULL;
1672 switch (key) {
1673 case (TEMPL_HEAD):
1674 kerr = khttp_puts(gw_trans->gw_req, head);
1675 if (kerr != KCGI_OK)
1676 return 0;
1677 break;
1678 case(TEMPL_HEADER):
1679 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1680 KATTR_ID, "got_link", KATTR__MAX);
1681 if (kerr != KCGI_OK)
1682 return 0;
1683 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1684 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
1685 KATTR_TARGET, "_sotd", KATTR__MAX);
1686 if (kerr != KCGI_OK)
1687 return 0;
1688 if (asprintf(&img_src, "/%s",
1689 gw_trans->gw_conf->got_logo) == -1)
1690 return 0;
1691 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
1692 KATTR_SRC, img_src, KATTR__MAX);
1693 if (kerr != KCGI_OK) {
1694 free(img_src);
1695 return 0;
1697 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1698 if (kerr != KCGI_OK) {
1699 free(img_src);
1700 return 0;
1702 break;
1703 case (TEMPL_SITEPATH):
1704 gw_site_link = gw_get_site_link(gw_trans);
1705 if (gw_site_link != NULL) {
1706 kerr = khttp_puts(gw_trans->gw_req, gw_site_link);
1707 if (kerr != KCGI_OK) {
1708 free(gw_site_link);
1709 return 0;
1712 free(gw_site_link);
1713 break;
1714 case(TEMPL_TITLE):
1715 if (gw_trans->gw_conf->got_site_name != NULL) {
1716 kerr = khtml_puts(gw_trans->gw_html_req,
1717 gw_trans->gw_conf->got_site_name);
1718 if (kerr != KCGI_OK)
1719 return 0;
1721 break;
1722 case (TEMPL_SEARCH):
1723 kerr = khttp_puts(gw_trans->gw_req, search);
1724 if (kerr != KCGI_OK)
1725 return 0;
1726 break;
1727 case(TEMPL_SITEOWNER):
1728 if (gw_trans->gw_conf->got_site_owner != NULL &&
1729 gw_trans->gw_conf->got_show_site_owner) {
1730 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1731 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
1732 if (kerr != KCGI_OK)
1733 return 0;
1734 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1735 KATTR_ID, "site_owner", KATTR__MAX);
1736 if (kerr != KCGI_OK)
1737 return 0;
1738 kerr = khtml_puts(gw_trans->gw_html_req,
1739 gw_trans->gw_conf->got_site_owner);
1740 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1741 if (kerr != KCGI_OK)
1742 return 0;
1744 break;
1745 case(TEMPL_CONTENT):
1746 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1747 if (error) {
1748 kerr = khttp_puts(gw_trans->gw_req, error->msg);
1749 if (kerr != KCGI_OK)
1750 return 0;
1752 break;
1753 default:
1754 return 0;
1756 return 1;
1759 static const struct got_error *
1760 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
1762 const struct got_error *error = NULL;
1763 char *ref_str = NULL;
1764 enum kcgi_err kerr = KCGI_OK;
1766 if (strcmp(str2, "") != 0) {
1767 if (asprintf(&ref_str, "(%s)", str2) == -1) {
1768 error = got_error_from_errno("asprintf");
1769 goto done;
1771 } else
1772 ref_str = strdup("");
1774 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1775 KATTR_ID, "header_commit_title", KATTR__MAX);
1776 if (kerr != KCGI_OK)
1777 goto done;
1778 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
1779 if (kerr != KCGI_OK)
1780 goto done;
1781 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1782 if (kerr != KCGI_OK)
1783 goto done;
1784 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1785 KATTR_ID, "header_commit", KATTR__MAX);
1786 if (kerr != KCGI_OK)
1787 goto done;
1788 kerr = khtml_puts(gw_trans->gw_html_req, str1);
1789 if (kerr != KCGI_OK)
1790 goto done;
1791 kerr = khtml_puts(gw_trans->gw_html_req, " ");
1792 if (kerr != KCGI_OK)
1793 goto done;
1794 kerr = khtml_puts(gw_trans->gw_html_req, ref_str);
1795 if (kerr != KCGI_OK)
1796 goto done;
1797 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1798 done:
1799 if (error == NULL && kerr != KCGI_OK)
1800 error = gw_kcgi_error(kerr);
1801 return error;
1804 static const struct got_error *
1805 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
1807 const struct got_error *error = NULL;
1808 enum kcgi_err kerr = KCGI_OK;
1810 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1811 KATTR_ID, "header_diff_title", KATTR__MAX);
1812 if (kerr != KCGI_OK)
1813 goto done;
1814 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
1815 if (kerr != KCGI_OK)
1816 goto done;
1817 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1818 if (kerr != KCGI_OK)
1819 goto done;
1820 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1821 KATTR_ID, "header_diff", KATTR__MAX);
1822 if (kerr != KCGI_OK)
1823 goto done;
1824 kerr = khtml_puts(gw_trans->gw_html_req, str1);
1825 if (kerr != KCGI_OK)
1826 goto done;
1827 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
1828 if (kerr != KCGI_OK)
1829 goto done;
1830 kerr = khtml_puts(gw_trans->gw_html_req, str2);
1831 if (kerr != KCGI_OK)
1832 goto done;
1833 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1834 done:
1835 if (error == NULL && kerr != KCGI_OK)
1836 error = gw_kcgi_error(kerr);
1837 return error;
1840 static const struct got_error *
1841 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
1843 const struct got_error *error = NULL;
1844 enum kcgi_err kerr = KCGI_OK;
1846 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1847 KATTR_ID, "header_age_title", KATTR__MAX);
1848 if (kerr != KCGI_OK)
1849 goto done;
1850 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
1851 if (kerr != KCGI_OK)
1852 goto done;
1853 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1854 if (kerr != KCGI_OK)
1855 goto done;
1856 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1857 KATTR_ID, "header_age", KATTR__MAX);
1858 if (kerr != KCGI_OK)
1859 goto done;
1860 kerr = khtml_puts(gw_trans->gw_html_req, str);
1861 if (kerr != KCGI_OK)
1862 goto done;
1863 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1864 done:
1865 if (error == NULL && kerr != KCGI_OK)
1866 error = gw_kcgi_error(kerr);
1867 return error;
1870 static const struct got_error *
1871 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
1873 const struct got_error *error = NULL;
1874 enum kcgi_err kerr;
1876 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1877 KATTR_ID, "header_author_title", KATTR__MAX);
1878 if (kerr != KCGI_OK)
1879 goto done;
1880 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
1881 if (kerr != KCGI_OK)
1882 goto done;
1883 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1884 if (kerr != KCGI_OK)
1885 goto done;
1886 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1887 KATTR_ID, "header_author", KATTR__MAX);
1888 if (kerr != KCGI_OK)
1889 goto done;
1890 kerr = khtml_puts(gw_trans->gw_html_req, str);
1891 if (kerr != KCGI_OK)
1892 goto done;
1893 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1894 done:
1895 if (error == NULL && kerr != KCGI_OK)
1896 error = gw_kcgi_error(kerr);
1897 return error;
1900 static const struct got_error *
1901 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
1903 const struct got_error *error = NULL;
1904 enum kcgi_err kerr;
1906 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1907 KATTR_ID, "header_committer_title", KATTR__MAX);
1908 if (kerr != KCGI_OK)
1909 goto done;
1910 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
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 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1917 KATTR_ID, "header_committer", KATTR__MAX);
1918 if (kerr != KCGI_OK)
1919 goto done;
1920 kerr = khtml_puts(gw_trans->gw_html_req, str);
1921 if (kerr != KCGI_OK)
1922 goto done;
1923 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1924 done:
1925 if (error == NULL && kerr != KCGI_OK)
1926 error = gw_kcgi_error(kerr);
1927 return error;
1930 static const struct got_error *
1931 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
1933 const struct got_error *error = NULL;
1934 enum kcgi_err kerr;
1936 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1937 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
1938 if (kerr != KCGI_OK)
1939 goto done;
1940 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
1941 if (kerr != KCGI_OK)
1942 goto done;
1943 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1944 if (kerr != KCGI_OK)
1945 goto done;
1946 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1947 KATTR_ID, "header_commit_msg", KATTR__MAX);
1948 if (kerr != KCGI_OK)
1949 goto done;
1950 kerr = khttp_puts(gw_trans->gw_req, str);
1951 if (kerr != KCGI_OK)
1952 goto done;
1953 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1954 done:
1955 if (error == NULL && kerr != KCGI_OK)
1956 error = gw_kcgi_error(kerr);
1957 return error;
1960 static const struct got_error *
1961 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
1963 const struct got_error *error = NULL;
1964 enum kcgi_err kerr;
1966 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1967 KATTR_ID, "header_tree_title", KATTR__MAX);
1968 if (kerr != KCGI_OK)
1969 goto done;
1970 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
1971 if (kerr != KCGI_OK)
1972 goto done;
1973 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1974 if (kerr != KCGI_OK)
1975 goto done;
1976 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1977 KATTR_ID, "header_tree", KATTR__MAX);
1978 if (kerr != KCGI_OK)
1979 goto done;
1980 kerr = khtml_puts(gw_trans->gw_html_req, str);
1981 if (kerr != KCGI_OK)
1982 goto done;
1983 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1984 done:
1985 if (error == NULL && kerr != KCGI_OK)
1986 error = gw_kcgi_error(kerr);
1987 return error;
1990 static const struct got_error *
1991 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
1992 char *dir)
1994 const struct got_error *error = NULL;
1995 FILE *f = NULL;
1996 char *d_file = NULL;
1997 unsigned int len;
1998 size_t n;
2000 *description = NULL;
2001 if (gw_trans->gw_conf->got_show_repo_description == 0)
2002 return gw_empty_string(description);
2004 if (asprintf(&d_file, "%s/description", dir) == -1)
2005 return got_error_from_errno("asprintf");
2007 f = fopen(d_file, "r");
2008 if (f == NULL) {
2009 if (errno == ENOENT || errno == EACCES)
2010 return gw_empty_string(description);
2011 error = got_error_from_errno2("fopen", d_file);
2012 goto done;
2015 if (fseek(f, 0, SEEK_END) == -1) {
2016 error = got_ferror(f, GOT_ERR_IO);
2017 goto done;
2019 len = ftell(f);
2020 if (len == -1) {
2021 error = got_ferror(f, GOT_ERR_IO);
2022 goto done;
2024 if (fseek(f, 0, SEEK_SET) == -1) {
2025 error = got_ferror(f, GOT_ERR_IO);
2026 goto done;
2028 *description = calloc(len + 1, sizeof(**description));
2029 if (*description == NULL) {
2030 error = got_error_from_errno("calloc");
2031 goto done;
2034 n = fread(*description, 1, len, f);
2035 if (n == 0 && ferror(f))
2036 error = got_ferror(f, GOT_ERR_IO);
2037 done:
2038 if (f != NULL && fclose(f) == -1 && error == NULL)
2039 error = got_error_from_errno("fclose");
2040 free(d_file);
2041 return error;
2044 static const struct got_error *
2045 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2047 struct tm tm;
2048 time_t diff_time;
2049 char *years = "years ago", *months = "months ago";
2050 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2051 char *minutes = "minutes ago", *seconds = "seconds ago";
2052 char *now = "right now";
2053 char *s;
2054 char datebuf[29];
2056 *repo_age = NULL;
2058 switch (ref_tm) {
2059 case TM_DIFF:
2060 diff_time = time(NULL) - committer_time;
2061 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2062 if (asprintf(repo_age, "%lld %s",
2063 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2064 return got_error_from_errno("asprintf");
2065 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2066 if (asprintf(repo_age, "%lld %s",
2067 (diff_time / 60 / 60 / 24 / (365 / 12)),
2068 months) == -1)
2069 return got_error_from_errno("asprintf");
2070 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2071 if (asprintf(repo_age, "%lld %s",
2072 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2073 return got_error_from_errno("asprintf");
2074 } else if (diff_time > 60 * 60 * 24 * 2) {
2075 if (asprintf(repo_age, "%lld %s",
2076 (diff_time / 60 / 60 / 24), days) == -1)
2077 return got_error_from_errno("asprintf");
2078 } else if (diff_time > 60 * 60 * 2) {
2079 if (asprintf(repo_age, "%lld %s",
2080 (diff_time / 60 / 60), hours) == -1)
2081 return got_error_from_errno("asprintf");
2082 } else if (diff_time > 60 * 2) {
2083 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2084 minutes) == -1)
2085 return got_error_from_errno("asprintf");
2086 } else if (diff_time > 2) {
2087 if (asprintf(repo_age, "%lld %s", diff_time,
2088 seconds) == -1)
2089 return got_error_from_errno("asprintf");
2090 } else {
2091 if (asprintf(repo_age, "%s", now) == -1)
2092 return got_error_from_errno("asprintf");
2094 break;
2095 case TM_LONG:
2096 if (gmtime_r(&committer_time, &tm) == NULL)
2097 return got_error_from_errno("gmtime_r");
2099 s = asctime_r(&tm, datebuf);
2100 if (s == NULL)
2101 return got_error_from_errno("asctime_r");
2103 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2104 return got_error_from_errno("asprintf");
2105 break;
2107 return NULL;
2110 static const struct got_error *
2111 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2112 char *repo_ref, int ref_tm)
2114 const struct got_error *error = NULL;
2115 struct got_object_id *id = NULL;
2116 struct got_repository *repo = NULL;
2117 struct got_commit_object *commit = NULL;
2118 struct got_reflist_head refs;
2119 struct got_reflist_entry *re;
2120 struct got_reference *head_ref;
2121 int is_head = 0;
2122 time_t committer_time = 0, cmp_time = 0;
2123 const char *refname;
2125 *repo_age = NULL;
2126 SIMPLEQ_INIT(&refs);
2128 if (repo_ref == NULL)
2129 return NULL;
2131 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
2132 is_head = 1;
2134 if (gw_trans->gw_conf->got_show_repo_age == 0)
2135 return NULL;
2137 error = got_repo_open(&repo, dir, NULL);
2138 if (error)
2139 goto done;
2141 if (is_head)
2142 error = got_ref_list(&refs, repo, "refs/heads",
2143 got_ref_cmp_by_name, NULL);
2144 else
2145 error = got_ref_list(&refs, repo, repo_ref,
2146 got_ref_cmp_by_name, NULL);
2147 if (error)
2148 goto done;
2150 SIMPLEQ_FOREACH(re, &refs, entry) {
2151 if (is_head)
2152 refname = strdup(repo_ref);
2153 else
2154 refname = got_ref_get_name(re->ref);
2155 error = got_ref_open(&head_ref, repo, refname, 0);
2156 if (error)
2157 goto done;
2159 error = got_ref_resolve(&id, repo, head_ref);
2160 got_ref_close(head_ref);
2161 if (error)
2162 goto done;
2164 error = got_object_open_as_commit(&commit, repo, id);
2165 if (error)
2166 goto done;
2168 committer_time =
2169 got_object_commit_get_committer_time(commit);
2171 if (cmp_time < committer_time)
2172 cmp_time = committer_time;
2175 if (cmp_time != 0) {
2176 committer_time = cmp_time;
2177 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2179 done:
2180 got_ref_list_free(&refs);
2181 free(id);
2182 return error;
2185 static const struct got_error *
2186 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2188 const struct got_error *error;
2189 FILE *f = NULL;
2190 struct got_object_id *id1 = NULL, *id2 = NULL;
2191 char *label1 = NULL, *label2 = NULL, *line = NULL;
2192 int obj_type;
2193 size_t linesize = 0;
2194 ssize_t linelen;
2195 enum kcgi_err kerr = KCGI_OK;
2197 f = got_opentemp();
2198 if (f == NULL)
2199 return NULL;
2201 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
2202 if (error)
2203 goto done;
2205 if (strncmp(header->parent_id, "/dev/null", 9) != 0) {
2206 error = got_repo_match_object_id(&id1, &label1,
2207 header->parent_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2208 if (error)
2209 goto done;
2212 error = got_repo_match_object_id(&id2, &label2,
2213 header->commit_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2214 if (error)
2215 goto done;
2217 error = got_object_get_type(&obj_type, header->repo, id2);
2218 if (error)
2219 goto done;
2220 switch (obj_type) {
2221 case GOT_OBJ_TYPE_BLOB:
2222 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
2223 header->repo, f);
2224 break;
2225 case GOT_OBJ_TYPE_TREE:
2226 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
2227 header->repo, f);
2228 break;
2229 case GOT_OBJ_TYPE_COMMIT:
2230 error = got_diff_objects_as_commits(id1, id2, 3, 0,
2231 header->repo, f);
2232 break;
2233 default:
2234 error = got_error(GOT_ERR_OBJ_TYPE);
2236 if (error)
2237 goto done;
2239 if (fseek(f, 0, SEEK_SET) == -1) {
2240 error = got_ferror(f, GOT_ERR_IO);
2241 goto done;
2244 while ((linelen = getline(&line, &linesize, f)) != -1) {
2245 error = gw_colordiff_line(gw_trans, line);
2246 if (error)
2247 goto done;
2248 /* XXX: KHTML_PRETTY breaks this */
2249 kerr = khtml_puts(gw_trans->gw_html_req, line);
2250 if (kerr != KCGI_OK)
2251 goto done;
2252 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2253 if (kerr != KCGI_OK)
2254 goto done;
2256 if (linelen == -1 && ferror(f))
2257 error = got_error_from_errno("getline");
2258 done:
2259 if (f && fclose(f) == -1 && error == NULL)
2260 error = got_error_from_errno("fclose");
2261 free(line);
2262 free(label1);
2263 free(label2);
2264 free(id1);
2265 free(id2);
2267 if (error == NULL && kerr != KCGI_OK)
2268 error = gw_kcgi_error(kerr);
2269 return error;
2272 static const struct got_error *
2273 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2275 const struct got_error *error = NULL;
2276 struct got_repository *repo;
2277 const char *gitconfig_owner;
2279 *owner = NULL;
2281 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2282 return NULL;
2284 error = got_repo_open(&repo, dir, NULL);
2285 if (error)
2286 return error;
2287 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2288 if (gitconfig_owner) {
2289 *owner = strdup(gitconfig_owner);
2290 if (*owner == NULL)
2291 error = got_error_from_errno("strdup");
2293 got_repo_close(repo);
2294 return error;
2297 static const struct got_error *
2298 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2300 const struct got_error *error = NULL;
2301 FILE *f;
2302 char *d_file = NULL;
2303 unsigned int len;
2304 size_t n;
2306 *url = NULL;
2308 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2309 return got_error_from_errno("asprintf");
2311 f = fopen(d_file, "r");
2312 if (f == NULL) {
2313 if (errno != ENOENT && errno != EACCES)
2314 error = got_error_from_errno2("fopen", d_file);
2315 goto done;
2318 if (fseek(f, 0, SEEK_END) == -1) {
2319 error = got_ferror(f, GOT_ERR_IO);
2320 goto done;
2322 len = ftell(f);
2323 if (len == -1) {
2324 error = got_ferror(f, GOT_ERR_IO);
2325 goto done;
2327 if (fseek(f, 0, SEEK_SET) == -1) {
2328 error = got_ferror(f, GOT_ERR_IO);
2329 goto done;
2332 *url = calloc(len + 1, sizeof(**url));
2333 if (*url == NULL) {
2334 error = got_error_from_errno("calloc");
2335 goto done;
2338 n = fread(*url, 1, len, f);
2339 if (n == 0 && ferror(f))
2340 error = got_ferror(f, GOT_ERR_IO);
2341 done:
2342 if (f && fclose(f) == -1 && error == NULL)
2343 error = got_error_from_errno("fclose");
2344 free(d_file);
2345 return NULL;
2348 static const struct got_error *
2349 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
2350 int limit, int tag_type)
2352 const struct got_error *error = NULL;
2353 struct got_repository *repo = NULL;
2354 struct got_reflist_head refs;
2355 struct got_reflist_entry *re;
2356 char *age = NULL;
2357 char *escaped_tag_commit = NULL;
2358 char *id_str = NULL, *refstr = NULL, *newline, *href_commits = NULL;
2359 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
2360 struct got_tag_object *tag = NULL;
2361 enum kcgi_err kerr = KCGI_OK;
2362 int summary_header_displayed = 0;
2364 SIMPLEQ_INIT(&refs);
2366 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2367 if (error)
2368 goto done;
2370 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
2371 if (error)
2372 goto done;
2374 SIMPLEQ_FOREACH(re, &refs, entry) {
2375 const char *refname;
2376 const char *tagger;
2377 const char *tag_commit;
2378 time_t tagger_time;
2379 struct got_object_id *id;
2381 refname = got_ref_get_name(re->ref);
2382 if (strncmp(refname, "refs/tags/", 10) != 0)
2383 continue;
2384 refname += 10;
2385 refstr = got_ref_to_str(re->ref);
2386 if (refstr == NULL) {
2387 error = got_error_from_errno("got_ref_to_str");
2388 goto done;
2391 error = got_ref_resolve(&id, repo, re->ref);
2392 if (error)
2393 goto done;
2396 * XXX: some of my repos are failing here. need to investigate.
2397 * currently setting error to NULL so no error is returned,
2398 * which stops Heads from being displayed on gw_summary.
2400 * got ref -l lists refs and first tag ref above can be
2401 * displayed
2403 * got tag -l will list tags just fine, so I don't know what
2404 * is happening.
2406 error = got_object_open_as_tag(&tag, repo, id);
2407 free(id);
2408 if (error) {
2409 error = NULL;
2410 goto done;
2413 tagger = got_object_tag_get_tagger(tag);
2414 tagger_time = got_object_tag_get_tagger_time(tag);
2416 error = got_object_id_str(&id_str,
2417 got_object_tag_get_object_id(tag));
2418 if (error)
2419 goto done;
2421 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
2422 strlen(id_str)) != 0)
2423 continue;
2425 tag_commit0 = strdup(got_object_tag_get_message(tag));
2426 if (tag_commit0 == NULL) {
2427 error = got_error_from_errno("strdup");
2428 goto done;
2431 tag_commit = tag_commit0;
2432 while (*tag_commit == '\n')
2433 tag_commit++;
2435 switch (tag_type) {
2436 case TAGBRIEF:
2437 newline = strchr(tag_commit, '\n');
2438 if (newline)
2439 *newline = '\0';
2441 if (summary_header_displayed == 0) {
2442 kerr = khtml_attr(gw_trans->gw_html_req,
2443 KELEM_DIV, KATTR_ID,
2444 "summary_tags_title_wrapper", KATTR__MAX);
2445 if (kerr != KCGI_OK)
2446 goto done;
2447 kerr = khtml_attr(gw_trans->gw_html_req,
2448 KELEM_DIV, KATTR_ID,
2449 "summary_tags_title", KATTR__MAX);
2450 if (kerr != KCGI_OK)
2451 goto done;
2452 kerr = khtml_puts(gw_trans->gw_html_req,
2453 "Tags");
2454 if (kerr != KCGI_OK)
2455 goto done;
2456 kerr = khtml_closeelem(gw_trans->gw_html_req,
2457 2);
2458 if (kerr != KCGI_OK)
2459 goto done;
2460 kerr = khtml_attr(gw_trans->gw_html_req,
2461 KELEM_DIV, KATTR_ID,
2462 "summary_tags_content", KATTR__MAX);
2463 if (kerr != KCGI_OK)
2464 goto done;
2465 summary_header_displayed = 1;
2468 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2469 KATTR_ID, "tags_wrapper", KATTR__MAX);
2470 if (kerr != KCGI_OK)
2471 goto done;
2472 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2473 KATTR_ID, "tags_age", KATTR__MAX);
2474 if (kerr != KCGI_OK)
2475 goto done;
2476 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
2477 if (error)
2478 goto done;
2479 kerr = khtml_puts(gw_trans->gw_html_req,
2480 age ? age : "");
2481 if (kerr != KCGI_OK)
2482 goto done;
2483 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2484 if (kerr != KCGI_OK)
2485 goto done;
2486 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2487 KATTR_ID, "tags", KATTR__MAX);
2488 if (kerr != KCGI_OK)
2489 goto done;
2490 kerr = khtml_puts(gw_trans->gw_html_req, refname);
2491 if (kerr != KCGI_OK)
2492 goto done;
2493 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2494 if (kerr != KCGI_OK)
2495 goto done;
2496 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2497 KATTR_ID, "tags_name", KATTR__MAX);
2498 if (kerr != KCGI_OK)
2499 goto done;
2500 if (asprintf(&href_tag, "?path=%s&action=tag&commit=%s",
2501 gw_trans->repo_name, id_str) == -1) {
2502 error = got_error_from_errno("asprintf");
2503 goto done;
2505 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2506 KATTR_HREF, href_tag, KATTR__MAX);
2507 if (kerr != KCGI_OK)
2508 goto done;
2509 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
2510 if (kerr != KCGI_OK)
2511 goto done;
2512 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2513 if (kerr != KCGI_OK)
2514 goto done;
2516 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2517 KATTR_ID, "navs_wrapper", KATTR__MAX);
2518 if (kerr != KCGI_OK)
2519 goto done;
2520 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2521 KATTR_ID, "navs", KATTR__MAX);
2522 if (kerr != KCGI_OK)
2523 goto done;
2525 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2526 KATTR_HREF, href_tag, KATTR__MAX);
2527 if (kerr != KCGI_OK)
2528 goto done;
2529 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
2530 if (kerr != KCGI_OK)
2531 goto done;
2532 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2533 if (kerr != KCGI_OK)
2534 goto done;
2536 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2537 if (kerr != KCGI_OK)
2538 goto done;
2540 if (asprintf(&href_briefs,
2541 "?path=%s&action=briefs&commit=%s",
2542 gw_trans->repo_name, id_str) == -1) {
2543 error = got_error_from_errno("asprintf");
2544 goto done;
2546 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2547 KATTR_HREF, href_briefs, KATTR__MAX);
2548 if (kerr != KCGI_OK)
2549 goto done;
2550 kerr = khtml_puts(gw_trans->gw_html_req,
2551 "commit briefs");
2552 if (kerr != KCGI_OK)
2553 goto done;
2554 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2555 if (kerr != KCGI_OK)
2556 goto done;
2558 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2559 if (kerr != KCGI_OK)
2560 goto done;
2562 if (asprintf(&href_commits,
2563 "?path=%s&action=commits&commit=%s",
2564 gw_trans->repo_name, id_str) == -1) {
2565 error = got_error_from_errno("asprintf");
2566 goto done;
2568 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2569 KATTR_HREF, href_commits, KATTR__MAX);
2570 if (kerr != KCGI_OK)
2571 goto done;
2572 kerr = khtml_puts(gw_trans->gw_html_req,
2573 "commits");
2574 if (kerr != KCGI_OK)
2575 goto done;
2576 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2577 if (kerr != KCGI_OK)
2578 goto done;
2580 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2581 KATTR_ID, "dotted_line", KATTR__MAX);
2582 if (kerr != KCGI_OK)
2583 goto done;
2584 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2585 if (kerr != KCGI_OK)
2586 goto done;
2587 break;
2588 case TAGFULL:
2589 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2590 KATTR_ID, "tag_info_date_title", KATTR__MAX);
2591 if (kerr != KCGI_OK)
2592 goto done;
2593 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
2594 if (kerr != KCGI_OK)
2595 goto done;
2596 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2597 if (kerr != KCGI_OK)
2598 goto done;
2599 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2600 KATTR_ID, "tag_info_date", KATTR__MAX);
2601 if (kerr != KCGI_OK)
2602 goto done;
2603 error = gw_get_time_str(&age, tagger_time, TM_LONG);
2604 if (error)
2605 goto done;
2606 kerr = khtml_puts(gw_trans->gw_html_req,
2607 age ? age : "");
2608 if (kerr != KCGI_OK)
2609 goto done;
2610 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2611 if (kerr != KCGI_OK)
2612 goto done;
2614 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2615 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
2616 if (kerr != KCGI_OK)
2617 goto done;
2618 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
2619 if (kerr != KCGI_OK)
2620 goto done;
2621 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2622 if (kerr != KCGI_OK)
2623 goto done;
2624 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2625 KATTR_ID, "tag_info_date", KATTR__MAX);
2626 if (kerr != KCGI_OK)
2627 goto done;
2628 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
2629 if (kerr != KCGI_OK)
2630 goto done;
2631 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2632 if (kerr != KCGI_OK)
2633 goto done;
2635 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2636 KATTR_ID, "tag_info", KATTR__MAX);
2637 if (kerr != KCGI_OK)
2638 goto done;
2640 * XXX: keeping this for now, since kcgihtml does not
2641 * convert \n into <br /> yet.
2643 error = gw_html_escape(&escaped_tag_commit, tag_commit);
2644 if (error)
2645 goto done;
2646 kerr = khttp_puts(gw_trans->gw_req, escaped_tag_commit);
2647 if (kerr != KCGI_OK)
2648 goto done;
2649 break;
2650 default:
2651 break;
2653 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2654 if (kerr != KCGI_OK)
2655 goto done;
2657 if (limit && --limit == 0)
2658 break;
2660 got_object_tag_close(tag);
2661 tag = NULL;
2662 free(id_str);
2663 id_str = NULL;
2664 free(refstr);
2665 refstr = NULL;
2666 free(age);
2667 age = NULL;
2668 free(escaped_tag_commit);
2669 escaped_tag_commit = NULL;
2670 free(tag_commit0);
2671 tag_commit0 = NULL;
2672 free(href_tag);
2673 href_tag = NULL;
2674 free(href_briefs);
2675 href_briefs = NULL;
2676 free(href_commits);
2677 href_commits = NULL;
2679 done:
2680 if (tag)
2681 got_object_tag_close(tag);
2682 free(id_str);
2683 free(refstr);
2684 free(age);
2685 free(escaped_tag_commit);
2686 free(tag_commit0);
2687 free(href_tag);
2688 free(href_briefs);
2689 free(href_commits);
2690 got_ref_list_free(&refs);
2691 if (repo)
2692 got_repo_close(repo);
2693 if (error == NULL && kerr != KCGI_OK)
2694 error = gw_kcgi_error(kerr);
2695 return error;
2698 static void
2699 gw_free_headers(struct gw_header *header)
2701 free(header->id);
2702 free(header->path);
2703 if (header->commit != NULL)
2704 got_object_commit_close(header->commit);
2705 if (header->repo)
2706 got_repo_close(header->repo);
2707 free(header->refs_str);
2708 free(header->commit_id);
2709 free(header->parent_id);
2710 free(header->tree_id);
2711 free(header->commit_msg);
2714 static struct gw_header *
2715 gw_init_header()
2717 struct gw_header *header;
2719 header = malloc(sizeof(*header));
2720 if (header == NULL)
2721 return NULL;
2723 header->repo = NULL;
2724 header->commit = NULL;
2725 header->id = NULL;
2726 header->path = NULL;
2727 SIMPLEQ_INIT(&header->refs);
2729 return header;
2732 static const struct got_error *
2733 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
2734 int limit)
2736 const struct got_error *error = NULL;
2737 struct got_commit_graph *graph = NULL;
2739 error = got_commit_graph_open(&graph, header->path, 0);
2740 if (error)
2741 goto done;
2743 error = got_commit_graph_iter_start(graph, header->id, header->repo,
2744 NULL, NULL);
2745 if (error)
2746 goto done;
2748 for (;;) {
2749 error = got_commit_graph_iter_next(&header->id, graph,
2750 header->repo, NULL, NULL);
2751 if (error) {
2752 if (error->code == GOT_ERR_ITER_COMPLETED)
2753 error = NULL;
2754 goto done;
2756 if (header->id == NULL)
2757 goto done;
2759 error = got_object_open_as_commit(&header->commit, header->repo,
2760 header->id);
2761 if (error)
2762 goto done;
2764 error = gw_get_commit(gw_trans, header);
2765 if (limit > 1) {
2766 struct gw_header *n_header = NULL;
2767 if ((n_header = gw_init_header()) == NULL) {
2768 error = got_error_from_errno("malloc");
2769 goto done;
2772 n_header->refs_str = strdup(header->refs_str);
2773 n_header->commit_id = strdup(header->commit_id);
2774 n_header->parent_id = strdup(header->parent_id);
2775 n_header->tree_id = strdup(header->tree_id);
2776 n_header->author = strdup(header->author);
2777 n_header->committer = strdup(header->committer);
2778 n_header->commit_msg = strdup(header->commit_msg);
2779 n_header->committer_time = header->committer_time;
2780 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
2781 entry);
2783 if (error || (limit && --limit == 0))
2784 break;
2786 done:
2787 if (graph)
2788 got_commit_graph_close(graph);
2789 return error;
2792 static const struct got_error *
2793 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
2795 const struct got_error *error = NULL;
2796 struct got_reflist_entry *re;
2797 struct got_object_id *id2 = NULL;
2798 struct got_object_qid *parent_id;
2799 char *refs_str = NULL, *commit_msg = NULL, *commit_msg0;
2801 /*print commit*/
2802 SIMPLEQ_FOREACH(re, &header->refs, entry) {
2803 char *s;
2804 const char *name;
2805 struct got_tag_object *tag = NULL;
2806 int cmp;
2808 name = got_ref_get_name(re->ref);
2809 if (strcmp(name, GOT_REF_HEAD) == 0)
2810 continue;
2811 if (strncmp(name, "refs/", 5) == 0)
2812 name += 5;
2813 if (strncmp(name, "got/", 4) == 0)
2814 continue;
2815 if (strncmp(name, "heads/", 6) == 0)
2816 name += 6;
2817 if (strncmp(name, "remotes/", 8) == 0)
2818 name += 8;
2819 if (strncmp(name, "tags/", 5) == 0) {
2820 error = got_object_open_as_tag(&tag, header->repo,
2821 re->id);
2822 if (error) {
2823 if (error->code != GOT_ERR_OBJ_TYPE)
2824 continue;
2826 * Ref points at something other
2827 * than a tag.
2829 error = NULL;
2830 tag = NULL;
2833 cmp = got_object_id_cmp(tag ?
2834 got_object_tag_get_object_id(tag) : re->id, header->id);
2835 if (tag)
2836 got_object_tag_close(tag);
2837 if (cmp != 0)
2838 continue;
2839 s = refs_str;
2840 if (asprintf(&refs_str, "%s%s%s", s ? s : "",
2841 s ? ", " : "", name) == -1) {
2842 error = got_error_from_errno("asprintf");
2843 free(s);
2844 return error;
2846 header->refs_str = strdup(refs_str);
2847 free(s);
2850 if (refs_str == NULL)
2851 header->refs_str = strdup("");
2852 free(refs_str);
2854 error = got_object_id_str(&header->commit_id, header->id);
2855 if (error)
2856 return error;
2858 error = got_object_id_str(&header->tree_id,
2859 got_object_commit_get_tree_id(header->commit));
2860 if (error)
2861 return error;
2863 if (gw_trans->action == GW_DIFF) {
2864 parent_id = SIMPLEQ_FIRST(
2865 got_object_commit_get_parent_ids(header->commit));
2866 if (parent_id != NULL) {
2867 id2 = got_object_id_dup(parent_id->id);
2868 free (parent_id);
2869 error = got_object_id_str(&header->parent_id, id2);
2870 if (error)
2871 return error;
2872 free(id2);
2873 } else
2874 header->parent_id = strdup("/dev/null");
2875 } else
2876 header->parent_id = strdup("");
2878 header->committer_time =
2879 got_object_commit_get_committer_time(header->commit);
2881 header->author =
2882 got_object_commit_get_author(header->commit);
2883 header->committer =
2884 got_object_commit_get_committer(header->commit);
2885 if (error)
2886 return error;
2888 /* XXX Doesn't the log message require escaping? */
2889 error = got_object_commit_get_logmsg(&commit_msg0, header->commit);
2890 if (error)
2891 return error;
2893 commit_msg = commit_msg0;
2894 while (*commit_msg == '\n')
2895 commit_msg++;
2897 header->commit_msg = strdup(commit_msg);
2898 free(commit_msg0);
2899 return error;
2902 static const struct got_error *
2903 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
2905 const struct got_error *error = NULL;
2906 char *in_repo_path = NULL;
2908 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
2909 if (error)
2910 return error;
2912 if (gw_trans->commit == NULL) {
2913 struct got_reference *head_ref;
2914 error = got_ref_open(&head_ref, header->repo,
2915 gw_trans->headref, 0);
2916 if (error)
2917 return error;
2919 error = got_ref_resolve(&header->id, header->repo, head_ref);
2920 got_ref_close(head_ref);
2921 if (error)
2922 return error;
2924 error = got_object_open_as_commit(&header->commit,
2925 header->repo, header->id);
2926 } else {
2927 struct got_reference *ref;
2928 error = got_ref_open(&ref, header->repo, gw_trans->commit, 0);
2929 if (error == NULL) {
2930 int obj_type;
2931 error = got_ref_resolve(&header->id, header->repo, ref);
2932 got_ref_close(ref);
2933 if (error)
2934 return error;
2935 error = got_object_get_type(&obj_type, header->repo,
2936 header->id);
2937 if (error)
2938 return error;
2939 if (obj_type == GOT_OBJ_TYPE_TAG) {
2940 struct got_tag_object *tag;
2941 error = got_object_open_as_tag(&tag,
2942 header->repo, header->id);
2943 if (error)
2944 return error;
2945 if (got_object_tag_get_object_type(tag) !=
2946 GOT_OBJ_TYPE_COMMIT) {
2947 got_object_tag_close(tag);
2948 error = got_error(GOT_ERR_OBJ_TYPE);
2949 return error;
2951 free(header->id);
2952 header->id = got_object_id_dup(
2953 got_object_tag_get_object_id(tag));
2954 if (header->id == NULL)
2955 error = got_error_from_errno(
2956 "got_object_id_dup");
2957 got_object_tag_close(tag);
2958 if (error)
2959 return error;
2960 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2961 error = got_error(GOT_ERR_OBJ_TYPE);
2962 return error;
2964 error = got_object_open_as_commit(&header->commit,
2965 header->repo, header->id);
2966 if (error)
2967 return error;
2969 if (header->commit == NULL) {
2970 error = got_repo_match_object_id_prefix(&header->id,
2971 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2972 header->repo);
2973 if (error)
2974 return error;
2976 error = got_repo_match_object_id_prefix(&header->id,
2977 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2978 header->repo);
2981 error = got_repo_map_path(&in_repo_path, header->repo,
2982 gw_trans->repo_path, 1);
2983 if (error)
2984 return error;
2986 if (in_repo_path) {
2987 header->path = strdup(in_repo_path);
2989 free(in_repo_path);
2991 error = got_ref_list(&header->refs, header->repo, NULL,
2992 got_ref_cmp_by_name, NULL);
2993 if (error)
2994 return error;
2996 error = gw_get_commits(gw_trans, header, limit);
2997 return error;
3000 struct blame_line {
3001 int annotated;
3002 char *id_str;
3003 char *committer;
3004 char datebuf[11]; /* YYYY-MM-DD + NUL */
3007 struct gw_blame_cb_args {
3008 struct blame_line *lines;
3009 int nlines;
3010 int nlines_prec;
3011 int lineno_cur;
3012 off_t *line_offsets;
3013 FILE *f;
3014 struct got_repository *repo;
3015 struct gw_trans *gw_trans;
3018 static const struct got_error *
3019 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3021 const struct got_error *err = NULL;
3022 struct gw_blame_cb_args *a = arg;
3023 struct blame_line *bline;
3024 char *line = NULL;
3025 size_t linesize = 0;
3026 struct got_commit_object *commit = NULL;
3027 off_t offset;
3028 struct tm tm;
3029 time_t committer_time;
3030 enum kcgi_err kerr = KCGI_OK;
3032 if (nlines != a->nlines ||
3033 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3034 return got_error(GOT_ERR_RANGE);
3036 if (lineno == -1)
3037 return NULL; /* no change in this commit */
3039 /* Annotate this line. */
3040 bline = &a->lines[lineno - 1];
3041 if (bline->annotated)
3042 return NULL;
3043 err = got_object_id_str(&bline->id_str, id);
3044 if (err)
3045 return err;
3047 err = got_object_open_as_commit(&commit, a->repo, id);
3048 if (err)
3049 goto done;
3051 bline->committer = strdup(got_object_commit_get_committer(commit));
3052 if (bline->committer == NULL) {
3053 err = got_error_from_errno("strdup");
3054 goto done;
3057 committer_time = got_object_commit_get_committer_time(commit);
3058 if (localtime_r(&committer_time, &tm) == NULL)
3059 return got_error_from_errno("localtime_r");
3060 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3061 &tm) >= sizeof(bline->datebuf)) {
3062 err = got_error(GOT_ERR_NO_SPACE);
3063 goto done;
3065 bline->annotated = 1;
3067 /* Print lines annotated so far. */
3068 bline = &a->lines[a->lineno_cur - 1];
3069 if (!bline->annotated)
3070 goto done;
3072 offset = a->line_offsets[a->lineno_cur - 1];
3073 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3074 err = got_error_from_errno("fseeko");
3075 goto done;
3078 while (bline->annotated) {
3079 char *smallerthan, *at, *nl, *committer;
3080 char *lineno = NULL, *href_diff = NULL, *href_link = NULL;
3081 size_t len;
3083 if (getline(&line, &linesize, a->f) == -1) {
3084 if (ferror(a->f))
3085 err = got_error_from_errno("getline");
3086 break;
3089 committer = bline->committer;
3090 smallerthan = strchr(committer, '<');
3091 if (smallerthan && smallerthan[1] != '\0')
3092 committer = smallerthan + 1;
3093 at = strchr(committer, '@');
3094 if (at)
3095 *at = '\0';
3096 len = strlen(committer);
3097 if (len >= 9)
3098 committer[8] = '\0';
3100 nl = strchr(line, '\n');
3101 if (nl)
3102 *nl = '\0';
3104 if (a->gw_trans->repo_folder == NULL)
3105 a->gw_trans->repo_folder = strdup("");
3106 if (a->gw_trans->repo_folder == NULL)
3107 goto err;
3109 /* blame line */
3110 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3111 "blame_wrapper", KATTR__MAX);
3112 if (kerr != KCGI_OK)
3113 goto err;
3114 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3115 "blame_number", KATTR__MAX);
3116 if (kerr != KCGI_OK)
3117 goto err;
3118 if (asprintf(&lineno, "%.*d", a->nlines_prec,
3119 a->lineno_cur) == -1)
3120 goto err;
3121 kerr = khtml_puts(a->gw_trans->gw_html_req, lineno);
3122 if (kerr != KCGI_OK)
3123 goto err;
3124 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3125 if (kerr != KCGI_OK)
3126 goto err;
3128 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3129 "blame_hash", KATTR__MAX);
3130 if (kerr != KCGI_OK)
3131 goto err;
3132 if (asprintf(&href_diff,
3133 "?path=%s&action=diff&commit=%s&file=%s&folder=%s",
3134 a->gw_trans->repo_name, bline->id_str,
3135 a->gw_trans->repo_file, a->gw_trans->repo_folder) == -1) {
3136 err = got_error_from_errno("asprintf");
3137 goto err;
3139 if (asprintf(&href_link, "%.8s", bline->id_str) == -1) {
3140 err = got_error_from_errno("asprintf");
3141 goto err;
3143 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3144 KATTR_HREF, href_diff, KATTR__MAX);
3145 if (kerr != KCGI_OK)
3146 goto done;
3147 kerr = khtml_puts(a->gw_trans->gw_html_req, href_link);
3148 if (kerr != KCGI_OK)
3149 goto err;
3150 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3151 if (kerr != KCGI_OK)
3152 goto err;
3154 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3155 "blame_date", KATTR__MAX);
3156 if (kerr != KCGI_OK)
3157 goto err;
3158 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3159 if (kerr != KCGI_OK)
3160 goto err;
3161 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3162 if (kerr != KCGI_OK)
3163 goto err;
3165 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3166 "blame_author", KATTR__MAX);
3167 if (kerr != KCGI_OK)
3168 goto err;
3169 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
3170 if (kerr != KCGI_OK)
3171 goto err;
3172 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3173 if (kerr != KCGI_OK)
3174 goto err;
3176 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3177 "blame_code", KATTR__MAX);
3178 if (kerr != KCGI_OK)
3179 goto err;
3180 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
3181 if (kerr != KCGI_OK)
3182 goto err;
3183 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3184 if (kerr != KCGI_OK)
3185 goto err;
3187 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3188 if (kerr != KCGI_OK)
3189 goto err;
3191 a->lineno_cur++;
3192 bline = &a->lines[a->lineno_cur - 1];
3193 err:
3194 free(lineno);
3195 free(href_diff);
3196 free(href_link);
3198 done:
3199 if (commit)
3200 got_object_commit_close(commit);
3201 free(line);
3202 if (err == NULL && kerr != KCGI_OK)
3203 err = gw_kcgi_error(kerr);
3204 return err;
3207 static const struct got_error *
3208 gw_output_file_blame(struct gw_trans *gw_trans)
3210 const struct got_error *error = NULL;
3211 struct got_repository *repo = NULL;
3212 struct got_object_id *obj_id = NULL;
3213 struct got_object_id *commit_id = NULL;
3214 struct got_blob_object *blob = NULL;
3215 char *path = NULL, *in_repo_path = NULL;
3216 struct gw_blame_cb_args bca;
3217 int i, obj_type;
3218 size_t filesize;
3220 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3221 if (error)
3222 return error;
3224 if (asprintf(&path, "%s%s%s",
3225 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3226 gw_trans->repo_folder ? "/" : "",
3227 gw_trans->repo_file) == -1) {
3228 error = got_error_from_errno("asprintf");
3229 goto done;
3232 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3233 if (error)
3234 goto done;
3236 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3237 GOT_OBJ_TYPE_COMMIT, 1, repo);
3238 if (error)
3239 goto done;
3241 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3242 if (error)
3243 goto done;
3245 if (obj_id == NULL) {
3246 error = got_error(GOT_ERR_NO_OBJ);
3247 goto done;
3250 error = got_object_get_type(&obj_type, repo, obj_id);
3251 if (error)
3252 goto done;
3254 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3255 error = got_error(GOT_ERR_OBJ_TYPE);
3256 goto done;
3259 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3260 if (error)
3261 goto done;
3263 bca.f = got_opentemp();
3264 if (bca.f == NULL) {
3265 error = got_error_from_errno("got_opentemp");
3266 goto done;
3268 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3269 &bca.line_offsets, bca.f, blob);
3270 if (error || bca.nlines == 0)
3271 goto done;
3273 /* Don't include \n at EOF in the blame line count. */
3274 if (bca.line_offsets[bca.nlines - 1] == filesize)
3275 bca.nlines--;
3277 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3278 if (bca.lines == NULL) {
3279 error = got_error_from_errno("calloc");
3280 goto done;
3282 bca.lineno_cur = 1;
3283 bca.nlines_prec = 0;
3284 i = bca.nlines;
3285 while (i > 0) {
3286 i /= 10;
3287 bca.nlines_prec++;
3289 bca.repo = repo;
3290 bca.gw_trans = gw_trans;
3292 error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
3293 NULL, NULL);
3294 if (error)
3295 goto done;
3296 done:
3297 free(bca.line_offsets);
3298 free(in_repo_path);
3299 free(commit_id);
3300 free(obj_id);
3301 free(path);
3303 for (i = 0; i < bca.nlines; i++) {
3304 struct blame_line *bline = &bca.lines[i];
3305 free(bline->id_str);
3306 free(bline->committer);
3308 free(bca.lines);
3309 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3310 error = got_error_from_errno("fclose");
3311 if (blob)
3312 got_object_blob_close(blob);
3313 if (repo)
3314 got_repo_close(repo);
3315 return error;
3318 static const struct got_error *
3319 gw_output_blob_buf(struct gw_trans *gw_trans)
3321 const struct got_error *error = NULL;
3322 struct got_repository *repo = NULL;
3323 struct got_object_id *obj_id = NULL;
3324 struct got_object_id *commit_id = NULL;
3325 struct got_blob_object *blob = NULL;
3326 char *path = NULL, *in_repo_path = NULL;
3327 int obj_type, set_mime = 0;
3328 size_t len, hdrlen;
3329 const uint8_t *buf;
3330 enum kcgi_err kerr = KCGI_OK;
3332 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3333 if (error)
3334 return error;
3336 if (asprintf(&path, "%s%s%s",
3337 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3338 gw_trans->repo_folder ? "/" : "",
3339 gw_trans->repo_file) == -1) {
3340 error = got_error_from_errno("asprintf");
3341 goto done;
3344 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3345 if (error)
3346 goto done;
3348 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3349 GOT_OBJ_TYPE_COMMIT, 1, repo);
3350 if (error)
3351 goto done;
3353 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3354 if (error)
3355 goto done;
3357 if (obj_id == NULL) {
3358 error = got_error(GOT_ERR_NO_OBJ);
3359 goto done;
3362 error = got_object_get_type(&obj_type, repo, obj_id);
3363 if (error)
3364 goto done;
3366 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3367 error = got_error(GOT_ERR_OBJ_TYPE);
3368 goto done;
3371 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3372 if (error)
3373 goto done;
3375 hdrlen = got_object_blob_get_hdrlen(blob);
3376 do {
3377 error = got_object_blob_read_block(&len, blob);
3378 if (error)
3379 goto done;
3380 buf = got_object_blob_get_read_buf(blob);
3383 * Skip blob object header first time around,
3384 * which also contains a zero byte.
3386 buf += hdrlen;
3387 if (set_mime == 0) {
3388 if (isbinary(buf, len - hdrlen))
3389 gw_trans->mime = KMIME_APP_OCTET_STREAM;
3390 else
3391 gw_trans->mime = KMIME_TEXT_PLAIN;
3392 set_mime = 1;
3393 error = gw_display_index(gw_trans);
3394 if (error)
3395 goto done;
3397 khttp_write(gw_trans->gw_req, buf, len - hdrlen);
3398 hdrlen = 0;
3399 } while (len != 0);
3400 done:
3401 free(in_repo_path);
3402 free(commit_id);
3403 free(obj_id);
3404 free(path);
3405 if (blob)
3406 got_object_blob_close(blob);
3407 if (repo)
3408 got_repo_close(repo);
3409 if (error == NULL && kerr != KCGI_OK)
3410 error = gw_kcgi_error(kerr);
3411 return error;
3414 static const struct got_error *
3415 gw_output_repo_tree(struct gw_trans *gw_trans)
3417 const struct got_error *error = NULL;
3418 struct got_repository *repo = NULL;
3419 struct got_object_id *tree_id = NULL, *commit_id = NULL;
3420 struct got_tree_object *tree = NULL;
3421 char *path = NULL, *in_repo_path = NULL;
3422 char *id_str = NULL;
3423 char *build_folder = NULL;
3424 char *href_blob = NULL, *href_blame = NULL;
3425 const char *class = NULL;
3426 int nentries, i, class_flip = 0;
3427 enum kcgi_err kerr = KCGI_OK;
3429 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3430 if (error)
3431 goto done;
3433 if (gw_trans->repo_folder != NULL)
3434 path = strdup(gw_trans->repo_folder);
3435 else {
3436 error = got_repo_map_path(&in_repo_path, repo,
3437 gw_trans->repo_path, 1);
3438 if (error)
3439 goto done;
3440 free(path);
3441 path = in_repo_path;
3444 if (gw_trans->commit == NULL) {
3445 struct got_reference *head_ref;
3446 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
3447 if (error)
3448 goto done;
3449 error = got_ref_resolve(&commit_id, repo, head_ref);
3450 if (error)
3451 goto done;
3452 got_ref_close(head_ref);
3454 } else {
3455 error = got_repo_match_object_id(&commit_id, NULL,
3456 gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
3457 if (error)
3458 goto done;
3461 error = got_object_id_str(&gw_trans->commit, commit_id);
3462 if (error)
3463 goto done;
3465 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
3466 if (error)
3467 goto done;
3469 error = got_object_open_as_tree(&tree, repo, tree_id);
3470 if (error)
3471 goto done;
3473 nentries = got_object_tree_get_nentries(tree);
3474 for (i = 0; i < nentries; i++) {
3475 struct got_tree_entry *te;
3476 const char *modestr = "";
3477 mode_t mode;
3479 te = got_object_tree_get_entry(tree, i);
3481 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
3482 if (error)
3483 goto done;
3485 mode = got_tree_entry_get_mode(te);
3486 if (got_object_tree_entry_is_submodule(te))
3487 modestr = "$";
3488 else if (S_ISLNK(mode))
3489 modestr = "@";
3490 else if (S_ISDIR(mode))
3491 modestr = "/";
3492 else if (mode & S_IXUSR)
3493 modestr = "*";
3495 if (class_flip == 0) {
3496 class = "back_lightgray";
3497 class_flip = 1;
3498 } else {
3499 class = "back_white";
3500 class_flip = 0;
3503 if (S_ISDIR(mode)) {
3504 if (asprintf(&build_folder, "%s/%s",
3505 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3506 got_tree_entry_get_name(te)) == -1) {
3507 error = got_error_from_errno(
3508 "asprintf");
3509 goto done;
3511 if (asprintf(&href_blob,
3512 "?path=%s&action=%s&commit=%s&folder=%s",
3513 gw_trans->repo_name, gw_trans->action_name,
3514 gw_trans->commit, build_folder) == -1) {
3515 error = got_error_from_errno("asprintf");
3516 goto done;
3519 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3520 KATTR_ID, "tree_wrapper", KATTR__MAX);
3521 if (kerr != KCGI_OK)
3522 goto done;
3523 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3524 KATTR_ID, "tree_line", KATTR_CLASS, class,
3525 KATTR__MAX);
3526 if (kerr != KCGI_OK)
3527 goto done;
3528 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3529 KATTR_HREF, href_blob, KATTR_CLASS,
3530 "diff_directory", KATTR__MAX);
3531 if (kerr != KCGI_OK)
3532 goto done;
3533 kerr = khtml_puts(gw_trans->gw_html_req,
3534 got_tree_entry_get_name(te));
3535 if (kerr != KCGI_OK)
3536 goto done;
3537 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3538 if (kerr != KCGI_OK)
3539 goto done;
3540 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3541 if (kerr != KCGI_OK)
3542 goto done;
3543 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3544 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
3545 KATTR__MAX);
3546 if (kerr != KCGI_OK)
3547 goto done;
3548 kerr = khtml_entity(gw_trans->gw_html_req,
3549 KENTITY_nbsp);
3550 if (kerr != KCGI_OK)
3551 goto done;
3552 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3553 if (kerr != KCGI_OK)
3554 goto done;
3555 } else {
3556 if (asprintf(&href_blob,
3557 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3558 gw_trans->repo_name, "blob", gw_trans->commit,
3559 got_tree_entry_get_name(te),
3560 gw_trans->repo_folder ?
3561 gw_trans->repo_folder : "") == -1) {
3562 error = got_error_from_errno("asprintf");
3563 goto done;
3565 if (asprintf(&href_blame,
3566 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3567 gw_trans->repo_name, "blame", gw_trans->commit,
3568 got_tree_entry_get_name(te),
3569 gw_trans->repo_folder ?
3570 gw_trans->repo_folder : "") == -1) {
3571 error = got_error_from_errno("asprintf");
3572 goto done;
3575 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3576 KATTR_ID, "tree_wrapper", KATTR__MAX);
3577 if (kerr != KCGI_OK)
3578 goto done;
3579 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3580 KATTR_ID, "tree_line", KATTR_CLASS, class,
3581 KATTR__MAX);
3582 if (kerr != KCGI_OK)
3583 goto done;
3584 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3585 KATTR_HREF, href_blob, KATTR__MAX);
3586 if (kerr != KCGI_OK)
3587 goto done;
3588 kerr = khtml_puts(gw_trans->gw_html_req,
3589 got_tree_entry_get_name(te));
3590 if (kerr != KCGI_OK)
3591 goto done;
3592 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3593 if (kerr != KCGI_OK)
3594 goto done;
3595 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3596 if (kerr != KCGI_OK)
3597 goto done;
3598 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3599 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
3600 KATTR__MAX);
3601 if (kerr != KCGI_OK)
3602 goto done;
3604 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3605 KATTR_HREF, href_blob, KATTR__MAX);
3606 if (kerr != KCGI_OK)
3607 goto done;
3608 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
3609 if (kerr != KCGI_OK)
3610 goto done;
3611 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3612 if (kerr != KCGI_OK)
3613 goto done;
3615 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3616 if (kerr != KCGI_OK)
3617 goto done;
3619 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3620 KATTR_HREF, href_blame, KATTR__MAX);
3621 if (kerr != KCGI_OK)
3622 goto done;
3623 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
3624 if (kerr != KCGI_OK)
3625 goto done;
3627 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3628 if (kerr != KCGI_OK)
3629 goto done;
3631 free(id_str);
3632 id_str = NULL;
3633 free(href_blob);
3634 href_blob = NULL;
3635 free(build_folder);
3636 build_folder = NULL;
3638 done:
3639 if (tree)
3640 got_object_tree_close(tree);
3641 if (repo)
3642 got_repo_close(repo);
3643 free(id_str);
3644 free(href_blob);
3645 free(href_blame);
3646 free(in_repo_path);
3647 free(tree_id);
3648 free(build_folder);
3649 if (error == NULL && kerr != KCGI_OK)
3650 error = gw_kcgi_error(kerr);
3651 return error;
3654 static const struct got_error *
3655 gw_get_repo_heads(char **head_html, struct gw_trans *gw_trans)
3657 const struct got_error *error = NULL;
3658 struct got_repository *repo = NULL;
3659 struct got_reflist_head refs;
3660 struct got_reflist_entry *re;
3661 char *head_row = NULL, *head_navs_disp = NULL, *age = NULL;
3662 struct buf *diffbuf = NULL;
3663 size_t newsize;
3665 *head_html = NULL;
3667 SIMPLEQ_INIT(&refs);
3669 error = buf_alloc(&diffbuf, 0);
3670 if (error)
3671 return NULL;
3673 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3674 if (error)
3675 goto done;
3677 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
3678 NULL);
3679 if (error)
3680 goto done;
3682 SIMPLEQ_FOREACH(re, &refs, entry) {
3683 char *refname;
3685 refname = strdup(got_ref_get_name(re->ref));
3686 if (refname == NULL) {
3687 error = got_error_from_errno("got_ref_to_str");
3688 goto done;
3691 if (strncmp(refname, "refs/heads/", 11) != 0) {
3692 free(refname);
3693 continue;
3696 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
3697 refname, TM_DIFF);
3698 if (error)
3699 goto done;
3701 if (asprintf(&head_navs_disp, heads_navs, gw_trans->repo_name,
3702 refname, gw_trans->repo_name, refname,
3703 gw_trans->repo_name, refname, gw_trans->repo_name,
3704 refname) == -1) {
3705 error = got_error_from_errno("asprintf");
3706 goto done;
3709 if (strncmp(refname, "refs/heads/", 11) == 0)
3710 refname += 11;
3712 if (asprintf(&head_row, heads_row, age, refname,
3713 head_navs_disp) == -1) {
3714 error = got_error_from_errno("asprintf");
3715 goto done;
3718 error = buf_puts(&newsize, diffbuf, head_row);
3720 free(head_navs_disp);
3721 free(head_row);
3724 if (buf_len(diffbuf) > 0) {
3725 error = buf_putc(diffbuf, '\0');
3726 *head_html = strdup(buf_get(diffbuf));
3727 if (*head_html == NULL)
3728 error = got_error_from_errno("strdup");
3730 done:
3731 buf_free(diffbuf);
3732 got_ref_list_free(&refs);
3733 if (repo)
3734 got_repo_close(repo);
3735 return error;
3738 static char *
3739 gw_get_site_link(struct gw_trans *gw_trans)
3741 char *link = NULL, *repo = NULL, *action = NULL;
3743 if (gw_trans->repo_name != NULL &&
3744 asprintf(&repo, " / <a href='?path=%s&action=summary'>%s</a>",
3745 gw_trans->repo_name, gw_trans->repo_name) == -1)
3746 return NULL;
3748 if (gw_trans->action_name != NULL &&
3749 asprintf(&action, " / %s", gw_trans->action_name) == -1) {
3750 free(repo);
3751 return NULL;
3754 if (asprintf(&link, site_link, GOTWEB,
3755 gw_trans->gw_conf->got_site_link,
3756 repo ? repo : "", action ? action : "") == -1) {
3757 free(repo);
3758 free(action);
3759 return NULL;
3762 free(repo);
3763 free(action);
3764 return link;
3767 static const struct got_error *
3768 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
3770 const struct got_error *error = NULL;
3771 char *color = NULL;
3772 enum kcgi_err kerr = KCGI_OK;
3774 if (strncmp(buf, "-", 1) == 0)
3775 color = "diff_minus";
3776 else if (strncmp(buf, "+", 1) == 0)
3777 color = "diff_plus";
3778 else if (strncmp(buf, "@@", 2) == 0)
3779 color = "diff_chunk_header";
3780 else if (strncmp(buf, "@@", 2) == 0)
3781 color = "diff_chunk_header";
3782 else if (strncmp(buf, "commit +", 8) == 0)
3783 color = "diff_meta";
3784 else if (strncmp(buf, "commit -", 8) == 0)
3785 color = "diff_meta";
3786 else if (strncmp(buf, "blob +", 6) == 0)
3787 color = "diff_meta";
3788 else if (strncmp(buf, "blob -", 6) == 0)
3789 color = "diff_meta";
3790 else if (strncmp(buf, "file +", 6) == 0)
3791 color = "diff_meta";
3792 else if (strncmp(buf, "file -", 6) == 0)
3793 color = "diff_meta";
3794 else if (strncmp(buf, "from:", 5) == 0)
3795 color = "diff_author";
3796 else if (strncmp(buf, "via:", 4) == 0)
3797 color = "diff_author";
3798 else if (strncmp(buf, "date:", 5) == 0)
3799 color = "diff_date";
3800 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3801 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
3802 if (error == NULL && kerr != KCGI_OK)
3803 error = gw_kcgi_error(kerr);
3804 return error;
3808 * XXX This function should not exist.
3809 * We should let khtml_puts(3) handle HTML escaping.
3811 static const struct got_error *
3812 gw_html_escape(char **escaped_html, const char *orig_html)
3814 const struct got_error *error = NULL;
3815 struct escape_pair {
3816 char c;
3817 const char *s;
3818 } esc[] = {
3819 { '>', "&gt;" },
3820 { '<', "&lt;" },
3821 { '&', "&amp;" },
3822 { '"', "&quot;" },
3823 { '\'', "&apos;" },
3824 { '\n', "<br />" },
3826 size_t orig_len, len;
3827 int i, j, x;
3829 orig_len = strlen(orig_html);
3830 len = orig_len;
3831 for (i = 0; i < orig_len; i++) {
3832 for (j = 0; j < nitems(esc); j++) {
3833 if (orig_html[i] != esc[j].c)
3834 continue;
3835 len += strlen(esc[j].s) - 1 /* escaped char */;
3839 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
3840 if (*escaped_html == NULL)
3841 return got_error_from_errno("calloc");
3843 x = 0;
3844 for (i = 0; i < orig_len; i++) {
3845 int escaped = 0;
3846 for (j = 0; j < nitems(esc); j++) {
3847 if (orig_html[i] != esc[j].c)
3848 continue;
3850 if (strlcat(*escaped_html, esc[j].s, len + 1)
3851 >= len + 1) {
3852 error = got_error(GOT_ERR_NO_SPACE);
3853 goto done;
3855 x += strlen(esc[j].s);
3856 escaped = 1;
3857 break;
3859 if (!escaped) {
3860 (*escaped_html)[x] = orig_html[i];
3861 x++;
3864 done:
3865 if (error) {
3866 free(*escaped_html);
3867 *escaped_html = NULL;
3868 } else {
3869 (*escaped_html)[x] = '\0';
3871 return error;
3874 int
3875 main(int argc, char *argv[])
3877 const struct got_error *error = NULL;
3878 struct gw_trans *gw_trans;
3879 struct gw_dir *dir = NULL, *tdir;
3880 const char *page = "index";
3881 int gw_malloc = 1;
3882 enum kcgi_err kerr;
3884 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
3885 errx(1, "malloc");
3887 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
3888 errx(1, "malloc");
3890 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
3891 errx(1, "malloc");
3893 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
3894 errx(1, "malloc");
3896 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
3897 if (kerr != KCGI_OK) {
3898 error = gw_kcgi_error(kerr);
3899 goto done;
3902 if ((gw_trans->gw_conf =
3903 malloc(sizeof(struct gotweb_conf))) == NULL) {
3904 gw_malloc = 0;
3905 error = got_error_from_errno("malloc");
3906 goto done;
3909 TAILQ_INIT(&gw_trans->gw_dirs);
3910 TAILQ_INIT(&gw_trans->gw_headers);
3912 gw_trans->page = 0;
3913 gw_trans->repos_total = 0;
3914 gw_trans->repo_path = NULL;
3915 gw_trans->commit = NULL;
3916 gw_trans->headref = strdup(GOT_REF_HEAD);
3917 gw_trans->mime = KMIME_TEXT_HTML;
3918 gw_trans->gw_tmpl->key = gw_templs;
3919 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
3920 gw_trans->gw_tmpl->arg = gw_trans;
3921 gw_trans->gw_tmpl->cb = gw_template;
3922 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
3923 if (error)
3924 goto done;
3926 error = gw_parse_querystring(gw_trans);
3927 if (error)
3928 goto done;
3930 if (gw_trans->action == GW_BLOB)
3931 error = gw_blob(gw_trans);
3932 else
3933 error = gw_display_index(gw_trans);
3934 done:
3935 if (error) {
3936 gw_trans->mime = KMIME_TEXT_PLAIN;
3937 gw_trans->action = GW_ERR;
3938 gw_display_error(gw_trans, error);
3940 if (gw_malloc) {
3941 free(gw_trans->gw_conf->got_repos_path);
3942 free(gw_trans->gw_conf->got_site_name);
3943 free(gw_trans->gw_conf->got_site_owner);
3944 free(gw_trans->gw_conf->got_site_link);
3945 free(gw_trans->gw_conf->got_logo);
3946 free(gw_trans->gw_conf->got_logo_url);
3947 free(gw_trans->gw_conf);
3948 free(gw_trans->commit);
3949 free(gw_trans->repo_path);
3950 free(gw_trans->repo_name);
3951 free(gw_trans->repo_file);
3952 free(gw_trans->action_name);
3953 free(gw_trans->headref);
3955 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
3956 free(dir->name);
3957 free(dir->description);
3958 free(dir->age);
3959 free(dir->url);
3960 free(dir->path);
3961 free(dir);
3966 khttp_free(gw_trans->gw_req);
3967 return 0;