1 | const path = require("path"); |
2 | const child_process = require("child_process"); |
3 |
|
4 | function exec(commandstring) { |
5 | return child_process.execSync(commandstring).toString(); |
6 | } |
7 |
|
8 | const skip_count = 20; |
9 |
|
10 | const count_commits_command = "git --git-dir $REPOSITORY/.git rev-list --count $BRANCH"; |
11 | function count_commits(repository, branch="HEAD") { |
12 | call = count_commits_command.replace("$REPOSITORY", repository).replace("$BRANCH", branch); |
13 | return exec(call).trim(); |
14 | } |
15 |
|
16 | const get_commit_command = "git --git-dir $REPOSITORY/.git log $BRANCH --pretty=format:'%aI ## %H ## %an ## %ae ## %s ## %b' -n 1 --"; |
17 | function get_commit(repository, branch="HEAD") { |
18 | call = get_commit_command.replace("$REPOSITORY", repository).replace("$BRANCH", branch); |
19 | properties = exec(call).split(" ## "); |
20 | commit = {}; |
21 | commit.number = count_commits(repository, properties[1]); |
22 | commit.date = properties[0]; |
23 | commit.hash = properties[1]; |
24 | commit.author = properties[2]; |
25 | commit.author_email = properties[3]; |
26 | commit.subject = properties[4]; |
27 | commit.body = properties[5].trim(); |
28 | return commit; |
29 | } |
30 |
|
31 | const list_commits_command = `git --git-dir $REPOSITORY/.git log $BRANCH --pretty=format:'%aI ## %H ## %an ## %ae ## %s' -n ${skip_count} --skip=$SKIP --`; |
32 | function list_commits(repository, branch="HEAD", skip=0) { |
33 | call = list_commits_command.replace("$REPOSITORY", repository).replace("$BRANCH", branch).replace("$SKIP", skip*skip_count); |
34 | commits = []; |
35 | lines = exec(call).split("\n"); |
36 | if (lines == ['']) return commits; |
37 | for (let i = 0; i < lines.length; i++) { |
38 | properties = lines[i].split(" ## "); |
39 | commit = {}; |
40 | commit.number = count_commits(repository, properties[1]); |
41 | commit.date = properties[0]; |
42 | commit.hash = properties[1]; |
43 | commit.author = properties[2]; |
44 | commit.author_email = properties[3]; |
45 | commit.subject = properties[4]; |
46 | commits.push(commit); |
47 | } |
48 | return commits; |
49 | } |
50 |
|
51 | module.exports = { |
52 | count_commits: count_commits |
53 | ,get_commit: get_commit |
54 | ,list_commits: list_commits |
55 | } |
56 |
|