Index

joshstock.in / b2d3957

Source for serving and static templating/compiling of https://joshstock.in.

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
7926 Jan 2020 17:34d75af6aAdd timezone to date string on commitlist sectionsJosh Stockin111N

Blob @ joshstock.in / gitpager / src / pages.js

application/javascript5121 bytesdownload raw
1const config = require('./config');
2const template = require('./templates');
3const git = require('./git');
4
5function index() {
6 let repoListings = '';
7 for (name in config.repositories) {
8 let repository = config.repositories[name];
9
10 let commit = git.getLatestCommit(repository.location, repository.branch);
11 let stats = `${commit.hash.substring(0,7)} (${repository.branch}/${commit.number})`;
12 let commitTime = new Date(commit.date);
13 let date = commitTime.toLocaleDateString('en-US', {timeZone:'America/Chicago',hourCycle:'h24',month:'short',day:'numeric',year:'numeric',hour:'numeric',minute:'numeric',second:'numeric'}) + ' (latest commit)';
14
15 let latest = template(config.templates.inline_commit, {'name': name, 'hash': commit.hash, 'shorthash': stats, 'date': date, 'subject': commit.subject, 'author': commit.author, 'email': commit.email});
16 let listing = template(config.templates.inline_repository, {'name': name, 'description': repository.description, 'github': repository.github, 'latest': latest});
17
18 repoListings += listing;
19 }
20 return template(config.templates.index, {'repositories': repoListings});
21}
22
23function repository(repo, page) {
24 let branch = config.repositories[repo].branch;
25 let commits = git.listCommits(config.repositories[repo].location, branch, page);
26 if (commits == null) return null;
27 let commitList = '';
28 let lastDate = '';
29 for (i in commits) {
30 let commit = commits[i];
31
32 let stats = `${commit.hash.substring(0,7)} (${branch}/${commit.number})`;
33 let commitTime = new Date(commit.date);
34 let date = commitTime.toLocaleDateString('en-US', {timeZone:'America/Chicago',hourCycle:'h24',month:'short',day:'numeric',year:'numeric',hour:'numeric',minute:'numeric',second:'numeric'});
35
36 let inline = template(config.templates.inline_commit, {'name': repo, 'hash': commit.hash, 'shorthash': stats, 'date': date, 'subject': commit.subject, 'author': commit.author, 'email': commit.email});
37
38 let dayString = commitTime.toLocaleDateString('en-US', {timeZone:'America/Chicago',month:'long',day:'numeric',year:'numeric'});
39 if (dayString != lastDate) {
40 lastDate = dayString;
41 commitList += '<h2 class=\"date category\">' + dayString + '</h2>';
42 }
43 commitList += inline;
44 }
45 let pagePrevious;
46 let pageNext;
47 if (page > 1) {
48 pagePrevious = `<a href="/${repo}/${page-1}" style="margin-right:10px;"><< previous page</a>`;
49 }
50 if (commits[commits.length-1].number > 1) {
51 pageNext = `<a href="/${repo}/${page+1}" style="margin-right:10px;">next page >></a>`;
52 }
53 pageControls = `<p style="text-align:center">${pagePrevious || ""}${pageNext || ""}</p>`
54 let commitCount = git.getCommitNumber(config.repositories[repo].location, branch);
55 let stats = `on branch ${branch} with ${commitCount} commits`;
56 return template(config.templates.repository, {'repo': repo, 'commits': commitList, 'pagecontrols': pageControls, 'stats': stats, 'github': config.repositories[repo].github});
57}
58
59function commit(repo, hash) {
60 let repositoryConfig = config.repositories[repo];
61 let commit = git.getLatestCommit(repositoryConfig.location, hash);
62 if (commit == null) return null;
63 let commitTime = new Date(commit.date);
64 let date = commitTime.toLocaleDateString('en-US', {timeZone:'America/Chicago',hourCycle:'h24',month:'short',day:'numeric',year:'numeric',hour:'numeric',minute:'numeric',second:'numeric'});
65 let stats = `${hash.substring(0,7)} (${repositoryConfig.branch}/${commit.number})`
66 let commitCount = git.getCommitNumber(repositoryConfig.location, repositoryConfig.branch);
67 let repoStats = `on branch ${repositoryConfig.branch} with ${commitCount} commits`;
68 let diff = git.getCommitDiff(repositoryConfig.location, commit.hash);
69 let max = 0;
70 for (change in diff) {
71 diff[change] = diff[change].split('\t');
72 diff[change][0] = parseInt(diff[change][0]);
73 diff[change][1] = parseInt(diff[change][1]);
74 let sum = diff[change][0] + diff[change][1];
75 if (sum > max) {
76 max = sum;
77 }
78 }
79 let diffString = '';
80 for (change in diff) {
81 let thisDiff = '';
82 thisDiff += diff[change][2] + ' ';
83 let plusCount = 0;
84 if (diff[change][0] + diff[change][1] < 60) {
85 if (diff[change][0] > 0) {
86 thisDiff += '<span style="color:lawngreen">' + '+'.repeat(diff[change][0]) + '</span>';
87 }
88 if (diff[change][1] > 0) {
89 thisDiff += '<span style="color:red">' + '-'.repeat(diff[change][1]) + '</span>';
90 }
91 } else {
92 if (diff[change][0] > 0) {
93 thisDiff += '<span style="color:lawngreen">' + '+'.repeat(Math.floor(((diff[change][0] || 1)/max)*60)) + '</span>';
94 }
95 if (diff[change][1] > 0) {
96 thisDiff += '<span style="color:red">' + '-'.repeat(Math.floor(((diff[change][1] || 1)/max)*60)) + '</span>';
97 }
98 }
99 thisDiff += '</span>';
100 diffString += `${thisDiff}\n`
101 }
102 return template(config.templates.commit, {'repo': repo, 'github': repositoryConfig.github, 'statsRepo': repoStats, 'shorthash': stats, 'hash': hash, 'date': date, 'subject': commit.subject, 'author': commit.author, 'email': commit.email, 'body': commit.body || '[[no commit body]]', 'diffs': diffString});
103}
104
105module.exports = {
106 index: index,
107 repository: repository,
108 commit: commit
109}
110