Index

joshstock.in / 8d865bc

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

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
7621 Jan 2020 21:10e9bc72eFix broken try-catch block in gitpager sourceJosh Stockin111N

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

application/javascript1790 bytesdownload raw
1const child_process = require('child_process');
2
3function execute(repository, command) {
4 return child_process.execSync(`git --git-dir=${repository} ${command}`).toString();
5}
6
7function getCommitNumber(repository, hash) {
8 return parseInt(execute(repository, `rev-list --count ${hash} --`).trim());
9}
10
11function getCommitDiff(repository, hash) {
12 let diff = execute(repository, `show --pretty="" --numstat ${hash} --`).trim().split('\n');
13 return diff;
14}
15
16function getLatestCommit(repository, branch) {
17 let commit_raw;
18 try {
19 commit_raw = execute(repository, `log ${branch || 'HEAD'} --pretty=format:'%aI ## %H ## %an ## %ae ## %s ## %b' -n 1 --`).split(' ## ');
20 } catch (e) {
21 return null;
22 }
23 let commit = {};
24 commit.date = commit_raw[0];
25 commit.hash = commit_raw[1];
26 commit.number = getCommitNumber(repository, commit.hash);
27 commit.author = commit_raw[2];
28 commit.email = commit_raw[3];
29 commit.subject = commit_raw[4];
30 commit.body = commit_raw[5];
31 return commit;
32}
33
34function listCommits(repository, branch, page) {
35 let commits_raw = execute(repository, `log ${branch} --pretty=format:'%aI ## %H ## %an ## %ae ## %s' -n 20 --skip=${20*(page-1)} --`).trim();
36 if (commits_raw.length == 0) return null;
37 commits_raw = commits_raw.split('\n');
38 let commits = [];
39 for (line in commits_raw) {
40 let commit_raw = commits_raw[line].split(' ## ');
41 let commit = {};
42 commit.date = commit_raw[0];
43 commit.hash = commit_raw[1];
44 commit.number = getCommitNumber(repository, commit.hash);
45 commit.author = commit_raw[2];
46 commit.email = commit_raw[3];
47 commit.subject = commit_raw[4];
48 commits.push(commit);
49 }
50 return commits;
51}
52
53module.exports = {
54 getCommitDiff: getCommitDiff,
55 getCommitNumber: getCommitNumber,
56 getLatestCommit: getLatestCommit,
57 listCommits: listCommits
58}
59