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 const char *action = "tree";
110 char *t, *s = NULL, *dir = NULL;
111 char ch;
113 memset(&url, 0, sizeof(url));
114 url.index_page = -1;
115 url.page = -1;
116 url.action = TREE;
117 url.path = qs->path;
118 url.commit = qs->commit;
120 if (qs->action != TREE && qs->action != BLOB) {
121 action = gotweb_action_name(qs->action);
122 url.action = qs->action;
125 if (folder && *folder != '\0') {
126 while (*folder == '/')
127 folder++;
128 dir = strdup(folder);
129 if (dir == NULL)
130 return (-1);
131 s = dir;
133 !}
134 {{ " / " }}
135 <a href="{{ render gotweb_render_url(c, &url) }}">{{ action }}</a>
136 {{ " / " }}
137 {{ if dir }}
138 {{ while (s = nextsep(s, &t)) != NULL }}
139 {!
140 ch = *s;
141 *s = '\0';
142 url.folder = dir;
143 !}
145 <a href="{{ render gotweb_render_url(c, &url) }}">
146 {{ t }}
147 </a>
148 {{ " / " }}
150 {! *s = ch; !}
151 {{ end }}
152 {{ end }}
154 {{ if qs->file }}
155 {{ qs->file }}
156 {{ end}}
158 {{ finally }}
159 {! free(dir); !}
160 {{ end }}
162 {{ define gotweb_render_page(struct template *tp,
163 int (*body)(struct template *)) }}
164 {!
165 struct request *c = tp->tp_arg;
166 struct server *srv = c->srv;
167 struct querystring *qs = c->t->qs;
168 struct gotweb_url u_path;
169 const char *prfx = c->document_uri;
170 const char *css = srv->custom_css;
172 memset(&u_path, 0, sizeof(u_path));
173 u_path.index_page = -1;
174 u_path.page = -1;
175 u_path.action = SUMMARY;
176 !}
177 <!doctype html>
178 <html>
179 <head>
180 <meta charset="utf-8" />
181 <title>{{ srv->site_name }}</title>
182 <meta name="viewport" content="initial-scale=1.0" />
183 <meta name="msapplication-TileColor" content="#da532c" />
184 <meta name="theme-color" content="#ffffff"/>
185 <link rel="apple-touch-icon" sizes="180x180" href="{{ prfx }}apple-touch-icon.png" />
186 <link rel="icon" type="image/png" sizes="32x32" href="{{ prfx }}favicon-32x32.png" />
187 <link rel="icon" type="image/png" sizes="16x16" href="{{ prfx }}favicon-16x16.png" />
188 <link rel="manifest" href="{{ prfx }}site.webmanifest"/>
189 <link rel="mask-icon" href="{{ prfx }}safari-pinned-tab.svg" />
190 <link rel="stylesheet" type="text/css" href="{{ prfx }}{{ css }}" />
191 </head>
192 <body>
193 <header id="header">
194 <div id="got_link">
195 <a href="{{ srv->logo_url }}" target="_blank">
196 <img src="{{ prfx }}{{ srv->logo }}" />
197 </a>
198 </div>
199 </header>
200 <nav id="site_path">
201 <div id="site_link">
202 <a href="?index_page={{ printf "%d", qs->index_page }}">
203 {{ srv->site_link }}
204 </a>
205 {{ if qs->path }}
206 {! u_path.path = qs->path; !}
207 {{ " / " }}
208 <a href="{{ render gotweb_render_url(tp->tp_arg, &u_path)}}">
209 {{ qs->path }}
210 </a>
211 {{ end }}
212 {{ if qs->action == SUMMARY || qs->action == DIFF ||
213 qs->action == TAG || qs->action == TAGS }}
214 {{ " / " }}{{ gotweb_action_name(qs->action) }}
215 {{ else if qs->action != INDEX}}
216 {{ render breadcumbs(tp) }}
217 {{ end }}
218 </div>
219 </nav>
220 <main>
221 {{ render body(tp) }}
222 </main>
223 <footer id="site_owner_wrapper">
224 <p id="site_owner">
225 {{ if srv->show_site_owner }}
226 {{ srv->site_owner }}
227 {{ end }}
228 </p>
229 </footer>
230 </body>
231 </html>
232 {{ end }}
234 {{ define gotweb_render_error(struct template *tp) }}
235 {!
236 struct request *c = tp->tp_arg;
237 struct transport *t = c->t;
238 !}
239 <div id="err_content">
240 {{ if t->error }}
241 {{ t->error->msg }}
242 {{ else }}
243 See daemon logs for details
244 {{ end }}
245 </div>
246 {{ end }}
248 {{ define gotweb_render_repo_table_hdr(struct template *tp) }}
249 {!
250 struct request *c = tp->tp_arg;
251 struct server *srv = c->srv;
252 !}
253 <div id="index_header">
254 <div class="index_project">
255 Project
256 </div>
257 {{ if srv->show_repo_description }}
258 <div class="index_project_description">
259 Description
260 </div>
261 {{ end }}
262 {{ if srv->show_repo_owner }}
263 <div class="index_project_owner">
264 Owner
265 </div>
266 {{ end }}
267 {{ if srv->show_repo_age }}
268 <div class="index_project_age">
269 Last Change
270 </div>
271 {{ end }}
272 </div>
273 {{ end }}
275 {{ define gotweb_render_repo_fragment(struct template *tp, struct repo_dir *repo_dir) }}
276 {!
277 struct request *c = tp->tp_arg;
278 struct server *srv = c->srv;
279 struct gotweb_url summary = {
280 .action = SUMMARY,
281 .index_page = -1,
282 .page = -1,
283 .path = repo_dir->name,
284 }, briefs = {
285 .action = BRIEFS,
286 .index_page = -1,
287 .page = -1,
288 .path = repo_dir->name,
289 }, commits = {
290 .action = COMMITS,
291 .index_page = -1,
292 .page = -1,
293 .path = repo_dir->name,
294 }, tags = {
295 .action = TAGS,
296 .index_page = -1,
297 .page = -1,
298 .path = repo_dir->name,
299 }, tree = {
300 .action = TREE,
301 .index_page = -1,
302 .page = -1,
303 .path = repo_dir->name,
304 }, rss = {
305 .action = RSS,
306 .index_page = -1,
307 .page = -1,
308 .path = repo_dir->name,
309 };
310 !}
311 <div class="index_wrapper">
312 <div class="index_project">
313 <a href="{{ render gotweb_render_url(tp->tp_arg, &summary) }}">{{ repo_dir->name }}</a>
314 </div>
315 {{ if srv->show_repo_description }}
316 <div class="index_project_description">
317 {{ repo_dir->description }}
318 </div>
319 {{ end }}
320 {{ if srv->show_repo_owner }}
321 <div class="index_project_owner">
322 {{ repo_dir->owner }}
323 </div>
324 {{ end }}
325 {{ if srv->show_repo_age }}
326 <div class="index_project_age">
327 {{ render datetime(tp, repo_dir->age, TM_DIFF) }}
328 </div>
329 {{ end }}
330 <div class="navs_wrapper">
331 <div class="navs">
332 <a href="{{ render gotweb_render_url(tp->tp_arg, &summary) }}">summary</a>
333 {{ " | " }}
334 <a href="{{ render gotweb_render_url(tp->tp_arg, &briefs) }}">briefs</a>
335 {{ " | " }}
336 <a href="{{ render gotweb_render_url(tp->tp_arg, &commits) }}">commits</a>
337 {{ " | " }}
338 <a href="{{ render gotweb_render_url(tp->tp_arg, &tags) }}">tags</a>
339 {{ " | " }}
340 <a href="{{ render gotweb_render_url(tp->tp_arg, &tree) }}">tree</a>
341 {{ " | " }}
342 <a href="{{ render gotweb_render_url(tp->tp_arg, &rss) }}">rss</a>
343 </div>
344 <hr />
345 </div>
346 </div>
347 {{ end }}
349 {{ define gotweb_render_briefs(struct template *tp) }}
350 {!
351 struct request *c = tp->tp_arg;
352 struct transport *t = c->t;
353 struct querystring *qs = c->t->qs;
354 struct repo_commit *rc;
355 struct repo_dir *repo_dir = t->repo_dir;
356 struct gotweb_url diff_url, patch_url, tree_url;
357 char *tmp;
359 diff_url = (struct gotweb_url){
360 .action = DIFF,
361 .index_page = -1,
362 .page = -1,
363 .path = repo_dir->name,
364 .headref = qs->headref,
365 };
366 patch_url = (struct gotweb_url){
367 .action = PATCH,
368 .index_page = -1,
369 .page = -1,
370 .path = repo_dir->name,
371 .headref = qs->headref,
372 };
373 tree_url = (struct gotweb_url){
374 .action = TREE,
375 .index_page = -1,
376 .page = -1,
377 .path = repo_dir->name,
378 .headref = qs->headref,
379 };
380 !}
381 <header class='subtitle'>
382 <h2>Commit Briefs</h2>
383 </header>
384 <div id="briefs_content">
385 {{ tailq-foreach rc &t->repo_commits entry }}
386 {!
387 diff_url.commit = rc->commit_id;
388 patch_url.commit = rc->commit_id;
389 tree_url.commit = rc->commit_id;
391 tmp = strchr(rc->committer, '<');
392 if (tmp)
393 *tmp = '\0';
395 tmp = strchr(rc->commit_msg, '\n');
396 if (tmp)
397 *tmp = '\0';
398 !}
399 <div class='brief'>
400 <p class='brief_meta'>
401 <span class='briefs_age'>
402 {{ render datetime(tp, rc->committer_time, TM_DIFF) }}
403 </span>
404 {{" "}}
405 <span class="briefs_author">
406 {{ rc->committer }}
407 </span>
408 </p>
409 <p class="briefs_log">
410 <a href="{{ render gotweb_render_url(tp->tp_arg, &diff_url) }}">
411 {{ rc->commit_msg }}
412 </a>
413 {{ if rc->refs_str }}
414 {{ " " }} <span class="refs_str">({{ rc->refs_str }})</span>
415 {{ end }}
416 </a>
417 </p>
418 </div>
419 <div class="navs_wrapper">
420 <div class="navs">
421 <a href="{{ render gotweb_render_url(tp->tp_arg, &diff_url) }}">diff</a>
422 {{ " | " }}
423 <a href="{{ render gotweb_render_url(tp->tp_arg, &patch_url) }}">patch</a>
424 {{ " | " }}
425 <a href="{{ render gotweb_render_url(tp->tp_arg, &tree_url) }}">tree</a>
426 </div>
427 </div>
428 <hr />
429 {{ end }}
430 {{ render gotweb_render_more(tp, BRIEFS) }}
431 </div>
432 {{ end }}
434 {{ define gotweb_render_more(struct template *tp, int action) }}
435 {!
436 struct request *c = tp->tp_arg;
437 struct transport *t = c->t;
438 struct querystring *qs = t->qs;
439 struct gotweb_url more = {
440 .action = action,
441 .index_page = -1,
442 .path = qs->path,
443 .commit = t->more_id,
444 .headref = qs->headref,
445 .file = qs->file,
446 };
447 !}
448 {{ if t->more_id }}
449 <div id="np_wrapper">
450 <div id="nav_more">
451 <a href="{{ render gotweb_render_url(c, &more) }}">
452 More&nbsp;&darr;
453 </a>
454 </div>
455 </div>
456 {{ end }}
457 {{ end }}
459 {{ define gotweb_render_navs(struct template *tp) }}
460 {!
461 struct request *c = tp->tp_arg;
462 struct transport *t = c->t;
463 struct gotweb_url prev, next;
464 int have_prev, have_next;
466 gotweb_get_navs(c, &prev, &have_prev, &next, &have_next);
467 !}
468 <div id="np_wrapper">
469 <div id="nav_prev">
470 {{ if have_prev }}
471 <a href="{{ render gotweb_render_url(c, &prev) }}">
472 Previous
473 </a>
474 {{ end }}
475 </div>
476 <div id="nav_next">
477 {{ if have_next }}
478 <a href="{{ render gotweb_render_url(c, &next) }}">
479 Next
480 </a>
481 {{ end }}
482 </div>
483 </div>
484 {{ finally }}
485 {!
486 free(t->next_id);
487 t->next_id = NULL;
488 free(t->prev_id);
489 t->prev_id = NULL;
490 !}
491 {{ end }}
493 {{ define gotweb_render_commits(struct template *tp) }}
494 {!
495 struct request *c = tp->tp_arg;
496 struct transport *t = c->t;
497 struct repo_dir *repo_dir = t->repo_dir;
498 struct repo_commit *rc;
499 struct gotweb_url diff, patch, tree;
501 diff = (struct gotweb_url){
502 .action = DIFF,
503 .index_page = -1,
504 .page = -1,
505 .path = repo_dir->name,
506 };
507 patch = (struct gotweb_url){
508 .action = PATCH,
509 .index_page = -1,
510 .page = -1,
511 .path = repo_dir->name,
512 };
513 tree = (struct gotweb_url){
514 .action = TREE,
515 .index_page = -1,
516 .page = -1,
517 .path = repo_dir->name,
518 };
519 !}
520 <header class="subtitle">
521 <h2>Commits</h2>
522 </header>
523 <div class="commits_content">
524 {{ tailq-foreach rc &t->repo_commits entry }}
525 {!
526 diff.commit = rc->commit_id;
527 patch.commit = rc->commit_id;
528 tree.commit = rc->commit_id;
529 !}
530 <div class="page_header_wrapper">
531 <dl>
532 <dt>Commit:</dt>
533 <dd><code class="commit-id">{{ rc->commit_id }}</code></dd>
534 <dt>From:</dt>
535 <dd>{{ rc->author }}</dd>
536 {{ if strcmp(rc->committer, rc->author) != 0 }}
537 <dt>Via:</dt>
538 <dd>{{ rc->committer }}</dd>
539 {{ end }}
540 <dt>Date:</dt>
541 <dd>
542 {{ render datetime(tp, rc->committer_time, TM_LONG) }}
543 </dd>
544 </dl>
545 </div>
546 <hr />
547 <div class="commit">
548 {{ "\n" }}
549 {{ rc->commit_msg }}
550 </div>
551 <div class="navs_wrapper">
552 <div class="navs">
553 <a href="{{ render gotweb_render_url(c, &diff) }}">diff</a>
554 {{ " | " }}
555 <a href="{{ render gotweb_render_url(c, &patch) }}">patch</a>
556 {{ " | " }}
557 <a href="{{ render gotweb_render_url(c, &tree) }}">tree</a>
558 </div>
559 </div>
560 <hr />
561 {{ end }}
562 {{ render gotweb_render_more(tp, COMMITS) }}
563 </div>
564 {{ end }}
566 {{ define gotweb_render_blob(struct template *tp) }}
567 {!
568 struct request *c = tp->tp_arg;
569 struct transport *t = c->t;
570 struct querystring *qs = t->qs;
571 struct got_blob_object *blob = t->blob;
572 struct repo_commit *rc = TAILQ_FIRST(&t->repo_commits);
573 struct gotweb_url briefs_url, blame_url, raw_url;
575 memset(&briefs_url, 0, sizeof(briefs_url));
576 briefs_url.index_page = -1,
577 briefs_url.page = -1,
578 briefs_url.action = BRIEFS,
579 briefs_url.path = qs->path,
580 briefs_url.commit = qs->commit,
581 briefs_url.folder = qs->folder,
582 briefs_url.file = qs->file,
584 memcpy(&blame_url, &briefs_url, sizeof(blame_url));
585 blame_url.action = BLAME;
587 memcpy(&raw_url, &briefs_url, sizeof(raw_url));
588 raw_url.action = BLOBRAW;
589 !}
590 <header class="subtitle">
591 <h2>Blob</h2>
592 </header>
593 <div id="blob_content">
594 <div class="page_header_wrapper">
595 <dl>
596 <dt>Date:</dt>
597 <dd>
598 {{ render datetime(tp, rc->committer_time, TM_LONG) }}
599 </dd>
600 <dt>Message:</dt>
601 <dd class="commit-msg">{{ rc->commit_msg }}</dd>
602 <dt>Actions:</dt>
603 <dd>
604 <a href="{{ render gotweb_render_url(c, &briefs_url) }}">
605 History
606 </a>
607 {{" | "}}
608 <a href="{{ render gotweb_render_url(c, &blame_url) }}">
609 Blame
610 </a>
611 {{" | "}}
612 <a href="{{ render gotweb_render_url(c, &raw_url) }}">
613 Raw File
614 </a>
615 </dd>
616 </dl>
617 </div>
618 <hr />
619 <div id="blob">
620 <pre>
621 {{ render got_output_blob_by_lines(tp, blob, gotweb_render_blob_line) }}
622 </pre>
623 </div>
624 </div>
625 {{ end }}
627 {{ define gotweb_render_blob_line(struct template *tp, const char *line,
628 size_t no) }}
629 {!
630 char lineno[16];
631 int r;
633 r = snprintf(lineno, sizeof(lineno), "%zu", no);
634 if (r < 0 || (size_t)r >= sizeof(lineno))
635 return -1;
636 !}
637 <div class="blob_line" id="line{{ lineno }}">
638 <a href="#line{{ lineno }}">{{ lineno }}</a>
639 {{" "}}
640 <span class="blob_code">{{ line }}</span>
641 </div>
642 {{ end }}
644 {{ define gotweb_render_tree(struct template *tp) }}
645 {!
646 struct request *c = tp->tp_arg;
647 struct transport *t = c->t;
648 struct repo_commit *rc = TAILQ_FIRST(&t->repo_commits);
649 !}
650 <header class='subtitle'>
651 <h2>Tree</h2>
652 </header>
653 <div id="tree_content">
654 <div class="page_header_wrapper">
655 <dl>
656 <dt>Tree:</dt>
657 <dd><code class="commit-id">{{ rc->tree_id }}</code></dd>
658 <dt>Date:</dt>
659 <dd>
660 {{ render datetime(tp, rc->committer_time, TM_LONG) }}
661 </dd>
662 <dt>Message:</dt>
663 <dd class="commit-msg">{{ rc->commit_msg }}</dd>
664 </dl>
665 </div>
666 <hr />
667 <table id="tree">
668 {{ render got_output_repo_tree(c, gotweb_render_tree_item) }}
669 </table>
670 </div>
671 {{ end }}
673 {{ define gotweb_render_tree_item(struct template *tp,
674 struct got_tree_entry *te) }}
675 {!
676 struct request *c = tp->tp_arg;
677 struct transport *t = c->t;
678 struct querystring *qs = t->qs;
679 struct repo_commit *rc = TAILQ_FIRST(&t->repo_commits);
680 const char *modestr = "";
681 const char *name;
682 const char *folder;
683 char *dir = NULL;
684 mode_t mode;
685 struct gotweb_url url = {
686 .index_page = -1,
687 .page = -1,
688 .commit = rc->commit_id,
689 .path = qs->path,
690 };
692 name = got_tree_entry_get_name(te);
693 mode = got_tree_entry_get_mode(te);
695 folder = qs->folder ? qs->folder : "";
696 if (S_ISDIR(mode)) {
697 if (asprintf(&dir, "%s/%s", folder, name) == -1)
698 return (-1);
700 url.action = TREE;
701 url.folder = dir;
702 } else {
703 url.action = BLOB;
704 url.folder = folder;
705 url.file = name;
708 if (got_object_tree_entry_is_submodule(te))
709 modestr = "$";
710 else if (S_ISLNK(mode))
711 modestr = "@";
712 else if (S_ISDIR(mode))
713 modestr = "/";
714 else if (mode & S_IXUSR)
715 modestr = "*";
716 !}
717 <tr class="tree_wrapper">
718 {{ if S_ISDIR(mode) }}
719 <td class="tree_line" colspan=2>
720 <a href="{{ render gotweb_render_url(c, &url) }}">
721 {{ name }}{{ modestr }}
722 </a>
723 </td>
724 {{ else }}
725 <td class="tree_line">
726 <a href="{{ render gotweb_render_url(c, &url) }}">
727 {{ name }}{{ modestr }}
728 </a>
729 </td>
730 <td class="tree_line_blank">
731 {! url.action = COMMITS; !}
732 <a href="{{ render gotweb_render_url(c, &url) }}">
733 commits
734 </a>
735 {{ " | " }}
736 {! url.action = BLAME; !}
737 <a href="{{ render gotweb_render_url(c, &url) }}">
738 blame
739 </a>
740 </td>
741 {{ end }}
742 </tr>
743 {{ finally }}
744 {!
745 free(dir);
746 !}
747 {{ end }}
749 {{ define gotweb_render_tags(struct template *tp) }}
750 {!
751 struct request *c = tp->tp_arg;
752 struct transport *t = c->t;
753 struct querystring *qs = t->qs;
754 struct repo_tag *rt;
755 int commit_found;
757 commit_found = qs->commit == NULL;
758 !}
759 <header class='subtitle'>
760 <h2>Tags</h2>
761 </header>
762 <div id="tags_content">
763 {{ if t->tag_count == 0 }}
764 <div id="err_content">
765 This repository contains no tags
766 </div>
767 {{ else }}
768 {{ tailq-foreach rt &t->repo_tags entry }}
769 {{ if commit_found || !strcmp(qs->commit, rt->commit_id) }}
770 {! commit_found = 1; !}
771 {{ render tag_item(tp, rt) }}
772 {{ end }}
773 {{ end }}
774 {{ if t->next_id || t->prev_id }}
775 {! qs->action = TAGS; !}
776 {{ render gotweb_render_navs(tp) }}
777 {{ end }}
778 {{ end }}
779 </div>
780 {{ end }}
782 {{ define tag_item(struct template *tp, struct repo_tag *rt) }}
783 {!
784 struct request *c = tp->tp_arg;
785 struct transport *t = c->t;
786 struct repo_dir *repo_dir = t->repo_dir;
787 char *tag_name = rt->tag_name;
788 char *msg = rt->tag_commit;
789 char *nl;
790 struct gotweb_url url = {
791 .action = TAG,
792 .index_page = -1,
793 .page = -1,
794 .path = repo_dir->name,
795 .commit = rt->commit_id,
796 };
798 if (strncmp(tag_name, "refs/tags/", 10) == 0)
799 tag_name += 10;
801 if (msg) {
802 nl = strchr(msg, '\n');
803 if (nl)
804 *nl = '\0';
806 !}
807 <div class="tag_age">
808 {{ render datetime(tp, rt->tagger_time, TM_DIFF) }}
809 </div>
810 <div class="tag_name">{{ tag_name }}</div>
811 <div class="tag_log">
812 <a href="{{ render gotweb_render_url(c, &url) }}">
813 {{ msg }}
814 </a>
815 </div>
816 <div class="navs_wrapper">
817 <div class="navs">
818 <a href="{{ render gotweb_render_url(c, &url) }}">tag</a>
819 {{ " | " }}
820 {! url.action = BRIEFS; !}
821 <a href="{{ render gotweb_render_url(c, &url) }}">commit briefs</a>
822 {{ " | " }}
823 {! url.action = COMMITS; !}
824 <a href="{{ render gotweb_render_url(c, &url) }}">commits</a>
825 </div>
826 </div>
827 <hr />
828 {{ end }}
830 {{ define gotweb_render_tag(struct template *tp) }}
831 {!
832 struct request *c = tp->tp_arg;
833 struct transport *t = c->t;
834 struct repo_tag *rt;
835 const char *tag_name;
837 rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
838 tag_name = rt->tag_name;
840 if (strncmp(tag_name, "refs/", 5) == 0)
841 tag_name += 5;
842 !}
843 <header class="subtitle">
844 <h2>Tag</h2>
845 </header>
846 <div id="tags_content">
847 <div class="page_header_wrapper">
848 <dl>
849 <dt>Commit:</dt>
850 <dd>
851 <code class="commit-id">{{ rt->commit_id }}</code>
852 {{ " " }}
853 <span class="refs_str">({{ tag_name }})</span>
854 </dd>
855 <dt>Tagger:</dt>
856 <dd>{{ rt->tagger }}</dd>
857 <dt>Date:</dt>
858 <dd>
859 {{ render datetime(tp, rt->tagger_time, TM_LONG)}}
860 </dd>
861 <dt>Message:</dt>
862 <dd class="commit-msg">{{ rt->commit_msg }}</dd>
863 </dl>
864 <hr />
865 <pre id="tag_commit">
866 {{ rt->tag_commit }}
867 </pre>
868 </div>
869 </div>
870 {{ end }}
872 {{ define gotweb_render_diff(struct template *tp) }}
873 {!
874 struct request *c = tp->tp_arg;
875 struct transport *t = c->t;
876 struct querystring *qs = t->qs;
877 FILE *fp = t->fp;
878 struct repo_commit *rc = TAILQ_FIRST(&t->repo_commits);
879 char *line = NULL;
880 size_t linesize = 0;
881 ssize_t linelen;
882 struct gotweb_url patch_url, tree_url = {
883 .action = TREE,
884 .index_page = -1,
885 .page = -1,
886 .path = qs->path,
887 .commit = rc->commit_id,
888 };
890 memcpy(&patch_url, &tree_url, sizeof(patch_url));
891 patch_url.action = PATCH;
892 !}
893 <header class="subtitle">
894 <h2>Commit Diff</h2>
895 </header>
896 <div id="diff_content">
897 <div class="page_header_wrapper">
898 <dl>
899 <dt>Commit:</dt>
900 <dd><code class="commit-id">{{ rc->commit_id }}</code></dd>
901 <dt>From:</dt>
902 <dd>{{ rc->author }}</dd>
903 {{ if strcmp(rc->committer, rc->author) != 0 }}
904 <dt>Via:</dt>
905 <dd>{{ rc->committer }}</dd>
906 {{ end }}
907 <dt>Date:</dt>
908 <dd>
909 {{ render datetime(tp, rc->committer_time, TM_LONG) }}
910 </dd>
911 <dt>Message:</dt>
912 <dd class="commit-msg">{{ rc->commit_msg }}</dd>
913 <dt>Actions:</dt>
914 <dd>
915 <a href="{{ render gotweb_render_url(c, &patch_url) }}">
916 Patch
917 </a>
918 {{" | "}}
919 <a href="{{ render gotweb_render_url(c, &tree_url) }}">
920 Tree
921 </a>
922 </dd>
923 </dl>
924 </div>
925 <hr />
926 <pre id="diff">
927 {{ while (linelen = getline(&line, &linesize, fp)) != -1 }}
928 {{ render diff_line(tp, line) }}
929 {{ end }}
930 </pre>
931 </div>
932 {{ finally }}
933 {! free(line); !}
934 {{ end }}
936 {{ define diff_line(struct template *tp, char *line )}}
937 {!
938 const char *color = NULL;
939 char *nl;
941 if (!strncmp(line, "-", 1))
942 color = "diff_minus";
943 else if (!strncmp(line, "+", 1))
944 color = "diff_plus";
945 else if (!strncmp(line, "@@", 2))
946 color = "diff_chunk_header";
947 else if (!strncmp(line, "commit +", 8) ||
948 !strncmp(line, "commit -", 8) ||
949 !strncmp(line, "blob +", 6) ||
950 !strncmp(line, "blob -", 6) ||
951 !strncmp(line, "file +", 6) ||
952 !strncmp(line, "file -", 6))
953 color = "diff_meta";
954 else if (!strncmp(line, "from:", 5) || !strncmp(line, "via:", 4))
955 color = "diff_author";
956 else if (!strncmp(line, "date:", 5))
957 color = "diff_date";
959 nl = strchr(line, '\n');
960 if (nl)
961 *nl = '\0';
962 !}
963 <span class="diff_line {{ color }}">{{ line }}</span>{{"\n"}}
964 {{ end }}
966 {{ define gotweb_render_branches(struct template *tp,
967 struct got_reflist_head *refs) }}
968 {!
969 struct got_reflist_entry *re;
970 !}
971 <header class='subtitle'>
972 <h2>Branches</h2>
973 </header>
974 <div id="branches_content">
975 {{ tailq-foreach re refs entry }}
976 {{ if !got_ref_is_symbolic(re->ref) }}
977 {{ render branch(tp, re) }}
978 {{ end }}
979 {{ end }}
980 </div>
981 {{ end }}
983 {{ define branch(struct template *tp, struct got_reflist_entry *re) }}
984 {!
985 const struct got_error *err;
986 struct request *c = tp->tp_arg;
987 struct querystring *qs = c->t->qs;
988 const char *refname;
989 time_t age;
990 struct gotweb_url url = {
991 .action = SUMMARY,
992 .index_page = -1,
993 .page = -1,
994 .path = qs->path,
995 };
997 refname = got_ref_get_name(re->ref);
999 err = got_get_repo_age(&age, c, refname);
1000 if (err) {
1001 log_warnx("%s: %s", __func__, err->msg);
1002 return -1;
1005 if (strncmp(refname, "refs/heads/", 11) == 0)
1006 refname += 11;
1008 url.headref = refname;
1010 <section class="branches_wrapper">
1011 <div class="branches_age">
1012 {{ render datetime(tp, age, TM_DIFF) }}
1013 </div>
1014 <div class="branch">
1015 <a href="{{ render gotweb_render_url(c, &url) }}">{{ refname }}</a>
1016 </div>
1017 <div class="navs_wrapper">
1018 <div class="navs">
1019 <a href="{{ render gotweb_render_url(c, &url) }}">summary</a>
1020 {{" | "}}
1021 {! url.action = BRIEFS; !}
1022 <a href="{{ render gotweb_render_url(c, &url) }}">commit briefs</a>
1023 {{" | "}}
1024 {! url.action = COMMITS; !}
1025 <a href="{{ render gotweb_render_url(c, &url) }}">commits</a>
1026 </div>
1027 </div>
1028 <hr />
1029 </section>
1030 {{ end }}
1032 {{ define gotweb_render_summary(struct template *tp) }}
1034 struct request *c = tp->tp_arg;
1035 struct server *srv = c->srv;
1036 struct transport *t = c->t;
1037 struct got_reflist_head *refs = &t->refs;
1039 <dl id="summary_wrapper" class="page_header_wrapper">
1040 {{ if srv->show_repo_description }}
1041 <dt>Description:</dt>
1042 <dd>{{ t->repo_dir->description }}</dd>
1043 {{ end }}
1044 {{ if srv->show_repo_owner }}
1045 <dt>Owner:</dt>
1046 <dd>{{ t->repo_dir->owner }}</dd>
1047 {{ end }}
1048 {{ if srv->show_repo_age }}
1049 <dt>Last Change:</dt>
1050 <dd>
1051 {{ render datetime(tp, t->repo_dir->age, TM_DIFF) }}
1052 </dd>
1053 {{ end }}
1054 {{ if srv->show_repo_cloneurl }}
1055 <dt>Clone URL:</dt>
1056 <dd><pre class="clone-url">{{ t->repo_dir->url }}</pre></dd>
1057 {{ end }}
1058 </dl>
1059 {{ render gotweb_render_briefs(tp) }}
1060 {{ render gotweb_render_tags(tp) }}
1061 {{ render gotweb_render_branches(tp, refs) }}
1062 {{ end }}
1064 {{ define gotweb_render_blame(struct template *tp) }}
1066 const struct got_error *err;
1067 struct request *c = tp->tp_arg;
1068 struct transport *t = c->t;
1069 struct querystring *qs = t->qs;
1070 struct repo_commit *rc = TAILQ_FIRST(&t->repo_commits);
1071 struct gotweb_url briefs_url, blob_url, raw_url;
1073 memset(&briefs_url, 0, sizeof(briefs_url));
1074 briefs_url.index_page = -1,
1075 briefs_url.page = -1,
1076 briefs_url.action = BRIEFS,
1077 briefs_url.path = qs->path,
1078 briefs_url.commit = qs->commit,
1079 briefs_url.folder = qs->folder,
1080 briefs_url.file = qs->file,
1082 memcpy(&blob_url, &briefs_url, sizeof(blob_url));
1083 blob_url.action = BLOB;
1085 memcpy(&raw_url, &briefs_url, sizeof(raw_url));
1086 raw_url.action = BLOBRAW;
1088 <header class="subtitle">
1089 <h2>Blame</h2>
1090 </header>
1091 <div id="blame_content">
1092 <div class="page_header_wrapper">
1093 <dl>
1094 <dt>Date:</dt>
1095 <dd>
1096 {{ render datetime(tp, rc->committer_time, TM_LONG) }}
1097 </dd>
1098 <dt>Message:</dt>
1099 <dd class="commit-msg">{{ rc->commit_msg }}</dd>
1100 <dt>Actions:</dt>
1101 <dd>
1102 <a href="{{ render gotweb_render_url(c, &briefs_url) }}">
1103 History
1104 </a>
1105 {{" | "}}
1106 <a href="{{ render gotweb_render_url(c, &blob_url) }}">
1107 Blob
1108 </a>
1109 {{" | "}}
1110 <a href="{{ render gotweb_render_url(c, &raw_url) }}">
1111 Raw File
1112 </a>
1113 </dd>
1114 </dl>
1115 </div>
1116 <hr />
1117 <pre id="blame">
1119 err = got_output_file_blame(c, &blame_line);
1120 if (err && err->code != GOT_ERR_CANCELLED)
1121 log_warnx("%s: got_output_file_blame: %s", __func__,
1122 err->msg);
1123 if (err)
1124 return (-1);
1126 </pre>
1127 </div>
1128 {{ end }}
1130 {{ define blame_line(struct template *tp, const char *line,
1131 struct blame_line *bline, int lprec, int lcur) }}
1133 struct request *c = tp->tp_arg;
1134 struct transport *t = c->t;
1135 struct repo_dir *repo_dir = t->repo_dir;
1136 char *committer, *s;
1137 struct gotweb_url url = {
1138 .action = DIFF,
1139 .index_page = -1,
1140 .page = -1,
1141 .path = repo_dir->name,
1142 .commit = bline->id_str,
1145 s = strchr(bline->committer, '<');
1146 committer = s ? s + 1 : bline->committer;
1148 s = strchr(committer, '@');
1149 if (s)
1150 *s = '\0';
1152 <div class="blame_line">
1153 <span class="blame_number">{{ printf "%*d ", lprec, lcur }}</span>
1154 <span class="blame_hash">
1155 <a href="{{ render gotweb_render_url(c, &url) }}">
1156 {{ printf "%.8s", bline->id_str }}
1157 </a>
1158 </span>
1159 {{" "}}
1160 <span class="blame_date">{{ bline->datebuf }}</span>
1161 {{" "}}
1162 <span class="blame_author">{{ printf "%.9s", committer }}</span>
1163 {{" "}}
1164 <span class="blame_code">{{ line }}</span>
1165 </div>
1166 {{ end }}
1168 {{ define gotweb_render_patch(struct template *tp) }}
1170 struct request *c = tp->tp_arg;
1171 struct transport *t = c->t;
1172 struct repo_commit *rc = TAILQ_FIRST(&t->repo_commits);
1173 struct tm tm;
1174 char buf[BUFSIZ], datebuf[64];
1175 size_t r;
1177 if (gmtime_r(&rc->committer_time, &tm) == NULL ||
1178 strftime(datebuf, sizeof(datebuf), "%a %b %d %T %Y UTC", &tm) == 0)
1179 return (-1);
1181 commit {{ rc->commit_id }} {{ "\n" }}
1182 from: {{ rc->author | unsafe }} {{ "\n" }}
1183 {{ if strcmp(rc->committer, rc->author) != 0 }}
1184 via: {{ rc->committer | unsafe }} {{ "\n" }}
1185 {{ end }}
1186 date: {{ datebuf }} {{ "\n" }}
1187 {{ "\n" }}
1188 {{ rc->commit_msg | unsafe }} {{ "\n" }}
1190 if (template_flush(tp) == -1)
1191 return (-1);
1192 for (;;) {
1193 r = fread(buf, 1, sizeof(buf), t->fp);
1194 if (fcgi_write(c, buf, r) == -1 ||
1195 r != sizeof(buf))
1196 break;
1199 {{ end }}
1201 {{ define gotweb_render_rss(struct template *tp) }}
1203 struct request *c = tp->tp_arg;
1204 struct server *srv = c->srv;
1205 struct transport *t = c->t;
1206 struct repo_dir *repo_dir = t->repo_dir;
1207 struct repo_tag *rt;
1208 struct gotweb_url summary = {
1209 .action = SUMMARY,
1210 .index_page = -1,
1211 .page = -1,
1212 .path = repo_dir->name,
1215 <?xml version="1.0" encoding="UTF-8"?>
1216 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
1217 <channel>
1218 <title>Tags of {{ repo_dir->name }}</title>
1219 <link>
1220 <![CDATA[
1221 {{ render gotweb_render_absolute_url(c, &summary) }}
1222 ]]>
1223 </link>
1224 {{ if srv->show_repo_description }}
1225 <description>{{ repo_dir->description }}</description>
1226 {{ end }}
1227 {{ tailq-foreach rt &t->repo_tags entry }}
1228 {{ render rss_tag_item(tp, rt) }}
1229 {{ end }}
1230 </channel>
1231 </rss>
1232 {{ end }}
1234 {{ define rss_tag_item(struct template *tp, struct repo_tag *rt) }}
1236 struct request *c = tp->tp_arg;
1237 struct transport *t = c->t;
1238 struct repo_dir *repo_dir = t->repo_dir;
1239 struct tm tm;
1240 char rfc822[128];
1241 int r;
1242 char *tag_name = rt->tag_name;
1243 struct gotweb_url tag = {
1244 .action = TAG,
1245 .index_page = -1,
1246 .page = -1,
1247 .path = repo_dir->name,
1248 .commit = rt->commit_id,
1251 if (strncmp(tag_name, "refs/tags/", 10) == 0)
1252 tag_name += 10;
1254 if (gmtime_r(&rt->tagger_time, &tm) == NULL)
1255 return -1;
1256 r = strftime(rfc822, sizeof(rfc822), "%a, %d %b %Y %H:%M:%S GMT", &tm);
1257 if (r == 0)
1258 return 0;
1260 <item>
1261 <title>{{ repo_dir->name }} {{" "}} {{ tag_name }}</title>
1262 <link>
1263 <![CDATA[
1264 {{ render gotweb_render_absolute_url(c, &tag) }}
1265 ]]>
1266 </link>
1267 <description>
1268 <![CDATA[<pre>{{ rt->tag_commit }}</pre>]]>
1269 </description>
1270 {{ render rss_author(tp, rt->tagger) }}
1271 <guid isPermaLink="false">{{ rt->commit_id }}</guid>
1272 <pubDate>
1273 {{ rfc822 }}
1274 </pubDate>
1275 </item>
1276 {{ end }}
1278 {{ define rss_author(struct template *tp, char *author) }}
1280 char *t, *mail;
1282 /* what to do if the author name contains a paren? */
1283 if (strchr(author, '(') != NULL || strchr(author, ')') != NULL)
1284 return 0;
1286 t = strchr(author, '<');
1287 if (t == NULL)
1288 return 0;
1289 *t = '\0';
1290 mail = t+1;
1292 while (isspace((unsigned char)*--t))
1293 *t = '\0';
1295 t = strchr(mail, '>');
1296 if (t == NULL)
1297 return 0;
1298 *t = '\0';
1300 <author>
1301 {{ mail }} {{" "}} ({{ author }})
1302 </author>
1303 {{ end }}