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