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 char *repo_name;
64 char *repo_path;
65 char *commit;
66 char *repo_file;
67 char *repo_folder;
68 char *action_name;
69 char *headref;
70 unsigned int action;
71 unsigned int page;
72 unsigned int repos_total;
73 enum kmime mime;
74 };
76 struct gw_header {
77 TAILQ_ENTRY(gw_header) entry;
78 struct got_repository *repo;
79 struct got_reflist_head refs;
80 struct got_commit_object *commit;
81 struct got_object_id *id;
82 char *path;
84 char *refs_str;
85 char *commit_id; /* id_str1 */
86 char *parent_id; /* id_str2 */
87 char *tree_id;
88 char *author;
89 char *committer;
90 char *commit_msg;
91 time_t committer_time;
92 };
94 struct gw_dir {
95 TAILQ_ENTRY(gw_dir) entry;
96 char *name;
97 char *owner;
98 char *description;
99 char *url;
100 char *age;
101 char *path;
102 };
104 enum gw_key {
105 KEY_ACTION,
106 KEY_COMMIT_ID,
107 KEY_FILE,
108 KEY_FOLDER,
109 KEY_HEADREF,
110 KEY_PAGE,
111 KEY_PATH,
112 KEY__ZMAX
113 };
115 enum gw_tmpl {
116 TEMPL_CONTENT,
117 TEMPL_HEAD,
118 TEMPL_HEADER,
119 TEMPL_SEARCH,
120 TEMPL_SITEPATH,
121 TEMPL_SITEOWNER,
122 TEMPL_TITLE,
123 TEMPL__MAX
124 };
126 enum gw_ref_tm {
127 TM_DIFF,
128 TM_LONG,
129 };
131 enum gw_tags {
132 TAGBRIEF,
133 TAGFULL,
134 };
136 static const char *const gw_templs[TEMPL__MAX] = {
137 "content",
138 "head",
139 "header",
140 "search",
141 "sitepath",
142 "siteowner",
143 "title",
144 };
146 static const struct kvalid gw_keys[KEY__ZMAX] = {
147 { kvalid_stringne, "action" },
148 { kvalid_stringne, "commit" },
149 { kvalid_stringne, "file" },
150 { kvalid_stringne, "folder" },
151 { kvalid_stringne, "headref" },
152 { kvalid_int, "page" },
153 { kvalid_stringne, "path" },
154 };
156 static const struct got_error *gw_init_gw_dir(struct gw_dir **, char *);
157 static struct gw_header *gw_init_header(void);
159 static void gw_free_headers(struct gw_header *);
160 static void gw_display_error(struct gw_trans *,
161 const struct got_error *);
163 static int gw_template(size_t, void *);
165 static const struct got_error *gw_strdup_string(char **, char *,
166 const char *);
167 static const struct got_error *gw_get_repo_description(char **,
168 struct gw_trans *, char *);
169 static const struct got_error *gw_get_repo_owner(char **, struct gw_trans *,
170 char *);
171 static const struct got_error *gw_get_time_str(char **, time_t, int);
172 static const struct got_error *gw_get_repo_age(char **, struct gw_trans *,
173 char *, char *, int);
174 static const struct got_error *gw_output_file_blame(struct gw_trans *);
175 static const struct got_error *gw_output_blob_buf(struct gw_trans *);
176 static const struct got_error *gw_output_repo_tree(struct gw_trans *);
177 static const struct got_error *gw_output_diff(struct gw_trans *,
178 struct gw_header *);
179 static const struct got_error *gw_output_repo_tags(struct gw_trans *,
180 struct gw_header *, int, int);
181 static const struct got_error *gw_output_repo_heads(struct gw_trans *);
182 static const struct got_error *gw_output_site_link(struct gw_trans *);
183 static const struct got_error *gw_get_clone_url(char **, struct gw_trans *,
184 char *);
185 static const struct got_error *gw_colordiff_line(struct gw_trans *, char *);
187 static const struct got_error *gw_gen_commit_header(struct gw_trans *, char *,
188 char*);
189 static const struct got_error *gw_gen_diff_header(struct gw_trans *, char *,
190 char*);
191 static const struct got_error *gw_gen_author_header(struct gw_trans *,
192 const char *);
193 static const struct got_error *gw_gen_age_header(struct gw_trans *,
194 const char *);
195 static const struct got_error *gw_gen_committer_header(struct gw_trans *,
196 const char *);
197 static const struct got_error *gw_gen_commit_msg_header(struct gw_trans*,
198 char *);
199 static const struct got_error *gw_gen_tree_header(struct gw_trans *, char *);
200 static const struct got_error *gw_display_open(struct gw_trans *, enum khttp,
201 enum kmime);
202 static const struct got_error *gw_display_index(struct gw_trans *);
203 static const struct got_error *gw_get_header(struct gw_trans *,
204 struct gw_header *, int);
205 static const struct got_error *gw_get_commits(struct gw_trans *,
206 struct gw_header *, int);
207 static const struct got_error *gw_get_commit(struct gw_trans *,
208 struct gw_header *);
209 static const struct got_error *gw_apply_unveil(const char *);
210 static const struct got_error *gw_blame_cb(void *, int, int,
211 struct got_object_id *);
212 static const struct got_error *gw_load_got_paths(struct gw_trans *);
213 static const struct got_error *gw_load_got_path(struct gw_trans *,
214 struct gw_dir *);
215 static const struct got_error *gw_parse_querystring(struct gw_trans *);
216 static const struct got_error *gw_blame(struct gw_trans *);
217 static const struct got_error *gw_blob(struct gw_trans *);
218 static const struct got_error *gw_diff(struct gw_trans *);
219 static const struct got_error *gw_index(struct gw_trans *);
220 static const struct got_error *gw_commits(struct gw_trans *);
221 static const struct got_error *gw_briefs(struct gw_trans *);
222 static const struct got_error *gw_summary(struct gw_trans *);
223 static const struct got_error *gw_tree(struct gw_trans *);
224 static const struct got_error *gw_tag(struct gw_trans *);
226 struct gw_query_action {
227 unsigned int func_id;
228 const char *func_name;
229 const struct got_error *(*func_main)(struct gw_trans *);
230 char *template;
231 };
233 enum gw_query_actions {
234 GW_BLAME,
235 GW_BLOB,
236 GW_BRIEFS,
237 GW_COMMITS,
238 GW_DIFF,
239 GW_ERR,
240 GW_INDEX,
241 GW_SUMMARY,
242 GW_TAG,
243 GW_TREE,
244 };
246 static struct gw_query_action gw_query_funcs[] = {
247 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
248 { GW_BLOB, "blob", NULL, NULL },
249 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
250 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
251 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
252 { GW_ERR, NULL, NULL, "gw_tmpl/err.tmpl" },
253 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
254 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
255 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
256 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
257 };
259 static const struct got_error *
260 gw_kcgi_error(enum kcgi_err kerr)
262 if (kerr == KCGI_OK)
263 return NULL;
265 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
266 return got_error(GOT_ERR_CANCELLED);
268 if (kerr == KCGI_ENOMEM)
269 return got_error_set_errno(ENOMEM,
270 kcgi_strerror(kerr));
272 if (kerr == KCGI_ENFILE)
273 return got_error_set_errno(ENFILE,
274 kcgi_strerror(kerr));
276 if (kerr == KCGI_EAGAIN)
277 return got_error_set_errno(EAGAIN,
278 kcgi_strerror(kerr));
280 if (kerr == KCGI_FORM)
281 return got_error_msg(GOT_ERR_IO,
282 kcgi_strerror(kerr));
284 return got_error_from_errno(kcgi_strerror(kerr));
287 static const struct got_error *
288 gw_apply_unveil(const char *repo_path)
290 const struct got_error *err;
292 if (repo_path && unveil(repo_path, "r") != 0)
293 return got_error_from_errno2("unveil", repo_path);
295 if (unveil("/tmp", "rwc") != 0)
296 return got_error_from_errno2("unveil", "/tmp");
298 err = got_privsep_unveil_exec_helpers();
299 if (err != NULL)
300 return err;
302 if (unveil(NULL, NULL) != 0)
303 return got_error_from_errno("unveil");
305 return NULL;
308 static const struct got_error *
309 gw_strdup_string(char **s, char *str1, const char *str2)
311 if (str1 && str2)
312 return got_error_from_errno("strdup");
313 if (str1)
314 *s = strdup(str1);
315 else
316 *s = strdup(str2);
317 if (*s == NULL)
318 return got_error_from_errno("strdup");
319 return NULL;
322 static int
323 isbinary(const uint8_t *buf, size_t n)
325 size_t i;
327 for (i = 0; i < n; i++)
328 if (buf[i] == 0)
329 return 1;
330 return 0;
333 static const struct got_error *
334 gw_blame(struct gw_trans *gw_trans)
336 const struct got_error *error = NULL;
337 struct gw_header *header = NULL;
338 char *age = NULL;
339 enum kcgi_err kerr;
341 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
342 NULL) == -1)
343 return got_error_from_errno("pledge");
345 if ((header = gw_init_header()) == NULL)
346 return got_error_from_errno("malloc");
348 error = gw_apply_unveil(gw_trans->gw_dir->path);
349 if (error)
350 goto done;
352 error = gw_get_header(gw_trans, header, 1);
353 if (error)
354 goto done;
356 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
357 "blame_header_wrapper", KATTR__MAX);
358 if (kerr != KCGI_OK)
359 goto done;
360 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
361 "blame_header", KATTR__MAX);
362 if (kerr != KCGI_OK)
363 goto done;
364 error = gw_get_time_str(&age, header->committer_time,
365 TM_LONG);
366 if (error)
367 goto done;
368 error = gw_gen_age_header(gw_trans, age ?age : "");
369 if (error)
370 goto done;
371 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
372 if (error)
373 goto done;
374 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
375 if (kerr != KCGI_OK)
376 goto done;
377 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
378 "dotted_line", KATTR__MAX);
379 if (kerr != KCGI_OK)
380 goto done;
381 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
382 if (kerr != KCGI_OK)
383 goto done;
385 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
386 "blame", KATTR__MAX);
387 if (kerr != KCGI_OK)
388 goto done;
389 error = gw_output_file_blame(gw_trans);
390 if (error)
391 goto done;
392 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
393 done:
394 got_ref_list_free(&header->refs);
395 gw_free_headers(header);
396 if (error == NULL && kerr != KCGI_OK)
397 error = gw_kcgi_error(kerr);
398 return error;
401 static const struct got_error *
402 gw_blob(struct gw_trans *gw_trans)
404 const struct got_error *error = NULL;
405 struct gw_header *header = NULL;
407 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
408 NULL) == -1)
409 return got_error_from_errno("pledge");
411 if ((header = gw_init_header()) == NULL)
412 return got_error_from_errno("malloc");
414 error = gw_apply_unveil(gw_trans->gw_dir->path);
415 if (error)
416 goto done;
418 error = gw_get_header(gw_trans, header, 1);
419 if (error)
420 goto done;
422 error = gw_output_blob_buf(gw_trans);
423 done:
424 got_ref_list_free(&header->refs);
425 gw_free_headers(header);
426 return error;
429 static const struct got_error *
430 gw_diff(struct gw_trans *gw_trans)
432 const struct got_error *error = NULL;
433 struct gw_header *header = NULL;
434 char *age = NULL;
435 enum kcgi_err kerr = KCGI_OK;
437 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
438 NULL) == -1)
439 return got_error_from_errno("pledge");
441 if ((header = gw_init_header()) == NULL)
442 return got_error_from_errno("malloc");
444 error = gw_apply_unveil(gw_trans->gw_dir->path);
445 if (error)
446 goto done;
448 error = gw_get_header(gw_trans, header, 1);
449 if (error)
450 goto done;
452 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
453 "diff_header_wrapper", KATTR__MAX);
454 if (kerr != KCGI_OK)
455 goto done;
456 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
457 "diff_header", KATTR__MAX);
458 if (kerr != KCGI_OK)
459 goto done;
460 error = gw_gen_diff_header(gw_trans, header->parent_id,
461 header->commit_id);
462 if (error)
463 goto done;
464 error = gw_gen_commit_header(gw_trans, header->commit_id,
465 header->refs_str);
466 if (error)
467 goto done;
468 error = gw_gen_tree_header(gw_trans, header->tree_id);
469 if (error)
470 goto done;
471 error = gw_gen_author_header(gw_trans, header->author);
472 if (error)
473 goto done;
474 error = gw_gen_committer_header(gw_trans, header->author);
475 if (error)
476 goto done;
477 error = gw_get_time_str(&age, header->committer_time,
478 TM_LONG);
479 if (error)
480 goto done;
481 error = gw_gen_age_header(gw_trans, age ?age : "");
482 if (error)
483 goto done;
484 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
485 if (error)
486 goto done;
487 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
488 if (kerr != KCGI_OK)
489 goto done;
490 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
491 "dotted_line", KATTR__MAX);
492 if (kerr != KCGI_OK)
493 goto done;
494 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
495 if (kerr != KCGI_OK)
496 goto done;
498 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
499 "diff", KATTR__MAX);
500 if (kerr != KCGI_OK)
501 goto done;
502 error = gw_output_diff(gw_trans, header);
503 if (error)
504 goto done;
506 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
507 done:
508 got_ref_list_free(&header->refs);
509 gw_free_headers(header);
510 free(age);
511 if (error == NULL && kerr != KCGI_OK)
512 error = gw_kcgi_error(kerr);
513 return error;
516 static const struct got_error *
517 gw_index(struct gw_trans *gw_trans)
519 const struct got_error *error = NULL;
520 struct gw_dir *gw_dir = NULL;
521 char *href_next = NULL, *href_prev = NULL, *href_summary = NULL;
522 char *href_briefs = NULL, *href_commits = NULL, *href_tree = NULL;
523 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
524 enum kcgi_err kerr;
526 if (pledge("stdio rpath proc exec sendfd unveil",
527 NULL) == -1) {
528 error = got_error_from_errno("pledge");
529 return error;
532 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path);
533 if (error)
534 return error;
536 error = gw_load_got_paths(gw_trans);
537 if (error)
538 return error;
540 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
541 "index_header", KATTR__MAX);
542 if (kerr != KCGI_OK)
543 return gw_kcgi_error(kerr);
544 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
545 "index_header_project", KATTR__MAX);
546 if (kerr != KCGI_OK)
547 return gw_kcgi_error(kerr);
548 kerr = khtml_puts(gw_trans->gw_html_req, "Project");
549 if (kerr != KCGI_OK)
550 return gw_kcgi_error(kerr);
551 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
552 if (kerr != KCGI_OK)
553 return gw_kcgi_error(kerr);
555 if (gw_trans->gw_conf->got_show_repo_description) {
556 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
557 "index_header_description", KATTR__MAX);
558 if (kerr != KCGI_OK)
559 return gw_kcgi_error(kerr);
560 kerr = khtml_puts(gw_trans->gw_html_req, "Description");
561 if (kerr != KCGI_OK)
562 return gw_kcgi_error(kerr);
563 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
564 if (kerr != KCGI_OK)
565 return gw_kcgi_error(kerr);
568 if (gw_trans->gw_conf->got_show_repo_owner) {
569 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
570 "index_header_owner", KATTR__MAX);
571 if (kerr != KCGI_OK)
572 return gw_kcgi_error(kerr);
573 kerr = khtml_puts(gw_trans->gw_html_req, "Owner");
574 if (kerr != KCGI_OK)
575 return gw_kcgi_error(kerr);
576 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
577 if (kerr != KCGI_OK)
578 return gw_kcgi_error(kerr);
581 if (gw_trans->gw_conf->got_show_repo_age) {
582 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
583 "index_header_age", KATTR__MAX);
584 if (kerr != KCGI_OK)
585 return gw_kcgi_error(kerr);
586 kerr = khtml_puts(gw_trans->gw_html_req, "Last Change");
587 if (kerr != KCGI_OK)
588 return gw_kcgi_error(kerr);
589 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
590 if (kerr != KCGI_OK)
591 return gw_kcgi_error(kerr);
594 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
595 if (kerr != KCGI_OK)
596 return gw_kcgi_error(kerr);
598 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
599 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
600 "index_wrapper", KATTR__MAX);
601 if (kerr != KCGI_OK)
602 return gw_kcgi_error(kerr);
603 kerr = khtml_puts(gw_trans->gw_html_req,
604 "No repositories found in ");
605 if (kerr != KCGI_OK)
606 return gw_kcgi_error(kerr);
607 kerr = khtml_puts(gw_trans->gw_html_req,
608 gw_trans->gw_conf->got_repos_path);
609 if (kerr != KCGI_OK)
610 return gw_kcgi_error(kerr);
611 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
612 if (kerr != KCGI_OK)
613 return gw_kcgi_error(kerr);
614 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
615 "dotted_line", KATTR__MAX);
616 if (kerr != KCGI_OK)
617 return gw_kcgi_error(kerr);
618 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
619 if (kerr != KCGI_OK)
620 return gw_kcgi_error(kerr);
621 return error;
624 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
625 dir_c++;
627 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
628 if (gw_trans->page > 0 && (gw_trans->page *
629 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
630 prev_disp++;
631 continue;
634 prev_disp++;
636 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
637 "index_wrapper", KATTR__MAX);
638 if (kerr != KCGI_OK)
639 goto done;
641 if (asprintf(&href_summary, "?path=%s&action=summary",
642 gw_dir->name) == -1) {
643 error = got_error_from_errno("asprintf");
644 goto done;
646 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
647 "index_project", KATTR__MAX);
648 if (kerr != KCGI_OK)
649 goto done;
650 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
651 href_summary, KATTR__MAX);
652 if (kerr != KCGI_OK)
653 goto done;
654 kerr = khtml_puts(gw_trans->gw_html_req, gw_dir->name);
655 if (kerr != KCGI_OK)
656 goto done;
657 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
658 if (kerr != KCGI_OK)
659 goto done;
660 if (gw_trans->gw_conf->got_show_repo_description) {
661 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
662 KATTR_ID, "index_project_description", KATTR__MAX);
663 if (kerr != KCGI_OK)
664 goto done;
665 kerr = khtml_puts(gw_trans->gw_html_req,
666 gw_dir->description ? gw_dir->description : "");
667 if (kerr != KCGI_OK)
668 goto done;
669 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
670 if (kerr != KCGI_OK)
671 goto done;
673 if (gw_trans->gw_conf->got_show_repo_owner) {
674 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
675 KATTR_ID, "index_project_owner", KATTR__MAX);
676 if (kerr != KCGI_OK)
677 goto done;
678 kerr = khtml_puts(gw_trans->gw_html_req,
679 gw_dir->owner ? gw_dir->owner : "");
680 if (kerr != KCGI_OK)
681 goto done;
682 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
683 if (kerr != KCGI_OK)
684 goto done;
686 if (gw_trans->gw_conf->got_show_repo_age) {
687 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
688 KATTR_ID, "index_project_age", KATTR__MAX);
689 if (kerr != KCGI_OK)
690 goto done;
691 kerr = khtml_puts(gw_trans->gw_html_req,
692 gw_dir->age ? gw_dir->age : "");
693 if (kerr != KCGI_OK)
694 goto done;
695 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
696 if (kerr != KCGI_OK)
697 goto done;
700 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
701 "navs_wrapper", KATTR__MAX);
702 if (kerr != KCGI_OK)
703 goto done;
704 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
705 "navs", KATTR__MAX);
706 if (kerr != KCGI_OK)
707 goto done;
709 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
710 href_summary, KATTR__MAX);
711 if (kerr != KCGI_OK)
712 goto done;
713 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
714 if (kerr != KCGI_OK)
715 goto done;
716 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
717 if (kerr != KCGI_OK)
718 goto done;
720 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
721 if (kerr != KCGI_OK)
722 goto done;
724 if (asprintf(&href_briefs, "?path=%s&action=briefs",
725 gw_dir->name) == -1) {
726 error = got_error_from_errno("asprintf");
727 goto done;
729 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
730 href_briefs, KATTR__MAX);
731 if (kerr != KCGI_OK)
732 goto done;
733 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
734 if (kerr != KCGI_OK)
735 goto done;
736 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
737 if (kerr != KCGI_OK)
738 error = gw_kcgi_error(kerr);
740 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
741 if (kerr != KCGI_OK)
742 goto done;
744 if (asprintf(&href_commits, "?path=%s&action=commits",
745 gw_dir->name) == -1) {
746 error = got_error_from_errno("asprintf");
747 goto done;
749 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
750 href_commits, KATTR__MAX);
751 if (kerr != KCGI_OK)
752 goto done;
753 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
754 if (kerr != KCGI_OK)
755 goto done;
756 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
757 if (kerr != KCGI_OK)
758 goto done;
760 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
761 if (kerr != KCGI_OK)
762 goto done;
764 if (asprintf(&href_tree, "?path=%s&action=tree",
765 gw_dir->name) == -1) {
766 error = got_error_from_errno("asprintf");
767 goto done;
769 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
770 href_tree, KATTR__MAX);
771 if (kerr != KCGI_OK)
772 goto done;
773 kerr = khtml_puts(gw_trans->gw_html_req, "tree");
774 if (kerr != KCGI_OK)
775 goto done;
777 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
778 if (kerr != KCGI_OK)
779 goto done;
780 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
781 "dotted_line", KATTR__MAX);
782 if (kerr != KCGI_OK)
783 goto done;
784 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
785 if (kerr != KCGI_OK)
786 goto done;
788 free(href_summary);
789 href_summary = NULL;
790 free(href_briefs);
791 href_briefs = NULL;
792 free(href_commits);
793 href_commits = NULL;
794 free(href_tree);
795 href_tree = NULL;
797 if (gw_trans->gw_conf->got_max_repos_display == 0)
798 continue;
800 if (next_disp == gw_trans->gw_conf->got_max_repos_display) {
801 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
802 KATTR_ID, "np_wrapper", KATTR__MAX);
803 if (kerr != KCGI_OK)
804 goto done;
805 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
806 KATTR_ID, "nav_prev", KATTR__MAX);
807 if (kerr != KCGI_OK)
808 goto done;
809 } else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
810 (gw_trans->page > 0) &&
811 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
812 prev_disp == gw_trans->repos_total)) {
813 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
814 KATTR_ID, "np_wrapper", KATTR__MAX);
815 if (kerr != KCGI_OK)
816 goto done;
817 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
818 KATTR_ID, "nav_prev", KATTR__MAX);
819 if (kerr != KCGI_OK)
820 goto done;
823 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
824 (gw_trans->page > 0) &&
825 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
826 prev_disp == gw_trans->repos_total)) {
827 if (asprintf(&href_prev, "?page=%d",
828 gw_trans->page - 1) == -1) {
829 error = got_error_from_errno("asprintf");
830 goto done;
832 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
833 KATTR_HREF, href_prev, KATTR__MAX);
834 if (kerr != KCGI_OK)
835 goto done;
836 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
837 if (kerr != KCGI_OK)
838 goto done;
839 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
840 if (kerr != KCGI_OK)
841 goto done;
844 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
845 if (kerr != KCGI_OK)
846 return gw_kcgi_error(kerr);
848 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
849 next_disp == gw_trans->gw_conf->got_max_repos_display &&
850 dir_c != (gw_trans->page + 1) *
851 gw_trans->gw_conf->got_max_repos_display) {
852 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
853 KATTR_ID, "nav_next", KATTR__MAX);
854 if (kerr != KCGI_OK)
855 goto done;
856 if (asprintf(&href_next, "?page=%d",
857 gw_trans->page + 1) == -1) {
858 error = got_error_from_errno("calloc");
859 goto done;
861 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
862 KATTR_HREF, href_next, KATTR__MAX);
863 if (kerr != KCGI_OK)
864 goto done;
865 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
866 if (kerr != KCGI_OK)
867 goto done;
868 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
869 if (kerr != KCGI_OK)
870 goto done;
871 next_disp = 0;
872 break;
875 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
876 (gw_trans->page > 0) &&
877 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
878 prev_disp == gw_trans->repos_total)) {
879 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
880 if (kerr != KCGI_OK)
881 goto done;
883 next_disp++;
885 done:
886 free(href_prev);
887 free(href_next);
888 free(href_summary);
889 free(href_briefs);
890 free(href_commits);
891 free(href_tree);
892 if (error == NULL && kerr != KCGI_OK)
893 error = gw_kcgi_error(kerr);
894 return error;
897 static const struct got_error *
898 gw_commits(struct gw_trans *gw_trans)
900 const struct got_error *error = NULL;
901 struct gw_header *header = NULL, *n_header = NULL;
902 char *age = NULL;
903 char *href_diff = NULL, *href_blob = NULL;
904 enum kcgi_err kerr = KCGI_OK;
906 if ((header = gw_init_header()) == NULL)
907 return got_error_from_errno("malloc");
909 if (pledge("stdio rpath proc exec sendfd unveil",
910 NULL) == -1) {
911 error = got_error_from_errno("pledge");
912 goto done;
915 error = gw_apply_unveil(gw_trans->gw_dir->path);
916 if (error)
917 goto done;
919 error = gw_get_header(gw_trans, header,
920 gw_trans->gw_conf->got_max_commits_display);
921 if (error)
922 goto done;
924 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
925 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
926 "commits_line_wrapper", KATTR__MAX);
927 if (kerr != KCGI_OK)
928 goto done;
929 error = gw_gen_commit_header(gw_trans, n_header->commit_id,
930 n_header->refs_str);
931 if (error)
932 goto done;
933 error = gw_gen_author_header(gw_trans, n_header->author);
934 if (error)
935 goto done;
936 error = gw_gen_committer_header(gw_trans, n_header->author);
937 if (error)
938 goto done;
939 error = gw_get_time_str(&age, n_header->committer_time,
940 TM_LONG);
941 if (error)
942 goto done;
943 error = gw_gen_age_header(gw_trans, age ?age : "");
944 if (error)
945 goto done;
946 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
947 if (kerr != KCGI_OK)
948 goto done;
950 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
951 "dotted_line", KATTR__MAX);
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 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
959 "commit", KATTR__MAX);
960 if (kerr != KCGI_OK)
961 goto done;
962 kerr = khttp_puts(gw_trans->gw_req, n_header->commit_msg);
963 if (kerr != KCGI_OK)
964 goto done;
965 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
966 if (kerr != KCGI_OK)
967 goto done;
969 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
970 gw_trans->repo_name, n_header->commit_id) == -1) {
971 error = got_error_from_errno("asprintf");
972 goto done;
974 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
975 KATTR_ID, "navs_wrapper", KATTR__MAX);
976 if (kerr != KCGI_OK)
977 goto done;
978 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
979 KATTR_ID, "navs", KATTR__MAX);
980 if (kerr != KCGI_OK)
981 goto done;
982 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
983 KATTR_HREF, href_diff, KATTR__MAX);
984 if (kerr != KCGI_OK)
985 goto done;
986 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
987 if (kerr != KCGI_OK)
988 goto done;
989 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
990 if (kerr != KCGI_OK)
991 goto done;
993 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
994 if (kerr != KCGI_OK)
995 goto done;
997 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
998 gw_trans->repo_name, n_header->commit_id) == -1) {
999 error = got_error_from_errno("asprintf");
1000 goto done;
1002 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1003 KATTR_HREF, href_blob, KATTR__MAX);
1004 if (kerr != KCGI_OK)
1005 goto done;
1006 khtml_puts(gw_trans->gw_html_req, "tree");
1007 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1008 if (kerr != KCGI_OK)
1009 goto done;
1010 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1011 if (kerr != KCGI_OK)
1012 goto done;
1014 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1015 "solid_line", KATTR__MAX);
1016 if (kerr != KCGI_OK)
1017 goto done;
1018 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1019 if (kerr != KCGI_OK)
1020 goto done;
1022 free(age);
1023 age = NULL;
1025 done:
1026 got_ref_list_free(&header->refs);
1027 gw_free_headers(header);
1028 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1029 gw_free_headers(n_header);
1030 free(age);
1031 free(href_diff);
1032 free(href_blob);
1033 if (error == NULL && kerr != KCGI_OK)
1034 error = gw_kcgi_error(kerr);
1035 return error;
1038 static const struct got_error *
1039 gw_briefs(struct gw_trans *gw_trans)
1041 const struct got_error *error = NULL;
1042 struct gw_header *header = NULL, *n_header = NULL;
1043 char *age = NULL, *age_html = NULL;
1044 char *href_diff = NULL, *href_blob = NULL;
1045 char *newline, *smallerthan;
1046 enum kcgi_err kerr = KCGI_OK;
1048 if ((header = gw_init_header()) == NULL)
1049 return got_error_from_errno("malloc");
1051 if (pledge("stdio rpath proc exec sendfd unveil",
1052 NULL) == -1) {
1053 error = got_error_from_errno("pledge");
1054 goto done;
1057 error = gw_apply_unveil(gw_trans->gw_dir->path);
1058 if (error)
1059 goto done;
1061 if (gw_trans->action == GW_SUMMARY)
1062 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
1063 else
1064 error = gw_get_header(gw_trans, header,
1065 gw_trans->gw_conf->got_max_commits_display);
1066 if (error)
1067 goto done;
1069 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
1070 error = gw_get_time_str(&age, n_header->committer_time,
1071 TM_DIFF);
1072 if (error)
1073 goto done;
1075 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1076 KATTR_ID, "briefs_wrapper", KATTR__MAX);
1077 if (kerr != KCGI_OK)
1078 goto done;
1080 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1081 KATTR_ID, "briefs_age", KATTR__MAX);
1082 if (kerr != KCGI_OK)
1083 goto done;
1084 if (asprintf(&age_html, "%s", age ? age : "") == -1) {
1085 error = got_error_from_errno("asprintf");
1086 goto done;
1088 kerr = khtml_puts(gw_trans->gw_html_req, age_html);
1089 if (kerr != KCGI_OK)
1090 goto done;
1091 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1092 if (kerr != KCGI_OK)
1093 goto done;
1095 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1096 KATTR_ID, "briefs_author", KATTR__MAX);
1097 if (kerr != KCGI_OK)
1098 goto done;
1099 smallerthan = strchr(n_header->author, '<');
1100 if (smallerthan)
1101 *smallerthan = '\0';
1102 kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
1103 if (kerr != KCGI_OK)
1104 goto done;
1105 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1106 if (kerr != KCGI_OK)
1107 goto done;
1109 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
1110 gw_trans->repo_name, n_header->commit_id) == -1) {
1111 error = got_error_from_errno("asprintf");
1112 goto done;
1114 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1115 KATTR_ID, "briefs_log", KATTR__MAX);
1116 if (kerr != KCGI_OK)
1117 goto done;
1118 newline = strchr(n_header->commit_msg, '\n');
1119 if (newline)
1120 *newline = '\0';
1121 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1122 KATTR_HREF, href_diff, KATTR__MAX);
1123 if (kerr != KCGI_OK)
1124 goto done;
1125 kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
1126 if (kerr != KCGI_OK)
1127 goto done;
1128 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1129 if (kerr != KCGI_OK)
1130 goto done;
1132 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1133 KATTR_ID, "navs_wrapper", KATTR__MAX);
1134 if (kerr != KCGI_OK)
1135 goto done;
1136 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1137 KATTR_ID, "navs", KATTR__MAX);
1138 if (kerr != KCGI_OK)
1139 goto done;
1140 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1141 KATTR_HREF, href_diff, KATTR__MAX);
1142 if (kerr != KCGI_OK)
1143 goto done;
1144 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1145 if (kerr != KCGI_OK)
1146 goto done;
1147 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1148 if (kerr != KCGI_OK)
1149 goto done;
1151 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1152 if (kerr != KCGI_OK)
1153 goto done;
1155 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
1156 gw_trans->repo_name, n_header->commit_id) == -1) {
1157 error = got_error_from_errno("asprintf");
1158 goto done;
1160 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1161 KATTR_HREF, href_blob, KATTR__MAX);
1162 if (kerr != KCGI_OK)
1163 goto done;
1164 khtml_puts(gw_trans->gw_html_req, "tree");
1165 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1166 if (kerr != KCGI_OK)
1167 goto done;
1168 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1169 if (kerr != KCGI_OK)
1170 goto done;
1172 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1173 KATTR_ID, "dotted_line", KATTR__MAX);
1174 if (kerr != KCGI_OK)
1175 goto done;
1176 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1177 if (kerr != KCGI_OK)
1178 goto done;
1180 free(age);
1181 age = NULL;
1182 free(age_html);
1183 age_html = NULL;
1184 free(href_diff);
1185 href_diff = NULL;
1186 free(href_blob);
1187 href_blob = NULL;
1189 done:
1190 got_ref_list_free(&header->refs);
1191 gw_free_headers(header);
1192 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1193 gw_free_headers(n_header);
1194 free(age);
1195 free(age_html);
1196 free(href_diff);
1197 free(href_blob);
1198 if (error == NULL && kerr != KCGI_OK)
1199 error = gw_kcgi_error(kerr);
1200 return error;
1203 static const struct got_error *
1204 gw_summary(struct gw_trans *gw_trans)
1206 const struct got_error *error = NULL;
1207 char *age = NULL;
1208 enum kcgi_err kerr = KCGI_OK;
1210 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1211 return got_error_from_errno("pledge");
1213 /* unveil is applied with gw_briefs below */
1215 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1216 "summary_wrapper", KATTR__MAX);
1217 if (kerr != KCGI_OK)
1218 return gw_kcgi_error(kerr);
1220 if (gw_trans->gw_conf->got_show_repo_description &&
1221 gw_trans->gw_dir->description != NULL &&
1222 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1223 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1224 KATTR_ID, "description_title", KATTR__MAX);
1225 if (kerr != KCGI_OK)
1226 goto done;
1227 kerr = khtml_puts(gw_trans->gw_html_req, "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;
1233 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1234 KATTR_ID, "description", KATTR__MAX);
1235 if (kerr != KCGI_OK)
1236 goto done;
1237 kerr = khtml_puts(gw_trans->gw_html_req,
1238 gw_trans->gw_dir->description);
1239 if (kerr != KCGI_OK)
1240 goto done;
1241 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1242 if (kerr != KCGI_OK)
1243 goto done;
1246 if (gw_trans->gw_conf->got_show_repo_owner &&
1247 gw_trans->gw_dir->owner != NULL &&
1248 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
1249 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1250 KATTR_ID, "repo_owner_title", KATTR__MAX);
1251 if (kerr != KCGI_OK)
1252 goto done;
1253 kerr = khtml_puts(gw_trans->gw_html_req, "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;
1259 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1260 KATTR_ID, "repo_owner", KATTR__MAX);
1261 if (kerr != KCGI_OK)
1262 goto done;
1263 kerr = khtml_puts(gw_trans->gw_html_req,
1264 gw_trans->gw_dir->owner);
1265 if (kerr != KCGI_OK)
1266 goto done;
1267 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1268 if (kerr != KCGI_OK)
1269 goto done;
1272 if (gw_trans->gw_conf->got_show_repo_age) {
1273 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1274 "refs/heads", TM_LONG);
1275 if (error)
1276 goto done;
1277 if (age != NULL) {
1278 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1279 KATTR_ID, "last_change_title", KATTR__MAX);
1280 if (kerr != KCGI_OK)
1281 goto done;
1282 kerr = khtml_puts(gw_trans->gw_html_req,
1283 "Last Change: ");
1284 if (kerr != KCGI_OK)
1285 goto done;
1286 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1287 if (kerr != KCGI_OK)
1288 goto done;
1289 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1290 KATTR_ID, "last_change", KATTR__MAX);
1291 if (kerr != KCGI_OK)
1292 goto done;
1293 kerr = khtml_puts(gw_trans->gw_html_req, age);
1294 if (kerr != KCGI_OK)
1295 goto done;
1296 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1297 if (kerr != KCGI_OK)
1298 goto done;
1302 if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1303 gw_trans->gw_dir->url != NULL &&
1304 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1305 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1306 KATTR_ID, "cloneurl_title", KATTR__MAX);
1307 if (kerr != KCGI_OK)
1308 goto done;
1309 kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1310 if (kerr != KCGI_OK)
1311 goto done;
1312 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1313 if (kerr != KCGI_OK)
1314 goto done;
1315 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1316 KATTR_ID, "cloneurl", KATTR__MAX);
1317 if (kerr != KCGI_OK)
1318 goto done;
1319 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1320 if (kerr != KCGI_OK)
1321 goto done;
1322 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1323 if (kerr != KCGI_OK)
1324 goto done;
1327 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1328 if (kerr != KCGI_OK)
1329 goto done;
1331 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1332 "briefs_title_wrapper", KATTR__MAX);
1333 if (kerr != KCGI_OK)
1334 goto done;
1335 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1336 "briefs_title", KATTR__MAX);
1337 if (kerr != KCGI_OK)
1338 goto done;
1339 kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1340 if (kerr != KCGI_OK)
1341 goto done;
1342 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1343 if (kerr != KCGI_OK)
1344 goto done;
1345 error = gw_briefs(gw_trans);
1346 if (error)
1347 goto done;
1349 error = gw_output_repo_tags(gw_trans, NULL, D_MAXSLCOMMDISP,
1350 TAGBRIEF);
1351 if (error)
1352 goto done;
1354 error = gw_output_repo_heads(gw_trans);
1355 done:
1356 free(age);
1357 if (error == NULL && kerr != KCGI_OK)
1358 error = gw_kcgi_error(kerr);
1359 return error;
1362 static const struct got_error *
1363 gw_tree(struct gw_trans *gw_trans)
1365 const struct got_error *error = NULL;
1366 struct gw_header *header = NULL;
1367 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1368 char *age = NULL, *age_html = NULL;
1369 enum kcgi_err kerr;
1371 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1372 return got_error_from_errno("pledge");
1374 if ((header = gw_init_header()) == NULL)
1375 return got_error_from_errno("malloc");
1377 error = gw_apply_unveil(gw_trans->gw_dir->path);
1378 if (error)
1379 goto done;
1381 error = gw_get_header(gw_trans, header, 1);
1382 if (error)
1383 goto done;
1385 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1386 "tree_header_wrapper", KATTR__MAX);
1387 if (kerr != KCGI_OK)
1388 goto done;
1389 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1390 "tree_header", KATTR__MAX);
1391 if (kerr != KCGI_OK)
1392 goto done;
1393 error = gw_gen_tree_header(gw_trans, header->tree_id);
1394 if (error)
1395 goto done;
1396 error = gw_get_time_str(&age, header->committer_time,
1397 TM_LONG);
1398 if (error)
1399 goto done;
1400 error = gw_gen_age_header(gw_trans, age ?age : "");
1401 if (error)
1402 goto done;
1403 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1404 if (error)
1405 goto done;
1406 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1407 if (kerr != KCGI_OK)
1408 goto done;
1409 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1410 "dotted_line", KATTR__MAX);
1411 if (kerr != KCGI_OK)
1412 goto done;
1413 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1414 if (kerr != KCGI_OK)
1415 goto done;
1417 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1418 "tree", KATTR__MAX);
1419 if (kerr != KCGI_OK)
1420 goto done;
1421 error = gw_output_repo_tree(gw_trans);
1422 if (error)
1423 goto done;
1425 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1426 done:
1427 got_ref_list_free(&header->refs);
1428 gw_free_headers(header);
1429 free(tree_html_disp);
1430 free(tree_html);
1431 free(tree);
1432 free(age);
1433 free(age_html);
1434 if (error == NULL && kerr != KCGI_OK)
1435 error = gw_kcgi_error(kerr);
1436 return error;
1439 static const struct got_error *
1440 gw_tag(struct gw_trans *gw_trans)
1442 const struct got_error *error = NULL;
1443 struct gw_header *header = NULL;
1444 enum kcgi_err kerr;
1446 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1447 return got_error_from_errno("pledge");
1449 if ((header = gw_init_header()) == NULL)
1450 return got_error_from_errno("malloc");
1452 error = gw_apply_unveil(gw_trans->gw_dir->path);
1453 if (error)
1454 goto done;
1456 error = gw_get_header(gw_trans, header, 1);
1457 if (error)
1458 goto done;
1460 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1461 "tag_header_wrapper", KATTR__MAX);
1462 if (kerr != KCGI_OK)
1463 goto done;
1464 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1465 "tag_header", KATTR__MAX);
1466 if (kerr != KCGI_OK)
1467 goto done;
1468 error = gw_gen_commit_header(gw_trans, header->commit_id,
1469 header->refs_str);
1470 if (error)
1471 goto done;
1472 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1473 if (error)
1474 goto done;
1475 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1476 if (kerr != KCGI_OK)
1477 goto done;
1478 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1479 "dotted_line", KATTR__MAX);
1480 if (kerr != KCGI_OK)
1481 goto done;
1482 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1483 if (kerr != KCGI_OK)
1484 goto done;
1486 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1487 "tree", KATTR__MAX);
1488 if (kerr != KCGI_OK)
1489 goto done;
1491 error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1492 if (error)
1493 goto done;
1495 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1496 done:
1497 got_ref_list_free(&header->refs);
1498 gw_free_headers(header);
1499 if (error == NULL && kerr != KCGI_OK)
1500 error = gw_kcgi_error(kerr);
1501 return error;
1504 static const struct got_error *
1505 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1507 const struct got_error *error = NULL;
1508 DIR *dt;
1509 char *dir_test;
1510 int opened = 0;
1512 if (asprintf(&dir_test, "%s/%s/%s",
1513 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1514 GOTWEB_GIT_DIR) == -1)
1515 return got_error_from_errno("asprintf");
1517 dt = opendir(dir_test);
1518 if (dt == NULL) {
1519 free(dir_test);
1520 } else {
1521 error = gw_strdup_string(&gw_dir->path, dir_test, NULL);
1522 opened = 1;
1523 if (error)
1524 goto errored;
1525 goto done;
1528 if (asprintf(&dir_test, "%s/%s/%s",
1529 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1530 GOTWEB_GOT_DIR) == -1)
1531 return got_error_from_errno("asprintf");
1533 dt = opendir(dir_test);
1534 if (dt == NULL)
1535 free(dir_test);
1536 else {
1537 opened = 1;
1538 error = got_error(GOT_ERR_NOT_GIT_REPO);
1539 goto errored;
1542 if (asprintf(&dir_test, "%s/%s",
1543 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1)
1544 return got_error_from_errno("asprintf");
1546 error = gw_strdup_string(&gw_dir->path, dir_test, NULL);
1547 if (error) {
1548 opened = 1;
1549 goto errored;
1551 done:
1552 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1553 gw_dir->path);
1554 if (error)
1555 goto errored;
1556 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1557 if (error)
1558 goto errored;
1559 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1560 "refs/heads", TM_DIFF);
1561 if (error)
1562 goto errored;
1563 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1564 errored:
1565 free(dir_test);
1566 if (opened)
1567 closedir(dt);
1568 return error;
1571 static const struct got_error *
1572 gw_load_got_paths(struct gw_trans *gw_trans)
1574 const struct got_error *error = NULL;
1575 DIR *d;
1576 struct dirent **sd_dent;
1577 struct gw_dir *gw_dir;
1578 struct stat st;
1579 unsigned int d_cnt, d_i;
1581 d = opendir(gw_trans->gw_conf->got_repos_path);
1582 if (d == NULL) {
1583 error = got_error_from_errno2("opendir",
1584 gw_trans->gw_conf->got_repos_path);
1585 return error;
1588 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1589 alphasort);
1590 if (d_cnt == -1) {
1591 error = got_error_from_errno2("scandir",
1592 gw_trans->gw_conf->got_repos_path);
1593 return error;
1596 for (d_i = 0; d_i < d_cnt; d_i++) {
1597 if (gw_trans->gw_conf->got_max_repos > 0 &&
1598 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1599 break; /* account for parent and self */
1601 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1602 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1603 continue;
1605 error = gw_init_gw_dir(&gw_dir, sd_dent[d_i]->d_name);
1606 if (error)
1607 return error;
1609 error = gw_load_got_path(gw_trans, gw_dir);
1610 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1611 error = NULL;
1612 continue;
1614 else if (error)
1615 return error;
1617 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1618 !got_path_dir_is_empty(gw_dir->path)) {
1619 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1620 entry);
1621 gw_trans->repos_total++;
1625 closedir(d);
1626 return error;
1629 static const struct got_error *
1630 gw_parse_querystring(struct gw_trans *gw_trans)
1632 const struct got_error *error = NULL;
1633 struct kpair *p;
1634 struct gw_query_action *action = NULL;
1635 unsigned int i;
1637 if (gw_trans->gw_req->fieldnmap[0]) {
1638 error = got_error_from_errno("bad parse");
1639 return error;
1640 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1641 /* define gw_trans->repo_path */
1642 if (asprintf(&gw_trans->repo_name, "%s", p->parsed.s) == -1)
1643 return got_error_from_errno("asprintf");
1645 if (asprintf(&gw_trans->repo_path, "%s/%s",
1646 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
1647 return got_error_from_errno("asprintf");
1649 /* get action and set function */
1650 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
1651 for (i = 0; i < nitems(gw_query_funcs); i++) {
1652 action = &gw_query_funcs[i];
1653 if (action->func_name == NULL)
1654 continue;
1656 if (strcmp(action->func_name,
1657 p->parsed.s) == 0) {
1658 gw_trans->action = i;
1659 if (asprintf(&gw_trans->action_name,
1660 "%s", action->func_name) == -1)
1661 return
1662 got_error_from_errno(
1663 "asprintf");
1665 break;
1668 action = NULL;
1671 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
1672 if (asprintf(&gw_trans->commit, "%s",
1673 p->parsed.s) == -1)
1674 return got_error_from_errno("asprintf");
1676 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1677 if (asprintf(&gw_trans->repo_file, "%s",
1678 p->parsed.s) == -1)
1679 return got_error_from_errno("asprintf");
1681 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER]))
1682 if (asprintf(&gw_trans->repo_folder, "%s",
1683 p->parsed.s) == -1)
1684 return got_error_from_errno("asprintf");
1686 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
1687 if (asprintf(&gw_trans->headref, "%s",
1688 p->parsed.s) == -1)
1689 return got_error_from_errno("asprintf");
1691 if (action == NULL) {
1692 error = got_error_from_errno("invalid action");
1693 return error;
1695 error = gw_init_gw_dir(&gw_trans->gw_dir, gw_trans->repo_name);
1696 if (error)
1697 return error;
1699 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1700 if (error)
1701 return error;
1702 } else
1703 gw_trans->action = GW_INDEX;
1705 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1706 gw_trans->page = p->parsed.i;
1708 return error;
1711 static const struct got_error *
1712 gw_init_gw_dir(struct gw_dir **gw_dir, char *dir)
1714 const struct got_error *error;
1716 *gw_dir = malloc(sizeof(**gw_dir));
1717 if (*gw_dir == NULL)
1718 return got_error_from_errno("malloc");
1720 if (asprintf(&(*gw_dir)->name, "%s", dir) == -1) {
1721 error = got_error_from_errno("asprintf");
1722 free(*gw_dir);
1723 *gw_dir = NULL;
1724 return NULL;
1727 return NULL;
1730 static const struct got_error *
1731 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1733 enum kcgi_err kerr;
1735 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1736 if (kerr != KCGI_OK)
1737 return gw_kcgi_error(kerr);
1738 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1739 khttps[code]);
1740 if (kerr != KCGI_OK)
1741 return gw_kcgi_error(kerr);
1742 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1743 kmimetypes[mime]);
1744 if (kerr != KCGI_OK)
1745 return gw_kcgi_error(kerr);
1746 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
1747 "nosniff");
1748 if (kerr != KCGI_OK)
1749 return gw_kcgi_error(kerr);
1750 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1751 if (kerr != KCGI_OK)
1752 return gw_kcgi_error(kerr);
1753 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
1754 "1; mode=block");
1755 if (kerr != KCGI_OK)
1756 return gw_kcgi_error(kerr);
1758 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
1759 kerr = khttp_head(gw_trans->gw_req,
1760 kresps[KRESP_CONTENT_DISPOSITION],
1761 "attachment; filename=%s", gw_trans->repo_file);
1762 if (kerr != KCGI_OK)
1763 return gw_kcgi_error(kerr);
1766 kerr = khttp_body(gw_trans->gw_req);
1767 return gw_kcgi_error(kerr);
1770 static const struct got_error *
1771 gw_display_index(struct gw_trans *gw_trans)
1773 const struct got_error *error;
1774 enum kcgi_err kerr;
1776 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1777 if (error)
1778 return error;
1780 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1781 if (kerr != KCGI_OK)
1782 return gw_kcgi_error(kerr);
1784 if (gw_trans->action != GW_BLOB) {
1785 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1786 gw_query_funcs[gw_trans->action].template);
1787 if (kerr != KCGI_OK) {
1788 khtml_close(gw_trans->gw_html_req);
1789 return gw_kcgi_error(kerr);
1793 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
1796 static void
1797 gw_display_error(struct gw_trans *gw_trans, const struct got_error *err)
1799 if (gw_display_open(gw_trans, KHTTP_200, gw_trans->mime) != NULL)
1800 return;
1802 if (khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0) != KCGI_OK)
1803 return;
1804 khtml_puts(gw_trans->gw_html_req, err->msg);
1805 khtml_close(gw_trans->gw_html_req);
1808 static int
1809 gw_template(size_t key, void *arg)
1811 const struct got_error *error = NULL;
1812 enum kcgi_err kerr;
1813 struct gw_trans *gw_trans = arg;
1814 char *img_src = NULL;
1816 switch (key) {
1817 case (TEMPL_HEAD):
1818 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1819 KATTR_NAME, "viewport",
1820 KATTR_CONTENT, "initial-scale=.75, user-scalable=yes",
1821 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_CHARSET, "utf-8",
1829 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_META,
1836 KATTR_NAME, "msapplication-TileColor",
1837 KATTR_CONTENT, "#da532c", 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_META,
1844 KATTR_NAME, "theme-color",
1845 KATTR_CONTENT, "#ffffff", 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, "apple-touch-icon", KATTR_SIZES, "180x180",
1853 KATTR_HREF, "/apple-touch-icon.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, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
1861 "32x32", KATTR_HREF, "/favicon-32x32.png", 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, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
1869 "16x16", KATTR_HREF, "/favicon-16x16.png", 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, "manifest", KATTR_HREF, "/site.webmanifest",
1877 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 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1884 KATTR_REL, "mask-icon", KATTR_HREF,
1885 "/safari-pinned-tab.svg", KATTR__MAX);
1886 if (kerr != KCGI_OK)
1887 return 0;
1888 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1889 if (kerr != KCGI_OK)
1890 return 0;
1891 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1892 KATTR_REL, "stylesheet", KATTR_TYPE, "text/css",
1893 KATTR_HREF, "/gotweb.css", KATTR__MAX);
1894 if (kerr != KCGI_OK)
1895 return 0;
1896 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1897 if (kerr != KCGI_OK)
1898 return 0;
1899 break;
1900 case(TEMPL_HEADER):
1901 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1902 KATTR_ID, "got_link", KATTR__MAX);
1903 if (kerr != KCGI_OK)
1904 return 0;
1905 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1906 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
1907 KATTR_TARGET, "_sotd", KATTR__MAX);
1908 if (kerr != KCGI_OK)
1909 return 0;
1910 if (asprintf(&img_src, "/%s",
1911 gw_trans->gw_conf->got_logo) == -1)
1912 return 0;
1913 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
1914 KATTR_SRC, img_src, KATTR__MAX);
1915 if (kerr != KCGI_OK) {
1916 free(img_src);
1917 return 0;
1919 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1920 if (kerr != KCGI_OK) {
1921 free(img_src);
1922 return 0;
1924 break;
1925 case (TEMPL_SITEPATH):
1926 error = gw_output_site_link(gw_trans);
1927 if (error)
1928 return 0;
1929 break;
1930 case(TEMPL_TITLE):
1931 if (gw_trans->gw_conf->got_site_name != NULL) {
1932 kerr = khtml_puts(gw_trans->gw_html_req,
1933 gw_trans->gw_conf->got_site_name);
1934 if (kerr != KCGI_OK)
1935 return 0;
1937 break;
1938 case (TEMPL_SEARCH):
1939 break;
1940 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1941 "search", KATTR__MAX);
1942 if (kerr != KCGI_OK)
1943 return 0;
1944 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
1945 KATTR_METHOD, "POST", KATTR__MAX);
1946 if (kerr != KCGI_OK)
1947 return 0;
1948 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
1949 "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
1950 KATTR_MAXLENGTH, "50", KATTR__MAX);
1951 if (kerr != KCGI_OK)
1952 return 0;
1953 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
1954 KATTR__MAX);
1955 if (kerr != KCGI_OK)
1956 return 0;
1957 kerr = khtml_puts(gw_trans->gw_html_req, "Search");
1958 if (kerr != KCGI_OK)
1959 return 0;
1960 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
1961 if (kerr != KCGI_OK)
1962 return 0;
1963 break;
1964 case(TEMPL_SITEOWNER):
1965 if (gw_trans->gw_conf->got_site_owner != NULL &&
1966 gw_trans->gw_conf->got_show_site_owner) {
1967 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1968 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
1969 if (kerr != KCGI_OK)
1970 return 0;
1971 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1972 KATTR_ID, "site_owner", KATTR__MAX);
1973 if (kerr != KCGI_OK)
1974 return 0;
1975 kerr = khtml_puts(gw_trans->gw_html_req,
1976 gw_trans->gw_conf->got_site_owner);
1977 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1978 if (kerr != KCGI_OK)
1979 return 0;
1981 break;
1982 case(TEMPL_CONTENT):
1983 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1984 if (error) {
1985 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1986 KATTR_ID, "tmpl_err", KATTR__MAX);
1987 if (kerr != KCGI_OK)
1988 return 0;
1989 kerr = khttp_puts(gw_trans->gw_req, "Error: ");
1990 if (kerr != KCGI_OK)
1991 return 0;
1992 kerr = khttp_puts(gw_trans->gw_req, error->msg);
1993 if (kerr != KCGI_OK)
1994 return 0;
1995 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1996 if (kerr != KCGI_OK)
1997 return 0;
1999 break;
2000 default:
2001 return 0;
2003 return 1;
2006 static const struct got_error *
2007 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
2009 const struct got_error *error = NULL;
2010 char *ref_str = NULL;
2011 enum kcgi_err kerr = KCGI_OK;
2013 if (strcmp(str2, "") != 0) {
2014 if (asprintf(&ref_str, "(%s)", str2) == -1) {
2015 error = got_error_from_errno("asprintf");
2016 goto done;
2018 } else {
2019 error = gw_strdup_string(&ref_str, "", NULL);
2020 if (error)
2021 goto done;
2024 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2025 KATTR_ID, "header_commit_title", KATTR__MAX);
2026 if (kerr != KCGI_OK)
2027 goto done;
2028 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
2029 if (kerr != KCGI_OK)
2030 goto done;
2031 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2032 if (kerr != KCGI_OK)
2033 goto done;
2034 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2035 KATTR_ID, "header_commit", KATTR__MAX);
2036 if (kerr != KCGI_OK)
2037 goto done;
2038 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2039 if (kerr != KCGI_OK)
2040 goto done;
2041 kerr = khtml_puts(gw_trans->gw_html_req, " ");
2042 if (kerr != KCGI_OK)
2043 goto done;
2044 kerr = khtml_puts(gw_trans->gw_html_req, ref_str);
2045 if (kerr != KCGI_OK)
2046 goto done;
2047 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2048 done:
2049 if (error == NULL && kerr != KCGI_OK)
2050 error = gw_kcgi_error(kerr);
2051 return error;
2054 static const struct got_error *
2055 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2057 const struct got_error *error = NULL;
2058 enum kcgi_err kerr = KCGI_OK;
2060 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2061 KATTR_ID, "header_diff_title", KATTR__MAX);
2062 if (kerr != KCGI_OK)
2063 goto done;
2064 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2065 if (kerr != KCGI_OK)
2066 goto done;
2067 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2068 if (kerr != KCGI_OK)
2069 goto done;
2070 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2071 KATTR_ID, "header_diff", KATTR__MAX);
2072 if (kerr != KCGI_OK)
2073 goto done;
2074 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2075 if (kerr != KCGI_OK)
2076 goto done;
2077 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2078 if (kerr != KCGI_OK)
2079 goto done;
2080 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2081 if (kerr != KCGI_OK)
2082 goto done;
2083 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2084 done:
2085 if (error == NULL && kerr != KCGI_OK)
2086 error = gw_kcgi_error(kerr);
2087 return error;
2090 static const struct got_error *
2091 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2093 const struct got_error *error = NULL;
2094 enum kcgi_err kerr = KCGI_OK;
2096 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2097 KATTR_ID, "header_age_title", KATTR__MAX);
2098 if (kerr != KCGI_OK)
2099 goto done;
2100 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2101 if (kerr != KCGI_OK)
2102 goto done;
2103 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2104 if (kerr != KCGI_OK)
2105 goto done;
2106 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2107 KATTR_ID, "header_age", KATTR__MAX);
2108 if (kerr != KCGI_OK)
2109 goto done;
2110 kerr = khtml_puts(gw_trans->gw_html_req, str);
2111 if (kerr != KCGI_OK)
2112 goto done;
2113 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2114 done:
2115 if (error == NULL && kerr != KCGI_OK)
2116 error = gw_kcgi_error(kerr);
2117 return error;
2120 static const struct got_error *
2121 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2123 const struct got_error *error = NULL;
2124 enum kcgi_err kerr;
2126 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2127 KATTR_ID, "header_author_title", KATTR__MAX);
2128 if (kerr != KCGI_OK)
2129 goto done;
2130 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2131 if (kerr != KCGI_OK)
2132 goto done;
2133 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2134 if (kerr != KCGI_OK)
2135 goto done;
2136 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2137 KATTR_ID, "header_author", KATTR__MAX);
2138 if (kerr != KCGI_OK)
2139 goto done;
2140 kerr = khtml_puts(gw_trans->gw_html_req, str);
2141 if (kerr != KCGI_OK)
2142 goto done;
2143 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2144 done:
2145 if (error == NULL && kerr != KCGI_OK)
2146 error = gw_kcgi_error(kerr);
2147 return error;
2150 static const struct got_error *
2151 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2153 const struct got_error *error = NULL;
2154 enum kcgi_err kerr;
2156 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2157 KATTR_ID, "header_committer_title", KATTR__MAX);
2158 if (kerr != KCGI_OK)
2159 goto done;
2160 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2161 if (kerr != KCGI_OK)
2162 goto done;
2163 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2164 if (kerr != KCGI_OK)
2165 goto done;
2166 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2167 KATTR_ID, "header_committer", KATTR__MAX);
2168 if (kerr != KCGI_OK)
2169 goto done;
2170 kerr = khtml_puts(gw_trans->gw_html_req, str);
2171 if (kerr != KCGI_OK)
2172 goto done;
2173 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2174 done:
2175 if (error == NULL && kerr != KCGI_OK)
2176 error = gw_kcgi_error(kerr);
2177 return error;
2180 static const struct got_error *
2181 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2183 const struct got_error *error = NULL;
2184 enum kcgi_err kerr;
2186 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2187 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2188 if (kerr != KCGI_OK)
2189 goto done;
2190 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2191 if (kerr != KCGI_OK)
2192 goto done;
2193 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2194 if (kerr != KCGI_OK)
2195 goto done;
2196 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2197 KATTR_ID, "header_commit_msg", KATTR__MAX);
2198 if (kerr != KCGI_OK)
2199 goto done;
2200 kerr = khttp_puts(gw_trans->gw_req, str);
2201 if (kerr != KCGI_OK)
2202 goto done;
2203 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2204 done:
2205 if (error == NULL && kerr != KCGI_OK)
2206 error = gw_kcgi_error(kerr);
2207 return error;
2210 static const struct got_error *
2211 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2213 const struct got_error *error = NULL;
2214 enum kcgi_err kerr;
2216 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2217 KATTR_ID, "header_tree_title", KATTR__MAX);
2218 if (kerr != KCGI_OK)
2219 goto done;
2220 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2221 if (kerr != KCGI_OK)
2222 goto done;
2223 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2224 if (kerr != KCGI_OK)
2225 goto done;
2226 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2227 KATTR_ID, "header_tree", KATTR__MAX);
2228 if (kerr != KCGI_OK)
2229 goto done;
2230 kerr = khtml_puts(gw_trans->gw_html_req, str);
2231 if (kerr != KCGI_OK)
2232 goto done;
2233 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2234 done:
2235 if (error == NULL && kerr != KCGI_OK)
2236 error = gw_kcgi_error(kerr);
2237 return error;
2240 static const struct got_error *
2241 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2242 char *dir)
2244 const struct got_error *error = NULL;
2245 FILE *f = NULL;
2246 char *d_file = NULL;
2247 unsigned int len;
2248 size_t n;
2250 *description = NULL;
2251 if (gw_trans->gw_conf->got_show_repo_description == 0)
2252 return NULL;
2254 if (asprintf(&d_file, "%s/description", dir) == -1)
2255 return got_error_from_errno("asprintf");
2257 f = fopen(d_file, "r");
2258 if (f == NULL) {
2259 if (errno == ENOENT || errno == EACCES)
2260 return NULL;
2261 error = got_error_from_errno2("fopen", d_file);
2262 goto done;
2265 if (fseek(f, 0, SEEK_END) == -1) {
2266 error = got_ferror(f, GOT_ERR_IO);
2267 goto done;
2269 len = ftell(f);
2270 if (len == -1) {
2271 error = got_ferror(f, GOT_ERR_IO);
2272 goto done;
2274 if (fseek(f, 0, SEEK_SET) == -1) {
2275 error = got_ferror(f, GOT_ERR_IO);
2276 goto done;
2278 *description = calloc(len + 1, sizeof(**description));
2279 if (*description == NULL) {
2280 error = got_error_from_errno("calloc");
2281 goto done;
2284 n = fread(*description, 1, len, f);
2285 if (n == 0 && ferror(f))
2286 error = got_ferror(f, GOT_ERR_IO);
2287 done:
2288 if (f != NULL && fclose(f) == -1 && error == NULL)
2289 error = got_error_from_errno("fclose");
2290 free(d_file);
2291 return error;
2294 static const struct got_error *
2295 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2297 struct tm tm;
2298 time_t diff_time;
2299 char *years = "years ago", *months = "months ago";
2300 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2301 char *minutes = "minutes ago", *seconds = "seconds ago";
2302 char *now = "right now";
2303 char *s;
2304 char datebuf[29];
2306 *repo_age = NULL;
2308 switch (ref_tm) {
2309 case TM_DIFF:
2310 diff_time = time(NULL) - committer_time;
2311 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2312 if (asprintf(repo_age, "%lld %s",
2313 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2314 return got_error_from_errno("asprintf");
2315 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2316 if (asprintf(repo_age, "%lld %s",
2317 (diff_time / 60 / 60 / 24 / (365 / 12)),
2318 months) == -1)
2319 return got_error_from_errno("asprintf");
2320 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2321 if (asprintf(repo_age, "%lld %s",
2322 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2323 return got_error_from_errno("asprintf");
2324 } else if (diff_time > 60 * 60 * 24 * 2) {
2325 if (asprintf(repo_age, "%lld %s",
2326 (diff_time / 60 / 60 / 24), days) == -1)
2327 return got_error_from_errno("asprintf");
2328 } else if (diff_time > 60 * 60 * 2) {
2329 if (asprintf(repo_age, "%lld %s",
2330 (diff_time / 60 / 60), hours) == -1)
2331 return got_error_from_errno("asprintf");
2332 } else if (diff_time > 60 * 2) {
2333 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2334 minutes) == -1)
2335 return got_error_from_errno("asprintf");
2336 } else if (diff_time > 2) {
2337 if (asprintf(repo_age, "%lld %s", diff_time,
2338 seconds) == -1)
2339 return got_error_from_errno("asprintf");
2340 } else {
2341 if (asprintf(repo_age, "%s", now) == -1)
2342 return got_error_from_errno("asprintf");
2344 break;
2345 case TM_LONG:
2346 if (gmtime_r(&committer_time, &tm) == NULL)
2347 return got_error_from_errno("gmtime_r");
2349 s = asctime_r(&tm, datebuf);
2350 if (s == NULL)
2351 return got_error_from_errno("asctime_r");
2353 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2354 return got_error_from_errno("asprintf");
2355 break;
2357 return NULL;
2360 static const struct got_error *
2361 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2362 char *repo_ref, int ref_tm)
2364 const struct got_error *error = NULL;
2365 struct got_object_id *id = NULL;
2366 struct got_repository *repo = NULL;
2367 struct got_commit_object *commit = NULL;
2368 struct got_reflist_head refs;
2369 struct got_reflist_entry *re;
2370 struct got_reference *head_ref;
2371 int is_head = 0;
2372 time_t committer_time = 0, cmp_time = 0;
2373 char *refname;
2375 *repo_age = NULL;
2376 SIMPLEQ_INIT(&refs);
2378 if (repo_ref == NULL)
2379 return NULL;
2381 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
2382 is_head = 1;
2384 if (gw_trans->gw_conf->got_show_repo_age == 0)
2385 return NULL;
2387 error = got_repo_open(&repo, dir, NULL);
2388 if (error)
2389 goto done;
2391 if (is_head)
2392 error = got_ref_list(&refs, repo, "refs/heads",
2393 got_ref_cmp_by_name, NULL);
2394 else
2395 error = got_ref_list(&refs, repo, repo_ref,
2396 got_ref_cmp_by_name, NULL);
2397 if (error)
2398 goto done;
2400 SIMPLEQ_FOREACH(re, &refs, entry) {
2401 if (is_head) {
2402 error = gw_strdup_string(&refname, repo_ref, NULL);
2403 if (error)
2404 goto done;
2405 } else {
2406 error = gw_strdup_string(&refname, NULL,
2407 got_ref_get_name(re->ref));
2408 if (error)
2409 goto done;
2411 error = got_ref_open(&head_ref, repo, refname, 0);
2412 if (error)
2413 goto done;
2415 error = got_ref_resolve(&id, repo, head_ref);
2416 got_ref_close(head_ref);
2417 if (error)
2418 goto done;
2420 error = got_object_open_as_commit(&commit, repo, id);
2421 if (error)
2422 goto done;
2424 committer_time =
2425 got_object_commit_get_committer_time(commit);
2427 if (cmp_time < committer_time)
2428 cmp_time = committer_time;
2431 if (cmp_time != 0) {
2432 committer_time = cmp_time;
2433 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2435 done:
2436 got_ref_list_free(&refs);
2437 free(id);
2438 return error;
2441 static const struct got_error *
2442 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2444 const struct got_error *error;
2445 FILE *f = NULL;
2446 struct got_object_id *id1 = NULL, *id2 = NULL;
2447 char *label1 = NULL, *label2 = NULL, *line = NULL;
2448 int obj_type;
2449 size_t linesize = 0;
2450 ssize_t linelen;
2451 enum kcgi_err kerr = KCGI_OK;
2453 f = got_opentemp();
2454 if (f == NULL)
2455 return NULL;
2457 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
2458 if (error)
2459 goto done;
2461 if (strncmp(header->parent_id, "/dev/null", 9) != 0) {
2462 error = got_repo_match_object_id(&id1, &label1,
2463 header->parent_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2464 if (error)
2465 goto done;
2468 error = got_repo_match_object_id(&id2, &label2,
2469 header->commit_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2470 if (error)
2471 goto done;
2473 error = got_object_get_type(&obj_type, header->repo, id2);
2474 if (error)
2475 goto done;
2476 switch (obj_type) {
2477 case GOT_OBJ_TYPE_BLOB:
2478 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
2479 header->repo, f);
2480 break;
2481 case GOT_OBJ_TYPE_TREE:
2482 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
2483 header->repo, f);
2484 break;
2485 case GOT_OBJ_TYPE_COMMIT:
2486 error = got_diff_objects_as_commits(id1, id2, 3, 0,
2487 header->repo, f);
2488 break;
2489 default:
2490 error = got_error(GOT_ERR_OBJ_TYPE);
2492 if (error)
2493 goto done;
2495 if (fseek(f, 0, SEEK_SET) == -1) {
2496 error = got_ferror(f, GOT_ERR_IO);
2497 goto done;
2500 while ((linelen = getline(&line, &linesize, f)) != -1) {
2501 error = gw_colordiff_line(gw_trans, line);
2502 if (error)
2503 goto done;
2504 /* XXX: KHTML_PRETTY breaks this */
2505 kerr = khtml_puts(gw_trans->gw_html_req, line);
2506 if (kerr != KCGI_OK)
2507 goto done;
2508 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2509 if (kerr != KCGI_OK)
2510 goto done;
2512 if (linelen == -1 && ferror(f))
2513 error = got_error_from_errno("getline");
2514 done:
2515 if (f && fclose(f) == -1 && error == NULL)
2516 error = got_error_from_errno("fclose");
2517 free(line);
2518 free(label1);
2519 free(label2);
2520 free(id1);
2521 free(id2);
2523 if (error == NULL && kerr != KCGI_OK)
2524 error = gw_kcgi_error(kerr);
2525 return error;
2528 static const struct got_error *
2529 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2531 const struct got_error *error = NULL;
2532 struct got_repository *repo;
2533 const char *gitconfig_owner;
2535 *owner = NULL;
2537 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2538 return NULL;
2540 error = got_repo_open(&repo, dir, NULL);
2541 if (error)
2542 return error;
2543 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2544 if (gitconfig_owner)
2545 error = gw_strdup_string(owner, NULL, gitconfig_owner);
2546 got_repo_close(repo);
2547 return error;
2550 static const struct got_error *
2551 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2553 const struct got_error *error = NULL;
2554 FILE *f;
2555 char *d_file = NULL;
2556 unsigned int len;
2557 size_t n;
2559 *url = NULL;
2561 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2562 return got_error_from_errno("asprintf");
2564 f = fopen(d_file, "r");
2565 if (f == NULL) {
2566 if (errno != ENOENT && errno != EACCES)
2567 error = got_error_from_errno2("fopen", d_file);
2568 goto done;
2571 if (fseek(f, 0, SEEK_END) == -1) {
2572 error = got_ferror(f, GOT_ERR_IO);
2573 goto done;
2575 len = ftell(f);
2576 if (len == -1) {
2577 error = got_ferror(f, GOT_ERR_IO);
2578 goto done;
2580 if (fseek(f, 0, SEEK_SET) == -1) {
2581 error = got_ferror(f, GOT_ERR_IO);
2582 goto done;
2585 *url = calloc(len + 1, sizeof(**url));
2586 if (*url == NULL) {
2587 error = got_error_from_errno("calloc");
2588 goto done;
2591 n = fread(*url, 1, len, f);
2592 if (n == 0 && ferror(f))
2593 error = got_ferror(f, GOT_ERR_IO);
2594 done:
2595 if (f && fclose(f) == -1 && error == NULL)
2596 error = got_error_from_errno("fclose");
2597 free(d_file);
2598 return NULL;
2601 static const struct got_error *
2602 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
2603 int limit, int tag_type)
2605 const struct got_error *error = NULL;
2606 struct got_repository *repo = NULL;
2607 struct got_reflist_head refs;
2608 struct got_reflist_entry *re;
2609 char *age = NULL;
2610 char *id_str = NULL, *newline, *href_commits = NULL;
2611 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
2612 struct got_tag_object *tag = NULL;
2613 enum kcgi_err kerr = KCGI_OK;
2614 int summary_header_displayed = 0;
2616 SIMPLEQ_INIT(&refs);
2618 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2619 if (error)
2620 return error;
2622 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
2623 if (error)
2624 goto done;
2626 SIMPLEQ_FOREACH(re, &refs, entry) {
2627 const char *refname;
2628 const char *tagger;
2629 const char *tag_commit;
2630 time_t tagger_time;
2631 struct got_object_id *id;
2632 struct got_commit_object *commit = NULL;
2634 refname = got_ref_get_name(re->ref);
2635 if (strncmp(refname, "refs/tags/", 10) != 0)
2636 continue;
2637 refname += 10;
2639 error = got_ref_resolve(&id, repo, re->ref);
2640 if (error)
2641 goto done;
2643 error = got_object_open_as_tag(&tag, repo, id);
2644 if (error) {
2645 if (error->code != GOT_ERR_OBJ_TYPE) {
2646 free(id);
2647 goto done;
2649 /* "lightweight" tag */
2650 error = got_object_open_as_commit(&commit, repo, id);
2651 if (error) {
2652 free(id);
2653 goto done;
2655 tagger = got_object_commit_get_committer(commit);
2656 tagger_time =
2657 got_object_commit_get_committer_time(commit);
2658 error = got_object_id_str(&id_str, id);
2659 free(id);
2660 } else {
2661 free(id);
2662 tagger = got_object_tag_get_tagger(tag);
2663 tagger_time = got_object_tag_get_tagger_time(tag);
2664 error = got_object_id_str(&id_str,
2665 got_object_tag_get_object_id(tag));
2667 if (error)
2668 goto done;
2670 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
2671 strlen(id_str)) != 0)
2672 continue;
2674 if (commit) {
2675 error = got_object_commit_get_logmsg(&tag_commit0,
2676 commit);
2677 if (error)
2678 goto done;
2679 got_object_commit_close(commit);
2680 } else {
2681 tag_commit0 = strdup(got_object_tag_get_message(tag));
2682 if (tag_commit0 == NULL) {
2683 error = got_error_from_errno("strdup");
2684 goto done;
2688 tag_commit = tag_commit0;
2689 while (*tag_commit == '\n')
2690 tag_commit++;
2692 switch (tag_type) {
2693 case TAGBRIEF:
2694 newline = strchr(tag_commit, '\n');
2695 if (newline)
2696 *newline = '\0';
2698 if (summary_header_displayed == 0) {
2699 kerr = khtml_attr(gw_trans->gw_html_req,
2700 KELEM_DIV, KATTR_ID,
2701 "summary_tags_title_wrapper", KATTR__MAX);
2702 if (kerr != KCGI_OK)
2703 goto done;
2704 kerr = khtml_attr(gw_trans->gw_html_req,
2705 KELEM_DIV, KATTR_ID,
2706 "summary_tags_title", KATTR__MAX);
2707 if (kerr != KCGI_OK)
2708 goto done;
2709 kerr = khtml_puts(gw_trans->gw_html_req,
2710 "Tags");
2711 if (kerr != KCGI_OK)
2712 goto done;
2713 kerr = khtml_closeelem(gw_trans->gw_html_req,
2714 2);
2715 if (kerr != KCGI_OK)
2716 goto done;
2717 kerr = khtml_attr(gw_trans->gw_html_req,
2718 KELEM_DIV, KATTR_ID,
2719 "summary_tags_content", KATTR__MAX);
2720 if (kerr != KCGI_OK)
2721 goto done;
2722 summary_header_displayed = 1;
2725 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2726 KATTR_ID, "tags_wrapper", KATTR__MAX);
2727 if (kerr != KCGI_OK)
2728 goto done;
2729 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2730 KATTR_ID, "tags_age", KATTR__MAX);
2731 if (kerr != KCGI_OK)
2732 goto done;
2733 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
2734 if (error)
2735 goto done;
2736 kerr = khtml_puts(gw_trans->gw_html_req,
2737 age ? age : "");
2738 if (kerr != KCGI_OK)
2739 goto done;
2740 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2741 if (kerr != KCGI_OK)
2742 goto done;
2743 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2744 KATTR_ID, "tags", KATTR__MAX);
2745 if (kerr != KCGI_OK)
2746 goto done;
2747 kerr = khtml_puts(gw_trans->gw_html_req, refname);
2748 if (kerr != KCGI_OK)
2749 goto done;
2750 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2751 if (kerr != KCGI_OK)
2752 goto done;
2753 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2754 KATTR_ID, "tags_name", KATTR__MAX);
2755 if (kerr != KCGI_OK)
2756 goto done;
2757 if (asprintf(&href_tag, "?path=%s&action=tag&commit=%s",
2758 gw_trans->repo_name, id_str) == -1) {
2759 error = got_error_from_errno("asprintf");
2760 goto done;
2762 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2763 KATTR_HREF, href_tag, KATTR__MAX);
2764 if (kerr != KCGI_OK)
2765 goto done;
2766 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
2767 if (kerr != KCGI_OK)
2768 goto done;
2769 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2770 if (kerr != KCGI_OK)
2771 goto done;
2773 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2774 KATTR_ID, "navs_wrapper", KATTR__MAX);
2775 if (kerr != KCGI_OK)
2776 goto done;
2777 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2778 KATTR_ID, "navs", KATTR__MAX);
2779 if (kerr != KCGI_OK)
2780 goto done;
2782 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2783 KATTR_HREF, href_tag, KATTR__MAX);
2784 if (kerr != KCGI_OK)
2785 goto done;
2786 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
2787 if (kerr != KCGI_OK)
2788 goto done;
2789 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2790 if (kerr != KCGI_OK)
2791 goto done;
2793 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2794 if (kerr != KCGI_OK)
2795 goto done;
2796 if (asprintf(&href_briefs,
2797 "?path=%s&action=briefs&commit=%s",
2798 gw_trans->repo_name, id_str) == -1) {
2799 error = got_error_from_errno("asprintf");
2800 goto done;
2802 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2803 KATTR_HREF, href_briefs, KATTR__MAX);
2804 if (kerr != KCGI_OK)
2805 goto done;
2806 kerr = khtml_puts(gw_trans->gw_html_req,
2807 "commit briefs");
2808 if (kerr != KCGI_OK)
2809 goto done;
2810 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2811 if (kerr != KCGI_OK)
2812 goto done;
2814 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2815 if (kerr != KCGI_OK)
2816 goto done;
2818 if (asprintf(&href_commits,
2819 "?path=%s&action=commits&commit=%s",
2820 gw_trans->repo_name, id_str) == -1) {
2821 error = got_error_from_errno("asprintf");
2822 goto done;
2824 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2825 KATTR_HREF, href_commits, KATTR__MAX);
2826 if (kerr != KCGI_OK)
2827 goto done;
2828 kerr = khtml_puts(gw_trans->gw_html_req,
2829 "commits");
2830 if (kerr != KCGI_OK)
2831 goto done;
2832 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2833 if (kerr != KCGI_OK)
2834 goto done;
2836 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2837 KATTR_ID, "dotted_line", KATTR__MAX);
2838 if (kerr != KCGI_OK)
2839 goto done;
2840 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2841 if (kerr != KCGI_OK)
2842 goto done;
2843 break;
2844 case TAGFULL:
2845 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2846 KATTR_ID, "tag_info_date_title", KATTR__MAX);
2847 if (kerr != KCGI_OK)
2848 goto done;
2849 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
2850 if (kerr != KCGI_OK)
2851 goto done;
2852 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2853 if (kerr != KCGI_OK)
2854 goto done;
2855 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2856 KATTR_ID, "tag_info_date", KATTR__MAX);
2857 if (kerr != KCGI_OK)
2858 goto done;
2859 error = gw_get_time_str(&age, tagger_time, TM_LONG);
2860 if (error)
2861 goto done;
2862 kerr = khtml_puts(gw_trans->gw_html_req,
2863 age ? age : "");
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;
2870 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2871 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
2872 if (kerr != KCGI_OK)
2873 goto done;
2874 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
2875 if (kerr != KCGI_OK)
2876 goto done;
2877 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2878 if (kerr != KCGI_OK)
2879 goto done;
2880 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2881 KATTR_ID, "tag_info_date", KATTR__MAX);
2882 if (kerr != KCGI_OK)
2883 goto done;
2884 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
2885 if (kerr != KCGI_OK)
2886 goto done;
2887 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2888 if (kerr != KCGI_OK)
2889 goto done;
2891 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2892 KATTR_ID, "tag_info", KATTR__MAX);
2893 if (kerr != KCGI_OK)
2894 goto done;
2895 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
2896 if (kerr != KCGI_OK)
2897 goto done;
2898 break;
2899 default:
2900 break;
2902 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2903 if (kerr != KCGI_OK)
2904 goto done;
2906 if (limit && --limit == 0)
2907 break;
2909 if (tag)
2910 got_object_tag_close(tag);
2911 tag = NULL;
2912 free(id_str);
2913 id_str = NULL;
2914 free(age);
2915 age = NULL;
2916 free(tag_commit0);
2917 tag_commit0 = NULL;
2918 free(href_tag);
2919 href_tag = NULL;
2920 free(href_briefs);
2921 href_briefs = NULL;
2922 free(href_commits);
2923 href_commits = NULL;
2925 done:
2926 if (tag)
2927 got_object_tag_close(tag);
2928 free(id_str);
2929 free(age);
2930 free(tag_commit0);
2931 free(href_tag);
2932 free(href_briefs);
2933 free(href_commits);
2934 got_ref_list_free(&refs);
2935 if (repo)
2936 got_repo_close(repo);
2937 if (error == NULL && kerr != KCGI_OK)
2938 error = gw_kcgi_error(kerr);
2939 return error;
2942 static void
2943 gw_free_headers(struct gw_header *header)
2945 free(header->id);
2946 free(header->path);
2947 if (header->commit != NULL)
2948 got_object_commit_close(header->commit);
2949 if (header->repo)
2950 got_repo_close(header->repo);
2951 free(header->refs_str);
2952 free(header->commit_id);
2953 free(header->author);
2954 free(header->committer);
2955 free(header->parent_id);
2956 free(header->tree_id);
2957 free(header->commit_msg);
2960 static struct gw_header *
2961 gw_init_header()
2963 struct gw_header *header;
2965 header = malloc(sizeof(*header));
2966 if (header == NULL)
2967 return NULL;
2969 header->repo = NULL;
2970 header->commit = NULL;
2971 header->id = NULL;
2972 header->path = NULL;
2973 SIMPLEQ_INIT(&header->refs);
2975 return header;
2978 static const struct got_error *
2979 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
2980 int limit)
2982 const struct got_error *error = NULL;
2983 struct got_commit_graph *graph = NULL;
2985 error = got_commit_graph_open(&graph, header->path, 0);
2986 if (error)
2987 return error;
2989 error = got_commit_graph_iter_start(graph, header->id, header->repo,
2990 NULL, NULL);
2991 if (error)
2992 goto done;
2994 for (;;) {
2995 error = got_commit_graph_iter_next(&header->id, graph,
2996 header->repo, NULL, NULL);
2997 if (error) {
2998 if (error->code == GOT_ERR_ITER_COMPLETED)
2999 error = NULL;
3000 goto done;
3002 if (header->id == NULL)
3003 goto done;
3005 error = got_object_open_as_commit(&header->commit, header->repo,
3006 header->id);
3007 if (error)
3008 goto done;
3010 error = gw_get_commit(gw_trans, header);
3011 if (limit > 1) {
3012 struct gw_header *n_header = NULL;
3013 if ((n_header = gw_init_header()) == NULL) {
3014 error = got_error_from_errno("malloc");
3015 goto done;
3018 error = gw_strdup_string(&n_header->refs_str,
3019 header->refs_str, NULL);
3020 if (error)
3021 goto done;
3022 error = gw_strdup_string(&n_header->commit_id,
3023 header->commit_id, NULL);
3024 if (error)
3025 goto done;
3026 error = gw_strdup_string(&n_header->parent_id,
3027 header->parent_id, NULL);
3028 if (error)
3029 goto done;
3030 error = gw_strdup_string(&n_header->tree_id,
3031 header->tree_id, NULL);
3032 if (error)
3033 goto done;
3034 error = gw_strdup_string(&n_header->author,
3035 header->author, NULL);
3036 if (error)
3037 goto done;
3038 error = gw_strdup_string(&n_header->committer,
3039 header->committer, NULL);
3040 if (error)
3041 goto done;
3042 error = gw_strdup_string(&n_header->commit_msg,
3043 header->commit_msg, NULL);
3044 if (error)
3045 goto done;
3046 n_header->committer_time = header->committer_time;
3047 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3048 entry);
3050 if (error || (limit && --limit == 0))
3051 break;
3053 done:
3054 if (graph)
3055 got_commit_graph_close(graph);
3056 return error;
3059 static const struct got_error *
3060 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
3062 const struct got_error *error = NULL;
3063 struct got_reflist_entry *re;
3064 struct got_object_id *id2 = NULL;
3065 struct got_object_qid *parent_id;
3066 char *refs_str = NULL, *commit_msg = NULL, *commit_msg0;
3068 /*print commit*/
3069 SIMPLEQ_FOREACH(re, &header->refs, entry) {
3070 char *s;
3071 const char *name;
3072 struct got_tag_object *tag = NULL;
3073 int cmp;
3075 name = got_ref_get_name(re->ref);
3076 if (strcmp(name, GOT_REF_HEAD) == 0)
3077 continue;
3078 if (strncmp(name, "refs/", 5) == 0)
3079 name += 5;
3080 if (strncmp(name, "got/", 4) == 0)
3081 continue;
3082 if (strncmp(name, "heads/", 6) == 0)
3083 name += 6;
3084 if (strncmp(name, "remotes/", 8) == 0)
3085 name += 8;
3086 if (strncmp(name, "tags/", 5) == 0) {
3087 error = got_object_open_as_tag(&tag, header->repo,
3088 re->id);
3089 if (error) {
3090 if (error->code != GOT_ERR_OBJ_TYPE)
3091 continue;
3093 * Ref points at something other
3094 * than a tag.
3096 error = NULL;
3097 tag = NULL;
3100 cmp = got_object_id_cmp(tag ?
3101 got_object_tag_get_object_id(tag) : re->id, header->id);
3102 if (tag)
3103 got_object_tag_close(tag);
3104 if (cmp != 0)
3105 continue;
3106 s = refs_str;
3107 if (asprintf(&refs_str, "%s%s%s", s ? s : "",
3108 s ? ", " : "", name) == -1) {
3109 error = got_error_from_errno("asprintf");
3110 free(s);
3111 return error;
3113 error = gw_strdup_string(&header->refs_str, refs_str, NULL);
3114 free(s);
3115 if (error)
3116 return error;
3119 if (refs_str == NULL) {
3120 error = gw_strdup_string(&header->refs_str, "", NULL);
3121 if (error)
3122 return error;
3124 free(refs_str);
3126 error = got_object_id_str(&header->commit_id, header->id);
3127 if (error)
3128 return error;
3130 error = got_object_id_str(&header->tree_id,
3131 got_object_commit_get_tree_id(header->commit));
3132 if (error)
3133 return error;
3135 if (gw_trans->action == GW_DIFF) {
3136 parent_id = SIMPLEQ_FIRST(
3137 got_object_commit_get_parent_ids(header->commit));
3138 if (parent_id != NULL) {
3139 id2 = got_object_id_dup(parent_id->id);
3140 free (parent_id);
3141 error = got_object_id_str(&header->parent_id, id2);
3142 if (error)
3143 return error;
3144 free(id2);
3145 } else {
3146 error = gw_strdup_string(&header->parent_id,
3147 "/dev/null", NULL);
3148 if (error)
3149 return error;
3151 } else {
3152 error = gw_strdup_string(&header->parent_id, "", NULL);
3153 if (error)
3154 return error;
3157 header->committer_time =
3158 got_object_commit_get_committer_time(header->commit);
3160 error = gw_strdup_string(&header->author, NULL,
3161 got_object_commit_get_author(header->commit));
3162 if (error)
3163 return error;
3164 error = gw_strdup_string(&header->committer, NULL,
3165 got_object_commit_get_committer(header->commit));
3166 if (error)
3167 return error;
3168 error = got_object_commit_get_logmsg(&commit_msg0, header->commit);
3169 if (error)
3170 return error;
3172 commit_msg = commit_msg0;
3173 while (*commit_msg == '\n')
3174 commit_msg++;
3176 error = gw_strdup_string(&header->commit_msg, commit_msg, NULL);
3177 free(commit_msg0);
3178 return error;
3181 static const struct got_error *
3182 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3184 const struct got_error *error = NULL;
3185 char *in_repo_path = NULL;
3187 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
3188 if (error)
3189 return error;
3191 if (gw_trans->commit == NULL) {
3192 struct got_reference *head_ref;
3193 error = got_ref_open(&head_ref, header->repo,
3194 gw_trans->headref, 0);
3195 if (error)
3196 return error;
3198 error = got_ref_resolve(&header->id, header->repo, head_ref);
3199 got_ref_close(head_ref);
3200 if (error)
3201 return error;
3203 error = got_object_open_as_commit(&header->commit,
3204 header->repo, header->id);
3205 } else {
3206 struct got_reference *ref;
3207 error = got_ref_open(&ref, header->repo, gw_trans->commit, 0);
3208 if (error == NULL) {
3209 int obj_type;
3210 error = got_ref_resolve(&header->id, header->repo, ref);
3211 got_ref_close(ref);
3212 if (error)
3213 return error;
3214 error = got_object_get_type(&obj_type, header->repo,
3215 header->id);
3216 if (error)
3217 return error;
3218 if (obj_type == GOT_OBJ_TYPE_TAG) {
3219 struct got_tag_object *tag;
3220 error = got_object_open_as_tag(&tag,
3221 header->repo, header->id);
3222 if (error)
3223 return error;
3224 if (got_object_tag_get_object_type(tag) !=
3225 GOT_OBJ_TYPE_COMMIT) {
3226 got_object_tag_close(tag);
3227 error = got_error(GOT_ERR_OBJ_TYPE);
3228 return error;
3230 free(header->id);
3231 header->id = got_object_id_dup(
3232 got_object_tag_get_object_id(tag));
3233 if (header->id == NULL)
3234 error = got_error_from_errno(
3235 "got_object_id_dup");
3236 got_object_tag_close(tag);
3237 if (error)
3238 return error;
3239 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3240 error = got_error(GOT_ERR_OBJ_TYPE);
3241 return error;
3243 error = got_object_open_as_commit(&header->commit,
3244 header->repo, header->id);
3245 if (error)
3246 return error;
3248 if (header->commit == NULL) {
3249 error = got_repo_match_object_id_prefix(&header->id,
3250 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
3251 header->repo);
3252 if (error)
3253 return error;
3255 error = got_repo_match_object_id_prefix(&header->id,
3256 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
3257 header->repo);
3260 error = got_repo_map_path(&in_repo_path, header->repo,
3261 gw_trans->repo_path, 1);
3262 if (error)
3263 return error;
3265 if (in_repo_path) {
3266 error = gw_strdup_string(&header->path, in_repo_path, NULL);
3267 if (error) {
3268 free(in_repo_path);
3269 return error;
3272 free(in_repo_path);
3274 error = got_ref_list(&header->refs, header->repo, NULL,
3275 got_ref_cmp_by_name, NULL);
3276 if (error)
3277 return error;
3279 error = gw_get_commits(gw_trans, header, limit);
3280 return error;
3283 struct blame_line {
3284 int annotated;
3285 char *id_str;
3286 char *committer;
3287 char datebuf[11]; /* YYYY-MM-DD + NUL */
3290 struct gw_blame_cb_args {
3291 struct blame_line *lines;
3292 int nlines;
3293 int nlines_prec;
3294 int lineno_cur;
3295 off_t *line_offsets;
3296 FILE *f;
3297 struct got_repository *repo;
3298 struct gw_trans *gw_trans;
3301 static const struct got_error *
3302 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3304 const struct got_error *err = NULL;
3305 struct gw_blame_cb_args *a = arg;
3306 struct blame_line *bline;
3307 char *line = NULL;
3308 size_t linesize = 0;
3309 struct got_commit_object *commit = NULL;
3310 off_t offset;
3311 struct tm tm;
3312 time_t committer_time;
3313 enum kcgi_err kerr = KCGI_OK;
3315 if (nlines != a->nlines ||
3316 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3317 return got_error(GOT_ERR_RANGE);
3319 if (lineno == -1)
3320 return NULL; /* no change in this commit */
3322 /* Annotate this line. */
3323 bline = &a->lines[lineno - 1];
3324 if (bline->annotated)
3325 return NULL;
3326 err = got_object_id_str(&bline->id_str, id);
3327 if (err)
3328 return err;
3330 err = got_object_open_as_commit(&commit, a->repo, id);
3331 if (err)
3332 goto done;
3334 err = gw_strdup_string(&bline->committer, NULL,
3335 got_object_commit_get_committer(commit));
3336 if (err)
3337 goto done;
3339 committer_time = got_object_commit_get_committer_time(commit);
3340 if (localtime_r(&committer_time, &tm) == NULL)
3341 return got_error_from_errno("localtime_r");
3342 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3343 &tm) >= sizeof(bline->datebuf)) {
3344 err = got_error(GOT_ERR_NO_SPACE);
3345 goto done;
3347 bline->annotated = 1;
3349 /* Print lines annotated so far. */
3350 bline = &a->lines[a->lineno_cur - 1];
3351 if (!bline->annotated)
3352 goto done;
3354 offset = a->line_offsets[a->lineno_cur - 1];
3355 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3356 err = got_error_from_errno("fseeko");
3357 goto done;
3360 while (bline->annotated) {
3361 char *smallerthan, *at, *nl, *committer;
3362 char *lineno = NULL, *href_diff = NULL, *href_link = NULL;
3363 size_t len;
3365 if (getline(&line, &linesize, a->f) == -1) {
3366 if (ferror(a->f))
3367 err = got_error_from_errno("getline");
3368 break;
3371 committer = bline->committer;
3372 smallerthan = strchr(committer, '<');
3373 if (smallerthan && smallerthan[1] != '\0')
3374 committer = smallerthan + 1;
3375 at = strchr(committer, '@');
3376 if (at)
3377 *at = '\0';
3378 len = strlen(committer);
3379 if (len >= 9)
3380 committer[8] = '\0';
3382 nl = strchr(line, '\n');
3383 if (nl)
3384 *nl = '\0';
3386 if (a->gw_trans->repo_folder == NULL) {
3387 err = gw_strdup_string(&a->gw_trans->repo_folder, "",
3388 NULL);
3389 if (err)
3390 goto err;
3392 if (a->gw_trans->repo_folder == NULL)
3393 goto err;
3395 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3396 "blame_wrapper", KATTR__MAX);
3397 if (kerr != KCGI_OK)
3398 goto err;
3399 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3400 "blame_number", KATTR__MAX);
3401 if (kerr != KCGI_OK)
3402 goto err;
3403 if (asprintf(&lineno, "%.*d", a->nlines_prec,
3404 a->lineno_cur) == -1)
3405 goto err;
3406 kerr = khtml_puts(a->gw_trans->gw_html_req, lineno);
3407 if (kerr != KCGI_OK)
3408 goto err;
3409 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3410 if (kerr != KCGI_OK)
3411 goto err;
3413 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3414 "blame_hash", KATTR__MAX);
3415 if (kerr != KCGI_OK)
3416 goto err;
3417 if (asprintf(&href_diff,
3418 "?path=%s&action=diff&commit=%s&file=%s&folder=%s",
3419 a->gw_trans->repo_name, bline->id_str,
3420 a->gw_trans->repo_file, a->gw_trans->repo_folder) == -1) {
3421 err = got_error_from_errno("asprintf");
3422 goto err;
3424 if (asprintf(&href_link, "%.8s", bline->id_str) == -1) {
3425 err = got_error_from_errno("asprintf");
3426 goto err;
3428 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3429 KATTR_HREF, href_diff, KATTR__MAX);
3430 if (kerr != KCGI_OK)
3431 goto done;
3432 kerr = khtml_puts(a->gw_trans->gw_html_req, href_link);
3433 if (kerr != KCGI_OK)
3434 goto err;
3435 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3436 if (kerr != KCGI_OK)
3437 goto err;
3439 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3440 "blame_date", KATTR__MAX);
3441 if (kerr != KCGI_OK)
3442 goto err;
3443 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3444 if (kerr != KCGI_OK)
3445 goto err;
3446 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3447 if (kerr != KCGI_OK)
3448 goto err;
3450 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3451 "blame_author", KATTR__MAX);
3452 if (kerr != KCGI_OK)
3453 goto err;
3454 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
3455 if (kerr != KCGI_OK)
3456 goto err;
3457 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3458 if (kerr != KCGI_OK)
3459 goto err;
3461 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3462 "blame_code", KATTR__MAX);
3463 if (kerr != KCGI_OK)
3464 goto err;
3465 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
3466 if (kerr != KCGI_OK)
3467 goto err;
3468 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3469 if (kerr != KCGI_OK)
3470 goto err;
3472 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3473 if (kerr != KCGI_OK)
3474 goto err;
3476 a->lineno_cur++;
3477 bline = &a->lines[a->lineno_cur - 1];
3478 err:
3479 free(lineno);
3480 free(href_diff);
3481 free(href_link);
3483 done:
3484 if (commit)
3485 got_object_commit_close(commit);
3486 free(line);
3487 if (err == NULL && kerr != KCGI_OK)
3488 err = gw_kcgi_error(kerr);
3489 return err;
3492 static const struct got_error *
3493 gw_output_file_blame(struct gw_trans *gw_trans)
3495 const struct got_error *error = NULL;
3496 struct got_repository *repo = NULL;
3497 struct got_object_id *obj_id = NULL;
3498 struct got_object_id *commit_id = NULL;
3499 struct got_blob_object *blob = NULL;
3500 char *path = NULL, *in_repo_path = NULL;
3501 struct gw_blame_cb_args bca;
3502 int i, obj_type;
3503 size_t filesize;
3505 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3506 if (error)
3507 return error;
3509 if (asprintf(&path, "%s%s%s",
3510 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3511 gw_trans->repo_folder ? "/" : "",
3512 gw_trans->repo_file) == -1) {
3513 error = got_error_from_errno("asprintf");
3514 goto done;
3517 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3518 if (error)
3519 goto done;
3521 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3522 GOT_OBJ_TYPE_COMMIT, 1, repo);
3523 if (error)
3524 goto done;
3526 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3527 if (error)
3528 goto done;
3530 if (obj_id == NULL) {
3531 error = got_error(GOT_ERR_NO_OBJ);
3532 goto done;
3535 error = got_object_get_type(&obj_type, repo, obj_id);
3536 if (error)
3537 goto done;
3539 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3540 error = got_error(GOT_ERR_OBJ_TYPE);
3541 goto done;
3544 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3545 if (error)
3546 goto done;
3548 bca.f = got_opentemp();
3549 if (bca.f == NULL) {
3550 error = got_error_from_errno("got_opentemp");
3551 goto done;
3553 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3554 &bca.line_offsets, bca.f, blob);
3555 if (error || bca.nlines == 0)
3556 goto done;
3558 /* Don't include \n at EOF in the blame line count. */
3559 if (bca.line_offsets[bca.nlines - 1] == filesize)
3560 bca.nlines--;
3562 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3563 if (bca.lines == NULL) {
3564 error = got_error_from_errno("calloc");
3565 goto done;
3567 bca.lineno_cur = 1;
3568 bca.nlines_prec = 0;
3569 i = bca.nlines;
3570 while (i > 0) {
3571 i /= 10;
3572 bca.nlines_prec++;
3574 bca.repo = repo;
3575 bca.gw_trans = gw_trans;
3577 error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
3578 NULL, NULL);
3579 done:
3580 free(bca.line_offsets);
3581 free(in_repo_path);
3582 free(commit_id);
3583 free(obj_id);
3584 free(path);
3586 for (i = 0; i < bca.nlines; i++) {
3587 struct blame_line *bline = &bca.lines[i];
3588 free(bline->id_str);
3589 free(bline->committer);
3591 free(bca.lines);
3592 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3593 error = got_error_from_errno("fclose");
3594 if (blob)
3595 got_object_blob_close(blob);
3596 if (repo)
3597 got_repo_close(repo);
3598 return error;
3601 static const struct got_error *
3602 gw_output_blob_buf(struct gw_trans *gw_trans)
3604 const struct got_error *error = NULL;
3605 struct got_repository *repo = NULL;
3606 struct got_object_id *obj_id = NULL;
3607 struct got_object_id *commit_id = NULL;
3608 struct got_blob_object *blob = NULL;
3609 char *path = NULL, *in_repo_path = NULL;
3610 int obj_type, set_mime = 0;
3611 size_t len, hdrlen;
3612 const uint8_t *buf;
3613 enum kcgi_err kerr = KCGI_OK;
3615 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3616 if (error)
3617 return error;
3619 if (asprintf(&path, "%s%s%s",
3620 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3621 gw_trans->repo_folder ? "/" : "",
3622 gw_trans->repo_file) == -1) {
3623 error = got_error_from_errno("asprintf");
3624 goto done;
3627 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3628 if (error)
3629 goto done;
3631 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3632 GOT_OBJ_TYPE_COMMIT, 1, repo);
3633 if (error)
3634 goto done;
3636 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3637 if (error)
3638 goto done;
3640 if (obj_id == NULL) {
3641 error = got_error(GOT_ERR_NO_OBJ);
3642 goto done;
3645 error = got_object_get_type(&obj_type, repo, obj_id);
3646 if (error)
3647 goto done;
3649 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3650 error = got_error(GOT_ERR_OBJ_TYPE);
3651 goto done;
3654 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3655 if (error)
3656 goto done;
3658 hdrlen = got_object_blob_get_hdrlen(blob);
3659 do {
3660 error = got_object_blob_read_block(&len, blob);
3661 if (error)
3662 goto done;
3663 buf = got_object_blob_get_read_buf(blob);
3666 * Skip blob object header first time around,
3667 * which also contains a zero byte.
3669 buf += hdrlen;
3670 if (set_mime == 0) {
3671 if (isbinary(buf, len - hdrlen))
3672 gw_trans->mime = KMIME_APP_OCTET_STREAM;
3673 else
3674 gw_trans->mime = KMIME_TEXT_PLAIN;
3675 set_mime = 1;
3676 error = gw_display_index(gw_trans);
3677 if (error)
3678 goto done;
3680 khttp_write(gw_trans->gw_req, buf, len - hdrlen);
3681 hdrlen = 0;
3682 } while (len != 0);
3683 done:
3684 free(in_repo_path);
3685 free(commit_id);
3686 free(obj_id);
3687 free(path);
3688 if (blob)
3689 got_object_blob_close(blob);
3690 if (repo)
3691 got_repo_close(repo);
3692 if (error == NULL && kerr != KCGI_OK)
3693 error = gw_kcgi_error(kerr);
3694 return error;
3697 static const struct got_error *
3698 gw_output_repo_tree(struct gw_trans *gw_trans)
3700 const struct got_error *error = NULL;
3701 struct got_repository *repo = NULL;
3702 struct got_object_id *tree_id = NULL, *commit_id = NULL;
3703 struct got_tree_object *tree = NULL;
3704 char *path = NULL, *in_repo_path = NULL;
3705 char *id_str = NULL;
3706 char *build_folder = NULL;
3707 char *href_blob = NULL, *href_blame = NULL;
3708 const char *class = NULL;
3709 int nentries, i, class_flip = 0;
3710 enum kcgi_err kerr = KCGI_OK;
3712 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3713 if (error)
3714 return error;
3716 if (gw_trans->repo_folder != NULL) {
3717 error = gw_strdup_string(&path, gw_trans->repo_folder, NULL);
3718 if (error)
3719 goto done;
3720 } else {
3721 error = got_repo_map_path(&in_repo_path, repo,
3722 gw_trans->repo_path, 1);
3723 if (error)
3724 goto done;
3725 free(path);
3726 path = in_repo_path;
3729 if (gw_trans->commit == NULL) {
3730 struct got_reference *head_ref;
3731 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
3732 if (error)
3733 goto done;
3734 error = got_ref_resolve(&commit_id, repo, head_ref);
3735 if (error)
3736 goto done;
3737 got_ref_close(head_ref);
3739 } else {
3740 error = got_repo_match_object_id(&commit_id, NULL,
3741 gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
3742 if (error)
3743 goto done;
3746 error = got_object_id_str(&gw_trans->commit, commit_id);
3747 if (error)
3748 goto done;
3750 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
3751 if (error)
3752 goto done;
3754 error = got_object_open_as_tree(&tree, repo, tree_id);
3755 if (error)
3756 goto done;
3758 nentries = got_object_tree_get_nentries(tree);
3759 for (i = 0; i < nentries; i++) {
3760 struct got_tree_entry *te;
3761 const char *modestr = "";
3762 mode_t mode;
3764 te = got_object_tree_get_entry(tree, i);
3766 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
3767 if (error)
3768 goto done;
3770 mode = got_tree_entry_get_mode(te);
3771 if (got_object_tree_entry_is_submodule(te))
3772 modestr = "$";
3773 else if (S_ISLNK(mode))
3774 modestr = "@";
3775 else if (S_ISDIR(mode))
3776 modestr = "/";
3777 else if (mode & S_IXUSR)
3778 modestr = "*";
3780 if (class_flip == 0) {
3781 class = "back_lightgray";
3782 class_flip = 1;
3783 } else {
3784 class = "back_white";
3785 class_flip = 0;
3788 if (S_ISDIR(mode)) {
3789 if (asprintf(&build_folder, "%s/%s",
3790 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3791 got_tree_entry_get_name(te)) == -1) {
3792 error = got_error_from_errno(
3793 "asprintf");
3794 goto done;
3796 if (asprintf(&href_blob,
3797 "?path=%s&action=%s&commit=%s&folder=%s",
3798 gw_trans->repo_name, gw_trans->action_name,
3799 gw_trans->commit, build_folder) == -1) {
3800 error = got_error_from_errno("asprintf");
3801 goto done;
3804 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3805 KATTR_ID, "tree_wrapper", KATTR__MAX);
3806 if (kerr != KCGI_OK)
3807 goto done;
3808 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3809 KATTR_ID, "tree_line", KATTR_CLASS, class,
3810 KATTR__MAX);
3811 if (kerr != KCGI_OK)
3812 goto done;
3813 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3814 KATTR_HREF, href_blob, KATTR_CLASS,
3815 "diff_directory", KATTR__MAX);
3816 if (kerr != KCGI_OK)
3817 goto done;
3818 kerr = khtml_puts(gw_trans->gw_html_req,
3819 got_tree_entry_get_name(te));
3820 if (kerr != KCGI_OK)
3821 goto done;
3822 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3823 if (kerr != KCGI_OK)
3824 goto done;
3825 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3826 if (kerr != KCGI_OK)
3827 goto done;
3828 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3829 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
3830 KATTR__MAX);
3831 if (kerr != KCGI_OK)
3832 goto done;
3833 kerr = khtml_entity(gw_trans->gw_html_req,
3834 KENTITY_nbsp);
3835 if (kerr != KCGI_OK)
3836 goto done;
3837 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3838 if (kerr != KCGI_OK)
3839 goto done;
3840 } else {
3841 if (asprintf(&href_blob,
3842 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3843 gw_trans->repo_name, "blob", gw_trans->commit,
3844 got_tree_entry_get_name(te),
3845 gw_trans->repo_folder ?
3846 gw_trans->repo_folder : "") == -1) {
3847 error = got_error_from_errno("asprintf");
3848 goto done;
3850 if (asprintf(&href_blame,
3851 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3852 gw_trans->repo_name, "blame", gw_trans->commit,
3853 got_tree_entry_get_name(te),
3854 gw_trans->repo_folder ?
3855 gw_trans->repo_folder : "") == -1) {
3856 error = got_error_from_errno("asprintf");
3857 goto done;
3860 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3861 KATTR_ID, "tree_wrapper", KATTR__MAX);
3862 if (kerr != KCGI_OK)
3863 goto done;
3864 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3865 KATTR_ID, "tree_line", KATTR_CLASS, class,
3866 KATTR__MAX);
3867 if (kerr != KCGI_OK)
3868 goto done;
3869 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3870 KATTR_HREF, href_blob, KATTR__MAX);
3871 if (kerr != KCGI_OK)
3872 goto done;
3873 kerr = khtml_puts(gw_trans->gw_html_req,
3874 got_tree_entry_get_name(te));
3875 if (kerr != KCGI_OK)
3876 goto done;
3877 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3878 if (kerr != KCGI_OK)
3879 goto done;
3880 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3881 if (kerr != KCGI_OK)
3882 goto done;
3883 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3884 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
3885 KATTR__MAX);
3886 if (kerr != KCGI_OK)
3887 goto done;
3889 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3890 KATTR_HREF, href_blob, KATTR__MAX);
3891 if (kerr != KCGI_OK)
3892 goto done;
3893 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
3894 if (kerr != KCGI_OK)
3895 goto done;
3896 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3897 if (kerr != KCGI_OK)
3898 goto done;
3900 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3901 if (kerr != KCGI_OK)
3902 goto done;
3904 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3905 KATTR_HREF, href_blame, KATTR__MAX);
3906 if (kerr != KCGI_OK)
3907 goto done;
3908 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
3909 if (kerr != KCGI_OK)
3910 goto done;
3912 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3913 if (kerr != KCGI_OK)
3914 goto done;
3916 free(id_str);
3917 id_str = NULL;
3918 free(href_blob);
3919 href_blob = NULL;
3920 free(build_folder);
3921 build_folder = NULL;
3923 done:
3924 if (tree)
3925 got_object_tree_close(tree);
3926 if (repo)
3927 got_repo_close(repo);
3928 free(id_str);
3929 free(href_blob);
3930 free(href_blame);
3931 free(in_repo_path);
3932 free(tree_id);
3933 free(build_folder);
3934 if (error == NULL && kerr != KCGI_OK)
3935 error = gw_kcgi_error(kerr);
3936 return error;
3939 static const struct got_error *
3940 gw_output_repo_heads(struct gw_trans *gw_trans)
3942 const struct got_error *error = NULL;
3943 struct got_repository *repo = NULL;
3944 struct got_reflist_head refs;
3945 struct got_reflist_entry *re;
3946 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
3947 char *href_commits = NULL;
3948 enum kcgi_err kerr = KCGI_OK;
3950 SIMPLEQ_INIT(&refs);
3952 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3953 if (error)
3954 goto done;
3956 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
3957 NULL);
3958 if (error)
3959 goto done;
3961 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3962 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
3963 if (kerr != KCGI_OK)
3964 goto done;
3965 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3966 KATTR_ID, "summary_heads_title", KATTR__MAX);
3967 if (kerr != KCGI_OK)
3968 goto done;
3969 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
3970 if (kerr != KCGI_OK)
3971 goto done;
3972 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3973 if (kerr != KCGI_OK)
3974 goto done;
3975 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3976 KATTR_ID, "summary_heads_content", KATTR__MAX);
3977 if (kerr != KCGI_OK)
3978 goto done;
3980 SIMPLEQ_FOREACH(re, &refs, entry) {
3981 char *refname;
3983 error = gw_strdup_string(&refname, NULL,
3984 got_ref_get_name(re->ref));
3985 if (error)
3986 goto done;
3988 if (strncmp(refname, "refs/heads/", 11) != 0) {
3989 free(refname);
3990 continue;
3993 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
3994 refname, TM_DIFF);
3995 if (error)
3996 goto done;
3998 if (strncmp(refname, "refs/heads/", 11) == 0)
3999 refname += 11;
4001 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4002 KATTR_ID, "heads_wrapper", KATTR__MAX);
4003 if (kerr != KCGI_OK)
4004 goto done;
4005 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4006 KATTR_ID, "heads_age", KATTR__MAX);
4007 if (kerr != KCGI_OK)
4008 goto done;
4009 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
4010 if (kerr != KCGI_OK)
4011 goto done;
4012 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4013 if (kerr != KCGI_OK)
4014 goto done;
4015 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4016 KATTR_ID, "heads_space", KATTR__MAX);
4017 if (kerr != KCGI_OK)
4018 goto done;
4019 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
4020 if (kerr != KCGI_OK)
4021 goto done;
4022 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4023 if (kerr != KCGI_OK)
4024 goto done;
4025 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4026 KATTR_ID, "head", KATTR__MAX);
4027 if (kerr != KCGI_OK)
4028 goto done;
4029 if (asprintf(&href_summary,
4030 "?path=%s&action=summary&headref=%s",
4031 gw_trans->repo_name, refname) == -1) {
4032 error = got_error_from_errno("asprintf");
4033 goto done;
4035 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4036 href_summary, KATTR__MAX);
4037 kerr = khtml_puts(gw_trans->gw_html_req, refname);
4038 if (kerr != KCGI_OK)
4039 goto done;
4040 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4041 if (kerr != KCGI_OK)
4042 goto done;
4044 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4045 "navs_wrapper", KATTR__MAX);
4046 if (kerr != KCGI_OK)
4047 goto done;
4048 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4049 "navs", KATTR__MAX);
4050 if (kerr != KCGI_OK)
4051 goto done;
4053 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4054 href_summary, KATTR__MAX);
4055 if (kerr != KCGI_OK)
4056 goto done;
4057 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4058 if (kerr != KCGI_OK)
4059 goto done;
4060 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4061 if (kerr != KCGI_OK)
4062 goto done;
4064 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4065 if (kerr != KCGI_OK)
4066 goto done;
4067 if (asprintf(&href_briefs, "?path=%s&action=briefs&headref=%s",
4068 gw_trans->repo_name, refname) == -1) {
4069 error = got_error_from_errno("asprintf");
4070 goto done;
4072 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4073 href_briefs, KATTR__MAX);
4074 if (kerr != KCGI_OK)
4075 goto done;
4076 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4077 if (kerr != KCGI_OK)
4078 goto done;
4079 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4080 if (kerr != KCGI_OK)
4081 goto done;
4083 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4084 if (kerr != KCGI_OK)
4085 goto done;
4087 if (asprintf(&href_commits,
4088 "?path=%s&action=commits&headref=%s",
4089 gw_trans->repo_name, refname) == -1) {
4090 error = got_error_from_errno("asprintf");
4091 goto done;
4093 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4094 href_commits, KATTR__MAX);
4095 if (kerr != KCGI_OK)
4096 goto done;
4097 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4098 if (kerr != KCGI_OK)
4099 goto done;
4100 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4101 if (kerr != KCGI_OK)
4102 goto done;
4104 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4105 "dotted_line", KATTR__MAX);
4106 if (kerr != KCGI_OK)
4107 goto done;
4108 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4109 if (kerr != KCGI_OK)
4110 goto done;
4111 free(href_summary);
4112 href_summary = NULL;
4113 free(href_briefs);
4114 href_briefs = NULL;
4115 free(href_commits);
4116 href_commits = NULL;
4118 done:
4119 got_ref_list_free(&refs);
4120 free(href_summary);
4121 free(href_briefs);
4122 free(href_commits);
4123 if (repo)
4124 got_repo_close(repo);
4125 return error;
4128 static const struct got_error *
4129 gw_output_site_link(struct gw_trans *gw_trans)
4131 const struct got_error *error = NULL;
4132 char *href_summary = NULL;
4133 enum kcgi_err kerr = KCGI_OK;
4135 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4136 "site_link", KATTR__MAX);
4137 if (kerr != KCGI_OK)
4138 goto done;
4139 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4140 KATTR__MAX);
4141 if (kerr != KCGI_OK)
4142 goto done;
4143 kerr = khtml_puts(gw_trans->gw_html_req,
4144 gw_trans->gw_conf->got_site_link);
4145 if (kerr != KCGI_OK)
4146 goto done;
4147 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4148 if (kerr != KCGI_OK)
4149 goto done;
4151 if (gw_trans->repo_name != NULL) {
4152 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4153 if (kerr != KCGI_OK)
4154 goto done;
4155 if (asprintf(&href_summary, "?path=%s&action=summary",
4156 gw_trans->repo_name) == -1)
4157 goto done;
4158 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4159 href_summary, KATTR__MAX);
4160 if (kerr != KCGI_OK)
4161 goto done;
4162 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4163 if (kerr != KCGI_OK)
4164 goto done;
4165 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4166 if (kerr != KCGI_OK)
4167 goto done;
4168 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4169 if (kerr != KCGI_OK)
4170 goto done;
4171 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->action_name);
4172 if (kerr != KCGI_OK)
4173 goto done;
4176 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4177 if (kerr != KCGI_OK)
4178 goto done;
4179 done:
4180 free(href_summary);
4181 return error;
4184 static const struct got_error *
4185 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4187 const struct got_error *error = NULL;
4188 char *color = NULL;
4189 enum kcgi_err kerr = KCGI_OK;
4191 if (strncmp(buf, "-", 1) == 0)
4192 color = "diff_minus";
4193 else if (strncmp(buf, "+", 1) == 0)
4194 color = "diff_plus";
4195 else if (strncmp(buf, "@@", 2) == 0)
4196 color = "diff_chunk_header";
4197 else if (strncmp(buf, "@@", 2) == 0)
4198 color = "diff_chunk_header";
4199 else if (strncmp(buf, "commit +", 8) == 0)
4200 color = "diff_meta";
4201 else if (strncmp(buf, "commit -", 8) == 0)
4202 color = "diff_meta";
4203 else if (strncmp(buf, "blob +", 6) == 0)
4204 color = "diff_meta";
4205 else if (strncmp(buf, "blob -", 6) == 0)
4206 color = "diff_meta";
4207 else if (strncmp(buf, "file +", 6) == 0)
4208 color = "diff_meta";
4209 else if (strncmp(buf, "file -", 6) == 0)
4210 color = "diff_meta";
4211 else if (strncmp(buf, "from:", 5) == 0)
4212 color = "diff_author";
4213 else if (strncmp(buf, "via:", 4) == 0)
4214 color = "diff_author";
4215 else if (strncmp(buf, "date:", 5) == 0)
4216 color = "diff_date";
4217 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4218 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4219 if (error == NULL && kerr != KCGI_OK)
4220 error = gw_kcgi_error(kerr);
4221 return error;
4224 int
4225 main(int argc, char *argv[])
4227 const struct got_error *error = NULL;
4228 struct gw_trans *gw_trans;
4229 struct gw_dir *dir = NULL, *tdir;
4230 const char *page = "index";
4231 int gw_malloc = 1;
4232 enum kcgi_err kerr;
4234 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4235 errx(1, "malloc");
4237 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4238 errx(1, "malloc");
4240 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4241 errx(1, "malloc");
4243 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4244 errx(1, "malloc");
4246 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4247 if (kerr != KCGI_OK) {
4248 error = gw_kcgi_error(kerr);
4249 goto done;
4252 if ((gw_trans->gw_conf =
4253 malloc(sizeof(struct gotweb_conf))) == NULL) {
4254 gw_malloc = 0;
4255 error = got_error_from_errno("malloc");
4256 goto done;
4259 TAILQ_INIT(&gw_trans->gw_dirs);
4260 TAILQ_INIT(&gw_trans->gw_headers);
4262 gw_trans->page = 0;
4263 gw_trans->repos_total = 0;
4264 gw_trans->repo_path = NULL;
4265 gw_trans->commit = NULL;
4266 error = gw_strdup_string(&gw_trans->headref, GOT_REF_HEAD, NULL);
4267 if (error)
4268 goto done;
4269 gw_trans->mime = KMIME_TEXT_HTML;
4270 gw_trans->gw_tmpl->key = gw_templs;
4271 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4272 gw_trans->gw_tmpl->arg = gw_trans;
4273 gw_trans->gw_tmpl->cb = gw_template;
4274 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
4275 if (error)
4276 goto done;
4278 error = gw_parse_querystring(gw_trans);
4279 if (error)
4280 goto done;
4282 if (gw_trans->action == GW_BLOB)
4283 error = gw_blob(gw_trans);
4284 else
4285 error = gw_display_index(gw_trans);
4286 done:
4287 if (error) {
4288 gw_trans->mime = KMIME_TEXT_PLAIN;
4289 gw_trans->action = GW_ERR;
4290 gw_display_error(gw_trans, error);
4292 if (gw_malloc) {
4293 free(gw_trans->gw_conf->got_repos_path);
4294 free(gw_trans->gw_conf->got_site_name);
4295 free(gw_trans->gw_conf->got_site_owner);
4296 free(gw_trans->gw_conf->got_site_link);
4297 free(gw_trans->gw_conf->got_logo);
4298 free(gw_trans->gw_conf->got_logo_url);
4299 free(gw_trans->gw_conf);
4300 free(gw_trans->commit);
4301 free(gw_trans->repo_path);
4302 free(gw_trans->repo_name);
4303 free(gw_trans->repo_file);
4304 free(gw_trans->action_name);
4305 free(gw_trans->headref);
4307 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4308 free(dir->name);
4309 free(dir->description);
4310 free(dir->age);
4311 free(dir->url);
4312 free(dir->path);
4313 free(dir);
4318 khttp_free(gw_trans->gw_req);
4319 return 0;