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_error.h>
35 #include <got_object.h>
36 #include <got_reference.h>
37 #include <got_repository.h>
38 #include <got_path.h>
39 #include <got_cancel.h>
40 #include <got_worktree.h>
41 #include <got_diff.h>
42 #include <got_commit_graph.h>
43 #include <got_blame.h>
44 #include <got_privsep.h>
45 #include <got_opentemp.h>
47 #include <kcgi.h>
48 #include <kcgihtml.h>
50 #include "gotweb.h"
52 #ifndef nitems
53 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
54 #endif
56 struct gw_trans {
57 TAILQ_HEAD(headers, gw_header) gw_headers;
58 TAILQ_HEAD(dirs, gw_dir) gw_dirs;
59 struct got_repository *repo;
60 struct gw_dir *gw_dir;
61 struct gotweb_config *gw_conf;
62 struct ktemplate *gw_tmpl;
63 struct khtmlreq *gw_html_req;
64 struct kreq *gw_req;
65 const struct got_error *error;
66 const char *repo_name;
67 char *repo_path;
68 char *commit_id;
69 char *next_id;
70 char *next_prev_id;
71 char *prev_id;
72 char *prev_prev_id;
73 const char *repo_file;
74 char *repo_folder;
75 const char *headref;
76 unsigned int action;
77 unsigned int page;
78 unsigned int repos_total;
79 enum kmime mime;
80 };
82 struct gw_header {
83 TAILQ_ENTRY(gw_header) entry;
84 struct got_reflist_head refs;
85 char *path;
87 char *refs_str;
88 char *commit_id; /* id_str1 */
89 char *parent_id; /* id_str2 */
90 char *tree_id;
91 char *author;
92 char *committer;
93 char *commit_msg;
94 time_t committer_time;
95 };
97 struct gw_dir {
98 TAILQ_ENTRY(gw_dir) entry;
99 char *name;
100 char *owner;
101 char *description;
102 char *url;
103 char *age;
104 char *path;
105 };
107 enum gw_key {
108 KEY_ACTION,
109 KEY_COMMIT_ID,
110 KEY_FILE,
111 KEY_FOLDER,
112 KEY_HEADREF,
113 KEY_PAGE,
114 KEY_PATH,
115 KEY_PREV_ID,
116 KEY_PREV_PREV_ID,
117 KEY__ZMAX
118 };
120 enum gw_tmpl {
121 TEMPL_CONTENT,
122 TEMPL_HEAD,
123 TEMPL_HEADER,
124 TEMPL_SEARCH,
125 TEMPL_SITEPATH,
126 TEMPL_SITEOWNER,
127 TEMPL_TITLE,
128 TEMPL__MAX
129 };
131 enum gw_ref_tm {
132 TM_DIFF,
133 TM_LONG,
134 };
136 enum gw_tags_type {
137 TAGBRIEF,
138 TAGFULL,
139 };
141 static const char *const gw_templs[TEMPL__MAX] = {
142 "content",
143 "head",
144 "header",
145 "search",
146 "sitepath",
147 "siteowner",
148 "title",
149 };
151 static const struct kvalid gw_keys[KEY__ZMAX] = {
152 { kvalid_stringne, "action" },
153 { kvalid_stringne, "commit" },
154 { kvalid_stringne, "file" },
155 { kvalid_stringne, "folder" },
156 { kvalid_stringne, "headref" },
157 { kvalid_int, "page" },
158 { kvalid_stringne, "path" },
159 { kvalid_stringne, "prev" },
160 { kvalid_stringne, "prev_prev" },
161 };
163 static struct gw_header *gw_init_header(void);
165 static void gw_free_header(struct gw_header *);
167 static int gw_template(size_t, void *);
169 static const struct got_error *gw_error(struct gw_trans *);
170 static const struct got_error *gw_init_gw_dir(struct gw_dir **, const char *);
171 static const struct got_error *gw_get_repo_description(char **,
172 struct gw_trans *, char *);
173 static const struct got_error *gw_get_repo_owner(char **, struct gw_trans *,
174 char *);
175 static const struct got_error *gw_get_time_str(char **, time_t, int);
176 static const struct got_error *gw_get_repo_age(char **, struct gw_trans *,
177 char *, const char *, int);
178 static const struct got_error *gw_output_file_blame(struct gw_trans *,
179 struct gw_header *);
180 static const struct got_error *gw_output_blob_buf(struct gw_trans *,
181 struct gw_header *);
182 static const struct got_error *gw_output_repo_tree(struct gw_trans *,
183 struct gw_header *);
184 static const struct got_error *gw_output_diff(struct gw_trans *,
185 struct gw_header *);
186 static const struct got_error *gw_output_repo_tags(struct gw_trans *,
187 struct gw_header *, int, int);
188 static const struct got_error *gw_output_repo_heads(struct gw_trans *);
189 static const struct got_error *gw_output_site_link(struct gw_trans *);
190 static const struct got_error *gw_get_clone_url(char **, struct gw_trans *,
191 char *);
192 static const struct got_error *gw_colordiff_line(struct gw_trans *, char *);
194 static const struct got_error *gw_gen_commit_header(struct gw_trans *, char *,
195 char*);
196 static const struct got_error *gw_gen_diff_header(struct gw_trans *, char *,
197 char*);
198 static const struct got_error *gw_gen_author_header(struct gw_trans *,
199 const char *);
200 static const struct got_error *gw_gen_age_header(struct gw_trans *,
201 const char *);
202 static const struct got_error *gw_gen_committer_header(struct gw_trans *,
203 const char *);
204 static const struct got_error *gw_gen_commit_msg_header(struct gw_trans*,
205 char *);
206 static const struct got_error *gw_gen_tree_header(struct gw_trans *, char *);
207 static const struct got_error *gw_display_open(struct gw_trans *, enum khttp,
208 enum kmime);
209 static const struct got_error *gw_display_index(struct gw_trans *);
210 static const struct got_error *gw_get_header(struct gw_trans *,
211 struct gw_header *, int);
212 static const struct got_error *gw_get_commits(struct gw_trans *,
213 struct gw_header *, int,
214 struct got_object_id *);
215 static const struct got_error *gw_get_commit(struct gw_trans *,
216 struct gw_header *,
217 struct got_commit_object *,
218 struct got_object_id *);
219 static const struct got_error *gw_apply_unveil(const char *);
220 static const struct got_error *gw_blame_cb(void *, int, int,
221 struct got_object_id *);
222 static const struct got_error *gw_load_got_paths(struct gw_trans *);
223 static const struct got_error *gw_load_got_path(struct gw_trans *,
224 struct gw_dir *);
225 static const struct got_error *gw_parse_querystring(struct gw_trans *);
226 static const struct got_error *gw_blame(struct gw_trans *);
227 static const struct got_error *gw_blob(struct gw_trans *);
228 static const struct got_error *gw_diff(struct gw_trans *);
229 static const struct got_error *gw_index(struct gw_trans *);
230 static const struct got_error *gw_commits(struct gw_trans *);
231 static const struct got_error *gw_briefs(struct gw_trans *);
232 static const struct got_error *gw_summary(struct gw_trans *);
233 static const struct got_error *gw_tree(struct gw_trans *);
234 static const struct got_error *gw_tag(struct gw_trans *);
235 static const struct got_error *gw_tags(struct gw_trans *);
237 struct gw_query_action {
238 unsigned int func_id;
239 const char *func_name;
240 const struct got_error *(*func_main)(struct gw_trans *);
241 char *template;
242 };
244 enum gw_query_actions {
245 GW_BLAME,
246 GW_BLOB,
247 GW_BRIEFS,
248 GW_COMMITS,
249 GW_DIFF,
250 GW_ERR,
251 GW_INDEX,
252 GW_SUMMARY,
253 GW_TAG,
254 GW_TAGS,
255 GW_TREE,
256 };
258 static struct gw_query_action gw_query_funcs[] = {
259 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
260 { GW_BLOB, "blob", NULL, NULL },
261 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
262 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
263 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
264 { GW_ERR, "error", gw_error, "gw_tmpl/err.tmpl" },
265 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
266 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
267 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
268 { GW_TAGS, "tags", gw_tags, "gw_tmpl/tags.tmpl" },
269 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
270 };
272 static const char *
273 gw_get_action_name(struct gw_trans *gw_trans)
275 return gw_query_funcs[gw_trans->action].func_name;
278 static const struct got_error *
279 gw_kcgi_error(enum kcgi_err kerr)
281 if (kerr == KCGI_OK)
282 return NULL;
284 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
285 return got_error(GOT_ERR_CANCELLED);
287 if (kerr == KCGI_ENOMEM)
288 return got_error_set_errno(ENOMEM,
289 kcgi_strerror(kerr));
291 if (kerr == KCGI_ENFILE)
292 return got_error_set_errno(ENFILE,
293 kcgi_strerror(kerr));
295 if (kerr == KCGI_EAGAIN)
296 return got_error_set_errno(EAGAIN,
297 kcgi_strerror(kerr));
299 if (kerr == KCGI_FORM)
300 return got_error_msg(GOT_ERR_IO,
301 kcgi_strerror(kerr));
303 return got_error_from_errno(kcgi_strerror(kerr));
306 static const struct got_error *
307 gw_apply_unveil(const char *repo_path)
309 const struct got_error *err;
311 #ifdef PROFILE
312 if (unveil("gmon.out", "rwc") != 0)
313 return got_error_from_errno2("unveil", "gmon.out");
314 #endif
315 if (repo_path && unveil(repo_path, "r") != 0)
316 return got_error_from_errno2("unveil", repo_path);
318 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
319 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
321 err = got_privsep_unveil_exec_helpers();
322 if (err != NULL)
323 return err;
325 if (unveil(NULL, NULL) != 0)
326 return got_error_from_errno("unveil");
328 return NULL;
331 static int
332 isbinary(const uint8_t *buf, size_t n)
334 size_t i;
336 for (i = 0; i < n; i++)
337 if (buf[i] == 0)
338 return 1;
339 return 0;
342 static const struct got_error *
343 gw_blame(struct gw_trans *gw_trans)
345 const struct got_error *error = NULL;
346 struct gw_header *header = NULL;
347 char *age = NULL;
348 enum kcgi_err kerr = KCGI_OK;
350 #ifndef PROFILE
351 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
352 NULL) == -1)
353 return got_error_from_errno("pledge");
354 #endif
355 if ((header = gw_init_header()) == NULL)
356 return got_error_from_errno("malloc");
358 error = gw_apply_unveil(gw_trans->gw_dir->path);
359 if (error)
360 goto done;
362 /* check querystring */
363 if (gw_trans->repo_file == NULL) {
364 error = got_error_msg(GOT_ERR_QUERYSTRING,
365 "file required in querystring");
366 goto done;
368 if (gw_trans->commit_id == NULL) {
369 error = got_error_msg(GOT_ERR_QUERYSTRING,
370 "commit required in querystring");
371 goto done;
374 error = gw_get_header(gw_trans, header, 1);
375 if (error)
376 goto done;
377 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
378 "blame_header_wrapper", KATTR__MAX);
379 if (kerr != KCGI_OK)
380 goto done;
381 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
382 "blame_header", KATTR__MAX);
383 if (kerr != KCGI_OK)
384 goto done;
385 error = gw_get_time_str(&age, header->committer_time,
386 TM_LONG);
387 if (error)
388 goto done;
389 error = gw_gen_age_header(gw_trans, age ?age : "");
390 if (error)
391 goto done;
392 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
393 if (error)
394 goto done;
395 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
396 if (kerr != KCGI_OK)
397 goto done;
398 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
399 "dotted_line", KATTR__MAX);
400 if (kerr != KCGI_OK)
401 goto done;
402 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
403 if (kerr != KCGI_OK)
404 goto done;
406 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
407 "blame", KATTR__MAX);
408 if (kerr != KCGI_OK)
409 goto done;
410 error = gw_output_file_blame(gw_trans, header);
411 if (error)
412 goto done;
413 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
414 done:
415 gw_free_header(header);
416 if (error == NULL && kerr != KCGI_OK)
417 error = gw_kcgi_error(kerr);
418 return error;
421 static const struct got_error *
422 gw_blob(struct gw_trans *gw_trans)
424 const struct got_error *error = NULL, *err = NULL;
425 struct gw_header *header = NULL;
426 enum kcgi_err kerr = KCGI_OK;
428 #ifndef PROFILE
429 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
430 NULL) == -1)
431 return got_error_from_errno("pledge");
432 #endif
433 if ((header = gw_init_header()) == NULL)
434 return got_error_from_errno("malloc");
436 error = gw_apply_unveil(gw_trans->gw_dir->path);
437 if (error)
438 goto done;
440 /* check querystring */
441 if (gw_trans->repo_file == NULL) {
442 error = got_error_msg(GOT_ERR_QUERYSTRING,
443 "file required in querystring");
444 goto done;
446 if (gw_trans->commit_id == NULL) {
447 error = got_error_msg(GOT_ERR_QUERYSTRING,
448 "commit required in querystring");
449 goto done;
451 error = gw_get_header(gw_trans, header, 1);
452 if (error)
453 goto done;
455 error = gw_output_blob_buf(gw_trans, header);
456 done:
458 if (error) {
459 gw_trans->mime = KMIME_TEXT_PLAIN;
460 err = gw_display_index(gw_trans);
461 if (err) {
462 error = err;
463 goto errored;
465 kerr = khttp_puts(gw_trans->gw_req, error->msg);
467 errored:
468 gw_free_header(header);
469 if (error == NULL && kerr != KCGI_OK)
470 error = gw_kcgi_error(kerr);
471 return error;
474 static const struct got_error *
475 gw_diff(struct gw_trans *gw_trans)
477 const struct got_error *error = NULL;
478 struct gw_header *header = NULL;
479 char *age = NULL;
480 enum kcgi_err kerr = KCGI_OK;
482 #ifndef PROFILE
483 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
484 NULL) == -1)
485 return got_error_from_errno("pledge");
486 #endif
487 if ((header = gw_init_header()) == NULL)
488 return got_error_from_errno("malloc");
490 error = gw_apply_unveil(gw_trans->gw_dir->path);
491 if (error)
492 goto done;
494 error = gw_get_header(gw_trans, header, 1);
495 if (error)
496 goto done;
498 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
499 "diff_header_wrapper", KATTR__MAX);
500 if (kerr != KCGI_OK)
501 goto done;
502 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
503 "diff_header", KATTR__MAX);
504 if (kerr != KCGI_OK)
505 goto done;
506 error = gw_gen_diff_header(gw_trans, header->parent_id,
507 header->commit_id);
508 if (error)
509 goto done;
510 error = gw_gen_commit_header(gw_trans, header->commit_id,
511 header->refs_str);
512 if (error)
513 goto done;
514 error = gw_gen_tree_header(gw_trans, header->tree_id);
515 if (error)
516 goto done;
517 error = gw_gen_author_header(gw_trans, header->author);
518 if (error)
519 goto done;
520 error = gw_gen_committer_header(gw_trans, header->author);
521 if (error)
522 goto done;
523 error = gw_get_time_str(&age, header->committer_time,
524 TM_LONG);
525 if (error)
526 goto done;
527 error = gw_gen_age_header(gw_trans, age ?age : "");
528 if (error)
529 goto done;
530 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
531 if (error)
532 goto done;
533 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
534 if (kerr != KCGI_OK)
535 goto done;
536 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
537 "dotted_line", KATTR__MAX);
538 if (kerr != KCGI_OK)
539 goto done;
540 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
541 if (kerr != KCGI_OK)
542 goto done;
544 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
545 "diff", KATTR__MAX);
546 if (kerr != KCGI_OK)
547 goto done;
548 error = gw_output_diff(gw_trans, header);
549 if (error)
550 goto done;
552 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
553 done:
554 gw_free_header(header);
555 free(age);
556 if (error == NULL && kerr != KCGI_OK)
557 error = gw_kcgi_error(kerr);
558 return error;
561 static const struct got_error *
562 gw_index(struct gw_trans *gw_trans)
564 const struct got_error *error = NULL;
565 struct gw_dir *gw_dir = NULL;
566 char *href_next = NULL, *href_prev = NULL, *href_summary = NULL;
567 char *href_briefs = NULL, *href_commits = NULL, *href_tree = NULL;
568 char *href_tags = NULL;
569 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
570 enum kcgi_err kerr = KCGI_OK;
572 #ifndef PROFILE
573 if (pledge("stdio rpath proc exec sendfd unveil",
574 NULL) == -1) {
575 error = got_error_from_errno("pledge");
576 return error;
578 #endif
579 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path);
580 if (error)
581 return error;
583 error = gw_load_got_paths(gw_trans);
584 if (error)
585 return error;
587 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
588 "index_header", KATTR__MAX);
589 if (kerr != KCGI_OK)
590 return gw_kcgi_error(kerr);
591 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
592 "index_header_project", KATTR__MAX);
593 if (kerr != KCGI_OK)
594 return gw_kcgi_error(kerr);
595 kerr = khtml_puts(gw_trans->gw_html_req, "Project");
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);
602 if (gw_trans->gw_conf->got_show_repo_description) {
603 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
604 "index_header_description", KATTR__MAX);
605 if (kerr != KCGI_OK)
606 return gw_kcgi_error(kerr);
607 kerr = khtml_puts(gw_trans->gw_html_req, "Description");
608 if (kerr != KCGI_OK)
609 return gw_kcgi_error(kerr);
610 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
611 if (kerr != KCGI_OK)
612 return gw_kcgi_error(kerr);
615 if (gw_trans->gw_conf->got_show_repo_owner) {
616 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
617 "index_header_owner", KATTR__MAX);
618 if (kerr != KCGI_OK)
619 return gw_kcgi_error(kerr);
620 kerr = khtml_puts(gw_trans->gw_html_req, "Owner");
621 if (kerr != KCGI_OK)
622 return gw_kcgi_error(kerr);
623 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
624 if (kerr != KCGI_OK)
625 return gw_kcgi_error(kerr);
628 if (gw_trans->gw_conf->got_show_repo_age) {
629 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
630 "index_header_age", KATTR__MAX);
631 if (kerr != KCGI_OK)
632 return gw_kcgi_error(kerr);
633 kerr = khtml_puts(gw_trans->gw_html_req, "Last Change");
634 if (kerr != KCGI_OK)
635 return gw_kcgi_error(kerr);
636 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
637 if (kerr != KCGI_OK)
638 return gw_kcgi_error(kerr);
641 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
642 if (kerr != KCGI_OK)
643 return gw_kcgi_error(kerr);
645 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
646 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
647 "index_wrapper", KATTR__MAX);
648 if (kerr != KCGI_OK)
649 return gw_kcgi_error(kerr);
650 kerr = khtml_printf(gw_trans->gw_html_req,
651 "No repositories found in %s",
652 gw_trans->gw_conf->got_repos_path);
653 if (kerr != KCGI_OK)
654 return gw_kcgi_error(kerr);
655 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
656 if (kerr != KCGI_OK)
657 return gw_kcgi_error(kerr);
658 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
659 "dotted_line", KATTR__MAX);
660 if (kerr != KCGI_OK)
661 return gw_kcgi_error(kerr);
662 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
663 if (kerr != KCGI_OK)
664 return gw_kcgi_error(kerr);
665 return error;
668 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
669 dir_c++;
671 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
672 if (gw_trans->page > 0 && (gw_trans->page *
673 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
674 prev_disp++;
675 continue;
678 prev_disp++;
680 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
681 "index_wrapper", KATTR__MAX);
682 if (kerr != KCGI_OK)
683 goto done;
685 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
686 gw_dir->name, "action", "summary", NULL);
687 if (href_summary == NULL) {
688 error = got_error_from_errno("khttp_urlpart");
689 goto done;
691 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
692 "index_project", KATTR__MAX);
693 if (kerr != KCGI_OK)
694 goto done;
695 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
696 href_summary, KATTR__MAX);
697 if (kerr != KCGI_OK)
698 goto done;
699 kerr = khtml_puts(gw_trans->gw_html_req, gw_dir->name);
700 if (kerr != KCGI_OK)
701 goto done;
702 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
703 if (kerr != KCGI_OK)
704 goto done;
705 if (gw_trans->gw_conf->got_show_repo_description) {
706 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
707 KATTR_ID, "index_project_description", KATTR__MAX);
708 if (kerr != KCGI_OK)
709 goto done;
710 kerr = khtml_puts(gw_trans->gw_html_req,
711 gw_dir->description ? gw_dir->description : "");
712 if (kerr != KCGI_OK)
713 goto done;
714 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
715 if (kerr != KCGI_OK)
716 goto done;
718 if (gw_trans->gw_conf->got_show_repo_owner) {
719 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
720 KATTR_ID, "index_project_owner", KATTR__MAX);
721 if (kerr != KCGI_OK)
722 goto done;
723 kerr = khtml_puts(gw_trans->gw_html_req,
724 gw_dir->owner ? gw_dir->owner : "");
725 if (kerr != KCGI_OK)
726 goto done;
727 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
728 if (kerr != KCGI_OK)
729 goto done;
731 if (gw_trans->gw_conf->got_show_repo_age) {
732 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
733 KATTR_ID, "index_project_age", KATTR__MAX);
734 if (kerr != KCGI_OK)
735 goto done;
736 kerr = khtml_puts(gw_trans->gw_html_req,
737 gw_dir->age ? gw_dir->age : "");
738 if (kerr != KCGI_OK)
739 goto done;
740 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
741 if (kerr != KCGI_OK)
742 goto done;
745 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
746 "navs_wrapper", KATTR__MAX);
747 if (kerr != KCGI_OK)
748 goto done;
749 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
750 "navs", KATTR__MAX);
751 if (kerr != KCGI_OK)
752 goto done;
754 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
755 href_summary, KATTR__MAX);
756 if (kerr != KCGI_OK)
757 goto done;
758 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
759 if (kerr != KCGI_OK)
760 goto done;
761 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
762 if (kerr != KCGI_OK)
763 goto done;
765 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
766 if (kerr != KCGI_OK)
767 goto done;
769 href_briefs = khttp_urlpart(NULL, NULL, "gotweb", "path",
770 gw_dir->name, "action", "briefs", NULL);
771 if (href_briefs == NULL) {
772 error = got_error_from_errno("khttp_urlpart");
773 goto done;
775 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
776 href_briefs, KATTR__MAX);
777 if (kerr != KCGI_OK)
778 goto done;
779 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
780 if (kerr != KCGI_OK)
781 goto done;
782 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
783 if (kerr != KCGI_OK)
784 error = gw_kcgi_error(kerr);
786 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
787 if (kerr != KCGI_OK)
788 goto done;
790 href_commits = khttp_urlpart(NULL, NULL, "gotweb", "path",
791 gw_dir->name, "action", "commits", NULL);
792 if (href_commits == NULL) {
793 error = got_error_from_errno("khttp_urlpart");
794 goto done;
796 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
797 href_commits, KATTR__MAX);
798 if (kerr != KCGI_OK)
799 goto done;
800 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
801 if (kerr != KCGI_OK)
802 goto done;
803 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
804 if (kerr != KCGI_OK)
805 goto done;
807 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
808 if (kerr != KCGI_OK)
809 goto done;
811 href_tags = khttp_urlpart(NULL, NULL, "gotweb", "path",
812 gw_dir->name, "action", "tags", NULL);
813 if (href_tags == NULL) {
814 error = got_error_from_errno("khttp_urlpart");
815 goto done;
817 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
818 href_tags, KATTR__MAX);
819 if (kerr != KCGI_OK)
820 goto done;
821 kerr = khtml_puts(gw_trans->gw_html_req, "tags");
822 if (kerr != KCGI_OK)
823 goto done;
824 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
825 if (kerr != KCGI_OK)
826 goto done;
828 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
829 if (kerr != KCGI_OK)
830 goto done;
832 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
833 gw_dir->name, "action", "tree", NULL);
834 if (href_tree == NULL) {
835 error = got_error_from_errno("khttp_urlpart");
836 goto done;
838 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
839 href_tree, KATTR__MAX);
840 if (kerr != KCGI_OK)
841 goto done;
842 kerr = khtml_puts(gw_trans->gw_html_req, "tree");
843 if (kerr != KCGI_OK)
844 goto done;
846 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
847 if (kerr != KCGI_OK)
848 goto done;
849 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
850 "dotted_line", KATTR__MAX);
851 if (kerr != KCGI_OK)
852 goto done;
853 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
854 if (kerr != KCGI_OK)
855 goto done;
857 free(href_summary);
858 href_summary = NULL;
859 free(href_briefs);
860 href_briefs = NULL;
861 free(href_commits);
862 href_commits = NULL;
863 free(href_tags);
864 href_tags = NULL;
865 free(href_tree);
866 href_tree = NULL;
868 if (gw_trans->gw_conf->got_max_repos_display == 0)
869 continue;
871 if ((next_disp == gw_trans->gw_conf->got_max_repos_display) ||
872 ((gw_trans->gw_conf->got_max_repos_display > 0) &&
873 (gw_trans->page > 0) &&
874 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
875 prev_disp == gw_trans->repos_total))) {
876 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
877 KATTR_ID, "np_wrapper", KATTR__MAX);
878 if (kerr != KCGI_OK)
879 goto done;
880 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
881 KATTR_ID, "nav_prev", KATTR__MAX);
882 if (kerr != KCGI_OK)
883 goto done;
886 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
887 (gw_trans->page > 0) &&
888 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
889 prev_disp == gw_trans->repos_total)) {
890 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "page",
891 KATTRX_INT, (int64_t)(gw_trans->page - 1), NULL);
892 if (href_prev == NULL) {
893 error = got_error_from_errno("khttp_urlpartx");
894 goto done;
896 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
897 KATTR_HREF, href_prev, KATTR__MAX);
898 if (kerr != KCGI_OK)
899 goto done;
900 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
901 if (kerr != KCGI_OK)
902 goto done;
903 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
904 if (kerr != KCGI_OK)
905 goto done;
908 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
909 if (kerr != KCGI_OK)
910 return gw_kcgi_error(kerr);
912 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
913 next_disp == gw_trans->gw_conf->got_max_repos_display &&
914 dir_c != (gw_trans->page + 1) *
915 gw_trans->gw_conf->got_max_repos_display) {
916 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
917 KATTR_ID, "nav_next", KATTR__MAX);
918 if (kerr != KCGI_OK)
919 goto done;
920 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "page",
921 KATTRX_INT, (int64_t)(gw_trans->page + 1), NULL);
922 if (href_next == NULL) {
923 error = got_error_from_errno("khttp_urlpartx");
924 goto done;
926 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
927 KATTR_HREF, href_next, KATTR__MAX);
928 if (kerr != KCGI_OK)
929 goto done;
930 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
931 if (kerr != KCGI_OK)
932 goto done;
933 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
934 if (kerr != KCGI_OK)
935 goto done;
936 next_disp = 0;
937 break;
940 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
941 (gw_trans->page > 0) &&
942 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
943 prev_disp == gw_trans->repos_total)) {
944 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
945 if (kerr != KCGI_OK)
946 goto done;
948 next_disp++;
950 done:
951 free(href_prev);
952 free(href_next);
953 free(href_summary);
954 free(href_briefs);
955 free(href_commits);
956 free(href_tags);
957 free(href_tree);
958 if (error == NULL && kerr != KCGI_OK)
959 error = gw_kcgi_error(kerr);
960 return error;
963 static const struct got_error *
964 gw_commits(struct gw_trans *gw_trans)
966 const struct got_error *error = NULL;
967 struct gw_header *header = NULL, *n_header = NULL;
968 char *age = NULL, *href_diff = NULL, *href_tree = NULL;
969 char *href_prev = NULL, *href_next = NULL;
970 enum kcgi_err kerr = KCGI_OK;
972 if ((header = gw_init_header()) == NULL)
973 return got_error_from_errno("malloc");
975 #ifndef PROFILE
976 if (pledge("stdio rpath proc exec sendfd unveil",
977 NULL) == -1) {
978 error = got_error_from_errno("pledge");
979 goto done;
981 #endif
982 error = gw_apply_unveil(gw_trans->gw_dir->path);
983 if (error)
984 goto done;
986 error = gw_get_header(gw_trans, header,
987 gw_trans->gw_conf->got_max_commits_display);
988 if (error)
989 goto done;
991 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
992 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
993 "commits_line_wrapper", KATTR__MAX);
994 if (kerr != KCGI_OK)
995 goto done;
996 error = gw_gen_commit_header(gw_trans, n_header->commit_id,
997 n_header->refs_str);
998 if (error)
999 goto done;
1000 error = gw_gen_author_header(gw_trans, n_header->author);
1001 if (error)
1002 goto done;
1003 error = gw_gen_committer_header(gw_trans, n_header->author);
1004 if (error)
1005 goto done;
1006 error = gw_get_time_str(&age, n_header->committer_time,
1007 TM_LONG);
1008 if (error)
1009 goto done;
1010 error = gw_gen_age_header(gw_trans, age ?age : "");
1011 if (error)
1012 goto done;
1013 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1014 if (kerr != KCGI_OK)
1015 goto done;
1017 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1018 "dotted_line", KATTR__MAX);
1019 if (kerr != KCGI_OK)
1020 goto done;
1021 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1022 if (kerr != KCGI_OK)
1023 goto done;
1025 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1026 "commit", KATTR__MAX);
1027 if (kerr != KCGI_OK)
1028 goto done;
1029 kerr = khttp_puts(gw_trans->gw_req, n_header->commit_msg);
1030 if (kerr != KCGI_OK)
1031 goto done;
1032 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1033 if (kerr != KCGI_OK)
1034 goto done;
1036 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
1037 gw_trans->repo_name, "action", "diff", "commit",
1038 n_header->commit_id, NULL);
1039 if (href_diff == NULL) {
1040 error = got_error_from_errno("khttp_urlpart");
1041 goto done;
1043 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1044 KATTR_ID, "navs_wrapper", KATTR__MAX);
1045 if (kerr != KCGI_OK)
1046 goto done;
1047 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1048 KATTR_ID, "navs", KATTR__MAX);
1049 if (kerr != KCGI_OK)
1050 goto done;
1051 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1052 KATTR_HREF, href_diff, KATTR__MAX);
1053 if (kerr != KCGI_OK)
1054 goto done;
1055 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1056 if (kerr != KCGI_OK)
1057 goto done;
1058 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1059 if (kerr != KCGI_OK)
1060 goto done;
1062 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1063 if (kerr != KCGI_OK)
1064 goto done;
1066 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
1067 gw_trans->repo_name, "action", "tree", "commit",
1068 n_header->commit_id, NULL);
1069 if (href_tree == NULL) {
1070 error = got_error_from_errno("khttp_urlpart");
1071 goto done;
1073 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1074 KATTR_HREF, href_tree, KATTR__MAX);
1075 if (kerr != KCGI_OK)
1076 goto done;
1077 khtml_puts(gw_trans->gw_html_req, "tree");
1078 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1079 if (kerr != KCGI_OK)
1080 goto done;
1081 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1082 if (kerr != KCGI_OK)
1083 goto done;
1085 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1086 "solid_line", KATTR__MAX);
1087 if (kerr != KCGI_OK)
1088 goto done;
1089 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1090 if (kerr != KCGI_OK)
1091 goto done;
1093 free(age);
1094 age = NULL;
1097 if (gw_trans->next_id || gw_trans->page > 0) {
1098 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1099 KATTR_ID, "np_wrapper", KATTR__MAX);
1100 if (kerr != KCGI_OK)
1101 goto done;
1102 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1103 KATTR_ID, "nav_prev", KATTR__MAX);
1104 if (kerr != KCGI_OK)
1105 goto done;
1108 if (gw_trans->page > 0 && gw_trans->prev_id) {
1109 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1110 KATTRX_STRING, gw_trans->repo_name, "page",
1111 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1112 KATTRX_STRING, "commits", "commit", KATTRX_STRING,
1113 gw_trans->prev_id ? gw_trans->prev_id : "", "prev",
1114 KATTRX_STRING, gw_trans->prev_prev_id ?
1115 gw_trans->prev_prev_id : "", NULL);
1116 if (href_prev == NULL) {
1117 error = got_error_from_errno("khttp_urlpartx");
1118 goto done;
1120 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1121 KATTR_HREF, href_prev, KATTR__MAX);
1122 if (kerr != KCGI_OK)
1123 goto done;
1124 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1125 if (kerr != KCGI_OK)
1126 goto done;
1127 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1128 if (kerr != KCGI_OK)
1129 goto done;
1132 if (gw_trans->next_id || gw_trans->page > 0) {
1133 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1134 if (kerr != KCGI_OK)
1135 return gw_kcgi_error(kerr);
1138 if (gw_trans->next_id) {
1139 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1140 KATTR_ID, "nav_next", KATTR__MAX);
1141 if (kerr != KCGI_OK)
1142 goto done;
1143 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1144 KATTRX_STRING, gw_trans->repo_name, "page",
1145 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1146 KATTRX_STRING, "commits", "commit", KATTRX_STRING,
1147 gw_trans->next_id, "prev", KATTRX_STRING,
1148 gw_trans->next_prev_id ? gw_trans->next_prev_id : "",
1149 "prev_prev", KATTRX_STRING, gw_trans->prev_prev_id ?
1150 gw_trans->prev_prev_id : "", NULL);
1151 if (href_next == NULL) {
1152 error = got_error_from_errno("khttp_urlpartx");
1153 goto done;
1155 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1156 KATTR_HREF, href_next, KATTR__MAX);
1157 if (kerr != KCGI_OK)
1158 goto done;
1159 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1160 if (kerr != KCGI_OK)
1161 goto done;
1162 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1163 if (kerr != KCGI_OK)
1164 goto done;
1167 if (gw_trans->next_id || gw_trans->page > 0) {
1168 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1169 if (kerr != KCGI_OK)
1170 goto done;
1172 done:
1173 gw_free_header(header);
1174 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1175 gw_free_header(n_header);
1176 free(age);
1177 free(href_next);
1178 free(href_prev);
1179 free(href_diff);
1180 free(href_tree);
1181 if (error == NULL && kerr != KCGI_OK)
1182 error = gw_kcgi_error(kerr);
1183 return error;
1186 static const struct got_error *
1187 gw_briefs(struct gw_trans *gw_trans)
1189 const struct got_error *error = NULL;
1190 struct gw_header *header = NULL, *n_header = NULL;
1191 char *age = NULL, *href_diff = NULL, *href_tree = NULL;
1192 char *href_prev = NULL, *href_next = NULL;
1193 char *newline, *smallerthan;
1194 enum kcgi_err kerr = KCGI_OK;
1196 if ((header = gw_init_header()) == NULL)
1197 return got_error_from_errno("malloc");
1199 #ifndef PROFILE
1200 if (pledge("stdio rpath proc exec sendfd unveil",
1201 NULL) == -1) {
1202 error = got_error_from_errno("pledge");
1203 goto done;
1205 #endif
1206 if (gw_trans->action != GW_SUMMARY) {
1207 error = gw_apply_unveil(gw_trans->gw_dir->path);
1208 if (error)
1209 goto done;
1212 if (gw_trans->action == GW_SUMMARY)
1213 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
1214 else
1215 error = gw_get_header(gw_trans, header,
1216 gw_trans->gw_conf->got_max_commits_display);
1217 if (error)
1218 goto done;
1220 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
1221 error = gw_get_time_str(&age, n_header->committer_time,
1222 TM_DIFF);
1223 if (error)
1224 goto done;
1226 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1227 KATTR_ID, "briefs_wrapper", KATTR__MAX);
1228 if (kerr != KCGI_OK)
1229 goto done;
1231 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1232 KATTR_ID, "briefs_age", KATTR__MAX);
1233 if (kerr != KCGI_OK)
1234 goto done;
1235 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
1236 if (kerr != KCGI_OK)
1237 goto done;
1238 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1239 if (kerr != KCGI_OK)
1240 goto done;
1242 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1243 KATTR_ID, "briefs_author", KATTR__MAX);
1244 if (kerr != KCGI_OK)
1245 goto done;
1246 smallerthan = strchr(n_header->author, '<');
1247 if (smallerthan)
1248 *smallerthan = '\0';
1249 kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
1250 if (kerr != KCGI_OK)
1251 goto done;
1252 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1253 if (kerr != KCGI_OK)
1254 goto done;
1256 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
1257 gw_trans->repo_name, "action", "diff", "commit",
1258 n_header->commit_id, NULL);
1259 if (href_diff == NULL) {
1260 error = got_error_from_errno("khttp_urlpart");
1261 goto done;
1263 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1264 KATTR_ID, "briefs_log", KATTR__MAX);
1265 if (kerr != KCGI_OK)
1266 goto done;
1267 newline = strchr(n_header->commit_msg, '\n');
1268 if (newline)
1269 *newline = '\0';
1270 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1271 KATTR_HREF, href_diff, KATTR__MAX);
1272 if (kerr != KCGI_OK)
1273 goto done;
1274 kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
1275 if (kerr != KCGI_OK)
1276 goto done;
1277 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1278 if (kerr != KCGI_OK)
1279 goto done;
1281 if (n_header->refs_str) {
1282 kerr = khtml_puts(gw_trans->gw_html_req, " ");
1283 if (kerr != KCGI_OK)
1284 goto done;
1285 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
1286 KATTR_ID, "refs_str", KATTR__MAX);
1287 if (kerr != KCGI_OK)
1288 goto done;
1289 kerr = khtml_printf(gw_trans->gw_html_req, "(%s)",
1290 n_header->refs_str);
1291 if (kerr != KCGI_OK)
1292 goto done;
1293 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1294 if (kerr != KCGI_OK)
1295 goto done;
1298 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1299 if (kerr != KCGI_OK)
1300 goto done;
1302 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1303 KATTR_ID, "navs_wrapper", KATTR__MAX);
1304 if (kerr != KCGI_OK)
1305 goto done;
1306 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1307 KATTR_ID, "navs", KATTR__MAX);
1308 if (kerr != KCGI_OK)
1309 goto done;
1310 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1311 KATTR_HREF, href_diff, KATTR__MAX);
1312 if (kerr != KCGI_OK)
1313 goto done;
1314 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1315 if (kerr != KCGI_OK)
1316 goto done;
1317 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1318 if (kerr != KCGI_OK)
1319 goto done;
1321 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1322 if (kerr != KCGI_OK)
1323 goto done;
1325 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
1326 gw_trans->repo_name, "action", "tree", "commit",
1327 n_header->commit_id, NULL);
1328 if (href_tree == NULL) {
1329 error = got_error_from_errno("khttp_urlpart");
1330 goto done;
1332 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1333 KATTR_HREF, href_tree, KATTR__MAX);
1334 if (kerr != KCGI_OK)
1335 goto done;
1336 khtml_puts(gw_trans->gw_html_req, "tree");
1337 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1338 if (kerr != KCGI_OK)
1339 goto done;
1340 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1341 if (kerr != KCGI_OK)
1342 goto done;
1344 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1345 KATTR_ID, "dotted_line", KATTR__MAX);
1346 if (kerr != KCGI_OK)
1347 goto done;
1348 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1349 if (kerr != KCGI_OK)
1350 goto done;
1352 free(age);
1353 age = NULL;
1354 free(href_diff);
1355 href_diff = NULL;
1356 free(href_tree);
1357 href_tree = NULL;
1360 if (gw_trans->next_id || gw_trans->page > 0) {
1361 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1362 KATTR_ID, "np_wrapper", KATTR__MAX);
1363 if (kerr != KCGI_OK)
1364 goto done;
1365 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1366 KATTR_ID, "nav_prev", KATTR__MAX);
1367 if (kerr != KCGI_OK)
1368 goto done;
1371 if (gw_trans->page > 0 && gw_trans->prev_id) {
1372 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1373 KATTRX_STRING, gw_trans->repo_name, "page",
1374 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1375 KATTRX_STRING, "briefs", "commit", KATTRX_STRING,
1376 gw_trans->prev_id ? gw_trans->prev_id : "", "prev",
1377 KATTRX_STRING, gw_trans->prev_prev_id ?
1378 gw_trans->prev_prev_id : "", NULL);
1379 if (href_prev == NULL) {
1380 error = got_error_from_errno("khttp_urlpartx");
1381 goto done;
1383 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1384 KATTR_HREF, href_prev, KATTR__MAX);
1385 if (kerr != KCGI_OK)
1386 goto done;
1387 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1388 if (kerr != KCGI_OK)
1389 goto done;
1390 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1391 if (kerr != KCGI_OK)
1392 goto done;
1395 if (gw_trans->next_id || gw_trans->page > 0) {
1396 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1397 if (kerr != KCGI_OK)
1398 return gw_kcgi_error(kerr);
1401 if (gw_trans->next_id) {
1402 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1403 KATTR_ID, "nav_next", KATTR__MAX);
1404 if (kerr != KCGI_OK)
1405 goto done;
1407 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1408 KATTRX_STRING, gw_trans->repo_name, "page",
1409 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1410 KATTRX_STRING, "briefs", "commit", KATTRX_STRING,
1411 gw_trans->next_id, "prev", KATTRX_STRING,
1412 gw_trans->next_prev_id ? gw_trans->next_prev_id : "",
1413 "prev_prev", KATTRX_STRING, gw_trans->prev_id ?
1414 gw_trans->prev_id : "", NULL);
1415 if (href_next == NULL) {
1416 error = got_error_from_errno("khttp_urlpartx");
1417 goto done;
1419 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1420 KATTR_HREF, href_next, KATTR__MAX);
1421 if (kerr != KCGI_OK)
1422 goto done;
1423 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1424 if (kerr != KCGI_OK)
1425 goto done;
1426 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1427 if (kerr != KCGI_OK)
1428 goto done;
1431 if (gw_trans->next_id || gw_trans->page > 0) {
1432 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1433 if (kerr != KCGI_OK)
1434 goto done;
1436 done:
1437 gw_free_header(header);
1438 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1439 gw_free_header(n_header);
1440 free(age);
1441 free(href_next);
1442 free(href_prev);
1443 free(href_diff);
1444 free(href_tree);
1445 if (error == NULL && kerr != KCGI_OK)
1446 error = gw_kcgi_error(kerr);
1447 return error;
1450 static const struct got_error *
1451 gw_summary(struct gw_trans *gw_trans)
1453 const struct got_error *error = NULL;
1454 char *age = NULL;
1455 enum kcgi_err kerr = KCGI_OK;
1457 #ifndef PROFILE
1458 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1459 return got_error_from_errno("pledge");
1460 #endif
1461 error = gw_apply_unveil(gw_trans->gw_dir->path);
1462 if (error)
1463 goto done;
1465 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1466 "summary_wrapper", KATTR__MAX);
1467 if (kerr != KCGI_OK)
1468 return gw_kcgi_error(kerr);
1470 if (gw_trans->gw_conf->got_show_repo_description &&
1471 gw_trans->gw_dir->description != NULL &&
1472 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1473 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1474 KATTR_ID, "description_title", KATTR__MAX);
1475 if (kerr != KCGI_OK)
1476 goto done;
1477 kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1478 if (kerr != KCGI_OK)
1479 goto done;
1480 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1481 if (kerr != KCGI_OK)
1482 goto done;
1483 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1484 KATTR_ID, "description", KATTR__MAX);
1485 if (kerr != KCGI_OK)
1486 goto done;
1487 kerr = khtml_puts(gw_trans->gw_html_req,
1488 gw_trans->gw_dir->description);
1489 if (kerr != KCGI_OK)
1490 goto done;
1491 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1492 if (kerr != KCGI_OK)
1493 goto done;
1496 if (gw_trans->gw_conf->got_show_repo_owner &&
1497 gw_trans->gw_dir->owner != NULL &&
1498 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
1499 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1500 KATTR_ID, "repo_owner_title", KATTR__MAX);
1501 if (kerr != KCGI_OK)
1502 goto done;
1503 kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1504 if (kerr != KCGI_OK)
1505 goto done;
1506 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1507 if (kerr != KCGI_OK)
1508 goto done;
1509 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1510 KATTR_ID, "repo_owner", KATTR__MAX);
1511 if (kerr != KCGI_OK)
1512 goto done;
1513 kerr = khtml_puts(gw_trans->gw_html_req,
1514 gw_trans->gw_dir->owner);
1515 if (kerr != KCGI_OK)
1516 goto done;
1517 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1518 if (kerr != KCGI_OK)
1519 goto done;
1522 if (gw_trans->gw_conf->got_show_repo_age) {
1523 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1524 NULL, TM_LONG);
1525 if (error)
1526 goto done;
1527 if (age != NULL) {
1528 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1529 KATTR_ID, "last_change_title", KATTR__MAX);
1530 if (kerr != KCGI_OK)
1531 goto done;
1532 kerr = khtml_puts(gw_trans->gw_html_req,
1533 "Last Change: ");
1534 if (kerr != KCGI_OK)
1535 goto done;
1536 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1537 if (kerr != KCGI_OK)
1538 goto done;
1539 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1540 KATTR_ID, "last_change", KATTR__MAX);
1541 if (kerr != KCGI_OK)
1542 goto done;
1543 kerr = khtml_puts(gw_trans->gw_html_req, age);
1544 if (kerr != KCGI_OK)
1545 goto done;
1546 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1547 if (kerr != KCGI_OK)
1548 goto done;
1552 if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1553 gw_trans->gw_dir->url != NULL &&
1554 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1555 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1556 KATTR_ID, "cloneurl_title", KATTR__MAX);
1557 if (kerr != KCGI_OK)
1558 goto done;
1559 kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1560 if (kerr != KCGI_OK)
1561 goto done;
1562 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1563 if (kerr != KCGI_OK)
1564 goto done;
1565 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1566 KATTR_ID, "cloneurl", KATTR__MAX);
1567 if (kerr != KCGI_OK)
1568 goto done;
1569 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1570 if (kerr != KCGI_OK)
1571 goto done;
1572 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1573 if (kerr != KCGI_OK)
1574 goto done;
1577 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1578 if (kerr != KCGI_OK)
1579 goto done;
1581 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1582 "briefs_title_wrapper", KATTR__MAX);
1583 if (kerr != KCGI_OK)
1584 goto done;
1585 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1586 "briefs_title", KATTR__MAX);
1587 if (kerr != KCGI_OK)
1588 goto done;
1589 kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1590 if (kerr != KCGI_OK)
1591 goto done;
1592 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1593 if (kerr != KCGI_OK)
1594 goto done;
1595 error = gw_briefs(gw_trans);
1596 if (error)
1597 goto done;
1599 error = gw_tags(gw_trans);
1600 if (error)
1601 goto done;
1603 error = gw_output_repo_heads(gw_trans);
1604 done:
1605 free(age);
1606 if (error == NULL && kerr != KCGI_OK)
1607 error = gw_kcgi_error(kerr);
1608 return error;
1611 static const struct got_error *
1612 gw_tree(struct gw_trans *gw_trans)
1614 const struct got_error *error = NULL;
1615 struct gw_header *header = NULL;
1616 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1617 char *age = NULL;
1618 enum kcgi_err kerr = KCGI_OK;
1620 #ifndef PROFILE
1621 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1622 return got_error_from_errno("pledge");
1623 #endif
1624 if ((header = gw_init_header()) == NULL)
1625 return got_error_from_errno("malloc");
1627 error = gw_apply_unveil(gw_trans->gw_dir->path);
1628 if (error)
1629 goto done;
1631 error = gw_get_header(gw_trans, header, 1);
1632 if (error)
1633 goto done;
1635 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1636 "tree_header_wrapper", KATTR__MAX);
1637 if (kerr != KCGI_OK)
1638 goto done;
1639 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1640 "tree_header", KATTR__MAX);
1641 if (kerr != KCGI_OK)
1642 goto done;
1643 error = gw_gen_tree_header(gw_trans, header->tree_id);
1644 if (error)
1645 goto done;
1646 error = gw_get_time_str(&age, header->committer_time,
1647 TM_LONG);
1648 if (error)
1649 goto done;
1650 error = gw_gen_age_header(gw_trans, age ?age : "");
1651 if (error)
1652 goto done;
1653 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1654 if (error)
1655 goto done;
1656 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1657 if (kerr != KCGI_OK)
1658 goto done;
1659 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1660 "dotted_line", KATTR__MAX);
1661 if (kerr != KCGI_OK)
1662 goto done;
1663 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1664 if (kerr != KCGI_OK)
1665 goto done;
1667 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1668 "tree", KATTR__MAX);
1669 if (kerr != KCGI_OK)
1670 goto done;
1671 error = gw_output_repo_tree(gw_trans, header);
1672 if (error)
1673 goto done;
1675 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1676 done:
1677 gw_free_header(header);
1678 free(tree_html_disp);
1679 free(tree_html);
1680 free(tree);
1681 free(age);
1682 if (error == NULL && kerr != KCGI_OK)
1683 error = gw_kcgi_error(kerr);
1684 return error;
1687 static const struct got_error *
1688 gw_tags(struct gw_trans *gw_trans)
1690 const struct got_error *error = NULL;
1691 struct gw_header *header = NULL;
1692 char *href_next = NULL, *href_prev = NULL;
1693 enum kcgi_err kerr = KCGI_OK;
1695 #ifndef PROFILE
1696 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1697 return got_error_from_errno("pledge");
1698 #endif
1699 if ((header = gw_init_header()) == NULL)
1700 return got_error_from_errno("malloc");
1702 if (gw_trans->action != GW_SUMMARY) {
1703 error = gw_apply_unveil(gw_trans->gw_dir->path);
1704 if (error)
1705 goto done;
1708 error = gw_get_header(gw_trans, header, 1);
1709 if (error)
1710 goto done;
1712 if (gw_trans->action == GW_SUMMARY) {
1713 gw_trans->next_id = NULL;
1714 error = gw_output_repo_tags(gw_trans, header,
1715 D_MAXSLCOMMDISP, TAGBRIEF);
1716 if (error)
1717 goto done;
1718 } else {
1719 error = gw_output_repo_tags(gw_trans, header,
1720 gw_trans->gw_conf->got_max_commits_display, TAGBRIEF);
1721 if (error)
1722 goto done;
1725 if (gw_trans->next_id || gw_trans->page > 0) {
1726 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1727 KATTR_ID, "np_wrapper", KATTR__MAX);
1728 if (kerr != KCGI_OK)
1729 goto done;
1730 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1731 KATTR_ID, "nav_prev", KATTR__MAX);
1732 if (kerr != KCGI_OK)
1733 goto done;
1736 if (gw_trans->page > 0 && gw_trans->prev_id) {
1737 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1738 KATTRX_STRING, gw_trans->repo_name, "page",
1739 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1740 KATTRX_STRING, "tags", "commit", KATTRX_STRING,
1741 gw_trans->prev_id ? gw_trans->prev_id : "", "prev",
1742 KATTRX_STRING, gw_trans->prev_prev_id ?
1743 gw_trans->prev_prev_id : "", NULL);
1744 if (href_prev == NULL) {
1745 error = got_error_from_errno("khttp_urlpartx");
1746 goto done;
1748 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1749 KATTR_HREF, href_prev, KATTR__MAX);
1750 if (kerr != KCGI_OK)
1751 goto done;
1752 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1753 if (kerr != KCGI_OK)
1754 goto done;
1755 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1756 if (kerr != KCGI_OK)
1757 goto done;
1760 if (gw_trans->next_id || gw_trans->page > 0) {
1761 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1762 if (kerr != KCGI_OK)
1763 return gw_kcgi_error(kerr);
1766 if (gw_trans->next_id) {
1767 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1768 KATTR_ID, "nav_next", KATTR__MAX);
1769 if (kerr != KCGI_OK)
1770 goto done;
1771 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1772 KATTRX_STRING, gw_trans->repo_name, "page",
1773 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1774 KATTRX_STRING, "tags", "commit", KATTRX_STRING,
1775 gw_trans->next_id, "prev", KATTRX_STRING,
1776 gw_trans->next_prev_id ? gw_trans->next_prev_id : "",
1777 "prev_prev", KATTRX_STRING, gw_trans->prev_id ?
1778 gw_trans->prev_id : "", NULL);
1779 if (href_next == NULL) {
1780 error = got_error_from_errno("khttp_urlpartx");
1781 goto done;
1783 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1784 KATTR_HREF, href_next, KATTR__MAX);
1785 if (kerr != KCGI_OK)
1786 goto done;
1787 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1788 if (kerr != KCGI_OK)
1789 goto done;
1790 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1791 if (kerr != KCGI_OK)
1792 goto done;
1795 if (gw_trans->next_id || gw_trans->page > 0) {
1796 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1797 if (kerr != KCGI_OK)
1798 goto done;
1800 done:
1801 gw_free_header(header);
1802 free(href_next);
1803 free(href_prev);
1804 if (error == NULL && kerr != KCGI_OK)
1805 error = gw_kcgi_error(kerr);
1806 return error;
1809 static const struct got_error *
1810 gw_tag(struct gw_trans *gw_trans)
1812 const struct got_error *error = NULL;
1813 struct gw_header *header = NULL;
1814 enum kcgi_err kerr = KCGI_OK;
1816 #ifndef PROFILE
1817 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1818 return got_error_from_errno("pledge");
1819 #endif
1820 if ((header = gw_init_header()) == NULL)
1821 return got_error_from_errno("malloc");
1823 error = gw_apply_unveil(gw_trans->gw_dir->path);
1824 if (error)
1825 goto done;
1827 if (gw_trans->commit_id == NULL) {
1828 error = got_error_msg(GOT_ERR_QUERYSTRING,
1829 "commit required in querystring");
1830 goto done;
1833 error = gw_get_header(gw_trans, header, 1);
1834 if (error)
1835 goto done;
1837 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1838 "tag_header_wrapper", KATTR__MAX);
1839 if (kerr != KCGI_OK)
1840 goto done;
1841 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1842 "tag_header", KATTR__MAX);
1843 if (kerr != KCGI_OK)
1844 goto done;
1845 error = gw_gen_commit_header(gw_trans, header->commit_id,
1846 header->refs_str);
1847 if (error)
1848 goto done;
1849 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1850 if (error)
1851 goto done;
1852 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1853 if (kerr != KCGI_OK)
1854 goto done;
1855 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1856 "dotted_line", KATTR__MAX);
1857 if (kerr != KCGI_OK)
1858 goto done;
1859 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1860 if (kerr != KCGI_OK)
1861 goto done;
1863 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1864 "tree", KATTR__MAX);
1865 if (kerr != KCGI_OK)
1866 goto done;
1868 error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1869 if (error)
1870 goto done;
1872 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1873 done:
1874 gw_free_header(header);
1875 if (error == NULL && kerr != KCGI_OK)
1876 error = gw_kcgi_error(kerr);
1877 return error;
1880 static const struct got_error *
1881 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1883 const struct got_error *error = NULL;
1884 DIR *dt;
1885 char *dir_test;
1886 int opened = 0;
1888 if (asprintf(&dir_test, "%s/%s/%s",
1889 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1890 GOTWEB_GIT_DIR) == -1)
1891 return got_error_from_errno("asprintf");
1893 dt = opendir(dir_test);
1894 if (dt == NULL) {
1895 free(dir_test);
1896 } else {
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;
1903 opened = 1;
1904 goto done;
1907 if (asprintf(&dir_test, "%s/%s/%s",
1908 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1909 GOTWEB_GOT_DIR) == -1) {
1910 dir_test = NULL;
1911 error = got_error_from_errno("asprintf");
1912 goto errored;
1915 dt = opendir(dir_test);
1916 if (dt == NULL)
1917 free(dir_test);
1918 else {
1919 opened = 1;
1920 error = got_error(GOT_ERR_NOT_GIT_REPO);
1921 goto errored;
1924 if (asprintf(&dir_test, "%s/%s",
1925 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1) {
1926 error = got_error_from_errno("asprintf");
1927 dir_test = NULL;
1928 goto errored;
1931 gw_dir->path = strdup(dir_test);
1932 if (gw_dir->path == NULL) {
1933 opened = 1;
1934 error = got_error_from_errno("strdup");
1935 goto errored;
1938 dt = opendir(dir_test);
1939 if (dt == NULL) {
1940 error = got_error_path(gw_dir->name, GOT_ERR_NOT_GIT_REPO);
1941 goto errored;
1942 } else
1943 opened = 1;
1944 done:
1945 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1946 gw_dir->path);
1947 if (error)
1948 goto errored;
1949 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1950 if (error)
1951 goto errored;
1952 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1953 NULL, TM_DIFF);
1954 if (error)
1955 goto errored;
1956 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1957 errored:
1958 free(dir_test);
1959 if (opened)
1960 if (dt && closedir(dt) == -1 && error == NULL)
1961 error = got_error_from_errno("closedir");
1962 return error;
1965 static const struct got_error *
1966 gw_load_got_paths(struct gw_trans *gw_trans)
1968 const struct got_error *error = NULL;
1969 DIR *d;
1970 struct dirent **sd_dent;
1971 struct gw_dir *gw_dir;
1972 struct stat st;
1973 unsigned int d_cnt, d_i;
1975 d = opendir(gw_trans->gw_conf->got_repos_path);
1976 if (d == NULL) {
1977 error = got_error_from_errno2("opendir",
1978 gw_trans->gw_conf->got_repos_path);
1979 return error;
1982 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1983 alphasort);
1984 if (d_cnt == -1) {
1985 error = got_error_from_errno2("scandir",
1986 gw_trans->gw_conf->got_repos_path);
1987 goto done;
1990 for (d_i = 0; d_i < d_cnt; d_i++) {
1991 if (gw_trans->gw_conf->got_max_repos > 0 &&
1992 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1993 break; /* account for parent and self */
1995 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1996 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1997 continue;
1999 error = gw_init_gw_dir(&gw_dir, sd_dent[d_i]->d_name);
2000 if (error)
2001 goto done;
2003 error = gw_load_got_path(gw_trans, gw_dir);
2004 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
2005 error = NULL;
2006 continue;
2008 else if (error)
2009 goto done;
2011 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
2012 !got_path_dir_is_empty(gw_dir->path)) {
2013 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
2014 entry);
2015 gw_trans->repos_total++;
2018 done:
2019 if (d && closedir(d) == -1 && error == NULL)
2020 error = got_error_from_errno("closedir");
2021 return error;
2024 static const struct got_error *
2025 gw_parse_querystring(struct gw_trans *gw_trans)
2027 const struct got_error *error = NULL;
2028 struct kpair *p;
2029 struct gw_query_action *action = NULL;
2030 unsigned int i;
2032 if (gw_trans->gw_req->fieldnmap[0]) {
2033 return got_error(GOT_ERR_QUERYSTRING);
2034 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
2035 /* define gw_trans->repo_path */
2036 gw_trans->repo_name = p->parsed.s;
2038 if (asprintf(&gw_trans->repo_path, "%s/%s",
2039 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
2040 return got_error_from_errno("asprintf");
2042 /* get action and set function */
2043 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION])) {
2044 for (i = 0; i < nitems(gw_query_funcs); i++) {
2045 action = &gw_query_funcs[i];
2046 if (action->func_name == NULL)
2047 continue;
2048 if (strcmp(action->func_name,
2049 p->parsed.s) == 0) {
2050 gw_trans->action = i;
2051 break;
2055 if (gw_trans->action == -1) {
2056 gw_trans->action = GW_ERR;
2057 gw_trans->error = got_error_msg(GOT_ERR_QUERYSTRING,
2058 p != NULL ? "bad action in querystring" :
2059 "no action in querystring");
2060 return error;
2063 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID])) {
2064 if (asprintf(&gw_trans->commit_id, "%s",
2065 p->parsed.s) == -1)
2066 return got_error_from_errno("asprintf");
2069 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
2070 gw_trans->repo_file = p->parsed.s;
2072 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER])) {
2073 if (asprintf(&gw_trans->repo_folder, "%s",
2074 p->parsed.s) == -1)
2075 return got_error_from_errno("asprintf");
2078 if ((p = gw_trans->gw_req->fieldmap[KEY_PREV_ID])) {
2079 if (asprintf(&gw_trans->prev_id, "%s",
2080 p->parsed.s) == -1)
2081 return got_error_from_errno("asprintf");
2084 if ((p = gw_trans->gw_req->fieldmap[KEY_PREV_PREV_ID])) {
2085 if (asprintf(&gw_trans->prev_prev_id, "%s",
2086 p->parsed.s) == -1)
2087 return got_error_from_errno("asprintf");
2090 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
2091 gw_trans->headref = p->parsed.s;
2093 error = gw_init_gw_dir(&gw_trans->gw_dir, gw_trans->repo_name);
2094 if (error)
2095 return error;
2097 gw_trans->error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
2098 } else
2099 gw_trans->action = GW_INDEX;
2101 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
2102 gw_trans->page = p->parsed.i;
2104 return error;
2107 static const struct got_error *
2108 gw_init_gw_dir(struct gw_dir **gw_dir, const char *dir)
2110 const struct got_error *error;
2112 *gw_dir = malloc(sizeof(**gw_dir));
2113 if (*gw_dir == NULL)
2114 return got_error_from_errno("malloc");
2116 if (asprintf(&(*gw_dir)->name, "%s", dir) == -1) {
2117 error = got_error_from_errno("asprintf");
2118 free(*gw_dir);
2119 *gw_dir = NULL;
2120 return error;
2123 return NULL;
2126 static const struct got_error *
2127 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
2129 enum kcgi_err kerr = KCGI_OK;
2131 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
2132 if (kerr != KCGI_OK)
2133 return gw_kcgi_error(kerr);
2134 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
2135 khttps[code]);
2136 if (kerr != KCGI_OK)
2137 return gw_kcgi_error(kerr);
2138 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
2139 kmimetypes[mime]);
2140 if (kerr != KCGI_OK)
2141 return gw_kcgi_error(kerr);
2142 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
2143 "nosniff");
2144 if (kerr != KCGI_OK)
2145 return gw_kcgi_error(kerr);
2146 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
2147 if (kerr != KCGI_OK)
2148 return gw_kcgi_error(kerr);
2149 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
2150 "1; mode=block");
2151 if (kerr != KCGI_OK)
2152 return gw_kcgi_error(kerr);
2154 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
2155 kerr = khttp_head(gw_trans->gw_req,
2156 kresps[KRESP_CONTENT_DISPOSITION],
2157 "attachment; filename=%s", gw_trans->repo_file);
2158 if (kerr != KCGI_OK)
2159 return gw_kcgi_error(kerr);
2162 kerr = khttp_body(gw_trans->gw_req);
2163 return gw_kcgi_error(kerr);
2166 static const struct got_error *
2167 gw_display_index(struct gw_trans *gw_trans)
2169 const struct got_error *error;
2170 enum kcgi_err kerr = KCGI_OK;
2172 /* catch early querystring errors */
2173 if (gw_trans->error)
2174 gw_trans->action = GW_ERR;
2176 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
2177 if (error)
2178 return error;
2180 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
2181 if (kerr != KCGI_OK)
2182 return gw_kcgi_error(kerr);
2184 if (gw_trans->action != GW_BLOB) {
2185 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
2186 gw_query_funcs[gw_trans->action].template);
2187 if (kerr != KCGI_OK) {
2188 khtml_close(gw_trans->gw_html_req);
2189 return gw_kcgi_error(kerr);
2193 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
2196 static const struct got_error *
2197 gw_error(struct gw_trans *gw_trans)
2199 enum kcgi_err kerr = KCGI_OK;
2201 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->error->msg);
2203 return gw_kcgi_error(kerr);
2206 static int
2207 gw_template(size_t key, void *arg)
2209 const struct got_error *error = NULL;
2210 enum kcgi_err kerr = KCGI_OK;
2211 struct gw_trans *gw_trans = arg;
2212 char *ati = NULL, *fic32 = NULL, *fic16 = NULL;
2213 char *swm = NULL, *spt = NULL, *css = NULL, *logo = NULL;
2215 if (asprintf(&ati, "%s%s", gw_trans->gw_conf->got_www_path,
2216 "/apple-touch-icon.png") == -1)
2217 goto err;
2218 if (asprintf(&fic32, "%s%s", gw_trans->gw_conf->got_www_path,
2219 "/favicon-32x32.png") == -1)
2220 goto err;
2221 if (asprintf(&fic16, "%s%s", gw_trans->gw_conf->got_www_path,
2222 "/favicon-16x16.png") == -1)
2223 goto err;
2224 if (asprintf(&swm, "%s%s", gw_trans->gw_conf->got_www_path,
2225 "/site.webmanifest") == -1)
2226 goto err;
2227 if (asprintf(&spt, "%s%s", gw_trans->gw_conf->got_www_path,
2228 "/safari-pinned-tab.svg") == -1)
2229 goto err;
2230 if (asprintf(&css, "%s%s", gw_trans->gw_conf->got_www_path,
2231 "/gotweb.css") == -1)
2232 goto err;
2233 if (asprintf(&logo, "%s%s%s", gw_trans->gw_conf->got_www_path,
2234 gw_trans->gw_conf->got_www_path ? "/" : "",
2235 gw_trans->gw_conf->got_logo) == -1)
2236 goto err;
2238 switch (key) {
2239 case (TEMPL_HEAD):
2240 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2241 KATTR_NAME, "viewport",
2242 KATTR_CONTENT, "initial-scale=.75, user-scalable=yes",
2243 KATTR__MAX);
2244 if (kerr != KCGI_OK)
2245 return 0;
2246 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2247 if (kerr != KCGI_OK)
2248 return 0;
2249 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2250 KATTR_CHARSET, "utf-8",
2251 KATTR__MAX);
2252 if (kerr != KCGI_OK)
2253 return 0;
2254 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2255 if (kerr != KCGI_OK)
2256 return 0;
2257 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2258 KATTR_NAME, "msapplication-TileColor",
2259 KATTR_CONTENT, "#da532c", KATTR__MAX);
2260 if (kerr != KCGI_OK)
2261 return 0;
2262 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2263 if (kerr != KCGI_OK)
2264 return 0;
2265 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2266 KATTR_NAME, "theme-color",
2267 KATTR_CONTENT, "#ffffff", KATTR__MAX);
2268 if (kerr != KCGI_OK)
2269 return 0;
2270 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2271 if (kerr != KCGI_OK)
2272 return 0;
2273 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2274 KATTR_REL, "apple-touch-icon", KATTR_SIZES, "180x180",
2275 KATTR_HREF, ati, KATTR__MAX);
2276 if (kerr != KCGI_OK)
2277 return 0;
2278 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2279 if (kerr != KCGI_OK)
2280 return 0;
2281 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2282 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2283 "32x32", KATTR_HREF, fic32, KATTR__MAX);
2284 if (kerr != KCGI_OK)
2285 return 0;
2286 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2287 if (kerr != KCGI_OK)
2288 return 0;
2289 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2290 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2291 "16x16", KATTR_HREF, fic16, KATTR__MAX);
2292 if (kerr != KCGI_OK)
2293 return 0;
2294 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2295 if (kerr != KCGI_OK)
2296 return 0;
2297 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2298 KATTR_REL, "manifest", KATTR_HREF, swm,
2299 KATTR__MAX);
2300 if (kerr != KCGI_OK)
2301 return 0;
2302 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2303 if (kerr != KCGI_OK)
2304 return 0;
2305 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2306 KATTR_REL, "mask-icon", KATTR_HREF,
2307 spt, KATTR__MAX);
2308 if (kerr != KCGI_OK)
2309 return 0;
2310 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2311 if (kerr != KCGI_OK)
2312 return 0;
2313 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2314 KATTR_REL, "stylesheet", KATTR_TYPE, "text/css",
2315 KATTR_HREF, css, KATTR__MAX);
2316 if (kerr != KCGI_OK)
2317 return 0;
2318 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2319 if (kerr != KCGI_OK)
2320 return 0;
2321 break;
2322 case(TEMPL_HEADER):
2323 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2324 KATTR_ID, "got_link", KATTR__MAX);
2325 if (kerr != KCGI_OK)
2326 return 0;
2327 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2328 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
2329 KATTR_TARGET, "_sotd", KATTR__MAX);
2330 if (kerr != KCGI_OK)
2331 return 0;
2332 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
2333 KATTR_SRC, logo, KATTR__MAX);
2334 if (kerr != KCGI_OK)
2335 return 0;
2336 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2337 if (kerr != KCGI_OK)
2338 return 0;
2339 break;
2340 case (TEMPL_SITEPATH):
2341 error = gw_output_site_link(gw_trans);
2342 if (error)
2343 return 0;
2344 break;
2345 case(TEMPL_TITLE):
2346 if (gw_trans->gw_conf->got_site_name != NULL) {
2347 kerr = khtml_puts(gw_trans->gw_html_req,
2348 gw_trans->gw_conf->got_site_name);
2349 if (kerr != KCGI_OK)
2350 return 0;
2352 break;
2353 case (TEMPL_SEARCH):
2354 break;
2355 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
2356 "search", KATTR__MAX);
2357 if (kerr != KCGI_OK)
2358 return 0;
2359 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
2360 KATTR_METHOD, "POST", KATTR__MAX);
2361 if (kerr != KCGI_OK)
2362 return 0;
2363 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
2364 "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
2365 KATTR_MAXLENGTH, "50", KATTR__MAX);
2366 if (kerr != KCGI_OK)
2367 return 0;
2368 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
2369 KATTR__MAX);
2370 if (kerr != KCGI_OK)
2371 return 0;
2372 kerr = khtml_puts(gw_trans->gw_html_req, "Search");
2373 if (kerr != KCGI_OK)
2374 return 0;
2375 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
2376 if (kerr != KCGI_OK)
2377 return 0;
2378 break;
2379 case(TEMPL_SITEOWNER):
2380 if (gw_trans->gw_conf->got_site_owner != NULL &&
2381 gw_trans->gw_conf->got_show_site_owner) {
2382 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2383 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
2384 if (kerr != KCGI_OK)
2385 return 0;
2386 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2387 KATTR_ID, "site_owner", KATTR__MAX);
2388 if (kerr != KCGI_OK)
2389 return 0;
2390 kerr = khtml_puts(gw_trans->gw_html_req,
2391 gw_trans->gw_conf->got_site_owner);
2392 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2393 if (kerr != KCGI_OK)
2394 return 0;
2396 break;
2397 case(TEMPL_CONTENT):
2398 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
2399 if (error) {
2400 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2401 KATTR_ID, "tmpl_err", KATTR__MAX);
2402 if (kerr != KCGI_OK)
2403 return 0;
2404 kerr = khttp_printf(gw_trans->gw_req, "Error: %s",
2405 error->msg);
2406 if (kerr != KCGI_OK)
2407 return 0;
2408 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2409 if (kerr != KCGI_OK)
2410 return 0;
2412 break;
2413 default:
2414 return 0;
2416 free(ati);
2417 free(fic32);
2418 free(fic16);
2419 free(swm);
2420 free(spt);
2421 free(css);
2422 free(logo);
2423 return 1;
2424 err:
2425 free(ati);
2426 free(fic32);
2427 free(fic16);
2428 free(swm);
2429 free(spt);
2430 free(css);
2431 free(logo);
2432 return 0;
2435 static const struct got_error *
2436 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
2438 const struct got_error *error = NULL;
2439 enum kcgi_err kerr = KCGI_OK;
2441 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2442 KATTR_ID, "header_commit_title", KATTR__MAX);
2443 if (kerr != KCGI_OK)
2444 goto done;
2445 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
2446 if (kerr != KCGI_OK)
2447 goto done;
2448 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2449 if (kerr != KCGI_OK)
2450 goto done;
2451 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2452 KATTR_ID, "header_commit", KATTR__MAX);
2453 if (kerr != KCGI_OK)
2454 goto done;
2455 kerr = khtml_printf(gw_trans->gw_html_req, "%s ", str1);
2456 if (kerr != KCGI_OK)
2457 goto done;
2458 if (str2 != NULL) {
2459 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
2460 KATTR_ID, "refs_str", KATTR__MAX);
2461 if (kerr != KCGI_OK)
2462 goto done;
2463 kerr = khtml_printf(gw_trans->gw_html_req, "(%s)", str2);
2464 if (kerr != KCGI_OK)
2465 goto done;
2466 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2467 if (kerr != KCGI_OK)
2468 goto done;
2470 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2471 done:
2472 if (error == NULL && kerr != KCGI_OK)
2473 error = gw_kcgi_error(kerr);
2474 return error;
2477 static const struct got_error *
2478 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2480 const struct got_error *error = NULL;
2481 enum kcgi_err kerr = KCGI_OK;
2483 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2484 KATTR_ID, "header_diff_title", KATTR__MAX);
2485 if (kerr != KCGI_OK)
2486 goto done;
2487 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2488 if (kerr != KCGI_OK)
2489 goto done;
2490 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2491 if (kerr != KCGI_OK)
2492 goto done;
2493 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2494 KATTR_ID, "header_diff", KATTR__MAX);
2495 if (kerr != KCGI_OK)
2496 goto done;
2497 if (str1 != NULL) {
2498 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2499 if (kerr != KCGI_OK)
2500 goto done;
2502 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2503 if (kerr != KCGI_OK)
2504 goto done;
2505 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2506 if (kerr != KCGI_OK)
2507 goto done;
2508 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2509 done:
2510 if (error == NULL && kerr != KCGI_OK)
2511 error = gw_kcgi_error(kerr);
2512 return error;
2515 static const struct got_error *
2516 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2518 const struct got_error *error = NULL;
2519 enum kcgi_err kerr = KCGI_OK;
2521 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2522 KATTR_ID, "header_age_title", KATTR__MAX);
2523 if (kerr != KCGI_OK)
2524 goto done;
2525 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2526 if (kerr != KCGI_OK)
2527 goto done;
2528 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2529 if (kerr != KCGI_OK)
2530 goto done;
2531 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2532 KATTR_ID, "header_age", KATTR__MAX);
2533 if (kerr != KCGI_OK)
2534 goto done;
2535 kerr = khtml_puts(gw_trans->gw_html_req, str);
2536 if (kerr != KCGI_OK)
2537 goto done;
2538 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2539 done:
2540 if (error == NULL && kerr != KCGI_OK)
2541 error = gw_kcgi_error(kerr);
2542 return error;
2545 static const struct got_error *
2546 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2548 const struct got_error *error = NULL;
2549 enum kcgi_err kerr = KCGI_OK;
2551 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2552 KATTR_ID, "header_author_title", KATTR__MAX);
2553 if (kerr != KCGI_OK)
2554 goto done;
2555 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2556 if (kerr != KCGI_OK)
2557 goto done;
2558 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2559 if (kerr != KCGI_OK)
2560 goto done;
2561 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2562 KATTR_ID, "header_author", KATTR__MAX);
2563 if (kerr != KCGI_OK)
2564 goto done;
2565 kerr = khtml_puts(gw_trans->gw_html_req, str);
2566 if (kerr != KCGI_OK)
2567 goto done;
2568 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2569 done:
2570 if (error == NULL && kerr != KCGI_OK)
2571 error = gw_kcgi_error(kerr);
2572 return error;
2575 static const struct got_error *
2576 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2578 const struct got_error *error = NULL;
2579 enum kcgi_err kerr = KCGI_OK;
2581 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2582 KATTR_ID, "header_committer_title", KATTR__MAX);
2583 if (kerr != KCGI_OK)
2584 goto done;
2585 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2586 if (kerr != KCGI_OK)
2587 goto done;
2588 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2589 if (kerr != KCGI_OK)
2590 goto done;
2591 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2592 KATTR_ID, "header_committer", KATTR__MAX);
2593 if (kerr != KCGI_OK)
2594 goto done;
2595 kerr = khtml_puts(gw_trans->gw_html_req, str);
2596 if (kerr != KCGI_OK)
2597 goto done;
2598 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2599 done:
2600 if (error == NULL && kerr != KCGI_OK)
2601 error = gw_kcgi_error(kerr);
2602 return error;
2605 static const struct got_error *
2606 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2608 const struct got_error *error = NULL;
2609 enum kcgi_err kerr = KCGI_OK;
2611 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2612 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2613 if (kerr != KCGI_OK)
2614 goto done;
2615 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2616 if (kerr != KCGI_OK)
2617 goto done;
2618 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2619 if (kerr != KCGI_OK)
2620 goto done;
2621 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2622 KATTR_ID, "header_commit_msg", KATTR__MAX);
2623 if (kerr != KCGI_OK)
2624 goto done;
2625 kerr = khttp_puts(gw_trans->gw_req, str);
2626 if (kerr != KCGI_OK)
2627 goto done;
2628 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2629 done:
2630 if (error == NULL && kerr != KCGI_OK)
2631 error = gw_kcgi_error(kerr);
2632 return error;
2635 static const struct got_error *
2636 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2638 const struct got_error *error = NULL;
2639 enum kcgi_err kerr = KCGI_OK;
2641 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2642 KATTR_ID, "header_tree_title", KATTR__MAX);
2643 if (kerr != KCGI_OK)
2644 goto done;
2645 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2646 if (kerr != KCGI_OK)
2647 goto done;
2648 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2649 if (kerr != KCGI_OK)
2650 goto done;
2651 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2652 KATTR_ID, "header_tree", KATTR__MAX);
2653 if (kerr != KCGI_OK)
2654 goto done;
2655 kerr = khtml_puts(gw_trans->gw_html_req, str);
2656 if (kerr != KCGI_OK)
2657 goto done;
2658 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2659 done:
2660 if (error == NULL && kerr != KCGI_OK)
2661 error = gw_kcgi_error(kerr);
2662 return error;
2665 static const struct got_error *
2666 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2667 char *dir)
2669 const struct got_error *error = NULL;
2670 FILE *f = NULL;
2671 char *d_file = NULL;
2672 unsigned int len;
2673 size_t n;
2675 *description = NULL;
2676 if (gw_trans->gw_conf->got_show_repo_description == 0)
2677 return NULL;
2679 if (asprintf(&d_file, "%s/description", dir) == -1)
2680 return got_error_from_errno("asprintf");
2682 f = fopen(d_file, "r");
2683 if (f == NULL) {
2684 if (errno == ENOENT || errno == EACCES)
2685 return NULL;
2686 error = got_error_from_errno2("fopen", d_file);
2687 goto done;
2690 if (fseek(f, 0, SEEK_END) == -1) {
2691 error = got_ferror(f, GOT_ERR_IO);
2692 goto done;
2694 len = ftell(f);
2695 if (len == -1) {
2696 error = got_ferror(f, GOT_ERR_IO);
2697 goto done;
2699 if (fseek(f, 0, SEEK_SET) == -1) {
2700 error = got_ferror(f, GOT_ERR_IO);
2701 goto done;
2703 *description = calloc(len + 1, sizeof(**description));
2704 if (*description == NULL) {
2705 error = got_error_from_errno("calloc");
2706 goto done;
2709 n = fread(*description, 1, len, f);
2710 if (n == 0 && ferror(f))
2711 error = got_ferror(f, GOT_ERR_IO);
2712 done:
2713 if (f != NULL && fclose(f) == EOF && error == NULL)
2714 error = got_error_from_errno("fclose");
2715 free(d_file);
2716 return error;
2719 static const struct got_error *
2720 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2722 struct tm tm;
2723 time_t diff_time;
2724 char *years = "years ago", *months = "months ago";
2725 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2726 char *minutes = "minutes ago", *seconds = "seconds ago";
2727 char *now = "right now";
2728 char *s;
2729 char datebuf[29];
2731 *repo_age = NULL;
2733 switch (ref_tm) {
2734 case TM_DIFF:
2735 diff_time = time(NULL) - committer_time;
2736 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2737 if (asprintf(repo_age, "%lld %s",
2738 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2739 return got_error_from_errno("asprintf");
2740 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2741 if (asprintf(repo_age, "%lld %s",
2742 (diff_time / 60 / 60 / 24 / (365 / 12)),
2743 months) == -1)
2744 return got_error_from_errno("asprintf");
2745 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2746 if (asprintf(repo_age, "%lld %s",
2747 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2748 return got_error_from_errno("asprintf");
2749 } else if (diff_time > 60 * 60 * 24 * 2) {
2750 if (asprintf(repo_age, "%lld %s",
2751 (diff_time / 60 / 60 / 24), days) == -1)
2752 return got_error_from_errno("asprintf");
2753 } else if (diff_time > 60 * 60 * 2) {
2754 if (asprintf(repo_age, "%lld %s",
2755 (diff_time / 60 / 60), hours) == -1)
2756 return got_error_from_errno("asprintf");
2757 } else if (diff_time > 60 * 2) {
2758 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2759 minutes) == -1)
2760 return got_error_from_errno("asprintf");
2761 } else if (diff_time > 2) {
2762 if (asprintf(repo_age, "%lld %s", diff_time,
2763 seconds) == -1)
2764 return got_error_from_errno("asprintf");
2765 } else {
2766 if (asprintf(repo_age, "%s", now) == -1)
2767 return got_error_from_errno("asprintf");
2769 break;
2770 case TM_LONG:
2771 if (gmtime_r(&committer_time, &tm) == NULL)
2772 return got_error_from_errno("gmtime_r");
2774 s = asctime_r(&tm, datebuf);
2775 if (s == NULL)
2776 return got_error_from_errno("asctime_r");
2778 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2779 return got_error_from_errno("asprintf");
2780 break;
2782 return NULL;
2785 static const struct got_error *
2786 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2787 const char *refname, int ref_tm)
2789 const struct got_error *error = NULL;
2790 struct got_repository *repo = NULL;
2791 struct got_commit_object *commit = NULL;
2792 struct got_reflist_head refs;
2793 struct got_reflist_entry *re;
2794 time_t committer_time = 0, cmp_time = 0;
2796 *repo_age = NULL;
2797 TAILQ_INIT(&refs);
2799 if (gw_trans->gw_conf->got_show_repo_age == 0)
2800 return NULL;
2802 if (gw_trans->repo)
2803 repo = gw_trans->repo;
2804 else {
2805 error = got_repo_open(&repo, dir, NULL);
2806 if (error)
2807 return error;
2810 error = got_ref_list(&refs, repo, "refs/heads",
2811 got_ref_cmp_by_name, NULL);
2812 if (error)
2813 goto done;
2816 * Find the youngest branch tip in the repository, or the age of
2817 * the a specific branch tip if a name was provided by the caller.
2819 TAILQ_FOREACH(re, &refs, entry) {
2820 struct got_object_id *id = NULL;
2822 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
2823 continue;
2825 error = got_ref_resolve(&id, repo, re->ref);
2826 if (error)
2827 goto done;
2829 error = got_object_open_as_commit(&commit, repo, id);
2830 free(id);
2831 if (error)
2832 goto done;
2834 committer_time =
2835 got_object_commit_get_committer_time(commit);
2836 got_object_commit_close(commit);
2837 if (cmp_time < committer_time)
2838 cmp_time = committer_time;
2840 if (refname)
2841 break;
2844 if (cmp_time != 0) {
2845 committer_time = cmp_time;
2846 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2848 done:
2849 got_ref_list_free(&refs);
2850 if (gw_trans->repo == NULL) {
2851 const struct got_error *close_err = got_repo_close(repo);
2852 if (error == NULL)
2853 error = close_err;
2855 return error;
2858 static const struct got_error *
2859 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2861 const struct got_error *error;
2862 FILE *f = NULL;
2863 struct got_object_id *id1 = NULL, *id2 = NULL;
2864 char *label1 = NULL, *label2 = NULL, *line = NULL;
2865 int obj_type;
2866 size_t linesize = 0;
2867 ssize_t linelen;
2868 enum kcgi_err kerr = KCGI_OK;
2870 f = got_opentemp();
2871 if (f == NULL)
2872 return NULL;
2874 if (header->parent_id != NULL &&
2875 strncmp(header->parent_id, "/dev/null", 9) != 0) {
2876 error = got_repo_match_object_id(&id1, &label1,
2877 header->parent_id, GOT_OBJ_TYPE_ANY,
2878 &header->refs, gw_trans->repo);
2879 if (error)
2880 goto done;
2883 error = got_repo_match_object_id(&id2, &label2,
2884 header->commit_id, GOT_OBJ_TYPE_ANY, &header->refs,
2885 gw_trans->repo);
2886 if (error)
2887 goto done;
2889 error = got_object_get_type(&obj_type, gw_trans->repo, id2);
2890 if (error)
2891 goto done;
2892 switch (obj_type) {
2893 case GOT_OBJ_TYPE_BLOB:
2894 error = got_diff_objects_as_blobs(NULL, NULL, id1, id2,
2895 NULL, NULL, 3, 0, 0, gw_trans->repo, f);
2896 break;
2897 case GOT_OBJ_TYPE_TREE:
2898 error = got_diff_objects_as_trees(NULL, NULL, id1, id2,
2899 "", "", 3, 0, 0, gw_trans->repo, f);
2900 break;
2901 case GOT_OBJ_TYPE_COMMIT:
2902 error = got_diff_objects_as_commits(NULL, NULL, id1, id2,
2903 3, 0, 0, gw_trans->repo, f);
2904 break;
2905 default:
2906 error = got_error(GOT_ERR_OBJ_TYPE);
2908 if (error)
2909 goto done;
2911 if (fseek(f, 0, SEEK_SET) == -1) {
2912 error = got_ferror(f, GOT_ERR_IO);
2913 goto done;
2916 while ((linelen = getline(&line, &linesize, f)) != -1) {
2917 error = gw_colordiff_line(gw_trans, line);
2918 if (error)
2919 goto done;
2920 /* XXX: KHTML_PRETTY breaks this */
2921 kerr = khtml_puts(gw_trans->gw_html_req, line);
2922 if (kerr != KCGI_OK)
2923 goto done;
2924 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2925 if (kerr != KCGI_OK)
2926 goto done;
2928 if (linelen == -1 && ferror(f))
2929 error = got_error_from_errno("getline");
2930 done:
2931 if (f && fclose(f) == EOF && error == NULL)
2932 error = got_error_from_errno("fclose");
2933 free(line);
2934 free(label1);
2935 free(label2);
2936 free(id1);
2937 free(id2);
2939 if (error == NULL && kerr != KCGI_OK)
2940 error = gw_kcgi_error(kerr);
2941 return error;
2944 static const struct got_error *
2945 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2947 const struct got_error *error = NULL, *close_err;
2948 struct got_repository *repo;
2949 const char *gitconfig_owner;
2951 *owner = NULL;
2953 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2954 return NULL;
2956 error = got_repo_open(&repo, dir, NULL);
2957 if (error)
2958 return error;
2959 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2960 if (gitconfig_owner) {
2961 *owner = strdup(gitconfig_owner);
2962 if (*owner == NULL)
2963 error = got_error_from_errno("strdup");
2965 close_err = got_repo_close(repo);
2966 if (error == NULL)
2967 error = close_err;
2968 return error;
2971 static const struct got_error *
2972 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2974 const struct got_error *error = NULL;
2975 FILE *f;
2976 char *d_file = NULL;
2977 unsigned int len;
2978 size_t n;
2980 *url = NULL;
2982 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2983 return got_error_from_errno("asprintf");
2985 f = fopen(d_file, "r");
2986 if (f == NULL) {
2987 if (errno != ENOENT && errno != EACCES)
2988 error = got_error_from_errno2("fopen", d_file);
2989 goto done;
2992 if (fseek(f, 0, SEEK_END) == -1) {
2993 error = got_ferror(f, GOT_ERR_IO);
2994 goto done;
2996 len = ftell(f);
2997 if (len == -1) {
2998 error = got_ferror(f, GOT_ERR_IO);
2999 goto done;
3001 if (fseek(f, 0, SEEK_SET) == -1) {
3002 error = got_ferror(f, GOT_ERR_IO);
3003 goto done;
3006 *url = calloc(len + 1, sizeof(**url));
3007 if (*url == NULL) {
3008 error = got_error_from_errno("calloc");
3009 goto done;
3012 n = fread(*url, 1, len, f);
3013 if (n == 0 && ferror(f))
3014 error = got_ferror(f, GOT_ERR_IO);
3015 done:
3016 if (f && fclose(f) == EOF && error == NULL)
3017 error = got_error_from_errno("fclose");
3018 free(d_file);
3019 return NULL;
3022 static const struct got_error *
3023 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
3024 int limit, int tag_type)
3026 const struct got_error *error = NULL;
3027 struct got_reflist_head refs;
3028 struct got_reflist_entry *re;
3029 char *age = NULL;
3030 char *id_str = NULL, *newline, *href_commits = NULL;
3031 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
3032 struct got_tag_object *tag = NULL;
3033 enum kcgi_err kerr = KCGI_OK;
3034 int summary_header_displayed = 0, start_tag = 0, chk_next = 0;
3035 int prev_set = 0, tag_count = 0;
3037 TAILQ_INIT(&refs);
3039 error = got_ref_list(&refs, gw_trans->repo, "refs/tags",
3040 got_ref_cmp_tags, gw_trans->repo);
3041 if (error)
3042 goto done;
3044 TAILQ_FOREACH(re, &refs, entry) {
3045 const char *refname;
3046 const char *tagger;
3047 const char *tag_commit;
3048 time_t tagger_time;
3049 struct got_object_id *id;
3050 struct got_commit_object *commit = NULL;
3052 refname = got_ref_get_name(re->ref);
3053 if (strncmp(refname, "refs/tags/", 10) != 0)
3054 continue;
3055 refname += 10;
3057 error = got_ref_resolve(&id, gw_trans->repo, re->ref);
3058 if (error)
3059 goto done;
3061 error = got_object_open_as_tag(&tag, gw_trans->repo, id);
3062 if (error) {
3063 if (error->code != GOT_ERR_OBJ_TYPE) {
3064 free(id);
3065 goto done;
3067 /* "lightweight" tag */
3068 error = got_object_open_as_commit(&commit,
3069 gw_trans->repo, id);
3070 if (error) {
3071 free(id);
3072 goto done;
3074 tagger = got_object_commit_get_committer(commit);
3075 tagger_time =
3076 got_object_commit_get_committer_time(commit);
3077 error = got_object_id_str(&id_str, id);
3078 free(id);
3079 } else {
3080 free(id);
3081 tagger = got_object_tag_get_tagger(tag);
3082 tagger_time = got_object_tag_get_tagger_time(tag);
3083 error = got_object_id_str(&id_str,
3084 got_object_tag_get_object_id(tag));
3086 if (error)
3087 goto done;
3089 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
3090 strlen(id_str)) != 0)
3091 continue;
3093 if (tag_type == TAGBRIEF && gw_trans->commit_id &&
3094 start_tag == 0 && strncmp(id_str, header->commit_id,
3095 strlen(id_str)) != 0) {
3096 continue;
3097 } else {
3098 start_tag = 1;
3101 tag_count++;
3103 if (prev_set == 0 && start_tag == 1) {
3104 gw_trans->next_prev_id = strdup(id_str);
3105 if (gw_trans->next_prev_id == NULL) {
3106 error = got_error_from_errno("strdup");
3107 goto done;
3109 prev_set = 1;
3112 if (chk_next) {
3113 gw_trans->next_id = strdup(id_str);
3114 if (gw_trans->next_id == NULL)
3115 error = got_error_from_errno("strdup");
3116 goto done;
3119 if (commit) {
3120 error = got_object_commit_get_logmsg(&tag_commit0,
3121 commit);
3122 if (error)
3123 goto done;
3124 got_object_commit_close(commit);
3125 } else {
3126 tag_commit0 = strdup(got_object_tag_get_message(tag));
3127 if (tag_commit0 == NULL) {
3128 error = got_error_from_errno("strdup");
3129 goto done;
3133 tag_commit = tag_commit0;
3134 while (*tag_commit == '\n')
3135 tag_commit++;
3137 switch (tag_type) {
3138 case TAGBRIEF:
3139 newline = strchr(tag_commit, '\n');
3140 if (newline)
3141 *newline = '\0';
3143 if (summary_header_displayed == 0) {
3144 kerr = khtml_attr(gw_trans->gw_html_req,
3145 KELEM_DIV, KATTR_ID,
3146 "summary_tags_title_wrapper", KATTR__MAX);
3147 if (kerr != KCGI_OK)
3148 goto done;
3149 kerr = khtml_attr(gw_trans->gw_html_req,
3150 KELEM_DIV, KATTR_ID,
3151 "summary_tags_title", KATTR__MAX);
3152 if (kerr != KCGI_OK)
3153 goto done;
3154 kerr = khtml_puts(gw_trans->gw_html_req,
3155 "Tags");
3156 if (kerr != KCGI_OK)
3157 goto done;
3158 kerr = khtml_closeelem(gw_trans->gw_html_req,
3159 2);
3160 if (kerr != KCGI_OK)
3161 goto done;
3162 kerr = khtml_attr(gw_trans->gw_html_req,
3163 KELEM_DIV, KATTR_ID,
3164 "summary_tags_content", KATTR__MAX);
3165 if (kerr != KCGI_OK)
3166 goto done;
3167 summary_header_displayed = 1;
3170 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3171 KATTR_ID, "tag_wrapper", KATTR__MAX);
3172 if (kerr != KCGI_OK)
3173 goto done;
3174 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3175 KATTR_ID, "tag_age", KATTR__MAX);
3176 if (kerr != KCGI_OK)
3177 goto done;
3178 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
3179 if (error)
3180 goto done;
3181 kerr = khtml_puts(gw_trans->gw_html_req,
3182 age ? age : "");
3183 if (kerr != KCGI_OK)
3184 goto done;
3185 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3186 if (kerr != KCGI_OK)
3187 goto done;
3188 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3189 KATTR_ID, "tag", KATTR__MAX);
3190 if (kerr != KCGI_OK)
3191 goto done;
3192 kerr = khtml_puts(gw_trans->gw_html_req, refname);
3193 if (kerr != KCGI_OK)
3194 goto done;
3195 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3196 if (kerr != KCGI_OK)
3197 goto done;
3198 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3199 KATTR_ID, "tag_name", KATTR__MAX);
3200 if (kerr != KCGI_OK)
3201 goto done;
3203 href_tag = khttp_urlpart(NULL, NULL, "gotweb", "path",
3204 gw_trans->repo_name, "action", "tag", "commit",
3205 id_str, NULL);
3206 if (href_tag == NULL) {
3207 error = got_error_from_errno("khttp_urlpart");
3208 goto done;
3210 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3211 KATTR_HREF, href_tag, KATTR__MAX);
3212 if (kerr != KCGI_OK)
3213 goto done;
3214 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
3215 if (kerr != KCGI_OK)
3216 goto done;
3217 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3218 if (kerr != KCGI_OK)
3219 goto done;
3221 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3222 KATTR_ID, "navs_wrapper", KATTR__MAX);
3223 if (kerr != KCGI_OK)
3224 goto done;
3225 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3226 KATTR_ID, "navs", KATTR__MAX);
3227 if (kerr != KCGI_OK)
3228 goto done;
3230 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3231 KATTR_HREF, href_tag, KATTR__MAX);
3232 if (kerr != KCGI_OK)
3233 goto done;
3234 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
3235 if (kerr != KCGI_OK)
3236 goto done;
3237 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3238 if (kerr != KCGI_OK)
3239 goto done;
3241 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3242 if (kerr != KCGI_OK)
3243 goto done;
3245 href_briefs = khttp_urlpart(NULL, NULL, "gotweb",
3246 "path", gw_trans->repo_name, "action", "briefs",
3247 "commit", id_str, NULL);
3248 if (href_briefs == NULL) {
3249 error = got_error_from_errno("khttp_urlpart");
3250 goto done;
3252 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3253 KATTR_HREF, href_briefs, KATTR__MAX);
3254 if (kerr != KCGI_OK)
3255 goto done;
3256 kerr = khtml_puts(gw_trans->gw_html_req,
3257 "commit briefs");
3258 if (kerr != KCGI_OK)
3259 goto done;
3260 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3261 if (kerr != KCGI_OK)
3262 goto done;
3264 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3265 if (kerr != KCGI_OK)
3266 goto done;
3268 href_commits = khttp_urlpart(NULL, NULL, "gotweb",
3269 "path", gw_trans->repo_name, "action", "commits",
3270 "commit", id_str, NULL);
3271 if (href_commits == NULL) {
3272 error = got_error_from_errno("khttp_urlpart");
3273 goto done;
3275 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3276 KATTR_HREF, href_commits, KATTR__MAX);
3277 if (kerr != KCGI_OK)
3278 goto done;
3279 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
3280 if (kerr != KCGI_OK)
3281 goto done;
3282 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3283 if (kerr != KCGI_OK)
3284 goto done;
3286 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3287 KATTR_ID, "dotted_line", KATTR__MAX);
3288 if (kerr != KCGI_OK)
3289 goto done;
3290 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3291 if (kerr != KCGI_OK)
3292 goto done;
3293 break;
3294 case TAGFULL:
3295 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3296 KATTR_ID, "tag_info_date_title", KATTR__MAX);
3297 if (kerr != KCGI_OK)
3298 goto done;
3299 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
3300 if (kerr != KCGI_OK)
3301 goto done;
3302 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3303 if (kerr != KCGI_OK)
3304 goto done;
3305 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3306 KATTR_ID, "tag_info_date", KATTR__MAX);
3307 if (kerr != KCGI_OK)
3308 goto done;
3309 error = gw_get_time_str(&age, tagger_time, TM_LONG);
3310 if (error)
3311 goto done;
3312 kerr = khtml_puts(gw_trans->gw_html_req,
3313 age ? age : "");
3314 if (kerr != KCGI_OK)
3315 goto done;
3316 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3317 if (kerr != KCGI_OK)
3318 goto done;
3320 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3321 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
3322 if (kerr != KCGI_OK)
3323 goto done;
3324 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
3325 if (kerr != KCGI_OK)
3326 goto done;
3327 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3328 if (kerr != KCGI_OK)
3329 goto done;
3330 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3331 KATTR_ID, "tag_info_date", KATTR__MAX);
3332 if (kerr != KCGI_OK)
3333 goto done;
3334 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
3335 if (kerr != KCGI_OK)
3336 goto done;
3337 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3338 if (kerr != KCGI_OK)
3339 goto done;
3341 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3342 KATTR_ID, "tag_info", KATTR__MAX);
3343 if (kerr != KCGI_OK)
3344 goto done;
3345 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
3346 if (kerr != KCGI_OK)
3347 goto done;
3348 break;
3349 default:
3350 break;
3352 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3353 if (kerr != KCGI_OK)
3354 goto done;
3356 if (limit && --limit == 0)
3357 chk_next = 1;
3359 if (tag)
3360 got_object_tag_close(tag);
3361 tag = NULL;
3362 free(id_str);
3363 id_str = NULL;
3364 free(age);
3365 age = NULL;
3366 free(tag_commit0);
3367 tag_commit0 = NULL;
3368 free(href_tag);
3369 href_tag = NULL;
3370 free(href_briefs);
3371 href_briefs = NULL;
3372 free(href_commits);
3373 href_commits = NULL;
3375 if (tag_count == 0) {
3376 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3377 "summary_tags_title_wrapper", KATTR__MAX);
3378 if (kerr != KCGI_OK)
3379 goto done;
3380 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3381 "summary_tags_title", KATTR__MAX);
3382 if (kerr != KCGI_OK)
3383 goto done;
3384 kerr = khtml_puts(gw_trans->gw_html_req, "Tags");
3385 if (kerr != KCGI_OK)
3386 goto done;
3387 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3388 if (kerr != KCGI_OK)
3389 goto done;
3390 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3391 "summary_tags_content", KATTR__MAX);
3392 if (kerr != KCGI_OK)
3393 goto done;
3394 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3395 "tags_info", KATTR__MAX);
3396 if (kerr != KCGI_OK)
3397 goto done;
3398 kerr = khttp_puts(gw_trans->gw_req,
3399 "There are no tags for this repo.");
3400 if (kerr != KCGI_OK)
3401 goto done;
3402 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3404 done:
3405 if (tag)
3406 got_object_tag_close(tag);
3407 free(id_str);
3408 free(age);
3409 free(tag_commit0);
3410 free(href_tag);
3411 free(href_briefs);
3412 free(href_commits);
3413 got_ref_list_free(&refs);
3414 if (error == NULL && kerr != KCGI_OK)
3415 error = gw_kcgi_error(kerr);
3416 return error;
3419 static void
3420 gw_free_header(struct gw_header *header)
3422 free(header->path);
3423 free(header->author);
3424 free(header->committer);
3425 free(header->refs_str);
3426 free(header->commit_id);
3427 free(header->parent_id);
3428 free(header->tree_id);
3429 free(header->commit_msg);
3432 static struct gw_header *
3433 gw_init_header()
3435 struct gw_header *header;
3437 header = malloc(sizeof(*header));
3438 if (header == NULL)
3439 return NULL;
3441 header->path = NULL;
3442 TAILQ_INIT(&header->refs);
3444 header->refs_str = NULL;
3445 header->commit_id = NULL;
3446 header->committer = NULL;
3447 header->author = NULL;
3448 header->parent_id = NULL;
3449 header->tree_id = NULL;
3450 header->commit_msg = NULL;
3452 return header;
3455 static const struct got_error *
3456 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
3457 int limit, struct got_object_id *id)
3459 const struct got_error *error = NULL;
3460 struct got_commit_graph *graph = NULL;
3461 struct got_commit_object *commit = NULL;
3462 int chk_next = 0, chk_multi = 0, prev_set = 0;
3464 error = got_commit_graph_open(&graph, header->path, 0);
3465 if (error)
3466 return error;
3468 error = got_commit_graph_iter_start(graph, id, gw_trans->repo, NULL,
3469 NULL);
3470 if (error)
3471 goto done;
3473 for (;;) {
3474 error = got_commit_graph_iter_next(&id, graph, gw_trans->repo,
3475 NULL, NULL);
3476 if (error) {
3477 if (error->code == GOT_ERR_ITER_COMPLETED)
3478 error = NULL;
3479 goto done;
3481 if (id == NULL)
3482 goto done;
3484 error = got_object_open_as_commit(&commit, gw_trans->repo, id);
3485 if (error)
3486 goto done;
3487 if (limit == 1 && chk_multi == 0 &&
3488 gw_trans->gw_conf->got_max_commits_display != 1) {
3489 error = gw_get_commit(gw_trans, header, commit, id);
3490 if (error)
3491 goto done;
3492 } else {
3493 chk_multi = 1;
3494 struct gw_header *n_header = NULL;
3495 if ((n_header = gw_init_header()) == NULL) {
3496 error = got_error_from_errno("malloc");
3497 goto done;
3499 error = got_ref_list(&n_header->refs, gw_trans->repo,
3500 NULL, got_ref_cmp_by_name, NULL);
3501 if (error)
3502 goto done;
3504 error = gw_get_commit(gw_trans, n_header, commit, id);
3505 if (error)
3506 goto done;
3507 got_ref_list_free(&n_header->refs);
3510 * we have a commit_id now, so copy it to next_prev_id
3511 * for navigation through gw_briefs and gw_commits
3513 if (gw_trans->next_prev_id == NULL && prev_set == 0 &&
3514 (gw_trans->action == GW_BRIEFS ||
3515 gw_trans->action == GW_COMMITS ||
3516 gw_trans->action == GW_SUMMARY)) {
3517 prev_set = 1;
3518 gw_trans->next_prev_id =
3519 strdup(n_header->commit_id);
3520 if (gw_trans->next_prev_id == NULL) {
3521 error = got_error_from_errno("strdup");
3522 goto done;
3527 * check for one more commit before breaking,
3528 * so we know whether to navicate through gw_briefs
3529 * gw_commits and gw_summary
3531 if (chk_next && (gw_trans->action == GW_BRIEFS||
3532 gw_trans->action == GW_COMMITS ||
3533 gw_trans->action == GW_SUMMARY)) {
3534 gw_trans->next_id = strdup(n_header->commit_id);
3535 if (gw_trans->next_id == NULL)
3536 error = got_error_from_errno("strdup");
3537 goto done;
3540 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3541 entry);
3543 if (error || (limit && --limit == 0)) {
3544 if (chk_multi == 0)
3545 break;
3546 chk_next = 1;
3549 done:
3550 if (commit != NULL)
3551 got_object_commit_close(commit);
3552 if (graph)
3553 got_commit_graph_close(graph);
3554 return error;
3557 static const struct got_error *
3558 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header,
3559 struct got_commit_object *commit, struct got_object_id *id)
3561 const struct got_error *error = NULL;
3562 struct got_reflist_entry *re;
3563 struct got_object_id *id2 = NULL;
3564 struct got_object_qid *parent_id;
3565 char *commit_msg = NULL, *commit_msg0;
3567 /*print commit*/
3568 TAILQ_FOREACH(re, &header->refs, entry) {
3569 char *s;
3570 const char *name;
3571 struct got_tag_object *tag = NULL;
3572 struct got_object_id *ref_id;
3573 int cmp;
3575 if (got_ref_is_symbolic(re->ref))
3576 continue;
3578 name = got_ref_get_name(re->ref);
3579 if (strncmp(name, "refs/", 5) == 0)
3580 name += 5;
3581 if (strncmp(name, "got/", 4) == 0)
3582 continue;
3583 if (strncmp(name, "heads/", 6) == 0)
3584 name += 6;
3585 if (strncmp(name, "remotes/", 8) == 0) {
3586 name += 8;
3587 s = strstr(name, "/" GOT_REF_HEAD);
3588 if (s != NULL && s[strlen(s)] == '\0')
3589 continue;
3591 error = got_ref_resolve(&ref_id, gw_trans->repo, re->ref);
3592 if (error)
3593 return error;
3594 if (strncmp(name, "tags/", 5) == 0) {
3595 error = got_object_open_as_tag(&tag, gw_trans->repo,
3596 ref_id);
3597 if (error) {
3598 if (error->code != GOT_ERR_OBJ_TYPE) {
3599 free(ref_id);
3600 continue;
3603 * Ref points at something other
3604 * than a tag.
3606 error = NULL;
3607 tag = NULL;
3610 cmp = got_object_id_cmp(tag ?
3611 got_object_tag_get_object_id(tag) : ref_id, id);
3612 free(ref_id);
3613 if (tag)
3614 got_object_tag_close(tag);
3615 if (cmp != 0)
3616 continue;
3617 s = header->refs_str;
3618 if (asprintf(&header->refs_str, "%s%s%s", s ? s : "",
3619 s ? ", " : "", name) == -1) {
3620 error = got_error_from_errno("asprintf");
3621 free(s);
3622 header->refs_str = NULL;
3623 return error;
3625 free(s);
3628 error = got_object_id_str(&header->commit_id, id);
3629 if (error)
3630 return error;
3632 error = got_object_id_str(&header->tree_id,
3633 got_object_commit_get_tree_id(commit));
3634 if (error)
3635 return error;
3637 if (gw_trans->action == GW_DIFF) {
3638 parent_id = SIMPLEQ_FIRST(
3639 got_object_commit_get_parent_ids(commit));
3640 if (parent_id != NULL) {
3641 id2 = got_object_id_dup(parent_id->id);
3642 free (parent_id);
3643 error = got_object_id_str(&header->parent_id, id2);
3644 if (error)
3645 return error;
3646 free(id2);
3647 } else {
3648 header->parent_id = strdup("/dev/null");
3649 if (header->parent_id == NULL) {
3650 error = got_error_from_errno("strdup");
3651 return error;
3656 header->committer_time =
3657 got_object_commit_get_committer_time(commit);
3659 header->author =
3660 strdup(got_object_commit_get_author(commit));
3661 if (header->author == NULL) {
3662 error = got_error_from_errno("strdup");
3663 return error;
3665 header->committer =
3666 strdup(got_object_commit_get_committer(commit));
3667 if (header->committer == NULL) {
3668 error = got_error_from_errno("strdup");
3669 return error;
3671 error = got_object_commit_get_logmsg(&commit_msg0, commit);
3672 if (error)
3673 return error;
3675 commit_msg = commit_msg0;
3676 while (*commit_msg == '\n')
3677 commit_msg++;
3679 header->commit_msg = strdup(commit_msg);
3680 if (header->commit_msg == NULL)
3681 error = got_error_from_errno("strdup");
3682 free(commit_msg0);
3683 return error;
3686 static const struct got_error *
3687 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3689 const struct got_error *error = NULL;
3690 char *in_repo_path = NULL;
3691 struct got_object_id *id = NULL;
3693 error = got_repo_open(&gw_trans->repo, gw_trans->repo_path, NULL);
3694 if (error)
3695 return error;
3697 if (gw_trans->commit_id == NULL) {
3698 struct got_reference *head_ref;
3699 error = got_ref_open(&head_ref, gw_trans->repo,
3700 gw_trans->headref, 0);
3701 if (error)
3702 return error;
3704 error = got_ref_resolve(&id, gw_trans->repo, head_ref);
3705 got_ref_close(head_ref);
3706 if (error)
3707 return error;
3708 } else {
3709 struct got_reference *ref;
3711 error = got_ref_open(&ref, gw_trans->repo,
3712 gw_trans->commit_id, 0);
3713 if (error == NULL) {
3714 int obj_type;
3715 error = got_ref_resolve(&id, gw_trans->repo, ref);
3716 got_ref_close(ref);
3717 if (error)
3718 return error;
3719 error = got_object_get_type(&obj_type, gw_trans->repo,
3720 id);
3721 if (error)
3722 goto done;
3723 if (obj_type == GOT_OBJ_TYPE_TAG) {
3724 struct got_tag_object *tag;
3725 error = got_object_open_as_tag(&tag,
3726 gw_trans->repo, id);
3727 if (error)
3728 goto done;
3729 if (got_object_tag_get_object_type(tag) !=
3730 GOT_OBJ_TYPE_COMMIT) {
3731 got_object_tag_close(tag);
3732 error = got_error(GOT_ERR_OBJ_TYPE);
3733 goto done;
3735 free(id);
3736 id = got_object_id_dup(
3737 got_object_tag_get_object_id(tag));
3738 if (id == NULL)
3739 error = got_error_from_errno(
3740 "got_object_id_dup");
3741 got_object_tag_close(tag);
3742 if (error)
3743 goto done;
3744 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3745 error = got_error(GOT_ERR_OBJ_TYPE);
3746 goto done;
3749 error = got_repo_match_object_id_prefix(&id,
3750 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT,
3751 gw_trans->repo);
3752 if (error)
3753 goto done;
3756 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
3757 gw_trans->repo_path);
3758 if (error)
3759 goto done;
3761 if (in_repo_path) {
3762 header->path = strdup(in_repo_path);
3763 if (header->path == NULL) {
3764 error = got_error_from_errno("strdup");
3765 goto done;
3769 error = got_ref_list(&header->refs, gw_trans->repo, NULL,
3770 got_ref_cmp_by_name, NULL);
3771 if (error)
3772 goto done;
3774 error = gw_get_commits(gw_trans, header, limit, id);
3775 done:
3776 got_ref_list_free(&header->refs);
3777 free(id);
3778 free(in_repo_path);
3779 return error;
3782 struct blame_line {
3783 int annotated;
3784 char *id_str;
3785 char *committer;
3786 char datebuf[11]; /* YYYY-MM-DD + NUL */
3789 struct gw_blame_cb_args {
3790 struct blame_line *lines;
3791 int nlines;
3792 int nlines_prec;
3793 int lineno_cur;
3794 off_t *line_offsets;
3795 FILE *f;
3796 struct got_repository *repo;
3797 struct gw_trans *gw_trans;
3800 static const struct got_error *
3801 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3803 const struct got_error *err = NULL;
3804 struct gw_blame_cb_args *a = arg;
3805 struct blame_line *bline;
3806 char *line = NULL;
3807 size_t linesize = 0;
3808 struct got_commit_object *commit = NULL;
3809 off_t offset;
3810 struct tm tm;
3811 time_t committer_time;
3812 enum kcgi_err kerr = KCGI_OK;
3814 if (nlines != a->nlines ||
3815 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3816 return got_error(GOT_ERR_RANGE);
3818 if (lineno == -1)
3819 return NULL; /* no change in this commit */
3821 /* Annotate this line. */
3822 bline = &a->lines[lineno - 1];
3823 if (bline->annotated)
3824 return NULL;
3825 err = got_object_id_str(&bline->id_str, id);
3826 if (err)
3827 return err;
3829 err = got_object_open_as_commit(&commit, a->repo, id);
3830 if (err)
3831 goto done;
3833 bline->committer = strdup(got_object_commit_get_committer(commit));
3834 if (bline->committer == NULL) {
3835 err = got_error_from_errno("strdup");
3836 goto done;
3839 committer_time = got_object_commit_get_committer_time(commit);
3840 if (localtime_r(&committer_time, &tm) == NULL)
3841 return got_error_from_errno("localtime_r");
3842 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3843 &tm) == 0) {
3844 err = got_error(GOT_ERR_NO_SPACE);
3845 goto done;
3847 bline->annotated = 1;
3849 /* Print lines annotated so far. */
3850 bline = &a->lines[a->lineno_cur - 1];
3851 if (!bline->annotated)
3852 goto done;
3854 offset = a->line_offsets[a->lineno_cur - 1];
3855 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3856 err = got_error_from_errno("fseeko");
3857 goto done;
3860 while (bline->annotated) {
3861 char *smallerthan, *at, *nl, *committer;
3862 char *href_diff = NULL;
3863 size_t len;
3865 if (getline(&line, &linesize, a->f) == -1) {
3866 if (ferror(a->f))
3867 err = got_error_from_errno("getline");
3868 break;
3871 committer = bline->committer;
3872 smallerthan = strchr(committer, '<');
3873 if (smallerthan && smallerthan[1] != '\0')
3874 committer = smallerthan + 1;
3875 at = strchr(committer, '@');
3876 if (at)
3877 *at = '\0';
3878 len = strlen(committer);
3879 if (len >= 9)
3880 committer[8] = '\0';
3882 nl = strchr(line, '\n');
3883 if (nl)
3884 *nl = '\0';
3886 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3887 "blame_wrapper", KATTR__MAX);
3888 if (kerr != KCGI_OK)
3889 goto err;
3890 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3891 "blame_number", KATTR__MAX);
3892 if (kerr != KCGI_OK)
3893 goto err;
3894 kerr = khtml_printf(a->gw_trans->gw_html_req, "%.*d",
3895 a->nlines_prec, a->lineno_cur);
3896 if (kerr != KCGI_OK)
3897 goto err;
3898 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3899 if (kerr != KCGI_OK)
3900 goto err;
3902 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3903 "blame_hash", KATTR__MAX);
3904 if (kerr != KCGI_OK)
3905 goto err;
3907 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
3908 a->gw_trans->repo_name, "action", "diff", "commit",
3909 bline->id_str, NULL);
3910 if (href_diff == NULL) {
3911 err = got_error_from_errno("khttp_urlpart");
3912 goto done;
3914 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3915 KATTR_HREF, href_diff, KATTR__MAX);
3916 if (kerr != KCGI_OK)
3917 goto done;
3918 kerr = khtml_printf(a->gw_trans->gw_html_req, "%.8s",
3919 bline->id_str);
3920 if (kerr != KCGI_OK)
3921 goto err;
3922 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3923 if (kerr != KCGI_OK)
3924 goto err;
3926 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3927 "blame_date", KATTR__MAX);
3928 if (kerr != KCGI_OK)
3929 goto err;
3930 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3931 if (kerr != KCGI_OK)
3932 goto err;
3933 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3934 if (kerr != KCGI_OK)
3935 goto err;
3937 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3938 "blame_author", KATTR__MAX);
3939 if (kerr != KCGI_OK)
3940 goto err;
3941 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
3942 if (kerr != KCGI_OK)
3943 goto err;
3944 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3945 if (kerr != KCGI_OK)
3946 goto err;
3948 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3949 "blame_code", KATTR__MAX);
3950 if (kerr != KCGI_OK)
3951 goto err;
3952 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
3953 if (kerr != KCGI_OK)
3954 goto err;
3955 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3956 if (kerr != KCGI_OK)
3957 goto err;
3959 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3960 if (kerr != KCGI_OK)
3961 goto err;
3963 a->lineno_cur++;
3964 bline = &a->lines[a->lineno_cur - 1];
3965 err:
3966 free(href_diff);
3968 done:
3969 if (commit)
3970 got_object_commit_close(commit);
3971 free(line);
3972 if (err == NULL && kerr != KCGI_OK)
3973 err = gw_kcgi_error(kerr);
3974 return err;
3977 static const struct got_error *
3978 gw_output_file_blame(struct gw_trans *gw_trans, struct gw_header *header)
3980 const struct got_error *error = NULL;
3981 struct got_object_id *obj_id = NULL;
3982 struct got_object_id *commit_id = NULL;
3983 struct got_blob_object *blob = NULL;
3984 char *path = NULL, *in_repo_path = NULL;
3985 struct gw_blame_cb_args bca;
3986 int i, obj_type;
3987 off_t filesize;
3989 if (asprintf(&path, "%s%s%s",
3990 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3991 gw_trans->repo_folder ? "/" : "",
3992 gw_trans->repo_file) == -1) {
3993 error = got_error_from_errno("asprintf");
3994 goto done;
3997 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path);
3998 if (error)
3999 goto done;
4001 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
4002 GOT_OBJ_TYPE_COMMIT, &header->refs, gw_trans->repo);
4003 if (error)
4004 goto done;
4006 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
4007 in_repo_path);
4008 if (error)
4009 goto done;
4011 if (obj_id == NULL) {
4012 error = got_error(GOT_ERR_NO_OBJ);
4013 goto done;
4016 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
4017 if (error)
4018 goto done;
4020 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4021 error = got_error(GOT_ERR_OBJ_TYPE);
4022 goto done;
4025 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
4026 if (error)
4027 goto done;
4029 bca.f = got_opentemp();
4030 if (bca.f == NULL) {
4031 error = got_error_from_errno("got_opentemp");
4032 goto done;
4034 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4035 &bca.line_offsets, bca.f, blob);
4036 if (error || bca.nlines == 0)
4037 goto done;
4039 /* Don't include \n at EOF in the blame line count. */
4040 if (bca.line_offsets[bca.nlines - 1] == filesize)
4041 bca.nlines--;
4043 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4044 if (bca.lines == NULL) {
4045 error = got_error_from_errno("calloc");
4046 goto done;
4048 bca.lineno_cur = 1;
4049 bca.nlines_prec = 0;
4050 i = bca.nlines;
4051 while (i > 0) {
4052 i /= 10;
4053 bca.nlines_prec++;
4055 bca.repo = gw_trans->repo;
4056 bca.gw_trans = gw_trans;
4058 error = got_blame(in_repo_path, commit_id, gw_trans->repo, gw_blame_cb,
4059 &bca, NULL, NULL);
4060 done:
4061 free(in_repo_path);
4062 free(commit_id);
4063 free(obj_id);
4064 free(path);
4066 if (blob) {
4067 free(bca.line_offsets);
4068 for (i = 0; i < bca.nlines; i++) {
4069 struct blame_line *bline = &bca.lines[i];
4070 free(bline->id_str);
4071 free(bline->committer);
4073 free(bca.lines);
4074 if (bca.f && fclose(bca.f) == EOF && error == NULL)
4075 error = got_error_from_errno("fclose");
4077 if (blob)
4078 got_object_blob_close(blob);
4079 return error;
4082 static const struct got_error *
4083 gw_output_blob_buf(struct gw_trans *gw_trans, struct gw_header *header)
4085 const struct got_error *error = NULL;
4086 struct got_object_id *obj_id = NULL;
4087 struct got_object_id *commit_id = NULL;
4088 struct got_blob_object *blob = NULL;
4089 char *path = NULL, *in_repo_path = NULL;
4090 int obj_type, set_mime = 0;
4091 size_t len, hdrlen;
4092 const uint8_t *buf;
4093 enum kcgi_err kerr = KCGI_OK;
4095 if (asprintf(&path, "%s%s%s",
4096 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4097 gw_trans->repo_folder ? "/" : "",
4098 gw_trans->repo_file) == -1) {
4099 error = got_error_from_errno("asprintf");
4100 goto done;
4103 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path);
4104 if (error)
4105 goto done;
4107 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
4108 GOT_OBJ_TYPE_COMMIT, &header->refs, gw_trans->repo);
4109 if (error)
4110 goto done;
4112 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
4113 in_repo_path);
4114 if (error)
4115 goto done;
4117 if (obj_id == NULL) {
4118 error = got_error(GOT_ERR_NO_OBJ);
4119 goto done;
4122 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
4123 if (error)
4124 goto done;
4126 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4127 error = got_error(GOT_ERR_OBJ_TYPE);
4128 goto done;
4131 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
4132 if (error)
4133 goto done;
4135 hdrlen = got_object_blob_get_hdrlen(blob);
4136 do {
4137 error = got_object_blob_read_block(&len, blob);
4138 if (error)
4139 goto done;
4140 buf = got_object_blob_get_read_buf(blob);
4143 * Skip blob object header first time around,
4144 * which also contains a zero byte.
4146 buf += hdrlen;
4147 if (set_mime == 0) {
4148 if (isbinary(buf, len - hdrlen))
4149 gw_trans->mime = KMIME_APP_OCTET_STREAM;
4150 else
4151 gw_trans->mime = KMIME_TEXT_PLAIN;
4152 set_mime = 1;
4153 error = gw_display_index(gw_trans);
4154 if (error)
4155 goto done;
4157 kerr = khttp_write(gw_trans->gw_req, buf, len - hdrlen);
4158 if (kerr != KCGI_OK)
4159 goto done;
4160 hdrlen = 0;
4161 } while (len != 0);
4162 done:
4163 free(in_repo_path);
4164 free(commit_id);
4165 free(obj_id);
4166 free(path);
4167 if (blob)
4168 got_object_blob_close(blob);
4169 if (error == NULL && kerr != KCGI_OK)
4170 error = gw_kcgi_error(kerr);
4171 return error;
4174 static const struct got_error *
4175 gw_output_repo_tree(struct gw_trans *gw_trans, struct gw_header *header)
4177 const struct got_error *error = NULL;
4178 struct got_object_id *tree_id = NULL, *commit_id = NULL;
4179 struct got_tree_object *tree = NULL;
4180 char *path = NULL, *in_repo_path = NULL;
4181 char *id_str = NULL;
4182 char *build_folder = NULL;
4183 char *href_blob = NULL, *href_blame = NULL;
4184 const char *class = NULL;
4185 int nentries, i, class_flip = 0;
4186 enum kcgi_err kerr = KCGI_OK;
4188 if (gw_trans->repo_folder != NULL) {
4189 path = strdup(gw_trans->repo_folder);
4190 if (path == NULL) {
4191 error = got_error_from_errno("strdup");
4192 goto done;
4194 } else {
4195 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
4196 gw_trans->repo_path);
4197 if (error)
4198 goto done;
4199 free(path);
4200 path = in_repo_path;
4203 if (gw_trans->commit_id == NULL) {
4204 struct got_reference *head_ref;
4205 error = got_ref_open(&head_ref, gw_trans->repo,
4206 gw_trans->headref, 0);
4207 if (error)
4208 goto done;
4209 error = got_ref_resolve(&commit_id, gw_trans->repo, head_ref);
4210 if (error)
4211 goto done;
4212 got_ref_close(head_ref);
4214 * gw_trans->commit_id was not parsed from the querystring
4215 * we hit this code path from gw_index, where we don't know the
4216 * commit values for the tree link yet, so set
4217 * gw_trans->commit_id here to continue further into the tree
4219 error = got_object_id_str(&gw_trans->commit_id, commit_id);
4220 if (error)
4221 goto done;
4223 } else {
4224 error = got_repo_match_object_id(&commit_id, NULL,
4225 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT, &header->refs,
4226 gw_trans->repo);
4227 if (error)
4228 goto done;
4231 error = got_object_id_by_path(&tree_id, gw_trans->repo, commit_id,
4232 path);
4233 if (error)
4234 goto done;
4236 error = got_object_open_as_tree(&tree, gw_trans->repo, tree_id);
4237 if (error)
4238 goto done;
4240 nentries = got_object_tree_get_nentries(tree);
4241 for (i = 0; i < nentries; i++) {
4242 struct got_tree_entry *te;
4243 const char *modestr = "";
4244 mode_t mode;
4246 te = got_object_tree_get_entry(tree, i);
4248 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
4249 if (error)
4250 goto done;
4252 mode = got_tree_entry_get_mode(te);
4253 if (got_object_tree_entry_is_submodule(te))
4254 modestr = "$";
4255 else if (S_ISLNK(mode))
4256 modestr = "@";
4257 else if (S_ISDIR(mode))
4258 modestr = "/";
4259 else if (mode & S_IXUSR)
4260 modestr = "*";
4262 if (class_flip == 0) {
4263 class = "back_lightgray";
4264 class_flip = 1;
4265 } else {
4266 class = "back_white";
4267 class_flip = 0;
4270 if (S_ISDIR(mode)) {
4271 if (asprintf(&build_folder, "%s/%s",
4272 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4273 got_tree_entry_get_name(te)) == -1) {
4274 error = got_error_from_errno("asprintf");
4275 goto done;
4278 href_blob = khttp_urlpart(NULL, NULL, "gotweb", "path",
4279 gw_trans->repo_name, "action",
4280 gw_get_action_name(gw_trans), "commit",
4281 gw_trans->commit_id, "folder", build_folder, NULL);
4282 if (href_blob == NULL) {
4283 error = got_error_from_errno("khttp_urlpart");
4284 goto done;
4286 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4287 KATTR_ID, "tree_wrapper", KATTR__MAX);
4288 if (kerr != KCGI_OK)
4289 goto done;
4290 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4291 KATTR_ID, "tree_line", KATTR_CLASS, class,
4292 KATTR__MAX);
4293 if (kerr != KCGI_OK)
4294 goto done;
4295 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4296 KATTR_HREF, href_blob, KATTR_CLASS,
4297 "diff_directory", KATTR__MAX);
4298 if (kerr != KCGI_OK)
4299 goto done;
4300 kerr = khtml_printf(gw_trans->gw_html_req, "%s%s",
4301 got_tree_entry_get_name(te), modestr);
4302 if (kerr != KCGI_OK)
4303 goto done;
4304 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4305 if (kerr != KCGI_OK)
4306 goto done;
4307 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4308 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
4309 KATTR__MAX);
4310 if (kerr != KCGI_OK)
4311 goto done;
4312 kerr = khtml_entity(gw_trans->gw_html_req,
4313 KENTITY_nbsp);
4314 if (kerr != KCGI_OK)
4315 goto done;
4316 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4317 if (kerr != KCGI_OK)
4318 goto done;
4319 } else {
4320 href_blob = khttp_urlpart(NULL, NULL, "gotweb", "path",
4321 gw_trans->repo_name, "action", "blob", "commit",
4322 gw_trans->commit_id, "file",
4323 got_tree_entry_get_name(te), "folder",
4324 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4325 NULL);
4326 if (href_blob == NULL) {
4327 error = got_error_from_errno("khttp_urlpart");
4328 goto done;
4330 href_blame = khttp_urlpart(NULL, NULL, "gotweb", "path",
4331 gw_trans->repo_name, "action", "blame", "commit",
4332 gw_trans->commit_id, "file",
4333 got_tree_entry_get_name(te), "folder",
4334 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4335 NULL);
4336 if (href_blame == NULL) {
4337 error = got_error_from_errno("khttp_urlpart");
4338 goto done;
4340 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4341 KATTR_ID, "tree_wrapper", KATTR__MAX);
4342 if (kerr != KCGI_OK)
4343 goto done;
4344 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4345 KATTR_ID, "tree_line", KATTR_CLASS, class,
4346 KATTR__MAX);
4347 if (kerr != KCGI_OK)
4348 goto done;
4349 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4350 KATTR_HREF, href_blob, KATTR__MAX);
4351 if (kerr != KCGI_OK)
4352 goto done;
4353 kerr = khtml_printf(gw_trans->gw_html_req, "%s%s",
4354 got_tree_entry_get_name(te), modestr);
4355 if (kerr != KCGI_OK)
4356 goto done;
4357 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4358 if (kerr != KCGI_OK)
4359 goto done;
4360 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4361 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
4362 KATTR__MAX);
4363 if (kerr != KCGI_OK)
4364 goto done;
4366 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4367 KATTR_HREF, href_blob, KATTR__MAX);
4368 if (kerr != KCGI_OK)
4369 goto done;
4370 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
4371 if (kerr != KCGI_OK)
4372 goto done;
4373 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4374 if (kerr != KCGI_OK)
4375 goto done;
4377 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4378 if (kerr != KCGI_OK)
4379 goto done;
4381 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4382 KATTR_HREF, href_blame, KATTR__MAX);
4383 if (kerr != KCGI_OK)
4384 goto done;
4385 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
4386 if (kerr != KCGI_OK)
4387 goto done;
4389 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4390 if (kerr != KCGI_OK)
4391 goto done;
4393 free(id_str);
4394 id_str = NULL;
4395 free(href_blob);
4396 href_blob = NULL;
4397 free(build_folder);
4398 build_folder = NULL;
4400 done:
4401 if (tree)
4402 got_object_tree_close(tree);
4403 free(id_str);
4404 free(href_blob);
4405 free(href_blame);
4406 free(in_repo_path);
4407 free(tree_id);
4408 free(build_folder);
4409 if (error == NULL && kerr != KCGI_OK)
4410 error = gw_kcgi_error(kerr);
4411 return error;
4414 static const struct got_error *
4415 gw_output_repo_heads(struct gw_trans *gw_trans)
4417 const struct got_error *error = NULL;
4418 struct got_reflist_head refs;
4419 struct got_reflist_entry *re;
4420 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
4421 char *href_commits = NULL;
4422 enum kcgi_err kerr = KCGI_OK;
4424 TAILQ_INIT(&refs);
4426 error = got_ref_list(&refs, gw_trans->repo, "refs/heads",
4427 got_ref_cmp_by_name, NULL);
4428 if (error)
4429 goto done;
4431 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4432 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
4433 if (kerr != KCGI_OK)
4434 goto done;
4435 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4436 KATTR_ID, "summary_heads_title", KATTR__MAX);
4437 if (kerr != KCGI_OK)
4438 goto done;
4439 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
4440 if (kerr != KCGI_OK)
4441 goto done;
4442 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4443 if (kerr != KCGI_OK)
4444 goto done;
4445 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4446 KATTR_ID, "summary_heads_content", KATTR__MAX);
4447 if (kerr != KCGI_OK)
4448 goto done;
4450 TAILQ_FOREACH(re, &refs, entry) {
4451 const char *refname;
4453 if (got_ref_is_symbolic(re->ref))
4454 continue;
4456 refname = got_ref_get_name(re->ref);
4457 if (strncmp(refname, "refs/heads/", 11) != 0)
4458 continue;
4460 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
4461 refname, TM_DIFF);
4462 if (error)
4463 goto done;
4465 if (strncmp(refname, "refs/heads/", 11) == 0)
4466 refname += 11;
4468 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4469 KATTR_ID, "heads_wrapper", KATTR__MAX);
4470 if (kerr != KCGI_OK)
4471 goto done;
4472 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4473 KATTR_ID, "heads_age", KATTR__MAX);
4474 if (kerr != KCGI_OK)
4475 goto done;
4476 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
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;
4482 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4483 KATTR_ID, "heads_space", KATTR__MAX);
4484 if (kerr != KCGI_OK)
4485 goto done;
4486 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
4487 if (kerr != KCGI_OK)
4488 goto done;
4489 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4490 if (kerr != KCGI_OK)
4491 goto done;
4492 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4493 KATTR_ID, "head", KATTR__MAX);
4494 if (kerr != KCGI_OK)
4495 goto done;
4497 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
4498 gw_trans->repo_name, "action", "summary", "headref",
4499 refname, NULL);
4500 if (href_summary == NULL) {
4501 error = got_error_from_errno("khttp_urlpart");
4502 goto done;
4504 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4505 href_summary, KATTR__MAX);
4506 kerr = khtml_puts(gw_trans->gw_html_req, refname);
4507 if (kerr != KCGI_OK)
4508 goto done;
4509 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4510 if (kerr != KCGI_OK)
4511 goto done;
4513 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4514 "navs_wrapper", KATTR__MAX);
4515 if (kerr != KCGI_OK)
4516 goto done;
4517 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4518 "navs", KATTR__MAX);
4519 if (kerr != KCGI_OK)
4520 goto done;
4522 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4523 href_summary, KATTR__MAX);
4524 if (kerr != KCGI_OK)
4525 goto done;
4526 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4527 if (kerr != KCGI_OK)
4528 goto done;
4529 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4530 if (kerr != KCGI_OK)
4531 goto done;
4533 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4534 if (kerr != KCGI_OK)
4535 goto done;
4537 href_briefs = khttp_urlpart(NULL, NULL, "gotweb", "path",
4538 gw_trans->repo_name, "action", "briefs", "headref",
4539 refname, NULL);
4540 if (href_briefs == NULL) {
4541 error = got_error_from_errno("khttp_urlpart");
4542 goto done;
4544 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4545 href_briefs, KATTR__MAX);
4546 if (kerr != KCGI_OK)
4547 goto done;
4548 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4549 if (kerr != KCGI_OK)
4550 goto done;
4551 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4552 if (kerr != KCGI_OK)
4553 goto done;
4555 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4556 if (kerr != KCGI_OK)
4557 goto done;
4559 href_commits = khttp_urlpart(NULL, NULL, "gotweb", "path",
4560 gw_trans->repo_name, "action", "commits", "headref",
4561 refname, NULL);
4562 if (href_commits == NULL) {
4563 error = got_error_from_errno("khttp_urlpart");
4564 goto done;
4566 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4567 href_commits, KATTR__MAX);
4568 if (kerr != KCGI_OK)
4569 goto done;
4570 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4571 if (kerr != KCGI_OK)
4572 goto done;
4573 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4574 if (kerr != KCGI_OK)
4575 goto done;
4577 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4578 "dotted_line", KATTR__MAX);
4579 if (kerr != KCGI_OK)
4580 goto done;
4581 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4582 if (kerr != KCGI_OK)
4583 goto done;
4584 free(href_summary);
4585 href_summary = NULL;
4586 free(href_briefs);
4587 href_briefs = NULL;
4588 free(href_commits);
4589 href_commits = NULL;
4591 done:
4592 got_ref_list_free(&refs);
4593 free(href_summary);
4594 free(href_briefs);
4595 free(href_commits);
4596 return error;
4599 static const struct got_error *
4600 gw_output_site_link(struct gw_trans *gw_trans)
4602 const struct got_error *error = NULL;
4603 char *href_summary = NULL;
4604 enum kcgi_err kerr = KCGI_OK;
4606 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4607 "site_link", KATTR__MAX);
4608 if (kerr != KCGI_OK)
4609 goto done;
4610 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4611 KATTR__MAX);
4612 if (kerr != KCGI_OK)
4613 goto done;
4614 kerr = khtml_puts(gw_trans->gw_html_req,
4615 gw_trans->gw_conf->got_site_link);
4616 if (kerr != KCGI_OK)
4617 goto done;
4618 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4619 if (kerr != KCGI_OK)
4620 goto done;
4622 if (gw_trans->repo_name != NULL) {
4623 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4624 if (kerr != KCGI_OK)
4625 goto done;
4627 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
4628 gw_trans->repo_name, "action", "summary", NULL);
4629 if (href_summary == NULL) {
4630 error = got_error_from_errno("khttp_urlpart");
4631 goto done;
4633 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4634 href_summary, KATTR__MAX);
4635 if (kerr != KCGI_OK)
4636 goto done;
4637 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4638 if (kerr != KCGI_OK)
4639 goto done;
4640 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4641 if (kerr != KCGI_OK)
4642 goto done;
4643 kerr = khtml_printf(gw_trans->gw_html_req, " / %s",
4644 gw_get_action_name(gw_trans));
4645 if (kerr != KCGI_OK)
4646 goto done;
4649 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4650 if (kerr != KCGI_OK)
4651 goto done;
4652 done:
4653 free(href_summary);
4654 if (error == NULL && kerr != KCGI_OK)
4655 error = gw_kcgi_error(kerr);
4656 return error;
4659 static const struct got_error *
4660 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4662 const struct got_error *error = NULL;
4663 char *color = NULL;
4664 enum kcgi_err kerr = KCGI_OK;
4666 if (strncmp(buf, "-", 1) == 0)
4667 color = "diff_minus";
4668 else if (strncmp(buf, "+", 1) == 0)
4669 color = "diff_plus";
4670 else if (strncmp(buf, "@@", 2) == 0)
4671 color = "diff_chunk_header";
4672 else if (strncmp(buf, "@@", 2) == 0)
4673 color = "diff_chunk_header";
4674 else if (strncmp(buf, "commit +", 8) == 0)
4675 color = "diff_meta";
4676 else if (strncmp(buf, "commit -", 8) == 0)
4677 color = "diff_meta";
4678 else if (strncmp(buf, "blob +", 6) == 0)
4679 color = "diff_meta";
4680 else if (strncmp(buf, "blob -", 6) == 0)
4681 color = "diff_meta";
4682 else if (strncmp(buf, "file +", 6) == 0)
4683 color = "diff_meta";
4684 else if (strncmp(buf, "file -", 6) == 0)
4685 color = "diff_meta";
4686 else if (strncmp(buf, "from:", 5) == 0)
4687 color = "diff_author";
4688 else if (strncmp(buf, "via:", 4) == 0)
4689 color = "diff_author";
4690 else if (strncmp(buf, "date:", 5) == 0)
4691 color = "diff_date";
4692 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4693 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4694 if (error == NULL && kerr != KCGI_OK)
4695 error = gw_kcgi_error(kerr);
4696 return error;
4699 int
4700 main(int argc, char *argv[])
4702 const struct got_error *error = NULL, *error2 = NULL;
4703 struct gw_trans *gw_trans;
4704 struct gw_dir *dir = NULL, *tdir;
4705 const char *page = "index";
4706 enum kcgi_err kerr = KCGI_OK;
4708 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4709 errx(1, "malloc");
4711 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4712 errx(1, "malloc");
4714 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4715 errx(1, "malloc");
4717 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4718 errx(1, "malloc");
4720 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4721 if (kerr != KCGI_OK) {
4722 error = gw_kcgi_error(kerr);
4723 goto done;
4726 TAILQ_INIT(&gw_trans->gw_dirs);
4727 TAILQ_INIT(&gw_trans->gw_headers);
4729 gw_trans->action = -1;
4730 gw_trans->page = 0;
4731 gw_trans->repos_total = 0;
4732 gw_trans->repo_path = NULL;
4733 gw_trans->commit_id = NULL;
4734 gw_trans->next_id = NULL;
4735 gw_trans->next_prev_id = NULL;
4736 gw_trans->prev_id = NULL;
4737 gw_trans->prev_prev_id = NULL;
4738 gw_trans->headref = GOT_REF_HEAD;
4739 gw_trans->mime = KMIME_TEXT_HTML;
4740 gw_trans->gw_tmpl->key = gw_templs;
4741 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4742 gw_trans->gw_tmpl->arg = gw_trans;
4743 gw_trans->gw_tmpl->cb = gw_template;
4745 error = parse_gotweb_config(&gw_trans->gw_conf, GOTWEB_CONF);
4746 if (error)
4747 goto done;
4749 error = gw_parse_querystring(gw_trans);
4750 if (error)
4751 goto done;
4753 if (gw_trans->action == GW_BLOB)
4754 error = gw_blob(gw_trans);
4755 else
4756 error = gw_display_index(gw_trans);
4757 done:
4758 if (gw_trans->repo) {
4759 const struct got_error *close_err;
4760 close_err = got_repo_close(gw_trans->repo);
4761 if (error == NULL)
4762 error = close_err;
4764 if (error) {
4765 gw_trans->error = error;
4766 gw_trans->action = GW_ERR;
4767 error2 = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
4768 if (error2)
4769 goto cleanup; /* we can't display an error page */
4770 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
4771 if (kerr != KCGI_OK)
4772 goto cleanup; /* we can't display an error page */
4773 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
4774 gw_query_funcs[gw_trans->action].template);
4775 if (kerr != KCGI_OK) {
4776 khtml_close(gw_trans->gw_html_req);
4777 goto cleanup; /* we can't display an error page */
4781 cleanup:
4782 free(gw_trans->gw_conf->got_repos_path);
4783 free(gw_trans->gw_conf->got_www_path);
4784 free(gw_trans->gw_conf->got_site_name);
4785 free(gw_trans->gw_conf->got_site_owner);
4786 free(gw_trans->gw_conf->got_site_link);
4787 free(gw_trans->gw_conf->got_logo);
4788 free(gw_trans->gw_conf->got_logo_url);
4789 free(gw_trans->gw_conf);
4790 free(gw_trans->commit_id);
4791 free(gw_trans->next_id);
4792 free(gw_trans->next_prev_id);
4793 free(gw_trans->prev_id);
4794 free(gw_trans->prev_prev_id);
4795 free(gw_trans->repo_path);
4796 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4797 free(dir->name);
4798 free(dir->description);
4799 free(dir->age);
4800 free(dir->url);
4801 free(dir->path);
4802 free(dir);
4805 khttp_free(gw_trans->gw_req);
4806 return 0;