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 char *repo_name;
64 char *repo_path;
65 char *commit;
66 const char *repo_file;
67 char *repo_folder;
68 char *headref;
69 unsigned int action;
70 unsigned int page;
71 unsigned int repos_total;
72 enum kmime mime;
73 };
75 struct gw_header {
76 TAILQ_ENTRY(gw_header) entry;
77 struct got_repository *repo;
78 struct got_reflist_head refs;
79 struct got_commit_object *commit;
80 struct got_object_id *id;
81 char *path;
83 char *refs_str;
84 char *commit_id; /* id_str1 */
85 char *parent_id; /* id_str2 */
86 char *tree_id;
87 const char *author;
88 const char *committer;
89 char *commit_msg;
90 time_t committer_time;
91 };
93 struct gw_dir {
94 TAILQ_ENTRY(gw_dir) entry;
95 char *name;
96 char *owner;
97 char *description;
98 char *url;
99 char *age;
100 char *path;
101 };
103 enum gw_key {
104 KEY_ACTION,
105 KEY_COMMIT_ID,
106 KEY_FILE,
107 KEY_FOLDER,
108 KEY_HEADREF,
109 KEY_PAGE,
110 KEY_PATH,
111 KEY__ZMAX
112 };
114 enum gw_tmpl {
115 TEMPL_CONTENT,
116 TEMPL_HEAD,
117 TEMPL_HEADER,
118 TEMPL_SEARCH,
119 TEMPL_SITEPATH,
120 TEMPL_SITEOWNER,
121 TEMPL_TITLE,
122 TEMPL__MAX
123 };
125 enum gw_ref_tm {
126 TM_DIFF,
127 TM_LONG,
128 };
130 enum gw_tags {
131 TAGBRIEF,
132 TAGFULL,
133 };
135 static const char *const gw_templs[TEMPL__MAX] = {
136 "content",
137 "head",
138 "header",
139 "search",
140 "sitepath",
141 "siteowner",
142 "title",
143 };
145 static const struct kvalid gw_keys[KEY__ZMAX] = {
146 { kvalid_stringne, "action" },
147 { kvalid_stringne, "commit" },
148 { kvalid_stringne, "file" },
149 { kvalid_stringne, "folder" },
150 { kvalid_stringne, "headref" },
151 { kvalid_int, "page" },
152 { kvalid_stringne, "path" },
153 };
155 static const struct got_error *gw_init_gw_dir(struct gw_dir **, const char *);
156 static struct gw_header *gw_init_header(void);
158 static void gw_free_headers(struct gw_header *);
159 static void gw_display_error(struct gw_trans *,
160 const struct got_error *);
162 static int gw_template(size_t, void *);
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, NULL, NULL, "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, *age_html = 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 if (asprintf(&age_html, "%s", age ? age : "") == -1) {
1074 error = got_error_from_errno("asprintf");
1075 goto done;
1077 kerr = khtml_puts(gw_trans->gw_html_req, age_html);
1078 if (kerr != KCGI_OK)
1079 goto done;
1080 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1081 if (kerr != KCGI_OK)
1082 goto done;
1084 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1085 KATTR_ID, "briefs_author", KATTR__MAX);
1086 if (kerr != KCGI_OK)
1087 goto done;
1088 smallerthan = strchr(n_header->author, '<');
1089 if (smallerthan)
1090 *smallerthan = '\0';
1091 kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
1092 if (kerr != KCGI_OK)
1093 goto done;
1094 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1095 if (kerr != KCGI_OK)
1096 goto done;
1098 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
1099 gw_trans->repo_name, n_header->commit_id) == -1) {
1100 error = got_error_from_errno("asprintf");
1101 goto done;
1103 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1104 KATTR_ID, "briefs_log", KATTR__MAX);
1105 if (kerr != KCGI_OK)
1106 goto done;
1107 newline = strchr(n_header->commit_msg, '\n');
1108 if (newline)
1109 *newline = '\0';
1110 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1111 KATTR_HREF, href_diff, KATTR__MAX);
1112 if (kerr != KCGI_OK)
1113 goto done;
1114 kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
1115 if (kerr != KCGI_OK)
1116 goto done;
1117 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1118 if (kerr != KCGI_OK)
1119 goto done;
1121 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1122 KATTR_ID, "navs_wrapper", KATTR__MAX);
1123 if (kerr != KCGI_OK)
1124 goto done;
1125 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1126 KATTR_ID, "navs", KATTR__MAX);
1127 if (kerr != KCGI_OK)
1128 goto done;
1129 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1130 KATTR_HREF, href_diff, KATTR__MAX);
1131 if (kerr != KCGI_OK)
1132 goto done;
1133 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1134 if (kerr != KCGI_OK)
1135 goto done;
1136 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1137 if (kerr != KCGI_OK)
1138 goto done;
1140 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1141 if (kerr != KCGI_OK)
1142 goto done;
1144 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
1145 gw_trans->repo_name, n_header->commit_id) == -1) {
1146 error = got_error_from_errno("asprintf");
1147 goto done;
1149 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1150 KATTR_HREF, href_blob, KATTR__MAX);
1151 if (kerr != KCGI_OK)
1152 goto done;
1153 khtml_puts(gw_trans->gw_html_req, "tree");
1154 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1155 if (kerr != KCGI_OK)
1156 goto done;
1157 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1158 if (kerr != KCGI_OK)
1159 goto done;
1161 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1162 KATTR_ID, "dotted_line", KATTR__MAX);
1163 if (kerr != KCGI_OK)
1164 goto done;
1165 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1166 if (kerr != KCGI_OK)
1167 goto done;
1169 free(age);
1170 age = NULL;
1171 free(age_html);
1172 age_html = NULL;
1173 free(href_diff);
1174 href_diff = NULL;
1175 free(href_blob);
1176 href_blob = NULL;
1178 done:
1179 got_ref_list_free(&header->refs);
1180 gw_free_headers(header);
1181 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1182 gw_free_headers(n_header);
1183 free(age);
1184 free(age_html);
1185 free(href_diff);
1186 free(href_blob);
1187 if (error == NULL && kerr != KCGI_OK)
1188 error = gw_kcgi_error(kerr);
1189 return error;
1192 static const struct got_error *
1193 gw_summary(struct gw_trans *gw_trans)
1195 const struct got_error *error = NULL;
1196 char *age = NULL;
1197 enum kcgi_err kerr = KCGI_OK;
1199 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1200 return got_error_from_errno("pledge");
1202 /* unveil is applied with gw_briefs below */
1204 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1205 "summary_wrapper", KATTR__MAX);
1206 if (kerr != KCGI_OK)
1207 return gw_kcgi_error(kerr);
1209 if (gw_trans->gw_conf->got_show_repo_description &&
1210 gw_trans->gw_dir->description != NULL &&
1211 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1212 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1213 KATTR_ID, "description_title", KATTR__MAX);
1214 if (kerr != KCGI_OK)
1215 goto done;
1216 kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1217 if (kerr != KCGI_OK)
1218 goto done;
1219 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1220 if (kerr != KCGI_OK)
1221 goto done;
1222 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1223 KATTR_ID, "description", KATTR__MAX);
1224 if (kerr != KCGI_OK)
1225 goto done;
1226 kerr = khtml_puts(gw_trans->gw_html_req,
1227 gw_trans->gw_dir->description);
1228 if (kerr != KCGI_OK)
1229 goto done;
1230 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1231 if (kerr != KCGI_OK)
1232 goto done;
1235 if (gw_trans->gw_conf->got_show_repo_owner &&
1236 gw_trans->gw_dir->owner != NULL &&
1237 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
1238 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1239 KATTR_ID, "repo_owner_title", KATTR__MAX);
1240 if (kerr != KCGI_OK)
1241 goto done;
1242 kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1243 if (kerr != KCGI_OK)
1244 goto done;
1245 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1246 if (kerr != KCGI_OK)
1247 goto done;
1248 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1249 KATTR_ID, "repo_owner", KATTR__MAX);
1250 if (kerr != KCGI_OK)
1251 goto done;
1252 kerr = khtml_puts(gw_trans->gw_html_req,
1253 gw_trans->gw_dir->owner);
1254 if (kerr != KCGI_OK)
1255 goto done;
1256 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1257 if (kerr != KCGI_OK)
1258 goto done;
1261 if (gw_trans->gw_conf->got_show_repo_age) {
1262 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1263 "refs/heads", TM_LONG);
1264 if (error)
1265 goto done;
1266 if (age != NULL) {
1267 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1268 KATTR_ID, "last_change_title", KATTR__MAX);
1269 if (kerr != KCGI_OK)
1270 goto done;
1271 kerr = khtml_puts(gw_trans->gw_html_req,
1272 "Last Change: ");
1273 if (kerr != KCGI_OK)
1274 goto done;
1275 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1276 if (kerr != KCGI_OK)
1277 goto done;
1278 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1279 KATTR_ID, "last_change", KATTR__MAX);
1280 if (kerr != KCGI_OK)
1281 goto done;
1282 kerr = khtml_puts(gw_trans->gw_html_req, age);
1283 if (kerr != KCGI_OK)
1284 goto done;
1285 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1286 if (kerr != KCGI_OK)
1287 goto done;
1291 if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1292 gw_trans->gw_dir->url != NULL &&
1293 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1294 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1295 KATTR_ID, "cloneurl_title", KATTR__MAX);
1296 if (kerr != KCGI_OK)
1297 goto done;
1298 kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1299 if (kerr != KCGI_OK)
1300 goto done;
1301 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1302 if (kerr != KCGI_OK)
1303 goto done;
1304 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1305 KATTR_ID, "cloneurl", KATTR__MAX);
1306 if (kerr != KCGI_OK)
1307 goto done;
1308 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1309 if (kerr != KCGI_OK)
1310 goto done;
1311 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1312 if (kerr != KCGI_OK)
1313 goto done;
1316 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1317 if (kerr != KCGI_OK)
1318 goto done;
1320 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1321 "briefs_title_wrapper", KATTR__MAX);
1322 if (kerr != KCGI_OK)
1323 goto done;
1324 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1325 "briefs_title", KATTR__MAX);
1326 if (kerr != KCGI_OK)
1327 goto done;
1328 kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1329 if (kerr != KCGI_OK)
1330 goto done;
1331 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1332 if (kerr != KCGI_OK)
1333 goto done;
1334 error = gw_briefs(gw_trans);
1335 if (error)
1336 goto done;
1338 error = gw_output_repo_tags(gw_trans, NULL, D_MAXSLCOMMDISP,
1339 TAGBRIEF);
1340 if (error)
1341 goto done;
1343 error = gw_output_repo_heads(gw_trans);
1344 done:
1345 free(age);
1346 if (error == NULL && kerr != KCGI_OK)
1347 error = gw_kcgi_error(kerr);
1348 return error;
1351 static const struct got_error *
1352 gw_tree(struct gw_trans *gw_trans)
1354 const struct got_error *error = NULL;
1355 struct gw_header *header = NULL;
1356 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1357 char *age = NULL, *age_html = NULL;
1358 enum kcgi_err kerr;
1360 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1361 return got_error_from_errno("pledge");
1363 if ((header = gw_init_header()) == NULL)
1364 return got_error_from_errno("malloc");
1366 error = gw_apply_unveil(gw_trans->gw_dir->path);
1367 if (error)
1368 goto done;
1370 error = gw_get_header(gw_trans, header, 1);
1371 if (error)
1372 goto done;
1374 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1375 "tree_header_wrapper", KATTR__MAX);
1376 if (kerr != KCGI_OK)
1377 goto done;
1378 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1379 "tree_header", KATTR__MAX);
1380 if (kerr != KCGI_OK)
1381 goto done;
1382 error = gw_gen_tree_header(gw_trans, header->tree_id);
1383 if (error)
1384 goto done;
1385 error = gw_get_time_str(&age, header->committer_time,
1386 TM_LONG);
1387 if (error)
1388 goto done;
1389 error = gw_gen_age_header(gw_trans, age ?age : "");
1390 if (error)
1391 goto done;
1392 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1393 if (error)
1394 goto done;
1395 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1396 if (kerr != KCGI_OK)
1397 goto done;
1398 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1399 "dotted_line", KATTR__MAX);
1400 if (kerr != KCGI_OK)
1401 goto done;
1402 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1403 if (kerr != KCGI_OK)
1404 goto done;
1406 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1407 "tree", KATTR__MAX);
1408 if (kerr != KCGI_OK)
1409 goto done;
1410 error = gw_output_repo_tree(gw_trans);
1411 if (error)
1412 goto done;
1414 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1415 done:
1416 got_ref_list_free(&header->refs);
1417 gw_free_headers(header);
1418 free(tree_html_disp);
1419 free(tree_html);
1420 free(tree);
1421 free(age);
1422 free(age_html);
1423 if (error == NULL && kerr != KCGI_OK)
1424 error = gw_kcgi_error(kerr);
1425 return error;
1428 static const struct got_error *
1429 gw_tag(struct gw_trans *gw_trans)
1431 const struct got_error *error = NULL;
1432 struct gw_header *header = NULL;
1433 enum kcgi_err kerr;
1435 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1436 return got_error_from_errno("pledge");
1438 if ((header = gw_init_header()) == NULL)
1439 return got_error_from_errno("malloc");
1441 error = gw_apply_unveil(gw_trans->gw_dir->path);
1442 if (error)
1443 goto done;
1445 error = gw_get_header(gw_trans, header, 1);
1446 if (error)
1447 goto done;
1449 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1450 "tag_header_wrapper", KATTR__MAX);
1451 if (kerr != KCGI_OK)
1452 goto done;
1453 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1454 "tag_header", KATTR__MAX);
1455 if (kerr != KCGI_OK)
1456 goto done;
1457 error = gw_gen_commit_header(gw_trans, header->commit_id,
1458 header->refs_str);
1459 if (error)
1460 goto done;
1461 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1462 if (error)
1463 goto done;
1464 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1465 if (kerr != KCGI_OK)
1466 goto done;
1467 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1468 "dotted_line", KATTR__MAX);
1469 if (kerr != KCGI_OK)
1470 goto done;
1471 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1472 if (kerr != KCGI_OK)
1473 goto done;
1475 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1476 "tree", KATTR__MAX);
1477 if (kerr != KCGI_OK)
1478 goto done;
1480 error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1481 if (error)
1482 goto done;
1484 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1485 done:
1486 got_ref_list_free(&header->refs);
1487 gw_free_headers(header);
1488 if (error == NULL && kerr != KCGI_OK)
1489 error = gw_kcgi_error(kerr);
1490 return error;
1493 static const struct got_error *
1494 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1496 const struct got_error *error = NULL;
1497 DIR *dt;
1498 char *dir_test;
1499 int opened = 0;
1501 if (asprintf(&dir_test, "%s/%s/%s",
1502 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1503 GOTWEB_GIT_DIR) == -1)
1504 return got_error_from_errno("asprintf");
1506 dt = opendir(dir_test);
1507 if (dt == NULL) {
1508 free(dir_test);
1509 } else {
1510 gw_dir->path = strdup(dir_test);
1511 if (gw_dir->path == NULL) {
1512 opened = 1;
1513 error = got_error_from_errno("strdup");
1514 goto errored;
1516 opened = 1;
1517 goto done;
1520 if (asprintf(&dir_test, "%s/%s/%s",
1521 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1522 GOTWEB_GOT_DIR) == -1)
1523 return got_error_from_errno("asprintf");
1525 dt = opendir(dir_test);
1526 if (dt == NULL)
1527 free(dir_test);
1528 else {
1529 opened = 1;
1530 error = got_error(GOT_ERR_NOT_GIT_REPO);
1531 goto errored;
1534 if (asprintf(&dir_test, "%s/%s",
1535 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1)
1536 return got_error_from_errno("asprintf");
1538 gw_dir->path = strdup(dir_test);
1539 if (gw_dir->path == NULL) {
1540 opened = 1;
1541 error = got_error_from_errno("strdup");
1542 goto errored;
1544 done:
1545 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1546 gw_dir->path);
1547 if (error)
1548 goto errored;
1549 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1550 if (error)
1551 goto errored;
1552 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1553 "refs/heads", TM_DIFF);
1554 if (error)
1555 goto errored;
1556 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1557 errored:
1558 free(dir_test);
1559 if (opened)
1560 closedir(dt);
1561 return error;
1564 static const struct got_error *
1565 gw_load_got_paths(struct gw_trans *gw_trans)
1567 const struct got_error *error = NULL;
1568 DIR *d;
1569 struct dirent **sd_dent;
1570 struct gw_dir *gw_dir;
1571 struct stat st;
1572 unsigned int d_cnt, d_i;
1574 d = opendir(gw_trans->gw_conf->got_repos_path);
1575 if (d == NULL) {
1576 error = got_error_from_errno2("opendir",
1577 gw_trans->gw_conf->got_repos_path);
1578 return error;
1581 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1582 alphasort);
1583 if (d_cnt == -1) {
1584 error = got_error_from_errno2("scandir",
1585 gw_trans->gw_conf->got_repos_path);
1586 return error;
1589 for (d_i = 0; d_i < d_cnt; d_i++) {
1590 if (gw_trans->gw_conf->got_max_repos > 0 &&
1591 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1592 break; /* account for parent and self */
1594 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1595 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1596 continue;
1598 error = gw_init_gw_dir(&gw_dir, sd_dent[d_i]->d_name);
1599 if (error)
1600 return error;
1602 error = gw_load_got_path(gw_trans, gw_dir);
1603 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1604 error = NULL;
1605 continue;
1607 else if (error)
1608 return error;
1610 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1611 !got_path_dir_is_empty(gw_dir->path)) {
1612 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1613 entry);
1614 gw_trans->repos_total++;
1618 closedir(d);
1619 return error;
1622 static const struct got_error *
1623 gw_parse_querystring(struct gw_trans *gw_trans)
1625 const struct got_error *error = NULL;
1626 struct kpair *p;
1627 struct gw_query_action *action = NULL;
1628 unsigned int i;
1630 if (gw_trans->gw_req->fieldnmap[0]) {
1631 error = got_error_from_errno("bad parse");
1632 return error;
1633 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1634 /* define gw_trans->repo_path */
1635 gw_trans->repo_name = p->parsed.s;
1637 if (asprintf(&gw_trans->repo_path, "%s/%s",
1638 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
1639 return got_error_from_errno("asprintf");
1641 /* get action and set function */
1642 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
1643 for (i = 0; i < nitems(gw_query_funcs); i++) {
1644 action = &gw_query_funcs[i];
1645 if (action->func_name == NULL ||
1646 strcmp(action->func_name, p->parsed.s))
1647 continue;
1649 gw_trans->action = i;
1650 break;
1653 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID])) {
1654 if (asprintf(&gw_trans->commit, "%s",
1655 p->parsed.s) == -1)
1656 return got_error_from_errno("asprintf");
1659 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1660 gw_trans->repo_file = p->parsed.s;
1662 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER])) {
1663 if (asprintf(&gw_trans->repo_folder, "%s",
1664 p->parsed.s) == -1)
1665 return got_error_from_errno("asprintf");
1668 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF])) {
1669 if (asprintf(&gw_trans->headref, "%s",
1670 p->parsed.s) == -1)
1671 return got_error_from_errno("asprintf");
1674 if (action == NULL) {
1675 error = got_error_from_errno("invalid action");
1676 return error;
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 void
1781 gw_display_error(struct gw_trans *gw_trans, const struct got_error *err)
1783 if (gw_display_open(gw_trans, KHTTP_200, gw_trans->mime) != NULL)
1784 return;
1786 if (khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0) != KCGI_OK)
1787 return;
1788 khtml_puts(gw_trans->gw_html_req, err->msg);
1789 khtml_close(gw_trans->gw_html_req);
1792 static int
1793 gw_template(size_t key, void *arg)
1795 const struct got_error *error = NULL;
1796 enum kcgi_err kerr;
1797 struct gw_trans *gw_trans = arg;
1798 char *img_src = NULL;
1800 switch (key) {
1801 case (TEMPL_HEAD):
1802 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1803 KATTR_NAME, "viewport",
1804 KATTR_CONTENT, "initial-scale=.75, user-scalable=yes",
1805 KATTR__MAX);
1806 if (kerr != KCGI_OK)
1807 return 0;
1808 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1809 if (kerr != KCGI_OK)
1810 return 0;
1811 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1812 KATTR_CHARSET, "utf-8",
1813 KATTR__MAX);
1814 if (kerr != KCGI_OK)
1815 return 0;
1816 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1817 if (kerr != KCGI_OK)
1818 return 0;
1819 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1820 KATTR_NAME, "msapplication-TileColor",
1821 KATTR_CONTENT, "#da532c", KATTR__MAX);
1822 if (kerr != KCGI_OK)
1823 return 0;
1824 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1825 if (kerr != KCGI_OK)
1826 return 0;
1827 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1828 KATTR_NAME, "theme-color",
1829 KATTR_CONTENT, "#ffffff", KATTR__MAX);
1830 if (kerr != KCGI_OK)
1831 return 0;
1832 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1833 if (kerr != KCGI_OK)
1834 return 0;
1835 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1836 KATTR_REL, "apple-touch-icon", KATTR_SIZES, "180x180",
1837 KATTR_HREF, "/apple-touch-icon.png", KATTR__MAX);
1838 if (kerr != KCGI_OK)
1839 return 0;
1840 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1841 if (kerr != KCGI_OK)
1842 return 0;
1843 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1844 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
1845 "32x32", KATTR_HREF, "/favicon-32x32.png", KATTR__MAX);
1846 if (kerr != KCGI_OK)
1847 return 0;
1848 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1849 if (kerr != KCGI_OK)
1850 return 0;
1851 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1852 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
1853 "16x16", KATTR_HREF, "/favicon-16x16.png", KATTR__MAX);
1854 if (kerr != KCGI_OK)
1855 return 0;
1856 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1857 if (kerr != KCGI_OK)
1858 return 0;
1859 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1860 KATTR_REL, "manifest", KATTR_HREF, "/site.webmanifest",
1861 KATTR__MAX);
1862 if (kerr != KCGI_OK)
1863 return 0;
1864 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1865 if (kerr != KCGI_OK)
1866 return 0;
1867 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1868 KATTR_REL, "mask-icon", KATTR_HREF,
1869 "/safari-pinned-tab.svg", KATTR__MAX);
1870 if (kerr != KCGI_OK)
1871 return 0;
1872 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1873 if (kerr != KCGI_OK)
1874 return 0;
1875 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1876 KATTR_REL, "stylesheet", KATTR_TYPE, "text/css",
1877 KATTR_HREF, "/gotweb.css", KATTR__MAX);
1878 if (kerr != KCGI_OK)
1879 return 0;
1880 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1881 if (kerr != KCGI_OK)
1882 return 0;
1883 break;
1884 case(TEMPL_HEADER):
1885 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1886 KATTR_ID, "got_link", KATTR__MAX);
1887 if (kerr != KCGI_OK)
1888 return 0;
1889 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1890 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
1891 KATTR_TARGET, "_sotd", KATTR__MAX);
1892 if (kerr != KCGI_OK)
1893 return 0;
1894 if (asprintf(&img_src, "/%s",
1895 gw_trans->gw_conf->got_logo) == -1)
1896 return 0;
1897 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
1898 KATTR_SRC, img_src, KATTR__MAX);
1899 if (kerr != KCGI_OK) {
1900 free(img_src);
1901 return 0;
1903 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1904 if (kerr != KCGI_OK) {
1905 free(img_src);
1906 return 0;
1908 break;
1909 case (TEMPL_SITEPATH):
1910 error = gw_output_site_link(gw_trans);
1911 if (error)
1912 return 0;
1913 break;
1914 case(TEMPL_TITLE):
1915 if (gw_trans->gw_conf->got_site_name != NULL) {
1916 kerr = khtml_puts(gw_trans->gw_html_req,
1917 gw_trans->gw_conf->got_site_name);
1918 if (kerr != KCGI_OK)
1919 return 0;
1921 break;
1922 case (TEMPL_SEARCH):
1923 break;
1924 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1925 "search", KATTR__MAX);
1926 if (kerr != KCGI_OK)
1927 return 0;
1928 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
1929 KATTR_METHOD, "POST", KATTR__MAX);
1930 if (kerr != KCGI_OK)
1931 return 0;
1932 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
1933 "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
1934 KATTR_MAXLENGTH, "50", KATTR__MAX);
1935 if (kerr != KCGI_OK)
1936 return 0;
1937 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
1938 KATTR__MAX);
1939 if (kerr != KCGI_OK)
1940 return 0;
1941 kerr = khtml_puts(gw_trans->gw_html_req, "Search");
1942 if (kerr != KCGI_OK)
1943 return 0;
1944 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
1945 if (kerr != KCGI_OK)
1946 return 0;
1947 break;
1948 case(TEMPL_SITEOWNER):
1949 if (gw_trans->gw_conf->got_site_owner != NULL &&
1950 gw_trans->gw_conf->got_show_site_owner) {
1951 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1952 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
1953 if (kerr != KCGI_OK)
1954 return 0;
1955 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1956 KATTR_ID, "site_owner", KATTR__MAX);
1957 if (kerr != KCGI_OK)
1958 return 0;
1959 kerr = khtml_puts(gw_trans->gw_html_req,
1960 gw_trans->gw_conf->got_site_owner);
1961 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1962 if (kerr != KCGI_OK)
1963 return 0;
1965 break;
1966 case(TEMPL_CONTENT):
1967 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1968 if (error) {
1969 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1970 KATTR_ID, "tmpl_err", KATTR__MAX);
1971 if (kerr != KCGI_OK)
1972 return 0;
1973 kerr = khttp_puts(gw_trans->gw_req, "Error: ");
1974 if (kerr != KCGI_OK)
1975 return 0;
1976 kerr = khttp_puts(gw_trans->gw_req, error->msg);
1977 if (kerr != KCGI_OK)
1978 return 0;
1979 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1980 if (kerr != KCGI_OK)
1981 return 0;
1983 break;
1984 default:
1985 return 0;
1987 return 1;
1990 static const struct got_error *
1991 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
1993 const struct got_error *error = NULL;
1994 enum kcgi_err kerr = KCGI_OK;
1996 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1997 KATTR_ID, "header_commit_title", KATTR__MAX);
1998 if (kerr != KCGI_OK)
1999 goto done;
2000 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
2001 if (kerr != KCGI_OK)
2002 goto done;
2003 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2004 if (kerr != KCGI_OK)
2005 goto done;
2006 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2007 KATTR_ID, "header_commit", KATTR__MAX);
2008 if (kerr != KCGI_OK)
2009 goto done;
2010 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2011 if (kerr != KCGI_OK)
2012 goto done;
2013 kerr = khtml_puts(gw_trans->gw_html_req, " ");
2014 if (kerr != KCGI_OK)
2015 goto done;
2016 if (str2 != NULL) {
2017 kerr = khtml_puts(gw_trans->gw_html_req, "(");
2018 if (kerr != KCGI_OK)
2019 goto done;
2020 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2021 if (kerr != KCGI_OK)
2022 goto done;
2023 kerr = khtml_puts(gw_trans->gw_html_req, ")");
2024 if (kerr != KCGI_OK)
2025 goto done;
2027 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2028 done:
2029 if (error == NULL && kerr != KCGI_OK)
2030 error = gw_kcgi_error(kerr);
2031 return error;
2034 static const struct got_error *
2035 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2037 const struct got_error *error = NULL;
2038 enum kcgi_err kerr = KCGI_OK;
2040 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2041 KATTR_ID, "header_diff_title", KATTR__MAX);
2042 if (kerr != KCGI_OK)
2043 goto done;
2044 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2045 if (kerr != KCGI_OK)
2046 goto done;
2047 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2048 if (kerr != KCGI_OK)
2049 goto done;
2050 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2051 KATTR_ID, "header_diff", KATTR__MAX);
2052 if (kerr != KCGI_OK)
2053 goto done;
2054 if (str1 != NULL) {
2055 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2056 if (kerr != KCGI_OK)
2057 goto done;
2059 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2060 if (kerr != KCGI_OK)
2061 goto done;
2062 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2063 if (kerr != KCGI_OK)
2064 goto done;
2065 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2066 done:
2067 if (error == NULL && kerr != KCGI_OK)
2068 error = gw_kcgi_error(kerr);
2069 return error;
2072 static const struct got_error *
2073 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2075 const struct got_error *error = NULL;
2076 enum kcgi_err kerr = KCGI_OK;
2078 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2079 KATTR_ID, "header_age_title", KATTR__MAX);
2080 if (kerr != KCGI_OK)
2081 goto done;
2082 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2083 if (kerr != KCGI_OK)
2084 goto done;
2085 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2086 if (kerr != KCGI_OK)
2087 goto done;
2088 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2089 KATTR_ID, "header_age", KATTR__MAX);
2090 if (kerr != KCGI_OK)
2091 goto done;
2092 kerr = khtml_puts(gw_trans->gw_html_req, str);
2093 if (kerr != KCGI_OK)
2094 goto done;
2095 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2096 done:
2097 if (error == NULL && kerr != KCGI_OK)
2098 error = gw_kcgi_error(kerr);
2099 return error;
2102 static const struct got_error *
2103 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2105 const struct got_error *error = NULL;
2106 enum kcgi_err kerr;
2108 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2109 KATTR_ID, "header_author_title", KATTR__MAX);
2110 if (kerr != KCGI_OK)
2111 goto done;
2112 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2113 if (kerr != KCGI_OK)
2114 goto done;
2115 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2116 if (kerr != KCGI_OK)
2117 goto done;
2118 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2119 KATTR_ID, "header_author", KATTR__MAX);
2120 if (kerr != KCGI_OK)
2121 goto done;
2122 kerr = khtml_puts(gw_trans->gw_html_req, str);
2123 if (kerr != KCGI_OK)
2124 goto done;
2125 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2126 done:
2127 if (error == NULL && kerr != KCGI_OK)
2128 error = gw_kcgi_error(kerr);
2129 return error;
2132 static const struct got_error *
2133 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2135 const struct got_error *error = NULL;
2136 enum kcgi_err kerr;
2138 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2139 KATTR_ID, "header_committer_title", KATTR__MAX);
2140 if (kerr != KCGI_OK)
2141 goto done;
2142 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2143 if (kerr != KCGI_OK)
2144 goto done;
2145 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2146 if (kerr != KCGI_OK)
2147 goto done;
2148 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2149 KATTR_ID, "header_committer", KATTR__MAX);
2150 if (kerr != KCGI_OK)
2151 goto done;
2152 kerr = khtml_puts(gw_trans->gw_html_req, str);
2153 if (kerr != KCGI_OK)
2154 goto done;
2155 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2156 done:
2157 if (error == NULL && kerr != KCGI_OK)
2158 error = gw_kcgi_error(kerr);
2159 return error;
2162 static const struct got_error *
2163 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2165 const struct got_error *error = NULL;
2166 enum kcgi_err kerr;
2168 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2169 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2170 if (kerr != KCGI_OK)
2171 goto done;
2172 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2173 if (kerr != KCGI_OK)
2174 goto done;
2175 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2176 if (kerr != KCGI_OK)
2177 goto done;
2178 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2179 KATTR_ID, "header_commit_msg", KATTR__MAX);
2180 if (kerr != KCGI_OK)
2181 goto done;
2182 kerr = khttp_puts(gw_trans->gw_req, str);
2183 if (kerr != KCGI_OK)
2184 goto done;
2185 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2186 done:
2187 if (error == NULL && kerr != KCGI_OK)
2188 error = gw_kcgi_error(kerr);
2189 return error;
2192 static const struct got_error *
2193 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2195 const struct got_error *error = NULL;
2196 enum kcgi_err kerr;
2198 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2199 KATTR_ID, "header_tree_title", KATTR__MAX);
2200 if (kerr != KCGI_OK)
2201 goto done;
2202 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2203 if (kerr != KCGI_OK)
2204 goto done;
2205 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2206 if (kerr != KCGI_OK)
2207 goto done;
2208 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2209 KATTR_ID, "header_tree", KATTR__MAX);
2210 if (kerr != KCGI_OK)
2211 goto done;
2212 kerr = khtml_puts(gw_trans->gw_html_req, str);
2213 if (kerr != KCGI_OK)
2214 goto done;
2215 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2216 done:
2217 if (error == NULL && kerr != KCGI_OK)
2218 error = gw_kcgi_error(kerr);
2219 return error;
2222 static const struct got_error *
2223 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2224 char *dir)
2226 const struct got_error *error = NULL;
2227 FILE *f = NULL;
2228 char *d_file = NULL;
2229 unsigned int len;
2230 size_t n;
2232 *description = NULL;
2233 if (gw_trans->gw_conf->got_show_repo_description == 0)
2234 return NULL;
2236 if (asprintf(&d_file, "%s/description", dir) == -1)
2237 return got_error_from_errno("asprintf");
2239 f = fopen(d_file, "r");
2240 if (f == NULL) {
2241 if (errno == ENOENT || errno == EACCES)
2242 return NULL;
2243 error = got_error_from_errno2("fopen", d_file);
2244 goto done;
2247 if (fseek(f, 0, SEEK_END) == -1) {
2248 error = got_ferror(f, GOT_ERR_IO);
2249 goto done;
2251 len = ftell(f);
2252 if (len == -1) {
2253 error = got_ferror(f, GOT_ERR_IO);
2254 goto done;
2256 if (fseek(f, 0, SEEK_SET) == -1) {
2257 error = got_ferror(f, GOT_ERR_IO);
2258 goto done;
2260 *description = calloc(len + 1, sizeof(**description));
2261 if (*description == NULL) {
2262 error = got_error_from_errno("calloc");
2263 goto done;
2266 n = fread(*description, 1, len, f);
2267 if (n == 0 && ferror(f))
2268 error = got_ferror(f, GOT_ERR_IO);
2269 done:
2270 if (f != NULL && fclose(f) == -1 && error == NULL)
2271 error = got_error_from_errno("fclose");
2272 free(d_file);
2273 return error;
2276 static const struct got_error *
2277 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2279 struct tm tm;
2280 time_t diff_time;
2281 char *years = "years ago", *months = "months ago";
2282 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2283 char *minutes = "minutes ago", *seconds = "seconds ago";
2284 char *now = "right now";
2285 char *s;
2286 char datebuf[29];
2288 *repo_age = NULL;
2290 switch (ref_tm) {
2291 case TM_DIFF:
2292 diff_time = time(NULL) - committer_time;
2293 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2294 if (asprintf(repo_age, "%lld %s",
2295 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2296 return got_error_from_errno("asprintf");
2297 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2298 if (asprintf(repo_age, "%lld %s",
2299 (diff_time / 60 / 60 / 24 / (365 / 12)),
2300 months) == -1)
2301 return got_error_from_errno("asprintf");
2302 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2303 if (asprintf(repo_age, "%lld %s",
2304 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2305 return got_error_from_errno("asprintf");
2306 } else if (diff_time > 60 * 60 * 24 * 2) {
2307 if (asprintf(repo_age, "%lld %s",
2308 (diff_time / 60 / 60 / 24), days) == -1)
2309 return got_error_from_errno("asprintf");
2310 } else if (diff_time > 60 * 60 * 2) {
2311 if (asprintf(repo_age, "%lld %s",
2312 (diff_time / 60 / 60), hours) == -1)
2313 return got_error_from_errno("asprintf");
2314 } else if (diff_time > 60 * 2) {
2315 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2316 minutes) == -1)
2317 return got_error_from_errno("asprintf");
2318 } else if (diff_time > 2) {
2319 if (asprintf(repo_age, "%lld %s", diff_time,
2320 seconds) == -1)
2321 return got_error_from_errno("asprintf");
2322 } else {
2323 if (asprintf(repo_age, "%s", now) == -1)
2324 return got_error_from_errno("asprintf");
2326 break;
2327 case TM_LONG:
2328 if (gmtime_r(&committer_time, &tm) == NULL)
2329 return got_error_from_errno("gmtime_r");
2331 s = asctime_r(&tm, datebuf);
2332 if (s == NULL)
2333 return got_error_from_errno("asctime_r");
2335 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2336 return got_error_from_errno("asprintf");
2337 break;
2339 return NULL;
2342 static const struct got_error *
2343 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2344 char *repo_ref, int ref_tm)
2346 const struct got_error *error = NULL;
2347 struct got_object_id *id = NULL;
2348 struct got_repository *repo = NULL;
2349 struct got_commit_object *commit = NULL;
2350 struct got_reflist_head refs;
2351 struct got_reflist_entry *re;
2352 struct got_reference *head_ref;
2353 int is_head = 0;
2354 time_t committer_time = 0, cmp_time = 0;
2355 const char *refname;
2357 *repo_age = NULL;
2358 SIMPLEQ_INIT(&refs);
2360 if (repo_ref == NULL)
2361 return NULL;
2363 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
2364 is_head = 1;
2366 if (gw_trans->gw_conf->got_show_repo_age == 0)
2367 return NULL;
2369 error = got_repo_open(&repo, dir, NULL);
2370 if (error)
2371 goto done;
2373 if (is_head)
2374 error = got_ref_list(&refs, repo, "refs/heads",
2375 got_ref_cmp_by_name, NULL);
2376 else
2377 error = got_ref_list(&refs, repo, repo_ref,
2378 got_ref_cmp_by_name, NULL);
2379 if (error)
2380 goto done;
2382 SIMPLEQ_FOREACH(re, &refs, entry) {
2383 if (is_head) {
2384 refname = strdup(repo_ref);
2385 if (refname == NULL) {
2386 error = got_error_from_errno("strdup");
2387 goto done;
2389 } else {
2390 refname = got_ref_get_name(re->ref);
2391 if (refname == NULL) {
2392 error = got_error_from_errno("strdup");
2393 goto done;
2396 error = got_ref_open(&head_ref, repo, refname, 0);
2397 if (error)
2398 goto done;
2400 error = got_ref_resolve(&id, repo, head_ref);
2401 got_ref_close(head_ref);
2402 if (error)
2403 goto done;
2405 error = got_object_open_as_commit(&commit, repo, id);
2406 if (error)
2407 goto done;
2409 committer_time =
2410 got_object_commit_get_committer_time(commit);
2412 if (cmp_time < committer_time)
2413 cmp_time = committer_time;
2416 if (cmp_time != 0) {
2417 committer_time = cmp_time;
2418 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2420 done:
2421 got_ref_list_free(&refs);
2422 free(id);
2423 return error;
2426 static const struct got_error *
2427 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2429 const struct got_error *error;
2430 FILE *f = NULL;
2431 struct got_object_id *id1 = NULL, *id2 = NULL;
2432 char *label1 = NULL, *label2 = NULL, *line = NULL;
2433 int obj_type;
2434 size_t linesize = 0;
2435 ssize_t linelen;
2436 enum kcgi_err kerr = KCGI_OK;
2438 f = got_opentemp();
2439 if (f == NULL)
2440 return NULL;
2442 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
2443 if (error)
2444 goto done;
2446 if (header->parent_id != NULL &&
2447 strncmp(header->parent_id, "/dev/null", 9) != 0) {
2448 error = got_repo_match_object_id(&id1, &label1,
2449 header->parent_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2450 if (error)
2451 goto done;
2454 error = got_repo_match_object_id(&id2, &label2,
2455 header->commit_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2456 if (error)
2457 goto done;
2459 error = got_object_get_type(&obj_type, header->repo, id2);
2460 if (error)
2461 goto done;
2462 switch (obj_type) {
2463 case GOT_OBJ_TYPE_BLOB:
2464 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
2465 header->repo, f);
2466 break;
2467 case GOT_OBJ_TYPE_TREE:
2468 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
2469 header->repo, f);
2470 break;
2471 case GOT_OBJ_TYPE_COMMIT:
2472 error = got_diff_objects_as_commits(id1, id2, 3, 0,
2473 header->repo, f);
2474 break;
2475 default:
2476 error = got_error(GOT_ERR_OBJ_TYPE);
2478 if (error)
2479 goto done;
2481 if (fseek(f, 0, SEEK_SET) == -1) {
2482 error = got_ferror(f, GOT_ERR_IO);
2483 goto done;
2486 while ((linelen = getline(&line, &linesize, f)) != -1) {
2487 error = gw_colordiff_line(gw_trans, line);
2488 if (error)
2489 goto done;
2490 /* XXX: KHTML_PRETTY breaks this */
2491 kerr = khtml_puts(gw_trans->gw_html_req, line);
2492 if (kerr != KCGI_OK)
2493 goto done;
2494 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2495 if (kerr != KCGI_OK)
2496 goto done;
2498 if (linelen == -1 && ferror(f))
2499 error = got_error_from_errno("getline");
2500 done:
2501 if (f && fclose(f) == -1 && error == NULL)
2502 error = got_error_from_errno("fclose");
2503 free(line);
2504 free(label1);
2505 free(label2);
2506 free(id1);
2507 free(id2);
2509 if (error == NULL && kerr != KCGI_OK)
2510 error = gw_kcgi_error(kerr);
2511 return error;
2514 static const struct got_error *
2515 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2517 const struct got_error *error = NULL;
2518 struct got_repository *repo;
2519 const char *gitconfig_owner;
2521 *owner = NULL;
2523 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2524 return NULL;
2526 error = got_repo_open(&repo, dir, NULL);
2527 if (error)
2528 return error;
2529 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2530 if (gitconfig_owner) {
2531 *owner = strdup(gitconfig_owner);
2532 if (*owner == NULL)
2533 error = got_error_from_errno("strdup");
2535 got_repo_close(repo);
2536 return error;
2539 static const struct got_error *
2540 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2542 const struct got_error *error = NULL;
2543 FILE *f;
2544 char *d_file = NULL;
2545 unsigned int len;
2546 size_t n;
2548 *url = NULL;
2550 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2551 return got_error_from_errno("asprintf");
2553 f = fopen(d_file, "r");
2554 if (f == NULL) {
2555 if (errno != ENOENT && errno != EACCES)
2556 error = got_error_from_errno2("fopen", d_file);
2557 goto done;
2560 if (fseek(f, 0, SEEK_END) == -1) {
2561 error = got_ferror(f, GOT_ERR_IO);
2562 goto done;
2564 len = ftell(f);
2565 if (len == -1) {
2566 error = got_ferror(f, GOT_ERR_IO);
2567 goto done;
2569 if (fseek(f, 0, SEEK_SET) == -1) {
2570 error = got_ferror(f, GOT_ERR_IO);
2571 goto done;
2574 *url = calloc(len + 1, sizeof(**url));
2575 if (*url == NULL) {
2576 error = got_error_from_errno("calloc");
2577 goto done;
2580 n = fread(*url, 1, len, f);
2581 if (n == 0 && ferror(f))
2582 error = got_ferror(f, GOT_ERR_IO);
2583 done:
2584 if (f && fclose(f) == -1 && error == NULL)
2585 error = got_error_from_errno("fclose");
2586 free(d_file);
2587 return NULL;
2590 static const struct got_error *
2591 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
2592 int limit, int tag_type)
2594 const struct got_error *error = NULL;
2595 struct got_repository *repo = NULL;
2596 struct got_reflist_head refs;
2597 struct got_reflist_entry *re;
2598 char *age = NULL;
2599 char *id_str = NULL, *newline, *href_commits = NULL;
2600 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
2601 struct got_tag_object *tag = NULL;
2602 enum kcgi_err kerr = KCGI_OK;
2603 int summary_header_displayed = 0;
2605 SIMPLEQ_INIT(&refs);
2607 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2608 if (error)
2609 return error;
2611 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
2612 if (error)
2613 goto done;
2615 SIMPLEQ_FOREACH(re, &refs, entry) {
2616 const char *refname;
2617 const char *tagger;
2618 const char *tag_commit;
2619 time_t tagger_time;
2620 struct got_object_id *id;
2621 struct got_commit_object *commit = NULL;
2623 refname = got_ref_get_name(re->ref);
2624 if (strncmp(refname, "refs/tags/", 10) != 0)
2625 continue;
2626 refname += 10;
2628 error = got_ref_resolve(&id, repo, re->ref);
2629 if (error)
2630 goto done;
2632 error = got_object_open_as_tag(&tag, repo, id);
2633 if (error) {
2634 if (error->code != GOT_ERR_OBJ_TYPE) {
2635 free(id);
2636 goto done;
2638 /* "lightweight" tag */
2639 error = got_object_open_as_commit(&commit, repo, id);
2640 if (error) {
2641 free(id);
2642 goto done;
2644 tagger = got_object_commit_get_committer(commit);
2645 tagger_time =
2646 got_object_commit_get_committer_time(commit);
2647 error = got_object_id_str(&id_str, id);
2648 free(id);
2649 } else {
2650 free(id);
2651 tagger = got_object_tag_get_tagger(tag);
2652 tagger_time = got_object_tag_get_tagger_time(tag);
2653 error = got_object_id_str(&id_str,
2654 got_object_tag_get_object_id(tag));
2656 if (error)
2657 goto done;
2659 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
2660 strlen(id_str)) != 0)
2661 continue;
2663 if (commit) {
2664 error = got_object_commit_get_logmsg(&tag_commit0,
2665 commit);
2666 if (error)
2667 goto done;
2668 got_object_commit_close(commit);
2669 } else {
2670 tag_commit0 = strdup(got_object_tag_get_message(tag));
2671 if (tag_commit0 == NULL) {
2672 error = got_error_from_errno("strdup");
2673 goto done;
2677 tag_commit = tag_commit0;
2678 while (*tag_commit == '\n')
2679 tag_commit++;
2681 switch (tag_type) {
2682 case TAGBRIEF:
2683 newline = strchr(tag_commit, '\n');
2684 if (newline)
2685 *newline = '\0';
2687 if (summary_header_displayed == 0) {
2688 kerr = khtml_attr(gw_trans->gw_html_req,
2689 KELEM_DIV, KATTR_ID,
2690 "summary_tags_title_wrapper", KATTR__MAX);
2691 if (kerr != KCGI_OK)
2692 goto done;
2693 kerr = khtml_attr(gw_trans->gw_html_req,
2694 KELEM_DIV, KATTR_ID,
2695 "summary_tags_title", KATTR__MAX);
2696 if (kerr != KCGI_OK)
2697 goto done;
2698 kerr = khtml_puts(gw_trans->gw_html_req,
2699 "Tags");
2700 if (kerr != KCGI_OK)
2701 goto done;
2702 kerr = khtml_closeelem(gw_trans->gw_html_req,
2703 2);
2704 if (kerr != KCGI_OK)
2705 goto done;
2706 kerr = khtml_attr(gw_trans->gw_html_req,
2707 KELEM_DIV, KATTR_ID,
2708 "summary_tags_content", KATTR__MAX);
2709 if (kerr != KCGI_OK)
2710 goto done;
2711 summary_header_displayed = 1;
2714 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2715 KATTR_ID, "tags_wrapper", KATTR__MAX);
2716 if (kerr != KCGI_OK)
2717 goto done;
2718 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2719 KATTR_ID, "tags_age", KATTR__MAX);
2720 if (kerr != KCGI_OK)
2721 goto done;
2722 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
2723 if (error)
2724 goto done;
2725 kerr = khtml_puts(gw_trans->gw_html_req,
2726 age ? age : "");
2727 if (kerr != KCGI_OK)
2728 goto done;
2729 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2730 if (kerr != KCGI_OK)
2731 goto done;
2732 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2733 KATTR_ID, "tags", KATTR__MAX);
2734 if (kerr != KCGI_OK)
2735 goto done;
2736 kerr = khtml_puts(gw_trans->gw_html_req, refname);
2737 if (kerr != KCGI_OK)
2738 goto done;
2739 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2740 if (kerr != KCGI_OK)
2741 goto done;
2742 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2743 KATTR_ID, "tags_name", KATTR__MAX);
2744 if (kerr != KCGI_OK)
2745 goto done;
2746 if (asprintf(&href_tag, "?path=%s&action=tag&commit=%s",
2747 gw_trans->repo_name, id_str) == -1) {
2748 error = got_error_from_errno("asprintf");
2749 goto done;
2751 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2752 KATTR_HREF, href_tag, KATTR__MAX);
2753 if (kerr != KCGI_OK)
2754 goto done;
2755 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
2756 if (kerr != KCGI_OK)
2757 goto done;
2758 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2759 if (kerr != KCGI_OK)
2760 goto done;
2762 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2763 KATTR_ID, "navs_wrapper", KATTR__MAX);
2764 if (kerr != KCGI_OK)
2765 goto done;
2766 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2767 KATTR_ID, "navs", KATTR__MAX);
2768 if (kerr != KCGI_OK)
2769 goto done;
2771 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2772 KATTR_HREF, href_tag, KATTR__MAX);
2773 if (kerr != KCGI_OK)
2774 goto done;
2775 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
2776 if (kerr != KCGI_OK)
2777 goto done;
2778 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2779 if (kerr != KCGI_OK)
2780 goto done;
2782 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2783 if (kerr != KCGI_OK)
2784 goto done;
2785 if (asprintf(&href_briefs,
2786 "?path=%s&action=briefs&commit=%s",
2787 gw_trans->repo_name, id_str) == -1) {
2788 error = got_error_from_errno("asprintf");
2789 goto done;
2791 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2792 KATTR_HREF, href_briefs, KATTR__MAX);
2793 if (kerr != KCGI_OK)
2794 goto done;
2795 kerr = khtml_puts(gw_trans->gw_html_req,
2796 "commit briefs");
2797 if (kerr != KCGI_OK)
2798 goto done;
2799 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2800 if (kerr != KCGI_OK)
2801 goto done;
2803 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2804 if (kerr != KCGI_OK)
2805 goto done;
2807 if (asprintf(&href_commits,
2808 "?path=%s&action=commits&commit=%s",
2809 gw_trans->repo_name, id_str) == -1) {
2810 error = got_error_from_errno("asprintf");
2811 goto done;
2813 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2814 KATTR_HREF, href_commits, KATTR__MAX);
2815 if (kerr != KCGI_OK)
2816 goto done;
2817 kerr = khtml_puts(gw_trans->gw_html_req,
2818 "commits");
2819 if (kerr != KCGI_OK)
2820 goto done;
2821 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2822 if (kerr != KCGI_OK)
2823 goto done;
2825 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2826 KATTR_ID, "dotted_line", KATTR__MAX);
2827 if (kerr != KCGI_OK)
2828 goto done;
2829 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2830 if (kerr != KCGI_OK)
2831 goto done;
2832 break;
2833 case TAGFULL:
2834 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2835 KATTR_ID, "tag_info_date_title", KATTR__MAX);
2836 if (kerr != KCGI_OK)
2837 goto done;
2838 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
2839 if (kerr != KCGI_OK)
2840 goto done;
2841 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2842 if (kerr != KCGI_OK)
2843 goto done;
2844 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2845 KATTR_ID, "tag_info_date", KATTR__MAX);
2846 if (kerr != KCGI_OK)
2847 goto done;
2848 error = gw_get_time_str(&age, tagger_time, TM_LONG);
2849 if (error)
2850 goto done;
2851 kerr = khtml_puts(gw_trans->gw_html_req,
2852 age ? age : "");
2853 if (kerr != KCGI_OK)
2854 goto done;
2855 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2856 if (kerr != KCGI_OK)
2857 goto done;
2859 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2860 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
2861 if (kerr != KCGI_OK)
2862 goto done;
2863 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
2864 if (kerr != KCGI_OK)
2865 goto done;
2866 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2867 if (kerr != KCGI_OK)
2868 goto done;
2869 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2870 KATTR_ID, "tag_info_date", KATTR__MAX);
2871 if (kerr != KCGI_OK)
2872 goto done;
2873 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
2874 if (kerr != KCGI_OK)
2875 goto done;
2876 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2877 if (kerr != KCGI_OK)
2878 goto done;
2880 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2881 KATTR_ID, "tag_info", KATTR__MAX);
2882 if (kerr != KCGI_OK)
2883 goto done;
2884 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
2885 if (kerr != KCGI_OK)
2886 goto done;
2887 break;
2888 default:
2889 break;
2891 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2892 if (kerr != KCGI_OK)
2893 goto done;
2895 if (limit && --limit == 0)
2896 break;
2898 if (tag)
2899 got_object_tag_close(tag);
2900 tag = NULL;
2901 free(id_str);
2902 id_str = NULL;
2903 free(age);
2904 age = NULL;
2905 free(tag_commit0);
2906 tag_commit0 = NULL;
2907 free(href_tag);
2908 href_tag = NULL;
2909 free(href_briefs);
2910 href_briefs = NULL;
2911 free(href_commits);
2912 href_commits = NULL;
2914 done:
2915 if (tag)
2916 got_object_tag_close(tag);
2917 free(id_str);
2918 free(age);
2919 free(tag_commit0);
2920 free(href_tag);
2921 free(href_briefs);
2922 free(href_commits);
2923 got_ref_list_free(&refs);
2924 if (repo)
2925 got_repo_close(repo);
2926 if (error == NULL && kerr != KCGI_OK)
2927 error = gw_kcgi_error(kerr);
2928 return error;
2931 static void
2932 gw_free_headers(struct gw_header *header)
2934 free(header->id);
2935 free(header->path);
2936 if (header->commit != NULL)
2937 got_object_commit_close(header->commit);
2938 if (header->repo)
2939 got_repo_close(header->repo);
2940 free(header->refs_str);
2941 free(header->commit_id);
2942 free(header->parent_id);
2943 free(header->tree_id);
2944 free(header->commit_msg);
2947 static struct gw_header *
2948 gw_init_header()
2950 struct gw_header *header;
2952 header = malloc(sizeof(*header));
2953 if (header == NULL)
2954 return NULL;
2956 header->repo = NULL;
2957 header->commit = NULL;
2958 header->id = NULL;
2959 header->path = NULL;
2960 SIMPLEQ_INIT(&header->refs);
2962 header->refs_str = NULL;
2963 header->commit_id = NULL;
2964 header->parent_id = NULL;
2965 header->tree_id = NULL;
2966 header->commit_msg = NULL;
2968 return header;
2971 static const struct got_error *
2972 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
2973 int limit)
2975 const struct got_error *error = NULL;
2976 struct got_commit_graph *graph = NULL;
2978 error = got_commit_graph_open(&graph, header->path, 0);
2979 if (error)
2980 return error;
2982 error = got_commit_graph_iter_start(graph, header->id, header->repo,
2983 NULL, NULL);
2984 if (error)
2985 goto done;
2987 for (;;) {
2988 error = got_commit_graph_iter_next(&header->id, graph,
2989 header->repo, NULL, NULL);
2990 if (error) {
2991 if (error->code == GOT_ERR_ITER_COMPLETED)
2992 error = NULL;
2993 goto done;
2995 if (header->id == NULL)
2996 goto done;
2998 error = got_object_open_as_commit(&header->commit, header->repo,
2999 header->id);
3000 if (error)
3001 goto done;
3003 error = gw_get_commit(gw_trans, header);
3004 if (limit > 1) {
3005 struct gw_header *n_header = NULL;
3006 if ((n_header = gw_init_header()) == NULL) {
3007 error = got_error_from_errno("malloc");
3008 goto done;
3011 if (header->refs_str != NULL) {
3012 n_header->refs_str = strdup(header->refs_str);
3013 if (n_header->refs_str == NULL) {
3014 error = got_error_from_errno("strdup");
3015 goto done;
3018 n_header->commit_id = strdup(header->commit_id);
3019 if (n_header->commit_id == NULL) {
3020 error = got_error_from_errno("strdup");
3021 goto done;
3023 if (header->parent_id != NULL) {
3024 n_header->parent_id = strdup(header->parent_id);
3025 if (n_header->parent_id == NULL) {
3026 error = got_error_from_errno("strdup");
3027 goto done;
3030 n_header->tree_id = strdup(header->tree_id);
3031 if (n_header->tree_id == NULL) {
3032 error = got_error_from_errno("strdup");
3033 goto done;
3035 n_header->author = header->author;
3036 n_header->committer = header->committer;
3037 n_header->commit_msg = strdup(header->commit_msg);
3038 if (n_header->commit_msg == NULL) {
3039 error = got_error_from_errno("strdup");
3040 goto done;
3042 n_header->committer_time = header->committer_time;
3043 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3044 entry);
3046 if (error || (limit && --limit == 0))
3047 break;
3049 done:
3050 if (graph)
3051 got_commit_graph_close(graph);
3052 return error;
3055 static const struct got_error *
3056 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
3058 const struct got_error *error = NULL;
3059 struct got_reflist_entry *re;
3060 struct got_object_id *id2 = NULL;
3061 struct got_object_qid *parent_id;
3062 char *refs_str = NULL, *commit_msg = NULL, *commit_msg0;
3064 /*print commit*/
3065 SIMPLEQ_FOREACH(re, &header->refs, entry) {
3066 char *s;
3067 const char *name;
3068 struct got_tag_object *tag = NULL;
3069 int cmp;
3071 name = got_ref_get_name(re->ref);
3072 if (strcmp(name, GOT_REF_HEAD) == 0)
3073 continue;
3074 if (strncmp(name, "refs/", 5) == 0)
3075 name += 5;
3076 if (strncmp(name, "got/", 4) == 0)
3077 continue;
3078 if (strncmp(name, "heads/", 6) == 0)
3079 name += 6;
3080 if (strncmp(name, "remotes/", 8) == 0)
3081 name += 8;
3082 if (strncmp(name, "tags/", 5) == 0) {
3083 error = got_object_open_as_tag(&tag, header->repo,
3084 re->id);
3085 if (error) {
3086 if (error->code != GOT_ERR_OBJ_TYPE)
3087 continue;
3089 * Ref points at something other
3090 * than a tag.
3092 error = NULL;
3093 tag = NULL;
3096 cmp = got_object_id_cmp(tag ?
3097 got_object_tag_get_object_id(tag) : re->id, header->id);
3098 if (tag)
3099 got_object_tag_close(tag);
3100 if (cmp != 0)
3101 continue;
3102 s = refs_str;
3103 if (asprintf(&refs_str, "%s%s%s", s ? s : "",
3104 s ? ", " : "", name) == -1) {
3105 error = got_error_from_errno("asprintf");
3106 free(s);
3107 return error;
3109 free(s);
3110 header->refs_str = strdup(refs_str);
3111 if (header->refs_str == NULL) {
3112 error = got_error_from_errno("strdup");
3113 return error;
3117 free(refs_str);
3119 error = got_object_id_str(&header->commit_id, header->id);
3120 if (error)
3121 return error;
3123 error = got_object_id_str(&header->tree_id,
3124 got_object_commit_get_tree_id(header->commit));
3125 if (error)
3126 return error;
3128 if (gw_trans->action == GW_DIFF) {
3129 parent_id = SIMPLEQ_FIRST(
3130 got_object_commit_get_parent_ids(header->commit));
3131 if (parent_id != NULL) {
3132 id2 = got_object_id_dup(parent_id->id);
3133 free (parent_id);
3134 error = got_object_id_str(&header->parent_id, id2);
3135 if (error)
3136 return error;
3137 free(id2);
3138 } else {
3139 header->parent_id = strdup("/dev/null");
3140 if (header->parent_id == NULL) {
3141 error = got_error_from_errno("strdup");
3142 return error;
3147 header->committer_time =
3148 got_object_commit_get_committer_time(header->commit);
3150 header->author =
3151 got_object_commit_get_author(header->commit);
3152 header->committer =
3153 got_object_commit_get_committer(header->commit);
3154 if (error)
3155 return error;
3156 error = got_object_commit_get_logmsg(&commit_msg0, header->commit);
3157 if (error)
3158 return error;
3160 commit_msg = commit_msg0;
3161 while (*commit_msg == '\n')
3162 commit_msg++;
3164 header->commit_msg = strdup(commit_msg);
3165 if (header->commit_msg == NULL)
3166 error = got_error_from_errno("strdup");
3167 free(commit_msg0);
3168 return error;
3171 static const struct got_error *
3172 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3174 const struct got_error *error = NULL;
3175 char *in_repo_path = NULL;
3177 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
3178 if (error)
3179 return error;
3181 if (gw_trans->commit == NULL) {
3182 struct got_reference *head_ref;
3183 error = got_ref_open(&head_ref, header->repo,
3184 gw_trans->headref, 0);
3185 if (error)
3186 return error;
3188 error = got_ref_resolve(&header->id, header->repo, head_ref);
3189 got_ref_close(head_ref);
3190 if (error)
3191 return error;
3193 error = got_object_open_as_commit(&header->commit,
3194 header->repo, header->id);
3195 } else {
3196 struct got_reference *ref;
3197 error = got_ref_open(&ref, header->repo, gw_trans->commit, 0);
3198 if (error == NULL) {
3199 int obj_type;
3200 error = got_ref_resolve(&header->id, header->repo, ref);
3201 got_ref_close(ref);
3202 if (error)
3203 return error;
3204 error = got_object_get_type(&obj_type, header->repo,
3205 header->id);
3206 if (error)
3207 return error;
3208 if (obj_type == GOT_OBJ_TYPE_TAG) {
3209 struct got_tag_object *tag;
3210 error = got_object_open_as_tag(&tag,
3211 header->repo, header->id);
3212 if (error)
3213 return error;
3214 if (got_object_tag_get_object_type(tag) !=
3215 GOT_OBJ_TYPE_COMMIT) {
3216 got_object_tag_close(tag);
3217 error = got_error(GOT_ERR_OBJ_TYPE);
3218 return error;
3220 free(header->id);
3221 header->id = got_object_id_dup(
3222 got_object_tag_get_object_id(tag));
3223 if (header->id == NULL)
3224 error = got_error_from_errno(
3225 "got_object_id_dup");
3226 got_object_tag_close(tag);
3227 if (error)
3228 return error;
3229 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3230 error = got_error(GOT_ERR_OBJ_TYPE);
3231 return error;
3233 error = got_object_open_as_commit(&header->commit,
3234 header->repo, header->id);
3235 if (error)
3236 return error;
3238 if (header->commit == NULL) {
3239 error = got_repo_match_object_id_prefix(&header->id,
3240 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
3241 header->repo);
3242 if (error)
3243 return error;
3245 error = got_repo_match_object_id_prefix(&header->id,
3246 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
3247 header->repo);
3250 error = got_repo_map_path(&in_repo_path, header->repo,
3251 gw_trans->repo_path, 1);
3252 if (error)
3253 return error;
3255 if (in_repo_path) {
3256 header->path = strdup(in_repo_path);
3257 if (header->path == NULL) {
3258 error = got_error_from_errno("strdup");
3259 free(in_repo_path);
3260 return error;
3263 free(in_repo_path);
3265 error = got_ref_list(&header->refs, header->repo, NULL,
3266 got_ref_cmp_by_name, NULL);
3267 if (error)
3268 return error;
3270 error = gw_get_commits(gw_trans, header, limit);
3271 return error;
3274 struct blame_line {
3275 int annotated;
3276 char *id_str;
3277 char *committer;
3278 char datebuf[11]; /* YYYY-MM-DD + NUL */
3281 struct gw_blame_cb_args {
3282 struct blame_line *lines;
3283 int nlines;
3284 int nlines_prec;
3285 int lineno_cur;
3286 off_t *line_offsets;
3287 FILE *f;
3288 struct got_repository *repo;
3289 struct gw_trans *gw_trans;
3292 static const struct got_error *
3293 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3295 const struct got_error *err = NULL;
3296 struct gw_blame_cb_args *a = arg;
3297 struct blame_line *bline;
3298 char *line = NULL;
3299 size_t linesize = 0;
3300 struct got_commit_object *commit = NULL;
3301 off_t offset;
3302 struct tm tm;
3303 time_t committer_time;
3304 enum kcgi_err kerr = KCGI_OK;
3306 if (nlines != a->nlines ||
3307 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3308 return got_error(GOT_ERR_RANGE);
3310 if (lineno == -1)
3311 return NULL; /* no change in this commit */
3313 /* Annotate this line. */
3314 bline = &a->lines[lineno - 1];
3315 if (bline->annotated)
3316 return NULL;
3317 err = got_object_id_str(&bline->id_str, id);
3318 if (err)
3319 return err;
3321 err = got_object_open_as_commit(&commit, a->repo, id);
3322 if (err)
3323 goto done;
3325 bline->committer = strdup(got_object_commit_get_committer(commit));
3326 if (bline->committer == NULL) {
3327 err = got_error_from_errno("strdup");
3328 goto done;
3331 committer_time = got_object_commit_get_committer_time(commit);
3332 if (localtime_r(&committer_time, &tm) == NULL)
3333 return got_error_from_errno("localtime_r");
3334 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3335 &tm) >= sizeof(bline->datebuf)) {
3336 err = got_error(GOT_ERR_NO_SPACE);
3337 goto done;
3339 bline->annotated = 1;
3341 /* Print lines annotated so far. */
3342 bline = &a->lines[a->lineno_cur - 1];
3343 if (!bline->annotated)
3344 goto done;
3346 offset = a->line_offsets[a->lineno_cur - 1];
3347 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3348 err = got_error_from_errno("fseeko");
3349 goto done;
3352 while (bline->annotated) {
3353 char *smallerthan, *at, *nl, *committer;
3354 char *lineno = NULL, *href_diff = NULL, *href_link = NULL;
3355 size_t len;
3357 if (getline(&line, &linesize, a->f) == -1) {
3358 if (ferror(a->f))
3359 err = got_error_from_errno("getline");
3360 break;
3363 committer = bline->committer;
3364 smallerthan = strchr(committer, '<');
3365 if (smallerthan && smallerthan[1] != '\0')
3366 committer = smallerthan + 1;
3367 at = strchr(committer, '@');
3368 if (at)
3369 *at = '\0';
3370 len = strlen(committer);
3371 if (len >= 9)
3372 committer[8] = '\0';
3374 nl = strchr(line, '\n');
3375 if (nl)
3376 *nl = '\0';
3378 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3379 "blame_wrapper", KATTR__MAX);
3380 if (kerr != KCGI_OK)
3381 goto err;
3382 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3383 "blame_number", KATTR__MAX);
3384 if (kerr != KCGI_OK)
3385 goto err;
3386 if (asprintf(&lineno, "%.*d", a->nlines_prec,
3387 a->lineno_cur) == -1)
3388 goto err;
3389 kerr = khtml_puts(a->gw_trans->gw_html_req, lineno);
3390 if (kerr != KCGI_OK)
3391 goto err;
3392 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3393 if (kerr != KCGI_OK)
3394 goto err;
3396 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3397 "blame_hash", KATTR__MAX);
3398 if (kerr != KCGI_OK)
3399 goto err;
3400 /* XXX repo_file could be NULL if not present in querystring */
3401 if (asprintf(&href_diff,
3402 "?path=%s&action=diff&commit=%s&file=%s&folder=%s",
3403 a->gw_trans->repo_name, bline->id_str,
3404 a->gw_trans->repo_file, a->gw_trans->repo_folder ?
3405 a->gw_trans->repo_folder : "") == -1) {
3406 err = got_error_from_errno("asprintf");
3407 goto err;
3409 if (asprintf(&href_link, "%.8s", bline->id_str) == -1) {
3410 err = got_error_from_errno("asprintf");
3411 goto err;
3413 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3414 KATTR_HREF, href_diff, KATTR__MAX);
3415 if (kerr != KCGI_OK)
3416 goto done;
3417 kerr = khtml_puts(a->gw_trans->gw_html_req, href_link);
3418 if (kerr != KCGI_OK)
3419 goto err;
3420 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3421 if (kerr != KCGI_OK)
3422 goto err;
3424 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3425 "blame_date", KATTR__MAX);
3426 if (kerr != KCGI_OK)
3427 goto err;
3428 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3429 if (kerr != KCGI_OK)
3430 goto err;
3431 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3432 if (kerr != KCGI_OK)
3433 goto err;
3435 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3436 "blame_author", KATTR__MAX);
3437 if (kerr != KCGI_OK)
3438 goto err;
3439 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
3440 if (kerr != KCGI_OK)
3441 goto err;
3442 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3443 if (kerr != KCGI_OK)
3444 goto err;
3446 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3447 "blame_code", KATTR__MAX);
3448 if (kerr != KCGI_OK)
3449 goto err;
3450 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
3451 if (kerr != KCGI_OK)
3452 goto err;
3453 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3454 if (kerr != KCGI_OK)
3455 goto err;
3457 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3458 if (kerr != KCGI_OK)
3459 goto err;
3461 a->lineno_cur++;
3462 bline = &a->lines[a->lineno_cur - 1];
3463 err:
3464 free(lineno);
3465 free(href_diff);
3466 free(href_link);
3468 done:
3469 if (commit)
3470 got_object_commit_close(commit);
3471 free(line);
3472 if (err == NULL && kerr != KCGI_OK)
3473 err = gw_kcgi_error(kerr);
3474 return err;
3477 static const struct got_error *
3478 gw_output_file_blame(struct gw_trans *gw_trans)
3480 const struct got_error *error = NULL;
3481 struct got_repository *repo = NULL;
3482 struct got_object_id *obj_id = NULL;
3483 struct got_object_id *commit_id = NULL;
3484 struct got_blob_object *blob = NULL;
3485 char *path = NULL, *in_repo_path = NULL;
3486 struct gw_blame_cb_args bca;
3487 int i, obj_type;
3488 size_t filesize;
3490 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3491 if (error)
3492 return error;
3494 /* XXX repo_file could be NULL if not present in querystring */
3495 if (asprintf(&path, "%s%s%s",
3496 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3497 gw_trans->repo_folder ? "/" : "",
3498 gw_trans->repo_file) == -1) {
3499 error = got_error_from_errno("asprintf");
3500 goto done;
3503 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3504 if (error)
3505 goto done;
3507 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3508 GOT_OBJ_TYPE_COMMIT, 1, repo);
3509 if (error)
3510 goto done;
3512 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3513 if (error)
3514 goto done;
3516 if (obj_id == NULL) {
3517 error = got_error(GOT_ERR_NO_OBJ);
3518 goto done;
3521 error = got_object_get_type(&obj_type, repo, obj_id);
3522 if (error)
3523 goto done;
3525 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3526 error = got_error(GOT_ERR_OBJ_TYPE);
3527 goto done;
3530 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3531 if (error)
3532 goto done;
3534 bca.f = got_opentemp();
3535 if (bca.f == NULL) {
3536 error = got_error_from_errno("got_opentemp");
3537 goto done;
3539 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3540 &bca.line_offsets, bca.f, blob);
3541 if (error || bca.nlines == 0)
3542 goto done;
3544 /* Don't include \n at EOF in the blame line count. */
3545 if (bca.line_offsets[bca.nlines - 1] == filesize)
3546 bca.nlines--;
3548 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3549 if (bca.lines == NULL) {
3550 error = got_error_from_errno("calloc");
3551 goto done;
3553 bca.lineno_cur = 1;
3554 bca.nlines_prec = 0;
3555 i = bca.nlines;
3556 while (i > 0) {
3557 i /= 10;
3558 bca.nlines_prec++;
3560 bca.repo = repo;
3561 bca.gw_trans = gw_trans;
3563 error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
3564 NULL, NULL);
3565 done:
3566 free(bca.line_offsets);
3567 free(in_repo_path);
3568 free(commit_id);
3569 free(obj_id);
3570 free(path);
3572 for (i = 0; i < bca.nlines; i++) {
3573 struct blame_line *bline = &bca.lines[i];
3574 free(bline->id_str);
3575 free(bline->committer);
3577 free(bca.lines);
3578 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3579 error = got_error_from_errno("fclose");
3580 if (blob)
3581 got_object_blob_close(blob);
3582 if (repo)
3583 got_repo_close(repo);
3584 return error;
3587 static const struct got_error *
3588 gw_output_blob_buf(struct gw_trans *gw_trans)
3590 const struct got_error *error = NULL;
3591 struct got_repository *repo = NULL;
3592 struct got_object_id *obj_id = NULL;
3593 struct got_object_id *commit_id = NULL;
3594 struct got_blob_object *blob = NULL;
3595 char *path = NULL, *in_repo_path = NULL;
3596 int obj_type, set_mime = 0;
3597 size_t len, hdrlen;
3598 const uint8_t *buf;
3599 enum kcgi_err kerr = KCGI_OK;
3601 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3602 if (error)
3603 return error;
3605 /* XXX repo_file could be NULL if not present in querystring */
3606 if (asprintf(&path, "%s%s%s",
3607 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3608 gw_trans->repo_folder ? "/" : "",
3609 gw_trans->repo_file) == -1) {
3610 error = got_error_from_errno("asprintf");
3611 goto done;
3614 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3615 if (error)
3616 goto done;
3618 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3619 GOT_OBJ_TYPE_COMMIT, 1, repo);
3620 if (error)
3621 goto done;
3623 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3624 if (error)
3625 goto done;
3627 if (obj_id == NULL) {
3628 error = got_error(GOT_ERR_NO_OBJ);
3629 goto done;
3632 error = got_object_get_type(&obj_type, repo, obj_id);
3633 if (error)
3634 goto done;
3636 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3637 error = got_error(GOT_ERR_OBJ_TYPE);
3638 goto done;
3641 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3642 if (error)
3643 goto done;
3645 hdrlen = got_object_blob_get_hdrlen(blob);
3646 do {
3647 error = got_object_blob_read_block(&len, blob);
3648 if (error)
3649 goto done;
3650 buf = got_object_blob_get_read_buf(blob);
3653 * Skip blob object header first time around,
3654 * which also contains a zero byte.
3656 buf += hdrlen;
3657 if (set_mime == 0) {
3658 if (isbinary(buf, len - hdrlen))
3659 gw_trans->mime = KMIME_APP_OCTET_STREAM;
3660 else
3661 gw_trans->mime = KMIME_TEXT_PLAIN;
3662 set_mime = 1;
3663 error = gw_display_index(gw_trans);
3664 if (error)
3665 goto done;
3667 khttp_write(gw_trans->gw_req, buf, len - hdrlen);
3668 hdrlen = 0;
3669 } while (len != 0);
3670 done:
3671 free(in_repo_path);
3672 free(commit_id);
3673 free(obj_id);
3674 free(path);
3675 if (blob)
3676 got_object_blob_close(blob);
3677 if (repo)
3678 got_repo_close(repo);
3679 if (error == NULL && kerr != KCGI_OK)
3680 error = gw_kcgi_error(kerr);
3681 return error;
3684 static const struct got_error *
3685 gw_output_repo_tree(struct gw_trans *gw_trans)
3687 const struct got_error *error = NULL;
3688 struct got_repository *repo = NULL;
3689 struct got_object_id *tree_id = NULL, *commit_id = NULL;
3690 struct got_tree_object *tree = NULL;
3691 char *path = NULL, *in_repo_path = NULL;
3692 char *id_str = NULL;
3693 char *build_folder = NULL;
3694 char *href_blob = NULL, *href_blame = NULL;
3695 const char *class = NULL;
3696 int nentries, i, class_flip = 0;
3697 enum kcgi_err kerr = KCGI_OK;
3699 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3700 if (error)
3701 return error;
3703 if (gw_trans->repo_folder != NULL) {
3704 path = strdup(gw_trans->repo_folder);
3705 if (path == NULL) {
3706 error = got_error_from_errno("strdup");
3707 goto done;
3709 } else {
3710 error = got_repo_map_path(&in_repo_path, repo,
3711 gw_trans->repo_path, 1);
3712 if (error)
3713 goto done;
3714 free(path);
3715 path = in_repo_path;
3718 if (gw_trans->commit == NULL) {
3719 struct got_reference *head_ref;
3720 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
3721 if (error)
3722 goto done;
3723 error = got_ref_resolve(&commit_id, repo, head_ref);
3724 if (error)
3725 goto done;
3726 got_ref_close(head_ref);
3728 } else {
3729 error = got_repo_match_object_id(&commit_id, NULL,
3730 gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
3731 if (error)
3732 goto done;
3736 * XXX gw_trans->commit might have already been allocated in
3737 * gw_parse_querystring(); could we more cleanly seperate values
3738 * we received as arguments from values we compute ourselves?
3740 error = got_object_id_str(&gw_trans->commit, commit_id);
3741 if (error)
3742 goto done;
3744 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
3745 if (error)
3746 goto done;
3748 error = got_object_open_as_tree(&tree, repo, tree_id);
3749 if (error)
3750 goto done;
3752 nentries = got_object_tree_get_nentries(tree);
3753 for (i = 0; i < nentries; i++) {
3754 struct got_tree_entry *te;
3755 const char *modestr = "";
3756 mode_t mode;
3758 te = got_object_tree_get_entry(tree, i);
3760 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
3761 if (error)
3762 goto done;
3764 mode = got_tree_entry_get_mode(te);
3765 if (got_object_tree_entry_is_submodule(te))
3766 modestr = "$";
3767 else if (S_ISLNK(mode))
3768 modestr = "@";
3769 else if (S_ISDIR(mode))
3770 modestr = "/";
3771 else if (mode & S_IXUSR)
3772 modestr = "*";
3774 if (class_flip == 0) {
3775 class = "back_lightgray";
3776 class_flip = 1;
3777 } else {
3778 class = "back_white";
3779 class_flip = 0;
3782 if (S_ISDIR(mode)) {
3783 if (asprintf(&build_folder, "%s/%s",
3784 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3785 got_tree_entry_get_name(te)) == -1) {
3786 error = got_error_from_errno(
3787 "asprintf");
3788 goto done;
3790 if (asprintf(&href_blob,
3791 "?path=%s&action=%s&commit=%s&folder=%s",
3792 gw_trans->repo_name, gw_get_action_name(gw_trans),
3793 gw_trans->commit, build_folder) == -1) {
3794 error = got_error_from_errno("asprintf");
3795 goto done;
3798 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3799 KATTR_ID, "tree_wrapper", KATTR__MAX);
3800 if (kerr != KCGI_OK)
3801 goto done;
3802 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3803 KATTR_ID, "tree_line", KATTR_CLASS, class,
3804 KATTR__MAX);
3805 if (kerr != KCGI_OK)
3806 goto done;
3807 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3808 KATTR_HREF, href_blob, KATTR_CLASS,
3809 "diff_directory", KATTR__MAX);
3810 if (kerr != KCGI_OK)
3811 goto done;
3812 kerr = khtml_puts(gw_trans->gw_html_req,
3813 got_tree_entry_get_name(te));
3814 if (kerr != KCGI_OK)
3815 goto done;
3816 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3817 if (kerr != KCGI_OK)
3818 goto done;
3819 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3820 if (kerr != KCGI_OK)
3821 goto done;
3822 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3823 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
3824 KATTR__MAX);
3825 if (kerr != KCGI_OK)
3826 goto done;
3827 kerr = khtml_entity(gw_trans->gw_html_req,
3828 KENTITY_nbsp);
3829 if (kerr != KCGI_OK)
3830 goto done;
3831 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3832 if (kerr != KCGI_OK)
3833 goto done;
3834 } else {
3835 if (asprintf(&href_blob,
3836 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3837 gw_trans->repo_name, "blob", 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;
3844 if (asprintf(&href_blame,
3845 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3846 gw_trans->repo_name, "blame", gw_trans->commit,
3847 got_tree_entry_get_name(te),
3848 gw_trans->repo_folder ?
3849 gw_trans->repo_folder : "") == -1) {
3850 error = got_error_from_errno("asprintf");
3851 goto done;
3854 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3855 KATTR_ID, "tree_wrapper", KATTR__MAX);
3856 if (kerr != KCGI_OK)
3857 goto done;
3858 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3859 KATTR_ID, "tree_line", KATTR_CLASS, class,
3860 KATTR__MAX);
3861 if (kerr != KCGI_OK)
3862 goto done;
3863 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3864 KATTR_HREF, href_blob, KATTR__MAX);
3865 if (kerr != KCGI_OK)
3866 goto done;
3867 kerr = khtml_puts(gw_trans->gw_html_req,
3868 got_tree_entry_get_name(te));
3869 if (kerr != KCGI_OK)
3870 goto done;
3871 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3872 if (kerr != KCGI_OK)
3873 goto done;
3874 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3875 if (kerr != KCGI_OK)
3876 goto done;
3877 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3878 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
3879 KATTR__MAX);
3880 if (kerr != KCGI_OK)
3881 goto done;
3883 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3884 KATTR_HREF, href_blob, KATTR__MAX);
3885 if (kerr != KCGI_OK)
3886 goto done;
3887 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
3888 if (kerr != KCGI_OK)
3889 goto done;
3890 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3891 if (kerr != KCGI_OK)
3892 goto done;
3894 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3895 if (kerr != KCGI_OK)
3896 goto done;
3898 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3899 KATTR_HREF, href_blame, KATTR__MAX);
3900 if (kerr != KCGI_OK)
3901 goto done;
3902 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
3903 if (kerr != KCGI_OK)
3904 goto done;
3906 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3907 if (kerr != KCGI_OK)
3908 goto done;
3910 free(id_str);
3911 id_str = NULL;
3912 free(href_blob);
3913 href_blob = NULL;
3914 free(build_folder);
3915 build_folder = NULL;
3917 done:
3918 if (tree)
3919 got_object_tree_close(tree);
3920 if (repo)
3921 got_repo_close(repo);
3922 free(id_str);
3923 free(href_blob);
3924 free(href_blame);
3925 free(in_repo_path);
3926 free(tree_id);
3927 free(build_folder);
3928 if (error == NULL && kerr != KCGI_OK)
3929 error = gw_kcgi_error(kerr);
3930 return error;
3933 static const struct got_error *
3934 gw_output_repo_heads(struct gw_trans *gw_trans)
3936 const struct got_error *error = NULL;
3937 struct got_repository *repo = NULL;
3938 struct got_reflist_head refs;
3939 struct got_reflist_entry *re;
3940 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
3941 char *href_commits = NULL;
3942 enum kcgi_err kerr = KCGI_OK;
3944 SIMPLEQ_INIT(&refs);
3946 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3947 if (error)
3948 goto done;
3950 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
3951 NULL);
3952 if (error)
3953 goto done;
3955 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3956 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
3957 if (kerr != KCGI_OK)
3958 goto done;
3959 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3960 KATTR_ID, "summary_heads_title", KATTR__MAX);
3961 if (kerr != KCGI_OK)
3962 goto done;
3963 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
3964 if (kerr != KCGI_OK)
3965 goto done;
3966 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3967 if (kerr != KCGI_OK)
3968 goto done;
3969 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3970 KATTR_ID, "summary_heads_content", KATTR__MAX);
3971 if (kerr != KCGI_OK)
3972 goto done;
3974 SIMPLEQ_FOREACH(re, &refs, entry) {
3975 char *refname;
3977 refname = strdup(got_ref_get_name(re->ref));
3978 if (refname == NULL) {
3979 error = got_error_from_errno("got_ref_to_str");
3980 goto done;
3983 if (strncmp(refname, "refs/heads/", 11) != 0) {
3984 free(refname);
3985 continue;
3988 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
3989 refname, TM_DIFF);
3990 if (error)
3991 goto done;
3993 if (strncmp(refname, "refs/heads/", 11) == 0)
3994 refname += 11;
3996 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3997 KATTR_ID, "heads_wrapper", KATTR__MAX);
3998 if (kerr != KCGI_OK)
3999 goto done;
4000 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4001 KATTR_ID, "heads_age", KATTR__MAX);
4002 if (kerr != KCGI_OK)
4003 goto done;
4004 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
4005 if (kerr != KCGI_OK)
4006 goto done;
4007 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4008 if (kerr != KCGI_OK)
4009 goto done;
4010 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4011 KATTR_ID, "heads_space", KATTR__MAX);
4012 if (kerr != KCGI_OK)
4013 goto done;
4014 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
4015 if (kerr != KCGI_OK)
4016 goto done;
4017 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4018 if (kerr != KCGI_OK)
4019 goto done;
4020 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4021 KATTR_ID, "head", KATTR__MAX);
4022 if (kerr != KCGI_OK)
4023 goto done;
4024 if (asprintf(&href_summary,
4025 "?path=%s&action=summary&headref=%s",
4026 gw_trans->repo_name, refname) == -1) {
4027 error = got_error_from_errno("asprintf");
4028 goto done;
4030 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4031 href_summary, KATTR__MAX);
4032 kerr = khtml_puts(gw_trans->gw_html_req, refname);
4033 if (kerr != KCGI_OK)
4034 goto done;
4035 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4036 if (kerr != KCGI_OK)
4037 goto done;
4039 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4040 "navs_wrapper", KATTR__MAX);
4041 if (kerr != KCGI_OK)
4042 goto done;
4043 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4044 "navs", KATTR__MAX);
4045 if (kerr != KCGI_OK)
4046 goto done;
4048 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4049 href_summary, KATTR__MAX);
4050 if (kerr != KCGI_OK)
4051 goto done;
4052 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4053 if (kerr != KCGI_OK)
4054 goto done;
4055 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4056 if (kerr != KCGI_OK)
4057 goto done;
4059 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4060 if (kerr != KCGI_OK)
4061 goto done;
4062 if (asprintf(&href_briefs, "?path=%s&action=briefs&headref=%s",
4063 gw_trans->repo_name, refname) == -1) {
4064 error = got_error_from_errno("asprintf");
4065 goto done;
4067 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4068 href_briefs, KATTR__MAX);
4069 if (kerr != KCGI_OK)
4070 goto done;
4071 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4072 if (kerr != KCGI_OK)
4073 goto done;
4074 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4075 if (kerr != KCGI_OK)
4076 goto done;
4078 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4079 if (kerr != KCGI_OK)
4080 goto done;
4082 if (asprintf(&href_commits,
4083 "?path=%s&action=commits&headref=%s",
4084 gw_trans->repo_name, refname) == -1) {
4085 error = got_error_from_errno("asprintf");
4086 goto done;
4088 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4089 href_commits, KATTR__MAX);
4090 if (kerr != KCGI_OK)
4091 goto done;
4092 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4093 if (kerr != KCGI_OK)
4094 goto done;
4095 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4096 if (kerr != KCGI_OK)
4097 goto done;
4099 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4100 "dotted_line", KATTR__MAX);
4101 if (kerr != KCGI_OK)
4102 goto done;
4103 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4104 if (kerr != KCGI_OK)
4105 goto done;
4106 free(href_summary);
4107 href_summary = NULL;
4108 free(href_briefs);
4109 href_briefs = NULL;
4110 free(href_commits);
4111 href_commits = NULL;
4113 done:
4114 got_ref_list_free(&refs);
4115 free(href_summary);
4116 free(href_briefs);
4117 free(href_commits);
4118 if (repo)
4119 got_repo_close(repo);
4120 return error;
4123 static const struct got_error *
4124 gw_output_site_link(struct gw_trans *gw_trans)
4126 const struct got_error *error = NULL;
4127 char *href_summary = NULL;
4128 enum kcgi_err kerr = KCGI_OK;
4130 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4131 "site_link", KATTR__MAX);
4132 if (kerr != KCGI_OK)
4133 goto done;
4134 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4135 KATTR__MAX);
4136 if (kerr != KCGI_OK)
4137 goto done;
4138 kerr = khtml_puts(gw_trans->gw_html_req,
4139 gw_trans->gw_conf->got_site_link);
4140 if (kerr != KCGI_OK)
4141 goto done;
4142 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4143 if (kerr != KCGI_OK)
4144 goto done;
4146 if (gw_trans->repo_name != NULL) {
4147 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4148 if (kerr != KCGI_OK)
4149 goto done;
4150 if (asprintf(&href_summary, "?path=%s&action=summary",
4151 gw_trans->repo_name) == -1)
4152 goto done;
4153 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4154 href_summary, KATTR__MAX);
4155 if (kerr != KCGI_OK)
4156 goto done;
4157 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4158 if (kerr != KCGI_OK)
4159 goto done;
4160 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4161 if (kerr != KCGI_OK)
4162 goto done;
4163 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4164 if (kerr != KCGI_OK)
4165 goto done;
4166 kerr = khtml_puts(gw_trans->gw_html_req,
4167 gw_get_action_name(gw_trans));
4168 if (kerr != KCGI_OK)
4169 goto done;
4172 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4173 if (kerr != KCGI_OK)
4174 goto done;
4175 done:
4176 free(href_summary);
4177 return error;
4180 static const struct got_error *
4181 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4183 const struct got_error *error = NULL;
4184 char *color = NULL;
4185 enum kcgi_err kerr = KCGI_OK;
4187 if (strncmp(buf, "-", 1) == 0)
4188 color = "diff_minus";
4189 else if (strncmp(buf, "+", 1) == 0)
4190 color = "diff_plus";
4191 else if (strncmp(buf, "@@", 2) == 0)
4192 color = "diff_chunk_header";
4193 else if (strncmp(buf, "@@", 2) == 0)
4194 color = "diff_chunk_header";
4195 else if (strncmp(buf, "commit +", 8) == 0)
4196 color = "diff_meta";
4197 else if (strncmp(buf, "commit -", 8) == 0)
4198 color = "diff_meta";
4199 else if (strncmp(buf, "blob +", 6) == 0)
4200 color = "diff_meta";
4201 else if (strncmp(buf, "blob -", 6) == 0)
4202 color = "diff_meta";
4203 else if (strncmp(buf, "file +", 6) == 0)
4204 color = "diff_meta";
4205 else if (strncmp(buf, "file -", 6) == 0)
4206 color = "diff_meta";
4207 else if (strncmp(buf, "from:", 5) == 0)
4208 color = "diff_author";
4209 else if (strncmp(buf, "via:", 4) == 0)
4210 color = "diff_author";
4211 else if (strncmp(buf, "date:", 5) == 0)
4212 color = "diff_date";
4213 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4214 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4215 if (error == NULL && kerr != KCGI_OK)
4216 error = gw_kcgi_error(kerr);
4217 return error;
4220 int
4221 main(int argc, char *argv[])
4223 const struct got_error *error = NULL;
4224 struct gw_trans *gw_trans;
4225 struct gw_dir *dir = NULL, *tdir;
4226 const char *page = "index";
4227 int gw_malloc = 1;
4228 enum kcgi_err kerr;
4230 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4231 errx(1, "malloc");
4233 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4234 errx(1, "malloc");
4236 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4237 errx(1, "malloc");
4239 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4240 errx(1, "malloc");
4242 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4243 if (kerr != KCGI_OK) {
4244 error = gw_kcgi_error(kerr);
4245 goto done;
4248 if ((gw_trans->gw_conf =
4249 malloc(sizeof(struct gotweb_conf))) == NULL) {
4250 gw_malloc = 0;
4251 error = got_error_from_errno("malloc");
4252 goto done;
4255 TAILQ_INIT(&gw_trans->gw_dirs);
4256 TAILQ_INIT(&gw_trans->gw_headers);
4258 gw_trans->page = 0;
4259 gw_trans->repos_total = 0;
4260 gw_trans->repo_path = NULL;
4261 gw_trans->commit = NULL;
4262 gw_trans->headref = strdup(GOT_REF_HEAD);
4263 if (gw_trans->headref == NULL) {
4264 error = got_error_from_errno("strdup");
4265 goto done;
4267 gw_trans->mime = KMIME_TEXT_HTML;
4268 gw_trans->gw_tmpl->key = gw_templs;
4269 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4270 gw_trans->gw_tmpl->arg = gw_trans;
4271 gw_trans->gw_tmpl->cb = gw_template;
4272 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
4273 if (error)
4274 goto done;
4276 error = gw_parse_querystring(gw_trans);
4277 if (error)
4278 goto done;
4280 if (gw_trans->action == GW_BLOB)
4281 error = gw_blob(gw_trans);
4282 else
4283 error = gw_display_index(gw_trans);
4284 done:
4285 if (error) {
4286 gw_trans->mime = KMIME_TEXT_PLAIN;
4287 gw_trans->action = GW_ERR;
4288 gw_display_error(gw_trans, error);
4290 if (gw_malloc) {
4291 free(gw_trans->gw_conf->got_repos_path);
4292 free(gw_trans->gw_conf->got_site_name);
4293 free(gw_trans->gw_conf->got_site_owner);
4294 free(gw_trans->gw_conf->got_site_link);
4295 free(gw_trans->gw_conf->got_logo);
4296 free(gw_trans->gw_conf->got_logo_url);
4297 free(gw_trans->gw_conf);
4298 free(gw_trans->commit);
4299 free(gw_trans->repo_path);
4300 free(gw_trans->headref);
4302 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4303 free(dir->name);
4304 free(dir->description);
4305 free(dir->age);
4306 free(dir->url);
4307 free(dir->path);
4308 free(dir);
4313 khttp_free(gw_trans->gw_req);
4314 return 0;