Blob


1 /*
2 * Copyright (c) 2019, 2020 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/queue.h>
19 #include <sys/stat.h>
20 #include <sys/types.h>
22 #include <ctype.h>
23 #include <dirent.h>
24 #include <err.h>
25 #include <errno.h>
26 #include <regex.h>
27 #include <stdarg.h>
28 #include <stdint.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
34 #include <got_object.h>
35 #include <got_reference.h>
36 #include <got_repository.h>
37 #include <got_path.h>
38 #include <got_cancel.h>
39 #include <got_worktree.h>
40 #include <got_diff.h>
41 #include <got_commit_graph.h>
42 #include <got_blame.h>
43 #include <got_privsep.h>
44 #include <got_opentemp.h>
46 #include <kcgi.h>
47 #include <kcgihtml.h>
49 #include "gotweb.h"
51 #ifndef nitems
52 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
53 #endif
55 struct gw_trans {
56 TAILQ_HEAD(headers, gw_header) gw_headers;
57 TAILQ_HEAD(dirs, gw_dir) gw_dirs;
58 struct gw_dir *gw_dir;
59 struct gotweb_conf *gw_conf;
60 struct ktemplate *gw_tmpl;
61 struct khtmlreq *gw_html_req;
62 struct kreq *gw_req;
63 const struct got_error *error;
64 const char *repo_name;
65 char *repo_path;
66 char *commit;
67 const char *repo_file;
68 char *repo_folder;
69 const char *headref;
70 unsigned int action;
71 unsigned int page;
72 unsigned int repos_total;
73 enum kmime mime;
74 };
76 struct gw_header {
77 TAILQ_ENTRY(gw_header) entry;
78 struct got_repository *repo;
79 struct got_reflist_head refs;
80 struct got_commit_object *commit;
81 struct got_object_id *id;
82 char *path;
84 char *refs_str;
85 char *commit_id; /* id_str1 */
86 char *parent_id; /* id_str2 */
87 char *tree_id;
88 const char *author;
89 const char *committer;
90 char *commit_msg;
91 time_t committer_time;
92 };
94 struct gw_dir {
95 TAILQ_ENTRY(gw_dir) entry;
96 char *name;
97 char *owner;
98 char *description;
99 char *url;
100 char *age;
101 char *path;
102 };
104 enum gw_key {
105 KEY_ACTION,
106 KEY_COMMIT_ID,
107 KEY_FILE,
108 KEY_FOLDER,
109 KEY_HEADREF,
110 KEY_PAGE,
111 KEY_PATH,
112 KEY__ZMAX
113 };
115 enum gw_tmpl {
116 TEMPL_CONTENT,
117 TEMPL_HEAD,
118 TEMPL_HEADER,
119 TEMPL_SEARCH,
120 TEMPL_SITEPATH,
121 TEMPL_SITEOWNER,
122 TEMPL_TITLE,
123 TEMPL__MAX
124 };
126 enum gw_ref_tm {
127 TM_DIFF,
128 TM_LONG,
129 };
131 enum gw_tags {
132 TAGBRIEF,
133 TAGFULL,
134 };
136 static const char *const gw_templs[TEMPL__MAX] = {
137 "content",
138 "head",
139 "header",
140 "search",
141 "sitepath",
142 "siteowner",
143 "title",
144 };
146 static const struct kvalid gw_keys[KEY__ZMAX] = {
147 { kvalid_stringne, "action" },
148 { kvalid_stringne, "commit" },
149 { kvalid_stringne, "file" },
150 { kvalid_stringne, "folder" },
151 { kvalid_stringne, "headref" },
152 { kvalid_int, "page" },
153 { kvalid_stringne, "path" },
154 };
156 static struct gw_header *gw_init_header(void);
158 static void gw_free_header(struct gw_header *);
160 static int gw_template(size_t, void *);
162 static const struct got_error *gw_error(struct gw_trans *);
163 static const struct got_error *gw_init_gw_dir(struct gw_dir **, const char *);
164 static const struct got_error *gw_get_repo_description(char **,
165 struct gw_trans *, char *);
166 static const struct got_error *gw_get_repo_owner(char **, struct gw_trans *,
167 char *);
168 static const struct got_error *gw_get_time_str(char **, time_t, int);
169 static const struct got_error *gw_get_repo_age(char **, struct gw_trans *,
170 char *, const char *, int);
171 static const struct got_error *gw_output_file_blame(struct gw_trans *);
172 static const struct got_error *gw_output_blob_buf(struct gw_trans *);
173 static const struct got_error *gw_output_repo_tree(struct gw_trans *);
174 static const struct got_error *gw_output_diff(struct gw_trans *,
175 struct gw_header *);
176 static const struct got_error *gw_output_repo_tags(struct gw_trans *,
177 struct gw_header *, int, int);
178 static const struct got_error *gw_output_repo_heads(struct gw_trans *);
179 static const struct got_error *gw_output_site_link(struct gw_trans *);
180 static const struct got_error *gw_get_clone_url(char **, struct gw_trans *,
181 char *);
182 static const struct got_error *gw_colordiff_line(struct gw_trans *, char *);
184 static const struct got_error *gw_gen_commit_header(struct gw_trans *, char *,
185 char*);
186 static const struct got_error *gw_gen_diff_header(struct gw_trans *, char *,
187 char*);
188 static const struct got_error *gw_gen_author_header(struct gw_trans *,
189 const char *);
190 static const struct got_error *gw_gen_age_header(struct gw_trans *,
191 const char *);
192 static const struct got_error *gw_gen_committer_header(struct gw_trans *,
193 const char *);
194 static const struct got_error *gw_gen_commit_msg_header(struct gw_trans*,
195 char *);
196 static const struct got_error *gw_gen_tree_header(struct gw_trans *, char *);
197 static const struct got_error *gw_display_open(struct gw_trans *, enum khttp,
198 enum kmime);
199 static const struct got_error *gw_display_index(struct gw_trans *);
200 static const struct got_error *gw_get_header(struct gw_trans *,
201 struct gw_header *, int);
202 static const struct got_error *gw_get_commits(struct gw_trans *,
203 struct gw_header *, int);
204 static const struct got_error *gw_get_commit(struct gw_trans *,
205 struct gw_header *);
206 static const struct got_error *gw_apply_unveil(const char *);
207 static const struct got_error *gw_blame_cb(void *, int, int,
208 struct got_object_id *);
209 static const struct got_error *gw_load_got_paths(struct gw_trans *);
210 static const struct got_error *gw_load_got_path(struct gw_trans *,
211 struct gw_dir *);
212 static const struct got_error *gw_parse_querystring(struct gw_trans *);
213 static const struct got_error *gw_blame(struct gw_trans *);
214 static const struct got_error *gw_blob(struct gw_trans *);
215 static const struct got_error *gw_diff(struct gw_trans *);
216 static const struct got_error *gw_index(struct gw_trans *);
217 static const struct got_error *gw_commits(struct gw_trans *);
218 static const struct got_error *gw_briefs(struct gw_trans *);
219 static const struct got_error *gw_summary(struct gw_trans *);
220 static const struct got_error *gw_tree(struct gw_trans *);
221 static const struct got_error *gw_tag(struct gw_trans *);
223 struct gw_query_action {
224 unsigned int func_id;
225 const char *func_name;
226 const struct got_error *(*func_main)(struct gw_trans *);
227 char *template;
228 };
230 enum gw_query_actions {
231 GW_BLAME,
232 GW_BLOB,
233 GW_BRIEFS,
234 GW_COMMITS,
235 GW_DIFF,
236 GW_ERR,
237 GW_INDEX,
238 GW_SUMMARY,
239 GW_TAG,
240 GW_TREE,
241 };
243 static struct gw_query_action gw_query_funcs[] = {
244 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
245 { GW_BLOB, "blob", NULL, NULL },
246 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
247 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
248 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
249 { GW_ERR, "error", gw_error, "gw_tmpl/err.tmpl" },
250 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
251 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
252 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
253 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
254 };
256 static const char *
257 gw_get_action_name(struct gw_trans *gw_trans)
259 return gw_query_funcs[gw_trans->action].func_name;
262 static const struct got_error *
263 gw_kcgi_error(enum kcgi_err kerr)
265 if (kerr == KCGI_OK)
266 return NULL;
268 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
269 return got_error(GOT_ERR_CANCELLED);
271 if (kerr == KCGI_ENOMEM)
272 return got_error_set_errno(ENOMEM,
273 kcgi_strerror(kerr));
275 if (kerr == KCGI_ENFILE)
276 return got_error_set_errno(ENFILE,
277 kcgi_strerror(kerr));
279 if (kerr == KCGI_EAGAIN)
280 return got_error_set_errno(EAGAIN,
281 kcgi_strerror(kerr));
283 if (kerr == KCGI_FORM)
284 return got_error_msg(GOT_ERR_IO,
285 kcgi_strerror(kerr));
287 return got_error_from_errno(kcgi_strerror(kerr));
290 static const struct got_error *
291 gw_apply_unveil(const char *repo_path)
293 const struct got_error *err;
295 if (repo_path && unveil(repo_path, "r") != 0)
296 return got_error_from_errno2("unveil", repo_path);
298 if (unveil("/tmp", "rwc") != 0)
299 return got_error_from_errno2("unveil", "/tmp");
301 err = got_privsep_unveil_exec_helpers();
302 if (err != NULL)
303 return err;
305 if (unveil(NULL, NULL) != 0)
306 return got_error_from_errno("unveil");
308 return NULL;
311 static int
312 isbinary(const uint8_t *buf, size_t n)
314 size_t i;
316 for (i = 0; i < n; i++)
317 if (buf[i] == 0)
318 return 1;
319 return 0;
322 static const struct got_error *
323 gw_blame(struct gw_trans *gw_trans)
325 const struct got_error *error = NULL;
326 struct gw_header *header = NULL;
327 char *age = NULL;
328 enum kcgi_err kerr;
330 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
331 NULL) == -1)
332 return got_error_from_errno("pledge");
334 if ((header = gw_init_header()) == NULL)
335 return got_error_from_errno("malloc");
337 error = gw_apply_unveil(gw_trans->gw_dir->path);
338 if (error)
339 goto done;
341 error = gw_get_header(gw_trans, header, 1);
342 if (error)
343 goto done;
345 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
346 "blame_header_wrapper", KATTR__MAX);
347 if (kerr != KCGI_OK)
348 goto done;
349 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
350 "blame_header", KATTR__MAX);
351 if (kerr != KCGI_OK)
352 goto done;
353 error = gw_get_time_str(&age, header->committer_time,
354 TM_LONG);
355 if (error)
356 goto done;
357 error = gw_gen_age_header(gw_trans, age ?age : "");
358 if (error)
359 goto done;
360 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
361 if (error)
362 goto done;
363 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
364 if (kerr != KCGI_OK)
365 goto done;
366 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
367 "dotted_line", KATTR__MAX);
368 if (kerr != KCGI_OK)
369 goto done;
370 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
371 if (kerr != KCGI_OK)
372 goto done;
374 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
375 "blame", KATTR__MAX);
376 if (kerr != KCGI_OK)
377 goto done;
378 error = gw_output_file_blame(gw_trans);
379 if (error)
380 goto done;
381 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
382 done:
383 got_ref_list_free(&header->refs);
384 gw_free_header(header);
385 if (error == NULL && kerr != KCGI_OK)
386 error = gw_kcgi_error(kerr);
387 return error;
390 static const struct got_error *
391 gw_blob(struct gw_trans *gw_trans)
393 const struct got_error *error = NULL;
394 struct gw_header *header = NULL;
396 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
397 NULL) == -1)
398 return got_error_from_errno("pledge");
400 if ((header = gw_init_header()) == NULL)
401 return got_error_from_errno("malloc");
403 error = gw_apply_unveil(gw_trans->gw_dir->path);
404 if (error)
405 goto done;
407 error = gw_get_header(gw_trans, header, 1);
408 if (error)
409 goto done;
411 error = gw_output_blob_buf(gw_trans);
412 done:
413 got_ref_list_free(&header->refs);
414 gw_free_header(header);
415 return error;
418 static const struct got_error *
419 gw_diff(struct gw_trans *gw_trans)
421 const struct got_error *error = NULL;
422 struct gw_header *header = NULL;
423 char *age = NULL;
424 enum kcgi_err kerr = KCGI_OK;
426 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
427 NULL) == -1)
428 return got_error_from_errno("pledge");
430 if ((header = gw_init_header()) == NULL)
431 return got_error_from_errno("malloc");
433 error = gw_apply_unveil(gw_trans->gw_dir->path);
434 if (error)
435 goto done;
437 error = gw_get_header(gw_trans, header, 1);
438 if (error)
439 goto done;
441 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
442 "diff_header_wrapper", KATTR__MAX);
443 if (kerr != KCGI_OK)
444 goto done;
445 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
446 "diff_header", KATTR__MAX);
447 if (kerr != KCGI_OK)
448 goto done;
449 error = gw_gen_diff_header(gw_trans, header->parent_id,
450 header->commit_id);
451 if (error)
452 goto done;
453 error = gw_gen_commit_header(gw_trans, header->commit_id,
454 header->refs_str);
455 if (error)
456 goto done;
457 error = gw_gen_tree_header(gw_trans, header->tree_id);
458 if (error)
459 goto done;
460 error = gw_gen_author_header(gw_trans, header->author);
461 if (error)
462 goto done;
463 error = gw_gen_committer_header(gw_trans, header->author);
464 if (error)
465 goto done;
466 error = gw_get_time_str(&age, header->committer_time,
467 TM_LONG);
468 if (error)
469 goto done;
470 error = gw_gen_age_header(gw_trans, age ?age : "");
471 if (error)
472 goto done;
473 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
474 if (error)
475 goto done;
476 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
477 if (kerr != KCGI_OK)
478 goto done;
479 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
480 "dotted_line", KATTR__MAX);
481 if (kerr != KCGI_OK)
482 goto done;
483 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
484 if (kerr != KCGI_OK)
485 goto done;
487 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
488 "diff", KATTR__MAX);
489 if (kerr != KCGI_OK)
490 goto done;
491 error = gw_output_diff(gw_trans, header);
492 if (error)
493 goto done;
495 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
496 done:
497 got_ref_list_free(&header->refs);
498 gw_free_header(header);
499 free(age);
500 if (error == NULL && kerr != KCGI_OK)
501 error = gw_kcgi_error(kerr);
502 return error;
505 static const struct got_error *
506 gw_index(struct gw_trans *gw_trans)
508 const struct got_error *error = NULL;
509 struct gw_dir *gw_dir = NULL;
510 char *href_next = NULL, *href_prev = NULL, *href_summary = NULL;
511 char *href_briefs = NULL, *href_commits = NULL, *href_tree = NULL;
512 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
513 enum kcgi_err kerr;
515 if (pledge("stdio rpath proc exec sendfd unveil",
516 NULL) == -1) {
517 error = got_error_from_errno("pledge");
518 return error;
521 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path);
522 if (error)
523 return error;
525 error = gw_load_got_paths(gw_trans);
526 if (error)
527 return error;
529 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
530 "index_header", KATTR__MAX);
531 if (kerr != KCGI_OK)
532 return gw_kcgi_error(kerr);
533 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
534 "index_header_project", KATTR__MAX);
535 if (kerr != KCGI_OK)
536 return gw_kcgi_error(kerr);
537 kerr = khtml_puts(gw_trans->gw_html_req, "Project");
538 if (kerr != KCGI_OK)
539 return gw_kcgi_error(kerr);
540 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
541 if (kerr != KCGI_OK)
542 return gw_kcgi_error(kerr);
544 if (gw_trans->gw_conf->got_show_repo_description) {
545 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
546 "index_header_description", KATTR__MAX);
547 if (kerr != KCGI_OK)
548 return gw_kcgi_error(kerr);
549 kerr = khtml_puts(gw_trans->gw_html_req, "Description");
550 if (kerr != KCGI_OK)
551 return gw_kcgi_error(kerr);
552 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
553 if (kerr != KCGI_OK)
554 return gw_kcgi_error(kerr);
557 if (gw_trans->gw_conf->got_show_repo_owner) {
558 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
559 "index_header_owner", KATTR__MAX);
560 if (kerr != KCGI_OK)
561 return gw_kcgi_error(kerr);
562 kerr = khtml_puts(gw_trans->gw_html_req, "Owner");
563 if (kerr != KCGI_OK)
564 return gw_kcgi_error(kerr);
565 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
566 if (kerr != KCGI_OK)
567 return gw_kcgi_error(kerr);
570 if (gw_trans->gw_conf->got_show_repo_age) {
571 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
572 "index_header_age", KATTR__MAX);
573 if (kerr != KCGI_OK)
574 return gw_kcgi_error(kerr);
575 kerr = khtml_puts(gw_trans->gw_html_req, "Last Change");
576 if (kerr != KCGI_OK)
577 return gw_kcgi_error(kerr);
578 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
579 if (kerr != KCGI_OK)
580 return gw_kcgi_error(kerr);
583 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
584 if (kerr != KCGI_OK)
585 return gw_kcgi_error(kerr);
587 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
588 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
589 "index_wrapper", KATTR__MAX);
590 if (kerr != KCGI_OK)
591 return gw_kcgi_error(kerr);
592 kerr = khtml_puts(gw_trans->gw_html_req,
593 "No repositories found in ");
594 if (kerr != KCGI_OK)
595 return gw_kcgi_error(kerr);
596 kerr = khtml_puts(gw_trans->gw_html_req,
597 gw_trans->gw_conf->got_repos_path);
598 if (kerr != KCGI_OK)
599 return gw_kcgi_error(kerr);
600 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
601 if (kerr != KCGI_OK)
602 return gw_kcgi_error(kerr);
603 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
604 "dotted_line", KATTR__MAX);
605 if (kerr != KCGI_OK)
606 return gw_kcgi_error(kerr);
607 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
608 if (kerr != KCGI_OK)
609 return gw_kcgi_error(kerr);
610 return error;
613 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
614 dir_c++;
616 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
617 if (gw_trans->page > 0 && (gw_trans->page *
618 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
619 prev_disp++;
620 continue;
623 prev_disp++;
625 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
626 "index_wrapper", KATTR__MAX);
627 if (kerr != KCGI_OK)
628 goto done;
630 if (asprintf(&href_summary, "?path=%s&action=summary",
631 gw_dir->name) == -1) {
632 error = got_error_from_errno("asprintf");
633 goto done;
635 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
636 "index_project", KATTR__MAX);
637 if (kerr != KCGI_OK)
638 goto done;
639 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
640 href_summary, KATTR__MAX);
641 if (kerr != KCGI_OK)
642 goto done;
643 kerr = khtml_puts(gw_trans->gw_html_req, gw_dir->name);
644 if (kerr != KCGI_OK)
645 goto done;
646 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
647 if (kerr != KCGI_OK)
648 goto done;
649 if (gw_trans->gw_conf->got_show_repo_description) {
650 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
651 KATTR_ID, "index_project_description", KATTR__MAX);
652 if (kerr != KCGI_OK)
653 goto done;
654 kerr = khtml_puts(gw_trans->gw_html_req,
655 gw_dir->description ? gw_dir->description : "");
656 if (kerr != KCGI_OK)
657 goto done;
658 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
659 if (kerr != KCGI_OK)
660 goto done;
662 if (gw_trans->gw_conf->got_show_repo_owner) {
663 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
664 KATTR_ID, "index_project_owner", KATTR__MAX);
665 if (kerr != KCGI_OK)
666 goto done;
667 kerr = khtml_puts(gw_trans->gw_html_req,
668 gw_dir->owner ? gw_dir->owner : "");
669 if (kerr != KCGI_OK)
670 goto done;
671 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
672 if (kerr != KCGI_OK)
673 goto done;
675 if (gw_trans->gw_conf->got_show_repo_age) {
676 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
677 KATTR_ID, "index_project_age", KATTR__MAX);
678 if (kerr != KCGI_OK)
679 goto done;
680 kerr = khtml_puts(gw_trans->gw_html_req,
681 gw_dir->age ? gw_dir->age : "");
682 if (kerr != KCGI_OK)
683 goto done;
684 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
685 if (kerr != KCGI_OK)
686 goto done;
689 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
690 "navs_wrapper", KATTR__MAX);
691 if (kerr != KCGI_OK)
692 goto done;
693 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
694 "navs", KATTR__MAX);
695 if (kerr != KCGI_OK)
696 goto done;
698 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
699 href_summary, KATTR__MAX);
700 if (kerr != KCGI_OK)
701 goto done;
702 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
703 if (kerr != KCGI_OK)
704 goto done;
705 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
706 if (kerr != KCGI_OK)
707 goto done;
709 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
710 if (kerr != KCGI_OK)
711 goto done;
713 if (asprintf(&href_briefs, "?path=%s&action=briefs",
714 gw_dir->name) == -1) {
715 error = got_error_from_errno("asprintf");
716 goto done;
718 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
719 href_briefs, KATTR__MAX);
720 if (kerr != KCGI_OK)
721 goto done;
722 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
723 if (kerr != KCGI_OK)
724 goto done;
725 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
726 if (kerr != KCGI_OK)
727 error = gw_kcgi_error(kerr);
729 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
730 if (kerr != KCGI_OK)
731 goto done;
733 if (asprintf(&href_commits, "?path=%s&action=commits",
734 gw_dir->name) == -1) {
735 error = got_error_from_errno("asprintf");
736 goto done;
738 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
739 href_commits, KATTR__MAX);
740 if (kerr != KCGI_OK)
741 goto done;
742 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
743 if (kerr != KCGI_OK)
744 goto done;
745 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
746 if (kerr != KCGI_OK)
747 goto done;
749 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
750 if (kerr != KCGI_OK)
751 goto done;
753 if (asprintf(&href_tree, "?path=%s&action=tree",
754 gw_dir->name) == -1) {
755 error = got_error_from_errno("asprintf");
756 goto done;
758 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
759 href_tree, KATTR__MAX);
760 if (kerr != KCGI_OK)
761 goto done;
762 kerr = khtml_puts(gw_trans->gw_html_req, "tree");
763 if (kerr != KCGI_OK)
764 goto done;
766 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
767 if (kerr != KCGI_OK)
768 goto done;
769 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
770 "dotted_line", KATTR__MAX);
771 if (kerr != KCGI_OK)
772 goto done;
773 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
774 if (kerr != KCGI_OK)
775 goto done;
777 free(href_summary);
778 href_summary = NULL;
779 free(href_briefs);
780 href_briefs = NULL;
781 free(href_commits);
782 href_commits = NULL;
783 free(href_tree);
784 href_tree = NULL;
786 if (gw_trans->gw_conf->got_max_repos_display == 0)
787 continue;
789 if (next_disp == gw_trans->gw_conf->got_max_repos_display) {
790 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
791 KATTR_ID, "np_wrapper", KATTR__MAX);
792 if (kerr != KCGI_OK)
793 goto done;
794 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
795 KATTR_ID, "nav_prev", KATTR__MAX);
796 if (kerr != KCGI_OK)
797 goto done;
798 } else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
799 (gw_trans->page > 0) &&
800 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
801 prev_disp == gw_trans->repos_total)) {
802 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
803 KATTR_ID, "np_wrapper", KATTR__MAX);
804 if (kerr != KCGI_OK)
805 goto done;
806 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
807 KATTR_ID, "nav_prev", KATTR__MAX);
808 if (kerr != KCGI_OK)
809 goto done;
812 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
813 (gw_trans->page > 0) &&
814 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
815 prev_disp == gw_trans->repos_total)) {
816 if (asprintf(&href_prev, "?page=%d",
817 gw_trans->page - 1) == -1) {
818 error = got_error_from_errno("asprintf");
819 goto done;
821 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
822 KATTR_HREF, href_prev, KATTR__MAX);
823 if (kerr != KCGI_OK)
824 goto done;
825 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
826 if (kerr != KCGI_OK)
827 goto done;
828 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
829 if (kerr != KCGI_OK)
830 goto done;
833 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
834 if (kerr != KCGI_OK)
835 return gw_kcgi_error(kerr);
837 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
838 next_disp == gw_trans->gw_conf->got_max_repos_display &&
839 dir_c != (gw_trans->page + 1) *
840 gw_trans->gw_conf->got_max_repos_display) {
841 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
842 KATTR_ID, "nav_next", KATTR__MAX);
843 if (kerr != KCGI_OK)
844 goto done;
845 if (asprintf(&href_next, "?page=%d",
846 gw_trans->page + 1) == -1) {
847 error = got_error_from_errno("calloc");
848 goto done;
850 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
851 KATTR_HREF, href_next, KATTR__MAX);
852 if (kerr != KCGI_OK)
853 goto done;
854 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
855 if (kerr != KCGI_OK)
856 goto done;
857 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
858 if (kerr != KCGI_OK)
859 goto done;
860 next_disp = 0;
861 break;
864 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
865 (gw_trans->page > 0) &&
866 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
867 prev_disp == gw_trans->repos_total)) {
868 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
869 if (kerr != KCGI_OK)
870 goto done;
872 next_disp++;
874 done:
875 free(href_prev);
876 free(href_next);
877 free(href_summary);
878 free(href_briefs);
879 free(href_commits);
880 free(href_tree);
881 if (error == NULL && kerr != KCGI_OK)
882 error = gw_kcgi_error(kerr);
883 return error;
886 static const struct got_error *
887 gw_commits(struct gw_trans *gw_trans)
889 const struct got_error *error = NULL;
890 struct gw_header *header = NULL, *n_header = NULL;
891 char *age = NULL;
892 char *href_diff = NULL, *href_blob = NULL;
893 enum kcgi_err kerr = KCGI_OK;
895 if ((header = gw_init_header()) == NULL)
896 return got_error_from_errno("malloc");
898 if (pledge("stdio rpath proc exec sendfd unveil",
899 NULL) == -1) {
900 error = got_error_from_errno("pledge");
901 goto done;
904 error = gw_apply_unveil(gw_trans->gw_dir->path);
905 if (error)
906 goto done;
908 error = gw_get_header(gw_trans, header,
909 gw_trans->gw_conf->got_max_commits_display);
910 if (error)
911 goto done;
913 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
914 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
915 "commits_line_wrapper", KATTR__MAX);
916 if (kerr != KCGI_OK)
917 goto done;
918 error = gw_gen_commit_header(gw_trans, n_header->commit_id,
919 n_header->refs_str);
920 if (error)
921 goto done;
922 error = gw_gen_author_header(gw_trans, n_header->author);
923 if (error)
924 goto done;
925 error = gw_gen_committer_header(gw_trans, n_header->author);
926 if (error)
927 goto done;
928 error = gw_get_time_str(&age, n_header->committer_time,
929 TM_LONG);
930 if (error)
931 goto done;
932 error = gw_gen_age_header(gw_trans, age ?age : "");
933 if (error)
934 goto done;
935 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
936 if (kerr != KCGI_OK)
937 goto done;
939 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
940 "dotted_line", KATTR__MAX);
941 if (kerr != KCGI_OK)
942 goto done;
943 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
944 if (kerr != KCGI_OK)
945 goto done;
947 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
948 "commit", KATTR__MAX);
949 if (kerr != KCGI_OK)
950 goto done;
951 kerr = khttp_puts(gw_trans->gw_req, n_header->commit_msg);
952 if (kerr != KCGI_OK)
953 goto done;
954 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
955 if (kerr != KCGI_OK)
956 goto done;
958 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
959 gw_trans->repo_name, n_header->commit_id) == -1) {
960 error = got_error_from_errno("asprintf");
961 goto done;
963 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
964 KATTR_ID, "navs_wrapper", KATTR__MAX);
965 if (kerr != KCGI_OK)
966 goto done;
967 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
968 KATTR_ID, "navs", KATTR__MAX);
969 if (kerr != KCGI_OK)
970 goto done;
971 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
972 KATTR_HREF, href_diff, KATTR__MAX);
973 if (kerr != KCGI_OK)
974 goto done;
975 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
976 if (kerr != KCGI_OK)
977 goto done;
978 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
979 if (kerr != KCGI_OK)
980 goto done;
982 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
983 if (kerr != KCGI_OK)
984 goto done;
986 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
987 gw_trans->repo_name, n_header->commit_id) == -1) {
988 error = got_error_from_errno("asprintf");
989 goto done;
991 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
992 KATTR_HREF, href_blob, KATTR__MAX);
993 if (kerr != KCGI_OK)
994 goto done;
995 khtml_puts(gw_trans->gw_html_req, "tree");
996 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
997 if (kerr != KCGI_OK)
998 goto done;
999 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1000 if (kerr != KCGI_OK)
1001 goto done;
1003 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1004 "solid_line", KATTR__MAX);
1005 if (kerr != KCGI_OK)
1006 goto done;
1007 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1008 if (kerr != KCGI_OK)
1009 goto done;
1011 free(age);
1012 age = NULL;
1014 done:
1015 got_ref_list_free(&header->refs);
1016 gw_free_header(header);
1017 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1018 gw_free_header(n_header);
1019 free(age);
1020 free(href_diff);
1021 free(href_blob);
1022 if (error == NULL && kerr != KCGI_OK)
1023 error = gw_kcgi_error(kerr);
1024 return error;
1027 static const struct got_error *
1028 gw_briefs(struct gw_trans *gw_trans)
1030 const struct got_error *error = NULL;
1031 struct gw_header *header = NULL, *n_header = NULL;
1032 char *age = NULL;
1033 char *href_diff = NULL, *href_blob = NULL;
1034 char *newline, *smallerthan;
1035 enum kcgi_err kerr = KCGI_OK;
1037 if ((header = gw_init_header()) == NULL)
1038 return got_error_from_errno("malloc");
1040 if (pledge("stdio rpath proc exec sendfd unveil",
1041 NULL) == -1) {
1042 error = got_error_from_errno("pledge");
1043 goto done;
1046 error = gw_apply_unveil(gw_trans->gw_dir->path);
1047 if (error)
1048 goto done;
1050 if (gw_trans->action == GW_SUMMARY)
1051 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
1052 else
1053 error = gw_get_header(gw_trans, header,
1054 gw_trans->gw_conf->got_max_commits_display);
1055 if (error)
1056 goto done;
1058 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
1059 error = gw_get_time_str(&age, n_header->committer_time,
1060 TM_DIFF);
1061 if (error)
1062 goto done;
1064 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1065 KATTR_ID, "briefs_wrapper", KATTR__MAX);
1066 if (kerr != KCGI_OK)
1067 goto done;
1069 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1070 KATTR_ID, "briefs_age", KATTR__MAX);
1071 if (kerr != KCGI_OK)
1072 goto done;
1073 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
1074 if (kerr != KCGI_OK)
1075 goto done;
1076 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1077 if (kerr != KCGI_OK)
1078 goto done;
1080 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1081 KATTR_ID, "briefs_author", KATTR__MAX);
1082 if (kerr != KCGI_OK)
1083 goto done;
1084 smallerthan = strchr(n_header->author, '<');
1085 if (smallerthan)
1086 *smallerthan = '\0';
1087 kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
1088 if (kerr != KCGI_OK)
1089 goto done;
1090 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1091 if (kerr != KCGI_OK)
1092 goto done;
1094 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
1095 gw_trans->repo_name, n_header->commit_id) == -1) {
1096 error = got_error_from_errno("asprintf");
1097 goto done;
1099 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1100 KATTR_ID, "briefs_log", KATTR__MAX);
1101 if (kerr != KCGI_OK)
1102 goto done;
1103 newline = strchr(n_header->commit_msg, '\n');
1104 if (newline)
1105 *newline = '\0';
1106 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1107 KATTR_HREF, href_diff, KATTR__MAX);
1108 if (kerr != KCGI_OK)
1109 goto done;
1110 kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
1111 if (kerr != KCGI_OK)
1112 goto done;
1113 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1114 if (kerr != KCGI_OK)
1115 goto done;
1117 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1118 KATTR_ID, "navs_wrapper", KATTR__MAX);
1119 if (kerr != KCGI_OK)
1120 goto done;
1121 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1122 KATTR_ID, "navs", KATTR__MAX);
1123 if (kerr != KCGI_OK)
1124 goto done;
1125 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1126 KATTR_HREF, href_diff, KATTR__MAX);
1127 if (kerr != KCGI_OK)
1128 goto done;
1129 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1130 if (kerr != KCGI_OK)
1131 goto done;
1132 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1133 if (kerr != KCGI_OK)
1134 goto done;
1136 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1137 if (kerr != KCGI_OK)
1138 goto done;
1140 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
1141 gw_trans->repo_name, n_header->commit_id) == -1) {
1142 error = got_error_from_errno("asprintf");
1143 goto done;
1145 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1146 KATTR_HREF, href_blob, KATTR__MAX);
1147 if (kerr != KCGI_OK)
1148 goto done;
1149 khtml_puts(gw_trans->gw_html_req, "tree");
1150 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1151 if (kerr != KCGI_OK)
1152 goto done;
1153 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1154 if (kerr != KCGI_OK)
1155 goto done;
1157 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1158 KATTR_ID, "dotted_line", KATTR__MAX);
1159 if (kerr != KCGI_OK)
1160 goto done;
1161 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1162 if (kerr != KCGI_OK)
1163 goto done;
1165 free(age);
1166 age = NULL;
1167 free(href_diff);
1168 href_diff = NULL;
1169 free(href_blob);
1170 href_blob = NULL;
1172 done:
1173 got_ref_list_free(&header->refs);
1174 gw_free_header(header);
1175 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1176 gw_free_header(n_header);
1177 free(age);
1178 free(href_diff);
1179 free(href_blob);
1180 if (error == NULL && kerr != KCGI_OK)
1181 error = gw_kcgi_error(kerr);
1182 return error;
1185 static const struct got_error *
1186 gw_summary(struct gw_trans *gw_trans)
1188 const struct got_error *error = NULL;
1189 char *age = NULL;
1190 enum kcgi_err kerr = KCGI_OK;
1192 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1193 return got_error_from_errno("pledge");
1195 /* unveil is applied with gw_briefs below */
1197 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1198 "summary_wrapper", KATTR__MAX);
1199 if (kerr != KCGI_OK)
1200 return gw_kcgi_error(kerr);
1202 if (gw_trans->gw_conf->got_show_repo_description &&
1203 gw_trans->gw_dir->description != NULL &&
1204 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1205 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1206 KATTR_ID, "description_title", KATTR__MAX);
1207 if (kerr != KCGI_OK)
1208 goto done;
1209 kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1210 if (kerr != KCGI_OK)
1211 goto done;
1212 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1213 if (kerr != KCGI_OK)
1214 goto done;
1215 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1216 KATTR_ID, "description", KATTR__MAX);
1217 if (kerr != KCGI_OK)
1218 goto done;
1219 kerr = khtml_puts(gw_trans->gw_html_req,
1220 gw_trans->gw_dir->description);
1221 if (kerr != KCGI_OK)
1222 goto done;
1223 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1224 if (kerr != KCGI_OK)
1225 goto done;
1228 if (gw_trans->gw_conf->got_show_repo_owner &&
1229 gw_trans->gw_dir->owner != NULL &&
1230 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
1231 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1232 KATTR_ID, "repo_owner_title", KATTR__MAX);
1233 if (kerr != KCGI_OK)
1234 goto done;
1235 kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1236 if (kerr != KCGI_OK)
1237 goto done;
1238 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1239 if (kerr != KCGI_OK)
1240 goto done;
1241 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1242 KATTR_ID, "repo_owner", KATTR__MAX);
1243 if (kerr != KCGI_OK)
1244 goto done;
1245 kerr = khtml_puts(gw_trans->gw_html_req,
1246 gw_trans->gw_dir->owner);
1247 if (kerr != KCGI_OK)
1248 goto done;
1249 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1250 if (kerr != KCGI_OK)
1251 goto done;
1254 if (gw_trans->gw_conf->got_show_repo_age) {
1255 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1256 NULL, TM_LONG);
1257 if (error)
1258 goto done;
1259 if (age != NULL) {
1260 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1261 KATTR_ID, "last_change_title", KATTR__MAX);
1262 if (kerr != KCGI_OK)
1263 goto done;
1264 kerr = khtml_puts(gw_trans->gw_html_req,
1265 "Last Change: ");
1266 if (kerr != KCGI_OK)
1267 goto done;
1268 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1269 if (kerr != KCGI_OK)
1270 goto done;
1271 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1272 KATTR_ID, "last_change", KATTR__MAX);
1273 if (kerr != KCGI_OK)
1274 goto done;
1275 kerr = khtml_puts(gw_trans->gw_html_req, age);
1276 if (kerr != KCGI_OK)
1277 goto done;
1278 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1279 if (kerr != KCGI_OK)
1280 goto done;
1284 if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1285 gw_trans->gw_dir->url != NULL &&
1286 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1287 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1288 KATTR_ID, "cloneurl_title", KATTR__MAX);
1289 if (kerr != KCGI_OK)
1290 goto done;
1291 kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1292 if (kerr != KCGI_OK)
1293 goto done;
1294 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1295 if (kerr != KCGI_OK)
1296 goto done;
1297 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1298 KATTR_ID, "cloneurl", KATTR__MAX);
1299 if (kerr != KCGI_OK)
1300 goto done;
1301 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1302 if (kerr != KCGI_OK)
1303 goto done;
1304 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1305 if (kerr != KCGI_OK)
1306 goto done;
1309 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1310 if (kerr != KCGI_OK)
1311 goto done;
1313 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1314 "briefs_title_wrapper", KATTR__MAX);
1315 if (kerr != KCGI_OK)
1316 goto done;
1317 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1318 "briefs_title", KATTR__MAX);
1319 if (kerr != KCGI_OK)
1320 goto done;
1321 kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1322 if (kerr != KCGI_OK)
1323 goto done;
1324 if (gw_trans->headref) {
1325 kerr = khtml_puts(gw_trans->gw_html_req, " (");
1326 if (kerr != KCGI_OK)
1327 goto done;
1328 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->headref);
1329 if (kerr != KCGI_OK)
1330 goto done;
1331 kerr = khtml_puts(gw_trans->gw_html_req, ")");
1332 if (kerr != KCGI_OK)
1333 goto done;
1335 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1336 if (kerr != KCGI_OK)
1337 goto done;
1338 error = gw_briefs(gw_trans);
1339 if (error)
1340 goto done;
1342 error = gw_output_repo_tags(gw_trans, NULL, D_MAXSLCOMMDISP,
1343 TAGBRIEF);
1344 if (error)
1345 goto done;
1347 error = gw_output_repo_heads(gw_trans);
1348 done:
1349 free(age);
1350 if (error == NULL && kerr != KCGI_OK)
1351 error = gw_kcgi_error(kerr);
1352 return error;
1355 static const struct got_error *
1356 gw_tree(struct gw_trans *gw_trans)
1358 const struct got_error *error = NULL;
1359 struct gw_header *header = NULL;
1360 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1361 char *age = NULL;
1362 enum kcgi_err kerr;
1364 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1365 return got_error_from_errno("pledge");
1367 if ((header = gw_init_header()) == NULL)
1368 return got_error_from_errno("malloc");
1370 error = gw_apply_unveil(gw_trans->gw_dir->path);
1371 if (error)
1372 goto done;
1374 error = gw_get_header(gw_trans, header, 1);
1375 if (error)
1376 goto done;
1378 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1379 "tree_header_wrapper", KATTR__MAX);
1380 if (kerr != KCGI_OK)
1381 goto done;
1382 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1383 "tree_header", KATTR__MAX);
1384 if (kerr != KCGI_OK)
1385 goto done;
1386 error = gw_gen_tree_header(gw_trans, header->tree_id);
1387 if (error)
1388 goto done;
1389 error = gw_get_time_str(&age, header->committer_time,
1390 TM_LONG);
1391 if (error)
1392 goto done;
1393 error = gw_gen_age_header(gw_trans, age ?age : "");
1394 if (error)
1395 goto done;
1396 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1397 if (error)
1398 goto done;
1399 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1400 if (kerr != KCGI_OK)
1401 goto done;
1402 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1403 "dotted_line", KATTR__MAX);
1404 if (kerr != KCGI_OK)
1405 goto done;
1406 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1407 if (kerr != KCGI_OK)
1408 goto done;
1410 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1411 "tree", KATTR__MAX);
1412 if (kerr != KCGI_OK)
1413 goto done;
1414 error = gw_output_repo_tree(gw_trans);
1415 if (error)
1416 goto done;
1418 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1419 done:
1420 got_ref_list_free(&header->refs);
1421 gw_free_header(header);
1422 free(tree_html_disp);
1423 free(tree_html);
1424 free(tree);
1425 free(age);
1426 if (error == NULL && kerr != KCGI_OK)
1427 error = gw_kcgi_error(kerr);
1428 return error;
1431 static const struct got_error *
1432 gw_tag(struct gw_trans *gw_trans)
1434 const struct got_error *error = NULL;
1435 struct gw_header *header = NULL;
1436 enum kcgi_err kerr;
1438 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1439 return got_error_from_errno("pledge");
1441 if ((header = gw_init_header()) == NULL)
1442 return got_error_from_errno("malloc");
1444 error = gw_apply_unveil(gw_trans->gw_dir->path);
1445 if (error)
1446 goto done;
1448 error = gw_get_header(gw_trans, header, 1);
1449 if (error)
1450 goto done;
1452 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1453 "tag_header_wrapper", KATTR__MAX);
1454 if (kerr != KCGI_OK)
1455 goto done;
1456 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1457 "tag_header", KATTR__MAX);
1458 if (kerr != KCGI_OK)
1459 goto done;
1460 error = gw_gen_commit_header(gw_trans, header->commit_id,
1461 header->refs_str);
1462 if (error)
1463 goto done;
1464 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1465 if (error)
1466 goto done;
1467 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1468 if (kerr != KCGI_OK)
1469 goto done;
1470 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1471 "dotted_line", KATTR__MAX);
1472 if (kerr != KCGI_OK)
1473 goto done;
1474 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1475 if (kerr != KCGI_OK)
1476 goto done;
1478 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1479 "tree", KATTR__MAX);
1480 if (kerr != KCGI_OK)
1481 goto done;
1483 error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1484 if (error)
1485 goto done;
1487 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1488 done:
1489 got_ref_list_free(&header->refs);
1490 gw_free_header(header);
1491 if (error == NULL && kerr != KCGI_OK)
1492 error = gw_kcgi_error(kerr);
1493 return error;
1496 static const struct got_error *
1497 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1499 const struct got_error *error = NULL;
1500 DIR *dt;
1501 char *dir_test;
1502 int opened = 0;
1504 if (asprintf(&dir_test, "%s/%s/%s",
1505 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1506 GOTWEB_GIT_DIR) == -1)
1507 return got_error_from_errno("asprintf");
1509 dt = opendir(dir_test);
1510 if (dt == NULL) {
1511 free(dir_test);
1512 } else {
1513 gw_dir->path = strdup(dir_test);
1514 if (gw_dir->path == NULL) {
1515 opened = 1;
1516 error = got_error_from_errno("strdup");
1517 goto errored;
1519 opened = 1;
1520 goto done;
1523 if (asprintf(&dir_test, "%s/%s/%s",
1524 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1525 GOTWEB_GOT_DIR) == -1) {
1526 dir_test = NULL;
1527 error = got_error_from_errno("asprintf");
1528 goto errored;
1531 dt = opendir(dir_test);
1532 if (dt == NULL)
1533 free(dir_test);
1534 else {
1535 opened = 1;
1536 error = got_error(GOT_ERR_NOT_GIT_REPO);
1537 goto errored;
1540 if (asprintf(&dir_test, "%s/%s",
1541 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1) {
1542 error = got_error_from_errno("asprintf");
1543 dir_test = NULL;
1544 goto errored;
1547 gw_dir->path = strdup(dir_test);
1548 if (gw_dir->path == NULL) {
1549 opened = 1;
1550 error = got_error_from_errno("strdup");
1551 goto errored;
1554 dt = opendir(dir_test);
1555 if (dt == NULL) {
1556 error = got_error_from_errno2("bad path", dir_test);
1557 goto errored;
1558 } else
1559 opened = 1;
1560 done:
1561 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1562 gw_dir->path);
1563 if (error)
1564 goto errored;
1565 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1566 if (error)
1567 goto errored;
1568 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1569 NULL, TM_DIFF);
1570 if (error)
1571 goto errored;
1572 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1573 errored:
1574 free(dir_test);
1575 if (opened)
1576 closedir(dt);
1577 return error;
1580 static const struct got_error *
1581 gw_load_got_paths(struct gw_trans *gw_trans)
1583 const struct got_error *error = NULL;
1584 DIR *d;
1585 struct dirent **sd_dent;
1586 struct gw_dir *gw_dir;
1587 struct stat st;
1588 unsigned int d_cnt, d_i;
1590 d = opendir(gw_trans->gw_conf->got_repos_path);
1591 if (d == NULL) {
1592 error = got_error_from_errno2("opendir",
1593 gw_trans->gw_conf->got_repos_path);
1594 return error;
1597 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1598 alphasort);
1599 if (d_cnt == -1) {
1600 error = got_error_from_errno2("scandir",
1601 gw_trans->gw_conf->got_repos_path);
1602 return error;
1605 for (d_i = 0; d_i < d_cnt; d_i++) {
1606 if (gw_trans->gw_conf->got_max_repos > 0 &&
1607 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1608 break; /* account for parent and self */
1610 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1611 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1612 continue;
1614 error = gw_init_gw_dir(&gw_dir, sd_dent[d_i]->d_name);
1615 if (error)
1616 return error;
1618 error = gw_load_got_path(gw_trans, gw_dir);
1619 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1620 error = NULL;
1621 continue;
1623 else if (error)
1624 return error;
1626 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1627 !got_path_dir_is_empty(gw_dir->path)) {
1628 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1629 entry);
1630 gw_trans->repos_total++;
1634 closedir(d);
1635 return error;
1638 static const struct got_error *
1639 gw_parse_querystring(struct gw_trans *gw_trans)
1641 const struct got_error *error = NULL;
1642 struct kpair *p;
1643 struct gw_query_action *action = NULL;
1644 unsigned int i;
1646 if (gw_trans->gw_req->fieldnmap[0]) {
1647 error = got_error_from_errno("bad parse");
1648 return error;
1649 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1650 /* define gw_trans->repo_path */
1651 gw_trans->repo_name = p->parsed.s;
1653 if (asprintf(&gw_trans->repo_path, "%s/%s",
1654 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
1655 return got_error_from_errno("asprintf");
1657 /* get action and set function */
1658 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION])) {
1659 for (i = 0; i < nitems(gw_query_funcs); i++) {
1660 action = &gw_query_funcs[i];
1661 if (action->func_name == NULL)
1662 continue;
1663 if (strcmp(action->func_name,
1664 p->parsed.s) == 0) {
1665 gw_trans->action = i;
1666 break;
1670 if (gw_trans->action == -1) {
1671 gw_trans->action = GW_ERR;
1672 gw_trans->error = got_error_from_errno("bad action");
1673 return error;
1676 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID])) {
1677 if (asprintf(&gw_trans->commit, "%s",
1678 p->parsed.s) == -1)
1679 return got_error_from_errno("asprintf");
1682 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1683 gw_trans->repo_file = p->parsed.s;
1685 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER])) {
1686 if (asprintf(&gw_trans->repo_folder, "%s",
1687 p->parsed.s) == -1)
1688 return got_error_from_errno("asprintf");
1691 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
1692 gw_trans->headref = p->parsed.s;
1694 error = gw_init_gw_dir(&gw_trans->gw_dir, gw_trans->repo_name);
1695 if (error)
1696 return error;
1698 gw_trans->error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1699 } else
1700 gw_trans->action = GW_INDEX;
1702 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1703 gw_trans->page = p->parsed.i;
1705 return error;
1708 static const struct got_error *
1709 gw_init_gw_dir(struct gw_dir **gw_dir, const char *dir)
1711 const struct got_error *error;
1713 *gw_dir = malloc(sizeof(**gw_dir));
1714 if (*gw_dir == NULL)
1715 return got_error_from_errno("malloc");
1717 if (asprintf(&(*gw_dir)->name, "%s", dir) == -1) {
1718 error = got_error_from_errno("asprintf");
1719 free(*gw_dir);
1720 *gw_dir = NULL;
1721 return NULL;
1724 return NULL;
1727 static const struct got_error *
1728 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1730 enum kcgi_err kerr;
1732 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1733 if (kerr != KCGI_OK)
1734 return gw_kcgi_error(kerr);
1735 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1736 khttps[code]);
1737 if (kerr != KCGI_OK)
1738 return gw_kcgi_error(kerr);
1739 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1740 kmimetypes[mime]);
1741 if (kerr != KCGI_OK)
1742 return gw_kcgi_error(kerr);
1743 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
1744 "nosniff");
1745 if (kerr != KCGI_OK)
1746 return gw_kcgi_error(kerr);
1747 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1748 if (kerr != KCGI_OK)
1749 return gw_kcgi_error(kerr);
1750 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
1751 "1; mode=block");
1752 if (kerr != KCGI_OK)
1753 return gw_kcgi_error(kerr);
1755 /* XXX repo_file could be NULL if not present in querystring */
1756 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
1757 kerr = khttp_head(gw_trans->gw_req,
1758 kresps[KRESP_CONTENT_DISPOSITION],
1759 "attachment; filename=%s", gw_trans->repo_file);
1760 if (kerr != KCGI_OK)
1761 return gw_kcgi_error(kerr);
1764 kerr = khttp_body(gw_trans->gw_req);
1765 return gw_kcgi_error(kerr);
1768 static const struct got_error *
1769 gw_display_index(struct gw_trans *gw_trans)
1771 const struct got_error *error;
1772 enum kcgi_err kerr;
1774 /* catch early querystring errors */
1775 if (gw_trans->error)
1776 gw_trans->action = GW_ERR;
1778 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1779 if (error)
1780 return error;
1782 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1783 if (kerr != KCGI_OK)
1784 return gw_kcgi_error(kerr);
1786 if (gw_trans->action != GW_BLOB) {
1787 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1788 gw_query_funcs[gw_trans->action].template);
1789 if (kerr != KCGI_OK) {
1790 khtml_close(gw_trans->gw_html_req);
1791 return gw_kcgi_error(kerr);
1795 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
1798 static const struct got_error *
1799 gw_error(struct gw_trans *gw_trans)
1801 enum kcgi_err kerr;
1803 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->error->msg);
1805 return gw_kcgi_error(kerr);
1808 static int
1809 gw_template(size_t key, void *arg)
1811 const struct got_error *error = NULL;
1812 enum kcgi_err kerr;
1813 struct gw_trans *gw_trans = arg;
1814 char *img_src = NULL;
1816 switch (key) {
1817 case (TEMPL_HEAD):
1818 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1819 KATTR_NAME, "viewport",
1820 KATTR_CONTENT, "initial-scale=.75, user-scalable=yes",
1821 KATTR__MAX);
1822 if (kerr != KCGI_OK)
1823 return 0;
1824 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1825 if (kerr != KCGI_OK)
1826 return 0;
1827 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1828 KATTR_CHARSET, "utf-8",
1829 KATTR__MAX);
1830 if (kerr != KCGI_OK)
1831 return 0;
1832 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1833 if (kerr != KCGI_OK)
1834 return 0;
1835 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1836 KATTR_NAME, "msapplication-TileColor",
1837 KATTR_CONTENT, "#da532c", KATTR__MAX);
1838 if (kerr != KCGI_OK)
1839 return 0;
1840 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1841 if (kerr != KCGI_OK)
1842 return 0;
1843 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1844 KATTR_NAME, "theme-color",
1845 KATTR_CONTENT, "#ffffff", KATTR__MAX);
1846 if (kerr != KCGI_OK)
1847 return 0;
1848 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1849 if (kerr != KCGI_OK)
1850 return 0;
1851 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1852 KATTR_REL, "apple-touch-icon", KATTR_SIZES, "180x180",
1853 KATTR_HREF, "/apple-touch-icon.png", KATTR__MAX);
1854 if (kerr != KCGI_OK)
1855 return 0;
1856 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1857 if (kerr != KCGI_OK)
1858 return 0;
1859 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1860 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
1861 "32x32", KATTR_HREF, "/favicon-32x32.png", KATTR__MAX);
1862 if (kerr != KCGI_OK)
1863 return 0;
1864 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1865 if (kerr != KCGI_OK)
1866 return 0;
1867 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1868 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
1869 "16x16", KATTR_HREF, "/favicon-16x16.png", KATTR__MAX);
1870 if (kerr != KCGI_OK)
1871 return 0;
1872 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1873 if (kerr != KCGI_OK)
1874 return 0;
1875 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1876 KATTR_REL, "manifest", KATTR_HREF, "/site.webmanifest",
1877 KATTR__MAX);
1878 if (kerr != KCGI_OK)
1879 return 0;
1880 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1881 if (kerr != KCGI_OK)
1882 return 0;
1883 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1884 KATTR_REL, "mask-icon", KATTR_HREF,
1885 "/safari-pinned-tab.svg", KATTR__MAX);
1886 if (kerr != KCGI_OK)
1887 return 0;
1888 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1889 if (kerr != KCGI_OK)
1890 return 0;
1891 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1892 KATTR_REL, "stylesheet", KATTR_TYPE, "text/css",
1893 KATTR_HREF, "/gotweb.css", KATTR__MAX);
1894 if (kerr != KCGI_OK)
1895 return 0;
1896 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1897 if (kerr != KCGI_OK)
1898 return 0;
1899 break;
1900 case(TEMPL_HEADER):
1901 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1902 KATTR_ID, "got_link", KATTR__MAX);
1903 if (kerr != KCGI_OK)
1904 return 0;
1905 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1906 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
1907 KATTR_TARGET, "_sotd", KATTR__MAX);
1908 if (kerr != KCGI_OK)
1909 return 0;
1910 if (asprintf(&img_src, "/%s",
1911 gw_trans->gw_conf->got_logo) == -1)
1912 return 0;
1913 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
1914 KATTR_SRC, img_src, KATTR__MAX);
1915 if (kerr != KCGI_OK) {
1916 free(img_src);
1917 return 0;
1919 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1920 if (kerr != KCGI_OK) {
1921 free(img_src);
1922 return 0;
1924 break;
1925 case (TEMPL_SITEPATH):
1926 error = gw_output_site_link(gw_trans);
1927 if (error)
1928 return 0;
1929 break;
1930 case(TEMPL_TITLE):
1931 if (gw_trans->gw_conf->got_site_name != NULL) {
1932 kerr = khtml_puts(gw_trans->gw_html_req,
1933 gw_trans->gw_conf->got_site_name);
1934 if (kerr != KCGI_OK)
1935 return 0;
1937 break;
1938 case (TEMPL_SEARCH):
1939 break;
1940 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1941 "search", KATTR__MAX);
1942 if (kerr != KCGI_OK)
1943 return 0;
1944 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
1945 KATTR_METHOD, "POST", KATTR__MAX);
1946 if (kerr != KCGI_OK)
1947 return 0;
1948 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
1949 "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
1950 KATTR_MAXLENGTH, "50", KATTR__MAX);
1951 if (kerr != KCGI_OK)
1952 return 0;
1953 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
1954 KATTR__MAX);
1955 if (kerr != KCGI_OK)
1956 return 0;
1957 kerr = khtml_puts(gw_trans->gw_html_req, "Search");
1958 if (kerr != KCGI_OK)
1959 return 0;
1960 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
1961 if (kerr != KCGI_OK)
1962 return 0;
1963 break;
1964 case(TEMPL_SITEOWNER):
1965 if (gw_trans->gw_conf->got_site_owner != NULL &&
1966 gw_trans->gw_conf->got_show_site_owner) {
1967 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1968 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
1969 if (kerr != KCGI_OK)
1970 return 0;
1971 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1972 KATTR_ID, "site_owner", KATTR__MAX);
1973 if (kerr != KCGI_OK)
1974 return 0;
1975 kerr = khtml_puts(gw_trans->gw_html_req,
1976 gw_trans->gw_conf->got_site_owner);
1977 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1978 if (kerr != KCGI_OK)
1979 return 0;
1981 break;
1982 case(TEMPL_CONTENT):
1983 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1984 if (error) {
1985 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1986 KATTR_ID, "tmpl_err", KATTR__MAX);
1987 if (kerr != KCGI_OK)
1988 return 0;
1989 kerr = khttp_puts(gw_trans->gw_req, "Error: ");
1990 if (kerr != KCGI_OK)
1991 return 0;
1992 kerr = khttp_puts(gw_trans->gw_req, error->msg);
1993 if (kerr != KCGI_OK)
1994 return 0;
1995 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1996 if (kerr != KCGI_OK)
1997 return 0;
1999 break;
2000 default:
2001 return 0;
2003 return 1;
2006 static const struct got_error *
2007 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
2009 const struct got_error *error = NULL;
2010 enum kcgi_err kerr = KCGI_OK;
2012 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2013 KATTR_ID, "header_commit_title", KATTR__MAX);
2014 if (kerr != KCGI_OK)
2015 goto done;
2016 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
2017 if (kerr != KCGI_OK)
2018 goto done;
2019 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2020 if (kerr != KCGI_OK)
2021 goto done;
2022 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2023 KATTR_ID, "header_commit", KATTR__MAX);
2024 if (kerr != KCGI_OK)
2025 goto done;
2026 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2027 if (kerr != KCGI_OK)
2028 goto done;
2029 kerr = khtml_puts(gw_trans->gw_html_req, " ");
2030 if (kerr != KCGI_OK)
2031 goto done;
2032 if (str2 != NULL) {
2033 kerr = khtml_puts(gw_trans->gw_html_req, "(");
2034 if (kerr != KCGI_OK)
2035 goto done;
2036 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2037 if (kerr != KCGI_OK)
2038 goto done;
2039 kerr = khtml_puts(gw_trans->gw_html_req, ")");
2040 if (kerr != KCGI_OK)
2041 goto done;
2043 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2044 done:
2045 if (error == NULL && kerr != KCGI_OK)
2046 error = gw_kcgi_error(kerr);
2047 return error;
2050 static const struct got_error *
2051 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2053 const struct got_error *error = NULL;
2054 enum kcgi_err kerr = KCGI_OK;
2056 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2057 KATTR_ID, "header_diff_title", KATTR__MAX);
2058 if (kerr != KCGI_OK)
2059 goto done;
2060 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2061 if (kerr != KCGI_OK)
2062 goto done;
2063 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2064 if (kerr != KCGI_OK)
2065 goto done;
2066 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2067 KATTR_ID, "header_diff", KATTR__MAX);
2068 if (kerr != KCGI_OK)
2069 goto done;
2070 if (str1 != NULL) {
2071 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2072 if (kerr != KCGI_OK)
2073 goto done;
2075 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2076 if (kerr != KCGI_OK)
2077 goto done;
2078 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2079 if (kerr != KCGI_OK)
2080 goto done;
2081 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2082 done:
2083 if (error == NULL && kerr != KCGI_OK)
2084 error = gw_kcgi_error(kerr);
2085 return error;
2088 static const struct got_error *
2089 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2091 const struct got_error *error = NULL;
2092 enum kcgi_err kerr = KCGI_OK;
2094 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2095 KATTR_ID, "header_age_title", KATTR__MAX);
2096 if (kerr != KCGI_OK)
2097 goto done;
2098 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2099 if (kerr != KCGI_OK)
2100 goto done;
2101 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2102 if (kerr != KCGI_OK)
2103 goto done;
2104 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2105 KATTR_ID, "header_age", KATTR__MAX);
2106 if (kerr != KCGI_OK)
2107 goto done;
2108 kerr = khtml_puts(gw_trans->gw_html_req, str);
2109 if (kerr != KCGI_OK)
2110 goto done;
2111 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2112 done:
2113 if (error == NULL && kerr != KCGI_OK)
2114 error = gw_kcgi_error(kerr);
2115 return error;
2118 static const struct got_error *
2119 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2121 const struct got_error *error = NULL;
2122 enum kcgi_err kerr;
2124 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2125 KATTR_ID, "header_author_title", KATTR__MAX);
2126 if (kerr != KCGI_OK)
2127 goto done;
2128 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2129 if (kerr != KCGI_OK)
2130 goto done;
2131 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2132 if (kerr != KCGI_OK)
2133 goto done;
2134 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2135 KATTR_ID, "header_author", KATTR__MAX);
2136 if (kerr != KCGI_OK)
2137 goto done;
2138 kerr = khtml_puts(gw_trans->gw_html_req, str);
2139 if (kerr != KCGI_OK)
2140 goto done;
2141 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2142 done:
2143 if (error == NULL && kerr != KCGI_OK)
2144 error = gw_kcgi_error(kerr);
2145 return error;
2148 static const struct got_error *
2149 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2151 const struct got_error *error = NULL;
2152 enum kcgi_err kerr;
2154 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2155 KATTR_ID, "header_committer_title", KATTR__MAX);
2156 if (kerr != KCGI_OK)
2157 goto done;
2158 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2159 if (kerr != KCGI_OK)
2160 goto done;
2161 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2162 if (kerr != KCGI_OK)
2163 goto done;
2164 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2165 KATTR_ID, "header_committer", KATTR__MAX);
2166 if (kerr != KCGI_OK)
2167 goto done;
2168 kerr = khtml_puts(gw_trans->gw_html_req, str);
2169 if (kerr != KCGI_OK)
2170 goto done;
2171 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2172 done:
2173 if (error == NULL && kerr != KCGI_OK)
2174 error = gw_kcgi_error(kerr);
2175 return error;
2178 static const struct got_error *
2179 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2181 const struct got_error *error = NULL;
2182 enum kcgi_err kerr;
2184 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2185 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2186 if (kerr != KCGI_OK)
2187 goto done;
2188 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2189 if (kerr != KCGI_OK)
2190 goto done;
2191 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2192 if (kerr != KCGI_OK)
2193 goto done;
2194 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2195 KATTR_ID, "header_commit_msg", KATTR__MAX);
2196 if (kerr != KCGI_OK)
2197 goto done;
2198 kerr = khttp_puts(gw_trans->gw_req, str);
2199 if (kerr != KCGI_OK)
2200 goto done;
2201 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2202 done:
2203 if (error == NULL && kerr != KCGI_OK)
2204 error = gw_kcgi_error(kerr);
2205 return error;
2208 static const struct got_error *
2209 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2211 const struct got_error *error = NULL;
2212 enum kcgi_err kerr;
2214 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2215 KATTR_ID, "header_tree_title", KATTR__MAX);
2216 if (kerr != KCGI_OK)
2217 goto done;
2218 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2219 if (kerr != KCGI_OK)
2220 goto done;
2221 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2222 if (kerr != KCGI_OK)
2223 goto done;
2224 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2225 KATTR_ID, "header_tree", KATTR__MAX);
2226 if (kerr != KCGI_OK)
2227 goto done;
2228 kerr = khtml_puts(gw_trans->gw_html_req, str);
2229 if (kerr != KCGI_OK)
2230 goto done;
2231 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2232 done:
2233 if (error == NULL && kerr != KCGI_OK)
2234 error = gw_kcgi_error(kerr);
2235 return error;
2238 static const struct got_error *
2239 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2240 char *dir)
2242 const struct got_error *error = NULL;
2243 FILE *f = NULL;
2244 char *d_file = NULL;
2245 unsigned int len;
2246 size_t n;
2248 *description = NULL;
2249 if (gw_trans->gw_conf->got_show_repo_description == 0)
2250 return NULL;
2252 if (asprintf(&d_file, "%s/description", dir) == -1)
2253 return got_error_from_errno("asprintf");
2255 f = fopen(d_file, "r");
2256 if (f == NULL) {
2257 if (errno == ENOENT || errno == EACCES)
2258 return NULL;
2259 error = got_error_from_errno2("fopen", d_file);
2260 goto done;
2263 if (fseek(f, 0, SEEK_END) == -1) {
2264 error = got_ferror(f, GOT_ERR_IO);
2265 goto done;
2267 len = ftell(f);
2268 if (len == -1) {
2269 error = got_ferror(f, GOT_ERR_IO);
2270 goto done;
2272 if (fseek(f, 0, SEEK_SET) == -1) {
2273 error = got_ferror(f, GOT_ERR_IO);
2274 goto done;
2276 *description = calloc(len + 1, sizeof(**description));
2277 if (*description == NULL) {
2278 error = got_error_from_errno("calloc");
2279 goto done;
2282 n = fread(*description, 1, len, f);
2283 if (n == 0 && ferror(f))
2284 error = got_ferror(f, GOT_ERR_IO);
2285 done:
2286 if (f != NULL && fclose(f) == -1 && error == NULL)
2287 error = got_error_from_errno("fclose");
2288 free(d_file);
2289 return error;
2292 static const struct got_error *
2293 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2295 struct tm tm;
2296 time_t diff_time;
2297 char *years = "years ago", *months = "months ago";
2298 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2299 char *minutes = "minutes ago", *seconds = "seconds ago";
2300 char *now = "right now";
2301 char *s;
2302 char datebuf[29];
2304 *repo_age = NULL;
2306 switch (ref_tm) {
2307 case TM_DIFF:
2308 diff_time = time(NULL) - committer_time;
2309 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2310 if (asprintf(repo_age, "%lld %s",
2311 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2312 return got_error_from_errno("asprintf");
2313 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2314 if (asprintf(repo_age, "%lld %s",
2315 (diff_time / 60 / 60 / 24 / (365 / 12)),
2316 months) == -1)
2317 return got_error_from_errno("asprintf");
2318 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2319 if (asprintf(repo_age, "%lld %s",
2320 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2321 return got_error_from_errno("asprintf");
2322 } else if (diff_time > 60 * 60 * 24 * 2) {
2323 if (asprintf(repo_age, "%lld %s",
2324 (diff_time / 60 / 60 / 24), days) == -1)
2325 return got_error_from_errno("asprintf");
2326 } else if (diff_time > 60 * 60 * 2) {
2327 if (asprintf(repo_age, "%lld %s",
2328 (diff_time / 60 / 60), hours) == -1)
2329 return got_error_from_errno("asprintf");
2330 } else if (diff_time > 60 * 2) {
2331 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2332 minutes) == -1)
2333 return got_error_from_errno("asprintf");
2334 } else if (diff_time > 2) {
2335 if (asprintf(repo_age, "%lld %s", diff_time,
2336 seconds) == -1)
2337 return got_error_from_errno("asprintf");
2338 } else {
2339 if (asprintf(repo_age, "%s", now) == -1)
2340 return got_error_from_errno("asprintf");
2342 break;
2343 case TM_LONG:
2344 if (gmtime_r(&committer_time, &tm) == NULL)
2345 return got_error_from_errno("gmtime_r");
2347 s = asctime_r(&tm, datebuf);
2348 if (s == NULL)
2349 return got_error_from_errno("asctime_r");
2351 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2352 return got_error_from_errno("asprintf");
2353 break;
2355 return NULL;
2358 static const struct got_error *
2359 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2360 const char *refname, int ref_tm)
2362 const struct got_error *error = NULL;
2363 struct got_repository *repo = NULL;
2364 struct got_commit_object *commit = NULL;
2365 struct got_reflist_head refs;
2366 struct got_reflist_entry *re;
2367 time_t committer_time = 0, cmp_time = 0;
2369 *repo_age = NULL;
2370 SIMPLEQ_INIT(&refs);
2372 if (gw_trans->gw_conf->got_show_repo_age == 0)
2373 return NULL;
2375 error = got_repo_open(&repo, dir, NULL);
2376 if (error)
2377 return error;
2379 error = got_ref_list(&refs, repo, "refs/heads",
2380 got_ref_cmp_by_name, NULL);
2381 if (error)
2382 goto done;
2385 * Find the youngest branch tip in the repository, or the age of
2386 * the a specific branch tip if a name was provided by the caller.
2388 SIMPLEQ_FOREACH(re, &refs, entry) {
2389 struct got_object_id *id = NULL;
2391 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
2392 continue;
2394 error = got_ref_resolve(&id, repo, re->ref);
2395 if (error)
2396 goto done;
2398 error = got_object_open_as_commit(&commit, repo, id);
2399 free(id);
2400 if (error)
2401 goto done;
2403 committer_time =
2404 got_object_commit_get_committer_time(commit);
2405 got_object_commit_close(commit);
2406 if (cmp_time < committer_time)
2407 cmp_time = committer_time;
2409 if (refname)
2410 break;
2413 if (cmp_time != 0) {
2414 committer_time = cmp_time;
2415 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2417 done:
2418 got_ref_list_free(&refs);
2419 got_repo_close(repo);
2420 return error;
2423 static const struct got_error *
2424 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2426 const struct got_error *error;
2427 FILE *f = NULL;
2428 struct got_object_id *id1 = NULL, *id2 = NULL;
2429 char *label1 = NULL, *label2 = NULL, *line = NULL;
2430 int obj_type;
2431 size_t linesize = 0;
2432 ssize_t linelen;
2433 enum kcgi_err kerr = KCGI_OK;
2435 f = got_opentemp();
2436 if (f == NULL)
2437 return NULL;
2439 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
2440 if (error)
2441 goto done;
2443 if (header->parent_id != NULL &&
2444 strncmp(header->parent_id, "/dev/null", 9) != 0) {
2445 error = got_repo_match_object_id(&id1, &label1,
2446 header->parent_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2447 if (error)
2448 goto done;
2451 error = got_repo_match_object_id(&id2, &label2,
2452 header->commit_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2453 if (error)
2454 goto done;
2456 error = got_object_get_type(&obj_type, header->repo, id2);
2457 if (error)
2458 goto done;
2459 switch (obj_type) {
2460 case GOT_OBJ_TYPE_BLOB:
2461 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
2462 header->repo, f);
2463 break;
2464 case GOT_OBJ_TYPE_TREE:
2465 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
2466 header->repo, f);
2467 break;
2468 case GOT_OBJ_TYPE_COMMIT:
2469 error = got_diff_objects_as_commits(id1, id2, 3, 0,
2470 header->repo, f);
2471 break;
2472 default:
2473 error = got_error(GOT_ERR_OBJ_TYPE);
2475 if (error)
2476 goto done;
2478 if (fseek(f, 0, SEEK_SET) == -1) {
2479 error = got_ferror(f, GOT_ERR_IO);
2480 goto done;
2483 while ((linelen = getline(&line, &linesize, f)) != -1) {
2484 error = gw_colordiff_line(gw_trans, line);
2485 if (error)
2486 goto done;
2487 /* XXX: KHTML_PRETTY breaks this */
2488 kerr = khtml_puts(gw_trans->gw_html_req, line);
2489 if (kerr != KCGI_OK)
2490 goto done;
2491 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2492 if (kerr != KCGI_OK)
2493 goto done;
2495 if (linelen == -1 && ferror(f))
2496 error = got_error_from_errno("getline");
2497 done:
2498 if (f && fclose(f) == -1 && error == NULL)
2499 error = got_error_from_errno("fclose");
2500 free(line);
2501 free(label1);
2502 free(label2);
2503 free(id1);
2504 free(id2);
2506 if (error == NULL && kerr != KCGI_OK)
2507 error = gw_kcgi_error(kerr);
2508 return error;
2511 static const struct got_error *
2512 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2514 const struct got_error *error = NULL;
2515 struct got_repository *repo;
2516 const char *gitconfig_owner;
2518 *owner = NULL;
2520 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2521 return NULL;
2523 error = got_repo_open(&repo, dir, NULL);
2524 if (error)
2525 return error;
2526 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2527 if (gitconfig_owner) {
2528 *owner = strdup(gitconfig_owner);
2529 if (*owner == NULL)
2530 error = got_error_from_errno("strdup");
2532 got_repo_close(repo);
2533 return error;
2536 static const struct got_error *
2537 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2539 const struct got_error *error = NULL;
2540 FILE *f;
2541 char *d_file = NULL;
2542 unsigned int len;
2543 size_t n;
2545 *url = NULL;
2547 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2548 return got_error_from_errno("asprintf");
2550 f = fopen(d_file, "r");
2551 if (f == NULL) {
2552 if (errno != ENOENT && errno != EACCES)
2553 error = got_error_from_errno2("fopen", d_file);
2554 goto done;
2557 if (fseek(f, 0, SEEK_END) == -1) {
2558 error = got_ferror(f, GOT_ERR_IO);
2559 goto done;
2561 len = ftell(f);
2562 if (len == -1) {
2563 error = got_ferror(f, GOT_ERR_IO);
2564 goto done;
2566 if (fseek(f, 0, SEEK_SET) == -1) {
2567 error = got_ferror(f, GOT_ERR_IO);
2568 goto done;
2571 *url = calloc(len + 1, sizeof(**url));
2572 if (*url == NULL) {
2573 error = got_error_from_errno("calloc");
2574 goto done;
2577 n = fread(*url, 1, len, f);
2578 if (n == 0 && ferror(f))
2579 error = got_ferror(f, GOT_ERR_IO);
2580 done:
2581 if (f && fclose(f) == -1 && error == NULL)
2582 error = got_error_from_errno("fclose");
2583 free(d_file);
2584 return NULL;
2587 static const struct got_error *
2588 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
2589 int limit, int tag_type)
2591 const struct got_error *error = NULL;
2592 struct got_repository *repo = NULL;
2593 struct got_reflist_head refs;
2594 struct got_reflist_entry *re;
2595 char *age = NULL;
2596 char *id_str = NULL, *newline, *href_commits = NULL;
2597 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
2598 struct got_tag_object *tag = NULL;
2599 enum kcgi_err kerr = KCGI_OK;
2600 int summary_header_displayed = 0;
2602 SIMPLEQ_INIT(&refs);
2604 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2605 if (error)
2606 return error;
2608 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
2609 if (error)
2610 goto done;
2612 SIMPLEQ_FOREACH(re, &refs, entry) {
2613 const char *refname;
2614 const char *tagger;
2615 const char *tag_commit;
2616 time_t tagger_time;
2617 struct got_object_id *id;
2618 struct got_commit_object *commit = NULL;
2620 refname = got_ref_get_name(re->ref);
2621 if (strncmp(refname, "refs/tags/", 10) != 0)
2622 continue;
2623 refname += 10;
2625 error = got_ref_resolve(&id, repo, re->ref);
2626 if (error)
2627 goto done;
2629 error = got_object_open_as_tag(&tag, repo, id);
2630 if (error) {
2631 if (error->code != GOT_ERR_OBJ_TYPE) {
2632 free(id);
2633 goto done;
2635 /* "lightweight" tag */
2636 error = got_object_open_as_commit(&commit, repo, id);
2637 if (error) {
2638 free(id);
2639 goto done;
2641 tagger = got_object_commit_get_committer(commit);
2642 tagger_time =
2643 got_object_commit_get_committer_time(commit);
2644 error = got_object_id_str(&id_str, id);
2645 free(id);
2646 } else {
2647 free(id);
2648 tagger = got_object_tag_get_tagger(tag);
2649 tagger_time = got_object_tag_get_tagger_time(tag);
2650 error = got_object_id_str(&id_str,
2651 got_object_tag_get_object_id(tag));
2653 if (error)
2654 goto done;
2656 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
2657 strlen(id_str)) != 0)
2658 continue;
2660 if (commit) {
2661 error = got_object_commit_get_logmsg(&tag_commit0,
2662 commit);
2663 if (error)
2664 goto done;
2665 got_object_commit_close(commit);
2666 } else {
2667 tag_commit0 = strdup(got_object_tag_get_message(tag));
2668 if (tag_commit0 == NULL) {
2669 error = got_error_from_errno("strdup");
2670 goto done;
2674 tag_commit = tag_commit0;
2675 while (*tag_commit == '\n')
2676 tag_commit++;
2678 switch (tag_type) {
2679 case TAGBRIEF:
2680 newline = strchr(tag_commit, '\n');
2681 if (newline)
2682 *newline = '\0';
2684 if (summary_header_displayed == 0) {
2685 kerr = khtml_attr(gw_trans->gw_html_req,
2686 KELEM_DIV, KATTR_ID,
2687 "summary_tags_title_wrapper", KATTR__MAX);
2688 if (kerr != KCGI_OK)
2689 goto done;
2690 kerr = khtml_attr(gw_trans->gw_html_req,
2691 KELEM_DIV, KATTR_ID,
2692 "summary_tags_title", KATTR__MAX);
2693 if (kerr != KCGI_OK)
2694 goto done;
2695 kerr = khtml_puts(gw_trans->gw_html_req,
2696 "Tags");
2697 if (kerr != KCGI_OK)
2698 goto done;
2699 kerr = khtml_closeelem(gw_trans->gw_html_req,
2700 2);
2701 if (kerr != KCGI_OK)
2702 goto done;
2703 kerr = khtml_attr(gw_trans->gw_html_req,
2704 KELEM_DIV, KATTR_ID,
2705 "summary_tags_content", KATTR__MAX);
2706 if (kerr != KCGI_OK)
2707 goto done;
2708 summary_header_displayed = 1;
2711 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2712 KATTR_ID, "tags_wrapper", KATTR__MAX);
2713 if (kerr != KCGI_OK)
2714 goto done;
2715 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2716 KATTR_ID, "tags_age", KATTR__MAX);
2717 if (kerr != KCGI_OK)
2718 goto done;
2719 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
2720 if (error)
2721 goto done;
2722 kerr = khtml_puts(gw_trans->gw_html_req,
2723 age ? age : "");
2724 if (kerr != KCGI_OK)
2725 goto done;
2726 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2727 if (kerr != KCGI_OK)
2728 goto done;
2729 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2730 KATTR_ID, "tags", KATTR__MAX);
2731 if (kerr != KCGI_OK)
2732 goto done;
2733 kerr = khtml_puts(gw_trans->gw_html_req, refname);
2734 if (kerr != KCGI_OK)
2735 goto done;
2736 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2737 if (kerr != KCGI_OK)
2738 goto done;
2739 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2740 KATTR_ID, "tags_name", KATTR__MAX);
2741 if (kerr != KCGI_OK)
2742 goto done;
2743 if (asprintf(&href_tag, "?path=%s&action=tag&commit=%s",
2744 gw_trans->repo_name, id_str) == -1) {
2745 error = got_error_from_errno("asprintf");
2746 goto done;
2748 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2749 KATTR_HREF, href_tag, KATTR__MAX);
2750 if (kerr != KCGI_OK)
2751 goto done;
2752 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
2753 if (kerr != KCGI_OK)
2754 goto done;
2755 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2756 if (kerr != KCGI_OK)
2757 goto done;
2759 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2760 KATTR_ID, "navs_wrapper", KATTR__MAX);
2761 if (kerr != KCGI_OK)
2762 goto done;
2763 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2764 KATTR_ID, "navs", KATTR__MAX);
2765 if (kerr != KCGI_OK)
2766 goto done;
2768 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2769 KATTR_HREF, href_tag, KATTR__MAX);
2770 if (kerr != KCGI_OK)
2771 goto done;
2772 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
2773 if (kerr != KCGI_OK)
2774 goto done;
2775 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2776 if (kerr != KCGI_OK)
2777 goto done;
2779 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2780 if (kerr != KCGI_OK)
2781 goto done;
2782 if (asprintf(&href_briefs,
2783 "?path=%s&action=briefs&commit=%s",
2784 gw_trans->repo_name, id_str) == -1) {
2785 error = got_error_from_errno("asprintf");
2786 goto done;
2788 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2789 KATTR_HREF, href_briefs, KATTR__MAX);
2790 if (kerr != KCGI_OK)
2791 goto done;
2792 kerr = khtml_puts(gw_trans->gw_html_req,
2793 "commit briefs");
2794 if (kerr != KCGI_OK)
2795 goto done;
2796 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2797 if (kerr != KCGI_OK)
2798 goto done;
2800 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2801 if (kerr != KCGI_OK)
2802 goto done;
2804 if (asprintf(&href_commits,
2805 "?path=%s&action=commits&commit=%s",
2806 gw_trans->repo_name, id_str) == -1) {
2807 error = got_error_from_errno("asprintf");
2808 goto done;
2810 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2811 KATTR_HREF, href_commits, KATTR__MAX);
2812 if (kerr != KCGI_OK)
2813 goto done;
2814 kerr = khtml_puts(gw_trans->gw_html_req,
2815 "commits");
2816 if (kerr != KCGI_OK)
2817 goto done;
2818 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2819 if (kerr != KCGI_OK)
2820 goto done;
2822 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2823 KATTR_ID, "dotted_line", KATTR__MAX);
2824 if (kerr != KCGI_OK)
2825 goto done;
2826 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2827 if (kerr != KCGI_OK)
2828 goto done;
2829 break;
2830 case TAGFULL:
2831 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2832 KATTR_ID, "tag_info_date_title", KATTR__MAX);
2833 if (kerr != KCGI_OK)
2834 goto done;
2835 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
2836 if (kerr != KCGI_OK)
2837 goto done;
2838 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2839 if (kerr != KCGI_OK)
2840 goto done;
2841 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2842 KATTR_ID, "tag_info_date", KATTR__MAX);
2843 if (kerr != KCGI_OK)
2844 goto done;
2845 error = gw_get_time_str(&age, tagger_time, TM_LONG);
2846 if (error)
2847 goto done;
2848 kerr = khtml_puts(gw_trans->gw_html_req,
2849 age ? age : "");
2850 if (kerr != KCGI_OK)
2851 goto done;
2852 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2853 if (kerr != KCGI_OK)
2854 goto done;
2856 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2857 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
2858 if (kerr != KCGI_OK)
2859 goto done;
2860 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
2861 if (kerr != KCGI_OK)
2862 goto done;
2863 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2864 if (kerr != KCGI_OK)
2865 goto done;
2866 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2867 KATTR_ID, "tag_info_date", KATTR__MAX);
2868 if (kerr != KCGI_OK)
2869 goto done;
2870 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
2871 if (kerr != KCGI_OK)
2872 goto done;
2873 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2874 if (kerr != KCGI_OK)
2875 goto done;
2877 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2878 KATTR_ID, "tag_info", KATTR__MAX);
2879 if (kerr != KCGI_OK)
2880 goto done;
2881 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
2882 if (kerr != KCGI_OK)
2883 goto done;
2884 break;
2885 default:
2886 break;
2888 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2889 if (kerr != KCGI_OK)
2890 goto done;
2892 if (limit && --limit == 0)
2893 break;
2895 if (tag)
2896 got_object_tag_close(tag);
2897 tag = NULL;
2898 free(id_str);
2899 id_str = NULL;
2900 free(age);
2901 age = NULL;
2902 free(tag_commit0);
2903 tag_commit0 = NULL;
2904 free(href_tag);
2905 href_tag = NULL;
2906 free(href_briefs);
2907 href_briefs = NULL;
2908 free(href_commits);
2909 href_commits = NULL;
2911 done:
2912 if (tag)
2913 got_object_tag_close(tag);
2914 free(id_str);
2915 free(age);
2916 free(tag_commit0);
2917 free(href_tag);
2918 free(href_briefs);
2919 free(href_commits);
2920 got_ref_list_free(&refs);
2921 if (repo)
2922 got_repo_close(repo);
2923 if (error == NULL && kerr != KCGI_OK)
2924 error = gw_kcgi_error(kerr);
2925 return error;
2928 static void
2929 gw_free_header(struct gw_header *header)
2931 free(header->id);
2932 free(header->path);
2933 if (header->commit != NULL)
2934 got_object_commit_close(header->commit);
2935 if (header->repo)
2936 got_repo_close(header->repo);
2937 free(header->refs_str);
2938 free(header->commit_id);
2939 free(header->parent_id);
2940 free(header->tree_id);
2941 free(header->commit_msg);
2944 static struct gw_header *
2945 gw_init_header()
2947 struct gw_header *header;
2949 header = malloc(sizeof(*header));
2950 if (header == NULL)
2951 return NULL;
2953 header->repo = NULL;
2954 header->commit = NULL;
2955 header->id = NULL;
2956 header->path = NULL;
2957 SIMPLEQ_INIT(&header->refs);
2959 header->refs_str = NULL;
2960 header->commit_id = NULL;
2961 header->parent_id = NULL;
2962 header->tree_id = NULL;
2963 header->commit_msg = NULL;
2965 return header;
2968 static const struct got_error *
2969 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
2970 int limit)
2972 const struct got_error *error = NULL;
2973 struct got_commit_graph *graph = NULL;
2975 error = got_commit_graph_open(&graph, header->path, 0);
2976 if (error)
2977 return error;
2979 error = got_commit_graph_iter_start(graph, header->id, header->repo,
2980 NULL, NULL);
2981 if (error)
2982 goto done;
2984 for (;;) {
2986 * XXX This is gross; Some fields of 'header' change during every
2987 * loop iteration, some remain constant (e.g. header->repo).
2988 * We should refactor this to be able to call gw_free_header()
2989 * during every loop iteration. Or perhaps do away with the
2990 * appraoch of passing a struct around which contains data
2991 * of various lifetimes, and instead pass globals like 'repo'
2992 * around separately as done in e.g. tog(1). Any state which
2993 * keeps changing with every iteration (e.g. header->id) would
2994 * better stored in local variables of this function instead.
2996 /* Clear fields that will be filled again by gw_get_commit. */
2997 free(header->refs_str);
2998 header->refs_str = NULL;
2999 free(header->commit_id);
3000 header->commit_id = NULL;
3001 free(header->parent_id);
3002 header->parent_id = NULL;
3003 free(header->tree_id);
3004 header->tree_id = NULL;
3005 free(header->commit_msg);
3006 header->commit_msg = NULL;
3008 free(header->id); /* XXX see above comment */
3009 error = got_commit_graph_iter_next(&header->id, graph,
3010 header->repo, NULL, NULL);
3011 if (error) {
3012 if (error->code == GOT_ERR_ITER_COMPLETED)
3013 error = NULL;
3014 goto done;
3016 if (header->id == NULL)
3017 goto done;
3019 if (header->commit != NULL) /* XXX see above comment */
3020 got_object_commit_close(header->commit);
3021 error = got_object_open_as_commit(&header->commit, header->repo,
3022 header->id);
3023 if (error)
3024 goto done;
3026 error = gw_get_commit(gw_trans, header);
3027 if (limit > 1) {
3028 struct gw_header *n_header = NULL;
3029 if ((n_header = gw_init_header()) == NULL) {
3030 error = got_error_from_errno("malloc");
3031 goto done;
3034 if (header->refs_str != NULL) {
3035 n_header->refs_str = strdup(header->refs_str);
3036 if (n_header->refs_str == NULL) {
3037 error = got_error_from_errno("strdup");
3038 goto done;
3041 n_header->commit_id = strdup(header->commit_id);
3042 if (n_header->commit_id == NULL) {
3043 error = got_error_from_errno("strdup");
3044 goto done;
3046 if (header->parent_id != NULL) {
3047 n_header->parent_id = strdup(header->parent_id);
3048 if (n_header->parent_id == NULL) {
3049 error = got_error_from_errno("strdup");
3050 goto done;
3053 n_header->tree_id = strdup(header->tree_id);
3054 if (n_header->tree_id == NULL) {
3055 error = got_error_from_errno("strdup");
3056 goto done;
3058 n_header->author = header->author;
3059 n_header->committer = header->committer;
3060 n_header->commit_msg = strdup(header->commit_msg);
3061 if (n_header->commit_msg == NULL) {
3062 error = got_error_from_errno("strdup");
3063 goto done;
3065 n_header->committer_time = header->committer_time;
3066 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3067 entry);
3069 if (error || (limit && --limit == 0))
3070 break;
3072 done:
3073 if (graph)
3074 got_commit_graph_close(graph);
3075 return error;
3078 static const struct got_error *
3079 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
3081 const struct got_error *error = NULL;
3082 struct got_reflist_entry *re;
3083 struct got_object_id *id2 = NULL;
3084 struct got_object_qid *parent_id;
3085 char *commit_msg = NULL, *commit_msg0;
3087 /*print commit*/
3088 SIMPLEQ_FOREACH(re, &header->refs, entry) {
3089 char *s;
3090 const char *name;
3091 struct got_tag_object *tag = NULL;
3092 int cmp;
3094 if (got_ref_is_symbolic(re->ref))
3095 continue;
3097 name = got_ref_get_name(re->ref);
3098 if (strncmp(name, "refs/", 5) == 0)
3099 name += 5;
3100 if (strncmp(name, "got/", 4) == 0)
3101 continue;
3102 if (strncmp(name, "heads/", 6) == 0)
3103 name += 6;
3104 if (strncmp(name, "remotes/", 8) == 0)
3105 name += 8;
3106 if (strncmp(name, "tags/", 5) == 0) {
3107 error = got_object_open_as_tag(&tag, header->repo,
3108 re->id);
3109 if (error) {
3110 if (error->code != GOT_ERR_OBJ_TYPE)
3111 continue;
3113 * Ref points at something other
3114 * than a tag.
3116 error = NULL;
3117 tag = NULL;
3120 cmp = got_object_id_cmp(tag ?
3121 got_object_tag_get_object_id(tag) : re->id, header->id);
3122 if (tag)
3123 got_object_tag_close(tag);
3124 if (cmp != 0)
3125 continue;
3126 s = header->refs_str;
3127 if (asprintf(&header->refs_str, "%s%s%s", s ? s : "",
3128 s ? ", " : "", name) == -1) {
3129 error = got_error_from_errno("asprintf");
3130 free(s);
3131 header->refs_str = NULL;
3132 return error;
3134 free(s);
3137 error = got_object_id_str(&header->commit_id, header->id);
3138 if (error)
3139 return error;
3141 error = got_object_id_str(&header->tree_id,
3142 got_object_commit_get_tree_id(header->commit));
3143 if (error)
3144 return error;
3146 if (gw_trans->action == GW_DIFF) {
3147 parent_id = SIMPLEQ_FIRST(
3148 got_object_commit_get_parent_ids(header->commit));
3149 if (parent_id != NULL) {
3150 id2 = got_object_id_dup(parent_id->id);
3151 free (parent_id);
3152 error = got_object_id_str(&header->parent_id, id2);
3153 if (error)
3154 return error;
3155 free(id2);
3156 } else {
3157 header->parent_id = strdup("/dev/null");
3158 if (header->parent_id == NULL) {
3159 error = got_error_from_errno("strdup");
3160 return error;
3165 header->committer_time =
3166 got_object_commit_get_committer_time(header->commit);
3168 header->author =
3169 got_object_commit_get_author(header->commit);
3170 header->committer =
3171 got_object_commit_get_committer(header->commit);
3172 if (error)
3173 return error;
3174 error = got_object_commit_get_logmsg(&commit_msg0, header->commit);
3175 if (error)
3176 return error;
3178 commit_msg = commit_msg0;
3179 while (*commit_msg == '\n')
3180 commit_msg++;
3182 header->commit_msg = strdup(commit_msg);
3183 if (header->commit_msg == NULL)
3184 error = got_error_from_errno("strdup");
3185 free(commit_msg0);
3186 return error;
3189 static const struct got_error *
3190 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3192 const struct got_error *error = NULL;
3193 char *in_repo_path = NULL;
3195 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
3196 if (error)
3197 return error;
3199 if (gw_trans->commit == NULL) {
3200 struct got_reference *head_ref;
3201 error = got_ref_open(&head_ref, header->repo,
3202 gw_trans->headref, 0);
3203 if (error)
3204 return error;
3206 error = got_ref_resolve(&header->id, header->repo, head_ref);
3207 got_ref_close(head_ref);
3208 if (error)
3209 return error;
3211 error = got_object_open_as_commit(&header->commit,
3212 header->repo, header->id);
3213 if (error)
3214 return error;
3215 } else {
3216 struct got_reference *ref;
3217 error = got_ref_open(&ref, header->repo, gw_trans->commit, 0);
3218 if (error == NULL) {
3219 int obj_type;
3220 error = got_ref_resolve(&header->id, header->repo, ref);
3221 got_ref_close(ref);
3222 if (error)
3223 return error;
3224 error = got_object_get_type(&obj_type, header->repo,
3225 header->id);
3226 if (error)
3227 return error;
3228 if (obj_type == GOT_OBJ_TYPE_TAG) {
3229 struct got_tag_object *tag;
3230 error = got_object_open_as_tag(&tag,
3231 header->repo, header->id);
3232 if (error)
3233 return error;
3234 if (got_object_tag_get_object_type(tag) !=
3235 GOT_OBJ_TYPE_COMMIT) {
3236 got_object_tag_close(tag);
3237 error = got_error(GOT_ERR_OBJ_TYPE);
3238 return error;
3240 free(header->id);
3241 header->id = got_object_id_dup(
3242 got_object_tag_get_object_id(tag));
3243 if (header->id == NULL)
3244 error = got_error_from_errno(
3245 "got_object_id_dup");
3246 got_object_tag_close(tag);
3247 if (error)
3248 return error;
3249 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3250 error = got_error(GOT_ERR_OBJ_TYPE);
3251 return error;
3253 error = got_object_open_as_commit(&header->commit,
3254 header->repo, header->id);
3255 if (error)
3256 return error;
3258 if (header->commit == NULL) {
3259 error = got_repo_match_object_id_prefix(&header->id,
3260 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
3261 header->repo);
3262 if (error)
3263 return error;
3265 error = got_repo_match_object_id_prefix(&header->id,
3266 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
3267 header->repo);
3268 if (error)
3269 return error;
3272 error = got_repo_map_path(&in_repo_path, header->repo,
3273 gw_trans->repo_path, 1);
3274 if (error)
3275 return error;
3277 if (in_repo_path) {
3278 header->path = strdup(in_repo_path);
3279 if (header->path == NULL) {
3280 error = got_error_from_errno("strdup");
3281 free(in_repo_path);
3282 return error;
3285 free(in_repo_path);
3287 error = got_ref_list(&header->refs, header->repo, NULL,
3288 got_ref_cmp_by_name, NULL);
3289 if (error)
3290 return error;
3292 error = gw_get_commits(gw_trans, header, limit);
3293 return error;
3296 struct blame_line {
3297 int annotated;
3298 char *id_str;
3299 char *committer;
3300 char datebuf[11]; /* YYYY-MM-DD + NUL */
3303 struct gw_blame_cb_args {
3304 struct blame_line *lines;
3305 int nlines;
3306 int nlines_prec;
3307 int lineno_cur;
3308 off_t *line_offsets;
3309 FILE *f;
3310 struct got_repository *repo;
3311 struct gw_trans *gw_trans;
3314 static const struct got_error *
3315 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3317 const struct got_error *err = NULL;
3318 struct gw_blame_cb_args *a = arg;
3319 struct blame_line *bline;
3320 char *line = NULL;
3321 size_t linesize = 0;
3322 struct got_commit_object *commit = NULL;
3323 off_t offset;
3324 struct tm tm;
3325 time_t committer_time;
3326 enum kcgi_err kerr = KCGI_OK;
3328 if (nlines != a->nlines ||
3329 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3330 return got_error(GOT_ERR_RANGE);
3332 if (lineno == -1)
3333 return NULL; /* no change in this commit */
3335 /* Annotate this line. */
3336 bline = &a->lines[lineno - 1];
3337 if (bline->annotated)
3338 return NULL;
3339 err = got_object_id_str(&bline->id_str, id);
3340 if (err)
3341 return err;
3343 err = got_object_open_as_commit(&commit, a->repo, id);
3344 if (err)
3345 goto done;
3347 bline->committer = strdup(got_object_commit_get_committer(commit));
3348 if (bline->committer == NULL) {
3349 err = got_error_from_errno("strdup");
3350 goto done;
3353 committer_time = got_object_commit_get_committer_time(commit);
3354 if (localtime_r(&committer_time, &tm) == NULL)
3355 return got_error_from_errno("localtime_r");
3356 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3357 &tm) >= sizeof(bline->datebuf)) {
3358 err = got_error(GOT_ERR_NO_SPACE);
3359 goto done;
3361 bline->annotated = 1;
3363 /* Print lines annotated so far. */
3364 bline = &a->lines[a->lineno_cur - 1];
3365 if (!bline->annotated)
3366 goto done;
3368 offset = a->line_offsets[a->lineno_cur - 1];
3369 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3370 err = got_error_from_errno("fseeko");
3371 goto done;
3374 while (bline->annotated) {
3375 char *smallerthan, *at, *nl, *committer;
3376 char *lineno = NULL, *href_diff = NULL, *href_link = NULL;
3377 size_t len;
3379 if (getline(&line, &linesize, a->f) == -1) {
3380 if (ferror(a->f))
3381 err = got_error_from_errno("getline");
3382 break;
3385 committer = bline->committer;
3386 smallerthan = strchr(committer, '<');
3387 if (smallerthan && smallerthan[1] != '\0')
3388 committer = smallerthan + 1;
3389 at = strchr(committer, '@');
3390 if (at)
3391 *at = '\0';
3392 len = strlen(committer);
3393 if (len >= 9)
3394 committer[8] = '\0';
3396 nl = strchr(line, '\n');
3397 if (nl)
3398 *nl = '\0';
3400 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3401 "blame_wrapper", KATTR__MAX);
3402 if (kerr != KCGI_OK)
3403 goto err;
3404 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3405 "blame_number", KATTR__MAX);
3406 if (kerr != KCGI_OK)
3407 goto err;
3408 if (asprintf(&lineno, "%.*d", a->nlines_prec,
3409 a->lineno_cur) == -1)
3410 goto err;
3411 kerr = khtml_puts(a->gw_trans->gw_html_req, lineno);
3412 if (kerr != KCGI_OK)
3413 goto err;
3414 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3415 if (kerr != KCGI_OK)
3416 goto err;
3418 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3419 "blame_hash", KATTR__MAX);
3420 if (kerr != KCGI_OK)
3421 goto err;
3422 /* XXX repo_file could be NULL if not present in querystring */
3423 if (asprintf(&href_diff,
3424 "?path=%s&action=diff&commit=%s&file=%s&folder=%s",
3425 a->gw_trans->repo_name, bline->id_str,
3426 a->gw_trans->repo_file, a->gw_trans->repo_folder ?
3427 a->gw_trans->repo_folder : "") == -1) {
3428 err = got_error_from_errno("asprintf");
3429 goto err;
3431 if (asprintf(&href_link, "%.8s", bline->id_str) == -1) {
3432 err = got_error_from_errno("asprintf");
3433 goto err;
3435 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3436 KATTR_HREF, href_diff, KATTR__MAX);
3437 if (kerr != KCGI_OK)
3438 goto done;
3439 kerr = khtml_puts(a->gw_trans->gw_html_req, href_link);
3440 if (kerr != KCGI_OK)
3441 goto err;
3442 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3443 if (kerr != KCGI_OK)
3444 goto err;
3446 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3447 "blame_date", KATTR__MAX);
3448 if (kerr != KCGI_OK)
3449 goto err;
3450 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3451 if (kerr != KCGI_OK)
3452 goto err;
3453 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3454 if (kerr != KCGI_OK)
3455 goto err;
3457 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3458 "blame_author", KATTR__MAX);
3459 if (kerr != KCGI_OK)
3460 goto err;
3461 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
3462 if (kerr != KCGI_OK)
3463 goto err;
3464 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3465 if (kerr != KCGI_OK)
3466 goto err;
3468 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3469 "blame_code", KATTR__MAX);
3470 if (kerr != KCGI_OK)
3471 goto err;
3472 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
3473 if (kerr != KCGI_OK)
3474 goto err;
3475 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3476 if (kerr != KCGI_OK)
3477 goto err;
3479 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3480 if (kerr != KCGI_OK)
3481 goto err;
3483 a->lineno_cur++;
3484 bline = &a->lines[a->lineno_cur - 1];
3485 err:
3486 free(lineno);
3487 free(href_diff);
3488 free(href_link);
3490 done:
3491 if (commit)
3492 got_object_commit_close(commit);
3493 free(line);
3494 if (err == NULL && kerr != KCGI_OK)
3495 err = gw_kcgi_error(kerr);
3496 return err;
3499 static const struct got_error *
3500 gw_output_file_blame(struct gw_trans *gw_trans)
3502 const struct got_error *error = NULL;
3503 struct got_repository *repo = NULL;
3504 struct got_object_id *obj_id = NULL;
3505 struct got_object_id *commit_id = NULL;
3506 struct got_blob_object *blob = NULL;
3507 char *path = NULL, *in_repo_path = NULL;
3508 struct gw_blame_cb_args bca;
3509 int i, obj_type;
3510 size_t filesize;
3512 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3513 if (error)
3514 return error;
3516 /* XXX repo_file could be NULL if not present in querystring */
3517 if (asprintf(&path, "%s%s%s",
3518 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3519 gw_trans->repo_folder ? "/" : "",
3520 gw_trans->repo_file) == -1) {
3521 error = got_error_from_errno("asprintf");
3522 goto done;
3525 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3526 if (error)
3527 goto done;
3529 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3530 GOT_OBJ_TYPE_COMMIT, 1, repo);
3531 if (error)
3532 goto done;
3534 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3535 if (error)
3536 goto done;
3538 if (obj_id == NULL) {
3539 error = got_error(GOT_ERR_NO_OBJ);
3540 goto done;
3543 error = got_object_get_type(&obj_type, repo, obj_id);
3544 if (error)
3545 goto done;
3547 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3548 error = got_error(GOT_ERR_OBJ_TYPE);
3549 goto done;
3552 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3553 if (error)
3554 goto done;
3556 bca.f = got_opentemp();
3557 if (bca.f == NULL) {
3558 error = got_error_from_errno("got_opentemp");
3559 goto done;
3561 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3562 &bca.line_offsets, bca.f, blob);
3563 if (error || bca.nlines == 0)
3564 goto done;
3566 /* Don't include \n at EOF in the blame line count. */
3567 if (bca.line_offsets[bca.nlines - 1] == filesize)
3568 bca.nlines--;
3570 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3571 if (bca.lines == NULL) {
3572 error = got_error_from_errno("calloc");
3573 goto done;
3575 bca.lineno_cur = 1;
3576 bca.nlines_prec = 0;
3577 i = bca.nlines;
3578 while (i > 0) {
3579 i /= 10;
3580 bca.nlines_prec++;
3582 bca.repo = repo;
3583 bca.gw_trans = gw_trans;
3585 error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
3586 NULL, NULL);
3587 done:
3588 free(bca.line_offsets);
3589 free(in_repo_path);
3590 free(commit_id);
3591 free(obj_id);
3592 free(path);
3594 for (i = 0; i < bca.nlines; i++) {
3595 struct blame_line *bline = &bca.lines[i];
3596 free(bline->id_str);
3597 free(bline->committer);
3599 free(bca.lines);
3600 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3601 error = got_error_from_errno("fclose");
3602 if (blob)
3603 got_object_blob_close(blob);
3604 if (repo)
3605 got_repo_close(repo);
3606 return error;
3609 static const struct got_error *
3610 gw_output_blob_buf(struct gw_trans *gw_trans)
3612 const struct got_error *error = NULL;
3613 struct got_repository *repo = NULL;
3614 struct got_object_id *obj_id = NULL;
3615 struct got_object_id *commit_id = NULL;
3616 struct got_blob_object *blob = NULL;
3617 char *path = NULL, *in_repo_path = NULL;
3618 int obj_type, set_mime = 0;
3619 size_t len, hdrlen;
3620 const uint8_t *buf;
3621 enum kcgi_err kerr = KCGI_OK;
3623 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3624 if (error)
3625 return error;
3627 /* XXX repo_file could be NULL if not present in querystring */
3628 if (asprintf(&path, "%s%s%s",
3629 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3630 gw_trans->repo_folder ? "/" : "",
3631 gw_trans->repo_file) == -1) {
3632 error = got_error_from_errno("asprintf");
3633 goto done;
3636 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3637 if (error)
3638 goto done;
3640 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3641 GOT_OBJ_TYPE_COMMIT, 1, repo);
3642 if (error)
3643 goto done;
3645 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3646 if (error)
3647 goto done;
3649 if (obj_id == NULL) {
3650 error = got_error(GOT_ERR_NO_OBJ);
3651 goto done;
3654 error = got_object_get_type(&obj_type, repo, obj_id);
3655 if (error)
3656 goto done;
3658 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3659 error = got_error(GOT_ERR_OBJ_TYPE);
3660 goto done;
3663 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3664 if (error)
3665 goto done;
3667 hdrlen = got_object_blob_get_hdrlen(blob);
3668 do {
3669 error = got_object_blob_read_block(&len, blob);
3670 if (error)
3671 goto done;
3672 buf = got_object_blob_get_read_buf(blob);
3675 * Skip blob object header first time around,
3676 * which also contains a zero byte.
3678 buf += hdrlen;
3679 if (set_mime == 0) {
3680 if (isbinary(buf, len - hdrlen))
3681 gw_trans->mime = KMIME_APP_OCTET_STREAM;
3682 else
3683 gw_trans->mime = KMIME_TEXT_PLAIN;
3684 set_mime = 1;
3685 error = gw_display_index(gw_trans);
3686 if (error)
3687 goto done;
3689 khttp_write(gw_trans->gw_req, buf, len - hdrlen);
3690 hdrlen = 0;
3691 } while (len != 0);
3692 done:
3693 free(in_repo_path);
3694 free(commit_id);
3695 free(obj_id);
3696 free(path);
3697 if (blob)
3698 got_object_blob_close(blob);
3699 if (repo)
3700 got_repo_close(repo);
3701 if (error == NULL && kerr != KCGI_OK)
3702 error = gw_kcgi_error(kerr);
3703 return error;
3706 static const struct got_error *
3707 gw_output_repo_tree(struct gw_trans *gw_trans)
3709 const struct got_error *error = NULL;
3710 struct got_repository *repo = NULL;
3711 struct got_object_id *tree_id = NULL, *commit_id = NULL;
3712 struct got_tree_object *tree = NULL;
3713 char *path = NULL, *in_repo_path = NULL;
3714 char *id_str = NULL;
3715 char *build_folder = NULL;
3716 char *href_blob = NULL, *href_blame = NULL;
3717 const char *class = NULL;
3718 int nentries, i, class_flip = 0;
3719 enum kcgi_err kerr = KCGI_OK;
3721 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3722 if (error)
3723 return error;
3725 if (gw_trans->repo_folder != NULL) {
3726 path = strdup(gw_trans->repo_folder);
3727 if (path == NULL) {
3728 error = got_error_from_errno("strdup");
3729 goto done;
3731 } else {
3732 error = got_repo_map_path(&in_repo_path, repo,
3733 gw_trans->repo_path, 1);
3734 if (error)
3735 goto done;
3736 free(path);
3737 path = in_repo_path;
3740 if (gw_trans->commit == NULL) {
3741 struct got_reference *head_ref;
3742 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
3743 if (error)
3744 goto done;
3745 error = got_ref_resolve(&commit_id, repo, head_ref);
3746 if (error)
3747 goto done;
3748 got_ref_close(head_ref);
3750 } else {
3751 error = got_repo_match_object_id(&commit_id, NULL,
3752 gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
3753 if (error)
3754 goto done;
3758 * XXX gw_trans->commit might have already been allocated in
3759 * gw_parse_querystring(); could we more cleanly seperate values
3760 * we received as arguments from values we compute ourselves?
3762 error = got_object_id_str(&gw_trans->commit, commit_id);
3763 if (error)
3764 goto done;
3766 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
3767 if (error)
3768 goto done;
3770 error = got_object_open_as_tree(&tree, repo, tree_id);
3771 if (error)
3772 goto done;
3774 nentries = got_object_tree_get_nentries(tree);
3775 for (i = 0; i < nentries; i++) {
3776 struct got_tree_entry *te;
3777 const char *modestr = "";
3778 mode_t mode;
3780 te = got_object_tree_get_entry(tree, i);
3782 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
3783 if (error)
3784 goto done;
3786 mode = got_tree_entry_get_mode(te);
3787 if (got_object_tree_entry_is_submodule(te))
3788 modestr = "$";
3789 else if (S_ISLNK(mode))
3790 modestr = "@";
3791 else if (S_ISDIR(mode))
3792 modestr = "/";
3793 else if (mode & S_IXUSR)
3794 modestr = "*";
3796 if (class_flip == 0) {
3797 class = "back_lightgray";
3798 class_flip = 1;
3799 } else {
3800 class = "back_white";
3801 class_flip = 0;
3804 if (S_ISDIR(mode)) {
3805 if (asprintf(&build_folder, "%s/%s",
3806 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3807 got_tree_entry_get_name(te)) == -1) {
3808 error = got_error_from_errno("asprintf");
3809 goto done;
3811 if (asprintf(&href_blob,
3812 "?path=%s&action=%s&commit=%s&folder=%s",
3813 gw_trans->repo_name, gw_get_action_name(gw_trans),
3814 gw_trans->commit, build_folder) == -1) {
3815 error = got_error_from_errno("asprintf");
3816 goto done;
3819 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3820 KATTR_ID, "tree_wrapper", KATTR__MAX);
3821 if (kerr != KCGI_OK)
3822 goto done;
3823 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3824 KATTR_ID, "tree_line", KATTR_CLASS, class,
3825 KATTR__MAX);
3826 if (kerr != KCGI_OK)
3827 goto done;
3828 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3829 KATTR_HREF, href_blob, KATTR_CLASS,
3830 "diff_directory", KATTR__MAX);
3831 if (kerr != KCGI_OK)
3832 goto done;
3833 kerr = khtml_puts(gw_trans->gw_html_req,
3834 got_tree_entry_get_name(te));
3835 if (kerr != KCGI_OK)
3836 goto done;
3837 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3838 if (kerr != KCGI_OK)
3839 goto done;
3840 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3841 if (kerr != KCGI_OK)
3842 goto done;
3843 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3844 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
3845 KATTR__MAX);
3846 if (kerr != KCGI_OK)
3847 goto done;
3848 kerr = khtml_entity(gw_trans->gw_html_req,
3849 KENTITY_nbsp);
3850 if (kerr != KCGI_OK)
3851 goto done;
3852 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3853 if (kerr != KCGI_OK)
3854 goto done;
3855 } else {
3856 if (asprintf(&href_blob,
3857 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3858 gw_trans->repo_name, "blob", gw_trans->commit,
3859 got_tree_entry_get_name(te),
3860 gw_trans->repo_folder ?
3861 gw_trans->repo_folder : "") == -1) {
3862 error = got_error_from_errno("asprintf");
3863 goto done;
3865 if (asprintf(&href_blame,
3866 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3867 gw_trans->repo_name, "blame", gw_trans->commit,
3868 got_tree_entry_get_name(te),
3869 gw_trans->repo_folder ?
3870 gw_trans->repo_folder : "") == -1) {
3871 error = got_error_from_errno("asprintf");
3872 goto done;
3875 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3876 KATTR_ID, "tree_wrapper", KATTR__MAX);
3877 if (kerr != KCGI_OK)
3878 goto done;
3879 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3880 KATTR_ID, "tree_line", KATTR_CLASS, class,
3881 KATTR__MAX);
3882 if (kerr != KCGI_OK)
3883 goto done;
3884 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3885 KATTR_HREF, href_blob, KATTR__MAX);
3886 if (kerr != KCGI_OK)
3887 goto done;
3888 kerr = khtml_puts(gw_trans->gw_html_req,
3889 got_tree_entry_get_name(te));
3890 if (kerr != KCGI_OK)
3891 goto done;
3892 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3893 if (kerr != KCGI_OK)
3894 goto done;
3895 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3896 if (kerr != KCGI_OK)
3897 goto done;
3898 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3899 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
3900 KATTR__MAX);
3901 if (kerr != KCGI_OK)
3902 goto done;
3904 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3905 KATTR_HREF, href_blob, KATTR__MAX);
3906 if (kerr != KCGI_OK)
3907 goto done;
3908 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
3909 if (kerr != KCGI_OK)
3910 goto done;
3911 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3912 if (kerr != KCGI_OK)
3913 goto done;
3915 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3916 if (kerr != KCGI_OK)
3917 goto done;
3919 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3920 KATTR_HREF, href_blame, KATTR__MAX);
3921 if (kerr != KCGI_OK)
3922 goto done;
3923 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
3924 if (kerr != KCGI_OK)
3925 goto done;
3927 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3928 if (kerr != KCGI_OK)
3929 goto done;
3931 free(id_str);
3932 id_str = NULL;
3933 free(href_blob);
3934 href_blob = NULL;
3935 free(build_folder);
3936 build_folder = NULL;
3938 done:
3939 if (tree)
3940 got_object_tree_close(tree);
3941 if (repo)
3942 got_repo_close(repo);
3943 free(id_str);
3944 free(href_blob);
3945 free(href_blame);
3946 free(in_repo_path);
3947 free(tree_id);
3948 free(build_folder);
3949 if (error == NULL && kerr != KCGI_OK)
3950 error = gw_kcgi_error(kerr);
3951 return error;
3954 static const struct got_error *
3955 gw_output_repo_heads(struct gw_trans *gw_trans)
3957 const struct got_error *error = NULL;
3958 struct got_repository *repo = NULL;
3959 struct got_reflist_head refs;
3960 struct got_reflist_entry *re;
3961 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
3962 char *href_commits = NULL;
3963 enum kcgi_err kerr = KCGI_OK;
3965 SIMPLEQ_INIT(&refs);
3967 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3968 if (error)
3969 goto done;
3971 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
3972 NULL);
3973 if (error)
3974 goto done;
3976 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3977 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
3978 if (kerr != KCGI_OK)
3979 goto done;
3980 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3981 KATTR_ID, "summary_heads_title", KATTR__MAX);
3982 if (kerr != KCGI_OK)
3983 goto done;
3984 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
3985 if (kerr != KCGI_OK)
3986 goto done;
3987 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3988 if (kerr != KCGI_OK)
3989 goto done;
3990 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3991 KATTR_ID, "summary_heads_content", KATTR__MAX);
3992 if (kerr != KCGI_OK)
3993 goto done;
3995 SIMPLEQ_FOREACH(re, &refs, entry) {
3996 const char *refname;
3998 if (got_ref_is_symbolic(re->ref))
3999 continue;
4001 refname = got_ref_get_name(re->ref);
4002 if (refname == NULL) {
4003 error = got_error_from_errno("got_ref_to_str");
4004 goto done;
4007 if (strncmp(refname, "refs/heads/", 11) != 0)
4008 continue;
4010 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
4011 refname, TM_DIFF);
4012 if (error)
4013 goto done;
4015 if (strncmp(refname, "refs/heads/", 11) == 0)
4016 refname += 11;
4018 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4019 KATTR_ID, "heads_wrapper", KATTR__MAX);
4020 if (kerr != KCGI_OK)
4021 goto done;
4022 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4023 KATTR_ID, "heads_age", KATTR__MAX);
4024 if (kerr != KCGI_OK)
4025 goto done;
4026 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
4027 if (kerr != KCGI_OK)
4028 goto done;
4029 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4030 if (kerr != KCGI_OK)
4031 goto done;
4032 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4033 KATTR_ID, "heads_space", KATTR__MAX);
4034 if (kerr != KCGI_OK)
4035 goto done;
4036 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
4037 if (kerr != KCGI_OK)
4038 goto done;
4039 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4040 if (kerr != KCGI_OK)
4041 goto done;
4042 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4043 KATTR_ID, "head", KATTR__MAX);
4044 if (kerr != KCGI_OK)
4045 goto done;
4046 if (asprintf(&href_summary,
4047 "?path=%s&action=summary&headref=%s",
4048 gw_trans->repo_name, refname) == -1) {
4049 error = got_error_from_errno("asprintf");
4050 goto done;
4052 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4053 href_summary, KATTR__MAX);
4054 kerr = khtml_puts(gw_trans->gw_html_req, refname);
4055 if (kerr != KCGI_OK)
4056 goto done;
4057 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4058 if (kerr != KCGI_OK)
4059 goto done;
4061 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4062 "navs_wrapper", KATTR__MAX);
4063 if (kerr != KCGI_OK)
4064 goto done;
4065 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4066 "navs", KATTR__MAX);
4067 if (kerr != KCGI_OK)
4068 goto done;
4070 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4071 href_summary, KATTR__MAX);
4072 if (kerr != KCGI_OK)
4073 goto done;
4074 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4075 if (kerr != KCGI_OK)
4076 goto done;
4077 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4078 if (kerr != KCGI_OK)
4079 goto done;
4081 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4082 if (kerr != KCGI_OK)
4083 goto done;
4084 if (asprintf(&href_briefs, "?path=%s&action=briefs&headref=%s",
4085 gw_trans->repo_name, refname) == -1) {
4086 error = got_error_from_errno("asprintf");
4087 goto done;
4089 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4090 href_briefs, KATTR__MAX);
4091 if (kerr != KCGI_OK)
4092 goto done;
4093 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4094 if (kerr != KCGI_OK)
4095 goto done;
4096 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4097 if (kerr != KCGI_OK)
4098 goto done;
4100 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4101 if (kerr != KCGI_OK)
4102 goto done;
4104 if (asprintf(&href_commits,
4105 "?path=%s&action=commits&headref=%s",
4106 gw_trans->repo_name, refname) == -1) {
4107 error = got_error_from_errno("asprintf");
4108 goto done;
4110 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4111 href_commits, KATTR__MAX);
4112 if (kerr != KCGI_OK)
4113 goto done;
4114 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4115 if (kerr != KCGI_OK)
4116 goto done;
4117 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4118 if (kerr != KCGI_OK)
4119 goto done;
4121 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4122 "dotted_line", KATTR__MAX);
4123 if (kerr != KCGI_OK)
4124 goto done;
4125 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4126 if (kerr != KCGI_OK)
4127 goto done;
4128 free(href_summary);
4129 href_summary = NULL;
4130 free(href_briefs);
4131 href_briefs = NULL;
4132 free(href_commits);
4133 href_commits = NULL;
4135 done:
4136 got_ref_list_free(&refs);
4137 free(href_summary);
4138 free(href_briefs);
4139 free(href_commits);
4140 if (repo)
4141 got_repo_close(repo);
4142 return error;
4145 static const struct got_error *
4146 gw_output_site_link(struct gw_trans *gw_trans)
4148 const struct got_error *error = NULL;
4149 char *href_summary = NULL;
4150 enum kcgi_err kerr = KCGI_OK;
4152 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4153 "site_link", KATTR__MAX);
4154 if (kerr != KCGI_OK)
4155 goto done;
4156 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4157 KATTR__MAX);
4158 if (kerr != KCGI_OK)
4159 goto done;
4160 kerr = khtml_puts(gw_trans->gw_html_req,
4161 gw_trans->gw_conf->got_site_link);
4162 if (kerr != KCGI_OK)
4163 goto done;
4164 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4165 if (kerr != KCGI_OK)
4166 goto done;
4168 if (gw_trans->repo_name != NULL) {
4169 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4170 if (kerr != KCGI_OK)
4171 goto done;
4172 if (asprintf(&href_summary, "?path=%s&action=summary",
4173 gw_trans->repo_name) == -1)
4174 goto done;
4175 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4176 href_summary, KATTR__MAX);
4177 if (kerr != KCGI_OK)
4178 goto done;
4179 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4180 if (kerr != KCGI_OK)
4181 goto done;
4182 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4183 if (kerr != KCGI_OK)
4184 goto done;
4185 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4186 if (kerr != KCGI_OK)
4187 goto done;
4188 kerr = khtml_puts(gw_trans->gw_html_req,
4189 gw_get_action_name(gw_trans));
4190 if (kerr != KCGI_OK)
4191 goto done;
4194 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4195 if (kerr != KCGI_OK)
4196 goto done;
4197 done:
4198 free(href_summary);
4199 return error;
4202 static const struct got_error *
4203 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4205 const struct got_error *error = NULL;
4206 char *color = NULL;
4207 enum kcgi_err kerr = KCGI_OK;
4209 if (strncmp(buf, "-", 1) == 0)
4210 color = "diff_minus";
4211 else if (strncmp(buf, "+", 1) == 0)
4212 color = "diff_plus";
4213 else if (strncmp(buf, "@@", 2) == 0)
4214 color = "diff_chunk_header";
4215 else if (strncmp(buf, "@@", 2) == 0)
4216 color = "diff_chunk_header";
4217 else if (strncmp(buf, "commit +", 8) == 0)
4218 color = "diff_meta";
4219 else if (strncmp(buf, "commit -", 8) == 0)
4220 color = "diff_meta";
4221 else if (strncmp(buf, "blob +", 6) == 0)
4222 color = "diff_meta";
4223 else if (strncmp(buf, "blob -", 6) == 0)
4224 color = "diff_meta";
4225 else if (strncmp(buf, "file +", 6) == 0)
4226 color = "diff_meta";
4227 else if (strncmp(buf, "file -", 6) == 0)
4228 color = "diff_meta";
4229 else if (strncmp(buf, "from:", 5) == 0)
4230 color = "diff_author";
4231 else if (strncmp(buf, "via:", 4) == 0)
4232 color = "diff_author";
4233 else if (strncmp(buf, "date:", 5) == 0)
4234 color = "diff_date";
4235 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4236 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4237 if (error == NULL && kerr != KCGI_OK)
4238 error = gw_kcgi_error(kerr);
4239 return error;
4242 int
4243 main(int argc, char *argv[])
4245 const struct got_error *error = NULL;
4246 struct gw_trans *gw_trans;
4247 struct gw_dir *dir = NULL, *tdir;
4248 const char *page = "index";
4249 int gw_malloc = 1;
4250 enum kcgi_err kerr;
4252 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4253 errx(1, "malloc");
4255 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4256 errx(1, "malloc");
4258 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4259 errx(1, "malloc");
4261 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4262 errx(1, "malloc");
4264 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4265 if (kerr != KCGI_OK) {
4266 error = gw_kcgi_error(kerr);
4267 goto done;
4270 if ((gw_trans->gw_conf =
4271 malloc(sizeof(struct gotweb_conf))) == NULL) {
4272 gw_malloc = 0;
4273 error = got_error_from_errno("malloc");
4274 goto done;
4277 TAILQ_INIT(&gw_trans->gw_dirs);
4278 TAILQ_INIT(&gw_trans->gw_headers);
4280 gw_trans->action = -1;
4281 gw_trans->page = 0;
4282 gw_trans->repos_total = 0;
4283 gw_trans->repo_path = NULL;
4284 gw_trans->commit = NULL;
4285 gw_trans->headref = GOT_REF_HEAD;
4286 gw_trans->mime = KMIME_TEXT_HTML;
4287 gw_trans->gw_tmpl->key = gw_templs;
4288 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4289 gw_trans->gw_tmpl->arg = gw_trans;
4290 gw_trans->gw_tmpl->cb = gw_template;
4291 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
4292 if (error)
4293 goto done;
4295 error = gw_parse_querystring(gw_trans);
4296 if (error)
4297 goto done;
4299 if (gw_trans->action == GW_BLOB)
4300 error = gw_blob(gw_trans);
4301 else
4302 error = gw_display_index(gw_trans);
4303 done:
4304 if (gw_malloc) {
4305 free(gw_trans->gw_conf->got_repos_path);
4306 free(gw_trans->gw_conf->got_site_name);
4307 free(gw_trans->gw_conf->got_site_owner);
4308 free(gw_trans->gw_conf->got_site_link);
4309 free(gw_trans->gw_conf->got_logo);
4310 free(gw_trans->gw_conf->got_logo_url);
4311 free(gw_trans->gw_conf);
4312 free(gw_trans->commit);
4313 free(gw_trans->repo_path);
4315 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4316 free(dir->name);
4317 free(dir->description);
4318 free(dir->age);
4319 free(dir->url);
4320 free(dir->path);
4321 free(dir);
4326 khttp_free(gw_trans->gw_req);
4327 return 0;