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 "proc.h"
39 #include "gotwebd.h"
40 #include "tmpl.h"
42 enum gotweb_ref_tm {
43 TM_DIFF,
44 TM_LONG,
45 };
47 static int datetime(struct template *, time_t, int);
48 static int gotweb_render_blob_line(struct template *, const char *, size_t);
49 static int gotweb_render_tree_item(struct template *, struct got_tree_entry *);
50 static int blame_line(struct template *, const char *, struct blame_line *,
51 int, int);
53 static inline int gotweb_render_more(struct template *, int);
55 static inline int diff_line(struct template *, char *);
56 static inline int tag_item(struct template *, struct repo_tag *);
57 static inline int branch(struct template *, struct got_reflist_entry *);
58 static inline int rss_tag_item(struct template *, struct repo_tag *);
59 static inline int rss_author(struct template *, char *);
61 !}
63 {{ define datetime(struct template *tp, time_t t, int fmt) }}
64 {!
65 struct tm tm;
66 char rfc3339[64];
67 char datebuf[64];
69 if (gmtime_r(&t, &tm) == NULL)
70 return -1;
72 if (strftime(rfc3339, sizeof(rfc3339), "%FT%TZ", &tm) == 0)
73 return -1;
75 if (fmt != TM_DIFF && asctime_r(&tm, datebuf) == NULL)
76 return -1;
77 !}
78 <time datetime="{{ rfc3339 }}">
79 {{ if fmt == TM_DIFF }}
80 {{ render gotweb_render_age(tp, t) }}
81 {{ else }}
82 {{ datebuf }} {{ " UTC" }}
83 {{ end }}
84 </time>
85 {{ end }}
87 {{ define gotweb_render_page(struct template *tp,
88 int (*body)(struct template *)) }}
89 {!
90 struct request *c = tp->tp_arg;
91 struct server *srv = c->srv;
92 struct querystring *qs = c->t->qs;
93 struct gotweb_url u_path;
94 const char *prfx = c->document_uri;
95 const char *css = srv->custom_css;
97 memset(&u_path, 0, sizeof(u_path));
98 u_path.index_page = -1;
99 u_path.page = -1;
100 u_path.action = SUMMARY;
101 !}
102 <!doctype html>
103 <html>
104 <head>
105 <meta charset="utf-8" />
106 <title>{{ srv->site_name }}</title>
107 <meta name="viewport" content="initial-scale=1.0" />
108 <meta name="msapplication-TileColor" content="#da532c" />
109 <meta name="theme-color" content="#ffffff"/>
110 <link rel="apple-touch-icon" sizes="180x180" href="{{ prfx }}apple-touch-icon.png" />
111 <link rel="icon" type="image/png" sizes="32x32" href="{{ prfx }}favicon-32x32.png" />
112 <link rel="icon" type="image/png" sizes="16x16" href="{{ prfx }}favicon-16x16.png" />
113 <link rel="manifest" href="{{ prfx }}site.webmanifest"/>
114 <link rel="mask-icon" href="{{ prfx }}safari-pinned-tab.svg" />
115 <link rel="stylesheet" type="text/css" href="{{ prfx }}{{ css }}" />
116 </head>
117 <body>
118 <header id="header">
119 <div id="got_link">
120 <a href="{{ srv->logo_url }}" target="_blank">
121 <img src="{{ prfx }}{{ srv->logo }}" />
122 </a>
123 </div>
124 </header>
125 <nav id="site_path">
126 <div id="site_link">
127 <a href="?index_page={{ printf "%d", qs->index_page }}">
128 {{ srv->site_link }}
129 </a>
130 {{ if qs->path }}
131 {! u_path.path = qs->path; !}
132 {{ " / " }}
133 <a href="{{ render gotweb_render_url(tp->tp_arg, &u_path)}}">
134 {{ qs->path }}
135 </a>
136 {{ end }}
137 {{ if qs->action != INDEX }}
138 {{ " / " }}{{ gotweb_action_name(qs->action) }}
139 {{ end }}
140 </div>
141 </nav>
142 <main>
143 {{ render body(tp) }}
144 </main>
145 <footer id="site_owner_wrapper">
146 <p id="site_owner">
147 {{ if srv->show_site_owner }}
148 {{ srv->site_owner }}
149 {{ end }}
150 </p>
151 </footer>
152 </body>
153 </html>
154 {{ end }}
156 {{ define gotweb_render_error(struct template *tp) }}
157 {!
158 struct request *c = tp->tp_arg;
159 struct transport *t = c->t;
160 !}
161 <div id="err_content">
162 {{ if t->error }}
163 {{ t->error->msg }}
164 {{ else }}
165 See daemon logs for details
166 {{ end }}
167 </div>
168 {{ end }}
170 {{ define gotweb_render_repo_table_hdr(struct template *tp) }}
171 {!
172 struct request *c = tp->tp_arg;
173 struct server *srv = c->srv;
174 !}
175 <div id="index_header">
176 <div class="index_project">
177 Project
178 </div>
179 {{ if srv->show_repo_description }}
180 <div class="index_project_description">
181 Description
182 </div>
183 {{ end }}
184 {{ if srv->show_repo_owner }}
185 <div class="index_project_owner">
186 Owner
187 </div>
188 {{ end }}
189 {{ if srv->show_repo_age }}
190 <div class="index_project_age">
191 Last Change
192 </div>
193 {{ end }}
194 </div>
195 {{ end }}
197 {{ define gotweb_render_repo_fragment(struct template *tp, struct repo_dir *repo_dir) }}
198 {!
199 struct request *c = tp->tp_arg;
200 struct server *srv = c->srv;
201 struct gotweb_url summary = {
202 .action = SUMMARY,
203 .index_page = -1,
204 .page = -1,
205 .path = repo_dir->name,
206 }, briefs = {
207 .action = BRIEFS,
208 .index_page = -1,
209 .page = -1,
210 .path = repo_dir->name,
211 }, commits = {
212 .action = COMMITS,
213 .index_page = -1,
214 .page = -1,
215 .path = repo_dir->name,
216 }, tags = {
217 .action = TAGS,
218 .index_page = -1,
219 .page = -1,
220 .path = repo_dir->name,
221 }, tree = {
222 .action = TREE,
223 .index_page = -1,
224 .page = -1,
225 .path = repo_dir->name,
226 }, rss = {
227 .action = RSS,
228 .index_page = -1,
229 .page = -1,
230 .path = repo_dir->name,
231 };
232 !}
233 <div class="index_wrapper">
234 <div class="index_project">
235 <a href="{{ render gotweb_render_url(tp->tp_arg, &summary) }}">{{ repo_dir->name }}</a>
236 </div>
237 {{ if srv->show_repo_description }}
238 <div class="index_project_description">
239 {{ repo_dir->description }}
240 </div>
241 {{ end }}
242 {{ if srv->show_repo_owner }}
243 <div class="index_project_owner">
244 {{ repo_dir->owner }}
245 </div>
246 {{ end }}
247 {{ if srv->show_repo_age }}
248 <div class="index_project_age">
249 {{ render datetime(tp, repo_dir->age, TM_DIFF) }}
250 </div>
251 {{ end }}
252 <div class="navs_wrapper">
253 <div class="navs">
254 <a href="{{ render gotweb_render_url(tp->tp_arg, &summary) }}">summary</a>
255 {{ " | " }}
256 <a href="{{ render gotweb_render_url(tp->tp_arg, &briefs) }}">briefs</a>
257 {{ " | " }}
258 <a href="{{ render gotweb_render_url(tp->tp_arg, &commits) }}">commits</a>
259 {{ " | " }}
260 <a href="{{ render gotweb_render_url(tp->tp_arg, &tags) }}">tags</a>
261 {{ " | " }}
262 <a href="{{ render gotweb_render_url(tp->tp_arg, &tree) }}">tree</a>
263 {{ " | " }}
264 <a href="{{ render gotweb_render_url(tp->tp_arg, &rss) }}">rss</a>
265 </div>
266 <hr />
267 </div>
268 </div>
269 {{ end }}
271 {{ define gotweb_render_briefs(struct template *tp) }}
272 {!
273 struct request *c = tp->tp_arg;
274 struct transport *t = c->t;
275 struct querystring *qs = c->t->qs;
276 struct repo_commit *rc;
277 struct repo_dir *repo_dir = t->repo_dir;
278 struct gotweb_url diff_url, tree_url;
279 char *tmp;
281 diff_url = (struct gotweb_url){
282 .action = DIFF,
283 .index_page = -1,
284 .page = -1,
285 .path = repo_dir->name,
286 .headref = qs->headref,
287 };
288 tree_url = (struct gotweb_url){
289 .action = TREE,
290 .index_page = -1,
291 .page = -1,
292 .path = repo_dir->name,
293 .headref = qs->headref,
294 };
295 !}
296 <header class='subtitle'>
297 <h2>Commit Briefs</h2>
298 </header>
299 <div id="briefs_content">
300 {{ tailq-foreach rc &t->repo_commits entry }}
301 {!
302 diff_url.commit = rc->commit_id;
303 tree_url.commit = rc->commit_id;
305 tmp = strchr(rc->committer, '<');
306 if (tmp)
307 *tmp = '\0';
309 tmp = strchr(rc->commit_msg, '\n');
310 if (tmp)
311 *tmp = '\0';
312 !}
313 <div class='brief'>
314 <p class='brief_meta'>
315 <span class='briefs_age'>
316 {{ render datetime(tp, rc->committer_time, TM_DIFF) }}
317 </span>
318 {{" "}}
319 <span class="briefs_author">
320 {{ rc->committer }}
321 </span>
322 </p>
323 <p class="briefs_log">
324 <a href="{{ render gotweb_render_url(tp->tp_arg, &diff_url) }}">
325 {{ rc->commit_msg }}
326 </a>
327 {{ if rc->refs_str }}
328 {{ " " }} <span class="refs_str">({{ rc->refs_str }})</span>
329 {{ end }}
330 </a>
331 </p>
332 </div>
333 <div class="navs_wrapper">
334 <div class="navs">
335 <a href="{{ render gotweb_render_url(tp->tp_arg, &diff_url) }}">diff</a>
336 {{ " | " }}
337 <a href="{{ render gotweb_render_url(tp->tp_arg, &tree_url) }}">tree</a>
338 </div>
339 </div>
340 <hr />
341 {{ end }}
342 {{ render gotweb_render_more(tp, BRIEFS) }}
343 </div>
344 {{ end }}
346 {{ define gotweb_render_more(struct template *tp, int action) }}
347 {!
348 struct request *c = tp->tp_arg;
349 struct transport *t = c->t;
350 struct querystring *qs = t->qs;
351 struct gotweb_url more = {
352 .action = action,
353 .index_page = -1,
354 .path = qs->path,
355 .commit = t->more_id,
356 .headref = qs->headref,
357 .file = qs->file,
358 };
359 !}
360 {{ if t->more_id }}
361 <div id="np_wrapper">
362 <div id="nav_more">
363 <a href="{{ render gotweb_render_url(c, &more) }}">
364 More&nbsp;&darr;
365 </a>
366 </div>
367 </div>
368 {{ end }}
369 {{ end }}
371 {{ define gotweb_render_navs(struct template *tp) }}
372 {!
373 struct request *c = tp->tp_arg;
374 struct transport *t = c->t;
375 struct gotweb_url prev, next;
376 int have_prev, have_next;
378 gotweb_get_navs(c, &prev, &have_prev, &next, &have_next);
379 !}
380 <div id="np_wrapper">
381 <div id="nav_prev">
382 {{ if have_prev }}
383 <a href="{{ render gotweb_render_url(c, &prev) }}">
384 Previous
385 </a>
386 {{ end }}
387 </div>
388 <div id="nav_next">
389 {{ if have_next }}
390 <a href="{{ render gotweb_render_url(c, &next) }}">
391 Next
392 </a>
393 {{ end }}
394 </div>
395 </div>
396 {{ finally }}
397 {!
398 free(t->next_id);
399 t->next_id = NULL;
400 free(t->prev_id);
401 t->prev_id = NULL;
402 !}
403 {{ end }}
405 {{ define gotweb_render_commits(struct template *tp) }}
406 {!
407 struct request *c = tp->tp_arg;
408 struct transport *t = c->t;
409 struct repo_dir *repo_dir = t->repo_dir;
410 struct repo_commit *rc;
411 struct gotweb_url diff, tree;
413 diff = (struct gotweb_url){
414 .action = DIFF,
415 .index_page = -1,
416 .page = -1,
417 .path = repo_dir->name,
418 };
419 tree = (struct gotweb_url){
420 .action = TREE,
421 .index_page = -1,
422 .page = -1,
423 .path = repo_dir->name,
424 };
425 !}
426 <header class="subtitle">
427 <h2>Commits</h2>
428 </header>
429 <div class="commits_content">
430 {{ tailq-foreach rc &t->repo_commits entry }}
431 {!
432 diff.commit = rc->commit_id;
433 tree.commit = rc->commit_id;
434 !}
435 <div class="commits_header_wrapper">
436 <dl class="commits_header">
437 <dt>Commit:</dt>
438 <dd><code class="commit-id">{{ rc->commit_id }}</code></dd>
439 <dt>From:</dt>
440 <dd>{{ rc->author }}</dd>
441 {{ if strcmp(rc->committer, rc->author) != 0 }}
442 <dt>Via:</dt>
443 <dd>{{ rc->committer }}</dd>
444 {{ end }}
445 <dt>Date:</dt>
446 <dd>
447 {{ render datetime(tp, rc->committer_time, TM_LONG) }}
448 </dd>
449 </dl>
450 </div>
451 <hr />
452 <div class="commit">
453 {{ "\n" }}
454 {{ rc->commit_msg }}
455 </div>
456 <div class="navs_wrapper">
457 <div class="navs">
458 <a href="{{ render gotweb_render_url(c, &diff) }}">diff</a>
459 {{ " | " }}
460 <a href="{{ render gotweb_render_url(c, &tree) }}">tree</a>
461 </div>
462 </div>
463 <hr />
464 {{ end }}
465 {{ render gotweb_render_more(tp, COMMITS) }}
466 </div>
467 {{ end }}
469 {{ define gotweb_render_blob(struct template *tp) }}
470 {!
471 struct request *c = tp->tp_arg;
472 struct transport *t = c->t;
473 struct got_blob_object *blob = t->blob;
474 struct repo_commit *rc = TAILQ_FIRST(&t->repo_commits);
475 !}
476 <header class="subtitle">
477 <h2>Blob</h2>
478 </header>
479 <div id="blob_content">
480 <div id="blob_header_wrapper">
481 <dl id="blob_header">
482 <dt>Date:</dt>
483 <dd>
484 {{ render datetime(tp, rc->committer_time, TM_LONG) }}
485 </dd>
486 <dt>Message:</dt>
487 <dd class="commit-msg">{{ rc->commit_msg }}</dd>
488 </dl>
489 </div>
490 <hr />
491 <div id="blob">
492 <pre>
493 {{ render got_output_blob_by_lines(tp, blob, gotweb_render_blob_line) }}
494 </pre>
495 </div>
496 </div>
497 {{ end }}
499 {{ define gotweb_render_blob_line(struct template *tp, const char *line,
500 size_t no) }}
501 {!
502 char lineno[16];
503 int r;
505 r = snprintf(lineno, sizeof(lineno), "%zu", no);
506 if (r < 0 || (size_t)r >= sizeof(lineno))
507 return -1;
508 !}
509 <div class="blob_line" id="line{{ lineno }}">
510 <a href="#line{{ lineno }}">{{ lineno }}</a>
511 <span class="blob_code">{{ line }}</span>
512 </div>
513 {{ end }}
515 {{ define gotweb_render_tree(struct template *tp) }}
516 {!
517 struct request *c = tp->tp_arg;
518 struct transport *t = c->t;
519 struct repo_commit *rc = TAILQ_FIRST(&t->repo_commits);
520 !}
521 <header class='subtitle'>
522 <h2>Tree</h2>
523 </header>
524 <div id="tree_content">
525 <div id="tree_header_wrapper">
526 <dl id="tree_header">
527 <dt>Tree:</dt>
528 <dd><code class="commit-id">{{ rc->tree_id }}</code></dd>
529 <dt>Date:</dt>
530 <dd>
531 {{ render datetime(tp, rc->committer_time, TM_LONG) }}
532 </dd>
533 <dt>Message:</dt>
534 <dd class="commit-msg">{{ rc->commit_msg }}</dd>
535 </dl>
536 </div>
537 <hr />
538 <table id="tree">
539 {{ render got_output_repo_tree(c, gotweb_render_tree_item) }}
540 </table>
541 </div>
542 {{ end }}
544 {{ define gotweb_render_tree_item(struct template *tp,
545 struct got_tree_entry *te) }}
546 {!
547 struct request *c = tp->tp_arg;
548 struct transport *t = c->t;
549 struct querystring *qs = t->qs;
550 struct repo_commit *rc = TAILQ_FIRST(&t->repo_commits);
551 const char *modestr = "";
552 const char *name;
553 const char *folder;
554 char *dir = NULL;
555 mode_t mode;
556 struct gotweb_url url = {
557 .index_page = -1,
558 .page = -1,
559 .commit = rc->commit_id,
560 .path = qs->path,
561 };
563 name = got_tree_entry_get_name(te);
564 mode = got_tree_entry_get_mode(te);
566 folder = qs->folder ? qs->folder : "";
567 if (S_ISDIR(mode)) {
568 if (asprintf(&dir, "%s/%s", folder, name) == -1)
569 return (-1);
571 url.action = TREE;
572 url.folder = dir;
573 } else {
574 url.action = BLOB;
575 url.folder = folder;
576 url.file = name;
579 if (got_object_tree_entry_is_submodule(te))
580 modestr = "$";
581 else if (S_ISLNK(mode))
582 modestr = "@";
583 else if (S_ISDIR(mode))
584 modestr = "/";
585 else if (mode & S_IXUSR)
586 modestr = "*";
587 !}
588 <tr class="tree_wrapper">
589 {{ if S_ISDIR(mode) }}
590 <td class="tree_line" colspan=2>
591 <a href="{{ render gotweb_render_url(c, &url) }}">
592 {{ name }}{{ modestr }}
593 </a>
594 </td>
595 {{ else }}
596 <td class="tree_line">
597 <a href="{{ render gotweb_render_url(c, &url) }}">
598 {{ name }}{{ modestr }}
599 </a>
600 </td>
601 <td class="tree_line_blank">
602 {! url.action = COMMITS; !}
603 <a href="{{ render gotweb_render_url(c, &url) }}">
604 commits
605 </a>
606 {{ " | " }}
607 {! url.action = BLAME; !}
608 <a href="{{ render gotweb_render_url(c, &url) }}">
609 blame
610 </a>
611 </td>
612 {{ end }}
613 </tr>
614 {{ finally }}
615 {!
616 free(dir);
617 !}
618 {{ end }}
620 {{ define gotweb_render_tags(struct template *tp) }}
621 {!
622 struct request *c = tp->tp_arg;
623 struct transport *t = c->t;
624 struct querystring *qs = t->qs;
625 struct repo_tag *rt;
626 int commit_found;
628 commit_found = qs->commit == NULL;
629 !}
630 <header class='subtitle'>
631 <h2>Tags</h2>
632 </header>
633 <div id="tags_content">
634 {{ if t->tag_count == 0 }}
635 <div id="err_content">
636 This repository contains no tags
637 </div>
638 {{ else }}
639 {{ tailq-foreach rt &t->repo_tags entry }}
640 {{ if commit_found || !strcmp(qs->commit, rt->commit_id) }}
641 {! commit_found = 1; !}
642 {{ render tag_item(tp, rt) }}
643 {{ end }}
644 {{ end }}
645 {{ if t->next_id || t->prev_id }}
646 {! qs->action = TAGS; !}
647 {{ render gotweb_render_navs(tp) }}
648 {{ end }}
649 {{ end }}
650 </div>
651 {{ end }}
653 {{ define tag_item(struct template *tp, struct repo_tag *rt) }}
654 {!
655 struct request *c = tp->tp_arg;
656 struct transport *t = c->t;
657 struct repo_dir *repo_dir = t->repo_dir;
658 char *tag_name = rt->tag_name;
659 char *msg = rt->tag_commit;
660 char *nl;
661 struct gotweb_url url = {
662 .action = TAG,
663 .index_page = -1,
664 .page = -1,
665 .path = repo_dir->name,
666 .commit = rt->commit_id,
667 };
669 if (strncmp(tag_name, "refs/tags/", 10) == 0)
670 tag_name += 10;
672 if (msg) {
673 nl = strchr(msg, '\n');
674 if (nl)
675 *nl = '\0';
677 !}
678 <div class="tag_age">
679 {{ render datetime(tp, rt->tagger_time, TM_DIFF) }}
680 </div>
681 <div class="tag_name">{{ tag_name }}</div>
682 <div class="tag_log">
683 <a href="{{ render gotweb_render_url(c, &url) }}">
684 {{ msg }}
685 </a>
686 </div>
687 <div class="navs_wrapper">
688 <div class="navs">
689 <a href="{{ render gotweb_render_url(c, &url) }}">tag</a>
690 {{ " | " }}
691 {! url.action = BRIEFS; !}
692 <a href="{{ render gotweb_render_url(c, &url) }}">commit briefs</a>
693 {{ " | " }}
694 {! url.action = COMMITS; !}
695 <a href="{{ render gotweb_render_url(c, &url) }}">commits</a>
696 </div>
697 </div>
698 <hr />
699 {{ end }}
701 {{ define gotweb_render_tag(struct template *tp) }}
702 {!
703 struct request *c = tp->tp_arg;
704 struct transport *t = c->t;
705 struct repo_tag *rt;
706 const char *tag_name;
708 rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
709 tag_name = rt->tag_name;
711 if (strncmp(tag_name, "refs/", 5) == 0)
712 tag_name += 5;
713 !}
714 <header class="subtitle">
715 <h2>Tag</h2>
716 </header>
717 <div id="tags_content">
718 <div id="tag_header_wrapper">
719 <dl id="tag_header">
720 <dt>Commit:</dt>
721 <dd>
722 <code class="commit-id">{{ rt->commit_id }}</code>
723 {{ " " }}
724 <span class="refs_str">({{ tag_name }})</span>
725 </dd>
726 <dt>Tagger:</dt>
727 <dd>{{ rt->tagger }}</dd>
728 <dt>Date:</dt>
729 <dd>
730 {{ render datetime(tp, rt->tagger_time, TM_LONG)}}
731 </dd>
732 <dt>Message:</dt>
733 <dd class="commit-msg">{{ rt->commit_msg }}</dd>
734 </dl>
735 <hr />
736 <pre id="tag_commit">
737 {{ rt->tag_commit }}
738 </pre>
739 </div>
740 </div>
741 {{ end }}
743 {{ define gotweb_render_diff(struct template *tp) }}
744 {!
745 struct request *c = tp->tp_arg;
746 struct transport *t = c->t;
747 FILE *fp = t->fp;
748 struct repo_commit *rc = TAILQ_FIRST(&t->repo_commits);
749 char *line = NULL;
750 size_t linesize = 0;
751 ssize_t linelen;
752 !}
753 <header class="subtitle">
754 <h2>Commit Diff</h2>
755 </header>
756 <div id="diff_content">
757 <div id="diff_header_wrapper">
758 <dl id="diff_header">
759 <dt>Commit:</dt>
760 <dd><code class="commit-id">{{ rc->commit_id }}</code></dd>
761 <dt>From:</dt>
762 <dd>{{ rc->author }}</dd>
763 {{ if strcmp(rc->committer, rc->author) != 0 }}
764 <dt>Via:</dt>
765 <dd>{{ rc->committer }}</dd>
766 {{ end }}
767 <dt>Date:</dt>
768 <dd>
769 {{ render datetime(tp, rc->committer_time, TM_LONG) }}
770 </dd>
771 <dt>Message:</dt>
772 <dd class="commit-msg">{{ rc->commit_msg }}</dd>
773 </dl>
774 </div>
775 <hr />
776 <pre id="diff">
777 {{ while (linelen = getline(&line, &linesize, fp)) != -1 }}
778 {{ render diff_line(tp, line) }}
779 {{ end }}
780 </pre>
781 </div>
782 {{ finally }}
783 {! free(line); !}
784 {{ end }}
786 {{ define diff_line(struct template *tp, char *line )}}
787 {!
788 const char *color = NULL;
789 char *nl;
791 if (!strncmp(line, "-", 1))
792 color = "diff_minus";
793 else if (!strncmp(line, "+", 1))
794 color = "diff_plus";
795 else if (!strncmp(line, "@@", 2))
796 color = "diff_chunk_header";
797 else if (!strncmp(line, "commit +", 8) ||
798 !strncmp(line, "commit -", 8) ||
799 !strncmp(line, "blob +", 6) ||
800 !strncmp(line, "blob -", 6) ||
801 !strncmp(line, "file +", 6) ||
802 !strncmp(line, "file -", 6))
803 color = "diff_meta";
804 else if (!strncmp(line, "from:", 5) || !strncmp(line, "via:", 4))
805 color = "diff_author";
806 else if (!strncmp(line, "date:", 5))
807 color = "diff_date";
809 nl = strchr(line, '\n');
810 if (nl)
811 *nl = '\0';
812 !}
813 <span class="diff_line {{ color }}">{{ line }}</span>{{"\n"}}
814 {{ end }}
816 {{ define gotweb_render_branches(struct template *tp,
817 struct got_reflist_head *refs) }}
818 {!
819 struct got_reflist_entry *re;
820 !}
821 <header class='subtitle'>
822 <h2>Branches</h2>
823 </header>
824 <div id="branches_content">
825 {{ tailq-foreach re refs entry }}
826 {{ if !got_ref_is_symbolic(re->ref) }}
827 {{ render branch(tp, re) }}
828 {{ end }}
829 {{ end }}
830 </div>
831 {{ end }}
833 {{ define branch(struct template *tp, struct got_reflist_entry *re) }}
834 {!
835 const struct got_error *err;
836 struct request *c = tp->tp_arg;
837 struct querystring *qs = c->t->qs;
838 const char *refname;
839 time_t age;
840 struct gotweb_url url = {
841 .action = SUMMARY,
842 .index_page = -1,
843 .page = -1,
844 .path = qs->path,
845 };
847 refname = got_ref_get_name(re->ref);
849 err = got_get_repo_age(&age, c, refname);
850 if (err) {
851 log_warnx("%s: %s", __func__, err->msg);
852 return -1;
855 if (strncmp(refname, "refs/heads/", 11) == 0)
856 refname += 11;
858 url.headref = refname;
859 !}
860 <section class="branches_wrapper">
861 <div class="branches_age">
862 {{ render datetime(tp, age, TM_DIFF) }}
863 </div>
864 <div class="branch">
865 <a href="{{ render gotweb_render_url(c, &url) }}">{{ refname }}</a>
866 </div>
867 <div class="navs_wrapper">
868 <div class="navs">
869 <a href="{{ render gotweb_render_url(c, &url) }}">summary</a>
870 {{" | "}}
871 {! url.action = BRIEFS; !}
872 <a href="{{ render gotweb_render_url(c, &url) }}">commit briefs</a>
873 {{" | "}}
874 {! url.action = COMMITS; !}
875 <a href="{{ render gotweb_render_url(c, &url) }}">commits</a>
876 </div>
877 </div>
878 <hr />
879 </section>
880 {{ end }}
882 {{ define gotweb_render_summary(struct template *tp) }}
883 {!
884 struct request *c = tp->tp_arg;
885 struct server *srv = c->srv;
886 struct transport *t = c->t;
887 struct got_reflist_head *refs = &t->refs;
888 !}
889 <dl id="summary_wrapper">
890 {{ if srv->show_repo_description }}
891 <dt>Description:</dt>
892 <dd>{{ t->repo_dir->description }}</dd>
893 {{ end }}
894 {{ if srv->show_repo_owner }}
895 <dt>Owner:</dt>
896 <dd>{{ t->repo_dir->owner }}</dd>
897 {{ end }}
898 {{ if srv->show_repo_age }}
899 <dt>Last Change:</dt>
900 <dd>
901 {{ render datetime(tp, t->repo_dir->age, TM_DIFF) }}
902 </dd>
903 {{ end }}
904 {{ if srv->show_repo_cloneurl }}
905 <dt>Clone URL:</dt>
906 <dd><pre class="clone-url">{{ t->repo_dir->url }}</pre></dd>
907 {{ end }}
908 </dl>
909 {{ render gotweb_render_briefs(tp) }}
910 {{ render gotweb_render_tags(tp) }}
911 {{ render gotweb_render_branches(tp, refs) }}
912 {{ end }}
914 {{ define gotweb_render_blame(struct template *tp) }}
915 {!
916 const struct got_error *err;
917 struct request *c = tp->tp_arg;
918 struct transport *t = c->t;
919 struct repo_commit *rc = TAILQ_FIRST(&t->repo_commits);
920 !}
921 <header class="subtitle">
922 <h2>Blame</h2>
923 </header>
924 <div id="blame_content">
925 <div id="blame_header_wrapper">
926 <dl id="blame_header">
927 <dt>Date:</dt>
928 <dd>
929 {{ render datetime(tp, rc->committer_time, TM_LONG) }}
930 </dd>
931 <dt>Message:</dt>
932 <dd class="commit-msg">{{ rc->commit_msg }}</dd>
933 </dl>
934 </div>
935 <hr />
936 <pre id="blame">
937 {!
938 err = got_output_file_blame(c, &blame_line);
939 if (err && err->code != GOT_ERR_CANCELLED)
940 log_warnx("%s: got_output_file_blame: %s", __func__,
941 err->msg);
942 if (err)
943 return (-1);
944 !}
945 </pre>
946 </div>
947 {{ end }}
949 {{ define blame_line(struct template *tp, const char *line,
950 struct blame_line *bline, int lprec, int lcur) }}
951 {!
952 struct request *c = tp->tp_arg;
953 struct transport *t = c->t;
954 struct repo_dir *repo_dir = t->repo_dir;
955 char *committer, *s;
956 struct gotweb_url url = {
957 .action = DIFF,
958 .index_page = -1,
959 .page = -1,
960 .path = repo_dir->name,
961 .commit = bline->id_str,
962 };
964 s = strchr(bline->committer, '<');
965 committer = s ? s + 1 : bline->committer;
967 s = strchr(committer, '@');
968 if (s)
969 *s = '\0';
970 !}
971 <div class="blame_wrapper">
972 <div class="blame_number">{{ printf "%.*d", lprec, lcur }}</div>
973 <div class="blame_hash">
974 <a href="{{ render gotweb_render_url(c, &url) }}">
975 {{ printf "%.8s", bline->id_str }}
976 </a>
977 </div>
978 <div class="blame_date">{{ bline->datebuf }}</div>
979 <div class="blame_author">{{ printf "%.9s", committer }}</div>
980 <div class="blame_code">{{ line }}</div>
981 </div>
982 {{ end }}
984 {{ define gotweb_render_rss(struct template *tp) }}
985 {!
986 struct request *c = tp->tp_arg;
987 struct server *srv = c->srv;
988 struct transport *t = c->t;
989 struct repo_dir *repo_dir = t->repo_dir;
990 struct repo_tag *rt;
991 struct gotweb_url summary = {
992 .action = SUMMARY,
993 .index_page = -1,
994 .page = -1,
995 .path = repo_dir->name,
996 };
997 !}
998 <?xml version="1.0" encoding="UTF-8"?>
999 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
1000 <channel>
1001 <title>Tags of {{ repo_dir->name }}</title>
1002 <link>
1003 <![CDATA[
1004 {{ render gotweb_render_absolute_url(c, &summary) }}
1005 ]]>
1006 </link>
1007 {{ if srv->show_repo_description }}
1008 <description>{{ repo_dir->description }}</description>
1009 {{ end }}
1010 {{ tailq-foreach rt &t->repo_tags entry }}
1011 {{ render rss_tag_item(tp, rt) }}
1012 {{ end }}
1013 </channel>
1014 </rss>
1015 {{ end }}
1017 {{ define rss_tag_item(struct template *tp, struct repo_tag *rt) }}
1019 struct request *c = tp->tp_arg;
1020 struct transport *t = c->t;
1021 struct repo_dir *repo_dir = t->repo_dir;
1022 struct tm tm;
1023 char rfc822[128];
1024 int r;
1025 char *tag_name = rt->tag_name;
1026 struct gotweb_url tag = {
1027 .action = TAG,
1028 .index_page = -1,
1029 .page = -1,
1030 .path = repo_dir->name,
1031 .commit = rt->commit_id,
1034 if (strncmp(tag_name, "refs/tags/", 10) == 0)
1035 tag_name += 10;
1037 if (gmtime_r(&rt->tagger_time, &tm) == NULL)
1038 return -1;
1039 r = strftime(rfc822, sizeof(rfc822), "%a, %d %b %Y %H:%M:%S GMT", &tm);
1040 if (r == 0)
1041 return 0;
1043 <item>
1044 <title>{{ repo_dir->name }} {{" "}} {{ tag_name }}</title>
1045 <link>
1046 <![CDATA[
1047 {{ render gotweb_render_absolute_url(c, &tag) }}
1048 ]]>
1049 </link>
1050 <description>
1051 <![CDATA[<pre>{{ rt->tag_commit }}</pre>]]>
1052 </description>
1053 {{ render rss_author(tp, rt->tagger) }}
1054 <guid isPermaLink="false">{{ rt->commit_id }}</guid>
1055 <pubDate>
1056 {{ rfc822 }}
1057 </pubDate>
1058 </item>
1059 {{ end }}
1061 {{ define rss_author(struct template *tp, char *author) }}
1063 char *t, *mail;
1065 /* what to do if the author name contains a paren? */
1066 if (strchr(author, '(') != NULL || strchr(author, ')') != NULL)
1067 return 0;
1069 t = strchr(author, '<');
1070 if (t == NULL)
1071 return 0;
1072 *t = '\0';
1073 mail = t+1;
1075 while (isspace((unsigned char)*--t))
1076 *t = '\0';
1078 t = strchr(mail, '>');
1079 if (t == NULL)
1080 return 0;
1081 *t = '\0';
1083 <author>
1084 {{ mail }} {{" "}} ({{ author }})
1085 </author>
1086 {{ end }}