Blob


1 /*
2 * Copyright (c) 2019, 2020 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/queue.h>
19 #include <sys/stat.h>
20 #include <sys/types.h>
22 #include <ctype.h>
23 #include <dirent.h>
24 #include <err.h>
25 #include <errno.h>
26 #include <regex.h>
27 #include <stdarg.h>
28 #include <stdint.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
34 #include <got_object.h>
35 #include <got_reference.h>
36 #include <got_repository.h>
37 #include <got_path.h>
38 #include <got_cancel.h>
39 #include <got_worktree.h>
40 #include <got_diff.h>
41 #include <got_commit_graph.h>
42 #include <got_blame.h>
43 #include <got_privsep.h>
44 #include <got_opentemp.h>
46 #include <kcgi.h>
47 #include <kcgihtml.h>
49 #include "gotweb.h"
51 #ifndef nitems
52 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
53 #endif
55 struct gw_trans {
56 TAILQ_HEAD(headers, gw_header) gw_headers;
57 TAILQ_HEAD(dirs, gw_dir) gw_dirs;
58 struct got_repository *repo;
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 const struct got_error *error;
65 const char *repo_name;
66 char *repo_path;
67 char *commit_id;
68 char *next_id;
69 char *next_prev_id;
70 char *prev_id;
71 char *prev_prev_id;
72 const char *repo_file;
73 char *repo_folder;
74 const char *headref;
75 unsigned int action;
76 unsigned int page;
77 unsigned int repos_total;
78 enum kmime mime;
79 };
81 struct gw_header {
82 TAILQ_ENTRY(gw_header) entry;
83 struct got_reflist_head refs;
84 char *path;
86 char *refs_str;
87 char *commit_id; /* id_str1 */
88 char *parent_id; /* id_str2 */
89 char *tree_id;
90 char *author;
91 char *committer;
92 char *commit_msg;
93 time_t committer_time;
94 };
96 struct gw_dir {
97 TAILQ_ENTRY(gw_dir) entry;
98 char *name;
99 char *owner;
100 char *description;
101 char *url;
102 char *age;
103 char *path;
104 };
106 enum gw_key {
107 KEY_ACTION,
108 KEY_COMMIT_ID,
109 KEY_FILE,
110 KEY_FOLDER,
111 KEY_HEADREF,
112 KEY_PAGE,
113 KEY_PATH,
114 KEY_PREV_ID,
115 KEY_PREV_PREV_ID,
116 KEY__ZMAX
117 };
119 enum gw_tmpl {
120 TEMPL_CONTENT,
121 TEMPL_HEAD,
122 TEMPL_HEADER,
123 TEMPL_SEARCH,
124 TEMPL_SITEPATH,
125 TEMPL_SITEOWNER,
126 TEMPL_TITLE,
127 TEMPL__MAX
128 };
130 enum gw_ref_tm {
131 TM_DIFF,
132 TM_LONG,
133 };
135 enum gw_tags_type {
136 TAGBRIEF,
137 TAGFULL,
138 };
140 static const char *const gw_templs[TEMPL__MAX] = {
141 "content",
142 "head",
143 "header",
144 "search",
145 "sitepath",
146 "siteowner",
147 "title",
148 };
150 static const struct kvalid gw_keys[KEY__ZMAX] = {
151 { kvalid_stringne, "action" },
152 { kvalid_stringne, "commit" },
153 { kvalid_stringne, "file" },
154 { kvalid_stringne, "folder" },
155 { kvalid_stringne, "headref" },
156 { kvalid_int, "page" },
157 { kvalid_stringne, "path" },
158 { kvalid_stringne, "prev" },
159 { kvalid_stringne, "prev_prev" },
160 };
162 static struct gw_header *gw_init_header(void);
164 static void gw_free_header(struct gw_header *);
166 static int gw_template(size_t, void *);
168 static const struct got_error *gw_error(struct gw_trans *);
169 static const struct got_error *gw_init_gw_dir(struct gw_dir **, const char *);
170 static const struct got_error *gw_get_repo_description(char **,
171 struct gw_trans *, char *);
172 static const struct got_error *gw_get_repo_owner(char **, struct gw_trans *,
173 char *);
174 static const struct got_error *gw_get_time_str(char **, time_t, int);
175 static const struct got_error *gw_get_repo_age(char **, struct gw_trans *,
176 char *, const char *, int);
177 static const struct got_error *gw_output_file_blame(struct gw_trans *);
178 static const struct got_error *gw_output_blob_buf(struct gw_trans *);
179 static const struct got_error *gw_output_repo_tree(struct gw_trans *);
180 static const struct got_error *gw_output_diff(struct gw_trans *,
181 struct gw_header *);
182 static const struct got_error *gw_output_repo_tags(struct gw_trans *,
183 struct gw_header *, int, int);
184 static const struct got_error *gw_output_repo_heads(struct gw_trans *);
185 static const struct got_error *gw_output_site_link(struct gw_trans *);
186 static const struct got_error *gw_get_clone_url(char **, struct gw_trans *,
187 char *);
188 static const struct got_error *gw_colordiff_line(struct gw_trans *, char *);
190 static const struct got_error *gw_gen_commit_header(struct gw_trans *, char *,
191 char*);
192 static const struct got_error *gw_gen_diff_header(struct gw_trans *, char *,
193 char*);
194 static const struct got_error *gw_gen_author_header(struct gw_trans *,
195 const char *);
196 static const struct got_error *gw_gen_age_header(struct gw_trans *,
197 const char *);
198 static const struct got_error *gw_gen_committer_header(struct gw_trans *,
199 const char *);
200 static const struct got_error *gw_gen_commit_msg_header(struct gw_trans*,
201 char *);
202 static const struct got_error *gw_gen_tree_header(struct gw_trans *, char *);
203 static const struct got_error *gw_display_open(struct gw_trans *, enum khttp,
204 enum kmime);
205 static const struct got_error *gw_display_index(struct gw_trans *);
206 static const struct got_error *gw_get_header(struct gw_trans *,
207 struct gw_header *, int);
208 static const struct got_error *gw_get_commits(struct gw_trans *,
209 struct gw_header *, int,
210 struct got_object_id *);
211 static const struct got_error *gw_get_commit(struct gw_trans *,
212 struct gw_header *,
213 struct got_commit_object *,
214 struct got_object_id *);
215 static const struct got_error *gw_apply_unveil(const char *);
216 static const struct got_error *gw_blame_cb(void *, int, int,
217 struct got_object_id *);
218 static const struct got_error *gw_load_got_paths(struct gw_trans *);
219 static const struct got_error *gw_load_got_path(struct gw_trans *,
220 struct gw_dir *);
221 static const struct got_error *gw_parse_querystring(struct gw_trans *);
222 static const struct got_error *gw_blame(struct gw_trans *);
223 static const struct got_error *gw_blob(struct gw_trans *);
224 static const struct got_error *gw_diff(struct gw_trans *);
225 static const struct got_error *gw_index(struct gw_trans *);
226 static const struct got_error *gw_commits(struct gw_trans *);
227 static const struct got_error *gw_briefs(struct gw_trans *);
228 static const struct got_error *gw_summary(struct gw_trans *);
229 static const struct got_error *gw_tree(struct gw_trans *);
230 static const struct got_error *gw_tag(struct gw_trans *);
231 static const struct got_error *gw_tags(struct gw_trans *);
233 struct gw_query_action {
234 unsigned int func_id;
235 const char *func_name;
236 const struct got_error *(*func_main)(struct gw_trans *);
237 char *template;
238 };
240 enum gw_query_actions {
241 GW_BLAME,
242 GW_BLOB,
243 GW_BRIEFS,
244 GW_COMMITS,
245 GW_DIFF,
246 GW_ERR,
247 GW_INDEX,
248 GW_SUMMARY,
249 GW_TAG,
250 GW_TAGS,
251 GW_TREE,
252 };
254 static struct gw_query_action gw_query_funcs[] = {
255 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
256 { GW_BLOB, "blob", NULL, NULL },
257 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
258 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
259 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
260 { GW_ERR, "error", gw_error, "gw_tmpl/err.tmpl" },
261 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
262 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
263 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
264 { GW_TAGS, "tags", gw_tags, "gw_tmpl/tags.tmpl" },
265 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
266 };
268 static const char *
269 gw_get_action_name(struct gw_trans *gw_trans)
271 return gw_query_funcs[gw_trans->action].func_name;
274 static const struct got_error *
275 gw_kcgi_error(enum kcgi_err kerr)
277 if (kerr == KCGI_OK)
278 return NULL;
280 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
281 return got_error(GOT_ERR_CANCELLED);
283 if (kerr == KCGI_ENOMEM)
284 return got_error_set_errno(ENOMEM,
285 kcgi_strerror(kerr));
287 if (kerr == KCGI_ENFILE)
288 return got_error_set_errno(ENFILE,
289 kcgi_strerror(kerr));
291 if (kerr == KCGI_EAGAIN)
292 return got_error_set_errno(EAGAIN,
293 kcgi_strerror(kerr));
295 if (kerr == KCGI_FORM)
296 return got_error_msg(GOT_ERR_IO,
297 kcgi_strerror(kerr));
299 return got_error_from_errno(kcgi_strerror(kerr));
302 static const struct got_error *
303 gw_apply_unveil(const char *repo_path)
305 const struct got_error *err;
307 if (repo_path && unveil(repo_path, "r") != 0)
308 return got_error_from_errno2("unveil", repo_path);
310 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
311 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
313 err = got_privsep_unveil_exec_helpers();
314 if (err != NULL)
315 return err;
317 if (unveil(NULL, NULL) != 0)
318 return got_error_from_errno("unveil");
320 return NULL;
323 static int
324 isbinary(const uint8_t *buf, size_t n)
326 size_t i;
328 for (i = 0; i < n; i++)
329 if (buf[i] == 0)
330 return 1;
331 return 0;
334 static const struct got_error *
335 gw_blame(struct gw_trans *gw_trans)
337 const struct got_error *error = NULL;
338 struct gw_header *header = NULL;
339 char *age = NULL;
340 enum kcgi_err kerr = KCGI_OK;
342 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
343 NULL) == -1)
344 return got_error_from_errno("pledge");
346 if ((header = gw_init_header()) == NULL)
347 return got_error_from_errno("malloc");
349 error = gw_apply_unveil(gw_trans->gw_dir->path);
350 if (error)
351 goto done;
353 /* check querystring */
354 if (gw_trans->repo_file == NULL) {
355 error = got_error_msg(GOT_ERR_QUERYSTRING,
356 "file required in querystring");
357 goto done;
359 if (gw_trans->commit_id == NULL) {
360 error = got_error_msg(GOT_ERR_QUERYSTRING,
361 "commit required in querystring");
362 goto done;
365 error = gw_get_header(gw_trans, header, 1);
366 if (error)
367 goto done;
368 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
369 "blame_header_wrapper", KATTR__MAX);
370 if (kerr != KCGI_OK)
371 goto done;
372 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
373 "blame_header", KATTR__MAX);
374 if (kerr != KCGI_OK)
375 goto done;
376 error = gw_get_time_str(&age, header->committer_time,
377 TM_LONG);
378 if (error)
379 goto done;
380 error = gw_gen_age_header(gw_trans, age ?age : "");
381 if (error)
382 goto done;
383 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
384 if (error)
385 goto done;
386 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
387 if (kerr != KCGI_OK)
388 goto done;
389 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
390 "dotted_line", KATTR__MAX);
391 if (kerr != KCGI_OK)
392 goto done;
393 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
394 if (kerr != KCGI_OK)
395 goto done;
397 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
398 "blame", KATTR__MAX);
399 if (kerr != KCGI_OK)
400 goto done;
401 error = gw_output_file_blame(gw_trans);
402 if (error)
403 goto done;
404 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
405 done:
406 gw_free_header(header);
407 if (error == NULL && kerr != KCGI_OK)
408 error = gw_kcgi_error(kerr);
409 return error;
412 static const struct got_error *
413 gw_blob(struct gw_trans *gw_trans)
415 const struct got_error *error = NULL, *err = NULL;
416 struct gw_header *header = NULL;
417 enum kcgi_err kerr = KCGI_OK;
419 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
420 NULL) == -1)
421 return got_error_from_errno("pledge");
423 if ((header = gw_init_header()) == NULL)
424 return got_error_from_errno("malloc");
426 error = gw_apply_unveil(gw_trans->gw_dir->path);
427 if (error)
428 goto done;
430 /* check querystring */
431 if (gw_trans->repo_file == NULL) {
432 error = got_error_msg(GOT_ERR_QUERYSTRING,
433 "file required in querystring");
434 goto done;
436 if (gw_trans->commit_id == NULL) {
437 error = got_error_msg(GOT_ERR_QUERYSTRING,
438 "commit required in querystring");
439 goto done;
441 error = gw_get_header(gw_trans, header, 1);
442 if (error)
443 goto done;
445 error = gw_output_blob_buf(gw_trans);
446 done:
448 if (error) {
449 gw_trans->mime = KMIME_TEXT_PLAIN;
450 err = gw_display_index(gw_trans);
451 if (err) {
452 error = err;
453 goto errored;
455 kerr = khttp_puts(gw_trans->gw_req, error->msg);
457 errored:
458 gw_free_header(header);
459 if (error == NULL && kerr != KCGI_OK)
460 error = gw_kcgi_error(kerr);
461 return error;
464 static const struct got_error *
465 gw_diff(struct gw_trans *gw_trans)
467 const struct got_error *error = NULL;
468 struct gw_header *header = NULL;
469 char *age = NULL;
470 enum kcgi_err kerr = KCGI_OK;
472 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
473 NULL) == -1)
474 return got_error_from_errno("pledge");
476 if ((header = gw_init_header()) == NULL)
477 return got_error_from_errno("malloc");
479 error = gw_apply_unveil(gw_trans->gw_dir->path);
480 if (error)
481 goto done;
483 error = gw_get_header(gw_trans, header, 1);
484 if (error)
485 goto done;
487 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
488 "diff_header_wrapper", KATTR__MAX);
489 if (kerr != KCGI_OK)
490 goto done;
491 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
492 "diff_header", KATTR__MAX);
493 if (kerr != KCGI_OK)
494 goto done;
495 error = gw_gen_diff_header(gw_trans, header->parent_id,
496 header->commit_id);
497 if (error)
498 goto done;
499 error = gw_gen_commit_header(gw_trans, header->commit_id,
500 header->refs_str);
501 if (error)
502 goto done;
503 error = gw_gen_tree_header(gw_trans, header->tree_id);
504 if (error)
505 goto done;
506 error = gw_gen_author_header(gw_trans, header->author);
507 if (error)
508 goto done;
509 error = gw_gen_committer_header(gw_trans, header->author);
510 if (error)
511 goto done;
512 error = gw_get_time_str(&age, header->committer_time,
513 TM_LONG);
514 if (error)
515 goto done;
516 error = gw_gen_age_header(gw_trans, age ?age : "");
517 if (error)
518 goto done;
519 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
520 if (error)
521 goto done;
522 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
523 if (kerr != KCGI_OK)
524 goto done;
525 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
526 "dotted_line", KATTR__MAX);
527 if (kerr != KCGI_OK)
528 goto done;
529 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
530 if (kerr != KCGI_OK)
531 goto done;
533 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
534 "diff", KATTR__MAX);
535 if (kerr != KCGI_OK)
536 goto done;
537 error = gw_output_diff(gw_trans, header);
538 if (error)
539 goto done;
541 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
542 done:
543 gw_free_header(header);
544 free(age);
545 if (error == NULL && kerr != KCGI_OK)
546 error = gw_kcgi_error(kerr);
547 return error;
550 static const struct got_error *
551 gw_index(struct gw_trans *gw_trans)
553 const struct got_error *error = NULL;
554 struct gw_dir *gw_dir = NULL;
555 char *href_next = NULL, *href_prev = NULL, *href_summary = NULL;
556 char *href_briefs = NULL, *href_commits = NULL, *href_tree = NULL;
557 char *href_tags = NULL;
558 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
559 enum kcgi_err kerr;
561 if (pledge("stdio rpath proc exec sendfd unveil",
562 NULL) == -1) {
563 error = got_error_from_errno("pledge");
564 return error;
567 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path);
568 if (error)
569 return error;
571 error = gw_load_got_paths(gw_trans);
572 if (error)
573 return error;
575 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
576 "index_header", KATTR__MAX);
577 if (kerr != KCGI_OK)
578 return gw_kcgi_error(kerr);
579 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
580 "index_header_project", KATTR__MAX);
581 if (kerr != KCGI_OK)
582 return gw_kcgi_error(kerr);
583 kerr = khtml_puts(gw_trans->gw_html_req, "Project");
584 if (kerr != KCGI_OK)
585 return gw_kcgi_error(kerr);
586 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
587 if (kerr != KCGI_OK)
588 return gw_kcgi_error(kerr);
590 if (gw_trans->gw_conf->got_show_repo_description) {
591 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
592 "index_header_description", KATTR__MAX);
593 if (kerr != KCGI_OK)
594 return gw_kcgi_error(kerr);
595 kerr = khtml_puts(gw_trans->gw_html_req, "Description");
596 if (kerr != KCGI_OK)
597 return gw_kcgi_error(kerr);
598 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
599 if (kerr != KCGI_OK)
600 return gw_kcgi_error(kerr);
603 if (gw_trans->gw_conf->got_show_repo_owner) {
604 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
605 "index_header_owner", KATTR__MAX);
606 if (kerr != KCGI_OK)
607 return gw_kcgi_error(kerr);
608 kerr = khtml_puts(gw_trans->gw_html_req, "Owner");
609 if (kerr != KCGI_OK)
610 return gw_kcgi_error(kerr);
611 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
612 if (kerr != KCGI_OK)
613 return gw_kcgi_error(kerr);
616 if (gw_trans->gw_conf->got_show_repo_age) {
617 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
618 "index_header_age", KATTR__MAX);
619 if (kerr != KCGI_OK)
620 return gw_kcgi_error(kerr);
621 kerr = khtml_puts(gw_trans->gw_html_req, "Last Change");
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);
629 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
630 if (kerr != KCGI_OK)
631 return gw_kcgi_error(kerr);
633 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
634 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
635 "index_wrapper", KATTR__MAX);
636 if (kerr != KCGI_OK)
637 return gw_kcgi_error(kerr);
638 kerr = khtml_puts(gw_trans->gw_html_req,
639 "No repositories found in ");
640 if (kerr != KCGI_OK)
641 return gw_kcgi_error(kerr);
642 kerr = khtml_puts(gw_trans->gw_html_req,
643 gw_trans->gw_conf->got_repos_path);
644 if (kerr != KCGI_OK)
645 return gw_kcgi_error(kerr);
646 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
647 if (kerr != KCGI_OK)
648 return gw_kcgi_error(kerr);
649 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
650 "dotted_line", KATTR__MAX);
651 if (kerr != KCGI_OK)
652 return gw_kcgi_error(kerr);
653 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
654 if (kerr != KCGI_OK)
655 return gw_kcgi_error(kerr);
656 return error;
659 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
660 dir_c++;
662 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
663 if (gw_trans->page > 0 && (gw_trans->page *
664 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
665 prev_disp++;
666 continue;
669 prev_disp++;
671 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
672 "index_wrapper", KATTR__MAX);
673 if (kerr != KCGI_OK)
674 goto done;
676 if (asprintf(&href_summary, "?path=%s&action=summary",
677 gw_dir->name) == -1) {
678 error = got_error_from_errno("asprintf");
679 goto done;
681 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
682 "index_project", KATTR__MAX);
683 if (kerr != KCGI_OK)
684 goto done;
685 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
686 href_summary, KATTR__MAX);
687 if (kerr != KCGI_OK)
688 goto done;
689 kerr = khtml_puts(gw_trans->gw_html_req, gw_dir->name);
690 if (kerr != KCGI_OK)
691 goto done;
692 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
693 if (kerr != KCGI_OK)
694 goto done;
695 if (gw_trans->gw_conf->got_show_repo_description) {
696 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
697 KATTR_ID, "index_project_description", KATTR__MAX);
698 if (kerr != KCGI_OK)
699 goto done;
700 kerr = khtml_puts(gw_trans->gw_html_req,
701 gw_dir->description ? gw_dir->description : "");
702 if (kerr != KCGI_OK)
703 goto done;
704 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
705 if (kerr != KCGI_OK)
706 goto done;
708 if (gw_trans->gw_conf->got_show_repo_owner) {
709 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
710 KATTR_ID, "index_project_owner", KATTR__MAX);
711 if (kerr != KCGI_OK)
712 goto done;
713 kerr = khtml_puts(gw_trans->gw_html_req,
714 gw_dir->owner ? gw_dir->owner : "");
715 if (kerr != KCGI_OK)
716 goto done;
717 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
718 if (kerr != KCGI_OK)
719 goto done;
721 if (gw_trans->gw_conf->got_show_repo_age) {
722 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
723 KATTR_ID, "index_project_age", KATTR__MAX);
724 if (kerr != KCGI_OK)
725 goto done;
726 kerr = khtml_puts(gw_trans->gw_html_req,
727 gw_dir->age ? gw_dir->age : "");
728 if (kerr != KCGI_OK)
729 goto done;
730 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
731 if (kerr != KCGI_OK)
732 goto done;
735 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
736 "navs_wrapper", KATTR__MAX);
737 if (kerr != KCGI_OK)
738 goto done;
739 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
740 "navs", KATTR__MAX);
741 if (kerr != KCGI_OK)
742 goto done;
744 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
745 href_summary, KATTR__MAX);
746 if (kerr != KCGI_OK)
747 goto done;
748 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
749 if (kerr != KCGI_OK)
750 goto done;
751 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
752 if (kerr != KCGI_OK)
753 goto done;
755 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
756 if (kerr != KCGI_OK)
757 goto done;
759 if (asprintf(&href_briefs, "?path=%s&action=briefs",
760 gw_dir->name) == -1) {
761 error = got_error_from_errno("asprintf");
762 goto done;
764 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
765 href_briefs, KATTR__MAX);
766 if (kerr != KCGI_OK)
767 goto done;
768 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
769 if (kerr != KCGI_OK)
770 goto done;
771 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
772 if (kerr != KCGI_OK)
773 error = gw_kcgi_error(kerr);
775 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
776 if (kerr != KCGI_OK)
777 goto done;
779 if (asprintf(&href_commits, "?path=%s&action=commits",
780 gw_dir->name) == -1) {
781 error = got_error_from_errno("asprintf");
782 goto done;
784 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
785 href_commits, KATTR__MAX);
786 if (kerr != KCGI_OK)
787 goto done;
788 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
789 if (kerr != KCGI_OK)
790 goto done;
791 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
792 if (kerr != KCGI_OK)
793 goto done;
795 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
796 if (kerr != KCGI_OK)
797 goto done;
800 if (asprintf(&href_tags, "?path=%s&action=tags",
801 gw_dir->name) == -1) {
802 error = got_error_from_errno("asprintf");
803 goto done;
805 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
806 href_tags, KATTR__MAX);
807 if (kerr != KCGI_OK)
808 goto done;
809 kerr = khtml_puts(gw_trans->gw_html_req, "tags");
810 if (kerr != KCGI_OK)
811 goto done;
812 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
813 if (kerr != KCGI_OK)
814 goto done;
816 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
817 if (kerr != KCGI_OK)
818 goto done;
820 if (asprintf(&href_tree, "?path=%s&action=tree",
821 gw_dir->name) == -1) {
822 error = got_error_from_errno("asprintf");
823 goto done;
825 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
826 href_tree, KATTR__MAX);
827 if (kerr != KCGI_OK)
828 goto done;
829 kerr = khtml_puts(gw_trans->gw_html_req, "tree");
830 if (kerr != KCGI_OK)
831 goto done;
833 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
834 if (kerr != KCGI_OK)
835 goto done;
836 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
837 "dotted_line", KATTR__MAX);
838 if (kerr != KCGI_OK)
839 goto done;
840 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
841 if (kerr != KCGI_OK)
842 goto done;
844 free(href_summary);
845 href_summary = NULL;
846 free(href_briefs);
847 href_briefs = NULL;
848 free(href_commits);
849 href_commits = NULL;
850 free(href_tags);
851 href_tags = NULL;
852 free(href_tree);
853 href_tree = NULL;
855 if (gw_trans->gw_conf->got_max_repos_display == 0)
856 continue;
858 if ((next_disp == gw_trans->gw_conf->got_max_repos_display) ||
859 ((gw_trans->gw_conf->got_max_repos_display > 0) &&
860 (gw_trans->page > 0) &&
861 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
862 prev_disp == gw_trans->repos_total))) {
863 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
864 KATTR_ID, "np_wrapper", KATTR__MAX);
865 if (kerr != KCGI_OK)
866 goto done;
867 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
868 KATTR_ID, "nav_prev", KATTR__MAX);
869 if (kerr != KCGI_OK)
870 goto done;
873 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
874 (gw_trans->page > 0) &&
875 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
876 prev_disp == gw_trans->repos_total)) {
877 if (asprintf(&href_prev, "?page=%d",
878 gw_trans->page - 1) == -1) {
879 error = got_error_from_errno("asprintf");
880 goto done;
882 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
883 KATTR_HREF, href_prev, KATTR__MAX);
884 if (kerr != KCGI_OK)
885 goto done;
886 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
887 if (kerr != KCGI_OK)
888 goto done;
889 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
890 if (kerr != KCGI_OK)
891 goto done;
894 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
895 if (kerr != KCGI_OK)
896 return gw_kcgi_error(kerr);
898 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
899 next_disp == gw_trans->gw_conf->got_max_repos_display &&
900 dir_c != (gw_trans->page + 1) *
901 gw_trans->gw_conf->got_max_repos_display) {
902 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
903 KATTR_ID, "nav_next", KATTR__MAX);
904 if (kerr != KCGI_OK)
905 goto done;
906 if (asprintf(&href_next, "?page=%d",
907 gw_trans->page + 1) == -1) {
908 error = got_error_from_errno("calloc");
909 goto done;
911 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
912 KATTR_HREF, href_next, KATTR__MAX);
913 if (kerr != KCGI_OK)
914 goto done;
915 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
916 if (kerr != KCGI_OK)
917 goto done;
918 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
919 if (kerr != KCGI_OK)
920 goto done;
921 next_disp = 0;
922 break;
925 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
926 (gw_trans->page > 0) &&
927 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
928 prev_disp == gw_trans->repos_total)) {
929 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
930 if (kerr != KCGI_OK)
931 goto done;
933 next_disp++;
935 done:
936 free(href_prev);
937 free(href_next);
938 free(href_summary);
939 free(href_briefs);
940 free(href_commits);
941 free(href_tags);
942 free(href_tree);
943 if (error == NULL && kerr != KCGI_OK)
944 error = gw_kcgi_error(kerr);
945 return error;
948 static const struct got_error *
949 gw_commits(struct gw_trans *gw_trans)
951 const struct got_error *error = NULL;
952 struct gw_header *header = NULL, *n_header = NULL;
953 char *age = NULL, *href_diff = NULL, *href_blob = NULL;
954 char *href_prev = NULL, *href_next = NULL;
955 enum kcgi_err kerr = KCGI_OK;
957 if ((header = gw_init_header()) == NULL)
958 return got_error_from_errno("malloc");
960 if (pledge("stdio rpath proc exec sendfd unveil",
961 NULL) == -1) {
962 error = got_error_from_errno("pledge");
963 goto done;
966 error = gw_apply_unveil(gw_trans->gw_dir->path);
967 if (error)
968 goto done;
970 error = gw_get_header(gw_trans, header,
971 gw_trans->gw_conf->got_max_commits_display);
972 if (error)
973 goto done;
975 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
976 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
977 "commits_line_wrapper", KATTR__MAX);
978 if (kerr != KCGI_OK)
979 goto done;
980 error = gw_gen_commit_header(gw_trans, n_header->commit_id,
981 n_header->refs_str);
982 if (error)
983 goto done;
984 error = gw_gen_author_header(gw_trans, n_header->author);
985 if (error)
986 goto done;
987 error = gw_gen_committer_header(gw_trans, n_header->author);
988 if (error)
989 goto done;
990 error = gw_get_time_str(&age, n_header->committer_time,
991 TM_LONG);
992 if (error)
993 goto done;
994 error = gw_gen_age_header(gw_trans, age ?age : "");
995 if (error)
996 goto done;
997 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
998 if (kerr != KCGI_OK)
999 goto done;
1001 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1002 "dotted_line", KATTR__MAX);
1003 if (kerr != KCGI_OK)
1004 goto done;
1005 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1006 if (kerr != KCGI_OK)
1007 goto done;
1009 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1010 "commit", KATTR__MAX);
1011 if (kerr != KCGI_OK)
1012 goto done;
1013 kerr = khttp_puts(gw_trans->gw_req, n_header->commit_msg);
1014 if (kerr != KCGI_OK)
1015 goto done;
1016 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1017 if (kerr != KCGI_OK)
1018 goto done;
1020 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
1021 gw_trans->repo_name, n_header->commit_id) == -1) {
1022 error = got_error_from_errno("asprintf");
1023 goto done;
1025 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1026 KATTR_ID, "navs_wrapper", KATTR__MAX);
1027 if (kerr != KCGI_OK)
1028 goto done;
1029 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1030 KATTR_ID, "navs", KATTR__MAX);
1031 if (kerr != KCGI_OK)
1032 goto done;
1033 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1034 KATTR_HREF, href_diff, KATTR__MAX);
1035 if (kerr != KCGI_OK)
1036 goto done;
1037 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1038 if (kerr != KCGI_OK)
1039 goto done;
1040 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1041 if (kerr != KCGI_OK)
1042 goto done;
1044 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1045 if (kerr != KCGI_OK)
1046 goto done;
1048 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
1049 gw_trans->repo_name, n_header->commit_id) == -1) {
1050 error = got_error_from_errno("asprintf");
1051 goto done;
1053 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1054 KATTR_HREF, href_blob, KATTR__MAX);
1055 if (kerr != KCGI_OK)
1056 goto done;
1057 khtml_puts(gw_trans->gw_html_req, "tree");
1058 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1059 if (kerr != KCGI_OK)
1060 goto done;
1061 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1062 if (kerr != KCGI_OK)
1063 goto done;
1065 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1066 "solid_line", KATTR__MAX);
1067 if (kerr != KCGI_OK)
1068 goto done;
1069 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1070 if (kerr != KCGI_OK)
1071 goto done;
1073 free(age);
1074 age = NULL;
1077 if (gw_trans->next_id || gw_trans->page > 0) {
1078 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1079 KATTR_ID, "np_wrapper", KATTR__MAX);
1080 if (kerr != KCGI_OK)
1081 goto done;
1082 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1083 KATTR_ID, "nav_prev", KATTR__MAX);
1084 if (kerr != KCGI_OK)
1085 goto done;
1088 if (gw_trans->page > 0 && gw_trans->prev_id) {
1089 if (asprintf(&href_prev,
1090 "?path=%s&page=%d&action=commits&commit=%s&prev=%s",
1091 gw_trans->repo_name, gw_trans->page - 1,
1092 gw_trans->prev_id ? gw_trans->prev_id : "",
1093 gw_trans->prev_prev_id ?
1094 gw_trans->prev_prev_id : "") == -1) {
1095 error = got_error_from_errno("asprintf");
1096 goto done;
1098 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1099 KATTR_HREF, href_prev, KATTR__MAX);
1100 if (kerr != KCGI_OK)
1101 goto done;
1102 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1103 if (kerr != KCGI_OK)
1104 goto done;
1105 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1106 if (kerr != KCGI_OK)
1107 goto done;
1110 if (gw_trans->next_id || gw_trans->page > 0) {
1111 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1112 if (kerr != KCGI_OK)
1113 return gw_kcgi_error(kerr);
1116 if (gw_trans->next_id) {
1117 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1118 KATTR_ID, "nav_next", KATTR__MAX);
1119 if (kerr != KCGI_OK)
1120 goto done;
1121 if (asprintf(&href_next,
1122 "?path=%s&page=%d&action=commits" \
1123 "&commit=%s&prev=%s&prev_prev=%s",
1124 gw_trans->repo_name, gw_trans->page + 1,
1125 gw_trans->next_id,
1126 gw_trans->next_prev_id ? gw_trans->next_prev_id : "",
1127 gw_trans->prev_id ?
1128 gw_trans->prev_id : "") == -1) {
1129 error = got_error_from_errno("calloc");
1130 goto done;
1132 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1133 KATTR_HREF, href_next, KATTR__MAX);
1134 if (kerr != KCGI_OK)
1135 goto done;
1136 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1137 if (kerr != KCGI_OK)
1138 goto done;
1139 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1140 if (kerr != KCGI_OK)
1141 goto done;
1144 if (gw_trans->next_id || gw_trans->page > 0) {
1145 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1146 if (kerr != KCGI_OK)
1147 goto done;
1149 done:
1150 gw_free_header(header);
1151 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1152 gw_free_header(n_header);
1153 free(age);
1154 free(href_next);
1155 free(href_prev);
1156 free(href_diff);
1157 free(href_blob);
1158 if (error == NULL && kerr != KCGI_OK)
1159 error = gw_kcgi_error(kerr);
1160 return error;
1163 static const struct got_error *
1164 gw_briefs(struct gw_trans *gw_trans)
1166 const struct got_error *error = NULL;
1167 struct gw_header *header = NULL, *n_header = NULL;
1168 char *age = NULL, *href_diff = NULL, *href_blob = NULL;
1169 char *href_prev = NULL, *href_next = NULL;
1170 char *newline, *smallerthan;
1171 enum kcgi_err kerr = KCGI_OK;
1173 if ((header = gw_init_header()) == NULL)
1174 return got_error_from_errno("malloc");
1176 if (pledge("stdio rpath proc exec sendfd unveil",
1177 NULL) == -1) {
1178 error = got_error_from_errno("pledge");
1179 goto done;
1182 if (gw_trans->action != GW_SUMMARY) {
1183 error = gw_apply_unveil(gw_trans->gw_dir->path);
1184 if (error)
1185 goto done;
1188 if (gw_trans->action == GW_SUMMARY)
1189 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
1190 else
1191 error = gw_get_header(gw_trans, header,
1192 gw_trans->gw_conf->got_max_commits_display);
1193 if (error)
1194 goto done;
1196 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
1197 error = gw_get_time_str(&age, n_header->committer_time,
1198 TM_DIFF);
1199 if (error)
1200 goto done;
1202 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1203 KATTR_ID, "briefs_wrapper", KATTR__MAX);
1204 if (kerr != KCGI_OK)
1205 goto done;
1207 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1208 KATTR_ID, "briefs_age", KATTR__MAX);
1209 if (kerr != KCGI_OK)
1210 goto done;
1211 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
1212 if (kerr != KCGI_OK)
1213 goto done;
1214 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1215 if (kerr != KCGI_OK)
1216 goto done;
1218 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1219 KATTR_ID, "briefs_author", KATTR__MAX);
1220 if (kerr != KCGI_OK)
1221 goto done;
1222 smallerthan = strchr(n_header->author, '<');
1223 if (smallerthan)
1224 *smallerthan = '\0';
1225 kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
1226 if (kerr != KCGI_OK)
1227 goto done;
1228 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1229 if (kerr != KCGI_OK)
1230 goto done;
1232 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
1233 gw_trans->repo_name, n_header->commit_id) == -1) {
1234 error = got_error_from_errno("asprintf");
1235 goto done;
1237 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1238 KATTR_ID, "briefs_log", KATTR__MAX);
1239 if (kerr != KCGI_OK)
1240 goto done;
1241 newline = strchr(n_header->commit_msg, '\n');
1242 if (newline)
1243 *newline = '\0';
1244 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1245 KATTR_HREF, href_diff, KATTR__MAX);
1246 if (kerr != KCGI_OK)
1247 goto done;
1248 kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
1249 if (kerr != KCGI_OK)
1250 goto done;
1251 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1252 if (kerr != KCGI_OK)
1253 goto done;
1255 if (n_header->refs_str) {
1256 kerr = khtml_puts(gw_trans->gw_html_req, " ");
1257 if (kerr != KCGI_OK)
1258 goto done;
1259 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
1260 KATTR_ID, "refs_str", KATTR__MAX);
1261 if (kerr != KCGI_OK)
1262 goto done;
1263 kerr = khtml_puts(gw_trans->gw_html_req, "(");
1264 if (kerr != KCGI_OK)
1265 goto done;
1266 kerr = khtml_puts(gw_trans->gw_html_req,
1267 n_header->refs_str);
1268 if (kerr != KCGI_OK)
1269 goto done;
1270 kerr = khtml_puts(gw_trans->gw_html_req, ")");
1271 if (kerr != KCGI_OK)
1272 goto done;
1273 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1274 if (kerr != KCGI_OK)
1275 goto done;
1278 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1279 if (kerr != KCGI_OK)
1280 goto done;
1282 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1283 KATTR_ID, "navs_wrapper", KATTR__MAX);
1284 if (kerr != KCGI_OK)
1285 goto done;
1286 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1287 KATTR_ID, "navs", KATTR__MAX);
1288 if (kerr != KCGI_OK)
1289 goto done;
1290 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1291 KATTR_HREF, href_diff, KATTR__MAX);
1292 if (kerr != KCGI_OK)
1293 goto done;
1294 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1295 if (kerr != KCGI_OK)
1296 goto done;
1297 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1298 if (kerr != KCGI_OK)
1299 goto done;
1301 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1302 if (kerr != KCGI_OK)
1303 goto done;
1305 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
1306 gw_trans->repo_name, n_header->commit_id) == -1) {
1307 error = got_error_from_errno("asprintf");
1308 goto done;
1310 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1311 KATTR_HREF, href_blob, KATTR__MAX);
1312 if (kerr != KCGI_OK)
1313 goto done;
1314 khtml_puts(gw_trans->gw_html_req, "tree");
1315 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1316 if (kerr != KCGI_OK)
1317 goto done;
1318 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1319 if (kerr != KCGI_OK)
1320 goto done;
1322 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1323 KATTR_ID, "dotted_line", KATTR__MAX);
1324 if (kerr != KCGI_OK)
1325 goto done;
1326 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1327 if (kerr != KCGI_OK)
1328 goto done;
1330 free(age);
1331 age = NULL;
1332 free(href_diff);
1333 href_diff = NULL;
1334 free(href_blob);
1335 href_blob = NULL;
1338 if (gw_trans->next_id || gw_trans->page > 0) {
1339 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1340 KATTR_ID, "np_wrapper", KATTR__MAX);
1341 if (kerr != KCGI_OK)
1342 goto done;
1343 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1344 KATTR_ID, "nav_prev", KATTR__MAX);
1345 if (kerr != KCGI_OK)
1346 goto done;
1349 if (gw_trans->page > 0 && gw_trans->prev_id) {
1350 if (asprintf(&href_prev,
1351 "?path=%s&page=%d&action=briefs&commit=%s&prev=%s",
1352 gw_trans->repo_name, gw_trans->page - 1,
1353 gw_trans->prev_id ? gw_trans->prev_id : "",
1354 gw_trans->prev_prev_id ?
1355 gw_trans->prev_prev_id : "") == -1) {
1356 error = got_error_from_errno("asprintf");
1357 goto done;
1359 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1360 KATTR_HREF, href_prev, KATTR__MAX);
1361 if (kerr != KCGI_OK)
1362 goto done;
1363 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1364 if (kerr != KCGI_OK)
1365 goto done;
1366 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1367 if (kerr != KCGI_OK)
1368 goto done;
1371 if (gw_trans->next_id || gw_trans->page > 0) {
1372 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1373 if (kerr != KCGI_OK)
1374 return gw_kcgi_error(kerr);
1377 if (gw_trans->next_id) {
1378 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1379 KATTR_ID, "nav_next", KATTR__MAX);
1380 if (kerr != KCGI_OK)
1381 goto done;
1382 if (asprintf(&href_next,
1383 "?path=%s&page=%d&action=briefs" \
1384 "&commit=%s&prev=%s&prev_prev=%s",
1385 gw_trans->repo_name, gw_trans->page + 1,
1386 gw_trans->next_id,
1387 gw_trans->next_prev_id ? gw_trans->next_prev_id : "",
1388 gw_trans->prev_id ?
1389 gw_trans->prev_id : "") == -1) {
1390 error = got_error_from_errno("calloc");
1391 goto done;
1393 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1394 KATTR_HREF, href_next, KATTR__MAX);
1395 if (kerr != KCGI_OK)
1396 goto done;
1397 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1398 if (kerr != KCGI_OK)
1399 goto done;
1400 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1401 if (kerr != KCGI_OK)
1402 goto done;
1405 if (gw_trans->next_id || gw_trans->page > 0) {
1406 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1407 if (kerr != KCGI_OK)
1408 goto done;
1410 done:
1411 gw_free_header(header);
1412 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1413 gw_free_header(n_header);
1414 free(age);
1415 free(href_next);
1416 free(href_prev);
1417 free(href_diff);
1418 free(href_blob);
1419 if (error == NULL && kerr != KCGI_OK)
1420 error = gw_kcgi_error(kerr);
1421 return error;
1424 static const struct got_error *
1425 gw_summary(struct gw_trans *gw_trans)
1427 const struct got_error *error = NULL;
1428 char *age = NULL;
1429 enum kcgi_err kerr = KCGI_OK;
1431 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1432 return got_error_from_errno("pledge");
1434 error = gw_apply_unveil(gw_trans->gw_dir->path);
1435 if (error)
1436 goto done;
1438 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1439 "summary_wrapper", KATTR__MAX);
1440 if (kerr != KCGI_OK)
1441 return gw_kcgi_error(kerr);
1443 if (gw_trans->gw_conf->got_show_repo_description &&
1444 gw_trans->gw_dir->description != NULL &&
1445 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1446 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1447 KATTR_ID, "description_title", KATTR__MAX);
1448 if (kerr != KCGI_OK)
1449 goto done;
1450 kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1451 if (kerr != KCGI_OK)
1452 goto done;
1453 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1454 if (kerr != KCGI_OK)
1455 goto done;
1456 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1457 KATTR_ID, "description", KATTR__MAX);
1458 if (kerr != KCGI_OK)
1459 goto done;
1460 kerr = khtml_puts(gw_trans->gw_html_req,
1461 gw_trans->gw_dir->description);
1462 if (kerr != KCGI_OK)
1463 goto done;
1464 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1465 if (kerr != KCGI_OK)
1466 goto done;
1469 if (gw_trans->gw_conf->got_show_repo_owner &&
1470 gw_trans->gw_dir->owner != NULL &&
1471 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
1472 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1473 KATTR_ID, "repo_owner_title", KATTR__MAX);
1474 if (kerr != KCGI_OK)
1475 goto done;
1476 kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1477 if (kerr != KCGI_OK)
1478 goto done;
1479 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1480 if (kerr != KCGI_OK)
1481 goto done;
1482 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1483 KATTR_ID, "repo_owner", KATTR__MAX);
1484 if (kerr != KCGI_OK)
1485 goto done;
1486 kerr = khtml_puts(gw_trans->gw_html_req,
1487 gw_trans->gw_dir->owner);
1488 if (kerr != KCGI_OK)
1489 goto done;
1490 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1491 if (kerr != KCGI_OK)
1492 goto done;
1495 if (gw_trans->gw_conf->got_show_repo_age) {
1496 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1497 NULL, TM_LONG);
1498 if (error)
1499 goto done;
1500 if (age != NULL) {
1501 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1502 KATTR_ID, "last_change_title", KATTR__MAX);
1503 if (kerr != KCGI_OK)
1504 goto done;
1505 kerr = khtml_puts(gw_trans->gw_html_req,
1506 "Last Change: ");
1507 if (kerr != KCGI_OK)
1508 goto done;
1509 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1510 if (kerr != KCGI_OK)
1511 goto done;
1512 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1513 KATTR_ID, "last_change", KATTR__MAX);
1514 if (kerr != KCGI_OK)
1515 goto done;
1516 kerr = khtml_puts(gw_trans->gw_html_req, age);
1517 if (kerr != KCGI_OK)
1518 goto done;
1519 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1520 if (kerr != KCGI_OK)
1521 goto done;
1525 if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1526 gw_trans->gw_dir->url != NULL &&
1527 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1528 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1529 KATTR_ID, "cloneurl_title", KATTR__MAX);
1530 if (kerr != KCGI_OK)
1531 goto done;
1532 kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1533 if (kerr != KCGI_OK)
1534 goto done;
1535 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1536 if (kerr != KCGI_OK)
1537 goto done;
1538 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1539 KATTR_ID, "cloneurl", KATTR__MAX);
1540 if (kerr != KCGI_OK)
1541 goto done;
1542 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1543 if (kerr != KCGI_OK)
1544 goto done;
1545 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1546 if (kerr != KCGI_OK)
1547 goto done;
1550 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1551 if (kerr != KCGI_OK)
1552 goto done;
1554 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1555 "briefs_title_wrapper", KATTR__MAX);
1556 if (kerr != KCGI_OK)
1557 goto done;
1558 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1559 "briefs_title", KATTR__MAX);
1560 if (kerr != KCGI_OK)
1561 goto done;
1562 kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1563 if (kerr != KCGI_OK)
1564 goto done;
1565 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1566 if (kerr != KCGI_OK)
1567 goto done;
1568 error = gw_briefs(gw_trans);
1569 if (error)
1570 goto done;
1572 error = gw_tags(gw_trans);
1573 if (error)
1574 goto done;
1576 error = gw_output_repo_heads(gw_trans);
1577 done:
1578 free(age);
1579 if (error == NULL && kerr != KCGI_OK)
1580 error = gw_kcgi_error(kerr);
1581 return error;
1584 static const struct got_error *
1585 gw_tree(struct gw_trans *gw_trans)
1587 const struct got_error *error = NULL;
1588 struct gw_header *header = NULL;
1589 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1590 char *age = NULL;
1591 enum kcgi_err kerr;
1593 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1594 return got_error_from_errno("pledge");
1596 if ((header = gw_init_header()) == NULL)
1597 return got_error_from_errno("malloc");
1599 error = gw_apply_unveil(gw_trans->gw_dir->path);
1600 if (error)
1601 goto done;
1603 error = gw_get_header(gw_trans, header, 1);
1604 if (error)
1605 goto done;
1607 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1608 "tree_header_wrapper", KATTR__MAX);
1609 if (kerr != KCGI_OK)
1610 goto done;
1611 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1612 "tree_header", KATTR__MAX);
1613 if (kerr != KCGI_OK)
1614 goto done;
1615 error = gw_gen_tree_header(gw_trans, header->tree_id);
1616 if (error)
1617 goto done;
1618 error = gw_get_time_str(&age, header->committer_time,
1619 TM_LONG);
1620 if (error)
1621 goto done;
1622 error = gw_gen_age_header(gw_trans, age ?age : "");
1623 if (error)
1624 goto done;
1625 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1626 if (error)
1627 goto done;
1628 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1629 if (kerr != KCGI_OK)
1630 goto done;
1631 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1632 "dotted_line", KATTR__MAX);
1633 if (kerr != KCGI_OK)
1634 goto done;
1635 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1636 if (kerr != KCGI_OK)
1637 goto done;
1639 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1640 "tree", KATTR__MAX);
1641 if (kerr != KCGI_OK)
1642 goto done;
1643 error = gw_output_repo_tree(gw_trans);
1644 if (error)
1645 goto done;
1647 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1648 done:
1649 gw_free_header(header);
1650 free(tree_html_disp);
1651 free(tree_html);
1652 free(tree);
1653 free(age);
1654 if (error == NULL && kerr != KCGI_OK)
1655 error = gw_kcgi_error(kerr);
1656 return error;
1659 static const struct got_error *
1660 gw_tags(struct gw_trans *gw_trans)
1662 const struct got_error *error = NULL;
1663 struct gw_header *header = NULL;
1664 char *href_next = NULL, *href_prev = NULL;
1665 enum kcgi_err kerr = KCGI_OK;
1667 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1668 return got_error_from_errno("pledge");
1670 if ((header = gw_init_header()) == NULL)
1671 return got_error_from_errno("malloc");
1673 if (gw_trans->action != GW_SUMMARY) {
1674 error = gw_apply_unveil(gw_trans->gw_dir->path);
1675 if (error)
1676 goto done;
1679 error = gw_get_header(gw_trans, header, 1);
1680 if (error)
1681 goto done;
1683 if (gw_trans->action == GW_SUMMARY) {
1684 error = gw_output_repo_tags(gw_trans, header,
1685 D_MAXSLCOMMDISP, TAGBRIEF);
1686 if (error)
1687 goto done;
1688 } else {
1689 error = gw_output_repo_tags(gw_trans, header,
1690 gw_trans->gw_conf->got_max_commits_display, TAGBRIEF);
1691 if (error)
1692 goto done;
1695 if (gw_trans->next_id || gw_trans->page > 0) {
1696 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1697 KATTR_ID, "np_wrapper", KATTR__MAX);
1698 if (kerr != KCGI_OK)
1699 goto done;
1700 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1701 KATTR_ID, "nav_prev", KATTR__MAX);
1702 if (kerr != KCGI_OK)
1703 goto done;
1706 if (gw_trans->page > 0 && gw_trans->prev_id) {
1707 if (asprintf(&href_prev,
1708 "?path=%s&page=%d&action=tags&commit=%s&prev=%s",
1709 gw_trans->repo_name, gw_trans->page - 1,
1710 gw_trans->prev_id ? gw_trans->prev_id : "",
1711 gw_trans->prev_prev_id ?
1712 gw_trans->prev_prev_id : "") == -1) {
1713 error = got_error_from_errno("asprintf");
1714 goto done;
1716 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1717 KATTR_HREF, href_prev, KATTR__MAX);
1718 if (kerr != KCGI_OK)
1719 goto done;
1720 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1721 if (kerr != KCGI_OK)
1722 goto done;
1723 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1724 if (kerr != KCGI_OK)
1725 goto done;
1728 if (gw_trans->next_id || gw_trans->page > 0) {
1729 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1730 if (kerr != KCGI_OK)
1731 return gw_kcgi_error(kerr);
1734 if (gw_trans->next_id) {
1735 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1736 KATTR_ID, "nav_next", KATTR__MAX);
1737 if (kerr != KCGI_OK)
1738 goto done;
1739 if (asprintf(&href_next,
1740 "?path=%s&page=%d&action=tags" \
1741 "&commit=%s&prev=%s&prev_prev=%s",
1742 gw_trans->repo_name, gw_trans->page + 1,
1743 gw_trans->next_id,
1744 gw_trans->next_prev_id ? gw_trans->next_prev_id : "",
1745 gw_trans->prev_id ?
1746 gw_trans->prev_id : "") == -1) {
1747 error = got_error_from_errno("calloc");
1748 goto done;
1750 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1751 KATTR_HREF, href_next, KATTR__MAX);
1752 if (kerr != KCGI_OK)
1753 goto done;
1754 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1755 if (kerr != KCGI_OK)
1756 goto done;
1757 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1758 if (kerr != KCGI_OK)
1759 goto done;
1762 if (gw_trans->next_id || gw_trans->page > 0) {
1763 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1764 if (kerr != KCGI_OK)
1765 goto done;
1767 done:
1768 gw_free_header(header);
1769 free(href_next);
1770 free(href_prev);
1771 if (error == NULL && kerr != KCGI_OK)
1772 error = gw_kcgi_error(kerr);
1773 return error;
1776 static const struct got_error *
1777 gw_tag(struct gw_trans *gw_trans)
1779 const struct got_error *error = NULL;
1780 struct gw_header *header = NULL;
1781 enum kcgi_err kerr;
1783 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1784 return got_error_from_errno("pledge");
1786 if ((header = gw_init_header()) == NULL)
1787 return got_error_from_errno("malloc");
1789 error = gw_apply_unveil(gw_trans->gw_dir->path);
1790 if (error)
1791 goto done;
1793 if (gw_trans->commit_id == NULL) {
1794 error = got_error_msg(GOT_ERR_QUERYSTRING,
1795 "commit required in querystring");
1796 goto done;
1799 error = gw_get_header(gw_trans, header, 1);
1800 if (error)
1801 goto done;
1803 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1804 "tag_header_wrapper", KATTR__MAX);
1805 if (kerr != KCGI_OK)
1806 goto done;
1807 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1808 "tag_header", KATTR__MAX);
1809 if (kerr != KCGI_OK)
1810 goto done;
1811 error = gw_gen_commit_header(gw_trans, header->commit_id,
1812 header->refs_str);
1813 if (error)
1814 goto done;
1815 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1816 if (error)
1817 goto done;
1818 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1819 if (kerr != KCGI_OK)
1820 goto done;
1821 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1822 "dotted_line", KATTR__MAX);
1823 if (kerr != KCGI_OK)
1824 goto done;
1825 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1826 if (kerr != KCGI_OK)
1827 goto done;
1829 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1830 "tree", KATTR__MAX);
1831 if (kerr != KCGI_OK)
1832 goto done;
1834 error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1835 if (error)
1836 goto done;
1838 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1839 done:
1840 gw_free_header(header);
1841 if (error == NULL && kerr != KCGI_OK)
1842 error = gw_kcgi_error(kerr);
1843 return error;
1846 static const struct got_error *
1847 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1849 const struct got_error *error = NULL;
1850 DIR *dt;
1851 char *dir_test;
1852 int opened = 0;
1854 if (asprintf(&dir_test, "%s/%s/%s",
1855 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1856 GOTWEB_GIT_DIR) == -1)
1857 return got_error_from_errno("asprintf");
1859 dt = opendir(dir_test);
1860 if (dt == NULL) {
1861 free(dir_test);
1862 } else {
1863 gw_dir->path = strdup(dir_test);
1864 if (gw_dir->path == NULL) {
1865 opened = 1;
1866 error = got_error_from_errno("strdup");
1867 goto errored;
1869 opened = 1;
1870 goto done;
1873 if (asprintf(&dir_test, "%s/%s/%s",
1874 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1875 GOTWEB_GOT_DIR) == -1) {
1876 dir_test = NULL;
1877 error = got_error_from_errno("asprintf");
1878 goto errored;
1881 dt = opendir(dir_test);
1882 if (dt == NULL)
1883 free(dir_test);
1884 else {
1885 opened = 1;
1886 error = got_error(GOT_ERR_NOT_GIT_REPO);
1887 goto errored;
1890 if (asprintf(&dir_test, "%s/%s",
1891 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1) {
1892 error = got_error_from_errno("asprintf");
1893 dir_test = NULL;
1894 goto errored;
1897 gw_dir->path = strdup(dir_test);
1898 if (gw_dir->path == NULL) {
1899 opened = 1;
1900 error = got_error_from_errno("strdup");
1901 goto errored;
1904 dt = opendir(dir_test);
1905 if (dt == NULL) {
1906 error = got_error_path(gw_dir->name, GOT_ERR_NOT_GIT_REPO);
1907 goto errored;
1908 } else
1909 opened = 1;
1910 done:
1911 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1912 gw_dir->path);
1913 if (error)
1914 goto errored;
1915 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1916 if (error)
1917 goto errored;
1918 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1919 NULL, TM_DIFF);
1920 if (error)
1921 goto errored;
1922 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1923 errored:
1924 free(dir_test);
1925 if (opened)
1926 if (dt && closedir(dt) == -1 && error == NULL)
1927 error = got_error_from_errno("closedir");
1928 return error;
1931 static const struct got_error *
1932 gw_load_got_paths(struct gw_trans *gw_trans)
1934 const struct got_error *error = NULL;
1935 DIR *d;
1936 struct dirent **sd_dent;
1937 struct gw_dir *gw_dir;
1938 struct stat st;
1939 unsigned int d_cnt, d_i;
1941 d = opendir(gw_trans->gw_conf->got_repos_path);
1942 if (d == NULL) {
1943 error = got_error_from_errno2("opendir",
1944 gw_trans->gw_conf->got_repos_path);
1945 return error;
1948 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1949 alphasort);
1950 if (d_cnt == -1) {
1951 error = got_error_from_errno2("scandir",
1952 gw_trans->gw_conf->got_repos_path);
1953 goto done;
1956 for (d_i = 0; d_i < d_cnt; d_i++) {
1957 if (gw_trans->gw_conf->got_max_repos > 0 &&
1958 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1959 break; /* account for parent and self */
1961 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1962 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1963 continue;
1965 error = gw_init_gw_dir(&gw_dir, sd_dent[d_i]->d_name);
1966 if (error)
1967 goto done;
1969 error = gw_load_got_path(gw_trans, gw_dir);
1970 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1971 error = NULL;
1972 continue;
1974 else if (error)
1975 goto done;
1977 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1978 !got_path_dir_is_empty(gw_dir->path)) {
1979 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1980 entry);
1981 gw_trans->repos_total++;
1984 done:
1985 if (d && closedir(d) == -1 && error == NULL)
1986 error = got_error_from_errno("closedir");
1987 return error;
1990 static const struct got_error *
1991 gw_parse_querystring(struct gw_trans *gw_trans)
1993 const struct got_error *error = NULL;
1994 struct kpair *p;
1995 struct gw_query_action *action = NULL;
1996 unsigned int i;
1998 if (gw_trans->gw_req->fieldnmap[0]) {
1999 return got_error(GOT_ERR_QUERYSTRING);
2000 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
2001 /* define gw_trans->repo_path */
2002 gw_trans->repo_name = p->parsed.s;
2004 if (asprintf(&gw_trans->repo_path, "%s/%s",
2005 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
2006 return got_error_from_errno("asprintf");
2008 /* get action and set function */
2009 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION])) {
2010 for (i = 0; i < nitems(gw_query_funcs); i++) {
2011 action = &gw_query_funcs[i];
2012 if (action->func_name == NULL)
2013 continue;
2014 if (strcmp(action->func_name,
2015 p->parsed.s) == 0) {
2016 gw_trans->action = i;
2017 break;
2021 if (gw_trans->action == -1) {
2022 gw_trans->action = GW_ERR;
2023 gw_trans->error = got_error_msg(GOT_ERR_QUERYSTRING,
2024 p != NULL ? "bad action in querystring" :
2025 "no action in querystring");
2026 return error;
2029 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID])) {
2030 if (asprintf(&gw_trans->commit_id, "%s",
2031 p->parsed.s) == -1)
2032 return got_error_from_errno("asprintf");
2035 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
2036 gw_trans->repo_file = p->parsed.s;
2038 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER])) {
2039 if (asprintf(&gw_trans->repo_folder, "%s",
2040 p->parsed.s) == -1)
2041 return got_error_from_errno("asprintf");
2044 if ((p = gw_trans->gw_req->fieldmap[KEY_PREV_ID])) {
2045 if (asprintf(&gw_trans->prev_id, "%s",
2046 p->parsed.s) == -1)
2047 return got_error_from_errno("asprintf");
2050 if ((p = gw_trans->gw_req->fieldmap[KEY_PREV_PREV_ID])) {
2051 if (asprintf(&gw_trans->prev_prev_id, "%s",
2052 p->parsed.s) == -1)
2053 return got_error_from_errno("asprintf");
2056 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
2057 gw_trans->headref = p->parsed.s;
2059 error = gw_init_gw_dir(&gw_trans->gw_dir, gw_trans->repo_name);
2060 if (error)
2061 return error;
2063 gw_trans->error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
2064 } else
2065 gw_trans->action = GW_INDEX;
2067 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
2068 gw_trans->page = p->parsed.i;
2070 return error;
2073 static const struct got_error *
2074 gw_init_gw_dir(struct gw_dir **gw_dir, const char *dir)
2076 const struct got_error *error;
2078 *gw_dir = malloc(sizeof(**gw_dir));
2079 if (*gw_dir == NULL)
2080 return got_error_from_errno("malloc");
2082 if (asprintf(&(*gw_dir)->name, "%s", dir) == -1) {
2083 error = got_error_from_errno("asprintf");
2084 free(*gw_dir);
2085 *gw_dir = NULL;
2086 return NULL;
2089 return NULL;
2092 static const struct got_error *
2093 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
2095 enum kcgi_err kerr;
2097 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
2098 if (kerr != KCGI_OK)
2099 return gw_kcgi_error(kerr);
2100 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
2101 khttps[code]);
2102 if (kerr != KCGI_OK)
2103 return gw_kcgi_error(kerr);
2104 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
2105 kmimetypes[mime]);
2106 if (kerr != KCGI_OK)
2107 return gw_kcgi_error(kerr);
2108 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
2109 "nosniff");
2110 if (kerr != KCGI_OK)
2111 return gw_kcgi_error(kerr);
2112 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
2113 if (kerr != KCGI_OK)
2114 return gw_kcgi_error(kerr);
2115 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
2116 "1; mode=block");
2117 if (kerr != KCGI_OK)
2118 return gw_kcgi_error(kerr);
2120 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
2121 kerr = khttp_head(gw_trans->gw_req,
2122 kresps[KRESP_CONTENT_DISPOSITION],
2123 "attachment; filename=%s", gw_trans->repo_file);
2124 if (kerr != KCGI_OK)
2125 return gw_kcgi_error(kerr);
2128 kerr = khttp_body(gw_trans->gw_req);
2129 return gw_kcgi_error(kerr);
2132 static const struct got_error *
2133 gw_display_index(struct gw_trans *gw_trans)
2135 const struct got_error *error;
2136 enum kcgi_err kerr;
2138 /* catch early querystring errors */
2139 if (gw_trans->error)
2140 gw_trans->action = GW_ERR;
2142 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
2143 if (error)
2144 return error;
2146 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
2147 if (kerr != KCGI_OK)
2148 return gw_kcgi_error(kerr);
2150 if (gw_trans->action != GW_BLOB) {
2151 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
2152 gw_query_funcs[gw_trans->action].template);
2153 if (kerr != KCGI_OK) {
2154 khtml_close(gw_trans->gw_html_req);
2155 return gw_kcgi_error(kerr);
2159 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
2162 static const struct got_error *
2163 gw_error(struct gw_trans *gw_trans)
2165 enum kcgi_err kerr;
2167 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->error->msg);
2169 return gw_kcgi_error(kerr);
2172 static int
2173 gw_template(size_t key, void *arg)
2175 const struct got_error *error = NULL;
2176 enum kcgi_err kerr;
2177 struct gw_trans *gw_trans = arg;
2178 char *img_src = NULL;
2180 switch (key) {
2181 case (TEMPL_HEAD):
2182 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2183 KATTR_NAME, "viewport",
2184 KATTR_CONTENT, "initial-scale=.75, user-scalable=yes",
2185 KATTR__MAX);
2186 if (kerr != KCGI_OK)
2187 return 0;
2188 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2189 if (kerr != KCGI_OK)
2190 return 0;
2191 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2192 KATTR_CHARSET, "utf-8",
2193 KATTR__MAX);
2194 if (kerr != KCGI_OK)
2195 return 0;
2196 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2197 if (kerr != KCGI_OK)
2198 return 0;
2199 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2200 KATTR_NAME, "msapplication-TileColor",
2201 KATTR_CONTENT, "#da532c", KATTR__MAX);
2202 if (kerr != KCGI_OK)
2203 return 0;
2204 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2205 if (kerr != KCGI_OK)
2206 return 0;
2207 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2208 KATTR_NAME, "theme-color",
2209 KATTR_CONTENT, "#ffffff", KATTR__MAX);
2210 if (kerr != KCGI_OK)
2211 return 0;
2212 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2213 if (kerr != KCGI_OK)
2214 return 0;
2215 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2216 KATTR_REL, "apple-touch-icon", KATTR_SIZES, "180x180",
2217 KATTR_HREF, "/apple-touch-icon.png", KATTR__MAX);
2218 if (kerr != KCGI_OK)
2219 return 0;
2220 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2221 if (kerr != KCGI_OK)
2222 return 0;
2223 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2224 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2225 "32x32", KATTR_HREF, "/favicon-32x32.png", KATTR__MAX);
2226 if (kerr != KCGI_OK)
2227 return 0;
2228 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2229 if (kerr != KCGI_OK)
2230 return 0;
2231 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2232 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2233 "16x16", KATTR_HREF, "/favicon-16x16.png", KATTR__MAX);
2234 if (kerr != KCGI_OK)
2235 return 0;
2236 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2237 if (kerr != KCGI_OK)
2238 return 0;
2239 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2240 KATTR_REL, "manifest", KATTR_HREF, "/site.webmanifest",
2241 KATTR__MAX);
2242 if (kerr != KCGI_OK)
2243 return 0;
2244 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2245 if (kerr != KCGI_OK)
2246 return 0;
2247 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2248 KATTR_REL, "mask-icon", KATTR_HREF,
2249 "/safari-pinned-tab.svg", KATTR__MAX);
2250 if (kerr != KCGI_OK)
2251 return 0;
2252 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2253 if (kerr != KCGI_OK)
2254 return 0;
2255 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2256 KATTR_REL, "stylesheet", KATTR_TYPE, "text/css",
2257 KATTR_HREF, "/gotweb.css", KATTR__MAX);
2258 if (kerr != KCGI_OK)
2259 return 0;
2260 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2261 if (kerr != KCGI_OK)
2262 return 0;
2263 break;
2264 case(TEMPL_HEADER):
2265 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2266 KATTR_ID, "got_link", KATTR__MAX);
2267 if (kerr != KCGI_OK)
2268 return 0;
2269 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2270 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
2271 KATTR_TARGET, "_sotd", KATTR__MAX);
2272 if (kerr != KCGI_OK)
2273 return 0;
2274 if (asprintf(&img_src, "/%s",
2275 gw_trans->gw_conf->got_logo) == -1)
2276 return 0;
2277 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
2278 KATTR_SRC, img_src, KATTR__MAX);
2279 if (kerr != KCGI_OK) {
2280 free(img_src);
2281 return 0;
2283 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2284 if (kerr != KCGI_OK) {
2285 free(img_src);
2286 return 0;
2288 break;
2289 case (TEMPL_SITEPATH):
2290 error = gw_output_site_link(gw_trans);
2291 if (error)
2292 return 0;
2293 break;
2294 case(TEMPL_TITLE):
2295 if (gw_trans->gw_conf->got_site_name != NULL) {
2296 kerr = khtml_puts(gw_trans->gw_html_req,
2297 gw_trans->gw_conf->got_site_name);
2298 if (kerr != KCGI_OK)
2299 return 0;
2301 break;
2302 case (TEMPL_SEARCH):
2303 break;
2304 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
2305 "search", KATTR__MAX);
2306 if (kerr != KCGI_OK)
2307 return 0;
2308 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
2309 KATTR_METHOD, "POST", KATTR__MAX);
2310 if (kerr != KCGI_OK)
2311 return 0;
2312 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
2313 "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
2314 KATTR_MAXLENGTH, "50", KATTR__MAX);
2315 if (kerr != KCGI_OK)
2316 return 0;
2317 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
2318 KATTR__MAX);
2319 if (kerr != KCGI_OK)
2320 return 0;
2321 kerr = khtml_puts(gw_trans->gw_html_req, "Search");
2322 if (kerr != KCGI_OK)
2323 return 0;
2324 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
2325 if (kerr != KCGI_OK)
2326 return 0;
2327 break;
2328 case(TEMPL_SITEOWNER):
2329 if (gw_trans->gw_conf->got_site_owner != NULL &&
2330 gw_trans->gw_conf->got_show_site_owner) {
2331 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2332 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
2333 if (kerr != KCGI_OK)
2334 return 0;
2335 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2336 KATTR_ID, "site_owner", KATTR__MAX);
2337 if (kerr != KCGI_OK)
2338 return 0;
2339 kerr = khtml_puts(gw_trans->gw_html_req,
2340 gw_trans->gw_conf->got_site_owner);
2341 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2342 if (kerr != KCGI_OK)
2343 return 0;
2345 break;
2346 case(TEMPL_CONTENT):
2347 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
2348 if (error) {
2349 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2350 KATTR_ID, "tmpl_err", KATTR__MAX);
2351 if (kerr != KCGI_OK)
2352 return 0;
2353 kerr = khttp_puts(gw_trans->gw_req, "Error: ");
2354 if (kerr != KCGI_OK)
2355 return 0;
2356 kerr = khttp_puts(gw_trans->gw_req, error->msg);
2357 if (kerr != KCGI_OK)
2358 return 0;
2359 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2360 if (kerr != KCGI_OK)
2361 return 0;
2363 break;
2364 default:
2365 return 0;
2367 return 1;
2370 static const struct got_error *
2371 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
2373 const struct got_error *error = NULL;
2374 enum kcgi_err kerr = KCGI_OK;
2376 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2377 KATTR_ID, "header_commit_title", KATTR__MAX);
2378 if (kerr != KCGI_OK)
2379 goto done;
2380 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
2381 if (kerr != KCGI_OK)
2382 goto done;
2383 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2384 if (kerr != KCGI_OK)
2385 goto done;
2386 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2387 KATTR_ID, "header_commit", KATTR__MAX);
2388 if (kerr != KCGI_OK)
2389 goto done;
2390 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2391 if (kerr != KCGI_OK)
2392 goto done;
2393 kerr = khtml_puts(gw_trans->gw_html_req, " ");
2394 if (kerr != KCGI_OK)
2395 goto done;
2396 if (str2 != NULL) {
2397 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
2398 KATTR_ID, "refs_str", KATTR__MAX);
2399 if (kerr != KCGI_OK)
2400 goto done;
2401 kerr = khtml_puts(gw_trans->gw_html_req, "(");
2402 if (kerr != KCGI_OK)
2403 goto done;
2404 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2405 if (kerr != KCGI_OK)
2406 goto done;
2407 kerr = khtml_puts(gw_trans->gw_html_req, ")");
2408 if (kerr != KCGI_OK)
2409 goto done;
2410 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2411 if (kerr != KCGI_OK)
2412 goto done;
2414 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2415 done:
2416 if (error == NULL && kerr != KCGI_OK)
2417 error = gw_kcgi_error(kerr);
2418 return error;
2421 static const struct got_error *
2422 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2424 const struct got_error *error = NULL;
2425 enum kcgi_err kerr = KCGI_OK;
2427 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2428 KATTR_ID, "header_diff_title", KATTR__MAX);
2429 if (kerr != KCGI_OK)
2430 goto done;
2431 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2432 if (kerr != KCGI_OK)
2433 goto done;
2434 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2435 if (kerr != KCGI_OK)
2436 goto done;
2437 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2438 KATTR_ID, "header_diff", KATTR__MAX);
2439 if (kerr != KCGI_OK)
2440 goto done;
2441 if (str1 != NULL) {
2442 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2443 if (kerr != KCGI_OK)
2444 goto done;
2446 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2447 if (kerr != KCGI_OK)
2448 goto done;
2449 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2450 if (kerr != KCGI_OK)
2451 goto done;
2452 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2453 done:
2454 if (error == NULL && kerr != KCGI_OK)
2455 error = gw_kcgi_error(kerr);
2456 return error;
2459 static const struct got_error *
2460 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2462 const struct got_error *error = NULL;
2463 enum kcgi_err kerr = KCGI_OK;
2465 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2466 KATTR_ID, "header_age_title", KATTR__MAX);
2467 if (kerr != KCGI_OK)
2468 goto done;
2469 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2470 if (kerr != KCGI_OK)
2471 goto done;
2472 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2473 if (kerr != KCGI_OK)
2474 goto done;
2475 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2476 KATTR_ID, "header_age", KATTR__MAX);
2477 if (kerr != KCGI_OK)
2478 goto done;
2479 kerr = khtml_puts(gw_trans->gw_html_req, str);
2480 if (kerr != KCGI_OK)
2481 goto done;
2482 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2483 done:
2484 if (error == NULL && kerr != KCGI_OK)
2485 error = gw_kcgi_error(kerr);
2486 return error;
2489 static const struct got_error *
2490 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2492 const struct got_error *error = NULL;
2493 enum kcgi_err kerr;
2495 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2496 KATTR_ID, "header_author_title", KATTR__MAX);
2497 if (kerr != KCGI_OK)
2498 goto done;
2499 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2500 if (kerr != KCGI_OK)
2501 goto done;
2502 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2503 if (kerr != KCGI_OK)
2504 goto done;
2505 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2506 KATTR_ID, "header_author", KATTR__MAX);
2507 if (kerr != KCGI_OK)
2508 goto done;
2509 kerr = khtml_puts(gw_trans->gw_html_req, str);
2510 if (kerr != KCGI_OK)
2511 goto done;
2512 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2513 done:
2514 if (error == NULL && kerr != KCGI_OK)
2515 error = gw_kcgi_error(kerr);
2516 return error;
2519 static const struct got_error *
2520 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2522 const struct got_error *error = NULL;
2523 enum kcgi_err kerr;
2525 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2526 KATTR_ID, "header_committer_title", KATTR__MAX);
2527 if (kerr != KCGI_OK)
2528 goto done;
2529 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2530 if (kerr != KCGI_OK)
2531 goto done;
2532 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2533 if (kerr != KCGI_OK)
2534 goto done;
2535 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2536 KATTR_ID, "header_committer", KATTR__MAX);
2537 if (kerr != KCGI_OK)
2538 goto done;
2539 kerr = khtml_puts(gw_trans->gw_html_req, str);
2540 if (kerr != KCGI_OK)
2541 goto done;
2542 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2543 done:
2544 if (error == NULL && kerr != KCGI_OK)
2545 error = gw_kcgi_error(kerr);
2546 return error;
2549 static const struct got_error *
2550 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2552 const struct got_error *error = NULL;
2553 enum kcgi_err kerr;
2555 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2556 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2557 if (kerr != KCGI_OK)
2558 goto done;
2559 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2560 if (kerr != KCGI_OK)
2561 goto done;
2562 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2563 if (kerr != KCGI_OK)
2564 goto done;
2565 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2566 KATTR_ID, "header_commit_msg", KATTR__MAX);
2567 if (kerr != KCGI_OK)
2568 goto done;
2569 kerr = khttp_puts(gw_trans->gw_req, str);
2570 if (kerr != KCGI_OK)
2571 goto done;
2572 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2573 done:
2574 if (error == NULL && kerr != KCGI_OK)
2575 error = gw_kcgi_error(kerr);
2576 return error;
2579 static const struct got_error *
2580 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2582 const struct got_error *error = NULL;
2583 enum kcgi_err kerr;
2585 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2586 KATTR_ID, "header_tree_title", KATTR__MAX);
2587 if (kerr != KCGI_OK)
2588 goto done;
2589 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2590 if (kerr != KCGI_OK)
2591 goto done;
2592 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2593 if (kerr != KCGI_OK)
2594 goto done;
2595 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2596 KATTR_ID, "header_tree", KATTR__MAX);
2597 if (kerr != KCGI_OK)
2598 goto done;
2599 kerr = khtml_puts(gw_trans->gw_html_req, str);
2600 if (kerr != KCGI_OK)
2601 goto done;
2602 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2603 done:
2604 if (error == NULL && kerr != KCGI_OK)
2605 error = gw_kcgi_error(kerr);
2606 return error;
2609 static const struct got_error *
2610 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2611 char *dir)
2613 const struct got_error *error = NULL;
2614 FILE *f = NULL;
2615 char *d_file = NULL;
2616 unsigned int len;
2617 size_t n;
2619 *description = NULL;
2620 if (gw_trans->gw_conf->got_show_repo_description == 0)
2621 return NULL;
2623 if (asprintf(&d_file, "%s/description", dir) == -1)
2624 return got_error_from_errno("asprintf");
2626 f = fopen(d_file, "r");
2627 if (f == NULL) {
2628 if (errno == ENOENT || errno == EACCES)
2629 return NULL;
2630 error = got_error_from_errno2("fopen", d_file);
2631 goto done;
2634 if (fseek(f, 0, SEEK_END) == -1) {
2635 error = got_ferror(f, GOT_ERR_IO);
2636 goto done;
2638 len = ftell(f);
2639 if (len == -1) {
2640 error = got_ferror(f, GOT_ERR_IO);
2641 goto done;
2643 if (fseek(f, 0, SEEK_SET) == -1) {
2644 error = got_ferror(f, GOT_ERR_IO);
2645 goto done;
2647 *description = calloc(len + 1, sizeof(**description));
2648 if (*description == NULL) {
2649 error = got_error_from_errno("calloc");
2650 goto done;
2653 n = fread(*description, 1, len, f);
2654 if (n == 0 && ferror(f))
2655 error = got_ferror(f, GOT_ERR_IO);
2656 done:
2657 if (f != NULL && fclose(f) == -1 && error == NULL)
2658 error = got_error_from_errno("fclose");
2659 free(d_file);
2660 return error;
2663 static const struct got_error *
2664 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2666 struct tm tm;
2667 time_t diff_time;
2668 char *years = "years ago", *months = "months ago";
2669 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2670 char *minutes = "minutes ago", *seconds = "seconds ago";
2671 char *now = "right now";
2672 char *s;
2673 char datebuf[29];
2675 *repo_age = NULL;
2677 switch (ref_tm) {
2678 case TM_DIFF:
2679 diff_time = time(NULL) - committer_time;
2680 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2681 if (asprintf(repo_age, "%lld %s",
2682 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2683 return got_error_from_errno("asprintf");
2684 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2685 if (asprintf(repo_age, "%lld %s",
2686 (diff_time / 60 / 60 / 24 / (365 / 12)),
2687 months) == -1)
2688 return got_error_from_errno("asprintf");
2689 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2690 if (asprintf(repo_age, "%lld %s",
2691 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2692 return got_error_from_errno("asprintf");
2693 } else if (diff_time > 60 * 60 * 24 * 2) {
2694 if (asprintf(repo_age, "%lld %s",
2695 (diff_time / 60 / 60 / 24), days) == -1)
2696 return got_error_from_errno("asprintf");
2697 } else if (diff_time > 60 * 60 * 2) {
2698 if (asprintf(repo_age, "%lld %s",
2699 (diff_time / 60 / 60), hours) == -1)
2700 return got_error_from_errno("asprintf");
2701 } else if (diff_time > 60 * 2) {
2702 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2703 minutes) == -1)
2704 return got_error_from_errno("asprintf");
2705 } else if (diff_time > 2) {
2706 if (asprintf(repo_age, "%lld %s", diff_time,
2707 seconds) == -1)
2708 return got_error_from_errno("asprintf");
2709 } else {
2710 if (asprintf(repo_age, "%s", now) == -1)
2711 return got_error_from_errno("asprintf");
2713 break;
2714 case TM_LONG:
2715 if (gmtime_r(&committer_time, &tm) == NULL)
2716 return got_error_from_errno("gmtime_r");
2718 s = asctime_r(&tm, datebuf);
2719 if (s == NULL)
2720 return got_error_from_errno("asctime_r");
2722 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2723 return got_error_from_errno("asprintf");
2724 break;
2726 return NULL;
2729 static const struct got_error *
2730 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2731 const char *refname, int ref_tm)
2733 const struct got_error *error = NULL;
2734 struct got_repository *repo = NULL;
2735 struct got_commit_object *commit = NULL;
2736 struct got_reflist_head refs;
2737 struct got_reflist_entry *re;
2738 time_t committer_time = 0, cmp_time = 0;
2740 *repo_age = NULL;
2741 SIMPLEQ_INIT(&refs);
2743 if (gw_trans->gw_conf->got_show_repo_age == 0)
2744 return NULL;
2746 if (gw_trans->repo)
2747 repo = gw_trans->repo;
2748 else {
2749 error = got_repo_open(&repo, dir, NULL);
2750 if (error)
2751 return error;
2754 error = got_ref_list(&refs, repo, "refs/heads",
2755 got_ref_cmp_by_name, NULL);
2756 if (error)
2757 goto done;
2760 * Find the youngest branch tip in the repository, or the age of
2761 * the a specific branch tip if a name was provided by the caller.
2763 SIMPLEQ_FOREACH(re, &refs, entry) {
2764 struct got_object_id *id = NULL;
2766 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
2767 continue;
2769 error = got_ref_resolve(&id, repo, re->ref);
2770 if (error)
2771 goto done;
2773 error = got_object_open_as_commit(&commit, repo, id);
2774 free(id);
2775 if (error)
2776 goto done;
2778 committer_time =
2779 got_object_commit_get_committer_time(commit);
2780 got_object_commit_close(commit);
2781 if (cmp_time < committer_time)
2782 cmp_time = committer_time;
2784 if (refname)
2785 break;
2788 if (cmp_time != 0) {
2789 committer_time = cmp_time;
2790 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2792 done:
2793 got_ref_list_free(&refs);
2794 if (gw_trans->repo == NULL)
2795 got_repo_close(repo);
2796 return error;
2799 static const struct got_error *
2800 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2802 const struct got_error *error;
2803 FILE *f = NULL;
2804 struct got_object_id *id1 = NULL, *id2 = NULL;
2805 char *label1 = NULL, *label2 = NULL, *line = NULL;
2806 int obj_type;
2807 size_t linesize = 0;
2808 ssize_t linelen;
2809 enum kcgi_err kerr = KCGI_OK;
2811 f = got_opentemp();
2812 if (f == NULL)
2813 return NULL;
2815 if (header->parent_id != NULL &&
2816 strncmp(header->parent_id, "/dev/null", 9) != 0) {
2817 error = got_repo_match_object_id(&id1, &label1,
2818 header->parent_id, GOT_OBJ_TYPE_ANY, 1, gw_trans->repo);
2819 if (error)
2820 goto done;
2823 error = got_repo_match_object_id(&id2, &label2,
2824 header->commit_id, GOT_OBJ_TYPE_ANY, 1, gw_trans->repo);
2825 if (error)
2826 goto done;
2828 error = got_object_get_type(&obj_type, gw_trans->repo, id2);
2829 if (error)
2830 goto done;
2831 switch (obj_type) {
2832 case GOT_OBJ_TYPE_BLOB:
2833 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
2834 gw_trans->repo, f);
2835 break;
2836 case GOT_OBJ_TYPE_TREE:
2837 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
2838 gw_trans->repo, f);
2839 break;
2840 case GOT_OBJ_TYPE_COMMIT:
2841 error = got_diff_objects_as_commits(id1, id2, 3, 0,
2842 gw_trans->repo, f);
2843 break;
2844 default:
2845 error = got_error(GOT_ERR_OBJ_TYPE);
2847 if (error)
2848 goto done;
2850 if (fseek(f, 0, SEEK_SET) == -1) {
2851 error = got_ferror(f, GOT_ERR_IO);
2852 goto done;
2855 while ((linelen = getline(&line, &linesize, f)) != -1) {
2856 error = gw_colordiff_line(gw_trans, line);
2857 if (error)
2858 goto done;
2859 /* XXX: KHTML_PRETTY breaks this */
2860 kerr = khtml_puts(gw_trans->gw_html_req, line);
2861 if (kerr != KCGI_OK)
2862 goto done;
2863 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2864 if (kerr != KCGI_OK)
2865 goto done;
2867 if (linelen == -1 && ferror(f))
2868 error = got_error_from_errno("getline");
2869 done:
2870 if (f && fclose(f) == -1 && error == NULL)
2871 error = got_error_from_errno("fclose");
2872 free(line);
2873 free(label1);
2874 free(label2);
2875 free(id1);
2876 free(id2);
2878 if (error == NULL && kerr != KCGI_OK)
2879 error = gw_kcgi_error(kerr);
2880 return error;
2883 static const struct got_error *
2884 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2886 const struct got_error *error = NULL;
2887 struct got_repository *repo;
2888 const char *gitconfig_owner;
2890 *owner = NULL;
2892 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2893 return NULL;
2895 error = got_repo_open(&repo, dir, NULL);
2896 if (error)
2897 return error;
2898 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2899 if (gitconfig_owner) {
2900 *owner = strdup(gitconfig_owner);
2901 if (*owner == NULL)
2902 error = got_error_from_errno("strdup");
2904 got_repo_close(repo);
2905 return error;
2908 static const struct got_error *
2909 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2911 const struct got_error *error = NULL;
2912 FILE *f;
2913 char *d_file = NULL;
2914 unsigned int len;
2915 size_t n;
2917 *url = NULL;
2919 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2920 return got_error_from_errno("asprintf");
2922 f = fopen(d_file, "r");
2923 if (f == NULL) {
2924 if (errno != ENOENT && errno != EACCES)
2925 error = got_error_from_errno2("fopen", d_file);
2926 goto done;
2929 if (fseek(f, 0, SEEK_END) == -1) {
2930 error = got_ferror(f, GOT_ERR_IO);
2931 goto done;
2933 len = ftell(f);
2934 if (len == -1) {
2935 error = got_ferror(f, GOT_ERR_IO);
2936 goto done;
2938 if (fseek(f, 0, SEEK_SET) == -1) {
2939 error = got_ferror(f, GOT_ERR_IO);
2940 goto done;
2943 *url = calloc(len + 1, sizeof(**url));
2944 if (*url == NULL) {
2945 error = got_error_from_errno("calloc");
2946 goto done;
2949 n = fread(*url, 1, len, f);
2950 if (n == 0 && ferror(f))
2951 error = got_ferror(f, GOT_ERR_IO);
2952 done:
2953 if (f && fclose(f) == -1 && error == NULL)
2954 error = got_error_from_errno("fclose");
2955 free(d_file);
2956 return NULL;
2959 static const struct got_error *
2960 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
2961 int limit, int tag_type)
2963 const struct got_error *error = NULL;
2964 struct got_reflist_head refs;
2965 struct got_reflist_entry *re;
2966 char *age = NULL;
2967 char *id_str = NULL, *newline, *href_commits = NULL;
2968 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
2969 struct got_tag_object *tag = NULL;
2970 enum kcgi_err kerr = KCGI_OK;
2971 int summary_header_displayed = 0, start_tag = 0, chk_next = 0;
2972 int prev_set = 0, tag_count = 0;
2974 SIMPLEQ_INIT(&refs);
2976 error = got_ref_list(&refs, gw_trans->repo, "refs/tags",
2977 got_ref_cmp_tags, gw_trans->repo);
2978 if (error)
2979 goto done;
2981 SIMPLEQ_FOREACH(re, &refs, entry) {
2982 const char *refname;
2983 const char *tagger;
2984 const char *tag_commit;
2985 time_t tagger_time;
2986 struct got_object_id *id;
2987 struct got_commit_object *commit = NULL;
2989 refname = got_ref_get_name(re->ref);
2990 if (strncmp(refname, "refs/tags/", 10) != 0)
2991 continue;
2992 refname += 10;
2994 error = got_ref_resolve(&id, gw_trans->repo, re->ref);
2995 if (error)
2996 goto done;
2998 error = got_object_open_as_tag(&tag, gw_trans->repo, id);
2999 if (error) {
3000 if (error->code != GOT_ERR_OBJ_TYPE) {
3001 free(id);
3002 goto done;
3004 /* "lightweight" tag */
3005 error = got_object_open_as_commit(&commit,
3006 gw_trans->repo, id);
3007 if (error) {
3008 free(id);
3009 goto done;
3011 tagger = got_object_commit_get_committer(commit);
3012 tagger_time =
3013 got_object_commit_get_committer_time(commit);
3014 error = got_object_id_str(&id_str, id);
3015 free(id);
3016 } else {
3017 free(id);
3018 tagger = got_object_tag_get_tagger(tag);
3019 tagger_time = got_object_tag_get_tagger_time(tag);
3020 error = got_object_id_str(&id_str,
3021 got_object_tag_get_object_id(tag));
3023 if (error)
3024 goto done;
3026 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
3027 strlen(id_str)) != 0)
3028 continue;
3030 if (tag_type == TAGBRIEF && gw_trans->commit_id &&
3031 start_tag == 0 && strncmp(id_str, header->commit_id,
3032 strlen(id_str)) != 0) {
3033 continue;
3034 } else {
3035 start_tag = 1;
3038 tag_count++;
3040 if (prev_set == 0 && start_tag == 1) {
3041 gw_trans->next_prev_id = strdup(id_str);
3042 if (gw_trans->next_prev_id == NULL) {
3043 error = got_error_from_errno("strdup");
3044 goto done;
3046 prev_set = 1;
3049 if (chk_next) {
3050 gw_trans->next_id = strdup(id_str);
3051 if (gw_trans->next_id == NULL)
3052 error = got_error_from_errno("strdup");
3053 goto done;
3056 if (commit) {
3057 error = got_object_commit_get_logmsg(&tag_commit0,
3058 commit);
3059 if (error)
3060 goto done;
3061 got_object_commit_close(commit);
3062 } else {
3063 tag_commit0 = strdup(got_object_tag_get_message(tag));
3064 if (tag_commit0 == NULL) {
3065 error = got_error_from_errno("strdup");
3066 goto done;
3070 tag_commit = tag_commit0;
3071 while (*tag_commit == '\n')
3072 tag_commit++;
3074 switch (tag_type) {
3075 case TAGBRIEF:
3076 newline = strchr(tag_commit, '\n');
3077 if (newline)
3078 *newline = '\0';
3080 if (summary_header_displayed == 0) {
3081 kerr = khtml_attr(gw_trans->gw_html_req,
3082 KELEM_DIV, KATTR_ID,
3083 "summary_tags_title_wrapper", KATTR__MAX);
3084 if (kerr != KCGI_OK)
3085 goto done;
3086 kerr = khtml_attr(gw_trans->gw_html_req,
3087 KELEM_DIV, KATTR_ID,
3088 "summary_tags_title", KATTR__MAX);
3089 if (kerr != KCGI_OK)
3090 goto done;
3091 kerr = khtml_puts(gw_trans->gw_html_req,
3092 "Tags");
3093 if (kerr != KCGI_OK)
3094 goto done;
3095 kerr = khtml_closeelem(gw_trans->gw_html_req,
3096 2);
3097 if (kerr != KCGI_OK)
3098 goto done;
3099 kerr = khtml_attr(gw_trans->gw_html_req,
3100 KELEM_DIV, KATTR_ID,
3101 "summary_tags_content", KATTR__MAX);
3102 if (kerr != KCGI_OK)
3103 goto done;
3104 summary_header_displayed = 1;
3107 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3108 KATTR_ID, "tag_wrapper", KATTR__MAX);
3109 if (kerr != KCGI_OK)
3110 goto done;
3111 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3112 KATTR_ID, "tag_age", KATTR__MAX);
3113 if (kerr != KCGI_OK)
3114 goto done;
3115 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
3116 if (error)
3117 goto done;
3118 kerr = khtml_puts(gw_trans->gw_html_req,
3119 age ? age : "");
3120 if (kerr != KCGI_OK)
3121 goto done;
3122 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3123 if (kerr != KCGI_OK)
3124 goto done;
3125 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3126 KATTR_ID, "tag", KATTR__MAX);
3127 if (kerr != KCGI_OK)
3128 goto done;
3129 kerr = khtml_puts(gw_trans->gw_html_req, refname);
3130 if (kerr != KCGI_OK)
3131 goto done;
3132 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3133 if (kerr != KCGI_OK)
3134 goto done;
3135 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3136 KATTR_ID, "tag_name", KATTR__MAX);
3137 if (kerr != KCGI_OK)
3138 goto done;
3139 if (asprintf(&href_tag, "?path=%s&action=tag&commit=%s",
3140 gw_trans->repo_name, id_str) == -1) {
3141 error = got_error_from_errno("asprintf");
3142 goto done;
3144 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3145 KATTR_HREF, href_tag, KATTR__MAX);
3146 if (kerr != KCGI_OK)
3147 goto done;
3148 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
3149 if (kerr != KCGI_OK)
3150 goto done;
3151 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3152 if (kerr != KCGI_OK)
3153 goto done;
3155 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3156 KATTR_ID, "navs_wrapper", KATTR__MAX);
3157 if (kerr != KCGI_OK)
3158 goto done;
3159 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3160 KATTR_ID, "navs", KATTR__MAX);
3161 if (kerr != KCGI_OK)
3162 goto done;
3164 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3165 KATTR_HREF, href_tag, KATTR__MAX);
3166 if (kerr != KCGI_OK)
3167 goto done;
3168 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
3169 if (kerr != KCGI_OK)
3170 goto done;
3171 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3172 if (kerr != KCGI_OK)
3173 goto done;
3175 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3176 if (kerr != KCGI_OK)
3177 goto done;
3178 if (asprintf(&href_briefs,
3179 "?path=%s&action=briefs&commit=%s",
3180 gw_trans->repo_name, id_str) == -1) {
3181 error = got_error_from_errno("asprintf");
3182 goto done;
3184 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3185 KATTR_HREF, href_briefs, KATTR__MAX);
3186 if (kerr != KCGI_OK)
3187 goto done;
3188 kerr = khtml_puts(gw_trans->gw_html_req,
3189 "commit briefs");
3190 if (kerr != KCGI_OK)
3191 goto done;
3192 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3193 if (kerr != KCGI_OK)
3194 goto done;
3196 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3197 if (kerr != KCGI_OK)
3198 goto done;
3200 if (asprintf(&href_commits,
3201 "?path=%s&action=commits&commit=%s",
3202 gw_trans->repo_name, id_str) == -1) {
3203 error = got_error_from_errno("asprintf");
3204 goto done;
3206 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3207 KATTR_HREF, href_commits, KATTR__MAX);
3208 if (kerr != KCGI_OK)
3209 goto done;
3210 kerr = khtml_puts(gw_trans->gw_html_req,
3211 "commits");
3212 if (kerr != KCGI_OK)
3213 goto done;
3214 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3215 if (kerr != KCGI_OK)
3216 goto done;
3218 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3219 KATTR_ID, "dotted_line", KATTR__MAX);
3220 if (kerr != KCGI_OK)
3221 goto done;
3222 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3223 if (kerr != KCGI_OK)
3224 goto done;
3225 break;
3226 case TAGFULL:
3227 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3228 KATTR_ID, "tag_info_date_title", KATTR__MAX);
3229 if (kerr != KCGI_OK)
3230 goto done;
3231 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
3232 if (kerr != KCGI_OK)
3233 goto done;
3234 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3235 if (kerr != KCGI_OK)
3236 goto done;
3237 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3238 KATTR_ID, "tag_info_date", KATTR__MAX);
3239 if (kerr != KCGI_OK)
3240 goto done;
3241 error = gw_get_time_str(&age, tagger_time, TM_LONG);
3242 if (error)
3243 goto done;
3244 kerr = khtml_puts(gw_trans->gw_html_req,
3245 age ? age : "");
3246 if (kerr != KCGI_OK)
3247 goto done;
3248 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3249 if (kerr != KCGI_OK)
3250 goto done;
3252 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3253 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
3254 if (kerr != KCGI_OK)
3255 goto done;
3256 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
3257 if (kerr != KCGI_OK)
3258 goto done;
3259 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3260 if (kerr != KCGI_OK)
3261 goto done;
3262 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3263 KATTR_ID, "tag_info_date", KATTR__MAX);
3264 if (kerr != KCGI_OK)
3265 goto done;
3266 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
3267 if (kerr != KCGI_OK)
3268 goto done;
3269 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3270 if (kerr != KCGI_OK)
3271 goto done;
3273 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3274 KATTR_ID, "tag_info", KATTR__MAX);
3275 if (kerr != KCGI_OK)
3276 goto done;
3277 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
3278 if (kerr != KCGI_OK)
3279 goto done;
3280 break;
3281 default:
3282 break;
3284 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3285 if (kerr != KCGI_OK)
3286 goto done;
3288 if (limit && --limit == 0)
3289 chk_next = 1;
3291 if (tag)
3292 got_object_tag_close(tag);
3293 tag = NULL;
3294 free(id_str);
3295 id_str = NULL;
3296 free(age);
3297 age = NULL;
3298 free(tag_commit0);
3299 tag_commit0 = NULL;
3300 free(href_tag);
3301 href_tag = NULL;
3302 free(href_briefs);
3303 href_briefs = NULL;
3304 free(href_commits);
3305 href_commits = NULL;
3307 if (tag_count == 0) {
3308 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3309 "summary_tags_title_wrapper", KATTR__MAX);
3310 if (kerr != KCGI_OK)
3311 goto done;
3312 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3313 "summary_tags_title", KATTR__MAX);
3314 if (kerr != KCGI_OK)
3315 goto done;
3316 kerr = khtml_puts(gw_trans->gw_html_req, "Tags");
3317 if (kerr != KCGI_OK)
3318 goto done;
3319 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3320 if (kerr != KCGI_OK)
3321 goto done;
3322 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3323 "summary_tags_content", KATTR__MAX);
3324 if (kerr != KCGI_OK)
3325 goto done;
3326 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3327 "tags_info", KATTR__MAX);
3328 if (kerr != KCGI_OK)
3329 goto done;
3330 kerr = khttp_puts(gw_trans->gw_req,
3331 "There are no tags for this repo.");
3332 if (kerr != KCGI_OK)
3333 goto done;
3334 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3335 if (kerr != KCGI_OK)
3336 goto done;
3338 done:
3339 if (tag)
3340 got_object_tag_close(tag);
3341 free(id_str);
3342 free(age);
3343 free(tag_commit0);
3344 free(href_tag);
3345 free(href_briefs);
3346 free(href_commits);
3347 got_ref_list_free(&refs);
3348 if (error == NULL && kerr != KCGI_OK)
3349 error = gw_kcgi_error(kerr);
3350 return error;
3353 static void
3354 gw_free_header(struct gw_header *header)
3356 free(header->path);
3357 free(header->author);
3358 free(header->committer);
3359 free(header->refs_str);
3360 free(header->commit_id);
3361 free(header->parent_id);
3362 free(header->tree_id);
3363 free(header->commit_msg);
3366 static struct gw_header *
3367 gw_init_header()
3369 struct gw_header *header;
3371 header = malloc(sizeof(*header));
3372 if (header == NULL)
3373 return NULL;
3375 header->path = NULL;
3376 SIMPLEQ_INIT(&header->refs);
3378 header->refs_str = NULL;
3379 header->commit_id = NULL;
3380 header->committer = NULL;
3381 header->author = NULL;
3382 header->parent_id = NULL;
3383 header->tree_id = NULL;
3384 header->commit_msg = NULL;
3386 return header;
3389 static const struct got_error *
3390 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
3391 int limit, struct got_object_id *id)
3393 const struct got_error *error = NULL;
3394 struct got_commit_graph *graph = NULL;
3395 struct got_commit_object *commit = NULL;
3396 int chk_next = 0, chk_multi = 0, prev_set = 0;
3398 error = got_commit_graph_open(&graph, header->path, 0);
3399 if (error)
3400 return error;
3402 error = got_commit_graph_iter_start(graph, id, gw_trans->repo, NULL,
3403 NULL);
3404 if (error)
3405 goto done;
3407 for (;;) {
3408 error = got_commit_graph_iter_next(&id, graph, gw_trans->repo,
3409 NULL, NULL);
3410 if (error) {
3411 if (error->code == GOT_ERR_ITER_COMPLETED)
3412 error = NULL;
3413 goto done;
3415 if (id == NULL)
3416 goto done;
3418 error = got_object_open_as_commit(&commit, gw_trans->repo, id);
3419 if (error)
3420 goto done;
3421 if (limit == 1 && chk_multi == 0 &&
3422 gw_trans->gw_conf->got_max_commits_display != 1) {
3423 error = gw_get_commit(gw_trans, header, commit, id);
3424 if (error)
3425 goto done;
3426 } else {
3427 chk_multi = 1;
3428 struct gw_header *n_header = NULL;
3429 if ((n_header = gw_init_header()) == NULL) {
3430 error = got_error_from_errno("malloc");
3431 goto done;
3433 error = got_ref_list(&n_header->refs, gw_trans->repo,
3434 NULL, got_ref_cmp_by_name, NULL);
3435 if (error)
3436 goto done;
3438 error = gw_get_commit(gw_trans, n_header, commit, id);
3439 if (error)
3440 goto done;
3441 got_ref_list_free(&n_header->refs);
3444 * we have a commit_id now, so copy it to next_prev_id
3445 * for navigation through gw_briefs and gw_commits
3447 if (gw_trans->next_prev_id == NULL && prev_set == 0 &&
3448 (gw_trans->action == GW_BRIEFS ||
3449 gw_trans->action == GW_COMMITS ||
3450 gw_trans->action == GW_SUMMARY)) {
3451 prev_set = 1;
3452 gw_trans->next_prev_id =
3453 strdup(n_header->commit_id);
3454 if (gw_trans->next_prev_id == NULL) {
3455 error = got_error_from_errno("strdup");
3456 goto done;
3461 * check for one more commit before breaking,
3462 * so we know whether to navicate through gw_briefs
3463 * gw_commits and gw_summary
3465 if (chk_next && (gw_trans->action == GW_BRIEFS||
3466 gw_trans->action == GW_COMMITS ||
3467 gw_trans->action == GW_SUMMARY)) {
3468 gw_trans->next_id = strdup(n_header->commit_id);
3469 if (gw_trans->next_id == NULL)
3470 error = got_error_from_errno("strdup");
3471 goto done;
3474 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3475 entry);
3477 if (error || (limit && --limit == 0)) {
3478 if (chk_multi == 0)
3479 break;
3480 chk_next = 1;
3483 done:
3484 if (commit != NULL)
3485 got_object_commit_close(commit);
3486 if (graph)
3487 got_commit_graph_close(graph);
3488 return error;
3491 static const struct got_error *
3492 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header,
3493 struct got_commit_object *commit, struct got_object_id *id)
3495 const struct got_error *error = NULL;
3496 struct got_reflist_entry *re;
3497 struct got_object_id *id2 = NULL;
3498 struct got_object_qid *parent_id;
3499 char *commit_msg = NULL, *commit_msg0;
3501 /*print commit*/
3502 SIMPLEQ_FOREACH(re, &header->refs, entry) {
3503 char *s;
3504 const char *name;
3505 struct got_tag_object *tag = NULL;
3506 int cmp;
3508 if (got_ref_is_symbolic(re->ref))
3509 continue;
3511 name = got_ref_get_name(re->ref);
3512 if (strncmp(name, "refs/", 5) == 0)
3513 name += 5;
3514 if (strncmp(name, "got/", 4) == 0)
3515 continue;
3516 if (strncmp(name, "heads/", 6) == 0)
3517 name += 6;
3518 if (strncmp(name, "remotes/", 8) == 0)
3519 name += 8;
3520 if (strncmp(name, "tags/", 5) == 0) {
3521 error = got_object_open_as_tag(&tag, gw_trans->repo,
3522 re->id);
3523 if (error) {
3524 if (error->code != GOT_ERR_OBJ_TYPE)
3525 continue;
3527 * Ref points at something other
3528 * than a tag.
3530 error = NULL;
3531 tag = NULL;
3534 cmp = got_object_id_cmp(tag ?
3535 got_object_tag_get_object_id(tag) : re->id, id);
3536 if (tag)
3537 got_object_tag_close(tag);
3538 if (cmp != 0)
3539 continue;
3540 s = header->refs_str;
3541 if (asprintf(&header->refs_str, "%s%s%s", s ? s : "",
3542 s ? ", " : "", name) == -1) {
3543 error = got_error_from_errno("asprintf");
3544 free(s);
3545 header->refs_str = NULL;
3546 return error;
3548 free(s);
3551 error = got_object_id_str(&header->commit_id, id);
3552 if (error)
3553 return error;
3555 error = got_object_id_str(&header->tree_id,
3556 got_object_commit_get_tree_id(commit));
3557 if (error)
3558 return error;
3560 if (gw_trans->action == GW_DIFF) {
3561 parent_id = SIMPLEQ_FIRST(
3562 got_object_commit_get_parent_ids(commit));
3563 if (parent_id != NULL) {
3564 id2 = got_object_id_dup(parent_id->id);
3565 free (parent_id);
3566 error = got_object_id_str(&header->parent_id, id2);
3567 if (error)
3568 return error;
3569 free(id2);
3570 } else {
3571 header->parent_id = strdup("/dev/null");
3572 if (header->parent_id == NULL) {
3573 error = got_error_from_errno("strdup");
3574 return error;
3579 header->committer_time =
3580 got_object_commit_get_committer_time(commit);
3582 header->author =
3583 strdup(got_object_commit_get_author(commit));
3584 if (header->author == NULL) {
3585 error = got_error_from_errno("strdup");
3586 return error;
3588 header->committer =
3589 strdup(got_object_commit_get_committer(commit));
3590 if (header->committer == NULL) {
3591 error = got_error_from_errno("strdup");
3592 return error;
3594 error = got_object_commit_get_logmsg(&commit_msg0, commit);
3595 if (error)
3596 return error;
3598 commit_msg = commit_msg0;
3599 while (*commit_msg == '\n')
3600 commit_msg++;
3602 header->commit_msg = strdup(commit_msg);
3603 if (header->commit_msg == NULL)
3604 error = got_error_from_errno("strdup");
3605 free(commit_msg0);
3606 return error;
3609 static const struct got_error *
3610 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3612 const struct got_error *error = NULL;
3613 char *in_repo_path = NULL;
3614 struct got_object_id *id = NULL;
3616 error = got_repo_open(&gw_trans->repo, gw_trans->repo_path, NULL);
3617 if (error)
3618 return error;
3620 if (gw_trans->commit_id == NULL) {
3621 struct got_reference *head_ref;
3622 error = got_ref_open(&head_ref, gw_trans->repo,
3623 gw_trans->headref, 0);
3624 if (error)
3625 return error;
3627 error = got_ref_resolve(&id, gw_trans->repo, head_ref);
3628 got_ref_close(head_ref);
3629 if (error)
3630 return error;
3631 } else {
3632 struct got_reference *ref;
3634 error = got_ref_open(&ref, gw_trans->repo,
3635 gw_trans->commit_id, 0);
3636 if (error == NULL) {
3637 int obj_type;
3638 error = got_ref_resolve(&id, gw_trans->repo, ref);
3639 got_ref_close(ref);
3640 if (error)
3641 return error;
3642 error = got_object_get_type(&obj_type, gw_trans->repo,
3643 id);
3644 if (error)
3645 goto done;
3646 if (obj_type == GOT_OBJ_TYPE_TAG) {
3647 struct got_tag_object *tag;
3648 error = got_object_open_as_tag(&tag,
3649 gw_trans->repo, id);
3650 if (error)
3651 goto done;
3652 if (got_object_tag_get_object_type(tag) !=
3653 GOT_OBJ_TYPE_COMMIT) {
3654 got_object_tag_close(tag);
3655 error = got_error(GOT_ERR_OBJ_TYPE);
3656 goto done;
3658 free(id);
3659 id = got_object_id_dup(
3660 got_object_tag_get_object_id(tag));
3661 if (id == NULL)
3662 error = got_error_from_errno(
3663 "got_object_id_dup");
3664 got_object_tag_close(tag);
3665 if (error)
3666 goto done;
3667 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3668 error = got_error(GOT_ERR_OBJ_TYPE);
3669 goto done;
3672 error = got_repo_match_object_id_prefix(&id,
3673 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT,
3674 gw_trans->repo);
3675 if (error)
3676 goto done;
3679 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
3680 gw_trans->repo_path, 1);
3681 if (error)
3682 goto done;
3684 if (in_repo_path) {
3685 header->path = strdup(in_repo_path);
3686 if (header->path == NULL) {
3687 error = got_error_from_errno("strdup");
3688 goto done;
3692 error = got_ref_list(&header->refs, gw_trans->repo, NULL,
3693 got_ref_cmp_by_name, NULL);
3694 if (error)
3695 goto done;
3697 error = gw_get_commits(gw_trans, header, limit, id);
3698 done:
3699 got_ref_list_free(&header->refs);
3700 free(id);
3701 free(in_repo_path);
3702 return error;
3705 struct blame_line {
3706 int annotated;
3707 char *id_str;
3708 char *committer;
3709 char datebuf[11]; /* YYYY-MM-DD + NUL */
3712 struct gw_blame_cb_args {
3713 struct blame_line *lines;
3714 int nlines;
3715 int nlines_prec;
3716 int lineno_cur;
3717 off_t *line_offsets;
3718 FILE *f;
3719 struct got_repository *repo;
3720 struct gw_trans *gw_trans;
3723 static const struct got_error *
3724 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3726 const struct got_error *err = NULL;
3727 struct gw_blame_cb_args *a = arg;
3728 struct blame_line *bline;
3729 char *line = NULL;
3730 size_t linesize = 0;
3731 struct got_commit_object *commit = NULL;
3732 off_t offset;
3733 struct tm tm;
3734 time_t committer_time;
3735 enum kcgi_err kerr = KCGI_OK;
3737 if (nlines != a->nlines ||
3738 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3739 return got_error(GOT_ERR_RANGE);
3741 if (lineno == -1)
3742 return NULL; /* no change in this commit */
3744 /* Annotate this line. */
3745 bline = &a->lines[lineno - 1];
3746 if (bline->annotated)
3747 return NULL;
3748 err = got_object_id_str(&bline->id_str, id);
3749 if (err)
3750 return err;
3752 err = got_object_open_as_commit(&commit, a->repo, id);
3753 if (err)
3754 goto done;
3756 bline->committer = strdup(got_object_commit_get_committer(commit));
3757 if (bline->committer == NULL) {
3758 err = got_error_from_errno("strdup");
3759 goto done;
3762 committer_time = got_object_commit_get_committer_time(commit);
3763 if (localtime_r(&committer_time, &tm) == NULL)
3764 return got_error_from_errno("localtime_r");
3765 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3766 &tm) >= sizeof(bline->datebuf)) {
3767 err = got_error(GOT_ERR_NO_SPACE);
3768 goto done;
3770 bline->annotated = 1;
3772 /* Print lines annotated so far. */
3773 bline = &a->lines[a->lineno_cur - 1];
3774 if (!bline->annotated)
3775 goto done;
3777 offset = a->line_offsets[a->lineno_cur - 1];
3778 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3779 err = got_error_from_errno("fseeko");
3780 goto done;
3783 while (bline->annotated) {
3784 char *smallerthan, *at, *nl, *committer;
3785 char *lineno = NULL, *href_diff = NULL, *href_link = NULL;
3786 size_t len;
3788 if (getline(&line, &linesize, a->f) == -1) {
3789 if (ferror(a->f))
3790 err = got_error_from_errno("getline");
3791 break;
3794 committer = bline->committer;
3795 smallerthan = strchr(committer, '<');
3796 if (smallerthan && smallerthan[1] != '\0')
3797 committer = smallerthan + 1;
3798 at = strchr(committer, '@');
3799 if (at)
3800 *at = '\0';
3801 len = strlen(committer);
3802 if (len >= 9)
3803 committer[8] = '\0';
3805 nl = strchr(line, '\n');
3806 if (nl)
3807 *nl = '\0';
3809 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3810 "blame_wrapper", KATTR__MAX);
3811 if (kerr != KCGI_OK)
3812 goto err;
3813 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3814 "blame_number", KATTR__MAX);
3815 if (kerr != KCGI_OK)
3816 goto err;
3817 if (asprintf(&lineno, "%.*d", a->nlines_prec,
3818 a->lineno_cur) == -1)
3819 goto err;
3820 kerr = khtml_puts(a->gw_trans->gw_html_req, lineno);
3821 if (kerr != KCGI_OK)
3822 goto err;
3823 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3824 if (kerr != KCGI_OK)
3825 goto err;
3827 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3828 "blame_hash", KATTR__MAX);
3829 if (kerr != KCGI_OK)
3830 goto err;
3832 if (asprintf(&href_diff,
3833 "?path=%s&action=diff&commit=%s",
3834 a->gw_trans->repo_name, bline->id_str) == -1) {
3835 err = got_error_from_errno("asprintf");
3836 goto err;
3838 if (asprintf(&href_link, "%.8s", bline->id_str) == -1) {
3839 err = got_error_from_errno("asprintf");
3840 goto err;
3842 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3843 KATTR_HREF, href_diff, KATTR__MAX);
3844 if (kerr != KCGI_OK)
3845 goto done;
3846 kerr = khtml_puts(a->gw_trans->gw_html_req, href_link);
3847 if (kerr != KCGI_OK)
3848 goto err;
3849 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3850 if (kerr != KCGI_OK)
3851 goto err;
3853 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3854 "blame_date", KATTR__MAX);
3855 if (kerr != KCGI_OK)
3856 goto err;
3857 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3858 if (kerr != KCGI_OK)
3859 goto err;
3860 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3861 if (kerr != KCGI_OK)
3862 goto err;
3864 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3865 "blame_author", KATTR__MAX);
3866 if (kerr != KCGI_OK)
3867 goto err;
3868 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
3869 if (kerr != KCGI_OK)
3870 goto err;
3871 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3872 if (kerr != KCGI_OK)
3873 goto err;
3875 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3876 "blame_code", KATTR__MAX);
3877 if (kerr != KCGI_OK)
3878 goto err;
3879 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
3880 if (kerr != KCGI_OK)
3881 goto err;
3882 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3883 if (kerr != KCGI_OK)
3884 goto err;
3886 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3887 if (kerr != KCGI_OK)
3888 goto err;
3890 a->lineno_cur++;
3891 bline = &a->lines[a->lineno_cur - 1];
3892 err:
3893 free(lineno);
3894 free(href_diff);
3895 free(href_link);
3897 done:
3898 if (commit)
3899 got_object_commit_close(commit);
3900 free(line);
3901 if (err == NULL && kerr != KCGI_OK)
3902 err = gw_kcgi_error(kerr);
3903 return err;
3906 static const struct got_error *
3907 gw_output_file_blame(struct gw_trans *gw_trans)
3909 const struct got_error *error = NULL;
3910 struct got_object_id *obj_id = NULL;
3911 struct got_object_id *commit_id = NULL;
3912 struct got_blob_object *blob = NULL;
3913 char *path = NULL, *in_repo_path = NULL;
3914 struct gw_blame_cb_args bca;
3915 int i, obj_type;
3916 size_t filesize;
3918 if (asprintf(&path, "%s%s%s",
3919 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3920 gw_trans->repo_folder ? "/" : "",
3921 gw_trans->repo_file) == -1) {
3922 error = got_error_from_errno("asprintf");
3923 goto done;
3926 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path, 1);
3927 if (error)
3928 goto done;
3930 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
3931 GOT_OBJ_TYPE_COMMIT, 1, gw_trans->repo);
3932 if (error)
3933 goto done;
3935 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
3936 in_repo_path);
3937 if (error)
3938 goto done;
3940 if (obj_id == NULL) {
3941 error = got_error(GOT_ERR_NO_OBJ);
3942 goto done;
3945 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
3946 if (error)
3947 goto done;
3949 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3950 error = got_error(GOT_ERR_OBJ_TYPE);
3951 goto done;
3954 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
3955 if (error)
3956 goto done;
3958 bca.f = got_opentemp();
3959 if (bca.f == NULL) {
3960 error = got_error_from_errno("got_opentemp");
3961 goto done;
3963 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3964 &bca.line_offsets, bca.f, blob);
3965 if (error || bca.nlines == 0)
3966 goto done;
3968 /* Don't include \n at EOF in the blame line count. */
3969 if (bca.line_offsets[bca.nlines - 1] == filesize)
3970 bca.nlines--;
3972 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3973 if (bca.lines == NULL) {
3974 error = got_error_from_errno("calloc");
3975 goto done;
3977 bca.lineno_cur = 1;
3978 bca.nlines_prec = 0;
3979 i = bca.nlines;
3980 while (i > 0) {
3981 i /= 10;
3982 bca.nlines_prec++;
3984 bca.repo = gw_trans->repo;
3985 bca.gw_trans = gw_trans;
3987 error = got_blame(in_repo_path, commit_id, gw_trans->repo, gw_blame_cb,
3988 &bca, NULL, NULL);
3989 done:
3990 free(in_repo_path);
3991 free(commit_id);
3992 free(obj_id);
3993 free(path);
3995 if (blob) {
3996 free(bca.line_offsets);
3997 for (i = 0; i < bca.nlines; i++) {
3998 struct blame_line *bline = &bca.lines[i];
3999 free(bline->id_str);
4000 free(bline->committer);
4002 free(bca.lines);
4003 if (bca.f && fclose(bca.f) == EOF && error == NULL)
4004 error = got_error_from_errno("fclose");
4006 if (blob)
4007 got_object_blob_close(blob);
4008 return error;
4011 static const struct got_error *
4012 gw_output_blob_buf(struct gw_trans *gw_trans)
4014 const struct got_error *error = NULL;
4015 struct got_object_id *obj_id = NULL;
4016 struct got_object_id *commit_id = NULL;
4017 struct got_blob_object *blob = NULL;
4018 char *path = NULL, *in_repo_path = NULL;
4019 int obj_type, set_mime = 0;
4020 size_t len, hdrlen;
4021 const uint8_t *buf;
4022 enum kcgi_err kerr = KCGI_OK;
4024 if (asprintf(&path, "%s%s%s",
4025 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4026 gw_trans->repo_folder ? "/" : "",
4027 gw_trans->repo_file) == -1) {
4028 error = got_error_from_errno("asprintf");
4029 goto done;
4032 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path, 1);
4033 if (error)
4034 goto done;
4036 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
4037 GOT_OBJ_TYPE_COMMIT, 1, gw_trans->repo);
4038 if (error)
4039 goto done;
4041 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
4042 in_repo_path);
4043 if (error)
4044 goto done;
4046 if (obj_id == NULL) {
4047 error = got_error(GOT_ERR_NO_OBJ);
4048 goto done;
4051 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
4052 if (error)
4053 goto done;
4055 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4056 error = got_error(GOT_ERR_OBJ_TYPE);
4057 goto done;
4060 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
4061 if (error)
4062 goto done;
4064 hdrlen = got_object_blob_get_hdrlen(blob);
4065 do {
4066 error = got_object_blob_read_block(&len, blob);
4067 if (error)
4068 goto done;
4069 buf = got_object_blob_get_read_buf(blob);
4072 * Skip blob object header first time around,
4073 * which also contains a zero byte.
4075 buf += hdrlen;
4076 if (set_mime == 0) {
4077 if (isbinary(buf, len - hdrlen))
4078 gw_trans->mime = KMIME_APP_OCTET_STREAM;
4079 else
4080 gw_trans->mime = KMIME_TEXT_PLAIN;
4081 set_mime = 1;
4082 error = gw_display_index(gw_trans);
4083 if (error)
4084 goto done;
4086 kerr = khttp_write(gw_trans->gw_req, buf, len - hdrlen);
4087 if (kerr != KCGI_OK)
4088 goto done;
4089 hdrlen = 0;
4090 } while (len != 0);
4091 done:
4092 free(in_repo_path);
4093 free(commit_id);
4094 free(obj_id);
4095 free(path);
4096 if (blob)
4097 got_object_blob_close(blob);
4098 if (error == NULL && kerr != KCGI_OK)
4099 error = gw_kcgi_error(kerr);
4100 return error;
4103 static const struct got_error *
4104 gw_output_repo_tree(struct gw_trans *gw_trans)
4106 const struct got_error *error = NULL;
4107 struct got_object_id *tree_id = NULL, *commit_id = NULL;
4108 struct got_tree_object *tree = NULL;
4109 char *path = NULL, *in_repo_path = NULL;
4110 char *id_str = NULL;
4111 char *build_folder = NULL;
4112 char *href_blob = NULL, *href_blame = NULL;
4113 const char *class = NULL;
4114 int nentries, i, class_flip = 0;
4115 enum kcgi_err kerr = KCGI_OK;
4117 if (gw_trans->repo_folder != NULL) {
4118 path = strdup(gw_trans->repo_folder);
4119 if (path == NULL) {
4120 error = got_error_from_errno("strdup");
4121 goto done;
4123 } else {
4124 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
4125 gw_trans->repo_path, 1);
4126 if (error)
4127 goto done;
4128 free(path);
4129 path = in_repo_path;
4132 if (gw_trans->commit_id == NULL) {
4133 struct got_reference *head_ref;
4134 error = got_ref_open(&head_ref, gw_trans->repo,
4135 gw_trans->headref, 0);
4136 if (error)
4137 goto done;
4138 error = got_ref_resolve(&commit_id, gw_trans->repo, head_ref);
4139 if (error)
4140 goto done;
4141 got_ref_close(head_ref);
4143 * gw_trans->commit_id was not parsed from the querystring
4144 * we hit this code path from gw_index, where we don't know the
4145 * commit values for the tree link yet, so set
4146 * gw_trans->commit_id here to continue further into the tree
4148 error = got_object_id_str(&gw_trans->commit_id, commit_id);
4149 if (error)
4150 goto done;
4152 } else {
4153 error = got_repo_match_object_id(&commit_id, NULL,
4154 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT, 1,
4155 gw_trans->repo);
4156 if (error)
4157 goto done;
4160 error = got_object_id_by_path(&tree_id, gw_trans->repo, commit_id,
4161 path);
4162 if (error)
4163 goto done;
4165 error = got_object_open_as_tree(&tree, gw_trans->repo, tree_id);
4166 if (error)
4167 goto done;
4169 nentries = got_object_tree_get_nentries(tree);
4170 for (i = 0; i < nentries; i++) {
4171 struct got_tree_entry *te;
4172 const char *modestr = "";
4173 mode_t mode;
4175 te = got_object_tree_get_entry(tree, i);
4177 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
4178 if (error)
4179 goto done;
4181 mode = got_tree_entry_get_mode(te);
4182 if (got_object_tree_entry_is_submodule(te))
4183 modestr = "$";
4184 else if (S_ISLNK(mode))
4185 modestr = "@";
4186 else if (S_ISDIR(mode))
4187 modestr = "/";
4188 else if (mode & S_IXUSR)
4189 modestr = "*";
4191 if (class_flip == 0) {
4192 class = "back_lightgray";
4193 class_flip = 1;
4194 } else {
4195 class = "back_white";
4196 class_flip = 0;
4199 if (S_ISDIR(mode)) {
4200 if (asprintf(&build_folder, "%s/%s",
4201 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4202 got_tree_entry_get_name(te)) == -1) {
4203 error = got_error_from_errno("asprintf");
4204 goto done;
4206 if (asprintf(&href_blob,
4207 "?path=%s&action=%s&commit=%s&folder=%s",
4208 gw_trans->repo_name, gw_get_action_name(gw_trans),
4209 gw_trans->commit_id, build_folder) == -1) {
4210 error = got_error_from_errno("asprintf");
4211 goto done;
4214 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4215 KATTR_ID, "tree_wrapper", KATTR__MAX);
4216 if (kerr != KCGI_OK)
4217 goto done;
4218 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4219 KATTR_ID, "tree_line", KATTR_CLASS, class,
4220 KATTR__MAX);
4221 if (kerr != KCGI_OK)
4222 goto done;
4223 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4224 KATTR_HREF, href_blob, KATTR_CLASS,
4225 "diff_directory", KATTR__MAX);
4226 if (kerr != KCGI_OK)
4227 goto done;
4228 kerr = khtml_puts(gw_trans->gw_html_req,
4229 got_tree_entry_get_name(te));
4230 if (kerr != KCGI_OK)
4231 goto done;
4232 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
4233 if (kerr != KCGI_OK)
4234 goto done;
4235 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4236 if (kerr != KCGI_OK)
4237 goto done;
4238 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4239 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
4240 KATTR__MAX);
4241 if (kerr != KCGI_OK)
4242 goto done;
4243 kerr = khtml_entity(gw_trans->gw_html_req,
4244 KENTITY_nbsp);
4245 if (kerr != KCGI_OK)
4246 goto done;
4247 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4248 if (kerr != KCGI_OK)
4249 goto done;
4250 } else {
4251 if (asprintf(&href_blob,
4252 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
4253 gw_trans->repo_name, "blob", gw_trans->commit_id,
4254 got_tree_entry_get_name(te),
4255 gw_trans->repo_folder ?
4256 gw_trans->repo_folder : "") == -1) {
4257 error = got_error_from_errno("asprintf");
4258 goto done;
4260 if (asprintf(&href_blame,
4261 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
4262 gw_trans->repo_name, "blame", gw_trans->commit_id,
4263 got_tree_entry_get_name(te),
4264 gw_trans->repo_folder ?
4265 gw_trans->repo_folder : "") == -1) {
4266 error = got_error_from_errno("asprintf");
4267 goto done;
4270 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4271 KATTR_ID, "tree_wrapper", KATTR__MAX);
4272 if (kerr != KCGI_OK)
4273 goto done;
4274 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4275 KATTR_ID, "tree_line", KATTR_CLASS, class,
4276 KATTR__MAX);
4277 if (kerr != KCGI_OK)
4278 goto done;
4279 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4280 KATTR_HREF, href_blob, KATTR__MAX);
4281 if (kerr != KCGI_OK)
4282 goto done;
4283 kerr = khtml_puts(gw_trans->gw_html_req,
4284 got_tree_entry_get_name(te));
4285 if (kerr != KCGI_OK)
4286 goto done;
4287 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
4288 if (kerr != KCGI_OK)
4289 goto done;
4290 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4291 if (kerr != KCGI_OK)
4292 goto done;
4293 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4294 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
4295 KATTR__MAX);
4296 if (kerr != KCGI_OK)
4297 goto done;
4299 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4300 KATTR_HREF, href_blob, KATTR__MAX);
4301 if (kerr != KCGI_OK)
4302 goto done;
4303 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
4304 if (kerr != KCGI_OK)
4305 goto done;
4306 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4307 if (kerr != KCGI_OK)
4308 goto done;
4310 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4311 if (kerr != KCGI_OK)
4312 goto done;
4314 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4315 KATTR_HREF, href_blame, KATTR__MAX);
4316 if (kerr != KCGI_OK)
4317 goto done;
4318 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
4319 if (kerr != KCGI_OK)
4320 goto done;
4322 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4323 if (kerr != KCGI_OK)
4324 goto done;
4326 free(id_str);
4327 id_str = NULL;
4328 free(href_blob);
4329 href_blob = NULL;
4330 free(build_folder);
4331 build_folder = NULL;
4333 done:
4334 if (tree)
4335 got_object_tree_close(tree);
4336 free(id_str);
4337 free(href_blob);
4338 free(href_blame);
4339 free(in_repo_path);
4340 free(tree_id);
4341 free(build_folder);
4342 if (error == NULL && kerr != KCGI_OK)
4343 error = gw_kcgi_error(kerr);
4344 return error;
4347 static const struct got_error *
4348 gw_output_repo_heads(struct gw_trans *gw_trans)
4350 const struct got_error *error = NULL;
4351 struct got_reflist_head refs;
4352 struct got_reflist_entry *re;
4353 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
4354 char *href_commits = NULL;
4355 enum kcgi_err kerr = KCGI_OK;
4357 SIMPLEQ_INIT(&refs);
4359 error = got_ref_list(&refs, gw_trans->repo, "refs/heads",
4360 got_ref_cmp_by_name, NULL);
4361 if (error)
4362 goto done;
4364 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4365 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
4366 if (kerr != KCGI_OK)
4367 goto done;
4368 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4369 KATTR_ID, "summary_heads_title", KATTR__MAX);
4370 if (kerr != KCGI_OK)
4371 goto done;
4372 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
4373 if (kerr != KCGI_OK)
4374 goto done;
4375 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4376 if (kerr != KCGI_OK)
4377 goto done;
4378 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4379 KATTR_ID, "summary_heads_content", KATTR__MAX);
4380 if (kerr != KCGI_OK)
4381 goto done;
4383 SIMPLEQ_FOREACH(re, &refs, entry) {
4384 const char *refname;
4386 if (got_ref_is_symbolic(re->ref))
4387 continue;
4389 refname = got_ref_get_name(re->ref);
4390 if (strncmp(refname, "refs/heads/", 11) != 0)
4391 continue;
4393 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
4394 refname, TM_DIFF);
4395 if (error)
4396 goto done;
4398 if (strncmp(refname, "refs/heads/", 11) == 0)
4399 refname += 11;
4401 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4402 KATTR_ID, "heads_wrapper", KATTR__MAX);
4403 if (kerr != KCGI_OK)
4404 goto done;
4405 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4406 KATTR_ID, "heads_age", KATTR__MAX);
4407 if (kerr != KCGI_OK)
4408 goto done;
4409 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
4410 if (kerr != KCGI_OK)
4411 goto done;
4412 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4413 if (kerr != KCGI_OK)
4414 goto done;
4415 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4416 KATTR_ID, "heads_space", KATTR__MAX);
4417 if (kerr != KCGI_OK)
4418 goto done;
4419 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
4420 if (kerr != KCGI_OK)
4421 goto done;
4422 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4423 if (kerr != KCGI_OK)
4424 goto done;
4425 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4426 KATTR_ID, "head", KATTR__MAX);
4427 if (kerr != KCGI_OK)
4428 goto done;
4429 if (asprintf(&href_summary,
4430 "?path=%s&action=summary&headref=%s",
4431 gw_trans->repo_name, refname) == -1) {
4432 error = got_error_from_errno("asprintf");
4433 goto done;
4435 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4436 href_summary, KATTR__MAX);
4437 kerr = khtml_puts(gw_trans->gw_html_req, refname);
4438 if (kerr != KCGI_OK)
4439 goto done;
4440 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4441 if (kerr != KCGI_OK)
4442 goto done;
4444 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4445 "navs_wrapper", KATTR__MAX);
4446 if (kerr != KCGI_OK)
4447 goto done;
4448 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4449 "navs", KATTR__MAX);
4450 if (kerr != KCGI_OK)
4451 goto done;
4453 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4454 href_summary, KATTR__MAX);
4455 if (kerr != KCGI_OK)
4456 goto done;
4457 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4458 if (kerr != KCGI_OK)
4459 goto done;
4460 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4461 if (kerr != KCGI_OK)
4462 goto done;
4464 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4465 if (kerr != KCGI_OK)
4466 goto done;
4467 if (asprintf(&href_briefs, "?path=%s&action=briefs&headref=%s",
4468 gw_trans->repo_name, refname) == -1) {
4469 error = got_error_from_errno("asprintf");
4470 goto done;
4472 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4473 href_briefs, KATTR__MAX);
4474 if (kerr != KCGI_OK)
4475 goto done;
4476 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4477 if (kerr != KCGI_OK)
4478 goto done;
4479 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4480 if (kerr != KCGI_OK)
4481 goto done;
4483 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4484 if (kerr != KCGI_OK)
4485 goto done;
4487 if (asprintf(&href_commits,
4488 "?path=%s&action=commits&headref=%s",
4489 gw_trans->repo_name, refname) == -1) {
4490 error = got_error_from_errno("asprintf");
4491 goto done;
4493 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4494 href_commits, KATTR__MAX);
4495 if (kerr != KCGI_OK)
4496 goto done;
4497 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4498 if (kerr != KCGI_OK)
4499 goto done;
4500 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4501 if (kerr != KCGI_OK)
4502 goto done;
4504 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4505 "dotted_line", KATTR__MAX);
4506 if (kerr != KCGI_OK)
4507 goto done;
4508 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4509 if (kerr != KCGI_OK)
4510 goto done;
4511 free(href_summary);
4512 href_summary = NULL;
4513 free(href_briefs);
4514 href_briefs = NULL;
4515 free(href_commits);
4516 href_commits = NULL;
4518 done:
4519 got_ref_list_free(&refs);
4520 free(href_summary);
4521 free(href_briefs);
4522 free(href_commits);
4523 return error;
4526 static const struct got_error *
4527 gw_output_site_link(struct gw_trans *gw_trans)
4529 const struct got_error *error = NULL;
4530 char *href_summary = NULL;
4531 enum kcgi_err kerr = KCGI_OK;
4533 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4534 "site_link", KATTR__MAX);
4535 if (kerr != KCGI_OK)
4536 goto done;
4537 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4538 KATTR__MAX);
4539 if (kerr != KCGI_OK)
4540 goto done;
4541 kerr = khtml_puts(gw_trans->gw_html_req,
4542 gw_trans->gw_conf->got_site_link);
4543 if (kerr != KCGI_OK)
4544 goto done;
4545 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4546 if (kerr != KCGI_OK)
4547 goto done;
4549 if (gw_trans->repo_name != NULL) {
4550 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4551 if (kerr != KCGI_OK)
4552 goto done;
4553 if (asprintf(&href_summary, "?path=%s&action=summary",
4554 gw_trans->repo_name) == -1)
4555 goto done;
4556 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4557 href_summary, KATTR__MAX);
4558 if (kerr != KCGI_OK)
4559 goto done;
4560 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4561 if (kerr != KCGI_OK)
4562 goto done;
4563 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4564 if (kerr != KCGI_OK)
4565 goto done;
4566 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4567 if (kerr != KCGI_OK)
4568 goto done;
4569 kerr = khtml_puts(gw_trans->gw_html_req,
4570 gw_get_action_name(gw_trans));
4571 if (kerr != KCGI_OK)
4572 goto done;
4575 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4576 if (kerr != KCGI_OK)
4577 goto done;
4578 done:
4579 free(href_summary);
4580 return error;
4583 static const struct got_error *
4584 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4586 const struct got_error *error = NULL;
4587 char *color = NULL;
4588 enum kcgi_err kerr = KCGI_OK;
4590 if (strncmp(buf, "-", 1) == 0)
4591 color = "diff_minus";
4592 else if (strncmp(buf, "+", 1) == 0)
4593 color = "diff_plus";
4594 else if (strncmp(buf, "@@", 2) == 0)
4595 color = "diff_chunk_header";
4596 else if (strncmp(buf, "@@", 2) == 0)
4597 color = "diff_chunk_header";
4598 else if (strncmp(buf, "commit +", 8) == 0)
4599 color = "diff_meta";
4600 else if (strncmp(buf, "commit -", 8) == 0)
4601 color = "diff_meta";
4602 else if (strncmp(buf, "blob +", 6) == 0)
4603 color = "diff_meta";
4604 else if (strncmp(buf, "blob -", 6) == 0)
4605 color = "diff_meta";
4606 else if (strncmp(buf, "file +", 6) == 0)
4607 color = "diff_meta";
4608 else if (strncmp(buf, "file -", 6) == 0)
4609 color = "diff_meta";
4610 else if (strncmp(buf, "from:", 5) == 0)
4611 color = "diff_author";
4612 else if (strncmp(buf, "via:", 4) == 0)
4613 color = "diff_author";
4614 else if (strncmp(buf, "date:", 5) == 0)
4615 color = "diff_date";
4616 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4617 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4618 if (error == NULL && kerr != KCGI_OK)
4619 error = gw_kcgi_error(kerr);
4620 return error;
4623 int
4624 main(int argc, char *argv[])
4626 const struct got_error *error = NULL;
4627 struct gw_trans *gw_trans;
4628 struct gw_dir *dir = NULL, *tdir;
4629 const char *page = "index";
4630 int gw_malloc = 1;
4631 enum kcgi_err kerr;
4633 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4634 errx(1, "malloc");
4636 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4637 errx(1, "malloc");
4639 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4640 errx(1, "malloc");
4642 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4643 errx(1, "malloc");
4645 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4646 if (kerr != KCGI_OK) {
4647 error = gw_kcgi_error(kerr);
4648 goto done;
4651 if ((gw_trans->gw_conf =
4652 malloc(sizeof(struct gotweb_conf))) == NULL) {
4653 gw_malloc = 0;
4654 error = got_error_from_errno("malloc");
4655 goto done;
4658 TAILQ_INIT(&gw_trans->gw_dirs);
4659 TAILQ_INIT(&gw_trans->gw_headers);
4661 gw_trans->action = -1;
4662 gw_trans->page = 0;
4663 gw_trans->repos_total = 0;
4664 gw_trans->repo_path = NULL;
4665 gw_trans->commit_id = NULL;
4666 gw_trans->next_id = NULL;
4667 gw_trans->next_prev_id = NULL;
4668 gw_trans->prev_id = NULL;
4669 gw_trans->prev_prev_id = NULL;
4670 gw_trans->headref = GOT_REF_HEAD;
4671 gw_trans->mime = KMIME_TEXT_HTML;
4672 gw_trans->gw_tmpl->key = gw_templs;
4673 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4674 gw_trans->gw_tmpl->arg = gw_trans;
4675 gw_trans->gw_tmpl->cb = gw_template;
4676 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
4677 if (error)
4678 goto done;
4680 error = gw_parse_querystring(gw_trans);
4681 if (error)
4682 goto done;
4684 if (gw_trans->action == GW_BLOB)
4685 error = gw_blob(gw_trans);
4686 else
4687 error = gw_display_index(gw_trans);
4688 done:
4689 if (gw_malloc) {
4690 free(gw_trans->gw_conf->got_repos_path);
4691 free(gw_trans->gw_conf->got_site_name);
4692 free(gw_trans->gw_conf->got_site_owner);
4693 free(gw_trans->gw_conf->got_site_link);
4694 free(gw_trans->gw_conf->got_logo);
4695 free(gw_trans->gw_conf->got_logo_url);
4696 free(gw_trans->gw_conf);
4697 free(gw_trans->commit_id);
4698 free(gw_trans->next_id);
4699 free(gw_trans->next_prev_id);
4700 free(gw_trans->prev_id);
4701 free(gw_trans->prev_prev_id);
4702 free(gw_trans->repo_path);
4703 if (gw_trans->repo)
4704 got_repo_close(gw_trans->repo);
4706 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4707 free(dir->name);
4708 free(dir->description);
4709 free(dir->age);
4710 free(dir->url);
4711 free(dir->path);
4712 free(dir);
4717 khttp_free(gw_trans->gw_req);
4718 return 0;