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 "gotweb.h"
51 #ifndef nitems
52 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
53 #endif
55 struct gw_trans {
56 TAILQ_HEAD(headers, gw_header) gw_headers;
57 TAILQ_HEAD(dirs, gw_dir) gw_dirs;
58 struct gw_dir *gw_dir;
59 struct gotweb_conf *gw_conf;
60 struct ktemplate *gw_tmpl;
61 struct khtmlreq *gw_html_req;
62 struct kreq *gw_req;
63 const struct got_error *error;
64 const char *repo_name;
65 char *repo_path;
66 char *commit;
67 const char *repo_file;
68 char *repo_folder;
69 char *headref;
70 unsigned int action;
71 unsigned int page;
72 unsigned int repos_total;
73 enum kmime mime;
74 };
76 struct gw_header {
77 TAILQ_ENTRY(gw_header) entry;
78 struct got_repository *repo;
79 struct got_reflist_head refs;
80 struct got_commit_object *commit;
81 struct got_object_id *id;
82 char *path;
84 char *refs_str;
85 char *commit_id; /* id_str1 */
86 char *parent_id; /* id_str2 */
87 char *tree_id;
88 const char *author;
89 const char *committer;
90 char *commit_msg;
91 time_t committer_time;
92 };
94 struct gw_dir {
95 TAILQ_ENTRY(gw_dir) entry;
96 char *name;
97 char *owner;
98 char *description;
99 char *url;
100 char *age;
101 char *path;
102 };
104 enum gw_key {
105 KEY_ACTION,
106 KEY_COMMIT_ID,
107 KEY_FILE,
108 KEY_FOLDER,
109 KEY_HEADREF,
110 KEY_PAGE,
111 KEY_PATH,
112 KEY__ZMAX
113 };
115 enum gw_tmpl {
116 TEMPL_CONTENT,
117 TEMPL_HEAD,
118 TEMPL_HEADER,
119 TEMPL_SEARCH,
120 TEMPL_SITEPATH,
121 TEMPL_SITEOWNER,
122 TEMPL_TITLE,
123 TEMPL__MAX
124 };
126 enum gw_ref_tm {
127 TM_DIFF,
128 TM_LONG,
129 };
131 enum gw_tags {
132 TAGBRIEF,
133 TAGFULL,
134 };
136 static const char *const gw_templs[TEMPL__MAX] = {
137 "content",
138 "head",
139 "header",
140 "search",
141 "sitepath",
142 "siteowner",
143 "title",
144 };
146 static const struct kvalid gw_keys[KEY__ZMAX] = {
147 { kvalid_stringne, "action" },
148 { kvalid_stringne, "commit" },
149 { kvalid_stringne, "file" },
150 { kvalid_stringne, "folder" },
151 { kvalid_stringne, "headref" },
152 { kvalid_int, "page" },
153 { kvalid_stringne, "path" },
154 };
156 static struct gw_header *gw_init_header(void);
158 static void gw_free_headers(struct gw_header *);
160 static int gw_template(size_t, void *);
162 static const struct got_error *gw_error(struct gw_trans *);
163 static const struct got_error *gw_init_gw_dir(struct gw_dir **, const char *);
164 static const struct got_error *gw_get_repo_description(char **,
165 struct gw_trans *, char *);
166 static const struct got_error *gw_get_repo_owner(char **, struct gw_trans *,
167 char *);
168 static const struct got_error *gw_get_time_str(char **, time_t, int);
169 static const struct got_error *gw_get_repo_age(char **, struct gw_trans *,
170 char *, char *, int);
171 static const struct got_error *gw_output_file_blame(struct gw_trans *);
172 static const struct got_error *gw_output_blob_buf(struct gw_trans *);
173 static const struct got_error *gw_output_repo_tree(struct gw_trans *);
174 static const struct got_error *gw_output_diff(struct gw_trans *,
175 struct gw_header *);
176 static const struct got_error *gw_output_repo_tags(struct gw_trans *,
177 struct gw_header *, int, int);
178 static const struct got_error *gw_output_repo_heads(struct gw_trans *);
179 static const struct got_error *gw_output_site_link(struct gw_trans *);
180 static const struct got_error *gw_get_clone_url(char **, struct gw_trans *,
181 char *);
182 static const struct got_error *gw_colordiff_line(struct gw_trans *, char *);
184 static const struct got_error *gw_gen_commit_header(struct gw_trans *, char *,
185 char*);
186 static const struct got_error *gw_gen_diff_header(struct gw_trans *, char *,
187 char*);
188 static const struct got_error *gw_gen_author_header(struct gw_trans *,
189 const char *);
190 static const struct got_error *gw_gen_age_header(struct gw_trans *,
191 const char *);
192 static const struct got_error *gw_gen_committer_header(struct gw_trans *,
193 const char *);
194 static const struct got_error *gw_gen_commit_msg_header(struct gw_trans*,
195 char *);
196 static const struct got_error *gw_gen_tree_header(struct gw_trans *, char *);
197 static const struct got_error *gw_display_open(struct gw_trans *, enum khttp,
198 enum kmime);
199 static const struct got_error *gw_display_index(struct gw_trans *);
200 static const struct got_error *gw_get_header(struct gw_trans *,
201 struct gw_header *, int);
202 static const struct got_error *gw_get_commits(struct gw_trans *,
203 struct gw_header *, int);
204 static const struct got_error *gw_get_commit(struct gw_trans *,
205 struct gw_header *);
206 static const struct got_error *gw_apply_unveil(const char *);
207 static const struct got_error *gw_blame_cb(void *, int, int,
208 struct got_object_id *);
209 static const struct got_error *gw_load_got_paths(struct gw_trans *);
210 static const struct got_error *gw_load_got_path(struct gw_trans *,
211 struct gw_dir *);
212 static const struct got_error *gw_parse_querystring(struct gw_trans *);
213 static const struct got_error *gw_blame(struct gw_trans *);
214 static const struct got_error *gw_blob(struct gw_trans *);
215 static const struct got_error *gw_diff(struct gw_trans *);
216 static const struct got_error *gw_index(struct gw_trans *);
217 static const struct got_error *gw_commits(struct gw_trans *);
218 static const struct got_error *gw_briefs(struct gw_trans *);
219 static const struct got_error *gw_summary(struct gw_trans *);
220 static const struct got_error *gw_tree(struct gw_trans *);
221 static const struct got_error *gw_tag(struct gw_trans *);
223 struct gw_query_action {
224 unsigned int func_id;
225 const char *func_name;
226 const struct got_error *(*func_main)(struct gw_trans *);
227 char *template;
228 };
230 enum gw_query_actions {
231 GW_BLAME,
232 GW_BLOB,
233 GW_BRIEFS,
234 GW_COMMITS,
235 GW_DIFF,
236 GW_ERR,
237 GW_INDEX,
238 GW_SUMMARY,
239 GW_TAG,
240 GW_TREE,
241 };
243 static struct gw_query_action gw_query_funcs[] = {
244 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
245 { GW_BLOB, "blob", NULL, NULL },
246 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
247 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
248 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
249 { GW_ERR, "error", gw_error, "gw_tmpl/err.tmpl" },
250 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
251 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
252 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
253 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
254 };
256 static const char *
257 gw_get_action_name(struct gw_trans *gw_trans)
259 return gw_query_funcs[gw_trans->action].func_name;
262 static const struct got_error *
263 gw_kcgi_error(enum kcgi_err kerr)
265 if (kerr == KCGI_OK)
266 return NULL;
268 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
269 return got_error(GOT_ERR_CANCELLED);
271 if (kerr == KCGI_ENOMEM)
272 return got_error_set_errno(ENOMEM,
273 kcgi_strerror(kerr));
275 if (kerr == KCGI_ENFILE)
276 return got_error_set_errno(ENFILE,
277 kcgi_strerror(kerr));
279 if (kerr == KCGI_EAGAIN)
280 return got_error_set_errno(EAGAIN,
281 kcgi_strerror(kerr));
283 if (kerr == KCGI_FORM)
284 return got_error_msg(GOT_ERR_IO,
285 kcgi_strerror(kerr));
287 return got_error_from_errno(kcgi_strerror(kerr));
290 static const struct got_error *
291 gw_apply_unveil(const char *repo_path)
293 const struct got_error *err;
295 if (repo_path && unveil(repo_path, "r") != 0)
296 return got_error_from_errno2("unveil", repo_path);
298 if (unveil("/tmp", "rwc") != 0)
299 return got_error_from_errno2("unveil", "/tmp");
301 err = got_privsep_unveil_exec_helpers();
302 if (err != NULL)
303 return err;
305 if (unveil(NULL, NULL) != 0)
306 return got_error_from_errno("unveil");
308 return NULL;
311 static int
312 isbinary(const uint8_t *buf, size_t n)
314 size_t i;
316 for (i = 0; i < n; i++)
317 if (buf[i] == 0)
318 return 1;
319 return 0;
322 static const struct got_error *
323 gw_blame(struct gw_trans *gw_trans)
325 const struct got_error *error = NULL;
326 struct gw_header *header = NULL;
327 char *age = NULL;
328 enum kcgi_err kerr;
330 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
331 NULL) == -1)
332 return got_error_from_errno("pledge");
334 if ((header = gw_init_header()) == NULL)
335 return got_error_from_errno("malloc");
337 error = gw_apply_unveil(gw_trans->gw_dir->path);
338 if (error)
339 goto done;
341 error = gw_get_header(gw_trans, header, 1);
342 if (error)
343 goto done;
345 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
346 "blame_header_wrapper", KATTR__MAX);
347 if (kerr != KCGI_OK)
348 goto done;
349 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
350 "blame_header", KATTR__MAX);
351 if (kerr != KCGI_OK)
352 goto done;
353 error = gw_get_time_str(&age, header->committer_time,
354 TM_LONG);
355 if (error)
356 goto done;
357 error = gw_gen_age_header(gw_trans, age ?age : "");
358 if (error)
359 goto done;
360 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
361 if (error)
362 goto done;
363 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
364 if (kerr != KCGI_OK)
365 goto done;
366 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
367 "dotted_line", KATTR__MAX);
368 if (kerr != KCGI_OK)
369 goto done;
370 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
371 if (kerr != KCGI_OK)
372 goto done;
374 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
375 "blame", KATTR__MAX);
376 if (kerr != KCGI_OK)
377 goto done;
378 error = gw_output_file_blame(gw_trans);
379 if (error)
380 goto done;
381 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
382 done:
383 got_ref_list_free(&header->refs);
384 gw_free_headers(header);
385 if (error == NULL && kerr != KCGI_OK)
386 error = gw_kcgi_error(kerr);
387 return error;
390 static const struct got_error *
391 gw_blob(struct gw_trans *gw_trans)
393 const struct got_error *error = NULL;
394 struct gw_header *header = NULL;
396 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
397 NULL) == -1)
398 return got_error_from_errno("pledge");
400 if ((header = gw_init_header()) == NULL)
401 return got_error_from_errno("malloc");
403 error = gw_apply_unveil(gw_trans->gw_dir->path);
404 if (error)
405 goto done;
407 error = gw_get_header(gw_trans, header, 1);
408 if (error)
409 goto done;
411 error = gw_output_blob_buf(gw_trans);
412 done:
413 got_ref_list_free(&header->refs);
414 gw_free_headers(header);
415 return error;
418 static const struct got_error *
419 gw_diff(struct gw_trans *gw_trans)
421 const struct got_error *error = NULL;
422 struct gw_header *header = NULL;
423 char *age = NULL;
424 enum kcgi_err kerr = KCGI_OK;
426 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
427 NULL) == -1)
428 return got_error_from_errno("pledge");
430 if ((header = gw_init_header()) == NULL)
431 return got_error_from_errno("malloc");
433 error = gw_apply_unveil(gw_trans->gw_dir->path);
434 if (error)
435 goto done;
437 error = gw_get_header(gw_trans, header, 1);
438 if (error)
439 goto done;
441 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
442 "diff_header_wrapper", KATTR__MAX);
443 if (kerr != KCGI_OK)
444 goto done;
445 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
446 "diff_header", KATTR__MAX);
447 if (kerr != KCGI_OK)
448 goto done;
449 error = gw_gen_diff_header(gw_trans, header->parent_id,
450 header->commit_id);
451 if (error)
452 goto done;
453 error = gw_gen_commit_header(gw_trans, header->commit_id,
454 header->refs_str);
455 if (error)
456 goto done;
457 error = gw_gen_tree_header(gw_trans, header->tree_id);
458 if (error)
459 goto done;
460 error = gw_gen_author_header(gw_trans, header->author);
461 if (error)
462 goto done;
463 error = gw_gen_committer_header(gw_trans, header->author);
464 if (error)
465 goto done;
466 error = gw_get_time_str(&age, header->committer_time,
467 TM_LONG);
468 if (error)
469 goto done;
470 error = gw_gen_age_header(gw_trans, age ?age : "");
471 if (error)
472 goto done;
473 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
474 if (error)
475 goto done;
476 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
477 if (kerr != KCGI_OK)
478 goto done;
479 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
480 "dotted_line", KATTR__MAX);
481 if (kerr != KCGI_OK)
482 goto done;
483 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
484 if (kerr != KCGI_OK)
485 goto done;
487 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
488 "diff", KATTR__MAX);
489 if (kerr != KCGI_OK)
490 goto done;
491 error = gw_output_diff(gw_trans, header);
492 if (error)
493 goto done;
495 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
496 done:
497 got_ref_list_free(&header->refs);
498 gw_free_headers(header);
499 free(age);
500 if (error == NULL && kerr != KCGI_OK)
501 error = gw_kcgi_error(kerr);
502 return error;
505 static const struct got_error *
506 gw_index(struct gw_trans *gw_trans)
508 const struct got_error *error = NULL;
509 struct gw_dir *gw_dir = NULL;
510 char *href_next = NULL, *href_prev = NULL, *href_summary = NULL;
511 char *href_briefs = NULL, *href_commits = NULL, *href_tree = NULL;
512 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
513 enum kcgi_err kerr;
515 if (pledge("stdio rpath proc exec sendfd unveil",
516 NULL) == -1) {
517 error = got_error_from_errno("pledge");
518 return error;
521 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path);
522 if (error)
523 return error;
525 error = gw_load_got_paths(gw_trans);
526 if (error)
527 return error;
529 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
530 "index_header", KATTR__MAX);
531 if (kerr != KCGI_OK)
532 return gw_kcgi_error(kerr);
533 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
534 "index_header_project", KATTR__MAX);
535 if (kerr != KCGI_OK)
536 return gw_kcgi_error(kerr);
537 kerr = khtml_puts(gw_trans->gw_html_req, "Project");
538 if (kerr != KCGI_OK)
539 return gw_kcgi_error(kerr);
540 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
541 if (kerr != KCGI_OK)
542 return gw_kcgi_error(kerr);
544 if (gw_trans->gw_conf->got_show_repo_description) {
545 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
546 "index_header_description", KATTR__MAX);
547 if (kerr != KCGI_OK)
548 return gw_kcgi_error(kerr);
549 kerr = khtml_puts(gw_trans->gw_html_req, "Description");
550 if (kerr != KCGI_OK)
551 return gw_kcgi_error(kerr);
552 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
553 if (kerr != KCGI_OK)
554 return gw_kcgi_error(kerr);
557 if (gw_trans->gw_conf->got_show_repo_owner) {
558 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
559 "index_header_owner", KATTR__MAX);
560 if (kerr != KCGI_OK)
561 return gw_kcgi_error(kerr);
562 kerr = khtml_puts(gw_trans->gw_html_req, "Owner");
563 if (kerr != KCGI_OK)
564 return gw_kcgi_error(kerr);
565 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
566 if (kerr != KCGI_OK)
567 return gw_kcgi_error(kerr);
570 if (gw_trans->gw_conf->got_show_repo_age) {
571 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
572 "index_header_age", KATTR__MAX);
573 if (kerr != KCGI_OK)
574 return gw_kcgi_error(kerr);
575 kerr = khtml_puts(gw_trans->gw_html_req, "Last Change");
576 if (kerr != KCGI_OK)
577 return gw_kcgi_error(kerr);
578 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
579 if (kerr != KCGI_OK)
580 return gw_kcgi_error(kerr);
583 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
584 if (kerr != KCGI_OK)
585 return gw_kcgi_error(kerr);
587 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
588 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
589 "index_wrapper", KATTR__MAX);
590 if (kerr != KCGI_OK)
591 return gw_kcgi_error(kerr);
592 kerr = khtml_puts(gw_trans->gw_html_req,
593 "No repositories found in ");
594 if (kerr != KCGI_OK)
595 return gw_kcgi_error(kerr);
596 kerr = khtml_puts(gw_trans->gw_html_req,
597 gw_trans->gw_conf->got_repos_path);
598 if (kerr != KCGI_OK)
599 return gw_kcgi_error(kerr);
600 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
601 if (kerr != KCGI_OK)
602 return gw_kcgi_error(kerr);
603 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
604 "dotted_line", KATTR__MAX);
605 if (kerr != KCGI_OK)
606 return gw_kcgi_error(kerr);
607 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
608 if (kerr != KCGI_OK)
609 return gw_kcgi_error(kerr);
610 return error;
613 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
614 dir_c++;
616 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
617 if (gw_trans->page > 0 && (gw_trans->page *
618 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
619 prev_disp++;
620 continue;
623 prev_disp++;
625 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
626 "index_wrapper", KATTR__MAX);
627 if (kerr != KCGI_OK)
628 goto done;
630 if (asprintf(&href_summary, "?path=%s&action=summary",
631 gw_dir->name) == -1) {
632 error = got_error_from_errno("asprintf");
633 goto done;
635 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
636 "index_project", KATTR__MAX);
637 if (kerr != KCGI_OK)
638 goto done;
639 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
640 href_summary, KATTR__MAX);
641 if (kerr != KCGI_OK)
642 goto done;
643 kerr = khtml_puts(gw_trans->gw_html_req, gw_dir->name);
644 if (kerr != KCGI_OK)
645 goto done;
646 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
647 if (kerr != KCGI_OK)
648 goto done;
649 if (gw_trans->gw_conf->got_show_repo_description) {
650 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
651 KATTR_ID, "index_project_description", KATTR__MAX);
652 if (kerr != KCGI_OK)
653 goto done;
654 kerr = khtml_puts(gw_trans->gw_html_req,
655 gw_dir->description ? gw_dir->description : "");
656 if (kerr != KCGI_OK)
657 goto done;
658 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
659 if (kerr != KCGI_OK)
660 goto done;
662 if (gw_trans->gw_conf->got_show_repo_owner) {
663 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
664 KATTR_ID, "index_project_owner", KATTR__MAX);
665 if (kerr != KCGI_OK)
666 goto done;
667 kerr = khtml_puts(gw_trans->gw_html_req,
668 gw_dir->owner ? gw_dir->owner : "");
669 if (kerr != KCGI_OK)
670 goto done;
671 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
672 if (kerr != KCGI_OK)
673 goto done;
675 if (gw_trans->gw_conf->got_show_repo_age) {
676 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
677 KATTR_ID, "index_project_age", KATTR__MAX);
678 if (kerr != KCGI_OK)
679 goto done;
680 kerr = khtml_puts(gw_trans->gw_html_req,
681 gw_dir->age ? gw_dir->age : "");
682 if (kerr != KCGI_OK)
683 goto done;
684 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
685 if (kerr != KCGI_OK)
686 goto done;
689 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
690 "navs_wrapper", KATTR__MAX);
691 if (kerr != KCGI_OK)
692 goto done;
693 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
694 "navs", KATTR__MAX);
695 if (kerr != KCGI_OK)
696 goto done;
698 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
699 href_summary, KATTR__MAX);
700 if (kerr != KCGI_OK)
701 goto done;
702 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
703 if (kerr != KCGI_OK)
704 goto done;
705 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
706 if (kerr != KCGI_OK)
707 goto done;
709 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
710 if (kerr != KCGI_OK)
711 goto done;
713 if (asprintf(&href_briefs, "?path=%s&action=briefs",
714 gw_dir->name) == -1) {
715 error = got_error_from_errno("asprintf");
716 goto done;
718 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
719 href_briefs, KATTR__MAX);
720 if (kerr != KCGI_OK)
721 goto done;
722 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
723 if (kerr != KCGI_OK)
724 goto done;
725 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
726 if (kerr != KCGI_OK)
727 error = gw_kcgi_error(kerr);
729 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
730 if (kerr != KCGI_OK)
731 goto done;
733 if (asprintf(&href_commits, "?path=%s&action=commits",
734 gw_dir->name) == -1) {
735 error = got_error_from_errno("asprintf");
736 goto done;
738 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
739 href_commits, KATTR__MAX);
740 if (kerr != KCGI_OK)
741 goto done;
742 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
743 if (kerr != KCGI_OK)
744 goto done;
745 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
746 if (kerr != KCGI_OK)
747 goto done;
749 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
750 if (kerr != KCGI_OK)
751 goto done;
753 if (asprintf(&href_tree, "?path=%s&action=tree",
754 gw_dir->name) == -1) {
755 error = got_error_from_errno("asprintf");
756 goto done;
758 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
759 href_tree, KATTR__MAX);
760 if (kerr != KCGI_OK)
761 goto done;
762 kerr = khtml_puts(gw_trans->gw_html_req, "tree");
763 if (kerr != KCGI_OK)
764 goto done;
766 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
767 if (kerr != KCGI_OK)
768 goto done;
769 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
770 "dotted_line", KATTR__MAX);
771 if (kerr != KCGI_OK)
772 goto done;
773 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
774 if (kerr != KCGI_OK)
775 goto done;
777 free(href_summary);
778 href_summary = NULL;
779 free(href_briefs);
780 href_briefs = NULL;
781 free(href_commits);
782 href_commits = NULL;
783 free(href_tree);
784 href_tree = NULL;
786 if (gw_trans->gw_conf->got_max_repos_display == 0)
787 continue;
789 if (next_disp == gw_trans->gw_conf->got_max_repos_display) {
790 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
791 KATTR_ID, "np_wrapper", KATTR__MAX);
792 if (kerr != KCGI_OK)
793 goto done;
794 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
795 KATTR_ID, "nav_prev", KATTR__MAX);
796 if (kerr != KCGI_OK)
797 goto done;
798 } else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
799 (gw_trans->page > 0) &&
800 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
801 prev_disp == gw_trans->repos_total)) {
802 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
803 KATTR_ID, "np_wrapper", KATTR__MAX);
804 if (kerr != KCGI_OK)
805 goto done;
806 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
807 KATTR_ID, "nav_prev", KATTR__MAX);
808 if (kerr != KCGI_OK)
809 goto done;
812 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
813 (gw_trans->page > 0) &&
814 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
815 prev_disp == gw_trans->repos_total)) {
816 if (asprintf(&href_prev, "?page=%d",
817 gw_trans->page - 1) == -1) {
818 error = got_error_from_errno("asprintf");
819 goto done;
821 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
822 KATTR_HREF, href_prev, KATTR__MAX);
823 if (kerr != KCGI_OK)
824 goto done;
825 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
826 if (kerr != KCGI_OK)
827 goto done;
828 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
829 if (kerr != KCGI_OK)
830 goto done;
833 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
834 if (kerr != KCGI_OK)
835 return gw_kcgi_error(kerr);
837 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
838 next_disp == gw_trans->gw_conf->got_max_repos_display &&
839 dir_c != (gw_trans->page + 1) *
840 gw_trans->gw_conf->got_max_repos_display) {
841 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
842 KATTR_ID, "nav_next", KATTR__MAX);
843 if (kerr != KCGI_OK)
844 goto done;
845 if (asprintf(&href_next, "?page=%d",
846 gw_trans->page + 1) == -1) {
847 error = got_error_from_errno("calloc");
848 goto done;
850 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
851 KATTR_HREF, href_next, KATTR__MAX);
852 if (kerr != KCGI_OK)
853 goto done;
854 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
855 if (kerr != KCGI_OK)
856 goto done;
857 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
858 if (kerr != KCGI_OK)
859 goto done;
860 next_disp = 0;
861 break;
864 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
865 (gw_trans->page > 0) &&
866 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
867 prev_disp == gw_trans->repos_total)) {
868 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
869 if (kerr != KCGI_OK)
870 goto done;
872 next_disp++;
874 done:
875 free(href_prev);
876 free(href_next);
877 free(href_summary);
878 free(href_briefs);
879 free(href_commits);
880 free(href_tree);
881 if (error == NULL && kerr != KCGI_OK)
882 error = gw_kcgi_error(kerr);
883 return error;
886 static const struct got_error *
887 gw_commits(struct gw_trans *gw_trans)
889 const struct got_error *error = NULL;
890 struct gw_header *header = NULL, *n_header = NULL;
891 char *age = NULL;
892 char *href_diff = NULL, *href_blob = NULL;
893 enum kcgi_err kerr = KCGI_OK;
895 if ((header = gw_init_header()) == NULL)
896 return got_error_from_errno("malloc");
898 if (pledge("stdio rpath proc exec sendfd unveil",
899 NULL) == -1) {
900 error = got_error_from_errno("pledge");
901 goto done;
904 error = gw_apply_unveil(gw_trans->gw_dir->path);
905 if (error)
906 goto done;
908 error = gw_get_header(gw_trans, header,
909 gw_trans->gw_conf->got_max_commits_display);
910 if (error)
911 goto done;
913 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
914 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
915 "commits_line_wrapper", KATTR__MAX);
916 if (kerr != KCGI_OK)
917 goto done;
918 error = gw_gen_commit_header(gw_trans, n_header->commit_id,
919 n_header->refs_str);
920 if (error)
921 goto done;
922 error = gw_gen_author_header(gw_trans, n_header->author);
923 if (error)
924 goto done;
925 error = gw_gen_committer_header(gw_trans, n_header->author);
926 if (error)
927 goto done;
928 error = gw_get_time_str(&age, n_header->committer_time,
929 TM_LONG);
930 if (error)
931 goto done;
932 error = gw_gen_age_header(gw_trans, age ?age : "");
933 if (error)
934 goto done;
935 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
936 if (kerr != KCGI_OK)
937 goto done;
939 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
940 "dotted_line", KATTR__MAX);
941 if (kerr != KCGI_OK)
942 goto done;
943 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
944 if (kerr != KCGI_OK)
945 goto done;
947 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
948 "commit", KATTR__MAX);
949 if (kerr != KCGI_OK)
950 goto done;
951 kerr = khttp_puts(gw_trans->gw_req, n_header->commit_msg);
952 if (kerr != KCGI_OK)
953 goto done;
954 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
955 if (kerr != KCGI_OK)
956 goto done;
958 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
959 gw_trans->repo_name, n_header->commit_id) == -1) {
960 error = got_error_from_errno("asprintf");
961 goto done;
963 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
964 KATTR_ID, "navs_wrapper", KATTR__MAX);
965 if (kerr != KCGI_OK)
966 goto done;
967 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
968 KATTR_ID, "navs", KATTR__MAX);
969 if (kerr != KCGI_OK)
970 goto done;
971 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
972 KATTR_HREF, href_diff, KATTR__MAX);
973 if (kerr != KCGI_OK)
974 goto done;
975 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
976 if (kerr != KCGI_OK)
977 goto done;
978 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
979 if (kerr != KCGI_OK)
980 goto done;
982 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
983 if (kerr != KCGI_OK)
984 goto done;
986 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
987 gw_trans->repo_name, n_header->commit_id) == -1) {
988 error = got_error_from_errno("asprintf");
989 goto done;
991 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
992 KATTR_HREF, href_blob, KATTR__MAX);
993 if (kerr != KCGI_OK)
994 goto done;
995 khtml_puts(gw_trans->gw_html_req, "tree");
996 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
997 if (kerr != KCGI_OK)
998 goto done;
999 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1000 if (kerr != KCGI_OK)
1001 goto done;
1003 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1004 "solid_line", KATTR__MAX);
1005 if (kerr != KCGI_OK)
1006 goto done;
1007 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1008 if (kerr != KCGI_OK)
1009 goto done;
1011 free(age);
1012 age = NULL;
1014 done:
1015 got_ref_list_free(&header->refs);
1016 gw_free_headers(header);
1017 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1018 gw_free_headers(n_header);
1019 free(age);
1020 free(href_diff);
1021 free(href_blob);
1022 if (error == NULL && kerr != KCGI_OK)
1023 error = gw_kcgi_error(kerr);
1024 return error;
1027 static const struct got_error *
1028 gw_briefs(struct gw_trans *gw_trans)
1030 const struct got_error *error = NULL;
1031 struct gw_header *header = NULL, *n_header = NULL;
1032 char *age = NULL;
1033 char *href_diff = NULL, *href_blob = NULL;
1034 char *newline, *smallerthan;
1035 enum kcgi_err kerr = KCGI_OK;
1037 if ((header = gw_init_header()) == NULL)
1038 return got_error_from_errno("malloc");
1040 if (pledge("stdio rpath proc exec sendfd unveil",
1041 NULL) == -1) {
1042 error = got_error_from_errno("pledge");
1043 goto done;
1046 error = gw_apply_unveil(gw_trans->gw_dir->path);
1047 if (error)
1048 goto done;
1050 if (gw_trans->action == GW_SUMMARY)
1051 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
1052 else
1053 error = gw_get_header(gw_trans, header,
1054 gw_trans->gw_conf->got_max_commits_display);
1055 if (error)
1056 goto done;
1058 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
1059 error = gw_get_time_str(&age, n_header->committer_time,
1060 TM_DIFF);
1061 if (error)
1062 goto done;
1064 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1065 KATTR_ID, "briefs_wrapper", KATTR__MAX);
1066 if (kerr != KCGI_OK)
1067 goto done;
1069 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1070 KATTR_ID, "briefs_age", KATTR__MAX);
1071 if (kerr != KCGI_OK)
1072 goto done;
1073 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
1074 if (kerr != KCGI_OK)
1075 goto done;
1076 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1077 if (kerr != KCGI_OK)
1078 goto done;
1080 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1081 KATTR_ID, "briefs_author", KATTR__MAX);
1082 if (kerr != KCGI_OK)
1083 goto done;
1084 smallerthan = strchr(n_header->author, '<');
1085 if (smallerthan)
1086 *smallerthan = '\0';
1087 kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
1088 if (kerr != KCGI_OK)
1089 goto done;
1090 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1091 if (kerr != KCGI_OK)
1092 goto done;
1094 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
1095 gw_trans->repo_name, n_header->commit_id) == -1) {
1096 error = got_error_from_errno("asprintf");
1097 goto done;
1099 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1100 KATTR_ID, "briefs_log", KATTR__MAX);
1101 if (kerr != KCGI_OK)
1102 goto done;
1103 newline = strchr(n_header->commit_msg, '\n');
1104 if (newline)
1105 *newline = '\0';
1106 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1107 KATTR_HREF, href_diff, KATTR__MAX);
1108 if (kerr != KCGI_OK)
1109 goto done;
1110 kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
1111 if (kerr != KCGI_OK)
1112 goto done;
1113 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1114 if (kerr != KCGI_OK)
1115 goto done;
1117 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1118 KATTR_ID, "navs_wrapper", KATTR__MAX);
1119 if (kerr != KCGI_OK)
1120 goto done;
1121 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1122 KATTR_ID, "navs", KATTR__MAX);
1123 if (kerr != KCGI_OK)
1124 goto done;
1125 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1126 KATTR_HREF, href_diff, KATTR__MAX);
1127 if (kerr != KCGI_OK)
1128 goto done;
1129 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1130 if (kerr != KCGI_OK)
1131 goto done;
1132 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1133 if (kerr != KCGI_OK)
1134 goto done;
1136 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1137 if (kerr != KCGI_OK)
1138 goto done;
1140 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
1141 gw_trans->repo_name, n_header->commit_id) == -1) {
1142 error = got_error_from_errno("asprintf");
1143 goto done;
1145 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1146 KATTR_HREF, href_blob, KATTR__MAX);
1147 if (kerr != KCGI_OK)
1148 goto done;
1149 khtml_puts(gw_trans->gw_html_req, "tree");
1150 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1151 if (kerr != KCGI_OK)
1152 goto done;
1153 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1154 if (kerr != KCGI_OK)
1155 goto done;
1157 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1158 KATTR_ID, "dotted_line", KATTR__MAX);
1159 if (kerr != KCGI_OK)
1160 goto done;
1161 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1162 if (kerr != KCGI_OK)
1163 goto done;
1165 free(age);
1166 age = NULL;
1167 free(href_diff);
1168 href_diff = NULL;
1169 free(href_blob);
1170 href_blob = NULL;
1172 done:
1173 got_ref_list_free(&header->refs);
1174 gw_free_headers(header);
1175 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1176 gw_free_headers(n_header);
1177 free(age);
1178 free(href_diff);
1179 free(href_blob);
1180 if (error == NULL && kerr != KCGI_OK)
1181 error = gw_kcgi_error(kerr);
1182 return error;
1185 static const struct got_error *
1186 gw_summary(struct gw_trans *gw_trans)
1188 const struct got_error *error = NULL;
1189 char *age = NULL;
1190 enum kcgi_err kerr = KCGI_OK;
1192 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1193 return got_error_from_errno("pledge");
1195 /* unveil is applied with gw_briefs below */
1197 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1198 "summary_wrapper", KATTR__MAX);
1199 if (kerr != KCGI_OK)
1200 return gw_kcgi_error(kerr);
1202 if (gw_trans->gw_conf->got_show_repo_description &&
1203 gw_trans->gw_dir->description != NULL &&
1204 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1205 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1206 KATTR_ID, "description_title", KATTR__MAX);
1207 if (kerr != KCGI_OK)
1208 goto done;
1209 kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1210 if (kerr != KCGI_OK)
1211 goto done;
1212 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1213 if (kerr != KCGI_OK)
1214 goto done;
1215 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1216 KATTR_ID, "description", KATTR__MAX);
1217 if (kerr != KCGI_OK)
1218 goto done;
1219 kerr = khtml_puts(gw_trans->gw_html_req,
1220 gw_trans->gw_dir->description);
1221 if (kerr != KCGI_OK)
1222 goto done;
1223 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1224 if (kerr != KCGI_OK)
1225 goto done;
1228 if (gw_trans->gw_conf->got_show_repo_owner &&
1229 gw_trans->gw_dir->owner != NULL &&
1230 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
1231 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1232 KATTR_ID, "repo_owner_title", KATTR__MAX);
1233 if (kerr != KCGI_OK)
1234 goto done;
1235 kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1236 if (kerr != KCGI_OK)
1237 goto done;
1238 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1239 if (kerr != KCGI_OK)
1240 goto done;
1241 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1242 KATTR_ID, "repo_owner", KATTR__MAX);
1243 if (kerr != KCGI_OK)
1244 goto done;
1245 kerr = khtml_puts(gw_trans->gw_html_req,
1246 gw_trans->gw_dir->owner);
1247 if (kerr != KCGI_OK)
1248 goto done;
1249 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1250 if (kerr != KCGI_OK)
1251 goto done;
1254 if (gw_trans->gw_conf->got_show_repo_age) {
1255 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1256 "refs/heads", TM_LONG);
1257 if (error)
1258 goto done;
1259 if (age != NULL) {
1260 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1261 KATTR_ID, "last_change_title", KATTR__MAX);
1262 if (kerr != KCGI_OK)
1263 goto done;
1264 kerr = khtml_puts(gw_trans->gw_html_req,
1265 "Last Change: ");
1266 if (kerr != KCGI_OK)
1267 goto done;
1268 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1269 if (kerr != KCGI_OK)
1270 goto done;
1271 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1272 KATTR_ID, "last_change", KATTR__MAX);
1273 if (kerr != KCGI_OK)
1274 goto done;
1275 kerr = khtml_puts(gw_trans->gw_html_req, age);
1276 if (kerr != KCGI_OK)
1277 goto done;
1278 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1279 if (kerr != KCGI_OK)
1280 goto done;
1284 if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1285 gw_trans->gw_dir->url != NULL &&
1286 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1287 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1288 KATTR_ID, "cloneurl_title", KATTR__MAX);
1289 if (kerr != KCGI_OK)
1290 goto done;
1291 kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1292 if (kerr != KCGI_OK)
1293 goto done;
1294 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1295 if (kerr != KCGI_OK)
1296 goto done;
1297 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1298 KATTR_ID, "cloneurl", KATTR__MAX);
1299 if (kerr != KCGI_OK)
1300 goto done;
1301 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1302 if (kerr != KCGI_OK)
1303 goto done;
1304 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1305 if (kerr != KCGI_OK)
1306 goto done;
1309 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1310 if (kerr != KCGI_OK)
1311 goto done;
1313 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1314 "briefs_title_wrapper", KATTR__MAX);
1315 if (kerr != KCGI_OK)
1316 goto done;
1317 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1318 "briefs_title", KATTR__MAX);
1319 if (kerr != KCGI_OK)
1320 goto done;
1321 kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1322 if (kerr != KCGI_OK)
1323 goto done;
1324 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1325 if (kerr != KCGI_OK)
1326 goto done;
1327 error = gw_briefs(gw_trans);
1328 if (error)
1329 goto done;
1331 error = gw_output_repo_tags(gw_trans, NULL, D_MAXSLCOMMDISP,
1332 TAGBRIEF);
1333 if (error)
1334 goto done;
1336 error = gw_output_repo_heads(gw_trans);
1337 done:
1338 free(age);
1339 if (error == NULL && kerr != KCGI_OK)
1340 error = gw_kcgi_error(kerr);
1341 return error;
1344 static const struct got_error *
1345 gw_tree(struct gw_trans *gw_trans)
1347 const struct got_error *error = NULL;
1348 struct gw_header *header = NULL;
1349 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1350 char *age = NULL;
1351 enum kcgi_err kerr;
1353 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1354 return got_error_from_errno("pledge");
1356 if ((header = gw_init_header()) == NULL)
1357 return got_error_from_errno("malloc");
1359 error = gw_apply_unveil(gw_trans->gw_dir->path);
1360 if (error)
1361 goto done;
1363 error = gw_get_header(gw_trans, header, 1);
1364 if (error)
1365 goto done;
1367 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1368 "tree_header_wrapper", KATTR__MAX);
1369 if (kerr != KCGI_OK)
1370 goto done;
1371 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1372 "tree_header", KATTR__MAX);
1373 if (kerr != KCGI_OK)
1374 goto done;
1375 error = gw_gen_tree_header(gw_trans, header->tree_id);
1376 if (error)
1377 goto done;
1378 error = gw_get_time_str(&age, header->committer_time,
1379 TM_LONG);
1380 if (error)
1381 goto done;
1382 error = gw_gen_age_header(gw_trans, age ?age : "");
1383 if (error)
1384 goto done;
1385 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1386 if (error)
1387 goto done;
1388 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1389 if (kerr != KCGI_OK)
1390 goto done;
1391 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1392 "dotted_line", KATTR__MAX);
1393 if (kerr != KCGI_OK)
1394 goto done;
1395 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1396 if (kerr != KCGI_OK)
1397 goto done;
1399 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1400 "tree", KATTR__MAX);
1401 if (kerr != KCGI_OK)
1402 goto done;
1403 error = gw_output_repo_tree(gw_trans);
1404 if (error)
1405 goto done;
1407 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1408 done:
1409 got_ref_list_free(&header->refs);
1410 gw_free_headers(header);
1411 free(tree_html_disp);
1412 free(tree_html);
1413 free(tree);
1414 free(age);
1415 if (error == NULL && kerr != KCGI_OK)
1416 error = gw_kcgi_error(kerr);
1417 return error;
1420 static const struct got_error *
1421 gw_tag(struct gw_trans *gw_trans)
1423 const struct got_error *error = NULL;
1424 struct gw_header *header = NULL;
1425 enum kcgi_err kerr;
1427 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1428 return got_error_from_errno("pledge");
1430 if ((header = gw_init_header()) == NULL)
1431 return got_error_from_errno("malloc");
1433 error = gw_apply_unveil(gw_trans->gw_dir->path);
1434 if (error)
1435 goto done;
1437 error = gw_get_header(gw_trans, header, 1);
1438 if (error)
1439 goto done;
1441 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1442 "tag_header_wrapper", KATTR__MAX);
1443 if (kerr != KCGI_OK)
1444 goto done;
1445 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1446 "tag_header", KATTR__MAX);
1447 if (kerr != KCGI_OK)
1448 goto done;
1449 error = gw_gen_commit_header(gw_trans, header->commit_id,
1450 header->refs_str);
1451 if (error)
1452 goto done;
1453 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1454 if (error)
1455 goto done;
1456 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1457 if (kerr != KCGI_OK)
1458 goto done;
1459 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1460 "dotted_line", KATTR__MAX);
1461 if (kerr != KCGI_OK)
1462 goto done;
1463 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1464 if (kerr != KCGI_OK)
1465 goto done;
1467 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1468 "tree", KATTR__MAX);
1469 if (kerr != KCGI_OK)
1470 goto done;
1472 error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1473 if (error)
1474 goto done;
1476 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1477 done:
1478 got_ref_list_free(&header->refs);
1479 gw_free_headers(header);
1480 if (error == NULL && kerr != KCGI_OK)
1481 error = gw_kcgi_error(kerr);
1482 return error;
1485 static const struct got_error *
1486 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1488 const struct got_error *error = NULL;
1489 DIR *dt;
1490 char *dir_test;
1491 int opened = 0;
1493 if (asprintf(&dir_test, "%s/%s/%s",
1494 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1495 GOTWEB_GIT_DIR) == -1)
1496 return got_error_from_errno("asprintf");
1498 dt = opendir(dir_test);
1499 if (dt == NULL) {
1500 free(dir_test);
1501 } else {
1502 gw_dir->path = strdup(dir_test);
1503 if (gw_dir->path == NULL) {
1504 opened = 1;
1505 error = got_error_from_errno("strdup");
1506 goto errored;
1508 opened = 1;
1509 goto done;
1512 if (asprintf(&dir_test, "%s/%s/%s",
1513 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1514 GOTWEB_GOT_DIR) == -1) {
1515 dir_test = NULL;
1516 error = got_error_from_errno("asprintf");
1517 goto errored;
1520 dt = opendir(dir_test);
1521 if (dt == NULL)
1522 free(dir_test);
1523 else {
1524 opened = 1;
1525 error = got_error(GOT_ERR_NOT_GIT_REPO);
1526 goto errored;
1529 if (asprintf(&dir_test, "%s/%s",
1530 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1) {
1531 error = got_error_from_errno("asprintf");
1532 dir_test = NULL;
1533 goto errored;
1536 gw_dir->path = strdup(dir_test);
1537 if (gw_dir->path == NULL) {
1538 opened = 1;
1539 error = got_error_from_errno("strdup");
1540 goto errored;
1542 done:
1543 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1544 gw_dir->path);
1545 if (error)
1546 goto errored;
1547 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1548 if (error)
1549 goto errored;
1550 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1551 "refs/heads", TM_DIFF);
1552 if (error)
1553 goto errored;
1554 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1555 errored:
1556 free(dir_test);
1557 if (opened)
1558 closedir(dt);
1559 return error;
1562 static const struct got_error *
1563 gw_load_got_paths(struct gw_trans *gw_trans)
1565 const struct got_error *error = NULL;
1566 DIR *d;
1567 struct dirent **sd_dent;
1568 struct gw_dir *gw_dir;
1569 struct stat st;
1570 unsigned int d_cnt, d_i;
1572 d = opendir(gw_trans->gw_conf->got_repos_path);
1573 if (d == NULL) {
1574 error = got_error_from_errno2("opendir",
1575 gw_trans->gw_conf->got_repos_path);
1576 return error;
1579 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1580 alphasort);
1581 if (d_cnt == -1) {
1582 error = got_error_from_errno2("scandir",
1583 gw_trans->gw_conf->got_repos_path);
1584 return error;
1587 for (d_i = 0; d_i < d_cnt; d_i++) {
1588 if (gw_trans->gw_conf->got_max_repos > 0 &&
1589 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1590 break; /* account for parent and self */
1592 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1593 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1594 continue;
1596 error = gw_init_gw_dir(&gw_dir, sd_dent[d_i]->d_name);
1597 if (error)
1598 return error;
1600 error = gw_load_got_path(gw_trans, gw_dir);
1601 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1602 error = NULL;
1603 continue;
1605 else if (error)
1606 return error;
1608 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1609 !got_path_dir_is_empty(gw_dir->path)) {
1610 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1611 entry);
1612 gw_trans->repos_total++;
1616 closedir(d);
1617 return error;
1620 static const struct got_error *
1621 gw_parse_querystring(struct gw_trans *gw_trans)
1623 const struct got_error *error = NULL;
1624 struct kpair *p;
1625 struct gw_query_action *action = NULL;
1626 unsigned int i;
1628 if (gw_trans->gw_req->fieldnmap[0]) {
1629 error = got_error_from_errno("bad parse");
1630 return error;
1631 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1632 /* define gw_trans->repo_path */
1633 gw_trans->repo_name = p->parsed.s;
1635 if (asprintf(&gw_trans->repo_path, "%s/%s",
1636 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
1637 return got_error_from_errno("asprintf");
1639 /* get action and set function */
1640 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION])) {
1641 for (i = 0; i < nitems(gw_query_funcs); i++) {
1642 action = &gw_query_funcs[i];
1643 if (action->func_name == NULL)
1644 continue;
1645 if (strcmp(action->func_name,
1646 p->parsed.s) == 0) {
1647 gw_trans->action = i;
1648 break;
1651 if (gw_trans->action == -1) {
1652 gw_trans->action = GW_ERR;
1653 gw_trans->error = got_error_from_errno("bad action");
1657 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID])) {
1658 if (asprintf(&gw_trans->commit, "%s",
1659 p->parsed.s) == -1)
1660 return got_error_from_errno("asprintf");
1663 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1664 gw_trans->repo_file = p->parsed.s;
1666 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER])) {
1667 if (asprintf(&gw_trans->repo_folder, "%s",
1668 p->parsed.s) == -1)
1669 return got_error_from_errno("asprintf");
1672 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF])) {
1673 if (asprintf(&gw_trans->headref, "%s",
1674 p->parsed.s) == -1)
1675 return got_error_from_errno("asprintf");
1678 error = gw_init_gw_dir(&gw_trans->gw_dir, gw_trans->repo_name);
1679 if (error)
1680 return error;
1682 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1683 if (error)
1684 return error;
1685 } else
1686 gw_trans->action = GW_INDEX;
1688 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1689 gw_trans->page = p->parsed.i;
1691 return error;
1694 static const struct got_error *
1695 gw_init_gw_dir(struct gw_dir **gw_dir, const char *dir)
1697 const struct got_error *error;
1699 *gw_dir = malloc(sizeof(**gw_dir));
1700 if (*gw_dir == NULL)
1701 return got_error_from_errno("malloc");
1703 if (asprintf(&(*gw_dir)->name, "%s", dir) == -1) {
1704 error = got_error_from_errno("asprintf");
1705 free(*gw_dir);
1706 *gw_dir = NULL;
1707 return NULL;
1710 return NULL;
1713 static const struct got_error *
1714 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1716 enum kcgi_err kerr;
1718 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1719 if (kerr != KCGI_OK)
1720 return gw_kcgi_error(kerr);
1721 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1722 khttps[code]);
1723 if (kerr != KCGI_OK)
1724 return gw_kcgi_error(kerr);
1725 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1726 kmimetypes[mime]);
1727 if (kerr != KCGI_OK)
1728 return gw_kcgi_error(kerr);
1729 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
1730 "nosniff");
1731 if (kerr != KCGI_OK)
1732 return gw_kcgi_error(kerr);
1733 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1734 if (kerr != KCGI_OK)
1735 return gw_kcgi_error(kerr);
1736 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
1737 "1; mode=block");
1738 if (kerr != KCGI_OK)
1739 return gw_kcgi_error(kerr);
1741 /* XXX repo_file could be NULL if not present in querystring */
1742 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
1743 kerr = khttp_head(gw_trans->gw_req,
1744 kresps[KRESP_CONTENT_DISPOSITION],
1745 "attachment; filename=%s", gw_trans->repo_file);
1746 if (kerr != KCGI_OK)
1747 return gw_kcgi_error(kerr);
1750 kerr = khttp_body(gw_trans->gw_req);
1751 return gw_kcgi_error(kerr);
1754 static const struct got_error *
1755 gw_display_index(struct gw_trans *gw_trans)
1757 const struct got_error *error;
1758 enum kcgi_err kerr;
1760 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1761 if (error)
1762 return error;
1764 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1765 if (kerr != KCGI_OK)
1766 return gw_kcgi_error(kerr);
1768 if (gw_trans->action != GW_BLOB) {
1769 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1770 gw_query_funcs[gw_trans->action].template);
1771 if (kerr != KCGI_OK) {
1772 khtml_close(gw_trans->gw_html_req);
1773 return gw_kcgi_error(kerr);
1777 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
1780 static const struct got_error *
1781 gw_error(struct gw_trans *gw_trans)
1783 enum kcgi_err kerr;
1785 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->error->msg);
1787 return gw_kcgi_error(kerr);
1790 static int
1791 gw_template(size_t key, void *arg)
1793 const struct got_error *error = NULL;
1794 enum kcgi_err kerr;
1795 struct gw_trans *gw_trans = arg;
1796 char *img_src = NULL;
1798 switch (key) {
1799 case (TEMPL_HEAD):
1800 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1801 KATTR_NAME, "viewport",
1802 KATTR_CONTENT, "initial-scale=.75, user-scalable=yes",
1803 KATTR__MAX);
1804 if (kerr != KCGI_OK)
1805 return 0;
1806 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1807 if (kerr != KCGI_OK)
1808 return 0;
1809 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1810 KATTR_CHARSET, "utf-8",
1811 KATTR__MAX);
1812 if (kerr != KCGI_OK)
1813 return 0;
1814 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1815 if (kerr != KCGI_OK)
1816 return 0;
1817 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1818 KATTR_NAME, "msapplication-TileColor",
1819 KATTR_CONTENT, "#da532c", KATTR__MAX);
1820 if (kerr != KCGI_OK)
1821 return 0;
1822 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1823 if (kerr != KCGI_OK)
1824 return 0;
1825 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1826 KATTR_NAME, "theme-color",
1827 KATTR_CONTENT, "#ffffff", KATTR__MAX);
1828 if (kerr != KCGI_OK)
1829 return 0;
1830 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1831 if (kerr != KCGI_OK)
1832 return 0;
1833 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1834 KATTR_REL, "apple-touch-icon", KATTR_SIZES, "180x180",
1835 KATTR_HREF, "/apple-touch-icon.png", KATTR__MAX);
1836 if (kerr != KCGI_OK)
1837 return 0;
1838 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1839 if (kerr != KCGI_OK)
1840 return 0;
1841 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1842 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
1843 "32x32", KATTR_HREF, "/favicon-32x32.png", KATTR__MAX);
1844 if (kerr != KCGI_OK)
1845 return 0;
1846 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1847 if (kerr != KCGI_OK)
1848 return 0;
1849 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1850 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
1851 "16x16", KATTR_HREF, "/favicon-16x16.png", KATTR__MAX);
1852 if (kerr != KCGI_OK)
1853 return 0;
1854 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1855 if (kerr != KCGI_OK)
1856 return 0;
1857 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1858 KATTR_REL, "manifest", KATTR_HREF, "/site.webmanifest",
1859 KATTR__MAX);
1860 if (kerr != KCGI_OK)
1861 return 0;
1862 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1863 if (kerr != KCGI_OK)
1864 return 0;
1865 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1866 KATTR_REL, "mask-icon", KATTR_HREF,
1867 "/safari-pinned-tab.svg", KATTR__MAX);
1868 if (kerr != KCGI_OK)
1869 return 0;
1870 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1871 if (kerr != KCGI_OK)
1872 return 0;
1873 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1874 KATTR_REL, "stylesheet", KATTR_TYPE, "text/css",
1875 KATTR_HREF, "/gotweb.css", KATTR__MAX);
1876 if (kerr != KCGI_OK)
1877 return 0;
1878 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1879 if (kerr != KCGI_OK)
1880 return 0;
1881 break;
1882 case(TEMPL_HEADER):
1883 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1884 KATTR_ID, "got_link", KATTR__MAX);
1885 if (kerr != KCGI_OK)
1886 return 0;
1887 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1888 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
1889 KATTR_TARGET, "_sotd", KATTR__MAX);
1890 if (kerr != KCGI_OK)
1891 return 0;
1892 if (asprintf(&img_src, "/%s",
1893 gw_trans->gw_conf->got_logo) == -1)
1894 return 0;
1895 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
1896 KATTR_SRC, img_src, KATTR__MAX);
1897 if (kerr != KCGI_OK) {
1898 free(img_src);
1899 return 0;
1901 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1902 if (kerr != KCGI_OK) {
1903 free(img_src);
1904 return 0;
1906 break;
1907 case (TEMPL_SITEPATH):
1908 error = gw_output_site_link(gw_trans);
1909 if (error)
1910 return 0;
1911 break;
1912 case(TEMPL_TITLE):
1913 if (gw_trans->gw_conf->got_site_name != NULL) {
1914 kerr = khtml_puts(gw_trans->gw_html_req,
1915 gw_trans->gw_conf->got_site_name);
1916 if (kerr != KCGI_OK)
1917 return 0;
1919 break;
1920 case (TEMPL_SEARCH):
1921 break;
1922 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1923 "search", KATTR__MAX);
1924 if (kerr != KCGI_OK)
1925 return 0;
1926 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
1927 KATTR_METHOD, "POST", KATTR__MAX);
1928 if (kerr != KCGI_OK)
1929 return 0;
1930 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
1931 "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
1932 KATTR_MAXLENGTH, "50", KATTR__MAX);
1933 if (kerr != KCGI_OK)
1934 return 0;
1935 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
1936 KATTR__MAX);
1937 if (kerr != KCGI_OK)
1938 return 0;
1939 kerr = khtml_puts(gw_trans->gw_html_req, "Search");
1940 if (kerr != KCGI_OK)
1941 return 0;
1942 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
1943 if (kerr != KCGI_OK)
1944 return 0;
1945 break;
1946 case(TEMPL_SITEOWNER):
1947 if (gw_trans->gw_conf->got_site_owner != NULL &&
1948 gw_trans->gw_conf->got_show_site_owner) {
1949 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1950 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
1951 if (kerr != KCGI_OK)
1952 return 0;
1953 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1954 KATTR_ID, "site_owner", KATTR__MAX);
1955 if (kerr != KCGI_OK)
1956 return 0;
1957 kerr = khtml_puts(gw_trans->gw_html_req,
1958 gw_trans->gw_conf->got_site_owner);
1959 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1960 if (kerr != KCGI_OK)
1961 return 0;
1963 break;
1964 case(TEMPL_CONTENT):
1965 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1966 if (error) {
1967 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1968 KATTR_ID, "tmpl_err", KATTR__MAX);
1969 if (kerr != KCGI_OK)
1970 return 0;
1971 kerr = khttp_puts(gw_trans->gw_req, "Error: ");
1972 if (kerr != KCGI_OK)
1973 return 0;
1974 kerr = khttp_puts(gw_trans->gw_req, error->msg);
1975 if (kerr != KCGI_OK)
1976 return 0;
1977 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1978 if (kerr != KCGI_OK)
1979 return 0;
1981 break;
1982 default:
1983 return 0;
1985 return 1;
1988 static const struct got_error *
1989 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
1991 const struct got_error *error = NULL;
1992 enum kcgi_err kerr = KCGI_OK;
1994 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1995 KATTR_ID, "header_commit_title", KATTR__MAX);
1996 if (kerr != KCGI_OK)
1997 goto done;
1998 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
1999 if (kerr != KCGI_OK)
2000 goto done;
2001 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2002 if (kerr != KCGI_OK)
2003 goto done;
2004 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2005 KATTR_ID, "header_commit", KATTR__MAX);
2006 if (kerr != KCGI_OK)
2007 goto done;
2008 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2009 if (kerr != KCGI_OK)
2010 goto done;
2011 kerr = khtml_puts(gw_trans->gw_html_req, " ");
2012 if (kerr != KCGI_OK)
2013 goto done;
2014 if (str2 != NULL) {
2015 kerr = khtml_puts(gw_trans->gw_html_req, "(");
2016 if (kerr != KCGI_OK)
2017 goto done;
2018 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2019 if (kerr != KCGI_OK)
2020 goto done;
2021 kerr = khtml_puts(gw_trans->gw_html_req, ")");
2022 if (kerr != KCGI_OK)
2023 goto done;
2025 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2026 done:
2027 if (error == NULL && kerr != KCGI_OK)
2028 error = gw_kcgi_error(kerr);
2029 return error;
2032 static const struct got_error *
2033 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2035 const struct got_error *error = NULL;
2036 enum kcgi_err kerr = KCGI_OK;
2038 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2039 KATTR_ID, "header_diff_title", KATTR__MAX);
2040 if (kerr != KCGI_OK)
2041 goto done;
2042 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2043 if (kerr != KCGI_OK)
2044 goto done;
2045 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2046 if (kerr != KCGI_OK)
2047 goto done;
2048 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2049 KATTR_ID, "header_diff", KATTR__MAX);
2050 if (kerr != KCGI_OK)
2051 goto done;
2052 if (str1 != NULL) {
2053 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2054 if (kerr != KCGI_OK)
2055 goto done;
2057 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2058 if (kerr != KCGI_OK)
2059 goto done;
2060 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2061 if (kerr != KCGI_OK)
2062 goto done;
2063 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2064 done:
2065 if (error == NULL && kerr != KCGI_OK)
2066 error = gw_kcgi_error(kerr);
2067 return error;
2070 static const struct got_error *
2071 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2073 const struct got_error *error = NULL;
2074 enum kcgi_err kerr = KCGI_OK;
2076 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2077 KATTR_ID, "header_age_title", KATTR__MAX);
2078 if (kerr != KCGI_OK)
2079 goto done;
2080 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2081 if (kerr != KCGI_OK)
2082 goto done;
2083 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2084 if (kerr != KCGI_OK)
2085 goto done;
2086 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2087 KATTR_ID, "header_age", KATTR__MAX);
2088 if (kerr != KCGI_OK)
2089 goto done;
2090 kerr = khtml_puts(gw_trans->gw_html_req, str);
2091 if (kerr != KCGI_OK)
2092 goto done;
2093 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2094 done:
2095 if (error == NULL && kerr != KCGI_OK)
2096 error = gw_kcgi_error(kerr);
2097 return error;
2100 static const struct got_error *
2101 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2103 const struct got_error *error = NULL;
2104 enum kcgi_err kerr;
2106 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2107 KATTR_ID, "header_author_title", KATTR__MAX);
2108 if (kerr != KCGI_OK)
2109 goto done;
2110 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2111 if (kerr != KCGI_OK)
2112 goto done;
2113 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2114 if (kerr != KCGI_OK)
2115 goto done;
2116 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2117 KATTR_ID, "header_author", KATTR__MAX);
2118 if (kerr != KCGI_OK)
2119 goto done;
2120 kerr = khtml_puts(gw_trans->gw_html_req, str);
2121 if (kerr != KCGI_OK)
2122 goto done;
2123 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2124 done:
2125 if (error == NULL && kerr != KCGI_OK)
2126 error = gw_kcgi_error(kerr);
2127 return error;
2130 static const struct got_error *
2131 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2133 const struct got_error *error = NULL;
2134 enum kcgi_err kerr;
2136 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2137 KATTR_ID, "header_committer_title", KATTR__MAX);
2138 if (kerr != KCGI_OK)
2139 goto done;
2140 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2141 if (kerr != KCGI_OK)
2142 goto done;
2143 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2144 if (kerr != KCGI_OK)
2145 goto done;
2146 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2147 KATTR_ID, "header_committer", KATTR__MAX);
2148 if (kerr != KCGI_OK)
2149 goto done;
2150 kerr = khtml_puts(gw_trans->gw_html_req, str);
2151 if (kerr != KCGI_OK)
2152 goto done;
2153 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2154 done:
2155 if (error == NULL && kerr != KCGI_OK)
2156 error = gw_kcgi_error(kerr);
2157 return error;
2160 static const struct got_error *
2161 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2163 const struct got_error *error = NULL;
2164 enum kcgi_err kerr;
2166 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2167 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2168 if (kerr != KCGI_OK)
2169 goto done;
2170 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2171 if (kerr != KCGI_OK)
2172 goto done;
2173 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2174 if (kerr != KCGI_OK)
2175 goto done;
2176 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2177 KATTR_ID, "header_commit_msg", KATTR__MAX);
2178 if (kerr != KCGI_OK)
2179 goto done;
2180 kerr = khttp_puts(gw_trans->gw_req, str);
2181 if (kerr != KCGI_OK)
2182 goto done;
2183 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2184 done:
2185 if (error == NULL && kerr != KCGI_OK)
2186 error = gw_kcgi_error(kerr);
2187 return error;
2190 static const struct got_error *
2191 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2193 const struct got_error *error = NULL;
2194 enum kcgi_err kerr;
2196 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2197 KATTR_ID, "header_tree_title", KATTR__MAX);
2198 if (kerr != KCGI_OK)
2199 goto done;
2200 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2201 if (kerr != KCGI_OK)
2202 goto done;
2203 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2204 if (kerr != KCGI_OK)
2205 goto done;
2206 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2207 KATTR_ID, "header_tree", KATTR__MAX);
2208 if (kerr != KCGI_OK)
2209 goto done;
2210 kerr = khtml_puts(gw_trans->gw_html_req, str);
2211 if (kerr != KCGI_OK)
2212 goto done;
2213 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2214 done:
2215 if (error == NULL && kerr != KCGI_OK)
2216 error = gw_kcgi_error(kerr);
2217 return error;
2220 static const struct got_error *
2221 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2222 char *dir)
2224 const struct got_error *error = NULL;
2225 FILE *f = NULL;
2226 char *d_file = NULL;
2227 unsigned int len;
2228 size_t n;
2230 *description = NULL;
2231 if (gw_trans->gw_conf->got_show_repo_description == 0)
2232 return NULL;
2234 if (asprintf(&d_file, "%s/description", dir) == -1)
2235 return got_error_from_errno("asprintf");
2237 f = fopen(d_file, "r");
2238 if (f == NULL) {
2239 if (errno == ENOENT || errno == EACCES)
2240 return NULL;
2241 error = got_error_from_errno2("fopen", d_file);
2242 goto done;
2245 if (fseek(f, 0, SEEK_END) == -1) {
2246 error = got_ferror(f, GOT_ERR_IO);
2247 goto done;
2249 len = ftell(f);
2250 if (len == -1) {
2251 error = got_ferror(f, GOT_ERR_IO);
2252 goto done;
2254 if (fseek(f, 0, SEEK_SET) == -1) {
2255 error = got_ferror(f, GOT_ERR_IO);
2256 goto done;
2258 *description = calloc(len + 1, sizeof(**description));
2259 if (*description == NULL) {
2260 error = got_error_from_errno("calloc");
2261 goto done;
2264 n = fread(*description, 1, len, f);
2265 if (n == 0 && ferror(f))
2266 error = got_ferror(f, GOT_ERR_IO);
2267 done:
2268 if (f != NULL && fclose(f) == -1 && error == NULL)
2269 error = got_error_from_errno("fclose");
2270 free(d_file);
2271 return error;
2274 static const struct got_error *
2275 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2277 struct tm tm;
2278 time_t diff_time;
2279 char *years = "years ago", *months = "months ago";
2280 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2281 char *minutes = "minutes ago", *seconds = "seconds ago";
2282 char *now = "right now";
2283 char *s;
2284 char datebuf[29];
2286 *repo_age = NULL;
2288 switch (ref_tm) {
2289 case TM_DIFF:
2290 diff_time = time(NULL) - committer_time;
2291 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2292 if (asprintf(repo_age, "%lld %s",
2293 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2294 return got_error_from_errno("asprintf");
2295 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2296 if (asprintf(repo_age, "%lld %s",
2297 (diff_time / 60 / 60 / 24 / (365 / 12)),
2298 months) == -1)
2299 return got_error_from_errno("asprintf");
2300 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2301 if (asprintf(repo_age, "%lld %s",
2302 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2303 return got_error_from_errno("asprintf");
2304 } else if (diff_time > 60 * 60 * 24 * 2) {
2305 if (asprintf(repo_age, "%lld %s",
2306 (diff_time / 60 / 60 / 24), days) == -1)
2307 return got_error_from_errno("asprintf");
2308 } else if (diff_time > 60 * 60 * 2) {
2309 if (asprintf(repo_age, "%lld %s",
2310 (diff_time / 60 / 60), hours) == -1)
2311 return got_error_from_errno("asprintf");
2312 } else if (diff_time > 60 * 2) {
2313 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2314 minutes) == -1)
2315 return got_error_from_errno("asprintf");
2316 } else if (diff_time > 2) {
2317 if (asprintf(repo_age, "%lld %s", diff_time,
2318 seconds) == -1)
2319 return got_error_from_errno("asprintf");
2320 } else {
2321 if (asprintf(repo_age, "%s", now) == -1)
2322 return got_error_from_errno("asprintf");
2324 break;
2325 case TM_LONG:
2326 if (gmtime_r(&committer_time, &tm) == NULL)
2327 return got_error_from_errno("gmtime_r");
2329 s = asctime_r(&tm, datebuf);
2330 if (s == NULL)
2331 return got_error_from_errno("asctime_r");
2333 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2334 return got_error_from_errno("asprintf");
2335 break;
2337 return NULL;
2340 static const struct got_error *
2341 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2342 char *repo_ref, int ref_tm)
2344 const struct got_error *error = NULL;
2345 struct got_object_id *id = NULL;
2346 struct got_repository *repo = NULL;
2347 struct got_commit_object *commit = NULL;
2348 struct got_reflist_head refs;
2349 struct got_reflist_entry *re;
2350 struct got_reference *head_ref;
2351 int is_head = 0;
2352 time_t committer_time = 0, cmp_time = 0;
2353 const char *refname;
2355 *repo_age = NULL;
2356 SIMPLEQ_INIT(&refs);
2358 if (repo_ref == NULL)
2359 return NULL;
2361 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
2362 is_head = 1;
2364 if (gw_trans->gw_conf->got_show_repo_age == 0)
2365 return NULL;
2367 error = got_repo_open(&repo, dir, NULL);
2368 if (error)
2369 goto done;
2371 if (is_head)
2372 error = got_ref_list(&refs, repo, "refs/heads",
2373 got_ref_cmp_by_name, NULL);
2374 else
2375 error = got_ref_list(&refs, repo, repo_ref,
2376 got_ref_cmp_by_name, NULL);
2377 if (error)
2378 goto done;
2380 SIMPLEQ_FOREACH(re, &refs, entry) {
2381 if (is_head) {
2382 refname = strdup(repo_ref);
2383 if (refname == NULL) {
2384 error = got_error_from_errno("strdup");
2385 goto done;
2387 } else {
2388 refname = got_ref_get_name(re->ref);
2389 if (refname == NULL) {
2390 error = got_error_from_errno("strdup");
2391 goto done;
2394 error = got_ref_open(&head_ref, repo, refname, 0);
2395 if (error)
2396 goto done;
2398 error = got_ref_resolve(&id, repo, head_ref);
2399 got_ref_close(head_ref);
2400 if (error)
2401 goto done;
2403 error = got_object_open_as_commit(&commit, repo, id);
2404 if (error)
2405 goto done;
2407 committer_time =
2408 got_object_commit_get_committer_time(commit);
2410 if (cmp_time < committer_time)
2411 cmp_time = committer_time;
2414 if (cmp_time != 0) {
2415 committer_time = cmp_time;
2416 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2418 done:
2419 got_ref_list_free(&refs);
2420 free(id);
2421 return error;
2424 static const struct got_error *
2425 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2427 const struct got_error *error;
2428 FILE *f = NULL;
2429 struct got_object_id *id1 = NULL, *id2 = NULL;
2430 char *label1 = NULL, *label2 = NULL, *line = NULL;
2431 int obj_type;
2432 size_t linesize = 0;
2433 ssize_t linelen;
2434 enum kcgi_err kerr = KCGI_OK;
2436 f = got_opentemp();
2437 if (f == NULL)
2438 return NULL;
2440 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
2441 if (error)
2442 goto done;
2444 if (header->parent_id != NULL &&
2445 strncmp(header->parent_id, "/dev/null", 9) != 0) {
2446 error = got_repo_match_object_id(&id1, &label1,
2447 header->parent_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2448 if (error)
2449 goto done;
2452 error = got_repo_match_object_id(&id2, &label2,
2453 header->commit_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2454 if (error)
2455 goto done;
2457 error = got_object_get_type(&obj_type, header->repo, id2);
2458 if (error)
2459 goto done;
2460 switch (obj_type) {
2461 case GOT_OBJ_TYPE_BLOB:
2462 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
2463 header->repo, f);
2464 break;
2465 case GOT_OBJ_TYPE_TREE:
2466 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
2467 header->repo, f);
2468 break;
2469 case GOT_OBJ_TYPE_COMMIT:
2470 error = got_diff_objects_as_commits(id1, id2, 3, 0,
2471 header->repo, f);
2472 break;
2473 default:
2474 error = got_error(GOT_ERR_OBJ_TYPE);
2476 if (error)
2477 goto done;
2479 if (fseek(f, 0, SEEK_SET) == -1) {
2480 error = got_ferror(f, GOT_ERR_IO);
2481 goto done;
2484 while ((linelen = getline(&line, &linesize, f)) != -1) {
2485 error = gw_colordiff_line(gw_trans, line);
2486 if (error)
2487 goto done;
2488 /* XXX: KHTML_PRETTY breaks this */
2489 kerr = khtml_puts(gw_trans->gw_html_req, line);
2490 if (kerr != KCGI_OK)
2491 goto done;
2492 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2493 if (kerr != KCGI_OK)
2494 goto done;
2496 if (linelen == -1 && ferror(f))
2497 error = got_error_from_errno("getline");
2498 done:
2499 if (f && fclose(f) == -1 && error == NULL)
2500 error = got_error_from_errno("fclose");
2501 free(line);
2502 free(label1);
2503 free(label2);
2504 free(id1);
2505 free(id2);
2507 if (error == NULL && kerr != KCGI_OK)
2508 error = gw_kcgi_error(kerr);
2509 return error;
2512 static const struct got_error *
2513 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2515 const struct got_error *error = NULL;
2516 struct got_repository *repo;
2517 const char *gitconfig_owner;
2519 *owner = NULL;
2521 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2522 return NULL;
2524 error = got_repo_open(&repo, dir, NULL);
2525 if (error)
2526 return error;
2527 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2528 if (gitconfig_owner) {
2529 *owner = strdup(gitconfig_owner);
2530 if (*owner == NULL)
2531 error = got_error_from_errno("strdup");
2533 got_repo_close(repo);
2534 return error;
2537 static const struct got_error *
2538 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2540 const struct got_error *error = NULL;
2541 FILE *f;
2542 char *d_file = NULL;
2543 unsigned int len;
2544 size_t n;
2546 *url = NULL;
2548 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2549 return got_error_from_errno("asprintf");
2551 f = fopen(d_file, "r");
2552 if (f == NULL) {
2553 if (errno != ENOENT && errno != EACCES)
2554 error = got_error_from_errno2("fopen", d_file);
2555 goto done;
2558 if (fseek(f, 0, SEEK_END) == -1) {
2559 error = got_ferror(f, GOT_ERR_IO);
2560 goto done;
2562 len = ftell(f);
2563 if (len == -1) {
2564 error = got_ferror(f, GOT_ERR_IO);
2565 goto done;
2567 if (fseek(f, 0, SEEK_SET) == -1) {
2568 error = got_ferror(f, GOT_ERR_IO);
2569 goto done;
2572 *url = calloc(len + 1, sizeof(**url));
2573 if (*url == NULL) {
2574 error = got_error_from_errno("calloc");
2575 goto done;
2578 n = fread(*url, 1, len, f);
2579 if (n == 0 && ferror(f))
2580 error = got_ferror(f, GOT_ERR_IO);
2581 done:
2582 if (f && fclose(f) == -1 && error == NULL)
2583 error = got_error_from_errno("fclose");
2584 free(d_file);
2585 return NULL;
2588 static const struct got_error *
2589 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
2590 int limit, int tag_type)
2592 const struct got_error *error = NULL;
2593 struct got_repository *repo = NULL;
2594 struct got_reflist_head refs;
2595 struct got_reflist_entry *re;
2596 char *age = NULL;
2597 char *id_str = NULL, *newline, *href_commits = NULL;
2598 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
2599 struct got_tag_object *tag = NULL;
2600 enum kcgi_err kerr = KCGI_OK;
2601 int summary_header_displayed = 0;
2603 SIMPLEQ_INIT(&refs);
2605 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2606 if (error)
2607 return error;
2609 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
2610 if (error)
2611 goto done;
2613 SIMPLEQ_FOREACH(re, &refs, entry) {
2614 const char *refname;
2615 const char *tagger;
2616 const char *tag_commit;
2617 time_t tagger_time;
2618 struct got_object_id *id;
2619 struct got_commit_object *commit = NULL;
2621 refname = got_ref_get_name(re->ref);
2622 if (strncmp(refname, "refs/tags/", 10) != 0)
2623 continue;
2624 refname += 10;
2626 error = got_ref_resolve(&id, repo, re->ref);
2627 if (error)
2628 goto done;
2630 error = got_object_open_as_tag(&tag, repo, id);
2631 if (error) {
2632 if (error->code != GOT_ERR_OBJ_TYPE) {
2633 free(id);
2634 goto done;
2636 /* "lightweight" tag */
2637 error = got_object_open_as_commit(&commit, repo, id);
2638 if (error) {
2639 free(id);
2640 goto done;
2642 tagger = got_object_commit_get_committer(commit);
2643 tagger_time =
2644 got_object_commit_get_committer_time(commit);
2645 error = got_object_id_str(&id_str, id);
2646 free(id);
2647 } else {
2648 free(id);
2649 tagger = got_object_tag_get_tagger(tag);
2650 tagger_time = got_object_tag_get_tagger_time(tag);
2651 error = got_object_id_str(&id_str,
2652 got_object_tag_get_object_id(tag));
2654 if (error)
2655 goto done;
2657 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
2658 strlen(id_str)) != 0)
2659 continue;
2661 if (commit) {
2662 error = got_object_commit_get_logmsg(&tag_commit0,
2663 commit);
2664 if (error)
2665 goto done;
2666 got_object_commit_close(commit);
2667 } else {
2668 tag_commit0 = strdup(got_object_tag_get_message(tag));
2669 if (tag_commit0 == NULL) {
2670 error = got_error_from_errno("strdup");
2671 goto done;
2675 tag_commit = tag_commit0;
2676 while (*tag_commit == '\n')
2677 tag_commit++;
2679 switch (tag_type) {
2680 case TAGBRIEF:
2681 newline = strchr(tag_commit, '\n');
2682 if (newline)
2683 *newline = '\0';
2685 if (summary_header_displayed == 0) {
2686 kerr = khtml_attr(gw_trans->gw_html_req,
2687 KELEM_DIV, KATTR_ID,
2688 "summary_tags_title_wrapper", KATTR__MAX);
2689 if (kerr != KCGI_OK)
2690 goto done;
2691 kerr = khtml_attr(gw_trans->gw_html_req,
2692 KELEM_DIV, KATTR_ID,
2693 "summary_tags_title", KATTR__MAX);
2694 if (kerr != KCGI_OK)
2695 goto done;
2696 kerr = khtml_puts(gw_trans->gw_html_req,
2697 "Tags");
2698 if (kerr != KCGI_OK)
2699 goto done;
2700 kerr = khtml_closeelem(gw_trans->gw_html_req,
2701 2);
2702 if (kerr != KCGI_OK)
2703 goto done;
2704 kerr = khtml_attr(gw_trans->gw_html_req,
2705 KELEM_DIV, KATTR_ID,
2706 "summary_tags_content", KATTR__MAX);
2707 if (kerr != KCGI_OK)
2708 goto done;
2709 summary_header_displayed = 1;
2712 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2713 KATTR_ID, "tags_wrapper", KATTR__MAX);
2714 if (kerr != KCGI_OK)
2715 goto done;
2716 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2717 KATTR_ID, "tags_age", KATTR__MAX);
2718 if (kerr != KCGI_OK)
2719 goto done;
2720 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
2721 if (error)
2722 goto done;
2723 kerr = khtml_puts(gw_trans->gw_html_req,
2724 age ? age : "");
2725 if (kerr != KCGI_OK)
2726 goto done;
2727 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2728 if (kerr != KCGI_OK)
2729 goto done;
2730 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2731 KATTR_ID, "tags", KATTR__MAX);
2732 if (kerr != KCGI_OK)
2733 goto done;
2734 kerr = khtml_puts(gw_trans->gw_html_req, refname);
2735 if (kerr != KCGI_OK)
2736 goto done;
2737 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2738 if (kerr != KCGI_OK)
2739 goto done;
2740 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2741 KATTR_ID, "tags_name", KATTR__MAX);
2742 if (kerr != KCGI_OK)
2743 goto done;
2744 if (asprintf(&href_tag, "?path=%s&action=tag&commit=%s",
2745 gw_trans->repo_name, id_str) == -1) {
2746 error = got_error_from_errno("asprintf");
2747 goto done;
2749 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2750 KATTR_HREF, href_tag, KATTR__MAX);
2751 if (kerr != KCGI_OK)
2752 goto done;
2753 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
2754 if (kerr != KCGI_OK)
2755 goto done;
2756 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2757 if (kerr != KCGI_OK)
2758 goto done;
2760 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2761 KATTR_ID, "navs_wrapper", KATTR__MAX);
2762 if (kerr != KCGI_OK)
2763 goto done;
2764 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2765 KATTR_ID, "navs", KATTR__MAX);
2766 if (kerr != KCGI_OK)
2767 goto done;
2769 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2770 KATTR_HREF, href_tag, KATTR__MAX);
2771 if (kerr != KCGI_OK)
2772 goto done;
2773 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
2774 if (kerr != KCGI_OK)
2775 goto done;
2776 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2777 if (kerr != KCGI_OK)
2778 goto done;
2780 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2781 if (kerr != KCGI_OK)
2782 goto done;
2783 if (asprintf(&href_briefs,
2784 "?path=%s&action=briefs&commit=%s",
2785 gw_trans->repo_name, id_str) == -1) {
2786 error = got_error_from_errno("asprintf");
2787 goto done;
2789 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2790 KATTR_HREF, href_briefs, KATTR__MAX);
2791 if (kerr != KCGI_OK)
2792 goto done;
2793 kerr = khtml_puts(gw_trans->gw_html_req,
2794 "commit briefs");
2795 if (kerr != KCGI_OK)
2796 goto done;
2797 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2798 if (kerr != KCGI_OK)
2799 goto done;
2801 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2802 if (kerr != KCGI_OK)
2803 goto done;
2805 if (asprintf(&href_commits,
2806 "?path=%s&action=commits&commit=%s",
2807 gw_trans->repo_name, id_str) == -1) {
2808 error = got_error_from_errno("asprintf");
2809 goto done;
2811 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2812 KATTR_HREF, href_commits, KATTR__MAX);
2813 if (kerr != KCGI_OK)
2814 goto done;
2815 kerr = khtml_puts(gw_trans->gw_html_req,
2816 "commits");
2817 if (kerr != KCGI_OK)
2818 goto done;
2819 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2820 if (kerr != KCGI_OK)
2821 goto done;
2823 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2824 KATTR_ID, "dotted_line", KATTR__MAX);
2825 if (kerr != KCGI_OK)
2826 goto done;
2827 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2828 if (kerr != KCGI_OK)
2829 goto done;
2830 break;
2831 case TAGFULL:
2832 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2833 KATTR_ID, "tag_info_date_title", KATTR__MAX);
2834 if (kerr != KCGI_OK)
2835 goto done;
2836 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
2837 if (kerr != KCGI_OK)
2838 goto done;
2839 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2840 if (kerr != KCGI_OK)
2841 goto done;
2842 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2843 KATTR_ID, "tag_info_date", KATTR__MAX);
2844 if (kerr != KCGI_OK)
2845 goto done;
2846 error = gw_get_time_str(&age, tagger_time, TM_LONG);
2847 if (error)
2848 goto done;
2849 kerr = khtml_puts(gw_trans->gw_html_req,
2850 age ? age : "");
2851 if (kerr != KCGI_OK)
2852 goto done;
2853 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2854 if (kerr != KCGI_OK)
2855 goto done;
2857 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2858 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
2859 if (kerr != KCGI_OK)
2860 goto done;
2861 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
2862 if (kerr != KCGI_OK)
2863 goto done;
2864 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2865 if (kerr != KCGI_OK)
2866 goto done;
2867 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2868 KATTR_ID, "tag_info_date", KATTR__MAX);
2869 if (kerr != KCGI_OK)
2870 goto done;
2871 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
2872 if (kerr != KCGI_OK)
2873 goto done;
2874 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2875 if (kerr != KCGI_OK)
2876 goto done;
2878 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2879 KATTR_ID, "tag_info", KATTR__MAX);
2880 if (kerr != KCGI_OK)
2881 goto done;
2882 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
2883 if (kerr != KCGI_OK)
2884 goto done;
2885 break;
2886 default:
2887 break;
2889 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2890 if (kerr != KCGI_OK)
2891 goto done;
2893 if (limit && --limit == 0)
2894 break;
2896 if (tag)
2897 got_object_tag_close(tag);
2898 tag = NULL;
2899 free(id_str);
2900 id_str = NULL;
2901 free(age);
2902 age = NULL;
2903 free(tag_commit0);
2904 tag_commit0 = NULL;
2905 free(href_tag);
2906 href_tag = NULL;
2907 free(href_briefs);
2908 href_briefs = NULL;
2909 free(href_commits);
2910 href_commits = NULL;
2912 done:
2913 if (tag)
2914 got_object_tag_close(tag);
2915 free(id_str);
2916 free(age);
2917 free(tag_commit0);
2918 free(href_tag);
2919 free(href_briefs);
2920 free(href_commits);
2921 got_ref_list_free(&refs);
2922 if (repo)
2923 got_repo_close(repo);
2924 if (error == NULL && kerr != KCGI_OK)
2925 error = gw_kcgi_error(kerr);
2926 return error;
2929 static void
2930 gw_free_headers(struct gw_header *header)
2932 free(header->id);
2933 free(header->path);
2934 if (header->commit != NULL)
2935 got_object_commit_close(header->commit);
2936 if (header->repo)
2937 got_repo_close(header->repo);
2938 free(header->refs_str);
2939 free(header->commit_id);
2940 free(header->parent_id);
2941 free(header->tree_id);
2942 free(header->commit_msg);
2945 static struct gw_header *
2946 gw_init_header()
2948 struct gw_header *header;
2950 header = malloc(sizeof(*header));
2951 if (header == NULL)
2952 return NULL;
2954 header->repo = NULL;
2955 header->commit = NULL;
2956 header->id = NULL;
2957 header->path = NULL;
2958 SIMPLEQ_INIT(&header->refs);
2960 header->refs_str = NULL;
2961 header->commit_id = NULL;
2962 header->parent_id = NULL;
2963 header->tree_id = NULL;
2964 header->commit_msg = NULL;
2966 return header;
2969 static const struct got_error *
2970 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
2971 int limit)
2973 const struct got_error *error = NULL;
2974 struct got_commit_graph *graph = NULL;
2976 error = got_commit_graph_open(&graph, header->path, 0);
2977 if (error)
2978 return error;
2980 error = got_commit_graph_iter_start(graph, header->id, header->repo,
2981 NULL, NULL);
2982 if (error)
2983 goto done;
2985 for (;;) {
2986 error = got_commit_graph_iter_next(&header->id, graph,
2987 header->repo, NULL, NULL);
2988 if (error) {
2989 if (error->code == GOT_ERR_ITER_COMPLETED)
2990 error = NULL;
2991 goto done;
2993 if (header->id == NULL)
2994 goto done;
2996 error = got_object_open_as_commit(&header->commit, header->repo,
2997 header->id);
2998 if (error)
2999 goto done;
3001 error = gw_get_commit(gw_trans, header);
3002 if (limit > 1) {
3003 struct gw_header *n_header = NULL;
3004 if ((n_header = gw_init_header()) == NULL) {
3005 error = got_error_from_errno("malloc");
3006 goto done;
3009 if (header->refs_str != NULL) {
3010 n_header->refs_str = strdup(header->refs_str);
3011 if (n_header->refs_str == NULL) {
3012 error = got_error_from_errno("strdup");
3013 goto done;
3016 n_header->commit_id = strdup(header->commit_id);
3017 if (n_header->commit_id == NULL) {
3018 error = got_error_from_errno("strdup");
3019 goto done;
3021 if (header->parent_id != NULL) {
3022 n_header->parent_id = strdup(header->parent_id);
3023 if (n_header->parent_id == NULL) {
3024 error = got_error_from_errno("strdup");
3025 goto done;
3028 n_header->tree_id = strdup(header->tree_id);
3029 if (n_header->tree_id == NULL) {
3030 error = got_error_from_errno("strdup");
3031 goto done;
3033 n_header->author = header->author;
3034 n_header->committer = header->committer;
3035 n_header->commit_msg = strdup(header->commit_msg);
3036 if (n_header->commit_msg == NULL) {
3037 error = got_error_from_errno("strdup");
3038 goto done;
3040 n_header->committer_time = header->committer_time;
3041 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3042 entry);
3044 if (error || (limit && --limit == 0))
3045 break;
3047 done:
3048 if (graph)
3049 got_commit_graph_close(graph);
3050 return error;
3053 static const struct got_error *
3054 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
3056 const struct got_error *error = NULL;
3057 struct got_reflist_entry *re;
3058 struct got_object_id *id2 = NULL;
3059 struct got_object_qid *parent_id;
3060 char *commit_msg = NULL, *commit_msg0;
3062 /*print commit*/
3063 SIMPLEQ_FOREACH(re, &header->refs, entry) {
3064 char *s;
3065 const char *name;
3066 struct got_tag_object *tag = NULL;
3067 int cmp;
3069 name = got_ref_get_name(re->ref);
3070 if (strcmp(name, GOT_REF_HEAD) == 0)
3071 continue;
3072 if (strncmp(name, "refs/", 5) == 0)
3073 name += 5;
3074 if (strncmp(name, "got/", 4) == 0)
3075 continue;
3076 if (strncmp(name, "heads/", 6) == 0)
3077 name += 6;
3078 if (strncmp(name, "remotes/", 8) == 0)
3079 name += 8;
3080 if (strncmp(name, "tags/", 5) == 0) {
3081 error = got_object_open_as_tag(&tag, header->repo,
3082 re->id);
3083 if (error) {
3084 if (error->code != GOT_ERR_OBJ_TYPE)
3085 continue;
3087 * Ref points at something other
3088 * than a tag.
3090 error = NULL;
3091 tag = NULL;
3094 cmp = got_object_id_cmp(tag ?
3095 got_object_tag_get_object_id(tag) : re->id, header->id);
3096 if (tag)
3097 got_object_tag_close(tag);
3098 if (cmp != 0)
3099 continue;
3100 s = header->refs_str;
3101 if (asprintf(&header->refs_str, "%s%s%s", s ? s : "",
3102 s ? ", " : "", name) == -1) {
3103 error = got_error_from_errno("asprintf");
3104 free(s);
3105 header->refs_str = NULL;
3106 return error;
3108 free(s);
3111 error = got_object_id_str(&header->commit_id, header->id);
3112 if (error)
3113 return error;
3115 error = got_object_id_str(&header->tree_id,
3116 got_object_commit_get_tree_id(header->commit));
3117 if (error)
3118 return error;
3120 if (gw_trans->action == GW_DIFF) {
3121 parent_id = SIMPLEQ_FIRST(
3122 got_object_commit_get_parent_ids(header->commit));
3123 if (parent_id != NULL) {
3124 id2 = got_object_id_dup(parent_id->id);
3125 free (parent_id);
3126 error = got_object_id_str(&header->parent_id, id2);
3127 if (error)
3128 return error;
3129 free(id2);
3130 } else {
3131 header->parent_id = strdup("/dev/null");
3132 if (header->parent_id == NULL) {
3133 error = got_error_from_errno("strdup");
3134 return error;
3139 header->committer_time =
3140 got_object_commit_get_committer_time(header->commit);
3142 header->author =
3143 got_object_commit_get_author(header->commit);
3144 header->committer =
3145 got_object_commit_get_committer(header->commit);
3146 if (error)
3147 return error;
3148 error = got_object_commit_get_logmsg(&commit_msg0, header->commit);
3149 if (error)
3150 return error;
3152 commit_msg = commit_msg0;
3153 while (*commit_msg == '\n')
3154 commit_msg++;
3156 header->commit_msg = strdup(commit_msg);
3157 if (header->commit_msg == NULL)
3158 error = got_error_from_errno("strdup");
3159 free(commit_msg0);
3160 return error;
3163 static const struct got_error *
3164 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3166 const struct got_error *error = NULL;
3167 char *in_repo_path = NULL;
3169 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
3170 if (error)
3171 return error;
3173 if (gw_trans->commit == NULL) {
3174 struct got_reference *head_ref;
3175 error = got_ref_open(&head_ref, header->repo,
3176 gw_trans->headref, 0);
3177 if (error)
3178 return error;
3180 error = got_ref_resolve(&header->id, header->repo, head_ref);
3181 got_ref_close(head_ref);
3182 if (error)
3183 return error;
3185 error = got_object_open_as_commit(&header->commit,
3186 header->repo, header->id);
3187 } else {
3188 struct got_reference *ref;
3189 error = got_ref_open(&ref, header->repo, gw_trans->commit, 0);
3190 if (error == NULL) {
3191 int obj_type;
3192 error = got_ref_resolve(&header->id, header->repo, ref);
3193 got_ref_close(ref);
3194 if (error)
3195 return error;
3196 error = got_object_get_type(&obj_type, header->repo,
3197 header->id);
3198 if (error)
3199 return error;
3200 if (obj_type == GOT_OBJ_TYPE_TAG) {
3201 struct got_tag_object *tag;
3202 error = got_object_open_as_tag(&tag,
3203 header->repo, header->id);
3204 if (error)
3205 return error;
3206 if (got_object_tag_get_object_type(tag) !=
3207 GOT_OBJ_TYPE_COMMIT) {
3208 got_object_tag_close(tag);
3209 error = got_error(GOT_ERR_OBJ_TYPE);
3210 return error;
3212 free(header->id);
3213 header->id = got_object_id_dup(
3214 got_object_tag_get_object_id(tag));
3215 if (header->id == NULL)
3216 error = got_error_from_errno(
3217 "got_object_id_dup");
3218 got_object_tag_close(tag);
3219 if (error)
3220 return error;
3221 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3222 error = got_error(GOT_ERR_OBJ_TYPE);
3223 return error;
3225 error = got_object_open_as_commit(&header->commit,
3226 header->repo, header->id);
3227 if (error)
3228 return error;
3230 if (header->commit == NULL) {
3231 error = got_repo_match_object_id_prefix(&header->id,
3232 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
3233 header->repo);
3234 if (error)
3235 return error;
3237 error = got_repo_match_object_id_prefix(&header->id,
3238 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
3239 header->repo);
3242 error = got_repo_map_path(&in_repo_path, header->repo,
3243 gw_trans->repo_path, 1);
3244 if (error)
3245 return error;
3247 if (in_repo_path) {
3248 header->path = strdup(in_repo_path);
3249 if (header->path == NULL) {
3250 error = got_error_from_errno("strdup");
3251 free(in_repo_path);
3252 return error;
3255 free(in_repo_path);
3257 error = got_ref_list(&header->refs, header->repo, NULL,
3258 got_ref_cmp_by_name, NULL);
3259 if (error)
3260 return error;
3262 error = gw_get_commits(gw_trans, header, limit);
3263 return error;
3266 struct blame_line {
3267 int annotated;
3268 char *id_str;
3269 char *committer;
3270 char datebuf[11]; /* YYYY-MM-DD + NUL */
3273 struct gw_blame_cb_args {
3274 struct blame_line *lines;
3275 int nlines;
3276 int nlines_prec;
3277 int lineno_cur;
3278 off_t *line_offsets;
3279 FILE *f;
3280 struct got_repository *repo;
3281 struct gw_trans *gw_trans;
3284 static const struct got_error *
3285 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3287 const struct got_error *err = NULL;
3288 struct gw_blame_cb_args *a = arg;
3289 struct blame_line *bline;
3290 char *line = NULL;
3291 size_t linesize = 0;
3292 struct got_commit_object *commit = NULL;
3293 off_t offset;
3294 struct tm tm;
3295 time_t committer_time;
3296 enum kcgi_err kerr = KCGI_OK;
3298 if (nlines != a->nlines ||
3299 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3300 return got_error(GOT_ERR_RANGE);
3302 if (lineno == -1)
3303 return NULL; /* no change in this commit */
3305 /* Annotate this line. */
3306 bline = &a->lines[lineno - 1];
3307 if (bline->annotated)
3308 return NULL;
3309 err = got_object_id_str(&bline->id_str, id);
3310 if (err)
3311 return err;
3313 err = got_object_open_as_commit(&commit, a->repo, id);
3314 if (err)
3315 goto done;
3317 bline->committer = strdup(got_object_commit_get_committer(commit));
3318 if (bline->committer == NULL) {
3319 err = got_error_from_errno("strdup");
3320 goto done;
3323 committer_time = got_object_commit_get_committer_time(commit);
3324 if (localtime_r(&committer_time, &tm) == NULL)
3325 return got_error_from_errno("localtime_r");
3326 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3327 &tm) >= sizeof(bline->datebuf)) {
3328 err = got_error(GOT_ERR_NO_SPACE);
3329 goto done;
3331 bline->annotated = 1;
3333 /* Print lines annotated so far. */
3334 bline = &a->lines[a->lineno_cur - 1];
3335 if (!bline->annotated)
3336 goto done;
3338 offset = a->line_offsets[a->lineno_cur - 1];
3339 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3340 err = got_error_from_errno("fseeko");
3341 goto done;
3344 while (bline->annotated) {
3345 char *smallerthan, *at, *nl, *committer;
3346 char *lineno = NULL, *href_diff = NULL, *href_link = NULL;
3347 size_t len;
3349 if (getline(&line, &linesize, a->f) == -1) {
3350 if (ferror(a->f))
3351 err = got_error_from_errno("getline");
3352 break;
3355 committer = bline->committer;
3356 smallerthan = strchr(committer, '<');
3357 if (smallerthan && smallerthan[1] != '\0')
3358 committer = smallerthan + 1;
3359 at = strchr(committer, '@');
3360 if (at)
3361 *at = '\0';
3362 len = strlen(committer);
3363 if (len >= 9)
3364 committer[8] = '\0';
3366 nl = strchr(line, '\n');
3367 if (nl)
3368 *nl = '\0';
3370 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3371 "blame_wrapper", KATTR__MAX);
3372 if (kerr != KCGI_OK)
3373 goto err;
3374 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3375 "blame_number", KATTR__MAX);
3376 if (kerr != KCGI_OK)
3377 goto err;
3378 if (asprintf(&lineno, "%.*d", a->nlines_prec,
3379 a->lineno_cur) == -1)
3380 goto err;
3381 kerr = khtml_puts(a->gw_trans->gw_html_req, lineno);
3382 if (kerr != KCGI_OK)
3383 goto err;
3384 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3385 if (kerr != KCGI_OK)
3386 goto err;
3388 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3389 "blame_hash", KATTR__MAX);
3390 if (kerr != KCGI_OK)
3391 goto err;
3392 /* XXX repo_file could be NULL if not present in querystring */
3393 if (asprintf(&href_diff,
3394 "?path=%s&action=diff&commit=%s&file=%s&folder=%s",
3395 a->gw_trans->repo_name, bline->id_str,
3396 a->gw_trans->repo_file, a->gw_trans->repo_folder ?
3397 a->gw_trans->repo_folder : "") == -1) {
3398 err = got_error_from_errno("asprintf");
3399 goto err;
3401 if (asprintf(&href_link, "%.8s", bline->id_str) == -1) {
3402 err = got_error_from_errno("asprintf");
3403 goto err;
3405 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3406 KATTR_HREF, href_diff, KATTR__MAX);
3407 if (kerr != KCGI_OK)
3408 goto done;
3409 kerr = khtml_puts(a->gw_trans->gw_html_req, href_link);
3410 if (kerr != KCGI_OK)
3411 goto err;
3412 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3413 if (kerr != KCGI_OK)
3414 goto err;
3416 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3417 "blame_date", KATTR__MAX);
3418 if (kerr != KCGI_OK)
3419 goto err;
3420 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3421 if (kerr != KCGI_OK)
3422 goto err;
3423 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3424 if (kerr != KCGI_OK)
3425 goto err;
3427 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3428 "blame_author", KATTR__MAX);
3429 if (kerr != KCGI_OK)
3430 goto err;
3431 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
3432 if (kerr != KCGI_OK)
3433 goto err;
3434 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3435 if (kerr != KCGI_OK)
3436 goto err;
3438 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3439 "blame_code", KATTR__MAX);
3440 if (kerr != KCGI_OK)
3441 goto err;
3442 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
3443 if (kerr != KCGI_OK)
3444 goto err;
3445 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3446 if (kerr != KCGI_OK)
3447 goto err;
3449 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3450 if (kerr != KCGI_OK)
3451 goto err;
3453 a->lineno_cur++;
3454 bline = &a->lines[a->lineno_cur - 1];
3455 err:
3456 free(lineno);
3457 free(href_diff);
3458 free(href_link);
3460 done:
3461 if (commit)
3462 got_object_commit_close(commit);
3463 free(line);
3464 if (err == NULL && kerr != KCGI_OK)
3465 err = gw_kcgi_error(kerr);
3466 return err;
3469 static const struct got_error *
3470 gw_output_file_blame(struct gw_trans *gw_trans)
3472 const struct got_error *error = NULL;
3473 struct got_repository *repo = NULL;
3474 struct got_object_id *obj_id = NULL;
3475 struct got_object_id *commit_id = NULL;
3476 struct got_blob_object *blob = NULL;
3477 char *path = NULL, *in_repo_path = NULL;
3478 struct gw_blame_cb_args bca;
3479 int i, obj_type;
3480 size_t filesize;
3482 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3483 if (error)
3484 return error;
3486 /* XXX repo_file could be NULL if not present in querystring */
3487 if (asprintf(&path, "%s%s%s",
3488 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3489 gw_trans->repo_folder ? "/" : "",
3490 gw_trans->repo_file) == -1) {
3491 error = got_error_from_errno("asprintf");
3492 goto done;
3495 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3496 if (error)
3497 goto done;
3499 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3500 GOT_OBJ_TYPE_COMMIT, 1, repo);
3501 if (error)
3502 goto done;
3504 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3505 if (error)
3506 goto done;
3508 if (obj_id == NULL) {
3509 error = got_error(GOT_ERR_NO_OBJ);
3510 goto done;
3513 error = got_object_get_type(&obj_type, repo, obj_id);
3514 if (error)
3515 goto done;
3517 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3518 error = got_error(GOT_ERR_OBJ_TYPE);
3519 goto done;
3522 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3523 if (error)
3524 goto done;
3526 bca.f = got_opentemp();
3527 if (bca.f == NULL) {
3528 error = got_error_from_errno("got_opentemp");
3529 goto done;
3531 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3532 &bca.line_offsets, bca.f, blob);
3533 if (error || bca.nlines == 0)
3534 goto done;
3536 /* Don't include \n at EOF in the blame line count. */
3537 if (bca.line_offsets[bca.nlines - 1] == filesize)
3538 bca.nlines--;
3540 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3541 if (bca.lines == NULL) {
3542 error = got_error_from_errno("calloc");
3543 goto done;
3545 bca.lineno_cur = 1;
3546 bca.nlines_prec = 0;
3547 i = bca.nlines;
3548 while (i > 0) {
3549 i /= 10;
3550 bca.nlines_prec++;
3552 bca.repo = repo;
3553 bca.gw_trans = gw_trans;
3555 error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
3556 NULL, NULL);
3557 done:
3558 free(bca.line_offsets);
3559 free(in_repo_path);
3560 free(commit_id);
3561 free(obj_id);
3562 free(path);
3564 for (i = 0; i < bca.nlines; i++) {
3565 struct blame_line *bline = &bca.lines[i];
3566 free(bline->id_str);
3567 free(bline->committer);
3569 free(bca.lines);
3570 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3571 error = got_error_from_errno("fclose");
3572 if (blob)
3573 got_object_blob_close(blob);
3574 if (repo)
3575 got_repo_close(repo);
3576 return error;
3579 static const struct got_error *
3580 gw_output_blob_buf(struct gw_trans *gw_trans)
3582 const struct got_error *error = NULL;
3583 struct got_repository *repo = NULL;
3584 struct got_object_id *obj_id = NULL;
3585 struct got_object_id *commit_id = NULL;
3586 struct got_blob_object *blob = NULL;
3587 char *path = NULL, *in_repo_path = NULL;
3588 int obj_type, set_mime = 0;
3589 size_t len, hdrlen;
3590 const uint8_t *buf;
3591 enum kcgi_err kerr = KCGI_OK;
3593 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3594 if (error)
3595 return error;
3597 /* XXX repo_file could be NULL if not present in querystring */
3598 if (asprintf(&path, "%s%s%s",
3599 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3600 gw_trans->repo_folder ? "/" : "",
3601 gw_trans->repo_file) == -1) {
3602 error = got_error_from_errno("asprintf");
3603 goto done;
3606 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3607 if (error)
3608 goto done;
3610 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3611 GOT_OBJ_TYPE_COMMIT, 1, repo);
3612 if (error)
3613 goto done;
3615 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3616 if (error)
3617 goto done;
3619 if (obj_id == NULL) {
3620 error = got_error(GOT_ERR_NO_OBJ);
3621 goto done;
3624 error = got_object_get_type(&obj_type, repo, obj_id);
3625 if (error)
3626 goto done;
3628 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3629 error = got_error(GOT_ERR_OBJ_TYPE);
3630 goto done;
3633 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3634 if (error)
3635 goto done;
3637 hdrlen = got_object_blob_get_hdrlen(blob);
3638 do {
3639 error = got_object_blob_read_block(&len, blob);
3640 if (error)
3641 goto done;
3642 buf = got_object_blob_get_read_buf(blob);
3645 * Skip blob object header first time around,
3646 * which also contains a zero byte.
3648 buf += hdrlen;
3649 if (set_mime == 0) {
3650 if (isbinary(buf, len - hdrlen))
3651 gw_trans->mime = KMIME_APP_OCTET_STREAM;
3652 else
3653 gw_trans->mime = KMIME_TEXT_PLAIN;
3654 set_mime = 1;
3655 error = gw_display_index(gw_trans);
3656 if (error)
3657 goto done;
3659 khttp_write(gw_trans->gw_req, buf, len - hdrlen);
3660 hdrlen = 0;
3661 } while (len != 0);
3662 done:
3663 free(in_repo_path);
3664 free(commit_id);
3665 free(obj_id);
3666 free(path);
3667 if (blob)
3668 got_object_blob_close(blob);
3669 if (repo)
3670 got_repo_close(repo);
3671 if (error == NULL && kerr != KCGI_OK)
3672 error = gw_kcgi_error(kerr);
3673 return error;
3676 static const struct got_error *
3677 gw_output_repo_tree(struct gw_trans *gw_trans)
3679 const struct got_error *error = NULL;
3680 struct got_repository *repo = NULL;
3681 struct got_object_id *tree_id = NULL, *commit_id = NULL;
3682 struct got_tree_object *tree = NULL;
3683 char *path = NULL, *in_repo_path = NULL;
3684 char *id_str = NULL;
3685 char *build_folder = NULL;
3686 char *href_blob = NULL, *href_blame = NULL;
3687 const char *class = NULL;
3688 int nentries, i, class_flip = 0;
3689 enum kcgi_err kerr = KCGI_OK;
3691 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3692 if (error)
3693 return error;
3695 if (gw_trans->repo_folder != NULL) {
3696 path = strdup(gw_trans->repo_folder);
3697 if (path == NULL) {
3698 error = got_error_from_errno("strdup");
3699 goto done;
3701 } else {
3702 error = got_repo_map_path(&in_repo_path, repo,
3703 gw_trans->repo_path, 1);
3704 if (error)
3705 goto done;
3706 free(path);
3707 path = in_repo_path;
3710 if (gw_trans->commit == NULL) {
3711 struct got_reference *head_ref;
3712 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
3713 if (error)
3714 goto done;
3715 error = got_ref_resolve(&commit_id, repo, head_ref);
3716 if (error)
3717 goto done;
3718 got_ref_close(head_ref);
3720 } else {
3721 error = got_repo_match_object_id(&commit_id, NULL,
3722 gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
3723 if (error)
3724 goto done;
3728 * XXX gw_trans->commit might have already been allocated in
3729 * gw_parse_querystring(); could we more cleanly seperate values
3730 * we received as arguments from values we compute ourselves?
3732 error = got_object_id_str(&gw_trans->commit, commit_id);
3733 if (error)
3734 goto done;
3736 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
3737 if (error)
3738 goto done;
3740 error = got_object_open_as_tree(&tree, repo, tree_id);
3741 if (error)
3742 goto done;
3744 nentries = got_object_tree_get_nentries(tree);
3745 for (i = 0; i < nentries; i++) {
3746 struct got_tree_entry *te;
3747 const char *modestr = "";
3748 mode_t mode;
3750 te = got_object_tree_get_entry(tree, i);
3752 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
3753 if (error)
3754 goto done;
3756 mode = got_tree_entry_get_mode(te);
3757 if (got_object_tree_entry_is_submodule(te))
3758 modestr = "$";
3759 else if (S_ISLNK(mode))
3760 modestr = "@";
3761 else if (S_ISDIR(mode))
3762 modestr = "/";
3763 else if (mode & S_IXUSR)
3764 modestr = "*";
3766 if (class_flip == 0) {
3767 class = "back_lightgray";
3768 class_flip = 1;
3769 } else {
3770 class = "back_white";
3771 class_flip = 0;
3774 if (S_ISDIR(mode)) {
3775 if (asprintf(&build_folder, "%s/%s",
3776 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3777 got_tree_entry_get_name(te)) == -1) {
3778 error = got_error_from_errno("asprintf");
3779 goto done;
3781 if (asprintf(&href_blob,
3782 "?path=%s&action=%s&commit=%s&folder=%s",
3783 gw_trans->repo_name, gw_get_action_name(gw_trans),
3784 gw_trans->commit, build_folder) == -1) {
3785 error = got_error_from_errno("asprintf");
3786 goto done;
3789 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3790 KATTR_ID, "tree_wrapper", KATTR__MAX);
3791 if (kerr != KCGI_OK)
3792 goto done;
3793 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3794 KATTR_ID, "tree_line", KATTR_CLASS, class,
3795 KATTR__MAX);
3796 if (kerr != KCGI_OK)
3797 goto done;
3798 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3799 KATTR_HREF, href_blob, KATTR_CLASS,
3800 "diff_directory", KATTR__MAX);
3801 if (kerr != KCGI_OK)
3802 goto done;
3803 kerr = khtml_puts(gw_trans->gw_html_req,
3804 got_tree_entry_get_name(te));
3805 if (kerr != KCGI_OK)
3806 goto done;
3807 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3808 if (kerr != KCGI_OK)
3809 goto done;
3810 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3811 if (kerr != KCGI_OK)
3812 goto done;
3813 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3814 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
3815 KATTR__MAX);
3816 if (kerr != KCGI_OK)
3817 goto done;
3818 kerr = khtml_entity(gw_trans->gw_html_req,
3819 KENTITY_nbsp);
3820 if (kerr != KCGI_OK)
3821 goto done;
3822 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3823 if (kerr != KCGI_OK)
3824 goto done;
3825 } else {
3826 if (asprintf(&href_blob,
3827 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3828 gw_trans->repo_name, "blob", gw_trans->commit,
3829 got_tree_entry_get_name(te),
3830 gw_trans->repo_folder ?
3831 gw_trans->repo_folder : "") == -1) {
3832 error = got_error_from_errno("asprintf");
3833 goto done;
3835 if (asprintf(&href_blame,
3836 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3837 gw_trans->repo_name, "blame", gw_trans->commit,
3838 got_tree_entry_get_name(te),
3839 gw_trans->repo_folder ?
3840 gw_trans->repo_folder : "") == -1) {
3841 error = got_error_from_errno("asprintf");
3842 goto done;
3845 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3846 KATTR_ID, "tree_wrapper", KATTR__MAX);
3847 if (kerr != KCGI_OK)
3848 goto done;
3849 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3850 KATTR_ID, "tree_line", KATTR_CLASS, class,
3851 KATTR__MAX);
3852 if (kerr != KCGI_OK)
3853 goto done;
3854 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3855 KATTR_HREF, href_blob, KATTR__MAX);
3856 if (kerr != KCGI_OK)
3857 goto done;
3858 kerr = khtml_puts(gw_trans->gw_html_req,
3859 got_tree_entry_get_name(te));
3860 if (kerr != KCGI_OK)
3861 goto done;
3862 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3863 if (kerr != KCGI_OK)
3864 goto done;
3865 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3866 if (kerr != KCGI_OK)
3867 goto done;
3868 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3869 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
3870 KATTR__MAX);
3871 if (kerr != KCGI_OK)
3872 goto done;
3874 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3875 KATTR_HREF, href_blob, KATTR__MAX);
3876 if (kerr != KCGI_OK)
3877 goto done;
3878 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
3879 if (kerr != KCGI_OK)
3880 goto done;
3881 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3882 if (kerr != KCGI_OK)
3883 goto done;
3885 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3886 if (kerr != KCGI_OK)
3887 goto done;
3889 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3890 KATTR_HREF, href_blame, KATTR__MAX);
3891 if (kerr != KCGI_OK)
3892 goto done;
3893 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
3894 if (kerr != KCGI_OK)
3895 goto done;
3897 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3898 if (kerr != KCGI_OK)
3899 goto done;
3901 free(id_str);
3902 id_str = NULL;
3903 free(href_blob);
3904 href_blob = NULL;
3905 free(build_folder);
3906 build_folder = NULL;
3908 done:
3909 if (tree)
3910 got_object_tree_close(tree);
3911 if (repo)
3912 got_repo_close(repo);
3913 free(id_str);
3914 free(href_blob);
3915 free(href_blame);
3916 free(in_repo_path);
3917 free(tree_id);
3918 free(build_folder);
3919 if (error == NULL && kerr != KCGI_OK)
3920 error = gw_kcgi_error(kerr);
3921 return error;
3924 static const struct got_error *
3925 gw_output_repo_heads(struct gw_trans *gw_trans)
3927 const struct got_error *error = NULL;
3928 struct got_repository *repo = NULL;
3929 struct got_reflist_head refs;
3930 struct got_reflist_entry *re;
3931 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
3932 char *href_commits = NULL;
3933 enum kcgi_err kerr = KCGI_OK;
3935 SIMPLEQ_INIT(&refs);
3937 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3938 if (error)
3939 goto done;
3941 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
3942 NULL);
3943 if (error)
3944 goto done;
3946 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3947 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
3948 if (kerr != KCGI_OK)
3949 goto done;
3950 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3951 KATTR_ID, "summary_heads_title", KATTR__MAX);
3952 if (kerr != KCGI_OK)
3953 goto done;
3954 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
3955 if (kerr != KCGI_OK)
3956 goto done;
3957 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3958 if (kerr != KCGI_OK)
3959 goto done;
3960 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3961 KATTR_ID, "summary_heads_content", KATTR__MAX);
3962 if (kerr != KCGI_OK)
3963 goto done;
3965 SIMPLEQ_FOREACH(re, &refs, entry) {
3966 char *refname;
3968 refname = strdup(got_ref_get_name(re->ref));
3969 if (refname == NULL) {
3970 error = got_error_from_errno("got_ref_to_str");
3971 goto done;
3974 if (strncmp(refname, "refs/heads/", 11) != 0) {
3975 free(refname);
3976 continue;
3979 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
3980 refname, TM_DIFF);
3981 if (error)
3982 goto done;
3984 if (strncmp(refname, "refs/heads/", 11) == 0)
3985 refname += 11;
3987 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3988 KATTR_ID, "heads_wrapper", KATTR__MAX);
3989 if (kerr != KCGI_OK)
3990 goto done;
3991 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3992 KATTR_ID, "heads_age", KATTR__MAX);
3993 if (kerr != KCGI_OK)
3994 goto done;
3995 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
3996 if (kerr != KCGI_OK)
3997 goto done;
3998 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3999 if (kerr != KCGI_OK)
4000 goto done;
4001 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4002 KATTR_ID, "heads_space", KATTR__MAX);
4003 if (kerr != KCGI_OK)
4004 goto done;
4005 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
4006 if (kerr != KCGI_OK)
4007 goto done;
4008 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4009 if (kerr != KCGI_OK)
4010 goto done;
4011 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4012 KATTR_ID, "head", KATTR__MAX);
4013 if (kerr != KCGI_OK)
4014 goto done;
4015 if (asprintf(&href_summary,
4016 "?path=%s&action=summary&headref=%s",
4017 gw_trans->repo_name, refname) == -1) {
4018 error = got_error_from_errno("asprintf");
4019 goto done;
4021 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4022 href_summary, KATTR__MAX);
4023 kerr = khtml_puts(gw_trans->gw_html_req, refname);
4024 if (kerr != KCGI_OK)
4025 goto done;
4026 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4027 if (kerr != KCGI_OK)
4028 goto done;
4030 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4031 "navs_wrapper", KATTR__MAX);
4032 if (kerr != KCGI_OK)
4033 goto done;
4034 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4035 "navs", KATTR__MAX);
4036 if (kerr != KCGI_OK)
4037 goto done;
4039 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4040 href_summary, KATTR__MAX);
4041 if (kerr != KCGI_OK)
4042 goto done;
4043 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4044 if (kerr != KCGI_OK)
4045 goto done;
4046 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4047 if (kerr != KCGI_OK)
4048 goto done;
4050 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4051 if (kerr != KCGI_OK)
4052 goto done;
4053 if (asprintf(&href_briefs, "?path=%s&action=briefs&headref=%s",
4054 gw_trans->repo_name, refname) == -1) {
4055 error = got_error_from_errno("asprintf");
4056 goto done;
4058 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4059 href_briefs, KATTR__MAX);
4060 if (kerr != KCGI_OK)
4061 goto done;
4062 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4063 if (kerr != KCGI_OK)
4064 goto done;
4065 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4066 if (kerr != KCGI_OK)
4067 goto done;
4069 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4070 if (kerr != KCGI_OK)
4071 goto done;
4073 if (asprintf(&href_commits,
4074 "?path=%s&action=commits&headref=%s",
4075 gw_trans->repo_name, refname) == -1) {
4076 error = got_error_from_errno("asprintf");
4077 goto done;
4079 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4080 href_commits, KATTR__MAX);
4081 if (kerr != KCGI_OK)
4082 goto done;
4083 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4084 if (kerr != KCGI_OK)
4085 goto done;
4086 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4087 if (kerr != KCGI_OK)
4088 goto done;
4090 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4091 "dotted_line", KATTR__MAX);
4092 if (kerr != KCGI_OK)
4093 goto done;
4094 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4095 if (kerr != KCGI_OK)
4096 goto done;
4097 free(href_summary);
4098 href_summary = NULL;
4099 free(href_briefs);
4100 href_briefs = NULL;
4101 free(href_commits);
4102 href_commits = NULL;
4104 done:
4105 got_ref_list_free(&refs);
4106 free(href_summary);
4107 free(href_briefs);
4108 free(href_commits);
4109 if (repo)
4110 got_repo_close(repo);
4111 return error;
4114 static const struct got_error *
4115 gw_output_site_link(struct gw_trans *gw_trans)
4117 const struct got_error *error = NULL;
4118 char *href_summary = NULL;
4119 enum kcgi_err kerr = KCGI_OK;
4121 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4122 "site_link", KATTR__MAX);
4123 if (kerr != KCGI_OK)
4124 goto done;
4125 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4126 KATTR__MAX);
4127 if (kerr != KCGI_OK)
4128 goto done;
4129 kerr = khtml_puts(gw_trans->gw_html_req,
4130 gw_trans->gw_conf->got_site_link);
4131 if (kerr != KCGI_OK)
4132 goto done;
4133 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4134 if (kerr != KCGI_OK)
4135 goto done;
4137 if (gw_trans->repo_name != NULL) {
4138 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4139 if (kerr != KCGI_OK)
4140 goto done;
4141 if (asprintf(&href_summary, "?path=%s&action=summary",
4142 gw_trans->repo_name) == -1)
4143 goto done;
4144 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4145 href_summary, KATTR__MAX);
4146 if (kerr != KCGI_OK)
4147 goto done;
4148 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4149 if (kerr != KCGI_OK)
4150 goto done;
4151 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4152 if (kerr != KCGI_OK)
4153 goto done;
4154 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4155 if (kerr != KCGI_OK)
4156 goto done;
4157 kerr = khtml_puts(gw_trans->gw_html_req,
4158 gw_get_action_name(gw_trans));
4159 if (kerr != KCGI_OK)
4160 goto done;
4163 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4164 if (kerr != KCGI_OK)
4165 goto done;
4166 done:
4167 free(href_summary);
4168 return error;
4171 static const struct got_error *
4172 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4174 const struct got_error *error = NULL;
4175 char *color = NULL;
4176 enum kcgi_err kerr = KCGI_OK;
4178 if (strncmp(buf, "-", 1) == 0)
4179 color = "diff_minus";
4180 else if (strncmp(buf, "+", 1) == 0)
4181 color = "diff_plus";
4182 else if (strncmp(buf, "@@", 2) == 0)
4183 color = "diff_chunk_header";
4184 else if (strncmp(buf, "@@", 2) == 0)
4185 color = "diff_chunk_header";
4186 else if (strncmp(buf, "commit +", 8) == 0)
4187 color = "diff_meta";
4188 else if (strncmp(buf, "commit -", 8) == 0)
4189 color = "diff_meta";
4190 else if (strncmp(buf, "blob +", 6) == 0)
4191 color = "diff_meta";
4192 else if (strncmp(buf, "blob -", 6) == 0)
4193 color = "diff_meta";
4194 else if (strncmp(buf, "file +", 6) == 0)
4195 color = "diff_meta";
4196 else if (strncmp(buf, "file -", 6) == 0)
4197 color = "diff_meta";
4198 else if (strncmp(buf, "from:", 5) == 0)
4199 color = "diff_author";
4200 else if (strncmp(buf, "via:", 4) == 0)
4201 color = "diff_author";
4202 else if (strncmp(buf, "date:", 5) == 0)
4203 color = "diff_date";
4204 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4205 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4206 if (error == NULL && kerr != KCGI_OK)
4207 error = gw_kcgi_error(kerr);
4208 return error;
4211 int
4212 main(int argc, char *argv[])
4214 const struct got_error *error = NULL;
4215 struct gw_trans *gw_trans;
4216 struct gw_dir *dir = NULL, *tdir;
4217 const char *page = "index";
4218 int gw_malloc = 1;
4219 enum kcgi_err kerr;
4221 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4222 errx(1, "malloc");
4224 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4225 errx(1, "malloc");
4227 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4228 errx(1, "malloc");
4230 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4231 errx(1, "malloc");
4233 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4234 if (kerr != KCGI_OK) {
4235 error = gw_kcgi_error(kerr);
4236 goto done;
4239 if ((gw_trans->gw_conf =
4240 malloc(sizeof(struct gotweb_conf))) == NULL) {
4241 gw_malloc = 0;
4242 error = got_error_from_errno("malloc");
4243 goto done;
4246 TAILQ_INIT(&gw_trans->gw_dirs);
4247 TAILQ_INIT(&gw_trans->gw_headers);
4249 gw_trans->action = -1;
4250 gw_trans->page = 0;
4251 gw_trans->repos_total = 0;
4252 gw_trans->repo_path = NULL;
4253 gw_trans->commit = NULL;
4254 gw_trans->headref = strdup(GOT_REF_HEAD);
4255 if (gw_trans->headref == NULL) {
4256 error = got_error_from_errno("strdup");
4257 goto done;
4259 gw_trans->mime = KMIME_TEXT_HTML;
4260 gw_trans->gw_tmpl->key = gw_templs;
4261 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4262 gw_trans->gw_tmpl->arg = gw_trans;
4263 gw_trans->gw_tmpl->cb = gw_template;
4264 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
4265 if (error)
4266 goto done;
4268 error = gw_parse_querystring(gw_trans);
4269 if (error)
4270 goto done;
4272 if (gw_trans->action == GW_BLOB)
4273 error = gw_blob(gw_trans);
4274 else
4275 error = gw_display_index(gw_trans);
4276 done:
4277 if (gw_malloc) {
4278 free(gw_trans->gw_conf->got_repos_path);
4279 free(gw_trans->gw_conf->got_site_name);
4280 free(gw_trans->gw_conf->got_site_owner);
4281 free(gw_trans->gw_conf->got_site_link);
4282 free(gw_trans->gw_conf->got_logo);
4283 free(gw_trans->gw_conf->got_logo_url);
4284 free(gw_trans->gw_conf);
4285 free(gw_trans->commit);
4286 free(gw_trans->repo_path);
4287 free(gw_trans->headref);
4289 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4290 free(dir->name);
4291 free(dir->description);
4292 free(dir->age);
4293 free(dir->url);
4294 free(dir->path);
4295 free(dir);
4300 khttp_free(gw_trans->gw_req);
4301 return 0;