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 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
395 dir_c++;
397 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
398 if (gw_trans->page > 0 && (gw_trans->page *
399 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
400 prev_disp++;
401 continue;
404 prev_disp++;
406 if (error)
407 return error;
408 if((asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
409 gw_dir->name, gw_dir->name)) == -1)
410 return got_error_from_errno("asprintf");
412 if ((asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
413 gw_dir->description, gw_dir->owner, gw_dir->age,
414 navs)) == -1)
415 return got_error_from_errno("asprintf");
417 khttp_puts(gw_trans->gw_req, html);
419 free(navs);
420 free(html);
422 if (gw_trans->gw_conf->got_max_repos_display == 0)
423 continue;
425 if (next_disp == gw_trans->gw_conf->got_max_repos_display)
426 khttp_puts(gw_trans->gw_req, np_wrapper_start);
427 else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
428 (gw_trans->page > 0) &&
429 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
430 prev_disp == gw_trans->repos_total))
431 khttp_puts(gw_trans->gw_req, np_wrapper_start);
433 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
434 (gw_trans->page > 0) &&
435 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
436 prev_disp == gw_trans->repos_total)) {
437 if ((asprintf(&prev, nav_prev,
438 gw_trans->page - 1)) == -1)
439 return got_error_from_errno("asprintf");
440 khttp_puts(gw_trans->gw_req, prev);
441 free(prev);
444 khttp_puts(gw_trans->gw_req, div_end);
446 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
447 next_disp == gw_trans->gw_conf->got_max_repos_display &&
448 dir_c != (gw_trans->page + 1) *
449 gw_trans->gw_conf->got_max_repos_display) {
450 if ((asprintf(&next, nav_next,
451 gw_trans->page + 1)) == -1)
452 return got_error_from_errno("calloc");
453 khttp_puts(gw_trans->gw_req, next);
454 khttp_puts(gw_trans->gw_req, div_end);
455 free(next);
456 next_disp = 0;
457 break;
460 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
461 (gw_trans->page > 0) &&
462 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
463 prev_disp == gw_trans->repos_total))
464 khttp_puts(gw_trans->gw_req, div_end);
466 next_disp++;
468 return error;
471 static const struct got_error *
472 gw_commits(struct gw_trans *gw_trans)
474 const struct got_error *error = NULL;
475 char *commits_html, *commits_navs_html;
476 struct gw_header *header = NULL, *n_header = NULL;
478 if ((header = malloc(sizeof(struct gw_header))) == NULL)
479 return got_error_from_errno("malloc");
481 if (pledge("stdio rpath proc exec sendfd unveil",
482 NULL) == -1) {
483 error = got_error_from_errno("pledge");
484 return error;
487 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
488 if (error)
489 return error;
491 error = gw_get_header(gw_trans, header,
492 gw_trans->gw_conf->got_max_commits_display);
494 khttp_puts(gw_trans->gw_req, commits_wrapper);
495 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
496 if ((asprintf(&commits_navs_html, commits_navs,
497 gw_trans->repo_name, n_header->commit_id,
498 gw_trans->repo_name, n_header->commit_id,
499 gw_trans->repo_name, n_header->commit_id)) == -1)
500 return got_error_from_errno("asprintf");
502 if ((asprintf(&commits_html, commits_line,
503 gw_gen_commit_header(n_header->commit_id,
504 n_header->refs_str),
505 gw_gen_author_header(n_header->author),
506 gw_gen_committer_header(n_header->committer),
507 gw_gen_age_header(gw_get_time_str(n_header->committer_time,
508 TM_LONG)), gw_html_escape(n_header->commit_msg),
509 commits_navs_html)) == -1)
510 return got_error_from_errno("asprintf");
511 khttp_puts(gw_trans->gw_req, commits_html);
513 khttp_puts(gw_trans->gw_req, div_end);
515 got_ref_list_free(&header->refs);
516 gw_free_headers(header);
517 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
518 gw_free_headers(n_header);
519 return error;
522 static const struct got_error *
523 gw_briefs(struct gw_trans *gw_trans)
525 const struct got_error *error = NULL;
526 char *briefs_html = NULL, *briefs_navs_html = NULL, *newline;
527 struct gw_header *header = NULL, *n_header = NULL;
529 if ((header = malloc(sizeof(struct gw_header))) == NULL)
530 return got_error_from_errno("malloc");
532 if (pledge("stdio rpath proc exec sendfd unveil",
533 NULL) == -1) {
534 error = got_error_from_errno("pledge");
535 return error;
538 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
539 if (error)
540 return error;
542 if (gw_trans->action == GW_SUMMARY)
543 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
544 else
545 error = gw_get_header(gw_trans, header,
546 gw_trans->gw_conf->got_max_commits_display);
548 khttp_puts(gw_trans->gw_req, briefs_wrapper);
549 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
550 if ((asprintf(&briefs_navs_html, briefs_navs,
551 gw_trans->repo_name, n_header->commit_id,
552 gw_trans->repo_name, n_header->commit_id,
553 gw_trans->repo_name, n_header->commit_id)) == -1)
554 return got_error_from_errno("asprintf");
555 newline = strchr(n_header->commit_msg, '\n');
556 if (newline)
557 *newline = '\0';
558 if ((asprintf(&briefs_html, briefs_line,
559 gw_get_time_str(n_header->committer_time, TM_DIFF),
560 n_header->author, gw_html_escape(n_header->commit_msg),
561 briefs_navs_html)) == -1)
562 return got_error_from_errno("asprintf");
563 khttp_puts(gw_trans->gw_req, briefs_html);
565 khttp_puts(gw_trans->gw_req, div_end);
567 got_ref_list_free(&header->refs);
568 gw_free_headers(header);
569 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
570 gw_free_headers(n_header);
571 return error;
574 static const struct got_error *
575 gw_summary(struct gw_trans *gw_trans)
577 const struct got_error *error = NULL;
578 char *description_html, *repo_owner_html, *repo_age_html,
579 *cloneurl_html, *tags, *heads, *tags_html,
580 *heads_html, *age;
582 if (pledge("stdio rpath proc exec sendfd unveil",
583 NULL) == -1) {
584 error = got_error_from_errno("pledge");
585 return error;
588 /* unveil is applied with gw_briefs below */
590 khttp_puts(gw_trans->gw_req, summary_wrapper);
591 if (gw_trans->gw_conf->got_show_repo_description) {
592 if (gw_trans->gw_dir->description != NULL &&
593 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
594 if ((asprintf(&description_html, description,
595 gw_trans->gw_dir->description)) == -1)
596 return got_error_from_errno("asprintf");
598 khttp_puts(gw_trans->gw_req, description_html);
599 free(description_html);
603 if (gw_trans->gw_conf->got_show_repo_owner) {
604 if (gw_trans->gw_dir->owner != NULL &&
605 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
606 if ((asprintf(&repo_owner_html, repo_owner,
607 gw_trans->gw_dir->owner)) == -1)
608 return got_error_from_errno("asprintf");
610 khttp_puts(gw_trans->gw_req, repo_owner_html);
611 free(repo_owner_html);
615 if (gw_trans->gw_conf->got_show_repo_age) {
616 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path,
617 "refs/heads", TM_LONG);
618 if (age != NULL && (strcmp(age, "") != 0)) {
619 if ((asprintf(&repo_age_html, last_change, age)) == -1)
620 return got_error_from_errno("asprintf");
622 khttp_puts(gw_trans->gw_req, repo_age_html);
623 free(repo_age_html);
624 free(age);
628 if (gw_trans->gw_conf->got_show_repo_cloneurl) {
629 if (gw_trans->gw_dir->url != NULL &&
630 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
631 if ((asprintf(&cloneurl_html, cloneurl,
632 gw_trans->gw_dir->url)) == -1)
633 return got_error_from_errno("asprintf");
635 khttp_puts(gw_trans->gw_req, cloneurl_html);
636 free(cloneurl_html);
639 khttp_puts(gw_trans->gw_req, div_end);
641 error = gw_briefs(gw_trans);
642 if (error)
643 return error;
645 tags = gw_get_repo_tags(gw_trans, D_MAXSLCOMMDISP, TAGBRIEF);
646 heads = gw_get_repo_heads(gw_trans);
648 if (tags != NULL && strcmp(tags, "") != 0) {
649 if ((asprintf(&tags_html, summary_tags,
650 tags)) == -1)
651 return got_error_from_errno("asprintf");
652 khttp_puts(gw_trans->gw_req, tags_html);
653 free(tags_html);
654 free(tags);
657 if (heads != NULL && strcmp(heads, "") != 0) {
658 if ((asprintf(&heads_html, summary_heads,
659 heads)) == -1)
660 return got_error_from_errno("asprintf");
661 khttp_puts(gw_trans->gw_req, heads_html);
662 free(heads_html);
663 free(heads);
665 return error;
668 static const struct got_error *
669 gw_tree(struct gw_trans *gw_trans)
671 const struct got_error *error = NULL;
672 struct gw_header *header = NULL;
673 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
675 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
676 return got_error_from_errno("pledge");
678 if ((header = malloc(sizeof(struct gw_header))) == NULL)
679 return got_error_from_errno("malloc");
681 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
682 if (error)
683 return error;
685 error = gw_get_header(gw_trans, header, 1);
686 if (error)
687 return error;
689 tree_html = gw_get_repo_tree(gw_trans);
691 if (tree_html == NULL)
692 tree_html = strdup("");
694 if ((asprintf(&tree_html_disp, tree_header,
695 gw_gen_age_header(gw_get_time_str(header->committer_time, TM_LONG)),
696 gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
697 tree_html)) == -1)
698 return got_error_from_errno("asprintf");
700 if ((asprintf(&tree, tree_wrapper, tree_html_disp)) == -1)
701 return got_error_from_errno("asprintf");
703 khttp_puts(gw_trans->gw_req, tree);
704 got_ref_list_free(&header->refs);
705 gw_free_headers(header);
706 free(tree_html_disp);
707 free(tree_html);
708 free(tree);
709 return error;
712 static const struct got_error *
713 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
715 const struct got_error *error = NULL;
716 DIR *dt;
717 char *dir_test;
718 int opened = 0;
720 if ((asprintf(&dir_test, "%s/%s/%s",
721 gw_trans->gw_conf->got_repos_path, gw_dir->name,
722 GOTWEB_GIT_DIR)) == -1)
723 return got_error_from_errno("asprintf");
725 dt = opendir(dir_test);
726 if (dt == NULL) {
727 free(dir_test);
728 } else {
729 gw_dir->path = strdup(dir_test);
730 opened = 1;
731 goto done;
734 if ((asprintf(&dir_test, "%s/%s/%s",
735 gw_trans->gw_conf->got_repos_path, gw_dir->name,
736 GOTWEB_GOT_DIR)) == -1)
737 return got_error_from_errno("asprintf");
739 dt = opendir(dir_test);
740 if (dt == NULL)
741 free(dir_test);
742 else {
743 opened = 1;
744 error = got_error(GOT_ERR_NOT_GIT_REPO);
745 goto errored;
748 if ((asprintf(&dir_test, "%s/%s",
749 gw_trans->gw_conf->got_repos_path, gw_dir->name)) == -1)
750 return got_error_from_errno("asprintf");
752 gw_dir->path = strdup(dir_test);
754 done:
755 gw_dir->description = gw_get_repo_description(gw_trans,
756 gw_dir->path);
757 gw_dir->owner = gw_get_repo_owner(gw_trans, gw_dir->path);
758 gw_dir->age = gw_get_repo_age(gw_trans, gw_dir->path, "refs/heads",
759 TM_DIFF);
760 gw_dir->url = gw_get_clone_url(gw_trans, gw_dir->path);
762 errored:
763 free(dir_test);
764 if (opened)
765 closedir(dt);
766 return error;
769 static const struct got_error *
770 gw_load_got_paths(struct gw_trans *gw_trans)
772 const struct got_error *error = NULL;
773 DIR *d;
774 struct dirent **sd_dent;
775 struct gw_dir *gw_dir;
776 struct stat st;
777 unsigned int d_cnt, d_i;
779 d = opendir(gw_trans->gw_conf->got_repos_path);
780 if (d == NULL) {
781 error = got_error_from_errno2("opendir",
782 gw_trans->gw_conf->got_repos_path);
783 return error;
786 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
787 alphasort);
788 if (d_cnt == -1) {
789 error = got_error_from_errno2("scandir",
790 gw_trans->gw_conf->got_repos_path);
791 return error;
794 for (d_i = 0; d_i < d_cnt; d_i++) {
795 if (gw_trans->gw_conf->got_max_repos > 0 &&
796 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
797 break; /* account for parent and self */
799 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
800 strcmp(sd_dent[d_i]->d_name, "..") == 0)
801 continue;
803 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
804 return got_error_from_errno("gw_dir malloc");
806 error = gw_load_got_path(gw_trans, gw_dir);
807 if (error && error->code == GOT_ERR_NOT_GIT_REPO)
808 continue;
809 else if (error)
810 return error;
812 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
813 !got_path_dir_is_empty(gw_dir->path)) {
814 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
815 entry);
816 gw_trans->repos_total++;
820 closedir(d);
821 return error;
824 static const struct got_error *
825 gw_parse_querystring(struct gw_trans *gw_trans)
827 const struct got_error *error = NULL;
828 struct kpair *p;
829 struct gw_query_action *action = NULL;
830 unsigned int i;
832 if (gw_trans->gw_req->fieldnmap[0]) {
833 error = got_error_from_errno("bad parse");
834 return error;
835 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
836 /* define gw_trans->repo_path */
837 if ((asprintf(&gw_trans->repo_name, "%s", p->parsed.s)) == -1)
838 return got_error_from_errno("asprintf");
840 if ((asprintf(&gw_trans->repo_path, "%s/%s",
841 gw_trans->gw_conf->got_repos_path, p->parsed.s)) == -1)
842 return got_error_from_errno("asprintf");
844 /* get action and set function */
845 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
846 for (i = 0; i < nitems(gw_query_funcs); i++) {
847 action = &gw_query_funcs[i];
848 if (action->func_name == NULL)
849 continue;
851 if (strcmp(action->func_name,
852 p->parsed.s) == 0) {
853 gw_trans->action = i;
854 if ((asprintf(&gw_trans->action_name,
855 "%s", action->func_name)) == -1)
856 return
857 got_error_from_errno(
858 "asprintf");
860 break;
863 action = NULL;
866 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
867 if ((asprintf(&gw_trans->commit, "%s",
868 p->parsed.s)) == -1)
869 return got_error_from_errno("asprintf");
871 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
872 if ((asprintf(&gw_trans->repo_file, "%s",
873 p->parsed.s)) == -1)
874 return got_error_from_errno("asprintf");
876 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER]))
877 if ((asprintf(&gw_trans->repo_folder, "%s",
878 p->parsed.s)) == -1)
879 return got_error_from_errno("asprintf");
881 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
882 if ((asprintf(&gw_trans->headref, "%s",
883 p->parsed.s)) == -1)
884 return got_error_from_errno("asprintf");
886 if (action == NULL) {
887 error = got_error_from_errno("invalid action");
888 return error;
890 if ((gw_trans->gw_dir =
891 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
892 return got_error_from_errno("gw_dir malloc");
894 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
895 if (error)
896 return error;
897 } else
898 gw_trans->action = GW_INDEX;
900 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
901 gw_trans->page = p->parsed.i;
903 /* if (gw_trans->action == GW_RAW) */
904 /* gw_trans->mime = KMIME_TEXT_PLAIN; */
906 return error;
909 static struct gw_dir *
910 gw_init_gw_dir(char *dir)
912 struct gw_dir *gw_dir;
914 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
915 return NULL;
917 if ((asprintf(&gw_dir->name, "%s", dir)) == -1)
918 return NULL;
920 return gw_dir;
923 static void
924 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
926 khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
927 khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
928 khttps[code]);
929 khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
930 kmimetypes[mime]);
931 khttp_head(gw_trans->gw_req, "X-Content-Type-Options", "nosniff");
932 khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
933 khttp_head(gw_trans->gw_req, "X-XSS-Protection", "1; mode=block");
934 khttp_body(gw_trans->gw_req);
937 static void
938 gw_display_index(struct gw_trans *gw_trans, const struct got_error *err)
940 gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
941 khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
943 if (err)
944 khttp_puts(gw_trans->gw_req, err->msg);
945 else
946 khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
947 gw_query_funcs[gw_trans->action].template);
949 khtml_close(gw_trans->gw_html_req);
952 static int
953 gw_template(size_t key, void *arg)
955 const struct got_error *error = NULL;
956 struct gw_trans *gw_trans = arg;
957 char *gw_got_link, *gw_site_link;
958 char *site_owner_name, *site_owner_name_h;
960 switch (key) {
961 case (TEMPL_HEAD):
962 khttp_puts(gw_trans->gw_req, head);
963 break;
964 case(TEMPL_HEADER):
965 gw_got_link = gw_get_got_link(gw_trans);
966 if (gw_got_link != NULL)
967 khttp_puts(gw_trans->gw_req, gw_got_link);
969 free(gw_got_link);
970 break;
971 case (TEMPL_SITEPATH):
972 gw_site_link = gw_get_site_link(gw_trans);
973 if (gw_site_link != NULL)
974 khttp_puts(gw_trans->gw_req, gw_site_link);
976 free(gw_site_link);
977 break;
978 case(TEMPL_TITLE):
979 if (gw_trans->gw_conf->got_site_name != NULL)
980 khtml_puts(gw_trans->gw_html_req,
981 gw_trans->gw_conf->got_site_name);
983 break;
984 case (TEMPL_SEARCH):
985 khttp_puts(gw_trans->gw_req, search);
986 break;
987 case(TEMPL_SITEOWNER):
988 if (gw_trans->gw_conf->got_site_owner != NULL &&
989 gw_trans->gw_conf->got_show_site_owner) {
990 site_owner_name =
991 gw_html_escape(gw_trans->gw_conf->got_site_owner);
992 if ((asprintf(&site_owner_name_h, site_owner,
993 site_owner_name))
994 == -1)
995 return 0;
997 khttp_puts(gw_trans->gw_req, site_owner_name_h);
998 free(site_owner_name);
999 free(site_owner_name_h);
1001 break;
1002 case(TEMPL_CONTENT):
1003 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1004 if (error)
1005 khttp_puts(gw_trans->gw_req, error->msg);
1007 break;
1008 default:
1009 return 0;
1010 break;
1012 return 1;
1015 static char *
1016 gw_gen_commit_header(char *str1, char *str2)
1018 char *return_html = NULL, *ref_str = NULL;
1020 if (strcmp(str2, "") != 0) {
1021 if ((asprintf(&ref_str, "(%s)", str2)) == -1) {
1022 return_html = strdup("");
1023 return return_html;
1025 } else
1026 ref_str = strdup("");
1029 if ((asprintf(&return_html, header_commit_html, str1, ref_str)) == -1)
1030 return_html = strdup("");
1032 free(ref_str);
1033 return return_html;
1036 static char *
1037 gw_gen_diff_header(char *str1, char *str2)
1039 char *return_html = NULL;
1041 if ((asprintf(&return_html, header_diff_html, str1, str2)) == -1)
1042 return_html = strdup("");
1044 return return_html;
1047 static char *
1048 gw_gen_author_header(char *str)
1050 char *return_html = NULL;
1052 if ((asprintf(&return_html, header_author_html, str)) == -1)
1053 return_html = strdup("");
1055 return return_html;
1058 static char *
1059 gw_gen_committer_header(char *str)
1061 char *return_html = NULL;
1063 if ((asprintf(&return_html, header_committer_html, str)) == -1)
1064 return_html = strdup("");
1066 return return_html;
1069 static char *
1070 gw_gen_age_header(char *str)
1072 char *return_html = NULL;
1074 if ((asprintf(&return_html, header_age_html, str)) == -1)
1075 return_html = strdup("");
1077 return return_html;
1080 static char *
1081 gw_gen_commit_msg_header(char *str)
1083 char *return_html = NULL;
1085 if ((asprintf(&return_html, header_commit_msg_html, str)) == -1)
1086 return_html = strdup("");
1088 return return_html;
1091 static char *
1092 gw_gen_tree_header(char *str)
1094 char *return_html = NULL;
1096 if ((asprintf(&return_html, header_tree_html, str)) == -1)
1097 return_html = strdup("");
1099 return return_html;
1102 static char *
1103 gw_get_repo_description(struct gw_trans *gw_trans, char *dir)
1105 FILE *f;
1106 char *description = NULL, *d_file = NULL;
1107 unsigned int len;
1109 if (gw_trans->gw_conf->got_show_repo_description == false)
1110 goto err;
1112 if ((asprintf(&d_file, "%s/description", dir)) == -1)
1113 goto err;
1115 if ((f = fopen(d_file, "r")) == NULL)
1116 goto err;
1118 fseek(f, 0, SEEK_END);
1119 len = ftell(f) + 1;
1120 fseek(f, 0, SEEK_SET);
1121 if ((description = calloc(len, sizeof(char *))) == NULL)
1122 goto err;
1124 fread(description, 1, len, f);
1125 fclose(f);
1126 free(d_file);
1127 return description;
1128 err:
1129 if ((asprintf(&description, "%s", "")) == -1)
1130 return NULL;
1132 return description;
1135 static char *
1136 gw_get_time_str(time_t committer_time, int ref_tm)
1138 struct tm tm;
1139 time_t diff_time;
1140 char *years = "years ago", *months = "months ago";
1141 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
1142 char *minutes = "minutes ago", *seconds = "seconds ago";
1143 char *now = "right now";
1144 char *repo_age, *s;
1145 char datebuf[29];
1147 switch (ref_tm) {
1148 case TM_DIFF:
1149 diff_time = time(NULL) - committer_time;
1150 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1151 if ((asprintf(&repo_age, "%lld %s",
1152 (diff_time / 60 / 60 / 24 / 365), years)) == -1)
1153 return NULL;
1154 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1155 if ((asprintf(&repo_age, "%lld %s",
1156 (diff_time / 60 / 60 / 24 / (365 / 12)),
1157 months)) == -1)
1158 return NULL;
1159 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1160 if ((asprintf(&repo_age, "%lld %s",
1161 (diff_time / 60 / 60 / 24 / 7), weeks)) == -1)
1162 return NULL;
1163 } else if (diff_time > 60 * 60 * 24 * 2) {
1164 if ((asprintf(&repo_age, "%lld %s",
1165 (diff_time / 60 / 60 / 24), days)) == -1)
1166 return NULL;
1167 } else if (diff_time > 60 * 60 * 2) {
1168 if ((asprintf(&repo_age, "%lld %s",
1169 (diff_time / 60 / 60), hours)) == -1)
1170 return NULL;
1171 } else if (diff_time > 60 * 2) {
1172 if ((asprintf(&repo_age, "%lld %s", (diff_time / 60),
1173 minutes)) == -1)
1174 return NULL;
1175 } else if (diff_time > 2) {
1176 if ((asprintf(&repo_age, "%lld %s", diff_time,
1177 seconds)) == -1)
1178 return NULL;
1179 } else {
1180 if ((asprintf(&repo_age, "%s", now)) == -1)
1181 return NULL;
1183 break;
1184 case TM_LONG:
1185 if (gmtime_r(&committer_time, &tm) == NULL)
1186 return NULL;
1188 s = asctime_r(&tm, datebuf);
1189 if (s == NULL)
1190 return NULL;
1192 if ((asprintf(&repo_age, "%s UTC", datebuf)) == -1)
1193 return NULL;
1194 break;
1196 return repo_age;
1199 static char *
1200 gw_get_repo_age(struct gw_trans *gw_trans, char *dir, char *repo_ref,
1201 int ref_tm)
1203 const struct got_error *error = NULL;
1204 struct got_object_id *id = NULL;
1205 struct got_repository *repo = NULL;
1206 struct got_commit_object *commit = NULL;
1207 struct got_reflist_head refs;
1208 struct got_reflist_entry *re;
1209 struct got_reference *head_ref;
1210 int is_head = 0;
1211 time_t committer_time = 0, cmp_time = 0;
1212 const char *refname;
1213 char *repo_age = NULL;
1215 if (repo_ref == NULL)
1216 return NULL;
1218 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
1219 is_head = 1;
1221 SIMPLEQ_INIT(&refs);
1222 if (gw_trans->gw_conf->got_show_repo_age == false) {
1223 if ((asprintf(&repo_age, "")) == -1)
1224 return NULL;
1225 return repo_age;
1228 error = got_repo_open(&repo, dir, NULL);
1229 if (error)
1230 goto err;
1232 if (is_head)
1233 error = got_ref_list(&refs, repo, "refs/heads",
1234 got_ref_cmp_by_name, NULL);
1235 else
1236 error = got_ref_list(&refs, repo, repo_ref,
1237 got_ref_cmp_by_name, NULL);
1238 if (error)
1239 goto err;
1241 SIMPLEQ_FOREACH(re, &refs, entry) {
1242 if (is_head)
1243 refname = strdup(repo_ref);
1244 else
1245 refname = got_ref_get_name(re->ref);
1246 error = got_ref_open(&head_ref, repo, refname, 0);
1247 if (error)
1248 goto err;
1250 error = got_ref_resolve(&id, repo, head_ref);
1251 got_ref_close(head_ref);
1252 if (error)
1253 goto err;
1255 error = got_object_open_as_commit(&commit, repo, id);
1256 if (error)
1257 goto err;
1259 committer_time =
1260 got_object_commit_get_committer_time(commit);
1262 if (cmp_time < committer_time)
1263 cmp_time = committer_time;
1266 if (cmp_time != 0) {
1267 committer_time = cmp_time;
1268 repo_age = gw_get_time_str(committer_time, ref_tm);
1269 } else
1270 if ((asprintf(&repo_age, "")) == -1)
1271 return NULL;
1272 got_ref_list_free(&refs);
1273 free(id);
1274 return repo_age;
1275 err:
1276 if ((asprintf(&repo_age, "%s", error->msg)) == -1)
1277 return NULL;
1279 return repo_age;
1282 static char *
1283 gw_get_diff(struct gw_trans *gw_trans, struct gw_header *header)
1285 const struct got_error *error;
1286 FILE *f = NULL;
1287 struct got_object_id *id1 = NULL, *id2 = NULL;
1288 struct buf *diffbuf = NULL;
1289 char *label1 = NULL, *label2 = NULL, *diff_html = NULL, *buf = NULL,
1290 *buf_color = NULL, *n_buf = NULL, *newline = NULL;
1291 int obj_type;
1292 size_t newsize;
1294 f = got_opentemp();
1295 if (f == NULL)
1296 return NULL;
1298 error = buf_alloc(&diffbuf, 0);
1299 if (error)
1300 return NULL;
1302 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
1303 if (error)
1304 goto done;
1306 if (strncmp(header->parent_id, "/dev/null", 9) != 0) {
1307 error = got_repo_match_object_id(&id1, &label1,
1308 header->parent_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
1309 if (error)
1310 goto done;
1313 error = got_repo_match_object_id(&id2, &label2,
1314 header->commit_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
1315 if (error)
1316 goto done;
1318 error = got_object_get_type(&obj_type, header->repo, id2);
1319 if (error)
1320 goto done;
1321 switch (obj_type) {
1322 case GOT_OBJ_TYPE_BLOB:
1323 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
1324 header->repo, f);
1325 break;
1326 case GOT_OBJ_TYPE_TREE:
1327 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
1328 header->repo, f);
1329 break;
1330 case GOT_OBJ_TYPE_COMMIT:
1331 error = got_diff_objects_as_commits(id1, id2, 3, 0,
1332 header->repo, f);
1333 break;
1334 default:
1335 error = got_error(GOT_ERR_OBJ_TYPE);
1338 if ((buf = calloc(128, sizeof(char *))) == NULL)
1339 goto done;
1341 fseek(f, 0, SEEK_SET);
1343 while ((fgets(buf, 2048, f)) != NULL) {
1344 n_buf = buf;
1345 while (*n_buf == '\n')
1346 n_buf++;
1347 newline = strchr(n_buf, '\n');
1348 if (newline)
1349 *newline = ' ';
1351 buf_color = gw_colordiff_line(gw_html_escape(n_buf));
1352 if (buf_color == NULL)
1353 continue;
1355 error = buf_puts(&newsize, diffbuf, buf_color);
1356 if (error)
1357 return NULL;
1359 error = buf_puts(&newsize, diffbuf, div_end);
1360 if (error)
1361 return NULL;
1364 if (buf_len(diffbuf) > 0) {
1365 error = buf_putc(diffbuf, '\0');
1366 diff_html = strdup(buf_get(diffbuf));
1368 done:
1369 fclose(f);
1370 free(buf_color);
1371 free(buf);
1372 free(diffbuf);
1373 free(label1);
1374 free(label2);
1375 free(id1);
1376 free(id2);
1378 if (error)
1379 return NULL;
1380 else
1381 return diff_html;
1384 static char *
1385 gw_get_repo_owner(struct gw_trans *gw_trans, char *dir)
1387 FILE *f;
1388 char *owner = NULL, *d_file = NULL;
1389 char *gotweb = "[gotweb]", *gitweb = "[gitweb]", *gw_owner = "owner";
1390 char *comp, *pos, *buf;
1391 unsigned int i;
1393 if (gw_trans->gw_conf->got_show_repo_owner == false)
1394 goto err;
1396 if ((asprintf(&d_file, "%s/config", dir)) == -1)
1397 goto err;
1399 if ((f = fopen(d_file, "r")) == NULL)
1400 goto err;
1402 if ((buf = calloc(128, sizeof(char *))) == NULL)
1403 goto err;
1405 while ((fgets(buf, 128, f)) != NULL) {
1406 if ((pos = strstr(buf, gotweb)) != NULL)
1407 break;
1409 if ((pos = strstr(buf, gitweb)) != NULL)
1410 break;
1413 if (pos == NULL)
1414 goto err;
1416 do {
1417 fgets(buf, 128, f);
1418 } while ((comp = strcasestr(buf, gw_owner)) == NULL);
1420 if (comp == NULL)
1421 goto err;
1423 if (strncmp(gw_owner, comp, strlen(gw_owner)) != 0)
1424 goto err;
1426 for (i = 0; i < 2; i++) {
1427 owner = strsep(&buf, "\"");
1430 if (owner == NULL)
1431 goto err;
1433 fclose(f);
1434 free(d_file);
1435 return owner;
1436 err:
1437 if ((asprintf(&owner, "%s", "")) == -1)
1438 return NULL;
1440 return owner;
1443 static char *
1444 gw_get_clone_url(struct gw_trans *gw_trans, char *dir)
1446 FILE *f;
1447 char *url = NULL, *d_file = NULL;
1448 unsigned int len;
1450 if ((asprintf(&d_file, "%s/cloneurl", dir)) == -1)
1451 return NULL;
1453 if ((f = fopen(d_file, "r")) == NULL)
1454 return NULL;
1456 fseek(f, 0, SEEK_END);
1457 len = ftell(f) + 1;
1458 fseek(f, 0, SEEK_SET);
1460 if ((url = calloc(len, sizeof(char *))) == NULL)
1461 return NULL;
1463 fread(url, 1, len, f);
1464 fclose(f);
1465 free(d_file);
1466 return url;
1469 static char *
1470 gw_get_repo_tags(struct gw_trans *gw_trans, int limit, int tag_type)
1472 const struct got_error *error = NULL;
1473 struct got_repository *repo = NULL;
1474 struct got_reflist_head refs;
1475 struct got_reflist_entry *re;
1476 char *tags = NULL, *tag_row = NULL, *tags_navs_disp = NULL,
1477 *age = NULL;
1478 char *newline;
1479 struct buf *diffbuf = NULL;
1480 size_t newsize;
1482 error = buf_alloc(&diffbuf, 0);
1483 if (error)
1484 return NULL;
1485 SIMPLEQ_INIT(&refs);
1487 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1488 if (error)
1489 goto done;
1491 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
1492 if (error)
1493 goto done;
1495 SIMPLEQ_FOREACH(re, &refs, entry) {
1496 const char *refname;
1497 char *refstr, *tag_commit0, *tag_commit, *id_str;
1498 time_t tagger_time;
1499 struct got_object_id *id;
1500 struct got_tag_object *tag;
1502 refname = got_ref_get_name(re->ref);
1503 if (strncmp(refname, "refs/tags/", 10) != 0)
1504 continue;
1505 refname += 10;
1506 refstr = got_ref_to_str(re->ref);
1507 if (refstr == NULL) {
1508 error = got_error_from_errno("got_ref_to_str");
1509 goto done;
1512 error = got_ref_resolve(&id, repo, re->ref);
1513 if (error)
1514 goto done;
1515 error = got_object_open_as_tag(&tag, repo, id);
1516 free(id);
1517 if (error)
1518 goto done;
1520 tagger_time = got_object_tag_get_tagger_time(tag);
1522 error = got_object_id_str(&id_str,
1523 got_object_tag_get_object_id(tag));
1524 if (error)
1525 goto done;
1527 tag_commit0 = strdup(got_object_tag_get_message(tag));
1529 if (tag_commit0 == NULL) {
1530 error = got_error_from_errno("strdup");
1531 goto done;
1534 tag_commit = tag_commit0;
1535 while (*tag_commit == '\n')
1536 tag_commit++;
1538 switch (tag_type) {
1539 case TAGBRIEF:
1540 newline = strchr(tag_commit, '\n');
1541 if (newline)
1542 *newline = '\0';
1544 if ((asprintf(&age, "%s", gw_get_time_str(tagger_time,
1545 TM_DIFF))) == -1) {
1546 error = got_error_from_errno("asprintf");
1547 goto done;
1550 if ((asprintf(&tags_navs_disp, tags_navs,
1551 gw_trans->repo_name, id_str, gw_trans->repo_name,
1552 id_str, gw_trans->repo_name, id_str,
1553 gw_trans->repo_name, id_str)) == -1) {
1554 error = got_error_from_errno("asprintf");
1555 goto done;
1558 if ((asprintf(&tag_row, tags_row, age, refname,
1559 tag_commit, tags_navs_disp)) == -1) {
1560 error = got_error_from_errno("asprintf");
1561 goto done;
1564 free(tags_navs_disp);
1565 break;
1566 case TAGFULL:
1567 break;
1568 default:
1569 break;
1572 got_object_tag_close(tag);
1574 error = buf_puts(&newsize, diffbuf, tag_row);
1576 free(id_str);
1577 free(refstr);
1578 free(age);
1579 free(tag_commit0);
1580 free(tag_row);
1582 if (error || (limit && --limit == 0))
1583 break;
1586 if (buf_len(diffbuf) > 0) {
1587 error = buf_putc(diffbuf, '\0');
1588 tags = strdup(buf_get(diffbuf));
1590 done:
1591 buf_free(diffbuf);
1592 got_ref_list_free(&refs);
1593 if (repo)
1594 got_repo_close(repo);
1595 if (error)
1596 return NULL;
1597 else
1598 return tags;
1601 static void
1602 gw_free_headers(struct gw_header *header)
1604 free(header->id);
1605 free(header->path);
1606 if (header->commit != NULL)
1607 got_object_commit_close(header->commit);
1608 if (header->repo)
1609 got_repo_close(header->repo);
1610 free(header->refs_str);
1611 free(header->commit_id);
1612 free(header->parent_id);
1613 free(header->tree_id);
1614 free(header->author);
1615 free(header->committer);
1616 free(header->commit_msg);
1619 static struct gw_header *
1620 gw_init_header()
1622 struct gw_header *header;
1624 if ((header = malloc(sizeof(*header))) == NULL)
1625 return NULL;
1627 header->repo = NULL;
1628 header->commit = NULL;
1629 header->id = NULL;
1630 header->path = NULL;
1632 return header;
1635 static const struct got_error *
1636 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
1637 int limit)
1639 const struct got_error *error = NULL;
1640 struct got_commit_graph *graph = NULL;
1642 error = got_commit_graph_open(&graph, header->path, 0);
1643 if (error)
1644 goto done;
1646 error = got_commit_graph_iter_start(graph, header->id, header->repo,
1647 NULL, NULL);
1648 if (error)
1649 goto done;
1651 for (;;) {
1652 error = got_commit_graph_iter_next(&header->id, graph,
1653 header->repo, NULL, NULL);
1654 if (error) {
1655 if (error->code == GOT_ERR_ITER_COMPLETED)
1656 error = NULL;
1657 goto done;
1659 if (header->id == NULL)
1660 goto done;
1662 error = got_object_open_as_commit(&header->commit, header->repo,
1663 header->id);
1664 if (error)
1665 goto done;
1667 error = gw_get_commit(gw_trans, header);
1668 if (limit > 1) {
1669 struct gw_header *n_header = NULL;
1670 if ((n_header = gw_init_header()) == NULL)
1671 error = got_error_from_errno("malloc");
1673 n_header->refs_str = strdup(header->refs_str);
1674 n_header->commit_id = strdup(header->commit_id);
1675 n_header->parent_id = strdup(header->parent_id);
1676 n_header->tree_id = strdup(header->tree_id);
1677 n_header->author = strdup(header->author);
1678 n_header->committer = strdup(header->committer);
1679 n_header->commit_msg = strdup(header->commit_msg);
1680 n_header->committer_time = header->committer_time;
1681 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
1682 entry);
1684 if (error || (limit && --limit == 0))
1685 break;
1687 done:
1688 if (graph)
1689 got_commit_graph_close(graph);
1690 return error;
1693 static const struct got_error *
1694 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
1696 const struct got_error *error = NULL;
1697 struct got_reflist_entry *re;
1698 struct got_object_id *id2 = NULL;
1699 struct got_object_qid *parent_id;
1700 char *refs_str = NULL,
1701 *commit_msg = NULL, *commit_msg0;
1703 /*print commit*/
1704 SIMPLEQ_FOREACH(re, &header->refs, entry) {
1705 char *s;
1706 const char *name;
1707 struct got_tag_object *tag = NULL;
1708 int cmp;
1710 name = got_ref_get_name(re->ref);
1711 if (strcmp(name, GOT_REF_HEAD) == 0)
1712 continue;
1713 if (strncmp(name, "refs/", 5) == 0)
1714 name += 5;
1715 if (strncmp(name, "got/", 4) == 0)
1716 continue;
1717 if (strncmp(name, "heads/", 6) == 0)
1718 name += 6;
1719 if (strncmp(name, "remotes/", 8) == 0)
1720 name += 8;
1721 if (strncmp(name, "tags/", 5) == 0) {
1722 error = got_object_open_as_tag(&tag, header->repo,
1723 re->id);
1724 if (error) {
1725 if (error->code != GOT_ERR_OBJ_TYPE)
1726 continue;
1728 * Ref points at something other
1729 * than a tag.
1731 error = NULL;
1732 tag = NULL;
1735 cmp = got_object_id_cmp(tag ?
1736 got_object_tag_get_object_id(tag) : re->id, header->id);
1737 if (tag)
1738 got_object_tag_close(tag);
1739 if (cmp != 0)
1740 continue;
1741 s = refs_str;
1742 if ((asprintf(&refs_str, "%s%s%s", s ? s : "",
1743 s ? ", " : "", name)) == -1) {
1744 error = got_error_from_errno("asprintf");
1745 free(s);
1746 return error;
1748 header->refs_str = strdup(refs_str);
1749 free(s);
1752 if (refs_str == NULL)
1753 header->refs_str = strdup("");
1754 free(refs_str);
1756 error = got_object_id_str(&header->commit_id, header->id);
1757 if (error)
1758 return error;
1760 error = got_object_id_str(&header->tree_id,
1761 got_object_commit_get_tree_id(header->commit));
1762 if (error)
1763 return error;
1765 if (gw_trans->action == GW_DIFF) {
1766 parent_id = SIMPLEQ_FIRST(
1767 got_object_commit_get_parent_ids(header->commit));
1768 if (parent_id != NULL) {
1769 id2 = got_object_id_dup(parent_id->id);
1770 free (parent_id);
1771 error = got_object_id_str(&header->parent_id, id2);
1772 if (error)
1773 return error;
1774 free(id2);
1775 } else
1776 header->parent_id = strdup("/dev/null");
1777 } else
1778 header->parent_id = strdup("");
1780 header->committer_time =
1781 got_object_commit_get_committer_time(header->commit);
1782 header->author = strdup(got_object_commit_get_author(header->commit));
1783 header->committer =
1784 strdup(got_object_commit_get_committer(header->commit));
1786 error = got_object_commit_get_logmsg(&commit_msg0, header->commit);
1787 if (error)
1788 return error;
1790 commit_msg = commit_msg0;
1791 while (*commit_msg == '\n')
1792 commit_msg++;
1794 header->commit_msg = strdup(commit_msg);
1795 free(commit_msg0);
1796 return error;
1799 static const struct got_error *
1800 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
1802 const struct got_error *error = NULL;
1803 char *in_repo_path = NULL;
1805 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
1806 if (error)
1807 return error;
1809 SIMPLEQ_INIT(&header->refs);
1811 if (gw_trans->commit == NULL) {
1812 struct got_reference *head_ref;
1813 error = got_ref_open(&head_ref, header->repo,
1814 gw_trans->headref, 0);
1815 if (error)
1816 return error;
1818 error = got_ref_resolve(&header->id, header->repo, head_ref);
1819 got_ref_close(head_ref);
1820 if (error)
1821 return error;
1823 error = got_object_open_as_commit(&header->commit,
1824 header->repo, header->id);
1825 } else {
1826 struct got_reference *ref;
1827 error = got_ref_open(&ref, header->repo, gw_trans->commit, 0);
1828 if (error == NULL) {
1829 int obj_type;
1830 error = got_ref_resolve(&header->id, header->repo, ref);
1831 got_ref_close(ref);
1832 if (error)
1833 return error;
1834 error = got_object_get_type(&obj_type, header->repo,
1835 header->id);
1836 if (error)
1837 return error;
1838 if (obj_type == GOT_OBJ_TYPE_TAG) {
1839 struct got_tag_object *tag;
1840 error = got_object_open_as_tag(&tag,
1841 header->repo, header->id);
1842 if (error)
1843 return error;
1844 if (got_object_tag_get_object_type(tag) !=
1845 GOT_OBJ_TYPE_COMMIT) {
1846 got_object_tag_close(tag);
1847 error = got_error(GOT_ERR_OBJ_TYPE);
1848 return error;
1850 free(header->id);
1851 header->id = got_object_id_dup(
1852 got_object_tag_get_object_id(tag));
1853 if (header->id == NULL)
1854 error = got_error_from_errno(
1855 "got_object_id_dup");
1856 got_object_tag_close(tag);
1857 if (error)
1858 return error;
1859 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1860 error = got_error(GOT_ERR_OBJ_TYPE);
1861 return error;
1863 error = got_object_open_as_commit(&header->commit,
1864 header->repo, header->id);
1865 if (error)
1866 return error;
1868 if (header->commit == NULL) {
1869 error = got_repo_match_object_id_prefix(&header->id,
1870 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
1871 header->repo);
1872 if (error)
1873 return error;
1875 error = got_repo_match_object_id_prefix(&header->id,
1876 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
1877 header->repo);
1880 error = got_repo_map_path(&in_repo_path, header->repo,
1881 gw_trans->repo_path, 1);
1882 if (error)
1883 return error;
1885 if (in_repo_path) {
1886 header->path = strdup(in_repo_path);
1888 free(in_repo_path);
1890 error = got_ref_list(&header->refs, header->repo, NULL,
1891 got_ref_cmp_by_name, NULL);
1892 if (error)
1893 return error;
1895 error = gw_get_commits(gw_trans, header, limit);
1896 return error;
1899 struct blame_line {
1900 int annotated;
1901 char *id_str;
1902 char *committer;
1903 char datebuf[11]; /* YYYY-MM-DD + NUL */
1906 struct gw_blame_cb_args {
1907 struct blame_line *lines;
1908 int nlines;
1909 int nlines_prec;
1910 int lineno_cur;
1911 off_t *line_offsets;
1912 FILE *f;
1913 struct got_repository *repo;
1914 struct gw_trans *gw_trans;
1915 struct buf *blamebuf;
1918 static const struct got_error *
1919 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1921 const struct got_error *err = NULL;
1922 struct gw_blame_cb_args *a = arg;
1923 struct blame_line *bline;
1924 char *line = NULL;
1925 size_t linesize = 0, newsize;
1926 struct got_commit_object *commit = NULL;
1927 off_t offset;
1928 struct tm tm;
1929 time_t committer_time;
1931 if (nlines != a->nlines ||
1932 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1933 return got_error(GOT_ERR_RANGE);
1935 if (lineno == -1)
1936 return NULL; /* no change in this commit */
1938 /* Annotate this line. */
1939 bline = &a->lines[lineno - 1];
1940 if (bline->annotated)
1941 return NULL;
1942 err = got_object_id_str(&bline->id_str, id);
1943 if (err)
1944 return err;
1946 err = got_object_open_as_commit(&commit, a->repo, id);
1947 if (err)
1948 goto done;
1950 bline->committer = strdup(got_object_commit_get_committer(commit));
1951 if (bline->committer == NULL) {
1952 err = got_error_from_errno("strdup");
1953 goto done;
1956 committer_time = got_object_commit_get_committer_time(commit);
1957 if (localtime_r(&committer_time, &tm) == NULL)
1958 return got_error_from_errno("localtime_r");
1959 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
1960 &tm) >= sizeof(bline->datebuf)) {
1961 err = got_error(GOT_ERR_NO_SPACE);
1962 goto done;
1964 bline->annotated = 1;
1966 /* Print lines annotated so far. */
1967 bline = &a->lines[a->lineno_cur - 1];
1968 if (!bline->annotated)
1969 goto done;
1971 offset = a->line_offsets[a->lineno_cur - 1];
1972 if (fseeko(a->f, offset, SEEK_SET) == -1) {
1973 err = got_error_from_errno("fseeko");
1974 goto done;
1977 while (bline->annotated) {
1978 char *smallerthan, *at, *nl, *committer, *blame_row = NULL,
1979 *line_escape = NULL;
1980 size_t len;
1982 if (getline(&line, &linesize, a->f) == -1) {
1983 if (ferror(a->f))
1984 err = got_error_from_errno("getline");
1985 break;
1988 committer = bline->committer;
1989 smallerthan = strchr(committer, '<');
1990 if (smallerthan && smallerthan[1] != '\0')
1991 committer = smallerthan + 1;
1992 at = strchr(committer, '@');
1993 if (at)
1994 *at = '\0';
1995 len = strlen(committer);
1996 if (len >= 9)
1997 committer[8] = '\0';
1999 nl = strchr(line, '\n');
2000 if (nl)
2001 *nl = '\0';
2003 if (strcmp(line, "") != 0)
2004 line_escape = strdup(gw_html_escape(line));
2005 else
2006 line_escape = strdup("");
2008 asprintf(&blame_row, blame_line, a->nlines_prec,
2009 a->lineno_cur, bline->id_str, bline->datebuf, committer,
2010 line_escape);
2011 a->lineno_cur++;
2012 err = buf_puts(&newsize, a->blamebuf, blame_row);
2013 if (err)
2014 return err;
2016 bline = &a->lines[a->lineno_cur - 1];
2017 free(line_escape);
2018 free(blame_row);
2020 done:
2021 if (commit)
2022 got_object_commit_close(commit);
2023 free(line);
2024 return err;
2027 static char*
2028 gw_get_file_blame(struct gw_trans *gw_trans)
2030 const struct got_error *error = NULL;
2031 struct got_repository *repo = NULL;
2032 struct got_object_id *obj_id = NULL;
2033 struct got_object_id *commit_id = NULL;
2034 struct got_blob_object *blob = NULL;
2035 char *blame_html = NULL, *path = NULL, *in_repo_path = NULL,
2036 *folder = NULL;
2037 struct gw_blame_cb_args bca;
2038 int i, obj_type;
2039 size_t filesize;
2041 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2042 if (error)
2043 goto done;
2045 if (gw_trans->repo_folder != NULL) {
2046 if ((asprintf(&folder, "%s/", gw_trans->repo_folder)) == -1) {
2047 error = got_error_from_errno("asprintf");
2048 goto done;
2050 } else
2051 folder = strdup("");
2053 if ((asprintf(&path, "%s%s", folder, gw_trans->repo_file)) == -1) {
2054 error = got_error_from_errno("asprintf");
2055 goto done;
2057 free(folder);
2059 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2060 if (error)
2061 goto done;
2063 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
2064 GOT_OBJ_TYPE_COMMIT, 1, repo);
2065 if (error)
2066 goto done;
2068 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2069 if (error)
2070 goto done;
2072 if (obj_id == NULL) {
2073 error = got_error(GOT_ERR_NO_OBJ);
2074 goto done;
2077 error = got_object_get_type(&obj_type, repo, obj_id);
2078 if (error)
2079 goto done;
2081 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2082 error = got_error(GOT_ERR_OBJ_TYPE);
2083 goto done;
2086 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2087 if (error)
2088 goto done;
2090 error = buf_alloc(&bca.blamebuf, 0);
2091 if (error)
2092 goto done;
2094 bca.f = got_opentemp();
2095 if (bca.f == NULL) {
2096 error = got_error_from_errno("got_opentemp");
2097 goto done;
2099 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2100 &bca.line_offsets, bca.f, blob);
2101 if (error || bca.nlines == 0)
2102 goto done;
2104 /* Don't include \n at EOF in the blame line count. */
2105 if (bca.line_offsets[bca.nlines - 1] == filesize)
2106 bca.nlines--;
2108 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2109 if (bca.lines == NULL) {
2110 error = got_error_from_errno("calloc");
2111 goto done;
2113 bca.lineno_cur = 1;
2114 bca.nlines_prec = 0;
2115 i = bca.nlines;
2116 while (i > 0) {
2117 i /= 10;
2118 bca.nlines_prec++;
2120 bca.repo = repo;
2121 bca.gw_trans = gw_trans;
2123 error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
2124 NULL, NULL);
2125 if (buf_len(bca.blamebuf) > 0) {
2126 error = buf_putc(bca.blamebuf, '\0');
2127 blame_html = strdup(buf_get(bca.blamebuf));
2129 done:
2130 free(bca.blamebuf);
2131 free(in_repo_path);
2132 free(commit_id);
2133 free(obj_id);
2134 free(path);
2136 if (blob)
2137 error = got_object_blob_close(blob);
2138 if (repo)
2139 error = got_repo_close(repo);
2140 if (error)
2141 return NULL;
2142 if (bca.lines) {
2143 for (i = 0; i < bca.nlines; i++) {
2144 struct blame_line *bline = &bca.lines[i];
2145 free(bline->id_str);
2146 free(bline->committer);
2148 free(bca.lines);
2150 free(bca.line_offsets);
2151 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2152 error = got_error_from_errno("fclose");
2153 if (error)
2154 return NULL;
2155 else
2156 return blame_html;
2159 static char*
2160 gw_get_repo_tree(struct gw_trans *gw_trans)
2162 const struct got_error *error = NULL;
2163 struct got_repository *repo = NULL;
2164 struct got_object_id *tree_id = NULL, *commit_id = NULL;
2165 struct got_tree_object *tree = NULL;
2166 struct buf *diffbuf = NULL;
2167 size_t newsize;
2168 char *tree_html = NULL, *path = NULL, *in_repo_path = NULL,
2169 *tree_row = NULL, *id_str;
2170 int nentries, i;
2172 error = buf_alloc(&diffbuf, 0);
2173 if (error)
2174 return NULL;
2176 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2177 if (error)
2178 goto done;
2180 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
2181 if (error)
2182 goto done;
2184 if (gw_trans->repo_folder != NULL)
2185 path = strdup(gw_trans->repo_folder);
2186 else if (in_repo_path) {
2187 free(path);
2188 path = in_repo_path;
2191 if (gw_trans->commit == NULL) {
2192 struct got_reference *head_ref;
2193 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
2194 if (error)
2195 goto done;
2197 error = got_ref_resolve(&commit_id, repo, head_ref);
2198 got_ref_close(head_ref);
2200 } else
2201 error = got_repo_match_object_id(&commit_id, NULL,
2202 gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
2203 if (error)
2204 goto done;
2206 error = got_object_id_str(&gw_trans->commit, commit_id);
2207 if (error)
2208 goto done;
2210 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
2211 if (error)
2212 goto done;
2214 error = got_object_open_as_tree(&tree, repo, tree_id);
2215 if (error)
2216 goto done;
2218 nentries = got_object_tree_get_nentries(tree);
2220 for (i = 0; i < nentries; i++) {
2221 struct got_tree_entry *te;
2222 const char *modestr = "";
2223 char *id = NULL, *url_html = NULL;
2225 te = got_object_tree_get_entry(tree, i);
2227 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
2228 if (error)
2229 goto done;
2231 if ((asprintf(&id, "%s", id_str)) == -1) {
2232 error = got_error_from_errno("asprintf");
2233 free(id_str);
2234 goto done;
2237 mode_t mode = got_tree_entry_get_mode(te);
2239 if (got_object_tree_entry_is_submodule(te))
2240 modestr = "$";
2241 else if (S_ISLNK(mode))
2242 modestr = "@";
2243 else if (S_ISDIR(mode))
2244 modestr = "/";
2245 else if (mode & S_IXUSR)
2246 modestr = "*";
2248 char *build_folder = NULL;
2249 if (S_ISDIR(got_tree_entry_get_mode(te))) {
2250 if (gw_trans->repo_folder != NULL) {
2251 if ((asprintf(&build_folder, "%s/%s",
2252 gw_trans->repo_folder,
2253 got_tree_entry_get_name(te))) == -1) {
2254 error =
2255 got_error_from_errno("asprintf");
2256 goto done;
2258 } else {
2259 if (asprintf(&build_folder, "%s",
2260 got_tree_entry_get_name(te)) == -1)
2261 goto done;
2264 if ((asprintf(&url_html, folder_html,
2265 gw_trans->repo_name, gw_trans->action_name,
2266 gw_trans->commit, build_folder,
2267 got_tree_entry_get_name(te), modestr)) == -1) {
2268 error = got_error_from_errno("asprintf");
2269 goto done;
2271 } else {
2272 if (gw_trans->repo_folder != NULL) {
2273 if ((asprintf(&build_folder, "%s",
2274 gw_trans->repo_folder)) == -1) {
2275 error =
2276 got_error_from_errno("asprintf");
2277 goto done;
2279 } else
2280 build_folder = strdup("");
2282 if ((asprintf(&url_html, file_html, gw_trans->repo_name,
2283 "blame", gw_trans->commit,
2284 got_tree_entry_get_name(te), build_folder,
2285 got_tree_entry_get_name(te), modestr)) == -1) {
2286 error = got_error_from_errno("asprintf");
2287 goto done;
2290 free(build_folder);
2292 if (error)
2293 goto done;
2295 if ((asprintf(&tree_row, tree_line, url_html)) == -1) {
2296 error = got_error_from_errno("asprintf");
2297 goto done;
2299 error = buf_puts(&newsize, diffbuf, tree_row);
2300 if (error)
2301 goto done;
2303 free(id);
2304 free(id_str);
2305 free(url_html);
2306 free(tree_row);
2309 if (buf_len(diffbuf) > 0) {
2310 error = buf_putc(diffbuf, '\0');
2311 tree_html = strdup(buf_get(diffbuf));
2313 done:
2314 if (tree)
2315 got_object_tree_close(tree);
2316 if (repo)
2317 got_repo_close(repo);
2319 free(in_repo_path);
2320 free(tree_id);
2321 free(diffbuf);
2322 if (error)
2323 return NULL;
2324 else
2325 return tree_html;
2328 static char *
2329 gw_get_repo_heads(struct gw_trans *gw_trans)
2331 const struct got_error *error = NULL;
2332 struct got_repository *repo = NULL;
2333 struct got_reflist_head refs;
2334 struct got_reflist_entry *re;
2335 char *heads, *head_row = NULL, *head_navs_disp = NULL, *age = NULL;
2336 struct buf *diffbuf = NULL;
2337 size_t newsize;
2339 error = buf_alloc(&diffbuf, 0);
2340 if (error)
2341 return NULL;
2343 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2344 if (error)
2345 goto done;
2347 SIMPLEQ_INIT(&refs);
2348 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
2349 NULL);
2350 if (error)
2351 goto done;
2353 SIMPLEQ_FOREACH(re, &refs, entry) {
2354 char *refname;
2356 refname = strdup(got_ref_get_name(re->ref));
2357 if (refname == NULL) {
2358 error = got_error_from_errno("got_ref_to_str");
2359 goto done;
2362 if (strncmp(refname, "refs/heads/", 11) != 0) {
2363 free(refname);
2364 continue;
2367 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path, refname,
2368 TM_DIFF);
2370 if ((asprintf(&head_navs_disp, heads_navs, gw_trans->repo_name,
2371 refname, gw_trans->repo_name, refname,
2372 gw_trans->repo_name, refname, gw_trans->repo_name,
2373 refname)) == -1) {
2374 error = got_error_from_errno("asprintf");
2375 goto done;
2378 if (strncmp(refname, "refs/heads/", 11) == 0)
2379 refname += 11;
2381 if ((asprintf(&head_row, heads_row, age, refname,
2382 head_navs_disp)) == -1) {
2383 error = got_error_from_errno("asprintf");
2384 goto done;
2387 error = buf_puts(&newsize, diffbuf, head_row);
2389 free(head_navs_disp);
2390 free(head_row);
2393 if (buf_len(diffbuf) > 0) {
2394 error = buf_putc(diffbuf, '\0');
2395 heads = strdup(buf_get(diffbuf));
2397 done:
2398 buf_free(diffbuf);
2399 got_ref_list_free(&refs);
2400 if (repo)
2401 got_repo_close(repo);
2402 if (error)
2403 return NULL;
2404 else
2405 return heads;
2408 static char *
2409 gw_get_got_link(struct gw_trans *gw_trans)
2411 char *link;
2413 if ((asprintf(&link, got_link, gw_trans->gw_conf->got_logo_url,
2414 gw_trans->gw_conf->got_logo)) == -1)
2415 return NULL;
2417 return link;
2420 static char *
2421 gw_get_site_link(struct gw_trans *gw_trans)
2423 char *link, *repo = "", *action = "";
2425 if (gw_trans->repo_name != NULL)
2426 if ((asprintf(&repo, " / <a href='?path=%s&action=summary'>%s" \
2427 "</a>", gw_trans->repo_name, gw_trans->repo_name)) == -1)
2428 return NULL;
2430 if (gw_trans->action_name != NULL)
2431 if ((asprintf(&action, " / %s", gw_trans->action_name)) == -1)
2432 return NULL;
2434 if ((asprintf(&link, site_link, GOTWEB,
2435 gw_trans->gw_conf->got_site_link, repo, action)) == -1)
2436 return NULL;
2438 return link;
2441 static char *
2442 gw_colordiff_line(char *buf)
2444 const struct got_error *error = NULL;
2445 char *colorized_line = NULL, *div_diff_line_div = NULL, *color = NULL;
2446 struct buf *diffbuf = NULL;
2447 size_t newsize;
2449 error = buf_alloc(&diffbuf, 0);
2450 if (error)
2451 return NULL;
2453 if (buf == NULL)
2454 return NULL;
2455 if (strncmp(buf, "-", 1) == 0)
2456 color = "diff_minus";
2457 if (strncmp(buf, "+", 1) == 0)
2458 color = "diff_plus";
2459 if (strncmp(buf, "@@", 2) == 0)
2460 color = "diff_chunk_header";
2461 if (strncmp(buf, "@@", 2) == 0)
2462 color = "diff_chunk_header";
2463 if (strncmp(buf, "commit +", 8) == 0)
2464 color = "diff_meta";
2465 if (strncmp(buf, "commit -", 8) == 0)
2466 color = "diff_meta";
2467 if (strncmp(buf, "blob +", 6) == 0)
2468 color = "diff_meta";
2469 if (strncmp(buf, "blob -", 6) == 0)
2470 color = "diff_meta";
2471 if (strncmp(buf, "file +", 6) == 0)
2472 color = "diff_meta";
2473 if (strncmp(buf, "file -", 6) == 0)
2474 color = "diff_meta";
2475 if (strncmp(buf, "from:", 5) == 0)
2476 color = "diff_author";
2477 if (strncmp(buf, "via:", 4) == 0)
2478 color = "diff_author";
2479 if (strncmp(buf, "date:", 5) == 0)
2480 color = "diff_date";
2482 if ((asprintf(&div_diff_line_div, div_diff_line, color)) == -1)
2483 return NULL;
2485 error = buf_puts(&newsize, diffbuf, div_diff_line_div);
2486 if (error)
2487 return NULL;
2489 error = buf_puts(&newsize, diffbuf, buf);
2490 if (error)
2491 return NULL;
2493 if (buf_len(diffbuf) > 0) {
2494 error = buf_putc(diffbuf, '\0');
2495 colorized_line = strdup(buf_get(diffbuf));
2498 free(diffbuf);
2499 free(div_diff_line_div);
2500 return colorized_line;
2503 static char *
2504 gw_html_escape(const char *html)
2506 char *escaped_str = NULL, *buf;
2507 char c[1];
2508 size_t sz, i, buff_sz = 2048;
2510 if ((buf = calloc(buff_sz, sizeof(char *))) == NULL)
2511 return NULL;
2513 if (html == NULL)
2514 return NULL;
2515 else
2516 if ((sz = strlen(html)) == 0)
2517 return NULL;
2519 /* only work with buff_sz */
2520 if (buff_sz < sz)
2521 sz = buff_sz;
2523 for (i = 0; i < sz; i++) {
2524 c[0] = html[i];
2525 switch (c[0]) {
2526 case ('>'):
2527 strcat(buf, "&gt;");
2528 break;
2529 case ('&'):
2530 strcat(buf, "&amp;");
2531 break;
2532 case ('<'):
2533 strcat(buf, "&lt;");
2534 break;
2535 case ('"'):
2536 strcat(buf, "&quot;");
2537 break;
2538 case ('\''):
2539 strcat(buf, "&apos;");
2540 break;
2541 case ('\n'):
2542 strcat(buf, "<br />");
2543 default:
2544 strcat(buf, &c[0]);
2545 break;
2548 asprintf(&escaped_str, "%s", buf);
2549 free(buf);
2550 return escaped_str;
2553 int
2554 main(int argc, char *argv[])
2556 const struct got_error *error = NULL;
2557 struct gw_trans *gw_trans;
2558 struct gw_dir *dir = NULL, *tdir;
2559 const char *page = "index";
2560 int gw_malloc = 1;
2562 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
2563 errx(1, "malloc");
2565 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
2566 errx(1, "malloc");
2568 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
2569 errx(1, "malloc");
2571 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
2572 errx(1, "malloc");
2574 if (KCGI_OK != khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX,
2575 &page, 1, 0))
2576 errx(1, "khttp_parse");
2578 if ((gw_trans->gw_conf =
2579 malloc(sizeof(struct gotweb_conf))) == NULL) {
2580 gw_malloc = 0;
2581 error = got_error_from_errno("malloc");
2582 goto err;
2585 TAILQ_INIT(&gw_trans->gw_dirs);
2586 TAILQ_INIT(&gw_trans->gw_headers);
2588 gw_trans->page = 0;
2589 gw_trans->repos_total = 0;
2590 gw_trans->repo_path = NULL;
2591 gw_trans->commit = NULL;
2592 gw_trans->headref = strdup(GOT_REF_HEAD);
2593 gw_trans->mime = KMIME_TEXT_HTML;
2594 gw_trans->gw_tmpl->key = gw_templs;
2595 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
2596 gw_trans->gw_tmpl->arg = gw_trans;
2597 gw_trans->gw_tmpl->cb = gw_template;
2598 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
2600 err:
2601 if (error) {
2602 gw_trans->mime = KMIME_TEXT_PLAIN;
2603 gw_trans->action = GW_ERR;
2604 gw_display_index(gw_trans, error);
2605 goto done;
2608 error = gw_parse_querystring(gw_trans);
2609 if (error)
2610 goto err;
2612 gw_display_index(gw_trans, error);
2614 done:
2615 if (gw_malloc) {
2616 free(gw_trans->gw_conf->got_repos_path);
2617 free(gw_trans->gw_conf->got_www_path);
2618 free(gw_trans->gw_conf->got_site_name);
2619 free(gw_trans->gw_conf->got_site_owner);
2620 free(gw_trans->gw_conf->got_site_link);
2621 free(gw_trans->gw_conf->got_logo);
2622 free(gw_trans->gw_conf->got_logo_url);
2623 free(gw_trans->gw_conf);
2624 free(gw_trans->commit);
2625 free(gw_trans->repo_path);
2626 free(gw_trans->repo_name);
2627 free(gw_trans->repo_file);
2628 free(gw_trans->action_name);
2629 free(gw_trans->headref);
2631 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
2632 free(dir->name);
2633 free(dir->description);
2634 free(dir->age);
2635 free(dir->url);
2636 free(dir->path);
2637 free(dir);
2642 khttp_free(gw_trans->gw_req);
2643 return EXIT_SUCCESS;