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 got_repository *repo;
59 struct gw_dir *gw_dir;
60 struct gotweb_conf *gw_conf;
61 struct ktemplate *gw_tmpl;
62 struct khtmlreq *gw_html_req;
63 struct kreq *gw_req;
64 const struct got_error *error;
65 const char *repo_name;
66 char *repo_path;
67 char *commit;
68 const char *repo_file;
69 char *repo_folder;
70 const char *headref;
71 unsigned int action;
72 unsigned int page;
73 unsigned int repos_total;
74 enum kmime mime;
75 };
77 struct gw_header {
78 TAILQ_ENTRY(gw_header) entry;
79 struct got_reflist_head refs;
80 char *path;
82 char *refs_str;
83 char *commit_id; /* id_str1 */
84 char *parent_id; /* id_str2 */
85 char *tree_id;
86 char *author;
87 char *committer;
88 char *commit_msg;
89 time_t committer_time;
90 };
92 struct gw_dir {
93 TAILQ_ENTRY(gw_dir) entry;
94 char *name;
95 char *owner;
96 char *description;
97 char *url;
98 char *age;
99 char *path;
100 };
102 enum gw_key {
103 KEY_ACTION,
104 KEY_COMMIT_ID,
105 KEY_FILE,
106 KEY_FOLDER,
107 KEY_HEADREF,
108 KEY_PAGE,
109 KEY_PATH,
110 KEY__ZMAX
111 };
113 enum gw_tmpl {
114 TEMPL_CONTENT,
115 TEMPL_HEAD,
116 TEMPL_HEADER,
117 TEMPL_SEARCH,
118 TEMPL_SITEPATH,
119 TEMPL_SITEOWNER,
120 TEMPL_TITLE,
121 TEMPL__MAX
122 };
124 enum gw_ref_tm {
125 TM_DIFF,
126 TM_LONG,
127 };
129 enum gw_tags {
130 TAGBRIEF,
131 TAGFULL,
132 };
134 static const char *const gw_templs[TEMPL__MAX] = {
135 "content",
136 "head",
137 "header",
138 "search",
139 "sitepath",
140 "siteowner",
141 "title",
142 };
144 static const struct kvalid gw_keys[KEY__ZMAX] = {
145 { kvalid_stringne, "action" },
146 { kvalid_stringne, "commit" },
147 { kvalid_stringne, "file" },
148 { kvalid_stringne, "folder" },
149 { kvalid_stringne, "headref" },
150 { kvalid_int, "page" },
151 { kvalid_stringne, "path" },
152 };
154 static struct gw_header *gw_init_header(void);
156 static void gw_free_header(struct gw_header *);
158 static int gw_template(size_t, void *);
160 static const struct got_error *gw_error(struct gw_trans *);
161 static const struct got_error *gw_init_gw_dir(struct gw_dir **, const char *);
162 static const struct got_error *gw_get_repo_description(char **,
163 struct gw_trans *, char *);
164 static const struct got_error *gw_get_repo_owner(char **, struct gw_trans *,
165 char *);
166 static const struct got_error *gw_get_time_str(char **, time_t, int);
167 static const struct got_error *gw_get_repo_age(char **, struct gw_trans *,
168 char *, const char *, int);
169 static const struct got_error *gw_output_file_blame(struct gw_trans *);
170 static const struct got_error *gw_output_blob_buf(struct gw_trans *);
171 static const struct got_error *gw_output_repo_tree(struct gw_trans *);
172 static const struct got_error *gw_output_diff(struct gw_trans *,
173 struct gw_header *);
174 static const struct got_error *gw_output_repo_tags(struct gw_trans *,
175 struct gw_header *, int, int);
176 static const struct got_error *gw_output_repo_heads(struct gw_trans *);
177 static const struct got_error *gw_output_site_link(struct gw_trans *);
178 static const struct got_error *gw_get_clone_url(char **, struct gw_trans *,
179 char *);
180 static const struct got_error *gw_colordiff_line(struct gw_trans *, char *);
182 static const struct got_error *gw_gen_commit_header(struct gw_trans *, char *,
183 char*);
184 static const struct got_error *gw_gen_diff_header(struct gw_trans *, char *,
185 char*);
186 static const struct got_error *gw_gen_author_header(struct gw_trans *,
187 const char *);
188 static const struct got_error *gw_gen_age_header(struct gw_trans *,
189 const char *);
190 static const struct got_error *gw_gen_committer_header(struct gw_trans *,
191 const char *);
192 static const struct got_error *gw_gen_commit_msg_header(struct gw_trans*,
193 char *);
194 static const struct got_error *gw_gen_tree_header(struct gw_trans *, char *);
195 static const struct got_error *gw_display_open(struct gw_trans *, enum khttp,
196 enum kmime);
197 static const struct got_error *gw_display_index(struct gw_trans *);
198 static const struct got_error *gw_get_header(struct gw_trans *,
199 struct gw_header *, int);
200 static const struct got_error *gw_get_commits(struct gw_trans *,
201 struct gw_header *, int,
202 struct got_object_id *);
203 static const struct got_error *gw_get_commit(struct gw_trans *,
204 struct gw_header *,
205 struct got_commit_object *,
206 struct got_object_id *);
207 static const struct got_error *gw_apply_unveil(const char *);
208 static const struct got_error *gw_blame_cb(void *, int, int,
209 struct got_object_id *);
210 static const struct got_error *gw_load_got_paths(struct gw_trans *);
211 static const struct got_error *gw_load_got_path(struct gw_trans *,
212 struct gw_dir *);
213 static const struct got_error *gw_parse_querystring(struct gw_trans *);
214 static const struct got_error *gw_blame(struct gw_trans *);
215 static const struct got_error *gw_blob(struct gw_trans *);
216 static const struct got_error *gw_diff(struct gw_trans *);
217 static const struct got_error *gw_index(struct gw_trans *);
218 static const struct got_error *gw_commits(struct gw_trans *);
219 static const struct got_error *gw_briefs(struct gw_trans *);
220 static const struct got_error *gw_summary(struct gw_trans *);
221 static const struct got_error *gw_tree(struct gw_trans *);
222 static const struct got_error *gw_tag(struct gw_trans *);
224 struct gw_query_action {
225 unsigned int func_id;
226 const char *func_name;
227 const struct got_error *(*func_main)(struct gw_trans *);
228 char *template;
229 };
231 enum gw_query_actions {
232 GW_BLAME,
233 GW_BLOB,
234 GW_BRIEFS,
235 GW_COMMITS,
236 GW_DIFF,
237 GW_ERR,
238 GW_INDEX,
239 GW_SUMMARY,
240 GW_TAG,
241 GW_TREE,
242 };
244 static struct gw_query_action gw_query_funcs[] = {
245 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
246 { GW_BLOB, "blob", NULL, NULL },
247 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
248 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
249 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
250 { GW_ERR, "error", gw_error, "gw_tmpl/err.tmpl" },
251 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
252 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
253 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
254 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
255 };
257 static const char *
258 gw_get_action_name(struct gw_trans *gw_trans)
260 return gw_query_funcs[gw_trans->action].func_name;
263 static const struct got_error *
264 gw_kcgi_error(enum kcgi_err kerr)
266 if (kerr == KCGI_OK)
267 return NULL;
269 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
270 return got_error(GOT_ERR_CANCELLED);
272 if (kerr == KCGI_ENOMEM)
273 return got_error_set_errno(ENOMEM,
274 kcgi_strerror(kerr));
276 if (kerr == KCGI_ENFILE)
277 return got_error_set_errno(ENFILE,
278 kcgi_strerror(kerr));
280 if (kerr == KCGI_EAGAIN)
281 return got_error_set_errno(EAGAIN,
282 kcgi_strerror(kerr));
284 if (kerr == KCGI_FORM)
285 return got_error_msg(GOT_ERR_IO,
286 kcgi_strerror(kerr));
288 return got_error_from_errno(kcgi_strerror(kerr));
291 static const struct got_error *
292 gw_apply_unveil(const char *repo_path)
294 const struct got_error *err;
296 if (repo_path && unveil(repo_path, "r") != 0)
297 return got_error_from_errno2("unveil", repo_path);
299 if (unveil("/tmp", "rwc") != 0)
300 return got_error_from_errno2("unveil", "/tmp");
302 err = got_privsep_unveil_exec_helpers();
303 if (err != NULL)
304 return err;
306 if (unveil(NULL, NULL) != 0)
307 return got_error_from_errno("unveil");
309 return NULL;
312 static int
313 isbinary(const uint8_t *buf, size_t n)
315 size_t i;
317 for (i = 0; i < n; i++)
318 if (buf[i] == 0)
319 return 1;
320 return 0;
323 static const struct got_error *
324 gw_blame(struct gw_trans *gw_trans)
326 const struct got_error *error = NULL;
327 struct gw_header *header = NULL;
328 char *age = NULL;
329 enum kcgi_err kerr;
331 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
332 NULL) == -1)
333 return got_error_from_errno("pledge");
335 if ((header = gw_init_header()) == NULL)
336 return got_error_from_errno("malloc");
338 error = gw_apply_unveil(gw_trans->gw_dir->path);
339 if (error)
340 goto done;
342 error = gw_get_header(gw_trans, header, 1);
343 if (error)
344 goto done;
346 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
347 "blame_header_wrapper", KATTR__MAX);
348 if (kerr != KCGI_OK)
349 goto done;
350 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
351 "blame_header", KATTR__MAX);
352 if (kerr != KCGI_OK)
353 goto done;
354 error = gw_get_time_str(&age, header->committer_time,
355 TM_LONG);
356 if (error)
357 goto done;
358 error = gw_gen_age_header(gw_trans, age ?age : "");
359 if (error)
360 goto done;
361 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
362 if (error)
363 goto done;
364 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
365 if (kerr != KCGI_OK)
366 goto done;
367 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
368 "dotted_line", KATTR__MAX);
369 if (kerr != KCGI_OK)
370 goto done;
371 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
372 if (kerr != KCGI_OK)
373 goto done;
375 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
376 "blame", KATTR__MAX);
377 if (kerr != KCGI_OK)
378 goto done;
379 error = gw_output_file_blame(gw_trans);
380 if (error)
381 goto done;
382 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
383 done:
384 gw_free_header(header);
385 if (error == NULL && kerr != KCGI_OK)
386 error = gw_kcgi_error(kerr);
387 return error;
390 static const struct got_error *
391 gw_blob(struct gw_trans *gw_trans)
393 const struct got_error *error = NULL;
394 struct gw_header *header = NULL;
396 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
397 NULL) == -1)
398 return got_error_from_errno("pledge");
400 if ((header = gw_init_header()) == NULL)
401 return got_error_from_errno("malloc");
403 error = gw_apply_unveil(gw_trans->gw_dir->path);
404 if (error)
405 goto done;
407 error = gw_get_header(gw_trans, header, 1);
408 if (error)
409 goto done;
411 error = gw_output_blob_buf(gw_trans);
412 done:
413 gw_free_header(header);
414 return error;
417 static const struct got_error *
418 gw_diff(struct gw_trans *gw_trans)
420 const struct got_error *error = NULL;
421 struct gw_header *header = NULL;
422 char *age = NULL;
423 enum kcgi_err kerr = KCGI_OK;
425 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
426 NULL) == -1)
427 return got_error_from_errno("pledge");
429 if ((header = gw_init_header()) == NULL)
430 return got_error_from_errno("malloc");
432 error = gw_apply_unveil(gw_trans->gw_dir->path);
433 if (error)
434 goto done;
436 error = gw_get_header(gw_trans, header, 1);
437 if (error)
438 goto done;
440 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
441 "diff_header_wrapper", KATTR__MAX);
442 if (kerr != KCGI_OK)
443 goto done;
444 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
445 "diff_header", KATTR__MAX);
446 if (kerr != KCGI_OK)
447 goto done;
448 error = gw_gen_diff_header(gw_trans, header->parent_id,
449 header->commit_id);
450 if (error)
451 goto done;
452 error = gw_gen_commit_header(gw_trans, header->commit_id,
453 header->refs_str);
454 if (error)
455 goto done;
456 error = gw_gen_tree_header(gw_trans, header->tree_id);
457 if (error)
458 goto done;
459 error = gw_gen_author_header(gw_trans, header->author);
460 if (error)
461 goto done;
462 error = gw_gen_committer_header(gw_trans, header->author);
463 if (error)
464 goto done;
465 error = gw_get_time_str(&age, header->committer_time,
466 TM_LONG);
467 if (error)
468 goto done;
469 error = gw_gen_age_header(gw_trans, age ?age : "");
470 if (error)
471 goto done;
472 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
473 if (error)
474 goto done;
475 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
476 if (kerr != KCGI_OK)
477 goto done;
478 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
479 "dotted_line", KATTR__MAX);
480 if (kerr != KCGI_OK)
481 goto done;
482 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
483 if (kerr != KCGI_OK)
484 goto done;
486 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
487 "diff", KATTR__MAX);
488 if (kerr != KCGI_OK)
489 goto done;
490 error = gw_output_diff(gw_trans, header);
491 if (error)
492 goto done;
494 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
495 done:
496 gw_free_header(header);
497 free(age);
498 if (error == NULL && kerr != KCGI_OK)
499 error = gw_kcgi_error(kerr);
500 return error;
503 static const struct got_error *
504 gw_index(struct gw_trans *gw_trans)
506 const struct got_error *error = NULL;
507 struct gw_dir *gw_dir = NULL;
508 char *href_next = NULL, *href_prev = NULL, *href_summary = NULL;
509 char *href_briefs = NULL, *href_commits = NULL, *href_tree = NULL;
510 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
511 enum kcgi_err kerr;
513 if (pledge("stdio rpath proc exec sendfd unveil",
514 NULL) == -1) {
515 error = got_error_from_errno("pledge");
516 return error;
519 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path);
520 if (error)
521 return error;
523 error = gw_load_got_paths(gw_trans);
524 if (error)
525 return error;
527 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
528 "index_header", KATTR__MAX);
529 if (kerr != KCGI_OK)
530 return gw_kcgi_error(kerr);
531 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
532 "index_header_project", KATTR__MAX);
533 if (kerr != KCGI_OK)
534 return gw_kcgi_error(kerr);
535 kerr = khtml_puts(gw_trans->gw_html_req, "Project");
536 if (kerr != KCGI_OK)
537 return gw_kcgi_error(kerr);
538 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
539 if (kerr != KCGI_OK)
540 return gw_kcgi_error(kerr);
542 if (gw_trans->gw_conf->got_show_repo_description) {
543 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
544 "index_header_description", KATTR__MAX);
545 if (kerr != KCGI_OK)
546 return gw_kcgi_error(kerr);
547 kerr = khtml_puts(gw_trans->gw_html_req, "Description");
548 if (kerr != KCGI_OK)
549 return gw_kcgi_error(kerr);
550 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
551 if (kerr != KCGI_OK)
552 return gw_kcgi_error(kerr);
555 if (gw_trans->gw_conf->got_show_repo_owner) {
556 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
557 "index_header_owner", KATTR__MAX);
558 if (kerr != KCGI_OK)
559 return gw_kcgi_error(kerr);
560 kerr = khtml_puts(gw_trans->gw_html_req, "Owner");
561 if (kerr != KCGI_OK)
562 return gw_kcgi_error(kerr);
563 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
564 if (kerr != KCGI_OK)
565 return gw_kcgi_error(kerr);
568 if (gw_trans->gw_conf->got_show_repo_age) {
569 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
570 "index_header_age", KATTR__MAX);
571 if (kerr != KCGI_OK)
572 return gw_kcgi_error(kerr);
573 kerr = khtml_puts(gw_trans->gw_html_req, "Last Change");
574 if (kerr != KCGI_OK)
575 return gw_kcgi_error(kerr);
576 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
577 if (kerr != KCGI_OK)
578 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);
585 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
586 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
587 "index_wrapper", KATTR__MAX);
588 if (kerr != KCGI_OK)
589 return gw_kcgi_error(kerr);
590 kerr = khtml_puts(gw_trans->gw_html_req,
591 "No repositories found in ");
592 if (kerr != KCGI_OK)
593 return gw_kcgi_error(kerr);
594 kerr = khtml_puts(gw_trans->gw_html_req,
595 gw_trans->gw_conf->got_repos_path);
596 if (kerr != KCGI_OK)
597 return gw_kcgi_error(kerr);
598 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
599 if (kerr != KCGI_OK)
600 return gw_kcgi_error(kerr);
601 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
602 "dotted_line", KATTR__MAX);
603 if (kerr != KCGI_OK)
604 return gw_kcgi_error(kerr);
605 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
606 if (kerr != KCGI_OK)
607 return gw_kcgi_error(kerr);
608 return error;
611 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
612 dir_c++;
614 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
615 if (gw_trans->page > 0 && (gw_trans->page *
616 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
617 prev_disp++;
618 continue;
621 prev_disp++;
623 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
624 "index_wrapper", KATTR__MAX);
625 if (kerr != KCGI_OK)
626 goto done;
628 if (asprintf(&href_summary, "?path=%s&action=summary",
629 gw_dir->name) == -1) {
630 error = got_error_from_errno("asprintf");
631 goto done;
633 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
634 "index_project", KATTR__MAX);
635 if (kerr != KCGI_OK)
636 goto done;
637 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
638 href_summary, KATTR__MAX);
639 if (kerr != KCGI_OK)
640 goto done;
641 kerr = khtml_puts(gw_trans->gw_html_req, gw_dir->name);
642 if (kerr != KCGI_OK)
643 goto done;
644 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
645 if (kerr != KCGI_OK)
646 goto done;
647 if (gw_trans->gw_conf->got_show_repo_description) {
648 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
649 KATTR_ID, "index_project_description", KATTR__MAX);
650 if (kerr != KCGI_OK)
651 goto done;
652 kerr = khtml_puts(gw_trans->gw_html_req,
653 gw_dir->description ? gw_dir->description : "");
654 if (kerr != KCGI_OK)
655 goto done;
656 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
657 if (kerr != KCGI_OK)
658 goto done;
660 if (gw_trans->gw_conf->got_show_repo_owner) {
661 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
662 KATTR_ID, "index_project_owner", KATTR__MAX);
663 if (kerr != KCGI_OK)
664 goto done;
665 kerr = khtml_puts(gw_trans->gw_html_req,
666 gw_dir->owner ? gw_dir->owner : "");
667 if (kerr != KCGI_OK)
668 goto done;
669 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
670 if (kerr != KCGI_OK)
671 goto done;
673 if (gw_trans->gw_conf->got_show_repo_age) {
674 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
675 KATTR_ID, "index_project_age", KATTR__MAX);
676 if (kerr != KCGI_OK)
677 goto done;
678 kerr = khtml_puts(gw_trans->gw_html_req,
679 gw_dir->age ? gw_dir->age : "");
680 if (kerr != KCGI_OK)
681 goto done;
682 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
683 if (kerr != KCGI_OK)
684 goto done;
687 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
688 "navs_wrapper", KATTR__MAX);
689 if (kerr != KCGI_OK)
690 goto done;
691 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
692 "navs", KATTR__MAX);
693 if (kerr != KCGI_OK)
694 goto done;
696 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
697 href_summary, KATTR__MAX);
698 if (kerr != KCGI_OK)
699 goto done;
700 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
701 if (kerr != KCGI_OK)
702 goto done;
703 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
704 if (kerr != KCGI_OK)
705 goto done;
707 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
708 if (kerr != KCGI_OK)
709 goto done;
711 if (asprintf(&href_briefs, "?path=%s&action=briefs",
712 gw_dir->name) == -1) {
713 error = got_error_from_errno("asprintf");
714 goto done;
716 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
717 href_briefs, KATTR__MAX);
718 if (kerr != KCGI_OK)
719 goto done;
720 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
721 if (kerr != KCGI_OK)
722 goto done;
723 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
724 if (kerr != KCGI_OK)
725 error = gw_kcgi_error(kerr);
727 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
728 if (kerr != KCGI_OK)
729 goto done;
731 if (asprintf(&href_commits, "?path=%s&action=commits",
732 gw_dir->name) == -1) {
733 error = got_error_from_errno("asprintf");
734 goto done;
736 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
737 href_commits, KATTR__MAX);
738 if (kerr != KCGI_OK)
739 goto done;
740 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
741 if (kerr != KCGI_OK)
742 goto done;
743 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
744 if (kerr != KCGI_OK)
745 goto done;
747 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
748 if (kerr != KCGI_OK)
749 goto done;
751 if (asprintf(&href_tree, "?path=%s&action=tree",
752 gw_dir->name) == -1) {
753 error = got_error_from_errno("asprintf");
754 goto done;
756 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
757 href_tree, KATTR__MAX);
758 if (kerr != KCGI_OK)
759 goto done;
760 kerr = khtml_puts(gw_trans->gw_html_req, "tree");
761 if (kerr != KCGI_OK)
762 goto done;
764 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
765 if (kerr != KCGI_OK)
766 goto done;
767 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
768 "dotted_line", KATTR__MAX);
769 if (kerr != KCGI_OK)
770 goto done;
771 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
772 if (kerr != KCGI_OK)
773 goto done;
775 free(href_summary);
776 href_summary = NULL;
777 free(href_briefs);
778 href_briefs = NULL;
779 free(href_commits);
780 href_commits = NULL;
781 free(href_tree);
782 href_tree = NULL;
784 if (gw_trans->gw_conf->got_max_repos_display == 0)
785 continue;
787 if (next_disp == gw_trans->gw_conf->got_max_repos_display) {
788 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
789 KATTR_ID, "np_wrapper", KATTR__MAX);
790 if (kerr != KCGI_OK)
791 goto done;
792 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
793 KATTR_ID, "nav_prev", KATTR__MAX);
794 if (kerr != KCGI_OK)
795 goto done;
796 } else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
797 (gw_trans->page > 0) &&
798 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
799 prev_disp == gw_trans->repos_total)) {
800 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
801 KATTR_ID, "np_wrapper", KATTR__MAX);
802 if (kerr != KCGI_OK)
803 goto done;
804 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
805 KATTR_ID, "nav_prev", KATTR__MAX);
806 if (kerr != KCGI_OK)
807 goto done;
810 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
811 (gw_trans->page > 0) &&
812 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
813 prev_disp == gw_trans->repos_total)) {
814 if (asprintf(&href_prev, "?page=%d",
815 gw_trans->page - 1) == -1) {
816 error = got_error_from_errno("asprintf");
817 goto done;
819 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
820 KATTR_HREF, href_prev, KATTR__MAX);
821 if (kerr != KCGI_OK)
822 goto done;
823 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
824 if (kerr != KCGI_OK)
825 goto done;
826 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
827 if (kerr != KCGI_OK)
828 goto done;
831 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
832 if (kerr != KCGI_OK)
833 return gw_kcgi_error(kerr);
835 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
836 next_disp == gw_trans->gw_conf->got_max_repos_display &&
837 dir_c != (gw_trans->page + 1) *
838 gw_trans->gw_conf->got_max_repos_display) {
839 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
840 KATTR_ID, "nav_next", KATTR__MAX);
841 if (kerr != KCGI_OK)
842 goto done;
843 if (asprintf(&href_next, "?page=%d",
844 gw_trans->page + 1) == -1) {
845 error = got_error_from_errno("calloc");
846 goto done;
848 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
849 KATTR_HREF, href_next, KATTR__MAX);
850 if (kerr != KCGI_OK)
851 goto done;
852 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
853 if (kerr != KCGI_OK)
854 goto done;
855 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
856 if (kerr != KCGI_OK)
857 goto done;
858 next_disp = 0;
859 break;
862 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
863 (gw_trans->page > 0) &&
864 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
865 prev_disp == gw_trans->repos_total)) {
866 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
867 if (kerr != KCGI_OK)
868 goto done;
870 next_disp++;
872 done:
873 free(href_prev);
874 free(href_next);
875 free(href_summary);
876 free(href_briefs);
877 free(href_commits);
878 free(href_tree);
879 if (error == NULL && kerr != KCGI_OK)
880 error = gw_kcgi_error(kerr);
881 return error;
884 static const struct got_error *
885 gw_commits(struct gw_trans *gw_trans)
887 const struct got_error *error = NULL;
888 struct gw_header *header = NULL, *n_header = NULL;
889 char *age = NULL;
890 char *href_diff = NULL, *href_blob = NULL;
891 enum kcgi_err kerr = KCGI_OK;
893 if ((header = gw_init_header()) == NULL)
894 return got_error_from_errno("malloc");
896 if (pledge("stdio rpath proc exec sendfd unveil",
897 NULL) == -1) {
898 error = got_error_from_errno("pledge");
899 goto done;
902 error = gw_apply_unveil(gw_trans->gw_dir->path);
903 if (error)
904 goto done;
906 error = gw_get_header(gw_trans, header,
907 gw_trans->gw_conf->got_max_commits_display);
908 if (error)
909 goto done;
911 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
912 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
913 "commits_line_wrapper", KATTR__MAX);
914 if (kerr != KCGI_OK)
915 goto done;
916 error = gw_gen_commit_header(gw_trans, n_header->commit_id,
917 n_header->refs_str);
918 if (error)
919 goto done;
920 error = gw_gen_author_header(gw_trans, n_header->author);
921 if (error)
922 goto done;
923 error = gw_gen_committer_header(gw_trans, n_header->author);
924 if (error)
925 goto done;
926 error = gw_get_time_str(&age, n_header->committer_time,
927 TM_LONG);
928 if (error)
929 goto done;
930 error = gw_gen_age_header(gw_trans, age ?age : "");
931 if (error)
932 goto done;
933 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
934 if (kerr != KCGI_OK)
935 goto done;
937 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
938 "dotted_line", KATTR__MAX);
939 if (kerr != KCGI_OK)
940 goto done;
941 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
942 if (kerr != KCGI_OK)
943 goto done;
945 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
946 "commit", KATTR__MAX);
947 if (kerr != KCGI_OK)
948 goto done;
949 kerr = khttp_puts(gw_trans->gw_req, n_header->commit_msg);
950 if (kerr != KCGI_OK)
951 goto done;
952 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
953 if (kerr != KCGI_OK)
954 goto done;
956 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
957 gw_trans->repo_name, n_header->commit_id) == -1) {
958 error = got_error_from_errno("asprintf");
959 goto done;
961 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
962 KATTR_ID, "navs_wrapper", KATTR__MAX);
963 if (kerr != KCGI_OK)
964 goto done;
965 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
966 KATTR_ID, "navs", KATTR__MAX);
967 if (kerr != KCGI_OK)
968 goto done;
969 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
970 KATTR_HREF, href_diff, KATTR__MAX);
971 if (kerr != KCGI_OK)
972 goto done;
973 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
974 if (kerr != KCGI_OK)
975 goto done;
976 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
977 if (kerr != KCGI_OK)
978 goto done;
980 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
981 if (kerr != KCGI_OK)
982 goto done;
984 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
985 gw_trans->repo_name, n_header->commit_id) == -1) {
986 error = got_error_from_errno("asprintf");
987 goto done;
989 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
990 KATTR_HREF, href_blob, KATTR__MAX);
991 if (kerr != KCGI_OK)
992 goto done;
993 khtml_puts(gw_trans->gw_html_req, "tree");
994 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
995 if (kerr != KCGI_OK)
996 goto done;
997 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
998 if (kerr != KCGI_OK)
999 goto done;
1001 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1002 "solid_line", KATTR__MAX);
1003 if (kerr != KCGI_OK)
1004 goto done;
1005 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1006 if (kerr != KCGI_OK)
1007 goto done;
1009 free(age);
1010 age = NULL;
1012 done:
1013 gw_free_header(header);
1014 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1015 gw_free_header(n_header);
1016 free(age);
1017 free(href_diff);
1018 free(href_blob);
1019 if (error == NULL && kerr != KCGI_OK)
1020 error = gw_kcgi_error(kerr);
1021 return error;
1024 static const struct got_error *
1025 gw_briefs(struct gw_trans *gw_trans)
1027 const struct got_error *error = NULL;
1028 struct gw_header *header = NULL, *n_header = NULL;
1029 char *age = NULL;
1030 char *href_diff = NULL, *href_blob = NULL;
1031 char *newline, *smallerthan;
1032 enum kcgi_err kerr = KCGI_OK;
1034 if ((header = gw_init_header()) == NULL)
1035 return got_error_from_errno("malloc");
1037 if (pledge("stdio rpath proc exec sendfd unveil",
1038 NULL) == -1) {
1039 error = got_error_from_errno("pledge");
1040 goto done;
1043 error = gw_apply_unveil(gw_trans->gw_dir->path);
1044 if (error)
1045 goto done;
1047 if (gw_trans->action == GW_SUMMARY)
1048 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
1049 else
1050 error = gw_get_header(gw_trans, header,
1051 gw_trans->gw_conf->got_max_commits_display);
1052 if (error)
1053 goto done;
1055 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
1056 error = gw_get_time_str(&age, n_header->committer_time,
1057 TM_DIFF);
1058 if (error)
1059 goto done;
1061 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1062 KATTR_ID, "briefs_wrapper", KATTR__MAX);
1063 if (kerr != KCGI_OK)
1064 goto done;
1066 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1067 KATTR_ID, "briefs_age", KATTR__MAX);
1068 if (kerr != KCGI_OK)
1069 goto done;
1070 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
1071 if (kerr != KCGI_OK)
1072 goto done;
1073 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1074 if (kerr != KCGI_OK)
1075 goto done;
1077 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1078 KATTR_ID, "briefs_author", KATTR__MAX);
1079 if (kerr != KCGI_OK)
1080 goto done;
1081 smallerthan = strchr(n_header->author, '<');
1082 if (smallerthan)
1083 *smallerthan = '\0';
1084 kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
1085 if (kerr != KCGI_OK)
1086 goto done;
1087 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1088 if (kerr != KCGI_OK)
1089 goto done;
1091 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
1092 gw_trans->repo_name, n_header->commit_id) == -1) {
1093 error = got_error_from_errno("asprintf");
1094 goto done;
1096 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1097 KATTR_ID, "briefs_log", KATTR__MAX);
1098 if (kerr != KCGI_OK)
1099 goto done;
1100 newline = strchr(n_header->commit_msg, '\n');
1101 if (newline)
1102 *newline = '\0';
1103 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1104 KATTR_HREF, href_diff, KATTR__MAX);
1105 if (kerr != KCGI_OK)
1106 goto done;
1107 kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
1108 if (kerr != KCGI_OK)
1109 goto done;
1110 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1111 if (kerr != KCGI_OK)
1112 goto done;
1114 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1115 KATTR_ID, "navs_wrapper", KATTR__MAX);
1116 if (kerr != KCGI_OK)
1117 goto done;
1118 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1119 KATTR_ID, "navs", KATTR__MAX);
1120 if (kerr != KCGI_OK)
1121 goto done;
1122 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1123 KATTR_HREF, href_diff, KATTR__MAX);
1124 if (kerr != KCGI_OK)
1125 goto done;
1126 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1127 if (kerr != KCGI_OK)
1128 goto done;
1129 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1130 if (kerr != KCGI_OK)
1131 goto done;
1133 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1134 if (kerr != KCGI_OK)
1135 goto done;
1137 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
1138 gw_trans->repo_name, n_header->commit_id) == -1) {
1139 error = got_error_from_errno("asprintf");
1140 goto done;
1142 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1143 KATTR_HREF, href_blob, KATTR__MAX);
1144 if (kerr != KCGI_OK)
1145 goto done;
1146 khtml_puts(gw_trans->gw_html_req, "tree");
1147 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
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_attr(gw_trans->gw_html_req, KELEM_DIV,
1155 KATTR_ID, "dotted_line", KATTR__MAX);
1156 if (kerr != KCGI_OK)
1157 goto done;
1158 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1159 if (kerr != KCGI_OK)
1160 goto done;
1162 free(age);
1163 age = NULL;
1164 free(href_diff);
1165 href_diff = NULL;
1166 free(href_blob);
1167 href_blob = NULL;
1169 done:
1170 gw_free_header(header);
1171 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1172 gw_free_header(n_header);
1173 free(age);
1174 free(href_diff);
1175 free(href_blob);
1176 if (error == NULL && kerr != KCGI_OK)
1177 error = gw_kcgi_error(kerr);
1178 return error;
1181 static const struct got_error *
1182 gw_summary(struct gw_trans *gw_trans)
1184 const struct got_error *error = NULL;
1185 char *age = NULL;
1186 enum kcgi_err kerr = KCGI_OK;
1188 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1189 return got_error_from_errno("pledge");
1191 /* unveil is applied with gw_briefs below */
1193 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1194 "summary_wrapper", KATTR__MAX);
1195 if (kerr != KCGI_OK)
1196 return gw_kcgi_error(kerr);
1198 if (gw_trans->gw_conf->got_show_repo_description &&
1199 gw_trans->gw_dir->description != NULL &&
1200 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1201 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1202 KATTR_ID, "description_title", KATTR__MAX);
1203 if (kerr != KCGI_OK)
1204 goto done;
1205 kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1206 if (kerr != KCGI_OK)
1207 goto done;
1208 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1209 if (kerr != KCGI_OK)
1210 goto done;
1211 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1212 KATTR_ID, "description", KATTR__MAX);
1213 if (kerr != KCGI_OK)
1214 goto done;
1215 kerr = khtml_puts(gw_trans->gw_html_req,
1216 gw_trans->gw_dir->description);
1217 if (kerr != KCGI_OK)
1218 goto done;
1219 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1220 if (kerr != KCGI_OK)
1221 goto done;
1224 if (gw_trans->gw_conf->got_show_repo_owner &&
1225 gw_trans->gw_dir->owner != NULL &&
1226 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
1227 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1228 KATTR_ID, "repo_owner_title", KATTR__MAX);
1229 if (kerr != KCGI_OK)
1230 goto done;
1231 kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1232 if (kerr != KCGI_OK)
1233 goto done;
1234 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1235 if (kerr != KCGI_OK)
1236 goto done;
1237 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1238 KATTR_ID, "repo_owner", KATTR__MAX);
1239 if (kerr != KCGI_OK)
1240 goto done;
1241 kerr = khtml_puts(gw_trans->gw_html_req,
1242 gw_trans->gw_dir->owner);
1243 if (kerr != KCGI_OK)
1244 goto done;
1245 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1246 if (kerr != KCGI_OK)
1247 goto done;
1250 if (gw_trans->gw_conf->got_show_repo_age) {
1251 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1252 NULL, TM_LONG);
1253 if (error)
1254 goto done;
1255 if (age != NULL) {
1256 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1257 KATTR_ID, "last_change_title", KATTR__MAX);
1258 if (kerr != KCGI_OK)
1259 goto done;
1260 kerr = khtml_puts(gw_trans->gw_html_req,
1261 "Last Change: ");
1262 if (kerr != KCGI_OK)
1263 goto done;
1264 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1265 if (kerr != KCGI_OK)
1266 goto done;
1267 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1268 KATTR_ID, "last_change", KATTR__MAX);
1269 if (kerr != KCGI_OK)
1270 goto done;
1271 kerr = khtml_puts(gw_trans->gw_html_req, age);
1272 if (kerr != KCGI_OK)
1273 goto done;
1274 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1275 if (kerr != KCGI_OK)
1276 goto done;
1280 if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1281 gw_trans->gw_dir->url != NULL &&
1282 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1283 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1284 KATTR_ID, "cloneurl_title", KATTR__MAX);
1285 if (kerr != KCGI_OK)
1286 goto done;
1287 kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1288 if (kerr != KCGI_OK)
1289 goto done;
1290 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1291 if (kerr != KCGI_OK)
1292 goto done;
1293 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1294 KATTR_ID, "cloneurl", KATTR__MAX);
1295 if (kerr != KCGI_OK)
1296 goto done;
1297 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1298 if (kerr != KCGI_OK)
1299 goto done;
1300 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1301 if (kerr != KCGI_OK)
1302 goto done;
1305 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1306 if (kerr != KCGI_OK)
1307 goto done;
1309 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1310 "briefs_title_wrapper", KATTR__MAX);
1311 if (kerr != KCGI_OK)
1312 goto done;
1313 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1314 "briefs_title", KATTR__MAX);
1315 if (kerr != KCGI_OK)
1316 goto done;
1317 kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1318 if (kerr != KCGI_OK)
1319 goto done;
1320 if (gw_trans->headref) {
1321 kerr = khtml_puts(gw_trans->gw_html_req, " (");
1322 if (kerr != KCGI_OK)
1323 goto done;
1324 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->headref);
1325 if (kerr != KCGI_OK)
1326 goto done;
1327 kerr = khtml_puts(gw_trans->gw_html_req, ")");
1328 if (kerr != KCGI_OK)
1329 goto done;
1331 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1332 if (kerr != KCGI_OK)
1333 goto done;
1334 error = gw_briefs(gw_trans);
1335 if (error)
1336 goto done;
1338 error = gw_output_repo_tags(gw_trans, NULL, D_MAXSLCOMMDISP,
1339 TAGBRIEF);
1340 if (error)
1341 goto done;
1343 error = gw_output_repo_heads(gw_trans);
1344 done:
1345 free(age);
1346 if (error == NULL && kerr != KCGI_OK)
1347 error = gw_kcgi_error(kerr);
1348 return error;
1351 static const struct got_error *
1352 gw_tree(struct gw_trans *gw_trans)
1354 const struct got_error *error = NULL;
1355 struct gw_header *header = NULL;
1356 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1357 char *age = NULL;
1358 enum kcgi_err kerr;
1360 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1361 return got_error_from_errno("pledge");
1363 if ((header = gw_init_header()) == NULL)
1364 return got_error_from_errno("malloc");
1366 error = gw_apply_unveil(gw_trans->gw_dir->path);
1367 if (error)
1368 goto done;
1370 error = gw_get_header(gw_trans, header, 1);
1371 if (error)
1372 goto done;
1374 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1375 "tree_header_wrapper", KATTR__MAX);
1376 if (kerr != KCGI_OK)
1377 goto done;
1378 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1379 "tree_header", KATTR__MAX);
1380 if (kerr != KCGI_OK)
1381 goto done;
1382 error = gw_gen_tree_header(gw_trans, header->tree_id);
1383 if (error)
1384 goto done;
1385 error = gw_get_time_str(&age, header->committer_time,
1386 TM_LONG);
1387 if (error)
1388 goto done;
1389 error = gw_gen_age_header(gw_trans, age ?age : "");
1390 if (error)
1391 goto done;
1392 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1393 if (error)
1394 goto done;
1395 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1396 if (kerr != KCGI_OK)
1397 goto done;
1398 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1399 "dotted_line", KATTR__MAX);
1400 if (kerr != KCGI_OK)
1401 goto done;
1402 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1403 if (kerr != KCGI_OK)
1404 goto done;
1406 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1407 "tree", KATTR__MAX);
1408 if (kerr != KCGI_OK)
1409 goto done;
1410 error = gw_output_repo_tree(gw_trans);
1411 if (error)
1412 goto done;
1414 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1415 done:
1416 gw_free_header(header);
1417 free(tree_html_disp);
1418 free(tree_html);
1419 free(tree);
1420 free(age);
1421 if (error == NULL && kerr != KCGI_OK)
1422 error = gw_kcgi_error(kerr);
1423 return error;
1426 static const struct got_error *
1427 gw_tag(struct gw_trans *gw_trans)
1429 const struct got_error *error = NULL;
1430 struct gw_header *header = NULL;
1431 enum kcgi_err kerr;
1433 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1434 return got_error_from_errno("pledge");
1436 if ((header = gw_init_header()) == NULL)
1437 return got_error_from_errno("malloc");
1439 error = gw_apply_unveil(gw_trans->gw_dir->path);
1440 if (error)
1441 goto done;
1443 error = gw_get_header(gw_trans, header, 1);
1444 if (error)
1445 goto done;
1447 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1448 "tag_header_wrapper", KATTR__MAX);
1449 if (kerr != KCGI_OK)
1450 goto done;
1451 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1452 "tag_header", KATTR__MAX);
1453 if (kerr != KCGI_OK)
1454 goto done;
1455 error = gw_gen_commit_header(gw_trans, header->commit_id,
1456 header->refs_str);
1457 if (error)
1458 goto done;
1459 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1460 if (error)
1461 goto done;
1462 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1463 if (kerr != KCGI_OK)
1464 goto done;
1465 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1466 "dotted_line", KATTR__MAX);
1467 if (kerr != KCGI_OK)
1468 goto done;
1469 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1470 if (kerr != KCGI_OK)
1471 goto done;
1473 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1474 "tree", KATTR__MAX);
1475 if (kerr != KCGI_OK)
1476 goto done;
1478 error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1479 if (error)
1480 goto done;
1482 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1483 done:
1484 gw_free_header(header);
1485 if (error == NULL && kerr != KCGI_OK)
1486 error = gw_kcgi_error(kerr);
1487 return error;
1490 static const struct got_error *
1491 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1493 const struct got_error *error = NULL;
1494 DIR *dt;
1495 char *dir_test;
1496 int opened = 0;
1498 if (asprintf(&dir_test, "%s/%s/%s",
1499 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1500 GOTWEB_GIT_DIR) == -1)
1501 return got_error_from_errno("asprintf");
1503 dt = opendir(dir_test);
1504 if (dt == NULL) {
1505 free(dir_test);
1506 } else {
1507 gw_dir->path = strdup(dir_test);
1508 if (gw_dir->path == NULL) {
1509 opened = 1;
1510 error = got_error_from_errno("strdup");
1511 goto errored;
1513 opened = 1;
1514 goto done;
1517 if (asprintf(&dir_test, "%s/%s/%s",
1518 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1519 GOTWEB_GOT_DIR) == -1) {
1520 dir_test = NULL;
1521 error = got_error_from_errno("asprintf");
1522 goto errored;
1525 dt = opendir(dir_test);
1526 if (dt == NULL)
1527 free(dir_test);
1528 else {
1529 opened = 1;
1530 error = got_error(GOT_ERR_NOT_GIT_REPO);
1531 goto errored;
1534 if (asprintf(&dir_test, "%s/%s",
1535 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1) {
1536 error = got_error_from_errno("asprintf");
1537 dir_test = NULL;
1538 goto errored;
1541 gw_dir->path = strdup(dir_test);
1542 if (gw_dir->path == NULL) {
1543 opened = 1;
1544 error = got_error_from_errno("strdup");
1545 goto errored;
1548 dt = opendir(dir_test);
1549 if (dt == NULL) {
1550 error = got_error_from_errno2("bad path", dir_test);
1551 goto errored;
1552 } else
1553 opened = 1;
1554 done:
1555 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1556 gw_dir->path);
1557 if (error)
1558 goto errored;
1559 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1560 if (error)
1561 goto errored;
1562 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1563 NULL, TM_DIFF);
1564 if (error)
1565 goto errored;
1566 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1567 errored:
1568 free(dir_test);
1569 if (opened)
1570 closedir(dt);
1571 return error;
1574 static const struct got_error *
1575 gw_load_got_paths(struct gw_trans *gw_trans)
1577 const struct got_error *error = NULL;
1578 DIR *d;
1579 struct dirent **sd_dent;
1580 struct gw_dir *gw_dir;
1581 struct stat st;
1582 unsigned int d_cnt, d_i;
1584 d = opendir(gw_trans->gw_conf->got_repos_path);
1585 if (d == NULL) {
1586 error = got_error_from_errno2("opendir",
1587 gw_trans->gw_conf->got_repos_path);
1588 return error;
1591 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1592 alphasort);
1593 if (d_cnt == -1) {
1594 error = got_error_from_errno2("scandir",
1595 gw_trans->gw_conf->got_repos_path);
1596 return error;
1599 for (d_i = 0; d_i < d_cnt; d_i++) {
1600 if (gw_trans->gw_conf->got_max_repos > 0 &&
1601 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1602 break; /* account for parent and self */
1604 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1605 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1606 continue;
1608 error = gw_init_gw_dir(&gw_dir, sd_dent[d_i]->d_name);
1609 if (error)
1610 return error;
1612 error = gw_load_got_path(gw_trans, gw_dir);
1613 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1614 error = NULL;
1615 continue;
1617 else if (error)
1618 return error;
1620 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1621 !got_path_dir_is_empty(gw_dir->path)) {
1622 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1623 entry);
1624 gw_trans->repos_total++;
1628 closedir(d);
1629 return error;
1632 static const struct got_error *
1633 gw_parse_querystring(struct gw_trans *gw_trans)
1635 const struct got_error *error = NULL;
1636 struct kpair *p;
1637 struct gw_query_action *action = NULL;
1638 unsigned int i;
1640 if (gw_trans->gw_req->fieldnmap[0]) {
1641 error = got_error_from_errno("bad parse");
1642 return error;
1643 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1644 /* define gw_trans->repo_path */
1645 gw_trans->repo_name = p->parsed.s;
1647 if (asprintf(&gw_trans->repo_path, "%s/%s",
1648 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
1649 return got_error_from_errno("asprintf");
1651 /* get action and set function */
1652 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION])) {
1653 for (i = 0; i < nitems(gw_query_funcs); i++) {
1654 action = &gw_query_funcs[i];
1655 if (action->func_name == NULL)
1656 continue;
1657 if (strcmp(action->func_name,
1658 p->parsed.s) == 0) {
1659 gw_trans->action = i;
1660 break;
1664 if (gw_trans->action == -1) {
1665 gw_trans->action = GW_ERR;
1666 gw_trans->error = got_error_from_errno("bad action");
1667 return error;
1670 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID])) {
1671 if (asprintf(&gw_trans->commit, "%s",
1672 p->parsed.s) == -1)
1673 return got_error_from_errno("asprintf");
1676 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1677 gw_trans->repo_file = p->parsed.s;
1679 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER])) {
1680 if (asprintf(&gw_trans->repo_folder, "%s",
1681 p->parsed.s) == -1)
1682 return got_error_from_errno("asprintf");
1685 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
1686 gw_trans->headref = p->parsed.s;
1688 error = gw_init_gw_dir(&gw_trans->gw_dir, gw_trans->repo_name);
1689 if (error)
1690 return error;
1692 gw_trans->error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1693 } else
1694 gw_trans->action = GW_INDEX;
1696 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1697 gw_trans->page = p->parsed.i;
1699 return error;
1702 static const struct got_error *
1703 gw_init_gw_dir(struct gw_dir **gw_dir, const char *dir)
1705 const struct got_error *error;
1707 *gw_dir = malloc(sizeof(**gw_dir));
1708 if (*gw_dir == NULL)
1709 return got_error_from_errno("malloc");
1711 if (asprintf(&(*gw_dir)->name, "%s", dir) == -1) {
1712 error = got_error_from_errno("asprintf");
1713 free(*gw_dir);
1714 *gw_dir = NULL;
1715 return NULL;
1718 return NULL;
1721 static const struct got_error *
1722 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1724 enum kcgi_err kerr;
1726 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1727 if (kerr != KCGI_OK)
1728 return gw_kcgi_error(kerr);
1729 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1730 khttps[code]);
1731 if (kerr != KCGI_OK)
1732 return gw_kcgi_error(kerr);
1733 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1734 kmimetypes[mime]);
1735 if (kerr != KCGI_OK)
1736 return gw_kcgi_error(kerr);
1737 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
1738 "nosniff");
1739 if (kerr != KCGI_OK)
1740 return gw_kcgi_error(kerr);
1741 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1742 if (kerr != KCGI_OK)
1743 return gw_kcgi_error(kerr);
1744 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
1745 "1; mode=block");
1746 if (kerr != KCGI_OK)
1747 return gw_kcgi_error(kerr);
1749 /* XXX repo_file could be NULL if not present in querystring */
1750 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
1751 kerr = khttp_head(gw_trans->gw_req,
1752 kresps[KRESP_CONTENT_DISPOSITION],
1753 "attachment; filename=%s", gw_trans->repo_file);
1754 if (kerr != KCGI_OK)
1755 return gw_kcgi_error(kerr);
1758 kerr = khttp_body(gw_trans->gw_req);
1759 return gw_kcgi_error(kerr);
1762 static const struct got_error *
1763 gw_display_index(struct gw_trans *gw_trans)
1765 const struct got_error *error;
1766 enum kcgi_err kerr;
1768 /* catch early querystring errors */
1769 if (gw_trans->error)
1770 gw_trans->action = GW_ERR;
1772 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1773 if (error)
1774 return error;
1776 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1777 if (kerr != KCGI_OK)
1778 return gw_kcgi_error(kerr);
1780 if (gw_trans->action != GW_BLOB) {
1781 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1782 gw_query_funcs[gw_trans->action].template);
1783 if (kerr != KCGI_OK) {
1784 khtml_close(gw_trans->gw_html_req);
1785 return gw_kcgi_error(kerr);
1789 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
1792 static const struct got_error *
1793 gw_error(struct gw_trans *gw_trans)
1795 enum kcgi_err kerr;
1797 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->error->msg);
1799 return gw_kcgi_error(kerr);
1802 static int
1803 gw_template(size_t key, void *arg)
1805 const struct got_error *error = NULL;
1806 enum kcgi_err kerr;
1807 struct gw_trans *gw_trans = arg;
1808 char *img_src = NULL;
1810 switch (key) {
1811 case (TEMPL_HEAD):
1812 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1813 KATTR_NAME, "viewport",
1814 KATTR_CONTENT, "initial-scale=.75, user-scalable=yes",
1815 KATTR__MAX);
1816 if (kerr != KCGI_OK)
1817 return 0;
1818 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1819 if (kerr != KCGI_OK)
1820 return 0;
1821 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1822 KATTR_CHARSET, "utf-8",
1823 KATTR__MAX);
1824 if (kerr != KCGI_OK)
1825 return 0;
1826 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1827 if (kerr != KCGI_OK)
1828 return 0;
1829 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1830 KATTR_NAME, "msapplication-TileColor",
1831 KATTR_CONTENT, "#da532c", KATTR__MAX);
1832 if (kerr != KCGI_OK)
1833 return 0;
1834 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1835 if (kerr != KCGI_OK)
1836 return 0;
1837 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1838 KATTR_NAME, "theme-color",
1839 KATTR_CONTENT, "#ffffff", KATTR__MAX);
1840 if (kerr != KCGI_OK)
1841 return 0;
1842 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1843 if (kerr != KCGI_OK)
1844 return 0;
1845 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1846 KATTR_REL, "apple-touch-icon", KATTR_SIZES, "180x180",
1847 KATTR_HREF, "/apple-touch-icon.png", KATTR__MAX);
1848 if (kerr != KCGI_OK)
1849 return 0;
1850 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1851 if (kerr != KCGI_OK)
1852 return 0;
1853 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1854 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
1855 "32x32", KATTR_HREF, "/favicon-32x32.png", KATTR__MAX);
1856 if (kerr != KCGI_OK)
1857 return 0;
1858 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1859 if (kerr != KCGI_OK)
1860 return 0;
1861 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1862 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
1863 "16x16", KATTR_HREF, "/favicon-16x16.png", KATTR__MAX);
1864 if (kerr != KCGI_OK)
1865 return 0;
1866 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1867 if (kerr != KCGI_OK)
1868 return 0;
1869 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1870 KATTR_REL, "manifest", KATTR_HREF, "/site.webmanifest",
1871 KATTR__MAX);
1872 if (kerr != KCGI_OK)
1873 return 0;
1874 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1875 if (kerr != KCGI_OK)
1876 return 0;
1877 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1878 KATTR_REL, "mask-icon", KATTR_HREF,
1879 "/safari-pinned-tab.svg", KATTR__MAX);
1880 if (kerr != KCGI_OK)
1881 return 0;
1882 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1883 if (kerr != KCGI_OK)
1884 return 0;
1885 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1886 KATTR_REL, "stylesheet", KATTR_TYPE, "text/css",
1887 KATTR_HREF, "/gotweb.css", KATTR__MAX);
1888 if (kerr != KCGI_OK)
1889 return 0;
1890 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1891 if (kerr != KCGI_OK)
1892 return 0;
1893 break;
1894 case(TEMPL_HEADER):
1895 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1896 KATTR_ID, "got_link", KATTR__MAX);
1897 if (kerr != KCGI_OK)
1898 return 0;
1899 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1900 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
1901 KATTR_TARGET, "_sotd", KATTR__MAX);
1902 if (kerr != KCGI_OK)
1903 return 0;
1904 if (asprintf(&img_src, "/%s",
1905 gw_trans->gw_conf->got_logo) == -1)
1906 return 0;
1907 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
1908 KATTR_SRC, img_src, KATTR__MAX);
1909 if (kerr != KCGI_OK) {
1910 free(img_src);
1911 return 0;
1913 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1914 if (kerr != KCGI_OK) {
1915 free(img_src);
1916 return 0;
1918 break;
1919 case (TEMPL_SITEPATH):
1920 error = gw_output_site_link(gw_trans);
1921 if (error)
1922 return 0;
1923 break;
1924 case(TEMPL_TITLE):
1925 if (gw_trans->gw_conf->got_site_name != NULL) {
1926 kerr = khtml_puts(gw_trans->gw_html_req,
1927 gw_trans->gw_conf->got_site_name);
1928 if (kerr != KCGI_OK)
1929 return 0;
1931 break;
1932 case (TEMPL_SEARCH):
1933 break;
1934 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1935 "search", KATTR__MAX);
1936 if (kerr != KCGI_OK)
1937 return 0;
1938 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
1939 KATTR_METHOD, "POST", KATTR__MAX);
1940 if (kerr != KCGI_OK)
1941 return 0;
1942 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
1943 "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
1944 KATTR_MAXLENGTH, "50", KATTR__MAX);
1945 if (kerr != KCGI_OK)
1946 return 0;
1947 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
1948 KATTR__MAX);
1949 if (kerr != KCGI_OK)
1950 return 0;
1951 kerr = khtml_puts(gw_trans->gw_html_req, "Search");
1952 if (kerr != KCGI_OK)
1953 return 0;
1954 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
1955 if (kerr != KCGI_OK)
1956 return 0;
1957 break;
1958 case(TEMPL_SITEOWNER):
1959 if (gw_trans->gw_conf->got_site_owner != NULL &&
1960 gw_trans->gw_conf->got_show_site_owner) {
1961 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1962 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
1963 if (kerr != KCGI_OK)
1964 return 0;
1965 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1966 KATTR_ID, "site_owner", KATTR__MAX);
1967 if (kerr != KCGI_OK)
1968 return 0;
1969 kerr = khtml_puts(gw_trans->gw_html_req,
1970 gw_trans->gw_conf->got_site_owner);
1971 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1972 if (kerr != KCGI_OK)
1973 return 0;
1975 break;
1976 case(TEMPL_CONTENT):
1977 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1978 if (error) {
1979 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1980 KATTR_ID, "tmpl_err", KATTR__MAX);
1981 if (kerr != KCGI_OK)
1982 return 0;
1983 kerr = khttp_puts(gw_trans->gw_req, "Error: ");
1984 if (kerr != KCGI_OK)
1985 return 0;
1986 kerr = khttp_puts(gw_trans->gw_req, error->msg);
1987 if (kerr != KCGI_OK)
1988 return 0;
1989 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1990 if (kerr != KCGI_OK)
1991 return 0;
1993 break;
1994 default:
1995 return 0;
1997 return 1;
2000 static const struct got_error *
2001 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
2003 const struct got_error *error = NULL;
2004 enum kcgi_err kerr = KCGI_OK;
2006 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2007 KATTR_ID, "header_commit_title", KATTR__MAX);
2008 if (kerr != KCGI_OK)
2009 goto done;
2010 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
2011 if (kerr != KCGI_OK)
2012 goto done;
2013 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2014 if (kerr != KCGI_OK)
2015 goto done;
2016 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2017 KATTR_ID, "header_commit", KATTR__MAX);
2018 if (kerr != KCGI_OK)
2019 goto done;
2020 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2021 if (kerr != KCGI_OK)
2022 goto done;
2023 kerr = khtml_puts(gw_trans->gw_html_req, " ");
2024 if (kerr != KCGI_OK)
2025 goto done;
2026 if (str2 != NULL) {
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, str2);
2031 if (kerr != KCGI_OK)
2032 goto done;
2033 kerr = khtml_puts(gw_trans->gw_html_req, ")");
2034 if (kerr != KCGI_OK)
2035 goto done;
2037 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2038 done:
2039 if (error == NULL && kerr != KCGI_OK)
2040 error = gw_kcgi_error(kerr);
2041 return error;
2044 static const struct got_error *
2045 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2047 const struct got_error *error = NULL;
2048 enum kcgi_err kerr = KCGI_OK;
2050 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2051 KATTR_ID, "header_diff_title", KATTR__MAX);
2052 if (kerr != KCGI_OK)
2053 goto done;
2054 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2055 if (kerr != KCGI_OK)
2056 goto done;
2057 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2058 if (kerr != KCGI_OK)
2059 goto done;
2060 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2061 KATTR_ID, "header_diff", KATTR__MAX);
2062 if (kerr != KCGI_OK)
2063 goto done;
2064 if (str1 != NULL) {
2065 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2066 if (kerr != KCGI_OK)
2067 goto done;
2069 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2070 if (kerr != KCGI_OK)
2071 goto done;
2072 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2073 if (kerr != KCGI_OK)
2074 goto done;
2075 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2076 done:
2077 if (error == NULL && kerr != KCGI_OK)
2078 error = gw_kcgi_error(kerr);
2079 return error;
2082 static const struct got_error *
2083 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2085 const struct got_error *error = NULL;
2086 enum kcgi_err kerr = KCGI_OK;
2088 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2089 KATTR_ID, "header_age_title", KATTR__MAX);
2090 if (kerr != KCGI_OK)
2091 goto done;
2092 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2093 if (kerr != KCGI_OK)
2094 goto done;
2095 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2096 if (kerr != KCGI_OK)
2097 goto done;
2098 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2099 KATTR_ID, "header_age", KATTR__MAX);
2100 if (kerr != KCGI_OK)
2101 goto done;
2102 kerr = khtml_puts(gw_trans->gw_html_req, str);
2103 if (kerr != KCGI_OK)
2104 goto done;
2105 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2106 done:
2107 if (error == NULL && kerr != KCGI_OK)
2108 error = gw_kcgi_error(kerr);
2109 return error;
2112 static const struct got_error *
2113 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2115 const struct got_error *error = NULL;
2116 enum kcgi_err kerr;
2118 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2119 KATTR_ID, "header_author_title", KATTR__MAX);
2120 if (kerr != KCGI_OK)
2121 goto done;
2122 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2123 if (kerr != KCGI_OK)
2124 goto done;
2125 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2126 if (kerr != KCGI_OK)
2127 goto done;
2128 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2129 KATTR_ID, "header_author", KATTR__MAX);
2130 if (kerr != KCGI_OK)
2131 goto done;
2132 kerr = khtml_puts(gw_trans->gw_html_req, str);
2133 if (kerr != KCGI_OK)
2134 goto done;
2135 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2136 done:
2137 if (error == NULL && kerr != KCGI_OK)
2138 error = gw_kcgi_error(kerr);
2139 return error;
2142 static const struct got_error *
2143 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2145 const struct got_error *error = NULL;
2146 enum kcgi_err kerr;
2148 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2149 KATTR_ID, "header_committer_title", KATTR__MAX);
2150 if (kerr != KCGI_OK)
2151 goto done;
2152 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2153 if (kerr != KCGI_OK)
2154 goto done;
2155 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2156 if (kerr != KCGI_OK)
2157 goto done;
2158 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2159 KATTR_ID, "header_committer", KATTR__MAX);
2160 if (kerr != KCGI_OK)
2161 goto done;
2162 kerr = khtml_puts(gw_trans->gw_html_req, str);
2163 if (kerr != KCGI_OK)
2164 goto done;
2165 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2166 done:
2167 if (error == NULL && kerr != KCGI_OK)
2168 error = gw_kcgi_error(kerr);
2169 return error;
2172 static const struct got_error *
2173 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2175 const struct got_error *error = NULL;
2176 enum kcgi_err kerr;
2178 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2179 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2180 if (kerr != KCGI_OK)
2181 goto done;
2182 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2183 if (kerr != KCGI_OK)
2184 goto done;
2185 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2186 if (kerr != KCGI_OK)
2187 goto done;
2188 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2189 KATTR_ID, "header_commit_msg", KATTR__MAX);
2190 if (kerr != KCGI_OK)
2191 goto done;
2192 kerr = khttp_puts(gw_trans->gw_req, str);
2193 if (kerr != KCGI_OK)
2194 goto done;
2195 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2196 done:
2197 if (error == NULL && kerr != KCGI_OK)
2198 error = gw_kcgi_error(kerr);
2199 return error;
2202 static const struct got_error *
2203 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2205 const struct got_error *error = NULL;
2206 enum kcgi_err kerr;
2208 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2209 KATTR_ID, "header_tree_title", KATTR__MAX);
2210 if (kerr != KCGI_OK)
2211 goto done;
2212 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2213 if (kerr != KCGI_OK)
2214 goto done;
2215 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2216 if (kerr != KCGI_OK)
2217 goto done;
2218 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2219 KATTR_ID, "header_tree", KATTR__MAX);
2220 if (kerr != KCGI_OK)
2221 goto done;
2222 kerr = khtml_puts(gw_trans->gw_html_req, str);
2223 if (kerr != KCGI_OK)
2224 goto done;
2225 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2226 done:
2227 if (error == NULL && kerr != KCGI_OK)
2228 error = gw_kcgi_error(kerr);
2229 return error;
2232 static const struct got_error *
2233 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2234 char *dir)
2236 const struct got_error *error = NULL;
2237 FILE *f = NULL;
2238 char *d_file = NULL;
2239 unsigned int len;
2240 size_t n;
2242 *description = NULL;
2243 if (gw_trans->gw_conf->got_show_repo_description == 0)
2244 return NULL;
2246 if (asprintf(&d_file, "%s/description", dir) == -1)
2247 return got_error_from_errno("asprintf");
2249 f = fopen(d_file, "r");
2250 if (f == NULL) {
2251 if (errno == ENOENT || errno == EACCES)
2252 return NULL;
2253 error = got_error_from_errno2("fopen", d_file);
2254 goto done;
2257 if (fseek(f, 0, SEEK_END) == -1) {
2258 error = got_ferror(f, GOT_ERR_IO);
2259 goto done;
2261 len = ftell(f);
2262 if (len == -1) {
2263 error = got_ferror(f, GOT_ERR_IO);
2264 goto done;
2266 if (fseek(f, 0, SEEK_SET) == -1) {
2267 error = got_ferror(f, GOT_ERR_IO);
2268 goto done;
2270 *description = calloc(len + 1, sizeof(**description));
2271 if (*description == NULL) {
2272 error = got_error_from_errno("calloc");
2273 goto done;
2276 n = fread(*description, 1, len, f);
2277 if (n == 0 && ferror(f))
2278 error = got_ferror(f, GOT_ERR_IO);
2279 done:
2280 if (f != NULL && fclose(f) == -1 && error == NULL)
2281 error = got_error_from_errno("fclose");
2282 free(d_file);
2283 return error;
2286 static const struct got_error *
2287 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2289 struct tm tm;
2290 time_t diff_time;
2291 char *years = "years ago", *months = "months ago";
2292 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2293 char *minutes = "minutes ago", *seconds = "seconds ago";
2294 char *now = "right now";
2295 char *s;
2296 char datebuf[29];
2298 *repo_age = NULL;
2300 switch (ref_tm) {
2301 case TM_DIFF:
2302 diff_time = time(NULL) - committer_time;
2303 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2304 if (asprintf(repo_age, "%lld %s",
2305 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2306 return got_error_from_errno("asprintf");
2307 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2308 if (asprintf(repo_age, "%lld %s",
2309 (diff_time / 60 / 60 / 24 / (365 / 12)),
2310 months) == -1)
2311 return got_error_from_errno("asprintf");
2312 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2313 if (asprintf(repo_age, "%lld %s",
2314 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2315 return got_error_from_errno("asprintf");
2316 } else if (diff_time > 60 * 60 * 24 * 2) {
2317 if (asprintf(repo_age, "%lld %s",
2318 (diff_time / 60 / 60 / 24), days) == -1)
2319 return got_error_from_errno("asprintf");
2320 } else if (diff_time > 60 * 60 * 2) {
2321 if (asprintf(repo_age, "%lld %s",
2322 (diff_time / 60 / 60), hours) == -1)
2323 return got_error_from_errno("asprintf");
2324 } else if (diff_time > 60 * 2) {
2325 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2326 minutes) == -1)
2327 return got_error_from_errno("asprintf");
2328 } else if (diff_time > 2) {
2329 if (asprintf(repo_age, "%lld %s", diff_time,
2330 seconds) == -1)
2331 return got_error_from_errno("asprintf");
2332 } else {
2333 if (asprintf(repo_age, "%s", now) == -1)
2334 return got_error_from_errno("asprintf");
2336 break;
2337 case TM_LONG:
2338 if (gmtime_r(&committer_time, &tm) == NULL)
2339 return got_error_from_errno("gmtime_r");
2341 s = asctime_r(&tm, datebuf);
2342 if (s == NULL)
2343 return got_error_from_errno("asctime_r");
2345 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2346 return got_error_from_errno("asprintf");
2347 break;
2349 return NULL;
2352 static const struct got_error *
2353 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2354 const char *refname, int ref_tm)
2356 const struct got_error *error = NULL;
2357 struct got_repository *repo = NULL;
2358 struct got_commit_object *commit = NULL;
2359 struct got_reflist_head refs;
2360 struct got_reflist_entry *re;
2361 time_t committer_time = 0, cmp_time = 0;
2363 *repo_age = NULL;
2364 SIMPLEQ_INIT(&refs);
2366 if (gw_trans->gw_conf->got_show_repo_age == 0)
2367 return NULL;
2369 if (gw_trans->repo)
2370 repo = gw_trans->repo;
2371 else {
2372 error = got_repo_open(&repo, dir, NULL);
2373 if (error)
2374 return error;
2377 error = got_ref_list(&refs, repo, "refs/heads",
2378 got_ref_cmp_by_name, NULL);
2379 if (error)
2380 goto done;
2383 * Find the youngest branch tip in the repository, or the age of
2384 * the a specific branch tip if a name was provided by the caller.
2386 SIMPLEQ_FOREACH(re, &refs, entry) {
2387 struct got_object_id *id = NULL;
2389 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
2390 continue;
2392 error = got_ref_resolve(&id, repo, re->ref);
2393 if (error)
2394 goto done;
2396 error = got_object_open_as_commit(&commit, repo, id);
2397 free(id);
2398 if (error)
2399 goto done;
2401 committer_time =
2402 got_object_commit_get_committer_time(commit);
2403 got_object_commit_close(commit);
2404 if (cmp_time < committer_time)
2405 cmp_time = committer_time;
2407 if (refname)
2408 break;
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 if (gw_trans->repo == NULL)
2418 got_repo_close(repo);
2419 return error;
2422 static const struct got_error *
2423 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2425 const struct got_error *error;
2426 FILE *f = NULL;
2427 struct got_object_id *id1 = NULL, *id2 = NULL;
2428 char *label1 = NULL, *label2 = NULL, *line = NULL;
2429 int obj_type;
2430 size_t linesize = 0;
2431 ssize_t linelen;
2432 enum kcgi_err kerr = KCGI_OK;
2434 f = got_opentemp();
2435 if (f == NULL)
2436 return NULL;
2438 if (header->parent_id != NULL &&
2439 strncmp(header->parent_id, "/dev/null", 9) != 0) {
2440 error = got_repo_match_object_id(&id1, &label1,
2441 header->parent_id, GOT_OBJ_TYPE_ANY, 1, gw_trans->repo);
2442 if (error)
2443 goto done;
2446 error = got_repo_match_object_id(&id2, &label2,
2447 header->commit_id, GOT_OBJ_TYPE_ANY, 1, gw_trans->repo);
2448 if (error)
2449 goto done;
2451 error = got_object_get_type(&obj_type, gw_trans->repo, id2);
2452 if (error)
2453 goto done;
2454 switch (obj_type) {
2455 case GOT_OBJ_TYPE_BLOB:
2456 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
2457 gw_trans->repo, f);
2458 break;
2459 case GOT_OBJ_TYPE_TREE:
2460 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
2461 gw_trans->repo, f);
2462 break;
2463 case GOT_OBJ_TYPE_COMMIT:
2464 error = got_diff_objects_as_commits(id1, id2, 3, 0,
2465 gw_trans->repo, f);
2466 break;
2467 default:
2468 error = got_error(GOT_ERR_OBJ_TYPE);
2470 if (error)
2471 goto done;
2473 if (fseek(f, 0, SEEK_SET) == -1) {
2474 error = got_ferror(f, GOT_ERR_IO);
2475 goto done;
2478 while ((linelen = getline(&line, &linesize, f)) != -1) {
2479 error = gw_colordiff_line(gw_trans, line);
2480 if (error)
2481 goto done;
2482 /* XXX: KHTML_PRETTY breaks this */
2483 kerr = khtml_puts(gw_trans->gw_html_req, line);
2484 if (kerr != KCGI_OK)
2485 goto done;
2486 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2487 if (kerr != KCGI_OK)
2488 goto done;
2490 if (linelen == -1 && ferror(f))
2491 error = got_error_from_errno("getline");
2492 done:
2493 if (f && fclose(f) == -1 && error == NULL)
2494 error = got_error_from_errno("fclose");
2495 free(line);
2496 free(label1);
2497 free(label2);
2498 free(id1);
2499 free(id2);
2501 if (error == NULL && kerr != KCGI_OK)
2502 error = gw_kcgi_error(kerr);
2503 return error;
2506 static const struct got_error *
2507 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2509 const struct got_error *error = NULL;
2510 struct got_repository *repo;
2511 const char *gitconfig_owner;
2513 *owner = NULL;
2515 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2516 return NULL;
2518 error = got_repo_open(&repo, dir, NULL);
2519 if (error)
2520 return error;
2521 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2522 if (gitconfig_owner) {
2523 *owner = strdup(gitconfig_owner);
2524 if (*owner == NULL)
2525 error = got_error_from_errno("strdup");
2527 got_repo_close(repo);
2528 return error;
2531 static const struct got_error *
2532 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2534 const struct got_error *error = NULL;
2535 FILE *f;
2536 char *d_file = NULL;
2537 unsigned int len;
2538 size_t n;
2540 *url = NULL;
2542 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2543 return got_error_from_errno("asprintf");
2545 f = fopen(d_file, "r");
2546 if (f == NULL) {
2547 if (errno != ENOENT && errno != EACCES)
2548 error = got_error_from_errno2("fopen", d_file);
2549 goto done;
2552 if (fseek(f, 0, SEEK_END) == -1) {
2553 error = got_ferror(f, GOT_ERR_IO);
2554 goto done;
2556 len = ftell(f);
2557 if (len == -1) {
2558 error = got_ferror(f, GOT_ERR_IO);
2559 goto done;
2561 if (fseek(f, 0, SEEK_SET) == -1) {
2562 error = got_ferror(f, GOT_ERR_IO);
2563 goto done;
2566 *url = calloc(len + 1, sizeof(**url));
2567 if (*url == NULL) {
2568 error = got_error_from_errno("calloc");
2569 goto done;
2572 n = fread(*url, 1, len, f);
2573 if (n == 0 && ferror(f))
2574 error = got_ferror(f, GOT_ERR_IO);
2575 done:
2576 if (f && fclose(f) == -1 && error == NULL)
2577 error = got_error_from_errno("fclose");
2578 free(d_file);
2579 return NULL;
2582 static const struct got_error *
2583 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
2584 int limit, int tag_type)
2586 const struct got_error *error = NULL;
2587 struct got_reflist_head refs;
2588 struct got_reflist_entry *re;
2589 char *age = NULL;
2590 char *id_str = NULL, *newline, *href_commits = NULL;
2591 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
2592 struct got_tag_object *tag = NULL;
2593 enum kcgi_err kerr = KCGI_OK;
2594 int summary_header_displayed = 0;
2596 SIMPLEQ_INIT(&refs);
2598 error = got_ref_list(&refs, gw_trans->repo, "refs/tags",
2599 got_ref_cmp_tags, gw_trans->repo);
2600 if (error)
2601 goto done;
2603 SIMPLEQ_FOREACH(re, &refs, entry) {
2604 const char *refname;
2605 const char *tagger;
2606 const char *tag_commit;
2607 time_t tagger_time;
2608 struct got_object_id *id;
2609 struct got_commit_object *commit = NULL;
2611 refname = got_ref_get_name(re->ref);
2612 if (strncmp(refname, "refs/tags/", 10) != 0)
2613 continue;
2614 refname += 10;
2616 error = got_ref_resolve(&id, gw_trans->repo, re->ref);
2617 if (error)
2618 goto done;
2620 error = got_object_open_as_tag(&tag, gw_trans->repo, id);
2621 if (error) {
2622 if (error->code != GOT_ERR_OBJ_TYPE) {
2623 free(id);
2624 goto done;
2626 /* "lightweight" tag */
2627 error = got_object_open_as_commit(&commit,
2628 gw_trans->repo, id);
2629 if (error) {
2630 free(id);
2631 goto done;
2633 tagger = got_object_commit_get_committer(commit);
2634 tagger_time =
2635 got_object_commit_get_committer_time(commit);
2636 error = got_object_id_str(&id_str, id);
2637 free(id);
2638 } else {
2639 free(id);
2640 tagger = got_object_tag_get_tagger(tag);
2641 tagger_time = got_object_tag_get_tagger_time(tag);
2642 error = got_object_id_str(&id_str,
2643 got_object_tag_get_object_id(tag));
2645 if (error)
2646 goto done;
2648 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
2649 strlen(id_str)) != 0)
2650 continue;
2652 if (commit) {
2653 error = got_object_commit_get_logmsg(&tag_commit0,
2654 commit);
2655 if (error)
2656 goto done;
2657 got_object_commit_close(commit);
2658 } else {
2659 tag_commit0 = strdup(got_object_tag_get_message(tag));
2660 if (tag_commit0 == NULL) {
2661 error = got_error_from_errno("strdup");
2662 goto done;
2666 tag_commit = tag_commit0;
2667 while (*tag_commit == '\n')
2668 tag_commit++;
2670 switch (tag_type) {
2671 case TAGBRIEF:
2672 newline = strchr(tag_commit, '\n');
2673 if (newline)
2674 *newline = '\0';
2676 if (summary_header_displayed == 0) {
2677 kerr = khtml_attr(gw_trans->gw_html_req,
2678 KELEM_DIV, KATTR_ID,
2679 "summary_tags_title_wrapper", KATTR__MAX);
2680 if (kerr != KCGI_OK)
2681 goto done;
2682 kerr = khtml_attr(gw_trans->gw_html_req,
2683 KELEM_DIV, KATTR_ID,
2684 "summary_tags_title", KATTR__MAX);
2685 if (kerr != KCGI_OK)
2686 goto done;
2687 kerr = khtml_puts(gw_trans->gw_html_req,
2688 "Tags");
2689 if (kerr != KCGI_OK)
2690 goto done;
2691 kerr = khtml_closeelem(gw_trans->gw_html_req,
2692 2);
2693 if (kerr != KCGI_OK)
2694 goto done;
2695 kerr = khtml_attr(gw_trans->gw_html_req,
2696 KELEM_DIV, KATTR_ID,
2697 "summary_tags_content", KATTR__MAX);
2698 if (kerr != KCGI_OK)
2699 goto done;
2700 summary_header_displayed = 1;
2703 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2704 KATTR_ID, "tags_wrapper", KATTR__MAX);
2705 if (kerr != KCGI_OK)
2706 goto done;
2707 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2708 KATTR_ID, "tags_age", KATTR__MAX);
2709 if (kerr != KCGI_OK)
2710 goto done;
2711 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
2712 if (error)
2713 goto done;
2714 kerr = khtml_puts(gw_trans->gw_html_req,
2715 age ? age : "");
2716 if (kerr != KCGI_OK)
2717 goto done;
2718 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2719 if (kerr != KCGI_OK)
2720 goto done;
2721 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2722 KATTR_ID, "tags", KATTR__MAX);
2723 if (kerr != KCGI_OK)
2724 goto done;
2725 kerr = khtml_puts(gw_trans->gw_html_req, refname);
2726 if (kerr != KCGI_OK)
2727 goto done;
2728 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2729 if (kerr != KCGI_OK)
2730 goto done;
2731 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2732 KATTR_ID, "tags_name", KATTR__MAX);
2733 if (kerr != KCGI_OK)
2734 goto done;
2735 if (asprintf(&href_tag, "?path=%s&action=tag&commit=%s",
2736 gw_trans->repo_name, id_str) == -1) {
2737 error = got_error_from_errno("asprintf");
2738 goto done;
2740 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2741 KATTR_HREF, href_tag, KATTR__MAX);
2742 if (kerr != KCGI_OK)
2743 goto done;
2744 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
2745 if (kerr != KCGI_OK)
2746 goto done;
2747 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2748 if (kerr != KCGI_OK)
2749 goto done;
2751 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2752 KATTR_ID, "navs_wrapper", KATTR__MAX);
2753 if (kerr != KCGI_OK)
2754 goto done;
2755 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2756 KATTR_ID, "navs", KATTR__MAX);
2757 if (kerr != KCGI_OK)
2758 goto done;
2760 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2761 KATTR_HREF, href_tag, KATTR__MAX);
2762 if (kerr != KCGI_OK)
2763 goto done;
2764 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
2765 if (kerr != KCGI_OK)
2766 goto done;
2767 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2768 if (kerr != KCGI_OK)
2769 goto done;
2771 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2772 if (kerr != KCGI_OK)
2773 goto done;
2774 if (asprintf(&href_briefs,
2775 "?path=%s&action=briefs&commit=%s",
2776 gw_trans->repo_name, id_str) == -1) {
2777 error = got_error_from_errno("asprintf");
2778 goto done;
2780 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2781 KATTR_HREF, href_briefs, KATTR__MAX);
2782 if (kerr != KCGI_OK)
2783 goto done;
2784 kerr = khtml_puts(gw_trans->gw_html_req,
2785 "commit briefs");
2786 if (kerr != KCGI_OK)
2787 goto done;
2788 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2789 if (kerr != KCGI_OK)
2790 goto done;
2792 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2793 if (kerr != KCGI_OK)
2794 goto done;
2796 if (asprintf(&href_commits,
2797 "?path=%s&action=commits&commit=%s",
2798 gw_trans->repo_name, id_str) == -1) {
2799 error = got_error_from_errno("asprintf");
2800 goto done;
2802 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2803 KATTR_HREF, href_commits, KATTR__MAX);
2804 if (kerr != KCGI_OK)
2805 goto done;
2806 kerr = khtml_puts(gw_trans->gw_html_req,
2807 "commits");
2808 if (kerr != KCGI_OK)
2809 goto done;
2810 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2811 if (kerr != KCGI_OK)
2812 goto done;
2814 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2815 KATTR_ID, "dotted_line", KATTR__MAX);
2816 if (kerr != KCGI_OK)
2817 goto done;
2818 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2819 if (kerr != KCGI_OK)
2820 goto done;
2821 break;
2822 case TAGFULL:
2823 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2824 KATTR_ID, "tag_info_date_title", KATTR__MAX);
2825 if (kerr != KCGI_OK)
2826 goto done;
2827 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
2828 if (kerr != KCGI_OK)
2829 goto done;
2830 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2831 if (kerr != KCGI_OK)
2832 goto done;
2833 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2834 KATTR_ID, "tag_info_date", KATTR__MAX);
2835 if (kerr != KCGI_OK)
2836 goto done;
2837 error = gw_get_time_str(&age, tagger_time, TM_LONG);
2838 if (error)
2839 goto done;
2840 kerr = khtml_puts(gw_trans->gw_html_req,
2841 age ? age : "");
2842 if (kerr != KCGI_OK)
2843 goto done;
2844 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2845 if (kerr != KCGI_OK)
2846 goto done;
2848 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2849 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
2850 if (kerr != KCGI_OK)
2851 goto done;
2852 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
2853 if (kerr != KCGI_OK)
2854 goto done;
2855 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2856 if (kerr != KCGI_OK)
2857 goto done;
2858 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2859 KATTR_ID, "tag_info_date", KATTR__MAX);
2860 if (kerr != KCGI_OK)
2861 goto done;
2862 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
2863 if (kerr != KCGI_OK)
2864 goto done;
2865 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2866 if (kerr != KCGI_OK)
2867 goto done;
2869 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2870 KATTR_ID, "tag_info", KATTR__MAX);
2871 if (kerr != KCGI_OK)
2872 goto done;
2873 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
2874 if (kerr != KCGI_OK)
2875 goto done;
2876 break;
2877 default:
2878 break;
2880 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2881 if (kerr != KCGI_OK)
2882 goto done;
2884 if (limit && --limit == 0)
2885 break;
2887 if (tag)
2888 got_object_tag_close(tag);
2889 tag = NULL;
2890 free(id_str);
2891 id_str = NULL;
2892 free(age);
2893 age = NULL;
2894 free(tag_commit0);
2895 tag_commit0 = NULL;
2896 free(href_tag);
2897 href_tag = NULL;
2898 free(href_briefs);
2899 href_briefs = NULL;
2900 free(href_commits);
2901 href_commits = NULL;
2903 done:
2904 if (tag)
2905 got_object_tag_close(tag);
2906 free(id_str);
2907 free(age);
2908 free(tag_commit0);
2909 free(href_tag);
2910 free(href_briefs);
2911 free(href_commits);
2912 got_ref_list_free(&refs);
2913 if (error == NULL && kerr != KCGI_OK)
2914 error = gw_kcgi_error(kerr);
2915 return error;
2918 static void
2919 gw_free_header(struct gw_header *header)
2921 free(header->path);
2922 free(header->author);
2923 free(header->committer);
2924 free(header->refs_str);
2925 free(header->commit_id);
2926 free(header->parent_id);
2927 free(header->tree_id);
2928 free(header->commit_msg);
2931 static struct gw_header *
2932 gw_init_header()
2934 struct gw_header *header;
2936 header = malloc(sizeof(*header));
2937 if (header == NULL)
2938 return NULL;
2940 header->path = NULL;
2941 SIMPLEQ_INIT(&header->refs);
2943 header->refs_str = NULL;
2944 header->commit_id = NULL;
2945 header->committer = NULL;
2946 header->author = NULL;
2947 header->parent_id = NULL;
2948 header->tree_id = NULL;
2949 header->commit_msg = NULL;
2951 return header;
2954 static const struct got_error *
2955 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
2956 int limit, struct got_object_id *id)
2958 const struct got_error *error = NULL;
2959 struct got_commit_graph *graph = NULL;
2960 struct got_commit_object *commit = NULL;
2962 error = got_commit_graph_open(&graph, header->path, 0);
2963 if (error)
2964 return error;
2966 error = got_commit_graph_iter_start(graph, id, gw_trans->repo, NULL,
2967 NULL);
2968 if (error)
2969 goto done;
2971 for (;;) {
2972 error = got_commit_graph_iter_next(&id, graph, gw_trans->repo,
2973 NULL, NULL);
2974 if (error) {
2975 if (error->code == GOT_ERR_ITER_COMPLETED)
2976 error = NULL;
2977 goto done;
2979 if (id == NULL)
2980 goto done;
2982 error = got_object_open_as_commit(&commit, gw_trans->repo, id);
2983 if (error)
2984 goto done;
2985 if (limit == 1) {
2986 error = gw_get_commit(gw_trans, header, commit, id);
2987 if (error)
2988 goto done;
2989 } else {
2990 struct gw_header *n_header = NULL;
2991 if ((n_header = gw_init_header()) == NULL) {
2992 error = got_error_from_errno("malloc");
2993 goto done;
2995 error = gw_get_commit(gw_trans, n_header, commit, id);
2996 if (error)
2997 goto done;
2998 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
2999 entry);
3001 if (error || (limit && --limit == 0))
3002 break;
3004 done:
3005 if (commit != NULL)
3006 got_object_commit_close(commit);
3007 if (graph)
3008 got_commit_graph_close(graph);
3009 return error;
3012 static const struct got_error *
3013 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header,
3014 struct got_commit_object *commit, struct got_object_id *id)
3016 const struct got_error *error = NULL;
3017 struct got_reflist_entry *re;
3018 struct got_object_id *id2 = NULL;
3019 struct got_object_qid *parent_id;
3020 char *commit_msg = NULL, *commit_msg0;
3022 /*print commit*/
3023 SIMPLEQ_FOREACH(re, &header->refs, entry) {
3024 char *s;
3025 const char *name;
3026 struct got_tag_object *tag = NULL;
3027 int cmp;
3029 if (got_ref_is_symbolic(re->ref))
3030 continue;
3032 name = got_ref_get_name(re->ref);
3033 if (strncmp(name, "refs/", 5) == 0)
3034 name += 5;
3035 if (strncmp(name, "got/", 4) == 0)
3036 continue;
3037 if (strncmp(name, "heads/", 6) == 0)
3038 name += 6;
3039 if (strncmp(name, "remotes/", 8) == 0)
3040 name += 8;
3041 if (strncmp(name, "tags/", 5) == 0) {
3042 error = got_object_open_as_tag(&tag, gw_trans->repo,
3043 re->id);
3044 if (error) {
3045 if (error->code != GOT_ERR_OBJ_TYPE)
3046 continue;
3048 * Ref points at something other
3049 * than a tag.
3051 error = NULL;
3052 tag = NULL;
3055 cmp = got_object_id_cmp(tag ?
3056 got_object_tag_get_object_id(tag) : re->id, id);
3057 if (tag)
3058 got_object_tag_close(tag);
3059 if (cmp != 0)
3060 continue;
3061 s = header->refs_str;
3062 if (asprintf(&header->refs_str, "%s%s%s", s ? s : "",
3063 s ? ", " : "", name) == -1) {
3064 error = got_error_from_errno("asprintf");
3065 free(s);
3066 header->refs_str = NULL;
3067 return error;
3069 free(s);
3072 error = got_object_id_str(&header->commit_id, id);
3073 if (error)
3074 return error;
3076 error = got_object_id_str(&header->tree_id,
3077 got_object_commit_get_tree_id(commit));
3078 if (error)
3079 return error;
3081 if (gw_trans->action == GW_DIFF) {
3082 parent_id = SIMPLEQ_FIRST(
3083 got_object_commit_get_parent_ids(commit));
3084 if (parent_id != NULL) {
3085 id2 = got_object_id_dup(parent_id->id);
3086 free (parent_id);
3087 error = got_object_id_str(&header->parent_id, id2);
3088 if (error)
3089 return error;
3090 free(id2);
3091 } else {
3092 header->parent_id = strdup("/dev/null");
3093 if (header->parent_id == NULL) {
3094 error = got_error_from_errno("strdup");
3095 return error;
3100 header->committer_time =
3101 got_object_commit_get_committer_time(commit);
3103 header->author =
3104 strdup(got_object_commit_get_author(commit));
3105 if (header->author == NULL) {
3106 error = got_error_from_errno("strdup");
3107 return error;
3109 header->committer =
3110 strdup(got_object_commit_get_committer(commit));
3111 if (header->committer == NULL) {
3112 error = got_error_from_errno("strdup");
3113 return error;
3115 error = got_object_commit_get_logmsg(&commit_msg0, commit);
3116 if (error)
3117 return error;
3119 commit_msg = commit_msg0;
3120 while (*commit_msg == '\n')
3121 commit_msg++;
3123 header->commit_msg = strdup(commit_msg);
3124 if (header->commit_msg == NULL)
3125 error = got_error_from_errno("strdup");
3126 free(commit_msg0);
3127 return error;
3130 static const struct got_error *
3131 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3133 const struct got_error *error = NULL;
3134 char *in_repo_path = NULL;
3135 struct got_object_id *id = NULL;
3137 error = got_repo_open(&gw_trans->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, gw_trans->repo,
3144 gw_trans->headref, 0);
3145 if (error)
3146 return error;
3148 error = got_ref_resolve(&id, gw_trans->repo, head_ref);
3149 got_ref_close(head_ref);
3150 if (error)
3151 return error;
3152 } else {
3153 struct got_reference *ref;
3154 error = got_ref_open(&ref, gw_trans->repo, gw_trans->commit, 0);
3155 if (error == NULL) {
3156 int obj_type;
3157 error = got_ref_resolve(&id, gw_trans->repo, ref);
3158 got_ref_close(ref);
3159 if (error)
3160 return error;
3161 error = got_object_get_type(&obj_type, gw_trans->repo,
3162 id);
3163 if (error)
3164 goto done;
3165 if (obj_type == GOT_OBJ_TYPE_TAG) {
3166 struct got_tag_object *tag;
3167 error = got_object_open_as_tag(&tag,
3168 gw_trans->repo, id);
3169 if (error)
3170 goto done;
3171 if (got_object_tag_get_object_type(tag) !=
3172 GOT_OBJ_TYPE_COMMIT) {
3173 got_object_tag_close(tag);
3174 error = got_error(GOT_ERR_OBJ_TYPE);
3175 goto done;
3177 free(id);
3178 id = got_object_id_dup(
3179 got_object_tag_get_object_id(tag));
3180 if (id == NULL)
3181 error = got_error_from_errno(
3182 "got_object_id_dup");
3183 got_object_tag_close(tag);
3184 if (error)
3185 goto done;
3186 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3187 error = got_error(GOT_ERR_OBJ_TYPE);
3188 goto done;
3191 error = got_repo_match_object_id_prefix(&id,
3192 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
3193 gw_trans->repo);
3194 if (error)
3195 goto done;
3198 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
3199 gw_trans->repo_path, 1);
3200 if (error)
3201 goto done;
3203 if (in_repo_path) {
3204 header->path = strdup(in_repo_path);
3205 if (header->path == NULL) {
3206 error = got_error_from_errno("strdup");
3207 goto done;
3211 error = got_ref_list(&header->refs, gw_trans->repo, NULL,
3212 got_ref_cmp_by_name, NULL);
3213 if (error)
3214 goto done;
3216 error = gw_get_commits(gw_trans, header, limit, id);
3217 done:
3218 got_ref_list_free(&header->refs);
3219 free(id);
3220 free(in_repo_path);
3221 return error;
3224 struct blame_line {
3225 int annotated;
3226 char *id_str;
3227 char *committer;
3228 char datebuf[11]; /* YYYY-MM-DD + NUL */
3231 struct gw_blame_cb_args {
3232 struct blame_line *lines;
3233 int nlines;
3234 int nlines_prec;
3235 int lineno_cur;
3236 off_t *line_offsets;
3237 FILE *f;
3238 struct got_repository *repo;
3239 struct gw_trans *gw_trans;
3242 static const struct got_error *
3243 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3245 const struct got_error *err = NULL;
3246 struct gw_blame_cb_args *a = arg;
3247 struct blame_line *bline;
3248 char *line = NULL;
3249 size_t linesize = 0;
3250 struct got_commit_object *commit = NULL;
3251 off_t offset;
3252 struct tm tm;
3253 time_t committer_time;
3254 enum kcgi_err kerr = KCGI_OK;
3256 if (nlines != a->nlines ||
3257 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3258 return got_error(GOT_ERR_RANGE);
3260 if (lineno == -1)
3261 return NULL; /* no change in this commit */
3263 /* Annotate this line. */
3264 bline = &a->lines[lineno - 1];
3265 if (bline->annotated)
3266 return NULL;
3267 err = got_object_id_str(&bline->id_str, id);
3268 if (err)
3269 return err;
3271 err = got_object_open_as_commit(&commit, a->repo, id);
3272 if (err)
3273 goto done;
3275 bline->committer = strdup(got_object_commit_get_committer(commit));
3276 if (bline->committer == NULL) {
3277 err = got_error_from_errno("strdup");
3278 goto done;
3281 committer_time = got_object_commit_get_committer_time(commit);
3282 if (localtime_r(&committer_time, &tm) == NULL)
3283 return got_error_from_errno("localtime_r");
3284 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3285 &tm) >= sizeof(bline->datebuf)) {
3286 err = got_error(GOT_ERR_NO_SPACE);
3287 goto done;
3289 bline->annotated = 1;
3291 /* Print lines annotated so far. */
3292 bline = &a->lines[a->lineno_cur - 1];
3293 if (!bline->annotated)
3294 goto done;
3296 offset = a->line_offsets[a->lineno_cur - 1];
3297 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3298 err = got_error_from_errno("fseeko");
3299 goto done;
3302 while (bline->annotated) {
3303 char *smallerthan, *at, *nl, *committer;
3304 char *lineno = NULL, *href_diff = NULL, *href_link = NULL;
3305 size_t len;
3307 if (getline(&line, &linesize, a->f) == -1) {
3308 if (ferror(a->f))
3309 err = got_error_from_errno("getline");
3310 break;
3313 committer = bline->committer;
3314 smallerthan = strchr(committer, '<');
3315 if (smallerthan && smallerthan[1] != '\0')
3316 committer = smallerthan + 1;
3317 at = strchr(committer, '@');
3318 if (at)
3319 *at = '\0';
3320 len = strlen(committer);
3321 if (len >= 9)
3322 committer[8] = '\0';
3324 nl = strchr(line, '\n');
3325 if (nl)
3326 *nl = '\0';
3328 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3329 "blame_wrapper", KATTR__MAX);
3330 if (kerr != KCGI_OK)
3331 goto err;
3332 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3333 "blame_number", KATTR__MAX);
3334 if (kerr != KCGI_OK)
3335 goto err;
3336 if (asprintf(&lineno, "%.*d", a->nlines_prec,
3337 a->lineno_cur) == -1)
3338 goto err;
3339 kerr = khtml_puts(a->gw_trans->gw_html_req, lineno);
3340 if (kerr != KCGI_OK)
3341 goto err;
3342 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3343 if (kerr != KCGI_OK)
3344 goto err;
3346 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3347 "blame_hash", KATTR__MAX);
3348 if (kerr != KCGI_OK)
3349 goto err;
3350 /* XXX repo_file could be NULL if not present in querystring */
3351 if (asprintf(&href_diff,
3352 "?path=%s&action=diff&commit=%s&file=%s&folder=%s",
3353 a->gw_trans->repo_name, bline->id_str,
3354 a->gw_trans->repo_file, a->gw_trans->repo_folder ?
3355 a->gw_trans->repo_folder : "") == -1) {
3356 err = got_error_from_errno("asprintf");
3357 goto err;
3359 if (asprintf(&href_link, "%.8s", bline->id_str) == -1) {
3360 err = got_error_from_errno("asprintf");
3361 goto err;
3363 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3364 KATTR_HREF, href_diff, KATTR__MAX);
3365 if (kerr != KCGI_OK)
3366 goto done;
3367 kerr = khtml_puts(a->gw_trans->gw_html_req, href_link);
3368 if (kerr != KCGI_OK)
3369 goto err;
3370 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3371 if (kerr != KCGI_OK)
3372 goto err;
3374 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3375 "blame_date", KATTR__MAX);
3376 if (kerr != KCGI_OK)
3377 goto err;
3378 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3379 if (kerr != KCGI_OK)
3380 goto err;
3381 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3382 if (kerr != KCGI_OK)
3383 goto err;
3385 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3386 "blame_author", KATTR__MAX);
3387 if (kerr != KCGI_OK)
3388 goto err;
3389 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
3390 if (kerr != KCGI_OK)
3391 goto err;
3392 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3393 if (kerr != KCGI_OK)
3394 goto err;
3396 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3397 "blame_code", KATTR__MAX);
3398 if (kerr != KCGI_OK)
3399 goto err;
3400 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
3401 if (kerr != KCGI_OK)
3402 goto err;
3403 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3404 if (kerr != KCGI_OK)
3405 goto err;
3407 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3408 if (kerr != KCGI_OK)
3409 goto err;
3411 a->lineno_cur++;
3412 bline = &a->lines[a->lineno_cur - 1];
3413 err:
3414 free(lineno);
3415 free(href_diff);
3416 free(href_link);
3418 done:
3419 if (commit)
3420 got_object_commit_close(commit);
3421 free(line);
3422 if (err == NULL && kerr != KCGI_OK)
3423 err = gw_kcgi_error(kerr);
3424 return err;
3427 static const struct got_error *
3428 gw_output_file_blame(struct gw_trans *gw_trans)
3430 const struct got_error *error = NULL;
3431 struct got_object_id *obj_id = NULL;
3432 struct got_object_id *commit_id = NULL;
3433 struct got_blob_object *blob = NULL;
3434 char *path = NULL, *in_repo_path = NULL;
3435 struct gw_blame_cb_args bca;
3436 int i, obj_type;
3437 size_t filesize;
3439 /* XXX repo_file could be NULL if not present in querystring */
3440 if (asprintf(&path, "%s%s%s",
3441 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3442 gw_trans->repo_folder ? "/" : "",
3443 gw_trans->repo_file) == -1) {
3444 error = got_error_from_errno("asprintf");
3445 goto done;
3448 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path, 1);
3449 if (error)
3450 goto done;
3452 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3453 GOT_OBJ_TYPE_COMMIT, 1, gw_trans->repo);
3454 if (error)
3455 goto done;
3457 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
3458 in_repo_path);
3459 if (error)
3460 goto done;
3462 if (obj_id == NULL) {
3463 error = got_error(GOT_ERR_NO_OBJ);
3464 goto done;
3467 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
3468 if (error)
3469 goto done;
3471 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3472 error = got_error(GOT_ERR_OBJ_TYPE);
3473 goto done;
3476 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
3477 if (error)
3478 goto done;
3480 bca.f = got_opentemp();
3481 if (bca.f == NULL) {
3482 error = got_error_from_errno("got_opentemp");
3483 goto done;
3485 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3486 &bca.line_offsets, bca.f, blob);
3487 if (error || bca.nlines == 0)
3488 goto done;
3490 /* Don't include \n at EOF in the blame line count. */
3491 if (bca.line_offsets[bca.nlines - 1] == filesize)
3492 bca.nlines--;
3494 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3495 if (bca.lines == NULL) {
3496 error = got_error_from_errno("calloc");
3497 goto done;
3499 bca.lineno_cur = 1;
3500 bca.nlines_prec = 0;
3501 i = bca.nlines;
3502 while (i > 0) {
3503 i /= 10;
3504 bca.nlines_prec++;
3506 bca.repo = gw_trans->repo;
3507 bca.gw_trans = gw_trans;
3509 error = got_blame(in_repo_path, commit_id, gw_trans->repo, gw_blame_cb,
3510 &bca, NULL, NULL);
3511 done:
3512 free(bca.line_offsets);
3513 free(in_repo_path);
3514 free(commit_id);
3515 free(obj_id);
3516 free(path);
3518 for (i = 0; i < bca.nlines; i++) {
3519 struct blame_line *bline = &bca.lines[i];
3520 free(bline->id_str);
3521 free(bline->committer);
3523 free(bca.lines);
3524 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3525 error = got_error_from_errno("fclose");
3526 if (blob)
3527 got_object_blob_close(blob);
3528 return error;
3531 static const struct got_error *
3532 gw_output_blob_buf(struct gw_trans *gw_trans)
3534 const struct got_error *error = NULL;
3535 struct got_object_id *obj_id = NULL;
3536 struct got_object_id *commit_id = NULL;
3537 struct got_blob_object *blob = NULL;
3538 char *path = NULL, *in_repo_path = NULL;
3539 int obj_type, set_mime = 0;
3540 size_t len, hdrlen;
3541 const uint8_t *buf;
3542 enum kcgi_err kerr = KCGI_OK;
3544 /* XXX repo_file could be NULL if not present in querystring */
3545 if (asprintf(&path, "%s%s%s",
3546 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3547 gw_trans->repo_folder ? "/" : "",
3548 gw_trans->repo_file) == -1) {
3549 error = got_error_from_errno("asprintf");
3550 goto done;
3553 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path, 1);
3554 if (error)
3555 goto done;
3557 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3558 GOT_OBJ_TYPE_COMMIT, 1, gw_trans->repo);
3559 if (error)
3560 goto done;
3562 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
3563 in_repo_path);
3564 if (error)
3565 goto done;
3567 if (obj_id == NULL) {
3568 error = got_error(GOT_ERR_NO_OBJ);
3569 goto done;
3572 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
3573 if (error)
3574 goto done;
3576 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3577 error = got_error(GOT_ERR_OBJ_TYPE);
3578 goto done;
3581 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
3582 if (error)
3583 goto done;
3585 hdrlen = got_object_blob_get_hdrlen(blob);
3586 do {
3587 error = got_object_blob_read_block(&len, blob);
3588 if (error)
3589 goto done;
3590 buf = got_object_blob_get_read_buf(blob);
3593 * Skip blob object header first time around,
3594 * which also contains a zero byte.
3596 buf += hdrlen;
3597 if (set_mime == 0) {
3598 if (isbinary(buf, len - hdrlen))
3599 gw_trans->mime = KMIME_APP_OCTET_STREAM;
3600 else
3601 gw_trans->mime = KMIME_TEXT_PLAIN;
3602 set_mime = 1;
3603 error = gw_display_index(gw_trans);
3604 if (error)
3605 goto done;
3607 khttp_write(gw_trans->gw_req, buf, len - hdrlen);
3608 hdrlen = 0;
3609 } while (len != 0);
3610 done:
3611 free(in_repo_path);
3612 free(commit_id);
3613 free(obj_id);
3614 free(path);
3615 if (blob)
3616 got_object_blob_close(blob);
3617 if (error == NULL && kerr != KCGI_OK)
3618 error = gw_kcgi_error(kerr);
3619 return error;
3622 static const struct got_error *
3623 gw_output_repo_tree(struct gw_trans *gw_trans)
3625 const struct got_error *error = NULL;
3626 struct got_object_id *tree_id = NULL, *commit_id = NULL;
3627 struct got_tree_object *tree = NULL;
3628 char *path = NULL, *in_repo_path = NULL;
3629 char *id_str = NULL;
3630 char *build_folder = NULL;
3631 char *href_blob = NULL, *href_blame = NULL;
3632 const char *class = NULL;
3633 int nentries, i, class_flip = 0;
3634 enum kcgi_err kerr = KCGI_OK;
3636 if (gw_trans->repo_folder != NULL) {
3637 path = strdup(gw_trans->repo_folder);
3638 if (path == NULL) {
3639 error = got_error_from_errno("strdup");
3640 goto done;
3642 } else {
3643 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
3644 gw_trans->repo_path, 1);
3645 if (error)
3646 goto done;
3647 free(path);
3648 path = in_repo_path;
3651 if (gw_trans->commit == NULL) {
3652 struct got_reference *head_ref;
3653 error = got_ref_open(&head_ref, gw_trans->repo,
3654 gw_trans->headref, 0);
3655 if (error)
3656 goto done;
3657 error = got_ref_resolve(&commit_id, gw_trans->repo, head_ref);
3658 if (error)
3659 goto done;
3660 got_ref_close(head_ref);
3662 } else {
3663 error = got_repo_match_object_id(&commit_id, NULL,
3664 gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, gw_trans->repo);
3665 if (error)
3666 goto done;
3670 * XXX gw_trans->commit might have already been allocated in
3671 * gw_parse_querystring(); could we more cleanly seperate values
3672 * we received as arguments from values we compute ourselves?
3674 error = got_object_id_str(&gw_trans->commit, commit_id);
3675 if (error)
3676 goto done;
3678 error = got_object_id_by_path(&tree_id, gw_trans->repo, commit_id, path);
3679 if (error)
3680 goto done;
3682 error = got_object_open_as_tree(&tree, gw_trans->repo, tree_id);
3683 if (error)
3684 goto done;
3686 nentries = got_object_tree_get_nentries(tree);
3687 for (i = 0; i < nentries; i++) {
3688 struct got_tree_entry *te;
3689 const char *modestr = "";
3690 mode_t mode;
3692 te = got_object_tree_get_entry(tree, i);
3694 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
3695 if (error)
3696 goto done;
3698 mode = got_tree_entry_get_mode(te);
3699 if (got_object_tree_entry_is_submodule(te))
3700 modestr = "$";
3701 else if (S_ISLNK(mode))
3702 modestr = "@";
3703 else if (S_ISDIR(mode))
3704 modestr = "/";
3705 else if (mode & S_IXUSR)
3706 modestr = "*";
3708 if (class_flip == 0) {
3709 class = "back_lightgray";
3710 class_flip = 1;
3711 } else {
3712 class = "back_white";
3713 class_flip = 0;
3716 if (S_ISDIR(mode)) {
3717 if (asprintf(&build_folder, "%s/%s",
3718 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3719 got_tree_entry_get_name(te)) == -1) {
3720 error = got_error_from_errno("asprintf");
3721 goto done;
3723 if (asprintf(&href_blob,
3724 "?path=%s&action=%s&commit=%s&folder=%s",
3725 gw_trans->repo_name, gw_get_action_name(gw_trans),
3726 gw_trans->commit, build_folder) == -1) {
3727 error = got_error_from_errno("asprintf");
3728 goto done;
3731 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3732 KATTR_ID, "tree_wrapper", KATTR__MAX);
3733 if (kerr != KCGI_OK)
3734 goto done;
3735 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3736 KATTR_ID, "tree_line", KATTR_CLASS, class,
3737 KATTR__MAX);
3738 if (kerr != KCGI_OK)
3739 goto done;
3740 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3741 KATTR_HREF, href_blob, KATTR_CLASS,
3742 "diff_directory", KATTR__MAX);
3743 if (kerr != KCGI_OK)
3744 goto done;
3745 kerr = khtml_puts(gw_trans->gw_html_req,
3746 got_tree_entry_get_name(te));
3747 if (kerr != KCGI_OK)
3748 goto done;
3749 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3750 if (kerr != KCGI_OK)
3751 goto done;
3752 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3753 if (kerr != KCGI_OK)
3754 goto done;
3755 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3756 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
3757 KATTR__MAX);
3758 if (kerr != KCGI_OK)
3759 goto done;
3760 kerr = khtml_entity(gw_trans->gw_html_req,
3761 KENTITY_nbsp);
3762 if (kerr != KCGI_OK)
3763 goto done;
3764 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3765 if (kerr != KCGI_OK)
3766 goto done;
3767 } else {
3768 if (asprintf(&href_blob,
3769 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3770 gw_trans->repo_name, "blob", gw_trans->commit,
3771 got_tree_entry_get_name(te),
3772 gw_trans->repo_folder ?
3773 gw_trans->repo_folder : "") == -1) {
3774 error = got_error_from_errno("asprintf");
3775 goto done;
3777 if (asprintf(&href_blame,
3778 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3779 gw_trans->repo_name, "blame", gw_trans->commit,
3780 got_tree_entry_get_name(te),
3781 gw_trans->repo_folder ?
3782 gw_trans->repo_folder : "") == -1) {
3783 error = got_error_from_errno("asprintf");
3784 goto done;
3787 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3788 KATTR_ID, "tree_wrapper", KATTR__MAX);
3789 if (kerr != KCGI_OK)
3790 goto done;
3791 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3792 KATTR_ID, "tree_line", KATTR_CLASS, class,
3793 KATTR__MAX);
3794 if (kerr != KCGI_OK)
3795 goto done;
3796 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3797 KATTR_HREF, href_blob, KATTR__MAX);
3798 if (kerr != KCGI_OK)
3799 goto done;
3800 kerr = khtml_puts(gw_trans->gw_html_req,
3801 got_tree_entry_get_name(te));
3802 if (kerr != KCGI_OK)
3803 goto done;
3804 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3805 if (kerr != KCGI_OK)
3806 goto done;
3807 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3808 if (kerr != KCGI_OK)
3809 goto done;
3810 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3811 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
3812 KATTR__MAX);
3813 if (kerr != KCGI_OK)
3814 goto done;
3816 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3817 KATTR_HREF, href_blob, KATTR__MAX);
3818 if (kerr != KCGI_OK)
3819 goto done;
3820 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
3821 if (kerr != KCGI_OK)
3822 goto done;
3823 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3824 if (kerr != KCGI_OK)
3825 goto done;
3827 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3828 if (kerr != KCGI_OK)
3829 goto done;
3831 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3832 KATTR_HREF, href_blame, KATTR__MAX);
3833 if (kerr != KCGI_OK)
3834 goto done;
3835 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
3836 if (kerr != KCGI_OK)
3837 goto done;
3839 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3840 if (kerr != KCGI_OK)
3841 goto done;
3843 free(id_str);
3844 id_str = NULL;
3845 free(href_blob);
3846 href_blob = NULL;
3847 free(build_folder);
3848 build_folder = NULL;
3850 done:
3851 if (tree)
3852 got_object_tree_close(tree);
3853 free(id_str);
3854 free(href_blob);
3855 free(href_blame);
3856 free(in_repo_path);
3857 free(tree_id);
3858 free(build_folder);
3859 if (error == NULL && kerr != KCGI_OK)
3860 error = gw_kcgi_error(kerr);
3861 return error;
3864 static const struct got_error *
3865 gw_output_repo_heads(struct gw_trans *gw_trans)
3867 const struct got_error *error = NULL;
3868 struct got_reflist_head refs;
3869 struct got_reflist_entry *re;
3870 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
3871 char *href_commits = NULL;
3872 enum kcgi_err kerr = KCGI_OK;
3874 SIMPLEQ_INIT(&refs);
3876 error = got_ref_list(&refs, gw_trans->repo, "refs/heads",
3877 got_ref_cmp_by_name, NULL);
3878 if (error)
3879 goto done;
3881 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3882 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
3883 if (kerr != KCGI_OK)
3884 goto done;
3885 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3886 KATTR_ID, "summary_heads_title", KATTR__MAX);
3887 if (kerr != KCGI_OK)
3888 goto done;
3889 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
3890 if (kerr != KCGI_OK)
3891 goto done;
3892 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3893 if (kerr != KCGI_OK)
3894 goto done;
3895 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3896 KATTR_ID, "summary_heads_content", KATTR__MAX);
3897 if (kerr != KCGI_OK)
3898 goto done;
3900 SIMPLEQ_FOREACH(re, &refs, entry) {
3901 const char *refname;
3903 if (got_ref_is_symbolic(re->ref))
3904 continue;
3906 refname = got_ref_get_name(re->ref);
3907 if (refname == NULL) {
3908 error = got_error_from_errno("got_ref_to_str");
3909 goto done;
3912 if (strncmp(refname, "refs/heads/", 11) != 0)
3913 continue;
3915 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
3916 refname, TM_DIFF);
3917 if (error)
3918 goto done;
3920 if (strncmp(refname, "refs/heads/", 11) == 0)
3921 refname += 11;
3923 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3924 KATTR_ID, "heads_wrapper", KATTR__MAX);
3925 if (kerr != KCGI_OK)
3926 goto done;
3927 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3928 KATTR_ID, "heads_age", KATTR__MAX);
3929 if (kerr != KCGI_OK)
3930 goto done;
3931 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
3932 if (kerr != KCGI_OK)
3933 goto done;
3934 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3935 if (kerr != KCGI_OK)
3936 goto done;
3937 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3938 KATTR_ID, "heads_space", KATTR__MAX);
3939 if (kerr != KCGI_OK)
3940 goto done;
3941 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
3942 if (kerr != KCGI_OK)
3943 goto done;
3944 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3945 if (kerr != KCGI_OK)
3946 goto done;
3947 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3948 KATTR_ID, "head", KATTR__MAX);
3949 if (kerr != KCGI_OK)
3950 goto done;
3951 if (asprintf(&href_summary,
3952 "?path=%s&action=summary&headref=%s",
3953 gw_trans->repo_name, refname) == -1) {
3954 error = got_error_from_errno("asprintf");
3955 goto done;
3957 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
3958 href_summary, KATTR__MAX);
3959 kerr = khtml_puts(gw_trans->gw_html_req, refname);
3960 if (kerr != KCGI_OK)
3961 goto done;
3962 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3963 if (kerr != KCGI_OK)
3964 goto done;
3966 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3967 "navs_wrapper", KATTR__MAX);
3968 if (kerr != KCGI_OK)
3969 goto done;
3970 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3971 "navs", KATTR__MAX);
3972 if (kerr != KCGI_OK)
3973 goto done;
3975 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
3976 href_summary, KATTR__MAX);
3977 if (kerr != KCGI_OK)
3978 goto done;
3979 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
3980 if (kerr != KCGI_OK)
3981 goto done;
3982 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3983 if (kerr != KCGI_OK)
3984 goto done;
3986 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3987 if (kerr != KCGI_OK)
3988 goto done;
3989 if (asprintf(&href_briefs, "?path=%s&action=briefs&headref=%s",
3990 gw_trans->repo_name, refname) == -1) {
3991 error = got_error_from_errno("asprintf");
3992 goto done;
3994 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
3995 href_briefs, KATTR__MAX);
3996 if (kerr != KCGI_OK)
3997 goto done;
3998 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
3999 if (kerr != KCGI_OK)
4000 goto done;
4001 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4002 if (kerr != KCGI_OK)
4003 goto done;
4005 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4006 if (kerr != KCGI_OK)
4007 goto done;
4009 if (asprintf(&href_commits,
4010 "?path=%s&action=commits&headref=%s",
4011 gw_trans->repo_name, refname) == -1) {
4012 error = got_error_from_errno("asprintf");
4013 goto done;
4015 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4016 href_commits, KATTR__MAX);
4017 if (kerr != KCGI_OK)
4018 goto done;
4019 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4020 if (kerr != KCGI_OK)
4021 goto done;
4022 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4023 if (kerr != KCGI_OK)
4024 goto done;
4026 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4027 "dotted_line", KATTR__MAX);
4028 if (kerr != KCGI_OK)
4029 goto done;
4030 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4031 if (kerr != KCGI_OK)
4032 goto done;
4033 free(href_summary);
4034 href_summary = NULL;
4035 free(href_briefs);
4036 href_briefs = NULL;
4037 free(href_commits);
4038 href_commits = NULL;
4040 done:
4041 got_ref_list_free(&refs);
4042 free(href_summary);
4043 free(href_briefs);
4044 free(href_commits);
4045 return error;
4048 static const struct got_error *
4049 gw_output_site_link(struct gw_trans *gw_trans)
4051 const struct got_error *error = NULL;
4052 char *href_summary = NULL;
4053 enum kcgi_err kerr = KCGI_OK;
4055 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4056 "site_link", KATTR__MAX);
4057 if (kerr != KCGI_OK)
4058 goto done;
4059 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4060 KATTR__MAX);
4061 if (kerr != KCGI_OK)
4062 goto done;
4063 kerr = khtml_puts(gw_trans->gw_html_req,
4064 gw_trans->gw_conf->got_site_link);
4065 if (kerr != KCGI_OK)
4066 goto done;
4067 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4068 if (kerr != KCGI_OK)
4069 goto done;
4071 if (gw_trans->repo_name != NULL) {
4072 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4073 if (kerr != KCGI_OK)
4074 goto done;
4075 if (asprintf(&href_summary, "?path=%s&action=summary",
4076 gw_trans->repo_name) == -1)
4077 goto done;
4078 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4079 href_summary, KATTR__MAX);
4080 if (kerr != KCGI_OK)
4081 goto done;
4082 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4083 if (kerr != KCGI_OK)
4084 goto done;
4085 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4086 if (kerr != KCGI_OK)
4087 goto done;
4088 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4089 if (kerr != KCGI_OK)
4090 goto done;
4091 kerr = khtml_puts(gw_trans->gw_html_req,
4092 gw_get_action_name(gw_trans));
4093 if (kerr != KCGI_OK)
4094 goto done;
4097 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4098 if (kerr != KCGI_OK)
4099 goto done;
4100 done:
4101 free(href_summary);
4102 return error;
4105 static const struct got_error *
4106 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4108 const struct got_error *error = NULL;
4109 char *color = NULL;
4110 enum kcgi_err kerr = KCGI_OK;
4112 if (strncmp(buf, "-", 1) == 0)
4113 color = "diff_minus";
4114 else if (strncmp(buf, "+", 1) == 0)
4115 color = "diff_plus";
4116 else if (strncmp(buf, "@@", 2) == 0)
4117 color = "diff_chunk_header";
4118 else if (strncmp(buf, "@@", 2) == 0)
4119 color = "diff_chunk_header";
4120 else if (strncmp(buf, "commit +", 8) == 0)
4121 color = "diff_meta";
4122 else if (strncmp(buf, "commit -", 8) == 0)
4123 color = "diff_meta";
4124 else if (strncmp(buf, "blob +", 6) == 0)
4125 color = "diff_meta";
4126 else if (strncmp(buf, "blob -", 6) == 0)
4127 color = "diff_meta";
4128 else if (strncmp(buf, "file +", 6) == 0)
4129 color = "diff_meta";
4130 else if (strncmp(buf, "file -", 6) == 0)
4131 color = "diff_meta";
4132 else if (strncmp(buf, "from:", 5) == 0)
4133 color = "diff_author";
4134 else if (strncmp(buf, "via:", 4) == 0)
4135 color = "diff_author";
4136 else if (strncmp(buf, "date:", 5) == 0)
4137 color = "diff_date";
4138 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4139 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4140 if (error == NULL && kerr != KCGI_OK)
4141 error = gw_kcgi_error(kerr);
4142 return error;
4145 int
4146 main(int argc, char *argv[])
4148 const struct got_error *error = NULL;
4149 struct gw_trans *gw_trans;
4150 struct gw_dir *dir = NULL, *tdir;
4151 const char *page = "index";
4152 int gw_malloc = 1;
4153 enum kcgi_err kerr;
4155 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4156 errx(1, "malloc");
4158 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4159 errx(1, "malloc");
4161 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4162 errx(1, "malloc");
4164 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4165 errx(1, "malloc");
4167 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4168 if (kerr != KCGI_OK) {
4169 error = gw_kcgi_error(kerr);
4170 goto done;
4173 if ((gw_trans->gw_conf =
4174 malloc(sizeof(struct gotweb_conf))) == NULL) {
4175 gw_malloc = 0;
4176 error = got_error_from_errno("malloc");
4177 goto done;
4180 TAILQ_INIT(&gw_trans->gw_dirs);
4181 TAILQ_INIT(&gw_trans->gw_headers);
4183 gw_trans->action = -1;
4184 gw_trans->page = 0;
4185 gw_trans->repos_total = 0;
4186 gw_trans->repo_path = NULL;
4187 gw_trans->commit = NULL;
4188 gw_trans->headref = GOT_REF_HEAD;
4189 gw_trans->mime = KMIME_TEXT_HTML;
4190 gw_trans->gw_tmpl->key = gw_templs;
4191 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4192 gw_trans->gw_tmpl->arg = gw_trans;
4193 gw_trans->gw_tmpl->cb = gw_template;
4194 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
4195 if (error)
4196 goto done;
4198 error = gw_parse_querystring(gw_trans);
4199 if (error)
4200 goto done;
4202 if (gw_trans->action == GW_BLOB)
4203 error = gw_blob(gw_trans);
4204 else
4205 error = gw_display_index(gw_trans);
4206 done:
4207 if (gw_malloc) {
4208 free(gw_trans->gw_conf->got_repos_path);
4209 free(gw_trans->gw_conf->got_site_name);
4210 free(gw_trans->gw_conf->got_site_owner);
4211 free(gw_trans->gw_conf->got_site_link);
4212 free(gw_trans->gw_conf->got_logo);
4213 free(gw_trans->gw_conf->got_logo_url);
4214 free(gw_trans->gw_conf);
4215 free(gw_trans->commit);
4216 free(gw_trans->repo_path);
4217 if (gw_trans->repo)
4218 got_repo_close(gw_trans->repo);
4220 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4221 free(dir->name);
4222 free(dir->description);
4223 free(dir->age);
4224 free(dir->url);
4225 free(dir->path);
4226 free(dir);
4231 khttp_free(gw_trans->gw_req);
4232 return 0;