Blob


1 /*
2 * Copyright (c) 2019, 2020 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/queue.h>
19 #include <sys/stat.h>
20 #include <sys/types.h>
22 #include <ctype.h>
23 #include <dirent.h>
24 #include <err.h>
25 #include <errno.h>
26 #include <regex.h>
27 #include <stdarg.h>
28 #include <stdint.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
34 #include <got_object.h>
35 #include <got_reference.h>
36 #include <got_repository.h>
37 #include <got_path.h>
38 #include <got_cancel.h>
39 #include <got_worktree.h>
40 #include <got_diff.h>
41 #include <got_commit_graph.h>
42 #include <got_blame.h>
43 #include <got_privsep.h>
44 #include <got_opentemp.h>
46 #include <kcgi.h>
47 #include <kcgihtml.h>
49 #include "gotweb.h"
51 #ifndef nitems
52 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
53 #endif
55 struct gw_trans {
56 TAILQ_HEAD(headers, gw_header) gw_headers;
57 TAILQ_HEAD(dirs, gw_dir) gw_dirs;
58 struct gw_dir *gw_dir;
59 struct gotweb_conf *gw_conf;
60 struct ktemplate *gw_tmpl;
61 struct khtmlreq *gw_html_req;
62 struct kreq *gw_req;
63 char *repo_name;
64 char *repo_path;
65 char *commit;
66 char *repo_file;
67 char *repo_folder;
68 char *action_name;
69 char *headref;
70 unsigned int action;
71 unsigned int page;
72 unsigned int repos_total;
73 enum kmime mime;
74 };
76 struct gw_header {
77 TAILQ_ENTRY(gw_header) entry;
78 struct got_repository *repo;
79 struct got_reflist_head refs;
80 struct got_commit_object *commit;
81 struct got_object_id *id;
82 char *path;
84 char *refs_str;
85 char *commit_id; /* id_str1 */
86 char *parent_id; /* id_str2 */
87 char *tree_id;
88 const char *author;
89 const char *committer;
90 char *commit_msg;
91 time_t committer_time;
92 };
94 struct gw_dir {
95 TAILQ_ENTRY(gw_dir) entry;
96 char *name;
97 char *owner;
98 char *description;
99 char *url;
100 char *age;
101 char *path;
102 };
104 enum gw_key {
105 KEY_ACTION,
106 KEY_COMMIT_ID,
107 KEY_FILE,
108 KEY_FOLDER,
109 KEY_HEADREF,
110 KEY_PAGE,
111 KEY_PATH,
112 KEY__ZMAX
113 };
115 enum gw_tmpl {
116 TEMPL_CONTENT,
117 TEMPL_HEAD,
118 TEMPL_HEADER,
119 TEMPL_SEARCH,
120 TEMPL_SITEPATH,
121 TEMPL_SITEOWNER,
122 TEMPL_TITLE,
123 TEMPL__MAX
124 };
126 enum gw_ref_tm {
127 TM_DIFF,
128 TM_LONG,
129 };
131 enum gw_tags {
132 TAGBRIEF,
133 TAGFULL,
134 };
136 static const char *const gw_templs[TEMPL__MAX] = {
137 "content",
138 "head",
139 "header",
140 "search",
141 "sitepath",
142 "siteowner",
143 "title",
144 };
146 static const struct kvalid gw_keys[KEY__ZMAX] = {
147 { kvalid_stringne, "action" },
148 { kvalid_stringne, "commit" },
149 { kvalid_stringne, "file" },
150 { kvalid_stringne, "folder" },
151 { kvalid_stringne, "headref" },
152 { kvalid_int, "page" },
153 { kvalid_stringne, "path" },
154 };
156 static struct gw_dir *gw_init_gw_dir(char *);
157 static struct gw_header *gw_init_header(void);
159 static const struct got_error *gw_get_repo_description(char **,
160 struct gw_trans *, char *);
161 static const struct got_error *gw_get_repo_owner(char **, struct gw_trans *,
162 char *);
163 static const struct got_error *gw_get_time_str(char **, time_t, int);
164 static const struct got_error *gw_get_repo_age(char **, struct gw_trans *,
165 char *, char *, int);
166 static const struct got_error *gw_output_file_blame(struct gw_trans *);
167 static const struct got_error *gw_output_blob_buf(struct gw_trans *);
168 static const struct got_error *gw_output_repo_tree(struct gw_trans *);
169 static const struct got_error *gw_output_diff(struct gw_trans *,
170 struct gw_header *);
171 static const struct got_error *gw_output_repo_tags(struct gw_trans *,
172 struct gw_header *, int, int);
173 static const struct got_error *gw_output_repo_heads(struct gw_trans *);
174 static const struct got_error *gw_output_site_link(struct gw_trans *);
175 static const struct got_error *gw_get_clone_url(char **, struct gw_trans *,
176 char *);
177 static const struct got_error *gw_colordiff_line(struct gw_trans *, char *);
179 static const struct got_error *gw_gen_commit_header(struct gw_trans *, char *,
180 char*);
181 static const struct got_error *gw_gen_diff_header(struct gw_trans *, char *,
182 char*);
183 static const struct got_error *gw_gen_author_header(struct gw_trans *,
184 const char *);
185 static const struct got_error *gw_gen_age_header(struct gw_trans *,
186 const char *);
187 static const struct got_error *gw_gen_committer_header(struct gw_trans *,
188 const char *);
189 static const struct got_error *gw_gen_commit_msg_header(struct gw_trans*,
190 char *);
191 static const struct got_error *gw_gen_tree_header(struct gw_trans *, char *);
193 static void gw_free_headers(struct gw_header *);
194 static const struct got_error* gw_display_open(struct gw_trans *, enum khttp,
195 enum kmime);
196 static const struct got_error* gw_display_index(struct gw_trans *);
197 static void gw_display_error(struct gw_trans *,
198 const struct got_error *);
200 static int gw_template(size_t, void *);
202 static const struct got_error* gw_get_header(struct gw_trans *,
203 struct gw_header *, int);
204 static const struct got_error* gw_get_commits(struct gw_trans *,
205 struct gw_header *, int);
206 static const struct got_error* gw_get_commit(struct gw_trans *,
207 struct gw_header *);
208 static const struct got_error* gw_apply_unveil(const char *, const char *);
209 static const struct got_error* gw_blame_cb(void *, int, int,
210 struct got_object_id *);
211 static const struct got_error* gw_load_got_paths(struct gw_trans *);
212 static const struct got_error* gw_load_got_path(struct gw_trans *,
213 struct gw_dir *);
214 static const struct got_error* gw_parse_querystring(struct gw_trans *);
216 static const struct got_error* gw_blame(struct gw_trans *);
217 static const struct got_error* gw_blob(struct gw_trans *);
218 static const struct got_error* gw_diff(struct gw_trans *);
219 static const struct got_error* gw_index(struct gw_trans *);
220 static const struct got_error* gw_commits(struct gw_trans *);
221 static const struct got_error* gw_briefs(struct gw_trans *);
222 static const struct got_error* gw_summary(struct gw_trans *);
223 static const struct got_error* gw_tree(struct gw_trans *);
224 static const struct got_error* gw_tag(struct gw_trans *);
226 struct gw_query_action {
227 unsigned int func_id;
228 const char *func_name;
229 const struct got_error *(*func_main)(struct gw_trans *);
230 char *template;
231 };
233 enum gw_query_actions {
234 GW_BLAME,
235 GW_BLOB,
236 GW_BRIEFS,
237 GW_COMMITS,
238 GW_DIFF,
239 GW_ERR,
240 GW_INDEX,
241 GW_SUMMARY,
242 GW_TAG,
243 GW_TREE,
244 };
246 static struct gw_query_action gw_query_funcs[] = {
247 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
248 { GW_BLOB, "blob", NULL, NULL },
249 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
250 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
251 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
252 { GW_ERR, NULL, NULL, "gw_tmpl/err.tmpl" },
253 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
254 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
255 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
256 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
257 };
259 static const struct got_error *
260 gw_kcgi_error(enum kcgi_err kerr)
262 if (kerr == KCGI_OK)
263 return NULL;
265 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
266 return got_error(GOT_ERR_CANCELLED);
268 if (kerr == KCGI_ENOMEM)
269 return got_error_set_errno(ENOMEM,
270 kcgi_strerror(kerr));
272 if (kerr == KCGI_ENFILE)
273 return got_error_set_errno(ENFILE,
274 kcgi_strerror(kerr));
276 if (kerr == KCGI_EAGAIN)
277 return got_error_set_errno(EAGAIN,
278 kcgi_strerror(kerr));
280 if (kerr == KCGI_FORM)
281 return got_error_msg(GOT_ERR_IO,
282 kcgi_strerror(kerr));
284 return got_error_from_errno(kcgi_strerror(kerr));
287 static const struct got_error *
288 gw_apply_unveil(const char *repo_path, const char *repo_file)
290 const struct got_error *err;
292 if (repo_path && repo_file) {
293 char *full_path;
294 if (asprintf(&full_path, "%s/%s", repo_path, repo_file) == -1)
295 return got_error_from_errno("asprintf unveil");
296 if (unveil(full_path, "r") != 0)
297 return got_error_from_errno2("unveil", full_path);
300 if (repo_path && unveil(repo_path, "r") != 0)
301 return got_error_from_errno2("unveil", repo_path);
303 if (unveil("/tmp", "rwc") != 0)
304 return got_error_from_errno2("unveil", "/tmp");
306 err = got_privsep_unveil_exec_helpers();
307 if (err != NULL)
308 return err;
310 if (unveil(NULL, NULL) != 0)
311 return got_error_from_errno("unveil");
313 return NULL;
316 static const struct got_error *
317 gw_empty_string(char **s)
319 *s = strdup("");
320 if (*s == NULL)
321 return got_error_from_errno("strdup");
322 return NULL;
325 static int
326 isbinary(const uint8_t *buf, size_t n)
328 size_t i;
330 for (i = 0; i < n; i++)
331 if (buf[i] == 0)
332 return 1;
333 return 0;
336 static const struct got_error *
337 gw_blame(struct gw_trans *gw_trans)
339 const struct got_error *error = NULL;
340 struct gw_header *header = NULL;
341 char *age = NULL;
342 enum kcgi_err kerr;
344 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
345 NULL) == -1)
346 return got_error_from_errno("pledge");
348 if ((header = gw_init_header()) == NULL)
349 return got_error_from_errno("malloc");
351 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
352 if (error)
353 goto done;
355 error = gw_get_header(gw_trans, header, 1);
356 if (error)
357 goto done;
359 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
360 "blame_header_wrapper", KATTR__MAX);
361 if (kerr != KCGI_OK)
362 goto done;
363 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
364 "blame_header", KATTR__MAX);
365 if (kerr != KCGI_OK)
366 goto done;
367 error = gw_get_time_str(&age, header->committer_time,
368 TM_LONG);
369 if (error)
370 goto done;
371 error = gw_gen_age_header(gw_trans, age ?age : "");
372 if (error)
373 goto done;
374 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
375 if (error)
376 goto done;
377 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
378 if (kerr != KCGI_OK)
379 goto done;
380 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
381 "dotted_line", KATTR__MAX);
382 if (kerr != KCGI_OK)
383 goto done;
384 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
385 if (kerr != KCGI_OK)
386 goto done;
388 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
389 "blame", KATTR__MAX);
390 if (kerr != KCGI_OK)
391 goto done;
392 error = gw_output_file_blame(gw_trans);
393 if (error)
394 goto done;
395 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
396 done:
397 got_ref_list_free(&header->refs);
398 gw_free_headers(header);
399 if (error == NULL && kerr != KCGI_OK)
400 error = gw_kcgi_error(kerr);
401 return error;
404 static const struct got_error *
405 gw_blob(struct gw_trans *gw_trans)
407 const struct got_error *error = NULL;
408 struct gw_header *header = NULL;
410 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
411 NULL) == -1)
412 return got_error_from_errno("pledge");
414 if ((header = gw_init_header()) == NULL)
415 return got_error_from_errno("malloc");
417 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
418 if (error)
419 goto done;
421 error = gw_get_header(gw_trans, header, 1);
422 if (error)
423 goto done;
425 error = gw_output_blob_buf(gw_trans);
426 done:
427 got_ref_list_free(&header->refs);
428 gw_free_headers(header);
429 return error;
432 static const struct got_error *
433 gw_diff(struct gw_trans *gw_trans)
435 const struct got_error *error = NULL;
436 struct gw_header *header = NULL;
437 char *age = NULL;
438 enum kcgi_err kerr = KCGI_OK;
440 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
441 NULL) == -1)
442 return got_error_from_errno("pledge");
444 if ((header = gw_init_header()) == NULL)
445 return got_error_from_errno("malloc");
447 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
448 if (error)
449 goto done;
451 error = gw_get_header(gw_trans, header, 1);
452 if (error)
453 goto done;
455 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
456 "diff_header_wrapper", KATTR__MAX);
457 if (kerr != KCGI_OK)
458 goto done;
459 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
460 "diff_header", KATTR__MAX);
461 if (kerr != KCGI_OK)
462 goto done;
463 error = gw_gen_diff_header(gw_trans, header->parent_id,
464 header->commit_id);
465 if (error)
466 goto done;
467 error = gw_gen_commit_header(gw_trans, header->commit_id,
468 header->refs_str);
469 if (error)
470 goto done;
471 error = gw_gen_tree_header(gw_trans, header->tree_id);
472 if (error)
473 goto done;
474 error = gw_gen_author_header(gw_trans, header->author);
475 if (error)
476 goto done;
477 error = gw_gen_committer_header(gw_trans, header->author);
478 if (error)
479 goto done;
480 error = gw_get_time_str(&age, header->committer_time,
481 TM_LONG);
482 if (error)
483 goto done;
484 error = gw_gen_age_header(gw_trans, age ?age : "");
485 if (error)
486 goto done;
487 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
488 if (error)
489 goto done;
490 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
491 if (kerr != KCGI_OK)
492 goto done;
493 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
494 "dotted_line", KATTR__MAX);
495 if (kerr != KCGI_OK)
496 goto done;
497 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
498 if (kerr != KCGI_OK)
499 goto done;
501 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
502 "diff", KATTR__MAX);
503 if (kerr != KCGI_OK)
504 goto done;
505 error = gw_output_diff(gw_trans, header);
506 if (error)
507 goto done;
509 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
510 if (kerr != KCGI_OK)
511 goto done;
512 done:
513 got_ref_list_free(&header->refs);
514 gw_free_headers(header);
515 free(age);
516 if (error == NULL && kerr != KCGI_OK)
517 error = gw_kcgi_error(kerr);
518 return error;
521 static const struct got_error *
522 gw_index(struct gw_trans *gw_trans)
524 const struct got_error *error = NULL;
525 struct gw_dir *gw_dir = NULL;
526 char *href_next = NULL, *href_prev = NULL, *href_summary = NULL;
527 char *href_briefs = NULL, *href_commits = NULL, *href_tree = NULL;
528 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
529 enum kcgi_err kerr;
531 if (pledge("stdio rpath proc exec sendfd unveil",
532 NULL) == -1) {
533 error = got_error_from_errno("pledge");
534 return error;
537 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
538 if (error)
539 return error;
541 error = gw_load_got_paths(gw_trans);
542 if (error)
543 return error;
545 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
546 "index_header", KATTR__MAX);
547 if (kerr != KCGI_OK)
548 return gw_kcgi_error(kerr);
549 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
550 "index_header_project", KATTR__MAX);
551 if (kerr != KCGI_OK)
552 return gw_kcgi_error(kerr);
553 kerr = khtml_puts(gw_trans->gw_html_req, "Project");
554 if (kerr != KCGI_OK)
555 return gw_kcgi_error(kerr);
556 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
557 if (kerr != KCGI_OK)
558 return gw_kcgi_error(kerr);
560 if (gw_trans->gw_conf->got_show_repo_description) {
561 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
562 "index_header_description", KATTR__MAX);
563 if (kerr != KCGI_OK)
564 return gw_kcgi_error(kerr);
565 kerr = khtml_puts(gw_trans->gw_html_req, "Description");
566 if (kerr != KCGI_OK)
567 return gw_kcgi_error(kerr);
568 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
569 if (kerr != KCGI_OK)
570 return gw_kcgi_error(kerr);
573 if (gw_trans->gw_conf->got_show_repo_owner) {
574 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
575 "index_header_owner", KATTR__MAX);
576 if (kerr != KCGI_OK)
577 return gw_kcgi_error(kerr);
578 kerr = khtml_puts(gw_trans->gw_html_req, "Owner");
579 if (kerr != KCGI_OK)
580 return gw_kcgi_error(kerr);
581 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
582 if (kerr != KCGI_OK)
583 return gw_kcgi_error(kerr);
586 if (gw_trans->gw_conf->got_show_repo_age) {
587 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
588 "index_header_age", KATTR__MAX);
589 if (kerr != KCGI_OK)
590 return gw_kcgi_error(kerr);
591 kerr = khtml_puts(gw_trans->gw_html_req, "Last Change");
592 if (kerr != KCGI_OK)
593 return gw_kcgi_error(kerr);
594 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
595 if (kerr != KCGI_OK)
596 return gw_kcgi_error(kerr);
599 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
600 if (kerr != KCGI_OK)
601 return gw_kcgi_error(kerr);
603 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
604 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
605 "index_wrapper", KATTR__MAX);
606 if (kerr != KCGI_OK)
607 return gw_kcgi_error(kerr);
608 kerr = khtml_puts(gw_trans->gw_html_req,
609 "No repositories found in ");
610 if (kerr != KCGI_OK)
611 return gw_kcgi_error(kerr);
612 kerr = khtml_puts(gw_trans->gw_html_req,
613 gw_trans->gw_conf->got_repos_path);
614 if (kerr != KCGI_OK)
615 return gw_kcgi_error(kerr);
616 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
617 if (kerr != KCGI_OK)
618 return gw_kcgi_error(kerr);
619 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
620 "dotted_line", KATTR__MAX);
621 if (kerr != KCGI_OK)
622 return gw_kcgi_error(kerr);
623 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
624 if (kerr != KCGI_OK)
625 return gw_kcgi_error(kerr);
626 return error;
629 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
630 dir_c++;
632 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
633 if (gw_trans->page > 0 && (gw_trans->page *
634 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
635 prev_disp++;
636 continue;
639 prev_disp++;
641 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
642 "index_wrapper", KATTR__MAX);
643 if (kerr != KCGI_OK)
644 return gw_kcgi_error(kerr);
646 if (asprintf(&href_summary, "?path=%s&action=summary",
647 gw_dir->name) == -1)
648 return got_error_from_errno("asprintf");
649 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
650 "index_project", KATTR__MAX);
651 if (kerr != KCGI_OK)
652 goto done;
653 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
654 href_summary, KATTR__MAX);
655 if (kerr != KCGI_OK)
656 goto done;
657 kerr = khtml_puts(gw_trans->gw_html_req, gw_dir->name);
658 if (kerr != KCGI_OK)
659 goto done;
660 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
661 if (kerr != KCGI_OK)
662 goto done;
663 if (gw_trans->gw_conf->got_show_repo_description) {
664 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
665 KATTR_ID, "index_project_description", KATTR__MAX);
666 if (kerr != KCGI_OK)
667 goto done;
668 kerr = khtml_puts(gw_trans->gw_html_req,
669 gw_dir->description ? gw_dir->description : "");
670 if (kerr != KCGI_OK)
671 goto done;
672 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
673 if (kerr != KCGI_OK)
674 goto done;
676 if (gw_trans->gw_conf->got_show_repo_owner) {
677 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
678 KATTR_ID, "index_project_owner", KATTR__MAX);
679 if (kerr != KCGI_OK)
680 goto done;
681 kerr = khtml_puts(gw_trans->gw_html_req,
682 gw_dir->owner ? gw_dir->owner : "");
683 if (kerr != KCGI_OK)
684 goto done;
685 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
686 if (kerr != KCGI_OK)
687 goto done;
689 if (gw_trans->gw_conf->got_show_repo_age) {
690 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
691 KATTR_ID, "index_project_age", KATTR__MAX);
692 if (kerr != KCGI_OK)
693 goto done;
694 kerr = khtml_puts(gw_trans->gw_html_req,
695 gw_dir->age ? gw_dir->age : "");
696 if (kerr != KCGI_OK)
697 goto done;
698 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
699 if (kerr != KCGI_OK)
700 goto done;
703 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
704 "navs_wrapper", KATTR__MAX);
705 if (kerr != KCGI_OK)
706 goto done;
707 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
708 "navs", KATTR__MAX);
709 if (kerr != KCGI_OK)
710 goto done;
712 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
713 href_summary, KATTR__MAX);
714 if (kerr != KCGI_OK)
715 goto done;
716 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
717 if (kerr != KCGI_OK)
718 goto done;
719 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
720 if (kerr != KCGI_OK)
721 goto done;
723 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
724 if (kerr != KCGI_OK)
725 goto done;
727 if (asprintf(&href_briefs, "?path=%s&action=briefs",
728 gw_dir->name) == -1) {
729 error = got_error_from_errno("asprintf");
730 goto done;
732 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
733 href_briefs, KATTR__MAX);
734 if (kerr != KCGI_OK)
735 goto done;
736 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
737 if (kerr != KCGI_OK)
738 goto done;
739 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
740 if (kerr != KCGI_OK)
741 error = gw_kcgi_error(kerr);
743 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
744 if (kerr != KCGI_OK)
745 goto done;
747 if (asprintf(&href_commits, "?path=%s&action=commits",
748 gw_dir->name) == -1) {
749 error = got_error_from_errno("asprintf");
750 goto done;
752 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
753 href_commits, KATTR__MAX);
754 if (kerr != KCGI_OK)
755 goto done;
756 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
757 if (kerr != KCGI_OK)
758 goto done;
759 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
760 if (kerr != KCGI_OK)
761 goto done;
763 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
764 if (kerr != KCGI_OK)
765 goto done;
767 if (asprintf(&href_tree, "?path=%s&action=tree",
768 gw_dir->name) == -1) {
769 error = got_error_from_errno("asprintf");
770 goto done;
772 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
773 href_tree, KATTR__MAX);
774 if (kerr != KCGI_OK)
775 goto done;
776 kerr = khtml_puts(gw_trans->gw_html_req, "tree");
777 if (kerr != KCGI_OK)
778 goto done;
780 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
781 if (kerr != KCGI_OK)
782 goto done;
783 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
784 "dotted_line", KATTR__MAX);
785 if (kerr != KCGI_OK)
786 goto done;
787 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
788 if (kerr != KCGI_OK)
789 goto done;
791 free(href_summary);
792 href_summary = NULL;
793 free(href_briefs);
794 href_briefs = NULL;
795 free(href_commits);
796 href_commits = NULL;
797 free(href_tree);
798 href_tree = NULL;
800 if (gw_trans->gw_conf->got_max_repos_display == 0)
801 continue;
803 if (next_disp == gw_trans->gw_conf->got_max_repos_display) {
804 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
805 KATTR_ID, "np_wrapper", KATTR__MAX);
806 if (kerr != KCGI_OK)
807 goto done;
808 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
809 KATTR_ID, "nav_prev", KATTR__MAX);
810 if (kerr != KCGI_OK)
811 goto done;
812 } else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
813 (gw_trans->page > 0) &&
814 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
815 prev_disp == gw_trans->repos_total)) {
816 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
817 KATTR_ID, "np_wrapper", KATTR__MAX);
818 if (kerr != KCGI_OK)
819 goto done;
820 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
821 KATTR_ID, "nav_prev", KATTR__MAX);
822 if (kerr != KCGI_OK)
823 goto done;
826 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
827 (gw_trans->page > 0) &&
828 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
829 prev_disp == gw_trans->repos_total)) {
830 if (asprintf(&href_prev, "?page=%d",
831 gw_trans->page - 1) == -1) {
832 error = got_error_from_errno("asprintf");
833 goto done;
835 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
836 KATTR_HREF, href_prev, KATTR__MAX);
837 free(href_prev);
838 if (kerr != KCGI_OK)
839 goto done;
840 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
841 if (kerr != KCGI_OK)
842 goto done;
843 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
844 if (kerr != KCGI_OK)
845 goto done;
848 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
849 if (kerr != KCGI_OK)
850 return gw_kcgi_error(kerr);
852 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
853 next_disp == gw_trans->gw_conf->got_max_repos_display &&
854 dir_c != (gw_trans->page + 1) *
855 gw_trans->gw_conf->got_max_repos_display) {
856 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
857 KATTR_ID, "nav_next", KATTR__MAX);
858 if (kerr != KCGI_OK)
859 goto done;
860 if (asprintf(&href_next, "?page=%d",
861 gw_trans->page + 1) == -1) {
862 error = got_error_from_errno("calloc");
863 goto done;
865 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
866 KATTR_HREF, href_next, KATTR__MAX);
867 free(href_next);
868 if (kerr != KCGI_OK)
869 goto done;
870 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
871 if (kerr != KCGI_OK)
872 goto done;
873 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
874 if (kerr != KCGI_OK)
875 goto done;
876 next_disp = 0;
877 break;
880 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
881 (gw_trans->page > 0) &&
882 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
883 prev_disp == gw_trans->repos_total)) {
884 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
885 if (kerr != KCGI_OK)
886 goto done;
888 next_disp++;
890 done:
891 free(href_summary);
892 free(href_briefs);
893 free(href_commits);
894 free(href_tree);
895 if (error == NULL && kerr != KCGI_OK)
896 error = gw_kcgi_error(kerr);
897 return error;
900 static const struct got_error *
901 gw_commits(struct gw_trans *gw_trans)
903 const struct got_error *error = NULL;
904 struct gw_header *header = NULL, *n_header = NULL;
905 char *age = NULL;
906 char *href_diff = NULL, *href_blob = NULL;
907 enum kcgi_err kerr = KCGI_OK;
909 if ((header = gw_init_header()) == NULL)
910 return got_error_from_errno("malloc");
912 if (pledge("stdio rpath proc exec sendfd unveil",
913 NULL) == -1) {
914 error = got_error_from_errno("pledge");
915 goto done;
918 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
919 if (error)
920 goto done;
922 error = gw_get_header(gw_trans, header,
923 gw_trans->gw_conf->got_max_commits_display);
924 if (error)
925 goto done;
927 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
928 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
929 "commits_line_wrapper", KATTR__MAX);
930 if (kerr != KCGI_OK)
931 goto done;
932 error = gw_gen_commit_header(gw_trans, n_header->commit_id,
933 n_header->refs_str);
934 if (error)
935 goto done;
936 error = gw_gen_author_header(gw_trans, n_header->author);
937 if (error)
938 goto done;
939 error = gw_gen_committer_header(gw_trans, n_header->author);
940 if (error)
941 goto done;
942 error = gw_get_time_str(&age, n_header->committer_time,
943 TM_LONG);
944 if (error)
945 goto done;
946 error = gw_gen_age_header(gw_trans, age ?age : "");
947 if (error)
948 goto done;
949 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
950 if (kerr != KCGI_OK)
951 goto done;
953 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
954 "dotted_line", KATTR__MAX);
955 if (kerr != KCGI_OK)
956 goto done;
957 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
958 if (kerr != KCGI_OK)
959 goto done;
961 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
962 "commit", KATTR__MAX);
963 if (kerr != KCGI_OK)
964 goto done;
965 kerr = khttp_puts(gw_trans->gw_req, n_header->commit_msg);
966 if (kerr != KCGI_OK)
967 goto done;
968 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
969 if (kerr != KCGI_OK)
970 goto done;
972 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
973 gw_trans->repo_name, n_header->commit_id) == -1) {
974 error = got_error_from_errno("asprintf");
975 goto done;
977 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
978 KATTR_ID, "navs_wrapper", KATTR__MAX);
979 if (kerr != KCGI_OK)
980 goto done;
981 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
982 KATTR_ID, "navs", KATTR__MAX);
983 if (kerr != KCGI_OK)
984 goto done;
985 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
986 KATTR_HREF, href_diff, KATTR__MAX);
987 if (kerr != KCGI_OK)
988 goto done;
989 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
990 if (kerr != KCGI_OK)
991 goto done;
992 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
993 if (kerr != KCGI_OK)
994 goto done;
996 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
997 if (kerr != KCGI_OK)
998 goto done;
1000 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
1001 gw_trans->repo_name, n_header->commit_id) == -1) {
1002 error = got_error_from_errno("asprintf");
1003 goto done;
1005 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1006 KATTR_HREF, href_blob, KATTR__MAX);
1007 if (kerr != KCGI_OK)
1008 goto done;
1009 khtml_puts(gw_trans->gw_html_req, "tree");
1010 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1011 if (kerr != KCGI_OK)
1012 goto done;
1013 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1014 if (kerr != KCGI_OK)
1015 goto done;
1017 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1018 "solid_line", KATTR__MAX);
1019 if (kerr != KCGI_OK)
1020 goto done;
1021 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1022 if (kerr != KCGI_OK)
1023 goto done;
1025 free(age);
1026 age = NULL;
1028 done:
1029 got_ref_list_free(&header->refs);
1030 gw_free_headers(header);
1031 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1032 gw_free_headers(n_header);
1033 free(age);
1034 free(href_diff);
1035 free(href_blob);
1036 if (error == NULL && kerr != KCGI_OK)
1037 error = gw_kcgi_error(kerr);
1038 return error;
1041 static const struct got_error *
1042 gw_briefs(struct gw_trans *gw_trans)
1044 const struct got_error *error = NULL;
1045 struct gw_header *header = NULL, *n_header = NULL;
1046 char *age = NULL, *age_html = NULL;
1047 char *href_diff = NULL, *href_blob = NULL;
1048 char *newline, *smallerthan;
1049 enum kcgi_err kerr = KCGI_OK;
1051 if ((header = gw_init_header()) == NULL)
1052 return got_error_from_errno("malloc");
1054 if (pledge("stdio rpath proc exec sendfd unveil",
1055 NULL) == -1) {
1056 error = got_error_from_errno("pledge");
1057 goto done;
1060 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
1061 if (error)
1062 goto done;
1064 if (gw_trans->action == GW_SUMMARY)
1065 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
1066 else
1067 error = gw_get_header(gw_trans, header,
1068 gw_trans->gw_conf->got_max_commits_display);
1069 if (error)
1070 goto done;
1072 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
1073 error = gw_get_time_str(&age, n_header->committer_time,
1074 TM_DIFF);
1075 if (error)
1076 goto done;
1078 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1079 KATTR_ID, "briefs_wrapper", KATTR__MAX);
1080 if (kerr != KCGI_OK)
1081 goto done;
1083 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1084 KATTR_ID, "briefs_age", KATTR__MAX);
1085 if (kerr != KCGI_OK)
1086 goto done;
1087 if (asprintf(&age_html, "%s", age ? age : "") == -1) {
1088 error = got_error_from_errno("asprintf");
1089 goto done;
1091 kerr = khtml_puts(gw_trans->gw_html_req, age_html);
1092 if (kerr != KCGI_OK)
1093 goto done;
1094 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1095 if (kerr != KCGI_OK)
1096 goto done;
1098 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1099 KATTR_ID, "briefs_author", KATTR__MAX);
1100 if (kerr != KCGI_OK)
1101 goto done;
1102 smallerthan = strchr(n_header->author, '<');
1103 if (smallerthan)
1104 *smallerthan = '\0';
1105 kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
1106 if (kerr != KCGI_OK)
1107 goto done;
1108 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1109 if (kerr != KCGI_OK)
1110 goto done;
1112 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
1113 gw_trans->repo_name, n_header->commit_id) == -1) {
1114 error = got_error_from_errno("asprintf");
1115 goto done;
1117 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1118 KATTR_ID, "briefs_log", KATTR__MAX);
1119 if (kerr != KCGI_OK)
1120 goto done;
1121 newline = strchr(n_header->commit_msg, '\n');
1122 if (newline)
1123 *newline = '\0';
1124 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1125 KATTR_HREF, href_diff, KATTR__MAX);
1126 if (kerr != KCGI_OK)
1127 goto done;
1128 kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
1129 if (kerr != KCGI_OK)
1130 goto done;
1131 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1132 if (kerr != KCGI_OK)
1133 goto done;
1135 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1136 KATTR_ID, "navs_wrapper", KATTR__MAX);
1137 if (kerr != KCGI_OK)
1138 goto done;
1139 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1140 KATTR_ID, "navs", KATTR__MAX);
1141 if (kerr != KCGI_OK)
1142 goto done;
1143 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1144 KATTR_HREF, href_diff, KATTR__MAX);
1145 if (kerr != KCGI_OK)
1146 goto done;
1147 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1148 if (kerr != KCGI_OK)
1149 goto done;
1150 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1151 if (kerr != KCGI_OK)
1152 goto done;
1154 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1155 if (kerr != KCGI_OK)
1156 goto done;
1158 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
1159 gw_trans->repo_name, n_header->commit_id) == -1) {
1160 error = got_error_from_errno("asprintf");
1161 goto done;
1163 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1164 KATTR_HREF, href_blob, KATTR__MAX);
1165 if (kerr != KCGI_OK)
1166 goto done;
1167 khtml_puts(gw_trans->gw_html_req, "tree");
1168 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1169 if (kerr != KCGI_OK)
1170 goto done;
1171 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1172 if (kerr != KCGI_OK)
1173 goto done;
1175 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1176 KATTR_ID, "dotted_line", KATTR__MAX);
1177 if (kerr != KCGI_OK)
1178 goto done;
1179 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1180 if (kerr != KCGI_OK)
1181 goto done;
1183 free(age);
1184 age = NULL;
1185 free(age_html);
1186 age_html = NULL;
1187 free(href_diff);
1188 href_diff = NULL;
1189 free(href_blob);
1190 href_blob = NULL;
1192 done:
1193 got_ref_list_free(&header->refs);
1194 gw_free_headers(header);
1195 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1196 gw_free_headers(n_header);
1197 free(age);
1198 free(age_html);
1199 free(href_diff);
1200 free(href_blob);
1201 if (error == NULL && kerr != KCGI_OK)
1202 error = gw_kcgi_error(kerr);
1203 return error;
1206 static const struct got_error *
1207 gw_summary(struct gw_trans *gw_trans)
1209 const struct got_error *error = NULL;
1210 char *age = NULL;
1211 enum kcgi_err kerr = KCGI_OK;
1213 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1214 return got_error_from_errno("pledge");
1216 /* unveil is applied with gw_briefs below */
1218 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1219 "summary_wrapper", KATTR__MAX);
1220 if (kerr != KCGI_OK)
1221 return gw_kcgi_error(kerr);
1223 if (gw_trans->gw_conf->got_show_repo_description &&
1224 gw_trans->gw_dir->description != NULL &&
1225 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1226 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1227 KATTR_ID, "description_title", KATTR__MAX);
1228 if (kerr != KCGI_OK)
1229 goto done;
1230 kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1231 if (kerr != KCGI_OK)
1232 goto done;
1233 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1234 if (kerr != KCGI_OK)
1235 goto done;
1236 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1237 KATTR_ID, "description", KATTR__MAX);
1238 if (kerr != KCGI_OK)
1239 goto done;
1240 kerr = khtml_puts(gw_trans->gw_html_req,
1241 gw_trans->gw_dir->description);
1242 if (kerr != KCGI_OK)
1243 goto done;
1244 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1245 if (kerr != KCGI_OK)
1246 goto done;
1249 if (gw_trans->gw_conf->got_show_repo_owner &&
1250 gw_trans->gw_dir->owner != NULL) {
1251 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1252 KATTR_ID, "repo_owner_title", KATTR__MAX);
1253 if (kerr != KCGI_OK)
1254 goto done;
1255 kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1256 if (kerr != KCGI_OK)
1257 goto done;
1258 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1259 if (kerr != KCGI_OK)
1260 goto done;
1261 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1262 KATTR_ID, "repo_owner", KATTR__MAX);
1263 if (kerr != KCGI_OK)
1264 goto done;
1265 kerr = khtml_puts(gw_trans->gw_html_req,
1266 gw_trans->gw_dir->owner);
1267 if (kerr != KCGI_OK)
1268 goto done;
1269 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1270 if (kerr != KCGI_OK)
1271 goto done;
1274 if (gw_trans->gw_conf->got_show_repo_age) {
1275 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1276 "refs/heads", TM_LONG);
1277 if (error)
1278 goto done;
1279 if (age != NULL) {
1280 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1281 KATTR_ID, "last_change_title", KATTR__MAX);
1282 if (kerr != KCGI_OK)
1283 goto done;
1284 kerr = khtml_puts(gw_trans->gw_html_req,
1285 "Last Change: ");
1286 if (kerr != KCGI_OK)
1287 goto done;
1288 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1289 if (kerr != KCGI_OK)
1290 goto done;
1291 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1292 KATTR_ID, "last_change", KATTR__MAX);
1293 if (kerr != KCGI_OK)
1294 goto done;
1295 kerr = khtml_puts(gw_trans->gw_html_req, age);
1296 if (kerr != KCGI_OK)
1297 goto done;
1298 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1299 if (kerr != KCGI_OK)
1300 goto done;
1304 if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1305 gw_trans->gw_dir->url != NULL &&
1306 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1307 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1308 KATTR_ID, "cloneurl_title", KATTR__MAX);
1309 if (kerr != KCGI_OK)
1310 goto done;
1311 kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1312 if (kerr != KCGI_OK)
1313 goto done;
1314 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1315 if (kerr != KCGI_OK)
1316 goto done;
1317 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1318 KATTR_ID, "cloneurl", KATTR__MAX);
1319 if (kerr != KCGI_OK)
1320 goto done;
1321 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1322 if (kerr != KCGI_OK)
1323 goto done;
1324 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1325 if (kerr != KCGI_OK)
1326 goto done;
1329 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1330 if (kerr != KCGI_OK)
1331 goto done;
1333 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1334 "briefs_title_wrapper", KATTR__MAX);
1335 if (kerr != KCGI_OK)
1336 goto done;
1337 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1338 "briefs_title", KATTR__MAX);
1339 if (kerr != KCGI_OK)
1340 goto done;
1341 kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1342 if (kerr != KCGI_OK)
1343 goto done;
1344 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1345 if (kerr != KCGI_OK)
1346 goto done;
1347 error = gw_briefs(gw_trans);
1348 if (error)
1349 goto done;
1351 error = gw_output_repo_tags(gw_trans, NULL, D_MAXSLCOMMDISP,
1352 TAGBRIEF);
1353 if (error)
1354 goto done;
1356 error = gw_output_repo_heads(gw_trans);
1357 done:
1358 free(age);
1359 if (error == NULL && kerr != KCGI_OK)
1360 error = gw_kcgi_error(kerr);
1361 return error;
1364 static const struct got_error *
1365 gw_tree(struct gw_trans *gw_trans)
1367 const struct got_error *error = NULL;
1368 struct gw_header *header = NULL;
1369 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1370 char *age = NULL, *age_html = NULL;
1371 enum kcgi_err kerr;
1373 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1374 return got_error_from_errno("pledge");
1376 if ((header = gw_init_header()) == NULL)
1377 return got_error_from_errno("malloc");
1379 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
1380 if (error)
1381 goto done;
1383 error = gw_get_header(gw_trans, header, 1);
1384 if (error)
1385 goto done;
1387 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1388 "tree_header_wrapper", KATTR__MAX);
1389 if (kerr != KCGI_OK)
1390 goto done;
1391 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1392 "tree_header", KATTR__MAX);
1393 if (kerr != KCGI_OK)
1394 goto done;
1395 error = gw_gen_tree_header(gw_trans, header->tree_id);
1396 if (error)
1397 goto done;
1398 error = gw_get_time_str(&age, header->committer_time,
1399 TM_LONG);
1400 if (error)
1401 goto done;
1402 error = gw_gen_age_header(gw_trans, age ?age : "");
1403 if (error)
1404 goto done;
1405 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1406 if (error)
1407 goto done;
1408 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1409 if (kerr != KCGI_OK)
1410 goto done;
1411 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1412 "dotted_line", KATTR__MAX);
1413 if (kerr != KCGI_OK)
1414 goto done;
1415 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1416 if (kerr != KCGI_OK)
1417 goto done;
1419 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1420 "tree", KATTR__MAX);
1421 if (kerr != KCGI_OK)
1422 goto done;
1423 error = gw_output_repo_tree(gw_trans);
1424 if (error)
1425 goto done;
1427 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1428 done:
1429 got_ref_list_free(&header->refs);
1430 gw_free_headers(header);
1431 free(tree_html_disp);
1432 free(tree_html);
1433 free(tree);
1434 free(age);
1435 free(age_html);
1436 if (error == NULL && kerr != KCGI_OK)
1437 error = gw_kcgi_error(kerr);
1438 return error;
1441 static const struct got_error *
1442 gw_tag(struct gw_trans *gw_trans)
1444 const struct got_error *error = NULL;
1445 struct gw_header *header = NULL;
1446 enum kcgi_err kerr;
1448 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1449 return got_error_from_errno("pledge");
1451 if ((header = gw_init_header()) == NULL)
1452 return got_error_from_errno("malloc");
1454 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
1455 if (error)
1456 goto done;
1458 error = gw_get_header(gw_trans, header, 1);
1459 if (error)
1460 goto done;
1462 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1463 "tag_header_wrapper", KATTR__MAX);
1464 if (kerr != KCGI_OK)
1465 goto done;
1466 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1467 "tag_header", KATTR__MAX);
1468 if (kerr != KCGI_OK)
1469 goto done;
1470 error = gw_gen_commit_header(gw_trans, header->commit_id,
1471 header->refs_str);
1472 if (error)
1473 goto done;
1474 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1475 if (error)
1476 goto done;
1477 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1478 if (kerr != KCGI_OK)
1479 goto done;
1480 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1481 "dotted_line", KATTR__MAX);
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;
1488 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1489 "tree", KATTR__MAX);
1490 if (kerr != KCGI_OK)
1491 goto done;
1493 error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1494 if (error)
1495 goto done;
1497 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1498 done:
1499 got_ref_list_free(&header->refs);
1500 gw_free_headers(header);
1501 if (error == NULL && kerr != KCGI_OK)
1502 error = gw_kcgi_error(kerr);
1503 return error;
1506 static const struct got_error *
1507 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1509 const struct got_error *error = NULL;
1510 DIR *dt;
1511 char *dir_test;
1512 int opened = 0;
1514 if (asprintf(&dir_test, "%s/%s/%s",
1515 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1516 GOTWEB_GIT_DIR) == -1)
1517 return got_error_from_errno("asprintf");
1519 dt = opendir(dir_test);
1520 if (dt == NULL) {
1521 free(dir_test);
1522 } else {
1523 gw_dir->path = strdup(dir_test);
1524 opened = 1;
1525 goto done;
1528 if (asprintf(&dir_test, "%s/%s/%s",
1529 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1530 GOTWEB_GOT_DIR) == -1)
1531 return got_error_from_errno("asprintf");
1533 dt = opendir(dir_test);
1534 if (dt == NULL)
1535 free(dir_test);
1536 else {
1537 opened = 1;
1538 error = got_error(GOT_ERR_NOT_GIT_REPO);
1539 goto errored;
1542 if (asprintf(&dir_test, "%s/%s",
1543 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1)
1544 return got_error_from_errno("asprintf");
1546 gw_dir->path = strdup(dir_test);
1547 done:
1548 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1549 gw_dir->path);
1550 if (error)
1551 goto errored;
1552 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1553 if (error)
1554 goto errored;
1555 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1556 "refs/heads", TM_DIFF);
1557 if (error)
1558 goto errored;
1559 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1560 errored:
1561 free(dir_test);
1562 if (opened)
1563 closedir(dt);
1564 return error;
1567 static const struct got_error *
1568 gw_load_got_paths(struct gw_trans *gw_trans)
1570 const struct got_error *error = NULL;
1571 DIR *d;
1572 struct dirent **sd_dent;
1573 struct gw_dir *gw_dir;
1574 struct stat st;
1575 unsigned int d_cnt, d_i;
1577 d = opendir(gw_trans->gw_conf->got_repos_path);
1578 if (d == NULL) {
1579 error = got_error_from_errno2("opendir",
1580 gw_trans->gw_conf->got_repos_path);
1581 return error;
1584 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1585 alphasort);
1586 if (d_cnt == -1) {
1587 error = got_error_from_errno2("scandir",
1588 gw_trans->gw_conf->got_repos_path);
1589 return error;
1592 for (d_i = 0; d_i < d_cnt; d_i++) {
1593 if (gw_trans->gw_conf->got_max_repos > 0 &&
1594 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1595 break; /* account for parent and self */
1597 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1598 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1599 continue;
1601 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
1602 return got_error_from_errno("gw_dir malloc");
1604 error = gw_load_got_path(gw_trans, gw_dir);
1605 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1606 error = NULL;
1607 continue;
1609 else if (error)
1610 return error;
1612 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1613 !got_path_dir_is_empty(gw_dir->path)) {
1614 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1615 entry);
1616 gw_trans->repos_total++;
1620 closedir(d);
1621 return error;
1624 static const struct got_error *
1625 gw_parse_querystring(struct gw_trans *gw_trans)
1627 const struct got_error *error = NULL;
1628 struct kpair *p;
1629 struct gw_query_action *action = NULL;
1630 unsigned int i;
1632 if (gw_trans->gw_req->fieldnmap[0]) {
1633 error = got_error_from_errno("bad parse");
1634 return error;
1635 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1636 /* define gw_trans->repo_path */
1637 if (asprintf(&gw_trans->repo_name, "%s", p->parsed.s) == -1)
1638 return got_error_from_errno("asprintf");
1640 if (asprintf(&gw_trans->repo_path, "%s/%s",
1641 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
1642 return got_error_from_errno("asprintf");
1644 /* get action and set function */
1645 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
1646 for (i = 0; i < nitems(gw_query_funcs); i++) {
1647 action = &gw_query_funcs[i];
1648 if (action->func_name == NULL)
1649 continue;
1651 if (strcmp(action->func_name,
1652 p->parsed.s) == 0) {
1653 gw_trans->action = i;
1654 if (asprintf(&gw_trans->action_name,
1655 "%s", action->func_name) == -1)
1656 return
1657 got_error_from_errno(
1658 "asprintf");
1660 break;
1663 action = NULL;
1666 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
1667 if (asprintf(&gw_trans->commit, "%s",
1668 p->parsed.s) == -1)
1669 return got_error_from_errno("asprintf");
1671 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1672 if (asprintf(&gw_trans->repo_file, "%s",
1673 p->parsed.s) == -1)
1674 return got_error_from_errno("asprintf");
1676 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER]))
1677 if (asprintf(&gw_trans->repo_folder, "%s",
1678 p->parsed.s) == -1)
1679 return got_error_from_errno("asprintf");
1681 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
1682 if (asprintf(&gw_trans->headref, "%s",
1683 p->parsed.s) == -1)
1684 return got_error_from_errno("asprintf");
1686 if (action == NULL) {
1687 error = got_error_from_errno("invalid action");
1688 return error;
1690 if ((gw_trans->gw_dir =
1691 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
1692 return got_error_from_errno("gw_dir malloc");
1694 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1695 if (error)
1696 return error;
1697 } else
1698 gw_trans->action = GW_INDEX;
1700 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1701 gw_trans->page = p->parsed.i;
1703 return error;
1706 static struct gw_dir *
1707 gw_init_gw_dir(char *dir)
1709 struct gw_dir *gw_dir;
1711 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
1712 return NULL;
1714 if (asprintf(&gw_dir->name, "%s", dir) == -1)
1715 return NULL;
1717 return gw_dir;
1720 static const struct got_error *
1721 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1723 enum kcgi_err kerr;
1725 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1726 if (kerr != KCGI_OK)
1727 return gw_kcgi_error(kerr);
1728 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1729 khttps[code]);
1730 if (kerr != KCGI_OK)
1731 return gw_kcgi_error(kerr);
1732 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1733 kmimetypes[mime]);
1734 if (kerr != KCGI_OK)
1735 return gw_kcgi_error(kerr);
1736 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
1737 "nosniff");
1738 if (kerr != KCGI_OK)
1739 return gw_kcgi_error(kerr);
1740 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1741 if (kerr != KCGI_OK)
1742 return gw_kcgi_error(kerr);
1743 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
1744 "1; mode=block");
1745 if (kerr != KCGI_OK)
1746 return gw_kcgi_error(kerr);
1748 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
1749 kerr = khttp_head(gw_trans->gw_req,
1750 kresps[KRESP_CONTENT_DISPOSITION],
1751 "attachment; filename=%s", gw_trans->repo_file);
1752 if (kerr != KCGI_OK)
1753 return gw_kcgi_error(kerr);
1756 kerr = khttp_body(gw_trans->gw_req);
1757 return gw_kcgi_error(kerr);
1760 static const struct got_error *
1761 gw_display_index(struct gw_trans *gw_trans)
1763 const struct got_error *error;
1764 enum kcgi_err kerr;
1766 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1767 if (error)
1768 return error;
1770 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1771 if (kerr != KCGI_OK)
1772 return gw_kcgi_error(kerr);
1774 if (gw_trans->action != GW_BLOB) {
1775 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1776 gw_query_funcs[gw_trans->action].template);
1777 if (kerr != KCGI_OK) {
1778 khtml_close(gw_trans->gw_html_req);
1779 return gw_kcgi_error(kerr);
1783 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
1786 static void
1787 gw_display_error(struct gw_trans *gw_trans, const struct got_error *err)
1789 if (gw_display_open(gw_trans, KHTTP_200, gw_trans->mime) != NULL)
1790 return;
1792 if (khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0) != KCGI_OK)
1793 return;
1794 khtml_puts(gw_trans->gw_html_req, err->msg);
1795 khtml_close(gw_trans->gw_html_req);
1798 static int
1799 gw_template(size_t key, void *arg)
1801 const struct got_error *error = NULL;
1802 enum kcgi_err kerr;
1803 struct gw_trans *gw_trans = arg;
1804 char *img_src = NULL;
1806 switch (key) {
1807 case (TEMPL_HEAD):
1808 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1809 KATTR_CONTENT, "initial-scale=1.0, user-scalable=no",
1810 KATTR__MAX);
1811 if (kerr != KCGI_OK)
1812 return 0;
1813 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1814 if (kerr != KCGI_OK)
1815 return 0;
1816 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1817 KATTR_CHARSET, "utf-8",
1818 KATTR__MAX);
1819 if (kerr != KCGI_OK)
1820 return 0;
1821 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1822 if (kerr != KCGI_OK)
1823 return 0;
1824 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1825 KATTR_NAME, "msapplication-TileColor",
1826 KATTR_CONTENT, "#da532c", KATTR__MAX);
1827 if (kerr != KCGI_OK)
1828 return 0;
1829 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1830 if (kerr != KCGI_OK)
1831 return 0;
1832 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1833 KATTR_NAME, "theme-color",
1834 KATTR_CONTENT, "#ffffff", KATTR__MAX);
1835 if (kerr != KCGI_OK)
1836 return 0;
1837 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1838 if (kerr != KCGI_OK)
1839 return 0;
1840 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1841 KATTR_REL, "apple-touch-icon", KATTR_SIZES, "180x180",
1842 KATTR_HREF, "/apple-touch-icon.png", KATTR__MAX);
1843 if (kerr != KCGI_OK)
1844 return 0;
1845 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1846 if (kerr != KCGI_OK)
1847 return 0;
1848 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1849 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
1850 "32x32", KATTR_HREF, "/favicon-32x32.png", KATTR__MAX);
1851 if (kerr != KCGI_OK)
1852 return 0;
1853 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1854 if (kerr != KCGI_OK)
1855 return 0;
1856 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1857 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
1858 "16x16", KATTR_HREF, "/favicon-16x16.png", KATTR__MAX);
1859 if (kerr != KCGI_OK)
1860 return 0;
1861 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1862 if (kerr != KCGI_OK)
1863 return 0;
1864 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1865 KATTR_REL, "manifest", KATTR_HREF, "/site.webmanifest",
1866 KATTR__MAX);
1867 if (kerr != KCGI_OK)
1868 return 0;
1869 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1870 if (kerr != KCGI_OK)
1871 return 0;
1872 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1873 KATTR_REL, "mask-icon", KATTR_HREF,
1874 "/safari-pinned-tab.svg", KATTR__MAX);
1875 if (kerr != KCGI_OK)
1876 return 0;
1877 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1878 if (kerr != KCGI_OK)
1879 return 0;
1880 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1881 KATTR_REL, "stylesheet", KATTR_TYPE, "text/css",
1882 KATTR_HREF, "/gotweb.css", KATTR__MAX);
1883 if (kerr != KCGI_OK)
1884 return 0;
1885 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1886 if (kerr != KCGI_OK)
1887 return 0;
1888 break;
1889 case(TEMPL_HEADER):
1890 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1891 KATTR_ID, "got_link", KATTR__MAX);
1892 if (kerr != KCGI_OK)
1893 return 0;
1894 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1895 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
1896 KATTR_TARGET, "_sotd", KATTR__MAX);
1897 if (kerr != KCGI_OK)
1898 return 0;
1899 if (asprintf(&img_src, "/%s",
1900 gw_trans->gw_conf->got_logo) == -1)
1901 return 0;
1902 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
1903 KATTR_SRC, img_src, KATTR__MAX);
1904 if (kerr != KCGI_OK) {
1905 free(img_src);
1906 return 0;
1908 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1909 if (kerr != KCGI_OK) {
1910 free(img_src);
1911 return 0;
1913 break;
1914 case (TEMPL_SITEPATH):
1915 error = gw_output_site_link(gw_trans);
1916 if (error)
1917 return 0;
1918 break;
1919 case(TEMPL_TITLE):
1920 if (gw_trans->gw_conf->got_site_name != NULL) {
1921 kerr = khtml_puts(gw_trans->gw_html_req,
1922 gw_trans->gw_conf->got_site_name);
1923 if (kerr != KCGI_OK)
1924 return 0;
1926 break;
1927 case (TEMPL_SEARCH):
1928 break;
1929 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1930 "search", KATTR__MAX);
1931 if (kerr != KCGI_OK)
1932 return 0;
1933 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
1934 KATTR_METHOD, "POST", KATTR__MAX);
1935 if (kerr != KCGI_OK)
1936 return 0;
1937 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
1938 "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
1939 KATTR_MAXLENGTH, "50", KATTR__MAX);
1940 if (kerr != KCGI_OK)
1941 return 0;
1942 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
1943 KATTR__MAX);
1944 if (kerr != KCGI_OK)
1945 return 0;
1946 kerr = khtml_puts(gw_trans->gw_html_req, "Search");
1947 if (kerr != KCGI_OK)
1948 return 0;
1949 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
1950 if (kerr != KCGI_OK)
1951 return 0;
1952 break;
1953 case(TEMPL_SITEOWNER):
1954 if (gw_trans->gw_conf->got_site_owner != NULL &&
1955 gw_trans->gw_conf->got_show_site_owner) {
1956 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1957 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
1958 if (kerr != KCGI_OK)
1959 return 0;
1960 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1961 KATTR_ID, "site_owner", KATTR__MAX);
1962 if (kerr != KCGI_OK)
1963 return 0;
1964 kerr = khtml_puts(gw_trans->gw_html_req,
1965 gw_trans->gw_conf->got_site_owner);
1966 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1967 if (kerr != KCGI_OK)
1968 return 0;
1970 break;
1971 case(TEMPL_CONTENT):
1972 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1973 if (error) {
1974 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1975 KATTR_ID, "tmpl_err", KATTR__MAX);
1976 if (kerr != KCGI_OK)
1977 return 0;
1978 kerr = khttp_puts(gw_trans->gw_req, "Error: ");
1979 if (kerr != KCGI_OK)
1980 return 0;
1981 kerr = khttp_puts(gw_trans->gw_req, error->msg);
1982 if (kerr != KCGI_OK)
1983 return 0;
1984 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1985 if (kerr != KCGI_OK)
1986 return 0;
1988 break;
1989 default:
1990 return 0;
1992 return 1;
1995 static const struct got_error *
1996 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
1998 const struct got_error *error = NULL;
1999 char *ref_str = NULL;
2000 enum kcgi_err kerr = KCGI_OK;
2002 if (strcmp(str2, "") != 0) {
2003 if (asprintf(&ref_str, "(%s)", str2) == -1) {
2004 error = got_error_from_errno("asprintf");
2005 goto done;
2007 } else
2008 ref_str = strdup("");
2010 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2011 KATTR_ID, "header_commit_title", KATTR__MAX);
2012 if (kerr != KCGI_OK)
2013 goto done;
2014 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
2015 if (kerr != KCGI_OK)
2016 goto done;
2017 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2018 if (kerr != KCGI_OK)
2019 goto done;
2020 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2021 KATTR_ID, "header_commit", KATTR__MAX);
2022 if (kerr != KCGI_OK)
2023 goto done;
2024 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2025 if (kerr != KCGI_OK)
2026 goto done;
2027 kerr = khtml_puts(gw_trans->gw_html_req, " ");
2028 if (kerr != KCGI_OK)
2029 goto done;
2030 kerr = khtml_puts(gw_trans->gw_html_req, ref_str);
2031 if (kerr != KCGI_OK)
2032 goto done;
2033 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2034 done:
2035 if (error == NULL && kerr != KCGI_OK)
2036 error = gw_kcgi_error(kerr);
2037 return error;
2040 static const struct got_error *
2041 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2043 const struct got_error *error = NULL;
2044 enum kcgi_err kerr = KCGI_OK;
2046 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2047 KATTR_ID, "header_diff_title", KATTR__MAX);
2048 if (kerr != KCGI_OK)
2049 goto done;
2050 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2051 if (kerr != KCGI_OK)
2052 goto done;
2053 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2054 if (kerr != KCGI_OK)
2055 goto done;
2056 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2057 KATTR_ID, "header_diff", KATTR__MAX);
2058 if (kerr != KCGI_OK)
2059 goto done;
2060 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2061 if (kerr != KCGI_OK)
2062 goto done;
2063 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2064 if (kerr != KCGI_OK)
2065 goto done;
2066 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2067 if (kerr != KCGI_OK)
2068 goto done;
2069 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2070 done:
2071 if (error == NULL && kerr != KCGI_OK)
2072 error = gw_kcgi_error(kerr);
2073 return error;
2076 static const struct got_error *
2077 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2079 const struct got_error *error = NULL;
2080 enum kcgi_err kerr = KCGI_OK;
2082 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2083 KATTR_ID, "header_age_title", KATTR__MAX);
2084 if (kerr != KCGI_OK)
2085 goto done;
2086 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2087 if (kerr != KCGI_OK)
2088 goto done;
2089 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2090 if (kerr != KCGI_OK)
2091 goto done;
2092 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2093 KATTR_ID, "header_age", KATTR__MAX);
2094 if (kerr != KCGI_OK)
2095 goto done;
2096 kerr = khtml_puts(gw_trans->gw_html_req, str);
2097 if (kerr != KCGI_OK)
2098 goto done;
2099 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2100 done:
2101 if (error == NULL && kerr != KCGI_OK)
2102 error = gw_kcgi_error(kerr);
2103 return error;
2106 static const struct got_error *
2107 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2109 const struct got_error *error = NULL;
2110 enum kcgi_err kerr;
2112 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2113 KATTR_ID, "header_author_title", KATTR__MAX);
2114 if (kerr != KCGI_OK)
2115 goto done;
2116 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2117 if (kerr != KCGI_OK)
2118 goto done;
2119 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2120 if (kerr != KCGI_OK)
2121 goto done;
2122 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2123 KATTR_ID, "header_author", KATTR__MAX);
2124 if (kerr != KCGI_OK)
2125 goto done;
2126 kerr = khtml_puts(gw_trans->gw_html_req, str);
2127 if (kerr != KCGI_OK)
2128 goto done;
2129 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2130 done:
2131 if (error == NULL && kerr != KCGI_OK)
2132 error = gw_kcgi_error(kerr);
2133 return error;
2136 static const struct got_error *
2137 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2139 const struct got_error *error = NULL;
2140 enum kcgi_err kerr;
2142 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2143 KATTR_ID, "header_committer_title", KATTR__MAX);
2144 if (kerr != KCGI_OK)
2145 goto done;
2146 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2147 if (kerr != KCGI_OK)
2148 goto done;
2149 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2150 if (kerr != KCGI_OK)
2151 goto done;
2152 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2153 KATTR_ID, "header_committer", KATTR__MAX);
2154 if (kerr != KCGI_OK)
2155 goto done;
2156 kerr = khtml_puts(gw_trans->gw_html_req, str);
2157 if (kerr != KCGI_OK)
2158 goto done;
2159 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2160 done:
2161 if (error == NULL && kerr != KCGI_OK)
2162 error = gw_kcgi_error(kerr);
2163 return error;
2166 static const struct got_error *
2167 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2169 const struct got_error *error = NULL;
2170 enum kcgi_err kerr;
2172 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2173 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2174 if (kerr != KCGI_OK)
2175 goto done;
2176 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2177 if (kerr != KCGI_OK)
2178 goto done;
2179 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2180 if (kerr != KCGI_OK)
2181 goto done;
2182 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2183 KATTR_ID, "header_commit_msg", KATTR__MAX);
2184 if (kerr != KCGI_OK)
2185 goto done;
2186 kerr = khttp_puts(gw_trans->gw_req, str);
2187 if (kerr != KCGI_OK)
2188 goto done;
2189 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2190 done:
2191 if (error == NULL && kerr != KCGI_OK)
2192 error = gw_kcgi_error(kerr);
2193 return error;
2196 static const struct got_error *
2197 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2199 const struct got_error *error = NULL;
2200 enum kcgi_err kerr;
2202 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2203 KATTR_ID, "header_tree_title", KATTR__MAX);
2204 if (kerr != KCGI_OK)
2205 goto done;
2206 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2207 if (kerr != KCGI_OK)
2208 goto done;
2209 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2210 if (kerr != KCGI_OK)
2211 goto done;
2212 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2213 KATTR_ID, "header_tree", KATTR__MAX);
2214 if (kerr != KCGI_OK)
2215 goto done;
2216 kerr = khtml_puts(gw_trans->gw_html_req, str);
2217 if (kerr != KCGI_OK)
2218 goto done;
2219 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2220 done:
2221 if (error == NULL && kerr != KCGI_OK)
2222 error = gw_kcgi_error(kerr);
2223 return error;
2226 static const struct got_error *
2227 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2228 char *dir)
2230 const struct got_error *error = NULL;
2231 FILE *f = NULL;
2232 char *d_file = NULL;
2233 unsigned int len;
2234 size_t n;
2236 *description = NULL;
2237 if (gw_trans->gw_conf->got_show_repo_description == 0)
2238 return gw_empty_string(description);
2240 if (asprintf(&d_file, "%s/description", dir) == -1)
2241 return got_error_from_errno("asprintf");
2243 f = fopen(d_file, "r");
2244 if (f == NULL) {
2245 if (errno == ENOENT || errno == EACCES)
2246 return gw_empty_string(description);
2247 error = got_error_from_errno2("fopen", d_file);
2248 goto done;
2251 if (fseek(f, 0, SEEK_END) == -1) {
2252 error = got_ferror(f, GOT_ERR_IO);
2253 goto done;
2255 len = ftell(f);
2256 if (len == -1) {
2257 error = got_ferror(f, GOT_ERR_IO);
2258 goto done;
2260 if (fseek(f, 0, SEEK_SET) == -1) {
2261 error = got_ferror(f, GOT_ERR_IO);
2262 goto done;
2264 *description = calloc(len + 1, sizeof(**description));
2265 if (*description == NULL) {
2266 error = got_error_from_errno("calloc");
2267 goto done;
2270 n = fread(*description, 1, len, f);
2271 if (n == 0 && ferror(f))
2272 error = got_ferror(f, GOT_ERR_IO);
2273 done:
2274 if (f != NULL && fclose(f) == -1 && error == NULL)
2275 error = got_error_from_errno("fclose");
2276 free(d_file);
2277 return error;
2280 static const struct got_error *
2281 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2283 struct tm tm;
2284 time_t diff_time;
2285 char *years = "years ago", *months = "months ago";
2286 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2287 char *minutes = "minutes ago", *seconds = "seconds ago";
2288 char *now = "right now";
2289 char *s;
2290 char datebuf[29];
2292 *repo_age = NULL;
2294 switch (ref_tm) {
2295 case TM_DIFF:
2296 diff_time = time(NULL) - committer_time;
2297 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2298 if (asprintf(repo_age, "%lld %s",
2299 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2300 return got_error_from_errno("asprintf");
2301 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2302 if (asprintf(repo_age, "%lld %s",
2303 (diff_time / 60 / 60 / 24 / (365 / 12)),
2304 months) == -1)
2305 return got_error_from_errno("asprintf");
2306 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2307 if (asprintf(repo_age, "%lld %s",
2308 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2309 return got_error_from_errno("asprintf");
2310 } else if (diff_time > 60 * 60 * 24 * 2) {
2311 if (asprintf(repo_age, "%lld %s",
2312 (diff_time / 60 / 60 / 24), days) == -1)
2313 return got_error_from_errno("asprintf");
2314 } else if (diff_time > 60 * 60 * 2) {
2315 if (asprintf(repo_age, "%lld %s",
2316 (diff_time / 60 / 60), hours) == -1)
2317 return got_error_from_errno("asprintf");
2318 } else if (diff_time > 60 * 2) {
2319 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2320 minutes) == -1)
2321 return got_error_from_errno("asprintf");
2322 } else if (diff_time > 2) {
2323 if (asprintf(repo_age, "%lld %s", diff_time,
2324 seconds) == -1)
2325 return got_error_from_errno("asprintf");
2326 } else {
2327 if (asprintf(repo_age, "%s", now) == -1)
2328 return got_error_from_errno("asprintf");
2330 break;
2331 case TM_LONG:
2332 if (gmtime_r(&committer_time, &tm) == NULL)
2333 return got_error_from_errno("gmtime_r");
2335 s = asctime_r(&tm, datebuf);
2336 if (s == NULL)
2337 return got_error_from_errno("asctime_r");
2339 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2340 return got_error_from_errno("asprintf");
2341 break;
2343 return NULL;
2346 static const struct got_error *
2347 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2348 char *repo_ref, int ref_tm)
2350 const struct got_error *error = NULL;
2351 struct got_object_id *id = NULL;
2352 struct got_repository *repo = NULL;
2353 struct got_commit_object *commit = NULL;
2354 struct got_reflist_head refs;
2355 struct got_reflist_entry *re;
2356 struct got_reference *head_ref;
2357 int is_head = 0;
2358 time_t committer_time = 0, cmp_time = 0;
2359 const char *refname;
2361 *repo_age = NULL;
2362 SIMPLEQ_INIT(&refs);
2364 if (repo_ref == NULL)
2365 return NULL;
2367 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
2368 is_head = 1;
2370 if (gw_trans->gw_conf->got_show_repo_age == 0)
2371 return NULL;
2373 error = got_repo_open(&repo, dir, NULL);
2374 if (error)
2375 goto done;
2377 if (is_head)
2378 error = got_ref_list(&refs, repo, "refs/heads",
2379 got_ref_cmp_by_name, NULL);
2380 else
2381 error = got_ref_list(&refs, repo, repo_ref,
2382 got_ref_cmp_by_name, NULL);
2383 if (error)
2384 goto done;
2386 SIMPLEQ_FOREACH(re, &refs, entry) {
2387 if (is_head)
2388 refname = strdup(repo_ref);
2389 else
2390 refname = got_ref_get_name(re->ref);
2391 error = got_ref_open(&head_ref, repo, refname, 0);
2392 if (error)
2393 goto done;
2395 error = got_ref_resolve(&id, repo, head_ref);
2396 got_ref_close(head_ref);
2397 if (error)
2398 goto done;
2400 error = got_object_open_as_commit(&commit, repo, id);
2401 if (error)
2402 goto done;
2404 committer_time =
2405 got_object_commit_get_committer_time(commit);
2407 if (cmp_time < committer_time)
2408 cmp_time = committer_time;
2411 if (cmp_time != 0) {
2412 committer_time = cmp_time;
2413 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2415 done:
2416 got_ref_list_free(&refs);
2417 free(id);
2418 return error;
2421 static const struct got_error *
2422 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2424 const struct got_error *error;
2425 FILE *f = NULL;
2426 struct got_object_id *id1 = NULL, *id2 = NULL;
2427 char *label1 = NULL, *label2 = NULL, *line = NULL;
2428 int obj_type;
2429 size_t linesize = 0;
2430 ssize_t linelen;
2431 enum kcgi_err kerr = KCGI_OK;
2433 f = got_opentemp();
2434 if (f == NULL)
2435 return NULL;
2437 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
2438 if (error)
2439 goto done;
2441 if (strncmp(header->parent_id, "/dev/null", 9) != 0) {
2442 error = got_repo_match_object_id(&id1, &label1,
2443 header->parent_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2444 if (error)
2445 goto done;
2448 error = got_repo_match_object_id(&id2, &label2,
2449 header->commit_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2450 if (error)
2451 goto done;
2453 error = got_object_get_type(&obj_type, header->repo, id2);
2454 if (error)
2455 goto done;
2456 switch (obj_type) {
2457 case GOT_OBJ_TYPE_BLOB:
2458 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
2459 header->repo, f);
2460 break;
2461 case GOT_OBJ_TYPE_TREE:
2462 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
2463 header->repo, f);
2464 break;
2465 case GOT_OBJ_TYPE_COMMIT:
2466 error = got_diff_objects_as_commits(id1, id2, 3, 0,
2467 header->repo, f);
2468 break;
2469 default:
2470 error = got_error(GOT_ERR_OBJ_TYPE);
2472 if (error)
2473 goto done;
2475 if (fseek(f, 0, SEEK_SET) == -1) {
2476 error = got_ferror(f, GOT_ERR_IO);
2477 goto done;
2480 while ((linelen = getline(&line, &linesize, f)) != -1) {
2481 error = gw_colordiff_line(gw_trans, line);
2482 if (error)
2483 goto done;
2484 /* XXX: KHTML_PRETTY breaks this */
2485 kerr = khtml_puts(gw_trans->gw_html_req, line);
2486 if (kerr != KCGI_OK)
2487 goto done;
2488 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2489 if (kerr != KCGI_OK)
2490 goto done;
2492 if (linelen == -1 && ferror(f))
2493 error = got_error_from_errno("getline");
2494 done:
2495 if (f && fclose(f) == -1 && error == NULL)
2496 error = got_error_from_errno("fclose");
2497 free(line);
2498 free(label1);
2499 free(label2);
2500 free(id1);
2501 free(id2);
2503 if (error == NULL && kerr != KCGI_OK)
2504 error = gw_kcgi_error(kerr);
2505 return error;
2508 static const struct got_error *
2509 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2511 const struct got_error *error = NULL;
2512 struct got_repository *repo;
2513 const char *gitconfig_owner;
2515 *owner = NULL;
2517 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2518 return NULL;
2520 error = got_repo_open(&repo, dir, NULL);
2521 if (error)
2522 return error;
2523 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2524 if (gitconfig_owner) {
2525 *owner = strdup(gitconfig_owner);
2526 if (*owner == NULL)
2527 error = got_error_from_errno("strdup");
2529 got_repo_close(repo);
2530 return error;
2533 static const struct got_error *
2534 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2536 const struct got_error *error = NULL;
2537 FILE *f;
2538 char *d_file = NULL;
2539 unsigned int len;
2540 size_t n;
2542 *url = NULL;
2544 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2545 return got_error_from_errno("asprintf");
2547 f = fopen(d_file, "r");
2548 if (f == NULL) {
2549 if (errno != ENOENT && errno != EACCES)
2550 error = got_error_from_errno2("fopen", d_file);
2551 goto done;
2554 if (fseek(f, 0, SEEK_END) == -1) {
2555 error = got_ferror(f, GOT_ERR_IO);
2556 goto done;
2558 len = ftell(f);
2559 if (len == -1) {
2560 error = got_ferror(f, GOT_ERR_IO);
2561 goto done;
2563 if (fseek(f, 0, SEEK_SET) == -1) {
2564 error = got_ferror(f, GOT_ERR_IO);
2565 goto done;
2568 *url = calloc(len + 1, sizeof(**url));
2569 if (*url == NULL) {
2570 error = got_error_from_errno("calloc");
2571 goto done;
2574 n = fread(*url, 1, len, f);
2575 if (n == 0 && ferror(f))
2576 error = got_ferror(f, GOT_ERR_IO);
2577 done:
2578 if (f && fclose(f) == -1 && error == NULL)
2579 error = got_error_from_errno("fclose");
2580 free(d_file);
2581 return NULL;
2584 static const struct got_error *
2585 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
2586 int limit, int tag_type)
2588 const struct got_error *error = NULL;
2589 struct got_repository *repo = NULL;
2590 struct got_reflist_head refs;
2591 struct got_reflist_entry *re;
2592 char *age = NULL;
2593 char *id_str = NULL, *newline, *href_commits = NULL;
2594 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
2595 struct got_tag_object *tag = NULL;
2596 enum kcgi_err kerr = KCGI_OK;
2597 int summary_header_displayed = 0;
2599 SIMPLEQ_INIT(&refs);
2601 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2602 if (error)
2603 goto done;
2605 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
2606 if (error)
2607 goto done;
2609 SIMPLEQ_FOREACH(re, &refs, entry) {
2610 const char *refname;
2611 const char *tagger;
2612 const char *tag_commit;
2613 time_t tagger_time;
2614 struct got_object_id *id;
2615 struct got_commit_object *commit = NULL;
2617 refname = got_ref_get_name(re->ref);
2618 if (strncmp(refname, "refs/tags/", 10) != 0)
2619 continue;
2620 refname += 10;
2622 error = got_ref_resolve(&id, repo, re->ref);
2623 if (error)
2624 goto done;
2626 error = got_object_open_as_tag(&tag, repo, id);
2627 if (error) {
2628 if (error->code != GOT_ERR_OBJ_TYPE) {
2629 free(id);
2630 goto done;
2632 /* "lightweight" tag */
2633 error = got_object_open_as_commit(&commit, repo, id);
2634 if (error) {
2635 free(id);
2636 goto done;
2638 tagger = got_object_commit_get_committer(commit);
2639 tagger_time =
2640 got_object_commit_get_committer_time(commit);
2641 error = got_object_id_str(&id_str, id);
2642 free(id);
2643 if (error)
2644 goto done;
2645 } else {
2646 free(id);
2647 tagger = got_object_tag_get_tagger(tag);
2648 tagger_time = got_object_tag_get_tagger_time(tag);
2649 error = got_object_id_str(&id_str,
2650 got_object_tag_get_object_id(tag));
2651 if (error)
2652 goto done;
2654 if (error)
2655 goto done;
2657 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
2658 strlen(id_str)) != 0)
2659 continue;
2661 if (commit) {
2662 error = got_object_commit_get_logmsg(&tag_commit0,
2663 commit);
2664 if (error)
2665 goto done;
2666 got_object_commit_close(commit);
2667 } else {
2668 tag_commit0 = strdup(got_object_tag_get_message(tag));
2669 if (tag_commit0 == NULL) {
2670 error = got_error_from_errno("strdup");
2671 goto done;
2675 tag_commit = tag_commit0;
2676 while (*tag_commit == '\n')
2677 tag_commit++;
2679 switch (tag_type) {
2680 case TAGBRIEF:
2681 newline = strchr(tag_commit, '\n');
2682 if (newline)
2683 *newline = '\0';
2685 if (summary_header_displayed == 0) {
2686 kerr = khtml_attr(gw_trans->gw_html_req,
2687 KELEM_DIV, KATTR_ID,
2688 "summary_tags_title_wrapper", KATTR__MAX);
2689 if (kerr != KCGI_OK)
2690 goto done;
2691 kerr = khtml_attr(gw_trans->gw_html_req,
2692 KELEM_DIV, KATTR_ID,
2693 "summary_tags_title", KATTR__MAX);
2694 if (kerr != KCGI_OK)
2695 goto done;
2696 kerr = khtml_puts(gw_trans->gw_html_req,
2697 "Tags");
2698 if (kerr != KCGI_OK)
2699 goto done;
2700 kerr = khtml_closeelem(gw_trans->gw_html_req,
2701 2);
2702 if (kerr != KCGI_OK)
2703 goto done;
2704 kerr = khtml_attr(gw_trans->gw_html_req,
2705 KELEM_DIV, KATTR_ID,
2706 "summary_tags_content", KATTR__MAX);
2707 if (kerr != KCGI_OK)
2708 goto done;
2709 summary_header_displayed = 1;
2712 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2713 KATTR_ID, "tags_wrapper", KATTR__MAX);
2714 if (kerr != KCGI_OK)
2715 goto done;
2716 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2717 KATTR_ID, "tags_age", KATTR__MAX);
2718 if (kerr != KCGI_OK)
2719 goto done;
2720 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
2721 if (error)
2722 goto done;
2723 kerr = khtml_puts(gw_trans->gw_html_req,
2724 age ? age : "");
2725 if (kerr != KCGI_OK)
2726 goto done;
2727 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2728 if (kerr != KCGI_OK)
2729 goto done;
2730 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2731 KATTR_ID, "tags", KATTR__MAX);
2732 if (kerr != KCGI_OK)
2733 goto done;
2734 kerr = khtml_puts(gw_trans->gw_html_req, refname);
2735 if (kerr != KCGI_OK)
2736 goto done;
2737 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2738 if (kerr != KCGI_OK)
2739 goto done;
2740 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2741 KATTR_ID, "tags_name", KATTR__MAX);
2742 if (kerr != KCGI_OK)
2743 goto done;
2744 if (asprintf(&href_tag, "?path=%s&action=tag&commit=%s",
2745 gw_trans->repo_name, id_str) == -1) {
2746 error = got_error_from_errno("asprintf");
2747 goto done;
2749 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2750 KATTR_HREF, href_tag, KATTR__MAX);
2751 if (kerr != KCGI_OK)
2752 goto done;
2753 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
2754 if (kerr != KCGI_OK)
2755 goto done;
2756 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2757 if (kerr != KCGI_OK)
2758 goto done;
2760 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2761 KATTR_ID, "navs_wrapper", KATTR__MAX);
2762 if (kerr != KCGI_OK)
2763 goto done;
2764 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2765 KATTR_ID, "navs", KATTR__MAX);
2766 if (kerr != KCGI_OK)
2767 goto done;
2769 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2770 KATTR_HREF, href_tag, KATTR__MAX);
2771 if (kerr != KCGI_OK)
2772 goto done;
2773 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
2774 if (kerr != KCGI_OK)
2775 goto done;
2776 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2777 if (kerr != KCGI_OK)
2778 goto done;
2780 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2781 if (kerr != KCGI_OK)
2782 goto done;
2783 if (asprintf(&href_briefs,
2784 "?path=%s&action=briefs&commit=%s",
2785 gw_trans->repo_name, id_str) == -1) {
2786 error = got_error_from_errno("asprintf");
2787 goto done;
2789 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2790 KATTR_HREF, href_briefs, KATTR__MAX);
2791 if (kerr != KCGI_OK)
2792 goto done;
2793 kerr = khtml_puts(gw_trans->gw_html_req,
2794 "commit briefs");
2795 if (kerr != KCGI_OK)
2796 goto done;
2797 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2798 if (kerr != KCGI_OK)
2799 goto done;
2801 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2802 if (kerr != KCGI_OK)
2803 goto done;
2805 if (asprintf(&href_commits,
2806 "?path=%s&action=commits&commit=%s",
2807 gw_trans->repo_name, id_str) == -1) {
2808 error = got_error_from_errno("asprintf");
2809 goto done;
2811 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2812 KATTR_HREF, href_commits, KATTR__MAX);
2813 if (kerr != KCGI_OK)
2814 goto done;
2815 kerr = khtml_puts(gw_trans->gw_html_req,
2816 "commits");
2817 if (kerr != KCGI_OK)
2818 goto done;
2819 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2820 if (kerr != KCGI_OK)
2821 goto done;
2823 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2824 KATTR_ID, "dotted_line", KATTR__MAX);
2825 if (kerr != KCGI_OK)
2826 goto done;
2827 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2828 if (kerr != KCGI_OK)
2829 goto done;
2830 break;
2831 case TAGFULL:
2832 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2833 KATTR_ID, "tag_info_date_title", KATTR__MAX);
2834 if (kerr != KCGI_OK)
2835 goto done;
2836 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
2837 if (kerr != KCGI_OK)
2838 goto done;
2839 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2840 if (kerr != KCGI_OK)
2841 goto done;
2842 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2843 KATTR_ID, "tag_info_date", KATTR__MAX);
2844 if (kerr != KCGI_OK)
2845 goto done;
2846 error = gw_get_time_str(&age, tagger_time, TM_LONG);
2847 if (error)
2848 goto done;
2849 kerr = khtml_puts(gw_trans->gw_html_req,
2850 age ? age : "");
2851 if (kerr != KCGI_OK)
2852 goto done;
2853 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2854 if (kerr != KCGI_OK)
2855 goto done;
2857 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2858 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
2859 if (kerr != KCGI_OK)
2860 goto done;
2861 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
2862 if (kerr != KCGI_OK)
2863 goto done;
2864 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2865 if (kerr != KCGI_OK)
2866 goto done;
2867 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2868 KATTR_ID, "tag_info_date", KATTR__MAX);
2869 if (kerr != KCGI_OK)
2870 goto done;
2871 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
2872 if (kerr != KCGI_OK)
2873 goto done;
2874 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2875 if (kerr != KCGI_OK)
2876 goto done;
2878 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2879 KATTR_ID, "tag_info", KATTR__MAX);
2880 if (kerr != KCGI_OK)
2881 goto done;
2882 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
2883 if (kerr != KCGI_OK)
2884 goto done;
2885 break;
2886 default:
2887 break;
2889 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2890 if (kerr != KCGI_OK)
2891 goto done;
2893 if (limit && --limit == 0)
2894 break;
2896 if (tag)
2897 got_object_tag_close(tag);
2898 tag = NULL;
2899 free(id_str);
2900 id_str = NULL;
2901 free(age);
2902 age = NULL;
2903 free(tag_commit0);
2904 tag_commit0 = NULL;
2905 free(href_tag);
2906 href_tag = NULL;
2907 free(href_briefs);
2908 href_briefs = NULL;
2909 free(href_commits);
2910 href_commits = NULL;
2912 done:
2913 if (tag)
2914 got_object_tag_close(tag);
2915 free(id_str);
2916 free(age);
2917 free(tag_commit0);
2918 free(href_tag);
2919 free(href_briefs);
2920 free(href_commits);
2921 got_ref_list_free(&refs);
2922 if (repo)
2923 got_repo_close(repo);
2924 if (error == NULL && kerr != KCGI_OK)
2925 error = gw_kcgi_error(kerr);
2926 return error;
2929 static void
2930 gw_free_headers(struct gw_header *header)
2932 free(header->id);
2933 free(header->path);
2934 if (header->commit != NULL)
2935 got_object_commit_close(header->commit);
2936 if (header->repo)
2937 got_repo_close(header->repo);
2938 free(header->refs_str);
2939 free(header->commit_id);
2940 free(header->parent_id);
2941 free(header->tree_id);
2942 free(header->commit_msg);
2945 static struct gw_header *
2946 gw_init_header()
2948 struct gw_header *header;
2950 header = malloc(sizeof(*header));
2951 if (header == NULL)
2952 return NULL;
2954 header->repo = NULL;
2955 header->commit = NULL;
2956 header->id = NULL;
2957 header->path = NULL;
2958 SIMPLEQ_INIT(&header->refs);
2960 return header;
2963 static const struct got_error *
2964 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
2965 int limit)
2967 const struct got_error *error = NULL;
2968 struct got_commit_graph *graph = NULL;
2970 error = got_commit_graph_open(&graph, header->path, 0);
2971 if (error)
2972 goto done;
2974 error = got_commit_graph_iter_start(graph, header->id, header->repo,
2975 NULL, NULL);
2976 if (error)
2977 goto done;
2979 for (;;) {
2980 error = got_commit_graph_iter_next(&header->id, graph,
2981 header->repo, NULL, NULL);
2982 if (error) {
2983 if (error->code == GOT_ERR_ITER_COMPLETED)
2984 error = NULL;
2985 goto done;
2987 if (header->id == NULL)
2988 goto done;
2990 error = got_object_open_as_commit(&header->commit, header->repo,
2991 header->id);
2992 if (error)
2993 goto done;
2995 error = gw_get_commit(gw_trans, header);
2996 if (limit > 1) {
2997 struct gw_header *n_header = NULL;
2998 if ((n_header = gw_init_header()) == NULL) {
2999 error = got_error_from_errno("malloc");
3000 goto done;
3003 n_header->refs_str = strdup(header->refs_str);
3004 n_header->commit_id = strdup(header->commit_id);
3005 n_header->parent_id = strdup(header->parent_id);
3006 n_header->tree_id = strdup(header->tree_id);
3007 n_header->author = strdup(header->author);
3008 n_header->committer = strdup(header->committer);
3009 n_header->commit_msg = strdup(header->commit_msg);
3010 n_header->committer_time = header->committer_time;
3011 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3012 entry);
3014 if (error || (limit && --limit == 0))
3015 break;
3017 done:
3018 if (graph)
3019 got_commit_graph_close(graph);
3020 return error;
3023 static const struct got_error *
3024 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
3026 const struct got_error *error = NULL;
3027 struct got_reflist_entry *re;
3028 struct got_object_id *id2 = NULL;
3029 struct got_object_qid *parent_id;
3030 char *refs_str = NULL, *commit_msg = NULL, *commit_msg0;
3032 /*print commit*/
3033 SIMPLEQ_FOREACH(re, &header->refs, entry) {
3034 char *s;
3035 const char *name;
3036 struct got_tag_object *tag = NULL;
3037 int cmp;
3039 name = got_ref_get_name(re->ref);
3040 if (strcmp(name, GOT_REF_HEAD) == 0)
3041 continue;
3042 if (strncmp(name, "refs/", 5) == 0)
3043 name += 5;
3044 if (strncmp(name, "got/", 4) == 0)
3045 continue;
3046 if (strncmp(name, "heads/", 6) == 0)
3047 name += 6;
3048 if (strncmp(name, "remotes/", 8) == 0)
3049 name += 8;
3050 if (strncmp(name, "tags/", 5) == 0) {
3051 error = got_object_open_as_tag(&tag, header->repo,
3052 re->id);
3053 if (error) {
3054 if (error->code != GOT_ERR_OBJ_TYPE)
3055 continue;
3057 * Ref points at something other
3058 * than a tag.
3060 error = NULL;
3061 tag = NULL;
3064 cmp = got_object_id_cmp(tag ?
3065 got_object_tag_get_object_id(tag) : re->id, header->id);
3066 if (tag)
3067 got_object_tag_close(tag);
3068 if (cmp != 0)
3069 continue;
3070 s = refs_str;
3071 if (asprintf(&refs_str, "%s%s%s", s ? s : "",
3072 s ? ", " : "", name) == -1) {
3073 error = got_error_from_errno("asprintf");
3074 free(s);
3075 return error;
3077 header->refs_str = strdup(refs_str);
3078 free(s);
3081 if (refs_str == NULL)
3082 header->refs_str = strdup("");
3083 free(refs_str);
3085 error = got_object_id_str(&header->commit_id, header->id);
3086 if (error)
3087 return error;
3089 error = got_object_id_str(&header->tree_id,
3090 got_object_commit_get_tree_id(header->commit));
3091 if (error)
3092 return error;
3094 if (gw_trans->action == GW_DIFF) {
3095 parent_id = SIMPLEQ_FIRST(
3096 got_object_commit_get_parent_ids(header->commit));
3097 if (parent_id != NULL) {
3098 id2 = got_object_id_dup(parent_id->id);
3099 free (parent_id);
3100 error = got_object_id_str(&header->parent_id, id2);
3101 if (error)
3102 return error;
3103 free(id2);
3104 } else
3105 header->parent_id = strdup("/dev/null");
3106 } else
3107 header->parent_id = strdup("");
3109 header->committer_time =
3110 got_object_commit_get_committer_time(header->commit);
3112 header->author =
3113 got_object_commit_get_author(header->commit);
3114 header->committer =
3115 got_object_commit_get_committer(header->commit);
3116 if (error)
3117 return error;
3118 error = got_object_commit_get_logmsg(&commit_msg0, header->commit);
3119 if (error)
3120 return error;
3122 commit_msg = commit_msg0;
3123 while (*commit_msg == '\n')
3124 commit_msg++;
3126 header->commit_msg = strdup(commit_msg);
3127 free(commit_msg0);
3128 return error;
3131 static const struct got_error *
3132 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3134 const struct got_error *error = NULL;
3135 char *in_repo_path = NULL;
3137 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
3138 if (error)
3139 return error;
3141 if (gw_trans->commit == NULL) {
3142 struct got_reference *head_ref;
3143 error = got_ref_open(&head_ref, header->repo,
3144 gw_trans->headref, 0);
3145 if (error)
3146 return error;
3148 error = got_ref_resolve(&header->id, header->repo, head_ref);
3149 got_ref_close(head_ref);
3150 if (error)
3151 return error;
3153 error = got_object_open_as_commit(&header->commit,
3154 header->repo, header->id);
3155 } else {
3156 struct got_reference *ref;
3157 error = got_ref_open(&ref, header->repo, gw_trans->commit, 0);
3158 if (error == NULL) {
3159 int obj_type;
3160 error = got_ref_resolve(&header->id, header->repo, ref);
3161 got_ref_close(ref);
3162 if (error)
3163 return error;
3164 error = got_object_get_type(&obj_type, header->repo,
3165 header->id);
3166 if (error)
3167 return error;
3168 if (obj_type == GOT_OBJ_TYPE_TAG) {
3169 struct got_tag_object *tag;
3170 error = got_object_open_as_tag(&tag,
3171 header->repo, header->id);
3172 if (error)
3173 return error;
3174 if (got_object_tag_get_object_type(tag) !=
3175 GOT_OBJ_TYPE_COMMIT) {
3176 got_object_tag_close(tag);
3177 error = got_error(GOT_ERR_OBJ_TYPE);
3178 return error;
3180 free(header->id);
3181 header->id = got_object_id_dup(
3182 got_object_tag_get_object_id(tag));
3183 if (header->id == NULL)
3184 error = got_error_from_errno(
3185 "got_object_id_dup");
3186 got_object_tag_close(tag);
3187 if (error)
3188 return error;
3189 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3190 error = got_error(GOT_ERR_OBJ_TYPE);
3191 return error;
3193 error = got_object_open_as_commit(&header->commit,
3194 header->repo, header->id);
3195 if (error)
3196 return error;
3198 if (header->commit == NULL) {
3199 error = got_repo_match_object_id_prefix(&header->id,
3200 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
3201 header->repo);
3202 if (error)
3203 return error;
3205 error = got_repo_match_object_id_prefix(&header->id,
3206 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
3207 header->repo);
3210 error = got_repo_map_path(&in_repo_path, header->repo,
3211 gw_trans->repo_path, 1);
3212 if (error)
3213 return error;
3215 if (in_repo_path) {
3216 header->path = strdup(in_repo_path);
3218 free(in_repo_path);
3220 error = got_ref_list(&header->refs, header->repo, NULL,
3221 got_ref_cmp_by_name, NULL);
3222 if (error)
3223 return error;
3225 error = gw_get_commits(gw_trans, header, limit);
3226 return error;
3229 struct blame_line {
3230 int annotated;
3231 char *id_str;
3232 char *committer;
3233 char datebuf[11]; /* YYYY-MM-DD + NUL */
3236 struct gw_blame_cb_args {
3237 struct blame_line *lines;
3238 int nlines;
3239 int nlines_prec;
3240 int lineno_cur;
3241 off_t *line_offsets;
3242 FILE *f;
3243 struct got_repository *repo;
3244 struct gw_trans *gw_trans;
3247 static const struct got_error *
3248 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3250 const struct got_error *err = NULL;
3251 struct gw_blame_cb_args *a = arg;
3252 struct blame_line *bline;
3253 char *line = NULL;
3254 size_t linesize = 0;
3255 struct got_commit_object *commit = NULL;
3256 off_t offset;
3257 struct tm tm;
3258 time_t committer_time;
3259 enum kcgi_err kerr = KCGI_OK;
3261 if (nlines != a->nlines ||
3262 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3263 return got_error(GOT_ERR_RANGE);
3265 if (lineno == -1)
3266 return NULL; /* no change in this commit */
3268 /* Annotate this line. */
3269 bline = &a->lines[lineno - 1];
3270 if (bline->annotated)
3271 return NULL;
3272 err = got_object_id_str(&bline->id_str, id);
3273 if (err)
3274 return err;
3276 err = got_object_open_as_commit(&commit, a->repo, id);
3277 if (err)
3278 goto done;
3280 bline->committer = strdup(got_object_commit_get_committer(commit));
3281 if (bline->committer == NULL) {
3282 err = got_error_from_errno("strdup");
3283 goto done;
3286 committer_time = got_object_commit_get_committer_time(commit);
3287 if (localtime_r(&committer_time, &tm) == NULL)
3288 return got_error_from_errno("localtime_r");
3289 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3290 &tm) >= sizeof(bline->datebuf)) {
3291 err = got_error(GOT_ERR_NO_SPACE);
3292 goto done;
3294 bline->annotated = 1;
3296 /* Print lines annotated so far. */
3297 bline = &a->lines[a->lineno_cur - 1];
3298 if (!bline->annotated)
3299 goto done;
3301 offset = a->line_offsets[a->lineno_cur - 1];
3302 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3303 err = got_error_from_errno("fseeko");
3304 goto done;
3307 while (bline->annotated) {
3308 char *smallerthan, *at, *nl, *committer;
3309 char *lineno = NULL, *href_diff = NULL, *href_link = NULL;
3310 size_t len;
3312 if (getline(&line, &linesize, a->f) == -1) {
3313 if (ferror(a->f))
3314 err = got_error_from_errno("getline");
3315 break;
3318 committer = bline->committer;
3319 smallerthan = strchr(committer, '<');
3320 if (smallerthan && smallerthan[1] != '\0')
3321 committer = smallerthan + 1;
3322 at = strchr(committer, '@');
3323 if (at)
3324 *at = '\0';
3325 len = strlen(committer);
3326 if (len >= 9)
3327 committer[8] = '\0';
3329 nl = strchr(line, '\n');
3330 if (nl)
3331 *nl = '\0';
3333 if (a->gw_trans->repo_folder == NULL)
3334 a->gw_trans->repo_folder = strdup("");
3335 if (a->gw_trans->repo_folder == NULL)
3336 goto err;
3338 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3339 "blame_wrapper", KATTR__MAX);
3340 if (kerr != KCGI_OK)
3341 goto err;
3342 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3343 "blame_number", KATTR__MAX);
3344 if (kerr != KCGI_OK)
3345 goto err;
3346 if (asprintf(&lineno, "%.*d", a->nlines_prec,
3347 a->lineno_cur) == -1)
3348 goto err;
3349 kerr = khtml_puts(a->gw_trans->gw_html_req, lineno);
3350 if (kerr != KCGI_OK)
3351 goto err;
3352 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3353 if (kerr != KCGI_OK)
3354 goto err;
3356 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3357 "blame_hash", KATTR__MAX);
3358 if (kerr != KCGI_OK)
3359 goto err;
3360 if (asprintf(&href_diff,
3361 "?path=%s&action=diff&commit=%s&file=%s&folder=%s",
3362 a->gw_trans->repo_name, bline->id_str,
3363 a->gw_trans->repo_file, a->gw_trans->repo_folder) == -1) {
3364 err = got_error_from_errno("asprintf");
3365 goto err;
3367 if (asprintf(&href_link, "%.8s", bline->id_str) == -1) {
3368 err = got_error_from_errno("asprintf");
3369 goto err;
3371 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3372 KATTR_HREF, href_diff, KATTR__MAX);
3373 if (kerr != KCGI_OK)
3374 goto done;
3375 kerr = khtml_puts(a->gw_trans->gw_html_req, href_link);
3376 if (kerr != KCGI_OK)
3377 goto err;
3378 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3379 if (kerr != KCGI_OK)
3380 goto err;
3382 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3383 "blame_date", KATTR__MAX);
3384 if (kerr != KCGI_OK)
3385 goto err;
3386 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3387 if (kerr != KCGI_OK)
3388 goto err;
3389 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3390 if (kerr != KCGI_OK)
3391 goto err;
3393 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3394 "blame_author", KATTR__MAX);
3395 if (kerr != KCGI_OK)
3396 goto err;
3397 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
3398 if (kerr != KCGI_OK)
3399 goto err;
3400 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3401 if (kerr != KCGI_OK)
3402 goto err;
3404 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3405 "blame_code", KATTR__MAX);
3406 if (kerr != KCGI_OK)
3407 goto err;
3408 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
3409 if (kerr != KCGI_OK)
3410 goto err;
3411 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3412 if (kerr != KCGI_OK)
3413 goto err;
3415 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3416 if (kerr != KCGI_OK)
3417 goto err;
3419 a->lineno_cur++;
3420 bline = &a->lines[a->lineno_cur - 1];
3421 err:
3422 free(lineno);
3423 free(href_diff);
3424 free(href_link);
3426 done:
3427 if (commit)
3428 got_object_commit_close(commit);
3429 free(line);
3430 if (err == NULL && kerr != KCGI_OK)
3431 err = gw_kcgi_error(kerr);
3432 return err;
3435 static const struct got_error *
3436 gw_output_file_blame(struct gw_trans *gw_trans)
3438 const struct got_error *error = NULL;
3439 struct got_repository *repo = NULL;
3440 struct got_object_id *obj_id = NULL;
3441 struct got_object_id *commit_id = NULL;
3442 struct got_blob_object *blob = NULL;
3443 char *path = NULL, *in_repo_path = NULL;
3444 struct gw_blame_cb_args bca;
3445 int i, obj_type;
3446 size_t filesize;
3448 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3449 if (error)
3450 return error;
3452 if (asprintf(&path, "%s%s%s",
3453 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3454 gw_trans->repo_folder ? "/" : "",
3455 gw_trans->repo_file) == -1) {
3456 error = got_error_from_errno("asprintf");
3457 goto done;
3460 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3461 if (error)
3462 goto done;
3464 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3465 GOT_OBJ_TYPE_COMMIT, 1, repo);
3466 if (error)
3467 goto done;
3469 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3470 if (error)
3471 goto done;
3473 if (obj_id == NULL) {
3474 error = got_error(GOT_ERR_NO_OBJ);
3475 goto done;
3478 error = got_object_get_type(&obj_type, repo, obj_id);
3479 if (error)
3480 goto done;
3482 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3483 error = got_error(GOT_ERR_OBJ_TYPE);
3484 goto done;
3487 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3488 if (error)
3489 goto done;
3491 bca.f = got_opentemp();
3492 if (bca.f == NULL) {
3493 error = got_error_from_errno("got_opentemp");
3494 goto done;
3496 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3497 &bca.line_offsets, bca.f, blob);
3498 if (error || bca.nlines == 0)
3499 goto done;
3501 /* Don't include \n at EOF in the blame line count. */
3502 if (bca.line_offsets[bca.nlines - 1] == filesize)
3503 bca.nlines--;
3505 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3506 if (bca.lines == NULL) {
3507 error = got_error_from_errno("calloc");
3508 goto done;
3510 bca.lineno_cur = 1;
3511 bca.nlines_prec = 0;
3512 i = bca.nlines;
3513 while (i > 0) {
3514 i /= 10;
3515 bca.nlines_prec++;
3517 bca.repo = repo;
3518 bca.gw_trans = gw_trans;
3520 error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
3521 NULL, NULL);
3522 if (error)
3523 goto done;
3524 done:
3525 free(bca.line_offsets);
3526 free(in_repo_path);
3527 free(commit_id);
3528 free(obj_id);
3529 free(path);
3531 for (i = 0; i < bca.nlines; i++) {
3532 struct blame_line *bline = &bca.lines[i];
3533 free(bline->id_str);
3534 free(bline->committer);
3536 free(bca.lines);
3537 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3538 error = got_error_from_errno("fclose");
3539 if (blob)
3540 got_object_blob_close(blob);
3541 if (repo)
3542 got_repo_close(repo);
3543 return error;
3546 static const struct got_error *
3547 gw_output_blob_buf(struct gw_trans *gw_trans)
3549 const struct got_error *error = NULL;
3550 struct got_repository *repo = NULL;
3551 struct got_object_id *obj_id = NULL;
3552 struct got_object_id *commit_id = NULL;
3553 struct got_blob_object *blob = NULL;
3554 char *path = NULL, *in_repo_path = NULL;
3555 int obj_type, set_mime = 0;
3556 size_t len, hdrlen;
3557 const uint8_t *buf;
3558 enum kcgi_err kerr = KCGI_OK;
3560 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3561 if (error)
3562 return error;
3564 if (asprintf(&path, "%s%s%s",
3565 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3566 gw_trans->repo_folder ? "/" : "",
3567 gw_trans->repo_file) == -1) {
3568 error = got_error_from_errno("asprintf");
3569 goto done;
3572 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3573 if (error)
3574 goto done;
3576 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3577 GOT_OBJ_TYPE_COMMIT, 1, repo);
3578 if (error)
3579 goto done;
3581 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3582 if (error)
3583 goto done;
3585 if (obj_id == NULL) {
3586 error = got_error(GOT_ERR_NO_OBJ);
3587 goto done;
3590 error = got_object_get_type(&obj_type, repo, obj_id);
3591 if (error)
3592 goto done;
3594 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3595 error = got_error(GOT_ERR_OBJ_TYPE);
3596 goto done;
3599 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3600 if (error)
3601 goto done;
3603 hdrlen = got_object_blob_get_hdrlen(blob);
3604 do {
3605 error = got_object_blob_read_block(&len, blob);
3606 if (error)
3607 goto done;
3608 buf = got_object_blob_get_read_buf(blob);
3611 * Skip blob object header first time around,
3612 * which also contains a zero byte.
3614 buf += hdrlen;
3615 if (set_mime == 0) {
3616 if (isbinary(buf, len - hdrlen))
3617 gw_trans->mime = KMIME_APP_OCTET_STREAM;
3618 else
3619 gw_trans->mime = KMIME_TEXT_PLAIN;
3620 set_mime = 1;
3621 error = gw_display_index(gw_trans);
3622 if (error)
3623 goto done;
3625 khttp_write(gw_trans->gw_req, buf, len - hdrlen);
3626 hdrlen = 0;
3627 } while (len != 0);
3628 done:
3629 free(in_repo_path);
3630 free(commit_id);
3631 free(obj_id);
3632 free(path);
3633 if (blob)
3634 got_object_blob_close(blob);
3635 if (repo)
3636 got_repo_close(repo);
3637 if (error == NULL && kerr != KCGI_OK)
3638 error = gw_kcgi_error(kerr);
3639 return error;
3642 static const struct got_error *
3643 gw_output_repo_tree(struct gw_trans *gw_trans)
3645 const struct got_error *error = NULL;
3646 struct got_repository *repo = NULL;
3647 struct got_object_id *tree_id = NULL, *commit_id = NULL;
3648 struct got_tree_object *tree = NULL;
3649 char *path = NULL, *in_repo_path = NULL;
3650 char *id_str = NULL;
3651 char *build_folder = NULL;
3652 char *href_blob = NULL, *href_blame = NULL;
3653 const char *class = NULL;
3654 int nentries, i, class_flip = 0;
3655 enum kcgi_err kerr = KCGI_OK;
3657 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3658 if (error)
3659 goto done;
3661 if (gw_trans->repo_folder != NULL)
3662 path = strdup(gw_trans->repo_folder);
3663 else {
3664 error = got_repo_map_path(&in_repo_path, repo,
3665 gw_trans->repo_path, 1);
3666 if (error)
3667 goto done;
3668 free(path);
3669 path = in_repo_path;
3672 if (gw_trans->commit == NULL) {
3673 struct got_reference *head_ref;
3674 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
3675 if (error)
3676 goto done;
3677 error = got_ref_resolve(&commit_id, repo, head_ref);
3678 if (error)
3679 goto done;
3680 got_ref_close(head_ref);
3682 } else {
3683 error = got_repo_match_object_id(&commit_id, NULL,
3684 gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
3685 if (error)
3686 goto done;
3689 error = got_object_id_str(&gw_trans->commit, commit_id);
3690 if (error)
3691 goto done;
3693 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
3694 if (error)
3695 goto done;
3697 error = got_object_open_as_tree(&tree, repo, tree_id);
3698 if (error)
3699 goto done;
3701 nentries = got_object_tree_get_nentries(tree);
3702 for (i = 0; i < nentries; i++) {
3703 struct got_tree_entry *te;
3704 const char *modestr = "";
3705 mode_t mode;
3707 te = got_object_tree_get_entry(tree, i);
3709 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
3710 if (error)
3711 goto done;
3713 mode = got_tree_entry_get_mode(te);
3714 if (got_object_tree_entry_is_submodule(te))
3715 modestr = "$";
3716 else if (S_ISLNK(mode))
3717 modestr = "@";
3718 else if (S_ISDIR(mode))
3719 modestr = "/";
3720 else if (mode & S_IXUSR)
3721 modestr = "*";
3723 if (class_flip == 0) {
3724 class = "back_lightgray";
3725 class_flip = 1;
3726 } else {
3727 class = "back_white";
3728 class_flip = 0;
3731 if (S_ISDIR(mode)) {
3732 if (asprintf(&build_folder, "%s/%s",
3733 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3734 got_tree_entry_get_name(te)) == -1) {
3735 error = got_error_from_errno(
3736 "asprintf");
3737 goto done;
3739 if (asprintf(&href_blob,
3740 "?path=%s&action=%s&commit=%s&folder=%s",
3741 gw_trans->repo_name, gw_trans->action_name,
3742 gw_trans->commit, build_folder) == -1) {
3743 error = got_error_from_errno("asprintf");
3744 goto done;
3747 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3748 KATTR_ID, "tree_wrapper", KATTR__MAX);
3749 if (kerr != KCGI_OK)
3750 goto done;
3751 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3752 KATTR_ID, "tree_line", KATTR_CLASS, class,
3753 KATTR__MAX);
3754 if (kerr != KCGI_OK)
3755 goto done;
3756 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3757 KATTR_HREF, href_blob, KATTR_CLASS,
3758 "diff_directory", KATTR__MAX);
3759 if (kerr != KCGI_OK)
3760 goto done;
3761 kerr = khtml_puts(gw_trans->gw_html_req,
3762 got_tree_entry_get_name(te));
3763 if (kerr != KCGI_OK)
3764 goto done;
3765 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3766 if (kerr != KCGI_OK)
3767 goto done;
3768 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3769 if (kerr != KCGI_OK)
3770 goto done;
3771 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3772 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
3773 KATTR__MAX);
3774 if (kerr != KCGI_OK)
3775 goto done;
3776 kerr = khtml_entity(gw_trans->gw_html_req,
3777 KENTITY_nbsp);
3778 if (kerr != KCGI_OK)
3779 goto done;
3780 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3781 if (kerr != KCGI_OK)
3782 goto done;
3783 } else {
3784 if (asprintf(&href_blob,
3785 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3786 gw_trans->repo_name, "blob", gw_trans->commit,
3787 got_tree_entry_get_name(te),
3788 gw_trans->repo_folder ?
3789 gw_trans->repo_folder : "") == -1) {
3790 error = got_error_from_errno("asprintf");
3791 goto done;
3793 if (asprintf(&href_blame,
3794 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3795 gw_trans->repo_name, "blame", gw_trans->commit,
3796 got_tree_entry_get_name(te),
3797 gw_trans->repo_folder ?
3798 gw_trans->repo_folder : "") == -1) {
3799 error = got_error_from_errno("asprintf");
3800 goto done;
3803 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3804 KATTR_ID, "tree_wrapper", KATTR__MAX);
3805 if (kerr != KCGI_OK)
3806 goto done;
3807 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3808 KATTR_ID, "tree_line", KATTR_CLASS, class,
3809 KATTR__MAX);
3810 if (kerr != KCGI_OK)
3811 goto done;
3812 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3813 KATTR_HREF, href_blob, KATTR__MAX);
3814 if (kerr != KCGI_OK)
3815 goto done;
3816 kerr = khtml_puts(gw_trans->gw_html_req,
3817 got_tree_entry_get_name(te));
3818 if (kerr != KCGI_OK)
3819 goto done;
3820 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3821 if (kerr != KCGI_OK)
3822 goto done;
3823 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3824 if (kerr != KCGI_OK)
3825 goto done;
3826 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3827 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
3828 KATTR__MAX);
3829 if (kerr != KCGI_OK)
3830 goto done;
3832 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3833 KATTR_HREF, href_blob, KATTR__MAX);
3834 if (kerr != KCGI_OK)
3835 goto done;
3836 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
3837 if (kerr != KCGI_OK)
3838 goto done;
3839 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3840 if (kerr != KCGI_OK)
3841 goto done;
3843 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3844 if (kerr != KCGI_OK)
3845 goto done;
3847 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3848 KATTR_HREF, href_blame, KATTR__MAX);
3849 if (kerr != KCGI_OK)
3850 goto done;
3851 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
3852 if (kerr != KCGI_OK)
3853 goto done;
3855 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3856 if (kerr != KCGI_OK)
3857 goto done;
3859 free(id_str);
3860 id_str = NULL;
3861 free(href_blob);
3862 href_blob = NULL;
3863 free(build_folder);
3864 build_folder = NULL;
3866 done:
3867 if (tree)
3868 got_object_tree_close(tree);
3869 if (repo)
3870 got_repo_close(repo);
3871 free(id_str);
3872 free(href_blob);
3873 free(href_blame);
3874 free(in_repo_path);
3875 free(tree_id);
3876 free(build_folder);
3877 if (error == NULL && kerr != KCGI_OK)
3878 error = gw_kcgi_error(kerr);
3879 return error;
3882 static const struct got_error *
3883 gw_output_repo_heads(struct gw_trans *gw_trans)
3885 const struct got_error *error = NULL;
3886 struct got_repository *repo = NULL;
3887 struct got_reflist_head refs;
3888 struct got_reflist_entry *re;
3889 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
3890 char *href_commits = NULL;
3891 enum kcgi_err kerr = KCGI_OK;
3893 SIMPLEQ_INIT(&refs);
3895 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3896 if (error)
3897 goto done;
3899 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
3900 NULL);
3901 if (error)
3902 goto done;
3904 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3905 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
3906 if (kerr != KCGI_OK)
3907 goto done;
3908 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3909 KATTR_ID, "summary_heads_title", KATTR__MAX);
3910 if (kerr != KCGI_OK)
3911 goto done;
3912 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
3913 if (kerr != KCGI_OK)
3914 goto done;
3915 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3916 if (kerr != KCGI_OK)
3917 goto done;
3918 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3919 KATTR_ID, "summary_heads_content", KATTR__MAX);
3920 if (kerr != KCGI_OK)
3921 goto done;
3923 SIMPLEQ_FOREACH(re, &refs, entry) {
3924 char *refname;
3926 refname = strdup(got_ref_get_name(re->ref));
3927 if (refname == NULL) {
3928 error = got_error_from_errno("got_ref_to_str");
3929 goto done;
3932 if (strncmp(refname, "refs/heads/", 11) != 0) {
3933 free(refname);
3934 continue;
3937 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
3938 refname, TM_DIFF);
3939 if (error)
3940 goto done;
3942 if (strncmp(refname, "refs/heads/", 11) == 0)
3943 refname += 11;
3945 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3946 KATTR_ID, "heads_wrapper", KATTR__MAX);
3947 if (kerr != KCGI_OK)
3948 goto done;
3949 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3950 KATTR_ID, "heads_age", KATTR__MAX);
3951 if (kerr != KCGI_OK)
3952 goto done;
3953 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
3954 if (kerr != KCGI_OK)
3955 goto done;
3956 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3957 if (kerr != KCGI_OK)
3958 goto done;
3959 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3960 KATTR_ID, "heads_space", KATTR__MAX);
3961 if (kerr != KCGI_OK)
3962 goto done;
3963 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
3964 if (kerr != KCGI_OK)
3965 goto done;
3966 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3967 if (kerr != KCGI_OK)
3968 goto done;
3969 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3970 KATTR_ID, "head", KATTR__MAX);
3971 if (kerr != KCGI_OK)
3972 goto done;
3973 if (asprintf(&href_summary,
3974 "?path=%s&action=summary&headref=%s",
3975 gw_trans->repo_name, refname) == -1) {
3976 error = got_error_from_errno("asprintf");
3977 goto done;
3979 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
3980 href_summary, KATTR__MAX);
3981 kerr = khtml_puts(gw_trans->gw_html_req, refname);
3982 if (kerr != KCGI_OK)
3983 goto done;
3984 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3985 if (kerr != KCGI_OK)
3986 goto done;
3988 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3989 "navs_wrapper", KATTR__MAX);
3990 if (kerr != KCGI_OK)
3991 goto done;
3992 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3993 "navs", KATTR__MAX);
3994 if (kerr != KCGI_OK)
3995 goto done;
3997 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
3998 href_summary, KATTR__MAX);
3999 if (kerr != KCGI_OK)
4000 goto done;
4001 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4002 if (kerr != KCGI_OK)
4003 goto done;
4004 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4005 if (kerr != KCGI_OK)
4006 goto done;
4008 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4009 if (kerr != KCGI_OK)
4010 goto done;
4011 if (asprintf(&href_briefs, "?path=%s&action=briefs&headref=%s",
4012 gw_trans->repo_name, refname) == -1) {
4013 error = got_error_from_errno("asprintf");
4014 goto done;
4016 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4017 href_briefs, KATTR__MAX);
4018 if (kerr != KCGI_OK)
4019 goto done;
4020 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4021 if (kerr != KCGI_OK)
4022 goto done;
4023 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4024 if (kerr != KCGI_OK)
4025 goto done;
4027 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4028 if (kerr != KCGI_OK)
4029 goto done;
4031 if (asprintf(&href_commits,
4032 "?path=%s&action=commits&headref=%s",
4033 gw_trans->repo_name, refname) == -1) {
4034 error = got_error_from_errno("asprintf");
4035 goto done;
4037 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4038 href_commits, KATTR__MAX);
4039 if (kerr != KCGI_OK)
4040 goto done;
4041 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4042 if (kerr != KCGI_OK)
4043 goto done;
4044 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4045 if (kerr != KCGI_OK)
4046 goto done;
4048 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4049 "dotted_line", KATTR__MAX);
4050 if (kerr != KCGI_OK)
4051 goto done;
4052 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4053 if (kerr != KCGI_OK)
4054 goto done;
4055 break;
4056 free(href_summary);
4057 href_summary = NULL;
4058 free(href_briefs);
4059 href_briefs = NULL;
4060 free(href_commits);
4061 href_commits = NULL;
4063 done:
4064 got_ref_list_free(&refs);
4065 free(href_summary);
4066 free(href_briefs);
4067 free(href_commits);
4068 if (repo)
4069 got_repo_close(repo);
4070 return error;
4073 static const struct got_error *
4074 gw_output_site_link(struct gw_trans *gw_trans)
4076 const struct got_error *error = NULL;
4077 char *href_summary = NULL;
4078 enum kcgi_err kerr = KCGI_OK;
4080 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4081 "site_link", KATTR__MAX);
4082 if (kerr != KCGI_OK)
4083 goto done;
4084 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4085 KATTR__MAX);
4086 if (kerr != KCGI_OK)
4087 goto done;
4088 kerr = khtml_puts(gw_trans->gw_html_req,
4089 gw_trans->gw_conf->got_site_link);
4090 if (kerr != KCGI_OK)
4091 goto done;
4092 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4093 if (kerr != KCGI_OK)
4094 goto done;
4096 if (gw_trans->repo_name != NULL) {
4097 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4098 if (kerr != KCGI_OK)
4099 goto done;
4100 if (asprintf(&href_summary, "?path=%s&action=summary",
4101 gw_trans->repo_name) == -1)
4102 goto done;
4103 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4104 href_summary, KATTR__MAX);
4105 if (kerr != KCGI_OK)
4106 goto done;
4107 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4108 if (kerr != KCGI_OK)
4109 goto done;
4110 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4111 if (kerr != KCGI_OK)
4112 goto done;
4113 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4114 if (kerr != KCGI_OK)
4115 goto done;
4116 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->action_name);
4117 if (kerr != KCGI_OK)
4118 goto done;
4121 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4122 if (kerr != KCGI_OK)
4123 goto done;
4124 done:
4125 free(href_summary);
4126 return error;
4129 static const struct got_error *
4130 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4132 const struct got_error *error = NULL;
4133 char *color = NULL;
4134 enum kcgi_err kerr = KCGI_OK;
4136 if (strncmp(buf, "-", 1) == 0)
4137 color = "diff_minus";
4138 else if (strncmp(buf, "+", 1) == 0)
4139 color = "diff_plus";
4140 else if (strncmp(buf, "@@", 2) == 0)
4141 color = "diff_chunk_header";
4142 else if (strncmp(buf, "@@", 2) == 0)
4143 color = "diff_chunk_header";
4144 else if (strncmp(buf, "commit +", 8) == 0)
4145 color = "diff_meta";
4146 else if (strncmp(buf, "commit -", 8) == 0)
4147 color = "diff_meta";
4148 else if (strncmp(buf, "blob +", 6) == 0)
4149 color = "diff_meta";
4150 else if (strncmp(buf, "blob -", 6) == 0)
4151 color = "diff_meta";
4152 else if (strncmp(buf, "file +", 6) == 0)
4153 color = "diff_meta";
4154 else if (strncmp(buf, "file -", 6) == 0)
4155 color = "diff_meta";
4156 else if (strncmp(buf, "from:", 5) == 0)
4157 color = "diff_author";
4158 else if (strncmp(buf, "via:", 4) == 0)
4159 color = "diff_author";
4160 else if (strncmp(buf, "date:", 5) == 0)
4161 color = "diff_date";
4162 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4163 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4164 if (error == NULL && kerr != KCGI_OK)
4165 error = gw_kcgi_error(kerr);
4166 return error;
4169 int
4170 main(int argc, char *argv[])
4172 const struct got_error *error = NULL;
4173 struct gw_trans *gw_trans;
4174 struct gw_dir *dir = NULL, *tdir;
4175 const char *page = "index";
4176 int gw_malloc = 1;
4177 enum kcgi_err kerr;
4179 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4180 errx(1, "malloc");
4182 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4183 errx(1, "malloc");
4185 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4186 errx(1, "malloc");
4188 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4189 errx(1, "malloc");
4191 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4192 if (kerr != KCGI_OK) {
4193 error = gw_kcgi_error(kerr);
4194 goto done;
4197 if ((gw_trans->gw_conf =
4198 malloc(sizeof(struct gotweb_conf))) == NULL) {
4199 gw_malloc = 0;
4200 error = got_error_from_errno("malloc");
4201 goto done;
4204 TAILQ_INIT(&gw_trans->gw_dirs);
4205 TAILQ_INIT(&gw_trans->gw_headers);
4207 gw_trans->page = 0;
4208 gw_trans->repos_total = 0;
4209 gw_trans->repo_path = NULL;
4210 gw_trans->commit = NULL;
4211 gw_trans->headref = strdup(GOT_REF_HEAD);
4212 gw_trans->mime = KMIME_TEXT_HTML;
4213 gw_trans->gw_tmpl->key = gw_templs;
4214 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4215 gw_trans->gw_tmpl->arg = gw_trans;
4216 gw_trans->gw_tmpl->cb = gw_template;
4217 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
4218 if (error)
4219 goto done;
4221 error = gw_parse_querystring(gw_trans);
4222 if (error)
4223 goto done;
4225 if (gw_trans->action == GW_BLOB)
4226 error = gw_blob(gw_trans);
4227 else
4228 error = gw_display_index(gw_trans);
4229 done:
4230 if (error) {
4231 gw_trans->mime = KMIME_TEXT_PLAIN;
4232 gw_trans->action = GW_ERR;
4233 gw_display_error(gw_trans, error);
4235 if (gw_malloc) {
4236 free(gw_trans->gw_conf->got_repos_path);
4237 free(gw_trans->gw_conf->got_site_name);
4238 free(gw_trans->gw_conf->got_site_owner);
4239 free(gw_trans->gw_conf->got_site_link);
4240 free(gw_trans->gw_conf->got_logo);
4241 free(gw_trans->gw_conf->got_logo_url);
4242 free(gw_trans->gw_conf);
4243 free(gw_trans->commit);
4244 free(gw_trans->repo_path);
4245 free(gw_trans->repo_name);
4246 free(gw_trans->repo_file);
4247 free(gw_trans->action_name);
4248 free(gw_trans->headref);
4250 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4251 free(dir->name);
4252 free(dir->description);
4253 free(dir->age);
4254 free(dir->url);
4255 free(dir->path);
4256 free(dir);
4261 khttp_free(gw_trans->gw_req);
4262 return 0;