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_colordiff_line(struct gw_trans *, char *);
181 static const struct got_error *gw_gen_commit_header(struct gw_trans *, char *,
182 char*);
183 static const struct got_error *gw_gen_diff_header(struct gw_trans *, char *,
184 char*);
185 static const struct got_error *gw_gen_author_header(struct gw_trans *,
186 const char *);
187 static const struct got_error *gw_gen_age_header(struct gw_trans *,
188 const char *);
189 static const struct got_error *gw_gen_committer_header(struct gw_trans *,
190 const char *);
191 static const struct got_error *gw_gen_commit_msg_header(struct gw_trans*,
192 char *);
193 static const struct got_error *gw_gen_tree_header(struct gw_trans *, char *);
195 static void gw_free_headers(struct gw_header *);
196 static const struct got_error* gw_display_open(struct gw_trans *, enum khttp,
197 enum kmime);
198 static const struct got_error* gw_display_index(struct gw_trans *);
199 static void gw_display_error(struct gw_trans *,
200 const struct got_error *);
202 static int gw_template(size_t, void *);
204 static const struct got_error* gw_get_header(struct gw_trans *,
205 struct gw_header *, int);
206 static const struct got_error* gw_get_commits(struct gw_trans *,
207 struct gw_header *, int);
208 static const struct got_error* gw_get_commit(struct gw_trans *,
209 struct gw_header *);
210 static const struct got_error* gw_apply_unveil(const char *, const char *);
211 static const struct got_error* gw_blame_cb(void *, int, int,
212 struct got_object_id *);
213 static const struct got_error* gw_load_got_paths(struct gw_trans *);
214 static const struct got_error* gw_load_got_path(struct gw_trans *,
215 struct gw_dir *);
216 static const struct got_error* gw_parse_querystring(struct gw_trans *);
218 static const struct got_error* gw_blame(struct gw_trans *);
219 static const struct got_error* gw_blob(struct gw_trans *);
220 static const struct got_error* gw_diff(struct gw_trans *);
221 static const struct got_error* gw_index(struct gw_trans *);
222 static const struct got_error* gw_commits(struct gw_trans *);
223 static const struct got_error* gw_briefs(struct gw_trans *);
224 static const struct got_error* gw_summary(struct gw_trans *);
225 static const struct got_error* gw_tree(struct gw_trans *);
226 static const struct got_error* gw_tag(struct gw_trans *);
228 struct gw_query_action {
229 unsigned int func_id;
230 const char *func_name;
231 const struct got_error *(*func_main)(struct gw_trans *);
232 char *template;
233 };
235 enum gw_query_actions {
236 GW_BLAME,
237 GW_BLOB,
238 GW_BRIEFS,
239 GW_COMMITS,
240 GW_DIFF,
241 GW_ERR,
242 GW_INDEX,
243 GW_SUMMARY,
244 GW_TAG,
245 GW_TREE,
246 };
248 static struct gw_query_action gw_query_funcs[] = {
249 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
250 { GW_BLOB, "blob", NULL, NULL },
251 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
252 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
253 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
254 { GW_ERR, NULL, NULL, "gw_tmpl/err.tmpl" },
255 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
256 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
257 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
258 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
259 };
261 static const struct got_error *
262 gw_kcgi_error(enum kcgi_err kerr)
264 if (kerr == KCGI_OK)
265 return NULL;
267 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
268 return got_error(GOT_ERR_CANCELLED);
270 if (kerr == KCGI_ENOMEM)
271 return got_error_set_errno(ENOMEM,
272 kcgi_strerror(kerr));
274 if (kerr == KCGI_ENFILE)
275 return got_error_set_errno(ENFILE,
276 kcgi_strerror(kerr));
278 if (kerr == KCGI_EAGAIN)
279 return got_error_set_errno(EAGAIN,
280 kcgi_strerror(kerr));
282 if (kerr == KCGI_FORM)
283 return got_error_msg(GOT_ERR_IO,
284 kcgi_strerror(kerr));
286 return got_error_from_errno(kcgi_strerror(kerr));
289 static const struct got_error *
290 gw_apply_unveil(const char *repo_path, const char *repo_file)
292 const struct got_error *err;
294 if (repo_path && repo_file) {
295 char *full_path;
296 if (asprintf(&full_path, "%s/%s", repo_path, repo_file) == -1)
297 return got_error_from_errno("asprintf unveil");
298 if (unveil(full_path, "r") != 0)
299 return got_error_from_errno2("unveil", full_path);
302 if (repo_path && unveil(repo_path, "r") != 0)
303 return got_error_from_errno2("unveil", repo_path);
305 if (unveil("/tmp", "rwc") != 0)
306 return got_error_from_errno2("unveil", "/tmp");
308 err = got_privsep_unveil_exec_helpers();
309 if (err != NULL)
310 return err;
312 if (unveil(NULL, NULL) != 0)
313 return got_error_from_errno("unveil");
315 return NULL;
318 static const struct got_error *
319 gw_empty_string(char **s)
321 *s = strdup("");
322 if (*s == NULL)
323 return got_error_from_errno("strdup");
324 return NULL;
327 static int
328 isbinary(const uint8_t *buf, size_t n)
330 size_t i;
332 for (i = 0; i < n; i++)
333 if (buf[i] == 0)
334 return 1;
335 return 0;
338 static const struct got_error *
339 gw_blame(struct gw_trans *gw_trans)
341 const struct got_error *error = NULL;
342 struct gw_header *header = NULL;
343 char *age = NULL;
344 enum kcgi_err kerr;
346 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
347 NULL) == -1)
348 return got_error_from_errno("pledge");
350 if ((header = gw_init_header()) == NULL)
351 return got_error_from_errno("malloc");
353 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
354 if (error)
355 goto done;
357 error = gw_get_header(gw_trans, header, 1);
358 if (error)
359 goto done;
361 /* blame header */
362 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
363 "blame_header_wrapper", KATTR__MAX);
364 if (kerr != KCGI_OK)
365 goto done;
366 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
367 "blame_header", KATTR__MAX);
368 if (kerr != KCGI_OK)
369 goto done;
370 error = gw_get_time_str(&age, header->committer_time,
371 TM_LONG);
372 if (error)
373 goto done;
374 error = gw_gen_age_header(gw_trans, age ?age : "");
375 if (error)
376 goto done;
377 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
378 if (error)
379 goto done;
380 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
381 if (kerr != KCGI_OK)
382 goto done;
383 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
384 "dotted_line", KATTR__MAX);
385 if (kerr != KCGI_OK)
386 goto done;
387 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
388 if (kerr != KCGI_OK)
389 goto done;
391 /* blame */
392 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
393 "blame", KATTR__MAX);
394 if (kerr != KCGI_OK)
395 goto done;
396 error = gw_output_file_blame(gw_trans);
397 if (error)
398 goto done;
399 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
400 done:
401 got_ref_list_free(&header->refs);
402 gw_free_headers(header);
403 if (error == NULL && kerr != KCGI_OK)
404 error = gw_kcgi_error(kerr);
405 return error;
408 static const struct got_error *
409 gw_blob(struct gw_trans *gw_trans)
411 const struct got_error *error = NULL;
412 struct gw_header *header = NULL;
414 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
415 NULL) == -1)
416 return got_error_from_errno("pledge");
418 if ((header = gw_init_header()) == NULL)
419 return got_error_from_errno("malloc");
421 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
422 if (error)
423 goto done;
425 error = gw_get_header(gw_trans, header, 1);
426 if (error)
427 goto done;
429 error = gw_output_blob_buf(gw_trans);
430 done:
431 got_ref_list_free(&header->refs);
432 gw_free_headers(header);
433 return error;
436 static const struct got_error *
437 gw_diff(struct gw_trans *gw_trans)
439 const struct got_error *error = NULL;
440 struct gw_header *header = NULL;
441 char *age = NULL;
442 enum kcgi_err kerr = KCGI_OK;
444 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
445 NULL) == -1)
446 return got_error_from_errno("pledge");
448 if ((header = gw_init_header()) == NULL)
449 return got_error_from_errno("malloc");
451 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
452 if (error)
453 goto done;
455 error = gw_get_header(gw_trans, header, 1);
456 if (error)
457 goto done;
459 /* diff header */
460 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
461 "diff_header_wrapper", KATTR__MAX);
462 if (kerr != KCGI_OK)
463 goto done;
464 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
465 "diff_header", KATTR__MAX);
466 if (kerr != KCGI_OK)
467 goto done;
468 error = gw_gen_diff_header(gw_trans, header->parent_id,
469 header->commit_id);
470 if (error)
471 goto done;
472 error = gw_gen_commit_header(gw_trans, header->commit_id,
473 header->refs_str);
474 if (error)
475 goto done;
476 error = gw_gen_tree_header(gw_trans, header->tree_id);
477 if (error)
478 goto done;
479 error = gw_gen_author_header(gw_trans, header->author);
480 if (error)
481 goto done;
482 error = gw_gen_committer_header(gw_trans, header->author);
483 if (error)
484 goto done;
485 error = gw_get_time_str(&age, header->committer_time,
486 TM_LONG);
487 if (error)
488 goto done;
489 error = gw_gen_age_header(gw_trans, age ?age : "");
490 if (error)
491 goto done;
492 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
493 if (error)
494 goto done;
495 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
496 if (kerr != KCGI_OK)
497 goto done;
498 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
499 "dotted_line", KATTR__MAX);
500 if (kerr != KCGI_OK)
501 goto done;
502 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
503 if (kerr != KCGI_OK)
504 goto done;
506 /* diff */
507 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
508 "diff", KATTR__MAX);
509 if (kerr != KCGI_OK)
510 goto done;
511 error = gw_output_diff(gw_trans, header);
512 if (error)
513 goto done;
515 /* diff content close */
516 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
517 if (kerr != KCGI_OK)
518 goto done;
519 done:
520 got_ref_list_free(&header->refs);
521 gw_free_headers(header);
522 free(age);
523 if (error == NULL && kerr != KCGI_OK)
524 error = gw_kcgi_error(kerr);
525 return error;
528 static const struct got_error *
529 gw_index(struct gw_trans *gw_trans)
531 const struct got_error *error = NULL;
532 struct gw_dir *gw_dir = NULL;
533 char *html, *navs, *next, *prev;
534 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
535 enum kcgi_err kerr;
537 if (pledge("stdio rpath proc exec sendfd unveil",
538 NULL) == -1) {
539 error = got_error_from_errno("pledge");
540 return error;
543 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
544 if (error)
545 return error;
547 error = gw_load_got_paths(gw_trans);
548 if (error)
549 return error;
551 kerr = khttp_puts(gw_trans->gw_req, index_projects_header);
552 if (kerr != KCGI_OK)
553 return gw_kcgi_error(kerr);
555 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
556 if (asprintf(&html, index_projects_empty,
557 gw_trans->gw_conf->got_repos_path) == -1)
558 return got_error_from_errno("asprintf");
559 kerr = khttp_puts(gw_trans->gw_req, html);
560 if (kerr != KCGI_OK)
561 error = gw_kcgi_error(kerr);
562 free(html);
563 return error;
566 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
567 dir_c++;
569 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
570 if (gw_trans->page > 0 && (gw_trans->page *
571 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
572 prev_disp++;
573 continue;
576 prev_disp++;
578 if (error)
579 return error;
580 if(asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
581 gw_dir->name, gw_dir->name) == -1)
582 return got_error_from_errno("asprintf");
584 if (asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
585 gw_dir->description, gw_dir->owner ? gw_dir->owner : "",
586 gw_dir->age,
587 navs) == -1)
588 return got_error_from_errno("asprintf");
590 kerr = khttp_puts(gw_trans->gw_req, html);
591 free(navs);
592 free(html);
593 if (kerr != KCGI_OK)
594 return gw_kcgi_error(kerr);
596 if (gw_trans->gw_conf->got_max_repos_display == 0)
597 continue;
599 if (next_disp == gw_trans->gw_conf->got_max_repos_display) {
600 kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
601 if (kerr != KCGI_OK)
602 return gw_kcgi_error(kerr);
603 } else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
604 (gw_trans->page > 0) &&
605 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
606 prev_disp == gw_trans->repos_total)) {
607 kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
608 if (kerr != KCGI_OK)
609 return gw_kcgi_error(kerr);
612 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
613 (gw_trans->page > 0) &&
614 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
615 prev_disp == gw_trans->repos_total)) {
616 if (asprintf(&prev, nav_prev, gw_trans->page - 1) == -1)
617 return got_error_from_errno("asprintf");
618 kerr = khttp_puts(gw_trans->gw_req, prev);
619 free(prev);
620 if (kerr != KCGI_OK)
621 return gw_kcgi_error(kerr);
624 kerr = khttp_puts(gw_trans->gw_req, div_end);
625 if (kerr != KCGI_OK)
626 return gw_kcgi_error(kerr);
628 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
629 next_disp == gw_trans->gw_conf->got_max_repos_display &&
630 dir_c != (gw_trans->page + 1) *
631 gw_trans->gw_conf->got_max_repos_display) {
632 if (asprintf(&next, nav_next, gw_trans->page + 1) == -1)
633 return got_error_from_errno("calloc");
634 kerr = khttp_puts(gw_trans->gw_req, next);
635 free(next);
636 if (kerr != KCGI_OK)
637 return gw_kcgi_error(kerr);
638 kerr = khttp_puts(gw_trans->gw_req, div_end);
639 if (kerr != KCGI_OK)
640 error = gw_kcgi_error(kerr);
641 next_disp = 0;
642 break;
645 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
646 (gw_trans->page > 0) &&
647 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
648 prev_disp == gw_trans->repos_total)) {
649 kerr = khttp_puts(gw_trans->gw_req, div_end);
650 if (kerr != KCGI_OK)
651 return gw_kcgi_error(kerr);
654 next_disp++;
656 return error;
659 static const struct got_error *
660 gw_commits(struct gw_trans *gw_trans)
662 const struct got_error *error = NULL;
663 struct gw_header *header = NULL, *n_header = NULL;
664 char *age = NULL;
665 char *href_diff = NULL, *href_blob = NULL;
666 enum kcgi_err kerr = KCGI_OK;
668 if ((header = gw_init_header()) == NULL)
669 return got_error_from_errno("malloc");
671 if (pledge("stdio rpath proc exec sendfd unveil",
672 NULL) == -1) {
673 error = got_error_from_errno("pledge");
674 goto done;
677 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
678 if (error)
679 goto done;
681 error = gw_get_header(gw_trans, header,
682 gw_trans->gw_conf->got_max_commits_display);
683 if (error)
684 goto done;
686 /* commit content */
687 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
688 /* commit line */
689 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
690 "commits_line_wrapper", KATTR__MAX);
691 if (kerr != KCGI_OK)
692 goto done;
693 error = gw_gen_commit_header(gw_trans, n_header->commit_id,
694 n_header->refs_str);
695 if (error)
696 goto done;
697 error = gw_gen_author_header(gw_trans, n_header->author);
698 if (error)
699 goto done;
700 error = gw_gen_committer_header(gw_trans, n_header->author);
701 if (error)
702 goto done;
703 error = gw_get_time_str(&age, n_header->committer_time,
704 TM_LONG);
705 if (error)
706 goto done;
707 error = gw_gen_age_header(gw_trans, age ?age : "");
708 if (error)
709 goto done;
710 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
711 if (kerr != KCGI_OK)
712 goto done;
714 /* dotted line */
715 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
716 "dotted_line", KATTR__MAX);
717 if (kerr != KCGI_OK)
718 goto done;
719 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
720 if (kerr != KCGI_OK)
721 goto done;
723 /* commit */
724 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
725 "commit", KATTR__MAX);
726 if (kerr != KCGI_OK)
727 goto done;
728 kerr = khttp_puts(gw_trans->gw_req, n_header->commit_msg);
729 if (kerr != KCGI_OK)
730 goto done;
731 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
732 if (kerr != KCGI_OK)
733 goto done;
735 /* navs */
737 /* XXX: create gen code for this */
738 /* build diff nav */
739 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
740 gw_trans->repo_name, n_header->commit_id) == -1) {
741 error = got_error_from_errno("asprintf");
742 goto done;
744 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
745 KATTR_ID, "navs_wrapper", KATTR__MAX);
746 if (kerr != KCGI_OK)
747 goto done;
748 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
749 KATTR_ID, "navs", KATTR__MAX);
750 if (kerr != KCGI_OK)
751 goto done;
752 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
753 KATTR_HREF, href_diff, KATTR__MAX);
754 if (kerr != KCGI_OK)
755 goto done;
756 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
757 if (kerr != KCGI_OK)
758 goto done;
759 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
760 if (kerr != KCGI_OK)
761 goto done;
763 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
764 if (kerr != KCGI_OK)
765 goto done;
767 /* XXX: create gen code for this */
768 /* build tree nav */
769 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
770 gw_trans->repo_name, n_header->commit_id) == -1) {
771 error = got_error_from_errno("asprintf");
772 goto done;
774 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
775 KATTR_HREF, href_blob, KATTR__MAX);
776 if (kerr != KCGI_OK)
777 goto done;
778 khtml_puts(gw_trans->gw_html_req, "tree");
779 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
780 if (kerr != KCGI_OK)
781 goto done;
782 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
783 if (kerr != KCGI_OK)
784 goto done;
786 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
787 "solid_line", KATTR__MAX);
788 if (kerr != KCGI_OK)
789 goto done;
790 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
791 if (kerr != KCGI_OK)
792 goto done;
794 free(age);
795 age = NULL;
797 done:
798 got_ref_list_free(&header->refs);
799 gw_free_headers(header);
800 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
801 gw_free_headers(n_header);
802 free(age);
803 free(href_diff);
804 free(href_blob);
805 if (error == NULL && kerr != KCGI_OK)
806 error = gw_kcgi_error(kerr);
807 return error;
810 static const struct got_error *
811 gw_briefs(struct gw_trans *gw_trans)
813 const struct got_error *error = NULL;
814 struct gw_header *header = NULL, *n_header = NULL;
815 char *age = NULL, *age_html = NULL;
816 char *href_diff = NULL, *href_blob = NULL;
817 char *newline, *smallerthan;
818 enum kcgi_err kerr = KCGI_OK;
820 if ((header = gw_init_header()) == NULL)
821 return got_error_from_errno("malloc");
823 if (pledge("stdio rpath proc exec sendfd unveil",
824 NULL) == -1) {
825 error = got_error_from_errno("pledge");
826 goto done;
829 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
830 if (error)
831 goto done;
833 if (gw_trans->action == GW_SUMMARY)
834 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
835 else
836 error = gw_get_header(gw_trans, header,
837 gw_trans->gw_conf->got_max_commits_display);
838 if (error)
839 goto done;
841 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
842 error = gw_get_time_str(&age, n_header->committer_time,
843 TM_DIFF);
844 if (error)
845 goto done;
847 /* briefs wrapper */
848 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
849 KATTR_ID, "briefs_wrapper", KATTR__MAX);
850 if (kerr != KCGI_OK)
851 goto done;
853 /* briefs age */
854 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
855 KATTR_ID, "briefs_age", KATTR__MAX);
856 if (kerr != KCGI_OK)
857 goto done;
858 if (asprintf(&age_html, "%s", age ? age : "") == -1) {
859 error = got_error_from_errno("asprintf");
860 goto done;
862 kerr = khtml_puts(gw_trans->gw_html_req, age_html);
863 if (kerr != KCGI_OK)
864 goto done;
865 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
866 if (kerr != KCGI_OK)
867 goto done;
869 /* briefs author */
870 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
871 KATTR_ID, "briefs_author", KATTR__MAX);
872 if (kerr != KCGI_OK)
873 goto done;
874 smallerthan = strchr(n_header->author, '<');
875 if (smallerthan)
876 *smallerthan = '\0';
877 kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
878 if (kerr != KCGI_OK)
879 goto done;
880 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
881 if (kerr != KCGI_OK)
882 goto done;
884 /* briefs log */
885 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
886 gw_trans->repo_name, n_header->commit_id) == -1) {
887 error = got_error_from_errno("asprintf");
888 goto done;
890 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
891 KATTR_ID, "briefs_log", KATTR__MAX);
892 if (kerr != KCGI_OK)
893 goto done;
894 newline = strchr(n_header->commit_msg, '\n');
895 if (newline)
896 *newline = '\0';
897 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
898 KATTR_HREF, href_diff, KATTR__MAX);
899 if (kerr != KCGI_OK)
900 goto done;
901 kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
902 if (kerr != KCGI_OK)
903 goto done;
904 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
905 if (kerr != KCGI_OK)
906 goto done;
908 /* build diff nav */
909 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
910 KATTR_ID, "navs_wrapper", KATTR__MAX);
911 if (kerr != KCGI_OK)
912 goto done;
913 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
914 KATTR_ID, "navs", KATTR__MAX);
915 if (kerr != KCGI_OK)
916 goto done;
917 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
918 KATTR_HREF, href_diff, KATTR__MAX);
919 if (kerr != KCGI_OK)
920 goto done;
921 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
922 if (kerr != KCGI_OK)
923 goto done;
924 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
925 if (kerr != KCGI_OK)
926 goto done;
928 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
929 if (kerr != KCGI_OK)
930 goto done;
932 /* build tree nav */
933 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
934 gw_trans->repo_name, n_header->commit_id) == -1) {
935 error = got_error_from_errno("asprintf");
936 goto done;
938 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
939 KATTR_HREF, href_blob, KATTR__MAX);
940 if (kerr != KCGI_OK)
941 goto done;
942 khtml_puts(gw_trans->gw_html_req, "tree");
943 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
944 if (kerr != KCGI_OK)
945 goto done;
946 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
947 if (kerr != KCGI_OK)
948 goto done;
950 /* dotted line */
951 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
952 KATTR_ID, "dotted_line", KATTR__MAX);
953 if (kerr != KCGI_OK)
954 goto done;
955 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
956 if (kerr != KCGI_OK)
957 goto done;
959 free(age);
960 age = NULL;
961 free(age_html);
962 age_html = NULL;
963 free(href_diff);
964 href_diff = NULL;
965 free(href_blob);
966 href_blob = NULL;
968 done:
969 got_ref_list_free(&header->refs);
970 gw_free_headers(header);
971 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
972 gw_free_headers(n_header);
973 free(age);
974 free(age_html);
975 free(href_diff);
976 free(href_blob);
977 if (error == NULL && kerr != KCGI_OK)
978 error = gw_kcgi_error(kerr);
979 return error;
982 static const struct got_error *
983 gw_summary(struct gw_trans *gw_trans)
985 const struct got_error *error = NULL;
986 char *age = NULL, *tags = NULL, *heads = NULL;
987 enum kcgi_err kerr = KCGI_OK;
989 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
990 return got_error_from_errno("pledge");
992 /* unveil is applied with gw_briefs below */
994 /* summary wrapper */
995 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
996 "summary_wrapper", KATTR__MAX);
997 if (kerr != KCGI_OK)
998 return gw_kcgi_error(kerr);
1000 /* description */
1001 if (gw_trans->gw_conf->got_show_repo_description &&
1002 gw_trans->gw_dir->description != NULL &&
1003 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1004 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1005 KATTR_ID, "description_title", KATTR__MAX);
1006 if (kerr != KCGI_OK)
1007 goto done;
1008 kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1009 if (kerr != KCGI_OK)
1010 goto done;
1011 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1012 if (kerr != KCGI_OK)
1013 goto done;
1014 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1015 KATTR_ID, "description", KATTR__MAX);
1016 if (kerr != KCGI_OK)
1017 goto done;
1018 kerr = khtml_puts(gw_trans->gw_html_req,
1019 gw_trans->gw_dir->description);
1020 if (kerr != KCGI_OK)
1021 goto done;
1022 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1023 if (kerr != KCGI_OK)
1024 goto done;
1027 /* repo owner */
1028 if (gw_trans->gw_conf->got_show_repo_owner &&
1029 gw_trans->gw_dir->owner != NULL) {
1030 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1031 KATTR_ID, "repo_owner_title", KATTR__MAX);
1032 if (kerr != KCGI_OK)
1033 goto done;
1034 kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1035 if (kerr != KCGI_OK)
1036 goto done;
1037 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1038 if (kerr != KCGI_OK)
1039 goto done;
1040 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1041 KATTR_ID, "repo_owner", KATTR__MAX);
1042 if (kerr != KCGI_OK)
1043 goto done;
1044 kerr = khtml_puts(gw_trans->gw_html_req,
1045 gw_trans->gw_dir->owner);
1046 if (kerr != KCGI_OK)
1047 goto done;
1048 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1049 if (kerr != KCGI_OK)
1050 goto done;
1053 /* last change */
1054 if (gw_trans->gw_conf->got_show_repo_age) {
1055 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1056 "refs/heads", TM_LONG);
1057 if (error)
1058 goto done;
1059 if (age != NULL) {
1060 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1061 KATTR_ID, "last_change_title", KATTR__MAX);
1062 if (kerr != KCGI_OK)
1063 goto done;
1064 kerr = khtml_puts(gw_trans->gw_html_req,
1065 "Last Change: ");
1066 if (kerr != KCGI_OK)
1067 goto done;
1068 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1069 if (kerr != KCGI_OK)
1070 goto done;
1071 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1072 KATTR_ID, "last_change", KATTR__MAX);
1073 if (kerr != KCGI_OK)
1074 goto done;
1075 kerr = khtml_puts(gw_trans->gw_html_req, age);
1076 if (kerr != KCGI_OK)
1077 goto done;
1078 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1079 if (kerr != KCGI_OK)
1080 goto done;
1084 /* cloneurl */
1085 if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1086 gw_trans->gw_dir->url != NULL &&
1087 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1088 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1089 KATTR_ID, "cloneurl_title", KATTR__MAX);
1090 if (kerr != KCGI_OK)
1091 goto done;
1092 kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1093 if (kerr != KCGI_OK)
1094 goto done;
1095 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1096 if (kerr != KCGI_OK)
1097 goto done;
1098 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1099 KATTR_ID, "cloneurl", KATTR__MAX);
1100 if (kerr != KCGI_OK)
1101 goto done;
1102 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1103 if (kerr != KCGI_OK)
1104 goto done;
1105 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1106 if (kerr != KCGI_OK)
1107 goto done;
1110 /* close summary wrapper */
1111 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1112 if (kerr != KCGI_OK)
1113 goto done;
1115 /* commit briefs header */
1116 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1117 "briefs_title_wrapper", KATTR__MAX);
1118 if (kerr != KCGI_OK)
1119 goto done;
1120 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1121 "briefs_title", KATTR__MAX);
1122 if (kerr != KCGI_OK)
1123 goto done;
1124 kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1125 if (kerr != KCGI_OK)
1126 goto done;
1127 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1128 if (kerr != KCGI_OK)
1129 goto done;
1130 error = gw_briefs(gw_trans);
1131 if (error)
1132 goto done;
1134 /* tags */
1135 error = gw_output_repo_tags(gw_trans, NULL, D_MAXSLCOMMDISP,
1136 TAGBRIEF);
1137 if (error)
1138 goto done;
1140 /* heads */
1141 error = gw_get_repo_heads(&heads, gw_trans);
1142 if (error)
1143 goto done;
1144 if (heads != NULL && strcmp(heads, "") != 0) {
1145 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1146 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
1147 if (kerr != KCGI_OK)
1148 goto done;
1149 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1150 KATTR_ID, "summary_heads_title", KATTR__MAX);
1151 if (kerr != KCGI_OK)
1152 goto done;
1153 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
1154 if (kerr != KCGI_OK)
1155 goto done;
1156 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1157 if (kerr != KCGI_OK)
1158 goto done;
1159 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1160 KATTR_ID, "summary_heads_content", KATTR__MAX);
1161 if (kerr != KCGI_OK)
1162 goto done;
1163 kerr = khttp_puts(gw_trans->gw_req, heads);
1164 if (kerr != KCGI_OK)
1165 goto done;
1166 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1167 if (kerr != KCGI_OK)
1168 goto done;
1170 done:
1171 free(age);
1172 free(tags);
1173 free(heads);
1174 if (error == NULL && kerr != KCGI_OK)
1175 error = gw_kcgi_error(kerr);
1176 return error;
1179 static const struct got_error *
1180 gw_tree(struct gw_trans *gw_trans)
1182 const struct got_error *error = NULL;
1183 struct gw_header *header = NULL;
1184 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1185 char *age = NULL, *age_html = NULL;
1186 enum kcgi_err kerr;
1188 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1189 return got_error_from_errno("pledge");
1191 if ((header = gw_init_header()) == NULL)
1192 return got_error_from_errno("malloc");
1194 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
1195 if (error)
1196 goto done;
1198 error = gw_get_header(gw_trans, header, 1);
1199 if (error)
1200 goto done;
1202 /* tree header */
1203 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1204 "tree_header_wrapper", KATTR__MAX);
1205 if (kerr != KCGI_OK)
1206 goto done;
1207 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1208 "tree_header", KATTR__MAX);
1209 if (kerr != KCGI_OK)
1210 goto done;
1211 error = gw_gen_tree_header(gw_trans, header->tree_id);
1212 if (error)
1213 goto done;
1214 error = gw_get_time_str(&age, header->committer_time,
1215 TM_LONG);
1216 if (error)
1217 goto done;
1218 error = gw_gen_age_header(gw_trans, age ?age : "");
1219 if (error)
1220 goto done;
1221 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1222 if (error)
1223 goto done;
1224 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1225 if (kerr != KCGI_OK)
1226 goto done;
1227 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1228 "dotted_line", KATTR__MAX);
1229 if (kerr != KCGI_OK)
1230 goto done;
1231 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1232 if (kerr != KCGI_OK)
1233 goto done;
1235 /* tree */
1236 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1237 "tree", KATTR__MAX);
1238 if (kerr != KCGI_OK)
1239 goto done;
1240 error = gw_output_repo_tree(gw_trans);
1241 if (error)
1242 goto done;
1244 /* tree content close */
1245 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1246 done:
1247 got_ref_list_free(&header->refs);
1248 gw_free_headers(header);
1249 free(tree_html_disp);
1250 free(tree_html);
1251 free(tree);
1252 free(age);
1253 free(age_html);
1254 if (error == NULL && kerr != KCGI_OK)
1255 error = gw_kcgi_error(kerr);
1256 return error;
1259 static const struct got_error *
1260 gw_tag(struct gw_trans *gw_trans)
1262 const struct got_error *error = NULL;
1263 struct gw_header *header = NULL;
1264 enum kcgi_err kerr;
1266 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1267 return got_error_from_errno("pledge");
1269 if ((header = gw_init_header()) == NULL)
1270 return got_error_from_errno("malloc");
1272 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
1273 if (error)
1274 goto done;
1276 error = gw_get_header(gw_trans, header, 1);
1277 if (error)
1278 goto done;
1280 /* tag header */
1281 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1282 "tag_header_wrapper", KATTR__MAX);
1283 if (kerr != KCGI_OK)
1284 goto done;
1285 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1286 "tag_header", KATTR__MAX);
1287 if (kerr != KCGI_OK)
1288 goto done;
1289 error = gw_gen_commit_header(gw_trans, header->commit_id,
1290 header->refs_str);
1291 if (error)
1292 goto done;
1293 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1294 if (error)
1295 goto done;
1296 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1297 if (kerr != KCGI_OK)
1298 goto done;
1299 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1300 "dotted_line", KATTR__MAX);
1301 if (kerr != KCGI_OK)
1302 goto done;
1303 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1304 if (kerr != KCGI_OK)
1305 goto done;
1307 /* tag */
1308 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1309 "tree", KATTR__MAX);
1310 if (kerr != KCGI_OK)
1311 goto done;
1313 error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1314 if (error)
1315 goto done;
1317 /* tag content close */
1318 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1319 done:
1320 got_ref_list_free(&header->refs);
1321 gw_free_headers(header);
1322 if (error == NULL && kerr != KCGI_OK)
1323 error = gw_kcgi_error(kerr);
1324 return error;
1327 static const struct got_error *
1328 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1330 const struct got_error *error = NULL;
1331 DIR *dt;
1332 char *dir_test;
1333 int opened = 0;
1335 if (asprintf(&dir_test, "%s/%s/%s",
1336 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1337 GOTWEB_GIT_DIR) == -1)
1338 return got_error_from_errno("asprintf");
1340 dt = opendir(dir_test);
1341 if (dt == NULL) {
1342 free(dir_test);
1343 } else {
1344 gw_dir->path = strdup(dir_test);
1345 opened = 1;
1346 goto done;
1349 if (asprintf(&dir_test, "%s/%s/%s",
1350 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1351 GOTWEB_GOT_DIR) == -1)
1352 return got_error_from_errno("asprintf");
1354 dt = opendir(dir_test);
1355 if (dt == NULL)
1356 free(dir_test);
1357 else {
1358 opened = 1;
1359 error = got_error(GOT_ERR_NOT_GIT_REPO);
1360 goto errored;
1363 if (asprintf(&dir_test, "%s/%s",
1364 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1)
1365 return got_error_from_errno("asprintf");
1367 gw_dir->path = strdup(dir_test);
1368 done:
1369 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1370 gw_dir->path);
1371 if (error)
1372 goto errored;
1373 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1374 if (error)
1375 goto errored;
1376 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1377 "refs/heads", TM_DIFF);
1378 if (error)
1379 goto errored;
1380 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1381 errored:
1382 free(dir_test);
1383 if (opened)
1384 closedir(dt);
1385 return error;
1388 static const struct got_error *
1389 gw_load_got_paths(struct gw_trans *gw_trans)
1391 const struct got_error *error = NULL;
1392 DIR *d;
1393 struct dirent **sd_dent;
1394 struct gw_dir *gw_dir;
1395 struct stat st;
1396 unsigned int d_cnt, d_i;
1398 d = opendir(gw_trans->gw_conf->got_repos_path);
1399 if (d == NULL) {
1400 error = got_error_from_errno2("opendir",
1401 gw_trans->gw_conf->got_repos_path);
1402 return error;
1405 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1406 alphasort);
1407 if (d_cnt == -1) {
1408 error = got_error_from_errno2("scandir",
1409 gw_trans->gw_conf->got_repos_path);
1410 return error;
1413 for (d_i = 0; d_i < d_cnt; d_i++) {
1414 if (gw_trans->gw_conf->got_max_repos > 0 &&
1415 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1416 break; /* account for parent and self */
1418 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1419 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1420 continue;
1422 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
1423 return got_error_from_errno("gw_dir malloc");
1425 error = gw_load_got_path(gw_trans, gw_dir);
1426 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1427 error = NULL;
1428 continue;
1430 else if (error)
1431 return error;
1433 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1434 !got_path_dir_is_empty(gw_dir->path)) {
1435 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1436 entry);
1437 gw_trans->repos_total++;
1441 closedir(d);
1442 return error;
1445 static const struct got_error *
1446 gw_parse_querystring(struct gw_trans *gw_trans)
1448 const struct got_error *error = NULL;
1449 struct kpair *p;
1450 struct gw_query_action *action = NULL;
1451 unsigned int i;
1453 if (gw_trans->gw_req->fieldnmap[0]) {
1454 error = got_error_from_errno("bad parse");
1455 return error;
1456 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1457 /* define gw_trans->repo_path */
1458 if (asprintf(&gw_trans->repo_name, "%s", p->parsed.s) == -1)
1459 return got_error_from_errno("asprintf");
1461 if (asprintf(&gw_trans->repo_path, "%s/%s",
1462 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
1463 return got_error_from_errno("asprintf");
1465 /* get action and set function */
1466 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
1467 for (i = 0; i < nitems(gw_query_funcs); i++) {
1468 action = &gw_query_funcs[i];
1469 if (action->func_name == NULL)
1470 continue;
1472 if (strcmp(action->func_name,
1473 p->parsed.s) == 0) {
1474 gw_trans->action = i;
1475 if (asprintf(&gw_trans->action_name,
1476 "%s", action->func_name) == -1)
1477 return
1478 got_error_from_errno(
1479 "asprintf");
1481 break;
1484 action = NULL;
1487 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
1488 if (asprintf(&gw_trans->commit, "%s",
1489 p->parsed.s) == -1)
1490 return got_error_from_errno("asprintf");
1492 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1493 if (asprintf(&gw_trans->repo_file, "%s",
1494 p->parsed.s) == -1)
1495 return got_error_from_errno("asprintf");
1497 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER]))
1498 if (asprintf(&gw_trans->repo_folder, "%s",
1499 p->parsed.s) == -1)
1500 return got_error_from_errno("asprintf");
1502 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
1503 if (asprintf(&gw_trans->headref, "%s",
1504 p->parsed.s) == -1)
1505 return got_error_from_errno("asprintf");
1507 if (action == NULL) {
1508 error = got_error_from_errno("invalid action");
1509 return error;
1511 if ((gw_trans->gw_dir =
1512 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
1513 return got_error_from_errno("gw_dir malloc");
1515 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1516 if (error)
1517 return error;
1518 } else
1519 gw_trans->action = GW_INDEX;
1521 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1522 gw_trans->page = p->parsed.i;
1524 return error;
1527 static struct gw_dir *
1528 gw_init_gw_dir(char *dir)
1530 struct gw_dir *gw_dir;
1532 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
1533 return NULL;
1535 if (asprintf(&gw_dir->name, "%s", dir) == -1)
1536 return NULL;
1538 return gw_dir;
1541 static const struct got_error *
1542 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1544 enum kcgi_err kerr;
1546 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1547 if (kerr != KCGI_OK)
1548 return gw_kcgi_error(kerr);
1549 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1550 khttps[code]);
1551 if (kerr != KCGI_OK)
1552 return gw_kcgi_error(kerr);
1553 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1554 kmimetypes[mime]);
1555 if (kerr != KCGI_OK)
1556 return gw_kcgi_error(kerr);
1557 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
1558 "nosniff");
1559 if (kerr != KCGI_OK)
1560 return gw_kcgi_error(kerr);
1561 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1562 if (kerr != KCGI_OK)
1563 return gw_kcgi_error(kerr);
1564 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
1565 "1; mode=block");
1566 if (kerr != KCGI_OK)
1567 return gw_kcgi_error(kerr);
1569 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
1570 kerr = khttp_head(gw_trans->gw_req,
1571 kresps[KRESP_CONTENT_DISPOSITION],
1572 "attachment; filename=%s", gw_trans->repo_file);
1573 if (kerr != KCGI_OK)
1574 return gw_kcgi_error(kerr);
1577 kerr = khttp_body(gw_trans->gw_req);
1578 return gw_kcgi_error(kerr);
1581 static const struct got_error *
1582 gw_display_index(struct gw_trans *gw_trans)
1584 const struct got_error *error;
1585 enum kcgi_err kerr;
1587 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1588 if (error)
1589 return error;
1591 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1592 if (kerr != KCGI_OK)
1593 return gw_kcgi_error(kerr);
1595 if (gw_trans->action != GW_BLOB) {
1596 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1597 gw_query_funcs[gw_trans->action].template);
1598 if (kerr != KCGI_OK) {
1599 khtml_close(gw_trans->gw_html_req);
1600 return gw_kcgi_error(kerr);
1604 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
1607 static void
1608 gw_display_error(struct gw_trans *gw_trans, const struct got_error *err)
1610 if (gw_display_open(gw_trans, KHTTP_200, gw_trans->mime) != NULL)
1611 return;
1613 if (khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0) != KCGI_OK)
1614 return;
1615 khtml_puts(gw_trans->gw_html_req, err->msg);
1616 khtml_close(gw_trans->gw_html_req);
1619 static int
1620 gw_template(size_t key, void *arg)
1622 const struct got_error *error = NULL;
1623 enum kcgi_err kerr;
1624 struct gw_trans *gw_trans = arg;
1625 char *gw_site_link, *img_src = NULL;
1627 switch (key) {
1628 case (TEMPL_HEAD):
1629 kerr = khttp_puts(gw_trans->gw_req, head);
1630 if (kerr != KCGI_OK)
1631 return 0;
1632 break;
1633 case(TEMPL_HEADER):
1634 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1635 KATTR_ID, "got_link", KATTR__MAX);
1636 if (kerr != KCGI_OK)
1637 return 0;
1638 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1639 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
1640 KATTR_TARGET, "_sotd", KATTR__MAX);
1641 if (kerr != KCGI_OK)
1642 return 0;
1643 if (asprintf(&img_src, "/%s",
1644 gw_trans->gw_conf->got_logo) == -1)
1645 return 0;
1646 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
1647 KATTR_SRC, img_src, KATTR__MAX);
1648 if (kerr != KCGI_OK) {
1649 free(img_src);
1650 return 0;
1652 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1653 if (kerr != KCGI_OK) {
1654 free(img_src);
1655 return 0;
1657 break;
1658 case (TEMPL_SITEPATH):
1659 gw_site_link = gw_get_site_link(gw_trans);
1660 if (gw_site_link != NULL) {
1661 kerr = khttp_puts(gw_trans->gw_req, gw_site_link);
1662 if (kerr != KCGI_OK) {
1663 free(gw_site_link);
1664 return 0;
1667 free(gw_site_link);
1668 break;
1669 case(TEMPL_TITLE):
1670 if (gw_trans->gw_conf->got_site_name != NULL) {
1671 kerr = khtml_puts(gw_trans->gw_html_req,
1672 gw_trans->gw_conf->got_site_name);
1673 if (kerr != KCGI_OK)
1674 return 0;
1676 break;
1677 case (TEMPL_SEARCH):
1678 kerr = khttp_puts(gw_trans->gw_req, search);
1679 if (kerr != KCGI_OK)
1680 return 0;
1681 break;
1682 case(TEMPL_SITEOWNER):
1683 if (gw_trans->gw_conf->got_site_owner != NULL &&
1684 gw_trans->gw_conf->got_show_site_owner) {
1685 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1686 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
1687 if (kerr != KCGI_OK)
1688 return 0;
1689 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1690 KATTR_ID, "site_owner", KATTR__MAX);
1691 if (kerr != KCGI_OK)
1692 return 0;
1693 kerr = khtml_puts(gw_trans->gw_html_req,
1694 gw_trans->gw_conf->got_site_owner);
1695 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1696 if (kerr != KCGI_OK)
1697 return 0;
1699 break;
1700 case(TEMPL_CONTENT):
1701 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1702 if (error) {
1703 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1704 KATTR_ID, "tmpl_err", KATTR__MAX);
1705 if (kerr != KCGI_OK)
1706 return 0;
1707 kerr = khttp_puts(gw_trans->gw_req, "Error: ");
1708 if (kerr != KCGI_OK)
1709 return 0;
1710 kerr = khttp_puts(gw_trans->gw_req, error->msg);
1711 if (kerr != KCGI_OK)
1712 return 0;
1713 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1714 if (kerr != KCGI_OK)
1715 return 0;
1717 break;
1718 default:
1719 return 0;
1721 return 1;
1724 static const struct got_error *
1725 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
1727 const struct got_error *error = NULL;
1728 char *ref_str = NULL;
1729 enum kcgi_err kerr = KCGI_OK;
1731 if (strcmp(str2, "") != 0) {
1732 if (asprintf(&ref_str, "(%s)", str2) == -1) {
1733 error = got_error_from_errno("asprintf");
1734 goto done;
1736 } else
1737 ref_str = strdup("");
1739 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1740 KATTR_ID, "header_commit_title", KATTR__MAX);
1741 if (kerr != KCGI_OK)
1742 goto done;
1743 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
1744 if (kerr != KCGI_OK)
1745 goto done;
1746 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1747 if (kerr != KCGI_OK)
1748 goto done;
1749 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1750 KATTR_ID, "header_commit", KATTR__MAX);
1751 if (kerr != KCGI_OK)
1752 goto done;
1753 kerr = khtml_puts(gw_trans->gw_html_req, str1);
1754 if (kerr != KCGI_OK)
1755 goto done;
1756 kerr = khtml_puts(gw_trans->gw_html_req, " ");
1757 if (kerr != KCGI_OK)
1758 goto done;
1759 kerr = khtml_puts(gw_trans->gw_html_req, ref_str);
1760 if (kerr != KCGI_OK)
1761 goto done;
1762 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1763 done:
1764 if (error == NULL && kerr != KCGI_OK)
1765 error = gw_kcgi_error(kerr);
1766 return error;
1769 static const struct got_error *
1770 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
1772 const struct got_error *error = NULL;
1773 enum kcgi_err kerr = KCGI_OK;
1775 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1776 KATTR_ID, "header_diff_title", KATTR__MAX);
1777 if (kerr != KCGI_OK)
1778 goto done;
1779 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
1780 if (kerr != KCGI_OK)
1781 goto done;
1782 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1783 if (kerr != KCGI_OK)
1784 goto done;
1785 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1786 KATTR_ID, "header_diff", KATTR__MAX);
1787 if (kerr != KCGI_OK)
1788 goto done;
1789 kerr = khtml_puts(gw_trans->gw_html_req, str1);
1790 if (kerr != KCGI_OK)
1791 goto done;
1792 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
1793 if (kerr != KCGI_OK)
1794 goto done;
1795 kerr = khtml_puts(gw_trans->gw_html_req, str2);
1796 if (kerr != KCGI_OK)
1797 goto done;
1798 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1799 done:
1800 if (error == NULL && kerr != KCGI_OK)
1801 error = gw_kcgi_error(kerr);
1802 return error;
1805 static const struct got_error *
1806 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
1808 const struct got_error *error = NULL;
1809 enum kcgi_err kerr = KCGI_OK;
1811 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1812 KATTR_ID, "header_age_title", KATTR__MAX);
1813 if (kerr != KCGI_OK)
1814 goto done;
1815 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
1816 if (kerr != KCGI_OK)
1817 goto done;
1818 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1819 if (kerr != KCGI_OK)
1820 goto done;
1821 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1822 KATTR_ID, "header_age", KATTR__MAX);
1823 if (kerr != KCGI_OK)
1824 goto done;
1825 kerr = khtml_puts(gw_trans->gw_html_req, str);
1826 if (kerr != KCGI_OK)
1827 goto done;
1828 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1829 done:
1830 if (error == NULL && kerr != KCGI_OK)
1831 error = gw_kcgi_error(kerr);
1832 return error;
1835 static const struct got_error *
1836 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
1838 const struct got_error *error = NULL;
1839 enum kcgi_err kerr;
1841 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1842 KATTR_ID, "header_author_title", KATTR__MAX);
1843 if (kerr != KCGI_OK)
1844 goto done;
1845 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
1846 if (kerr != KCGI_OK)
1847 goto done;
1848 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1849 if (kerr != KCGI_OK)
1850 goto done;
1851 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1852 KATTR_ID, "header_author", KATTR__MAX);
1853 if (kerr != KCGI_OK)
1854 goto done;
1855 kerr = khtml_puts(gw_trans->gw_html_req, str);
1856 if (kerr != KCGI_OK)
1857 goto done;
1858 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1859 done:
1860 if (error == NULL && kerr != KCGI_OK)
1861 error = gw_kcgi_error(kerr);
1862 return error;
1865 static const struct got_error *
1866 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
1868 const struct got_error *error = NULL;
1869 enum kcgi_err kerr;
1871 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1872 KATTR_ID, "header_committer_title", KATTR__MAX);
1873 if (kerr != KCGI_OK)
1874 goto done;
1875 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
1876 if (kerr != KCGI_OK)
1877 goto done;
1878 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1879 if (kerr != KCGI_OK)
1880 goto done;
1881 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1882 KATTR_ID, "header_committer", KATTR__MAX);
1883 if (kerr != KCGI_OK)
1884 goto done;
1885 kerr = khtml_puts(gw_trans->gw_html_req, str);
1886 if (kerr != KCGI_OK)
1887 goto done;
1888 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1889 done:
1890 if (error == NULL && kerr != KCGI_OK)
1891 error = gw_kcgi_error(kerr);
1892 return error;
1895 static const struct got_error *
1896 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
1898 const struct got_error *error = NULL;
1899 enum kcgi_err kerr;
1901 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1902 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
1903 if (kerr != KCGI_OK)
1904 goto done;
1905 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
1906 if (kerr != KCGI_OK)
1907 goto done;
1908 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1909 if (kerr != KCGI_OK)
1910 goto done;
1911 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1912 KATTR_ID, "header_commit_msg", KATTR__MAX);
1913 if (kerr != KCGI_OK)
1914 goto done;
1915 kerr = khttp_puts(gw_trans->gw_req, str);
1916 if (kerr != KCGI_OK)
1917 goto done;
1918 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1919 done:
1920 if (error == NULL && kerr != KCGI_OK)
1921 error = gw_kcgi_error(kerr);
1922 return error;
1925 static const struct got_error *
1926 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
1928 const struct got_error *error = NULL;
1929 enum kcgi_err kerr;
1931 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1932 KATTR_ID, "header_tree_title", KATTR__MAX);
1933 if (kerr != KCGI_OK)
1934 goto done;
1935 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
1936 if (kerr != KCGI_OK)
1937 goto done;
1938 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1939 if (kerr != KCGI_OK)
1940 goto done;
1941 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1942 KATTR_ID, "header_tree", KATTR__MAX);
1943 if (kerr != KCGI_OK)
1944 goto done;
1945 kerr = khtml_puts(gw_trans->gw_html_req, str);
1946 if (kerr != KCGI_OK)
1947 goto done;
1948 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1949 done:
1950 if (error == NULL && kerr != KCGI_OK)
1951 error = gw_kcgi_error(kerr);
1952 return error;
1955 static const struct got_error *
1956 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
1957 char *dir)
1959 const struct got_error *error = NULL;
1960 FILE *f = NULL;
1961 char *d_file = NULL;
1962 unsigned int len;
1963 size_t n;
1965 *description = NULL;
1966 if (gw_trans->gw_conf->got_show_repo_description == 0)
1967 return gw_empty_string(description);
1969 if (asprintf(&d_file, "%s/description", dir) == -1)
1970 return got_error_from_errno("asprintf");
1972 f = fopen(d_file, "r");
1973 if (f == NULL) {
1974 if (errno == ENOENT || errno == EACCES)
1975 return gw_empty_string(description);
1976 error = got_error_from_errno2("fopen", d_file);
1977 goto done;
1980 if (fseek(f, 0, SEEK_END) == -1) {
1981 error = got_ferror(f, GOT_ERR_IO);
1982 goto done;
1984 len = ftell(f);
1985 if (len == -1) {
1986 error = got_ferror(f, GOT_ERR_IO);
1987 goto done;
1989 if (fseek(f, 0, SEEK_SET) == -1) {
1990 error = got_ferror(f, GOT_ERR_IO);
1991 goto done;
1993 *description = calloc(len + 1, sizeof(**description));
1994 if (*description == NULL) {
1995 error = got_error_from_errno("calloc");
1996 goto done;
1999 n = fread(*description, 1, len, f);
2000 if (n == 0 && ferror(f))
2001 error = got_ferror(f, GOT_ERR_IO);
2002 done:
2003 if (f != NULL && fclose(f) == -1 && error == NULL)
2004 error = got_error_from_errno("fclose");
2005 free(d_file);
2006 return error;
2009 static const struct got_error *
2010 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2012 struct tm tm;
2013 time_t diff_time;
2014 char *years = "years ago", *months = "months ago";
2015 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2016 char *minutes = "minutes ago", *seconds = "seconds ago";
2017 char *now = "right now";
2018 char *s;
2019 char datebuf[29];
2021 *repo_age = NULL;
2023 switch (ref_tm) {
2024 case TM_DIFF:
2025 diff_time = time(NULL) - committer_time;
2026 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2027 if (asprintf(repo_age, "%lld %s",
2028 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2029 return got_error_from_errno("asprintf");
2030 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2031 if (asprintf(repo_age, "%lld %s",
2032 (diff_time / 60 / 60 / 24 / (365 / 12)),
2033 months) == -1)
2034 return got_error_from_errno("asprintf");
2035 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2036 if (asprintf(repo_age, "%lld %s",
2037 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2038 return got_error_from_errno("asprintf");
2039 } else if (diff_time > 60 * 60 * 24 * 2) {
2040 if (asprintf(repo_age, "%lld %s",
2041 (diff_time / 60 / 60 / 24), days) == -1)
2042 return got_error_from_errno("asprintf");
2043 } else if (diff_time > 60 * 60 * 2) {
2044 if (asprintf(repo_age, "%lld %s",
2045 (diff_time / 60 / 60), hours) == -1)
2046 return got_error_from_errno("asprintf");
2047 } else if (diff_time > 60 * 2) {
2048 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2049 minutes) == -1)
2050 return got_error_from_errno("asprintf");
2051 } else if (diff_time > 2) {
2052 if (asprintf(repo_age, "%lld %s", diff_time,
2053 seconds) == -1)
2054 return got_error_from_errno("asprintf");
2055 } else {
2056 if (asprintf(repo_age, "%s", now) == -1)
2057 return got_error_from_errno("asprintf");
2059 break;
2060 case TM_LONG:
2061 if (gmtime_r(&committer_time, &tm) == NULL)
2062 return got_error_from_errno("gmtime_r");
2064 s = asctime_r(&tm, datebuf);
2065 if (s == NULL)
2066 return got_error_from_errno("asctime_r");
2068 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2069 return got_error_from_errno("asprintf");
2070 break;
2072 return NULL;
2075 static const struct got_error *
2076 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2077 char *repo_ref, int ref_tm)
2079 const struct got_error *error = NULL;
2080 struct got_object_id *id = NULL;
2081 struct got_repository *repo = NULL;
2082 struct got_commit_object *commit = NULL;
2083 struct got_reflist_head refs;
2084 struct got_reflist_entry *re;
2085 struct got_reference *head_ref;
2086 int is_head = 0;
2087 time_t committer_time = 0, cmp_time = 0;
2088 const char *refname;
2090 *repo_age = NULL;
2091 SIMPLEQ_INIT(&refs);
2093 if (repo_ref == NULL)
2094 return NULL;
2096 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
2097 is_head = 1;
2099 if (gw_trans->gw_conf->got_show_repo_age == 0)
2100 return NULL;
2102 error = got_repo_open(&repo, dir, NULL);
2103 if (error)
2104 goto done;
2106 if (is_head)
2107 error = got_ref_list(&refs, repo, "refs/heads",
2108 got_ref_cmp_by_name, NULL);
2109 else
2110 error = got_ref_list(&refs, repo, repo_ref,
2111 got_ref_cmp_by_name, NULL);
2112 if (error)
2113 goto done;
2115 SIMPLEQ_FOREACH(re, &refs, entry) {
2116 if (is_head)
2117 refname = strdup(repo_ref);
2118 else
2119 refname = got_ref_get_name(re->ref);
2120 error = got_ref_open(&head_ref, repo, refname, 0);
2121 if (error)
2122 goto done;
2124 error = got_ref_resolve(&id, repo, head_ref);
2125 got_ref_close(head_ref);
2126 if (error)
2127 goto done;
2129 error = got_object_open_as_commit(&commit, repo, id);
2130 if (error)
2131 goto done;
2133 committer_time =
2134 got_object_commit_get_committer_time(commit);
2136 if (cmp_time < committer_time)
2137 cmp_time = committer_time;
2140 if (cmp_time != 0) {
2141 committer_time = cmp_time;
2142 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2144 done:
2145 got_ref_list_free(&refs);
2146 free(id);
2147 return error;
2150 static const struct got_error *
2151 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2153 const struct got_error *error;
2154 FILE *f = NULL;
2155 struct got_object_id *id1 = NULL, *id2 = NULL;
2156 char *label1 = NULL, *label2 = NULL, *line = NULL;
2157 int obj_type;
2158 size_t linesize = 0;
2159 ssize_t linelen;
2160 enum kcgi_err kerr = KCGI_OK;
2162 f = got_opentemp();
2163 if (f == NULL)
2164 return NULL;
2166 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
2167 if (error)
2168 goto done;
2170 if (strncmp(header->parent_id, "/dev/null", 9) != 0) {
2171 error = got_repo_match_object_id(&id1, &label1,
2172 header->parent_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2173 if (error)
2174 goto done;
2177 error = got_repo_match_object_id(&id2, &label2,
2178 header->commit_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2179 if (error)
2180 goto done;
2182 error = got_object_get_type(&obj_type, header->repo, id2);
2183 if (error)
2184 goto done;
2185 switch (obj_type) {
2186 case GOT_OBJ_TYPE_BLOB:
2187 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
2188 header->repo, f);
2189 break;
2190 case GOT_OBJ_TYPE_TREE:
2191 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
2192 header->repo, f);
2193 break;
2194 case GOT_OBJ_TYPE_COMMIT:
2195 error = got_diff_objects_as_commits(id1, id2, 3, 0,
2196 header->repo, f);
2197 break;
2198 default:
2199 error = got_error(GOT_ERR_OBJ_TYPE);
2201 if (error)
2202 goto done;
2204 if (fseek(f, 0, SEEK_SET) == -1) {
2205 error = got_ferror(f, GOT_ERR_IO);
2206 goto done;
2209 while ((linelen = getline(&line, &linesize, f)) != -1) {
2210 error = gw_colordiff_line(gw_trans, line);
2211 if (error)
2212 goto done;
2213 /* XXX: KHTML_PRETTY breaks this */
2214 kerr = khtml_puts(gw_trans->gw_html_req, line);
2215 if (kerr != KCGI_OK)
2216 goto done;
2217 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2218 if (kerr != KCGI_OK)
2219 goto done;
2221 if (linelen == -1 && ferror(f))
2222 error = got_error_from_errno("getline");
2223 done:
2224 if (f && fclose(f) == -1 && error == NULL)
2225 error = got_error_from_errno("fclose");
2226 free(line);
2227 free(label1);
2228 free(label2);
2229 free(id1);
2230 free(id2);
2232 if (error == NULL && kerr != KCGI_OK)
2233 error = gw_kcgi_error(kerr);
2234 return error;
2237 static const struct got_error *
2238 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2240 const struct got_error *error = NULL;
2241 struct got_repository *repo;
2242 const char *gitconfig_owner;
2244 *owner = NULL;
2246 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2247 return NULL;
2249 error = got_repo_open(&repo, dir, NULL);
2250 if (error)
2251 return error;
2252 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2253 if (gitconfig_owner) {
2254 *owner = strdup(gitconfig_owner);
2255 if (*owner == NULL)
2256 error = got_error_from_errno("strdup");
2258 got_repo_close(repo);
2259 return error;
2262 static const struct got_error *
2263 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2265 const struct got_error *error = NULL;
2266 FILE *f;
2267 char *d_file = NULL;
2268 unsigned int len;
2269 size_t n;
2271 *url = NULL;
2273 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2274 return got_error_from_errno("asprintf");
2276 f = fopen(d_file, "r");
2277 if (f == NULL) {
2278 if (errno != ENOENT && errno != EACCES)
2279 error = got_error_from_errno2("fopen", d_file);
2280 goto done;
2283 if (fseek(f, 0, SEEK_END) == -1) {
2284 error = got_ferror(f, GOT_ERR_IO);
2285 goto done;
2287 len = ftell(f);
2288 if (len == -1) {
2289 error = got_ferror(f, GOT_ERR_IO);
2290 goto done;
2292 if (fseek(f, 0, SEEK_SET) == -1) {
2293 error = got_ferror(f, GOT_ERR_IO);
2294 goto done;
2297 *url = calloc(len + 1, sizeof(**url));
2298 if (*url == NULL) {
2299 error = got_error_from_errno("calloc");
2300 goto done;
2303 n = fread(*url, 1, len, f);
2304 if (n == 0 && ferror(f))
2305 error = got_ferror(f, GOT_ERR_IO);
2306 done:
2307 if (f && fclose(f) == -1 && error == NULL)
2308 error = got_error_from_errno("fclose");
2309 free(d_file);
2310 return NULL;
2313 static const struct got_error *
2314 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
2315 int limit, int tag_type)
2317 const struct got_error *error = NULL;
2318 struct got_repository *repo = NULL;
2319 struct got_reflist_head refs;
2320 struct got_reflist_entry *re;
2321 char *age = NULL;
2322 char *id_str = NULL, *newline, *href_commits = NULL;
2323 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
2324 struct got_tag_object *tag = NULL;
2325 enum kcgi_err kerr = KCGI_OK;
2326 int summary_header_displayed = 0;
2328 SIMPLEQ_INIT(&refs);
2330 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2331 if (error)
2332 goto done;
2334 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
2335 if (error)
2336 goto done;
2338 SIMPLEQ_FOREACH(re, &refs, entry) {
2339 const char *refname;
2340 const char *tagger;
2341 const char *tag_commit;
2342 time_t tagger_time;
2343 struct got_object_id *id;
2344 struct got_commit_object *commit = NULL;
2346 refname = got_ref_get_name(re->ref);
2347 if (strncmp(refname, "refs/tags/", 10) != 0)
2348 continue;
2349 refname += 10;
2351 error = got_ref_resolve(&id, repo, re->ref);
2352 if (error)
2353 goto done;
2355 error = got_object_open_as_tag(&tag, repo, id);
2356 if (error) {
2357 if (error->code != GOT_ERR_OBJ_TYPE) {
2358 free(id);
2359 goto done;
2361 /* "lightweight" tag */
2362 error = got_object_open_as_commit(&commit, repo, id);
2363 if (error) {
2364 free(id);
2365 goto done;
2367 tagger = got_object_commit_get_committer(commit);
2368 tagger_time =
2369 got_object_commit_get_committer_time(commit);
2370 error = got_object_id_str(&id_str, id);
2371 free(id);
2372 if (error)
2373 goto done;
2374 } else {
2375 free(id);
2376 tagger = got_object_tag_get_tagger(tag);
2377 tagger_time = got_object_tag_get_tagger_time(tag);
2378 error = got_object_id_str(&id_str,
2379 got_object_tag_get_object_id(tag));
2380 if (error)
2381 goto done;
2383 if (error)
2384 goto done;
2386 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
2387 strlen(id_str)) != 0)
2388 continue;
2390 if (commit) {
2391 error = got_object_commit_get_logmsg(&tag_commit0,
2392 commit);
2393 if (error)
2394 goto done;
2395 got_object_commit_close(commit);
2396 } else {
2397 tag_commit0 = strdup(got_object_tag_get_message(tag));
2398 if (tag_commit0 == NULL) {
2399 error = got_error_from_errno("strdup");
2400 goto done;
2404 tag_commit = tag_commit0;
2405 while (*tag_commit == '\n')
2406 tag_commit++;
2408 switch (tag_type) {
2409 case TAGBRIEF:
2410 newline = strchr(tag_commit, '\n');
2411 if (newline)
2412 *newline = '\0';
2414 if (summary_header_displayed == 0) {
2415 kerr = khtml_attr(gw_trans->gw_html_req,
2416 KELEM_DIV, KATTR_ID,
2417 "summary_tags_title_wrapper", KATTR__MAX);
2418 if (kerr != KCGI_OK)
2419 goto done;
2420 kerr = khtml_attr(gw_trans->gw_html_req,
2421 KELEM_DIV, KATTR_ID,
2422 "summary_tags_title", KATTR__MAX);
2423 if (kerr != KCGI_OK)
2424 goto done;
2425 kerr = khtml_puts(gw_trans->gw_html_req,
2426 "Tags");
2427 if (kerr != KCGI_OK)
2428 goto done;
2429 kerr = khtml_closeelem(gw_trans->gw_html_req,
2430 2);
2431 if (kerr != KCGI_OK)
2432 goto done;
2433 kerr = khtml_attr(gw_trans->gw_html_req,
2434 KELEM_DIV, KATTR_ID,
2435 "summary_tags_content", KATTR__MAX);
2436 if (kerr != KCGI_OK)
2437 goto done;
2438 summary_header_displayed = 1;
2441 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2442 KATTR_ID, "tags_wrapper", KATTR__MAX);
2443 if (kerr != KCGI_OK)
2444 goto done;
2445 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2446 KATTR_ID, "tags_age", KATTR__MAX);
2447 if (kerr != KCGI_OK)
2448 goto done;
2449 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
2450 if (error)
2451 goto done;
2452 kerr = khtml_puts(gw_trans->gw_html_req,
2453 age ? age : "");
2454 if (kerr != KCGI_OK)
2455 goto done;
2456 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2457 if (kerr != KCGI_OK)
2458 goto done;
2459 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2460 KATTR_ID, "tags", KATTR__MAX);
2461 if (kerr != KCGI_OK)
2462 goto done;
2463 kerr = khtml_puts(gw_trans->gw_html_req, refname);
2464 if (kerr != KCGI_OK)
2465 goto done;
2466 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2467 if (kerr != KCGI_OK)
2468 goto done;
2469 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2470 KATTR_ID, "tags_name", KATTR__MAX);
2471 if (kerr != KCGI_OK)
2472 goto done;
2473 if (asprintf(&href_tag, "?path=%s&action=tag&commit=%s",
2474 gw_trans->repo_name, id_str) == -1) {
2475 error = got_error_from_errno("asprintf");
2476 goto done;
2478 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2479 KATTR_HREF, href_tag, KATTR__MAX);
2480 if (kerr != KCGI_OK)
2481 goto done;
2482 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
2483 if (kerr != KCGI_OK)
2484 goto done;
2485 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2486 if (kerr != KCGI_OK)
2487 goto done;
2489 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2490 KATTR_ID, "navs_wrapper", KATTR__MAX);
2491 if (kerr != KCGI_OK)
2492 goto done;
2493 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2494 KATTR_ID, "navs", KATTR__MAX);
2495 if (kerr != KCGI_OK)
2496 goto done;
2498 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2499 KATTR_HREF, href_tag, KATTR__MAX);
2500 if (kerr != KCGI_OK)
2501 goto done;
2502 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
2503 if (kerr != KCGI_OK)
2504 goto done;
2505 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2506 if (kerr != KCGI_OK)
2507 goto done;
2509 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2510 if (kerr != KCGI_OK)
2511 goto done;
2512 if (asprintf(&href_briefs,
2513 "?path=%s&action=briefs&commit=%s",
2514 gw_trans->repo_name, id_str) == -1) {
2515 error = got_error_from_errno("asprintf");
2516 goto done;
2518 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2519 KATTR_HREF, href_briefs, KATTR__MAX);
2520 if (kerr != KCGI_OK)
2521 goto done;
2522 kerr = khtml_puts(gw_trans->gw_html_req,
2523 "commit briefs");
2524 if (kerr != KCGI_OK)
2525 goto done;
2526 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2527 if (kerr != KCGI_OK)
2528 goto done;
2530 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2531 if (kerr != KCGI_OK)
2532 goto done;
2534 if (asprintf(&href_commits,
2535 "?path=%s&action=commits&commit=%s",
2536 gw_trans->repo_name, id_str) == -1) {
2537 error = got_error_from_errno("asprintf");
2538 goto done;
2540 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2541 KATTR_HREF, href_commits, KATTR__MAX);
2542 if (kerr != KCGI_OK)
2543 goto done;
2544 kerr = khtml_puts(gw_trans->gw_html_req,
2545 "commits");
2546 if (kerr != KCGI_OK)
2547 goto done;
2548 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2549 if (kerr != KCGI_OK)
2550 goto done;
2552 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2553 KATTR_ID, "dotted_line", KATTR__MAX);
2554 if (kerr != KCGI_OK)
2555 goto done;
2556 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2557 if (kerr != KCGI_OK)
2558 goto done;
2559 break;
2560 case TAGFULL:
2561 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2562 KATTR_ID, "tag_info_date_title", KATTR__MAX);
2563 if (kerr != KCGI_OK)
2564 goto done;
2565 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
2566 if (kerr != KCGI_OK)
2567 goto done;
2568 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2569 if (kerr != KCGI_OK)
2570 goto done;
2571 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2572 KATTR_ID, "tag_info_date", KATTR__MAX);
2573 if (kerr != KCGI_OK)
2574 goto done;
2575 error = gw_get_time_str(&age, tagger_time, TM_LONG);
2576 if (error)
2577 goto done;
2578 kerr = khtml_puts(gw_trans->gw_html_req,
2579 age ? age : "");
2580 if (kerr != KCGI_OK)
2581 goto done;
2582 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2583 if (kerr != KCGI_OK)
2584 goto done;
2586 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2587 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
2588 if (kerr != KCGI_OK)
2589 goto done;
2590 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
2591 if (kerr != KCGI_OK)
2592 goto done;
2593 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2594 if (kerr != KCGI_OK)
2595 goto done;
2596 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2597 KATTR_ID, "tag_info_date", KATTR__MAX);
2598 if (kerr != KCGI_OK)
2599 goto done;
2600 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
2601 if (kerr != KCGI_OK)
2602 goto done;
2603 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2604 if (kerr != KCGI_OK)
2605 goto done;
2607 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2608 KATTR_ID, "tag_info", KATTR__MAX);
2609 if (kerr != KCGI_OK)
2610 goto done;
2611 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
2612 if (kerr != KCGI_OK)
2613 goto done;
2614 break;
2615 default:
2616 break;
2618 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2619 if (kerr != KCGI_OK)
2620 goto done;
2622 if (limit && --limit == 0)
2623 break;
2625 if (tag)
2626 got_object_tag_close(tag);
2627 tag = NULL;
2628 free(id_str);
2629 id_str = NULL;
2630 free(age);
2631 age = NULL;
2632 free(tag_commit0);
2633 tag_commit0 = NULL;
2634 free(href_tag);
2635 href_tag = NULL;
2636 free(href_briefs);
2637 href_briefs = NULL;
2638 free(href_commits);
2639 href_commits = NULL;
2641 done:
2642 if (tag)
2643 got_object_tag_close(tag);
2644 free(id_str);
2645 free(age);
2646 free(tag_commit0);
2647 free(href_tag);
2648 free(href_briefs);
2649 free(href_commits);
2650 got_ref_list_free(&refs);
2651 if (repo)
2652 got_repo_close(repo);
2653 if (error == NULL && kerr != KCGI_OK)
2654 error = gw_kcgi_error(kerr);
2655 return error;
2658 static void
2659 gw_free_headers(struct gw_header *header)
2661 free(header->id);
2662 free(header->path);
2663 if (header->commit != NULL)
2664 got_object_commit_close(header->commit);
2665 if (header->repo)
2666 got_repo_close(header->repo);
2667 free(header->refs_str);
2668 free(header->commit_id);
2669 free(header->parent_id);
2670 free(header->tree_id);
2671 free(header->commit_msg);
2674 static struct gw_header *
2675 gw_init_header()
2677 struct gw_header *header;
2679 header = malloc(sizeof(*header));
2680 if (header == NULL)
2681 return NULL;
2683 header->repo = NULL;
2684 header->commit = NULL;
2685 header->id = NULL;
2686 header->path = NULL;
2687 SIMPLEQ_INIT(&header->refs);
2689 return header;
2692 static const struct got_error *
2693 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
2694 int limit)
2696 const struct got_error *error = NULL;
2697 struct got_commit_graph *graph = NULL;
2699 error = got_commit_graph_open(&graph, header->path, 0);
2700 if (error)
2701 goto done;
2703 error = got_commit_graph_iter_start(graph, header->id, header->repo,
2704 NULL, NULL);
2705 if (error)
2706 goto done;
2708 for (;;) {
2709 error = got_commit_graph_iter_next(&header->id, graph,
2710 header->repo, NULL, NULL);
2711 if (error) {
2712 if (error->code == GOT_ERR_ITER_COMPLETED)
2713 error = NULL;
2714 goto done;
2716 if (header->id == NULL)
2717 goto done;
2719 error = got_object_open_as_commit(&header->commit, header->repo,
2720 header->id);
2721 if (error)
2722 goto done;
2724 error = gw_get_commit(gw_trans, header);
2725 if (limit > 1) {
2726 struct gw_header *n_header = NULL;
2727 if ((n_header = gw_init_header()) == NULL) {
2728 error = got_error_from_errno("malloc");
2729 goto done;
2732 n_header->refs_str = strdup(header->refs_str);
2733 n_header->commit_id = strdup(header->commit_id);
2734 n_header->parent_id = strdup(header->parent_id);
2735 n_header->tree_id = strdup(header->tree_id);
2736 n_header->author = strdup(header->author);
2737 n_header->committer = strdup(header->committer);
2738 n_header->commit_msg = strdup(header->commit_msg);
2739 n_header->committer_time = header->committer_time;
2740 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
2741 entry);
2743 if (error || (limit && --limit == 0))
2744 break;
2746 done:
2747 if (graph)
2748 got_commit_graph_close(graph);
2749 return error;
2752 static const struct got_error *
2753 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
2755 const struct got_error *error = NULL;
2756 struct got_reflist_entry *re;
2757 struct got_object_id *id2 = NULL;
2758 struct got_object_qid *parent_id;
2759 char *refs_str = NULL, *commit_msg = NULL, *commit_msg0;
2761 /*print commit*/
2762 SIMPLEQ_FOREACH(re, &header->refs, entry) {
2763 char *s;
2764 const char *name;
2765 struct got_tag_object *tag = NULL;
2766 int cmp;
2768 name = got_ref_get_name(re->ref);
2769 if (strcmp(name, GOT_REF_HEAD) == 0)
2770 continue;
2771 if (strncmp(name, "refs/", 5) == 0)
2772 name += 5;
2773 if (strncmp(name, "got/", 4) == 0)
2774 continue;
2775 if (strncmp(name, "heads/", 6) == 0)
2776 name += 6;
2777 if (strncmp(name, "remotes/", 8) == 0)
2778 name += 8;
2779 if (strncmp(name, "tags/", 5) == 0) {
2780 error = got_object_open_as_tag(&tag, header->repo,
2781 re->id);
2782 if (error) {
2783 if (error->code != GOT_ERR_OBJ_TYPE)
2784 continue;
2786 * Ref points at something other
2787 * than a tag.
2789 error = NULL;
2790 tag = NULL;
2793 cmp = got_object_id_cmp(tag ?
2794 got_object_tag_get_object_id(tag) : re->id, header->id);
2795 if (tag)
2796 got_object_tag_close(tag);
2797 if (cmp != 0)
2798 continue;
2799 s = refs_str;
2800 if (asprintf(&refs_str, "%s%s%s", s ? s : "",
2801 s ? ", " : "", name) == -1) {
2802 error = got_error_from_errno("asprintf");
2803 free(s);
2804 return error;
2806 header->refs_str = strdup(refs_str);
2807 free(s);
2810 if (refs_str == NULL)
2811 header->refs_str = strdup("");
2812 free(refs_str);
2814 error = got_object_id_str(&header->commit_id, header->id);
2815 if (error)
2816 return error;
2818 error = got_object_id_str(&header->tree_id,
2819 got_object_commit_get_tree_id(header->commit));
2820 if (error)
2821 return error;
2823 if (gw_trans->action == GW_DIFF) {
2824 parent_id = SIMPLEQ_FIRST(
2825 got_object_commit_get_parent_ids(header->commit));
2826 if (parent_id != NULL) {
2827 id2 = got_object_id_dup(parent_id->id);
2828 free (parent_id);
2829 error = got_object_id_str(&header->parent_id, id2);
2830 if (error)
2831 return error;
2832 free(id2);
2833 } else
2834 header->parent_id = strdup("/dev/null");
2835 } else
2836 header->parent_id = strdup("");
2838 header->committer_time =
2839 got_object_commit_get_committer_time(header->commit);
2841 header->author =
2842 got_object_commit_get_author(header->commit);
2843 header->committer =
2844 got_object_commit_get_committer(header->commit);
2845 if (error)
2846 return error;
2847 error = got_object_commit_get_logmsg(&commit_msg0, header->commit);
2848 if (error)
2849 return error;
2851 commit_msg = commit_msg0;
2852 while (*commit_msg == '\n')
2853 commit_msg++;
2855 header->commit_msg = strdup(commit_msg);
2856 free(commit_msg0);
2857 return error;
2860 static const struct got_error *
2861 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
2863 const struct got_error *error = NULL;
2864 char *in_repo_path = NULL;
2866 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
2867 if (error)
2868 return error;
2870 if (gw_trans->commit == NULL) {
2871 struct got_reference *head_ref;
2872 error = got_ref_open(&head_ref, header->repo,
2873 gw_trans->headref, 0);
2874 if (error)
2875 return error;
2877 error = got_ref_resolve(&header->id, header->repo, head_ref);
2878 got_ref_close(head_ref);
2879 if (error)
2880 return error;
2882 error = got_object_open_as_commit(&header->commit,
2883 header->repo, header->id);
2884 } else {
2885 struct got_reference *ref;
2886 error = got_ref_open(&ref, header->repo, gw_trans->commit, 0);
2887 if (error == NULL) {
2888 int obj_type;
2889 error = got_ref_resolve(&header->id, header->repo, ref);
2890 got_ref_close(ref);
2891 if (error)
2892 return error;
2893 error = got_object_get_type(&obj_type, header->repo,
2894 header->id);
2895 if (error)
2896 return error;
2897 if (obj_type == GOT_OBJ_TYPE_TAG) {
2898 struct got_tag_object *tag;
2899 error = got_object_open_as_tag(&tag,
2900 header->repo, header->id);
2901 if (error)
2902 return error;
2903 if (got_object_tag_get_object_type(tag) !=
2904 GOT_OBJ_TYPE_COMMIT) {
2905 got_object_tag_close(tag);
2906 error = got_error(GOT_ERR_OBJ_TYPE);
2907 return error;
2909 free(header->id);
2910 header->id = got_object_id_dup(
2911 got_object_tag_get_object_id(tag));
2912 if (header->id == NULL)
2913 error = got_error_from_errno(
2914 "got_object_id_dup");
2915 got_object_tag_close(tag);
2916 if (error)
2917 return error;
2918 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2919 error = got_error(GOT_ERR_OBJ_TYPE);
2920 return error;
2922 error = got_object_open_as_commit(&header->commit,
2923 header->repo, header->id);
2924 if (error)
2925 return error;
2927 if (header->commit == NULL) {
2928 error = got_repo_match_object_id_prefix(&header->id,
2929 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2930 header->repo);
2931 if (error)
2932 return error;
2934 error = got_repo_match_object_id_prefix(&header->id,
2935 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2936 header->repo);
2939 error = got_repo_map_path(&in_repo_path, header->repo,
2940 gw_trans->repo_path, 1);
2941 if (error)
2942 return error;
2944 if (in_repo_path) {
2945 header->path = strdup(in_repo_path);
2947 free(in_repo_path);
2949 error = got_ref_list(&header->refs, header->repo, NULL,
2950 got_ref_cmp_by_name, NULL);
2951 if (error)
2952 return error;
2954 error = gw_get_commits(gw_trans, header, limit);
2955 return error;
2958 struct blame_line {
2959 int annotated;
2960 char *id_str;
2961 char *committer;
2962 char datebuf[11]; /* YYYY-MM-DD + NUL */
2965 struct gw_blame_cb_args {
2966 struct blame_line *lines;
2967 int nlines;
2968 int nlines_prec;
2969 int lineno_cur;
2970 off_t *line_offsets;
2971 FILE *f;
2972 struct got_repository *repo;
2973 struct gw_trans *gw_trans;
2976 static const struct got_error *
2977 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2979 const struct got_error *err = NULL;
2980 struct gw_blame_cb_args *a = arg;
2981 struct blame_line *bline;
2982 char *line = NULL;
2983 size_t linesize = 0;
2984 struct got_commit_object *commit = NULL;
2985 off_t offset;
2986 struct tm tm;
2987 time_t committer_time;
2988 enum kcgi_err kerr = KCGI_OK;
2990 if (nlines != a->nlines ||
2991 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2992 return got_error(GOT_ERR_RANGE);
2994 if (lineno == -1)
2995 return NULL; /* no change in this commit */
2997 /* Annotate this line. */
2998 bline = &a->lines[lineno - 1];
2999 if (bline->annotated)
3000 return NULL;
3001 err = got_object_id_str(&bline->id_str, id);
3002 if (err)
3003 return err;
3005 err = got_object_open_as_commit(&commit, a->repo, id);
3006 if (err)
3007 goto done;
3009 bline->committer = strdup(got_object_commit_get_committer(commit));
3010 if (bline->committer == NULL) {
3011 err = got_error_from_errno("strdup");
3012 goto done;
3015 committer_time = got_object_commit_get_committer_time(commit);
3016 if (localtime_r(&committer_time, &tm) == NULL)
3017 return got_error_from_errno("localtime_r");
3018 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3019 &tm) >= sizeof(bline->datebuf)) {
3020 err = got_error(GOT_ERR_NO_SPACE);
3021 goto done;
3023 bline->annotated = 1;
3025 /* Print lines annotated so far. */
3026 bline = &a->lines[a->lineno_cur - 1];
3027 if (!bline->annotated)
3028 goto done;
3030 offset = a->line_offsets[a->lineno_cur - 1];
3031 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3032 err = got_error_from_errno("fseeko");
3033 goto done;
3036 while (bline->annotated) {
3037 char *smallerthan, *at, *nl, *committer;
3038 char *lineno = NULL, *href_diff = NULL, *href_link = NULL;
3039 size_t len;
3041 if (getline(&line, &linesize, a->f) == -1) {
3042 if (ferror(a->f))
3043 err = got_error_from_errno("getline");
3044 break;
3047 committer = bline->committer;
3048 smallerthan = strchr(committer, '<');
3049 if (smallerthan && smallerthan[1] != '\0')
3050 committer = smallerthan + 1;
3051 at = strchr(committer, '@');
3052 if (at)
3053 *at = '\0';
3054 len = strlen(committer);
3055 if (len >= 9)
3056 committer[8] = '\0';
3058 nl = strchr(line, '\n');
3059 if (nl)
3060 *nl = '\0';
3062 if (a->gw_trans->repo_folder == NULL)
3063 a->gw_trans->repo_folder = strdup("");
3064 if (a->gw_trans->repo_folder == NULL)
3065 goto err;
3067 /* blame line */
3068 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3069 "blame_wrapper", KATTR__MAX);
3070 if (kerr != KCGI_OK)
3071 goto err;
3072 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3073 "blame_number", KATTR__MAX);
3074 if (kerr != KCGI_OK)
3075 goto err;
3076 if (asprintf(&lineno, "%.*d", a->nlines_prec,
3077 a->lineno_cur) == -1)
3078 goto err;
3079 kerr = khtml_puts(a->gw_trans->gw_html_req, lineno);
3080 if (kerr != KCGI_OK)
3081 goto err;
3082 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3083 if (kerr != KCGI_OK)
3084 goto err;
3086 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3087 "blame_hash", KATTR__MAX);
3088 if (kerr != KCGI_OK)
3089 goto err;
3090 if (asprintf(&href_diff,
3091 "?path=%s&action=diff&commit=%s&file=%s&folder=%s",
3092 a->gw_trans->repo_name, bline->id_str,
3093 a->gw_trans->repo_file, a->gw_trans->repo_folder) == -1) {
3094 err = got_error_from_errno("asprintf");
3095 goto err;
3097 if (asprintf(&href_link, "%.8s", bline->id_str) == -1) {
3098 err = got_error_from_errno("asprintf");
3099 goto err;
3101 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3102 KATTR_HREF, href_diff, KATTR__MAX);
3103 if (kerr != KCGI_OK)
3104 goto done;
3105 kerr = khtml_puts(a->gw_trans->gw_html_req, href_link);
3106 if (kerr != KCGI_OK)
3107 goto err;
3108 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3109 if (kerr != KCGI_OK)
3110 goto err;
3112 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3113 "blame_date", KATTR__MAX);
3114 if (kerr != KCGI_OK)
3115 goto err;
3116 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3117 if (kerr != KCGI_OK)
3118 goto err;
3119 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3120 if (kerr != KCGI_OK)
3121 goto err;
3123 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3124 "blame_author", KATTR__MAX);
3125 if (kerr != KCGI_OK)
3126 goto err;
3127 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
3128 if (kerr != KCGI_OK)
3129 goto err;
3130 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3131 if (kerr != KCGI_OK)
3132 goto err;
3134 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3135 "blame_code", KATTR__MAX);
3136 if (kerr != KCGI_OK)
3137 goto err;
3138 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
3139 if (kerr != KCGI_OK)
3140 goto err;
3141 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3142 if (kerr != KCGI_OK)
3143 goto err;
3145 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3146 if (kerr != KCGI_OK)
3147 goto err;
3149 a->lineno_cur++;
3150 bline = &a->lines[a->lineno_cur - 1];
3151 err:
3152 free(lineno);
3153 free(href_diff);
3154 free(href_link);
3156 done:
3157 if (commit)
3158 got_object_commit_close(commit);
3159 free(line);
3160 if (err == NULL && kerr != KCGI_OK)
3161 err = gw_kcgi_error(kerr);
3162 return err;
3165 static const struct got_error *
3166 gw_output_file_blame(struct gw_trans *gw_trans)
3168 const struct got_error *error = NULL;
3169 struct got_repository *repo = NULL;
3170 struct got_object_id *obj_id = NULL;
3171 struct got_object_id *commit_id = NULL;
3172 struct got_blob_object *blob = NULL;
3173 char *path = NULL, *in_repo_path = NULL;
3174 struct gw_blame_cb_args bca;
3175 int i, obj_type;
3176 size_t filesize;
3178 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3179 if (error)
3180 return error;
3182 if (asprintf(&path, "%s%s%s",
3183 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3184 gw_trans->repo_folder ? "/" : "",
3185 gw_trans->repo_file) == -1) {
3186 error = got_error_from_errno("asprintf");
3187 goto done;
3190 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3191 if (error)
3192 goto done;
3194 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3195 GOT_OBJ_TYPE_COMMIT, 1, repo);
3196 if (error)
3197 goto done;
3199 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3200 if (error)
3201 goto done;
3203 if (obj_id == NULL) {
3204 error = got_error(GOT_ERR_NO_OBJ);
3205 goto done;
3208 error = got_object_get_type(&obj_type, repo, obj_id);
3209 if (error)
3210 goto done;
3212 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3213 error = got_error(GOT_ERR_OBJ_TYPE);
3214 goto done;
3217 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3218 if (error)
3219 goto done;
3221 bca.f = got_opentemp();
3222 if (bca.f == NULL) {
3223 error = got_error_from_errno("got_opentemp");
3224 goto done;
3226 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3227 &bca.line_offsets, bca.f, blob);
3228 if (error || bca.nlines == 0)
3229 goto done;
3231 /* Don't include \n at EOF in the blame line count. */
3232 if (bca.line_offsets[bca.nlines - 1] == filesize)
3233 bca.nlines--;
3235 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3236 if (bca.lines == NULL) {
3237 error = got_error_from_errno("calloc");
3238 goto done;
3240 bca.lineno_cur = 1;
3241 bca.nlines_prec = 0;
3242 i = bca.nlines;
3243 while (i > 0) {
3244 i /= 10;
3245 bca.nlines_prec++;
3247 bca.repo = repo;
3248 bca.gw_trans = gw_trans;
3250 error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
3251 NULL, NULL);
3252 if (error)
3253 goto done;
3254 done:
3255 free(bca.line_offsets);
3256 free(in_repo_path);
3257 free(commit_id);
3258 free(obj_id);
3259 free(path);
3261 for (i = 0; i < bca.nlines; i++) {
3262 struct blame_line *bline = &bca.lines[i];
3263 free(bline->id_str);
3264 free(bline->committer);
3266 free(bca.lines);
3267 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3268 error = got_error_from_errno("fclose");
3269 if (blob)
3270 got_object_blob_close(blob);
3271 if (repo)
3272 got_repo_close(repo);
3273 return error;
3276 static const struct got_error *
3277 gw_output_blob_buf(struct gw_trans *gw_trans)
3279 const struct got_error *error = NULL;
3280 struct got_repository *repo = NULL;
3281 struct got_object_id *obj_id = NULL;
3282 struct got_object_id *commit_id = NULL;
3283 struct got_blob_object *blob = NULL;
3284 char *path = NULL, *in_repo_path = NULL;
3285 int obj_type, set_mime = 0;
3286 size_t len, hdrlen;
3287 const uint8_t *buf;
3288 enum kcgi_err kerr = KCGI_OK;
3290 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3291 if (error)
3292 return error;
3294 if (asprintf(&path, "%s%s%s",
3295 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3296 gw_trans->repo_folder ? "/" : "",
3297 gw_trans->repo_file) == -1) {
3298 error = got_error_from_errno("asprintf");
3299 goto done;
3302 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3303 if (error)
3304 goto done;
3306 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3307 GOT_OBJ_TYPE_COMMIT, 1, repo);
3308 if (error)
3309 goto done;
3311 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3312 if (error)
3313 goto done;
3315 if (obj_id == NULL) {
3316 error = got_error(GOT_ERR_NO_OBJ);
3317 goto done;
3320 error = got_object_get_type(&obj_type, repo, obj_id);
3321 if (error)
3322 goto done;
3324 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3325 error = got_error(GOT_ERR_OBJ_TYPE);
3326 goto done;
3329 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3330 if (error)
3331 goto done;
3333 hdrlen = got_object_blob_get_hdrlen(blob);
3334 do {
3335 error = got_object_blob_read_block(&len, blob);
3336 if (error)
3337 goto done;
3338 buf = got_object_blob_get_read_buf(blob);
3341 * Skip blob object header first time around,
3342 * which also contains a zero byte.
3344 buf += hdrlen;
3345 if (set_mime == 0) {
3346 if (isbinary(buf, len - hdrlen))
3347 gw_trans->mime = KMIME_APP_OCTET_STREAM;
3348 else
3349 gw_trans->mime = KMIME_TEXT_PLAIN;
3350 set_mime = 1;
3351 error = gw_display_index(gw_trans);
3352 if (error)
3353 goto done;
3355 khttp_write(gw_trans->gw_req, buf, len - hdrlen);
3356 hdrlen = 0;
3357 } while (len != 0);
3358 done:
3359 free(in_repo_path);
3360 free(commit_id);
3361 free(obj_id);
3362 free(path);
3363 if (blob)
3364 got_object_blob_close(blob);
3365 if (repo)
3366 got_repo_close(repo);
3367 if (error == NULL && kerr != KCGI_OK)
3368 error = gw_kcgi_error(kerr);
3369 return error;
3372 static const struct got_error *
3373 gw_output_repo_tree(struct gw_trans *gw_trans)
3375 const struct got_error *error = NULL;
3376 struct got_repository *repo = NULL;
3377 struct got_object_id *tree_id = NULL, *commit_id = NULL;
3378 struct got_tree_object *tree = NULL;
3379 char *path = NULL, *in_repo_path = NULL;
3380 char *id_str = NULL;
3381 char *build_folder = NULL;
3382 char *href_blob = NULL, *href_blame = NULL;
3383 const char *class = NULL;
3384 int nentries, i, class_flip = 0;
3385 enum kcgi_err kerr = KCGI_OK;
3387 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3388 if (error)
3389 goto done;
3391 if (gw_trans->repo_folder != NULL)
3392 path = strdup(gw_trans->repo_folder);
3393 else {
3394 error = got_repo_map_path(&in_repo_path, repo,
3395 gw_trans->repo_path, 1);
3396 if (error)
3397 goto done;
3398 free(path);
3399 path = in_repo_path;
3402 if (gw_trans->commit == NULL) {
3403 struct got_reference *head_ref;
3404 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
3405 if (error)
3406 goto done;
3407 error = got_ref_resolve(&commit_id, repo, head_ref);
3408 if (error)
3409 goto done;
3410 got_ref_close(head_ref);
3412 } else {
3413 error = got_repo_match_object_id(&commit_id, NULL,
3414 gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
3415 if (error)
3416 goto done;
3419 error = got_object_id_str(&gw_trans->commit, commit_id);
3420 if (error)
3421 goto done;
3423 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
3424 if (error)
3425 goto done;
3427 error = got_object_open_as_tree(&tree, repo, tree_id);
3428 if (error)
3429 goto done;
3431 nentries = got_object_tree_get_nentries(tree);
3432 for (i = 0; i < nentries; i++) {
3433 struct got_tree_entry *te;
3434 const char *modestr = "";
3435 mode_t mode;
3437 te = got_object_tree_get_entry(tree, i);
3439 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
3440 if (error)
3441 goto done;
3443 mode = got_tree_entry_get_mode(te);
3444 if (got_object_tree_entry_is_submodule(te))
3445 modestr = "$";
3446 else if (S_ISLNK(mode))
3447 modestr = "@";
3448 else if (S_ISDIR(mode))
3449 modestr = "/";
3450 else if (mode & S_IXUSR)
3451 modestr = "*";
3453 if (class_flip == 0) {
3454 class = "back_lightgray";
3455 class_flip = 1;
3456 } else {
3457 class = "back_white";
3458 class_flip = 0;
3461 if (S_ISDIR(mode)) {
3462 if (asprintf(&build_folder, "%s/%s",
3463 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3464 got_tree_entry_get_name(te)) == -1) {
3465 error = got_error_from_errno(
3466 "asprintf");
3467 goto done;
3469 if (asprintf(&href_blob,
3470 "?path=%s&action=%s&commit=%s&folder=%s",
3471 gw_trans->repo_name, gw_trans->action_name,
3472 gw_trans->commit, build_folder) == -1) {
3473 error = got_error_from_errno("asprintf");
3474 goto done;
3477 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3478 KATTR_ID, "tree_wrapper", KATTR__MAX);
3479 if (kerr != KCGI_OK)
3480 goto done;
3481 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3482 KATTR_ID, "tree_line", KATTR_CLASS, class,
3483 KATTR__MAX);
3484 if (kerr != KCGI_OK)
3485 goto done;
3486 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3487 KATTR_HREF, href_blob, KATTR_CLASS,
3488 "diff_directory", KATTR__MAX);
3489 if (kerr != KCGI_OK)
3490 goto done;
3491 kerr = khtml_puts(gw_trans->gw_html_req,
3492 got_tree_entry_get_name(te));
3493 if (kerr != KCGI_OK)
3494 goto done;
3495 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3496 if (kerr != KCGI_OK)
3497 goto done;
3498 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3499 if (kerr != KCGI_OK)
3500 goto done;
3501 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3502 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
3503 KATTR__MAX);
3504 if (kerr != KCGI_OK)
3505 goto done;
3506 kerr = khtml_entity(gw_trans->gw_html_req,
3507 KENTITY_nbsp);
3508 if (kerr != KCGI_OK)
3509 goto done;
3510 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3511 if (kerr != KCGI_OK)
3512 goto done;
3513 } else {
3514 if (asprintf(&href_blob,
3515 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3516 gw_trans->repo_name, "blob", gw_trans->commit,
3517 got_tree_entry_get_name(te),
3518 gw_trans->repo_folder ?
3519 gw_trans->repo_folder : "") == -1) {
3520 error = got_error_from_errno("asprintf");
3521 goto done;
3523 if (asprintf(&href_blame,
3524 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3525 gw_trans->repo_name, "blame", gw_trans->commit,
3526 got_tree_entry_get_name(te),
3527 gw_trans->repo_folder ?
3528 gw_trans->repo_folder : "") == -1) {
3529 error = got_error_from_errno("asprintf");
3530 goto done;
3533 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3534 KATTR_ID, "tree_wrapper", KATTR__MAX);
3535 if (kerr != KCGI_OK)
3536 goto done;
3537 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3538 KATTR_ID, "tree_line", KATTR_CLASS, class,
3539 KATTR__MAX);
3540 if (kerr != KCGI_OK)
3541 goto done;
3542 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3543 KATTR_HREF, href_blob, KATTR__MAX);
3544 if (kerr != KCGI_OK)
3545 goto done;
3546 kerr = khtml_puts(gw_trans->gw_html_req,
3547 got_tree_entry_get_name(te));
3548 if (kerr != KCGI_OK)
3549 goto done;
3550 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3551 if (kerr != KCGI_OK)
3552 goto done;
3553 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3554 if (kerr != KCGI_OK)
3555 goto done;
3556 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3557 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
3558 KATTR__MAX);
3559 if (kerr != KCGI_OK)
3560 goto done;
3562 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3563 KATTR_HREF, href_blob, KATTR__MAX);
3564 if (kerr != KCGI_OK)
3565 goto done;
3566 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
3567 if (kerr != KCGI_OK)
3568 goto done;
3569 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3570 if (kerr != KCGI_OK)
3571 goto done;
3573 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3574 if (kerr != KCGI_OK)
3575 goto done;
3577 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3578 KATTR_HREF, href_blame, KATTR__MAX);
3579 if (kerr != KCGI_OK)
3580 goto done;
3581 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
3582 if (kerr != KCGI_OK)
3583 goto done;
3585 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3586 if (kerr != KCGI_OK)
3587 goto done;
3589 free(id_str);
3590 id_str = NULL;
3591 free(href_blob);
3592 href_blob = NULL;
3593 free(build_folder);
3594 build_folder = NULL;
3596 done:
3597 if (tree)
3598 got_object_tree_close(tree);
3599 if (repo)
3600 got_repo_close(repo);
3601 free(id_str);
3602 free(href_blob);
3603 free(href_blame);
3604 free(in_repo_path);
3605 free(tree_id);
3606 free(build_folder);
3607 if (error == NULL && kerr != KCGI_OK)
3608 error = gw_kcgi_error(kerr);
3609 return error;
3612 static const struct got_error *
3613 gw_get_repo_heads(char **head_html, struct gw_trans *gw_trans)
3615 const struct got_error *error = NULL;
3616 struct got_repository *repo = NULL;
3617 struct got_reflist_head refs;
3618 struct got_reflist_entry *re;
3619 char *head_row = NULL, *head_navs_disp = NULL, *age = NULL;
3620 struct buf *diffbuf = NULL;
3621 size_t newsize;
3623 *head_html = NULL;
3625 SIMPLEQ_INIT(&refs);
3627 error = buf_alloc(&diffbuf, 0);
3628 if (error)
3629 return NULL;
3631 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3632 if (error)
3633 goto done;
3635 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
3636 NULL);
3637 if (error)
3638 goto done;
3640 SIMPLEQ_FOREACH(re, &refs, entry) {
3641 char *refname;
3643 refname = strdup(got_ref_get_name(re->ref));
3644 if (refname == NULL) {
3645 error = got_error_from_errno("got_ref_to_str");
3646 goto done;
3649 if (strncmp(refname, "refs/heads/", 11) != 0) {
3650 free(refname);
3651 continue;
3654 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
3655 refname, TM_DIFF);
3656 if (error)
3657 goto done;
3659 if (asprintf(&head_navs_disp, heads_navs, gw_trans->repo_name,
3660 refname, gw_trans->repo_name, refname,
3661 gw_trans->repo_name, refname, gw_trans->repo_name,
3662 refname) == -1) {
3663 error = got_error_from_errno("asprintf");
3664 goto done;
3667 if (strncmp(refname, "refs/heads/", 11) == 0)
3668 refname += 11;
3670 if (asprintf(&head_row, heads_row, age, refname,
3671 head_navs_disp) == -1) {
3672 error = got_error_from_errno("asprintf");
3673 goto done;
3676 error = buf_puts(&newsize, diffbuf, head_row);
3678 free(head_navs_disp);
3679 free(head_row);
3682 if (buf_len(diffbuf) > 0) {
3683 error = buf_putc(diffbuf, '\0');
3684 *head_html = strdup(buf_get(diffbuf));
3685 if (*head_html == NULL)
3686 error = got_error_from_errno("strdup");
3688 done:
3689 buf_free(diffbuf);
3690 got_ref_list_free(&refs);
3691 if (repo)
3692 got_repo_close(repo);
3693 return error;
3696 static char *
3697 gw_get_site_link(struct gw_trans *gw_trans)
3699 char *link = NULL, *repo = NULL, *action = NULL;
3701 if (gw_trans->repo_name != NULL &&
3702 asprintf(&repo, " / <a href='?path=%s&action=summary'>%s</a>",
3703 gw_trans->repo_name, gw_trans->repo_name) == -1)
3704 return NULL;
3706 if (gw_trans->action_name != NULL &&
3707 asprintf(&action, " / %s", gw_trans->action_name) == -1) {
3708 free(repo);
3709 return NULL;
3712 if (asprintf(&link, site_link, GOTWEB,
3713 gw_trans->gw_conf->got_site_link,
3714 repo ? repo : "", action ? action : "") == -1) {
3715 free(repo);
3716 free(action);
3717 return NULL;
3720 free(repo);
3721 free(action);
3722 return link;
3725 static const struct got_error *
3726 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
3728 const struct got_error *error = NULL;
3729 char *color = NULL;
3730 enum kcgi_err kerr = KCGI_OK;
3732 if (strncmp(buf, "-", 1) == 0)
3733 color = "diff_minus";
3734 else if (strncmp(buf, "+", 1) == 0)
3735 color = "diff_plus";
3736 else if (strncmp(buf, "@@", 2) == 0)
3737 color = "diff_chunk_header";
3738 else if (strncmp(buf, "@@", 2) == 0)
3739 color = "diff_chunk_header";
3740 else if (strncmp(buf, "commit +", 8) == 0)
3741 color = "diff_meta";
3742 else if (strncmp(buf, "commit -", 8) == 0)
3743 color = "diff_meta";
3744 else if (strncmp(buf, "blob +", 6) == 0)
3745 color = "diff_meta";
3746 else if (strncmp(buf, "blob -", 6) == 0)
3747 color = "diff_meta";
3748 else if (strncmp(buf, "file +", 6) == 0)
3749 color = "diff_meta";
3750 else if (strncmp(buf, "file -", 6) == 0)
3751 color = "diff_meta";
3752 else if (strncmp(buf, "from:", 5) == 0)
3753 color = "diff_author";
3754 else if (strncmp(buf, "via:", 4) == 0)
3755 color = "diff_author";
3756 else if (strncmp(buf, "date:", 5) == 0)
3757 color = "diff_date";
3758 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3759 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
3760 if (error == NULL && kerr != KCGI_OK)
3761 error = gw_kcgi_error(kerr);
3762 return error;
3765 int
3766 main(int argc, char *argv[])
3768 const struct got_error *error = NULL;
3769 struct gw_trans *gw_trans;
3770 struct gw_dir *dir = NULL, *tdir;
3771 const char *page = "index";
3772 int gw_malloc = 1;
3773 enum kcgi_err kerr;
3775 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
3776 errx(1, "malloc");
3778 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
3779 errx(1, "malloc");
3781 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
3782 errx(1, "malloc");
3784 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
3785 errx(1, "malloc");
3787 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
3788 if (kerr != KCGI_OK) {
3789 error = gw_kcgi_error(kerr);
3790 goto done;
3793 if ((gw_trans->gw_conf =
3794 malloc(sizeof(struct gotweb_conf))) == NULL) {
3795 gw_malloc = 0;
3796 error = got_error_from_errno("malloc");
3797 goto done;
3800 TAILQ_INIT(&gw_trans->gw_dirs);
3801 TAILQ_INIT(&gw_trans->gw_headers);
3803 gw_trans->page = 0;
3804 gw_trans->repos_total = 0;
3805 gw_trans->repo_path = NULL;
3806 gw_trans->commit = NULL;
3807 gw_trans->headref = strdup(GOT_REF_HEAD);
3808 gw_trans->mime = KMIME_TEXT_HTML;
3809 gw_trans->gw_tmpl->key = gw_templs;
3810 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
3811 gw_trans->gw_tmpl->arg = gw_trans;
3812 gw_trans->gw_tmpl->cb = gw_template;
3813 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
3814 if (error)
3815 goto done;
3817 error = gw_parse_querystring(gw_trans);
3818 if (error)
3819 goto done;
3821 if (gw_trans->action == GW_BLOB)
3822 error = gw_blob(gw_trans);
3823 else
3824 error = gw_display_index(gw_trans);
3825 done:
3826 if (error) {
3827 gw_trans->mime = KMIME_TEXT_PLAIN;
3828 gw_trans->action = GW_ERR;
3829 gw_display_error(gw_trans, error);
3831 if (gw_malloc) {
3832 free(gw_trans->gw_conf->got_repos_path);
3833 free(gw_trans->gw_conf->got_site_name);
3834 free(gw_trans->gw_conf->got_site_owner);
3835 free(gw_trans->gw_conf->got_site_link);
3836 free(gw_trans->gw_conf->got_logo);
3837 free(gw_trans->gw_conf->got_logo_url);
3838 free(gw_trans->gw_conf);
3839 free(gw_trans->commit);
3840 free(gw_trans->repo_path);
3841 free(gw_trans->repo_name);
3842 free(gw_trans->repo_file);
3843 free(gw_trans->action_name);
3844 free(gw_trans->headref);
3846 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
3847 free(dir->name);
3848 free(dir->description);
3849 free(dir->age);
3850 free(dir->url);
3851 free(dir->path);
3852 free(dir);
3857 khttp_free(gw_trans->gw_req);
3858 return 0;