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 <sha1.h>
28 #include <stdarg.h>
29 #include <stdint.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
35 #include <got_error.h>
36 #include <got_object.h>
37 #include <got_reference.h>
38 #include <got_repository.h>
39 #include <got_path.h>
40 #include <got_cancel.h>
41 #include <got_worktree.h>
42 #include <got_diff.h>
43 #include <got_commit_graph.h>
44 #include <got_blame.h>
45 #include <got_privsep.h>
46 #include <got_opentemp.h>
48 #include <kcgi.h>
49 #include <kcgihtml.h>
51 #include "gotweb.h"
53 #ifndef nitems
54 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
55 #endif
57 struct gw_trans {
58 TAILQ_HEAD(headers, gw_header) gw_headers;
59 TAILQ_HEAD(dirs, gw_dir) gw_dirs;
60 struct got_repository *repo;
61 struct gw_dir *gw_dir;
62 struct gotweb_config *gw_conf;
63 struct ktemplate *gw_tmpl;
64 struct khtmlreq *gw_html_req;
65 struct kreq *gw_req;
66 const struct got_error *error;
67 const char *repo_name;
68 char *repo_path;
69 char *commit_id;
70 char *next_id;
71 char *prev_id;
72 const char *repo_file;
73 char *repo_folder;
74 const char *headref;
75 unsigned int action;
76 unsigned int page;
77 unsigned int repos_total;
78 enum kmime mime;
79 int *pack_fds;
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__ZMAX
117 };
119 enum gw_tmpl {
120 TEMPL_CONTENT,
121 TEMPL_HEAD,
122 TEMPL_HEADER,
123 TEMPL_SEARCH,
124 TEMPL_SITEPATH,
125 TEMPL_SITEOWNER,
126 TEMPL_TITLE,
127 TEMPL__MAX
128 };
130 enum gw_ref_tm {
131 TM_DIFF,
132 TM_LONG,
133 };
135 enum gw_tags_type {
136 TAGBRIEF,
137 TAGFULL,
138 };
140 static const char *const gw_templs[TEMPL__MAX] = {
141 "content",
142 "head",
143 "header",
144 "search",
145 "sitepath",
146 "siteowner",
147 "title",
148 };
150 static const struct kvalid gw_keys[KEY__ZMAX] = {
151 { kvalid_stringne, "action" },
152 { kvalid_stringne, "commit" },
153 { kvalid_stringne, "file" },
154 { kvalid_stringne, "folder" },
155 { kvalid_stringne, "headref" },
156 { kvalid_int, "page" },
157 { kvalid_stringne, "path" },
158 { kvalid_stringne, "prev" },
159 };
161 static struct gw_header *gw_init_header(void);
163 static void gw_free_header(struct gw_header *);
165 static int gw_template(size_t, void *);
167 static const struct got_error *gw_error(struct gw_trans *);
168 static const struct got_error *gw_init_gw_dir(struct gw_dir **, const char *);
169 static const struct got_error *gw_get_repo_description(char **,
170 struct gw_trans *, char *);
171 static const struct got_error *gw_get_repo_owner(char **, struct gw_trans *,
172 char *);
173 static const struct got_error *gw_get_time_str(char **, time_t, int);
174 static const struct got_error *gw_get_repo_age(char **, struct gw_trans *,
175 char *, const char *, int);
176 static const struct got_error *gw_output_file_blame(struct gw_trans *,
177 struct gw_header *);
178 static const struct got_error *gw_output_blob_buf(struct gw_trans *,
179 struct gw_header *);
180 static const struct got_error *gw_output_repo_tree(struct gw_trans *,
181 struct gw_header *);
182 static const struct got_error *gw_output_diff(struct gw_trans *,
183 struct gw_header *);
184 static const struct got_error *gw_output_repo_tags(struct gw_trans *,
185 struct gw_header *, int, int);
186 static const struct got_error *gw_output_repo_heads(struct gw_trans *);
187 static const struct got_error *gw_output_site_link(struct gw_trans *);
188 static const struct got_error *gw_get_clone_url(char **, struct gw_trans *,
189 char *);
190 static const struct got_error *gw_colordiff_line(struct gw_trans *, char *);
192 static const struct got_error *gw_gen_commit_header(struct gw_trans *, char *,
193 char*);
194 static const struct got_error *gw_gen_diff_header(struct gw_trans *, char *,
195 char*);
196 static const struct got_error *gw_gen_author_header(struct gw_trans *,
197 const char *);
198 static const struct got_error *gw_gen_age_header(struct gw_trans *,
199 const char *);
200 static const struct got_error *gw_gen_committer_header(struct gw_trans *,
201 const char *);
202 static const struct got_error *gw_gen_commit_msg_header(struct gw_trans*,
203 char *);
204 static const struct got_error *gw_gen_tree_header(struct gw_trans *, char *);
205 static const struct got_error *gw_display_open(struct gw_trans *, enum khttp,
206 enum kmime);
207 static const struct got_error *gw_display_index(struct gw_trans *);
208 static const struct got_error *gw_get_header(struct gw_trans *,
209 struct gw_header *, int);
210 static const struct got_error *gw_get_commits(struct gw_trans *,
211 struct gw_header *, int,
212 struct got_object_id *);
213 static const struct got_error *gw_get_commit(struct gw_trans *,
214 struct gw_header *,
215 struct got_commit_object *,
216 struct got_object_id *);
217 static const struct got_error *gw_apply_unveil(const char *);
218 static const struct got_error *gw_blame_cb(void *, int, int,
219 struct got_commit_object *,
220 struct got_object_id *);
221 static const struct got_error *gw_load_got_paths(struct gw_trans *);
222 static const struct got_error *gw_load_got_path(struct gw_trans *,
223 struct gw_dir *);
224 static const struct got_error *gw_parse_querystring(struct gw_trans *);
225 static const struct got_error *gw_blame(struct gw_trans *);
226 static const struct got_error *gw_blob(struct gw_trans *);
227 static const struct got_error *gw_diff(struct gw_trans *);
228 static const struct got_error *gw_index(struct gw_trans *);
229 static const struct got_error *gw_commits(struct gw_trans *);
230 static const struct got_error *gw_briefs(struct gw_trans *);
231 static const struct got_error *gw_summary(struct gw_trans *);
232 static const struct got_error *gw_tree(struct gw_trans *);
233 static const struct got_error *gw_tag(struct gw_trans *);
234 static const struct got_error *gw_tags(struct gw_trans *);
236 struct gw_query_action {
237 unsigned int func_id;
238 const char *func_name;
239 const struct got_error *(*func_main)(struct gw_trans *);
240 const char *template;
241 };
243 enum gw_query_actions {
244 GW_BLAME,
245 GW_BLOB,
246 GW_BRIEFS,
247 GW_COMMITS,
248 GW_DIFF,
249 GW_ERR,
250 GW_INDEX,
251 GW_SUMMARY,
252 GW_TAG,
253 GW_TAGS,
254 GW_TREE,
255 };
257 static const struct gw_query_action gw_query_funcs[] = {
258 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
259 { GW_BLOB, "blob", NULL, NULL },
260 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
261 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
262 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
263 { GW_ERR, "error", gw_error, "gw_tmpl/err.tmpl" },
264 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
265 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
266 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
267 { GW_TAGS, "tags", gw_tags, "gw_tmpl/tags.tmpl" },
268 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
269 };
271 static const char *
272 gw_get_action_name(struct gw_trans *gw_trans)
274 return gw_query_funcs[gw_trans->action].func_name;
277 static const struct got_error *
278 gw_kcgi_error(enum kcgi_err kerr)
280 if (kerr == KCGI_OK)
281 return NULL;
283 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
284 return got_error(GOT_ERR_CANCELLED);
286 if (kerr == KCGI_ENOMEM)
287 return got_error_set_errno(ENOMEM,
288 kcgi_strerror(kerr));
290 if (kerr == KCGI_ENFILE)
291 return got_error_set_errno(ENFILE,
292 kcgi_strerror(kerr));
294 if (kerr == KCGI_EAGAIN)
295 return got_error_set_errno(EAGAIN,
296 kcgi_strerror(kerr));
298 if (kerr == KCGI_FORM)
299 return got_error_msg(GOT_ERR_IO,
300 kcgi_strerror(kerr));
302 return got_error_from_errno(kcgi_strerror(kerr));
305 static const struct got_error *
306 gw_apply_unveil(const char *repo_path)
308 const struct got_error *err;
310 #ifdef PROFILE
311 if (unveil("gmon.out", "rwc") != 0)
312 return got_error_from_errno2("unveil", "gmon.out");
313 #endif
314 if (repo_path && unveil(repo_path, "r") != 0)
315 return got_error_from_errno2("unveil", repo_path);
317 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
318 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
320 err = got_privsep_unveil_exec_helpers();
321 if (err != NULL)
322 return err;
324 if (unveil(NULL, NULL) != 0)
325 return got_error_from_errno("unveil");
327 return NULL;
330 static int
331 isbinary(const uint8_t *buf, size_t n)
333 size_t i;
335 for (i = 0; i < n; i++)
336 if (buf[i] == 0)
337 return 1;
338 return 0;
341 static const struct got_error *
342 gw_blame(struct gw_trans *gw_trans)
344 const struct got_error *error = NULL;
345 struct gw_header *header = NULL;
346 char *age = NULL;
347 enum kcgi_err kerr = KCGI_OK;
349 #ifndef PROFILE
350 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
351 NULL) == -1)
352 return got_error_from_errno("pledge");
353 #endif
354 if ((header = gw_init_header()) == NULL)
355 return got_error_from_errno("malloc");
357 error = gw_apply_unveil(gw_trans->gw_dir->path);
358 if (error)
359 goto done;
361 /* check querystring */
362 if (gw_trans->repo_file == NULL) {
363 error = got_error_msg(GOT_ERR_QUERYSTRING,
364 "file required in querystring");
365 goto done;
367 if (gw_trans->commit_id == NULL) {
368 error = got_error_msg(GOT_ERR_QUERYSTRING,
369 "commit required in querystring");
370 goto done;
373 error = gw_get_header(gw_trans, header, 1);
374 if (error)
375 goto done;
376 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
377 "blame_header_wrapper", KATTR__MAX);
378 if (kerr != KCGI_OK)
379 goto done;
380 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
381 "blame_header", KATTR__MAX);
382 if (kerr != KCGI_OK)
383 goto done;
384 error = gw_get_time_str(&age, header->committer_time,
385 TM_LONG);
386 if (error)
387 goto done;
388 error = gw_gen_age_header(gw_trans, age ?age : "");
389 if (error)
390 goto done;
391 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
392 if (error)
393 goto done;
394 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
395 if (kerr != KCGI_OK)
396 goto done;
397 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
398 "dotted_line", KATTR__MAX);
399 if (kerr != KCGI_OK)
400 goto done;
401 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
402 if (kerr != KCGI_OK)
403 goto done;
405 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
406 "blame", KATTR__MAX);
407 if (kerr != KCGI_OK)
408 goto done;
409 error = gw_output_file_blame(gw_trans, header);
410 if (error)
411 goto done;
412 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
413 done:
414 gw_free_header(header);
415 if (error == NULL && kerr != KCGI_OK)
416 error = gw_kcgi_error(kerr);
417 return error;
420 static const struct got_error *
421 gw_blob(struct gw_trans *gw_trans)
423 const struct got_error *error = NULL, *err = NULL;
424 struct gw_header *header = NULL;
425 enum kcgi_err kerr = KCGI_OK;
427 #ifndef PROFILE
428 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
429 NULL) == -1)
430 return got_error_from_errno("pledge");
431 #endif
432 if ((header = gw_init_header()) == NULL)
433 return got_error_from_errno("malloc");
435 error = gw_apply_unveil(gw_trans->gw_dir->path);
436 if (error)
437 goto done;
439 /* check querystring */
440 if (gw_trans->repo_file == NULL) {
441 error = got_error_msg(GOT_ERR_QUERYSTRING,
442 "file required in querystring");
443 goto done;
445 if (gw_trans->commit_id == NULL) {
446 error = got_error_msg(GOT_ERR_QUERYSTRING,
447 "commit required in querystring");
448 goto done;
450 error = gw_get_header(gw_trans, header, 1);
451 if (error)
452 goto done;
454 error = gw_output_blob_buf(gw_trans, header);
455 done:
456 if (error) {
457 gw_trans->mime = KMIME_TEXT_PLAIN;
458 err = gw_display_index(gw_trans);
459 if (err) {
460 error = err;
461 goto errored;
463 kerr = khttp_puts(gw_trans->gw_req, error->msg);
465 errored:
466 gw_free_header(header);
467 if (error == NULL && kerr != KCGI_OK)
468 error = gw_kcgi_error(kerr);
469 return error;
472 static const struct got_error *
473 gw_diff(struct gw_trans *gw_trans)
475 const struct got_error *error = NULL;
476 struct gw_header *header = NULL;
477 char *age = NULL;
478 enum kcgi_err kerr = KCGI_OK;
480 #ifndef PROFILE
481 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
482 NULL) == -1)
483 return got_error_from_errno("pledge");
484 #endif
485 if ((header = gw_init_header()) == NULL)
486 return got_error_from_errno("malloc");
488 error = gw_apply_unveil(gw_trans->gw_dir->path);
489 if (error)
490 goto done;
492 error = gw_get_header(gw_trans, header, 1);
493 if (error)
494 goto done;
496 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
497 "diff_header_wrapper", KATTR__MAX);
498 if (kerr != KCGI_OK)
499 goto done;
500 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
501 "diff_header", KATTR__MAX);
502 if (kerr != KCGI_OK)
503 goto done;
504 error = gw_gen_diff_header(gw_trans, header->parent_id,
505 header->commit_id);
506 if (error)
507 goto done;
508 error = gw_gen_commit_header(gw_trans, header->commit_id,
509 header->refs_str);
510 if (error)
511 goto done;
512 error = gw_gen_tree_header(gw_trans, header->tree_id);
513 if (error)
514 goto done;
515 error = gw_gen_author_header(gw_trans, header->author);
516 if (error)
517 goto done;
518 error = gw_gen_committer_header(gw_trans, header->author);
519 if (error)
520 goto done;
521 error = gw_get_time_str(&age, header->committer_time,
522 TM_LONG);
523 if (error)
524 goto done;
525 error = gw_gen_age_header(gw_trans, age ?age : "");
526 if (error)
527 goto done;
528 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
529 if (error)
530 goto done;
531 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
532 if (kerr != KCGI_OK)
533 goto done;
534 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
535 "dotted_line", KATTR__MAX);
536 if (kerr != KCGI_OK)
537 goto done;
538 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
539 if (kerr != KCGI_OK)
540 goto done;
542 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
543 "diff", KATTR__MAX);
544 if (kerr != KCGI_OK)
545 goto done;
546 error = gw_output_diff(gw_trans, header);
547 if (error)
548 goto done;
550 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
551 done:
552 gw_free_header(header);
553 free(age);
554 if (error == NULL && kerr != KCGI_OK)
555 error = gw_kcgi_error(kerr);
556 return error;
559 static const struct got_error *
560 gw_index(struct gw_trans *gw_trans)
562 const struct got_error *error = NULL;
563 struct gw_dir *gw_dir = NULL;
564 char *href_next = NULL, *href_prev = NULL, *href_summary = NULL;
565 char *href_briefs = NULL, *href_commits = NULL, *href_tree = NULL;
566 char *href_tags = NULL;
567 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
568 enum kcgi_err kerr = KCGI_OK;
570 #ifndef PROFILE
571 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
572 NULL) == -1) {
573 error = got_error_from_errno("pledge");
574 return error;
576 #endif
577 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path);
578 if (error)
579 return error;
581 error = gw_load_got_paths(gw_trans);
582 if (error)
583 return error;
585 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
586 "index_header", KATTR__MAX);
587 if (kerr != KCGI_OK)
588 return gw_kcgi_error(kerr);
589 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
590 "index_header_project", KATTR__MAX);
591 if (kerr != KCGI_OK)
592 return gw_kcgi_error(kerr);
593 kerr = khtml_puts(gw_trans->gw_html_req, "Project");
594 if (kerr != KCGI_OK)
595 return gw_kcgi_error(kerr);
596 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
597 if (kerr != KCGI_OK)
598 return gw_kcgi_error(kerr);
600 if (gw_trans->gw_conf->got_show_repo_description) {
601 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
602 "index_header_description", KATTR__MAX);
603 if (kerr != KCGI_OK)
604 return gw_kcgi_error(kerr);
605 kerr = khtml_puts(gw_trans->gw_html_req, "Description");
606 if (kerr != KCGI_OK)
607 return gw_kcgi_error(kerr);
608 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
609 if (kerr != KCGI_OK)
610 return gw_kcgi_error(kerr);
613 if (gw_trans->gw_conf->got_show_repo_owner) {
614 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
615 "index_header_owner", KATTR__MAX);
616 if (kerr != KCGI_OK)
617 return gw_kcgi_error(kerr);
618 kerr = khtml_puts(gw_trans->gw_html_req, "Owner");
619 if (kerr != KCGI_OK)
620 return gw_kcgi_error(kerr);
621 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
622 if (kerr != KCGI_OK)
623 return gw_kcgi_error(kerr);
626 if (gw_trans->gw_conf->got_show_repo_age) {
627 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
628 "index_header_age", KATTR__MAX);
629 if (kerr != KCGI_OK)
630 return gw_kcgi_error(kerr);
631 kerr = khtml_puts(gw_trans->gw_html_req, "Last Change");
632 if (kerr != KCGI_OK)
633 return gw_kcgi_error(kerr);
634 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
635 if (kerr != KCGI_OK)
636 return gw_kcgi_error(kerr);
639 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
640 if (kerr != KCGI_OK)
641 return gw_kcgi_error(kerr);
643 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
644 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
645 "index_wrapper", KATTR__MAX);
646 if (kerr != KCGI_OK)
647 return gw_kcgi_error(kerr);
648 kerr = khtml_printf(gw_trans->gw_html_req,
649 "No repositories found in %s",
650 gw_trans->gw_conf->got_repos_path);
651 if (kerr != KCGI_OK)
652 return gw_kcgi_error(kerr);
653 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
654 if (kerr != KCGI_OK)
655 return gw_kcgi_error(kerr);
656 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
657 "dotted_line", KATTR__MAX);
658 if (kerr != KCGI_OK)
659 return gw_kcgi_error(kerr);
660 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
661 if (kerr != KCGI_OK)
662 return gw_kcgi_error(kerr);
663 return error;
666 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
667 dir_c++;
669 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
670 if (gw_trans->page > 0 && (gw_trans->page *
671 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
672 prev_disp++;
673 continue;
676 prev_disp++;
678 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
679 "index_wrapper", KATTR__MAX);
680 if (kerr != KCGI_OK)
681 goto done;
683 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
684 gw_dir->name, "action", "summary", NULL);
685 if (href_summary == NULL) {
686 error = got_error_from_errno("khttp_urlpart");
687 goto done;
689 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
690 "index_project", KATTR__MAX);
691 if (kerr != KCGI_OK)
692 goto done;
693 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
694 href_summary, KATTR__MAX);
695 if (kerr != KCGI_OK)
696 goto done;
697 kerr = khtml_puts(gw_trans->gw_html_req, gw_dir->name);
698 if (kerr != KCGI_OK)
699 goto done;
700 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
701 if (kerr != KCGI_OK)
702 goto done;
703 if (gw_trans->gw_conf->got_show_repo_description) {
704 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
705 KATTR_ID, "index_project_description", KATTR__MAX);
706 if (kerr != KCGI_OK)
707 goto done;
708 kerr = khtml_puts(gw_trans->gw_html_req,
709 gw_dir->description ? gw_dir->description : "");
710 if (kerr != KCGI_OK)
711 goto done;
712 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
713 if (kerr != KCGI_OK)
714 goto done;
716 if (gw_trans->gw_conf->got_show_repo_owner) {
717 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
718 KATTR_ID, "index_project_owner", KATTR__MAX);
719 if (kerr != KCGI_OK)
720 goto done;
721 kerr = khtml_puts(gw_trans->gw_html_req,
722 gw_dir->owner ? gw_dir->owner : "");
723 if (kerr != KCGI_OK)
724 goto done;
725 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
726 if (kerr != KCGI_OK)
727 goto done;
729 if (gw_trans->gw_conf->got_show_repo_age) {
730 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
731 KATTR_ID, "index_project_age", KATTR__MAX);
732 if (kerr != KCGI_OK)
733 goto done;
734 kerr = khtml_puts(gw_trans->gw_html_req,
735 gw_dir->age ? gw_dir->age : "");
736 if (kerr != KCGI_OK)
737 goto done;
738 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
739 if (kerr != KCGI_OK)
740 goto done;
743 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
744 "navs_wrapper", KATTR__MAX);
745 if (kerr != KCGI_OK)
746 goto done;
747 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
748 "navs", KATTR__MAX);
749 if (kerr != KCGI_OK)
750 goto done;
752 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
753 href_summary, KATTR__MAX);
754 if (kerr != KCGI_OK)
755 goto done;
756 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
757 if (kerr != KCGI_OK)
758 goto done;
759 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
760 if (kerr != KCGI_OK)
761 goto done;
763 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
764 if (kerr != KCGI_OK)
765 goto done;
767 href_briefs = khttp_urlpart(NULL, NULL, "gotweb", "path",
768 gw_dir->name, "action", "briefs", NULL);
769 if (href_briefs == NULL) {
770 error = got_error_from_errno("khttp_urlpart");
771 goto done;
773 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
774 href_briefs, KATTR__MAX);
775 if (kerr != KCGI_OK)
776 goto done;
777 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
778 if (kerr != KCGI_OK)
779 goto done;
780 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
781 if (kerr != KCGI_OK)
782 error = gw_kcgi_error(kerr);
784 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
785 if (kerr != KCGI_OK)
786 goto done;
788 href_commits = khttp_urlpart(NULL, NULL, "gotweb", "path",
789 gw_dir->name, "action", "commits", NULL);
790 if (href_commits == NULL) {
791 error = got_error_from_errno("khttp_urlpart");
792 goto done;
794 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
795 href_commits, KATTR__MAX);
796 if (kerr != KCGI_OK)
797 goto done;
798 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
799 if (kerr != KCGI_OK)
800 goto done;
801 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
802 if (kerr != KCGI_OK)
803 goto done;
805 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
806 if (kerr != KCGI_OK)
807 goto done;
809 href_tags = khttp_urlpart(NULL, NULL, "gotweb", "path",
810 gw_dir->name, "action", "tags", NULL);
811 if (href_tags == NULL) {
812 error = got_error_from_errno("khttp_urlpart");
813 goto done;
815 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
816 href_tags, KATTR__MAX);
817 if (kerr != KCGI_OK)
818 goto done;
819 kerr = khtml_puts(gw_trans->gw_html_req, "tags");
820 if (kerr != KCGI_OK)
821 goto done;
822 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
823 if (kerr != KCGI_OK)
824 goto done;
826 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
827 if (kerr != KCGI_OK)
828 goto done;
830 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
831 gw_dir->name, "action", "tree", NULL);
832 if (href_tree == NULL) {
833 error = got_error_from_errno("khttp_urlpart");
834 goto done;
836 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
837 href_tree, KATTR__MAX);
838 if (kerr != KCGI_OK)
839 goto done;
840 kerr = khtml_puts(gw_trans->gw_html_req, "tree");
841 if (kerr != KCGI_OK)
842 goto done;
844 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
845 if (kerr != KCGI_OK)
846 goto done;
847 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
848 "dotted_line", KATTR__MAX);
849 if (kerr != KCGI_OK)
850 goto done;
851 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
852 if (kerr != KCGI_OK)
853 goto done;
855 free(href_summary);
856 href_summary = NULL;
857 free(href_briefs);
858 href_briefs = NULL;
859 free(href_commits);
860 href_commits = NULL;
861 free(href_tags);
862 href_tags = NULL;
863 free(href_tree);
864 href_tree = NULL;
866 if (gw_trans->gw_conf->got_max_repos_display == 0)
867 continue;
869 if ((next_disp == gw_trans->gw_conf->got_max_repos_display) ||
870 ((gw_trans->gw_conf->got_max_repos_display > 0) &&
871 (gw_trans->page > 0) &&
872 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
873 prev_disp == gw_trans->repos_total))) {
874 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
875 KATTR_ID, "np_wrapper", KATTR__MAX);
876 if (kerr != KCGI_OK)
877 goto done;
878 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
879 KATTR_ID, "nav_prev", KATTR__MAX);
880 if (kerr != KCGI_OK)
881 goto done;
884 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
885 (gw_trans->page > 0) &&
886 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
887 prev_disp == gw_trans->repos_total)) {
888 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "page",
889 KATTRX_INT, (int64_t)(gw_trans->page - 1), NULL);
890 if (href_prev == NULL) {
891 error = got_error_from_errno("khttp_urlpartx");
892 goto done;
894 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
895 KATTR_HREF, href_prev, KATTR__MAX);
896 if (kerr != KCGI_OK)
897 goto done;
898 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
899 if (kerr != KCGI_OK)
900 goto done;
901 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
902 if (kerr != KCGI_OK)
903 goto done;
906 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
907 if (kerr != KCGI_OK)
908 return gw_kcgi_error(kerr);
910 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
911 next_disp == gw_trans->gw_conf->got_max_repos_display &&
912 dir_c != (gw_trans->page + 1) *
913 gw_trans->gw_conf->got_max_repos_display) {
914 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
915 KATTR_ID, "nav_next", KATTR__MAX);
916 if (kerr != KCGI_OK)
917 goto done;
918 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "page",
919 KATTRX_INT, (int64_t)(gw_trans->page + 1), NULL);
920 if (href_next == NULL) {
921 error = got_error_from_errno("khttp_urlpartx");
922 goto done;
924 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
925 KATTR_HREF, href_next, KATTR__MAX);
926 if (kerr != KCGI_OK)
927 goto done;
928 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
929 if (kerr != KCGI_OK)
930 goto done;
931 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
932 if (kerr != KCGI_OK)
933 goto done;
934 next_disp = 0;
935 break;
938 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
939 (gw_trans->page > 0) &&
940 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
941 prev_disp == gw_trans->repos_total)) {
942 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
943 if (kerr != KCGI_OK)
944 goto done;
946 next_disp++;
948 done:
949 free(href_prev);
950 free(href_next);
951 free(href_summary);
952 free(href_briefs);
953 free(href_commits);
954 free(href_tags);
955 free(href_tree);
956 if (error == NULL && kerr != KCGI_OK)
957 error = gw_kcgi_error(kerr);
958 return error;
961 static const struct got_error *
962 gw_commits(struct gw_trans *gw_trans)
964 const struct got_error *error = NULL;
965 struct gw_header *header = NULL, *n_header = NULL;
966 char *age = NULL, *href_diff = NULL, *href_tree = NULL;
967 char *href_prev = NULL, *href_next = NULL;
968 enum kcgi_err kerr = KCGI_OK;
969 int commit_found = 0;
971 if ((header = gw_init_header()) == NULL)
972 return got_error_from_errno("malloc");
974 #ifndef PROFILE
975 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
976 NULL) == -1) {
977 error = got_error_from_errno("pledge");
978 goto done;
980 #endif
981 error = gw_apply_unveil(gw_trans->gw_dir->path);
982 if (error)
983 goto done;
985 error = gw_get_header(gw_trans, header,
986 gw_trans->gw_conf->got_max_commits_display);
987 if (error)
988 goto done;
990 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
991 if (commit_found == 0 && gw_trans->commit_id != NULL) {
992 if (strcmp(gw_trans->commit_id,
993 n_header->commit_id) != 0)
994 continue;
995 else
996 commit_found = 1;
998 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
999 "commits_line_wrapper", KATTR__MAX);
1000 if (kerr != KCGI_OK)
1001 goto done;
1002 error = gw_gen_commit_header(gw_trans, n_header->commit_id,
1003 n_header->refs_str);
1004 if (error)
1005 goto done;
1006 error = gw_gen_author_header(gw_trans, n_header->author);
1007 if (error)
1008 goto done;
1009 error = gw_gen_committer_header(gw_trans, n_header->author);
1010 if (error)
1011 goto done;
1012 error = gw_get_time_str(&age, n_header->committer_time,
1013 TM_LONG);
1014 if (error)
1015 goto done;
1016 error = gw_gen_age_header(gw_trans, age ?age : "");
1017 if (error)
1018 goto done;
1019 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1020 if (kerr != KCGI_OK)
1021 goto done;
1023 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1024 "dotted_line", KATTR__MAX);
1025 if (kerr != KCGI_OK)
1026 goto done;
1027 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1028 if (kerr != KCGI_OK)
1029 goto done;
1031 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1032 "commit", KATTR__MAX);
1033 if (kerr != KCGI_OK)
1034 goto done;
1035 kerr = khttp_puts(gw_trans->gw_req, n_header->commit_msg);
1036 if (kerr != KCGI_OK)
1037 goto done;
1038 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1039 if (kerr != KCGI_OK)
1040 goto done;
1042 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
1043 gw_trans->repo_name, "action", "diff", "commit",
1044 n_header->commit_id, NULL);
1045 if (href_diff == NULL) {
1046 error = got_error_from_errno("khttp_urlpart");
1047 goto done;
1049 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1050 KATTR_ID, "navs_wrapper", KATTR__MAX);
1051 if (kerr != KCGI_OK)
1052 goto done;
1053 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1054 KATTR_ID, "navs", KATTR__MAX);
1055 if (kerr != KCGI_OK)
1056 goto done;
1057 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1058 KATTR_HREF, href_diff, KATTR__MAX);
1059 if (kerr != KCGI_OK)
1060 goto done;
1061 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1062 if (kerr != KCGI_OK)
1063 goto done;
1064 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1065 if (kerr != KCGI_OK)
1066 goto done;
1068 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1069 if (kerr != KCGI_OK)
1070 goto done;
1072 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
1073 gw_trans->repo_name, "action", "tree", "commit",
1074 n_header->commit_id, NULL);
1075 if (href_tree == NULL) {
1076 error = got_error_from_errno("khttp_urlpart");
1077 goto done;
1079 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1080 KATTR_HREF, href_tree, KATTR__MAX);
1081 if (kerr != KCGI_OK)
1082 goto done;
1083 khtml_puts(gw_trans->gw_html_req, "tree");
1084 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1085 if (kerr != KCGI_OK)
1086 goto done;
1087 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1088 if (kerr != KCGI_OK)
1089 goto done;
1091 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1092 "solid_line", KATTR__MAX);
1093 if (kerr != KCGI_OK)
1094 goto done;
1095 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1096 if (kerr != KCGI_OK)
1097 goto done;
1099 free(age);
1100 age = NULL;
1103 if (gw_trans->next_id || gw_trans->prev_id) {
1104 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1105 KATTR_ID, "np_wrapper", KATTR__MAX);
1106 if (kerr != KCGI_OK)
1107 goto done;
1108 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1109 KATTR_ID, "nav_prev", KATTR__MAX);
1110 if (kerr != KCGI_OK)
1111 goto done;
1114 if (gw_trans->prev_id) {
1115 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1116 KATTRX_STRING, gw_trans->repo_name, "page",
1117 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1118 KATTRX_STRING, "commits", "commit", KATTRX_STRING,
1119 gw_trans->prev_id ? gw_trans->prev_id : "", NULL);
1120 if (href_prev == NULL) {
1121 error = got_error_from_errno("khttp_urlpartx");
1122 goto done;
1124 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1125 KATTR_HREF, href_prev, KATTR__MAX);
1126 if (kerr != KCGI_OK)
1127 goto done;
1128 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1129 if (kerr != KCGI_OK)
1130 goto done;
1131 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1132 if (kerr != KCGI_OK)
1133 goto done;
1136 if (gw_trans->next_id || gw_trans->page > 0) {
1137 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1138 if (kerr != KCGI_OK)
1139 return gw_kcgi_error(kerr);
1142 if (gw_trans->next_id) {
1143 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1144 KATTR_ID, "nav_next", KATTR__MAX);
1145 if (kerr != KCGI_OK)
1146 goto done;
1147 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1148 KATTRX_STRING, gw_trans->repo_name, "page",
1149 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1150 KATTRX_STRING, "commits", "commit", KATTRX_STRING,
1151 gw_trans->next_id, NULL);
1152 if (href_next == NULL) {
1153 error = got_error_from_errno("khttp_urlpartx");
1154 goto done;
1156 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1157 KATTR_HREF, href_next, KATTR__MAX);
1158 if (kerr != KCGI_OK)
1159 goto done;
1160 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1161 if (kerr != KCGI_OK)
1162 goto done;
1163 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1164 if (kerr != KCGI_OK)
1165 goto done;
1168 if (gw_trans->next_id || gw_trans->page > 0) {
1169 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1170 if (kerr != KCGI_OK)
1171 goto done;
1173 done:
1174 gw_free_header(header);
1175 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1176 gw_free_header(n_header);
1177 free(age);
1178 free(href_next);
1179 free(href_prev);
1180 free(href_diff);
1181 free(href_tree);
1182 if (error == NULL && kerr != KCGI_OK)
1183 error = gw_kcgi_error(kerr);
1184 return error;
1187 static const struct got_error *
1188 gw_briefs(struct gw_trans *gw_trans)
1190 const struct got_error *error = NULL;
1191 struct gw_header *header = NULL, *n_header = NULL;
1192 char *age = NULL, *href_diff = NULL, *href_tree = NULL;
1193 char *href_prev = NULL, *href_next = NULL;
1194 char *newline, *smallerthan;
1195 enum kcgi_err kerr = KCGI_OK;
1196 int commit_found = 0;
1198 if ((header = gw_init_header()) == NULL)
1199 return got_error_from_errno("malloc");
1201 #ifndef PROFILE
1202 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
1203 NULL) == -1) {
1204 error = got_error_from_errno("pledge");
1205 goto done;
1207 #endif
1208 if (gw_trans->action != GW_SUMMARY) {
1209 error = gw_apply_unveil(gw_trans->gw_dir->path);
1210 if (error)
1211 goto done;
1214 if (gw_trans->action == GW_SUMMARY)
1215 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
1216 else
1217 error = gw_get_header(gw_trans, header,
1218 gw_trans->gw_conf->got_max_commits_display);
1219 if (error)
1220 goto done;
1222 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
1223 if (commit_found == 0 && gw_trans->commit_id != NULL) {
1224 if (strcmp(gw_trans->commit_id,
1225 n_header->commit_id) != 0)
1226 continue;
1227 else
1228 commit_found = 1;
1230 error = gw_get_time_str(&age, n_header->committer_time,
1231 TM_DIFF);
1232 if (error)
1233 goto done;
1235 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1236 KATTR_ID, "briefs_wrapper", KATTR__MAX);
1237 if (kerr != KCGI_OK)
1238 goto done;
1240 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1241 KATTR_ID, "briefs_age", KATTR__MAX);
1242 if (kerr != KCGI_OK)
1243 goto done;
1244 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
1245 if (kerr != KCGI_OK)
1246 goto done;
1247 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1248 if (kerr != KCGI_OK)
1249 goto done;
1251 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1252 KATTR_ID, "briefs_author", KATTR__MAX);
1253 if (kerr != KCGI_OK)
1254 goto done;
1255 smallerthan = strchr(n_header->author, '<');
1256 if (smallerthan)
1257 *smallerthan = '\0';
1258 kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
1259 if (kerr != KCGI_OK)
1260 goto done;
1261 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1262 if (kerr != KCGI_OK)
1263 goto done;
1265 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
1266 gw_trans->repo_name, "action", "diff", "commit",
1267 n_header->commit_id, NULL);
1268 if (href_diff == NULL) {
1269 error = got_error_from_errno("khttp_urlpart");
1270 goto done;
1272 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1273 KATTR_ID, "briefs_log", KATTR__MAX);
1274 if (kerr != KCGI_OK)
1275 goto done;
1276 newline = strchr(n_header->commit_msg, '\n');
1277 if (newline)
1278 *newline = '\0';
1279 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1280 KATTR_HREF, href_diff, KATTR__MAX);
1281 if (kerr != KCGI_OK)
1282 goto done;
1283 kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
1284 if (kerr != KCGI_OK)
1285 goto done;
1286 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1287 if (kerr != KCGI_OK)
1288 goto done;
1290 if (n_header->refs_str) {
1291 kerr = khtml_puts(gw_trans->gw_html_req, " ");
1292 if (kerr != KCGI_OK)
1293 goto done;
1294 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
1295 KATTR_ID, "refs_str", KATTR__MAX);
1296 if (kerr != KCGI_OK)
1297 goto done;
1298 kerr = khtml_printf(gw_trans->gw_html_req, "(%s)",
1299 n_header->refs_str);
1300 if (kerr != KCGI_OK)
1301 goto done;
1302 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1303 if (kerr != KCGI_OK)
1304 goto done;
1307 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1308 if (kerr != KCGI_OK)
1309 goto done;
1311 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1312 KATTR_ID, "navs_wrapper", KATTR__MAX);
1313 if (kerr != KCGI_OK)
1314 goto done;
1315 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1316 KATTR_ID, "navs", KATTR__MAX);
1317 if (kerr != KCGI_OK)
1318 goto done;
1319 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1320 KATTR_HREF, href_diff, KATTR__MAX);
1321 if (kerr != KCGI_OK)
1322 goto done;
1323 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1324 if (kerr != KCGI_OK)
1325 goto done;
1326 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1327 if (kerr != KCGI_OK)
1328 goto done;
1330 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1331 if (kerr != KCGI_OK)
1332 goto done;
1334 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
1335 gw_trans->repo_name, "action", "tree", "commit",
1336 n_header->commit_id, NULL);
1337 if (href_tree == NULL) {
1338 error = got_error_from_errno("khttp_urlpart");
1339 goto done;
1341 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1342 KATTR_HREF, href_tree, KATTR__MAX);
1343 if (kerr != KCGI_OK)
1344 goto done;
1345 khtml_puts(gw_trans->gw_html_req, "tree");
1346 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1347 if (kerr != KCGI_OK)
1348 goto done;
1349 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1350 if (kerr != KCGI_OK)
1351 goto done;
1353 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1354 KATTR_ID, "dotted_line", KATTR__MAX);
1355 if (kerr != KCGI_OK)
1356 goto done;
1357 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1358 if (kerr != KCGI_OK)
1359 goto done;
1361 free(age);
1362 age = NULL;
1363 free(href_diff);
1364 href_diff = NULL;
1365 free(href_tree);
1366 href_tree = NULL;
1369 if (gw_trans->next_id || gw_trans->prev_id) {
1370 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1371 KATTR_ID, "np_wrapper", KATTR__MAX);
1372 if (kerr != KCGI_OK)
1373 goto done;
1374 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1375 KATTR_ID, "nav_prev", KATTR__MAX);
1376 if (kerr != KCGI_OK)
1377 goto done;
1380 if (gw_trans->prev_id) {
1381 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1382 KATTRX_STRING, gw_trans->repo_name, "page",
1383 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1384 KATTRX_STRING, "briefs", "commit", KATTRX_STRING,
1385 gw_trans->prev_id ? gw_trans->prev_id : "", NULL);
1386 if (href_prev == NULL) {
1387 error = got_error_from_errno("khttp_urlpartx");
1388 goto done;
1390 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1391 KATTR_HREF, href_prev, KATTR__MAX);
1392 if (kerr != KCGI_OK)
1393 goto done;
1394 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1395 if (kerr != KCGI_OK)
1396 goto done;
1397 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1398 if (kerr != KCGI_OK)
1399 goto done;
1402 if (gw_trans->next_id || gw_trans->page > 0) {
1403 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1404 if (kerr != KCGI_OK)
1405 return gw_kcgi_error(kerr);
1408 if (gw_trans->next_id) {
1409 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1410 KATTR_ID, "nav_next", KATTR__MAX);
1411 if (kerr != KCGI_OK)
1412 goto done;
1414 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1415 KATTRX_STRING, gw_trans->repo_name, "page",
1416 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1417 KATTRX_STRING, "briefs", "commit", KATTRX_STRING,
1418 gw_trans->next_id, NULL);
1419 if (href_next == NULL) {
1420 error = got_error_from_errno("khttp_urlpartx");
1421 goto done;
1423 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1424 KATTR_HREF, href_next, KATTR__MAX);
1425 if (kerr != KCGI_OK)
1426 goto done;
1427 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1428 if (kerr != KCGI_OK)
1429 goto done;
1430 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1431 if (kerr != KCGI_OK)
1432 goto done;
1435 if (gw_trans->next_id || gw_trans->page > 0) {
1436 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1437 if (kerr != KCGI_OK)
1438 goto done;
1440 done:
1441 gw_free_header(header);
1442 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1443 gw_free_header(n_header);
1444 free(age);
1445 free(href_next);
1446 free(href_prev);
1447 free(href_diff);
1448 free(href_tree);
1449 if (error == NULL && kerr != KCGI_OK)
1450 error = gw_kcgi_error(kerr);
1451 return error;
1454 static const struct got_error *
1455 gw_summary(struct gw_trans *gw_trans)
1457 const struct got_error *error = NULL;
1458 char *age = NULL;
1459 enum kcgi_err kerr = KCGI_OK;
1461 #ifndef PROFILE
1462 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
1463 NULL) == -1)
1464 return got_error_from_errno("pledge");
1465 #endif
1466 error = gw_apply_unveil(gw_trans->gw_dir->path);
1467 if (error)
1468 goto done;
1470 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1471 "summary_wrapper", KATTR__MAX);
1472 if (kerr != KCGI_OK)
1473 return gw_kcgi_error(kerr);
1475 if (gw_trans->gw_conf->got_show_repo_description &&
1476 gw_trans->gw_dir->description != NULL &&
1477 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1478 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1479 KATTR_ID, "description_title", KATTR__MAX);
1480 if (kerr != KCGI_OK)
1481 goto done;
1482 kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1483 if (kerr != KCGI_OK)
1484 goto done;
1485 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1486 if (kerr != KCGI_OK)
1487 goto done;
1488 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1489 KATTR_ID, "description", KATTR__MAX);
1490 if (kerr != KCGI_OK)
1491 goto done;
1492 kerr = khtml_puts(gw_trans->gw_html_req,
1493 gw_trans->gw_dir->description);
1494 if (kerr != KCGI_OK)
1495 goto done;
1496 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1497 if (kerr != KCGI_OK)
1498 goto done;
1501 if (gw_trans->gw_conf->got_show_repo_owner &&
1502 gw_trans->gw_dir->owner != NULL &&
1503 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
1504 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1505 KATTR_ID, "repo_owner_title", KATTR__MAX);
1506 if (kerr != KCGI_OK)
1507 goto done;
1508 kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1509 if (kerr != KCGI_OK)
1510 goto done;
1511 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1512 if (kerr != KCGI_OK)
1513 goto done;
1514 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1515 KATTR_ID, "repo_owner", KATTR__MAX);
1516 if (kerr != KCGI_OK)
1517 goto done;
1518 kerr = khtml_puts(gw_trans->gw_html_req,
1519 gw_trans->gw_dir->owner);
1520 if (kerr != KCGI_OK)
1521 goto done;
1522 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1523 if (kerr != KCGI_OK)
1524 goto done;
1527 if (gw_trans->gw_conf->got_show_repo_age) {
1528 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1529 NULL, TM_LONG);
1530 if (error)
1531 goto done;
1532 if (age != NULL) {
1533 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1534 KATTR_ID, "last_change_title", KATTR__MAX);
1535 if (kerr != KCGI_OK)
1536 goto done;
1537 kerr = khtml_puts(gw_trans->gw_html_req,
1538 "Last Change: ");
1539 if (kerr != KCGI_OK)
1540 goto done;
1541 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1542 if (kerr != KCGI_OK)
1543 goto done;
1544 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1545 KATTR_ID, "last_change", KATTR__MAX);
1546 if (kerr != KCGI_OK)
1547 goto done;
1548 kerr = khtml_puts(gw_trans->gw_html_req, age);
1549 if (kerr != KCGI_OK)
1550 goto done;
1551 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1552 if (kerr != KCGI_OK)
1553 goto done;
1557 if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1558 gw_trans->gw_dir->url != NULL &&
1559 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1560 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1561 KATTR_ID, "cloneurl_title", KATTR__MAX);
1562 if (kerr != KCGI_OK)
1563 goto done;
1564 kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1565 if (kerr != KCGI_OK)
1566 goto done;
1567 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1568 if (kerr != KCGI_OK)
1569 goto done;
1570 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1571 KATTR_ID, "cloneurl", KATTR__MAX);
1572 if (kerr != KCGI_OK)
1573 goto done;
1574 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1575 if (kerr != KCGI_OK)
1576 goto done;
1577 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1578 if (kerr != KCGI_OK)
1579 goto done;
1582 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1583 if (kerr != KCGI_OK)
1584 goto done;
1586 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1587 "briefs_title_wrapper", KATTR__MAX);
1588 if (kerr != KCGI_OK)
1589 goto done;
1590 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1591 "briefs_title", KATTR__MAX);
1592 if (kerr != KCGI_OK)
1593 goto done;
1594 kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1595 if (kerr != KCGI_OK)
1596 goto done;
1597 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1598 if (kerr != KCGI_OK)
1599 goto done;
1600 error = gw_briefs(gw_trans);
1601 if (error)
1602 goto done;
1604 error = gw_tags(gw_trans);
1605 if (error)
1606 goto done;
1608 error = gw_output_repo_heads(gw_trans);
1609 done:
1610 free(age);
1611 if (error == NULL && kerr != KCGI_OK)
1612 error = gw_kcgi_error(kerr);
1613 return error;
1616 static const struct got_error *
1617 gw_tree(struct gw_trans *gw_trans)
1619 const struct got_error *error = NULL;
1620 struct gw_header *header = NULL;
1621 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1622 char *age = NULL;
1623 enum kcgi_err kerr = KCGI_OK;
1625 #ifndef PROFILE
1626 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
1627 NULL) == -1)
1628 return got_error_from_errno("pledge");
1629 #endif
1630 if ((header = gw_init_header()) == NULL)
1631 return got_error_from_errno("malloc");
1633 error = gw_apply_unveil(gw_trans->gw_dir->path);
1634 if (error)
1635 goto done;
1637 error = gw_get_header(gw_trans, header, 1);
1638 if (error)
1639 goto done;
1641 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1642 "tree_header_wrapper", KATTR__MAX);
1643 if (kerr != KCGI_OK)
1644 goto done;
1645 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1646 "tree_header", KATTR__MAX);
1647 if (kerr != KCGI_OK)
1648 goto done;
1649 error = gw_gen_tree_header(gw_trans, header->tree_id);
1650 if (error)
1651 goto done;
1652 error = gw_get_time_str(&age, header->committer_time,
1653 TM_LONG);
1654 if (error)
1655 goto done;
1656 error = gw_gen_age_header(gw_trans, age ?age : "");
1657 if (error)
1658 goto done;
1659 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1660 if (error)
1661 goto done;
1662 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1663 if (kerr != KCGI_OK)
1664 goto done;
1665 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1666 "dotted_line", KATTR__MAX);
1667 if (kerr != KCGI_OK)
1668 goto done;
1669 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1670 if (kerr != KCGI_OK)
1671 goto done;
1673 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1674 "tree", KATTR__MAX);
1675 if (kerr != KCGI_OK)
1676 goto done;
1677 error = gw_output_repo_tree(gw_trans, header);
1678 if (error)
1679 goto done;
1681 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1682 done:
1683 gw_free_header(header);
1684 free(tree_html_disp);
1685 free(tree_html);
1686 free(tree);
1687 free(age);
1688 if (error == NULL && kerr != KCGI_OK)
1689 error = gw_kcgi_error(kerr);
1690 return error;
1693 static const struct got_error *
1694 gw_tags(struct gw_trans *gw_trans)
1696 const struct got_error *error = NULL;
1697 struct gw_header *header = NULL;
1698 char *href_next = NULL, *href_prev = NULL;
1699 enum kcgi_err kerr = KCGI_OK;
1701 #ifndef PROFILE
1702 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
1703 NULL) == -1)
1704 return got_error_from_errno("pledge");
1705 #endif
1706 if ((header = gw_init_header()) == NULL)
1707 return got_error_from_errno("malloc");
1709 if (gw_trans->action != GW_SUMMARY) {
1710 error = gw_apply_unveil(gw_trans->gw_dir->path);
1711 if (error)
1712 goto done;
1715 error = gw_get_header(gw_trans, header, 1);
1716 if (error)
1717 goto done;
1719 if (gw_trans->action == GW_SUMMARY) {
1720 gw_trans->next_id = NULL;
1721 error = gw_output_repo_tags(gw_trans, header,
1722 D_MAXSLCOMMDISP, TAGBRIEF);
1723 if (error)
1724 goto done;
1725 } else {
1726 error = gw_output_repo_tags(gw_trans, header,
1727 gw_trans->gw_conf->got_max_commits_display, TAGBRIEF);
1728 if (error)
1729 goto done;
1732 if (gw_trans->next_id || gw_trans->page > 0) {
1733 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1734 KATTR_ID, "np_wrapper", KATTR__MAX);
1735 if (kerr != KCGI_OK)
1736 goto done;
1737 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1738 KATTR_ID, "nav_prev", KATTR__MAX);
1739 if (kerr != KCGI_OK)
1740 goto done;
1743 if (gw_trans->prev_id) {
1744 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1745 KATTRX_STRING, gw_trans->repo_name, "page",
1746 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1747 KATTRX_STRING, "tags", "commit", KATTRX_STRING,
1748 gw_trans->prev_id ? gw_trans->prev_id : "", NULL);
1749 if (href_prev == NULL) {
1750 error = got_error_from_errno("khttp_urlpartx");
1751 goto done;
1753 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1754 KATTR_HREF, href_prev, KATTR__MAX);
1755 if (kerr != KCGI_OK)
1756 goto done;
1757 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1758 if (kerr != KCGI_OK)
1759 goto done;
1760 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1761 if (kerr != KCGI_OK)
1762 goto done;
1765 if (gw_trans->next_id || gw_trans->page > 0) {
1766 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1767 if (kerr != KCGI_OK)
1768 return gw_kcgi_error(kerr);
1771 if (gw_trans->next_id) {
1772 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1773 KATTR_ID, "nav_next", KATTR__MAX);
1774 if (kerr != KCGI_OK)
1775 goto done;
1776 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1777 KATTRX_STRING, gw_trans->repo_name, "page",
1778 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1779 KATTRX_STRING, "tags", "commit", KATTRX_STRING,
1780 gw_trans->next_id, NULL);
1781 if (href_next == NULL) {
1782 error = got_error_from_errno("khttp_urlpartx");
1783 goto done;
1785 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1786 KATTR_HREF, href_next, KATTR__MAX);
1787 if (kerr != KCGI_OK)
1788 goto done;
1789 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1790 if (kerr != KCGI_OK)
1791 goto done;
1792 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1793 if (kerr != KCGI_OK)
1794 goto done;
1797 if (gw_trans->next_id || gw_trans->page > 0) {
1798 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1799 if (kerr != KCGI_OK)
1800 goto done;
1802 done:
1803 gw_free_header(header);
1804 free(href_next);
1805 free(href_prev);
1806 if (error == NULL && kerr != KCGI_OK)
1807 error = gw_kcgi_error(kerr);
1808 return error;
1811 static const struct got_error *
1812 gw_tag(struct gw_trans *gw_trans)
1814 const struct got_error *error = NULL;
1815 struct gw_header *header = NULL;
1816 enum kcgi_err kerr = KCGI_OK;
1818 #ifndef PROFILE
1819 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil", NULL) == -1)
1820 return got_error_from_errno("pledge");
1821 #endif
1822 if ((header = gw_init_header()) == NULL)
1823 return got_error_from_errno("malloc");
1825 error = gw_apply_unveil(gw_trans->gw_dir->path);
1826 if (error)
1827 goto done;
1829 if (gw_trans->commit_id == NULL) {
1830 error = got_error_msg(GOT_ERR_QUERYSTRING,
1831 "commit required in querystring");
1832 goto done;
1835 error = gw_get_header(gw_trans, header, 1);
1836 if (error)
1837 goto done;
1839 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1840 "tag_header_wrapper", KATTR__MAX);
1841 if (kerr != KCGI_OK)
1842 goto done;
1843 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1844 "tag_header", KATTR__MAX);
1845 if (kerr != KCGI_OK)
1846 goto done;
1847 error = gw_gen_commit_header(gw_trans, header->commit_id,
1848 header->refs_str);
1849 if (error)
1850 goto done;
1851 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1852 if (error)
1853 goto done;
1854 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1855 if (kerr != KCGI_OK)
1856 goto done;
1857 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1858 "dotted_line", KATTR__MAX);
1859 if (kerr != KCGI_OK)
1860 goto done;
1861 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1862 if (kerr != KCGI_OK)
1863 goto done;
1865 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1866 "tree", KATTR__MAX);
1867 if (kerr != KCGI_OK)
1868 goto done;
1870 error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1871 if (error)
1872 goto done;
1874 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1875 done:
1876 gw_free_header(header);
1877 if (error == NULL && kerr != KCGI_OK)
1878 error = gw_kcgi_error(kerr);
1879 return error;
1882 static const struct got_error *
1883 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1885 const struct got_error *error = NULL;
1886 DIR *dt;
1887 char *dir_test;
1888 int opened = 0;
1890 if (asprintf(&dir_test, "%s/%s/%s",
1891 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1892 GOTWEB_GIT_DIR) == -1)
1893 return got_error_from_errno("asprintf");
1895 dt = opendir(dir_test);
1896 if (dt == NULL) {
1897 free(dir_test);
1898 } else {
1899 gw_dir->path = strdup(dir_test);
1900 if (gw_dir->path == NULL) {
1901 opened = 1;
1902 error = got_error_from_errno("strdup");
1903 goto errored;
1905 opened = 1;
1906 goto done;
1909 if (asprintf(&dir_test, "%s/%s/%s",
1910 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1911 GOTWEB_GOT_DIR) == -1) {
1912 dir_test = NULL;
1913 error = got_error_from_errno("asprintf");
1914 goto errored;
1917 dt = opendir(dir_test);
1918 if (dt == NULL)
1919 free(dir_test);
1920 else {
1921 opened = 1;
1922 error = got_error(GOT_ERR_NOT_GIT_REPO);
1923 goto errored;
1926 if (asprintf(&dir_test, "%s/%s",
1927 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1) {
1928 error = got_error_from_errno("asprintf");
1929 dir_test = NULL;
1930 goto errored;
1933 gw_dir->path = strdup(dir_test);
1934 if (gw_dir->path == NULL) {
1935 opened = 1;
1936 error = got_error_from_errno("strdup");
1937 goto errored;
1940 dt = opendir(dir_test);
1941 if (dt == NULL) {
1942 error = got_error_path(gw_dir->name, GOT_ERR_NOT_GIT_REPO);
1943 goto errored;
1944 } else
1945 opened = 1;
1946 done:
1947 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1948 gw_dir->path);
1949 if (error)
1950 goto errored;
1951 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1952 if (error)
1953 goto errored;
1954 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1955 NULL, TM_DIFF);
1956 if (error)
1957 goto errored;
1958 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1959 errored:
1960 free(dir_test);
1961 if (opened)
1962 if (dt && closedir(dt) == -1 && error == NULL)
1963 error = got_error_from_errno("closedir");
1964 return error;
1967 static const struct got_error *
1968 gw_load_got_paths(struct gw_trans *gw_trans)
1970 const struct got_error *error = NULL;
1971 DIR *d;
1972 struct dirent **sd_dent;
1973 struct gw_dir *gw_dir;
1974 struct stat st;
1975 unsigned int d_cnt, d_i;
1977 d = opendir(gw_trans->gw_conf->got_repos_path);
1978 if (d == NULL) {
1979 error = got_error_from_errno2("opendir",
1980 gw_trans->gw_conf->got_repos_path);
1981 return error;
1984 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1985 alphasort);
1986 if (d_cnt == -1) {
1987 error = got_error_from_errno2("scandir",
1988 gw_trans->gw_conf->got_repos_path);
1989 goto done;
1992 for (d_i = 0; d_i < d_cnt; d_i++) {
1993 if (gw_trans->gw_conf->got_max_repos > 0 &&
1994 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1995 break; /* account for parent and self */
1997 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1998 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1999 continue;
2001 error = gw_init_gw_dir(&gw_dir, sd_dent[d_i]->d_name);
2002 if (error)
2003 goto done;
2005 error = gw_load_got_path(gw_trans, gw_dir);
2006 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
2007 error = NULL;
2008 continue;
2009 } else if (error && error->code != GOT_ERR_LONELY_PACKIDX)
2010 goto done;
2012 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
2013 !got_path_dir_is_empty(gw_dir->path)) {
2014 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
2015 entry);
2016 gw_trans->repos_total++;
2019 done:
2020 if (d && closedir(d) == -1 && error == NULL)
2021 error = got_error_from_errno("closedir");
2022 return error;
2025 static const struct got_error *
2026 gw_parse_querystring(struct gw_trans *gw_trans)
2028 const struct got_error *error = NULL;
2029 struct kpair *p;
2030 const struct gw_query_action *action = NULL;
2031 unsigned int i;
2033 if (gw_trans->gw_req->fieldnmap[0]) {
2034 return got_error(GOT_ERR_QUERYSTRING);
2035 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
2036 /* define gw_trans->repo_path */
2037 gw_trans->repo_name = p->parsed.s;
2039 if (asprintf(&gw_trans->repo_path, "%s/%s",
2040 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
2041 return got_error_from_errno("asprintf");
2043 /* get action and set function */
2044 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION])) {
2045 for (i = 0; i < nitems(gw_query_funcs); i++) {
2046 action = &gw_query_funcs[i];
2047 if (action->func_name == NULL)
2048 continue;
2049 if (strcmp(action->func_name,
2050 p->parsed.s) == 0) {
2051 gw_trans->action = i;
2052 break;
2056 if (gw_trans->action == -1) {
2057 gw_trans->action = GW_ERR;
2058 gw_trans->error = got_error_msg(GOT_ERR_QUERYSTRING,
2059 p != NULL ? "bad action in querystring" :
2060 "no action in querystring");
2061 return error;
2064 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID])) {
2065 if (asprintf(&gw_trans->commit_id, "%s",
2066 p->parsed.s) == -1)
2067 return got_error_from_errno("asprintf");
2070 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
2071 gw_trans->repo_file = p->parsed.s;
2073 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER])) {
2074 if (asprintf(&gw_trans->repo_folder, "%s",
2075 p->parsed.s) == -1)
2076 return got_error_from_errno("asprintf");
2079 if ((p = gw_trans->gw_req->fieldmap[KEY_PREV_ID])) {
2080 if (asprintf(&gw_trans->prev_id, "%s",
2081 p->parsed.s) == -1)
2082 return got_error_from_errno("asprintf");
2085 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
2086 gw_trans->headref = p->parsed.s;
2088 error = gw_init_gw_dir(&gw_trans->gw_dir, gw_trans->repo_name);
2089 if (error)
2090 return error;
2092 gw_trans->error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
2093 } else
2094 gw_trans->action = GW_INDEX;
2096 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
2097 gw_trans->page = p->parsed.i;
2099 return error;
2102 static const struct got_error *
2103 gw_init_gw_dir(struct gw_dir **gw_dir, const char *dir)
2105 const struct got_error *error;
2107 *gw_dir = malloc(sizeof(**gw_dir));
2108 if (*gw_dir == NULL)
2109 return got_error_from_errno("malloc");
2111 if (asprintf(&(*gw_dir)->name, "%s", dir) == -1) {
2112 error = got_error_from_errno("asprintf");
2113 free(*gw_dir);
2114 *gw_dir = NULL;
2115 return error;
2118 return NULL;
2121 static const struct got_error *
2122 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
2124 enum kcgi_err kerr = KCGI_OK;
2126 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
2127 if (kerr != KCGI_OK)
2128 return gw_kcgi_error(kerr);
2129 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
2130 khttps[code]);
2131 if (kerr != KCGI_OK)
2132 return gw_kcgi_error(kerr);
2133 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
2134 kmimetypes[mime]);
2135 if (kerr != KCGI_OK)
2136 return gw_kcgi_error(kerr);
2137 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
2138 "nosniff");
2139 if (kerr != KCGI_OK)
2140 return gw_kcgi_error(kerr);
2141 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
2142 if (kerr != KCGI_OK)
2143 return gw_kcgi_error(kerr);
2144 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
2145 "1; mode=block");
2146 if (kerr != KCGI_OK)
2147 return gw_kcgi_error(kerr);
2149 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
2150 kerr = khttp_head(gw_trans->gw_req,
2151 kresps[KRESP_CONTENT_DISPOSITION],
2152 "attachment; filename=%s", gw_trans->repo_file);
2153 if (kerr != KCGI_OK)
2154 return gw_kcgi_error(kerr);
2157 kerr = khttp_body(gw_trans->gw_req);
2158 return gw_kcgi_error(kerr);
2161 static const struct got_error *
2162 gw_display_index(struct gw_trans *gw_trans)
2164 const struct got_error *error;
2165 enum kcgi_err kerr = KCGI_OK;
2167 /* catch early querystring errors */
2168 if (gw_trans->error)
2169 gw_trans->action = GW_ERR;
2171 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
2172 if (error)
2173 return error;
2175 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
2176 if (kerr != KCGI_OK)
2177 return gw_kcgi_error(kerr);
2179 if (gw_trans->action != GW_BLOB) {
2180 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
2181 gw_query_funcs[gw_trans->action].template);
2182 if (kerr != KCGI_OK) {
2183 khtml_close(gw_trans->gw_html_req);
2184 return gw_kcgi_error(kerr);
2188 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
2191 static const struct got_error *
2192 gw_error(struct gw_trans *gw_trans)
2194 enum kcgi_err kerr = KCGI_OK;
2196 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->error->msg);
2198 return gw_kcgi_error(kerr);
2201 static int
2202 gw_template(size_t key, void *arg)
2204 const struct got_error *error = NULL;
2205 enum kcgi_err kerr = KCGI_OK;
2206 struct gw_trans *gw_trans = arg;
2207 char *ati = NULL, *fic32 = NULL, *fic16 = NULL;
2208 char *swm = NULL, *spt = NULL, *css = NULL, *logo = NULL;
2210 if (asprintf(&ati, "%s%s", gw_trans->gw_conf->got_www_path,
2211 "/apple-touch-icon.png") == -1)
2212 goto err;
2213 if (asprintf(&fic32, "%s%s", gw_trans->gw_conf->got_www_path,
2214 "/favicon-32x32.png") == -1)
2215 goto err;
2216 if (asprintf(&fic16, "%s%s", gw_trans->gw_conf->got_www_path,
2217 "/favicon-16x16.png") == -1)
2218 goto err;
2219 if (asprintf(&swm, "%s%s", gw_trans->gw_conf->got_www_path,
2220 "/site.webmanifest") == -1)
2221 goto err;
2222 if (asprintf(&spt, "%s%s", gw_trans->gw_conf->got_www_path,
2223 "/safari-pinned-tab.svg") == -1)
2224 goto err;
2225 if (asprintf(&css, "%s%s", gw_trans->gw_conf->got_www_path,
2226 "/gotweb.css") == -1)
2227 goto err;
2228 if (asprintf(&logo, "%s%s%s", gw_trans->gw_conf->got_www_path,
2229 gw_trans->gw_conf->got_www_path ? "/" : "",
2230 gw_trans->gw_conf->got_logo) == -1)
2231 goto err;
2233 switch (key) {
2234 case (TEMPL_HEAD):
2235 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2236 KATTR_NAME, "viewport",
2237 KATTR_CONTENT, "initial-scale=.75, user-scalable=yes",
2238 KATTR__MAX);
2239 if (kerr != KCGI_OK)
2240 return 0;
2241 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2242 if (kerr != KCGI_OK)
2243 return 0;
2244 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2245 KATTR_CHARSET, "utf-8",
2246 KATTR__MAX);
2247 if (kerr != KCGI_OK)
2248 return 0;
2249 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2250 if (kerr != KCGI_OK)
2251 return 0;
2252 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2253 KATTR_NAME, "msapplication-TileColor",
2254 KATTR_CONTENT, "#da532c", KATTR__MAX);
2255 if (kerr != KCGI_OK)
2256 return 0;
2257 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2258 if (kerr != KCGI_OK)
2259 return 0;
2260 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2261 KATTR_NAME, "theme-color",
2262 KATTR_CONTENT, "#ffffff", KATTR__MAX);
2263 if (kerr != KCGI_OK)
2264 return 0;
2265 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2266 if (kerr != KCGI_OK)
2267 return 0;
2268 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2269 KATTR_REL, "apple-touch-icon", KATTR_SIZES, "180x180",
2270 KATTR_HREF, ati, KATTR__MAX);
2271 if (kerr != KCGI_OK)
2272 return 0;
2273 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2274 if (kerr != KCGI_OK)
2275 return 0;
2276 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2277 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2278 "32x32", KATTR_HREF, fic32, KATTR__MAX);
2279 if (kerr != KCGI_OK)
2280 return 0;
2281 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2282 if (kerr != KCGI_OK)
2283 return 0;
2284 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2285 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2286 "16x16", KATTR_HREF, fic16, KATTR__MAX);
2287 if (kerr != KCGI_OK)
2288 return 0;
2289 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2290 if (kerr != KCGI_OK)
2291 return 0;
2292 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2293 KATTR_REL, "manifest", KATTR_HREF, swm,
2294 KATTR__MAX);
2295 if (kerr != KCGI_OK)
2296 return 0;
2297 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2298 if (kerr != KCGI_OK)
2299 return 0;
2300 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2301 KATTR_REL, "mask-icon", KATTR_HREF,
2302 spt, KATTR__MAX);
2303 if (kerr != KCGI_OK)
2304 return 0;
2305 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2306 if (kerr != KCGI_OK)
2307 return 0;
2308 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2309 KATTR_REL, "stylesheet", KATTR_TYPE, "text/css",
2310 KATTR_HREF, css, KATTR__MAX);
2311 if (kerr != KCGI_OK)
2312 return 0;
2313 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2314 if (kerr != KCGI_OK)
2315 return 0;
2316 break;
2317 case(TEMPL_HEADER):
2318 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2319 KATTR_ID, "got_link", KATTR__MAX);
2320 if (kerr != KCGI_OK)
2321 return 0;
2322 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2323 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
2324 KATTR_TARGET, "_sotd", KATTR__MAX);
2325 if (kerr != KCGI_OK)
2326 return 0;
2327 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
2328 KATTR_SRC, logo, KATTR__MAX);
2329 if (kerr != KCGI_OK)
2330 return 0;
2331 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2332 if (kerr != KCGI_OK)
2333 return 0;
2334 break;
2335 case (TEMPL_SITEPATH):
2336 error = gw_output_site_link(gw_trans);
2337 if (error)
2338 return 0;
2339 break;
2340 case(TEMPL_TITLE):
2341 if (gw_trans->gw_conf->got_site_name != NULL) {
2342 kerr = khtml_puts(gw_trans->gw_html_req,
2343 gw_trans->gw_conf->got_site_name);
2344 if (kerr != KCGI_OK)
2345 return 0;
2347 break;
2348 case (TEMPL_SEARCH):
2349 break;
2350 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
2351 "search", KATTR__MAX);
2352 if (kerr != KCGI_OK)
2353 return 0;
2354 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
2355 KATTR_METHOD, "POST", KATTR__MAX);
2356 if (kerr != KCGI_OK)
2357 return 0;
2358 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
2359 "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
2360 KATTR_MAXLENGTH, "50", KATTR__MAX);
2361 if (kerr != KCGI_OK)
2362 return 0;
2363 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
2364 KATTR__MAX);
2365 if (kerr != KCGI_OK)
2366 return 0;
2367 kerr = khtml_puts(gw_trans->gw_html_req, "Search");
2368 if (kerr != KCGI_OK)
2369 return 0;
2370 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
2371 if (kerr != KCGI_OK)
2372 return 0;
2373 break;
2374 case(TEMPL_SITEOWNER):
2375 if (gw_trans->gw_conf->got_site_owner != NULL &&
2376 gw_trans->gw_conf->got_show_site_owner) {
2377 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2378 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
2379 if (kerr != KCGI_OK)
2380 return 0;
2381 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2382 KATTR_ID, "site_owner", KATTR__MAX);
2383 if (kerr != KCGI_OK)
2384 return 0;
2385 kerr = khtml_puts(gw_trans->gw_html_req,
2386 gw_trans->gw_conf->got_site_owner);
2387 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2388 if (kerr != KCGI_OK)
2389 return 0;
2391 break;
2392 case(TEMPL_CONTENT):
2393 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
2394 if (error) {
2395 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2396 KATTR_ID, "tmpl_err", KATTR__MAX);
2397 if (kerr != KCGI_OK)
2398 return 0;
2399 kerr = khttp_printf(gw_trans->gw_req, "Error: %s",
2400 error->msg);
2401 if (kerr != KCGI_OK)
2402 return 0;
2403 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2404 if (kerr != KCGI_OK)
2405 return 0;
2407 break;
2408 default:
2409 return 0;
2411 free(ati);
2412 free(fic32);
2413 free(fic16);
2414 free(swm);
2415 free(spt);
2416 free(css);
2417 free(logo);
2418 return 1;
2419 err:
2420 free(ati);
2421 free(fic32);
2422 free(fic16);
2423 free(swm);
2424 free(spt);
2425 free(css);
2426 free(logo);
2427 return 0;
2430 static const struct got_error *
2431 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
2433 const struct got_error *error = NULL;
2434 enum kcgi_err kerr = KCGI_OK;
2436 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2437 KATTR_ID, "header_commit_title", KATTR__MAX);
2438 if (kerr != KCGI_OK)
2439 goto done;
2440 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
2441 if (kerr != KCGI_OK)
2442 goto done;
2443 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2444 if (kerr != KCGI_OK)
2445 goto done;
2446 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2447 KATTR_ID, "header_commit", KATTR__MAX);
2448 if (kerr != KCGI_OK)
2449 goto done;
2450 kerr = khtml_printf(gw_trans->gw_html_req, "%s ", str1);
2451 if (kerr != KCGI_OK)
2452 goto done;
2453 if (str2 != NULL) {
2454 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
2455 KATTR_ID, "refs_str", KATTR__MAX);
2456 if (kerr != KCGI_OK)
2457 goto done;
2458 kerr = khtml_printf(gw_trans->gw_html_req, "(%s)", str2);
2459 if (kerr != KCGI_OK)
2460 goto done;
2461 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2462 if (kerr != KCGI_OK)
2463 goto done;
2465 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2466 done:
2467 if (error == NULL && kerr != KCGI_OK)
2468 error = gw_kcgi_error(kerr);
2469 return error;
2472 static const struct got_error *
2473 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2475 const struct got_error *error = NULL;
2476 enum kcgi_err kerr = KCGI_OK;
2478 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2479 KATTR_ID, "header_diff_title", KATTR__MAX);
2480 if (kerr != KCGI_OK)
2481 goto done;
2482 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2483 if (kerr != KCGI_OK)
2484 goto done;
2485 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2486 if (kerr != KCGI_OK)
2487 goto done;
2488 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2489 KATTR_ID, "header_diff", KATTR__MAX);
2490 if (kerr != KCGI_OK)
2491 goto done;
2492 if (str1 != NULL) {
2493 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2494 if (kerr != KCGI_OK)
2495 goto done;
2497 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2498 if (kerr != KCGI_OK)
2499 goto done;
2500 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2501 if (kerr != KCGI_OK)
2502 goto done;
2503 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2504 done:
2505 if (error == NULL && kerr != KCGI_OK)
2506 error = gw_kcgi_error(kerr);
2507 return error;
2510 static const struct got_error *
2511 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2513 const struct got_error *error = NULL;
2514 enum kcgi_err kerr = KCGI_OK;
2516 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2517 KATTR_ID, "header_age_title", KATTR__MAX);
2518 if (kerr != KCGI_OK)
2519 goto done;
2520 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2521 if (kerr != KCGI_OK)
2522 goto done;
2523 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2524 if (kerr != KCGI_OK)
2525 goto done;
2526 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2527 KATTR_ID, "header_age", KATTR__MAX);
2528 if (kerr != KCGI_OK)
2529 goto done;
2530 kerr = khtml_puts(gw_trans->gw_html_req, str);
2531 if (kerr != KCGI_OK)
2532 goto done;
2533 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2534 done:
2535 if (error == NULL && kerr != KCGI_OK)
2536 error = gw_kcgi_error(kerr);
2537 return error;
2540 static const struct got_error *
2541 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2543 const struct got_error *error = NULL;
2544 enum kcgi_err kerr = KCGI_OK;
2546 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2547 KATTR_ID, "header_author_title", KATTR__MAX);
2548 if (kerr != KCGI_OK)
2549 goto done;
2550 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2551 if (kerr != KCGI_OK)
2552 goto done;
2553 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2554 if (kerr != KCGI_OK)
2555 goto done;
2556 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2557 KATTR_ID, "header_author", KATTR__MAX);
2558 if (kerr != KCGI_OK)
2559 goto done;
2560 kerr = khtml_puts(gw_trans->gw_html_req, str);
2561 if (kerr != KCGI_OK)
2562 goto done;
2563 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2564 done:
2565 if (error == NULL && kerr != KCGI_OK)
2566 error = gw_kcgi_error(kerr);
2567 return error;
2570 static const struct got_error *
2571 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2573 const struct got_error *error = NULL;
2574 enum kcgi_err kerr = KCGI_OK;
2576 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2577 KATTR_ID, "header_committer_title", KATTR__MAX);
2578 if (kerr != KCGI_OK)
2579 goto done;
2580 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2581 if (kerr != KCGI_OK)
2582 goto done;
2583 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2584 if (kerr != KCGI_OK)
2585 goto done;
2586 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2587 KATTR_ID, "header_committer", KATTR__MAX);
2588 if (kerr != KCGI_OK)
2589 goto done;
2590 kerr = khtml_puts(gw_trans->gw_html_req, str);
2591 if (kerr != KCGI_OK)
2592 goto done;
2593 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2594 done:
2595 if (error == NULL && kerr != KCGI_OK)
2596 error = gw_kcgi_error(kerr);
2597 return error;
2600 static const struct got_error *
2601 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2603 const struct got_error *error = NULL;
2604 enum kcgi_err kerr = KCGI_OK;
2606 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2607 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2608 if (kerr != KCGI_OK)
2609 goto done;
2610 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2611 if (kerr != KCGI_OK)
2612 goto done;
2613 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2614 if (kerr != KCGI_OK)
2615 goto done;
2616 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2617 KATTR_ID, "header_commit_msg", KATTR__MAX);
2618 if (kerr != KCGI_OK)
2619 goto done;
2620 kerr = khttp_puts(gw_trans->gw_req, str);
2621 if (kerr != KCGI_OK)
2622 goto done;
2623 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2624 done:
2625 if (error == NULL && kerr != KCGI_OK)
2626 error = gw_kcgi_error(kerr);
2627 return error;
2630 static const struct got_error *
2631 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2633 const struct got_error *error = NULL;
2634 enum kcgi_err kerr = KCGI_OK;
2636 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2637 KATTR_ID, "header_tree_title", KATTR__MAX);
2638 if (kerr != KCGI_OK)
2639 goto done;
2640 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2641 if (kerr != KCGI_OK)
2642 goto done;
2643 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2644 if (kerr != KCGI_OK)
2645 goto done;
2646 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2647 KATTR_ID, "header_tree", KATTR__MAX);
2648 if (kerr != KCGI_OK)
2649 goto done;
2650 kerr = khtml_puts(gw_trans->gw_html_req, str);
2651 if (kerr != KCGI_OK)
2652 goto done;
2653 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2654 done:
2655 if (error == NULL && kerr != KCGI_OK)
2656 error = gw_kcgi_error(kerr);
2657 return error;
2660 static const struct got_error *
2661 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2662 char *dir)
2664 const struct got_error *error = NULL;
2665 FILE *f = NULL;
2666 char *d_file = NULL;
2667 unsigned int len;
2668 size_t n;
2670 *description = NULL;
2671 if (gw_trans->gw_conf->got_show_repo_description == 0)
2672 return NULL;
2674 if (asprintf(&d_file, "%s/description", dir) == -1)
2675 return got_error_from_errno("asprintf");
2677 f = fopen(d_file, "re");
2678 if (f == NULL) {
2679 if (errno == ENOENT || errno == EACCES)
2680 return NULL;
2681 error = got_error_from_errno2("fopen", d_file);
2682 goto done;
2685 if (fseek(f, 0, SEEK_END) == -1) {
2686 error = got_ferror(f, GOT_ERR_IO);
2687 goto done;
2689 len = ftell(f);
2690 if (len == -1) {
2691 error = got_ferror(f, GOT_ERR_IO);
2692 goto done;
2694 if (fseek(f, 0, SEEK_SET) == -1) {
2695 error = got_ferror(f, GOT_ERR_IO);
2696 goto done;
2698 *description = calloc(len + 1, sizeof(**description));
2699 if (*description == NULL) {
2700 error = got_error_from_errno("calloc");
2701 goto done;
2704 n = fread(*description, 1, len, f);
2705 if (n == 0 && ferror(f))
2706 error = got_ferror(f, GOT_ERR_IO);
2707 done:
2708 if (f != NULL && fclose(f) == EOF && error == NULL)
2709 error = got_error_from_errno("fclose");
2710 free(d_file);
2711 return error;
2714 static const struct got_error *
2715 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2717 struct tm tm;
2718 time_t diff_time;
2719 const char *years = "years ago", *months = "months ago";
2720 const char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2721 const char *minutes = "minutes ago", *seconds = "seconds ago";
2722 const char *now = "right now";
2723 const char *s;
2724 char datebuf[29];
2726 *repo_age = NULL;
2728 switch (ref_tm) {
2729 case TM_DIFF:
2730 diff_time = time(NULL) - committer_time;
2731 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2732 if (asprintf(repo_age, "%lld %s",
2733 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2734 return got_error_from_errno("asprintf");
2735 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2736 if (asprintf(repo_age, "%lld %s",
2737 (diff_time / 60 / 60 / 24 / (365 / 12)),
2738 months) == -1)
2739 return got_error_from_errno("asprintf");
2740 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2741 if (asprintf(repo_age, "%lld %s",
2742 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2743 return got_error_from_errno("asprintf");
2744 } else if (diff_time > 60 * 60 * 24 * 2) {
2745 if (asprintf(repo_age, "%lld %s",
2746 (diff_time / 60 / 60 / 24), days) == -1)
2747 return got_error_from_errno("asprintf");
2748 } else if (diff_time > 60 * 60 * 2) {
2749 if (asprintf(repo_age, "%lld %s",
2750 (diff_time / 60 / 60), hours) == -1)
2751 return got_error_from_errno("asprintf");
2752 } else if (diff_time > 60 * 2) {
2753 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2754 minutes) == -1)
2755 return got_error_from_errno("asprintf");
2756 } else if (diff_time > 2) {
2757 if (asprintf(repo_age, "%lld %s", diff_time,
2758 seconds) == -1)
2759 return got_error_from_errno("asprintf");
2760 } else {
2761 if (asprintf(repo_age, "%s", now) == -1)
2762 return got_error_from_errno("asprintf");
2764 break;
2765 case TM_LONG:
2766 if (gmtime_r(&committer_time, &tm) == NULL)
2767 return got_error_from_errno("gmtime_r");
2769 s = asctime_r(&tm, datebuf);
2770 if (s == NULL)
2771 return got_error_from_errno("asctime_r");
2773 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2774 return got_error_from_errno("asprintf");
2775 break;
2777 return NULL;
2780 static const struct got_error *
2781 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2782 const char *refname, int ref_tm)
2784 const struct got_error *error = NULL;
2785 struct got_repository *repo = NULL;
2786 struct got_commit_object *commit = NULL;
2787 struct got_reflist_head refs;
2788 struct got_reflist_entry *re;
2789 time_t committer_time = 0, cmp_time = 0;
2791 *repo_age = NULL;
2792 TAILQ_INIT(&refs);
2794 if (gw_trans->gw_conf->got_show_repo_age == 0)
2795 return NULL;
2797 if (gw_trans->repo)
2798 repo = gw_trans->repo;
2799 else {
2800 error = got_repo_open(&repo, dir, NULL, gw_trans->pack_fds);
2801 if (error)
2802 return error;
2805 error = got_ref_list(&refs, repo, "refs/heads",
2806 got_ref_cmp_by_name, NULL);
2807 if (error)
2808 goto done;
2811 * Find the youngest branch tip in the repository, or the age of
2812 * the a specific branch tip if a name was provided by the caller.
2814 TAILQ_FOREACH(re, &refs, entry) {
2815 struct got_object_id *id = NULL;
2817 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
2818 continue;
2820 error = got_ref_resolve(&id, repo, re->ref);
2821 if (error)
2822 goto done;
2824 error = got_object_open_as_commit(&commit, repo, id);
2825 free(id);
2826 if (error)
2827 goto done;
2829 committer_time =
2830 got_object_commit_get_committer_time(commit);
2831 got_object_commit_close(commit);
2832 if (cmp_time < committer_time)
2833 cmp_time = committer_time;
2835 if (refname)
2836 break;
2839 if (cmp_time != 0) {
2840 committer_time = cmp_time;
2841 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2843 done:
2844 got_ref_list_free(&refs);
2845 if (gw_trans->repo == NULL) {
2846 const struct got_error *close_err = got_repo_close(repo);
2847 if (error == NULL)
2848 error = close_err;
2850 return error;
2853 static const struct got_error *
2854 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2856 const struct got_error *error;
2857 FILE *f = NULL, *f1 = NULL, *f2 = NULL;
2858 int fd1 = -1, fd2 = -1;
2859 struct got_object_id *id1 = NULL, *id2 = NULL;
2860 char *label1 = NULL, *label2 = NULL, *line = NULL;
2861 int obj_type;
2862 size_t linesize = 0;
2863 ssize_t linelen;
2864 enum kcgi_err kerr = KCGI_OK;
2866 f = got_opentemp();
2867 if (f == NULL)
2868 return NULL;
2870 f1 = got_opentemp();
2871 if (f1 == NULL) {
2872 error = got_error_from_errno("got_opentemp");
2873 goto done;
2876 f2 = got_opentemp();
2877 if (f2 == NULL) {
2878 error = got_error_from_errno("got_opentemp");
2879 goto done;
2882 fd1 = got_opentempfd();
2883 if (fd1 == -1) {
2884 error = got_error_from_errno("got_opentempfd");
2885 goto done;
2888 fd2 = got_opentempfd();
2889 if (fd2 == -1) {
2890 error = got_error_from_errno("got_opentempfd");
2891 goto done;
2894 if (header->parent_id != NULL &&
2895 strncmp(header->parent_id, "/dev/null", 9) != 0) {
2896 error = got_repo_match_object_id(&id1, &label1,
2897 header->parent_id, GOT_OBJ_TYPE_ANY,
2898 &header->refs, gw_trans->repo);
2899 if (error)
2900 goto done;
2903 error = got_repo_match_object_id(&id2, &label2,
2904 header->commit_id, GOT_OBJ_TYPE_ANY, &header->refs,
2905 gw_trans->repo);
2906 if (error)
2907 goto done;
2909 error = got_object_get_type(&obj_type, gw_trans->repo, id2);
2910 if (error)
2911 goto done;
2912 switch (obj_type) {
2913 case GOT_OBJ_TYPE_BLOB:
2914 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
2915 fd1, fd2, id1, id2, NULL, NULL, GOT_DIFF_ALGORITHM_PATIENCE,
2916 3, 0, 0, gw_trans->repo, f);
2917 break;
2918 case GOT_OBJ_TYPE_TREE:
2919 error = got_diff_objects_as_trees(NULL, NULL, f1, f2,
2920 fd1, fd2, id1, id2, NULL, "", "",
2921 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, gw_trans->repo, f);
2922 break;
2923 case GOT_OBJ_TYPE_COMMIT:
2924 error = got_diff_objects_as_commits(NULL, NULL, f1, f2,
2925 fd1, fd2, id1, id2, NULL, GOT_DIFF_ALGORITHM_PATIENCE,
2926 3, 0, 0, gw_trans->repo, f);
2927 break;
2928 default:
2929 error = got_error(GOT_ERR_OBJ_TYPE);
2931 if (error)
2932 goto done;
2934 if (fseek(f, 0, SEEK_SET) == -1) {
2935 error = got_ferror(f, GOT_ERR_IO);
2936 goto done;
2939 while ((linelen = getline(&line, &linesize, f)) != -1) {
2940 error = gw_colordiff_line(gw_trans, line);
2941 if (error)
2942 goto done;
2943 /* XXX: KHTML_PRETTY breaks this */
2944 kerr = khtml_puts(gw_trans->gw_html_req, line);
2945 if (kerr != KCGI_OK)
2946 goto done;
2947 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2948 if (kerr != KCGI_OK)
2949 goto done;
2951 if (linelen == -1 && ferror(f))
2952 error = got_error_from_errno("getline");
2953 done:
2954 if (f && fclose(f) == EOF && error == NULL)
2955 error = got_error_from_errno("fclose");
2956 if (f1 && fclose(f1) == EOF && error == NULL)
2957 error = got_error_from_errno("fclose");
2958 if (f2 && fclose(f2) == EOF && error == NULL)
2959 error = got_error_from_errno("fclose");
2960 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
2961 error = got_error_from_errno("close");
2962 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
2963 error = got_error_from_errno("close");
2964 free(line);
2965 free(label1);
2966 free(label2);
2967 free(id1);
2968 free(id2);
2970 if (error == NULL && kerr != KCGI_OK)
2971 error = gw_kcgi_error(kerr);
2972 return error;
2975 static const struct got_error *
2976 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2978 const struct got_error *error = NULL, *close_err;
2979 struct got_repository *repo;
2980 const char *gitconfig_owner;
2982 *owner = NULL;
2984 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2985 return NULL;
2987 error = got_repo_open(&repo, dir, NULL, gw_trans->pack_fds);
2988 if (error)
2989 return error;
2991 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2992 if (gitconfig_owner) {
2993 *owner = strdup(gitconfig_owner);
2994 if (*owner == NULL)
2995 error = got_error_from_errno("strdup");
2997 close_err = got_repo_close(repo);
2998 if (error == NULL)
2999 error = close_err;
3000 return error;
3003 static const struct got_error *
3004 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
3006 const struct got_error *error = NULL;
3007 FILE *f;
3008 char *d_file = NULL;
3009 unsigned int len;
3010 size_t n;
3012 *url = NULL;
3014 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
3015 return got_error_from_errno("asprintf");
3017 f = fopen(d_file, "re");
3018 if (f == NULL) {
3019 if (errno != ENOENT && errno != EACCES)
3020 error = got_error_from_errno2("fopen", d_file);
3021 goto done;
3024 if (fseek(f, 0, SEEK_END) == -1) {
3025 error = got_ferror(f, GOT_ERR_IO);
3026 goto done;
3028 len = ftell(f);
3029 if (len == -1) {
3030 error = got_ferror(f, GOT_ERR_IO);
3031 goto done;
3033 if (fseek(f, 0, SEEK_SET) == -1) {
3034 error = got_ferror(f, GOT_ERR_IO);
3035 goto done;
3038 *url = calloc(len + 1, sizeof(**url));
3039 if (*url == NULL) {
3040 error = got_error_from_errno("calloc");
3041 goto done;
3044 n = fread(*url, 1, len, f);
3045 if (n == 0 && ferror(f))
3046 error = got_ferror(f, GOT_ERR_IO);
3047 done:
3048 if (f && fclose(f) == EOF && error == NULL)
3049 error = got_error_from_errno("fclose");
3050 free(d_file);
3051 return NULL;
3054 static const struct got_error *
3055 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
3056 int limit, int tag_type)
3058 const struct got_error *error = NULL;
3059 struct got_reflist_head refs;
3060 struct got_reflist_entry *re;
3061 char *age = NULL;
3062 char *id_str = NULL, *newline, *href_commits = NULL;
3063 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
3064 struct got_tag_object *tag = NULL;
3065 enum kcgi_err kerr = KCGI_OK;
3066 int summary_header_displayed = 0, chk_next = 0;
3067 int tag_count = 0, commit_found = 0, c_cnt = 0;
3069 TAILQ_INIT(&refs);
3071 error = got_ref_list(&refs, gw_trans->repo, "refs/tags",
3072 got_ref_cmp_tags, gw_trans->repo);
3073 if (error)
3074 goto done;
3076 TAILQ_FOREACH(re, &refs, entry) {
3077 const char *refname;
3078 const char *tagger;
3079 const char *tag_commit;
3080 time_t tagger_time;
3081 struct got_object_id *id;
3082 struct got_commit_object *commit = NULL;
3084 refname = got_ref_get_name(re->ref);
3085 if (strncmp(refname, "refs/tags/", 10) != 0)
3086 continue;
3087 refname += 10;
3089 error = got_ref_resolve(&id, gw_trans->repo, re->ref);
3090 if (error)
3091 goto done;
3093 error = got_object_open_as_tag(&tag, gw_trans->repo, id);
3094 if (error) {
3095 if (error->code != GOT_ERR_OBJ_TYPE) {
3096 free(id);
3097 goto done;
3099 /* "lightweight" tag */
3100 error = got_object_open_as_commit(&commit,
3101 gw_trans->repo, id);
3102 if (error) {
3103 free(id);
3104 goto done;
3106 tagger = got_object_commit_get_committer(commit);
3107 tagger_time =
3108 got_object_commit_get_committer_time(commit);
3109 error = got_object_id_str(&id_str, id);
3110 free(id);
3111 } else {
3112 free(id);
3113 tagger = got_object_tag_get_tagger(tag);
3114 tagger_time = got_object_tag_get_tagger_time(tag);
3115 error = got_object_id_str(&id_str,
3116 got_object_tag_get_object_id(tag));
3118 if (error)
3119 goto done;
3121 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
3122 strlen(id_str)) != 0)
3123 continue;
3125 if (tag_type == TAGBRIEF && gw_trans->commit_id &&
3126 commit_found == 0 && strncmp(id_str, gw_trans->commit_id,
3127 strlen(id_str)) != 0)
3128 continue;
3129 else
3130 commit_found = 1;
3132 tag_count++;
3134 if (chk_next) {
3135 gw_trans->next_id = strdup(id_str);
3136 if (gw_trans->next_id == NULL)
3137 error = got_error_from_errno("strdup");
3138 goto prev;
3141 if (commit) {
3142 error = got_object_commit_get_logmsg(&tag_commit0,
3143 commit);
3144 if (error)
3145 goto done;
3146 got_object_commit_close(commit);
3147 } else {
3148 tag_commit0 = strdup(got_object_tag_get_message(tag));
3149 if (tag_commit0 == NULL) {
3150 error = got_error_from_errno("strdup");
3151 goto done;
3155 tag_commit = tag_commit0;
3156 while (*tag_commit == '\n')
3157 tag_commit++;
3159 switch (tag_type) {
3160 case TAGBRIEF:
3161 newline = strchr(tag_commit, '\n');
3162 if (newline)
3163 *newline = '\0';
3165 if (summary_header_displayed == 0) {
3166 kerr = khtml_attr(gw_trans->gw_html_req,
3167 KELEM_DIV, KATTR_ID,
3168 "summary_tags_title_wrapper", KATTR__MAX);
3169 if (kerr != KCGI_OK)
3170 goto done;
3171 kerr = khtml_attr(gw_trans->gw_html_req,
3172 KELEM_DIV, KATTR_ID,
3173 "summary_tags_title", KATTR__MAX);
3174 if (kerr != KCGI_OK)
3175 goto done;
3176 kerr = khtml_puts(gw_trans->gw_html_req,
3177 "Tags");
3178 if (kerr != KCGI_OK)
3179 goto done;
3180 kerr = khtml_closeelem(gw_trans->gw_html_req,
3181 2);
3182 if (kerr != KCGI_OK)
3183 goto done;
3184 kerr = khtml_attr(gw_trans->gw_html_req,
3185 KELEM_DIV, KATTR_ID,
3186 "summary_tags_content", KATTR__MAX);
3187 if (kerr != KCGI_OK)
3188 goto done;
3189 summary_header_displayed = 1;
3192 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3193 KATTR_ID, "tag_wrapper", KATTR__MAX);
3194 if (kerr != KCGI_OK)
3195 goto done;
3196 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3197 KATTR_ID, "tag_age", KATTR__MAX);
3198 if (kerr != KCGI_OK)
3199 goto done;
3200 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
3201 if (error)
3202 goto done;
3203 kerr = khtml_puts(gw_trans->gw_html_req,
3204 age ? age : "");
3205 if (kerr != KCGI_OK)
3206 goto done;
3207 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3208 if (kerr != KCGI_OK)
3209 goto done;
3210 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3211 KATTR_ID, "tag", KATTR__MAX);
3212 if (kerr != KCGI_OK)
3213 goto done;
3214 kerr = khtml_puts(gw_trans->gw_html_req, refname);
3215 if (kerr != KCGI_OK)
3216 goto done;
3217 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3218 if (kerr != KCGI_OK)
3219 goto done;
3220 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3221 KATTR_ID, "tag_name", KATTR__MAX);
3222 if (kerr != KCGI_OK)
3223 goto done;
3225 href_tag = khttp_urlpart(NULL, NULL, "gotweb", "path",
3226 gw_trans->repo_name, "action", "tag", "commit",
3227 id_str, NULL);
3228 if (href_tag == NULL) {
3229 error = got_error_from_errno("khttp_urlpart");
3230 goto done;
3232 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3233 KATTR_HREF, href_tag, KATTR__MAX);
3234 if (kerr != KCGI_OK)
3235 goto done;
3236 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
3237 if (kerr != KCGI_OK)
3238 goto done;
3239 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3240 if (kerr != KCGI_OK)
3241 goto done;
3243 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3244 KATTR_ID, "navs_wrapper", KATTR__MAX);
3245 if (kerr != KCGI_OK)
3246 goto done;
3247 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3248 KATTR_ID, "navs", KATTR__MAX);
3249 if (kerr != KCGI_OK)
3250 goto done;
3252 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3253 KATTR_HREF, href_tag, KATTR__MAX);
3254 if (kerr != KCGI_OK)
3255 goto done;
3256 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
3257 if (kerr != KCGI_OK)
3258 goto done;
3259 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3260 if (kerr != KCGI_OK)
3261 goto done;
3263 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3264 if (kerr != KCGI_OK)
3265 goto done;
3267 href_briefs = khttp_urlpart(NULL, NULL, "gotweb",
3268 "path", gw_trans->repo_name, "action", "briefs",
3269 "commit", id_str, NULL);
3270 if (href_briefs == NULL) {
3271 error = got_error_from_errno("khttp_urlpart");
3272 goto done;
3274 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3275 KATTR_HREF, href_briefs, KATTR__MAX);
3276 if (kerr != KCGI_OK)
3277 goto done;
3278 kerr = khtml_puts(gw_trans->gw_html_req,
3279 "commit briefs");
3280 if (kerr != KCGI_OK)
3281 goto done;
3282 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3283 if (kerr != KCGI_OK)
3284 goto done;
3286 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3287 if (kerr != KCGI_OK)
3288 goto done;
3290 href_commits = khttp_urlpart(NULL, NULL, "gotweb",
3291 "path", gw_trans->repo_name, "action", "commits",
3292 "commit", id_str, NULL);
3293 if (href_commits == NULL) {
3294 error = got_error_from_errno("khttp_urlpart");
3295 goto done;
3297 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3298 KATTR_HREF, href_commits, KATTR__MAX);
3299 if (kerr != KCGI_OK)
3300 goto done;
3301 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
3302 if (kerr != KCGI_OK)
3303 goto done;
3304 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3305 if (kerr != KCGI_OK)
3306 goto done;
3308 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3309 KATTR_ID, "dotted_line", KATTR__MAX);
3310 if (kerr != KCGI_OK)
3311 goto done;
3312 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3313 if (kerr != KCGI_OK)
3314 goto done;
3315 break;
3316 case TAGFULL:
3317 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3318 KATTR_ID, "tag_info_date_title", KATTR__MAX);
3319 if (kerr != KCGI_OK)
3320 goto done;
3321 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
3322 if (kerr != KCGI_OK)
3323 goto done;
3324 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3325 if (kerr != KCGI_OK)
3326 goto done;
3327 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3328 KATTR_ID, "tag_info_date", KATTR__MAX);
3329 if (kerr != KCGI_OK)
3330 goto done;
3331 error = gw_get_time_str(&age, tagger_time, TM_LONG);
3332 if (error)
3333 goto done;
3334 kerr = khtml_puts(gw_trans->gw_html_req,
3335 age ? age : "");
3336 if (kerr != KCGI_OK)
3337 goto done;
3338 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3339 if (kerr != KCGI_OK)
3340 goto done;
3342 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3343 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
3344 if (kerr != KCGI_OK)
3345 goto done;
3346 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
3347 if (kerr != KCGI_OK)
3348 goto done;
3349 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3350 if (kerr != KCGI_OK)
3351 goto done;
3352 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3353 KATTR_ID, "tag_info_date", KATTR__MAX);
3354 if (kerr != KCGI_OK)
3355 goto done;
3356 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
3357 if (kerr != KCGI_OK)
3358 goto done;
3359 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3360 if (kerr != KCGI_OK)
3361 goto done;
3363 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3364 KATTR_ID, "tag_info", KATTR__MAX);
3365 if (kerr != KCGI_OK)
3366 goto done;
3367 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
3368 if (kerr != KCGI_OK)
3369 goto done;
3370 break;
3371 default:
3372 break;
3374 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3375 if (kerr != KCGI_OK)
3376 goto done;
3378 if (limit && --limit == 0)
3379 chk_next = 1;
3381 if (tag)
3382 got_object_tag_close(tag);
3383 tag = NULL;
3384 free(id_str);
3385 id_str = NULL;
3386 free(age);
3387 age = NULL;
3388 free(tag_commit0);
3389 tag_commit0 = NULL;
3390 free(href_tag);
3391 href_tag = NULL;
3392 free(href_briefs);
3393 href_briefs = NULL;
3394 free(href_commits);
3395 href_commits = NULL;
3397 if (tag_count == 0) {
3398 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3399 "summary_tags_title_wrapper", KATTR__MAX);
3400 if (kerr != KCGI_OK)
3401 goto done;
3402 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3403 "summary_tags_title", KATTR__MAX);
3404 if (kerr != KCGI_OK)
3405 goto done;
3406 kerr = khtml_puts(gw_trans->gw_html_req, "Tags");
3407 if (kerr != KCGI_OK)
3408 goto done;
3409 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3410 if (kerr != KCGI_OK)
3411 goto done;
3412 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3413 "summary_tags_content", KATTR__MAX);
3414 if (kerr != KCGI_OK)
3415 goto done;
3416 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3417 "tags_info", KATTR__MAX);
3418 if (kerr != KCGI_OK)
3419 goto done;
3420 kerr = khttp_puts(gw_trans->gw_req,
3421 "There are no tags for this repo.");
3422 if (kerr != KCGI_OK)
3423 goto done;
3424 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3425 goto done;
3427 prev:
3428 commit_found = 0;
3429 TAILQ_FOREACH_REVERSE(re, &refs, got_reflist_head, entry) {
3430 const char *refname;
3431 struct got_object_id *id;
3432 struct got_commit_object *commit = NULL;
3434 refname = got_ref_get_name(re->ref);
3435 if (strncmp(refname, "refs/tags/", 10) != 0)
3436 continue;
3437 refname += 10;
3439 error = got_ref_resolve(&id, gw_trans->repo, re->ref);
3440 if (error)
3441 goto done;
3443 error = got_object_open_as_tag(&tag, gw_trans->repo, id);
3444 if (error) {
3445 if (error->code != GOT_ERR_OBJ_TYPE) {
3446 free(id);
3447 goto done;
3449 /* "lightweight" tag */
3450 error = got_object_open_as_commit(&commit,
3451 gw_trans->repo, id);
3452 if (error) {
3453 free(id);
3454 goto done;
3456 error = got_object_id_str(&id_str, id);
3457 free(id);
3458 } else {
3459 free(id);
3460 error = got_object_id_str(&id_str,
3461 got_object_tag_get_object_id(tag));
3463 if (error)
3464 goto done;
3466 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
3467 strlen(id_str)) != 0)
3468 continue;
3470 if (commit_found == 0 && tag_type == TAGBRIEF &&
3471 gw_trans->commit_id != NULL &&
3472 strncmp(id_str, gw_trans->commit_id, strlen(id_str)) != 0)
3473 continue;
3474 else
3475 commit_found = 1;
3477 if (gw_trans->commit_id != NULL &&
3478 strcmp(id_str, gw_trans->commit_id) != 0 &&
3479 (re == TAILQ_FIRST(&refs) ||
3480 c_cnt == gw_trans->gw_conf->got_max_commits_display)) {
3481 gw_trans->prev_id = strdup(id_str);
3482 if (gw_trans->prev_id == NULL) {
3483 error = got_error_from_errno("strdup");
3484 goto done;
3486 break;
3488 c_cnt++;
3490 done:
3491 if (tag)
3492 got_object_tag_close(tag);
3493 free(id_str);
3494 free(age);
3495 free(tag_commit0);
3496 free(href_tag);
3497 free(href_briefs);
3498 free(href_commits);
3499 got_ref_list_free(&refs);
3500 if (error == NULL && kerr != KCGI_OK)
3501 error = gw_kcgi_error(kerr);
3502 return error;
3505 static void
3506 gw_free_header(struct gw_header *header)
3508 free(header->path);
3509 free(header->author);
3510 free(header->committer);
3511 free(header->refs_str);
3512 free(header->commit_id);
3513 free(header->parent_id);
3514 free(header->tree_id);
3515 free(header->commit_msg);
3518 static struct gw_header *
3519 gw_init_header()
3521 struct gw_header *header;
3523 header = malloc(sizeof(*header));
3524 if (header == NULL)
3525 return NULL;
3527 header->path = NULL;
3528 TAILQ_INIT(&header->refs);
3530 header->refs_str = NULL;
3531 header->commit_id = NULL;
3532 header->committer = NULL;
3533 header->author = NULL;
3534 header->parent_id = NULL;
3535 header->tree_id = NULL;
3536 header->commit_msg = NULL;
3538 return header;
3541 static const struct got_error *
3542 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
3543 int limit, struct got_object_id *id)
3545 const struct got_error *error = NULL;
3546 struct got_commit_graph *graph = NULL;
3547 struct got_commit_object *commit = NULL;
3548 int chk_next = 0, chk_multi = 0, c_cnt = 0, commit_found = 0;
3549 struct gw_header *t_header = NULL;
3551 error = got_commit_graph_open(&graph, header->path, 0);
3552 if (error)
3553 return error;
3555 error = got_commit_graph_iter_start(graph, id, gw_trans->repo, NULL,
3556 NULL);
3557 if (error)
3558 goto err;
3560 for (;;) {
3561 error = got_commit_graph_iter_next(&id, graph, gw_trans->repo,
3562 NULL, NULL);
3563 if (error) {
3564 if (error->code == GOT_ERR_ITER_COMPLETED)
3565 error = NULL;
3566 goto done;
3568 if (id == NULL)
3569 goto err;
3571 error = got_object_open_as_commit(&commit, gw_trans->repo, id);
3572 if (error)
3573 goto err;
3574 if (limit == 1 && chk_multi == 0 &&
3575 gw_trans->gw_conf->got_max_commits_display != 1) {
3576 error = gw_get_commit(gw_trans, header, commit, id);
3577 if (error)
3578 goto err;
3579 commit_found = 1;
3580 } else {
3581 chk_multi = 1;
3582 struct gw_header *n_header = NULL;
3583 if ((n_header = gw_init_header()) == NULL) {
3584 error = got_error_from_errno("malloc");
3585 goto err;
3587 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3588 entry);
3589 error = got_ref_list(&n_header->refs, gw_trans->repo,
3590 NULL, got_ref_cmp_by_name, NULL);
3591 if (error)
3592 goto err;
3594 error = gw_get_commit(gw_trans, n_header, commit, id);
3595 if (error)
3596 goto err;
3597 got_ref_list_free(&n_header->refs);
3599 if (gw_trans->commit_id != NULL) {
3600 if (strcmp(gw_trans->commit_id,
3601 n_header->commit_id) == 0)
3602 commit_found = 1;
3603 } else
3604 commit_found = 1;
3607 * check for one more commit before breaking,
3608 * so we know whether to navigate through gw_briefs
3609 * gw_commits and gw_summary
3611 if (chk_next && (gw_trans->action == GW_BRIEFS ||
3612 gw_trans->action == GW_COMMITS ||
3613 gw_trans->action == GW_SUMMARY)) {
3614 gw_trans->next_id = strdup(n_header->commit_id);
3615 if (gw_trans->next_id == NULL)
3616 error = got_error_from_errno("strdup");
3617 TAILQ_REMOVE(&gw_trans->gw_headers, n_header,
3618 entry);
3619 goto done;
3623 if (commit_found == 1 && (error || (limit && --limit == 0))) {
3624 if (chk_multi == 0)
3625 break;
3626 chk_next = 1;
3629 done:
3630 if (gw_trans->prev_id == NULL && gw_trans->commit_id != NULL &&
3631 (gw_trans->action == GW_BRIEFS || gw_trans->action == GW_COMMITS)) {
3632 commit_found = 0;
3633 TAILQ_FOREACH_REVERSE(t_header, &gw_trans->gw_headers,
3634 headers, entry) {
3635 if (commit_found == 0 &&
3636 strcmp(gw_trans->commit_id,
3637 t_header->commit_id) != 0)
3638 continue;
3639 else
3640 commit_found = 1;
3641 if (gw_trans->commit_id != NULL &&
3642 strcmp(gw_trans->commit_id,
3643 t_header->commit_id) != 0 &&
3644 (c_cnt == gw_trans->gw_conf->got_max_commits_display
3645 || t_header ==
3646 TAILQ_FIRST(&gw_trans->gw_headers))) {
3647 gw_trans->prev_id = strdup(t_header->commit_id);
3648 if (gw_trans->prev_id == NULL)
3649 error = got_error_from_errno("strdup");
3650 break;
3652 c_cnt++;
3655 err:
3656 if (commit != NULL)
3657 got_object_commit_close(commit);
3658 if (graph)
3659 got_commit_graph_close(graph);
3660 return error;
3663 static const struct got_error *
3664 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header,
3665 struct got_commit_object *commit, struct got_object_id *id)
3667 const struct got_error *error = NULL;
3668 struct got_reflist_entry *re;
3669 struct got_object_id *id2 = NULL;
3670 struct got_object_qid *parent_id;
3671 char *commit_msg = NULL, *commit_msg0;
3673 /*print commit*/
3674 TAILQ_FOREACH(re, &header->refs, entry) {
3675 char *s;
3676 const char *name;
3677 struct got_tag_object *tag = NULL;
3678 struct got_object_id *ref_id;
3679 int cmp;
3681 if (got_ref_is_symbolic(re->ref))
3682 continue;
3684 name = got_ref_get_name(re->ref);
3685 if (strncmp(name, "refs/", 5) == 0)
3686 name += 5;
3687 if (strncmp(name, "got/", 4) == 0)
3688 continue;
3689 if (strncmp(name, "heads/", 6) == 0)
3690 name += 6;
3691 if (strncmp(name, "remotes/", 8) == 0) {
3692 name += 8;
3693 s = strstr(name, "/" GOT_REF_HEAD);
3694 if (s != NULL && s[strlen(s)] == '\0')
3695 continue;
3697 error = got_ref_resolve(&ref_id, gw_trans->repo, re->ref);
3698 if (error)
3699 return error;
3700 if (strncmp(name, "tags/", 5) == 0) {
3701 error = got_object_open_as_tag(&tag, gw_trans->repo,
3702 ref_id);
3703 if (error) {
3704 if (error->code != GOT_ERR_OBJ_TYPE) {
3705 free(ref_id);
3706 continue;
3709 * Ref points at something other
3710 * than a tag.
3712 error = NULL;
3713 tag = NULL;
3716 cmp = got_object_id_cmp(tag ?
3717 got_object_tag_get_object_id(tag) : ref_id, id);
3718 free(ref_id);
3719 if (tag)
3720 got_object_tag_close(tag);
3721 if (cmp != 0)
3722 continue;
3723 s = header->refs_str;
3724 if (asprintf(&header->refs_str, "%s%s%s", s ? s : "",
3725 s ? ", " : "", name) == -1) {
3726 error = got_error_from_errno("asprintf");
3727 free(s);
3728 header->refs_str = NULL;
3729 return error;
3731 free(s);
3734 error = got_object_id_str(&header->commit_id, id);
3735 if (error)
3736 return error;
3738 error = got_object_id_str(&header->tree_id,
3739 got_object_commit_get_tree_id(commit));
3740 if (error)
3741 return error;
3743 if (gw_trans->action == GW_DIFF) {
3744 parent_id = STAILQ_FIRST(
3745 got_object_commit_get_parent_ids(commit));
3746 if (parent_id != NULL) {
3747 id2 = got_object_id_dup(&parent_id->id);
3748 free (parent_id);
3749 error = got_object_id_str(&header->parent_id, id2);
3750 if (error)
3751 return error;
3752 free(id2);
3753 } else {
3754 header->parent_id = strdup("/dev/null");
3755 if (header->parent_id == NULL) {
3756 error = got_error_from_errno("strdup");
3757 return error;
3762 header->committer_time =
3763 got_object_commit_get_committer_time(commit);
3765 header->author =
3766 strdup(got_object_commit_get_author(commit));
3767 if (header->author == NULL) {
3768 error = got_error_from_errno("strdup");
3769 return error;
3771 header->committer =
3772 strdup(got_object_commit_get_committer(commit));
3773 if (header->committer == NULL) {
3774 error = got_error_from_errno("strdup");
3775 return error;
3777 error = got_object_commit_get_logmsg(&commit_msg0, commit);
3778 if (error)
3779 return error;
3781 commit_msg = commit_msg0;
3782 while (*commit_msg == '\n')
3783 commit_msg++;
3785 header->commit_msg = strdup(commit_msg);
3786 if (header->commit_msg == NULL)
3787 error = got_error_from_errno("strdup");
3788 free(commit_msg0);
3789 return error;
3792 static const struct got_error *
3793 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3795 const struct got_error *error = NULL;
3796 char *in_repo_path = NULL;
3797 struct got_object_id *id = NULL;
3798 struct got_reference *ref;
3800 error = got_repo_open(&gw_trans->repo, gw_trans->repo_path, NULL,
3801 gw_trans->pack_fds);
3802 if (error)
3803 return error;
3805 if (gw_trans->commit_id == NULL || gw_trans->action == GW_COMMITS ||
3806 gw_trans->action == GW_BRIEFS || gw_trans->action == GW_SUMMARY ||
3807 gw_trans->action == GW_TAGS) {
3808 error = got_ref_open(&ref, gw_trans->repo,
3809 gw_trans->headref, 0);
3810 if (error)
3811 return error;
3813 error = got_ref_resolve(&id, gw_trans->repo, ref);
3814 got_ref_close(ref);
3815 if (error)
3816 return error;
3817 } else {
3818 error = got_ref_open(&ref, gw_trans->repo,
3819 gw_trans->commit_id, 0);
3820 if (error == NULL) {
3821 int obj_type;
3822 error = got_ref_resolve(&id, gw_trans->repo, ref);
3823 got_ref_close(ref);
3824 if (error)
3825 return error;
3826 error = got_object_get_type(&obj_type, gw_trans->repo,
3827 id);
3828 if (error)
3829 goto done;
3830 if (obj_type == GOT_OBJ_TYPE_TAG) {
3831 struct got_tag_object *tag;
3832 error = got_object_open_as_tag(&tag,
3833 gw_trans->repo, id);
3834 if (error)
3835 goto done;
3836 if (got_object_tag_get_object_type(tag) !=
3837 GOT_OBJ_TYPE_COMMIT) {
3838 got_object_tag_close(tag);
3839 error = got_error(GOT_ERR_OBJ_TYPE);
3840 goto done;
3842 free(id);
3843 id = got_object_id_dup(
3844 got_object_tag_get_object_id(tag));
3845 if (id == NULL)
3846 error = got_error_from_errno(
3847 "got_object_id_dup");
3848 got_object_tag_close(tag);
3849 if (error)
3850 goto done;
3851 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3852 error = got_error(GOT_ERR_OBJ_TYPE);
3853 goto done;
3856 error = got_repo_match_object_id_prefix(&id,
3857 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT,
3858 gw_trans->repo);
3859 if (error)
3860 goto done;
3863 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
3864 gw_trans->repo_path);
3865 if (error)
3866 goto done;
3868 if (in_repo_path) {
3869 header->path = strdup(in_repo_path);
3870 if (header->path == NULL) {
3871 error = got_error_from_errno("strdup");
3872 goto done;
3876 error = got_ref_list(&header->refs, gw_trans->repo, NULL,
3877 got_ref_cmp_by_name, NULL);
3878 if (error)
3879 goto done;
3881 error = gw_get_commits(gw_trans, header, limit, id);
3882 done:
3883 free(id);
3884 free(in_repo_path);
3885 return error;
3888 struct blame_line {
3889 int annotated;
3890 char *id_str;
3891 char *committer;
3892 char datebuf[11]; /* YYYY-MM-DD + NUL */
3895 struct gw_blame_cb_args {
3896 struct blame_line *lines;
3897 int nlines;
3898 int nlines_prec;
3899 int lineno_cur;
3900 off_t *line_offsets;
3901 FILE *f;
3902 struct got_repository *repo;
3903 struct gw_trans *gw_trans;
3906 static const struct got_error *
3907 gw_blame_cb(void *arg, int nlines, int lineno,
3908 struct got_commit_object *commit, struct got_object_id *id)
3910 const struct got_error *err = NULL;
3911 struct gw_blame_cb_args *a = arg;
3912 struct blame_line *bline;
3913 char *line = NULL;
3914 size_t linesize = 0;
3915 off_t offset;
3916 struct tm tm;
3917 time_t committer_time;
3918 enum kcgi_err kerr = KCGI_OK;
3920 if (nlines != a->nlines ||
3921 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3922 return got_error(GOT_ERR_RANGE);
3924 if (lineno == -1)
3925 return NULL; /* no change in this commit */
3927 /* Annotate this line. */
3928 bline = &a->lines[lineno - 1];
3929 if (bline->annotated)
3930 return NULL;
3931 err = got_object_id_str(&bline->id_str, id);
3932 if (err)
3933 return err;
3935 bline->committer = strdup(got_object_commit_get_committer(commit));
3936 if (bline->committer == NULL) {
3937 err = got_error_from_errno("strdup");
3938 goto done;
3941 committer_time = got_object_commit_get_committer_time(commit);
3942 if (gmtime_r(&committer_time, &tm) == NULL)
3943 return got_error_from_errno("gmtime_r");
3944 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3945 &tm) == 0) {
3946 err = got_error(GOT_ERR_NO_SPACE);
3947 goto done;
3949 bline->annotated = 1;
3951 /* Print lines annotated so far. */
3952 bline = &a->lines[a->lineno_cur - 1];
3953 if (!bline->annotated)
3954 goto done;
3956 offset = a->line_offsets[a->lineno_cur - 1];
3957 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3958 err = got_error_from_errno("fseeko");
3959 goto done;
3962 while (bline->annotated) {
3963 char *smallerthan, *at, *nl, *committer;
3964 char *href_diff = NULL;
3965 size_t len;
3967 if (getline(&line, &linesize, a->f) == -1) {
3968 if (ferror(a->f))
3969 err = got_error_from_errno("getline");
3970 break;
3973 committer = bline->committer;
3974 smallerthan = strchr(committer, '<');
3975 if (smallerthan && smallerthan[1] != '\0')
3976 committer = smallerthan + 1;
3977 at = strchr(committer, '@');
3978 if (at)
3979 *at = '\0';
3980 len = strlen(committer);
3981 if (len >= 9)
3982 committer[8] = '\0';
3984 nl = strchr(line, '\n');
3985 if (nl)
3986 *nl = '\0';
3988 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3989 "blame_wrapper", KATTR__MAX);
3990 if (kerr != KCGI_OK)
3991 goto err;
3992 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3993 "blame_number", KATTR__MAX);
3994 if (kerr != KCGI_OK)
3995 goto err;
3996 kerr = khtml_printf(a->gw_trans->gw_html_req, "%.*d",
3997 a->nlines_prec, a->lineno_cur);
3998 if (kerr != KCGI_OK)
3999 goto err;
4000 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
4001 if (kerr != KCGI_OK)
4002 goto err;
4004 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4005 "blame_hash", KATTR__MAX);
4006 if (kerr != KCGI_OK)
4007 goto err;
4009 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
4010 a->gw_trans->repo_name, "action", "diff", "commit",
4011 bline->id_str, NULL);
4012 if (href_diff == NULL) {
4013 err = got_error_from_errno("khttp_urlpart");
4014 goto done;
4016 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
4017 KATTR_HREF, href_diff, KATTR__MAX);
4018 if (kerr != KCGI_OK)
4019 goto done;
4020 kerr = khtml_printf(a->gw_trans->gw_html_req, "%.8s",
4021 bline->id_str);
4022 if (kerr != KCGI_OK)
4023 goto err;
4024 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
4025 if (kerr != KCGI_OK)
4026 goto err;
4028 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4029 "blame_date", KATTR__MAX);
4030 if (kerr != KCGI_OK)
4031 goto err;
4032 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
4033 if (kerr != KCGI_OK)
4034 goto err;
4035 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
4036 if (kerr != KCGI_OK)
4037 goto err;
4039 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4040 "blame_author", KATTR__MAX);
4041 if (kerr != KCGI_OK)
4042 goto err;
4043 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
4044 if (kerr != KCGI_OK)
4045 goto err;
4046 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
4047 if (kerr != KCGI_OK)
4048 goto err;
4050 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4051 "blame_code", KATTR__MAX);
4052 if (kerr != KCGI_OK)
4053 goto err;
4054 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
4055 if (kerr != KCGI_OK)
4056 goto err;
4057 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
4058 if (kerr != KCGI_OK)
4059 goto err;
4061 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
4062 if (kerr != KCGI_OK)
4063 goto err;
4065 a->lineno_cur++;
4066 bline = &a->lines[a->lineno_cur - 1];
4067 err:
4068 free(href_diff);
4070 done:
4071 free(line);
4072 if (err == NULL && kerr != KCGI_OK)
4073 err = gw_kcgi_error(kerr);
4074 return err;
4077 static const struct got_error *
4078 gw_output_file_blame(struct gw_trans *gw_trans, struct gw_header *header)
4080 const struct got_error *error = NULL;
4081 struct got_object_id *obj_id = NULL;
4082 struct got_object_id *commit_id = NULL;
4083 struct got_commit_object *commit = NULL;
4084 struct got_blob_object *blob = NULL;
4085 char *path = NULL, *in_repo_path = NULL;
4086 struct gw_blame_cb_args bca;
4087 int i, obj_type, fd1 = -1, fd2 = -1, fd3 = -1;
4088 off_t filesize;
4089 FILE *f1 = NULL, *f2 = NULL;
4091 fd1 = got_opentempfd();
4092 if (fd1 == -1)
4093 return got_error_from_errno("got_opentempfd");
4094 fd2 = got_opentempfd();
4095 if (fd2 == -1) {
4096 error = got_error_from_errno("got_opentempfd");
4097 goto done;
4099 fd3 = got_opentempfd();
4100 if (fd3 == -1) {
4101 error = got_error_from_errno("got_opentempfd");
4102 goto done;
4105 memset(&bca, 0, sizeof(bca));
4107 if (asprintf(&path, "%s%s%s",
4108 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4109 gw_trans->repo_folder ? "/" : "",
4110 gw_trans->repo_file) == -1) {
4111 error = got_error_from_errno("asprintf");
4112 goto done;
4115 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path);
4116 if (error)
4117 goto done;
4119 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
4120 GOT_OBJ_TYPE_COMMIT, &header->refs, gw_trans->repo);
4121 if (error)
4122 goto done;
4124 error = got_object_open_as_commit(&commit, gw_trans->repo, commit_id);
4125 if (error)
4126 goto done;
4128 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit,
4129 in_repo_path);
4130 if (error)
4131 goto done;
4133 if (obj_id == NULL) {
4134 error = got_error(GOT_ERR_NO_OBJ);
4135 goto done;
4138 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
4139 if (error)
4140 goto done;
4142 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4143 error = got_error(GOT_ERR_OBJ_TYPE);
4144 goto done;
4147 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192,
4148 fd1);
4149 if (error)
4150 goto done;
4152 bca.f = got_opentemp();
4153 if (bca.f == NULL) {
4154 error = got_error_from_errno("got_opentemp");
4155 goto done;
4157 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4158 &bca.line_offsets, bca.f, blob);
4159 if (error || bca.nlines == 0)
4160 goto done;
4162 /* Don't include \n at EOF in the blame line count. */
4163 if (bca.line_offsets[bca.nlines - 1] == filesize)
4164 bca.nlines--;
4166 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4167 if (bca.lines == NULL) {
4168 error = got_error_from_errno("calloc");
4169 goto done;
4171 bca.lineno_cur = 1;
4172 bca.nlines_prec = 0;
4173 i = bca.nlines;
4174 while (i > 0) {
4175 i /= 10;
4176 bca.nlines_prec++;
4178 bca.repo = gw_trans->repo;
4179 bca.gw_trans = gw_trans;
4181 fd1 = got_opentempfd();
4182 if (fd1 == -1) {
4183 error = got_error_from_errno("got_opentempfd");
4184 goto done;
4187 f1 = got_opentemp();
4188 if (f1 == NULL) {
4189 error = got_error_from_errno("got_opentempfd");
4190 goto done;
4192 f2 = got_opentemp();
4193 if (f2 == NULL) {
4194 error = got_error_from_errno("got_opentempfd");
4195 goto done;
4198 error = got_blame(in_repo_path, commit_id, gw_trans->repo,
4199 GOT_DIFF_ALGORITHM_PATIENCE, gw_blame_cb, &bca, NULL, NULL,
4200 fd2, fd3, f1, f2);
4201 done:
4202 free(in_repo_path);
4203 free(commit_id);
4204 free(obj_id);
4205 free(path);
4207 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
4208 error = got_error_from_errno("close");
4209 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
4210 error = got_error_from_errno("close");
4211 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
4212 error = got_error_from_errno("close");
4213 if (f1 && fclose(f1) == EOF && error == NULL)
4214 error = got_error_from_errno("fclose");
4215 if (f2 && fclose(f2) == EOF && error == NULL)
4216 error = got_error_from_errno("fclose");
4218 if (blob) {
4219 free(bca.line_offsets);
4220 for (i = 0; i < bca.nlines; i++) {
4221 struct blame_line *bline = &bca.lines[i];
4222 free(bline->id_str);
4223 free(bline->committer);
4225 free(bca.lines);
4226 if (bca.f && fclose(bca.f) == EOF && error == NULL)
4227 error = got_error_from_errno("fclose");
4229 if (blob)
4230 got_object_blob_close(blob);
4231 if (commit)
4232 got_object_commit_close(commit);
4233 return error;
4236 static const struct got_error *
4237 gw_output_blob_buf(struct gw_trans *gw_trans, struct gw_header *header)
4239 const struct got_error *error = NULL;
4240 struct got_object_id *obj_id = NULL;
4241 struct got_object_id *commit_id = NULL;
4242 struct got_commit_object *commit = NULL;
4243 struct got_blob_object *blob = NULL;
4244 char *path = NULL, *in_repo_path = NULL;
4245 int obj_type, set_mime = 0, fd = -1;
4246 size_t len, hdrlen;
4247 const uint8_t *buf;
4248 enum kcgi_err kerr = KCGI_OK;
4250 fd = got_opentempfd();
4251 if (fd == -1)
4252 return got_error_from_errno("got_opentempfd");
4254 if (asprintf(&path, "%s%s%s",
4255 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4256 gw_trans->repo_folder ? "/" : "",
4257 gw_trans->repo_file) == -1) {
4258 error = got_error_from_errno("asprintf");
4259 goto done;
4262 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path);
4263 if (error)
4264 goto done;
4266 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
4267 GOT_OBJ_TYPE_COMMIT, &header->refs, gw_trans->repo);
4268 if (error)
4269 goto done;
4271 error = got_object_open_as_commit(&commit, gw_trans->repo, commit_id);
4272 if (error)
4273 goto done;
4275 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit,
4276 in_repo_path);
4277 if (error)
4278 goto done;
4280 if (obj_id == NULL) {
4281 error = got_error(GOT_ERR_NO_OBJ);
4282 goto done;
4285 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
4286 if (error)
4287 goto done;
4289 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4290 error = got_error(GOT_ERR_OBJ_TYPE);
4291 goto done;
4294 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192,
4295 fd);
4296 if (error)
4297 goto done;
4299 hdrlen = got_object_blob_get_hdrlen(blob);
4300 do {
4301 error = got_object_blob_read_block(&len, blob);
4302 if (error)
4303 goto done;
4304 buf = got_object_blob_get_read_buf(blob);
4307 * Skip blob object header first time around,
4308 * which also contains a zero byte.
4310 buf += hdrlen;
4311 if (set_mime == 0) {
4312 if (isbinary(buf, len - hdrlen))
4313 gw_trans->mime = KMIME_APP_OCTET_STREAM;
4314 else
4315 gw_trans->mime = KMIME_TEXT_PLAIN;
4316 set_mime = 1;
4317 error = gw_display_index(gw_trans);
4318 if (error)
4319 goto done;
4321 kerr = khttp_write(gw_trans->gw_req, buf, len - hdrlen);
4322 if (kerr != KCGI_OK)
4323 goto done;
4324 hdrlen = 0;
4325 } while (len != 0);
4326 done:
4327 free(in_repo_path);
4328 free(commit_id);
4329 free(obj_id);
4330 free(path);
4331 if (fd != -1 && close(fd) == -1 && error == NULL)
4332 error = got_error_from_errno("close");
4333 if (blob)
4334 got_object_blob_close(blob);
4335 if (commit)
4336 got_object_commit_close(commit);
4337 if (error == NULL && kerr != KCGI_OK)
4338 error = gw_kcgi_error(kerr);
4339 return error;
4342 static const struct got_error *
4343 gw_output_repo_tree(struct gw_trans *gw_trans, struct gw_header *header)
4345 const struct got_error *error = NULL;
4346 struct got_object_id *tree_id = NULL, *commit_id = NULL;
4347 struct got_tree_object *tree = NULL;
4348 struct got_commit_object *commit = NULL;
4349 char *path = NULL, *in_repo_path = NULL;
4350 char *id_str = NULL;
4351 char *build_folder = NULL;
4352 char *href_blob = NULL, *href_blame = NULL;
4353 const char *class = NULL;
4354 int nentries, i, class_flip = 0;
4355 enum kcgi_err kerr = KCGI_OK;
4357 if (gw_trans->repo_folder != NULL) {
4358 path = strdup(gw_trans->repo_folder);
4359 if (path == NULL) {
4360 error = got_error_from_errno("strdup");
4361 goto done;
4363 } else {
4364 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
4365 gw_trans->repo_path);
4366 if (error)
4367 goto done;
4368 free(path);
4369 path = in_repo_path;
4372 if (gw_trans->commit_id == NULL) {
4373 struct got_reference *head_ref;
4374 error = got_ref_open(&head_ref, gw_trans->repo,
4375 gw_trans->headref, 0);
4376 if (error)
4377 goto done;
4378 error = got_ref_resolve(&commit_id, gw_trans->repo, head_ref);
4379 if (error)
4380 goto done;
4381 got_ref_close(head_ref);
4383 * gw_trans->commit_id was not parsed from the querystring
4384 * we hit this code path from gw_index, where we don't know the
4385 * commit values for the tree link yet, so set
4386 * gw_trans->commit_id here to continue further into the tree
4388 error = got_object_id_str(&gw_trans->commit_id, commit_id);
4389 if (error)
4390 goto done;
4392 } else {
4393 error = got_repo_match_object_id(&commit_id, NULL,
4394 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT, &header->refs,
4395 gw_trans->repo);
4396 if (error)
4397 goto done;
4400 error = got_object_open_as_commit(&commit, gw_trans->repo, commit_id);
4401 if (error)
4402 goto done;
4404 error = got_object_id_by_path(&tree_id, gw_trans->repo, commit,
4405 path);
4406 if (error)
4407 goto done;
4409 error = got_object_open_as_tree(&tree, gw_trans->repo, tree_id);
4410 if (error)
4411 goto done;
4413 nentries = got_object_tree_get_nentries(tree);
4414 for (i = 0; i < nentries; i++) {
4415 struct got_tree_entry *te;
4416 const char *modestr = "";
4417 mode_t mode;
4419 te = got_object_tree_get_entry(tree, i);
4421 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
4422 if (error)
4423 goto done;
4425 mode = got_tree_entry_get_mode(te);
4426 if (got_object_tree_entry_is_submodule(te))
4427 modestr = "$";
4428 else if (S_ISLNK(mode))
4429 modestr = "@";
4430 else if (S_ISDIR(mode))
4431 modestr = "/";
4432 else if (mode & S_IXUSR)
4433 modestr = "*";
4435 if (class_flip == 0) {
4436 class = "back_lightgray";
4437 class_flip = 1;
4438 } else {
4439 class = "back_white";
4440 class_flip = 0;
4443 if (S_ISDIR(mode)) {
4444 if (asprintf(&build_folder, "%s/%s",
4445 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4446 got_tree_entry_get_name(te)) == -1) {
4447 error = got_error_from_errno("asprintf");
4448 goto done;
4451 href_blob = khttp_urlpart(NULL, NULL, "gotweb", "path",
4452 gw_trans->repo_name, "action",
4453 gw_get_action_name(gw_trans), "commit",
4454 gw_trans->commit_id, "folder", build_folder, NULL);
4455 if (href_blob == NULL) {
4456 error = got_error_from_errno("khttp_urlpart");
4457 goto done;
4459 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4460 KATTR_ID, "tree_wrapper", KATTR__MAX);
4461 if (kerr != KCGI_OK)
4462 goto done;
4463 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4464 KATTR_ID, "tree_line", KATTR_CLASS, class,
4465 KATTR__MAX);
4466 if (kerr != KCGI_OK)
4467 goto done;
4468 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4469 KATTR_HREF, href_blob, KATTR_CLASS,
4470 "diff_directory", KATTR__MAX);
4471 if (kerr != KCGI_OK)
4472 goto done;
4473 kerr = khtml_printf(gw_trans->gw_html_req, "%s%s",
4474 got_tree_entry_get_name(te), modestr);
4475 if (kerr != KCGI_OK)
4476 goto done;
4477 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4478 if (kerr != KCGI_OK)
4479 goto done;
4480 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4481 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
4482 KATTR__MAX);
4483 if (kerr != KCGI_OK)
4484 goto done;
4485 kerr = khtml_entity(gw_trans->gw_html_req,
4486 KENTITY_nbsp);
4487 if (kerr != KCGI_OK)
4488 goto done;
4489 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4490 if (kerr != KCGI_OK)
4491 goto done;
4492 } else {
4493 href_blob = khttp_urlpart(NULL, NULL, "gotweb", "path",
4494 gw_trans->repo_name, "action", "blob", "commit",
4495 gw_trans->commit_id, "file",
4496 got_tree_entry_get_name(te), "folder",
4497 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4498 NULL);
4499 if (href_blob == NULL) {
4500 error = got_error_from_errno("khttp_urlpart");
4501 goto done;
4503 href_blame = khttp_urlpart(NULL, NULL, "gotweb", "path",
4504 gw_trans->repo_name, "action", "blame", "commit",
4505 gw_trans->commit_id, "file",
4506 got_tree_entry_get_name(te), "folder",
4507 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4508 NULL);
4509 if (href_blame == NULL) {
4510 error = got_error_from_errno("khttp_urlpart");
4511 goto done;
4513 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4514 KATTR_ID, "tree_wrapper", KATTR__MAX);
4515 if (kerr != KCGI_OK)
4516 goto done;
4517 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4518 KATTR_ID, "tree_line", KATTR_CLASS, class,
4519 KATTR__MAX);
4520 if (kerr != KCGI_OK)
4521 goto done;
4522 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4523 KATTR_HREF, href_blob, KATTR__MAX);
4524 if (kerr != KCGI_OK)
4525 goto done;
4526 kerr = khtml_printf(gw_trans->gw_html_req, "%s%s",
4527 got_tree_entry_get_name(te), modestr);
4528 if (kerr != KCGI_OK)
4529 goto done;
4530 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4531 if (kerr != KCGI_OK)
4532 goto done;
4533 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4534 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
4535 KATTR__MAX);
4536 if (kerr != KCGI_OK)
4537 goto done;
4539 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4540 KATTR_HREF, href_blob, KATTR__MAX);
4541 if (kerr != KCGI_OK)
4542 goto done;
4543 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
4544 if (kerr != KCGI_OK)
4545 goto done;
4546 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4547 if (kerr != KCGI_OK)
4548 goto done;
4550 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4551 if (kerr != KCGI_OK)
4552 goto done;
4554 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4555 KATTR_HREF, href_blame, KATTR__MAX);
4556 if (kerr != KCGI_OK)
4557 goto done;
4558 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
4559 if (kerr != KCGI_OK)
4560 goto done;
4562 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4563 if (kerr != KCGI_OK)
4564 goto done;
4566 free(id_str);
4567 id_str = NULL;
4568 free(href_blob);
4569 href_blob = NULL;
4570 free(build_folder);
4571 build_folder = NULL;
4573 done:
4574 if (tree)
4575 got_object_tree_close(tree);
4576 if (commit)
4577 got_object_commit_close(commit);
4578 free(id_str);
4579 free(href_blob);
4580 free(href_blame);
4581 free(in_repo_path);
4582 free(tree_id);
4583 free(build_folder);
4584 if (error == NULL && kerr != KCGI_OK)
4585 error = gw_kcgi_error(kerr);
4586 return error;
4589 static const struct got_error *
4590 gw_output_repo_heads(struct gw_trans *gw_trans)
4592 const struct got_error *error = NULL;
4593 struct got_reflist_head refs;
4594 struct got_reflist_entry *re;
4595 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
4596 char *href_commits = NULL;
4597 enum kcgi_err kerr = KCGI_OK;
4599 TAILQ_INIT(&refs);
4601 error = got_ref_list(&refs, gw_trans->repo, "refs/heads",
4602 got_ref_cmp_by_name, NULL);
4603 if (error)
4604 goto done;
4606 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4607 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
4608 if (kerr != KCGI_OK)
4609 goto done;
4610 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4611 KATTR_ID, "summary_heads_title", KATTR__MAX);
4612 if (kerr != KCGI_OK)
4613 goto done;
4614 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
4615 if (kerr != KCGI_OK)
4616 goto done;
4617 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4618 if (kerr != KCGI_OK)
4619 goto done;
4620 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4621 KATTR_ID, "summary_heads_content", KATTR__MAX);
4622 if (kerr != KCGI_OK)
4623 goto done;
4625 TAILQ_FOREACH(re, &refs, entry) {
4626 const char *refname;
4628 if (got_ref_is_symbolic(re->ref))
4629 continue;
4631 refname = got_ref_get_name(re->ref);
4632 if (strncmp(refname, "refs/heads/", 11) != 0)
4633 continue;
4635 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
4636 refname, TM_DIFF);
4637 if (error)
4638 goto done;
4640 if (strncmp(refname, "refs/heads/", 11) == 0)
4641 refname += 11;
4643 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4644 KATTR_ID, "heads_wrapper", KATTR__MAX);
4645 if (kerr != KCGI_OK)
4646 goto done;
4647 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4648 KATTR_ID, "heads_age", KATTR__MAX);
4649 if (kerr != KCGI_OK)
4650 goto done;
4651 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
4652 if (kerr != KCGI_OK)
4653 goto done;
4654 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4655 if (kerr != KCGI_OK)
4656 goto done;
4657 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4658 KATTR_ID, "heads_space", KATTR__MAX);
4659 if (kerr != KCGI_OK)
4660 goto done;
4661 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
4662 if (kerr != KCGI_OK)
4663 goto done;
4664 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4665 if (kerr != KCGI_OK)
4666 goto done;
4667 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4668 KATTR_ID, "head", KATTR__MAX);
4669 if (kerr != KCGI_OK)
4670 goto done;
4672 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
4673 gw_trans->repo_name, "action", "summary", "headref",
4674 refname, NULL);
4675 if (href_summary == NULL) {
4676 error = got_error_from_errno("khttp_urlpart");
4677 goto done;
4679 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4680 href_summary, KATTR__MAX);
4681 kerr = khtml_puts(gw_trans->gw_html_req, refname);
4682 if (kerr != KCGI_OK)
4683 goto done;
4684 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4685 if (kerr != KCGI_OK)
4686 goto done;
4688 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4689 "navs_wrapper", KATTR__MAX);
4690 if (kerr != KCGI_OK)
4691 goto done;
4692 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4693 "navs", KATTR__MAX);
4694 if (kerr != KCGI_OK)
4695 goto done;
4697 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4698 href_summary, KATTR__MAX);
4699 if (kerr != KCGI_OK)
4700 goto done;
4701 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4702 if (kerr != KCGI_OK)
4703 goto done;
4704 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4705 if (kerr != KCGI_OK)
4706 goto done;
4708 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4709 if (kerr != KCGI_OK)
4710 goto done;
4712 href_briefs = khttp_urlpart(NULL, NULL, "gotweb", "path",
4713 gw_trans->repo_name, "action", "briefs", "headref",
4714 refname, NULL);
4715 if (href_briefs == NULL) {
4716 error = got_error_from_errno("khttp_urlpart");
4717 goto done;
4719 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4720 href_briefs, KATTR__MAX);
4721 if (kerr != KCGI_OK)
4722 goto done;
4723 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4724 if (kerr != KCGI_OK)
4725 goto done;
4726 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4727 if (kerr != KCGI_OK)
4728 goto done;
4730 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4731 if (kerr != KCGI_OK)
4732 goto done;
4734 href_commits = khttp_urlpart(NULL, NULL, "gotweb", "path",
4735 gw_trans->repo_name, "action", "commits", "headref",
4736 refname, NULL);
4737 if (href_commits == NULL) {
4738 error = got_error_from_errno("khttp_urlpart");
4739 goto done;
4741 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4742 href_commits, KATTR__MAX);
4743 if (kerr != KCGI_OK)
4744 goto done;
4745 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4746 if (kerr != KCGI_OK)
4747 goto done;
4748 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4749 if (kerr != KCGI_OK)
4750 goto done;
4752 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4753 "dotted_line", KATTR__MAX);
4754 if (kerr != KCGI_OK)
4755 goto done;
4756 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4757 if (kerr != KCGI_OK)
4758 goto done;
4759 free(href_summary);
4760 href_summary = NULL;
4761 free(href_briefs);
4762 href_briefs = NULL;
4763 free(href_commits);
4764 href_commits = NULL;
4766 done:
4767 got_ref_list_free(&refs);
4768 free(href_summary);
4769 free(href_briefs);
4770 free(href_commits);
4771 return error;
4774 static const struct got_error *
4775 gw_output_site_link(struct gw_trans *gw_trans)
4777 const struct got_error *error = NULL;
4778 char *href_summary = NULL;
4779 enum kcgi_err kerr = KCGI_OK;
4781 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4782 "site_link", KATTR__MAX);
4783 if (kerr != KCGI_OK)
4784 goto done;
4785 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4786 KATTR__MAX);
4787 if (kerr != KCGI_OK)
4788 goto done;
4789 kerr = khtml_puts(gw_trans->gw_html_req,
4790 gw_trans->gw_conf->got_site_link);
4791 if (kerr != KCGI_OK)
4792 goto done;
4793 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4794 if (kerr != KCGI_OK)
4795 goto done;
4797 if (gw_trans->repo_name != NULL) {
4798 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4799 if (kerr != KCGI_OK)
4800 goto done;
4802 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
4803 gw_trans->repo_name, "action", "summary", NULL);
4804 if (href_summary == NULL) {
4805 error = got_error_from_errno("khttp_urlpart");
4806 goto done;
4808 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4809 href_summary, KATTR__MAX);
4810 if (kerr != KCGI_OK)
4811 goto done;
4812 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4813 if (kerr != KCGI_OK)
4814 goto done;
4815 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4816 if (kerr != KCGI_OK)
4817 goto done;
4818 kerr = khtml_printf(gw_trans->gw_html_req, " / %s",
4819 gw_get_action_name(gw_trans));
4820 if (kerr != KCGI_OK)
4821 goto done;
4824 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4825 if (kerr != KCGI_OK)
4826 goto done;
4827 done:
4828 free(href_summary);
4829 if (error == NULL && kerr != KCGI_OK)
4830 error = gw_kcgi_error(kerr);
4831 return error;
4834 static const struct got_error *
4835 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4837 const struct got_error *error = NULL;
4838 const char *color = NULL;
4839 enum kcgi_err kerr = KCGI_OK;
4841 if (strncmp(buf, "-", 1) == 0)
4842 color = "diff_minus";
4843 else if (strncmp(buf, "+", 1) == 0)
4844 color = "diff_plus";
4845 else if (strncmp(buf, "@@", 2) == 0)
4846 color = "diff_chunk_header";
4847 else if (strncmp(buf, "@@", 2) == 0)
4848 color = "diff_chunk_header";
4849 else if (strncmp(buf, "commit +", 8) == 0)
4850 color = "diff_meta";
4851 else if (strncmp(buf, "commit -", 8) == 0)
4852 color = "diff_meta";
4853 else if (strncmp(buf, "blob +", 6) == 0)
4854 color = "diff_meta";
4855 else if (strncmp(buf, "blob -", 6) == 0)
4856 color = "diff_meta";
4857 else if (strncmp(buf, "file +", 6) == 0)
4858 color = "diff_meta";
4859 else if (strncmp(buf, "file -", 6) == 0)
4860 color = "diff_meta";
4861 else if (strncmp(buf, "from:", 5) == 0)
4862 color = "diff_author";
4863 else if (strncmp(buf, "via:", 4) == 0)
4864 color = "diff_author";
4865 else if (strncmp(buf, "date:", 5) == 0)
4866 color = "diff_date";
4867 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4868 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4869 if (error == NULL && kerr != KCGI_OK)
4870 error = gw_kcgi_error(kerr);
4871 return error;
4874 int
4875 main(int argc, char *argv[])
4877 const struct got_error *error = NULL, *error2 = NULL;
4878 struct gw_trans *gw_trans;
4879 struct gw_dir *dir = NULL, *tdir;
4880 const char *page = "index";
4881 enum kcgi_err kerr = KCGI_OK;
4883 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4884 errx(1, "malloc");
4886 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4887 errx(1, "malloc");
4889 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4890 errx(1, "malloc");
4892 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4893 errx(1, "malloc");
4895 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4896 if (kerr != KCGI_OK) {
4897 error = gw_kcgi_error(kerr);
4898 goto done;
4901 TAILQ_INIT(&gw_trans->gw_dirs);
4902 TAILQ_INIT(&gw_trans->gw_headers);
4904 gw_trans->action = -1;
4905 gw_trans->page = 0;
4906 gw_trans->repos_total = 0;
4907 gw_trans->repo_path = NULL;
4908 gw_trans->commit_id = NULL;
4909 gw_trans->next_id = NULL;
4910 gw_trans->prev_id = NULL;
4911 gw_trans->headref = GOT_REF_HEAD;
4912 gw_trans->mime = KMIME_TEXT_HTML;
4913 gw_trans->gw_tmpl->key = gw_templs;
4914 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4915 gw_trans->gw_tmpl->arg = gw_trans;
4916 gw_trans->gw_tmpl->cb = gw_template;
4918 error = got_repo_pack_fds_open(&gw_trans->pack_fds);
4919 if (error != NULL)
4920 goto done;
4922 error = parse_gotweb_config(&gw_trans->gw_conf, GOTWEB_CONF);
4923 if (error)
4924 goto done;
4926 error = gw_parse_querystring(gw_trans);
4927 if (error)
4928 goto done;
4930 if (gw_trans->repo) {
4931 const struct got_error *close_err;
4932 close_err = got_repo_close(gw_trans->repo);
4933 if (error == NULL)
4934 error = close_err;
4936 if (gw_trans->action == GW_BLOB)
4937 error = gw_blob(gw_trans);
4938 else
4939 error = gw_display_index(gw_trans);
4940 done:
4941 if (error) {
4942 gw_trans->error = error;
4943 gw_trans->action = GW_ERR;
4944 error2 = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
4945 if (error2)
4946 goto cleanup; /* we can't display an error page */
4947 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
4948 if (kerr != KCGI_OK)
4949 goto cleanup; /* we can't display an error page */
4950 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
4951 gw_query_funcs[gw_trans->action].template);
4952 if (kerr != KCGI_OK) {
4953 khtml_close(gw_trans->gw_html_req);
4954 goto cleanup; /* we can't display an error page */
4958 cleanup:
4959 if (gw_trans->pack_fds) {
4960 const struct got_error *pack_err =
4961 got_repo_pack_fds_close(gw_trans->pack_fds);
4962 if (error == NULL)
4963 error = pack_err;
4964 gw_trans->pack_fds = NULL;
4966 free(gw_trans->gw_conf->got_repos_path);
4967 free(gw_trans->gw_conf->got_www_path);
4968 free(gw_trans->gw_conf->got_site_name);
4969 free(gw_trans->gw_conf->got_site_owner);
4970 free(gw_trans->gw_conf->got_site_link);
4971 free(gw_trans->gw_conf->got_logo);
4972 free(gw_trans->gw_conf->got_logo_url);
4973 free(gw_trans->gw_conf);
4974 free(gw_trans->commit_id);
4975 free(gw_trans->next_id);
4976 free(gw_trans->prev_id);
4977 free(gw_trans->repo_path);
4978 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4979 free(dir->name);
4980 free(dir->description);
4981 free(dir->age);
4982 free(dir->url);
4983 free(dir->path);
4984 free(dir);
4987 khttp_free(gw_trans->gw_req);
4988 return 0;