Blob


1 /*
2 * Copyright (c) 2019, 2020 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/queue.h>
19 #include <sys/stat.h>
20 #include <sys/types.h>
22 #include <ctype.h>
23 #include <dirent.h>
24 #include <err.h>
25 #include <errno.h>
26 #include <regex.h>
27 #include <stdarg.h>
28 #include <stdint.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
34 #include <got_object.h>
35 #include <got_reference.h>
36 #include <got_repository.h>
37 #include <got_path.h>
38 #include <got_cancel.h>
39 #include <got_worktree.h>
40 #include <got_diff.h>
41 #include <got_commit_graph.h>
42 #include <got_blame.h>
43 #include <got_privsep.h>
44 #include <got_opentemp.h>
46 #include <kcgi.h>
47 #include <kcgihtml.h>
49 #include "gotweb.h"
51 #ifndef nitems
52 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
53 #endif
55 struct gw_trans {
56 TAILQ_HEAD(headers, gw_header) gw_headers;
57 TAILQ_HEAD(dirs, gw_dir) gw_dirs;
58 struct gw_dir *gw_dir;
59 struct gotweb_conf *gw_conf;
60 struct ktemplate *gw_tmpl;
61 struct khtmlreq *gw_html_req;
62 struct kreq *gw_req;
63 const char *repo_name;
64 char *repo_path;
65 char *commit;
66 char *repo_file;
67 char *repo_folder;
68 char *headref;
69 unsigned int action;
70 unsigned int page;
71 unsigned int repos_total;
72 enum kmime mime;
73 };
75 struct gw_header {
76 TAILQ_ENTRY(gw_header) entry;
77 struct got_repository *repo;
78 struct got_reflist_head refs;
79 struct got_commit_object *commit;
80 struct got_object_id *id;
81 char *path;
83 char *refs_str;
84 char *commit_id; /* id_str1 */
85 char *parent_id; /* id_str2 */
86 char *tree_id;
87 char *author;
88 char *committer;
89 char *commit_msg;
90 time_t committer_time;
91 };
93 struct gw_dir {
94 TAILQ_ENTRY(gw_dir) entry;
95 char *name;
96 char *owner;
97 char *description;
98 char *url;
99 char *age;
100 char *path;
101 };
103 enum gw_key {
104 KEY_ACTION,
105 KEY_COMMIT_ID,
106 KEY_FILE,
107 KEY_FOLDER,
108 KEY_HEADREF,
109 KEY_PAGE,
110 KEY_PATH,
111 KEY__ZMAX
112 };
114 enum gw_tmpl {
115 TEMPL_CONTENT,
116 TEMPL_HEAD,
117 TEMPL_HEADER,
118 TEMPL_SEARCH,
119 TEMPL_SITEPATH,
120 TEMPL_SITEOWNER,
121 TEMPL_TITLE,
122 TEMPL__MAX
123 };
125 enum gw_ref_tm {
126 TM_DIFF,
127 TM_LONG,
128 };
130 enum gw_tags {
131 TAGBRIEF,
132 TAGFULL,
133 };
135 static const char *const gw_templs[TEMPL__MAX] = {
136 "content",
137 "head",
138 "header",
139 "search",
140 "sitepath",
141 "siteowner",
142 "title",
143 };
145 static const struct kvalid gw_keys[KEY__ZMAX] = {
146 { kvalid_stringne, "action" },
147 { kvalid_stringne, "commit" },
148 { kvalid_stringne, "file" },
149 { kvalid_stringne, "folder" },
150 { kvalid_stringne, "headref" },
151 { kvalid_int, "page" },
152 { kvalid_stringne, "path" },
153 };
155 static const struct got_error *gw_init_gw_dir(struct gw_dir **, const char *);
156 static struct gw_header *gw_init_header(void);
158 static void gw_free_headers(struct gw_header *);
159 static void gw_display_error(struct gw_trans *,
160 const struct got_error *);
162 static int gw_template(size_t, void *);
164 static const struct got_error *gw_strdup_string(char **, char *,
165 const char *);
166 static const struct got_error *gw_get_repo_description(char **,
167 struct gw_trans *, char *);
168 static const struct got_error *gw_get_repo_owner(char **, struct gw_trans *,
169 char *);
170 static const struct got_error *gw_get_time_str(char **, time_t, int);
171 static const struct got_error *gw_get_repo_age(char **, struct gw_trans *,
172 char *, char *, int);
173 static const struct got_error *gw_output_file_blame(struct gw_trans *);
174 static const struct got_error *gw_output_blob_buf(struct gw_trans *);
175 static const struct got_error *gw_output_repo_tree(struct gw_trans *);
176 static const struct got_error *gw_output_diff(struct gw_trans *,
177 struct gw_header *);
178 static const struct got_error *gw_output_repo_tags(struct gw_trans *,
179 struct gw_header *, int, int);
180 static const struct got_error *gw_output_repo_heads(struct gw_trans *);
181 static const struct got_error *gw_output_site_link(struct gw_trans *);
182 static const struct got_error *gw_get_clone_url(char **, struct gw_trans *,
183 char *);
184 static const struct got_error *gw_colordiff_line(struct gw_trans *, char *);
186 static const struct got_error *gw_gen_commit_header(struct gw_trans *, char *,
187 char*);
188 static const struct got_error *gw_gen_diff_header(struct gw_trans *, char *,
189 char*);
190 static const struct got_error *gw_gen_author_header(struct gw_trans *,
191 const char *);
192 static const struct got_error *gw_gen_age_header(struct gw_trans *,
193 const char *);
194 static const struct got_error *gw_gen_committer_header(struct gw_trans *,
195 const char *);
196 static const struct got_error *gw_gen_commit_msg_header(struct gw_trans*,
197 char *);
198 static const struct got_error *gw_gen_tree_header(struct gw_trans *, char *);
199 static const struct got_error *gw_display_open(struct gw_trans *, enum khttp,
200 enum kmime);
201 static const struct got_error *gw_display_index(struct gw_trans *);
202 static const struct got_error *gw_get_header(struct gw_trans *,
203 struct gw_header *, int);
204 static const struct got_error *gw_get_commits(struct gw_trans *,
205 struct gw_header *, int);
206 static const struct got_error *gw_get_commit(struct gw_trans *,
207 struct gw_header *);
208 static const struct got_error *gw_apply_unveil(const char *);
209 static const struct got_error *gw_blame_cb(void *, int, int,
210 struct got_object_id *);
211 static const struct got_error *gw_load_got_paths(struct gw_trans *);
212 static const struct got_error *gw_load_got_path(struct gw_trans *,
213 struct gw_dir *);
214 static const struct got_error *gw_parse_querystring(struct gw_trans *);
215 static const struct got_error *gw_blame(struct gw_trans *);
216 static const struct got_error *gw_blob(struct gw_trans *);
217 static const struct got_error *gw_diff(struct gw_trans *);
218 static const struct got_error *gw_index(struct gw_trans *);
219 static const struct got_error *gw_commits(struct gw_trans *);
220 static const struct got_error *gw_briefs(struct gw_trans *);
221 static const struct got_error *gw_summary(struct gw_trans *);
222 static const struct got_error *gw_tree(struct gw_trans *);
223 static const struct got_error *gw_tag(struct gw_trans *);
225 struct gw_query_action {
226 unsigned int func_id;
227 const char *func_name;
228 const struct got_error *(*func_main)(struct gw_trans *);
229 char *template;
230 };
232 enum gw_query_actions {
233 GW_BLAME,
234 GW_BLOB,
235 GW_BRIEFS,
236 GW_COMMITS,
237 GW_DIFF,
238 GW_ERR,
239 GW_INDEX,
240 GW_SUMMARY,
241 GW_TAG,
242 GW_TREE,
243 };
245 static struct gw_query_action gw_query_funcs[] = {
246 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
247 { GW_BLOB, "blob", NULL, NULL },
248 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
249 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
250 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
251 { GW_ERR, NULL, NULL, "gw_tmpl/err.tmpl" },
252 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
253 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
254 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
255 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
256 };
258 static const char *
259 gw_get_action_name(struct gw_trans *gw_trans)
261 return gw_query_funcs[gw_trans->action].func_name;
264 static const struct got_error *
265 gw_kcgi_error(enum kcgi_err kerr)
267 if (kerr == KCGI_OK)
268 return NULL;
270 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
271 return got_error(GOT_ERR_CANCELLED);
273 if (kerr == KCGI_ENOMEM)
274 return got_error_set_errno(ENOMEM,
275 kcgi_strerror(kerr));
277 if (kerr == KCGI_ENFILE)
278 return got_error_set_errno(ENFILE,
279 kcgi_strerror(kerr));
281 if (kerr == KCGI_EAGAIN)
282 return got_error_set_errno(EAGAIN,
283 kcgi_strerror(kerr));
285 if (kerr == KCGI_FORM)
286 return got_error_msg(GOT_ERR_IO,
287 kcgi_strerror(kerr));
289 return got_error_from_errno(kcgi_strerror(kerr));
292 static const struct got_error *
293 gw_apply_unveil(const char *repo_path)
295 const struct got_error *err;
297 if (repo_path && unveil(repo_path, "r") != 0)
298 return got_error_from_errno2("unveil", repo_path);
300 if (unveil("/tmp", "rwc") != 0)
301 return got_error_from_errno2("unveil", "/tmp");
303 err = got_privsep_unveil_exec_helpers();
304 if (err != NULL)
305 return err;
307 if (unveil(NULL, NULL) != 0)
308 return got_error_from_errno("unveil");
310 return NULL;
313 static const struct got_error *
314 gw_strdup_string(char **s, char *str1, const char *str2)
316 if (str1 && str2)
317 /* XXX and what is the value of errno ?!? */
318 return got_error_from_errno("strdup");
319 if (str1)
320 *s = strdup(str1);
321 else
322 *s = strdup(str2);
323 if (*s == NULL)
324 return got_error_from_errno("strdup");
325 return NULL;
328 static int
329 isbinary(const uint8_t *buf, size_t n)
331 size_t i;
333 for (i = 0; i < n; i++)
334 if (buf[i] == 0)
335 return 1;
336 return 0;
339 static const struct got_error *
340 gw_blame(struct gw_trans *gw_trans)
342 const struct got_error *error = NULL;
343 struct gw_header *header = NULL;
344 char *age = NULL;
345 enum kcgi_err kerr;
347 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
348 NULL) == -1)
349 return got_error_from_errno("pledge");
351 if ((header = gw_init_header()) == NULL)
352 return got_error_from_errno("malloc");
354 error = gw_apply_unveil(gw_trans->gw_dir->path);
355 if (error)
356 goto done;
358 error = gw_get_header(gw_trans, header, 1);
359 if (error)
360 goto done;
362 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
363 "blame_header_wrapper", KATTR__MAX);
364 if (kerr != KCGI_OK)
365 goto done;
366 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
367 "blame_header", KATTR__MAX);
368 if (kerr != KCGI_OK)
369 goto done;
370 error = gw_get_time_str(&age, header->committer_time,
371 TM_LONG);
372 if (error)
373 goto done;
374 error = gw_gen_age_header(gw_trans, age ?age : "");
375 if (error)
376 goto done;
377 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
378 if (error)
379 goto done;
380 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
381 if (kerr != KCGI_OK)
382 goto done;
383 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
384 "dotted_line", KATTR__MAX);
385 if (kerr != KCGI_OK)
386 goto done;
387 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
388 if (kerr != KCGI_OK)
389 goto done;
391 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
392 "blame", KATTR__MAX);
393 if (kerr != KCGI_OK)
394 goto done;
395 error = gw_output_file_blame(gw_trans);
396 if (error)
397 goto done;
398 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
399 done:
400 got_ref_list_free(&header->refs);
401 gw_free_headers(header);
402 if (error == NULL && kerr != KCGI_OK)
403 error = gw_kcgi_error(kerr);
404 return error;
407 static const struct got_error *
408 gw_blob(struct gw_trans *gw_trans)
410 const struct got_error *error = NULL;
411 struct gw_header *header = NULL;
413 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
414 NULL) == -1)
415 return got_error_from_errno("pledge");
417 if ((header = gw_init_header()) == NULL)
418 return got_error_from_errno("malloc");
420 error = gw_apply_unveil(gw_trans->gw_dir->path);
421 if (error)
422 goto done;
424 error = gw_get_header(gw_trans, header, 1);
425 if (error)
426 goto done;
428 error = gw_output_blob_buf(gw_trans);
429 done:
430 got_ref_list_free(&header->refs);
431 gw_free_headers(header);
432 return error;
435 static const struct got_error *
436 gw_diff(struct gw_trans *gw_trans)
438 const struct got_error *error = NULL;
439 struct gw_header *header = NULL;
440 char *age = NULL;
441 enum kcgi_err kerr = KCGI_OK;
443 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
444 NULL) == -1)
445 return got_error_from_errno("pledge");
447 if ((header = gw_init_header()) == NULL)
448 return got_error_from_errno("malloc");
450 error = gw_apply_unveil(gw_trans->gw_dir->path);
451 if (error)
452 goto done;
454 error = gw_get_header(gw_trans, header, 1);
455 if (error)
456 goto done;
458 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
459 "diff_header_wrapper", KATTR__MAX);
460 if (kerr != KCGI_OK)
461 goto done;
462 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
463 "diff_header", KATTR__MAX);
464 if (kerr != KCGI_OK)
465 goto done;
466 error = gw_gen_diff_header(gw_trans, header->parent_id,
467 header->commit_id);
468 if (error)
469 goto done;
470 error = gw_gen_commit_header(gw_trans, header->commit_id,
471 header->refs_str);
472 if (error)
473 goto done;
474 error = gw_gen_tree_header(gw_trans, header->tree_id);
475 if (error)
476 goto done;
477 error = gw_gen_author_header(gw_trans, header->author);
478 if (error)
479 goto done;
480 error = gw_gen_committer_header(gw_trans, header->author);
481 if (error)
482 goto done;
483 error = gw_get_time_str(&age, header->committer_time,
484 TM_LONG);
485 if (error)
486 goto done;
487 error = gw_gen_age_header(gw_trans, age ?age : "");
488 if (error)
489 goto done;
490 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
491 if (error)
492 goto done;
493 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
494 if (kerr != KCGI_OK)
495 goto done;
496 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
497 "dotted_line", KATTR__MAX);
498 if (kerr != KCGI_OK)
499 goto done;
500 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
501 if (kerr != KCGI_OK)
502 goto done;
504 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
505 "diff", KATTR__MAX);
506 if (kerr != KCGI_OK)
507 goto done;
508 error = gw_output_diff(gw_trans, header);
509 if (error)
510 goto done;
512 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
513 done:
514 got_ref_list_free(&header->refs);
515 gw_free_headers(header);
516 free(age);
517 if (error == NULL && kerr != KCGI_OK)
518 error = gw_kcgi_error(kerr);
519 return error;
522 static const struct got_error *
523 gw_index(struct gw_trans *gw_trans)
525 const struct got_error *error = NULL;
526 struct gw_dir *gw_dir = NULL;
527 char *href_next = NULL, *href_prev = NULL, *href_summary = NULL;
528 char *href_briefs = NULL, *href_commits = NULL, *href_tree = NULL;
529 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
530 enum kcgi_err kerr;
532 if (pledge("stdio rpath proc exec sendfd unveil",
533 NULL) == -1) {
534 error = got_error_from_errno("pledge");
535 return error;
538 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path);
539 if (error)
540 return error;
542 error = gw_load_got_paths(gw_trans);
543 if (error)
544 return error;
546 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
547 "index_header", KATTR__MAX);
548 if (kerr != KCGI_OK)
549 return gw_kcgi_error(kerr);
550 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
551 "index_header_project", KATTR__MAX);
552 if (kerr != KCGI_OK)
553 return gw_kcgi_error(kerr);
554 kerr = khtml_puts(gw_trans->gw_html_req, "Project");
555 if (kerr != KCGI_OK)
556 return gw_kcgi_error(kerr);
557 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
558 if (kerr != KCGI_OK)
559 return gw_kcgi_error(kerr);
561 if (gw_trans->gw_conf->got_show_repo_description) {
562 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
563 "index_header_description", KATTR__MAX);
564 if (kerr != KCGI_OK)
565 return gw_kcgi_error(kerr);
566 kerr = khtml_puts(gw_trans->gw_html_req, "Description");
567 if (kerr != KCGI_OK)
568 return gw_kcgi_error(kerr);
569 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
570 if (kerr != KCGI_OK)
571 return gw_kcgi_error(kerr);
574 if (gw_trans->gw_conf->got_show_repo_owner) {
575 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
576 "index_header_owner", KATTR__MAX);
577 if (kerr != KCGI_OK)
578 return gw_kcgi_error(kerr);
579 kerr = khtml_puts(gw_trans->gw_html_req, "Owner");
580 if (kerr != KCGI_OK)
581 return gw_kcgi_error(kerr);
582 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
583 if (kerr != KCGI_OK)
584 return gw_kcgi_error(kerr);
587 if (gw_trans->gw_conf->got_show_repo_age) {
588 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
589 "index_header_age", KATTR__MAX);
590 if (kerr != KCGI_OK)
591 return gw_kcgi_error(kerr);
592 kerr = khtml_puts(gw_trans->gw_html_req, "Last Change");
593 if (kerr != KCGI_OK)
594 return gw_kcgi_error(kerr);
595 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
596 if (kerr != KCGI_OK)
597 return gw_kcgi_error(kerr);
600 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
601 if (kerr != KCGI_OK)
602 return gw_kcgi_error(kerr);
604 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
605 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
606 "index_wrapper", KATTR__MAX);
607 if (kerr != KCGI_OK)
608 return gw_kcgi_error(kerr);
609 kerr = khtml_puts(gw_trans->gw_html_req,
610 "No repositories found in ");
611 if (kerr != KCGI_OK)
612 return gw_kcgi_error(kerr);
613 kerr = khtml_puts(gw_trans->gw_html_req,
614 gw_trans->gw_conf->got_repos_path);
615 if (kerr != KCGI_OK)
616 return gw_kcgi_error(kerr);
617 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
618 if (kerr != KCGI_OK)
619 return gw_kcgi_error(kerr);
620 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
621 "dotted_line", KATTR__MAX);
622 if (kerr != KCGI_OK)
623 return gw_kcgi_error(kerr);
624 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
625 if (kerr != KCGI_OK)
626 return gw_kcgi_error(kerr);
627 return error;
630 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
631 dir_c++;
633 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
634 if (gw_trans->page > 0 && (gw_trans->page *
635 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
636 prev_disp++;
637 continue;
640 prev_disp++;
642 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
643 "index_wrapper", KATTR__MAX);
644 if (kerr != KCGI_OK)
645 goto done;
647 if (asprintf(&href_summary, "?path=%s&action=summary",
648 gw_dir->name) == -1) {
649 error = got_error_from_errno("asprintf");
650 goto done;
652 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
653 "index_project", KATTR__MAX);
654 if (kerr != KCGI_OK)
655 goto done;
656 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
657 href_summary, KATTR__MAX);
658 if (kerr != KCGI_OK)
659 goto done;
660 kerr = khtml_puts(gw_trans->gw_html_req, gw_dir->name);
661 if (kerr != KCGI_OK)
662 goto done;
663 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
664 if (kerr != KCGI_OK)
665 goto done;
666 if (gw_trans->gw_conf->got_show_repo_description) {
667 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
668 KATTR_ID, "index_project_description", KATTR__MAX);
669 if (kerr != KCGI_OK)
670 goto done;
671 kerr = khtml_puts(gw_trans->gw_html_req,
672 gw_dir->description ? gw_dir->description : "");
673 if (kerr != KCGI_OK)
674 goto done;
675 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
676 if (kerr != KCGI_OK)
677 goto done;
679 if (gw_trans->gw_conf->got_show_repo_owner) {
680 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
681 KATTR_ID, "index_project_owner", KATTR__MAX);
682 if (kerr != KCGI_OK)
683 goto done;
684 kerr = khtml_puts(gw_trans->gw_html_req,
685 gw_dir->owner ? gw_dir->owner : "");
686 if (kerr != KCGI_OK)
687 goto done;
688 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
689 if (kerr != KCGI_OK)
690 goto done;
692 if (gw_trans->gw_conf->got_show_repo_age) {
693 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
694 KATTR_ID, "index_project_age", KATTR__MAX);
695 if (kerr != KCGI_OK)
696 goto done;
697 kerr = khtml_puts(gw_trans->gw_html_req,
698 gw_dir->age ? gw_dir->age : "");
699 if (kerr != KCGI_OK)
700 goto done;
701 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
702 if (kerr != KCGI_OK)
703 goto done;
706 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
707 "navs_wrapper", KATTR__MAX);
708 if (kerr != KCGI_OK)
709 goto done;
710 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
711 "navs", KATTR__MAX);
712 if (kerr != KCGI_OK)
713 goto done;
715 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
716 href_summary, KATTR__MAX);
717 if (kerr != KCGI_OK)
718 goto done;
719 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
720 if (kerr != KCGI_OK)
721 goto done;
722 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
723 if (kerr != KCGI_OK)
724 goto done;
726 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
727 if (kerr != KCGI_OK)
728 goto done;
730 if (asprintf(&href_briefs, "?path=%s&action=briefs",
731 gw_dir->name) == -1) {
732 error = got_error_from_errno("asprintf");
733 goto done;
735 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
736 href_briefs, KATTR__MAX);
737 if (kerr != KCGI_OK)
738 goto done;
739 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
740 if (kerr != KCGI_OK)
741 goto done;
742 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
743 if (kerr != KCGI_OK)
744 error = gw_kcgi_error(kerr);
746 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
747 if (kerr != KCGI_OK)
748 goto done;
750 if (asprintf(&href_commits, "?path=%s&action=commits",
751 gw_dir->name) == -1) {
752 error = got_error_from_errno("asprintf");
753 goto done;
755 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
756 href_commits, KATTR__MAX);
757 if (kerr != KCGI_OK)
758 goto done;
759 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
760 if (kerr != KCGI_OK)
761 goto done;
762 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
763 if (kerr != KCGI_OK)
764 goto done;
766 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
767 if (kerr != KCGI_OK)
768 goto done;
770 if (asprintf(&href_tree, "?path=%s&action=tree",
771 gw_dir->name) == -1) {
772 error = got_error_from_errno("asprintf");
773 goto done;
775 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
776 href_tree, KATTR__MAX);
777 if (kerr != KCGI_OK)
778 goto done;
779 kerr = khtml_puts(gw_trans->gw_html_req, "tree");
780 if (kerr != KCGI_OK)
781 goto done;
783 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
784 if (kerr != KCGI_OK)
785 goto done;
786 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
787 "dotted_line", KATTR__MAX);
788 if (kerr != KCGI_OK)
789 goto done;
790 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
791 if (kerr != KCGI_OK)
792 goto done;
794 free(href_summary);
795 href_summary = NULL;
796 free(href_briefs);
797 href_briefs = NULL;
798 free(href_commits);
799 href_commits = NULL;
800 free(href_tree);
801 href_tree = NULL;
803 if (gw_trans->gw_conf->got_max_repos_display == 0)
804 continue;
806 if (next_disp == gw_trans->gw_conf->got_max_repos_display) {
807 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
808 KATTR_ID, "np_wrapper", KATTR__MAX);
809 if (kerr != KCGI_OK)
810 goto done;
811 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
812 KATTR_ID, "nav_prev", KATTR__MAX);
813 if (kerr != KCGI_OK)
814 goto done;
815 } else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
816 (gw_trans->page > 0) &&
817 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
818 prev_disp == gw_trans->repos_total)) {
819 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
820 KATTR_ID, "np_wrapper", KATTR__MAX);
821 if (kerr != KCGI_OK)
822 goto done;
823 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
824 KATTR_ID, "nav_prev", KATTR__MAX);
825 if (kerr != KCGI_OK)
826 goto done;
829 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
830 (gw_trans->page > 0) &&
831 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
832 prev_disp == gw_trans->repos_total)) {
833 if (asprintf(&href_prev, "?page=%d",
834 gw_trans->page - 1) == -1) {
835 error = got_error_from_errno("asprintf");
836 goto done;
838 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
839 KATTR_HREF, href_prev, KATTR__MAX);
840 if (kerr != KCGI_OK)
841 goto done;
842 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
843 if (kerr != KCGI_OK)
844 goto done;
845 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
846 if (kerr != KCGI_OK)
847 goto done;
850 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
851 if (kerr != KCGI_OK)
852 return gw_kcgi_error(kerr);
854 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
855 next_disp == gw_trans->gw_conf->got_max_repos_display &&
856 dir_c != (gw_trans->page + 1) *
857 gw_trans->gw_conf->got_max_repos_display) {
858 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
859 KATTR_ID, "nav_next", KATTR__MAX);
860 if (kerr != KCGI_OK)
861 goto done;
862 if (asprintf(&href_next, "?page=%d",
863 gw_trans->page + 1) == -1) {
864 error = got_error_from_errno("calloc");
865 goto done;
867 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
868 KATTR_HREF, href_next, KATTR__MAX);
869 if (kerr != KCGI_OK)
870 goto done;
871 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
872 if (kerr != KCGI_OK)
873 goto done;
874 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
875 if (kerr != KCGI_OK)
876 goto done;
877 next_disp = 0;
878 break;
881 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
882 (gw_trans->page > 0) &&
883 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
884 prev_disp == gw_trans->repos_total)) {
885 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
886 if (kerr != KCGI_OK)
887 goto done;
889 next_disp++;
891 done:
892 free(href_prev);
893 free(href_next);
894 free(href_summary);
895 free(href_briefs);
896 free(href_commits);
897 free(href_tree);
898 if (error == NULL && kerr != KCGI_OK)
899 error = gw_kcgi_error(kerr);
900 return error;
903 static const struct got_error *
904 gw_commits(struct gw_trans *gw_trans)
906 const struct got_error *error = NULL;
907 struct gw_header *header = NULL, *n_header = NULL;
908 char *age = NULL;
909 char *href_diff = NULL, *href_blob = NULL;
910 enum kcgi_err kerr = KCGI_OK;
912 if ((header = gw_init_header()) == NULL)
913 return got_error_from_errno("malloc");
915 if (pledge("stdio rpath proc exec sendfd unveil",
916 NULL) == -1) {
917 error = got_error_from_errno("pledge");
918 goto done;
921 error = gw_apply_unveil(gw_trans->gw_dir->path);
922 if (error)
923 goto done;
925 error = gw_get_header(gw_trans, header,
926 gw_trans->gw_conf->got_max_commits_display);
927 if (error)
928 goto done;
930 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
931 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
932 "commits_line_wrapper", KATTR__MAX);
933 if (kerr != KCGI_OK)
934 goto done;
935 error = gw_gen_commit_header(gw_trans, n_header->commit_id,
936 n_header->refs_str);
937 if (error)
938 goto done;
939 error = gw_gen_author_header(gw_trans, n_header->author);
940 if (error)
941 goto done;
942 error = gw_gen_committer_header(gw_trans, n_header->author);
943 if (error)
944 goto done;
945 error = gw_get_time_str(&age, n_header->committer_time,
946 TM_LONG);
947 if (error)
948 goto done;
949 error = gw_gen_age_header(gw_trans, age ?age : "");
950 if (error)
951 goto done;
952 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
953 if (kerr != KCGI_OK)
954 goto done;
956 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
957 "dotted_line", KATTR__MAX);
958 if (kerr != KCGI_OK)
959 goto done;
960 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
961 if (kerr != KCGI_OK)
962 goto done;
964 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
965 "commit", KATTR__MAX);
966 if (kerr != KCGI_OK)
967 goto done;
968 kerr = khttp_puts(gw_trans->gw_req, n_header->commit_msg);
969 if (kerr != KCGI_OK)
970 goto done;
971 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
972 if (kerr != KCGI_OK)
973 goto done;
975 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
976 gw_trans->repo_name, n_header->commit_id) == -1) {
977 error = got_error_from_errno("asprintf");
978 goto done;
980 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
981 KATTR_ID, "navs_wrapper", KATTR__MAX);
982 if (kerr != KCGI_OK)
983 goto done;
984 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
985 KATTR_ID, "navs", KATTR__MAX);
986 if (kerr != KCGI_OK)
987 goto done;
988 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
989 KATTR_HREF, href_diff, KATTR__MAX);
990 if (kerr != KCGI_OK)
991 goto done;
992 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
993 if (kerr != KCGI_OK)
994 goto done;
995 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
996 if (kerr != KCGI_OK)
997 goto done;
999 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1000 if (kerr != KCGI_OK)
1001 goto done;
1003 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
1004 gw_trans->repo_name, n_header->commit_id) == -1) {
1005 error = got_error_from_errno("asprintf");
1006 goto done;
1008 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1009 KATTR_HREF, href_blob, KATTR__MAX);
1010 if (kerr != KCGI_OK)
1011 goto done;
1012 khtml_puts(gw_trans->gw_html_req, "tree");
1013 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1014 if (kerr != KCGI_OK)
1015 goto done;
1016 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1017 if (kerr != KCGI_OK)
1018 goto done;
1020 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1021 "solid_line", KATTR__MAX);
1022 if (kerr != KCGI_OK)
1023 goto done;
1024 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1025 if (kerr != KCGI_OK)
1026 goto done;
1028 free(age);
1029 age = NULL;
1031 done:
1032 got_ref_list_free(&header->refs);
1033 gw_free_headers(header);
1034 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1035 gw_free_headers(n_header);
1036 free(age);
1037 free(href_diff);
1038 free(href_blob);
1039 if (error == NULL && kerr != KCGI_OK)
1040 error = gw_kcgi_error(kerr);
1041 return error;
1044 static const struct got_error *
1045 gw_briefs(struct gw_trans *gw_trans)
1047 const struct got_error *error = NULL;
1048 struct gw_header *header = NULL, *n_header = NULL;
1049 char *age = NULL, *age_html = NULL;
1050 char *href_diff = NULL, *href_blob = NULL;
1051 char *newline, *smallerthan;
1052 enum kcgi_err kerr = KCGI_OK;
1054 if ((header = gw_init_header()) == NULL)
1055 return got_error_from_errno("malloc");
1057 if (pledge("stdio rpath proc exec sendfd unveil",
1058 NULL) == -1) {
1059 error = got_error_from_errno("pledge");
1060 goto done;
1063 error = gw_apply_unveil(gw_trans->gw_dir->path);
1064 if (error)
1065 goto done;
1067 if (gw_trans->action == GW_SUMMARY)
1068 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
1069 else
1070 error = gw_get_header(gw_trans, header,
1071 gw_trans->gw_conf->got_max_commits_display);
1072 if (error)
1073 goto done;
1075 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
1076 error = gw_get_time_str(&age, n_header->committer_time,
1077 TM_DIFF);
1078 if (error)
1079 goto done;
1081 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1082 KATTR_ID, "briefs_wrapper", KATTR__MAX);
1083 if (kerr != KCGI_OK)
1084 goto done;
1086 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1087 KATTR_ID, "briefs_age", KATTR__MAX);
1088 if (kerr != KCGI_OK)
1089 goto done;
1090 if (asprintf(&age_html, "%s", age ? age : "") == -1) {
1091 error = got_error_from_errno("asprintf");
1092 goto done;
1094 kerr = khtml_puts(gw_trans->gw_html_req, age_html);
1095 if (kerr != KCGI_OK)
1096 goto done;
1097 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1098 if (kerr != KCGI_OK)
1099 goto done;
1101 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1102 KATTR_ID, "briefs_author", KATTR__MAX);
1103 if (kerr != KCGI_OK)
1104 goto done;
1105 smallerthan = strchr(n_header->author, '<');
1106 if (smallerthan)
1107 *smallerthan = '\0';
1108 kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
1109 if (kerr != KCGI_OK)
1110 goto done;
1111 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1112 if (kerr != KCGI_OK)
1113 goto done;
1115 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
1116 gw_trans->repo_name, n_header->commit_id) == -1) {
1117 error = got_error_from_errno("asprintf");
1118 goto done;
1120 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1121 KATTR_ID, "briefs_log", KATTR__MAX);
1122 if (kerr != KCGI_OK)
1123 goto done;
1124 newline = strchr(n_header->commit_msg, '\n');
1125 if (newline)
1126 *newline = '\0';
1127 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1128 KATTR_HREF, href_diff, KATTR__MAX);
1129 if (kerr != KCGI_OK)
1130 goto done;
1131 kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
1132 if (kerr != KCGI_OK)
1133 goto done;
1134 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1135 if (kerr != KCGI_OK)
1136 goto done;
1138 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1139 KATTR_ID, "navs_wrapper", KATTR__MAX);
1140 if (kerr != KCGI_OK)
1141 goto done;
1142 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1143 KATTR_ID, "navs", KATTR__MAX);
1144 if (kerr != KCGI_OK)
1145 goto done;
1146 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1147 KATTR_HREF, href_diff, KATTR__MAX);
1148 if (kerr != KCGI_OK)
1149 goto done;
1150 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
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_puts(gw_trans->gw_html_req, " | ");
1158 if (kerr != KCGI_OK)
1159 goto done;
1161 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
1162 gw_trans->repo_name, n_header->commit_id) == -1) {
1163 error = got_error_from_errno("asprintf");
1164 goto done;
1166 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1167 KATTR_HREF, href_blob, KATTR__MAX);
1168 if (kerr != KCGI_OK)
1169 goto done;
1170 khtml_puts(gw_trans->gw_html_req, "tree");
1171 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1172 if (kerr != KCGI_OK)
1173 goto done;
1174 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1175 if (kerr != KCGI_OK)
1176 goto done;
1178 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1179 KATTR_ID, "dotted_line", KATTR__MAX);
1180 if (kerr != KCGI_OK)
1181 goto done;
1182 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1183 if (kerr != KCGI_OK)
1184 goto done;
1186 free(age);
1187 age = NULL;
1188 free(age_html);
1189 age_html = NULL;
1190 free(href_diff);
1191 href_diff = NULL;
1192 free(href_blob);
1193 href_blob = NULL;
1195 done:
1196 got_ref_list_free(&header->refs);
1197 gw_free_headers(header);
1198 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1199 gw_free_headers(n_header);
1200 free(age);
1201 free(age_html);
1202 free(href_diff);
1203 free(href_blob);
1204 if (error == NULL && kerr != KCGI_OK)
1205 error = gw_kcgi_error(kerr);
1206 return error;
1209 static const struct got_error *
1210 gw_summary(struct gw_trans *gw_trans)
1212 const struct got_error *error = NULL;
1213 char *age = NULL;
1214 enum kcgi_err kerr = KCGI_OK;
1216 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1217 return got_error_from_errno("pledge");
1219 /* unveil is applied with gw_briefs below */
1221 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1222 "summary_wrapper", KATTR__MAX);
1223 if (kerr != KCGI_OK)
1224 return gw_kcgi_error(kerr);
1226 if (gw_trans->gw_conf->got_show_repo_description &&
1227 gw_trans->gw_dir->description != NULL &&
1228 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1229 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1230 KATTR_ID, "description_title", KATTR__MAX);
1231 if (kerr != KCGI_OK)
1232 goto done;
1233 kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1234 if (kerr != KCGI_OK)
1235 goto done;
1236 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1237 if (kerr != KCGI_OK)
1238 goto done;
1239 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1240 KATTR_ID, "description", KATTR__MAX);
1241 if (kerr != KCGI_OK)
1242 goto done;
1243 kerr = khtml_puts(gw_trans->gw_html_req,
1244 gw_trans->gw_dir->description);
1245 if (kerr != KCGI_OK)
1246 goto done;
1247 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1248 if (kerr != KCGI_OK)
1249 goto done;
1252 if (gw_trans->gw_conf->got_show_repo_owner &&
1253 gw_trans->gw_dir->owner != NULL &&
1254 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
1255 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1256 KATTR_ID, "repo_owner_title", KATTR__MAX);
1257 if (kerr != KCGI_OK)
1258 goto done;
1259 kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1260 if (kerr != KCGI_OK)
1261 goto done;
1262 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1263 if (kerr != KCGI_OK)
1264 goto done;
1265 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1266 KATTR_ID, "repo_owner", KATTR__MAX);
1267 if (kerr != KCGI_OK)
1268 goto done;
1269 kerr = khtml_puts(gw_trans->gw_html_req,
1270 gw_trans->gw_dir->owner);
1271 if (kerr != KCGI_OK)
1272 goto done;
1273 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1274 if (kerr != KCGI_OK)
1275 goto done;
1278 if (gw_trans->gw_conf->got_show_repo_age) {
1279 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1280 "refs/heads", TM_LONG);
1281 if (error)
1282 goto done;
1283 if (age != NULL) {
1284 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1285 KATTR_ID, "last_change_title", KATTR__MAX);
1286 if (kerr != KCGI_OK)
1287 goto done;
1288 kerr = khtml_puts(gw_trans->gw_html_req,
1289 "Last Change: ");
1290 if (kerr != KCGI_OK)
1291 goto done;
1292 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1293 if (kerr != KCGI_OK)
1294 goto done;
1295 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1296 KATTR_ID, "last_change", KATTR__MAX);
1297 if (kerr != KCGI_OK)
1298 goto done;
1299 kerr = khtml_puts(gw_trans->gw_html_req, age);
1300 if (kerr != KCGI_OK)
1301 goto done;
1302 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1303 if (kerr != KCGI_OK)
1304 goto done;
1308 if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1309 gw_trans->gw_dir->url != NULL &&
1310 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1311 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1312 KATTR_ID, "cloneurl_title", KATTR__MAX);
1313 if (kerr != KCGI_OK)
1314 goto done;
1315 kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1316 if (kerr != KCGI_OK)
1317 goto done;
1318 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1319 if (kerr != KCGI_OK)
1320 goto done;
1321 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1322 KATTR_ID, "cloneurl", KATTR__MAX);
1323 if (kerr != KCGI_OK)
1324 goto done;
1325 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1326 if (kerr != KCGI_OK)
1327 goto done;
1328 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1329 if (kerr != KCGI_OK)
1330 goto done;
1333 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1334 if (kerr != KCGI_OK)
1335 goto done;
1337 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1338 "briefs_title_wrapper", KATTR__MAX);
1339 if (kerr != KCGI_OK)
1340 goto done;
1341 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1342 "briefs_title", KATTR__MAX);
1343 if (kerr != KCGI_OK)
1344 goto done;
1345 kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1346 if (kerr != KCGI_OK)
1347 goto done;
1348 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1349 if (kerr != KCGI_OK)
1350 goto done;
1351 error = gw_briefs(gw_trans);
1352 if (error)
1353 goto done;
1355 error = gw_output_repo_tags(gw_trans, NULL, D_MAXSLCOMMDISP,
1356 TAGBRIEF);
1357 if (error)
1358 goto done;
1360 error = gw_output_repo_heads(gw_trans);
1361 done:
1362 free(age);
1363 if (error == NULL && kerr != KCGI_OK)
1364 error = gw_kcgi_error(kerr);
1365 return error;
1368 static const struct got_error *
1369 gw_tree(struct gw_trans *gw_trans)
1371 const struct got_error *error = NULL;
1372 struct gw_header *header = NULL;
1373 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1374 char *age = NULL, *age_html = NULL;
1375 enum kcgi_err kerr;
1377 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1378 return got_error_from_errno("pledge");
1380 if ((header = gw_init_header()) == NULL)
1381 return got_error_from_errno("malloc");
1383 error = gw_apply_unveil(gw_trans->gw_dir->path);
1384 if (error)
1385 goto done;
1387 error = gw_get_header(gw_trans, header, 1);
1388 if (error)
1389 goto done;
1391 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1392 "tree_header_wrapper", KATTR__MAX);
1393 if (kerr != KCGI_OK)
1394 goto done;
1395 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1396 "tree_header", KATTR__MAX);
1397 if (kerr != KCGI_OK)
1398 goto done;
1399 error = gw_gen_tree_header(gw_trans, header->tree_id);
1400 if (error)
1401 goto done;
1402 error = gw_get_time_str(&age, header->committer_time,
1403 TM_LONG);
1404 if (error)
1405 goto done;
1406 error = gw_gen_age_header(gw_trans, age ?age : "");
1407 if (error)
1408 goto done;
1409 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1410 if (error)
1411 goto done;
1412 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1413 if (kerr != KCGI_OK)
1414 goto done;
1415 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1416 "dotted_line", KATTR__MAX);
1417 if (kerr != KCGI_OK)
1418 goto done;
1419 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1420 if (kerr != KCGI_OK)
1421 goto done;
1423 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1424 "tree", KATTR__MAX);
1425 if (kerr != KCGI_OK)
1426 goto done;
1427 error = gw_output_repo_tree(gw_trans);
1428 if (error)
1429 goto done;
1431 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1432 done:
1433 got_ref_list_free(&header->refs);
1434 gw_free_headers(header);
1435 free(tree_html_disp);
1436 free(tree_html);
1437 free(tree);
1438 free(age);
1439 free(age_html);
1440 if (error == NULL && kerr != KCGI_OK)
1441 error = gw_kcgi_error(kerr);
1442 return error;
1445 static const struct got_error *
1446 gw_tag(struct gw_trans *gw_trans)
1448 const struct got_error *error = NULL;
1449 struct gw_header *header = NULL;
1450 enum kcgi_err kerr;
1452 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1453 return got_error_from_errno("pledge");
1455 if ((header = gw_init_header()) == NULL)
1456 return got_error_from_errno("malloc");
1458 error = gw_apply_unveil(gw_trans->gw_dir->path);
1459 if (error)
1460 goto done;
1462 error = gw_get_header(gw_trans, header, 1);
1463 if (error)
1464 goto done;
1466 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1467 "tag_header_wrapper", KATTR__MAX);
1468 if (kerr != KCGI_OK)
1469 goto done;
1470 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1471 "tag_header", KATTR__MAX);
1472 if (kerr != KCGI_OK)
1473 goto done;
1474 error = gw_gen_commit_header(gw_trans, header->commit_id,
1475 header->refs_str);
1476 if (error)
1477 goto done;
1478 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1479 if (error)
1480 goto done;
1481 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1482 if (kerr != KCGI_OK)
1483 goto done;
1484 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1485 "dotted_line", KATTR__MAX);
1486 if (kerr != KCGI_OK)
1487 goto done;
1488 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1489 if (kerr != KCGI_OK)
1490 goto done;
1492 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1493 "tree", KATTR__MAX);
1494 if (kerr != KCGI_OK)
1495 goto done;
1497 error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1498 if (error)
1499 goto done;
1501 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1502 done:
1503 got_ref_list_free(&header->refs);
1504 gw_free_headers(header);
1505 if (error == NULL && kerr != KCGI_OK)
1506 error = gw_kcgi_error(kerr);
1507 return error;
1510 static const struct got_error *
1511 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1513 const struct got_error *error = NULL;
1514 DIR *dt;
1515 char *dir_test;
1516 int opened = 0;
1518 if (asprintf(&dir_test, "%s/%s/%s",
1519 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1520 GOTWEB_GIT_DIR) == -1)
1521 return got_error_from_errno("asprintf");
1523 dt = opendir(dir_test);
1524 if (dt == NULL) {
1525 free(dir_test);
1526 } else {
1527 error = gw_strdup_string(&gw_dir->path, dir_test, NULL);
1528 opened = 1;
1529 if (error)
1530 goto errored;
1531 goto done;
1534 if (asprintf(&dir_test, "%s/%s/%s",
1535 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1536 GOTWEB_GOT_DIR) == -1)
1537 return got_error_from_errno("asprintf");
1539 dt = opendir(dir_test);
1540 if (dt == NULL)
1541 free(dir_test);
1542 else {
1543 opened = 1;
1544 error = got_error(GOT_ERR_NOT_GIT_REPO);
1545 goto errored;
1548 if (asprintf(&dir_test, "%s/%s",
1549 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1)
1550 return got_error_from_errno("asprintf");
1552 error = gw_strdup_string(&gw_dir->path, dir_test, NULL);
1553 if (error) {
1554 opened = 1;
1555 goto errored;
1557 done:
1558 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1559 gw_dir->path);
1560 if (error)
1561 goto errored;
1562 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1563 if (error)
1564 goto errored;
1565 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1566 "refs/heads", TM_DIFF);
1567 if (error)
1568 goto errored;
1569 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1570 errored:
1571 free(dir_test);
1572 if (opened)
1573 closedir(dt);
1574 return error;
1577 static const struct got_error *
1578 gw_load_got_paths(struct gw_trans *gw_trans)
1580 const struct got_error *error = NULL;
1581 DIR *d;
1582 struct dirent **sd_dent;
1583 struct gw_dir *gw_dir;
1584 struct stat st;
1585 unsigned int d_cnt, d_i;
1587 d = opendir(gw_trans->gw_conf->got_repos_path);
1588 if (d == NULL) {
1589 error = got_error_from_errno2("opendir",
1590 gw_trans->gw_conf->got_repos_path);
1591 return error;
1594 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1595 alphasort);
1596 if (d_cnt == -1) {
1597 error = got_error_from_errno2("scandir",
1598 gw_trans->gw_conf->got_repos_path);
1599 return error;
1602 for (d_i = 0; d_i < d_cnt; d_i++) {
1603 if (gw_trans->gw_conf->got_max_repos > 0 &&
1604 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1605 break; /* account for parent and self */
1607 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1608 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1609 continue;
1611 error = gw_init_gw_dir(&gw_dir, sd_dent[d_i]->d_name);
1612 if (error)
1613 return error;
1615 error = gw_load_got_path(gw_trans, gw_dir);
1616 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1617 error = NULL;
1618 continue;
1620 else if (error)
1621 return error;
1623 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1624 !got_path_dir_is_empty(gw_dir->path)) {
1625 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1626 entry);
1627 gw_trans->repos_total++;
1631 closedir(d);
1632 return error;
1635 static const struct got_error *
1636 gw_parse_querystring(struct gw_trans *gw_trans)
1638 const struct got_error *error = NULL;
1639 struct kpair *p;
1640 struct gw_query_action *action = NULL;
1641 unsigned int i;
1643 if (gw_trans->gw_req->fieldnmap[0]) {
1644 error = got_error_from_errno("bad parse");
1645 return error;
1646 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1647 /* define gw_trans->repo_path */
1648 gw_trans->repo_name = p->parsed.s;
1650 if (asprintf(&gw_trans->repo_path, "%s/%s",
1651 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
1652 return got_error_from_errno("asprintf");
1654 /* get action and set function */
1655 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
1656 for (i = 0; i < nitems(gw_query_funcs); i++) {
1657 action = &gw_query_funcs[i];
1658 if (action->func_name == NULL ||
1659 strcmp(action->func_name, p->parsed.s))
1660 continue;
1662 gw_trans->action = i;
1663 break;
1666 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID])) {
1667 if (asprintf(&gw_trans->commit, "%s",
1668 p->parsed.s) == -1)
1669 return got_error_from_errno("asprintf");
1672 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE])) {
1673 if (asprintf(&gw_trans->repo_file, "%s",
1674 p->parsed.s) == -1)
1675 return got_error_from_errno("asprintf");
1678 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER])) {
1679 if (asprintf(&gw_trans->repo_folder, "%s",
1680 p->parsed.s) == -1)
1681 return got_error_from_errno("asprintf");
1684 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF])) {
1685 if (asprintf(&gw_trans->headref, "%s",
1686 p->parsed.s) == -1)
1687 return got_error_from_errno("asprintf");
1690 if (action == NULL) {
1691 error = got_error_from_errno("invalid action");
1692 return error;
1694 error = gw_init_gw_dir(&gw_trans->gw_dir, gw_trans->repo_name);
1695 if (error)
1696 return error;
1698 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1699 if (error)
1700 return error;
1701 } else
1702 gw_trans->action = GW_INDEX;
1704 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1705 gw_trans->page = p->parsed.i;
1707 return error;
1710 static const struct got_error *
1711 gw_init_gw_dir(struct gw_dir **gw_dir, const char *dir)
1713 const struct got_error *error;
1715 *gw_dir = malloc(sizeof(**gw_dir));
1716 if (*gw_dir == NULL)
1717 return got_error_from_errno("malloc");
1719 if (asprintf(&(*gw_dir)->name, "%s", dir) == -1) {
1720 error = got_error_from_errno("asprintf");
1721 free(*gw_dir);
1722 *gw_dir = NULL;
1723 return NULL;
1726 return NULL;
1729 static const struct got_error *
1730 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1732 enum kcgi_err kerr;
1734 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1735 if (kerr != KCGI_OK)
1736 return gw_kcgi_error(kerr);
1737 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1738 khttps[code]);
1739 if (kerr != KCGI_OK)
1740 return gw_kcgi_error(kerr);
1741 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1742 kmimetypes[mime]);
1743 if (kerr != KCGI_OK)
1744 return gw_kcgi_error(kerr);
1745 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
1746 "nosniff");
1747 if (kerr != KCGI_OK)
1748 return gw_kcgi_error(kerr);
1749 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1750 if (kerr != KCGI_OK)
1751 return gw_kcgi_error(kerr);
1752 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
1753 "1; mode=block");
1754 if (kerr != KCGI_OK)
1755 return gw_kcgi_error(kerr);
1757 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
1758 kerr = khttp_head(gw_trans->gw_req,
1759 kresps[KRESP_CONTENT_DISPOSITION],
1760 "attachment; filename=%s", gw_trans->repo_file);
1761 if (kerr != KCGI_OK)
1762 return gw_kcgi_error(kerr);
1765 kerr = khttp_body(gw_trans->gw_req);
1766 return gw_kcgi_error(kerr);
1769 static const struct got_error *
1770 gw_display_index(struct gw_trans *gw_trans)
1772 const struct got_error *error;
1773 enum kcgi_err kerr;
1775 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1776 if (error)
1777 return error;
1779 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1780 if (kerr != KCGI_OK)
1781 return gw_kcgi_error(kerr);
1783 if (gw_trans->action != GW_BLOB) {
1784 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1785 gw_query_funcs[gw_trans->action].template);
1786 if (kerr != KCGI_OK) {
1787 khtml_close(gw_trans->gw_html_req);
1788 return gw_kcgi_error(kerr);
1792 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
1795 static void
1796 gw_display_error(struct gw_trans *gw_trans, const struct got_error *err)
1798 if (gw_display_open(gw_trans, KHTTP_200, gw_trans->mime) != NULL)
1799 return;
1801 if (khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0) != KCGI_OK)
1802 return;
1803 khtml_puts(gw_trans->gw_html_req, err->msg);
1804 khtml_close(gw_trans->gw_html_req);
1807 static int
1808 gw_template(size_t key, void *arg)
1810 const struct got_error *error = NULL;
1811 enum kcgi_err kerr;
1812 struct gw_trans *gw_trans = arg;
1813 char *img_src = NULL;
1815 switch (key) {
1816 case (TEMPL_HEAD):
1817 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1818 KATTR_NAME, "viewport",
1819 KATTR_CONTENT, "initial-scale=.75, user-scalable=yes",
1820 KATTR__MAX);
1821 if (kerr != KCGI_OK)
1822 return 0;
1823 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1824 if (kerr != KCGI_OK)
1825 return 0;
1826 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1827 KATTR_CHARSET, "utf-8",
1828 KATTR__MAX);
1829 if (kerr != KCGI_OK)
1830 return 0;
1831 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1832 if (kerr != KCGI_OK)
1833 return 0;
1834 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1835 KATTR_NAME, "msapplication-TileColor",
1836 KATTR_CONTENT, "#da532c", KATTR__MAX);
1837 if (kerr != KCGI_OK)
1838 return 0;
1839 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1840 if (kerr != KCGI_OK)
1841 return 0;
1842 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1843 KATTR_NAME, "theme-color",
1844 KATTR_CONTENT, "#ffffff", KATTR__MAX);
1845 if (kerr != KCGI_OK)
1846 return 0;
1847 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1848 if (kerr != KCGI_OK)
1849 return 0;
1850 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1851 KATTR_REL, "apple-touch-icon", KATTR_SIZES, "180x180",
1852 KATTR_HREF, "/apple-touch-icon.png", KATTR__MAX);
1853 if (kerr != KCGI_OK)
1854 return 0;
1855 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1856 if (kerr != KCGI_OK)
1857 return 0;
1858 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1859 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
1860 "32x32", KATTR_HREF, "/favicon-32x32.png", KATTR__MAX);
1861 if (kerr != KCGI_OK)
1862 return 0;
1863 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1864 if (kerr != KCGI_OK)
1865 return 0;
1866 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1867 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
1868 "16x16", KATTR_HREF, "/favicon-16x16.png", KATTR__MAX);
1869 if (kerr != KCGI_OK)
1870 return 0;
1871 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1872 if (kerr != KCGI_OK)
1873 return 0;
1874 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1875 KATTR_REL, "manifest", KATTR_HREF, "/site.webmanifest",
1876 KATTR__MAX);
1877 if (kerr != KCGI_OK)
1878 return 0;
1879 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1880 if (kerr != KCGI_OK)
1881 return 0;
1882 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1883 KATTR_REL, "mask-icon", KATTR_HREF,
1884 "/safari-pinned-tab.svg", KATTR__MAX);
1885 if (kerr != KCGI_OK)
1886 return 0;
1887 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1888 if (kerr != KCGI_OK)
1889 return 0;
1890 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1891 KATTR_REL, "stylesheet", KATTR_TYPE, "text/css",
1892 KATTR_HREF, "/gotweb.css", KATTR__MAX);
1893 if (kerr != KCGI_OK)
1894 return 0;
1895 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1896 if (kerr != KCGI_OK)
1897 return 0;
1898 break;
1899 case(TEMPL_HEADER):
1900 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1901 KATTR_ID, "got_link", KATTR__MAX);
1902 if (kerr != KCGI_OK)
1903 return 0;
1904 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1905 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
1906 KATTR_TARGET, "_sotd", KATTR__MAX);
1907 if (kerr != KCGI_OK)
1908 return 0;
1909 if (asprintf(&img_src, "/%s",
1910 gw_trans->gw_conf->got_logo) == -1)
1911 return 0;
1912 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
1913 KATTR_SRC, img_src, KATTR__MAX);
1914 if (kerr != KCGI_OK) {
1915 free(img_src);
1916 return 0;
1918 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1919 if (kerr != KCGI_OK) {
1920 free(img_src);
1921 return 0;
1923 break;
1924 case (TEMPL_SITEPATH):
1925 error = gw_output_site_link(gw_trans);
1926 if (error)
1927 return 0;
1928 break;
1929 case(TEMPL_TITLE):
1930 if (gw_trans->gw_conf->got_site_name != NULL) {
1931 kerr = khtml_puts(gw_trans->gw_html_req,
1932 gw_trans->gw_conf->got_site_name);
1933 if (kerr != KCGI_OK)
1934 return 0;
1936 break;
1937 case (TEMPL_SEARCH):
1938 break;
1939 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1940 "search", KATTR__MAX);
1941 if (kerr != KCGI_OK)
1942 return 0;
1943 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
1944 KATTR_METHOD, "POST", KATTR__MAX);
1945 if (kerr != KCGI_OK)
1946 return 0;
1947 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
1948 "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
1949 KATTR_MAXLENGTH, "50", KATTR__MAX);
1950 if (kerr != KCGI_OK)
1951 return 0;
1952 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
1953 KATTR__MAX);
1954 if (kerr != KCGI_OK)
1955 return 0;
1956 kerr = khtml_puts(gw_trans->gw_html_req, "Search");
1957 if (kerr != KCGI_OK)
1958 return 0;
1959 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
1960 if (kerr != KCGI_OK)
1961 return 0;
1962 break;
1963 case(TEMPL_SITEOWNER):
1964 if (gw_trans->gw_conf->got_site_owner != NULL &&
1965 gw_trans->gw_conf->got_show_site_owner) {
1966 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1967 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
1968 if (kerr != KCGI_OK)
1969 return 0;
1970 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1971 KATTR_ID, "site_owner", KATTR__MAX);
1972 if (kerr != KCGI_OK)
1973 return 0;
1974 kerr = khtml_puts(gw_trans->gw_html_req,
1975 gw_trans->gw_conf->got_site_owner);
1976 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1977 if (kerr != KCGI_OK)
1978 return 0;
1980 break;
1981 case(TEMPL_CONTENT):
1982 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1983 if (error) {
1984 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1985 KATTR_ID, "tmpl_err", KATTR__MAX);
1986 if (kerr != KCGI_OK)
1987 return 0;
1988 kerr = khttp_puts(gw_trans->gw_req, "Error: ");
1989 if (kerr != KCGI_OK)
1990 return 0;
1991 kerr = khttp_puts(gw_trans->gw_req, error->msg);
1992 if (kerr != KCGI_OK)
1993 return 0;
1994 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1995 if (kerr != KCGI_OK)
1996 return 0;
1998 break;
1999 default:
2000 return 0;
2002 return 1;
2005 static const struct got_error *
2006 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
2008 const struct got_error *error = NULL;
2009 char *ref_str = NULL;
2010 enum kcgi_err kerr = KCGI_OK;
2012 if (strcmp(str2, "") != 0) {
2013 if (asprintf(&ref_str, "(%s)", str2) == -1) {
2014 error = got_error_from_errno("asprintf");
2015 goto done;
2017 } else {
2018 error = gw_strdup_string(&ref_str, "", NULL);
2019 if (error)
2020 goto done;
2023 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2024 KATTR_ID, "header_commit_title", KATTR__MAX);
2025 if (kerr != KCGI_OK)
2026 goto done;
2027 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
2028 if (kerr != KCGI_OK)
2029 goto done;
2030 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2031 if (kerr != KCGI_OK)
2032 goto done;
2033 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2034 KATTR_ID, "header_commit", KATTR__MAX);
2035 if (kerr != KCGI_OK)
2036 goto done;
2037 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2038 if (kerr != KCGI_OK)
2039 goto done;
2040 kerr = khtml_puts(gw_trans->gw_html_req, " ");
2041 if (kerr != KCGI_OK)
2042 goto done;
2043 kerr = khtml_puts(gw_trans->gw_html_req, ref_str);
2044 if (kerr != KCGI_OK)
2045 goto done;
2046 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2047 done:
2048 if (error == NULL && kerr != KCGI_OK)
2049 error = gw_kcgi_error(kerr);
2050 return error;
2053 static const struct got_error *
2054 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2056 const struct got_error *error = NULL;
2057 enum kcgi_err kerr = KCGI_OK;
2059 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2060 KATTR_ID, "header_diff_title", KATTR__MAX);
2061 if (kerr != KCGI_OK)
2062 goto done;
2063 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2064 if (kerr != KCGI_OK)
2065 goto done;
2066 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2067 if (kerr != KCGI_OK)
2068 goto done;
2069 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2070 KATTR_ID, "header_diff", KATTR__MAX);
2071 if (kerr != KCGI_OK)
2072 goto done;
2073 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2074 if (kerr != KCGI_OK)
2075 goto done;
2076 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2077 if (kerr != KCGI_OK)
2078 goto done;
2079 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2080 if (kerr != KCGI_OK)
2081 goto done;
2082 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2083 done:
2084 if (error == NULL && kerr != KCGI_OK)
2085 error = gw_kcgi_error(kerr);
2086 return error;
2089 static const struct got_error *
2090 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2092 const struct got_error *error = NULL;
2093 enum kcgi_err kerr = KCGI_OK;
2095 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2096 KATTR_ID, "header_age_title", KATTR__MAX);
2097 if (kerr != KCGI_OK)
2098 goto done;
2099 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2100 if (kerr != KCGI_OK)
2101 goto done;
2102 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2103 if (kerr != KCGI_OK)
2104 goto done;
2105 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2106 KATTR_ID, "header_age", KATTR__MAX);
2107 if (kerr != KCGI_OK)
2108 goto done;
2109 kerr = khtml_puts(gw_trans->gw_html_req, str);
2110 if (kerr != KCGI_OK)
2111 goto done;
2112 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2113 done:
2114 if (error == NULL && kerr != KCGI_OK)
2115 error = gw_kcgi_error(kerr);
2116 return error;
2119 static const struct got_error *
2120 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2122 const struct got_error *error = NULL;
2123 enum kcgi_err kerr;
2125 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2126 KATTR_ID, "header_author_title", KATTR__MAX);
2127 if (kerr != KCGI_OK)
2128 goto done;
2129 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2130 if (kerr != KCGI_OK)
2131 goto done;
2132 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2133 if (kerr != KCGI_OK)
2134 goto done;
2135 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2136 KATTR_ID, "header_author", KATTR__MAX);
2137 if (kerr != KCGI_OK)
2138 goto done;
2139 kerr = khtml_puts(gw_trans->gw_html_req, str);
2140 if (kerr != KCGI_OK)
2141 goto done;
2142 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2143 done:
2144 if (error == NULL && kerr != KCGI_OK)
2145 error = gw_kcgi_error(kerr);
2146 return error;
2149 static const struct got_error *
2150 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2152 const struct got_error *error = NULL;
2153 enum kcgi_err kerr;
2155 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2156 KATTR_ID, "header_committer_title", KATTR__MAX);
2157 if (kerr != KCGI_OK)
2158 goto done;
2159 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2160 if (kerr != KCGI_OK)
2161 goto done;
2162 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2163 if (kerr != KCGI_OK)
2164 goto done;
2165 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2166 KATTR_ID, "header_committer", KATTR__MAX);
2167 if (kerr != KCGI_OK)
2168 goto done;
2169 kerr = khtml_puts(gw_trans->gw_html_req, str);
2170 if (kerr != KCGI_OK)
2171 goto done;
2172 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2173 done:
2174 if (error == NULL && kerr != KCGI_OK)
2175 error = gw_kcgi_error(kerr);
2176 return error;
2179 static const struct got_error *
2180 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2182 const struct got_error *error = NULL;
2183 enum kcgi_err kerr;
2185 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2186 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2187 if (kerr != KCGI_OK)
2188 goto done;
2189 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2190 if (kerr != KCGI_OK)
2191 goto done;
2192 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2193 if (kerr != KCGI_OK)
2194 goto done;
2195 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2196 KATTR_ID, "header_commit_msg", KATTR__MAX);
2197 if (kerr != KCGI_OK)
2198 goto done;
2199 kerr = khttp_puts(gw_trans->gw_req, str);
2200 if (kerr != KCGI_OK)
2201 goto done;
2202 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2203 done:
2204 if (error == NULL && kerr != KCGI_OK)
2205 error = gw_kcgi_error(kerr);
2206 return error;
2209 static const struct got_error *
2210 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2212 const struct got_error *error = NULL;
2213 enum kcgi_err kerr;
2215 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2216 KATTR_ID, "header_tree_title", KATTR__MAX);
2217 if (kerr != KCGI_OK)
2218 goto done;
2219 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2220 if (kerr != KCGI_OK)
2221 goto done;
2222 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2223 if (kerr != KCGI_OK)
2224 goto done;
2225 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2226 KATTR_ID, "header_tree", KATTR__MAX);
2227 if (kerr != KCGI_OK)
2228 goto done;
2229 kerr = khtml_puts(gw_trans->gw_html_req, str);
2230 if (kerr != KCGI_OK)
2231 goto done;
2232 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2233 done:
2234 if (error == NULL && kerr != KCGI_OK)
2235 error = gw_kcgi_error(kerr);
2236 return error;
2239 static const struct got_error *
2240 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2241 char *dir)
2243 const struct got_error *error = NULL;
2244 FILE *f = NULL;
2245 char *d_file = NULL;
2246 unsigned int len;
2247 size_t n;
2249 *description = NULL;
2250 if (gw_trans->gw_conf->got_show_repo_description == 0)
2251 return NULL;
2253 if (asprintf(&d_file, "%s/description", dir) == -1)
2254 return got_error_from_errno("asprintf");
2256 f = fopen(d_file, "r");
2257 if (f == NULL) {
2258 if (errno == ENOENT || errno == EACCES)
2259 return NULL;
2260 error = got_error_from_errno2("fopen", d_file);
2261 goto done;
2264 if (fseek(f, 0, SEEK_END) == -1) {
2265 error = got_ferror(f, GOT_ERR_IO);
2266 goto done;
2268 len = ftell(f);
2269 if (len == -1) {
2270 error = got_ferror(f, GOT_ERR_IO);
2271 goto done;
2273 if (fseek(f, 0, SEEK_SET) == -1) {
2274 error = got_ferror(f, GOT_ERR_IO);
2275 goto done;
2277 *description = calloc(len + 1, sizeof(**description));
2278 if (*description == NULL) {
2279 error = got_error_from_errno("calloc");
2280 goto done;
2283 n = fread(*description, 1, len, f);
2284 if (n == 0 && ferror(f))
2285 error = got_ferror(f, GOT_ERR_IO);
2286 done:
2287 if (f != NULL && fclose(f) == -1 && error == NULL)
2288 error = got_error_from_errno("fclose");
2289 free(d_file);
2290 return error;
2293 static const struct got_error *
2294 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2296 struct tm tm;
2297 time_t diff_time;
2298 char *years = "years ago", *months = "months ago";
2299 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2300 char *minutes = "minutes ago", *seconds = "seconds ago";
2301 char *now = "right now";
2302 char *s;
2303 char datebuf[29];
2305 *repo_age = NULL;
2307 switch (ref_tm) {
2308 case TM_DIFF:
2309 diff_time = time(NULL) - committer_time;
2310 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2311 if (asprintf(repo_age, "%lld %s",
2312 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2313 return got_error_from_errno("asprintf");
2314 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2315 if (asprintf(repo_age, "%lld %s",
2316 (diff_time / 60 / 60 / 24 / (365 / 12)),
2317 months) == -1)
2318 return got_error_from_errno("asprintf");
2319 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2320 if (asprintf(repo_age, "%lld %s",
2321 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2322 return got_error_from_errno("asprintf");
2323 } else if (diff_time > 60 * 60 * 24 * 2) {
2324 if (asprintf(repo_age, "%lld %s",
2325 (diff_time / 60 / 60 / 24), days) == -1)
2326 return got_error_from_errno("asprintf");
2327 } else if (diff_time > 60 * 60 * 2) {
2328 if (asprintf(repo_age, "%lld %s",
2329 (diff_time / 60 / 60), hours) == -1)
2330 return got_error_from_errno("asprintf");
2331 } else if (diff_time > 60 * 2) {
2332 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2333 minutes) == -1)
2334 return got_error_from_errno("asprintf");
2335 } else if (diff_time > 2) {
2336 if (asprintf(repo_age, "%lld %s", diff_time,
2337 seconds) == -1)
2338 return got_error_from_errno("asprintf");
2339 } else {
2340 if (asprintf(repo_age, "%s", now) == -1)
2341 return got_error_from_errno("asprintf");
2343 break;
2344 case TM_LONG:
2345 if (gmtime_r(&committer_time, &tm) == NULL)
2346 return got_error_from_errno("gmtime_r");
2348 s = asctime_r(&tm, datebuf);
2349 if (s == NULL)
2350 return got_error_from_errno("asctime_r");
2352 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2353 return got_error_from_errno("asprintf");
2354 break;
2356 return NULL;
2359 static const struct got_error *
2360 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2361 char *repo_ref, int ref_tm)
2363 const struct got_error *error = NULL;
2364 struct got_object_id *id = NULL;
2365 struct got_repository *repo = NULL;
2366 struct got_commit_object *commit = NULL;
2367 struct got_reflist_head refs;
2368 struct got_reflist_entry *re;
2369 struct got_reference *head_ref;
2370 int is_head = 0;
2371 time_t committer_time = 0, cmp_time = 0;
2372 char *refname;
2374 *repo_age = NULL;
2375 SIMPLEQ_INIT(&refs);
2377 if (repo_ref == NULL)
2378 return NULL;
2380 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
2381 is_head = 1;
2383 if (gw_trans->gw_conf->got_show_repo_age == 0)
2384 return NULL;
2386 error = got_repo_open(&repo, dir, NULL);
2387 if (error)
2388 goto done;
2390 if (is_head)
2391 error = got_ref_list(&refs, repo, "refs/heads",
2392 got_ref_cmp_by_name, NULL);
2393 else
2394 error = got_ref_list(&refs, repo, repo_ref,
2395 got_ref_cmp_by_name, NULL);
2396 if (error)
2397 goto done;
2399 SIMPLEQ_FOREACH(re, &refs, entry) {
2400 if (is_head) {
2401 error = gw_strdup_string(&refname, repo_ref, NULL);
2402 if (error)
2403 goto done;
2404 } else {
2405 error = gw_strdup_string(&refname, NULL,
2406 got_ref_get_name(re->ref));
2407 if (error)
2408 goto done;
2410 error = got_ref_open(&head_ref, repo, refname, 0);
2411 if (error)
2412 goto done;
2414 error = got_ref_resolve(&id, repo, head_ref);
2415 got_ref_close(head_ref);
2416 if (error)
2417 goto done;
2419 error = got_object_open_as_commit(&commit, repo, id);
2420 if (error)
2421 goto done;
2423 committer_time =
2424 got_object_commit_get_committer_time(commit);
2426 if (cmp_time < committer_time)
2427 cmp_time = committer_time;
2430 if (cmp_time != 0) {
2431 committer_time = cmp_time;
2432 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2434 done:
2435 got_ref_list_free(&refs);
2436 free(id);
2437 return error;
2440 static const struct got_error *
2441 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2443 const struct got_error *error;
2444 FILE *f = NULL;
2445 struct got_object_id *id1 = NULL, *id2 = NULL;
2446 char *label1 = NULL, *label2 = NULL, *line = NULL;
2447 int obj_type;
2448 size_t linesize = 0;
2449 ssize_t linelen;
2450 enum kcgi_err kerr = KCGI_OK;
2452 f = got_opentemp();
2453 if (f == NULL)
2454 return NULL;
2456 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
2457 if (error)
2458 goto done;
2460 if (strncmp(header->parent_id, "/dev/null", 9) != 0) {
2461 error = got_repo_match_object_id(&id1, &label1,
2462 header->parent_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2463 if (error)
2464 goto done;
2467 error = got_repo_match_object_id(&id2, &label2,
2468 header->commit_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2469 if (error)
2470 goto done;
2472 error = got_object_get_type(&obj_type, header->repo, id2);
2473 if (error)
2474 goto done;
2475 switch (obj_type) {
2476 case GOT_OBJ_TYPE_BLOB:
2477 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
2478 header->repo, f);
2479 break;
2480 case GOT_OBJ_TYPE_TREE:
2481 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
2482 header->repo, f);
2483 break;
2484 case GOT_OBJ_TYPE_COMMIT:
2485 error = got_diff_objects_as_commits(id1, id2, 3, 0,
2486 header->repo, f);
2487 break;
2488 default:
2489 error = got_error(GOT_ERR_OBJ_TYPE);
2491 if (error)
2492 goto done;
2494 if (fseek(f, 0, SEEK_SET) == -1) {
2495 error = got_ferror(f, GOT_ERR_IO);
2496 goto done;
2499 while ((linelen = getline(&line, &linesize, f)) != -1) {
2500 error = gw_colordiff_line(gw_trans, line);
2501 if (error)
2502 goto done;
2503 /* XXX: KHTML_PRETTY breaks this */
2504 kerr = khtml_puts(gw_trans->gw_html_req, line);
2505 if (kerr != KCGI_OK)
2506 goto done;
2507 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2508 if (kerr != KCGI_OK)
2509 goto done;
2511 if (linelen == -1 && ferror(f))
2512 error = got_error_from_errno("getline");
2513 done:
2514 if (f && fclose(f) == -1 && error == NULL)
2515 error = got_error_from_errno("fclose");
2516 free(line);
2517 free(label1);
2518 free(label2);
2519 free(id1);
2520 free(id2);
2522 if (error == NULL && kerr != KCGI_OK)
2523 error = gw_kcgi_error(kerr);
2524 return error;
2527 static const struct got_error *
2528 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2530 const struct got_error *error = NULL;
2531 struct got_repository *repo;
2532 const char *gitconfig_owner;
2534 *owner = NULL;
2536 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2537 return NULL;
2539 error = got_repo_open(&repo, dir, NULL);
2540 if (error)
2541 return error;
2542 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2543 if (gitconfig_owner)
2544 error = gw_strdup_string(owner, NULL, gitconfig_owner);
2545 got_repo_close(repo);
2546 return error;
2549 static const struct got_error *
2550 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2552 const struct got_error *error = NULL;
2553 FILE *f;
2554 char *d_file = NULL;
2555 unsigned int len;
2556 size_t n;
2558 *url = NULL;
2560 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2561 return got_error_from_errno("asprintf");
2563 f = fopen(d_file, "r");
2564 if (f == NULL) {
2565 if (errno != ENOENT && errno != EACCES)
2566 error = got_error_from_errno2("fopen", d_file);
2567 goto done;
2570 if (fseek(f, 0, SEEK_END) == -1) {
2571 error = got_ferror(f, GOT_ERR_IO);
2572 goto done;
2574 len = ftell(f);
2575 if (len == -1) {
2576 error = got_ferror(f, GOT_ERR_IO);
2577 goto done;
2579 if (fseek(f, 0, SEEK_SET) == -1) {
2580 error = got_ferror(f, GOT_ERR_IO);
2581 goto done;
2584 *url = calloc(len + 1, sizeof(**url));
2585 if (*url == NULL) {
2586 error = got_error_from_errno("calloc");
2587 goto done;
2590 n = fread(*url, 1, len, f);
2591 if (n == 0 && ferror(f))
2592 error = got_ferror(f, GOT_ERR_IO);
2593 done:
2594 if (f && fclose(f) == -1 && error == NULL)
2595 error = got_error_from_errno("fclose");
2596 free(d_file);
2597 return NULL;
2600 static const struct got_error *
2601 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
2602 int limit, int tag_type)
2604 const struct got_error *error = NULL;
2605 struct got_repository *repo = NULL;
2606 struct got_reflist_head refs;
2607 struct got_reflist_entry *re;
2608 char *age = NULL;
2609 char *id_str = NULL, *newline, *href_commits = NULL;
2610 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
2611 struct got_tag_object *tag = NULL;
2612 enum kcgi_err kerr = KCGI_OK;
2613 int summary_header_displayed = 0;
2615 SIMPLEQ_INIT(&refs);
2617 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2618 if (error)
2619 return error;
2621 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
2622 if (error)
2623 goto done;
2625 SIMPLEQ_FOREACH(re, &refs, entry) {
2626 const char *refname;
2627 const char *tagger;
2628 const char *tag_commit;
2629 time_t tagger_time;
2630 struct got_object_id *id;
2631 struct got_commit_object *commit = NULL;
2633 refname = got_ref_get_name(re->ref);
2634 if (strncmp(refname, "refs/tags/", 10) != 0)
2635 continue;
2636 refname += 10;
2638 error = got_ref_resolve(&id, repo, re->ref);
2639 if (error)
2640 goto done;
2642 error = got_object_open_as_tag(&tag, repo, id);
2643 if (error) {
2644 if (error->code != GOT_ERR_OBJ_TYPE) {
2645 free(id);
2646 goto done;
2648 /* "lightweight" tag */
2649 error = got_object_open_as_commit(&commit, repo, id);
2650 if (error) {
2651 free(id);
2652 goto done;
2654 tagger = got_object_commit_get_committer(commit);
2655 tagger_time =
2656 got_object_commit_get_committer_time(commit);
2657 error = got_object_id_str(&id_str, id);
2658 free(id);
2659 } else {
2660 free(id);
2661 tagger = got_object_tag_get_tagger(tag);
2662 tagger_time = got_object_tag_get_tagger_time(tag);
2663 error = got_object_id_str(&id_str,
2664 got_object_tag_get_object_id(tag));
2666 if (error)
2667 goto done;
2669 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
2670 strlen(id_str)) != 0)
2671 continue;
2673 if (commit) {
2674 error = got_object_commit_get_logmsg(&tag_commit0,
2675 commit);
2676 if (error)
2677 goto done;
2678 got_object_commit_close(commit);
2679 } else {
2680 tag_commit0 = strdup(got_object_tag_get_message(tag));
2681 if (tag_commit0 == NULL) {
2682 error = got_error_from_errno("strdup");
2683 goto done;
2687 tag_commit = tag_commit0;
2688 while (*tag_commit == '\n')
2689 tag_commit++;
2691 switch (tag_type) {
2692 case TAGBRIEF:
2693 newline = strchr(tag_commit, '\n');
2694 if (newline)
2695 *newline = '\0';
2697 if (summary_header_displayed == 0) {
2698 kerr = khtml_attr(gw_trans->gw_html_req,
2699 KELEM_DIV, KATTR_ID,
2700 "summary_tags_title_wrapper", KATTR__MAX);
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_title", KATTR__MAX);
2706 if (kerr != KCGI_OK)
2707 goto done;
2708 kerr = khtml_puts(gw_trans->gw_html_req,
2709 "Tags");
2710 if (kerr != KCGI_OK)
2711 goto done;
2712 kerr = khtml_closeelem(gw_trans->gw_html_req,
2713 2);
2714 if (kerr != KCGI_OK)
2715 goto done;
2716 kerr = khtml_attr(gw_trans->gw_html_req,
2717 KELEM_DIV, KATTR_ID,
2718 "summary_tags_content", KATTR__MAX);
2719 if (kerr != KCGI_OK)
2720 goto done;
2721 summary_header_displayed = 1;
2724 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2725 KATTR_ID, "tags_wrapper", KATTR__MAX);
2726 if (kerr != KCGI_OK)
2727 goto done;
2728 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2729 KATTR_ID, "tags_age", KATTR__MAX);
2730 if (kerr != KCGI_OK)
2731 goto done;
2732 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
2733 if (error)
2734 goto done;
2735 kerr = khtml_puts(gw_trans->gw_html_req,
2736 age ? age : "");
2737 if (kerr != KCGI_OK)
2738 goto done;
2739 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2740 if (kerr != KCGI_OK)
2741 goto done;
2742 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2743 KATTR_ID, "tags", KATTR__MAX);
2744 if (kerr != KCGI_OK)
2745 goto done;
2746 kerr = khtml_puts(gw_trans->gw_html_req, refname);
2747 if (kerr != KCGI_OK)
2748 goto done;
2749 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2750 if (kerr != KCGI_OK)
2751 goto done;
2752 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2753 KATTR_ID, "tags_name", KATTR__MAX);
2754 if (kerr != KCGI_OK)
2755 goto done;
2756 if (asprintf(&href_tag, "?path=%s&action=tag&commit=%s",
2757 gw_trans->repo_name, id_str) == -1) {
2758 error = got_error_from_errno("asprintf");
2759 goto done;
2761 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2762 KATTR_HREF, href_tag, KATTR__MAX);
2763 if (kerr != KCGI_OK)
2764 goto done;
2765 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
2766 if (kerr != KCGI_OK)
2767 goto done;
2768 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2769 if (kerr != KCGI_OK)
2770 goto done;
2772 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2773 KATTR_ID, "navs_wrapper", KATTR__MAX);
2774 if (kerr != KCGI_OK)
2775 goto done;
2776 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2777 KATTR_ID, "navs", KATTR__MAX);
2778 if (kerr != KCGI_OK)
2779 goto done;
2781 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2782 KATTR_HREF, href_tag, KATTR__MAX);
2783 if (kerr != KCGI_OK)
2784 goto done;
2785 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
2786 if (kerr != KCGI_OK)
2787 goto done;
2788 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2789 if (kerr != KCGI_OK)
2790 goto done;
2792 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2793 if (kerr != KCGI_OK)
2794 goto done;
2795 if (asprintf(&href_briefs,
2796 "?path=%s&action=briefs&commit=%s",
2797 gw_trans->repo_name, id_str) == -1) {
2798 error = got_error_from_errno("asprintf");
2799 goto done;
2801 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2802 KATTR_HREF, href_briefs, KATTR__MAX);
2803 if (kerr != KCGI_OK)
2804 goto done;
2805 kerr = khtml_puts(gw_trans->gw_html_req,
2806 "commit briefs");
2807 if (kerr != KCGI_OK)
2808 goto done;
2809 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2810 if (kerr != KCGI_OK)
2811 goto done;
2813 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2814 if (kerr != KCGI_OK)
2815 goto done;
2817 if (asprintf(&href_commits,
2818 "?path=%s&action=commits&commit=%s",
2819 gw_trans->repo_name, id_str) == -1) {
2820 error = got_error_from_errno("asprintf");
2821 goto done;
2823 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2824 KATTR_HREF, href_commits, KATTR__MAX);
2825 if (kerr != KCGI_OK)
2826 goto done;
2827 kerr = khtml_puts(gw_trans->gw_html_req,
2828 "commits");
2829 if (kerr != KCGI_OK)
2830 goto done;
2831 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2832 if (kerr != KCGI_OK)
2833 goto done;
2835 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2836 KATTR_ID, "dotted_line", KATTR__MAX);
2837 if (kerr != KCGI_OK)
2838 goto done;
2839 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2840 if (kerr != KCGI_OK)
2841 goto done;
2842 break;
2843 case TAGFULL:
2844 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2845 KATTR_ID, "tag_info_date_title", KATTR__MAX);
2846 if (kerr != KCGI_OK)
2847 goto done;
2848 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
2849 if (kerr != KCGI_OK)
2850 goto done;
2851 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2852 if (kerr != KCGI_OK)
2853 goto done;
2854 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2855 KATTR_ID, "tag_info_date", KATTR__MAX);
2856 if (kerr != KCGI_OK)
2857 goto done;
2858 error = gw_get_time_str(&age, tagger_time, TM_LONG);
2859 if (error)
2860 goto done;
2861 kerr = khtml_puts(gw_trans->gw_html_req,
2862 age ? age : "");
2863 if (kerr != KCGI_OK)
2864 goto done;
2865 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2866 if (kerr != KCGI_OK)
2867 goto done;
2869 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2870 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
2871 if (kerr != KCGI_OK)
2872 goto done;
2873 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
2874 if (kerr != KCGI_OK)
2875 goto done;
2876 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2877 if (kerr != KCGI_OK)
2878 goto done;
2879 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2880 KATTR_ID, "tag_info_date", KATTR__MAX);
2881 if (kerr != KCGI_OK)
2882 goto done;
2883 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
2884 if (kerr != KCGI_OK)
2885 goto done;
2886 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2887 if (kerr != KCGI_OK)
2888 goto done;
2890 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2891 KATTR_ID, "tag_info", KATTR__MAX);
2892 if (kerr != KCGI_OK)
2893 goto done;
2894 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
2895 if (kerr != KCGI_OK)
2896 goto done;
2897 break;
2898 default:
2899 break;
2901 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2902 if (kerr != KCGI_OK)
2903 goto done;
2905 if (limit && --limit == 0)
2906 break;
2908 if (tag)
2909 got_object_tag_close(tag);
2910 tag = NULL;
2911 free(id_str);
2912 id_str = NULL;
2913 free(age);
2914 age = NULL;
2915 free(tag_commit0);
2916 tag_commit0 = NULL;
2917 free(href_tag);
2918 href_tag = NULL;
2919 free(href_briefs);
2920 href_briefs = NULL;
2921 free(href_commits);
2922 href_commits = NULL;
2924 done:
2925 if (tag)
2926 got_object_tag_close(tag);
2927 free(id_str);
2928 free(age);
2929 free(tag_commit0);
2930 free(href_tag);
2931 free(href_briefs);
2932 free(href_commits);
2933 got_ref_list_free(&refs);
2934 if (repo)
2935 got_repo_close(repo);
2936 if (error == NULL && kerr != KCGI_OK)
2937 error = gw_kcgi_error(kerr);
2938 return error;
2941 static void
2942 gw_free_headers(struct gw_header *header)
2944 free(header->id);
2945 free(header->path);
2946 if (header->commit != NULL)
2947 got_object_commit_close(header->commit);
2948 if (header->repo)
2949 got_repo_close(header->repo);
2950 free(header->refs_str);
2951 free(header->commit_id);
2952 free(header->author);
2953 free(header->committer);
2954 free(header->parent_id);
2955 free(header->tree_id);
2956 free(header->commit_msg);
2959 static struct gw_header *
2960 gw_init_header()
2962 struct gw_header *header;
2964 header = malloc(sizeof(*header));
2965 if (header == NULL)
2966 return NULL;
2968 header->repo = NULL;
2969 header->commit = NULL;
2970 header->id = NULL;
2971 header->path = NULL;
2972 SIMPLEQ_INIT(&header->refs);
2974 return header;
2977 static const struct got_error *
2978 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
2979 int limit)
2981 const struct got_error *error = NULL;
2982 struct got_commit_graph *graph = NULL;
2984 error = got_commit_graph_open(&graph, header->path, 0);
2985 if (error)
2986 return error;
2988 error = got_commit_graph_iter_start(graph, header->id, header->repo,
2989 NULL, NULL);
2990 if (error)
2991 goto done;
2993 for (;;) {
2994 error = got_commit_graph_iter_next(&header->id, graph,
2995 header->repo, NULL, NULL);
2996 if (error) {
2997 if (error->code == GOT_ERR_ITER_COMPLETED)
2998 error = NULL;
2999 goto done;
3001 if (header->id == NULL)
3002 goto done;
3004 error = got_object_open_as_commit(&header->commit, header->repo,
3005 header->id);
3006 if (error)
3007 goto done;
3009 error = gw_get_commit(gw_trans, header);
3010 if (limit > 1) {
3011 struct gw_header *n_header = NULL;
3012 if ((n_header = gw_init_header()) == NULL) {
3013 error = got_error_from_errno("malloc");
3014 goto done;
3017 error = gw_strdup_string(&n_header->refs_str,
3018 header->refs_str, NULL);
3019 if (error)
3020 goto done;
3021 error = gw_strdup_string(&n_header->commit_id,
3022 header->commit_id, NULL);
3023 if (error)
3024 goto done;
3025 error = gw_strdup_string(&n_header->parent_id,
3026 header->parent_id, NULL);
3027 if (error)
3028 goto done;
3029 error = gw_strdup_string(&n_header->tree_id,
3030 header->tree_id, NULL);
3031 if (error)
3032 goto done;
3033 error = gw_strdup_string(&n_header->author,
3034 header->author, NULL);
3035 if (error)
3036 goto done;
3037 error = gw_strdup_string(&n_header->committer,
3038 header->committer, NULL);
3039 if (error)
3040 goto done;
3041 error = gw_strdup_string(&n_header->commit_msg,
3042 header->commit_msg, NULL);
3043 if (error)
3044 goto done;
3045 n_header->committer_time = header->committer_time;
3046 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3047 entry);
3049 if (error || (limit && --limit == 0))
3050 break;
3052 done:
3053 if (graph)
3054 got_commit_graph_close(graph);
3055 return error;
3058 static const struct got_error *
3059 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
3061 const struct got_error *error = NULL;
3062 struct got_reflist_entry *re;
3063 struct got_object_id *id2 = NULL;
3064 struct got_object_qid *parent_id;
3065 char *refs_str = NULL, *commit_msg = NULL, *commit_msg0;
3067 /*print commit*/
3068 SIMPLEQ_FOREACH(re, &header->refs, entry) {
3069 char *s;
3070 const char *name;
3071 struct got_tag_object *tag = NULL;
3072 int cmp;
3074 name = got_ref_get_name(re->ref);
3075 if (strcmp(name, GOT_REF_HEAD) == 0)
3076 continue;
3077 if (strncmp(name, "refs/", 5) == 0)
3078 name += 5;
3079 if (strncmp(name, "got/", 4) == 0)
3080 continue;
3081 if (strncmp(name, "heads/", 6) == 0)
3082 name += 6;
3083 if (strncmp(name, "remotes/", 8) == 0)
3084 name += 8;
3085 if (strncmp(name, "tags/", 5) == 0) {
3086 error = got_object_open_as_tag(&tag, header->repo,
3087 re->id);
3088 if (error) {
3089 if (error->code != GOT_ERR_OBJ_TYPE)
3090 continue;
3092 * Ref points at something other
3093 * than a tag.
3095 error = NULL;
3096 tag = NULL;
3099 cmp = got_object_id_cmp(tag ?
3100 got_object_tag_get_object_id(tag) : re->id, header->id);
3101 if (tag)
3102 got_object_tag_close(tag);
3103 if (cmp != 0)
3104 continue;
3105 s = refs_str;
3106 if (asprintf(&refs_str, "%s%s%s", s ? s : "",
3107 s ? ", " : "", name) == -1) {
3108 error = got_error_from_errno("asprintf");
3109 free(s);
3110 return error;
3112 error = gw_strdup_string(&header->refs_str, refs_str, NULL);
3113 free(s);
3114 if (error)
3115 return error;
3118 if (refs_str == NULL) {
3119 error = gw_strdup_string(&header->refs_str, "", NULL);
3120 if (error)
3121 return error;
3123 free(refs_str);
3125 error = got_object_id_str(&header->commit_id, header->id);
3126 if (error)
3127 return error;
3129 error = got_object_id_str(&header->tree_id,
3130 got_object_commit_get_tree_id(header->commit));
3131 if (error)
3132 return error;
3134 if (gw_trans->action == GW_DIFF) {
3135 parent_id = SIMPLEQ_FIRST(
3136 got_object_commit_get_parent_ids(header->commit));
3137 if (parent_id != NULL) {
3138 id2 = got_object_id_dup(parent_id->id);
3139 free (parent_id);
3140 error = got_object_id_str(&header->parent_id, id2);
3141 if (error)
3142 return error;
3143 free(id2);
3144 } else {
3145 error = gw_strdup_string(&header->parent_id,
3146 "/dev/null", NULL);
3147 if (error)
3148 return error;
3150 } else {
3151 error = gw_strdup_string(&header->parent_id, "", NULL);
3152 if (error)
3153 return error;
3156 header->committer_time =
3157 got_object_commit_get_committer_time(header->commit);
3159 error = gw_strdup_string(&header->author, NULL,
3160 got_object_commit_get_author(header->commit));
3161 if (error)
3162 return error;
3163 error = gw_strdup_string(&header->committer, NULL,
3164 got_object_commit_get_committer(header->commit));
3165 if (error)
3166 return error;
3167 error = got_object_commit_get_logmsg(&commit_msg0, header->commit);
3168 if (error)
3169 return error;
3171 commit_msg = commit_msg0;
3172 while (*commit_msg == '\n')
3173 commit_msg++;
3175 error = gw_strdup_string(&header->commit_msg, commit_msg, NULL);
3176 free(commit_msg0);
3177 return error;
3180 static const struct got_error *
3181 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3183 const struct got_error *error = NULL;
3184 char *in_repo_path = NULL;
3186 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
3187 if (error)
3188 return error;
3190 if (gw_trans->commit == NULL) {
3191 struct got_reference *head_ref;
3192 error = got_ref_open(&head_ref, header->repo,
3193 gw_trans->headref, 0);
3194 if (error)
3195 return error;
3197 error = got_ref_resolve(&header->id, header->repo, head_ref);
3198 got_ref_close(head_ref);
3199 if (error)
3200 return error;
3202 error = got_object_open_as_commit(&header->commit,
3203 header->repo, header->id);
3204 } else {
3205 struct got_reference *ref;
3206 error = got_ref_open(&ref, header->repo, gw_trans->commit, 0);
3207 if (error == NULL) {
3208 int obj_type;
3209 error = got_ref_resolve(&header->id, header->repo, ref);
3210 got_ref_close(ref);
3211 if (error)
3212 return error;
3213 error = got_object_get_type(&obj_type, header->repo,
3214 header->id);
3215 if (error)
3216 return error;
3217 if (obj_type == GOT_OBJ_TYPE_TAG) {
3218 struct got_tag_object *tag;
3219 error = got_object_open_as_tag(&tag,
3220 header->repo, header->id);
3221 if (error)
3222 return error;
3223 if (got_object_tag_get_object_type(tag) !=
3224 GOT_OBJ_TYPE_COMMIT) {
3225 got_object_tag_close(tag);
3226 error = got_error(GOT_ERR_OBJ_TYPE);
3227 return error;
3229 free(header->id);
3230 header->id = got_object_id_dup(
3231 got_object_tag_get_object_id(tag));
3232 if (header->id == NULL)
3233 error = got_error_from_errno(
3234 "got_object_id_dup");
3235 got_object_tag_close(tag);
3236 if (error)
3237 return error;
3238 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3239 error = got_error(GOT_ERR_OBJ_TYPE);
3240 return error;
3242 error = got_object_open_as_commit(&header->commit,
3243 header->repo, header->id);
3244 if (error)
3245 return error;
3247 if (header->commit == NULL) {
3248 error = got_repo_match_object_id_prefix(&header->id,
3249 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
3250 header->repo);
3251 if (error)
3252 return error;
3254 error = got_repo_match_object_id_prefix(&header->id,
3255 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
3256 header->repo);
3259 error = got_repo_map_path(&in_repo_path, header->repo,
3260 gw_trans->repo_path, 1);
3261 if (error)
3262 return error;
3264 if (in_repo_path) {
3265 error = gw_strdup_string(&header->path, in_repo_path, NULL);
3266 if (error) {
3267 free(in_repo_path);
3268 return error;
3271 free(in_repo_path);
3273 error = got_ref_list(&header->refs, header->repo, NULL,
3274 got_ref_cmp_by_name, NULL);
3275 if (error)
3276 return error;
3278 error = gw_get_commits(gw_trans, header, limit);
3279 return error;
3282 struct blame_line {
3283 int annotated;
3284 char *id_str;
3285 char *committer;
3286 char datebuf[11]; /* YYYY-MM-DD + NUL */
3289 struct gw_blame_cb_args {
3290 struct blame_line *lines;
3291 int nlines;
3292 int nlines_prec;
3293 int lineno_cur;
3294 off_t *line_offsets;
3295 FILE *f;
3296 struct got_repository *repo;
3297 struct gw_trans *gw_trans;
3300 static const struct got_error *
3301 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3303 const struct got_error *err = NULL;
3304 struct gw_blame_cb_args *a = arg;
3305 struct blame_line *bline;
3306 char *line = NULL;
3307 size_t linesize = 0;
3308 struct got_commit_object *commit = NULL;
3309 off_t offset;
3310 struct tm tm;
3311 time_t committer_time;
3312 enum kcgi_err kerr = KCGI_OK;
3314 if (nlines != a->nlines ||
3315 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3316 return got_error(GOT_ERR_RANGE);
3318 if (lineno == -1)
3319 return NULL; /* no change in this commit */
3321 /* Annotate this line. */
3322 bline = &a->lines[lineno - 1];
3323 if (bline->annotated)
3324 return NULL;
3325 err = got_object_id_str(&bline->id_str, id);
3326 if (err)
3327 return err;
3329 err = got_object_open_as_commit(&commit, a->repo, id);
3330 if (err)
3331 goto done;
3333 err = gw_strdup_string(&bline->committer, NULL,
3334 got_object_commit_get_committer(commit));
3335 if (err)
3336 goto done;
3338 committer_time = got_object_commit_get_committer_time(commit);
3339 if (localtime_r(&committer_time, &tm) == NULL)
3340 return got_error_from_errno("localtime_r");
3341 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3342 &tm) >= sizeof(bline->datebuf)) {
3343 err = got_error(GOT_ERR_NO_SPACE);
3344 goto done;
3346 bline->annotated = 1;
3348 /* Print lines annotated so far. */
3349 bline = &a->lines[a->lineno_cur - 1];
3350 if (!bline->annotated)
3351 goto done;
3353 offset = a->line_offsets[a->lineno_cur - 1];
3354 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3355 err = got_error_from_errno("fseeko");
3356 goto done;
3359 while (bline->annotated) {
3360 char *smallerthan, *at, *nl, *committer;
3361 char *lineno = NULL, *href_diff = NULL, *href_link = NULL;
3362 size_t len;
3364 if (getline(&line, &linesize, a->f) == -1) {
3365 if (ferror(a->f))
3366 err = got_error_from_errno("getline");
3367 break;
3370 committer = bline->committer;
3371 smallerthan = strchr(committer, '<');
3372 if (smallerthan && smallerthan[1] != '\0')
3373 committer = smallerthan + 1;
3374 at = strchr(committer, '@');
3375 if (at)
3376 *at = '\0';
3377 len = strlen(committer);
3378 if (len >= 9)
3379 committer[8] = '\0';
3381 nl = strchr(line, '\n');
3382 if (nl)
3383 *nl = '\0';
3385 if (a->gw_trans->repo_folder == NULL) {
3386 err = gw_strdup_string(&a->gw_trans->repo_folder, "",
3387 NULL);
3388 if (err)
3389 goto err;
3391 if (a->gw_trans->repo_folder == NULL)
3392 goto err;
3394 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3395 "blame_wrapper", KATTR__MAX);
3396 if (kerr != KCGI_OK)
3397 goto err;
3398 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3399 "blame_number", KATTR__MAX);
3400 if (kerr != KCGI_OK)
3401 goto err;
3402 if (asprintf(&lineno, "%.*d", a->nlines_prec,
3403 a->lineno_cur) == -1)
3404 goto err;
3405 kerr = khtml_puts(a->gw_trans->gw_html_req, lineno);
3406 if (kerr != KCGI_OK)
3407 goto err;
3408 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3409 if (kerr != KCGI_OK)
3410 goto err;
3412 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3413 "blame_hash", KATTR__MAX);
3414 if (kerr != KCGI_OK)
3415 goto err;
3416 if (asprintf(&href_diff,
3417 "?path=%s&action=diff&commit=%s&file=%s&folder=%s",
3418 a->gw_trans->repo_name, bline->id_str,
3419 a->gw_trans->repo_file, a->gw_trans->repo_folder) == -1) {
3420 err = got_error_from_errno("asprintf");
3421 goto err;
3423 if (asprintf(&href_link, "%.8s", bline->id_str) == -1) {
3424 err = got_error_from_errno("asprintf");
3425 goto err;
3427 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3428 KATTR_HREF, href_diff, KATTR__MAX);
3429 if (kerr != KCGI_OK)
3430 goto done;
3431 kerr = khtml_puts(a->gw_trans->gw_html_req, href_link);
3432 if (kerr != KCGI_OK)
3433 goto err;
3434 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3435 if (kerr != KCGI_OK)
3436 goto err;
3438 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3439 "blame_date", KATTR__MAX);
3440 if (kerr != KCGI_OK)
3441 goto err;
3442 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3443 if (kerr != KCGI_OK)
3444 goto err;
3445 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3446 if (kerr != KCGI_OK)
3447 goto err;
3449 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3450 "blame_author", KATTR__MAX);
3451 if (kerr != KCGI_OK)
3452 goto err;
3453 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
3454 if (kerr != KCGI_OK)
3455 goto err;
3456 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3457 if (kerr != KCGI_OK)
3458 goto err;
3460 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3461 "blame_code", KATTR__MAX);
3462 if (kerr != KCGI_OK)
3463 goto err;
3464 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
3465 if (kerr != KCGI_OK)
3466 goto err;
3467 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3468 if (kerr != KCGI_OK)
3469 goto err;
3471 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3472 if (kerr != KCGI_OK)
3473 goto err;
3475 a->lineno_cur++;
3476 bline = &a->lines[a->lineno_cur - 1];
3477 err:
3478 free(lineno);
3479 free(href_diff);
3480 free(href_link);
3482 done:
3483 if (commit)
3484 got_object_commit_close(commit);
3485 free(line);
3486 if (err == NULL && kerr != KCGI_OK)
3487 err = gw_kcgi_error(kerr);
3488 return err;
3491 static const struct got_error *
3492 gw_output_file_blame(struct gw_trans *gw_trans)
3494 const struct got_error *error = NULL;
3495 struct got_repository *repo = NULL;
3496 struct got_object_id *obj_id = NULL;
3497 struct got_object_id *commit_id = NULL;
3498 struct got_blob_object *blob = NULL;
3499 char *path = NULL, *in_repo_path = NULL;
3500 struct gw_blame_cb_args bca;
3501 int i, obj_type;
3502 size_t filesize;
3504 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3505 if (error)
3506 return error;
3508 if (asprintf(&path, "%s%s%s",
3509 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3510 gw_trans->repo_folder ? "/" : "",
3511 gw_trans->repo_file) == -1) {
3512 error = got_error_from_errno("asprintf");
3513 goto done;
3516 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3517 if (error)
3518 goto done;
3520 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3521 GOT_OBJ_TYPE_COMMIT, 1, repo);
3522 if (error)
3523 goto done;
3525 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3526 if (error)
3527 goto done;
3529 if (obj_id == NULL) {
3530 error = got_error(GOT_ERR_NO_OBJ);
3531 goto done;
3534 error = got_object_get_type(&obj_type, repo, obj_id);
3535 if (error)
3536 goto done;
3538 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3539 error = got_error(GOT_ERR_OBJ_TYPE);
3540 goto done;
3543 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3544 if (error)
3545 goto done;
3547 bca.f = got_opentemp();
3548 if (bca.f == NULL) {
3549 error = got_error_from_errno("got_opentemp");
3550 goto done;
3552 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3553 &bca.line_offsets, bca.f, blob);
3554 if (error || bca.nlines == 0)
3555 goto done;
3557 /* Don't include \n at EOF in the blame line count. */
3558 if (bca.line_offsets[bca.nlines - 1] == filesize)
3559 bca.nlines--;
3561 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3562 if (bca.lines == NULL) {
3563 error = got_error_from_errno("calloc");
3564 goto done;
3566 bca.lineno_cur = 1;
3567 bca.nlines_prec = 0;
3568 i = bca.nlines;
3569 while (i > 0) {
3570 i /= 10;
3571 bca.nlines_prec++;
3573 bca.repo = repo;
3574 bca.gw_trans = gw_trans;
3576 error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
3577 NULL, NULL);
3578 done:
3579 free(bca.line_offsets);
3580 free(in_repo_path);
3581 free(commit_id);
3582 free(obj_id);
3583 free(path);
3585 for (i = 0; i < bca.nlines; i++) {
3586 struct blame_line *bline = &bca.lines[i];
3587 free(bline->id_str);
3588 free(bline->committer);
3590 free(bca.lines);
3591 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3592 error = got_error_from_errno("fclose");
3593 if (blob)
3594 got_object_blob_close(blob);
3595 if (repo)
3596 got_repo_close(repo);
3597 return error;
3600 static const struct got_error *
3601 gw_output_blob_buf(struct gw_trans *gw_trans)
3603 const struct got_error *error = NULL;
3604 struct got_repository *repo = NULL;
3605 struct got_object_id *obj_id = NULL;
3606 struct got_object_id *commit_id = NULL;
3607 struct got_blob_object *blob = NULL;
3608 char *path = NULL, *in_repo_path = NULL;
3609 int obj_type, set_mime = 0;
3610 size_t len, hdrlen;
3611 const uint8_t *buf;
3612 enum kcgi_err kerr = KCGI_OK;
3614 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3615 if (error)
3616 return error;
3618 if (asprintf(&path, "%s%s%s",
3619 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3620 gw_trans->repo_folder ? "/" : "",
3621 gw_trans->repo_file) == -1) {
3622 error = got_error_from_errno("asprintf");
3623 goto done;
3626 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3627 if (error)
3628 goto done;
3630 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3631 GOT_OBJ_TYPE_COMMIT, 1, repo);
3632 if (error)
3633 goto done;
3635 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3636 if (error)
3637 goto done;
3639 if (obj_id == NULL) {
3640 error = got_error(GOT_ERR_NO_OBJ);
3641 goto done;
3644 error = got_object_get_type(&obj_type, repo, obj_id);
3645 if (error)
3646 goto done;
3648 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3649 error = got_error(GOT_ERR_OBJ_TYPE);
3650 goto done;
3653 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3654 if (error)
3655 goto done;
3657 hdrlen = got_object_blob_get_hdrlen(blob);
3658 do {
3659 error = got_object_blob_read_block(&len, blob);
3660 if (error)
3661 goto done;
3662 buf = got_object_blob_get_read_buf(blob);
3665 * Skip blob object header first time around,
3666 * which also contains a zero byte.
3668 buf += hdrlen;
3669 if (set_mime == 0) {
3670 if (isbinary(buf, len - hdrlen))
3671 gw_trans->mime = KMIME_APP_OCTET_STREAM;
3672 else
3673 gw_trans->mime = KMIME_TEXT_PLAIN;
3674 set_mime = 1;
3675 error = gw_display_index(gw_trans);
3676 if (error)
3677 goto done;
3679 khttp_write(gw_trans->gw_req, buf, len - hdrlen);
3680 hdrlen = 0;
3681 } while (len != 0);
3682 done:
3683 free(in_repo_path);
3684 free(commit_id);
3685 free(obj_id);
3686 free(path);
3687 if (blob)
3688 got_object_blob_close(blob);
3689 if (repo)
3690 got_repo_close(repo);
3691 if (error == NULL && kerr != KCGI_OK)
3692 error = gw_kcgi_error(kerr);
3693 return error;
3696 static const struct got_error *
3697 gw_output_repo_tree(struct gw_trans *gw_trans)
3699 const struct got_error *error = NULL;
3700 struct got_repository *repo = NULL;
3701 struct got_object_id *tree_id = NULL, *commit_id = NULL;
3702 struct got_tree_object *tree = NULL;
3703 char *path = NULL, *in_repo_path = NULL;
3704 char *id_str = NULL;
3705 char *build_folder = NULL;
3706 char *href_blob = NULL, *href_blame = NULL;
3707 const char *class = NULL;
3708 int nentries, i, class_flip = 0;
3709 enum kcgi_err kerr = KCGI_OK;
3711 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3712 if (error)
3713 return error;
3715 if (gw_trans->repo_folder != NULL) {
3716 error = gw_strdup_string(&path, gw_trans->repo_folder, NULL);
3717 if (error)
3718 goto done;
3719 } else {
3720 error = got_repo_map_path(&in_repo_path, repo,
3721 gw_trans->repo_path, 1);
3722 if (error)
3723 goto done;
3724 free(path);
3725 path = in_repo_path;
3728 if (gw_trans->commit == NULL) {
3729 struct got_reference *head_ref;
3730 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
3731 if (error)
3732 goto done;
3733 error = got_ref_resolve(&commit_id, repo, head_ref);
3734 if (error)
3735 goto done;
3736 got_ref_close(head_ref);
3738 } else {
3739 error = got_repo_match_object_id(&commit_id, NULL,
3740 gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
3741 if (error)
3742 goto done;
3746 * XXX gw_trans->commit might have already been allocated in
3747 * gw_parse_querystring(); could we more cleanly seperate values
3748 * we received as arguments from values we compute ourselves?
3750 error = got_object_id_str(&gw_trans->commit, commit_id);
3751 if (error)
3752 goto done;
3754 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
3755 if (error)
3756 goto done;
3758 error = got_object_open_as_tree(&tree, repo, tree_id);
3759 if (error)
3760 goto done;
3762 nentries = got_object_tree_get_nentries(tree);
3763 for (i = 0; i < nentries; i++) {
3764 struct got_tree_entry *te;
3765 const char *modestr = "";
3766 mode_t mode;
3768 te = got_object_tree_get_entry(tree, i);
3770 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
3771 if (error)
3772 goto done;
3774 mode = got_tree_entry_get_mode(te);
3775 if (got_object_tree_entry_is_submodule(te))
3776 modestr = "$";
3777 else if (S_ISLNK(mode))
3778 modestr = "@";
3779 else if (S_ISDIR(mode))
3780 modestr = "/";
3781 else if (mode & S_IXUSR)
3782 modestr = "*";
3784 if (class_flip == 0) {
3785 class = "back_lightgray";
3786 class_flip = 1;
3787 } else {
3788 class = "back_white";
3789 class_flip = 0;
3792 if (S_ISDIR(mode)) {
3793 if (asprintf(&build_folder, "%s/%s",
3794 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3795 got_tree_entry_get_name(te)) == -1) {
3796 error = got_error_from_errno(
3797 "asprintf");
3798 goto done;
3800 if (asprintf(&href_blob,
3801 "?path=%s&action=%s&commit=%s&folder=%s",
3802 gw_trans->repo_name, gw_get_action_name(gw_trans),
3803 gw_trans->commit, build_folder) == -1) {
3804 error = got_error_from_errno("asprintf");
3805 goto done;
3808 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3809 KATTR_ID, "tree_wrapper", KATTR__MAX);
3810 if (kerr != KCGI_OK)
3811 goto done;
3812 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3813 KATTR_ID, "tree_line", KATTR_CLASS, class,
3814 KATTR__MAX);
3815 if (kerr != KCGI_OK)
3816 goto done;
3817 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3818 KATTR_HREF, href_blob, KATTR_CLASS,
3819 "diff_directory", KATTR__MAX);
3820 if (kerr != KCGI_OK)
3821 goto done;
3822 kerr = khtml_puts(gw_trans->gw_html_req,
3823 got_tree_entry_get_name(te));
3824 if (kerr != KCGI_OK)
3825 goto done;
3826 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3827 if (kerr != KCGI_OK)
3828 goto done;
3829 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3830 if (kerr != KCGI_OK)
3831 goto done;
3832 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3833 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
3834 KATTR__MAX);
3835 if (kerr != KCGI_OK)
3836 goto done;
3837 kerr = khtml_entity(gw_trans->gw_html_req,
3838 KENTITY_nbsp);
3839 if (kerr != KCGI_OK)
3840 goto done;
3841 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3842 if (kerr != KCGI_OK)
3843 goto done;
3844 } else {
3845 if (asprintf(&href_blob,
3846 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3847 gw_trans->repo_name, "blob", gw_trans->commit,
3848 got_tree_entry_get_name(te),
3849 gw_trans->repo_folder ?
3850 gw_trans->repo_folder : "") == -1) {
3851 error = got_error_from_errno("asprintf");
3852 goto done;
3854 if (asprintf(&href_blame,
3855 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3856 gw_trans->repo_name, "blame", gw_trans->commit,
3857 got_tree_entry_get_name(te),
3858 gw_trans->repo_folder ?
3859 gw_trans->repo_folder : "") == -1) {
3860 error = got_error_from_errno("asprintf");
3861 goto done;
3864 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3865 KATTR_ID, "tree_wrapper", KATTR__MAX);
3866 if (kerr != KCGI_OK)
3867 goto done;
3868 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3869 KATTR_ID, "tree_line", KATTR_CLASS, class,
3870 KATTR__MAX);
3871 if (kerr != KCGI_OK)
3872 goto done;
3873 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3874 KATTR_HREF, href_blob, KATTR__MAX);
3875 if (kerr != KCGI_OK)
3876 goto done;
3877 kerr = khtml_puts(gw_trans->gw_html_req,
3878 got_tree_entry_get_name(te));
3879 if (kerr != KCGI_OK)
3880 goto done;
3881 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3882 if (kerr != KCGI_OK)
3883 goto done;
3884 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3885 if (kerr != KCGI_OK)
3886 goto done;
3887 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3888 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
3889 KATTR__MAX);
3890 if (kerr != KCGI_OK)
3891 goto done;
3893 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3894 KATTR_HREF, href_blob, KATTR__MAX);
3895 if (kerr != KCGI_OK)
3896 goto done;
3897 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
3898 if (kerr != KCGI_OK)
3899 goto done;
3900 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3901 if (kerr != KCGI_OK)
3902 goto done;
3904 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3905 if (kerr != KCGI_OK)
3906 goto done;
3908 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3909 KATTR_HREF, href_blame, KATTR__MAX);
3910 if (kerr != KCGI_OK)
3911 goto done;
3912 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
3913 if (kerr != KCGI_OK)
3914 goto done;
3916 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3917 if (kerr != KCGI_OK)
3918 goto done;
3920 free(id_str);
3921 id_str = NULL;
3922 free(href_blob);
3923 href_blob = NULL;
3924 free(build_folder);
3925 build_folder = NULL;
3927 done:
3928 if (tree)
3929 got_object_tree_close(tree);
3930 if (repo)
3931 got_repo_close(repo);
3932 free(id_str);
3933 free(href_blob);
3934 free(href_blame);
3935 free(in_repo_path);
3936 free(tree_id);
3937 free(build_folder);
3938 if (error == NULL && kerr != KCGI_OK)
3939 error = gw_kcgi_error(kerr);
3940 return error;
3943 static const struct got_error *
3944 gw_output_repo_heads(struct gw_trans *gw_trans)
3946 const struct got_error *error = NULL;
3947 struct got_repository *repo = NULL;
3948 struct got_reflist_head refs;
3949 struct got_reflist_entry *re;
3950 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
3951 char *href_commits = NULL;
3952 enum kcgi_err kerr = KCGI_OK;
3954 SIMPLEQ_INIT(&refs);
3956 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3957 if (error)
3958 goto done;
3960 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
3961 NULL);
3962 if (error)
3963 goto done;
3965 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3966 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
3967 if (kerr != KCGI_OK)
3968 goto done;
3969 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3970 KATTR_ID, "summary_heads_title", KATTR__MAX);
3971 if (kerr != KCGI_OK)
3972 goto done;
3973 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
3974 if (kerr != KCGI_OK)
3975 goto done;
3976 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3977 if (kerr != KCGI_OK)
3978 goto done;
3979 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3980 KATTR_ID, "summary_heads_content", KATTR__MAX);
3981 if (kerr != KCGI_OK)
3982 goto done;
3984 SIMPLEQ_FOREACH(re, &refs, entry) {
3985 char *refname;
3987 error = gw_strdup_string(&refname, NULL,
3988 got_ref_get_name(re->ref));
3989 if (error)
3990 goto done;
3992 if (strncmp(refname, "refs/heads/", 11) != 0) {
3993 free(refname);
3994 continue;
3997 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
3998 refname, TM_DIFF);
3999 if (error)
4000 goto done;
4002 if (strncmp(refname, "refs/heads/", 11) == 0)
4003 refname += 11;
4005 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4006 KATTR_ID, "heads_wrapper", KATTR__MAX);
4007 if (kerr != KCGI_OK)
4008 goto done;
4009 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4010 KATTR_ID, "heads_age", KATTR__MAX);
4011 if (kerr != KCGI_OK)
4012 goto done;
4013 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
4014 if (kerr != KCGI_OK)
4015 goto done;
4016 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4017 if (kerr != KCGI_OK)
4018 goto done;
4019 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4020 KATTR_ID, "heads_space", KATTR__MAX);
4021 if (kerr != KCGI_OK)
4022 goto done;
4023 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
4024 if (kerr != KCGI_OK)
4025 goto done;
4026 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4027 if (kerr != KCGI_OK)
4028 goto done;
4029 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4030 KATTR_ID, "head", KATTR__MAX);
4031 if (kerr != KCGI_OK)
4032 goto done;
4033 if (asprintf(&href_summary,
4034 "?path=%s&action=summary&headref=%s",
4035 gw_trans->repo_name, refname) == -1) {
4036 error = got_error_from_errno("asprintf");
4037 goto done;
4039 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4040 href_summary, KATTR__MAX);
4041 kerr = khtml_puts(gw_trans->gw_html_req, refname);
4042 if (kerr != KCGI_OK)
4043 goto done;
4044 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4045 if (kerr != KCGI_OK)
4046 goto done;
4048 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4049 "navs_wrapper", KATTR__MAX);
4050 if (kerr != KCGI_OK)
4051 goto done;
4052 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4053 "navs", KATTR__MAX);
4054 if (kerr != KCGI_OK)
4055 goto done;
4057 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4058 href_summary, KATTR__MAX);
4059 if (kerr != KCGI_OK)
4060 goto done;
4061 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4062 if (kerr != KCGI_OK)
4063 goto done;
4064 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4065 if (kerr != KCGI_OK)
4066 goto done;
4068 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4069 if (kerr != KCGI_OK)
4070 goto done;
4071 if (asprintf(&href_briefs, "?path=%s&action=briefs&headref=%s",
4072 gw_trans->repo_name, refname) == -1) {
4073 error = got_error_from_errno("asprintf");
4074 goto done;
4076 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4077 href_briefs, KATTR__MAX);
4078 if (kerr != KCGI_OK)
4079 goto done;
4080 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4081 if (kerr != KCGI_OK)
4082 goto done;
4083 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4084 if (kerr != KCGI_OK)
4085 goto done;
4087 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4088 if (kerr != KCGI_OK)
4089 goto done;
4091 if (asprintf(&href_commits,
4092 "?path=%s&action=commits&headref=%s",
4093 gw_trans->repo_name, refname) == -1) {
4094 error = got_error_from_errno("asprintf");
4095 goto done;
4097 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4098 href_commits, KATTR__MAX);
4099 if (kerr != KCGI_OK)
4100 goto done;
4101 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4102 if (kerr != KCGI_OK)
4103 goto done;
4104 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4105 if (kerr != KCGI_OK)
4106 goto done;
4108 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4109 "dotted_line", KATTR__MAX);
4110 if (kerr != KCGI_OK)
4111 goto done;
4112 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4113 if (kerr != KCGI_OK)
4114 goto done;
4115 free(href_summary);
4116 href_summary = NULL;
4117 free(href_briefs);
4118 href_briefs = NULL;
4119 free(href_commits);
4120 href_commits = NULL;
4122 done:
4123 got_ref_list_free(&refs);
4124 free(href_summary);
4125 free(href_briefs);
4126 free(href_commits);
4127 if (repo)
4128 got_repo_close(repo);
4129 return error;
4132 static const struct got_error *
4133 gw_output_site_link(struct gw_trans *gw_trans)
4135 const struct got_error *error = NULL;
4136 char *href_summary = NULL;
4137 enum kcgi_err kerr = KCGI_OK;
4139 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4140 "site_link", KATTR__MAX);
4141 if (kerr != KCGI_OK)
4142 goto done;
4143 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4144 KATTR__MAX);
4145 if (kerr != KCGI_OK)
4146 goto done;
4147 kerr = khtml_puts(gw_trans->gw_html_req,
4148 gw_trans->gw_conf->got_site_link);
4149 if (kerr != KCGI_OK)
4150 goto done;
4151 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4152 if (kerr != KCGI_OK)
4153 goto done;
4155 if (gw_trans->repo_name != NULL) {
4156 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4157 if (kerr != KCGI_OK)
4158 goto done;
4159 if (asprintf(&href_summary, "?path=%s&action=summary",
4160 gw_trans->repo_name) == -1)
4161 goto done;
4162 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4163 href_summary, KATTR__MAX);
4164 if (kerr != KCGI_OK)
4165 goto done;
4166 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4167 if (kerr != KCGI_OK)
4168 goto done;
4169 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4170 if (kerr != KCGI_OK)
4171 goto done;
4172 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4173 if (kerr != KCGI_OK)
4174 goto done;
4175 kerr = khtml_puts(gw_trans->gw_html_req,
4176 gw_get_action_name(gw_trans));
4177 if (kerr != KCGI_OK)
4178 goto done;
4181 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4182 if (kerr != KCGI_OK)
4183 goto done;
4184 done:
4185 free(href_summary);
4186 return error;
4189 static const struct got_error *
4190 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4192 const struct got_error *error = NULL;
4193 char *color = NULL;
4194 enum kcgi_err kerr = KCGI_OK;
4196 if (strncmp(buf, "-", 1) == 0)
4197 color = "diff_minus";
4198 else if (strncmp(buf, "+", 1) == 0)
4199 color = "diff_plus";
4200 else if (strncmp(buf, "@@", 2) == 0)
4201 color = "diff_chunk_header";
4202 else if (strncmp(buf, "@@", 2) == 0)
4203 color = "diff_chunk_header";
4204 else if (strncmp(buf, "commit +", 8) == 0)
4205 color = "diff_meta";
4206 else if (strncmp(buf, "commit -", 8) == 0)
4207 color = "diff_meta";
4208 else if (strncmp(buf, "blob +", 6) == 0)
4209 color = "diff_meta";
4210 else if (strncmp(buf, "blob -", 6) == 0)
4211 color = "diff_meta";
4212 else if (strncmp(buf, "file +", 6) == 0)
4213 color = "diff_meta";
4214 else if (strncmp(buf, "file -", 6) == 0)
4215 color = "diff_meta";
4216 else if (strncmp(buf, "from:", 5) == 0)
4217 color = "diff_author";
4218 else if (strncmp(buf, "via:", 4) == 0)
4219 color = "diff_author";
4220 else if (strncmp(buf, "date:", 5) == 0)
4221 color = "diff_date";
4222 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4223 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4224 if (error == NULL && kerr != KCGI_OK)
4225 error = gw_kcgi_error(kerr);
4226 return error;
4229 int
4230 main(int argc, char *argv[])
4232 const struct got_error *error = NULL;
4233 struct gw_trans *gw_trans;
4234 struct gw_dir *dir = NULL, *tdir;
4235 const char *page = "index";
4236 int gw_malloc = 1;
4237 enum kcgi_err kerr;
4239 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4240 errx(1, "malloc");
4242 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4243 errx(1, "malloc");
4245 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4246 errx(1, "malloc");
4248 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4249 errx(1, "malloc");
4251 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4252 if (kerr != KCGI_OK) {
4253 error = gw_kcgi_error(kerr);
4254 goto done;
4257 if ((gw_trans->gw_conf =
4258 malloc(sizeof(struct gotweb_conf))) == NULL) {
4259 gw_malloc = 0;
4260 error = got_error_from_errno("malloc");
4261 goto done;
4264 TAILQ_INIT(&gw_trans->gw_dirs);
4265 TAILQ_INIT(&gw_trans->gw_headers);
4267 gw_trans->page = 0;
4268 gw_trans->repos_total = 0;
4269 gw_trans->repo_path = NULL;
4270 gw_trans->commit = NULL;
4271 error = gw_strdup_string(&gw_trans->headref, GOT_REF_HEAD, NULL);
4272 if (error)
4273 goto done;
4274 gw_trans->mime = KMIME_TEXT_HTML;
4275 gw_trans->gw_tmpl->key = gw_templs;
4276 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4277 gw_trans->gw_tmpl->arg = gw_trans;
4278 gw_trans->gw_tmpl->cb = gw_template;
4279 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
4280 if (error)
4281 goto done;
4283 error = gw_parse_querystring(gw_trans);
4284 if (error)
4285 goto done;
4287 if (gw_trans->action == GW_BLOB)
4288 error = gw_blob(gw_trans);
4289 else
4290 error = gw_display_index(gw_trans);
4291 done:
4292 if (error) {
4293 gw_trans->mime = KMIME_TEXT_PLAIN;
4294 gw_trans->action = GW_ERR;
4295 gw_display_error(gw_trans, error);
4297 if (gw_malloc) {
4298 free(gw_trans->gw_conf->got_repos_path);
4299 free(gw_trans->gw_conf->got_site_name);
4300 free(gw_trans->gw_conf->got_site_owner);
4301 free(gw_trans->gw_conf->got_site_link);
4302 free(gw_trans->gw_conf->got_logo);
4303 free(gw_trans->gw_conf->got_logo_url);
4304 free(gw_trans->gw_conf);
4305 free(gw_trans->commit);
4306 free(gw_trans->repo_path);
4307 free(gw_trans->repo_file);
4308 free(gw_trans->headref);
4310 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4311 free(dir->name);
4312 free(dir->description);
4313 free(dir->age);
4314 free(dir->url);
4315 free(dir->path);
4316 free(dir);
4321 khttp_free(gw_trans->gw_req);
4322 return 0;