create-branch-cli 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +66 -4
- package/index.js +169 -7
- package/package.json +1 -2
package/README.md
CHANGED
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
- 👤 首次使用自动保存用户信息(工号、姓名)
|
|
10
10
|
- 🐛 支持 feature 和 hotFix 两种分支类型
|
|
11
11
|
- 📅 自动添加日期后缀
|
|
12
|
+
- 💾 支持保存需求唯一标识,快速提交和推送
|
|
12
13
|
|
|
13
14
|
## 分支命名规则
|
|
14
15
|
|
|
@@ -54,21 +55,67 @@ npm install -g create-branch-cli
|
|
|
54
55
|
### 创建 Feature 分支
|
|
55
56
|
|
|
56
57
|
```bash
|
|
58
|
+
# 仅创建分支
|
|
57
59
|
create-branch 用户登录功能
|
|
58
60
|
# 或使用别名
|
|
59
61
|
cb 用户登录功能
|
|
62
|
+
|
|
63
|
+
# 创建分支并保存需求唯一标识(用于后续快速提交)
|
|
64
|
+
create-branch 用户登录功能 PROJ-123
|
|
65
|
+
# 或使用别名
|
|
66
|
+
cb 用户登录功能 PROJ-123
|
|
60
67
|
```
|
|
61
68
|
|
|
62
69
|
### 创建 HotFix 分支
|
|
63
70
|
|
|
64
71
|
```bash
|
|
72
|
+
# 仅创建分支
|
|
65
73
|
create-branch 修复登录bug -b
|
|
66
74
|
# 或
|
|
67
75
|
create-branch 修复登录bug --bug
|
|
68
76
|
# 或使用别名
|
|
69
77
|
cb 修复登录bug -b
|
|
78
|
+
|
|
79
|
+
# 创建分支并保存需求唯一标识
|
|
80
|
+
create-branch 修复登录bug PROJ-124 -b
|
|
81
|
+
# 或使用别名
|
|
82
|
+
cb 修复登录bug PROJ-124 -b
|
|
70
83
|
```
|
|
71
84
|
|
|
85
|
+
### 快速提交和推送
|
|
86
|
+
|
|
87
|
+
如果创建分支时输入了需求唯一标识,可以使用以下命令快速提交和推送:
|
|
88
|
+
|
|
89
|
+
```bash
|
|
90
|
+
create-branch push
|
|
91
|
+
# 或使用别名
|
|
92
|
+
cb push
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
**说明**:
|
|
96
|
+
- 会自动添加所有更改到暂存区
|
|
97
|
+
- 提交信息格式:`需求唯一标识:需求标题`(例如:`PROJ-123:用户登录功能`)
|
|
98
|
+
- 自动推送到远程仓库的当前分支
|
|
99
|
+
|
|
100
|
+
### 重置用户信息
|
|
101
|
+
|
|
102
|
+
如果需要修改工号或姓名,可以使用以下命令:
|
|
103
|
+
|
|
104
|
+
```bash
|
|
105
|
+
create-branch reset
|
|
106
|
+
# 或
|
|
107
|
+
create-branch config
|
|
108
|
+
# 或使用别名
|
|
109
|
+
cb reset
|
|
110
|
+
# 或
|
|
111
|
+
cb config
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
**说明**:
|
|
115
|
+
- 会提示输入新的工号和姓名
|
|
116
|
+
- 重置后,之前保存的需求信息会保留
|
|
117
|
+
- 新创建的分支将使用新的工号和姓名
|
|
118
|
+
|
|
72
119
|
### 查看帮助
|
|
73
120
|
|
|
74
121
|
```bash
|
|
@@ -79,18 +126,31 @@ cb
|
|
|
79
126
|
|
|
80
127
|
## 配置
|
|
81
128
|
|
|
82
|
-
|
|
129
|
+
用户信息和需求信息保存在 `~/.create-branch/config.json` 文件中,格式如下:
|
|
83
130
|
|
|
84
131
|
```json
|
|
85
132
|
{
|
|
86
133
|
"workId": "001",
|
|
87
|
-
"name": "张三"
|
|
134
|
+
"name": "张三",
|
|
135
|
+
"requirements": {
|
|
136
|
+
"feature/001_张三_用户登录功能_20251226": {
|
|
137
|
+
"id": "PROJ-123",
|
|
138
|
+
"title": "用户登录功能",
|
|
139
|
+
"branchName": "feature/001_张三_用户登录功能_20251226"
|
|
140
|
+
}
|
|
141
|
+
}
|
|
88
142
|
}
|
|
89
143
|
```
|
|
90
144
|
|
|
145
|
+
**配置说明**:
|
|
146
|
+
- `workId`: 工号
|
|
147
|
+
- `name`: 姓名
|
|
148
|
+
- `requirements`: 需求信息(以分支名为 key,自动保存)
|
|
149
|
+
|
|
91
150
|
如果需要修改用户信息,可以:
|
|
92
|
-
1.
|
|
93
|
-
2.
|
|
151
|
+
1. 使用 `cb reset` 或 `cb config` 命令(推荐)
|
|
152
|
+
2. 直接编辑配置文件
|
|
153
|
+
3. 删除配置文件,下次使用时重新录入
|
|
94
154
|
|
|
95
155
|
## 注意事项
|
|
96
156
|
|
|
@@ -98,6 +158,8 @@ cb
|
|
|
98
158
|
2. 如果分支已存在,工具会提示是否切换到已存在的分支
|
|
99
159
|
3. 需求标题中的空格会自动替换为下划线
|
|
100
160
|
4. 日期格式为 YYYYMMDD(例如:20251226)
|
|
161
|
+
5. 使用 `cb push` 前,请确保创建分支时输入了需求唯一标识
|
|
162
|
+
6. `cb push` 会自动添加所有更改并提交,提交信息格式:`需求唯一标识:需求标题`
|
|
101
163
|
|
|
102
164
|
## 许可证
|
|
103
165
|
|
package/index.js
CHANGED
|
@@ -135,6 +135,30 @@ function branchExists(branchName) {
|
|
|
135
135
|
}
|
|
136
136
|
}
|
|
137
137
|
|
|
138
|
+
// 获取当前分支名
|
|
139
|
+
function getCurrentBranch() {
|
|
140
|
+
try {
|
|
141
|
+
const branch = execSync('git branch --show-current', { encoding: 'utf-8' }).trim();
|
|
142
|
+
return branch;
|
|
143
|
+
} catch (err) {
|
|
144
|
+
error('无法获取当前分支');
|
|
145
|
+
throw err;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// 保存需求信息到配置
|
|
150
|
+
function saveRequirementInfo(config, branchName, requirementId, title) {
|
|
151
|
+
if (!config.requirements) {
|
|
152
|
+
config.requirements = {};
|
|
153
|
+
}
|
|
154
|
+
config.requirements[branchName] = {
|
|
155
|
+
id: requirementId,
|
|
156
|
+
title: title,
|
|
157
|
+
branchName: branchName
|
|
158
|
+
};
|
|
159
|
+
saveConfig(config);
|
|
160
|
+
}
|
|
161
|
+
|
|
138
162
|
// 创建并切换到新分支
|
|
139
163
|
function createBranch(branchName) {
|
|
140
164
|
try {
|
|
@@ -178,33 +202,149 @@ function createBranch(branchName) {
|
|
|
178
202
|
}
|
|
179
203
|
}
|
|
180
204
|
|
|
205
|
+
// 重置用户信息
|
|
206
|
+
async function resetUserInfo() {
|
|
207
|
+
try {
|
|
208
|
+
console.log('\n=== 重置用户信息 ===\n');
|
|
209
|
+
|
|
210
|
+
const workId = await question('请输入新的工号: ');
|
|
211
|
+
if (!workId || !workId.trim()) {
|
|
212
|
+
error('工号不能为空');
|
|
213
|
+
return false;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const name = await question('请输入新的姓名: ');
|
|
217
|
+
if (!name || !name.trim()) {
|
|
218
|
+
error('姓名不能为空');
|
|
219
|
+
return false;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// 读取现有配置(保留需求信息)
|
|
223
|
+
let config = readConfig() || {};
|
|
224
|
+
|
|
225
|
+
// 更新用户信息
|
|
226
|
+
config.workId = workId.trim();
|
|
227
|
+
config.name = name.trim();
|
|
228
|
+
|
|
229
|
+
if (saveConfig(config)) {
|
|
230
|
+
success('用户信息已更新!');
|
|
231
|
+
console.log(`工号: ${config.workId}`);
|
|
232
|
+
console.log(`姓名: ${config.name}`);
|
|
233
|
+
return true;
|
|
234
|
+
} else {
|
|
235
|
+
error('保存用户信息失败');
|
|
236
|
+
return false;
|
|
237
|
+
}
|
|
238
|
+
} catch (err) {
|
|
239
|
+
error(`重置用户信息失败: ${err.message}`);
|
|
240
|
+
return false;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// 提交并推送代码
|
|
245
|
+
function commitAndPush() {
|
|
246
|
+
try {
|
|
247
|
+
// 检查是否在 Git 仓库中
|
|
248
|
+
if (!isGitRepo()) {
|
|
249
|
+
error('当前目录不是 Git 仓库!');
|
|
250
|
+
return false;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// 获取当前分支
|
|
254
|
+
const currentBranch = getCurrentBranch();
|
|
255
|
+
log(`当前分支: ${currentBranch}`);
|
|
256
|
+
|
|
257
|
+
// 读取配置,获取需求信息
|
|
258
|
+
const config = readConfig();
|
|
259
|
+
if (!config || !config.requirements || !config.requirements[currentBranch]) {
|
|
260
|
+
error('当前分支没有保存需求信息!');
|
|
261
|
+
error('请使用以下格式创建分支:cb <需求标题> <需求唯一标识>');
|
|
262
|
+
return false;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
const requirement = config.requirements[currentBranch];
|
|
266
|
+
const commitMessage = `${requirement.id}:${requirement.title}`;
|
|
267
|
+
|
|
268
|
+
// 检查是否有变更
|
|
269
|
+
try {
|
|
270
|
+
const status = execSync('git status --porcelain', { encoding: 'utf-8' });
|
|
271
|
+
if (!status.trim()) {
|
|
272
|
+
log('没有文件变更,无需提交');
|
|
273
|
+
return true;
|
|
274
|
+
}
|
|
275
|
+
} catch (err) {
|
|
276
|
+
error('无法获取 Git 状态');
|
|
277
|
+
return false;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// 添加所有更改
|
|
281
|
+
log('添加所有更改到暂存区...');
|
|
282
|
+
execSync('git add .', { stdio: 'inherit' });
|
|
283
|
+
|
|
284
|
+
// 提交
|
|
285
|
+
log(`提交更改: ${commitMessage}`);
|
|
286
|
+
execSync(`git commit -m "${commitMessage}"`, { stdio: 'inherit' });
|
|
287
|
+
|
|
288
|
+
// 推送
|
|
289
|
+
log('推送到远程仓库...');
|
|
290
|
+
execSync(`git push origin ${currentBranch}`, { stdio: 'inherit' });
|
|
291
|
+
|
|
292
|
+
success(`已提交并推送到分支: ${currentBranch}`);
|
|
293
|
+
return true;
|
|
294
|
+
} catch (err) {
|
|
295
|
+
error(`提交或推送失败: ${err.message}`);
|
|
296
|
+
return false;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
181
300
|
// 主函数
|
|
182
301
|
async function main() {
|
|
183
302
|
try {
|
|
184
303
|
// 解析命令行参数
|
|
185
304
|
const args = process.argv.slice(2);
|
|
186
305
|
|
|
306
|
+
// 检查是否是 push 命令
|
|
307
|
+
if (args.length > 0 && args[0].toLowerCase() === 'push') {
|
|
308
|
+
const success = commitAndPush();
|
|
309
|
+
process.exit(success ? 0 : 1);
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// 检查是否是 reset 命令
|
|
314
|
+
if (args.length > 0 && (args[0].toLowerCase() === 'reset' || args[0].toLowerCase() === 'config')) {
|
|
315
|
+
const success = await resetUserInfo();
|
|
316
|
+
process.exit(success ? 0 : 1);
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
|
|
187
320
|
// 检查是否是 bug 类型
|
|
188
321
|
const isBug = args.includes('-b') || args.includes('--bug');
|
|
189
322
|
|
|
190
|
-
//
|
|
323
|
+
// 移除标志参数,获取需求标题和需求唯一标识
|
|
191
324
|
const titleArgs = args.filter(arg => arg !== '-b' && arg !== '--bug');
|
|
192
325
|
|
|
193
326
|
if (titleArgs.length === 0) {
|
|
194
327
|
console.log('\n用法:');
|
|
195
|
-
console.log(' create-branch <需求标题>
|
|
196
|
-
console.log(' create-branch <需求标题>
|
|
197
|
-
console.log(' create-branch <需求标题>
|
|
328
|
+
console.log(' create-branch <需求标题> # 创建 feature 分支');
|
|
329
|
+
console.log(' create-branch <需求标题> <需求唯一标识> # 创建 feature 分支并保存需求信息');
|
|
330
|
+
console.log(' create-branch <需求标题> -b # 创建 hotFix 分支');
|
|
331
|
+
console.log(' create-branch <需求标题> <需求唯一标识> -b # 创建 hotFix 分支并保存需求信息');
|
|
332
|
+
console.log(' create-branch push # 提交并推送代码(使用保存的需求信息)');
|
|
333
|
+
console.log(' create-branch reset # 重置工号和姓名');
|
|
198
334
|
console.log('\n示例:');
|
|
199
335
|
console.log(' create-branch 用户登录功能');
|
|
336
|
+
console.log(' create-branch 用户登录功能 PROJ-123');
|
|
200
337
|
console.log(' create-branch 修复登录bug -b');
|
|
338
|
+
console.log(' create-branch 修复登录bug PROJ-124 -b');
|
|
339
|
+
console.log(' create-branch push');
|
|
340
|
+
console.log(' create-branch reset');
|
|
201
341
|
console.log('\n别名:');
|
|
202
|
-
console.log(' cb <需求标题> [-b|--bug]');
|
|
342
|
+
console.log(' cb <需求标题> [需求唯一标识] [-b|--bug]');
|
|
343
|
+
console.log(' cb push');
|
|
344
|
+
console.log(' cb reset 或 cb config');
|
|
203
345
|
process.exit(0);
|
|
204
346
|
}
|
|
205
347
|
|
|
206
|
-
const title = titleArgs.join(' ');
|
|
207
|
-
|
|
208
348
|
// 检查是否在 Git 仓库中
|
|
209
349
|
if (!isGitRepo()) {
|
|
210
350
|
error('当前目录不是 Git 仓库!');
|
|
@@ -217,12 +357,34 @@ async function main() {
|
|
|
217
357
|
config = await initUserInfo();
|
|
218
358
|
}
|
|
219
359
|
|
|
360
|
+
// 解析需求标题和需求唯一标识
|
|
361
|
+
// titleArgs 已经过滤掉了 -b 和 --bug
|
|
362
|
+
// 如果参数数量 >= 2,则最后一个参数是需求唯一标识
|
|
363
|
+
let title, requirementId;
|
|
364
|
+
if (titleArgs.length >= 2) {
|
|
365
|
+
// 最后一个参数是需求唯一标识
|
|
366
|
+
requirementId = titleArgs[titleArgs.length - 1];
|
|
367
|
+
title = titleArgs.slice(0, -1).join(' ');
|
|
368
|
+
} else {
|
|
369
|
+
// 只有需求标题
|
|
370
|
+
title = titleArgs.join(' ');
|
|
371
|
+
requirementId = null;
|
|
372
|
+
}
|
|
373
|
+
|
|
220
374
|
// 生成分支名
|
|
221
375
|
const branchName = generateBranchName(config.workId, config.name, title, isBug);
|
|
376
|
+
log(`生成的分支名: ${branchName}`);
|
|
222
377
|
|
|
223
378
|
// 创建分支
|
|
224
379
|
await createBranch(branchName);
|
|
225
380
|
|
|
381
|
+
// 如果提供了需求唯一标识,保存到配置
|
|
382
|
+
if (requirementId) {
|
|
383
|
+
saveRequirementInfo(config, branchName, requirementId.trim(), title);
|
|
384
|
+
success(`已保存需求信息: ${requirementId}:${title}`);
|
|
385
|
+
log('可以使用 "cb push" 快速提交和推送代码');
|
|
386
|
+
}
|
|
387
|
+
|
|
226
388
|
} catch (err) {
|
|
227
389
|
error(err.message);
|
|
228
390
|
process.exit(1);
|