Blob


1 /*
2 * Copyright (c) 2019, 2020 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/queue.h>
19 #include <sys/stat.h>
20 #include <sys/types.h>
22 #include <ctype.h>
23 #include <dirent.h>
24 #include <err.h>
25 #include <errno.h>
26 #include <regex.h>
27 #include <stdarg.h>
28 #include <stdint.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
34 #include <got_object.h>
35 #include <got_reference.h>
36 #include <got_repository.h>
37 #include <got_path.h>
38 #include <got_cancel.h>
39 #include <got_worktree.h>
40 #include <got_diff.h>
41 #include <got_commit_graph.h>
42 #include <got_blame.h>
43 #include <got_privsep.h>
44 #include <got_opentemp.h>
46 #include <kcgi.h>
47 #include <kcgihtml.h>
49 #include "gotweb.h"
51 #ifndef nitems
52 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
53 #endif
55 struct gw_trans {
56 TAILQ_HEAD(headers, gw_header) gw_headers;
57 TAILQ_HEAD(dirs, gw_dir) gw_dirs;
58 struct gw_dir *gw_dir;
59 struct gotweb_conf *gw_conf;
60 struct ktemplate *gw_tmpl;
61 struct khtmlreq *gw_html_req;
62 struct kreq *gw_req;
63 const char *repo_name;
64 char *repo_path;
65 char *commit;
66 const char *repo_file;
67 char *repo_folder;
68 char *headref;
69 unsigned int action;
70 unsigned int page;
71 unsigned int repos_total;
72 enum kmime mime;
73 };
75 struct gw_header {
76 TAILQ_ENTRY(gw_header) entry;
77 struct got_repository *repo;
78 struct got_reflist_head refs;
79 struct got_commit_object *commit;
80 struct got_object_id *id;
81 char *path;
83 char *refs_str;
84 char *commit_id; /* id_str1 */
85 char *parent_id; /* id_str2 */
86 char *tree_id;
87 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 gw_trans->repo_file = p->parsed.s;
1675 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER])) {
1676 if (asprintf(&gw_trans->repo_folder, "%s",
1677 p->parsed.s) == -1)
1678 return got_error_from_errno("asprintf");
1681 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF])) {
1682 if (asprintf(&gw_trans->headref, "%s",
1683 p->parsed.s) == -1)
1684 return got_error_from_errno("asprintf");
1687 if (action == NULL) {
1688 error = got_error_from_errno("invalid action");
1689 return error;
1691 error = gw_init_gw_dir(&gw_trans->gw_dir, gw_trans->repo_name);
1692 if (error)
1693 return error;
1695 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1696 if (error)
1697 return error;
1698 } else
1699 gw_trans->action = GW_INDEX;
1701 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1702 gw_trans->page = p->parsed.i;
1704 return error;
1707 static const struct got_error *
1708 gw_init_gw_dir(struct gw_dir **gw_dir, const char *dir)
1710 const struct got_error *error;
1712 *gw_dir = malloc(sizeof(**gw_dir));
1713 if (*gw_dir == NULL)
1714 return got_error_from_errno("malloc");
1716 if (asprintf(&(*gw_dir)->name, "%s", dir) == -1) {
1717 error = got_error_from_errno("asprintf");
1718 free(*gw_dir);
1719 *gw_dir = NULL;
1720 return NULL;
1723 return NULL;
1726 static const struct got_error *
1727 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1729 enum kcgi_err kerr;
1731 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1732 if (kerr != KCGI_OK)
1733 return gw_kcgi_error(kerr);
1734 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1735 khttps[code]);
1736 if (kerr != KCGI_OK)
1737 return gw_kcgi_error(kerr);
1738 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1739 kmimetypes[mime]);
1740 if (kerr != KCGI_OK)
1741 return gw_kcgi_error(kerr);
1742 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
1743 "nosniff");
1744 if (kerr != KCGI_OK)
1745 return gw_kcgi_error(kerr);
1746 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1747 if (kerr != KCGI_OK)
1748 return gw_kcgi_error(kerr);
1749 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
1750 "1; mode=block");
1751 if (kerr != KCGI_OK)
1752 return gw_kcgi_error(kerr);
1754 /* XXX repo_file could be NULL if not present in querystring */
1755 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
1756 kerr = khttp_head(gw_trans->gw_req,
1757 kresps[KRESP_CONTENT_DISPOSITION],
1758 "attachment; filename=%s", gw_trans->repo_file);
1759 if (kerr != KCGI_OK)
1760 return gw_kcgi_error(kerr);
1763 kerr = khttp_body(gw_trans->gw_req);
1764 return gw_kcgi_error(kerr);
1767 static const struct got_error *
1768 gw_display_index(struct gw_trans *gw_trans)
1770 const struct got_error *error;
1771 enum kcgi_err kerr;
1773 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1774 if (error)
1775 return error;
1777 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1778 if (kerr != KCGI_OK)
1779 return gw_kcgi_error(kerr);
1781 if (gw_trans->action != GW_BLOB) {
1782 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1783 gw_query_funcs[gw_trans->action].template);
1784 if (kerr != KCGI_OK) {
1785 khtml_close(gw_trans->gw_html_req);
1786 return gw_kcgi_error(kerr);
1790 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
1793 static void
1794 gw_display_error(struct gw_trans *gw_trans, const struct got_error *err)
1796 if (gw_display_open(gw_trans, KHTTP_200, gw_trans->mime) != NULL)
1797 return;
1799 if (khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0) != KCGI_OK)
1800 return;
1801 khtml_puts(gw_trans->gw_html_req, err->msg);
1802 khtml_close(gw_trans->gw_html_req);
1805 static int
1806 gw_template(size_t key, void *arg)
1808 const struct got_error *error = NULL;
1809 enum kcgi_err kerr;
1810 struct gw_trans *gw_trans = arg;
1811 char *img_src = NULL;
1813 switch (key) {
1814 case (TEMPL_HEAD):
1815 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1816 KATTR_NAME, "viewport",
1817 KATTR_CONTENT, "initial-scale=.75, user-scalable=yes",
1818 KATTR__MAX);
1819 if (kerr != KCGI_OK)
1820 return 0;
1821 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1822 if (kerr != KCGI_OK)
1823 return 0;
1824 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1825 KATTR_CHARSET, "utf-8",
1826 KATTR__MAX);
1827 if (kerr != KCGI_OK)
1828 return 0;
1829 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1830 if (kerr != KCGI_OK)
1831 return 0;
1832 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1833 KATTR_NAME, "msapplication-TileColor",
1834 KATTR_CONTENT, "#da532c", KATTR__MAX);
1835 if (kerr != KCGI_OK)
1836 return 0;
1837 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1838 if (kerr != KCGI_OK)
1839 return 0;
1840 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1841 KATTR_NAME, "theme-color",
1842 KATTR_CONTENT, "#ffffff", KATTR__MAX);
1843 if (kerr != KCGI_OK)
1844 return 0;
1845 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1846 if (kerr != KCGI_OK)
1847 return 0;
1848 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1849 KATTR_REL, "apple-touch-icon", KATTR_SIZES, "180x180",
1850 KATTR_HREF, "/apple-touch-icon.png", KATTR__MAX);
1851 if (kerr != KCGI_OK)
1852 return 0;
1853 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1854 if (kerr != KCGI_OK)
1855 return 0;
1856 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1857 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
1858 "32x32", KATTR_HREF, "/favicon-32x32.png", KATTR__MAX);
1859 if (kerr != KCGI_OK)
1860 return 0;
1861 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1862 if (kerr != KCGI_OK)
1863 return 0;
1864 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1865 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
1866 "16x16", KATTR_HREF, "/favicon-16x16.png", KATTR__MAX);
1867 if (kerr != KCGI_OK)
1868 return 0;
1869 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1870 if (kerr != KCGI_OK)
1871 return 0;
1872 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1873 KATTR_REL, "manifest", KATTR_HREF, "/site.webmanifest",
1874 KATTR__MAX);
1875 if (kerr != KCGI_OK)
1876 return 0;
1877 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1878 if (kerr != KCGI_OK)
1879 return 0;
1880 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1881 KATTR_REL, "mask-icon", KATTR_HREF,
1882 "/safari-pinned-tab.svg", KATTR__MAX);
1883 if (kerr != KCGI_OK)
1884 return 0;
1885 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1886 if (kerr != KCGI_OK)
1887 return 0;
1888 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1889 KATTR_REL, "stylesheet", KATTR_TYPE, "text/css",
1890 KATTR_HREF, "/gotweb.css", KATTR__MAX);
1891 if (kerr != KCGI_OK)
1892 return 0;
1893 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1894 if (kerr != KCGI_OK)
1895 return 0;
1896 break;
1897 case(TEMPL_HEADER):
1898 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1899 KATTR_ID, "got_link", KATTR__MAX);
1900 if (kerr != KCGI_OK)
1901 return 0;
1902 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1903 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
1904 KATTR_TARGET, "_sotd", KATTR__MAX);
1905 if (kerr != KCGI_OK)
1906 return 0;
1907 if (asprintf(&img_src, "/%s",
1908 gw_trans->gw_conf->got_logo) == -1)
1909 return 0;
1910 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
1911 KATTR_SRC, img_src, KATTR__MAX);
1912 if (kerr != KCGI_OK) {
1913 free(img_src);
1914 return 0;
1916 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1917 if (kerr != KCGI_OK) {
1918 free(img_src);
1919 return 0;
1921 break;
1922 case (TEMPL_SITEPATH):
1923 error = gw_output_site_link(gw_trans);
1924 if (error)
1925 return 0;
1926 break;
1927 case(TEMPL_TITLE):
1928 if (gw_trans->gw_conf->got_site_name != NULL) {
1929 kerr = khtml_puts(gw_trans->gw_html_req,
1930 gw_trans->gw_conf->got_site_name);
1931 if (kerr != KCGI_OK)
1932 return 0;
1934 break;
1935 case (TEMPL_SEARCH):
1936 break;
1937 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1938 "search", KATTR__MAX);
1939 if (kerr != KCGI_OK)
1940 return 0;
1941 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
1942 KATTR_METHOD, "POST", KATTR__MAX);
1943 if (kerr != KCGI_OK)
1944 return 0;
1945 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
1946 "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
1947 KATTR_MAXLENGTH, "50", KATTR__MAX);
1948 if (kerr != KCGI_OK)
1949 return 0;
1950 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
1951 KATTR__MAX);
1952 if (kerr != KCGI_OK)
1953 return 0;
1954 kerr = khtml_puts(gw_trans->gw_html_req, "Search");
1955 if (kerr != KCGI_OK)
1956 return 0;
1957 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
1958 if (kerr != KCGI_OK)
1959 return 0;
1960 break;
1961 case(TEMPL_SITEOWNER):
1962 if (gw_trans->gw_conf->got_site_owner != NULL &&
1963 gw_trans->gw_conf->got_show_site_owner) {
1964 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1965 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
1966 if (kerr != KCGI_OK)
1967 return 0;
1968 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1969 KATTR_ID, "site_owner", KATTR__MAX);
1970 if (kerr != KCGI_OK)
1971 return 0;
1972 kerr = khtml_puts(gw_trans->gw_html_req,
1973 gw_trans->gw_conf->got_site_owner);
1974 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1975 if (kerr != KCGI_OK)
1976 return 0;
1978 break;
1979 case(TEMPL_CONTENT):
1980 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1981 if (error) {
1982 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1983 KATTR_ID, "tmpl_err", KATTR__MAX);
1984 if (kerr != KCGI_OK)
1985 return 0;
1986 kerr = khttp_puts(gw_trans->gw_req, "Error: ");
1987 if (kerr != KCGI_OK)
1988 return 0;
1989 kerr = khttp_puts(gw_trans->gw_req, error->msg);
1990 if (kerr != KCGI_OK)
1991 return 0;
1992 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1993 if (kerr != KCGI_OK)
1994 return 0;
1996 break;
1997 default:
1998 return 0;
2000 return 1;
2003 static const struct got_error *
2004 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
2006 const struct got_error *error = NULL;
2007 char *ref_str = NULL;
2008 enum kcgi_err kerr = KCGI_OK;
2010 if (strcmp(str2, "") != 0) {
2011 if (asprintf(&ref_str, "(%s)", str2) == -1) {
2012 error = got_error_from_errno("asprintf");
2013 goto done;
2015 } else {
2016 error = gw_strdup_string(&ref_str, "", NULL);
2017 if (error)
2018 goto done;
2021 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2022 KATTR_ID, "header_commit_title", KATTR__MAX);
2023 if (kerr != KCGI_OK)
2024 goto done;
2025 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
2026 if (kerr != KCGI_OK)
2027 goto done;
2028 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2029 if (kerr != KCGI_OK)
2030 goto done;
2031 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2032 KATTR_ID, "header_commit", KATTR__MAX);
2033 if (kerr != KCGI_OK)
2034 goto done;
2035 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2036 if (kerr != KCGI_OK)
2037 goto done;
2038 kerr = khtml_puts(gw_trans->gw_html_req, " ");
2039 if (kerr != KCGI_OK)
2040 goto done;
2041 kerr = khtml_puts(gw_trans->gw_html_req, ref_str);
2042 if (kerr != KCGI_OK)
2043 goto done;
2044 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2045 done:
2046 if (error == NULL && kerr != KCGI_OK)
2047 error = gw_kcgi_error(kerr);
2048 return error;
2051 static const struct got_error *
2052 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2054 const struct got_error *error = NULL;
2055 enum kcgi_err kerr = KCGI_OK;
2057 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2058 KATTR_ID, "header_diff_title", KATTR__MAX);
2059 if (kerr != KCGI_OK)
2060 goto done;
2061 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2062 if (kerr != KCGI_OK)
2063 goto done;
2064 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2065 if (kerr != KCGI_OK)
2066 goto done;
2067 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2068 KATTR_ID, "header_diff", KATTR__MAX);
2069 if (kerr != KCGI_OK)
2070 goto done;
2071 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2072 if (kerr != KCGI_OK)
2073 goto done;
2074 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2075 if (kerr != KCGI_OK)
2076 goto done;
2077 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2078 if (kerr != KCGI_OK)
2079 goto done;
2080 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2081 done:
2082 if (error == NULL && kerr != KCGI_OK)
2083 error = gw_kcgi_error(kerr);
2084 return error;
2087 static const struct got_error *
2088 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2090 const struct got_error *error = NULL;
2091 enum kcgi_err kerr = KCGI_OK;
2093 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2094 KATTR_ID, "header_age_title", KATTR__MAX);
2095 if (kerr != KCGI_OK)
2096 goto done;
2097 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2098 if (kerr != KCGI_OK)
2099 goto done;
2100 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2101 if (kerr != KCGI_OK)
2102 goto done;
2103 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2104 KATTR_ID, "header_age", KATTR__MAX);
2105 if (kerr != KCGI_OK)
2106 goto done;
2107 kerr = khtml_puts(gw_trans->gw_html_req, str);
2108 if (kerr != KCGI_OK)
2109 goto done;
2110 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2111 done:
2112 if (error == NULL && kerr != KCGI_OK)
2113 error = gw_kcgi_error(kerr);
2114 return error;
2117 static const struct got_error *
2118 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2120 const struct got_error *error = NULL;
2121 enum kcgi_err kerr;
2123 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2124 KATTR_ID, "header_author_title", KATTR__MAX);
2125 if (kerr != KCGI_OK)
2126 goto done;
2127 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2128 if (kerr != KCGI_OK)
2129 goto done;
2130 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2131 if (kerr != KCGI_OK)
2132 goto done;
2133 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2134 KATTR_ID, "header_author", KATTR__MAX);
2135 if (kerr != KCGI_OK)
2136 goto done;
2137 kerr = khtml_puts(gw_trans->gw_html_req, str);
2138 if (kerr != KCGI_OK)
2139 goto done;
2140 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2141 done:
2142 if (error == NULL && kerr != KCGI_OK)
2143 error = gw_kcgi_error(kerr);
2144 return error;
2147 static const struct got_error *
2148 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2150 const struct got_error *error = NULL;
2151 enum kcgi_err kerr;
2153 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2154 KATTR_ID, "header_committer_title", KATTR__MAX);
2155 if (kerr != KCGI_OK)
2156 goto done;
2157 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2158 if (kerr != KCGI_OK)
2159 goto done;
2160 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2161 if (kerr != KCGI_OK)
2162 goto done;
2163 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2164 KATTR_ID, "header_committer", KATTR__MAX);
2165 if (kerr != KCGI_OK)
2166 goto done;
2167 kerr = khtml_puts(gw_trans->gw_html_req, str);
2168 if (kerr != KCGI_OK)
2169 goto done;
2170 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2171 done:
2172 if (error == NULL && kerr != KCGI_OK)
2173 error = gw_kcgi_error(kerr);
2174 return error;
2177 static const struct got_error *
2178 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2180 const struct got_error *error = NULL;
2181 enum kcgi_err kerr;
2183 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2184 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2185 if (kerr != KCGI_OK)
2186 goto done;
2187 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2188 if (kerr != KCGI_OK)
2189 goto done;
2190 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2191 if (kerr != KCGI_OK)
2192 goto done;
2193 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2194 KATTR_ID, "header_commit_msg", KATTR__MAX);
2195 if (kerr != KCGI_OK)
2196 goto done;
2197 kerr = khttp_puts(gw_trans->gw_req, str);
2198 if (kerr != KCGI_OK)
2199 goto done;
2200 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2201 done:
2202 if (error == NULL && kerr != KCGI_OK)
2203 error = gw_kcgi_error(kerr);
2204 return error;
2207 static const struct got_error *
2208 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2210 const struct got_error *error = NULL;
2211 enum kcgi_err kerr;
2213 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2214 KATTR_ID, "header_tree_title", KATTR__MAX);
2215 if (kerr != KCGI_OK)
2216 goto done;
2217 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2218 if (kerr != KCGI_OK)
2219 goto done;
2220 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2221 if (kerr != KCGI_OK)
2222 goto done;
2223 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2224 KATTR_ID, "header_tree", KATTR__MAX);
2225 if (kerr != KCGI_OK)
2226 goto done;
2227 kerr = khtml_puts(gw_trans->gw_html_req, str);
2228 if (kerr != KCGI_OK)
2229 goto done;
2230 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2231 done:
2232 if (error == NULL && kerr != KCGI_OK)
2233 error = gw_kcgi_error(kerr);
2234 return error;
2237 static const struct got_error *
2238 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2239 char *dir)
2241 const struct got_error *error = NULL;
2242 FILE *f = NULL;
2243 char *d_file = NULL;
2244 unsigned int len;
2245 size_t n;
2247 *description = NULL;
2248 if (gw_trans->gw_conf->got_show_repo_description == 0)
2249 return NULL;
2251 if (asprintf(&d_file, "%s/description", dir) == -1)
2252 return got_error_from_errno("asprintf");
2254 f = fopen(d_file, "r");
2255 if (f == NULL) {
2256 if (errno == ENOENT || errno == EACCES)
2257 return NULL;
2258 error = got_error_from_errno2("fopen", d_file);
2259 goto done;
2262 if (fseek(f, 0, SEEK_END) == -1) {
2263 error = got_ferror(f, GOT_ERR_IO);
2264 goto done;
2266 len = ftell(f);
2267 if (len == -1) {
2268 error = got_ferror(f, GOT_ERR_IO);
2269 goto done;
2271 if (fseek(f, 0, SEEK_SET) == -1) {
2272 error = got_ferror(f, GOT_ERR_IO);
2273 goto done;
2275 *description = calloc(len + 1, sizeof(**description));
2276 if (*description == NULL) {
2277 error = got_error_from_errno("calloc");
2278 goto done;
2281 n = fread(*description, 1, len, f);
2282 if (n == 0 && ferror(f))
2283 error = got_ferror(f, GOT_ERR_IO);
2284 done:
2285 if (f != NULL && fclose(f) == -1 && error == NULL)
2286 error = got_error_from_errno("fclose");
2287 free(d_file);
2288 return error;
2291 static const struct got_error *
2292 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2294 struct tm tm;
2295 time_t diff_time;
2296 char *years = "years ago", *months = "months ago";
2297 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2298 char *minutes = "minutes ago", *seconds = "seconds ago";
2299 char *now = "right now";
2300 char *s;
2301 char datebuf[29];
2303 *repo_age = NULL;
2305 switch (ref_tm) {
2306 case TM_DIFF:
2307 diff_time = time(NULL) - committer_time;
2308 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2309 if (asprintf(repo_age, "%lld %s",
2310 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2311 return got_error_from_errno("asprintf");
2312 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2313 if (asprintf(repo_age, "%lld %s",
2314 (diff_time / 60 / 60 / 24 / (365 / 12)),
2315 months) == -1)
2316 return got_error_from_errno("asprintf");
2317 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2318 if (asprintf(repo_age, "%lld %s",
2319 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2320 return got_error_from_errno("asprintf");
2321 } else if (diff_time > 60 * 60 * 24 * 2) {
2322 if (asprintf(repo_age, "%lld %s",
2323 (diff_time / 60 / 60 / 24), days) == -1)
2324 return got_error_from_errno("asprintf");
2325 } else if (diff_time > 60 * 60 * 2) {
2326 if (asprintf(repo_age, "%lld %s",
2327 (diff_time / 60 / 60), hours) == -1)
2328 return got_error_from_errno("asprintf");
2329 } else if (diff_time > 60 * 2) {
2330 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2331 minutes) == -1)
2332 return got_error_from_errno("asprintf");
2333 } else if (diff_time > 2) {
2334 if (asprintf(repo_age, "%lld %s", diff_time,
2335 seconds) == -1)
2336 return got_error_from_errno("asprintf");
2337 } else {
2338 if (asprintf(repo_age, "%s", now) == -1)
2339 return got_error_from_errno("asprintf");
2341 break;
2342 case TM_LONG:
2343 if (gmtime_r(&committer_time, &tm) == NULL)
2344 return got_error_from_errno("gmtime_r");
2346 s = asctime_r(&tm, datebuf);
2347 if (s == NULL)
2348 return got_error_from_errno("asctime_r");
2350 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2351 return got_error_from_errno("asprintf");
2352 break;
2354 return NULL;
2357 static const struct got_error *
2358 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2359 char *repo_ref, int ref_tm)
2361 const struct got_error *error = NULL;
2362 struct got_object_id *id = NULL;
2363 struct got_repository *repo = NULL;
2364 struct got_commit_object *commit = NULL;
2365 struct got_reflist_head refs;
2366 struct got_reflist_entry *re;
2367 struct got_reference *head_ref;
2368 int is_head = 0;
2369 time_t committer_time = 0, cmp_time = 0;
2370 char *refname;
2372 *repo_age = NULL;
2373 SIMPLEQ_INIT(&refs);
2375 if (repo_ref == NULL)
2376 return NULL;
2378 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
2379 is_head = 1;
2381 if (gw_trans->gw_conf->got_show_repo_age == 0)
2382 return NULL;
2384 error = got_repo_open(&repo, dir, NULL);
2385 if (error)
2386 goto done;
2388 if (is_head)
2389 error = got_ref_list(&refs, repo, "refs/heads",
2390 got_ref_cmp_by_name, NULL);
2391 else
2392 error = got_ref_list(&refs, repo, repo_ref,
2393 got_ref_cmp_by_name, NULL);
2394 if (error)
2395 goto done;
2397 SIMPLEQ_FOREACH(re, &refs, entry) {
2398 if (is_head) {
2399 error = gw_strdup_string(&refname, repo_ref, NULL);
2400 if (error)
2401 goto done;
2402 } else {
2403 error = gw_strdup_string(&refname, NULL,
2404 got_ref_get_name(re->ref));
2405 if (error)
2406 goto done;
2408 error = got_ref_open(&head_ref, repo, refname, 0);
2409 if (error)
2410 goto done;
2412 error = got_ref_resolve(&id, repo, head_ref);
2413 got_ref_close(head_ref);
2414 if (error)
2415 goto done;
2417 error = got_object_open_as_commit(&commit, repo, id);
2418 if (error)
2419 goto done;
2421 committer_time =
2422 got_object_commit_get_committer_time(commit);
2424 if (cmp_time < committer_time)
2425 cmp_time = committer_time;
2428 if (cmp_time != 0) {
2429 committer_time = cmp_time;
2430 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2432 done:
2433 got_ref_list_free(&refs);
2434 free(id);
2435 return error;
2438 static const struct got_error *
2439 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2441 const struct got_error *error;
2442 FILE *f = NULL;
2443 struct got_object_id *id1 = NULL, *id2 = NULL;
2444 char *label1 = NULL, *label2 = NULL, *line = NULL;
2445 int obj_type;
2446 size_t linesize = 0;
2447 ssize_t linelen;
2448 enum kcgi_err kerr = KCGI_OK;
2450 f = got_opentemp();
2451 if (f == NULL)
2452 return NULL;
2454 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
2455 if (error)
2456 goto done;
2458 if (strncmp(header->parent_id, "/dev/null", 9) != 0) {
2459 error = got_repo_match_object_id(&id1, &label1,
2460 header->parent_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2461 if (error)
2462 goto done;
2465 error = got_repo_match_object_id(&id2, &label2,
2466 header->commit_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2467 if (error)
2468 goto done;
2470 error = got_object_get_type(&obj_type, header->repo, id2);
2471 if (error)
2472 goto done;
2473 switch (obj_type) {
2474 case GOT_OBJ_TYPE_BLOB:
2475 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
2476 header->repo, f);
2477 break;
2478 case GOT_OBJ_TYPE_TREE:
2479 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
2480 header->repo, f);
2481 break;
2482 case GOT_OBJ_TYPE_COMMIT:
2483 error = got_diff_objects_as_commits(id1, id2, 3, 0,
2484 header->repo, f);
2485 break;
2486 default:
2487 error = got_error(GOT_ERR_OBJ_TYPE);
2489 if (error)
2490 goto done;
2492 if (fseek(f, 0, SEEK_SET) == -1) {
2493 error = got_ferror(f, GOT_ERR_IO);
2494 goto done;
2497 while ((linelen = getline(&line, &linesize, f)) != -1) {
2498 error = gw_colordiff_line(gw_trans, line);
2499 if (error)
2500 goto done;
2501 /* XXX: KHTML_PRETTY breaks this */
2502 kerr = khtml_puts(gw_trans->gw_html_req, line);
2503 if (kerr != KCGI_OK)
2504 goto done;
2505 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2506 if (kerr != KCGI_OK)
2507 goto done;
2509 if (linelen == -1 && ferror(f))
2510 error = got_error_from_errno("getline");
2511 done:
2512 if (f && fclose(f) == -1 && error == NULL)
2513 error = got_error_from_errno("fclose");
2514 free(line);
2515 free(label1);
2516 free(label2);
2517 free(id1);
2518 free(id2);
2520 if (error == NULL && kerr != KCGI_OK)
2521 error = gw_kcgi_error(kerr);
2522 return error;
2525 static const struct got_error *
2526 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2528 const struct got_error *error = NULL;
2529 struct got_repository *repo;
2530 const char *gitconfig_owner;
2532 *owner = NULL;
2534 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2535 return NULL;
2537 error = got_repo_open(&repo, dir, NULL);
2538 if (error)
2539 return error;
2540 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2541 if (gitconfig_owner)
2542 error = gw_strdup_string(owner, NULL, gitconfig_owner);
2543 got_repo_close(repo);
2544 return error;
2547 static const struct got_error *
2548 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2550 const struct got_error *error = NULL;
2551 FILE *f;
2552 char *d_file = NULL;
2553 unsigned int len;
2554 size_t n;
2556 *url = NULL;
2558 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2559 return got_error_from_errno("asprintf");
2561 f = fopen(d_file, "r");
2562 if (f == NULL) {
2563 if (errno != ENOENT && errno != EACCES)
2564 error = got_error_from_errno2("fopen", d_file);
2565 goto done;
2568 if (fseek(f, 0, SEEK_END) == -1) {
2569 error = got_ferror(f, GOT_ERR_IO);
2570 goto done;
2572 len = ftell(f);
2573 if (len == -1) {
2574 error = got_ferror(f, GOT_ERR_IO);
2575 goto done;
2577 if (fseek(f, 0, SEEK_SET) == -1) {
2578 error = got_ferror(f, GOT_ERR_IO);
2579 goto done;
2582 *url = calloc(len + 1, sizeof(**url));
2583 if (*url == NULL) {
2584 error = got_error_from_errno("calloc");
2585 goto done;
2588 n = fread(*url, 1, len, f);
2589 if (n == 0 && ferror(f))
2590 error = got_ferror(f, GOT_ERR_IO);
2591 done:
2592 if (f && fclose(f) == -1 && error == NULL)
2593 error = got_error_from_errno("fclose");
2594 free(d_file);
2595 return NULL;
2598 static const struct got_error *
2599 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
2600 int limit, int tag_type)
2602 const struct got_error *error = NULL;
2603 struct got_repository *repo = NULL;
2604 struct got_reflist_head refs;
2605 struct got_reflist_entry *re;
2606 char *age = NULL;
2607 char *id_str = NULL, *newline, *href_commits = NULL;
2608 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
2609 struct got_tag_object *tag = NULL;
2610 enum kcgi_err kerr = KCGI_OK;
2611 int summary_header_displayed = 0;
2613 SIMPLEQ_INIT(&refs);
2615 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2616 if (error)
2617 return error;
2619 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
2620 if (error)
2621 goto done;
2623 SIMPLEQ_FOREACH(re, &refs, entry) {
2624 const char *refname;
2625 const char *tagger;
2626 const char *tag_commit;
2627 time_t tagger_time;
2628 struct got_object_id *id;
2629 struct got_commit_object *commit = NULL;
2631 refname = got_ref_get_name(re->ref);
2632 if (strncmp(refname, "refs/tags/", 10) != 0)
2633 continue;
2634 refname += 10;
2636 error = got_ref_resolve(&id, repo, re->ref);
2637 if (error)
2638 goto done;
2640 error = got_object_open_as_tag(&tag, repo, id);
2641 if (error) {
2642 if (error->code != GOT_ERR_OBJ_TYPE) {
2643 free(id);
2644 goto done;
2646 /* "lightweight" tag */
2647 error = got_object_open_as_commit(&commit, repo, id);
2648 if (error) {
2649 free(id);
2650 goto done;
2652 tagger = got_object_commit_get_committer(commit);
2653 tagger_time =
2654 got_object_commit_get_committer_time(commit);
2655 error = got_object_id_str(&id_str, id);
2656 free(id);
2657 } else {
2658 free(id);
2659 tagger = got_object_tag_get_tagger(tag);
2660 tagger_time = got_object_tag_get_tagger_time(tag);
2661 error = got_object_id_str(&id_str,
2662 got_object_tag_get_object_id(tag));
2664 if (error)
2665 goto done;
2667 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
2668 strlen(id_str)) != 0)
2669 continue;
2671 if (commit) {
2672 error = got_object_commit_get_logmsg(&tag_commit0,
2673 commit);
2674 if (error)
2675 goto done;
2676 got_object_commit_close(commit);
2677 } else {
2678 tag_commit0 = strdup(got_object_tag_get_message(tag));
2679 if (tag_commit0 == NULL) {
2680 error = got_error_from_errno("strdup");
2681 goto done;
2685 tag_commit = tag_commit0;
2686 while (*tag_commit == '\n')
2687 tag_commit++;
2689 switch (tag_type) {
2690 case TAGBRIEF:
2691 newline = strchr(tag_commit, '\n');
2692 if (newline)
2693 *newline = '\0';
2695 if (summary_header_displayed == 0) {
2696 kerr = khtml_attr(gw_trans->gw_html_req,
2697 KELEM_DIV, KATTR_ID,
2698 "summary_tags_title_wrapper", KATTR__MAX);
2699 if (kerr != KCGI_OK)
2700 goto done;
2701 kerr = khtml_attr(gw_trans->gw_html_req,
2702 KELEM_DIV, KATTR_ID,
2703 "summary_tags_title", KATTR__MAX);
2704 if (kerr != KCGI_OK)
2705 goto done;
2706 kerr = khtml_puts(gw_trans->gw_html_req,
2707 "Tags");
2708 if (kerr != KCGI_OK)
2709 goto done;
2710 kerr = khtml_closeelem(gw_trans->gw_html_req,
2711 2);
2712 if (kerr != KCGI_OK)
2713 goto done;
2714 kerr = khtml_attr(gw_trans->gw_html_req,
2715 KELEM_DIV, KATTR_ID,
2716 "summary_tags_content", KATTR__MAX);
2717 if (kerr != KCGI_OK)
2718 goto done;
2719 summary_header_displayed = 1;
2722 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2723 KATTR_ID, "tags_wrapper", KATTR__MAX);
2724 if (kerr != KCGI_OK)
2725 goto done;
2726 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2727 KATTR_ID, "tags_age", KATTR__MAX);
2728 if (kerr != KCGI_OK)
2729 goto done;
2730 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
2731 if (error)
2732 goto done;
2733 kerr = khtml_puts(gw_trans->gw_html_req,
2734 age ? age : "");
2735 if (kerr != KCGI_OK)
2736 goto done;
2737 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2738 if (kerr != KCGI_OK)
2739 goto done;
2740 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2741 KATTR_ID, "tags", KATTR__MAX);
2742 if (kerr != KCGI_OK)
2743 goto done;
2744 kerr = khtml_puts(gw_trans->gw_html_req, refname);
2745 if (kerr != KCGI_OK)
2746 goto done;
2747 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2748 if (kerr != KCGI_OK)
2749 goto done;
2750 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2751 KATTR_ID, "tags_name", KATTR__MAX);
2752 if (kerr != KCGI_OK)
2753 goto done;
2754 if (asprintf(&href_tag, "?path=%s&action=tag&commit=%s",
2755 gw_trans->repo_name, id_str) == -1) {
2756 error = got_error_from_errno("asprintf");
2757 goto done;
2759 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2760 KATTR_HREF, href_tag, KATTR__MAX);
2761 if (kerr != KCGI_OK)
2762 goto done;
2763 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
2764 if (kerr != KCGI_OK)
2765 goto done;
2766 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2767 if (kerr != KCGI_OK)
2768 goto done;
2770 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2771 KATTR_ID, "navs_wrapper", KATTR__MAX);
2772 if (kerr != KCGI_OK)
2773 goto done;
2774 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2775 KATTR_ID, "navs", KATTR__MAX);
2776 if (kerr != KCGI_OK)
2777 goto done;
2779 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2780 KATTR_HREF, href_tag, KATTR__MAX);
2781 if (kerr != KCGI_OK)
2782 goto done;
2783 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
2784 if (kerr != KCGI_OK)
2785 goto done;
2786 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2787 if (kerr != KCGI_OK)
2788 goto done;
2790 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2791 if (kerr != KCGI_OK)
2792 goto done;
2793 if (asprintf(&href_briefs,
2794 "?path=%s&action=briefs&commit=%s",
2795 gw_trans->repo_name, id_str) == -1) {
2796 error = got_error_from_errno("asprintf");
2797 goto done;
2799 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2800 KATTR_HREF, href_briefs, KATTR__MAX);
2801 if (kerr != KCGI_OK)
2802 goto done;
2803 kerr = khtml_puts(gw_trans->gw_html_req,
2804 "commit briefs");
2805 if (kerr != KCGI_OK)
2806 goto done;
2807 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2808 if (kerr != KCGI_OK)
2809 goto done;
2811 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2812 if (kerr != KCGI_OK)
2813 goto done;
2815 if (asprintf(&href_commits,
2816 "?path=%s&action=commits&commit=%s",
2817 gw_trans->repo_name, id_str) == -1) {
2818 error = got_error_from_errno("asprintf");
2819 goto done;
2821 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2822 KATTR_HREF, href_commits, KATTR__MAX);
2823 if (kerr != KCGI_OK)
2824 goto done;
2825 kerr = khtml_puts(gw_trans->gw_html_req,
2826 "commits");
2827 if (kerr != KCGI_OK)
2828 goto done;
2829 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2830 if (kerr != KCGI_OK)
2831 goto done;
2833 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2834 KATTR_ID, "dotted_line", KATTR__MAX);
2835 if (kerr != KCGI_OK)
2836 goto done;
2837 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2838 if (kerr != KCGI_OK)
2839 goto done;
2840 break;
2841 case TAGFULL:
2842 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2843 KATTR_ID, "tag_info_date_title", KATTR__MAX);
2844 if (kerr != KCGI_OK)
2845 goto done;
2846 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
2847 if (kerr != KCGI_OK)
2848 goto done;
2849 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2850 if (kerr != KCGI_OK)
2851 goto done;
2852 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2853 KATTR_ID, "tag_info_date", KATTR__MAX);
2854 if (kerr != KCGI_OK)
2855 goto done;
2856 error = gw_get_time_str(&age, tagger_time, TM_LONG);
2857 if (error)
2858 goto done;
2859 kerr = khtml_puts(gw_trans->gw_html_req,
2860 age ? age : "");
2861 if (kerr != KCGI_OK)
2862 goto done;
2863 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2864 if (kerr != KCGI_OK)
2865 goto done;
2867 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2868 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
2869 if (kerr != KCGI_OK)
2870 goto done;
2871 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
2872 if (kerr != KCGI_OK)
2873 goto done;
2874 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2875 if (kerr != KCGI_OK)
2876 goto done;
2877 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2878 KATTR_ID, "tag_info_date", KATTR__MAX);
2879 if (kerr != KCGI_OK)
2880 goto done;
2881 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
2882 if (kerr != KCGI_OK)
2883 goto done;
2884 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2885 if (kerr != KCGI_OK)
2886 goto done;
2888 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2889 KATTR_ID, "tag_info", KATTR__MAX);
2890 if (kerr != KCGI_OK)
2891 goto done;
2892 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
2893 if (kerr != KCGI_OK)
2894 goto done;
2895 break;
2896 default:
2897 break;
2899 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2900 if (kerr != KCGI_OK)
2901 goto done;
2903 if (limit && --limit == 0)
2904 break;
2906 if (tag)
2907 got_object_tag_close(tag);
2908 tag = NULL;
2909 free(id_str);
2910 id_str = NULL;
2911 free(age);
2912 age = NULL;
2913 free(tag_commit0);
2914 tag_commit0 = NULL;
2915 free(href_tag);
2916 href_tag = NULL;
2917 free(href_briefs);
2918 href_briefs = NULL;
2919 free(href_commits);
2920 href_commits = NULL;
2922 done:
2923 if (tag)
2924 got_object_tag_close(tag);
2925 free(id_str);
2926 free(age);
2927 free(tag_commit0);
2928 free(href_tag);
2929 free(href_briefs);
2930 free(href_commits);
2931 got_ref_list_free(&refs);
2932 if (repo)
2933 got_repo_close(repo);
2934 if (error == NULL && kerr != KCGI_OK)
2935 error = gw_kcgi_error(kerr);
2936 return error;
2939 static void
2940 gw_free_headers(struct gw_header *header)
2942 free(header->id);
2943 free(header->path);
2944 if (header->commit != NULL)
2945 got_object_commit_close(header->commit);
2946 if (header->repo)
2947 got_repo_close(header->repo);
2948 free(header->refs_str);
2949 free(header->commit_id);
2950 free(header->author);
2951 free(header->committer);
2952 free(header->parent_id);
2953 free(header->tree_id);
2954 free(header->commit_msg);
2957 static struct gw_header *
2958 gw_init_header()
2960 struct gw_header *header;
2962 header = malloc(sizeof(*header));
2963 if (header == NULL)
2964 return NULL;
2966 header->repo = NULL;
2967 header->commit = NULL;
2968 header->id = NULL;
2969 header->path = NULL;
2970 SIMPLEQ_INIT(&header->refs);
2972 return header;
2975 static const struct got_error *
2976 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
2977 int limit)
2979 const struct got_error *error = NULL;
2980 struct got_commit_graph *graph = NULL;
2982 error = got_commit_graph_open(&graph, header->path, 0);
2983 if (error)
2984 return error;
2986 error = got_commit_graph_iter_start(graph, header->id, header->repo,
2987 NULL, NULL);
2988 if (error)
2989 goto done;
2991 for (;;) {
2992 error = got_commit_graph_iter_next(&header->id, graph,
2993 header->repo, NULL, NULL);
2994 if (error) {
2995 if (error->code == GOT_ERR_ITER_COMPLETED)
2996 error = NULL;
2997 goto done;
2999 if (header->id == NULL)
3000 goto done;
3002 error = got_object_open_as_commit(&header->commit, header->repo,
3003 header->id);
3004 if (error)
3005 goto done;
3007 error = gw_get_commit(gw_trans, header);
3008 if (limit > 1) {
3009 struct gw_header *n_header = NULL;
3010 if ((n_header = gw_init_header()) == NULL) {
3011 error = got_error_from_errno("malloc");
3012 goto done;
3015 error = gw_strdup_string(&n_header->refs_str,
3016 header->refs_str, NULL);
3017 if (error)
3018 goto done;
3019 error = gw_strdup_string(&n_header->commit_id,
3020 header->commit_id, NULL);
3021 if (error)
3022 goto done;
3023 error = gw_strdup_string(&n_header->parent_id,
3024 header->parent_id, NULL);
3025 if (error)
3026 goto done;
3027 error = gw_strdup_string(&n_header->tree_id,
3028 header->tree_id, NULL);
3029 if (error)
3030 goto done;
3031 error = gw_strdup_string(&n_header->author,
3032 header->author, NULL);
3033 if (error)
3034 goto done;
3035 error = gw_strdup_string(&n_header->committer,
3036 header->committer, NULL);
3037 if (error)
3038 goto done;
3039 error = gw_strdup_string(&n_header->commit_msg,
3040 header->commit_msg, NULL);
3041 if (error)
3042 goto done;
3043 n_header->committer_time = header->committer_time;
3044 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3045 entry);
3047 if (error || (limit && --limit == 0))
3048 break;
3050 done:
3051 if (graph)
3052 got_commit_graph_close(graph);
3053 return error;
3056 static const struct got_error *
3057 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
3059 const struct got_error *error = NULL;
3060 struct got_reflist_entry *re;
3061 struct got_object_id *id2 = NULL;
3062 struct got_object_qid *parent_id;
3063 char *refs_str = NULL, *commit_msg = NULL, *commit_msg0;
3065 /*print commit*/
3066 SIMPLEQ_FOREACH(re, &header->refs, entry) {
3067 char *s;
3068 const char *name;
3069 struct got_tag_object *tag = NULL;
3070 int cmp;
3072 name = got_ref_get_name(re->ref);
3073 if (strcmp(name, GOT_REF_HEAD) == 0)
3074 continue;
3075 if (strncmp(name, "refs/", 5) == 0)
3076 name += 5;
3077 if (strncmp(name, "got/", 4) == 0)
3078 continue;
3079 if (strncmp(name, "heads/", 6) == 0)
3080 name += 6;
3081 if (strncmp(name, "remotes/", 8) == 0)
3082 name += 8;
3083 if (strncmp(name, "tags/", 5) == 0) {
3084 error = got_object_open_as_tag(&tag, header->repo,
3085 re->id);
3086 if (error) {
3087 if (error->code != GOT_ERR_OBJ_TYPE)
3088 continue;
3090 * Ref points at something other
3091 * than a tag.
3093 error = NULL;
3094 tag = NULL;
3097 cmp = got_object_id_cmp(tag ?
3098 got_object_tag_get_object_id(tag) : re->id, header->id);
3099 if (tag)
3100 got_object_tag_close(tag);
3101 if (cmp != 0)
3102 continue;
3103 s = refs_str;
3104 if (asprintf(&refs_str, "%s%s%s", s ? s : "",
3105 s ? ", " : "", name) == -1) {
3106 error = got_error_from_errno("asprintf");
3107 free(s);
3108 return error;
3110 error = gw_strdup_string(&header->refs_str, refs_str, NULL);
3111 free(s);
3112 if (error)
3113 return error;
3116 if (refs_str == NULL) {
3117 error = gw_strdup_string(&header->refs_str, "", NULL);
3118 if (error)
3119 return error;
3121 free(refs_str);
3123 error = got_object_id_str(&header->commit_id, header->id);
3124 if (error)
3125 return error;
3127 error = got_object_id_str(&header->tree_id,
3128 got_object_commit_get_tree_id(header->commit));
3129 if (error)
3130 return error;
3132 if (gw_trans->action == GW_DIFF) {
3133 parent_id = SIMPLEQ_FIRST(
3134 got_object_commit_get_parent_ids(header->commit));
3135 if (parent_id != NULL) {
3136 id2 = got_object_id_dup(parent_id->id);
3137 free (parent_id);
3138 error = got_object_id_str(&header->parent_id, id2);
3139 if (error)
3140 return error;
3141 free(id2);
3142 } else {
3143 error = gw_strdup_string(&header->parent_id,
3144 "/dev/null", NULL);
3145 if (error)
3146 return error;
3148 } else {
3149 error = gw_strdup_string(&header->parent_id, "", NULL);
3150 if (error)
3151 return error;
3154 header->committer_time =
3155 got_object_commit_get_committer_time(header->commit);
3157 error = gw_strdup_string(&header->author, NULL,
3158 got_object_commit_get_author(header->commit));
3159 if (error)
3160 return error;
3161 error = gw_strdup_string(&header->committer, NULL,
3162 got_object_commit_get_committer(header->commit));
3163 if (error)
3164 return error;
3165 error = got_object_commit_get_logmsg(&commit_msg0, header->commit);
3166 if (error)
3167 return error;
3169 commit_msg = commit_msg0;
3170 while (*commit_msg == '\n')
3171 commit_msg++;
3173 error = gw_strdup_string(&header->commit_msg, commit_msg, NULL);
3174 free(commit_msg0);
3175 return error;
3178 static const struct got_error *
3179 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3181 const struct got_error *error = NULL;
3182 char *in_repo_path = NULL;
3184 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
3185 if (error)
3186 return error;
3188 if (gw_trans->commit == NULL) {
3189 struct got_reference *head_ref;
3190 error = got_ref_open(&head_ref, header->repo,
3191 gw_trans->headref, 0);
3192 if (error)
3193 return error;
3195 error = got_ref_resolve(&header->id, header->repo, head_ref);
3196 got_ref_close(head_ref);
3197 if (error)
3198 return error;
3200 error = got_object_open_as_commit(&header->commit,
3201 header->repo, header->id);
3202 } else {
3203 struct got_reference *ref;
3204 error = got_ref_open(&ref, header->repo, gw_trans->commit, 0);
3205 if (error == NULL) {
3206 int obj_type;
3207 error = got_ref_resolve(&header->id, header->repo, ref);
3208 got_ref_close(ref);
3209 if (error)
3210 return error;
3211 error = got_object_get_type(&obj_type, header->repo,
3212 header->id);
3213 if (error)
3214 return error;
3215 if (obj_type == GOT_OBJ_TYPE_TAG) {
3216 struct got_tag_object *tag;
3217 error = got_object_open_as_tag(&tag,
3218 header->repo, header->id);
3219 if (error)
3220 return error;
3221 if (got_object_tag_get_object_type(tag) !=
3222 GOT_OBJ_TYPE_COMMIT) {
3223 got_object_tag_close(tag);
3224 error = got_error(GOT_ERR_OBJ_TYPE);
3225 return error;
3227 free(header->id);
3228 header->id = got_object_id_dup(
3229 got_object_tag_get_object_id(tag));
3230 if (header->id == NULL)
3231 error = got_error_from_errno(
3232 "got_object_id_dup");
3233 got_object_tag_close(tag);
3234 if (error)
3235 return error;
3236 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3237 error = got_error(GOT_ERR_OBJ_TYPE);
3238 return error;
3240 error = got_object_open_as_commit(&header->commit,
3241 header->repo, header->id);
3242 if (error)
3243 return error;
3245 if (header->commit == NULL) {
3246 error = got_repo_match_object_id_prefix(&header->id,
3247 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
3248 header->repo);
3249 if (error)
3250 return error;
3252 error = got_repo_match_object_id_prefix(&header->id,
3253 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
3254 header->repo);
3257 error = got_repo_map_path(&in_repo_path, header->repo,
3258 gw_trans->repo_path, 1);
3259 if (error)
3260 return error;
3262 if (in_repo_path) {
3263 error = gw_strdup_string(&header->path, in_repo_path, NULL);
3264 if (error) {
3265 free(in_repo_path);
3266 return error;
3269 free(in_repo_path);
3271 error = got_ref_list(&header->refs, header->repo, NULL,
3272 got_ref_cmp_by_name, NULL);
3273 if (error)
3274 return error;
3276 error = gw_get_commits(gw_trans, header, limit);
3277 return error;
3280 struct blame_line {
3281 int annotated;
3282 char *id_str;
3283 char *committer;
3284 char datebuf[11]; /* YYYY-MM-DD + NUL */
3287 struct gw_blame_cb_args {
3288 struct blame_line *lines;
3289 int nlines;
3290 int nlines_prec;
3291 int lineno_cur;
3292 off_t *line_offsets;
3293 FILE *f;
3294 struct got_repository *repo;
3295 struct gw_trans *gw_trans;
3298 static const struct got_error *
3299 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3301 const struct got_error *err = NULL;
3302 struct gw_blame_cb_args *a = arg;
3303 struct blame_line *bline;
3304 char *line = NULL;
3305 size_t linesize = 0;
3306 struct got_commit_object *commit = NULL;
3307 off_t offset;
3308 struct tm tm;
3309 time_t committer_time;
3310 enum kcgi_err kerr = KCGI_OK;
3312 if (nlines != a->nlines ||
3313 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3314 return got_error(GOT_ERR_RANGE);
3316 if (lineno == -1)
3317 return NULL; /* no change in this commit */
3319 /* Annotate this line. */
3320 bline = &a->lines[lineno - 1];
3321 if (bline->annotated)
3322 return NULL;
3323 err = got_object_id_str(&bline->id_str, id);
3324 if (err)
3325 return err;
3327 err = got_object_open_as_commit(&commit, a->repo, id);
3328 if (err)
3329 goto done;
3331 err = gw_strdup_string(&bline->committer, NULL,
3332 got_object_commit_get_committer(commit));
3333 if (err)
3334 goto done;
3336 committer_time = got_object_commit_get_committer_time(commit);
3337 if (localtime_r(&committer_time, &tm) == NULL)
3338 return got_error_from_errno("localtime_r");
3339 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3340 &tm) >= sizeof(bline->datebuf)) {
3341 err = got_error(GOT_ERR_NO_SPACE);
3342 goto done;
3344 bline->annotated = 1;
3346 /* Print lines annotated so far. */
3347 bline = &a->lines[a->lineno_cur - 1];
3348 if (!bline->annotated)
3349 goto done;
3351 offset = a->line_offsets[a->lineno_cur - 1];
3352 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3353 err = got_error_from_errno("fseeko");
3354 goto done;
3357 while (bline->annotated) {
3358 char *smallerthan, *at, *nl, *committer;
3359 char *lineno = NULL, *href_diff = NULL, *href_link = NULL;
3360 size_t len;
3362 if (getline(&line, &linesize, a->f) == -1) {
3363 if (ferror(a->f))
3364 err = got_error_from_errno("getline");
3365 break;
3368 committer = bline->committer;
3369 smallerthan = strchr(committer, '<');
3370 if (smallerthan && smallerthan[1] != '\0')
3371 committer = smallerthan + 1;
3372 at = strchr(committer, '@');
3373 if (at)
3374 *at = '\0';
3375 len = strlen(committer);
3376 if (len >= 9)
3377 committer[8] = '\0';
3379 nl = strchr(line, '\n');
3380 if (nl)
3381 *nl = '\0';
3383 if (a->gw_trans->repo_folder == NULL) {
3384 err = gw_strdup_string(&a->gw_trans->repo_folder, "",
3385 NULL);
3386 if (err)
3387 goto err;
3389 if (a->gw_trans->repo_folder == NULL)
3390 goto err;
3392 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3393 "blame_wrapper", KATTR__MAX);
3394 if (kerr != KCGI_OK)
3395 goto err;
3396 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3397 "blame_number", KATTR__MAX);
3398 if (kerr != KCGI_OK)
3399 goto err;
3400 if (asprintf(&lineno, "%.*d", a->nlines_prec,
3401 a->lineno_cur) == -1)
3402 goto err;
3403 kerr = khtml_puts(a->gw_trans->gw_html_req, lineno);
3404 if (kerr != KCGI_OK)
3405 goto err;
3406 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3407 if (kerr != KCGI_OK)
3408 goto err;
3410 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3411 "blame_hash", KATTR__MAX);
3412 if (kerr != KCGI_OK)
3413 goto err;
3414 /* XXX repo_file could be NULL if not present in querystring */
3415 if (asprintf(&href_diff,
3416 "?path=%s&action=diff&commit=%s&file=%s&folder=%s",
3417 a->gw_trans->repo_name, bline->id_str,
3418 a->gw_trans->repo_file, a->gw_trans->repo_folder) == -1) {
3419 err = got_error_from_errno("asprintf");
3420 goto err;
3422 if (asprintf(&href_link, "%.8s", bline->id_str) == -1) {
3423 err = got_error_from_errno("asprintf");
3424 goto err;
3426 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3427 KATTR_HREF, href_diff, KATTR__MAX);
3428 if (kerr != KCGI_OK)
3429 goto done;
3430 kerr = khtml_puts(a->gw_trans->gw_html_req, href_link);
3431 if (kerr != KCGI_OK)
3432 goto err;
3433 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3434 if (kerr != KCGI_OK)
3435 goto err;
3437 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3438 "blame_date", KATTR__MAX);
3439 if (kerr != KCGI_OK)
3440 goto err;
3441 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3442 if (kerr != KCGI_OK)
3443 goto err;
3444 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3445 if (kerr != KCGI_OK)
3446 goto err;
3448 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3449 "blame_author", KATTR__MAX);
3450 if (kerr != KCGI_OK)
3451 goto err;
3452 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
3453 if (kerr != KCGI_OK)
3454 goto err;
3455 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3456 if (kerr != KCGI_OK)
3457 goto err;
3459 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3460 "blame_code", KATTR__MAX);
3461 if (kerr != KCGI_OK)
3462 goto err;
3463 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
3464 if (kerr != KCGI_OK)
3465 goto err;
3466 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3467 if (kerr != KCGI_OK)
3468 goto err;
3470 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3471 if (kerr != KCGI_OK)
3472 goto err;
3474 a->lineno_cur++;
3475 bline = &a->lines[a->lineno_cur - 1];
3476 err:
3477 free(lineno);
3478 free(href_diff);
3479 free(href_link);
3481 done:
3482 if (commit)
3483 got_object_commit_close(commit);
3484 free(line);
3485 if (err == NULL && kerr != KCGI_OK)
3486 err = gw_kcgi_error(kerr);
3487 return err;
3490 static const struct got_error *
3491 gw_output_file_blame(struct gw_trans *gw_trans)
3493 const struct got_error *error = NULL;
3494 struct got_repository *repo = NULL;
3495 struct got_object_id *obj_id = NULL;
3496 struct got_object_id *commit_id = NULL;
3497 struct got_blob_object *blob = NULL;
3498 char *path = NULL, *in_repo_path = NULL;
3499 struct gw_blame_cb_args bca;
3500 int i, obj_type;
3501 size_t filesize;
3503 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3504 if (error)
3505 return error;
3507 /* XXX repo_file could be NULL if not present in querystring */
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 /* XXX repo_file could be NULL if not present in querystring */
3619 if (asprintf(&path, "%s%s%s",
3620 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3621 gw_trans->repo_folder ? "/" : "",
3622 gw_trans->repo_file) == -1) {
3623 error = got_error_from_errno("asprintf");
3624 goto done;
3627 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3628 if (error)
3629 goto done;
3631 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3632 GOT_OBJ_TYPE_COMMIT, 1, repo);
3633 if (error)
3634 goto done;
3636 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3637 if (error)
3638 goto done;
3640 if (obj_id == NULL) {
3641 error = got_error(GOT_ERR_NO_OBJ);
3642 goto done;
3645 error = got_object_get_type(&obj_type, repo, obj_id);
3646 if (error)
3647 goto done;
3649 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3650 error = got_error(GOT_ERR_OBJ_TYPE);
3651 goto done;
3654 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3655 if (error)
3656 goto done;
3658 hdrlen = got_object_blob_get_hdrlen(blob);
3659 do {
3660 error = got_object_blob_read_block(&len, blob);
3661 if (error)
3662 goto done;
3663 buf = got_object_blob_get_read_buf(blob);
3666 * Skip blob object header first time around,
3667 * which also contains a zero byte.
3669 buf += hdrlen;
3670 if (set_mime == 0) {
3671 if (isbinary(buf, len - hdrlen))
3672 gw_trans->mime = KMIME_APP_OCTET_STREAM;
3673 else
3674 gw_trans->mime = KMIME_TEXT_PLAIN;
3675 set_mime = 1;
3676 error = gw_display_index(gw_trans);
3677 if (error)
3678 goto done;
3680 khttp_write(gw_trans->gw_req, buf, len - hdrlen);
3681 hdrlen = 0;
3682 } while (len != 0);
3683 done:
3684 free(in_repo_path);
3685 free(commit_id);
3686 free(obj_id);
3687 free(path);
3688 if (blob)
3689 got_object_blob_close(blob);
3690 if (repo)
3691 got_repo_close(repo);
3692 if (error == NULL && kerr != KCGI_OK)
3693 error = gw_kcgi_error(kerr);
3694 return error;
3697 static const struct got_error *
3698 gw_output_repo_tree(struct gw_trans *gw_trans)
3700 const struct got_error *error = NULL;
3701 struct got_repository *repo = NULL;
3702 struct got_object_id *tree_id = NULL, *commit_id = NULL;
3703 struct got_tree_object *tree = NULL;
3704 char *path = NULL, *in_repo_path = NULL;
3705 char *id_str = NULL;
3706 char *build_folder = NULL;
3707 char *href_blob = NULL, *href_blame = NULL;
3708 const char *class = NULL;
3709 int nentries, i, class_flip = 0;
3710 enum kcgi_err kerr = KCGI_OK;
3712 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3713 if (error)
3714 return error;
3716 if (gw_trans->repo_folder != NULL) {
3717 error = gw_strdup_string(&path, gw_trans->repo_folder, NULL);
3718 if (error)
3719 goto done;
3720 } else {
3721 error = got_repo_map_path(&in_repo_path, repo,
3722 gw_trans->repo_path, 1);
3723 if (error)
3724 goto done;
3725 free(path);
3726 path = in_repo_path;
3729 if (gw_trans->commit == NULL) {
3730 struct got_reference *head_ref;
3731 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
3732 if (error)
3733 goto done;
3734 error = got_ref_resolve(&commit_id, repo, head_ref);
3735 if (error)
3736 goto done;
3737 got_ref_close(head_ref);
3739 } else {
3740 error = got_repo_match_object_id(&commit_id, NULL,
3741 gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
3742 if (error)
3743 goto done;
3747 * XXX gw_trans->commit might have already been allocated in
3748 * gw_parse_querystring(); could we more cleanly seperate values
3749 * we received as arguments from values we compute ourselves?
3751 error = got_object_id_str(&gw_trans->commit, commit_id);
3752 if (error)
3753 goto done;
3755 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
3756 if (error)
3757 goto done;
3759 error = got_object_open_as_tree(&tree, repo, tree_id);
3760 if (error)
3761 goto done;
3763 nentries = got_object_tree_get_nentries(tree);
3764 for (i = 0; i < nentries; i++) {
3765 struct got_tree_entry *te;
3766 const char *modestr = "";
3767 mode_t mode;
3769 te = got_object_tree_get_entry(tree, i);
3771 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
3772 if (error)
3773 goto done;
3775 mode = got_tree_entry_get_mode(te);
3776 if (got_object_tree_entry_is_submodule(te))
3777 modestr = "$";
3778 else if (S_ISLNK(mode))
3779 modestr = "@";
3780 else if (S_ISDIR(mode))
3781 modestr = "/";
3782 else if (mode & S_IXUSR)
3783 modestr = "*";
3785 if (class_flip == 0) {
3786 class = "back_lightgray";
3787 class_flip = 1;
3788 } else {
3789 class = "back_white";
3790 class_flip = 0;
3793 if (S_ISDIR(mode)) {
3794 if (asprintf(&build_folder, "%s/%s",
3795 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3796 got_tree_entry_get_name(te)) == -1) {
3797 error = got_error_from_errno(
3798 "asprintf");
3799 goto done;
3801 if (asprintf(&href_blob,
3802 "?path=%s&action=%s&commit=%s&folder=%s",
3803 gw_trans->repo_name, gw_get_action_name(gw_trans),
3804 gw_trans->commit, build_folder) == -1) {
3805 error = got_error_from_errno("asprintf");
3806 goto done;
3809 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3810 KATTR_ID, "tree_wrapper", KATTR__MAX);
3811 if (kerr != KCGI_OK)
3812 goto done;
3813 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3814 KATTR_ID, "tree_line", KATTR_CLASS, class,
3815 KATTR__MAX);
3816 if (kerr != KCGI_OK)
3817 goto done;
3818 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3819 KATTR_HREF, href_blob, KATTR_CLASS,
3820 "diff_directory", KATTR__MAX);
3821 if (kerr != KCGI_OK)
3822 goto done;
3823 kerr = khtml_puts(gw_trans->gw_html_req,
3824 got_tree_entry_get_name(te));
3825 if (kerr != KCGI_OK)
3826 goto done;
3827 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3828 if (kerr != KCGI_OK)
3829 goto done;
3830 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3831 if (kerr != KCGI_OK)
3832 goto done;
3833 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3834 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
3835 KATTR__MAX);
3836 if (kerr != KCGI_OK)
3837 goto done;
3838 kerr = khtml_entity(gw_trans->gw_html_req,
3839 KENTITY_nbsp);
3840 if (kerr != KCGI_OK)
3841 goto done;
3842 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3843 if (kerr != KCGI_OK)
3844 goto done;
3845 } else {
3846 if (asprintf(&href_blob,
3847 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3848 gw_trans->repo_name, "blob", gw_trans->commit,
3849 got_tree_entry_get_name(te),
3850 gw_trans->repo_folder ?
3851 gw_trans->repo_folder : "") == -1) {
3852 error = got_error_from_errno("asprintf");
3853 goto done;
3855 if (asprintf(&href_blame,
3856 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3857 gw_trans->repo_name, "blame", gw_trans->commit,
3858 got_tree_entry_get_name(te),
3859 gw_trans->repo_folder ?
3860 gw_trans->repo_folder : "") == -1) {
3861 error = got_error_from_errno("asprintf");
3862 goto done;
3865 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3866 KATTR_ID, "tree_wrapper", KATTR__MAX);
3867 if (kerr != KCGI_OK)
3868 goto done;
3869 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3870 KATTR_ID, "tree_line", KATTR_CLASS, class,
3871 KATTR__MAX);
3872 if (kerr != KCGI_OK)
3873 goto done;
3874 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3875 KATTR_HREF, href_blob, KATTR__MAX);
3876 if (kerr != KCGI_OK)
3877 goto done;
3878 kerr = khtml_puts(gw_trans->gw_html_req,
3879 got_tree_entry_get_name(te));
3880 if (kerr != KCGI_OK)
3881 goto done;
3882 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3883 if (kerr != KCGI_OK)
3884 goto done;
3885 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3886 if (kerr != KCGI_OK)
3887 goto done;
3888 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3889 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
3890 KATTR__MAX);
3891 if (kerr != KCGI_OK)
3892 goto done;
3894 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3895 KATTR_HREF, href_blob, KATTR__MAX);
3896 if (kerr != KCGI_OK)
3897 goto done;
3898 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
3899 if (kerr != KCGI_OK)
3900 goto done;
3901 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3902 if (kerr != KCGI_OK)
3903 goto done;
3905 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3906 if (kerr != KCGI_OK)
3907 goto done;
3909 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3910 KATTR_HREF, href_blame, KATTR__MAX);
3911 if (kerr != KCGI_OK)
3912 goto done;
3913 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
3914 if (kerr != KCGI_OK)
3915 goto done;
3917 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3918 if (kerr != KCGI_OK)
3919 goto done;
3921 free(id_str);
3922 id_str = NULL;
3923 free(href_blob);
3924 href_blob = NULL;
3925 free(build_folder);
3926 build_folder = NULL;
3928 done:
3929 if (tree)
3930 got_object_tree_close(tree);
3931 if (repo)
3932 got_repo_close(repo);
3933 free(id_str);
3934 free(href_blob);
3935 free(href_blame);
3936 free(in_repo_path);
3937 free(tree_id);
3938 free(build_folder);
3939 if (error == NULL && kerr != KCGI_OK)
3940 error = gw_kcgi_error(kerr);
3941 return error;
3944 static const struct got_error *
3945 gw_output_repo_heads(struct gw_trans *gw_trans)
3947 const struct got_error *error = NULL;
3948 struct got_repository *repo = NULL;
3949 struct got_reflist_head refs;
3950 struct got_reflist_entry *re;
3951 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
3952 char *href_commits = NULL;
3953 enum kcgi_err kerr = KCGI_OK;
3955 SIMPLEQ_INIT(&refs);
3957 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3958 if (error)
3959 goto done;
3961 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
3962 NULL);
3963 if (error)
3964 goto done;
3966 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3967 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
3968 if (kerr != KCGI_OK)
3969 goto done;
3970 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3971 KATTR_ID, "summary_heads_title", KATTR__MAX);
3972 if (kerr != KCGI_OK)
3973 goto done;
3974 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
3975 if (kerr != KCGI_OK)
3976 goto done;
3977 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3978 if (kerr != KCGI_OK)
3979 goto done;
3980 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3981 KATTR_ID, "summary_heads_content", KATTR__MAX);
3982 if (kerr != KCGI_OK)
3983 goto done;
3985 SIMPLEQ_FOREACH(re, &refs, entry) {
3986 char *refname;
3988 error = gw_strdup_string(&refname, NULL,
3989 got_ref_get_name(re->ref));
3990 if (error)
3991 goto done;
3993 if (strncmp(refname, "refs/heads/", 11) != 0) {
3994 free(refname);
3995 continue;
3998 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
3999 refname, TM_DIFF);
4000 if (error)
4001 goto done;
4003 if (strncmp(refname, "refs/heads/", 11) == 0)
4004 refname += 11;
4006 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4007 KATTR_ID, "heads_wrapper", KATTR__MAX);
4008 if (kerr != KCGI_OK)
4009 goto done;
4010 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4011 KATTR_ID, "heads_age", KATTR__MAX);
4012 if (kerr != KCGI_OK)
4013 goto done;
4014 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
4015 if (kerr != KCGI_OK)
4016 goto done;
4017 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4018 if (kerr != KCGI_OK)
4019 goto done;
4020 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4021 KATTR_ID, "heads_space", KATTR__MAX);
4022 if (kerr != KCGI_OK)
4023 goto done;
4024 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
4025 if (kerr != KCGI_OK)
4026 goto done;
4027 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4028 if (kerr != KCGI_OK)
4029 goto done;
4030 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4031 KATTR_ID, "head", KATTR__MAX);
4032 if (kerr != KCGI_OK)
4033 goto done;
4034 if (asprintf(&href_summary,
4035 "?path=%s&action=summary&headref=%s",
4036 gw_trans->repo_name, refname) == -1) {
4037 error = got_error_from_errno("asprintf");
4038 goto done;
4040 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4041 href_summary, KATTR__MAX);
4042 kerr = khtml_puts(gw_trans->gw_html_req, refname);
4043 if (kerr != KCGI_OK)
4044 goto done;
4045 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4046 if (kerr != KCGI_OK)
4047 goto done;
4049 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4050 "navs_wrapper", KATTR__MAX);
4051 if (kerr != KCGI_OK)
4052 goto done;
4053 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4054 "navs", KATTR__MAX);
4055 if (kerr != KCGI_OK)
4056 goto done;
4058 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4059 href_summary, KATTR__MAX);
4060 if (kerr != KCGI_OK)
4061 goto done;
4062 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4063 if (kerr != KCGI_OK)
4064 goto done;
4065 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4066 if (kerr != KCGI_OK)
4067 goto done;
4069 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4070 if (kerr != KCGI_OK)
4071 goto done;
4072 if (asprintf(&href_briefs, "?path=%s&action=briefs&headref=%s",
4073 gw_trans->repo_name, refname) == -1) {
4074 error = got_error_from_errno("asprintf");
4075 goto done;
4077 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4078 href_briefs, KATTR__MAX);
4079 if (kerr != KCGI_OK)
4080 goto done;
4081 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4082 if (kerr != KCGI_OK)
4083 goto done;
4084 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4085 if (kerr != KCGI_OK)
4086 goto done;
4088 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4089 if (kerr != KCGI_OK)
4090 goto done;
4092 if (asprintf(&href_commits,
4093 "?path=%s&action=commits&headref=%s",
4094 gw_trans->repo_name, refname) == -1) {
4095 error = got_error_from_errno("asprintf");
4096 goto done;
4098 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4099 href_commits, KATTR__MAX);
4100 if (kerr != KCGI_OK)
4101 goto done;
4102 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4103 if (kerr != KCGI_OK)
4104 goto done;
4105 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4106 if (kerr != KCGI_OK)
4107 goto done;
4109 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4110 "dotted_line", KATTR__MAX);
4111 if (kerr != KCGI_OK)
4112 goto done;
4113 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4114 if (kerr != KCGI_OK)
4115 goto done;
4116 free(href_summary);
4117 href_summary = NULL;
4118 free(href_briefs);
4119 href_briefs = NULL;
4120 free(href_commits);
4121 href_commits = NULL;
4123 done:
4124 got_ref_list_free(&refs);
4125 free(href_summary);
4126 free(href_briefs);
4127 free(href_commits);
4128 if (repo)
4129 got_repo_close(repo);
4130 return error;
4133 static const struct got_error *
4134 gw_output_site_link(struct gw_trans *gw_trans)
4136 const struct got_error *error = NULL;
4137 char *href_summary = NULL;
4138 enum kcgi_err kerr = KCGI_OK;
4140 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4141 "site_link", KATTR__MAX);
4142 if (kerr != KCGI_OK)
4143 goto done;
4144 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4145 KATTR__MAX);
4146 if (kerr != KCGI_OK)
4147 goto done;
4148 kerr = khtml_puts(gw_trans->gw_html_req,
4149 gw_trans->gw_conf->got_site_link);
4150 if (kerr != KCGI_OK)
4151 goto done;
4152 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4153 if (kerr != KCGI_OK)
4154 goto done;
4156 if (gw_trans->repo_name != NULL) {
4157 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4158 if (kerr != KCGI_OK)
4159 goto done;
4160 if (asprintf(&href_summary, "?path=%s&action=summary",
4161 gw_trans->repo_name) == -1)
4162 goto done;
4163 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4164 href_summary, KATTR__MAX);
4165 if (kerr != KCGI_OK)
4166 goto done;
4167 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4168 if (kerr != KCGI_OK)
4169 goto done;
4170 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4171 if (kerr != KCGI_OK)
4172 goto done;
4173 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4174 if (kerr != KCGI_OK)
4175 goto done;
4176 kerr = khtml_puts(gw_trans->gw_html_req,
4177 gw_get_action_name(gw_trans));
4178 if (kerr != KCGI_OK)
4179 goto done;
4182 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4183 if (kerr != KCGI_OK)
4184 goto done;
4185 done:
4186 free(href_summary);
4187 return error;
4190 static const struct got_error *
4191 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4193 const struct got_error *error = NULL;
4194 char *color = NULL;
4195 enum kcgi_err kerr = KCGI_OK;
4197 if (strncmp(buf, "-", 1) == 0)
4198 color = "diff_minus";
4199 else if (strncmp(buf, "+", 1) == 0)
4200 color = "diff_plus";
4201 else if (strncmp(buf, "@@", 2) == 0)
4202 color = "diff_chunk_header";
4203 else if (strncmp(buf, "@@", 2) == 0)
4204 color = "diff_chunk_header";
4205 else if (strncmp(buf, "commit +", 8) == 0)
4206 color = "diff_meta";
4207 else if (strncmp(buf, "commit -", 8) == 0)
4208 color = "diff_meta";
4209 else if (strncmp(buf, "blob +", 6) == 0)
4210 color = "diff_meta";
4211 else if (strncmp(buf, "blob -", 6) == 0)
4212 color = "diff_meta";
4213 else if (strncmp(buf, "file +", 6) == 0)
4214 color = "diff_meta";
4215 else if (strncmp(buf, "file -", 6) == 0)
4216 color = "diff_meta";
4217 else if (strncmp(buf, "from:", 5) == 0)
4218 color = "diff_author";
4219 else if (strncmp(buf, "via:", 4) == 0)
4220 color = "diff_author";
4221 else if (strncmp(buf, "date:", 5) == 0)
4222 color = "diff_date";
4223 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4224 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4225 if (error == NULL && kerr != KCGI_OK)
4226 error = gw_kcgi_error(kerr);
4227 return error;
4230 int
4231 main(int argc, char *argv[])
4233 const struct got_error *error = NULL;
4234 struct gw_trans *gw_trans;
4235 struct gw_dir *dir = NULL, *tdir;
4236 const char *page = "index";
4237 int gw_malloc = 1;
4238 enum kcgi_err kerr;
4240 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4241 errx(1, "malloc");
4243 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4244 errx(1, "malloc");
4246 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4247 errx(1, "malloc");
4249 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4250 errx(1, "malloc");
4252 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4253 if (kerr != KCGI_OK) {
4254 error = gw_kcgi_error(kerr);
4255 goto done;
4258 if ((gw_trans->gw_conf =
4259 malloc(sizeof(struct gotweb_conf))) == NULL) {
4260 gw_malloc = 0;
4261 error = got_error_from_errno("malloc");
4262 goto done;
4265 TAILQ_INIT(&gw_trans->gw_dirs);
4266 TAILQ_INIT(&gw_trans->gw_headers);
4268 gw_trans->page = 0;
4269 gw_trans->repos_total = 0;
4270 gw_trans->repo_path = NULL;
4271 gw_trans->commit = NULL;
4272 error = gw_strdup_string(&gw_trans->headref, GOT_REF_HEAD, NULL);
4273 if (error)
4274 goto done;
4275 gw_trans->mime = KMIME_TEXT_HTML;
4276 gw_trans->gw_tmpl->key = gw_templs;
4277 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4278 gw_trans->gw_tmpl->arg = gw_trans;
4279 gw_trans->gw_tmpl->cb = gw_template;
4280 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
4281 if (error)
4282 goto done;
4284 error = gw_parse_querystring(gw_trans);
4285 if (error)
4286 goto done;
4288 if (gw_trans->action == GW_BLOB)
4289 error = gw_blob(gw_trans);
4290 else
4291 error = gw_display_index(gw_trans);
4292 done:
4293 if (error) {
4294 gw_trans->mime = KMIME_TEXT_PLAIN;
4295 gw_trans->action = GW_ERR;
4296 gw_display_error(gw_trans, error);
4298 if (gw_malloc) {
4299 free(gw_trans->gw_conf->got_repos_path);
4300 free(gw_trans->gw_conf->got_site_name);
4301 free(gw_trans->gw_conf->got_site_owner);
4302 free(gw_trans->gw_conf->got_site_link);
4303 free(gw_trans->gw_conf->got_logo);
4304 free(gw_trans->gw_conf->got_logo_url);
4305 free(gw_trans->gw_conf);
4306 free(gw_trans->commit);
4307 free(gw_trans->repo_path);
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;