Blob


1 {!
2 /*
3 * Copyright (c) 2022 Omar Polo <op@openbsd.org>
4 * Copyright (c) 2016, 2019, 2020-2022 Tracey Emery <tracey@traceyemery.net>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
19 #include <sys/types.h>
20 #include <sys/queue.h>
21 #include <sys/stat.h>
23 #include <ctype.h>
24 #include <event.h>
25 #include <stdint.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sha1.h>
30 #include <sha2.h>
31 #include <imsg.h>
33 #include "got_error.h"
34 #include "got_object.h"
35 #include "got_reference.h"
37 #include "gotwebd.h"
38 #include "tmpl.h"
40 enum gotweb_ref_tm {
41 TM_DIFF,
42 TM_LONG,
43 };
45 static int breadcumbs(struct template *);
46 static int datetime(struct template *, time_t, int);
47 static int gotweb_render_blob_line(struct template *, const char *, size_t);
48 static int gotweb_render_tree_item(struct template *, struct got_tree_entry *);
49 static int blame_line(struct template *, const char *, struct blame_line *,
50 int, int);
52 static inline int gotweb_render_more(struct template *, int);
54 static inline int diff_line(struct template *, char *);
55 static inline int tag_item(struct template *, struct repo_tag *);
56 static inline int branch(struct template *, struct got_reflist_entry *);
57 static inline int rss_tag_item(struct template *, struct repo_tag *);
58 static inline int rss_author(struct template *, char *);
60 static inline char *
61 nextsep(char *s, char **t)
62 {
63 char *q;
65 while (*s == '/')
66 s++;
67 *t = s;
68 if (*s == '\0')
69 return NULL;
71 q = strchr(s, '/');
72 if (q == NULL)
73 q = strchr(s, '\0');
74 return q;
75 }
77 !}
79 {{ define datetime(struct template *tp, time_t t, int fmt) }}
80 {!
81 struct tm tm;
82 char rfc3339[64];
83 char datebuf[64];
85 if (gmtime_r(&t, &tm) == NULL)
86 return -1;
88 if (strftime(rfc3339, sizeof(rfc3339), "%FT%TZ", &tm) == 0)
89 return -1;
91 if (fmt != TM_DIFF && asctime_r(&tm, datebuf) == NULL)
92 return -1;
93 !}
94 <time datetime="{{ rfc3339 }}">
95 {{ if fmt == TM_DIFF }}
96 {{ render gotweb_render_age(tp, t) }}
97 {{ else }}
98 {{ datebuf }} {{ " UTC" }}
99 {{ end }}
100 </time>
101 {{ end }}
103 {{ define breadcumbs(struct template *tp) }}
104 {!
105 struct request *c = tp->tp_arg;
106 struct querystring *qs = c->t->qs;
107 struct gotweb_url url;
108 const char *folder = qs->folder;
109 char *t, *s = NULL, *dir = NULL;
110 char ch;
112 memset(&url, 0, sizeof(url));
113 url.index_page = -1;
114 url.page = -1;
115 url.action = TREE;
116 url.path = qs->path;
117 url.commit = qs->commit;
119 if (folder && *folder != '\0') {
120 while (*folder == '/')
121 folder++;
122 dir = strdup(folder);
123 if (dir == NULL)
124 return (-1);
125 s = dir;
127 !}
128 {{ " / " }}
129 <a href="{{ render gotweb_render_url(c, &url) }}">tree</a>
130 {{ " / " }}
131 {{ if dir }}
132 {{ while (s = nextsep(s, &t)) != NULL }}
133 {!
134 ch = *s;
135 *s = '\0';
136 url.folder = dir;
137 !}
139 <a href="{{ render gotweb_render_url(c, &url) }}">
140 {{ t }}
141 </a>
142 {{ " / " }}
144 {! *s = ch; !}
145 {{ end }}
146 {{ end }}
148 {{ if qs->file }}
149 {{ qs->file }}
150 {{ end}}
152 {{ finally }}
153 {! free(dir); !}
154 {{ end }}
156 {{ define gotweb_render_page(struct template *tp,
157 int (*body)(struct template *)) }}
158 {!
159 struct request *c = tp->tp_arg;
160 struct server *srv = c->srv;
161 struct querystring *qs = c->t->qs;
162 struct gotweb_url u_path;
163 const char *prfx = c->document_uri;
164 const char *css = srv->custom_css;
166 memset(&u_path, 0, sizeof(u_path));
167 u_path.index_page = -1;
168 u_path.page = -1;
169 u_path.action = SUMMARY;
170 !}
171 <!doctype html>
172 <html>
173 <head>
174 <meta charset="utf-8" />
175 <title>{{ srv->site_name }}</title>
176 <meta name="viewport" content="initial-scale=1.0" />
177 <meta name="msapplication-TileColor" content="#da532c" />
178 <meta name="theme-color" content="#ffffff"/>
179 <link rel="apple-touch-icon" sizes="180x180" href="{{ prfx }}apple-touch-icon.png" />
180 <link rel="icon" type="image/png" sizes="32x32" href="{{ prfx }}favicon-32x32.png" />
181 <link rel="icon" type="image/png" sizes="16x16" href="{{ prfx }}favicon-16x16.png" />
182 <link rel="manifest" href="{{ prfx }}site.webmanifest"/>
183 <link rel="mask-icon" href="{{ prfx }}safari-pinned-tab.svg" />
184 <link rel="stylesheet" type="text/css" href="{{ prfx }}{{ css }}" />
185 </head>
186 <body>
187 <header id="header">
188 <div id="got_link">
189 <a href="{{ srv->logo_url }}" target="_blank">
190 <img src="{{ prfx }}{{ srv->logo }}" />
191 </a>
192 </div>
193 </header>
194 <nav id="site_path">
195 <div id="site_link">
196 <a href="?index_page={{ printf "%d", qs->index_page }}">
197 {{ srv->site_link }}
198 </a>
199 {{ if qs->path }}
200 {! u_path.path = qs->path; !}
201 {{ " / " }}
202 <a href="{{ render gotweb_render_url(tp->tp_arg, &u_path)}}">
203 {{ qs->path }}
204 </a>
205 {{ end }}
206 {{ if qs->action == TREE || qs->action == BLOB }}
207 {{ render breadcumbs(tp) }}
208 {{ else if qs->action != INDEX }}
209 {{ " / " }}{{ gotweb_action_name(qs->action) }}
210 {{ end }}
211 </div>
212 </nav>
213 <main>
214 {{ render body(tp) }}
215 </main>
216 <footer id="site_owner_wrapper">
217 <p id="site_owner">
218 {{ if srv->show_site_owner }}
219 {{ srv->site_owner }}
220 {{ end }}
221 </p>
222 </footer>
223 </body>
224 </html>
225 {{ end }}
227 {{ define gotweb_render_error(struct template *tp) }}
228 {!
229 struct request *c = tp->tp_arg;
230 struct transport *t = c->t;
231 !}
232 <div id="err_content">
233 {{ if t->error }}
234 {{ t->error->msg }}
235 {{ else }}
236 See daemon logs for details
237 {{ end }}
238 </div>
239 {{ end }}
241 {{ define gotweb_render_repo_table_hdr(struct template *tp) }}
242 {!
243 struct request *c = tp->tp_arg;
244 struct server *srv = c->srv;
245 !}
246 <div id="index_header">
247 <div class="index_project">
248 Project
249 </div>
250 {{ if srv->show_repo_description }}
251 <div class="index_project_description">
252 Description
253 </div>
254 {{ end }}
255 {{ if srv->show_repo_owner }}
256 <div class="index_project_owner">
257 Owner
258 </div>
259 {{ end }}
260 {{ if srv->show_repo_age }}
261 <div class="index_project_age">
262 Last Change
263 </div>
264 {{ end }}
265 </div>
266 {{ end }}
268 {{ define gotweb_render_repo_fragment(struct template *tp, struct repo_dir *repo_dir) }}
269 {!
270 struct request *c = tp->tp_arg;
271 struct server *srv = c->srv;
272 struct gotweb_url summary = {
273 .action = SUMMARY,
274 .index_page = -1,
275 .page = -1,
276 .path = repo_dir->name,
277 }, briefs = {
278 .action = BRIEFS,
279 .index_page = -1,
280 .page = -1,
281 .path = repo_dir->name,
282 }, commits = {
283 .action = COMMITS,
284 .index_page = -1,
285 .page = -1,
286 .path = repo_dir->name,
287 }, tags = {
288 .action = TAGS,
289 .index_page = -1,
290 .page = -1,
291 .path = repo_dir->name,
292 }, tree = {
293 .action = TREE,
294 .index_page = -1,
295 .page = -1,
296 .path = repo_dir->name,
297 }, rss = {
298 .action = RSS,
299 .index_page = -1,
300 .page = -1,
301 .path = repo_dir->name,
302 };
303 !}
304 <div class="index_wrapper">
305 <div class="index_project">
306 <a href="{{ render gotweb_render_url(tp->tp_arg, &summary) }}">{{ repo_dir->name }}</a>
307 </div>
308 {{ if srv->show_repo_description }}
309 <div class="index_project_description">
310 {{ repo_dir->description }}
311 </div>
312 {{ end }}
313 {{ if srv->show_repo_owner }}
314 <div class="index_project_owner">
315 {{ repo_dir->owner }}
316 </div>
317 {{ end }}
318 {{ if srv->show_repo_age }}
319 <div class="index_project_age">
320 {{ render datetime(tp, repo_dir->age, TM_DIFF) }}
321 </div>
322 {{ end }}
323 <div class="navs_wrapper">
324 <div class="navs">
325 <a href="{{ render gotweb_render_url(tp->tp_arg, &summary) }}">summary</a>
326 {{ " | " }}
327 <a href="{{ render gotweb_render_url(tp->tp_arg, &briefs) }}">briefs</a>
328 {{ " | " }}
329 <a href="{{ render gotweb_render_url(tp->tp_arg, &commits) }}">commits</a>
330 {{ " | " }}
331 <a href="{{ render gotweb_render_url(tp->tp_arg, &tags) }}">tags</a>
332 {{ " | " }}
333 <a href="{{ render gotweb_render_url(tp->tp_arg, &tree) }}">tree</a>
334 {{ " | " }}
335 <a href="{{ render gotweb_render_url(tp->tp_arg, &rss) }}">rss</a>
336 </div>
337 <hr />
338 </div>
339 </div>
340 {{ end }}
342 {{ define gotweb_render_briefs(struct template *tp) }}
343 {!
344 struct request *c = tp->tp_arg;
345 struct transport *t = c->t;
346 struct querystring *qs = c->t->qs;
347 struct repo_commit *rc;
348 struct repo_dir *repo_dir = t->repo_dir;
349 struct gotweb_url diff_url, patch_url, tree_url;
350 char *tmp, *body;
352 diff_url = (struct gotweb_url){
353 .action = DIFF,
354 .index_page = -1,
355 .page = -1,
356 .path = repo_dir->name,
357 .headref = qs->headref,
358 };
359 patch_url = (struct gotweb_url){
360 .action = PATCH,
361 .index_page = -1,
362 .page = -1,
363 .path = repo_dir->name,
364 .headref = qs->headref,
365 };
366 tree_url = (struct gotweb_url){
367 .action = TREE,
368 .index_page = -1,
369 .page = -1,
370 .path = repo_dir->name,
371 .headref = qs->headref,
372 };
373 !}
374 <header class='subtitle'>
375 <h2>Commit Briefs</h2>
376 </header>
377 <div id="briefs_content">
378 {{ tailq-foreach rc &t->repo_commits entry }}
379 {!
380 diff_url.commit = rc->commit_id;
381 patch_url.commit = rc->commit_id;
382 tree_url.commit = rc->commit_id;
384 tmp = strchr(rc->committer, '<');
385 if (tmp)
386 *tmp = '\0';
388 body = strchr(rc->commit_msg, '\n');
389 if (body) {
390 *body++ = '\0';
391 while (*body == '\n')
392 body++;
394 !}
395 <div class='brief'>
396 <p class='brief_meta'>
397 <span class='briefs_age'>
398 {{ render datetime(tp, rc->committer_time, TM_DIFF) }}
399 </span>
400 {{" "}}
401 <span class="briefs_author">
402 {{ rc->committer }}
403 </span>
404 </p>
405 {{ if body && *body != '\0' }}
406 <details class="briefs_log">
407 <summary>
408 <a href="{{ render gotweb_render_url(tp->tp_arg, &diff_url) }}">
409 {{ rc->commit_msg }}
410 </a>
411 {{ if rc->refs_str }}
412 {{ " " }} <span class="refs_str">({{ rc->refs_str }})</span>
413 {{ end }}
414 {{ " " }}
415 <span class="briefs_toggle" aria-hidden="true">
416 {{ " ⋅⋅⋅ " | unsafe }}
417 </span>
418 </summary>
419 {{ "\n" }}
420 <p>{{ body }}</p>
421 </details>
422 {{ else }}
423 <p class="briefs_log">
424 <a href="{{ render gotweb_render_url(tp->tp_arg, &diff_url) }}">
425 {{ rc->commit_msg }}
426 </a>
427 {{ if rc->refs_str }}
428 {{ " " }} <span class="refs_str">({{ rc->refs_str }})</span>
429 {{ end }}
430 </p>
431 {{ end }}
432 </div>
433 <div class="navs_wrapper">
434 <div class="navs">
435 <a href="{{ render gotweb_render_url(tp->tp_arg, &diff_url) }}">diff</a>
436 {{ " | " }}
437 <a href="{{ render gotweb_render_url(tp->tp_arg, &patch_url) }}">patch</a>
438 {{ " | " }}
439 <a href="{{ render gotweb_render_url(tp->tp_arg, &tree_url) }}">tree</a>
440 </div>
441 </div>
442 <hr />
443 {{ end }}
444 {{ render gotweb_render_more(tp, BRIEFS) }}
445 </div>
446 {{ end }}
448 {{ define gotweb_render_more(struct template *tp, int action) }}
449 {!
450 struct request *c = tp->tp_arg;
451 struct transport *t = c->t;
452 struct querystring *qs = t->qs;
453 struct gotweb_url more = {
454 .action = action,
455 .index_page = -1,
456 .path = qs->path,
457 .commit = t->more_id,
458 .headref = qs->headref,
459 .file = qs->file,
460 };
461 !}
462 {{ if t->more_id }}
463 <div id="np_wrapper">
464 <div id="nav_more">
465 <a href="{{ render gotweb_render_url(c, &more) }}">
466 More&nbsp;&darr;
467 </a>
468 </div>
469 </div>
470 {{ end }}
471 {{ end }}
473 {{ define gotweb_render_navs(struct template *tp) }}
474 {!
475 struct request *c = tp->tp_arg;
476 struct transport *t = c->t;
477 struct gotweb_url prev, next;
478 int have_prev, have_next;
480 gotweb_get_navs(c, &prev, &have_prev, &next, &have_next);
481 !}
482 <div id="np_wrapper">
483 <div id="nav_prev">
484 {{ if have_prev }}
485 <a href="{{ render gotweb_render_url(c, &prev) }}">
486 Previous
487 </a>
488 {{ end }}
489 </div>
490 <div id="nav_next">
491 {{ if have_next }}
492 <a href="{{ render gotweb_render_url(c, &next) }}">
493 Next
494 </a>
495 {{ end }}
496 </div>
497 </div>
498 {{ finally }}
499 {!
500 free(t->next_id);
501 t->next_id = NULL;
502 free(t->prev_id);
503 t->prev_id = NULL;
504 !}
505 {{ end }}
507 {{ define gotweb_render_commits(struct template *tp) }}
508 {!
509 struct request *c = tp->tp_arg;
510 struct transport *t = c->t;
511 struct repo_dir *repo_dir = t->repo_dir;
512 struct repo_commit *rc;
513 struct gotweb_url diff, patch, tree;
515 diff = (struct gotweb_url){
516 .action = DIFF,
517 .index_page = -1,
518 .page = -1,
519 .path = repo_dir->name,
520 };
521 patch = (struct gotweb_url){
522 .action = PATCH,
523 .index_page = -1,
524 .page = -1,
525 .path = repo_dir->name,
526 };
527 tree = (struct gotweb_url){
528 .action = TREE,
529 .index_page = -1,
530 .page = -1,
531 .path = repo_dir->name,
532 };
533 !}
534 <header class="subtitle">
535 <h2>Commits</h2>
536 </header>
537 <div class="commits_content">
538 {{ tailq-foreach rc &t->repo_commits entry }}
539 {!
540 diff.commit = rc->commit_id;
541 patch.commit = rc->commit_id;
542 tree.commit = rc->commit_id;
543 !}
544 <div class="page_header_wrapper">
545 <dl>
546 <dt>Commit:</dt>
547 <dd><code class="commit-id">{{ rc->commit_id }}</code></dd>
548 <dt>From:</dt>
549 <dd>{{ rc->author }}</dd>
550 {{ if strcmp(rc->committer, rc->author) != 0 }}
551 <dt>Via:</dt>
552 <dd>{{ rc->committer }}</dd>
553 {{ end }}
554 <dt>Date:</dt>
555 <dd>
556 {{ render datetime(tp, rc->committer_time, TM_LONG) }}
557 </dd>
558 </dl>
559 </div>
560 <hr />
561 <div class="commit">
562 {{ "\n" }}
563 {{ rc->commit_msg }}
564 </div>
565 <div class="navs_wrapper">
566 <div class="navs">
567 <a href="{{ render gotweb_render_url(c, &diff) }}">diff</a>
568 {{ " | " }}
569 <a href="{{ render gotweb_render_url(c, &patch) }}">patch</a>
570 {{ " | " }}
571 <a href="{{ render gotweb_render_url(c, &tree) }}">tree</a>
572 </div>
573 </div>
574 <hr />
575 {{ end }}
576 {{ render gotweb_render_more(tp, COMMITS) }}
577 </div>
578 {{ end }}
580 {{ define gotweb_render_blob(struct template *tp) }}
581 {!
582 struct request *c = tp->tp_arg;
583 struct transport *t = c->t;
584 struct querystring *qs = t->qs;
585 struct got_blob_object *blob = t->blob;
586 struct repo_commit *rc = TAILQ_FIRST(&t->repo_commits);
587 struct gotweb_url briefs_url, blame_url, raw_url;
589 memset(&briefs_url, 0, sizeof(briefs_url));
590 briefs_url.index_page = -1,
591 briefs_url.page = -1,
592 briefs_url.action = BRIEFS,
593 briefs_url.path = qs->path,
594 briefs_url.commit = qs->commit,
595 briefs_url.folder = qs->folder,
596 briefs_url.file = qs->file,
598 memcpy(&blame_url, &briefs_url, sizeof(blame_url));
599 blame_url.action = BLAME;
601 memcpy(&raw_url, &briefs_url, sizeof(raw_url));
602 raw_url.action = BLOBRAW;
603 !}
604 <header class="subtitle">
605 <h2>Blob</h2>
606 </header>
607 <div id="blob_content">
608 <div class="page_header_wrapper">
609 <dl>
610 <dt>Date:</dt>
611 <dd>
612 {{ render datetime(tp, rc->committer_time, TM_LONG) }}
613 </dd>
614 <dt>Message:</dt>
615 <dd class="commit-msg">{{ rc->commit_msg }}</dd>
616 <dt>Actions:</dt>
617 <dd>
618 <a href="{{ render gotweb_render_url(c, &briefs_url) }}">
619 History
620 </a>
621 {{" | "}}
622 <a href="{{ render gotweb_render_url(c, &blame_url) }}">
623 Blame
624 </a>
625 {{" | "}}
626 <a href="{{ render gotweb_render_url(c, &raw_url) }}">
627 Raw File
628 </a>
629 </dd>
630 </dl>
631 </div>
632 <hr />
633 <div id="blob">
634 <pre>
635 {{ render got_output_blob_by_lines(tp, blob, gotweb_render_blob_line) }}
636 </pre>
637 </div>
638 </div>
639 {{ end }}
641 {{ define gotweb_render_blob_line(struct template *tp, const char *line,
642 size_t no) }}
643 {!
644 char lineno[16];
645 int r;
647 r = snprintf(lineno, sizeof(lineno), "%zu", no);
648 if (r < 0 || (size_t)r >= sizeof(lineno))
649 return -1;
650 !}
651 <div class="blob_line" id="line{{ lineno }}">
652 <a href="#line{{ lineno }}">{{ lineno }}</a>
653 {{" "}}
654 <span class="blob_code">{{ line }}</span>
655 </div>
656 {{ end }}
658 {{ define gotweb_render_tree(struct template *tp) }}
659 {!
660 const struct got_error *error;
661 struct request *c = tp->tp_arg;
662 struct transport *t = c->t;
663 struct querystring *qs = t->qs;
664 struct repo_commit *rc = TAILQ_FIRST(&t->repo_commits);
665 struct gotweb_url url;
666 char *readme = NULL;
667 int binary;
668 const uint8_t *buf;
669 size_t len;
670 !}
671 <header class='subtitle'>
672 <h2>Tree</h2>
673 </header>
674 <div id="tree_content">
675 <div class="page_header_wrapper">
676 <dl>
677 <dt>Tree:</dt>
678 <dd><code class="commit-id">{{ rc->tree_id }}</code></dd>
679 <dt>Date:</dt>
680 <dd>
681 {{ render datetime(tp, rc->committer_time, TM_LONG) }}
682 </dd>
683 <dt>Message:</dt>
684 <dd class="commit-msg">{{ rc->commit_msg }}</dd>
685 </dl>
686 </div>
687 <hr />
688 <table id="tree">
689 {{ render got_output_repo_tree(c, &readme, gotweb_render_tree_item) }}
690 </table>
691 {{ if readme }}
692 {!
693 error = got_open_blob_for_output(&t->blob, &t->fd, &binary, c,
694 qs->folder, readme, qs->commit);
695 if (error) {
696 free(readme);
697 return (-1);
700 /* hum... */
701 memset(&url, 0, sizeof(url));
702 url.index_page = -1;
703 url.page = -1;
704 url.action = BLOB;
705 url.path = t->qs->path;
706 url.file = readme;
707 url.folder = t->qs->folder;
708 url.commit = t->qs->commit;
709 !}
710 {{ if !binary }}
711 <h2>
712 <a href="{{ render gotweb_render_url(c, &url) }}">
713 {{ readme }}
714 </a>
715 </h2>
716 <pre>
717 {!
718 for (;;) {
719 error = got_object_blob_read_block(&len, t->blob);
720 if (error) {
721 free(readme);
722 return (-1);
724 if (len == 0)
725 break;
726 buf = got_object_blob_get_read_buf(t->blob);
727 if (tp_write_htmlescape(tp, buf, len) == -1) {
728 free(readme);
729 return (-1);
732 !}
733 </pre>
734 {{ end }}
735 {{ end }}
736 </div>
737 {{ finally }}
738 {! free(readme); !}
739 {{ end }}
741 {{ define gotweb_render_tree_item(struct template *tp,
742 struct got_tree_entry *te) }}
743 {!
744 struct request *c = tp->tp_arg;
745 struct transport *t = c->t;
746 struct querystring *qs = t->qs;
747 struct repo_commit *rc = TAILQ_FIRST(&t->repo_commits);
748 const char *modestr = "";
749 const char *name;
750 const char *folder;
751 char *dir = NULL;
752 mode_t mode;
753 struct gotweb_url url = {
754 .index_page = -1,
755 .page = -1,
756 .commit = rc->commit_id,
757 .path = qs->path,
758 };
760 name = got_tree_entry_get_name(te);
761 mode = got_tree_entry_get_mode(te);
763 folder = qs->folder ? qs->folder : "";
764 if (S_ISDIR(mode)) {
765 if (asprintf(&dir, "%s/%s", folder, name) == -1)
766 return (-1);
768 url.action = TREE;
769 url.folder = dir;
770 } else {
771 url.action = BLOB;
772 url.folder = folder;
773 url.file = name;
776 if (got_object_tree_entry_is_submodule(te))
777 modestr = "$";
778 else if (S_ISLNK(mode))
779 modestr = "@";
780 else if (S_ISDIR(mode))
781 modestr = "/";
782 else if (mode & S_IXUSR)
783 modestr = "*";
784 !}
785 <tr class="tree_wrapper">
786 {{ if S_ISDIR(mode) }}
787 <td class="tree_line" colspan=2>
788 <a href="{{ render gotweb_render_url(c, &url) }}">
789 {{ name }}{{ modestr }}
790 </a>
791 </td>
792 {{ else }}
793 <td class="tree_line">
794 <a href="{{ render gotweb_render_url(c, &url) }}">
795 {{ name }}{{ modestr }}
796 </a>
797 </td>
798 <td class="tree_line_blank">
799 {! url.action = COMMITS; !}
800 <a href="{{ render gotweb_render_url(c, &url) }}">
801 commits
802 </a>
803 {{ " | " }}
804 {! url.action = BLAME; !}
805 <a href="{{ render gotweb_render_url(c, &url) }}">
806 blame
807 </a>
808 </td>
809 {{ end }}
810 </tr>
811 {{ finally }}
812 {!
813 free(dir);
814 !}
815 {{ end }}
817 {{ define gotweb_render_tags(struct template *tp) }}
818 {!
819 struct request *c = tp->tp_arg;
820 struct transport *t = c->t;
821 struct querystring *qs = t->qs;
822 struct repo_tag *rt;
823 int commit_found;
825 commit_found = qs->commit == NULL;
826 !}
827 <header class='subtitle'>
828 <h2>Tags</h2>
829 </header>
830 <div id="tags_content">
831 {{ if t->tag_count == 0 }}
832 <div id="err_content">
833 This repository contains no tags
834 </div>
835 {{ else }}
836 {{ tailq-foreach rt &t->repo_tags entry }}
837 {{ if commit_found || !strcmp(qs->commit, rt->commit_id) }}
838 {! commit_found = 1; !}
839 {{ render tag_item(tp, rt) }}
840 {{ end }}
841 {{ end }}
842 {{ if t->next_id || t->prev_id }}
843 {! qs->action = TAGS; !}
844 {{ render gotweb_render_navs(tp) }}
845 {{ end }}
846 {{ end }}
847 </div>
848 {{ end }}
850 {{ define tag_item(struct template *tp, struct repo_tag *rt) }}
851 {!
852 struct request *c = tp->tp_arg;
853 struct transport *t = c->t;
854 struct repo_dir *repo_dir = t->repo_dir;
855 char *tag_name = rt->tag_name;
856 char *msg = rt->tag_commit;
857 char *nl;
858 struct gotweb_url url = {
859 .action = TAG,
860 .index_page = -1,
861 .page = -1,
862 .path = repo_dir->name,
863 .commit = rt->commit_id,
864 };
866 if (strncmp(tag_name, "refs/tags/", 10) == 0)
867 tag_name += 10;
869 if (msg) {
870 nl = strchr(msg, '\n');
871 if (nl)
872 *nl = '\0';
874 !}
875 <div class="tag_age">
876 {{ render datetime(tp, rt->tagger_time, TM_DIFF) }}
877 </div>
878 <div class="tag_name">{{ tag_name }}</div>
879 <div class="tag_log">
880 <a href="{{ render gotweb_render_url(c, &url) }}">
881 {{ msg }}
882 </a>
883 </div>
884 <div class="navs_wrapper">
885 <div class="navs">
886 <a href="{{ render gotweb_render_url(c, &url) }}">tag</a>
887 {{ " | " }}
888 {! url.action = BRIEFS; !}
889 <a href="{{ render gotweb_render_url(c, &url) }}">commit briefs</a>
890 {{ " | " }}
891 {! url.action = COMMITS; !}
892 <a href="{{ render gotweb_render_url(c, &url) }}">commits</a>
893 </div>
894 </div>
895 <hr />
896 {{ end }}
898 {{ define gotweb_render_tag(struct template *tp) }}
899 {!
900 struct request *c = tp->tp_arg;
901 struct transport *t = c->t;
902 struct repo_tag *rt;
903 const char *tag_name;
905 rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
906 tag_name = rt->tag_name;
908 if (strncmp(tag_name, "refs/", 5) == 0)
909 tag_name += 5;
910 !}
911 <header class="subtitle">
912 <h2>Tag</h2>
913 </header>
914 <div id="tags_content">
915 <div class="page_header_wrapper">
916 <dl>
917 <dt>Commit:</dt>
918 <dd>
919 <code class="commit-id">{{ rt->commit_id }}</code>
920 {{ " " }}
921 <span class="refs_str">({{ tag_name }})</span>
922 </dd>
923 <dt>Tagger:</dt>
924 <dd>{{ rt->tagger }}</dd>
925 <dt>Date:</dt>
926 <dd>
927 {{ render datetime(tp, rt->tagger_time, TM_LONG)}}
928 </dd>
929 <dt>Message:</dt>
930 <dd class="commit-msg">{{ rt->commit_msg }}</dd>
931 </dl>
932 <hr />
933 <pre id="tag_commit">
934 {{ rt->tag_commit }}
935 </pre>
936 </div>
937 </div>
938 {{ end }}
940 {{ define gotweb_render_diff(struct template *tp) }}
941 {!
942 struct request *c = tp->tp_arg;
943 struct transport *t = c->t;
944 struct querystring *qs = t->qs;
945 FILE *fp = t->fp;
946 struct repo_commit *rc = TAILQ_FIRST(&t->repo_commits);
947 char *line = NULL;
948 size_t linesize = 0;
949 ssize_t linelen;
950 struct gotweb_url patch_url, tree_url = {
951 .action = TREE,
952 .index_page = -1,
953 .page = -1,
954 .path = qs->path,
955 .commit = rc->commit_id,
956 };
958 memcpy(&patch_url, &tree_url, sizeof(patch_url));
959 patch_url.action = PATCH;
960 !}
961 <header class="subtitle">
962 <h2>Commit Diff</h2>
963 </header>
964 <div id="diff_content">
965 <div class="page_header_wrapper">
966 <dl>
967 <dt>Commit:</dt>
968 <dd><code class="commit-id">{{ rc->commit_id }}</code></dd>
969 <dt>From:</dt>
970 <dd>{{ rc->author }}</dd>
971 {{ if strcmp(rc->committer, rc->author) != 0 }}
972 <dt>Via:</dt>
973 <dd>{{ rc->committer }}</dd>
974 {{ end }}
975 <dt>Date:</dt>
976 <dd>
977 {{ render datetime(tp, rc->committer_time, TM_LONG) }}
978 </dd>
979 <dt>Message:</dt>
980 <dd class="commit-msg">{{ rc->commit_msg }}</dd>
981 <dt>Actions:</dt>
982 <dd>
983 <a href="{{ render gotweb_render_url(c, &patch_url) }}">
984 Patch
985 </a>
986 {{ " &mdash; " | unsafe }}
987 <a href="{{ render gotweb_render_url(c, &tree_url) }}">
988 Tree
989 </a>
990 </dd>
991 </dl>
992 </div>
993 <hr />
994 <pre id="diff">
995 {{ while (linelen = getline(&line, &linesize, fp)) != -1 }}
996 {{ render diff_line(tp, line) }}
997 {{ end }}
998 </pre>
999 </div>
1000 {{ finally }}
1001 {! free(line); !}
1002 {{ end }}
1004 {{ define diff_line(struct template *tp, char *line )}}
1006 const char *color = NULL;
1007 char *nl;
1009 if (!strncmp(line, "-", 1))
1010 color = "diff_minus";
1011 else if (!strncmp(line, "+", 1))
1012 color = "diff_plus";
1013 else if (!strncmp(line, "@@", 2))
1014 color = "diff_chunk_header";
1015 else if (!strncmp(line, "commit +", 8) ||
1016 !strncmp(line, "commit -", 8) ||
1017 !strncmp(line, "blob +", 6) ||
1018 !strncmp(line, "blob -", 6) ||
1019 !strncmp(line, "file +", 6) ||
1020 !strncmp(line, "file -", 6))
1021 color = "diff_meta";
1022 else if (!strncmp(line, "from:", 5) || !strncmp(line, "via:", 4))
1023 color = "diff_author";
1024 else if (!strncmp(line, "date:", 5))
1025 color = "diff_date";
1027 nl = strchr(line, '\n');
1028 if (nl)
1029 *nl = '\0';
1031 <span class="diff_line {{ color }}">{{ line }}</span>{{"\n"}}
1032 {{ end }}
1034 {{ define gotweb_render_branches(struct template *tp,
1035 struct got_reflist_head *refs) }}
1037 struct got_reflist_entry *re;
1039 <header class='subtitle'>
1040 <h2>Branches</h2>
1041 </header>
1042 <div id="branches_content">
1043 {{ tailq-foreach re refs entry }}
1044 {{ if !got_ref_is_symbolic(re->ref) }}
1045 {{ render branch(tp, re) }}
1046 {{ end }}
1047 {{ end }}
1048 </div>
1049 {{ end }}
1051 {{ define branch(struct template *tp, struct got_reflist_entry *re) }}
1053 const struct got_error *err;
1054 struct request *c = tp->tp_arg;
1055 struct querystring *qs = c->t->qs;
1056 const char *refname;
1057 time_t age;
1058 struct gotweb_url url = {
1059 .action = SUMMARY,
1060 .index_page = -1,
1061 .page = -1,
1062 .path = qs->path,
1065 refname = got_ref_get_name(re->ref);
1067 err = got_get_repo_age(&age, c, refname);
1068 if (err) {
1069 log_warnx("%s: %s", __func__, err->msg);
1070 return -1;
1073 if (strncmp(refname, "refs/heads/", 11) == 0)
1074 refname += 11;
1076 url.headref = refname;
1078 <section class="branches_wrapper">
1079 <div class="branches_age">
1080 {{ render datetime(tp, age, TM_DIFF) }}
1081 </div>
1082 <div class="branch">
1083 <a href="{{ render gotweb_render_url(c, &url) }}">{{ refname }}</a>
1084 </div>
1085 <div class="navs_wrapper">
1086 <div class="navs">
1087 <a href="{{ render gotweb_render_url(c, &url) }}">summary</a>
1088 {{" | "}}
1089 {! url.action = BRIEFS; !}
1090 <a href="{{ render gotweb_render_url(c, &url) }}">commit briefs</a>
1091 {{" | "}}
1092 {! url.action = COMMITS; !}
1093 <a href="{{ render gotweb_render_url(c, &url) }}">commits</a>
1094 </div>
1095 </div>
1096 <hr />
1097 </section>
1098 {{ end }}
1100 {{ define gotweb_render_summary(struct template *tp) }}
1102 struct request *c = tp->tp_arg;
1103 struct server *srv = c->srv;
1104 struct transport *t = c->t;
1105 struct got_reflist_head *refs = &t->refs;
1107 <dl id="summary_wrapper" class="page_header_wrapper">
1108 {{ if srv->show_repo_description }}
1109 <dt>Description:</dt>
1110 <dd>{{ t->repo_dir->description }}</dd>
1111 {{ end }}
1112 {{ if srv->show_repo_owner }}
1113 <dt>Owner:</dt>
1114 <dd>{{ t->repo_dir->owner }}</dd>
1115 {{ end }}
1116 {{ if srv->show_repo_age }}
1117 <dt>Last Change:</dt>
1118 <dd>
1119 {{ render datetime(tp, t->repo_dir->age, TM_DIFF) }}
1120 </dd>
1121 {{ end }}
1122 {{ if srv->show_repo_cloneurl }}
1123 <dt>Clone URL:</dt>
1124 <dd><pre class="clone-url">{{ t->repo_dir->url }}</pre></dd>
1125 {{ end }}
1126 </dl>
1127 {{ render gotweb_render_briefs(tp) }}
1128 {{ render gotweb_render_tags(tp) }}
1129 {{ render gotweb_render_branches(tp, refs) }}
1130 {{ end }}
1132 {{ define gotweb_render_blame(struct template *tp) }}
1134 const struct got_error *err;
1135 struct request *c = tp->tp_arg;
1136 struct transport *t = c->t;
1137 struct querystring *qs = t->qs;
1138 struct repo_commit *rc = TAILQ_FIRST(&t->repo_commits);
1139 struct gotweb_url briefs_url, blob_url, raw_url;
1141 memset(&briefs_url, 0, sizeof(briefs_url));
1142 briefs_url.index_page = -1,
1143 briefs_url.page = -1,
1144 briefs_url.action = BRIEFS,
1145 briefs_url.path = qs->path,
1146 briefs_url.commit = qs->commit,
1147 briefs_url.folder = qs->folder,
1148 briefs_url.file = qs->file,
1150 memcpy(&blob_url, &briefs_url, sizeof(blob_url));
1151 blob_url.action = BLOB;
1153 memcpy(&raw_url, &briefs_url, sizeof(raw_url));
1154 raw_url.action = BLOBRAW;
1156 <header class="subtitle">
1157 <h2>Blame</h2>
1158 </header>
1159 <div id="blame_content">
1160 <div class="page_header_wrapper">
1161 <dl>
1162 <dt>Date:</dt>
1163 <dd>
1164 {{ render datetime(tp, rc->committer_time, TM_LONG) }}
1165 </dd>
1166 <dt>Message:</dt>
1167 <dd class="commit-msg">{{ rc->commit_msg }}</dd>
1168 <dt>Actions:</dt>
1169 <dd>
1170 <a href="{{ render gotweb_render_url(c, &briefs_url) }}">
1171 History
1172 </a>
1173 {{ " &mdash; " | unsafe }}
1174 <a href="{{ render gotweb_render_url(c, &blob_url) }}">
1175 Blob
1176 </a>
1177 {{ " &mdash; " | unsafe }}
1178 <a href="{{ render gotweb_render_url(c, &raw_url) }}">
1179 Raw File
1180 </a>
1181 </dd>
1182 </dl>
1183 </div>
1184 <hr />
1185 <pre id="blame">
1187 err = got_output_file_blame(c, &blame_line);
1188 if (err && err->code != GOT_ERR_CANCELLED)
1189 log_warnx("%s: got_output_file_blame: %s", __func__,
1190 err->msg);
1191 if (err)
1192 return (-1);
1194 </pre>
1195 </div>
1196 {{ end }}
1198 {{ define blame_line(struct template *tp, const char *line,
1199 struct blame_line *bline, int lprec, int lcur) }}
1201 struct request *c = tp->tp_arg;
1202 struct transport *t = c->t;
1203 struct repo_dir *repo_dir = t->repo_dir;
1204 char *committer, *s;
1205 struct gotweb_url url = {
1206 .action = DIFF,
1207 .index_page = -1,
1208 .page = -1,
1209 .path = repo_dir->name,
1210 .commit = bline->id_str,
1213 s = strchr(bline->committer, '<');
1214 committer = s ? s + 1 : bline->committer;
1216 s = strchr(committer, '@');
1217 if (s)
1218 *s = '\0';
1220 <div class="blame_line">
1221 <span class="blame_number">{{ printf "%*d ", lprec, lcur }}</span>
1222 <span class="blame_hash">
1223 <a href="{{ render gotweb_render_url(c, &url) }}">
1224 {{ printf "%.8s", bline->id_str }}
1225 </a>
1226 </span>
1227 {{" "}}
1228 <span class="blame_date">{{ bline->datebuf }}</span>
1229 {{" "}}
1230 <span class="blame_author">{{ printf "%.9s", committer }}</span>
1231 {{" "}}
1232 <span class="blame_code">{{ line }}</span>
1233 </div>
1234 {{ end }}
1236 {{ define gotweb_render_patch(struct template *tp) }}
1238 struct request *c = tp->tp_arg;
1239 struct transport *t = c->t;
1240 struct repo_commit *rc = TAILQ_FIRST(&t->repo_commits);
1241 struct tm tm;
1242 char buf[BUFSIZ], datebuf[64];
1243 size_t r;
1245 if (gmtime_r(&rc->committer_time, &tm) == NULL ||
1246 strftime(datebuf, sizeof(datebuf), "%a %b %d %T %Y UTC", &tm) == 0)
1247 return (-1);
1249 commit {{ rc->commit_id }} {{ "\n" }}
1250 from: {{ rc->author | unsafe }} {{ "\n" }}
1251 {{ if strcmp(rc->committer, rc->author) != 0 }}
1252 via: {{ rc->author | unsafe }} {{ "\n" }}
1253 {{ end }}
1254 date: {{ datebuf }} {{ "\n" }}
1255 {{ "\n" }}
1256 {{ rc->commit_msg | unsafe }} {{ "\n" }}
1258 if (template_flush(tp) == -1)
1259 return (-1);
1260 for (;;) {
1261 r = fread(buf, 1, sizeof(buf), t->fp);
1262 if (fcgi_write(c, buf, r) == -1 ||
1263 r != sizeof(buf))
1264 break;
1267 {{ end }}
1269 {{ define gotweb_render_rss(struct template *tp) }}
1271 struct request *c = tp->tp_arg;
1272 struct server *srv = c->srv;
1273 struct transport *t = c->t;
1274 struct repo_dir *repo_dir = t->repo_dir;
1275 struct repo_tag *rt;
1276 struct gotweb_url summary = {
1277 .action = SUMMARY,
1278 .index_page = -1,
1279 .page = -1,
1280 .path = repo_dir->name,
1283 <?xml version="1.0" encoding="UTF-8"?>
1284 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
1285 <channel>
1286 <title>Tags of {{ repo_dir->name }}</title>
1287 <link>
1288 <![CDATA[
1289 {{ render gotweb_render_absolute_url(c, &summary) }}
1290 ]]>
1291 </link>
1292 {{ if srv->show_repo_description }}
1293 <description>{{ repo_dir->description }}</description>
1294 {{ end }}
1295 {{ tailq-foreach rt &t->repo_tags entry }}
1296 {{ render rss_tag_item(tp, rt) }}
1297 {{ end }}
1298 </channel>
1299 </rss>
1300 {{ end }}
1302 {{ define rss_tag_item(struct template *tp, struct repo_tag *rt) }}
1304 struct request *c = tp->tp_arg;
1305 struct transport *t = c->t;
1306 struct repo_dir *repo_dir = t->repo_dir;
1307 struct tm tm;
1308 char rfc822[128];
1309 int r;
1310 char *tag_name = rt->tag_name;
1311 struct gotweb_url tag = {
1312 .action = TAG,
1313 .index_page = -1,
1314 .page = -1,
1315 .path = repo_dir->name,
1316 .commit = rt->commit_id,
1319 if (strncmp(tag_name, "refs/tags/", 10) == 0)
1320 tag_name += 10;
1322 if (gmtime_r(&rt->tagger_time, &tm) == NULL)
1323 return -1;
1324 r = strftime(rfc822, sizeof(rfc822), "%a, %d %b %Y %H:%M:%S GMT", &tm);
1325 if (r == 0)
1326 return 0;
1328 <item>
1329 <title>{{ repo_dir->name }} {{" "}} {{ tag_name }}</title>
1330 <link>
1331 <![CDATA[
1332 {{ render gotweb_render_absolute_url(c, &tag) }}
1333 ]]>
1334 </link>
1335 <description>
1336 <![CDATA[<pre>{{ rt->tag_commit }}</pre>]]>
1337 </description>
1338 {{ render rss_author(tp, rt->tagger) }}
1339 <guid isPermaLink="false">{{ rt->commit_id }}</guid>
1340 <pubDate>
1341 {{ rfc822 }}
1342 </pubDate>
1343 </item>
1344 {{ end }}
1346 {{ define rss_author(struct template *tp, char *author) }}
1348 char *t, *mail;
1350 /* what to do if the author name contains a paren? */
1351 if (strchr(author, '(') != NULL || strchr(author, ')') != NULL)
1352 return 0;
1354 t = strchr(author, '<');
1355 if (t == NULL)
1356 return 0;
1357 *t = '\0';
1358 mail = t+1;
1360 while (isspace((unsigned char)*--t))
1361 *t = '\0';
1363 t = strchr(mail, '>');
1364 if (t == NULL)
1365 return 0;
1366 *t = '\0';
1368 <author>
1369 {{ mail }} {{" "}} ({{ author }})
1370 </author>
1371 {{ end }}