从用户输入中提取:
信息提取原则:
如果用户没有指定提交类型,根据变更描述自动检测类型:
| 类型 | 关键词检测 | 说明 |
|---|---|---|
| ------ | ----------- | ------ |
| feat | 新增、添加、feature、新功能、实现、add、implement、introduce | 新功能、新特性 |
| fix | 修复、bug、错误、修复、fix、bugfix、resolve、correct | Bug 修复、问题解决 |
| docs | 文档、README、注释、doc、documentation、readme、comment | 文档更新、注释修改 |
| style | 格式、格式化、缩进、空格、style、format、indent、whitespace | 代码格式调整、不影响功能 |
| refactor | 重构、优化、改进、refactor、optimize、improve、restructure | 代码重构、不改变外部行为 |
| test | 测试、测试用例、test、spec、unittest、e2e | 测试相关变更 |
| chore | 构建、依赖、配置、chore、build、dependency、config、setup | 构建工具、依赖更新 |
检测规则:
feat 或让用户选择核心原则:如果不在 Git 仓库中,直接基于用户输入生成 commit 消息,不执行任何 git 相关操作。
详细流程:
git rev-parse --is-inside-work-tree 2>/dev/nullgit diff 获取当前变更根据提取的信息和规范,生成高质量的 commit 消息。
提示词模板(Conventional Commits):
请根据以下代码变更生成一个符合 Conventional Commits 规范的 Git commit 消息。
变更描述:
{变更描述}
{如果有 git diff,也提供 diff 内容}
要求:
1. 使用类型:{检测到的类型或用户指定的类型}
2. 标题简洁明了(不超过 50 字符)
3. 如有需要可添加详细描述(body)
4. 使用中文或英文,与用户输入语言一致
5. 直接输出 commit 消息,不要额外解释
Gitmoji 规范提示词:
请根据以下代码变更生成一个使用 Gitmoji 的 Git commit 消息。
变更描述:
{变更描述}
Gitmoji 对应关系:
✨ feat - 新功能
🐛 fix - Bug 修复
📚 docs - 文档更新
💎 style - 代码格式
♻️ refactor - 重构
🧪 test - 测试
🔧 chore - 构建/工具
要求:
1. 使用对应的 Gitmoji 开头
2. 标题简洁明了
3. 直接输出 commit 消息
生成4种版本供用户选择:
🎯 生成的 Commit 消息(4个版本)
【版本 1】精简中文(≤20字符)
{精简中文commit消息}
【版本 2】精简英文(≤20字符)
{精简英文commit消息}
【版本 3】详细中文
{详细中文commit消息}
【版本 4】详细英文
{详细英文commit消息}
📋 说明
- 类型:{type}
- 规范:{Conventional Commits/Angular/Gitmoji}
- 检测方式:{自动检测/用户指定}
💡 选择您喜欢的版本,或告诉我:
- "用版本1"
- "再生成一组"
- "改成 Gitmoji 格式"
示例输出:
🎯 生成的 Commit 消息(4个版本)
【版本 1】精简中文(≤20字符)
feat: 添加登录功能
【版本 2】精简英文(≤20字符)
feat: add login feature
【版本 3】详细中文
feat(auth): 添加用户登录功能
- 支持邮箱/密码登录
- 支持第三方登录(GitHub/Google)
- 添加登录状态管理
【版本 4】详细英文
feat(auth): add user login feature
- support email/password login
- support third-party login (GitHub/Google)
- add login state management
📋 说明
- 类型:feat
- 规范:Conventional Commits
- 检测方式:自动检测
💡 选择您喜欢的版本,或告诉我:
- "用版本1"
- "再生成一组"
- "改成 Gitmoji 格式"
<type>(<scope>): <subject>
<body>
<footer>
type:feat、fix、docs、style、refactor、test、chore
scope:可选,影响的范围(如 auth、api、ui)
subject:简洁描述(不超过 50 字符)
body:可选,详细说明
footer:可选,关联 issue 等
示例:
feat(auth): 添加用户登录功能
- 支持邮箱/密码登录
- 支持第三方登录
- 添加登录状态管理
Closes #123
与 Conventional Commits 类似,但有更严格的要求:
示例:
feat(auth): add user login functionality
使用 emoji 开头,更直观:
{emoji} {subject}
Gitmoji 对照表:
| Emoji | Type | 说明 |
|---|---|---|
| ------- | ------ | ------ |
| ✨ | feat | 新功能 |
| 🐛 | fix | Bug 修复 |
| 📚 | docs | 文档更新 |
| 💎 | style | 代码格式 |
| ♻️ | refactor | 重构 |
| 🧪 | test | 测试 |
| 🔧 | chore | 构建/工具 |
示例:
✨ 添加用户登录功能
根据以下变更生成 commit 消息:
{变更描述}
生成一个 {type} 类型的 commit 消息:
{变更描述}
用 {规范} 格式生成 commit 消息:
{变更描述}
根据 git diff 生成 commit 消息:
{git diff 内容}
用户输入:"我添加了用户登录功能,支持邮箱密码和第三方登录"
输出:
🎯 生成的 Commit 消息(4个版本)
【版本 1】精简中文(≤20字符)
feat: 添加登录功能
【版本 2】精简英文(≤20字符)
feat: add login feature
【版本 3】详细中文
feat(auth): 添加用户登录功能
- 支持邮箱/密码登录
- 支持第三方登录(GitHub/Google)
- 添加登录状态管理
【版本 4】详细英文
feat(auth): add user login feature
- support email/password login
- support third-party login (GitHub/Google)
- add login state management
📋 说明
- 类型:feat
- 规范:Conventional Commits
- 检测方式:自动检测
💡 选择您喜欢的版本,或告诉我:
- "用版本1"
- "再生成一组"
- "改成 Gitmoji 格式"
用户输入:"修复了登录页面在移动端崩溃的问题"
输出:
🎯 生成的 Commit 消息(4个版本)
【版本 1】精简中文(≤20字符)
fix: 修复登录崩溃
【版本 2】精简英文(≤20字符)
fix: fix login crash
【版本 3】详细中文
fix(auth): 修复登录页面移动端崩溃
- 修复移动端布局问题
- 添加响应式断点
- 优化触摸事件处理
【版本 4】详细英文
fix(auth): fix login page mobile crash
- fix mobile layout issues
- add responsive breakpoints
- optimize touch event handling
📋 说明
- 类型:fix
- 规范:Conventional Commits
- 检测方式:自动检测
💡 选择您喜欢的版本,或告诉我:
- "用版本1"
- "再生成一组"
- "改成 Gitmoji 格式"
用户输入:"用 Gitmoji 格式,更新了 README 文档"
输出:
🎯 生成的 Commit 消息(4个版本)
【版本 1】精简中文(≤20字符)
📚 更新文档
【版本 2】精简英文(≤20字符)
📚 update docs
【版本 3】详细中文
📚 更新 README 文档
- 添加快速开始指南
- 更新 API 文档
- 补充常见问题
【版本 4】详细英文
📚 update README documentation
- add quick start guide
- update API documentation
- add FAQs
📋 说明
- 类型:docs
- 规范:Gitmoji
- 检测方式:用户指定
💡 选择您喜欢的版本,或告诉我:
- "用版本1"
- "再生成一组"
- "改成 Conventional Commits 格式"
获取当前 git diff 的脚本:
#!/bin/bash
# 获取当前 git diff
git diff --staged # 暂存区
# 或者
git diff HEAD # 工作区
Python 参考脚本(可选使用):
#!/usr/bin/env python3
"""
Commit 消息生成参考脚本
使用示例:python generate-commit.py "添加用户登录功能"
"""
import sys
def detect_type(description):
"""自动检测提交类型"""
keywords = {
'feat': ['新增', '添加', 'feature', '新功能', '实现', 'add', 'implement', 'introduce'],
'fix': ['修复', 'bug', '错误', 'fix', 'bugfix', 'resolve', 'correct'],
'docs': ['文档', 'README', '注释', 'doc', 'documentation', 'readme', 'comment'],
'style': ['格式', '格式化', '缩进', '空格', 'style', 'format', 'indent', 'whitespace'],
'refactor': ['重构', '优化', '改进', 'refactor', 'optimize', 'improve', 'restructure'],
'test': ['测试', '测试用例', 'test', 'spec', 'unittest', 'e2e'],
'chore': ['构建', '依赖', '配置', 'chore', 'build', 'dependency', 'config', 'setup'],
}
description_lower = description.lower()
for commit_type, words in keywords.items():
if any(word in description_lower for word in words):
return commit_type
return 'feat'
def generate_commit(description, commit_type=None, convention='conventional'):
"""生成 commit 消息"""
if not commit_type:
commit_type = detect_type(description)
if convention == 'gitmoji':
gitmojis = {
'feat': '✨',
'fix': '🐛',
'docs': '📚',
'style': '💎',
'refactor': '♻️',
'test': '🧪',
'chore': '🔧',
}
emoji = gitmojis.get(commit_type, '✨')
return f"{emoji} {description}"
else:
return f"{commit_type}: {description}"
if __name__ == '__main__':
if len(sys.argv) < 2:
print("Usage: python generate-commit.py <description> [type] [convention]")
sys.exit(1)
description = sys.argv[1]
commit_type = sys.argv[2] if len(sys.argv) > 2 else None
convention = sys.argv[3] if len(sys.argv) > 3 else 'conventional'
commit_msg = generate_commit(description, commit_type, convention)
print(commit_msg)
| 场景 | 推荐类型 | 示例 |
|---|---|---|
| ------ | --------- | ------ |
| 添加新功能 | feat | feat: 添加用户登录功能 |
| 修复 Bug | fix | fix: 修复登录页面崩溃 |
| 更新文档 | docs | docs: 更新 API 文档 |
| 调整格式 | style | style: 格式化代码 |
| 代码重构 | refactor | refactor: 重构认证模块 |
| 添加测试 | test | test: 添加单元测试 |
| 更新依赖 | chore | chore: 更新依赖版本 |
共 1 个版本