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 <dirent.h>
23 #include <err.h>
24 #include <regex.h>
25 #include <stdarg.h>
26 #include <stdbool.h>
27 #include <stdint.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
33 #include <got_object.h>
34 #include <got_reference.h>
35 #include <got_repository.h>
36 #include <got_path.h>
37 #include <got_cancel.h>
38 #include <got_worktree.h>
39 #include <got_diff.h>
40 #include <got_commit_graph.h>
41 #include <got_blame.h>
42 #include <got_privsep.h>
43 #include <got_opentemp.h>
45 #include <kcgi.h>
46 #include <kcgihtml.h>
48 #include "buf.h"
49 #include "gotweb.h"
50 #include "gotweb_ui.h"
52 #ifndef nitems
53 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
54 #endif
56 struct gw_trans {
57 TAILQ_HEAD(headers, gw_header) gw_headers;
58 TAILQ_HEAD(dirs, gw_dir) gw_dirs;
59 struct 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 char *repo_name;
65 char *repo_path;
66 char *commit;
67 char *repo_file;
68 char *repo_folder;
69 char *action_name;
70 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_repository *repo;
80 struct got_reflist_head refs;
81 struct got_commit_object *commit;
82 struct got_object_id *id;
83 char *path;
85 char *refs_str;
86 char *commit_id; /* id_str1 */
87 char *parent_id; /* id_str2 */
88 char *tree_id;
89 char *author;
90 char *committer;
91 char *commit_msg;
92 time_t committer_time;
93 };
95 struct gw_dir {
96 TAILQ_ENTRY(gw_dir) entry;
97 char *name;
98 char *owner;
99 char *description;
100 char *url;
101 char *age;
102 char *path;
103 };
105 enum gw_key {
106 KEY_ACTION,
107 KEY_COMMIT_ID,
108 KEY_FILE,
109 KEY_FOLDER,
110 KEY_HEADREF,
111 KEY_PAGE,
112 KEY_PATH,
113 KEY__ZMAX
114 };
116 enum gw_tmpl {
117 TEMPL_CONTENT,
118 TEMPL_HEAD,
119 TEMPL_HEADER,
120 TEMPL_SEARCH,
121 TEMPL_SITEPATH,
122 TEMPL_SITEOWNER,
123 TEMPL_TITLE,
124 TEMPL__MAX
125 };
127 enum gw_ref_tm {
128 TM_DIFF,
129 TM_LONG,
130 };
132 enum gw_tags {
133 TAGBRIEF,
134 TAGFULL,
135 };
137 static const char *const gw_templs[TEMPL__MAX] = {
138 "content",
139 "head",
140 "header",
141 "search",
142 "sitepath",
143 "siteowner",
144 "title",
145 };
147 static const struct kvalid gw_keys[KEY__ZMAX] = {
148 { kvalid_stringne, "action" },
149 { kvalid_stringne, "commit" },
150 { kvalid_stringne, "file" },
151 { kvalid_stringne, "folder" },
152 { kvalid_stringne, "headref" },
153 { kvalid_int, "page" },
154 { kvalid_stringne, "path" },
155 };
157 static struct gw_dir *gw_init_gw_dir(char *);
158 static struct gw_header *gw_init_header(void);
160 static char *gw_get_repo_description(struct gw_trans *,
161 char *);
162 static char *gw_get_repo_owner(struct gw_trans *,
163 char *);
164 static char *gw_get_time_str(time_t, int);
165 static char *gw_get_repo_age(struct gw_trans *,
166 char *, char *, int);
167 static char *gw_get_file_blame(struct gw_trans *);
168 static char *gw_get_repo_tree(struct gw_trans *);
169 static char *gw_get_diff(struct gw_trans *,
170 struct gw_header *);
171 static char *gw_get_repo_tags(struct gw_trans *, int, int);
172 static char *gw_get_repo_heads(struct gw_trans *);
173 static char *gw_get_clone_url(struct gw_trans *, char *);
174 static char *gw_get_got_link(struct gw_trans *);
175 static char *gw_get_site_link(struct gw_trans *);
176 static char *gw_html_escape(const char *);
177 static char *gw_colordiff_line(char *);
179 static char *gw_gen_commit_header(char *, char*);
180 static char *gw_gen_diff_header(char *, char*);
181 static char *gw_gen_author_header(char *);
182 static char *gw_gen_committer_header(char *);
183 static char *gw_gen_age_header(char *);
184 static char *gw_gen_commit_msg_header(char *);
185 static char *gw_gen_tree_header(char *);
187 static void gw_free_headers(struct gw_header *);
188 static void gw_display_open(struct gw_trans *, enum khttp,
189 enum kmime);
190 static void gw_display_index(struct gw_trans *,
191 const struct got_error *);
193 static int gw_template(size_t, void *);
195 static const struct got_error* gw_get_header(struct gw_trans *,
196 struct gw_header *, int);
197 static const struct got_error* gw_get_commits(struct gw_trans *,
198 struct gw_header *, int);
199 static const struct got_error* gw_get_commit(struct gw_trans *,
200 struct gw_header *);
201 static const struct got_error* gw_apply_unveil(const char *, const char *);
202 static const struct got_error* gw_blame_cb(void *, int, int,
203 struct got_object_id *);
204 static const struct got_error* gw_load_got_paths(struct gw_trans *);
205 static const struct got_error* gw_load_got_path(struct gw_trans *,
206 struct gw_dir *);
207 static const struct got_error* gw_parse_querystring(struct gw_trans *);
209 static const struct got_error* gw_blame(struct gw_trans *);
210 static const struct got_error* gw_diff(struct gw_trans *);
211 static const struct got_error* gw_index(struct gw_trans *);
212 static const struct got_error* gw_commits(struct gw_trans *);
213 static const struct got_error* gw_briefs(struct gw_trans *);
214 static const struct got_error* gw_summary(struct gw_trans *);
215 static const struct got_error* gw_tree(struct gw_trans *);
217 struct gw_query_action {
218 unsigned int func_id;
219 const char *func_name;
220 const struct got_error *(*func_main)(struct gw_trans *);
221 char *template;
222 };
224 enum gw_query_actions {
225 GW_BLAME,
226 GW_BRIEFS,
227 GW_COMMITS,
228 GW_DIFF,
229 GW_ERR,
230 GW_INDEX,
231 GW_SUMMARY,
232 GW_TREE,
233 };
235 static struct gw_query_action gw_query_funcs[] = {
236 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
237 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
238 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
239 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
240 { GW_ERR, NULL, NULL, "gw_tmpl/err.tmpl" },
241 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
242 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
243 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
244 };
246 static const struct got_error *
247 gw_apply_unveil(const char *repo_path, const char *repo_file)
249 const struct got_error *err;
251 if (repo_path && repo_file) {
252 char *full_path;
253 if ((asprintf(&full_path, "%s/%s", repo_path, repo_file)) == -1)
254 return got_error_from_errno("asprintf unveil");
255 if (unveil(full_path, "r") != 0)
256 return got_error_from_errno2("unveil", full_path);
259 if (repo_path && unveil(repo_path, "r") != 0)
260 return got_error_from_errno2("unveil", repo_path);
262 if (unveil("/tmp", "rwc") != 0)
263 return got_error_from_errno2("unveil", "/tmp");
265 err = got_privsep_unveil_exec_helpers();
266 if (err != NULL)
267 return err;
269 if (unveil(NULL, NULL) != 0)
270 return got_error_from_errno("unveil");
272 return NULL;
275 static const struct got_error *
276 gw_blame(struct gw_trans *gw_trans)
278 const struct got_error *error = NULL;
279 struct gw_header *header = NULL;
280 char *blame = NULL, *blame_html = NULL, *blame_html_disp = NULL;
282 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
283 NULL) == -1)
284 return got_error_from_errno("pledge");
286 if ((header = gw_init_header()) == NULL)
287 return got_error_from_errno("malloc");
289 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
290 if (error)
291 return error;
293 error = gw_get_header(gw_trans, header, 1);
294 if (error)
295 return error;
297 blame_html = gw_get_file_blame(gw_trans);
299 if (blame_html == NULL)
300 blame_html = strdup("");
302 if ((asprintf(&blame_html_disp, blame_header,
303 gw_gen_age_header(gw_get_time_str(header->committer_time, TM_LONG)),
304 gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
305 blame_html)) == -1)
306 return got_error_from_errno("asprintf");
308 if ((asprintf(&blame, blame_wrapper, blame_html_disp)) == -1)
309 return got_error_from_errno("asprintf");
311 khttp_puts(gw_trans->gw_req, blame);
312 got_ref_list_free(&header->refs);
313 gw_free_headers(header);
314 free(blame_html_disp);
315 free(blame_html);
316 free(blame);
317 return error;
320 static const struct got_error *
321 gw_diff(struct gw_trans *gw_trans)
323 const struct got_error *error = NULL;
324 struct gw_header *header = NULL;
325 char *diff = NULL, *diff_html = NULL, *diff_html_disp = NULL;
327 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
328 NULL) == -1)
329 return got_error_from_errno("pledge");
331 if ((header = malloc(sizeof(struct gw_header))) == NULL)
332 return got_error_from_errno("malloc");
334 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
335 if (error)
336 return error;
338 error = gw_get_header(gw_trans, header, 1);
339 if (error)
340 return error;
342 diff_html = gw_get_diff(gw_trans, header);
344 if (diff_html == NULL)
345 diff_html = strdup("");
347 if ((asprintf(&diff_html_disp, diff_header,
348 gw_gen_diff_header(header->parent_id, header->commit_id),
349 gw_gen_commit_header(header->commit_id, header->refs_str),
350 gw_gen_tree_header(header->tree_id),
351 gw_gen_author_header(header->author),
352 gw_gen_committer_header(header->committer),
353 gw_gen_age_header(gw_get_time_str(header->committer_time, TM_LONG)),
354 gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
355 diff_html)) == -1)
356 return got_error_from_errno("asprintf");
358 if ((asprintf(&diff, diff_wrapper, diff_html_disp)) == -1)
359 return got_error_from_errno("asprintf");
361 khttp_puts(gw_trans->gw_req, diff);
362 got_ref_list_free(&header->refs);
363 gw_free_headers(header);
364 free(diff_html_disp);
365 free(diff_html);
366 free(diff);
367 return error;
370 static const struct got_error *
371 gw_index(struct gw_trans *gw_trans)
373 const struct got_error *error = NULL;
374 struct gw_dir *gw_dir = NULL;
375 char *html, *navs, *next, *prev;
376 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
378 if (pledge("stdio rpath proc exec sendfd unveil",
379 NULL) == -1) {
380 error = got_error_from_errno("pledge");
381 return error;
384 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
385 if (error)
386 return error;
388 error = gw_load_got_paths(gw_trans);
389 if (error)
390 return error;
392 khttp_puts(gw_trans->gw_req, index_projects_header);
394 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
395 if (asprintf(&html, index_projects_empty,
396 gw_trans->gw_conf->got_repos_path) == -1)
397 return got_error_from_errno("asprintf");
398 khttp_puts(gw_trans->gw_req, html);
399 free(html);
400 return NULL;
403 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
404 dir_c++;
406 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
407 if (gw_trans->page > 0 && (gw_trans->page *
408 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
409 prev_disp++;
410 continue;
413 prev_disp++;
415 if (error)
416 return error;
417 if((asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
418 gw_dir->name, gw_dir->name)) == -1)
419 return got_error_from_errno("asprintf");
421 if ((asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
422 gw_dir->description, gw_dir->owner, gw_dir->age,
423 navs)) == -1)
424 return got_error_from_errno("asprintf");
426 khttp_puts(gw_trans->gw_req, html);
428 free(navs);
429 free(html);
431 if (gw_trans->gw_conf->got_max_repos_display == 0)
432 continue;
434 if (next_disp == gw_trans->gw_conf->got_max_repos_display)
435 khttp_puts(gw_trans->gw_req, np_wrapper_start);
436 else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
437 (gw_trans->page > 0) &&
438 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
439 prev_disp == gw_trans->repos_total))
440 khttp_puts(gw_trans->gw_req, np_wrapper_start);
442 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
443 (gw_trans->page > 0) &&
444 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
445 prev_disp == gw_trans->repos_total)) {
446 if ((asprintf(&prev, nav_prev,
447 gw_trans->page - 1)) == -1)
448 return got_error_from_errno("asprintf");
449 khttp_puts(gw_trans->gw_req, prev);
450 free(prev);
453 khttp_puts(gw_trans->gw_req, div_end);
455 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
456 next_disp == gw_trans->gw_conf->got_max_repos_display &&
457 dir_c != (gw_trans->page + 1) *
458 gw_trans->gw_conf->got_max_repos_display) {
459 if ((asprintf(&next, nav_next,
460 gw_trans->page + 1)) == -1)
461 return got_error_from_errno("calloc");
462 khttp_puts(gw_trans->gw_req, next);
463 khttp_puts(gw_trans->gw_req, div_end);
464 free(next);
465 next_disp = 0;
466 break;
469 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
470 (gw_trans->page > 0) &&
471 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
472 prev_disp == gw_trans->repos_total))
473 khttp_puts(gw_trans->gw_req, div_end);
475 next_disp++;
477 return error;
480 static const struct got_error *
481 gw_commits(struct gw_trans *gw_trans)
483 const struct got_error *error = NULL;
484 char *commits_html, *commits_navs_html;
485 struct gw_header *header = NULL, *n_header = NULL;
487 if ((header = malloc(sizeof(struct gw_header))) == NULL)
488 return got_error_from_errno("malloc");
490 if (pledge("stdio rpath proc exec sendfd unveil",
491 NULL) == -1) {
492 error = got_error_from_errno("pledge");
493 return error;
496 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
497 if (error)
498 return error;
500 error = gw_get_header(gw_trans, header,
501 gw_trans->gw_conf->got_max_commits_display);
503 khttp_puts(gw_trans->gw_req, commits_wrapper);
504 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
505 if ((asprintf(&commits_navs_html, commits_navs,
506 gw_trans->repo_name, n_header->commit_id,
507 gw_trans->repo_name, n_header->commit_id,
508 gw_trans->repo_name, n_header->commit_id)) == -1)
509 return got_error_from_errno("asprintf");
511 if ((asprintf(&commits_html, commits_line,
512 gw_gen_commit_header(n_header->commit_id,
513 n_header->refs_str),
514 gw_gen_author_header(n_header->author),
515 gw_gen_committer_header(n_header->committer),
516 gw_gen_age_header(gw_get_time_str(n_header->committer_time,
517 TM_LONG)), gw_html_escape(n_header->commit_msg),
518 commits_navs_html)) == -1)
519 return got_error_from_errno("asprintf");
520 khttp_puts(gw_trans->gw_req, commits_html);
522 khttp_puts(gw_trans->gw_req, div_end);
524 got_ref_list_free(&header->refs);
525 gw_free_headers(header);
526 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
527 gw_free_headers(n_header);
528 return error;
531 static const struct got_error *
532 gw_briefs(struct gw_trans *gw_trans)
534 const struct got_error *error = NULL;
535 char *briefs_html = NULL, *briefs_navs_html = NULL, *newline;
536 struct gw_header *header = NULL, *n_header = NULL;
538 if ((header = malloc(sizeof(struct gw_header))) == NULL)
539 return got_error_from_errno("malloc");
541 if (pledge("stdio rpath proc exec sendfd unveil",
542 NULL) == -1) {
543 error = got_error_from_errno("pledge");
544 return error;
547 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
548 if (error)
549 return error;
551 if (gw_trans->action == GW_SUMMARY)
552 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
553 else
554 error = gw_get_header(gw_trans, header,
555 gw_trans->gw_conf->got_max_commits_display);
557 khttp_puts(gw_trans->gw_req, briefs_wrapper);
558 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
559 if ((asprintf(&briefs_navs_html, briefs_navs,
560 gw_trans->repo_name, n_header->commit_id,
561 gw_trans->repo_name, n_header->commit_id,
562 gw_trans->repo_name, n_header->commit_id)) == -1)
563 return got_error_from_errno("asprintf");
564 newline = strchr(n_header->commit_msg, '\n');
565 if (newline)
566 *newline = '\0';
567 if ((asprintf(&briefs_html, briefs_line,
568 gw_get_time_str(n_header->committer_time, TM_DIFF),
569 n_header->author, gw_html_escape(n_header->commit_msg),
570 briefs_navs_html)) == -1)
571 return got_error_from_errno("asprintf");
572 khttp_puts(gw_trans->gw_req, briefs_html);
574 khttp_puts(gw_trans->gw_req, div_end);
576 got_ref_list_free(&header->refs);
577 gw_free_headers(header);
578 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
579 gw_free_headers(n_header);
580 return error;
583 static const struct got_error *
584 gw_summary(struct gw_trans *gw_trans)
586 const struct got_error *error = NULL;
587 char *description_html, *repo_owner_html, *repo_age_html,
588 *cloneurl_html, *tags, *heads, *tags_html,
589 *heads_html, *age;
591 if (pledge("stdio rpath proc exec sendfd unveil",
592 NULL) == -1) {
593 error = got_error_from_errno("pledge");
594 return error;
597 /* unveil is applied with gw_briefs below */
599 khttp_puts(gw_trans->gw_req, summary_wrapper);
600 if (gw_trans->gw_conf->got_show_repo_description) {
601 if (gw_trans->gw_dir->description != NULL &&
602 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
603 if ((asprintf(&description_html, description,
604 gw_trans->gw_dir->description)) == -1)
605 return got_error_from_errno("asprintf");
607 khttp_puts(gw_trans->gw_req, description_html);
608 free(description_html);
612 if (gw_trans->gw_conf->got_show_repo_owner) {
613 if (gw_trans->gw_dir->owner != NULL &&
614 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
615 if ((asprintf(&repo_owner_html, repo_owner,
616 gw_trans->gw_dir->owner)) == -1)
617 return got_error_from_errno("asprintf");
619 khttp_puts(gw_trans->gw_req, repo_owner_html);
620 free(repo_owner_html);
624 if (gw_trans->gw_conf->got_show_repo_age) {
625 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path,
626 "refs/heads", TM_LONG);
627 if (age != NULL && (strcmp(age, "") != 0)) {
628 if ((asprintf(&repo_age_html, last_change, age)) == -1)
629 return got_error_from_errno("asprintf");
631 khttp_puts(gw_trans->gw_req, repo_age_html);
632 free(repo_age_html);
633 free(age);
637 if (gw_trans->gw_conf->got_show_repo_cloneurl) {
638 if (gw_trans->gw_dir->url != NULL &&
639 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
640 if ((asprintf(&cloneurl_html, cloneurl,
641 gw_trans->gw_dir->url)) == -1)
642 return got_error_from_errno("asprintf");
644 khttp_puts(gw_trans->gw_req, cloneurl_html);
645 free(cloneurl_html);
648 khttp_puts(gw_trans->gw_req, div_end);
650 error = gw_briefs(gw_trans);
651 if (error)
652 return error;
654 tags = gw_get_repo_tags(gw_trans, D_MAXSLCOMMDISP, TAGBRIEF);
655 heads = gw_get_repo_heads(gw_trans);
657 if (tags != NULL && strcmp(tags, "") != 0) {
658 if ((asprintf(&tags_html, summary_tags,
659 tags)) == -1)
660 return got_error_from_errno("asprintf");
661 khttp_puts(gw_trans->gw_req, tags_html);
662 free(tags_html);
663 free(tags);
666 if (heads != NULL && strcmp(heads, "") != 0) {
667 if ((asprintf(&heads_html, summary_heads,
668 heads)) == -1)
669 return got_error_from_errno("asprintf");
670 khttp_puts(gw_trans->gw_req, heads_html);
671 free(heads_html);
672 free(heads);
674 return error;
677 static const struct got_error *
678 gw_tree(struct gw_trans *gw_trans)
680 const struct got_error *error = NULL;
681 struct gw_header *header = NULL;
682 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
684 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
685 return got_error_from_errno("pledge");
687 if ((header = malloc(sizeof(struct gw_header))) == NULL)
688 return got_error_from_errno("malloc");
690 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
691 if (error)
692 return error;
694 error = gw_get_header(gw_trans, header, 1);
695 if (error)
696 return error;
698 tree_html = gw_get_repo_tree(gw_trans);
700 if (tree_html == NULL)
701 tree_html = strdup("");
703 if ((asprintf(&tree_html_disp, tree_header,
704 gw_gen_age_header(gw_get_time_str(header->committer_time, TM_LONG)),
705 gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
706 tree_html)) == -1)
707 return got_error_from_errno("asprintf");
709 if ((asprintf(&tree, tree_wrapper, tree_html_disp)) == -1)
710 return got_error_from_errno("asprintf");
712 khttp_puts(gw_trans->gw_req, tree);
713 got_ref_list_free(&header->refs);
714 gw_free_headers(header);
715 free(tree_html_disp);
716 free(tree_html);
717 free(tree);
718 return error;
721 static const struct got_error *
722 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
724 const struct got_error *error = NULL;
725 DIR *dt;
726 char *dir_test;
727 int opened = 0;
729 if ((asprintf(&dir_test, "%s/%s/%s",
730 gw_trans->gw_conf->got_repos_path, gw_dir->name,
731 GOTWEB_GIT_DIR)) == -1)
732 return got_error_from_errno("asprintf");
734 dt = opendir(dir_test);
735 if (dt == NULL) {
736 free(dir_test);
737 } else {
738 gw_dir->path = strdup(dir_test);
739 opened = 1;
740 goto done;
743 if ((asprintf(&dir_test, "%s/%s/%s",
744 gw_trans->gw_conf->got_repos_path, gw_dir->name,
745 GOTWEB_GOT_DIR)) == -1)
746 return got_error_from_errno("asprintf");
748 dt = opendir(dir_test);
749 if (dt == NULL)
750 free(dir_test);
751 else {
752 opened = 1;
753 error = got_error(GOT_ERR_NOT_GIT_REPO);
754 goto errored;
757 if ((asprintf(&dir_test, "%s/%s",
758 gw_trans->gw_conf->got_repos_path, gw_dir->name)) == -1)
759 return got_error_from_errno("asprintf");
761 gw_dir->path = strdup(dir_test);
763 done:
764 gw_dir->description = gw_get_repo_description(gw_trans,
765 gw_dir->path);
766 gw_dir->owner = gw_get_repo_owner(gw_trans, gw_dir->path);
767 gw_dir->age = gw_get_repo_age(gw_trans, gw_dir->path, "refs/heads",
768 TM_DIFF);
769 gw_dir->url = gw_get_clone_url(gw_trans, gw_dir->path);
771 errored:
772 free(dir_test);
773 if (opened)
774 closedir(dt);
775 return error;
778 static const struct got_error *
779 gw_load_got_paths(struct gw_trans *gw_trans)
781 const struct got_error *error = NULL;
782 DIR *d;
783 struct dirent **sd_dent;
784 struct gw_dir *gw_dir;
785 struct stat st;
786 unsigned int d_cnt, d_i;
788 d = opendir(gw_trans->gw_conf->got_repos_path);
789 if (d == NULL) {
790 error = got_error_from_errno2("opendir",
791 gw_trans->gw_conf->got_repos_path);
792 return error;
795 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
796 alphasort);
797 if (d_cnt == -1) {
798 error = got_error_from_errno2("scandir",
799 gw_trans->gw_conf->got_repos_path);
800 return error;
803 for (d_i = 0; d_i < d_cnt; d_i++) {
804 if (gw_trans->gw_conf->got_max_repos > 0 &&
805 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
806 break; /* account for parent and self */
808 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
809 strcmp(sd_dent[d_i]->d_name, "..") == 0)
810 continue;
812 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
813 return got_error_from_errno("gw_dir malloc");
815 error = gw_load_got_path(gw_trans, gw_dir);
816 if (error && error->code == GOT_ERR_NOT_GIT_REPO)
817 continue;
818 else if (error)
819 return error;
821 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
822 !got_path_dir_is_empty(gw_dir->path)) {
823 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
824 entry);
825 gw_trans->repos_total++;
829 closedir(d);
830 return error;
833 static const struct got_error *
834 gw_parse_querystring(struct gw_trans *gw_trans)
836 const struct got_error *error = NULL;
837 struct kpair *p;
838 struct gw_query_action *action = NULL;
839 unsigned int i;
841 if (gw_trans->gw_req->fieldnmap[0]) {
842 error = got_error_from_errno("bad parse");
843 return error;
844 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
845 /* define gw_trans->repo_path */
846 if ((asprintf(&gw_trans->repo_name, "%s", p->parsed.s)) == -1)
847 return got_error_from_errno("asprintf");
849 if ((asprintf(&gw_trans->repo_path, "%s/%s",
850 gw_trans->gw_conf->got_repos_path, p->parsed.s)) == -1)
851 return got_error_from_errno("asprintf");
853 /* get action and set function */
854 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
855 for (i = 0; i < nitems(gw_query_funcs); i++) {
856 action = &gw_query_funcs[i];
857 if (action->func_name == NULL)
858 continue;
860 if (strcmp(action->func_name,
861 p->parsed.s) == 0) {
862 gw_trans->action = i;
863 if ((asprintf(&gw_trans->action_name,
864 "%s", action->func_name)) == -1)
865 return
866 got_error_from_errno(
867 "asprintf");
869 break;
872 action = NULL;
875 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
876 if ((asprintf(&gw_trans->commit, "%s",
877 p->parsed.s)) == -1)
878 return got_error_from_errno("asprintf");
880 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
881 if ((asprintf(&gw_trans->repo_file, "%s",
882 p->parsed.s)) == -1)
883 return got_error_from_errno("asprintf");
885 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER]))
886 if ((asprintf(&gw_trans->repo_folder, "%s",
887 p->parsed.s)) == -1)
888 return got_error_from_errno("asprintf");
890 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
891 if ((asprintf(&gw_trans->headref, "%s",
892 p->parsed.s)) == -1)
893 return got_error_from_errno("asprintf");
895 if (action == NULL) {
896 error = got_error_from_errno("invalid action");
897 return error;
899 if ((gw_trans->gw_dir =
900 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
901 return got_error_from_errno("gw_dir malloc");
903 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
904 if (error)
905 return error;
906 } else
907 gw_trans->action = GW_INDEX;
909 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
910 gw_trans->page = p->parsed.i;
912 /* if (gw_trans->action == GW_RAW) */
913 /* gw_trans->mime = KMIME_TEXT_PLAIN; */
915 return error;
918 static struct gw_dir *
919 gw_init_gw_dir(char *dir)
921 struct gw_dir *gw_dir;
923 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
924 return NULL;
926 if ((asprintf(&gw_dir->name, "%s", dir)) == -1)
927 return NULL;
929 return gw_dir;
932 static void
933 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
935 khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
936 khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
937 khttps[code]);
938 khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
939 kmimetypes[mime]);
940 khttp_head(gw_trans->gw_req, "X-Content-Type-Options", "nosniff");
941 khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
942 khttp_head(gw_trans->gw_req, "X-XSS-Protection", "1; mode=block");
943 khttp_body(gw_trans->gw_req);
946 static void
947 gw_display_index(struct gw_trans *gw_trans, const struct got_error *err)
949 gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
950 khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
952 if (err)
953 khttp_puts(gw_trans->gw_req, err->msg);
954 else
955 khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
956 gw_query_funcs[gw_trans->action].template);
958 khtml_close(gw_trans->gw_html_req);
961 static int
962 gw_template(size_t key, void *arg)
964 const struct got_error *error = NULL;
965 struct gw_trans *gw_trans = arg;
966 char *gw_got_link, *gw_site_link;
967 char *site_owner_name, *site_owner_name_h;
969 switch (key) {
970 case (TEMPL_HEAD):
971 khttp_puts(gw_trans->gw_req, head);
972 break;
973 case(TEMPL_HEADER):
974 gw_got_link = gw_get_got_link(gw_trans);
975 if (gw_got_link != NULL)
976 khttp_puts(gw_trans->gw_req, gw_got_link);
978 free(gw_got_link);
979 break;
980 case (TEMPL_SITEPATH):
981 gw_site_link = gw_get_site_link(gw_trans);
982 if (gw_site_link != NULL)
983 khttp_puts(gw_trans->gw_req, gw_site_link);
985 free(gw_site_link);
986 break;
987 case(TEMPL_TITLE):
988 if (gw_trans->gw_conf->got_site_name != NULL)
989 khtml_puts(gw_trans->gw_html_req,
990 gw_trans->gw_conf->got_site_name);
992 break;
993 case (TEMPL_SEARCH):
994 khttp_puts(gw_trans->gw_req, search);
995 break;
996 case(TEMPL_SITEOWNER):
997 if (gw_trans->gw_conf->got_site_owner != NULL &&
998 gw_trans->gw_conf->got_show_site_owner) {
999 site_owner_name =
1000 gw_html_escape(gw_trans->gw_conf->got_site_owner);
1001 if ((asprintf(&site_owner_name_h, site_owner,
1002 site_owner_name))
1003 == -1)
1004 return 0;
1006 khttp_puts(gw_trans->gw_req, site_owner_name_h);
1007 free(site_owner_name);
1008 free(site_owner_name_h);
1010 break;
1011 case(TEMPL_CONTENT):
1012 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1013 if (error)
1014 khttp_puts(gw_trans->gw_req, error->msg);
1016 break;
1017 default:
1018 return 0;
1019 break;
1021 return 1;
1024 static char *
1025 gw_gen_commit_header(char *str1, char *str2)
1027 char *return_html = NULL, *ref_str = NULL;
1029 if (strcmp(str2, "") != 0) {
1030 if ((asprintf(&ref_str, "(%s)", str2)) == -1) {
1031 return_html = strdup("");
1032 return return_html;
1034 } else
1035 ref_str = strdup("");
1038 if ((asprintf(&return_html, header_commit_html, str1, ref_str)) == -1)
1039 return_html = strdup("");
1041 free(ref_str);
1042 return return_html;
1045 static char *
1046 gw_gen_diff_header(char *str1, char *str2)
1048 char *return_html = NULL;
1050 if ((asprintf(&return_html, header_diff_html, str1, str2)) == -1)
1051 return_html = strdup("");
1053 return return_html;
1056 static char *
1057 gw_gen_author_header(char *str)
1059 char *return_html = NULL;
1061 if ((asprintf(&return_html, header_author_html, str)) == -1)
1062 return_html = strdup("");
1064 return return_html;
1067 static char *
1068 gw_gen_committer_header(char *str)
1070 char *return_html = NULL;
1072 if ((asprintf(&return_html, header_committer_html, str)) == -1)
1073 return_html = strdup("");
1075 return return_html;
1078 static char *
1079 gw_gen_age_header(char *str)
1081 char *return_html = NULL;
1083 if ((asprintf(&return_html, header_age_html, str)) == -1)
1084 return_html = strdup("");
1086 return return_html;
1089 static char *
1090 gw_gen_commit_msg_header(char *str)
1092 char *return_html = NULL;
1094 if ((asprintf(&return_html, header_commit_msg_html, str)) == -1)
1095 return_html = strdup("");
1097 return return_html;
1100 static char *
1101 gw_gen_tree_header(char *str)
1103 char *return_html = NULL;
1105 if ((asprintf(&return_html, header_tree_html, str)) == -1)
1106 return_html = strdup("");
1108 return return_html;
1111 static char *
1112 gw_get_repo_description(struct gw_trans *gw_trans, char *dir)
1114 FILE *f;
1115 char *description = NULL, *d_file = NULL;
1116 unsigned int len;
1118 if (gw_trans->gw_conf->got_show_repo_description == false)
1119 goto err;
1121 if ((asprintf(&d_file, "%s/description", dir)) == -1)
1122 goto err;
1124 if ((f = fopen(d_file, "r")) == NULL)
1125 goto err;
1127 fseek(f, 0, SEEK_END);
1128 len = ftell(f) + 1;
1129 fseek(f, 0, SEEK_SET);
1130 if ((description = calloc(len, sizeof(char *))) == NULL)
1131 goto err;
1133 fread(description, 1, len, f);
1134 fclose(f);
1135 free(d_file);
1136 return description;
1137 err:
1138 if ((asprintf(&description, "%s", "")) == -1)
1139 return NULL;
1141 return description;
1144 static char *
1145 gw_get_time_str(time_t committer_time, int ref_tm)
1147 struct tm tm;
1148 time_t diff_time;
1149 char *years = "years ago", *months = "months ago";
1150 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
1151 char *minutes = "minutes ago", *seconds = "seconds ago";
1152 char *now = "right now";
1153 char *repo_age, *s;
1154 char datebuf[29];
1156 switch (ref_tm) {
1157 case TM_DIFF:
1158 diff_time = time(NULL) - committer_time;
1159 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1160 if ((asprintf(&repo_age, "%lld %s",
1161 (diff_time / 60 / 60 / 24 / 365), years)) == -1)
1162 return NULL;
1163 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1164 if ((asprintf(&repo_age, "%lld %s",
1165 (diff_time / 60 / 60 / 24 / (365 / 12)),
1166 months)) == -1)
1167 return NULL;
1168 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1169 if ((asprintf(&repo_age, "%lld %s",
1170 (diff_time / 60 / 60 / 24 / 7), weeks)) == -1)
1171 return NULL;
1172 } else if (diff_time > 60 * 60 * 24 * 2) {
1173 if ((asprintf(&repo_age, "%lld %s",
1174 (diff_time / 60 / 60 / 24), days)) == -1)
1175 return NULL;
1176 } else if (diff_time > 60 * 60 * 2) {
1177 if ((asprintf(&repo_age, "%lld %s",
1178 (diff_time / 60 / 60), hours)) == -1)
1179 return NULL;
1180 } else if (diff_time > 60 * 2) {
1181 if ((asprintf(&repo_age, "%lld %s", (diff_time / 60),
1182 minutes)) == -1)
1183 return NULL;
1184 } else if (diff_time > 2) {
1185 if ((asprintf(&repo_age, "%lld %s", diff_time,
1186 seconds)) == -1)
1187 return NULL;
1188 } else {
1189 if ((asprintf(&repo_age, "%s", now)) == -1)
1190 return NULL;
1192 break;
1193 case TM_LONG:
1194 if (gmtime_r(&committer_time, &tm) == NULL)
1195 return NULL;
1197 s = asctime_r(&tm, datebuf);
1198 if (s == NULL)
1199 return NULL;
1201 if ((asprintf(&repo_age, "%s UTC", datebuf)) == -1)
1202 return NULL;
1203 break;
1205 return repo_age;
1208 static char *
1209 gw_get_repo_age(struct gw_trans *gw_trans, char *dir, char *repo_ref,
1210 int ref_tm)
1212 const struct got_error *error = NULL;
1213 struct got_object_id *id = NULL;
1214 struct got_repository *repo = NULL;
1215 struct got_commit_object *commit = NULL;
1216 struct got_reflist_head refs;
1217 struct got_reflist_entry *re;
1218 struct got_reference *head_ref;
1219 int is_head = 0;
1220 time_t committer_time = 0, cmp_time = 0;
1221 const char *refname;
1222 char *repo_age = NULL;
1224 if (repo_ref == NULL)
1225 return NULL;
1227 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
1228 is_head = 1;
1230 SIMPLEQ_INIT(&refs);
1231 if (gw_trans->gw_conf->got_show_repo_age == false) {
1232 if ((asprintf(&repo_age, "")) == -1)
1233 return NULL;
1234 return repo_age;
1237 error = got_repo_open(&repo, dir, NULL);
1238 if (error)
1239 goto err;
1241 if (is_head)
1242 error = got_ref_list(&refs, repo, "refs/heads",
1243 got_ref_cmp_by_name, NULL);
1244 else
1245 error = got_ref_list(&refs, repo, repo_ref,
1246 got_ref_cmp_by_name, NULL);
1247 if (error)
1248 goto err;
1250 SIMPLEQ_FOREACH(re, &refs, entry) {
1251 if (is_head)
1252 refname = strdup(repo_ref);
1253 else
1254 refname = got_ref_get_name(re->ref);
1255 error = got_ref_open(&head_ref, repo, refname, 0);
1256 if (error)
1257 goto err;
1259 error = got_ref_resolve(&id, repo, head_ref);
1260 got_ref_close(head_ref);
1261 if (error)
1262 goto err;
1264 error = got_object_open_as_commit(&commit, repo, id);
1265 if (error)
1266 goto err;
1268 committer_time =
1269 got_object_commit_get_committer_time(commit);
1271 if (cmp_time < committer_time)
1272 cmp_time = committer_time;
1275 if (cmp_time != 0) {
1276 committer_time = cmp_time;
1277 repo_age = gw_get_time_str(committer_time, ref_tm);
1278 } else
1279 if ((asprintf(&repo_age, "")) == -1)
1280 return NULL;
1281 got_ref_list_free(&refs);
1282 free(id);
1283 return repo_age;
1284 err:
1285 if ((asprintf(&repo_age, "%s", error->msg)) == -1)
1286 return NULL;
1288 return repo_age;
1291 static char *
1292 gw_get_diff(struct gw_trans *gw_trans, struct gw_header *header)
1294 const struct got_error *error;
1295 FILE *f = NULL;
1296 struct got_object_id *id1 = NULL, *id2 = NULL;
1297 struct buf *diffbuf = NULL;
1298 char *label1 = NULL, *label2 = NULL, *diff_html = NULL, *buf = NULL,
1299 *buf_color = NULL, *n_buf = NULL, *newline = NULL;
1300 int obj_type;
1301 size_t newsize;
1303 f = got_opentemp();
1304 if (f == NULL)
1305 return NULL;
1307 error = buf_alloc(&diffbuf, 0);
1308 if (error)
1309 return NULL;
1311 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
1312 if (error)
1313 goto done;
1315 if (strncmp(header->parent_id, "/dev/null", 9) != 0) {
1316 error = got_repo_match_object_id(&id1, &label1,
1317 header->parent_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
1318 if (error)
1319 goto done;
1322 error = got_repo_match_object_id(&id2, &label2,
1323 header->commit_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
1324 if (error)
1325 goto done;
1327 error = got_object_get_type(&obj_type, header->repo, id2);
1328 if (error)
1329 goto done;
1330 switch (obj_type) {
1331 case GOT_OBJ_TYPE_BLOB:
1332 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
1333 header->repo, f);
1334 break;
1335 case GOT_OBJ_TYPE_TREE:
1336 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
1337 header->repo, f);
1338 break;
1339 case GOT_OBJ_TYPE_COMMIT:
1340 error = got_diff_objects_as_commits(id1, id2, 3, 0,
1341 header->repo, f);
1342 break;
1343 default:
1344 error = got_error(GOT_ERR_OBJ_TYPE);
1347 if ((buf = calloc(128, sizeof(char *))) == NULL)
1348 goto done;
1350 fseek(f, 0, SEEK_SET);
1352 while ((fgets(buf, 2048, f)) != NULL) {
1353 n_buf = buf;
1354 while (*n_buf == '\n')
1355 n_buf++;
1356 newline = strchr(n_buf, '\n');
1357 if (newline)
1358 *newline = ' ';
1360 buf_color = gw_colordiff_line(gw_html_escape(n_buf));
1361 if (buf_color == NULL)
1362 continue;
1364 error = buf_puts(&newsize, diffbuf, buf_color);
1365 if (error)
1366 return NULL;
1368 error = buf_puts(&newsize, diffbuf, div_end);
1369 if (error)
1370 return NULL;
1373 if (buf_len(diffbuf) > 0) {
1374 error = buf_putc(diffbuf, '\0');
1375 diff_html = strdup(buf_get(diffbuf));
1377 done:
1378 fclose(f);
1379 free(buf_color);
1380 free(buf);
1381 free(diffbuf);
1382 free(label1);
1383 free(label2);
1384 free(id1);
1385 free(id2);
1387 if (error)
1388 return NULL;
1389 else
1390 return diff_html;
1393 static char *
1394 gw_get_repo_owner(struct gw_trans *gw_trans, char *dir)
1396 FILE *f;
1397 char *owner = NULL, *d_file = NULL;
1398 char *gotweb = "[gotweb]", *gitweb = "[gitweb]", *gw_owner = "owner";
1399 char *comp, *pos, *buf;
1400 unsigned int i;
1402 if (gw_trans->gw_conf->got_show_repo_owner == false)
1403 goto err;
1405 if ((asprintf(&d_file, "%s/config", dir)) == -1)
1406 goto err;
1408 if ((f = fopen(d_file, "r")) == NULL)
1409 goto err;
1411 if ((buf = calloc(128, sizeof(char *))) == NULL)
1412 goto err;
1414 while ((fgets(buf, 128, f)) != NULL) {
1415 if ((pos = strstr(buf, gotweb)) != NULL)
1416 break;
1418 if ((pos = strstr(buf, gitweb)) != NULL)
1419 break;
1422 if (pos == NULL)
1423 goto err;
1425 do {
1426 fgets(buf, 128, f);
1427 } while ((comp = strcasestr(buf, gw_owner)) == NULL);
1429 if (comp == NULL)
1430 goto err;
1432 if (strncmp(gw_owner, comp, strlen(gw_owner)) != 0)
1433 goto err;
1435 for (i = 0; i < 2; i++) {
1436 owner = strsep(&buf, "\"");
1439 if (owner == NULL)
1440 goto err;
1442 fclose(f);
1443 free(d_file);
1444 return owner;
1445 err:
1446 if ((asprintf(&owner, "%s", "")) == -1)
1447 return NULL;
1449 return owner;
1452 static char *
1453 gw_get_clone_url(struct gw_trans *gw_trans, char *dir)
1455 FILE *f;
1456 char *url = NULL, *d_file = NULL;
1457 unsigned int len;
1459 if ((asprintf(&d_file, "%s/cloneurl", dir)) == -1)
1460 return NULL;
1462 if ((f = fopen(d_file, "r")) == NULL)
1463 return NULL;
1465 fseek(f, 0, SEEK_END);
1466 len = ftell(f) + 1;
1467 fseek(f, 0, SEEK_SET);
1469 if ((url = calloc(len, sizeof(char *))) == NULL)
1470 return NULL;
1472 fread(url, 1, len, f);
1473 fclose(f);
1474 free(d_file);
1475 return url;
1478 static char *
1479 gw_get_repo_tags(struct gw_trans *gw_trans, int limit, int tag_type)
1481 const struct got_error *error = NULL;
1482 struct got_repository *repo = NULL;
1483 struct got_reflist_head refs;
1484 struct got_reflist_entry *re;
1485 char *tags = NULL, *tag_row = NULL, *tags_navs_disp = NULL,
1486 *age = NULL;
1487 char *newline;
1488 struct buf *diffbuf = NULL;
1489 size_t newsize;
1491 error = buf_alloc(&diffbuf, 0);
1492 if (error)
1493 return NULL;
1494 SIMPLEQ_INIT(&refs);
1496 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1497 if (error)
1498 goto done;
1500 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
1501 if (error)
1502 goto done;
1504 SIMPLEQ_FOREACH(re, &refs, entry) {
1505 const char *refname;
1506 char *refstr, *tag_commit0, *tag_commit, *id_str;
1507 time_t tagger_time;
1508 struct got_object_id *id;
1509 struct got_tag_object *tag;
1511 refname = got_ref_get_name(re->ref);
1512 if (strncmp(refname, "refs/tags/", 10) != 0)
1513 continue;
1514 refname += 10;
1515 refstr = got_ref_to_str(re->ref);
1516 if (refstr == NULL) {
1517 error = got_error_from_errno("got_ref_to_str");
1518 goto done;
1521 error = got_ref_resolve(&id, repo, re->ref);
1522 if (error)
1523 goto done;
1524 error = got_object_open_as_tag(&tag, repo, id);
1525 free(id);
1526 if (error)
1527 goto done;
1529 tagger_time = got_object_tag_get_tagger_time(tag);
1531 error = got_object_id_str(&id_str,
1532 got_object_tag_get_object_id(tag));
1533 if (error)
1534 goto done;
1536 tag_commit0 = strdup(got_object_tag_get_message(tag));
1538 if (tag_commit0 == NULL) {
1539 error = got_error_from_errno("strdup");
1540 goto done;
1543 tag_commit = tag_commit0;
1544 while (*tag_commit == '\n')
1545 tag_commit++;
1547 switch (tag_type) {
1548 case TAGBRIEF:
1549 newline = strchr(tag_commit, '\n');
1550 if (newline)
1551 *newline = '\0';
1553 if ((asprintf(&age, "%s", gw_get_time_str(tagger_time,
1554 TM_DIFF))) == -1) {
1555 error = got_error_from_errno("asprintf");
1556 goto done;
1559 if ((asprintf(&tags_navs_disp, tags_navs,
1560 gw_trans->repo_name, id_str, gw_trans->repo_name,
1561 id_str, gw_trans->repo_name, id_str,
1562 gw_trans->repo_name, id_str)) == -1) {
1563 error = got_error_from_errno("asprintf");
1564 goto done;
1567 if ((asprintf(&tag_row, tags_row, age, refname,
1568 tag_commit, tags_navs_disp)) == -1) {
1569 error = got_error_from_errno("asprintf");
1570 goto done;
1573 free(tags_navs_disp);
1574 break;
1575 case TAGFULL:
1576 break;
1577 default:
1578 break;
1581 got_object_tag_close(tag);
1583 error = buf_puts(&newsize, diffbuf, tag_row);
1585 free(id_str);
1586 free(refstr);
1587 free(age);
1588 free(tag_commit0);
1589 free(tag_row);
1591 if (error || (limit && --limit == 0))
1592 break;
1595 if (buf_len(diffbuf) > 0) {
1596 error = buf_putc(diffbuf, '\0');
1597 tags = strdup(buf_get(diffbuf));
1599 done:
1600 buf_free(diffbuf);
1601 got_ref_list_free(&refs);
1602 if (repo)
1603 got_repo_close(repo);
1604 if (error)
1605 return NULL;
1606 else
1607 return tags;
1610 static void
1611 gw_free_headers(struct gw_header *header)
1613 free(header->id);
1614 free(header->path);
1615 if (header->commit != NULL)
1616 got_object_commit_close(header->commit);
1617 if (header->repo)
1618 got_repo_close(header->repo);
1619 free(header->refs_str);
1620 free(header->commit_id);
1621 free(header->parent_id);
1622 free(header->tree_id);
1623 free(header->author);
1624 free(header->committer);
1625 free(header->commit_msg);
1628 static struct gw_header *
1629 gw_init_header()
1631 struct gw_header *header;
1633 if ((header = malloc(sizeof(*header))) == NULL)
1634 return NULL;
1636 header->repo = NULL;
1637 header->commit = NULL;
1638 header->id = NULL;
1639 header->path = NULL;
1641 return header;
1644 static const struct got_error *
1645 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
1646 int limit)
1648 const struct got_error *error = NULL;
1649 struct got_commit_graph *graph = NULL;
1651 error = got_commit_graph_open(&graph, header->path, 0);
1652 if (error)
1653 goto done;
1655 error = got_commit_graph_iter_start(graph, header->id, header->repo,
1656 NULL, NULL);
1657 if (error)
1658 goto done;
1660 for (;;) {
1661 error = got_commit_graph_iter_next(&header->id, graph,
1662 header->repo, NULL, NULL);
1663 if (error) {
1664 if (error->code == GOT_ERR_ITER_COMPLETED)
1665 error = NULL;
1666 goto done;
1668 if (header->id == NULL)
1669 goto done;
1671 error = got_object_open_as_commit(&header->commit, header->repo,
1672 header->id);
1673 if (error)
1674 goto done;
1676 error = gw_get_commit(gw_trans, header);
1677 if (limit > 1) {
1678 struct gw_header *n_header = NULL;
1679 if ((n_header = gw_init_header()) == NULL)
1680 error = got_error_from_errno("malloc");
1682 n_header->refs_str = strdup(header->refs_str);
1683 n_header->commit_id = strdup(header->commit_id);
1684 n_header->parent_id = strdup(header->parent_id);
1685 n_header->tree_id = strdup(header->tree_id);
1686 n_header->author = strdup(header->author);
1687 n_header->committer = strdup(header->committer);
1688 n_header->commit_msg = strdup(header->commit_msg);
1689 n_header->committer_time = header->committer_time;
1690 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
1691 entry);
1693 if (error || (limit && --limit == 0))
1694 break;
1696 done:
1697 if (graph)
1698 got_commit_graph_close(graph);
1699 return error;
1702 static const struct got_error *
1703 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
1705 const struct got_error *error = NULL;
1706 struct got_reflist_entry *re;
1707 struct got_object_id *id2 = NULL;
1708 struct got_object_qid *parent_id;
1709 char *refs_str = NULL,
1710 *commit_msg = NULL, *commit_msg0;
1712 /*print commit*/
1713 SIMPLEQ_FOREACH(re, &header->refs, entry) {
1714 char *s;
1715 const char *name;
1716 struct got_tag_object *tag = NULL;
1717 int cmp;
1719 name = got_ref_get_name(re->ref);
1720 if (strcmp(name, GOT_REF_HEAD) == 0)
1721 continue;
1722 if (strncmp(name, "refs/", 5) == 0)
1723 name += 5;
1724 if (strncmp(name, "got/", 4) == 0)
1725 continue;
1726 if (strncmp(name, "heads/", 6) == 0)
1727 name += 6;
1728 if (strncmp(name, "remotes/", 8) == 0)
1729 name += 8;
1730 if (strncmp(name, "tags/", 5) == 0) {
1731 error = got_object_open_as_tag(&tag, header->repo,
1732 re->id);
1733 if (error) {
1734 if (error->code != GOT_ERR_OBJ_TYPE)
1735 continue;
1737 * Ref points at something other
1738 * than a tag.
1740 error = NULL;
1741 tag = NULL;
1744 cmp = got_object_id_cmp(tag ?
1745 got_object_tag_get_object_id(tag) : re->id, header->id);
1746 if (tag)
1747 got_object_tag_close(tag);
1748 if (cmp != 0)
1749 continue;
1750 s = refs_str;
1751 if ((asprintf(&refs_str, "%s%s%s", s ? s : "",
1752 s ? ", " : "", name)) == -1) {
1753 error = got_error_from_errno("asprintf");
1754 free(s);
1755 return error;
1757 header->refs_str = strdup(refs_str);
1758 free(s);
1761 if (refs_str == NULL)
1762 header->refs_str = strdup("");
1763 free(refs_str);
1765 error = got_object_id_str(&header->commit_id, header->id);
1766 if (error)
1767 return error;
1769 error = got_object_id_str(&header->tree_id,
1770 got_object_commit_get_tree_id(header->commit));
1771 if (error)
1772 return error;
1774 if (gw_trans->action == GW_DIFF) {
1775 parent_id = SIMPLEQ_FIRST(
1776 got_object_commit_get_parent_ids(header->commit));
1777 if (parent_id != NULL) {
1778 id2 = got_object_id_dup(parent_id->id);
1779 free (parent_id);
1780 error = got_object_id_str(&header->parent_id, id2);
1781 if (error)
1782 return error;
1783 free(id2);
1784 } else
1785 header->parent_id = strdup("/dev/null");
1786 } else
1787 header->parent_id = strdup("");
1789 header->committer_time =
1790 got_object_commit_get_committer_time(header->commit);
1791 header->author = strdup(got_object_commit_get_author(header->commit));
1792 header->committer =
1793 strdup(got_object_commit_get_committer(header->commit));
1795 error = got_object_commit_get_logmsg(&commit_msg0, header->commit);
1796 if (error)
1797 return error;
1799 commit_msg = commit_msg0;
1800 while (*commit_msg == '\n')
1801 commit_msg++;
1803 header->commit_msg = strdup(commit_msg);
1804 free(commit_msg0);
1805 return error;
1808 static const struct got_error *
1809 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
1811 const struct got_error *error = NULL;
1812 char *in_repo_path = NULL;
1814 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
1815 if (error)
1816 return error;
1818 SIMPLEQ_INIT(&header->refs);
1820 if (gw_trans->commit == NULL) {
1821 struct got_reference *head_ref;
1822 error = got_ref_open(&head_ref, header->repo,
1823 gw_trans->headref, 0);
1824 if (error)
1825 return error;
1827 error = got_ref_resolve(&header->id, header->repo, head_ref);
1828 got_ref_close(head_ref);
1829 if (error)
1830 return error;
1832 error = got_object_open_as_commit(&header->commit,
1833 header->repo, header->id);
1834 } else {
1835 struct got_reference *ref;
1836 error = got_ref_open(&ref, header->repo, gw_trans->commit, 0);
1837 if (error == NULL) {
1838 int obj_type;
1839 error = got_ref_resolve(&header->id, header->repo, ref);
1840 got_ref_close(ref);
1841 if (error)
1842 return error;
1843 error = got_object_get_type(&obj_type, header->repo,
1844 header->id);
1845 if (error)
1846 return error;
1847 if (obj_type == GOT_OBJ_TYPE_TAG) {
1848 struct got_tag_object *tag;
1849 error = got_object_open_as_tag(&tag,
1850 header->repo, header->id);
1851 if (error)
1852 return error;
1853 if (got_object_tag_get_object_type(tag) !=
1854 GOT_OBJ_TYPE_COMMIT) {
1855 got_object_tag_close(tag);
1856 error = got_error(GOT_ERR_OBJ_TYPE);
1857 return error;
1859 free(header->id);
1860 header->id = got_object_id_dup(
1861 got_object_tag_get_object_id(tag));
1862 if (header->id == NULL)
1863 error = got_error_from_errno(
1864 "got_object_id_dup");
1865 got_object_tag_close(tag);
1866 if (error)
1867 return error;
1868 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1869 error = got_error(GOT_ERR_OBJ_TYPE);
1870 return error;
1872 error = got_object_open_as_commit(&header->commit,
1873 header->repo, header->id);
1874 if (error)
1875 return error;
1877 if (header->commit == NULL) {
1878 error = got_repo_match_object_id_prefix(&header->id,
1879 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
1880 header->repo);
1881 if (error)
1882 return error;
1884 error = got_repo_match_object_id_prefix(&header->id,
1885 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
1886 header->repo);
1889 error = got_repo_map_path(&in_repo_path, header->repo,
1890 gw_trans->repo_path, 1);
1891 if (error)
1892 return error;
1894 if (in_repo_path) {
1895 header->path = strdup(in_repo_path);
1897 free(in_repo_path);
1899 error = got_ref_list(&header->refs, header->repo, NULL,
1900 got_ref_cmp_by_name, NULL);
1901 if (error)
1902 return error;
1904 error = gw_get_commits(gw_trans, header, limit);
1905 return error;
1908 struct blame_line {
1909 int annotated;
1910 char *id_str;
1911 char *committer;
1912 char datebuf[11]; /* YYYY-MM-DD + NUL */
1915 struct gw_blame_cb_args {
1916 struct blame_line *lines;
1917 int nlines;
1918 int nlines_prec;
1919 int lineno_cur;
1920 off_t *line_offsets;
1921 FILE *f;
1922 struct got_repository *repo;
1923 struct gw_trans *gw_trans;
1924 struct buf *blamebuf;
1927 static const struct got_error *
1928 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1930 const struct got_error *err = NULL;
1931 struct gw_blame_cb_args *a = arg;
1932 struct blame_line *bline;
1933 char *line = NULL;
1934 size_t linesize = 0, newsize;
1935 struct got_commit_object *commit = NULL;
1936 off_t offset;
1937 struct tm tm;
1938 time_t committer_time;
1940 if (nlines != a->nlines ||
1941 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1942 return got_error(GOT_ERR_RANGE);
1944 if (lineno == -1)
1945 return NULL; /* no change in this commit */
1947 /* Annotate this line. */
1948 bline = &a->lines[lineno - 1];
1949 if (bline->annotated)
1950 return NULL;
1951 err = got_object_id_str(&bline->id_str, id);
1952 if (err)
1953 return err;
1955 err = got_object_open_as_commit(&commit, a->repo, id);
1956 if (err)
1957 goto done;
1959 bline->committer = strdup(got_object_commit_get_committer(commit));
1960 if (bline->committer == NULL) {
1961 err = got_error_from_errno("strdup");
1962 goto done;
1965 committer_time = got_object_commit_get_committer_time(commit);
1966 if (localtime_r(&committer_time, &tm) == NULL)
1967 return got_error_from_errno("localtime_r");
1968 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
1969 &tm) >= sizeof(bline->datebuf)) {
1970 err = got_error(GOT_ERR_NO_SPACE);
1971 goto done;
1973 bline->annotated = 1;
1975 /* Print lines annotated so far. */
1976 bline = &a->lines[a->lineno_cur - 1];
1977 if (!bline->annotated)
1978 goto done;
1980 offset = a->line_offsets[a->lineno_cur - 1];
1981 if (fseeko(a->f, offset, SEEK_SET) == -1) {
1982 err = got_error_from_errno("fseeko");
1983 goto done;
1986 while (bline->annotated) {
1987 char *smallerthan, *at, *nl, *committer, *blame_row = NULL,
1988 *line_escape = NULL;
1989 size_t len;
1991 if (getline(&line, &linesize, a->f) == -1) {
1992 if (ferror(a->f))
1993 err = got_error_from_errno("getline");
1994 break;
1997 committer = bline->committer;
1998 smallerthan = strchr(committer, '<');
1999 if (smallerthan && smallerthan[1] != '\0')
2000 committer = smallerthan + 1;
2001 at = strchr(committer, '@');
2002 if (at)
2003 *at = '\0';
2004 len = strlen(committer);
2005 if (len >= 9)
2006 committer[8] = '\0';
2008 nl = strchr(line, '\n');
2009 if (nl)
2010 *nl = '\0';
2012 if (strcmp(line, "") != 0)
2013 line_escape = strdup(gw_html_escape(line));
2014 else
2015 line_escape = strdup("");
2017 asprintf(&blame_row, blame_line, a->nlines_prec,
2018 a->lineno_cur, bline->id_str, bline->datebuf, committer,
2019 line_escape);
2020 a->lineno_cur++;
2021 err = buf_puts(&newsize, a->blamebuf, blame_row);
2022 if (err)
2023 return err;
2025 bline = &a->lines[a->lineno_cur - 1];
2026 free(line_escape);
2027 free(blame_row);
2029 done:
2030 if (commit)
2031 got_object_commit_close(commit);
2032 free(line);
2033 return err;
2036 static char*
2037 gw_get_file_blame(struct gw_trans *gw_trans)
2039 const struct got_error *error = NULL;
2040 struct got_repository *repo = NULL;
2041 struct got_object_id *obj_id = NULL;
2042 struct got_object_id *commit_id = NULL;
2043 struct got_blob_object *blob = NULL;
2044 char *blame_html = NULL, *path = NULL, *in_repo_path = NULL,
2045 *folder = NULL;
2046 struct gw_blame_cb_args bca;
2047 int i, obj_type;
2048 size_t filesize;
2050 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2051 if (error)
2052 goto done;
2054 if (gw_trans->repo_folder != NULL) {
2055 if ((asprintf(&folder, "%s/", gw_trans->repo_folder)) == -1) {
2056 error = got_error_from_errno("asprintf");
2057 goto done;
2059 } else
2060 folder = strdup("");
2062 if ((asprintf(&path, "%s%s", folder, gw_trans->repo_file)) == -1) {
2063 error = got_error_from_errno("asprintf");
2064 goto done;
2066 free(folder);
2068 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2069 if (error)
2070 goto done;
2072 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
2073 GOT_OBJ_TYPE_COMMIT, 1, repo);
2074 if (error)
2075 goto done;
2077 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2078 if (error)
2079 goto done;
2081 if (obj_id == NULL) {
2082 error = got_error(GOT_ERR_NO_OBJ);
2083 goto done;
2086 error = got_object_get_type(&obj_type, repo, obj_id);
2087 if (error)
2088 goto done;
2090 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2091 error = got_error(GOT_ERR_OBJ_TYPE);
2092 goto done;
2095 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2096 if (error)
2097 goto done;
2099 error = buf_alloc(&bca.blamebuf, 0);
2100 if (error)
2101 goto done;
2103 bca.f = got_opentemp();
2104 if (bca.f == NULL) {
2105 error = got_error_from_errno("got_opentemp");
2106 goto done;
2108 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2109 &bca.line_offsets, bca.f, blob);
2110 if (error || bca.nlines == 0)
2111 goto done;
2113 /* Don't include \n at EOF in the blame line count. */
2114 if (bca.line_offsets[bca.nlines - 1] == filesize)
2115 bca.nlines--;
2117 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2118 if (bca.lines == NULL) {
2119 error = got_error_from_errno("calloc");
2120 goto done;
2122 bca.lineno_cur = 1;
2123 bca.nlines_prec = 0;
2124 i = bca.nlines;
2125 while (i > 0) {
2126 i /= 10;
2127 bca.nlines_prec++;
2129 bca.repo = repo;
2130 bca.gw_trans = gw_trans;
2132 error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
2133 NULL, NULL);
2134 if (buf_len(bca.blamebuf) > 0) {
2135 error = buf_putc(bca.blamebuf, '\0');
2136 blame_html = strdup(buf_get(bca.blamebuf));
2138 done:
2139 free(bca.blamebuf);
2140 free(in_repo_path);
2141 free(commit_id);
2142 free(obj_id);
2143 free(path);
2145 if (blob)
2146 error = got_object_blob_close(blob);
2147 if (repo)
2148 error = got_repo_close(repo);
2149 if (error)
2150 return NULL;
2151 if (bca.lines) {
2152 for (i = 0; i < bca.nlines; i++) {
2153 struct blame_line *bline = &bca.lines[i];
2154 free(bline->id_str);
2155 free(bline->committer);
2157 free(bca.lines);
2159 free(bca.line_offsets);
2160 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2161 error = got_error_from_errno("fclose");
2162 if (error)
2163 return NULL;
2164 else
2165 return blame_html;
2168 static char*
2169 gw_get_repo_tree(struct gw_trans *gw_trans)
2171 const struct got_error *error = NULL;
2172 struct got_repository *repo = NULL;
2173 struct got_object_id *tree_id = NULL, *commit_id = NULL;
2174 struct got_tree_object *tree = NULL;
2175 struct buf *diffbuf = NULL;
2176 size_t newsize;
2177 char *tree_html = NULL, *path = NULL, *in_repo_path = NULL,
2178 *tree_row = NULL, *id_str;
2179 int nentries, i;
2181 error = buf_alloc(&diffbuf, 0);
2182 if (error)
2183 return NULL;
2185 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2186 if (error)
2187 goto done;
2189 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
2190 if (error)
2191 goto done;
2193 if (gw_trans->repo_folder != NULL)
2194 path = strdup(gw_trans->repo_folder);
2195 else if (in_repo_path) {
2196 free(path);
2197 path = in_repo_path;
2200 if (gw_trans->commit == NULL) {
2201 struct got_reference *head_ref;
2202 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
2203 if (error)
2204 goto done;
2206 error = got_ref_resolve(&commit_id, repo, head_ref);
2207 got_ref_close(head_ref);
2209 } else
2210 error = got_repo_match_object_id(&commit_id, NULL,
2211 gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
2212 if (error)
2213 goto done;
2215 error = got_object_id_str(&gw_trans->commit, commit_id);
2216 if (error)
2217 goto done;
2219 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
2220 if (error)
2221 goto done;
2223 error = got_object_open_as_tree(&tree, repo, tree_id);
2224 if (error)
2225 goto done;
2227 nentries = got_object_tree_get_nentries(tree);
2229 for (i = 0; i < nentries; i++) {
2230 struct got_tree_entry *te;
2231 const char *modestr = "";
2232 char *id = NULL, *url_html = NULL;
2234 te = got_object_tree_get_entry(tree, i);
2236 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
2237 if (error)
2238 goto done;
2240 if ((asprintf(&id, "%s", id_str)) == -1) {
2241 error = got_error_from_errno("asprintf");
2242 free(id_str);
2243 goto done;
2246 mode_t mode = got_tree_entry_get_mode(te);
2248 if (got_object_tree_entry_is_submodule(te))
2249 modestr = "$";
2250 else if (S_ISLNK(mode))
2251 modestr = "@";
2252 else if (S_ISDIR(mode))
2253 modestr = "/";
2254 else if (mode & S_IXUSR)
2255 modestr = "*";
2257 char *build_folder = NULL;
2258 if (S_ISDIR(got_tree_entry_get_mode(te))) {
2259 if (gw_trans->repo_folder != NULL) {
2260 if ((asprintf(&build_folder, "%s/%s",
2261 gw_trans->repo_folder,
2262 got_tree_entry_get_name(te))) == -1) {
2263 error =
2264 got_error_from_errno("asprintf");
2265 goto done;
2267 } else {
2268 if (asprintf(&build_folder, "%s",
2269 got_tree_entry_get_name(te)) == -1)
2270 goto done;
2273 if ((asprintf(&url_html, folder_html,
2274 gw_trans->repo_name, gw_trans->action_name,
2275 gw_trans->commit, build_folder,
2276 got_tree_entry_get_name(te), modestr)) == -1) {
2277 error = got_error_from_errno("asprintf");
2278 goto done;
2280 } else {
2281 if (gw_trans->repo_folder != NULL) {
2282 if ((asprintf(&build_folder, "%s",
2283 gw_trans->repo_folder)) == -1) {
2284 error =
2285 got_error_from_errno("asprintf");
2286 goto done;
2288 } else
2289 build_folder = strdup("");
2291 if ((asprintf(&url_html, file_html, gw_trans->repo_name,
2292 "blame", gw_trans->commit,
2293 got_tree_entry_get_name(te), build_folder,
2294 got_tree_entry_get_name(te), modestr)) == -1) {
2295 error = got_error_from_errno("asprintf");
2296 goto done;
2299 free(build_folder);
2301 if (error)
2302 goto done;
2304 if ((asprintf(&tree_row, tree_line, url_html)) == -1) {
2305 error = got_error_from_errno("asprintf");
2306 goto done;
2308 error = buf_puts(&newsize, diffbuf, tree_row);
2309 if (error)
2310 goto done;
2312 free(id);
2313 free(id_str);
2314 free(url_html);
2315 free(tree_row);
2318 if (buf_len(diffbuf) > 0) {
2319 error = buf_putc(diffbuf, '\0');
2320 tree_html = strdup(buf_get(diffbuf));
2322 done:
2323 if (tree)
2324 got_object_tree_close(tree);
2325 if (repo)
2326 got_repo_close(repo);
2328 free(in_repo_path);
2329 free(tree_id);
2330 free(diffbuf);
2331 if (error)
2332 return NULL;
2333 else
2334 return tree_html;
2337 static char *
2338 gw_get_repo_heads(struct gw_trans *gw_trans)
2340 const struct got_error *error = NULL;
2341 struct got_repository *repo = NULL;
2342 struct got_reflist_head refs;
2343 struct got_reflist_entry *re;
2344 char *heads, *head_row = NULL, *head_navs_disp = NULL, *age = NULL;
2345 struct buf *diffbuf = NULL;
2346 size_t newsize;
2348 error = buf_alloc(&diffbuf, 0);
2349 if (error)
2350 return NULL;
2352 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2353 if (error)
2354 goto done;
2356 SIMPLEQ_INIT(&refs);
2357 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
2358 NULL);
2359 if (error)
2360 goto done;
2362 SIMPLEQ_FOREACH(re, &refs, entry) {
2363 char *refname;
2365 refname = strdup(got_ref_get_name(re->ref));
2366 if (refname == NULL) {
2367 error = got_error_from_errno("got_ref_to_str");
2368 goto done;
2371 if (strncmp(refname, "refs/heads/", 11) != 0) {
2372 free(refname);
2373 continue;
2376 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path, refname,
2377 TM_DIFF);
2379 if ((asprintf(&head_navs_disp, heads_navs, gw_trans->repo_name,
2380 refname, gw_trans->repo_name, refname,
2381 gw_trans->repo_name, refname, gw_trans->repo_name,
2382 refname)) == -1) {
2383 error = got_error_from_errno("asprintf");
2384 goto done;
2387 if (strncmp(refname, "refs/heads/", 11) == 0)
2388 refname += 11;
2390 if ((asprintf(&head_row, heads_row, age, refname,
2391 head_navs_disp)) == -1) {
2392 error = got_error_from_errno("asprintf");
2393 goto done;
2396 error = buf_puts(&newsize, diffbuf, head_row);
2398 free(head_navs_disp);
2399 free(head_row);
2402 if (buf_len(diffbuf) > 0) {
2403 error = buf_putc(diffbuf, '\0');
2404 heads = strdup(buf_get(diffbuf));
2406 done:
2407 buf_free(diffbuf);
2408 got_ref_list_free(&refs);
2409 if (repo)
2410 got_repo_close(repo);
2411 if (error)
2412 return NULL;
2413 else
2414 return heads;
2417 static char *
2418 gw_get_got_link(struct gw_trans *gw_trans)
2420 char *link;
2422 if ((asprintf(&link, got_link, gw_trans->gw_conf->got_logo_url,
2423 gw_trans->gw_conf->got_logo)) == -1)
2424 return NULL;
2426 return link;
2429 static char *
2430 gw_get_site_link(struct gw_trans *gw_trans)
2432 char *link, *repo = "", *action = "";
2434 if (gw_trans->repo_name != NULL)
2435 if ((asprintf(&repo, " / <a href='?path=%s&action=summary'>%s" \
2436 "</a>", gw_trans->repo_name, gw_trans->repo_name)) == -1)
2437 return NULL;
2439 if (gw_trans->action_name != NULL)
2440 if ((asprintf(&action, " / %s", gw_trans->action_name)) == -1)
2441 return NULL;
2443 if ((asprintf(&link, site_link, GOTWEB,
2444 gw_trans->gw_conf->got_site_link, repo, action)) == -1)
2445 return NULL;
2447 return link;
2450 static char *
2451 gw_colordiff_line(char *buf)
2453 const struct got_error *error = NULL;
2454 char *colorized_line = NULL, *div_diff_line_div = NULL, *color = NULL;
2455 struct buf *diffbuf = NULL;
2456 size_t newsize;
2458 error = buf_alloc(&diffbuf, 0);
2459 if (error)
2460 return NULL;
2462 if (buf == NULL)
2463 return NULL;
2464 if (strncmp(buf, "-", 1) == 0)
2465 color = "diff_minus";
2466 if (strncmp(buf, "+", 1) == 0)
2467 color = "diff_plus";
2468 if (strncmp(buf, "@@", 2) == 0)
2469 color = "diff_chunk_header";
2470 if (strncmp(buf, "@@", 2) == 0)
2471 color = "diff_chunk_header";
2472 if (strncmp(buf, "commit +", 8) == 0)
2473 color = "diff_meta";
2474 if (strncmp(buf, "commit -", 8) == 0)
2475 color = "diff_meta";
2476 if (strncmp(buf, "blob +", 6) == 0)
2477 color = "diff_meta";
2478 if (strncmp(buf, "blob -", 6) == 0)
2479 color = "diff_meta";
2480 if (strncmp(buf, "file +", 6) == 0)
2481 color = "diff_meta";
2482 if (strncmp(buf, "file -", 6) == 0)
2483 color = "diff_meta";
2484 if (strncmp(buf, "from:", 5) == 0)
2485 color = "diff_author";
2486 if (strncmp(buf, "via:", 4) == 0)
2487 color = "diff_author";
2488 if (strncmp(buf, "date:", 5) == 0)
2489 color = "diff_date";
2491 if ((asprintf(&div_diff_line_div, div_diff_line, color)) == -1)
2492 return NULL;
2494 error = buf_puts(&newsize, diffbuf, div_diff_line_div);
2495 if (error)
2496 return NULL;
2498 error = buf_puts(&newsize, diffbuf, buf);
2499 if (error)
2500 return NULL;
2502 if (buf_len(diffbuf) > 0) {
2503 error = buf_putc(diffbuf, '\0');
2504 colorized_line = strdup(buf_get(diffbuf));
2507 free(diffbuf);
2508 free(div_diff_line_div);
2509 return colorized_line;
2512 static char *
2513 gw_html_escape(const char *html)
2515 char *escaped_str = NULL, *buf;
2516 char c[1];
2517 size_t sz, i, buff_sz = 2048;
2519 if ((buf = calloc(buff_sz, sizeof(char *))) == NULL)
2520 return NULL;
2522 if (html == NULL)
2523 return NULL;
2524 else
2525 if ((sz = strlen(html)) == 0)
2526 return NULL;
2528 /* only work with buff_sz */
2529 if (buff_sz < sz)
2530 sz = buff_sz;
2532 for (i = 0; i < sz; i++) {
2533 c[0] = html[i];
2534 switch (c[0]) {
2535 case ('>'):
2536 strcat(buf, "&gt;");
2537 break;
2538 case ('&'):
2539 strcat(buf, "&amp;");
2540 break;
2541 case ('<'):
2542 strcat(buf, "&lt;");
2543 break;
2544 case ('"'):
2545 strcat(buf, "&quot;");
2546 break;
2547 case ('\''):
2548 strcat(buf, "&apos;");
2549 break;
2550 case ('\n'):
2551 strcat(buf, "<br />");
2552 default:
2553 strcat(buf, &c[0]);
2554 break;
2557 asprintf(&escaped_str, "%s", buf);
2558 free(buf);
2559 return escaped_str;
2562 int
2563 main(int argc, char *argv[])
2565 const struct got_error *error = NULL;
2566 struct gw_trans *gw_trans;
2567 struct gw_dir *dir = NULL, *tdir;
2568 const char *page = "index";
2569 int gw_malloc = 1;
2571 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
2572 errx(1, "malloc");
2574 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
2575 errx(1, "malloc");
2577 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
2578 errx(1, "malloc");
2580 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
2581 errx(1, "malloc");
2583 if (KCGI_OK != khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX,
2584 &page, 1, 0))
2585 errx(1, "khttp_parse");
2587 if ((gw_trans->gw_conf =
2588 malloc(sizeof(struct gotweb_conf))) == NULL) {
2589 gw_malloc = 0;
2590 error = got_error_from_errno("malloc");
2591 goto err;
2594 TAILQ_INIT(&gw_trans->gw_dirs);
2595 TAILQ_INIT(&gw_trans->gw_headers);
2597 gw_trans->page = 0;
2598 gw_trans->repos_total = 0;
2599 gw_trans->repo_path = NULL;
2600 gw_trans->commit = NULL;
2601 gw_trans->headref = strdup(GOT_REF_HEAD);
2602 gw_trans->mime = KMIME_TEXT_HTML;
2603 gw_trans->gw_tmpl->key = gw_templs;
2604 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
2605 gw_trans->gw_tmpl->arg = gw_trans;
2606 gw_trans->gw_tmpl->cb = gw_template;
2607 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
2609 err:
2610 if (error) {
2611 gw_trans->mime = KMIME_TEXT_PLAIN;
2612 gw_trans->action = GW_ERR;
2613 gw_display_index(gw_trans, error);
2614 goto done;
2617 error = gw_parse_querystring(gw_trans);
2618 if (error)
2619 goto err;
2621 gw_display_index(gw_trans, error);
2623 done:
2624 if (gw_malloc) {
2625 free(gw_trans->gw_conf->got_repos_path);
2626 free(gw_trans->gw_conf->got_www_path);
2627 free(gw_trans->gw_conf->got_site_name);
2628 free(gw_trans->gw_conf->got_site_owner);
2629 free(gw_trans->gw_conf->got_site_link);
2630 free(gw_trans->gw_conf->got_logo);
2631 free(gw_trans->gw_conf->got_logo_url);
2632 free(gw_trans->gw_conf);
2633 free(gw_trans->commit);
2634 free(gw_trans->repo_path);
2635 free(gw_trans->repo_name);
2636 free(gw_trans->repo_file);
2637 free(gw_trans->action_name);
2638 free(gw_trans->headref);
2640 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
2641 free(dir->name);
2642 free(dir->description);
2643 free(dir->age);
2644 free(dir->url);
2645 free(dir->path);
2646 free(dir);
2651 khttp_free(gw_trans->gw_req);
2652 return EXIT_SUCCESS;