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 };
81 struct gw_header {
82 TAILQ_ENTRY(gw_header) entry;
83 struct got_reflist_head refs;
84 char *path;
86 char *refs_str;
87 char *commit_id; /* id_str1 */
88 char *parent_id; /* id_str2 */
89 char *tree_id;
90 char *author;
91 char *committer;
92 char *commit_msg;
93 time_t committer_time;
94 };
96 struct gw_dir {
97 TAILQ_ENTRY(gw_dir) entry;
98 char *name;
99 char *owner;
100 char *description;
101 char *url;
102 char *age;
103 char *path;
104 };
106 enum gw_key {
107 KEY_ACTION,
108 KEY_COMMIT_ID,
109 KEY_FILE,
110 KEY_FOLDER,
111 KEY_HEADREF,
112 KEY_PAGE,
113 KEY_PATH,
114 KEY_PREV_ID,
115 KEY__ZMAX
116 };
118 enum gw_tmpl {
119 TEMPL_CONTENT,
120 TEMPL_HEAD,
121 TEMPL_HEADER,
122 TEMPL_SEARCH,
123 TEMPL_SITEPATH,
124 TEMPL_SITEOWNER,
125 TEMPL_TITLE,
126 TEMPL__MAX
127 };
129 enum gw_ref_tm {
130 TM_DIFF,
131 TM_LONG,
132 };
134 enum gw_tags_type {
135 TAGBRIEF,
136 TAGFULL,
137 };
139 static const char *const gw_templs[TEMPL__MAX] = {
140 "content",
141 "head",
142 "header",
143 "search",
144 "sitepath",
145 "siteowner",
146 "title",
147 };
149 static const struct kvalid gw_keys[KEY__ZMAX] = {
150 { kvalid_stringne, "action" },
151 { kvalid_stringne, "commit" },
152 { kvalid_stringne, "file" },
153 { kvalid_stringne, "folder" },
154 { kvalid_stringne, "headref" },
155 { kvalid_int, "page" },
156 { kvalid_stringne, "path" },
157 { kvalid_stringne, "prev" },
158 };
160 static struct gw_header *gw_init_header(void);
162 static void gw_free_header(struct gw_header *);
164 static int gw_template(size_t, void *);
166 static const struct got_error *gw_error(struct gw_trans *);
167 static const struct got_error *gw_init_gw_dir(struct gw_dir **, const char *);
168 static const struct got_error *gw_get_repo_description(char **,
169 struct gw_trans *, char *);
170 static const struct got_error *gw_get_repo_owner(char **, struct gw_trans *,
171 char *);
172 static const struct got_error *gw_get_time_str(char **, time_t, int);
173 static const struct got_error *gw_get_repo_age(char **, struct gw_trans *,
174 char *, const char *, int);
175 static const struct got_error *gw_output_file_blame(struct gw_trans *,
176 struct gw_header *);
177 static const struct got_error *gw_output_blob_buf(struct gw_trans *,
178 struct gw_header *);
179 static const struct got_error *gw_output_repo_tree(struct gw_trans *,
180 struct gw_header *);
181 static const struct got_error *gw_output_diff(struct gw_trans *,
182 struct gw_header *);
183 static const struct got_error *gw_output_repo_tags(struct gw_trans *,
184 struct gw_header *, int, int);
185 static const struct got_error *gw_output_repo_heads(struct gw_trans *);
186 static const struct got_error *gw_output_site_link(struct gw_trans *);
187 static const struct got_error *gw_get_clone_url(char **, struct gw_trans *,
188 char *);
189 static const struct got_error *gw_colordiff_line(struct gw_trans *, char *);
191 static const struct got_error *gw_gen_commit_header(struct gw_trans *, char *,
192 char*);
193 static const struct got_error *gw_gen_diff_header(struct gw_trans *, char *,
194 char*);
195 static const struct got_error *gw_gen_author_header(struct gw_trans *,
196 const char *);
197 static const struct got_error *gw_gen_age_header(struct gw_trans *,
198 const char *);
199 static const struct got_error *gw_gen_committer_header(struct gw_trans *,
200 const char *);
201 static const struct got_error *gw_gen_commit_msg_header(struct gw_trans*,
202 char *);
203 static const struct got_error *gw_gen_tree_header(struct gw_trans *, char *);
204 static const struct got_error *gw_display_open(struct gw_trans *, enum khttp,
205 enum kmime);
206 static const struct got_error *gw_display_index(struct gw_trans *);
207 static const struct got_error *gw_get_header(struct gw_trans *,
208 struct gw_header *, int);
209 static const struct got_error *gw_get_commits(struct gw_trans *,
210 struct gw_header *, int,
211 struct got_object_id *);
212 static const struct got_error *gw_get_commit(struct gw_trans *,
213 struct gw_header *,
214 struct got_commit_object *,
215 struct got_object_id *);
216 static const struct got_error *gw_apply_unveil(const char *);
217 static const struct got_error *gw_blame_cb(void *, int, int,
218 struct got_commit_object *,
219 struct got_object_id *);
220 static const struct got_error *gw_load_got_paths(struct gw_trans *);
221 static const struct got_error *gw_load_got_path(struct gw_trans *,
222 struct gw_dir *);
223 static const struct got_error *gw_parse_querystring(struct gw_trans *);
224 static const struct got_error *gw_blame(struct gw_trans *);
225 static const struct got_error *gw_blob(struct gw_trans *);
226 static const struct got_error *gw_diff(struct gw_trans *);
227 static const struct got_error *gw_index(struct gw_trans *);
228 static const struct got_error *gw_commits(struct gw_trans *);
229 static const struct got_error *gw_briefs(struct gw_trans *);
230 static const struct got_error *gw_summary(struct gw_trans *);
231 static const struct got_error *gw_tree(struct gw_trans *);
232 static const struct got_error *gw_tag(struct gw_trans *);
233 static const struct got_error *gw_tags(struct gw_trans *);
235 struct gw_query_action {
236 unsigned int func_id;
237 const char *func_name;
238 const struct got_error *(*func_main)(struct gw_trans *);
239 char *template;
240 };
242 enum gw_query_actions {
243 GW_BLAME,
244 GW_BLOB,
245 GW_BRIEFS,
246 GW_COMMITS,
247 GW_DIFF,
248 GW_ERR,
249 GW_INDEX,
250 GW_SUMMARY,
251 GW_TAG,
252 GW_TAGS,
253 GW_TREE,
254 };
256 static const struct gw_query_action gw_query_funcs[] = {
257 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
258 { GW_BLOB, "blob", NULL, NULL },
259 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
260 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
261 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
262 { GW_ERR, "error", gw_error, "gw_tmpl/err.tmpl" },
263 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
264 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
265 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
266 { GW_TAGS, "tags", gw_tags, "gw_tmpl/tags.tmpl" },
267 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
268 };
270 static const char *
271 gw_get_action_name(struct gw_trans *gw_trans)
273 return gw_query_funcs[gw_trans->action].func_name;
276 static const struct got_error *
277 gw_kcgi_error(enum kcgi_err kerr)
279 if (kerr == KCGI_OK)
280 return NULL;
282 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
283 return got_error(GOT_ERR_CANCELLED);
285 if (kerr == KCGI_ENOMEM)
286 return got_error_set_errno(ENOMEM,
287 kcgi_strerror(kerr));
289 if (kerr == KCGI_ENFILE)
290 return got_error_set_errno(ENFILE,
291 kcgi_strerror(kerr));
293 if (kerr == KCGI_EAGAIN)
294 return got_error_set_errno(EAGAIN,
295 kcgi_strerror(kerr));
297 if (kerr == KCGI_FORM)
298 return got_error_msg(GOT_ERR_IO,
299 kcgi_strerror(kerr));
301 return got_error_from_errno(kcgi_strerror(kerr));
304 static const struct got_error *
305 gw_apply_unveil(const char *repo_path)
307 const struct got_error *err;
309 #ifdef PROFILE
310 if (unveil("gmon.out", "rwc") != 0)
311 return got_error_from_errno2("unveil", "gmon.out");
312 #endif
313 if (repo_path && unveil(repo_path, "r") != 0)
314 return got_error_from_errno2("unveil", repo_path);
316 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
317 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
319 err = got_privsep_unveil_exec_helpers();
320 if (err != NULL)
321 return err;
323 if (unveil(NULL, NULL) != 0)
324 return got_error_from_errno("unveil");
326 return NULL;
329 static int
330 isbinary(const uint8_t *buf, size_t n)
332 size_t i;
334 for (i = 0; i < n; i++)
335 if (buf[i] == 0)
336 return 1;
337 return 0;
340 static const struct got_error *
341 gw_blame(struct gw_trans *gw_trans)
343 const struct got_error *error = NULL;
344 struct gw_header *header = NULL;
345 char *age = NULL;
346 enum kcgi_err kerr = KCGI_OK;
348 #ifndef PROFILE
349 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
350 NULL) == -1)
351 return got_error_from_errno("pledge");
352 #endif
353 if ((header = gw_init_header()) == NULL)
354 return got_error_from_errno("malloc");
356 error = gw_apply_unveil(gw_trans->gw_dir->path);
357 if (error)
358 goto done;
360 /* check querystring */
361 if (gw_trans->repo_file == NULL) {
362 error = got_error_msg(GOT_ERR_QUERYSTRING,
363 "file required in querystring");
364 goto done;
366 if (gw_trans->commit_id == NULL) {
367 error = got_error_msg(GOT_ERR_QUERYSTRING,
368 "commit required in querystring");
369 goto done;
372 error = gw_get_header(gw_trans, header, 1);
373 if (error)
374 goto done;
375 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
376 "blame_header_wrapper", KATTR__MAX);
377 if (kerr != KCGI_OK)
378 goto done;
379 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
380 "blame_header", KATTR__MAX);
381 if (kerr != KCGI_OK)
382 goto done;
383 error = gw_get_time_str(&age, header->committer_time,
384 TM_LONG);
385 if (error)
386 goto done;
387 error = gw_gen_age_header(gw_trans, age ?age : "");
388 if (error)
389 goto done;
390 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
391 if (error)
392 goto done;
393 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
394 if (kerr != KCGI_OK)
395 goto done;
396 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
397 "dotted_line", KATTR__MAX);
398 if (kerr != KCGI_OK)
399 goto done;
400 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
401 if (kerr != KCGI_OK)
402 goto done;
404 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
405 "blame", KATTR__MAX);
406 if (kerr != KCGI_OK)
407 goto done;
408 error = gw_output_file_blame(gw_trans, header);
409 if (error)
410 goto done;
411 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
412 done:
413 gw_free_header(header);
414 if (error == NULL && kerr != KCGI_OK)
415 error = gw_kcgi_error(kerr);
416 return error;
419 static const struct got_error *
420 gw_blob(struct gw_trans *gw_trans)
422 const struct got_error *error = NULL, *err = NULL;
423 struct gw_header *header = NULL;
424 enum kcgi_err kerr = KCGI_OK;
426 #ifndef PROFILE
427 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
428 NULL) == -1)
429 return got_error_from_errno("pledge");
430 #endif
431 if ((header = gw_init_header()) == NULL)
432 return got_error_from_errno("malloc");
434 error = gw_apply_unveil(gw_trans->gw_dir->path);
435 if (error)
436 goto done;
438 /* check querystring */
439 if (gw_trans->repo_file == NULL) {
440 error = got_error_msg(GOT_ERR_QUERYSTRING,
441 "file required in querystring");
442 goto done;
444 if (gw_trans->commit_id == NULL) {
445 error = got_error_msg(GOT_ERR_QUERYSTRING,
446 "commit required in querystring");
447 goto done;
449 error = gw_get_header(gw_trans, header, 1);
450 if (error)
451 goto done;
453 error = gw_output_blob_buf(gw_trans, header);
454 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 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 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 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 proc exec sendfd unveil", NULL) == -1)
1463 return got_error_from_errno("pledge");
1464 #endif
1465 error = gw_apply_unveil(gw_trans->gw_dir->path);
1466 if (error)
1467 goto done;
1469 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1470 "summary_wrapper", KATTR__MAX);
1471 if (kerr != KCGI_OK)
1472 return gw_kcgi_error(kerr);
1474 if (gw_trans->gw_conf->got_show_repo_description &&
1475 gw_trans->gw_dir->description != NULL &&
1476 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1477 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1478 KATTR_ID, "description_title", KATTR__MAX);
1479 if (kerr != KCGI_OK)
1480 goto done;
1481 kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1482 if (kerr != KCGI_OK)
1483 goto done;
1484 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1485 if (kerr != KCGI_OK)
1486 goto done;
1487 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1488 KATTR_ID, "description", KATTR__MAX);
1489 if (kerr != KCGI_OK)
1490 goto done;
1491 kerr = khtml_puts(gw_trans->gw_html_req,
1492 gw_trans->gw_dir->description);
1493 if (kerr != KCGI_OK)
1494 goto done;
1495 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1496 if (kerr != KCGI_OK)
1497 goto done;
1500 if (gw_trans->gw_conf->got_show_repo_owner &&
1501 gw_trans->gw_dir->owner != NULL &&
1502 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
1503 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1504 KATTR_ID, "repo_owner_title", KATTR__MAX);
1505 if (kerr != KCGI_OK)
1506 goto done;
1507 kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1508 if (kerr != KCGI_OK)
1509 goto done;
1510 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1511 if (kerr != KCGI_OK)
1512 goto done;
1513 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1514 KATTR_ID, "repo_owner", KATTR__MAX);
1515 if (kerr != KCGI_OK)
1516 goto done;
1517 kerr = khtml_puts(gw_trans->gw_html_req,
1518 gw_trans->gw_dir->owner);
1519 if (kerr != KCGI_OK)
1520 goto done;
1521 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1522 if (kerr != KCGI_OK)
1523 goto done;
1526 if (gw_trans->gw_conf->got_show_repo_age) {
1527 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1528 NULL, TM_LONG);
1529 if (error)
1530 goto done;
1531 if (age != NULL) {
1532 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1533 KATTR_ID, "last_change_title", KATTR__MAX);
1534 if (kerr != KCGI_OK)
1535 goto done;
1536 kerr = khtml_puts(gw_trans->gw_html_req,
1537 "Last Change: ");
1538 if (kerr != KCGI_OK)
1539 goto done;
1540 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1541 if (kerr != KCGI_OK)
1542 goto done;
1543 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1544 KATTR_ID, "last_change", KATTR__MAX);
1545 if (kerr != KCGI_OK)
1546 goto done;
1547 kerr = khtml_puts(gw_trans->gw_html_req, age);
1548 if (kerr != KCGI_OK)
1549 goto done;
1550 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1551 if (kerr != KCGI_OK)
1552 goto done;
1556 if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1557 gw_trans->gw_dir->url != NULL &&
1558 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1559 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1560 KATTR_ID, "cloneurl_title", KATTR__MAX);
1561 if (kerr != KCGI_OK)
1562 goto done;
1563 kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1564 if (kerr != KCGI_OK)
1565 goto done;
1566 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1567 if (kerr != KCGI_OK)
1568 goto done;
1569 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1570 KATTR_ID, "cloneurl", KATTR__MAX);
1571 if (kerr != KCGI_OK)
1572 goto done;
1573 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1574 if (kerr != KCGI_OK)
1575 goto done;
1576 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1577 if (kerr != KCGI_OK)
1578 goto done;
1581 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1582 if (kerr != KCGI_OK)
1583 goto done;
1585 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1586 "briefs_title_wrapper", KATTR__MAX);
1587 if (kerr != KCGI_OK)
1588 goto done;
1589 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1590 "briefs_title", KATTR__MAX);
1591 if (kerr != KCGI_OK)
1592 goto done;
1593 kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1594 if (kerr != KCGI_OK)
1595 goto done;
1596 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1597 if (kerr != KCGI_OK)
1598 goto done;
1599 error = gw_briefs(gw_trans);
1600 if (error)
1601 goto done;
1603 error = gw_tags(gw_trans);
1604 if (error)
1605 goto done;
1607 error = gw_output_repo_heads(gw_trans);
1608 done:
1609 free(age);
1610 if (error == NULL && kerr != KCGI_OK)
1611 error = gw_kcgi_error(kerr);
1612 return error;
1615 static const struct got_error *
1616 gw_tree(struct gw_trans *gw_trans)
1618 const struct got_error *error = NULL;
1619 struct gw_header *header = NULL;
1620 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1621 char *age = NULL;
1622 enum kcgi_err kerr = KCGI_OK;
1624 #ifndef PROFILE
1625 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1626 return got_error_from_errno("pledge");
1627 #endif
1628 if ((header = gw_init_header()) == NULL)
1629 return got_error_from_errno("malloc");
1631 error = gw_apply_unveil(gw_trans->gw_dir->path);
1632 if (error)
1633 goto done;
1635 error = gw_get_header(gw_trans, header, 1);
1636 if (error)
1637 goto done;
1639 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1640 "tree_header_wrapper", KATTR__MAX);
1641 if (kerr != KCGI_OK)
1642 goto done;
1643 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1644 "tree_header", KATTR__MAX);
1645 if (kerr != KCGI_OK)
1646 goto done;
1647 error = gw_gen_tree_header(gw_trans, header->tree_id);
1648 if (error)
1649 goto done;
1650 error = gw_get_time_str(&age, header->committer_time,
1651 TM_LONG);
1652 if (error)
1653 goto done;
1654 error = gw_gen_age_header(gw_trans, age ?age : "");
1655 if (error)
1656 goto done;
1657 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1658 if (error)
1659 goto done;
1660 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1661 if (kerr != KCGI_OK)
1662 goto done;
1663 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1664 "dotted_line", KATTR__MAX);
1665 if (kerr != KCGI_OK)
1666 goto done;
1667 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1668 if (kerr != KCGI_OK)
1669 goto done;
1671 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1672 "tree", KATTR__MAX);
1673 if (kerr != KCGI_OK)
1674 goto done;
1675 error = gw_output_repo_tree(gw_trans, header);
1676 if (error)
1677 goto done;
1679 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1680 done:
1681 gw_free_header(header);
1682 free(tree_html_disp);
1683 free(tree_html);
1684 free(tree);
1685 free(age);
1686 if (error == NULL && kerr != KCGI_OK)
1687 error = gw_kcgi_error(kerr);
1688 return error;
1691 static const struct got_error *
1692 gw_tags(struct gw_trans *gw_trans)
1694 const struct got_error *error = NULL;
1695 struct gw_header *header = NULL;
1696 char *href_next = NULL, *href_prev = NULL;
1697 enum kcgi_err kerr = KCGI_OK;
1699 #ifndef PROFILE
1700 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1701 return got_error_from_errno("pledge");
1702 #endif
1703 if ((header = gw_init_header()) == NULL)
1704 return got_error_from_errno("malloc");
1706 if (gw_trans->action != GW_SUMMARY) {
1707 error = gw_apply_unveil(gw_trans->gw_dir->path);
1708 if (error)
1709 goto done;
1712 error = gw_get_header(gw_trans, header, 1);
1713 if (error)
1714 goto done;
1716 if (gw_trans->action == GW_SUMMARY) {
1717 gw_trans->next_id = NULL;
1718 error = gw_output_repo_tags(gw_trans, header,
1719 D_MAXSLCOMMDISP, TAGBRIEF);
1720 if (error)
1721 goto done;
1722 } else {
1723 error = gw_output_repo_tags(gw_trans, header,
1724 gw_trans->gw_conf->got_max_commits_display, TAGBRIEF);
1725 if (error)
1726 goto done;
1729 if (gw_trans->next_id || gw_trans->page > 0) {
1730 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1731 KATTR_ID, "np_wrapper", KATTR__MAX);
1732 if (kerr != KCGI_OK)
1733 goto done;
1734 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1735 KATTR_ID, "nav_prev", KATTR__MAX);
1736 if (kerr != KCGI_OK)
1737 goto done;
1740 if (gw_trans->prev_id) {
1741 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1742 KATTRX_STRING, gw_trans->repo_name, "page",
1743 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1744 KATTRX_STRING, "tags", "commit", KATTRX_STRING,
1745 gw_trans->prev_id ? gw_trans->prev_id : "", NULL);
1746 if (href_prev == NULL) {
1747 error = got_error_from_errno("khttp_urlpartx");
1748 goto done;
1750 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1751 KATTR_HREF, href_prev, KATTR__MAX);
1752 if (kerr != KCGI_OK)
1753 goto done;
1754 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1755 if (kerr != KCGI_OK)
1756 goto done;
1757 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1758 if (kerr != KCGI_OK)
1759 goto done;
1762 if (gw_trans->next_id || gw_trans->page > 0) {
1763 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1764 if (kerr != KCGI_OK)
1765 return gw_kcgi_error(kerr);
1768 if (gw_trans->next_id) {
1769 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1770 KATTR_ID, "nav_next", KATTR__MAX);
1771 if (kerr != KCGI_OK)
1772 goto done;
1773 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1774 KATTRX_STRING, gw_trans->repo_name, "page",
1775 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1776 KATTRX_STRING, "tags", "commit", KATTRX_STRING,
1777 gw_trans->next_id, NULL);
1778 if (href_next == NULL) {
1779 error = got_error_from_errno("khttp_urlpartx");
1780 goto done;
1782 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1783 KATTR_HREF, href_next, KATTR__MAX);
1784 if (kerr != KCGI_OK)
1785 goto done;
1786 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1787 if (kerr != KCGI_OK)
1788 goto done;
1789 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1790 if (kerr != KCGI_OK)
1791 goto done;
1794 if (gw_trans->next_id || gw_trans->page > 0) {
1795 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1796 if (kerr != KCGI_OK)
1797 goto done;
1799 done:
1800 gw_free_header(header);
1801 free(href_next);
1802 free(href_prev);
1803 if (error == NULL && kerr != KCGI_OK)
1804 error = gw_kcgi_error(kerr);
1805 return error;
1808 static const struct got_error *
1809 gw_tag(struct gw_trans *gw_trans)
1811 const struct got_error *error = NULL;
1812 struct gw_header *header = NULL;
1813 enum kcgi_err kerr = KCGI_OK;
1815 #ifndef PROFILE
1816 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1817 return got_error_from_errno("pledge");
1818 #endif
1819 if ((header = gw_init_header()) == NULL)
1820 return got_error_from_errno("malloc");
1822 error = gw_apply_unveil(gw_trans->gw_dir->path);
1823 if (error)
1824 goto done;
1826 if (gw_trans->commit_id == NULL) {
1827 error = got_error_msg(GOT_ERR_QUERYSTRING,
1828 "commit required in querystring");
1829 goto done;
1832 error = gw_get_header(gw_trans, header, 1);
1833 if (error)
1834 goto done;
1836 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1837 "tag_header_wrapper", KATTR__MAX);
1838 if (kerr != KCGI_OK)
1839 goto done;
1840 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1841 "tag_header", KATTR__MAX);
1842 if (kerr != KCGI_OK)
1843 goto done;
1844 error = gw_gen_commit_header(gw_trans, header->commit_id,
1845 header->refs_str);
1846 if (error)
1847 goto done;
1848 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1849 if (error)
1850 goto done;
1851 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1852 if (kerr != KCGI_OK)
1853 goto done;
1854 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1855 "dotted_line", KATTR__MAX);
1856 if (kerr != KCGI_OK)
1857 goto done;
1858 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1859 if (kerr != KCGI_OK)
1860 goto done;
1862 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1863 "tree", KATTR__MAX);
1864 if (kerr != KCGI_OK)
1865 goto done;
1867 error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1868 if (error)
1869 goto done;
1871 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1872 done:
1873 gw_free_header(header);
1874 if (error == NULL && kerr != KCGI_OK)
1875 error = gw_kcgi_error(kerr);
1876 return error;
1879 static const struct got_error *
1880 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1882 const struct got_error *error = NULL;
1883 DIR *dt;
1884 char *dir_test;
1885 int opened = 0;
1887 if (asprintf(&dir_test, "%s/%s/%s",
1888 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1889 GOTWEB_GIT_DIR) == -1)
1890 return got_error_from_errno("asprintf");
1892 dt = opendir(dir_test);
1893 if (dt == NULL) {
1894 free(dir_test);
1895 } else {
1896 gw_dir->path = strdup(dir_test);
1897 if (gw_dir->path == NULL) {
1898 opened = 1;
1899 error = got_error_from_errno("strdup");
1900 goto errored;
1902 opened = 1;
1903 goto done;
1906 if (asprintf(&dir_test, "%s/%s/%s",
1907 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1908 GOTWEB_GOT_DIR) == -1) {
1909 dir_test = NULL;
1910 error = got_error_from_errno("asprintf");
1911 goto errored;
1914 dt = opendir(dir_test);
1915 if (dt == NULL)
1916 free(dir_test);
1917 else {
1918 opened = 1;
1919 error = got_error(GOT_ERR_NOT_GIT_REPO);
1920 goto errored;
1923 if (asprintf(&dir_test, "%s/%s",
1924 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1) {
1925 error = got_error_from_errno("asprintf");
1926 dir_test = NULL;
1927 goto errored;
1930 gw_dir->path = strdup(dir_test);
1931 if (gw_dir->path == NULL) {
1932 opened = 1;
1933 error = got_error_from_errno("strdup");
1934 goto errored;
1937 dt = opendir(dir_test);
1938 if (dt == NULL) {
1939 error = got_error_path(gw_dir->name, GOT_ERR_NOT_GIT_REPO);
1940 goto errored;
1941 } else
1942 opened = 1;
1943 done:
1944 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1945 gw_dir->path);
1946 if (error)
1947 goto errored;
1948 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1949 if (error)
1950 goto errored;
1951 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1952 NULL, TM_DIFF);
1953 if (error)
1954 goto errored;
1955 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1956 errored:
1957 free(dir_test);
1958 if (opened)
1959 if (dt && closedir(dt) == -1 && error == NULL)
1960 error = got_error_from_errno("closedir");
1961 return error;
1964 static const struct got_error *
1965 gw_load_got_paths(struct gw_trans *gw_trans)
1967 const struct got_error *error = NULL;
1968 DIR *d;
1969 struct dirent **sd_dent;
1970 struct gw_dir *gw_dir;
1971 struct stat st;
1972 unsigned int d_cnt, d_i;
1974 d = opendir(gw_trans->gw_conf->got_repos_path);
1975 if (d == NULL) {
1976 error = got_error_from_errno2("opendir",
1977 gw_trans->gw_conf->got_repos_path);
1978 return error;
1981 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1982 alphasort);
1983 if (d_cnt == -1) {
1984 error = got_error_from_errno2("scandir",
1985 gw_trans->gw_conf->got_repos_path);
1986 goto done;
1989 for (d_i = 0; d_i < d_cnt; d_i++) {
1990 if (gw_trans->gw_conf->got_max_repos > 0 &&
1991 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1992 break; /* account for parent and self */
1994 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1995 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1996 continue;
1998 error = gw_init_gw_dir(&gw_dir, sd_dent[d_i]->d_name);
1999 if (error)
2000 goto done;
2002 error = gw_load_got_path(gw_trans, gw_dir);
2003 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
2004 error = NULL;
2005 continue;
2006 } else if (error && error->code != GOT_ERR_LONELY_PACKIDX)
2007 goto done;
2009 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
2010 !got_path_dir_is_empty(gw_dir->path)) {
2011 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
2012 entry);
2013 gw_trans->repos_total++;
2016 done:
2017 if (d && closedir(d) == -1 && error == NULL)
2018 error = got_error_from_errno("closedir");
2019 return error;
2022 static const struct got_error *
2023 gw_parse_querystring(struct gw_trans *gw_trans)
2025 const struct got_error *error = NULL;
2026 struct kpair *p;
2027 const struct gw_query_action *action = NULL;
2028 unsigned int i;
2030 if (gw_trans->gw_req->fieldnmap[0]) {
2031 return got_error(GOT_ERR_QUERYSTRING);
2032 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
2033 /* define gw_trans->repo_path */
2034 gw_trans->repo_name = p->parsed.s;
2036 if (asprintf(&gw_trans->repo_path, "%s/%s",
2037 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
2038 return got_error_from_errno("asprintf");
2040 /* get action and set function */
2041 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION])) {
2042 for (i = 0; i < nitems(gw_query_funcs); i++) {
2043 action = &gw_query_funcs[i];
2044 if (action->func_name == NULL)
2045 continue;
2046 if (strcmp(action->func_name,
2047 p->parsed.s) == 0) {
2048 gw_trans->action = i;
2049 break;
2053 if (gw_trans->action == -1) {
2054 gw_trans->action = GW_ERR;
2055 gw_trans->error = got_error_msg(GOT_ERR_QUERYSTRING,
2056 p != NULL ? "bad action in querystring" :
2057 "no action in querystring");
2058 return error;
2061 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID])) {
2062 if (asprintf(&gw_trans->commit_id, "%s",
2063 p->parsed.s) == -1)
2064 return got_error_from_errno("asprintf");
2067 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
2068 gw_trans->repo_file = p->parsed.s;
2070 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER])) {
2071 if (asprintf(&gw_trans->repo_folder, "%s",
2072 p->parsed.s) == -1)
2073 return got_error_from_errno("asprintf");
2076 if ((p = gw_trans->gw_req->fieldmap[KEY_PREV_ID])) {
2077 if (asprintf(&gw_trans->prev_id, "%s",
2078 p->parsed.s) == -1)
2079 return got_error_from_errno("asprintf");
2082 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
2083 gw_trans->headref = p->parsed.s;
2085 error = gw_init_gw_dir(&gw_trans->gw_dir, gw_trans->repo_name);
2086 if (error)
2087 return error;
2089 gw_trans->error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
2090 } else
2091 gw_trans->action = GW_INDEX;
2093 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
2094 gw_trans->page = p->parsed.i;
2096 return error;
2099 static const struct got_error *
2100 gw_init_gw_dir(struct gw_dir **gw_dir, const char *dir)
2102 const struct got_error *error;
2104 *gw_dir = malloc(sizeof(**gw_dir));
2105 if (*gw_dir == NULL)
2106 return got_error_from_errno("malloc");
2108 if (asprintf(&(*gw_dir)->name, "%s", dir) == -1) {
2109 error = got_error_from_errno("asprintf");
2110 free(*gw_dir);
2111 *gw_dir = NULL;
2112 return error;
2115 return NULL;
2118 static const struct got_error *
2119 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
2121 enum kcgi_err kerr = KCGI_OK;
2123 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
2124 if (kerr != KCGI_OK)
2125 return gw_kcgi_error(kerr);
2126 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
2127 khttps[code]);
2128 if (kerr != KCGI_OK)
2129 return gw_kcgi_error(kerr);
2130 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
2131 kmimetypes[mime]);
2132 if (kerr != KCGI_OK)
2133 return gw_kcgi_error(kerr);
2134 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
2135 "nosniff");
2136 if (kerr != KCGI_OK)
2137 return gw_kcgi_error(kerr);
2138 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
2139 if (kerr != KCGI_OK)
2140 return gw_kcgi_error(kerr);
2141 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
2142 "1; mode=block");
2143 if (kerr != KCGI_OK)
2144 return gw_kcgi_error(kerr);
2146 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
2147 kerr = khttp_head(gw_trans->gw_req,
2148 kresps[KRESP_CONTENT_DISPOSITION],
2149 "attachment; filename=%s", gw_trans->repo_file);
2150 if (kerr != KCGI_OK)
2151 return gw_kcgi_error(kerr);
2154 kerr = khttp_body(gw_trans->gw_req);
2155 return gw_kcgi_error(kerr);
2158 static const struct got_error *
2159 gw_display_index(struct gw_trans *gw_trans)
2161 const struct got_error *error;
2162 enum kcgi_err kerr = KCGI_OK;
2164 /* catch early querystring errors */
2165 if (gw_trans->error)
2166 gw_trans->action = GW_ERR;
2168 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
2169 if (error)
2170 return error;
2172 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
2173 if (kerr != KCGI_OK)
2174 return gw_kcgi_error(kerr);
2176 if (gw_trans->action != GW_BLOB) {
2177 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
2178 gw_query_funcs[gw_trans->action].template);
2179 if (kerr != KCGI_OK) {
2180 khtml_close(gw_trans->gw_html_req);
2181 return gw_kcgi_error(kerr);
2185 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
2188 static const struct got_error *
2189 gw_error(struct gw_trans *gw_trans)
2191 enum kcgi_err kerr = KCGI_OK;
2193 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->error->msg);
2195 return gw_kcgi_error(kerr);
2198 static int
2199 gw_template(size_t key, void *arg)
2201 const struct got_error *error = NULL;
2202 enum kcgi_err kerr = KCGI_OK;
2203 struct gw_trans *gw_trans = arg;
2204 char *ati = NULL, *fic32 = NULL, *fic16 = NULL;
2205 char *swm = NULL, *spt = NULL, *css = NULL, *logo = NULL;
2207 if (asprintf(&ati, "%s%s", gw_trans->gw_conf->got_www_path,
2208 "/apple-touch-icon.png") == -1)
2209 goto err;
2210 if (asprintf(&fic32, "%s%s", gw_trans->gw_conf->got_www_path,
2211 "/favicon-32x32.png") == -1)
2212 goto err;
2213 if (asprintf(&fic16, "%s%s", gw_trans->gw_conf->got_www_path,
2214 "/favicon-16x16.png") == -1)
2215 goto err;
2216 if (asprintf(&swm, "%s%s", gw_trans->gw_conf->got_www_path,
2217 "/site.webmanifest") == -1)
2218 goto err;
2219 if (asprintf(&spt, "%s%s", gw_trans->gw_conf->got_www_path,
2220 "/safari-pinned-tab.svg") == -1)
2221 goto err;
2222 if (asprintf(&css, "%s%s", gw_trans->gw_conf->got_www_path,
2223 "/gotweb.css") == -1)
2224 goto err;
2225 if (asprintf(&logo, "%s%s%s", gw_trans->gw_conf->got_www_path,
2226 gw_trans->gw_conf->got_www_path ? "/" : "",
2227 gw_trans->gw_conf->got_logo) == -1)
2228 goto err;
2230 switch (key) {
2231 case (TEMPL_HEAD):
2232 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2233 KATTR_NAME, "viewport",
2234 KATTR_CONTENT, "initial-scale=.75, user-scalable=yes",
2235 KATTR__MAX);
2236 if (kerr != KCGI_OK)
2237 return 0;
2238 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2239 if (kerr != KCGI_OK)
2240 return 0;
2241 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2242 KATTR_CHARSET, "utf-8",
2243 KATTR__MAX);
2244 if (kerr != KCGI_OK)
2245 return 0;
2246 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2247 if (kerr != KCGI_OK)
2248 return 0;
2249 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2250 KATTR_NAME, "msapplication-TileColor",
2251 KATTR_CONTENT, "#da532c", KATTR__MAX);
2252 if (kerr != KCGI_OK)
2253 return 0;
2254 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2255 if (kerr != KCGI_OK)
2256 return 0;
2257 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2258 KATTR_NAME, "theme-color",
2259 KATTR_CONTENT, "#ffffff", KATTR__MAX);
2260 if (kerr != KCGI_OK)
2261 return 0;
2262 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2263 if (kerr != KCGI_OK)
2264 return 0;
2265 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2266 KATTR_REL, "apple-touch-icon", KATTR_SIZES, "180x180",
2267 KATTR_HREF, ati, KATTR__MAX);
2268 if (kerr != KCGI_OK)
2269 return 0;
2270 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2271 if (kerr != KCGI_OK)
2272 return 0;
2273 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2274 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2275 "32x32", KATTR_HREF, fic32, KATTR__MAX);
2276 if (kerr != KCGI_OK)
2277 return 0;
2278 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2279 if (kerr != KCGI_OK)
2280 return 0;
2281 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2282 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2283 "16x16", KATTR_HREF, fic16, KATTR__MAX);
2284 if (kerr != KCGI_OK)
2285 return 0;
2286 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2287 if (kerr != KCGI_OK)
2288 return 0;
2289 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2290 KATTR_REL, "manifest", KATTR_HREF, swm,
2291 KATTR__MAX);
2292 if (kerr != KCGI_OK)
2293 return 0;
2294 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2295 if (kerr != KCGI_OK)
2296 return 0;
2297 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2298 KATTR_REL, "mask-icon", KATTR_HREF,
2299 spt, KATTR__MAX);
2300 if (kerr != KCGI_OK)
2301 return 0;
2302 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2303 if (kerr != KCGI_OK)
2304 return 0;
2305 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2306 KATTR_REL, "stylesheet", KATTR_TYPE, "text/css",
2307 KATTR_HREF, css, KATTR__MAX);
2308 if (kerr != KCGI_OK)
2309 return 0;
2310 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2311 if (kerr != KCGI_OK)
2312 return 0;
2313 break;
2314 case(TEMPL_HEADER):
2315 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2316 KATTR_ID, "got_link", KATTR__MAX);
2317 if (kerr != KCGI_OK)
2318 return 0;
2319 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2320 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
2321 KATTR_TARGET, "_sotd", KATTR__MAX);
2322 if (kerr != KCGI_OK)
2323 return 0;
2324 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
2325 KATTR_SRC, logo, KATTR__MAX);
2326 if (kerr != KCGI_OK)
2327 return 0;
2328 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2329 if (kerr != KCGI_OK)
2330 return 0;
2331 break;
2332 case (TEMPL_SITEPATH):
2333 error = gw_output_site_link(gw_trans);
2334 if (error)
2335 return 0;
2336 break;
2337 case(TEMPL_TITLE):
2338 if (gw_trans->gw_conf->got_site_name != NULL) {
2339 kerr = khtml_puts(gw_trans->gw_html_req,
2340 gw_trans->gw_conf->got_site_name);
2341 if (kerr != KCGI_OK)
2342 return 0;
2344 break;
2345 case (TEMPL_SEARCH):
2346 break;
2347 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
2348 "search", KATTR__MAX);
2349 if (kerr != KCGI_OK)
2350 return 0;
2351 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
2352 KATTR_METHOD, "POST", KATTR__MAX);
2353 if (kerr != KCGI_OK)
2354 return 0;
2355 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
2356 "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
2357 KATTR_MAXLENGTH, "50", KATTR__MAX);
2358 if (kerr != KCGI_OK)
2359 return 0;
2360 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
2361 KATTR__MAX);
2362 if (kerr != KCGI_OK)
2363 return 0;
2364 kerr = khtml_puts(gw_trans->gw_html_req, "Search");
2365 if (kerr != KCGI_OK)
2366 return 0;
2367 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
2368 if (kerr != KCGI_OK)
2369 return 0;
2370 break;
2371 case(TEMPL_SITEOWNER):
2372 if (gw_trans->gw_conf->got_site_owner != NULL &&
2373 gw_trans->gw_conf->got_show_site_owner) {
2374 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2375 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
2376 if (kerr != KCGI_OK)
2377 return 0;
2378 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2379 KATTR_ID, "site_owner", KATTR__MAX);
2380 if (kerr != KCGI_OK)
2381 return 0;
2382 kerr = khtml_puts(gw_trans->gw_html_req,
2383 gw_trans->gw_conf->got_site_owner);
2384 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2385 if (kerr != KCGI_OK)
2386 return 0;
2388 break;
2389 case(TEMPL_CONTENT):
2390 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
2391 if (error) {
2392 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2393 KATTR_ID, "tmpl_err", KATTR__MAX);
2394 if (kerr != KCGI_OK)
2395 return 0;
2396 kerr = khttp_printf(gw_trans->gw_req, "Error: %s",
2397 error->msg);
2398 if (kerr != KCGI_OK)
2399 return 0;
2400 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2401 if (kerr != KCGI_OK)
2402 return 0;
2404 break;
2405 default:
2406 return 0;
2408 free(ati);
2409 free(fic32);
2410 free(fic16);
2411 free(swm);
2412 free(spt);
2413 free(css);
2414 free(logo);
2415 return 1;
2416 err:
2417 free(ati);
2418 free(fic32);
2419 free(fic16);
2420 free(swm);
2421 free(spt);
2422 free(css);
2423 free(logo);
2424 return 0;
2427 static const struct got_error *
2428 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
2430 const struct got_error *error = NULL;
2431 enum kcgi_err kerr = KCGI_OK;
2433 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2434 KATTR_ID, "header_commit_title", KATTR__MAX);
2435 if (kerr != KCGI_OK)
2436 goto done;
2437 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
2438 if (kerr != KCGI_OK)
2439 goto done;
2440 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2441 if (kerr != KCGI_OK)
2442 goto done;
2443 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2444 KATTR_ID, "header_commit", KATTR__MAX);
2445 if (kerr != KCGI_OK)
2446 goto done;
2447 kerr = khtml_printf(gw_trans->gw_html_req, "%s ", str1);
2448 if (kerr != KCGI_OK)
2449 goto done;
2450 if (str2 != NULL) {
2451 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
2452 KATTR_ID, "refs_str", KATTR__MAX);
2453 if (kerr != KCGI_OK)
2454 goto done;
2455 kerr = khtml_printf(gw_trans->gw_html_req, "(%s)", str2);
2456 if (kerr != KCGI_OK)
2457 goto done;
2458 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2459 if (kerr != KCGI_OK)
2460 goto done;
2462 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2463 done:
2464 if (error == NULL && kerr != KCGI_OK)
2465 error = gw_kcgi_error(kerr);
2466 return error;
2469 static const struct got_error *
2470 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2472 const struct got_error *error = NULL;
2473 enum kcgi_err kerr = KCGI_OK;
2475 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2476 KATTR_ID, "header_diff_title", KATTR__MAX);
2477 if (kerr != KCGI_OK)
2478 goto done;
2479 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2480 if (kerr != KCGI_OK)
2481 goto done;
2482 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2483 if (kerr != KCGI_OK)
2484 goto done;
2485 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2486 KATTR_ID, "header_diff", KATTR__MAX);
2487 if (kerr != KCGI_OK)
2488 goto done;
2489 if (str1 != NULL) {
2490 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2491 if (kerr != KCGI_OK)
2492 goto done;
2494 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2495 if (kerr != KCGI_OK)
2496 goto done;
2497 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2498 if (kerr != KCGI_OK)
2499 goto done;
2500 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2501 done:
2502 if (error == NULL && kerr != KCGI_OK)
2503 error = gw_kcgi_error(kerr);
2504 return error;
2507 static const struct got_error *
2508 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2510 const struct got_error *error = NULL;
2511 enum kcgi_err kerr = KCGI_OK;
2513 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2514 KATTR_ID, "header_age_title", KATTR__MAX);
2515 if (kerr != KCGI_OK)
2516 goto done;
2517 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2518 if (kerr != KCGI_OK)
2519 goto done;
2520 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2521 if (kerr != KCGI_OK)
2522 goto done;
2523 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2524 KATTR_ID, "header_age", KATTR__MAX);
2525 if (kerr != KCGI_OK)
2526 goto done;
2527 kerr = khtml_puts(gw_trans->gw_html_req, str);
2528 if (kerr != KCGI_OK)
2529 goto done;
2530 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2531 done:
2532 if (error == NULL && kerr != KCGI_OK)
2533 error = gw_kcgi_error(kerr);
2534 return error;
2537 static const struct got_error *
2538 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2540 const struct got_error *error = NULL;
2541 enum kcgi_err kerr = KCGI_OK;
2543 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2544 KATTR_ID, "header_author_title", KATTR__MAX);
2545 if (kerr != KCGI_OK)
2546 goto done;
2547 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2548 if (kerr != KCGI_OK)
2549 goto done;
2550 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2551 if (kerr != KCGI_OK)
2552 goto done;
2553 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2554 KATTR_ID, "header_author", KATTR__MAX);
2555 if (kerr != KCGI_OK)
2556 goto done;
2557 kerr = khtml_puts(gw_trans->gw_html_req, str);
2558 if (kerr != KCGI_OK)
2559 goto done;
2560 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2561 done:
2562 if (error == NULL && kerr != KCGI_OK)
2563 error = gw_kcgi_error(kerr);
2564 return error;
2567 static const struct got_error *
2568 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2570 const struct got_error *error = NULL;
2571 enum kcgi_err kerr = KCGI_OK;
2573 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2574 KATTR_ID, "header_committer_title", KATTR__MAX);
2575 if (kerr != KCGI_OK)
2576 goto done;
2577 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2578 if (kerr != KCGI_OK)
2579 goto done;
2580 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2581 if (kerr != KCGI_OK)
2582 goto done;
2583 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2584 KATTR_ID, "header_committer", KATTR__MAX);
2585 if (kerr != KCGI_OK)
2586 goto done;
2587 kerr = khtml_puts(gw_trans->gw_html_req, str);
2588 if (kerr != KCGI_OK)
2589 goto done;
2590 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2591 done:
2592 if (error == NULL && kerr != KCGI_OK)
2593 error = gw_kcgi_error(kerr);
2594 return error;
2597 static const struct got_error *
2598 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2600 const struct got_error *error = NULL;
2601 enum kcgi_err kerr = KCGI_OK;
2603 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2604 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2605 if (kerr != KCGI_OK)
2606 goto done;
2607 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2608 if (kerr != KCGI_OK)
2609 goto done;
2610 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2611 if (kerr != KCGI_OK)
2612 goto done;
2613 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2614 KATTR_ID, "header_commit_msg", KATTR__MAX);
2615 if (kerr != KCGI_OK)
2616 goto done;
2617 kerr = khttp_puts(gw_trans->gw_req, str);
2618 if (kerr != KCGI_OK)
2619 goto done;
2620 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2621 done:
2622 if (error == NULL && kerr != KCGI_OK)
2623 error = gw_kcgi_error(kerr);
2624 return error;
2627 static const struct got_error *
2628 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2630 const struct got_error *error = NULL;
2631 enum kcgi_err kerr = KCGI_OK;
2633 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2634 KATTR_ID, "header_tree_title", KATTR__MAX);
2635 if (kerr != KCGI_OK)
2636 goto done;
2637 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2638 if (kerr != KCGI_OK)
2639 goto done;
2640 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2641 if (kerr != KCGI_OK)
2642 goto done;
2643 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2644 KATTR_ID, "header_tree", KATTR__MAX);
2645 if (kerr != KCGI_OK)
2646 goto done;
2647 kerr = khtml_puts(gw_trans->gw_html_req, str);
2648 if (kerr != KCGI_OK)
2649 goto done;
2650 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2651 done:
2652 if (error == NULL && kerr != KCGI_OK)
2653 error = gw_kcgi_error(kerr);
2654 return error;
2657 static const struct got_error *
2658 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2659 char *dir)
2661 const struct got_error *error = NULL;
2662 FILE *f = NULL;
2663 char *d_file = NULL;
2664 unsigned int len;
2665 size_t n;
2667 *description = NULL;
2668 if (gw_trans->gw_conf->got_show_repo_description == 0)
2669 return NULL;
2671 if (asprintf(&d_file, "%s/description", dir) == -1)
2672 return got_error_from_errno("asprintf");
2674 f = fopen(d_file, "re");
2675 if (f == NULL) {
2676 if (errno == ENOENT || errno == EACCES)
2677 return NULL;
2678 error = got_error_from_errno2("fopen", d_file);
2679 goto done;
2682 if (fseek(f, 0, SEEK_END) == -1) {
2683 error = got_ferror(f, GOT_ERR_IO);
2684 goto done;
2686 len = ftell(f);
2687 if (len == -1) {
2688 error = got_ferror(f, GOT_ERR_IO);
2689 goto done;
2691 if (fseek(f, 0, SEEK_SET) == -1) {
2692 error = got_ferror(f, GOT_ERR_IO);
2693 goto done;
2695 *description = calloc(len + 1, sizeof(**description));
2696 if (*description == NULL) {
2697 error = got_error_from_errno("calloc");
2698 goto done;
2701 n = fread(*description, 1, len, f);
2702 if (n == 0 && ferror(f))
2703 error = got_ferror(f, GOT_ERR_IO);
2704 done:
2705 if (f != NULL && fclose(f) == EOF && error == NULL)
2706 error = got_error_from_errno("fclose");
2707 free(d_file);
2708 return error;
2711 static const struct got_error *
2712 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2714 struct tm tm;
2715 time_t diff_time;
2716 char *years = "years ago", *months = "months ago";
2717 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2718 char *minutes = "minutes ago", *seconds = "seconds ago";
2719 char *now = "right now";
2720 char *s;
2721 char datebuf[29];
2723 *repo_age = NULL;
2725 switch (ref_tm) {
2726 case TM_DIFF:
2727 diff_time = time(NULL) - committer_time;
2728 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2729 if (asprintf(repo_age, "%lld %s",
2730 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2731 return got_error_from_errno("asprintf");
2732 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2733 if (asprintf(repo_age, "%lld %s",
2734 (diff_time / 60 / 60 / 24 / (365 / 12)),
2735 months) == -1)
2736 return got_error_from_errno("asprintf");
2737 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2738 if (asprintf(repo_age, "%lld %s",
2739 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2740 return got_error_from_errno("asprintf");
2741 } else if (diff_time > 60 * 60 * 24 * 2) {
2742 if (asprintf(repo_age, "%lld %s",
2743 (diff_time / 60 / 60 / 24), days) == -1)
2744 return got_error_from_errno("asprintf");
2745 } else if (diff_time > 60 * 60 * 2) {
2746 if (asprintf(repo_age, "%lld %s",
2747 (diff_time / 60 / 60), hours) == -1)
2748 return got_error_from_errno("asprintf");
2749 } else if (diff_time > 60 * 2) {
2750 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2751 minutes) == -1)
2752 return got_error_from_errno("asprintf");
2753 } else if (diff_time > 2) {
2754 if (asprintf(repo_age, "%lld %s", diff_time,
2755 seconds) == -1)
2756 return got_error_from_errno("asprintf");
2757 } else {
2758 if (asprintf(repo_age, "%s", now) == -1)
2759 return got_error_from_errno("asprintf");
2761 break;
2762 case TM_LONG:
2763 if (gmtime_r(&committer_time, &tm) == NULL)
2764 return got_error_from_errno("gmtime_r");
2766 s = asctime_r(&tm, datebuf);
2767 if (s == NULL)
2768 return got_error_from_errno("asctime_r");
2770 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2771 return got_error_from_errno("asprintf");
2772 break;
2774 return NULL;
2777 static const struct got_error *
2778 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2779 const char *refname, int ref_tm)
2781 const struct got_error *error = NULL;
2782 struct got_repository *repo = NULL;
2783 struct got_commit_object *commit = NULL;
2784 struct got_reflist_head refs;
2785 struct got_reflist_entry *re;
2786 time_t committer_time = 0, cmp_time = 0;
2788 *repo_age = NULL;
2789 TAILQ_INIT(&refs);
2791 if (gw_trans->gw_conf->got_show_repo_age == 0)
2792 return NULL;
2794 if (gw_trans->repo)
2795 repo = gw_trans->repo;
2796 else {
2797 error = got_repo_open(&repo, dir, NULL);
2798 if (error)
2799 return error;
2802 error = got_ref_list(&refs, repo, "refs/heads",
2803 got_ref_cmp_by_name, NULL);
2804 if (error)
2805 goto done;
2808 * Find the youngest branch tip in the repository, or the age of
2809 * the a specific branch tip if a name was provided by the caller.
2811 TAILQ_FOREACH(re, &refs, entry) {
2812 struct got_object_id *id = NULL;
2814 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
2815 continue;
2817 error = got_ref_resolve(&id, repo, re->ref);
2818 if (error)
2819 goto done;
2821 error = got_object_open_as_commit(&commit, repo, id);
2822 free(id);
2823 if (error)
2824 goto done;
2826 committer_time =
2827 got_object_commit_get_committer_time(commit);
2828 got_object_commit_close(commit);
2829 if (cmp_time < committer_time)
2830 cmp_time = committer_time;
2832 if (refname)
2833 break;
2836 if (cmp_time != 0) {
2837 committer_time = cmp_time;
2838 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2840 done:
2841 got_ref_list_free(&refs);
2842 if (gw_trans->repo == NULL) {
2843 const struct got_error *close_err = got_repo_close(repo);
2844 if (error == NULL)
2845 error = close_err;
2847 return error;
2850 static const struct got_error *
2851 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2853 const struct got_error *error;
2854 FILE *f = NULL, *f1 = NULL, *f2 = NULL;
2855 struct got_object_id *id1 = NULL, *id2 = NULL;
2856 char *label1 = NULL, *label2 = NULL, *line = NULL;
2857 int obj_type;
2858 size_t linesize = 0;
2859 ssize_t linelen;
2860 enum kcgi_err kerr = KCGI_OK;
2862 f = got_opentemp();
2863 if (f == NULL)
2864 return NULL;
2866 f1 = got_opentemp();
2867 if (f1 == NULL) {
2868 error = got_error_from_errno("got_opentemp");
2869 goto done;
2872 f2 = got_opentemp();
2873 if (f2 == NULL) {
2874 error = got_error_from_errno("got_opentemp");
2875 goto done;
2878 if (header->parent_id != NULL &&
2879 strncmp(header->parent_id, "/dev/null", 9) != 0) {
2880 error = got_repo_match_object_id(&id1, &label1,
2881 header->parent_id, GOT_OBJ_TYPE_ANY,
2882 &header->refs, gw_trans->repo);
2883 if (error)
2884 goto done;
2887 error = got_repo_match_object_id(&id2, &label2,
2888 header->commit_id, GOT_OBJ_TYPE_ANY, &header->refs,
2889 gw_trans->repo);
2890 if (error)
2891 goto done;
2893 error = got_object_get_type(&obj_type, gw_trans->repo, id2);
2894 if (error)
2895 goto done;
2896 switch (obj_type) {
2897 case GOT_OBJ_TYPE_BLOB:
2898 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
2899 id1, id2, NULL, NULL, 3, 0, 0, gw_trans->repo, f);
2900 break;
2901 case GOT_OBJ_TYPE_TREE:
2902 error = got_diff_objects_as_trees(NULL, NULL, f1, f2,
2903 id1, id2, NULL, "", "", 3, 0, 0, gw_trans->repo, f);
2904 break;
2905 case GOT_OBJ_TYPE_COMMIT:
2906 error = got_diff_objects_as_commits(NULL, NULL, f1, f2,
2907 id1, id2, NULL, 3, 0, 0, gw_trans->repo, f);
2908 break;
2909 default:
2910 error = got_error(GOT_ERR_OBJ_TYPE);
2912 if (error)
2913 goto done;
2915 if (fseek(f, 0, SEEK_SET) == -1) {
2916 error = got_ferror(f, GOT_ERR_IO);
2917 goto done;
2920 while ((linelen = getline(&line, &linesize, f)) != -1) {
2921 error = gw_colordiff_line(gw_trans, line);
2922 if (error)
2923 goto done;
2924 /* XXX: KHTML_PRETTY breaks this */
2925 kerr = khtml_puts(gw_trans->gw_html_req, line);
2926 if (kerr != KCGI_OK)
2927 goto done;
2928 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2929 if (kerr != KCGI_OK)
2930 goto done;
2932 if (linelen == -1 && ferror(f))
2933 error = got_error_from_errno("getline");
2934 done:
2935 if (f && fclose(f) == EOF && error == NULL)
2936 error = got_error_from_errno("fclose");
2937 if (f1 && fclose(f1) == EOF && error == NULL)
2938 error = got_error_from_errno("fclose");
2939 if (f2 && fclose(f2) == EOF && error == NULL)
2940 error = got_error_from_errno("fclose");
2941 free(line);
2942 free(label1);
2943 free(label2);
2944 free(id1);
2945 free(id2);
2947 if (error == NULL && kerr != KCGI_OK)
2948 error = gw_kcgi_error(kerr);
2949 return error;
2952 static const struct got_error *
2953 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2955 const struct got_error *error = NULL, *close_err;
2956 struct got_repository *repo;
2957 const char *gitconfig_owner;
2959 *owner = NULL;
2961 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2962 return NULL;
2964 error = got_repo_open(&repo, dir, NULL);
2965 if (error)
2966 return error;
2967 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2968 if (gitconfig_owner) {
2969 *owner = strdup(gitconfig_owner);
2970 if (*owner == NULL)
2971 error = got_error_from_errno("strdup");
2973 close_err = got_repo_close(repo);
2974 if (error == NULL)
2975 error = close_err;
2976 return error;
2979 static const struct got_error *
2980 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2982 const struct got_error *error = NULL;
2983 FILE *f;
2984 char *d_file = NULL;
2985 unsigned int len;
2986 size_t n;
2988 *url = NULL;
2990 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2991 return got_error_from_errno("asprintf");
2993 f = fopen(d_file, "re");
2994 if (f == NULL) {
2995 if (errno != ENOENT && errno != EACCES)
2996 error = got_error_from_errno2("fopen", d_file);
2997 goto done;
3000 if (fseek(f, 0, SEEK_END) == -1) {
3001 error = got_ferror(f, GOT_ERR_IO);
3002 goto done;
3004 len = ftell(f);
3005 if (len == -1) {
3006 error = got_ferror(f, GOT_ERR_IO);
3007 goto done;
3009 if (fseek(f, 0, SEEK_SET) == -1) {
3010 error = got_ferror(f, GOT_ERR_IO);
3011 goto done;
3014 *url = calloc(len + 1, sizeof(**url));
3015 if (*url == NULL) {
3016 error = got_error_from_errno("calloc");
3017 goto done;
3020 n = fread(*url, 1, len, f);
3021 if (n == 0 && ferror(f))
3022 error = got_ferror(f, GOT_ERR_IO);
3023 done:
3024 if (f && fclose(f) == EOF && error == NULL)
3025 error = got_error_from_errno("fclose");
3026 free(d_file);
3027 return NULL;
3030 static const struct got_error *
3031 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
3032 int limit, int tag_type)
3034 const struct got_error *error = NULL;
3035 struct got_reflist_head refs;
3036 struct got_reflist_entry *re;
3037 char *age = NULL;
3038 char *id_str = NULL, *newline, *href_commits = NULL;
3039 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
3040 struct got_tag_object *tag = NULL;
3041 enum kcgi_err kerr = KCGI_OK;
3042 int summary_header_displayed = 0, chk_next = 0;
3043 int tag_count = 0, commit_found = 0, c_cnt = 0;
3045 TAILQ_INIT(&refs);
3047 error = got_ref_list(&refs, gw_trans->repo, "refs/tags",
3048 got_ref_cmp_tags, gw_trans->repo);
3049 if (error)
3050 goto done;
3052 TAILQ_FOREACH(re, &refs, entry) {
3053 const char *refname;
3054 const char *tagger;
3055 const char *tag_commit;
3056 time_t tagger_time;
3057 struct got_object_id *id;
3058 struct got_commit_object *commit = NULL;
3060 refname = got_ref_get_name(re->ref);
3061 if (strncmp(refname, "refs/tags/", 10) != 0)
3062 continue;
3063 refname += 10;
3065 error = got_ref_resolve(&id, gw_trans->repo, re->ref);
3066 if (error)
3067 goto done;
3069 error = got_object_open_as_tag(&tag, gw_trans->repo, id);
3070 if (error) {
3071 if (error->code != GOT_ERR_OBJ_TYPE) {
3072 free(id);
3073 goto done;
3075 /* "lightweight" tag */
3076 error = got_object_open_as_commit(&commit,
3077 gw_trans->repo, id);
3078 if (error) {
3079 free(id);
3080 goto done;
3082 tagger = got_object_commit_get_committer(commit);
3083 tagger_time =
3084 got_object_commit_get_committer_time(commit);
3085 error = got_object_id_str(&id_str, id);
3086 free(id);
3087 } else {
3088 free(id);
3089 tagger = got_object_tag_get_tagger(tag);
3090 tagger_time = got_object_tag_get_tagger_time(tag);
3091 error = got_object_id_str(&id_str,
3092 got_object_tag_get_object_id(tag));
3094 if (error)
3095 goto done;
3097 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
3098 strlen(id_str)) != 0)
3099 continue;
3101 if (tag_type == TAGBRIEF && gw_trans->commit_id &&
3102 commit_found == 0 && strncmp(id_str, gw_trans->commit_id,
3103 strlen(id_str)) != 0)
3104 continue;
3105 else
3106 commit_found = 1;
3108 tag_count++;
3110 if (chk_next) {
3111 gw_trans->next_id = strdup(id_str);
3112 if (gw_trans->next_id == NULL)
3113 error = got_error_from_errno("strdup");
3114 goto prev;
3117 if (commit) {
3118 error = got_object_commit_get_logmsg(&tag_commit0,
3119 commit);
3120 if (error)
3121 goto done;
3122 got_object_commit_close(commit);
3123 } else {
3124 tag_commit0 = strdup(got_object_tag_get_message(tag));
3125 if (tag_commit0 == NULL) {
3126 error = got_error_from_errno("strdup");
3127 goto done;
3131 tag_commit = tag_commit0;
3132 while (*tag_commit == '\n')
3133 tag_commit++;
3135 switch (tag_type) {
3136 case TAGBRIEF:
3137 newline = strchr(tag_commit, '\n');
3138 if (newline)
3139 *newline = '\0';
3141 if (summary_header_displayed == 0) {
3142 kerr = khtml_attr(gw_trans->gw_html_req,
3143 KELEM_DIV, KATTR_ID,
3144 "summary_tags_title_wrapper", KATTR__MAX);
3145 if (kerr != KCGI_OK)
3146 goto done;
3147 kerr = khtml_attr(gw_trans->gw_html_req,
3148 KELEM_DIV, KATTR_ID,
3149 "summary_tags_title", KATTR__MAX);
3150 if (kerr != KCGI_OK)
3151 goto done;
3152 kerr = khtml_puts(gw_trans->gw_html_req,
3153 "Tags");
3154 if (kerr != KCGI_OK)
3155 goto done;
3156 kerr = khtml_closeelem(gw_trans->gw_html_req,
3157 2);
3158 if (kerr != KCGI_OK)
3159 goto done;
3160 kerr = khtml_attr(gw_trans->gw_html_req,
3161 KELEM_DIV, KATTR_ID,
3162 "summary_tags_content", KATTR__MAX);
3163 if (kerr != KCGI_OK)
3164 goto done;
3165 summary_header_displayed = 1;
3168 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3169 KATTR_ID, "tag_wrapper", KATTR__MAX);
3170 if (kerr != KCGI_OK)
3171 goto done;
3172 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3173 KATTR_ID, "tag_age", KATTR__MAX);
3174 if (kerr != KCGI_OK)
3175 goto done;
3176 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
3177 if (error)
3178 goto done;
3179 kerr = khtml_puts(gw_trans->gw_html_req,
3180 age ? age : "");
3181 if (kerr != KCGI_OK)
3182 goto done;
3183 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3184 if (kerr != KCGI_OK)
3185 goto done;
3186 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3187 KATTR_ID, "tag", KATTR__MAX);
3188 if (kerr != KCGI_OK)
3189 goto done;
3190 kerr = khtml_puts(gw_trans->gw_html_req, refname);
3191 if (kerr != KCGI_OK)
3192 goto done;
3193 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3194 if (kerr != KCGI_OK)
3195 goto done;
3196 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3197 KATTR_ID, "tag_name", KATTR__MAX);
3198 if (kerr != KCGI_OK)
3199 goto done;
3201 href_tag = khttp_urlpart(NULL, NULL, "gotweb", "path",
3202 gw_trans->repo_name, "action", "tag", "commit",
3203 id_str, NULL);
3204 if (href_tag == NULL) {
3205 error = got_error_from_errno("khttp_urlpart");
3206 goto done;
3208 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3209 KATTR_HREF, href_tag, KATTR__MAX);
3210 if (kerr != KCGI_OK)
3211 goto done;
3212 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
3213 if (kerr != KCGI_OK)
3214 goto done;
3215 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3216 if (kerr != KCGI_OK)
3217 goto done;
3219 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3220 KATTR_ID, "navs_wrapper", KATTR__MAX);
3221 if (kerr != KCGI_OK)
3222 goto done;
3223 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3224 KATTR_ID, "navs", KATTR__MAX);
3225 if (kerr != KCGI_OK)
3226 goto done;
3228 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3229 KATTR_HREF, href_tag, KATTR__MAX);
3230 if (kerr != KCGI_OK)
3231 goto done;
3232 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
3233 if (kerr != KCGI_OK)
3234 goto done;
3235 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3236 if (kerr != KCGI_OK)
3237 goto done;
3239 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3240 if (kerr != KCGI_OK)
3241 goto done;
3243 href_briefs = khttp_urlpart(NULL, NULL, "gotweb",
3244 "path", gw_trans->repo_name, "action", "briefs",
3245 "commit", id_str, NULL);
3246 if (href_briefs == NULL) {
3247 error = got_error_from_errno("khttp_urlpart");
3248 goto done;
3250 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3251 KATTR_HREF, href_briefs, KATTR__MAX);
3252 if (kerr != KCGI_OK)
3253 goto done;
3254 kerr = khtml_puts(gw_trans->gw_html_req,
3255 "commit briefs");
3256 if (kerr != KCGI_OK)
3257 goto done;
3258 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3259 if (kerr != KCGI_OK)
3260 goto done;
3262 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3263 if (kerr != KCGI_OK)
3264 goto done;
3266 href_commits = khttp_urlpart(NULL, NULL, "gotweb",
3267 "path", gw_trans->repo_name, "action", "commits",
3268 "commit", id_str, NULL);
3269 if (href_commits == NULL) {
3270 error = got_error_from_errno("khttp_urlpart");
3271 goto done;
3273 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3274 KATTR_HREF, href_commits, KATTR__MAX);
3275 if (kerr != KCGI_OK)
3276 goto done;
3277 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
3278 if (kerr != KCGI_OK)
3279 goto done;
3280 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3281 if (kerr != KCGI_OK)
3282 goto done;
3284 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3285 KATTR_ID, "dotted_line", KATTR__MAX);
3286 if (kerr != KCGI_OK)
3287 goto done;
3288 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3289 if (kerr != KCGI_OK)
3290 goto done;
3291 break;
3292 case TAGFULL:
3293 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3294 KATTR_ID, "tag_info_date_title", KATTR__MAX);
3295 if (kerr != KCGI_OK)
3296 goto done;
3297 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
3298 if (kerr != KCGI_OK)
3299 goto done;
3300 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3301 if (kerr != KCGI_OK)
3302 goto done;
3303 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3304 KATTR_ID, "tag_info_date", KATTR__MAX);
3305 if (kerr != KCGI_OK)
3306 goto done;
3307 error = gw_get_time_str(&age, tagger_time, TM_LONG);
3308 if (error)
3309 goto done;
3310 kerr = khtml_puts(gw_trans->gw_html_req,
3311 age ? age : "");
3312 if (kerr != KCGI_OK)
3313 goto done;
3314 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3315 if (kerr != KCGI_OK)
3316 goto done;
3318 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3319 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
3320 if (kerr != KCGI_OK)
3321 goto done;
3322 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
3323 if (kerr != KCGI_OK)
3324 goto done;
3325 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3326 if (kerr != KCGI_OK)
3327 goto done;
3328 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3329 KATTR_ID, "tag_info_date", KATTR__MAX);
3330 if (kerr != KCGI_OK)
3331 goto done;
3332 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
3333 if (kerr != KCGI_OK)
3334 goto done;
3335 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3336 if (kerr != KCGI_OK)
3337 goto done;
3339 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3340 KATTR_ID, "tag_info", KATTR__MAX);
3341 if (kerr != KCGI_OK)
3342 goto done;
3343 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
3344 if (kerr != KCGI_OK)
3345 goto done;
3346 break;
3347 default:
3348 break;
3350 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3351 if (kerr != KCGI_OK)
3352 goto done;
3354 if (limit && --limit == 0)
3355 chk_next = 1;
3357 if (tag)
3358 got_object_tag_close(tag);
3359 tag = NULL;
3360 free(id_str);
3361 id_str = NULL;
3362 free(age);
3363 age = NULL;
3364 free(tag_commit0);
3365 tag_commit0 = NULL;
3366 free(href_tag);
3367 href_tag = NULL;
3368 free(href_briefs);
3369 href_briefs = NULL;
3370 free(href_commits);
3371 href_commits = NULL;
3373 if (tag_count == 0) {
3374 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3375 "summary_tags_title_wrapper", KATTR__MAX);
3376 if (kerr != KCGI_OK)
3377 goto done;
3378 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3379 "summary_tags_title", KATTR__MAX);
3380 if (kerr != KCGI_OK)
3381 goto done;
3382 kerr = khtml_puts(gw_trans->gw_html_req, "Tags");
3383 if (kerr != KCGI_OK)
3384 goto done;
3385 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3386 if (kerr != KCGI_OK)
3387 goto done;
3388 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3389 "summary_tags_content", KATTR__MAX);
3390 if (kerr != KCGI_OK)
3391 goto done;
3392 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3393 "tags_info", KATTR__MAX);
3394 if (kerr != KCGI_OK)
3395 goto done;
3396 kerr = khttp_puts(gw_trans->gw_req,
3397 "There are no tags for this repo.");
3398 if (kerr != KCGI_OK)
3399 goto done;
3400 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3401 goto done;
3403 prev:
3404 commit_found = 0;
3405 TAILQ_FOREACH_REVERSE(re, &refs, got_reflist_head, entry) {
3406 const char *refname;
3407 struct got_object_id *id;
3408 struct got_commit_object *commit = NULL;
3410 refname = got_ref_get_name(re->ref);
3411 if (strncmp(refname, "refs/tags/", 10) != 0)
3412 continue;
3413 refname += 10;
3415 error = got_ref_resolve(&id, gw_trans->repo, re->ref);
3416 if (error)
3417 goto done;
3419 error = got_object_open_as_tag(&tag, gw_trans->repo, id);
3420 if (error) {
3421 if (error->code != GOT_ERR_OBJ_TYPE) {
3422 free(id);
3423 goto done;
3425 /* "lightweight" tag */
3426 error = got_object_open_as_commit(&commit,
3427 gw_trans->repo, id);
3428 if (error) {
3429 free(id);
3430 goto done;
3432 error = got_object_id_str(&id_str, id);
3433 free(id);
3434 } else {
3435 free(id);
3436 error = got_object_id_str(&id_str,
3437 got_object_tag_get_object_id(tag));
3439 if (error)
3440 goto done;
3442 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
3443 strlen(id_str)) != 0)
3444 continue;
3446 if (commit_found == 0 && tag_type == TAGBRIEF &&
3447 gw_trans->commit_id != NULL &&
3448 strncmp(id_str, gw_trans->commit_id, strlen(id_str)) != 0)
3449 continue;
3450 else
3451 commit_found = 1;
3453 if (gw_trans->commit_id != NULL &&
3454 strcmp(id_str, gw_trans->commit_id) != 0 &&
3455 (re == TAILQ_FIRST(&refs) ||
3456 c_cnt == gw_trans->gw_conf->got_max_commits_display)) {
3457 gw_trans->prev_id = strdup(id_str);
3458 if (gw_trans->prev_id == NULL) {
3459 error = got_error_from_errno("strdup");
3460 goto done;
3462 break;
3464 c_cnt++;
3466 done:
3467 if (tag)
3468 got_object_tag_close(tag);
3469 free(id_str);
3470 free(age);
3471 free(tag_commit0);
3472 free(href_tag);
3473 free(href_briefs);
3474 free(href_commits);
3475 got_ref_list_free(&refs);
3476 if (error == NULL && kerr != KCGI_OK)
3477 error = gw_kcgi_error(kerr);
3478 return error;
3481 static void
3482 gw_free_header(struct gw_header *header)
3484 free(header->path);
3485 free(header->author);
3486 free(header->committer);
3487 free(header->refs_str);
3488 free(header->commit_id);
3489 free(header->parent_id);
3490 free(header->tree_id);
3491 free(header->commit_msg);
3494 static struct gw_header *
3495 gw_init_header()
3497 struct gw_header *header;
3499 header = malloc(sizeof(*header));
3500 if (header == NULL)
3501 return NULL;
3503 header->path = NULL;
3504 TAILQ_INIT(&header->refs);
3506 header->refs_str = NULL;
3507 header->commit_id = NULL;
3508 header->committer = NULL;
3509 header->author = NULL;
3510 header->parent_id = NULL;
3511 header->tree_id = NULL;
3512 header->commit_msg = NULL;
3514 return header;
3517 static const struct got_error *
3518 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
3519 int limit, struct got_object_id *id)
3521 const struct got_error *error = NULL;
3522 struct got_commit_graph *graph = NULL;
3523 struct got_commit_object *commit = NULL;
3524 int chk_next = 0, chk_multi = 0, c_cnt = 0, commit_found = 0;
3525 struct gw_header *t_header = NULL;
3527 error = got_commit_graph_open(&graph, header->path, 0);
3528 if (error)
3529 return error;
3531 error = got_commit_graph_iter_start(graph, id, gw_trans->repo, NULL,
3532 NULL);
3533 if (error)
3534 goto err;
3536 for (;;) {
3537 error = got_commit_graph_iter_next(&id, graph, gw_trans->repo,
3538 NULL, NULL);
3539 if (error) {
3540 if (error->code == GOT_ERR_ITER_COMPLETED)
3541 error = NULL;
3542 goto done;
3544 if (id == NULL)
3545 goto err;
3547 error = got_object_open_as_commit(&commit, gw_trans->repo, id);
3548 if (error)
3549 goto err;
3550 if (limit == 1 && chk_multi == 0 &&
3551 gw_trans->gw_conf->got_max_commits_display != 1) {
3552 error = gw_get_commit(gw_trans, header, commit, id);
3553 if (error)
3554 goto err;
3555 commit_found = 1;
3556 } else {
3557 chk_multi = 1;
3558 struct gw_header *n_header = NULL;
3559 if ((n_header = gw_init_header()) == NULL) {
3560 error = got_error_from_errno("malloc");
3561 goto err;
3563 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3564 entry);
3565 error = got_ref_list(&n_header->refs, gw_trans->repo,
3566 NULL, got_ref_cmp_by_name, NULL);
3567 if (error)
3568 goto err;
3570 error = gw_get_commit(gw_trans, n_header, commit, id);
3571 if (error)
3572 goto err;
3573 got_ref_list_free(&n_header->refs);
3575 if (gw_trans->commit_id != NULL) {
3576 if (strcmp(gw_trans->commit_id,
3577 n_header->commit_id) == 0)
3578 commit_found = 1;
3579 } else
3580 commit_found = 1;
3583 * check for one more commit before breaking,
3584 * so we know whether to navigate through gw_briefs
3585 * gw_commits and gw_summary
3587 if (chk_next && (gw_trans->action == GW_BRIEFS ||
3588 gw_trans->action == GW_COMMITS ||
3589 gw_trans->action == GW_SUMMARY)) {
3590 gw_trans->next_id = strdup(n_header->commit_id);
3591 if (gw_trans->next_id == NULL)
3592 error = got_error_from_errno("strdup");
3593 TAILQ_REMOVE(&gw_trans->gw_headers, n_header,
3594 entry);
3595 goto done;
3599 if (commit_found == 1 && (error || (limit && --limit == 0))) {
3600 if (chk_multi == 0)
3601 break;
3602 chk_next = 1;
3605 done:
3606 if (gw_trans->prev_id == NULL && gw_trans->commit_id != NULL &&
3607 (gw_trans->action == GW_BRIEFS || gw_trans->action == GW_COMMITS)) {
3608 commit_found = 0;
3609 TAILQ_FOREACH_REVERSE(t_header, &gw_trans->gw_headers,
3610 headers, entry) {
3611 if (commit_found == 0 &&
3612 strcmp(gw_trans->commit_id,
3613 t_header->commit_id) != 0)
3614 continue;
3615 else
3616 commit_found = 1;
3617 if (gw_trans->commit_id != NULL &&
3618 strcmp(gw_trans->commit_id,
3619 t_header->commit_id) != 0 &&
3620 (c_cnt == gw_trans->gw_conf->got_max_commits_display
3621 || t_header ==
3622 TAILQ_FIRST(&gw_trans->gw_headers))) {
3623 gw_trans->prev_id = strdup(t_header->commit_id);
3624 if (gw_trans->prev_id == NULL)
3625 error = got_error_from_errno("strdup");
3626 break;
3628 c_cnt++;
3631 err:
3632 if (commit != NULL)
3633 got_object_commit_close(commit);
3634 if (graph)
3635 got_commit_graph_close(graph);
3636 return error;
3639 static const struct got_error *
3640 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header,
3641 struct got_commit_object *commit, struct got_object_id *id)
3643 const struct got_error *error = NULL;
3644 struct got_reflist_entry *re;
3645 struct got_object_id *id2 = NULL;
3646 struct got_object_qid *parent_id;
3647 char *commit_msg = NULL, *commit_msg0;
3649 /*print commit*/
3650 TAILQ_FOREACH(re, &header->refs, entry) {
3651 char *s;
3652 const char *name;
3653 struct got_tag_object *tag = NULL;
3654 struct got_object_id *ref_id;
3655 int cmp;
3657 if (got_ref_is_symbolic(re->ref))
3658 continue;
3660 name = got_ref_get_name(re->ref);
3661 if (strncmp(name, "refs/", 5) == 0)
3662 name += 5;
3663 if (strncmp(name, "got/", 4) == 0)
3664 continue;
3665 if (strncmp(name, "heads/", 6) == 0)
3666 name += 6;
3667 if (strncmp(name, "remotes/", 8) == 0) {
3668 name += 8;
3669 s = strstr(name, "/" GOT_REF_HEAD);
3670 if (s != NULL && s[strlen(s)] == '\0')
3671 continue;
3673 error = got_ref_resolve(&ref_id, gw_trans->repo, re->ref);
3674 if (error)
3675 return error;
3676 if (strncmp(name, "tags/", 5) == 0) {
3677 error = got_object_open_as_tag(&tag, gw_trans->repo,
3678 ref_id);
3679 if (error) {
3680 if (error->code != GOT_ERR_OBJ_TYPE) {
3681 free(ref_id);
3682 continue;
3685 * Ref points at something other
3686 * than a tag.
3688 error = NULL;
3689 tag = NULL;
3692 cmp = got_object_id_cmp(tag ?
3693 got_object_tag_get_object_id(tag) : ref_id, id);
3694 free(ref_id);
3695 if (tag)
3696 got_object_tag_close(tag);
3697 if (cmp != 0)
3698 continue;
3699 s = header->refs_str;
3700 if (asprintf(&header->refs_str, "%s%s%s", s ? s : "",
3701 s ? ", " : "", name) == -1) {
3702 error = got_error_from_errno("asprintf");
3703 free(s);
3704 header->refs_str = NULL;
3705 return error;
3707 free(s);
3710 error = got_object_id_str(&header->commit_id, id);
3711 if (error)
3712 return error;
3714 error = got_object_id_str(&header->tree_id,
3715 got_object_commit_get_tree_id(commit));
3716 if (error)
3717 return error;
3719 if (gw_trans->action == GW_DIFF) {
3720 parent_id = STAILQ_FIRST(
3721 got_object_commit_get_parent_ids(commit));
3722 if (parent_id != NULL) {
3723 id2 = got_object_id_dup(&parent_id->id);
3724 free (parent_id);
3725 error = got_object_id_str(&header->parent_id, id2);
3726 if (error)
3727 return error;
3728 free(id2);
3729 } else {
3730 header->parent_id = strdup("/dev/null");
3731 if (header->parent_id == NULL) {
3732 error = got_error_from_errno("strdup");
3733 return error;
3738 header->committer_time =
3739 got_object_commit_get_committer_time(commit);
3741 header->author =
3742 strdup(got_object_commit_get_author(commit));
3743 if (header->author == NULL) {
3744 error = got_error_from_errno("strdup");
3745 return error;
3747 header->committer =
3748 strdup(got_object_commit_get_committer(commit));
3749 if (header->committer == NULL) {
3750 error = got_error_from_errno("strdup");
3751 return error;
3753 error = got_object_commit_get_logmsg(&commit_msg0, commit);
3754 if (error)
3755 return error;
3757 commit_msg = commit_msg0;
3758 while (*commit_msg == '\n')
3759 commit_msg++;
3761 header->commit_msg = strdup(commit_msg);
3762 if (header->commit_msg == NULL)
3763 error = got_error_from_errno("strdup");
3764 free(commit_msg0);
3765 return error;
3768 static const struct got_error *
3769 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3771 const struct got_error *error = NULL;
3772 char *in_repo_path = NULL;
3773 struct got_object_id *id = NULL;
3774 struct got_reference *ref;
3776 error = got_repo_open(&gw_trans->repo, gw_trans->repo_path, NULL);
3777 if (error)
3778 return error;
3780 if (gw_trans->commit_id == NULL || gw_trans->action == GW_COMMITS ||
3781 gw_trans->action == GW_BRIEFS || gw_trans->action == GW_SUMMARY ||
3782 gw_trans->action == GW_TAGS) {
3783 error = got_ref_open(&ref, gw_trans->repo,
3784 gw_trans->headref, 0);
3785 if (error)
3786 return error;
3788 error = got_ref_resolve(&id, gw_trans->repo, ref);
3789 got_ref_close(ref);
3790 if (error)
3791 return error;
3792 } else {
3793 error = got_ref_open(&ref, gw_trans->repo,
3794 gw_trans->commit_id, 0);
3795 if (error == NULL) {
3796 int obj_type;
3797 error = got_ref_resolve(&id, gw_trans->repo, ref);
3798 got_ref_close(ref);
3799 if (error)
3800 return error;
3801 error = got_object_get_type(&obj_type, gw_trans->repo,
3802 id);
3803 if (error)
3804 goto done;
3805 if (obj_type == GOT_OBJ_TYPE_TAG) {
3806 struct got_tag_object *tag;
3807 error = got_object_open_as_tag(&tag,
3808 gw_trans->repo, id);
3809 if (error)
3810 goto done;
3811 if (got_object_tag_get_object_type(tag) !=
3812 GOT_OBJ_TYPE_COMMIT) {
3813 got_object_tag_close(tag);
3814 error = got_error(GOT_ERR_OBJ_TYPE);
3815 goto done;
3817 free(id);
3818 id = got_object_id_dup(
3819 got_object_tag_get_object_id(tag));
3820 if (id == NULL)
3821 error = got_error_from_errno(
3822 "got_object_id_dup");
3823 got_object_tag_close(tag);
3824 if (error)
3825 goto done;
3826 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3827 error = got_error(GOT_ERR_OBJ_TYPE);
3828 goto done;
3831 error = got_repo_match_object_id_prefix(&id,
3832 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT,
3833 gw_trans->repo);
3834 if (error)
3835 goto done;
3838 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
3839 gw_trans->repo_path);
3840 if (error)
3841 goto done;
3843 if (in_repo_path) {
3844 header->path = strdup(in_repo_path);
3845 if (header->path == NULL) {
3846 error = got_error_from_errno("strdup");
3847 goto done;
3851 error = got_ref_list(&header->refs, gw_trans->repo, NULL,
3852 got_ref_cmp_by_name, NULL);
3853 if (error)
3854 goto done;
3856 error = gw_get_commits(gw_trans, header, limit, id);
3857 done:
3858 got_ref_list_free(&header->refs);
3859 free(id);
3860 free(in_repo_path);
3861 return error;
3864 struct blame_line {
3865 int annotated;
3866 char *id_str;
3867 char *committer;
3868 char datebuf[11]; /* YYYY-MM-DD + NUL */
3871 struct gw_blame_cb_args {
3872 struct blame_line *lines;
3873 int nlines;
3874 int nlines_prec;
3875 int lineno_cur;
3876 off_t *line_offsets;
3877 FILE *f;
3878 struct got_repository *repo;
3879 struct gw_trans *gw_trans;
3882 static const struct got_error *
3883 gw_blame_cb(void *arg, int nlines, int lineno,
3884 struct got_commit_object *commit, struct got_object_id *id)
3886 const struct got_error *err = NULL;
3887 struct gw_blame_cb_args *a = arg;
3888 struct blame_line *bline;
3889 char *line = NULL;
3890 size_t linesize = 0;
3891 off_t offset;
3892 struct tm tm;
3893 time_t committer_time;
3894 enum kcgi_err kerr = KCGI_OK;
3896 if (nlines != a->nlines ||
3897 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3898 return got_error(GOT_ERR_RANGE);
3900 if (lineno == -1)
3901 return NULL; /* no change in this commit */
3903 /* Annotate this line. */
3904 bline = &a->lines[lineno - 1];
3905 if (bline->annotated)
3906 return NULL;
3907 err = got_object_id_str(&bline->id_str, id);
3908 if (err)
3909 return err;
3911 bline->committer = strdup(got_object_commit_get_committer(commit));
3912 if (bline->committer == NULL) {
3913 err = got_error_from_errno("strdup");
3914 goto done;
3917 committer_time = got_object_commit_get_committer_time(commit);
3918 if (gmtime_r(&committer_time, &tm) == NULL)
3919 return got_error_from_errno("gmtime_r");
3920 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3921 &tm) == 0) {
3922 err = got_error(GOT_ERR_NO_SPACE);
3923 goto done;
3925 bline->annotated = 1;
3927 /* Print lines annotated so far. */
3928 bline = &a->lines[a->lineno_cur - 1];
3929 if (!bline->annotated)
3930 goto done;
3932 offset = a->line_offsets[a->lineno_cur - 1];
3933 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3934 err = got_error_from_errno("fseeko");
3935 goto done;
3938 while (bline->annotated) {
3939 char *smallerthan, *at, *nl, *committer;
3940 char *href_diff = NULL;
3941 size_t len;
3943 if (getline(&line, &linesize, a->f) == -1) {
3944 if (ferror(a->f))
3945 err = got_error_from_errno("getline");
3946 break;
3949 committer = bline->committer;
3950 smallerthan = strchr(committer, '<');
3951 if (smallerthan && smallerthan[1] != '\0')
3952 committer = smallerthan + 1;
3953 at = strchr(committer, '@');
3954 if (at)
3955 *at = '\0';
3956 len = strlen(committer);
3957 if (len >= 9)
3958 committer[8] = '\0';
3960 nl = strchr(line, '\n');
3961 if (nl)
3962 *nl = '\0';
3964 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3965 "blame_wrapper", KATTR__MAX);
3966 if (kerr != KCGI_OK)
3967 goto err;
3968 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3969 "blame_number", KATTR__MAX);
3970 if (kerr != KCGI_OK)
3971 goto err;
3972 kerr = khtml_printf(a->gw_trans->gw_html_req, "%.*d",
3973 a->nlines_prec, a->lineno_cur);
3974 if (kerr != KCGI_OK)
3975 goto err;
3976 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3977 if (kerr != KCGI_OK)
3978 goto err;
3980 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3981 "blame_hash", KATTR__MAX);
3982 if (kerr != KCGI_OK)
3983 goto err;
3985 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
3986 a->gw_trans->repo_name, "action", "diff", "commit",
3987 bline->id_str, NULL);
3988 if (href_diff == NULL) {
3989 err = got_error_from_errno("khttp_urlpart");
3990 goto done;
3992 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3993 KATTR_HREF, href_diff, KATTR__MAX);
3994 if (kerr != KCGI_OK)
3995 goto done;
3996 kerr = khtml_printf(a->gw_trans->gw_html_req, "%.8s",
3997 bline->id_str);
3998 if (kerr != KCGI_OK)
3999 goto err;
4000 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
4001 if (kerr != KCGI_OK)
4002 goto err;
4004 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4005 "blame_date", KATTR__MAX);
4006 if (kerr != KCGI_OK)
4007 goto err;
4008 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
4009 if (kerr != KCGI_OK)
4010 goto err;
4011 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
4012 if (kerr != KCGI_OK)
4013 goto err;
4015 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4016 "blame_author", KATTR__MAX);
4017 if (kerr != KCGI_OK)
4018 goto err;
4019 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
4020 if (kerr != KCGI_OK)
4021 goto err;
4022 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
4023 if (kerr != KCGI_OK)
4024 goto err;
4026 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4027 "blame_code", KATTR__MAX);
4028 if (kerr != KCGI_OK)
4029 goto err;
4030 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
4031 if (kerr != KCGI_OK)
4032 goto err;
4033 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
4034 if (kerr != KCGI_OK)
4035 goto err;
4037 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
4038 if (kerr != KCGI_OK)
4039 goto err;
4041 a->lineno_cur++;
4042 bline = &a->lines[a->lineno_cur - 1];
4043 err:
4044 free(href_diff);
4046 done:
4047 free(line);
4048 if (err == NULL && kerr != KCGI_OK)
4049 err = gw_kcgi_error(kerr);
4050 return err;
4053 static const struct got_error *
4054 gw_output_file_blame(struct gw_trans *gw_trans, struct gw_header *header)
4056 const struct got_error *error = NULL;
4057 struct got_object_id *obj_id = NULL;
4058 struct got_object_id *commit_id = NULL;
4059 struct got_commit_object *commit = NULL;
4060 struct got_blob_object *blob = NULL;
4061 char *path = NULL, *in_repo_path = NULL;
4062 struct gw_blame_cb_args bca;
4063 int i, obj_type;
4064 off_t filesize;
4066 memset(&bca, 0, sizeof(bca));
4068 if (asprintf(&path, "%s%s%s",
4069 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4070 gw_trans->repo_folder ? "/" : "",
4071 gw_trans->repo_file) == -1) {
4072 error = got_error_from_errno("asprintf");
4073 goto done;
4076 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path);
4077 if (error)
4078 goto done;
4080 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
4081 GOT_OBJ_TYPE_COMMIT, &header->refs, gw_trans->repo);
4082 if (error)
4083 goto done;
4085 error = got_object_open_as_commit(&commit, gw_trans->repo, commit_id);
4086 if (error)
4087 goto done;
4089 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit,
4090 in_repo_path);
4091 if (error)
4092 goto done;
4094 if (obj_id == NULL) {
4095 error = got_error(GOT_ERR_NO_OBJ);
4096 goto done;
4099 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
4100 if (error)
4101 goto done;
4103 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4104 error = got_error(GOT_ERR_OBJ_TYPE);
4105 goto done;
4108 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
4109 if (error)
4110 goto done;
4112 bca.f = got_opentemp();
4113 if (bca.f == NULL) {
4114 error = got_error_from_errno("got_opentemp");
4115 goto done;
4117 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4118 &bca.line_offsets, bca.f, blob);
4119 if (error || bca.nlines == 0)
4120 goto done;
4122 /* Don't include \n at EOF in the blame line count. */
4123 if (bca.line_offsets[bca.nlines - 1] == filesize)
4124 bca.nlines--;
4126 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4127 if (bca.lines == NULL) {
4128 error = got_error_from_errno("calloc");
4129 goto done;
4131 bca.lineno_cur = 1;
4132 bca.nlines_prec = 0;
4133 i = bca.nlines;
4134 while (i > 0) {
4135 i /= 10;
4136 bca.nlines_prec++;
4138 bca.repo = gw_trans->repo;
4139 bca.gw_trans = gw_trans;
4141 error = got_blame(in_repo_path, commit_id, gw_trans->repo, gw_blame_cb,
4142 &bca, NULL, NULL);
4143 done:
4144 free(in_repo_path);
4145 free(commit_id);
4146 free(obj_id);
4147 free(path);
4149 if (blob) {
4150 free(bca.line_offsets);
4151 for (i = 0; i < bca.nlines; i++) {
4152 struct blame_line *bline = &bca.lines[i];
4153 free(bline->id_str);
4154 free(bline->committer);
4156 free(bca.lines);
4157 if (bca.f && fclose(bca.f) == EOF && error == NULL)
4158 error = got_error_from_errno("fclose");
4160 if (blob)
4161 got_object_blob_close(blob);
4162 if (commit)
4163 got_object_commit_close(commit);
4164 return error;
4167 static const struct got_error *
4168 gw_output_blob_buf(struct gw_trans *gw_trans, struct gw_header *header)
4170 const struct got_error *error = NULL;
4171 struct got_object_id *obj_id = NULL;
4172 struct got_object_id *commit_id = NULL;
4173 struct got_commit_object *commit = NULL;
4174 struct got_blob_object *blob = NULL;
4175 char *path = NULL, *in_repo_path = NULL;
4176 int obj_type, set_mime = 0;
4177 size_t len, hdrlen;
4178 const uint8_t *buf;
4179 enum kcgi_err kerr = KCGI_OK;
4181 if (asprintf(&path, "%s%s%s",
4182 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4183 gw_trans->repo_folder ? "/" : "",
4184 gw_trans->repo_file) == -1) {
4185 error = got_error_from_errno("asprintf");
4186 goto done;
4189 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path);
4190 if (error)
4191 goto done;
4193 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
4194 GOT_OBJ_TYPE_COMMIT, &header->refs, gw_trans->repo);
4195 if (error)
4196 goto done;
4198 error = got_object_open_as_commit(&commit, gw_trans->repo, commit_id);
4199 if (error)
4200 goto done;
4202 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit,
4203 in_repo_path);
4204 if (error)
4205 goto done;
4207 if (obj_id == NULL) {
4208 error = got_error(GOT_ERR_NO_OBJ);
4209 goto done;
4212 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
4213 if (error)
4214 goto done;
4216 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4217 error = got_error(GOT_ERR_OBJ_TYPE);
4218 goto done;
4221 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
4222 if (error)
4223 goto done;
4225 hdrlen = got_object_blob_get_hdrlen(blob);
4226 do {
4227 error = got_object_blob_read_block(&len, blob);
4228 if (error)
4229 goto done;
4230 buf = got_object_blob_get_read_buf(blob);
4233 * Skip blob object header first time around,
4234 * which also contains a zero byte.
4236 buf += hdrlen;
4237 if (set_mime == 0) {
4238 if (isbinary(buf, len - hdrlen))
4239 gw_trans->mime = KMIME_APP_OCTET_STREAM;
4240 else
4241 gw_trans->mime = KMIME_TEXT_PLAIN;
4242 set_mime = 1;
4243 error = gw_display_index(gw_trans);
4244 if (error)
4245 goto done;
4247 kerr = khttp_write(gw_trans->gw_req, buf, len - hdrlen);
4248 if (kerr != KCGI_OK)
4249 goto done;
4250 hdrlen = 0;
4251 } while (len != 0);
4252 done:
4253 free(in_repo_path);
4254 free(commit_id);
4255 free(obj_id);
4256 free(path);
4257 if (blob)
4258 got_object_blob_close(blob);
4259 if (commit)
4260 got_object_commit_close(commit);
4261 if (error == NULL && kerr != KCGI_OK)
4262 error = gw_kcgi_error(kerr);
4263 return error;
4266 static const struct got_error *
4267 gw_output_repo_tree(struct gw_trans *gw_trans, struct gw_header *header)
4269 const struct got_error *error = NULL;
4270 struct got_object_id *tree_id = NULL, *commit_id = NULL;
4271 struct got_tree_object *tree = NULL;
4272 struct got_commit_object *commit = NULL;
4273 char *path = NULL, *in_repo_path = NULL;
4274 char *id_str = NULL;
4275 char *build_folder = NULL;
4276 char *href_blob = NULL, *href_blame = NULL;
4277 const char *class = NULL;
4278 int nentries, i, class_flip = 0;
4279 enum kcgi_err kerr = KCGI_OK;
4281 if (gw_trans->repo_folder != NULL) {
4282 path = strdup(gw_trans->repo_folder);
4283 if (path == NULL) {
4284 error = got_error_from_errno("strdup");
4285 goto done;
4287 } else {
4288 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
4289 gw_trans->repo_path);
4290 if (error)
4291 goto done;
4292 free(path);
4293 path = in_repo_path;
4296 if (gw_trans->commit_id == NULL) {
4297 struct got_reference *head_ref;
4298 error = got_ref_open(&head_ref, gw_trans->repo,
4299 gw_trans->headref, 0);
4300 if (error)
4301 goto done;
4302 error = got_ref_resolve(&commit_id, gw_trans->repo, head_ref);
4303 if (error)
4304 goto done;
4305 got_ref_close(head_ref);
4307 * gw_trans->commit_id was not parsed from the querystring
4308 * we hit this code path from gw_index, where we don't know the
4309 * commit values for the tree link yet, so set
4310 * gw_trans->commit_id here to continue further into the tree
4312 error = got_object_id_str(&gw_trans->commit_id, commit_id);
4313 if (error)
4314 goto done;
4316 } else {
4317 error = got_repo_match_object_id(&commit_id, NULL,
4318 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT, &header->refs,
4319 gw_trans->repo);
4320 if (error)
4321 goto done;
4324 error = got_object_open_as_commit(&commit, gw_trans->repo, commit_id);
4325 if (error)
4326 goto done;
4328 error = got_object_id_by_path(&tree_id, gw_trans->repo, commit,
4329 path);
4330 if (error)
4331 goto done;
4333 error = got_object_open_as_tree(&tree, gw_trans->repo, tree_id);
4334 if (error)
4335 goto done;
4337 nentries = got_object_tree_get_nentries(tree);
4338 for (i = 0; i < nentries; i++) {
4339 struct got_tree_entry *te;
4340 const char *modestr = "";
4341 mode_t mode;
4343 te = got_object_tree_get_entry(tree, i);
4345 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
4346 if (error)
4347 goto done;
4349 mode = got_tree_entry_get_mode(te);
4350 if (got_object_tree_entry_is_submodule(te))
4351 modestr = "$";
4352 else if (S_ISLNK(mode))
4353 modestr = "@";
4354 else if (S_ISDIR(mode))
4355 modestr = "/";
4356 else if (mode & S_IXUSR)
4357 modestr = "*";
4359 if (class_flip == 0) {
4360 class = "back_lightgray";
4361 class_flip = 1;
4362 } else {
4363 class = "back_white";
4364 class_flip = 0;
4367 if (S_ISDIR(mode)) {
4368 if (asprintf(&build_folder, "%s/%s",
4369 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4370 got_tree_entry_get_name(te)) == -1) {
4371 error = got_error_from_errno("asprintf");
4372 goto done;
4375 href_blob = khttp_urlpart(NULL, NULL, "gotweb", "path",
4376 gw_trans->repo_name, "action",
4377 gw_get_action_name(gw_trans), "commit",
4378 gw_trans->commit_id, "folder", build_folder, NULL);
4379 if (href_blob == NULL) {
4380 error = got_error_from_errno("khttp_urlpart");
4381 goto done;
4383 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4384 KATTR_ID, "tree_wrapper", KATTR__MAX);
4385 if (kerr != KCGI_OK)
4386 goto done;
4387 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4388 KATTR_ID, "tree_line", KATTR_CLASS, class,
4389 KATTR__MAX);
4390 if (kerr != KCGI_OK)
4391 goto done;
4392 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4393 KATTR_HREF, href_blob, KATTR_CLASS,
4394 "diff_directory", KATTR__MAX);
4395 if (kerr != KCGI_OK)
4396 goto done;
4397 kerr = khtml_printf(gw_trans->gw_html_req, "%s%s",
4398 got_tree_entry_get_name(te), modestr);
4399 if (kerr != KCGI_OK)
4400 goto done;
4401 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4402 if (kerr != KCGI_OK)
4403 goto done;
4404 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4405 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
4406 KATTR__MAX);
4407 if (kerr != KCGI_OK)
4408 goto done;
4409 kerr = khtml_entity(gw_trans->gw_html_req,
4410 KENTITY_nbsp);
4411 if (kerr != KCGI_OK)
4412 goto done;
4413 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4414 if (kerr != KCGI_OK)
4415 goto done;
4416 } else {
4417 href_blob = khttp_urlpart(NULL, NULL, "gotweb", "path",
4418 gw_trans->repo_name, "action", "blob", "commit",
4419 gw_trans->commit_id, "file",
4420 got_tree_entry_get_name(te), "folder",
4421 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4422 NULL);
4423 if (href_blob == NULL) {
4424 error = got_error_from_errno("khttp_urlpart");
4425 goto done;
4427 href_blame = khttp_urlpart(NULL, NULL, "gotweb", "path",
4428 gw_trans->repo_name, "action", "blame", "commit",
4429 gw_trans->commit_id, "file",
4430 got_tree_entry_get_name(te), "folder",
4431 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4432 NULL);
4433 if (href_blame == NULL) {
4434 error = got_error_from_errno("khttp_urlpart");
4435 goto done;
4437 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4438 KATTR_ID, "tree_wrapper", KATTR__MAX);
4439 if (kerr != KCGI_OK)
4440 goto done;
4441 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4442 KATTR_ID, "tree_line", KATTR_CLASS, class,
4443 KATTR__MAX);
4444 if (kerr != KCGI_OK)
4445 goto done;
4446 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4447 KATTR_HREF, href_blob, KATTR__MAX);
4448 if (kerr != KCGI_OK)
4449 goto done;
4450 kerr = khtml_printf(gw_trans->gw_html_req, "%s%s",
4451 got_tree_entry_get_name(te), modestr);
4452 if (kerr != KCGI_OK)
4453 goto done;
4454 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4455 if (kerr != KCGI_OK)
4456 goto done;
4457 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4458 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
4459 KATTR__MAX);
4460 if (kerr != KCGI_OK)
4461 goto done;
4463 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4464 KATTR_HREF, href_blob, KATTR__MAX);
4465 if (kerr != KCGI_OK)
4466 goto done;
4467 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
4468 if (kerr != KCGI_OK)
4469 goto done;
4470 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4471 if (kerr != KCGI_OK)
4472 goto done;
4474 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4475 if (kerr != KCGI_OK)
4476 goto done;
4478 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4479 KATTR_HREF, href_blame, KATTR__MAX);
4480 if (kerr != KCGI_OK)
4481 goto done;
4482 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
4483 if (kerr != KCGI_OK)
4484 goto done;
4486 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4487 if (kerr != KCGI_OK)
4488 goto done;
4490 free(id_str);
4491 id_str = NULL;
4492 free(href_blob);
4493 href_blob = NULL;
4494 free(build_folder);
4495 build_folder = NULL;
4497 done:
4498 if (tree)
4499 got_object_tree_close(tree);
4500 if (commit)
4501 got_object_commit_close(commit);
4502 free(id_str);
4503 free(href_blob);
4504 free(href_blame);
4505 free(in_repo_path);
4506 free(tree_id);
4507 free(build_folder);
4508 if (error == NULL && kerr != KCGI_OK)
4509 error = gw_kcgi_error(kerr);
4510 return error;
4513 static const struct got_error *
4514 gw_output_repo_heads(struct gw_trans *gw_trans)
4516 const struct got_error *error = NULL;
4517 struct got_reflist_head refs;
4518 struct got_reflist_entry *re;
4519 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
4520 char *href_commits = NULL;
4521 enum kcgi_err kerr = KCGI_OK;
4523 TAILQ_INIT(&refs);
4525 error = got_ref_list(&refs, gw_trans->repo, "refs/heads",
4526 got_ref_cmp_by_name, NULL);
4527 if (error)
4528 goto done;
4530 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4531 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
4532 if (kerr != KCGI_OK)
4533 goto done;
4534 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4535 KATTR_ID, "summary_heads_title", KATTR__MAX);
4536 if (kerr != KCGI_OK)
4537 goto done;
4538 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
4539 if (kerr != KCGI_OK)
4540 goto done;
4541 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4542 if (kerr != KCGI_OK)
4543 goto done;
4544 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4545 KATTR_ID, "summary_heads_content", KATTR__MAX);
4546 if (kerr != KCGI_OK)
4547 goto done;
4549 TAILQ_FOREACH(re, &refs, entry) {
4550 const char *refname;
4552 if (got_ref_is_symbolic(re->ref))
4553 continue;
4555 refname = got_ref_get_name(re->ref);
4556 if (strncmp(refname, "refs/heads/", 11) != 0)
4557 continue;
4559 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
4560 refname, TM_DIFF);
4561 if (error)
4562 goto done;
4564 if (strncmp(refname, "refs/heads/", 11) == 0)
4565 refname += 11;
4567 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4568 KATTR_ID, "heads_wrapper", KATTR__MAX);
4569 if (kerr != KCGI_OK)
4570 goto done;
4571 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4572 KATTR_ID, "heads_age", KATTR__MAX);
4573 if (kerr != KCGI_OK)
4574 goto done;
4575 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
4576 if (kerr != KCGI_OK)
4577 goto done;
4578 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4579 if (kerr != KCGI_OK)
4580 goto done;
4581 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4582 KATTR_ID, "heads_space", KATTR__MAX);
4583 if (kerr != KCGI_OK)
4584 goto done;
4585 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
4586 if (kerr != KCGI_OK)
4587 goto done;
4588 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4589 if (kerr != KCGI_OK)
4590 goto done;
4591 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4592 KATTR_ID, "head", KATTR__MAX);
4593 if (kerr != KCGI_OK)
4594 goto done;
4596 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
4597 gw_trans->repo_name, "action", "summary", "headref",
4598 refname, NULL);
4599 if (href_summary == NULL) {
4600 error = got_error_from_errno("khttp_urlpart");
4601 goto done;
4603 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4604 href_summary, KATTR__MAX);
4605 kerr = khtml_puts(gw_trans->gw_html_req, refname);
4606 if (kerr != KCGI_OK)
4607 goto done;
4608 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4609 if (kerr != KCGI_OK)
4610 goto done;
4612 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4613 "navs_wrapper", KATTR__MAX);
4614 if (kerr != KCGI_OK)
4615 goto done;
4616 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4617 "navs", KATTR__MAX);
4618 if (kerr != KCGI_OK)
4619 goto done;
4621 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4622 href_summary, KATTR__MAX);
4623 if (kerr != KCGI_OK)
4624 goto done;
4625 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4626 if (kerr != KCGI_OK)
4627 goto done;
4628 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4629 if (kerr != KCGI_OK)
4630 goto done;
4632 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4633 if (kerr != KCGI_OK)
4634 goto done;
4636 href_briefs = khttp_urlpart(NULL, NULL, "gotweb", "path",
4637 gw_trans->repo_name, "action", "briefs", "headref",
4638 refname, NULL);
4639 if (href_briefs == NULL) {
4640 error = got_error_from_errno("khttp_urlpart");
4641 goto done;
4643 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4644 href_briefs, KATTR__MAX);
4645 if (kerr != KCGI_OK)
4646 goto done;
4647 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4648 if (kerr != KCGI_OK)
4649 goto done;
4650 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4651 if (kerr != KCGI_OK)
4652 goto done;
4654 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4655 if (kerr != KCGI_OK)
4656 goto done;
4658 href_commits = khttp_urlpart(NULL, NULL, "gotweb", "path",
4659 gw_trans->repo_name, "action", "commits", "headref",
4660 refname, NULL);
4661 if (href_commits == NULL) {
4662 error = got_error_from_errno("khttp_urlpart");
4663 goto done;
4665 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4666 href_commits, KATTR__MAX);
4667 if (kerr != KCGI_OK)
4668 goto done;
4669 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4670 if (kerr != KCGI_OK)
4671 goto done;
4672 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4673 if (kerr != KCGI_OK)
4674 goto done;
4676 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4677 "dotted_line", KATTR__MAX);
4678 if (kerr != KCGI_OK)
4679 goto done;
4680 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4681 if (kerr != KCGI_OK)
4682 goto done;
4683 free(href_summary);
4684 href_summary = NULL;
4685 free(href_briefs);
4686 href_briefs = NULL;
4687 free(href_commits);
4688 href_commits = NULL;
4690 done:
4691 got_ref_list_free(&refs);
4692 free(href_summary);
4693 free(href_briefs);
4694 free(href_commits);
4695 return error;
4698 static const struct got_error *
4699 gw_output_site_link(struct gw_trans *gw_trans)
4701 const struct got_error *error = NULL;
4702 char *href_summary = NULL;
4703 enum kcgi_err kerr = KCGI_OK;
4705 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4706 "site_link", KATTR__MAX);
4707 if (kerr != KCGI_OK)
4708 goto done;
4709 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4710 KATTR__MAX);
4711 if (kerr != KCGI_OK)
4712 goto done;
4713 kerr = khtml_puts(gw_trans->gw_html_req,
4714 gw_trans->gw_conf->got_site_link);
4715 if (kerr != KCGI_OK)
4716 goto done;
4717 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4718 if (kerr != KCGI_OK)
4719 goto done;
4721 if (gw_trans->repo_name != NULL) {
4722 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4723 if (kerr != KCGI_OK)
4724 goto done;
4726 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
4727 gw_trans->repo_name, "action", "summary", NULL);
4728 if (href_summary == NULL) {
4729 error = got_error_from_errno("khttp_urlpart");
4730 goto done;
4732 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4733 href_summary, KATTR__MAX);
4734 if (kerr != KCGI_OK)
4735 goto done;
4736 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4737 if (kerr != KCGI_OK)
4738 goto done;
4739 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4740 if (kerr != KCGI_OK)
4741 goto done;
4742 kerr = khtml_printf(gw_trans->gw_html_req, " / %s",
4743 gw_get_action_name(gw_trans));
4744 if (kerr != KCGI_OK)
4745 goto done;
4748 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4749 if (kerr != KCGI_OK)
4750 goto done;
4751 done:
4752 free(href_summary);
4753 if (error == NULL && kerr != KCGI_OK)
4754 error = gw_kcgi_error(kerr);
4755 return error;
4758 static const struct got_error *
4759 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4761 const struct got_error *error = NULL;
4762 char *color = NULL;
4763 enum kcgi_err kerr = KCGI_OK;
4765 if (strncmp(buf, "-", 1) == 0)
4766 color = "diff_minus";
4767 else if (strncmp(buf, "+", 1) == 0)
4768 color = "diff_plus";
4769 else if (strncmp(buf, "@@", 2) == 0)
4770 color = "diff_chunk_header";
4771 else if (strncmp(buf, "@@", 2) == 0)
4772 color = "diff_chunk_header";
4773 else if (strncmp(buf, "commit +", 8) == 0)
4774 color = "diff_meta";
4775 else if (strncmp(buf, "commit -", 8) == 0)
4776 color = "diff_meta";
4777 else if (strncmp(buf, "blob +", 6) == 0)
4778 color = "diff_meta";
4779 else if (strncmp(buf, "blob -", 6) == 0)
4780 color = "diff_meta";
4781 else if (strncmp(buf, "file +", 6) == 0)
4782 color = "diff_meta";
4783 else if (strncmp(buf, "file -", 6) == 0)
4784 color = "diff_meta";
4785 else if (strncmp(buf, "from:", 5) == 0)
4786 color = "diff_author";
4787 else if (strncmp(buf, "via:", 4) == 0)
4788 color = "diff_author";
4789 else if (strncmp(buf, "date:", 5) == 0)
4790 color = "diff_date";
4791 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4792 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4793 if (error == NULL && kerr != KCGI_OK)
4794 error = gw_kcgi_error(kerr);
4795 return error;
4798 int
4799 main(int argc, char *argv[])
4801 const struct got_error *error = NULL, *error2 = NULL;
4802 struct gw_trans *gw_trans;
4803 struct gw_dir *dir = NULL, *tdir;
4804 const char *page = "index";
4805 enum kcgi_err kerr = KCGI_OK;
4807 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4808 errx(1, "malloc");
4810 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4811 errx(1, "malloc");
4813 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4814 errx(1, "malloc");
4816 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4817 errx(1, "malloc");
4819 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4820 if (kerr != KCGI_OK) {
4821 error = gw_kcgi_error(kerr);
4822 goto done;
4825 TAILQ_INIT(&gw_trans->gw_dirs);
4826 TAILQ_INIT(&gw_trans->gw_headers);
4828 gw_trans->action = -1;
4829 gw_trans->page = 0;
4830 gw_trans->repos_total = 0;
4831 gw_trans->repo_path = NULL;
4832 gw_trans->commit_id = NULL;
4833 gw_trans->next_id = NULL;
4834 gw_trans->prev_id = NULL;
4835 gw_trans->headref = GOT_REF_HEAD;
4836 gw_trans->mime = KMIME_TEXT_HTML;
4837 gw_trans->gw_tmpl->key = gw_templs;
4838 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4839 gw_trans->gw_tmpl->arg = gw_trans;
4840 gw_trans->gw_tmpl->cb = gw_template;
4842 error = parse_gotweb_config(&gw_trans->gw_conf, GOTWEB_CONF);
4843 if (error)
4844 goto done;
4846 error = gw_parse_querystring(gw_trans);
4847 if (error)
4848 goto done;
4850 if (gw_trans->action == GW_BLOB)
4851 error = gw_blob(gw_trans);
4852 else
4853 error = gw_display_index(gw_trans);
4854 done:
4855 if (gw_trans->repo) {
4856 const struct got_error *close_err;
4857 close_err = got_repo_close(gw_trans->repo);
4858 if (error == NULL)
4859 error = close_err;
4861 if (error) {
4862 gw_trans->error = error;
4863 gw_trans->action = GW_ERR;
4864 error2 = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
4865 if (error2)
4866 goto cleanup; /* we can't display an error page */
4867 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
4868 if (kerr != KCGI_OK)
4869 goto cleanup; /* we can't display an error page */
4870 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
4871 gw_query_funcs[gw_trans->action].template);
4872 if (kerr != KCGI_OK) {
4873 khtml_close(gw_trans->gw_html_req);
4874 goto cleanup; /* we can't display an error page */
4878 cleanup:
4879 free(gw_trans->gw_conf->got_repos_path);
4880 free(gw_trans->gw_conf->got_www_path);
4881 free(gw_trans->gw_conf->got_site_name);
4882 free(gw_trans->gw_conf->got_site_owner);
4883 free(gw_trans->gw_conf->got_site_link);
4884 free(gw_trans->gw_conf->got_logo);
4885 free(gw_trans->gw_conf->got_logo_url);
4886 free(gw_trans->gw_conf);
4887 free(gw_trans->commit_id);
4888 free(gw_trans->next_id);
4889 free(gw_trans->prev_id);
4890 free(gw_trans->repo_path);
4891 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4892 free(dir->name);
4893 free(dir->description);
4894 free(dir->age);
4895 free(dir->url);
4896 free(dir->path);
4897 free(dir);
4900 khttp_free(gw_trans->gw_req);
4901 return 0;