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/stat.h>
19 #include <sys/types.h>
21 #include <ctype.h>
22 #include <dirent.h>
23 #include <err.h>
24 #include <errno.h>
25 #include <regex.h>
26 #include <sha1.h>
27 #include <stdarg.h>
28 #include <stdint.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
34 #include <got_error.h>
35 #include <got_object.h>
36 #include <got_reference.h>
37 #include <got_repository.h>
38 #include <got_path.h>
39 #include <got_cancel.h>
40 #include <got_worktree.h>
41 #include <got_diff.h>
42 #include <got_commit_graph.h>
43 #include <got_blame.h>
44 #include <got_privsep.h>
45 #include <got_opentemp.h>
47 #include <kcgi.h>
48 #include <kcgihtml.h>
50 #include "gotweb.h"
52 #ifndef nitems
53 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
54 #endif
56 struct gw_trans {
57 TAILQ_HEAD(headers, gw_header) gw_headers;
58 TAILQ_HEAD(dirs, gw_dir) gw_dirs;
59 struct got_repository *repo;
60 struct gw_dir *gw_dir;
61 struct gotweb_config *gw_conf;
62 struct ktemplate *gw_tmpl;
63 struct khtmlreq *gw_html_req;
64 struct kreq *gw_req;
65 const struct got_error *error;
66 const char *repo_name;
67 char *repo_path;
68 char *commit_id;
69 char *next_id;
70 char *prev_id;
71 const char *repo_file;
72 char *repo_folder;
73 const char *headref;
74 unsigned int action;
75 unsigned int page;
76 unsigned int repos_total;
77 enum kmime mime;
78 int *pack_fds;
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 const 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:
455 if (error) {
456 gw_trans->mime = KMIME_TEXT_PLAIN;
457 err = gw_display_index(gw_trans);
458 if (err) {
459 error = err;
460 goto errored;
462 kerr = khttp_puts(gw_trans->gw_req, error->msg);
464 errored:
465 gw_free_header(header);
466 if (error == NULL && kerr != KCGI_OK)
467 error = gw_kcgi_error(kerr);
468 return error;
471 static const struct got_error *
472 gw_diff(struct gw_trans *gw_trans)
474 const struct got_error *error = NULL;
475 struct gw_header *header = NULL;
476 char *age = NULL;
477 enum kcgi_err kerr = KCGI_OK;
479 #ifndef PROFILE
480 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
481 NULL) == -1)
482 return got_error_from_errno("pledge");
483 #endif
484 if ((header = gw_init_header()) == NULL)
485 return got_error_from_errno("malloc");
487 error = gw_apply_unveil(gw_trans->gw_dir->path);
488 if (error)
489 goto done;
491 error = gw_get_header(gw_trans, header, 1);
492 if (error)
493 goto done;
495 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
496 "diff_header_wrapper", KATTR__MAX);
497 if (kerr != KCGI_OK)
498 goto done;
499 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
500 "diff_header", KATTR__MAX);
501 if (kerr != KCGI_OK)
502 goto done;
503 error = gw_gen_diff_header(gw_trans, header->parent_id,
504 header->commit_id);
505 if (error)
506 goto done;
507 error = gw_gen_commit_header(gw_trans, header->commit_id,
508 header->refs_str);
509 if (error)
510 goto done;
511 error = gw_gen_tree_header(gw_trans, header->tree_id);
512 if (error)
513 goto done;
514 error = gw_gen_author_header(gw_trans, header->author);
515 if (error)
516 goto done;
517 error = gw_gen_committer_header(gw_trans, header->author);
518 if (error)
519 goto done;
520 error = gw_get_time_str(&age, header->committer_time,
521 TM_LONG);
522 if (error)
523 goto done;
524 error = gw_gen_age_header(gw_trans, age ?age : "");
525 if (error)
526 goto done;
527 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
528 if (error)
529 goto done;
530 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
531 if (kerr != KCGI_OK)
532 goto done;
533 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
534 "dotted_line", KATTR__MAX);
535 if (kerr != KCGI_OK)
536 goto done;
537 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
538 if (kerr != KCGI_OK)
539 goto done;
541 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
542 "diff", KATTR__MAX);
543 if (kerr != KCGI_OK)
544 goto done;
545 error = gw_output_diff(gw_trans, header);
546 if (error)
547 goto done;
549 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
550 done:
551 gw_free_header(header);
552 free(age);
553 if (error == NULL && kerr != KCGI_OK)
554 error = gw_kcgi_error(kerr);
555 return error;
558 static const struct got_error *
559 gw_index(struct gw_trans *gw_trans)
561 const struct got_error *error = NULL;
562 struct gw_dir *gw_dir = NULL;
563 char *href_next = NULL, *href_prev = NULL, *href_summary = NULL;
564 char *href_briefs = NULL, *href_commits = NULL, *href_tree = NULL;
565 char *href_tags = NULL;
566 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
567 enum kcgi_err kerr = KCGI_OK;
569 #ifndef PROFILE
570 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
571 NULL) == -1) {
572 error = got_error_from_errno("pledge");
573 return error;
575 #endif
576 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path);
577 if (error)
578 return error;
580 error = gw_load_got_paths(gw_trans);
581 if (error)
582 return error;
584 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
585 "index_header", KATTR__MAX);
586 if (kerr != KCGI_OK)
587 return gw_kcgi_error(kerr);
588 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
589 "index_header_project", KATTR__MAX);
590 if (kerr != KCGI_OK)
591 return gw_kcgi_error(kerr);
592 kerr = khtml_puts(gw_trans->gw_html_req, "Project");
593 if (kerr != KCGI_OK)
594 return gw_kcgi_error(kerr);
595 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
596 if (kerr != KCGI_OK)
597 return gw_kcgi_error(kerr);
599 if (gw_trans->gw_conf->got_show_repo_description) {
600 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
601 "index_header_description", KATTR__MAX);
602 if (kerr != KCGI_OK)
603 return gw_kcgi_error(kerr);
604 kerr = khtml_puts(gw_trans->gw_html_req, "Description");
605 if (kerr != KCGI_OK)
606 return gw_kcgi_error(kerr);
607 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
608 if (kerr != KCGI_OK)
609 return gw_kcgi_error(kerr);
612 if (gw_trans->gw_conf->got_show_repo_owner) {
613 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
614 "index_header_owner", KATTR__MAX);
615 if (kerr != KCGI_OK)
616 return gw_kcgi_error(kerr);
617 kerr = khtml_puts(gw_trans->gw_html_req, "Owner");
618 if (kerr != KCGI_OK)
619 return gw_kcgi_error(kerr);
620 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
621 if (kerr != KCGI_OK)
622 return gw_kcgi_error(kerr);
625 if (gw_trans->gw_conf->got_show_repo_age) {
626 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
627 "index_header_age", KATTR__MAX);
628 if (kerr != KCGI_OK)
629 return gw_kcgi_error(kerr);
630 kerr = khtml_puts(gw_trans->gw_html_req, "Last Change");
631 if (kerr != KCGI_OK)
632 return gw_kcgi_error(kerr);
633 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
634 if (kerr != KCGI_OK)
635 return gw_kcgi_error(kerr);
638 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
639 if (kerr != KCGI_OK)
640 return gw_kcgi_error(kerr);
642 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
643 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
644 "index_wrapper", KATTR__MAX);
645 if (kerr != KCGI_OK)
646 return gw_kcgi_error(kerr);
647 kerr = khtml_printf(gw_trans->gw_html_req,
648 "No repositories found in %s",
649 gw_trans->gw_conf->got_repos_path);
650 if (kerr != KCGI_OK)
651 return gw_kcgi_error(kerr);
652 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
653 if (kerr != KCGI_OK)
654 return gw_kcgi_error(kerr);
655 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
656 "dotted_line", KATTR__MAX);
657 if (kerr != KCGI_OK)
658 return gw_kcgi_error(kerr);
659 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
660 if (kerr != KCGI_OK)
661 return gw_kcgi_error(kerr);
662 return error;
665 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
666 dir_c++;
668 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
669 if (gw_trans->page > 0 && (gw_trans->page *
670 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
671 prev_disp++;
672 continue;
675 prev_disp++;
677 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
678 "index_wrapper", KATTR__MAX);
679 if (kerr != KCGI_OK)
680 goto done;
682 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
683 gw_dir->name, "action", "summary", NULL);
684 if (href_summary == NULL) {
685 error = got_error_from_errno("khttp_urlpart");
686 goto done;
688 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
689 "index_project", KATTR__MAX);
690 if (kerr != KCGI_OK)
691 goto done;
692 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
693 href_summary, KATTR__MAX);
694 if (kerr != KCGI_OK)
695 goto done;
696 kerr = khtml_puts(gw_trans->gw_html_req, gw_dir->name);
697 if (kerr != KCGI_OK)
698 goto done;
699 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
700 if (kerr != KCGI_OK)
701 goto done;
702 if (gw_trans->gw_conf->got_show_repo_description) {
703 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
704 KATTR_ID, "index_project_description", KATTR__MAX);
705 if (kerr != KCGI_OK)
706 goto done;
707 kerr = khtml_puts(gw_trans->gw_html_req,
708 gw_dir->description ? gw_dir->description : "");
709 if (kerr != KCGI_OK)
710 goto done;
711 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
712 if (kerr != KCGI_OK)
713 goto done;
715 if (gw_trans->gw_conf->got_show_repo_owner) {
716 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
717 KATTR_ID, "index_project_owner", KATTR__MAX);
718 if (kerr != KCGI_OK)
719 goto done;
720 kerr = khtml_puts(gw_trans->gw_html_req,
721 gw_dir->owner ? gw_dir->owner : "");
722 if (kerr != KCGI_OK)
723 goto done;
724 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
725 if (kerr != KCGI_OK)
726 goto done;
728 if (gw_trans->gw_conf->got_show_repo_age) {
729 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
730 KATTR_ID, "index_project_age", KATTR__MAX);
731 if (kerr != KCGI_OK)
732 goto done;
733 kerr = khtml_puts(gw_trans->gw_html_req,
734 gw_dir->age ? gw_dir->age : "");
735 if (kerr != KCGI_OK)
736 goto done;
737 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
738 if (kerr != KCGI_OK)
739 goto done;
742 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
743 "navs_wrapper", KATTR__MAX);
744 if (kerr != KCGI_OK)
745 goto done;
746 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
747 "navs", KATTR__MAX);
748 if (kerr != KCGI_OK)
749 goto done;
751 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
752 href_summary, KATTR__MAX);
753 if (kerr != KCGI_OK)
754 goto done;
755 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
756 if (kerr != KCGI_OK)
757 goto done;
758 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
759 if (kerr != KCGI_OK)
760 goto done;
762 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
763 if (kerr != KCGI_OK)
764 goto done;
766 href_briefs = khttp_urlpart(NULL, NULL, "gotweb", "path",
767 gw_dir->name, "action", "briefs", NULL);
768 if (href_briefs == NULL) {
769 error = got_error_from_errno("khttp_urlpart");
770 goto done;
772 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
773 href_briefs, KATTR__MAX);
774 if (kerr != KCGI_OK)
775 goto done;
776 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
777 if (kerr != KCGI_OK)
778 goto done;
779 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
780 if (kerr != KCGI_OK)
781 error = gw_kcgi_error(kerr);
783 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
784 if (kerr != KCGI_OK)
785 goto done;
787 href_commits = khttp_urlpart(NULL, NULL, "gotweb", "path",
788 gw_dir->name, "action", "commits", NULL);
789 if (href_commits == NULL) {
790 error = got_error_from_errno("khttp_urlpart");
791 goto done;
793 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
794 href_commits, KATTR__MAX);
795 if (kerr != KCGI_OK)
796 goto done;
797 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
798 if (kerr != KCGI_OK)
799 goto done;
800 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
801 if (kerr != KCGI_OK)
802 goto done;
804 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
805 if (kerr != KCGI_OK)
806 goto done;
808 href_tags = khttp_urlpart(NULL, NULL, "gotweb", "path",
809 gw_dir->name, "action", "tags", NULL);
810 if (href_tags == NULL) {
811 error = got_error_from_errno("khttp_urlpart");
812 goto done;
814 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
815 href_tags, KATTR__MAX);
816 if (kerr != KCGI_OK)
817 goto done;
818 kerr = khtml_puts(gw_trans->gw_html_req, "tags");
819 if (kerr != KCGI_OK)
820 goto done;
821 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
822 if (kerr != KCGI_OK)
823 goto done;
825 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
826 if (kerr != KCGI_OK)
827 goto done;
829 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
830 gw_dir->name, "action", "tree", NULL);
831 if (href_tree == NULL) {
832 error = got_error_from_errno("khttp_urlpart");
833 goto done;
835 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
836 href_tree, KATTR__MAX);
837 if (kerr != KCGI_OK)
838 goto done;
839 kerr = khtml_puts(gw_trans->gw_html_req, "tree");
840 if (kerr != KCGI_OK)
841 goto done;
843 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
844 if (kerr != KCGI_OK)
845 goto done;
846 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
847 "dotted_line", KATTR__MAX);
848 if (kerr != KCGI_OK)
849 goto done;
850 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
851 if (kerr != KCGI_OK)
852 goto done;
854 free(href_summary);
855 href_summary = NULL;
856 free(href_briefs);
857 href_briefs = NULL;
858 free(href_commits);
859 href_commits = NULL;
860 free(href_tags);
861 href_tags = NULL;
862 free(href_tree);
863 href_tree = NULL;
865 if (gw_trans->gw_conf->got_max_repos_display == 0)
866 continue;
868 if ((next_disp == gw_trans->gw_conf->got_max_repos_display) ||
869 ((gw_trans->gw_conf->got_max_repos_display > 0) &&
870 (gw_trans->page > 0) &&
871 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
872 prev_disp == gw_trans->repos_total))) {
873 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
874 KATTR_ID, "np_wrapper", KATTR__MAX);
875 if (kerr != KCGI_OK)
876 goto done;
877 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
878 KATTR_ID, "nav_prev", KATTR__MAX);
879 if (kerr != KCGI_OK)
880 goto done;
883 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
884 (gw_trans->page > 0) &&
885 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
886 prev_disp == gw_trans->repos_total)) {
887 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "page",
888 KATTRX_INT, (int64_t)(gw_trans->page - 1), NULL);
889 if (href_prev == NULL) {
890 error = got_error_from_errno("khttp_urlpartx");
891 goto done;
893 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
894 KATTR_HREF, href_prev, KATTR__MAX);
895 if (kerr != KCGI_OK)
896 goto done;
897 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
898 if (kerr != KCGI_OK)
899 goto done;
900 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
901 if (kerr != KCGI_OK)
902 goto done;
905 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
906 if (kerr != KCGI_OK)
907 return gw_kcgi_error(kerr);
909 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
910 next_disp == gw_trans->gw_conf->got_max_repos_display &&
911 dir_c != (gw_trans->page + 1) *
912 gw_trans->gw_conf->got_max_repos_display) {
913 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
914 KATTR_ID, "nav_next", KATTR__MAX);
915 if (kerr != KCGI_OK)
916 goto done;
917 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "page",
918 KATTRX_INT, (int64_t)(gw_trans->page + 1), NULL);
919 if (href_next == NULL) {
920 error = got_error_from_errno("khttp_urlpartx");
921 goto done;
923 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
924 KATTR_HREF, href_next, KATTR__MAX);
925 if (kerr != KCGI_OK)
926 goto done;
927 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
928 if (kerr != KCGI_OK)
929 goto done;
930 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
931 if (kerr != KCGI_OK)
932 goto done;
933 next_disp = 0;
934 break;
937 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
938 (gw_trans->page > 0) &&
939 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
940 prev_disp == gw_trans->repos_total)) {
941 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
942 if (kerr != KCGI_OK)
943 goto done;
945 next_disp++;
947 done:
948 free(href_prev);
949 free(href_next);
950 free(href_summary);
951 free(href_briefs);
952 free(href_commits);
953 free(href_tags);
954 free(href_tree);
955 if (error == NULL && kerr != KCGI_OK)
956 error = gw_kcgi_error(kerr);
957 return error;
960 static const struct got_error *
961 gw_commits(struct gw_trans *gw_trans)
963 const struct got_error *error = NULL;
964 struct gw_header *header = NULL, *n_header = NULL;
965 char *age = NULL, *href_diff = NULL, *href_tree = NULL;
966 char *href_prev = NULL, *href_next = NULL;
967 enum kcgi_err kerr = KCGI_OK;
968 int commit_found = 0;
970 if ((header = gw_init_header()) == NULL)
971 return got_error_from_errno("malloc");
973 #ifndef PROFILE
974 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
975 NULL) == -1) {
976 error = got_error_from_errno("pledge");
977 goto done;
979 #endif
980 error = gw_apply_unveil(gw_trans->gw_dir->path);
981 if (error)
982 goto done;
984 error = gw_get_header(gw_trans, header,
985 gw_trans->gw_conf->got_max_commits_display);
986 if (error)
987 goto done;
989 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
990 if (commit_found == 0 && gw_trans->commit_id != NULL) {
991 if (strcmp(gw_trans->commit_id,
992 n_header->commit_id) != 0)
993 continue;
994 else
995 commit_found = 1;
997 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
998 "commits_line_wrapper", KATTR__MAX);
999 if (kerr != KCGI_OK)
1000 goto done;
1001 error = gw_gen_commit_header(gw_trans, n_header->commit_id,
1002 n_header->refs_str);
1003 if (error)
1004 goto done;
1005 error = gw_gen_author_header(gw_trans, n_header->author);
1006 if (error)
1007 goto done;
1008 error = gw_gen_committer_header(gw_trans, n_header->author);
1009 if (error)
1010 goto done;
1011 error = gw_get_time_str(&age, n_header->committer_time,
1012 TM_LONG);
1013 if (error)
1014 goto done;
1015 error = gw_gen_age_header(gw_trans, age ?age : "");
1016 if (error)
1017 goto done;
1018 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1019 if (kerr != KCGI_OK)
1020 goto done;
1022 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1023 "dotted_line", KATTR__MAX);
1024 if (kerr != KCGI_OK)
1025 goto done;
1026 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1027 if (kerr != KCGI_OK)
1028 goto done;
1030 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1031 "commit", KATTR__MAX);
1032 if (kerr != KCGI_OK)
1033 goto done;
1034 kerr = khttp_puts(gw_trans->gw_req, n_header->commit_msg);
1035 if (kerr != KCGI_OK)
1036 goto done;
1037 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1038 if (kerr != KCGI_OK)
1039 goto done;
1041 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
1042 gw_trans->repo_name, "action", "diff", "commit",
1043 n_header->commit_id, NULL);
1044 if (href_diff == NULL) {
1045 error = got_error_from_errno("khttp_urlpart");
1046 goto done;
1048 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1049 KATTR_ID, "navs_wrapper", KATTR__MAX);
1050 if (kerr != KCGI_OK)
1051 goto done;
1052 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1053 KATTR_ID, "navs", KATTR__MAX);
1054 if (kerr != KCGI_OK)
1055 goto done;
1056 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1057 KATTR_HREF, href_diff, KATTR__MAX);
1058 if (kerr != KCGI_OK)
1059 goto done;
1060 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1061 if (kerr != KCGI_OK)
1062 goto done;
1063 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1064 if (kerr != KCGI_OK)
1065 goto done;
1067 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1068 if (kerr != KCGI_OK)
1069 goto done;
1071 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
1072 gw_trans->repo_name, "action", "tree", "commit",
1073 n_header->commit_id, NULL);
1074 if (href_tree == NULL) {
1075 error = got_error_from_errno("khttp_urlpart");
1076 goto done;
1078 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1079 KATTR_HREF, href_tree, KATTR__MAX);
1080 if (kerr != KCGI_OK)
1081 goto done;
1082 khtml_puts(gw_trans->gw_html_req, "tree");
1083 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1084 if (kerr != KCGI_OK)
1085 goto done;
1086 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1087 if (kerr != KCGI_OK)
1088 goto done;
1090 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1091 "solid_line", KATTR__MAX);
1092 if (kerr != KCGI_OK)
1093 goto done;
1094 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1095 if (kerr != KCGI_OK)
1096 goto done;
1098 free(age);
1099 age = NULL;
1102 if (gw_trans->next_id || gw_trans->prev_id) {
1103 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1104 KATTR_ID, "np_wrapper", KATTR__MAX);
1105 if (kerr != KCGI_OK)
1106 goto done;
1107 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1108 KATTR_ID, "nav_prev", KATTR__MAX);
1109 if (kerr != KCGI_OK)
1110 goto done;
1113 if (gw_trans->prev_id) {
1114 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1115 KATTRX_STRING, gw_trans->repo_name, "page",
1116 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1117 KATTRX_STRING, "commits", "commit", KATTRX_STRING,
1118 gw_trans->prev_id ? gw_trans->prev_id : "", NULL);
1119 if (href_prev == NULL) {
1120 error = got_error_from_errno("khttp_urlpartx");
1121 goto done;
1123 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1124 KATTR_HREF, href_prev, KATTR__MAX);
1125 if (kerr != KCGI_OK)
1126 goto done;
1127 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1128 if (kerr != KCGI_OK)
1129 goto done;
1130 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1131 if (kerr != KCGI_OK)
1132 goto done;
1135 if (gw_trans->next_id || gw_trans->page > 0) {
1136 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1137 if (kerr != KCGI_OK)
1138 return gw_kcgi_error(kerr);
1141 if (gw_trans->next_id) {
1142 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1143 KATTR_ID, "nav_next", KATTR__MAX);
1144 if (kerr != KCGI_OK)
1145 goto done;
1146 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1147 KATTRX_STRING, gw_trans->repo_name, "page",
1148 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1149 KATTRX_STRING, "commits", "commit", KATTRX_STRING,
1150 gw_trans->next_id, NULL);
1151 if (href_next == NULL) {
1152 error = got_error_from_errno("khttp_urlpartx");
1153 goto done;
1155 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1156 KATTR_HREF, href_next, KATTR__MAX);
1157 if (kerr != KCGI_OK)
1158 goto done;
1159 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1160 if (kerr != KCGI_OK)
1161 goto done;
1162 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1163 if (kerr != KCGI_OK)
1164 goto done;
1167 if (gw_trans->next_id || gw_trans->page > 0) {
1168 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1169 if (kerr != KCGI_OK)
1170 goto done;
1172 done:
1173 gw_free_header(header);
1174 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1175 gw_free_header(n_header);
1176 free(age);
1177 free(href_next);
1178 free(href_prev);
1179 free(href_diff);
1180 free(href_tree);
1181 if (error == NULL && kerr != KCGI_OK)
1182 error = gw_kcgi_error(kerr);
1183 return error;
1186 static const struct got_error *
1187 gw_briefs(struct gw_trans *gw_trans)
1189 const struct got_error *error = NULL;
1190 struct gw_header *header = NULL, *n_header = NULL;
1191 char *age = NULL, *href_diff = NULL, *href_tree = NULL;
1192 char *href_prev = NULL, *href_next = NULL;
1193 char *newline, *smallerthan;
1194 enum kcgi_err kerr = KCGI_OK;
1195 int commit_found = 0;
1197 if ((header = gw_init_header()) == NULL)
1198 return got_error_from_errno("malloc");
1200 #ifndef PROFILE
1201 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
1202 NULL) == -1) {
1203 error = got_error_from_errno("pledge");
1204 goto done;
1206 #endif
1207 if (gw_trans->action != GW_SUMMARY) {
1208 error = gw_apply_unveil(gw_trans->gw_dir->path);
1209 if (error)
1210 goto done;
1213 if (gw_trans->action == GW_SUMMARY)
1214 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
1215 else
1216 error = gw_get_header(gw_trans, header,
1217 gw_trans->gw_conf->got_max_commits_display);
1218 if (error)
1219 goto done;
1221 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
1222 if (commit_found == 0 && gw_trans->commit_id != NULL) {
1223 if (strcmp(gw_trans->commit_id,
1224 n_header->commit_id) != 0)
1225 continue;
1226 else
1227 commit_found = 1;
1229 error = gw_get_time_str(&age, n_header->committer_time,
1230 TM_DIFF);
1231 if (error)
1232 goto done;
1234 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1235 KATTR_ID, "briefs_wrapper", KATTR__MAX);
1236 if (kerr != KCGI_OK)
1237 goto done;
1239 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1240 KATTR_ID, "briefs_age", KATTR__MAX);
1241 if (kerr != KCGI_OK)
1242 goto done;
1243 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
1244 if (kerr != KCGI_OK)
1245 goto done;
1246 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1247 if (kerr != KCGI_OK)
1248 goto done;
1250 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1251 KATTR_ID, "briefs_author", KATTR__MAX);
1252 if (kerr != KCGI_OK)
1253 goto done;
1254 smallerthan = strchr(n_header->author, '<');
1255 if (smallerthan)
1256 *smallerthan = '\0';
1257 kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
1258 if (kerr != KCGI_OK)
1259 goto done;
1260 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1261 if (kerr != KCGI_OK)
1262 goto done;
1264 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
1265 gw_trans->repo_name, "action", "diff", "commit",
1266 n_header->commit_id, NULL);
1267 if (href_diff == NULL) {
1268 error = got_error_from_errno("khttp_urlpart");
1269 goto done;
1271 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1272 KATTR_ID, "briefs_log", KATTR__MAX);
1273 if (kerr != KCGI_OK)
1274 goto done;
1275 newline = strchr(n_header->commit_msg, '\n');
1276 if (newline)
1277 *newline = '\0';
1278 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1279 KATTR_HREF, href_diff, KATTR__MAX);
1280 if (kerr != KCGI_OK)
1281 goto done;
1282 kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
1283 if (kerr != KCGI_OK)
1284 goto done;
1285 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1286 if (kerr != KCGI_OK)
1287 goto done;
1289 if (n_header->refs_str) {
1290 kerr = khtml_puts(gw_trans->gw_html_req, " ");
1291 if (kerr != KCGI_OK)
1292 goto done;
1293 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
1294 KATTR_ID, "refs_str", KATTR__MAX);
1295 if (kerr != KCGI_OK)
1296 goto done;
1297 kerr = khtml_printf(gw_trans->gw_html_req, "(%s)",
1298 n_header->refs_str);
1299 if (kerr != KCGI_OK)
1300 goto done;
1301 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1302 if (kerr != KCGI_OK)
1303 goto done;
1306 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1307 if (kerr != KCGI_OK)
1308 goto done;
1310 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1311 KATTR_ID, "navs_wrapper", KATTR__MAX);
1312 if (kerr != KCGI_OK)
1313 goto done;
1314 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1315 KATTR_ID, "navs", KATTR__MAX);
1316 if (kerr != KCGI_OK)
1317 goto done;
1318 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1319 KATTR_HREF, href_diff, KATTR__MAX);
1320 if (kerr != KCGI_OK)
1321 goto done;
1322 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1323 if (kerr != KCGI_OK)
1324 goto done;
1325 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1326 if (kerr != KCGI_OK)
1327 goto done;
1329 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1330 if (kerr != KCGI_OK)
1331 goto done;
1333 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
1334 gw_trans->repo_name, "action", "tree", "commit",
1335 n_header->commit_id, NULL);
1336 if (href_tree == NULL) {
1337 error = got_error_from_errno("khttp_urlpart");
1338 goto done;
1340 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1341 KATTR_HREF, href_tree, KATTR__MAX);
1342 if (kerr != KCGI_OK)
1343 goto done;
1344 khtml_puts(gw_trans->gw_html_req, "tree");
1345 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1346 if (kerr != KCGI_OK)
1347 goto done;
1348 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1349 if (kerr != KCGI_OK)
1350 goto done;
1352 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1353 KATTR_ID, "dotted_line", KATTR__MAX);
1354 if (kerr != KCGI_OK)
1355 goto done;
1356 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1357 if (kerr != KCGI_OK)
1358 goto done;
1360 free(age);
1361 age = NULL;
1362 free(href_diff);
1363 href_diff = NULL;
1364 free(href_tree);
1365 href_tree = NULL;
1368 if (gw_trans->next_id || gw_trans->prev_id) {
1369 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1370 KATTR_ID, "np_wrapper", KATTR__MAX);
1371 if (kerr != KCGI_OK)
1372 goto done;
1373 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1374 KATTR_ID, "nav_prev", KATTR__MAX);
1375 if (kerr != KCGI_OK)
1376 goto done;
1379 if (gw_trans->prev_id) {
1380 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1381 KATTRX_STRING, gw_trans->repo_name, "page",
1382 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1383 KATTRX_STRING, "briefs", "commit", KATTRX_STRING,
1384 gw_trans->prev_id ? gw_trans->prev_id : "", NULL);
1385 if (href_prev == NULL) {
1386 error = got_error_from_errno("khttp_urlpartx");
1387 goto done;
1389 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1390 KATTR_HREF, href_prev, KATTR__MAX);
1391 if (kerr != KCGI_OK)
1392 goto done;
1393 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1394 if (kerr != KCGI_OK)
1395 goto done;
1396 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1397 if (kerr != KCGI_OK)
1398 goto done;
1401 if (gw_trans->next_id || gw_trans->page > 0) {
1402 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1403 if (kerr != KCGI_OK)
1404 return gw_kcgi_error(kerr);
1407 if (gw_trans->next_id) {
1408 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1409 KATTR_ID, "nav_next", KATTR__MAX);
1410 if (kerr != KCGI_OK)
1411 goto done;
1413 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1414 KATTRX_STRING, gw_trans->repo_name, "page",
1415 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1416 KATTRX_STRING, "briefs", "commit", KATTRX_STRING,
1417 gw_trans->next_id, NULL);
1418 if (href_next == NULL) {
1419 error = got_error_from_errno("khttp_urlpartx");
1420 goto done;
1422 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1423 KATTR_HREF, href_next, KATTR__MAX);
1424 if (kerr != KCGI_OK)
1425 goto done;
1426 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1427 if (kerr != KCGI_OK)
1428 goto done;
1429 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1430 if (kerr != KCGI_OK)
1431 goto done;
1434 if (gw_trans->next_id || gw_trans->page > 0) {
1435 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1436 if (kerr != KCGI_OK)
1437 goto done;
1439 done:
1440 gw_free_header(header);
1441 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1442 gw_free_header(n_header);
1443 free(age);
1444 free(href_next);
1445 free(href_prev);
1446 free(href_diff);
1447 free(href_tree);
1448 if (error == NULL && kerr != KCGI_OK)
1449 error = gw_kcgi_error(kerr);
1450 return error;
1453 static const struct got_error *
1454 gw_summary(struct gw_trans *gw_trans)
1456 const struct got_error *error = NULL;
1457 char *age = NULL;
1458 enum kcgi_err kerr = KCGI_OK;
1460 #ifndef PROFILE
1461 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
1462 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 wpath cpath proc exec sendfd unveil",
1626 NULL) == -1)
1627 return got_error_from_errno("pledge");
1628 #endif
1629 if ((header = gw_init_header()) == NULL)
1630 return got_error_from_errno("malloc");
1632 error = gw_apply_unveil(gw_trans->gw_dir->path);
1633 if (error)
1634 goto done;
1636 error = gw_get_header(gw_trans, header, 1);
1637 if (error)
1638 goto done;
1640 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1641 "tree_header_wrapper", KATTR__MAX);
1642 if (kerr != KCGI_OK)
1643 goto done;
1644 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1645 "tree_header", KATTR__MAX);
1646 if (kerr != KCGI_OK)
1647 goto done;
1648 error = gw_gen_tree_header(gw_trans, header->tree_id);
1649 if (error)
1650 goto done;
1651 error = gw_get_time_str(&age, header->committer_time,
1652 TM_LONG);
1653 if (error)
1654 goto done;
1655 error = gw_gen_age_header(gw_trans, age ?age : "");
1656 if (error)
1657 goto done;
1658 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1659 if (error)
1660 goto done;
1661 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1662 if (kerr != KCGI_OK)
1663 goto done;
1664 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1665 "dotted_line", KATTR__MAX);
1666 if (kerr != KCGI_OK)
1667 goto done;
1668 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1669 if (kerr != KCGI_OK)
1670 goto done;
1672 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1673 "tree", KATTR__MAX);
1674 if (kerr != KCGI_OK)
1675 goto done;
1676 error = gw_output_repo_tree(gw_trans, header);
1677 if (error)
1678 goto done;
1680 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1681 done:
1682 gw_free_header(header);
1683 free(tree_html_disp);
1684 free(tree_html);
1685 free(tree);
1686 free(age);
1687 if (error == NULL && kerr != KCGI_OK)
1688 error = gw_kcgi_error(kerr);
1689 return error;
1692 static const struct got_error *
1693 gw_tags(struct gw_trans *gw_trans)
1695 const struct got_error *error = NULL;
1696 struct gw_header *header = NULL;
1697 char *href_next = NULL, *href_prev = NULL;
1698 enum kcgi_err kerr = KCGI_OK;
1700 #ifndef PROFILE
1701 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
1702 NULL) == -1)
1703 return got_error_from_errno("pledge");
1704 #endif
1705 if ((header = gw_init_header()) == NULL)
1706 return got_error_from_errno("malloc");
1708 if (gw_trans->action != GW_SUMMARY) {
1709 error = gw_apply_unveil(gw_trans->gw_dir->path);
1710 if (error)
1711 goto done;
1714 error = gw_get_header(gw_trans, header, 1);
1715 if (error)
1716 goto done;
1718 if (gw_trans->action == GW_SUMMARY) {
1719 gw_trans->next_id = NULL;
1720 error = gw_output_repo_tags(gw_trans, header,
1721 D_MAXSLCOMMDISP, TAGBRIEF);
1722 if (error)
1723 goto done;
1724 } else {
1725 error = gw_output_repo_tags(gw_trans, header,
1726 gw_trans->gw_conf->got_max_commits_display, TAGBRIEF);
1727 if (error)
1728 goto done;
1731 if (gw_trans->next_id || gw_trans->page > 0) {
1732 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1733 KATTR_ID, "np_wrapper", KATTR__MAX);
1734 if (kerr != KCGI_OK)
1735 goto done;
1736 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1737 KATTR_ID, "nav_prev", KATTR__MAX);
1738 if (kerr != KCGI_OK)
1739 goto done;
1742 if (gw_trans->prev_id) {
1743 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1744 KATTRX_STRING, gw_trans->repo_name, "page",
1745 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1746 KATTRX_STRING, "tags", "commit", KATTRX_STRING,
1747 gw_trans->prev_id ? gw_trans->prev_id : "", NULL);
1748 if (href_prev == NULL) {
1749 error = got_error_from_errno("khttp_urlpartx");
1750 goto done;
1752 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1753 KATTR_HREF, href_prev, KATTR__MAX);
1754 if (kerr != KCGI_OK)
1755 goto done;
1756 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1757 if (kerr != KCGI_OK)
1758 goto done;
1759 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1760 if (kerr != KCGI_OK)
1761 goto done;
1764 if (gw_trans->next_id || gw_trans->page > 0) {
1765 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1766 if (kerr != KCGI_OK)
1767 return gw_kcgi_error(kerr);
1770 if (gw_trans->next_id) {
1771 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1772 KATTR_ID, "nav_next", KATTR__MAX);
1773 if (kerr != KCGI_OK)
1774 goto done;
1775 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1776 KATTRX_STRING, gw_trans->repo_name, "page",
1777 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1778 KATTRX_STRING, "tags", "commit", KATTRX_STRING,
1779 gw_trans->next_id, NULL);
1780 if (href_next == NULL) {
1781 error = got_error_from_errno("khttp_urlpartx");
1782 goto done;
1784 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1785 KATTR_HREF, href_next, KATTR__MAX);
1786 if (kerr != KCGI_OK)
1787 goto done;
1788 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1789 if (kerr != KCGI_OK)
1790 goto done;
1791 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1792 if (kerr != KCGI_OK)
1793 goto done;
1796 if (gw_trans->next_id || gw_trans->page > 0) {
1797 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1798 if (kerr != KCGI_OK)
1799 goto done;
1801 done:
1802 gw_free_header(header);
1803 free(href_next);
1804 free(href_prev);
1805 if (error == NULL && kerr != KCGI_OK)
1806 error = gw_kcgi_error(kerr);
1807 return error;
1810 static const struct got_error *
1811 gw_tag(struct gw_trans *gw_trans)
1813 const struct got_error *error = NULL;
1814 struct gw_header *header = NULL;
1815 enum kcgi_err kerr = KCGI_OK;
1817 #ifndef PROFILE
1818 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil", NULL) == -1)
1819 return got_error_from_errno("pledge");
1820 #endif
1821 if ((header = gw_init_header()) == NULL)
1822 return got_error_from_errno("malloc");
1824 error = gw_apply_unveil(gw_trans->gw_dir->path);
1825 if (error)
1826 goto done;
1828 if (gw_trans->commit_id == NULL) {
1829 error = got_error_msg(GOT_ERR_QUERYSTRING,
1830 "commit required in querystring");
1831 goto done;
1834 error = gw_get_header(gw_trans, header, 1);
1835 if (error)
1836 goto done;
1838 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1839 "tag_header_wrapper", KATTR__MAX);
1840 if (kerr != KCGI_OK)
1841 goto done;
1842 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1843 "tag_header", KATTR__MAX);
1844 if (kerr != KCGI_OK)
1845 goto done;
1846 error = gw_gen_commit_header(gw_trans, header->commit_id,
1847 header->refs_str);
1848 if (error)
1849 goto done;
1850 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1851 if (error)
1852 goto done;
1853 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1854 if (kerr != KCGI_OK)
1855 goto done;
1856 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1857 "dotted_line", KATTR__MAX);
1858 if (kerr != KCGI_OK)
1859 goto done;
1860 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1861 if (kerr != KCGI_OK)
1862 goto done;
1864 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1865 "tree", KATTR__MAX);
1866 if (kerr != KCGI_OK)
1867 goto done;
1869 error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1870 if (error)
1871 goto done;
1873 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1874 done:
1875 gw_free_header(header);
1876 if (error == NULL && kerr != KCGI_OK)
1877 error = gw_kcgi_error(kerr);
1878 return error;
1881 static const struct got_error *
1882 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1884 const struct got_error *error = NULL;
1885 DIR *dt;
1886 char *dir_test;
1887 int opened = 0;
1889 if (asprintf(&dir_test, "%s/%s/%s",
1890 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1891 GOTWEB_GIT_DIR) == -1)
1892 return got_error_from_errno("asprintf");
1894 dt = opendir(dir_test);
1895 if (dt == NULL) {
1896 free(dir_test);
1897 } else {
1898 gw_dir->path = strdup(dir_test);
1899 if (gw_dir->path == NULL) {
1900 opened = 1;
1901 error = got_error_from_errno("strdup");
1902 goto errored;
1904 opened = 1;
1905 goto done;
1908 if (asprintf(&dir_test, "%s/%s/%s",
1909 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1910 GOTWEB_GOT_DIR) == -1) {
1911 dir_test = NULL;
1912 error = got_error_from_errno("asprintf");
1913 goto errored;
1916 dt = opendir(dir_test);
1917 if (dt == NULL)
1918 free(dir_test);
1919 else {
1920 opened = 1;
1921 error = got_error(GOT_ERR_NOT_GIT_REPO);
1922 goto errored;
1925 if (asprintf(&dir_test, "%s/%s",
1926 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1) {
1927 error = got_error_from_errno("asprintf");
1928 dir_test = NULL;
1929 goto errored;
1932 gw_dir->path = strdup(dir_test);
1933 if (gw_dir->path == NULL) {
1934 opened = 1;
1935 error = got_error_from_errno("strdup");
1936 goto errored;
1939 dt = opendir(dir_test);
1940 if (dt == NULL) {
1941 error = got_error_path(gw_dir->name, GOT_ERR_NOT_GIT_REPO);
1942 goto errored;
1943 } else
1944 opened = 1;
1945 done:
1946 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1947 gw_dir->path);
1948 if (error)
1949 goto errored;
1950 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1951 if (error)
1952 goto errored;
1953 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1954 NULL, TM_DIFF);
1955 if (error)
1956 goto errored;
1957 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1958 errored:
1959 free(dir_test);
1960 if (opened)
1961 if (dt && closedir(dt) == -1 && error == NULL)
1962 error = got_error_from_errno("closedir");
1963 return error;
1966 static const struct got_error *
1967 gw_load_got_paths(struct gw_trans *gw_trans)
1969 const struct got_error *error = NULL;
1970 DIR *d;
1971 struct dirent **sd_dent;
1972 struct gw_dir *gw_dir;
1973 struct stat st;
1974 unsigned int d_cnt, d_i;
1976 d = opendir(gw_trans->gw_conf->got_repos_path);
1977 if (d == NULL) {
1978 error = got_error_from_errno2("opendir",
1979 gw_trans->gw_conf->got_repos_path);
1980 return error;
1983 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1984 alphasort);
1985 if (d_cnt == -1) {
1986 error = got_error_from_errno2("scandir",
1987 gw_trans->gw_conf->got_repos_path);
1988 goto done;
1991 for (d_i = 0; d_i < d_cnt; d_i++) {
1992 if (gw_trans->gw_conf->got_max_repos > 0 &&
1993 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1994 break; /* account for parent and self */
1996 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1997 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1998 continue;
2000 error = gw_init_gw_dir(&gw_dir, sd_dent[d_i]->d_name);
2001 if (error)
2002 goto done;
2004 error = gw_load_got_path(gw_trans, gw_dir);
2005 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
2006 error = NULL;
2007 continue;
2008 } else if (error && error->code != GOT_ERR_LONELY_PACKIDX)
2009 goto done;
2011 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
2012 !got_path_dir_is_empty(gw_dir->path)) {
2013 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
2014 entry);
2015 gw_trans->repos_total++;
2018 done:
2019 if (d && closedir(d) == -1 && error == NULL)
2020 error = got_error_from_errno("closedir");
2021 return error;
2024 static const struct got_error *
2025 gw_parse_querystring(struct gw_trans *gw_trans)
2027 const struct got_error *error = NULL;
2028 struct kpair *p;
2029 const struct gw_query_action *action = NULL;
2030 unsigned int i;
2032 if (gw_trans->gw_req->fieldnmap[0]) {
2033 return got_error(GOT_ERR_QUERYSTRING);
2034 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
2035 /* define gw_trans->repo_path */
2036 gw_trans->repo_name = p->parsed.s;
2038 if (asprintf(&gw_trans->repo_path, "%s/%s",
2039 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
2040 return got_error_from_errno("asprintf");
2042 /* get action and set function */
2043 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION])) {
2044 for (i = 0; i < nitems(gw_query_funcs); i++) {
2045 action = &gw_query_funcs[i];
2046 if (action->func_name == NULL)
2047 continue;
2048 if (strcmp(action->func_name,
2049 p->parsed.s) == 0) {
2050 gw_trans->action = i;
2051 break;
2055 if (gw_trans->action == -1) {
2056 gw_trans->action = GW_ERR;
2057 gw_trans->error = got_error_msg(GOT_ERR_QUERYSTRING,
2058 p != NULL ? "bad action in querystring" :
2059 "no action in querystring");
2060 return error;
2063 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID])) {
2064 if (asprintf(&gw_trans->commit_id, "%s",
2065 p->parsed.s) == -1)
2066 return got_error_from_errno("asprintf");
2069 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
2070 gw_trans->repo_file = p->parsed.s;
2072 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER])) {
2073 if (asprintf(&gw_trans->repo_folder, "%s",
2074 p->parsed.s) == -1)
2075 return got_error_from_errno("asprintf");
2078 if ((p = gw_trans->gw_req->fieldmap[KEY_PREV_ID])) {
2079 if (asprintf(&gw_trans->prev_id, "%s",
2080 p->parsed.s) == -1)
2081 return got_error_from_errno("asprintf");
2084 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
2085 gw_trans->headref = p->parsed.s;
2087 error = gw_init_gw_dir(&gw_trans->gw_dir, gw_trans->repo_name);
2088 if (error)
2089 return error;
2091 gw_trans->error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
2092 } else
2093 gw_trans->action = GW_INDEX;
2095 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
2096 gw_trans->page = p->parsed.i;
2098 return error;
2101 static const struct got_error *
2102 gw_init_gw_dir(struct gw_dir **gw_dir, const char *dir)
2104 const struct got_error *error;
2106 *gw_dir = malloc(sizeof(**gw_dir));
2107 if (*gw_dir == NULL)
2108 return got_error_from_errno("malloc");
2110 if (asprintf(&(*gw_dir)->name, "%s", dir) == -1) {
2111 error = got_error_from_errno("asprintf");
2112 free(*gw_dir);
2113 *gw_dir = NULL;
2114 return error;
2117 return NULL;
2120 static const struct got_error *
2121 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
2123 enum kcgi_err kerr = KCGI_OK;
2125 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
2126 if (kerr != KCGI_OK)
2127 return gw_kcgi_error(kerr);
2128 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
2129 khttps[code]);
2130 if (kerr != KCGI_OK)
2131 return gw_kcgi_error(kerr);
2132 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
2133 kmimetypes[mime]);
2134 if (kerr != KCGI_OK)
2135 return gw_kcgi_error(kerr);
2136 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
2137 "nosniff");
2138 if (kerr != KCGI_OK)
2139 return gw_kcgi_error(kerr);
2140 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
2141 if (kerr != KCGI_OK)
2142 return gw_kcgi_error(kerr);
2143 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
2144 "1; mode=block");
2145 if (kerr != KCGI_OK)
2146 return gw_kcgi_error(kerr);
2148 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
2149 kerr = khttp_head(gw_trans->gw_req,
2150 kresps[KRESP_CONTENT_DISPOSITION],
2151 "attachment; filename=%s", gw_trans->repo_file);
2152 if (kerr != KCGI_OK)
2153 return gw_kcgi_error(kerr);
2156 kerr = khttp_body(gw_trans->gw_req);
2157 return gw_kcgi_error(kerr);
2160 static const struct got_error *
2161 gw_display_index(struct gw_trans *gw_trans)
2163 const struct got_error *error;
2164 enum kcgi_err kerr = KCGI_OK;
2166 /* catch early querystring errors */
2167 if (gw_trans->error)
2168 gw_trans->action = GW_ERR;
2170 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
2171 if (error)
2172 return error;
2174 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
2175 if (kerr != KCGI_OK)
2176 return gw_kcgi_error(kerr);
2178 if (gw_trans->action != GW_BLOB) {
2179 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
2180 gw_query_funcs[gw_trans->action].template);
2181 if (kerr != KCGI_OK) {
2182 khtml_close(gw_trans->gw_html_req);
2183 return gw_kcgi_error(kerr);
2187 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
2190 static const struct got_error *
2191 gw_error(struct gw_trans *gw_trans)
2193 enum kcgi_err kerr = KCGI_OK;
2195 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->error->msg);
2197 return gw_kcgi_error(kerr);
2200 static int
2201 gw_template(size_t key, void *arg)
2203 const struct got_error *error = NULL;
2204 enum kcgi_err kerr = KCGI_OK;
2205 struct gw_trans *gw_trans = arg;
2206 char *ati = NULL, *fic32 = NULL, *fic16 = NULL;
2207 char *swm = NULL, *spt = NULL, *css = NULL, *logo = NULL;
2209 if (asprintf(&ati, "%s%s", gw_trans->gw_conf->got_www_path,
2210 "/apple-touch-icon.png") == -1)
2211 goto err;
2212 if (asprintf(&fic32, "%s%s", gw_trans->gw_conf->got_www_path,
2213 "/favicon-32x32.png") == -1)
2214 goto err;
2215 if (asprintf(&fic16, "%s%s", gw_trans->gw_conf->got_www_path,
2216 "/favicon-16x16.png") == -1)
2217 goto err;
2218 if (asprintf(&swm, "%s%s", gw_trans->gw_conf->got_www_path,
2219 "/site.webmanifest") == -1)
2220 goto err;
2221 if (asprintf(&spt, "%s%s", gw_trans->gw_conf->got_www_path,
2222 "/safari-pinned-tab.svg") == -1)
2223 goto err;
2224 if (asprintf(&css, "%s%s", gw_trans->gw_conf->got_www_path,
2225 "/gotweb.css") == -1)
2226 goto err;
2227 if (asprintf(&logo, "%s%s%s", gw_trans->gw_conf->got_www_path,
2228 gw_trans->gw_conf->got_www_path ? "/" : "",
2229 gw_trans->gw_conf->got_logo) == -1)
2230 goto err;
2232 switch (key) {
2233 case (TEMPL_HEAD):
2234 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2235 KATTR_NAME, "viewport",
2236 KATTR_CONTENT, "initial-scale=.75, user-scalable=yes",
2237 KATTR__MAX);
2238 if (kerr != KCGI_OK)
2239 return 0;
2240 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2241 if (kerr != KCGI_OK)
2242 return 0;
2243 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2244 KATTR_CHARSET, "utf-8",
2245 KATTR__MAX);
2246 if (kerr != KCGI_OK)
2247 return 0;
2248 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2249 if (kerr != KCGI_OK)
2250 return 0;
2251 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2252 KATTR_NAME, "msapplication-TileColor",
2253 KATTR_CONTENT, "#da532c", KATTR__MAX);
2254 if (kerr != KCGI_OK)
2255 return 0;
2256 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2257 if (kerr != KCGI_OK)
2258 return 0;
2259 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2260 KATTR_NAME, "theme-color",
2261 KATTR_CONTENT, "#ffffff", KATTR__MAX);
2262 if (kerr != KCGI_OK)
2263 return 0;
2264 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2265 if (kerr != KCGI_OK)
2266 return 0;
2267 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2268 KATTR_REL, "apple-touch-icon", KATTR_SIZES, "180x180",
2269 KATTR_HREF, ati, KATTR__MAX);
2270 if (kerr != KCGI_OK)
2271 return 0;
2272 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2273 if (kerr != KCGI_OK)
2274 return 0;
2275 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2276 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2277 "32x32", KATTR_HREF, fic32, KATTR__MAX);
2278 if (kerr != KCGI_OK)
2279 return 0;
2280 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2281 if (kerr != KCGI_OK)
2282 return 0;
2283 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2284 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2285 "16x16", KATTR_HREF, fic16, KATTR__MAX);
2286 if (kerr != KCGI_OK)
2287 return 0;
2288 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2289 if (kerr != KCGI_OK)
2290 return 0;
2291 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2292 KATTR_REL, "manifest", KATTR_HREF, swm,
2293 KATTR__MAX);
2294 if (kerr != KCGI_OK)
2295 return 0;
2296 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2297 if (kerr != KCGI_OK)
2298 return 0;
2299 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2300 KATTR_REL, "mask-icon", KATTR_HREF,
2301 spt, KATTR__MAX);
2302 if (kerr != KCGI_OK)
2303 return 0;
2304 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2305 if (kerr != KCGI_OK)
2306 return 0;
2307 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2308 KATTR_REL, "stylesheet", KATTR_TYPE, "text/css",
2309 KATTR_HREF, css, KATTR__MAX);
2310 if (kerr != KCGI_OK)
2311 return 0;
2312 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2313 if (kerr != KCGI_OK)
2314 return 0;
2315 break;
2316 case(TEMPL_HEADER):
2317 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2318 KATTR_ID, "got_link", KATTR__MAX);
2319 if (kerr != KCGI_OK)
2320 return 0;
2321 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2322 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
2323 KATTR_TARGET, "_sotd", KATTR__MAX);
2324 if (kerr != KCGI_OK)
2325 return 0;
2326 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
2327 KATTR_SRC, logo, KATTR__MAX);
2328 if (kerr != KCGI_OK)
2329 return 0;
2330 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2331 if (kerr != KCGI_OK)
2332 return 0;
2333 break;
2334 case (TEMPL_SITEPATH):
2335 error = gw_output_site_link(gw_trans);
2336 if (error)
2337 return 0;
2338 break;
2339 case(TEMPL_TITLE):
2340 if (gw_trans->gw_conf->got_site_name != NULL) {
2341 kerr = khtml_puts(gw_trans->gw_html_req,
2342 gw_trans->gw_conf->got_site_name);
2343 if (kerr != KCGI_OK)
2344 return 0;
2346 break;
2347 case (TEMPL_SEARCH):
2348 break;
2349 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
2350 "search", KATTR__MAX);
2351 if (kerr != KCGI_OK)
2352 return 0;
2353 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
2354 KATTR_METHOD, "POST", KATTR__MAX);
2355 if (kerr != KCGI_OK)
2356 return 0;
2357 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
2358 "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
2359 KATTR_MAXLENGTH, "50", KATTR__MAX);
2360 if (kerr != KCGI_OK)
2361 return 0;
2362 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
2363 KATTR__MAX);
2364 if (kerr != KCGI_OK)
2365 return 0;
2366 kerr = khtml_puts(gw_trans->gw_html_req, "Search");
2367 if (kerr != KCGI_OK)
2368 return 0;
2369 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
2370 if (kerr != KCGI_OK)
2371 return 0;
2372 break;
2373 case(TEMPL_SITEOWNER):
2374 if (gw_trans->gw_conf->got_site_owner != NULL &&
2375 gw_trans->gw_conf->got_show_site_owner) {
2376 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2377 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
2378 if (kerr != KCGI_OK)
2379 return 0;
2380 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2381 KATTR_ID, "site_owner", KATTR__MAX);
2382 if (kerr != KCGI_OK)
2383 return 0;
2384 kerr = khtml_puts(gw_trans->gw_html_req,
2385 gw_trans->gw_conf->got_site_owner);
2386 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2387 if (kerr != KCGI_OK)
2388 return 0;
2390 break;
2391 case(TEMPL_CONTENT):
2392 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
2393 if (error) {
2394 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2395 KATTR_ID, "tmpl_err", KATTR__MAX);
2396 if (kerr != KCGI_OK)
2397 return 0;
2398 kerr = khttp_printf(gw_trans->gw_req, "Error: %s",
2399 error->msg);
2400 if (kerr != KCGI_OK)
2401 return 0;
2402 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2403 if (kerr != KCGI_OK)
2404 return 0;
2406 break;
2407 default:
2408 return 0;
2410 free(ati);
2411 free(fic32);
2412 free(fic16);
2413 free(swm);
2414 free(spt);
2415 free(css);
2416 free(logo);
2417 return 1;
2418 err:
2419 free(ati);
2420 free(fic32);
2421 free(fic16);
2422 free(swm);
2423 free(spt);
2424 free(css);
2425 free(logo);
2426 return 0;
2429 static const struct got_error *
2430 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
2432 const struct got_error *error = NULL;
2433 enum kcgi_err kerr = KCGI_OK;
2435 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2436 KATTR_ID, "header_commit_title", KATTR__MAX);
2437 if (kerr != KCGI_OK)
2438 goto done;
2439 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
2440 if (kerr != KCGI_OK)
2441 goto done;
2442 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2443 if (kerr != KCGI_OK)
2444 goto done;
2445 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2446 KATTR_ID, "header_commit", KATTR__MAX);
2447 if (kerr != KCGI_OK)
2448 goto done;
2449 kerr = khtml_printf(gw_trans->gw_html_req, "%s ", str1);
2450 if (kerr != KCGI_OK)
2451 goto done;
2452 if (str2 != NULL) {
2453 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
2454 KATTR_ID, "refs_str", KATTR__MAX);
2455 if (kerr != KCGI_OK)
2456 goto done;
2457 kerr = khtml_printf(gw_trans->gw_html_req, "(%s)", str2);
2458 if (kerr != KCGI_OK)
2459 goto done;
2460 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2461 if (kerr != KCGI_OK)
2462 goto done;
2464 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2465 done:
2466 if (error == NULL && kerr != KCGI_OK)
2467 error = gw_kcgi_error(kerr);
2468 return error;
2471 static const struct got_error *
2472 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2474 const struct got_error *error = NULL;
2475 enum kcgi_err kerr = KCGI_OK;
2477 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2478 KATTR_ID, "header_diff_title", KATTR__MAX);
2479 if (kerr != KCGI_OK)
2480 goto done;
2481 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2482 if (kerr != KCGI_OK)
2483 goto done;
2484 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2485 if (kerr != KCGI_OK)
2486 goto done;
2487 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2488 KATTR_ID, "header_diff", KATTR__MAX);
2489 if (kerr != KCGI_OK)
2490 goto done;
2491 if (str1 != NULL) {
2492 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2493 if (kerr != KCGI_OK)
2494 goto done;
2496 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2497 if (kerr != KCGI_OK)
2498 goto done;
2499 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2500 if (kerr != KCGI_OK)
2501 goto done;
2502 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2503 done:
2504 if (error == NULL && kerr != KCGI_OK)
2505 error = gw_kcgi_error(kerr);
2506 return error;
2509 static const struct got_error *
2510 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2512 const struct got_error *error = NULL;
2513 enum kcgi_err kerr = KCGI_OK;
2515 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2516 KATTR_ID, "header_age_title", KATTR__MAX);
2517 if (kerr != KCGI_OK)
2518 goto done;
2519 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2520 if (kerr != KCGI_OK)
2521 goto done;
2522 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2523 if (kerr != KCGI_OK)
2524 goto done;
2525 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2526 KATTR_ID, "header_age", KATTR__MAX);
2527 if (kerr != KCGI_OK)
2528 goto done;
2529 kerr = khtml_puts(gw_trans->gw_html_req, str);
2530 if (kerr != KCGI_OK)
2531 goto done;
2532 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2533 done:
2534 if (error == NULL && kerr != KCGI_OK)
2535 error = gw_kcgi_error(kerr);
2536 return error;
2539 static const struct got_error *
2540 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2542 const struct got_error *error = NULL;
2543 enum kcgi_err kerr = KCGI_OK;
2545 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2546 KATTR_ID, "header_author_title", KATTR__MAX);
2547 if (kerr != KCGI_OK)
2548 goto done;
2549 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2550 if (kerr != KCGI_OK)
2551 goto done;
2552 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2553 if (kerr != KCGI_OK)
2554 goto done;
2555 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2556 KATTR_ID, "header_author", KATTR__MAX);
2557 if (kerr != KCGI_OK)
2558 goto done;
2559 kerr = khtml_puts(gw_trans->gw_html_req, str);
2560 if (kerr != KCGI_OK)
2561 goto done;
2562 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2563 done:
2564 if (error == NULL && kerr != KCGI_OK)
2565 error = gw_kcgi_error(kerr);
2566 return error;
2569 static const struct got_error *
2570 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2572 const struct got_error *error = NULL;
2573 enum kcgi_err kerr = KCGI_OK;
2575 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2576 KATTR_ID, "header_committer_title", KATTR__MAX);
2577 if (kerr != KCGI_OK)
2578 goto done;
2579 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2580 if (kerr != KCGI_OK)
2581 goto done;
2582 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2583 if (kerr != KCGI_OK)
2584 goto done;
2585 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2586 KATTR_ID, "header_committer", KATTR__MAX);
2587 if (kerr != KCGI_OK)
2588 goto done;
2589 kerr = khtml_puts(gw_trans->gw_html_req, str);
2590 if (kerr != KCGI_OK)
2591 goto done;
2592 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2593 done:
2594 if (error == NULL && kerr != KCGI_OK)
2595 error = gw_kcgi_error(kerr);
2596 return error;
2599 static const struct got_error *
2600 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2602 const struct got_error *error = NULL;
2603 enum kcgi_err kerr = KCGI_OK;
2605 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2606 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2607 if (kerr != KCGI_OK)
2608 goto done;
2609 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2610 if (kerr != KCGI_OK)
2611 goto done;
2612 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2613 if (kerr != KCGI_OK)
2614 goto done;
2615 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2616 KATTR_ID, "header_commit_msg", KATTR__MAX);
2617 if (kerr != KCGI_OK)
2618 goto done;
2619 kerr = khttp_puts(gw_trans->gw_req, str);
2620 if (kerr != KCGI_OK)
2621 goto done;
2622 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2623 done:
2624 if (error == NULL && kerr != KCGI_OK)
2625 error = gw_kcgi_error(kerr);
2626 return error;
2629 static const struct got_error *
2630 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2632 const struct got_error *error = NULL;
2633 enum kcgi_err kerr = KCGI_OK;
2635 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2636 KATTR_ID, "header_tree_title", KATTR__MAX);
2637 if (kerr != KCGI_OK)
2638 goto done;
2639 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2640 if (kerr != KCGI_OK)
2641 goto done;
2642 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2643 if (kerr != KCGI_OK)
2644 goto done;
2645 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2646 KATTR_ID, "header_tree", KATTR__MAX);
2647 if (kerr != KCGI_OK)
2648 goto done;
2649 kerr = khtml_puts(gw_trans->gw_html_req, str);
2650 if (kerr != KCGI_OK)
2651 goto done;
2652 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2653 done:
2654 if (error == NULL && kerr != KCGI_OK)
2655 error = gw_kcgi_error(kerr);
2656 return error;
2659 static const struct got_error *
2660 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2661 char *dir)
2663 const struct got_error *error = NULL;
2664 FILE *f = NULL;
2665 char *d_file = NULL;
2666 unsigned int len;
2667 size_t n;
2669 *description = NULL;
2670 if (gw_trans->gw_conf->got_show_repo_description == 0)
2671 return NULL;
2673 if (asprintf(&d_file, "%s/description", dir) == -1)
2674 return got_error_from_errno("asprintf");
2676 f = fopen(d_file, "re");
2677 if (f == NULL) {
2678 if (errno == ENOENT || errno == EACCES)
2679 return NULL;
2680 error = got_error_from_errno2("fopen", d_file);
2681 goto done;
2684 if (fseek(f, 0, SEEK_END) == -1) {
2685 error = got_ferror(f, GOT_ERR_IO);
2686 goto done;
2688 len = ftell(f);
2689 if (len == -1) {
2690 error = got_ferror(f, GOT_ERR_IO);
2691 goto done;
2693 if (fseek(f, 0, SEEK_SET) == -1) {
2694 error = got_ferror(f, GOT_ERR_IO);
2695 goto done;
2697 *description = calloc(len + 1, sizeof(**description));
2698 if (*description == NULL) {
2699 error = got_error_from_errno("calloc");
2700 goto done;
2703 n = fread(*description, 1, len, f);
2704 if (n == 0 && ferror(f))
2705 error = got_ferror(f, GOT_ERR_IO);
2706 done:
2707 if (f != NULL && fclose(f) == EOF && error == NULL)
2708 error = got_error_from_errno("fclose");
2709 free(d_file);
2710 return error;
2713 static const struct got_error *
2714 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2716 struct tm tm;
2717 time_t diff_time;
2718 const char *years = "years ago", *months = "months ago";
2719 const char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2720 const char *minutes = "minutes ago", *seconds = "seconds ago";
2721 const char *now = "right now";
2722 const char *s;
2723 char datebuf[29];
2725 *repo_age = NULL;
2727 switch (ref_tm) {
2728 case TM_DIFF:
2729 diff_time = time(NULL) - committer_time;
2730 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2731 if (asprintf(repo_age, "%lld %s",
2732 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2733 return got_error_from_errno("asprintf");
2734 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2735 if (asprintf(repo_age, "%lld %s",
2736 (diff_time / 60 / 60 / 24 / (365 / 12)),
2737 months) == -1)
2738 return got_error_from_errno("asprintf");
2739 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2740 if (asprintf(repo_age, "%lld %s",
2741 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2742 return got_error_from_errno("asprintf");
2743 } else if (diff_time > 60 * 60 * 24 * 2) {
2744 if (asprintf(repo_age, "%lld %s",
2745 (diff_time / 60 / 60 / 24), days) == -1)
2746 return got_error_from_errno("asprintf");
2747 } else if (diff_time > 60 * 60 * 2) {
2748 if (asprintf(repo_age, "%lld %s",
2749 (diff_time / 60 / 60), hours) == -1)
2750 return got_error_from_errno("asprintf");
2751 } else if (diff_time > 60 * 2) {
2752 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2753 minutes) == -1)
2754 return got_error_from_errno("asprintf");
2755 } else if (diff_time > 2) {
2756 if (asprintf(repo_age, "%lld %s", diff_time,
2757 seconds) == -1)
2758 return got_error_from_errno("asprintf");
2759 } else {
2760 if (asprintf(repo_age, "%s", now) == -1)
2761 return got_error_from_errno("asprintf");
2763 break;
2764 case TM_LONG:
2765 if (gmtime_r(&committer_time, &tm) == NULL)
2766 return got_error_from_errno("gmtime_r");
2768 s = asctime_r(&tm, datebuf);
2769 if (s == NULL)
2770 return got_error_from_errno("asctime_r");
2772 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2773 return got_error_from_errno("asprintf");
2774 break;
2776 return NULL;
2779 static const struct got_error *
2780 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2781 const char *refname, int ref_tm)
2783 const struct got_error *error = NULL;
2784 struct got_repository *repo = NULL;
2785 struct got_commit_object *commit = NULL;
2786 struct got_reflist_head refs;
2787 struct got_reflist_entry *re;
2788 time_t committer_time = 0, cmp_time = 0;
2790 *repo_age = NULL;
2791 TAILQ_INIT(&refs);
2793 if (gw_trans->gw_conf->got_show_repo_age == 0)
2794 return NULL;
2796 if (gw_trans->repo)
2797 repo = gw_trans->repo;
2798 else {
2799 error = got_repo_open(&repo, dir, NULL, gw_trans->pack_fds);
2800 if (error)
2801 return error;
2804 error = got_ref_list(&refs, repo, "refs/heads",
2805 got_ref_cmp_by_name, NULL);
2806 if (error)
2807 goto done;
2810 * Find the youngest branch tip in the repository, or the age of
2811 * the a specific branch tip if a name was provided by the caller.
2813 TAILQ_FOREACH(re, &refs, entry) {
2814 struct got_object_id *id = NULL;
2816 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
2817 continue;
2819 error = got_ref_resolve(&id, repo, re->ref);
2820 if (error)
2821 goto done;
2823 error = got_object_open_as_commit(&commit, repo, id);
2824 free(id);
2825 if (error)
2826 goto done;
2828 committer_time =
2829 got_object_commit_get_committer_time(commit);
2830 got_object_commit_close(commit);
2831 if (cmp_time < committer_time)
2832 cmp_time = committer_time;
2834 if (refname)
2835 break;
2838 if (cmp_time != 0) {
2839 committer_time = cmp_time;
2840 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2842 done:
2843 got_ref_list_free(&refs);
2844 if (gw_trans->repo == NULL) {
2845 const struct got_error *close_err = got_repo_close(repo);
2846 if (error == NULL)
2847 error = close_err;
2849 return error;
2852 static const struct got_error *
2853 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2855 const struct got_error *error;
2856 FILE *f = NULL, *f1 = NULL, *f2 = NULL;
2857 int fd1 = -1, fd2 = -1;
2858 struct got_object_id *id1 = NULL, *id2 = NULL;
2859 char *label1 = NULL, *label2 = NULL, *line = NULL;
2860 int obj_type;
2861 size_t linesize = 0;
2862 ssize_t linelen;
2863 enum kcgi_err kerr = KCGI_OK;
2865 f = got_opentemp();
2866 if (f == NULL)
2867 return NULL;
2869 f1 = got_opentemp();
2870 if (f1 == NULL) {
2871 error = got_error_from_errno("got_opentemp");
2872 goto done;
2875 f2 = got_opentemp();
2876 if (f2 == NULL) {
2877 error = got_error_from_errno("got_opentemp");
2878 goto done;
2881 fd1 = got_opentempfd();
2882 if (fd1 == -1) {
2883 error = got_error_from_errno("got_opentempfd");
2884 goto done;
2887 fd2 = got_opentempfd();
2888 if (fd2 == -1) {
2889 error = got_error_from_errno("got_opentempfd");
2890 goto done;
2893 if (header->parent_id != NULL &&
2894 strncmp(header->parent_id, "/dev/null", 9) != 0) {
2895 error = got_repo_match_object_id(&id1, &label1,
2896 header->parent_id, GOT_OBJ_TYPE_ANY,
2897 &header->refs, gw_trans->repo);
2898 if (error)
2899 goto done;
2902 error = got_repo_match_object_id(&id2, &label2,
2903 header->commit_id, GOT_OBJ_TYPE_ANY, &header->refs,
2904 gw_trans->repo);
2905 if (error)
2906 goto done;
2908 error = got_object_get_type(&obj_type, gw_trans->repo, id2);
2909 if (error)
2910 goto done;
2911 switch (obj_type) {
2912 case GOT_OBJ_TYPE_BLOB:
2913 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
2914 fd1, fd2, id1, id2, NULL, NULL, 3, 0, 0,
2915 gw_trans->repo, f);
2916 break;
2917 case GOT_OBJ_TYPE_TREE:
2918 error = got_diff_objects_as_trees(NULL, NULL, f1, f2,
2919 fd1, fd2, id1, id2, NULL, "", "", 3, 0, 0,
2920 gw_trans->repo, f);
2921 break;
2922 case GOT_OBJ_TYPE_COMMIT:
2923 error = got_diff_objects_as_commits(NULL, NULL, f1, f2,
2924 fd1, fd2, id1, id2, NULL, 3, 0, 0, gw_trans->repo, f);
2925 break;
2926 default:
2927 error = got_error(GOT_ERR_OBJ_TYPE);
2929 if (error)
2930 goto done;
2932 if (fseek(f, 0, SEEK_SET) == -1) {
2933 error = got_ferror(f, GOT_ERR_IO);
2934 goto done;
2937 while ((linelen = getline(&line, &linesize, f)) != -1) {
2938 error = gw_colordiff_line(gw_trans, line);
2939 if (error)
2940 goto done;
2941 /* XXX: KHTML_PRETTY breaks this */
2942 kerr = khtml_puts(gw_trans->gw_html_req, line);
2943 if (kerr != KCGI_OK)
2944 goto done;
2945 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2946 if (kerr != KCGI_OK)
2947 goto done;
2949 if (linelen == -1 && ferror(f))
2950 error = got_error_from_errno("getline");
2951 done:
2952 if (f && fclose(f) == EOF && error == NULL)
2953 error = got_error_from_errno("fclose");
2954 if (f1 && fclose(f1) == EOF && error == NULL)
2955 error = got_error_from_errno("fclose");
2956 if (f2 && fclose(f2) == EOF && error == NULL)
2957 error = got_error_from_errno("fclose");
2958 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
2959 error = got_error_from_errno("close");
2960 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
2961 error = got_error_from_errno("close");
2962 free(line);
2963 free(label1);
2964 free(label2);
2965 free(id1);
2966 free(id2);
2968 if (error == NULL && kerr != KCGI_OK)
2969 error = gw_kcgi_error(kerr);
2970 return error;
2973 static const struct got_error *
2974 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2976 const struct got_error *error = NULL, *close_err;
2977 struct got_repository *repo;
2978 const char *gitconfig_owner;
2980 *owner = NULL;
2982 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2983 return NULL;
2985 error = got_repo_open(&repo, dir, NULL, gw_trans->pack_fds);
2986 if (error)
2987 return error;
2989 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2990 if (gitconfig_owner) {
2991 *owner = strdup(gitconfig_owner);
2992 if (*owner == NULL)
2993 error = got_error_from_errno("strdup");
2995 close_err = got_repo_close(repo);
2996 if (error == NULL)
2997 error = close_err;
2998 return error;
3001 static const struct got_error *
3002 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
3004 const struct got_error *error = NULL;
3005 FILE *f;
3006 char *d_file = NULL;
3007 unsigned int len;
3008 size_t n;
3010 *url = NULL;
3012 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
3013 return got_error_from_errno("asprintf");
3015 f = fopen(d_file, "re");
3016 if (f == NULL) {
3017 if (errno != ENOENT && errno != EACCES)
3018 error = got_error_from_errno2("fopen", d_file);
3019 goto done;
3022 if (fseek(f, 0, SEEK_END) == -1) {
3023 error = got_ferror(f, GOT_ERR_IO);
3024 goto done;
3026 len = ftell(f);
3027 if (len == -1) {
3028 error = got_ferror(f, GOT_ERR_IO);
3029 goto done;
3031 if (fseek(f, 0, SEEK_SET) == -1) {
3032 error = got_ferror(f, GOT_ERR_IO);
3033 goto done;
3036 *url = calloc(len + 1, sizeof(**url));
3037 if (*url == NULL) {
3038 error = got_error_from_errno("calloc");
3039 goto done;
3042 n = fread(*url, 1, len, f);
3043 if (n == 0 && ferror(f))
3044 error = got_ferror(f, GOT_ERR_IO);
3045 done:
3046 if (f && fclose(f) == EOF && error == NULL)
3047 error = got_error_from_errno("fclose");
3048 free(d_file);
3049 return NULL;
3052 static const struct got_error *
3053 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
3054 int limit, int tag_type)
3056 const struct got_error *error = NULL;
3057 struct got_reflist_head refs;
3058 struct got_reflist_entry *re;
3059 char *age = NULL;
3060 char *id_str = NULL, *newline, *href_commits = NULL;
3061 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
3062 struct got_tag_object *tag = NULL;
3063 enum kcgi_err kerr = KCGI_OK;
3064 int summary_header_displayed = 0, chk_next = 0;
3065 int tag_count = 0, commit_found = 0, c_cnt = 0;
3067 TAILQ_INIT(&refs);
3069 error = got_ref_list(&refs, gw_trans->repo, "refs/tags",
3070 got_ref_cmp_tags, gw_trans->repo);
3071 if (error)
3072 goto done;
3074 TAILQ_FOREACH(re, &refs, entry) {
3075 const char *refname;
3076 const char *tagger;
3077 const char *tag_commit;
3078 time_t tagger_time;
3079 struct got_object_id *id;
3080 struct got_commit_object *commit = NULL;
3082 refname = got_ref_get_name(re->ref);
3083 if (strncmp(refname, "refs/tags/", 10) != 0)
3084 continue;
3085 refname += 10;
3087 error = got_ref_resolve(&id, gw_trans->repo, re->ref);
3088 if (error)
3089 goto done;
3091 error = got_object_open_as_tag(&tag, gw_trans->repo, id);
3092 if (error) {
3093 if (error->code != GOT_ERR_OBJ_TYPE) {
3094 free(id);
3095 goto done;
3097 /* "lightweight" tag */
3098 error = got_object_open_as_commit(&commit,
3099 gw_trans->repo, id);
3100 if (error) {
3101 free(id);
3102 goto done;
3104 tagger = got_object_commit_get_committer(commit);
3105 tagger_time =
3106 got_object_commit_get_committer_time(commit);
3107 error = got_object_id_str(&id_str, id);
3108 free(id);
3109 } else {
3110 free(id);
3111 tagger = got_object_tag_get_tagger(tag);
3112 tagger_time = got_object_tag_get_tagger_time(tag);
3113 error = got_object_id_str(&id_str,
3114 got_object_tag_get_object_id(tag));
3116 if (error)
3117 goto done;
3119 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
3120 strlen(id_str)) != 0)
3121 continue;
3123 if (tag_type == TAGBRIEF && gw_trans->commit_id &&
3124 commit_found == 0 && strncmp(id_str, gw_trans->commit_id,
3125 strlen(id_str)) != 0)
3126 continue;
3127 else
3128 commit_found = 1;
3130 tag_count++;
3132 if (chk_next) {
3133 gw_trans->next_id = strdup(id_str);
3134 if (gw_trans->next_id == NULL)
3135 error = got_error_from_errno("strdup");
3136 goto prev;
3139 if (commit) {
3140 error = got_object_commit_get_logmsg(&tag_commit0,
3141 commit);
3142 if (error)
3143 goto done;
3144 got_object_commit_close(commit);
3145 } else {
3146 tag_commit0 = strdup(got_object_tag_get_message(tag));
3147 if (tag_commit0 == NULL) {
3148 error = got_error_from_errno("strdup");
3149 goto done;
3153 tag_commit = tag_commit0;
3154 while (*tag_commit == '\n')
3155 tag_commit++;
3157 switch (tag_type) {
3158 case TAGBRIEF:
3159 newline = strchr(tag_commit, '\n');
3160 if (newline)
3161 *newline = '\0';
3163 if (summary_header_displayed == 0) {
3164 kerr = khtml_attr(gw_trans->gw_html_req,
3165 KELEM_DIV, KATTR_ID,
3166 "summary_tags_title_wrapper", KATTR__MAX);
3167 if (kerr != KCGI_OK)
3168 goto done;
3169 kerr = khtml_attr(gw_trans->gw_html_req,
3170 KELEM_DIV, KATTR_ID,
3171 "summary_tags_title", KATTR__MAX);
3172 if (kerr != KCGI_OK)
3173 goto done;
3174 kerr = khtml_puts(gw_trans->gw_html_req,
3175 "Tags");
3176 if (kerr != KCGI_OK)
3177 goto done;
3178 kerr = khtml_closeelem(gw_trans->gw_html_req,
3179 2);
3180 if (kerr != KCGI_OK)
3181 goto done;
3182 kerr = khtml_attr(gw_trans->gw_html_req,
3183 KELEM_DIV, KATTR_ID,
3184 "summary_tags_content", KATTR__MAX);
3185 if (kerr != KCGI_OK)
3186 goto done;
3187 summary_header_displayed = 1;
3190 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3191 KATTR_ID, "tag_wrapper", KATTR__MAX);
3192 if (kerr != KCGI_OK)
3193 goto done;
3194 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3195 KATTR_ID, "tag_age", KATTR__MAX);
3196 if (kerr != KCGI_OK)
3197 goto done;
3198 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
3199 if (error)
3200 goto done;
3201 kerr = khtml_puts(gw_trans->gw_html_req,
3202 age ? age : "");
3203 if (kerr != KCGI_OK)
3204 goto done;
3205 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3206 if (kerr != KCGI_OK)
3207 goto done;
3208 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3209 KATTR_ID, "tag", KATTR__MAX);
3210 if (kerr != KCGI_OK)
3211 goto done;
3212 kerr = khtml_puts(gw_trans->gw_html_req, refname);
3213 if (kerr != KCGI_OK)
3214 goto done;
3215 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3216 if (kerr != KCGI_OK)
3217 goto done;
3218 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3219 KATTR_ID, "tag_name", KATTR__MAX);
3220 if (kerr != KCGI_OK)
3221 goto done;
3223 href_tag = khttp_urlpart(NULL, NULL, "gotweb", "path",
3224 gw_trans->repo_name, "action", "tag", "commit",
3225 id_str, NULL);
3226 if (href_tag == NULL) {
3227 error = got_error_from_errno("khttp_urlpart");
3228 goto done;
3230 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3231 KATTR_HREF, href_tag, KATTR__MAX);
3232 if (kerr != KCGI_OK)
3233 goto done;
3234 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
3235 if (kerr != KCGI_OK)
3236 goto done;
3237 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3238 if (kerr != KCGI_OK)
3239 goto done;
3241 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3242 KATTR_ID, "navs_wrapper", KATTR__MAX);
3243 if (kerr != KCGI_OK)
3244 goto done;
3245 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3246 KATTR_ID, "navs", KATTR__MAX);
3247 if (kerr != KCGI_OK)
3248 goto done;
3250 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3251 KATTR_HREF, href_tag, KATTR__MAX);
3252 if (kerr != KCGI_OK)
3253 goto done;
3254 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
3255 if (kerr != KCGI_OK)
3256 goto done;
3257 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3258 if (kerr != KCGI_OK)
3259 goto done;
3261 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3262 if (kerr != KCGI_OK)
3263 goto done;
3265 href_briefs = khttp_urlpart(NULL, NULL, "gotweb",
3266 "path", gw_trans->repo_name, "action", "briefs",
3267 "commit", id_str, NULL);
3268 if (href_briefs == NULL) {
3269 error = got_error_from_errno("khttp_urlpart");
3270 goto done;
3272 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3273 KATTR_HREF, href_briefs, KATTR__MAX);
3274 if (kerr != KCGI_OK)
3275 goto done;
3276 kerr = khtml_puts(gw_trans->gw_html_req,
3277 "commit briefs");
3278 if (kerr != KCGI_OK)
3279 goto done;
3280 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3281 if (kerr != KCGI_OK)
3282 goto done;
3284 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3285 if (kerr != KCGI_OK)
3286 goto done;
3288 href_commits = khttp_urlpart(NULL, NULL, "gotweb",
3289 "path", gw_trans->repo_name, "action", "commits",
3290 "commit", id_str, NULL);
3291 if (href_commits == NULL) {
3292 error = got_error_from_errno("khttp_urlpart");
3293 goto done;
3295 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3296 KATTR_HREF, href_commits, KATTR__MAX);
3297 if (kerr != KCGI_OK)
3298 goto done;
3299 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
3300 if (kerr != KCGI_OK)
3301 goto done;
3302 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3303 if (kerr != KCGI_OK)
3304 goto done;
3306 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3307 KATTR_ID, "dotted_line", KATTR__MAX);
3308 if (kerr != KCGI_OK)
3309 goto done;
3310 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3311 if (kerr != KCGI_OK)
3312 goto done;
3313 break;
3314 case TAGFULL:
3315 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3316 KATTR_ID, "tag_info_date_title", KATTR__MAX);
3317 if (kerr != KCGI_OK)
3318 goto done;
3319 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
3320 if (kerr != KCGI_OK)
3321 goto done;
3322 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3323 if (kerr != KCGI_OK)
3324 goto done;
3325 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3326 KATTR_ID, "tag_info_date", KATTR__MAX);
3327 if (kerr != KCGI_OK)
3328 goto done;
3329 error = gw_get_time_str(&age, tagger_time, TM_LONG);
3330 if (error)
3331 goto done;
3332 kerr = khtml_puts(gw_trans->gw_html_req,
3333 age ? age : "");
3334 if (kerr != KCGI_OK)
3335 goto done;
3336 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3337 if (kerr != KCGI_OK)
3338 goto done;
3340 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3341 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
3342 if (kerr != KCGI_OK)
3343 goto done;
3344 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
3345 if (kerr != KCGI_OK)
3346 goto done;
3347 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3348 if (kerr != KCGI_OK)
3349 goto done;
3350 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3351 KATTR_ID, "tag_info_date", KATTR__MAX);
3352 if (kerr != KCGI_OK)
3353 goto done;
3354 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
3355 if (kerr != KCGI_OK)
3356 goto done;
3357 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3358 if (kerr != KCGI_OK)
3359 goto done;
3361 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3362 KATTR_ID, "tag_info", KATTR__MAX);
3363 if (kerr != KCGI_OK)
3364 goto done;
3365 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
3366 if (kerr != KCGI_OK)
3367 goto done;
3368 break;
3369 default:
3370 break;
3372 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3373 if (kerr != KCGI_OK)
3374 goto done;
3376 if (limit && --limit == 0)
3377 chk_next = 1;
3379 if (tag)
3380 got_object_tag_close(tag);
3381 tag = NULL;
3382 free(id_str);
3383 id_str = NULL;
3384 free(age);
3385 age = NULL;
3386 free(tag_commit0);
3387 tag_commit0 = NULL;
3388 free(href_tag);
3389 href_tag = NULL;
3390 free(href_briefs);
3391 href_briefs = NULL;
3392 free(href_commits);
3393 href_commits = NULL;
3395 if (tag_count == 0) {
3396 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3397 "summary_tags_title_wrapper", KATTR__MAX);
3398 if (kerr != KCGI_OK)
3399 goto done;
3400 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3401 "summary_tags_title", KATTR__MAX);
3402 if (kerr != KCGI_OK)
3403 goto done;
3404 kerr = khtml_puts(gw_trans->gw_html_req, "Tags");
3405 if (kerr != KCGI_OK)
3406 goto done;
3407 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3408 if (kerr != KCGI_OK)
3409 goto done;
3410 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3411 "summary_tags_content", KATTR__MAX);
3412 if (kerr != KCGI_OK)
3413 goto done;
3414 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3415 "tags_info", KATTR__MAX);
3416 if (kerr != KCGI_OK)
3417 goto done;
3418 kerr = khttp_puts(gw_trans->gw_req,
3419 "There are no tags for this repo.");
3420 if (kerr != KCGI_OK)
3421 goto done;
3422 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3423 goto done;
3425 prev:
3426 commit_found = 0;
3427 TAILQ_FOREACH_REVERSE(re, &refs, got_reflist_head, entry) {
3428 const char *refname;
3429 struct got_object_id *id;
3430 struct got_commit_object *commit = NULL;
3432 refname = got_ref_get_name(re->ref);
3433 if (strncmp(refname, "refs/tags/", 10) != 0)
3434 continue;
3435 refname += 10;
3437 error = got_ref_resolve(&id, gw_trans->repo, re->ref);
3438 if (error)
3439 goto done;
3441 error = got_object_open_as_tag(&tag, gw_trans->repo, id);
3442 if (error) {
3443 if (error->code != GOT_ERR_OBJ_TYPE) {
3444 free(id);
3445 goto done;
3447 /* "lightweight" tag */
3448 error = got_object_open_as_commit(&commit,
3449 gw_trans->repo, id);
3450 if (error) {
3451 free(id);
3452 goto done;
3454 error = got_object_id_str(&id_str, id);
3455 free(id);
3456 } else {
3457 free(id);
3458 error = got_object_id_str(&id_str,
3459 got_object_tag_get_object_id(tag));
3461 if (error)
3462 goto done;
3464 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
3465 strlen(id_str)) != 0)
3466 continue;
3468 if (commit_found == 0 && tag_type == TAGBRIEF &&
3469 gw_trans->commit_id != NULL &&
3470 strncmp(id_str, gw_trans->commit_id, strlen(id_str)) != 0)
3471 continue;
3472 else
3473 commit_found = 1;
3475 if (gw_trans->commit_id != NULL &&
3476 strcmp(id_str, gw_trans->commit_id) != 0 &&
3477 (re == TAILQ_FIRST(&refs) ||
3478 c_cnt == gw_trans->gw_conf->got_max_commits_display)) {
3479 gw_trans->prev_id = strdup(id_str);
3480 if (gw_trans->prev_id == NULL) {
3481 error = got_error_from_errno("strdup");
3482 goto done;
3484 break;
3486 c_cnt++;
3488 done:
3489 if (tag)
3490 got_object_tag_close(tag);
3491 free(id_str);
3492 free(age);
3493 free(tag_commit0);
3494 free(href_tag);
3495 free(href_briefs);
3496 free(href_commits);
3497 got_ref_list_free(&refs);
3498 if (error == NULL && kerr != KCGI_OK)
3499 error = gw_kcgi_error(kerr);
3500 return error;
3503 static void
3504 gw_free_header(struct gw_header *header)
3506 free(header->path);
3507 free(header->author);
3508 free(header->committer);
3509 free(header->refs_str);
3510 free(header->commit_id);
3511 free(header->parent_id);
3512 free(header->tree_id);
3513 free(header->commit_msg);
3516 static struct gw_header *
3517 gw_init_header()
3519 struct gw_header *header;
3521 header = malloc(sizeof(*header));
3522 if (header == NULL)
3523 return NULL;
3525 header->path = NULL;
3526 TAILQ_INIT(&header->refs);
3528 header->refs_str = NULL;
3529 header->commit_id = NULL;
3530 header->committer = NULL;
3531 header->author = NULL;
3532 header->parent_id = NULL;
3533 header->tree_id = NULL;
3534 header->commit_msg = NULL;
3536 return header;
3539 static const struct got_error *
3540 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
3541 int limit, struct got_object_id *id)
3543 const struct got_error *error = NULL;
3544 struct got_commit_graph *graph = NULL;
3545 struct got_commit_object *commit = NULL;
3546 int chk_next = 0, chk_multi = 0, c_cnt = 0, commit_found = 0;
3547 struct gw_header *t_header = NULL;
3549 error = got_commit_graph_open(&graph, header->path, 0);
3550 if (error)
3551 return error;
3553 error = got_commit_graph_iter_start(graph, id, gw_trans->repo, NULL,
3554 NULL);
3555 if (error)
3556 goto err;
3558 for (;;) {
3559 error = got_commit_graph_iter_next(&id, graph, gw_trans->repo,
3560 NULL, NULL);
3561 if (error) {
3562 if (error->code == GOT_ERR_ITER_COMPLETED)
3563 error = NULL;
3564 goto done;
3566 if (id == NULL)
3567 goto err;
3569 error = got_object_open_as_commit(&commit, gw_trans->repo, id);
3570 if (error)
3571 goto err;
3572 if (limit == 1 && chk_multi == 0 &&
3573 gw_trans->gw_conf->got_max_commits_display != 1) {
3574 error = gw_get_commit(gw_trans, header, commit, id);
3575 if (error)
3576 goto err;
3577 commit_found = 1;
3578 } else {
3579 chk_multi = 1;
3580 struct gw_header *n_header = NULL;
3581 if ((n_header = gw_init_header()) == NULL) {
3582 error = got_error_from_errno("malloc");
3583 goto err;
3585 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3586 entry);
3587 error = got_ref_list(&n_header->refs, gw_trans->repo,
3588 NULL, got_ref_cmp_by_name, NULL);
3589 if (error)
3590 goto err;
3592 error = gw_get_commit(gw_trans, n_header, commit, id);
3593 if (error)
3594 goto err;
3595 got_ref_list_free(&n_header->refs);
3597 if (gw_trans->commit_id != NULL) {
3598 if (strcmp(gw_trans->commit_id,
3599 n_header->commit_id) == 0)
3600 commit_found = 1;
3601 } else
3602 commit_found = 1;
3605 * check for one more commit before breaking,
3606 * so we know whether to navigate through gw_briefs
3607 * gw_commits and gw_summary
3609 if (chk_next && (gw_trans->action == GW_BRIEFS ||
3610 gw_trans->action == GW_COMMITS ||
3611 gw_trans->action == GW_SUMMARY)) {
3612 gw_trans->next_id = strdup(n_header->commit_id);
3613 if (gw_trans->next_id == NULL)
3614 error = got_error_from_errno("strdup");
3615 TAILQ_REMOVE(&gw_trans->gw_headers, n_header,
3616 entry);
3617 goto done;
3621 if (commit_found == 1 && (error || (limit && --limit == 0))) {
3622 if (chk_multi == 0)
3623 break;
3624 chk_next = 1;
3627 done:
3628 if (gw_trans->prev_id == NULL && gw_trans->commit_id != NULL &&
3629 (gw_trans->action == GW_BRIEFS || gw_trans->action == GW_COMMITS)) {
3630 commit_found = 0;
3631 TAILQ_FOREACH_REVERSE(t_header, &gw_trans->gw_headers,
3632 headers, entry) {
3633 if (commit_found == 0 &&
3634 strcmp(gw_trans->commit_id,
3635 t_header->commit_id) != 0)
3636 continue;
3637 else
3638 commit_found = 1;
3639 if (gw_trans->commit_id != NULL &&
3640 strcmp(gw_trans->commit_id,
3641 t_header->commit_id) != 0 &&
3642 (c_cnt == gw_trans->gw_conf->got_max_commits_display
3643 || t_header ==
3644 TAILQ_FIRST(&gw_trans->gw_headers))) {
3645 gw_trans->prev_id = strdup(t_header->commit_id);
3646 if (gw_trans->prev_id == NULL)
3647 error = got_error_from_errno("strdup");
3648 break;
3650 c_cnt++;
3653 err:
3654 if (commit != NULL)
3655 got_object_commit_close(commit);
3656 if (graph)
3657 got_commit_graph_close(graph);
3658 return error;
3661 static const struct got_error *
3662 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header,
3663 struct got_commit_object *commit, struct got_object_id *id)
3665 const struct got_error *error = NULL;
3666 struct got_reflist_entry *re;
3667 struct got_object_id *id2 = NULL;
3668 struct got_object_qid *parent_id;
3669 char *commit_msg = NULL, *commit_msg0;
3671 /*print commit*/
3672 TAILQ_FOREACH(re, &header->refs, entry) {
3673 char *s;
3674 const char *name;
3675 struct got_tag_object *tag = NULL;
3676 struct got_object_id *ref_id;
3677 int cmp;
3679 if (got_ref_is_symbolic(re->ref))
3680 continue;
3682 name = got_ref_get_name(re->ref);
3683 if (strncmp(name, "refs/", 5) == 0)
3684 name += 5;
3685 if (strncmp(name, "got/", 4) == 0)
3686 continue;
3687 if (strncmp(name, "heads/", 6) == 0)
3688 name += 6;
3689 if (strncmp(name, "remotes/", 8) == 0) {
3690 name += 8;
3691 s = strstr(name, "/" GOT_REF_HEAD);
3692 if (s != NULL && s[strlen(s)] == '\0')
3693 continue;
3695 error = got_ref_resolve(&ref_id, gw_trans->repo, re->ref);
3696 if (error)
3697 return error;
3698 if (strncmp(name, "tags/", 5) == 0) {
3699 error = got_object_open_as_tag(&tag, gw_trans->repo,
3700 ref_id);
3701 if (error) {
3702 if (error->code != GOT_ERR_OBJ_TYPE) {
3703 free(ref_id);
3704 continue;
3707 * Ref points at something other
3708 * than a tag.
3710 error = NULL;
3711 tag = NULL;
3714 cmp = got_object_id_cmp(tag ?
3715 got_object_tag_get_object_id(tag) : ref_id, id);
3716 free(ref_id);
3717 if (tag)
3718 got_object_tag_close(tag);
3719 if (cmp != 0)
3720 continue;
3721 s = header->refs_str;
3722 if (asprintf(&header->refs_str, "%s%s%s", s ? s : "",
3723 s ? ", " : "", name) == -1) {
3724 error = got_error_from_errno("asprintf");
3725 free(s);
3726 header->refs_str = NULL;
3727 return error;
3729 free(s);
3732 error = got_object_id_str(&header->commit_id, id);
3733 if (error)
3734 return error;
3736 error = got_object_id_str(&header->tree_id,
3737 got_object_commit_get_tree_id(commit));
3738 if (error)
3739 return error;
3741 if (gw_trans->action == GW_DIFF) {
3742 parent_id = STAILQ_FIRST(
3743 got_object_commit_get_parent_ids(commit));
3744 if (parent_id != NULL) {
3745 id2 = got_object_id_dup(&parent_id->id);
3746 free (parent_id);
3747 error = got_object_id_str(&header->parent_id, id2);
3748 if (error)
3749 return error;
3750 free(id2);
3751 } else {
3752 header->parent_id = strdup("/dev/null");
3753 if (header->parent_id == NULL) {
3754 error = got_error_from_errno("strdup");
3755 return error;
3760 header->committer_time =
3761 got_object_commit_get_committer_time(commit);
3763 header->author =
3764 strdup(got_object_commit_get_author(commit));
3765 if (header->author == NULL) {
3766 error = got_error_from_errno("strdup");
3767 return error;
3769 header->committer =
3770 strdup(got_object_commit_get_committer(commit));
3771 if (header->committer == NULL) {
3772 error = got_error_from_errno("strdup");
3773 return error;
3775 error = got_object_commit_get_logmsg(&commit_msg0, commit);
3776 if (error)
3777 return error;
3779 commit_msg = commit_msg0;
3780 while (*commit_msg == '\n')
3781 commit_msg++;
3783 header->commit_msg = strdup(commit_msg);
3784 if (header->commit_msg == NULL)
3785 error = got_error_from_errno("strdup");
3786 free(commit_msg0);
3787 return error;
3790 static const struct got_error *
3791 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3793 const struct got_error *error = NULL;
3794 char *in_repo_path = NULL;
3795 struct got_object_id *id = NULL;
3796 struct got_reference *ref;
3798 error = got_repo_open(&gw_trans->repo, gw_trans->repo_path, NULL,
3799 gw_trans->pack_fds);
3800 if (error)
3801 return error;
3803 if (gw_trans->commit_id == NULL || gw_trans->action == GW_COMMITS ||
3804 gw_trans->action == GW_BRIEFS || gw_trans->action == GW_SUMMARY ||
3805 gw_trans->action == GW_TAGS) {
3806 error = got_ref_open(&ref, gw_trans->repo,
3807 gw_trans->headref, 0);
3808 if (error)
3809 return error;
3811 error = got_ref_resolve(&id, gw_trans->repo, ref);
3812 got_ref_close(ref);
3813 if (error)
3814 return error;
3815 } else {
3816 error = got_ref_open(&ref, gw_trans->repo,
3817 gw_trans->commit_id, 0);
3818 if (error == NULL) {
3819 int obj_type;
3820 error = got_ref_resolve(&id, gw_trans->repo, ref);
3821 got_ref_close(ref);
3822 if (error)
3823 return error;
3824 error = got_object_get_type(&obj_type, gw_trans->repo,
3825 id);
3826 if (error)
3827 goto done;
3828 if (obj_type == GOT_OBJ_TYPE_TAG) {
3829 struct got_tag_object *tag;
3830 error = got_object_open_as_tag(&tag,
3831 gw_trans->repo, id);
3832 if (error)
3833 goto done;
3834 if (got_object_tag_get_object_type(tag) !=
3835 GOT_OBJ_TYPE_COMMIT) {
3836 got_object_tag_close(tag);
3837 error = got_error(GOT_ERR_OBJ_TYPE);
3838 goto done;
3840 free(id);
3841 id = got_object_id_dup(
3842 got_object_tag_get_object_id(tag));
3843 if (id == NULL)
3844 error = got_error_from_errno(
3845 "got_object_id_dup");
3846 got_object_tag_close(tag);
3847 if (error)
3848 goto done;
3849 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3850 error = got_error(GOT_ERR_OBJ_TYPE);
3851 goto done;
3854 error = got_repo_match_object_id_prefix(&id,
3855 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT,
3856 gw_trans->repo);
3857 if (error)
3858 goto done;
3861 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
3862 gw_trans->repo_path);
3863 if (error)
3864 goto done;
3866 if (in_repo_path) {
3867 header->path = strdup(in_repo_path);
3868 if (header->path == NULL) {
3869 error = got_error_from_errno("strdup");
3870 goto done;
3874 error = got_ref_list(&header->refs, gw_trans->repo, NULL,
3875 got_ref_cmp_by_name, NULL);
3876 if (error)
3877 goto done;
3879 error = gw_get_commits(gw_trans, header, limit, id);
3880 done:
3881 free(id);
3882 free(in_repo_path);
3883 return error;
3886 struct blame_line {
3887 int annotated;
3888 char *id_str;
3889 char *committer;
3890 char datebuf[11]; /* YYYY-MM-DD + NUL */
3893 struct gw_blame_cb_args {
3894 struct blame_line *lines;
3895 int nlines;
3896 int nlines_prec;
3897 int lineno_cur;
3898 off_t *line_offsets;
3899 FILE *f;
3900 struct got_repository *repo;
3901 struct gw_trans *gw_trans;
3904 static const struct got_error *
3905 gw_blame_cb(void *arg, int nlines, int lineno,
3906 struct got_commit_object *commit, struct got_object_id *id)
3908 const struct got_error *err = NULL;
3909 struct gw_blame_cb_args *a = arg;
3910 struct blame_line *bline;
3911 char *line = NULL;
3912 size_t linesize = 0;
3913 off_t offset;
3914 struct tm tm;
3915 time_t committer_time;
3916 enum kcgi_err kerr = KCGI_OK;
3918 if (nlines != a->nlines ||
3919 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3920 return got_error(GOT_ERR_RANGE);
3922 if (lineno == -1)
3923 return NULL; /* no change in this commit */
3925 /* Annotate this line. */
3926 bline = &a->lines[lineno - 1];
3927 if (bline->annotated)
3928 return NULL;
3929 err = got_object_id_str(&bline->id_str, id);
3930 if (err)
3931 return err;
3933 bline->committer = strdup(got_object_commit_get_committer(commit));
3934 if (bline->committer == NULL) {
3935 err = got_error_from_errno("strdup");
3936 goto done;
3939 committer_time = got_object_commit_get_committer_time(commit);
3940 if (gmtime_r(&committer_time, &tm) == NULL)
3941 return got_error_from_errno("gmtime_r");
3942 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3943 &tm) == 0) {
3944 err = got_error(GOT_ERR_NO_SPACE);
3945 goto done;
3947 bline->annotated = 1;
3949 /* Print lines annotated so far. */
3950 bline = &a->lines[a->lineno_cur - 1];
3951 if (!bline->annotated)
3952 goto done;
3954 offset = a->line_offsets[a->lineno_cur - 1];
3955 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3956 err = got_error_from_errno("fseeko");
3957 goto done;
3960 while (bline->annotated) {
3961 char *smallerthan, *at, *nl, *committer;
3962 char *href_diff = NULL;
3963 size_t len;
3965 if (getline(&line, &linesize, a->f) == -1) {
3966 if (ferror(a->f))
3967 err = got_error_from_errno("getline");
3968 break;
3971 committer = bline->committer;
3972 smallerthan = strchr(committer, '<');
3973 if (smallerthan && smallerthan[1] != '\0')
3974 committer = smallerthan + 1;
3975 at = strchr(committer, '@');
3976 if (at)
3977 *at = '\0';
3978 len = strlen(committer);
3979 if (len >= 9)
3980 committer[8] = '\0';
3982 nl = strchr(line, '\n');
3983 if (nl)
3984 *nl = '\0';
3986 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3987 "blame_wrapper", KATTR__MAX);
3988 if (kerr != KCGI_OK)
3989 goto err;
3990 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3991 "blame_number", KATTR__MAX);
3992 if (kerr != KCGI_OK)
3993 goto err;
3994 kerr = khtml_printf(a->gw_trans->gw_html_req, "%.*d",
3995 a->nlines_prec, a->lineno_cur);
3996 if (kerr != KCGI_OK)
3997 goto err;
3998 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3999 if (kerr != KCGI_OK)
4000 goto err;
4002 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4003 "blame_hash", KATTR__MAX);
4004 if (kerr != KCGI_OK)
4005 goto err;
4007 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
4008 a->gw_trans->repo_name, "action", "diff", "commit",
4009 bline->id_str, NULL);
4010 if (href_diff == NULL) {
4011 err = got_error_from_errno("khttp_urlpart");
4012 goto done;
4014 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
4015 KATTR_HREF, href_diff, KATTR__MAX);
4016 if (kerr != KCGI_OK)
4017 goto done;
4018 kerr = khtml_printf(a->gw_trans->gw_html_req, "%.8s",
4019 bline->id_str);
4020 if (kerr != KCGI_OK)
4021 goto err;
4022 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
4023 if (kerr != KCGI_OK)
4024 goto err;
4026 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4027 "blame_date", KATTR__MAX);
4028 if (kerr != KCGI_OK)
4029 goto err;
4030 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
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_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4038 "blame_author", KATTR__MAX);
4039 if (kerr != KCGI_OK)
4040 goto err;
4041 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
4042 if (kerr != KCGI_OK)
4043 goto err;
4044 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
4045 if (kerr != KCGI_OK)
4046 goto err;
4048 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4049 "blame_code", KATTR__MAX);
4050 if (kerr != KCGI_OK)
4051 goto err;
4052 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
4053 if (kerr != KCGI_OK)
4054 goto err;
4055 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
4056 if (kerr != KCGI_OK)
4057 goto err;
4059 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
4060 if (kerr != KCGI_OK)
4061 goto err;
4063 a->lineno_cur++;
4064 bline = &a->lines[a->lineno_cur - 1];
4065 err:
4066 free(href_diff);
4068 done:
4069 free(line);
4070 if (err == NULL && kerr != KCGI_OK)
4071 err = gw_kcgi_error(kerr);
4072 return err;
4075 static const struct got_error *
4076 gw_output_file_blame(struct gw_trans *gw_trans, struct gw_header *header)
4078 const struct got_error *error = NULL;
4079 struct got_object_id *obj_id = NULL;
4080 struct got_object_id *commit_id = NULL;
4081 struct got_commit_object *commit = NULL;
4082 struct got_blob_object *blob = NULL;
4083 char *path = NULL, *in_repo_path = NULL;
4084 struct gw_blame_cb_args bca;
4085 int i, obj_type, fd1 = -1, fd2 = -1, fd3 = -1;
4086 off_t filesize;
4087 FILE *f1 = NULL, *f2 = NULL;
4089 fd1 = got_opentempfd();
4090 if (fd1 == -1)
4091 return got_error_from_errno("got_opentempfd");
4092 fd2 = got_opentempfd();
4093 if (fd2 == -1) {
4094 error = got_error_from_errno("got_opentempfd");
4095 goto done;
4097 fd3 = got_opentempfd();
4098 if (fd3 == -1) {
4099 error = got_error_from_errno("got_opentempfd");
4100 goto done;
4103 memset(&bca, 0, sizeof(bca));
4105 if (asprintf(&path, "%s%s%s",
4106 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4107 gw_trans->repo_folder ? "/" : "",
4108 gw_trans->repo_file) == -1) {
4109 error = got_error_from_errno("asprintf");
4110 goto done;
4113 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path);
4114 if (error)
4115 goto done;
4117 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
4118 GOT_OBJ_TYPE_COMMIT, &header->refs, gw_trans->repo);
4119 if (error)
4120 goto done;
4122 error = got_object_open_as_commit(&commit, gw_trans->repo, commit_id);
4123 if (error)
4124 goto done;
4126 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit,
4127 in_repo_path);
4128 if (error)
4129 goto done;
4131 if (obj_id == NULL) {
4132 error = got_error(GOT_ERR_NO_OBJ);
4133 goto done;
4136 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
4137 if (error)
4138 goto done;
4140 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4141 error = got_error(GOT_ERR_OBJ_TYPE);
4142 goto done;
4145 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192,
4146 fd1);
4147 if (error)
4148 goto done;
4150 bca.f = got_opentemp();
4151 if (bca.f == NULL) {
4152 error = got_error_from_errno("got_opentemp");
4153 goto done;
4155 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4156 &bca.line_offsets, bca.f, blob);
4157 if (error || bca.nlines == 0)
4158 goto done;
4160 /* Don't include \n at EOF in the blame line count. */
4161 if (bca.line_offsets[bca.nlines - 1] == filesize)
4162 bca.nlines--;
4164 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4165 if (bca.lines == NULL) {
4166 error = got_error_from_errno("calloc");
4167 goto done;
4169 bca.lineno_cur = 1;
4170 bca.nlines_prec = 0;
4171 i = bca.nlines;
4172 while (i > 0) {
4173 i /= 10;
4174 bca.nlines_prec++;
4176 bca.repo = gw_trans->repo;
4177 bca.gw_trans = gw_trans;
4179 fd1 = got_opentempfd();
4180 if (fd1 == -1) {
4181 error = got_error_from_errno("got_opentempfd");
4182 goto done;
4185 f1 = got_opentemp();
4186 if (f1 == NULL) {
4187 error = got_error_from_errno("got_opentempfd");
4188 goto done;
4190 f2 = got_opentemp();
4191 if (f2 == NULL) {
4192 error = got_error_from_errno("got_opentempfd");
4193 goto done;
4196 error = got_blame(in_repo_path, commit_id, gw_trans->repo, gw_blame_cb,
4197 &bca, NULL, NULL, fd2, fd3, f1, f2);
4198 done:
4199 free(in_repo_path);
4200 free(commit_id);
4201 free(obj_id);
4202 free(path);
4204 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
4205 error = got_error_from_errno("close");
4206 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
4207 error = got_error_from_errno("close");
4208 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
4209 error = got_error_from_errno("close");
4210 if (f1 && fclose(f1) == EOF && error == NULL)
4211 error = got_error_from_errno("fclose");
4212 if (f2 && fclose(f2) == EOF && error == NULL)
4213 error = got_error_from_errno("fclose");
4215 if (blob) {
4216 free(bca.line_offsets);
4217 for (i = 0; i < bca.nlines; i++) {
4218 struct blame_line *bline = &bca.lines[i];
4219 free(bline->id_str);
4220 free(bline->committer);
4222 free(bca.lines);
4223 if (bca.f && fclose(bca.f) == EOF && error == NULL)
4224 error = got_error_from_errno("fclose");
4226 if (blob)
4227 got_object_blob_close(blob);
4228 if (commit)
4229 got_object_commit_close(commit);
4230 return error;
4233 static const struct got_error *
4234 gw_output_blob_buf(struct gw_trans *gw_trans, struct gw_header *header)
4236 const struct got_error *error = NULL;
4237 struct got_object_id *obj_id = NULL;
4238 struct got_object_id *commit_id = NULL;
4239 struct got_commit_object *commit = NULL;
4240 struct got_blob_object *blob = NULL;
4241 char *path = NULL, *in_repo_path = NULL;
4242 int obj_type, set_mime = 0, fd = -1;
4243 size_t len, hdrlen;
4244 const uint8_t *buf;
4245 enum kcgi_err kerr = KCGI_OK;
4247 fd = got_opentempfd();
4248 if (fd == -1)
4249 return got_error_from_errno("got_opentempfd");
4251 if (asprintf(&path, "%s%s%s",
4252 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4253 gw_trans->repo_folder ? "/" : "",
4254 gw_trans->repo_file) == -1) {
4255 error = got_error_from_errno("asprintf");
4256 goto done;
4259 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path);
4260 if (error)
4261 goto done;
4263 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
4264 GOT_OBJ_TYPE_COMMIT, &header->refs, gw_trans->repo);
4265 if (error)
4266 goto done;
4268 error = got_object_open_as_commit(&commit, gw_trans->repo, commit_id);
4269 if (error)
4270 goto done;
4272 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit,
4273 in_repo_path);
4274 if (error)
4275 goto done;
4277 if (obj_id == NULL) {
4278 error = got_error(GOT_ERR_NO_OBJ);
4279 goto done;
4282 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
4283 if (error)
4284 goto done;
4286 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4287 error = got_error(GOT_ERR_OBJ_TYPE);
4288 goto done;
4291 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192,
4292 fd);
4293 if (error)
4294 goto done;
4296 hdrlen = got_object_blob_get_hdrlen(blob);
4297 do {
4298 error = got_object_blob_read_block(&len, blob);
4299 if (error)
4300 goto done;
4301 buf = got_object_blob_get_read_buf(blob);
4304 * Skip blob object header first time around,
4305 * which also contains a zero byte.
4307 buf += hdrlen;
4308 if (set_mime == 0) {
4309 if (isbinary(buf, len - hdrlen))
4310 gw_trans->mime = KMIME_APP_OCTET_STREAM;
4311 else
4312 gw_trans->mime = KMIME_TEXT_PLAIN;
4313 set_mime = 1;
4314 error = gw_display_index(gw_trans);
4315 if (error)
4316 goto done;
4318 kerr = khttp_write(gw_trans->gw_req, buf, len - hdrlen);
4319 if (kerr != KCGI_OK)
4320 goto done;
4321 hdrlen = 0;
4322 } while (len != 0);
4323 done:
4324 free(in_repo_path);
4325 free(commit_id);
4326 free(obj_id);
4327 free(path);
4328 if (fd != -1 && close(fd) == -1 && error == NULL)
4329 error = got_error_from_errno("close");
4330 if (blob)
4331 got_object_blob_close(blob);
4332 if (commit)
4333 got_object_commit_close(commit);
4334 if (error == NULL && kerr != KCGI_OK)
4335 error = gw_kcgi_error(kerr);
4336 return error;
4339 static const struct got_error *
4340 gw_output_repo_tree(struct gw_trans *gw_trans, struct gw_header *header)
4342 const struct got_error *error = NULL;
4343 struct got_object_id *tree_id = NULL, *commit_id = NULL;
4344 struct got_tree_object *tree = NULL;
4345 struct got_commit_object *commit = NULL;
4346 char *path = NULL, *in_repo_path = NULL;
4347 char *id_str = NULL;
4348 char *build_folder = NULL;
4349 char *href_blob = NULL, *href_blame = NULL;
4350 const char *class = NULL;
4351 int nentries, i, class_flip = 0;
4352 enum kcgi_err kerr = KCGI_OK;
4354 if (gw_trans->repo_folder != NULL) {
4355 path = strdup(gw_trans->repo_folder);
4356 if (path == NULL) {
4357 error = got_error_from_errno("strdup");
4358 goto done;
4360 } else {
4361 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
4362 gw_trans->repo_path);
4363 if (error)
4364 goto done;
4365 free(path);
4366 path = in_repo_path;
4369 if (gw_trans->commit_id == NULL) {
4370 struct got_reference *head_ref;
4371 error = got_ref_open(&head_ref, gw_trans->repo,
4372 gw_trans->headref, 0);
4373 if (error)
4374 goto done;
4375 error = got_ref_resolve(&commit_id, gw_trans->repo, head_ref);
4376 if (error)
4377 goto done;
4378 got_ref_close(head_ref);
4380 * gw_trans->commit_id was not parsed from the querystring
4381 * we hit this code path from gw_index, where we don't know the
4382 * commit values for the tree link yet, so set
4383 * gw_trans->commit_id here to continue further into the tree
4385 error = got_object_id_str(&gw_trans->commit_id, commit_id);
4386 if (error)
4387 goto done;
4389 } else {
4390 error = got_repo_match_object_id(&commit_id, NULL,
4391 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT, &header->refs,
4392 gw_trans->repo);
4393 if (error)
4394 goto done;
4397 error = got_object_open_as_commit(&commit, gw_trans->repo, commit_id);
4398 if (error)
4399 goto done;
4401 error = got_object_id_by_path(&tree_id, gw_trans->repo, commit,
4402 path);
4403 if (error)
4404 goto done;
4406 error = got_object_open_as_tree(&tree, gw_trans->repo, tree_id);
4407 if (error)
4408 goto done;
4410 nentries = got_object_tree_get_nentries(tree);
4411 for (i = 0; i < nentries; i++) {
4412 struct got_tree_entry *te;
4413 const char *modestr = "";
4414 mode_t mode;
4416 te = got_object_tree_get_entry(tree, i);
4418 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
4419 if (error)
4420 goto done;
4422 mode = got_tree_entry_get_mode(te);
4423 if (got_object_tree_entry_is_submodule(te))
4424 modestr = "$";
4425 else if (S_ISLNK(mode))
4426 modestr = "@";
4427 else if (S_ISDIR(mode))
4428 modestr = "/";
4429 else if (mode & S_IXUSR)
4430 modestr = "*";
4432 if (class_flip == 0) {
4433 class = "back_lightgray";
4434 class_flip = 1;
4435 } else {
4436 class = "back_white";
4437 class_flip = 0;
4440 if (S_ISDIR(mode)) {
4441 if (asprintf(&build_folder, "%s/%s",
4442 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4443 got_tree_entry_get_name(te)) == -1) {
4444 error = got_error_from_errno("asprintf");
4445 goto done;
4448 href_blob = khttp_urlpart(NULL, NULL, "gotweb", "path",
4449 gw_trans->repo_name, "action",
4450 gw_get_action_name(gw_trans), "commit",
4451 gw_trans->commit_id, "folder", build_folder, NULL);
4452 if (href_blob == NULL) {
4453 error = got_error_from_errno("khttp_urlpart");
4454 goto done;
4456 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4457 KATTR_ID, "tree_wrapper", KATTR__MAX);
4458 if (kerr != KCGI_OK)
4459 goto done;
4460 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4461 KATTR_ID, "tree_line", KATTR_CLASS, class,
4462 KATTR__MAX);
4463 if (kerr != KCGI_OK)
4464 goto done;
4465 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4466 KATTR_HREF, href_blob, KATTR_CLASS,
4467 "diff_directory", KATTR__MAX);
4468 if (kerr != KCGI_OK)
4469 goto done;
4470 kerr = khtml_printf(gw_trans->gw_html_req, "%s%s",
4471 got_tree_entry_get_name(te), modestr);
4472 if (kerr != KCGI_OK)
4473 goto done;
4474 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4475 if (kerr != KCGI_OK)
4476 goto done;
4477 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4478 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
4479 KATTR__MAX);
4480 if (kerr != KCGI_OK)
4481 goto done;
4482 kerr = khtml_entity(gw_trans->gw_html_req,
4483 KENTITY_nbsp);
4484 if (kerr != KCGI_OK)
4485 goto done;
4486 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4487 if (kerr != KCGI_OK)
4488 goto done;
4489 } else {
4490 href_blob = khttp_urlpart(NULL, NULL, "gotweb", "path",
4491 gw_trans->repo_name, "action", "blob", "commit",
4492 gw_trans->commit_id, "file",
4493 got_tree_entry_get_name(te), "folder",
4494 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4495 NULL);
4496 if (href_blob == NULL) {
4497 error = got_error_from_errno("khttp_urlpart");
4498 goto done;
4500 href_blame = khttp_urlpart(NULL, NULL, "gotweb", "path",
4501 gw_trans->repo_name, "action", "blame", "commit",
4502 gw_trans->commit_id, "file",
4503 got_tree_entry_get_name(te), "folder",
4504 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4505 NULL);
4506 if (href_blame == NULL) {
4507 error = got_error_from_errno("khttp_urlpart");
4508 goto done;
4510 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4511 KATTR_ID, "tree_wrapper", KATTR__MAX);
4512 if (kerr != KCGI_OK)
4513 goto done;
4514 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4515 KATTR_ID, "tree_line", KATTR_CLASS, class,
4516 KATTR__MAX);
4517 if (kerr != KCGI_OK)
4518 goto done;
4519 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4520 KATTR_HREF, href_blob, KATTR__MAX);
4521 if (kerr != KCGI_OK)
4522 goto done;
4523 kerr = khtml_printf(gw_trans->gw_html_req, "%s%s",
4524 got_tree_entry_get_name(te), modestr);
4525 if (kerr != KCGI_OK)
4526 goto done;
4527 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4528 if (kerr != KCGI_OK)
4529 goto done;
4530 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4531 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
4532 KATTR__MAX);
4533 if (kerr != KCGI_OK)
4534 goto done;
4536 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4537 KATTR_HREF, href_blob, KATTR__MAX);
4538 if (kerr != KCGI_OK)
4539 goto done;
4540 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
4541 if (kerr != KCGI_OK)
4542 goto done;
4543 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4544 if (kerr != KCGI_OK)
4545 goto done;
4547 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4548 if (kerr != KCGI_OK)
4549 goto done;
4551 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4552 KATTR_HREF, href_blame, KATTR__MAX);
4553 if (kerr != KCGI_OK)
4554 goto done;
4555 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
4556 if (kerr != KCGI_OK)
4557 goto done;
4559 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4560 if (kerr != KCGI_OK)
4561 goto done;
4563 free(id_str);
4564 id_str = NULL;
4565 free(href_blob);
4566 href_blob = NULL;
4567 free(build_folder);
4568 build_folder = NULL;
4570 done:
4571 if (tree)
4572 got_object_tree_close(tree);
4573 if (commit)
4574 got_object_commit_close(commit);
4575 free(id_str);
4576 free(href_blob);
4577 free(href_blame);
4578 free(in_repo_path);
4579 free(tree_id);
4580 free(build_folder);
4581 if (error == NULL && kerr != KCGI_OK)
4582 error = gw_kcgi_error(kerr);
4583 return error;
4586 static const struct got_error *
4587 gw_output_repo_heads(struct gw_trans *gw_trans)
4589 const struct got_error *error = NULL;
4590 struct got_reflist_head refs;
4591 struct got_reflist_entry *re;
4592 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
4593 char *href_commits = NULL;
4594 enum kcgi_err kerr = KCGI_OK;
4596 TAILQ_INIT(&refs);
4598 error = got_ref_list(&refs, gw_trans->repo, "refs/heads",
4599 got_ref_cmp_by_name, NULL);
4600 if (error)
4601 goto done;
4603 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4604 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
4605 if (kerr != KCGI_OK)
4606 goto done;
4607 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4608 KATTR_ID, "summary_heads_title", KATTR__MAX);
4609 if (kerr != KCGI_OK)
4610 goto done;
4611 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
4612 if (kerr != KCGI_OK)
4613 goto done;
4614 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4615 if (kerr != KCGI_OK)
4616 goto done;
4617 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4618 KATTR_ID, "summary_heads_content", KATTR__MAX);
4619 if (kerr != KCGI_OK)
4620 goto done;
4622 TAILQ_FOREACH(re, &refs, entry) {
4623 const char *refname;
4625 if (got_ref_is_symbolic(re->ref))
4626 continue;
4628 refname = got_ref_get_name(re->ref);
4629 if (strncmp(refname, "refs/heads/", 11) != 0)
4630 continue;
4632 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
4633 refname, TM_DIFF);
4634 if (error)
4635 goto done;
4637 if (strncmp(refname, "refs/heads/", 11) == 0)
4638 refname += 11;
4640 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4641 KATTR_ID, "heads_wrapper", KATTR__MAX);
4642 if (kerr != KCGI_OK)
4643 goto done;
4644 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4645 KATTR_ID, "heads_age", KATTR__MAX);
4646 if (kerr != KCGI_OK)
4647 goto done;
4648 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
4649 if (kerr != KCGI_OK)
4650 goto done;
4651 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4652 if (kerr != KCGI_OK)
4653 goto done;
4654 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4655 KATTR_ID, "heads_space", KATTR__MAX);
4656 if (kerr != KCGI_OK)
4657 goto done;
4658 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
4659 if (kerr != KCGI_OK)
4660 goto done;
4661 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4662 if (kerr != KCGI_OK)
4663 goto done;
4664 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4665 KATTR_ID, "head", KATTR__MAX);
4666 if (kerr != KCGI_OK)
4667 goto done;
4669 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
4670 gw_trans->repo_name, "action", "summary", "headref",
4671 refname, NULL);
4672 if (href_summary == NULL) {
4673 error = got_error_from_errno("khttp_urlpart");
4674 goto done;
4676 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4677 href_summary, KATTR__MAX);
4678 kerr = khtml_puts(gw_trans->gw_html_req, refname);
4679 if (kerr != KCGI_OK)
4680 goto done;
4681 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4682 if (kerr != KCGI_OK)
4683 goto done;
4685 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4686 "navs_wrapper", KATTR__MAX);
4687 if (kerr != KCGI_OK)
4688 goto done;
4689 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4690 "navs", KATTR__MAX);
4691 if (kerr != KCGI_OK)
4692 goto done;
4694 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4695 href_summary, KATTR__MAX);
4696 if (kerr != KCGI_OK)
4697 goto done;
4698 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4699 if (kerr != KCGI_OK)
4700 goto done;
4701 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4702 if (kerr != KCGI_OK)
4703 goto done;
4705 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4706 if (kerr != KCGI_OK)
4707 goto done;
4709 href_briefs = khttp_urlpart(NULL, NULL, "gotweb", "path",
4710 gw_trans->repo_name, "action", "briefs", "headref",
4711 refname, NULL);
4712 if (href_briefs == NULL) {
4713 error = got_error_from_errno("khttp_urlpart");
4714 goto done;
4716 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4717 href_briefs, KATTR__MAX);
4718 if (kerr != KCGI_OK)
4719 goto done;
4720 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4721 if (kerr != KCGI_OK)
4722 goto done;
4723 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4724 if (kerr != KCGI_OK)
4725 goto done;
4727 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4728 if (kerr != KCGI_OK)
4729 goto done;
4731 href_commits = khttp_urlpart(NULL, NULL, "gotweb", "path",
4732 gw_trans->repo_name, "action", "commits", "headref",
4733 refname, NULL);
4734 if (href_commits == NULL) {
4735 error = got_error_from_errno("khttp_urlpart");
4736 goto done;
4738 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4739 href_commits, KATTR__MAX);
4740 if (kerr != KCGI_OK)
4741 goto done;
4742 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4743 if (kerr != KCGI_OK)
4744 goto done;
4745 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4746 if (kerr != KCGI_OK)
4747 goto done;
4749 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4750 "dotted_line", KATTR__MAX);
4751 if (kerr != KCGI_OK)
4752 goto done;
4753 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4754 if (kerr != KCGI_OK)
4755 goto done;
4756 free(href_summary);
4757 href_summary = NULL;
4758 free(href_briefs);
4759 href_briefs = NULL;
4760 free(href_commits);
4761 href_commits = NULL;
4763 done:
4764 got_ref_list_free(&refs);
4765 free(href_summary);
4766 free(href_briefs);
4767 free(href_commits);
4768 return error;
4771 static const struct got_error *
4772 gw_output_site_link(struct gw_trans *gw_trans)
4774 const struct got_error *error = NULL;
4775 char *href_summary = NULL;
4776 enum kcgi_err kerr = KCGI_OK;
4778 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4779 "site_link", KATTR__MAX);
4780 if (kerr != KCGI_OK)
4781 goto done;
4782 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4783 KATTR__MAX);
4784 if (kerr != KCGI_OK)
4785 goto done;
4786 kerr = khtml_puts(gw_trans->gw_html_req,
4787 gw_trans->gw_conf->got_site_link);
4788 if (kerr != KCGI_OK)
4789 goto done;
4790 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4791 if (kerr != KCGI_OK)
4792 goto done;
4794 if (gw_trans->repo_name != NULL) {
4795 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4796 if (kerr != KCGI_OK)
4797 goto done;
4799 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
4800 gw_trans->repo_name, "action", "summary", NULL);
4801 if (href_summary == NULL) {
4802 error = got_error_from_errno("khttp_urlpart");
4803 goto done;
4805 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4806 href_summary, KATTR__MAX);
4807 if (kerr != KCGI_OK)
4808 goto done;
4809 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4810 if (kerr != KCGI_OK)
4811 goto done;
4812 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4813 if (kerr != KCGI_OK)
4814 goto done;
4815 kerr = khtml_printf(gw_trans->gw_html_req, " / %s",
4816 gw_get_action_name(gw_trans));
4817 if (kerr != KCGI_OK)
4818 goto done;
4821 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4822 if (kerr != KCGI_OK)
4823 goto done;
4824 done:
4825 free(href_summary);
4826 if (error == NULL && kerr != KCGI_OK)
4827 error = gw_kcgi_error(kerr);
4828 return error;
4831 static const struct got_error *
4832 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4834 const struct got_error *error = NULL;
4835 const char *color = NULL;
4836 enum kcgi_err kerr = KCGI_OK;
4838 if (strncmp(buf, "-", 1) == 0)
4839 color = "diff_minus";
4840 else if (strncmp(buf, "+", 1) == 0)
4841 color = "diff_plus";
4842 else if (strncmp(buf, "@@", 2) == 0)
4843 color = "diff_chunk_header";
4844 else if (strncmp(buf, "@@", 2) == 0)
4845 color = "diff_chunk_header";
4846 else if (strncmp(buf, "commit +", 8) == 0)
4847 color = "diff_meta";
4848 else if (strncmp(buf, "commit -", 8) == 0)
4849 color = "diff_meta";
4850 else if (strncmp(buf, "blob +", 6) == 0)
4851 color = "diff_meta";
4852 else if (strncmp(buf, "blob -", 6) == 0)
4853 color = "diff_meta";
4854 else if (strncmp(buf, "file +", 6) == 0)
4855 color = "diff_meta";
4856 else if (strncmp(buf, "file -", 6) == 0)
4857 color = "diff_meta";
4858 else if (strncmp(buf, "from:", 5) == 0)
4859 color = "diff_author";
4860 else if (strncmp(buf, "via:", 4) == 0)
4861 color = "diff_author";
4862 else if (strncmp(buf, "date:", 5) == 0)
4863 color = "diff_date";
4864 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4865 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4866 if (error == NULL && kerr != KCGI_OK)
4867 error = gw_kcgi_error(kerr);
4868 return error;
4871 int
4872 main(int argc, char *argv[])
4874 const struct got_error *error = NULL, *error2 = NULL;
4875 struct gw_trans *gw_trans;
4876 struct gw_dir *dir = NULL, *tdir;
4877 const char *page = "index";
4878 enum kcgi_err kerr = KCGI_OK;
4880 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4881 errx(1, "malloc");
4883 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4884 errx(1, "malloc");
4886 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4887 errx(1, "malloc");
4889 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4890 errx(1, "malloc");
4892 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4893 if (kerr != KCGI_OK) {
4894 error = gw_kcgi_error(kerr);
4895 goto done;
4898 TAILQ_INIT(&gw_trans->gw_dirs);
4899 TAILQ_INIT(&gw_trans->gw_headers);
4901 gw_trans->action = -1;
4902 gw_trans->page = 0;
4903 gw_trans->repos_total = 0;
4904 gw_trans->repo_path = NULL;
4905 gw_trans->commit_id = NULL;
4906 gw_trans->next_id = NULL;
4907 gw_trans->prev_id = NULL;
4908 gw_trans->headref = GOT_REF_HEAD;
4909 gw_trans->mime = KMIME_TEXT_HTML;
4910 gw_trans->gw_tmpl->key = gw_templs;
4911 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4912 gw_trans->gw_tmpl->arg = gw_trans;
4913 gw_trans->gw_tmpl->cb = gw_template;
4915 error = got_repo_pack_fds_open(&gw_trans->pack_fds);
4916 if (error != NULL)
4917 goto done;
4919 error = parse_gotweb_config(&gw_trans->gw_conf, GOTWEB_CONF);
4920 if (error)
4921 goto done;
4923 error = gw_parse_querystring(gw_trans);
4924 if (error)
4925 goto done;
4927 if (gw_trans->repo) {
4928 const struct got_error *close_err;
4929 close_err = got_repo_close(gw_trans->repo);
4930 if (error == NULL)
4931 error = close_err;
4933 if (gw_trans->action == GW_BLOB)
4934 error = gw_blob(gw_trans);
4935 else
4936 error = gw_display_index(gw_trans);
4937 done:
4938 if (error) {
4939 gw_trans->error = error;
4940 gw_trans->action = GW_ERR;
4941 error2 = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
4942 if (error2)
4943 goto cleanup; /* we can't display an error page */
4944 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
4945 if (kerr != KCGI_OK)
4946 goto cleanup; /* we can't display an error page */
4947 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
4948 gw_query_funcs[gw_trans->action].template);
4949 if (kerr != KCGI_OK) {
4950 khtml_close(gw_trans->gw_html_req);
4951 goto cleanup; /* we can't display an error page */
4955 cleanup:
4956 if (gw_trans->pack_fds) {
4957 const struct got_error *pack_err =
4958 got_repo_pack_fds_close(gw_trans->pack_fds);
4959 if (error == NULL)
4960 error = pack_err;
4961 gw_trans->pack_fds = NULL;
4963 free(gw_trans->gw_conf->got_repos_path);
4964 free(gw_trans->gw_conf->got_www_path);
4965 free(gw_trans->gw_conf->got_site_name);
4966 free(gw_trans->gw_conf->got_site_owner);
4967 free(gw_trans->gw_conf->got_site_link);
4968 free(gw_trans->gw_conf->got_logo);
4969 free(gw_trans->gw_conf->got_logo_url);
4970 free(gw_trans->gw_conf);
4971 free(gw_trans->commit_id);
4972 free(gw_trans->next_id);
4973 free(gw_trans->prev_id);
4974 free(gw_trans->repo_path);
4975 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4976 free(dir->name);
4977 free(dir->description);
4978 free(dir->age);
4979 free(dir->url);
4980 free(dir->path);
4981 free(dir);
4984 khttp_free(gw_trans->gw_req);
4985 return 0;