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 /* XXX and what is the value of errno ?!? */
313 return got_error_from_errno("strdup");
314 if (str1)
315 *s = strdup(str1);
316 else
317 *s = strdup(str2);
318 if (*s == NULL)
319 return got_error_from_errno("strdup");
320 return NULL;
323 static int
324 isbinary(const uint8_t *buf, size_t n)
326 size_t i;
328 for (i = 0; i < n; i++)
329 if (buf[i] == 0)
330 return 1;
331 return 0;
334 static const struct got_error *
335 gw_blame(struct gw_trans *gw_trans)
337 const struct got_error *error = NULL;
338 struct gw_header *header = NULL;
339 char *age = NULL;
340 enum kcgi_err kerr;
342 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
343 NULL) == -1)
344 return got_error_from_errno("pledge");
346 if ((header = gw_init_header()) == NULL)
347 return got_error_from_errno("malloc");
349 error = gw_apply_unveil(gw_trans->gw_dir->path);
350 if (error)
351 goto done;
353 error = gw_get_header(gw_trans, header, 1);
354 if (error)
355 goto done;
357 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
358 "blame_header_wrapper", KATTR__MAX);
359 if (kerr != KCGI_OK)
360 goto done;
361 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
362 "blame_header", KATTR__MAX);
363 if (kerr != KCGI_OK)
364 goto done;
365 error = gw_get_time_str(&age, header->committer_time,
366 TM_LONG);
367 if (error)
368 goto done;
369 error = gw_gen_age_header(gw_trans, age ?age : "");
370 if (error)
371 goto done;
372 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
373 if (error)
374 goto done;
375 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
376 if (kerr != KCGI_OK)
377 goto done;
378 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
379 "dotted_line", KATTR__MAX);
380 if (kerr != KCGI_OK)
381 goto done;
382 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
383 if (kerr != KCGI_OK)
384 goto done;
386 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
387 "blame", KATTR__MAX);
388 if (kerr != KCGI_OK)
389 goto done;
390 error = gw_output_file_blame(gw_trans);
391 if (error)
392 goto done;
393 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
394 done:
395 got_ref_list_free(&header->refs);
396 gw_free_headers(header);
397 if (error == NULL && kerr != KCGI_OK)
398 error = gw_kcgi_error(kerr);
399 return error;
402 static const struct got_error *
403 gw_blob(struct gw_trans *gw_trans)
405 const struct got_error *error = NULL;
406 struct gw_header *header = NULL;
408 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
409 NULL) == -1)
410 return got_error_from_errno("pledge");
412 if ((header = gw_init_header()) == NULL)
413 return got_error_from_errno("malloc");
415 error = gw_apply_unveil(gw_trans->gw_dir->path);
416 if (error)
417 goto done;
419 error = gw_get_header(gw_trans, header, 1);
420 if (error)
421 goto done;
423 error = gw_output_blob_buf(gw_trans);
424 done:
425 got_ref_list_free(&header->refs);
426 gw_free_headers(header);
427 return error;
430 static const struct got_error *
431 gw_diff(struct gw_trans *gw_trans)
433 const struct got_error *error = NULL;
434 struct gw_header *header = NULL;
435 char *age = NULL;
436 enum kcgi_err kerr = KCGI_OK;
438 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
439 NULL) == -1)
440 return got_error_from_errno("pledge");
442 if ((header = gw_init_header()) == NULL)
443 return got_error_from_errno("malloc");
445 error = gw_apply_unveil(gw_trans->gw_dir->path);
446 if (error)
447 goto done;
449 error = gw_get_header(gw_trans, header, 1);
450 if (error)
451 goto done;
453 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
454 "diff_header_wrapper", KATTR__MAX);
455 if (kerr != KCGI_OK)
456 goto done;
457 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
458 "diff_header", KATTR__MAX);
459 if (kerr != KCGI_OK)
460 goto done;
461 error = gw_gen_diff_header(gw_trans, header->parent_id,
462 header->commit_id);
463 if (error)
464 goto done;
465 error = gw_gen_commit_header(gw_trans, header->commit_id,
466 header->refs_str);
467 if (error)
468 goto done;
469 error = gw_gen_tree_header(gw_trans, header->tree_id);
470 if (error)
471 goto done;
472 error = gw_gen_author_header(gw_trans, header->author);
473 if (error)
474 goto done;
475 error = gw_gen_committer_header(gw_trans, header->author);
476 if (error)
477 goto done;
478 error = gw_get_time_str(&age, header->committer_time,
479 TM_LONG);
480 if (error)
481 goto done;
482 error = gw_gen_age_header(gw_trans, age ?age : "");
483 if (error)
484 goto done;
485 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
486 if (error)
487 goto done;
488 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
489 if (kerr != KCGI_OK)
490 goto done;
491 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
492 "dotted_line", KATTR__MAX);
493 if (kerr != KCGI_OK)
494 goto done;
495 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
496 if (kerr != KCGI_OK)
497 goto done;
499 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
500 "diff", KATTR__MAX);
501 if (kerr != KCGI_OK)
502 goto done;
503 error = gw_output_diff(gw_trans, header);
504 if (error)
505 goto done;
507 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
508 done:
509 got_ref_list_free(&header->refs);
510 gw_free_headers(header);
511 free(age);
512 if (error == NULL && kerr != KCGI_OK)
513 error = gw_kcgi_error(kerr);
514 return error;
517 static const struct got_error *
518 gw_index(struct gw_trans *gw_trans)
520 const struct got_error *error = NULL;
521 struct gw_dir *gw_dir = NULL;
522 char *href_next = NULL, *href_prev = NULL, *href_summary = NULL;
523 char *href_briefs = NULL, *href_commits = NULL, *href_tree = NULL;
524 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
525 enum kcgi_err kerr;
527 if (pledge("stdio rpath proc exec sendfd unveil",
528 NULL) == -1) {
529 error = got_error_from_errno("pledge");
530 return error;
533 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path);
534 if (error)
535 return error;
537 error = gw_load_got_paths(gw_trans);
538 if (error)
539 return error;
541 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
542 "index_header", KATTR__MAX);
543 if (kerr != KCGI_OK)
544 return gw_kcgi_error(kerr);
545 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
546 "index_header_project", KATTR__MAX);
547 if (kerr != KCGI_OK)
548 return gw_kcgi_error(kerr);
549 kerr = khtml_puts(gw_trans->gw_html_req, "Project");
550 if (kerr != KCGI_OK)
551 return gw_kcgi_error(kerr);
552 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
553 if (kerr != KCGI_OK)
554 return gw_kcgi_error(kerr);
556 if (gw_trans->gw_conf->got_show_repo_description) {
557 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
558 "index_header_description", KATTR__MAX);
559 if (kerr != KCGI_OK)
560 return gw_kcgi_error(kerr);
561 kerr = khtml_puts(gw_trans->gw_html_req, "Description");
562 if (kerr != KCGI_OK)
563 return gw_kcgi_error(kerr);
564 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
565 if (kerr != KCGI_OK)
566 return gw_kcgi_error(kerr);
569 if (gw_trans->gw_conf->got_show_repo_owner) {
570 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
571 "index_header_owner", KATTR__MAX);
572 if (kerr != KCGI_OK)
573 return gw_kcgi_error(kerr);
574 kerr = khtml_puts(gw_trans->gw_html_req, "Owner");
575 if (kerr != KCGI_OK)
576 return gw_kcgi_error(kerr);
577 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
578 if (kerr != KCGI_OK)
579 return gw_kcgi_error(kerr);
582 if (gw_trans->gw_conf->got_show_repo_age) {
583 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
584 "index_header_age", KATTR__MAX);
585 if (kerr != KCGI_OK)
586 return gw_kcgi_error(kerr);
587 kerr = khtml_puts(gw_trans->gw_html_req, "Last Change");
588 if (kerr != KCGI_OK)
589 return gw_kcgi_error(kerr);
590 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
591 if (kerr != KCGI_OK)
592 return gw_kcgi_error(kerr);
595 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
596 if (kerr != KCGI_OK)
597 return gw_kcgi_error(kerr);
599 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
600 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
601 "index_wrapper", KATTR__MAX);
602 if (kerr != KCGI_OK)
603 return gw_kcgi_error(kerr);
604 kerr = khtml_puts(gw_trans->gw_html_req,
605 "No repositories found in ");
606 if (kerr != KCGI_OK)
607 return gw_kcgi_error(kerr);
608 kerr = khtml_puts(gw_trans->gw_html_req,
609 gw_trans->gw_conf->got_repos_path);
610 if (kerr != KCGI_OK)
611 return gw_kcgi_error(kerr);
612 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
613 if (kerr != KCGI_OK)
614 return gw_kcgi_error(kerr);
615 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
616 "dotted_line", KATTR__MAX);
617 if (kerr != KCGI_OK)
618 return gw_kcgi_error(kerr);
619 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
620 if (kerr != KCGI_OK)
621 return gw_kcgi_error(kerr);
622 return error;
625 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
626 dir_c++;
628 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
629 if (gw_trans->page > 0 && (gw_trans->page *
630 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
631 prev_disp++;
632 continue;
635 prev_disp++;
637 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
638 "index_wrapper", KATTR__MAX);
639 if (kerr != KCGI_OK)
640 goto done;
642 if (asprintf(&href_summary, "?path=%s&action=summary",
643 gw_dir->name) == -1) {
644 error = got_error_from_errno("asprintf");
645 goto done;
647 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
648 "index_project", KATTR__MAX);
649 if (kerr != KCGI_OK)
650 goto done;
651 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
652 href_summary, KATTR__MAX);
653 if (kerr != KCGI_OK)
654 goto done;
655 kerr = khtml_puts(gw_trans->gw_html_req, gw_dir->name);
656 if (kerr != KCGI_OK)
657 goto done;
658 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
659 if (kerr != KCGI_OK)
660 goto done;
661 if (gw_trans->gw_conf->got_show_repo_description) {
662 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
663 KATTR_ID, "index_project_description", KATTR__MAX);
664 if (kerr != KCGI_OK)
665 goto done;
666 kerr = khtml_puts(gw_trans->gw_html_req,
667 gw_dir->description ? gw_dir->description : "");
668 if (kerr != KCGI_OK)
669 goto done;
670 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
671 if (kerr != KCGI_OK)
672 goto done;
674 if (gw_trans->gw_conf->got_show_repo_owner) {
675 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
676 KATTR_ID, "index_project_owner", KATTR__MAX);
677 if (kerr != KCGI_OK)
678 goto done;
679 kerr = khtml_puts(gw_trans->gw_html_req,
680 gw_dir->owner ? gw_dir->owner : "");
681 if (kerr != KCGI_OK)
682 goto done;
683 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
684 if (kerr != KCGI_OK)
685 goto done;
687 if (gw_trans->gw_conf->got_show_repo_age) {
688 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
689 KATTR_ID, "index_project_age", KATTR__MAX);
690 if (kerr != KCGI_OK)
691 goto done;
692 kerr = khtml_puts(gw_trans->gw_html_req,
693 gw_dir->age ? gw_dir->age : "");
694 if (kerr != KCGI_OK)
695 goto done;
696 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
697 if (kerr != KCGI_OK)
698 goto done;
701 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
702 "navs_wrapper", KATTR__MAX);
703 if (kerr != KCGI_OK)
704 goto done;
705 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
706 "navs", KATTR__MAX);
707 if (kerr != KCGI_OK)
708 goto done;
710 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
711 href_summary, KATTR__MAX);
712 if (kerr != KCGI_OK)
713 goto done;
714 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
715 if (kerr != KCGI_OK)
716 goto done;
717 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
718 if (kerr != KCGI_OK)
719 goto done;
721 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
722 if (kerr != KCGI_OK)
723 goto done;
725 if (asprintf(&href_briefs, "?path=%s&action=briefs",
726 gw_dir->name) == -1) {
727 error = got_error_from_errno("asprintf");
728 goto done;
730 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
731 href_briefs, KATTR__MAX);
732 if (kerr != KCGI_OK)
733 goto done;
734 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
735 if (kerr != KCGI_OK)
736 goto done;
737 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
738 if (kerr != KCGI_OK)
739 error = gw_kcgi_error(kerr);
741 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
742 if (kerr != KCGI_OK)
743 goto done;
745 if (asprintf(&href_commits, "?path=%s&action=commits",
746 gw_dir->name) == -1) {
747 error = got_error_from_errno("asprintf");
748 goto done;
750 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
751 href_commits, KATTR__MAX);
752 if (kerr != KCGI_OK)
753 goto done;
754 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
755 if (kerr != KCGI_OK)
756 goto done;
757 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
758 if (kerr != KCGI_OK)
759 goto done;
761 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
762 if (kerr != KCGI_OK)
763 goto done;
765 if (asprintf(&href_tree, "?path=%s&action=tree",
766 gw_dir->name) == -1) {
767 error = got_error_from_errno("asprintf");
768 goto done;
770 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
771 href_tree, KATTR__MAX);
772 if (kerr != KCGI_OK)
773 goto done;
774 kerr = khtml_puts(gw_trans->gw_html_req, "tree");
775 if (kerr != KCGI_OK)
776 goto done;
778 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
779 if (kerr != KCGI_OK)
780 goto done;
781 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
782 "dotted_line", KATTR__MAX);
783 if (kerr != KCGI_OK)
784 goto done;
785 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
786 if (kerr != KCGI_OK)
787 goto done;
789 free(href_summary);
790 href_summary = NULL;
791 free(href_briefs);
792 href_briefs = NULL;
793 free(href_commits);
794 href_commits = NULL;
795 free(href_tree);
796 href_tree = NULL;
798 if (gw_trans->gw_conf->got_max_repos_display == 0)
799 continue;
801 if (next_disp == gw_trans->gw_conf->got_max_repos_display) {
802 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
803 KATTR_ID, "np_wrapper", KATTR__MAX);
804 if (kerr != KCGI_OK)
805 goto done;
806 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
807 KATTR_ID, "nav_prev", KATTR__MAX);
808 if (kerr != KCGI_OK)
809 goto done;
810 } else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
811 (gw_trans->page > 0) &&
812 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
813 prev_disp == gw_trans->repos_total)) {
814 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
815 KATTR_ID, "np_wrapper", KATTR__MAX);
816 if (kerr != KCGI_OK)
817 goto done;
818 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
819 KATTR_ID, "nav_prev", KATTR__MAX);
820 if (kerr != KCGI_OK)
821 goto done;
824 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
825 (gw_trans->page > 0) &&
826 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
827 prev_disp == gw_trans->repos_total)) {
828 if (asprintf(&href_prev, "?page=%d",
829 gw_trans->page - 1) == -1) {
830 error = got_error_from_errno("asprintf");
831 goto done;
833 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
834 KATTR_HREF, href_prev, KATTR__MAX);
835 if (kerr != KCGI_OK)
836 goto done;
837 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
838 if (kerr != KCGI_OK)
839 goto done;
840 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
841 if (kerr != KCGI_OK)
842 goto done;
845 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
846 if (kerr != KCGI_OK)
847 return gw_kcgi_error(kerr);
849 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
850 next_disp == gw_trans->gw_conf->got_max_repos_display &&
851 dir_c != (gw_trans->page + 1) *
852 gw_trans->gw_conf->got_max_repos_display) {
853 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
854 KATTR_ID, "nav_next", KATTR__MAX);
855 if (kerr != KCGI_OK)
856 goto done;
857 if (asprintf(&href_next, "?page=%d",
858 gw_trans->page + 1) == -1) {
859 error = got_error_from_errno("calloc");
860 goto done;
862 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
863 KATTR_HREF, href_next, KATTR__MAX);
864 if (kerr != KCGI_OK)
865 goto done;
866 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
867 if (kerr != KCGI_OK)
868 goto done;
869 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
870 if (kerr != KCGI_OK)
871 goto done;
872 next_disp = 0;
873 break;
876 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
877 (gw_trans->page > 0) &&
878 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
879 prev_disp == gw_trans->repos_total)) {
880 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
881 if (kerr != KCGI_OK)
882 goto done;
884 next_disp++;
886 done:
887 free(href_prev);
888 free(href_next);
889 free(href_summary);
890 free(href_briefs);
891 free(href_commits);
892 free(href_tree);
893 if (error == NULL && kerr != KCGI_OK)
894 error = gw_kcgi_error(kerr);
895 return error;
898 static const struct got_error *
899 gw_commits(struct gw_trans *gw_trans)
901 const struct got_error *error = NULL;
902 struct gw_header *header = NULL, *n_header = NULL;
903 char *age = NULL;
904 char *href_diff = NULL, *href_blob = NULL;
905 enum kcgi_err kerr = KCGI_OK;
907 if ((header = gw_init_header()) == NULL)
908 return got_error_from_errno("malloc");
910 if (pledge("stdio rpath proc exec sendfd unveil",
911 NULL) == -1) {
912 error = got_error_from_errno("pledge");
913 goto done;
916 error = gw_apply_unveil(gw_trans->gw_dir->path);
917 if (error)
918 goto done;
920 error = gw_get_header(gw_trans, header,
921 gw_trans->gw_conf->got_max_commits_display);
922 if (error)
923 goto done;
925 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
926 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
927 "commits_line_wrapper", KATTR__MAX);
928 if (kerr != KCGI_OK)
929 goto done;
930 error = gw_gen_commit_header(gw_trans, n_header->commit_id,
931 n_header->refs_str);
932 if (error)
933 goto done;
934 error = gw_gen_author_header(gw_trans, n_header->author);
935 if (error)
936 goto done;
937 error = gw_gen_committer_header(gw_trans, n_header->author);
938 if (error)
939 goto done;
940 error = gw_get_time_str(&age, n_header->committer_time,
941 TM_LONG);
942 if (error)
943 goto done;
944 error = gw_gen_age_header(gw_trans, age ?age : "");
945 if (error)
946 goto done;
947 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
948 if (kerr != KCGI_OK)
949 goto done;
951 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
952 "dotted_line", KATTR__MAX);
953 if (kerr != KCGI_OK)
954 goto done;
955 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
956 if (kerr != KCGI_OK)
957 goto done;
959 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
960 "commit", KATTR__MAX);
961 if (kerr != KCGI_OK)
962 goto done;
963 kerr = khttp_puts(gw_trans->gw_req, n_header->commit_msg);
964 if (kerr != KCGI_OK)
965 goto done;
966 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
967 if (kerr != KCGI_OK)
968 goto done;
970 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
971 gw_trans->repo_name, n_header->commit_id) == -1) {
972 error = got_error_from_errno("asprintf");
973 goto done;
975 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
976 KATTR_ID, "navs_wrapper", KATTR__MAX);
977 if (kerr != KCGI_OK)
978 goto done;
979 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
980 KATTR_ID, "navs", KATTR__MAX);
981 if (kerr != KCGI_OK)
982 goto done;
983 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
984 KATTR_HREF, href_diff, KATTR__MAX);
985 if (kerr != KCGI_OK)
986 goto done;
987 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
988 if (kerr != KCGI_OK)
989 goto done;
990 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
991 if (kerr != KCGI_OK)
992 goto done;
994 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
995 if (kerr != KCGI_OK)
996 goto done;
998 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
999 gw_trans->repo_name, n_header->commit_id) == -1) {
1000 error = got_error_from_errno("asprintf");
1001 goto done;
1003 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1004 KATTR_HREF, href_blob, KATTR__MAX);
1005 if (kerr != KCGI_OK)
1006 goto done;
1007 khtml_puts(gw_trans->gw_html_req, "tree");
1008 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1009 if (kerr != KCGI_OK)
1010 goto done;
1011 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1012 if (kerr != KCGI_OK)
1013 goto done;
1015 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1016 "solid_line", KATTR__MAX);
1017 if (kerr != KCGI_OK)
1018 goto done;
1019 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1020 if (kerr != KCGI_OK)
1021 goto done;
1023 free(age);
1024 age = NULL;
1026 done:
1027 got_ref_list_free(&header->refs);
1028 gw_free_headers(header);
1029 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1030 gw_free_headers(n_header);
1031 free(age);
1032 free(href_diff);
1033 free(href_blob);
1034 if (error == NULL && kerr != KCGI_OK)
1035 error = gw_kcgi_error(kerr);
1036 return error;
1039 static const struct got_error *
1040 gw_briefs(struct gw_trans *gw_trans)
1042 const struct got_error *error = NULL;
1043 struct gw_header *header = NULL, *n_header = NULL;
1044 char *age = NULL, *age_html = NULL;
1045 char *href_diff = NULL, *href_blob = NULL;
1046 char *newline, *smallerthan;
1047 enum kcgi_err kerr = KCGI_OK;
1049 if ((header = gw_init_header()) == NULL)
1050 return got_error_from_errno("malloc");
1052 if (pledge("stdio rpath proc exec sendfd unveil",
1053 NULL) == -1) {
1054 error = got_error_from_errno("pledge");
1055 goto done;
1058 error = gw_apply_unveil(gw_trans->gw_dir->path);
1059 if (error)
1060 goto done;
1062 if (gw_trans->action == GW_SUMMARY)
1063 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
1064 else
1065 error = gw_get_header(gw_trans, header,
1066 gw_trans->gw_conf->got_max_commits_display);
1067 if (error)
1068 goto done;
1070 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
1071 error = gw_get_time_str(&age, n_header->committer_time,
1072 TM_DIFF);
1073 if (error)
1074 goto done;
1076 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1077 KATTR_ID, "briefs_wrapper", KATTR__MAX);
1078 if (kerr != KCGI_OK)
1079 goto done;
1081 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1082 KATTR_ID, "briefs_age", KATTR__MAX);
1083 if (kerr != KCGI_OK)
1084 goto done;
1085 if (asprintf(&age_html, "%s", age ? age : "") == -1) {
1086 error = got_error_from_errno("asprintf");
1087 goto done;
1089 kerr = khtml_puts(gw_trans->gw_html_req, age_html);
1090 if (kerr != KCGI_OK)
1091 goto done;
1092 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1093 if (kerr != KCGI_OK)
1094 goto done;
1096 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1097 KATTR_ID, "briefs_author", KATTR__MAX);
1098 if (kerr != KCGI_OK)
1099 goto done;
1100 smallerthan = strchr(n_header->author, '<');
1101 if (smallerthan)
1102 *smallerthan = '\0';
1103 kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
1104 if (kerr != KCGI_OK)
1105 goto done;
1106 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1107 if (kerr != KCGI_OK)
1108 goto done;
1110 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
1111 gw_trans->repo_name, n_header->commit_id) == -1) {
1112 error = got_error_from_errno("asprintf");
1113 goto done;
1115 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1116 KATTR_ID, "briefs_log", KATTR__MAX);
1117 if (kerr != KCGI_OK)
1118 goto done;
1119 newline = strchr(n_header->commit_msg, '\n');
1120 if (newline)
1121 *newline = '\0';
1122 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1123 KATTR_HREF, href_diff, KATTR__MAX);
1124 if (kerr != KCGI_OK)
1125 goto done;
1126 kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
1127 if (kerr != KCGI_OK)
1128 goto done;
1129 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1130 if (kerr != KCGI_OK)
1131 goto done;
1133 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1134 KATTR_ID, "navs_wrapper", KATTR__MAX);
1135 if (kerr != KCGI_OK)
1136 goto done;
1137 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1138 KATTR_ID, "navs", KATTR__MAX);
1139 if (kerr != KCGI_OK)
1140 goto done;
1141 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1142 KATTR_HREF, href_diff, KATTR__MAX);
1143 if (kerr != KCGI_OK)
1144 goto done;
1145 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1146 if (kerr != KCGI_OK)
1147 goto done;
1148 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1149 if (kerr != KCGI_OK)
1150 goto done;
1152 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1153 if (kerr != KCGI_OK)
1154 goto done;
1156 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
1157 gw_trans->repo_name, n_header->commit_id) == -1) {
1158 error = got_error_from_errno("asprintf");
1159 goto done;
1161 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1162 KATTR_HREF, href_blob, KATTR__MAX);
1163 if (kerr != KCGI_OK)
1164 goto done;
1165 khtml_puts(gw_trans->gw_html_req, "tree");
1166 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1167 if (kerr != KCGI_OK)
1168 goto done;
1169 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1170 if (kerr != KCGI_OK)
1171 goto done;
1173 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1174 KATTR_ID, "dotted_line", KATTR__MAX);
1175 if (kerr != KCGI_OK)
1176 goto done;
1177 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1178 if (kerr != KCGI_OK)
1179 goto done;
1181 free(age);
1182 age = NULL;
1183 free(age_html);
1184 age_html = NULL;
1185 free(href_diff);
1186 href_diff = NULL;
1187 free(href_blob);
1188 href_blob = NULL;
1190 done:
1191 got_ref_list_free(&header->refs);
1192 gw_free_headers(header);
1193 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1194 gw_free_headers(n_header);
1195 free(age);
1196 free(age_html);
1197 free(href_diff);
1198 free(href_blob);
1199 if (error == NULL && kerr != KCGI_OK)
1200 error = gw_kcgi_error(kerr);
1201 return error;
1204 static const struct got_error *
1205 gw_summary(struct gw_trans *gw_trans)
1207 const struct got_error *error = NULL;
1208 char *age = NULL;
1209 enum kcgi_err kerr = KCGI_OK;
1211 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1212 return got_error_from_errno("pledge");
1214 /* unveil is applied with gw_briefs below */
1216 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1217 "summary_wrapper", KATTR__MAX);
1218 if (kerr != KCGI_OK)
1219 return gw_kcgi_error(kerr);
1221 if (gw_trans->gw_conf->got_show_repo_description &&
1222 gw_trans->gw_dir->description != NULL &&
1223 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1224 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1225 KATTR_ID, "description_title", KATTR__MAX);
1226 if (kerr != KCGI_OK)
1227 goto done;
1228 kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1229 if (kerr != KCGI_OK)
1230 goto done;
1231 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1232 if (kerr != KCGI_OK)
1233 goto done;
1234 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1235 KATTR_ID, "description", KATTR__MAX);
1236 if (kerr != KCGI_OK)
1237 goto done;
1238 kerr = khtml_puts(gw_trans->gw_html_req,
1239 gw_trans->gw_dir->description);
1240 if (kerr != KCGI_OK)
1241 goto done;
1242 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1243 if (kerr != KCGI_OK)
1244 goto done;
1247 if (gw_trans->gw_conf->got_show_repo_owner &&
1248 gw_trans->gw_dir->owner != NULL &&
1249 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
1250 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1251 KATTR_ID, "repo_owner_title", KATTR__MAX);
1252 if (kerr != KCGI_OK)
1253 goto done;
1254 kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1255 if (kerr != KCGI_OK)
1256 goto done;
1257 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1258 if (kerr != KCGI_OK)
1259 goto done;
1260 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1261 KATTR_ID, "repo_owner", KATTR__MAX);
1262 if (kerr != KCGI_OK)
1263 goto done;
1264 kerr = khtml_puts(gw_trans->gw_html_req,
1265 gw_trans->gw_dir->owner);
1266 if (kerr != KCGI_OK)
1267 goto done;
1268 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1269 if (kerr != KCGI_OK)
1270 goto done;
1273 if (gw_trans->gw_conf->got_show_repo_age) {
1274 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1275 "refs/heads", TM_LONG);
1276 if (error)
1277 goto done;
1278 if (age != NULL) {
1279 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1280 KATTR_ID, "last_change_title", KATTR__MAX);
1281 if (kerr != KCGI_OK)
1282 goto done;
1283 kerr = khtml_puts(gw_trans->gw_html_req,
1284 "Last Change: ");
1285 if (kerr != KCGI_OK)
1286 goto done;
1287 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1288 if (kerr != KCGI_OK)
1289 goto done;
1290 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1291 KATTR_ID, "last_change", KATTR__MAX);
1292 if (kerr != KCGI_OK)
1293 goto done;
1294 kerr = khtml_puts(gw_trans->gw_html_req, age);
1295 if (kerr != KCGI_OK)
1296 goto done;
1297 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1298 if (kerr != KCGI_OK)
1299 goto done;
1303 if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1304 gw_trans->gw_dir->url != NULL &&
1305 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1306 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1307 KATTR_ID, "cloneurl_title", KATTR__MAX);
1308 if (kerr != KCGI_OK)
1309 goto done;
1310 kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1311 if (kerr != KCGI_OK)
1312 goto done;
1313 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1314 if (kerr != KCGI_OK)
1315 goto done;
1316 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1317 KATTR_ID, "cloneurl", KATTR__MAX);
1318 if (kerr != KCGI_OK)
1319 goto done;
1320 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1321 if (kerr != KCGI_OK)
1322 goto done;
1323 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1324 if (kerr != KCGI_OK)
1325 goto done;
1328 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1329 if (kerr != KCGI_OK)
1330 goto done;
1332 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1333 "briefs_title_wrapper", KATTR__MAX);
1334 if (kerr != KCGI_OK)
1335 goto done;
1336 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1337 "briefs_title", KATTR__MAX);
1338 if (kerr != KCGI_OK)
1339 goto done;
1340 kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1341 if (kerr != KCGI_OK)
1342 goto done;
1343 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1344 if (kerr != KCGI_OK)
1345 goto done;
1346 error = gw_briefs(gw_trans);
1347 if (error)
1348 goto done;
1350 error = gw_output_repo_tags(gw_trans, NULL, D_MAXSLCOMMDISP,
1351 TAGBRIEF);
1352 if (error)
1353 goto done;
1355 error = gw_output_repo_heads(gw_trans);
1356 done:
1357 free(age);
1358 if (error == NULL && kerr != KCGI_OK)
1359 error = gw_kcgi_error(kerr);
1360 return error;
1363 static const struct got_error *
1364 gw_tree(struct gw_trans *gw_trans)
1366 const struct got_error *error = NULL;
1367 struct gw_header *header = NULL;
1368 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1369 char *age = NULL, *age_html = NULL;
1370 enum kcgi_err kerr;
1372 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1373 return got_error_from_errno("pledge");
1375 if ((header = gw_init_header()) == NULL)
1376 return got_error_from_errno("malloc");
1378 error = gw_apply_unveil(gw_trans->gw_dir->path);
1379 if (error)
1380 goto done;
1382 error = gw_get_header(gw_trans, header, 1);
1383 if (error)
1384 goto done;
1386 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1387 "tree_header_wrapper", KATTR__MAX);
1388 if (kerr != KCGI_OK)
1389 goto done;
1390 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1391 "tree_header", KATTR__MAX);
1392 if (kerr != KCGI_OK)
1393 goto done;
1394 error = gw_gen_tree_header(gw_trans, header->tree_id);
1395 if (error)
1396 goto done;
1397 error = gw_get_time_str(&age, header->committer_time,
1398 TM_LONG);
1399 if (error)
1400 goto done;
1401 error = gw_gen_age_header(gw_trans, age ?age : "");
1402 if (error)
1403 goto done;
1404 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1405 if (error)
1406 goto done;
1407 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1408 if (kerr != KCGI_OK)
1409 goto done;
1410 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1411 "dotted_line", KATTR__MAX);
1412 if (kerr != KCGI_OK)
1413 goto done;
1414 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1415 if (kerr != KCGI_OK)
1416 goto done;
1418 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1419 "tree", KATTR__MAX);
1420 if (kerr != KCGI_OK)
1421 goto done;
1422 error = gw_output_repo_tree(gw_trans);
1423 if (error)
1424 goto done;
1426 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1427 done:
1428 got_ref_list_free(&header->refs);
1429 gw_free_headers(header);
1430 free(tree_html_disp);
1431 free(tree_html);
1432 free(tree);
1433 free(age);
1434 free(age_html);
1435 if (error == NULL && kerr != KCGI_OK)
1436 error = gw_kcgi_error(kerr);
1437 return error;
1440 static const struct got_error *
1441 gw_tag(struct gw_trans *gw_trans)
1443 const struct got_error *error = NULL;
1444 struct gw_header *header = NULL;
1445 enum kcgi_err kerr;
1447 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1448 return got_error_from_errno("pledge");
1450 if ((header = gw_init_header()) == NULL)
1451 return got_error_from_errno("malloc");
1453 error = gw_apply_unveil(gw_trans->gw_dir->path);
1454 if (error)
1455 goto done;
1457 error = gw_get_header(gw_trans, header, 1);
1458 if (error)
1459 goto done;
1461 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1462 "tag_header_wrapper", KATTR__MAX);
1463 if (kerr != KCGI_OK)
1464 goto done;
1465 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1466 "tag_header", KATTR__MAX);
1467 if (kerr != KCGI_OK)
1468 goto done;
1469 error = gw_gen_commit_header(gw_trans, header->commit_id,
1470 header->refs_str);
1471 if (error)
1472 goto done;
1473 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1474 if (error)
1475 goto done;
1476 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1477 if (kerr != KCGI_OK)
1478 goto done;
1479 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1480 "dotted_line", KATTR__MAX);
1481 if (kerr != KCGI_OK)
1482 goto done;
1483 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1484 if (kerr != KCGI_OK)
1485 goto done;
1487 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1488 "tree", KATTR__MAX);
1489 if (kerr != KCGI_OK)
1490 goto done;
1492 error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1493 if (error)
1494 goto done;
1496 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1497 done:
1498 got_ref_list_free(&header->refs);
1499 gw_free_headers(header);
1500 if (error == NULL && kerr != KCGI_OK)
1501 error = gw_kcgi_error(kerr);
1502 return error;
1505 static const struct got_error *
1506 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1508 const struct got_error *error = NULL;
1509 DIR *dt;
1510 char *dir_test;
1511 int opened = 0;
1513 if (asprintf(&dir_test, "%s/%s/%s",
1514 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1515 GOTWEB_GIT_DIR) == -1)
1516 return got_error_from_errno("asprintf");
1518 dt = opendir(dir_test);
1519 if (dt == NULL) {
1520 free(dir_test);
1521 } else {
1522 error = gw_strdup_string(&gw_dir->path, dir_test, NULL);
1523 opened = 1;
1524 if (error)
1525 goto errored;
1526 goto done;
1529 if (asprintf(&dir_test, "%s/%s/%s",
1530 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1531 GOTWEB_GOT_DIR) == -1)
1532 return got_error_from_errno("asprintf");
1534 dt = opendir(dir_test);
1535 if (dt == NULL)
1536 free(dir_test);
1537 else {
1538 opened = 1;
1539 error = got_error(GOT_ERR_NOT_GIT_REPO);
1540 goto errored;
1543 if (asprintf(&dir_test, "%s/%s",
1544 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1)
1545 return got_error_from_errno("asprintf");
1547 error = gw_strdup_string(&gw_dir->path, dir_test, NULL);
1548 if (error) {
1549 opened = 1;
1550 goto errored;
1552 done:
1553 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1554 gw_dir->path);
1555 if (error)
1556 goto errored;
1557 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1558 if (error)
1559 goto errored;
1560 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1561 "refs/heads", TM_DIFF);
1562 if (error)
1563 goto errored;
1564 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1565 errored:
1566 free(dir_test);
1567 if (opened)
1568 closedir(dt);
1569 return error;
1572 static const struct got_error *
1573 gw_load_got_paths(struct gw_trans *gw_trans)
1575 const struct got_error *error = NULL;
1576 DIR *d;
1577 struct dirent **sd_dent;
1578 struct gw_dir *gw_dir;
1579 struct stat st;
1580 unsigned int d_cnt, d_i;
1582 d = opendir(gw_trans->gw_conf->got_repos_path);
1583 if (d == NULL) {
1584 error = got_error_from_errno2("opendir",
1585 gw_trans->gw_conf->got_repos_path);
1586 return error;
1589 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1590 alphasort);
1591 if (d_cnt == -1) {
1592 error = got_error_from_errno2("scandir",
1593 gw_trans->gw_conf->got_repos_path);
1594 return error;
1597 for (d_i = 0; d_i < d_cnt; d_i++) {
1598 if (gw_trans->gw_conf->got_max_repos > 0 &&
1599 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1600 break; /* account for parent and self */
1602 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1603 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1604 continue;
1606 error = gw_init_gw_dir(&gw_dir, sd_dent[d_i]->d_name);
1607 if (error)
1608 return error;
1610 error = gw_load_got_path(gw_trans, gw_dir);
1611 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1612 error = NULL;
1613 continue;
1615 else if (error)
1616 return error;
1618 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1619 !got_path_dir_is_empty(gw_dir->path)) {
1620 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1621 entry);
1622 gw_trans->repos_total++;
1626 closedir(d);
1627 return error;
1630 static const struct got_error *
1631 gw_parse_querystring(struct gw_trans *gw_trans)
1633 const struct got_error *error = NULL;
1634 struct kpair *p;
1635 struct gw_query_action *action = NULL;
1636 unsigned int i;
1638 if (gw_trans->gw_req->fieldnmap[0]) {
1639 error = got_error_from_errno("bad parse");
1640 return error;
1641 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1642 /* define gw_trans->repo_path */
1643 if (asprintf(&gw_trans->repo_name, "%s", p->parsed.s) == -1)
1644 return got_error_from_errno("asprintf");
1646 if (asprintf(&gw_trans->repo_path, "%s/%s",
1647 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
1648 return got_error_from_errno("asprintf");
1650 /* get action and set function */
1651 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
1652 for (i = 0; i < nitems(gw_query_funcs); i++) {
1653 action = &gw_query_funcs[i];
1654 if (action->func_name == NULL)
1655 continue;
1657 if (strcmp(action->func_name,
1658 p->parsed.s) == 0) {
1659 gw_trans->action = i;
1660 if (asprintf(&gw_trans->action_name,
1661 "%s", action->func_name) == -1)
1662 return
1663 got_error_from_errno(
1664 "asprintf");
1666 break;
1669 action = NULL;
1672 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
1673 if (asprintf(&gw_trans->commit, "%s",
1674 p->parsed.s) == -1)
1675 return got_error_from_errno("asprintf");
1677 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1678 if (asprintf(&gw_trans->repo_file, "%s",
1679 p->parsed.s) == -1)
1680 return got_error_from_errno("asprintf");
1682 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER]))
1683 if (asprintf(&gw_trans->repo_folder, "%s",
1684 p->parsed.s) == -1)
1685 return got_error_from_errno("asprintf");
1687 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
1688 if (asprintf(&gw_trans->headref, "%s",
1689 p->parsed.s) == -1)
1690 return got_error_from_errno("asprintf");
1692 if (action == NULL) {
1693 error = got_error_from_errno("invalid action");
1694 return error;
1696 error = gw_init_gw_dir(&gw_trans->gw_dir, gw_trans->repo_name);
1697 if (error)
1698 return error;
1700 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1701 if (error)
1702 return error;
1703 } else
1704 gw_trans->action = GW_INDEX;
1706 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1707 gw_trans->page = p->parsed.i;
1709 return error;
1712 static const struct got_error *
1713 gw_init_gw_dir(struct gw_dir **gw_dir, char *dir)
1715 const struct got_error *error;
1717 *gw_dir = malloc(sizeof(**gw_dir));
1718 if (*gw_dir == NULL)
1719 return got_error_from_errno("malloc");
1721 if (asprintf(&(*gw_dir)->name, "%s", dir) == -1) {
1722 error = got_error_from_errno("asprintf");
1723 free(*gw_dir);
1724 *gw_dir = NULL;
1725 return NULL;
1728 return NULL;
1731 static const struct got_error *
1732 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1734 enum kcgi_err kerr;
1736 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1737 if (kerr != KCGI_OK)
1738 return gw_kcgi_error(kerr);
1739 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1740 khttps[code]);
1741 if (kerr != KCGI_OK)
1742 return gw_kcgi_error(kerr);
1743 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1744 kmimetypes[mime]);
1745 if (kerr != KCGI_OK)
1746 return gw_kcgi_error(kerr);
1747 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
1748 "nosniff");
1749 if (kerr != KCGI_OK)
1750 return gw_kcgi_error(kerr);
1751 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1752 if (kerr != KCGI_OK)
1753 return gw_kcgi_error(kerr);
1754 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
1755 "1; mode=block");
1756 if (kerr != KCGI_OK)
1757 return gw_kcgi_error(kerr);
1759 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
1760 kerr = khttp_head(gw_trans->gw_req,
1761 kresps[KRESP_CONTENT_DISPOSITION],
1762 "attachment; filename=%s", gw_trans->repo_file);
1763 if (kerr != KCGI_OK)
1764 return gw_kcgi_error(kerr);
1767 kerr = khttp_body(gw_trans->gw_req);
1768 return gw_kcgi_error(kerr);
1771 static const struct got_error *
1772 gw_display_index(struct gw_trans *gw_trans)
1774 const struct got_error *error;
1775 enum kcgi_err kerr;
1777 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1778 if (error)
1779 return error;
1781 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1782 if (kerr != KCGI_OK)
1783 return gw_kcgi_error(kerr);
1785 if (gw_trans->action != GW_BLOB) {
1786 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1787 gw_query_funcs[gw_trans->action].template);
1788 if (kerr != KCGI_OK) {
1789 khtml_close(gw_trans->gw_html_req);
1790 return gw_kcgi_error(kerr);
1794 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
1797 static void
1798 gw_display_error(struct gw_trans *gw_trans, const struct got_error *err)
1800 if (gw_display_open(gw_trans, KHTTP_200, gw_trans->mime) != NULL)
1801 return;
1803 if (khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0) != KCGI_OK)
1804 return;
1805 khtml_puts(gw_trans->gw_html_req, err->msg);
1806 khtml_close(gw_trans->gw_html_req);
1809 static int
1810 gw_template(size_t key, void *arg)
1812 const struct got_error *error = NULL;
1813 enum kcgi_err kerr;
1814 struct gw_trans *gw_trans = arg;
1815 char *img_src = NULL;
1817 switch (key) {
1818 case (TEMPL_HEAD):
1819 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1820 KATTR_NAME, "viewport",
1821 KATTR_CONTENT, "initial-scale=.75, user-scalable=yes",
1822 KATTR__MAX);
1823 if (kerr != KCGI_OK)
1824 return 0;
1825 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1826 if (kerr != KCGI_OK)
1827 return 0;
1828 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1829 KATTR_CHARSET, "utf-8",
1830 KATTR__MAX);
1831 if (kerr != KCGI_OK)
1832 return 0;
1833 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1834 if (kerr != KCGI_OK)
1835 return 0;
1836 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1837 KATTR_NAME, "msapplication-TileColor",
1838 KATTR_CONTENT, "#da532c", KATTR__MAX);
1839 if (kerr != KCGI_OK)
1840 return 0;
1841 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1842 if (kerr != KCGI_OK)
1843 return 0;
1844 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1845 KATTR_NAME, "theme-color",
1846 KATTR_CONTENT, "#ffffff", KATTR__MAX);
1847 if (kerr != KCGI_OK)
1848 return 0;
1849 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1850 if (kerr != KCGI_OK)
1851 return 0;
1852 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1853 KATTR_REL, "apple-touch-icon", KATTR_SIZES, "180x180",
1854 KATTR_HREF, "/apple-touch-icon.png", KATTR__MAX);
1855 if (kerr != KCGI_OK)
1856 return 0;
1857 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1858 if (kerr != KCGI_OK)
1859 return 0;
1860 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1861 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
1862 "32x32", KATTR_HREF, "/favicon-32x32.png", KATTR__MAX);
1863 if (kerr != KCGI_OK)
1864 return 0;
1865 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1866 if (kerr != KCGI_OK)
1867 return 0;
1868 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1869 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
1870 "16x16", KATTR_HREF, "/favicon-16x16.png", KATTR__MAX);
1871 if (kerr != KCGI_OK)
1872 return 0;
1873 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1874 if (kerr != KCGI_OK)
1875 return 0;
1876 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1877 KATTR_REL, "manifest", KATTR_HREF, "/site.webmanifest",
1878 KATTR__MAX);
1879 if (kerr != KCGI_OK)
1880 return 0;
1881 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1882 if (kerr != KCGI_OK)
1883 return 0;
1884 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1885 KATTR_REL, "mask-icon", KATTR_HREF,
1886 "/safari-pinned-tab.svg", KATTR__MAX);
1887 if (kerr != KCGI_OK)
1888 return 0;
1889 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1890 if (kerr != KCGI_OK)
1891 return 0;
1892 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1893 KATTR_REL, "stylesheet", KATTR_TYPE, "text/css",
1894 KATTR_HREF, "/gotweb.css", KATTR__MAX);
1895 if (kerr != KCGI_OK)
1896 return 0;
1897 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1898 if (kerr != KCGI_OK)
1899 return 0;
1900 break;
1901 case(TEMPL_HEADER):
1902 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1903 KATTR_ID, "got_link", KATTR__MAX);
1904 if (kerr != KCGI_OK)
1905 return 0;
1906 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1907 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
1908 KATTR_TARGET, "_sotd", KATTR__MAX);
1909 if (kerr != KCGI_OK)
1910 return 0;
1911 if (asprintf(&img_src, "/%s",
1912 gw_trans->gw_conf->got_logo) == -1)
1913 return 0;
1914 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
1915 KATTR_SRC, img_src, KATTR__MAX);
1916 if (kerr != KCGI_OK) {
1917 free(img_src);
1918 return 0;
1920 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1921 if (kerr != KCGI_OK) {
1922 free(img_src);
1923 return 0;
1925 break;
1926 case (TEMPL_SITEPATH):
1927 error = gw_output_site_link(gw_trans);
1928 if (error)
1929 return 0;
1930 break;
1931 case(TEMPL_TITLE):
1932 if (gw_trans->gw_conf->got_site_name != NULL) {
1933 kerr = khtml_puts(gw_trans->gw_html_req,
1934 gw_trans->gw_conf->got_site_name);
1935 if (kerr != KCGI_OK)
1936 return 0;
1938 break;
1939 case (TEMPL_SEARCH):
1940 break;
1941 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1942 "search", KATTR__MAX);
1943 if (kerr != KCGI_OK)
1944 return 0;
1945 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
1946 KATTR_METHOD, "POST", KATTR__MAX);
1947 if (kerr != KCGI_OK)
1948 return 0;
1949 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
1950 "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
1951 KATTR_MAXLENGTH, "50", KATTR__MAX);
1952 if (kerr != KCGI_OK)
1953 return 0;
1954 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
1955 KATTR__MAX);
1956 if (kerr != KCGI_OK)
1957 return 0;
1958 kerr = khtml_puts(gw_trans->gw_html_req, "Search");
1959 if (kerr != KCGI_OK)
1960 return 0;
1961 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
1962 if (kerr != KCGI_OK)
1963 return 0;
1964 break;
1965 case(TEMPL_SITEOWNER):
1966 if (gw_trans->gw_conf->got_site_owner != NULL &&
1967 gw_trans->gw_conf->got_show_site_owner) {
1968 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1969 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
1970 if (kerr != KCGI_OK)
1971 return 0;
1972 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1973 KATTR_ID, "site_owner", KATTR__MAX);
1974 if (kerr != KCGI_OK)
1975 return 0;
1976 kerr = khtml_puts(gw_trans->gw_html_req,
1977 gw_trans->gw_conf->got_site_owner);
1978 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1979 if (kerr != KCGI_OK)
1980 return 0;
1982 break;
1983 case(TEMPL_CONTENT):
1984 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1985 if (error) {
1986 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1987 KATTR_ID, "tmpl_err", KATTR__MAX);
1988 if (kerr != KCGI_OK)
1989 return 0;
1990 kerr = khttp_puts(gw_trans->gw_req, "Error: ");
1991 if (kerr != KCGI_OK)
1992 return 0;
1993 kerr = khttp_puts(gw_trans->gw_req, error->msg);
1994 if (kerr != KCGI_OK)
1995 return 0;
1996 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1997 if (kerr != KCGI_OK)
1998 return 0;
2000 break;
2001 default:
2002 return 0;
2004 return 1;
2007 static const struct got_error *
2008 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
2010 const struct got_error *error = NULL;
2011 char *ref_str = NULL;
2012 enum kcgi_err kerr = KCGI_OK;
2014 if (strcmp(str2, "") != 0) {
2015 if (asprintf(&ref_str, "(%s)", str2) == -1) {
2016 error = got_error_from_errno("asprintf");
2017 goto done;
2019 } else {
2020 error = gw_strdup_string(&ref_str, "", NULL);
2021 if (error)
2022 goto done;
2025 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2026 KATTR_ID, "header_commit_title", KATTR__MAX);
2027 if (kerr != KCGI_OK)
2028 goto done;
2029 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
2030 if (kerr != KCGI_OK)
2031 goto done;
2032 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2033 if (kerr != KCGI_OK)
2034 goto done;
2035 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2036 KATTR_ID, "header_commit", KATTR__MAX);
2037 if (kerr != KCGI_OK)
2038 goto done;
2039 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2040 if (kerr != KCGI_OK)
2041 goto done;
2042 kerr = khtml_puts(gw_trans->gw_html_req, " ");
2043 if (kerr != KCGI_OK)
2044 goto done;
2045 kerr = khtml_puts(gw_trans->gw_html_req, ref_str);
2046 if (kerr != KCGI_OK)
2047 goto done;
2048 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2049 done:
2050 if (error == NULL && kerr != KCGI_OK)
2051 error = gw_kcgi_error(kerr);
2052 return error;
2055 static const struct got_error *
2056 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2058 const struct got_error *error = NULL;
2059 enum kcgi_err kerr = KCGI_OK;
2061 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2062 KATTR_ID, "header_diff_title", KATTR__MAX);
2063 if (kerr != KCGI_OK)
2064 goto done;
2065 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2066 if (kerr != KCGI_OK)
2067 goto done;
2068 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2069 if (kerr != KCGI_OK)
2070 goto done;
2071 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2072 KATTR_ID, "header_diff", KATTR__MAX);
2073 if (kerr != KCGI_OK)
2074 goto done;
2075 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2076 if (kerr != KCGI_OK)
2077 goto done;
2078 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2079 if (kerr != KCGI_OK)
2080 goto done;
2081 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2082 if (kerr != KCGI_OK)
2083 goto done;
2084 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2085 done:
2086 if (error == NULL && kerr != KCGI_OK)
2087 error = gw_kcgi_error(kerr);
2088 return error;
2091 static const struct got_error *
2092 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2094 const struct got_error *error = NULL;
2095 enum kcgi_err kerr = KCGI_OK;
2097 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2098 KATTR_ID, "header_age_title", KATTR__MAX);
2099 if (kerr != KCGI_OK)
2100 goto done;
2101 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2102 if (kerr != KCGI_OK)
2103 goto done;
2104 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2105 if (kerr != KCGI_OK)
2106 goto done;
2107 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2108 KATTR_ID, "header_age", KATTR__MAX);
2109 if (kerr != KCGI_OK)
2110 goto done;
2111 kerr = khtml_puts(gw_trans->gw_html_req, str);
2112 if (kerr != KCGI_OK)
2113 goto done;
2114 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2115 done:
2116 if (error == NULL && kerr != KCGI_OK)
2117 error = gw_kcgi_error(kerr);
2118 return error;
2121 static const struct got_error *
2122 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2124 const struct got_error *error = NULL;
2125 enum kcgi_err kerr;
2127 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2128 KATTR_ID, "header_author_title", KATTR__MAX);
2129 if (kerr != KCGI_OK)
2130 goto done;
2131 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2132 if (kerr != KCGI_OK)
2133 goto done;
2134 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2135 if (kerr != KCGI_OK)
2136 goto done;
2137 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2138 KATTR_ID, "header_author", KATTR__MAX);
2139 if (kerr != KCGI_OK)
2140 goto done;
2141 kerr = khtml_puts(gw_trans->gw_html_req, str);
2142 if (kerr != KCGI_OK)
2143 goto done;
2144 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2145 done:
2146 if (error == NULL && kerr != KCGI_OK)
2147 error = gw_kcgi_error(kerr);
2148 return error;
2151 static const struct got_error *
2152 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2154 const struct got_error *error = NULL;
2155 enum kcgi_err kerr;
2157 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2158 KATTR_ID, "header_committer_title", KATTR__MAX);
2159 if (kerr != KCGI_OK)
2160 goto done;
2161 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2162 if (kerr != KCGI_OK)
2163 goto done;
2164 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2165 if (kerr != KCGI_OK)
2166 goto done;
2167 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2168 KATTR_ID, "header_committer", KATTR__MAX);
2169 if (kerr != KCGI_OK)
2170 goto done;
2171 kerr = khtml_puts(gw_trans->gw_html_req, str);
2172 if (kerr != KCGI_OK)
2173 goto done;
2174 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2175 done:
2176 if (error == NULL && kerr != KCGI_OK)
2177 error = gw_kcgi_error(kerr);
2178 return error;
2181 static const struct got_error *
2182 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2184 const struct got_error *error = NULL;
2185 enum kcgi_err kerr;
2187 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2188 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2189 if (kerr != KCGI_OK)
2190 goto done;
2191 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2192 if (kerr != KCGI_OK)
2193 goto done;
2194 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2195 if (kerr != KCGI_OK)
2196 goto done;
2197 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2198 KATTR_ID, "header_commit_msg", KATTR__MAX);
2199 if (kerr != KCGI_OK)
2200 goto done;
2201 kerr = khttp_puts(gw_trans->gw_req, str);
2202 if (kerr != KCGI_OK)
2203 goto done;
2204 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2205 done:
2206 if (error == NULL && kerr != KCGI_OK)
2207 error = gw_kcgi_error(kerr);
2208 return error;
2211 static const struct got_error *
2212 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2214 const struct got_error *error = NULL;
2215 enum kcgi_err kerr;
2217 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2218 KATTR_ID, "header_tree_title", KATTR__MAX);
2219 if (kerr != KCGI_OK)
2220 goto done;
2221 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2222 if (kerr != KCGI_OK)
2223 goto done;
2224 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2225 if (kerr != KCGI_OK)
2226 goto done;
2227 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2228 KATTR_ID, "header_tree", KATTR__MAX);
2229 if (kerr != KCGI_OK)
2230 goto done;
2231 kerr = khtml_puts(gw_trans->gw_html_req, str);
2232 if (kerr != KCGI_OK)
2233 goto done;
2234 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2235 done:
2236 if (error == NULL && kerr != KCGI_OK)
2237 error = gw_kcgi_error(kerr);
2238 return error;
2241 static const struct got_error *
2242 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2243 char *dir)
2245 const struct got_error *error = NULL;
2246 FILE *f = NULL;
2247 char *d_file = NULL;
2248 unsigned int len;
2249 size_t n;
2251 *description = NULL;
2252 if (gw_trans->gw_conf->got_show_repo_description == 0)
2253 return NULL;
2255 if (asprintf(&d_file, "%s/description", dir) == -1)
2256 return got_error_from_errno("asprintf");
2258 f = fopen(d_file, "r");
2259 if (f == NULL) {
2260 if (errno == ENOENT || errno == EACCES)
2261 return NULL;
2262 error = got_error_from_errno2("fopen", d_file);
2263 goto done;
2266 if (fseek(f, 0, SEEK_END) == -1) {
2267 error = got_ferror(f, GOT_ERR_IO);
2268 goto done;
2270 len = ftell(f);
2271 if (len == -1) {
2272 error = got_ferror(f, GOT_ERR_IO);
2273 goto done;
2275 if (fseek(f, 0, SEEK_SET) == -1) {
2276 error = got_ferror(f, GOT_ERR_IO);
2277 goto done;
2279 *description = calloc(len + 1, sizeof(**description));
2280 if (*description == NULL) {
2281 error = got_error_from_errno("calloc");
2282 goto done;
2285 n = fread(*description, 1, len, f);
2286 if (n == 0 && ferror(f))
2287 error = got_ferror(f, GOT_ERR_IO);
2288 done:
2289 if (f != NULL && fclose(f) == -1 && error == NULL)
2290 error = got_error_from_errno("fclose");
2291 free(d_file);
2292 return error;
2295 static const struct got_error *
2296 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2298 struct tm tm;
2299 time_t diff_time;
2300 char *years = "years ago", *months = "months ago";
2301 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2302 char *minutes = "minutes ago", *seconds = "seconds ago";
2303 char *now = "right now";
2304 char *s;
2305 char datebuf[29];
2307 *repo_age = NULL;
2309 switch (ref_tm) {
2310 case TM_DIFF:
2311 diff_time = time(NULL) - committer_time;
2312 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2313 if (asprintf(repo_age, "%lld %s",
2314 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2315 return got_error_from_errno("asprintf");
2316 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2317 if (asprintf(repo_age, "%lld %s",
2318 (diff_time / 60 / 60 / 24 / (365 / 12)),
2319 months) == -1)
2320 return got_error_from_errno("asprintf");
2321 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2322 if (asprintf(repo_age, "%lld %s",
2323 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2324 return got_error_from_errno("asprintf");
2325 } else if (diff_time > 60 * 60 * 24 * 2) {
2326 if (asprintf(repo_age, "%lld %s",
2327 (diff_time / 60 / 60 / 24), days) == -1)
2328 return got_error_from_errno("asprintf");
2329 } else if (diff_time > 60 * 60 * 2) {
2330 if (asprintf(repo_age, "%lld %s",
2331 (diff_time / 60 / 60), hours) == -1)
2332 return got_error_from_errno("asprintf");
2333 } else if (diff_time > 60 * 2) {
2334 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2335 minutes) == -1)
2336 return got_error_from_errno("asprintf");
2337 } else if (diff_time > 2) {
2338 if (asprintf(repo_age, "%lld %s", diff_time,
2339 seconds) == -1)
2340 return got_error_from_errno("asprintf");
2341 } else {
2342 if (asprintf(repo_age, "%s", now) == -1)
2343 return got_error_from_errno("asprintf");
2345 break;
2346 case TM_LONG:
2347 if (gmtime_r(&committer_time, &tm) == NULL)
2348 return got_error_from_errno("gmtime_r");
2350 s = asctime_r(&tm, datebuf);
2351 if (s == NULL)
2352 return got_error_from_errno("asctime_r");
2354 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2355 return got_error_from_errno("asprintf");
2356 break;
2358 return NULL;
2361 static const struct got_error *
2362 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2363 char *repo_ref, int ref_tm)
2365 const struct got_error *error = NULL;
2366 struct got_object_id *id = NULL;
2367 struct got_repository *repo = NULL;
2368 struct got_commit_object *commit = NULL;
2369 struct got_reflist_head refs;
2370 struct got_reflist_entry *re;
2371 struct got_reference *head_ref;
2372 int is_head = 0;
2373 time_t committer_time = 0, cmp_time = 0;
2374 char *refname;
2376 *repo_age = NULL;
2377 SIMPLEQ_INIT(&refs);
2379 if (repo_ref == NULL)
2380 return NULL;
2382 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
2383 is_head = 1;
2385 if (gw_trans->gw_conf->got_show_repo_age == 0)
2386 return NULL;
2388 error = got_repo_open(&repo, dir, NULL);
2389 if (error)
2390 goto done;
2392 if (is_head)
2393 error = got_ref_list(&refs, repo, "refs/heads",
2394 got_ref_cmp_by_name, NULL);
2395 else
2396 error = got_ref_list(&refs, repo, repo_ref,
2397 got_ref_cmp_by_name, NULL);
2398 if (error)
2399 goto done;
2401 SIMPLEQ_FOREACH(re, &refs, entry) {
2402 if (is_head) {
2403 error = gw_strdup_string(&refname, repo_ref, NULL);
2404 if (error)
2405 goto done;
2406 } else {
2407 error = gw_strdup_string(&refname, NULL,
2408 got_ref_get_name(re->ref));
2409 if (error)
2410 goto done;
2412 error = got_ref_open(&head_ref, repo, refname, 0);
2413 if (error)
2414 goto done;
2416 error = got_ref_resolve(&id, repo, head_ref);
2417 got_ref_close(head_ref);
2418 if (error)
2419 goto done;
2421 error = got_object_open_as_commit(&commit, repo, id);
2422 if (error)
2423 goto done;
2425 committer_time =
2426 got_object_commit_get_committer_time(commit);
2428 if (cmp_time < committer_time)
2429 cmp_time = committer_time;
2432 if (cmp_time != 0) {
2433 committer_time = cmp_time;
2434 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2436 done:
2437 got_ref_list_free(&refs);
2438 free(id);
2439 return error;
2442 static const struct got_error *
2443 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2445 const struct got_error *error;
2446 FILE *f = NULL;
2447 struct got_object_id *id1 = NULL, *id2 = NULL;
2448 char *label1 = NULL, *label2 = NULL, *line = NULL;
2449 int obj_type;
2450 size_t linesize = 0;
2451 ssize_t linelen;
2452 enum kcgi_err kerr = KCGI_OK;
2454 f = got_opentemp();
2455 if (f == NULL)
2456 return NULL;
2458 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
2459 if (error)
2460 goto done;
2462 if (strncmp(header->parent_id, "/dev/null", 9) != 0) {
2463 error = got_repo_match_object_id(&id1, &label1,
2464 header->parent_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2465 if (error)
2466 goto done;
2469 error = got_repo_match_object_id(&id2, &label2,
2470 header->commit_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2471 if (error)
2472 goto done;
2474 error = got_object_get_type(&obj_type, header->repo, id2);
2475 if (error)
2476 goto done;
2477 switch (obj_type) {
2478 case GOT_OBJ_TYPE_BLOB:
2479 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
2480 header->repo, f);
2481 break;
2482 case GOT_OBJ_TYPE_TREE:
2483 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
2484 header->repo, f);
2485 break;
2486 case GOT_OBJ_TYPE_COMMIT:
2487 error = got_diff_objects_as_commits(id1, id2, 3, 0,
2488 header->repo, f);
2489 break;
2490 default:
2491 error = got_error(GOT_ERR_OBJ_TYPE);
2493 if (error)
2494 goto done;
2496 if (fseek(f, 0, SEEK_SET) == -1) {
2497 error = got_ferror(f, GOT_ERR_IO);
2498 goto done;
2501 while ((linelen = getline(&line, &linesize, f)) != -1) {
2502 error = gw_colordiff_line(gw_trans, line);
2503 if (error)
2504 goto done;
2505 /* XXX: KHTML_PRETTY breaks this */
2506 kerr = khtml_puts(gw_trans->gw_html_req, line);
2507 if (kerr != KCGI_OK)
2508 goto done;
2509 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2510 if (kerr != KCGI_OK)
2511 goto done;
2513 if (linelen == -1 && ferror(f))
2514 error = got_error_from_errno("getline");
2515 done:
2516 if (f && fclose(f) == -1 && error == NULL)
2517 error = got_error_from_errno("fclose");
2518 free(line);
2519 free(label1);
2520 free(label2);
2521 free(id1);
2522 free(id2);
2524 if (error == NULL && kerr != KCGI_OK)
2525 error = gw_kcgi_error(kerr);
2526 return error;
2529 static const struct got_error *
2530 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2532 const struct got_error *error = NULL;
2533 struct got_repository *repo;
2534 const char *gitconfig_owner;
2536 *owner = NULL;
2538 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2539 return NULL;
2541 error = got_repo_open(&repo, dir, NULL);
2542 if (error)
2543 return error;
2544 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2545 if (gitconfig_owner)
2546 error = gw_strdup_string(owner, NULL, gitconfig_owner);
2547 got_repo_close(repo);
2548 return error;
2551 static const struct got_error *
2552 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2554 const struct got_error *error = NULL;
2555 FILE *f;
2556 char *d_file = NULL;
2557 unsigned int len;
2558 size_t n;
2560 *url = NULL;
2562 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2563 return got_error_from_errno("asprintf");
2565 f = fopen(d_file, "r");
2566 if (f == NULL) {
2567 if (errno != ENOENT && errno != EACCES)
2568 error = got_error_from_errno2("fopen", d_file);
2569 goto done;
2572 if (fseek(f, 0, SEEK_END) == -1) {
2573 error = got_ferror(f, GOT_ERR_IO);
2574 goto done;
2576 len = ftell(f);
2577 if (len == -1) {
2578 error = got_ferror(f, GOT_ERR_IO);
2579 goto done;
2581 if (fseek(f, 0, SEEK_SET) == -1) {
2582 error = got_ferror(f, GOT_ERR_IO);
2583 goto done;
2586 *url = calloc(len + 1, sizeof(**url));
2587 if (*url == NULL) {
2588 error = got_error_from_errno("calloc");
2589 goto done;
2592 n = fread(*url, 1, len, f);
2593 if (n == 0 && ferror(f))
2594 error = got_ferror(f, GOT_ERR_IO);
2595 done:
2596 if (f && fclose(f) == -1 && error == NULL)
2597 error = got_error_from_errno("fclose");
2598 free(d_file);
2599 return NULL;
2602 static const struct got_error *
2603 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
2604 int limit, int tag_type)
2606 const struct got_error *error = NULL;
2607 struct got_repository *repo = NULL;
2608 struct got_reflist_head refs;
2609 struct got_reflist_entry *re;
2610 char *age = NULL;
2611 char *id_str = NULL, *newline, *href_commits = NULL;
2612 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
2613 struct got_tag_object *tag = NULL;
2614 enum kcgi_err kerr = KCGI_OK;
2615 int summary_header_displayed = 0;
2617 SIMPLEQ_INIT(&refs);
2619 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2620 if (error)
2621 return error;
2623 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
2624 if (error)
2625 goto done;
2627 SIMPLEQ_FOREACH(re, &refs, entry) {
2628 const char *refname;
2629 const char *tagger;
2630 const char *tag_commit;
2631 time_t tagger_time;
2632 struct got_object_id *id;
2633 struct got_commit_object *commit = NULL;
2635 refname = got_ref_get_name(re->ref);
2636 if (strncmp(refname, "refs/tags/", 10) != 0)
2637 continue;
2638 refname += 10;
2640 error = got_ref_resolve(&id, repo, re->ref);
2641 if (error)
2642 goto done;
2644 error = got_object_open_as_tag(&tag, repo, id);
2645 if (error) {
2646 if (error->code != GOT_ERR_OBJ_TYPE) {
2647 free(id);
2648 goto done;
2650 /* "lightweight" tag */
2651 error = got_object_open_as_commit(&commit, repo, id);
2652 if (error) {
2653 free(id);
2654 goto done;
2656 tagger = got_object_commit_get_committer(commit);
2657 tagger_time =
2658 got_object_commit_get_committer_time(commit);
2659 error = got_object_id_str(&id_str, id);
2660 free(id);
2661 } else {
2662 free(id);
2663 tagger = got_object_tag_get_tagger(tag);
2664 tagger_time = got_object_tag_get_tagger_time(tag);
2665 error = got_object_id_str(&id_str,
2666 got_object_tag_get_object_id(tag));
2668 if (error)
2669 goto done;
2671 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
2672 strlen(id_str)) != 0)
2673 continue;
2675 if (commit) {
2676 error = got_object_commit_get_logmsg(&tag_commit0,
2677 commit);
2678 if (error)
2679 goto done;
2680 got_object_commit_close(commit);
2681 } else {
2682 tag_commit0 = strdup(got_object_tag_get_message(tag));
2683 if (tag_commit0 == NULL) {
2684 error = got_error_from_errno("strdup");
2685 goto done;
2689 tag_commit = tag_commit0;
2690 while (*tag_commit == '\n')
2691 tag_commit++;
2693 switch (tag_type) {
2694 case TAGBRIEF:
2695 newline = strchr(tag_commit, '\n');
2696 if (newline)
2697 *newline = '\0';
2699 if (summary_header_displayed == 0) {
2700 kerr = khtml_attr(gw_trans->gw_html_req,
2701 KELEM_DIV, KATTR_ID,
2702 "summary_tags_title_wrapper", KATTR__MAX);
2703 if (kerr != KCGI_OK)
2704 goto done;
2705 kerr = khtml_attr(gw_trans->gw_html_req,
2706 KELEM_DIV, KATTR_ID,
2707 "summary_tags_title", KATTR__MAX);
2708 if (kerr != KCGI_OK)
2709 goto done;
2710 kerr = khtml_puts(gw_trans->gw_html_req,
2711 "Tags");
2712 if (kerr != KCGI_OK)
2713 goto done;
2714 kerr = khtml_closeelem(gw_trans->gw_html_req,
2715 2);
2716 if (kerr != KCGI_OK)
2717 goto done;
2718 kerr = khtml_attr(gw_trans->gw_html_req,
2719 KELEM_DIV, KATTR_ID,
2720 "summary_tags_content", KATTR__MAX);
2721 if (kerr != KCGI_OK)
2722 goto done;
2723 summary_header_displayed = 1;
2726 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2727 KATTR_ID, "tags_wrapper", KATTR__MAX);
2728 if (kerr != KCGI_OK)
2729 goto done;
2730 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2731 KATTR_ID, "tags_age", KATTR__MAX);
2732 if (kerr != KCGI_OK)
2733 goto done;
2734 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
2735 if (error)
2736 goto done;
2737 kerr = khtml_puts(gw_trans->gw_html_req,
2738 age ? age : "");
2739 if (kerr != KCGI_OK)
2740 goto done;
2741 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2742 if (kerr != KCGI_OK)
2743 goto done;
2744 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2745 KATTR_ID, "tags", KATTR__MAX);
2746 if (kerr != KCGI_OK)
2747 goto done;
2748 kerr = khtml_puts(gw_trans->gw_html_req, refname);
2749 if (kerr != KCGI_OK)
2750 goto done;
2751 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2752 if (kerr != KCGI_OK)
2753 goto done;
2754 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2755 KATTR_ID, "tags_name", KATTR__MAX);
2756 if (kerr != KCGI_OK)
2757 goto done;
2758 if (asprintf(&href_tag, "?path=%s&action=tag&commit=%s",
2759 gw_trans->repo_name, id_str) == -1) {
2760 error = got_error_from_errno("asprintf");
2761 goto done;
2763 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2764 KATTR_HREF, href_tag, KATTR__MAX);
2765 if (kerr != KCGI_OK)
2766 goto done;
2767 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
2768 if (kerr != KCGI_OK)
2769 goto done;
2770 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2771 if (kerr != KCGI_OK)
2772 goto done;
2774 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2775 KATTR_ID, "navs_wrapper", KATTR__MAX);
2776 if (kerr != KCGI_OK)
2777 goto done;
2778 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2779 KATTR_ID, "navs", KATTR__MAX);
2780 if (kerr != KCGI_OK)
2781 goto done;
2783 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2784 KATTR_HREF, href_tag, KATTR__MAX);
2785 if (kerr != KCGI_OK)
2786 goto done;
2787 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
2788 if (kerr != KCGI_OK)
2789 goto done;
2790 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2791 if (kerr != KCGI_OK)
2792 goto done;
2794 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2795 if (kerr != KCGI_OK)
2796 goto done;
2797 if (asprintf(&href_briefs,
2798 "?path=%s&action=briefs&commit=%s",
2799 gw_trans->repo_name, id_str) == -1) {
2800 error = got_error_from_errno("asprintf");
2801 goto done;
2803 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2804 KATTR_HREF, href_briefs, KATTR__MAX);
2805 if (kerr != KCGI_OK)
2806 goto done;
2807 kerr = khtml_puts(gw_trans->gw_html_req,
2808 "commit briefs");
2809 if (kerr != KCGI_OK)
2810 goto done;
2811 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2812 if (kerr != KCGI_OK)
2813 goto done;
2815 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2816 if (kerr != KCGI_OK)
2817 goto done;
2819 if (asprintf(&href_commits,
2820 "?path=%s&action=commits&commit=%s",
2821 gw_trans->repo_name, id_str) == -1) {
2822 error = got_error_from_errno("asprintf");
2823 goto done;
2825 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2826 KATTR_HREF, href_commits, KATTR__MAX);
2827 if (kerr != KCGI_OK)
2828 goto done;
2829 kerr = khtml_puts(gw_trans->gw_html_req,
2830 "commits");
2831 if (kerr != KCGI_OK)
2832 goto done;
2833 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2834 if (kerr != KCGI_OK)
2835 goto done;
2837 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2838 KATTR_ID, "dotted_line", KATTR__MAX);
2839 if (kerr != KCGI_OK)
2840 goto done;
2841 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2842 if (kerr != KCGI_OK)
2843 goto done;
2844 break;
2845 case TAGFULL:
2846 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2847 KATTR_ID, "tag_info_date_title", KATTR__MAX);
2848 if (kerr != KCGI_OK)
2849 goto done;
2850 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
2851 if (kerr != KCGI_OK)
2852 goto done;
2853 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2854 if (kerr != KCGI_OK)
2855 goto done;
2856 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2857 KATTR_ID, "tag_info_date", KATTR__MAX);
2858 if (kerr != KCGI_OK)
2859 goto done;
2860 error = gw_get_time_str(&age, tagger_time, TM_LONG);
2861 if (error)
2862 goto done;
2863 kerr = khtml_puts(gw_trans->gw_html_req,
2864 age ? age : "");
2865 if (kerr != KCGI_OK)
2866 goto done;
2867 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2868 if (kerr != KCGI_OK)
2869 goto done;
2871 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2872 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
2873 if (kerr != KCGI_OK)
2874 goto done;
2875 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
2876 if (kerr != KCGI_OK)
2877 goto done;
2878 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2879 if (kerr != KCGI_OK)
2880 goto done;
2881 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2882 KATTR_ID, "tag_info_date", KATTR__MAX);
2883 if (kerr != KCGI_OK)
2884 goto done;
2885 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
2886 if (kerr != KCGI_OK)
2887 goto done;
2888 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2889 if (kerr != KCGI_OK)
2890 goto done;
2892 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2893 KATTR_ID, "tag_info", KATTR__MAX);
2894 if (kerr != KCGI_OK)
2895 goto done;
2896 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
2897 if (kerr != KCGI_OK)
2898 goto done;
2899 break;
2900 default:
2901 break;
2903 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2904 if (kerr != KCGI_OK)
2905 goto done;
2907 if (limit && --limit == 0)
2908 break;
2910 if (tag)
2911 got_object_tag_close(tag);
2912 tag = NULL;
2913 free(id_str);
2914 id_str = NULL;
2915 free(age);
2916 age = NULL;
2917 free(tag_commit0);
2918 tag_commit0 = NULL;
2919 free(href_tag);
2920 href_tag = NULL;
2921 free(href_briefs);
2922 href_briefs = NULL;
2923 free(href_commits);
2924 href_commits = NULL;
2926 done:
2927 if (tag)
2928 got_object_tag_close(tag);
2929 free(id_str);
2930 free(age);
2931 free(tag_commit0);
2932 free(href_tag);
2933 free(href_briefs);
2934 free(href_commits);
2935 got_ref_list_free(&refs);
2936 if (repo)
2937 got_repo_close(repo);
2938 if (error == NULL && kerr != KCGI_OK)
2939 error = gw_kcgi_error(kerr);
2940 return error;
2943 static void
2944 gw_free_headers(struct gw_header *header)
2946 free(header->id);
2947 free(header->path);
2948 if (header->commit != NULL)
2949 got_object_commit_close(header->commit);
2950 if (header->repo)
2951 got_repo_close(header->repo);
2952 free(header->refs_str);
2953 free(header->commit_id);
2954 free(header->author);
2955 free(header->committer);
2956 free(header->parent_id);
2957 free(header->tree_id);
2958 free(header->commit_msg);
2961 static struct gw_header *
2962 gw_init_header()
2964 struct gw_header *header;
2966 header = malloc(sizeof(*header));
2967 if (header == NULL)
2968 return NULL;
2970 header->repo = NULL;
2971 header->commit = NULL;
2972 header->id = NULL;
2973 header->path = NULL;
2974 SIMPLEQ_INIT(&header->refs);
2976 return header;
2979 static const struct got_error *
2980 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
2981 int limit)
2983 const struct got_error *error = NULL;
2984 struct got_commit_graph *graph = NULL;
2986 error = got_commit_graph_open(&graph, header->path, 0);
2987 if (error)
2988 return error;
2990 error = got_commit_graph_iter_start(graph, header->id, header->repo,
2991 NULL, NULL);
2992 if (error)
2993 goto done;
2995 for (;;) {
2996 error = got_commit_graph_iter_next(&header->id, graph,
2997 header->repo, NULL, NULL);
2998 if (error) {
2999 if (error->code == GOT_ERR_ITER_COMPLETED)
3000 error = NULL;
3001 goto done;
3003 if (header->id == NULL)
3004 goto done;
3006 error = got_object_open_as_commit(&header->commit, header->repo,
3007 header->id);
3008 if (error)
3009 goto done;
3011 error = gw_get_commit(gw_trans, header);
3012 if (limit > 1) {
3013 struct gw_header *n_header = NULL;
3014 if ((n_header = gw_init_header()) == NULL) {
3015 error = got_error_from_errno("malloc");
3016 goto done;
3019 error = gw_strdup_string(&n_header->refs_str,
3020 header->refs_str, NULL);
3021 if (error)
3022 goto done;
3023 error = gw_strdup_string(&n_header->commit_id,
3024 header->commit_id, NULL);
3025 if (error)
3026 goto done;
3027 error = gw_strdup_string(&n_header->parent_id,
3028 header->parent_id, NULL);
3029 if (error)
3030 goto done;
3031 error = gw_strdup_string(&n_header->tree_id,
3032 header->tree_id, NULL);
3033 if (error)
3034 goto done;
3035 error = gw_strdup_string(&n_header->author,
3036 header->author, NULL);
3037 if (error)
3038 goto done;
3039 error = gw_strdup_string(&n_header->committer,
3040 header->committer, NULL);
3041 if (error)
3042 goto done;
3043 error = gw_strdup_string(&n_header->commit_msg,
3044 header->commit_msg, NULL);
3045 if (error)
3046 goto done;
3047 n_header->committer_time = header->committer_time;
3048 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3049 entry);
3051 if (error || (limit && --limit == 0))
3052 break;
3054 done:
3055 if (graph)
3056 got_commit_graph_close(graph);
3057 return error;
3060 static const struct got_error *
3061 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
3063 const struct got_error *error = NULL;
3064 struct got_reflist_entry *re;
3065 struct got_object_id *id2 = NULL;
3066 struct got_object_qid *parent_id;
3067 char *refs_str = NULL, *commit_msg = NULL, *commit_msg0;
3069 /*print commit*/
3070 SIMPLEQ_FOREACH(re, &header->refs, entry) {
3071 char *s;
3072 const char *name;
3073 struct got_tag_object *tag = NULL;
3074 int cmp;
3076 name = got_ref_get_name(re->ref);
3077 if (strcmp(name, GOT_REF_HEAD) == 0)
3078 continue;
3079 if (strncmp(name, "refs/", 5) == 0)
3080 name += 5;
3081 if (strncmp(name, "got/", 4) == 0)
3082 continue;
3083 if (strncmp(name, "heads/", 6) == 0)
3084 name += 6;
3085 if (strncmp(name, "remotes/", 8) == 0)
3086 name += 8;
3087 if (strncmp(name, "tags/", 5) == 0) {
3088 error = got_object_open_as_tag(&tag, header->repo,
3089 re->id);
3090 if (error) {
3091 if (error->code != GOT_ERR_OBJ_TYPE)
3092 continue;
3094 * Ref points at something other
3095 * than a tag.
3097 error = NULL;
3098 tag = NULL;
3101 cmp = got_object_id_cmp(tag ?
3102 got_object_tag_get_object_id(tag) : re->id, header->id);
3103 if (tag)
3104 got_object_tag_close(tag);
3105 if (cmp != 0)
3106 continue;
3107 s = refs_str;
3108 if (asprintf(&refs_str, "%s%s%s", s ? s : "",
3109 s ? ", " : "", name) == -1) {
3110 error = got_error_from_errno("asprintf");
3111 free(s);
3112 return error;
3114 error = gw_strdup_string(&header->refs_str, refs_str, NULL);
3115 free(s);
3116 if (error)
3117 return error;
3120 if (refs_str == NULL) {
3121 error = gw_strdup_string(&header->refs_str, "", NULL);
3122 if (error)
3123 return error;
3125 free(refs_str);
3127 error = got_object_id_str(&header->commit_id, header->id);
3128 if (error)
3129 return error;
3131 error = got_object_id_str(&header->tree_id,
3132 got_object_commit_get_tree_id(header->commit));
3133 if (error)
3134 return error;
3136 if (gw_trans->action == GW_DIFF) {
3137 parent_id = SIMPLEQ_FIRST(
3138 got_object_commit_get_parent_ids(header->commit));
3139 if (parent_id != NULL) {
3140 id2 = got_object_id_dup(parent_id->id);
3141 free (parent_id);
3142 error = got_object_id_str(&header->parent_id, id2);
3143 if (error)
3144 return error;
3145 free(id2);
3146 } else {
3147 error = gw_strdup_string(&header->parent_id,
3148 "/dev/null", NULL);
3149 if (error)
3150 return error;
3152 } else {
3153 error = gw_strdup_string(&header->parent_id, "", NULL);
3154 if (error)
3155 return error;
3158 header->committer_time =
3159 got_object_commit_get_committer_time(header->commit);
3161 error = gw_strdup_string(&header->author, NULL,
3162 got_object_commit_get_author(header->commit));
3163 if (error)
3164 return error;
3165 error = gw_strdup_string(&header->committer, NULL,
3166 got_object_commit_get_committer(header->commit));
3167 if (error)
3168 return error;
3169 error = got_object_commit_get_logmsg(&commit_msg0, header->commit);
3170 if (error)
3171 return error;
3173 commit_msg = commit_msg0;
3174 while (*commit_msg == '\n')
3175 commit_msg++;
3177 error = gw_strdup_string(&header->commit_msg, commit_msg, NULL);
3178 free(commit_msg0);
3179 return error;
3182 static const struct got_error *
3183 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3185 const struct got_error *error = NULL;
3186 char *in_repo_path = NULL;
3188 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
3189 if (error)
3190 return error;
3192 if (gw_trans->commit == NULL) {
3193 struct got_reference *head_ref;
3194 error = got_ref_open(&head_ref, header->repo,
3195 gw_trans->headref, 0);
3196 if (error)
3197 return error;
3199 error = got_ref_resolve(&header->id, header->repo, head_ref);
3200 got_ref_close(head_ref);
3201 if (error)
3202 return error;
3204 error = got_object_open_as_commit(&header->commit,
3205 header->repo, header->id);
3206 } else {
3207 struct got_reference *ref;
3208 error = got_ref_open(&ref, header->repo, gw_trans->commit, 0);
3209 if (error == NULL) {
3210 int obj_type;
3211 error = got_ref_resolve(&header->id, header->repo, ref);
3212 got_ref_close(ref);
3213 if (error)
3214 return error;
3215 error = got_object_get_type(&obj_type, header->repo,
3216 header->id);
3217 if (error)
3218 return error;
3219 if (obj_type == GOT_OBJ_TYPE_TAG) {
3220 struct got_tag_object *tag;
3221 error = got_object_open_as_tag(&tag,
3222 header->repo, header->id);
3223 if (error)
3224 return error;
3225 if (got_object_tag_get_object_type(tag) !=
3226 GOT_OBJ_TYPE_COMMIT) {
3227 got_object_tag_close(tag);
3228 error = got_error(GOT_ERR_OBJ_TYPE);
3229 return error;
3231 free(header->id);
3232 header->id = got_object_id_dup(
3233 got_object_tag_get_object_id(tag));
3234 if (header->id == NULL)
3235 error = got_error_from_errno(
3236 "got_object_id_dup");
3237 got_object_tag_close(tag);
3238 if (error)
3239 return error;
3240 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3241 error = got_error(GOT_ERR_OBJ_TYPE);
3242 return error;
3244 error = got_object_open_as_commit(&header->commit,
3245 header->repo, header->id);
3246 if (error)
3247 return error;
3249 if (header->commit == NULL) {
3250 error = got_repo_match_object_id_prefix(&header->id,
3251 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
3252 header->repo);
3253 if (error)
3254 return error;
3256 error = got_repo_match_object_id_prefix(&header->id,
3257 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
3258 header->repo);
3261 error = got_repo_map_path(&in_repo_path, header->repo,
3262 gw_trans->repo_path, 1);
3263 if (error)
3264 return error;
3266 if (in_repo_path) {
3267 error = gw_strdup_string(&header->path, in_repo_path, NULL);
3268 if (error) {
3269 free(in_repo_path);
3270 return error;
3273 free(in_repo_path);
3275 error = got_ref_list(&header->refs, header->repo, NULL,
3276 got_ref_cmp_by_name, NULL);
3277 if (error)
3278 return error;
3280 error = gw_get_commits(gw_trans, header, limit);
3281 return error;
3284 struct blame_line {
3285 int annotated;
3286 char *id_str;
3287 char *committer;
3288 char datebuf[11]; /* YYYY-MM-DD + NUL */
3291 struct gw_blame_cb_args {
3292 struct blame_line *lines;
3293 int nlines;
3294 int nlines_prec;
3295 int lineno_cur;
3296 off_t *line_offsets;
3297 FILE *f;
3298 struct got_repository *repo;
3299 struct gw_trans *gw_trans;
3302 static const struct got_error *
3303 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3305 const struct got_error *err = NULL;
3306 struct gw_blame_cb_args *a = arg;
3307 struct blame_line *bline;
3308 char *line = NULL;
3309 size_t linesize = 0;
3310 struct got_commit_object *commit = NULL;
3311 off_t offset;
3312 struct tm tm;
3313 time_t committer_time;
3314 enum kcgi_err kerr = KCGI_OK;
3316 if (nlines != a->nlines ||
3317 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3318 return got_error(GOT_ERR_RANGE);
3320 if (lineno == -1)
3321 return NULL; /* no change in this commit */
3323 /* Annotate this line. */
3324 bline = &a->lines[lineno - 1];
3325 if (bline->annotated)
3326 return NULL;
3327 err = got_object_id_str(&bline->id_str, id);
3328 if (err)
3329 return err;
3331 err = got_object_open_as_commit(&commit, a->repo, id);
3332 if (err)
3333 goto done;
3335 err = gw_strdup_string(&bline->committer, NULL,
3336 got_object_commit_get_committer(commit));
3337 if (err)
3338 goto done;
3340 committer_time = got_object_commit_get_committer_time(commit);
3341 if (localtime_r(&committer_time, &tm) == NULL)
3342 return got_error_from_errno("localtime_r");
3343 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3344 &tm) >= sizeof(bline->datebuf)) {
3345 err = got_error(GOT_ERR_NO_SPACE);
3346 goto done;
3348 bline->annotated = 1;
3350 /* Print lines annotated so far. */
3351 bline = &a->lines[a->lineno_cur - 1];
3352 if (!bline->annotated)
3353 goto done;
3355 offset = a->line_offsets[a->lineno_cur - 1];
3356 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3357 err = got_error_from_errno("fseeko");
3358 goto done;
3361 while (bline->annotated) {
3362 char *smallerthan, *at, *nl, *committer;
3363 char *lineno = NULL, *href_diff = NULL, *href_link = NULL;
3364 size_t len;
3366 if (getline(&line, &linesize, a->f) == -1) {
3367 if (ferror(a->f))
3368 err = got_error_from_errno("getline");
3369 break;
3372 committer = bline->committer;
3373 smallerthan = strchr(committer, '<');
3374 if (smallerthan && smallerthan[1] != '\0')
3375 committer = smallerthan + 1;
3376 at = strchr(committer, '@');
3377 if (at)
3378 *at = '\0';
3379 len = strlen(committer);
3380 if (len >= 9)
3381 committer[8] = '\0';
3383 nl = strchr(line, '\n');
3384 if (nl)
3385 *nl = '\0';
3387 if (a->gw_trans->repo_folder == NULL) {
3388 err = gw_strdup_string(&a->gw_trans->repo_folder, "",
3389 NULL);
3390 if (err)
3391 goto err;
3393 if (a->gw_trans->repo_folder == NULL)
3394 goto err;
3396 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3397 "blame_wrapper", KATTR__MAX);
3398 if (kerr != KCGI_OK)
3399 goto err;
3400 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3401 "blame_number", KATTR__MAX);
3402 if (kerr != KCGI_OK)
3403 goto err;
3404 if (asprintf(&lineno, "%.*d", a->nlines_prec,
3405 a->lineno_cur) == -1)
3406 goto err;
3407 kerr = khtml_puts(a->gw_trans->gw_html_req, lineno);
3408 if (kerr != KCGI_OK)
3409 goto err;
3410 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3411 if (kerr != KCGI_OK)
3412 goto err;
3414 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3415 "blame_hash", KATTR__MAX);
3416 if (kerr != KCGI_OK)
3417 goto err;
3418 if (asprintf(&href_diff,
3419 "?path=%s&action=diff&commit=%s&file=%s&folder=%s",
3420 a->gw_trans->repo_name, bline->id_str,
3421 a->gw_trans->repo_file, a->gw_trans->repo_folder) == -1) {
3422 err = got_error_from_errno("asprintf");
3423 goto err;
3425 if (asprintf(&href_link, "%.8s", bline->id_str) == -1) {
3426 err = got_error_from_errno("asprintf");
3427 goto err;
3429 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3430 KATTR_HREF, href_diff, KATTR__MAX);
3431 if (kerr != KCGI_OK)
3432 goto done;
3433 kerr = khtml_puts(a->gw_trans->gw_html_req, href_link);
3434 if (kerr != KCGI_OK)
3435 goto err;
3436 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3437 if (kerr != KCGI_OK)
3438 goto err;
3440 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3441 "blame_date", KATTR__MAX);
3442 if (kerr != KCGI_OK)
3443 goto err;
3444 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3445 if (kerr != KCGI_OK)
3446 goto err;
3447 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3448 if (kerr != KCGI_OK)
3449 goto err;
3451 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3452 "blame_author", KATTR__MAX);
3453 if (kerr != KCGI_OK)
3454 goto err;
3455 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
3456 if (kerr != KCGI_OK)
3457 goto err;
3458 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3459 if (kerr != KCGI_OK)
3460 goto err;
3462 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3463 "blame_code", KATTR__MAX);
3464 if (kerr != KCGI_OK)
3465 goto err;
3466 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
3467 if (kerr != KCGI_OK)
3468 goto err;
3469 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3470 if (kerr != KCGI_OK)
3471 goto err;
3473 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3474 if (kerr != KCGI_OK)
3475 goto err;
3477 a->lineno_cur++;
3478 bline = &a->lines[a->lineno_cur - 1];
3479 err:
3480 free(lineno);
3481 free(href_diff);
3482 free(href_link);
3484 done:
3485 if (commit)
3486 got_object_commit_close(commit);
3487 free(line);
3488 if (err == NULL && kerr != KCGI_OK)
3489 err = gw_kcgi_error(kerr);
3490 return err;
3493 static const struct got_error *
3494 gw_output_file_blame(struct gw_trans *gw_trans)
3496 const struct got_error *error = NULL;
3497 struct got_repository *repo = NULL;
3498 struct got_object_id *obj_id = NULL;
3499 struct got_object_id *commit_id = NULL;
3500 struct got_blob_object *blob = NULL;
3501 char *path = NULL, *in_repo_path = NULL;
3502 struct gw_blame_cb_args bca;
3503 int i, obj_type;
3504 size_t filesize;
3506 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3507 if (error)
3508 return error;
3510 if (asprintf(&path, "%s%s%s",
3511 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3512 gw_trans->repo_folder ? "/" : "",
3513 gw_trans->repo_file) == -1) {
3514 error = got_error_from_errno("asprintf");
3515 goto done;
3518 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3519 if (error)
3520 goto done;
3522 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3523 GOT_OBJ_TYPE_COMMIT, 1, repo);
3524 if (error)
3525 goto done;
3527 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3528 if (error)
3529 goto done;
3531 if (obj_id == NULL) {
3532 error = got_error(GOT_ERR_NO_OBJ);
3533 goto done;
3536 error = got_object_get_type(&obj_type, repo, obj_id);
3537 if (error)
3538 goto done;
3540 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3541 error = got_error(GOT_ERR_OBJ_TYPE);
3542 goto done;
3545 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3546 if (error)
3547 goto done;
3549 bca.f = got_opentemp();
3550 if (bca.f == NULL) {
3551 error = got_error_from_errno("got_opentemp");
3552 goto done;
3554 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3555 &bca.line_offsets, bca.f, blob);
3556 if (error || bca.nlines == 0)
3557 goto done;
3559 /* Don't include \n at EOF in the blame line count. */
3560 if (bca.line_offsets[bca.nlines - 1] == filesize)
3561 bca.nlines--;
3563 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3564 if (bca.lines == NULL) {
3565 error = got_error_from_errno("calloc");
3566 goto done;
3568 bca.lineno_cur = 1;
3569 bca.nlines_prec = 0;
3570 i = bca.nlines;
3571 while (i > 0) {
3572 i /= 10;
3573 bca.nlines_prec++;
3575 bca.repo = repo;
3576 bca.gw_trans = gw_trans;
3578 error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
3579 NULL, NULL);
3580 done:
3581 free(bca.line_offsets);
3582 free(in_repo_path);
3583 free(commit_id);
3584 free(obj_id);
3585 free(path);
3587 for (i = 0; i < bca.nlines; i++) {
3588 struct blame_line *bline = &bca.lines[i];
3589 free(bline->id_str);
3590 free(bline->committer);
3592 free(bca.lines);
3593 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3594 error = got_error_from_errno("fclose");
3595 if (blob)
3596 got_object_blob_close(blob);
3597 if (repo)
3598 got_repo_close(repo);
3599 return error;
3602 static const struct got_error *
3603 gw_output_blob_buf(struct gw_trans *gw_trans)
3605 const struct got_error *error = NULL;
3606 struct got_repository *repo = NULL;
3607 struct got_object_id *obj_id = NULL;
3608 struct got_object_id *commit_id = NULL;
3609 struct got_blob_object *blob = NULL;
3610 char *path = NULL, *in_repo_path = NULL;
3611 int obj_type, set_mime = 0;
3612 size_t len, hdrlen;
3613 const uint8_t *buf;
3614 enum kcgi_err kerr = KCGI_OK;
3616 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3617 if (error)
3618 return error;
3620 if (asprintf(&path, "%s%s%s",
3621 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3622 gw_trans->repo_folder ? "/" : "",
3623 gw_trans->repo_file) == -1) {
3624 error = got_error_from_errno("asprintf");
3625 goto done;
3628 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3629 if (error)
3630 goto done;
3632 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3633 GOT_OBJ_TYPE_COMMIT, 1, repo);
3634 if (error)
3635 goto done;
3637 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3638 if (error)
3639 goto done;
3641 if (obj_id == NULL) {
3642 error = got_error(GOT_ERR_NO_OBJ);
3643 goto done;
3646 error = got_object_get_type(&obj_type, repo, obj_id);
3647 if (error)
3648 goto done;
3650 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3651 error = got_error(GOT_ERR_OBJ_TYPE);
3652 goto done;
3655 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3656 if (error)
3657 goto done;
3659 hdrlen = got_object_blob_get_hdrlen(blob);
3660 do {
3661 error = got_object_blob_read_block(&len, blob);
3662 if (error)
3663 goto done;
3664 buf = got_object_blob_get_read_buf(blob);
3667 * Skip blob object header first time around,
3668 * which also contains a zero byte.
3670 buf += hdrlen;
3671 if (set_mime == 0) {
3672 if (isbinary(buf, len - hdrlen))
3673 gw_trans->mime = KMIME_APP_OCTET_STREAM;
3674 else
3675 gw_trans->mime = KMIME_TEXT_PLAIN;
3676 set_mime = 1;
3677 error = gw_display_index(gw_trans);
3678 if (error)
3679 goto done;
3681 khttp_write(gw_trans->gw_req, buf, len - hdrlen);
3682 hdrlen = 0;
3683 } while (len != 0);
3684 done:
3685 free(in_repo_path);
3686 free(commit_id);
3687 free(obj_id);
3688 free(path);
3689 if (blob)
3690 got_object_blob_close(blob);
3691 if (repo)
3692 got_repo_close(repo);
3693 if (error == NULL && kerr != KCGI_OK)
3694 error = gw_kcgi_error(kerr);
3695 return error;
3698 static const struct got_error *
3699 gw_output_repo_tree(struct gw_trans *gw_trans)
3701 const struct got_error *error = NULL;
3702 struct got_repository *repo = NULL;
3703 struct got_object_id *tree_id = NULL, *commit_id = NULL;
3704 struct got_tree_object *tree = NULL;
3705 char *path = NULL, *in_repo_path = NULL;
3706 char *id_str = NULL;
3707 char *build_folder = NULL;
3708 char *href_blob = NULL, *href_blame = NULL;
3709 const char *class = NULL;
3710 int nentries, i, class_flip = 0;
3711 enum kcgi_err kerr = KCGI_OK;
3713 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3714 if (error)
3715 return error;
3717 if (gw_trans->repo_folder != NULL) {
3718 error = gw_strdup_string(&path, gw_trans->repo_folder, NULL);
3719 if (error)
3720 goto done;
3721 } else {
3722 error = got_repo_map_path(&in_repo_path, repo,
3723 gw_trans->repo_path, 1);
3724 if (error)
3725 goto done;
3726 free(path);
3727 path = in_repo_path;
3730 if (gw_trans->commit == NULL) {
3731 struct got_reference *head_ref;
3732 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
3733 if (error)
3734 goto done;
3735 error = got_ref_resolve(&commit_id, repo, head_ref);
3736 if (error)
3737 goto done;
3738 got_ref_close(head_ref);
3740 } else {
3741 error = got_repo_match_object_id(&commit_id, NULL,
3742 gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
3743 if (error)
3744 goto done;
3747 error = got_object_id_str(&gw_trans->commit, commit_id);
3748 if (error)
3749 goto done;
3751 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
3752 if (error)
3753 goto done;
3755 error = got_object_open_as_tree(&tree, repo, tree_id);
3756 if (error)
3757 goto done;
3759 nentries = got_object_tree_get_nentries(tree);
3760 for (i = 0; i < nentries; i++) {
3761 struct got_tree_entry *te;
3762 const char *modestr = "";
3763 mode_t mode;
3765 te = got_object_tree_get_entry(tree, i);
3767 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
3768 if (error)
3769 goto done;
3771 mode = got_tree_entry_get_mode(te);
3772 if (got_object_tree_entry_is_submodule(te))
3773 modestr = "$";
3774 else if (S_ISLNK(mode))
3775 modestr = "@";
3776 else if (S_ISDIR(mode))
3777 modestr = "/";
3778 else if (mode & S_IXUSR)
3779 modestr = "*";
3781 if (class_flip == 0) {
3782 class = "back_lightgray";
3783 class_flip = 1;
3784 } else {
3785 class = "back_white";
3786 class_flip = 0;
3789 if (S_ISDIR(mode)) {
3790 if (asprintf(&build_folder, "%s/%s",
3791 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3792 got_tree_entry_get_name(te)) == -1) {
3793 error = got_error_from_errno(
3794 "asprintf");
3795 goto done;
3797 if (asprintf(&href_blob,
3798 "?path=%s&action=%s&commit=%s&folder=%s",
3799 gw_trans->repo_name, gw_trans->action_name,
3800 gw_trans->commit, build_folder) == -1) {
3801 error = got_error_from_errno("asprintf");
3802 goto done;
3805 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3806 KATTR_ID, "tree_wrapper", KATTR__MAX);
3807 if (kerr != KCGI_OK)
3808 goto done;
3809 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3810 KATTR_ID, "tree_line", KATTR_CLASS, class,
3811 KATTR__MAX);
3812 if (kerr != KCGI_OK)
3813 goto done;
3814 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3815 KATTR_HREF, href_blob, KATTR_CLASS,
3816 "diff_directory", KATTR__MAX);
3817 if (kerr != KCGI_OK)
3818 goto done;
3819 kerr = khtml_puts(gw_trans->gw_html_req,
3820 got_tree_entry_get_name(te));
3821 if (kerr != KCGI_OK)
3822 goto done;
3823 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3824 if (kerr != KCGI_OK)
3825 goto done;
3826 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3827 if (kerr != KCGI_OK)
3828 goto done;
3829 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3830 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
3831 KATTR__MAX);
3832 if (kerr != KCGI_OK)
3833 goto done;
3834 kerr = khtml_entity(gw_trans->gw_html_req,
3835 KENTITY_nbsp);
3836 if (kerr != KCGI_OK)
3837 goto done;
3838 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3839 if (kerr != KCGI_OK)
3840 goto done;
3841 } else {
3842 if (asprintf(&href_blob,
3843 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3844 gw_trans->repo_name, "blob", gw_trans->commit,
3845 got_tree_entry_get_name(te),
3846 gw_trans->repo_folder ?
3847 gw_trans->repo_folder : "") == -1) {
3848 error = got_error_from_errno("asprintf");
3849 goto done;
3851 if (asprintf(&href_blame,
3852 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3853 gw_trans->repo_name, "blame", gw_trans->commit,
3854 got_tree_entry_get_name(te),
3855 gw_trans->repo_folder ?
3856 gw_trans->repo_folder : "") == -1) {
3857 error = got_error_from_errno("asprintf");
3858 goto done;
3861 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3862 KATTR_ID, "tree_wrapper", KATTR__MAX);
3863 if (kerr != KCGI_OK)
3864 goto done;
3865 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3866 KATTR_ID, "tree_line", KATTR_CLASS, class,
3867 KATTR__MAX);
3868 if (kerr != KCGI_OK)
3869 goto done;
3870 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3871 KATTR_HREF, href_blob, KATTR__MAX);
3872 if (kerr != KCGI_OK)
3873 goto done;
3874 kerr = khtml_puts(gw_trans->gw_html_req,
3875 got_tree_entry_get_name(te));
3876 if (kerr != KCGI_OK)
3877 goto done;
3878 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3879 if (kerr != KCGI_OK)
3880 goto done;
3881 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3882 if (kerr != KCGI_OK)
3883 goto done;
3884 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3885 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
3886 KATTR__MAX);
3887 if (kerr != KCGI_OK)
3888 goto done;
3890 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3891 KATTR_HREF, href_blob, KATTR__MAX);
3892 if (kerr != KCGI_OK)
3893 goto done;
3894 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
3895 if (kerr != KCGI_OK)
3896 goto done;
3897 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3898 if (kerr != KCGI_OK)
3899 goto done;
3901 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3902 if (kerr != KCGI_OK)
3903 goto done;
3905 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3906 KATTR_HREF, href_blame, KATTR__MAX);
3907 if (kerr != KCGI_OK)
3908 goto done;
3909 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
3910 if (kerr != KCGI_OK)
3911 goto done;
3913 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3914 if (kerr != KCGI_OK)
3915 goto done;
3917 free(id_str);
3918 id_str = NULL;
3919 free(href_blob);
3920 href_blob = NULL;
3921 free(build_folder);
3922 build_folder = NULL;
3924 done:
3925 if (tree)
3926 got_object_tree_close(tree);
3927 if (repo)
3928 got_repo_close(repo);
3929 free(id_str);
3930 free(href_blob);
3931 free(href_blame);
3932 free(in_repo_path);
3933 free(tree_id);
3934 free(build_folder);
3935 if (error == NULL && kerr != KCGI_OK)
3936 error = gw_kcgi_error(kerr);
3937 return error;
3940 static const struct got_error *
3941 gw_output_repo_heads(struct gw_trans *gw_trans)
3943 const struct got_error *error = NULL;
3944 struct got_repository *repo = NULL;
3945 struct got_reflist_head refs;
3946 struct got_reflist_entry *re;
3947 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
3948 char *href_commits = NULL;
3949 enum kcgi_err kerr = KCGI_OK;
3951 SIMPLEQ_INIT(&refs);
3953 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3954 if (error)
3955 goto done;
3957 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
3958 NULL);
3959 if (error)
3960 goto done;
3962 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3963 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
3964 if (kerr != KCGI_OK)
3965 goto done;
3966 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3967 KATTR_ID, "summary_heads_title", KATTR__MAX);
3968 if (kerr != KCGI_OK)
3969 goto done;
3970 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
3971 if (kerr != KCGI_OK)
3972 goto done;
3973 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3974 if (kerr != KCGI_OK)
3975 goto done;
3976 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3977 KATTR_ID, "summary_heads_content", KATTR__MAX);
3978 if (kerr != KCGI_OK)
3979 goto done;
3981 SIMPLEQ_FOREACH(re, &refs, entry) {
3982 char *refname;
3984 error = gw_strdup_string(&refname, NULL,
3985 got_ref_get_name(re->ref));
3986 if (error)
3987 goto done;
3989 if (strncmp(refname, "refs/heads/", 11) != 0) {
3990 free(refname);
3991 continue;
3994 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
3995 refname, TM_DIFF);
3996 if (error)
3997 goto done;
3999 if (strncmp(refname, "refs/heads/", 11) == 0)
4000 refname += 11;
4002 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4003 KATTR_ID, "heads_wrapper", KATTR__MAX);
4004 if (kerr != KCGI_OK)
4005 goto done;
4006 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4007 KATTR_ID, "heads_age", KATTR__MAX);
4008 if (kerr != KCGI_OK)
4009 goto done;
4010 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
4011 if (kerr != KCGI_OK)
4012 goto done;
4013 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4014 if (kerr != KCGI_OK)
4015 goto done;
4016 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4017 KATTR_ID, "heads_space", KATTR__MAX);
4018 if (kerr != KCGI_OK)
4019 goto done;
4020 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
4021 if (kerr != KCGI_OK)
4022 goto done;
4023 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4024 if (kerr != KCGI_OK)
4025 goto done;
4026 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4027 KATTR_ID, "head", KATTR__MAX);
4028 if (kerr != KCGI_OK)
4029 goto done;
4030 if (asprintf(&href_summary,
4031 "?path=%s&action=summary&headref=%s",
4032 gw_trans->repo_name, refname) == -1) {
4033 error = got_error_from_errno("asprintf");
4034 goto done;
4036 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4037 href_summary, KATTR__MAX);
4038 kerr = khtml_puts(gw_trans->gw_html_req, refname);
4039 if (kerr != KCGI_OK)
4040 goto done;
4041 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4042 if (kerr != KCGI_OK)
4043 goto done;
4045 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4046 "navs_wrapper", KATTR__MAX);
4047 if (kerr != KCGI_OK)
4048 goto done;
4049 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4050 "navs", KATTR__MAX);
4051 if (kerr != KCGI_OK)
4052 goto done;
4054 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4055 href_summary, KATTR__MAX);
4056 if (kerr != KCGI_OK)
4057 goto done;
4058 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4059 if (kerr != KCGI_OK)
4060 goto done;
4061 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4062 if (kerr != KCGI_OK)
4063 goto done;
4065 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4066 if (kerr != KCGI_OK)
4067 goto done;
4068 if (asprintf(&href_briefs, "?path=%s&action=briefs&headref=%s",
4069 gw_trans->repo_name, refname) == -1) {
4070 error = got_error_from_errno("asprintf");
4071 goto done;
4073 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4074 href_briefs, KATTR__MAX);
4075 if (kerr != KCGI_OK)
4076 goto done;
4077 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4078 if (kerr != KCGI_OK)
4079 goto done;
4080 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4081 if (kerr != KCGI_OK)
4082 goto done;
4084 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4085 if (kerr != KCGI_OK)
4086 goto done;
4088 if (asprintf(&href_commits,
4089 "?path=%s&action=commits&headref=%s",
4090 gw_trans->repo_name, refname) == -1) {
4091 error = got_error_from_errno("asprintf");
4092 goto done;
4094 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4095 href_commits, KATTR__MAX);
4096 if (kerr != KCGI_OK)
4097 goto done;
4098 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4099 if (kerr != KCGI_OK)
4100 goto done;
4101 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4102 if (kerr != KCGI_OK)
4103 goto done;
4105 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4106 "dotted_line", KATTR__MAX);
4107 if (kerr != KCGI_OK)
4108 goto done;
4109 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4110 if (kerr != KCGI_OK)
4111 goto done;
4112 free(href_summary);
4113 href_summary = NULL;
4114 free(href_briefs);
4115 href_briefs = NULL;
4116 free(href_commits);
4117 href_commits = NULL;
4119 done:
4120 got_ref_list_free(&refs);
4121 free(href_summary);
4122 free(href_briefs);
4123 free(href_commits);
4124 if (repo)
4125 got_repo_close(repo);
4126 return error;
4129 static const struct got_error *
4130 gw_output_site_link(struct gw_trans *gw_trans)
4132 const struct got_error *error = NULL;
4133 char *href_summary = NULL;
4134 enum kcgi_err kerr = KCGI_OK;
4136 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4137 "site_link", KATTR__MAX);
4138 if (kerr != KCGI_OK)
4139 goto done;
4140 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4141 KATTR__MAX);
4142 if (kerr != KCGI_OK)
4143 goto done;
4144 kerr = khtml_puts(gw_trans->gw_html_req,
4145 gw_trans->gw_conf->got_site_link);
4146 if (kerr != KCGI_OK)
4147 goto done;
4148 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4149 if (kerr != KCGI_OK)
4150 goto done;
4152 if (gw_trans->repo_name != NULL) {
4153 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4154 if (kerr != KCGI_OK)
4155 goto done;
4156 if (asprintf(&href_summary, "?path=%s&action=summary",
4157 gw_trans->repo_name) == -1)
4158 goto done;
4159 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4160 href_summary, KATTR__MAX);
4161 if (kerr != KCGI_OK)
4162 goto done;
4163 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4164 if (kerr != KCGI_OK)
4165 goto done;
4166 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4167 if (kerr != KCGI_OK)
4168 goto done;
4169 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4170 if (kerr != KCGI_OK)
4171 goto done;
4172 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->action_name);
4173 if (kerr != KCGI_OK)
4174 goto done;
4177 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4178 if (kerr != KCGI_OK)
4179 goto done;
4180 done:
4181 free(href_summary);
4182 return error;
4185 static const struct got_error *
4186 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4188 const struct got_error *error = NULL;
4189 char *color = NULL;
4190 enum kcgi_err kerr = KCGI_OK;
4192 if (strncmp(buf, "-", 1) == 0)
4193 color = "diff_minus";
4194 else if (strncmp(buf, "+", 1) == 0)
4195 color = "diff_plus";
4196 else if (strncmp(buf, "@@", 2) == 0)
4197 color = "diff_chunk_header";
4198 else if (strncmp(buf, "@@", 2) == 0)
4199 color = "diff_chunk_header";
4200 else if (strncmp(buf, "commit +", 8) == 0)
4201 color = "diff_meta";
4202 else if (strncmp(buf, "commit -", 8) == 0)
4203 color = "diff_meta";
4204 else if (strncmp(buf, "blob +", 6) == 0)
4205 color = "diff_meta";
4206 else if (strncmp(buf, "blob -", 6) == 0)
4207 color = "diff_meta";
4208 else if (strncmp(buf, "file +", 6) == 0)
4209 color = "diff_meta";
4210 else if (strncmp(buf, "file -", 6) == 0)
4211 color = "diff_meta";
4212 else if (strncmp(buf, "from:", 5) == 0)
4213 color = "diff_author";
4214 else if (strncmp(buf, "via:", 4) == 0)
4215 color = "diff_author";
4216 else if (strncmp(buf, "date:", 5) == 0)
4217 color = "diff_date";
4218 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4219 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4220 if (error == NULL && kerr != KCGI_OK)
4221 error = gw_kcgi_error(kerr);
4222 return error;
4225 int
4226 main(int argc, char *argv[])
4228 const struct got_error *error = NULL;
4229 struct gw_trans *gw_trans;
4230 struct gw_dir *dir = NULL, *tdir;
4231 const char *page = "index";
4232 int gw_malloc = 1;
4233 enum kcgi_err kerr;
4235 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4236 errx(1, "malloc");
4238 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4239 errx(1, "malloc");
4241 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4242 errx(1, "malloc");
4244 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4245 errx(1, "malloc");
4247 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4248 if (kerr != KCGI_OK) {
4249 error = gw_kcgi_error(kerr);
4250 goto done;
4253 if ((gw_trans->gw_conf =
4254 malloc(sizeof(struct gotweb_conf))) == NULL) {
4255 gw_malloc = 0;
4256 error = got_error_from_errno("malloc");
4257 goto done;
4260 TAILQ_INIT(&gw_trans->gw_dirs);
4261 TAILQ_INIT(&gw_trans->gw_headers);
4263 gw_trans->page = 0;
4264 gw_trans->repos_total = 0;
4265 gw_trans->repo_path = NULL;
4266 gw_trans->commit = NULL;
4267 error = gw_strdup_string(&gw_trans->headref, GOT_REF_HEAD, NULL);
4268 if (error)
4269 goto done;
4270 gw_trans->mime = KMIME_TEXT_HTML;
4271 gw_trans->gw_tmpl->key = gw_templs;
4272 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4273 gw_trans->gw_tmpl->arg = gw_trans;
4274 gw_trans->gw_tmpl->cb = gw_template;
4275 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
4276 if (error)
4277 goto done;
4279 error = gw_parse_querystring(gw_trans);
4280 if (error)
4281 goto done;
4283 if (gw_trans->action == GW_BLOB)
4284 error = gw_blob(gw_trans);
4285 else
4286 error = gw_display_index(gw_trans);
4287 done:
4288 if (error) {
4289 gw_trans->mime = KMIME_TEXT_PLAIN;
4290 gw_trans->action = GW_ERR;
4291 gw_display_error(gw_trans, error);
4293 if (gw_malloc) {
4294 free(gw_trans->gw_conf->got_repos_path);
4295 free(gw_trans->gw_conf->got_site_name);
4296 free(gw_trans->gw_conf->got_site_owner);
4297 free(gw_trans->gw_conf->got_site_link);
4298 free(gw_trans->gw_conf->got_logo);
4299 free(gw_trans->gw_conf->got_logo_url);
4300 free(gw_trans->gw_conf);
4301 free(gw_trans->commit);
4302 free(gw_trans->repo_path);
4303 free(gw_trans->repo_name);
4304 free(gw_trans->repo_file);
4305 free(gw_trans->action_name);
4306 free(gw_trans->headref);
4308 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4309 free(dir->name);
4310 free(dir->description);
4311 free(dir->age);
4312 free(dir->url);
4313 free(dir->path);
4314 free(dir);
4319 khttp_free(gw_trans->gw_req);
4320 return 0;