@zjex/git-workflow 0.3.9 → 0.4.0

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/src/ai-service.ts CHANGED
@@ -60,7 +60,12 @@ function getGitDiff(): string {
60
60
  /**
61
61
  * 构建 AI prompt
62
62
  */
63
- function buildPrompt(diff: string, language: string, detailedDescription: boolean = false, useEmoji: boolean = true): string {
63
+ function buildPrompt(
64
+ diff: string,
65
+ language: string,
66
+ detailedDescription: boolean = false,
67
+ useEmoji: boolean = true
68
+ ): string {
64
69
  const isZh = language === "zh-CN";
65
70
 
66
71
  if (detailedDescription) {
@@ -69,7 +74,7 @@ function buildPrompt(diff: string, language: string, detailedDescription: boolea
69
74
  ? `你是一个专业的 Git commit message 生成助手。请根据提供的 git diff 生成符合 Conventional Commits 规范的详细 commit message。
70
75
 
71
76
  格式要求:
72
- 1. 第一行:${useEmoji ? '<emoji> ' : ''}<type>(<scope>): <subject>
77
+ 1. 第一行:${useEmoji ? "<emoji> " : ""}<type>(<scope>): <subject>
73
78
  2. 空行
74
79
  3. 详细描述:列出主要修改点,每个修改点一行,以 "- " 开头
75
80
 
@@ -80,7 +85,9 @@ function buildPrompt(diff: string, language: string, detailedDescription: boolea
80
85
  - 详细描述要列出 3-6 个主要修改点,每个修改点简洁明了
81
86
  - 如果修改较少,可以只列出 2-3 个修改点
82
87
  - 不要有其他解释或多余内容
83
- ${useEmoji ? `
88
+ ${
89
+ useEmoji
90
+ ? `
84
91
  Emoji 映射规则:
85
92
  - feat: ✨ (新功能)
86
93
  - fix: 🐛 (修复Bug)
@@ -92,10 +99,12 @@ Emoji 映射规则:
92
99
  - build: 📦 (构建)
93
100
  - ci: 👷 (CI/CD)
94
101
  - chore: 🔧 (其他杂项)
95
- - revert: ⏪ (回滚)` : ''}
102
+ - revert: ⏪ (回滚)`
103
+ : ""
104
+ }
96
105
 
97
106
  示例:
98
- ${useEmoji ? '' : ''}feat(auth): 添加用户登录功能
107
+ ${useEmoji ? "" : ""}feat(auth): 添加用户登录功能
99
108
 
100
109
  - 实现用户名密码登录接口
101
110
  - 添加登录状态验证中间件
@@ -104,7 +113,7 @@ ${useEmoji ? '✨ ' : ''}feat(auth): 添加用户登录功能
104
113
  : `You are a professional Git commit message generator. Generate a detailed commit message following Conventional Commits specification based on the provided git diff.
105
114
 
106
115
  Format requirements:
107
- 1. First line: ${useEmoji ? '<emoji> ' : ''}<type>(<scope>): <subject>
116
+ 1. First line: ${useEmoji ? "<emoji> " : ""}<type>(<scope>): <subject>
108
117
  2. Empty line
109
118
  3. Detailed description: List main changes, one per line, starting with "- "
110
119
 
@@ -115,7 +124,9 @@ Rules:
115
124
  - Detailed description should list 3-6 main changes, each change should be concise
116
125
  - If changes are minimal, list 2-3 changes
117
126
  - No explanations or extra content
118
- ${useEmoji ? `
127
+ ${
128
+ useEmoji
129
+ ? `
119
130
  Emoji mapping rules:
120
131
  - feat: ✨ (new feature)
121
132
  - fix: 🐛 (bug fix)
@@ -127,10 +138,12 @@ Emoji mapping rules:
127
138
  - build: 📦 (build)
128
139
  - ci: 👷 (CI/CD)
129
140
  - chore: 🔧 (chore)
130
- - revert: ⏪ (revert)` : ''}
141
+ - revert: ⏪ (revert)`
142
+ : ""
143
+ }
131
144
 
132
145
  Example:
133
- ${useEmoji ? '' : ''}feat(auth): add user login functionality
146
+ ${useEmoji ? "" : ""}feat(auth): add user login functionality
134
147
 
135
148
  - Implement username/password login API
136
149
  - Add login status validation middleware
@@ -148,13 +161,15 @@ ${useEmoji ? '✨ ' : ''}feat(auth): add user login functionality
148
161
  ? `你是一个专业的 Git commit message 生成助手。请根据提供的 git diff 生成符合 Conventional Commits 规范的 commit message。
149
162
 
150
163
  规则:
151
- 1. 格式:${useEmoji ? '<emoji> ' : ''}<type>(<scope>): <subject>
164
+ 1. 格式:${useEmoji ? "<emoji> " : ""}<type>(<scope>): <subject>
152
165
  2. type 必须是以下之一:feat, fix, docs, style, refactor, perf, test, build, ci, chore, revert
153
166
  3. scope 是可选的,表示影响范围
154
167
  4. subject 用中文描述,简洁明了,不超过 50 字
155
168
  5. 只返回一条 commit message,即使有多个文件改动也要总结成一条
156
169
  6. 不要有其他解释或多余内容
157
- ${useEmoji ? `
170
+ ${
171
+ useEmoji
172
+ ? `
158
173
  Emoji 映射规则:
159
174
  - feat: ✨ (新功能)
160
175
  - fix: 🐛 (修复Bug)
@@ -166,22 +181,26 @@ Emoji 映射规则:
166
181
  - build: 📦 (构建)
167
182
  - ci: 👷 (CI/CD)
168
183
  - chore: 🔧 (其他杂项)
169
- - revert: ⏪ (回滚)` : ''}
184
+ - revert: ⏪ (回滚)`
185
+ : ""
186
+ }
170
187
 
171
188
  示例:
172
- - ${useEmoji ? '' : ''}feat(auth): 添加用户登录功能
173
- - ${useEmoji ? '🐛 ' : ''}fix(api): 修复数据获取失败的问题
174
- - ${useEmoji ? '📝 ' : ''}docs(readme): 更新安装说明`
189
+ - ${useEmoji ? "" : ""}feat(auth): 添加用户登录功能
190
+ - ${useEmoji ? "🐛 " : ""}fix(api): 修复数据获取失败的问题
191
+ - ${useEmoji ? "📝 " : ""}docs(readme): 更新安装说明`
175
192
  : `You are a professional Git commit message generator. Generate a commit message following Conventional Commits specification based on the provided git diff.
176
193
 
177
194
  Rules:
178
- 1. Format: ${useEmoji ? '<emoji> ' : ''}<type>(<scope>): <subject>
195
+ 1. Format: ${useEmoji ? "<emoji> " : ""}<type>(<scope>): <subject>
179
196
  2. type must be one of: feat, fix, docs, style, refactor, perf, test, build, ci, chore, revert
180
197
  3. scope is optional, indicates the affected area
181
198
  4. subject should be concise, no more than 50 characters
182
199
  5. Return only ONE commit message, even if multiple files are changed, summarize into one message
183
200
  6. No explanations or extra content
184
- ${useEmoji ? `
201
+ ${
202
+ useEmoji
203
+ ? `
185
204
  Emoji mapping rules:
186
205
  - feat: ✨ (new feature)
187
206
  - fix: 🐛 (bug fix)
@@ -193,12 +212,14 @@ Emoji mapping rules:
193
212
  - build: 📦 (build)
194
213
  - ci: 👷 (CI/CD)
195
214
  - chore: 🔧 (chore)
196
- - revert: ⏪ (revert)` : ''}
215
+ - revert: ⏪ (revert)`
216
+ : ""
217
+ }
197
218
 
198
219
  Examples:
199
- - ${useEmoji ? '' : ''}feat(auth): add user login functionality
200
- - ${useEmoji ? '🐛 ' : ''}fix(api): resolve data fetching failure
201
- - ${useEmoji ? '📝 ' : ''}docs(readme): update installation guide`;
220
+ - ${useEmoji ? "" : ""}feat(auth): add user login functionality
221
+ - ${useEmoji ? "🐛 " : ""}fix(api): resolve data fetching failure
222
+ - ${useEmoji ? "📝 " : ""}docs(readme): update installation guide`;
202
223
 
203
224
  const userPrompt = isZh
204
225
  ? `请根据以下 git diff 生成 commit message:\n\n${diff}`
@@ -347,25 +368,29 @@ async function callOllamaAPI(
347
368
  */
348
369
  function cleanAIResponse(response: string): string {
349
370
  // 移除开头的特殊字符(如 ...、```、等)
350
- let cleaned = response.replace(/^[.\s`~-]+/, '').trim();
351
-
371
+ let cleaned = response.replace(/^[.\s`~-]+/, "").trim();
372
+
352
373
  // 移除结尾的特殊字符
353
- cleaned = cleaned.replace(/[.\s`~-]+$/, '').trim();
354
-
355
- const lines = cleaned.split('\n').map(line => line.trim()).filter(line => line);
356
-
374
+ cleaned = cleaned.replace(/[.\s`~-]+$/, "").trim();
375
+
376
+ // 分割成行并过滤空行
377
+ const lines = cleaned
378
+ .split("\n")
379
+ .map((line) => line.trim())
380
+ .filter((line) => line);
381
+
357
382
  // 移除重复的行
358
383
  const uniqueLines = [];
359
384
  const seen = new Set();
360
-
385
+
361
386
  for (const line of lines) {
362
387
  if (!seen.has(line)) {
363
388
  seen.add(line);
364
389
  uniqueLines.push(line);
365
390
  }
366
391
  }
367
-
368
- return uniqueLines.join('\n');
392
+
393
+ return uniqueLines.join("\n");
369
394
  }
370
395
 
371
396
  /**
@@ -379,9 +404,12 @@ export async function generateAICommitMessage(
379
404
  const language = aiConfig.language || "zh-CN";
380
405
  const detailedDescription = aiConfig.detailedDescription !== false; // 默认启用详细描述
381
406
  const maxTokens = aiConfig.maxTokens || (detailedDescription ? 400 : 200);
382
-
407
+
383
408
  // AI emoji配置:优先使用aiCommit.useEmoji,如果未设置则使用全局useEmoji,默认true
384
- const useEmoji = aiConfig.useEmoji !== undefined ? aiConfig.useEmoji : (config.useEmoji !== false);
409
+ const useEmoji =
410
+ aiConfig.useEmoji !== undefined
411
+ ? aiConfig.useEmoji
412
+ : config.useEmoji !== false;
385
413
 
386
414
  // 获取 git diff
387
415
  const diff = getGitDiff();
@@ -395,7 +423,12 @@ export async function generateAICommitMessage(
395
423
  diff.length > maxDiffLength ? diff.slice(0, maxDiffLength) + "\n..." : diff;
396
424
 
397
425
  // 构建 prompt
398
- const prompt = buildPrompt(truncatedDiff, language, detailedDescription, useEmoji);
426
+ const prompt = buildPrompt(
427
+ truncatedDiff,
428
+ language,
429
+ detailedDescription,
430
+ useEmoji
431
+ );
399
432
 
400
433
  // 根据提供商调用对应的 API
401
434
  const providerInfo = AI_PROVIDERS[provider];
@@ -624,3 +624,123 @@ export async function updateTag(): Promise<void> {
624
624
  }
625
625
  }
626
626
  }
627
+
628
+ /**
629
+ * 清理无效标签(不包含数字的标签)
630
+ */
631
+ export async function cleanInvalidTags(): Promise<void> {
632
+ const fetchSpinner = ora("正在获取 tags...").start();
633
+ exec("git fetch --tags", true);
634
+ fetchSpinner.stop();
635
+
636
+ divider();
637
+
638
+ // 获取所有标签
639
+ const allTags = execOutput("git tag -l").split("\n").filter(Boolean);
640
+
641
+ // 过滤出无效标签(不包含数字)
642
+ const invalidTags = allTags.filter((tag) => !/\d/.test(tag));
643
+
644
+ if (invalidTags.length === 0) {
645
+ console.log(colors.green("✅ 没有找到无效标签"));
646
+ return;
647
+ }
648
+
649
+ console.log(colors.yellow(`❌ 找到 ${invalidTags.length} 个无效标签:`));
650
+ console.log("");
651
+
652
+ // 显示每个无效标签的详细信息
653
+ for (const tag of invalidTags) {
654
+ try {
655
+ const commitHash = execOutput(`git rev-list -n 1 "${tag}"`).trim();
656
+ const commitDate = execOutput(`git log -1 --format=%ai "${tag}"`).trim();
657
+ const commitMsg = execOutput(`git log -1 --format=%s "${tag}"`).trim();
658
+
659
+ console.log(colors.red(` 标签: ${tag}`));
660
+ console.log(colors.dim(` Commit: ${commitHash}`));
661
+ console.log(colors.dim(` 日期: ${commitDate}`));
662
+ console.log(colors.dim(` 消息: ${commitMsg}`));
663
+ console.log("");
664
+ } catch {
665
+ console.log(colors.red(` 标签: ${tag}`));
666
+ console.log(colors.dim(` (无法获取提交信息)`));
667
+ console.log("");
668
+ }
669
+ }
670
+
671
+ divider();
672
+
673
+ const shouldClean = await select({
674
+ message: "是否删除这些无效标签?",
675
+ choices: [
676
+ { name: "是,删除所有无效标签", value: true },
677
+ { name: "否,取消操作", value: false },
678
+ ],
679
+ theme,
680
+ });
681
+
682
+ if (!shouldClean) {
683
+ console.log(colors.yellow("已取消"));
684
+ return;
685
+ }
686
+
687
+ divider();
688
+
689
+ // 删除本地标签
690
+ const localSpinner = ora("正在删除本地无效标签...").start();
691
+ let localSuccess = 0;
692
+ let localFailed = 0;
693
+
694
+ for (const tag of invalidTags) {
695
+ try {
696
+ execSync(`git tag -d "${tag}"`, { stdio: "pipe" });
697
+ localSuccess++;
698
+ } catch {
699
+ localFailed++;
700
+ }
701
+ }
702
+
703
+ if (localFailed === 0) {
704
+ localSpinner.succeed(`本地标签已删除: ${localSuccess} 个`);
705
+ } else {
706
+ localSpinner.warn(
707
+ `本地标签删除: 成功 ${localSuccess} 个,失败 ${localFailed} 个`
708
+ );
709
+ }
710
+
711
+ // 询问是否删除远程标签
712
+ const deleteRemote = await select({
713
+ message: "是否同时删除远程的无效标签?",
714
+ choices: [
715
+ { name: "是", value: true },
716
+ { name: "否", value: false },
717
+ ],
718
+ theme,
719
+ });
720
+
721
+ if (deleteRemote) {
722
+ const remoteSpinner = ora("正在删除远程无效标签...").start();
723
+ let remoteSuccess = 0;
724
+ let remoteFailed = 0;
725
+
726
+ for (const tag of invalidTags) {
727
+ try {
728
+ execSync(`git push origin --delete "${tag}"`, { stdio: "pipe" });
729
+ remoteSuccess++;
730
+ } catch {
731
+ remoteFailed++;
732
+ }
733
+ }
734
+
735
+ if (remoteFailed === 0) {
736
+ remoteSpinner.succeed(`远程标签已删除: ${remoteSuccess} 个`);
737
+ } else {
738
+ remoteSpinner.warn(
739
+ `远程标签删除: 成功 ${remoteSuccess} 个,失败 ${remoteFailed} 个`
740
+ );
741
+ }
742
+ }
743
+
744
+ console.log("");
745
+ console.log(colors.green("✨ 清理完成!"));
746
+ }
package/src/index.ts CHANGED
@@ -15,7 +15,13 @@ import { select } from "@inquirer/prompts";
15
15
  import { ExitPromptError } from "@inquirer/core";
16
16
  import { checkGitRepo, theme, colors } from "./utils.js";
17
17
  import { createBranch, deleteBranch } from "./commands/branch.js";
18
- import { listTags, createTag, deleteTag, updateTag } from "./commands/tag.js";
18
+ import {
19
+ listTags,
20
+ createTag,
21
+ deleteTag,
22
+ updateTag,
23
+ cleanInvalidTags,
24
+ } from "./commands/tag.js";
19
25
  import { release } from "./commands/release.js";
20
26
  import { init } from "./commands/init.js";
21
27
  import { stash } from "./commands/stash.js";
@@ -119,7 +125,7 @@ async function mainMenu(): Promise<void> {
119
125
  value: "hotfix",
120
126
  },
121
127
  {
122
- name: `[3] 🗑️ 删除分支 ${colors.dim("gw d")}`,
128
+ name: `[3] 🗑️ 删除分支 ${colors.dim("gw brd")}`,
123
129
  value: "delete",
124
130
  },
125
131
  {
@@ -143,15 +149,15 @@ async function mainMenu(): Promise<void> {
143
149
  value: "tags",
144
150
  },
145
151
  {
146
- name: `[9] 📦 发布版本 ${colors.dim("gw r")}`,
152
+ name: `[9] 发布版本 ${colors.dim("gw r")}`,
147
153
  value: "release",
148
154
  },
149
155
  {
150
- name: `[a] 💾 管理 stash ${colors.dim("gw s")}`,
156
+ name: `[a] 管理 stash ${colors.dim("gw s")}`,
151
157
  value: "stash",
152
158
  },
153
159
  {
154
- name: `[b] 📊 查看日志 ${colors.dim("gw log")}`,
160
+ name: `[b] 查看日志 ${colors.dim("gw log")}`,
155
161
  value: "log",
156
162
  },
157
163
  {
@@ -263,9 +269,8 @@ cli
263
269
  });
264
270
 
265
271
  cli
266
- .command("delete [branch]", "删除本地/远程分支")
267
- .alias("del")
268
- .alias("d")
272
+ .command("br:del [branch]", "删除本地/远程分支")
273
+ .alias("brd")
269
274
  .action(async (branch?: string) => {
270
275
  await checkForUpdates(version, "@zjex/git-workflow");
271
276
  checkGitRepo();
@@ -291,7 +296,7 @@ cli
291
296
  });
292
297
 
293
298
  cli
294
- .command("tag:delete", "删除 tag")
299
+ .command("tag:del", "删除 tag")
295
300
  .alias("td")
296
301
  .action(async () => {
297
302
  await checkForUpdates(version, "@zjex/git-workflow");
@@ -308,6 +313,15 @@ cli
308
313
  return updateTag();
309
314
  });
310
315
 
316
+ cli
317
+ .command("tag:clean", "清理无效 tag")
318
+ .alias("tc")
319
+ .action(async () => {
320
+ await checkForUpdates(version, "@zjex/git-workflow");
321
+ checkGitRepo();
322
+ return cleanInvalidTags();
323
+ });
324
+
311
325
  cli
312
326
  .command("release", "交互式选择版本号并更新 package.json")
313
327
  .alias("r")
@@ -364,13 +378,80 @@ cli
364
378
  return log(logOptions);
365
379
  });
366
380
 
367
- cli.command("clean", "清理缓存文件").action(async () => {
368
- const { clearUpdateCache } = await import("./update-notifier.js");
369
- clearUpdateCache();
370
- console.log("");
371
- console.log(colors.green("✔ 缓存已清理"));
372
- console.log("");
373
- });
381
+ cli
382
+ .command("clean", "清理缓存和临时文件")
383
+ .alias("cc")
384
+ .action(async () => {
385
+ const { clearUpdateCache } = await import("./update-notifier.js");
386
+ const { existsSync, unlinkSync, readdirSync } = await import("fs");
387
+ const { homedir, tmpdir } = await import("os");
388
+ const { join } = await import("path");
389
+ const { select } = await import("@inquirer/prompts");
390
+
391
+ let cleanedCount = 0;
392
+ let deletedGlobalConfig = false;
393
+
394
+ // 检查全局配置文件是否存在
395
+ const globalConfig = join(homedir(), ".gwrc.json");
396
+ const hasGlobalConfig = existsSync(globalConfig);
397
+
398
+ // 如果有全局配置文件,询问是否删除
399
+ if (hasGlobalConfig) {
400
+ const shouldDeleteConfig = await select({
401
+ message: "检测到全局配置文件,是否删除?",
402
+ choices: [
403
+ { name: "否,保留配置文件", value: false },
404
+ { name: "是,删除配置文件", value: true },
405
+ ],
406
+ theme,
407
+ });
408
+
409
+ if (shouldDeleteConfig) {
410
+ try {
411
+ unlinkSync(globalConfig);
412
+ cleanedCount++;
413
+ deletedGlobalConfig = true;
414
+ } catch {
415
+ // 静默失败
416
+ }
417
+ }
418
+ }
419
+
420
+ // 1. 清理更新缓存
421
+ clearUpdateCache();
422
+ cleanedCount++;
423
+
424
+ // 2. 清理临时 commit 消息文件
425
+ try {
426
+ const tmpDir = tmpdir();
427
+ const files = readdirSync(tmpDir);
428
+ const gwTmpFiles = files.filter((f) => f.startsWith(".gw-commit-msg-"));
429
+
430
+ for (const file of gwTmpFiles) {
431
+ try {
432
+ unlinkSync(join(tmpDir, file));
433
+ cleanedCount++;
434
+ } catch {
435
+ // 静默失败
436
+ }
437
+ }
438
+ } catch {
439
+ // 静默失败
440
+ }
441
+
442
+ console.log("");
443
+ console.log(colors.green(`✔ 已清理 ${cleanedCount} 个文件`));
444
+
445
+ if (deletedGlobalConfig) {
446
+ console.log("");
447
+ console.log(colors.yellow("⚠️ 全局配置文件已删除"));
448
+ console.log(
449
+ colors.dim(` 如需重新配置,请运行: ${colors.cyan("gw init")}`)
450
+ );
451
+ }
452
+
453
+ console.log("");
454
+ });
374
455
 
375
456
  // 不使用 cac 的 version,手动处理 --version 和 --help
376
457
  cli.option("-v, --version", "显示版本号");
@@ -181,7 +181,7 @@ async function showUpdateMessage(
181
181
  console.log(
182
182
  boxen(message, {
183
183
  padding: { top: 1, bottom: 1, left: 3, right: 3 },
184
- margin: 1,
184
+ margin: { top: 0, bottom: 0, left: 1, right: 1 },
185
185
  borderStyle: "round",
186
186
  borderColor: "yellow",
187
187
  align: "center",