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(dirs, gw_dir) gw_dirs;
58 struct gw_dir *gw_dir;
59 struct gotweb_conf *gw_conf;
60 struct ktemplate *gw_tmpl;
61 struct khtmlreq *gw_html_req;
62 struct kreq *gw_req;
63 char *repo_name;
64 char *repo_path;
65 char *commit;
66 char *repo_file;
67 char *repo_folder;
68 char *action_name;
69 char *headref;
70 unsigned int action;
71 unsigned int page;
72 unsigned int repos_total;
73 enum kmime mime;
74 };
76 enum gw_key {
77 KEY_ACTION,
78 KEY_COMMIT_ID,
79 KEY_FILE,
80 KEY_FOLDER,
81 KEY_HEADREF,
82 KEY_PAGE,
83 KEY_PATH,
84 KEY__ZMAX
85 };
87 struct gw_dir {
88 TAILQ_ENTRY(gw_dir) entry;
89 char *name;
90 char *owner;
91 char *description;
92 char *url;
93 char *age;
94 char *path;
95 };
97 enum gw_tmpl {
98 TEMPL_HEAD,
99 TEMPL_HEADER,
100 TEMPL_SITEPATH,
101 TEMPL_SITEOWNER,
102 TEMPL_TITLE,
103 TEMPL_SEARCH,
104 TEMPL_CONTENT,
105 TEMPL__MAX
106 };
108 enum gw_ref_tm {
109 TM_DIFF,
110 TM_LONG,
111 };
113 enum gw_logs {
114 LOGBRIEF,
115 LOGCOMMIT,
116 LOGFULL,
117 LOGTREE,
118 LOGDIFF,
119 LOGBLAME,
120 LOGTAG,
121 };
123 enum gw_tags {
124 TAGBRIEF,
125 TAGFULL,
126 };
128 static const char *const gw_templs[TEMPL__MAX] = {
129 "head",
130 "header",
131 "sitepath",
132 "siteowner",
133 "title",
134 "search",
135 "content",
136 };
138 static const struct kvalid gw_keys[KEY__ZMAX] = {
139 { kvalid_stringne, "action" },
140 { kvalid_stringne, "commit" },
141 { kvalid_stringne, "file" },
142 { kvalid_stringne, "folder" },
143 { kvalid_stringne, "headref" },
144 { kvalid_int, "page" },
145 { kvalid_stringne, "path" },
146 };
148 static struct gw_dir *gw_init_gw_dir(char *);
150 static char *gw_get_repo_description(struct gw_trans *,
151 char *);
152 static char *gw_get_repo_owner(struct gw_trans *,
153 char *);
154 static char *gw_get_time_str(time_t, int);
155 static char *gw_get_repo_age(struct gw_trans *,
156 char *, char *, int);
157 static char *gw_get_repo_log(struct gw_trans *,
158 const char *, char *, int, int);
159 static char *gw_get_file_blame(struct gw_trans *, char *);
160 static char *gw_get_repo_tree(struct gw_trans *, char *);
161 static char *gw_get_repo_diff(struct gw_trans *, char *,
162 char *);
163 static char *gw_get_repo_tags(struct gw_trans *, int, int);
164 static char *gw_get_repo_heads(struct gw_trans *);
165 static char *gw_get_clone_url(struct gw_trans *, char *);
166 static char *gw_get_got_link(struct gw_trans *);
167 static char *gw_get_site_link(struct gw_trans *);
168 static char *gw_html_escape(const char *);
169 static char *gw_colordiff_line(char *);
171 static void gw_display_open(struct gw_trans *, enum khttp,
172 enum kmime);
173 static void gw_display_index(struct gw_trans *,
174 const struct got_error *);
176 static int gw_template(size_t, void *);
178 static const struct got_error* gw_apply_unveil(const char *, const char *);
179 static const struct got_error* gw_blame_cb(void *, int, int,
180 struct got_object_id *);
181 static const struct got_error* gw_load_got_paths(struct gw_trans *);
182 static const struct got_error* gw_load_got_path(struct gw_trans *,
183 struct gw_dir *);
184 static const struct got_error* gw_parse_querystring(struct gw_trans *);
185 static const struct got_error* gw_match_logmsg(int *, struct got_object_id *,
186 struct got_commit_object *, regex_t *);
188 static const struct got_error* gw_blame(struct gw_trans *);
189 static const struct got_error* gw_commit(struct gw_trans *);
190 static const struct got_error* gw_commitdiff(struct gw_trans *);
191 static const struct got_error* gw_index(struct gw_trans *);
192 static const struct got_error* gw_log(struct gw_trans *);
193 static const struct got_error* gw_raw(struct gw_trans *);
194 static const struct got_error* gw_logbriefs(struct gw_trans *);
195 static const struct got_error* gw_summary(struct gw_trans *);
196 static const struct got_error* gw_tag(struct gw_trans *);
197 static const struct got_error* gw_tree(struct gw_trans *);
199 struct gw_query_action {
200 unsigned int func_id;
201 const char *func_name;
202 const struct got_error *(*func_main)(struct gw_trans *);
203 char *template;
204 };
206 enum gw_query_actions {
207 GW_BLAME,
208 GW_COMMIT,
209 GW_COMMITDIFF,
210 GW_ERR,
211 GW_INDEX,
212 GW_LOG,
213 GW_RAW,
214 GW_LOGBRIEFS,
215 GW_SUMMARY,
216 GW_TAG,
217 GW_TREE,
218 };
220 static struct gw_query_action gw_query_funcs[] = {
221 { GW_BLAME, "blame", gw_blame, "gw_tmpl/index.tmpl" },
222 { GW_COMMIT, "commit", gw_commit, "gw_tmpl/index.tmpl" },
223 { GW_COMMITDIFF, "commitdiff", gw_commitdiff, "gw_tmpl/index.tmpl" },
224 { GW_ERR, NULL, NULL, "gw_tmpl/index.tmpl" },
225 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
226 { GW_LOG, "log", gw_log, "gw_tmpl/index.tmpl" },
227 { GW_RAW, "raw", gw_raw, "gw_tmpl/index.tmpl" },
228 { GW_LOGBRIEFS, "logbriefs", gw_logbriefs, "gw_tmpl/index.tmpl" },
229 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/index.tmpl" },
230 { GW_TAG, "tag", gw_tag, "gw_tmpl/index.tmpl" },
231 { GW_TREE, "tree", gw_tree, "gw_tmpl/index.tmpl" },
232 };
234 static const struct got_error *
235 gw_apply_unveil(const char *repo_path, const char *repo_file)
237 const struct got_error *err;
239 if (repo_path && repo_file) {
240 char *full_path;
241 if ((asprintf(&full_path, "%s/%s", repo_path, repo_file)) == -1)
242 return got_error_from_errno("asprintf unveil");
243 if (unveil(full_path, "r") != 0)
244 return got_error_from_errno2("unveil", full_path);
247 if (repo_path && unveil(repo_path, "r") != 0)
248 return got_error_from_errno2("unveil", repo_path);
250 if (unveil("/tmp", "rwc") != 0)
251 return got_error_from_errno2("unveil", "/tmp");
253 err = got_privsep_unveil_exec_helpers();
254 if (err != NULL)
255 return err;
257 if (unveil(NULL, NULL) != 0)
258 return got_error_from_errno("unveil");
260 return NULL;
263 static const struct got_error *
264 gw_blame(struct gw_trans *gw_trans)
266 const struct got_error *error = NULL;
268 char *log, *log_html;
270 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
271 NULL) == -1) {
272 error = got_error_from_errno("pledge");
273 return error;
276 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
277 if (error)
278 return error;
280 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGBLAME);
282 if (log != NULL && strcmp(log, "") != 0) {
283 if ((asprintf(&log_html, log_blame, log)) == -1)
284 return got_error_from_errno("asprintf");
285 khttp_puts(gw_trans->gw_req, log_html);
286 free(log_html);
287 free(log);
289 return error;
292 static const struct got_error *
293 gw_commit(struct gw_trans *gw_trans)
295 const struct got_error *error = NULL;
296 char *log, *log_html;
298 if (pledge("stdio rpath proc exec sendfd unveil",
299 NULL) == -1) {
300 error = got_error_from_errno("pledge");
301 return error;
304 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
305 if (error)
306 return error;
308 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGCOMMIT);
310 if (log != NULL && strcmp(log, "") != 0) {
311 if ((asprintf(&log_html, log_commit, log)) == -1)
312 return got_error_from_errno("asprintf");
313 khttp_puts(gw_trans->gw_req, log_html);
314 free(log_html);
315 free(log);
317 return error;
320 static const struct got_error *
321 gw_commitdiff(struct gw_trans *gw_trans)
323 const struct got_error *error = NULL;
324 char *log, *log_html;
326 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
327 NULL) == -1) {
328 error = got_error_from_errno("pledge");
329 return error;
332 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
333 if (error)
334 return error;
336 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGDIFF);
338 if (log != NULL && strcmp(log, "") != 0) {
339 if ((asprintf(&log_html, log_diff, log)) == -1)
340 return got_error_from_errno("asprintf");
341 khttp_puts(gw_trans->gw_req, log_html);
342 free(log_html);
343 free(log);
345 return error;
348 static const struct got_error *
349 gw_index(struct gw_trans *gw_trans)
351 const struct got_error *error = NULL;
352 struct gw_dir *gw_dir = NULL;
353 char *html, *navs, *next, *prev;
354 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
356 if (pledge("stdio rpath proc exec sendfd unveil",
357 NULL) == -1) {
358 error = got_error_from_errno("pledge");
359 return error;
362 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
363 if (error)
364 return error;
366 error = gw_load_got_paths(gw_trans);
367 if (error)
368 return error;
370 khttp_puts(gw_trans->gw_req, index_projects_header);
372 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
373 dir_c++;
375 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
376 if (gw_trans->page > 0 && (gw_trans->page *
377 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
378 prev_disp++;
379 continue;
382 prev_disp++;
383 if((asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
384 gw_dir->name, gw_dir->name)) == -1)
385 return got_error_from_errno("asprintf");
387 if ((asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
388 gw_dir->description, gw_dir->owner, gw_dir->age,
389 navs)) == -1)
390 return got_error_from_errno("asprintf");
392 khttp_puts(gw_trans->gw_req, html);
394 free(navs);
395 free(html);
397 if (gw_trans->gw_conf->got_max_repos_display == 0)
398 continue;
400 if (next_disp == gw_trans->gw_conf->got_max_repos_display)
401 khttp_puts(gw_trans->gw_req, np_wrapper_start);
402 else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
403 (gw_trans->page > 0) &&
404 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
405 prev_disp == gw_trans->repos_total))
406 khttp_puts(gw_trans->gw_req, np_wrapper_start);
408 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
409 (gw_trans->page > 0) &&
410 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
411 prev_disp == gw_trans->repos_total)) {
412 if ((asprintf(&prev, nav_prev,
413 gw_trans->page - 1)) == -1)
414 return got_error_from_errno("asprintf");
415 khttp_puts(gw_trans->gw_req, prev);
416 free(prev);
419 khttp_puts(gw_trans->gw_req, div_end);
421 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
422 next_disp == gw_trans->gw_conf->got_max_repos_display &&
423 dir_c != (gw_trans->page + 1) *
424 gw_trans->gw_conf->got_max_repos_display) {
425 if ((asprintf(&next, nav_next,
426 gw_trans->page + 1)) == -1)
427 return got_error_from_errno("calloc");
428 khttp_puts(gw_trans->gw_req, next);
429 khttp_puts(gw_trans->gw_req, div_end);
430 free(next);
431 next_disp = 0;
432 break;
435 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
436 (gw_trans->page > 0) &&
437 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
438 prev_disp == gw_trans->repos_total))
439 khttp_puts(gw_trans->gw_req, div_end);
441 next_disp++;
443 return error;
446 static const struct got_error *
447 gw_log(struct gw_trans *gw_trans)
449 const struct got_error *error = NULL;
450 char *log, *log_html;
452 if (pledge("stdio rpath proc exec sendfd unveil",
453 NULL) == -1) {
454 error = got_error_from_errno("pledge");
455 return error;
458 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
459 if (error)
460 return error;
462 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit,
463 gw_trans->gw_conf->got_max_commits_display, LOGFULL);
465 if (log != NULL && strcmp(log, "") != 0) {
466 if ((asprintf(&log_html, logs, log)) == -1)
467 return got_error_from_errno("asprintf");
468 khttp_puts(gw_trans->gw_req, log_html);
469 free(log_html);
470 free(log);
472 return error;
475 static const struct got_error *
476 gw_raw(struct gw_trans *gw_trans)
478 const struct got_error *error = NULL;
480 return error;
483 static const struct got_error *
484 gw_logbriefs(struct gw_trans *gw_trans)
486 const struct got_error *error = NULL;
487 char *log, *log_html;
489 if (pledge("stdio rpath proc exec sendfd unveil",
490 NULL) == -1) {
491 error = got_error_from_errno("pledge");
492 return error;
495 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
496 if (error)
497 return error;
499 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit,
500 gw_trans->gw_conf->got_max_commits_display, LOGBRIEF);
502 if (log != NULL && strcmp(log, "") != 0) {
503 if ((asprintf(&log_html, summary_logbriefs,
504 log)) == -1)
505 return got_error_from_errno("asprintf");
506 khttp_puts(gw_trans->gw_req, log_html);
507 free(log_html);
508 free(log);
510 return error;
513 static const struct got_error *
514 gw_summary(struct gw_trans *gw_trans)
516 const struct got_error *error = NULL;
517 char *description_html, *repo_owner_html, *repo_age_html,
518 *cloneurl_html, *log, *log_html, *tags, *heads, *tags_html,
519 *heads_html, *age;
521 if (pledge("stdio rpath proc exec sendfd unveil",
522 NULL) == -1) {
523 error = got_error_from_errno("pledge");
524 return error;
527 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
528 if (error)
529 return error;
531 khttp_puts(gw_trans->gw_req, summary_wrapper);
532 if (gw_trans->gw_conf->got_show_repo_description) {
533 if (gw_trans->gw_dir->description != NULL &&
534 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
535 if ((asprintf(&description_html, description,
536 gw_trans->gw_dir->description)) == -1)
537 return got_error_from_errno("asprintf");
539 khttp_puts(gw_trans->gw_req, description_html);
540 free(description_html);
544 if (gw_trans->gw_conf->got_show_repo_owner) {
545 if (gw_trans->gw_dir->owner != NULL &&
546 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
547 if ((asprintf(&repo_owner_html, repo_owner,
548 gw_trans->gw_dir->owner)) == -1)
549 return got_error_from_errno("asprintf");
551 khttp_puts(gw_trans->gw_req, repo_owner_html);
552 free(repo_owner_html);
556 if (gw_trans->gw_conf->got_show_repo_age) {
557 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path,
558 "refs/heads", TM_LONG);
559 if (age != NULL && (strcmp(age, "") != 0)) {
560 if ((asprintf(&repo_age_html, last_change, age)) == -1)
561 return got_error_from_errno("asprintf");
563 khttp_puts(gw_trans->gw_req, repo_age_html);
564 free(repo_age_html);
565 free(age);
569 if (gw_trans->gw_conf->got_show_repo_cloneurl) {
570 if (gw_trans->gw_dir->url != NULL &&
571 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
572 if ((asprintf(&cloneurl_html, cloneurl,
573 gw_trans->gw_dir->url)) == -1)
574 return got_error_from_errno("asprintf");
576 khttp_puts(gw_trans->gw_req, cloneurl_html);
577 free(cloneurl_html);
580 khttp_puts(gw_trans->gw_req, div_end);
582 log = gw_get_repo_log(gw_trans, NULL, NULL, D_MAXSLCOMMDISP, 0);
583 tags = gw_get_repo_tags(gw_trans, D_MAXSLCOMMDISP, TAGBRIEF);
584 heads = gw_get_repo_heads(gw_trans);
586 if (log != NULL && strcmp(log, "") != 0) {
587 if ((asprintf(&log_html, summary_logbriefs,
588 log)) == -1)
589 return got_error_from_errno("asprintf");
590 khttp_puts(gw_trans->gw_req, log_html);
591 free(log_html);
592 free(log);
595 if (tags != NULL && strcmp(tags, "") != 0) {
596 if ((asprintf(&tags_html, summary_tags,
597 tags)) == -1)
598 return got_error_from_errno("asprintf");
599 khttp_puts(gw_trans->gw_req, tags_html);
600 free(tags_html);
601 free(tags);
604 if (heads != NULL && strcmp(heads, "") != 0) {
605 if ((asprintf(&heads_html, summary_heads,
606 heads)) == -1)
607 return got_error_from_errno("asprintf");
608 khttp_puts(gw_trans->gw_req, heads_html);
609 free(heads_html);
610 free(heads);
612 return error;
615 static const struct got_error *
616 gw_tag(struct gw_trans *gw_trans)
618 const struct got_error *error = NULL;
619 char *log, *log_html;
621 if (pledge("stdio rpath proc exec sendfd unveil",
622 NULL) == -1) {
623 error = got_error_from_errno("pledge");
624 return error;
627 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
628 if (error)
629 return error;
631 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGTAG);
633 if (log != NULL && strcmp(log, "") != 0) {
634 if ((asprintf(&log_html, log_tag, log)) == -1)
635 return got_error_from_errno("asprintf");
636 khttp_puts(gw_trans->gw_req, log_html);
637 free(log_html);
638 free(log);
640 return error;
643 static const struct got_error *
644 gw_tree(struct gw_trans *gw_trans)
646 const struct got_error *error = NULL;
647 char *log, *log_html;
649 if (pledge("stdio rpath proc exec sendfd unveil",
650 NULL) == -1) {
651 error = got_error_from_errno("pledge");
652 return error;
655 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
656 if (error)
657 return error;
659 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGTREE);
661 if (log != NULL && strcmp(log, "") != 0) {
662 if ((asprintf(&log_html, log_tree, log)) == -1)
663 return got_error_from_errno("asprintf");
664 khttp_puts(gw_trans->gw_req, log_html);
665 free(log_html);
666 free(log);
668 return error;
671 static const struct got_error *
672 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
674 const struct got_error *error = NULL;
675 DIR *dt;
676 char *dir_test;
677 int opened = 0;
679 if ((asprintf(&dir_test, "%s/%s/%s",
680 gw_trans->gw_conf->got_repos_path, gw_dir->name,
681 GOTWEB_GIT_DIR)) == -1)
682 return got_error_from_errno("asprintf");
684 dt = opendir(dir_test);
685 if (dt == NULL) {
686 free(dir_test);
687 } else {
688 gw_dir->path = strdup(dir_test);
689 opened = 1;
690 goto done;
693 if ((asprintf(&dir_test, "%s/%s/%s",
694 gw_trans->gw_conf->got_repos_path, gw_dir->name,
695 GOTWEB_GOT_DIR)) == -1)
696 return got_error_from_errno("asprintf");
698 dt = opendir(dir_test);
699 if (dt == NULL)
700 free(dir_test);
701 else {
702 opened = 1;
703 error = got_error(GOT_ERR_NOT_GIT_REPO);
704 goto errored;
707 if ((asprintf(&dir_test, "%s/%s",
708 gw_trans->gw_conf->got_repos_path, gw_dir->name)) == -1)
709 return got_error_from_errno("asprintf");
711 gw_dir->path = strdup(dir_test);
713 done:
714 gw_dir->description = gw_get_repo_description(gw_trans,
715 gw_dir->path);
716 gw_dir->owner = gw_get_repo_owner(gw_trans, gw_dir->path);
717 gw_dir->age = gw_get_repo_age(gw_trans, gw_dir->path, "refs/heads",
718 TM_DIFF);
719 gw_dir->url = gw_get_clone_url(gw_trans, gw_dir->path);
721 errored:
722 free(dir_test);
723 if (opened)
724 closedir(dt);
725 return error;
728 static const struct got_error *
729 gw_load_got_paths(struct gw_trans *gw_trans)
731 const struct got_error *error = NULL;
732 DIR *d;
733 struct dirent **sd_dent;
734 struct gw_dir *gw_dir;
735 struct stat st;
736 unsigned int d_cnt, d_i;
738 d = opendir(gw_trans->gw_conf->got_repos_path);
739 if (d == NULL) {
740 error = got_error_from_errno2("opendir",
741 gw_trans->gw_conf->got_repos_path);
742 return error;
745 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
746 alphasort);
747 if (d_cnt == -1) {
748 error = got_error_from_errno2("scandir",
749 gw_trans->gw_conf->got_repos_path);
750 return error;
753 for (d_i = 0; d_i < d_cnt; d_i++) {
754 if (gw_trans->gw_conf->got_max_repos > 0 &&
755 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
756 break; /* account for parent and self */
758 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
759 strcmp(sd_dent[d_i]->d_name, "..") == 0)
760 continue;
762 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
763 return got_error_from_errno("gw_dir malloc");
765 error = gw_load_got_path(gw_trans, gw_dir);
766 if (error && error->code == GOT_ERR_NOT_GIT_REPO)
767 continue;
768 else if (error)
769 return error;
771 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
772 !got_path_dir_is_empty(gw_dir->path)) {
773 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
774 entry);
775 gw_trans->repos_total++;
779 closedir(d);
780 return error;
783 static const struct got_error *
784 gw_parse_querystring(struct gw_trans *gw_trans)
786 const struct got_error *error = NULL;
787 struct kpair *p;
788 struct gw_query_action *action = NULL;
789 unsigned int i;
791 if (gw_trans->gw_req->fieldnmap[0]) {
792 error = got_error_from_errno("bad parse");
793 return error;
794 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
795 /* define gw_trans->repo_path */
796 if ((asprintf(&gw_trans->repo_name, "%s", p->parsed.s)) == -1)
797 return got_error_from_errno("asprintf");
799 if ((asprintf(&gw_trans->repo_path, "%s/%s",
800 gw_trans->gw_conf->got_repos_path, p->parsed.s)) == -1)
801 return got_error_from_errno("asprintf");
803 /* get action and set function */
804 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
805 for (i = 0; i < nitems(gw_query_funcs); i++) {
806 action = &gw_query_funcs[i];
807 if (action->func_name == NULL)
808 continue;
810 if (strcmp(action->func_name,
811 p->parsed.s) == 0) {
812 gw_trans->action = i;
813 if ((asprintf(&gw_trans->action_name,
814 "%s", action->func_name)) == -1)
815 return
816 got_error_from_errno(
817 "asprintf");
819 break;
822 action = NULL;
825 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
826 if ((asprintf(&gw_trans->commit, "%s",
827 p->parsed.s)) == -1)
828 return got_error_from_errno("asprintf");
830 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
831 if ((asprintf(&gw_trans->repo_file, "%s",
832 p->parsed.s)) == -1)
833 return got_error_from_errno("asprintf");
835 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER]))
836 if ((asprintf(&gw_trans->repo_folder, "%s",
837 p->parsed.s)) == -1)
838 return got_error_from_errno("asprintf");
840 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
841 if ((asprintf(&gw_trans->headref, "%s",
842 p->parsed.s)) == -1)
843 return got_error_from_errno("asprintf");
845 if (action == NULL) {
846 error = got_error_from_errno("invalid action");
847 return error;
849 if ((gw_trans->gw_dir =
850 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
851 return got_error_from_errno("gw_dir malloc");
853 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
854 if (error)
855 return error;
856 } else
857 gw_trans->action = GW_INDEX;
859 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
860 gw_trans->page = p->parsed.i;
862 if (gw_trans->action == GW_RAW)
863 gw_trans->mime = KMIME_TEXT_PLAIN;
865 return error;
868 static struct gw_dir *
869 gw_init_gw_dir(char *dir)
871 struct gw_dir *gw_dir;
873 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
874 return NULL;
876 if ((asprintf(&gw_dir->name, "%s", dir)) == -1)
877 return NULL;
879 return gw_dir;
882 static const struct got_error*
883 gw_match_logmsg(int *have_match, struct got_object_id *id,
884 struct got_commit_object *commit, regex_t *regex)
886 const struct got_error *err = NULL;
887 regmatch_t regmatch;
888 char *id_str = NULL, *logmsg = NULL;
890 *have_match = 0;
892 err = got_object_id_str(&id_str, id);
893 if (err)
894 return err;
896 err = got_object_commit_get_logmsg(&logmsg, commit);
897 if (err)
898 goto done;
900 if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
901 *have_match = 1;
902 done:
903 free(id_str);
904 free(logmsg);
905 return err;
908 static void
909 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
911 khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
912 khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
913 khttps[code]);
914 khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
915 kmimetypes[mime]);
916 khttp_head(gw_trans->gw_req, "X-Content-Type-Options", "nosniff");
917 khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
918 khttp_head(gw_trans->gw_req, "X-XSS-Protection", "1; mode=block");
919 khttp_body(gw_trans->gw_req);
922 static void
923 gw_display_index(struct gw_trans *gw_trans, const struct got_error *err)
925 gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
926 khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
928 if (err)
929 khttp_puts(gw_trans->gw_req, err->msg);
930 else
931 khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
932 gw_query_funcs[gw_trans->action].template);
934 khtml_close(gw_trans->gw_html_req);
937 static int
938 gw_template(size_t key, void *arg)
940 const struct got_error *error = NULL;
941 struct gw_trans *gw_trans = arg;
942 char *gw_got_link, *gw_site_link;
943 char *site_owner_name, *site_owner_name_h;
945 switch (key) {
946 case (TEMPL_HEAD):
947 khttp_puts(gw_trans->gw_req, head);
948 break;
949 case(TEMPL_HEADER):
950 gw_got_link = gw_get_got_link(gw_trans);
951 if (gw_got_link != NULL)
952 khttp_puts(gw_trans->gw_req, gw_got_link);
954 free(gw_got_link);
955 break;
956 case (TEMPL_SITEPATH):
957 gw_site_link = gw_get_site_link(gw_trans);
958 if (gw_site_link != NULL)
959 khttp_puts(gw_trans->gw_req, gw_site_link);
961 free(gw_site_link);
962 break;
963 case(TEMPL_TITLE):
964 if (gw_trans->gw_conf->got_site_name != NULL)
965 khtml_puts(gw_trans->gw_html_req,
966 gw_trans->gw_conf->got_site_name);
968 break;
969 case (TEMPL_SEARCH):
970 khttp_puts(gw_trans->gw_req, search);
971 break;
972 case(TEMPL_SITEOWNER):
973 if (gw_trans->gw_conf->got_site_owner != NULL &&
974 gw_trans->gw_conf->got_show_site_owner) {
975 site_owner_name =
976 gw_html_escape(gw_trans->gw_conf->got_site_owner);
977 if ((asprintf(&site_owner_name_h, site_owner,
978 site_owner_name))
979 == -1)
980 return 0;
982 khttp_puts(gw_trans->gw_req, site_owner_name_h);
983 free(site_owner_name);
984 free(site_owner_name_h);
986 break;
987 case(TEMPL_CONTENT):
988 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
989 if (error)
990 khttp_puts(gw_trans->gw_req, error->msg);
992 break;
993 default:
994 return 0;
995 break;
997 return 1;
1000 static char *
1001 gw_get_repo_description(struct gw_trans *gw_trans, char *dir)
1003 FILE *f;
1004 char *description = NULL, *d_file = NULL;
1005 unsigned int len;
1007 if (gw_trans->gw_conf->got_show_repo_description == false)
1008 goto err;
1010 if ((asprintf(&d_file, "%s/description", dir)) == -1)
1011 goto err;
1013 if ((f = fopen(d_file, "r")) == NULL)
1014 goto err;
1016 fseek(f, 0, SEEK_END);
1017 len = ftell(f) + 1;
1018 fseek(f, 0, SEEK_SET);
1019 if ((description = calloc(len, sizeof(char *))) == NULL)
1020 goto err;
1022 fread(description, 1, len, f);
1023 fclose(f);
1024 free(d_file);
1025 return description;
1026 err:
1027 if ((asprintf(&description, "%s", "")) == -1)
1028 return NULL;
1030 return description;
1033 static char *
1034 gw_get_time_str(time_t committer_time, int ref_tm)
1036 struct tm tm;
1037 time_t diff_time;
1038 char *years = "years ago", *months = "months ago";
1039 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
1040 char *minutes = "minutes ago", *seconds = "seconds ago";
1041 char *now = "right now";
1042 char *repo_age, *s;
1043 char datebuf[29];
1045 switch (ref_tm) {
1046 case TM_DIFF:
1047 diff_time = time(NULL) - committer_time;
1048 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1049 if ((asprintf(&repo_age, "%lld %s",
1050 (diff_time / 60 / 60 / 24 / 365), years)) == -1)
1051 return NULL;
1052 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1053 if ((asprintf(&repo_age, "%lld %s",
1054 (diff_time / 60 / 60 / 24 / (365 / 12)),
1055 months)) == -1)
1056 return NULL;
1057 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1058 if ((asprintf(&repo_age, "%lld %s",
1059 (diff_time / 60 / 60 / 24 / 7), weeks)) == -1)
1060 return NULL;
1061 } else if (diff_time > 60 * 60 * 24 * 2) {
1062 if ((asprintf(&repo_age, "%lld %s",
1063 (diff_time / 60 / 60 / 24), days)) == -1)
1064 return NULL;
1065 } else if (diff_time > 60 * 60 * 2) {
1066 if ((asprintf(&repo_age, "%lld %s",
1067 (diff_time / 60 / 60), hours)) == -1)
1068 return NULL;
1069 } else if (diff_time > 60 * 2) {
1070 if ((asprintf(&repo_age, "%lld %s", (diff_time / 60),
1071 minutes)) == -1)
1072 return NULL;
1073 } else if (diff_time > 2) {
1074 if ((asprintf(&repo_age, "%lld %s", diff_time,
1075 seconds)) == -1)
1076 return NULL;
1077 } else {
1078 if ((asprintf(&repo_age, "%s", now)) == -1)
1079 return NULL;
1081 break;
1082 case TM_LONG:
1083 if (gmtime_r(&committer_time, &tm) == NULL)
1084 return NULL;
1086 s = asctime_r(&tm, datebuf);
1087 if (s == NULL)
1088 return NULL;
1090 if ((asprintf(&repo_age, "%s UTC", datebuf)) == -1)
1091 return NULL;
1092 break;
1094 return repo_age;
1097 static char *
1098 gw_get_repo_age(struct gw_trans *gw_trans, char *dir, char *repo_ref,
1099 int ref_tm)
1101 const struct got_error *error = NULL;
1102 struct got_object_id *id = NULL;
1103 struct got_repository *repo = NULL;
1104 struct got_commit_object *commit = NULL;
1105 struct got_reflist_head refs;
1106 struct got_reflist_entry *re;
1107 struct got_reference *head_ref;
1108 int is_head = 0;
1109 time_t committer_time = 0, cmp_time = 0;
1110 const char *refname;
1111 char *repo_age = NULL;
1113 if (repo_ref == NULL)
1114 return NULL;
1116 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
1117 is_head = 1;
1119 SIMPLEQ_INIT(&refs);
1120 if (gw_trans->gw_conf->got_show_repo_age == false) {
1121 if ((asprintf(&repo_age, "")) == -1)
1122 return NULL;
1123 return repo_age;
1126 error = got_repo_open(&repo, dir, NULL);
1127 if (error)
1128 goto err;
1130 if (is_head)
1131 error = got_ref_list(&refs, repo, "refs/heads",
1132 got_ref_cmp_by_name, NULL);
1133 else
1134 error = got_ref_list(&refs, repo, repo_ref,
1135 got_ref_cmp_by_name, NULL);
1136 if (error)
1137 goto err;
1139 SIMPLEQ_FOREACH(re, &refs, entry) {
1140 if (is_head)
1141 refname = strdup(repo_ref);
1142 else
1143 refname = got_ref_get_name(re->ref);
1144 error = got_ref_open(&head_ref, repo, refname, 0);
1145 if (error)
1146 goto err;
1148 error = got_ref_resolve(&id, repo, head_ref);
1149 got_ref_close(head_ref);
1150 if (error)
1151 goto err;
1153 error = got_object_open_as_commit(&commit, repo, id);
1154 if (error)
1155 goto err;
1157 committer_time =
1158 got_object_commit_get_committer_time(commit);
1160 if (cmp_time < committer_time)
1161 cmp_time = committer_time;
1164 if (cmp_time != 0) {
1165 committer_time = cmp_time;
1166 repo_age = gw_get_time_str(committer_time, ref_tm);
1167 } else
1168 if ((asprintf(&repo_age, "")) == -1)
1169 return NULL;
1170 got_ref_list_free(&refs);
1171 free(id);
1172 return repo_age;
1173 err:
1174 if ((asprintf(&repo_age, "%s", error->msg)) == -1)
1175 return NULL;
1177 return repo_age;
1180 static char *
1181 gw_get_repo_diff(struct gw_trans *gw_trans, char *id_str1, char *id_str2)
1183 const struct got_error *error;
1184 FILE *f = NULL;
1185 struct got_object_id *id1 = NULL, *id2 = NULL;
1186 struct got_repository *repo = NULL;
1187 struct buf *diffbuf = NULL;
1188 char *label1 = NULL, *label2 = NULL, *diff_html = NULL, *buf = NULL,
1189 *buf_color = NULL;
1190 int type1, type2;
1191 size_t newsize;
1193 f = got_opentemp();
1194 if (f == NULL)
1195 return NULL;
1197 error = buf_alloc(&diffbuf, 0);
1198 if (error)
1199 return NULL;
1201 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1202 if (error)
1203 goto done;
1205 error = got_repo_match_object_id(&id1, &label1, id_str1,
1206 GOT_OBJ_TYPE_ANY, 1, repo);
1207 if (error)
1208 goto done;
1210 if (id_str2) {
1211 error = got_repo_match_object_id(&id2, &label2, id_str2,
1212 GOT_OBJ_TYPE_ANY, 1, repo);
1213 if (error)
1214 goto done;
1216 error = got_object_get_type(&type2, repo, id2);
1217 if (error)
1218 goto done;
1221 error = got_object_get_type(&type1, repo, id1);
1222 if (error)
1223 goto done;
1225 if (id_str2 && type1 != type2) {
1226 error = got_error(GOT_ERR_OBJ_TYPE);
1227 goto done;
1230 switch (type1) {
1231 case GOT_OBJ_TYPE_BLOB:
1232 error = got_diff_objects_as_blobs(id2, id1, NULL, NULL, 3, 0,
1233 repo, f);
1234 break;
1235 case GOT_OBJ_TYPE_TREE:
1236 error = got_diff_objects_as_trees(id2, id1, "", "", 3, 0, repo,
1237 f);
1238 break;
1239 case GOT_OBJ_TYPE_COMMIT:
1240 error = got_diff_objects_as_commits(id2, id1, 3, 0, repo, f);
1241 break;
1242 default:
1243 error = got_error(GOT_ERR_OBJ_TYPE);
1246 if ((buf = calloc(128, sizeof(char *))) == NULL)
1247 goto done;
1249 fseek(f, 0, SEEK_SET);
1251 while ((fgets(buf, 128, f)) != NULL) {
1252 buf_color = gw_colordiff_line(buf);
1253 error = buf_puts(&newsize, diffbuf, buf_color);
1254 if (error)
1255 return NULL;
1257 error = buf_puts(&newsize, diffbuf, div_end);
1258 if (error)
1259 return NULL;
1262 if (buf_len(diffbuf) > 0) {
1263 error = buf_putc(diffbuf, '\0');
1264 diff_html = strdup(buf_get(diffbuf));
1266 done:
1267 fclose(f);
1268 free(buf_color);
1269 free(buf);
1270 free(diffbuf);
1271 free(label1);
1272 free(label2);
1273 free(id1);
1274 free(id2);
1275 if (repo)
1276 got_repo_close(repo);
1278 if (error)
1279 return NULL;
1280 else
1281 return diff_html;
1284 static char *
1285 gw_get_repo_owner(struct gw_trans *gw_trans, char *dir)
1287 FILE *f;
1288 char *owner = NULL, *d_file = NULL;
1289 char *gotweb = "[gotweb]", *gitweb = "[gitweb]", *gw_owner = "owner";
1290 char *comp, *pos, *buf;
1291 unsigned int i;
1293 if (gw_trans->gw_conf->got_show_repo_owner == false)
1294 goto err;
1296 if ((asprintf(&d_file, "%s/config", dir)) == -1)
1297 goto err;
1299 if ((f = fopen(d_file, "r")) == NULL)
1300 goto err;
1302 if ((buf = calloc(128, sizeof(char *))) == NULL)
1303 goto err;
1305 while ((fgets(buf, 128, f)) != NULL) {
1306 if ((pos = strstr(buf, gotweb)) != NULL)
1307 break;
1309 if ((pos = strstr(buf, gitweb)) != NULL)
1310 break;
1313 if (pos == NULL)
1314 goto err;
1316 do {
1317 fgets(buf, 128, f);
1318 } while ((comp = strcasestr(buf, gw_owner)) == NULL);
1320 if (comp == NULL)
1321 goto err;
1323 if (strncmp(gw_owner, comp, strlen(gw_owner)) != 0)
1324 goto err;
1326 for (i = 0; i < 2; i++) {
1327 owner = strsep(&buf, "\"");
1330 if (owner == NULL)
1331 goto err;
1333 fclose(f);
1334 free(d_file);
1335 return owner;
1336 err:
1337 if ((asprintf(&owner, "%s", "")) == -1)
1338 return NULL;
1340 return owner;
1343 static char *
1344 gw_get_clone_url(struct gw_trans *gw_trans, char *dir)
1346 FILE *f;
1347 char *url = NULL, *d_file = NULL;
1348 unsigned int len;
1350 if ((asprintf(&d_file, "%s/cloneurl", dir)) == -1)
1351 return NULL;
1353 if ((f = fopen(d_file, "r")) == NULL)
1354 return NULL;
1356 fseek(f, 0, SEEK_END);
1357 len = ftell(f) + 1;
1358 fseek(f, 0, SEEK_SET);
1360 if ((url = calloc(len, sizeof(char *))) == NULL)
1361 return NULL;
1363 fread(url, 1, len, f);
1364 fclose(f);
1365 free(d_file);
1366 return url;
1369 static char *
1370 gw_get_repo_log(struct gw_trans *gw_trans, const char *search_pattern,
1371 char *start_commit, int limit, int log_type)
1373 const struct got_error *error;
1374 struct got_repository *repo = NULL;
1375 struct got_reflist_head refs;
1376 struct got_reflist_entry *re;
1377 struct got_commit_object *commit = NULL;
1378 struct got_object_id *id1 = NULL, *id2 = NULL;
1379 struct got_object_qid *parent_id;
1380 struct got_commit_graph *graph = NULL;
1381 char *logs = NULL, *id_str1 = NULL, *id_str2 = NULL, *path = NULL,
1382 *in_repo_path = NULL, *refs_str = NULL, *refs_str_disp = NULL,
1383 *treeid = NULL, *commit_row = NULL, *commit_commit = NULL,
1384 *commit_commit_disp = NULL, *commit_age_diff = NULL,
1385 *commit_age_diff_disp = NULL, *commit_age_long = NULL,
1386 *commit_age_long_disp = NULL, *commit_author = NULL,
1387 *commit_author_disp = NULL, *commit_committer = NULL,
1388 *commit_committer_disp = NULL, *commit_log = NULL,
1389 *commit_log_disp = NULL, *commit_parent = NULL,
1390 *commit_diff_disp = NULL, *logbriefs_navs_html = NULL,
1391 *log_tree_html = NULL, *log_commit_html = NULL,
1392 *log_diff_html = NULL, *commit_tree = NULL,
1393 *commit_tree_disp = NULL, *log_tag_html = NULL,
1394 *log_blame_html = NULL;
1395 char *commit_log0, *newline;
1396 regex_t regex;
1397 int have_match, has_parent = 1;
1398 size_t newsize;
1399 struct buf *diffbuf = NULL;
1400 time_t committer_time;
1402 error = buf_alloc(&diffbuf, 0);
1403 if (error)
1404 return NULL;
1406 if (search_pattern &&
1407 regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB |
1408 REG_NEWLINE))
1409 return NULL;
1411 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1412 if (error)
1413 return NULL;
1415 SIMPLEQ_INIT(&refs);
1417 if (start_commit == NULL) {
1418 struct got_reference *head_ref;
1419 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
1420 if (error)
1421 goto done;
1423 error = got_ref_resolve(&id1, repo, head_ref);
1424 got_ref_close(head_ref);
1425 if (error)
1426 goto done;
1428 error = got_object_open_as_commit(&commit, repo, id1);
1429 } else {
1430 struct got_reference *ref;
1431 error = got_ref_open(&ref, repo, start_commit, 0);
1432 if (error == NULL) {
1433 int obj_type;
1434 error = got_ref_resolve(&id1, repo, ref);
1435 got_ref_close(ref);
1436 if (error)
1437 goto done;
1438 error = got_object_get_type(&obj_type, repo, id1);
1439 if (error)
1440 goto done;
1441 if (obj_type == GOT_OBJ_TYPE_TAG) {
1442 struct got_tag_object *tag;
1443 error = got_object_open_as_tag(&tag, repo, id1);
1444 if (error)
1445 goto done;
1446 if (got_object_tag_get_object_type(tag) !=
1447 GOT_OBJ_TYPE_COMMIT) {
1448 got_object_tag_close(tag);
1449 error = got_error(GOT_ERR_OBJ_TYPE);
1450 goto done;
1452 free(id1);
1453 id1 = got_object_id_dup(
1454 got_object_tag_get_object_id(tag));
1455 if (id1 == NULL)
1456 error = got_error_from_errno(
1457 "got_object_id_dup");
1458 got_object_tag_close(tag);
1459 if (error)
1460 goto done;
1461 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1462 error = got_error(GOT_ERR_OBJ_TYPE);
1463 goto done;
1465 error = got_object_open_as_commit(&commit, repo, id1);
1466 if (error)
1467 goto done;
1469 if (commit == NULL) {
1470 error = got_repo_match_object_id_prefix(&id1,
1471 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1472 if (error)
1473 goto done;
1475 error = got_repo_match_object_id_prefix(&id1,
1476 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1479 if (error)
1480 goto done;
1482 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
1483 if (error)
1484 goto done;
1486 if (in_repo_path) {
1487 free(path);
1488 path = in_repo_path;
1491 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
1492 if (error)
1493 goto done;
1495 error = got_commit_graph_open(&graph, path, 0);
1496 if (error)
1497 goto done;
1499 error = got_commit_graph_iter_start(graph, id1, repo, NULL, NULL);
1500 if (error)
1501 goto done;
1503 for (;;) {
1504 error = got_commit_graph_iter_next(&id1, graph, repo, NULL,
1505 NULL);
1506 if (error) {
1507 if (error->code == GOT_ERR_ITER_COMPLETED)
1508 error = NULL;
1509 break;
1511 if (id1 == NULL)
1512 break;
1514 error = got_object_open_as_commit(&commit, repo, id1);
1515 if (error)
1516 break;
1518 if (search_pattern) {
1519 error = gw_match_logmsg(&have_match, id1, commit,
1520 &regex);
1521 if (error) {
1522 got_object_commit_close(commit);
1523 break;
1525 if (have_match == 0) {
1526 got_object_commit_close(commit);
1527 continue;
1531 SIMPLEQ_FOREACH(re, &refs, entry) {
1532 char *s;
1533 const char *name;
1534 struct got_tag_object *tag = NULL;
1535 int cmp;
1537 name = got_ref_get_name(re->ref);
1538 if (strcmp(name, GOT_REF_HEAD) == 0)
1539 continue;
1540 if (strncmp(name, "refs/", 5) == 0)
1541 name += 5;
1542 if (strncmp(name, "got/", 4) == 0)
1543 continue;
1544 if (strncmp(name, "heads/", 6) == 0)
1545 name += 6;
1546 if (strncmp(name, "remotes/", 8) == 0)
1547 name += 8;
1548 if (strncmp(name, "tags/", 5) == 0) {
1549 error = got_object_open_as_tag(&tag, repo,
1550 re->id);
1551 if (error) {
1552 if (error->code != GOT_ERR_OBJ_TYPE)
1553 continue;
1555 * Ref points at something other
1556 * than a tag.
1558 error = NULL;
1559 tag = NULL;
1562 cmp = got_object_id_cmp(tag ?
1563 got_object_tag_get_object_id(tag) : re->id, id1);
1564 if (tag)
1565 got_object_tag_close(tag);
1566 if (cmp != 0)
1567 continue;
1568 s = refs_str;
1569 if ((asprintf(&refs_str, "%s%s%s", s ? s : "",
1570 s ? ", " : "", name)) == -1) {
1571 error = got_error_from_errno("asprintf");
1572 free(s);
1573 goto done;
1575 free(s);
1578 if (refs_str == NULL)
1579 refs_str_disp = strdup("");
1580 else {
1581 if ((asprintf(&refs_str_disp, "(%s)",
1582 refs_str)) == -1) {
1583 error = got_error_from_errno("asprintf");
1584 free(refs_str);
1585 goto done;
1589 error = got_object_id_str(&id_str1, id1);
1590 if (error)
1591 goto done;
1593 error = got_object_id_str(&treeid,
1594 got_object_commit_get_tree_id(commit));
1595 if (error)
1596 goto done;
1598 if (gw_trans->action == GW_COMMIT ||
1599 gw_trans->action == GW_COMMITDIFF) {
1600 parent_id =
1601 SIMPLEQ_FIRST(
1602 got_object_commit_get_parent_ids(commit));
1603 if (parent_id != NULL) {
1604 id2 = got_object_id_dup(parent_id->id);
1605 free (parent_id);
1606 error = got_object_id_str(&id_str2, id2);
1607 if (error)
1608 goto done;
1609 free(id2);
1610 } else {
1611 has_parent = 0;
1612 id_str2 = strdup("/dev/null");
1616 committer_time =
1617 got_object_commit_get_committer_time(commit);
1619 if ((asprintf(&commit_parent, "%s", id_str2)) == -1) {
1620 error = got_error_from_errno("asprintf");
1621 goto done;
1624 if ((asprintf(&commit_tree, "%s", treeid)) == -1) {
1625 error = got_error_from_errno("asprintf");
1626 goto done;
1629 if ((asprintf(&commit_tree_disp, commit_tree_html,
1630 treeid)) == -1) {
1631 error = got_error_from_errno("asprintf");
1632 goto done;
1635 if ((asprintf(&commit_diff_disp, commit_diff_html, id_str2,
1636 id_str1)) == -1) {
1637 error = got_error_from_errno("asprintf");
1638 goto done;
1641 if ((asprintf(&commit_commit, "%s", id_str1)) == -1) {
1642 error = got_error_from_errno("asprintf");
1643 goto done;
1646 if ((asprintf(&commit_commit_disp, commit_commit_html,
1647 commit_commit, refs_str_disp)) == -1) {
1648 error = got_error_from_errno("asprintf");
1649 goto done;
1652 if ((asprintf(&commit_age_long, "%s",
1653 gw_get_time_str(committer_time, TM_LONG))) == -1) {
1654 error = got_error_from_errno("asprintf");
1655 goto done;
1658 if ((asprintf(&commit_age_long_disp, commit_age_html,
1659 commit_age_long)) == -1) {
1660 error = got_error_from_errno("asprintf");
1661 goto done;
1664 if ((asprintf(&commit_age_diff, "%s",
1665 gw_get_time_str(committer_time, TM_DIFF))) == -1) {
1666 error = got_error_from_errno("asprintf");
1667 goto done;
1670 if ((asprintf(&commit_age_diff_disp, commit_age_html,
1671 commit_age_diff)) == -1) {
1672 error = got_error_from_errno("asprintf");
1673 goto done;
1676 if ((asprintf(&commit_author, "%s",
1677 got_object_commit_get_author(commit))) == -1) {
1678 error = got_error_from_errno("asprintf");
1679 goto done;
1682 if ((asprintf(&commit_author_disp, commit_author_html,
1683 gw_html_escape(commit_author))) == -1) {
1684 error = got_error_from_errno("asprintf");
1685 goto done;
1688 if ((asprintf(&commit_committer, "%s",
1689 got_object_commit_get_committer(commit))) == -1) {
1690 error = got_error_from_errno("asprintf");
1691 goto done;
1694 if ((asprintf(&commit_committer_disp, commit_committer_html,
1695 gw_html_escape(commit_committer))) == -1) {
1696 error = got_error_from_errno("asprintf");
1697 goto done;
1700 if (strcmp(commit_author, commit_committer) == 0) {
1701 free(commit_committer_disp);
1702 commit_committer_disp = strdup("");
1705 error = got_object_commit_get_logmsg(&commit_log0, commit);
1706 if (error)
1707 goto done;
1709 commit_log = commit_log0;
1710 while (*commit_log == '\n')
1711 commit_log++;
1713 switch(log_type) {
1714 case (LOGBRIEF):
1715 newline = strchr(commit_log, '\n');
1716 if (newline)
1717 *newline = '\0';
1719 if ((asprintf(&logbriefs_navs_html, logbriefs_navs,
1720 gw_trans->repo_name, id_str1, gw_trans->repo_name,
1721 id_str1, gw_trans->repo_name, id_str1,
1722 gw_trans->repo_name, id_str1)) == -1) {
1723 error = got_error_from_errno("asprintf");
1724 goto done;
1727 if ((asprintf(&commit_row, logbriefs_row,
1728 commit_age_diff, commit_author, commit_log,
1729 logbriefs_navs_html)) == -1) {
1730 error = got_error_from_errno("asprintf");
1731 goto done;
1734 free(logbriefs_navs_html);
1735 logbriefs_navs_html = NULL;
1736 break;
1737 case (LOGFULL):
1738 if ((asprintf(&logbriefs_navs_html, logbriefs_navs,
1739 gw_trans->repo_name, id_str1, gw_trans->repo_name,
1740 id_str1, gw_trans->repo_name, id_str1,
1741 gw_trans->repo_name, id_str1)) == -1) {
1742 error = got_error_from_errno("asprintf");
1743 goto done;
1746 if ((asprintf(&commit_row, logs_row, commit_commit_disp,
1747 commit_author_disp, commit_committer_disp,
1748 commit_age_long_disp, gw_html_escape(commit_log),
1749 logbriefs_navs_html)) == -1) {
1750 error = got_error_from_errno("asprintf");
1751 goto done;
1754 free(logbriefs_navs_html);
1755 logbriefs_navs_html = NULL;
1756 break;
1757 case (LOGTAG):
1758 log_tag_html = strdup("tag log here");
1760 if ((asprintf(&commit_row, log_tag_row,
1761 gw_html_escape(commit_log), log_tag_html)) == -1) {
1762 error = got_error_from_errno("asprintf");
1763 goto done;
1766 free(log_tag_html);
1767 break;
1768 case (LOGBLAME):
1769 log_blame_html = gw_get_file_blame(gw_trans,
1770 start_commit);
1772 if ((asprintf(&commit_row, log_blame_row,
1773 gw_html_escape(commit_log), log_blame_html)) == -1) {
1774 error = got_error_from_errno("asprintf");
1775 goto done;
1778 free(log_blame_html);
1779 break;
1780 case (LOGTREE):
1781 log_tree_html = gw_get_repo_tree(gw_trans,
1782 start_commit);
1784 if ((asprintf(&commit_row, log_tree_row,
1785 gw_html_escape(commit_log), log_tree_html)) == -1) {
1786 error = got_error_from_errno("asprintf");
1787 goto done;
1790 free(log_tree_html);
1791 break;
1792 case (LOGCOMMIT):
1793 if ((asprintf(&commit_log_disp, commit_log_html,
1794 gw_html_escape(commit_log))) == -1) {
1795 error = got_error_from_errno("asprintf");
1796 goto done;
1799 log_commit_html = strdup("commit here");
1801 if ((asprintf(&commit_row, log_commit_row,
1802 commit_diff_disp, commit_commit_disp,
1803 commit_tree_disp, commit_author_disp,
1804 commit_committer_disp, commit_age_long_disp,
1805 commit_log_disp, log_commit_html)) == -1) {
1806 error = got_error_from_errno("asprintf");
1807 goto done;
1809 free(commit_log_disp);
1810 free(log_commit_html);
1812 break;
1813 case (LOGDIFF):
1814 if ((asprintf(&commit_log_disp, commit_log_html,
1815 gw_html_escape(commit_log))) == -1) {
1816 error = got_error_from_errno("asprintf");
1817 goto done;
1820 if (has_parent)
1821 log_diff_html = gw_get_repo_diff(gw_trans,
1822 commit_commit, commit_parent);
1823 else
1824 log_diff_html = gw_get_repo_diff(gw_trans,
1825 commit_commit, NULL);
1827 if ((asprintf(&commit_row, log_diff_row,
1828 commit_diff_disp, commit_commit_disp,
1829 commit_tree_disp, commit_author_disp,
1830 commit_committer_disp, commit_age_long_disp,
1831 commit_log_disp, log_diff_html)) == -1) {
1832 error = got_error_from_errno("asprintf");
1833 goto done;
1835 free(commit_log_disp);
1836 free(log_diff_html);
1838 break;
1839 default:
1840 return NULL;
1843 error = buf_puts(&newsize, diffbuf, commit_row);
1845 free(commit_parent);
1846 free(commit_diff_disp);
1847 free(commit_tree_disp);
1848 free(commit_age_diff);
1849 free(commit_age_diff_disp);
1850 free(commit_age_long);
1851 free(commit_age_long_disp);
1852 free(commit_author);
1853 free(commit_author_disp);
1854 free(commit_committer);
1855 free(commit_committer_disp);
1856 free(commit_log0);
1857 free(commit_row);
1858 free(refs_str_disp);
1859 free(refs_str);
1860 refs_str = NULL;
1861 free(id_str1);
1862 id_str1 = NULL;
1863 free(id_str2);
1864 id_str2 = NULL;
1866 if (error || (limit && --limit == 0))
1867 break;
1870 if (error)
1871 goto done;
1873 if (buf_len(diffbuf) > 0) {
1874 error = buf_putc(diffbuf, '\0');
1875 logs = strdup(buf_get(diffbuf));
1877 done:
1878 buf_free(diffbuf);
1879 free(in_repo_path);
1880 if (commit != NULL)
1881 got_object_commit_close(commit);
1882 if (search_pattern)
1883 regfree(&regex);
1884 if (graph)
1885 got_commit_graph_close(graph);
1886 if (repo) {
1887 error = got_repo_close(repo);
1888 if (error)
1889 return NULL;
1891 if (error) {
1892 khttp_puts(gw_trans->gw_req, "Error: ");
1893 khttp_puts(gw_trans->gw_req, error->msg);
1894 return NULL;
1895 } else
1896 return logs;
1899 static char *
1900 gw_get_repo_tags(struct gw_trans *gw_trans, int limit, int tag_type)
1902 const struct got_error *error = NULL;
1903 struct got_repository *repo = NULL;
1904 struct got_reflist_head refs;
1905 struct got_reflist_entry *re;
1906 char *tags = NULL, *tag_row = NULL, *tags_navs_disp = NULL,
1907 *age = NULL;
1908 char *newline;
1909 struct buf *diffbuf = NULL;
1910 size_t newsize;
1912 error = buf_alloc(&diffbuf, 0);
1913 if (error)
1914 return NULL;
1915 SIMPLEQ_INIT(&refs);
1917 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1918 if (error)
1919 goto done;
1921 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
1922 if (error)
1923 goto done;
1925 SIMPLEQ_FOREACH(re, &refs, entry) {
1926 const char *refname;
1927 char *refstr, *tag_log0, *tag_log, *id_str;
1928 time_t tagger_time;
1929 struct got_object_id *id;
1930 struct got_tag_object *tag;
1932 refname = got_ref_get_name(re->ref);
1933 if (strncmp(refname, "refs/tags/", 10) != 0)
1934 continue;
1935 refname += 10;
1936 refstr = got_ref_to_str(re->ref);
1937 if (refstr == NULL) {
1938 error = got_error_from_errno("got_ref_to_str");
1939 goto done;
1942 error = got_ref_resolve(&id, repo, re->ref);
1943 if (error)
1944 goto done;
1945 error = got_object_open_as_tag(&tag, repo, id);
1946 free(id);
1947 if (error)
1948 goto done;
1950 tagger_time = got_object_tag_get_tagger_time(tag);
1952 error = got_object_id_str(&id_str,
1953 got_object_tag_get_object_id(tag));
1954 if (error)
1955 goto done;
1957 tag_log0 = strdup(got_object_tag_get_message(tag));
1959 if (tag_log0 == NULL) {
1960 error = got_error_from_errno("strdup");
1961 goto done;
1964 tag_log = tag_log0;
1965 while (*tag_log == '\n')
1966 tag_log++;
1968 switch (tag_type) {
1969 case TAGBRIEF:
1970 newline = strchr(tag_log, '\n');
1971 if (newline)
1972 *newline = '\0';
1974 if ((asprintf(&age, "%s", gw_get_time_str(tagger_time,
1975 TM_DIFF))) == -1) {
1976 error = got_error_from_errno("asprintf");
1977 goto done;
1980 if ((asprintf(&tags_navs_disp, tags_navs,
1981 gw_trans->repo_name, id_str, gw_trans->repo_name,
1982 id_str, gw_trans->repo_name, id_str,
1983 gw_trans->repo_name, id_str)) == -1) {
1984 error = got_error_from_errno("asprintf");
1985 goto done;
1988 if ((asprintf(&tag_row, tags_row, age, refname, tag_log,
1989 tags_navs_disp)) == -1) {
1990 error = got_error_from_errno("asprintf");
1991 goto done;
1994 free(tags_navs_disp);
1995 break;
1996 case TAGFULL:
1997 break;
1998 default:
1999 break;
2002 got_object_tag_close(tag);
2004 error = buf_puts(&newsize, diffbuf, tag_row);
2006 free(id_str);
2007 free(refstr);
2008 free(age);
2009 free(tag_log0);
2010 free(tag_row);
2012 if (error || (limit && --limit == 0))
2013 break;
2016 if (buf_len(diffbuf) > 0) {
2017 error = buf_putc(diffbuf, '\0');
2018 tags = strdup(buf_get(diffbuf));
2020 done:
2021 buf_free(diffbuf);
2022 got_ref_list_free(&refs);
2023 if (repo)
2024 got_repo_close(repo);
2025 if (error)
2026 return NULL;
2027 else
2028 return tags;
2031 struct blame_line {
2032 int annotated;
2033 char *id_str;
2034 char *committer;
2035 char datebuf[11]; /* YYYY-MM-DD + NUL */
2038 struct gw_blame_cb_args {
2039 struct blame_line *lines;
2040 int nlines;
2041 int nlines_prec;
2042 int lineno_cur;
2043 off_t *line_offsets;
2044 FILE *f;
2045 struct got_repository *repo;
2046 struct gw_trans *gw_trans;
2047 struct buf *blamebuf;
2050 static const struct got_error *
2051 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2053 const struct got_error *err = NULL;
2054 struct gw_blame_cb_args *a = arg;
2055 struct blame_line *bline;
2056 char *line = NULL;
2057 size_t linesize = 0, newsize;
2058 struct got_commit_object *commit = NULL;
2059 off_t offset;
2060 struct tm tm;
2061 time_t committer_time;
2063 if (nlines != a->nlines ||
2064 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2065 return got_error(GOT_ERR_RANGE);
2067 if (lineno == -1)
2068 return NULL; /* no change in this commit */
2070 /* Annotate this line. */
2071 bline = &a->lines[lineno - 1];
2072 if (bline->annotated)
2073 return NULL;
2074 err = got_object_id_str(&bline->id_str, id);
2075 if (err)
2076 return err;
2078 err = got_object_open_as_commit(&commit, a->repo, id);
2079 if (err)
2080 goto done;
2082 bline->committer = strdup(got_object_commit_get_committer(commit));
2083 if (bline->committer == NULL) {
2084 err = got_error_from_errno("strdup");
2085 goto done;
2088 committer_time = got_object_commit_get_committer_time(commit);
2089 if (localtime_r(&committer_time, &tm) == NULL)
2090 return got_error_from_errno("localtime_r");
2091 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2092 &tm) >= sizeof(bline->datebuf)) {
2093 err = got_error(GOT_ERR_NO_SPACE);
2094 goto done;
2096 bline->annotated = 1;
2098 /* Print lines annotated so far. */
2099 bline = &a->lines[a->lineno_cur - 1];
2100 if (!bline->annotated)
2101 goto done;
2103 offset = a->line_offsets[a->lineno_cur - 1];
2104 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2105 err = got_error_from_errno("fseeko");
2106 goto done;
2109 while (bline->annotated) {
2110 char *smallerthan, *at, *nl, *committer, *blame_row = NULL,
2111 *line_escape = NULL;
2112 size_t len;
2114 if (getline(&line, &linesize, a->f) == -1) {
2115 if (ferror(a->f))
2116 err = got_error_from_errno("getline");
2117 break;
2120 committer = bline->committer;
2121 smallerthan = strchr(committer, '<');
2122 if (smallerthan && smallerthan[1] != '\0')
2123 committer = smallerthan + 1;
2124 at = strchr(committer, '@');
2125 if (at)
2126 *at = '\0';
2127 len = strlen(committer);
2128 if (len >= 9)
2129 committer[8] = '\0';
2131 nl = strchr(line, '\n');
2132 if (nl)
2133 *nl = '\0';
2135 if (strcmp(line, "") != 0)
2136 line_escape = strdup(gw_html_escape(line));
2137 else
2138 line_escape = strdup("");
2140 asprintf(&blame_row, log_blame_line, a->nlines_prec,
2141 a->lineno_cur, bline->id_str, bline->datebuf, committer,
2142 line_escape);
2143 a->lineno_cur++;
2144 err = buf_puts(&newsize, a->blamebuf, blame_row);
2145 if (err)
2146 return err;
2148 bline = &a->lines[a->lineno_cur - 1];
2149 free(line_escape);
2150 free(blame_row);
2152 done:
2153 if (commit)
2154 got_object_commit_close(commit);
2155 free(line);
2156 return err;
2159 static char*
2160 gw_get_file_blame(struct gw_trans *gw_trans, char *commit_str)
2162 const struct got_error *error = NULL;
2163 struct got_repository *repo = NULL;
2164 struct got_object_id *obj_id = NULL;
2165 struct got_object_id *commit_id = NULL;
2166 struct got_blob_object *blob = NULL;
2167 char *blame_html = NULL, *path = NULL, *in_repo_path = NULL,
2168 *folder = NULL;
2169 struct gw_blame_cb_args bca;
2170 int i, obj_type;
2171 size_t filesize;
2173 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2174 if (error)
2175 goto done;
2177 if (gw_trans->repo_folder != NULL) {
2178 if ((asprintf(&folder, "%s/", gw_trans->repo_folder)) == -1) {
2179 error = got_error_from_errno("asprintf");
2180 goto done;
2182 } else
2183 folder = strdup("");
2185 if ((asprintf(&path, "%s%s", folder, gw_trans->repo_file)) == -1) {
2186 error = got_error_from_errno("asprintf");
2187 goto done;
2189 free(folder);
2191 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2192 if (error)
2193 goto done;
2195 error = got_repo_match_object_id(&commit_id, NULL, commit_str,
2196 GOT_OBJ_TYPE_COMMIT, 1, repo);
2197 if (error)
2198 goto done;
2200 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2201 if (error)
2202 goto done;
2204 if (obj_id == NULL) {
2205 error = got_error(GOT_ERR_NO_OBJ);
2206 goto done;
2209 error = got_object_get_type(&obj_type, repo, obj_id);
2210 if (error)
2211 goto done;
2213 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2214 error = got_error(GOT_ERR_OBJ_TYPE);
2215 goto done;
2218 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2219 if (error)
2220 goto done;
2222 error = buf_alloc(&bca.blamebuf, 0);
2223 if (error)
2224 goto done;
2226 bca.f = got_opentemp();
2227 if (bca.f == NULL) {
2228 error = got_error_from_errno("got_opentemp");
2229 goto done;
2231 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2232 &bca.line_offsets, bca.f, blob);
2233 if (error || bca.nlines == 0)
2234 goto done;
2236 /* Don't include \n at EOF in the blame line count. */
2237 if (bca.line_offsets[bca.nlines - 1] == filesize)
2238 bca.nlines--;
2240 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2241 if (bca.lines == NULL) {
2242 error = got_error_from_errno("calloc");
2243 goto done;
2245 bca.lineno_cur = 1;
2246 bca.nlines_prec = 0;
2247 i = bca.nlines;
2248 while (i > 0) {
2249 i /= 10;
2250 bca.nlines_prec++;
2252 bca.repo = repo;
2253 bca.gw_trans = gw_trans;
2255 error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
2256 NULL, NULL);
2257 if (buf_len(bca.blamebuf) > 0) {
2258 error = buf_putc(bca.blamebuf, '\0');
2259 blame_html = strdup(buf_get(bca.blamebuf));
2261 done:
2262 free(bca.blamebuf);
2263 free(in_repo_path);
2264 free(commit_id);
2265 free(obj_id);
2266 free(path);
2268 if (blob)
2269 error = got_object_blob_close(blob);
2270 if (repo)
2271 error = got_repo_close(repo);
2272 if (error)
2273 return NULL;
2274 if (bca.lines) {
2275 for (i = 0; i < bca.nlines; i++) {
2276 struct blame_line *bline = &bca.lines[i];
2277 free(bline->id_str);
2278 free(bline->committer);
2280 free(bca.lines);
2282 free(bca.line_offsets);
2283 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2284 error = got_error_from_errno("fclose");
2285 if (error)
2286 return NULL;
2287 else
2288 return blame_html;
2291 static char*
2292 gw_get_repo_tree(struct gw_trans *gw_trans, char *commit_str)
2294 const struct got_error *error = NULL;
2295 struct got_repository *repo = NULL;
2296 struct got_object_id *tree_id = NULL, *commit_id = NULL;
2297 struct got_tree_object *tree = NULL;
2298 struct buf *diffbuf = NULL;
2299 size_t newsize;
2300 char *tree_html = NULL, *path = NULL, *in_repo_path = NULL,
2301 *tree_row = NULL, *id_str;
2302 int nentries, i;
2304 error = buf_alloc(&diffbuf, 0);
2305 if (error)
2306 return NULL;
2308 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2309 if (error)
2310 goto done;
2312 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
2313 if (error)
2314 goto done;
2316 if (gw_trans->repo_folder != NULL)
2317 path = strdup(gw_trans->repo_folder);
2318 else if (in_repo_path) {
2319 free(path);
2320 path = in_repo_path;
2323 if (commit_str == NULL) {
2324 struct got_reference *head_ref;
2325 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
2326 if (error)
2327 goto done;
2329 error = got_ref_resolve(&commit_id, repo, head_ref);
2330 got_ref_close(head_ref);
2332 } else
2333 error = got_repo_match_object_id(&commit_id, NULL, commit_str,
2334 GOT_OBJ_TYPE_COMMIT, 1, repo);
2335 if (error)
2336 goto done;
2338 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
2339 if (error)
2340 goto done;
2342 error = got_object_open_as_tree(&tree, repo, tree_id);
2343 if (error)
2344 goto done;
2346 nentries = got_object_tree_get_nentries(tree);
2348 for (i = 0; i < nentries; i++) {
2349 struct got_tree_entry *te;
2350 const char *modestr = "";
2351 char *id = NULL, *url_html = NULL;
2353 te = got_object_tree_get_entry(tree, i);
2355 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
2356 if (error)
2357 goto done;
2359 if ((asprintf(&id, "%s", id_str)) == -1) {
2360 error = got_error_from_errno("asprintf");
2361 free(id_str);
2362 goto done;
2365 mode_t mode = got_tree_entry_get_mode(te);
2367 if (got_object_tree_entry_is_submodule(te))
2368 modestr = "$";
2369 else if (S_ISLNK(mode))
2370 modestr = "@";
2371 else if (S_ISDIR(mode))
2372 modestr = "/";
2373 else if (mode & S_IXUSR)
2374 modestr = "*";
2376 char *build_folder = NULL;
2377 if (S_ISDIR(got_tree_entry_get_mode(te))) {
2378 if (gw_trans->repo_folder != NULL) {
2379 if ((asprintf(&build_folder, "%s/%s",
2380 gw_trans->repo_folder,
2381 got_tree_entry_get_name(te))) == -1) {
2382 error =
2383 got_error_from_errno("asprintf");
2384 goto done;
2386 } else {
2387 if (asprintf(&build_folder, "%s",
2388 got_tree_entry_get_name(te)) == -1)
2389 goto done;
2392 if ((asprintf(&url_html, folder_html,
2393 gw_trans->repo_name, gw_trans->action_name,
2394 gw_trans->commit, build_folder,
2395 got_tree_entry_get_name(te), modestr)) == -1) {
2396 error = got_error_from_errno("asprintf");
2397 goto done;
2399 } else {
2400 if (gw_trans->repo_folder != NULL) {
2401 if ((asprintf(&build_folder, "%s",
2402 gw_trans->repo_folder)) == -1) {
2403 error =
2404 got_error_from_errno("asprintf");
2405 goto done;
2407 } else
2408 build_folder = strdup("");
2410 if ((asprintf(&url_html, file_html, gw_trans->repo_name,
2411 "blame", gw_trans->commit,
2412 got_tree_entry_get_name(te), build_folder,
2413 got_tree_entry_get_name(te), modestr)) == -1) {
2414 error = got_error_from_errno("asprintf");
2415 goto done;
2418 free(build_folder);
2420 if (error)
2421 goto done;
2423 if ((asprintf(&tree_row, trees_row, "", url_html)) == -1) {
2424 error = got_error_from_errno("asprintf");
2425 goto done;
2427 error = buf_puts(&newsize, diffbuf, tree_row);
2428 if (error)
2429 goto done;
2431 free(id);
2432 free(id_str);
2433 free(url_html);
2434 free(tree_row);
2437 if (buf_len(diffbuf) > 0) {
2438 error = buf_putc(diffbuf, '\0');
2439 tree_html = strdup(buf_get(diffbuf));
2441 done:
2442 if (tree)
2443 got_object_tree_close(tree);
2444 if (repo)
2445 got_repo_close(repo);
2447 free(in_repo_path);
2448 free(tree_id);
2449 free(diffbuf);
2450 if (error)
2451 return NULL;
2452 else
2453 return tree_html;
2456 static char *
2457 gw_get_repo_heads(struct gw_trans *gw_trans)
2459 const struct got_error *error = NULL;
2460 struct got_repository *repo = NULL;
2461 struct got_reflist_head refs;
2462 struct got_reflist_entry *re;
2463 char *heads, *head_row = NULL, *head_navs_disp = NULL, *age = NULL;
2464 struct buf *diffbuf = NULL;
2465 size_t newsize;
2467 error = buf_alloc(&diffbuf, 0);
2468 if (error)
2469 return NULL;
2471 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2472 if (error)
2473 goto done;
2475 SIMPLEQ_INIT(&refs);
2476 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
2477 NULL);
2478 if (error)
2479 goto done;
2481 SIMPLEQ_FOREACH(re, &refs, entry) {
2482 char *refname;
2484 refname = strdup(got_ref_get_name(re->ref));
2485 if (refname == NULL) {
2486 error = got_error_from_errno("got_ref_to_str");
2487 goto done;
2490 if (strncmp(refname, "refs/heads/", 11) != 0) {
2491 free(refname);
2492 continue;
2495 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path, refname,
2496 TM_DIFF);
2498 if ((asprintf(&head_navs_disp, heads_navs, gw_trans->repo_name,
2499 refname, gw_trans->repo_name, refname,
2500 gw_trans->repo_name, refname, gw_trans->repo_name,
2501 refname)) == -1) {
2502 error = got_error_from_errno("asprintf");
2503 goto done;
2506 if (strncmp(refname, "refs/heads/", 11) == 0)
2507 refname += 11;
2509 if ((asprintf(&head_row, heads_row, age, refname,
2510 head_navs_disp)) == -1) {
2511 error = got_error_from_errno("asprintf");
2512 goto done;
2515 error = buf_puts(&newsize, diffbuf, head_row);
2517 free(head_navs_disp);
2518 free(head_row);
2521 if (buf_len(diffbuf) > 0) {
2522 error = buf_putc(diffbuf, '\0');
2523 heads = strdup(buf_get(diffbuf));
2525 done:
2526 buf_free(diffbuf);
2527 got_ref_list_free(&refs);
2528 if (repo)
2529 got_repo_close(repo);
2530 if (error)
2531 return NULL;
2532 else
2533 return heads;
2536 static char *
2537 gw_get_got_link(struct gw_trans *gw_trans)
2539 char *link;
2541 if ((asprintf(&link, got_link, gw_trans->gw_conf->got_logo_url,
2542 gw_trans->gw_conf->got_logo)) == -1)
2543 return NULL;
2545 return link;
2548 static char *
2549 gw_get_site_link(struct gw_trans *gw_trans)
2551 char *link, *repo = "", *action = "";
2553 if (gw_trans->repo_name != NULL)
2554 if ((asprintf(&repo, " / <a href='?path=%s&action=summary'>%s" \
2555 "</a>", gw_trans->repo_name, gw_trans->repo_name)) == -1)
2556 return NULL;
2558 if (gw_trans->action_name != NULL)
2559 if ((asprintf(&action, " / %s", gw_trans->action_name)) == -1)
2560 return NULL;
2562 if ((asprintf(&link, site_link, GOTWEB,
2563 gw_trans->gw_conf->got_site_link, repo, action)) == -1)
2564 return NULL;
2566 return link;
2569 static char *
2570 gw_colordiff_line(char *buf)
2572 const struct got_error *error = NULL;
2573 char *colorized_line = NULL, *div_diff_line_div = NULL, *color = NULL;
2574 struct buf *diffbuf = NULL;
2575 size_t newsize;
2577 error = buf_alloc(&diffbuf, 0);
2578 if (error)
2579 return NULL;
2581 if (strncmp(buf, "-", 1) == 0)
2582 color = "diff_minus";
2583 if (strncmp(buf, "+", 1) == 0)
2584 color = "diff_plus";
2585 if (strncmp(buf, "@@", 2) == 0)
2586 color = "diff_chunk_header";
2587 if (strncmp(buf, "@@", 2) == 0)
2588 color = "diff_chunk_header";
2589 if (strncmp(buf, "commit +", 8) == 0)
2590 color = "diff_meta";
2591 if (strncmp(buf, "commit -", 8) == 0)
2592 color = "diff_meta";
2593 if (strncmp(buf, "blob +", 6) == 0)
2594 color = "diff_meta";
2595 if (strncmp(buf, "blob -", 6) == 0)
2596 color = "diff_meta";
2597 if (strncmp(buf, "file +", 6) == 0)
2598 color = "diff_meta";
2599 if (strncmp(buf, "file -", 6) == 0)
2600 color = "diff_meta";
2601 if (strncmp(buf, "from:", 5) == 0)
2602 color = "diff_author";
2603 if (strncmp(buf, "via:", 4) == 0)
2604 color = "diff_author";
2605 if (strncmp(buf, "date:", 5) == 0)
2606 color = "diff_date";
2608 if ((asprintf(&div_diff_line_div, div_diff_line, color)) == -1)
2609 return NULL;
2611 error = buf_puts(&newsize, diffbuf, div_diff_line_div);
2612 if (error)
2613 return NULL;
2615 error = buf_puts(&newsize, diffbuf, buf);
2616 if (error)
2617 return NULL;
2619 if (buf_len(diffbuf) > 0) {
2620 error = buf_putc(diffbuf, '\0');
2621 colorized_line = strdup(buf_get(diffbuf));
2624 free(diffbuf);
2625 free(div_diff_line_div);
2626 return colorized_line;
2629 static char *
2630 gw_html_escape(const char *html)
2632 char *escaped_str = NULL, *buf;
2633 char c[1];
2634 size_t sz, i, buff_sz = 2048;
2636 if ((buf = calloc(buff_sz, sizeof(char *))) == NULL)
2637 return NULL;
2639 if (html == NULL)
2640 return NULL;
2641 else
2642 if ((sz = strlen(html)) == 0)
2643 return NULL;
2645 /* only work with buff_sz */
2646 if (buff_sz < sz)
2647 sz = buff_sz;
2649 for (i = 0; i < sz; i++) {
2650 c[0] = html[i];
2651 switch (c[0]) {
2652 case ('>'):
2653 strcat(buf, "&gt;");
2654 break;
2655 case ('&'):
2656 strcat(buf, "&amp;");
2657 break;
2658 case ('<'):
2659 strcat(buf, "&lt;");
2660 break;
2661 case ('"'):
2662 strcat(buf, "&quot;");
2663 break;
2664 case ('\''):
2665 strcat(buf, "&apos;");
2666 break;
2667 case ('\n'):
2668 strcat(buf, "<br />");
2669 default:
2670 strcat(buf, &c[0]);
2671 break;
2674 asprintf(&escaped_str, "%s", buf);
2675 free(buf);
2676 return escaped_str;
2679 int
2680 main(int argc, char *argv[])
2682 const struct got_error *error = NULL;
2683 struct gw_trans *gw_trans;
2684 struct gw_dir *dir = NULL, *tdir;
2685 const char *page = "index";
2686 int gw_malloc = 1;
2688 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
2689 errx(1, "malloc");
2691 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
2692 errx(1, "malloc");
2694 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
2695 errx(1, "malloc");
2697 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
2698 errx(1, "malloc");
2700 if (KCGI_OK != khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX,
2701 &page, 1, 0))
2702 errx(1, "khttp_parse");
2704 if ((gw_trans->gw_conf =
2705 malloc(sizeof(struct gotweb_conf))) == NULL) {
2706 gw_malloc = 0;
2707 error = got_error_from_errno("malloc");
2708 goto err;
2711 TAILQ_INIT(&gw_trans->gw_dirs);
2713 gw_trans->page = 0;
2714 gw_trans->repos_total = 0;
2715 gw_trans->repo_path = NULL;
2716 gw_trans->commit = NULL;
2717 gw_trans->headref = strdup(GOT_REF_HEAD);
2718 gw_trans->mime = KMIME_TEXT_HTML;
2719 gw_trans->gw_tmpl->key = gw_templs;
2720 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
2721 gw_trans->gw_tmpl->arg = gw_trans;
2722 gw_trans->gw_tmpl->cb = gw_template;
2723 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
2725 err:
2726 if (error) {
2727 gw_trans->mime = KMIME_TEXT_PLAIN;
2728 gw_trans->action = GW_ERR;
2729 gw_display_index(gw_trans, error);
2730 goto done;
2733 error = gw_parse_querystring(gw_trans);
2734 if (error)
2735 goto err;
2737 gw_display_index(gw_trans, error);
2739 done:
2740 if (gw_malloc) {
2741 free(gw_trans->gw_conf->got_repos_path);
2742 free(gw_trans->gw_conf->got_www_path);
2743 free(gw_trans->gw_conf->got_site_name);
2744 free(gw_trans->gw_conf->got_site_owner);
2745 free(gw_trans->gw_conf->got_site_link);
2746 free(gw_trans->gw_conf->got_logo);
2747 free(gw_trans->gw_conf->got_logo_url);
2748 free(gw_trans->gw_conf);
2749 free(gw_trans->commit);
2750 free(gw_trans->repo_path);
2751 free(gw_trans->repo_name);
2752 free(gw_trans->repo_file);
2753 free(gw_trans->action_name);
2754 free(gw_trans->headref);
2756 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
2757 free(dir->name);
2758 free(dir->description);
2759 free(dir->age);
2760 free(dir->url);
2761 free(dir->path);
2762 free(dir);
2767 khttp_free(gw_trans->gw_req);
2768 return EXIT_SUCCESS;