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 getCommitDiff(repository, hash) { |
12 | let diff = execute(repository, `show --pretty="" --numstat ${hash} --`).trim().split('\n'); |
13 | return diff; |
14 | } |
15 |
|
16 | function 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 { |
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 |
|
34 | function 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 |
|
53 | module.exports = { |
54 | getCommitDiff: getCommitDiff, |
55 | getCommitNumber: getCommitNumber, |
56 | getLatestCommit: getLatestCommit, |
57 | listCommits: listCommits |
58 | } |
59 |
|