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, *refstr = 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;
2345 refname = got_ref_get_name(re->ref);
2346 if (strncmp(refname, "refs/tags/", 10) != 0)
2347 continue;
2348 refname += 10;
2349 refstr = got_ref_to_str(re->ref);
2350 if (refstr == NULL) {
2351 error = got_error_from_errno("got_ref_to_str");
2352 goto done;
2355 error = got_ref_resolve(&id, repo, re->ref);
2356 if (error)
2357 goto done;
2360 * XXX: some of my repos are failing here. need to investigate.
2361 * currently setting error to NULL so no error is returned,
2362 * which stops Heads from being displayed on gw_summary.
2364 * got ref -l lists refs and first tag ref above can be
2365 * displayed
2367 * got tag -l will list tags just fine, so I don't know what
2368 * is happening.
2370 error = got_object_open_as_tag(&tag, repo, id);
2371 free(id);
2372 if (error) {
2373 error = NULL;
2374 goto done;
2377 tagger = got_object_tag_get_tagger(tag);
2378 tagger_time = got_object_tag_get_tagger_time(tag);
2380 error = got_object_id_str(&id_str,
2381 got_object_tag_get_object_id(tag));
2382 if (error)
2383 goto done;
2385 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
2386 strlen(id_str)) != 0)
2387 continue;
2389 tag_commit0 = strdup(got_object_tag_get_message(tag));
2390 if (tag_commit0 == NULL) {
2391 error = got_error_from_errno("strdup");
2392 goto done;
2395 tag_commit = tag_commit0;
2396 while (*tag_commit == '\n')
2397 tag_commit++;
2399 switch (tag_type) {
2400 case TAGBRIEF:
2401 newline = strchr(tag_commit, '\n');
2402 if (newline)
2403 *newline = '\0';
2405 if (summary_header_displayed == 0) {
2406 kerr = khtml_attr(gw_trans->gw_html_req,
2407 KELEM_DIV, KATTR_ID,
2408 "summary_tags_title_wrapper", KATTR__MAX);
2409 if (kerr != KCGI_OK)
2410 goto done;
2411 kerr = khtml_attr(gw_trans->gw_html_req,
2412 KELEM_DIV, KATTR_ID,
2413 "summary_tags_title", KATTR__MAX);
2414 if (kerr != KCGI_OK)
2415 goto done;
2416 kerr = khtml_puts(gw_trans->gw_html_req,
2417 "Tags");
2418 if (kerr != KCGI_OK)
2419 goto done;
2420 kerr = khtml_closeelem(gw_trans->gw_html_req,
2421 2);
2422 if (kerr != KCGI_OK)
2423 goto done;
2424 kerr = khtml_attr(gw_trans->gw_html_req,
2425 KELEM_DIV, KATTR_ID,
2426 "summary_tags_content", KATTR__MAX);
2427 if (kerr != KCGI_OK)
2428 goto done;
2429 summary_header_displayed = 1;
2432 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2433 KATTR_ID, "tags_wrapper", KATTR__MAX);
2434 if (kerr != KCGI_OK)
2435 goto done;
2436 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2437 KATTR_ID, "tags_age", KATTR__MAX);
2438 if (kerr != KCGI_OK)
2439 goto done;
2440 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
2441 if (error)
2442 goto done;
2443 kerr = khtml_puts(gw_trans->gw_html_req,
2444 age ? age : "");
2445 if (kerr != KCGI_OK)
2446 goto done;
2447 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2448 if (kerr != KCGI_OK)
2449 goto done;
2450 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2451 KATTR_ID, "tags", KATTR__MAX);
2452 if (kerr != KCGI_OK)
2453 goto done;
2454 kerr = khtml_puts(gw_trans->gw_html_req, refname);
2455 if (kerr != KCGI_OK)
2456 goto done;
2457 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2458 if (kerr != KCGI_OK)
2459 goto done;
2460 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2461 KATTR_ID, "tags_name", KATTR__MAX);
2462 if (kerr != KCGI_OK)
2463 goto done;
2464 if (asprintf(&href_tag, "?path=%s&action=tag&commit=%s",
2465 gw_trans->repo_name, id_str) == -1) {
2466 error = got_error_from_errno("asprintf");
2467 goto done;
2469 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2470 KATTR_HREF, href_tag, KATTR__MAX);
2471 if (kerr != KCGI_OK)
2472 goto done;
2473 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
2474 if (kerr != KCGI_OK)
2475 goto done;
2476 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2477 if (kerr != KCGI_OK)
2478 goto done;
2480 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2481 KATTR_ID, "navs_wrapper", KATTR__MAX);
2482 if (kerr != KCGI_OK)
2483 goto done;
2484 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2485 KATTR_ID, "navs", KATTR__MAX);
2486 if (kerr != KCGI_OK)
2487 goto done;
2489 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2490 KATTR_HREF, href_tag, KATTR__MAX);
2491 if (kerr != KCGI_OK)
2492 goto done;
2493 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
2494 if (kerr != KCGI_OK)
2495 goto done;
2496 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2497 if (kerr != KCGI_OK)
2498 goto done;
2500 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2501 if (kerr != KCGI_OK)
2502 goto done;
2504 if (asprintf(&href_briefs,
2505 "?path=%s&action=briefs&commit=%s",
2506 gw_trans->repo_name, id_str) == -1) {
2507 error = got_error_from_errno("asprintf");
2508 goto done;
2510 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2511 KATTR_HREF, href_briefs, KATTR__MAX);
2512 if (kerr != KCGI_OK)
2513 goto done;
2514 kerr = khtml_puts(gw_trans->gw_html_req,
2515 "commit briefs");
2516 if (kerr != KCGI_OK)
2517 goto done;
2518 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2519 if (kerr != KCGI_OK)
2520 goto done;
2522 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2523 if (kerr != KCGI_OK)
2524 goto done;
2526 if (asprintf(&href_commits,
2527 "?path=%s&action=commits&commit=%s",
2528 gw_trans->repo_name, id_str) == -1) {
2529 error = got_error_from_errno("asprintf");
2530 goto done;
2532 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2533 KATTR_HREF, href_commits, KATTR__MAX);
2534 if (kerr != KCGI_OK)
2535 goto done;
2536 kerr = khtml_puts(gw_trans->gw_html_req,
2537 "commits");
2538 if (kerr != KCGI_OK)
2539 goto done;
2540 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2541 if (kerr != KCGI_OK)
2542 goto done;
2544 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2545 KATTR_ID, "dotted_line", KATTR__MAX);
2546 if (kerr != KCGI_OK)
2547 goto done;
2548 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2549 if (kerr != KCGI_OK)
2550 goto done;
2551 break;
2552 case TAGFULL:
2553 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2554 KATTR_ID, "tag_info_date_title", KATTR__MAX);
2555 if (kerr != KCGI_OK)
2556 goto done;
2557 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
2558 if (kerr != KCGI_OK)
2559 goto done;
2560 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2561 if (kerr != KCGI_OK)
2562 goto done;
2563 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2564 KATTR_ID, "tag_info_date", KATTR__MAX);
2565 if (kerr != KCGI_OK)
2566 goto done;
2567 error = gw_get_time_str(&age, tagger_time, TM_LONG);
2568 if (error)
2569 goto done;
2570 kerr = khtml_puts(gw_trans->gw_html_req,
2571 age ? age : "");
2572 if (kerr != KCGI_OK)
2573 goto done;
2574 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2575 if (kerr != KCGI_OK)
2576 goto done;
2578 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2579 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
2580 if (kerr != KCGI_OK)
2581 goto done;
2582 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
2583 if (kerr != KCGI_OK)
2584 goto done;
2585 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2586 if (kerr != KCGI_OK)
2587 goto done;
2588 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2589 KATTR_ID, "tag_info_date", KATTR__MAX);
2590 if (kerr != KCGI_OK)
2591 goto done;
2592 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
2593 if (kerr != KCGI_OK)
2594 goto done;
2595 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2596 if (kerr != KCGI_OK)
2597 goto done;
2599 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2600 KATTR_ID, "tag_info", KATTR__MAX);
2601 if (kerr != KCGI_OK)
2602 goto done;
2603 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
2604 if (kerr != KCGI_OK)
2605 goto done;
2606 break;
2607 default:
2608 break;
2610 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2611 if (kerr != KCGI_OK)
2612 goto done;
2614 if (limit && --limit == 0)
2615 break;
2617 got_object_tag_close(tag);
2618 tag = NULL;
2619 free(id_str);
2620 id_str = NULL;
2621 free(refstr);
2622 refstr = NULL;
2623 free(age);
2624 age = NULL;
2625 free(tag_commit0);
2626 tag_commit0 = NULL;
2627 free(href_tag);
2628 href_tag = NULL;
2629 free(href_briefs);
2630 href_briefs = NULL;
2631 free(href_commits);
2632 href_commits = NULL;
2634 done:
2635 if (tag)
2636 got_object_tag_close(tag);
2637 free(id_str);
2638 free(refstr);
2639 free(age);
2640 free(tag_commit0);
2641 free(href_tag);
2642 free(href_briefs);
2643 free(href_commits);
2644 got_ref_list_free(&refs);
2645 if (repo)
2646 got_repo_close(repo);
2647 if (error == NULL && kerr != KCGI_OK)
2648 error = gw_kcgi_error(kerr);
2649 return error;
2652 static void
2653 gw_free_headers(struct gw_header *header)
2655 free(header->id);
2656 free(header->path);
2657 if (header->commit != NULL)
2658 got_object_commit_close(header->commit);
2659 if (header->repo)
2660 got_repo_close(header->repo);
2661 free(header->refs_str);
2662 free(header->commit_id);
2663 free(header->parent_id);
2664 free(header->tree_id);
2665 free(header->commit_msg);
2668 static struct gw_header *
2669 gw_init_header()
2671 struct gw_header *header;
2673 header = malloc(sizeof(*header));
2674 if (header == NULL)
2675 return NULL;
2677 header->repo = NULL;
2678 header->commit = NULL;
2679 header->id = NULL;
2680 header->path = NULL;
2681 SIMPLEQ_INIT(&header->refs);
2683 return header;
2686 static const struct got_error *
2687 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
2688 int limit)
2690 const struct got_error *error = NULL;
2691 struct got_commit_graph *graph = NULL;
2693 error = got_commit_graph_open(&graph, header->path, 0);
2694 if (error)
2695 goto done;
2697 error = got_commit_graph_iter_start(graph, header->id, header->repo,
2698 NULL, NULL);
2699 if (error)
2700 goto done;
2702 for (;;) {
2703 error = got_commit_graph_iter_next(&header->id, graph,
2704 header->repo, NULL, NULL);
2705 if (error) {
2706 if (error->code == GOT_ERR_ITER_COMPLETED)
2707 error = NULL;
2708 goto done;
2710 if (header->id == NULL)
2711 goto done;
2713 error = got_object_open_as_commit(&header->commit, header->repo,
2714 header->id);
2715 if (error)
2716 goto done;
2718 error = gw_get_commit(gw_trans, header);
2719 if (limit > 1) {
2720 struct gw_header *n_header = NULL;
2721 if ((n_header = gw_init_header()) == NULL) {
2722 error = got_error_from_errno("malloc");
2723 goto done;
2726 n_header->refs_str = strdup(header->refs_str);
2727 n_header->commit_id = strdup(header->commit_id);
2728 n_header->parent_id = strdup(header->parent_id);
2729 n_header->tree_id = strdup(header->tree_id);
2730 n_header->author = strdup(header->author);
2731 n_header->committer = strdup(header->committer);
2732 n_header->commit_msg = strdup(header->commit_msg);
2733 n_header->committer_time = header->committer_time;
2734 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
2735 entry);
2737 if (error || (limit && --limit == 0))
2738 break;
2740 done:
2741 if (graph)
2742 got_commit_graph_close(graph);
2743 return error;
2746 static const struct got_error *
2747 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
2749 const struct got_error *error = NULL;
2750 struct got_reflist_entry *re;
2751 struct got_object_id *id2 = NULL;
2752 struct got_object_qid *parent_id;
2753 char *refs_str = NULL, *commit_msg = NULL, *commit_msg0;
2755 /*print commit*/
2756 SIMPLEQ_FOREACH(re, &header->refs, entry) {
2757 char *s;
2758 const char *name;
2759 struct got_tag_object *tag = NULL;
2760 int cmp;
2762 name = got_ref_get_name(re->ref);
2763 if (strcmp(name, GOT_REF_HEAD) == 0)
2764 continue;
2765 if (strncmp(name, "refs/", 5) == 0)
2766 name += 5;
2767 if (strncmp(name, "got/", 4) == 0)
2768 continue;
2769 if (strncmp(name, "heads/", 6) == 0)
2770 name += 6;
2771 if (strncmp(name, "remotes/", 8) == 0)
2772 name += 8;
2773 if (strncmp(name, "tags/", 5) == 0) {
2774 error = got_object_open_as_tag(&tag, header->repo,
2775 re->id);
2776 if (error) {
2777 if (error->code != GOT_ERR_OBJ_TYPE)
2778 continue;
2780 * Ref points at something other
2781 * than a tag.
2783 error = NULL;
2784 tag = NULL;
2787 cmp = got_object_id_cmp(tag ?
2788 got_object_tag_get_object_id(tag) : re->id, header->id);
2789 if (tag)
2790 got_object_tag_close(tag);
2791 if (cmp != 0)
2792 continue;
2793 s = refs_str;
2794 if (asprintf(&refs_str, "%s%s%s", s ? s : "",
2795 s ? ", " : "", name) == -1) {
2796 error = got_error_from_errno("asprintf");
2797 free(s);
2798 return error;
2800 header->refs_str = strdup(refs_str);
2801 free(s);
2804 if (refs_str == NULL)
2805 header->refs_str = strdup("");
2806 free(refs_str);
2808 error = got_object_id_str(&header->commit_id, header->id);
2809 if (error)
2810 return error;
2812 error = got_object_id_str(&header->tree_id,
2813 got_object_commit_get_tree_id(header->commit));
2814 if (error)
2815 return error;
2817 if (gw_trans->action == GW_DIFF) {
2818 parent_id = SIMPLEQ_FIRST(
2819 got_object_commit_get_parent_ids(header->commit));
2820 if (parent_id != NULL) {
2821 id2 = got_object_id_dup(parent_id->id);
2822 free (parent_id);
2823 error = got_object_id_str(&header->parent_id, id2);
2824 if (error)
2825 return error;
2826 free(id2);
2827 } else
2828 header->parent_id = strdup("/dev/null");
2829 } else
2830 header->parent_id = strdup("");
2832 header->committer_time =
2833 got_object_commit_get_committer_time(header->commit);
2835 header->author =
2836 got_object_commit_get_author(header->commit);
2837 header->committer =
2838 got_object_commit_get_committer(header->commit);
2839 if (error)
2840 return error;
2841 error = got_object_commit_get_logmsg(&commit_msg0, header->commit);
2842 if (error)
2843 return error;
2845 commit_msg = commit_msg0;
2846 while (*commit_msg == '\n')
2847 commit_msg++;
2849 header->commit_msg = strdup(commit_msg);
2850 free(commit_msg0);
2851 return error;
2854 static const struct got_error *
2855 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
2857 const struct got_error *error = NULL;
2858 char *in_repo_path = NULL;
2860 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
2861 if (error)
2862 return error;
2864 if (gw_trans->commit == NULL) {
2865 struct got_reference *head_ref;
2866 error = got_ref_open(&head_ref, header->repo,
2867 gw_trans->headref, 0);
2868 if (error)
2869 return error;
2871 error = got_ref_resolve(&header->id, header->repo, head_ref);
2872 got_ref_close(head_ref);
2873 if (error)
2874 return error;
2876 error = got_object_open_as_commit(&header->commit,
2877 header->repo, header->id);
2878 } else {
2879 struct got_reference *ref;
2880 error = got_ref_open(&ref, header->repo, gw_trans->commit, 0);
2881 if (error == NULL) {
2882 int obj_type;
2883 error = got_ref_resolve(&header->id, header->repo, ref);
2884 got_ref_close(ref);
2885 if (error)
2886 return error;
2887 error = got_object_get_type(&obj_type, header->repo,
2888 header->id);
2889 if (error)
2890 return error;
2891 if (obj_type == GOT_OBJ_TYPE_TAG) {
2892 struct got_tag_object *tag;
2893 error = got_object_open_as_tag(&tag,
2894 header->repo, header->id);
2895 if (error)
2896 return error;
2897 if (got_object_tag_get_object_type(tag) !=
2898 GOT_OBJ_TYPE_COMMIT) {
2899 got_object_tag_close(tag);
2900 error = got_error(GOT_ERR_OBJ_TYPE);
2901 return error;
2903 free(header->id);
2904 header->id = got_object_id_dup(
2905 got_object_tag_get_object_id(tag));
2906 if (header->id == NULL)
2907 error = got_error_from_errno(
2908 "got_object_id_dup");
2909 got_object_tag_close(tag);
2910 if (error)
2911 return error;
2912 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2913 error = got_error(GOT_ERR_OBJ_TYPE);
2914 return error;
2916 error = got_object_open_as_commit(&header->commit,
2917 header->repo, header->id);
2918 if (error)
2919 return error;
2921 if (header->commit == NULL) {
2922 error = got_repo_match_object_id_prefix(&header->id,
2923 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2924 header->repo);
2925 if (error)
2926 return error;
2928 error = got_repo_match_object_id_prefix(&header->id,
2929 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2930 header->repo);
2933 error = got_repo_map_path(&in_repo_path, header->repo,
2934 gw_trans->repo_path, 1);
2935 if (error)
2936 return error;
2938 if (in_repo_path) {
2939 header->path = strdup(in_repo_path);
2941 free(in_repo_path);
2943 error = got_ref_list(&header->refs, header->repo, NULL,
2944 got_ref_cmp_by_name, NULL);
2945 if (error)
2946 return error;
2948 error = gw_get_commits(gw_trans, header, limit);
2949 return error;
2952 struct blame_line {
2953 int annotated;
2954 char *id_str;
2955 char *committer;
2956 char datebuf[11]; /* YYYY-MM-DD + NUL */
2959 struct gw_blame_cb_args {
2960 struct blame_line *lines;
2961 int nlines;
2962 int nlines_prec;
2963 int lineno_cur;
2964 off_t *line_offsets;
2965 FILE *f;
2966 struct got_repository *repo;
2967 struct gw_trans *gw_trans;
2970 static const struct got_error *
2971 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2973 const struct got_error *err = NULL;
2974 struct gw_blame_cb_args *a = arg;
2975 struct blame_line *bline;
2976 char *line = NULL;
2977 size_t linesize = 0;
2978 struct got_commit_object *commit = NULL;
2979 off_t offset;
2980 struct tm tm;
2981 time_t committer_time;
2982 enum kcgi_err kerr = KCGI_OK;
2984 if (nlines != a->nlines ||
2985 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2986 return got_error(GOT_ERR_RANGE);
2988 if (lineno == -1)
2989 return NULL; /* no change in this commit */
2991 /* Annotate this line. */
2992 bline = &a->lines[lineno - 1];
2993 if (bline->annotated)
2994 return NULL;
2995 err = got_object_id_str(&bline->id_str, id);
2996 if (err)
2997 return err;
2999 err = got_object_open_as_commit(&commit, a->repo, id);
3000 if (err)
3001 goto done;
3003 bline->committer = strdup(got_object_commit_get_committer(commit));
3004 if (bline->committer == NULL) {
3005 err = got_error_from_errno("strdup");
3006 goto done;
3009 committer_time = got_object_commit_get_committer_time(commit);
3010 if (localtime_r(&committer_time, &tm) == NULL)
3011 return got_error_from_errno("localtime_r");
3012 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3013 &tm) >= sizeof(bline->datebuf)) {
3014 err = got_error(GOT_ERR_NO_SPACE);
3015 goto done;
3017 bline->annotated = 1;
3019 /* Print lines annotated so far. */
3020 bline = &a->lines[a->lineno_cur - 1];
3021 if (!bline->annotated)
3022 goto done;
3024 offset = a->line_offsets[a->lineno_cur - 1];
3025 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3026 err = got_error_from_errno("fseeko");
3027 goto done;
3030 while (bline->annotated) {
3031 char *smallerthan, *at, *nl, *committer;
3032 char *lineno = NULL, *href_diff = NULL, *href_link = NULL;
3033 size_t len;
3035 if (getline(&line, &linesize, a->f) == -1) {
3036 if (ferror(a->f))
3037 err = got_error_from_errno("getline");
3038 break;
3041 committer = bline->committer;
3042 smallerthan = strchr(committer, '<');
3043 if (smallerthan && smallerthan[1] != '\0')
3044 committer = smallerthan + 1;
3045 at = strchr(committer, '@');
3046 if (at)
3047 *at = '\0';
3048 len = strlen(committer);
3049 if (len >= 9)
3050 committer[8] = '\0';
3052 nl = strchr(line, '\n');
3053 if (nl)
3054 *nl = '\0';
3056 if (a->gw_trans->repo_folder == NULL)
3057 a->gw_trans->repo_folder = strdup("");
3058 if (a->gw_trans->repo_folder == NULL)
3059 goto err;
3061 /* blame line */
3062 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3063 "blame_wrapper", KATTR__MAX);
3064 if (kerr != KCGI_OK)
3065 goto err;
3066 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3067 "blame_number", KATTR__MAX);
3068 if (kerr != KCGI_OK)
3069 goto err;
3070 if (asprintf(&lineno, "%.*d", a->nlines_prec,
3071 a->lineno_cur) == -1)
3072 goto err;
3073 kerr = khtml_puts(a->gw_trans->gw_html_req, lineno);
3074 if (kerr != KCGI_OK)
3075 goto err;
3076 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3077 if (kerr != KCGI_OK)
3078 goto err;
3080 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3081 "blame_hash", KATTR__MAX);
3082 if (kerr != KCGI_OK)
3083 goto err;
3084 if (asprintf(&href_diff,
3085 "?path=%s&action=diff&commit=%s&file=%s&folder=%s",
3086 a->gw_trans->repo_name, bline->id_str,
3087 a->gw_trans->repo_file, a->gw_trans->repo_folder) == -1) {
3088 err = got_error_from_errno("asprintf");
3089 goto err;
3091 if (asprintf(&href_link, "%.8s", bline->id_str) == -1) {
3092 err = got_error_from_errno("asprintf");
3093 goto err;
3095 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3096 KATTR_HREF, href_diff, KATTR__MAX);
3097 if (kerr != KCGI_OK)
3098 goto done;
3099 kerr = khtml_puts(a->gw_trans->gw_html_req, href_link);
3100 if (kerr != KCGI_OK)
3101 goto err;
3102 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3103 if (kerr != KCGI_OK)
3104 goto err;
3106 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3107 "blame_date", KATTR__MAX);
3108 if (kerr != KCGI_OK)
3109 goto err;
3110 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3111 if (kerr != KCGI_OK)
3112 goto err;
3113 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3114 if (kerr != KCGI_OK)
3115 goto err;
3117 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3118 "blame_author", KATTR__MAX);
3119 if (kerr != KCGI_OK)
3120 goto err;
3121 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
3122 if (kerr != KCGI_OK)
3123 goto err;
3124 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3125 if (kerr != KCGI_OK)
3126 goto err;
3128 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3129 "blame_code", KATTR__MAX);
3130 if (kerr != KCGI_OK)
3131 goto err;
3132 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
3133 if (kerr != KCGI_OK)
3134 goto err;
3135 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3136 if (kerr != KCGI_OK)
3137 goto err;
3139 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3140 if (kerr != KCGI_OK)
3141 goto err;
3143 a->lineno_cur++;
3144 bline = &a->lines[a->lineno_cur - 1];
3145 err:
3146 free(lineno);
3147 free(href_diff);
3148 free(href_link);
3150 done:
3151 if (commit)
3152 got_object_commit_close(commit);
3153 free(line);
3154 if (err == NULL && kerr != KCGI_OK)
3155 err = gw_kcgi_error(kerr);
3156 return err;
3159 static const struct got_error *
3160 gw_output_file_blame(struct gw_trans *gw_trans)
3162 const struct got_error *error = NULL;
3163 struct got_repository *repo = NULL;
3164 struct got_object_id *obj_id = NULL;
3165 struct got_object_id *commit_id = NULL;
3166 struct got_blob_object *blob = NULL;
3167 char *path = NULL, *in_repo_path = NULL;
3168 struct gw_blame_cb_args bca;
3169 int i, obj_type;
3170 size_t filesize;
3172 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3173 if (error)
3174 return error;
3176 if (asprintf(&path, "%s%s%s",
3177 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3178 gw_trans->repo_folder ? "/" : "",
3179 gw_trans->repo_file) == -1) {
3180 error = got_error_from_errno("asprintf");
3181 goto done;
3184 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3185 if (error)
3186 goto done;
3188 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3189 GOT_OBJ_TYPE_COMMIT, 1, repo);
3190 if (error)
3191 goto done;
3193 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3194 if (error)
3195 goto done;
3197 if (obj_id == NULL) {
3198 error = got_error(GOT_ERR_NO_OBJ);
3199 goto done;
3202 error = got_object_get_type(&obj_type, repo, obj_id);
3203 if (error)
3204 goto done;
3206 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3207 error = got_error(GOT_ERR_OBJ_TYPE);
3208 goto done;
3211 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3212 if (error)
3213 goto done;
3215 bca.f = got_opentemp();
3216 if (bca.f == NULL) {
3217 error = got_error_from_errno("got_opentemp");
3218 goto done;
3220 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3221 &bca.line_offsets, bca.f, blob);
3222 if (error || bca.nlines == 0)
3223 goto done;
3225 /* Don't include \n at EOF in the blame line count. */
3226 if (bca.line_offsets[bca.nlines - 1] == filesize)
3227 bca.nlines--;
3229 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3230 if (bca.lines == NULL) {
3231 error = got_error_from_errno("calloc");
3232 goto done;
3234 bca.lineno_cur = 1;
3235 bca.nlines_prec = 0;
3236 i = bca.nlines;
3237 while (i > 0) {
3238 i /= 10;
3239 bca.nlines_prec++;
3241 bca.repo = repo;
3242 bca.gw_trans = gw_trans;
3244 error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
3245 NULL, NULL);
3246 if (error)
3247 goto done;
3248 done:
3249 free(bca.line_offsets);
3250 free(in_repo_path);
3251 free(commit_id);
3252 free(obj_id);
3253 free(path);
3255 for (i = 0; i < bca.nlines; i++) {
3256 struct blame_line *bline = &bca.lines[i];
3257 free(bline->id_str);
3258 free(bline->committer);
3260 free(bca.lines);
3261 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3262 error = got_error_from_errno("fclose");
3263 if (blob)
3264 got_object_blob_close(blob);
3265 if (repo)
3266 got_repo_close(repo);
3267 return error;
3270 static const struct got_error *
3271 gw_output_blob_buf(struct gw_trans *gw_trans)
3273 const struct got_error *error = NULL;
3274 struct got_repository *repo = NULL;
3275 struct got_object_id *obj_id = NULL;
3276 struct got_object_id *commit_id = NULL;
3277 struct got_blob_object *blob = NULL;
3278 char *path = NULL, *in_repo_path = NULL;
3279 int obj_type, set_mime = 0;
3280 size_t len, hdrlen;
3281 const uint8_t *buf;
3282 enum kcgi_err kerr = KCGI_OK;
3284 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3285 if (error)
3286 return error;
3288 if (asprintf(&path, "%s%s%s",
3289 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3290 gw_trans->repo_folder ? "/" : "",
3291 gw_trans->repo_file) == -1) {
3292 error = got_error_from_errno("asprintf");
3293 goto done;
3296 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3297 if (error)
3298 goto done;
3300 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3301 GOT_OBJ_TYPE_COMMIT, 1, repo);
3302 if (error)
3303 goto done;
3305 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3306 if (error)
3307 goto done;
3309 if (obj_id == NULL) {
3310 error = got_error(GOT_ERR_NO_OBJ);
3311 goto done;
3314 error = got_object_get_type(&obj_type, repo, obj_id);
3315 if (error)
3316 goto done;
3318 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3319 error = got_error(GOT_ERR_OBJ_TYPE);
3320 goto done;
3323 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3324 if (error)
3325 goto done;
3327 hdrlen = got_object_blob_get_hdrlen(blob);
3328 do {
3329 error = got_object_blob_read_block(&len, blob);
3330 if (error)
3331 goto done;
3332 buf = got_object_blob_get_read_buf(blob);
3335 * Skip blob object header first time around,
3336 * which also contains a zero byte.
3338 buf += hdrlen;
3339 if (set_mime == 0) {
3340 if (isbinary(buf, len - hdrlen))
3341 gw_trans->mime = KMIME_APP_OCTET_STREAM;
3342 else
3343 gw_trans->mime = KMIME_TEXT_PLAIN;
3344 set_mime = 1;
3345 error = gw_display_index(gw_trans);
3346 if (error)
3347 goto done;
3349 khttp_write(gw_trans->gw_req, buf, len - hdrlen);
3350 hdrlen = 0;
3351 } while (len != 0);
3352 done:
3353 free(in_repo_path);
3354 free(commit_id);
3355 free(obj_id);
3356 free(path);
3357 if (blob)
3358 got_object_blob_close(blob);
3359 if (repo)
3360 got_repo_close(repo);
3361 if (error == NULL && kerr != KCGI_OK)
3362 error = gw_kcgi_error(kerr);
3363 return error;
3366 static const struct got_error *
3367 gw_output_repo_tree(struct gw_trans *gw_trans)
3369 const struct got_error *error = NULL;
3370 struct got_repository *repo = NULL;
3371 struct got_object_id *tree_id = NULL, *commit_id = NULL;
3372 struct got_tree_object *tree = NULL;
3373 char *path = NULL, *in_repo_path = NULL;
3374 char *id_str = NULL;
3375 char *build_folder = NULL;
3376 char *href_blob = NULL, *href_blame = NULL;
3377 const char *class = NULL;
3378 int nentries, i, class_flip = 0;
3379 enum kcgi_err kerr = KCGI_OK;
3381 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3382 if (error)
3383 goto done;
3385 if (gw_trans->repo_folder != NULL)
3386 path = strdup(gw_trans->repo_folder);
3387 else {
3388 error = got_repo_map_path(&in_repo_path, repo,
3389 gw_trans->repo_path, 1);
3390 if (error)
3391 goto done;
3392 free(path);
3393 path = in_repo_path;
3396 if (gw_trans->commit == NULL) {
3397 struct got_reference *head_ref;
3398 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
3399 if (error)
3400 goto done;
3401 error = got_ref_resolve(&commit_id, repo, head_ref);
3402 if (error)
3403 goto done;
3404 got_ref_close(head_ref);
3406 } else {
3407 error = got_repo_match_object_id(&commit_id, NULL,
3408 gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
3409 if (error)
3410 goto done;
3413 error = got_object_id_str(&gw_trans->commit, commit_id);
3414 if (error)
3415 goto done;
3417 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
3418 if (error)
3419 goto done;
3421 error = got_object_open_as_tree(&tree, repo, tree_id);
3422 if (error)
3423 goto done;
3425 nentries = got_object_tree_get_nentries(tree);
3426 for (i = 0; i < nentries; i++) {
3427 struct got_tree_entry *te;
3428 const char *modestr = "";
3429 mode_t mode;
3431 te = got_object_tree_get_entry(tree, i);
3433 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
3434 if (error)
3435 goto done;
3437 mode = got_tree_entry_get_mode(te);
3438 if (got_object_tree_entry_is_submodule(te))
3439 modestr = "$";
3440 else if (S_ISLNK(mode))
3441 modestr = "@";
3442 else if (S_ISDIR(mode))
3443 modestr = "/";
3444 else if (mode & S_IXUSR)
3445 modestr = "*";
3447 if (class_flip == 0) {
3448 class = "back_lightgray";
3449 class_flip = 1;
3450 } else {
3451 class = "back_white";
3452 class_flip = 0;
3455 if (S_ISDIR(mode)) {
3456 if (asprintf(&build_folder, "%s/%s",
3457 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3458 got_tree_entry_get_name(te)) == -1) {
3459 error = got_error_from_errno(
3460 "asprintf");
3461 goto done;
3463 if (asprintf(&href_blob,
3464 "?path=%s&action=%s&commit=%s&folder=%s",
3465 gw_trans->repo_name, gw_trans->action_name,
3466 gw_trans->commit, build_folder) == -1) {
3467 error = got_error_from_errno("asprintf");
3468 goto done;
3471 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3472 KATTR_ID, "tree_wrapper", KATTR__MAX);
3473 if (kerr != KCGI_OK)
3474 goto done;
3475 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3476 KATTR_ID, "tree_line", KATTR_CLASS, class,
3477 KATTR__MAX);
3478 if (kerr != KCGI_OK)
3479 goto done;
3480 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3481 KATTR_HREF, href_blob, KATTR_CLASS,
3482 "diff_directory", KATTR__MAX);
3483 if (kerr != KCGI_OK)
3484 goto done;
3485 kerr = khtml_puts(gw_trans->gw_html_req,
3486 got_tree_entry_get_name(te));
3487 if (kerr != KCGI_OK)
3488 goto done;
3489 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3490 if (kerr != KCGI_OK)
3491 goto done;
3492 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3493 if (kerr != KCGI_OK)
3494 goto done;
3495 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3496 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
3497 KATTR__MAX);
3498 if (kerr != KCGI_OK)
3499 goto done;
3500 kerr = khtml_entity(gw_trans->gw_html_req,
3501 KENTITY_nbsp);
3502 if (kerr != KCGI_OK)
3503 goto done;
3504 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3505 if (kerr != KCGI_OK)
3506 goto done;
3507 } else {
3508 if (asprintf(&href_blob,
3509 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3510 gw_trans->repo_name, "blob", gw_trans->commit,
3511 got_tree_entry_get_name(te),
3512 gw_trans->repo_folder ?
3513 gw_trans->repo_folder : "") == -1) {
3514 error = got_error_from_errno("asprintf");
3515 goto done;
3517 if (asprintf(&href_blame,
3518 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3519 gw_trans->repo_name, "blame", gw_trans->commit,
3520 got_tree_entry_get_name(te),
3521 gw_trans->repo_folder ?
3522 gw_trans->repo_folder : "") == -1) {
3523 error = got_error_from_errno("asprintf");
3524 goto done;
3527 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3528 KATTR_ID, "tree_wrapper", KATTR__MAX);
3529 if (kerr != KCGI_OK)
3530 goto done;
3531 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3532 KATTR_ID, "tree_line", KATTR_CLASS, class,
3533 KATTR__MAX);
3534 if (kerr != KCGI_OK)
3535 goto done;
3536 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3537 KATTR_HREF, href_blob, KATTR__MAX);
3538 if (kerr != KCGI_OK)
3539 goto done;
3540 kerr = khtml_puts(gw_trans->gw_html_req,
3541 got_tree_entry_get_name(te));
3542 if (kerr != KCGI_OK)
3543 goto done;
3544 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3545 if (kerr != KCGI_OK)
3546 goto done;
3547 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3548 if (kerr != KCGI_OK)
3549 goto done;
3550 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3551 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
3552 KATTR__MAX);
3553 if (kerr != KCGI_OK)
3554 goto done;
3556 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3557 KATTR_HREF, href_blob, KATTR__MAX);
3558 if (kerr != KCGI_OK)
3559 goto done;
3560 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
3561 if (kerr != KCGI_OK)
3562 goto done;
3563 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3564 if (kerr != KCGI_OK)
3565 goto done;
3567 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3568 if (kerr != KCGI_OK)
3569 goto done;
3571 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3572 KATTR_HREF, href_blame, KATTR__MAX);
3573 if (kerr != KCGI_OK)
3574 goto done;
3575 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
3576 if (kerr != KCGI_OK)
3577 goto done;
3579 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3580 if (kerr != KCGI_OK)
3581 goto done;
3583 free(id_str);
3584 id_str = NULL;
3585 free(href_blob);
3586 href_blob = NULL;
3587 free(build_folder);
3588 build_folder = NULL;
3590 done:
3591 if (tree)
3592 got_object_tree_close(tree);
3593 if (repo)
3594 got_repo_close(repo);
3595 free(id_str);
3596 free(href_blob);
3597 free(href_blame);
3598 free(in_repo_path);
3599 free(tree_id);
3600 free(build_folder);
3601 if (error == NULL && kerr != KCGI_OK)
3602 error = gw_kcgi_error(kerr);
3603 return error;
3606 static const struct got_error *
3607 gw_get_repo_heads(char **head_html, struct gw_trans *gw_trans)
3609 const struct got_error *error = NULL;
3610 struct got_repository *repo = NULL;
3611 struct got_reflist_head refs;
3612 struct got_reflist_entry *re;
3613 char *head_row = NULL, *head_navs_disp = NULL, *age = NULL;
3614 struct buf *diffbuf = NULL;
3615 size_t newsize;
3617 *head_html = NULL;
3619 SIMPLEQ_INIT(&refs);
3621 error = buf_alloc(&diffbuf, 0);
3622 if (error)
3623 return NULL;
3625 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3626 if (error)
3627 goto done;
3629 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
3630 NULL);
3631 if (error)
3632 goto done;
3634 SIMPLEQ_FOREACH(re, &refs, entry) {
3635 char *refname;
3637 refname = strdup(got_ref_get_name(re->ref));
3638 if (refname == NULL) {
3639 error = got_error_from_errno("got_ref_to_str");
3640 goto done;
3643 if (strncmp(refname, "refs/heads/", 11) != 0) {
3644 free(refname);
3645 continue;
3648 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
3649 refname, TM_DIFF);
3650 if (error)
3651 goto done;
3653 if (asprintf(&head_navs_disp, heads_navs, gw_trans->repo_name,
3654 refname, gw_trans->repo_name, refname,
3655 gw_trans->repo_name, refname, gw_trans->repo_name,
3656 refname) == -1) {
3657 error = got_error_from_errno("asprintf");
3658 goto done;
3661 if (strncmp(refname, "refs/heads/", 11) == 0)
3662 refname += 11;
3664 if (asprintf(&head_row, heads_row, age, refname,
3665 head_navs_disp) == -1) {
3666 error = got_error_from_errno("asprintf");
3667 goto done;
3670 error = buf_puts(&newsize, diffbuf, head_row);
3672 free(head_navs_disp);
3673 free(head_row);
3676 if (buf_len(diffbuf) > 0) {
3677 error = buf_putc(diffbuf, '\0');
3678 *head_html = strdup(buf_get(diffbuf));
3679 if (*head_html == NULL)
3680 error = got_error_from_errno("strdup");
3682 done:
3683 buf_free(diffbuf);
3684 got_ref_list_free(&refs);
3685 if (repo)
3686 got_repo_close(repo);
3687 return error;
3690 static char *
3691 gw_get_site_link(struct gw_trans *gw_trans)
3693 char *link = NULL, *repo = NULL, *action = NULL;
3695 if (gw_trans->repo_name != NULL &&
3696 asprintf(&repo, " / <a href='?path=%s&action=summary'>%s</a>",
3697 gw_trans->repo_name, gw_trans->repo_name) == -1)
3698 return NULL;
3700 if (gw_trans->action_name != NULL &&
3701 asprintf(&action, " / %s", gw_trans->action_name) == -1) {
3702 free(repo);
3703 return NULL;
3706 if (asprintf(&link, site_link, GOTWEB,
3707 gw_trans->gw_conf->got_site_link,
3708 repo ? repo : "", action ? action : "") == -1) {
3709 free(repo);
3710 free(action);
3711 return NULL;
3714 free(repo);
3715 free(action);
3716 return link;
3719 static const struct got_error *
3720 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
3722 const struct got_error *error = NULL;
3723 char *color = NULL;
3724 enum kcgi_err kerr = KCGI_OK;
3726 if (strncmp(buf, "-", 1) == 0)
3727 color = "diff_minus";
3728 else if (strncmp(buf, "+", 1) == 0)
3729 color = "diff_plus";
3730 else if (strncmp(buf, "@@", 2) == 0)
3731 color = "diff_chunk_header";
3732 else if (strncmp(buf, "@@", 2) == 0)
3733 color = "diff_chunk_header";
3734 else if (strncmp(buf, "commit +", 8) == 0)
3735 color = "diff_meta";
3736 else if (strncmp(buf, "commit -", 8) == 0)
3737 color = "diff_meta";
3738 else if (strncmp(buf, "blob +", 6) == 0)
3739 color = "diff_meta";
3740 else if (strncmp(buf, "blob -", 6) == 0)
3741 color = "diff_meta";
3742 else if (strncmp(buf, "file +", 6) == 0)
3743 color = "diff_meta";
3744 else if (strncmp(buf, "file -", 6) == 0)
3745 color = "diff_meta";
3746 else if (strncmp(buf, "from:", 5) == 0)
3747 color = "diff_author";
3748 else if (strncmp(buf, "via:", 4) == 0)
3749 color = "diff_author";
3750 else if (strncmp(buf, "date:", 5) == 0)
3751 color = "diff_date";
3752 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3753 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
3754 if (error == NULL && kerr != KCGI_OK)
3755 error = gw_kcgi_error(kerr);
3756 return error;
3759 int
3760 main(int argc, char *argv[])
3762 const struct got_error *error = NULL;
3763 struct gw_trans *gw_trans;
3764 struct gw_dir *dir = NULL, *tdir;
3765 const char *page = "index";
3766 int gw_malloc = 1;
3767 enum kcgi_err kerr;
3769 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
3770 errx(1, "malloc");
3772 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
3773 errx(1, "malloc");
3775 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
3776 errx(1, "malloc");
3778 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
3779 errx(1, "malloc");
3781 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
3782 if (kerr != KCGI_OK) {
3783 error = gw_kcgi_error(kerr);
3784 goto done;
3787 if ((gw_trans->gw_conf =
3788 malloc(sizeof(struct gotweb_conf))) == NULL) {
3789 gw_malloc = 0;
3790 error = got_error_from_errno("malloc");
3791 goto done;
3794 TAILQ_INIT(&gw_trans->gw_dirs);
3795 TAILQ_INIT(&gw_trans->gw_headers);
3797 gw_trans->page = 0;
3798 gw_trans->repos_total = 0;
3799 gw_trans->repo_path = NULL;
3800 gw_trans->commit = NULL;
3801 gw_trans->headref = strdup(GOT_REF_HEAD);
3802 gw_trans->mime = KMIME_TEXT_HTML;
3803 gw_trans->gw_tmpl->key = gw_templs;
3804 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
3805 gw_trans->gw_tmpl->arg = gw_trans;
3806 gw_trans->gw_tmpl->cb = gw_template;
3807 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
3808 if (error)
3809 goto done;
3811 error = gw_parse_querystring(gw_trans);
3812 if (error)
3813 goto done;
3815 if (gw_trans->action == GW_BLOB)
3816 error = gw_blob(gw_trans);
3817 else
3818 error = gw_display_index(gw_trans);
3819 done:
3820 if (error) {
3821 gw_trans->mime = KMIME_TEXT_PLAIN;
3822 gw_trans->action = GW_ERR;
3823 gw_display_error(gw_trans, error);
3825 if (gw_malloc) {
3826 free(gw_trans->gw_conf->got_repos_path);
3827 free(gw_trans->gw_conf->got_site_name);
3828 free(gw_trans->gw_conf->got_site_owner);
3829 free(gw_trans->gw_conf->got_site_link);
3830 free(gw_trans->gw_conf->got_logo);
3831 free(gw_trans->gw_conf->got_logo_url);
3832 free(gw_trans->gw_conf);
3833 free(gw_trans->commit);
3834 free(gw_trans->repo_path);
3835 free(gw_trans->repo_name);
3836 free(gw_trans->repo_file);
3837 free(gw_trans->action_name);
3838 free(gw_trans->headref);
3840 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
3841 free(dir->name);
3842 free(dir->description);
3843 free(dir->age);
3844 free(dir->url);
3845 free(dir->path);
3846 free(dir);
3851 khttp_free(gw_trans->gw_req);
3852 return 0;