-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgitAnalyzer.js
More file actions
223 lines (200 loc) · 5.98 KB
/
Copy pathgitAnalyzer.js
File metadata and controls
223 lines (200 loc) · 5.98 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
const simpleGit = require('simple-git');
const path = require('path');
class GitAnalyzer {
constructor(repoPath = process.cwd()) {
this.git = simpleGit(repoPath);
this.repoPath = repoPath;
}
/**
* 获取暂存区的变更内容
*/
async getStagedDiff() {
try {
const diff = await this.git.diff(['--staged']);
return diff;
} catch (error) {
throw new Error(`获取暂存区差异失败: ${error.message}`);
}
}
/**
* 获取工作区的变更内容
*/
async getWorkingDiff() {
try {
const diff = await this.git.diff();
return diff;
} catch (error) {
throw new Error(`获取工作区差异失败: ${error.message}`);
}
}
/**
* 获取变更的文件列表
*/
async getChangedFiles() {
try {
const status = await this.git.status();
return {
staged: status.staged,
modified: status.modified,
created: status.created,
deleted: status.deleted,
renamed: status.renamed
};
} catch (error) {
throw new Error(`获取文件状态失败: ${error.message}`);
}
}
/**
* 获取最近的提交历史
*/
async getRecentCommits(count = 5) {
try {
const log = await this.git.log(['--oneline', `-${count}`]);
return log.all.map(commit => ({
hash: commit.hash,
message: commit.message,
date: commit.date
}));
} catch (error) {
throw new Error(`获取提交历史失败: ${error.message}`);
}
}
/**
* 分析变更类型
*/
async analyzeChanges() {
const changes = await this.getChangedFiles();
const diff = await this.getStagedDiff() || await this.getWorkingDiff();
let analysis = {
type: 'chore',
scope: '',
files: [],
stats: {
additions: 0,
deletions: 0,
filesChanged: 0
}
};
// 统计文件变更
const allChangedFiles = [
...changes.staged,
...changes.modified,
...changes.created,
...changes.deleted
];
analysis.files = [...new Set(allChangedFiles)];
analysis.stats.filesChanged = analysis.files.length;
// 从diff中提取统计信息
if (diff) {
const lines = diff.split('\n');
analysis.stats.additions = lines.filter(line => line.startsWith('+')).length;
analysis.stats.deletions = lines.filter(line => line.startsWith('-')).length;
}
// 分析变更类型
if (changes.created.length > 0) {
analysis.type = 'feat';
} else if (changes.deleted.length > 0) {
analysis.type = 'feat';
} else if (analysis.files.some(file => file.includes('test'))) {
analysis.type = 'test';
} else if (analysis.files.some(file => file.includes('doc') || file.includes('README'))) {
analysis.type = 'docs';
} else if (analysis.files.some(file => file.includes('config') || file.includes('package.json'))) {
analysis.type = 'chore';
} else {
analysis.type = 'feat';
}
// 确定作用域
const commonPaths = analysis.files.map(file => {
const parts = file.split('/');
return parts.length > 1 ? parts[0] : '';
});
const scopeCounts = {};
commonPaths.forEach(scope => {
if (scope) {
scopeCounts[scope] = (scopeCounts[scope] || 0) + 1;
}
});
if (Object.keys(scopeCounts).length > 0) {
analysis.scope = Object.keys(scopeCounts).reduce((a, b) =>
scopeCounts[a] > scopeCounts[b] ? a : b
);
}
return analysis;
}
/**
* 检查是否在Git仓库中
*/
async isGitRepo() {
try {
await this.git.status();
return true;
} catch (error) {
return false;
}
}
/**
* 获取当前分支名
*/
async getCurrentBranch() {
try {
const status = await this.git.status();
return status.current;
} catch (error) {
return 'unknown';
}
}
/**
* 检查是否有变更
*/
async hasChanges() {
try {
const status = await this.git.status();
return status.files.length > 0;
} catch (error) {
return false;
}
}
/**
* 获取diff内容(优先暂存区,否则工作区)
*/
async getDiff() {
try {
const stagedDiff = await this.getStagedDiff();
if (stagedDiff && stagedDiff.trim()) {
return stagedDiff;
}
return await this.getWorkingDiff();
} catch (error) {
throw new Error(`获取diff失败: ${error.message}`);
}
}
/**
* 获取Git状态
*/
async getStatus() {
try {
return await this.git.status();
} catch (error) {
throw new Error(`获取Git状态失败: ${error.message}`);
}
}
/**
* 提交变更
*/
async commitChanges(message) {
try {
// 检查是否有暂存的文件
const status = await this.git.status();
if (status.staged.length === 0 && status.files.length > 0) {
// 如果没有暂存文件但有变更,先暂存所有文件
await this.git.add('.');
}
await this.git.commit(message);
return true;
} catch (error) {
throw new Error(`提交失败: ${error.message}`);
}
}
}
module.exports = GitAnalyzer;