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 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 log $BRANCH --pretty=format:'%aI ## %H ## %an ## %ae ## %s ## %b' -n 1 --"; |
17 | function get_commit(repository, branch="HEAD", body=true) { |
18 | call = get_commit_command.replace("$REPOSITORY", repository).replace("$BRANCH", branch); |
19 | let properties; |
20 | try { |
21 | properties = exec(call).split(" ## "); |
22 | } catch (e) { |
23 | return null; |
24 | } |
25 | commit = {}; |
26 | commit.number = count_commits(repository, properties[1]); |
27 | commit.date = properties[0]; |
28 | commit.hash = properties[1]; |
29 | commit.author = properties[2]; |
30 | commit.author_email = properties[3]; |
31 | commit.subject = properties[4]; |
32 | if (body) commit.body = properties[5].trim(); |
33 | return commit; |
34 | } |
35 |
|
36 | const list_commits_command = `git --git-dir $REPOSITORY log $BRANCH --pretty=format:'%aI ## %H ## %an ## %ae ## %s' -n ${skip_count} --skip=$SKIP --`; |
37 | function list_commits(repository, branch="HEAD", skip=0) { |
38 | call = list_commits_command.replace("$REPOSITORY", repository).replace("$BRANCH", branch).replace("$SKIP", skip*skip_count); |
39 | output = exec(call); |
40 | if (output == '') return null; |
41 | lines = output.split("\n"); |
42 | commits = []; |
43 | for (let i = 0; i < lines.length; i++) { |
44 | properties = lines[i].split(" ## "); |
45 | commit = {}; |
46 | commit.number = count_commits(repository, properties[1]); |
47 | commit.date = properties[0]; |
48 | commit.hash = properties[1]; |
49 | commit.author = properties[2]; |
50 | commit.author_email = properties[3]; |
51 | commit.subject = properties[4]; |
52 | commits.push(commit); |
53 | } |
54 | return commits; |
55 | } |
56 |
|
57 | module.exports = { |
58 | count_commits: count_commits |
59 | ,get_commit: get_commit |
60 | ,list_commits: list_commits |
61 | } |
62 |
|