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 type1, type2;
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 error = got_repo_match_object_id(&id1, &label1, header->parent_id,
1307 GOT_OBJ_TYPE_ANY, 1, header->repo);
1308 if (error)
1309 goto done;
1311 error = got_repo_match_object_id(&id2, &label2,
1312 header->commit_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
1313 if (error)
1314 goto done;
1316 error = got_object_get_type(&type1, header->repo, id1);
1317 if (error)
1318 goto done;
1320 error = got_object_get_type(&type2, header->repo, id2);
1321 if (error)
1322 goto done;
1324 if (type1 != type2) {
1325 error = got_error(GOT_ERR_OBJ_TYPE);
1326 goto done;
1329 switch (type1) {
1330 case GOT_OBJ_TYPE_BLOB:
1331 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
1332 header->repo, f);
1333 break;
1334 case GOT_OBJ_TYPE_TREE:
1335 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
1336 header->repo, f);
1337 break;
1338 case GOT_OBJ_TYPE_COMMIT:
1339 error = got_diff_objects_as_commits(id1, id2, 3, 0,
1340 header->repo, f);
1341 break;
1342 default:
1343 error = got_error(GOT_ERR_OBJ_TYPE);
1346 if ((buf = calloc(128, sizeof(char *))) == NULL)
1347 goto done;
1349 fseek(f, 0, SEEK_SET);
1351 while ((fgets(buf, 128, f)) != NULL) {
1352 n_buf = buf;
1353 while (*n_buf == '\n')
1354 n_buf++;
1355 newline = strchr(n_buf, '\n');
1356 if (newline)
1357 *newline = ' ';
1359 buf_color = gw_colordiff_line(gw_html_escape(n_buf));
1360 error = buf_puts(&newsize, diffbuf, buf_color);
1361 if (error)
1362 return NULL;
1364 error = buf_puts(&newsize, diffbuf, div_end);
1365 if (error)
1366 return NULL;
1369 if (buf_len(diffbuf) > 0) {
1370 error = buf_putc(diffbuf, '\0');
1371 diff_html = strdup(buf_get(diffbuf));
1373 done:
1374 fclose(f);
1375 free(buf_color);
1376 free(buf);
1377 free(diffbuf);
1378 free(label1);
1379 free(label2);
1380 free(id1);
1381 free(id2);
1383 if (error)
1384 return NULL;
1385 else
1386 return diff_html;
1389 static char *
1390 gw_get_repo_owner(struct gw_trans *gw_trans, char *dir)
1392 FILE *f;
1393 char *owner = NULL, *d_file = NULL;
1394 char *gotweb = "[gotweb]", *gitweb = "[gitweb]", *gw_owner = "owner";
1395 char *comp, *pos, *buf;
1396 unsigned int i;
1398 if (gw_trans->gw_conf->got_show_repo_owner == false)
1399 goto err;
1401 if ((asprintf(&d_file, "%s/config", dir)) == -1)
1402 goto err;
1404 if ((f = fopen(d_file, "r")) == NULL)
1405 goto err;
1407 if ((buf = calloc(128, sizeof(char *))) == NULL)
1408 goto err;
1410 while ((fgets(buf, 128, f)) != NULL) {
1411 if ((pos = strstr(buf, gotweb)) != NULL)
1412 break;
1414 if ((pos = strstr(buf, gitweb)) != NULL)
1415 break;
1418 if (pos == NULL)
1419 goto err;
1421 do {
1422 fgets(buf, 128, f);
1423 } while ((comp = strcasestr(buf, gw_owner)) == NULL);
1425 if (comp == NULL)
1426 goto err;
1428 if (strncmp(gw_owner, comp, strlen(gw_owner)) != 0)
1429 goto err;
1431 for (i = 0; i < 2; i++) {
1432 owner = strsep(&buf, "\"");
1435 if (owner == NULL)
1436 goto err;
1438 fclose(f);
1439 free(d_file);
1440 return owner;
1441 err:
1442 if ((asprintf(&owner, "%s", "")) == -1)
1443 return NULL;
1445 return owner;
1448 static char *
1449 gw_get_clone_url(struct gw_trans *gw_trans, char *dir)
1451 FILE *f;
1452 char *url = NULL, *d_file = NULL;
1453 unsigned int len;
1455 if ((asprintf(&d_file, "%s/cloneurl", dir)) == -1)
1456 return NULL;
1458 if ((f = fopen(d_file, "r")) == NULL)
1459 return NULL;
1461 fseek(f, 0, SEEK_END);
1462 len = ftell(f) + 1;
1463 fseek(f, 0, SEEK_SET);
1465 if ((url = calloc(len, sizeof(char *))) == NULL)
1466 return NULL;
1468 fread(url, 1, len, f);
1469 fclose(f);
1470 free(d_file);
1471 return url;
1474 static char *
1475 gw_get_repo_tags(struct gw_trans *gw_trans, int limit, int tag_type)
1477 const struct got_error *error = NULL;
1478 struct got_repository *repo = NULL;
1479 struct got_reflist_head refs;
1480 struct got_reflist_entry *re;
1481 char *tags = NULL, *tag_row = NULL, *tags_navs_disp = NULL,
1482 *age = NULL;
1483 char *newline;
1484 struct buf *diffbuf = NULL;
1485 size_t newsize;
1487 error = buf_alloc(&diffbuf, 0);
1488 if (error)
1489 return NULL;
1490 SIMPLEQ_INIT(&refs);
1492 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1493 if (error)
1494 goto done;
1496 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
1497 if (error)
1498 goto done;
1500 SIMPLEQ_FOREACH(re, &refs, entry) {
1501 const char *refname;
1502 char *refstr, *tag_commit0, *tag_commit, *id_str;
1503 time_t tagger_time;
1504 struct got_object_id *id;
1505 struct got_tag_object *tag;
1507 refname = got_ref_get_name(re->ref);
1508 if (strncmp(refname, "refs/tags/", 10) != 0)
1509 continue;
1510 refname += 10;
1511 refstr = got_ref_to_str(re->ref);
1512 if (refstr == NULL) {
1513 error = got_error_from_errno("got_ref_to_str");
1514 goto done;
1517 error = got_ref_resolve(&id, repo, re->ref);
1518 if (error)
1519 goto done;
1520 error = got_object_open_as_tag(&tag, repo, id);
1521 free(id);
1522 if (error)
1523 goto done;
1525 tagger_time = got_object_tag_get_tagger_time(tag);
1527 error = got_object_id_str(&id_str,
1528 got_object_tag_get_object_id(tag));
1529 if (error)
1530 goto done;
1532 tag_commit0 = strdup(got_object_tag_get_message(tag));
1534 if (tag_commit0 == NULL) {
1535 error = got_error_from_errno("strdup");
1536 goto done;
1539 tag_commit = tag_commit0;
1540 while (*tag_commit == '\n')
1541 tag_commit++;
1543 switch (tag_type) {
1544 case TAGBRIEF:
1545 newline = strchr(tag_commit, '\n');
1546 if (newline)
1547 *newline = '\0';
1549 if ((asprintf(&age, "%s", gw_get_time_str(tagger_time,
1550 TM_DIFF))) == -1) {
1551 error = got_error_from_errno("asprintf");
1552 goto done;
1555 if ((asprintf(&tags_navs_disp, tags_navs,
1556 gw_trans->repo_name, id_str, gw_trans->repo_name,
1557 id_str, gw_trans->repo_name, id_str,
1558 gw_trans->repo_name, id_str)) == -1) {
1559 error = got_error_from_errno("asprintf");
1560 goto done;
1563 if ((asprintf(&tag_row, tags_row, age, refname,
1564 tag_commit, tags_navs_disp)) == -1) {
1565 error = got_error_from_errno("asprintf");
1566 goto done;
1569 free(tags_navs_disp);
1570 break;
1571 case TAGFULL:
1572 break;
1573 default:
1574 break;
1577 got_object_tag_close(tag);
1579 error = buf_puts(&newsize, diffbuf, tag_row);
1581 free(id_str);
1582 free(refstr);
1583 free(age);
1584 free(tag_commit0);
1585 free(tag_row);
1587 if (error || (limit && --limit == 0))
1588 break;
1591 if (buf_len(diffbuf) > 0) {
1592 error = buf_putc(diffbuf, '\0');
1593 tags = strdup(buf_get(diffbuf));
1595 done:
1596 buf_free(diffbuf);
1597 got_ref_list_free(&refs);
1598 if (repo)
1599 got_repo_close(repo);
1600 if (error)
1601 return NULL;
1602 else
1603 return tags;
1606 static void
1607 gw_free_headers(struct gw_header *header)
1609 free(header->id);
1610 free(header->path);
1611 if (header->commit != NULL)
1612 got_object_commit_close(header->commit);
1613 if (header->repo)
1614 got_repo_close(header->repo);
1615 free(header->refs_str);
1616 free(header->commit_id);
1617 free(header->parent_id);
1618 free(header->tree_id);
1619 free(header->author);
1620 free(header->committer);
1621 free(header->commit_msg);
1624 static struct gw_header *
1625 gw_init_header()
1627 struct gw_header *header;
1629 if ((header = malloc(sizeof(*header))) == NULL)
1630 return NULL;
1632 header->repo = NULL;
1633 header->commit = NULL;
1634 header->id = NULL;
1635 header->path = NULL;
1637 return header;
1640 static const struct got_error *
1641 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
1642 int limit)
1644 const struct got_error *error = NULL;
1645 struct got_commit_graph *graph = NULL;
1647 error = got_commit_graph_open(&graph, header->path, 0);
1648 if (error)
1649 goto done;
1651 error = got_commit_graph_iter_start(graph, header->id, header->repo,
1652 NULL, NULL);
1653 if (error)
1654 goto done;
1656 for (;;) {
1657 error = got_commit_graph_iter_next(&header->id, graph,
1658 header->repo, NULL, NULL);
1659 if (error) {
1660 if (error->code == GOT_ERR_ITER_COMPLETED)
1661 error = NULL;
1662 goto done;
1664 if (header->id == NULL)
1665 goto done;
1667 error = got_object_open_as_commit(&header->commit, header->repo,
1668 header->id);
1669 if (error)
1670 goto done;
1672 error = gw_get_commit(gw_trans, header);
1673 if (limit > 1) {
1674 struct gw_header *n_header = NULL;
1675 if ((n_header = gw_init_header()) == NULL)
1676 error = got_error_from_errno("malloc");
1678 n_header->refs_str = strdup(header->refs_str);
1679 n_header->commit_id = strdup(header->commit_id);
1680 n_header->parent_id = strdup(header->parent_id);
1681 n_header->tree_id = strdup(header->tree_id);
1682 n_header->author = strdup(header->author);
1683 n_header->committer = strdup(header->committer);
1684 n_header->commit_msg = strdup(header->commit_msg);
1685 n_header->committer_time = header->committer_time;
1686 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
1687 entry);
1689 if (error || (limit && --limit == 0))
1690 break;
1692 done:
1693 if (graph)
1694 got_commit_graph_close(graph);
1695 return error;
1698 static const struct got_error *
1699 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
1701 const struct got_error *error = NULL;
1702 struct got_reflist_entry *re;
1703 struct got_object_id *id2 = NULL;
1704 struct got_object_qid *parent_id;
1705 char *refs_str = NULL,
1706 *commit_msg = NULL, *commit_msg0;
1708 /*print commit*/
1709 SIMPLEQ_FOREACH(re, &header->refs, entry) {
1710 char *s;
1711 const char *name;
1712 struct got_tag_object *tag = NULL;
1713 int cmp;
1715 name = got_ref_get_name(re->ref);
1716 if (strcmp(name, GOT_REF_HEAD) == 0)
1717 continue;
1718 if (strncmp(name, "refs/", 5) == 0)
1719 name += 5;
1720 if (strncmp(name, "got/", 4) == 0)
1721 continue;
1722 if (strncmp(name, "heads/", 6) == 0)
1723 name += 6;
1724 if (strncmp(name, "remotes/", 8) == 0)
1725 name += 8;
1726 if (strncmp(name, "tags/", 5) == 0) {
1727 error = got_object_open_as_tag(&tag, header->repo,
1728 re->id);
1729 if (error) {
1730 if (error->code != GOT_ERR_OBJ_TYPE)
1731 continue;
1733 * Ref points at something other
1734 * than a tag.
1736 error = NULL;
1737 tag = NULL;
1740 cmp = got_object_id_cmp(tag ?
1741 got_object_tag_get_object_id(tag) : re->id, header->id);
1742 if (tag)
1743 got_object_tag_close(tag);
1744 if (cmp != 0)
1745 continue;
1746 s = refs_str;
1747 if ((asprintf(&refs_str, "%s%s%s", s ? s : "",
1748 s ? ", " : "", name)) == -1) {
1749 error = got_error_from_errno("asprintf");
1750 free(s);
1751 return error;
1753 header->refs_str = strdup(refs_str);
1754 free(s);
1757 if (refs_str == NULL)
1758 header->refs_str = strdup("");
1759 free(refs_str);
1761 error = got_object_id_str(&header->commit_id, header->id);
1762 if (error)
1763 return error;
1765 error = got_object_id_str(&header->tree_id,
1766 got_object_commit_get_tree_id(header->commit));
1767 if (error)
1768 return error;
1770 if (gw_trans->action == GW_DIFF) {
1771 parent_id = SIMPLEQ_FIRST(
1772 got_object_commit_get_parent_ids(header->commit));
1773 if (parent_id != NULL) {
1774 id2 = got_object_id_dup(parent_id->id);
1775 free (parent_id);
1776 error = got_object_id_str(&header->parent_id, id2);
1777 if (error)
1778 return error;
1779 free(id2);
1780 } else
1781 header->parent_id = strdup("/dev/null");
1782 } else
1783 header->parent_id = strdup("");
1785 header->committer_time =
1786 got_object_commit_get_committer_time(header->commit);
1787 header->author = strdup(got_object_commit_get_author(header->commit));
1788 header->committer =
1789 strdup(got_object_commit_get_committer(header->commit));
1791 error = got_object_commit_get_logmsg(&commit_msg0, header->commit);
1792 if (error)
1793 return error;
1795 commit_msg = commit_msg0;
1796 while (*commit_msg == '\n')
1797 commit_msg++;
1799 header->commit_msg = strdup(commit_msg);
1800 free(commit_msg0);
1801 return error;
1804 static const struct got_error *
1805 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
1807 const struct got_error *error = NULL;
1808 char *in_repo_path = NULL;
1810 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
1811 if (error)
1812 return error;
1814 SIMPLEQ_INIT(&header->refs);
1816 if (gw_trans->commit == NULL) {
1817 struct got_reference *head_ref;
1818 error = got_ref_open(&head_ref, header->repo,
1819 gw_trans->headref, 0);
1820 if (error)
1821 return error;
1823 error = got_ref_resolve(&header->id, header->repo, head_ref);
1824 got_ref_close(head_ref);
1825 if (error)
1826 return error;
1828 error = got_object_open_as_commit(&header->commit,
1829 header->repo, header->id);
1830 } else {
1831 struct got_reference *ref;
1832 error = got_ref_open(&ref, header->repo, gw_trans->commit, 0);
1833 if (error == NULL) {
1834 int obj_type;
1835 error = got_ref_resolve(&header->id, header->repo, ref);
1836 got_ref_close(ref);
1837 if (error)
1838 return error;
1839 error = got_object_get_type(&obj_type, header->repo,
1840 header->id);
1841 if (error)
1842 return error;
1843 if (obj_type == GOT_OBJ_TYPE_TAG) {
1844 struct got_tag_object *tag;
1845 error = got_object_open_as_tag(&tag,
1846 header->repo, header->id);
1847 if (error)
1848 return error;
1849 if (got_object_tag_get_object_type(tag) !=
1850 GOT_OBJ_TYPE_COMMIT) {
1851 got_object_tag_close(tag);
1852 error = got_error(GOT_ERR_OBJ_TYPE);
1853 return error;
1855 free(header->id);
1856 header->id = got_object_id_dup(
1857 got_object_tag_get_object_id(tag));
1858 if (header->id == NULL)
1859 error = got_error_from_errno(
1860 "got_object_id_dup");
1861 got_object_tag_close(tag);
1862 if (error)
1863 return error;
1864 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1865 error = got_error(GOT_ERR_OBJ_TYPE);
1866 return error;
1868 error = got_object_open_as_commit(&header->commit,
1869 header->repo, header->id);
1870 if (error)
1871 return error;
1873 if (header->commit == NULL) {
1874 error = got_repo_match_object_id_prefix(&header->id,
1875 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
1876 header->repo);
1877 if (error)
1878 return error;
1880 error = got_repo_match_object_id_prefix(&header->id,
1881 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
1882 header->repo);
1885 error = got_repo_map_path(&in_repo_path, header->repo,
1886 gw_trans->repo_path, 1);
1887 if (error)
1888 return error;
1890 if (in_repo_path) {
1891 header->path = strdup(in_repo_path);
1893 free(in_repo_path);
1895 error = got_ref_list(&header->refs, header->repo, NULL,
1896 got_ref_cmp_by_name, NULL);
1897 if (error)
1898 return error;
1900 error = gw_get_commits(gw_trans, header, limit);
1901 return error;
1904 struct blame_line {
1905 int annotated;
1906 char *id_str;
1907 char *committer;
1908 char datebuf[11]; /* YYYY-MM-DD + NUL */
1911 struct gw_blame_cb_args {
1912 struct blame_line *lines;
1913 int nlines;
1914 int nlines_prec;
1915 int lineno_cur;
1916 off_t *line_offsets;
1917 FILE *f;
1918 struct got_repository *repo;
1919 struct gw_trans *gw_trans;
1920 struct buf *blamebuf;
1923 static const struct got_error *
1924 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1926 const struct got_error *err = NULL;
1927 struct gw_blame_cb_args *a = arg;
1928 struct blame_line *bline;
1929 char *line = NULL;
1930 size_t linesize = 0, newsize;
1931 struct got_commit_object *commit = NULL;
1932 off_t offset;
1933 struct tm tm;
1934 time_t committer_time;
1936 if (nlines != a->nlines ||
1937 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1938 return got_error(GOT_ERR_RANGE);
1940 if (lineno == -1)
1941 return NULL; /* no change in this commit */
1943 /* Annotate this line. */
1944 bline = &a->lines[lineno - 1];
1945 if (bline->annotated)
1946 return NULL;
1947 err = got_object_id_str(&bline->id_str, id);
1948 if (err)
1949 return err;
1951 err = got_object_open_as_commit(&commit, a->repo, id);
1952 if (err)
1953 goto done;
1955 bline->committer = strdup(got_object_commit_get_committer(commit));
1956 if (bline->committer == NULL) {
1957 err = got_error_from_errno("strdup");
1958 goto done;
1961 committer_time = got_object_commit_get_committer_time(commit);
1962 if (localtime_r(&committer_time, &tm) == NULL)
1963 return got_error_from_errno("localtime_r");
1964 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
1965 &tm) >= sizeof(bline->datebuf)) {
1966 err = got_error(GOT_ERR_NO_SPACE);
1967 goto done;
1969 bline->annotated = 1;
1971 /* Print lines annotated so far. */
1972 bline = &a->lines[a->lineno_cur - 1];
1973 if (!bline->annotated)
1974 goto done;
1976 offset = a->line_offsets[a->lineno_cur - 1];
1977 if (fseeko(a->f, offset, SEEK_SET) == -1) {
1978 err = got_error_from_errno("fseeko");
1979 goto done;
1982 while (bline->annotated) {
1983 char *smallerthan, *at, *nl, *committer, *blame_row = NULL,
1984 *line_escape = NULL;
1985 size_t len;
1987 if (getline(&line, &linesize, a->f) == -1) {
1988 if (ferror(a->f))
1989 err = got_error_from_errno("getline");
1990 break;
1993 committer = bline->committer;
1994 smallerthan = strchr(committer, '<');
1995 if (smallerthan && smallerthan[1] != '\0')
1996 committer = smallerthan + 1;
1997 at = strchr(committer, '@');
1998 if (at)
1999 *at = '\0';
2000 len = strlen(committer);
2001 if (len >= 9)
2002 committer[8] = '\0';
2004 nl = strchr(line, '\n');
2005 if (nl)
2006 *nl = '\0';
2008 if (strcmp(line, "") != 0)
2009 line_escape = strdup(gw_html_escape(line));
2010 else
2011 line_escape = strdup("");
2013 asprintf(&blame_row, blame_line, a->nlines_prec,
2014 a->lineno_cur, bline->id_str, bline->datebuf, committer,
2015 line_escape);
2016 a->lineno_cur++;
2017 err = buf_puts(&newsize, a->blamebuf, blame_row);
2018 if (err)
2019 return err;
2021 bline = &a->lines[a->lineno_cur - 1];
2022 free(line_escape);
2023 free(blame_row);
2025 done:
2026 if (commit)
2027 got_object_commit_close(commit);
2028 free(line);
2029 return err;
2032 static char*
2033 gw_get_file_blame(struct gw_trans *gw_trans)
2035 const struct got_error *error = NULL;
2036 struct got_repository *repo = NULL;
2037 struct got_object_id *obj_id = NULL;
2038 struct got_object_id *commit_id = NULL;
2039 struct got_blob_object *blob = NULL;
2040 char *blame_html = NULL, *path = NULL, *in_repo_path = NULL,
2041 *folder = NULL;
2042 struct gw_blame_cb_args bca;
2043 int i, obj_type;
2044 size_t filesize;
2046 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2047 if (error)
2048 goto done;
2050 if (gw_trans->repo_folder != NULL) {
2051 if ((asprintf(&folder, "%s/", gw_trans->repo_folder)) == -1) {
2052 error = got_error_from_errno("asprintf");
2053 goto done;
2055 } else
2056 folder = strdup("");
2058 if ((asprintf(&path, "%s%s", folder, gw_trans->repo_file)) == -1) {
2059 error = got_error_from_errno("asprintf");
2060 goto done;
2062 free(folder);
2064 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2065 if (error)
2066 goto done;
2068 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
2069 GOT_OBJ_TYPE_COMMIT, 1, repo);
2070 if (error)
2071 goto done;
2073 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2074 if (error)
2075 goto done;
2077 if (obj_id == NULL) {
2078 error = got_error(GOT_ERR_NO_OBJ);
2079 goto done;
2082 error = got_object_get_type(&obj_type, repo, obj_id);
2083 if (error)
2084 goto done;
2086 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2087 error = got_error(GOT_ERR_OBJ_TYPE);
2088 goto done;
2091 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2092 if (error)
2093 goto done;
2095 error = buf_alloc(&bca.blamebuf, 0);
2096 if (error)
2097 goto done;
2099 bca.f = got_opentemp();
2100 if (bca.f == NULL) {
2101 error = got_error_from_errno("got_opentemp");
2102 goto done;
2104 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2105 &bca.line_offsets, bca.f, blob);
2106 if (error || bca.nlines == 0)
2107 goto done;
2109 /* Don't include \n at EOF in the blame line count. */
2110 if (bca.line_offsets[bca.nlines - 1] == filesize)
2111 bca.nlines--;
2113 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2114 if (bca.lines == NULL) {
2115 error = got_error_from_errno("calloc");
2116 goto done;
2118 bca.lineno_cur = 1;
2119 bca.nlines_prec = 0;
2120 i = bca.nlines;
2121 while (i > 0) {
2122 i /= 10;
2123 bca.nlines_prec++;
2125 bca.repo = repo;
2126 bca.gw_trans = gw_trans;
2128 error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
2129 NULL, NULL);
2130 if (buf_len(bca.blamebuf) > 0) {
2131 error = buf_putc(bca.blamebuf, '\0');
2132 blame_html = strdup(buf_get(bca.blamebuf));
2134 done:
2135 free(bca.blamebuf);
2136 free(in_repo_path);
2137 free(commit_id);
2138 free(obj_id);
2139 free(path);
2141 if (blob)
2142 error = got_object_blob_close(blob);
2143 if (repo)
2144 error = got_repo_close(repo);
2145 if (error)
2146 return NULL;
2147 if (bca.lines) {
2148 for (i = 0; i < bca.nlines; i++) {
2149 struct blame_line *bline = &bca.lines[i];
2150 free(bline->id_str);
2151 free(bline->committer);
2153 free(bca.lines);
2155 free(bca.line_offsets);
2156 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2157 error = got_error_from_errno("fclose");
2158 if (error)
2159 return NULL;
2160 else
2161 return blame_html;
2164 static char*
2165 gw_get_repo_tree(struct gw_trans *gw_trans)
2167 const struct got_error *error = NULL;
2168 struct got_repository *repo = NULL;
2169 struct got_object_id *tree_id = NULL, *commit_id = NULL;
2170 struct got_tree_object *tree = NULL;
2171 struct buf *diffbuf = NULL;
2172 size_t newsize;
2173 char *tree_html = NULL, *path = NULL, *in_repo_path = NULL,
2174 *tree_row = NULL, *id_str;
2175 int nentries, i;
2177 error = buf_alloc(&diffbuf, 0);
2178 if (error)
2179 return NULL;
2181 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2182 if (error)
2183 goto done;
2185 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
2186 if (error)
2187 goto done;
2189 if (gw_trans->repo_folder != NULL)
2190 path = strdup(gw_trans->repo_folder);
2191 else if (in_repo_path) {
2192 free(path);
2193 path = in_repo_path;
2196 if (gw_trans->commit == NULL) {
2197 struct got_reference *head_ref;
2198 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
2199 if (error)
2200 goto done;
2202 error = got_ref_resolve(&commit_id, repo, head_ref);
2203 got_ref_close(head_ref);
2205 } else
2206 error = got_repo_match_object_id(&commit_id, NULL,
2207 gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
2208 if (error)
2209 goto done;
2211 error = got_object_id_str(&gw_trans->commit, commit_id);
2212 if (error)
2213 goto done;
2215 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
2216 if (error)
2217 goto done;
2219 error = got_object_open_as_tree(&tree, repo, tree_id);
2220 if (error)
2221 goto done;
2223 nentries = got_object_tree_get_nentries(tree);
2225 for (i = 0; i < nentries; i++) {
2226 struct got_tree_entry *te;
2227 const char *modestr = "";
2228 char *id = NULL, *url_html = NULL;
2230 te = got_object_tree_get_entry(tree, i);
2232 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
2233 if (error)
2234 goto done;
2236 if ((asprintf(&id, "%s", id_str)) == -1) {
2237 error = got_error_from_errno("asprintf");
2238 free(id_str);
2239 goto done;
2242 mode_t mode = got_tree_entry_get_mode(te);
2244 if (got_object_tree_entry_is_submodule(te))
2245 modestr = "$";
2246 else if (S_ISLNK(mode))
2247 modestr = "@";
2248 else if (S_ISDIR(mode))
2249 modestr = "/";
2250 else if (mode & S_IXUSR)
2251 modestr = "*";
2253 char *build_folder = NULL;
2254 if (S_ISDIR(got_tree_entry_get_mode(te))) {
2255 if (gw_trans->repo_folder != NULL) {
2256 if ((asprintf(&build_folder, "%s/%s",
2257 gw_trans->repo_folder,
2258 got_tree_entry_get_name(te))) == -1) {
2259 error =
2260 got_error_from_errno("asprintf");
2261 goto done;
2263 } else {
2264 if (asprintf(&build_folder, "%s",
2265 got_tree_entry_get_name(te)) == -1)
2266 goto done;
2269 if ((asprintf(&url_html, folder_html,
2270 gw_trans->repo_name, gw_trans->action_name,
2271 gw_trans->commit, build_folder,
2272 got_tree_entry_get_name(te), modestr)) == -1) {
2273 error = got_error_from_errno("asprintf");
2274 goto done;
2276 } else {
2277 if (gw_trans->repo_folder != NULL) {
2278 if ((asprintf(&build_folder, "%s",
2279 gw_trans->repo_folder)) == -1) {
2280 error =
2281 got_error_from_errno("asprintf");
2282 goto done;
2284 } else
2285 build_folder = strdup("");
2287 if ((asprintf(&url_html, file_html, gw_trans->repo_name,
2288 "blame", gw_trans->commit,
2289 got_tree_entry_get_name(te), build_folder,
2290 got_tree_entry_get_name(te), modestr)) == -1) {
2291 error = got_error_from_errno("asprintf");
2292 goto done;
2295 free(build_folder);
2297 if (error)
2298 goto done;
2300 if ((asprintf(&tree_row, tree_line, url_html)) == -1) {
2301 error = got_error_from_errno("asprintf");
2302 goto done;
2304 error = buf_puts(&newsize, diffbuf, tree_row);
2305 if (error)
2306 goto done;
2308 free(id);
2309 free(id_str);
2310 free(url_html);
2311 free(tree_row);
2314 if (buf_len(diffbuf) > 0) {
2315 error = buf_putc(diffbuf, '\0');
2316 tree_html = strdup(buf_get(diffbuf));
2318 done:
2319 if (tree)
2320 got_object_tree_close(tree);
2321 if (repo)
2322 got_repo_close(repo);
2324 free(in_repo_path);
2325 free(tree_id);
2326 free(diffbuf);
2327 if (error)
2328 return NULL;
2329 else
2330 return tree_html;
2333 static char *
2334 gw_get_repo_heads(struct gw_trans *gw_trans)
2336 const struct got_error *error = NULL;
2337 struct got_repository *repo = NULL;
2338 struct got_reflist_head refs;
2339 struct got_reflist_entry *re;
2340 char *heads, *head_row = NULL, *head_navs_disp = NULL, *age = NULL;
2341 struct buf *diffbuf = NULL;
2342 size_t newsize;
2344 error = buf_alloc(&diffbuf, 0);
2345 if (error)
2346 return NULL;
2348 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2349 if (error)
2350 goto done;
2352 SIMPLEQ_INIT(&refs);
2353 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
2354 NULL);
2355 if (error)
2356 goto done;
2358 SIMPLEQ_FOREACH(re, &refs, entry) {
2359 char *refname;
2361 refname = strdup(got_ref_get_name(re->ref));
2362 if (refname == NULL) {
2363 error = got_error_from_errno("got_ref_to_str");
2364 goto done;
2367 if (strncmp(refname, "refs/heads/", 11) != 0) {
2368 free(refname);
2369 continue;
2372 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path, refname,
2373 TM_DIFF);
2375 if ((asprintf(&head_navs_disp, heads_navs, gw_trans->repo_name,
2376 refname, gw_trans->repo_name, refname,
2377 gw_trans->repo_name, refname, gw_trans->repo_name,
2378 refname)) == -1) {
2379 error = got_error_from_errno("asprintf");
2380 goto done;
2383 if (strncmp(refname, "refs/heads/", 11) == 0)
2384 refname += 11;
2386 if ((asprintf(&head_row, heads_row, age, refname,
2387 head_navs_disp)) == -1) {
2388 error = got_error_from_errno("asprintf");
2389 goto done;
2392 error = buf_puts(&newsize, diffbuf, head_row);
2394 free(head_navs_disp);
2395 free(head_row);
2398 if (buf_len(diffbuf) > 0) {
2399 error = buf_putc(diffbuf, '\0');
2400 heads = strdup(buf_get(diffbuf));
2402 done:
2403 buf_free(diffbuf);
2404 got_ref_list_free(&refs);
2405 if (repo)
2406 got_repo_close(repo);
2407 if (error)
2408 return NULL;
2409 else
2410 return heads;
2413 static char *
2414 gw_get_got_link(struct gw_trans *gw_trans)
2416 char *link;
2418 if ((asprintf(&link, got_link, gw_trans->gw_conf->got_logo_url,
2419 gw_trans->gw_conf->got_logo)) == -1)
2420 return NULL;
2422 return link;
2425 static char *
2426 gw_get_site_link(struct gw_trans *gw_trans)
2428 char *link, *repo = "", *action = "";
2430 if (gw_trans->repo_name != NULL)
2431 if ((asprintf(&repo, " / <a href='?path=%s&action=summary'>%s" \
2432 "</a>", gw_trans->repo_name, gw_trans->repo_name)) == -1)
2433 return NULL;
2435 if (gw_trans->action_name != NULL)
2436 if ((asprintf(&action, " / %s", gw_trans->action_name)) == -1)
2437 return NULL;
2439 if ((asprintf(&link, site_link, GOTWEB,
2440 gw_trans->gw_conf->got_site_link, repo, action)) == -1)
2441 return NULL;
2443 return link;
2446 static char *
2447 gw_colordiff_line(char *buf)
2449 const struct got_error *error = NULL;
2450 char *colorized_line = NULL, *div_diff_line_div = NULL, *color = NULL;
2451 struct buf *diffbuf = NULL;
2452 size_t newsize;
2454 error = buf_alloc(&diffbuf, 0);
2455 if (error)
2456 return NULL;
2458 if (strncmp(buf, "-", 1) == 0)
2459 color = "diff_minus";
2460 if (strncmp(buf, "+", 1) == 0)
2461 color = "diff_plus";
2462 if (strncmp(buf, "@@", 2) == 0)
2463 color = "diff_chunk_header";
2464 if (strncmp(buf, "@@", 2) == 0)
2465 color = "diff_chunk_header";
2466 if (strncmp(buf, "commit +", 8) == 0)
2467 color = "diff_meta";
2468 if (strncmp(buf, "commit -", 8) == 0)
2469 color = "diff_meta";
2470 if (strncmp(buf, "blob +", 6) == 0)
2471 color = "diff_meta";
2472 if (strncmp(buf, "blob -", 6) == 0)
2473 color = "diff_meta";
2474 if (strncmp(buf, "file +", 6) == 0)
2475 color = "diff_meta";
2476 if (strncmp(buf, "file -", 6) == 0)
2477 color = "diff_meta";
2478 if (strncmp(buf, "from:", 5) == 0)
2479 color = "diff_author";
2480 if (strncmp(buf, "via:", 4) == 0)
2481 color = "diff_author";
2482 if (strncmp(buf, "date:", 5) == 0)
2483 color = "diff_date";
2485 if ((asprintf(&div_diff_line_div, div_diff_line, color)) == -1)
2486 return NULL;
2488 error = buf_puts(&newsize, diffbuf, div_diff_line_div);
2489 if (error)
2490 return NULL;
2492 error = buf_puts(&newsize, diffbuf, buf);
2493 if (error)
2494 return NULL;
2496 if (buf_len(diffbuf) > 0) {
2497 error = buf_putc(diffbuf, '\0');
2498 colorized_line = strdup(buf_get(diffbuf));
2501 free(diffbuf);
2502 free(div_diff_line_div);
2503 return colorized_line;
2506 static char *
2507 gw_html_escape(const char *html)
2509 char *escaped_str = NULL, *buf;
2510 char c[1];
2511 size_t sz, i, buff_sz = 2048;
2513 if ((buf = calloc(buff_sz, sizeof(char *))) == NULL)
2514 return NULL;
2516 if (html == NULL)
2517 return NULL;
2518 else
2519 if ((sz = strlen(html)) == 0)
2520 return NULL;
2522 /* only work with buff_sz */
2523 if (buff_sz < sz)
2524 sz = buff_sz;
2526 for (i = 0; i < sz; i++) {
2527 c[0] = html[i];
2528 switch (c[0]) {
2529 case ('>'):
2530 strcat(buf, "&gt;");
2531 break;
2532 case ('&'):
2533 strcat(buf, "&amp;");
2534 break;
2535 case ('<'):
2536 strcat(buf, "&lt;");
2537 break;
2538 case ('"'):
2539 strcat(buf, "&quot;");
2540 break;
2541 case ('\''):
2542 strcat(buf, "&apos;");
2543 break;
2544 case ('\n'):
2545 strcat(buf, "<br />");
2546 default:
2547 strcat(buf, &c[0]);
2548 break;
2551 asprintf(&escaped_str, "%s", buf);
2552 free(buf);
2553 return escaped_str;
2556 int
2557 main(int argc, char *argv[])
2559 const struct got_error *error = NULL;
2560 struct gw_trans *gw_trans;
2561 struct gw_dir *dir = NULL, *tdir;
2562 const char *page = "index";
2563 int gw_malloc = 1;
2565 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
2566 errx(1, "malloc");
2568 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
2569 errx(1, "malloc");
2571 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
2572 errx(1, "malloc");
2574 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
2575 errx(1, "malloc");
2577 if (KCGI_OK != khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX,
2578 &page, 1, 0))
2579 errx(1, "khttp_parse");
2581 if ((gw_trans->gw_conf =
2582 malloc(sizeof(struct gotweb_conf))) == NULL) {
2583 gw_malloc = 0;
2584 error = got_error_from_errno("malloc");
2585 goto err;
2588 TAILQ_INIT(&gw_trans->gw_dirs);
2589 TAILQ_INIT(&gw_trans->gw_headers);
2591 gw_trans->page = 0;
2592 gw_trans->repos_total = 0;
2593 gw_trans->repo_path = NULL;
2594 gw_trans->commit = NULL;
2595 gw_trans->headref = strdup(GOT_REF_HEAD);
2596 gw_trans->mime = KMIME_TEXT_HTML;
2597 gw_trans->gw_tmpl->key = gw_templs;
2598 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
2599 gw_trans->gw_tmpl->arg = gw_trans;
2600 gw_trans->gw_tmpl->cb = gw_template;
2601 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
2603 err:
2604 if (error) {
2605 gw_trans->mime = KMIME_TEXT_PLAIN;
2606 gw_trans->action = GW_ERR;
2607 gw_display_index(gw_trans, error);
2608 goto done;
2611 error = gw_parse_querystring(gw_trans);
2612 if (error)
2613 goto err;
2615 gw_display_index(gw_trans, error);
2617 done:
2618 if (gw_malloc) {
2619 free(gw_trans->gw_conf->got_repos_path);
2620 free(gw_trans->gw_conf->got_www_path);
2621 free(gw_trans->gw_conf->got_site_name);
2622 free(gw_trans->gw_conf->got_site_owner);
2623 free(gw_trans->gw_conf->got_site_link);
2624 free(gw_trans->gw_conf->got_logo);
2625 free(gw_trans->gw_conf->got_logo_url);
2626 free(gw_trans->gw_conf);
2627 free(gw_trans->commit);
2628 free(gw_trans->repo_path);
2629 free(gw_trans->repo_name);
2630 free(gw_trans->repo_file);
2631 free(gw_trans->action_name);
2632 free(gw_trans->headref);
2634 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
2635 free(dir->name);
2636 free(dir->description);
2637 free(dir->age);
2638 free(dir->url);
2639 free(dir->path);
2640 free(dir);
2645 khttp_free(gw_trans->gw_req);
2646 return EXIT_SUCCESS;