Index

joshstock.in / 7d8bf7d

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

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
6505 Jan 2020 15:20552e945Complete gitpager sourceJosh Stockin16312N

Blob @ joshstock.in / gitpager / index.js

application/javascript4970 bytesdownload raw
1const fs = require("fs");
2const path = require("path");
3const express = require("express");
4const app = express();
5const port = 8080;
6const gitcontrol = require(path.resolve(__dirname, "gitcontrol"))
7
8const allowed_repos = {"lognestmonster": "dev", "joshstock.in": "master", "auto-plow": "dev"};
9
10function repo_path(repo) {
11 if (process.env.NODE_ENV == "production") {
12 return "/home/git/" + repo + ".git/";
13 } else {
14 return "/home/josh/Desktop/" + repo + "/.git";
15 }
16}
17
18let commitFile = fs.readFileSync(path.resolve(__dirname, "commit.html")).toString();
19function format_commit(repo, commit, isgithub=false, islatest=false) {
20 commitString = commitFile;
21 if (isgithub) {
22 commitString = commitString.replace("$shorthash", "<a title=\"GitHub mirror\" href=\"https://github.com/JoshuaS3/" + repo + "/commit/" + commit.hash + "\">" + commit.hash.substring(0,7) + " (" + allowed_repos[repo] + "/" + commit.number + ")</a>");
23 commitString = commitString.replace("$onclick", "");
24 } else {
25 commitString = commitString.replace("$shorthash", commit.hash.substring(0,7) + " (" + allowed_repos[repo] + "/" + commit.number + ")");
26 commitString = commitString.replace("$onclick", "onclick=\"window.location='/$repo/$hash'\"");
27 }
28 commitString = commitString.replace("$repo", repo).replace("$hash", commit.hash)
29 .replace("$subject", commit.subject).replace("$author", commit.author).replace("$author_email", commit.author_email);
30 commitTime = new Date(commit.date);
31 time = commitTime.toDateString().split(" ").slice(1).join(" ") + ", " + commitTime.toLocaleTimeString().split(" ")[0];
32 if (islatest) time += " (latest commit)";
33 commitString = commitString.replace("$date", time);
34 if (commit.body == undefined) {
35 commitString = commitString.replace("$body", "");
36 } else {
37 commitString = commitString.replace("$body", "<pre class=\"message\">" + (commit.body || "[[no commit body]]") + "</pre>");
38 }
39 return commitString;
40}
41
42app.use(express.static(path.resolve(__dirname, "static")))
43
44let indexFile = fs.readFileSync(path.resolve(__dirname, "index.html")).toString();
45app.get("/", function(req, res) {
46 response = indexFile;
47 for (repo in allowed_repos) {
48 response = response.replace("$commit_" + repo, format_commit(repo, gitcontrol.get_commit(repo_path(repo), "HEAD", false), false, true));
49 }
50 res.send(response);
51});
52
53let listingFile = fs.readFileSync(path.resolve(__dirname, "list_page.html")).toString();
54app.get("/:repo/:page(\\d+)?", function(req, res) {
55 if (allowed_repos[req.params.repo] == null) {
56 res.status(404).send()
57 return;
58 }
59 req.params.page = parseInt(req.params.page) || 1;
60
61 response = listingFile;
62
63 commits = gitcontrol.list_commits(repo_path(req.params.repo), allowed_repos[req.params.repo], req.params.page-1);
64 total = gitcontrol.count_commits(repo_path(req.params.repo));
65 if (commits == null) {
66 res.send(404);
67 return;
68 } else {
69 commitsFormatted = "";
70 lastDate = "";
71 for (commit in commits) {
72 dateString = new Date(commits[commit].date).toLocaleDateString("en-US", {month:"long",day:"numeric",year:"numeric"});
73 if (dateString != lastDate) {
74 lastDate = dateString;
75 commitsFormatted += "<h2 style=\"margin-bottom:4px\">" + dateString + "</h2>";
76 }
77 commitsFormatted += format_commit(req.params.repo, commits[commit], false, commits[commit].number == total);
78 }
79 response = response.replace("$commits", commitsFormatted);
80 countLast = commits[commits.length-1].number;
81 lastPage = "";
82 nextPage = "";
83 if (req.params.page > 1) {
84 lastPage = `<a href="/${req.params.repo}/${req.params.page-1}" style="margin-right:10px;"><< previous page</a>`;
85 }
86 if (countLast > 1) {
87 nextPage = `<a href="/${req.params.repo}/${req.params.page+1}" style="margin-right:10px;">next page >></a>`;
88 }
89 if (lastPage != "" || nextPage != "") {
90 response = response.replace(/\$pagecontrols/g, `<p style="text-align:center">${lastPage}${nextPage}</p>`);
91 }
92 }
93 response = response.replace(/\$repo/g, req.params.repo);
94 stats = `on branch ${allowed_repos[req.params.repo]} with ${total} commits`;
95 response = response.replace("$stats", stats);
96
97 res.send(response);
98});
99
100let commitPage = fs.readFileSync(path.resolve(__dirname, "commit_page.html")).toString();
101app.get("/:repo/:commit([a-f0-9]{40})", function(req, res) {
102 if (allowed_repos[req.params.repo] == null) {
103 res.status(404).send()
104 return;
105 }
106 commit = gitcontrol.get_commit(repo_path(req.params.repo), req.params.commit);
107 if (commit == null) res.send(404);
108 response = commitPage;
109 response = response.replace(/\$repo/g, req.params.repo);
110 response = response.replace(/\$hash/g, commit.hash);
111 response = response.replace(/\$shorthash/g, commit.hash.substring(0,7));
112 response = response.replace(/\$subject/g, commit.subject);
113 total = gitcontrol.count_commits(repo_path(req.params.repo));
114 response = response.replace("$commit", format_commit(req.params.repo, commit, true, commit.number == total));
115 res.send(response);
116});
117
118app.listen(port);
119