Repository navigation
Expand file tree
/
Copy pathparse.js
More file actions
101 lines (84 loc) · 2.77 KB
/
Copy pathparse.js
File metadata and controls
101 lines (84 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
const simpleGit = require('simple-git');
const fs = require('fs');
// 本地 Git 仓库路径
const REPO_PATH = 'D:/Data/Godot/Polar';
const git = simpleGit(REPO_PATH);
async function getGitData() {
console.log(`Analyzing repo at ${REPO_PATH}...`);
// 1. 获取所有日志
const log = await git.log({
'--all': null,
//'--date': 'iso',
'--stat': null,
'format': {
'hash': '%H',
'parents': '%P',
'author': '%an',
'date': '%ai',
'message': '%s'
}
});
const commits = [];
const commitMap = new Map();
log.all.forEach(entry => {
const { hash, parents, author, date, message, diff } = entry;
const parentHashes = parents.split(' ').filter(p => p.length > 0);
const commit = {
id: hash,
hash: hash,
parents: parentHashes,
author: author,
date: date,
message: message,
stats: {
insertions: diff ? diff.insertions : 0,
deletions: diff ? diff.deletions : 0,
total_changes: diff ? (diff.insertions + diff.deletions) : 0
}
};
commits.push(commit);
commitMap.set(hash, commit);
});
const links = [];
commits.forEach(commit => {
commit.parents.forEach(parentHash => {
if (commitMap.has(parentHash)) {
links.push({
source: commit.id,
target: parentHash
});
}
});
});
const branches = await git.branch(['--all']);
const rawTagRefs = await git.raw(['show-ref', '--tags']);
const tagRefs = rawTagRefs
? rawTagRefs.split('\n')
.filter(line => line.length > 0)
.map(line => {
const [hash, ref] = line.split(' ');
return { hash, ref };
})
: [];
const refs = {};
branches.all.forEach(branchName => {
const commitHash = branches.branches[branchName].commit;
if (!refs[commitHash]) refs[commitHash] = [];
const cleanName = branchName.replace(/^remotes\//, '');
refs[commitHash].push(cleanName);
});
tagRefs.forEach(tag => {
const commitHash = tag.hash;
const tagName = tag.ref.replace('refs/tags/', '');
if (!refs[commitHash]) refs[commitHash] = [];
refs[commitHash].push(`tag: ${tagName}`);
});
const gitGraph = {
nodes: commits,
links: links,
refs: refs
};
fs.writeFileSync('./git-data.json', JSON.stringify(gitGraph, null, 2));
console.log('Successfully created git-data.json!');
}
getGitData().catch(console.error);