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 "buf.h"
50 #include "gotweb.h"
52 #ifndef nitems
53 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
54 #endif
56 struct gw_trans {
57 TAILQ_HEAD(headers, gw_header) gw_headers;
58 TAILQ_HEAD(dirs, gw_dir) gw_dirs;
59 struct gw_dir *gw_dir;
60 struct gotweb_conf *gw_conf;
61 struct ktemplate *gw_tmpl;
62 struct khtmlreq *gw_html_req;
63 struct kreq *gw_req;
64 char *repo_name;
65 char *repo_path;
66 char *commit;
67 char *repo_file;
68 char *repo_folder;
69 char *action_name;
70 char *headref;
71 unsigned int action;
72 unsigned int page;
73 unsigned int repos_total;
74 enum kmime mime;
75 };
77 struct gw_header {
78 TAILQ_ENTRY(gw_header) entry;
79 struct got_repository *repo;
80 struct got_reflist_head refs;
81 struct got_commit_object *commit;
82 struct got_object_id *id;
83 char *path;
85 char *refs_str;
86 char *commit_id; /* id_str1 */
87 char *parent_id; /* id_str2 */
88 char *tree_id;
89 const char *author;
90 const char *committer;
91 char *commit_msg;
92 time_t committer_time;
93 };
95 struct gw_dir {
96 TAILQ_ENTRY(gw_dir) entry;
97 char *name;
98 char *owner;
99 char *description;
100 char *url;
101 char *age;
102 char *path;
103 };
105 enum gw_key {
106 KEY_ACTION,
107 KEY_COMMIT_ID,
108 KEY_FILE,
109 KEY_FOLDER,
110 KEY_HEADREF,
111 KEY_PAGE,
112 KEY_PATH,
113 KEY__ZMAX
114 };
116 enum gw_tmpl {
117 TEMPL_CONTENT,
118 TEMPL_HEAD,
119 TEMPL_HEADER,
120 TEMPL_SEARCH,
121 TEMPL_SITEPATH,
122 TEMPL_SITEOWNER,
123 TEMPL_TITLE,
124 TEMPL__MAX
125 };
127 enum gw_ref_tm {
128 TM_DIFF,
129 TM_LONG,
130 };
132 enum gw_tags {
133 TAGBRIEF,
134 TAGFULL,
135 };
137 static const char *const gw_templs[TEMPL__MAX] = {
138 "content",
139 "head",
140 "header",
141 "search",
142 "sitepath",
143 "siteowner",
144 "title",
145 };
147 static const struct kvalid gw_keys[KEY__ZMAX] = {
148 { kvalid_stringne, "action" },
149 { kvalid_stringne, "commit" },
150 { kvalid_stringne, "file" },
151 { kvalid_stringne, "folder" },
152 { kvalid_stringne, "headref" },
153 { kvalid_int, "page" },
154 { kvalid_stringne, "path" },
155 };
157 static struct gw_dir *gw_init_gw_dir(char *);
158 static struct gw_header *gw_init_header(void);
160 static const struct got_error *gw_get_repo_description(char **,
161 struct gw_trans *, char *);
162 static const struct got_error *gw_get_repo_owner(char **, struct gw_trans *,
163 char *);
164 static const struct got_error *gw_get_time_str(char **, time_t, int);
165 static const struct got_error *gw_get_repo_age(char **, struct gw_trans *,
166 char *, char *, int);
167 static const struct got_error *gw_output_file_blame(struct gw_trans *);
168 static const struct got_error *gw_output_blob_buf(struct gw_trans *);
169 static const struct got_error *gw_output_repo_tree(struct gw_trans *);
170 static const struct got_error *gw_output_diff(struct gw_trans *,
171 struct gw_header *);
172 static const struct got_error *gw_output_repo_tags(struct gw_trans *,
173 struct gw_header *, int, int);
174 static const struct got_error *gw_output_repo_heads(struct gw_trans *);
175 static const struct got_error *gw_output_site_link(struct gw_trans *);
176 static const struct got_error *gw_get_clone_url(char **, struct gw_trans *,
177 char *);
178 static const struct got_error *gw_colordiff_line(struct gw_trans *, char *);
180 static const struct got_error *gw_gen_commit_header(struct gw_trans *, char *,
181 char*);
182 static const struct got_error *gw_gen_diff_header(struct gw_trans *, char *,
183 char*);
184 static const struct got_error *gw_gen_author_header(struct gw_trans *,
185 const char *);
186 static const struct got_error *gw_gen_age_header(struct gw_trans *,
187 const char *);
188 static const struct got_error *gw_gen_committer_header(struct gw_trans *,
189 const char *);
190 static const struct got_error *gw_gen_commit_msg_header(struct gw_trans*,
191 char *);
192 static const struct got_error *gw_gen_tree_header(struct gw_trans *, char *);
194 static void gw_free_headers(struct gw_header *);
195 static const struct got_error* gw_display_open(struct gw_trans *, enum khttp,
196 enum kmime);
197 static const struct got_error* gw_display_index(struct gw_trans *);
198 static void gw_display_error(struct gw_trans *,
199 const struct got_error *);
201 static int gw_template(size_t, void *);
203 static const struct got_error* gw_get_header(struct gw_trans *,
204 struct gw_header *, int);
205 static const struct got_error* gw_get_commits(struct gw_trans *,
206 struct gw_header *, int);
207 static const struct got_error* gw_get_commit(struct gw_trans *,
208 struct gw_header *);
209 static const struct got_error* gw_apply_unveil(const char *, const char *);
210 static const struct got_error* gw_blame_cb(void *, int, int,
211 struct got_object_id *);
212 static const struct got_error* gw_load_got_paths(struct gw_trans *);
213 static const struct got_error* gw_load_got_path(struct gw_trans *,
214 struct gw_dir *);
215 static const struct got_error* gw_parse_querystring(struct gw_trans *);
217 static const struct got_error* gw_blame(struct gw_trans *);
218 static const struct got_error* gw_blob(struct gw_trans *);
219 static const struct got_error* gw_diff(struct gw_trans *);
220 static const struct got_error* gw_index(struct gw_trans *);
221 static const struct got_error* gw_commits(struct gw_trans *);
222 static const struct got_error* gw_briefs(struct gw_trans *);
223 static const struct got_error* gw_summary(struct gw_trans *);
224 static const struct got_error* gw_tree(struct gw_trans *);
225 static const struct got_error* gw_tag(struct gw_trans *);
227 struct gw_query_action {
228 unsigned int func_id;
229 const char *func_name;
230 const struct got_error *(*func_main)(struct gw_trans *);
231 char *template;
232 };
234 enum gw_query_actions {
235 GW_BLAME,
236 GW_BLOB,
237 GW_BRIEFS,
238 GW_COMMITS,
239 GW_DIFF,
240 GW_ERR,
241 GW_INDEX,
242 GW_SUMMARY,
243 GW_TAG,
244 GW_TREE,
245 };
247 static struct gw_query_action gw_query_funcs[] = {
248 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
249 { GW_BLOB, "blob", NULL, NULL },
250 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
251 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
252 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
253 { GW_ERR, NULL, NULL, "gw_tmpl/err.tmpl" },
254 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
255 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
256 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
257 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
258 };
260 static const struct got_error *
261 gw_kcgi_error(enum kcgi_err kerr)
263 if (kerr == KCGI_OK)
264 return NULL;
266 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
267 return got_error(GOT_ERR_CANCELLED);
269 if (kerr == KCGI_ENOMEM)
270 return got_error_set_errno(ENOMEM,
271 kcgi_strerror(kerr));
273 if (kerr == KCGI_ENFILE)
274 return got_error_set_errno(ENFILE,
275 kcgi_strerror(kerr));
277 if (kerr == KCGI_EAGAIN)
278 return got_error_set_errno(EAGAIN,
279 kcgi_strerror(kerr));
281 if (kerr == KCGI_FORM)
282 return got_error_msg(GOT_ERR_IO,
283 kcgi_strerror(kerr));
285 return got_error_from_errno(kcgi_strerror(kerr));
288 static const struct got_error *
289 gw_apply_unveil(const char *repo_path, const char *repo_file)
291 const struct got_error *err;
293 if (repo_path && repo_file) {
294 char *full_path;
295 if (asprintf(&full_path, "%s/%s", repo_path, repo_file) == -1)
296 return got_error_from_errno("asprintf unveil");
297 if (unveil(full_path, "r") != 0)
298 return got_error_from_errno2("unveil", full_path);
301 if (repo_path && unveil(repo_path, "r") != 0)
302 return got_error_from_errno2("unveil", repo_path);
304 if (unveil("/tmp", "rwc") != 0)
305 return got_error_from_errno2("unveil", "/tmp");
307 err = got_privsep_unveil_exec_helpers();
308 if (err != NULL)
309 return err;
311 if (unveil(NULL, NULL) != 0)
312 return got_error_from_errno("unveil");
314 return NULL;
317 static const struct got_error *
318 gw_empty_string(char **s)
320 *s = strdup("");
321 if (*s == NULL)
322 return got_error_from_errno("strdup");
323 return NULL;
326 static int
327 isbinary(const uint8_t *buf, size_t n)
329 size_t i;
331 for (i = 0; i < n; i++)
332 if (buf[i] == 0)
333 return 1;
334 return 0;
337 static const struct got_error *
338 gw_blame(struct gw_trans *gw_trans)
340 const struct got_error *error = NULL;
341 struct gw_header *header = NULL;
342 char *age = NULL;
343 enum kcgi_err kerr;
345 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
346 NULL) == -1)
347 return got_error_from_errno("pledge");
349 if ((header = gw_init_header()) == NULL)
350 return got_error_from_errno("malloc");
352 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
353 if (error)
354 goto done;
356 error = gw_get_header(gw_trans, header, 1);
357 if (error)
358 goto done;
360 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
361 "blame_header_wrapper", KATTR__MAX);
362 if (kerr != KCGI_OK)
363 goto done;
364 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
365 "blame_header", KATTR__MAX);
366 if (kerr != KCGI_OK)
367 goto done;
368 error = gw_get_time_str(&age, header->committer_time,
369 TM_LONG);
370 if (error)
371 goto done;
372 error = gw_gen_age_header(gw_trans, age ?age : "");
373 if (error)
374 goto done;
375 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
376 if (error)
377 goto done;
378 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
379 if (kerr != KCGI_OK)
380 goto done;
381 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
382 "dotted_line", KATTR__MAX);
383 if (kerr != KCGI_OK)
384 goto done;
385 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
386 if (kerr != KCGI_OK)
387 goto done;
389 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
390 "blame", KATTR__MAX);
391 if (kerr != KCGI_OK)
392 goto done;
393 error = gw_output_file_blame(gw_trans);
394 if (error)
395 goto done;
396 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
397 done:
398 got_ref_list_free(&header->refs);
399 gw_free_headers(header);
400 if (error == NULL && kerr != KCGI_OK)
401 error = gw_kcgi_error(kerr);
402 return error;
405 static const struct got_error *
406 gw_blob(struct gw_trans *gw_trans)
408 const struct got_error *error = NULL;
409 struct gw_header *header = NULL;
411 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
412 NULL) == -1)
413 return got_error_from_errno("pledge");
415 if ((header = gw_init_header()) == NULL)
416 return got_error_from_errno("malloc");
418 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
419 if (error)
420 goto done;
422 error = gw_get_header(gw_trans, header, 1);
423 if (error)
424 goto done;
426 error = gw_output_blob_buf(gw_trans);
427 done:
428 got_ref_list_free(&header->refs);
429 gw_free_headers(header);
430 return error;
433 static const struct got_error *
434 gw_diff(struct gw_trans *gw_trans)
436 const struct got_error *error = NULL;
437 struct gw_header *header = NULL;
438 char *age = NULL;
439 enum kcgi_err kerr = KCGI_OK;
441 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
442 NULL) == -1)
443 return got_error_from_errno("pledge");
445 if ((header = gw_init_header()) == NULL)
446 return got_error_from_errno("malloc");
448 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
449 if (error)
450 goto done;
452 error = gw_get_header(gw_trans, header, 1);
453 if (error)
454 goto done;
456 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
457 "diff_header_wrapper", KATTR__MAX);
458 if (kerr != KCGI_OK)
459 goto done;
460 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
461 "diff_header", KATTR__MAX);
462 if (kerr != KCGI_OK)
463 goto done;
464 error = gw_gen_diff_header(gw_trans, header->parent_id,
465 header->commit_id);
466 if (error)
467 goto done;
468 error = gw_gen_commit_header(gw_trans, header->commit_id,
469 header->refs_str);
470 if (error)
471 goto done;
472 error = gw_gen_tree_header(gw_trans, header->tree_id);
473 if (error)
474 goto done;
475 error = gw_gen_author_header(gw_trans, header->author);
476 if (error)
477 goto done;
478 error = gw_gen_committer_header(gw_trans, header->author);
479 if (error)
480 goto done;
481 error = gw_get_time_str(&age, header->committer_time,
482 TM_LONG);
483 if (error)
484 goto done;
485 error = gw_gen_age_header(gw_trans, age ?age : "");
486 if (error)
487 goto done;
488 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
489 if (error)
490 goto done;
491 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
492 if (kerr != KCGI_OK)
493 goto done;
494 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
495 "dotted_line", KATTR__MAX);
496 if (kerr != KCGI_OK)
497 goto done;
498 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
499 if (kerr != KCGI_OK)
500 goto done;
502 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
503 "diff", KATTR__MAX);
504 if (kerr != KCGI_OK)
505 goto done;
506 error = gw_output_diff(gw_trans, header);
507 if (error)
508 goto done;
510 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
511 if (kerr != KCGI_OK)
512 goto done;
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, NULL);
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 return gw_kcgi_error(kerr);
647 if (asprintf(&href_summary, "?path=%s&action=summary",
648 gw_dir->name) == -1) {
649 error = got_error_from_errno("asprintf");
650 return error;
652 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
653 "index_project", KATTR__MAX);
654 if (kerr != KCGI_OK) {
655 error = gw_kcgi_error(kerr);
656 goto done;
658 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
659 href_summary, KATTR__MAX);
660 if (kerr != KCGI_OK) {
661 error = gw_kcgi_error(kerr);
662 goto done;
664 kerr = khtml_puts(gw_trans->gw_html_req, gw_dir->name);
665 if (kerr != KCGI_OK) {
666 error = gw_kcgi_error(kerr);
667 goto done;
669 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
670 if (kerr != KCGI_OK) {
671 error = gw_kcgi_error(kerr);
672 goto done;
674 if (gw_trans->gw_conf->got_show_repo_description) {
675 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
676 KATTR_ID, "index_project_description", KATTR__MAX);
677 if (kerr != KCGI_OK) {
678 error = gw_kcgi_error(kerr);
679 goto done;
681 kerr = khtml_puts(gw_trans->gw_html_req,
682 gw_dir->description ? gw_dir->description : "");
683 if (kerr != KCGI_OK) {
684 error = gw_kcgi_error(kerr);
685 goto done;
687 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
688 if (kerr != KCGI_OK) {
689 error = gw_kcgi_error(kerr);
690 goto done;
693 if (gw_trans->gw_conf->got_show_repo_owner) {
694 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
695 KATTR_ID, "index_project_owner", KATTR__MAX);
696 if (kerr != KCGI_OK) {
697 error = gw_kcgi_error(kerr);
698 goto done;
700 kerr = khtml_puts(gw_trans->gw_html_req,
701 gw_dir->owner ? gw_dir->owner : "");
702 if (kerr != KCGI_OK) {
703 error = gw_kcgi_error(kerr);
704 goto done;
706 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
707 if (kerr != KCGI_OK) {
708 error = gw_kcgi_error(kerr);
709 goto done;
712 if (gw_trans->gw_conf->got_show_repo_age) {
713 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
714 KATTR_ID, "index_project_age", KATTR__MAX);
715 if (kerr != KCGI_OK) {
716 error = gw_kcgi_error(kerr);
717 goto done;
719 kerr = khtml_puts(gw_trans->gw_html_req,
720 gw_dir->age ? gw_dir->age : "");
721 if (kerr != KCGI_OK) {
722 error = gw_kcgi_error(kerr);
723 goto done;
725 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
726 if (kerr != KCGI_OK) {
727 error = gw_kcgi_error(kerr);
728 goto done;
732 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
733 "navs_wrapper", KATTR__MAX);
734 if (kerr != KCGI_OK) {
735 error = gw_kcgi_error(kerr);
736 goto done;
738 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
739 "navs", KATTR__MAX);
740 if (kerr != KCGI_OK) {
741 error = gw_kcgi_error(kerr);
742 goto done;
745 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
746 href_summary, KATTR__MAX);
747 if (kerr != KCGI_OK) {
748 error = gw_kcgi_error(kerr);
749 goto done;
751 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
752 if (kerr != KCGI_OK) {
753 error = gw_kcgi_error(kerr);
754 goto done;
756 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
757 if (kerr != KCGI_OK) {
758 error = gw_kcgi_error(kerr);
759 goto done;
762 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
763 if (kerr != KCGI_OK) {
764 error = gw_kcgi_error(kerr);
765 goto done;
768 if (asprintf(&href_briefs, "?path=%s&action=briefs",
769 gw_dir->name) == -1) {
770 error = got_error_from_errno("asprintf");
771 goto done;
773 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
774 href_briefs, KATTR__MAX);
775 if (kerr != KCGI_OK) {
776 error = gw_kcgi_error(kerr);
777 goto done;
779 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
780 if (kerr != KCGI_OK) {
781 error = gw_kcgi_error(kerr);
782 goto done;
784 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
785 if (kerr != KCGI_OK) {
786 error = gw_kcgi_error(kerr);
787 goto done;
790 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
791 if (kerr != KCGI_OK) {
792 error = gw_kcgi_error(kerr);
793 goto done;
796 if (asprintf(&href_commits, "?path=%s&action=commits",
797 gw_dir->name) == -1) {
798 error = got_error_from_errno("asprintf");
799 goto done;
801 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
802 href_commits, KATTR__MAX);
803 if (kerr != KCGI_OK) {
804 error = gw_kcgi_error(kerr);
805 goto done;
807 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
808 if (kerr != KCGI_OK) {
809 error = gw_kcgi_error(kerr);
810 goto done;
812 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
813 if (kerr != KCGI_OK) {
814 error = gw_kcgi_error(kerr);
815 goto done;
818 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
819 if (kerr != KCGI_OK) {
820 error = gw_kcgi_error(kerr);
821 goto done;
824 if (asprintf(&href_tree, "?path=%s&action=tree",
825 gw_dir->name) == -1) {
826 error = got_error_from_errno("asprintf");
827 goto done;
829 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
830 href_tree, KATTR__MAX);
831 if (kerr != KCGI_OK) {
832 error = gw_kcgi_error(kerr);
833 goto done;
835 kerr = khtml_puts(gw_trans->gw_html_req, "tree");
836 if (kerr != KCGI_OK) {
837 error = gw_kcgi_error(kerr);
838 goto done;
841 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
842 if (kerr != KCGI_OK) {
843 error = gw_kcgi_error(kerr);
844 goto done;
846 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
847 "dotted_line", KATTR__MAX);
848 if (kerr != KCGI_OK) {
849 error = gw_kcgi_error(kerr);
850 goto done;
852 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
853 if (kerr != KCGI_OK) {
854 error = gw_kcgi_error(kerr);
855 goto done;
858 free(href_summary);
859 href_summary = NULL;
860 free(href_briefs);
861 href_briefs = NULL;
862 free(href_commits);
863 href_commits = NULL;
864 free(href_tree);
865 href_tree = NULL;
867 if (gw_trans->gw_conf->got_max_repos_display == 0)
868 continue;
870 if (next_disp == gw_trans->gw_conf->got_max_repos_display) {
871 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
872 KATTR_ID, "np_wrapper", KATTR__MAX);
873 if (kerr != KCGI_OK) {
874 error = gw_kcgi_error(kerr);
875 goto done;
877 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
878 KATTR_ID, "nav_prev", KATTR__MAX);
879 if (kerr != KCGI_OK) {
880 error = gw_kcgi_error(kerr);
881 goto done;
883 } else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
884 (gw_trans->page > 0) &&
885 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
886 prev_disp == gw_trans->repos_total)) {
887 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
888 KATTR_ID, "np_wrapper", KATTR__MAX);
889 if (kerr != KCGI_OK) {
890 error = gw_kcgi_error(kerr);
891 goto done;
893 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
894 KATTR_ID, "nav_prev", KATTR__MAX);
895 if (kerr != KCGI_OK) {
896 error = gw_kcgi_error(kerr);
897 goto done;
901 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
902 (gw_trans->page > 0) &&
903 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
904 prev_disp == gw_trans->repos_total)) {
905 if (asprintf(&href_prev, "?page=%d",
906 gw_trans->page - 1) == -1) {
907 error = got_error_from_errno("asprintf");
908 goto done;
910 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
911 KATTR_HREF, href_prev, KATTR__MAX);
912 free(href_prev);
913 if (kerr != KCGI_OK) {
914 error = gw_kcgi_error(kerr);
915 goto done;
917 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
918 if (kerr != KCGI_OK) {
919 error = gw_kcgi_error(kerr);
920 goto done;
922 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
923 if (kerr != KCGI_OK) {
924 error = gw_kcgi_error(kerr);
925 goto done;
929 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
930 if (kerr != KCGI_OK)
931 return gw_kcgi_error(kerr);
933 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
934 next_disp == gw_trans->gw_conf->got_max_repos_display &&
935 dir_c != (gw_trans->page + 1) *
936 gw_trans->gw_conf->got_max_repos_display) {
937 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
938 KATTR_ID, "nav_next", KATTR__MAX);
939 if (kerr != KCGI_OK) {
940 error = gw_kcgi_error(kerr);
941 goto done;
943 if (asprintf(&href_next, "?page=%d",
944 gw_trans->page + 1) == -1) {
945 error = got_error_from_errno("calloc");
946 goto done;
948 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
949 KATTR_HREF, href_next, KATTR__MAX);
950 free(href_next);
951 if (kerr != KCGI_OK) {
952 error = gw_kcgi_error(kerr);
953 goto done;
955 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
956 if (kerr != KCGI_OK) {
957 error = gw_kcgi_error(kerr);
958 goto done;
960 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
961 if (kerr != KCGI_OK) {
962 error = gw_kcgi_error(kerr);
963 goto done;
965 next_disp = 0;
966 break;
969 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
970 (gw_trans->page > 0) &&
971 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
972 prev_disp == gw_trans->repos_total)) {
973 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
974 if (kerr != KCGI_OK) {
975 error = gw_kcgi_error(kerr);
976 goto done;
979 next_disp++;
981 done:
982 free(href_summary);
983 free(href_briefs);
984 free(href_commits);
985 free(href_tree);
986 return error;
989 static const struct got_error *
990 gw_commits(struct gw_trans *gw_trans)
992 const struct got_error *error = NULL;
993 struct gw_header *header = NULL, *n_header = NULL;
994 char *age = NULL;
995 char *href_diff = NULL, *href_blob = NULL;
996 enum kcgi_err kerr = KCGI_OK;
998 if ((header = gw_init_header()) == NULL)
999 return got_error_from_errno("malloc");
1001 if (pledge("stdio rpath proc exec sendfd unveil",
1002 NULL) == -1) {
1003 error = got_error_from_errno("pledge");
1004 goto done;
1007 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
1008 if (error)
1009 goto done;
1011 error = gw_get_header(gw_trans, header,
1012 gw_trans->gw_conf->got_max_commits_display);
1013 if (error)
1014 goto done;
1016 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
1017 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1018 "commits_line_wrapper", KATTR__MAX);
1019 if (kerr != KCGI_OK)
1020 goto done;
1021 error = gw_gen_commit_header(gw_trans, n_header->commit_id,
1022 n_header->refs_str);
1023 if (error)
1024 goto done;
1025 error = gw_gen_author_header(gw_trans, n_header->author);
1026 if (error)
1027 goto done;
1028 error = gw_gen_committer_header(gw_trans, n_header->author);
1029 if (error)
1030 goto done;
1031 error = gw_get_time_str(&age, n_header->committer_time,
1032 TM_LONG);
1033 if (error)
1034 goto done;
1035 error = gw_gen_age_header(gw_trans, age ?age : "");
1036 if (error)
1037 goto done;
1038 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1039 if (kerr != KCGI_OK)
1040 goto done;
1042 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1043 "dotted_line", KATTR__MAX);
1044 if (kerr != KCGI_OK)
1045 goto done;
1046 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1047 if (kerr != KCGI_OK)
1048 goto done;
1050 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1051 "commit", KATTR__MAX);
1052 if (kerr != KCGI_OK)
1053 goto done;
1054 kerr = khttp_puts(gw_trans->gw_req, n_header->commit_msg);
1055 if (kerr != KCGI_OK)
1056 goto done;
1057 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1058 if (kerr != KCGI_OK)
1059 goto done;
1061 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
1062 gw_trans->repo_name, n_header->commit_id) == -1) {
1063 error = got_error_from_errno("asprintf");
1064 goto done;
1066 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1067 KATTR_ID, "navs_wrapper", KATTR__MAX);
1068 if (kerr != KCGI_OK)
1069 goto done;
1070 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1071 KATTR_ID, "navs", KATTR__MAX);
1072 if (kerr != KCGI_OK)
1073 goto done;
1074 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1075 KATTR_HREF, href_diff, KATTR__MAX);
1076 if (kerr != KCGI_OK)
1077 goto done;
1078 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1079 if (kerr != KCGI_OK)
1080 goto done;
1081 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1082 if (kerr != KCGI_OK)
1083 goto done;
1085 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1086 if (kerr != KCGI_OK)
1087 goto done;
1089 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
1090 gw_trans->repo_name, n_header->commit_id) == -1) {
1091 error = got_error_from_errno("asprintf");
1092 goto done;
1094 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1095 KATTR_HREF, href_blob, KATTR__MAX);
1096 if (kerr != KCGI_OK)
1097 goto done;
1098 khtml_puts(gw_trans->gw_html_req, "tree");
1099 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1100 if (kerr != KCGI_OK)
1101 goto done;
1102 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1103 if (kerr != KCGI_OK)
1104 goto done;
1106 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1107 "solid_line", KATTR__MAX);
1108 if (kerr != KCGI_OK)
1109 goto done;
1110 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1111 if (kerr != KCGI_OK)
1112 goto done;
1114 free(age);
1115 age = NULL;
1117 done:
1118 got_ref_list_free(&header->refs);
1119 gw_free_headers(header);
1120 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1121 gw_free_headers(n_header);
1122 free(age);
1123 free(href_diff);
1124 free(href_blob);
1125 if (error == NULL && kerr != KCGI_OK)
1126 error = gw_kcgi_error(kerr);
1127 return error;
1130 static const struct got_error *
1131 gw_briefs(struct gw_trans *gw_trans)
1133 const struct got_error *error = NULL;
1134 struct gw_header *header = NULL, *n_header = NULL;
1135 char *age = NULL, *age_html = NULL;
1136 char *href_diff = NULL, *href_blob = NULL;
1137 char *newline, *smallerthan;
1138 enum kcgi_err kerr = KCGI_OK;
1140 if ((header = gw_init_header()) == NULL)
1141 return got_error_from_errno("malloc");
1143 if (pledge("stdio rpath proc exec sendfd unveil",
1144 NULL) == -1) {
1145 error = got_error_from_errno("pledge");
1146 goto done;
1149 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
1150 if (error)
1151 goto done;
1153 if (gw_trans->action == GW_SUMMARY)
1154 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
1155 else
1156 error = gw_get_header(gw_trans, header,
1157 gw_trans->gw_conf->got_max_commits_display);
1158 if (error)
1159 goto done;
1161 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
1162 error = gw_get_time_str(&age, n_header->committer_time,
1163 TM_DIFF);
1164 if (error)
1165 goto done;
1167 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1168 KATTR_ID, "briefs_wrapper", KATTR__MAX);
1169 if (kerr != KCGI_OK)
1170 goto done;
1172 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1173 KATTR_ID, "briefs_age", KATTR__MAX);
1174 if (kerr != KCGI_OK)
1175 goto done;
1176 if (asprintf(&age_html, "%s", age ? age : "") == -1) {
1177 error = got_error_from_errno("asprintf");
1178 goto done;
1180 kerr = khtml_puts(gw_trans->gw_html_req, age_html);
1181 if (kerr != KCGI_OK)
1182 goto done;
1183 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1184 if (kerr != KCGI_OK)
1185 goto done;
1187 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1188 KATTR_ID, "briefs_author", KATTR__MAX);
1189 if (kerr != KCGI_OK)
1190 goto done;
1191 smallerthan = strchr(n_header->author, '<');
1192 if (smallerthan)
1193 *smallerthan = '\0';
1194 kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
1195 if (kerr != KCGI_OK)
1196 goto done;
1197 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1198 if (kerr != KCGI_OK)
1199 goto done;
1201 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
1202 gw_trans->repo_name, n_header->commit_id) == -1) {
1203 error = got_error_from_errno("asprintf");
1204 goto done;
1206 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1207 KATTR_ID, "briefs_log", KATTR__MAX);
1208 if (kerr != KCGI_OK)
1209 goto done;
1210 newline = strchr(n_header->commit_msg, '\n');
1211 if (newline)
1212 *newline = '\0';
1213 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1214 KATTR_HREF, href_diff, KATTR__MAX);
1215 if (kerr != KCGI_OK)
1216 goto done;
1217 kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
1218 if (kerr != KCGI_OK)
1219 goto done;
1220 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1221 if (kerr != KCGI_OK)
1222 goto done;
1224 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1225 KATTR_ID, "navs_wrapper", KATTR__MAX);
1226 if (kerr != KCGI_OK)
1227 goto done;
1228 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1229 KATTR_ID, "navs", KATTR__MAX);
1230 if (kerr != KCGI_OK)
1231 goto done;
1232 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1233 KATTR_HREF, href_diff, KATTR__MAX);
1234 if (kerr != KCGI_OK)
1235 goto done;
1236 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1237 if (kerr != KCGI_OK)
1238 goto done;
1239 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1240 if (kerr != KCGI_OK)
1241 goto done;
1243 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1244 if (kerr != KCGI_OK)
1245 goto done;
1247 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
1248 gw_trans->repo_name, n_header->commit_id) == -1) {
1249 error = got_error_from_errno("asprintf");
1250 goto done;
1252 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1253 KATTR_HREF, href_blob, KATTR__MAX);
1254 if (kerr != KCGI_OK)
1255 goto done;
1256 khtml_puts(gw_trans->gw_html_req, "tree");
1257 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1258 if (kerr != KCGI_OK)
1259 goto done;
1260 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1261 if (kerr != KCGI_OK)
1262 goto done;
1264 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1265 KATTR_ID, "dotted_line", KATTR__MAX);
1266 if (kerr != KCGI_OK)
1267 goto done;
1268 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1269 if (kerr != KCGI_OK)
1270 goto done;
1272 free(age);
1273 age = NULL;
1274 free(age_html);
1275 age_html = NULL;
1276 free(href_diff);
1277 href_diff = NULL;
1278 free(href_blob);
1279 href_blob = NULL;
1281 done:
1282 got_ref_list_free(&header->refs);
1283 gw_free_headers(header);
1284 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1285 gw_free_headers(n_header);
1286 free(age);
1287 free(age_html);
1288 free(href_diff);
1289 free(href_blob);
1290 if (error == NULL && kerr != KCGI_OK)
1291 error = gw_kcgi_error(kerr);
1292 return error;
1295 static const struct got_error *
1296 gw_summary(struct gw_trans *gw_trans)
1298 const struct got_error *error = NULL;
1299 char *age = NULL;
1300 enum kcgi_err kerr = KCGI_OK;
1302 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1303 return got_error_from_errno("pledge");
1305 /* unveil is applied with gw_briefs below */
1307 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1308 "summary_wrapper", KATTR__MAX);
1309 if (kerr != KCGI_OK)
1310 return gw_kcgi_error(kerr);
1312 if (gw_trans->gw_conf->got_show_repo_description &&
1313 gw_trans->gw_dir->description != NULL &&
1314 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1315 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1316 KATTR_ID, "description_title", KATTR__MAX);
1317 if (kerr != KCGI_OK)
1318 goto done;
1319 kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1320 if (kerr != KCGI_OK)
1321 goto done;
1322 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1323 if (kerr != KCGI_OK)
1324 goto done;
1325 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1326 KATTR_ID, "description", KATTR__MAX);
1327 if (kerr != KCGI_OK)
1328 goto done;
1329 kerr = khtml_puts(gw_trans->gw_html_req,
1330 gw_trans->gw_dir->description);
1331 if (kerr != KCGI_OK)
1332 goto done;
1333 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1334 if (kerr != KCGI_OK)
1335 goto done;
1338 if (gw_trans->gw_conf->got_show_repo_owner &&
1339 gw_trans->gw_dir->owner != NULL) {
1340 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1341 KATTR_ID, "repo_owner_title", KATTR__MAX);
1342 if (kerr != KCGI_OK)
1343 goto done;
1344 kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1345 if (kerr != KCGI_OK)
1346 goto done;
1347 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1348 if (kerr != KCGI_OK)
1349 goto done;
1350 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1351 KATTR_ID, "repo_owner", KATTR__MAX);
1352 if (kerr != KCGI_OK)
1353 goto done;
1354 kerr = khtml_puts(gw_trans->gw_html_req,
1355 gw_trans->gw_dir->owner);
1356 if (kerr != KCGI_OK)
1357 goto done;
1358 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1359 if (kerr != KCGI_OK)
1360 goto done;
1363 if (gw_trans->gw_conf->got_show_repo_age) {
1364 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1365 "refs/heads", TM_LONG);
1366 if (error)
1367 goto done;
1368 if (age != NULL) {
1369 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1370 KATTR_ID, "last_change_title", KATTR__MAX);
1371 if (kerr != KCGI_OK)
1372 goto done;
1373 kerr = khtml_puts(gw_trans->gw_html_req,
1374 "Last Change: ");
1375 if (kerr != KCGI_OK)
1376 goto done;
1377 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1378 if (kerr != KCGI_OK)
1379 goto done;
1380 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1381 KATTR_ID, "last_change", KATTR__MAX);
1382 if (kerr != KCGI_OK)
1383 goto done;
1384 kerr = khtml_puts(gw_trans->gw_html_req, age);
1385 if (kerr != KCGI_OK)
1386 goto done;
1387 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1388 if (kerr != KCGI_OK)
1389 goto done;
1393 if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1394 gw_trans->gw_dir->url != NULL &&
1395 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1396 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1397 KATTR_ID, "cloneurl_title", KATTR__MAX);
1398 if (kerr != KCGI_OK)
1399 goto done;
1400 kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1401 if (kerr != KCGI_OK)
1402 goto done;
1403 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1404 if (kerr != KCGI_OK)
1405 goto done;
1406 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1407 KATTR_ID, "cloneurl", KATTR__MAX);
1408 if (kerr != KCGI_OK)
1409 goto done;
1410 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1411 if (kerr != KCGI_OK)
1412 goto done;
1413 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1414 if (kerr != KCGI_OK)
1415 goto done;
1418 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1419 if (kerr != KCGI_OK)
1420 goto done;
1422 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1423 "briefs_title_wrapper", KATTR__MAX);
1424 if (kerr != KCGI_OK)
1425 goto done;
1426 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1427 "briefs_title", KATTR__MAX);
1428 if (kerr != KCGI_OK)
1429 goto done;
1430 kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1431 if (kerr != KCGI_OK)
1432 goto done;
1433 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1434 if (kerr != KCGI_OK)
1435 goto done;
1436 error = gw_briefs(gw_trans);
1437 if (error)
1438 goto done;
1440 error = gw_output_repo_tags(gw_trans, NULL, D_MAXSLCOMMDISP,
1441 TAGBRIEF);
1442 if (error)
1443 goto done;
1445 error = gw_output_repo_heads(gw_trans);
1446 done:
1447 free(age);
1448 if (error == NULL && kerr != KCGI_OK)
1449 error = gw_kcgi_error(kerr);
1450 return error;
1453 static const struct got_error *
1454 gw_tree(struct gw_trans *gw_trans)
1456 const struct got_error *error = NULL;
1457 struct gw_header *header = NULL;
1458 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1459 char *age = NULL, *age_html = NULL;
1460 enum kcgi_err kerr;
1462 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1463 return got_error_from_errno("pledge");
1465 if ((header = gw_init_header()) == NULL)
1466 return got_error_from_errno("malloc");
1468 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
1469 if (error)
1470 goto done;
1472 error = gw_get_header(gw_trans, header, 1);
1473 if (error)
1474 goto done;
1476 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1477 "tree_header_wrapper", KATTR__MAX);
1478 if (kerr != KCGI_OK)
1479 goto done;
1480 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1481 "tree_header", KATTR__MAX);
1482 if (kerr != KCGI_OK)
1483 goto done;
1484 error = gw_gen_tree_header(gw_trans, header->tree_id);
1485 if (error)
1486 goto done;
1487 error = gw_get_time_str(&age, header->committer_time,
1488 TM_LONG);
1489 if (error)
1490 goto done;
1491 error = gw_gen_age_header(gw_trans, age ?age : "");
1492 if (error)
1493 goto done;
1494 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1495 if (error)
1496 goto done;
1497 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1498 if (kerr != KCGI_OK)
1499 goto done;
1500 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1501 "dotted_line", KATTR__MAX);
1502 if (kerr != KCGI_OK)
1503 goto done;
1504 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1505 if (kerr != KCGI_OK)
1506 goto done;
1508 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1509 "tree", KATTR__MAX);
1510 if (kerr != KCGI_OK)
1511 goto done;
1512 error = gw_output_repo_tree(gw_trans);
1513 if (error)
1514 goto done;
1516 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1517 done:
1518 got_ref_list_free(&header->refs);
1519 gw_free_headers(header);
1520 free(tree_html_disp);
1521 free(tree_html);
1522 free(tree);
1523 free(age);
1524 free(age_html);
1525 if (error == NULL && kerr != KCGI_OK)
1526 error = gw_kcgi_error(kerr);
1527 return error;
1530 static const struct got_error *
1531 gw_tag(struct gw_trans *gw_trans)
1533 const struct got_error *error = NULL;
1534 struct gw_header *header = NULL;
1535 enum kcgi_err kerr;
1537 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1538 return got_error_from_errno("pledge");
1540 if ((header = gw_init_header()) == NULL)
1541 return got_error_from_errno("malloc");
1543 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
1544 if (error)
1545 goto done;
1547 error = gw_get_header(gw_trans, header, 1);
1548 if (error)
1549 goto done;
1551 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1552 "tag_header_wrapper", KATTR__MAX);
1553 if (kerr != KCGI_OK)
1554 goto done;
1555 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1556 "tag_header", KATTR__MAX);
1557 if (kerr != KCGI_OK)
1558 goto done;
1559 error = gw_gen_commit_header(gw_trans, header->commit_id,
1560 header->refs_str);
1561 if (error)
1562 goto done;
1563 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1564 if (error)
1565 goto done;
1566 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1567 if (kerr != KCGI_OK)
1568 goto done;
1569 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1570 "dotted_line", KATTR__MAX);
1571 if (kerr != KCGI_OK)
1572 goto done;
1573 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1574 if (kerr != KCGI_OK)
1575 goto done;
1577 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1578 "tree", KATTR__MAX);
1579 if (kerr != KCGI_OK)
1580 goto done;
1582 error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1583 if (error)
1584 goto done;
1586 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1587 done:
1588 got_ref_list_free(&header->refs);
1589 gw_free_headers(header);
1590 if (error == NULL && kerr != KCGI_OK)
1591 error = gw_kcgi_error(kerr);
1592 return error;
1595 static const struct got_error *
1596 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1598 const struct got_error *error = NULL;
1599 DIR *dt;
1600 char *dir_test;
1601 int opened = 0;
1603 if (asprintf(&dir_test, "%s/%s/%s",
1604 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1605 GOTWEB_GIT_DIR) == -1)
1606 return got_error_from_errno("asprintf");
1608 dt = opendir(dir_test);
1609 if (dt == NULL) {
1610 free(dir_test);
1611 } else {
1612 gw_dir->path = strdup(dir_test);
1613 opened = 1;
1614 goto done;
1617 if (asprintf(&dir_test, "%s/%s/%s",
1618 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1619 GOTWEB_GOT_DIR) == -1)
1620 return got_error_from_errno("asprintf");
1622 dt = opendir(dir_test);
1623 if (dt == NULL)
1624 free(dir_test);
1625 else {
1626 opened = 1;
1627 error = got_error(GOT_ERR_NOT_GIT_REPO);
1628 goto errored;
1631 if (asprintf(&dir_test, "%s/%s",
1632 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1)
1633 return got_error_from_errno("asprintf");
1635 gw_dir->path = strdup(dir_test);
1636 done:
1637 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1638 gw_dir->path);
1639 if (error)
1640 goto errored;
1641 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1642 if (error)
1643 goto errored;
1644 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1645 "refs/heads", TM_DIFF);
1646 if (error)
1647 goto errored;
1648 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1649 errored:
1650 free(dir_test);
1651 if (opened)
1652 closedir(dt);
1653 return error;
1656 static const struct got_error *
1657 gw_load_got_paths(struct gw_trans *gw_trans)
1659 const struct got_error *error = NULL;
1660 DIR *d;
1661 struct dirent **sd_dent;
1662 struct gw_dir *gw_dir;
1663 struct stat st;
1664 unsigned int d_cnt, d_i;
1666 d = opendir(gw_trans->gw_conf->got_repos_path);
1667 if (d == NULL) {
1668 error = got_error_from_errno2("opendir",
1669 gw_trans->gw_conf->got_repos_path);
1670 return error;
1673 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1674 alphasort);
1675 if (d_cnt == -1) {
1676 error = got_error_from_errno2("scandir",
1677 gw_trans->gw_conf->got_repos_path);
1678 return error;
1681 for (d_i = 0; d_i < d_cnt; d_i++) {
1682 if (gw_trans->gw_conf->got_max_repos > 0 &&
1683 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1684 break; /* account for parent and self */
1686 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1687 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1688 continue;
1690 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
1691 return got_error_from_errno("gw_dir malloc");
1693 error = gw_load_got_path(gw_trans, gw_dir);
1694 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1695 error = NULL;
1696 continue;
1698 else if (error)
1699 return error;
1701 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1702 !got_path_dir_is_empty(gw_dir->path)) {
1703 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1704 entry);
1705 gw_trans->repos_total++;
1709 closedir(d);
1710 return error;
1713 static const struct got_error *
1714 gw_parse_querystring(struct gw_trans *gw_trans)
1716 const struct got_error *error = NULL;
1717 struct kpair *p;
1718 struct gw_query_action *action = NULL;
1719 unsigned int i;
1721 if (gw_trans->gw_req->fieldnmap[0]) {
1722 error = got_error_from_errno("bad parse");
1723 return error;
1724 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1725 /* define gw_trans->repo_path */
1726 if (asprintf(&gw_trans->repo_name, "%s", p->parsed.s) == -1)
1727 return got_error_from_errno("asprintf");
1729 if (asprintf(&gw_trans->repo_path, "%s/%s",
1730 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
1731 return got_error_from_errno("asprintf");
1733 /* get action and set function */
1734 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
1735 for (i = 0; i < nitems(gw_query_funcs); i++) {
1736 action = &gw_query_funcs[i];
1737 if (action->func_name == NULL)
1738 continue;
1740 if (strcmp(action->func_name,
1741 p->parsed.s) == 0) {
1742 gw_trans->action = i;
1743 if (asprintf(&gw_trans->action_name,
1744 "%s", action->func_name) == -1)
1745 return
1746 got_error_from_errno(
1747 "asprintf");
1749 break;
1752 action = NULL;
1755 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
1756 if (asprintf(&gw_trans->commit, "%s",
1757 p->parsed.s) == -1)
1758 return got_error_from_errno("asprintf");
1760 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1761 if (asprintf(&gw_trans->repo_file, "%s",
1762 p->parsed.s) == -1)
1763 return got_error_from_errno("asprintf");
1765 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER]))
1766 if (asprintf(&gw_trans->repo_folder, "%s",
1767 p->parsed.s) == -1)
1768 return got_error_from_errno("asprintf");
1770 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
1771 if (asprintf(&gw_trans->headref, "%s",
1772 p->parsed.s) == -1)
1773 return got_error_from_errno("asprintf");
1775 if (action == NULL) {
1776 error = got_error_from_errno("invalid action");
1777 return error;
1779 if ((gw_trans->gw_dir =
1780 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
1781 return got_error_from_errno("gw_dir malloc");
1783 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1784 if (error)
1785 return error;
1786 } else
1787 gw_trans->action = GW_INDEX;
1789 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1790 gw_trans->page = p->parsed.i;
1792 return error;
1795 static struct gw_dir *
1796 gw_init_gw_dir(char *dir)
1798 struct gw_dir *gw_dir;
1800 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
1801 return NULL;
1803 if (asprintf(&gw_dir->name, "%s", dir) == -1)
1804 return NULL;
1806 return gw_dir;
1809 static const struct got_error *
1810 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1812 enum kcgi_err kerr;
1814 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1815 if (kerr != KCGI_OK)
1816 return gw_kcgi_error(kerr);
1817 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1818 khttps[code]);
1819 if (kerr != KCGI_OK)
1820 return gw_kcgi_error(kerr);
1821 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1822 kmimetypes[mime]);
1823 if (kerr != KCGI_OK)
1824 return gw_kcgi_error(kerr);
1825 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
1826 "nosniff");
1827 if (kerr != KCGI_OK)
1828 return gw_kcgi_error(kerr);
1829 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1830 if (kerr != KCGI_OK)
1831 return gw_kcgi_error(kerr);
1832 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
1833 "1; mode=block");
1834 if (kerr != KCGI_OK)
1835 return gw_kcgi_error(kerr);
1837 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
1838 kerr = khttp_head(gw_trans->gw_req,
1839 kresps[KRESP_CONTENT_DISPOSITION],
1840 "attachment; filename=%s", gw_trans->repo_file);
1841 if (kerr != KCGI_OK)
1842 return gw_kcgi_error(kerr);
1845 kerr = khttp_body(gw_trans->gw_req);
1846 return gw_kcgi_error(kerr);
1849 static const struct got_error *
1850 gw_display_index(struct gw_trans *gw_trans)
1852 const struct got_error *error;
1853 enum kcgi_err kerr;
1855 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1856 if (error)
1857 return error;
1859 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1860 if (kerr != KCGI_OK)
1861 return gw_kcgi_error(kerr);
1863 if (gw_trans->action != GW_BLOB) {
1864 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1865 gw_query_funcs[gw_trans->action].template);
1866 if (kerr != KCGI_OK) {
1867 khtml_close(gw_trans->gw_html_req);
1868 return gw_kcgi_error(kerr);
1872 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
1875 static void
1876 gw_display_error(struct gw_trans *gw_trans, const struct got_error *err)
1878 if (gw_display_open(gw_trans, KHTTP_200, gw_trans->mime) != NULL)
1879 return;
1881 if (khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0) != KCGI_OK)
1882 return;
1883 khtml_puts(gw_trans->gw_html_req, err->msg);
1884 khtml_close(gw_trans->gw_html_req);
1887 static int
1888 gw_template(size_t key, void *arg)
1890 const struct got_error *error = NULL;
1891 enum kcgi_err kerr;
1892 struct gw_trans *gw_trans = arg;
1893 char *img_src = NULL;
1895 switch (key) {
1896 case (TEMPL_HEAD):
1897 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1898 KATTR_CONTENT, "initial-scale=1.0, user-scalable=no",
1899 KATTR__MAX);
1900 if (kerr != KCGI_OK)
1901 return 0;
1902 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1903 if (kerr != KCGI_OK)
1904 return 0;
1905 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1906 KATTR_CHARSET, "utf-8",
1907 KATTR__MAX);
1908 if (kerr != KCGI_OK)
1909 return 0;
1910 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1911 if (kerr != KCGI_OK)
1912 return 0;
1913 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1914 KATTR_NAME, "msapplication-TileColor",
1915 KATTR_CONTENT, "#da532c", KATTR__MAX);
1916 if (kerr != KCGI_OK)
1917 return 0;
1918 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1919 if (kerr != KCGI_OK)
1920 return 0;
1921 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1922 KATTR_NAME, "theme-color",
1923 KATTR_CONTENT, "#ffffff", KATTR__MAX);
1924 if (kerr != KCGI_OK)
1925 return 0;
1926 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1927 if (kerr != KCGI_OK)
1928 return 0;
1929 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1930 KATTR_REL, "apple-touch-icon", KATTR_SIZES, "180x180",
1931 KATTR_HREF, "/apple-touch-icon.png", KATTR__MAX);
1932 if (kerr != KCGI_OK)
1933 return 0;
1934 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1935 if (kerr != KCGI_OK)
1936 return 0;
1937 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1938 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
1939 "32x32", KATTR_HREF, "/favicon-32x32.png", KATTR__MAX);
1940 if (kerr != KCGI_OK)
1941 return 0;
1942 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1943 if (kerr != KCGI_OK)
1944 return 0;
1945 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1946 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
1947 "16x16", KATTR_HREF, "/favicon-16x16.png", KATTR__MAX);
1948 if (kerr != KCGI_OK)
1949 return 0;
1950 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1951 if (kerr != KCGI_OK)
1952 return 0;
1953 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1954 KATTR_REL, "manifest", KATTR_HREF, "/site.webmanifest",
1955 KATTR__MAX);
1956 if (kerr != KCGI_OK)
1957 return 0;
1958 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1959 if (kerr != KCGI_OK)
1960 return 0;
1961 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1962 KATTR_REL, "mask-icon", KATTR_HREF,
1963 "/safari-pinned-tab.svg", KATTR__MAX);
1964 if (kerr != KCGI_OK)
1965 return 0;
1966 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1967 if (kerr != KCGI_OK)
1968 return 0;
1969 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1970 KATTR_REL, "stylesheet", KATTR_TYPE, "text/css",
1971 KATTR_HREF, "/gotweb.css", KATTR__MAX);
1972 if (kerr != KCGI_OK)
1973 return 0;
1974 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1975 if (kerr != KCGI_OK)
1976 return 0;
1977 break;
1978 case(TEMPL_HEADER):
1979 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1980 KATTR_ID, "got_link", KATTR__MAX);
1981 if (kerr != KCGI_OK)
1982 return 0;
1983 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1984 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
1985 KATTR_TARGET, "_sotd", KATTR__MAX);
1986 if (kerr != KCGI_OK)
1987 return 0;
1988 if (asprintf(&img_src, "/%s",
1989 gw_trans->gw_conf->got_logo) == -1)
1990 return 0;
1991 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
1992 KATTR_SRC, img_src, KATTR__MAX);
1993 if (kerr != KCGI_OK) {
1994 free(img_src);
1995 return 0;
1997 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1998 if (kerr != KCGI_OK) {
1999 free(img_src);
2000 return 0;
2002 break;
2003 case (TEMPL_SITEPATH):
2004 error = gw_output_site_link(gw_trans);
2005 if (error)
2006 return 0;
2007 break;
2008 case(TEMPL_TITLE):
2009 if (gw_trans->gw_conf->got_site_name != NULL) {
2010 kerr = khtml_puts(gw_trans->gw_html_req,
2011 gw_trans->gw_conf->got_site_name);
2012 if (kerr != KCGI_OK)
2013 return 0;
2015 break;
2016 case (TEMPL_SEARCH):
2017 break;
2018 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
2019 "search", KATTR__MAX);
2020 if (kerr != KCGI_OK)
2021 return 0;
2022 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
2023 KATTR_METHOD, "POST", KATTR__MAX);
2024 if (kerr != KCGI_OK)
2025 return 0;
2026 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
2027 "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
2028 KATTR_MAXLENGTH, "50", KATTR__MAX);
2029 if (kerr != KCGI_OK)
2030 return 0;
2031 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
2032 KATTR__MAX);
2033 if (kerr != KCGI_OK)
2034 return 0;
2035 kerr = khtml_puts(gw_trans->gw_html_req, "Search");
2036 if (kerr != KCGI_OK)
2037 return 0;
2038 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
2039 if (kerr != KCGI_OK)
2040 return 0;
2041 break;
2042 case(TEMPL_SITEOWNER):
2043 if (gw_trans->gw_conf->got_site_owner != NULL &&
2044 gw_trans->gw_conf->got_show_site_owner) {
2045 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2046 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
2047 if (kerr != KCGI_OK)
2048 return 0;
2049 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2050 KATTR_ID, "site_owner", KATTR__MAX);
2051 if (kerr != KCGI_OK)
2052 return 0;
2053 kerr = khtml_puts(gw_trans->gw_html_req,
2054 gw_trans->gw_conf->got_site_owner);
2055 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2056 if (kerr != KCGI_OK)
2057 return 0;
2059 break;
2060 case(TEMPL_CONTENT):
2061 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
2062 if (error) {
2063 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2064 KATTR_ID, "tmpl_err", KATTR__MAX);
2065 if (kerr != KCGI_OK)
2066 return 0;
2067 kerr = khttp_puts(gw_trans->gw_req, "Error: ");
2068 if (kerr != KCGI_OK)
2069 return 0;
2070 kerr = khttp_puts(gw_trans->gw_req, error->msg);
2071 if (kerr != KCGI_OK)
2072 return 0;
2073 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2074 if (kerr != KCGI_OK)
2075 return 0;
2077 break;
2078 default:
2079 return 0;
2081 return 1;
2084 static const struct got_error *
2085 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
2087 const struct got_error *error = NULL;
2088 char *ref_str = NULL;
2089 enum kcgi_err kerr = KCGI_OK;
2091 if (strcmp(str2, "") != 0) {
2092 if (asprintf(&ref_str, "(%s)", str2) == -1) {
2093 error = got_error_from_errno("asprintf");
2094 goto done;
2096 } else
2097 ref_str = strdup("");
2099 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2100 KATTR_ID, "header_commit_title", KATTR__MAX);
2101 if (kerr != KCGI_OK)
2102 goto done;
2103 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
2104 if (kerr != KCGI_OK)
2105 goto done;
2106 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2107 if (kerr != KCGI_OK)
2108 goto done;
2109 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2110 KATTR_ID, "header_commit", KATTR__MAX);
2111 if (kerr != KCGI_OK)
2112 goto done;
2113 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2114 if (kerr != KCGI_OK)
2115 goto done;
2116 kerr = khtml_puts(gw_trans->gw_html_req, " ");
2117 if (kerr != KCGI_OK)
2118 goto done;
2119 kerr = khtml_puts(gw_trans->gw_html_req, ref_str);
2120 if (kerr != KCGI_OK)
2121 goto done;
2122 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2123 done:
2124 if (error == NULL && kerr != KCGI_OK)
2125 error = gw_kcgi_error(kerr);
2126 return error;
2129 static const struct got_error *
2130 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2132 const struct got_error *error = NULL;
2133 enum kcgi_err kerr = KCGI_OK;
2135 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2136 KATTR_ID, "header_diff_title", KATTR__MAX);
2137 if (kerr != KCGI_OK)
2138 goto done;
2139 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2140 if (kerr != KCGI_OK)
2141 goto done;
2142 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2143 if (kerr != KCGI_OK)
2144 goto done;
2145 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2146 KATTR_ID, "header_diff", KATTR__MAX);
2147 if (kerr != KCGI_OK)
2148 goto done;
2149 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2150 if (kerr != KCGI_OK)
2151 goto done;
2152 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2153 if (kerr != KCGI_OK)
2154 goto done;
2155 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2156 if (kerr != KCGI_OK)
2157 goto done;
2158 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2159 done:
2160 if (error == NULL && kerr != KCGI_OK)
2161 error = gw_kcgi_error(kerr);
2162 return error;
2165 static const struct got_error *
2166 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2168 const struct got_error *error = NULL;
2169 enum kcgi_err kerr = KCGI_OK;
2171 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2172 KATTR_ID, "header_age_title", KATTR__MAX);
2173 if (kerr != KCGI_OK)
2174 goto done;
2175 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2176 if (kerr != KCGI_OK)
2177 goto done;
2178 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2179 if (kerr != KCGI_OK)
2180 goto done;
2181 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2182 KATTR_ID, "header_age", KATTR__MAX);
2183 if (kerr != KCGI_OK)
2184 goto done;
2185 kerr = khtml_puts(gw_trans->gw_html_req, str);
2186 if (kerr != KCGI_OK)
2187 goto done;
2188 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2189 done:
2190 if (error == NULL && kerr != KCGI_OK)
2191 error = gw_kcgi_error(kerr);
2192 return error;
2195 static const struct got_error *
2196 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2198 const struct got_error *error = NULL;
2199 enum kcgi_err kerr;
2201 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2202 KATTR_ID, "header_author_title", KATTR__MAX);
2203 if (kerr != KCGI_OK)
2204 goto done;
2205 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2206 if (kerr != KCGI_OK)
2207 goto done;
2208 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2209 if (kerr != KCGI_OK)
2210 goto done;
2211 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2212 KATTR_ID, "header_author", KATTR__MAX);
2213 if (kerr != KCGI_OK)
2214 goto done;
2215 kerr = khtml_puts(gw_trans->gw_html_req, str);
2216 if (kerr != KCGI_OK)
2217 goto done;
2218 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2219 done:
2220 if (error == NULL && kerr != KCGI_OK)
2221 error = gw_kcgi_error(kerr);
2222 return error;
2225 static const struct got_error *
2226 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2228 const struct got_error *error = NULL;
2229 enum kcgi_err kerr;
2231 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2232 KATTR_ID, "header_committer_title", KATTR__MAX);
2233 if (kerr != KCGI_OK)
2234 goto done;
2235 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2236 if (kerr != KCGI_OK)
2237 goto done;
2238 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2239 if (kerr != KCGI_OK)
2240 goto done;
2241 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2242 KATTR_ID, "header_committer", KATTR__MAX);
2243 if (kerr != KCGI_OK)
2244 goto done;
2245 kerr = khtml_puts(gw_trans->gw_html_req, str);
2246 if (kerr != KCGI_OK)
2247 goto done;
2248 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2249 done:
2250 if (error == NULL && kerr != KCGI_OK)
2251 error = gw_kcgi_error(kerr);
2252 return error;
2255 static const struct got_error *
2256 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2258 const struct got_error *error = NULL;
2259 enum kcgi_err kerr;
2261 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2262 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2263 if (kerr != KCGI_OK)
2264 goto done;
2265 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2266 if (kerr != KCGI_OK)
2267 goto done;
2268 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2269 if (kerr != KCGI_OK)
2270 goto done;
2271 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2272 KATTR_ID, "header_commit_msg", KATTR__MAX);
2273 if (kerr != KCGI_OK)
2274 goto done;
2275 kerr = khttp_puts(gw_trans->gw_req, str);
2276 if (kerr != KCGI_OK)
2277 goto done;
2278 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2279 done:
2280 if (error == NULL && kerr != KCGI_OK)
2281 error = gw_kcgi_error(kerr);
2282 return error;
2285 static const struct got_error *
2286 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2288 const struct got_error *error = NULL;
2289 enum kcgi_err kerr;
2291 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2292 KATTR_ID, "header_tree_title", KATTR__MAX);
2293 if (kerr != KCGI_OK)
2294 goto done;
2295 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2296 if (kerr != KCGI_OK)
2297 goto done;
2298 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2299 if (kerr != KCGI_OK)
2300 goto done;
2301 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2302 KATTR_ID, "header_tree", KATTR__MAX);
2303 if (kerr != KCGI_OK)
2304 goto done;
2305 kerr = khtml_puts(gw_trans->gw_html_req, str);
2306 if (kerr != KCGI_OK)
2307 goto done;
2308 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2309 done:
2310 if (error == NULL && kerr != KCGI_OK)
2311 error = gw_kcgi_error(kerr);
2312 return error;
2315 static const struct got_error *
2316 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2317 char *dir)
2319 const struct got_error *error = NULL;
2320 FILE *f = NULL;
2321 char *d_file = NULL;
2322 unsigned int len;
2323 size_t n;
2325 *description = NULL;
2326 if (gw_trans->gw_conf->got_show_repo_description == 0)
2327 return gw_empty_string(description);
2329 if (asprintf(&d_file, "%s/description", dir) == -1)
2330 return got_error_from_errno("asprintf");
2332 f = fopen(d_file, "r");
2333 if (f == NULL) {
2334 if (errno == ENOENT || errno == EACCES)
2335 return gw_empty_string(description);
2336 error = got_error_from_errno2("fopen", d_file);
2337 goto done;
2340 if (fseek(f, 0, SEEK_END) == -1) {
2341 error = got_ferror(f, GOT_ERR_IO);
2342 goto done;
2344 len = ftell(f);
2345 if (len == -1) {
2346 error = got_ferror(f, GOT_ERR_IO);
2347 goto done;
2349 if (fseek(f, 0, SEEK_SET) == -1) {
2350 error = got_ferror(f, GOT_ERR_IO);
2351 goto done;
2353 *description = calloc(len + 1, sizeof(**description));
2354 if (*description == NULL) {
2355 error = got_error_from_errno("calloc");
2356 goto done;
2359 n = fread(*description, 1, len, f);
2360 if (n == 0 && ferror(f))
2361 error = got_ferror(f, GOT_ERR_IO);
2362 done:
2363 if (f != NULL && fclose(f) == -1 && error == NULL)
2364 error = got_error_from_errno("fclose");
2365 free(d_file);
2366 return error;
2369 static const struct got_error *
2370 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2372 struct tm tm;
2373 time_t diff_time;
2374 char *years = "years ago", *months = "months ago";
2375 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2376 char *minutes = "minutes ago", *seconds = "seconds ago";
2377 char *now = "right now";
2378 char *s;
2379 char datebuf[29];
2381 *repo_age = NULL;
2383 switch (ref_tm) {
2384 case TM_DIFF:
2385 diff_time = time(NULL) - committer_time;
2386 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2387 if (asprintf(repo_age, "%lld %s",
2388 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2389 return got_error_from_errno("asprintf");
2390 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2391 if (asprintf(repo_age, "%lld %s",
2392 (diff_time / 60 / 60 / 24 / (365 / 12)),
2393 months) == -1)
2394 return got_error_from_errno("asprintf");
2395 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2396 if (asprintf(repo_age, "%lld %s",
2397 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2398 return got_error_from_errno("asprintf");
2399 } else if (diff_time > 60 * 60 * 24 * 2) {
2400 if (asprintf(repo_age, "%lld %s",
2401 (diff_time / 60 / 60 / 24), days) == -1)
2402 return got_error_from_errno("asprintf");
2403 } else if (diff_time > 60 * 60 * 2) {
2404 if (asprintf(repo_age, "%lld %s",
2405 (diff_time / 60 / 60), hours) == -1)
2406 return got_error_from_errno("asprintf");
2407 } else if (diff_time > 60 * 2) {
2408 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2409 minutes) == -1)
2410 return got_error_from_errno("asprintf");
2411 } else if (diff_time > 2) {
2412 if (asprintf(repo_age, "%lld %s", diff_time,
2413 seconds) == -1)
2414 return got_error_from_errno("asprintf");
2415 } else {
2416 if (asprintf(repo_age, "%s", now) == -1)
2417 return got_error_from_errno("asprintf");
2419 break;
2420 case TM_LONG:
2421 if (gmtime_r(&committer_time, &tm) == NULL)
2422 return got_error_from_errno("gmtime_r");
2424 s = asctime_r(&tm, datebuf);
2425 if (s == NULL)
2426 return got_error_from_errno("asctime_r");
2428 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2429 return got_error_from_errno("asprintf");
2430 break;
2432 return NULL;
2435 static const struct got_error *
2436 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2437 char *repo_ref, int ref_tm)
2439 const struct got_error *error = NULL;
2440 struct got_object_id *id = NULL;
2441 struct got_repository *repo = NULL;
2442 struct got_commit_object *commit = NULL;
2443 struct got_reflist_head refs;
2444 struct got_reflist_entry *re;
2445 struct got_reference *head_ref;
2446 int is_head = 0;
2447 time_t committer_time = 0, cmp_time = 0;
2448 const char *refname;
2450 *repo_age = NULL;
2451 SIMPLEQ_INIT(&refs);
2453 if (repo_ref == NULL)
2454 return NULL;
2456 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
2457 is_head = 1;
2459 if (gw_trans->gw_conf->got_show_repo_age == 0)
2460 return NULL;
2462 error = got_repo_open(&repo, dir, NULL);
2463 if (error)
2464 goto done;
2466 if (is_head)
2467 error = got_ref_list(&refs, repo, "refs/heads",
2468 got_ref_cmp_by_name, NULL);
2469 else
2470 error = got_ref_list(&refs, repo, repo_ref,
2471 got_ref_cmp_by_name, NULL);
2472 if (error)
2473 goto done;
2475 SIMPLEQ_FOREACH(re, &refs, entry) {
2476 if (is_head)
2477 refname = strdup(repo_ref);
2478 else
2479 refname = got_ref_get_name(re->ref);
2480 error = got_ref_open(&head_ref, repo, refname, 0);
2481 if (error)
2482 goto done;
2484 error = got_ref_resolve(&id, repo, head_ref);
2485 got_ref_close(head_ref);
2486 if (error)
2487 goto done;
2489 error = got_object_open_as_commit(&commit, repo, id);
2490 if (error)
2491 goto done;
2493 committer_time =
2494 got_object_commit_get_committer_time(commit);
2496 if (cmp_time < committer_time)
2497 cmp_time = committer_time;
2500 if (cmp_time != 0) {
2501 committer_time = cmp_time;
2502 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2504 done:
2505 got_ref_list_free(&refs);
2506 free(id);
2507 return error;
2510 static const struct got_error *
2511 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2513 const struct got_error *error;
2514 FILE *f = NULL;
2515 struct got_object_id *id1 = NULL, *id2 = NULL;
2516 char *label1 = NULL, *label2 = NULL, *line = NULL;
2517 int obj_type;
2518 size_t linesize = 0;
2519 ssize_t linelen;
2520 enum kcgi_err kerr = KCGI_OK;
2522 f = got_opentemp();
2523 if (f == NULL)
2524 return NULL;
2526 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
2527 if (error)
2528 goto done;
2530 if (strncmp(header->parent_id, "/dev/null", 9) != 0) {
2531 error = got_repo_match_object_id(&id1, &label1,
2532 header->parent_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2533 if (error)
2534 goto done;
2537 error = got_repo_match_object_id(&id2, &label2,
2538 header->commit_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2539 if (error)
2540 goto done;
2542 error = got_object_get_type(&obj_type, header->repo, id2);
2543 if (error)
2544 goto done;
2545 switch (obj_type) {
2546 case GOT_OBJ_TYPE_BLOB:
2547 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
2548 header->repo, f);
2549 break;
2550 case GOT_OBJ_TYPE_TREE:
2551 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
2552 header->repo, f);
2553 break;
2554 case GOT_OBJ_TYPE_COMMIT:
2555 error = got_diff_objects_as_commits(id1, id2, 3, 0,
2556 header->repo, f);
2557 break;
2558 default:
2559 error = got_error(GOT_ERR_OBJ_TYPE);
2561 if (error)
2562 goto done;
2564 if (fseek(f, 0, SEEK_SET) == -1) {
2565 error = got_ferror(f, GOT_ERR_IO);
2566 goto done;
2569 while ((linelen = getline(&line, &linesize, f)) != -1) {
2570 error = gw_colordiff_line(gw_trans, line);
2571 if (error)
2572 goto done;
2573 /* XXX: KHTML_PRETTY breaks this */
2574 kerr = khtml_puts(gw_trans->gw_html_req, line);
2575 if (kerr != KCGI_OK)
2576 goto done;
2577 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2578 if (kerr != KCGI_OK)
2579 goto done;
2581 if (linelen == -1 && ferror(f))
2582 error = got_error_from_errno("getline");
2583 done:
2584 if (f && fclose(f) == -1 && error == NULL)
2585 error = got_error_from_errno("fclose");
2586 free(line);
2587 free(label1);
2588 free(label2);
2589 free(id1);
2590 free(id2);
2592 if (error == NULL && kerr != KCGI_OK)
2593 error = gw_kcgi_error(kerr);
2594 return error;
2597 static const struct got_error *
2598 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2600 const struct got_error *error = NULL;
2601 struct got_repository *repo;
2602 const char *gitconfig_owner;
2604 *owner = NULL;
2606 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2607 return NULL;
2609 error = got_repo_open(&repo, dir, NULL);
2610 if (error)
2611 return error;
2612 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2613 if (gitconfig_owner) {
2614 *owner = strdup(gitconfig_owner);
2615 if (*owner == NULL)
2616 error = got_error_from_errno("strdup");
2618 got_repo_close(repo);
2619 return error;
2622 static const struct got_error *
2623 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2625 const struct got_error *error = NULL;
2626 FILE *f;
2627 char *d_file = NULL;
2628 unsigned int len;
2629 size_t n;
2631 *url = NULL;
2633 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2634 return got_error_from_errno("asprintf");
2636 f = fopen(d_file, "r");
2637 if (f == NULL) {
2638 if (errno != ENOENT && errno != EACCES)
2639 error = got_error_from_errno2("fopen", d_file);
2640 goto done;
2643 if (fseek(f, 0, SEEK_END) == -1) {
2644 error = got_ferror(f, GOT_ERR_IO);
2645 goto done;
2647 len = ftell(f);
2648 if (len == -1) {
2649 error = got_ferror(f, GOT_ERR_IO);
2650 goto done;
2652 if (fseek(f, 0, SEEK_SET) == -1) {
2653 error = got_ferror(f, GOT_ERR_IO);
2654 goto done;
2657 *url = calloc(len + 1, sizeof(**url));
2658 if (*url == NULL) {
2659 error = got_error_from_errno("calloc");
2660 goto done;
2663 n = fread(*url, 1, len, f);
2664 if (n == 0 && ferror(f))
2665 error = got_ferror(f, GOT_ERR_IO);
2666 done:
2667 if (f && fclose(f) == -1 && error == NULL)
2668 error = got_error_from_errno("fclose");
2669 free(d_file);
2670 return NULL;
2673 static const struct got_error *
2674 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
2675 int limit, int tag_type)
2677 const struct got_error *error = NULL;
2678 struct got_repository *repo = NULL;
2679 struct got_reflist_head refs;
2680 struct got_reflist_entry *re;
2681 char *age = NULL;
2682 char *id_str = NULL, *newline, *href_commits = NULL;
2683 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
2684 struct got_tag_object *tag = NULL;
2685 enum kcgi_err kerr = KCGI_OK;
2686 int summary_header_displayed = 0;
2688 SIMPLEQ_INIT(&refs);
2690 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2691 if (error)
2692 goto done;
2694 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
2695 if (error)
2696 goto done;
2698 SIMPLEQ_FOREACH(re, &refs, entry) {
2699 const char *refname;
2700 const char *tagger;
2701 const char *tag_commit;
2702 time_t tagger_time;
2703 struct got_object_id *id;
2704 struct got_commit_object *commit = NULL;
2706 refname = got_ref_get_name(re->ref);
2707 if (strncmp(refname, "refs/tags/", 10) != 0)
2708 continue;
2709 refname += 10;
2711 error = got_ref_resolve(&id, repo, re->ref);
2712 if (error)
2713 goto done;
2715 error = got_object_open_as_tag(&tag, repo, id);
2716 if (error) {
2717 if (error->code != GOT_ERR_OBJ_TYPE) {
2718 free(id);
2719 goto done;
2721 /* "lightweight" tag */
2722 error = got_object_open_as_commit(&commit, repo, id);
2723 if (error) {
2724 free(id);
2725 goto done;
2727 tagger = got_object_commit_get_committer(commit);
2728 tagger_time =
2729 got_object_commit_get_committer_time(commit);
2730 error = got_object_id_str(&id_str, id);
2731 free(id);
2732 if (error)
2733 goto done;
2734 } else {
2735 free(id);
2736 tagger = got_object_tag_get_tagger(tag);
2737 tagger_time = got_object_tag_get_tagger_time(tag);
2738 error = got_object_id_str(&id_str,
2739 got_object_tag_get_object_id(tag));
2740 if (error)
2741 goto done;
2743 if (error)
2744 goto done;
2746 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
2747 strlen(id_str)) != 0)
2748 continue;
2750 if (commit) {
2751 error = got_object_commit_get_logmsg(&tag_commit0,
2752 commit);
2753 if (error)
2754 goto done;
2755 got_object_commit_close(commit);
2756 } else {
2757 tag_commit0 = strdup(got_object_tag_get_message(tag));
2758 if (tag_commit0 == NULL) {
2759 error = got_error_from_errno("strdup");
2760 goto done;
2764 tag_commit = tag_commit0;
2765 while (*tag_commit == '\n')
2766 tag_commit++;
2768 switch (tag_type) {
2769 case TAGBRIEF:
2770 newline = strchr(tag_commit, '\n');
2771 if (newline)
2772 *newline = '\0';
2774 if (summary_header_displayed == 0) {
2775 kerr = khtml_attr(gw_trans->gw_html_req,
2776 KELEM_DIV, KATTR_ID,
2777 "summary_tags_title_wrapper", KATTR__MAX);
2778 if (kerr != KCGI_OK)
2779 goto done;
2780 kerr = khtml_attr(gw_trans->gw_html_req,
2781 KELEM_DIV, KATTR_ID,
2782 "summary_tags_title", KATTR__MAX);
2783 if (kerr != KCGI_OK)
2784 goto done;
2785 kerr = khtml_puts(gw_trans->gw_html_req,
2786 "Tags");
2787 if (kerr != KCGI_OK)
2788 goto done;
2789 kerr = khtml_closeelem(gw_trans->gw_html_req,
2790 2);
2791 if (kerr != KCGI_OK)
2792 goto done;
2793 kerr = khtml_attr(gw_trans->gw_html_req,
2794 KELEM_DIV, KATTR_ID,
2795 "summary_tags_content", KATTR__MAX);
2796 if (kerr != KCGI_OK)
2797 goto done;
2798 summary_header_displayed = 1;
2801 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2802 KATTR_ID, "tags_wrapper", KATTR__MAX);
2803 if (kerr != KCGI_OK)
2804 goto done;
2805 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2806 KATTR_ID, "tags_age", KATTR__MAX);
2807 if (kerr != KCGI_OK)
2808 goto done;
2809 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
2810 if (error)
2811 goto done;
2812 kerr = khtml_puts(gw_trans->gw_html_req,
2813 age ? age : "");
2814 if (kerr != KCGI_OK)
2815 goto done;
2816 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2817 if (kerr != KCGI_OK)
2818 goto done;
2819 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2820 KATTR_ID, "tags", KATTR__MAX);
2821 if (kerr != KCGI_OK)
2822 goto done;
2823 kerr = khtml_puts(gw_trans->gw_html_req, refname);
2824 if (kerr != KCGI_OK)
2825 goto done;
2826 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2827 if (kerr != KCGI_OK)
2828 goto done;
2829 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2830 KATTR_ID, "tags_name", KATTR__MAX);
2831 if (kerr != KCGI_OK)
2832 goto done;
2833 if (asprintf(&href_tag, "?path=%s&action=tag&commit=%s",
2834 gw_trans->repo_name, id_str) == -1) {
2835 error = got_error_from_errno("asprintf");
2836 goto done;
2838 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2839 KATTR_HREF, href_tag, KATTR__MAX);
2840 if (kerr != KCGI_OK)
2841 goto done;
2842 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
2843 if (kerr != KCGI_OK)
2844 goto done;
2845 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2846 if (kerr != KCGI_OK)
2847 goto done;
2849 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2850 KATTR_ID, "navs_wrapper", KATTR__MAX);
2851 if (kerr != KCGI_OK)
2852 goto done;
2853 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2854 KATTR_ID, "navs", KATTR__MAX);
2855 if (kerr != KCGI_OK)
2856 goto done;
2858 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2859 KATTR_HREF, href_tag, KATTR__MAX);
2860 if (kerr != KCGI_OK)
2861 goto done;
2862 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
2863 if (kerr != KCGI_OK)
2864 goto done;
2865 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2866 if (kerr != KCGI_OK)
2867 goto done;
2869 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2870 if (kerr != KCGI_OK)
2871 goto done;
2872 if (asprintf(&href_briefs,
2873 "?path=%s&action=briefs&commit=%s",
2874 gw_trans->repo_name, id_str) == -1) {
2875 error = got_error_from_errno("asprintf");
2876 goto done;
2878 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2879 KATTR_HREF, href_briefs, KATTR__MAX);
2880 if (kerr != KCGI_OK)
2881 goto done;
2882 kerr = khtml_puts(gw_trans->gw_html_req,
2883 "commit briefs");
2884 if (kerr != KCGI_OK)
2885 goto done;
2886 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2887 if (kerr != KCGI_OK)
2888 goto done;
2890 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2891 if (kerr != KCGI_OK)
2892 goto done;
2894 if (asprintf(&href_commits,
2895 "?path=%s&action=commits&commit=%s",
2896 gw_trans->repo_name, id_str) == -1) {
2897 error = got_error_from_errno("asprintf");
2898 goto done;
2900 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2901 KATTR_HREF, href_commits, KATTR__MAX);
2902 if (kerr != KCGI_OK)
2903 goto done;
2904 kerr = khtml_puts(gw_trans->gw_html_req,
2905 "commits");
2906 if (kerr != KCGI_OK)
2907 goto done;
2908 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2909 if (kerr != KCGI_OK)
2910 goto done;
2912 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2913 KATTR_ID, "dotted_line", KATTR__MAX);
2914 if (kerr != KCGI_OK)
2915 goto done;
2916 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2917 if (kerr != KCGI_OK)
2918 goto done;
2919 break;
2920 case TAGFULL:
2921 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2922 KATTR_ID, "tag_info_date_title", KATTR__MAX);
2923 if (kerr != KCGI_OK)
2924 goto done;
2925 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
2926 if (kerr != KCGI_OK)
2927 goto done;
2928 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2929 if (kerr != KCGI_OK)
2930 goto done;
2931 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2932 KATTR_ID, "tag_info_date", KATTR__MAX);
2933 if (kerr != KCGI_OK)
2934 goto done;
2935 error = gw_get_time_str(&age, tagger_time, TM_LONG);
2936 if (error)
2937 goto done;
2938 kerr = khtml_puts(gw_trans->gw_html_req,
2939 age ? age : "");
2940 if (kerr != KCGI_OK)
2941 goto done;
2942 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2943 if (kerr != KCGI_OK)
2944 goto done;
2946 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2947 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
2948 if (kerr != KCGI_OK)
2949 goto done;
2950 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
2951 if (kerr != KCGI_OK)
2952 goto done;
2953 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2954 if (kerr != KCGI_OK)
2955 goto done;
2956 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2957 KATTR_ID, "tag_info_date", KATTR__MAX);
2958 if (kerr != KCGI_OK)
2959 goto done;
2960 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
2961 if (kerr != KCGI_OK)
2962 goto done;
2963 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2964 if (kerr != KCGI_OK)
2965 goto done;
2967 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2968 KATTR_ID, "tag_info", KATTR__MAX);
2969 if (kerr != KCGI_OK)
2970 goto done;
2971 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
2972 if (kerr != KCGI_OK)
2973 goto done;
2974 break;
2975 default:
2976 break;
2978 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2979 if (kerr != KCGI_OK)
2980 goto done;
2982 if (limit && --limit == 0)
2983 break;
2985 if (tag)
2986 got_object_tag_close(tag);
2987 tag = NULL;
2988 free(id_str);
2989 id_str = NULL;
2990 free(age);
2991 age = NULL;
2992 free(tag_commit0);
2993 tag_commit0 = NULL;
2994 free(href_tag);
2995 href_tag = NULL;
2996 free(href_briefs);
2997 href_briefs = NULL;
2998 free(href_commits);
2999 href_commits = NULL;
3001 done:
3002 if (tag)
3003 got_object_tag_close(tag);
3004 free(id_str);
3005 free(age);
3006 free(tag_commit0);
3007 free(href_tag);
3008 free(href_briefs);
3009 free(href_commits);
3010 got_ref_list_free(&refs);
3011 if (repo)
3012 got_repo_close(repo);
3013 if (error == NULL && kerr != KCGI_OK)
3014 error = gw_kcgi_error(kerr);
3015 return error;
3018 static void
3019 gw_free_headers(struct gw_header *header)
3021 free(header->id);
3022 free(header->path);
3023 if (header->commit != NULL)
3024 got_object_commit_close(header->commit);
3025 if (header->repo)
3026 got_repo_close(header->repo);
3027 free(header->refs_str);
3028 free(header->commit_id);
3029 free(header->parent_id);
3030 free(header->tree_id);
3031 free(header->commit_msg);
3034 static struct gw_header *
3035 gw_init_header()
3037 struct gw_header *header;
3039 header = malloc(sizeof(*header));
3040 if (header == NULL)
3041 return NULL;
3043 header->repo = NULL;
3044 header->commit = NULL;
3045 header->id = NULL;
3046 header->path = NULL;
3047 SIMPLEQ_INIT(&header->refs);
3049 return header;
3052 static const struct got_error *
3053 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
3054 int limit)
3056 const struct got_error *error = NULL;
3057 struct got_commit_graph *graph = NULL;
3059 error = got_commit_graph_open(&graph, header->path, 0);
3060 if (error)
3061 goto done;
3063 error = got_commit_graph_iter_start(graph, header->id, header->repo,
3064 NULL, NULL);
3065 if (error)
3066 goto done;
3068 for (;;) {
3069 error = got_commit_graph_iter_next(&header->id, graph,
3070 header->repo, NULL, NULL);
3071 if (error) {
3072 if (error->code == GOT_ERR_ITER_COMPLETED)
3073 error = NULL;
3074 goto done;
3076 if (header->id == NULL)
3077 goto done;
3079 error = got_object_open_as_commit(&header->commit, header->repo,
3080 header->id);
3081 if (error)
3082 goto done;
3084 error = gw_get_commit(gw_trans, header);
3085 if (limit > 1) {
3086 struct gw_header *n_header = NULL;
3087 if ((n_header = gw_init_header()) == NULL) {
3088 error = got_error_from_errno("malloc");
3089 goto done;
3092 n_header->refs_str = strdup(header->refs_str);
3093 n_header->commit_id = strdup(header->commit_id);
3094 n_header->parent_id = strdup(header->parent_id);
3095 n_header->tree_id = strdup(header->tree_id);
3096 n_header->author = strdup(header->author);
3097 n_header->committer = strdup(header->committer);
3098 n_header->commit_msg = strdup(header->commit_msg);
3099 n_header->committer_time = header->committer_time;
3100 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3101 entry);
3103 if (error || (limit && --limit == 0))
3104 break;
3106 done:
3107 if (graph)
3108 got_commit_graph_close(graph);
3109 return error;
3112 static const struct got_error *
3113 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
3115 const struct got_error *error = NULL;
3116 struct got_reflist_entry *re;
3117 struct got_object_id *id2 = NULL;
3118 struct got_object_qid *parent_id;
3119 char *refs_str = NULL, *commit_msg = NULL, *commit_msg0;
3121 /*print commit*/
3122 SIMPLEQ_FOREACH(re, &header->refs, entry) {
3123 char *s;
3124 const char *name;
3125 struct got_tag_object *tag = NULL;
3126 int cmp;
3128 name = got_ref_get_name(re->ref);
3129 if (strcmp(name, GOT_REF_HEAD) == 0)
3130 continue;
3131 if (strncmp(name, "refs/", 5) == 0)
3132 name += 5;
3133 if (strncmp(name, "got/", 4) == 0)
3134 continue;
3135 if (strncmp(name, "heads/", 6) == 0)
3136 name += 6;
3137 if (strncmp(name, "remotes/", 8) == 0)
3138 name += 8;
3139 if (strncmp(name, "tags/", 5) == 0) {
3140 error = got_object_open_as_tag(&tag, header->repo,
3141 re->id);
3142 if (error) {
3143 if (error->code != GOT_ERR_OBJ_TYPE)
3144 continue;
3146 * Ref points at something other
3147 * than a tag.
3149 error = NULL;
3150 tag = NULL;
3153 cmp = got_object_id_cmp(tag ?
3154 got_object_tag_get_object_id(tag) : re->id, header->id);
3155 if (tag)
3156 got_object_tag_close(tag);
3157 if (cmp != 0)
3158 continue;
3159 s = refs_str;
3160 if (asprintf(&refs_str, "%s%s%s", s ? s : "",
3161 s ? ", " : "", name) == -1) {
3162 error = got_error_from_errno("asprintf");
3163 free(s);
3164 return error;
3166 header->refs_str = strdup(refs_str);
3167 free(s);
3170 if (refs_str == NULL)
3171 header->refs_str = strdup("");
3172 free(refs_str);
3174 error = got_object_id_str(&header->commit_id, header->id);
3175 if (error)
3176 return error;
3178 error = got_object_id_str(&header->tree_id,
3179 got_object_commit_get_tree_id(header->commit));
3180 if (error)
3181 return error;
3183 if (gw_trans->action == GW_DIFF) {
3184 parent_id = SIMPLEQ_FIRST(
3185 got_object_commit_get_parent_ids(header->commit));
3186 if (parent_id != NULL) {
3187 id2 = got_object_id_dup(parent_id->id);
3188 free (parent_id);
3189 error = got_object_id_str(&header->parent_id, id2);
3190 if (error)
3191 return error;
3192 free(id2);
3193 } else
3194 header->parent_id = strdup("/dev/null");
3195 } else
3196 header->parent_id = strdup("");
3198 header->committer_time =
3199 got_object_commit_get_committer_time(header->commit);
3201 header->author =
3202 got_object_commit_get_author(header->commit);
3203 header->committer =
3204 got_object_commit_get_committer(header->commit);
3205 if (error)
3206 return error;
3207 error = got_object_commit_get_logmsg(&commit_msg0, header->commit);
3208 if (error)
3209 return error;
3211 commit_msg = commit_msg0;
3212 while (*commit_msg == '\n')
3213 commit_msg++;
3215 header->commit_msg = strdup(commit_msg);
3216 free(commit_msg0);
3217 return error;
3220 static const struct got_error *
3221 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3223 const struct got_error *error = NULL;
3224 char *in_repo_path = NULL;
3226 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
3227 if (error)
3228 return error;
3230 if (gw_trans->commit == NULL) {
3231 struct got_reference *head_ref;
3232 error = got_ref_open(&head_ref, header->repo,
3233 gw_trans->headref, 0);
3234 if (error)
3235 return error;
3237 error = got_ref_resolve(&header->id, header->repo, head_ref);
3238 got_ref_close(head_ref);
3239 if (error)
3240 return error;
3242 error = got_object_open_as_commit(&header->commit,
3243 header->repo, header->id);
3244 } else {
3245 struct got_reference *ref;
3246 error = got_ref_open(&ref, header->repo, gw_trans->commit, 0);
3247 if (error == NULL) {
3248 int obj_type;
3249 error = got_ref_resolve(&header->id, header->repo, ref);
3250 got_ref_close(ref);
3251 if (error)
3252 return error;
3253 error = got_object_get_type(&obj_type, header->repo,
3254 header->id);
3255 if (error)
3256 return error;
3257 if (obj_type == GOT_OBJ_TYPE_TAG) {
3258 struct got_tag_object *tag;
3259 error = got_object_open_as_tag(&tag,
3260 header->repo, header->id);
3261 if (error)
3262 return error;
3263 if (got_object_tag_get_object_type(tag) !=
3264 GOT_OBJ_TYPE_COMMIT) {
3265 got_object_tag_close(tag);
3266 error = got_error(GOT_ERR_OBJ_TYPE);
3267 return error;
3269 free(header->id);
3270 header->id = got_object_id_dup(
3271 got_object_tag_get_object_id(tag));
3272 if (header->id == NULL)
3273 error = got_error_from_errno(
3274 "got_object_id_dup");
3275 got_object_tag_close(tag);
3276 if (error)
3277 return error;
3278 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3279 error = got_error(GOT_ERR_OBJ_TYPE);
3280 return error;
3282 error = got_object_open_as_commit(&header->commit,
3283 header->repo, header->id);
3284 if (error)
3285 return error;
3287 if (header->commit == NULL) {
3288 error = got_repo_match_object_id_prefix(&header->id,
3289 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
3290 header->repo);
3291 if (error)
3292 return error;
3294 error = got_repo_match_object_id_prefix(&header->id,
3295 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
3296 header->repo);
3299 error = got_repo_map_path(&in_repo_path, header->repo,
3300 gw_trans->repo_path, 1);
3301 if (error)
3302 return error;
3304 if (in_repo_path) {
3305 header->path = strdup(in_repo_path);
3307 free(in_repo_path);
3309 error = got_ref_list(&header->refs, header->repo, NULL,
3310 got_ref_cmp_by_name, NULL);
3311 if (error)
3312 return error;
3314 error = gw_get_commits(gw_trans, header, limit);
3315 return error;
3318 struct blame_line {
3319 int annotated;
3320 char *id_str;
3321 char *committer;
3322 char datebuf[11]; /* YYYY-MM-DD + NUL */
3325 struct gw_blame_cb_args {
3326 struct blame_line *lines;
3327 int nlines;
3328 int nlines_prec;
3329 int lineno_cur;
3330 off_t *line_offsets;
3331 FILE *f;
3332 struct got_repository *repo;
3333 struct gw_trans *gw_trans;
3336 static const struct got_error *
3337 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3339 const struct got_error *err = NULL;
3340 struct gw_blame_cb_args *a = arg;
3341 struct blame_line *bline;
3342 char *line = NULL;
3343 size_t linesize = 0;
3344 struct got_commit_object *commit = NULL;
3345 off_t offset;
3346 struct tm tm;
3347 time_t committer_time;
3348 enum kcgi_err kerr = KCGI_OK;
3350 if (nlines != a->nlines ||
3351 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3352 return got_error(GOT_ERR_RANGE);
3354 if (lineno == -1)
3355 return NULL; /* no change in this commit */
3357 /* Annotate this line. */
3358 bline = &a->lines[lineno - 1];
3359 if (bline->annotated)
3360 return NULL;
3361 err = got_object_id_str(&bline->id_str, id);
3362 if (err)
3363 return err;
3365 err = got_object_open_as_commit(&commit, a->repo, id);
3366 if (err)
3367 goto done;
3369 bline->committer = strdup(got_object_commit_get_committer(commit));
3370 if (bline->committer == NULL) {
3371 err = got_error_from_errno("strdup");
3372 goto done;
3375 committer_time = got_object_commit_get_committer_time(commit);
3376 if (localtime_r(&committer_time, &tm) == NULL)
3377 return got_error_from_errno("localtime_r");
3378 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3379 &tm) >= sizeof(bline->datebuf)) {
3380 err = got_error(GOT_ERR_NO_SPACE);
3381 goto done;
3383 bline->annotated = 1;
3385 /* Print lines annotated so far. */
3386 bline = &a->lines[a->lineno_cur - 1];
3387 if (!bline->annotated)
3388 goto done;
3390 offset = a->line_offsets[a->lineno_cur - 1];
3391 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3392 err = got_error_from_errno("fseeko");
3393 goto done;
3396 while (bline->annotated) {
3397 char *smallerthan, *at, *nl, *committer;
3398 char *lineno = NULL, *href_diff = NULL, *href_link = NULL;
3399 size_t len;
3401 if (getline(&line, &linesize, a->f) == -1) {
3402 if (ferror(a->f))
3403 err = got_error_from_errno("getline");
3404 break;
3407 committer = bline->committer;
3408 smallerthan = strchr(committer, '<');
3409 if (smallerthan && smallerthan[1] != '\0')
3410 committer = smallerthan + 1;
3411 at = strchr(committer, '@');
3412 if (at)
3413 *at = '\0';
3414 len = strlen(committer);
3415 if (len >= 9)
3416 committer[8] = '\0';
3418 nl = strchr(line, '\n');
3419 if (nl)
3420 *nl = '\0';
3422 if (a->gw_trans->repo_folder == NULL)
3423 a->gw_trans->repo_folder = strdup("");
3424 if (a->gw_trans->repo_folder == NULL)
3425 goto err;
3427 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3428 "blame_wrapper", KATTR__MAX);
3429 if (kerr != KCGI_OK)
3430 goto err;
3431 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3432 "blame_number", KATTR__MAX);
3433 if (kerr != KCGI_OK)
3434 goto err;
3435 if (asprintf(&lineno, "%.*d", a->nlines_prec,
3436 a->lineno_cur) == -1)
3437 goto err;
3438 kerr = khtml_puts(a->gw_trans->gw_html_req, lineno);
3439 if (kerr != KCGI_OK)
3440 goto err;
3441 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3442 if (kerr != KCGI_OK)
3443 goto err;
3445 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3446 "blame_hash", KATTR__MAX);
3447 if (kerr != KCGI_OK)
3448 goto err;
3449 if (asprintf(&href_diff,
3450 "?path=%s&action=diff&commit=%s&file=%s&folder=%s",
3451 a->gw_trans->repo_name, bline->id_str,
3452 a->gw_trans->repo_file, a->gw_trans->repo_folder) == -1) {
3453 err = got_error_from_errno("asprintf");
3454 goto err;
3456 if (asprintf(&href_link, "%.8s", bline->id_str) == -1) {
3457 err = got_error_from_errno("asprintf");
3458 goto err;
3460 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3461 KATTR_HREF, href_diff, KATTR__MAX);
3462 if (kerr != KCGI_OK)
3463 goto done;
3464 kerr = khtml_puts(a->gw_trans->gw_html_req, href_link);
3465 if (kerr != KCGI_OK)
3466 goto err;
3467 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3468 if (kerr != KCGI_OK)
3469 goto err;
3471 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3472 "blame_date", KATTR__MAX);
3473 if (kerr != KCGI_OK)
3474 goto err;
3475 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3476 if (kerr != KCGI_OK)
3477 goto err;
3478 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3479 if (kerr != KCGI_OK)
3480 goto err;
3482 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3483 "blame_author", KATTR__MAX);
3484 if (kerr != KCGI_OK)
3485 goto err;
3486 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
3487 if (kerr != KCGI_OK)
3488 goto err;
3489 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3490 if (kerr != KCGI_OK)
3491 goto err;
3493 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3494 "blame_code", KATTR__MAX);
3495 if (kerr != KCGI_OK)
3496 goto err;
3497 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
3498 if (kerr != KCGI_OK)
3499 goto err;
3500 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3501 if (kerr != KCGI_OK)
3502 goto err;
3504 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3505 if (kerr != KCGI_OK)
3506 goto err;
3508 a->lineno_cur++;
3509 bline = &a->lines[a->lineno_cur - 1];
3510 err:
3511 free(lineno);
3512 free(href_diff);
3513 free(href_link);
3515 done:
3516 if (commit)
3517 got_object_commit_close(commit);
3518 free(line);
3519 if (err == NULL && kerr != KCGI_OK)
3520 err = gw_kcgi_error(kerr);
3521 return err;
3524 static const struct got_error *
3525 gw_output_file_blame(struct gw_trans *gw_trans)
3527 const struct got_error *error = NULL;
3528 struct got_repository *repo = NULL;
3529 struct got_object_id *obj_id = NULL;
3530 struct got_object_id *commit_id = NULL;
3531 struct got_blob_object *blob = NULL;
3532 char *path = NULL, *in_repo_path = NULL;
3533 struct gw_blame_cb_args bca;
3534 int i, obj_type;
3535 size_t filesize;
3537 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3538 if (error)
3539 return error;
3541 if (asprintf(&path, "%s%s%s",
3542 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3543 gw_trans->repo_folder ? "/" : "",
3544 gw_trans->repo_file) == -1) {
3545 error = got_error_from_errno("asprintf");
3546 goto done;
3549 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3550 if (error)
3551 goto done;
3553 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3554 GOT_OBJ_TYPE_COMMIT, 1, repo);
3555 if (error)
3556 goto done;
3558 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3559 if (error)
3560 goto done;
3562 if (obj_id == NULL) {
3563 error = got_error(GOT_ERR_NO_OBJ);
3564 goto done;
3567 error = got_object_get_type(&obj_type, repo, obj_id);
3568 if (error)
3569 goto done;
3571 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3572 error = got_error(GOT_ERR_OBJ_TYPE);
3573 goto done;
3576 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3577 if (error)
3578 goto done;
3580 bca.f = got_opentemp();
3581 if (bca.f == NULL) {
3582 error = got_error_from_errno("got_opentemp");
3583 goto done;
3585 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3586 &bca.line_offsets, bca.f, blob);
3587 if (error || bca.nlines == 0)
3588 goto done;
3590 /* Don't include \n at EOF in the blame line count. */
3591 if (bca.line_offsets[bca.nlines - 1] == filesize)
3592 bca.nlines--;
3594 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3595 if (bca.lines == NULL) {
3596 error = got_error_from_errno("calloc");
3597 goto done;
3599 bca.lineno_cur = 1;
3600 bca.nlines_prec = 0;
3601 i = bca.nlines;
3602 while (i > 0) {
3603 i /= 10;
3604 bca.nlines_prec++;
3606 bca.repo = repo;
3607 bca.gw_trans = gw_trans;
3609 error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
3610 NULL, NULL);
3611 if (error)
3612 goto done;
3613 done:
3614 free(bca.line_offsets);
3615 free(in_repo_path);
3616 free(commit_id);
3617 free(obj_id);
3618 free(path);
3620 for (i = 0; i < bca.nlines; i++) {
3621 struct blame_line *bline = &bca.lines[i];
3622 free(bline->id_str);
3623 free(bline->committer);
3625 free(bca.lines);
3626 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3627 error = got_error_from_errno("fclose");
3628 if (blob)
3629 got_object_blob_close(blob);
3630 if (repo)
3631 got_repo_close(repo);
3632 return error;
3635 static const struct got_error *
3636 gw_output_blob_buf(struct gw_trans *gw_trans)
3638 const struct got_error *error = NULL;
3639 struct got_repository *repo = NULL;
3640 struct got_object_id *obj_id = NULL;
3641 struct got_object_id *commit_id = NULL;
3642 struct got_blob_object *blob = NULL;
3643 char *path = NULL, *in_repo_path = NULL;
3644 int obj_type, set_mime = 0;
3645 size_t len, hdrlen;
3646 const uint8_t *buf;
3647 enum kcgi_err kerr = KCGI_OK;
3649 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3650 if (error)
3651 return error;
3653 if (asprintf(&path, "%s%s%s",
3654 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3655 gw_trans->repo_folder ? "/" : "",
3656 gw_trans->repo_file) == -1) {
3657 error = got_error_from_errno("asprintf");
3658 goto done;
3661 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3662 if (error)
3663 goto done;
3665 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3666 GOT_OBJ_TYPE_COMMIT, 1, repo);
3667 if (error)
3668 goto done;
3670 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3671 if (error)
3672 goto done;
3674 if (obj_id == NULL) {
3675 error = got_error(GOT_ERR_NO_OBJ);
3676 goto done;
3679 error = got_object_get_type(&obj_type, repo, obj_id);
3680 if (error)
3681 goto done;
3683 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3684 error = got_error(GOT_ERR_OBJ_TYPE);
3685 goto done;
3688 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3689 if (error)
3690 goto done;
3692 hdrlen = got_object_blob_get_hdrlen(blob);
3693 do {
3694 error = got_object_blob_read_block(&len, blob);
3695 if (error)
3696 goto done;
3697 buf = got_object_blob_get_read_buf(blob);
3700 * Skip blob object header first time around,
3701 * which also contains a zero byte.
3703 buf += hdrlen;
3704 if (set_mime == 0) {
3705 if (isbinary(buf, len - hdrlen))
3706 gw_trans->mime = KMIME_APP_OCTET_STREAM;
3707 else
3708 gw_trans->mime = KMIME_TEXT_PLAIN;
3709 set_mime = 1;
3710 error = gw_display_index(gw_trans);
3711 if (error)
3712 goto done;
3714 khttp_write(gw_trans->gw_req, buf, len - hdrlen);
3715 hdrlen = 0;
3716 } while (len != 0);
3717 done:
3718 free(in_repo_path);
3719 free(commit_id);
3720 free(obj_id);
3721 free(path);
3722 if (blob)
3723 got_object_blob_close(blob);
3724 if (repo)
3725 got_repo_close(repo);
3726 if (error == NULL && kerr != KCGI_OK)
3727 error = gw_kcgi_error(kerr);
3728 return error;
3731 static const struct got_error *
3732 gw_output_repo_tree(struct gw_trans *gw_trans)
3734 const struct got_error *error = NULL;
3735 struct got_repository *repo = NULL;
3736 struct got_object_id *tree_id = NULL, *commit_id = NULL;
3737 struct got_tree_object *tree = NULL;
3738 char *path = NULL, *in_repo_path = NULL;
3739 char *id_str = NULL;
3740 char *build_folder = NULL;
3741 char *href_blob = NULL, *href_blame = NULL;
3742 const char *class = NULL;
3743 int nentries, i, class_flip = 0;
3744 enum kcgi_err kerr = KCGI_OK;
3746 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3747 if (error)
3748 goto done;
3750 if (gw_trans->repo_folder != NULL)
3751 path = strdup(gw_trans->repo_folder);
3752 else {
3753 error = got_repo_map_path(&in_repo_path, repo,
3754 gw_trans->repo_path, 1);
3755 if (error)
3756 goto done;
3757 free(path);
3758 path = in_repo_path;
3761 if (gw_trans->commit == NULL) {
3762 struct got_reference *head_ref;
3763 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
3764 if (error)
3765 goto done;
3766 error = got_ref_resolve(&commit_id, repo, head_ref);
3767 if (error)
3768 goto done;
3769 got_ref_close(head_ref);
3771 } else {
3772 error = got_repo_match_object_id(&commit_id, NULL,
3773 gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
3774 if (error)
3775 goto done;
3778 error = got_object_id_str(&gw_trans->commit, commit_id);
3779 if (error)
3780 goto done;
3782 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
3783 if (error)
3784 goto done;
3786 error = got_object_open_as_tree(&tree, repo, tree_id);
3787 if (error)
3788 goto done;
3790 nentries = got_object_tree_get_nentries(tree);
3791 for (i = 0; i < nentries; i++) {
3792 struct got_tree_entry *te;
3793 const char *modestr = "";
3794 mode_t mode;
3796 te = got_object_tree_get_entry(tree, i);
3798 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
3799 if (error)
3800 goto done;
3802 mode = got_tree_entry_get_mode(te);
3803 if (got_object_tree_entry_is_submodule(te))
3804 modestr = "$";
3805 else if (S_ISLNK(mode))
3806 modestr = "@";
3807 else if (S_ISDIR(mode))
3808 modestr = "/";
3809 else if (mode & S_IXUSR)
3810 modestr = "*";
3812 if (class_flip == 0) {
3813 class = "back_lightgray";
3814 class_flip = 1;
3815 } else {
3816 class = "back_white";
3817 class_flip = 0;
3820 if (S_ISDIR(mode)) {
3821 if (asprintf(&build_folder, "%s/%s",
3822 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3823 got_tree_entry_get_name(te)) == -1) {
3824 error = got_error_from_errno(
3825 "asprintf");
3826 goto done;
3828 if (asprintf(&href_blob,
3829 "?path=%s&action=%s&commit=%s&folder=%s",
3830 gw_trans->repo_name, gw_trans->action_name,
3831 gw_trans->commit, build_folder) == -1) {
3832 error = got_error_from_errno("asprintf");
3833 goto done;
3836 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3837 KATTR_ID, "tree_wrapper", KATTR__MAX);
3838 if (kerr != KCGI_OK)
3839 goto done;
3840 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3841 KATTR_ID, "tree_line", KATTR_CLASS, class,
3842 KATTR__MAX);
3843 if (kerr != KCGI_OK)
3844 goto done;
3845 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3846 KATTR_HREF, href_blob, KATTR_CLASS,
3847 "diff_directory", KATTR__MAX);
3848 if (kerr != KCGI_OK)
3849 goto done;
3850 kerr = khtml_puts(gw_trans->gw_html_req,
3851 got_tree_entry_get_name(te));
3852 if (kerr != KCGI_OK)
3853 goto done;
3854 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3855 if (kerr != KCGI_OK)
3856 goto done;
3857 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3858 if (kerr != KCGI_OK)
3859 goto done;
3860 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3861 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
3862 KATTR__MAX);
3863 if (kerr != KCGI_OK)
3864 goto done;
3865 kerr = khtml_entity(gw_trans->gw_html_req,
3866 KENTITY_nbsp);
3867 if (kerr != KCGI_OK)
3868 goto done;
3869 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3870 if (kerr != KCGI_OK)
3871 goto done;
3872 } else {
3873 if (asprintf(&href_blob,
3874 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3875 gw_trans->repo_name, "blob", gw_trans->commit,
3876 got_tree_entry_get_name(te),
3877 gw_trans->repo_folder ?
3878 gw_trans->repo_folder : "") == -1) {
3879 error = got_error_from_errno("asprintf");
3880 goto done;
3882 if (asprintf(&href_blame,
3883 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3884 gw_trans->repo_name, "blame", gw_trans->commit,
3885 got_tree_entry_get_name(te),
3886 gw_trans->repo_folder ?
3887 gw_trans->repo_folder : "") == -1) {
3888 error = got_error_from_errno("asprintf");
3889 goto done;
3892 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3893 KATTR_ID, "tree_wrapper", KATTR__MAX);
3894 if (kerr != KCGI_OK)
3895 goto done;
3896 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3897 KATTR_ID, "tree_line", KATTR_CLASS, class,
3898 KATTR__MAX);
3899 if (kerr != KCGI_OK)
3900 goto done;
3901 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3902 KATTR_HREF, href_blob, KATTR__MAX);
3903 if (kerr != KCGI_OK)
3904 goto done;
3905 kerr = khtml_puts(gw_trans->gw_html_req,
3906 got_tree_entry_get_name(te));
3907 if (kerr != KCGI_OK)
3908 goto done;
3909 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3910 if (kerr != KCGI_OK)
3911 goto done;
3912 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3913 if (kerr != KCGI_OK)
3914 goto done;
3915 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3916 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
3917 KATTR__MAX);
3918 if (kerr != KCGI_OK)
3919 goto done;
3921 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3922 KATTR_HREF, href_blob, KATTR__MAX);
3923 if (kerr != KCGI_OK)
3924 goto done;
3925 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
3926 if (kerr != KCGI_OK)
3927 goto done;
3928 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3929 if (kerr != KCGI_OK)
3930 goto done;
3932 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3933 if (kerr != KCGI_OK)
3934 goto done;
3936 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3937 KATTR_HREF, href_blame, KATTR__MAX);
3938 if (kerr != KCGI_OK)
3939 goto done;
3940 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
3941 if (kerr != KCGI_OK)
3942 goto done;
3944 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3945 if (kerr != KCGI_OK)
3946 goto done;
3948 free(id_str);
3949 id_str = NULL;
3950 free(href_blob);
3951 href_blob = NULL;
3952 free(build_folder);
3953 build_folder = NULL;
3955 done:
3956 if (tree)
3957 got_object_tree_close(tree);
3958 if (repo)
3959 got_repo_close(repo);
3960 free(id_str);
3961 free(href_blob);
3962 free(href_blame);
3963 free(in_repo_path);
3964 free(tree_id);
3965 free(build_folder);
3966 if (error == NULL && kerr != KCGI_OK)
3967 error = gw_kcgi_error(kerr);
3968 return error;
3971 static const struct got_error *
3972 gw_output_repo_heads(struct gw_trans *gw_trans)
3974 const struct got_error *error = NULL;
3975 struct got_repository *repo = NULL;
3976 struct got_reflist_head refs;
3977 struct got_reflist_entry *re;
3978 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
3979 char *href_commits = NULL;
3980 enum kcgi_err kerr = KCGI_OK;
3982 SIMPLEQ_INIT(&refs);
3984 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3985 if (error)
3986 goto done;
3988 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
3989 NULL);
3990 if (error)
3991 goto done;
3993 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3994 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
3995 if (kerr != KCGI_OK)
3996 goto done;
3997 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3998 KATTR_ID, "summary_heads_title", KATTR__MAX);
3999 if (kerr != KCGI_OK)
4000 goto done;
4001 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
4002 if (kerr != KCGI_OK)
4003 goto done;
4004 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4005 if (kerr != KCGI_OK)
4006 goto done;
4007 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4008 KATTR_ID, "summary_heads_content", KATTR__MAX);
4009 if (kerr != KCGI_OK)
4010 goto done;
4012 SIMPLEQ_FOREACH(re, &refs, entry) {
4013 char *refname;
4015 refname = strdup(got_ref_get_name(re->ref));
4016 if (refname == NULL) {
4017 error = got_error_from_errno("got_ref_to_str");
4018 goto done;
4021 if (strncmp(refname, "refs/heads/", 11) != 0) {
4022 free(refname);
4023 continue;
4026 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
4027 refname, TM_DIFF);
4028 if (error)
4029 goto done;
4031 if (strncmp(refname, "refs/heads/", 11) == 0)
4032 refname += 11;
4034 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4035 KATTR_ID, "heads_wrapper", KATTR__MAX);
4036 if (kerr != KCGI_OK)
4037 goto done;
4038 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4039 KATTR_ID, "heads_age", KATTR__MAX);
4040 if (kerr != KCGI_OK)
4041 goto done;
4042 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
4043 if (kerr != KCGI_OK)
4044 goto done;
4045 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4046 if (kerr != KCGI_OK)
4047 goto done;
4048 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4049 KATTR_ID, "heads_space", KATTR__MAX);
4050 if (kerr != KCGI_OK)
4051 goto done;
4052 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
4053 if (kerr != KCGI_OK)
4054 goto done;
4055 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4056 if (kerr != KCGI_OK)
4057 goto done;
4058 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4059 KATTR_ID, "head", KATTR__MAX);
4060 if (kerr != KCGI_OK)
4061 goto done;
4062 if (asprintf(&href_summary,
4063 "?path=%s&action=summary&headref=%s",
4064 gw_trans->repo_name, refname) == -1) {
4065 error = got_error_from_errno("asprintf");
4066 goto done;
4068 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4069 href_summary, KATTR__MAX);
4070 kerr = khtml_puts(gw_trans->gw_html_req, refname);
4071 if (kerr != KCGI_OK)
4072 goto done;
4073 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4074 if (kerr != KCGI_OK)
4075 goto done;
4077 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4078 "navs_wrapper", KATTR__MAX);
4079 if (kerr != KCGI_OK)
4080 goto done;
4081 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4082 "navs", KATTR__MAX);
4083 if (kerr != KCGI_OK)
4084 goto done;
4086 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4087 href_summary, KATTR__MAX);
4088 if (kerr != KCGI_OK)
4089 goto done;
4090 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4091 if (kerr != KCGI_OK)
4092 goto done;
4093 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4094 if (kerr != KCGI_OK)
4095 goto done;
4097 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4098 if (kerr != KCGI_OK)
4099 goto done;
4100 if (asprintf(&href_briefs, "?path=%s&action=briefs&headref=%s",
4101 gw_trans->repo_name, refname) == -1) {
4102 error = got_error_from_errno("asprintf");
4103 goto done;
4105 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4106 href_briefs, KATTR__MAX);
4107 if (kerr != KCGI_OK)
4108 goto done;
4109 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4110 if (kerr != KCGI_OK)
4111 goto done;
4112 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4113 if (kerr != KCGI_OK)
4114 goto done;
4116 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4117 if (kerr != KCGI_OK)
4118 goto done;
4120 if (asprintf(&href_commits,
4121 "?path=%s&action=commits&headref=%s",
4122 gw_trans->repo_name, refname) == -1) {
4123 error = got_error_from_errno("asprintf");
4124 goto done;
4126 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4127 href_commits, KATTR__MAX);
4128 if (kerr != KCGI_OK)
4129 goto done;
4130 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4131 if (kerr != KCGI_OK)
4132 goto done;
4133 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4134 if (kerr != KCGI_OK)
4135 goto done;
4137 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4138 "dotted_line", KATTR__MAX);
4139 if (kerr != KCGI_OK)
4140 goto done;
4141 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4142 if (kerr != KCGI_OK)
4143 goto done;
4144 break;
4145 free(href_summary);
4146 href_summary = NULL;
4147 free(href_briefs);
4148 href_briefs = NULL;
4149 free(href_commits);
4150 href_commits = NULL;
4152 done:
4153 got_ref_list_free(&refs);
4154 free(href_summary);
4155 free(href_briefs);
4156 free(href_commits);
4157 if (repo)
4158 got_repo_close(repo);
4159 return error;
4162 static const struct got_error *
4163 gw_output_site_link(struct gw_trans *gw_trans)
4165 const struct got_error *error = NULL;
4166 char *href_summary = NULL;
4167 enum kcgi_err kerr = KCGI_OK;
4169 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4170 "site_link", KATTR__MAX);
4171 if (kerr != KCGI_OK)
4172 goto done;
4173 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4174 KATTR__MAX);
4175 if (kerr != KCGI_OK)
4176 goto done;
4177 kerr = khtml_puts(gw_trans->gw_html_req,
4178 gw_trans->gw_conf->got_site_link);
4179 if (kerr != KCGI_OK)
4180 goto done;
4181 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4182 if (kerr != KCGI_OK)
4183 goto done;
4185 if (gw_trans->repo_name != NULL) {
4186 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4187 if (kerr != KCGI_OK)
4188 goto done;
4189 if (asprintf(&href_summary, "?path=%s&action=summary",
4190 gw_trans->repo_name) == -1)
4191 goto done;
4192 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4193 href_summary, KATTR__MAX);
4194 if (kerr != KCGI_OK)
4195 goto done;
4196 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4197 if (kerr != KCGI_OK)
4198 goto done;
4199 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4200 if (kerr != KCGI_OK)
4201 goto done;
4202 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4203 if (kerr != KCGI_OK)
4204 goto done;
4205 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->action_name);
4206 if (kerr != KCGI_OK)
4207 goto done;
4210 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4211 if (kerr != KCGI_OK)
4212 goto done;
4213 done:
4214 free(href_summary);
4215 return error;
4218 static const struct got_error *
4219 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4221 const struct got_error *error = NULL;
4222 char *color = NULL;
4223 enum kcgi_err kerr = KCGI_OK;
4225 if (strncmp(buf, "-", 1) == 0)
4226 color = "diff_minus";
4227 else if (strncmp(buf, "+", 1) == 0)
4228 color = "diff_plus";
4229 else if (strncmp(buf, "@@", 2) == 0)
4230 color = "diff_chunk_header";
4231 else if (strncmp(buf, "@@", 2) == 0)
4232 color = "diff_chunk_header";
4233 else if (strncmp(buf, "commit +", 8) == 0)
4234 color = "diff_meta";
4235 else if (strncmp(buf, "commit -", 8) == 0)
4236 color = "diff_meta";
4237 else if (strncmp(buf, "blob +", 6) == 0)
4238 color = "diff_meta";
4239 else if (strncmp(buf, "blob -", 6) == 0)
4240 color = "diff_meta";
4241 else if (strncmp(buf, "file +", 6) == 0)
4242 color = "diff_meta";
4243 else if (strncmp(buf, "file -", 6) == 0)
4244 color = "diff_meta";
4245 else if (strncmp(buf, "from:", 5) == 0)
4246 color = "diff_author";
4247 else if (strncmp(buf, "via:", 4) == 0)
4248 color = "diff_author";
4249 else if (strncmp(buf, "date:", 5) == 0)
4250 color = "diff_date";
4251 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4252 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4253 if (error == NULL && kerr != KCGI_OK)
4254 error = gw_kcgi_error(kerr);
4255 return error;
4258 int
4259 main(int argc, char *argv[])
4261 const struct got_error *error = NULL;
4262 struct gw_trans *gw_trans;
4263 struct gw_dir *dir = NULL, *tdir;
4264 const char *page = "index";
4265 int gw_malloc = 1;
4266 enum kcgi_err kerr;
4268 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4269 errx(1, "malloc");
4271 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4272 errx(1, "malloc");
4274 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4275 errx(1, "malloc");
4277 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4278 errx(1, "malloc");
4280 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4281 if (kerr != KCGI_OK) {
4282 error = gw_kcgi_error(kerr);
4283 goto done;
4286 if ((gw_trans->gw_conf =
4287 malloc(sizeof(struct gotweb_conf))) == NULL) {
4288 gw_malloc = 0;
4289 error = got_error_from_errno("malloc");
4290 goto done;
4293 TAILQ_INIT(&gw_trans->gw_dirs);
4294 TAILQ_INIT(&gw_trans->gw_headers);
4296 gw_trans->page = 0;
4297 gw_trans->repos_total = 0;
4298 gw_trans->repo_path = NULL;
4299 gw_trans->commit = NULL;
4300 gw_trans->headref = strdup(GOT_REF_HEAD);
4301 gw_trans->mime = KMIME_TEXT_HTML;
4302 gw_trans->gw_tmpl->key = gw_templs;
4303 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4304 gw_trans->gw_tmpl->arg = gw_trans;
4305 gw_trans->gw_tmpl->cb = gw_template;
4306 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
4307 if (error)
4308 goto done;
4310 error = gw_parse_querystring(gw_trans);
4311 if (error)
4312 goto done;
4314 if (gw_trans->action == GW_BLOB)
4315 error = gw_blob(gw_trans);
4316 else
4317 error = gw_display_index(gw_trans);
4318 done:
4319 if (error) {
4320 gw_trans->mime = KMIME_TEXT_PLAIN;
4321 gw_trans->action = GW_ERR;
4322 gw_display_error(gw_trans, error);
4324 if (gw_malloc) {
4325 free(gw_trans->gw_conf->got_repos_path);
4326 free(gw_trans->gw_conf->got_site_name);
4327 free(gw_trans->gw_conf->got_site_owner);
4328 free(gw_trans->gw_conf->got_site_link);
4329 free(gw_trans->gw_conf->got_logo);
4330 free(gw_trans->gw_conf->got_logo_url);
4331 free(gw_trans->gw_conf);
4332 free(gw_trans->commit);
4333 free(gw_trans->repo_path);
4334 free(gw_trans->repo_name);
4335 free(gw_trans->repo_file);
4336 free(gw_trans->action_name);
4337 free(gw_trans->headref);
4339 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4340 free(dir->name);
4341 free(dir->description);
4342 free(dir->age);
4343 free(dir->url);
4344 free(dir->path);
4345 free(dir);
4350 khttp_free(gw_trans->gw_req);
4351 return 0;