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;
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 tmp = strchr(rc->commit_msg, '\n');
389 if (tmp)
390 *tmp = '\0';
391 !}
392 <div class='brief'>
393 <p class='brief_meta'>
394 <span class='briefs_age'>
395 {{ render datetime(tp, rc->committer_time, TM_DIFF) }}
396 </span>
397 {{" "}}
398 <span class="briefs_author">
399 {{ rc->committer }}
400 </span>
401 </p>
402 <p class="briefs_log">
403 <a href="{{ render gotweb_render_url(tp->tp_arg, &diff_url) }}">
404 {{ rc->commit_msg }}
405 </a>
406 {{ if rc->refs_str }}
407 {{ " " }} <span class="refs_str">({{ rc->refs_str }})</span>
408 {{ end }}
409 </a>
410 </p>
411 </div>
412 <div class="navs_wrapper">
413 <div class="navs">
414 <a href="{{ render gotweb_render_url(tp->tp_arg, &diff_url) }}">diff</a>
415 {{ " | " }}
416 <a href="{{ render gotweb_render_url(tp->tp_arg, &patch_url) }}">patch</a>
417 {{ " | " }}
418 <a href="{{ render gotweb_render_url(tp->tp_arg, &tree_url) }}">tree</a>
419 </div>
420 </div>
421 <hr />
422 {{ end }}
423 {{ render gotweb_render_more(tp, BRIEFS) }}
424 </div>
425 {{ end }}
427 {{ define gotweb_render_more(struct template *tp, int action) }}
428 {!
429 struct request *c = tp->tp_arg;
430 struct transport *t = c->t;
431 struct querystring *qs = t->qs;
432 struct gotweb_url more = {
433 .action = action,
434 .index_page = -1,
435 .path = qs->path,
436 .commit = t->more_id,
437 .headref = qs->headref,
438 .file = qs->file,
439 };
440 !}
441 {{ if t->more_id }}
442 <div id="np_wrapper">
443 <div id="nav_more">
444 <a href="{{ render gotweb_render_url(c, &more) }}">
445 More&nbsp;&darr;
446 </a>
447 </div>
448 </div>
449 {{ end }}
450 {{ end }}
452 {{ define gotweb_render_navs(struct template *tp) }}
453 {!
454 struct request *c = tp->tp_arg;
455 struct transport *t = c->t;
456 struct gotweb_url prev, next;
457 int have_prev, have_next;
459 gotweb_get_navs(c, &prev, &have_prev, &next, &have_next);
460 !}
461 <div id="np_wrapper">
462 <div id="nav_prev">
463 {{ if have_prev }}
464 <a href="{{ render gotweb_render_url(c, &prev) }}">
465 Previous
466 </a>
467 {{ end }}
468 </div>
469 <div id="nav_next">
470 {{ if have_next }}
471 <a href="{{ render gotweb_render_url(c, &next) }}">
472 Next
473 </a>
474 {{ end }}
475 </div>
476 </div>
477 {{ finally }}
478 {!
479 free(t->next_id);
480 t->next_id = NULL;
481 free(t->prev_id);
482 t->prev_id = NULL;
483 !}
484 {{ end }}
486 {{ define gotweb_render_commits(struct template *tp) }}
487 {!
488 struct request *c = tp->tp_arg;
489 struct transport *t = c->t;
490 struct repo_dir *repo_dir = t->repo_dir;
491 struct repo_commit *rc;
492 struct gotweb_url diff, patch, tree;
494 diff = (struct gotweb_url){
495 .action = DIFF,
496 .index_page = -1,
497 .page = -1,
498 .path = repo_dir->name,
499 };
500 patch = (struct gotweb_url){
501 .action = PATCH,
502 .index_page = -1,
503 .page = -1,
504 .path = repo_dir->name,
505 };
506 tree = (struct gotweb_url){
507 .action = TREE,
508 .index_page = -1,
509 .page = -1,
510 .path = repo_dir->name,
511 };
512 !}
513 <header class="subtitle">
514 <h2>Commits</h2>
515 </header>
516 <div class="commits_content">
517 {{ tailq-foreach rc &t->repo_commits entry }}
518 {!
519 diff.commit = rc->commit_id;
520 patch.commit = rc->commit_id;
521 tree.commit = rc->commit_id;
522 !}
523 <div class="page_header_wrapper">
524 <dl>
525 <dt>Commit:</dt>
526 <dd><code class="commit-id">{{ rc->commit_id }}</code></dd>
527 <dt>From:</dt>
528 <dd>{{ rc->author }}</dd>
529 {{ if strcmp(rc->committer, rc->author) != 0 }}
530 <dt>Via:</dt>
531 <dd>{{ rc->committer }}</dd>
532 {{ end }}
533 <dt>Date:</dt>
534 <dd>
535 {{ render datetime(tp, rc->committer_time, TM_LONG) }}
536 </dd>
537 </dl>
538 </div>
539 <hr />
540 <div class="commit">
541 {{ "\n" }}
542 {{ rc->commit_msg }}
543 </div>
544 <div class="navs_wrapper">
545 <div class="navs">
546 <a href="{{ render gotweb_render_url(c, &diff) }}">diff</a>
547 {{ " | " }}
548 <a href="{{ render gotweb_render_url(c, &patch) }}">patch</a>
549 {{ " | " }}
550 <a href="{{ render gotweb_render_url(c, &tree) }}">tree</a>
551 </div>
552 </div>
553 <hr />
554 {{ end }}
555 {{ render gotweb_render_more(tp, COMMITS) }}
556 </div>
557 {{ end }}
559 {{ define gotweb_render_blob(struct template *tp) }}
560 {!
561 struct request *c = tp->tp_arg;
562 struct transport *t = c->t;
563 struct querystring *qs = t->qs;
564 struct got_blob_object *blob = t->blob;
565 struct repo_commit *rc = TAILQ_FIRST(&t->repo_commits);
566 struct gotweb_url briefs_url, blame_url, raw_url;
568 memset(&briefs_url, 0, sizeof(briefs_url));
569 briefs_url.index_page = -1,
570 briefs_url.page = -1,
571 briefs_url.action = BRIEFS,
572 briefs_url.path = qs->path,
573 briefs_url.commit = qs->commit,
574 briefs_url.folder = qs->folder,
575 briefs_url.file = qs->file,
577 memcpy(&blame_url, &briefs_url, sizeof(blame_url));
578 blame_url.action = BLAME;
580 memcpy(&raw_url, &briefs_url, sizeof(raw_url));
581 raw_url.action = BLOBRAW;
582 !}
583 <header class="subtitle">
584 <h2>Blob</h2>
585 </header>
586 <div id="blob_content">
587 <div class="page_header_wrapper">
588 <dl>
589 <dt>Date:</dt>
590 <dd>
591 {{ render datetime(tp, rc->committer_time, TM_LONG) }}
592 </dd>
593 <dt>Message:</dt>
594 <dd class="commit-msg">{{ rc->commit_msg }}</dd>
595 <dt>Actions:</dt>
596 <dd>
597 <a href="{{ render gotweb_render_url(c, &briefs_url) }}">
598 History
599 </a>
600 {{" | "}}
601 <a href="{{ render gotweb_render_url(c, &blame_url) }}">
602 Blame
603 </a>
604 {{" | "}}
605 <a href="{{ render gotweb_render_url(c, &raw_url) }}">
606 Raw File
607 </a>
608 </dd>
609 </dl>
610 </div>
611 <hr />
612 <div id="blob">
613 <pre>
614 {{ render got_output_blob_by_lines(tp, blob, gotweb_render_blob_line) }}
615 </pre>
616 </div>
617 </div>
618 {{ end }}
620 {{ define gotweb_render_blob_line(struct template *tp, const char *line,
621 size_t no) }}
622 {!
623 char lineno[16];
624 int r;
626 r = snprintf(lineno, sizeof(lineno), "%zu", no);
627 if (r < 0 || (size_t)r >= sizeof(lineno))
628 return -1;
629 !}
630 <div class="blob_line" id="line{{ lineno }}">
631 <a href="#line{{ lineno }}">{{ lineno }}</a>
632 {{" "}}
633 <span class="blob_code">{{ line }}</span>
634 </div>
635 {{ end }}
637 {{ define gotweb_render_tree(struct template *tp) }}
638 {!
639 struct request *c = tp->tp_arg;
640 struct transport *t = c->t;
641 struct repo_commit *rc = TAILQ_FIRST(&t->repo_commits);
642 !}
643 <header class='subtitle'>
644 <h2>Tree</h2>
645 </header>
646 <div id="tree_content">
647 <div class="page_header_wrapper">
648 <dl>
649 <dt>Tree:</dt>
650 <dd><code class="commit-id">{{ rc->tree_id }}</code></dd>
651 <dt>Date:</dt>
652 <dd>
653 {{ render datetime(tp, rc->committer_time, TM_LONG) }}
654 </dd>
655 <dt>Message:</dt>
656 <dd class="commit-msg">{{ rc->commit_msg }}</dd>
657 </dl>
658 </div>
659 <hr />
660 <table id="tree">
661 {{ render got_output_repo_tree(c, gotweb_render_tree_item) }}
662 </table>
663 </div>
664 {{ end }}
666 {{ define gotweb_render_tree_item(struct template *tp,
667 struct got_tree_entry *te) }}
668 {!
669 struct request *c = tp->tp_arg;
670 struct transport *t = c->t;
671 struct querystring *qs = t->qs;
672 struct repo_commit *rc = TAILQ_FIRST(&t->repo_commits);
673 const char *modestr = "";
674 const char *name;
675 const char *folder;
676 char *dir = NULL;
677 mode_t mode;
678 struct gotweb_url url = {
679 .index_page = -1,
680 .page = -1,
681 .commit = rc->commit_id,
682 .path = qs->path,
683 };
685 name = got_tree_entry_get_name(te);
686 mode = got_tree_entry_get_mode(te);
688 folder = qs->folder ? qs->folder : "";
689 if (S_ISDIR(mode)) {
690 if (asprintf(&dir, "%s/%s", folder, name) == -1)
691 return (-1);
693 url.action = TREE;
694 url.folder = dir;
695 } else {
696 url.action = BLOB;
697 url.folder = folder;
698 url.file = name;
701 if (got_object_tree_entry_is_submodule(te))
702 modestr = "$";
703 else if (S_ISLNK(mode))
704 modestr = "@";
705 else if (S_ISDIR(mode))
706 modestr = "/";
707 else if (mode & S_IXUSR)
708 modestr = "*";
709 !}
710 <tr class="tree_wrapper">
711 {{ if S_ISDIR(mode) }}
712 <td class="tree_line" colspan=2>
713 <a href="{{ render gotweb_render_url(c, &url) }}">
714 {{ name }}{{ modestr }}
715 </a>
716 </td>
717 {{ else }}
718 <td class="tree_line">
719 <a href="{{ render gotweb_render_url(c, &url) }}">
720 {{ name }}{{ modestr }}
721 </a>
722 </td>
723 <td class="tree_line_blank">
724 {! url.action = COMMITS; !}
725 <a href="{{ render gotweb_render_url(c, &url) }}">
726 commits
727 </a>
728 {{ " | " }}
729 {! url.action = BLAME; !}
730 <a href="{{ render gotweb_render_url(c, &url) }}">
731 blame
732 </a>
733 </td>
734 {{ end }}
735 </tr>
736 {{ finally }}
737 {!
738 free(dir);
739 !}
740 {{ end }}
742 {{ define gotweb_render_tags(struct template *tp) }}
743 {!
744 struct request *c = tp->tp_arg;
745 struct transport *t = c->t;
746 struct querystring *qs = t->qs;
747 struct repo_tag *rt;
748 int commit_found;
750 commit_found = qs->commit == NULL;
751 !}
752 <header class='subtitle'>
753 <h2>Tags</h2>
754 </header>
755 <div id="tags_content">
756 {{ if t->tag_count == 0 }}
757 <div id="err_content">
758 This repository contains no tags
759 </div>
760 {{ else }}
761 {{ tailq-foreach rt &t->repo_tags entry }}
762 {{ if commit_found || !strcmp(qs->commit, rt->commit_id) }}
763 {! commit_found = 1; !}
764 {{ render tag_item(tp, rt) }}
765 {{ end }}
766 {{ end }}
767 {{ if t->next_id || t->prev_id }}
768 {! qs->action = TAGS; !}
769 {{ render gotweb_render_navs(tp) }}
770 {{ end }}
771 {{ end }}
772 </div>
773 {{ end }}
775 {{ define tag_item(struct template *tp, struct repo_tag *rt) }}
776 {!
777 struct request *c = tp->tp_arg;
778 struct transport *t = c->t;
779 struct repo_dir *repo_dir = t->repo_dir;
780 char *tag_name = rt->tag_name;
781 char *msg = rt->tag_commit;
782 char *nl;
783 struct gotweb_url url = {
784 .action = TAG,
785 .index_page = -1,
786 .page = -1,
787 .path = repo_dir->name,
788 .commit = rt->commit_id,
789 };
791 if (strncmp(tag_name, "refs/tags/", 10) == 0)
792 tag_name += 10;
794 if (msg) {
795 nl = strchr(msg, '\n');
796 if (nl)
797 *nl = '\0';
799 !}
800 <div class="tag_age">
801 {{ render datetime(tp, rt->tagger_time, TM_DIFF) }}
802 </div>
803 <div class="tag_name">{{ tag_name }}</div>
804 <div class="tag_log">
805 <a href="{{ render gotweb_render_url(c, &url) }}">
806 {{ msg }}
807 </a>
808 </div>
809 <div class="navs_wrapper">
810 <div class="navs">
811 <a href="{{ render gotweb_render_url(c, &url) }}">tag</a>
812 {{ " | " }}
813 {! url.action = BRIEFS; !}
814 <a href="{{ render gotweb_render_url(c, &url) }}">commit briefs</a>
815 {{ " | " }}
816 {! url.action = COMMITS; !}
817 <a href="{{ render gotweb_render_url(c, &url) }}">commits</a>
818 </div>
819 </div>
820 <hr />
821 {{ end }}
823 {{ define gotweb_render_tag(struct template *tp) }}
824 {!
825 struct request *c = tp->tp_arg;
826 struct transport *t = c->t;
827 struct repo_tag *rt;
828 const char *tag_name;
830 rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
831 tag_name = rt->tag_name;
833 if (strncmp(tag_name, "refs/", 5) == 0)
834 tag_name += 5;
835 !}
836 <header class="subtitle">
837 <h2>Tag</h2>
838 </header>
839 <div id="tags_content">
840 <div class="page_header_wrapper">
841 <dl>
842 <dt>Commit:</dt>
843 <dd>
844 <code class="commit-id">{{ rt->commit_id }}</code>
845 {{ " " }}
846 <span class="refs_str">({{ tag_name }})</span>
847 </dd>
848 <dt>Tagger:</dt>
849 <dd>{{ rt->tagger }}</dd>
850 <dt>Date:</dt>
851 <dd>
852 {{ render datetime(tp, rt->tagger_time, TM_LONG)}}
853 </dd>
854 <dt>Message:</dt>
855 <dd class="commit-msg">{{ rt->commit_msg }}</dd>
856 </dl>
857 <hr />
858 <pre id="tag_commit">
859 {{ rt->tag_commit }}
860 </pre>
861 </div>
862 </div>
863 {{ end }}
865 {{ define gotweb_render_diff(struct template *tp) }}
866 {!
867 struct request *c = tp->tp_arg;
868 struct transport *t = c->t;
869 struct querystring *qs = t->qs;
870 FILE *fp = t->fp;
871 struct repo_commit *rc = TAILQ_FIRST(&t->repo_commits);
872 char *line = NULL;
873 size_t linesize = 0;
874 ssize_t linelen;
875 struct gotweb_url patch_url, tree_url = {
876 .action = TREE,
877 .index_page = -1,
878 .page = -1,
879 .path = qs->path,
880 .commit = rc->commit_id,
881 };
883 memcpy(&patch_url, &tree_url, sizeof(patch_url));
884 patch_url.action = PATCH;
885 !}
886 <header class="subtitle">
887 <h2>Commit Diff</h2>
888 </header>
889 <div id="diff_content">
890 <div class="page_header_wrapper">
891 <dl>
892 <dt>Commit:</dt>
893 <dd><code class="commit-id">{{ rc->commit_id }}</code></dd>
894 <dt>From:</dt>
895 <dd>{{ rc->author }}</dd>
896 {{ if strcmp(rc->committer, rc->author) != 0 }}
897 <dt>Via:</dt>
898 <dd>{{ rc->committer }}</dd>
899 {{ end }}
900 <dt>Date:</dt>
901 <dd>
902 {{ render datetime(tp, rc->committer_time, TM_LONG) }}
903 </dd>
904 <dt>Message:</dt>
905 <dd class="commit-msg">{{ rc->commit_msg }}</dd>
906 <dt>Actions:</dt>
907 <dd>
908 <a href="{{ render gotweb_render_url(c, &patch_url) }}">
909 Patch
910 </a>
911 {{" | "}}
912 <a href="{{ render gotweb_render_url(c, &tree_url) }}">
913 Tree
914 </a>
915 </dd>
916 </dl>
917 </div>
918 <hr />
919 <pre id="diff">
920 {{ while (linelen = getline(&line, &linesize, fp)) != -1 }}
921 {{ render diff_line(tp, line) }}
922 {{ end }}
923 </pre>
924 </div>
925 {{ finally }}
926 {! free(line); !}
927 {{ end }}
929 {{ define diff_line(struct template *tp, char *line )}}
930 {!
931 const char *color = NULL;
932 char *nl;
934 if (!strncmp(line, "-", 1))
935 color = "diff_minus";
936 else if (!strncmp(line, "+", 1))
937 color = "diff_plus";
938 else if (!strncmp(line, "@@", 2))
939 color = "diff_chunk_header";
940 else if (!strncmp(line, "commit +", 8) ||
941 !strncmp(line, "commit -", 8) ||
942 !strncmp(line, "blob +", 6) ||
943 !strncmp(line, "blob -", 6) ||
944 !strncmp(line, "file +", 6) ||
945 !strncmp(line, "file -", 6))
946 color = "diff_meta";
947 else if (!strncmp(line, "from:", 5) || !strncmp(line, "via:", 4))
948 color = "diff_author";
949 else if (!strncmp(line, "date:", 5))
950 color = "diff_date";
952 nl = strchr(line, '\n');
953 if (nl)
954 *nl = '\0';
955 !}
956 <span class="diff_line {{ color }}">{{ line }}</span>{{"\n"}}
957 {{ end }}
959 {{ define gotweb_render_branches(struct template *tp,
960 struct got_reflist_head *refs) }}
961 {!
962 struct got_reflist_entry *re;
963 !}
964 <header class='subtitle'>
965 <h2>Branches</h2>
966 </header>
967 <div id="branches_content">
968 {{ tailq-foreach re refs entry }}
969 {{ if !got_ref_is_symbolic(re->ref) }}
970 {{ render branch(tp, re) }}
971 {{ end }}
972 {{ end }}
973 </div>
974 {{ end }}
976 {{ define branch(struct template *tp, struct got_reflist_entry *re) }}
977 {!
978 const struct got_error *err;
979 struct request *c = tp->tp_arg;
980 struct querystring *qs = c->t->qs;
981 const char *refname;
982 time_t age;
983 struct gotweb_url url = {
984 .action = SUMMARY,
985 .index_page = -1,
986 .page = -1,
987 .path = qs->path,
988 };
990 refname = got_ref_get_name(re->ref);
992 err = got_get_repo_age(&age, c, refname);
993 if (err) {
994 log_warnx("%s: %s", __func__, err->msg);
995 return -1;
998 if (strncmp(refname, "refs/heads/", 11) == 0)
999 refname += 11;
1001 url.headref = refname;
1003 <section class="branches_wrapper">
1004 <div class="branches_age">
1005 {{ render datetime(tp, age, TM_DIFF) }}
1006 </div>
1007 <div class="branch">
1008 <a href="{{ render gotweb_render_url(c, &url) }}">{{ refname }}</a>
1009 </div>
1010 <div class="navs_wrapper">
1011 <div class="navs">
1012 <a href="{{ render gotweb_render_url(c, &url) }}">summary</a>
1013 {{" | "}}
1014 {! url.action = BRIEFS; !}
1015 <a href="{{ render gotweb_render_url(c, &url) }}">commit briefs</a>
1016 {{" | "}}
1017 {! url.action = COMMITS; !}
1018 <a href="{{ render gotweb_render_url(c, &url) }}">commits</a>
1019 </div>
1020 </div>
1021 <hr />
1022 </section>
1023 {{ end }}
1025 {{ define gotweb_render_summary(struct template *tp) }}
1027 struct request *c = tp->tp_arg;
1028 struct server *srv = c->srv;
1029 struct transport *t = c->t;
1030 struct got_reflist_head *refs = &t->refs;
1032 <dl id="summary_wrapper" class="page_header_wrapper">
1033 {{ if srv->show_repo_description }}
1034 <dt>Description:</dt>
1035 <dd>{{ t->repo_dir->description }}</dd>
1036 {{ end }}
1037 {{ if srv->show_repo_owner }}
1038 <dt>Owner:</dt>
1039 <dd>{{ t->repo_dir->owner }}</dd>
1040 {{ end }}
1041 {{ if srv->show_repo_age }}
1042 <dt>Last Change:</dt>
1043 <dd>
1044 {{ render datetime(tp, t->repo_dir->age, TM_DIFF) }}
1045 </dd>
1046 {{ end }}
1047 {{ if srv->show_repo_cloneurl }}
1048 <dt>Clone URL:</dt>
1049 <dd><pre class="clone-url">{{ t->repo_dir->url }}</pre></dd>
1050 {{ end }}
1051 </dl>
1052 {{ render gotweb_render_briefs(tp) }}
1053 {{ render gotweb_render_tags(tp) }}
1054 {{ render gotweb_render_branches(tp, refs) }}
1055 {{ end }}
1057 {{ define gotweb_render_blame(struct template *tp) }}
1059 const struct got_error *err;
1060 struct request *c = tp->tp_arg;
1061 struct transport *t = c->t;
1062 struct querystring *qs = t->qs;
1063 struct repo_commit *rc = TAILQ_FIRST(&t->repo_commits);
1064 struct gotweb_url briefs_url, blob_url, raw_url;
1066 memset(&briefs_url, 0, sizeof(briefs_url));
1067 briefs_url.index_page = -1,
1068 briefs_url.page = -1,
1069 briefs_url.action = BRIEFS,
1070 briefs_url.path = qs->path,
1071 briefs_url.commit = qs->commit,
1072 briefs_url.folder = qs->folder,
1073 briefs_url.file = qs->file,
1075 memcpy(&blob_url, &briefs_url, sizeof(blob_url));
1076 blob_url.action = BLOB;
1078 memcpy(&raw_url, &briefs_url, sizeof(raw_url));
1079 raw_url.action = BLOBRAW;
1081 <header class="subtitle">
1082 <h2>Blame</h2>
1083 </header>
1084 <div id="blame_content">
1085 <div class="page_header_wrapper">
1086 <dl>
1087 <dt>Date:</dt>
1088 <dd>
1089 {{ render datetime(tp, rc->committer_time, TM_LONG) }}
1090 </dd>
1091 <dt>Message:</dt>
1092 <dd class="commit-msg">{{ rc->commit_msg }}</dd>
1093 <dt>Actions:</dt>
1094 <dd>
1095 <a href="{{ render gotweb_render_url(c, &briefs_url) }}">
1096 History
1097 </a>
1098 {{" | "}}
1099 <a href="{{ render gotweb_render_url(c, &blob_url) }}">
1100 Blob
1101 </a>
1102 {{" | "}}
1103 <a href="{{ render gotweb_render_url(c, &raw_url) }}">
1104 Raw File
1105 </a>
1106 </dd>
1107 </dl>
1108 </div>
1109 <hr />
1110 <pre id="blame">
1112 err = got_output_file_blame(c, &blame_line);
1113 if (err && err->code != GOT_ERR_CANCELLED)
1114 log_warnx("%s: got_output_file_blame: %s", __func__,
1115 err->msg);
1116 if (err)
1117 return (-1);
1119 </pre>
1120 </div>
1121 {{ end }}
1123 {{ define blame_line(struct template *tp, const char *line,
1124 struct blame_line *bline, int lprec, int lcur) }}
1126 struct request *c = tp->tp_arg;
1127 struct transport *t = c->t;
1128 struct repo_dir *repo_dir = t->repo_dir;
1129 char *committer, *s;
1130 struct gotweb_url url = {
1131 .action = DIFF,
1132 .index_page = -1,
1133 .page = -1,
1134 .path = repo_dir->name,
1135 .commit = bline->id_str,
1138 s = strchr(bline->committer, '<');
1139 committer = s ? s + 1 : bline->committer;
1141 s = strchr(committer, '@');
1142 if (s)
1143 *s = '\0';
1145 <div class="blame_line">
1146 <span class="blame_number">{{ printf "%*d ", lprec, lcur }}</span>
1147 <span class="blame_hash">
1148 <a href="{{ render gotweb_render_url(c, &url) }}">
1149 {{ printf "%.8s", bline->id_str }}
1150 </a>
1151 </span>
1152 {{" "}}
1153 <span class="blame_date">{{ bline->datebuf }}</span>
1154 {{" "}}
1155 <span class="blame_author">{{ printf "%.9s", committer }}</span>
1156 {{" "}}
1157 <span class="blame_code">{{ line }}</span>
1158 </div>
1159 {{ end }}
1161 {{ define gotweb_render_patch(struct template *tp) }}
1163 struct request *c = tp->tp_arg;
1164 struct transport *t = c->t;
1165 struct repo_commit *rc = TAILQ_FIRST(&t->repo_commits);
1166 struct tm tm;
1167 char buf[BUFSIZ], datebuf[64];
1168 size_t r;
1170 if (gmtime_r(&rc->committer_time, &tm) == NULL ||
1171 strftime(datebuf, sizeof(datebuf), "%a %b %d %T %Y UTC", &tm) == 0)
1172 return (-1);
1174 commit {{ rc->commit_id }} {{ "\n" }}
1175 from: {{ rc->author | unsafe }} {{ "\n" }}
1176 {{ if strcmp(rc->committer, rc->author) != 0 }}
1177 via: {{ rc->committer | unsafe }} {{ "\n" }}
1178 {{ end }}
1179 date: {{ datebuf }} {{ "\n" }}
1180 {{ "\n" }}
1181 {{ rc->commit_msg | unsafe }} {{ "\n" }}
1183 if (template_flush(tp) == -1)
1184 return (-1);
1185 for (;;) {
1186 r = fread(buf, 1, sizeof(buf), t->fp);
1187 if (fcgi_write(c, buf, r) == -1 ||
1188 r != sizeof(buf))
1189 break;
1192 {{ end }}
1194 {{ define gotweb_render_rss(struct template *tp) }}
1196 struct request *c = tp->tp_arg;
1197 struct server *srv = c->srv;
1198 struct transport *t = c->t;
1199 struct repo_dir *repo_dir = t->repo_dir;
1200 struct repo_tag *rt;
1201 struct gotweb_url summary = {
1202 .action = SUMMARY,
1203 .index_page = -1,
1204 .page = -1,
1205 .path = repo_dir->name,
1208 <?xml version="1.0" encoding="UTF-8"?>
1209 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
1210 <channel>
1211 <title>Tags of {{ repo_dir->name }}</title>
1212 <link>
1213 <![CDATA[
1214 {{ render gotweb_render_absolute_url(c, &summary) }}
1215 ]]>
1216 </link>
1217 {{ if srv->show_repo_description }}
1218 <description>{{ repo_dir->description }}</description>
1219 {{ end }}
1220 {{ tailq-foreach rt &t->repo_tags entry }}
1221 {{ render rss_tag_item(tp, rt) }}
1222 {{ end }}
1223 </channel>
1224 </rss>
1225 {{ end }}
1227 {{ define rss_tag_item(struct template *tp, struct repo_tag *rt) }}
1229 struct request *c = tp->tp_arg;
1230 struct transport *t = c->t;
1231 struct repo_dir *repo_dir = t->repo_dir;
1232 struct tm tm;
1233 char rfc822[128];
1234 int r;
1235 char *tag_name = rt->tag_name;
1236 struct gotweb_url tag = {
1237 .action = TAG,
1238 .index_page = -1,
1239 .page = -1,
1240 .path = repo_dir->name,
1241 .commit = rt->commit_id,
1244 if (strncmp(tag_name, "refs/tags/", 10) == 0)
1245 tag_name += 10;
1247 if (gmtime_r(&rt->tagger_time, &tm) == NULL)
1248 return -1;
1249 r = strftime(rfc822, sizeof(rfc822), "%a, %d %b %Y %H:%M:%S GMT", &tm);
1250 if (r == 0)
1251 return 0;
1253 <item>
1254 <title>{{ repo_dir->name }} {{" "}} {{ tag_name }}</title>
1255 <link>
1256 <![CDATA[
1257 {{ render gotweb_render_absolute_url(c, &tag) }}
1258 ]]>
1259 </link>
1260 <description>
1261 <![CDATA[<pre>{{ rt->tag_commit }}</pre>]]>
1262 </description>
1263 {{ render rss_author(tp, rt->tagger) }}
1264 <guid isPermaLink="false">{{ rt->commit_id }}</guid>
1265 <pubDate>
1266 {{ rfc822 }}
1267 </pubDate>
1268 </item>
1269 {{ end }}
1271 {{ define rss_author(struct template *tp, char *author) }}
1273 char *t, *mail;
1275 /* what to do if the author name contains a paren? */
1276 if (strchr(author, '(') != NULL || strchr(author, ')') != NULL)
1277 return 0;
1279 t = strchr(author, '<');
1280 if (t == NULL)
1281 return 0;
1282 *t = '\0';
1283 mail = t+1;
1285 while (isspace((unsigned char)*--t))
1286 *t = '\0';
1288 t = strchr(mail, '>');
1289 if (t == NULL)
1290 return 0;
1291 *t = '\0';
1293 <author>
1294 {{ mail }} {{" "}} ({{ author }})
1295 </author>
1296 {{ end }}