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